Passed
Push — master ( 266f6d...355071 )
by Jonas
12:15
created

compileCycleDetectionRecursiveSelect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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