Skip to content

Commit e24761c

Browse files
authored
Merge pull request #4163 from morozov/deprecate-wrapper-query-exec
Deprecate duplicate and ambiguous wrapper connection methods
2 parents cedf3a2 + 35e9150 commit e24761c

29 files changed

+124
-78
lines changed

UPGRADE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Upgrade to 2.11
22

3+
## Deprecations in the wrapper `Connection` class
4+
5+
1. The `executeUpdate()` method has been deprecated in favor of `executeStatement()`.
6+
2. The `query()` method has been deprecated in favor of `executeQuery()`.
7+
3. The `exec()` method has been deprecated in favor of `executeStatement()`.
8+
39
## PDO-related classes outside of the PDO namespace are deprecated
410

511
The following outside of the PDO namespace have been deprecated in favor of their counterparts in the PDO namespace:

docs/en/reference/data-retrieval-and-manipulation.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ use prepared statements:
142142
SQL query, bind the given params with their binding types and execute the query.
143143
This method returns the executed prepared statement for iteration and is useful
144144
for SELECT statements.
145-
- ``executeUpdate($sql, $params, $types)`` - Create a prepared statement for the passed
145+
- ``executeStatement($sql, $params, $types)`` - Create a prepared statement for the passed
146146
SQL query, bind the given params with their binding types and execute the query.
147147
This method returns the number of affected rows by the executed query and is useful
148148
for UPDATE, DELETE and INSERT statements.
@@ -170,7 +170,7 @@ of this query using the fetch API of a statement:
170170
The fetch API of a prepared statement obviously works only for ``SELECT`` queries.
171171

172172
If you find it tedious to write all the prepared statement code you can alternatively use
173-
the ``Doctrine\DBAL\Connection#executeQuery()`` and ``Doctrine\DBAL\Connection#executeUpdate()``
173+
the ``Doctrine\DBAL\Connection#executeQuery()`` and ``Doctrine\DBAL\Connection#executeStatement()``
174174
methods. See the API section below on details how to use them.
175175

176176
Additionally there are lots of convenience methods for data-retrieval and manipulation
@@ -208,7 +208,7 @@ which means this code works independent of the database you are using.
208208
.. note::
209209

210210
Be aware this type conversion only works with ``Statement#bindValue()``,
211-
``Connection#executeQuery()`` and ``Connection#executeUpdate()``. It
211+
``Connection#executeQuery()`` and ``Connection#executeStatement()``. It
212212
is not supported to pass a doctrine type name to ``Statement#bindParam()``,
213213
because this would not work with binding by reference.
214214

@@ -286,7 +286,7 @@ This is much more complicated and is ugly to write generically.
286286
.. note::
287287

288288
The parameter list support only works with ``Doctrine\DBAL\Connection::executeQuery()``
289-
and ``Doctrine\DBAL\Connection::executeUpdate()``, NOT with the binding methods of
289+
and ``Doctrine\DBAL\Connection::executeStatement()``, NOT with the binding methods of
290290
a prepared statement.
291291

292292
API
@@ -319,7 +319,7 @@ Prepare a given SQL statement and return the
319319
)
320320
*/
321321
322-
executeUpdate()
322+
executeStatement()
323323
~~~~~~~~~~~~~~~
324324

325325
Executes a prepared statement with the given SQL and parameters and
@@ -328,7 +328,7 @@ returns the affected rows count:
328328
.. code-block:: php
329329
330330
<?php
331-
$count = $conn->executeUpdate('UPDATE user SET username = ? WHERE id = ?', array('jwage', 1));
331+
$count = $conn->executeStatement('UPDATE user SET username = ? WHERE id = ?', array('jwage', 1));
332332
echo $count; // 1
333333
334334
The ``$types`` variable contains the PDO or Doctrine Type constants

docs/en/reference/security.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ are using just the DBAL there are also helper methods which simplify the usage q
135135
$sql = "SELECT * FROM users WHERE username = ?";
136136
$stmt = $connection->executeQuery($sql, array($_GET['username']));
137137
138-
There is also ``executeUpdate`` which does not return a statement but the number of affected rows.
138+
There is also ``executeStatement`` which does not return a statement but the number of affected rows.
139139

140140
Besides binding parameters you can also pass the type of the variable. This allows Doctrine or the underlying
141141
vendor to not only escape but also cast the value to the correct type. See the docs on querying and DQL in the

