proceed further with beans
This commit is contained in:
33
Query.php
33
Query.php
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace DatabaseHelper;
|
||||
|
||||
use DatabaseHelper\beans\OrderBy;
|
||||
use DatabaseHelper\beans\Schema;
|
||||
use DatabaseHelper\enums\Aggregation;
|
||||
use DatabaseHelper\enums\Join;
|
||||
@@ -12,10 +13,10 @@ class Query
|
||||
use Conditionable;
|
||||
|
||||
protected Schema $schema;
|
||||
protected array $columns = ['*'];
|
||||
protected array $aggregations = [];
|
||||
protected array $cols = ['*'];
|
||||
protected array $aggregates = [];
|
||||
protected array $joins = [];
|
||||
public array $orderBy;
|
||||
public OrderBy $orderBy;
|
||||
|
||||
public function __construct(Schema $table) {
|
||||
$this->schema = $table;
|
||||
@@ -23,27 +24,27 @@ class Query
|
||||
|
||||
public function select(string ...$cols): Query {
|
||||
if (!empty($cols))
|
||||
$this->columns = $cols;
|
||||
$this->cols = $cols;
|
||||
foreach ($cols as $col)
|
||||
$this->schema->requireColumn($col);
|
||||
if (!isset($this->schema->cols[$col]))
|
||||
throw new InvalidArgumentException("Column '$col' does not exist");
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orderBy(string $col, Order $order): Query {
|
||||
$this->schema->requireColumn($col);
|
||||
$this->orderBy = [
|
||||
'name' => $col,
|
||||
'order' => $order
|
||||
];
|
||||
$this->schema->reqCol($col);
|
||||
$this->orderBy = new OrderBy;
|
||||
$this->orderBy->col = $col;
|
||||
$this->orderBy->order = $order;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function join(Join $join, Schema $other): Query {
|
||||
$foreignKey = null;
|
||||
if($this->schema->existsReference($other))
|
||||
$foreignKey = $this->schema->references[$other->name];
|
||||
$foreignKey = $this->schema->refs[$other->name];
|
||||
if ($other->existsReference($this->schema))
|
||||
$foreignKey = $other->references[$this->schema->name];
|
||||
$foreignKey = $other->refs[$this->schema->name];
|
||||
|
||||
if (is_null($foreignKey))
|
||||
throw new InvalidArgumentException('Joins can only applied to referencing columns.');
|
||||
@@ -68,9 +69,9 @@ class Query
|
||||
|
||||
public function toSql(): string {
|
||||
// Merge any aggregations with the standard columns.
|
||||
$selectColumns = $this->columns;
|
||||
$selectColumns = $this->cols;
|
||||
if ($this->hasAggregations())
|
||||
$selectColumns = array_merge($selectColumns, $this->aggregations);
|
||||
$selectColumns = array_merge($selectColumns, $this->aggregates);
|
||||
|
||||
// Build the SELECT clause.
|
||||
$columns = implode(", ", $selectColumns);
|
||||
@@ -100,12 +101,12 @@ class Query
|
||||
public function aggregate(string $col, string $alias, Aggregation $func): Query {
|
||||
if ($col != '*')
|
||||
$this->schema->requireColumn($col);
|
||||
$this->aggregations[] = strtoupper($func->toString()) . "($col) AS $alias";
|
||||
$this->aggregates[] = strtoupper($func->toString()) . "($col) AS $alias";
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasAggregations(): bool {
|
||||
return !empty($this->aggregations);
|
||||
return !empty($this->aggregates);
|
||||
}
|
||||
|
||||
public function query(): mixed {
|
||||
|
Reference in New Issue
Block a user