Passed
Push — master ( b170a3...f0e277 )
by Rougin
02:16
created

Query   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 407
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
eloc 70
dl 0
loc 407
ccs 100
cts 100
cp 1
rs 9.92
c 0
b 0
f 0

28 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __toString() 0 3 1
A select() 0 7 1
A insertInto() 0 5 1
A condition() 0 9 1
A orHaving() 0 3 1
A andWhere() 0 3 1
A innerJoin() 0 7 1
A orderBy() 0 3 1
A builder() 0 5 1
A orWhere() 0 3 1
A reset() 0 15 1
A where() 0 3 1
A leftJoin() 0 7 1
A instance() 0 3 1
A from() 0 9 1
A limit() 0 10 2
A rightJoin() 0 7 1
A update() 0 7 1
A having() 0 3 1
A bindings() 0 3 1
A groupBy() 0 5 1
A andHaving() 0 3 1
A types() 0 3 1
A deleteFrom() 0 11 1
A andOrderBy() 0 3 1
A sql() 0 3 1
A alias() 0 17 3
1
<?php
2
3
namespace Rougin\Windstorm\Doctrine;
4
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use Rougin\Windstorm\QueryInterface;
7
use Rougin\Windstorm\ResultInterface;
8
9
/**
10
 * Query
11
 *
12
 * @package Windstorm
13
 * @author  Rougin Gutib <[email protected]>
14
 */
