Completed
Push — master ( b6a1f4...39b084 )
by Jared
02:16
created

SelectQuery::getOrderBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
use JAQB\Query\Traits\OrderBy;
22
use JAQB\Query\Traits\WhereConditions;
23
24
class SelectQuery extends AbstractQuery
25
{
26
    use Executable, Fetchable, OrderBy, WhereConditions;
27
28
    /**
29
     * @var SelectStatement
30
     */
31
    protected $select;
32
33
    /**
34
     * @var FromStatement
35
     */
36
    protected $from;
37
38
    /**
39
     * @var WhereStatement
40
     */
41
    protected $having;
42
43
    /**
44
     * @var OrderStatement
45
     */
46
    protected $groupBy;
47
48
    /**
49
     * @var LimitStatement
50
     */
51
    protected $limit;
52
53
    /**
54
     * @var UnionStatement
55
     */
56
    protected $union;
57
58
    public function __construct()
59
    {
60
        $this->select = new SelectStatement();
61
        $this->from = new FromStatement();
62
        $this->where = new WhereStatement();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \JAQB\Statement\WhereStatement() of type object<JAQB\Statement\WhereStatement> is incompatible with the declared type object<JAQB\Query\Traits\WhereStatement> of property $where.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
        $this->having = new WhereStatement(true);
64
        $this->orderBy = new OrderStatement();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \JAQB\Statement\OrderStatement() of type object<JAQB\Statement\OrderStatement> is incompatible with the declared type object<JAQB\Query\Traits\OrderStatement> of property $orderBy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65
        $this->groupBy = new OrderStatement(true);
66
        $this->limit = new LimitStatement();
67
        $this->union = new UnionStatement();
68
    }
69
70
    /**
71
     * Sets the fields to be selected for the query.
72
     *
73
     * @param array|string $fields fields
74
     *
75
     * @return self
76
     */
77
    public function select($fields)
78
    {
79
        $this->select->clearFields()->addFields($fields);
80
81
        return $this;
82
    }
83
84
    /**
85
     * Sets the table for the query.
86
     *
87
     * @param string $table table name
88
     *
89
     * @return self
90
     */
91
    public function from($table)
92
    {
93
        $this->from->addTable($table);
94
95
        return $this;
96
    }
97
98
    /**
99
     * Adds a join to the query.
100
     *
101
     * @param string $table table name
102
     * @param string $on    ON condition
103
     * @param string $using USING columns
104
     * @param string $type  optional join type if not JOIN
105
     *
106
     * @return self
107
     */
108
    public function join($table, $on = null, $using = null, $type = 'JOIN')
109
    {
110
        $this->from->addJoin($table, $on, $using, $type);
111
112
        return $this;
113
    }
114
115
    /**
116
     * Sets the limit for the query.
117
     *
118
     * @param int $limit
119
     * @param int $offset
120
     *
121
     * @return self
122
     */
123
    public function limit($limit, $offset = 0)
124
    {
125
        $this->limit->setLimit($limit, $offset);
126
127
        return $this;
128
    }
129
130
    /**
131
     * Sets the group by fields for the query.
132
     *
133
     * @param string|array $fields
134
     * @param string       $direction
135
     *
136
     * @return self
137
     */
138
    public function groupBy($fields, $direction = false)
139
    {
140
        $this->groupBy->addFields($fields, $direction);
141
142
        return $this;
143
    }
144
145
    /**
146
     * Sets the having conditions for the query.
147
     *
148
     * @param array|string $field
149
     * @param string|bool  $condition condition value (optional)
150
     * @param string       $operator  operator (optional)
151
     *
152
     * @return self
153
     */
154 View Code Duplication
    public function having($field, $condition = false, $operator = '=')
155
    {
156
        if (func_num_args() >= 2) {
157
            $this->having->addCondition($field, $condition, $operator);
158
        } else {
159
            $this->having->addCondition($field);
160
        }
161
162
        return $this;
163
    }
164
165
    /**
166
     * Unions another select query with this query.
167
     *
168
     * @param SelectQuery $query
169
     * @param string      $type  optional union type
170
     *
171
     * @return self
172
     */
173
    public function union(SelectQuery $query, $type = '')
174
    {
175
        $this->union->addQuery($query, $type);
176
177
        return $this;
178
    }
179
180
    /**
181
     * Gets the select statement for the query.
182
     *
183
     * @return SelectStatement
184
     */
185
    public function getSelect()
186
    {
187
        return $this->select;
188
    }
189
190
    /**
191
     * Gets the from statement for the query.
192
     *
193
     * @return FromStatement
194
     */
195
    public function getFrom()
196
    {
197
        return $this->from;
198
    }
199
200
    /**
201
     * Gets the limit statement for the query.
202
     *
203
     * @return LimitStatement
204
     */
205
    public function getLimit()
206
    {
207
        return $this->limit;
208
    }
209
210
    /**
211
     * Gets the group by statement for the query.
212
     *
213
     * @return GroupByStatement
214
     */
215
    public function getGroupBy()
216
    {
217
        return $this->groupBy;
218
    }
219
220
    /**
221
     * Gets the having statement for the query.
222
     *
223
     * @return WhereStatement
224
     */
225
    public function getHaving()
226
    {
227
        return $this->having;
228
    }
229
230
    /**
231
     * Gets the union statement for the query.
232
     *
233
     * @return UnionStatement
234
     */
235
    public function getUnion()
236
    {
237
        return $this->union;
238
    }
239
240
    /**
241
     * Generates the raw SQL string for the query.
242
     *
243
     * @return string
244
     */
245
    public function build()
246
    {
247
        $sql = [
248
            $this->select->build(),
249
            $this->from->build(),
250
            $this->where->build(),
251
            $this->groupBy->build(),
252
            $this->having->build(),
253
            $this->orderBy->build(),
254
            $this->limit->build(),
255
            $this->union->build(),
256
        ];
257
258
        $this->values = array_merge(
259
            $this->where->getValues(),
260
            $this->having->getValues(),
261
            $this->union->getValues());
262
263
        $sql = implode(' ', array_filter($sql));
264
265
        // when there is no select statement then the query
266
        // is probably just a where subquery, thus does
267
        // not need to be prefixed with WHERE
268
        if (strtolower(substr($sql, 0, 6)) === 'where ') {
269
            return substr($sql, 6);
270
        }
271
272
        return $sql;
273
    }
274
275
    public function __clone()
276
    {
277
        $this->select = clone $this->select;
278
        $this->from = clone $this->from;
279
        $this->where = clone $this->where;
280
        $this->groupBy = clone $this->groupBy;
281
        $this->having = clone $this->having;
282
        $this->orderBy = clone $this->orderBy;
283
        $this->limit = clone $this->limit;
284
        $this->union = clone $this->union;
285
    }
286
}
287