diff --git a/src/Driver/IBMDB2/Statement.php b/src/Driver/IBMDB2/Statement.php index 39cd6a4793d..b87439f79ba 100644 --- a/src/Driver/IBMDB2/Statement.php +++ b/src/Driver/IBMDB2/Statement.php @@ -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; @@ -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 @@ -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); } } @@ -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); @@ -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); diff --git a/tests/Functional/StatementTest.php b/tests/Functional/StatementTest.php index 2b7fbc5a6a3..2e4b2336d4f 100644 --- a/tests/Functional/StatementTest.php +++ b/tests/Functional/StatementTest.php @@ -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 *