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