Completed
Push — master ( ffb751...b6a1f4 )
by Jared
02:13
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
use JAQB\Query\Traits\WhereConditions;
22
23
class SelectQuery extends AbstractQuery
24
{
25
    use Executable, Fetchable, WhereConditions;
26
27
    /**
28
     * @var SelectStatement
29
     */
30
    protected $select;
31
32
    /**
33
     * @var FromStatement
34
     */
35
    protected $from;
36
37
    /**
38
     * @var WhereStatement
39
     */
40
    protected $having;
41
42
    /**
43
     * @var OrderStatement
44
     */
45
    protected $orderBy;
46
47
    /**
48
     * @var OrderStatement
49
     */
50
    protected $groupBy;
51
52
    /**
53
     * @var LimitStatement
54
     */
55
    protected $limit;
56
57
    /**
58
     * @var UnionStatement
59
     */
60
    protected $union;
61
62
    public function __construct()
63
    {
64
        $this->select = new SelectStatement();
65
        $this->from = new FromStatement();
66
        $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...
67
        $this->having = new WhereStatement(true);
68
        $this->orderBy = new OrderStatement();
69
        $this->groupBy = new OrderStatement(true);
70
        $this->limit = new LimitStatement();
71
        $this->union = new UnionStatement();
72
    }
73
74
    /**
75
     * Sets the fields to be selected for the query.
76
     *
77
     * @param array|string $fields fields
78
     *
79
     * @return self
80
     */
81
    public function select($fields)
82
    {
83
        $this->select->clearFields()->addFields($fields);
84
85
        return $this;
86
    }
87
88
    /**
89
     * Sets the table for the query.
90
     *
91
     * @param string $table table name
92
     *
93
     * @return self
94
     */
95
    public function from($table)
96
    {
97
        $this->from->addTable($table);
98
99
        return $this;
100
    }
101
102
    /**
103
     * Adds a join to the query.
104
     *
105
     * @param string $table table name
106
     * @param string $on    ON condition
107
     * @param string $using USING columns
108
     * @param string $type  optional join type if not JOIN
109
     *
110
     * @return self
111
     */
112
    public function join($table, $on = null, $using = null, $type = 'JOIN')
113
    {
114
        $this->from->addJoin($table, $on, $using, $type);
115
116
        return $this;
117
    }
118
119
    /**
120
     * Sets the limit for the query.
121
     *
122
     * @param int $limit
123
     * @param int $offset
124
     *
125
     * @return self
126
     */
127
    public function limit($limit, $offset = 0)
128
    {
129
        $this->limit->setLimit($limit, $offset);
130
131
        return $this;
132
    }
133
134
    /**
135
     * Sets the group by fields for the query.
136
     *
137
     * @param string|array $fields
138
     * @param string       $direction
139
     *
140
     * @return self
141
     */
142
    public function groupBy($fields, $direction = false)
143
    {
144
        $this->groupBy->addFields($fields, $direction);
145
146
        return $this;
147
    }
148
149
    /**
150
     * Sets the having conditions for the query.
151
     *
152
     * @param array|string $field
153
     * @param string|bool  $condition condition value (optional)
154
     * @param string       $operator  operator (optional)
155
     *
156
     * @return self
157
     */
158 View Code Duplication
    public function having($field, $condition = false, $operator = '=')
159
    {
160
        if (func_num_args() >= 2) {
161
            $this->having->addCondition($field, $condition, $operator);
162
        } else {
163
            $this->having->addCondition($field);
164
        }
165
166
        return $this;
167
    }
168
169
    /**
170
     * Sets the order for the query.
171
     *
172
     * @param string|array $fields
173
     * @param string       $direction
174
     *
175
     * @return self
176
     */
177
    public function orderBy($fields, $direction = false)
178
    {
179
        $this->orderBy->addFields($fields, $direction);
180
181
        return $this;
182
    }
183
184
    /**
185
     * Unions another select query with this query.
186
     *
187
     * @param SelectQuery $query
188
     * @param string      $type  optional union type
189
     *
190
     * @return self
191
     */
192
    public function union(SelectQuery $query, $type = '')
193
    {
194
        $this->union->addQuery($query, $type);
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 limit statement for the query.
221
     *
222
     * @return LimitStatement
223
     */
224
    public function getLimit()
225
    {
226
        return $this->limit;
227
    }
228
229
    /**
230
     * Gets the group by statement for the query.
231
     *
232
     * @return GroupByStatement
233
     */
234
    public function getGroupBy()
235
    {
236
        return $this->groupBy;
237
    }
238
239
    /**
240
     * Gets the having statement for the query.
241
     *
242
     * @return WhereStatement
243
     */
244
    public function getHaving()
245
    {
246
        return $this->having;
247
    }
248
249
    /**
250
     * Gets the order by statement for the query.
251
     *
252
     * @return OrderByStatement
253
     */
254
    public function getOrderBy()
255
    {
256
        return $this->orderBy;
257
    }
258
259
    /**
260
     * Gets the union statement for the query.
261
     *
262
     * @return UnionStatement
263
     */
264
    public function getUnion()
265
    {
266
        return $this->union;
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
            $this->union->build(),
285
        ];
286
287
        $this->values = array_merge(
288
            $this->where->getValues(),
289
            $this->having->getValues(),
290
            $this->union->getValues());
291
292
        $sql = implode(' ', array_filter($sql));
293
294
        // when there is no select statement then the query
295
        // is probably just a where subquery, thus does
296
        // not need to be prefixed with WHERE
297
        if (strtolower(substr($sql, 0, 6)) === 'where ') {
298
            return substr($sql, 6);
299
        }
300
301
        return $sql;
302
    }
303
304
    public function __clone()
305
    {
306
        $this->select = clone $this->select;
307
        $this->from = clone $this->from;
308
        $this->where = clone $this->where;
309
        $this->groupBy = clone $this->groupBy;
310
        $this->having = clone $this->having;
311
        $this->orderBy = clone $this->orderBy;
312
        $this->limit = clone $this->limit;
313
        $this->union = clone $this->union;
314
    }
315
}
316