Passed
Push — master ( a69b6b...edb396 )
by Jonas
11:41 queued 57s
created

Builder::withMaterializedExpression()   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
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 recursion limit.
26
     *
27
     * @var int
28
     */
29
    public $recursionLimit;
30
31
    /**
32
     * Create a new query builder instance.
33
     *
34
     * @param \Illuminate\Database\Connection $connection
35
     * @param \Illuminate\Database\Query\Grammars\Grammar|null $grammar
36
     * @param \Illuminate\Database\Query\Processors\Processor|null $processor
37
     * @return void
38
     */
39 94
    public function __construct(Connection $connection, Grammar $grammar = null, Processor $processor = null)
40
    {
41 94
        $grammar = $grammar ?: $connection->withTablePrefix($this->getQueryGrammar($connection));
42 94
        $processor = $processor ?: $connection->getPostProcessor();
43
44 94
        parent::__construct($connection, $grammar, $processor);
45
46 94
        $this->bindings = ['expressions' => []] + $this->bindings;
47 94
    }
48
49
    /**
50
     * Get the query grammar.
51
     *
52
     * @param \Illuminate\Database\Connection $connection
53
     * @return \Illuminate\Database\Query\Grammars\Grammar
54
     */
55 94
    protected function getQueryGrammar(Connection $connection)
56
    {
57 94
        $driver = $connection->getDriverName();
58
59 94
        switch ($driver) {
60 94
            case 'mysql':
61 23
                return new MySqlGrammar();
62 71
            case 'pgsql':
63 25
                return new PostgresGrammar();
64 46
            case 'sqlite':
65 23
                return new SQLiteGrammar();
66 23
            case 'sqlsrv':
67 23
                return new SqlServerGrammar();
68
        }
69
70
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
71
    }
72
73
    /**
74
     * Add a common table expression to the query.
75
     *
76
     * @param string $name
77
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
78
     * @param array|null $columns
79
     * @param bool $recursive
80
     * @param bool|null $materialized
81
     * @return $this
82
     */
83 82
    public function withExpression($name, $query, array $columns = null, $recursive = false, $materialized = null)
84
    {
85 82
        [$query, $bindings] = $this->createSub($query);
86
87 82
        $this->expressions[] = compact('name', 'query', 'columns', 'recursive', 'materialized');
88
89 82
        $this->addBinding($bindings, 'expressions');
90
91 82
        return $this;
92
    }
93
94
    /**
95
     * Add a recursive common table expression to the query.
96
     *
97
     * @param string $name
98
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
99
     * @param array|null $columns
100
     * @return $this
101
     */
102 24
    public function withRecursiveExpression($name, $query, $columns = null)
103
    {
104 24
        return $this->withExpression($name, $query, $columns, true);
105
    }
106
107
    /**
108
     * Add a materialized common table expression to the query.
109
     *
110
     * @param string $name
111
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
112
     * @param array|null $columns
113
     * @return $this
114
     */
115 9
    public function withMaterializedExpression($name, $query, $columns = null)
116
    {
117 9
        return $this->withExpression($name, $query, $columns, false, true);
118
    }
119
120
    /**
121
     * Add a non-materialized common table expression to the query.
122
     *
123
     * @param string $name
124
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
125
     * @param array|null $columns
126
     * @return $this
127
     */
128 9
    public function withNonMaterializedExpression($name, $query, $columns = null)
129
    {
130 9
        return $this->withExpression($name, $query, $columns, false, false);
131
    }
132
133
    /**
134
     * Set the recursion limit of the query.
135
     *
136
     * @param int $value
137
     * @return $this
138
     */
139 8
    public function recursionLimit($value)
140
    {
141 8
        $this->recursionLimit = $value;
142
143 8
        return $this;
144
    }
145
146
    /**
147
     * Insert new records into the table using a subquery.
148
     *
149
     * @param array $columns
150
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
151
     * @return bool
152
     */
153 8
    public function insertUsing(array $columns, $query)
154
    {
155 8
        [$sql, $bindings] = $this->createSub($query);
156
157 8
        $bindings = array_merge($this->bindings['expressions'], $bindings);
158
159 8
        return $this->connection->insert(
160 8
            $this->grammar->compileInsertUsing($this, $columns, $sql),
161 8
            $this->cleanBindings($bindings)
162
        );
163
    }
164
}
165