Core Update 1.4.0
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace Driver\SQL;
|
||||
|
||||
use Driver\SQL\Column\Column;
|
||||
use Driver\SQL\Condition\Compare;
|
||||
|
||||
class Join {
|
||||
|
||||
private string $type;
|
||||
@@ -9,13 +12,16 @@ class Join {
|
||||
private string $columnA;
|
||||
private string $columnB;
|
||||
private ?string $tableAlias;
|
||||
private array $conditions;
|
||||
|
||||
public function __construct(string $type, string $table, string $columnA, string $columnB, ?string $tableAlias = null) {
|
||||
public function __construct(string $type, string $table, string $columnA, string $columnB, ?string $tableAlias = null, array $conditions = []) {
|
||||
$this->type = $type;
|
||||
$this->table = $table;
|
||||
$this->columnA = $columnA;
|
||||
$this->columnB = $columnB;
|
||||
$this->tableAlias = $tableAlias;
|
||||
$this->conditions = $conditions;
|
||||
array_unshift($this->conditions , new Compare($columnA, new Column($columnB), "="));
|
||||
}
|
||||
|
||||
public function getType(): string { return $this->type; }
|
||||
@@ -23,5 +29,6 @@ class Join {
|
||||
public function getColumnA(): string { return $this->columnA; }
|
||||
public function getColumnB(): string { return $this->columnB; }
|
||||
public function getTableAlias(): ?string { return $this->tableAlias; }
|
||||
public function getConditions(): array { return $this->conditions; }
|
||||
|
||||
}
|
||||
@@ -316,7 +316,13 @@ class MySQL extends SQL {
|
||||
foreach($table as $t) $tables[] = $this->tableName($t);
|
||||
return implode(",", $tables);
|
||||
} else {
|
||||
return "`$table`";
|
||||
$parts = explode(" ", $table);
|
||||
if (count($parts) === 2) {
|
||||
list ($name, $alias) = $parts;
|
||||
return "`$name` $alias";
|
||||
} else {
|
||||
return "`$table`";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,15 +352,17 @@ class MySQL extends SQL {
|
||||
return mysqli_stat($this->connection);
|
||||
}
|
||||
|
||||
public function createTriggerBody(CreateTrigger $trigger): ?string {
|
||||
public function createTriggerBody(CreateTrigger $trigger, array $parameters = []): ?string {
|
||||
$values = array();
|
||||
|
||||
foreach ($trigger->getProcedure()->getParameters() as $param) {
|
||||
if ($param instanceof CurrentTable) {
|
||||
foreach ($parameters as $paramValue) {
|
||||
if ($paramValue instanceof CurrentTable) {
|
||||
$values[] = $this->getUnsafeValue($trigger->getTable());
|
||||
} else {
|
||||
} elseif ($paramValue instanceof CurrentColumn) {
|
||||
$prefix = ($trigger->getEvent() !== "DELETE" ? "NEW." : "OLD.");
|
||||
$values[] = $this->columnName($prefix . $param->getName());
|
||||
$values[] = $this->columnName($prefix . $paramValue->getName());
|
||||
} else {
|
||||
$values[] = $paramValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ namespace Driver\SQL;
|
||||
|
||||
use \Api\Parameter\Parameter;
|
||||
|
||||
use Api\User\Create;
|
||||
use Driver\SQL\Column\Column;
|
||||
use \Driver\SQL\Column\IntColumn;
|
||||
use \Driver\SQL\Column\SerialColumn;
|
||||
@@ -304,7 +303,13 @@ class PostgreSQL extends SQL {
|
||||
foreach($table as $t) $tables[] = $this->tableName($t);
|
||||
return implode(",", $tables);
|
||||
} else {
|
||||
return "\"$table\"";
|
||||
$parts = explode(" ", $table);
|
||||
if (count($parts) === 2) {
|
||||
list ($name, $alias) = $parts;
|
||||
return "\"$name\" $alias";
|
||||
} else {
|
||||
return "\"$table\"";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,13 +371,10 @@ class PostgreSQL extends SQL {
|
||||
$query .= "END;";
|
||||
$query .= "\$table\$ LANGUAGE plpgsql;";
|
||||
|
||||
var_dump($query);
|
||||
var_dump($params);
|
||||
|
||||
return $this->execute($query, $params);
|
||||
}
|
||||
|
||||
public function createTriggerBody(CreateTrigger $trigger): ?string {
|
||||
public function createTriggerBody(CreateTrigger $trigger, array $params = []): ?string {
|
||||
$procName = $this->tableName($trigger->getProcedure()->getName());
|
||||
return "EXECUTE PROCEDURE $procName()";
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ class CreateTrigger extends Query {
|
||||
private string $time;
|
||||
private string $event;
|
||||
private string $tableName;
|
||||
private array $parameters;
|
||||
private ?CreateProcedure $procedure;
|
||||
|
||||
public function __construct(SQL $sql, string $triggerName) {
|
||||
@@ -19,6 +20,7 @@ class CreateTrigger extends Query {
|
||||
$this->time = "AFTER";
|
||||
$this->tableName = "";
|
||||
$this->event = "";
|
||||
$this->parameters = [];
|
||||
$this->procedure = null;
|
||||
}
|
||||
|
||||
@@ -50,8 +52,9 @@ class CreateTrigger extends Query {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function exec(CreateProcedure $procedure): CreateTrigger {
|
||||
public function exec(CreateProcedure $procedure, array $parameters = []): CreateTrigger {
|
||||
$this->procedure = $procedure;
|
||||
$this->parameters = $parameters;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -69,7 +72,7 @@ class CreateTrigger extends Query {
|
||||
|
||||
$params = array();
|
||||
$query = "CREATE TRIGGER $name $time $event ON $tableName FOR EACH ROW ";
|
||||
$triggerBody = $this->sql->createTriggerBody($this);
|
||||
$triggerBody = $this->sql->createTriggerBody($this, $this->parameters);
|
||||
if ($triggerBody === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Driver\SQL\Query;
|
||||
|
||||
use Driver\SQL\Condition\CondOr;
|
||||
use Driver\SQL\Expression\JsonArrayAgg;
|
||||
use Driver\SQL\Join;
|
||||
use Driver\SQL\SQL;
|
||||
|
||||
@@ -47,13 +48,13 @@ class Select extends Query {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function innerJoin(string $table, string $columnA, string $columnB, ?string $tableAlias = null): Select {
|
||||
$this->joins[] = new Join("INNER", $table, $columnA, $columnB, $tableAlias);
|
||||
public function innerJoin(string $table, string $columnA, string $columnB, ?string $tableAlias = null, array $conditions = []): Select {
|
||||
$this->joins[] = new Join("INNER", $table, $columnA, $columnB, $tableAlias, $conditions);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function leftJoin(string $table, string $columnA, string $columnB, ?string $tableAlias = null): Select {
|
||||
$this->joins[] = new Join("LEFT", $table, $columnA, $columnB, $tableAlias);
|
||||
public function leftJoin(string $table, string $columnA, string $columnB, ?string $tableAlias = null, array $conditions = []): Select {
|
||||
$this->joins[] = new Join("LEFT", $table, $columnA, $columnB, $tableAlias, $conditions);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -113,11 +114,19 @@ class Select extends Query {
|
||||
if (count($value->getSelectValues()) !== 1) {
|
||||
$selectValues[] = "($subSelect)";
|
||||
} else {
|
||||
$columnName = $value->getSelectValues()[0];
|
||||
if(($index = stripos($columnName, " as ")) !== FALSE) {
|
||||
$columnName = substr($columnName, $index + 4);
|
||||
$columnAlias = null;
|
||||
$subSelectColumn = $value->getSelectValues()[0];
|
||||
if (is_string($subSelectColumn) && ($index = stripos($subSelectColumn, " as ")) !== FALSE) {
|
||||
$columnAlias = substr($subSelectColumn, $index + 4);
|
||||
} else if ($subSelectColumn instanceof JsonArrayAgg) {
|
||||
$columnAlias = $subSelectColumn->getAlias();
|
||||
}
|
||||
|
||||
if ($columnAlias) {
|
||||
$selectValues[] = "($subSelect) as $columnAlias";
|
||||
} else {
|
||||
$selectValues[] = "($subSelect)";
|
||||
}
|
||||
$selectValues[] = "($subSelect) as $columnName";
|
||||
}
|
||||
} else {
|
||||
$selectValues[] = $this->sql->addValue($value, $params);
|
||||
@@ -144,11 +153,9 @@ class Select extends Query {
|
||||
foreach ($joins as $join) {
|
||||
$type = $join->getType();
|
||||
$joinTable = $this->sql->tableName($join->getTable());
|
||||
$columnA = $this->sql->columnName($join->getColumnA());
|
||||
$columnB = $this->sql->columnName($join->getColumnB());
|
||||
$tableAlias = ($join->getTableAlias() ? " " . $join->getTableAlias() : "");
|
||||
|
||||
$joinStr .= " $type JOIN $joinTable$tableAlias ON $columnA=$columnB";
|
||||
$condition = $this->sql->buildCondition($join->getConditions(), $params);
|
||||
$joinStr .= " $type JOIN $joinTable$tableAlias ON ($condition)";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,9 +14,9 @@ use Driver\Sql\Condition\CondNull;
|
||||
use Driver\SQL\Condition\CondOr;
|
||||
use Driver\SQL\Condition\Exists;
|
||||
use Driver\SQL\Constraint\Constraint;
|
||||
use \Driver\SQL\Constraint\Unique;
|
||||
use \Driver\SQL\Constraint\PrimaryKey;
|
||||
use \Driver\SQL\Constraint\ForeignKey;
|
||||
use Driver\SQL\Constraint\Unique;
|
||||
use Driver\SQL\Constraint\PrimaryKey;
|
||||
use Driver\SQL\Constraint\ForeignKey;
|
||||
use Driver\SQL\Expression\CaseWhen;
|
||||
use Driver\SQL\Expression\CurrentTimeStamp;
|
||||
use Driver\SQL\Expression\Expression;
|
||||
@@ -176,7 +176,7 @@ abstract class SQL {
|
||||
protected abstract function fetchReturning($res, string $returningCol);
|
||||
public abstract function getColumnDefinition(Column $column): ?string;
|
||||
public abstract function getOnDuplicateStrategy(?Strategy $strategy, &$params): ?string;
|
||||
public abstract function createTriggerBody(CreateTrigger $trigger): ?string;
|
||||
public abstract function createTriggerBody(CreateTrigger $trigger, array $params = []): ?string;
|
||||
public abstract function getProcedureHead(CreateProcedure $procedure): ?string;
|
||||
public abstract function getColumnType(Column $column): ?string;
|
||||
public function getProcedureTail(): string { return ""; }
|
||||
|
||||
Reference in New Issue
Block a user