Completed
Push — master ( 07eb26...46e6ae )
by Jared
02:23
created

SelectQuery::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace JAQB\Query;
12
13
use JAQB\Operations\Executable;
14
use JAQB\Operations\Fetchable;
15
use JAQB\Statement\SelectStatement;
16
use JAQB\Statement\FromStatement;
17
use JAQB\Statement\WhereStatement;
18
use JAQB\Statement\OrderStatement;
19
use JAQB\Statement\LimitStatement;
20
use JAQB\Statement\UnionStatement;
21
22
class SelectQuery extends AbstractQuery
23
{
24
    use Executable, Fetchable;
25
26
    /**
27
     * @var SelectStatement
28
     */
29
    protected $select;
30
31
    /**
32
     * @var FromStatement
33
     */
34
    protected $from;
35
36
    /**
37
     * @var WhereStatement
38
     */
39
    protected $where;
40
41
    /**
42
     * @var WhereStatement
43
     */
44
    protected $having;
45
46
    /**
47
     * @var OrderStatement
48
     */
49
    protected $orderBy;
50
51
    /**
52
     * @var OrderStatement
53
     */
54
    protected $groupBy;
55
56
    /**
57
     * @var LimitStatement
58
     */
59
    protected $limit;
60
61
    /**
62
     * @var UnionStatement
63
     */
64
    protected $union;
65
66
    public function __construct()
67
    {
68
        $this->select = new SelectStatement();
69
        $this->from = new FromStatement();
70
        $this->where = new WhereStatement();
71
        $this->having = new WhereStatement(true);
72
        $this->orderBy = new OrderStatement();
73
        $this->groupBy = new OrderStatement(true);
74
        $this->limit = new LimitStatement();
75
        $this->union = new UnionStatement();
76
    }
77
78
    /**
79
     * Sets the fields to be selected for the query.
80
     *
81
     * @param array|string $fields fields
82
     *
83
     * @return self
84
     */
85
    public function select($fields)
86
    {
87
        $this->select->addFields($fields);
88
89
        return $this;
90
    }
91
92
    /**
93
     * Sets the table for the query.
94
     *
95
     * @param string $table table name
96
     *
97
     * @return self
98
     */
99
    public function from($table)
100
    {
101
        $this->from->addTable($table);
102
103
        return $this;
104
    }
105
106
    /**
107
     * Adds a join to the query.
108
     *
109
     * @param string $table table name
110
     * @param string $on    ON condition
111
     * @param string $using USING columns
112
     * @param string $type  optional join type if not JOIN
113
     *
114
     * @return self
115
     */
116
    public function join($table, $on = null, $using = null, $type = 'JOIN')
117
    {
118
        $this->from->addJoin($table, $on, $using, $type);
119
120
        return $this;
121
    }
122
123
    /**
124
     * Adds a where condition to the query.
125
     *
126
     * @param array|string $field
127
     * @param string       $condition condition value (optional)
128
     * @param string       $operator  operator (optional)
129
     *
130
     * @return self
131
     */
132 View Code Duplication
    public function where($field, $condition = false, $operator = '=')
133
    {
134
        if (func_num_args() >= 2) {
135
            $this->where->addCondition($field, $condition, $operator);
136
        } else {
137
            $this->where->addCondition($field);
138
        }
139
140
        return $this;
141
    }
142
143
    /**
144
     * Adds a where or condition to the query.
145
     *
146
     * @param array|string $field
147
     * @param string       $condition condition value (optional)
148
     * @param string       $operator  operator (optional)
149
     *
150
     * @return self
151
     */
152 View Code Duplication
    public function orWhere($field, $condition = false, $operator = '=')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        if (func_num_args() >= 2) {
155
            $this->where->addConditionOr($field, $condition, $operator);
156
        } else {
157
            $this->where->addConditionOr($field);
158
        }
159
160
        return $this;
161
    }
162
163
    /**
164
     * Adds a where not condition to the query.
165
     *
166
     * @param string $field
167
     * @param string $condition condition value (optional)
168
     *
169
     * @return self
170
     */
171
    public function not($field, $condition = true)
172
    {
173
        $this->where->addCondition($field, $condition, '<>');
174
175
        return $this;
176
    }
177
178
    /**
179
     * Adds a where between condition to the query.
180
     *
181
     * @param string $field
182
     * @param mixed  $a     first between value
183
     * @param mixed  $b     second between value
184
     *
185
     * @return self
186
     */
187
    public function between($field, $a, $b)
188
    {
189
        $this->where->addBetweenCondition($field, $a, $b);
190
191
        return $this;
192
    }
193
194
    /**
195
     * Adds a where not between condition to the query.
196
     *
197
     * @param string $field
198
     * @param mixed  $a     first between value
199
     * @param mixed  $b     second between value
200
     *
201
     * @return self
202
     */
203
    public function notBetween($field, $a, $b)
204
    {
205
        $this->where->addNotBetweenCondition($field, $a, $b);
206
207
        return $this;
208
    }
209
210
    /**
211
     * Adds an exists condition to the query.
212
     *
213
     * @param callable $f
214
     *
215
     * @return self
216
     */
