|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentJsonRelations\Grammars; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Database\Query\Expression; |
|
6
|
|
|
use Illuminate\Database\ConnectionInterface; |
|
7
|
|
|
use Illuminate\Database\Query\Grammars\MySqlGrammar as Base; |
|
8
|
|
|
use PDO; |
|
9
|
|
|
|
|
10
|
|
|
class MySqlGrammar extends Base implements JsonGrammar |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Compile a "JSON array" statement into SQL. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $column |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
27 |
|
public function compileJsonArray($column) |
|
19
|
|
|
{ |
|
20
|
27 |
|
return 'json_array('.$this->wrap($column).')'; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Compile a "JSON object" statement into SQL. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $column |
|
27
|
|
|
* @param int $levels |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
24 |
|
public function compileJsonObject($column, $levels) |
|
31
|
|
|
{ |
|
32
|
24 |
|
return str_repeat('json_object(?, ', $levels) |
|
33
|
24 |
|
.$this->wrap($column) |
|
34
|
24 |
|
.str_repeat(')', $levels); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Compile a "JSON value select" statement into SQL. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $column |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
22 |
|
public function compileJsonValueSelect(string $column): string |
|
44
|
|
|
{ |
|
45
|
22 |
|
return $this->wrap($column); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Determine whether the database supports the "member of" operator. |
|
50
|
|
|
* |
|
51
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
152 |
|
public function supportsMemberOf(ConnectionInterface $connection): bool |
|
55
|
|
|
{ |
|
56
|
152 |
|
if ($connection->isMaria()) { |
|
57
|
76 |
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
76 |
|
$version = $connection->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); |
|
61
|
|
|
|
|
62
|
76 |
|
return version_compare($version, '8.0.17') >= 0; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Compile a "member of" statement into SQL. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $column |
|
69
|
|
|
* @param mixed $value |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
76 |
|
public function compileMemberOf(string $column, ?string $objectKey, mixed $value): string |
|
73
|
|
|
{ |
|
74
|
76 |
|
$columnWithKey = $objectKey ? $column . (str_contains($column, '->') ? '[*]' : '') . "->$objectKey" : $column; |
|
75
|
|
|
|
|
76
|
76 |
|
[$field, $path] = $this->wrapJsonFieldAndPath($columnWithKey); |
|
77
|
|
|
|
|
78
|
76 |
|
if ($objectKey && !str_contains($column, '->')) { |
|
79
|
5 |
|
$path = ", '$[*]" . substr($path, 4); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
76 |
|
$sql = $path ? "json_extract($field$path)" : $field; |
|
83
|
|
|
|
|
84
|
76 |
|
if ($value instanceof Expression) { |
|
85
|
49 |
|
return $value->getValue($this) . " member of($sql)"; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
33 |
|
return "? member of($sql)"; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Prepare the bindings for a "member of" statement. |
|
93
|
|
|
* |
|
94
|
|
|
* @param mixed $value |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
76 |
|
public function prepareBindingsForMemberOf(mixed $value): array |
|
98
|
|
|
{ |
|
99
|
76 |
|
return $value instanceof Expression ? [] : [$value]; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|