SQL expression rewrite, Pagination, some frontend stuff

This commit is contained in:
2023-01-05 22:47:17 +01:00
parent 4bfd6754cf
commit 99bfd7e505
61 changed files with 1745 additions and 570 deletions

View File

@@ -0,0 +1,32 @@
<?php
namespace Core\Driver\SQL\Expression;
use Core\Driver\SQL\SQL;
class Alias extends Expression {
private mixed $value;
private string $alias;
public function __construct(mixed $value, string $alias) {
$this->value = $value;
$this->alias = $alias;
}
public function getAlias(): string {
return $this->alias;
}
public function getValue(): mixed {
return $this->value;
}
protected function addValue(SQL $sql, array &$params): string {
return $sql->addValue($this->value, $params);
}
public function getExpression(SQL $sql, array &$params): string {
return $this->addValue($sql, $params) . " AS " . $this->getAlias();
}
}

View File

@@ -3,6 +3,7 @@
namespace Core\Driver\SQL\Expression;
use Core\Driver\SQL\Condition\Condition;
use Core\Driver\SQL\SQL;
class CaseWhen extends Expression {
@@ -20,4 +21,13 @@ class CaseWhen extends Expression {
public function getTrueCase() { return $this->trueCase; }
public function getFalseCase() { return $this->falseCase; }
function getExpression(SQL $sql, array &$params): string {
$condition = $sql->buildCondition($this->getCondition(), $params);
// psql requires constant values here
$trueCase = $sql->addValue($this->getTrueCase(), $params, true);
$falseCase = $sql->addValue($this->getFalseCase(), $params, true);
return "CASE WHEN $condition THEN $trueCase ELSE $falseCase END";
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Core\Driver\SQL\Expression;
use Core\Driver\SQL\SQL;
class Count extends Alias {
public function __construct(mixed $value = "*", string $alias = "count") {
parent::__construct($value, $alias);
}
function addValue(SQL $sql, array &$params): string {
$value = $this->getValue();
if (is_string($value)) {
if ($value === "*") {
return "COUNT(*)";
} else {
return "COUNT(" . $sql->columnName($value) . ")";
}
} else {
return "COUNT(" . $sql->addValue($value, $params) . ")";
}
}
}

View File

@@ -2,6 +2,20 @@
namespace Core\Driver\SQL\Expression;
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\PostgreSQL;
use Core\Driver\SQL\SQL;
use Exception;
class CurrentTimeStamp extends Expression {
function getExpression(SQL $sql, array &$params): string {
if ($sql instanceof MySQL) {
return "NOW()";
} else if ($sql instanceof PostgreSQL) {
return "CURRENT_TIMESTAMP";
} else {
throw new Exception("CurrentTimeStamp Not implemented for driver type: " . get_class($sql));
}
}
}

View File

@@ -2,6 +2,12 @@
namespace Core\Driver\SQL\Expression;
use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\PostgreSQL;
use Core\Driver\SQL\SQL;
use Core\External\PHPMailer\Exception;
class DateAdd extends Expression {
private Expression $lhs;
@@ -18,4 +24,26 @@ class DateAdd extends Expression {
public function getRHS(): Expression { return $this->rhs; }
public function getUnit(): string { return $this->unit; }
function getExpression(SQL $sql, array &$params): string {
if ($sql instanceof MySQL) {
$lhs = $sql->addValue($this->getLHS(), $params);
$rhs = $sql->addValue($this->getRHS(), $params);
$unit = $this->getUnit();
return "DATE_ADD($lhs, INTERVAL $rhs $unit)";
} else if ($sql instanceof PostgreSQL) {
$lhs = $sql->addValue($this->getLHS(), $params);
$rhs = $sql->addValue($this->getRHS(), $params);
$unit = $this->getUnit();
if ($this->getRHS() instanceof Column) {
$rhs = "$rhs * INTERVAL '1 $unit'";
} else {
$rhs = "$rhs $unit";
}
return "$lhs - $rhs";
} else {
throw new Exception("DateAdd Not implemented for driver type: " . get_class($sql));
}
}
}

View File

@@ -2,6 +2,12 @@
namespace Core\Driver\SQL\Expression;
use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\PostgreSQL;
use Core\Driver\SQL\SQL;
use Core\External\PHPMailer\Exception;
class DateSub extends Expression {
private Expression $lhs;
@@ -18,4 +24,26 @@ class DateSub extends Expression {
public function getRHS(): Expression { return $this->rhs; }
public function getUnit(): string { return $this->unit; }
function getExpression(SQL $sql, array &$params): string {
if ($sql instanceof MySQL) {
$lhs = $sql->addValue($this->getLHS(), $params);
$rhs = $sql->addValue($this->getRHS(), $params);
$unit = $this->getUnit();
return "DATE_SUB($lhs, INTERVAL $rhs $unit)";
} else if ($sql instanceof PostgreSQL) {
$lhs = $sql->addValue($this->getLHS(), $params);
$rhs = $sql->addValue($this->getRHS(), $params);
$unit = $this->getUnit();
if ($this->getRHS() instanceof Column) {
$rhs = "$rhs * INTERVAL '1 $unit'";
} else {
$rhs = "$rhs $unit";
}
return "$lhs - $rhs";
} else {
throw new Exception("DateSub Not implemented for driver type: " . get_class($sql));
}
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Core\Driver\SQL\Expression;
use Core\Driver\SQL\SQL;
class Distinct extends Expression {
private mixed $value;
public function __construct(mixed $value) {
$this->value = $value;
}
public function getValue(): mixed {
return $this->value;
}
function getExpression(SQL $sql, array &$params): string {
return "DISTINCT(" . $sql->addValue($this->getValue(), $params) . ")";
}
}

View File

@@ -2,6 +2,10 @@
namespace Core\Driver\SQL\Expression;
use Core\Driver\SQL\SQL;
abstract class Expression {
abstract function getExpression(SQL $sql, array &$params): string;
}

View File

@@ -2,17 +2,29 @@
namespace Core\Driver\SQL\Expression;
use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\PostgreSQL;
use Core\Driver\SQL\SQL;
use Exception;
class JsonArrayAgg extends Expression {
private $value;
private string $alias;
private mixed $value;
public function __construct($value, string $alias) {
public function __construct(mixed $value) {
$this->value = $value;
$this->alias = $alias;
}
public function getValue() { return $this->value; }
public function getAlias(): string { return $this->alias; }
public function getExpression(SQL $sql, array &$params): string {
$value = is_string($this->value) ? new Column($this->value) : $this->value;
$value = $sql->addValue($value, $params);
if ($sql instanceof MySQL) {
return "JSON_ARRAYAGG($value)";
} else if ($sql instanceof PostgreSQL) {
return "JSON_AGG($value)";
} else {
throw new Exception("JsonArrayAgg not implemented for driver type: " . get_class($sql));
}
}
}

View File

@@ -2,17 +2,15 @@
namespace Core\Driver\SQL\Expression;
class Sum extends Expression {
use Core\Driver\SQL\SQL;
private $value;
private string $alias;
class Sum extends Alias {
public function __construct($value, string $alias) {
$this->value = $value;
$this->alias = $alias;
public function __construct(mixed $value, string $alias) {
parent::__construct($value, $alias);
}
public function getValue() { return $this->value; }
public function getAlias(): string { return $this->alias; }
protected function addValue(SQL $sql, array &$params): string {
return "SUM(" . $sql->addValue($this->getValue(), $params) . ")";
}
}