3
namespace Upside\Db\SQL;
7
/** @var string|Expression */
13
protected SQLStatement $sql;
14
protected WhereStatement $statement;
16
public function __construct(WhereStatement $statement, SQLStatement $sql)
19
$this->statement = $statement;
22
public function __clone()
24
if ($this->column instanceof Expression) {
25
$this->column = clone $this->column;
27
$this->sql = clone $this->sql;
28
$this->statement = new WhereStatement($this->sql);
31
public function init(Expression|string|\Closure $column, string $separator): self
33
if ($column instanceof \Closure) {
34
$column = Expression::from_closure($column);
36
$this->column = $column;
37
$this->separator = $separator;
42
protected function add_condition(mixed $value, string $operator, bool $isColumn = false): WhereStatement|Select|Delete|Update
44
if ($isColumn && \is_string($value)) {
45
$value = static function (Expression $expr) use ($value) {
46
$expr->column($value);
49
$this->sql->add_where_condition($this->column, $value, $operator, $this->separator);
51
return $this->statement;
54
protected function add_between_condition(float|int|string $value1, float|int|string $value2, bool $not): WhereStatement|Select|Delete|Update
56
$this->sql->add_where_between_condition($this->column, $value1, $value2, $this->separator, $not);
58
return $this->statement;
61
protected function add_like_condition(string $pattern, bool $not): WhereStatement|Select|Delete|Update
63
$this->sql->add_where_like_condition($this->column, $pattern, $this->separator, $not);
65
return $this->statement;
68
protected function add_in_condition(mixed $value, bool $not): WhereStatement|Select|Delete|Update
70
$this->sql->add_where_in_condition($this->column, $value, $this->separator, $not);
72
return $this->statement;
75
protected function add_null_condition(bool $not): WhereStatement|Select|Delete|Update
77
$this->sql->add_where_null_condition($this->column, $this->separator, $not);
79
return $this->statement;
82
public function is(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
84
return $this->add_condition($value, '=', $is_column);
87
public function is_not(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
89
return $this->add_condition($value, '!=', $is_column);
92
public function less_than(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
94
return $this->add_condition($value, '<', $is_column);
97
public function greater_than(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
99
return $this->add_condition($value, '>', $is_column);
102
public function at_least(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
104
return $this->add_condition($value, '>=', $is_column);
107
public function at_most(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
109
return $this->add_condition($value, '<=', $is_column);
112
public function between(float|int|string $value1, float|int|string $value2): WhereStatement|Select|Delete|Update
114
return $this->add_between_condition($value1, $value2, false);
117
public function not_between(float|int|string $value1, float|int|string $value2): WhereStatement|Select|Delete|Update
119
return $this->add_between_condition($value1, $value2, true);
122
public function like(string $value): WhereStatement|Select|Delete|Update
124
return $this->add_like_condition($value, false);
127
public function not_like(string $value): WhereStatement|Select|Delete|Update
129
return $this->add_like_condition($value, true);
132
public function in(array|\Closure $value): WhereStatement|Select|Delete|Update
134
return $this->add_in_condition($value, false);
137
public function not_in(array|\Closure $value): WhereStatement|Select|Delete|Update
139
return $this->add_in_condition($value, true);
142
public function is_null(): WhereStatement|Select|Delete|Update
144
return $this->add_null_condition(false);
147
public function not_null(): WhereStatement|Select|Delete|Update
149
return $this->add_null_condition(true);
154
public function eq(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
156
return $this->is($value, $is_column);
159
public function ne(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
161
return $this->is_not($value, $is_column);
164
public function lt(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
166
return $this->less_than($value, $is_column);
169
public function gt(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
171
return $this->greater_than($value, $is_column);
174
public function gte(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
176
return $this->at_least($value, $is_column);
179
public function lte(mixed $value, bool $is_column = false): WhereStatement|Select|Delete|Update
181
return $this->at_most($value, $is_column);
184
public function nop(): WhereStatement|Select|Delete|Update
186
$this->sql->add_where_nop($this->column, $this->separator);
188
return $this->statement;