Files
query_builder/src/Database/Query/Builder.php

78 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace ProVM\Database\Query;
use ProVM\Concept\Database\Query;
class Builder implements Query\Builder
{
public function __construct(array $query_classes)
{
$this->setQueries($query_classes);
}
protected array $query_classes;
protected function getQueries(): array
{
return $this->query_classes;
}
protected function get(string $query_interface): string
{
return $this->query_classes[$query_interface];
}
protected function addQuery(string $query_interface, string $query): Builder
{
$this->query_classes[$query_interface] = $query;
return $this;
}
protected function setQueries(array $queries): Builder
{
foreach ($queries as $interface => $query) {
$this->addQuery($interface, $query);
}
return $this;
}
protected function build(string $query_interface, ?array $args = null): Query
{
$class = $this->get($query_interface);
if ($args !== null) {
return new $class(...$args);
}
return new $class;
}
public function select(array|string $columns = '*'): Query\Select
{
return $this->build(Query\Select::class, compact('columns'));
}
public function insert(?string $table = null): Query\Insert
{
return $this->build(Query\Insert::class, compact('table'));
}
public function update(?string $table = null): Query\Update
{
return $this->build(Query\Update::class, compact('table'));
}
public function delete(?string $table = null): Query\Delete
{
return $this->build(Query\Delete::class, compact('table'));
}
public function create(?string $table = null): Query\Create
{
return $this->build(Query\Create::class, compact('table'));
}
public function truncate(?string $table = null): Query\Truncate
{
return $this->build(Query\Truncate::class, compact('table'));
}
public function drop(?string $table = null): Query\Drop
{
return $this->build(Query\Drop::class, compact('table'));
}
}