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