Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

BUG Accept $limit=0 in SQLQuery->setLimit()

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...
commit 23e51b871b1f908d9c1051fbceb87bc251085ba3 1 parent 4603378
@chillu chillu authored
Showing with 35 additions and 1 deletion.
  1. +2 −1  model/SQLQuery.php
  2. +33 −0 tests/model/SQLQueryTest.php
View
3  model/SQLQuery.php
@@ -450,6 +450,7 @@ public function getLimit() {
* 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.
+ * Only applies for positive values, or if an $offset is set as well.
* @param int $offset
*
* @throws InvalidArgumentException
@@ -461,7 +462,7 @@ public function setLimit($limit, $offset = 0) {
throw new InvalidArgumentException("SQLQuery::setLimit() only takes positive values");
}
- if($limit && is_numeric($limit)) {
+ if(is_numeric($limit) && ($limit || $offset)) {
$this->limit = array(
'start' => $offset,
'limit' => $limit,
View
33 tests/model/SQLQueryTest.php
@@ -138,6 +138,39 @@ public function testSelectWithOrderbyClause() {
$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
*/
Please sign in to comment.
Something went wrong with that request. Please try again.