2025-02-03 17:44:52 +01:00
|
|
|
<?php
|
2025-02-05 12:29:49 +01:00
|
|
|
namespace DatabaseHelper;
|
2025-02-03 17:44:52 +01:00
|
|
|
|
|
|
|
use DatabaseHelper\enums\ColumnTypes;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2025-02-05 12:29:49 +01:00
|
|
|
class Query
|
2025-02-03 17:44:52 +01:00
|
|
|
{
|
2025-02-05 12:29:49 +01:00
|
|
|
private Table $table;
|
2025-02-03 17:44:52 +01:00
|
|
|
private array $columns = ['*'];
|
|
|
|
private array $conditions = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ColumnTypes[]
|
|
|
|
*/
|
|
|
|
private array $columnTypes = [];
|
|
|
|
|
2025-02-05 12:29:49 +01:00
|
|
|
public function __construct(Table $table) {
|
2025-02-03 17:44:52 +01:00
|
|
|
$this->columnTypes = array_map(fn($col) => $col['colType'], $table->columns);
|
|
|
|
$this->table = $table;
|
|
|
|
}
|
|
|
|
|
2025-02-05 12:29:49 +01:00
|
|
|
public function select(string ...$cols): Query {
|
2025-02-03 17:44:52 +01:00
|
|
|
if (!empty($cols))
|
|
|
|
$this->columns = $cols;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2025-02-05 12:29:49 +01:00
|
|
|
public function where(string $col, string $operator, mixed $val): Query {
|
2025-02-03 17:44:52 +01:00
|
|
|
return $this->addCondition($col, $operator, $val, "AND");
|
|
|
|
}
|
|
|
|
|
2025-02-05 12:29:49 +01:00
|
|
|
public function andWhere(string $col, string $operator, mixed $val): Query {
|
2025-02-03 17:44:52 +01:00
|
|
|
return $this->addCondition($col, $operator, $val, "AND");
|
|
|
|
}
|
|
|
|
|
2025-02-05 12:29:49 +01:00
|
|
|
public function orWhere(string $col, string $operator, mixed $val): Query {
|
2025-02-03 17:44:52 +01:00
|
|
|
return $this->addCondition($col, $operator, $val, "OR");
|
|
|
|
}
|
|
|
|
|
2025-02-05 12:29:49 +01:00
|
|
|
private function addCondition(string $colName, string $operator, mixed $val, string $prefix): Query {
|
2025-02-03 17:44:52 +01:00
|
|
|
if (!isset($this->columnTypes[$colName]))
|
|
|
|
throw new InvalidArgumentException("Unknown column: $colName");
|
|
|
|
|
|
|
|
$columnType = $this->columnTypes[$colName];
|
|
|
|
$castedValue = $columnType->dbCast($val);
|
|
|
|
|
|
|
|
if (!empty($this->conditions))
|
|
|
|
$this->conditions[] = $prefix;
|
|
|
|
$this->conditions[] = "$colName $operator $castedValue";
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toSql(): string {
|
|
|
|
$columns = implode(", ", $this->columns);
|
|
|
|
$table = $this->table->tableName;
|
|
|
|
$whereClause = !empty($this->conditions) ? " WHERE " . implode(" ", $this->conditions) : "";
|
|
|
|
return esc_sql("SELECT $columns FROM $table$whereClause");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function query(): array {
|
|
|
|
global $wpdb;
|
|
|
|
$query = $this->toSql();
|
|
|
|
$results = $wpdb->get_results($wpdb->prepare($query), ARRAY_A);
|
|
|
|
return $this->castResults($results);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function castResults(array $results): array {
|
|
|
|
foreach ($results as &$row)
|
|
|
|
foreach ($row as $column => &$value)
|
|
|
|
if (isset($this->columnTypes[$column]))
|
|
|
|
$value = $this->columnTypes[$column]->valCast($value);
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
}
|