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 2176223
Show file tree
Hide file tree
Showing 6 changed files with 457 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 <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\SqlWalker;

if ((new \ReflectionClass(SqlWalker::class))->getMethod('getExecutor')->hasReturnType()) {
// ORM 3.x
require_once __DIR__.'/orm-3.php';
} else {
// ORM 2.x
require_once __DIR__.'/orm-2.php';
}
218 changes: 218 additions & 0 deletions src/Tool/ORM/Walker/orm-2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?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;
use Doctrine\ORM\Query\Exec;
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 AST\SelectStatement|AST\UpdateStatement|AST\DeleteStatement $statement
*
* @return Exec\AbstractSqlExecutor
*/
public function getExecutor($statement)
{
return $this->doGetExecutorWithCompat($statement);
}

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

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

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

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

/**
* Walks down a HavingClause AST node, thereby generating the appropriate SQL.
*
* @param AST\HavingClause $havingClause
*
* @return string
*/
public function walkHavingClause($havingClause)
{
return $this->doWalkHavingClauseWithCompat($havingClause);
}

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

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

/**
* Walks down a GroupByClause AST node, thereby generating the appropriate SQL.
*
* @param AST\GroupByClause $groupByClause
*
* @return string
*/
public function walkGroupByClause($groupByClause)
{
return $this->doWalkGroupByClauseWithCompat($groupByClause);
}

/**
* Walks down a DeleteClause AST node, thereby generating the appropriate SQL.
*
* @param AST\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 AST\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 AST\SelectStatement|AST\UpdateStatement|AST\DeleteStatement $statement
*/
protected function doGetExecutorWithCompat($statement): Exec\AbstractSqlExecutor
{
return parent::getExecutor($statement);
}

protected function doWalkSelectStatementWithCompat(AST\SelectStatement $selectStatement): string
{
return parent::walkSelectStatement($selectStatement);
}

protected function doWalkSelectClauseWithCompat(AST\SelectClause $selectClause): string
{
return parent::walkSelectClause($selectClause);
}

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

protected function doWalkOrderByClauseWithCompat(AST\OrderByClause $orderByClause): string
{
return parent::walkOrderByClause($orderByClause);
}

protected function doWalkHavingClauseWithCompat(AST\HavingClause $havingClause): string
{
return parent::walkHavingClause($havingClause);
}

protected function doWalkSubselectFromClauseWithCompat(AST\SubselectFromClause $subselectFromClause): string
{
return parent::walkSubselectFromClause($subselectFromClause);
}

protected function doWalkSimpleSelectClauseWithCompat(AST\SimpleSelectClause $simpleSelectClause): string
{
return parent::walkSimpleSelectClause($simpleSelectClause);
}

protected function doWalkGroupByClauseWithCompat(AST\GroupByClause $groupByClause): string
{
return parent::walkGroupByClause($groupByClause);
}

protected function doWalkDeleteClauseWithCompat(AST\DeleteClause $deleteClause): string
{
return parent::walkDeleteClause($deleteClause);
}

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

0 comments on commit 2176223

Please sign in to comment.