Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
0fad17edc0 | |||
e6777af88a | |||
dc71bf7336 | |||
d266008224 | |||
849d42ea57 | |||
acce5b2b17 | |||
fc372b56c1 | |||
946a0b0b5e | |||
3b1902ed1a | |||
e7ab3fb8b7 | |||
d4fecf977d | |||
a22a927bb4 | |||
758a4b7c7f | |||
2fd0af5c2f | |||
97d5ae8450 | |||
a789231d5f | |||
0f4438bd5f | |||
7750ab95c6 | |||
47d38a6595 | |||
3458cf8532 | |||
682f0b87fa |
118
Readme.md
Normal file
118
Readme.md
Normal file
@ -0,0 +1,118 @@
|
||||
# QueryBuilder
|
||||
|
||||
## Requirements
|
||||
+ PHP 8+ (Should work with 7+, but haven't tested it).
|
||||
+ `Psr\Container\ContainerInterface` implementation like [`php-di/php-di`](https://packagist.org/packages/php-di/php-di).
|
||||
|
||||
## Installation
|
||||
|
||||
```composer.json```
|
||||
```
|
||||
{
|
||||
...
|
||||
"repositories": [
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://git.provm.cl/ProVM/query_builder.git"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
...
|
||||
"provm/query_builder": "^1.0",
|
||||
...
|
||||
},
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
## Setup
|
||||
|
||||
```
|
||||
$container->set(ProVM\Concept\Database\Query\Select::class, function(Psr\Container\ContainerInterface $container) {
|
||||
return $container->get(ProVM\Database\Query\MySQL\Select::class);
|
||||
});
|
||||
$container->set(ProVM\Concept\Database\Query\Insert::class, function(Psr\Container\ContainerInterface $container) {
|
||||
return $container->get(ProVM\Database\Query\MySQL\Insert::class);
|
||||
});
|
||||
$container->set(ProVM\Concept\Database\Query\Update::class, function(Psr\Container\ContainerInterface $container) {
|
||||
return $container->get(ProVM\Database\Query\MySQL\Update::class);
|
||||
});
|
||||
$container->set(ProVM\Concept\Database\Query\Delete::class, function(Psr\Container\ContainerInterface $container) {
|
||||
return $container->get(ProVM\Database\Query\MySQL\Delete::class);
|
||||
});
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
QueryBuilder
|
||||
```
|
||||
include_once 'vendor/autoload.php';
|
||||
$qb = new QueryBuilder();
|
||||
|
||||
$query = $qb->select(<*columns>);
|
||||
$query = $qb->insert(<table>);
|
||||
$query = $qb->update(<table>);
|
||||
$query = $qb->delete(<table>);
|
||||
```
|
||||
|
||||
### Queries
|
||||
|
||||
#### Select
|
||||
`SELECT <columns> FROM <table>[ JOIN <table2> ON <table>.<column_1> = <table2>.<column_2>][ WHERE <expr1>[ AND|OR <expr2>]][ GROUP BY <column1>[, <column2>]][ HAVING <expr1>[ AND|OR <expr2>]][ ORDER BY <column1> ASC|DESC[, <column2> ASC|DESC]]`
|
||||
|
||||
Mysql also include `[ LIMIT <limit>[ OFFSET <offset>]]`
|
||||
```
|
||||
$query = $qb->select(); // use '*' for columns
|
||||
or
|
||||
$query = $qb->select()->select(['id', 'column1',]);
|
||||
or
|
||||
$query = $qb->select(['id', 'column1',]);
|
||||
|
||||
$query->from('table1');
|
||||
$query->joins([['table2', 'table1.column1 = table2.column21'],]);
|
||||
$query->where(['table2.column22 = 10',]);
|
||||
$query->groupBy(['table1.column1',]);
|
||||
$query->having(['table1.column1 < 10',]);
|
||||
$query->orderBy(['table2.column22',]);
|
||||
$query->limit(10, 10);
|
||||
```
|
||||
|
||||
#### Insert
|
||||
`INSERT INTO <table>[ (<columns>)]`
|
||||
with two options
|
||||
`<select query>` or
|
||||
` VALUES (<values>)`
|
||||
```
|
||||
$query = $qb->insert('table');
|
||||
|
||||
$query->columns(['column1',]);
|
||||
$query->values(['value1',];
|
||||
or
|
||||
$query->select($select_query);
|
||||
```
|
||||
|
||||
#### Update
|
||||
`UPDATE <table> SET <column1> = <value1>[, <column2> = <value2>][ WHERE <expr1>[ AND|OR <expr2>]]`
|
||||
```
|
||||
$query = $qb->update('table');
|
||||
$query->set([['column1', 10],]); or $query->set([['column' => 'column1', 'value' => 10],]);
|
||||
$query->where(['column2 = 10', ]);
|
||||
```
|
||||
|
||||
#### Delete
|
||||
`DELETE FROM <table> WHERE <expr1>[ AND|OR <expr2>]`
|
||||
```
|
||||
$query = $qb->delete('table');
|
||||
$query->where(['column1 = 10',]);
|
||||
```
|
||||
|
||||
|
||||
Pass the query to a string
|
||||
```
|
||||
$str = $query->build();
|
||||
or
|
||||
$str = "{$query}";
|
||||
```
|
||||
|
||||
## TODO
|
||||
Implement other databases
|
@ -12,7 +12,6 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"psr/container": "^2.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
@ -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()
|
||||
]);
|
||||
}
|
||||
}
|
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;
|
||||
}
|
10
src/Concept/Database/Query/Create.php
Normal file
10
src/Concept/Database/Query/Create.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Database\Query;
|
||||
|
||||
use ProVM\Concept\Database\Query;
|
||||
|
||||
interface Create extends Query
|
||||
{
|
||||
public function table(string $table): Create;
|
||||
public function columns(array|string $columns): Create;
|
||||
}
|
@ -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;
|
||||
}
|
77
src/Database/Query/Builder.php
Normal file
77
src/Database/Query/Builder.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query;
|
||||
|
||||
use ProVM\Concept\Database\Query;
|
||||
|
||||
class Builder implements Query\Builder
|
||||
{
|
||||
public function __construct(array $query_classes)
|
||||
{
|
||||
$this->setQueries($query_classes);
|
||||
}
|
||||
|
||||
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/Database/Query/MySQL/Create.php
Normal file
8
src/Database/Query/MySQL/Create.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Implement\Database\Query;
|
||||
|
||||
class Create extends Query\Create
|
||||
{
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Alias\Database\Query\Delete as Base;
|
||||
use ProVM\Implement\Database\Query;
|
||||
|
||||
class Delete extends Base
|
||||
{}
|
||||
class Delete extends Query\Delete
|
||||
{
|
||||
}
|
||||
|
8
src/Database/Query/MySQL/Drop.php
Normal file
8
src/Database/Query/MySQL/Drop.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Implement\Database\Query;
|
||||
|
||||
class Drop extends Query\Drop
|
||||
{
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Alias\Database\Query\Insert as Base;
|
||||
use ProVM\Implement\Database\Query;
|
||||
|
||||
class Insert extends Base
|
||||
{}
|
||||
class Insert extends Query\Insert
|
||||
{
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Alias\Database\Query\Select as Base;
|
||||
use ProVM\Implement\Database\Query;
|
||||
|
||||
class Select extends Base
|
||||
class Select extends Query\Select
|
||||
{
|
||||
public function limit(int $limit, ?int $offset = null): Select
|
||||
{
|
||||
@ -13,38 +13,45 @@ 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);
|
||||
$query = [
|
||||
parent::build(),
|
||||
$this->getLimitString()
|
||||
];
|
||||
return implode('', $query);
|
||||
}
|
||||
}
|
||||
|
8
src/Database/Query/MySQL/Truncate.php
Normal file
8
src/Database/Query/MySQL/Truncate.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Implement\Database\Query;
|
||||
|
||||
class Truncate extends Query\Truncate
|
||||
{
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
<?php
|
||||
namespace ProVM\Database\Query\MySQL;
|
||||
|
||||
use ProVM\Alias\Database\Query\Update as Base;
|
||||
use ProVM\Implement\Database\Query;
|
||||
|
||||
class Update extends Base
|
||||
{}
|
||||
class Update extends Query\Update
|
||||
{
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
12
src/Implement/Database/Query.php
Normal file
12
src/Implement/Database/Query.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database;
|
||||
|
||||
use ProVM\Concept\Database;
|
||||
|
||||
abstract class Query implements Database\Query
|
||||
{
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->build();
|
||||
}
|
||||
}
|
62
src/Implement/Database/Query/Create.php
Normal file
62
src/Implement/Database/Query/Create.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database\Query;
|
||||
|
||||
use ProVM\Implement\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
|
||||
abstract class Create extends Query implements Database\Query\Create
|
||||
{
|
||||
use hasTable;
|
||||
|
||||
public function __construct(string $table)
|
||||
{
|
||||
$this->table($table);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
protected array|string $columns;
|
||||
|
||||
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->columns = $columns;
|
||||
return $this;
|
||||
}
|
||||
foreach ($columns as $column) {
|
||||
$this->addColumn($column);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getColumnsString(): string
|
||||
{
|
||||
if (is_string($this->columns)) {
|
||||
return $this->columns;
|
||||
}
|
||||
return implode(', ', $this->getColumns());
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
return "CREATE {$this->getTable()} ({$this->getColumnsString()})";
|
||||
}
|
||||
}
|
27
src/Implement/Database/Query/Delete.php
Normal file
27
src/Implement/Database/Query/Delete.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database\Query;
|
||||
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Implement\Database\Query;
|
||||
|
||||
abstract class Delete extends Query implements Database\Query\Delete
|
||||
{
|
||||
use hasTable, hasConditions;
|
||||
|
||||
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()
|
||||
]);
|
||||
}
|
||||
}
|
27
src/Implement/Database/Query/Drop.php
Normal file
27
src/Implement/Database/Query/Drop.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database\Query;
|
||||
|
||||
use ProVM\Implement\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
|
||||
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 build(): string
|
||||
{
|
||||
return "DROP TABLE {$this->getTable()}";
|
||||
}
|
||||
}
|
94
src/Implement/Database/Query/Insert.php
Normal file
94
src/Implement/Database/Query/Insert.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database\Query;
|
||||
|
||||
use ProVM\Implement\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
|
||||
abstract class Insert extends Query implements Database\Query\Insert
|
||||
{
|
||||
use hasTable;
|
||||
|
||||
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()
|
||||
]);
|
||||
}
|
||||
}
|
260
src/Implement/Database/Query/Select.php
Normal file
260
src/Implement/Database/Query/Select.php
Normal file
@ -0,0 +1,260 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database\Query;
|
||||
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Implement\Database\Query;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
protected array|string $columns;
|
||||
protected array|string $joins;
|
||||
protected array|string $groups;
|
||||
protected array|string $having;
|
||||
protected array|string $orders;
|
||||
|
||||
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
|
||||
{
|
||||
$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}";
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
return implode('', [
|
||||
"SELECT {$this->getColumnsString()}",
|
||||
"FROM {$this->getTable()}",
|
||||
$this->getJoinsString(),
|
||||
$this->getConditionsString(),
|
||||
$this->getGroupsString(),
|
||||
$this->getHavingString(),
|
||||
$this->getOrderString()
|
||||
]);
|
||||
}
|
||||
}
|
24
src/Implement/Database/Query/Truncate.php
Normal file
24
src/Implement/Database/Query/Truncate.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database\Query;
|
||||
|
||||
use ProVM\Implement\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
|
||||
abstract class Truncate extends Query implements Database\Query\Truncate
|
||||
{
|
||||
use hasTable;
|
||||
|
||||
public function __construct(string $table)
|
||||
{
|
||||
$this->table($table);
|
||||
}
|
||||
public function table(string $table): Database\Query\Truncate
|
||||
{
|
||||
return $this->setTable($table);
|
||||
}
|
||||
|
||||
public function build(): string
|
||||
{
|
||||
return "TRUNCATE TABLE {$this->getTable()}";
|
||||
}
|
||||
}
|
71
src/Implement/Database/Query/Update.php
Normal file
71
src/Implement/Database/Query/Update.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database\Query;
|
||||
|
||||
use ProVM\Implement\Database\Query;
|
||||
use ProVM\Concept\Database;
|
||||
|
||||
abstract class Update extends Query implements Database\Query\Update
|
||||
{
|
||||
use hasTable, hasConditions;
|
||||
|
||||
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 $column, int|string $value): Database\Query\Update
|
||||
{
|
||||
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->values = $values;
|
||||
return $this;
|
||||
}
|
||||
foreach ($values as $value) {
|
||||
$column = $value['column'] ?? $value[0];
|
||||
$val = $value['value'] ?? $value[1];
|
||||
$this->addValue($column, $val);
|
||||
}
|
||||
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()
|
||||
]);
|
||||
}
|
||||
}
|
43
src/Implement/Database/Query/hasConditions.php
Normal file
43
src/Implement/Database/Query/hasConditions.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database\Query;
|
||||
|
||||
trait hasConditions
|
||||
{
|
||||
protected array|string $conditions;
|
||||
|
||||
protected function getConditions(): array|string
|
||||
{
|
||||
return $this->conditions ?? '';
|
||||
}
|
||||
|
||||
protected function setConditions(array|string $conditions)
|
||||
{
|
||||
$this->conditions = $conditions;
|
||||
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));
|
||||
}
|
||||
if (trim($conditions) === '') {
|
||||
return '';
|
||||
}
|
||||
return " WHERE {$conditions}";
|
||||
}
|
||||
}
|
22
src/Implement/Database/Query/hasTable.php
Normal file
22
src/Implement/Database/Query/hasTable.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Database\Query;
|
||||
|
||||
trait hasTable
|
||||
{
|
||||
protected string $table;
|
||||
|
||||
public function getTable(): string
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
public function setTable(string $table, ?string $alias = null)
|
||||
{
|
||||
$table = "`{$table}`";
|
||||
if ($alias !== null) {
|
||||
$table = "{$table} '{$alias}'";
|
||||
}
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user