1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Database |
5
|
|
|
* |
6
|
|
|
* Platine Database is the abstraction layer using PDO with support of query and schema builder |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Database |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file Where.php |
33
|
|
|
* |
34
|
|
|
* The class for where query statement |
35
|
|
|
* |
36
|
|
|
* @package Platine\Database\Query |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Database\Query; |
48
|
|
|
|
49
|
|
|
use Closure; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @class Where |
53
|
|
|
* @package Platine\Database\Query |
54
|
|
|
*/ |
55
|
|
|
class Where |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* @var string|Expression |
59
|
|
|
*/ |
60
|
|
|
protected string|Expression $column; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected string $separator; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Where constructor. |
69
|
|
|
* @param WhereStatement $whereStatement |
70
|
|
|
* @param QueryStatement $queryStatement |
71
|
|
|
*/ |
72
|
|
|
public function __construct( |
73
|
|
|
protected WhereStatement $whereStatement, |
74
|
|
|
protected QueryStatement $queryStatement |
75
|
|
|
) { |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string|Expression|Closure $column |
80
|
|
|
* @param string $separator |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function init(string|Expression|Closure $column, string $separator): self |
84
|
|
|
{ |
85
|
|
|
if ($column instanceof Closure) { |
86
|
|
|
$column = Expression::fromClosure($column); |
87
|
|
|
} |
88
|
|
|
$this->column = $column; |
89
|
|
|
$this->separator = $separator; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $literal |
96
|
|
|
* @param mixed $value |
97
|
|
|
* @param bool $isColumn |
98
|
|
|
* @return WhereStatement|Select|Delete|Update |
99
|
|
|
*/ |
100
|
|
|
public function literal( |
101
|
|
|
string $literal, |
102
|
|
|
mixed $value, |
103
|
|
|
bool $isColumn = false |
104
|
|
|
): WhereStatement|Select|Delete|Update { |
105
|
|
|
return $this->addCondition($value, $literal, $isColumn); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param mixed $value |
110
|
|
|
* @param bool $isColumn |
111
|
|
|
* @return WhereStatement|Select|Delete|Update |
112
|
|
|
*/ |
113
|
|
|
public function is(mixed $value, bool $isColumn = false): WhereStatement|Select|Delete|Update |
114
|
|
|
{ |
115
|
|
|
return $this->addCondition($value, '=', $isColumn); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param mixed $value |
120
|
|
|
* @param bool $isColumn |
121
|
|
|
* @return WhereStatement|Select|Delete|Update |
122
|
|
|
*/ |
123
|
|
|
public function eq(mixed $value, bool $isColumn = false): WhereStatement|Select|Delete|Update |
124
|
|
|
{ |
125
|
|
|
return $this->is($value, $isColumn); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param mixed $value |
130
|
|
|
* @param bool $isColumn |
131
|
|
|
* @return WhereStatement|Select|Delete|Update |
132
|
|
|
*/ |
133
|
|
|
public function isNot(mixed $value, bool $isColumn = false): WhereStatement|Select|Delete|Update |
134
|
|
|
{ |
135
|
|
|
return $this->addCondition($value, '!=', $isColumn); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param mixed $value |
140
|
|
|
* @param bool $isColumn |
141
|
|
|
* @return WhereStatement|Select|Delete|Update |
142
|
|
|
*/ |
143
|
|
|
public function neq(mixed $value, bool $isColumn = false): WhereStatement|Select|Delete|Update |
144
|
|
|
{ |
145
|
|
|
return $this->isNot($value, $isColumn); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param mixed $value |
150
|
|
|
* @param bool $isColumn |
151
|
|
|
* @return WhereStatement|Select|Delete|Update |
152
|
|
|
*/ |
153
|
|
|
public function lt(mixed $value, bool $isColumn = false): WhereStatement|Select|Delete|Update |
154
|
|
|
{ |
155
|
|
|
return $this->addCondition($value, '<', $isColumn); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param mixed $value |
160
|
|
|
* @param bool $isColumn |
161
|
|
|
* @return WhereStatement|Select|Delete|Update |
162
|
|
|
*/ |
163
|
|
|
public function gt(mixed $value, bool $isColumn = false): WhereStatement|Select|Delete|Update |
164
|
|
|
{ |
165
|
|
|
return $this->addCondition($value, '>', $isColumn); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param mixed $value |
170
|
|
|
* @param bool $isColumn |
171
|
|
|
* @return WhereStatement|Select|Delete|Update |
172
|
|
|
*/ |
173
|
|
|
public function lte(mixed $value, bool $isColumn = false): WhereStatement|Select|Delete|Update |
174
|
|
|
{ |
175
|
|
|
return $this->addCondition($value, '<=', $isColumn); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param mixed $value |
180
|
|
|
* @param bool $isColumn |
181
|
|
|
* @return WhereStatement|Select|Delete|Update |
182
|
|
|
*/ |
183
|
|
|
public function gte(mixed $value, bool $isColumn = false): WhereStatement|Select|Delete|Update |
184
|
|
|
{ |
185
|
|
|
return $this->addCondition($value, '>=', $isColumn); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param mixed $value1 |
190
|
|
|
* @param mixed $value2 |
191
|
|
|
* @return WhereStatement|Select|Delete|Update |
192
|
|
|
*/ |
193
|
|
|
public function between(mixed $value1, mixed $value2): WhereStatement|Select|Delete|Update |
194
|
|
|
{ |
195
|
|
|
return $this->addBetweenCondition($value1, $value2, false); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param mixed $value1 |
200
|
|
|
* @param mixed $value2 |
201
|
|
|
* @return WhereStatement|Select|Delete|Update |
202
|
|
|
*/ |
203
|
|
|
public function notBetween(mixed $value1, mixed $value2): WhereStatement|Select|Delete|Update |
204
|
|
|
{ |
205
|
|
|
return $this->addBetweenCondition($value1, $value2, true); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param mixed $value |
210
|
|
|
* @return WhereStatement|Select|Delete|Update |
211
|
|
|
*/ |
212
|
|
|
public function like(mixed $value): WhereStatement|Select|Delete|Update |
213
|
|
|
{ |
214
|
|
|
return $this->addLikeCondition($value, false); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param mixed $value |
219
|
|
|
* @return WhereStatement|Select|Delete|Update |
220
|
|
|
*/ |
221
|
|
|
public function notLike(mixed $value): WhereStatement|Select|Delete|Update |
222
|
|
|
{ |
223
|
|
|
return $this->addLikeCondition($value, true); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param array<int, mixed>|Closure $value |
228
|
|
|
* @return WhereStatement |
229
|
|
|
*/ |
230
|
|
|
public function in(array|Closure $value): WhereStatement |
231
|
|
|
{ |
232
|
|
|
return $this->addInCondition($value, false); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param array<int, mixed>|Closure $value |
237
|
|
|
* @return WhereStatement |
238
|
|
|
*/ |
239
|
|
|
public function notIn(array|Closure $value): WhereStatement |
240
|
|
|
{ |
241
|
|
|
return $this->addInCondition($value, true); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return WhereStatement|Select|Delete|Update |
246
|
|
|
*/ |
247
|
|
|
public function isNull(): WhereStatement|Select|Delete|Update |
248
|
|
|
{ |
249
|
|
|
return $this->addNullCondition(false); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return WhereStatement|Select|Delete|Update |
254
|
|
|
*/ |
255
|
|
|
public function isNotNull(): WhereStatement|Select|Delete|Update |
256
|
|
|
{ |
257
|
|
|
return $this->addNullCondition(true); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return WhereStatement|Select|Delete|Update |
262
|
|
|
*/ |
263
|
|
|
public function nop(): WhereStatement|Select|Delete|Update |
264
|
|
|
{ |
265
|
|
|
$this->queryStatement->addWhereNop($this->column, $this->separator); |
266
|
|
|
|
267
|
|
|
return $this->whereStatement; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @inheritDoc |
272
|
|
|
*/ |
273
|
|
|
public function __clone(): void |
274
|
|
|
{ |
275
|
|
|
if ($this->column instanceof Expression) { |
276
|
|
|
$this->column = clone $this->column; |
277
|
|
|
} |
278
|
|
|
$this->queryStatement = clone $this->queryStatement; |
279
|
|
|
$this->whereStatement = new WhereStatement($this->queryStatement); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param mixed $value |
284
|
|
|
* @param string $operator |
285
|
|
|
* @param bool $isColumn |
286
|
|
|
* @return WhereStatement|Select|Delete|Update |
287
|
|
|
*/ |
288
|
|
|
protected function addCondition( |
289
|
|
|
mixed $value, |
290
|
|
|
string $operator, |
291
|
|
|
bool $isColumn = false |
292
|
|
|
): WhereStatement|Select|Delete|Update { |
293
|
|
|
if ($isColumn && is_string($value)) { |
294
|
|
|
$value = function (Expression $expr) use ($value) { |
295
|
|
|
return $expr->column($value); |
296
|
|
|
}; |
297
|
|
|
} |
298
|
|
|
$this->queryStatement->addWhere( |
299
|
|
|
$this->column, |
300
|
|
|
$value, |
301
|
|
|
$operator, |
302
|
|
|
$this->separator |
303
|
|
|
); |
304
|
|
|
|
305
|
|
|
return $this->whereStatement; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param mixed $value1 |
310
|
|
|
* @param mixed $value2 |
311
|
|
|
* @param bool $not |
312
|
|
|
* @return WhereStatement|Select|Delete|update |
313
|
|
|
*/ |
314
|
|
|
protected function addBetweenCondition( |
315
|
|
|
mixed $value1, |
316
|
|
|
mixed $value2, |
317
|
|
|
bool $not |
318
|
|
|
): WhereStatement|Select|Delete|update { |
319
|
|
|
$this->queryStatement->addWhereBetween( |
320
|
|
|
$this->column, |
321
|
|
|
$value1, |
322
|
|
|
$value2, |
323
|
|
|
$this->separator, |
324
|
|
|
$not |
325
|
|
|
); |
326
|
|
|
|
327
|
|
|
return $this->whereStatement; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @param string $pattern |
332
|
|
|
* @param bool $not |
333
|
|
|
* @return WhereStatement|Select|Delete|update |
334
|
|
|
*/ |
335
|
|
|
protected function addLikeCondition(string $pattern, bool $not): WhereStatement|Select|Delete|update |
336
|
|
|
{ |
337
|
|
|
$this->queryStatement->addWhereLike( |
338
|
|
|
$this->column, |
339
|
|
|
$pattern, |
340
|
|
|
$this->separator, |
341
|
|
|
$not |
342
|
|
|
); |
343
|
|
|
|
344
|
|
|
return $this->whereStatement; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param mixed $value |
349
|
|
|
* @param bool $not |
350
|
|
|
* @return WhereStatement|Select|Delete|update |
351
|
|
|
*/ |
352
|
|
|
protected function addInCondition(mixed $value, bool $not): WhereStatement|Select|Delete|update |
353
|
|
|
{ |
354
|
|
|
$this->queryStatement->addWhereIn( |
355
|
|
|
$this->column, |
356
|
|
|
$value, |
357
|
|
|
$this->separator, |
358
|
|
|
$not |
359
|
|
|
); |
360
|
|
|
|
361
|
|
|
return $this->whereStatement; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param bool $not |
366
|
|
|
* @return WhereStatement|Select|Delete|Update |
367
|
|
|
*/ |
368
|
|
|
protected function addNullCondition(bool $not): WhereStatement|Select|Delete|Update |
369
|
|
|
{ |
370
|
|
|
$this->queryStatement->addWhereNull( |
371
|
|
|
$this->column, |
372
|
|
|
$this->separator, |
373
|
|
|
$not |
374
|
|
|
); |
375
|
|
|
|
376
|
|
|
return $this->whereStatement; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|