-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
…mit as well as locking mode statements to a given SQL query. Add a FinalizedSelectExecutor that executes given, finalized SQL statements.
- Loading branch information
There are no files selected for viewing
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 | ||
Check failure on line 13 in lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php GitHub Actions / Static Analysis with PsalmPropertyNotSetInConstructor
Check failure on line 13 in lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php GitHub Actions / Static Analysis with PsalmPropertyNotSetInConstructor
|
||
{ | ||
public function __construct(string $sql) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->sqlStatements = $sql; | ||
} | ||
|
||
public function execute(Connection $conn, array $params, array $types): Result | ||
Check failure on line 22 in lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php GitHub Actions / coding-standards / Coding Standards (8.2)
Check failure on line 22 in lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php GitHub Actions / coding-standards / Coding Standards (8.2)
|
||
{ | ||
return $conn->executeQuery($this->getSqlStatements(), $params, $types, $this->queryCacheProfile); | ||
Check failure on line 24 in lib/Doctrine/ORM/Query/Exec/FinalizedSelectExecutor.php GitHub Actions / Static Analysis with PsalmPossiblyInvalidArgument
|
||
} | ||
} |
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(); | ||
Check failure on line 48 in lib/Doctrine/ORM/Query/Exec/SingleSelectSqlFinalizer.php GitHub Actions / Static Analysis with PsalmDeprecatedMethod
|
||
} elseif ($lockMode === LockMode::PESSIMISTIC_WRITE) { | ||
$sql .= ' ' . $platform->getWriteLockSQL(); | ||
} | ||
|
||
return $sql; | ||
} | ||
|
||
public function createExecutor(Query $query): AbstractSqlExecutor | ||
{ | ||
return new FinalizedSelectExecutor($this->finalizeSql($query)); | ||
} | ||
} |