mirror of
https://codeberg.org/vlw/php-mysql.git
synced 2025-09-14 08:43:40 +02:00
fix: convert indent. to tabs
This commit is contained in:
parent
eb11bca86a
commit
4c18f5686d
2 changed files with 34 additions and 34 deletions
|
@ -6,17 +6,17 @@
|
|||
use \mysqli_stmt;
|
||||
use \mysqli_result;
|
||||
|
||||
// MySQL query builder and executer abstractions
|
||||
class DatabaseDriver extends mysqli {
|
||||
// MySQL query builder and executer abstractions
|
||||
class DatabaseDriver extends mysqli {
|
||||
// Passing arguments to https://www.php.net/manual/en/mysqli.construct.php
|
||||
function __construct() {
|
||||
function __construct() {
|
||||
parent::__construct(...func_get_args());
|
||||
}
|
||||
|
||||
// Create CSV from array
|
||||
private static function csv(array $items): string {
|
||||
return implode(",", $items);
|
||||
}
|
||||
// Create CSV from array
|
||||
private static function csv(array $items): string {
|
||||
return implode(",", $items);
|
||||
}
|
||||
|
||||
/* ---- */
|
||||
|
||||
|
@ -118,4 +118,4 @@
|
|||
// Return true if rows were matched
|
||||
: $query->num_rows > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,10 +6,10 @@
|
|||
|
||||
require_once "DatabaseDriver.php";
|
||||
|
||||
// Interface for MySQL_Driver with abstractions for data manipulation
|
||||
class MySQL extends DatabaseDriver {
|
||||
// Interface for MySQL_Driver with abstractions for data manipulation
|
||||
class MySQL extends DatabaseDriver {
|
||||
// Pass constructor arguments to driver
|
||||
function __construct() {
|
||||
function __construct() {
|
||||
parent::__construct(...func_get_args());
|
||||
}
|
||||
|
||||
|
@ -61,20 +61,20 @@
|
|||
|
||||
/* ---- */
|
||||
|
||||
// Create Prepared Statament for SELECT with optional WHERE filters
|
||||
public function get(string $table, array|string $columns = null, ?array $filter = [], ?array $order_by = null, int|array|null $limit = null): array|bool {
|
||||
// Create CSV string of columns if argument defined, else return bool
|
||||
$columns_sql = $columns ? self::columns($columns) : "NULL";
|
||||
// Create Prepared Statament for SELECT with optional WHERE filters
|
||||
public function get(string $table, array|string $columns = null, ?array $filter = [], ?array $order_by = null, int|array|null $limit = null): array|bool {
|
||||
// Create CSV string of columns if argument defined, else return bool
|
||||
$columns_sql = $columns ? self::columns($columns) : "NULL";
|
||||
// Create LIMIT statement if argument is defined
|
||||
$limit_sql = $limit ? self::limit($limit) : "";
|
||||
// Create ORDER BY statement if argument is defined
|
||||
$order_by_sql = $order_by ? self::order_by($order_by) : "";
|
||||
|
||||
// Get array of SQL WHERE string and filter values
|
||||
// Get array of SQL WHERE string and filter values
|
||||
[$filter_sql, $filter_values] = self::where($filter);
|
||||
|
||||
// Interpolate components into an SQL SELECT statmenet and execute
|
||||
$sql = "SELECT {$columns_sql} FROM {$table}{$filter_sql}{$order_by_sql}{$limit_sql}";
|
||||
$sql = "SELECT {$columns_sql} FROM {$table}{$filter_sql}{$order_by_sql}{$limit_sql}";
|
||||
|
||||
// No columns were specified, return true if query matched rows
|
||||
if (!$columns) {
|
||||
|
@ -85,18 +85,18 @@
|
|||
$exec = $this->exec($sql, $filter_values);
|
||||
// Flatten array if $limit === 1
|
||||
return empty($exec) || $limit !== 1 ? $exec : $exec[0];
|
||||
}
|
||||
}
|
||||
|
||||
// Create Prepared Statement for UPDATE using PRIMARY KEY as anchor
|
||||
public function update(string $table, array $fields, ?array $filter = null): bool {
|
||||
// Create CSV string with Prepared Statement abbreviations from length of fields array.
|
||||
$changes = array_map(fn($column) => "{$column} = ?", array_keys($fields));
|
||||
$changes = implode(",", $changes);
|
||||
// Create Prepared Statement for UPDATE using PRIMARY KEY as anchor
|
||||
public function update(string $table, array $fields, ?array $filter = null): bool {
|
||||
// Create CSV string with Prepared Statement abbreviations from length of fields array.
|
||||
$changes = array_map(fn($column) => "{$column} = ?", array_keys($fields));
|
||||
$changes = implode(",", $changes);
|
||||
|
||||
// Get array of SQL WHERE string and filter values
|
||||
[$filter_sql, $filter_values] = self::where($filter);
|
||||
|
||||
$values = array_values($fields);
|
||||
$values = array_values($fields);
|
||||
// Append filter values if defined
|
||||
if ($filter_values) {
|
||||
array_push($values, ...$filter_values);
|
||||
|
@ -104,16 +104,16 @@
|
|||
|
||||
// Interpolate components into an SQL UPDATE statement and execute
|
||||
$sql = "UPDATE {$table} SET {$changes} {$filter_sql}";
|
||||
return $this->exec_bool($sql, $values);
|
||||
}
|
||||
return $this->exec_bool($sql, $values);
|
||||
}
|
||||
|
||||
// Create Prepared Statemt for INSERT
|
||||
public function insert(string $table, array $values): bool {
|
||||
// Return CSV string with Prepared Statement abbreviatons from length of fields array.
|
||||
$values_stmt = self::csv(array_fill(0, count($values), "?"));
|
||||
// Create Prepared Statemt for INSERT
|
||||
public function insert(string $table, array $values): bool {
|
||||
// Return CSV string with Prepared Statement abbreviatons from length of fields array.
|
||||
$values_stmt = self::csv(array_fill(0, count($values), "?"));
|
||||
|
||||
// Interpolate components into an SQL INSERT statement and execute
|
||||
$sql = "INSERT INTO {$table} VALUES ({$values_stmt})";
|
||||
return $this->exec_bool($sql, $values);
|
||||
}
|
||||
}
|
||||
$sql = "INSERT INTO {$table} VALUES ({$values_stmt})";
|
||||
return $this->exec_bool($sql, $values);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue