2020-06-23 22:24:56 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\Driver\SQL\Condition;
|
2020-06-23 22:24:56 +02:00
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
use Core\Driver\SQL\Query\Select;
|
|
|
|
use Core\Driver\SQL\SQL;
|
|
|
|
|
2020-06-23 22:24:56 +02:00
|
|
|
class CondIn extends Condition {
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
private $needle;
|
|
|
|
private $haystack;
|
2020-06-23 22:24:56 +02:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
public function __construct($needle, $haystack) {
|
|
|
|
$this->needle = $needle;
|
|
|
|
$this->haystack = $haystack;
|
2020-06-23 22:24:56 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
public function getNeedle() { return $this->needle; }
|
|
|
|
public function getHaystack() { return $this->haystack; }
|
2023-01-05 22:47:17 +01:00
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
2020-06-23 22:24:56 +02:00
|
|
|
}
|