docs/en/reference/sharding_azure_tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ operation for us:
263263
'LastName' => 'Brehm',
264264
));
265265
266-
$conn->executeUpdate("DECLARE @orderId INT
266+
$conn->executeStatement("DECLARE @orderId INT
267267
268268
DECLARE @customerId INT
269269

docs/examples/sharding/insert_data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
'LastName' => 'Brehm',
9191
));
9292

93-
$conn->executeUpdate("
93+
$conn->executeStatement("
9494
DECLARE @orderId INT
9595
9696
DECLARE @customerId INT

lib/Doctrine/DBAL/Connection.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ public function delete($tableExpression, array $identifier, array $types = [])
749749

750750
$this->addIdentifierCondition($identifier, $columns, $values, $conditions);
751751

752-
return $this->executeUpdate(
752+
return $this->executeStatement(
753753
'DELETE FROM ' . $tableExpression . ' WHERE ' . implode(' AND ', $conditions),
754754
$values,
755755
is_string(key($types)) ? $this->extractTypeValues($columns, $types) : $types
@@ -777,7 +777,7 @@ public function setTransactionIsolation($level)
777777
{
778778
$this->transactionIsolationLevel = $level;
779779

780-
return $this->executeUpdate($this->getDatabasePlatform()->getSetTransactionIsolationSQL($level));
780+
return $this->executeStatement($this->getDatabasePlatform()->getSetTransactionIsolationSQL($level));
781781
}
782782

783783
/**
@@ -827,7 +827,7 @@ public function update($tableExpression, array $data, array $identifier, array $
827827
$sql = 'UPDATE ' . $tableExpression . ' SET ' . implode(', ', $set)
828828
. ' WHERE ' . implode(' AND ', $conditions);
829829

830-
return $this->executeUpdate($sql, $values, $types);
830+
return $this->executeStatement($sql, $values, $types);
831831
}
832832

833833
/**
@@ -846,7 +846,7 @@ public function update($tableExpression, array $data, array $identifier, array $
846846
public function insert($tableExpression, array $data, array $types = [])
847847
{
848848
if (empty($data)) {
849-
return $this->executeUpdate('INSERT INTO ' . $tableExpression . ' () VALUES ()');
849+
return $this->executeStatement('INSERT INTO ' . $tableExpression . ' () VALUES ()');
850850
}
851851

852852
$columns = [];
@@ -859,7 +859,7 @@ public function insert($tableExpression, array $data, array $types = [])
859859
$set[] = '?';
860860
}
861861

862-
return $this->executeUpdate(
862+
return $this->executeStatement(
863863
'INSERT INTO ' . $tableExpression . ' (' . implode(', ', $columns) . ')' .
864864
' VALUES (' . implode(', ', $set) . ')',
865865
$values,
@@ -1249,6 +1249,8 @@ public function project($query, array $params, Closure $function)
12491249
/**
12501250
* Executes an SQL statement, returning a result set as a Statement object.
12511251
*
1252+
* @deprecated Use {@link executeQuery()} instead.
1253+
*
12521254
* @return \Doctrine\DBAL\Driver\Statement
12531255
*
12541256
* @throws DBALException
@@ -1285,6 +1287,8 @@ public function query()
12851287
*
12861288
* This method supports PDO binding types as well as DBAL mapping types.
12871289
*
1290+
* @deprecated Use {@link executeStatement()} instead.
1291+
*
12881292
* @param string $query The SQL query.
12891293
* @param array<mixed> $params The query parameters.
12901294
* @param array<int|string|null> $types The parameter types.
@@ -1294,19 +1298,44 @@ public function query()
12941298
* @throws DBALException
12951299
*/
12961300
public function executeUpdate($query, array $params = [], array $types = [])
1301+
{
1302+
return $this->executeStatement($query, $params, $types);
1303+
}
1304+
1305+
/**
1306+
* Executes an SQL statement with the given parameters and returns the number of affected rows.
1307+
*
1308+
* Could be used for:
1309+
* - DML statements: INSERT, UPDATE, DELETE, etc.
1310+
* - DDL statements: CREATE, DROP, ALTER, etc.
1311+
* - DCL statements: GRANT, REVOKE, etc.
1312+
* - Session control statements: ALTER SESSION, SET, DECLARE, etc.
1313+
* - Other statements that don't yield a row set.
1314+
*
1315+
* This method supports PDO binding types as well as DBAL mapping types.
1316+
*
1317+
* @param string $sql The statement SQL
1318+
* @param array<mixed> $params The query parameters
1319+
* @param array<int|string|null> $types The parameter types
1320+
*
1321+
* @return int The number of affected rows.
1322+
*
1323+
* @throws DBALException
1324+
*/
1325+
public function executeStatement($sql, array $params = [], array $types = [])
12971326
{
12981327
$connection = $this->getWrappedConnection();
12991328

13001329
$logger = $this->_config->getSQLLogger();
13011330
if ($logger) {
1302-
$logger->startQuery($query, $params, $types);
1331+
$logger->startQuery($sql, $params, $types);
13031332
}
13041333

13051334
try {
13061335
if ($params) {
1307-
[$query, $params, $types] = SQLParserUtils::expandListParameters($query, $params, $types);
1336+
[$sql, $params, $types] = SQLParserUtils::expandListParameters($sql, $params, $types);
13081337

1309-
$stmt = $connection->prepare($query);
1338+
$stmt = $connection->prepare($sql);
13101339

13111340
if ($types) {
13121341
$this->_bindTypedValues($stmt, $params, $types);
@@ -1317,10 +1346,10 @@ public function executeUpdate($query, array $params = [], array $types = [])
13171346

13181347
$result = $stmt->rowCount();
13191348
} else {
1320-
$result = $connection->exec($query);
1349+
$result = $connection->exec($sql);
13211350
}
13221351
} catch (Throwable $e) {
1323-
$this->handleExceptionDuringQuery($e, $query, $params, $types);
1352+
$this->handleExceptionDuringQuery($e, $sql, $params, $types);
13241353
}
13251354

13261355
if ($logger) {
@@ -1333,6 +1362,8 @@ public function executeUpdate($query, array $params = [], array $types = [])
13331362
/**
13341363
* Executes an SQL statement and return the number of affected rows.
13351364
*
1365+
* @deprecated Use {@link executeStatement()} instead.
1366+
*
13361367
* @param string $statement
13371368
*
13381369
* @return int The number of affected rows.

lib/Doctrine/DBAL/Connections/PrimaryReadReplicaConnection.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* 1. Replica if primary was never picked before and ONLY if 'getWrappedConnection'
2828
* or 'executeQuery' is used.
29-
* 2. Primary picked when 'exec', 'executeUpdate', 'insert', 'delete', 'update', 'createSavepoint',
29+
* 2. Primary picked when 'exec', 'executeUpdate', 'executeStatement', 'insert', 'delete', 'update', 'createSavepoint',
3030
* 'releaseSavepoint', 'beginTransaction', 'rollback', 'commit', 'query' or
3131
* 'prepare' is called.
3232
* 3. If Primary was picked once during the lifetime of the connection it will always get picked afterwards.
@@ -41,7 +41,7 @@
4141
* Be aware that Connection#executeQuery is a method specifically for READ
4242
* operations only.
4343
*
44-
* Use Connection#executeUpdate for any SQL statement that changes/updates
44+
* Use Connection#executeStatement for any SQL statement that changes/updates
4545
* state in the database (UPDATE, INSERT, DELETE or DDL statements).
4646
*
4747
* This connection is limited to replica operations using the
@@ -256,6 +256,8 @@ protected function chooseConnectionConfiguration($connectionName, $params)
256256

257257
/**
258258
* {@inheritDoc}
259+
*
260+
* @deprecated Use {@link executeStatement()} instead.
259261
*/
260262
public function executeUpdate($query, array $params = [], array $types = [])
261263
{
@@ -264,6 +266,16 @@ public function executeUpdate($query, array $params = [], array $types = [])
264266
return parent::executeUpdate($query, $params, $types);
265267
}
266268

269+
/**
270+
* {@inheritDoc}
271+
*/
272+
public function executeStatement($query, array $params = [], array $types = [])
273+
{
274+
$this->ensureConnectedToPrimary();
275+
276+
return parent::executeStatement($query, $params, $types);
277+
}
278+
267279
/**
268280
* {@inheritDoc}
269281
*/

lib/Doctrine/DBAL/Event/Listeners/MysqlSessionInit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct($charset = 'utf8', $collation = false)
4545
public function postConnect(ConnectionEventArgs $args)
4646
{
4747
$collation = $this->collation ? ' COLLATE ' . $this->collation : '';
48-
$args->getConnection()->executeUpdate('SET NAMES ' . $this->charset . $collation);
48+
$args->getConnection()->executeStatement('SET NAMES ' . $this->charset . $collation);
4949
}
5050

5151
/**

lib/Doctrine/DBAL/Event/Listeners/OracleSessionInit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function postConnect(ConnectionEventArgs $args)
6161
}
6262

6363
$sql = 'ALTER SESSION SET ' . implode(' ', $vars);
64-
$args->getConnection()->executeUpdate($sql);
64+
$args->getConnection()->executeStatement($sql);
6565
}
6666

6767
/**

lib/Doctrine/DBAL/Id/TableGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function nextValue($sequenceName)
131131
$sql = 'UPDATE ' . $this->generatorTableName . ' ' .
132132
'SET sequence_value = sequence_value + sequence_increment_by ' .
133133
'WHERE sequence_name = ? AND sequence_value = ?';
134-
$rows = $this->conn->executeUpdate($sql, [$sequenceName, $row['sequence_value']]);
134+
$rows = $this->conn->executeStatement($sql, [$sequenceName, $row['sequence_value']]);
135135

136136
if ($rows !== 1) {
137137
throw new DBALException('Race-condition detected while updating sequence. Aborting generation');

lib/Doctrine/DBAL/Query/QueryBuilder.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,6 @@ public function getState()
196196
/**
197197
* Executes this query using the bound parameters and their types.
198198
*
199-
* Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate}
200-
* for insert, update and delete statements.
201-
*
202199
* @return ResultStatement|int
203200
*/
204201
public function execute()
@@ -207,7 +204,7 @@ public function execute()
207204
return $this->connection->executeQuery($this->getSQL(), $this->params, $this->paramTypes);
208205
}
209206

210-
return $this->connection->executeUpdate($this->getSQL(), $this->params, $this->paramTypes);
207+
return $this->connection->executeStatement($this->getSQL(), $this->params, $this->paramTypes);
211208
}
212209

213210
/**

lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
10331033
protected function _execSql($sql)
10341034
{
10351035
foreach ((array) $sql as $query) {
1036-
$this->_conn->executeUpdate($query);
1036+
$this->_conn->executeStatement($query);
10371037
}
10381038
}
10391039

lib/Doctrine/DBAL/Schema/OracleSchemaManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ public function createDatabase($database = null)
307307
$password = $params['password'];
308308

309309
$query = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
310-
$this->_conn->executeUpdate($query);
310+
$this->_conn->executeStatement($query);
311311

312312
$query = 'GRANT DBA TO ' . $username;
313-
$this->_conn->executeUpdate($query);
313+
$this->_conn->executeStatement($query);
314314
}
315315

316316
/**
@@ -324,7 +324,7 @@ public function dropAutoincrement($table)
324324

325325
$sql = $this->_platform->getDropAutoincrementSql($table);
326326
foreach ($sql as $query) {
327-
$this->_conn->executeUpdate($query);
327+
$this->_conn->executeStatement($query);
328328
}
329329

330330
return true;

lib/Doctrine/DBAL/Tools/Console/Command/RunSqlCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8787
if (stripos($sql, 'select') === 0 || $input->getOption('force-fetch')) {
8888
$resultSet = $conn->fetchAllAssociative($sql);
8989
} else {
90-
$resultSet = $conn->executeUpdate($sql);
90+
$resultSet = $conn->executeStatement($sql);
9191
}
9292

9393
$output->write(Dumper::dump($resultSet, (int) $depth));

tests/Doctrine/Tests/DBAL/Connection/LoggingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public function testLogExecuteQuery(): void
2222
->executeQuery('SELECT * FROM table');
2323
}
2424

25-
public function testLogExecuteUpdate(): void
25+
public function testLogExecuteStatement(): void
2626
{
2727
$this->createConnection(
2828
$this->createStub(DriverConnection::class),
2929
'UPDATE table SET foo = ?'
3030
)
31-
->executeUpdate('UPDATE table SET foo = ?');
31+
->executeStatement('UPDATE table SET foo = ?');
3232
}
3333

3434
public function testLogPrepareExecute(): void

0 commit comments

Comments
 (0)