diff --git a/cli.php b/cli.php index 6511b88..f484120 100644 --- a/cli.php +++ b/cli.php @@ -8,7 +8,7 @@ use Configuration\DatabaseScript; use Driver\SQL\Column\Column; use Driver\SQL\Condition\Compare; use Driver\SQL\Condition\CondIn; -use Driver\SQL\Expression\DateAdd; +use Driver\SQL\Expression\DateSub; use Objects\ConnectionData; use Objects\User; @@ -169,7 +169,8 @@ function handleDatabase(array $argv) { $tables = []; $res = $sql->select("entityId", "tableName") ->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(); $success = ($res !== false); diff --git a/core/Driver/SQL/Condition/Compare.class.php b/core/Driver/SQL/Condition/Compare.class.php index 9a0229c..c9df65b 100644 --- a/core/Driver/SQL/Condition/Compare.class.php +++ b/core/Driver/SQL/Condition/Compare.class.php @@ -5,16 +5,16 @@ namespace Driver\SQL\Condition; class Compare extends Condition { private string $operator; - private $lhs; + private string $column; private $value; - public function __construct($col, $val, string $operator = '=') { + public function __construct(string $col, $val, string $operator = '=') { $this->operator = $operator; - $this->lhs = $col; + $this->column = $col; $this->value = $val; } - public function getLHS() { return $this->lhs; } + public function getColumn(): string { return $this->column; } public function getValue() { return $this->value; } public function getOperator(): string { return $this->operator; } diff --git a/core/Driver/SQL/Expression/DateSub.class.php b/core/Driver/SQL/Expression/DateSub.class.php new file mode 100644 index 0000000..f73e259 --- /dev/null +++ b/core/Driver/SQL/Expression/DateSub.class.php @@ -0,0 +1,23 @@ +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; } + +} \ No newline at end of file diff --git a/core/Driver/SQL/MySQL.class.php b/core/Driver/SQL/MySQL.class.php index a17cf39..5948a8b 100644 --- a/core/Driver/SQL/MySQL.class.php +++ b/core/Driver/SQL/MySQL.class.php @@ -18,6 +18,7 @@ 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\Query\CreateProcedure; use Driver\SQL\Query\CreateTrigger; @@ -195,7 +196,7 @@ class MySQL extends SQL { $columnName = $this->columnName($value->getName()); $updateValues[] = "$leftColumn=VALUES($columnName)"; } else if($value instanceof Add) { - $columnName = $this->columnName($value->getLHS()); + $columnName = $this->columnName($value->getColumn()); $operator = $value->getOperator(); $value = $value->getValue(); $updateValues[] = "$leftColumn=$columnName$operator" . $this->addValue($value, $params); @@ -403,11 +404,12 @@ class MySQL extends SQL { } protected function createExpression(Expression $exp, array &$params) { - if ($exp instanceof DateAdd) { + if ($exp instanceof DateAdd || $exp instanceof DateSub) { $lhs = $this->addValue($exp->getLHS(), $params); $rhs = $this->addValue($exp->getRHS(), $params); $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) { return "NOW()"; } else { diff --git a/core/Driver/SQL/PostgreSQL.class.php b/core/Driver/SQL/PostgreSQL.class.php index 2ed6946..9333bec 100644 --- a/core/Driver/SQL/PostgreSQL.class.php +++ b/core/Driver/SQL/PostgreSQL.class.php @@ -18,6 +18,7 @@ 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\Query\CreateProcedure; use Driver\SQL\Query\CreateTrigger; @@ -155,7 +156,7 @@ class PostgreSQL extends SQL { $columnName = $this->columnName($value->getName()); $updateValues[] = "$leftColumn=EXCLUDED.$columnName"; } else if ($value instanceof Add) { - $columnName = $this->columnName($value->getLHS()); + $columnName = $this->columnName($value->getColumn()); $operator = $value->getOperator(); $value = $value->getValue(); $updateValues[] = "$leftColumn=$columnName$operator" . $this->addValue($value, $params); @@ -419,7 +420,7 @@ class PostgreSQL extends SQL { } protected function createExpression(Expression $exp, array &$params) { - if ($exp instanceof DateAdd) { + if ($exp instanceof DateAdd || $exp instanceof DateSub) { $lhs = $this->addValue($exp->getLHS(), $params); $rhs = $this->addValue($exp->getRHS(), $params); $unit = $exp->getUnit(); @@ -430,7 +431,8 @@ class PostgreSQL extends SQL { $rhs = "$rhs $unit"; } - return "$lhs + $rhs"; + $operator = ($exp instanceof DateAdd ? "+" : "-"); + return "$lhs $operator $rhs"; } else if ($exp instanceof CurrentTimeStamp) { return "CURRENT_TIMESTAMP"; } else { diff --git a/core/Driver/SQL/SQL.class.php b/core/Driver/SQL/SQL.class.php index b97bf8c..646e6af 100644 --- a/core/Driver/SQL/SQL.class.php +++ b/core/Driver/SQL/SQL.class.php @@ -16,7 +16,6 @@ use \Driver\SQL\Constraint\Unique; use \Driver\SQL\Constraint\PrimaryKey; use \Driver\SQL\Constraint\ForeignKey; use Driver\SQL\Expression\CurrentTimeStamp; -use Driver\SQL\Expression\DateAdd; use Driver\SQL\Expression\Expression; use Driver\SQL\Query\AlterTable; use Driver\SQL\Query\CreateProcedure; @@ -246,23 +245,19 @@ abstract class SQL { } return "(" . implode(" OR ", $conditions) . ")"; } else if ($condition instanceof Compare) { - $lhs = $condition->getLHS(); - $lhs = ($lhs instanceof Expression ? - $this->createExpression($lhs, $params) : - $this->columnName($lhs)); - + $column = $this->columnName($condition->getColumn()); $value = $condition->getValue(); $operator = $condition->getOperator(); if ($value === null) { if ($operator === "=") { - return "$lhs IS NULL"; + return "$column IS NULL"; } 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) { return $this->columnName($condition->getValue()); } else if (is_array($condition)) {