Nette\Database\Drivers\SqlsrvDriver::applyLimit PHP Method

applyLimit() public method

Injects LIMIT/OFFSET to the SQL query.
public applyLimit ( &$sql, $limit, $offset )
    public function applyLimit(&$sql, $limit, $offset)
    {
        if ($limit < 0 || $offset < 0) {
            throw new Nette\InvalidArgumentException('Negative offset or limit.');
        } elseif (version_compare($this->version, 11, '<')) {
            // 11 == SQL Server 2012
            if ($offset) {
                throw new Nette\NotSupportedException('Offset is not supported by this database.');
            } elseif ($limit !== NULL) {
                $sql = preg_replace('#^\\s*(SELECT(\\s+DISTINCT|\\s+ALL)?|UPDATE|DELETE)#i', '$0 TOP ' . (int) $limit, $sql, 1, $count);
                if (!$count) {
                    throw new Nette\InvalidArgumentException('SQL query must begin with SELECT, UPDATE or DELETE command.');
                }
            }
        } elseif ($limit !== NULL || $offset) {
            // requires ORDER BY, see https://technet.microsoft.com/en-us/library/gg699618(v=sql.110).aspx
            $sql .= ' OFFSET ' . (int) $offset . ' ROWS ' . 'FETCH NEXT ' . (int) $limit . ' ROWS ONLY';
        }
    }