Skip to content

Commit

Permalink
Add a SingleSelectSqlFinalizer that can take care of adding offset/li…
Browse files Browse the repository at this point in the history
…mit as well as locking mode statements to a given SQL query.

Add a FinalizedSelectExecutor that executes given, finalized SQL statements.
  • Loading branch information
mpdude committed Jan 28, 2024
1 parent 65c5a72 commit f2b0a37
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Query\Exec;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;

/**
* SQL executor for a given, final, single SELECT SQL query
*/
class FinalizedSelectExecutor extends AbstractSqlExecutor
{
public function __construct(string $sql)
{
parent::__construct();

$this->sqlStatements = $sql;
}

public function execute(Connection $conn, array $params, array $types): Result
{
return $conn->executeQuery($this->getSqlStatements(), $params, $types, $this->queryCacheProfile);
}
}
60 changes: 60 additions & 0 deletions lib/Doctrine/ORM/Query/Exec/SingleSelectSqlFinalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Query\Exec;

use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\QueryException;

/**
* SingleSelectSqlFinalizer finalizes a given SQL query by applying
* the query's firstResult/maxResult values as well as extra read lock/write lock
* statements, both through the platform-specific methods.
*
* The resulting, "finalized" SQL is passed to a FinalizedSelectExecutor.
*/
class SingleSelectSqlFinalizer implements SqlFinalizer
{
/** @var string */
private $sql;

public function __construct(string $sql)
{
$this->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));
}
}

0 comments on commit f2b0a37

Please sign in to comment.