Skip to content

Commit

Permalink
Add support for new SqlOutputWalker class in the ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Nov 12, 2024
1 parent 22418b1 commit 0a88bfa
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 10 deletions.
38 changes: 30 additions & 8 deletions src/SoftDeleteable/Query/TreeWalker/SoftDeleteableWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\AST\UpdateStatement;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
use Doctrine\ORM\Query\Exec\PreparedExecutorFinalizer;
use Doctrine\ORM\Query\Exec\SingleTableDeleteUpdateExecutor;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\Exec\SqlFinalizer;
use Gedmo\Exception\RuntimeException;
use Gedmo\Exception\UnexpectedValueException;
use Gedmo\SoftDeleteable\Query\TreeWalker\Exec\MultiTableDeleteExecutor;
use Gedmo\SoftDeleteable\SoftDeleteableListener;
use Gedmo\Tool\ORM\Walker\CompatSqlOutputWalker;
use Gedmo\Tool\ORM\Walker\SqlWalkerCompat;

/**
Expand All @@ -36,7 +38,7 @@
*
* @final since gedmo/doctrine-extensions 3.11
*/
class SoftDeleteableWalker extends SqlWalker
class SoftDeleteableWalker extends CompatSqlOutputWalker
{
use SqlWalkerCompat;

Expand Down Expand Up @@ -104,18 +106,38 @@ protected function doGetExecutorWithCompat($statement): AbstractSqlExecutor
{
switch (true) {
case $statement instanceof DeleteStatement:
assert(class_exists($statement->deleteClause->abstractSchemaName));

$primaryClass = $this->getEntityManager()->getClassMetadata($statement->deleteClause->abstractSchemaName);
return $this->createDeleteStatementExecutor($statement);
default:
throw new UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');

Check warning on line 111 in src/SoftDeleteable/Query/TreeWalker/SoftDeleteableWalker.php

View check run for this annotation

Codecov / codecov/patch

src/SoftDeleteable/Query/TreeWalker/SoftDeleteableWalker.php#L111

Added line #L111 was not covered by tests
}
}

return $primaryClass->isInheritanceTypeJoined()
? new MultiTableDeleteExecutor($statement, $this, $this->meta, $this->getConnection()->getDatabasePlatform(), $this->configuration)
: new SingleTableDeleteUpdateExecutor($statement, $this);
/**
* @param DeleteStatement|UpdateStatement|SelectStatement $AST
*
* @throws UnexpectedValueException when an unsupported AST statement is given
*/
protected function doGetFinalizerWithCompat($AST): SqlFinalizer
{
switch (true) {
case $AST instanceof DeleteStatement:
return new PreparedExecutorFinalizer($this->createDeleteStatementExecutor($AST));
default:
throw new UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');
}
}

protected function createDeleteStatementExecutor(DeleteStatement $AST): AbstractSqlExecutor
{
assert(class_exists($AST->deleteClause->abstractSchemaName));

$primaryClass = $this->getEntityManager()->getClassMetadata($AST->deleteClause->abstractSchemaName);

return $primaryClass->isInheritanceTypeJoined()
? new MultiTableDeleteExecutor($AST, $this, $this->meta, $this->getConnection()->getDatabasePlatform(), $this->configuration)
: new SingleTableDeleteUpdateExecutor($AST, $this);
}

/**
* Changes a DELETE clause into an UPDATE clause for a soft-deleteable entity.
*/
Expand Down
59 changes: 59 additions & 0 deletions src/Tool/ORM/Walker/CompatSqlOutputWalker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tool\ORM\Walker;

use Doctrine\ORM\Query\SqlOutputWalker;
use Doctrine\ORM\Query\SqlWalker;

if (class_exists(SqlOutputWalker::class)) {
if ((new \ReflectionClass(SqlWalker::class))->getMethod('getExecutor')->hasReturnType()) {
/**
* Helper trait to address compatibility issues between ORM 2.x and 3.x.
*
* @internal
*/
abstract class CompatSqlOutputWalker extends SqlOutputWalker
{
use CompatSqlOutputWalkerForOrm3;
}
} else {
/**
* Helper trait to address compatibility issues between ORM 2.x and 3.x.
*
* @internal
*/
abstract class CompatSqlOutputWalker extends SqlWalker
{
use CompatSqlOutputWalkerForOrm2;
}
}
} else {
if ((new \ReflectionClass(SqlWalker::class))->getMethod('getExecutor')->hasReturnType()) {
/**
* Helper trait to address compatibility issues between ORM 2.x and 3.x.
*
* @internal
*/
abstract class CompatSqlOutputWalker extends SqlWalker
{
use CompatSqlOutputWalkerForOrm3;
}
} else {
/**
* Helper trait to address compatibility issues between ORM 2.x and 3.x.
*
* @internal
*/
abstract class CompatSqlOutputWalker extends SqlWalker
{
use CompatSqlOutputWalkerForOrm2;
}
}
}
44 changes: 44 additions & 0 deletions src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tool\ORM\Walker;

use Doctrine\ORM\Query\AST\DeleteStatement;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\AST\UpdateStatement;
use Doctrine\ORM\Query\Exec\SqlFinalizer;
use Doctrine\ORM\Query\SqlOutputWalker;

/**
* Helper trait to address compatibility issues between ORM 2.x and 3.x.
*
* @mixin SqlOutputWalker
*
* @internal
*/
trait CompatSqlOutputWalkerForOrm2
{
/**
* @param DeleteStatement|UpdateStatement|SelectStatement $statement
*
* @return SqlFinalizer
*/
public function getFinalizer($AST)

Check warning on line 32 in src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php#L32

Added line #L32 was not covered by tests
{
return $this->doGetFinalizerWithCompat($AST);

Check warning on line 34 in src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php#L34

Added line #L34 was not covered by tests
}

/**
* @param DeleteStatement|UpdateStatement|SelectStatement $statement
*/
protected function doGetFinalizerWithCompat($statement): SqlFinalizer

Check warning on line 40 in src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php#L40

Added line #L40 was not covered by tests
{
return parent::getFinalizer($statement);

Check warning on line 42 in src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php#L42

Added line #L42 was not covered by tests
}
}
39 changes: 39 additions & 0 deletions src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tool\ORM\Walker;

use Doctrine\ORM\Query\AST\DeleteStatement;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\AST\UpdateStatement;
use Doctrine\ORM\Query\Exec\SqlFinalizer;
use Doctrine\ORM\Query\SqlOutputWalker;

/**
* Helper trait to address compatibility issues between ORM 2.x and 3.x.
*
* @mixin SqlOutputWalker
*
* @internal
*/
trait CompatSqlOutputWalkerForOrm3
{
public function getFinalizer(DeleteStatement|UpdateStatement|SelectStatement $AST): SqlFinalizer
{
return $this->doGetFinalizerWithCompat($AST);
}

/**
* @param DeleteStatement|UpdateStatement|SelectStatement $statement
*/
protected function doGetFinalizerWithCompat($statement): SqlFinalizer

Check warning on line 35 in src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm3.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm3.php#L35

Added line #L35 was not covered by tests
{
return parent::getFinalizer($statement);

Check warning on line 37 in src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm3.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm3.php#L37

Added line #L37 was not covered by tests
}
}
21 changes: 19 additions & 2 deletions src/Translatable/Query/TreeWalker/TranslationWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
use Doctrine\ORM\Query\AST\WhereClause;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
use Doctrine\ORM\Query\Exec\SingleSelectExecutor;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\Exec\SingleSelectSqlFinalizer;
use Doctrine\ORM\Query\Exec\SqlFinalizer;
use Gedmo\Exception\RuntimeException;
use Gedmo\Tool\ORM\Walker\CompatSqlOutputWalker;
use Gedmo\Tool\ORM\Walker\SqlWalkerCompat;
use Gedmo\Translatable\Hydrator\ORM\ObjectHydrator;
use Gedmo\Translatable\Hydrator\ORM\SimpleObjectHydrator;
Expand All @@ -54,7 +56,7 @@
*
* @final since gedmo/doctrine-extensions 3.11
*/
class TranslationWalker extends SqlWalker
class TranslationWalker extends CompatSqlOutputWalker
{
use SqlWalkerCompat;

Expand Down Expand Up @@ -141,6 +143,21 @@ protected function doGetExecutorWithCompat($statement): AbstractSqlExecutor
return new SingleSelectExecutor($statement, $this);
}

/**
* @param DeleteStatement|UpdateStatement|SelectStatement $AST
*/
protected function doGetFinalizerWithCompat($AST): SqlFinalizer
{
// If it's not a Select, the TreeWalker ought to skip it, and just return the parent.
// @see https://github.com/doctrine-extensions/DoctrineExtensions/issues/2013
if (!$AST instanceof SelectStatement) {
return parent::getFinalizer($AST);

Check warning on line 154 in src/Translatable/Query/TreeWalker/TranslationWalker.php

View check run for this annotation

Codecov / codecov/patch

src/Translatable/Query/TreeWalker/TranslationWalker.php#L154

Added line #L154 was not covered by tests
}
$this->prepareTranslatedComponents();

return new SingleSelectSqlFinalizer($this->walkSelectStatement($AST));
}

protected function doWalkSelectStatementWithCompat(SelectStatement $selectStatement): string
{
$result = parent::walkSelectStatement($selectStatement);
Expand Down

0 comments on commit 0a88bfa

Please sign in to comment.