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
|
141 |
|
public function compileInitialPath($column, $alias) |
20
|
|
|
{ |
21
|
141 |
|
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
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
141 |
|
public function compileRecursivePath($column, $alias) |
32
|
|
|
{ |
33
|
141 |
|
return $this->wrap($alias).' || ? || '.$this->wrap($column); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the recursive path bindings. |
38
|
|
|
* |
39
|
|
|
* @param string $separator |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
141 |
|
public function getRecursivePathBindings($separator) |
43
|
|
|
{ |
44
|
141 |
|
return [$separator]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Select a concatenated list of paths. |
49
|
|
|
* |
50
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
51
|
|
|
* @param string $expression |
52
|
|
|
* @param string $column |
53
|
|
|
* @param string $pathSeparator |
54
|
|
|
* @param string $listSeparator |
55
|
|
|
* @return \Illuminate\Database\Query\Builder |
56
|
|
|
*/ |
57
|
16 |
|
public function selectPathList(Builder $query, $expression, $column, $pathSeparator, $listSeparator) |
58
|
|
|
{ |
59
|
16 |
|
return $query->selectRaw( |
60
|
16 |
|
'group_concat('.$this->wrap($column).', ?)', |
61
|
16 |
|
[$listSeparator] |
62
|
16 |
|
)->from($expression); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Compile a pivot column null value. |
67
|
|
|
* |
68
|
|
|
* @param string $type |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
26 |
|
public function compilePivotColumnNullValue(string $type): string |
72
|
|
|
{ |
73
|
26 |
|
return 'null'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Compile a cycle detection clause. |
78
|
|
|
* |
79
|
|
|
* @param string $localKey |
80
|
|
|
* @param string $path |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
20 |
|
public function compileCycleDetection(string $localKey, string $path): string |
84
|
|
|
{ |
85
|
20 |
|
$localKey = $this->wrap($localKey); |
86
|
20 |
|
$path = $this->wrap($path); |
87
|
|
|
|
88
|
20 |
|
return "instr($path, $localKey || ?) > 0 OR instr($path, ? || $localKey || ?) > 0"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the cycle detection bindings. |
93
|
|
|
* |
94
|
|
|
* @param string $pathSeparator |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
20 |
|
public function getCycleDetectionBindings(string $pathSeparator): array |
98
|
|
|
{ |
99
|
20 |
|
return [$pathSeparator, $pathSeparator, $pathSeparator]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|