1
0
mirror of synced 2024-12-13 22:56:04 +03:00

extracted pgsql sql generation into a helper method

This commit is contained in:
Miha Vrhovnik 2012-11-12 13:48:55 +01:00
parent c7a75f477f
commit 8fe9fa0dc7

View File

@ -86,7 +86,7 @@ class LimitSubqueryOutputWalker extends SqlWalker
*/ */
public function walkSelectStatement(SelectStatement $AST) 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 // 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 // It may be possible to make this work with multiple root entities but that
@ -133,13 +133,43 @@ class LimitSubqueryOutputWalker extends SqlWalker
} }
// Build the counter query // Build the counter query
$sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result',
implode(', ', $sqlIdentifier), $innerSql);
if ($this->platform instanceof PostgreSqlPlatform) { if ($this->platform instanceof PostgreSqlPlatform) {
//http://www.doctrine-project.org/jira/browse/DDC-1958 //http://www.doctrine-project.org/jira/browse/DDC-1958
$this->getPostgresqlSql($AST, $sqlIdentifier, $innerSql, $sql);
}
// Apply the limit and offset
$sql = $this->platform->modifyLimitQuery(
$sql, $this->maxResults, $this->firstResult
);
// Add the columns to the ResultSetMapping. It's not really nice but
// it works. Preferably I'd clear the RSM or simply create a new one
// but that is not possible from inside the output walker, so we dirty
// up the one we have.
foreach ($sqlIdentifier as $property => $alias) {
$this->rsm->addScalarResult($alias, $property);
}
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 // For every order by, find out the SQL alias by inspecting the ResultSetMapping
$sqlOrderColumns = array(); $sqlOrderColumns = array();
$orderBy = array(); $orderBy = array();
if (isset($AST->orderByClause)){ if (isset($AST->orderByClause)) {
foreach ($AST->orderByClause->orderByItems as $item) { foreach ($AST->orderByClause->orderByItems as $item) {
$possibleAliases = array_keys($this->rsm->fieldMappings, $item->expression->field); $possibleAliases = array_keys($this->rsm->fieldMappings, $item->expression->field);
@ -158,33 +188,15 @@ class LimitSubqueryOutputWalker extends SqlWalker
//we don't need orderBy in inner query //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 //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; /*$AST->orderByClause = null;
$sql = parent::walkSelectStatement($AST);*/ $innerSql = parent::walkSelectStatement($AST);*/
if (count($orderBy)) { if (count($orderBy)) {
$sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s', $sql = sprintf(
implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)), $sql, implode(', ', $orderBy)); 'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s',
} else { implode(', ', array_merge($sqlIdentifier, $sqlOrderColumns)),
$sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', $innerSql,
implode(', ', $sqlIdentifier), $sql); implode(', ', $orderBy)
}
} else {
$sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result',
implode(', ', $sqlIdentifier), $sql);
}
// Apply the limit and offset
$sql = $this->platform->modifyLimitQuery(
$sql, $this->maxResults, $this->firstResult
); );
// Add the columns to the ResultSetMapping. It's not really nice but
// it works. Preferably I'd clear the RSM or simply create a new one
// but that is not possible from inside the output walker, so we dirty
// up the one we have.
foreach ($sqlIdentifier as $property => $alias) {
$this->rsm->addScalarResult($alias, $property);
} }
return $sql;
} }
} }