Skip to content

Commit

Permalink
Remove support for passing parameters to Statement::execute()
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Aug 2, 2022
1 parent a7e1c94 commit 0eef042
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 224 deletions.
14 changes: 2 additions & 12 deletions src/Driver/IBMDB2/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Driver/Middleware/AbstractStatementMiddleware.php
Expand Up @@ -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();
}
}
34 changes: 4 additions & 30 deletions src/Driver/Mysqli/Statement.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 1 addition & 21 deletions src/Driver/OCI8/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 2 additions & 12 deletions src/Driver/PDO/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
20 changes: 1 addition & 19 deletions src/Driver/SQLSrv/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
11 changes: 1 addition & 10 deletions src/Driver/Statement.php
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions src/Logging/Statement.php
Expand Up @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/Portability/Statement.php
Expand Up @@ -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
);
}
Expand Down
41 changes: 6 additions & 35 deletions src/Statement.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions tests/Driver/Middleware/AbstractStatementMiddlewareTest.php
Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions tests/Functional/DataAccessTest.php
Expand Up @@ -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 = ?';
Expand Down

0 comments on commit 0eef042

Please sign in to comment.