Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #56 from lifekent/feature/offset
Browse files Browse the repository at this point in the history
Offset clause for the \InfluxDB\Query\Builder class
  • Loading branch information
TheCodeAssassin authored Jul 6, 2018
2 parents 4e571ea + 84d84ad commit 3868330
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ $result = $database->getQueryBuilder()
->select('cpucount')
->from('test_metric')
->limit(2)
->offset(2)
->getResultSet()
->getPoints();

Expand Down
23 changes: 23 additions & 0 deletions src/InfluxDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class Builder
*/
protected $limitClause = '';

/**
* @var string
*/
protected $offsetClause = '';

/**
* @var array
*/
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -322,6 +341,10 @@ protected function parseQuery()
$query .= $this->limitClause;
}

if ($this->offsetClause) {
$query .= $this->offsetClause;
}

return $query;
}
}
73 changes: 73 additions & 0 deletions tests/unit/Query/BuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace InfluxDB\Test\unit\Query;

use InfluxDB\Query\Builder;
use InfluxDB\Test\unit\AbstractTest;

class BuilderTest extends AbstractTest
{
/**
* @var Builder
*/
protected $queryBuilder;

public function setUp()
{
parent::setUp();

$this->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()
);
}
}

0 comments on commit 3868330

Please sign in to comment.