Completed
Push — master ( f9ab88...d8c240 )
by Jared
02:22
created

SelectQuery::join()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 4
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\Statement\SelectStatement;
14
use JAQB\Statement\FromStatement;
15
use JAQB\Statement\WhereStatement;
16
use JAQB\Statement\OrderStatement;
17
use JAQB\Statement\LimitStatement;
18
19
class SelectQuery extends Query
20
{
21
    use SelectableTrait;
22
23
    /**
24
     * @var SelectStatement
25
     */
26
    protected $select;
27
28
    /**
29
     * @var FromStatement
30
     */
31
    protected $from;
32
33
    /**
34
     * @var WhereStatement
35
     */
36
    protected $where;
37
38
    /**
39
     * @var WhereStatement
40
     */
41
    protected $having;
42
43
    /**
44
     * @var OrderStatement
45
     */
46
    protected $orderBy;
47
48
    /**
49
     * @var OrderStatement
50
     */
51
    protected $groupBy;
52
53
    /**
54
     * @var LimitStatement
55
     */
56
    protected $limit;
57
58
    public function initialize()
59
    {
60
        $this->select = new SelectStatement();
61
        $this->from = new FromStatement();
62
        $this->where = new WhereStatement();
63
        $this->having = new WhereStatement(true);
64
        $this->orderBy = new OrderStatement();
65
        $this->groupBy = new OrderStatement(true);
66
        $this->limit = new LimitStatement();
67
    }
68
69
    /**
70
     * Sets the fields to be selected for the query.
71
     *
72
     * @param array|string $fields fields
73
     *
74
     * @return self
75
     */
76
    public function select($fields)
77
    {
78
        $this->select->addFields($fields);
79
80
        return $this;
81
    }
82
83
    /**
84
     * Sets the table for the query.
85
     *
86
     * @param string $table table name
87
     *
88
     * @return self
89
     */
90
    public function from($table)
91
    {
92
        $this->from->addTable($table);
93
94
        return $this;
95
    }
96
97
    /**
98
     * Adds a join to the query.
99
     *
100
     * @param string $table table name
101
     * @param string $on    ON condition
102
     * @param string $using USING columns
103
     * @param string $type  optional join type if not JOIN
104
     *
105
     * @return self
106
     */
107
    public function join($table, $on = null, $using = null, $type = 'JOIN')
108
    {
109
        $this->from->addJoin($table, $on, $using, $type);
110
111
        return $this;
112
    }
113
114
    /**
115
     * Sets the where conditions for the query.
116
     *
117
     * @param array|string $field
118
     * @param string       $condition condition value (optional)
119
     * @param string       $operator  operator (optional)
120
     *
121
     * @return self
122
     */
123 View Code Duplication
    public function where($field, $condition = false, $operator = '=')
124
    {
125
        if (func_num_args() >= 2) {
126
            $this->where->addCondition($field, $condition, $operator);
127
        } else {
128
            $this->where->addCondition($field);
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * Sets the limit for the query.
136
     *
137
     * @param int $limit
138
     * @param int $offset
139
     *
140
     * @return self
141
     */
142
    public function limit($limit, $offset = 0)
143
    {
144
        $this->limit->setLimit($limit, $offset);
145
146
        return $this;
147
    }
148
149
    /**
150
     * Sets the group by fields for the query.
151
     *
152
     * @param string|array $fields
153
     * @param string       $direction
154
     *
155
     * @return self
156
     */
157
    public function groupBy($fields, $direction = false)
158
    {
159
        $this->groupBy->addFields($fields, $direction);
160
161
        return $this;
162
    }
163
164
    /**
165
     * Sets the having conditions for the query.
166
     *
167
     * @param array|string $field
168
     * @param string|bool  $condition condition value (optional)
169
     * @param string       $operator  operator (optional)
170
     *
171
     * @return self
172
     */
173 View Code Duplication
    public function having($field, $condition = false, $operator = '=')
174
    {
175
        if (func_num_args() >= 2) {
176
            $this->having->addCondition($field, $condition, $operator);
177
        } else {
178
            $this->having->addCondition($field);
179
        }
180
181
        return $this;
182
    }
183
184
    /**
185
     * Sets the order for the query.
186
     *
187
     * @param string|array $fields
188
     * @param string       $direction
189
     *
190
     * @return self
191
     */
192
    public function orderBy($fields, $direction = false)
193
    {
194
        $this->orderBy->addFields($fields, $direction);
195
196
        return $this;
197
    }
198
199
    /**
200
     * Gets the select statement for the query.
201
     *
202
     * @return SelectStatement
203
     */
204
    public function getSelect()
205
    {
206
        return $this->select;
207
    }
208
209
    /**
210
     * Gets the from statement for the query.
211
     *
212
     * @return FromStatement
213
     */
214
    public function getFrom()
215
    {
216
        return $this->from;
217
    }
218
219
    /**
220
     * Gets the where statement for the query.
221
     *
222
     * @return WhereStatement
223
     */
224
    public function getWhere()
225
    {
226
        return $this->where;
227
    }
228
229
    /**
230
     * Gets the limit statement for the query.
231
     *
232
     * @return LimitStatement
233
     */
234
    public function getLimit()
235
    {
236
        return $this->limit;
237
    }
238
239
    /**
240
     * Gets the group by statement for the query.
241
     *
242
     * @return GroupByStatement
243
     */
244
    public function getGroupBy()
245
    {
246
        return $this->groupBy;
247
    }
248
249
    /**
250
     * Gets the having statement for the query.
251
     *
252
     * @return HavingStatement
253
     */
254
    public function getHaving()
255
    {
256
        return $this->having;
257
    }
258
259
    /**
260
     * Gets the order by statement for the query.
261
     *
262
     * @return OrderByStatement
263
     */
264
    public function getOrderBy()
265
    {
266
        return $this->orderBy;
267
    }
268
269
    /**
270
     * Generates the raw SQL string for the query.
271
     *
272
     * @return string
273
     */
274
    public function build()
275
    {
276
        $sql = [
277
            $this->select->build(),
278
            $this->from->build(),
279
            $this->where->build(),
280
            $this->groupBy->build(),
281
            $this->having->build(),
282
            $this->orderBy->build(),
283
            $this->limit->build(),
284
        ];
285
286
        $this->values = array_merge($this->where->getValues(),
287
            $this->having->getValues());
288
289
        return implode(' ', array_filter($sql));
290
    }
291
}
292