SqlServerGrammar::compileCycleDetection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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 113
    public function compileInitialPath($column, $alias)
21
    {
22 113
        return 'cast('.$this->wrap($column).' as varchar(max)) 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 113
    public function compileRecursivePath($column, $alias, bool $reverse = false)
34
    {
35 113
        $wrappedColumn = $this->wrap($column);
36 113
        $wrappedAlias = $this->wrap($alias);
37
38 113
        if ($reverse) {
39 95
            return "cast(cast($wrappedColumn as varchar(max)) + ? + $wrappedAlias as varchar(max)) as $wrappedAlias";
40
        }
41
42 113
        return "cast($wrappedAlias + ? + cast($wrappedColumn as varchar(max)) as varchar(max)) as $wrappedAlias";
43
    }
44
45
    /**
46
     * Get the recursive path bindings.
47
     *
48
     * @param string $separator
49
     * @return array
50
     */
51 113
    public function getRecursivePathBindings($separator)
52
    {
53 113
        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 17
    public function selectPathList(Builder $query, $expression, $column, $pathSeparator, $listSeparator)
67
    {
68 17
        return $query->selectRaw(
69 17
            'stuff((select ? + '.$this->wrap($column).' from '.$this->wrapTable($expression)." for xml path('')), 1, ?, '')",
70 17
            [$listSeparator, strlen($listSeparator)]
71 17
        );
72
    }
73
74
    /**
75
     * Compile a pivot column null value.
76
     *
77
     * @param string $typeName
78
     * @param string $type
79
     * @return string
80
     */
81
    public function compilePivotColumnNullValue(string $typeName, string $type): string
82
    {
83
        throw new RuntimeException('This graph relationship feature is not supported on SQL Server.'); // @codeCoverageIgnore
84
    }
85
86
    /**
87
     * Compile a cycle detection clause.
88
     *
89
     * @param string $localKey
90
     * @param string $path
91
     * @return string
92
     */
93
    public function compileCycleDetection(string $localKey, string $path): string
94 8
    {
95
        $localKey = $this->wrap($localKey);
96 8
        $path = $this->wrap($path);
97 8
98
        $castLocalKey = "cast($localKey as varchar(max))";
99 8
100
        return "charindex($castLocalKey + ?, $path) > 0 or charindex(? + $castLocalKey + ?, $path) > 0";
101 8
    }
102
103
    /**
104
     * Get the cycle detection bindings.
105
     *
106
     * @param string $pathSeparator
107
     * @return array
108
     */
109
    public function getCycleDetectionBindings(string $pathSeparator): array
110 8
    {
111
        return [$pathSeparator, $pathSeparator, $pathSeparator];
112 8
    }
113
114
    /**
115
     * Compile the initial select expression for a cycle detection clause.
116
     *
117
     * @param string $column
118
     * @return string
119
     */
120
    public function compileCycleDetectionInitialSelect(string $column): string
121 4
    {
122
        return '0 as ' . $this->wrap($column);
123 4
    }
124
125
    /**
126
     * Compile the recursive select expression for a cycle detection clause.
127
     *
128
     * @param string $sql
129
     * @param string $column
130
     * @return string
131
     */
132
    public function compileCycleDetectionRecursiveSelect(string $sql, string $column): string
133 4
    {
134
        return "case when $sql then 1 else 0 end as " . $this->wrap($column);
135 4
    }
136
137
    /**
138
     * Compile the stop constraint for a cycle detection clause.
139
     *
140
     * @param string $column
141
     * @return string
142
     */
143
    public function compileCycleDetectionStopConstraint(string $column): string
144 4
    {
145
        return $this->wrap($column) . ' = 0';
146 4
    }
147
148
    /**
149
     * Determine whether the database supports the UNION operator in a recursive expression.
150
     *
151
     * @return bool
152
     */
153
    public function supportsUnionInRecursiveExpression(): bool
154 8
    {
155
        return false;
156 8
    }
157
}
158