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
|
|
|
use Cycle\Database\Exception\BuilderException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @internal |
25
|
|
|
* |
26
|
|
|
* @psalm-internal Cycle\Database\Driver\MySQL |
27
|
|
|
*/ |
28
|
|
|
trait WhereJsonTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param non-empty-string $column |
|
|
|
|
32
|
|
|
* @param non-empty-string $method |
33
|
|
|
* @param array<non-empty-string, mixed> $params |
34
|
|
|
*/ |
35
|
|
|
protected function buildJsonInjection( |
36
|
|
|
string $column, |
37
|
|
|
mixed $value, |
38
|
|
|
string $method, |
39
|
|
|
array $params, |
40
|
|
|
): array { |
41
|
|
|
return match ($method) { |
42
|
|
|
'whereJson', 'orWhereJson' => [new CompileJson($column), $value], |
43
|
|
|
'whereJsonContains', 'orWhereJsonContains' => [new CompileJsonContains( |
44
|
|
|
$column, |
45
|
|
|
Jsoner::toJson($value, $params['encode'], $params['validate']) |
46
|
|
|
)], |
47
|
|
|
'whereJsonDoesntContain', 'orWhereJsonDoesntContain' => [new CompileJsonDoesntContain( |
48
|
|
|
$column, |
49
|
|
|
Jsoner::toJson($value, $params['encode'], $params['validate']) |
50
|
|
|
)], |
51
|
|
|
'whereJsonContainsKey', 'orWhereJsonContainsKey' => [new CompileJsonContainsKey($column)], |
52
|
|
|
'whereJsonDoesntContainKey', 'orWhereJsonDoesntContainKey' => [new CompileJsonDoesntContainKey($column)], |
53
|
|
|
'whereJsonLength', 'orWhereJsonLength' => [new CompileJsonLength($column, $value, $params['operator'])], |
54
|
|
|
default => null, |
55
|
|
|
} ?? throw new BuilderException("This database engine can't handle the `$method` method."); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|