From 8fe2d78efe135096ba71fda7a48bb9716a933cf9 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sun, 31 Jul 2022 10:52:27 -0700 Subject: [PATCH] Deprecate not passing parameter type to bindParam() and bindValue() --- UPGRADE.md | 5 ++++ src/Connection.php | 10 ++++---- src/Driver/IBMDB2/Statement.php | 20 ++++++++++++++++ .../AbstractStatementMiddleware.php | 21 ++++++++++++++++ src/Driver/Mysqli/Statement.php | 20 ++++++++++++++++ src/Driver/OCI8/Statement.php | 24 +++++++++++++++++-- src/Driver/PDO/SQLSrv/Statement.php | 18 ++++++++++++++ src/Driver/PDO/Statement.php | 18 ++++++++++++++ src/Driver/SQLSrv/Statement.php | 24 +++++++++++++++++-- src/Logging/Statement.php | 20 ++++++++++++++++ 10 files changed, 172 insertions(+), 8 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 73d189c148c..ca4d70cd43f 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,11 @@ awareness about deprecated code. # Upgrade to 3.4 +## Deprecated not passing parameter type to the driver-level `Statement::bind*()` methods. + +Not passing `$type` to the driver-level `Statement::bindParam()` and `::bindValue()` is deprecated. +Pass the type corresponding to the parameter being bound. + ## Deprecated `QueryBuilder` methods and constants. 1. The `QueryBuilder::getState()` method has been deprecated as the builder state is an internal concern. diff --git a/src/Connection.php b/src/Connection.php index a85acbca446..b9241ae5a6b 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -1678,7 +1678,6 @@ private function _bindTypedValues(DriverStatement $stmt, array $params, array $t if (isset($types[$key])) { $type = $types[$key]; [$value, $bindingType] = $this->getBindingInfo($value, $type); - $stmt->bindValue($bindIndex, $value, $bindingType); } else { if (array_key_exists($key, $types)) { Deprecation::trigger( @@ -1689,9 +1688,11 @@ private function _bindTypedValues(DriverStatement $stmt, array $params, array $t ); } - $stmt->bindValue($bindIndex, $value); + $bindingType = ParameterType::STRING; } + $stmt->bindValue($bindIndex, $value, $bindingType); + ++$bindIndex; } } else { @@ -1700,7 +1701,6 @@ private function _bindTypedValues(DriverStatement $stmt, array $params, array $t if (isset($types[$name])) { $type = $types[$name]; [$value, $bindingType] = $this->getBindingInfo($value, $type); - $stmt->bindValue($name, $value, $bindingType); } else { if (array_key_exists($name, $types)) { Deprecation::trigger( @@ -1711,8 +1711,10 @@ private function _bindTypedValues(DriverStatement $stmt, array $params, array $t ); } - $stmt->bindValue($name, $value); + $bindingType = ParameterType::STRING; } + + $stmt->bindValue($name, $value, $bindingType); } } } diff --git a/src/Driver/IBMDB2/Statement.php b/src/Driver/IBMDB2/Statement.php index 5047f1ee819..548340419ad 100644 --- a/src/Driver/IBMDB2/Statement.php +++ b/src/Driver/IBMDB2/Statement.php @@ -9,12 +9,14 @@ use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; +use Doctrine\Deprecations\Deprecation; use function assert; use function db2_bind_param; 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; @@ -60,6 +62,15 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool { 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.' + ); + } + return $this->bindParam($param, $value, $type); } @@ -70,6 +81,15 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le { 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 3a3dc524eef..fba62cd6ed7 100644 --- a/src/Driver/Middleware/AbstractStatementMiddleware.php +++ b/src/Driver/Middleware/AbstractStatementMiddleware.php @@ -5,6 +5,9 @@ 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 { @@ -20,6 +23,15 @@ public function __construct(Statement $wrappedStatement) */ public function bindValue($param, $value, $type = ParameterType::STRING) { + 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.' + ); + } + return $this->wrappedStatement->bindValue($param, $value, $type); } @@ -28,6 +40,15 @@ public function bindValue($param, $value, $type = ParameterType::STRING) */ public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null) { + 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.' + ); + } + return $this->wrappedStatement->bindParam($param, $variable, $type, $length); } diff --git a/src/Driver/Mysqli/Statement.php b/src/Driver/Mysqli/Statement.php index fd43f41b800..f3c2077e4e2 100644 --- a/src/Driver/Mysqli/Statement.php +++ b/src/Driver/Mysqli/Statement.php @@ -10,6 +10,7 @@ use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; +use Doctrine\Deprecations\Deprecation; use mysqli_sql_exception; use mysqli_stmt; @@ -18,6 +19,7 @@ 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; @@ -69,6 +71,15 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le { 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.' + ); + } + if (! isset(self::$paramTypeMap[$type])) { throw UnknownParameterType::new($type); } @@ -86,6 +97,15 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool { 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.' + ); + } + if (! isset(self::$paramTypeMap[$type])) { throw UnknownParameterType::new($type); } diff --git a/src/Driver/OCI8/Statement.php b/src/Driver/OCI8/Statement.php index 076bc9ff791..3b8a2a49f48 100644 --- a/src/Driver/OCI8/Statement.php +++ b/src/Driver/OCI8/Statement.php @@ -7,7 +7,9 @@ use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Statement as StatementInterface; 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; @@ -54,6 +56,15 @@ public function __construct($connection, $statement, array $parameterMap, Execut */ public function bindValue($param, $value, $type = ParameterType::STRING): bool { + 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.' + ); + } + return $this->bindParam($param, $value, $type); } @@ -62,6 +73,15 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool */ public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool { + 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); @@ -115,9 +135,9 @@ public function execute($params = null): ResultInterface if ($params !== null) { foreach ($params as $key => $val) { if (is_int($key)) { - $this->bindValue($key + 1, $val); + $this->bindValue($key + 1, $val, ParameterType::STRING); } else { - $this->bindValue($key, $val); + $this->bindValue($key, $val, ParameterType::STRING); } } } diff --git a/src/Driver/PDO/SQLSrv/Statement.php b/src/Driver/PDO/SQLSrv/Statement.php index 862aefbb509..80e018d5494 100644 --- a/src/Driver/PDO/SQLSrv/Statement.php +++ b/src/Driver/PDO/SQLSrv/Statement.php @@ -40,6 +40,15 @@ public function bindParam( $length = null, $driverOptions = null ): bool { + 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 (func_num_args() > 4) { Deprecation::triggerIfCalledFromOutside( 'doctrine/dbal', @@ -70,6 +79,15 @@ public function bindParam( */ public function bindValue($param, $value, $type = ParameterType::STRING): bool { + 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.' + ); + } + return $this->bindParam($param, $value, $type); } } diff --git a/src/Driver/PDO/Statement.php b/src/Driver/PDO/Statement.php index 9c61bd74ec4..ebbb4e82e64 100644 --- a/src/Driver/PDO/Statement.php +++ b/src/Driver/PDO/Statement.php @@ -43,6 +43,15 @@ public function __construct(PDOStatement $stmt) */ public function bindValue($param, $value, $type = ParameterType::STRING) { + 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 { @@ -68,6 +77,15 @@ public function bindParam( $length = null, $driverOptions = null ): bool { + 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 (func_num_args() > 4) { Deprecation::triggerIfCalledFromOutside( 'doctrine/dbal', diff --git a/src/Driver/SQLSrv/Statement.php b/src/Driver/SQLSrv/Statement.php index 5255c7b9471..dd4bd19c95c 100644 --- a/src/Driver/SQLSrv/Statement.php +++ b/src/Driver/SQLSrv/Statement.php @@ -7,8 +7,10 @@ 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 func_num_args; use function is_int; use function sqlsrv_execute; use function SQLSRV_PHPTYPE_STREAM; @@ -86,6 +88,15 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool { 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; @@ -99,6 +110,15 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le { 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; @@ -116,9 +136,9 @@ public function execute($params = null): ResultInterface if ($params !== null) { foreach ($params as $key => $val) { if (is_int($key)) { - $this->bindValue($key + 1, $val); + $this->bindValue($key + 1, $val, ParameterType::STRING); } else { - $this->bindValue($key, $val); + $this->bindValue($key, $val, ParameterType::STRING); } } } diff --git a/src/Logging/Statement.php b/src/Logging/Statement.php index 8824d7d4f20..672f5647cc7 100644 --- a/src/Logging/Statement.php +++ b/src/Logging/Statement.php @@ -8,10 +8,12 @@ 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 array_slice; use function func_get_args; +use function func_num_args; final class Statement extends AbstractStatementMiddleware { @@ -40,6 +42,15 @@ public function __construct(StatementInterface $statement, LoggerInterface $logg */ public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null) { + 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; @@ -51,6 +62,15 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le */ public function bindValue($param, $value, $type = ParameterType::STRING) { + 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;