Completed
Push — master ( 5a832a...3b5c12 )
by Jared
02:26
created

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

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