Skip to content

Commit

Permalink
Merge pull request #5467 from morozov/db2-param-order
Browse files Browse the repository at this point in the history
Fix order of binding parameters on ibm_db2
  • Loading branch information
morozov committed Jun 25, 2022
2 parents b178d60 + e298506 commit 92022e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
19 changes: 4 additions & 15 deletions src/Driver/IBMDB2/Statement.php
Expand Up @@ -19,7 +19,6 @@
use function fwrite;
use function is_int;
use function is_resource;
use function ksort;
use function stream_copy_to_stream;
use function stream_get_meta_data;
use function tmpfile;
Expand All @@ -36,7 +35,7 @@ final class Statement implements StatementInterface
private $stmt;

/** @var mixed[] */
private $bindParam = [];
private $parameters = [];

/**
* Map of LOB parameter positions to the tuples containing reference to the variable bound to the driver statement
Expand Down Expand Up @@ -108,9 +107,9 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
*/
private function bind($position, &$variable, int $parameterType, int $dataType): void
{
$this->bindParam[$position] =& $variable;
$this->parameters[$position] =& $variable;

if (! db2_bind_param($this->stmt, $position, 'variable', $parameterType, $dataType)) {
if (! db2_bind_param($this->stmt, $position, '', $parameterType, $dataType)) {
throw StatementError::new($this->stmt);
}
}
Expand All @@ -120,16 +119,6 @@ private function bind($position, &$variable, int $parameterType, int $dataType):
*/
public function execute($params = null): ResultInterface
{
if ($params === null) {
ksort($this->bindParam);

$params = [];

foreach ($this->bindParam as $value) {
$params[] = $value;
}
}

foreach ($this->lobs as [$source, $target]) {
if (is_resource($source)) {
$this->copyStreamToStream($source, $target);
Expand All @@ -140,7 +129,7 @@ public function execute($params = null): ResultInterface
$this->writeStringToStream($source, $target);
}

$result = @db2_execute($this->stmt, $params);
$result = @db2_execute($this->stmt, $params ?? $this->parameters);

foreach ($this->lobs as [, $handle]) {
fclose($handle);
Expand Down
19 changes: 19 additions & 0 deletions tests/Functional/StatementTest.php
Expand Up @@ -263,6 +263,25 @@ public function testBindInvalidNamedParameter(): void
$statement->executeQuery(['bar' => 'baz']);
}

public function testParameterBindingOrder(): void
{
$platform = $this->connection->getDatabasePlatform();

// some supported drivers don't support selecting an untyped literal
// from a dummy table, so we wrap it into a function that assumes its type
$query = $platform->getDummySelectSQL(
$platform->getLengthExpression('?')
. ', '
. $platform->getLengthExpression('?')
);

$stmt = $this->connection->prepare($query);
$stmt->bindValue(2, 'banana');
$stmt->bindValue(1, 'apple');

self::assertEquals([5, 6], $stmt->executeQuery()->fetchNumeric());
}

/**
* @param mixed $expected
*
Expand Down

0 comments on commit 92022e7

Please sign in to comment.