Completed
Push — master ( 609ee9...07eb26 )
by Jared
02:17
created

SelectQuery::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
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
     * Sets the limit for the query.
212
     *
213
     * @param int $limit
214
     * @param int $offset
215
     *
216
     * @return self
217
     */
218
    public function limit($limit, $offset = 0)
219
    {
220
        $this->limit->setLimit($limit, $offset);
221
222
        return $this;
223
    }
224
225
    /**
226
     * Sets the group by fields for the query.
227
     *
228
     * @param string|array $fields
229
     * @param string       $direction
230
     *
231
     * @return self
232
     */
233
    public function groupBy($fields, $direction = false)
234
    {
235
        $this->groupBy->addFields($fields, $direction);
236
237
        return $this;
238
    }
239
240
    /**
241
     * Sets the having conditions for the query.
242
     *
243
     * @param array|string $field
244
     * @param string|bool  $condition condition value (optional)
245
     * @param string       $operator  operator (optional)
246
     *
247
     * @return self
248
     */
249 View Code Duplication
    public function having($field, $condition = false, $operator = '=')
250
    {
251
        if (func_num_args() >= 2) {
252
            $this->having->addCondition($field, $condition, $operator);
253
        } else {
254
            $this->having->addCondition($field);
255
        }
256
257
        return $this;
258
    }
259
260
    /**
261
     * Sets the order for the query.
262
     *
263
     * @param string|array $fields
264
     * @param string       $direction
265
     *
266
     * @return self
267
     */
268
    public function orderBy($fields, $direction = false)
269
    {
270
        $this->orderBy->addFields($fields, $direction);
271
272
        return $this;
273
    }
274
275
    /**
276
     * Unions another select query with this query.
277
     *
278
     * @param SelectQuery $query
279
     * @param string      $type  optional union type
280
     *
281
     * @return self
282
     */
283
    public function union(SelectQuery $query, $type = false)
284
    {
285
        $this->union->addQuery($query, $type);
286
287
        return $this;
288
    }
289
290
    /**
291
     * Gets the select statement for the query.
292
     *
293
     * @return SelectStatement
294
     */
295
    public function getSelect()
296
    {
297
        return $this->select;
298
    }
299
300
    /**
301
     * Gets the from statement for the query.
302
     *
303
     * @return FromStatement
304
     */
305
    public function getFrom()
306
    {
307
        return $this->from;
308
    }
309
310
    /**
311
     * Gets the where statement for the query.
312
     *
313
     * @return WhereStatement
314
     */
315
    public function getWhere()
316
    {
317
        return $this->where;
318
    }
319
320
    /**
321
     * Gets the limit statement for the query.
322
     *
323
     * @return LimitStatement
324
     */
325
    public function getLimit()
326
    {
327
        return $this->limit;
328
    }
329
330
    /**
331
     * Gets the group by statement for the query.
332
     *
333
     * @return GroupByStatement
334
     */
335
    public function getGroupBy()
336
    {
337
        return $this->groupBy;
338
    }
339
340
    /**
341
     * Gets the having statement for the query.
342
     *
343
     * @return HavingStatement
344
     */
345
    public function getHaving()
346
    {
347
        return $this->having;
348
    }
349
350
    /**
351
     * Gets the order by statement for the query.
352
     *
353
     * @return OrderByStatement
354
     */
355
    public function getOrderBy()
356
    {
357
        return $this->orderBy;
358
    }
359
360
    /**
361
     * Gets the union statement for the query.
362
     *
363
     * @return UnionStatement
364
     */
365
    public function getUnion()
366
    {
367
        return $this->union;
368
    }
369
370
    /**
371
     * Generates the raw SQL string for the query.
372
     *
373
     * @return string
374
     */
375
    public function build()
376
    {
377
        $sql = [
378
            $this->select->build(),
379
            $this->from->build(),
380
            $this->where->build(),
381
            $this->groupBy->build(),
382
            $this->having->build(),
383
            $this->orderBy->build(),
384
            $this->limit->build(),
385
            $this->union->build(),
386
        ];
387
388
        $this->values = array_merge(
389
            $this->where->getValues(),
390
            $this->having->getValues(),
391
            $this->union->getValues());
392
393
        return implode(' ', array_filter($sql));
394
    }
395
396
    public function __clone()
397
    {
398
        $this->select = clone $this->select;
399
        $this->from = clone $this->from;
400
        $this->where = clone $this->where;
401
        $this->groupBy = clone $this->groupBy;
402
        $this->having = clone $this->having;
403
        $this->orderBy = clone $this->orderBy;
404
        $this->limit = clone $this->limit;
405
        $this->union = clone $this->union;
406
    }
407
}
408