From 524a23b045d899a620f27f369acee7accf76cb2b Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Fri, 10 Jul 2020 19:03:16 -0700 Subject: [PATCH 1/2] Deprecate executeUpdate() in favor of executeStatement() --- UPGRADE.md | 4 ++ .../data-retrieval-and-manipulation.rst | 12 ++--- docs/en/reference/security.rst | 2 +- docs/en/reference/sharding_azure_tutorial.rst | 2 +- docs/examples/sharding/insert_data.php | 2 +- lib/Doctrine/DBAL/Connection.php | 47 +++++++++++++++---- .../PrimaryReadReplicaConnection.php | 16 ++++++- .../DBAL/Event/Listeners/MysqlSessionInit.php | 2 +- .../Event/Listeners/OracleSessionInit.php | 2 +- lib/Doctrine/DBAL/Id/TableGenerator.php | 2 +- lib/Doctrine/DBAL/Query/QueryBuilder.php | 5 +- .../DBAL/Schema/AbstractSchemaManager.php | 2 +- .../DBAL/Schema/OracleSchemaManager.php | 6 +-- .../Tools/Console/Command/RunSqlCommand.php | 2 +- .../Tests/DBAL/Connection/LoggingTest.php | 4 +- tests/Doctrine/Tests/DBAL/ConnectionTest.php | 28 +++++------ .../DBAL/Events/MysqlSessionInitTest.php | 2 +- .../DBAL/Events/OracleSessionInitTest.php | 4 +- .../Tests/DBAL/Functional/DataAccessTest.php | 8 ++-- .../Functional/Driver/OCI8/ConnectionTest.php | 2 +- .../Tests/DBAL/Functional/ExceptionTest.php | 2 +- .../Functional/MasterSlaveConnectionTest.php | 2 +- .../PrimaryReadReplicaConnectionTest.php | 2 +- .../Schema/MySqlSchemaManagerTest.php | 2 +- .../SchemaManagerFunctionalTestCase.php | 2 +- .../DBAL/Functional/TemporaryTableTest.php | 2 +- .../DBAL/Functional/Ticket/DBAL630Test.php | 4 +- .../Tests/DBAL/Functional/WriteTest.php | 18 +++---- .../DBAL/Tools/Console/RunSqlCommandTest.php | 8 ++-- 29 files changed, 118 insertions(+), 78 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 45b4e26383b..ea130f77144 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,9 @@ # Upgrade to 2.11 +## Deprecations in the wrapper `Connection` class + +1. The `executeUpdate()` method has been deprecated in favor of `executeStatement()`. + ## PDO-related classes outside of the PDO namespace are deprecated The following outside of the PDO namespace have been deprecated in favor of their counterparts in the PDO namespace: diff --git a/docs/en/reference/data-retrieval-and-manipulation.rst b/docs/en/reference/data-retrieval-and-manipulation.rst index e1afd8ec322..8a88deb6a64 100644 --- a/docs/en/reference/data-retrieval-and-manipulation.rst +++ b/docs/en/reference/data-retrieval-and-manipulation.rst @@ -142,7 +142,7 @@ use prepared statements: SQL query, bind the given params with their binding types and execute the query. This method returns the executed prepared statement for iteration and is useful for SELECT statements. -- ``executeUpdate($sql, $params, $types)`` - Create a prepared statement for the passed +- ``executeStatement($sql, $params, $types)`` - Create a prepared statement for the passed SQL query, bind the given params with their binding types and execute the query. This method returns the number of affected rows by the executed query and is useful for UPDATE, DELETE and INSERT statements. @@ -170,7 +170,7 @@ of this query using the fetch API of a statement: The fetch API of a prepared statement obviously works only for ``SELECT`` queries. If you find it tedious to write all the prepared statement code you can alternatively use -the ``Doctrine\DBAL\Connection#executeQuery()`` and ``Doctrine\DBAL\Connection#executeUpdate()`` +the ``Doctrine\DBAL\Connection#executeQuery()`` and ``Doctrine\DBAL\Connection#executeStatement()`` methods. See the API section below on details how to use them. 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. .. note:: Be aware this type conversion only works with ``Statement#bindValue()``, - ``Connection#executeQuery()`` and ``Connection#executeUpdate()``. It + ``Connection#executeQuery()`` and ``Connection#executeStatement()``. It is not supported to pass a doctrine type name to ``Statement#bindParam()``, because this would not work with binding by reference. @@ -286,7 +286,7 @@ This is much more complicated and is ugly to write generically. .. note:: The parameter list support only works with ``Doctrine\DBAL\Connection::executeQuery()`` - and ``Doctrine\DBAL\Connection::executeUpdate()``, NOT with the binding methods of + and ``Doctrine\DBAL\Connection::executeStatement()``, NOT with the binding methods of a prepared statement. API @@ -319,7 +319,7 @@ Prepare a given SQL statement and return the ) */ -executeUpdate() +executeStatement() ~~~~~~~~~~~~~~~ Executes a prepared statement with the given SQL and parameters and @@ -328,7 +328,7 @@ returns the affected rows count: .. code-block:: php executeUpdate('UPDATE user SET username = ? WHERE id = ?', array('jwage', 1)); + $count = $conn->executeStatement('UPDATE user SET username = ? WHERE id = ?', array('jwage', 1)); echo $count; // 1 The ``$types`` variable contains the PDO or Doctrine Type constants diff --git a/docs/en/reference/security.rst b/docs/en/reference/security.rst index 82f159c87fb..89e19636922 100644 --- a/docs/en/reference/security.rst +++ b/docs/en/reference/security.rst @@ -135,7 +135,7 @@ are using just the DBAL there are also helper methods which simplify the usage q $sql = "SELECT * FROM users WHERE username = ?"; $stmt = $connection->executeQuery($sql, array($_GET['username'])); -There is also ``executeUpdate`` which does not return a statement but the number of affected rows. +There is also ``executeStatement`` which does not return a statement but the number of affected rows. Besides binding parameters you can also pass the type of the variable. This allows Doctrine or the underlying vendor to not only escape but also cast the value to the correct type. See the docs on querying and DQL in the diff --git a/docs/en/reference/sharding_azure_tutorial.rst b/docs/en/reference/sharding_azure_tutorial.rst index 71a4fab5594..cb8e85d488d 100644 --- a/docs/en/reference/sharding_azure_tutorial.rst +++ b/docs/en/reference/sharding_azure_tutorial.rst @@ -263,7 +263,7 @@ operation for us: 'LastName' => 'Brehm', )); - $conn->executeUpdate("DECLARE @orderId INT + $conn->executeStatement("DECLARE @orderId INT DECLARE @customerId INT diff --git a/docs/examples/sharding/insert_data.php b/docs/examples/sharding/insert_data.php index 57aeda6c9f8..9d6e5aabcff 100644 --- a/docs/examples/sharding/insert_data.php +++ b/docs/examples/sharding/insert_data.php @@ -90,7 +90,7 @@ 'LastName' => 'Brehm', )); -$conn->executeUpdate(" +$conn->executeStatement(" DECLARE @orderId INT DECLARE @customerId INT diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 29eda274608..3aa16f2b20b 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -749,7 +749,7 @@ public function delete($tableExpression, array $identifier, array $types = []) $this->addIdentifierCondition($identifier, $columns, $values, $conditions); - return $this->executeUpdate( + return $this->executeStatement( 'DELETE FROM ' . $tableExpression . ' WHERE ' . implode(' AND ', $conditions), $values, is_string(key($types)) ? $this->extractTypeValues($columns, $types) : $types @@ -777,7 +777,7 @@ public function setTransactionIsolation($level) { $this->transactionIsolationLevel = $level; - return $this->executeUpdate($this->getDatabasePlatform()->getSetTransactionIsolationSQL($level)); + return $this->executeStatement($this->getDatabasePlatform()->getSetTransactionIsolationSQL($level)); } /** @@ -827,7 +827,7 @@ public function update($tableExpression, array $data, array $identifier, array $ $sql = 'UPDATE ' . $tableExpression . ' SET ' . implode(', ', $set) . ' WHERE ' . implode(' AND ', $conditions); - return $this->executeUpdate($sql, $values, $types); + return $this->executeStatement($sql, $values, $types); } /** @@ -846,7 +846,7 @@ public function update($tableExpression, array $data, array $identifier, array $ public function insert($tableExpression, array $data, array $types = []) { if (empty($data)) { - return $this->executeUpdate('INSERT INTO ' . $tableExpression . ' () VALUES ()'); + return $this->executeStatement('INSERT INTO ' . $tableExpression . ' () VALUES ()'); } $columns = []; @@ -859,7 +859,7 @@ public function insert($tableExpression, array $data, array $types = []) $set[] = '?'; } - return $this->executeUpdate( + return $this->executeStatement( 'INSERT INTO ' . $tableExpression . ' (' . implode(', ', $columns) . ')' . ' VALUES (' . implode(', ', $set) . ')', $values, @@ -1285,6 +1285,8 @@ public function query() * * This method supports PDO binding types as well as DBAL mapping types. * + * @deprecated Use {@link executeStatement()} instead. + * * @param string $query The SQL query. * @param array $params The query parameters. * @param array $types The parameter types. @@ -1294,19 +1296,44 @@ public function query() * @throws DBALException */ public function executeUpdate($query, array $params = [], array $types = []) + { + return $this->executeStatement($query, $params, $types); + } + + /** + * Executes an SQL statement with the given parameters and returns the number of affected rows. + * + * Could be used for: + * - DML statements: INSERT, UPDATE, DELETE, etc. + * - DDL statements: CREATE, DROP, ALTER, etc. + * - DCL statements: GRANT, REVOKE, etc. + * - Session control statements: ALTER SESSION, SET, DECLARE, etc. + * - Other statements that don't yield a row set. + * + * This method supports PDO binding types as well as DBAL mapping types. + * + * @param string $sql The statement SQL + * @param array $params The query parameters + * @param array $types The parameter types + * + * @return int The number of affected rows. + * + * @throws DBALException + */ + public function executeStatement($sql, array $params = [], array $types = []) { $connection = $this->getWrappedConnection(); $logger = $this->_config->getSQLLogger(); if ($logger) { - $logger->startQuery($query, $params, $types); + $logger->startQuery($sql, $params, $types); } try { if ($params) { - [$query, $params, $types] = SQLParserUtils::expandListParameters($query, $params, $types); + [$sql, $params, $types] = SQLParserUtils::expandListParameters($sql, $params, $types); - $stmt = $connection->prepare($query); + $stmt = $connection->prepare($sql); if ($types) { $this->_bindTypedValues($stmt, $params, $types); @@ -1317,10 +1344,10 @@ public function executeUpdate($query, array $params = [], array $types = []) $result = $stmt->rowCount(); } else { - $result = $connection->exec($query); + $result = $connection->exec($sql); } } catch (Throwable $e) { - $this->handleExceptionDuringQuery($e, $query, $params, $types); + $this->handleExceptionDuringQuery($e, $sql, $params, $types); } if ($logger) { diff --git a/lib/Doctrine/DBAL/Connections/PrimaryReadReplicaConnection.php b/lib/Doctrine/DBAL/Connections/PrimaryReadReplicaConnection.php index 86aad20d14b..8819d34ecba 100644 --- a/lib/Doctrine/DBAL/Connections/PrimaryReadReplicaConnection.php +++ b/lib/Doctrine/DBAL/Connections/PrimaryReadReplicaConnection.php @@ -26,7 +26,7 @@ * * 1. Replica if primary was never picked before and ONLY if 'getWrappedConnection' * or 'executeQuery' is used. - * 2. Primary picked when 'exec', 'executeUpdate', 'insert', 'delete', 'update', 'createSavepoint', + * 2. Primary picked when 'exec', 'executeUpdate', 'executeStatement', 'insert', 'delete', 'update', 'createSavepoint', * 'releaseSavepoint', 'beginTransaction', 'rollback', 'commit', 'query' or * 'prepare' is called. * 3. If Primary was picked once during the lifetime of the connection it will always get picked afterwards. @@ -41,7 +41,7 @@ * Be aware that Connection#executeQuery is a method specifically for READ * operations only. * - * Use Connection#executeUpdate for any SQL statement that changes/updates + * Use Connection#executeStatement for any SQL statement that changes/updates * state in the database (UPDATE, INSERT, DELETE or DDL statements). * * This connection is limited to replica operations using the @@ -256,6 +256,8 @@ protected function chooseConnectionConfiguration($connectionName, $params) /** * {@inheritDoc} + * + * @deprecated Use {@link executeStatement()} instead. */ public function executeUpdate($query, array $params = [], array $types = []) { @@ -264,6 +266,16 @@ public function executeUpdate($query, array $params = [], array $types = []) return parent::executeUpdate($query, $params, $types); } + /** + * {@inheritDoc} + */ + public function executeStatement($query, array $params = [], array $types = []) + { + $this->ensureConnectedToPrimary(); + + return parent::executeStatement($query, $params, $types); + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Event/Listeners/MysqlSessionInit.php b/lib/Doctrine/DBAL/Event/Listeners/MysqlSessionInit.php index 9e722904050..8748dd17474 100644 --- a/lib/Doctrine/DBAL/Event/Listeners/MysqlSessionInit.php +++ b/lib/Doctrine/DBAL/Event/Listeners/MysqlSessionInit.php @@ -45,7 +45,7 @@ public function __construct($charset = 'utf8', $collation = false) public function postConnect(ConnectionEventArgs $args) { $collation = $this->collation ? ' COLLATE ' . $this->collation : ''; - $args->getConnection()->executeUpdate('SET NAMES ' . $this->charset . $collation); + $args->getConnection()->executeStatement('SET NAMES ' . $this->charset . $collation); } /** diff --git a/lib/Doctrine/DBAL/Event/Listeners/OracleSessionInit.php b/lib/Doctrine/DBAL/Event/Listeners/OracleSessionInit.php index 3e382f72c61..a907716f8ea 100644 --- a/lib/Doctrine/DBAL/Event/Listeners/OracleSessionInit.php +++ b/lib/Doctrine/DBAL/Event/Listeners/OracleSessionInit.php @@ -61,7 +61,7 @@ public function postConnect(ConnectionEventArgs $args) } $sql = 'ALTER SESSION SET ' . implode(' ', $vars); - $args->getConnection()->executeUpdate($sql); + $args->getConnection()->executeStatement($sql); } /** diff --git a/lib/Doctrine/DBAL/Id/TableGenerator.php b/lib/Doctrine/DBAL/Id/TableGenerator.php index 6b5a62353a4..c6a11a9c5cf 100644 --- a/lib/Doctrine/DBAL/Id/TableGenerator.php +++ b/lib/Doctrine/DBAL/Id/TableGenerator.php @@ -131,7 +131,7 @@ public function nextValue($sequenceName) $sql = 'UPDATE ' . $this->generatorTableName . ' ' . 'SET sequence_value = sequence_value + sequence_increment_by ' . 'WHERE sequence_name = ? AND sequence_value = ?'; - $rows = $this->conn->executeUpdate($sql, [$sequenceName, $row['sequence_value']]); + $rows = $this->conn->executeStatement($sql, [$sequenceName, $row['sequence_value']]); if ($rows !== 1) { throw new DBALException('Race-condition detected while updating sequence. Aborting generation'); diff --git a/lib/Doctrine/DBAL/Query/QueryBuilder.php b/lib/Doctrine/DBAL/Query/QueryBuilder.php index 5fe983c59f7..e48c370f709 100644 --- a/lib/Doctrine/DBAL/Query/QueryBuilder.php +++ b/lib/Doctrine/DBAL/Query/QueryBuilder.php @@ -196,9 +196,6 @@ public function getState() /** * Executes this query using the bound parameters and their types. * - * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} - * for insert, update and delete statements. - * * @return ResultStatement|int */ public function execute() @@ -207,7 +204,7 @@ public function execute() return $this->connection->executeQuery($this->getSQL(), $this->params, $this->paramTypes); } - return $this->connection->executeUpdate($this->getSQL(), $this->params, $this->paramTypes); + return $this->connection->executeStatement($this->getSQL(), $this->params, $this->paramTypes); } /** diff --git a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php index 7f9b8ad6572..93276b21ebc 100644 --- a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php @@ -1033,7 +1033,7 @@ protected function _getPortableTableForeignKeyDefinition($tableForeignKey) protected function _execSql($sql) { foreach ((array) $sql as $query) { - $this->_conn->executeUpdate($query); + $this->_conn->executeStatement($query); } } diff --git a/lib/Doctrine/DBAL/Schema/OracleSchemaManager.php b/lib/Doctrine/DBAL/Schema/OracleSchemaManager.php index 301b7ae0184..f7e6a810577 100644 --- a/lib/Doctrine/DBAL/Schema/OracleSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/OracleSchemaManager.php @@ -307,10 +307,10 @@ public function createDatabase($database = null) $password = $params['password']; $query = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password; - $this->_conn->executeUpdate($query); + $this->_conn->executeStatement($query); $query = 'GRANT DBA TO ' . $username; - $this->_conn->executeUpdate($query); + $this->_conn->executeStatement($query); } /** @@ -324,7 +324,7 @@ public function dropAutoincrement($table) $sql = $this->_platform->getDropAutoincrementSql($table); foreach ($sql as $query) { - $this->_conn->executeUpdate($query); + $this->_conn->executeStatement($query); } return true; diff --git a/lib/Doctrine/DBAL/Tools/Console/Command/RunSqlCommand.php b/lib/Doctrine/DBAL/Tools/Console/Command/RunSqlCommand.php index 68d5a136fdd..82ef07006f5 100644 --- a/lib/Doctrine/DBAL/Tools/Console/Command/RunSqlCommand.php +++ b/lib/Doctrine/DBAL/Tools/Console/Command/RunSqlCommand.php @@ -87,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if (stripos($sql, 'select') === 0 || $input->getOption('force-fetch')) { $resultSet = $conn->fetchAllAssociative($sql); } else { - $resultSet = $conn->executeUpdate($sql); + $resultSet = $conn->executeStatement($sql); } $output->write(Dumper::dump($resultSet, (int) $depth)); diff --git a/tests/Doctrine/Tests/DBAL/Connection/LoggingTest.php b/tests/Doctrine/Tests/DBAL/Connection/LoggingTest.php index 7ffc32b6e06..9a9da438ab3 100644 --- a/tests/Doctrine/Tests/DBAL/Connection/LoggingTest.php +++ b/tests/Doctrine/Tests/DBAL/Connection/LoggingTest.php @@ -22,13 +22,13 @@ public function testLogExecuteQuery(): void ->executeQuery('SELECT * FROM table'); } - public function testLogExecuteUpdate(): void + public function testLogExecuteStatement(): void { $this->createConnection( $this->createStub(DriverConnection::class), 'UPDATE table SET foo = ?' ) - ->executeUpdate('UPDATE table SET foo = ?'); + ->executeStatement('UPDATE table SET foo = ?'); } public function testLogPrepareExecute(): void diff --git a/tests/Doctrine/Tests/DBAL/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/ConnectionTest.php index a0007208bd5..0d4d380993e 100644 --- a/tests/Doctrine/Tests/DBAL/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/ConnectionTest.php @@ -54,7 +54,7 @@ protected function setUp(): void /** * @return Connection|MockObject */ - private function getExecuteUpdateMockConnection() + private function getExecuteStatementMockConnection() { $driverMock = $this->createMock(Driver::class); @@ -67,7 +67,7 @@ private function getExecuteUpdateMockConnection() $platform = $this->getMockForAbstractClass(AbstractPlatform::class); return $this->getMockBuilder(Connection::class) - ->onlyMethods(['executeUpdate']) + ->onlyMethods(['executeStatement']) ->setConstructorArgs([['platform' => $platform], $driverMock]) ->getMock(); } @@ -203,7 +203,7 @@ public static function getQueryMethods(): iterable ['exec'], ['query'], ['executeQuery'], - ['executeUpdate'], + ['executeStatement'], ['prepare'], ]; } @@ -372,10 +372,10 @@ public function testSwitchingAutoCommitModeCommitsAllCurrentTransactions(): void public function testEmptyInsert(): void { - $conn = $this->getExecuteUpdateMockConnection(); + $conn = $this->getExecuteStatementMockConnection(); $conn->expects($this->once()) - ->method('executeUpdate') + ->method('executeStatement') ->with('INSERT INTO footable () VALUES ()'); $conn->insert('footable', []); @@ -386,10 +386,10 @@ public function testEmptyInsert(): void */ public function testUpdateWithDifferentColumnsInDataAndIdentifiers(): void { - $conn = $this->getExecuteUpdateMockConnection(); + $conn = $this->getExecuteStatementMockConnection(); $conn->expects($this->once()) - ->method('executeUpdate') + ->method('executeStatement') ->with( 'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND name = ?', [ @@ -430,10 +430,10 @@ public function testUpdateWithDifferentColumnsInDataAndIdentifiers(): void */ public function testUpdateWithSameColumnInDataAndIdentifiers(): void { - $conn = $this->getExecuteUpdateMockConnection(); + $conn = $this->getExecuteStatementMockConnection(); $conn->expects($this->once()) - ->method('executeUpdate') + ->method('executeStatement') ->with( 'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND is_edited = ?', [ @@ -473,10 +473,10 @@ public function testUpdateWithSameColumnInDataAndIdentifiers(): void */ public function testUpdateWithIsNull(): void { - $conn = $this->getExecuteUpdateMockConnection(); + $conn = $this->getExecuteStatementMockConnection(); $conn->expects($this->once()) - ->method('executeUpdate') + ->method('executeStatement') ->with( 'UPDATE TestTable SET text = ?, is_edited = ? WHERE id IS NULL AND name = ?', [ @@ -515,10 +515,10 @@ public function testUpdateWithIsNull(): void */ public function testDeleteWithIsNull(): void { - $conn = $this->getExecuteUpdateMockConnection(); + $conn = $this->getExecuteStatementMockConnection(); $conn->expects($this->once()) - ->method('executeUpdate') + ->method('executeStatement') ->with( 'DELETE FROM TestTable WHERE id IS NULL AND name = ?', ['foo'], @@ -725,7 +725,7 @@ public static function dataCallConnectOnce(): iterable ['insert', ['tbl', ['data' => 'foo']]], ['update', ['tbl', ['data' => 'bar'], ['id' => 12345]]], ['prepare', ['select * from dual']], - ['executeUpdate', ['insert into tbl (id) values (?)'], [123]], + ['executeStatement', ['insert into tbl (id) values (?)'], [123]], ]; } diff --git a/tests/Doctrine/Tests/DBAL/Events/MysqlSessionInitTest.php b/tests/Doctrine/Tests/DBAL/Events/MysqlSessionInitTest.php index 73df349a832..e2641e60106 100644 --- a/tests/Doctrine/Tests/DBAL/Events/MysqlSessionInitTest.php +++ b/tests/Doctrine/Tests/DBAL/Events/MysqlSessionInitTest.php @@ -14,7 +14,7 @@ public function testPostConnect(): void { $connectionMock = $this->createMock(Connection::class); $connectionMock->expects($this->once()) - ->method('executeUpdate') + ->method('executeStatement') ->with($this->equalTo('SET NAMES foo COLLATE bar')); $eventArgs = new ConnectionEventArgs($connectionMock); diff --git a/tests/Doctrine/Tests/DBAL/Events/OracleSessionInitTest.php b/tests/Doctrine/Tests/DBAL/Events/OracleSessionInitTest.php index a9dc64b8855..0de02b2f3c8 100644 --- a/tests/Doctrine/Tests/DBAL/Events/OracleSessionInitTest.php +++ b/tests/Doctrine/Tests/DBAL/Events/OracleSessionInitTest.php @@ -16,7 +16,7 @@ public function testPostConnect(): void { $connectionMock = $this->createMock(Connection::class); $connectionMock->expects($this->once()) - ->method('executeUpdate') + ->method('executeStatement') ->with($this->isType('string')); $eventArgs = new ConnectionEventArgs($connectionMock); @@ -35,7 +35,7 @@ public function testPostConnectQuotesSessionParameterValues(string $name, string ->disableOriginalConstructor() ->getMock(); $connectionMock->expects($this->once()) - ->method('executeUpdate') + ->method('executeStatement') ->with($this->stringContains(sprintf('%s = %s', $name, $value))); $eventArgs = new ConnectionEventArgs($connectionMock); diff --git a/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php b/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php index 0861608d888..eb388117140 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php @@ -437,12 +437,12 @@ public function testExecuteQueryBindDateTimeType(): void /** * @group DDC-697 */ - public function testExecuteUpdateBindDateTimeType(): void + public function testExecuteStatementBindDateTimeType(): void { $datetime = new DateTime('2010-02-02 20:20:20'); $sql = 'INSERT INTO fetch_table (test_int, test_string, test_datetime) VALUES (?, ?, ?)'; - $affectedRows = $this->connection->executeUpdate($sql, [ + $affectedRows = $this->connection->executeStatement($sql, [ 1 => 50, 2 => 'foo', 3 => $datetime, @@ -800,7 +800,7 @@ public function testFetchAllSupportFetchClass(): void public function testFetchAllStyleColumn(): void { $sql = 'DELETE FROM fetch_table'; - $this->connection->executeUpdate($sql); + $this->connection->executeStatement($sql); $this->connection->insert('fetch_table', ['test_int' => 1, 'test_string' => 'foo']); $this->connection->insert('fetch_table', ['test_int' => 10, 'test_string' => 'foo']); @@ -903,7 +903,7 @@ public function testEmptyParameters(): void */ public function testFetchColumnNullValue(): void { - $this->connection->executeUpdate( + $this->connection->executeStatement( 'INSERT INTO fetch_table (test_int, test_string) VALUES (?, ?)', [2, 'foo'] ); diff --git a/tests/Doctrine/Tests/DBAL/Functional/Driver/OCI8/ConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/Driver/OCI8/ConnectionTest.php index 6f296b6d286..70051407f77 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Driver/OCI8/ConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Driver/OCI8/ConnectionTest.php @@ -40,7 +40,7 @@ public function testLastInsertIdAcceptsFqn(): void $schemaManager->dropAndCreateTable($table); - $this->connection->executeUpdate('INSERT INTO DBAL2595 (foo) VALUES (1)'); + $this->connection->executeStatement('INSERT INTO DBAL2595 (foo) VALUES (1)'); $schema = $this->connection->getDatabase(); $sequence = $platform->getIdentitySequenceName($schema . '.DBAL2595', 'id'); diff --git a/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php index 70ce747fca1..95b2224f9d5 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php @@ -201,7 +201,7 @@ public function testForeignKeyConstraintViolationExceptionOnTruncate(): void $this->expectException(Exception\ForeignKeyConstraintViolationException::class); try { - $this->connection->executeUpdate($platform->getTruncateTableSQL('constraint_error_table')); + $this->connection->executeStatement($platform->getTruncateTableSQL('constraint_error_table')); } catch (Exception\ForeignKeyConstraintViolationException $exception) { $this->tearDownForeignKeyConstraintViolationExceptionTest(); diff --git a/tests/Doctrine/Tests/DBAL/Functional/MasterSlaveConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/MasterSlaveConnectionTest.php index ac7d84d030e..9cd2b5e3f4f 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/MasterSlaveConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/MasterSlaveConnectionTest.php @@ -43,7 +43,7 @@ protected function setUp(): void } catch (Throwable $e) { } - $this->connection->executeUpdate('DELETE FROM master_slave_table'); + $this->connection->executeStatement('DELETE FROM master_slave_table'); $this->connection->insert('master_slave_table', ['test_int' => 1]); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/PrimaryReadReplicaConnectionTest.php b/tests/Doctrine/Tests/DBAL/Functional/PrimaryReadReplicaConnectionTest.php index f2c714940a6..7dd14c355ee 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/PrimaryReadReplicaConnectionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/PrimaryReadReplicaConnectionTest.php @@ -43,7 +43,7 @@ protected function setUp(): void } catch (Throwable $e) { } - $this->connection->executeUpdate('DELETE FROM primary_replica_table'); + $this->connection->executeStatement('DELETE FROM primary_replica_table'); $this->connection->insert('primary_replica_table', ['test_int' => 1]); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php index ce175add9de..3f0d7bf1569 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php @@ -458,7 +458,7 @@ public function testColumnDefaultsAreValid(): void $this->schemaManager->dropAndCreateTable($table); - $this->connection->executeUpdate( + $this->connection->executeStatement( 'INSERT INTO test_column_defaults_are_valid () VALUES()' ); diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php index 54d7df5abbb..611cc727fa7 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -224,7 +224,7 @@ public function testListNamespaceNames(): void $namespaces = array_map('strtolower', $namespaces); if (! in_array('test_create_schema', $namespaces)) { - $this->connection->executeUpdate($this->schemaManager->getDatabasePlatform()->getCreateSchemaSQL('test_create_schema')); + $this->connection->executeStatement($this->schemaManager->getDatabasePlatform()->getCreateSchemaSQL('test_create_schema')); $namespaces = $this->schemaManager->listNamespaceNames(); $namespaces = array_map('strtolower', $namespaces); diff --git a/tests/Doctrine/Tests/DBAL/Functional/TemporaryTableTest.php b/tests/Doctrine/Tests/DBAL/Functional/TemporaryTableTest.php index 7ee7fb23ff9..345005b69c3 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/TemporaryTableTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/TemporaryTableTest.php @@ -49,7 +49,7 @@ public function testDropTemporaryTableNotAutoCommitTransaction(): void $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' (' . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')'; - $this->connection->executeUpdate($createTempTableSQL); + $this->connection->executeStatement($createTempTableSQL); $table = new Table('nontemporary'); $table->addColumn('id', 'integer'); diff --git a/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php b/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php index ca63bc0dc6d..9f2b1a8ccc3 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php @@ -48,7 +48,7 @@ protected function tearDown(): void public function testBooleanConversionSqlLiteral(): void { - $this->connection->executeUpdate('INSERT INTO dbal630 (bool_col) VALUES(false)'); + $this->connection->executeStatement('INSERT INTO dbal630 (bool_col) VALUES(false)'); $id = $this->connection->lastInsertId('dbal630_id_seq'); self::assertNotEmpty($id); @@ -59,7 +59,7 @@ public function testBooleanConversionSqlLiteral(): void public function testBooleanConversionBoolParamRealPrepares(): void { - $this->connection->executeUpdate( + $this->connection->executeStatement( 'INSERT INTO dbal630 (bool_col) VALUES(?)', ['false'], [ParameterType::BOOLEAN] diff --git a/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php index aac223c39ff..ce5712f05e1 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php @@ -32,39 +32,39 @@ protected function setUp(): void } catch (Throwable $e) { } - $this->connection->executeUpdate('DELETE FROM write_table'); + $this->connection->executeStatement('DELETE FROM write_table'); } /** * @group DBAL-80 */ - public function testExecuteUpdateFirstTypeIsNull(): void + public function testExecuteStatementFirstTypeIsNull(): void { $sql = 'INSERT INTO write_table (test_string, test_int) VALUES (?, ?)'; - $this->connection->executeUpdate($sql, ['text', 1111], [null, ParameterType::INTEGER]); + $this->connection->executeStatement($sql, ['text', 1111], [null, ParameterType::INTEGER]); $sql = 'SELECT * FROM write_table WHERE test_string = ? AND test_int = ?'; self::assertTrue((bool) $this->connection->fetchColumn($sql, ['text', 1111])); } - public function testExecuteUpdate(): void + public function testExecuteStatement(): void { $sql = 'INSERT INTO write_table (test_int) VALUES ( ' . $this->connection->quote(1) . ')'; - $affected = $this->connection->executeUpdate($sql); + $affected = $this->connection->executeStatement($sql); - self::assertEquals(1, $affected, 'executeUpdate() should return the number of affected rows!'); + self::assertEquals(1, $affected, 'executeStatement() should return the number of affected rows!'); } - public function testExecuteUpdateWithTypes(): void + public function testExecuteStatementWithTypes(): void { $sql = 'INSERT INTO write_table (test_int, test_string) VALUES (?, ?)'; - $affected = $this->connection->executeUpdate( + $affected = $this->connection->executeStatement( $sql, [1, 'foo'], [ParameterType::INTEGER, ParameterType::STRING] ); - self::assertEquals(1, $affected, 'executeUpdate() should return the number of affected rows!'); + self::assertEquals(1, $affected, 'executeStatement() should return the number of affected rows!'); } public function testPrepareRowCountReturnsAffectedRows(): void diff --git a/tests/Doctrine/Tests/DBAL/Tools/Console/RunSqlCommandTest.php b/tests/Doctrine/Tests/DBAL/Tools/Console/RunSqlCommandTest.php index 3c9013a0ca7..8a99dd366b8 100644 --- a/tests/Doctrine/Tests/DBAL/Tools/Console/RunSqlCommandTest.php +++ b/tests/Doctrine/Tests/DBAL/Tools/Console/RunSqlCommandTest.php @@ -79,7 +79,7 @@ public function testSelectStatementsPrintsResult(): void public function testUpdateStatementsPrintsAffectedLines(): void { - $this->expectConnectionExecuteUpdate(); + $this->expectConnectionExecuteStatement(); $this->commandTester->execute([ 'command' => $this->command->getName(), @@ -90,11 +90,11 @@ public function testUpdateStatementsPrintsAffectedLines(): void self::assertDoesNotMatchRegularExpression('@array.*1.*@', $this->commandTester->getDisplay()); } - private function expectConnectionExecuteUpdate(): void + private function expectConnectionExecuteStatement(): void { $this->connectionMock ->expects($this->once()) - ->method('executeUpdate') + ->method('executeStatement') ->willReturn(42); $this->connectionMock @@ -111,7 +111,7 @@ private function expectConnectionFetchAllAssociative(): void $this->connectionMock ->expects($this->never()) - ->method('executeUpdate'); + ->method('executeStatement'); } public function testStatementsWithFetchResultPrintsResult(): void From 35e9150dd4813b6460cb3e0148a5ac241e95508e Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Fri, 10 Jul 2020 19:04:16 -0700 Subject: [PATCH 2/2] Deprecate query() and exec() in favor of executeQuery() and executeStatement() --- UPGRADE.md | 2 ++ lib/Doctrine/DBAL/Connection.php | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/UPGRADE.md b/UPGRADE.md index ea130f77144..c6765ebf1bc 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -3,6 +3,8 @@ ## Deprecations in the wrapper `Connection` class 1. The `executeUpdate()` method has been deprecated in favor of `executeStatement()`. +2. The `query()` method has been deprecated in favor of `executeQuery()`. +3. The `exec()` method has been deprecated in favor of `executeStatement()`. ## PDO-related classes outside of the PDO namespace are deprecated diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index 3aa16f2b20b..cd4c33109eb 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -1249,6 +1249,8 @@ public function project($query, array $params, Closure $function) /** * Executes an SQL statement, returning a result set as a Statement object. * + * @deprecated Use {@link executeQuery()} instead. + * * @return \Doctrine\DBAL\Driver\Statement * * @throws DBALException @@ -1360,6 +1362,8 @@ public function executeStatement($sql, array $params = [], array $types = []) /** * Executes an SQL statement and return the number of affected rows. * + * @deprecated Use {@link executeStatement()} instead. + * * @param string $statement * * @return int The number of affected rows.