Limit clause

@param string $sql
@param int $limit
@param int|bool $offset
@return string
425891
public function limit(string $sql, int $limit, $offset=FALSE): string
	{
		$sql .= "\nLIMIT {$limit}";

		if (is_numeric($offset))
		{
			$sql .= " OFFSET {$offset}";
		}

		return $sql;
	}
316247
public function formatLimitOffset($limit, $offset = 0) {
        if ($limit && $offset) {
            return sprintf($this->getClause(self::LIMIT_OFFSET), (int) $limit, (int) $offset);
        }

        return $this->formatLimit($limit);
    }
619917
private function getSearchQueryLimit($query, $limit, $offset)
	{
		if ($limit > -1) {
			$query->limit($limit);
			$query->offset($offset);
		}

		return $query;
	}
740981
private function createSql2Query($sql2, $limit = null, $offset = null)
    {
        $queryManager = $this->sessionManager->getSession()->getWorkspace()->getQueryManager();
        $query = $queryManager->createQuery($sql2, 'JCR-SQL2');

        if ($limit) {
            $query->setLimit($limit);
        }
        if ($offset) {
            $query->setOffset($offset);
        }

        return $query;
    }
193370
protected function _limit($sql)
	{
		$limit = $this->qb_offset + $this->qb_limit;

		// As of SQL Server 2005 (9.0.*) ROW_NUMBER() is supported,
		// however an ORDER BY clause is required for it to work
		if (version_compare($this->version(), '9', '>=') && $this->qb_offset && ! empty($this->qb_orderby))
		{
			$orderby = $this->_compile_order_by();

			// We have to strip the ORDER BY clause
			$sql = trim(substr($sql, 0, strrpos($sql, $orderby)));

			// Get the fields to select from our subquery, so that we can avoid CI_rownum appearing in the actual results
			if (count($this->qb_select) === 0)
			{
				$select = '*'; // Inevitable
			}
			else
			{
				// Use only field names and their aliases, everything else is out of our scope.
				$select = array();
				$field_regexp = ($this->_quoted_identifier)
					? '("[^\"]+")' : '(\[[^\]]+\])';
				for ($i = 0, $c = count($this->qb_select); $i < $c; $i++)
				{
					$select[] = preg_match('/(?:\s|\.)'.$field_regexp.'$/i', $this->qb_select[$i], $m)
						? $m[1] : $this->qb_select[$i];
				}
				$select = implode(', ', $select);
			}

			return 'SELECT '.$select." FROM (\n\n"
				.preg_replace('/^(SELECT( DISTINCT)?)/i', '\\1 ROW_NUMBER() OVER('.trim($orderby).') AS '.$this->escape_identifiers('CI_rownum').', ', $sql)
				."\n\n) ".$this->escape_identifiers('CI_subquery')
				."\nWHERE ".$this->escape_identifiers('CI_rownum').' BETWEEN '.($this->qb_offset + 1).' AND '.$limit;
		}

		return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$limit.' ', $sql);
	}
469413
public function limit($sql, $count, $offset = 0)
    {
        $count = intval($count);
        if ($count <= 0) {
            /**
             * @see Zend_Db_Adapter_Mysqli_Exception
             */
            // require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
            throw new Zend_Db_Adapter_Mysqli_Exception("LIMIT argument count=$count is not valid");
        }

        $offset = intval($offset);
        if ($offset < 0) {
            /**
             * @see Zend_Db_Adapter_Mysqli_Exception
             */
            // require_once 'Zend/Db/Adapter/Mysqli/Exception.php';
            throw new Zend_Db_Adapter_Mysqli_Exception("LIMIT argument offset=$offset is not valid");
        }

        $sql .= " LIMIT $count";
        if ($offset > 0) {
            $sql .= " OFFSET $offset";
        }

        return $sql;
    }
244037
protected function _limit($sql)
	{
		// Limit clause depends on if Interbase or Firebird
		if (stripos($this->version(), 'firebird') !== FALSE)
		{
			$select = 'FIRST '.$this->qb_limit
				.($this->qb_offset ? ' SKIP '.$this->qb_offset : '');
		}
		else
		{
			$select = 'ROWS '
				.($this->qb_offset ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit);
		}

		return preg_replace('`SELECT`i', 'SELECT '.$select, $sql, 1);
	}
281927
function applyLimit(&$sql, $offset, $limit) {

		$max = $offset + $limit;

		// nesting all queries, in case there's already a WHERE clause
		$sql = <<<EOF
SELECT A.*, rownum AS PROPEL\$ROWNUM
FROM (
  $sql
) A
WHERE rownum <= $max
EOF;

		if ($offset > 0) {
			$sql = <<<EOF
SELECT B.*
FROM (
  $sql
) B
WHERE B.PROPEL\$ROWNUM > $offset
EOF;
		}
	}
