Completed
Push — master ( cc1f48...9b2df7 )
by Beniamin
04:10
created

QueryBuilder::addRootTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Phuria\QueryBuilder;
4
5
use Phuria\QueryBuilder\Table\AbstractTable;
6
7
/**
8
 * @author Beniamin Jonatan Šimko <[email protected]>
9
 */
10
class QueryBuilder
11
{
12
    /**
13
     * @var TableFactory $tableFactory
14
     */
15
    private $tableFactory;
16
17
    /**
18
     * @var CompilerManager $compilerManager
19
     */
20
    private $compilerManager;
21
22
    /**
23
     * @var array $selectClauses
24
     */
25
    private $selectClauses;
26
27
    /**
28
     * @var array $whereClauses
29
     */
30
    private $whereClauses;
31
32
    /**
33
     * @var array $orderByClauses
34
     */
35
    private $orderByClauses;
36
37
    /**
38
     * @var array $setClauses
39
     */
40
    private $setClauses;
41
42
    /**
43
     * @var array $groupByClauses
44
     */
45
    private $groupByClauses;
46
47
    /**
48
     * @var array $havingClauses
49
     */
50
    private $havingClauses;
51
52
    /**
53
     * @var AbstractTable[] $tables
54
     */
55
    private $tables = [];
56
57
    /**
58
     * @var array $mods
59
     */
60
    private $hints = [];
61
62
    /**
63
     * @param TableFactory    $tableFactory
64
     * @param CompilerManager $compilerManager
65
     */
66 25
    public function __construct(TableFactory $tableFactory = null, CompilerManager $compilerManager = null)
67
    {
68 25
        $this->tableFactory = $tableFactory ?: new TableFactory();
69 25
        $this->compilerManager = $compilerManager ?: new CompilerManager();
70 25
        $this->selectClauses = [];
71 25
        $this->whereClauses = [];
72 25
        $this->orderByClauses = [];
73 25
        $this->groupByClauses = [];
74 25
        $this->setClauses = [];
75 25
        $this->havingClauses = [];
76 25
    }
77
78
    /**
79
     * @return ExprBuilder
80
     */
81 2
    public function expr()
82
    {
83 2
        return new ExprBuilder(func_get_args());
84
    }
85
86
    /**
87
     * @return $this
88
     */
89 21
    public function addSelect()
90
    {
91 21
        $this->selectClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
92
93 21
        return $this;
94
    }
95
96
    /**
97
     * @return $this
98
     */
99 3
    public function andWhere()
100
    {
101 3
        $this->whereClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
102
103 3
        return $this;
104
    }
105
106
    /**
107
     * @return $this
108
     */
109 1
    public function andHaving()
110
    {
111 1
        $this->havingClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
112
113 1
        return $this;
114
    }
115
116
    /**
117
     * @param string $hint
118
     *
119
     * @return $this
120
     */
121 1
    public function addHint($hint)
122
    {
123 1
        $this->hints[] = $hint;
124
125 1
        return $this;
126
    }
127
128
    /**
129
     * @param $table
130
     *
131
     * @return AbstractTable
132
     */
133 23
    private function addRootTable($table)
134
    {
135 23
        $table = $this->tableFactory->createNewTable($table, $this);
136 23
        $table->setRoot(true);
137
138 23
        $this->tables[] = $table;
139
140 23
        return $table;
141
    }
142
143
    /**
144
     * @param mixed $table
145
     *
146
     * @return AbstractTable
147
     */
148 22
    public function from($table)
149
    {
150 22
        return $this->addFrom($table);
151
    }
152
153
    /**
154
     * @param mixed $table
155
     *
156
     * @return AbstractTable
157
     */
158 22
    public function addFrom($table)
159
    {
160 22
        return $this->addRootTable($table);
161
    }
162
163
    /**
164
     * @param mixed $table
165
     *
166
     * @return AbstractTable
167
     */
168 1
    public function update($table)
169
    {
170 1
        return $this->addRootTable($table);
171
    }
172
173
    /**
174
     * @param string $joinType
175
     * @param mixed  $table
176
     *
177
     * @return AbstractTable
178
     */
179 3
    public function join($joinType, $table)
180
    {
181 3
        $table = $this->tableFactory->createNewTable($table, $this);
182 3
        $table->setJoinType($joinType);
183
184 3
        $this->tables[] = $table;
185
186 3
        return $table;
187
    }
188
189
    /**
190
     * @param mixed $table
191
     *
192
     * @return AbstractTable
193
     */
194 1
    public function crossJoin($table)
195
    {
196 1
        return $this->join(AbstractTable::CROSS_JOIN, $table);
197
    }
198
199
    /**
200
     * @param mixed $table
201
     *
202
     * @return AbstractTable
203
     */
204 2
    public function leftJoin($table)
205
    {
206 2
        return $this->join(AbstractTable::LEFT_JOIN, $table);
207
    }
208
209
    /**
210
     * @param string $table
211
     *
212
     * @return AbstractTable
213
     */
214 1
    public function innerJoin($table)
215
    {
216 1
        return $this->join(AbstractTable::INNER_JOIN, $table);
217
    }
218
219
    /**
220
     * @return $this
221
     */
222 1
    public function addOrderBy()
223
    {
224 1
        $this->orderByClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
225
226 1
        return $this;
227
    }
228
229
    /**
230
     * @return $this
231
     */
232 1
    public function addSet()
233
    {
234 1
        $this->setClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
235
236 1
        return $this;
237
    }
238
239
    /**
240
     * @return $this
241
     */
242 2
    public function addGroupBy()
243
    {
244 2
        $this->groupByClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
245
246 2
        return $this;
247
    }
248
249
    /**
250
     * @return string
251
     */
252 22
    public function buildSQL()
253
    {
254 22
        return $this->compilerManager->compile($this);
255
    }
256
257
    /**
258
     * @return Query
259
     */
260 11
    public function buildQuery()
261
    {
262 11
        return new Query($this->buildSQL());
263
    }
264
265
    /**
266
     * @return array
267
     */
268 22
    public function getSelectClauses()
269
    {
270 22
        return $this->selectClauses;
271
    }
272
273
    /**
274
     * @return array
275
     */
276 21
    public function getWhereClauses()
277
    {
278 21
        return $this->whereClauses;
279
    }
280
281
    /**
282
     * @return array
283
     */
284 21
    public function getOrderByClauses()
285
    {
286 21
        return $this->orderByClauses;
287
    }
288
289
    /**
290
     * @return array
291
     */
292 1
    public function getSetClauses()
293
    {
294 1
        return $this->setClauses;
295
    }
296
297
    /**
298
     * @return array
299
     */
300 21
    public function getGroupByClauses()
301
    {
302 21
        return $this->groupByClauses;
303
    }
304
305
    /**
306
     * @return array
307
     */
308 21
    public function getHavingClauses()
309
    {
310 21
        return $this->havingClauses;
311
    }
312
313
    /**
314
     * @return AbstractTable[]
315
     */
316 22
    public function getTables()
317
    {
318 22
        return $this->tables;
319
    }
320
321
    /**
322
     * @return AbstractTable[]
323
     */
324 22
    public function getRootTables()
325
    {
326
        return array_filter($this->getTables(), function (AbstractTable $table) {
327 21
            return $table->isRoot();
328 22
        });
329
    }
330
331
    /**
332
     * @return AbstractTable[]
333
     */
334
    public function getJoinTables()
335
    {
336 21
        return array_filter($this->getTables(), function (AbstractTable $table) {
337 20
            return $table->isJoin();
338 21
        });
339
    }
340
341
    /**
342
     * @return array
343
     */
344 21
    public function getHints()
345
    {
346 21
        return $this->hints;
347
    }
348
}