15
class Query implements QueryInterface
16
{
17
    /**
18
     * @var \Doctrine\DBAL\Query\QueryBuilder
19
     */
20
    protected $builder;
21
22
    /**
23
     * @var string|null
24
     */
25
    protected $initial = null;
26
27
    /**
28
     * @var string
29
     */
30
    protected $table = '';
31
32
    /**
33
     * Returns the safe and compiled SQL.
34
     *
35
     * @return string
36
     */
37 3
    public function __toString()
38
    {
39 3
        return $this->sql();
40
    }
41
42
    /**
43
     * Initializes the query instance.
44
     *
45
     * @param \Doctrine\DBAL\Query\QueryBuilder $builder
46
     */
47 144
    public function __construct(QueryBuilder $builder)
48
    {
49 144
        $this->builder = $builder;
50 144
    }
51
52
    /**
53
     * Generates a SELECT query.
54
     *
55
     * @param  array  $fields
56
     * @return self
57
     */
58 108
    public function select(array $fields)
59 3
    {
60 108
        $this->reset();
61
62 105
        $this->builder->select($fields);
63
64 105
        return $this;
65
    }
66
67
    /**
68
     * Generates a FROM query.
69
     *
70
     * @param  string      $table
71
     * @param  string|null $alias
72
     * @return self
73
     */
74 105
    public function from($table, $alias = null)
75
    {
76 105
        $this->initial = $alias;
77
78 105
        $this->table = $table;
79
80 105
        $this->builder->from($table, $alias);
81
82 105
        return $this;
83
    }
84
85
    /**
86
     * Generates an INNER JOIN query.
87
     *
88
     * @param  string $table
89
     * @param  string $local
90
     * @param  string $foreign
91
     * @return self
92
     */
93 3
    public function innerJoin($table, $local, $foreign)
94
    {
95 3
        list($alias, $where) = $this->condition($table, $local, $foreign);
96
97 3
        $this->builder->innerJoin($this->initial, $table, $alias, $where);
98
99 3
        return $this;
100
    }
101
102
    /**
103
     * Generates a LEFT JOIN query.
104
     *
105
     * @param  string $table
106
     * @param  string $local
107
     * @param  string $foreign
108
     * @return self
109
     */
110 3
    public function leftJoin($table, $local, $foreign)
111
    {
112 3
        list($alias, $where) = $this->condition($table, $local, $foreign);
113
114 3
        $this->builder->leftJoin($this->initial, $table, $alias, $where);
115
116 3
        return $this;
117
    }
118
119
    /**
120
     * Generates a RIGHT JOIN query.
121
     *
122
     * @param  string $table
123
     * @param  string $local
124
     * @param  string $foreign
125
     * @return self
126
     */
127 3
    public function rightJoin($table, $local, $foreign)
128
    {
129 3
        list($alias, $where) = $this->condition($table, $local, $foreign);
130
131 3
        $this->builder->rightJoin($this->initial, $table, $alias, $where);
132
133 3
        return $this;
134
    }
135
136
    /**
137
     * Generates an INSERT INTO query.
138
     *
139
     * @param  string $table
140
     * @return \Rougin\Windstorm\InsertInterface
141
     */
142 6
    public function insertInto($table)
143
    {
144 6
        $this->reset();
145
146 6
        return new Insert($this, $this->builder, $table);
147
    }
148
149
    /**
150
     * Generates an UPDATE query.
151
     *
152
     * @param  string      $table
153
     * @param  string|null $alias
154
     * @return \Rougin\Windstorm\UpdateInterface
155
     */
156 12
    public function update($table, $alias = null)
157
    {
158 12
        $this->reset();
159
160 12
        list($this->initial, $this->table) = array($alias, $table);
161
162 12
        return new Update($this, $this->builder, $table, $alias);
163
    }
164
165
    /**
166
     * Generates a DELETE FROM query.
167
     *
168
     * @param  string      $table
169
     * @param  string|null $alias
170
     * @return self
171
     */
172 18
    public function deleteFrom($table, $alias = null)
173
    {
174 18
        $this->reset();
175
176 18
        $this->initial = $alias;
177
178 18
        $this->table = $table;
179
180 18
        $this->builder->delete($table, $this->initial);
181
182 18
        return $this;
183
    }
184
185
    /**
186
     * Generates a WHERE query.
187
     *
188
     * @param  string $key
189
     * @return \Rougin\Windstorm\WhereInterface
190
     */
191 84
    public function where($key)
192
    {
193 84
        return new Where($this, $this->builder, $key, $this->initial);
194
    }
195
196
    /**
197
     * Generates an AND WHERE query.
198
     *
199
     * @param  string $key
200
     * @return \Rougin\Windstorm\WhereInterface
201
     */
202 3
    public function andWhere($key)
203
    {
204 3
        return new Where($this, $this->builder, $key, $this->initial, 'AND');
205
    }
206
207
    /**
208
     * Generates an OR WHERE query.
209
     *
210
     * @param  string $key
211
     * @return \Rougin\Windstorm\WhereInterface
212
     */
213 3
    public function orWhere($key)
214
    {
215 3
        return new Where($this, $this->builder, $key, $this->initial, 'OR');
216
    }
217
218
    /**
219
     * Generates a GROUP BY query.
220
     *
221
     * @param  array $fields
222
     * @return self
223
     */
224 3
    public function groupBy(array $fields)
225
    {
226 3
        $this->builder->groupBy($fields);
227
228 3
        return $this;
229
    }
230
231
    /**
232
     * Generates a HAVING query.
233
     *
234
     * @param  string $key
235
     * @return \Rougin\Windstorm\HavingInterface
236
     */
237 9
    public function having($key)
238
    {
239 9
        return new Having($this, $this->builder, $key, $this->initial);
240
    }
241
242
    /**
243
     * Generates an AND HAVING query.
244
     *
245
     * @param  string $key
246
     * @return \Rougin\Windstorm\HavingInterface
247
     */
248 3
    public function andHaving($key)
249
    {
250 3
        return new Having($this, $this->builder, $key, $this->initial, 'AND');
251
    }
252
253
    /**
254
     * Generates an OR HAVING query.
255
     *
256
     * @param  string $key
257
     * @return \Rougin\Windstorm\HavingInterface
258
     */
259 3
    public function orHaving($key)
260
    {
261 3
        return new Having($this, $this->builder, $key, $this->initial, 'OR');
262
    }
263
264
    /**
265
     * Generates an ORDER BY query.
266
     *
267
     * @param  string $key
268
     * @return \Rougin\Windstorm\OrderInterface
269
     */
270 18
    public function orderBy($key)
271
    {
272 18
        return new Order($this, $this->builder, $key, $this->initial);
273
    }
274
275
    /**
276
     * Generates a multiple ORDER BY query.
277
     *
278
     * @param  string $key
279
     * @return \Rougin\Windstorm\OrderInterface
280
     */
281 3
    public function andOrderBy($key)
282
    {
283 3
        return new Order($this, $this->builder, $key, $this->initial, 'ADD');
284
    }
285
286
    /**
287
     * Performs a LIMIT query.
288
     *
289
     * @param  integer      $limit
290
     * @param  integer|null $offset
291
     * @return self
292
     */
293 15
    public function limit($limit, $offset = null)
294
    {
295 15
        $this->builder->setMaxResults($limit);
296
297 15
        if ($offset !== null)
298 15
        {
299 12
            $this->builder->setFirstResult($offset);
300 12
        }
301
302 15
        return $this;
303
    }
304
305
    /**
306
     * Sets the Builder instance.
307
     *
308
     * @param  \Doctrine\DBAL\Query\QueryBuilder $builder
309
     * @return self
310
     */
311 117
    public function builder(QueryBuilder $builder)
312
    {
313 117
        $this->builder = $builder;
314
315 117
        return $this;
316
    }
317
318
    /**
319
     * Returns the safe and compiled SQL.
320
     *
321
     * @return string
322
     */
323 135
    public function sql()
324
    {
325 135
        return $this->builder->getSql();
326
    }
327
328
    /**
329
     * Returns the SQL bindings specified.
330
     *
331
     * @return array
332
     */
333 12
    public function bindings()
334
    {
335 12
        return $this->builder->getParameters();
336
    }
337
338
    /**
339
     * Returns the data types of the bindings.
340
     *
341
     * @return array
342
     */
343 12
    public function types()
344
    {
345 12
        return $this->builder->getParameterTypes();
346
    }
347
348
    /**
349
     * Returns the instance of the query builder, if available.
350
     *
351
     * @return mixed
352
     */
353 3
    public function instance()
354
    {
355 3
        return $this->builder;
356
    }
357
358
    /**
359
     * Returns an available table alias.
360
     *
361
     * @param  string $table
362
     * @return string
363
     */
364 9
    protected function alias($table)
365
    {
366 9
        $characters = str_split($table);
367
368 9
        $result = $characters[0];
369
370 9
        foreach ($characters as $character)
371
        {
372 9
            $character = strtolower($character);
373
374 9
            if ($this->initial !== $character)
375 9
            {
376 9
                $result = $character; break;
377
            }
378 9
        }
379
380 9
        return (string) $result;
381
    }
382
383
    /**
384
     * Returns a JOIN condition.
385
     *
386
     * @param  string $table
387
     * @param  string $local
388
     * @param  string $foreign
389
     * @return string
390
     */
391 9
    protected function condition($table, $local, $foreign)
392
    {
393 9
        $condition = $this->initial . '.' . $local;
394
395 9
        $alias = $this->alias((string) $table);
396
397 9
        $condition .= ' = ' . $alias . '.' . $foreign;
398
399 9
        return array($alias, (string) $condition);
400
    }
401
402
    /**
403
     * Resets the whole query builder.
404
     *
405
     * @return self
406
     */
407 141
    protected function reset()
408
    {
409 141
        $this->builder->setMaxResults(null);
410
411 141
        $this->builder->setFirstResult(null);
412
413 141
        $this->initial = (string) '';
414
415 141
        $this->builder->resetQueryParts();
416
417 141
        $this->table = (string) '';
418
419 141
        $this->builder->setParameters(array());
420
421 141
        return $this;
422
    }
423
}
424