yii\db\mssql\QueryBuilder::oldBuildOrderByAndLimit PHP Method

oldBuildOrderByAndLimit() protected method

Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2005 to 2008.
protected oldBuildOrderByAndLimit ( string $sql, array $orderBy, integer $limit, integer $offset ) : string
$sql string the existing SQL (without ORDER BY/LIMIT/OFFSET)
$orderBy array the order by columns. See [[\yii\db\Query::orderBy]] for more details on how to specify this parameter.
$limit integer the limit number. See [[\yii\db\Query::limit]] for more details.
$offset integer the offset number. See [[\yii\db\Query::offset]] for more details.
return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any)
    protected function oldBuildOrderByAndLimit($sql, $orderBy, $limit, $offset)
    {
        $orderBy = $this->buildOrderBy($orderBy);
        if ($orderBy === '') {
            // ROW_NUMBER() requires an ORDER BY clause
            $orderBy = 'ORDER BY (SELECT NULL)';
        }
        $sql = preg_replace('/^([\\s(])*SELECT(\\s+DISTINCT)?(?!\\s*TOP\\s*\\()/i', "\\1SELECT\\2 rowNum = ROW_NUMBER() over ({$orderBy}),", $sql);
        if ($this->hasLimit($limit)) {
            $sql = "SELECT TOP {$limit} * FROM ({$sql}) sub";
        } else {
            $sql = "SELECT * FROM ({$sql}) sub";
        }
        if ($this->hasOffset($offset)) {
            $sql .= $this->separator . "WHERE rowNum > {$offset}";
        }
        return $sql;
    }