Passed
Push — master ( 9db5ce...9e9229 )
by Rougin
01:41
created

Query::innerJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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