web-base/Core/Driver/SQL/Query/Select.class.php

214 lines
6.5 KiB
PHP
Raw Normal View History

2020-04-02 00:02:51 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Driver\SQL\Query;
2020-04-02 00:02:51 +02:00
2022-11-18 18:06:46 +01:00
use Core\Driver\SQL\Condition\CondOr;
use Core\Driver\SQL\Expression\JsonArrayAgg;
use Core\Driver\SQL\Join;
use Core\Driver\SQL\SQL;
2020-04-03 17:39:58 +02:00
2020-04-02 00:02:51 +02:00
class Select extends Query {
private array $selectValues;
2020-04-03 17:39:58 +02:00
private array $tables;
private array $conditions;
private array $joins;
private array $orderColumns;
2020-06-15 21:14:59 +02:00
private array $groupColumns;
2022-06-08 18:37:08 +02:00
private array $havings;
2020-04-03 17:39:58 +02:00
private bool $sortAscending;
private int $limit;
private int $offset;
2022-03-08 11:50:18 +01:00
private bool $forUpdate;
2022-06-08 18:37:08 +02:00
private int $fetchType;
2020-04-02 00:02:51 +02:00
public function __construct($sql, ...$selectValues) {
2020-04-02 00:02:51 +02:00
parent::__construct($sql);
$this->selectValues = (!empty($selectValues) && is_array($selectValues[0])) ? $selectValues[0] : $selectValues;
2020-04-02 00:02:51 +02:00
$this->tables = array();
$this->conditions = array();
2021-11-11 14:25:26 +01:00
$this->havings = array();
2020-04-02 00:02:51 +02:00
$this->joins = array();
2020-04-03 14:46:29 +02:00
$this->orderColumns = array();
2020-06-15 21:14:59 +02:00
$this->groupColumns = array();
2020-04-03 14:46:29 +02:00
$this->limit = 0;
$this->offset = 0;
$this->sortAscending = true;
2022-03-08 11:50:18 +01:00
$this->forUpdate = false;
2022-06-08 18:37:08 +02:00
$this->fetchType = SQL::FETCH_ALL;
2020-04-02 00:02:51 +02:00
}
2021-04-02 21:58:06 +02:00
public function from(...$tables): Select {
2020-04-02 00:02:51 +02:00
$this->tables = array_merge($this->tables, $tables);
return $this;
}
2022-06-20 19:52:31 +02:00
public function addValue($value): Select {
$this->selectValues[] = $value;
return $this;
}
2021-04-02 21:58:06 +02:00
public function where(...$conditions): Select {
2020-04-04 01:15:59 +02:00
$this->conditions[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
2020-04-02 00:02:51 +02:00
return $this;
}
2021-11-11 14:25:26 +01:00
public function having(...$conditions): Select {
$this->havings[] = (count($conditions) === 1 ? $conditions : new CondOr($conditions));
return $this;
}
2022-02-20 16:53:26 +01:00
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);
2020-04-02 00:02:51 +02:00
return $this;
}
2022-02-20 16:53:26 +01:00
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);
2020-04-02 00:02:51 +02:00
return $this;
}
2022-06-20 19:52:31 +02:00
public function addJoin(Join $join): Select {
$this->joins[] = $join;
return $this;
}
2021-04-02 21:58:06 +02:00
public function groupBy(...$columns): Select {
2020-06-15 21:14:59 +02:00
$this->groupColumns = $columns;
return $this;
}
2021-04-02 21:58:06 +02:00
public function orderBy(...$columns): Select {
2020-04-03 14:46:29 +02:00
$this->orderColumns = $columns;
return $this;
}
2021-04-02 21:58:06 +02:00
public function ascending(): Select {
2020-04-03 17:39:58 +02:00
$this->sortAscending = true;
2020-04-03 14:46:29 +02:00
return $this;
}
2021-04-02 21:58:06 +02:00
public function descending(): Select {
2020-04-03 17:39:58 +02:00
$this->sortAscending = false;
2020-04-03 14:46:29 +02:00
return $this;
}
2021-04-02 21:58:06 +02:00
public function limit(int $limit): Select {
2020-04-03 14:46:29 +02:00
$this->limit = $limit;
return $this;
}
2021-04-02 21:58:06 +02:00
public function offset(int $offset): Select {
2020-04-03 14:46:29 +02:00
$this->offset = $offset;
return $this;
}
2022-03-08 11:50:18 +01:00
public function lockForUpdate(): Select {
$this->forUpdate = true;
return $this;
}
2022-06-08 18:37:08 +02:00
public function iterator(): Select {
$this->fetchType = SQL::FETCH_ITERATIVE;
return $this;
}
public function first(): Select {
$this->fetchType = SQL::FETCH_ONE;
$this->limit = 1;
return $this;
}
/**
* @return mixed
*/
2021-04-02 22:41:24 +02:00
public function execute() {
2022-06-08 18:37:08 +02:00
return $this->sql->executeQuery($this, $this->fetchType);
2020-04-02 00:02:51 +02:00
}
public function getSelectValues(): array { return $this->selectValues; }
2021-04-02 21:58:06 +02:00
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; }
public function getLimit(): int { return $this->limit; }
public function getOffset(): int { return $this->offset; }
public function getGroupBy(): array { return $this->groupColumns; }
2021-11-11 14:25:26 +01:00
public function getHavings(): array { return $this->havings; }
2020-04-03 14:46:29 +02:00
2021-04-09 16:05:36 +02:00
public function build(array &$params): ?string {
$selectValues = [];
foreach ($this->selectValues as $value) {
if (is_string($value)) {
$selectValues[] = $this->sql->columnName($value);
2021-11-11 14:25:26 +01:00
} else if ($value instanceof Select) {
$subSelect = $value->build($params);
if (count($value->getSelectValues()) !== 1) {
$selectValues[] = "($subSelect)";
} else {
2022-02-20 16:53:26 +01:00
$columnAlias = null;
$subSelectColumn = $value->getSelectValues()[0];
if (is_string($subSelectColumn) && ($index = stripos($subSelectColumn, " as ")) !== FALSE) {
$columnAlias = substr($subSelectColumn, $index + 4);
} else if ($subSelectColumn instanceof JsonArrayAgg) {
$columnAlias = $subSelectColumn->getAlias();
}
if ($columnAlias) {
$selectValues[] = "($subSelect) as $columnAlias";
} else {
$selectValues[] = "($subSelect)";
2021-11-11 14:25:26 +01:00
}
}
} else {
$selectValues[] = $this->sql->addValue($value, $params);
}
}
2021-04-08 18:29:47 +02:00
$tables = $this->getTables();
$selectValues = implode(",", $selectValues);
2021-04-08 18:29:47 +02:00
if (!$tables) {
return "SELECT $selectValues";
2021-04-08 18:29:47 +02:00
}
$tables = $this->sql->tableName($tables);
$where = $this->sql->getWhereClause($this->getConditions(), $params);
2021-11-11 14:25:26 +01:00
$havingClause = "";
if (count($this->havings) > 0) {
$havingClause = " HAVING " . $this->sql->buildCondition($this->getHavings(), $params);
}
2021-04-08 18:29:47 +02:00
$joinStr = "";
$joins = $this->getJoins();
if (!empty($joins)) {
foreach ($joins as $join) {
$type = $join->getType();
$joinTable = $this->sql->tableName($join->getTable());
$tableAlias = ($join->getTableAlias() ? " " . $join->getTableAlias() : "");
2022-02-20 16:53:26 +01:00
$condition = $this->sql->buildCondition($join->getConditions(), $params);
$joinStr .= " $type JOIN $joinTable$tableAlias ON ($condition)";
2021-04-08 18:29:47 +02:00
}
}
$groupBy = "";
$groupColumns = $this->getGroupBy();
if (!empty($groupColumns)) {
$groupBy = " GROUP BY " . $this->sql->columnName($groupColumns);
}
$orderBy = "";
$orderColumns = $this->getOrderBy();
if (!empty($orderColumns)) {
$orderBy = " ORDER BY " . $this->sql->columnName($orderColumns);
$orderBy .= ($this->isOrderedAscending() ? " ASC" : " DESC");
}
$limit = ($this->getLimit() > 0 ? (" LIMIT " . $this->getLimit()) : "");
$offset = ($this->getOffset() > 0 ? (" OFFSET " . $this->getOffset()) : "");
2022-03-08 11:50:18 +01:00
$forUpdate = ($this->forUpdate ? " FOR UPDATE" : "");
return "SELECT $selectValues FROM $tables$joinStr$where$groupBy$havingClause$orderBy$limit$offset$forUpdate";
2021-04-08 18:29:47 +02:00
}
2020-04-03 17:39:58 +02:00
}