Doctrine\DBAL\Platforms\SQLServerPlatform::isOrderByInTopNSubquery PHP Method

isOrderByInTopNSubquery() private method

Check an ORDER BY clause to see if it is in a TOP N query or subquery.
private isOrderByInTopNSubquery ( string $query, integer $currentPosition ) : boolean
$query string The query
$currentPosition integer Start position of ORDER BY clause
return boolean true if ORDER BY is in a TOP N query, false otherwise
    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--;
        }
        if (preg_match('/SELECT\\s+(DISTINCT\\s+)?TOP\\s/i', $subQueryBuffer)) {
            return true;
        }
        return false;
    }
SQLServerPlatform