Passed
Push — master ( 89b134...c252a6 )
by Jonas
10:11
created

Builder::recursionLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\LaravelCte\Query;
4
5
use Illuminate\Database\Connection;
6
use Illuminate\Database\Query\Builder as Base;
7
use Illuminate\Database\Query\Grammars\Grammar;
8
use Illuminate\Database\Query\Processors\Processor;
9
use RuntimeException;
10
use Staudenmeir\LaravelCte\Query\Grammars\MySqlGrammar;
11
use Staudenmeir\LaravelCte\Query\Grammars\PostgresGrammar;
12
use Staudenmeir\LaravelCte\Query\Grammars\SQLiteGrammar;
13
use Staudenmeir\LaravelCte\Query\Grammars\SqlServerGrammar;
14
15
class Builder extends Base
16
{
17
    /**
18
     * The common table expressions.
19
     *
20
     * @var array
21
     */
22
    public $expressions = [];
23
24
    /**
25
     * The common table expressions for union queries.
26
     *
27
     * @var array
28
     */
29
    public $unionExpressions = [];
30
31
    /**
32
     * The recursion limit.
33
     *
34
     * @var int
35
     */
36
    public $recursionLimit;
37
38
    /**
39
     * The recursion limit for union queries.
40
     *
41
     * @var int
42
     */
43
    public $unionRecursionLimit;
44
45
    /**
46
     * Create a new query builder instance.
47
     *
48
     * @param \Illuminate\Database\Connection $connection
49
     * @param \Illuminate\Database\Query\Grammars\Grammar|null $grammar
50
     * @param \Illuminate\Database\Query\Processors\Processor|null $processor
51
     * @return void
52
     */
53 116
    public function __construct(Connection $connection, Grammar $grammar = null, Processor $processor = null)
54
    {
55 116
        $grammar = $grammar ?: $connection->withTablePrefix($this->getQueryGrammar($connection));
56 116
        $processor = $processor ?: $connection->getPostProcessor();
57
58 116
        parent::__construct($connection, $grammar, $processor);
59
60 116
        $this->bindings = ['expressions' => []] + $this->bindings;
61 116
    }
62
63
    /**
64
     * Get the query grammar.
65
     *
66
     * @param \Illuminate\Database\Connection $connection
67
     * @return \Illuminate\Database\Query\Grammars\Grammar
68
     */
69 116
    protected function getQueryGrammar(Connection $connection)
70
    {
71 116
        $driver = $connection->getDriverName();
72
73 116
        switch ($driver) {
74 116
            case 'mysql':
75 29
                return new MySqlGrammar();
76 87
            case 'pgsql':
77 31
                return new PostgresGrammar();
78 56
            case 'sqlite':
79 29
                return new SQLiteGrammar();
80 27
            case 'sqlsrv':
81 27
                return new SqlServerGrammar();
82
        }
83
84
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
85
    }
86
87
    /**
88
     * Add a common table expression to the query.
89
     *
90
     * @param string $name
91
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
92
     * @param array|null $columns
93
     * @param bool $recursive
94
     * @param bool|null $materialized
95
     * @return $this
96
     */
97 104
    public function withExpression($name, $query, array $columns = null, $recursive = false, $materialized = null)
98
    {
99 104
        [$query, $bindings] = $this->createSub($query);
100
101 104
        $this->{$this->unions ? 'unionExpressions' : 'expressions'}[] = compact('name', 'query', 'columns', 'recursive', 'materialized');
102
103 104
        $this->addBinding($bindings, 'expressions');
104
105 104
        return $this;
106
    }
107
108
    /**
109
     * Add a recursive common table expression to the query.
110
     *
111
     * @param string $name
112
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
113
     * @param array|null $columns
114
     * @return $this
115
     */
116 24
    public function withRecursiveExpression($name, $query, $columns = null)
117
    {
118 24
        return $this->withExpression($name, $query, $columns, true);
119
    }
120
121
    /**
122
     * Add a materialized common table expression to the query.
123
     *
124
     * @param string $name
125
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
126
     * @param array|null $columns
127
     * @return $this
128
     */
129 9
    public function withMaterializedExpression($name, $query, $columns = null)
130
    {
131 9
        return $this->withExpression($name, $query, $columns, false, true);
132
    }
133
134
    /**
135
     * Add a non-materialized common table expression to the query.
136
     *
137
     * @param string $name
138
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
139
     * @param array|null $columns
140
     * @return $this
141
     */
142 9
    public function withNonMaterializedExpression($name, $query, $columns = null)
143
    {
144 9
        return $this->withExpression($name, $query, $columns, false, false);
145
    }
146
147
    /**
148
     * Set the recursion limit of the query.
149
     *
150
     * @param int $value
151
     * @return $this
152
     */
153 12
    public function recursionLimit($value)
154
    {
155 12
        $this->{$this->unions ? 'unionRecursionLimit' : 'recursionLimit'} = $value;
156
157 12
        return $this;
158
    }
159
160
    /**
161
     * Insert new records into the table using a subquery.
162
     *
163
     * @param array $columns
164
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
165
     * @return bool
166
     */
167 8
    public function insertUsing(array $columns, $query)
168
    {
169 8
        [$sql, $bindings] = $this->createSub($query);
170
171 8
        $bindings = array_merge($this->bindings['expressions'], $bindings);
172
173 8
        return $this->connection->insert(
174 8
            $this->grammar->compileInsertUsing($this, $columns, $sql),
175 8
            $this->cleanBindings($bindings)
176
        );
177
    }
178
179
    /**
180
     * Update records in the database.
181
     *
182
     * @param  array  $values
183
     * @return int
184
     */
185 11
    public function update(array $values)
186
    {
187 11
        $this->applyBeforeQueryCallbacks();
188
189 11
        $sql = $this->grammar->compileUpdate($this, $values);
190
191 11
        return $this->connection->update($sql, $this->cleanBindings(
192 11
            $this->grammar->getBindingsForUpdate($this, $this->bindings, $values)
193
        ));
194
    }
195
}
196