Completed
Push — master ( e25a2e...09c2a6 )
by Beniamin
03:10
created

QueryBuilder   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 299
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 21
Bugs 1 Features 12
Metric Value
wmc 26
c 21
b 1
f 12
lcom 3
cbo 9
dl 0
loc 299
ccs 72
cts 72
cp 1
rs 10

24 Methods

Rating   Name   Duplication   Size   Complexity  
A buildSQL() 0 4 1
A __construct() 0 8 3
A addSelect() 0 6 1
A andWhere() 0 6 1
A andHaving() 0 6 1
A addOrderBy() 0 6 1
A addSet() 0 6 1
A addGroupBy() 0 6 1
A getQueryClauses() 0 4 1
A addRootTable() 0 9 1
A from() 0 4 1
A addFrom() 0 4 1
A update() 0 4 1
A join() 0 9 1
A crossJoin() 0 4 1
A leftJoin() 0 4 1
A innerJoin() 0 4 1
A limit() 0 6 1
A buildQuery() 0 8 1
A getTables() 0 4 1
A getRootTables() 0 6 1
A getJoinTables() 0 6 1
A setParameter() 0 6 1
A getReferenceManager() 0 4 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\Connection\ConnectionInterface;
15
use Phuria\QueryBuilder\Parameter\ParameterManager;
16
use Phuria\QueryBuilder\Parameter\ParameterManagerInterface;
17
use Phuria\QueryBuilder\Table\AbstractTable;
18
19
/**
20
 * @author Beniamin Jonatan Šimko <[email protected]>
21
 */
