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
|
|
|
public function buildWhereQuery($conditions = array(), $query = array()) |
25
|
|
|
{ |
26
|
|
|
$whereQuery = array(); |
27
|
|
|
if (!count($conditions)) { |
28
|
|
|
return $whereQuery; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$firstTime = true; |
32
|
|
|
$whereQuery[] = 'WHERE'; |
33
|
|
|
$this->whereAdded = true; |
34
|
|
|
|
35
|
|
|
foreach ($conditions as $where) { |
36
|
|
|
$sign = '='; |
37
|
|
|
if(count($where)==3) { |
38
|
|
|
$sign = $where[1]; |
39
|
|
|
} |
40
|
|
|
if ($firstTime) { |
41
|
|
|
$whereQuery[] = $this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
42
|
|
|
$firstTime = false; |
43
|
|
|
} else { |
44
|
|
|
$whereQuery[] = 'AND '.$this->qb->quote(trim($where[0])).' '.$sign.' '.$this->qb->enclose(end($where)); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (count($whereQuery)) { |
49
|
|
|
$query = array_merge($query, $whereQuery); |
50
|
|
|
} |
51
|
|
|
return $query; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function buildWhereInClauseQuery($terms = array()) |
55
|
|
|
{ |
56
|
|
|
if (is_int($terms[0])) { |
57
|
|
|
$dataStr = join(',', $terms); |
58
|
|
|
} elseif (is_string($terms[0])) { |
59
|
|
|
$dataStr = join("', '", $terms); |
60
|
|
|
$dataStr = "'".$dataStr."'"; |
61
|
|
|
} else { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $dataStr; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function buildWhereRawQuery($conditions = array(), $query = array()) |
69
|
|
|
{ |
70
|
|
|
$whereRawQuery = array(); |
71
|
|
|
|
72
|
|
|
if (!count($conditions)) { |
73
|
|
|
return $whereRawQuery; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$firstTime = false; |
77
|
|
|
if ($this->whereAdded === false) { |
78
|
|
|
$whereRawQuery[] = 'WHERE'; |
79
|
|
|
$firstTime = true; |
80
|
|
|
$this->whereAdded = true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($conditions as $whereRaw) { |
84
|
|
|
if ($firstTime) { |
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(), $query = array()) |
99
|
|
|
{ |
100
|
|
|
$whereInQuery = array(); |
101
|
|
|
|
102
|
|
|
if (!count($conditions)) { |
103
|
|
|
return $whereInQuery; |
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
|
|
|
if (count($whereInQuery)) { |
127
|
|
|
$query = array_merge($query, $whereInQuery); |
128
|
|
|
} |
129
|
|
|
return $query; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function buildWhereNotInQuery($conditions = array(), $query = array()) |
133
|
|
|
{ |
134
|
|
|
$whereNotInQuery = array(); |
135
|
|
|
|
136
|
|
|
if (!count($conditions)) { |
137
|
|
|
return $whereNotInQuery; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$firstTime = false; |
141
|
|
|
if ($this->whereAdded === false) { |
142
|
|
|
$whereNotInQuery[] = 'WHERE'; |
143
|
|
|
$firstTime = true; |
144
|
|
|
$this->whereAdded = true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
foreach ($conditions as $whereNotIn) { |
148
|
|
|
$dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]); |
149
|
|
|
if ($dataStr === null) { |
150
|
|
|
continue; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($firstTime) { |
154
|
|
|
$whereNotInQuery[] = trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
155
|
|
|
$firstTime = false; |
156
|
|
|
} else { |
157
|
|
|
$whereNotInQuery[] = 'AND '.trim($whereNotIn[0]).' NOT IN ('.$dataStr.')'; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
if (count($whereNotInQuery)) { |
161
|
|
|
$query = array_merge($query, $whereNotInQuery); |
162
|
|
|
} |
163
|
|
|
return $query; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function buildWhereNullQuery($conditions = array(), $query = array()) |
167
|
|
|
{ |
168
|
|
|
$whereNullQuery = array(); |
169
|
|
|
|
170
|
|
|
if (!count($conditions)) { |
171
|
|
|
return $whereNullQuery; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$firstTime = false; |
175
|
|
|
if ($this->whereAdded === false) { |
176
|
|
|
$whereNullQuery[] = 'WHERE'; |
177
|
|
|
$firstTime = true; |
178
|
|
|
$this->whereAdded = true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
foreach ($conditions as $whereNull) { |
182
|
|
|
if ($firstTime) { |
183
|
|
|
$whereNullQuery[] = trim($whereNull).' IS NULL'; |
184
|
|
|
$firstTime = false; |
185
|
|
|
} else { |
186
|
|
|
$whereNullQuery[] = 'AND '.trim($whereNull).' IS NULL'; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
if (count($whereNullQuery)) { |
190
|
|
|
$query = array_merge($query, $whereNullQuery); |
191
|
|
|
} |
192
|
|
|
return $query; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function buildWhereNotNullQuery($conditions = array(), $query = array()) |
196
|
|
|
{ |
197
|
|
|
$whereNotNullQuery = array(); |
198
|
|
|
|
199
|
|
|
if (!count($conditions)) { |
200
|
|
|
return $whereNotNullQuery; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$firstTime = false; |
204
|
|
|
if ($this->whereAdded === false) { |
205
|
|
|
$whereNotNullQuery[] = 'WHERE'; |
206
|
|
|
$firstTime = true; |
207
|
|
|
$this->whereAdded = true; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
foreach ($conditions as $whereNotNull) { |
211
|
|
|
if ($firstTime) { |
212
|
|
|
$whereNotNullQuery[] = trim($whereNotNull).' IS NOT NULL'; |
213
|
|
|
$firstTime = false; |
214
|
|
|
} else { |
215
|
|
|
$whereNotNullQuery[] = 'AND '.trim($whereNotNull).' IS NOT NULL'; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
if (count($whereNotNullQuery)) { |
219
|
|
|
$query = array_merge($query, $whereNotNullQuery); |
220
|
|
|
} |
221
|
|
|
return $query; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function buildAllWhereQuery($where, $whereRaw, $whereIn, $whereNotIn, $whereNull, $whereNotNull) |
225
|
|
|
{ |
226
|
|
|
$query = array(); |
227
|
|
|
$query = $this->buildWhereQuery($where, $query); |
228
|
|
|
$query = $this->buildWhereRawQuery($whereRaw, $query); |
229
|
|
|
$query = $this->buildWhereInQuery($whereIn, $query); |
230
|
|
|
$query = $this->buildWhereNotInQuery($whereNotIn, $query); |
231
|
|
|
$query = $this->buildWhereNullQuery($whereNull, $query); |
232
|
|
|
$query = $this->buildWhereNotNullQuery($whereNotNull, $query); |
233
|
|
|
|
234
|
|
|
return $query; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|