Compare commits

...

2 commits

Author SHA1 Message Date
c64eb96049 fix: add proper Order Enum handling for MySQL->order() (#43)
I made a rushed merge with #41 and it doesn't work properly.. it throws an exception when passing an `Order` enum to the method, because we're only accepting strings. #42

Reviewed-on: https://codeberg.org/vlw/php-mysql/pulls/43
2025-01-30 09:33:10 +00:00
e65c74797b feat: add ORDER BY statement Enum (#41)
Importable with:
```php
use vlw\MySQL\Order
```
To be used with the `MySQL->order()` method, for example:
```php
$db->for("table")->order(["column" => Order::ASC])->select("*");
```

Reviewed-on: https://codeberg.org/vlw/php-mysql/pulls/41
2025-01-30 08:16:27 +00:00
2 changed files with 22 additions and 8 deletions

View file

@ -8,8 +8,10 @@
use mysqli_stmt;
use mysqli_result;
use vlw\MySQL\Order;
use vlw\MySQL\Operators;
require_once "Order.php";
require_once "Operators.php";
// Interface for MySQL_Driver with abstractions for data manipulation
@ -50,7 +52,11 @@
// Convert all boolean type values to tinyints in array
private static function filter_booleans(array $values): array {
return array_map(fn($v): mixed => gettype($v) === "boolean" ? self::filter_boolean($v) : $v, $values);
return array_map(fn(mixed $v): mixed => gettype($v) === "boolean" ? self::filter_boolean($v) : $v, $values);
}
private static function array_wrap_accents(array $input): array {
return array_map(fn(mixed $v): string => "`{$v}`", $input);
}
/*
@ -174,12 +180,12 @@
return $this;
}
// Create CSV from columns
$sql = implode(",", array_keys($order_by));
// Create pipe DSV from values
$sql .= " " . implode("|", array_values($order_by));
// Assign Order Enum entries from array of arrays<Order|string>
$orders = array_map(fn(Order|string $order): Order => $order instanceof Order ? $order : Order::tryFrom($order), array_values($order_by));
// Create CSV string with Prepared Statement abbreviations from length of fields array.
$sql = array_map(fn(string $column, Order|string $order): string => "`{$column}` " . $order->value, array_keys($order_by), $orders);
$this->order_by = $sql;
$this->order_by = implode(",", $sql);
return $this;
}
@ -196,7 +202,7 @@
$this->columns = is_array($columns) || is_null($columns) ? $columns : explode(",", $columns);
// Create CSV from columns or default to SQL NULL as a string
$columns_sql = $this->columns ? implode(",", $this->columns) : "NULL";
$columns_sql = $this->columns ? implode(",", self::array_wrap_accents($this->columns)) : "NULL";
// Create LIMIT statement if argument is defined
$limit_sql = !is_null($this->limit) ? " LIMIT {$this->limit}" : "";
@ -218,7 +224,7 @@
$this->throw_if_no_table();
// Create CSV string with Prepared Statement abbreviations from length of fields array.
$changes = array_map(fn($column) => "{$column} = ?", array_keys($entity));
$changes = array_map(fn($column) => "`{$column}` = ?", array_keys($entity));
$changes = implode(",", $changes);
// Get array of SQL WHERE string and filter values

8
src/Order.php Normal file
View file

@ -0,0 +1,8 @@
<?php
namespace vlw\MySQL;
enum Order: string {
case ASC = "ASC";
case DESC = "DESC";
}