Skip to content

Commit

Permalink
Table returns specific query instances
Browse files Browse the repository at this point in the history
  • Loading branch information
mecha committed May 17, 2022
1 parent 2825ce8 commit a4323b3
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use RebelCode\Atlas\Exception\NoTableSchemaException;
use RebelCode\Atlas\Expression\ExprInterface;
use RebelCode\Atlas\Expression\Term;
use RebelCode\Atlas\Query\DeleteQuery;
use RebelCode\Atlas\Query\InsertQuery;
use RebelCode\Atlas\Query\SelectQuery;
use RebelCode\Atlas\Query\UpdateQuery;
use RebelCode\Atlas\QueryType\CreateIndex;
use RebelCode\Atlas\QueryType\CreateTable;
use RebelCode\Atlas\QueryType\Delete;
Expand Down Expand Up @@ -118,7 +122,7 @@ public function create(bool $ifNotExists = true, ?string $collate = null): array
);
} else {
$queries = [
$this->createQuery(QueryType::CREATE_TABLE, [
new Query($this->getQueryType(QueryType::CREATE_TABLE), [
CreateTable::NAME => $this->name,
CreateTable::SCHEMA => $this->schema,
CreateTable::IF_NOT_EXISTS => $ifNotExists,
Expand All @@ -127,7 +131,7 @@ public function create(bool $ifNotExists = true, ?string $collate = null): array
];

foreach ($this->schema->getIndexes() as $name => $index) {
$queries[] = $this->createQuery(QueryType::CREATE_INDEX, [
$queries[] = new Query($this->getQueryType(QueryType::CREATE_INDEX), [
CreateIndex::TABLE => $this->name,
CreateIndex::NAME => $name,
CreateIndex::INDEX => $index,
Expand All @@ -147,7 +151,7 @@ public function drop(bool $ifExists = true, bool $cascade = false): Query
$this
);
} else {
return $this->createQuery(QueryType::DROP_TABLE, [
return new Query($this->getQueryType(QueryType::DROP_TABLE), [
DropTable::TABLE => $this->name,
DropTable::IF_EXISTS => $ifExists,
DropTable::CASCADE => $cascade,
Expand All @@ -162,8 +166,8 @@ public function select(
?array $order = null,
?int $limit = null,
?int $offset = null
): Query {
return $this->createQuery(QueryType::SELECT, [
): SelectQuery {
return new SelectQuery($this->getQueryType(QueryType::SELECT), [
Select::FROM => $this->name,
Select::COLUMNS => empty($columns) ? ['*'] : $columns,
Select::WHERE => $this->useWhereState($where),
Expand All @@ -177,15 +181,14 @@ public function select(
* Create an INSERT query.
*
* @param array<string, mixed>[] $records
* @return Query
*/
public function insert(array $records): Query
public function insert(array $records): InsertQuery
{
if (empty($records)) {
throw new DomainException('List of values to insert is empty');
}

return $this->createQuery(QueryType::INSERT, [
return new InsertQuery($this->getQueryType(QueryType::INSERT), [
Insert::TABLE => $this->name,
Insert::COLUMNS => array_keys($records[0]),
Insert::VALUES => $records,
Expand All @@ -197,8 +200,8 @@ public function update(
?ExprInterface $where = null,
?array $order = null,
?int $limit = null
): Query {
return $this->createQuery(QueryType::SELECT, [
): UpdateQuery {
return new UpdateQuery($this->getQueryType(QueryType::UPDATE), [
Update::TABLE => $this->name,
Update::SET => $set,
Update::WHERE => $this->useWhereState($where),
Expand All @@ -211,25 +214,27 @@ public function delete(
?ExprInterface $where = null,
?array $order = null,
?int $limit = null
): Query {
return $this->createQuery(QueryType::SELECT, [
): DeleteQuery {
return new DeleteQuery($this->getQueryType(QueryType::DELETE), [
Delete::FROM => $this->name,
Delete::WHERE => $this->useWhereState($where),
Delete::ORDER => $this->useOrderState($order),
Delete::LIMIT => $limit,
]);
}

/** Utility method for creating queries with the type extracted from config. */
protected function createQuery(string $typeKey, array $data): Query
/**
* Utility method for retrieving a query type from the config, throwing an exception if it's not found.
*/
protected function getQueryType(string $typeKey): QueryTypeInterface
{
$type = $this->config->getQueryType($typeKey);

if ($type === null) {
throw new MissingQueryTypeException("Query type \"$typeKey\" is missing in config", $typeKey);
}

return new Query($type, $data);
return $type;
}

protected function useWhereState(?ExprInterface $where): ?ExprInterface
Expand Down

0 comments on commit a4323b3

Please sign in to comment.