feat: pass null to reset statements (#31)

This commit is contained in:
Victor Westerlund 2024-01-12 13:22:06 +01:00 committed by GitHub
parent 111bd5c822
commit 5fefc5d19f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 8 deletions

View file

@ -5,10 +5,10 @@ This library provides abstraction methods for common operations on MySQL-like da
For example: For example:
```php ```php
MySQL->for(string $table) MySQL->for(string $table)
->with(array $model) ->with(?array $model)
->where(array $filters) ->where(?array ...$conditions)
->order(array $order_by) ->order(?array $order_by)
->limit(int|array $limit) ->limit(int|array|null $limit)
->select(array $columns): array|bool; ->select(array $columns): array|bool;
``` ```
which would be equivalent to the following in MySQL: which would be equivalent to the following in MySQL:
@ -142,7 +142,7 @@ true
Modify existing rows with `MySQL->update()` Modify existing rows with `MySQL->update()`
```php ```php
MySQL->get( MySQL->update(
// Key, value array of column names and values to update // Key, value array of column names and values to update
array $fields, array $fields,
): mysqli_result|bool; ): mysqli_result|bool;
@ -164,6 +164,12 @@ In most cases you probably want to UPDATE against a constaint. Chain a [`where()
Filter a `select()` or `update()` method by chaining the `MySQL->where()` method anywhere before it. Filter a `select()` or `update()` method by chaining the `MySQL->where()` method anywhere before it.
```php
MySQL->where(
?array ...$conditions
): self;
```
### Example ### Example
```php ```php
$coffee = MySQL->for("beverages")->where(["beverage_type" => "coffee"])->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages WHERE (beverage_type = "coffee"); $coffee = MySQL->for("beverages")->where(["beverage_type" => "coffee"])->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages WHERE (beverage_type = "coffee");
@ -225,6 +231,12 @@ WHERE (beverage_type = 'coffee' AND beverage_size = 15) OR (beverage_type = 'tea
Chain the `order()` method before a `select()` statement to order by a specific column Chain the `order()` method before a `select()` statement to order by a specific column
```php
MySQL->order(
?array $order_by
): self;
```
```php ```php
$coffee = MySQL->for("beverages")->order(["beverage_name" => "ASC"])->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages ORDER BY beverage_name ASC $coffee = MySQL->for("beverages")->order(["beverage_name" => "ASC"])->select(["beverage_name", "beverage_size"]); // SELECT beverage_name, beverage_size FROM beverages ORDER BY beverage_name ASC
``` ```
@ -246,6 +258,12 @@ $coffee = MySQL->for("beverages")->order(["beverage_name" => "ASC"])->select(["b
Chain the `limit()` method before a `select()` statement to limit the amount of columns returned Chain the `limit()` method before a `select()` statement to limit the amount of columns returned
```php
MySQL->limit(
int|array|null $limit
): self;
```
> **Note** > **Note**
> You can also flatten to a single dimensional array from the first entity by chaining [`MySQL->flatten()`](#flatten-array-to-single-dimension) > You can also flatten to a single dimensional array from the first entity by chaining [`MySQL->flatten()`](#flatten-array-to-single-dimension)

View file

@ -94,7 +94,15 @@
} }
// Create a WHERE statement from filters // Create a WHERE statement from filters
public function where(array ...$conditions): self { public function where(?array ...$conditions): self {
// Unset filters if null was passed
if ($conditions === null) {
$this->filter_sql = null;
$this->filter_values = null;
return $this;
}
$values = []; $values = [];
$filters = []; $filters = [];
@ -143,7 +151,13 @@
} }
// 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 $limit): self { public function limit(int|array|null $limit): self {
// Unset row limit if null was passed
if ($limit === null) {
$this->limit = null;
return $this;
}
// Set LIMIT without range directly as integer // Set LIMIT without range directly as integer
if (is_int($limit)) { if (is_int($limit)) {
$this->limit = $limit; $this->limit = $limit;
@ -167,7 +181,13 @@
} }
// Return SQL SORT BY string from assoc array of columns and direction // Return SQL SORT BY string from assoc array of columns and direction
public function order(array $order_by): self { public function order(?array $order_by): self {
// Unset row order by if null was passed
if ($order_by === null) {
$this->order_by = null;
return $this;
}
// Create CSV from columns // Create CSV from columns
$sql = implode(",", array_keys($order_by)); $sql = implode(",", array_keys($order_by));
// Create pipe DSV from values // Create pipe DSV from values