lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
private function isOrderByInTopNSubquery($query, $currentPosition)
    {
        // Grab query text on the same nesting level as the ORDER BY clause we're examining.
        $subQueryBuffer = '';
        $parenCount     = 0;

        // If $parenCount goes negative, we've exited the subquery we're examining.
        // If $currentPosition goes negative, we've reached the beginning of the query.
        while ($parenCount >= 0 && $currentPosition >= 0) {
            if ($query[$currentPosition] === '(') {
                $parenCount--;
            } elseif ($query[$currentPosition] === ')') {
                $parenCount++;
            }

            // Only yank query text on the same nesting level as the ORDER BY clause.
            $subQueryBuffer = ($parenCount === 0 ? $query[$currentPosition] : ' ') . $subQueryBuffer;

            $currentPosition--;
        }

        return (bool) preg_match('/SELECT\s+(DISTINCT\s+)?TOP\s/i', $subQueryBuffer);
    }
private function isOrderByInTopNSubquery($query, $currentPosition)
    {
        // Grab query text on the same nesting level as the ORDER BY clause we're examining.
        $subQueryBuffer = '';
        $parenCount     = 0;

        // If $parenCount goes negative, we've exited the subquery we're examining.
        // If $currentPosition goes negative, we've reached the beginning of the query.
        while ($parenCount >= 0 && $currentPosition >= 0) {
            if ($query[$currentPosition] === '(') {
                $parenCount--;
            } elseif ($query[$currentPosition] === ')') {
                $parenCount++;
            }

            // Only yank query text on the same nesting level as the ORDER BY clause.
            $subQueryBuffer = ($parenCount === 0 ? $query[$currentPosition] : ' ') . $subQueryBuffer;

            $currentPosition--;
        }

        return (bool) preg_match('/SELECT\s+(DISTINCT\s+)?TOP\s/i', $subQueryBuffer);
    }
private function isOrderByInTopNSubquery ( $ query , $ currentPosition ) { // Grab query text on the same nesting level as the ORDER BY clause we're examining. $ subQueryBuffer = '' ; $ parenCount = 0 ; // If $parenCount goes negative, we've exited the subquery we're examining. // If $currentPosition goes negative, we've reached the beginning of the query. while ( $ parenCount >= 0 && $ currentPosition >= 0 ) { if ( $ query [ $ currentPosition ] === '(' ) { $ parenCount -- ; } elseif ( $ query [ $ currentPosition ] === ')' ) { $ parenCount ++ ; } // Only yank query text on the same nesting level as the ORDER BY clause. $ subQueryBuffer = ( $ parenCount === 0 ? $ query [ $ currentPosition ] : ' ' ) . $ subQueryBuffer ; $ currentPosition -- ; } return ( bool ) preg_match ( '/SELECT\s+(DISTINCT\s+)?TOP\s/i' , $ subQueryBuffer ) ; }
Check an ORDER BY clause to see if it is in a TOP N query or subquery.

@param string $query           The query
@param int    $currentPosition Start position of ORDER BY clause

@return bool true if ORDER BY is in a TOP N query, false otherwise
Check an ORDER BY clause to see if it is in a TOP N query or subquery .