Skip to content

Commit

Permalink
Add an internal trait to allow cross-version compatibility for ORM SQ…
Browse files Browse the repository at this point in the history
…L walkers
  • Loading branch information
mbabker committed Jun 9, 2024
1 parent db204b9 commit 018761a
Show file tree
Hide file tree
Showing 6 changed files with 479 additions and 82 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ parameters:
excludePaths:
# Generates non-ignorable errors like " Parameter #1 $method (string) of method Gedmo\Tree\Entity\Repository\NestedTreeRepository::__call() is not contravariant with parameter #1 $method (mixed) of method Doctrine\ORM\EntityRepository::__call()."
- src/Tool/ORM/Repository/EntityRepositoryCompat.php
# Compat file for ORM 3, causes analysis errors with ORM 2
- src/Tool/ORM/Walker/orm-3.php
27 changes: 16 additions & 11 deletions src/SoftDeleteable/Query/TreeWalker/SoftDeleteableWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\Query\AST\DeleteClause;
use Doctrine\ORM\Query\AST\DeleteStatement;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\AST\UpdateStatement;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
use Doctrine\ORM\Query\Exec\SingleTableDeleteUpdateExecutor;
use Doctrine\ORM\Query\SqlWalker;
use Gedmo\Exception\RuntimeException;
use Gedmo\Exception\UnexpectedValueException;
use Gedmo\SoftDeleteable\Query\TreeWalker\Exec\MultiTableDeleteExecutor;
use Gedmo\SoftDeleteable\SoftDeleteableListener;
use Gedmo\Tool\ORM\Walker\SqlWalkerCompat;

