From 761f7044f71be833461610af5cbe337fe5166b21 Mon Sep 17 00:00:00 2001 From: rohit267 Date: Sun, 29 Sep 2024 10:21:37 +0530 Subject: [PATCH 1/2] add offset query builder --- src/Db.php | 12 ++++++++++++ src/Db/Builder.php | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Db.php b/src/Db.php index 86e5e15..7069a83 100644 --- a/src/Db.php +++ b/src/Db.php @@ -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 * diff --git a/src/Db/Builder.php b/src/Db/Builder.php index d071fe7..33548d4 100644 --- a/src/Db/Builder.php +++ b/src/Db/Builder.php @@ -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 * From 24ab41d4e28e46bce5e928e1ee3c82747faefa8d Mon Sep 17 00:00:00 2001 From: rohit267 Date: Sun, 29 Sep 2024 10:26:04 +0530 Subject: [PATCH 2/2] add test --- tests/mysql/leaf-builder.test.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/mysql/leaf-builder.test.php b/tests/mysql/leaf-builder.test.php index 2f87dd6..853ef99 100644 --- a/tests/mysql/leaf-builder.test.php +++ b/tests/mysql/leaf-builder.test.php @@ -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); +});