22
class QueryBuilder
23
{
24
    /**
25
     * @var TableFactory $tableFactory
26
     */
27
    private $tableFactory;
28
29
    /**
30
     * @var CompilerManager $compilerManager
31
     */
32
    private $compilerManager;
33
34
    /**
35
     * @var QueryClauses
36
     */
37
    private $queryClauses;
38
39
    /**
40
     * @var AbstractTable[] $tables
41
     */
42
    private $tables = [];
43
44
    /**
45
     * @var ReferenceManager $referenceManager
46
     */
47
    private $referenceManager;
48
49
    /**
50
     * @var ParameterManagerInterface $parameterManager
51
     */
52
    private $parameterManager;
53
54
    /**
55
     * @param TableFactory    $tableFactory
0 ignored issues
show
Documentation introduced by
Should the type for parameter $tableFactory not be null|TableFactory?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
56
     * @param CompilerManager $compilerManager
0 ignored issues
show
Documentation introduced by
Should the type for parameter $compilerManager not be null|CompilerManager?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
57
     */
58 25
    public function __construct(TableFactory $tableFactory = null, CompilerManager $compilerManager = null)
59
    {
60 25
        $this->tableFactory = $tableFactory ?: new TableFactory();
61 25
        $this->compilerManager = $compilerManager ?: new CompilerManager();
62 25
        $this->queryClauses = new QueryClauses();
63 25
        $this->referenceManager = new ReferenceManager();
64 25
        $this->parameterManager = new ParameterManager();
65 25
    }
66
67
    /**
68
     * @param string $clause
69
     *
70
     * @return $this
71
     */
72 21
    public function addSelect($clause)
73
    {
74 21
        $this->queryClauses->addSelect($clause);
75
76 21
        return $this;
77
    }
78
79
    /**
80
     * @param string $clause
81
     *
82
     * @return $this
83
     */
84 5
    public function andWhere($clause)
85
    {
86 5
        $this->queryClauses->andWhere($clause);
87
88 5
        return $this;
89
    }
90
91
    /**
92
     * @param string $clause
93
     *
94
     * @return $this
95
     */
96 1
    public function andHaving($clause)
97
    {
98 1
        $this->queryClauses->andHaving($clause);
99
100 1
        return $this;
101
    }
102
103
    /**
104
     * @param string $clause
105
     *
106
     * @return $this
107
     */
108 1
    public function addOrderBy($clause)
109
    {
110 1
        $this->queryClauses->addOrderBy($clause);
111
112 1
        return $this;
113
    }
114
115
    /**
116
     * @param string $clause
117
     *
118
     * @return $this
119
     */
120 1
    public function addSet($clause)
121
    {
122 1
        $this->queryClauses->addSet($clause);
123
124 1
        return $this;
125
    }
126
127
    /**
128
     * @param string $clause
129
     *
130
     * @return $this
131
     */
132 3
    public function addGroupBy($clause)
133
    {
134 3
        $this->queryClauses->addGroupBy($clause);
135
136 3
        return $this;
137
    }
138
139
    /**
140
     * @return QueryClauses
141
     */
142 22
    public function getQueryClauses()
143
    {
144 22
        return $this->queryClauses;
145
    }
146
147
    /**
148
     * @param $table
149
     *
150
     * @return AbstractTable
151
     */
152 23
    private function addRootTable($table)
153
    {
154 23
        $table = $this->tableFactory->createNewTable($table, $this);
155 23
        $table->setRoot(true);
156
157 23
        $this->tables[] = $table;
158
159 23
        return $table;
160
    }
161
162
    /**
163
     * @param mixed $table
164
     *
165
     * @return AbstractTable
166
     */
167 22
    public function from($table)
168
    {
169 22
        return $this->addFrom($table);
170
    }
171
172
    /**
173
     * @param mixed $table
174
     *
175
     * @return AbstractTable
176
     */
177 22
    public function addFrom($table)
178
    {
179 22
        return $this->addRootTable($table);
180
    }
181
182
    /**
183
     * @param mixed $table
184
     *
185
     * @return AbstractTable
186
     */
187 1
    public function update($table)
188
    {
189 1
        return $this->addRootTable($table);
190
    }
191
192
    /**
193
     * @param string $joinType
194
     * @param mixed  $table
195
     *
196
     * @return AbstractTable
197
     */
198 3
    public function join($joinType, $table)
199
    {
200 3
        $table = $this->tableFactory->createNewTable($table, $this);
201 3
        $table->setJoinType($joinType);
202
203 3
        $this->tables[] = $table;
204
205 3
        return $table;
206
    }
207
208
    /**
209
     * @param mixed $table
210
     *
211
     * @return AbstractTable
212
     */
213 1
    public function crossJoin($table)
214
    {
215 1
        return $this->join(AbstractTable::CROSS_JOIN, $table);
216
    }
217
218
    /**
219
     * @param mixed $table
220
     *
221
     * @return AbstractTable
222
     */
223 2
    public function leftJoin($table)
224
    {
225 2
        return $this->join(AbstractTable::LEFT_JOIN, $table);
226
    }
227
228
    /**
229
     * @param string $table
230
     *
231
     * @return AbstractTable
232
     */
233 1
    public function innerJoin($table)
234
    {
235 1
        return $this->join(AbstractTable::INNER_JOIN, $table);
236
    }
237
238
    /**
239
     * @param string $clause
240
     *
241
     * @return $this
242
     */
243 1
    public function limit($clause)
244
    {
245 1
        $this->queryClauses->setLimit($clause);
246
247 1
        return $this;
248
    }
249
250
    /**
251
     * @return string
252
     */
253 22
    public function buildSQL()
254
    {
255 22
        return $this->compilerManager->compile($this);
256
    }
257
258
    /**
259
     * @param ConnectionInterface $connection
0 ignored issues
show
Documentation introduced by
Should the type for parameter $connection not be null|ConnectionInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
260
     *
261
     * @return Query
262
     */
263 12
    public function buildQuery(ConnectionInterface $connection = null)
264
    {
265 12
        return new Query(
266 12
            $this->buildSQL(),
267 12
            $this->parameterManager,
268
            $connection
269 12
        );
270
    }
271
272
    /**
273
     * @return AbstractTable[]
274
     */
275 22
    public function getTables()
276
    {
277 22
        return $this->tables;
278
    }
279
280
    /**
281
     * @return AbstractTable[]
282
     */
283 22
    public function getRootTables()
284
    {
285
        return array_filter($this->getTables(), function(AbstractTable $table) {
286 21
            return $table->isRoot();
287 22
        });
288
    }
289
290
    /**
291
     * @return AbstractTable[]
292
     */
293
    public function getJoinTables()
294
    {
295 21
        return array_filter($this->getTables(), function(AbstractTable $table) {
296 20
            return $table->isJoin();
297 21
        });
298
    }
299
300
    /**
301
     * @param int|string $name
302
     * @param mixed      $value
303
     *
304
     * @return $this
305
     */
306 1
    public function setParameter($name, $value)
307
    {
308 1
        $this->parameterManager->createOrGetParameter($name)->setValue($value);
309
310 1
        return $this;
311
    }
312
313
    /**
314
     * @return ReferenceManager
315
     */
316 22
    public function getReferenceManager()
317
    {
318 22
        return $this->referenceManager;
319
    }
320
}