-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a SingleSelectSqlFinalizer that can take care of adding offset/li…
…mit as well as locking mode statements to a given SQL query. Add a FinalizedSelectExecutor that executes given, finalized SQL statements.
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |