Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Driver/Test/TestTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Aternos\Model\ModelInterface;
use Aternos\Model\ModelRegistry;
use Aternos\Model\Query\DeleteQuery;
use Aternos\Model\Query\Direction;
use Aternos\Model\Query\GroupField;
use Aternos\Model\Query\OrderField;
use Aternos\Model\Query\Query;
Expand Down Expand Up @@ -163,7 +164,7 @@ protected function compareEntries(TestTableEntry $a, TestTableEntry $b, array $o
if ($aValue === $bValue) {
continue;
}
if ($orderField->direction === OrderField::ASCENDING) {
if ($orderField->direction === Direction::ASCENDING) {
return $aValue > $bValue ? 1 : -1;
} else {
return $aValue < $bValue ? 1 : -1;
Expand Down
7 changes: 4 additions & 3 deletions src/Driver/Test/TestTableEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ArrayAccess;
use Aternos\Model\ModelInterface;
use Aternos\Model\Query\Conjunction;
use Aternos\Model\Query\SelectField;
use Aternos\Model\Query\UpdateField;
use Aternos\Model\Query\WhereCondition;
Expand Down Expand Up @@ -42,14 +43,14 @@ public function matchesWhereGroup(?WhereGroup $where): bool
} else if ($condition instanceof WhereCondition) {
$matches = $this->matchesWhereCondition($condition);
}
if ($where->conjunction === WhereGroup::AND && !$matches) {
if ($where->conjunction === Conjunction::AND && !$matches) {
return false;
}
if ($where->conjunction === WhereGroup::OR && $matches) {
if ($where->conjunction === Conjunction::OR && $matches) {
return true;
}
}
return $where->conjunction === WhereGroup::AND;
return $where->conjunction === Conjunction::AND;
}

/**
Expand Down
15 changes: 8 additions & 7 deletions src/Driver/Test/TestTableEntryGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Aternos\Model\Driver\Test;

use Aternos\Model\Query\AggregateFunction;
use Aternos\Model\Query\GroupField;
use Aternos\Model\Query\SelectField;

Expand Down Expand Up @@ -88,9 +89,9 @@ public function aggregateAndAlias(?array $fields, bool $collapse = true): static
$aggregatedEntry[$key] = $entry[$field->key] ?? null;

// init functions
if ($field->function === SelectField::COUNT) {
if ($field->function === AggregateFunction::COUNT) {
$aggregatedEntry[$key] = 1;
} elseif ($field->function === SelectField::AVERAGE) {
} elseif ($field->function === AggregateFunction::AVERAGE) {
$averageFields[$key] = 1;
}
}
Expand All @@ -102,17 +103,17 @@ public function aggregateAndAlias(?array $fields, bool $collapse = true): static
$entry[$key] = $entry[$field->key] ?? null;

// handle functions
if ($field->function === SelectField::SUM) {
if ($field->function === AggregateFunction::SUM) {
$aggregatedEntry[$key] += $entry[$field->key] ?? 0;
} elseif ($field->function === SelectField::COUNT) {
} elseif ($field->function === AggregateFunction::COUNT) {
// ++ does not work with ArrayAccess
$aggregatedEntry[$key] += 1;
} elseif ($field->function === SelectField::AVERAGE) {
} elseif ($field->function === AggregateFunction::AVERAGE) {
$averageFields[$key]++;
$aggregatedEntry[$key] += $entry[$field->key] ?? 0;
} elseif ($field->function === SelectField::MIN) {
} elseif ($field->function === AggregateFunction::MIN) {
$aggregatedEntry[$key] = min($aggregatedEntry[$key] ?? PHP_INT_MAX, $entry[$field->key] ?? PHP_INT_MAX);
} elseif ($field->function === SelectField::MAX) {
} elseif ($field->function === AggregateFunction::MAX) {
$aggregatedEntry[$key] = max($aggregatedEntry[$key] ?? PHP_INT_MIN, $entry[$field->key] ?? PHP_INT_MIN);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/GenericModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Aternos\Model\Driver\Mysqli\Mysqli;
use Aternos\Model\Driver\Redis\Redis;
use Aternos\Model\Driver\Test\TestDriver;
use Aternos\Model\Query\Conjunction;
use Aternos\Model\Query\CountField;
use Aternos\Model\Query\DeleteQuery;
use Aternos\Model\Query\GroupField;
Expand Down Expand Up @@ -455,7 +456,7 @@ public static function query(Query $query): QueryResult
$query->modelClassName = static::class;

if (static::$filters !== null && count(static::$filters) > 0) {
$wrappedWhereGroup = new WhereGroup(conjunction: WhereGroup::AND);
$wrappedWhereGroup = new WhereGroup(conjunction: Conjunction::AND);
foreach (static::$filters as $key => $value) {
$wrappedWhereGroup->add(new WhereCondition($key, $value));
}
Expand Down
12 changes: 12 additions & 0 deletions src/Query/AggregateFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Aternos\Model\Query;

enum AggregateFunction
{
case COUNT;
case SUM;
case AVERAGE;
case MIN;
case MAX;
}
2 changes: 1 addition & 1 deletion src/Query/AverageField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
*/
class AverageField extends SelectField
{
public ?int $function = self::AVERAGE;
public ?AggregateFunction $function = AggregateFunction::AVERAGE;
}
9 changes: 9 additions & 0 deletions src/Query/Conjunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Aternos\Model\Query;

enum Conjunction
{
case AND;
case OR;
}
2 changes: 1 addition & 1 deletion src/Query/CountField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CountField extends SelectField
{
public const string COUNT_FIELD = "count";

public ?int $function = self::COUNT;
public ?AggregateFunction $function = AggregateFunction::COUNT;
public ?string $alias = self::COUNT_FIELD;

public function __construct()
Expand Down
9 changes: 9 additions & 0 deletions src/Query/Direction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Aternos\Model\Query;

enum Direction
{
case ASCENDING;
case DESCENDING;
}
43 changes: 16 additions & 27 deletions src/Query/Generator/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Aternos\Model\Query\Generator;

use Aternos\Model\Query\{DeleteQuery,
use Aternos\Model\Query\{AggregateFunction,
Conjunction,
DeleteQuery,
Direction,
OrderField,
Query,
SelectField,
Expand Down Expand Up @@ -140,9 +143,8 @@ private function generateWhere(Query $query, WhereCondition|WhereGroup|null $whe
return $field . " " . $where->operator . " " . $value;
} elseif ($where instanceof WhereGroup) {
$conjunction = match ($where->conjunction) {
WhereGroup:: AND => " AND ",
WhereGroup:: OR => " OR ",
default => throw new UnexpectedValueException("Invalid conjunction: " . $where->conjunction),
Conjunction::AND => " AND ",
Conjunction::OR => " OR "
};

$whereStrings = [];
Expand Down Expand Up @@ -172,9 +174,8 @@ private function generateOrder(Query $query): string
foreach ($orderFields as $orderField) {
/** @var OrderField $orderField */
$direction = match ($orderField->direction) {
OrderField::ASCENDING => "ASC",
OrderField::DESCENDING => "DESC",
default => throw new UnexpectedValueException("Invalid direction: " . $orderField->direction),
Direction::ASCENDING => "ASC",
Direction::DESCENDING => "DESC"
};

if ($orderField->raw) {
Expand Down Expand Up @@ -206,26 +207,14 @@ private function generateFields(Query $query): string
if ($field->raw === true) {
$fieldStrings[] = $field->key;
} else {
$fieldString = "";
if ($field->function !== null) {
switch ($field->function) {
case SelectField::COUNT:
$fieldString .= "COUNT(";
break;
case SelectField::SUM:
$fieldString .= "SUM(";
break;
case SelectField::AVERAGE:
$fieldString .= "AVG(";
break;
case SelectField::MIN:
$fieldString .= "MIN(";
break;
case SelectField::MAX:
$fieldString .= "MAX(";
break;
}
}
$fieldString = match($field->function) {
AggregateFunction::COUNT => "COUNT(",
AggregateFunction::SUM => "SUM(",
AggregateFunction::AVERAGE => "AVG(",
AggregateFunction::MIN => "MIN(",
AggregateFunction::MAX => "MAX(",
default => ""
};

if ($field->key !== "*") {
$fieldString .= $this->columnEnclosure . $field->key . $this->columnEnclosure;
Expand Down
2 changes: 1 addition & 1 deletion src/Query/MaxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class MaxField extends SelectField
{
public ?int $function = self::MAX;
public ?AggregateFunction $function = AggregateFunction::MAX;

/**
* MaxField constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/Query/MinField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class MinField extends SelectField
{
public ?int $function = self::MIN;
public ?AggregateFunction $function = AggregateFunction::MIN;

/**
* MinField constructor.
Expand Down
35 changes: 10 additions & 25 deletions src/Query/OrderField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,26 @@
*/
class OrderField
{
/**
* Direction constants
*/
const int ASCENDING = 0;
const int DESCENDING = 1;

/**
* Field name to order by
*
* @var string|null
*/
public ?string $field = null;
/** @deprecated */
const Direction ASCENDING = Direction::ASCENDING;
/** @deprecated */
const Direction DESCENDING = Direction::DESCENDING;

/**
* @var bool
*/
public bool $raw = false;

/**
* Order direction
*
* @var int
*/
public int $direction = self::ASCENDING;

/**
* OrderField constructor.
*
* @param string|null $field
* @param int $direction
* @param Direction $direction
*/
public function __construct(?string $field = null, int $direction = self::ASCENDING)
public function __construct(
public ?string $field = null,
public Direction $direction = Direction::ASCENDING)
{
$this->field = $field;
$this->direction = $direction;
}

/**
Expand All @@ -57,10 +42,10 @@ public function setRaw(bool $raw): OrderField
}

/**
* @param int $direction
* @param Direction $direction
* @return OrderField
*/
public function setDirection(int $direction): OrderField
public function setDirection(Direction $direction): OrderField
{
$this->direction = $direction;
return $this;
Expand Down
6 changes: 3 additions & 3 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ public function orderBy(array $order): static
continue;
}

if (!is_int($value)) {
if (!$value instanceof Direction) {
$value = match (strtoupper($value)) {
"ASCENDING", "ASC" => OrderField::ASCENDING,
"DESCENDING", "DESC" => OrderField::DESCENDING,
"ASCENDING", "ASC" => Direction::ASCENDING,
"DESCENDING", "DESC" => Direction::DESCENDING,
default => throw new InvalidArgumentException('Argument $order contains invalid order direction: ' . $value),
};
}
Expand Down
34 changes: 18 additions & 16 deletions src/Query/SelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
*/
class SelectField extends Field
{
const int COUNT = 0,
SUM = 1,
AVERAGE = 2,
MIN = 3,
MAX = 4;
/** @deprecated */
const AggregateFunction COUNT = AggregateFunction::COUNT;
/** @deprecated */
const AggregateFunction SUM = AggregateFunction::SUM;
/** @deprecated */
const AggregateFunction AVERAGE = AggregateFunction::AVERAGE;
/** @deprecated */
const AggregateFunction MIN = AggregateFunction::MIN;
/** @deprecated */
const AggregateFunction MAX = AggregateFunction::MAX;

/**
* @var string|null
*/
public ?string $alias = null;

/**
* @var int|null
*/
public ?int $function = null;
public ?AggregateFunction $function = null;

/**
* @var bool
Expand All @@ -32,29 +34,29 @@ class SelectField extends Field

/**
* @param string|null $alias
* @return SelectField
* @return $this
*/
public function setAlias(?string $alias): SelectField
public function setAlias(?string $alias): static
{
$this->alias = $alias;
return $this;
}

/**
* @param int|null $function
* @return SelectField
* @param AggregateFunction $function
* @return $this
*/
public function setFunction(?int $function): SelectField
public function setFunction(AggregateFunction $function): static
{
$this->function = $function;
return $this;
}

/**
* @param bool $raw
* @return SelectField
* @return $this
*/
public function setRaw(bool $raw = true): SelectField
public function setRaw(bool $raw = true): static
{
$this->raw = $raw;
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Query/SumField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class SumField extends SelectField
{
public ?int $function = self::SUM;
public ?AggregateFunction $function = AggregateFunction::SUM;

/**
* Field constructor.
Expand Down
Loading
Loading