diff --git a/src/MySQL.php b/src/MySQL.php index 8fe9ef8..48f9cef 100644 --- a/src/MySQL.php +++ b/src/MySQL.php @@ -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 + $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