From f4b532ccd45e74aa7415da000d6a55a4aa9d2d77 Mon Sep 17 00:00:00 2001 From: Roman Nehrulenko Date: Tue, 26 Jun 2018 15:34:01 +0300 Subject: [PATCH 1/2] Offset clause, unit tests for query builder: limit, offset --- src/InfluxDB/Query/Builder.php | 23 ++++++++++ tests/unit/Query/BuilderTest.php | 73 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 tests/unit/Query/BuilderTest.php diff --git a/src/InfluxDB/Query/Builder.php b/src/InfluxDB/Query/Builder.php index 7afe2c2..b91d8f0 100644 --- a/src/InfluxDB/Query/Builder.php +++ b/src/InfluxDB/Query/Builder.php @@ -65,6 +65,11 @@ class Builder */ protected $limitClause = ''; + /** + * @var string + */ + protected $offsetClause = ''; + /** * @var array */ @@ -248,6 +253,20 @@ public function limit($count) return $this; } + /** + * Offset the ResultSet to n records + * + * @param int $count + * + * @return $this + */ + public function offset($count) + { + $this->offsetClause = sprintf(' OFFSET %s', (int) $count); + + return $this; + } + /** * Add retention policy to query * @@ -322,6 +341,10 @@ protected function parseQuery() $query .= $this->limitClause; } + if ($this->offsetClause) { + $query .= $this->offsetClause; + } + return $query; } } diff --git a/tests/unit/Query/BuilderTest.php b/tests/unit/Query/BuilderTest.php new file mode 100644 index 0000000..06e7aa1 --- /dev/null +++ b/tests/unit/Query/BuilderTest.php @@ -0,0 +1,73 @@ +queryBuilder = $this->database->getQueryBuilder(); + } + + /** + * @return array + */ + public function provideLimit() + { + return [ + ['test_metric', 2], + ]; + } + + /** + * @dataProvider provideLimit + * @param $from + * @param $limit + */ + public function testLimit($from, $limit) + { + $this->assertEquals( + sprintf('SELECT * FROM "%s" LIMIT %s', $from, $limit), + $this->database->getQueryBuilder() + ->from($from) + ->limit($limit) + ->getQuery() + ); + } + + /** + * @return array + */ + public function provideOffset() + { + return [ + ['test_metric', 2], + ]; + } + + /** + * @dataProvider provideOffset + * @param $offset + * @param $from + */ + public function testOffset($from, $offset) + { + $this->assertEquals( + sprintf('SELECT * FROM "%s" OFFSET %s', $from, $offset), + $this->database->getQueryBuilder() + ->from($from) + ->offset($offset) + ->getQuery() + ); + } + } \ No newline at end of file From 84d84ad4e5f2c1bba97b65f6603fbce2563587f0 Mon Sep 17 00:00:00 2001 From: Roman Nehrulenko Date: Fri, 6 Jul 2018 13:04:37 +0300 Subject: [PATCH 2/2] README, using offset --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4bf686a..70b1ab0 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ $result = $database->getQueryBuilder() ->select('cpucount') ->from('test_metric') ->limit(2) + ->offset(2) ->getResultSet() ->getPoints();