generate nested join sql for CTIs, closes #6464
This commit is contained in:
parent
c73ec2aa76
commit
bf1188127e
@ -866,10 +866,11 @@ class SqlWalker implements TreeWalker
|
|||||||
* Walks down a RangeVariableDeclaration AST node, thereby generating the appropriate SQL.
|
* Walks down a RangeVariableDeclaration AST node, thereby generating the appropriate SQL.
|
||||||
*
|
*
|
||||||
* @param AST\RangeVariableDeclaration $rangeVariableDeclaration
|
* @param AST\RangeVariableDeclaration $rangeVariableDeclaration
|
||||||
|
* @param bool $buildNestedJoins
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function walkRangeVariableDeclaration($rangeVariableDeclaration)
|
public function walkRangeVariableDeclaration($rangeVariableDeclaration, $buildNestedJoins = false)
|
||||||
{
|
{
|
||||||
$class = $this->em->getClassMetadata($rangeVariableDeclaration->abstractSchemaName);
|
$class = $this->em->getClassMetadata($rangeVariableDeclaration->abstractSchemaName);
|
||||||
$dqlAlias = $rangeVariableDeclaration->aliasIdentificationVariable;
|
$dqlAlias = $rangeVariableDeclaration->aliasIdentificationVariable;
|
||||||
@ -885,8 +886,12 @@ class SqlWalker implements TreeWalker
|
|||||||
);
|
);
|
||||||
|
|
||||||
if ($class->isInheritanceTypeJoined()) {
|
if ($class->isInheritanceTypeJoined()) {
|
||||||
|
if ($buildNestedJoins) {
|
||||||
|
$sql = '(' . $sql . $this->_generateClassTableInheritanceJoins($class, $dqlAlias) . ')';
|
||||||
|
} else {
|
||||||
$sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
|
$sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $sql;
|
return $sql;
|
||||||
}
|
}
|
||||||
@ -1126,11 +1131,11 @@ class SqlWalker implements TreeWalker
|
|||||||
$dqlAlias = $joinDeclaration->aliasIdentificationVariable;
|
$dqlAlias = $joinDeclaration->aliasIdentificationVariable;
|
||||||
$tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
|
$tableAlias = $this->getSQLTableAlias($class->table['name'], $dqlAlias);
|
||||||
$condition = '(' . $this->walkConditionalExpression($join->conditionalExpression) . ')';
|
$condition = '(' . $this->walkConditionalExpression($join->conditionalExpression) . ')';
|
||||||
$condExprConjunction = ($class->isInheritanceTypeJoined() && $joinType != AST\Join::JOIN_TYPE_LEFT && $joinType != AST\Join::JOIN_TYPE_LEFTOUTER)
|
$condExprConjunction = ($class->isInheritanceTypeJoined() && $joinType != AST\Join::JOIN_TYPE_LEFT && $joinType != AST\Join::JOIN_TYPE_LEFTOUTER && empty($conditions))
|
||||||
? ' AND '
|
? ' AND '
|
||||||
: ' ON ';
|
: ' ON ';
|
||||||
|
|
||||||
$sql .= $this->walkRangeVariableDeclaration($joinDeclaration);
|
$sql .= $this->walkRangeVariableDeclaration($joinDeclaration, !empty($conditions));
|
||||||
|
|
||||||
$conditions = array($condition);
|
$conditions = array($condition);
|
||||||
|
|
||||||
|
@ -153,12 +153,12 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
|
|||||||
{
|
{
|
||||||
$this->assertSqlGeneration(
|
$this->assertSqlGeneration(
|
||||||
'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e JOIN Doctrine\Tests\Models\Company\CompanyManager m WITH e.id = m.id',
|
'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e JOIN Doctrine\Tests\Models\Company\CompanyManager m WITH e.id = m.id',
|
||||||
'SELECT c0_.id AS id_0, c0_.name AS name_1, c1_.salary AS salary_2, c1_.department AS department_3, c1_.startDate AS startDate_4, c0_.discr AS discr_5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id INNER JOIN company_managers c2_ INNER JOIN company_employees c4_ ON c2_.id = c4_.id INNER JOIN company_persons c3_ ON c2_.id = c3_.id AND (c0_.id = c3_.id)'
|
'SELECT c0_.id AS id_0, c0_.name AS name_1, c1_.salary AS salary_2, c1_.department AS department_3, c1_.startDate AS startDate_4, c0_.discr AS discr_5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id INNER JOIN (company_managers c2_ INNER JOIN company_employees c4_ ON c2_.id = c4_.id INNER JOIN company_persons c3_ ON c2_.id = c3_.id) ON (c0_.id = c3_.id)'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertSqlGeneration(
|
$this->assertSqlGeneration(
|
||||||
'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e LEFT JOIN Doctrine\Tests\Models\Company\CompanyManager m WITH e.id = m.id',
|
'SELECT e FROM Doctrine\Tests\Models\Company\CompanyEmployee e LEFT JOIN Doctrine\Tests\Models\Company\CompanyManager m WITH e.id = m.id',
|
||||||
'SELECT c0_.id AS id_0, c0_.name AS name_1, c1_.salary AS salary_2, c1_.department AS department_3, c1_.startDate AS startDate_4, c0_.discr AS discr_5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id LEFT JOIN company_managers c2_ INNER JOIN company_employees c4_ ON c2_.id = c4_.id INNER JOIN company_persons c3_ ON c2_.id = c3_.id ON (c0_.id = c3_.id)'
|
'SELECT c0_.id AS id_0, c0_.name AS name_1, c1_.salary AS salary_2, c1_.department AS department_3, c1_.startDate AS startDate_4, c0_.discr AS discr_5 FROM company_employees c1_ INNER JOIN company_persons c0_ ON c1_.id = c0_.id LEFT JOIN (company_managers c2_ INNER JOIN company_employees c4_ ON c2_.id = c4_.id INNER JOIN company_persons c3_ ON c2_.id = c3_.id) ON (c0_.id = c3_.id)'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2165,7 +2165,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
|
|||||||
// the where clause when not joining onto that table
|
// the where clause when not joining onto that table
|
||||||
$this->assertSqlGeneration(
|
$this->assertSqlGeneration(
|
||||||
'SELECT c FROM Doctrine\Tests\Models\Company\CompanyContract c LEFT JOIN Doctrine\Tests\Models\Company\CompanyEmployee e WITH e.id = c.salesPerson WHERE c.completed = true',
|
'SELECT c FROM Doctrine\Tests\Models\Company\CompanyContract c LEFT JOIN Doctrine\Tests\Models\Company\CompanyEmployee e WITH e.id = c.salesPerson WHERE c.completed = true',
|
||||||
"SELECT c0_.id AS id_0, c0_.completed AS completed_1, c0_.fixPrice AS fixPrice_2, c0_.hoursWorked AS hoursWorked_3, c0_.pricePerHour AS pricePerHour_4, c0_.maxPrice AS maxPrice_5, c0_.discr AS discr_6 FROM company_contracts c0_ LEFT JOIN company_employees c1_ INNER JOIN company_persons c2_ ON c1_.id = c2_.id ON (c2_.id = c0_.salesPerson_id) WHERE (c0_.completed = 1) AND c0_.discr IN ('fix', 'flexible', 'flexultra')"
|
"SELECT c0_.id AS id_0, c0_.completed AS completed_1, c0_.fixPrice AS fixPrice_2, c0_.hoursWorked AS hoursWorked_3, c0_.pricePerHour AS pricePerHour_4, c0_.maxPrice AS maxPrice_5, c0_.discr AS discr_6 FROM company_contracts c0_ LEFT JOIN (company_employees c1_ INNER JOIN company_persons c2_ ON c1_.id = c2_.id) ON (c2_.id = c0_.salesPerson_id) WHERE (c0_.completed = 1) AND c0_.discr IN ('fix', 'flexible', 'flexultra')"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user