1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle ORM package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Driver\MySQL\Query\Traits; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Driver\MySQL\Injection\CompileJson; |
15
|
|
|
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContains; |
16
|
|
|
use Cycle\Database\Driver\MySQL\Injection\CompileJsonContainsKey; |
17
|
|
|
use Cycle\Database\Driver\MySQL\Injection\CompileJsonDoesntContain; |
18
|
|
|
use Cycle\Database\Driver\MySQL\Injection\CompileJsonDoesntContainKey; |
19
|
|
|
use Cycle\Database\Driver\MySQL\Injection\CompileJsonLength; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
trait WhereJsonTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param non-empty-string $column |
|
|
|
|
28
|
|
|
* |
29
|
|
|
* @return $this|self |
30
|
|
|
*/ |
31
|
|
|
public function whereJson(string $column, mixed $value): self |
32
|
|
|
{ |
33
|
|
|
$this->registerToken( |
|
|
|
|
34
|
|
|
'AND', |
35
|
|
|
[new CompileJson($column), $value], |
36
|
|
|
$this->whereTokens, |
37
|
|
|
$this->whereWrapper() |
|
|
|
|
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param non-empty-string $column |
|
|
|
|
45
|
|
|
* |
46
|
|
|
* @return $this|self |
47
|
|
|
*/ |
48
|
|
|
public function andWhereJson(string $column, mixed $value): self |
49
|
|
|
{ |
50
|
|
|
return $this->whereJson($column, $value); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param non-empty-string $column |
|
|
|
|
55
|
|
|
* |
56
|
|
|
* @return $this|self |
57
|
|
|
*/ |
58
|
|
|
public function orWhereJson(string $column, mixed $value): self |
59
|
|
|
{ |
60
|
|
|
$this->registerToken( |
61
|
|
|
'OR', |
62
|
|
|
[new CompileJson($column), $value], |
63
|
|
|
$this->whereTokens, |
64
|
|
|
$this->whereWrapper() |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param non-empty-string $column |
|
|
|
|
72
|
|
|
* |
73
|
|
|
* @return $this|self |
74
|
|
|
*/ |
75
|
|
|
public function whereJsonContains(string $column, mixed $value): self |
76
|
|
|
{ |
77
|
|
|
$this->registerToken( |
78
|
|
|
'AND', |
79
|
|
|
[new CompileJsonContains($column, json_validate($value) ? $value : \json_encode($value))], |
80
|
|
|
$this->whereTokens, |
81
|
|
|
$this->whereWrapper() |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param non-empty-string $column |
|
|
|
|
89
|
|
|
* |
90
|
|
|
* @return $this|self |
91
|
|
|
*/ |
92
|
|
|
public function andWhereJsonContains(string $column, mixed $value): self |
93
|
|
|
{ |
94
|
|
|
return $this->whereJsonContains($column, $value); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param non-empty-string $column |
|
|
|
|
99
|
|
|
* |
100
|
|
|
* @return $this|self |
101
|
|
|
*/ |
102
|
|
|
public function orWhereJsonContains(string $column, mixed $value): self |
103
|
|
|
{ |
104
|
|
|
$this->registerToken( |
105
|
|
|
'OR', |
106
|
|
|
[new CompileJsonContains($column, json_validate($value) ? $value : \json_encode($value))], |
107
|
|
|
$this->whereTokens, |
108
|
|
|
$this->whereWrapper() |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param non-empty-string $column |
|
|
|
|
116
|
|
|
* |
117
|
|
|
* @return $this|self |
118
|
|
|
*/ |
119
|
|
|
public function whereJsonDoesntContain(string $column, mixed $value): self |
120
|
|
|
{ |
121
|
|
|
$this->registerToken( |
122
|
|
|
'AND', |
123
|
|
|
[new CompileJsonDoesntContain($column, json_validate($value) ? $value : \json_encode($value))], |
124
|
|
|
$this->whereTokens, |
125
|
|
|
$this->whereWrapper() |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param non-empty-string $column |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @return $this|self |
135
|
|
|
*/ |
136
|
|
|
public function andWhereJsonDoesntContain(string $column, mixed $value): self |
137
|
|
|
{ |
138
|
|
|
return $this->whereJsonDoesntContain($column, $value); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param non-empty-string $column |
|
|
|
|
143
|
|
|
* |
144
|
|
|
* @return $this|self |
145
|
|
|
*/ |
146
|
|
|
public function orWhereJsonDoesntContain(string $column, mixed $value): self |
147
|
|
|
{ |
148
|
|
|
$this->registerToken( |
149
|
|
|
'OR', |
150
|
|
|
[new CompileJsonDoesntContain($column, json_validate($value) ? $value : \json_encode($value))], |
151
|
|
|
$this->whereTokens, |
152
|
|
|
$this->whereWrapper() |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param non-empty-string $column |
|
|
|
|
160
|
|
|
* |
161
|
|
|
* @return $this|self |
162
|
|
|
*/ |
163
|
|
|
public function whereJsonContainsKey(string $column): self |
164
|
|
|
{ |
165
|
|
|
$this->registerToken( |
166
|
|
|
'AND', |
167
|
|
|
[new CompileJsonContainsKey($column)], |
168
|
|
|
$this->whereTokens, |
169
|
|
|
$this->whereWrapper() |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param non-empty-string $column |
|
|
|
|
177
|
|
|
* |
178
|
|
|
* @return $this|self |
179
|
|
|
*/ |
180
|
|
|
public function andWhereJsonContainsKey(string $column): self |
181
|
|
|
{ |
182
|
|
|
return $this->whereJsonContainsKey($column); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param non-empty-string $column |
|
|
|
|
187
|
|
|
* |
188
|
|
|
* @return $this|self |
189
|
|
|
*/ |
190
|
|
|
public function orWhereJsonContainsKey(string $column): self |
191
|
|
|
{ |
192
|
|
|
$this->registerToken( |
193
|
|
|
'OR', |
194
|
|
|
[new CompileJsonContainsKey($column)], |
195
|
|
|
$this->whereTokens, |
196
|
|
|
$this->whereWrapper() |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param non-empty-string $column |
|
|
|
|
204
|
|
|
* |
205
|
|
|
* @return $this|self |
206
|
|
|
*/ |
207
|
|
|
public function whereJsonDoesntContainKey(string $column): self |
208
|
|
|
{ |
209
|
|
|
$this->registerToken( |
210
|
|
|
'AND', |
211
|
|
|
[new CompileJsonDoesntContainKey($column)], |
212
|
|
|
$this->whereTokens, |
213
|
|
|
$this->whereWrapper() |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param non-empty-string $column |
|
|
|
|
221
|
|
|
* |
222
|
|
|
* @return $this|self |
223
|
|
|
*/ |
224
|
|
|
public function andWhereJsonDoesntContainKey(string $column): self |
225
|
|
|
{ |
226
|
|
|
return $this->whereJsonDoesntContainKey($column); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param non-empty-string $column |
|
|
|
|
231
|
|
|
* |
232
|
|
|
* @return $this|self |
233
|
|
|
*/ |
234
|
|
|
public function orWhereJsonDoesntContainKey(string $column): self |
235
|
|
|
{ |
236
|
|
|
$this->registerToken( |
237
|
|
|
'OR', |
238
|
|
|
[new CompileJsonDoesntContainKey($column)], |
239
|
|
|
$this->whereTokens, |
240
|
|
|
$this->whereWrapper() |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param non-empty-string $column |
|
|
|
|
248
|
|
|
* @param int<0, max> $length |
249
|
|
|
* @param non-empty-string $operator |
250
|
|
|
* |
251
|
|
|
* @return $this|self |
252
|
|
|
*/ |
253
|
|
|
public function whereJsonLength(string $column, int $length, string $operator = '='): self |
254
|
|
|
{ |
255
|
|
|
$this->registerToken( |
256
|
|
|
'AND', |
257
|
|
|
[new CompileJsonLength($column, $length, $operator)], |
258
|
|
|
$this->whereTokens, |
259
|
|
|
$this->whereWrapper() |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param non-empty-string $column |
|
|
|
|
267
|
|
|
* @param int<0, max> $length |
268
|
|
|
* @param non-empty-string $operator |
269
|
|
|
* |
270
|
|
|
* @return $this|self |
271
|
|
|
*/ |
272
|
|
|
public function andWhereJsonLength(string $column, int $length, string $operator = '='): self |
273
|
|
|
{ |
274
|
|
|
return $this->whereJsonLength($column, $length, $operator); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param non-empty-string $column |
|
|
|
|
279
|
|
|
* @param int<0, max> $length |
280
|
|
|
* @param non-empty-string $operator |
281
|
|
|
* |
282
|
|
|
* @return $this|self |
283
|
|
|
*/ |
284
|
|
|
public function orWhereJsonLength(string $column, int $length, string $operator = '='): self |
285
|
|
|
{ |
286
|
|
|
$this->registerToken( |
287
|
|
|
'OR', |
288
|
|
|
[new CompileJsonLength($column, $length, $operator)], |
289
|
|
|
$this->whereTokens, |
290
|
|
|
$this->whereWrapper() |
291
|
|
|
); |
292
|
|
|
|
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|