From ddd921f2c253de0633189464bf99d26db53fe22d Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sun, 27 Jun 2021 16:48:09 +0200 Subject: [PATCH] Fix deprecated DBAL calls --- lib/Doctrine/ORM/Id/SequenceGenerator.php | 2 +- lib/Doctrine/ORM/Id/TableGenerator.php | 4 +-- lib/Doctrine/ORM/Id/UuidGenerator.php | 2 +- .../Collection/ManyToManyPersister.php | 10 +++--- .../Collection/OneToManyPersister.php | 10 +++--- .../Entity/BasicEntityPersister.php | 12 +++---- .../Entity/JoinedSubclassPersister.php | 10 ++---- .../Query/Exec/MultiTableUpdateExecutor.php | 18 ++++------ .../Exec/SingleTableDeleteUpdateExecutor.php | 2 +- lib/Doctrine/ORM/Tools/SchemaTool.php | 2 +- tests/Doctrine/Tests/Mocks/ConnectionMock.php | 33 +++++++++++++++---- tests/Doctrine/Tests/Mocks/StatementMock.php | 7 ++-- .../BasicEntityPersisterTypeValueSqlTest.php | 4 +-- .../Persisters/ManyToManyPersisterTest.php | 8 ++--- 14 files changed, 64 insertions(+), 60 deletions(-) diff --git a/lib/Doctrine/ORM/Id/SequenceGenerator.php b/lib/Doctrine/ORM/Id/SequenceGenerator.php index 1a532cc557b..10b9d26b785 100644 --- a/lib/Doctrine/ORM/Id/SequenceGenerator.php +++ b/lib/Doctrine/ORM/Id/SequenceGenerator.php @@ -74,7 +74,7 @@ public function generate(EntityManager $em, $entity) $sql = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName); // Using `query` to force usage of the master server in MasterSlaveConnection - $this->_nextValue = (int) $conn->query($sql)->fetchColumn(); + $this->_nextValue = (int) $conn->executeQuery($sql)->fetchOne(); $this->_maxValue = $this->_nextValue + $this->_allocationSize; } diff --git a/lib/Doctrine/ORM/Id/TableGenerator.php b/lib/Doctrine/ORM/Id/TableGenerator.php index bf8994d9bdb..5bea2319825 100644 --- a/lib/Doctrine/ORM/Id/TableGenerator.php +++ b/lib/Doctrine/ORM/Id/TableGenerator.php @@ -68,7 +68,7 @@ public function generate( if ($conn->getTransactionNestingLevel() === 0) { // use select for update $sql = $conn->getDatabasePlatform()->getTableHiLoCurrentValSql($this->_tableName, $this->_sequenceName); - $currentLevel = $conn->fetchColumn($sql); + $currentLevel = $conn->fetchOne($sql); if ($currentLevel !== null) { $this->_nextValue = $currentLevel; @@ -80,7 +80,7 @@ public function generate( $this->_allocationSize ); - if ($conn->executeUpdate($updateSql, [1 => $currentLevel, 2 => $currentLevel + 1]) !== 1) { + if ($conn->executeStatement($updateSql, [1 => $currentLevel, 2 => $currentLevel + 1]) !== 1) { // no affected rows, concurrency issue, throw exception } } else { diff --git a/lib/Doctrine/ORM/Id/UuidGenerator.php b/lib/Doctrine/ORM/Id/UuidGenerator.php index b48c81af12b..2555e7e33dd 100644 --- a/lib/Doctrine/ORM/Id/UuidGenerator.php +++ b/lib/Doctrine/ORM/Id/UuidGenerator.php @@ -35,6 +35,6 @@ public function generate(EntityManager $em, $entity) $conn = $em->getConnection(); $sql = 'SELECT ' . $conn->getDatabasePlatform()->getGuidExpression(); - return $conn->query($sql)->fetchColumn(0); + return $conn->executeQuery($sql)->fetchOne(); } } diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index 3720e47b090..36e1dc8d3a3 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -61,7 +61,7 @@ public function delete(PersistentCollection $collection) $types[] = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $class, $this->em); } - $this->conn->executeUpdate($this->getDeleteSQL($collection), $this->getDeleteSQLParameters($collection), $types); + $this->conn->executeStatement($this->getDeleteSQL($collection), $this->getDeleteSQLParameters($collection), $types); } /** @@ -79,7 +79,7 @@ public function update(PersistentCollection $collection) [$insertSql, $insertTypes] = $this->getInsertRowSQL($collection); foreach ($collection->getDeleteDiff() as $element) { - $this->conn->executeUpdate( + $this->conn->executeStatement( $deleteSql, $this->getDeleteRowSQLParameters($collection, $element), $deleteTypes @@ -87,7 +87,7 @@ public function update(PersistentCollection $collection) } foreach ($collection->getInsertDiff() as $element) { - $this->conn->executeUpdate( + $this->conn->executeStatement( $insertSql, $this->getInsertRowSQLParameters($collection, $element), $insertTypes @@ -170,7 +170,7 @@ public function count(PersistentCollection $collection) . $joinTargetEntitySQL . ' WHERE ' . implode(' AND ', $conditions); - return $this->conn->fetchColumn($sql, $params, 0, $types); + return (int) $this->conn->fetchOne($sql, $params, $types); } /** @@ -223,7 +223,7 @@ public function contains(PersistentCollection $collection, $element) $sql = 'SELECT 1 FROM ' . $quotedJoinTable . ' WHERE ' . implode(' AND ', $whereClauses); - return (bool) $this->conn->fetchColumn($sql, $params, 0, $types); + return (bool) $this->conn->fetchOne($sql, $params, $types); } /** diff --git a/lib/Doctrine/ORM/Persisters/Collection/OneToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/OneToManyPersister.php index 36efcc6999a..bc225c84bfa 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/OneToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/OneToManyPersister.php @@ -199,7 +199,7 @@ private function deleteEntityCollection(PersistentCollection $collection): int $statement = 'DELETE FROM ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' WHERE ' . implode(' = ? AND ', $columns) . ' = ?'; - return $this->conn->executeUpdate($statement, $parameters); + return $this->conn->executeStatement($statement, $parameters); } /** @@ -233,7 +233,7 @@ private function deleteJoinedEntityCollection(PersistentCollection $collection): $statement = $this->platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' (' . $this->platform->getColumnDeclarationListSQL($columnDefinitions) . ')'; - $this->conn->executeUpdate($statement); + $this->conn->executeStatement($statement); // 2) Build insert table records into temporary table $query = $this->em->createQuery( @@ -243,7 +243,7 @@ private function deleteJoinedEntityCollection(PersistentCollection $collection): $statement = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ') ' . $query->getSQL(); $parameters = array_values($sourceClass->getIdentifierValues($collection->getOwner())); - $numDeleted = $this->conn->executeUpdate($statement, $parameters); + $numDeleted = $this->conn->executeStatement($statement, $parameters); // 3) Delete records on each table in the hierarchy $classNames = array_merge($targetClass->parentClasses, [$targetClass->name], $targetClass->subClasses); @@ -253,13 +253,13 @@ private function deleteJoinedEntityCollection(PersistentCollection $collection): $statement = 'DELETE FROM ' . $tableName . ' WHERE (' . $idColumnList . ')' . ' IN (SELECT ' . $idColumnList . ' FROM ' . $tempTable . ')'; - $this->conn->executeUpdate($statement); + $this->conn->executeStatement($statement); } // 4) Drop temporary table $statement = $this->platform->getDropTemporaryTableSQL($tempTable); - $this->conn->executeUpdate($statement); + $this->conn->executeStatement($statement); return $numDeleted; } diff --git a/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php index 2ed07d7985d..f67a978492b 100644 --- a/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php @@ -289,7 +289,7 @@ public function executeInserts() } } - $stmt->execute(); + $stmt->executeStatement(); if ($isPostInsertId) { $generatedId = $idGenerator->generate($this->em, $entity); @@ -307,7 +307,6 @@ public function executeInserts() } } - $stmt->closeCursor(); $this->queuedInserts = []; return $postInsertIds; @@ -353,10 +352,9 @@ protected function fetchVersionValue($versionedClass, array $id) $flatId = $this->identifierFlattener->flattenIdentifier($versionedClass, $id); - $value = $this->conn->fetchColumn( + $value = $this->conn->fetchOne( $sql, array_values($flatId), - 0, $this->extractIdentifierTypes($id, $versionedClass) ); @@ -514,7 +512,7 @@ final protected function updateTable( . ' SET ' . implode(', ', $set) . ' WHERE ' . implode(' = ? AND ', $where) . ' = ?'; - $result = $this->conn->executeUpdate($sql, $params, $types); + $result = $this->conn->executeStatement($sql, $params, $types); if ($versioned && ! $result) { throw OptimisticLockException::lockFailed($entity); @@ -837,7 +835,7 @@ public function count($criteria = []) ? $this->expandCriteriaParameters($criteria) : $this->expandParameters($criteria); - return (int) $this->conn->executeQuery($sql, $params, $types)->fetchColumn(); + return (int) $this->conn->executeQuery($sql, $params, $types)->fetchOne(); } /** @@ -2015,7 +2013,7 @@ public function exists($entity, ?Criteria $extraConditions = null) $sql .= ' AND ' . $filterSql; } - return (bool) $this->conn->fetchColumn($sql, $params, 0, $types); + return (bool) $this->conn->fetchOne($sql, $params, $types); } /** diff --git a/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php index 686612f50a5..87a519eef36 100644 --- a/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/Entity/JoinedSubclassPersister.php @@ -169,7 +169,7 @@ public function executeInserts() $rootTableStmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]); } - $rootTableStmt->execute(); + $rootTableStmt->executeStatement(); if ($isPostInsertId) { $generatedId = $idGenerator->generate($this->em, $entity); @@ -204,16 +204,10 @@ public function executeInserts() } } - $stmt->execute(); + $stmt->executeStatement(); } } - $rootTableStmt->closeCursor(); - - foreach ($subTableStmts as $stmt) { - $stmt->closeCursor(); - } - $this->queuedInserts = []; return $postInsertIds; diff --git a/lib/Doctrine/ORM/Query/Exec/MultiTableUpdateExecutor.php b/lib/Doctrine/ORM/Query/Exec/MultiTableUpdateExecutor.php index 5f1526d8642..1e5f8f90e64 100644 --- a/lib/Doctrine/ORM/Query/Exec/MultiTableUpdateExecutor.php +++ b/lib/Doctrine/ORM/Query/Exec/MultiTableUpdateExecutor.php @@ -164,11 +164,11 @@ public function __construct(AST\Node $AST, $sqlWalker) public function execute(Connection $conn, array $params, array $types) { // Create temporary id table - $conn->executeUpdate($this->_createTempTableSql); + $conn->executeStatement($this->_createTempTableSql); try { // Insert identifiers. Parameters from the update clause are cut off. - $numUpdated = $conn->executeUpdate( + $numUpdated = $conn->executeStatement( $this->_insertSql, array_slice($params, $this->_numParametersInUpdateClause), array_slice($types, $this->_numParametersInUpdateClause) @@ -186,19 +186,13 @@ public function execute(Connection $conn, array $params, array $types) } } - $conn->executeUpdate($statement, $paramValues, $paramTypes); + $conn->executeStatement($statement, $paramValues, $paramTypes); } - } catch (Throwable $exception) { - // FAILURE! Drop temporary table to avoid possible collisions - $conn->executeUpdate($this->_dropTempTableSql); - - // Re-throw exception - throw $exception; + } finally { + // Drop temporary table + $conn->executeStatement($this->_dropTempTableSql); } - // Drop temporary table - $conn->executeUpdate($this->_dropTempTableSql); - return $numUpdated; } } diff --git a/lib/Doctrine/ORM/Query/Exec/SingleTableDeleteUpdateExecutor.php b/lib/Doctrine/ORM/Query/Exec/SingleTableDeleteUpdateExecutor.php index 404ae994e55..3902d340e48 100644 --- a/lib/Doctrine/ORM/Query/Exec/SingleTableDeleteUpdateExecutor.php +++ b/lib/Doctrine/ORM/Query/Exec/SingleTableDeleteUpdateExecutor.php @@ -53,6 +53,6 @@ public function __construct(AST\Node $AST, $sqlWalker) */ public function execute(Connection $conn, array $params, array $types) { - return $conn->executeUpdate($this->_sqlStatements, $params, $types); + return $conn->executeStatement($this->_sqlStatements, $params, $types); } } diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 87e8a7809b9..89d5969e2a7 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -777,7 +777,7 @@ private function gatherRelationJoinColumns( $blacklistedFks[$compositeName] = true; } elseif (! isset($blacklistedFks[$compositeName])) { $addedFks[$compositeName] = ['foreignTableName' => $foreignTableName, 'foreignColumns' => $foreignColumns]; - $theJoinTable->addUnnamedForeignKeyConstraint( + $theJoinTable->addForeignKeyConstraint( $foreignTableName, $localColumns, $foreignColumns, diff --git a/tests/Doctrine/Tests/Mocks/ConnectionMock.php b/tests/Doctrine/Tests/Mocks/ConnectionMock.php index 5aec4511787..ebc219252fa 100644 --- a/tests/Doctrine/Tests/Mocks/ConnectionMock.php +++ b/tests/Doctrine/Tests/Mocks/ConnectionMock.php @@ -4,15 +4,19 @@ namespace Doctrine\Tests\Mocks; +use BadMethodCallException; use Doctrine\Common\EventManager; +use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\Statement; +use Doctrine\DBAL\ForwardCompatibility\Result as ForwardCompatibilityResult; use Doctrine\DBAL\Platforms\AbstractPlatform; use Exception; use function is_string; +use function sprintf; /** * Mock class for Connection. @@ -25,7 +29,7 @@ class ConnectionMock extends Connection /** @var Exception|null */ private $_fetchOneException; - /** @var Statement|null */ + /** @var ForwardCompatibilityResult|null */ private $_queryResult; /** @var DatabasePlatformMock */ @@ -38,7 +42,7 @@ class ConnectionMock extends Connection private $_inserts = []; /** @var array */ - private $_executeUpdates = []; + private $_executeStatements = []; /** @var array */ private $_deletes = []; @@ -74,7 +78,17 @@ public function insert($tableName, array $data, array $types = []) */ public function executeUpdate($query, array $params = [], array $types = []) { - $this->_executeUpdates[] = ['query' => $query, 'params' => $params, 'types' => $types]; + throw new BadMethodCallException(sprintf('Call to deprecated method %s().', __METHOD__)); + } + + /** + * {@inheritdoc} + */ + public function executeStatement($sql, array $params = [], array $types = []): int + { + $this->_executeStatements[] = ['sql' => $sql, 'params' => $params, 'types' => $types]; + + return 1; } /** @@ -96,7 +110,7 @@ public function lastInsertId($seqName = null) /** * {@inheritdoc} */ - public function fetchColumn($statement, array $params = [], $colnum = 0, array $types = []) + public function fetchColumn($statement, array $params = [], $colunm = 0, array $types = []) { if ($this->_fetchOneException !== null) { throw $this->_fetchOneException; @@ -110,6 +124,11 @@ public function query(): Statement return $this->_queryResult; } + public function executeQuery($sql, array $params = [], $types = [], ?QueryCacheProfile $qcp = null): ForwardCompatibilityResult + { + return $this->_queryResult ?? parent::executeQuery($sql, $params, $types, $qcp); + } + /** * {@inheritdoc} */ @@ -149,7 +168,7 @@ public function setLastInsertId(int $id): void public function setQueryResult(Statement $result): void { - $this->_queryResult = $result; + $this->_queryResult = ForwardCompatibilityResult::ensure($result); } /** @@ -163,9 +182,9 @@ public function getInserts(): array /** * @return array */ - public function getExecuteUpdates(): array + public function getExecuteStatements(): array { - return $this->_executeUpdates; + return $this->_executeStatements; } /** diff --git a/tests/Doctrine/Tests/Mocks/StatementMock.php b/tests/Doctrine/Tests/Mocks/StatementMock.php index 9bf1d0fd66e..02780227def 100644 --- a/tests/Doctrine/Tests/Mocks/StatementMock.php +++ b/tests/Doctrine/Tests/Mocks/StatementMock.php @@ -48,11 +48,9 @@ public function execute($params = null) { } - /** - * {@inheritdoc} - */ - public function rowCount() + public function rowCount(): int { + return 1; } /** @@ -81,6 +79,7 @@ public function setFetchMode($fetchStyle, $arg2 = null, $arg3 = null) */ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { + return false; } /** diff --git a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php index 5642e7df5f9..bf5284f1964 100644 --- a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php +++ b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php @@ -72,9 +72,9 @@ public function testUpdateUsesTypeValuesSQL(): void $this->persister->update($parent); - $executeUpdates = $this->entityManager->getConnection()->getExecuteUpdates(); + $executeStatements = $this->entityManager->getConnection()->getExecuteStatements(); - $this->assertEquals('UPDATE customtype_parents SET customInteger = ABS(?), child_id = ? WHERE id = ?', $executeUpdates[0]['query']); + $this->assertEquals('UPDATE customtype_parents SET customInteger = ABS(?), child_id = ? WHERE id = ?', $executeStatements[0]['sql']); } public function testGetSelectConditionSQLUsesTypeValuesSQL(): void diff --git a/tests/Doctrine/Tests/ORM/Persisters/ManyToManyPersisterTest.php b/tests/Doctrine/Tests/ORM/Persisters/ManyToManyPersisterTest.php index 48cedc6c0b5..e992ef1ead9 100644 --- a/tests/Doctrine/Tests/ORM/Persisters/ManyToManyPersisterTest.php +++ b/tests/Doctrine/Tests/ORM/Persisters/ManyToManyPersisterTest.php @@ -48,10 +48,10 @@ public function testDeleteManyToManyCollection(): void $conn = $em->getConnection(); assert($conn instanceof ConnectionMock); - $updates = $conn->getExecuteUpdates(); - $lastUpdate = array_pop($updates); + $updates = $conn->getExecuteStatements(); + $lastStatement = array_pop($updates); - self::assertEquals('DELETE FROM parent_child WHERE child_id1 = ? AND child_id2 = ?', $lastUpdate['query']); - self::assertEquals([1, 42], $lastUpdate['params']); + self::assertEquals('DELETE FROM parent_child WHERE child_id1 = ? AND child_id2 = ?', $lastStatement['sql']); + self::assertEquals([1, 42], $lastStatement['params']); } }