Completed
Push — master ( 860e6f...c234e1 )
by Jared
02:12
created

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

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