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