Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Offset Query Builder #20 #23

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
Loading