Skip to content

Commit

Permalink
Require passing parameter type to bindParam() and bindValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Aug 2, 2022
1 parent 38d20ca commit e17632d
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 184 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Expand Up @@ -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 using NULL as prepared statement parameter type.

The value of parameter type used in the wrapper layer (e.g. in `Connection::executeQuery()`
Expand Down
29 changes: 3 additions & 26 deletions src/Driver/IBMDB2/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 2 additions & 23 deletions src/Driver/Middleware/AbstractStatementMiddleware.php
Expand Up @@ -7,45 +7,24 @@
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
{
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);
}

Expand Down
23 changes: 2 additions & 21 deletions src/Driver/Mysqli/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
23 changes: 2 additions & 21 deletions src/Driver/OCI8/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 2 additions & 23 deletions src/Driver/PDO/SQLSrv/Statement.php
Expand Up @@ -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;
Expand All @@ -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:
Expand Down Expand Up @@ -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);
}
}
24 changes: 2 additions & 22 deletions src/Driver/PDO/Statement.php
Expand Up @@ -12,8 +12,6 @@
use PDOException;
use PDOStatement;

use function func_num_args;

final class Statement implements StatementInterface
{
/**
Expand All @@ -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 {
Expand All @@ -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));
Expand Down
23 changes: 2 additions & 21 deletions src/Driver/SQLSrv/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -68,40 +67,22 @@ 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;
}

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;

Expand Down
6 changes: 3 additions & 3 deletions src/Driver/Statement.php
Expand Up @@ -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
Expand All @@ -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.
*
Expand All @@ -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;

Expand Down

0 comments on commit e17632d

Please sign in to comment.