Compile the limit / offset row constraint for a query.

@param  \Illuminate\Database\Query\Builder $query
@return string
193938
protected function compileRowConstraint($query)
    {
        $start  = $query->offset + 1;
        $finish = $query->offset + $query->limit;

        if ($query->limit == 1 && is_null($query->offset)) {
            return '= 1';
        }

        if ($query->offset && is_null($query->limit)) {
            return ">= {$start}";
        }

        return "between {$start} and {$finish}";
    }
740045
protected function compileAnsiOffset(Builder $query, $components)
	{
		// An ORDER BY clause is required to make this offset query work, so if one does
		// not exist we'll just create a dummy clause to trick the database and so it
		// does not complain about the queries for not having an "order by" clause.
		if (! isset($components['orders'])) {
			$components['orders'] = 'order by (select 0)';
		}

		// We need to add the row number to the query so we can compare it to the offset
		// and limit values given for the statements. So we will add an expression to
		// the "select" that will give back the row numbers on each of the records.
		$orderings = $components['orders'];

		$components['columns'] .= $this->compileOver($orderings);

		unset($components['orders']);

		// Next we need to calculate the constraints that should be placed on the query
		// to get the right offset and limit from our query but if there is no limit
		// set we will just handle the offset only since that is all that matters.
		$constraint = $this->compileRowConstraint($query);

		$sql = $this->concatenate($components);

		// We are now ready to build the final SQL query so we'll create a common table
		// expression from the query and get the records with row numbers within our
		// given limit and offset value that we just put on as a query constraint.
		return $this->compileTableExpression($sql, $constraint);
	}
687
public function update(Query $query)
    {
        $options = $query->getOptions();

        $table = $this->parseTable($query, $options['table']);
        $data  = $this->parseData($query, $options['data']);

        if (empty($data)) {
            return '';
        }

        foreach ($data as $key => $val) {
            $set[] = $key . ' = ' . $val;
        }

        return str_replace(
            ['%TABLE%', '%SET%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
            [
                $this->parseTable($query, $options['table']),
                implode(' , ', $set),
                $this->parseJoin($query, $options['join']),
                $this->parseWhere($query, $options['where']),
                $this->parseOrder($query, $options['order']),
                $this->parseLimit($query, $options['limit']),
                $this->parseLock($query, $options['lock']),
                $this->parseComment($query, $options['comment']),
            ],
            $this->updateSql);
    }
760826
protected function setKeysForSaveQuery(Builder $query)
    {
        parent::setKeysForSaveQuery($query);
        $query->where('locale', '=', $this->locale);

        return $query;
    }
211147
protected function compileUnions(Builder $query)
    {
        $sql = '';

        foreach ($query->unions as $union) {
            $sql .= $this->compileUnion($union);
        }

        if (! empty($query->unionOrders)) {
            $sql .= ' '.$this->compileOrders($query, $query->unionOrders);
        }

        if (isset($query->unionLimit)) {
            $sql .= ' '.$this->compileLimit($query, $query->unionLimit);
        }

        if (isset($query->unionOffset)) {
            $sql .= ' '.$this->compileOffset($query, $query->unionOffset);
        }

        return ltrim($sql);
    }
1284835
public function compileSelect(Builder $query)
    {
        if ($query->unions && $query->aggregate) {
            return $this->compileUnionAggregate($query);
        }

        // If the query does not have any columns set, we'll set the columns to the
        // * character to just get all of the columns from the database. Then we
        // can build the query and concatenate all the pieces together as one.
        $original = $query->columns;

        if (is_null($query->columns)) {
            $query->columns = ['*'];
        }

        // To compile the query, we'll spin through each component of the query and
        // see if that component exists. If it does we'll just call the compiler
        // function for the component which is responsible for making the SQL.
        $sql = trim($this->concatenate(
            $this->compileComponents($query))
        );

        $query->columns = $original;

        return $sql;
    }
714308
public function scopeMostVisited(Builder $query, $limit = null)
    {
        $query->select(\DB::raw('nodes.*, count(*) as `aggregate`'))
            ->join('node_site_view', 'nodes.id', '=', 'node_site_view.node_id')
            ->groupBy('nodes.id')
            ->orderBy('aggregate', 'desc');

        if ($limit)
        {
            $query->limit($limit);
        }

        return $query;
    }
115319
protected function search(Builder $query)
    {
        $q = request('q');

        if (!$q || strlen($q) < $this->minimumSearchInputLength)
            return $query;

        return $this->searchQuery($query, $q);
    }
680512
protected function compileUnions(Builder $query)
	{
		$sql = '';

		foreach ($query->unions as $union)
		{
			$sql .= $this->compileUnion($union);
		}

		return ltrim($sql);
	}
612462
public function compileSelect(Builder $query)
    {
        if (is_null($query->columns)) $query->columns = array('*');

        return trim($this->concatenate($this->compileComponents($query)));
    }
1370072
protected function compileComponents(Builder $query)
    {
        if($query->orders === null) {
            $query->orderByRaw('1 asc');
        }

        $sql = parent::compileComponents($query);

        return $sql;
    }
264377
public function applyOffsetAndLimit(QueryBuilder $query, ?int $offset, ?int $limit): void
    {
        $query->setFirstResult($offset ?? 0);

        if (is_int($limit) && $limit > 0) {
            $query->setMaxResults($limit);
        }
    }
796759
public function compileJoins(Builder $query): string {
        if (empty($query->joins)) {
            return '';
        }
        $sql = '';
        foreach ($query->joins as $item) {
            $sql .= sprintf(' %s JOIN %s ON %s',
                strtoupper($item['type']),
                $query->addPrefix($item['table']),
                $item['on']);
        }
        return $sql;
    }
112314
public function buildTruncate(Query $query) {
        return $this->renderStatement(Query::TRUNCATE, [
            'table' => $this->formatTable($query->getTable())
        ] + $this->formatAttributes($query->getAttributes()));
    }
479812
protected function set_limit($query, $limit = 0, $offset = 0)
    {
        $limit  = intval($limit);
        $offset = intval($offset);
        $end    = $offset + $limit;

        // @TODO: Oracle 12g has better OFFSET support

        if (!$offset) {
            $query = "SELECT * FROM ($query) a WHERE rownum <= $end";
        }
        else {
            $query = "SELECT * FROM (SELECT a.*, rownum as rn FROM ($query) a WHERE rownum <= $end) b WHERE rn > $offset";
        }

        return $query;
    }
476283
public function buildQuery(QueryAbstract $query)
    {
        $queryParts = $query->getQueryParts();
        return implode(
            ' ',
            array_filter(
                [
                    $queryParts['type'],
                    $this->getFields($queryParts),
                    $this->getTable($queryParts),
                    $this->getSet($queryParts),
                    $this->getValues($queryParts),
                    $this->getJoins($queryParts),
                    $this->getWhere($queryParts),
                    $this->getGroup($queryParts),
                    $this->getOrder($queryParts),
                    $this->getLimit($queryParts)
                ],
                function ($var) {
                    return $var !== null;
                }
            )
        );
    }
142788
public function compile_update( &$query )
	{
		$build = 'update '.$this->escape_table( $query ).' set ';

		// add the array values.
		foreach( $query->values as $key => $value )
		{
			$build .= $this->escape( $key ).' = '.$this->param( $value ).', ';
		}

		$build = substr( $build, 0, -2 );
		$build .= $this->compile_where( $query );
		$build .= $this->compile_limit( $query );

		// cut the last comma away
		return $build;
	}