Commit bcc72a56 authored by Bill Schaller's avatar Bill Schaller Committed by Steve Müller

Rebuild SQLServerPlatform::doModifyLimitQuery again to use a CTE and only 1 regex.

* fix tests to use new CTE-based method
parent b9ec9276
......@@ -1182,77 +1182,63 @@ class SQLServerPlatform extends AbstractPlatform
$start = $offset + 1;
$end = $offset + $limit;
$orderBy = stristr($query, 'ORDER BY');
//Remove ORDER BY from $query (including nested parentheses in order by list).
$query = preg_replace('/\s+ORDER\s+BY\s+([^()]+|\((?:(?:(?>[^()]+)|(?R))*)\))+/i', '', $query);
$format = 'SELECT * FROM (%s) AS doctrine_tbl WHERE doctrine_rownum BETWEEN %d AND %d ORDER BY doctrine_rownum';
// Pattern to match "main" SELECT ... FROM clause (including nested parentheses in select list).
$selectFromPattern = '/^(\s*SELECT\s+(?:(.*)(?![^(]*\))))\sFROM\s/i';
if ( ! $orderBy) {
//Replace only "main" FROM with OVER to prevent changing FROM also in subqueries.
$query = preg_replace(
$selectFromPattern,
'$1, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM ',
// We'll find a SELECT or SELECT distinct and append TOP n to it
// Even if the TOP n is very large, the use of a CTE will
// allow the SQL Server query planner to optimize it so it doesn't
// actually scan the entire range covered by the TOP clause.
$selectPattern = '/^(\s*SELECT\s+(?:DISTINCT|)\s*)(.*)$/i';
$replacePattern = sprintf('$1%s $2', "TOP $end");
$query = preg_replace($selectPattern, $replacePattern, $query);
if (stristr($query, "ORDER BY")) {
// Inner order by is not valid in SQL Server for our purposes
// TODO throw DBALException here?
$query = $this->scrubInnerOrderBy($query);
}
// Build a new limited query around the original, using a CTE
return sprintf(
"WITH dctrn_cte AS (%s) "
. "SELECT * FROM ("
. "SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM dctrn_cte"
. ") AS doctrine_tbl "
. "WHERE doctrine_rownum BETWEEN %d AND %d ORDER BY doctrine_rownum ASC",
$query,
1
);
return sprintf($format, $query, $start, $end);
}
//Clear ORDER BY
$orderBy = preg_replace('/ORDER\s+BY\s+(.*)/i', '$1', $orderBy);
$orderByParts = explode(',', $orderBy);
$orderByColumns = array();
//Split ORDER BY into parts
foreach ($orderByParts as &$part) {
if (preg_match('/(([^\s]*)\.)?([^\.\s]*)\s*(ASC|DESC)?/i', trim($part), $matches)) {
$orderByColumns[] = array(
'column' => $matches[3],
'hasTable' => ( ! empty($matches[2])),
'sort' => isset($matches[4]) ? $matches[4] : null,
'table' => empty($matches[2]) ? '[^\.\s]*' : $matches[2]
$start,
$end
);
}
}
$isWrapped = (preg_match('/SELECT DISTINCT .* FROM \(.*\) dctrn_result/', $query)) ? true : false;
$overColumns = array();
//Find alias for each column used in ORDER BY
if ( ! empty($orderByColumns)) {
foreach ($orderByColumns as $column) {
$pattern = sprintf('/%s\.%s\s+(?:AS\s+)?([^,\s)]+)/i', $column['table'], $column['column']);
if ($isWrapped) {
$overColumn = preg_match($pattern, $query, $matches)
? $matches[1] : '';
} else {
$overColumn = preg_match($pattern, $query, $matches)
? ($column['hasTable'] ? $column['table'] . '.' : '') . $column['column']
: $column['column'];
/**
* Remove ORDER BY clauses in subqueries - they're not supported by SQL Server.
*
* @param $query
* @return string
*
* @todo allow ORDER BY clauses in subqueries that have TOP n, as that is ok.
*/
private function scrubInnerOrderBy($query) {
$count = substr_count(strtoupper($query), "ORDER BY");
while ($count-- > 0) {
$qLen = strlen($query);
$orderByPos = stripos($query, " ORDER BY");
$parenCount = 0;
$currentPosition = $orderByPos;
while ($parenCount >= 0 && $currentPosition < $qLen) {
if ($query[$currentPosition] == '(') {
$parenCount++;
} elseif ($query[$currentPosition] == ')') {
$parenCount--;
}
if (isset($column['sort'])) {
$overColumn .= ' ' . $column['sort'];
$currentPosition++;
}
$overColumns[] = $overColumn;
if ($currentPosition < $qLen - 1) {
$query = substr($query, 0, $orderByPos) . substr($query, $currentPosition - 1);
}
}
//Replace only first occurrence of FROM with $over to prevent changing FROM also in subqueries.
$over = 'ORDER BY ' . implode(', ', $overColumns);
$query = preg_replace($selectFromPattern, "$1, ROW_NUMBER() OVER ($over) AS doctrine_rownum FROM ", $query, 1);
return sprintf($format, $query, $start, $end);
return $query;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment