Skip to content

Commit 0c9f45e

Browse files
committed
Deprecate passing parameters to Statement::execute*()
1 parent 4b75526 commit 0c9f45e

File tree

9 files changed

+84
-17
lines changed

9 files changed

+84
-17
lines changed

UPGRADE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ awareness about deprecated code.
88

99
# Upgrade to 3.4
1010

11+
## Deprecated passing `$params` to `Statement::execute*()` methods.
12+
13+
Passing `$params` to the driver-level `Statement::execute()` and the wrapper-level `Statement::executeQuery()`
14+
and `Statement::executeStatement()` methods has been deprecated.
15+
16+
Bind parameters using `Statement::bindParam()` or `Statement::bindValue()` instead.
17+
1118
## Deprecated `QueryBuilder` methods and constants.
1219

1320
1. The `QueryBuilder::getState()` method has been deprecated as the builder state is an internal concern.

src/Connection.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,12 +1021,10 @@ public function executeQuery(
10211021
}
10221022

10231023
$stmt = $connection->prepare($sql);
1024-
if (count($types) > 0) {
1025-
$this->_bindTypedValues($stmt, $params, $types);
1026-
$result = $stmt->execute();
1027-
} else {
1028-
$result = $stmt->execute($params);
1029-
}
1024+
1025+
$this->bindParameters($stmt, $params, $types);
1026+
1027+
$result = $stmt->execute();
10301028
} else {
10311029
$result = $connection->query($sql);
10321030
}
@@ -1128,15 +1126,10 @@ public function executeStatement($sql, array $params = [], array $types = [])
11281126

11291127
$stmt = $connection->prepare($sql);
11301128

1131-
if (count($types) > 0) {
1132-
$this->_bindTypedValues($stmt, $params, $types);
1133-
1134-
$result = $stmt->execute();
1135-
} else {
1136-
$result = $stmt->execute($params);
1137-
}
1129+
$this->bindParameters($stmt, $params, $types);
11381130

1139-
return $result->rowCount();
1131+
return $stmt->execute()
1132+
->rowCount();
11401133
}
11411134

