AbstractSqlExecutor.php
9 months ago
FinalizedSelectExecutor.php
9 months ago
MultiTableDeleteExecutor.php
9 months ago
MultiTableUpdateExecutor.php
9 months ago
PreparedExecutorFinalizer.php
9 months ago
SingleSelectExecutor.php
9 months ago
SingleSelectSqlFinalizer.php
9 months ago
SingleTableDeleteUpdateExecutor.php
9 months ago
SqlFinalizer.php
9 months ago
index.php
9 months ago
MultiTableDeleteExecutor.php
85 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Query\Exec; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\DBAL\Connection; |
| 6 | use MailPoetVendor\Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; |
| 7 | use MailPoetVendor\Doctrine\DBAL\Types\Type; |
| 8 | use MailPoetVendor\Doctrine\ORM\Query\AST; |
| 9 | use MailPoetVendor\Doctrine\ORM\Query\AST\DeleteStatement; |
| 10 | use MailPoetVendor\Doctrine\ORM\Query\SqlWalker; |
| 11 | use MailPoetVendor\Doctrine\ORM\Utility\PersisterHelper; |
| 12 | use Throwable; |
| 13 | use function array_merge; |
| 14 | use function array_reverse; |
| 15 | use function implode; |
| 16 | class MultiTableDeleteExecutor extends AbstractSqlExecutor |
| 17 | { |
| 18 | private $createTempTableSql; |
| 19 | private $dropTempTableSql; |
| 20 | private $insertSql; |
| 21 | public function __construct(AST\Node $AST, $sqlWalker) |
| 22 | { |
| 23 | parent::__construct(); |
| 24 | $em = $sqlWalker->getEntityManager(); |
| 25 | $conn = $em->getConnection(); |
| 26 | $platform = $conn->getDatabasePlatform(); |
| 27 | $quoteStrategy = $em->getConfiguration()->getQuoteStrategy(); |
| 28 | if ($conn instanceof PrimaryReadReplicaConnection) { |
| 29 | $conn->ensureConnectedToPrimary(); |
| 30 | } |
| 31 | $primaryClass = $em->getClassMetadata($AST->deleteClause->abstractSchemaName); |
| 32 | $primaryDqlAlias = $AST->deleteClause->aliasIdentificationVariable; |
| 33 | $rootClass = $em->getClassMetadata($primaryClass->rootEntityName); |
| 34 | $tempTable = $platform->getTemporaryTableName($rootClass->getTemporaryIdTableName()); |
| 35 | $idColumnNames = $rootClass->getIdentifierColumnNames(); |
| 36 | $idColumnList = implode(', ', $idColumnNames); |
| 37 | // 1. Create an INSERT INTO temptable ... SELECT identifiers WHERE $AST->getWhereClause() |
| 38 | $sqlWalker->setSQLTableAlias($primaryClass->getTableName(), 't0', $primaryDqlAlias); |
| 39 | $this->insertSql = 'INSERT INTO ' . $tempTable . ' (' . $idColumnList . ')' . ' SELECT t0.' . implode(', t0.', $idColumnNames); |
| 40 | $rangeDecl = new AST\RangeVariableDeclaration($primaryClass->name, $primaryDqlAlias); |
| 41 | $fromClause = new AST\FromClause([new AST\IdentificationVariableDeclaration($rangeDecl, null, [])]); |
| 42 | $this->insertSql .= $sqlWalker->walkFromClause($fromClause); |
| 43 | // Append WHERE clause, if there is one. |
| 44 | if ($AST->whereClause) { |
| 45 | $this->insertSql .= $sqlWalker->walkWhereClause($AST->whereClause); |
| 46 | } |
| 47 | // 2. Create ID subselect statement used in DELETE ... WHERE ... IN (subselect) |
| 48 | $idSubselect = 'SELECT ' . $idColumnList . ' FROM ' . $tempTable; |
| 49 | // 3. Create and store DELETE statements |
| 50 | $classNames = array_merge($primaryClass->parentClasses, [$primaryClass->name], $primaryClass->subClasses); |
| 51 | foreach (array_reverse($classNames) as $className) { |
| 52 | $tableName = $quoteStrategy->getTableName($em->getClassMetadata($className), $platform); |
| 53 | $this->sqlStatements[] = 'DELETE FROM ' . $tableName . ' WHERE (' . $idColumnList . ') IN (' . $idSubselect . ')'; |
| 54 | } |
| 55 | // 4. Store DDL for temporary identifier table. |
| 56 | $columnDefinitions = []; |
| 57 | foreach ($idColumnNames as $idColumnName) { |
| 58 | $columnDefinitions[$idColumnName] = ['notnull' => \true, 'type' => Type::getType(PersisterHelper::getTypeOfColumn($idColumnName, $rootClass, $em))]; |
| 59 | } |
| 60 | $this->createTempTableSql = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' (' . $platform->getColumnDeclarationListSQL($columnDefinitions) . ', PRIMARY KEY(' . implode(',', $idColumnNames) . '))'; |
| 61 | $this->dropTempTableSql = $platform->getDropTemporaryTableSQL($tempTable); |
| 62 | } |
| 63 | public function execute(Connection $conn, array $params, array $types) |
| 64 | { |
| 65 | // Create temporary id table |
| 66 | $conn->executeStatement($this->createTempTableSql); |
| 67 | try { |
| 68 | // Insert identifiers |
| 69 | $numDeleted = $conn->executeStatement($this->insertSql, $params, $types); |
| 70 | // Execute DELETE statements |
| 71 | foreach ($this->sqlStatements as $sql) { |
| 72 | $conn->executeStatement($sql); |
| 73 | } |
| 74 | } catch (Throwable $exception) { |
| 75 | // FAILURE! Drop temporary table to avoid possible collisions |
| 76 | $conn->executeStatement($this->dropTempTableSql); |
| 77 | // Re-throw exception |
| 78 | throw $exception; |
| 79 | } |
| 80 | // Drop temporary table |
| 81 | $conn->executeStatement($this->dropTempTableSql); |
| 82 | return $numDeleted; |
| 83 | } |
| 84 | } |
| 85 |