Completed
Push — master ( 78477d...70d38b )
by Beniamin
03:04
created

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