sql compare lhs must be col
This commit is contained in:
parent
1812b87fb4
commit
c863a9083e
5
cli.php
5
cli.php
@ -8,7 +8,7 @@ use Configuration\DatabaseScript;
|
|||||||
use Driver\SQL\Column\Column;
|
use Driver\SQL\Column\Column;
|
||||||
use Driver\SQL\Condition\Compare;
|
use Driver\SQL\Condition\Compare;
|
||||||
use Driver\SQL\Condition\CondIn;
|
use Driver\SQL\Condition\CondIn;
|
||||||
use Driver\SQL\Expression\DateAdd;
|
use Driver\SQL\Expression\DateSub;
|
||||||
use Objects\ConnectionData;
|
use Objects\ConnectionData;
|
||||||
use Objects\User;
|
use Objects\User;
|
||||||
|
|
||||||
@ -169,7 +169,8 @@ function handleDatabase(array $argv) {
|
|||||||
$tables = [];
|
$tables = [];
|
||||||
$res = $sql->select("entityId", "tableName")
|
$res = $sql->select("entityId", "tableName")
|
||||||
->from("EntityLog")
|
->from("EntityLog")
|
||||||
->where(new Compare($sql->now(), new DateAdd(new Column("modified"), new Column("lifetime"), "DAY"), ">="))
|
->where(new Compare("modified", new DateSub($sql->now(), new Column("lifetime"), "DAY"), "<="))
|
||||||
|
->dump()
|
||||||
->execute();
|
->execute();
|
||||||
|
|
||||||
$success = ($res !== false);
|
$success = ($res !== false);
|
||||||
|
@ -5,16 +5,16 @@ namespace Driver\SQL\Condition;
|
|||||||
class Compare extends Condition {
|
class Compare extends Condition {
|
||||||
|
|
||||||
private string $operator;
|
private string $operator;
|
||||||
private $lhs;
|
private string $column;
|
||||||
private $value;
|
private $value;
|
||||||
|
|
||||||
public function __construct($col, $val, string $operator = '=') {
|
public function __construct(string $col, $val, string $operator = '=') {
|
||||||
$this->operator = $operator;
|
$this->operator = $operator;
|
||||||
$this->lhs = $col;
|
$this->column = $col;
|
||||||
$this->value = $val;
|
$this->value = $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLHS() { return $this->lhs; }
|
public function getColumn(): string { return $this->column; }
|
||||||
public function getValue() { return $this->value; }
|
public function getValue() { return $this->value; }
|
||||||
public function getOperator(): string { return $this->operator; }
|
public function getOperator(): string { return $this->operator; }
|
||||||
|
|
||||||
|
23
core/Driver/SQL/Expression/DateSub.class.php
Normal file
23
core/Driver/SQL/Expression/DateSub.class.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Driver\SQL\Expression;
|
||||||
|
|
||||||
|
use Driver\SQL\SQL;
|
||||||
|
|
||||||
|
class DateSub extends Expression {
|
||||||
|
|
||||||
|
private Expression $lhs;
|
||||||
|
private Expression $rhs;
|
||||||
|
private string $unit;
|
||||||
|
|
||||||
|
public function __construct(Expression $lhs, Expression $rhs, string $unit) {
|
||||||
|
$this->lhs = $lhs;
|
||||||
|
$this->rhs = $rhs;
|
||||||
|
$this->unit = $unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLHS(): Expression { return $this->lhs; }
|
||||||
|
public function getRHS(): Expression { return $this->rhs; }
|
||||||
|
public function getUnit(): string { return $this->unit; }
|
||||||
|
|
||||||
|
}
|
@ -18,6 +18,7 @@ use Driver\SQL\Condition\CondRegex;
|
|||||||
use Driver\SQL\Expression\Add;
|
use Driver\SQL\Expression\Add;
|
||||||
use Driver\SQL\Expression\CurrentTimeStamp;
|
use Driver\SQL\Expression\CurrentTimeStamp;
|
||||||
use Driver\SQL\Expression\DateAdd;
|
use Driver\SQL\Expression\DateAdd;
|
||||||
|
use Driver\SQL\Expression\DateSub;
|
||||||
use Driver\SQL\Expression\Expression;
|
use Driver\SQL\Expression\Expression;
|
||||||
use Driver\SQL\Query\CreateProcedure;
|
use Driver\SQL\Query\CreateProcedure;
|
||||||
use Driver\SQL\Query\CreateTrigger;
|
use Driver\SQL\Query\CreateTrigger;
|
||||||
@ -195,7 +196,7 @@ class MySQL extends SQL {
|
|||||||
$columnName = $this->columnName($value->getName());
|
$columnName = $this->columnName($value->getName());
|
||||||
$updateValues[] = "$leftColumn=VALUES($columnName)";
|
$updateValues[] = "$leftColumn=VALUES($columnName)";
|
||||||
} else if($value instanceof Add) {
|
} else if($value instanceof Add) {
|
||||||
$columnName = $this->columnName($value->getLHS());
|
$columnName = $this->columnName($value->getColumn());
|
||||||
$operator = $value->getOperator();
|
$operator = $value->getOperator();
|
||||||
$value = $value->getValue();
|
$value = $value->getValue();
|
||||||
$updateValues[] = "$leftColumn=$columnName$operator" . $this->addValue($value, $params);
|
$updateValues[] = "$leftColumn=$columnName$operator" . $this->addValue($value, $params);
|
||||||
@ -403,11 +404,12 @@ class MySQL extends SQL {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function createExpression(Expression $exp, array &$params) {
|
protected function createExpression(Expression $exp, array &$params) {
|
||||||
if ($exp instanceof DateAdd) {
|
if ($exp instanceof DateAdd || $exp instanceof DateSub) {
|
||||||
$lhs = $this->addValue($exp->getLHS(), $params);
|
$lhs = $this->addValue($exp->getLHS(), $params);
|
||||||
$rhs = $this->addValue($exp->getRHS(), $params);
|
$rhs = $this->addValue($exp->getRHS(), $params);
|
||||||
$unit = $exp->getUnit();
|
$unit = $exp->getUnit();
|
||||||
return "DATE_ADD($lhs, INTERVAL $rhs $unit)";
|
$dateFunction = ($exp instanceof DateAdd ? "DATE_ADD" : "DATE_SUB");
|
||||||
|
return "$dateFunction($lhs, INTERVAL $rhs $unit)";
|
||||||
} else if ($exp instanceof CurrentTimeStamp) {
|
} else if ($exp instanceof CurrentTimeStamp) {
|
||||||
return "NOW()";
|
return "NOW()";
|
||||||
} else {
|
} else {
|
||||||
|
@ -18,6 +18,7 @@ use Driver\SQL\Condition\CondRegex;
|
|||||||
use Driver\SQL\Expression\Add;
|
use Driver\SQL\Expression\Add;
|
||||||
use Driver\SQL\Expression\CurrentTimeStamp;
|
use Driver\SQL\Expression\CurrentTimeStamp;
|
||||||
use Driver\SQL\Expression\DateAdd;
|
use Driver\SQL\Expression\DateAdd;
|
||||||
|
use Driver\SQL\Expression\DateSub;
|
||||||
use Driver\SQL\Expression\Expression;
|
use Driver\SQL\Expression\Expression;
|
||||||
use Driver\SQL\Query\CreateProcedure;
|
use Driver\SQL\Query\CreateProcedure;
|
||||||
use Driver\SQL\Query\CreateTrigger;
|
use Driver\SQL\Query\CreateTrigger;
|
||||||
@ -155,7 +156,7 @@ class PostgreSQL extends SQL {
|
|||||||
$columnName = $this->columnName($value->getName());
|
$columnName = $this->columnName($value->getName());
|
||||||
$updateValues[] = "$leftColumn=EXCLUDED.$columnName";
|
$updateValues[] = "$leftColumn=EXCLUDED.$columnName";
|
||||||
} else if ($value instanceof Add) {
|
} else if ($value instanceof Add) {
|
||||||
$columnName = $this->columnName($value->getLHS());
|
$columnName = $this->columnName($value->getColumn());
|
||||||
$operator = $value->getOperator();
|
$operator = $value->getOperator();
|
||||||
$value = $value->getValue();
|
$value = $value->getValue();
|
||||||
$updateValues[] = "$leftColumn=$columnName$operator" . $this->addValue($value, $params);
|
$updateValues[] = "$leftColumn=$columnName$operator" . $this->addValue($value, $params);
|
||||||
@ -419,7 +420,7 @@ class PostgreSQL extends SQL {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function createExpression(Expression $exp, array &$params) {
|
protected function createExpression(Expression $exp, array &$params) {
|
||||||
if ($exp instanceof DateAdd) {
|
if ($exp instanceof DateAdd || $exp instanceof DateSub) {
|
||||||
$lhs = $this->addValue($exp->getLHS(), $params);
|
$lhs = $this->addValue($exp->getLHS(), $params);
|
||||||
$rhs = $this->addValue($exp->getRHS(), $params);
|
$rhs = $this->addValue($exp->getRHS(), $params);
|
||||||
$unit = $exp->getUnit();
|
$unit = $exp->getUnit();
|
||||||
@ -430,7 +431,8 @@ class PostgreSQL extends SQL {
|
|||||||
$rhs = "$rhs $unit";
|
$rhs = "$rhs $unit";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "$lhs + $rhs";
|
$operator = ($exp instanceof DateAdd ? "+" : "-");
|
||||||
|
return "$lhs $operator $rhs";
|
||||||
} else if ($exp instanceof CurrentTimeStamp) {
|
} else if ($exp instanceof CurrentTimeStamp) {
|
||||||
return "CURRENT_TIMESTAMP";
|
return "CURRENT_TIMESTAMP";
|
||||||
} else {
|
} else {
|
||||||
|
@ -16,7 +16,6 @@ use \Driver\SQL\Constraint\Unique;
|
|||||||
use \Driver\SQL\Constraint\PrimaryKey;
|
use \Driver\SQL\Constraint\PrimaryKey;
|
||||||
use \Driver\SQL\Constraint\ForeignKey;
|
use \Driver\SQL\Constraint\ForeignKey;
|
||||||
use Driver\SQL\Expression\CurrentTimeStamp;
|
use Driver\SQL\Expression\CurrentTimeStamp;
|
||||||
use Driver\SQL\Expression\DateAdd;
|
|
||||||
use Driver\SQL\Expression\Expression;
|
use Driver\SQL\Expression\Expression;
|
||||||
use Driver\SQL\Query\AlterTable;
|
use Driver\SQL\Query\AlterTable;
|
||||||
use Driver\SQL\Query\CreateProcedure;
|
use Driver\SQL\Query\CreateProcedure;
|
||||||
@ -246,23 +245,19 @@ abstract class SQL {
|
|||||||
}
|
}
|
||||||
return "(" . implode(" OR ", $conditions) . ")";
|
return "(" . implode(" OR ", $conditions) . ")";
|
||||||
} else if ($condition instanceof Compare) {
|
} else if ($condition instanceof Compare) {
|
||||||
$lhs = $condition->getLHS();
|
$column = $this->columnName($condition->getColumn());
|
||||||
$lhs = ($lhs instanceof Expression ?
|
|
||||||
$this->createExpression($lhs, $params) :
|
|
||||||
$this->columnName($lhs));
|
|
||||||
|
|
||||||
$value = $condition->getValue();
|
$value = $condition->getValue();
|
||||||
$operator = $condition->getOperator();
|
$operator = $condition->getOperator();
|
||||||
|
|
||||||
if ($value === null) {
|
if ($value === null) {
|
||||||
if ($operator === "=") {
|
if ($operator === "=") {
|
||||||
return "$lhs IS NULL";
|
return "$column IS NULL";
|
||||||
} else if ($operator === "!=") {
|
} else if ($operator === "!=") {
|
||||||
return "$lhs IS NOT NULL";
|
return "$column IS NOT NULL";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $lhs . $operator . $this->addValue($value, $params);
|
return $column . $operator . $this->addValue($value, $params);
|
||||||
} else if ($condition instanceof CondBool) {
|
} else if ($condition instanceof CondBool) {
|
||||||
return $this->columnName($condition->getValue());
|
return $this->columnName($condition->getValue());
|
||||||
} else if (is_array($condition)) {
|
} else if (is_array($condition)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user