Skip to content

Commit

Permalink
Merge pull request #23 from rohit267/feat/offset-query-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko authored Sep 29, 2024
2 parents 2602273 + 24ab41d commit 5002a68
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ public function limit($limit)
return $this;
}

/**
* Offset query items by a specific number
*
* @param string|number $offset The number to offset by
*/
public function offset($offset)
{
$this->query = Builder::offset($this->query, $offset);

return $this;
}

/**
* Retrieve a row from table
*
Expand Down
18 changes: 18 additions & 0 deletions src/Db/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ public static function limit(string $query, $number): string
return $query;
}


/**
* Offset query items by a specific number
*
* @param string $query The query to modify (if any)
* @param string|number $number Offset to query
*/
public static function offset(string $query, $number): string
{
if (strpos($query, ' OFFSET ') === false) {
$query .= " OFFSET $number";
} else {
$parts = explode(' OFFSET ', $query);
$query = implode(" OFFSET $number ", $parts);
}

}

/**
* Controls inner workings of all where blocks
*
Expand Down
9 changes: 9 additions & 0 deletions tests/mysql/leaf-builder.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@

expect(count($data))->toBe(2);
});

it('orders by dummy name and count with limit and offset', function () {
$db = new \Leaf\Db();
$db->connect('eu-cdbr-west-03.cleardb.net', 'heroku_fb1311a639bb407', 'b9607a8a6d5ebb', 'cc589b17');

$data = $db->select('test', 'name, COUNT(*)')->groupBy("created_at")->limit(1)->offset(1)->all();

expect(count($data))->toBe(1);
});

0 comments on commit 5002a68

Please sign in to comment.