Compare commits
24 Commits
Author | SHA1 | Date | |
---|---|---|---|
c9ec400433 | |||
af62fe079f | |||
f07ad79e75 | |||
bc3421942f | |||
e6759bcf06 | |||
3944fab3f1 | |||
0fad17edc0 | |||
e64ea2709c | |||
e6777af88a | |||
dc71bf7336 | |||
d266008224 | |||
849d42ea57 | |||
acce5b2b17 | |||
fc372b56c1 | |||
946a0b0b5e | |||
3b1902ed1a | |||
e7ab3fb8b7 | |||
d4fecf977d | |||
a22a927bb4 | |||
758a4b7c7f | |||
2fd0af5c2f | |||
97d5ae8450 | |||
a789231d5f | |||
682f0b87fa |
3
.gitignore
vendored
3
.gitignore
vendored
@ -4,3 +4,6 @@
|
|||||||
|
|
||||||
# PHPStorm
|
# PHPStorm
|
||||||
**/.idea/
|
**/.idea/
|
||||||
|
|
||||||
|
# PHPUnit
|
||||||
|
**/.phpunit.cache/
|
||||||
|
10
Readme.md
10
Readme.md
@ -13,7 +13,7 @@
|
|||||||
"repositories": [
|
"repositories": [
|
||||||
{
|
{
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"path": "https://git.provm.cl/ProVM/query_builder.git"
|
"url": "https://git.provm.cl/ProVM/query_builder.git"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
@ -28,16 +28,16 @@
|
|||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
```
|
```
|
||||||
$container->set(ProVM\Concept\Database\Query\Select::class, function(Psr\Container\ContainerInterface $container) {
|
$container->set(Database\Define\Query\Select::class, function(Psr\Container\ContainerInterface $container) {
|
||||||
return $container->get(ProVM\Database\Query\MySQL\Select::class);
|
return $container->get(ProVM\Database\Query\MySQL\Select::class);
|
||||||
});
|
});
|
||||||
$container->set(ProVM\Concept\Database\Query\Insert::class, function(Psr\Container\ContainerInterface $container) {
|
$container->set(Database\Define\Query\Insert::class, function(Psr\Container\ContainerInterface $container) {
|
||||||
return $container->get(ProVM\Database\Query\MySQL\Insert::class);
|
return $container->get(ProVM\Database\Query\MySQL\Insert::class);
|
||||||
});
|
});
|
||||||
$container->set(ProVM\Concept\Database\Query\Update::class, function(Psr\Container\ContainerInterface $container) {
|
$container->set(Database\Define\Query\Update::class, function(Psr\Container\ContainerInterface $container) {
|
||||||
return $container->get(ProVM\Database\Query\MySQL\Update::class);
|
return $container->get(ProVM\Database\Query\MySQL\Update::class);
|
||||||
});
|
});
|
||||||
$container->set(ProVM\Concept\Database\Query\Delete::class, function(Psr\Container\ContainerInterface $container) {
|
$container->set(Database\Define\Query\Delete::class, function(Psr\Container\ContainerInterface $container) {
|
||||||
return $container->get(ProVM\Database\Query\MySQL\Delete::class);
|
return $container->get(ProVM\Database\Query\MySQL\Delete::class);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "provm/query_builder",
|
"name": "provm/query_builder",
|
||||||
|
"version": "3.0.0",
|
||||||
"type": "project",
|
"type": "project",
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^9.5",
|
"phpunit/phpunit": "^10"
|
||||||
"kint-php/kint": "^4.2"
|
|
||||||
},
|
},
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
@ -15,7 +15,7 @@
|
|||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"ProVM\\": "src/"
|
"Database\\": "src/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
25
phpunit.xml
Normal file
25
phpunit.xml
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
|
||||||
|
bootstrap="vendor/autoload.php"
|
||||||
|
cacheDirectory=".phpunit.cache"
|
||||||
|
executionOrder="depends,defects"
|
||||||
|
requireCoverageMetadata="false"
|
||||||
|
beStrictAboutCoverageMetadata="false"
|
||||||
|
beStrictAboutOutputDuringTests="true"
|
||||||
|
displayDetailsOnPhpunitDeprecations="true"
|
||||||
|
failOnPhpunitDeprecation="true"
|
||||||
|
failOnRisky="true"
|
||||||
|
failOnWarning="true">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="default">
|
||||||
|
<directory>tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
|
||||||
|
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
|
||||||
|
<include>
|
||||||
|
<directory>src</directory>
|
||||||
|
</include>
|
||||||
|
</source>
|
||||||
|
</phpunit>
|
@ -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 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Concept\Database;
|
|
||||||
|
|
||||||
interface Query
|
|
||||||
{
|
|
||||||
public function build(): string;
|
|
||||||
public function __toString(): string;
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Concept\Database\Query;
|
|
||||||
|
|
||||||
use ProVM\Concept\Database\Query;
|
|
||||||
|
|
||||||
interface Delete extends Query
|
|
||||||
{
|
|
||||||
public function from(string $table): Delete;
|
|
||||||
public function where(array $conditions): Delete;
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Concept\Database\Query;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Concept\Database\Query;
|
|
||||||
|
|
||||||
use ProVM\Concept\Database\Query;
|
|
||||||
|
|
||||||
interface Select extends Query
|
|
||||||
{
|
|
||||||
public function select(array $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;
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Concept\Database\Query;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
@ -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,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\MySQL;
|
|
||||||
|
|
||||||
use ProVM\Alias\Database\Query\Select as Base;
|
|
||||||
|
|
||||||
class Select extends Base
|
|
||||||
{
|
|
||||||
public function limit(int $limit, ?int $offset = null): Select
|
|
||||||
{
|
|
||||||
$this->setLimit($limit);
|
|
||||||
if ($offset !== null) {
|
|
||||||
$this->setOffset($offset);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
protected int $limit;
|
|
||||||
public function setLimit(int $limit): Select
|
|
||||||
{
|
|
||||||
$this->limit = $limit;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
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 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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
9
src/Define/Query.php
Normal file
9
src/Define/Query.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define;
|
||||||
|
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
|
interface Query extends Stringable
|
||||||
|
{
|
||||||
|
public function build(): string;
|
||||||
|
}
|
14
src/Define/Query/Builder.php
Normal file
14
src/Define/Query/Builder.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\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/Define/Query/Create.php
Normal file
11
src/Define/Query/Create.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\Query;
|
||||||
|
|
||||||
|
use Database\Define\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;
|
||||||
|
}
|
10
src/Define/Query/Delete.php
Normal file
10
src/Define/Query/Delete.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\Query;
|
||||||
|
|
||||||
|
use Database\Define\Query;
|
||||||
|
|
||||||
|
interface Delete extends Query
|
||||||
|
{
|
||||||
|
public function from(string $table): self;
|
||||||
|
public function where(array|string $conditions): self;
|
||||||
|
}
|
9
src/Define/Query/Drop.php
Normal file
9
src/Define/Query/Drop.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\Query;
|
||||||
|
|
||||||
|
use Database\Define\Query;
|
||||||
|
|
||||||
|
interface Drop extends Query
|
||||||
|
{
|
||||||
|
public function table(string $table): self;
|
||||||
|
}
|
12
src/Define/Query/Insert.php
Normal file
12
src/Define/Query/Insert.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\Query;
|
||||||
|
|
||||||
|
use Database\Define\Query;
|
||||||
|
|
||||||
|
interface Insert extends Query
|
||||||
|
{
|
||||||
|
public function into(string $table): self;
|
||||||
|
public function columns(array|string $columns): self;
|
||||||
|
public function values(array|string $values): self;
|
||||||
|
public function select(Select|string $select): self;
|
||||||
|
}
|
15
src/Define/Query/Select.php
Normal file
15
src/Define/Query/Select.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\Query;
|
||||||
|
|
||||||
|
use Database\Define\Query;
|
||||||
|
|
||||||
|
interface Select extends Query
|
||||||
|
{
|
||||||
|
public function columns(array|string $columns = '*'): self;
|
||||||
|
public function from(string $table): self;
|
||||||
|
public function joined(array|string $joins): self;
|
||||||
|
public function where(array|string $conditions): self;
|
||||||
|
public function groupBy(array|string $grouping): self;
|
||||||
|
public function having(array|string $having): self;
|
||||||
|
public function orderBy(array|string $ordering): self;
|
||||||
|
}
|
9
src/Define/Query/Truncate.php
Normal file
9
src/Define/Query/Truncate.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\Query;
|
||||||
|
|
||||||
|
use Database\Define\Query;
|
||||||
|
|
||||||
|
interface Truncate extends Query
|
||||||
|
{
|
||||||
|
public function table(string $table): self;
|
||||||
|
}
|
11
src/Define/Query/Update.php
Normal file
11
src/Define/Query/Update.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\Query;
|
||||||
|
|
||||||
|
use Database\Define\Query;
|
||||||
|
|
||||||
|
interface Update extends Query
|
||||||
|
{
|
||||||
|
public function table(string $table): self;
|
||||||
|
public function set(array|string $value_pairs): self;
|
||||||
|
public function where(array|string $conditions): self;
|
||||||
|
}
|
15
src/Except/Missing/Columns.php
Normal file
15
src/Except/Missing/Columns.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Except\Missing;
|
||||||
|
|
||||||
|
use Throwable;
|
||||||
|
use Database\Except;
|
||||||
|
|
||||||
|
class Columns extends Except\Query
|
||||||
|
{
|
||||||
|
public function __construct(string $tableName, ?Throwable $previous = null)
|
||||||
|
{
|
||||||
|
$message = "Missing columns for {$tableName}";
|
||||||
|
$code = 1;
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
}
|
||||||
|
}
|
15
src/Except/Query.php
Normal file
15
src/Except/Query.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Except;
|
||||||
|
|
||||||
|
use Throwable;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
abstract class Query extends Exception
|
||||||
|
{
|
||||||
|
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
|
||||||
|
{
|
||||||
|
$message = "Query Exception: {$message}";
|
||||||
|
$code = 1100 + $code;
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
}
|
||||||
|
}
|
17
src/Ideal/Query.php
Normal file
17
src/Ideal/Query.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Ideal;
|
||||||
|
|
||||||
|
use Database\Define;
|
||||||
|
|
||||||
|
abstract class Query implements Define\Query
|
||||||
|
{
|
||||||
|
public function getType(): string
|
||||||
|
{
|
||||||
|
$class = explode("\\", get_class($this));
|
||||||
|
return array_pop($class);
|
||||||
|
}
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return $this->build();
|
||||||
|
}
|
||||||
|
}
|
139
src/Ideal/Query/Create.php
Normal file
139
src/Ideal/Query/Create.php
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Ideal\Query;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
use Database\Define;
|
||||||
|
use Database\Reinforce\Query\hasTable;
|
||||||
|
use Database\Except;
|
||||||
|
|
||||||
|
abstract class Create extends Query implements Define\Query\Create
|
||||||
|
{
|
||||||
|
use hasTable;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->table($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isTemp(): self
|
||||||
|
{
|
||||||
|
$this->temp = true;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function ifNotExists(): self
|
||||||
|
{
|
||||||
|
$this->exists = true;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function table(string $table): self
|
||||||
|
{
|
||||||
|
return $this->setTable($table);
|
||||||
|
}
|
||||||
|
public function columns(array|string $columns): self
|
||||||
|
{
|
||||||
|
return $this->setColumns($columns);
|
||||||
|
}
|
||||||
|
public function foreign(array|string $columnDefinition): self
|
||||||
|
{
|
||||||
|
return $this->setForeign($columnDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool $temp = false;
|
||||||
|
protected bool $exists = false;
|
||||||
|
protected array|string $columns;
|
||||||
|
protected array|string $foreign;
|
||||||
|
|
||||||
|
public function getColumns(): array
|
||||||
|
{
|
||||||
|
if (!isset($this->columns)) {
|
||||||
|
throw new Except\Missing\Columns($this->getTable());
|
||||||
|
}
|
||||||
|
return $this->columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addColumn(string $column): self
|
||||||
|
{
|
||||||
|
$this->columns []= $column;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setColumns(array|string $columns): self
|
||||||
|
{
|
||||||
|
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): self
|
||||||
|
{
|
||||||
|
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): self
|
||||||
|
{
|
||||||
|
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 (!isset($this->columns)) {
|
||||||
|
throw new Except\Missing\Columns($this->getTable());
|
||||||
|
}
|
||||||
|
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/Query/Delete.php
Normal file
34
src/Ideal/Query/Delete.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Ideal\Query;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
use Database\Define;
|
||||||
|
use Database\Reinforce\Query\{hasConditions, hasTable};
|
||||||
|
|
||||||
|
abstract class Delete extends Query implements Define\Query\Delete
|
||||||
|
{
|
||||||
|
use hasTable, hasConditions;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->from($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function from(string $table): self
|
||||||
|
{
|
||||||
|
return $this->setTable($table);
|
||||||
|
}
|
||||||
|
public function where(array|string $conditions): self
|
||||||
|
{
|
||||||
|
return $this->setConditions($conditions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function build(): string
|
||||||
|
{
|
||||||
|
return implode('', [
|
||||||
|
"DELETE FROM {$this->getTable()}",
|
||||||
|
$this->getConditionsString()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
37
src/Ideal/Query/Drop.php
Normal file
37
src/Ideal/Query/Drop.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Ideal\Query;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
use Database\Define;
|
||||||
|
use Database\Reinforce\Query\hasTable;
|
||||||
|
|
||||||
|
abstract class Drop extends Query implements Define\Query\Drop
|
||||||
|
{
|
||||||
|
use hasTable;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->table($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function table(string $table): self
|
||||||
|
{
|
||||||
|
return $this->setTable($table);
|
||||||
|
}
|
||||||
|
public function cascade(): self
|
||||||
|
{
|
||||||
|
$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/Query/Insert.php
Normal file
102
src/Ideal/Query/Insert.php
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Ideal\Query;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
use Database\Define;
|
||||||
|
use Database\Reinforce\Query\hasTable;
|
||||||
|
|
||||||
|
abstract class Insert extends Query implements Define\Query\Insert
|
||||||
|
{
|
||||||
|
use hasTable;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->into($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function into(string $table): self
|
||||||
|
{
|
||||||
|
return $this->setTable($table);
|
||||||
|
}
|
||||||
|
public function columns(array|string $columns): self
|
||||||
|
{
|
||||||
|
return $this->setColumns($columns);
|
||||||
|
}
|
||||||
|
public function values(array|string $values): self
|
||||||
|
{
|
||||||
|
return $this->setValues($values);
|
||||||
|
}
|
||||||
|
public function select(Define\Query\Select|string $select): self
|
||||||
|
{
|
||||||
|
return $this->setSelect($select);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected array|string $columns;
|
||||||
|
protected array|string $values;
|
||||||
|
protected Define\Query\Select|string $select;
|
||||||
|
|
||||||
|
public function getColumns(): array|string
|
||||||
|
{
|
||||||
|
return $this->columns;
|
||||||
|
}
|
||||||
|
public function getValues(): array|string
|
||||||
|
{
|
||||||
|
return $this->values;
|
||||||
|
}
|
||||||
|
public function getSelect(): Define\Query\Select|string
|
||||||
|
{
|
||||||
|
return $this->select;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setColumns(array|string $columns): self
|
||||||
|
{
|
||||||
|
$this->columns = $columns;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function setValues(array|string $values): self
|
||||||
|
{
|
||||||
|
$this->values = $values;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function setSelect(Define\Query\Select|string $select): self
|
||||||
|
{
|
||||||
|
$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/Query/Select.php
Normal file
292
src/Ideal/Query/Select.php
Normal file
@ -0,0 +1,292 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Ideal\Query;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
use Database\Define;
|
||||||
|
use Database\Reinforce\Query\{hasConditions, hasTable};
|
||||||
|
|
||||||
|
abstract class Select extends Query implements Define\Query\Select
|
||||||
|
{
|
||||||
|
use hasTable, hasConditions;
|
||||||
|
|
||||||
|
public function __construct(array|string $columns = '*')
|
||||||
|
{
|
||||||
|
$this->columns($columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function columns(array|string $columns = '*'): self
|
||||||
|
{
|
||||||
|
return $this->setColumns($columns);
|
||||||
|
}
|
||||||
|
public function from(string $table): self
|
||||||
|
{
|
||||||
|
return $this->setTable($table);
|
||||||
|
}
|
||||||
|
public function joined(array|string $joins): self
|
||||||
|
{
|
||||||
|
return $this->setJoins($joins);
|
||||||
|
}
|
||||||
|
public function where(array|string $conditions): self
|
||||||
|
{
|
||||||
|
return $this->setConditions($conditions);
|
||||||
|
}
|
||||||
|
public function groupBy(array|string $grouping): self
|
||||||
|
{
|
||||||
|
return $this->setGroups($grouping);
|
||||||
|
}
|
||||||
|
public function having(array|string $having): self
|
||||||
|
{
|
||||||
|
return $this->setHaving($having);
|
||||||
|
}
|
||||||
|
public function orderBy(array|string $ordering): self
|
||||||
|
{
|
||||||
|
return $this->setOrders($ordering);
|
||||||
|
}
|
||||||
|
public function limit(int $limit, ?int $offset = null): self
|
||||||
|
{
|
||||||
|
$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): self
|
||||||
|
{
|
||||||
|
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): self
|
||||||
|
{
|
||||||
|
$this->joins []= "{$table} ON {$expression}";
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function addGroup(string $group): self
|
||||||
|
{
|
||||||
|
$this->groups []= "`{$group}`";
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function addHaving(string $having): self
|
||||||
|
{
|
||||||
|
$this->having []= $having;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function addOrder(string $column, ?string $direction = null): self
|
||||||
|
{
|
||||||
|
if ($direction === null) {
|
||||||
|
$direction = 'ASC';
|
||||||
|
}
|
||||||
|
$this->orders []= "{$column} {$direction}";
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setColumns(array|string $columns): self
|
||||||
|
{
|
||||||
|
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): self
|
||||||
|
{
|
||||||
|
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): self
|
||||||
|
{
|
||||||
|
if (is_string($groups)) {
|
||||||
|
$this->groups = $groups;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
foreach ($groups as $group) {
|
||||||
|
$this->addGroup($group);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function setHaving(array|string $having): self
|
||||||
|
{
|
||||||
|
if (is_string($having)) {
|
||||||
|
$this->having = $having;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
foreach ($having as $item) {
|
||||||
|
$this->addHaving($item);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function setOrders(array|string $orders): self
|
||||||
|
{
|
||||||
|
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/Query/Truncate.php
Normal file
27
src/Ideal/Query/Truncate.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Ideal\Query;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
use Database\Define;
|
||||||
|
use Database\Reinforce\Query\hasTable;
|
||||||
|
|
||||||
|
abstract class Truncate extends Query implements Define\Query\Truncate
|
||||||
|
{
|
||||||
|
use hasTable;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->table($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function table(string $table): self
|
||||||
|
{
|
||||||
|
return $this->setTable($table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function build(): string
|
||||||
|
{
|
||||||
|
return "TRUNCATE TABLE {$this->getTable()}";
|
||||||
|
}
|
||||||
|
}
|
82
src/Ideal/Query/Update.php
Normal file
82
src/Ideal/Query/Update.php
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Ideal\Query;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
use Database\Define;
|
||||||
|
use Database\Reinforce\Query\{hasConditions, hasTable};
|
||||||
|
|
||||||
|
abstract class Update extends Query implements Define\Query\Update
|
||||||
|
{
|
||||||
|
use hasTable, hasConditions;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->table($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function table(string $table): self
|
||||||
|
{
|
||||||
|
return $this->setTable($table);
|
||||||
|
}
|
||||||
|
public function set(array|string $value_pairs): self
|
||||||
|
{
|
||||||
|
return $this->setValues($value_pairs);
|
||||||
|
}
|
||||||
|
public function where(array|string $conditions): self
|
||||||
|
{
|
||||||
|
return $this->setConditions($conditions);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected array|string $values;
|
||||||
|
|
||||||
|
public function getValues(): array
|
||||||
|
{
|
||||||
|
return $this->values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addValue(string|array $values): self
|
||||||
|
{
|
||||||
|
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): self
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
107
src/Implement/Query/Builder.php
Normal file
107
src/Implement/Query/Builder.php
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use FilesystemIterator;
|
||||||
|
use Database\Define\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): self
|
||||||
|
{
|
||||||
|
$this->query_classes[$query_interface] = $query;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setQueries(array $queries): self
|
||||||
|
{
|
||||||
|
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'));
|
||||||
|
}
|
||||||
|
}
|
13
src/Implement/Query/MySQL/Create.php
Normal file
13
src/Implement/Query/MySQL/Create.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\MySQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Create extends Query\Create
|
||||||
|
{
|
||||||
|
public function getTable(): string
|
||||||
|
{
|
||||||
|
$table = parent::getTable();
|
||||||
|
return "`{$table}`";
|
||||||
|
}
|
||||||
|
}
|
13
src/Implement/Query/MySQL/Delete.php
Normal file
13
src/Implement/Query/MySQL/Delete.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\MySQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Delete extends Query\Delete
|
||||||
|
{
|
||||||
|
public function getTable(): string
|
||||||
|
{
|
||||||
|
$table = parent::getTable();
|
||||||
|
return "`{$table}`";
|
||||||
|
}
|
||||||
|
}
|
13
src/Implement/Query/MySQL/Drop.php
Normal file
13
src/Implement/Query/MySQL/Drop.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\MySQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Drop extends Query\Drop
|
||||||
|
{
|
||||||
|
public function getTable(): string
|
||||||
|
{
|
||||||
|
$table = parent::getTable();
|
||||||
|
return "`{$table}`";
|
||||||
|
}
|
||||||
|
}
|
13
src/Implement/Query/MySQL/Insert.php
Normal file
13
src/Implement/Query/MySQL/Insert.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\MySQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Insert extends Query\Insert
|
||||||
|
{
|
||||||
|
public function getTable(): string
|
||||||
|
{
|
||||||
|
$table = parent::getTable();
|
||||||
|
return "`{$table}`";
|
||||||
|
}
|
||||||
|
}
|
64
src/Implement/Query/MySQL/Select.php
Normal file
64
src/Implement/Query/MySQL/Select.php
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\MySQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Select extends Query\Select
|
||||||
|
{
|
||||||
|
public function limit(int $limit, ?int $offset = null): self
|
||||||
|
{
|
||||||
|
$this->setLimit($limit);
|
||||||
|
if ($offset !== null) {
|
||||||
|
$this->setOffset($offset);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTable(): string
|
||||||
|
{
|
||||||
|
$table = parent::getTable();
|
||||||
|
return "`{$table}`";
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int $limit;
|
||||||
|
protected int $offset;
|
||||||
|
|
||||||
|
public function getLimit(): int
|
||||||
|
{
|
||||||
|
return $this->limit;
|
||||||
|
}
|
||||||
|
public function getOffset(): int
|
||||||
|
{
|
||||||
|
return $this->offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setLimit(int $limit): self
|
||||||
|
{
|
||||||
|
$this->limit = $limit;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function setOffset(int $offset): self
|
||||||
|
{
|
||||||
|
$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
|
||||||
|
{
|
||||||
|
return parent::build();
|
||||||
|
/*$query = [
|
||||||
|
parent::build(),
|
||||||
|
$this->getLimitString()
|
||||||
|
];
|
||||||
|
return implode('', $query);*/
|
||||||
|
}
|
||||||
|
}
|
13
src/Implement/Query/MySQL/Truncate.php
Normal file
13
src/Implement/Query/MySQL/Truncate.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\MySQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Truncate extends Query\Truncate
|
||||||
|
{
|
||||||
|
public function getTable(): string
|
||||||
|
{
|
||||||
|
$table = parent::getTable();
|
||||||
|
return "`{$table}`";
|
||||||
|
}
|
||||||
|
}
|
22
src/Implement/Query/MySQL/Update.php
Normal file
22
src/Implement/Query/MySQL/Update.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\MySQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Update extends Query\Update
|
||||||
|
{
|
||||||
|
public function getTable(): string
|
||||||
|
{
|
||||||
|
$table = parent::getTable();
|
||||||
|
return "`{$table}`";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function build(): string
|
||||||
|
{
|
||||||
|
return implode('', [
|
||||||
|
"UPDATE TABLE {$this->getTable()}",
|
||||||
|
$this->getValuesString(),
|
||||||
|
$this->getConditionsString()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
8
src/Implement/Query/PostgreSQL/Create.php
Normal file
8
src/Implement/Query/PostgreSQL/Create.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Create extends Query\Create
|
||||||
|
{
|
||||||
|
}
|
8
src/Implement/Query/PostgreSQL/Delete.php
Normal file
8
src/Implement/Query/PostgreSQL/Delete.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Delete extends Query\Delete
|
||||||
|
{
|
||||||
|
}
|
8
src/Implement/Query/PostgreSQL/Drop.php
Normal file
8
src/Implement/Query/PostgreSQL/Drop.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Drop extends Query\Drop
|
||||||
|
{
|
||||||
|
}
|
8
src/Implement/Query/PostgreSQL/Insert.php
Normal file
8
src/Implement/Query/PostgreSQL/Insert.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Insert extends Query\Insert
|
||||||
|
{
|
||||||
|
}
|
8
src/Implement/Query/PostgreSQL/Select.php
Normal file
8
src/Implement/Query/PostgreSQL/Select.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Select extends Query\Select
|
||||||
|
{
|
||||||
|
}
|
8
src/Implement/Query/PostgreSQL/Truncate.php
Normal file
8
src/Implement/Query/PostgreSQL/Truncate.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Truncate extends Query\Truncate
|
||||||
|
{
|
||||||
|
}
|
8
src/Implement/Query/PostgreSQL/Update.php
Normal file
8
src/Implement/Query/PostgreSQL/Update.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use Database\Ideal\Query;
|
||||||
|
|
||||||
|
class Update extends Query\Update
|
||||||
|
{
|
||||||
|
}
|
55
src/Reinforce/Query/hasConditions.php
Normal file
55
src/Reinforce/Query/hasConditions.php
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Reinforce\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/Query/hasTable.php
Normal file
21
src/Reinforce/Query/hasTable.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Reinforce\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;
|
||||||
|
}
|
||||||
|
}
|
24
tests/MySQLCreateTest.php
Normal file
24
tests/MySQLCreateTest.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class MySQLCreateTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testBaseQuery(): void
|
||||||
|
{
|
||||||
|
$query = new Database\Implement\Query\MySQL\Create();
|
||||||
|
$query->table('test_table');
|
||||||
|
$query->columns('`column1` int');
|
||||||
|
|
||||||
|
$expected = "CREATE TABLE `test_table` (`column1` int)";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, "{$query}");
|
||||||
|
}
|
||||||
|
public function testMissingColumns(): void
|
||||||
|
{
|
||||||
|
$query = new Database\Implement\Query\MySQL\Create();
|
||||||
|
$query->table('test_table');
|
||||||
|
|
||||||
|
$this->expectException(Database\Except\Missing\Columns::class);
|
||||||
|
echo "{$query}";
|
||||||
|
}
|
||||||
|
}
|
15
tests/MySQLDeleteTest.php
Normal file
15
tests/MySQLDeleteTest.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class MySQLDeleteTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testBaseQuery(): void
|
||||||
|
{
|
||||||
|
$query = new Database\Implement\Query\MySQL\Delete();
|
||||||
|
$query->from('test_table');
|
||||||
|
|
||||||
|
$expected = "DELETE FROM `test_table`";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, "{$query}");
|
||||||
|
}
|
||||||
|
}
|
15
tests/MySQLDropTest.php
Normal file
15
tests/MySQLDropTest.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class MySQLDropTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testBaseQuery(): void
|
||||||
|
{
|
||||||
|
$query = new Database\Implement\Query\MySQL\Drop();
|
||||||
|
$query->table('test_table');
|
||||||
|
|
||||||
|
$expected = "DROP TABLE `test_table`";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, "{$query}");
|
||||||
|
}
|
||||||
|
}
|
16
tests/MySQLInsertTest.php
Normal file
16
tests/MySQLInsertTest.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class MySQLInsertTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testBaseQuery(): void
|
||||||
|
{
|
||||||
|
$query = new Database\Implement\Query\MySQL\Insert();
|
||||||
|
$query->into('test_table');
|
||||||
|
$query->values('?, ?, ?');
|
||||||
|
|
||||||
|
$expected = "INSERT INTO `test_table` VALUES (?, ?, ?)";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, "{$query}");
|
||||||
|
}
|
||||||
|
}
|
24
tests/MySQLSelectTest.php
Normal file
24
tests/MySQLSelectTest.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class MySQLSelectTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testBaseQuery(): void
|
||||||
|
{
|
||||||
|
$query = new Database\Implement\Query\MySQL\Select();
|
||||||
|
$query->from('test_table');
|
||||||
|
|
||||||
|
$expected = "SELECT * FROM `test_table`";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, "{$query}");
|
||||||
|
}
|
||||||
|
public function testColumns(): void
|
||||||
|
{
|
||||||
|
$query = new Database\Implement\Query\MySQL\Select('asd');
|
||||||
|
$query->from('test_table');
|
||||||
|
|
||||||
|
$expected = "SELECT asd FROM `test_table`";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, "{$query}");
|
||||||
|
}
|
||||||
|
}
|
15
tests/MySQLTruncateTest.php
Normal file
15
tests/MySQLTruncateTest.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class MySQLTruncateTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testBaseQuery(): void
|
||||||
|
{
|
||||||
|
$query = new Database\Implement\Query\MySQL\Truncate();
|
||||||
|
$query->table('test_table');
|
||||||
|
|
||||||
|
$expected = "TRUNCATE TABLE `test_table`";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, "{$query}");
|
||||||
|
}
|
||||||
|
}
|
16
tests/MySQLUpdateTest.php
Normal file
16
tests/MySQLUpdateTest.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class MySQLUpdateTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testBaseQuery(): void
|
||||||
|
{
|
||||||
|
$query = new Database\Implement\Query\MySQL\Update();
|
||||||
|
$query->table('test_table');
|
||||||
|
$query->set('column1 = 0');
|
||||||
|
|
||||||
|
$expected = "UPDATE TABLE `test_table` SET column1 = 0";
|
||||||
|
|
||||||
|
$this->assertEquals($expected, "{$query}");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user