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