Completed
Push — master ( 77e4e4...2e1347 )
by Beniamin
05:23
created

QueryBuilder::getJoinTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
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\Connection\ConnectionInterface;
15
use Phuria\QueryBuilder\Parameter\ParameterManager;
16
use Phuria\QueryBuilder\Table\AbstractTable;
17
18
/**
19
 * @author Beniamin Jonatan Šimko <[email protected]>
20
 */
21
class QueryBuilder
22
{
23
    /**
24
     * @var TableFactory $tableFactory
25
     */
26
    private $tableFactory;
27
28
    /**
29
     * @var CompilerManager $compilerManager
30
     */
31
    private $compilerManager;
32
33
    /**
34
     * @var QueryClauses
35
     */
36
    private $queryClauses;
37
38
    /**
39
     * @var AbstractTable[] $tables
40
     */
41
    private $tables = [];
42
43
    /**
44
     * @var ReferenceManager $referenceManager
45
     */
46
    private $referenceManager;
47
48
    /**
49
     * @var ParameterManager $parameterManager
50
     */
51
    private $parameterManager;
52
53
    /**
54
     * @param TableFactory    $tableFactory
55
     * @param CompilerManager $compilerManager
56
     */
57 25
    public function __construct(TableFactory $tableFactory = null, CompilerManager $compilerManager = null)
58
    {
59 25
        $this->tableFactory = $tableFactory ?: new TableFactory();
60 25
        $this->compilerManager = $compilerManager ?: new CompilerManager();
61 25
        $this->queryClauses = new QueryClauses();
62 25
        $this->referenceManager = new ReferenceManager();
63 25
        $this->parameterManager = new ParameterManager();
64 25
    }
65
66
    /**
67
     * @param string $clause
68
     *
69
     * @return $this
70
     */
71 21
    public function addSelect($clause)
72
    {
73 21
        $this->queryClauses->addSelect($clause);
74
75 21
        return $this;
76
    }
77
78
    /**
79
     * @param string $clause
80
     *
81
     * @return $this
82
     */
83 5
    public function andWhere($clause)
84
    {
85 5
        $this->queryClauses->andWhere($clause);
86
87 5
        return $this;
88
    }
89
90
    /**
91
     * @param string $clause
92
     *
93
     * @return $this
94
     */
95 1
    public function andHaving($clause)
96
    {
97 1
        $this->queryClauses->andHaving($clause);
98
99 1
        return $this;
100
    }
101
102
    /**
103
     * @param string $clause
104
     *
105
     * @return $this
106
     */
107 1
    public function addOrderBy($clause)
108
    {
109 1
        $this->queryClauses->addOrderBy($clause);
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * @param string $clause
116
     *
117
     * @return $this
118
     */
119 1
    public function addSet($clause)
120
    {
121 1
        $this->queryClauses->addSet($clause);
122
123 1
        return $this;
124
    }
125
126
    /**
127
     * @param string $clause
128
     *
129
     * @return $this
130
     */
131 3
    public function addGroupBy($clause)
132
    {
133 3
        $this->queryClauses->addGroupBy($clause);
134
135 3
        return $this;
136
    }
137
138
    /**
139
     * @return QueryClauses
140
     */
141 22
    public function getQueryClauses()
142
    {
143 22
        return $this->queryClauses;
144
    }
145
146
    /**
147
     * @param $table
148
     *
149
     * @return AbstractTable
150
     */
151 23
    private function addRootTable($table)
152
    {
153 23
        $table = $this->tableFactory->createNewTable($table, $this);
154 23
        $table->setRoot(true);
155
156 23
        $this->tables[] = $table;
157
158 23
        return $table;
159
    }
160
161
    /**
162
     * @param mixed $table
163
     *
164
     * @return AbstractTable
165
     */
166 22
    public function from($table)
167
    {
168 22
        return $this->addFrom($table);
169
    }
170
171
    /**
172
     * @param mixed $table
173
     *
174
     * @return AbstractTable
175
     */
176 22
    public function addFrom($table)
177
    {
178 22
        return $this->addRootTable($table);
179
    }
180
181
    /**
182
     * @param mixed $table
183
     *
184
     * @return AbstractTable
185
     */
186 1
    public function update($table)
187
    {
188 1
        return $this->addRootTable($table);
189
    }
190
191
    /**
192
     * @param string $joinType
193
     * @param mixed  $table
194
     *
195
     * @return AbstractTable
196
     */
197 3
    public function join($joinType, $table)
198
    {
199 3
        $table = $this->tableFactory->createNewTable($table, $this);
200 3
        $table->setJoinType($joinType);
201
202 3
        $this->tables[] = $table;
203
204 3
        return $table;
205
    }
206
207
    /**
208
     * @param mixed $table
209
     *
210
     * @return AbstractTable
211
     */
212 1
    public function crossJoin($table)
213
    {
214 1
        return $this->join(AbstractTable::CROSS_JOIN, $table);
215
    }
216
217
    /**
218
     * @param mixed $table
219
     *
220
     * @return AbstractTable
221
     */
222 2
    public function leftJoin($table)
223
    {
224 2
        return $this->join(AbstractTable::LEFT_JOIN, $table);
225
    }
226
227
    /**
228
     * @param string $table
229
     *
230
     * @return AbstractTable
231
     */
232 1
    public function innerJoin($table)
233
    {
234 1
        return $this->join(AbstractTable::INNER_JOIN, $table);
235
    }
236
237
    /**
238
     * @param string $clause
239
     *
240
     * @return $this
241
     */
242 1
    public function limit($clause)
243
    {
244 1
        $this->queryClauses->setLimit($clause);
245
246 1
        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
     * @param ConnectionInterface $connection
259
     *
260
     * @return Query
261
     */
262 11
    public function buildQuery(ConnectionInterface $connection = null)
263
    {
264 11
        return new Query(
265 11
            $this->buildSQL(),
266 11
            $this->parameterManager,
267
            $connection
268 11
        );
269
    }
270
271
    /**
272
     * @return AbstractTable[]
273
     */
274 22
    public function getTables()
275
    {
276 22
        return $this->tables;
277
    }
278
279
    /**
280
     * @return AbstractTable[]
281
     */
282 22
    public function getRootTables()
283
    {
284
        return array_filter($this->getTables(), function(AbstractTable $table) {
285 21
            return $table->isRoot();
286 22
        });
287
    }
288
289
    /**
290
     * @return AbstractTable[]
291
     */
292
    public function getJoinTables()
293
    {
294 21
        return array_filter($this->getTables(), function(AbstractTable $table) {
295 20
            return $table->isJoin();
296 21
        });
297
    }
298
299
    /**
300
     * @param string $name
301
     * @param mixed  $value
302
     *
303
     * @return string
304
     */
305 2
    public function param($name, $value = null)
306
    {
307 2
        $param = $this->parameterManager->createOrGetParameter($name);
308
309 2
        if ($value) {
310 1
            $param->setValue($value);
311 1
        }
312
313 2
        return $param;
314
    }
315
316
    /**
317
     * @return ReferenceManager
318
     */
319 22
    public function getReferenceManager()
320
    {
321 22
        return $this->referenceManager;
322
    }
323
}