Abstract implementations

This commit is contained in:
Juan Pablo Vial
2022-09-08 17:40:51 -04:00
parent e83638e223
commit a2d560ab1d
5 changed files with 492 additions and 0 deletions

View File

@ -0,0 +1,12 @@
<?php
namespace ProVM\Alias\Database;
use ProVM\Concept\Database\Query as QueryInterface;
abstract class Query implements QueryInterface
{
public function __toString(): string
{
return $this->build();
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace ProVM\Alias\Database\Query;
use ProVM\Alias\Database\Query;
use ProVM\Concept\Database\Query\Delete as DeleteInterface;
abstract class Delete extends Query implements DeleteInterface
{
public function from(string $table): DeleteInterface
{
return $this->setTable($table);
}
public function where(array $conditions): DeleteInterface
{
return $this->setConditions($conditions);
}
protected string $table;
public function setTable(string $table): DeleteInterface
{
$this->table = $table;
return $this;
}
public function getTable(): string
{
return $this->table;
}
protected array $conditions;
public function setConditions(array $conditions): DeleteInterface
{
foreach ($conditions as $condition) {
$this->addCondition($condition);
}
return $this;
}
public function addCondition(string $expression): DeleteInterface
{
$this->conditions []= $expression;
return $this;
}
public function getConditions(): array
{
return $this->conditions;
}
public function getConditionString(): string
{
return implode(' ', $this->getConditions());
}
public function build(): string
{
return implode(' ', [
'DELETE FROM',
$this->getTable(),
'WHERE',
$this->getConditionString()
]);
}
}

View File

@ -0,0 +1,108 @@
<?php
namespace ProVM\Alias\Database\Query;
use ProVM\Alias\Database\Query;
use ProVM\Concept\Database\Query\Insert as InsertInterface;
use ProVM\Concept\Database\Query\Select;
abstract class Insert extends Query implements InsertInterface
{
public function into(string $table): InsertInterface
{
return $this->setTable($table);
}
public function columns(array $columns): InsertInterface
{
return $this->setColumns($columns);
}
public function values(array $values): InsertInterface
{
return $this->setValues($values);
}
public function select(Select $select): InsertInterface
{
return $this->setSelect($select);
}
protected string $table;
public function setTable(string $table): InsertInterface
{
$this->table = $table;
return $this;
}
public function getTable(): string
{
return $this->table;
}
protected array $columns;
public function setColumns(array $columns): InsertInterface
{
foreach ($columns as $column) {
$this->addColumn($column);
}
return $this;
}
public function addColumn(string $column): InsertInterface
{
$this->columns []= $column;
return $this;
}
public function getColumns(): array
{
return $this->columns;
}
public function getColumnString(): string
{
return implode(', ', $this->getColumns());
}
protected array $values;
public function setValues(array $values): InsertInterface
{
foreach ($values as $value) {
$this->addValue($value);
}
return $this;
}
public function addValue(int|string $value): InsertInterface
{
if (!is_numeric($value)) {
$value = "'{$value}'";
}
$this->values []= $value;
return $this;
}
public function getValues(): array
{
return $this->values;
}
public function getValueString(): string
{
return implode(', ', $this->getValues());
}
protected Select $select;
public function setSelect(Select $select): InsertInterface
{
$this->select = $select;
return $this;
}
public function getSelect(): Select
{
return $this->select;
}
public function build(): string
{
$query = ["INSERT INTO"];
$query []= $this->getTable();
if (isset($this->columns)) {
$query []= "({$this->getColumnString()})";
}
if (isset($this->select)) {
$query []= "{$this->getSelect()}";
return implode(' ', $query);
}
$query []= 'VALUES';
$query []= "({$this->getValueString()})";
return implode(' ', $query);
}
}

View File

@ -0,0 +1,222 @@
<?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);
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace ProVM\Alias\Database\Query;
use ProVM\Alias\Database\Query;
use ProVM\Concept\Database\Query\Update as UpdateInterface;
abstract class Update extends Query implements UpdateInterface
{
public function table(string $table): UpdateInterface
{
return $this->setTable($table);
}
public function set(array $value_pairs): UpdateInterface
{
return $this->setValues($value_pairs);
}
public function where(array $conditions): UpdateInterface
{
return $this->setConditions($conditions);
}
protected string $table;
public function setTable(string $table): UpdateInterface
{
$this->table = $table;
return $this;
}
public function getTable(): string
{
return $this->table;
}
protected array $values;
public function setValues(array $values): UpdateInterface
{
foreach ($values as $value) {
$column = $value['column'] ?? $value[0];
$val = $value['value'] ?? $value[1];
$this->addValue($column, $val);
}
return $this;
}
public function addValue(string $column, int|string $value): UpdateInterface
{
if (!is_numeric($value)) {
$value = "'{$value}'";
}
$this->values []= "`{$column}` = {$value}";
return $this;
}
public function getValues(): array
{
return $this->values;
}
public function getValueString(): string
{
return implode(', ', $this->getValues());
}
protected array $conditions;
public function setConditions(array $conditions): UpdateInterface
{
foreach ($conditions as $condition) {
$this->addCondition($condition);
}
return $this;
}
public function addCondition(string $expression): UpdateInterface
{
$this->conditions []= $expression;
return $this;
}
public function getConditions(): array
{
return $this->conditions;
}
public function getConditionString(): string
{
return implode(' ', $this->getConditions());
}
public function build(): string
{
return implode(' ', [
'UPDATE',
$this->getTable(),
'SET',
$this->getValueString(),
'WHERE',
$this->getConditionString()
]);
}
}