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

41 lines
1.0 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 {
2021-12-08 16:53:43 +01:00
private $needle;
private $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 {
$haystack = $this->getHaystack();
if (is_array($haystack)) {
$values = array();
foreach ($haystack as $value) {
$values[] = $sql->addValue($value, $params);
}
$values = implode(",", $values);
$values = "($values)";
} else if($haystack instanceof Select) {
$values = $haystack->build($params);
} else {
$sql->getLogger()->error("Unsupported in-expression value: " . get_class($haystack));
return false;
}
return $sql->addValue($this->needle, $params) . " IN $values";
}
}