217
    public function exists(callable $f)
218
    {
219
        $this->where->addExistsCondition($f);
220
221
        return $this;
222
    }
223
224
    /**
225
     * Adds a not exists condition to the query.
226
     *
227
     * @param callable $f
228
     *
229
     * @return self
230
     */
231
    public function notExists(callable $f)
232
    {
233
        $this->where->addNotExistsCondition($f);
234
235
        return $this;
236
    }
237
238
    /**
239
     * Sets the limit for the query.
240
     *
241
     * @param int $limit
242
     * @param int $offset
243
     *
244
     * @return self
245
     */
246
    public function limit($limit, $offset = 0)
247
    {
248
        $this->limit->setLimit($limit, $offset);
249
250
        return $this;
251
    }
252
253
    /**
254
     * Sets the group by fields for the query.
255
     *
256
     * @param string|array $fields
257
     * @param string       $direction
258
     *
259
     * @return self
260
     */
261
    public function groupBy($fields, $direction = false)
262
    {
263
        $this->groupBy->addFields($fields, $direction);
264
265
        return $this;
266
    }
267
268
    /**
269
     * Sets the having conditions for the query.
270
     *
271
     * @param array|string $field
272
     * @param string|bool  $condition condition value (optional)
273
     * @param string       $operator  operator (optional)
274
     *
275
     * @return self
276
     */
277 View Code Duplication
    public function having($field, $condition = false, $operator = '=')
278
    {
279
        if (func_num_args() >= 2) {
280
            $this->having->addCondition($field, $condition, $operator);
281
        } else {
282
            $this->having->addCondition($field);
283
        }
284
285
        return $this;
286
    }
287
288
    /**
289
     * Sets the order for the query.
290
     *
291
     * @param string|array $fields
292
     * @param string       $direction
293
     *
294
     * @return self
295
     */
296
    public function orderBy($fields, $direction = false)
297
    {
298
        $this->orderBy->addFields($fields, $direction);
299
300
        return $this;
301
    }
302
303
    /**
304
     * Unions another select query with this query.
305
     *
306
     * @param SelectQuery $query
307
     * @param string      $type  optional union type
308
     *
309
     * @return self
310
     */
311
    public function union(SelectQuery $query, $type = false)
312
    {
313
        $this->union->addQuery($query, $type);
314
315
        return $this;
316
    }
317
318
    /**
319
     * Gets the select statement for the query.
320
     *
321
     * @return SelectStatement
322
     */
323
    public function getSelect()
324
    {
325
        return $this->select;
326
    }
327
328
    /**
329
     * Gets the from statement for the query.
330
     *
331
     * @return FromStatement
332
     */
333
    public function getFrom()
334
    {
335
        return $this->from;
336
    }
337
338
    /**
339
     * Gets the where statement for the query.
340
     *
341
     * @return WhereStatement
342
     */
343
    public function getWhere()
344
    {
345
        return $this->where;
346
    }
347
348
    /**
349
     * Gets the limit statement for the query.
350
     *
351
     * @return LimitStatement
352
     */
353
    public function getLimit()
354
    {
355
        return $this->limit;
356
    }
357
358
    /**
359
     * Gets the group by statement for the query.
360
     *
361
     * @return GroupByStatement
362
     */
363
    public function getGroupBy()
364
    {
365
        return $this->groupBy;
366
    }
367
368
    /**
369
     * Gets the having statement for the query.
370
     *
371
     * @return HavingStatement
372
     */
373
    public function getHaving()
374
    {
375
        return $this->having;
376
    }
377
378
    /**
379
     * Gets the order by statement for the query.
380
     *
381
     * @return OrderByStatement
382
     */
383
    public function getOrderBy()
384
    {
385
        return $this->orderBy;
386
    }
387
388
    /**
389
     * Gets the union statement for the query.
390
     *
391
     * @return UnionStatement
392
     */
393
    public function getUnion()
394
    {
395
        return $this->union;
396
    }
397
398
    /**
399
     * Generates the raw SQL string for the query.
400
     *
401
     * @return string
402
     */
403
    public function build()
404
    {
405
        $sql = [
406
            $this->select->build(),
407
            $this->from->build(),
408
            $this->where->build(),
409
            $this->groupBy->build(),
410
            $this->having->build(),
411
            $this->orderBy->build(),
412
            $this->limit->build(),
413
            $this->union->build(),
414
        ];
415
416
        $this->values = array_merge(
417
            $this->where->getValues(),
418
            $this->having->getValues(),
419
            $this->union->getValues());
420
421
        return implode(' ', array_filter($sql));
422
    }
423
424
    public function __clone()
425
    {
426
        $this->select = clone $this->select;
427
        $this->from = clone $this->from;
428
        $this->where = clone $this->where;
429
        $this->groupBy = clone $this->groupBy;
430
        $this->having = clone $this->having;
431
        $this->orderBy = clone $this->orderBy;
432
        $this->limit = clone $this->limit;
433
        $this->union = clone $this->union;
434
    }
435
}
436