2021-04-10 01:33:40 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\Driver\SQL\Expression;
|
2021-04-10 01:33:40 +02:00
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Condition\Condition;
|
2023-01-05 22:47:17 +01:00
|
|
|
use Core\Driver\SQL\SQL;
|
2021-04-10 01:33:40 +02:00
|
|
|
|
|
|
|
class CaseWhen extends Expression {
|
|
|
|
|
|
|
|
private Condition $condition;
|
|
|
|
private $trueCase;
|
|
|
|
private $falseCase;
|
|
|
|
|
|
|
|
public function __construct(Condition $condition, $trueCase, $falseCase) {
|
|
|
|
$this->condition = $condition;
|
|
|
|
$this->trueCase = $trueCase;
|
|
|
|
$this->falseCase = $falseCase;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCondition(): Condition { return $this->condition; }
|
|
|
|
public function getTrueCase() { return $this->trueCase; }
|
|
|
|
public function getFalseCase() { return $this->falseCase; }
|
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
function getExpression(SQL $sql, array &$params): string {
|
|
|
|
$condition = $sql->buildCondition($this->getCondition(), $params);
|
|
|
|
|
|
|
|
// psql requires constant values here
|
|
|
|
$trueCase = $sql->addValue($this->getTrueCase(), $params, true);
|
|
|
|
$falseCase = $sql->addValue($this->getFalseCase(), $params, true);
|
|
|
|
|
|
|
|
return "CASE WHEN $condition THEN $trueCase ELSE $falseCase END";
|
|
|
|
}
|
2021-04-10 01:33:40 +02:00
|
|
|
}
|