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

43 lines
1.2 KiB
PHP
Raw Normal View History

2020-04-02 00:02:51 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Driver\SQL\Condition;
2020-04-02 00:02:51 +02:00
2023-01-09 20:27:01 +01:00
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\SQL;
2020-04-02 00:02:51 +02:00
class Compare extends Condition {
2020-04-03 17:39:58 +02:00
private string $operator;
2021-04-08 19:48:04 +02:00
private string $column;
2023-01-09 20:27:01 +01:00
private mixed $value;
private bool $binary;
2020-04-03 17:39:58 +02:00
2023-01-09 20:27:01 +01:00
public function __construct(string $col, $val, string $operator = '=', bool $binary = false) {
2020-04-02 00:02:51 +02:00
$this->operator = $operator;
2021-04-08 19:48:04 +02:00
$this->column = $col;
2020-04-02 00:02:51 +02:00
$this->value = $val;
2023-01-09 20:27:01 +01:00
$this->binary = $binary;
2020-04-02 00:02:51 +02:00
}
2021-04-08 19:48:04 +02:00
public function getColumn(): string { return $this->column; }
2020-04-02 00:02:51 +02:00
public function getValue() { return $this->value; }
2021-04-02 21:58:06 +02:00
public function getOperator(): string { return $this->operator; }
2020-04-02 00:02:51 +02:00
function getExpression(SQL $sql, array &$params): string {
if ($this->value === null) {
if ($this->operator === "=") {
return $sql->columnName($this->column) . " IS NULL";
} else if ($this->operator === "!=") {
return $sql->columnName($this->column) . " IS NOT NULL";
}
}
2023-01-09 20:27:01 +01:00
$condition = $sql->columnName($this->column) . $this->operator . $sql->addValue($this->value, $params);
if ($this->binary && $sql instanceof MySQL) {
$condition = "BINARY $condition";
}
return $condition;
}
2020-04-03 17:39:58 +02:00
}