Passed
Push — main ( b3daef...4d79c5 )
by Jonas
03:48
created

MySqlGrammar::compileParentKeyOfFirstPathSegment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Query\Grammars;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Database\Query\Grammars\MySqlGrammar as Base;
7
8
class MySqlGrammar extends Base implements EagerLimitGrammar
9
{
10
    /**
11
     * Compile an extraction of the first path segment.
12
     *
13
     * @param string $pathColumn
14
     * @return string
15
     */
16 20
    public function compileFirstPathSegment(string $pathColumn): string
17
    {
18 20
        $pathColumn = $this->wrap($pathColumn);
19
20 20
        return "substring_index($pathColumn, ?, 1)";
21
    }
22
23
    /**
24
     * Compile an extraction of the first path segment's parent key.
25
     *
26
     * @param string $pathColumn
27
     * @param string $parentKeyColumn
28
     * @param \Illuminate\Database\Query\Builder $parentKeyQuery
29
     * @return string
30
     */
31 4
    public function compileParentKeyOfFirstPathSegment(string $pathColumn, string $parentKeyColumn, Builder $parentKeyQuery): string
32
    {
33 4
        $column = $this->wrap($pathColumn);
34 4
        $parentKeyColumn = $this->wrap($parentKeyColumn);
35 4
        $sql = $parentKeyQuery->toSql();
36
37 4
        return "if(instr($column, ?) = 0, $parentKeyColumn, ($sql))";
38
    }
39
}
40