Skip to content

Commit

Permalink
BUG Accept $limit=0 in SQLQuery->setLimit()
Browse files Browse the repository at this point in the history
SQLQuery->setLimit(0, 99) should result in "SELECT ... LIMIT 0 OFFSET 1".
In fact it does "SELECT ..." without a LIMIT clause at all,
which is unexpected. This is regardless of the $offset value.
  • Loading branch information
chillu committed Jun 6, 2013
1 parent 4603378 commit 23e51b8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion model/SQLQuery.php
Expand Up @@ -450,6 +450,7 @@ public function getLimit() {
* Internally, limit will always be stored as a map containing the keys 'start' and 'limit' * Internally, limit will always be stored as a map containing the keys 'start' and 'limit'
* *
* @param int|string|array $limit If passed as a string or array, assumes SQL escaped data. * @param int|string|array $limit If passed as a string or array, assumes SQL escaped data.
* Only applies for positive values, or if an $offset is set as well.
* @param int $offset * @param int $offset
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
Expand All @@ -461,7 +462,7 @@ public function setLimit($limit, $offset = 0) {
throw new InvalidArgumentException("SQLQuery::setLimit() only takes positive values"); throw new InvalidArgumentException("SQLQuery::setLimit() only takes positive values");
} }


if($limit && is_numeric($limit)) { if(is_numeric($limit) && ($limit || $offset)) {
$this->limit = array( $this->limit = array(
'start' => $offset, 'start' => $offset,
'limit' => $limit, 'limit' => $limit,
Expand Down
33 changes: 33 additions & 0 deletions tests/model/SQLQueryTest.php
Expand Up @@ -138,6 +138,39 @@ public function testSelectWithOrderbyClause() {
$query->sql()); $query->sql());
} }


public function testNullLimit() {
$query = new SQLQuery();
$query->setFrom("MyTable");
$query->setLimit(null);

$this->assertEquals(
'SELECT * FROM MyTable',
$query->sql()
);
}

public function testZeroLimit() {
$query = new SQLQuery();
$query->setFrom("MyTable");
$query->setLimit(0);

$this->assertEquals(
'SELECT * FROM MyTable',
$query->sql()
);
}

public function testZeroLimitWithOffset() {
$query = new SQLQuery();
$query->setFrom("MyTable");
$query->setLimit(0, 99);

$this->assertEquals(
'SELECT * FROM MyTable LIMIT 0 OFFSET 99',
$query->sql()
);
}

/** /**
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
*/ */
Expand Down

0 comments on commit 23e51b8

Please sign in to comment.