Reduce complexity of LimitSubqueryWalker#walkSelectStatement()
Ensuring that the code follows our code standards.
This commit is contained in:
parent
aacea65519
commit
441c5d138c
@ -90,29 +90,33 @@ class LimitSubqueryWalker extends TreeWalkerAdapter
|
||||
|
||||
$pathExpression->type = PathExpression::TYPE_STATE_FIELD;
|
||||
|
||||
$AST->selectClause->selectExpressions = array(new SelectExpression($pathExpression, '_dctrn_id'));
|
||||
$AST->selectClause->selectExpressions = [new SelectExpression($pathExpression, '_dctrn_id')];
|
||||
$AST->selectClause->isDistinct = true;
|
||||
|
||||
if (isset($AST->orderByClause)) {
|
||||
foreach ($AST->orderByClause->orderByItems as $item) {
|
||||
if ( ! $item->expression instanceof PathExpression) {
|
||||
if(is_string($item->expression) && isset($queryComponents[$item->expression])) {
|
||||
$qComp = $queryComponents[$item->expression];
|
||||
if (isset($qComp['resultVariable'])) {
|
||||
$AST->selectClause->selectExpressions[] = new SelectExpression($qComp['resultVariable'], $item->expression);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
$AST->selectClause->selectExpressions[] = new SelectExpression(
|
||||
$this->createSelectExpressionItem($item->expression),
|
||||
'_dctrn_ord' . $this->_aliasCounter++
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
if ( ! isset($AST->orderByClause)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$AST->selectClause->isDistinct = true;
|
||||
foreach ($AST->orderByClause->orderByItems as $item) {
|
||||
if ($item->expression instanceof PathExpression) {
|
||||
$AST->selectClause->selectExpressions[] = new SelectExpression(
|
||||
$this->createSelectExpressionItem($item->expression), '_dctrn_ord' . $this->_aliasCounter++
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_string($item->expression) && isset($queryComponents[$item->expression])) {
|
||||
$qComp = $queryComponents[$item->expression];
|
||||
|
||||
if (isset($qComp['resultVariable'])) {
|
||||
$AST->selectClause->selectExpressions[] = new SelectExpression(
|
||||
$qComp['resultVariable'],
|
||||
$item->expression
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,21 +38,20 @@ class LimitSubqueryWalkerTest extends PaginationTestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testLimitSubqueryWithSortFunction()
|
||||
public function testLimitSubqueryWithSortFunction() : void
|
||||
{
|
||||
$dql = 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c GROUP BY p.id ORDER BY COUNT(c.id)';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
$limitQuery = clone $query;
|
||||
$dql = 'SELECT p FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c GROUP BY p.id ORDER BY COUNT(c.id)';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
|
||||
$limitQuery = clone $query;
|
||||
$limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [LimitSubqueryWalker::class]);
|
||||
|
||||
$this->assertEquals(
|
||||
"SELECT DISTINCT m0_.id AS id_0 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id GROUP BY m0_.id ORDER BY COUNT(c1_.id) ASC",
|
||||
self::assertSame(
|
||||
'SELECT DISTINCT m0_.id AS id_0 FROM MyBlogPost m0_ INNER JOIN Category c1_ ON m0_.category_id = c1_.id GROUP BY m0_.id ORDER BY COUNT(c1_.id) ASC',
|
||||
$limitQuery->getSQL()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testCountQuery_MixedResultsWithName()
|
||||
{
|
||||
$dql = 'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a';
|
||||
@ -67,30 +66,30 @@ class LimitSubqueryWalkerTest extends PaginationTestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testAggQuery_MixedResultsWithNameAndSort()
|
||||
public function testAggQuery_MixedResultsWithNameAndSort() : void
|
||||
{
|
||||
$dql = 'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a ORDER BY foo DESC';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
$limitQuery = clone $query;
|
||||
$dql = 'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a ORDER BY foo DESC';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
|
||||
$limitQuery = clone $query;
|
||||
$limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [LimitSubqueryWalker::class]);
|
||||
|
||||
$this->assertEquals(
|
||||
"SELECT DISTINCT a0_.id AS id_0, sum(a0_.name) AS sclr_1 FROM Author a0_ ORDER BY sclr_1 DESC",
|
||||
self::assertSame(
|
||||
'SELECT DISTINCT a0_.id AS id_0, sum(a0_.name) AS sclr_1 FROM Author a0_ ORDER BY sclr_1 DESC',
|
||||
$limitQuery->getSQL()
|
||||
);
|
||||
}
|
||||
|
||||
public function testAggQuery_MultipleMixedResultsWithSort()
|
||||
public function testAggQuery_MultipleMixedResultsWithSort() : void
|
||||
{
|
||||
$dql = 'SELECT a, sum(a.name) as foo, (SELECT count(subA.id) FROM Doctrine\Tests\ORM\Tools\Pagination\Author subA WHERE subA.id = a.id ) as bar FROM Doctrine\Tests\ORM\Tools\Pagination\Author a ORDER BY foo DESC, bar ASC';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
$limitQuery = clone $query;
|
||||
$dql = 'SELECT a, sum(a.name) as foo, (SELECT count(subA.id) FROM Doctrine\Tests\ORM\Tools\Pagination\Author subA WHERE subA.id = a.id ) as bar FROM Doctrine\Tests\ORM\Tools\Pagination\Author a ORDER BY foo DESC, bar ASC';
|
||||
$query = $this->entityManager->createQuery($dql);
|
||||
|
||||
$limitQuery = clone $query;
|
||||
$limitQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [LimitSubqueryWalker::class]);
|
||||
|
||||
$this->assertEquals(
|
||||
"SELECT DISTINCT a0_.id AS id_0, sum(a0_.name) AS sclr_1, (SELECT count(a1_.id) AS sclr_3 FROM Author a1_ WHERE a1_.id = a0_.id) AS sclr_2 FROM Author a0_ ORDER BY sclr_1 DESC, sclr_2 ASC",
|
||||
self::assertSame(
|
||||
'SELECT DISTINCT a0_.id AS id_0, sum(a0_.name) AS sclr_1, (SELECT count(a1_.id) AS sclr_3 FROM Author a1_ WHERE a1_.id = a0_.id) AS sclr_2 FROM Author a0_ ORDER BY sclr_1 DESC, sclr_2 ASC',
|
||||
$limitQuery->getSQL()
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user