From 1662b494eb32eaebc7c18f94da0a740f36121f8b Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Fri, 29 Jul 2022 15:16:31 -0700 Subject: [PATCH 1/3] Remove support for using NULL as prepared statement parameter type --- UPGRADE.md | 5 + phpcs.xml.dist | 5 - src/Cache/QueryCacheProfile.php | 6 +- src/Connection.php | 128 ++++++++---------- src/ExpandArrayParameters.php | 10 +- src/Query.php | 6 +- src/Query/QueryBuilder.php | 34 ++--- .../Connection/ExpandArrayParametersTest.php | 18 +-- tests/Query/QueryBuilderTest.php | 42 +++--- 9 files changed, 113 insertions(+), 141 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index d9111306612..c3b7bbdb6a3 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,11 @@ awareness about deprecated code. # Upgrade to 4.0 +## BC BREAK: removed support for using NULL as prepared statement parameter type. + +The value of parameter type used in the wrapper layer (e.g. in `Connection::executeQuery()` +or `Statement::bindValue()`) can no longer be `NULL`. + ## BC BREAK: converted enum-like classes to enums The following classes have been converted to enums: diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 951ab36a0e3..4acb7c33cfa 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -124,9 +124,4 @@ src/ParameterType.php - - - - src/Cache/QueryCacheProfile.php - diff --git a/src/Cache/QueryCacheProfile.php b/src/Cache/QueryCacheProfile.php index 4633dc01396..619d5e44aa5 100644 --- a/src/Cache/QueryCacheProfile.php +++ b/src/Cache/QueryCacheProfile.php @@ -52,9 +52,9 @@ public function getCacheKey(): string /** * Generates the real cache key from query, params, types and connection parameters. * - * @param list|array $params - * @param array|array $types - * @param array $connectionParams + * @param list|array $params + * @param array|array $types + * @param array $connectionParams * * @return string[] */ diff --git a/src/Connection.php b/src/Connection.php index 7ca488b6d61..42e375d87f5 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -361,8 +361,8 @@ public function setAutoCommit(bool $autoCommit): void * Prepares and executes an SQL query and returns the first row of the result * as an associative array. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return array|false False is returned if no rows are found. * @@ -377,8 +377,8 @@ public function fetchAssociative(string $query, array $params = [], array $types * Prepares and executes an SQL query and returns the first row of the result * as a numerically indexed array. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return list|false False is returned if no rows are found. * @@ -393,8 +393,8 @@ public function fetchNumeric(string $query, array $params = [], array $types = [ * Prepares and executes an SQL query and returns the value of a single column * of the first row of the result. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return mixed|false False is returned if no rows are found. * @@ -456,8 +456,8 @@ private function addCriteriaCondition( * * Table expression and columns are not escaped and are not safe for user-input. * - * @param array $criteria - * @param array|array $types + * @param array $criteria + * @param array|array $types * * @return int|string The number of affected rows. * @@ -520,9 +520,9 @@ public function getTransactionIsolation(): TransactionIsolationLevel * * Table expression and columns are not escaped and are not safe for user-input. * - * @param array $data - * @param array $criteria - * @param array|array $types + * @param array $data + * @param array $criteria + * @param array|array $types * * @return int|string The number of affected rows. * @@ -555,8 +555,8 @@ public function update(string $table, array $data, array $criteria, array $types * * Table expression and columns are not escaped and are not safe for user-input. * - * @param array $data - * @param array|array $types + * @param array $data + * @param array|array $types * * @return int|string The number of affected rows. * @@ -589,10 +589,10 @@ public function insert(string $table, array $data, array $types = []): int|strin /** * Extract ordered type list from an ordered column list and type map. * - * @param array $columns - * @param array|array $types + * @param array $columns + * @param array|array $types * - * @return array|array + * @return array|array */ private function extractTypeValues(array $columns, array $types): array { @@ -636,8 +636,8 @@ public function quote(string $value): string /** * Prepares and executes an SQL query and returns the result as an array of numeric arrays. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return list> * @@ -651,8 +651,8 @@ public function fetchAllNumeric(string $query, array $params = [], array $types /** * Prepares and executes an SQL query and returns the result as an array of associative arrays. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return list> * @@ -667,8 +667,8 @@ public function fetchAllAssociative(string $query, array $params = [], array $ty * Prepares and executes an SQL query and returns the result as an associative array with the keys * mapped to the first column and the values mapped to the second column. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return array * @@ -684,8 +684,8 @@ public function fetchAllKeyValue(string $query, array $params = [], array $types * to the first column and the values being an associative array representing the rest of the columns * and their values. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return array> * @@ -699,8 +699,8 @@ public function fetchAllAssociativeIndexed(string $query, array $params = [], ar /** * Prepares and executes an SQL query and returns the result as an array of the first column values. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return list * @@ -714,8 +714,8 @@ public function fetchFirstColumn(string $query, array $params = [], array $types /** * Prepares and executes an SQL query and returns the result as an iterator over rows represented as numeric arrays. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return Traversable> * @@ -730,8 +730,8 @@ public function iterateNumeric(string $query, array $params = [], array $types = * Prepares and executes an SQL query and returns the result as an iterator over rows represented * as associative arrays. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return Traversable> * @@ -746,8 +746,8 @@ public function iterateAssociative(string $query, array $params = [], array $typ * Prepares and executes an SQL query and returns the result as an iterator with the keys * mapped to the first column and the values mapped to the second column. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return Traversable * @@ -779,8 +779,8 @@ public function iterateAssociativeIndexed(string $query, array $params = [], arr /** * Prepares and executes an SQL query and returns the result as an iterator over the first column values. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return Traversable * @@ -816,8 +816,8 @@ public function prepare(string $sql): Statement * * If the query is parametrized, a prepared statement is used. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @throws Exception */ @@ -855,8 +855,8 @@ public function executeQuery( /** * Executes a caching query. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @throws CacheException * @throws Exception @@ -913,8 +913,8 @@ public function executeCacheQuery(string $sql, array $params, array $types, Quer * * This method supports PDO binding types as well as DBAL mapping types. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @throws Exception */ @@ -1302,8 +1302,8 @@ public function convertToPHPValue(mixed $value, string $type): mixed * Binds a set of parameters, some or all of which are typed with a PDO binding type * or DBAL mapping type, to a given statement. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @throws Exception */ @@ -1314,19 +1314,10 @@ private function bindParameters(DriverStatement $stmt, array $params, array $typ $bindIndex = 1; foreach ($params as $key => $value) { - if (isset($types[$key])) { + if (array_key_exists($key, $types)) { $type = $types[$key]; [$value, $bindingType] = $this->getBindingInfo($value, $type); } else { - if (array_key_exists($key, $types)) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5550', - 'Using NULL as prepared statement parameter type is deprecated.' - . 'Omit or use Parameter::STRING instead' - ); - } - $bindingType = ParameterType::STRING; } @@ -1337,19 +1328,10 @@ private function bindParameters(DriverStatement $stmt, array $params, array $typ } else { // Named parameters foreach ($params as $name => $value) { - if (isset($types[$name])) { + if (array_key_exists($name, $types)) { $type = $types[$name]; [$value, $bindingType] = $this->getBindingInfo($value, $type); } else { - if (array_key_exists($name, $types)) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5550', - 'Using NULL as prepared statement parameter type is deprecated.' - . 'Omit or use Parameter::STRING instead' - ); - } - $bindingType = ParameterType::STRING; } @@ -1361,14 +1343,14 @@ private function bindParameters(DriverStatement $stmt, array $params, array $typ /** * Gets the binding type of a given type. * - * @param mixed $value The value to bind. - * @param string|ParameterType|Type|null $type The type to bind. + * @param mixed $value The value to bind. + * @param string|ParameterType|Type $type The type to bind. * * @return array{mixed, ParameterType} [0] => the (escaped) value, [1] => the binding type. * * @throws Exception */ - private function getBindingInfo(mixed $value, string|ParameterType|Type|null $type): array + private function getBindingInfo(mixed $value, string|ParameterType|Type $type): array { if (is_string($type)) { $type = Type::getType($type); @@ -1378,7 +1360,7 @@ private function getBindingInfo(mixed $value, string|ParameterType|Type|null $ty $value = $type->convertToDatabaseValue($value, $this->getDatabasePlatform()); $bindingType = $type->getBindingType(); } else { - $bindingType = $type ?? ParameterType::STRING; + $bindingType = $type; } return [$value, $bindingType]; @@ -1395,8 +1377,8 @@ public function createQueryBuilder(): QueryBuilder /** * @internal * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types */ final public function convertExceptionDuringQuery( Driver\Exception $e, @@ -1416,13 +1398,13 @@ final public function convertException(Driver\Exception $e): DriverException } /** - * @param array|array $params - * @param array|array $types + * @param array|array $params + * @param array|array $types * * @return array{ * string, * array|array, - * array|array + * array|array * } */ private function expandArrayParameters(string $sql, array $params, array $types): array @@ -1478,8 +1460,8 @@ private function handleDriverException( * * @deprecated This API is deprecated and will be removed after 2022 * - * @param array|array $params - * @param array|array $types + * @param array|array $params + * @param array|array $types * * @throws Exception */ diff --git a/src/ExpandArrayParameters.php b/src/ExpandArrayParameters.php index 0e7e8ad2c50..4daae7bc7df 100644 --- a/src/ExpandArrayParameters.php +++ b/src/ExpandArrayParameters.php @@ -27,12 +27,12 @@ final class ExpandArrayParameters implements Visitor /** @var list */ private array $convertedParameters = []; - /** @var array */ + /** @var array */ private array $convertedTypes = []; /** - * @param array|array $parameters - * @param array|array $types + * @param array|array $parameters + * @param array|array $types */ public function __construct(private readonly array $parameters, private readonly array $types) { @@ -115,7 +115,7 @@ private function acceptParameter(int|string $key, mixed $value): void } /** - * @return array + * @return array */ public function getTypes(): array { @@ -125,7 +125,7 @@ public function getTypes(): array /** * @param list $values */ - private function appendTypedParameter(array $values, string|ParameterType|Type|null $type): void + private function appendTypedParameter(array $values, string|ParameterType|Type $type): void { $this->convertedSQL[] = implode(', ', array_fill(0, count($values), '?')); diff --git a/src/Query.php b/src/Query.php index 82e92f3a23b..8caae6ef57b 100644 --- a/src/Query.php +++ b/src/Query.php @@ -14,8 +14,8 @@ final class Query { /** - * @param array $params - * @param array $types + * @param array $params + * @param array $types * * @psalm-suppress ImpurePropertyAssignment */ @@ -40,7 +40,7 @@ public function getParams(): array } /** - * @return array + * @return array */ public function getTypes(): array { diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index d94a046043c..110b030d0c4 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -53,7 +53,7 @@ class QueryBuilder /** * The parameter type map of this query. * - * @var array|array + * @var array|array */ private array $types = []; @@ -353,29 +353,19 @@ public function getSQL(): string * ->setParameter('user_id', 1); * * - * @param int|string $key Parameter position or name - * @param mixed $value Parameter value - * @param string|ParameterType|Type|null $type Parameter type + * @param int|string $key Parameter position or name + * @param mixed $value Parameter value + * @param string|ParameterType|Type $type Parameter type * * @return $this This QueryBuilder instance. */ public function setParameter( int|string $key, mixed $value, - string|ParameterType|Type|null $type = ParameterType::STRING + string|ParameterType|Type $type = ParameterType::STRING ): self { - if ($type !== null) { - $this->types[$key] = $type; - } else { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5550', - 'Using NULL as prepared statement parameter type is deprecated.' - . 'Omit or use Parameter::STRING instead' - ); - } - $this->params[$key] = $value; + $this->types[$key] = $type; return $this; } @@ -394,8 +384,8 @@ public function setParameter( * )); * * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return $this This QueryBuilder instance. */ @@ -432,7 +422,7 @@ public function getParameter(string|int $key): mixed /** * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. * - * @return array|array + * @return array|array */ public function getParameterTypes(): array { @@ -446,7 +436,7 @@ public function getParameterTypes(): array * * @return int|string|ParameterType|Type The value of the bound parameter type */ - public function getParameterType(int|string $key): int|string|ParameterType|Type|null + public function getParameterType(int|string $key): int|string|ParameterType|Type { return $this->types[$key] ?? ParameterType::STRING; } @@ -1331,7 +1321,7 @@ public function __toString(): string */ public function createNamedParameter( mixed $value, - string|ParameterType|Type|null $type = ParameterType::STRING, + string|ParameterType|Type $type = ParameterType::STRING, ?string $placeHolder = null ): string { if ($placeHolder === null) { @@ -1363,7 +1353,7 @@ public function createNamedParameter( */ public function createPositionalParameter( mixed $value, - string|ParameterType|Type|null $type = ParameterType::STRING + string|ParameterType|Type $type = ParameterType::STRING ): string { $this->setParameter($this->boundCounter, $value, $type); $this->boundCounter++; diff --git a/tests/Connection/ExpandArrayParametersTest.php b/tests/Connection/ExpandArrayParametersTest.php index fb4c63bfb47..f69bf5703fb 100644 --- a/tests/Connection/ExpandArrayParametersTest.php +++ b/tests/Connection/ExpandArrayParametersTest.php @@ -318,10 +318,10 @@ public static function dataExpandListParameters(): iterable } /** - * @param array|array $params - * @param array|array $types - * @param list $expectedParams - * @param array $expectedTypes + * @param array|array $params + * @param array|array $types + * @param list $expectedParams + * @param array $expectedTypes * * @dataProvider dataExpandListParameters */ @@ -370,8 +370,8 @@ public static function missingNamedParameterProvider(): iterable } /** - * @param array|array $params - * @param array|array $types + * @param array|array $params + * @param array|array $types * * @dataProvider missingNamedParameterProvider */ @@ -412,10 +412,10 @@ public static function missingPositionalParameterProvider(): iterable } /** - * @param array|array $params - * @param array|array $types + * @param array|array $params + * @param array|array $types * - * @return array{string, list, array} + * @return array{string, list, array} */ private function expandArrayParameters(string $sql, array $params, array $types): array { diff --git a/tests/Query/QueryBuilderTest.php b/tests/Query/QueryBuilderTest.php index ab92925ef3d..9776fa665ad 100644 --- a/tests/Query/QueryBuilderTest.php +++ b/tests/Query/QueryBuilderTest.php @@ -826,8 +826,8 @@ public function testJoinWithNonUniqueAliasThrowsException(): void } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -861,8 +861,8 @@ public function testFetchAssociative( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -896,8 +896,8 @@ public function testFetchNumeric( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -931,8 +931,8 @@ public function testFetchOne( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -977,8 +977,8 @@ public function testFetchAllAssociative( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -1023,8 +1023,8 @@ public function testFetchAllNumeric( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -1069,8 +1069,8 @@ public function testFetchAllKeyValue( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -1119,8 +1119,8 @@ public function testFetchAllAssociativeIndexed( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -1172,7 +1172,7 @@ public function testFetchFirstColumn( * string, * string, * list|array, - * array|array, + * array|array, * string * }> */ @@ -1216,8 +1216,8 @@ public static function fetchProvider(): iterable } /** - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @dataProvider fetchProvider */ @@ -1250,8 +1250,8 @@ public function testExecuteQuery( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ From 0f1a68a7d217bd6f973e2ce3462e41b917a63427 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 31 Jul 2022 09:20:01 -0700 Subject: [PATCH 2/3] Require passing parameter type to bindParam() and bindValue() --- UPGRADE.md | 4 +++ src/Driver/IBMDB2/Statement.php | 29 ++----------------- .../AbstractStatementMiddleware.php | 25 ++-------------- src/Driver/Mysqli/Statement.php | 23 ++------------- src/Driver/OCI8/Statement.php | 23 ++------------- src/Driver/PDO/SQLSrv/Statement.php | 25 ++-------------- src/Driver/PDO/Statement.php | 24 ++------------- src/Driver/SQLSrv/Statement.php | 23 ++------------- src/Driver/Statement.php | 6 ++-- src/Logging/Statement.php | 25 ++-------------- tests/Logging/MiddlewareTest.php | 2 +- 11 files changed, 25 insertions(+), 184 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index c3b7bbdb6a3..1bf5c94c447 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,10 @@ awareness about deprecated code. # Upgrade to 4.0 +## BC BREAK: made parameter type in driver-level `Statement::bind*()` methods required. + +The `$type` parameter of the driver-level `Statement::bindParam()` and `::bindValue()` has been made required. + ## BC BREAK: removed support for using NULL as prepared statement parameter type. The value of parameter type used in the wrapper layer (e.g. in `Connection::executeQuery()` diff --git a/src/Driver/IBMDB2/Statement.php b/src/Driver/IBMDB2/Statement.php index 453a0f2ebb4..9aed4de77ca 100644 --- a/src/Driver/IBMDB2/Statement.php +++ b/src/Driver/IBMDB2/Statement.php @@ -17,7 +17,6 @@ use function db2_execute; use function error_get_last; use function fclose; -use function func_num_args; use function is_int; use function is_resource; use function stream_copy_to_stream; @@ -52,39 +51,17 @@ public function __construct(private readonly mixed $stmt) { } - public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void + public function bindValue(int|string $param, mixed $value, ParameterType $type): void { assert(is_int($param)); - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindValue() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->bindParam($param, $value, $type); } - public function bindParam( - int|string $param, - mixed &$variable, - ParameterType $type = ParameterType::STRING, - ?int $length = null - ): void { + public function bindParam(int|string $param, mixed &$variable, ParameterType $type, ?int $length = null): void + { assert(is_int($param)); - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindParam() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - switch ($type) { case ParameterType::INTEGER: $this->bind($param, $variable, DB2_PARAM_IN, DB2_LONG); diff --git a/src/Driver/Middleware/AbstractStatementMiddleware.php b/src/Driver/Middleware/AbstractStatementMiddleware.php index d566810fa74..e00e45ee9f9 100644 --- a/src/Driver/Middleware/AbstractStatementMiddleware.php +++ b/src/Driver/Middleware/AbstractStatementMiddleware.php @@ -7,9 +7,6 @@ use Doctrine\DBAL\Driver\Result; use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; - -use function func_num_args; abstract class AbstractStatementMiddleware implements Statement { @@ -17,35 +14,17 @@ public function __construct(private readonly Statement $wrappedStatement) { } - public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void + public function bindValue(int|string $param, mixed $value, ParameterType $type): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindValue() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->wrappedStatement->bindValue($param, $value, $type); } public function bindParam( int|string $param, mixed &$variable, - ParameterType $type = ParameterType::STRING, + ParameterType $type, ?int $length = null ): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindParam() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->wrappedStatement->bindParam($param, $variable, $type, $length); } diff --git a/src/Driver/Mysqli/Statement.php b/src/Driver/Mysqli/Statement.php index 94e5f20f23b..c4d87dfff48 100644 --- a/src/Driver/Mysqli/Statement.php +++ b/src/Driver/Mysqli/Statement.php @@ -19,7 +19,6 @@ use function count; use function feof; use function fread; -use function func_num_args; use function get_resource_type; use function is_int; use function is_resource; @@ -56,37 +55,19 @@ public function __construct(private readonly mysqli_stmt $stmt) public function bindParam( int|string $param, mixed &$variable, - ParameterType $type = ParameterType::STRING, + ParameterType $type, ?int $length = null ): void { assert(is_int($param)); - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindParam() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->types[$param - 1] = $this->convertParameterType($type); $this->boundValues[$param] =& $variable; } - public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void + public function bindValue(int|string $param, mixed $value, ParameterType $type): void { assert(is_int($param)); - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindValue() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->types[$param - 1] = $this->convertParameterType($type); $this->values[$param] = $value; $this->boundValues[$param] =& $this->values[$param]; diff --git a/src/Driver/OCI8/Statement.php b/src/Driver/OCI8/Statement.php index 486486fb9ce..c452e37eefa 100644 --- a/src/Driver/OCI8/Statement.php +++ b/src/Driver/OCI8/Statement.php @@ -10,7 +10,6 @@ use Doctrine\DBAL\ParameterType; use Doctrine\Deprecations\Deprecation; -use function func_num_args; use function is_int; use function oci_bind_by_name; use function oci_execute; @@ -41,35 +40,17 @@ public function __construct( ) { } - public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void + public function bindValue(int|string $param, mixed $value, ParameterType $type): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindValue() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->bindParam($param, $value, $type); } public function bindParam( int|string $param, mixed &$variable, - ParameterType $type = ParameterType::STRING, + ParameterType $type, ?int $length = null ): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindParam() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - if (is_int($param)) { if (! isset($this->parameterMap[$param])) { throw UnknownParameterIndex::new($param); diff --git a/src/Driver/PDO/SQLSrv/Statement.php b/src/Driver/PDO/SQLSrv/Statement.php index 7d9694bbe02..955eccd5204 100644 --- a/src/Driver/PDO/SQLSrv/Statement.php +++ b/src/Driver/PDO/SQLSrv/Statement.php @@ -7,11 +7,8 @@ use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware; use Doctrine\DBAL\Driver\PDO\Statement as PDOStatement; use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; use PDO; -use function func_num_args; - final class Statement extends AbstractStatementMiddleware { private readonly PDOStatement $statement; @@ -29,18 +26,9 @@ public function __construct(PDOStatement $statement) public function bindParam( int|string $param, mixed &$variable, - ParameterType $type = ParameterType::STRING, + ParameterType $type, ?int $length = null ): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindParam() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - switch ($type) { case ParameterType::LARGE_OBJECT: case ParameterType::BINARY: @@ -68,17 +56,8 @@ public function bindParam( } } - public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void + public function bindValue(int|string $param, mixed $value, ParameterType $type): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindValue() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->bindParam($param, $value, $type); } } diff --git a/src/Driver/PDO/Statement.php b/src/Driver/PDO/Statement.php index 35b6c3afa72..9b6a5ab401a 100644 --- a/src/Driver/PDO/Statement.php +++ b/src/Driver/PDO/Statement.php @@ -12,8 +12,6 @@ use PDOException; use PDOStatement; -use function func_num_args; - final class Statement implements StatementInterface { /** @@ -23,17 +21,8 @@ public function __construct(private readonly PDOStatement $stmt) { } - public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void + public function bindValue(int|string $param, mixed $value, ParameterType $type): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindValue() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $type = $this->convertParamType($type); try { @@ -46,18 +35,9 @@ public function bindValue(int|string $param, mixed $value, ParameterType $type = public function bindParam( string|int $param, mixed &$variable, - ParameterType $type = ParameterType::STRING, + ParameterType $type, ?int $length = null ): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindParam() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - try { if ($length === null) { $this->stmt->bindParam($param, $variable, $this->convertParamType($type)); diff --git a/src/Driver/SQLSrv/Statement.php b/src/Driver/SQLSrv/Statement.php index b42f9d13ffd..61617fa50c1 100644 --- a/src/Driver/SQLSrv/Statement.php +++ b/src/Driver/SQLSrv/Statement.php @@ -11,7 +11,6 @@ use Doctrine\Deprecations\Deprecation; use function assert; -use function func_num_args; use function is_int; use function sqlsrv_execute; use function SQLSRV_PHPTYPE_STREAM; @@ -68,19 +67,10 @@ public function __construct( $this->sql .= self::LAST_INSERT_ID_SQL; } - public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void + public function bindValue(int|string $param, mixed $value, ParameterType $type): void { assert(is_int($param)); - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindValue() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->variables[$param] = $value; $this->types[$param] = $type; } @@ -88,20 +78,11 @@ public function bindValue(int|string $param, mixed $value, ParameterType $type = public function bindParam( int|string $param, mixed &$variable, - ParameterType $type = ParameterType::STRING, + ParameterType $type, ?int $length = null ): void { assert(is_int($param)); - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindParam() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->variables[$param] =& $variable; $this->types[$param] = $type; diff --git a/src/Driver/Statement.php b/src/Driver/Statement.php index 9ded2932196..f37a78bd581 100644 --- a/src/Driver/Statement.php +++ b/src/Driver/Statement.php @@ -28,7 +28,7 @@ interface Statement * * @throws Exception */ - public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void; + public function bindValue(int|string $param, mixed $value, ParameterType $type): void; /** * Binds a PHP variable to a corresponding named (not supported by mysqli driver, see comment below) or question @@ -49,7 +49,7 @@ public function bindValue(int|string $param, mixed $value, ParameterType $type = * question mark placeholders, this will be the 1-indexed position of the parameter. * @param mixed $variable The variable to bind to the parameter. * @param ParameterType $type Explicit data type for the parameter using the {@see ParameterType} - * constants. + * constants. * @param int|null $length You must specify maxlength when using an OUT bind * so that PHP allocates enough memory to hold the returned value. * @@ -58,7 +58,7 @@ public function bindValue(int|string $param, mixed $value, ParameterType $type = public function bindParam( int|string $param, mixed &$variable, - ParameterType $type = ParameterType::STRING, + ParameterType $type, ?int $length = null ): void; diff --git a/src/Logging/Statement.php b/src/Logging/Statement.php index 891c2d4951e..d789bae7c5d 100644 --- a/src/Logging/Statement.php +++ b/src/Logging/Statement.php @@ -8,11 +8,8 @@ use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; use Psr\Log\LoggerInterface; -use function func_num_args; - final class Statement extends AbstractStatementMiddleware { /** @var array|array */ @@ -35,35 +32,17 @@ public function __construct( public function bindParam( int|string $param, mixed &$variable, - ParameterType $type = ParameterType::STRING, + ParameterType $type, ?int $length = null ): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindParam() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->params[$param] = &$variable; $this->types[$param] = $type; parent::bindParam($param, $variable, $type, $length); } - public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void + public function bindValue(int|string $param, mixed $value, ParameterType $type): void { - if (func_num_args() < 3) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5558', - 'Not passing $type to Statement::bindValue() is deprecated.' - . ' Pass the type corresponding to the parameter being bound.' - ); - } - $this->params[$param] = $value; $this->types[$param] = $type; diff --git a/tests/Logging/MiddlewareTest.php b/tests/Logging/MiddlewareTest.php index c23fd195641..d3df2444fb6 100644 --- a/tests/Logging/MiddlewareTest.php +++ b/tests/Logging/MiddlewareTest.php @@ -140,7 +140,7 @@ public function testExecuteStatementWithNamedParameters(): void $connection = $this->driver->connect([]); $statement = $connection->prepare('SELECT :value'); - $statement->bindValue('value', 'Test'); + $statement->bindValue('value', 'Test', ParameterType::STRING); $statement->execute(); } From 2beb0823fb2ab42f11ac8aa43d4513b6329db1cc Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 30 Jul 2022 23:49:59 -0700 Subject: [PATCH 3/3] Remove support for passing parameters to Statement::execute() --- src/Driver/IBMDB2/Statement.php | 14 +------ .../AbstractStatementMiddleware.php | 4 +- src/Driver/Mysqli/Statement.php | 34 ++------------- src/Driver/OCI8/Statement.php | 22 +--------- src/Driver/PDO/Statement.php | 14 +------ src/Driver/SQLSrv/Statement.php | 20 +-------- src/Driver/Statement.php | 11 +---- src/Logging/Statement.php | 6 +-- src/Portability/Statement.php | 4 +- src/Statement.php | 41 +++---------------- .../AbstractStatementMiddlewareTest.php | 3 +- tests/Functional/DataAccessTest.php | 15 ------- .../Functional/Driver/OCI8/StatementTest.php | 30 ++++++-------- tests/Functional/StatementTest.php | 34 +++++++-------- tests/Logging/MiddlewareTest.php | 17 +------- tests/Portability/StatementTest.php | 10 +---- 16 files changed, 55 insertions(+), 224 deletions(-) diff --git a/src/Driver/IBMDB2/Statement.php b/src/Driver/IBMDB2/Statement.php index 9aed4de77ca..62191c29b0a 100644 --- a/src/Driver/IBMDB2/Statement.php +++ b/src/Driver/IBMDB2/Statement.php @@ -10,7 +10,6 @@ use Doctrine\DBAL\Driver\IBMDB2\Exception\StatementError; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; use function assert; use function db2_bind_param; @@ -89,20 +88,11 @@ private function bind(int $position, mixed &$variable, int $parameterType, int $ } } - public function execute(?array $params = null): Result + public function execute(): Result { - if ($params !== null) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5556', - 'Passing $params to Statement::execute() is deprecated. Bind parameters using' - . ' Statement::bindParam() or Statement::bindValue() instead.' - ); - } - $handles = $this->bindLobs(); - $result = @db2_execute($this->stmt, $params ?? $this->parameters); + $result = @db2_execute($this->stmt, $this->parameters); foreach ($handles as $handle) { fclose($handle); diff --git a/src/Driver/Middleware/AbstractStatementMiddleware.php b/src/Driver/Middleware/AbstractStatementMiddleware.php index e00e45ee9f9..91c995db44a 100644 --- a/src/Driver/Middleware/AbstractStatementMiddleware.php +++ b/src/Driver/Middleware/AbstractStatementMiddleware.php @@ -28,8 +28,8 @@ public function bindParam( $this->wrappedStatement->bindParam($param, $variable, $type, $length); } - public function execute(?array $params = null): Result + public function execute(): Result { - return $this->wrappedStatement->execute($params); + return $this->wrappedStatement->execute(); } } diff --git a/src/Driver/Mysqli/Statement.php b/src/Driver/Mysqli/Statement.php index c4d87dfff48..519f4e065d0 100644 --- a/src/Driver/Mysqli/Statement.php +++ b/src/Driver/Mysqli/Statement.php @@ -10,7 +10,6 @@ use Doctrine\DBAL\Driver\Mysqli\Exception\StatementError; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; use mysqli_sql_exception; use mysqli_stmt; @@ -73,21 +72,10 @@ public function bindValue(int|string $param, mixed $value, ParameterType $type): $this->boundValues[$param] =& $this->values[$param]; } - public function execute(?array $params = null): Result + public function execute(): Result { - if ($params !== null) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5556', - 'Passing $params to Statement::execute() is deprecated. Bind parameters using' - . ' Statement::bindParam() or Statement::bindValue() instead.' - ); - } - - if ($params !== null && count($params) > 0) { - $this->bindUntypedValues($params); - } elseif (count($this->boundValues) > 0) { - $this->bindTypedParameters(); + if (count($this->boundValues) > 0) { + $this->bindParameters(); } try { @@ -106,7 +94,7 @@ public function execute(?array $params = null): Result * * @throws Exception */ - private function bindTypedParameters(): void + private function bindParameters(): void { $streams = $values = []; $types = $this->types; @@ -165,20 +153,6 @@ private function sendLongData(array $streams): void } } - /** - * Binds a array of values to bound parameters. - * - * @param mixed[] $values - * - * @throws Exception - */ - private function bindUntypedValues(array $values): void - { - if (! $this->stmt->bind_param(str_repeat(self::PARAMETER_TYPE_STRING, count($values)), ...$values)) { - throw StatementError::new($this->stmt); - } - } - private function convertParameterType(ParameterType $type): string { return match ($type) { diff --git a/src/Driver/OCI8/Statement.php b/src/Driver/OCI8/Statement.php index c452e37eefa..55c2e89e6d2 100644 --- a/src/Driver/OCI8/Statement.php +++ b/src/Driver/OCI8/Statement.php @@ -8,7 +8,6 @@ use Doctrine\DBAL\Driver\OCI8\Exception\UnknownParameterIndex; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; use function is_int; use function oci_bind_by_name; @@ -95,27 +94,8 @@ private function convertParameterType(ParameterType $type): int }; } - public function execute(?array $params = null): Result + public function execute(): Result { - if ($params !== null) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5556', - 'Passing $params to Statement::execute() is deprecated. Bind parameters using' - . ' Statement::bindParam() or Statement::bindValue() instead.' - ); - - foreach ($params as $key => $val) { - if (is_int($key)) { - $param = $key + 1; - } else { - $param = $key; - } - - $this->bindValue($param, $val, ParameterType::STRING); - } - } - if ($this->executionMode->isAutoCommitEnabled()) { $mode = OCI_COMMIT_ON_SUCCESS; } else { diff --git a/src/Driver/PDO/Statement.php b/src/Driver/PDO/Statement.php index 9b6a5ab401a..35dcf055a32 100644 --- a/src/Driver/PDO/Statement.php +++ b/src/Driver/PDO/Statement.php @@ -7,7 +7,6 @@ use Doctrine\DBAL\Driver\Exception as ExceptionInterface; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; use PDO; use PDOException; use PDOStatement; @@ -68,19 +67,10 @@ public function bindParamWithDriverOptions( } } - public function execute(?array $params = null): Result + public function execute(): Result { - if ($params !== null) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5556', - 'Passing $params to Statement::execute() is deprecated. Bind parameters using' - . ' Statement::bindParam() or Statement::bindValue() instead.' - ); - } - try { - $this->stmt->execute($params); + $this->stmt->execute(); } catch (PDOException $exception) { throw Exception::new($exception); } diff --git a/src/Driver/SQLSrv/Statement.php b/src/Driver/SQLSrv/Statement.php index 61617fa50c1..850b6baf9e5 100644 --- a/src/Driver/SQLSrv/Statement.php +++ b/src/Driver/SQLSrv/Statement.php @@ -8,7 +8,6 @@ use Doctrine\DBAL\Driver\SQLSrv\Exception\Error; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; use function assert; use function is_int; @@ -90,25 +89,8 @@ public function bindParam( $this->stmt = null; } - public function execute(?array $params = null): Result + public function execute(): Result { - if ($params !== null) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5556', - 'Passing $params to Statement::execute() is deprecated. Bind parameters using' - . ' Statement::bindParam() or Statement::bindValue() instead.' - ); - - foreach ($params as $key => $val) { - if (is_int($key)) { - $this->bindValue($key + 1, $val, ParameterType::STRING); - } else { - $this->bindValue($key, $val, ParameterType::STRING); - } - } - } - $this->stmt ??= $this->prepare(); if (! sqlsrv_execute($this->stmt)) { diff --git a/src/Driver/Statement.php b/src/Driver/Statement.php index f37a78bd581..cf5f8a0e2c7 100644 --- a/src/Driver/Statement.php +++ b/src/Driver/Statement.php @@ -65,16 +65,7 @@ public function bindParam( /** * Executes a prepared statement * - * If the prepared statement included parameter markers, you must either: - * call {@see bindParam()} to bind PHP variables to the parameter markers: - * bound variables pass their value as input and receive the output value, - * if any, of their associated parameter markers or pass an array of input-only - * parameter values. - * - * @param mixed[]|null $params A numeric array of values with as many elements as there are - * bound parameters in the SQL statement being executed. - * * @throws Exception */ - public function execute(?array $params = null): Result; + public function execute(): Result; } diff --git a/src/Logging/Statement.php b/src/Logging/Statement.php index d789bae7c5d..6a639b838e2 100644 --- a/src/Logging/Statement.php +++ b/src/Logging/Statement.php @@ -49,14 +49,14 @@ public function bindValue(int|string $param, mixed $value, ParameterType $type): parent::bindValue($param, $value, $type); } - public function execute(?array $params = null): ResultInterface + public function execute(): ResultInterface { $this->logger->debug('Executing statement: {sql} (parameters: {params}, types: {types})', [ 'sql' => $this->sql, - 'params' => $params ?? $this->params, + 'params' => $this->params, 'types' => $this->types, ]); - return parent::execute($params); + return parent::execute(); } } diff --git a/src/Portability/Statement.php b/src/Portability/Statement.php index 659b988c26e..3d005891df4 100644 --- a/src/Portability/Statement.php +++ b/src/Portability/Statement.php @@ -21,10 +21,10 @@ public function __construct(DriverStatement $stmt, private readonly Converter $c parent::__construct($stmt); } - public function execute(?array $params = null): ResultInterface + public function execute(): ResultInterface { return new Result( - parent::execute($params), + parent::execute(), $this->converter ); } diff --git a/src/Statement.php b/src/Statement.php index 1f0703ecd49..93b4c96cc6c 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -6,7 +6,6 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; -use Doctrine\Deprecations\Deprecation; use function func_num_args; use function is_string; @@ -135,19 +134,13 @@ public function bindParam( } /** - * @param mixed[] $params - * * @throws Exception */ - private function execute(array $params): Result + private function execute(): Result { - if ($params !== []) { - $this->params = $params; - } - try { return new Result( - $this->stmt->execute($params === [] ? null : $params), + $this->stmt->execute(), $this->conn ); } catch (Driver\Exception $ex) { @@ -158,43 +151,21 @@ private function execute(array $params): Result /** * Executes the statement with the currently bound parameters and return result. * - * @param mixed[] $params - * * @throws Exception */ - public function executeQuery(array $params = []): Result + public function executeQuery(): Result { - if (func_num_args() > 0) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5556', - 'Passing $params to Statement::executeQuery() is deprecated. Bind parameters using' - . ' Statement::bindParam() or Statement::bindValue() instead.' - ); - } - - return $this->execute($params); + return $this->execute(); } /** * Executes the statement with the currently bound parameters and return affected rows. * - * @param mixed[] $params - * * @throws Exception */ - public function executeStatement(array $params = []): int + public function executeStatement(): int { - if (func_num_args() > 0) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5556', - 'Passing $params to Statement::executeStatement() is deprecated. Bind parameters using' - . ' Statement::bindParam() or Statement::bindValue() instead.' - ); - } - - return $this->execute($params)->rowCount(); + return $this->execute()->rowCount(); } /** diff --git a/tests/Driver/Middleware/AbstractStatementMiddlewareTest.php b/tests/Driver/Middleware/AbstractStatementMiddlewareTest.php index 5578310b91d..c341b0e5139 100644 --- a/tests/Driver/Middleware/AbstractStatementMiddlewareTest.php +++ b/tests/Driver/Middleware/AbstractStatementMiddlewareTest.php @@ -17,10 +17,9 @@ public function testExecute(): void $statement = $this->createMock(Statement::class); $statement->expects(self::once()) ->method('execute') - ->with(['foo' => 'bar']) ->willReturn($result); - self::assertSame($result, $this->createMiddleware($statement)->execute(['foo' => 'bar'])); + self::assertSame($result, $this->createMiddleware($statement)->execute()); } private function createMiddleware(Statement $statement): AbstractStatementMiddleware diff --git a/tests/Functional/DataAccessTest.php b/tests/Functional/DataAccessTest.php index c5f27f954df..a39a0393ccd 100644 --- a/tests/Functional/DataAccessTest.php +++ b/tests/Functional/DataAccessTest.php @@ -105,21 +105,6 @@ public function testPrepareWithFetchOne(): void self::assertEquals(1, $column); } - public function testPrepareWithExecuteParams(): void - { - $paramInt = 1; - $paramStr = 'foo'; - - $sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?'; - $stmt = $this->connection->prepare($sql); - $result = $stmt->executeQuery([$paramInt, $paramStr]); - - $row = $result->fetchAssociative(); - self::assertNotFalse($row); - $row = array_change_key_case($row, CASE_LOWER); - self::assertEquals(['test_int' => 1, 'test_string' => 'foo'], $row); - } - public function testFetchAllAssociative(): void { $sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?'; diff --git a/tests/Functional/Driver/OCI8/StatementTest.php b/tests/Functional/Driver/OCI8/StatementTest.php index 94ca082f53b..6a96ed1f5f9 100644 --- a/tests/Functional/Driver/OCI8/StatementTest.php +++ b/tests/Functional/Driver/OCI8/StatementTest.php @@ -35,24 +35,6 @@ public function testQueryConversion(string $query, array $params, array $expecte ); } - /** - * Low-level approach to working with parameter binding - * - * @param mixed[] $params - * @param mixed[] $expected - * - * @dataProvider queryConversionProvider - */ - public function testStatementBindParameters(string $query, array $params, array $expected): void - { - self::assertEquals( - $expected, - $this->connection->prepare($query) - ->executeQuery($params) - ->fetchAssociative() - ); - } - /** * @return array> */ @@ -106,4 +88,16 @@ public static function queryConversionProvider(): iterable ], ]; } + + public function testBindPositionalParameter(): void + { + $statement = $this->connection->prepare('SELECT ? COL1 FROM DUAL'); + $statement->bindValue(1, 1); + + self::assertEquals( + ['COL1' => 1], + $statement->executeQuery() + ->fetchAssociative() + ); + } } diff --git a/tests/Functional/StatementTest.php b/tests/Functional/StatementTest.php index 298cc4e8443..77a326c66fb 100644 --- a/tests/Functional/StatementTest.php +++ b/tests/Functional/StatementTest.php @@ -153,9 +153,7 @@ public function testIncompletelyFetchedStatementDoesNotBlockConnection(): void // fetching only one record out of two $result->fetchAssociative(); - $stmt2 = $this->connection->prepare('SELECT id FROM stmt_test WHERE id = ?'); - $result = $stmt2->executeQuery([1]); - self::assertEquals(1, $result->fetchOne()); + self::assertEquals(1, $this->connection->fetchOne('SELECT id FROM stmt_test WHERE id = ?', [1])); } public function testReuseStatementAfterFreeingResult(): void @@ -168,15 +166,17 @@ public function testReuseStatementAfterFreeingResult(): void $this->connection->insert('stmt_test', ['id' => 2]); $stmt = $this->connection->prepare('SELECT id FROM stmt_test WHERE id = ?'); + $stmt->bindValue(1, 1); - $result = $stmt->executeQuery([1]); + $result = $stmt->executeQuery(); $id = $result->fetchOne(); self::assertEquals(1, $id); $result->free(); - $result = $stmt->executeQuery([2]); + $stmt->bindValue(1, 2); + $result = $stmt->executeQuery(); $id = $result->fetchOne(); self::assertEquals(2, $id); @@ -255,9 +255,11 @@ public function testBindInvalidNamedParameter(): void $platform = $this->connection->getDatabasePlatform(); $statement = $this->connection->prepare($platform->getDummySelectSQL(':foo')); + $this->expectException(DriverException::class); - $statement->executeQuery(['bar' => 'baz']); + $statement->bindValue('bar', 'baz'); + $statement->executeQuery(); } public function testParameterBindingOrder(): void @@ -391,7 +393,8 @@ public function testExecWithRedundantParameters(): void $this->expectException(Exception::class); } - $stmt->executeQuery([null]); + $stmt->bindValue(1, null); + $stmt->executeQuery(); } public function testExecuteQuery(): void @@ -403,16 +406,6 @@ public function testExecuteQuery(): void self::assertEquals(1, $result); } - public function testExecuteQueryWithParams(): void - { - $this->connection->insert('stmt_test', ['id' => 1]); - - $query = 'SELECT id FROM stmt_test WHERE id = ?'; - $result = $this->connection->prepare($query)->executeQuery([1])->fetchOne(); - - self::assertEquals(1, $result); - } - public function testExecuteStatement(): void { $this->connection->insert('stmt_test', ['id' => 1]); @@ -426,8 +419,11 @@ public function testExecuteStatement(): void self::assertEquals(1, $result); - $query = 'UPDATE stmt_test SET name = ? WHERE id = ?'; - $result = $this->connection->prepare($query)->executeStatement(['foo', 1]); + $query = 'UPDATE stmt_test SET name = ? WHERE id = ?'; + $stmt = $this->connection->prepare($query); + $stmt->bindValue(1, 'foo'); + $stmt->bindValue(2, 1); + $result = $stmt->executeStatement(); self::assertEquals(1, $result); } diff --git a/tests/Logging/MiddlewareTest.php b/tests/Logging/MiddlewareTest.php index d3df2444fb6..d4f3c5618aa 100644 --- a/tests/Logging/MiddlewareTest.php +++ b/tests/Logging/MiddlewareTest.php @@ -94,22 +94,7 @@ public function testBeginCommitRollback(): void $connection->rollBack(); } - public function testExecuteStatementWithUntypedParameters(): void - { - $this->logger->expects(self::once()) - ->method('debug') - ->with('Executing statement: {sql} (parameters: {params}, types: {types})', [ - 'sql' => 'SELECT ?', - 'params' => [42], - 'types' => [], - ]); - - $connection = $this->driver->connect([]); - $statement = $connection->prepare('SELECT ?'); - $statement->execute([42]); - } - - public function testExecuteStatementWithTypedParameters(): void + public function testExecuteStatementWithParameters(): void { $this->logger->expects(self::once()) ->method('debug') diff --git a/tests/Portability/StatementTest.php b/tests/Portability/StatementTest.php index 9d37b9b071b..9fd1a1e687a 100644 --- a/tests/Portability/StatementTest.php +++ b/tests/Portability/StatementTest.php @@ -54,15 +54,9 @@ public function testBindValue(): void public function testExecute(): void { - $params = [ - 'foo', - 'bar', - ]; - $this->wrappedStmt->expects(self::once()) - ->method('execute') - ->with($params); + ->method('execute'); - $this->stmt->execute($params); + $this->stmt->execute(); } }