2021-04-08 19:08:05 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\Driver\SQL\Expression;
|
2021-04-08 19:08:05 +02:00
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
use Core\Driver\SQL\Column\Column;
|
|
|
|
use Core\Driver\SQL\MySQL;
|
|
|
|
use Core\Driver\SQL\PostgreSQL;
|
|
|
|
use Core\Driver\SQL\SQL;
|
2024-05-21 12:32:44 +02:00
|
|
|
use Exception;
|
2023-01-05 22:47:17 +01:00
|
|
|
|
2021-04-08 19:08:05 +02:00
|
|
|
class DateAdd 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; }
|
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 19:08:05 +02:00
|
|
|
}
|