This commit is contained in:
2021-11-11 14:25:26 +01:00
parent 1737a2f592
commit 25d47f7528
32 changed files with 633 additions and 121 deletions

View File

@@ -0,0 +1,22 @@
<?php
namespace Driver\SQL\Condition;
use Driver\SQL\Query\Select;
class Exists extends Condition
{
private Select $subQuery;
public function __construct(Select $subQuery)
{
$this->subQuery = $subQuery;
}
public function getSubQuery(): Select
{
return $this->subQuery;
}
}

View File

@@ -340,7 +340,7 @@ class PostgreSQL extends SQL {
return ($statusTexts[$status] ?? "Unknown") . " (v$version)";
}
protected function buildCondition($condition, &$params) {
public function buildCondition($condition, &$params) {
if($condition instanceof CondRegex) {
$left = $condition->getLeftExp();
$right = $condition->getRightExp();

View File

@@ -54,6 +54,11 @@ class AlterTable extends Query {
return $this;
}
public function resetAutoIncrement(): AlterTable {
$this->action = "RESET_AUTO_INCREMENT";
return $this;
}
public function getAction(): string { return $this->action; }
public function getColumn(): ?Column { return $this->column; }
public function getConstraint(): ?Constraint { return $this->constraint; }
@@ -65,6 +70,10 @@ class AlterTable extends Query {
$column = $this->getColumn();
$constraint = $this->getConstraint();
if ($action === "RESET_AUTO_INCREMENT") {
return "ALTER TABLE $tableName AUTO_INCREMENT=1";
}
$query = "ALTER TABLE $tableName $action ";
if ($column) {

View File

@@ -23,6 +23,7 @@ class Select extends Query {
$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();
$this->groupColumns = array();
@@ -41,6 +42,11 @@ class Select extends Query {
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): Select {
$this->joins[] = new Join("INNER", $table, $columnA, $columnB, $tableAlias);
return $this;
@@ -94,6 +100,7 @@ class Select extends Query {
public function getLimit(): int { return $this->limit; }
public function getOffset(): int { return $this->offset; }
public function getGroupBy(): array { return $this->groupColumns; }
public function getHavings(): array { return $this->havings; }
public function build(array &$params): ?string {
@@ -101,6 +108,17 @@ class Select extends Query {
foreach ($this->selectValues as $value) {
if (is_string($value)) {
$selectValues[] = $this->sql->columnName($value);
} else if ($value instanceof Select) {
$subSelect = $value->build($params);
if (count($value->getSelectValues()) !== 1) {
$selectValues[] = "($subSelect)";
} else {
$columnName = $value->getSelectValues()[0];
if(($index = stripos($columnName, " as ")) !== FALSE) {
$columnName = substr($columnName, $index + 4);
}
$selectValues[] = "($subSelect) as $columnName";
}
} else {
$selectValues[] = $this->sql->addValue($value, $params);
}
@@ -115,6 +133,10 @@ class Select extends Query {
$tables = $this->sql->tableName($tables);
$where = $this->sql->getWhereClause($this->getConditions(), $params);
$havingClause = "";
if (count($this->havings) > 0) {
$havingClause = " HAVING " . $this->sql->buildCondition($this->getHavings(), $params);
}
$joinStr = "";
$joins = $this->getJoins();
@@ -145,6 +167,6 @@ class Select extends Query {
$limit = ($this->getLimit() > 0 ? (" LIMIT " . $this->getLimit()) : "");
$offset = ($this->getOffset() > 0 ? (" OFFSET " . $this->getOffset()) : "");
return "SELECT $selectValues FROM $tables$joinStr$where$groupBy$orderBy$limit$offset";
return "SELECT $selectValues FROM $tables$joinStr$where$groupBy$havingClause$orderBy$limit$offset";
}
}

View File

@@ -4,6 +4,7 @@ namespace Driver\SQL;
use Driver\SQL\Column\Column;
use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondAnd;
use Driver\SQL\Condition\CondBool;
use Driver\SQL\Condition\CondIn;
use Driver\SQL\Condition\Condition;
@@ -11,6 +12,7 @@ use Driver\SQL\Condition\CondKeyword;
use Driver\SQL\Condition\CondNot;
use Driver\Sql\Condition\CondNull;
use Driver\SQL\Condition\CondOr;
use Driver\SQL\Condition\Exists;
use Driver\SQL\Constraint\Constraint;
use \Driver\SQL\Constraint\Unique;
use \Driver\SQL\Constraint\PrimaryKey;
@@ -234,7 +236,7 @@ abstract class SQL {
// Statements
protected abstract function execute($query, $values=NULL, $returnValues=false);
protected function buildCondition($condition, &$params) {
public function buildCondition($condition, &$params) {
if ($condition instanceof CondOr) {
$conditions = array();
@@ -242,6 +244,12 @@ abstract class SQL {
$conditions[] = $this->buildCondition($cond, $params);
}
return "(" . implode(" OR ", $conditions) . ")";
} else if ($condition instanceof CondAnd) {
$conditions = array();
foreach($condition->getConditions() as $cond) {
$conditions[] = $this->buildCondition($cond, $params);
}
return "(" . implode(" AND ", $conditions) . ")";
} else if ($condition instanceof Compare) {
$column = $this->columnName($condition->getColumn());
$value = $condition->getValue();
@@ -302,8 +310,10 @@ abstract class SQL {
}
return "NOT $expression";
} else if($condition instanceof CondNull) {
return $this->columnName($condition->getColumn()) . " IS NULL";
} else if ($condition instanceof CondNull) {
return $this->columnName($condition->getColumn()) . " IS NULL";
} else if ($condition instanceof Exists) {
return "EXISTS(" .$condition->getSubQuery()->build($params) . ")";
} else {
$this->lastError = "Unsupported condition type: " . get_class($condition);
return null;