Skip to content

Commit f272929

Browse files
committed
- Allowing OFFSET without LIMIT
1 parent 9ef975e commit f272929

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

src/Builder/Select.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function __toString() {
145145
$query = $this->buildGroups($query);
146146
$query = $this->buildHavingConditions($query);
147147
$query = $this->buildOrder($query);
148-
$query = $this->buildLimit($query);
148+
$query = $this->buildLimit($query, $this->getOffset());
149149
$query = $this->buildOffset($query);
150150
$query = $this->buildUnions($query);
151151
$query = $this->buildForUpdate($query);

src/Builder/Traits/LimitBuilder.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ trait LimitBuilder {
55
/** @var int */
66
private $limit = null;
77

8+
/**
9+
* @return int
10+
*/
11+
protected function getLimit() {
12+
return $this->limit;
13+
}
14+
815
/**
916
* @param int $limit
1017
* @return $this
@@ -16,11 +23,16 @@ public function limit($limit) {
1623

1724
/**
1825
* @param string $query
26+
* @param null $offset
1927
* @return string
2028
*/
21-
protected function buildLimit($query) {
22-
if($this->limit !== null) {
23-
$query .= "LIMIT\n\t{$this->limit}\n";
29+
protected function buildLimit($query, $offset = null) {
30+
$limit = $this->limit;
31+
if($limit === null && $offset !== null) {
32+
$limit = '18446744073709551615';
33+
}
34+
if($limit !== null) {
35+
$query .= "LIMIT\n\t{$limit}\n";
2436
}
2537
return $query;
2638
}

src/Builder/Traits/OffsetBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ trait OffsetBuilder {
55
/** @var int */
66
private $offset = null;
77

8+
/**
9+
* @return int
10+
*/
11+
protected function getOffset() {
12+
return $this->offset;
13+
}
14+
815
/**
916
* @param int $offset
1017
* @return $this

tests/Builder/SelectTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ public function testOffset() {
146146
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nLIMIT\n\t100\nOFFSET\n\t50\n", $str);
147147
}
148148

149+
public function testOffsetWithoutLimit() {
150+
$str = TestSelect::create()
151+
->field('a')
152+
->from('t', 'test')
153+
->offset(50)
154+
->asString();
155+
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nLIMIT\n\t18446744073709551615\nOFFSET\n\t50\n", $str);
156+
}
157+
149158
public function testForUpdate() {
150159
$str = TestSelect::create()
151160
->field('a')

0 commit comments

Comments
 (0)