Twig, Tests, AES,
This commit is contained in:
13
core/Driver/SQL/Column/BigIntColumn.php
Normal file
13
core/Driver/SQL/Column/BigIntColumn.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
use Driver\SQL\Column\IntColumn;
|
||||
|
||||
class BigIntColumn extends IntColumn {
|
||||
|
||||
public function __construct(string $name, bool $nullable, $defaultValue, bool $unsigned) {
|
||||
parent::__construct($name, $nullable, $defaultValue, $unsigned);
|
||||
$this->type = "BIGINT";
|
||||
}
|
||||
}
|
||||
@@ -11,5 +11,9 @@ class EnumColumn extends Column {
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
public function addValues(string $value) {
|
||||
$this->values[] = $value;
|
||||
}
|
||||
|
||||
public function getValues(): array { return $this->values; }
|
||||
}
|
||||
|
||||
@@ -4,8 +4,20 @@ namespace Driver\SQL\Column;
|
||||
|
||||
class IntColumn extends Column {
|
||||
|
||||
public function __construct(string $name, bool $nullable = false, $defaultValue = NULL) {
|
||||
protected string $type;
|
||||
private bool $unsigned;
|
||||
|
||||
public function __construct(string $name, bool $nullable = false, $defaultValue = NULL, bool $unsigned = false) {
|
||||
parent::__construct($name, $nullable, $defaultValue);
|
||||
$this->type = "INTEGER";
|
||||
$this->unsigned = $unsigned;
|
||||
}
|
||||
|
||||
public function isUnsigned(): bool {
|
||||
return $this->unsigned;
|
||||
}
|
||||
|
||||
public function getType(): string {
|
||||
return $this->type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,14 @@ namespace Driver\SQL\Condition;
|
||||
|
||||
class CondIn extends Condition {
|
||||
|
||||
private string $column;
|
||||
private $expression;
|
||||
private $needle;
|
||||
private $haystack;
|
||||
|
||||
public function __construct(string $column, $expression) {
|
||||
$this->column = $column;
|
||||
$this->expression = $expression;
|
||||
public function __construct($needle, $haystack) {
|
||||
$this->needle = $needle;
|
||||
$this->haystack = $haystack;
|
||||
}
|
||||
|
||||
public function getColumn(): string { return $this->column; }
|
||||
public function getExpression() { return $this->expression; }
|
||||
public function getNeedle() { return $this->needle; }
|
||||
public function getHaystack() { return $this->haystack; }
|
||||
}
|
||||
18
core/Driver/SQL/Expression/JsonArrayAgg.class.php
Normal file
18
core/Driver/SQL/Expression/JsonArrayAgg.class.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Driver\SQL\Expression;
|
||||
|
||||
class JsonArrayAgg extends Expression {
|
||||
|
||||
private $value;
|
||||
private string $alias;
|
||||
|
||||
public function __construct($value, string $alias) {
|
||||
$this->value = $value;
|
||||
$this->alias = $alias;
|
||||
}
|
||||
|
||||
public function getValue() { return $this->value; }
|
||||
public function getAlias(): string { return $this->alias; }
|
||||
|
||||
}
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
namespace Driver\SQL\Expression;
|
||||
|
||||
use Driver\SQL\Condition\Condition;
|
||||
|
||||
class Sum extends Expression {
|
||||
|
||||
private $value;
|
||||
|
||||
@@ -14,12 +14,12 @@ use \Driver\SQL\Column\DateTimeColumn;
|
||||
use Driver\SQL\Column\BoolColumn;
|
||||
use Driver\SQL\Column\JsonColumn;
|
||||
|
||||
use Driver\SQL\Condition\CondRegex;
|
||||
use Driver\SQL\Expression\Add;
|
||||
use Driver\SQL\Expression\CurrentTimeStamp;
|
||||
use Driver\SQL\Expression\DateAdd;
|
||||
use Driver\SQL\Expression\DateSub;
|
||||
use Driver\SQL\Expression\Expression;
|
||||
use Driver\SQL\Expression\JsonArrayAgg;
|
||||
use Driver\SQL\Query\CreateProcedure;
|
||||
use Driver\SQL\Query\CreateTrigger;
|
||||
use Driver\SQL\Query\Query;
|
||||
@@ -228,7 +228,8 @@ class MySQL extends SQL {
|
||||
} else if($column instanceof SerialColumn) {
|
||||
return "INTEGER AUTO_INCREMENT";
|
||||
} else if($column instanceof IntColumn) {
|
||||
return "INTEGER";
|
||||
$unsigned = $column->isUnsigned() ? " UNSIGNED" : "";
|
||||
return $column->getType() . $unsigned;
|
||||
} else if($column instanceof DateTimeColumn) {
|
||||
return "DATETIME";
|
||||
} else if($column instanceof BoolColumn) {
|
||||
@@ -416,6 +417,10 @@ class MySQL extends SQL {
|
||||
return "$dateFunction($lhs, INTERVAL $rhs $unit)";
|
||||
} else if ($exp instanceof CurrentTimeStamp) {
|
||||
return "NOW()";
|
||||
} else if ($exp instanceof JsonArrayAgg) {
|
||||
$value = $this->addValue($exp->getValue(), $params);
|
||||
$alias = $this->columnName($exp->getAlias());
|
||||
return "JSON_ARRAYAGG($value) as $alias";
|
||||
} else {
|
||||
return parent::createExpression($exp, $params);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ use Driver\SQL\Expression\CurrentTimeStamp;
|
||||
use Driver\SQL\Expression\DateAdd;
|
||||
use Driver\SQL\Expression\DateSub;
|
||||
use Driver\SQL\Expression\Expression;
|
||||
use Driver\SQL\Expression\JsonArrayAgg;
|
||||
use Driver\SQL\Query\CreateProcedure;
|
||||
use Driver\SQL\Query\CreateTrigger;
|
||||
use Driver\SQL\Query\Insert;
|
||||
@@ -219,7 +220,7 @@ class PostgreSQL extends SQL {
|
||||
} else if($column instanceof SerialColumn) {
|
||||
return "SERIAL";
|
||||
} else if($column instanceof IntColumn) {
|
||||
return "INTEGER";
|
||||
return $column->getType();
|
||||
} else if($column instanceof DateTimeColumn) {
|
||||
return "TIMESTAMP";
|
||||
} else if($column instanceof EnumColumn) {
|
||||
@@ -439,6 +440,10 @@ class PostgreSQL extends SQL {
|
||||
return "$lhs $operator $rhs";
|
||||
} else if ($exp instanceof CurrentTimeStamp) {
|
||||
return "CURRENT_TIMESTAMP";
|
||||
} else if ($exp instanceof JsonArrayAgg) {
|
||||
$value = $this->addValue($exp->getValue(), $params);
|
||||
$alias = $this->columnName($exp->getAlias());
|
||||
return "JSON_AGG($value) as $alias";
|
||||
} else {
|
||||
return parent::createExpression($exp, $params);
|
||||
}
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
use Driver\SQL\Column\Column;
|
||||
use Driver\SQL\Column\EnumColumn;
|
||||
use Driver\SQL\Constraint\Constraint;
|
||||
use Driver\SQL\Constraint\ForeignKey;
|
||||
use Driver\SQL\Constraint\PrimaryKey;
|
||||
use Driver\SQL\MySQL;
|
||||
use Driver\SQL\PostgreSQL;
|
||||
use Driver\SQL\SQL;
|
||||
|
||||
class AlterTable extends Query {
|
||||
|
||||
private string $table;
|
||||
private string $action;
|
||||
private $data;
|
||||
|
||||
private ?Column $column;
|
||||
private ?Constraint $constraint;
|
||||
@@ -59,6 +63,13 @@ class AlterTable extends Query {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addToEnum(EnumColumn $column, string $newValue): AlterTable {
|
||||
$this->action = "MODIFY";
|
||||
$this->column = $column;
|
||||
$this->data = $newValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAction(): string { return $this->action; }
|
||||
public function getColumn(): ?Column { return $this->column; }
|
||||
public function getConstraint(): ?Constraint { return $this->constraint; }
|
||||
@@ -82,6 +93,15 @@ class AlterTable extends Query {
|
||||
$query .= $this->sql->columnName($column->getName());
|
||||
} else {
|
||||
// ADD or modify
|
||||
if ($column instanceof EnumColumn) {
|
||||
if ($this->sql instanceof PostgreSQL) {
|
||||
$typeName = $this->sql->getColumnType($column);
|
||||
$value = $this->sql->addValue($this->data, $params);
|
||||
return "ALTER TYPE $typeName ADD VALUE $value";
|
||||
}
|
||||
$column->addValue($this->data);
|
||||
}
|
||||
|
||||
$query .= $this->sql->getColumnDefinition($column);
|
||||
}
|
||||
} else if ($constraint) {
|
||||
|
||||
@@ -46,8 +46,13 @@ class CreateTable extends Query {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addInt(string $name, bool $nullable = false, $defaultValue = NULL): CreateTable {
|
||||
$this->columns[$name] = new IntColumn($name, $nullable, $defaultValue);
|
||||
public function addInt(string $name, bool $nullable = false, $defaultValue = NULL, bool $unsigned = false): CreateTable {
|
||||
$this->columns[$name] = new IntColumn($name, $nullable, $defaultValue, $unsigned);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addBigInt(string $name, bool $nullable = false, $defaultValue = NULL, bool $unsigned = false): CreateTable {
|
||||
$this->columns[$name] = new BigIntColumn($name, $nullable, $defaultValue, $unsigned);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
@@ -278,22 +278,29 @@ abstract class SQL {
|
||||
}
|
||||
} else if($condition instanceof CondIn) {
|
||||
|
||||
$expression = $condition->getExpression();
|
||||
if (is_array($expression)) {
|
||||
$needle = $condition->getNeedle();
|
||||
$haystack = $condition->getHaystack();
|
||||
if (is_array($haystack)) {
|
||||
$values = array();
|
||||
foreach ($expression as $value) {
|
||||
foreach ($haystack as $value) {
|
||||
$values[] = $this->addValue($value, $params);
|
||||
}
|
||||
|
||||
$values = implode(",", $values);
|
||||
} else if($expression instanceof Select) {
|
||||
$values = $expression->build($params);
|
||||
} else if($haystack instanceof Select) {
|
||||
$values = $haystack->build($params);
|
||||
} else {
|
||||
$this->lastError = "Unsupported in-expression value: " . get_class($condition);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->columnName($condition->getColumn()) . " IN ($values)";
|
||||
if ($needle instanceof Column) {
|
||||
$lhs = $this->createExpression($needle, $params);
|
||||
} else {
|
||||
$lhs = $this->addValue($needle, $params);
|
||||
}
|
||||
|
||||
return "$lhs IN ($values)";
|
||||
} else if($condition instanceof CondKeyword) {
|
||||
$left = $condition->getLeftExp();
|
||||
$right = $condition->getRightExp();
|
||||
@@ -315,14 +322,14 @@ abstract class SQL {
|
||||
} else if ($condition instanceof Exists) {
|
||||
return "EXISTS(" .$condition->getSubQuery()->build($params) . ")";
|
||||
} else {
|
||||
$this->lastError = "Unsupported condition type: " . get_class($condition);
|
||||
$this->lastError = "Unsupported condition type: " . gettype($condition);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected function createExpression(Expression $exp, array &$params): ?string {
|
||||
if ($exp instanceof Column) {
|
||||
return $this->columnName($exp);
|
||||
return $this->columnName($exp->getName());
|
||||
} else if ($exp instanceof Query) {
|
||||
return "(" . $exp->build($params) . ")";
|
||||
} else if ($exp instanceof CaseWhen) {
|
||||
@@ -335,7 +342,7 @@ abstract class SQL {
|
||||
return "CASE WHEN $condition THEN $trueCase ELSE $falseCase END";
|
||||
} else if ($exp instanceof Sum) {
|
||||
$value = $this->addValue($exp->getValue(), $params);
|
||||
$alias = $exp->getAlias();
|
||||
$alias = $this->columnName($exp->getAlias());
|
||||
return "SUM($value) AS $alias";
|
||||
} else {
|
||||
$this->lastError = "Unsupported expression type: " . get_class($exp);
|
||||
|
||||
Reference in New Issue
Block a user