Completed
Push — master ( f3cdf4...360009 )
by Beniamin
04:07
created

QueryBuilder::getQueryClauses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\QueryBuilder;
13
14
use Phuria\QueryBuilder\Expression\ExpressionCollection;
15
use Phuria\QueryBuilder\Table\AbstractTable;
16
17
/**
18
 * @author Beniamin Jonatan Šimko <[email protected]>
19
 */
20
class QueryBuilder
21
{
22
    /**
23
     * @var TableFactory $tableFactory
24
     */
25
    private $tableFactory;
26
27
    /**
28
     * @var CompilerManager $compilerManager
29
     */
30
    private $compilerManager;
31
32
    /**
33
     * @var QueryClauses
34
     */
35
    private $queryClauses;
36
37
    /**
38
     * @var AbstractTable[] $tables
39
     */
40
    private $tables = [];
41
42
    /**
43
     * @param TableFactory    $tableFactory
44
     * @param CompilerManager $compilerManager
45
     */
46 25
    public function __construct(TableFactory $tableFactory = null, CompilerManager $compilerManager = null)
47
    {
48 25
        $this->tableFactory = $tableFactory ?: new TableFactory();
49 25
        $this->compilerManager = $compilerManager ?: new CompilerManager();
50 25
        $this->queryClauses = new QueryClauses();
51 25
    }
52
53
    /**
54
     * @return ExprBuilder
55
     */
56 2
    public function expr()
57
    {
58 2
        return new ExprBuilder(func_get_args());
59
    }
60
61
    /**
62
     * @return $this
63
     */
64 21
    public function addSelect()
65
    {
66 21
        $this->queryClauses->addSelect(...func_get_args());
67
68 21
        return $this;
69
    }
70
71
    /**
72
     * @return $this
73
     */
74 11
    public function andWhere()
75
    {
76 3
        $this->queryClauses->andWhere(...func_get_args());
77
78 3
        return $this;
79 11
    }
80
81
    /**
82
     * @return $this
83
     */
84 1
    public function andHaving()
85
    {
86 1
        $this->queryClauses->andHaving(...func_get_args());
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * @return $this
93
     */
94 1
    public function addOrderBy()
95
    {
96 1
        $this->queryClauses->addOrderBy(...func_get_args());
97
98 1
        return $this;
99
    }
100
101
    /**
102
     * @return $this
103
     */
104 1
    public function addSet()
105
    {
106 1
        $this->queryClauses->addSet(...func_get_args());
107
108 1
        return $this;
109
    }
110
111
    /**
112
     * @return $this
113
     */
114 2
    public function addGroupBy()
115
    {
116 2
        $this->queryClauses->addGroupBy(...func_get_args());
117
118 2
        return $this;
119
    }
120
121
    /**
122
     * @return QueryClauses
123
     */
124 22
    public function getQueryClauses()
125
    {
126 22
        return $this->queryClauses;
127
    }
128
129
    /**
130
     * @param string $hint
131
     *
132
     * @return $this
133
     */
134 1
    public function addHint($hint)
0 ignored issues
show
Unused Code introduced by
The parameter $hint is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136 1
        $this->queryClauses->addHint(...func_get_args());
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * @param $table
143
     *
144
     * @return AbstractTable
145
     */
146 23
    private function addRootTable($table)
147
    {
148 23
        $table = $this->tableFactory->createNewTable($table, $this);
149 23
        $table->setRoot(true);
150
151 23
        $this->tables[] = $table;
152
153 23
        return $table;
154
    }
155
156
    /**
157
     * @param mixed $table
158
     *
159
     * @return AbstractTable
160
     */
161 22
    public function from($table)
162
    {
163 22
        return $this->addFrom($table);
164
    }
165
166
    /**
167
     * @param mixed $table
168
     *
169
     * @return AbstractTable
170
     */
171 22
    public function addFrom($table)
172
    {
173 22
        return $this->addRootTable($table);
174
    }
175
176
    /**
177
     * @param mixed $table
178
     *
179
     * @return AbstractTable
180
     */
181 1
    public function update($table)
182
    {
183 1
        return $this->addRootTable($table);
184
    }
185
186
    /**
187
     * @param string $joinType
188
     * @param mixed  $table
189
     *
190
     * @return AbstractTable
191
     */
192 3
    public function join($joinType, $table)
193
    {
194 3
        $table = $this->tableFactory->createNewTable($table, $this);
195 3
        $table->setJoinType($joinType);
196
197 3
        $this->tables[] = $table;
198
199 3
        return $table;
200
    }
201
202
    /**
203
     * @param mixed $table
204
     *
205
     * @return AbstractTable
206
     */
207 1
    public function crossJoin($table)
208
    {
209 1
        return $this->join(AbstractTable::CROSS_JOIN, $table);
210
    }
211
212
    /**
213
     * @param mixed $table
214
     *
215
     * @return AbstractTable
216
     */
217 2
    public function leftJoin($table)
218
    {
219 2
        return $this->join(AbstractTable::LEFT_JOIN, $table);
220
    }
221
222
    /**
223
     * @param string $table
224
     *
225
     * @return AbstractTable
226
     */
227 1
    public function innerJoin($table)
228
    {
229 1
        return $this->join(AbstractTable::INNER_JOIN, $table);
230
    }
231
232
233
    /**
234
     * @return string
235
     */
236 22
    public function buildSQL()
237
    {
238 22
        return $this->compilerManager->compile($this);
239
    }
240
241
    /**
242
     * @return Query
243
     */
244 11
    public function buildQuery()
245
    {
246 11
        return new Query($this->buildSQL());
247
    }
248
249
    /**
250
     * @return AbstractTable[]
251
     */
252 22
    public function getTables()
253
    {
254 22
        return $this->tables;
255
    }
256
257
    /**
258
     * @return ExpressionCollection
259
     */
260 22
    public function getRootTables()
261
    {
262
        return new ExpressionCollection(array_filter($this->getTables(), function (AbstractTable $table) {
263 21
            return $table->isRoot();
264 22
        }), ', ');
265
    }
266
267
    /**
268
     * @return ExpressionCollection
269
     */
270
    public function getJoinTables()
271
    {
272 21
        return new ExpressionCollection(array_filter($this->getTables(), function (AbstractTable $table) {
273 20
            return $table->isJoin();
274 21
        }), ' ');
275
    }
276
}