From 8fe9fa0dc7d62ba9f9a4157c643c73d8c7436fbd Mon Sep 17 00:00:00 2001 From: Miha Vrhovnik Date: Mon, 12 Nov 2012 13:48:55 +0100 Subject: [PATCH] extracted pgsql sql generation into a helper method --- .../Pagination/LimitSubqueryOutputWalker.php | 84 +++++++++++-------- 1 file changed, 48 insertions(+), 36 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index f09217ffd..a3e1df2dc 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -86,7 +86,7 @@ class LimitSubqueryOutputWalker extends SqlWalker */ public function walkSelectStatement(SelectStatement $AST) { - $sql = parent::walkSelectStatement($AST); + $innerSql = parent::walkSelectStatement($AST); // Find out the SQL alias of the identifier column of the root entity // It may be possible to make this work with multiple root entities but that @@ -133,43 +133,12 @@ class LimitSubqueryOutputWalker extends SqlWalker } // Build the counter query + $sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', + implode(', ', $sqlIdentifier), $innerSql); + if ($this->platform instanceof PostgreSqlPlatform) { //http://www.doctrine-project.org/jira/browse/DDC-1958 - - // For every order by, find out the SQL alias by inspecting the ResultSetMapping - $sqlOrderColumns = array(); - $orderBy = array(); - if (isset($AST->orderByClause)){ - foreach ($AST->orderByClause->orderByItems as $item) { - $possibleAliases = array_keys($this->rsm->fieldMappings, $item->expression->field); - - foreach ($possibleAliases as $alias) { - if ($this->rsm->columnOwnerMap[$alias] == $item->expression->identificationVariable) { - $sqlOrderColumns[] = $alias; - $orderBy[] = $alias . ' ' . $item->type; - break; - } - } - } - //remove identifier aliases - $sqlOrderColumns = array_diff($sqlOrderColumns, $sqlIdentifier); - } - - //we don't need orderBy in inner query - //However at least on 5.4.6 I'm getting a segmentation fault and thus we don't clear it for now - /*$AST->orderByClause = null; - $sql = parent::walkSelectStatement($AST);*/ - - if (count($orderBy)) { - $sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s', - implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)), $sql, implode(', ', $orderBy)); - } else { - $sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', - implode(', ', $sqlIdentifier), $sql); - } - } else { - $sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', - implode(', ', $sqlIdentifier), $sql); + $this->getPostgresqlSql($AST, $sqlIdentifier, $innerSql, $sql); } // Apply the limit and offset @@ -187,4 +156,47 @@ class LimitSubqueryOutputWalker extends SqlWalker return $sql; } + + /** + * Generate new SQL for postgresql if necessary + * + * @param SelectStatement $AST + * @param array sqlIdentifier + * @param string $sql + */ + public function getPostgresqlSql(SelectStatement $AST, array $sqlIdentifier, $innerSql, &$sql) + { + // For every order by, find out the SQL alias by inspecting the ResultSetMapping + $sqlOrderColumns = array(); + $orderBy = array(); + if (isset($AST->orderByClause)) { + foreach ($AST->orderByClause->orderByItems as $item) { + $possibleAliases = array_keys($this->rsm->fieldMappings, $item->expression->field); + + foreach ($possibleAliases as $alias) { + if ($this->rsm->columnOwnerMap[$alias] == $item->expression->identificationVariable) { + $sqlOrderColumns[] = $alias; + $orderBy[] = $alias . ' ' . $item->type; + break; + } + } + } + //remove identifier aliases + $sqlOrderColumns = array_diff($sqlOrderColumns, $sqlIdentifier); + } + + //we don't need orderBy in inner query + //However at least on 5.4.6 I'm getting a segmentation fault and thus we don't clear it for now + /*$AST->orderByClause = null; + $innerSql = parent::walkSelectStatement($AST);*/ + + if (count($orderBy)) { + $sql = sprintf( + 'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s', + implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)), + $innerSql, + implode(', ', $orderBy) + ); + } + } }