Skip to content

Commit

Permalink
Deprecate not passing parameter type to bindParam() and bindValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Jul 31, 2022
1 parent 4b75526 commit ebb805c
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 8 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Expand Up @@ -8,6 +8,11 @@ awareness about deprecated code.

# Upgrade to 3.4

## Deprecated not passing parameter type to `Statement::bind*()` methods.

Not passing `$type` to `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.
Expand Down
10 changes: 6 additions & 4 deletions src/Connection.php
Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -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(
Expand All @@ -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);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/Driver/IBMDB2/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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/5557',
'Not passing $type to Statement::bindValue() is deprecated.'
. ' Pass the type corresponding to the parameter being bound.'
);
}

return $this->bindParam($param, $value, $type);
}

Expand All @@ -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/5557',
'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);
Expand Down
21 changes: 21 additions & 0 deletions src/Driver/Middleware/AbstractStatementMiddleware.php
Expand Up @@ -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
{
Expand All @@ -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/5557',
'Not passing $type to Statement::bindValue() is deprecated.'
. ' Pass the type corresponding to the parameter being bound.'
);
}

return $this->wrappedStatement->bindValue($param, $value, $type);
}

Expand All @@ -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/5557',
'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);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Driver/Mysqli/Statement.php
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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/5557',
'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);
}
Expand All @@ -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/5557',
'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);
}
Expand Down
24 changes: 22 additions & 2 deletions src/Driver/OCI8/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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/5557',
'Not passing $type to Statement::bindValue() is deprecated.'
. ' Pass the type corresponding to the parameter being bound.'
);
}

return $this->bindParam($param, $value, $type);
}

Expand All @@ -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/5557',
'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);
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/Driver/PDO/SQLSrv/Statement.php
Expand Up @@ -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/5557',
'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',
Expand Down Expand Up @@ -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/5557',
'Not passing $type to Statement::bindValue() is deprecated.'
. ' Pass the type corresponding to the parameter being bound.'
);
}

return $this->bindParam($param, $value, $type);
}
}
18 changes: 18 additions & 0 deletions src/Driver/PDO/Statement.php
Expand Up @@ -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/5557',
'Not passing $type to Statement::bindValue() is deprecated.'
. ' Pass the type corresponding to the parameter being bound.'
);
}

$type = $this->convertParamType($type);

try {
Expand All @@ -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/5557',
'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',
Expand Down
24 changes: 22 additions & 2 deletions src/Driver/SQLSrv/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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/5557',
'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;

Expand All @@ -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/5557',
'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;

Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit ebb805c

Please sign in to comment.