1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Phuria SQL Builder package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phuria\QueryBuilder; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class QueryClauses |
18
|
|
|
{ |
19
|
|
|
const QUERY_SELECT = 1; |
20
|
|
|
const QUERY_UPDATE = 2; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array $selectClauses |
24
|
|
|
*/ |
25
|
|
|
private $selectClauses = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array $whereClauses |
29
|
|
|
*/ |
30
|
|
|
private $whereClauses = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array $orderByClauses |
34
|
|
|
*/ |
35
|
|
|
private $orderByClauses = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array $setClauses |
39
|
|
|
*/ |
40
|
|
|
private $setClauses = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array $groupByClauses |
44
|
|
|
*/ |
45
|
|
|
private $groupByClauses = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array $havingClauses |
49
|
|
|
*/ |
50
|
|
|
private $havingClauses = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string $limitClause |
54
|
|
|
*/ |
55
|
|
|
private $limitClause; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array $queryHints |
59
|
|
|
*/ |
60
|
|
|
private $queryHints = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
24 |
|
public function guessQueryType() |
66
|
|
|
{ |
67
|
24 |
|
if ($this->selectClauses) { |
|
|
|
|
68
|
22 |
|
return static::QUERY_SELECT; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
return static::QUERY_UPDATE; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $clause |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
22 |
|
public function addSelect($clause) |
80
|
|
|
{ |
81
|
22 |
|
$this->selectClauses[] = $clause; |
82
|
|
|
|
83
|
22 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $clause |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
6 |
|
public function andWhere($clause) |
92
|
|
|
{ |
93
|
6 |
|
$this->whereClauses[] = $clause; |
94
|
|
|
|
95
|
6 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $clause |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
1 |
|
public function andHaving($clause) |
104
|
|
|
{ |
105
|
1 |
|
$this->havingClauses[] = $clause; |
106
|
|
|
|
107
|
1 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $clause |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
1 |
|
public function addOrderBy($clause) |
116
|
|
|
{ |
117
|
1 |
|
$this->orderByClauses[] = $clause; |
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $clause |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
2 |
|
public function addSet($clause) |
128
|
|
|
{ |
129
|
2 |
|
$this->setClauses[] = $clause; |
130
|
|
|
|
131
|
2 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $clause |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
3 |
|
public function addGroupBy($clause) |
140
|
|
|
{ |
141
|
3 |
|
$this->groupByClauses[] = $clause; |
142
|
|
|
|
143
|
3 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $clause |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
1 |
|
public function setLimit($clause) |
152
|
|
|
{ |
153
|
1 |
|
$this->limitClause = $clause; |
154
|
|
|
|
155
|
1 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $hint |
160
|
|
|
* @param mixed $value |
161
|
|
|
* |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
1 |
|
public function addQueryHint($hint, $value = null) |
165
|
|
|
{ |
166
|
1 |
|
$this->queryHints[$hint] = $value; |
167
|
|
|
|
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
22 |
|
public function getSelectClauses() |
175
|
|
|
{ |
176
|
22 |
|
return $this->selectClauses; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
22 |
|
public function getWhereClauses() |
183
|
|
|
{ |
184
|
22 |
|
return $this->whereClauses; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
22 |
|
public function getOrderByClauses() |
191
|
|
|
{ |
192
|
22 |
|
return $this->orderByClauses; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
2 |
|
public function getSetClauses() |
199
|
|
|
{ |
200
|
2 |
|
return $this->setClauses; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
22 |
|
public function getGroupByClauses() |
207
|
|
|
{ |
208
|
22 |
|
return $this->groupByClauses; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return array |
213
|
|
|
*/ |
214
|
22 |
|
public function getHavingClauses() |
215
|
|
|
{ |
216
|
22 |
|
return $this->havingClauses; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
22 |
|
public function getLimitClause() |
223
|
|
|
{ |
224
|
22 |
|
return $this->limitClause; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
2 |
|
public function getQueryHints() |
231
|
|
|
{ |
232
|
2 |
|
return $this->queryHints; |
233
|
|
|
} |
234
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.