108 lines
3.1 KiB
PHP
108 lines
3.1 KiB
PHP
<?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'));
|
|
}
|
|
}
|