Completed
Push — master ( a7b8c4...d685b1 )
by Jared
02:06
created

SelectQuery::union()   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 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
     * Sets the where conditions for 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
     * Sets the limit for the query.
145
     *
146
     * @param int $limit
147
     * @param int $offset
148
     *
149
     * @return self
150
     */
151
    public function limit($limit, $offset = 0)
152
    {
153
        $this->limit->setLimit($limit, $offset);
154
155
        return $this;
156
    }
157
158
    /**
159
     * Sets the group by fields for the query.
160
     *
161
     * @param string|array $fields
162
     * @param string       $direction
163
     *
164
     * @return self
165
     */
166
    public function groupBy($fields, $direction = false)
167
    {
168
        $this->groupBy->addFields($fields, $direction);
169
170
        return $this;
171
    }
172
173
    /**
174
     * Sets the having conditions for the query.
175
     *
176
     * @param array|string $field
177
     * @param string|bool  $condition condition value (optional)
178
     * @param string       $operator  operator (optional)
179
     *
180
     * @return self
181
     */
182 View Code Duplication
    public function having($field, $condition = false, $operator = '=')
183
    {
184
        if (func_num_args() >= 2) {
185
            $this->having->addCondition($field, $condition, $operator);
186
        } else {
187
            $this->having->addCondition($field);
188
        }
189
190
        return $this;
191
    }
192
193
    /**
194
     * Sets the order for the query.
195
     *
196
     * @param string|array $fields
197
     * @param string       $direction
198
     *
199
     * @return self
200
     */
201
    public function orderBy($fields, $direction = false)
202
    {
203
        $this->orderBy->addFields($fields, $direction);
204
205
        return $this;
206
    }
207
208
    /**
209
     * Unions another select query with this query.
210
     *
211
     * @param SelectQuery $query
212
     * @param string      $type  optional union type
213
     *
214
     * @return self
215
     */
216
    public function union(SelectQuery $query, $type = false)
217
    {
218
        $this->union->addQuery($query, $type);
219
220
        return $this;
221
    }
222
223
    /**
224
     * Gets the select statement for the query.
225
     *
226
     * @return SelectStatement
227
     */
228
    public function getSelect()
229
    {
230
        return $this->select;
231
    }
232
233
    /**
234
     * Gets the from statement for the query.
235
     *
236
     * @return FromStatement
237
     */
238
    public function getFrom()
239
    {
240
        return $this->from;
241
    }
242
243
    /**
244
     * Gets the where statement for the query.
245
     *
246
     * @return WhereStatement
247
     */
248
    public function getWhere()
249
    {
250
        return $this->where;
251
    }
252
253
    /**
254
     * Gets the limit statement for the query.
255
     *
256
     * @return LimitStatement
257
     */
258
    public function getLimit()
259
    {
260
        return $this->limit;
261
    }
262
263
    /**
264
     * Gets the group by statement for the query.
265
     *
266
     * @return GroupByStatement
267
     */
268
    public function getGroupBy()
269
    {
270
        return $this->groupBy;
271
    }
272
273
    /**
274
     * Gets the having statement for the query.
275
     *
276
     * @return HavingStatement
277
     */
278
    public function getHaving()
279
    {
280
        return $this->having;
281
    }
282
283
    /**
284
     * Gets the order by statement for the query.
285
     *
286
     * @return OrderByStatement
287
     */
288
    public function getOrderBy()
289
    {
290
        return $this->orderBy;
291
    }
292
293
    /**
294
     * Gets the union statement for the query.
295
     *
296
     * @return UnionStatement
297
     */
298
    public function getUnion()
299
    {
300
        return $this->union;
301
    }
302
303
    /**
304
     * Generates the raw SQL string for the query.
305
     *
306
     * @return string
307
     */
308
    public function build()
309
    {
310
        $sql = [
311
            $this->select->build(),
312
            $this->from->build(),
313
            $this->where->build(),
314
            $this->groupBy->build(),
315
            $this->having->build(),
316
            $this->orderBy->build(),
317
            $this->limit->build(),
318
            $this->union->build(),
319
        ];
320
321
        $this->values = array_merge(
322
            $this->where->getValues(),
323
            $this->having->getValues(),
324
            $this->union->getValues());
325
326
        return implode(' ', array_filter($sql));
327
    }
328
329
    public function __clone()
330
    {
331
        $this->select = clone $this->select;
332
        $this->from = clone $this->from;
333
        $this->where = clone $this->where;
334
        $this->groupBy = clone $this->groupBy;
335
        $this->having = clone $this->having;
336
        $this->orderBy = clone $this->orderBy;
337
        $this->limit = clone $this->limit;
338
        $this->union = clone $this->union;
339
    }
340
}
341