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