223 lines
6.1 KiB
PHP
223 lines
6.1 KiB
PHP
<?php
|
|
namespace ProVM\Alias\Database\Query;
|
|
|
|
use ProVM\Alias\Database\Query;
|
|
use ProVM\Concept\Database\Query\Select as SelectInterface;
|
|
|
|
abstract class Select extends Query implements SelectInterface
|
|
{
|
|
public function select(array $columns = ['*']): SelectInterface
|
|
{
|
|
return $this->setColumns($columns);
|
|
}
|
|
public function from(string $table): SelectInterface
|
|
{
|
|
return $this->setTable($table);
|
|
}
|
|
public function joins(array $joins): SelectInterface
|
|
{
|
|
return $this->setJoins($joins);
|
|
}
|
|
public function where(array $conditions): SelectInterface
|
|
{
|
|
return $this->setConditions($conditions);
|
|
}
|
|
public function groupBy(array $grouping): SelectInterface
|
|
{
|
|
return $this->setGroups($grouping);
|
|
}
|
|
public function having(array $having): SelectInterface
|
|
{
|
|
return $this->setHaving($having);
|
|
}
|
|
public function orderBy(array $ordering): SelectInterface
|
|
{
|
|
return $this->setOrders($ordering);
|
|
}
|
|
|
|
protected array $columns;
|
|
public function setColumns(array $columns): SelectInterface
|
|
{
|
|
foreach ($columns as $column) {
|
|
$col = $column;
|
|
$alias = null;
|
|
if (is_array($column)) {
|
|
$col = $column['column'] ?? $column[0];
|
|
$alias = $column['alias'] ?? $column[1];
|
|
}
|
|
$this->addColumn($column, $alias);
|
|
}
|
|
return $this;
|
|
}
|
|
public function addColumn(string $column, ?string $alias = null): SelectInterface
|
|
{
|
|
$a = '';
|
|
if ($alias !== null) {
|
|
$a = " AS '{$alias}'";
|
|
}
|
|
$this->columns[] = "`{$column}`{$a}";
|
|
return $this;
|
|
}
|
|
public function getColumns(): array
|
|
{
|
|
return $this->columns;
|
|
}
|
|
public function getColumnString(): string
|
|
{
|
|
return implode(', ', $this->getColumns());
|
|
}
|
|
protected string $table;
|
|
public function setTable(string $table, ?string $alias = null): SelectInterface
|
|
{
|
|
$table = "`{$table}`";
|
|
if ($alias !== null) {
|
|
$table = "{$table} '{$alias}'";
|
|
}
|
|
$this->table = $table;
|
|
return $this;
|
|
}
|
|
public function getTable(): string
|
|
{
|
|
return $this->table;
|
|
}
|
|
protected array $joins;
|
|
public function setJoins(array $joins): SelectInterface
|
|
{
|
|
foreach ($joins as $join) {
|
|
$table = $join['table'] ?? $join[0];
|
|
$expression = $join['expression'] ?? $join[1];
|
|
$this->addJoin($table, $expression);
|
|
}
|
|
return $this;
|
|
}
|
|
public function addJoin(string $table, string $expression): SelectInterface
|
|
{
|
|
$this->joins []= "{$table} ON {$expression}";
|
|
return $this;
|
|
}
|
|
public function getJoins(): array
|
|
{
|
|
return $this->joins;
|
|
}
|
|
public function getJoinString(): string
|
|
{
|
|
return implode(' ', $this->getJoins());
|
|
}
|
|
protected array $conditions;
|
|
public function setConditions(array $conditions): SelectInterface
|
|
{
|
|
foreach ($conditions as $condition) {
|
|
$this->addCondition($condition);
|
|
}
|
|
return $this;
|
|
}
|
|
public function addCondition(string $expression): SelectInterface
|
|
{
|
|
$this->conditions []= $expression;
|
|
return $this;
|
|
}
|
|
public function getConditions(): array
|
|
{
|
|
return $this->conditions;
|
|
}
|
|
public function getConditionString(): string
|
|
{
|
|
return implode(' ', $this->getConditions());
|
|
}
|
|
protected array $groups;
|
|
public function setGroups(array $groups): SelectInterface
|
|
{
|
|
foreach ($groups as $group) {
|
|
$this->addGroup($group);
|
|
}
|
|
return $this;
|
|
}
|
|
public function addGroup(string $group): SelectInterface
|
|
{
|
|
$this->groups []= "`{$group}`";
|
|
return $this;
|
|
}
|
|
public function getGroups(): array
|
|
{
|
|
return $this->groups;
|
|
}
|
|
public function getGroupString(): string
|
|
{
|
|
return implode(', ', $this->getGroups());
|
|
}
|
|
protected array $having;
|
|
public function setHaving(array $having): SelectInterface
|
|
{
|
|
foreach ($having as $item) {
|
|
$this->addHaving($item);
|
|
}
|
|
return $this;
|
|
}
|
|
public function addHaving(string $having): SelectInterface
|
|
{
|
|
$this->having []= $having;
|
|
return $this;
|
|
}
|
|
public function getHaving(): array
|
|
{
|
|
return $this->having;
|
|
}
|
|
public function getHavingString(): string
|
|
{
|
|
return implode(', ', $this->getHaving());
|
|
}
|
|
protected array $orders;
|
|
public function setOrders(array $orders): SelectInterface
|
|
{
|
|
foreach ($orders as $order) {
|
|
$column = $order;
|
|
$direction = null;
|
|
if (is_array($order)) {
|
|
$direction = $order['direction'] ?? $order[1];
|
|
$column = $order['column'] ?? $order[0];
|
|
}
|
|
$this->addOrder($column, $direction);
|
|
}
|
|
return $this;
|
|
}
|
|
public function addOrder(string $column, ?string $direction = null): SelectInterface
|
|
{
|
|
if ($direction === null) {
|
|
$direction = 'ASC';
|
|
}
|
|
$this->orders []= "{$column} {$direction}";
|
|
return $this;
|
|
}
|
|
public function getOrders(): array
|
|
{
|
|
return $this->orders;
|
|
}
|
|
public function getOrderString(): string
|
|
{
|
|
return implode(', ', $this->getOrders());
|
|
}
|
|
|
|
public function build(): string
|
|
{
|
|
$query = ["SELECT"];
|
|
$query []= $this->getColumnString();
|
|
$query []= "FROM {$this->getTable()}";
|
|
if (isset($this->joins)) {
|
|
$query []= $this->getJoinString();
|
|
}
|
|
if (isset($this->conditions)) {
|
|
$query []= "WHERE {$this->getConditionString()}";
|
|
}
|
|
if (isset($this->groups)) {
|
|
$query []= "GROUP BY {$this->getGroupString()}";
|
|
}
|
|
if (isset($this->having)) {
|
|
$query []= "HAVING {$this->getHavingString()}";
|
|
}
|
|
if (isset($this->orders)) {
|
|
$query []= "ORDER BY {$this->getOrderString()}";
|
|
}
|
|
return implode(' ', $query);
|
|
}
|
|
}
|