1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentJsonRelations\Grammars; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\ConnectionInterface; |
6
|
|
|
use Illuminate\Database\Query\Expression; |
7
|
|
|
use Illuminate\Database\Query\Grammars\PostgresGrammar as Base; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
class PostgresGrammar extends Base implements JsonGrammar |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Compile a "JSON array" statement into SQL. |
14
|
|
|
* |
15
|
|
|
* @param string $column |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public function compileJsonArray($column) |
19
|
|
|
{ |
20
|
|
|
return 'jsonb_build_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
|
|
|
public function compileJsonObject($column, $levels) |
31
|
|
|
{ |
32
|
|
|
$sql = str_repeat('jsonb_build_object(?::text, ', $levels) |
33
|
|
|
.$this->wrap($column) |
34
|
|
|
.str_repeat(')', $levels); |
35
|
|
|
|
36
|
|
|
return $this->compileJsonArray(new Expression($sql)); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Compile a "JSON value select" statement into SQL. |
41
|
|
|
* |
42
|
|
|
* @param string $column |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function compileJsonValueSelect(string $column): string |
46
|
|
|
{ |
47
|
|
|
return $this->wrap($column); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Determine whether the database supports the "member of" operator. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function supportsMemberOf(ConnectionInterface $connection): bool |
57
|
|
|
{ |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Compile a "member of" statement into SQL. |
63
|
|
|
* |
64
|
|
|
* @param string $column |
65
|
|
|
* @param string|null $objectKey |
66
|
|
|
* @param mixed $value |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function compileMemberOf(string $column, ?string $objectKey, mixed $value): string |
70
|
|
|
{ |
71
|
|
|
throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Prepare the bindings for a "member of" statement. |
76
|
|
|
* |
77
|
|
|
* @param mixed $value |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function prepareBindingsForMemberOf(mixed $value): array |
81
|
|
|
{ |
82
|
|
|
throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|