1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Query\Grammars; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder; |
6
|
|
|
use Illuminate\Database\Query\Grammars\PostgresGrammar as Base; |
7
|
|
|
|
8
|
|
|
class PostgresGrammar extends Base implements ExpressionGrammar |
9
|
|
|
{ |
10
|
|
|
use OrdersByPath; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Compile an initial path. |
14
|
|
|
* |
15
|
|
|
* @param string $column |
16
|
|
|
* @param string $alias |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
190 |
|
public function compileInitialPath($column, $alias) |
20
|
|
|
{ |
21
|
190 |
|
if (is_string($column) && $this->model->isIntegerAttribute($column)) { |
22
|
181 |
|
return 'array['.$this->wrap($column).'] as '.$this->wrap($alias); |
23
|
|
|
} |
24
|
|
|
|
25
|
173 |
|
return 'array[('.$this->wrap($column)." || '')::varchar] as ".$this->wrap($alias); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Compile a recursive path. |
30
|
|
|
* |
31
|
|
|
* @param string $column |
32
|
|
|
* @param string $alias |
33
|
|
|
* @param bool $reverse |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
190 |
|
public function compileRecursivePath($column, $alias, bool $reverse = false) |
37
|
|
|
{ |
38
|
190 |
|
$wrappedColumn = $this->wrap($column); |
39
|
190 |
|
$wrappedAlias = $this->wrap($alias); |
40
|
|
|
|
41
|
190 |
|
if (is_string($column) && !$this->model->isIntegerAttribute($column)) { |
42
|
173 |
|
$wrappedColumn .= '::varchar'; |
43
|
|
|
} |
44
|
|
|
|
45
|
190 |
|
return $reverse ? "$wrappedColumn || $wrappedAlias" : "$wrappedAlias || $wrappedColumn"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the recursive path bindings. |
50
|
|
|
* |
51
|
|
|
* @param string $separator |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
190 |
|
public function getRecursivePathBindings($separator) |
55
|
|
|
{ |
56
|
190 |
|
return []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Select a concatenated list of paths. |
61
|
|
|
* |
62
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
63
|
|
|
* @param string $expression |
64
|
|
|
* @param string $column |
65
|
|
|
* @param string $pathSeparator |
66
|
|
|
* @param string $listSeparator |
67
|
|
|
* @return \Illuminate\Database\Query\Builder |
68
|
|
|
*/ |
69
|
17 |
|
public function selectPathList(Builder $query, $expression, $column, $pathSeparator, $listSeparator) |
70
|
|
|
{ |
71
|
17 |
|
return $query->selectRaw( |
72
|
17 |
|
'string_agg(array_to_string('.$this->wrap($column).', ?), ?)', |
73
|
17 |
|
[$pathSeparator, $listSeparator] |
74
|
17 |
|
)->from($expression); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Compile a pivot column null value. |
79
|
|
|
* |
80
|
|
|
* @param string $typeName |
81
|
|
|
* @param string $type |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function compilePivotColumnNullValue(string $typeName, string $type): string |
85
|
26 |
|
{ |
86
|
|
|
$cast = match ($typeName) { |
87
|
26 |
|
'datetime' => 'timestamp', |
88
|
26 |
|
'string' => 'varchar', |
89
|
26 |
|
default => $typeName, |
90
|
26 |
|
}; |
91
|
26 |
|
|
92
|
|
|
return "null::$cast"; |
93
|
26 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Compile a cycle detection clause. |
97
|
|
|
* |
98
|
|
|
* @param string $localKey |
99
|
|
|
* @param string $path |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function compileCycleDetection(string $localKey, string $path): string |
103
|
28 |
|
{ |
104
|
|
|
$wrappedLocalKey = $this->wrap($localKey); |
105
|
28 |
|
$wrappedPath = $this->wrap($path); |
106
|
28 |
|
|
107
|
|
|
if ($this->model->isIntegerAttribute($localKey)) { |
108
|
28 |
|
return "$wrappedLocalKey = any($wrappedPath)"; |
109
|
20 |
|
} |
110
|
|
|
|
111
|
|
|
return "$wrappedLocalKey::varchar = any($wrappedPath)"; |
112
|
8 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the cycle detection bindings. |
116
|
|
|
* |
117
|
|
|
* @param string $pathSeparator |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function getCycleDetectionBindings(string $pathSeparator): array |
121
|
28 |
|
{ |
122
|
|
|
return []; |
123
|
28 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Compile the initial select expression for a cycle detection clause. |
127
|
|
|
* |
128
|
|
|
* @param string $column |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function compileCycleDetectionInitialSelect(string $column): string |
132
|
14 |
|
{ |
133
|
|
|
return 'false as ' . $this->wrap($column); |
134
|
14 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Compile the recursive select expression for a cycle detection clause. |
138
|
|
|
* |
139
|
|
|
* @param string $sql |
140
|
|
|
* @param string $column |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function compileCycleDetectionRecursiveSelect(string $sql, string $column): string |
144
|
14 |
|
{ |
145
|
|
|
return $sql; |
146
|
14 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Compile the stop constraint for a cycle detection clause. |
150
|
|
|
* |
151
|
|
|
* @param string $column |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function compileCycleDetectionStopConstraint(string $column): string |
155
|
14 |
|
{ |
156
|
|
|
return 'not ' . $this->wrap($column); |
157
|
14 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Determine whether the database supports the UNION operator in a recursive expression. |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function supportsUnionInRecursiveExpression(): bool |
165
|
20 |
|
{ |
166
|
|
|
return true; |
167
|
20 |
|
} |
168
|
|
|
} |
169
|
|
|
|