Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Tests/DatabaseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ protected function setUp(): void
$this->query = new TestDatabaseQuery($this->db);
}

#[TestDox('The bindValue method binds a copy of the value')]
public function testBindValue()
{
$this->assertSame($this->query, $this->query->bindValue(':literal', 'JustValueNoReference'), 'The query builder supports method chaining');

foreach ([1, 2, 3] as $i) {
$this->query->bindValue(':id' . $i, $i, ParameterType::INTEGER);
}

$bounded = $this->query->getBounded();

$this->assertSame('JustValueNoReference', $bounded[':literal']->value);
$this->assertSame(ParameterType::STRING, $bounded[':literal']->dataType);
$this->assertSame([1, 2, 3], [$bounded[':id1']->value, $bounded[':id2']->value, $bounded[':id3']->value], 'Each binding keeps its own value');
$this->assertSame(ParameterType::INTEGER, $bounded[':id2']->dataType);
}

#[TestDox('The call method correctly creates and manages a CALL query element')]
public function testCall()
{
Expand Down
34 changes: 34 additions & 0 deletions src/DatabaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -1866,9 +1866,43 @@ public function andWhere($conditions, $glue = 'OR')
return $this->extendWhere('AND', $conditions, $glue);
}

/**
* Method to add a value to an internal array that will be bound to a prepared SQL statement before query execution.
*
* Unlike bind(), the value is copied, so literals can be bound and changing a variable afterwards does not change
* the bound value.
*
* @param array|string|integer $key The key that will be used in your SQL query to reference the value. Usually of
* the form ':key', but can also be an integer.
* @param mixed $value The value that will be bound. It can be an array, in this case it has to be
* same length of $key.
* @param array|string $dataType Constant corresponding to a SQL datatype. It can be an array, in this case it
* has to be same length of $key
* @param integer $length The length of the variable. Usually required for OUTPUT parameters.
* @param array $driverOptions Optional driver options to be used.
*
* @return $this
*
* @since __DEPLOY_VERSION__
* @throws \InvalidArgumentException
*/
public function bindValue(
array|string|int $key,
mixed $value,
array|string $dataType = ParameterType::STRING,
int $length = 0,
array $driverOptions = []
): static {
// $value is a copy here, so bind() references the copy instead of the caller's variable
return $this->bind($key, $value, $dataType, $length, $driverOptions);
}

/**
* Method to add a variable to an internal array that will be bound to a prepared SQL statement before query execution.
*
* The variable is bound by reference: the value it has when the query is executed is used, not the value it has
* when bind() is called. Use bindValue() to bind a literal or the current value of a variable.
*
* @param array|string|integer $key The key that will be used in your SQL query to reference the value. Usually of
* the form ':key', but can also be an integer.
* @param mixed $value The value that will be bound. It can be an array, in this case it has to be
Expand Down
Loading