web-base/Core/Driver/SQL/Condition/CondIn.class.php

47 lines
1.2 KiB
PHP
Raw Normal View History

<?php
2022-11-18 18:06:46 +01:00
namespace Core\Driver\SQL\Condition;
use Core\Driver\SQL\Query\Select;
use Core\Driver\SQL\SQL;
class CondIn extends Condition {
2023-01-16 21:47:23 +01:00
private mixed $needle;
private mixed $haystack;
2021-12-08 16:53:43 +01:00
public function __construct($needle, $haystack) {
$this->needle = $needle;
$this->haystack = $haystack;
}
2021-12-08 16:53:43 +01:00
public function getNeedle() { return $this->needle; }
public function getHaystack() { return $this->haystack; }
function getExpression(SQL $sql, array &$params): string {
2023-01-16 21:47:23 +01:00
$needle = $sql->addValue($this->needle, $params);
if (is_array($this->haystack)) {
if (!empty($this->haystack)) {
$values = array();
foreach ($this->haystack as $value) {
$values[] = $sql->addValue($value, $params);
}
2023-01-16 21:47:23 +01:00
$values = implode(",", $values);
$values = "($values)";
} else {
$sql->getLogger()->error("Empty haystack for in-expression with needle: " . $needle);
return false;
}
} else if ($this->haystack instanceof Select) {
$values = $this->haystack->getExpression($sql, $params);
} else {
2023-01-16 21:47:23 +01:00
$sql->getLogger()->error("Unsupported in-expression value: " . get_class($this->haystack));
return false;
}
2023-01-16 21:47:23 +01:00
return "$needle IN $values";
}
}