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
|
144 |
|
public function compileInitialPath($column, $alias) |
20
|
|
|
{ |
21
|
144 |
|
if ($this->model->isIntegerAttribute($column)) { |
22
|
143 |
|
return 'array['.$this->wrap($column).'] as '.$this->wrap($alias); |
23
|
|
|
} |
24
|
|
|
|
25
|
128 |
|
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
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
144 |
|
public function compileRecursivePath($column, $alias) |
36
|
|
|
{ |
37
|
144 |
|
if ($this->model->isIntegerAttribute($column)) { |
38
|
143 |
|
return $this->wrap($alias).' || '.$this->wrap($column); |
39
|
|
|
} |
40
|
|
|
|
41
|
128 |
|
return $this->wrap($alias) . ' || ' . $this->wrap($column) . '::varchar'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the recursive path bindings. |
46
|
|
|
* |
47
|
|
|
* @param string $separator |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
144 |
|
public function getRecursivePathBindings($separator) |
51
|
|
|
{ |
52
|
144 |
|
return []; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Select a concatenated list of paths. |
57
|
|
|
* |
58
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
59
|
|
|
* @param string $expression |
60
|
|
|
* @param string $column |
61
|
|
|
* @param string $pathSeparator |
62
|
|
|
* @param string $listSeparator |
63
|
|
|
* @return \Illuminate\Database\Query\Builder |
64
|
|
|
*/ |
65
|
16 |
|
public function selectPathList(Builder $query, $expression, $column, $pathSeparator, $listSeparator) |
66
|
|
|
{ |
67
|
16 |
|
return $query->selectRaw( |
68
|
16 |
|
'string_agg(array_to_string('.$this->wrap($column).', ?), ?)', |
69
|
16 |
|
[$pathSeparator, $listSeparator] |
70
|
16 |
|
)->from($expression); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Compile a pivot column null value. |
75
|
|
|
* |
76
|
|
|
* @param string $type |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
26 |
|
public function compilePivotColumnNullValue(string $type): string |
80
|
|
|
{ |
81
|
26 |
|
$cast = match ($type) { |
82
|
26 |
|
'datetime' => 'timestamp', |
83
|
26 |
|
'string' => 'varchar', |
84
|
26 |
|
default => $type, |
85
|
|
|
}; |
86
|
|
|
|
87
|
26 |
|
return "null::$cast"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Compile a cycle detection clause. |
92
|
|
|
* |
93
|
|
|
* @param string $localKey |
94
|
|
|
* @param string $path |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
20 |
|
public function compileCycleDetection(string $localKey, string $path): string |
98
|
|
|
{ |
99
|
20 |
|
$localKey = $this->wrap($localKey); |
100
|
20 |
|
$path = $this->wrap($path); |
101
|
|
|
|
102
|
20 |
|
return "$localKey = any($path)"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the cycle detection bindings. |
107
|
|
|
* |
108
|
|
|
* @param string $pathSeparator |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
20 |
|
public function getCycleDetectionBindings(string $pathSeparator): array |
112
|
|
|
{ |
113
|
20 |
|
return []; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Compile the initial select expression for a cycle detection clause. |
118
|
|
|
* |
119
|
|
|
* @param string $column |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
10 |
|
public function compileCycleDetectionInitialSelect(string $column): string |
123
|
|
|
{ |
124
|
10 |
|
return 'false as ' . $this->wrap($column); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Compile the recursive select expression for a cycle detection clause. |
129
|
|
|
* |
130
|
|
|
* @param string $sql |
131
|
|
|
* @param string $column |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
10 |
|
public function compileCycleDetectionRecursiveSelect(string $sql, string $column): string |
135
|
|
|
{ |
136
|
10 |
|
return $sql; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Compile the stop constraint for a cycle detection clause. |
141
|
|
|
* |
142
|
|
|
* @param string $column |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
10 |
|
public function compileCycleDetectionStopConstraint(string $column): string |
146
|
|
|
{ |
147
|
10 |
|
return 'not ' . $this->wrap($column); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Determine whether the database supports the UNION operator in a recursive expression. |
152
|
|
|
* |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
16 |
|
public function supportsUnionInRecursiveExpression(): bool |
156
|
|
|
{ |
157
|
16 |
|
return true; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|