From 9f025dba5657e1077a8c3fd1ce094742a4ba500d Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Thu, 19 May 2016 12:59:48 -0500 Subject: [PATCH] allow OFFSET even when LIMIT not specified. fixes #88. --- phpunit.xml.dist | 5 +++++ src/AbstractQuery.php | 27 ++++++++++++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 761e67c..a43a7ac 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,4 +4,9 @@ ./tests + + + ./src + + diff --git a/src/AbstractQuery.php b/src/AbstractQuery.php index 93f616a..9813207 100644 --- a/src/AbstractQuery.php +++ b/src/AbstractQuery.php @@ -468,24 +468,29 @@ protected function buildOrderBy() * * Builds the `LIMIT ... OFFSET` clause of the statement. * + * Note that this will allow OFFSET values with a LIMIT. + * * @return string * */ protected function buildLimit() { - $has_limit = $this instanceof LimitInterface; - $has_offset = $this instanceof LimitOffsetInterface; + $clause = ''; + $limit = $this instanceof LimitInterface && $this->limit; + $offset = $this instanceof LimitOffsetInterface && $this->offset; - if ($has_offset && $this->limit) { - $clause = PHP_EOL . "LIMIT {$this->limit}"; - if ($this->offset) { - $clause .= " OFFSET {$this->offset}"; - } - return $clause; - } elseif ($has_limit && $this->limit) { - return PHP_EOL . "LIMIT {$this->limit}"; + if ($limit) { + $clause .= "LIMIT {$this->limit}"; + } + + if ($offset) { + $clause .= " OFFSET {$this->offset}"; + } + + if ($clause) { + $clause = PHP_EOL . trim($clause); } - return ''; // not applicable + return $clause; } }