From d7e6a644158b23831dbd92e3284e7ab8f639561e Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Fri, 12 Jan 2024 12:58:06 +0100 Subject: [PATCH] feat: pass null to reset statements --- README.md | 28 +++++++++++++++++++++++----- src/MySQL.php | 26 +++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 936bef3..4361ae3 100644 --- a/README.md +++ b/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) diff --git a/src/MySQL.php b/src/MySQL.php index 7919b31..cd4964b 100644 --- a/src/MySQL.php +++ b/src/MySQL.php @@ -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