1239051
public function getResult($limit = 0, $offset = 0)
    {
        $this->count();
        $this->queryBuilder->setFirstResult($offset)->setMaxResults($limit);

        if ($limit > 0) {
            $pathOrderBy = $this->getSqlPart($this->queryBuilder, 'orderBy');
            $this->resetSqlParts($this->queryBuilder, ['orderBy']);
            $qb = $this->cloneQb($this->queryBuilder);
            $qb->select(sprintf('%s', $this->aliasDotPrimary()))
                ->setFirstResult(null)
                ->setMaxResults(null);

            /** @var OrderBy $part */
            foreach ($pathOrderBy as $part) {
                foreach ($part->getParts() as $order) {
                    $out = [];
                    preg_match('/(.*[^\s])?\s+(asc|desc)/i', $order, $out);
                    $property = $out[1];
                    $direction = $out[2];
                    $alias = ($key = array_search($property, $this->aliasMapping)) ?: $property;
                    if ($this->isAggFunc($property)) {
                        $qb->addSelect($property.' AS '.$alias);
                        $qb->addOrderBy($alias, $direction);
                        $this->queryBuilder->addOrderBy($alias, $direction);
                    } else {
                        $qb->addOrderBy($property, $direction);
                        $this->queryBuilder->addOrderBy($property, $direction);
                    }
                }
            }

            $query = $qb->getQuery();

            foreach ($this->hints as $name => $hint) {
                $query->setHint($name, $hint);
            }

            $params = $types = [];
            /** @var Parameter $parameter */
            foreach ($query->getParameters() as $key => $parameter) {
                $params[$key] = $parameter->getValue();
                $types[$key] = $parameter->getType();
            }

            /** @var \PDOStatement $stmt */
            $stmt = $this->queryBuilder->getEntityManager()->getConnection()->executeQuery(
                $query->getSQL(),
                $params,
                $types
            );

            $ids = $stmt->fetchAll(\PDO::FETCH_COLUMN, 0);
            $ids = array_values(array_unique($ids));

            if (!$ids) {
                return [];
            }
            $ids = array_slice($ids, $offset, $limit);
            $this->queryBuilder
                ->setFirstResult(null)
                ->setMaxResults(null)
                ->where(sprintf("%s IN (:_sps_ids_)", $this->aliasDotPrimary()))
                ->setParameters(['_sps_ids_' => $ids]);
        }
        $query = $this->queryBuilder->getQuery();
        foreach ($this->hints as $name => $hint) {
            $query->setHint($name, $hint);
        }
        $this->result = $query->getResult();
        if (!$this->totalResultCount) {
            $this->totalResultCount = count($this->result);
        }

        return $this->result;
    }
395623
protected function applyLimit($sql){
		list($limit, $offset) = $this->stmt->getLimit();
		if((int) $limit < 1 && (int) $offset < 1)
			return $sql;

		switch($this->stmt->getConnection()->getDriver()->getDriverName()){
			case 'mysql':
			case 'pgsql':
			case 'sqlite':
			case 'sqlite2':
				if((int) $limit > 0){
					$sql .= "\nLIMIT " . (int) $limit;
					if((int) $offset > 0)
						$sql .= ' OFFSET ' . (int) $offset;
				}
				return $sql;

			case 'odbc':
			case 'mssql':
				if($offset < 1)
					return 'SELECT TOP ' . (int) $limit . " *\nFROM (\n\t"
						 . implode("\n\t", explode("\n", $sql)) . "\n)";

			default:
				throw new DriverException('PDO or selected driver does not allow apllying limit or offset.');
		}
	}
80631
public function get($term = null, $limit = null, $offset = 0)
    {
        if (false === $this->has_table()) return false;

        $term = $this->normalize_term($term);

        $this->db->where($term);

        $limit !== null || $limit = $this->data_limit;

        // Limit the result if $limit is int and greater than 0
        if (false !== $limit and is_int($limit) and $limit > 0) {
            // Forget about offset for now :P
            $this->db->limit($limit, $offset);
        }

        if ( ! $data = $this->db->get($this->table)) {
            $this->set_error($this->db->error());
            return false;
        }

        return 1 === $limit ? $data->row() : $data->result();
    }
1284811
public function prepareLimit($limit, $offset = null)
    {
        if ($offset) {
            $this->setLimit = ' LIMIT ' . $limit . ', ' . $offset . '';
        } else {
            $this->setLimit = ' LIMIT ' . $limit . '';
        }

        return $this;
    }
1029925
public function find($limit = 20, $offset = 0)
	{
		return $this->buildFind()
			->setLimit($limit)
			->setOffset($offset)
			->find();
	}
743935
public function limit($offset, $limit)
	{
		$this->data = $this->data_source->limit($limit, $offset)->fetchAll();

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

        if (is_int($limit) && $limit > 0) {
            $query->setMaxResults($limit);
        }
    }
821165
public function setLimit($count, $offset = null)
    {
        $this->_dsql()->limit($count, $offset);

        return $this;
    }
1354310
public function setLimit(int $limit, ?int $offset = null): self
    {
        $this->limit = $limit;
        if ($offset !== null) {
            $this->setOffset($offset);
        }

        return $this;
    }