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

Builder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 120
ccs 33
cts 33
cp 1
rs 10
c 1
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A recursionLimit() 0 5 1
A insertUsing() 0 9 1
A withRecursiveExpression() 0 3 1
A getQueryGrammar() 0 16 5
A withExpression() 0 9 1
A __construct() 0 8 3
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 76
    public function __construct(Connection $connection, Grammar $grammar = null, Processor $processor = null)
40
    {
41 76
        $grammar = $grammar ?: $connection->withTablePrefix($this->getQueryGrammar($connection));
42 76
        $processor = $processor ?: $connection->getPostProcessor();
43
44 76
        parent::__construct($connection, $grammar, $processor);
45
46 76
        $this->bindings = ['expressions' => []] + $this->bindings;
47 76
    }
48
49
    /**
50
     * Get the query grammar.
51
     *
52
     * @param \Illuminate\Database\Connection $connection
53
     * @return \Illuminate\Database\Query\Grammars\Grammar
54
     */
55 76
    protected function getQueryGrammar(Connection $connection)
56
    {
57 76
        $driver = $connection->getDriverName();
58
59 76
        switch ($driver) {
60 76
            case 'mysql':
61 19
                return new MySqlGrammar;
62 57
            case 'pgsql':
63 19
                return new PostgresGrammar;
64 38
            case 'sqlite':
65 19
                return new SQLiteGrammar;
66 19
            case 'sqlsrv':
67 19
                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
     * @return $this
81
     */
82 64
    public function withExpression($name, $query, array $columns = null, $recursive = false)
83
    {
84 64
        [$query, $bindings] = $this->createSub($query);
85
86 64
        $this->expressions[] = compact('name', 'query', 'columns', 'recursive');
87
88 64
        $this->addBinding($bindings, 'expressions');
89
90 64
        return $this;
91
    }
92
93
    /**
94
     * Add a recursive common table expression to the query.
95
     *
96
     * @param string $name
97
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
98
     * @param array|null $columns
99
     * @return $this
100
     */
101 24
    public function withRecursiveExpression($name, $query, $columns = null)
102
    {
103 24
        return $this->withExpression($name, $query, $columns, true);
104
    }
105
106
    /**
107
     * Set the recursion limit of the query.
108
     *
109
     * @param int $value
110
     * @return $this
111
     */
112 8
    public function recursionLimit($value)
113
    {
114 8
        $this->recursionLimit = $value;
115
116 8
        return $this;
117
    }
118
119
    /**
120
     * Insert new records into the table using a subquery.
121
     *
122
     * @param array $columns
123
     * @param \Closure|\Illuminate\Database\Query\Builder|string $query
124
     * @return bool
125
     */
126 8
    public function insertUsing(array $columns, $query)
127
    {
128 8
        [$sql, $bindings] = $this->createSub($query);
129
130 8
        $bindings = array_merge($this->bindings['expressions'], $bindings);
131
132 8
        return $this->connection->insert(
133 8
            $this->grammar->compileInsertUsing($this, $columns, $sql),
134 8
            $this->cleanBindings($bindings)
135
        );
136
    }
137
}
138