composer update + SQL Compare refactored

This commit is contained in:
Roman Hergenreder
2022-11-26 23:57:28 +01:00
parent b1c4c9e976
commit 3b2b5984d6
23 changed files with 317 additions and 540 deletions

View File

@@ -0,0 +1,63 @@
<?php
namespace Core\Driver\SQL\Query;
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Condition\CondBool;
use Core\Driver\SQL\Condition\CondNot;
use Core\Driver\SQL\Condition\CondOr;
use Core\Driver\SQL\SQL;
abstract class ConditionalQuery extends Query {
private array $conditions;
public function __construct(SQL $sql) {
parent::__construct($sql);
$this->conditions = [];
}
public function getWhereClause(array &$params): string {
return $this->sql->getWhereClause($this->getConditions(), $params);
}
public function getConditions(): array {
return $this->conditions;
}
public function where(...$conditions): static {
$this->conditions[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
return $this;
}
public function whereEq(string $col, mixed $val): static {
$this->conditions[] = new Compare($col, $val, "=");
return $this;
}
public function whereNeq(string $col, mixed $val): static {
$this->conditions[] = new Compare($col, $val, "!=");
return $this;
}
public function whereGt(string $col, mixed $val): static {
$this->conditions[] = new Compare($col, $val, ">");
return $this;
}
public function whereLt(string $col, mixed $val): static {
$this->conditions[] = new Compare($col, $val, "<");
return $this;
}
public function whereTrue(string $col): static {
$this->conditions[] = new CondBool($col);
return $this;
}
public function whereFalse(string $col): static {
$this->conditions[] = new CondNot(new CondBool($col));
return $this;
}
}

View File

@@ -5,28 +5,20 @@ namespace Core\Driver\SQL\Query;
use Core\Driver\SQL\Condition\CondOr;
use Core\Driver\SQL\SQL;
class Delete extends Query {
class Delete extends ConditionalQuery {
private string $table;
private array $conditions;
public function __construct(SQL $sql, string $table) {
parent::__construct($sql);
$this->table = $table;
$this->conditions = array();
}
public function where(...$conditions): Delete {
$this->conditions[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
return $this;
}
public function getTable(): string { return $this->table; }
public function getConditions(): array { return $this->conditions; }
public function build(array &$params): ?string {
$table = $this->sql->tableName($this->getTable());
$where = $this->sql->getWhereClause($this->getConditions(), $params);
$where = $this->getWhereClause($params);
return "DELETE FROM $table$where";
}
}

View File

@@ -4,14 +4,15 @@ namespace Core\Driver\SQL\Query;
use Core\Driver\SQL\Condition\CondOr;
use Core\Driver\SQL\Expression\JsonArrayAgg;
use Core\Driver\SQL\Join;
use Core\Driver\SQL\Join\InnerJoin;
use Core\Driver\SQL\Join\Join;
use Core\Driver\SQL\Join\LeftJoin;
use Core\Driver\SQL\SQL;
class Select extends Query {
class Select extends ConditionalQuery {
private array $selectValues;
private array $tables;
private array $conditions;
private array $joins;
private array $orderColumns;
private array $groupColumns;
@@ -26,7 +27,6 @@ class Select extends Query {
parent::__construct($sql);
$this->selectValues = (!empty($selectValues) && is_array($selectValues[0])) ? $selectValues[0] : $selectValues;
$this->tables = array();
$this->conditions = array();
$this->havings = array();
$this->joins = array();
$this->orderColumns = array();
@@ -53,23 +53,18 @@ class Select extends Query {
return $this;
}
public function where(...$conditions): Select {
$this->conditions[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
return $this;
}
public function having(...$conditions): Select {
$this->havings[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
return $this;
}
public function innerJoin(string $table, string $columnA, string $columnB, ?string $tableAlias = null, array $conditions = []): Select {
$this->joins[] = new Join("INNER", $table, $columnA, $columnB, $tableAlias, $conditions);
$this->joins[] = new InnerJoin($table, $columnA, $columnB, $tableAlias, $conditions);
return $this;
}
public function leftJoin(string $table, string $columnA, string $columnB, ?string $tableAlias = null, array $conditions = []): Select {
$this->joins[] = new Join("LEFT", $table, $columnA, $columnB, $tableAlias, $conditions);
$this->joins[] = new LeftJoin($table, $columnA, $columnB, $tableAlias, $conditions);
return $this;
}
@@ -133,7 +128,6 @@ class Select extends Query {
public function getSelectValues(): array { return $this->selectValues; }
public function getTables(): array { return $this->tables; }
public function getConditions(): array { return $this->conditions; }
public function getJoins(): array { return $this->joins; }
public function isOrderedAscending(): bool { return $this->sortAscending; }
public function getOrderBy(): array { return $this->orderColumns; }
@@ -180,7 +174,7 @@ class Select extends Query {
}
$tables = $this->sql->tableName($tables);
$where = $this->sql->getWhereClause($this->getConditions(), $params);
$where = $this->getWhereClause($params);
$havingClause = "";
if (count($this->havings) > 0) {
$havingClause = " HAVING " . $this->sql->buildCondition($this->getHavings(), $params);

View File

@@ -2,25 +2,17 @@
namespace Core\Driver\SQL\Query;
use Core\Driver\SQL\Condition\CondOr;
use Core\Driver\SQL\SQL;
class Update extends Query {
class Update extends ConditionalQuery {
private array $values;
private string $table;
private array $conditions;
public function __construct(SQL $sql, string $table) {
parent::__construct($sql);
$this->values = array();
$this->table = $table;
$this->conditions = array();
}
public function where(...$conditions): Update {
$this->conditions[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
return $this;
}
public function set(string $key, $val): Update {
@@ -29,7 +21,6 @@ class Update extends Query {
}
public function getTable(): string { return $this->table; }
public function getConditions(): array { return $this->conditions; }
public function getValues(): array { return $this->values; }
public function build(array &$params): ?string {
@@ -41,7 +32,7 @@ class Update extends Query {
}
$valueStr = implode(",", $valueStr);
$where = $this->sql->getWhereClause($this->getConditions(), $params);
$where = $this->getWhereClause($params);
return "UPDATE $table SET $valueStr$where";
}
}