mirror of
https://codeberg.org/vlw/php-mysql.git
synced 2025-09-14 08:43:40 +02:00
feat: limit offset as second argument
This commit is contained in:
parent
d7e6a64415
commit
63baaf0793
1 changed files with 6 additions and 11 deletions
|
@ -17,7 +17,7 @@
|
||||||
private ?string $order_by = null;
|
private ?string $order_by = null;
|
||||||
private ?string $filter_sql = null;
|
private ?string $filter_sql = null;
|
||||||
private array $filter_values = [];
|
private array $filter_values = [];
|
||||||
private int|string|null $limit = null;
|
private ?string $limit = null;
|
||||||
|
|
||||||
// Pass constructor arguments to driver
|
// Pass constructor arguments to driver
|
||||||
function __construct() {
|
function __construct() {
|
||||||
|
@ -151,25 +151,20 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return SQL LIMIT string from integer or array of [offset => limit]
|
// Return SQL LIMIT string from integer or array of [offset => limit]
|
||||||
public function limit(int|array|null $limit): self {
|
public function limit(?int $limit, ?int $offset = null): self {
|
||||||
// Unset row limit if null was passed
|
// Unset row limit if null was passed
|
||||||
if ($limit === null) {
|
if ($limit === null) {
|
||||||
$this->limit = null;
|
$this->limit = null;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set LIMIT without range directly as integer
|
// No offset defined, set limit property directly as string
|
||||||
if (is_int($limit)) {
|
if (is_null($offset)) {
|
||||||
$this->limit = $limit;
|
$this->limit = (string) $limit;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use array key as LIMIT range start value
|
// Set limit and offset as SQL CSV
|
||||||
$offset = (int) array_keys($limit)[0];
|
|
||||||
// Use array value as LIMIT range end value
|
|
||||||
$limit = (int) array_values($limit)[0];
|
|
||||||
|
|
||||||
// Set limit as SQL CSV
|
|
||||||
$this->limit = "{$offset},{$limit}";
|
$this->limit = "{$offset},{$limit}";
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue