1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Database\Query; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Database\Query\Grammar\Grammar; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Query |
8
|
|
|
* @property array where |
9
|
|
|
* @property array columns |
10
|
|
|
* @property mixed query |
11
|
|
|
* @property null params |
12
|
|
|
* @property array values |
13
|
|
|
* @property array sort |
14
|
|
|
* @package JayaCode\Framework\Core\Database\Query |
15
|
|
|
*/ |
16
|
|
|
class Query |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
const TYPE_QUERY = 'QUERY'; |
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
const TYPE_SELECT = 'SELECT'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
const TYPE_INSERT = 'INSERT'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
const TYPE_UPDATE = 'UPDATE'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
const TYPE_DELETE = 'DELETE'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
public $table; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $attributes = array( |
51
|
|
|
'where' => array(), |
52
|
|
|
'columns' => array() |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $type = "SELECT"; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param null $table |
62
|
|
|
*/ |
63
|
|
|
public function __construct($table = null) |
64
|
|
|
{ |
65
|
|
|
$this->table = $table; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $queryStr |
70
|
|
|
* @param $params |
71
|
|
|
* @return Query |
72
|
|
|
*/ |
73
|
|
|
public static function sql($queryStr, $params = null) |
74
|
|
|
{ |
75
|
|
|
$query = new self(); |
76
|
|
|
if ($params) { |
77
|
|
|
$query->params = $params; |
78
|
|
|
} |
79
|
|
|
$query->setType(self::TYPE_QUERY); |
80
|
|
|
$query->query = $queryStr; |
81
|
|
|
return $query; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $table |
86
|
|
|
* @return Query |
87
|
|
|
*/ |
88
|
|
|
public static function table($table) |
89
|
|
|
{ |
90
|
|
|
return new self($table); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $table |
95
|
|
|
*/ |
96
|
|
|
public function setTable($table) |
97
|
|
|
{ |
98
|
|
|
$this->table = $table; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $columns |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function select($columns = null) |
106
|
|
|
{ |
107
|
|
|
$this->attributes['columns'] = $columns; |
108
|
|
|
$this->type = Query::TYPE_SELECT; |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $columnsVal |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function insert(array $columnsVal) |
117
|
|
|
{ |
118
|
|
|
$this->type = Query::TYPE_INSERT; |
119
|
|
|
$this->initColVal($columnsVal); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $columnsVal |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function update($columnsVal) |
130
|
|
|
{ |
131
|
|
|
$this->type = Query::TYPE_UPDATE; |
132
|
|
|
$this->initColVal($columnsVal); |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function delete() |
141
|
|
|
{ |
142
|
|
|
$this->type = Query::TYPE_DELETE; |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param $query |
148
|
|
|
* @param string $type |
149
|
|
|
* @return Query |
150
|
|
|
*/ |
151
|
|
|
public function whereQ(Query $query, $type = "AND") |
152
|
|
|
{ |
153
|
|
|
if ($query->getType() == Query::TYPE_QUERY) { |
154
|
|
|
$this->attributes['where'][] = array($type, $query->query); |
155
|
|
|
} |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $column |
161
|
|
|
* @param $value |
162
|
|
|
* @param string $operator |
163
|
|
|
* @param string $type |
164
|
|
|
* @return Query |
165
|
|
|
*/ |
166
|
|
|
public function where($column, $value, $operator = "=", $type = "AND") |
167
|
|
|
{ |
168
|
|
|
$this->attributes['where'][] = array($type, array($column, $operator, $value)); |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param $column |
174
|
|
|
* @param $value |
175
|
|
|
* @param string $operator |
176
|
|
|
* @return Query |
177
|
|
|
*/ |
178
|
|
|
public function andWhere($column, $value, $operator = "=") |
179
|
|
|
{ |
180
|
|
|
return $this->where($column, $value, $operator, "AND"); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param $column |
185
|
|
|
* @param $value |
186
|
|
|
* @param string $operator |
187
|
|
|
* @return Query |
188
|
|
|
*/ |
189
|
|
|
public function orWhere($column, $value, $operator = "=") |
190
|
|
|
{ |
191
|
|
|
return $this->where($column, $value, $operator, "OR"); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param $column |
196
|
|
|
* @param $value |
197
|
|
|
* @param string $type |
198
|
|
|
* @return Query |
199
|
|
|
*/ |
200
|
|
|
public function like($column, $value, $type = "AND") |
201
|
|
|
{ |
202
|
|
|
return $this->where($column, $value, "LIKE", $type); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param $column |
207
|
|
|
* @param $value |
208
|
|
|
* @return Query |
209
|
|
|
*/ |
210
|
|
|
public function andLike($column, $value) |
211
|
|
|
{ |
212
|
|
|
return $this->like($column, $value); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param $column |
217
|
|
|
* @param $value |
218
|
|
|
* @return Query |
219
|
|
|
*/ |
220
|
|
|
public function orLike($column, $value) |
221
|
|
|
{ |
222
|
|
|
return $this->like($column, $value, "OR"); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param $column |
227
|
|
|
* @param array $value |
228
|
|
|
* @param string $type |
229
|
|
|
* @return Query |
230
|
|
|
*/ |
231
|
|
|
public function between($column, $value = array(), $type = "AND") |
232
|
|
|
{ |
233
|
|
|
if (count($value) != 2) { |
234
|
|
|
throw new \OutOfBoundsException(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this->where($column, $value, "BETWEEN", $type); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $column |
242
|
|
|
* @param array $value |
243
|
|
|
* @return Query |
244
|
|
|
*/ |
245
|
|
|
public function andBetween($column, $value = array()) |
246
|
|
|
{ |
247
|
|
|
return $this->between($column, $value); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param $column |
252
|
|
|
* @param $value |
253
|
|
|
* @return Query |
254
|
|
|
*/ |
255
|
|
|
public function orBetween($column, $value) |
256
|
|
|
{ |
257
|
|
|
return $this->between($column, $value, "OR"); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param $column |
262
|
|
|
* @param $order |
263
|
|
|
* @return $this |
264
|
|
|
*/ |
265
|
|
|
public function sort($column, $order) |
266
|
|
|
{ |
267
|
|
|
$this->attributes['sort'] = array( |
268
|
|
|
'column' => $column, |
269
|
|
|
'order' => $order |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param $column |
277
|
|
|
* @return Query |
278
|
|
|
*/ |
279
|
|
|
public function asc($column) |
280
|
|
|
{ |
281
|
|
|
return $this->sort($column, "ASC"); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param $column |
286
|
|
|
* @return Query |
287
|
|
|
*/ |
288
|
|
|
public function desc($column) |
289
|
|
|
{ |
290
|
|
|
return $this->sort($column, "DESC"); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param Grammar $grammar |
295
|
|
|
* @return array |
296
|
|
|
*/ |
297
|
|
|
public function build(Grammar $grammar) |
298
|
|
|
{ |
299
|
|
|
$grammar->setQuery($this); |
300
|
|
|
|
301
|
|
|
$queryStr = $grammar->build(); |
302
|
|
|
|
303
|
|
|
$queryParams = isset($this->attributes['params']) && !empty($this->attributes['params'])? |
304
|
|
|
$this->attributes['params'] |
305
|
|
|
:$grammar->getParams(); |
306
|
|
|
|
307
|
|
|
return [$queryStr, $queryParams]; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Clear Query Builder |
312
|
|
|
*/ |
313
|
|
|
public function clear() |
314
|
|
|
{ |
315
|
|
|
$this->attributes = array(); |
316
|
|
|
$this->params = array(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param $name |
321
|
|
|
* @return array |
322
|
|
|
*/ |
323
|
|
|
public function __get($name) |
324
|
|
|
{ |
325
|
|
|
return arr_get($this->attributes, $name); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param $name |
330
|
|
|
* @param $value |
331
|
|
|
*/ |
332
|
|
|
public function __set($name, $value) |
333
|
|
|
{ |
334
|
|
|
$this->attributes[$name] = $value; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @return string |
339
|
|
|
*/ |
340
|
|
|
public function getType() |
341
|
|
|
{ |
342
|
|
|
return $this->type; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @param string $type |
347
|
|
|
*/ |
348
|
|
|
public function setType($type) |
349
|
|
|
{ |
350
|
|
|
$this->type = $type; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* init col and val to attributes |
355
|
|
|
* @param $columnsVal |
356
|
|
|
*/ |
357
|
|
|
public function initColVal($columnsVal) |
358
|
|
|
{ |
359
|
|
|
$this->attributes['columns'] = array_keys($columnsVal); |
360
|
|
|
$this->attributes['values'] = array_values($columnsVal); |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
|