Compare commits
13 Commits
1.0.1
...
e6759bcf06
Author | SHA1 | Date | |
---|---|---|---|
e6759bcf06 | |||
3944fab3f1 | |||
e64ea2709c | |||
d266008224 | |||
849d42ea57 | |||
fc372b56c1 | |||
3b1902ed1a | |||
e7ab3fb8b7 | |||
d4fecf977d | |||
a22a927bb4 | |||
758a4b7c7f | |||
2fd0af5c2f | |||
97d5ae8450 |
@ -1,9 +1,9 @@
|
||||
{
|
||||
"name": "provm/query_builder",
|
||||
"version": "3.0.0",
|
||||
"type": "project",
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.5",
|
||||
"kint-php/kint": "^4.2"
|
||||
"phpunit/phpunit": "^10"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Alias\Database;
|
||||
|
||||
use ProVM\Concept\Database\Query as QueryInterface;
|
||||
|
||||
abstract class Query implements QueryInterface
|
||||
{
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->build();
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
<?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()
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
@ -1,222 +0,0 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
<?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()
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Database;
|
||||
|
||||
interface Query
|
||||
use Stringable;
|
||||
|
||||
interface Query extends Stringable
|
||||
{
|
||||
public function build(): string;
|
||||
public function __toString(): string;
|
||||
}
|
||||
|
14
src/Concept/Database/Query/Builder.php
Normal file
14
src/Concept/Database/Query/Builder.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Database\Query;
|
||||
|
||||
interface Builder
|
||||
{
|
||||
public function select(array|string $columns = '*'): Select;
|
||||
public function insert(?string $table = null): Insert;
|
||||
public function update(?string $table = null): Update;
|
||||
public function delete(?string $table = null): Delete;
|
||||
|
||||
public function create(?string $table = null): Create;
|
||||
public function truncate(?string $table = null): Truncate;
|
||||
public function drop(?string $table = null): Drop;
|
||||
}
|
11
src/Concept/Database/Query/Create.php
Normal file
11
src/Concept/Database/Query/Create.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Database\Query;
|
||||
|
||||
use ProVM\Concept\Database\Query;
|
||||
|
||||
interface Create extends Query
|
||||
{
|
||||
public function table(string $table): self;
|
||||
public function columns(array|string $columns): self;
|
||||
public function foreign(array|string $columnDefinition): self;
|
||||
}
|
@ -6,5 +6,5 @@ use ProVM\Concept\Database\Query;
|
||||
interface Delete extends Query
|
||||
{
|
||||
public function from(string $table): Delete;
|
||||
public function where(array $conditions): Delete;
|
||||
public function where(array|string $conditions): Delete;
|
||||
}
|
||||
|
9
src/Concept/Database/Query/Drop.php
Normal file
9
src/Concept/Database/Query/Drop.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Database\Query;
|
||||
|
||||
use ProVM\Concept\Database\Query;
|
||||
|
||||
interface Drop extends Query
|
||||
{
|
||||
public function table(string $table): Drop;
|
||||
}
|
@ -6,7 +6,7 @@ use ProVM\Concept\Database\Query;
|
||||
interface Insert extends Query
|
||||
{
|
||||
public function into(string $table): Insert;
|
||||
public function columns(array $columns): Insert;
|
||||
public function values(array $values): Insert;
|
||||
public function select(Select $select): Insert;
|
||||
public function columns(array|string $columns): Insert;
|
||||
public function values(array|string $values): Insert;
|
||||
public function select(Select|string $select): Insert;
|
||||
}
|
||||
|
@ -5,11 +5,11 @@ use ProVM\Concept\Database\Query;
|
||||
|
||||
interface Select extends Query
|
||||
{
|
||||
public function select(array $columns = ['*']): Select;
|
||||
public function columns(array|string $columns = '*'): Select;
|
||||
public function from(string $table): Select;
|
||||
public function joins(array $joins): Select;
|
||||
public function where(array $conditions): Select;
|
||||
public function groupBy(array $grouping): Select;
|
||||
public function having(array $having): Select;
|
||||
public function orderBy(array $ordering): Select;
|
||||
public function joined(array|string $joins): Select;
|
||||
public function where(array|string $conditions): Select;
|
||||
public function groupBy(array|string $grouping): Select;
|
||||
public function having(array|string $having): Select;
|
||||
public function orderBy(array|string $ordering): Select;
|
||||
}
|
||||
|
9
src/Concept/Database/Query/Truncate.php
Normal file
9
src/Concept/Database/Query/Truncate.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Database\Query;
|
||||
|
||||
use ProVM\Concept\Database\Query;
|
||||
|
||||
interface Truncate extends Query
|
||||
{
|
||||
public function table(string $table): Truncate;
|
||||
}
|
@ -6,6 +6,6 @@ use ProVM\Concept\Database\Query;
|
||||
interface Update extends Query
|
||||
{
|
||||
public function table(string $table): Update;
|
||||
public function set(array $value_pairs): Update;
|
||||
public function where(array $conditions): Update;
|
||||
public function set(array|string $value_pairs): Update;
|
||||
public function where(array|string $conditions): Update;
|
||||
}
|
||||
|
@ -1,15 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Database;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Concept\Database\Query\{Delete,Insert,Select,Update};
|
||||
|
||||
interface QueryBuilder
|
||||
{
|
||||
public function setContainer(ContainerInterface $container): QueryBuilder;
|
||||
public function getContainer(): ContainerInterface;
|
||||
public function select(array $columns = ['*']): Select;
|
||||
public function insert(string $table): Insert;
|
||||
public function update(string $table): Update;
|
||||
public function delete(string $table): Delete;
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Alias\Database\Query\Delete as Base;
|
||||
|
||||
class Delete extends Base
|
||||
{}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Alias\Database\Query\Insert as Base;
|
||||
|
||||
class Insert extends Base
|
||||
{}
|
@ -1,7 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Alias\Database\Query\Update as Base;
|
||||
|
||||
class Update extends Base
|
||||
{}
|
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Concept\Database\QueryBuilder as QBInterface;
|
||||
use ProVM\Concept\Database\Query\{Delete,Insert,Select,Update};
|
||||
|
||||
class QueryBuilder implements QBInterface
|
||||
{
|
||||
protected ContainerInterface $container;
|
||||
public function setContainer(ContainerInterface $container): QBInterface
|
||||
{
|
||||
$this->container = $container;
|
||||
return $this;
|
||||
}
|
||||
public function getContainer(): ContainerInterface
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
public function select(array $columns = ['*']): Select
|
||||
{
|
||||
return $this->getContainer()->get(Select::class)->select($columns);
|
||||
}
|
||||
public function insert(string $table): Insert
|
||||
{
|
||||
return $this->getContainer()->get(Insert::class)->into($table);
|
||||
}
|
||||
public function update(string $table): Update
|
||||
{
|
||||
return $this->getContainer()->get(Update::class)->table($table);
|
||||
}
|
||||
public function delete(string $table): Delete
|
||||
{
|
||||
return $this->getContainer()->get(Delete::class)->from($table);
|
||||
}
|
||||
}
|
107
src/Enforce/Database/Query/Builder.php
Normal file
107
src/Enforce/Database/Query/Builder.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query;
|
||||
|
||||
use Exception;
|
||||
use FilesystemIterator;
|
||||
use ProVM\Concept\Database\Query;
|
||||
|
||||
class Builder implements Query\Builder
|
||||
{
|
||||
public function __construct(array $query_classes)
|
||||
{
|
||||
$this->setQueries($query_classes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getClasses(string $databaseType): array
|
||||
{
|
||||
$directories = new FilesystemIterator(__DIR__);
|
||||
foreach ($directories as $directory) {
|
||||
if (!$directory->isDir()) {
|
||||
continue;
|
||||
}
|
||||
if (strtolower($directory->getBasename()) !== strtolower($databaseType)) {
|
||||
continue;
|
||||
}
|
||||
$classes = [];
|
||||
$files = new FilesystemIterator($directory->getPathname());
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir()) {
|
||||
continue;
|
||||
}
|
||||
$base = $file->getBasename('.php');
|
||||
$interface = "ProVM\\Concept\\Database\\Query\\{$base}";
|
||||
$class = __NAMESPACE__ . "\\{$directory->getBasename()}\\{$base}";
|
||||
$classes[$interface] = $class;
|
||||
}
|
||||
return $classes;
|
||||
}
|
||||
throw new Exception("Database type's classes not found");
|
||||
}
|
||||
protected array $query_classes;
|
||||
|
||||
protected function getQueries(): array
|
||||
{
|
||||
return $this->query_classes;
|
||||
}
|
||||
|
||||
protected function get(string $query_interface): string
|
||||
{
|
||||
return $this->query_classes[$query_interface];
|
||||
}
|
||||
|
||||
protected function addQuery(string $query_interface, string $query): Builder
|
||||
{
|
||||
$this->query_classes[$query_interface] = $query;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setQueries(array $queries): Builder
|
||||
{
|
||||
foreach ($queries as $interface => $query) {
|
||||
$this->addQuery($interface, $query);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function build(string $query_interface, ?array $args = null): Query
|
||||
{
|
||||
$class = $this->get($query_interface);
|
||||
if ($args !== null) {
|
||||
return new $class(...$args);
|
||||
}
|
||||
return new $class;
|
||||
}
|
||||
|
||||
public function select(array|string $columns = '*'): Query\Select
|
||||
{
|
||||
return $this->build(Query\Select::class, compact('columns'));
|
||||
}
|
||||
public function insert(?string $table = null): Query\Insert
|
||||
{
|
||||
return $this->build(Query\Insert::class, compact('table'));
|
||||
}
|
||||
public function update(?string $table = null): Query\Update
|
||||
{
|
||||
return $this->build(Query\Update::class, compact('table'));
|
||||
}
|
||||
public function delete(?string $table = null): Query\Delete
|
||||
{
|
||||
return $this->build(Query\Delete::class, compact('table'));
|
||||
}
|
||||
|
||||
public function create(?string $table = null): Query\Create
|
||||
{
|
||||
return $this->build(Query\Create::class, compact('table'));
|
||||
}
|
||||
public function truncate(?string $table = null): Query\Truncate
|
||||
{
|
||||
return $this->build(Query\Truncate::class, compact('table'));
|
||||
}
|
||||
public function drop(?string $table = null): Query\Drop
|
||||
{
|
||||
return $this->build(Query\Drop::class, compact('table'));
|
||||
}
|
||||
}
|
8
src/Enforce/Database/Query/MySQL/Create.php
Normal file
8
src/Enforce/Database/Query/MySQL/Create.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Create extends Query\Create
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/MySQL/Delete.php
Normal file
8
src/Enforce/Database/Query/MySQL/Delete.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Delete extends Query\Delete
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/MySQL/Drop.php
Normal file
8
src/Enforce/Database/Query/MySQL/Drop.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Drop extends Query\Drop
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/MySQL/Insert.php
Normal file
8
src/Enforce/Database/Query/MySQL/Insert.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Insert extends Query\Insert
|
||||
{
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Alias\Database\Query\Select as Base;
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Select extends Base
|
||||
class Select extends Query\Select
|
||||
{
|
||||
public function limit(int $limit, ?int $offset = null): Select
|
||||
{
|
||||
@ -13,38 +13,46 @@ class Select extends Base
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected int $limit;
|
||||
public function setLimit(int $limit): Select
|
||||
{
|
||||
$this->limit = $limit;
|
||||
return $this;
|
||||
}
|
||||
protected int $offset;
|
||||
|
||||
public function getLimit(): int
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
protected int $offset;
|
||||
public function setOffset(int $offset): Select
|
||||
{
|
||||
$this->offset = $offset;
|
||||
return $this;
|
||||
}
|
||||
public function getOffset(): int
|
||||
{
|
||||
return $this->offset;
|
||||
}
|
||||
|
||||
public function setLimit(int $limit): Select
|
||||
{
|
||||
$this->limit = $limit;
|
||||
return $this;
|
||||
}
|
||||
public function setOffset(int $offset): Select
|
||||
{
|
||||
$this->offset = $offset;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getLimitString(): string
|
||||
{
|
||||
return (isset($this->limit)) ? " LIMIT {$this->getLimit()}{$this->getOffsetString()}" : '';
|
||||
}
|
||||
protected function getOffsetString(): string
|
||||
{
|
||||
return (isset($this->offset)) ? " OFFSET {$this->getOffset()}" : '';
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
$query = [parent::build()];
|
||||
if (isset($this->limit)) {
|
||||
$query []= 'LIMIT';
|
||||
$query []= $this->getLimit();
|
||||
if (isset($this->offset)) {
|
||||
$query []= 'OFFSET';
|
||||
$query []= $this->getOffset();
|
||||
}
|
||||
}
|
||||
return implode(' ', $query);
|
||||
return parent::build();
|
||||
/*$query = [
|
||||
parent::build(),
|
||||
$this->getLimitString()
|
||||
];
|
||||
return implode('', $query);*/
|
||||
}
|
||||
}
|
8
src/Enforce/Database/Query/MySQL/Truncate.php
Normal file
8
src/Enforce/Database/Query/MySQL/Truncate.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Truncate extends Query\Truncate
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/MySQL/Update.php
Normal file
8
src/Enforce/Database/Query/MySQL/Update.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Update extends Query\Update
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/PostgreSQL/Create.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Create.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Create extends Query\Create
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/PostgreSQL/Delete.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Delete.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Delete extends Query\Delete
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/PostgreSQL/Drop.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Drop.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Drop extends Query\Drop
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/PostgreSQL/Insert.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Insert.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Insert extends Query\Insert
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/PostgreSQL/Select.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Select.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Select extends Query\Select
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/PostgreSQL/Truncate.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Truncate.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Truncate extends Query\Truncate
|
||||
{
|
||||
}
|
8
src/Enforce/Database/Query/PostgreSQL/Update.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Update.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
|
||||
class Update extends Query\Update
|
||||
{
|
||||
}
|
17
src/Ideal/Database/Query.php
Normal file
17
src/Ideal/Database/Query.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace ProVM\Ideal\Database;
|
||||
|
||||
use ProVM\Concept\Database;
|
||||
|
||||
abstract class Query implements Database\Query
|
||||
{
|
||||
public function getType(): string
|
||||
{
|
||||
$class = explode("\\", get_class($this));
|
||||
return array_pop($class);
|
||||
}
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->build();
|
||||
}
|
||||
}
|
132
src/Ideal/Database/Query/Create.php
Normal file
132
src/Ideal/Database/Query/Create.php
Normal file
@ -0,0 +1,132 @@
|
||||
<?php
|
||||
namespace ProVM\Ideal\Database\Query;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Reinforce\Database\Query\hasTable;
|
||||
|
||||
abstract class Create extends Query implements Database\Query\Create
|
||||
{
|
||||
use hasTable;
|
||||
|
||||
public function __construct(?string $table = null)
|
||||
{
|
||||
if ($table !== null) {
|
||||
$this->table($table);
|
||||
}
|
||||
}
|
||||
|
||||
public function isTemp(): Database\Query\Create
|
||||
{
|
||||
$this->temp = true;
|
||||
return $this;
|
||||
}
|
||||
public function ifNotExists(): Database\Query\Create
|
||||
{
|
||||
$this->exists = true;
|
||||
return $this;
|
||||
}
|
||||
public function table(string $table): Database\Query\Create
|
||||
{
|
||||
return $this->setTable($table);
|
||||
}
|
||||
public function columns(array|string $columns): Database\Query\Create
|
||||
{
|
||||
return $this->setColumns($columns);
|
||||
}
|
||||
public function foreign(array|string $columnDefinition): Database\Query\Create
|
||||
{
|
||||
return $this->setForeign($columnDefinition);
|
||||
}
|
||||
|
||||
protected bool $temp = false;
|
||||
protected bool $exists = false;
|
||||
protected array|string $columns;
|
||||
protected array|string $foreign;
|
||||
|
||||
public function getColumns(): array
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
|
||||
public function addColumn(string $column): Database\Query\Create
|
||||
{
|
||||
$this->columns []= $column;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setColumns(array|string $columns): Database\Query\Create
|
||||
{
|
||||
if (is_string($columns)) {
|
||||
$this->addColumn($columns);
|
||||
return $this;
|
||||
}
|
||||
foreach ($columns as $column) {
|
||||
$this->addColumn($column);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function getForegin(): array
|
||||
{
|
||||
return $this->foreign;
|
||||
}
|
||||
public function addForeign(string|array $definition): Database\Query\Create
|
||||
{
|
||||
if (is_string($definition)) {
|
||||
$this->foreign []= $definition;
|
||||
return $this;
|
||||
}
|
||||
$ids = implode(', ', $definition['ids']);
|
||||
$columns = $definition['columns'];
|
||||
if (is_array($definition['columns'])) {
|
||||
$columns = implode(', ', $definition['columns']);
|
||||
}
|
||||
$delete = $definition['delete'] ?? 'CASCADE';
|
||||
$update = $definition['update'] ?? 'CASCADE';
|
||||
$fk = implode('_', ['fk', str_replace('.', '_', $definition['table']), $ids]);
|
||||
$this->foreign []= "CONSTRAINT {$fk} FOREIGN KEY ({$columns}) REFERENCES {$definition['table']} ({$ids}) ON DELETE {$delete} ON UPDATE {$update}";
|
||||
return $this;
|
||||
}
|
||||
public function setForeign(array|string $columnDefinition): Database\Query\Create
|
||||
{
|
||||
if (is_string($columnDefinition)) {
|
||||
$this->addForeign($columnDefinition);
|
||||
return $this;
|
||||
}
|
||||
foreach ($columnDefinition as $column) {
|
||||
$this->addForeign($column);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getTempString(): string
|
||||
{
|
||||
return ($this->temp) ? ' TEMPORARY' : '';
|
||||
}
|
||||
protected function getIfNotExistsString(): string
|
||||
{
|
||||
return ($this->exists) ? ' IF NOT EXISTS' : '';
|
||||
}
|
||||
protected function getColumnsString(): string
|
||||
{
|
||||
if (is_string($this->columns)) {
|
||||
return $this->columns;
|
||||
}
|
||||
return implode(', ', $this->getColumns());
|
||||
}
|
||||
protected function getForeignString(): string
|
||||
{
|
||||
if (!isset($this->foreign)) {
|
||||
return '';
|
||||
}
|
||||
if (is_string($this->foreign)) {
|
||||
return ", {$this->foreign}";
|
||||
}
|
||||
return ', ' . implode(', ', $this->getForegin());
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
return "CREATE{$this->getTempString()} TABLE{$this->getIfNotExistsString()} {$this->getTable()} ({$this->getColumnsString()}{$this->getForeignString()})";
|
||||
}
|
||||
}
|
34
src/Ideal/Database/Query/Delete.php
Normal file
34
src/Ideal/Database/Query/Delete.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace ProVM\Ideal\Database\Query;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Reinforce\Database\Query\{hasConditions, hasTable};
|
||||
|
||||
abstract class Delete extends Query implements Database\Query\Delete
|
||||
{
|
||||
use hasTable, hasConditions;
|
||||
|
||||
public function __construct(?string $table = null)
|
||||
{
|
||||
if ($table !== null) {
|
||||
$this->from($table);
|
||||
}
|
||||
}
|
||||
public function from(string $table): Database\Query\Delete
|
||||
{
|
||||
return $this->setTable($table);
|
||||
}
|
||||
public function where(array|string $conditions): Database\Query\Delete
|
||||
{
|
||||
return $this->setConditions($conditions);
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
return implode('', [
|
||||
"DELETE FROM {$this->getTable()}",
|
||||
$this->getConditionsString()
|
||||
]);
|
||||
}
|
||||
}
|
37
src/Ideal/Database/Query/Drop.php
Normal file
37
src/Ideal/Database/Query/Drop.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
namespace ProVM\Ideal\Database\Query;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Reinforce\Database\Query\hasTable;
|
||||
|
||||
abstract class Drop extends Query implements Database\Query\Drop
|
||||
{
|
||||
use hasTable;
|
||||
|
||||
public function __construct(?string $table = null)
|
||||
{
|
||||
if ($table !== null) {
|
||||
$this->table($table);
|
||||
}
|
||||
}
|
||||
public function table(string $table): Database\Query\Drop
|
||||
{
|
||||
return $this->setTable($table);
|
||||
}
|
||||
public function cascade(): Database\Query\Drop
|
||||
{
|
||||
$this->cascade = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected bool $cascade = false;
|
||||
protected function getCascadeString(): string
|
||||
{
|
||||
return $this->cascade ? ' CASCADE' : '';
|
||||
}
|
||||
public function build(): string
|
||||
{
|
||||
return "DROP TABLE {$this->getTable()}{$this->getCascadeString()}";
|
||||
}
|
||||
}
|
102
src/Ideal/Database/Query/Insert.php
Normal file
102
src/Ideal/Database/Query/Insert.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
namespace ProVM\Ideal\Database\Query;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Reinforce\Database\Query\hasTable;
|
||||
|
||||
abstract class Insert extends Query implements Database\Query\Insert
|
||||
{
|
||||
use hasTable;
|
||||
|
||||
public function __construct(?string $table = null)
|
||||
{
|
||||
if ($table !== null) {
|
||||
$this->into($table);
|
||||
}
|
||||
}
|
||||
|
||||
public function into(string $table): Database\Query\Insert
|
||||
{
|
||||
return $this->setTable($table);
|
||||
}
|
||||
public function columns(array|string $columns): Database\Query\Insert
|
||||
{
|
||||
return $this->setColumns($columns);
|
||||
}
|
||||
public function values(array|string $values): Database\Query\Insert
|
||||
{
|
||||
return $this->setValues($values);
|
||||
}
|
||||
public function select(Database\Query\Select|string $select): Database\Query\Insert
|
||||
{
|
||||
return $this->setSelect($select);
|
||||
}
|
||||
|
||||
protected array|string $columns;
|
||||
protected array|string $values;
|
||||
protected Database\Query\Select|string $select;
|
||||
|
||||
public function getColumns(): array|string
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
public function getValues(): array|string
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
public function getSelect(): Database\Query\Select|string
|
||||
{
|
||||
return $this->select;
|
||||
}
|
||||
|
||||
public function setColumns(array|string $columns): Database\Query\Insert
|
||||
{
|
||||
$this->columns = $columns;
|
||||
return $this;
|
||||
}
|
||||
public function setValues(array|string $values): Database\Query\Insert
|
||||
{
|
||||
$this->values = $values;
|
||||
return $this;
|
||||
}
|
||||
public function setSelect(Database\Query\Select|string $select): Database\Query\Insert
|
||||
{
|
||||
$this->select = $select;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getColumnString(): string
|
||||
{
|
||||
if (!isset($this->columns)) {
|
||||
return '';
|
||||
}
|
||||
$columns = (is_array($this->getColumns())) ? implode(', ', $this->getColumns()) : $this->getColumns();
|
||||
if (trim($columns) === '') {
|
||||
return '';
|
||||
}
|
||||
return " ({$columns})";
|
||||
}
|
||||
protected function getValueString(): string
|
||||
{
|
||||
if (!isset($this->values)) {
|
||||
return '';
|
||||
}
|
||||
$values = (is_array($this->getValues())) ? implode(', ', $this->getValues()) : $this->getValues();
|
||||
return " VALUES ({$values})";
|
||||
}
|
||||
protected function getSelectString(): string
|
||||
{
|
||||
return (isset($this->select)) ? " {$this->getSelect()}" : '';
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
return implode('', [
|
||||
"INSERT INTO {$this->getTable()}",
|
||||
$this->getColumnString(),
|
||||
$this->getValueString(),
|
||||
$this->getSelectString()
|
||||
]);
|
||||
}
|
||||
}
|
292
src/Ideal/Database/Query/Select.php
Normal file
292
src/Ideal/Database/Query/Select.php
Normal file
@ -0,0 +1,292 @@
|
||||
<?php
|
||||
namespace ProVM\Ideal\Database\Query;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Reinforce\Database\Query\{hasConditions, hasTable};
|
||||
|
||||
abstract class Select extends Query implements Database\Query\Select
|
||||
{
|
||||
use hasTable, hasConditions;
|
||||
|
||||
public function __construct(array|string $columns = '*')
|
||||
{
|
||||
$this->columns($columns);
|
||||
}
|
||||
|
||||
public function columns(array|string $columns = '*'): Database\Query\Select
|
||||
{
|
||||
return $this->setColumns($columns);
|
||||
}
|
||||
public function from(string $table): Database\Query\Select
|
||||
{
|
||||
return $this->setTable($table);
|
||||
}
|
||||
public function joined(array|string $joins): Database\Query\Select
|
||||
{
|
||||
return $this->setJoins($joins);
|
||||
}
|
||||
public function where(array|string $conditions): Database\Query\Select
|
||||
{
|
||||
return $this->setConditions($conditions);
|
||||
}
|
||||
public function groupBy(array|string $grouping): Database\Query\Select
|
||||
{
|
||||
return $this->setGroups($grouping);
|
||||
}
|
||||
public function having(array|string $having): Database\Query\Select
|
||||
{
|
||||
return $this->setHaving($having);
|
||||
}
|
||||
public function orderBy(array|string $ordering): Database\Query\Select
|
||||
{
|
||||
return $this->setOrders($ordering);
|
||||
}
|
||||
public function limit(int $limit, ?int $offset = null): Database\Query\Select
|
||||
{
|
||||
$this->limit = $limit;
|
||||
if ($offset !== null) {
|
||||
$this->offset($offset);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function offset(int $offset): self
|
||||
{
|
||||
$this->offset = $offset;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected array|string $columns;
|
||||
protected array|string $joins;
|
||||
protected array|string $groups;
|
||||
protected array|string $having;
|
||||
protected array|string $orders;
|
||||
protected int $limit;
|
||||
protected int $offset;
|
||||
|
||||
public function getColumns(): array|string
|
||||
{
|
||||
return $this->columns ?? '*';
|
||||
}
|
||||
public function getJoins(): array|string
|
||||
{
|
||||
return $this->joins;
|
||||
}
|
||||
public function getGroups(): array|string
|
||||
{
|
||||
return $this->groups;
|
||||
}
|
||||
public function getHaving(): array|string
|
||||
{
|
||||
return $this->having;
|
||||
}
|
||||
public function getOrders(): array|string
|
||||
{
|
||||
return $this->orders;
|
||||
}
|
||||
|
||||
public function addColumn(string $column, ?string $alias = null): Database\Query\Select
|
||||
{
|
||||
if ($column === '*') {
|
||||
$this->columns []= $column;
|
||||
return $this;
|
||||
}
|
||||
$a = '';
|
||||
if ($alias !== null) {
|
||||
$a = " AS '{$alias}'";
|
||||
}
|
||||
$this->columns[] = "`{$column}`{$a}";
|
||||
return $this;
|
||||
}
|
||||
public function addJoin(string $table, string $expression): Database\Query\Select
|
||||
{
|
||||
$this->joins []= "{$table} ON {$expression}";
|
||||
return $this;
|
||||
}
|
||||
public function addGroup(string $group): Database\Query\Select
|
||||
{
|
||||
$this->groups []= "`{$group}`";
|
||||
return $this;
|
||||
}
|
||||
public function addHaving(string $having): Database\Query\Select
|
||||
{
|
||||
$this->having []= $having;
|
||||
return $this;
|
||||
}
|
||||
public function addOrder(string $column, ?string $direction = null): Database\Query\Select
|
||||
{
|
||||
if ($direction === null) {
|
||||
$direction = 'ASC';
|
||||
}
|
||||
$this->orders []= "{$column} {$direction}";
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setColumns(array|string $columns): Database\Query\Select
|
||||
{
|
||||
if (is_string($columns)) {
|
||||
$this->columns = $columns;
|
||||
return $this;
|
||||
}
|
||||
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 setJoins(array|string $joins): Database\Query\Select
|
||||
{
|
||||
if (is_string($joins)) {
|
||||
$this->joins = $joins;
|
||||
return $this;
|
||||
}
|
||||
foreach ($joins as $join) {
|
||||
$table = $join['table'] ?? $join[0];
|
||||
$expression = $join['expression'] ?? $join[1];
|
||||
$this->addJoin($table, $expression);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function setGroups(array|string $groups): Database\Query\Select
|
||||
{
|
||||
if (is_string($groups)) {
|
||||
$this->groups = $groups;
|
||||
return $this;
|
||||
}
|
||||
foreach ($groups as $group) {
|
||||
$this->addGroup($group);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function setHaving(array|string $having): Database\Query\Select
|
||||
{
|
||||
if (is_string($having)) {
|
||||
$this->having = $having;
|
||||
return $this;
|
||||
}
|
||||
foreach ($having as $item) {
|
||||
$this->addHaving($item);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function setOrders(array|string $orders): Database\Query\Select
|
||||
{
|
||||
if (is_string($orders)) {
|
||||
$this->orders = $orders;
|
||||
return $this;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
protected function getColumnsString(): string
|
||||
{
|
||||
if (!isset($this->columns) or (is_array($this->getColumns()) and count($this->getColumns()) === 0)
|
||||
or (is_string($this->getColumns()) and $this->getColumns() === '*')) {
|
||||
return '*';
|
||||
}
|
||||
$columns = (is_array($this->getColumns())) ? implode(', ', $this->getColumns()) : $this->getColumns();
|
||||
return "{$columns}";
|
||||
}
|
||||
protected function getJoinsString(): string
|
||||
{
|
||||
if (!isset($this->joins)) {
|
||||
return '';
|
||||
}
|
||||
if (is_string($this->getJoins())) {
|
||||
$str = $this->getJoins();
|
||||
return " {$str}";
|
||||
}
|
||||
$str = [];
|
||||
foreach ($this->getJoins() as $i => $join) {
|
||||
if (!str_contains(strtolower($join), 'join')) {
|
||||
$str []= "JOIN {$join}";
|
||||
continue;
|
||||
}
|
||||
$str []= $join;
|
||||
}
|
||||
$str = implode(' ', $str);
|
||||
return " {$str}";
|
||||
}
|
||||
protected function getGroupsString(): string
|
||||
{
|
||||
if (!isset($this->groups)) {
|
||||
return '';
|
||||
}
|
||||
$groups = (is_array($this->getGroups())) ? implode(', ', $this->getGroups()) : $this->getGroups();
|
||||
return " GROUP BY {$groups}";
|
||||
}
|
||||
protected function getHavingString(): string
|
||||
{
|
||||
if (!isset($this->having)) {
|
||||
return '';
|
||||
}
|
||||
$having = $this->getHaving();
|
||||
if (is_array($having)) {
|
||||
$first = array_shift($having);
|
||||
$matched = preg_match_all('/^and|or\s/i', trim($first));
|
||||
if ($matched > 0) {
|
||||
$temp = str_replace(['and ', 'or '], ['', ''], strtolower($first));
|
||||
$dif = strlen($first) - strlen($temp);
|
||||
$first = substr($first, $dif);
|
||||
}
|
||||
$having = "$first " . implode(' ', array_map(function(string $h) {
|
||||
$matched = preg_match_all('/^and|or\s/i', trim($h));
|
||||
if ($matched > 0) {
|
||||
return $h;
|
||||
}
|
||||
return "AND {$h}";
|
||||
}, $having));
|
||||
}
|
||||
if (trim($having) === '') {
|
||||
return '';
|
||||
}
|
||||
return " HAVING {$having}";
|
||||
}
|
||||
protected function getOrderString(): string
|
||||
{
|
||||
if (!isset($this->orders)) {
|
||||
return '';
|
||||
}
|
||||
$ordering = (is_array($this->getOrders())) ? implode(', ', $this->getOrders()) : $this->getOrders();
|
||||
return " ORDER BY {$ordering}";
|
||||
}
|
||||
protected function getLimitString(): string
|
||||
{
|
||||
if (!isset($this->limit)) {
|
||||
return '';
|
||||
}
|
||||
$limit = " LIMIT {$this->limit}";
|
||||
if (isset($this->offset)) {
|
||||
$limit .= " OFFSET {$this->offset}";
|
||||
}
|
||||
return $limit;
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
return implode('', [
|
||||
"SELECT {$this->getColumnsString()}",
|
||||
" FROM {$this->getTable()}",
|
||||
$this->getJoinsString(),
|
||||
$this->getConditionsString(),
|
||||
$this->getGroupsString(),
|
||||
$this->getHavingString(),
|
||||
$this->getOrderString(),
|
||||
$this->getLimitString()
|
||||
]);
|
||||
}
|
||||
}
|
27
src/Ideal/Database/Query/Truncate.php
Normal file
27
src/Ideal/Database/Query/Truncate.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace ProVM\Ideal\Database\Query;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Reinforce\Database\Query\hasTable;
|
||||
|
||||
abstract class Truncate extends Query implements Database\Query\Truncate
|
||||
{
|
||||
use hasTable;
|
||||
|
||||
public function __construct(?string $table = null)
|
||||
{
|
||||
if ($table !== null) {
|
||||
$this->table($table);
|
||||
}
|
||||
}
|
||||
public function table(string $table): Database\Query\Truncate
|
||||
{
|
||||
return $this->setTable($table);
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
return "TRUNCATE TABLE {$this->getTable()}";
|
||||
}
|
||||
}
|
82
src/Ideal/Database/Query/Update.php
Normal file
82
src/Ideal/Database/Query/Update.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
namespace ProVM\Ideal\Database\Query;
|
||||
|
||||
use ProVM\Ideal\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Reinforce\Database\Query\{hasConditions, hasTable};
|
||||
|
||||
abstract class Update extends Query implements Database\Query\Update
|
||||
{
|
||||
use hasTable, hasConditions;
|
||||
|
||||
public function __construct(?string $table = null)
|
||||
{
|
||||
if ($table !== null) {
|
||||
$this->table($table);
|
||||
}
|
||||
}
|
||||
public function table(string $table): Database\Query\Update
|
||||
{
|
||||
return $this->setTable($table);
|
||||
}
|
||||
public function set(array|string $value_pairs): Database\Query\Update
|
||||
{
|
||||
return $this->setValues($value_pairs);
|
||||
}
|
||||
public function where(array|string $conditions): Database\Query\Update
|
||||
{
|
||||
return $this->setConditions($conditions);
|
||||
}
|
||||
|
||||
protected array|string $values;
|
||||
|
||||
public function getValues(): array
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
public function addValue(string|array $values): Database\Query\Update
|
||||
{
|
||||
if (is_string($values)) {
|
||||
$this->values []= $values;
|
||||
return $this;
|
||||
}
|
||||
$column = $values['column'] ?? $values[0];
|
||||
$value = $values['value'] ?? $values[1];
|
||||
if (!is_numeric($value)) {
|
||||
$value = "'{$value}'";
|
||||
}
|
||||
$this->values []= "`{$column}` = {$value}";
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setValues(array|string $values): Database\Query\Update
|
||||
{
|
||||
if (is_string($values)) {
|
||||
$this->addValue($values);
|
||||
return $this;
|
||||
}
|
||||
foreach ($values as $value) {
|
||||
$this->addValue($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getValuesString(): string
|
||||
{
|
||||
if (!isset($this->values)) {
|
||||
return '';
|
||||
}
|
||||
$values = (is_array($this->getValues())) ? implode(', ', $this->getValues()) : $this->getValues();
|
||||
return " SET {$values}";
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
return implode('', [
|
||||
"UPDATE {$this->getTable()}",
|
||||
$this->getValuesString(),
|
||||
$this->getConditionsString()
|
||||
]);
|
||||
}
|
||||
}
|
55
src/Reinforce/Database/Query/hasConditions.php
Normal file
55
src/Reinforce/Database/Query/hasConditions.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace ProVM\Reinforce\Database\Query;
|
||||
|
||||
trait hasConditions
|
||||
{
|
||||
protected array|string $conditions;
|
||||
|
||||
protected function getConditions(): array|string
|
||||
{
|
||||
return $this->conditions ?? '';
|
||||
}
|
||||
|
||||
protected function setConditions(array|string $conditions): self
|
||||
{
|
||||
if (is_array($conditions)) {
|
||||
foreach ($conditions as $condition) {
|
||||
$this->addCondition($condition);
|
||||
}
|
||||
} else {
|
||||
$this->addCondition($conditions);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
protected function addCondition(array|string $condition): self
|
||||
{
|
||||
$this->conditions []= $condition;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getConditionsString(): string
|
||||
{
|
||||
$conditions = $this->getConditions();
|
||||
if (is_array($conditions)) {
|
||||
$first = array_shift($conditions);
|
||||
$matched = preg_match_all('/^and|or\s/i', trim($first));
|
||||
if ($matched > 0) {
|
||||
$temp = str_replace(['and ', 'or '], ['', ''], strtolower($first));
|
||||
$dif = strlen($first) - strlen($temp);
|
||||
$first = substr($first, $dif);
|
||||
}
|
||||
$conditions = "{$first} " . implode(' ', array_map(function(string $condition) {
|
||||
$matched = preg_match_all('/^and|or\s/i', trim($condition));
|
||||
if ($matched > 0) {
|
||||
return $condition;
|
||||
}
|
||||
return "AND {$condition}";
|
||||
}, $conditions));
|
||||
}
|
||||
$conditions = trim($conditions);
|
||||
if ($conditions === '') {
|
||||
return '';
|
||||
}
|
||||
return " WHERE {$conditions}";
|
||||
}
|
||||
}
|
21
src/Reinforce/Database/Query/hasTable.php
Normal file
21
src/Reinforce/Database/Query/hasTable.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace ProVM\Reinforce\Database\Query;
|
||||
|
||||
trait hasTable
|
||||
{
|
||||
protected string $table;
|
||||
|
||||
public function getTable(): string
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
public function setTable(string $table, ?string $alias = null): self
|
||||
{
|
||||
if ($alias !== null) {
|
||||
$table = "{$table} '{$alias}'";
|
||||
}
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user