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\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 143
    public function compileInitialPath($column, $alias)
20
    {
21 143
        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 143
    public function compileRecursivePath($column, $alias)
32
    {
33 143
        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 143
    public function getRecursivePathBindings($separator)
43
    {
44 143
        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
    /**
103
     * Compile the initial select expression for a cycle detection clause.
104
     *
105
     * @param string $column
106
     * @return string
107
     */
108 10
    public function compileCycleDetectionInitialSelect(string $column): string
109
    {
110 10
        return 'false as ' . $this->wrap($column);
111
    }
112
113
    /**
114
     * Compile the recursive select expression for a cycle detection clause.
115
     *
116
     * @param string $sql
117
     * @param string $column
118
     * @return string
119
     */
120 10
    public function compileCycleDetectionRecursiveSelect(string $sql, string $column): string
121
    {
122 10
        return $sql;
123
    }
124
125
    /**
126
     * Compile the stop constraint for a cycle detection clause.
127
     *
128
     * @param string $column
129
     * @return string
130
     */
131 10
    public function compileCycleDetectionStopConstraint(string $column): string
132
    {
133 10
        return 'not ' . $this->wrap($column);
134
    }
135
136
    /**
137
     * Determine whether the database supports the UNION operator in a recursive expression.
138
     *
139
     * @return bool
140
     */
141 16
    public function supportsUnionInRecursiveExpression(): bool
142
    {
143 16
        return true;
144
    }
145
}
146