Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for new SqlOutputWalker class in the ORM #2895

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ parameters:
count: 1
path: src/Timestampable/Mapping/Driver/Yaml.php

-
message: "#^Call to an undefined static method Doctrine\\\\ORM\\\\Query\\\\SqlWalker\\:\\:getFinalizer\\(\\)\\.$#"
count: 2
path: src/Tool/ORM/Walker/CompatSqlOutputWalker.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
count: 1
Expand Down
46 changes: 35 additions & 11 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 @@ -99,21 +101,43 @@
* @param SelectStatement|UpdateStatement|DeleteStatement $statement
*
* @throws UnexpectedValueException when an unsupported AST statement is given
*
* @phpstan-assert DeleteStatement $statement
*/
protected function doGetExecutorWithCompat($statement): AbstractSqlExecutor
{
switch (true) {
case $statement instanceof DeleteStatement:
assert(class_exists($statement->deleteClause->abstractSchemaName));
if (!$statement instanceof DeleteStatement) {
throw new UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L110 was not covered by tests
}

$primaryClass = $this->getEntityManager()->getClassMetadata($statement->deleteClause->abstractSchemaName);
return $this->createDeleteStatementExecutor($statement);
}

return $primaryClass->isInheritanceTypeJoined()
? new MultiTableDeleteExecutor($statement, $this, $this->meta, $this->getConnection()->getDatabasePlatform(), $this->configuration)
: new SingleTableDeleteUpdateExecutor($statement, $this);
default:
throw new UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');
/**
* @param DeleteStatement|UpdateStatement|SelectStatement $AST
*
* @throws UnexpectedValueException when an unsupported AST statement is given
mbabker marked this conversation as resolved.
Show resolved Hide resolved
*
* @phpstan-assert DeleteStatement $AST
*/
protected function doGetFinalizerWithCompat($AST): SqlFinalizer
{
if (!$AST instanceof DeleteStatement) {
throw new UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L126 was not covered by tests
}

return new PreparedExecutorFinalizer($this->createDeleteStatementExecutor($AST));
}

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);
}

/**
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 <gediminas.morkevicius@gmail.com> 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 SqlOutputWalker
{
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;
}
}
}
42 changes: 42 additions & 0 deletions src/Tool/ORM/Walker/CompatSqlOutputWalkerForOrm2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> 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 $AST
*/
public function getFinalizer($AST): SqlFinalizer
{
return $this->doGetFinalizerWithCompat($AST);
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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
}
}
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 <gediminas.morkevicius@gmail.com> 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 $AST
*/
protected function doGetFinalizerWithCompat($AST): 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($AST);

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 @@
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
Loading