11421135
return $connection->exec($sql);
@@ -1668,7 +1661,7 @@ public function convertToPHPValue($value, $type)
16681661
*
16691662
* @throws Exception
16701663
*/
1671-
private function _bindTypedValues(DriverStatement $stmt, array $params, array $types): void
1664+
private function bindParameters(DriverStatement $stmt, array $params, array $types): void
16721665
{
16731666
// Check whether parameters are positional or named. Mixing is not allowed.
16741667
if (is_int(key($params))) {

src/Driver/IBMDB2/Statement.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\DBAL\Driver\Result as ResultInterface;
1010
use Doctrine\DBAL\Driver\Statement as StatementInterface;
1111
use Doctrine\DBAL\ParameterType;
12+
use Doctrine\Deprecations\Deprecation;
1213

1314
use function assert;
1415
use function db2_bind_param;
@@ -107,6 +108,15 @@ private function bind($position, &$variable, int $parameterType, int $dataType):
107108
*/
108109
public function execute($params = null): ResultInterface
109110
{
111+
if ($params !== null) {
112+
Deprecation::trigger(
113+
'doctrine/dbal',
114+
'https://github.com/doctrine/dbal/pull/5556',
115+
'Passing $params to Statement::execute() is deprecated. Bind parameters using'
116+
. ' Statement::bindParam() or Statement::bindValue() instead.'
117+
);
118+
}
119+
110120
$handles = $this->bindLobs();
111121

112122
$result = @db2_execute($this->stmt, $params ?? $this->parameters);

src/Driver/Mysqli/Statement.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Doctrine\DBAL\Driver\Result as ResultInterface;
1111
use Doctrine\DBAL\Driver\Statement as StatementInterface;
1212
use Doctrine\DBAL\ParameterType;
13+
use Doctrine\Deprecations\Deprecation;
1314
use mysqli_sql_exception;
1415
use mysqli_stmt;
1516

@@ -102,6 +103,15 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
102103
*/
103104
public function execute($params = null): ResultInterface
104105
{
106+
if ($params !== null) {
107+
Deprecation::trigger(
108+
'doctrine/dbal',
109+
'https://github.com/doctrine/dbal/pull/5556',
110+
'Passing $params to Statement::execute() is deprecated. Bind parameters using'
111+
. ' Statement::bindParam() or Statement::bindValue() instead.'
112+
);
113+
}
114+
105115
if ($params !== null && count($params) > 0) {
106116
if (! $this->bindUntypedValues($params)) {
107117
throw StatementError::new($this->stmt);

src/Driver/OCI8/Statement.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Driver\Result as ResultInterface;
88
use Doctrine\DBAL\Driver\Statement as StatementInterface;
99
use Doctrine\DBAL\ParameterType;
10+
use Doctrine\Deprecations\Deprecation;
1011

1112
use function is_int;
1213
use function oci_bind_by_name;
@@ -113,6 +114,13 @@ private function convertParameterType(int $type): int
113114
public function execute($params = null): ResultInterface
114115
{
115116
if ($params !== null) {
117+
Deprecation::trigger(
118+
'doctrine/dbal',
119+
'https://github.com/doctrine/dbal/pull/5556',
120+
'Passing $params to Statement::execute() is deprecated. Bind parameters using'
121+
. ' Statement::bindParam() or Statement::bindValue() instead.'
122+
);
123+
116124
foreach ($params as $key => $val) {
117125
if (is_int($key)) {
118126
$this->bindValue($key + 1, $val);

src/Driver/PDO/SQLSrv/Connection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ public function lastInsertId($name = null)
4141
'The usage of Connection::lastInsertId() with a sequence name is deprecated.'
4242
);
4343

44-
return $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?')
45-
->execute([$name])
44+
$statement = $this->prepare(
45+
'SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?'
46+
);
47+
$statement->bindValue(1, $name);
48+
49+
return $statement->execute()
4650
->fetchOne();
4751
}
4852

src/Driver/PDO/Statement.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ public function bindParam(
9696
*/
9797
public function execute($params = null): ResultInterface
9898
{
99+
if ($params !== null) {
100+
Deprecation::trigger(
101+
'doctrine/dbal',
102+
'https://github.com/doctrine/dbal/pull/5556',
103+
'Passing $params to Statement::execute() is deprecated. Bind parameters using'
104+
. ' Statement::bindParam() or Statement::bindValue() instead.'
105+
);
106+
}
107+
99108
try {
100109
$this->stmt->execute($params);
101110
} catch (PDOException $exception) {

src/Driver/SQLSrv/Statement.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Driver\SQLSrv\Exception\Error;
88
use Doctrine\DBAL\Driver\Statement as StatementInterface;
99
use Doctrine\DBAL\ParameterType;
10+
use Doctrine\Deprecations\Deprecation;
1011

1112
use function assert;
1213
use function is_int;
@@ -114,6 +115,13 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
114115
public function execute($params = null): ResultInterface
115116
{
116117
if ($params !== null) {
118+
Deprecation::trigger(
119+
'doctrine/dbal',
120+
'https://github.com/doctrine/dbal/pull/5556',
121+
'Passing $params to Statement::execute() is deprecated. Bind parameters using'
122+
. ' Statement::bindParam() or Statement::bindValue() instead.'
123+
);
124+
117125
foreach ($params as $key => $val) {
118126
if (is_int($key)) {
119127
$this->bindValue($key + 1, $val);

src/Statement.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ public function execute($params = null): Result
198198
*/
199199
public function executeQuery(array $params = []): Result
200200
{
201+
if (func_num_args() > 0) {
202+
Deprecation::trigger(
203+
'doctrine/dbal',
204+
'https://github.com/doctrine/dbal/pull/5556',
205+
'Passing $params to Statement::executeQuery() is deprecated. Bind parameters using'
206+
. ' Statement::bindParam() or Statement::bindValue() instead.'
207+
);
208+
}
209+
201210
if ($params === []) {
202211
$params = null; // Workaround as long execute() exists and used internally.
203212
}
@@ -214,6 +223,15 @@ public function executeQuery(array $params = []): Result
214223
*/
215224
public function executeStatement(array $params = []): int
216225
{
226+
if (func_num_args() > 0) {
227+
Deprecation::trigger(
228+
'doctrine/dbal',
229+
'https://github.com/doctrine/dbal/pull/5556',
230+
'Passing $params to Statement::executeStatement() is deprecated. Bind parameters using'
231+
. ' Statement::bindParam() or Statement::bindValue() instead.'
232+
);
233+
}
234+
217235
if ($params === []) {
218236
$params = null; // Workaround as long execute() exists and used internally.
219237
}

0 commit comments

Comments
 (0)