/**
* This SqlWalker is needed when you need to use a DELETE DQL query.
Expand All @@ -35,6 +38,8 @@
*/
class SoftDeleteableWalker extends SqlWalker
{
use SqlWalkerCompat;

/**
* @var Connection
*
Expand Down Expand Up @@ -94,30 +99,30 @@ public function __construct($query, $parserResult, array $queryComponents)
}

/**
* @return AbstractSqlExecutor
* @param SelectStatement|UpdateStatement|DeleteStatement $statement
*
* @throws UnexpectedValueException when an unsupported AST statement is given
*/
public function getExecutor($AST)
protected function doGetExecutorWithCompat($statement): AbstractSqlExecutor
{
switch (true) {
case $AST instanceof DeleteStatement:
assert(class_exists($AST->deleteClause->abstractSchemaName));
case $statement instanceof DeleteStatement:
assert(class_exists($statement->deleteClause->abstractSchemaName));

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

return $primaryClass->isInheritanceTypeJoined()
? new MultiTableDeleteExecutor($AST, $this, $this->meta, $this->getConnection()->getDatabasePlatform(), $this->configuration)
: new SingleTableDeleteUpdateExecutor($AST, $this);
? 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');
}
}

/**
* Change a DELETE clause for an UPDATE clause
*
* @return string the SQL
* Changes a DELETE clause into an UPDATE clause for a soft-deleteable entity.
*/
public function walkDeleteClause(DeleteClause $deleteClause)
protected function doWalkDeleteClauseWithCompat(DeleteClause $deleteClause): string
{
$em = $this->getEntityManager();

Expand Down
20 changes: 20 additions & 0 deletions src/Tool/ORM/Walker/SqlWalkerCompat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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\SqlWalker;

if ((new \ReflectionClass(SqlWalker::class))->getMethod('getExecutor')->hasReturnType()) {
// ORM 3.x
require_once __DIR__.'/orm-3.php';

Check warning on line 16 in src/Tool/ORM/Walker/SqlWalkerCompat.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/SqlWalkerCompat.php#L16

Added line #L16 was not covered by tests
} else {
// ORM 2.x
require_once __DIR__.'/orm-2.php';
}
228 changes: 228 additions & 0 deletions src/Tool/ORM/Walker/orm-2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?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;
use Doctrine\ORM\Query\AST\DeleteClause;
use Doctrine\ORM\Query\AST\FromClause;
use Doctrine\ORM\Query\AST\GroupByClause;
use Doctrine\ORM\Query\AST\HavingClause;
use Doctrine\ORM\Query\AST\OrderByClause;
use Doctrine\ORM\Query\AST\SelectClause;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\AST\SimpleSelectClause;
use Doctrine\ORM\Query\AST\SubselectFromClause;
use Doctrine\ORM\Query\AST\WhereClause;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
use Doctrine\ORM\Query\SqlWalker;

/**
* Helper trait to address compatibility issues between ORM 2.x and 3.x.
*
* @mixin SqlWalker
*
* @internal
*/
trait SqlWalkerCompat
{
/**
* Gets an executor that can be used to execute the result of this walker.
*
* @param SelectStatement|AST\UpdateStatement|AST\DeleteStatement $statement
*
* @return AbstractSqlExecutor
*/
public function getExecutor($statement)
{
return $this->doGetExecutorWithCompat($statement);
}

/**
* Walks down a SelectStatement AST node, thereby generating the appropriate SQL.
*
* @param SelectStatement $selectStatement
*
* @return string
*/
public function walkSelectStatement($selectStatement)
{
return $this->doWalkSelectStatementWithCompat($selectStatement);
}

/**
* Walks down a SelectClause AST node, thereby generating the appropriate SQL.
*
* @param SelectClause $selectClause
*
* @return string
*/
public function walkSelectClause($selectClause)
{
return $this->doWalkSelectClauseWithCompat($selectClause);
}

/**
* Walks down a FromClause AST node, thereby generating the appropriate SQL.
*
* @param FromClause $fromClause
*
* @return string
*/
public function walkFromClause($fromClause)
{
return $this->doWalkFromClauseWithCompat($fromClause);
}

/**
* Walks down a OrderByClause AST node, thereby generating the appropriate SQL.
*
* @param OrderByClause $orderByClause
*
* @return string
*/
public function walkOrderByClause($orderByClause)
{
return $this->doWalkOrderByClauseWithCompat($orderByClause);
}

/**
* Walks down a HavingClause AST node, thereby generating the appropriate SQL.
*
* @param HavingClause $havingClause
*
* @return string
*/
public function walkHavingClause($havingClause)

Check warning on line 102 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L102

Added line #L102 was not covered by tests
{
return $this->doWalkHavingClauseWithCompat($havingClause);

Check warning on line 104 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L104

Added line #L104 was not covered by tests
}

/**
* Walks down a SubselectFromClause AST node, thereby generating the appropriate SQL.
*
* @param SubselectFromClause $subselectFromClause
*
* @return string
*/
public function walkSubselectFromClause($subselectFromClause)
{
return $this->doWalkSubselectFromClauseWithCompat($subselectFromClause);
}

/**
* Walks down a SimpleSelectClause AST node, thereby generating the appropriate SQL.
*
* @param SimpleSelectClause $simpleSelectClause
*
* @return string
*/
public function walkSimpleSelectClause($simpleSelectClause)
{
return $this->doWalkSimpleSelectClauseWithCompat($simpleSelectClause);
}

/**
* Walks down a GroupByClause AST node, thereby generating the appropriate SQL.
*
* @param GroupByClause $groupByClause
*
* @return string
*/
public function walkGroupByClause($groupByClause)

Check warning on line 138 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L138

Added line #L138 was not covered by tests
{
return $this->doWalkGroupByClauseWithCompat($groupByClause);

Check warning on line 140 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L140

Added line #L140 was not covered by tests
}

/**
* Walks down a DeleteClause AST node, thereby generating the appropriate SQL.
*
* @param DeleteClause $deleteClause
*
* @return string
*/
public function walkDeleteClause($deleteClause)
{
return $this->doWalkDeleteClauseWithCompat($deleteClause);
}

/**
* Walks down a WhereClause AST node, thereby generating the appropriate SQL.
*
* WhereClause or not, the appropriate discriminator sql is added.
*
* @param WhereClause|null $whereClause
*
* @return string
*/
public function walkWhereClause($whereClause)
{
return $this->doWalkWhereClauseWithCompat($whereClause);
}

/**
* Gets an executor that can be used to execute the result of this walker.
*
* @param SelectStatement|AST\UpdateStatement|AST\DeleteStatement $statement
*/
protected function doGetExecutorWithCompat($statement): AbstractSqlExecutor

Check warning on line 174 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L174

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

Check warning on line 176 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L176

Added line #L176 was not covered by tests
}

protected function doWalkSelectStatementWithCompat(SelectStatement $selectStatement): string

Check warning on line 179 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L179

Added line #L179 was not covered by tests
{
return parent::walkSelectStatement($selectStatement);

Check warning on line 181 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L181

Added line #L181 was not covered by tests
}

protected function doWalkSelectClauseWithCompat(SelectClause $selectClause): string

Check warning on line 184 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L184

Added line #L184 was not covered by tests
{
return parent::walkSelectClause($selectClause);

Check warning on line 186 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L186

Added line #L186 was not covered by tests
}

protected function doWalkFromClauseWithCompat(FromClause $fromClause): string
{
return parent::walkFromClause($fromClause);
}

protected function doWalkOrderByClauseWithCompat(OrderByClause $orderByClause): string

Check warning on line 194 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L194

Added line #L194 was not covered by tests
{
return parent::walkOrderByClause($orderByClause);

Check warning on line 196 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L196

Added line #L196 was not covered by tests
}

protected function doWalkHavingClauseWithCompat(HavingClause $havingClause): string

Check warning on line 199 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L199

Added line #L199 was not covered by tests
{
return parent::walkHavingClause($havingClause);

Check warning on line 201 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L201

Added line #L201 was not covered by tests
}

protected function doWalkSubselectFromClauseWithCompat(SubselectFromClause $subselectFromClause): string

Check warning on line 204 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L204

Added line #L204 was not covered by tests
{
return parent::walkSubselectFromClause($subselectFromClause);

Check warning on line 206 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L206

Added line #L206 was not covered by tests
}

protected function doWalkSimpleSelectClauseWithCompat(SimpleSelectClause $simpleSelectClause): string

Check warning on line 209 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L209

Added line #L209 was not covered by tests
{
return parent::walkSimpleSelectClause($simpleSelectClause);

Check warning on line 211 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L211

Added line #L211 was not covered by tests
}

protected function doWalkGroupByClauseWithCompat(GroupByClause $groupByClause): string

Check warning on line 214 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L214

Added line #L214 was not covered by tests
{
return parent::walkGroupByClause($groupByClause);

Check warning on line 216 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L216

Added line #L216 was not covered by tests
}

protected function doWalkDeleteClauseWithCompat(DeleteClause $deleteClause): string

Check warning on line 219 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L219

Added line #L219 was not covered by tests
{
return parent::walkDeleteClause($deleteClause);

Check warning on line 221 in src/Tool/ORM/Walker/orm-2.php

View check run for this annotation

Codecov / codecov/patch

src/Tool/ORM/Walker/orm-2.php#L221

Added line #L221 was not covered by tests
}

protected function doWalkWhereClauseWithCompat(?WhereClause $whereClause): string
{
return parent::walkWhereClause($whereClause);
}
}
Loading

0 comments on commit 018761a

Please sign in to comment.