1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Query builder API. |
4
|
|
|
* |
5
|
|
|
* @author RN Kushwaha <[email protected]> |
6
|
|
|
* @since v0.0.1 <Date: 16th April, 2019> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Dolphin\Builders; |
10
|
|
|
|
11
|
|
|
use Dolphin\Parsers\WhereQueryParser; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This class provides the mechanism to build the Where Queries. |
15
|
|
|
*/ |
16
|
|
|
class WhereQueryBuilder extends QueryBuilder |
17
|
|
|
{ |
18
|
|
|
private $qb; |
19
|
|
|
|
20
|
|
|
public function __construct(){ |
21
|
|
|
$this->qb = new QueryBuilder(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function buildWhereInClauseQuery($terms = array()) |
25
|
|
|
{ |
26
|
|
|
if (is_int($terms[0])) { |
27
|
|
|
$dataStr = join(',', $terms); |
28
|
|
|
} elseif (is_string($terms[0])) { |
29
|
|
|
$dataStr = join("', '", $terms); |
30
|
|
|
$dataStr = "'".$dataStr."'"; |
31
|
|
|
} else { |
32
|
|
|
return null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $dataStr; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function buildWhereQuery($conditions = array()) |
39
|
|
|
{ |
40
|
|
|
$whereQuery = array(); |
41
|
|
|
if (count($conditions)<=0) { |
42
|
|
|
return array(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$firstTime = true; |
46
|
|
|
if ($this->whereAdded === false && count($conditions)) { |
47
|
|
|
$whereQuery[] = 'WHERE'; |
48
|
|
|
$this->whereAdded = true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($conditions as $where) { |
52
|
|
|
$sign = '='; |
53
|
|
|
if(count($where)==3) { |
54
|
|
|
$sign = $where[1]; |
55
|
|
|
} |
56
|
|
|
if ($firstTime) { |
57
|
|
|
$whereQuery[] = $this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
58
|
|
|
$firstTime = false; |
59
|
|
|
} else { |
60
|
|
|
$whereQuery[] = 'AND '.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (count($whereQuery)) { |
65
|
|
|
return $whereQuery; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function buildWhereRawQuery($conditions = array(), $query = array()) |
70
|
|
|
{ |
71
|
|
|
$whereRawQuery = array(); |
72
|
|
|
|
73
|
|
|
if (count($conditions)<=0) { |
74
|
|
|
return $query; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$firstTime = true; |
78
|
|
|
if ($this->whereAdded === false && count($conditions)) { |
79
|
|
|
$whereRawQuery[] = 'WHERE'; |
80
|
|
|
$this->whereAdded = true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($conditions as $whereRaw) { |
84
|
|
|
if ($firstTime === true) { |
85
|
|
|
$whereRawQuery[] = $whereRaw; |
86
|
|
|
$firstTime = false; |
87
|
|
|
} else { |
88
|
|
|
$whereRawQuery[] = 'AND '.$whereRaw; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (count($whereRawQuery)) { |
93
|
|
|
$query = array_merge($query, $whereRawQuery); |
94
|
|
|
} |
95
|
|
|
return $query; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function buildWhereInQuery($conditions = array()) |
99
|
|
|
{ |
100
|
|
|
$whereInQuery = array(); |
101
|
|
|
|
102
|
|
|
if (count($conditions)<=0) { |
103
|
|
|
return array(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$firstTime = false; |
107
|
|
|
if ($this->whereAdded === false) { |
108
|
|
|
$whereInQuery[] = 'WHERE'; |
109
|
|
|
$firstTime = true; |
110
|
|
|
$this->whereAdded = true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach ($conditions as $whereIn) { |
114
|
|
|
$dataStr = $this->buildWhereInClauseQuery($whereIn[1]); |
115
|
|
|
if ($dataStr === null) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($firstTime) { |
120
|
|
|
$whereInQuery[] = trim($whereIn[0]).' IN ('.$dataStr.')'; |
121
|
|
|
$firstTime = false; |
122
|
|
|
} else { |
123
|
|
|
$whereInQuery[] = 'AND '.trim($whereIn[0]).' IN ('.$dataStr.')'; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $whereInQuery; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function buildWhereNotInQuery($conditions = array()) |
131
|
|
|
{ |
132
|
|
|
$whereNotInQuery = array(); |
133
|
|
|
|
134
|
|
|
if (count($conditions)<=0) { |
135
|
|
|
return array(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$firstTime = false; |
139
|
|
|
if ($this->whereAdded === false) { |
140
|
|
|
$whereNotInQuery[] = 'WHERE'; |
141
|
|
|
$firstTime = true; |
142
|
|
|
$this->whereAdded = true; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
foreach ($conditions as $whereNotIn) { |
146
|
|
|
$dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]); |
147
|
|
|
if ($dataStr === null) { |
148
|
|
|
continue; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($firstTime) { |
152
|
|
|
$whereNotInQuery[] = trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
153
|
|
|
$firstTime = false; |
154
|
|
|
} else { |
155
|
|
|
$whereNotInQuery[] = 'AND '.trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $whereNotInQuery; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function buildWhereNullQuery($conditions = array(), $query = array()) |
163
|
|
|
{ |
164
|
|
|
$whereNullQuery = array(); |
165
|
|
|
|
166
|
|
|
if (count($conditions)<=0) { |
167
|
|
|
return $query; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$firstTime = false; |
171
|
|
|
if ($this->whereAdded === false) { |
172
|
|
|
$whereNullQuery[] = 'WHERE'; |
173
|
|
|
$firstTime = true; |
174
|
|
|
$this->whereAdded = true; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
foreach ($conditions as $whereNull) { |
178
|
|
|
if ($firstTime) { |
179
|
|
|
$whereNullQuery[] = trim($whereNull).' IS NULL'; |
180
|
|
|
$firstTime = false; |
181
|
|
|
} else { |
182
|
|
|
$whereNullQuery[] = 'AND '.trim($whereNull).' IS NULL'; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
if (count($whereNullQuery)) { |
186
|
|
|
$query = array_merge($query, $whereNullQuery); |
187
|
|
|
} |
188
|
|
|
return $query; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function buildWhereNotNullQuery($conditions = array(), $query = array()) |
192
|
|
|
{ |
193
|
|
|
$whereNotNullQuery = array(); |
194
|
|
|
|
195
|
|
|
if (count($conditions)<=0) { |
196
|
|
|
return $query; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$firstTime = false; |
200
|
|
|
if ($this->whereAdded === false) { |
201
|
|
|
$whereNotNullQuery[] = 'WHERE'; |
202
|
|
|
$firstTime = true; |
203
|
|
|
$this->whereAdded = true; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
foreach ($conditions as $whereNotNull) { |
207
|
|
|
if ($firstTime) { |
208
|
|
|
$whereNotNullQuery[] = trim($whereNotNull).' IS NOT NULL'; |
209
|
|
|
$firstTime = false; |
210
|
|
|
} else { |
211
|
|
|
$whereNotNullQuery[] = 'AND '.trim($whereNotNull).' IS NOT NULL'; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
if (count($whereNotNullQuery)) { |
215
|
|
|
$query = array_merge($query, $whereNotNullQuery); |
216
|
|
|
} |
217
|
|
|
return $query; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull, $mainQuery = array()) |
221
|
|
|
{ |
222
|
|
|
$query = array(); |
|
|
|
|
223
|
|
|
$query = $this->buildWhereQuery($where); |
224
|
|
|
$query = $this->buildWhereRawQuery($whereRaw, $query); |
225
|
|
|
$whereInQuery = $this->buildWhereInQuery($whereIn); |
226
|
|
|
if (count($whereInQuery)) { |
227
|
|
|
$query = array_merge($query, $whereInQuery); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
$whereNotInQuery = $this->buildWhereNotInQuery($whereNotIn); |
230
|
|
|
if (count($whereNotInQuery)) { |
231
|
|
|
$query = array_merge($query, $whereNotInQuery); |
232
|
|
|
} |
233
|
|
|
$query = $this->buildWhereNullQuery($whereNull, $query); |
234
|
|
|
$query = $this->buildWhereNotNullQuery($whereNotNull, $query); |
235
|
|
|
|
236
|
|
|
if (count($query)) { |
237
|
|
|
$mainQuery = array_merge($mainQuery, $query); |
238
|
|
|
} |
239
|
|
|
return $mainQuery; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|