fix: remove where method for models

This commit is contained in:
Victor Westerlund 2024-09-25 15:15:17 +02:00
parent adc2fda90a
commit c5d5954fcb

View file

@ -15,7 +15,6 @@
// Interface for MySQL_Driver with abstractions for data manipulation // Interface for MySQL_Driver with abstractions for data manipulation
class MySQL extends mysqli { class MySQL extends mysqli {
private string $table; private string $table;
private ?array $model = null;
private ?string $limit = null; private ?string $limit = null;
private ?string $order_by = null; private ?string $order_by = null;
@ -55,14 +54,6 @@
return array_map(fn($v): mixed => gettype($v) === "boolean" ? self::filter_boolean($v) : $v, $values); return array_map(fn($v): mixed => gettype($v) === "boolean" ? self::filter_boolean($v) : $v, $values);
} }
// Return value(s) that exist in $this->model
private function in_model(string|array $columns): ?array {
// Place string into array
$columns = is_array($columns) ? $columns : [$columns];
// Return columns that exist in table model
return array_filter($columns, fn($col): string => in_array($col, $this->model));
}
/* /*
# Definers # Definers
These methods are used to build an SQL query by chaining methods together. These methods are used to build an SQL query by chaining methods together.
@ -80,30 +71,6 @@
return $this; return $this;
} }
// Restrict query to array of column names
public function with(?array $model = null): self {
// Remove table model if empty
if (!$model) {
$this->model = null;
return $this;
}
// Reset table model
$this->model = [];
foreach ($model as $k => $v) {
// Column values must be strings
if (!is_string($v)) {
throw new Exception("Key {$k} must have a value of type string");
}
// Append column to model
$this->model[] = $v;
}
return $this;
}
// Create a WHERE statement from filters // Create a WHERE statement from filters
public function where(?array ...$conditions): self { public function where(?array ...$conditions): self {
// Unset filters if null was passed // Unset filters if null was passed
@ -128,10 +95,6 @@
// Create SQL string and append values to array for prepared statement // Create SQL string and append values to array for prepared statement
foreach ($condition as $col => $operation) { foreach ($condition as $col => $operation) {
if ($this->model && !$this->in_model($col)) {
continue;
}
// Assume we want an equals comparison if value is not an array // Assume we want an equals comparison if value is not an array
if (!is_array($operation)) { if (!is_array($operation)) {
$operation = [Operators::EQUALS->value => $operation]; $operation = [Operators::EQUALS->value => $operation];
@ -230,11 +193,6 @@
// Create array of columns from CSV // Create array of columns from CSV
$this->columns = is_array($columns) || is_null($columns) ? $columns : explode(",", $columns); $this->columns = is_array($columns) || is_null($columns) ? $columns : explode(",", $columns);
// Filter columns that aren't in the model if defiend
if ($columns && $this->model) {
$columns = $this->in_model($this->columns);
}
// Create CSV from columns or default to SQL NULL as a string // 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(",", $this->columns) : "NULL";
@ -257,16 +215,6 @@
public function update(array $entity): mysqli_result|bool { public function update(array $entity): mysqli_result|bool {
$this->throw_if_no_table(); $this->throw_if_no_table();
// Make constraint for table model if defined
if ($this->model) {
foreach (array_keys($entity) as $col) {
// Throw if column in entity does not exist in defiend table model
if (!in_array($col, $this->model)) {
throw new Exception("Column key '{$col}' does not exist in table model");
}
}
}
// Create CSV string with Prepared Statement abbreviations from length of fields array. // 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); $changes = implode(",", $changes);
@ -291,11 +239,6 @@
public function insert(array $values): mysqli_result|bool { public function insert(array $values): mysqli_result|bool {
$this->throw_if_no_table(); $this->throw_if_no_table();
// A value for each column in table model must be provided
if ($this->model && count($values) !== count($this->model)) {
throw new Exception("Values length does not match columns in model");
}
/* /*
Use array keys from $values as columns to insert if array is associative. Use array keys from $values as columns to insert if array is associative.
Treat statement as an all-columns INSERT if the $values array is sequential. Treat statement as an all-columns INSERT if the $values array is sequential.
@ -317,18 +260,6 @@
public function delete(array ...$conditions): mysqli_result|bool { public function delete(array ...$conditions): mysqli_result|bool {
$this->throw_if_no_table(); $this->throw_if_no_table();
// Make constraint for table model if defined
if ($this->model) {
foreach ($conditions as $condition) {
foreach (array_keys($condition) as $col) {
// Throw if column in entity does not exist in defiend table model
if (!in_array($col, $this->model)) {
throw new Exception("Column key '{$col}' does not exist in table model");
}
}
}
}
// Set DELETE WHERE conditions from arguments // Set DELETE WHERE conditions from arguments
$this->where(...$conditions); $this->where(...$conditions);