From f2b0a37c4d48ad4ed39c1e7704cf4eed792f52c3 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sat, 27 Jan 2024 15:19:48 +0100 Subject: [PATCH] Add a SingleSelectSqlFinalizer that can take care of adding offset/limit as well as locking mode statements to a given SQL query. Add a FinalizedSelectExecutor that executes given, finalized SQL statements. --- .../Query/Exec/FinalizedSelectExecutor.php | 26 ++++++++ .../Query/Exec/SingleSelectSqlFinalizer.php | 60 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php create mode 100644 lib/Doctrine/ORM/Query/Exec/SingleSelectSqlFinalizer.php diff --git a/lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php b/lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php new file mode 100644 index 00000000000..b777b3d68cc --- /dev/null +++ b/lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php @@ -0,0 +1,26 @@ +sqlStatements = $sql; + } + + public function execute(Connection $conn, array $params, array $types): Result + { + return $conn->executeQuery($this->getSqlStatements(), $params, $types, $this->queryCacheProfile); + } +} diff --git a/lib/Doctrine/ORM/Query/Exec/SingleSelectSqlFinalizer.php b/lib/Doctrine/ORM/Query/Exec/SingleSelectSqlFinalizer.php new file mode 100644 index 00000000000..1420bf39c1a --- /dev/null +++ b/lib/Doctrine/ORM/Query/Exec/SingleSelectSqlFinalizer.php @@ -0,0 +1,60 @@ +sql = $sql; + } + + /** + * @internal + * + * @psalm-internal Doctrine\ORM + * + * This method exists temporarily to support old SqlWalker interfaces. + */ + public function finalizeSql(Query $query): string + { + $platform = $query->getEntityManager()->getConnection()->getDatabasePlatform(); + + $sql = $platform->modifyLimitQuery($this->sql, $query->getMaxResults(), $query->getFirstResult()); + + $lockMode = $query->getHint(Query::HINT_LOCK_MODE) ?: LockMode::NONE; + + if ($lockMode !== LockMode::NONE && $lockMode !== LockMode::OPTIMISTIC && $lockMode !== LockMode::PESSIMISTIC_READ && $lockMode !== LockMode::PESSIMISTIC_WRITE) { + throw QueryException::invalidLockMode(); + } + + if ($lockMode === LockMode::PESSIMISTIC_READ) { + $sql .= ' ' . $platform->getReadLockSQL(); + } elseif ($lockMode === LockMode::PESSIMISTIC_WRITE) { + $sql .= ' ' . $platform->getWriteLockSQL(); + } + + return $sql; + } + + public function createExecutor(Query $query): AbstractSqlExecutor + { + return new FinalizedSelectExecutor($this->finalizeSql($query)); + } +}