Completed
Push — master ( 7aa158...94753f )
by Jared
02:14
created

SelectQuery::not()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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