mirror of
https://codeberg.org/vlw/php-mysql.git
synced 2025-09-13 16:23:42 +02:00
feat: pass null to reset statements (#31)
This commit is contained in:
parent
111bd5c822
commit
5fefc5d19f
2 changed files with 46 additions and 8 deletions
28
README.md
28
README.md
|
@ -5,10 +5,10 @@ This library provides abstraction methods for common operations on MySQL-like da
|
|||
For example:
|
||||
```php
|
||||
MySQL->for(string $table)
|
||||
->with(array $model)
|
||||
->where(array $filters)
|
||||
->order(array $order_by)
|
||||
->limit(int|array $limit)
|
||||
->with(?array $model)
|
||||
->where(?array ...$conditions)
|
||||
->order(?array $order_by)
|
||||
->limit(int|array|null $limit)
|
||||
->select(array $columns): array|bool;
|
||||
```
|
||||
which would be equivalent to the following in MySQL:
|
||||
|
@ -142,7 +142,7 @@ true
|
|||
Modify existing rows with `MySQL->update()`
|
||||
|
||||
```php
|
||||
MySQL->get(
|
||||
MySQL->update(
|
||||
// Key, value array of column names and values to update
|
||||
array $fields,
|
||||
): 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.
|
||||
|
||||
```php
|
||||
MySQL->where(
|
||||
?array ...$conditions
|
||||
): self;
|
||||
```
|
||||
|
||||
### Example
|
||||
```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");
|
||||
|
@ -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
|
||||
|
||||
```php
|
||||
MySQL->order(
|
||||
?array $order_by
|
||||
): self;
|
||||
```
|
||||
|
||||
```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
|
||||
```
|
||||
|
@ -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
|
||||
|
||||
```php
|
||||
MySQL->limit(
|
||||
int|array|null $limit
|
||||
): self;
|
||||
```
|
||||
|
||||
> **Note**
|
||||
> You can also flatten to a single dimensional array from the first entity by chaining [`MySQL->flatten()`](#flatten-array-to-single-dimension)
|
||||
|
||||
|
|
|
@ -94,7 +94,15 @@
|
|||
}
|
||||
|
||||
// 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 = [];
|
||||
$filters = [];
|
||||
|
||||
|
@ -143,7 +151,13 @@
|
|||
}
|
||||
|
||||
// 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
|
||||
if (is_int($limit)) {
|
||||
$this->limit = $limit;
|
||||
|
@ -167,7 +181,13 @@
|
|||
}
|
||||
|
||||
// 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
|
||||
$sql = implode(",", array_keys($order_by));
|
||||
// Create pipe DSV from values
|
||||
|
|
Loading…
Add table
Reference in a new issue