web-base/Core/Driver/SQL/Expression/CaseWhen.class.php

33 lines
998 B
PHP
Raw Permalink Normal View History

<?php
2022-11-18 18:06:46 +01:00
namespace Core\Driver\SQL\Expression;
2022-11-18 18:06:46 +01:00
use Core\Driver\SQL\Condition\Condition;
use Core\Driver\SQL\SQL;
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; }
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";
}
}