Passed
Push — master ( e1711c...6da277 )
by Jonas
04:38
created

CompilesExpressions::prepareBindingsForUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\LaravelCte\Query\Grammars;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Support\Str;
7
8
trait CompilesExpressions
9
{
10
    /**
11
     * Create a new grammar instance.
12
     */
13 76
    public function __construct()
14
    {
15 76
        array_unshift($this->selectComponents, 'expressions');
16
17 76
        $this->selectComponents[] = 'recursionLimit';
0 ignored issues
show
Bug Best Practice introduced by
The property selectComponents does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18 76
    }
19
20
    /**
21
     * Compile the common table expressions.
22
     *
23
     * @param \Illuminate\Database\Query\Builder $query
24
     * @return string
25
     */
26 76
    public function compileExpressions(Builder $query)
27
    {
28 76
        if (!$query->expressions) {
29 72
            return '';
30
        }
31
32 64
        $recursive = $this->recursiveKeyword($query->expressions);
33
34 64
        $statements = [];
35
36 64
        foreach ($query->expressions as $expression) {
37 64
            $columns = $expression['columns'] ? '('.$this->columnize($expression['columns']).') ' : '';
0 ignored issues
show
Bug introduced by
It seems like columnize() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            $columns = $expression['columns'] ? '('.$this->/** @scrutinizer ignore-call */ columnize($expression['columns']).') ' : '';
Loading history...
38
39 64
            $statements[] = $this->wrapTable($expression['name']).' '.$columns.'as ('.$expression['query'].')';
0 ignored issues
show
Bug introduced by
It seems like wrapTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            $statements[] = $this->/** @scrutinizer ignore-call */ wrapTable($expression['name']).' '.$columns.'as ('.$expression['query'].')';
Loading history...
40
        }
41
42 64
        return 'with '.$recursive.implode(', ', $statements);
43
    }
44
45
    /**
46
     * Get the "recursive" keyword.
47
     *
48
     * @param array $expressions
49
     * @return string
50
     */
51 45
    protected function recursiveKeyword(array $expressions)
52
    {
53 45
        return collect($expressions)->where('recursive', true)->isNotEmpty() ? 'recursive ' : '';
54
    }
55
56
    /**
57
     * Compile the recursion limit.
58
     *
59
     * @param \Illuminate\Database\Query\Builder $query
60
     * @return string
61
     */
62 11
    public function compileRecursionLimit(Builder $query)
63
    {
64 11
        if (is_null($query->recursionLimit)) {
65 3
            return '';
66
        }
67
68 8
        return 'option (maxrecursion '.(int) $query->recursionLimit.')';
69
    }
70
71
    /**
72
     * Compile an insert statement using a subquery into SQL.
73
     *
74
     * @param \Illuminate\Database\Query\Builder $query
75
     * @param array $columns
76
     * @param string $sql
77
     * @return string
78
     */
79 7
    public function compileInsertUsing(Builder $query, array $columns, string $sql)
80
    {
81 7
        $expressions = $this->compileExpressions($query);
82
83 7
        $recursionLimit = $this->compileRecursionLimit($query);
84
85 7
        $compiled = parent::compileInsertUsing($query, $columns, $sql);
86
87 7
        return (string) Str::of($compiled)
88 7
            ->prepend($expressions, ' ')
89 7
            ->append(' ', $recursionLimit)
90 7
            ->trim();
91
    }
92
93
    /**
94
     * Compile an update statement into SQL.
95
     *
96
     * @param \Illuminate\Database\Query\Builder $query
97
     * @param array $values
98
     * @return string
99
     */
100 4
    public function compileUpdate(Builder $query, array $values)
101
    {
102 4
        $compiled = parent::compileUpdate($query, $values);
103
104 4
        return (string) Str::of($compiled)
105 4
            ->prepend($this->compileExpressions($query), ' ')
106 4
            ->trim();
107
    }
108
109
    /**
110
     * Prepare the bindings for an update statement.
111
     *
112
     * @param array $bindings
113
     * @param array $values
114
     * @return array
115
     */
116 4
    public function prepareBindingsForUpdate(array $bindings, array $values)
117
    {
118 4
        $values = array_merge($bindings['expressions'], $values);
119
120 4
        unset($bindings['expressions']);
121
122 4
        return parent::prepareBindingsForUpdate($bindings, $values);
123
    }
124
125
    /**
126
     * Compile a delete statement into SQL.
127
     *
128
     * @param \Illuminate\Database\Query\Builder $query
129
     * @return string
130
     */
131 4
    public function compileDelete(Builder $query)
132
    {
133 4
        $compiled = parent::compileDelete($query);
134
135 4
        return (string) Str::of($compiled)
136 4
            ->prepend($this->compileExpressions($query), ' ')
137 4
            ->trim();
138
    }
139
}
140