develop #2
@ -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": [
|
||||||
{
|
{
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept\Database;
|
namespace ProVM\Concept\Database;
|
||||||
|
|
||||||
interface Query
|
use Stringable;
|
||||||
|
|
||||||
|
interface Query extends Stringable
|
||||||
{
|
{
|
||||||
public function build(): string;
|
public function build(): string;
|
||||||
public function __toString(): string;
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ use ProVM\Concept\Database\Query;
|
|||||||
|
|
||||||
interface Create extends Query
|
interface Create extends Query
|
||||||
{
|
{
|
||||||
public function table(string $table): Create;
|
public function table(string $table): self;
|
||||||
public function columns(array|string $columns): Create;
|
public function columns(array|string $columns): self;
|
||||||
|
public function foreign(array|string $columnDefinition): self;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\MySQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Create extends Query\Create
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\MySQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Delete extends Query\Delete
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\MySQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Drop extends Query\Drop
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\MySQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Insert extends Query\Insert
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\MySQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Truncate extends Query\Truncate
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\MySQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Update extends Query\Update
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\PostgreSQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Create extends Query\Create
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\PostgreSQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Delete extends Query\Delete
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\PostgreSQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Drop extends Query\Drop
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\PostgreSQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Insert extends Query\Insert
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\PostgreSQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Select extends Query\Select
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\PostgreSQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Truncate extends Query\Truncate
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database\Query\PostgreSQL;
|
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
|
||||||
|
|
||||||
class Update extends Query\Update
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database\Query;
|
namespace ProVM\Enforce\Database\Query;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use FilesystemIterator;
|
||||||
use ProVM\Concept\Database\Query;
|
use ProVM\Concept\Database\Query;
|
||||||
|
|
||||||
class Builder implements Query\Builder
|
class Builder implements Query\Builder
|
||||||
@ -10,6 +12,34 @@ class Builder implements Query\Builder
|
|||||||
$this->setQueries($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 array $query_classes;
|
||||||
|
|
||||||
protected function getQueries(): array
|
protected function getQueries(): array
|
8
src/Enforce/Database/Query/MySQL/Create.php
Normal file
8
src/Enforce/Database/Query/MySQL/Create.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Create extends Query\Create
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/MySQL/Delete.php
Normal file
8
src/Enforce/Database/Query/MySQL/Delete.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Delete extends Query\Delete
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/MySQL/Drop.php
Normal file
8
src/Enforce/Database/Query/MySQL/Drop.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Drop extends Query\Drop
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/MySQL/Insert.php
Normal file
8
src/Enforce/Database/Query/MySQL/Insert.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Insert extends Query\Insert
|
||||||
|
{
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database\Query\MySQL;
|
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
class Select extends Query\Select
|
class Select extends Query\Select
|
||||||
{
|
{
|
||||||
@ -48,10 +48,11 @@ class Select extends Query\Select
|
|||||||
|
|
||||||
public function build(): string
|
public function build(): string
|
||||||
{
|
{
|
||||||
$query = [
|
return parent::build();
|
||||||
|
/*$query = [
|
||||||
parent::build(),
|
parent::build(),
|
||||||
$this->getLimitString()
|
$this->getLimitString()
|
||||||
];
|
];
|
||||||
return implode('', $query);
|
return implode('', $query);*/
|
||||||
}
|
}
|
||||||
}
|
}
|
8
src/Enforce/Database/Query/MySQL/Truncate.php
Normal file
8
src/Enforce/Database/Query/MySQL/Truncate.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Truncate extends Query\Truncate
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/MySQL/Update.php
Normal file
8
src/Enforce/Database/Query/MySQL/Update.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\MySQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Update extends Query\Update
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/PostgreSQL/Create.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Create.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Create extends Query\Create
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/PostgreSQL/Delete.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Delete.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Delete extends Query\Delete
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/PostgreSQL/Drop.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Drop.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Drop extends Query\Drop
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/PostgreSQL/Insert.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Insert.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Insert extends Query\Insert
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/PostgreSQL/Select.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Select.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Select extends Query\Select
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/PostgreSQL/Truncate.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Truncate.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Truncate extends Query\Truncate
|
||||||
|
{
|
||||||
|
}
|
8
src/Enforce/Database/Query/PostgreSQL/Update.php
Normal file
8
src/Enforce/Database/Query/PostgreSQL/Update.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Enforce\Database\Query\PostgreSQL;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
class Update extends Query\Update
|
||||||
|
{
|
||||||
|
}
|
@ -1,10 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Database;
|
namespace ProVM\Ideal\Database;
|
||||||
|
|
||||||
use ProVM\Concept\Database;
|
use ProVM\Concept\Database;
|
||||||
|
|
||||||
abstract class Query implements Database\Query
|
abstract class Query implements Database\Query
|
||||||
{
|
{
|
||||||
|
public function getType(): string
|
||||||
|
{
|
||||||
|
$class = explode("\\", get_class($this));
|
||||||
|
return array_pop($class);
|
||||||
|
}
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
return $this->build();
|
return $this->build();
|
132
src/Ideal/Database/Query/Create.php
Normal file
132
src/Ideal/Database/Query/Create.php
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
use ProVM\Concept\Database;
|
||||||
|
use ProVM\Reinforce\Database\Query\hasTable;
|
||||||
|
|
||||||
|
abstract class Create extends Query implements Database\Query\Create
|
||||||
|
{
|
||||||
|
use hasTable;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->table($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isTemp(): Database\Query\Create
|
||||||
|
{
|
||||||
|
$this->temp = true;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function ifNotExists(): Database\Query\Create
|
||||||
|
{
|
||||||
|
$this->exists = true;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function table(string $table): Database\Query\Create
|
||||||
|
{
|
||||||
|
return $this->setTable($table);
|
||||||
|
}
|
||||||
|
public function columns(array|string $columns): Database\Query\Create
|
||||||
|
{
|
||||||
|
return $this->setColumns($columns);
|
||||||
|
}
|
||||||
|
public function foreign(array|string $columnDefinition): Database\Query\Create
|
||||||
|
{
|
||||||
|
return $this->setForeign($columnDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool $temp = false;
|
||||||
|
protected bool $exists = false;
|
||||||
|
protected array|string $columns;
|
||||||
|
protected array|string $foreign;
|
||||||
|
|
||||||
|
public function getColumns(): array
|
||||||
|
{
|
||||||
|
return $this->columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addColumn(string $column): Database\Query\Create
|
||||||
|
{
|
||||||
|
$this->columns []= $column;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setColumns(array|string $columns): Database\Query\Create
|
||||||
|
{
|
||||||
|
if (is_string($columns)) {
|
||||||
|
$this->addColumn($columns);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
foreach ($columns as $column) {
|
||||||
|
$this->addColumn($column);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function getForegin(): array
|
||||||
|
{
|
||||||
|
return $this->foreign;
|
||||||
|
}
|
||||||
|
public function addForeign(string|array $definition): Database\Query\Create
|
||||||
|
{
|
||||||
|
if (is_string($definition)) {
|
||||||
|
$this->foreign []= $definition;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
$ids = implode(', ', $definition['ids']);
|
||||||
|
$columns = $definition['columns'];
|
||||||
|
if (is_array($definition['columns'])) {
|
||||||
|
$columns = implode(', ', $definition['columns']);
|
||||||
|
}
|
||||||
|
$delete = $definition['delete'] ?? 'CASCADE';
|
||||||
|
$update = $definition['update'] ?? 'CASCADE';
|
||||||
|
$fk = implode('_', ['fk', str_replace('.', '_', $definition['table']), $ids]);
|
||||||
|
$this->foreign []= "CONSTRAINT {$fk} FOREIGN KEY ({$columns}) REFERENCES {$definition['table']} ({$ids}) ON DELETE {$delete} ON UPDATE {$update}";
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function setForeign(array|string $columnDefinition): Database\Query\Create
|
||||||
|
{
|
||||||
|
if (is_string($columnDefinition)) {
|
||||||
|
$this->addForeign($columnDefinition);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
foreach ($columnDefinition as $column) {
|
||||||
|
$this->addForeign($column);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getTempString(): string
|
||||||
|
{
|
||||||
|
return ($this->temp) ? ' TEMPORARY' : '';
|
||||||
|
}
|
||||||
|
protected function getIfNotExistsString(): string
|
||||||
|
{
|
||||||
|
return ($this->exists) ? ' IF NOT EXISTS' : '';
|
||||||
|
}
|
||||||
|
protected function getColumnsString(): string
|
||||||
|
{
|
||||||
|
if (is_string($this->columns)) {
|
||||||
|
return $this->columns;
|
||||||
|
}
|
||||||
|
return implode(', ', $this->getColumns());
|
||||||
|
}
|
||||||
|
protected function getForeignString(): string
|
||||||
|
{
|
||||||
|
if (!isset($this->foreign)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
if (is_string($this->foreign)) {
|
||||||
|
return ", {$this->foreign}";
|
||||||
|
}
|
||||||
|
return ', ' . implode(', ', $this->getForegin());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function build(): string
|
||||||
|
{
|
||||||
|
return "CREATE{$this->getTempString()} TABLE{$this->getIfNotExistsString()} {$this->getTable()} ({$this->getColumnsString()}{$this->getForeignString()})";
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Database\Query;
|
namespace ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
use ProVM\Concept\Database;
|
use ProVM\Concept\Database;
|
||||||
use ProVM\Implement\Database\Query;
|
use ProVM\Reinforce\Database\Query\{hasConditions, hasTable};
|
||||||
|
|
||||||
abstract class Delete extends Query implements Database\Query\Delete
|
abstract class Delete extends Query implements Database\Query\Delete
|
||||||
{
|
{
|
||||||
use hasTable, hasConditions;
|
use hasTable, hasConditions;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->from($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
public function from(string $table): Database\Query\Delete
|
public function from(string $table): Database\Query\Delete
|
||||||
{
|
{
|
||||||
return $this->setTable($table);
|
return $this->setTable($table);
|
37
src/Ideal/Database/Query/Drop.php
Normal file
37
src/Ideal/Database/Query/Drop.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
|
use ProVM\Concept\Database;
|
||||||
|
use ProVM\Reinforce\Database\Query\hasTable;
|
||||||
|
|
||||||
|
abstract class Drop extends Query implements Database\Query\Drop
|
||||||
|
{
|
||||||
|
use hasTable;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->table($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function table(string $table): Database\Query\Drop
|
||||||
|
{
|
||||||
|
return $this->setTable($table);
|
||||||
|
}
|
||||||
|
public function cascade(): Database\Query\Drop
|
||||||
|
{
|
||||||
|
$this->cascade = true;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected bool $cascade = false;
|
||||||
|
protected function getCascadeString(): string
|
||||||
|
{
|
||||||
|
return $this->cascade ? ' CASCADE' : '';
|
||||||
|
}
|
||||||
|
public function build(): string
|
||||||
|
{
|
||||||
|
return "DROP TABLE {$this->getTable()}{$this->getCascadeString()}";
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Database\Query;
|
namespace ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
use ProVM\Ideal\Database\Query;
|
||||||
use ProVM\Concept\Database;
|
use ProVM\Concept\Database;
|
||||||
|
use ProVM\Reinforce\Database\Query\hasTable;
|
||||||
|
|
||||||
abstract class Insert extends Query implements Database\Query\Insert
|
abstract class Insert extends Query implements Database\Query\Insert
|
||||||
{
|
{
|
||||||
use hasTable;
|
use hasTable;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->into($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function into(string $table): Database\Query\Insert
|
public function into(string $table): Database\Query\Insert
|
||||||
{
|
{
|
||||||
return $this->setTable($table);
|
return $this->setTable($table);
|
@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Database\Query;
|
namespace ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
|
use ProVM\Ideal\Database\Query;
|
||||||
use ProVM\Concept\Database;
|
use ProVM\Concept\Database;
|
||||||
use ProVM\Implement\Database\Query;
|
use ProVM\Reinforce\Database\Query\{hasConditions, hasTable};
|
||||||
|
|
||||||
abstract class Select extends Query implements Database\Query\Select
|
abstract class Select extends Query implements Database\Query\Select
|
||||||
{
|
{
|
||||||
@ -41,12 +42,27 @@ abstract class Select extends Query implements Database\Query\Select
|
|||||||
{
|
{
|
||||||
return $this->setOrders($ordering);
|
return $this->setOrders($ordering);
|
||||||
}
|
}
|
||||||
|
public function limit(int $limit, ?int $offset = null): Database\Query\Select
|
||||||
|
{
|
||||||
|
$this->limit = $limit;
|
||||||
|
if ($offset !== null) {
|
||||||
|
$this->offset($offset);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function offset(int $offset): self
|
||||||
|
{
|
||||||
|
$this->offset = $offset;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
protected array|string $columns;
|
protected array|string $columns;
|
||||||
protected array|string $joins;
|
protected array|string $joins;
|
||||||
protected array|string $groups;
|
protected array|string $groups;
|
||||||
protected array|string $having;
|
protected array|string $having;
|
||||||
protected array|string $orders;
|
protected array|string $orders;
|
||||||
|
protected int $limit;
|
||||||
|
protected int $offset;
|
||||||
|
|
||||||
public function getColumns(): array|string
|
public function getColumns(): array|string
|
||||||
{
|
{
|
||||||
@ -178,6 +194,10 @@ abstract class Select extends Query implements Database\Query\Select
|
|||||||
|
|
||||||
protected function getColumnsString(): string
|
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();
|
$columns = (is_array($this->getColumns())) ? implode(', ', $this->getColumns()) : $this->getColumns();
|
||||||
return "({$columns})";
|
return "({$columns})";
|
||||||
}
|
}
|
||||||
@ -244,6 +264,17 @@ abstract class Select extends Query implements Database\Query\Select
|
|||||||
$ordering = (is_array($this->getOrders())) ? implode(', ', $this->getOrders()) : $this->getOrders();
|
$ordering = (is_array($this->getOrders())) ? implode(', ', $this->getOrders()) : $this->getOrders();
|
||||||
return " ORDER BY {$ordering}";
|
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
|
public function build(): string
|
||||||
{
|
{
|
||||||
@ -254,7 +285,8 @@ abstract class Select extends Query implements Database\Query\Select
|
|||||||
$this->getConditionsString(),
|
$this->getConditionsString(),
|
||||||
$this->getGroupsString(),
|
$this->getGroupsString(),
|
||||||
$this->getHavingString(),
|
$this->getHavingString(),
|
||||||
$this->getOrderString()
|
$this->getOrderString(),
|
||||||
|
$this->getLimitString()
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,17 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Database\Query;
|
namespace ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
use ProVM\Ideal\Database\Query;
|
||||||
use ProVM\Concept\Database;
|
use ProVM\Concept\Database;
|
||||||
|
use ProVM\Reinforce\Database\Query\hasTable;
|
||||||
|
|
||||||
abstract class Truncate extends Query implements Database\Query\Truncate
|
abstract class Truncate extends Query implements Database\Query\Truncate
|
||||||
{
|
{
|
||||||
use hasTable;
|
use hasTable;
|
||||||
|
|
||||||
public function __construct(string $table)
|
public function __construct(?string $table = null)
|
||||||
{
|
{
|
||||||
|
if ($table !== null) {
|
||||||
$this->table($table);
|
$this->table($table);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public function table(string $table): Database\Query\Truncate
|
public function table(string $table): Database\Query\Truncate
|
||||||
{
|
{
|
||||||
return $this->setTable($table);
|
return $this->setTable($table);
|
@ -1,13 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Database\Query;
|
namespace ProVM\Ideal\Database\Query;
|
||||||
|
|
||||||
use ProVM\Implement\Database\Query;
|
use ProVM\Ideal\Database\Query;
|
||||||
use ProVM\Concept\Database;
|
use ProVM\Concept\Database;
|
||||||
|
use ProVM\Reinforce\Database\Query\{hasConditions, hasTable};
|
||||||
|
|
||||||
abstract class Update extends Query implements Database\Query\Update
|
abstract class Update extends Query implements Database\Query\Update
|
||||||
{
|
{
|
||||||
use hasTable, hasConditions;
|
use hasTable, hasConditions;
|
||||||
|
|
||||||
|
public function __construct(?string $table = null)
|
||||||
|
{
|
||||||
|
if ($table !== null) {
|
||||||
|
$this->table($table);
|
||||||
|
}
|
||||||
|
}
|
||||||
public function table(string $table): Database\Query\Update
|
public function table(string $table): Database\Query\Update
|
||||||
{
|
{
|
||||||
return $this->setTable($table);
|
return $this->setTable($table);
|
||||||
@ -28,8 +35,14 @@ abstract class Update extends Query implements Database\Query\Update
|
|||||||
return $this->values;
|
return $this->values;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addValue(string $column, int|string $value): Database\Query\Update
|
public function addValue(string|array $values): Database\Query\Update
|
||||||
{
|
{
|
||||||
|
if (is_string($values)) {
|
||||||
|
$this->values []= $values;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
$column = $values['column'] ?? $values[0];
|
||||||
|
$value = $values['value'] ?? $values[1];
|
||||||
if (!is_numeric($value)) {
|
if (!is_numeric($value)) {
|
||||||
$value = "'{$value}'";
|
$value = "'{$value}'";
|
||||||
}
|
}
|
||||||
@ -40,13 +53,11 @@ abstract class Update extends Query implements Database\Query\Update
|
|||||||
public function setValues(array|string $values): Database\Query\Update
|
public function setValues(array|string $values): Database\Query\Update
|
||||||
{
|
{
|
||||||
if (is_string($values)) {
|
if (is_string($values)) {
|
||||||
$this->values = $values;
|
$this->addValue($values);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
foreach ($values as $value) {
|
foreach ($values as $value) {
|
||||||
$column = $value['column'] ?? $value[0];
|
$this->addValue($value);
|
||||||
$val = $value['value'] ?? $value[1];
|
|
||||||
$this->addValue($column, $val);
|
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
@ -1,62 +0,0 @@
|
|||||||
<?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()})";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
<?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()}";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Database\Query;
|
namespace ProVM\Reinforce\Database\Query;
|
||||||
|
|
||||||
trait hasConditions
|
trait hasConditions
|
||||||
{
|
{
|
||||||
@ -10,9 +10,20 @@ trait hasConditions
|
|||||||
return $this->conditions ?? '';
|
return $this->conditions ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setConditions(array|string $conditions)
|
protected function setConditions(array|string $conditions): self
|
||||||
{
|
{
|
||||||
$this->conditions = $conditions;
|
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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +46,8 @@ trait hasConditions
|
|||||||
return "AND {$condition}";
|
return "AND {$condition}";
|
||||||
}, $conditions));
|
}, $conditions));
|
||||||
}
|
}
|
||||||
if (trim($conditions) === '') {
|
$conditions = trim($conditions);
|
||||||
|
if ($conditions === '') {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return " WHERE {$conditions}";
|
return " WHERE {$conditions}";
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement\Database\Query;
|
namespace ProVM\Reinforce\Database\Query;
|
||||||
|
|
||||||
trait hasTable
|
trait hasTable
|
||||||
{
|
{
|
||||||
@ -10,9 +10,8 @@ trait hasTable
|
|||||||
return $this->table;
|
return $this->table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTable(string $table, ?string $alias = null)
|
public function setTable(string $table, ?string $alias = null): self
|
||||||
{
|
{
|
||||||
$table = "`{$table}`";
|
|
||||||
if ($alias !== null) {
|
if ($alias !== null) {
|
||||||
$table = "{$table} '{$alias}'";
|
$table = "{$table} '{$alias}'";
|
||||||
}
|
}
|
Reference in New Issue
Block a user