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