1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElePHPant; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class LightQueryBuilder |
7
|
|
|
* @package ElePHPant |
8
|
|
|
*/ |
9
|
|
|
class LightQueryBuilder |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* INNER JOIN |
13
|
|
|
*/ |
14
|
|
|
public const INNER_JOIN = 'INNER'; |
15
|
|
|
/** |
16
|
|
|
* LEFT JOIN |
17
|
|
|
*/ |
18
|
|
|
public const LEFT_JOIN = 'LEFT'; |
19
|
|
|
/** |
20
|
|
|
* RIGHT JOIN |
21
|
|
|
*/ |
22
|
|
|
public const RIGHT_JOIN = 'RIGHT'; |
23
|
|
|
/** |
24
|
|
|
* FULL JOIN |
25
|
|
|
*/ |
26
|
|
|
public const FULL_JOIN = 'FULL'; |
27
|
|
|
/** |
28
|
|
|
* CROSS JOIN |
29
|
|
|
*/ |
30
|
|
|
public const CROSS_JOIN = 'CROSS'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var |
34
|
|
|
*/ |
35
|
|
|
private $table; |
36
|
|
|
/** |
37
|
|
|
* @var |
38
|
|
|
*/ |
39
|
|
|
private $class; |
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $query = ''; |
44
|
|
|
/** |
45
|
|
|
* @var |
46
|
|
|
*/ |
47
|
|
|
protected $params; |
48
|
|
|
/** |
49
|
|
|
* @var |
50
|
|
|
*/ |
51
|
|
|
protected $terms; |
52
|
|
|
/** |
53
|
|
|
* @var |
54
|
|
|
*/ |
55
|
|
|
protected $columns; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var |
59
|
|
|
*/ |
60
|
|
|
protected $order; |
61
|
|
|
/** |
62
|
|
|
* @var |
63
|
|
|
*/ |
64
|
|
|
protected $limit; |
65
|
|
|
/** |
66
|
|
|
* @var |
67
|
|
|
*/ |
68
|
|
|
protected $offset; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var |
72
|
|
|
*/ |
73
|
|
|
public $fail; |
74
|
|
|
|
75
|
|
|
private array $config; |
76
|
|
|
|
77
|
|
|
public function __construct(array $config) |
78
|
|
|
{ |
79
|
|
|
$this->config = $config; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function bootstrap(array $config, string $table) |
83
|
|
|
{ |
84
|
|
|
return (new self($config))->setTable($table); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $table |
89
|
|
|
*/ |
90
|
|
|
public function setTable(string $table):self |
91
|
|
|
{ |
92
|
|
|
$this->table = $table; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $class |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setFetchClass(string $class) |
101
|
|
|
{ |
102
|
|
|
$this->class = $class; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string|null |
108
|
|
|
*/ |
109
|
|
|
public function getQuery(): ?string |
110
|
|
|
{ |
111
|
|
|
return $this->query; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
|
|
public function getFail() |
118
|
|
|
{ |
119
|
|
|
return $this->fail; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $columns |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function select($columns = '*') |
127
|
|
|
{ |
128
|
|
|
$this->columns = $columns; |
129
|
|
|
return $this->toQuery("SELECT {$this->columns} FROM " . $this->table); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $terms |
135
|
|
|
* @param string|null $param |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function where(string $terms, ?string $param = null): self |
139
|
|
|
{ |
140
|
|
|
if ($param) { |
141
|
|
|
$this->params = $param; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->terms = $terms; |
145
|
|
|
return $this->toQuery(" WHERE {$this->terms}"); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $columns |
150
|
|
|
* @param string $search |
151
|
|
|
* @param bool $all |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
|
public function match(string $columns, string $search, bool $all = true) |
155
|
|
|
{ |
156
|
|
|
$this->params = "s={$search}"; |
157
|
|
|
return $this |
158
|
|
|
->select("MATCH({$columns}) AGAINST(:s)") |
159
|
|
|
->get($all); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function and(string $query) |
166
|
|
|
{ |
167
|
|
|
return $this->toQuery('AND ' . $query); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
public function or(string $query) |
174
|
|
|
{ |
175
|
|
|
return $this->toQuery('OR ' . $query); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $columns |
180
|
|
|
* @param string $table |
181
|
|
|
* @param $condition |
182
|
|
|
* @param string $type |
183
|
|
|
* @return LightQueryBuilder |
184
|
|
|
*/ |
185
|
|
|
public function join(string $columns, string $table, $condition, $type = self::INNER_JOIN) |
186
|
|
|
{ |
187
|
|
|
return $this->select($columns) |
188
|
|
|
->toQuery("{$type} JOIN {$table} ON {$condition}"); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $firstValue |
193
|
|
|
* @param string $secondValue |
194
|
|
|
* @return LightQueryBuilder |
195
|
|
|
*/ |
196
|
|
|
public function between(string $firstValue, string $secondValue) |
197
|
|
|
{ |
198
|
|
|
return $this->toQuery(" BETWEEN {$firstValue}") |
199
|
|
|
->and($secondValue); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $partial |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
|
|
public function toQuery(string $partial): self |
207
|
|
|
{ |
208
|
|
|
$this->query = trim(preg_replace('[\s+]', ' ', $this->query . " {$partial}")); |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/*-----------------------------------------------------------------------------------*/ |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param bool $all |
216
|
|
|
* @return array|mixed|null |
217
|
|
|
*/ |
218
|
|
|
public function get(bool $all = false) |
219
|
|
|
{ |
220
|
|
|
$class = $this->class ?? \stdClass::class; |
221
|
|
|
$crud = new CRUD(); |
222
|
|
|
$crud->setQuery($this->query); |
223
|
|
|
|
224
|
|
|
if ($this->params) { |
225
|
|
|
$crud->setParams($this->params); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $crud->read($class, $all); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param array $data |
233
|
|
|
* @param string $terms |
234
|
|
|
* @param $params |
235
|
|
|
* @return int|null |
236
|
|
|
*/ |
237
|
|
|
public function update(array $data, string $terms, $params) |
238
|
|
|
{ |
239
|
|
|
$this->terms = $terms; |
240
|
|
|
$this->params = $params; |
241
|
|
|
return $this->crud()->update($data, $this->terms); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param array $data |
246
|
|
|
* @return int|null |
247
|
|
|
*/ |
248
|
|
|
public function create(array $data) |
249
|
|
|
{ |
250
|
|
|
return $this->crud()->create($data); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param string $terms |
255
|
|
|
* @param string $param |
256
|
|
|
* @return bool |
257
|
|
|
*/ |
258
|
|
|
public function delete(string $terms, string $param): bool |
259
|
|
|
{ |
260
|
|
|
$this->params = $param; |
261
|
|
|
$this->terms = $terms; |
262
|
|
|
|
263
|
|
|
return $this->crud()->delete($this->terms, $this->params); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/*-----------------------------------------------------------------------------------*/ |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string $key |
272
|
|
|
* @return int |
273
|
|
|
*/ |
274
|
|
|
public function count($key = 'id'): int |
275
|
|
|
{ |
276
|
|
|
$crud = $this->crud(); |
277
|
|
|
|
278
|
|
|
if ($this->params) { |
279
|
|
|
$crud->setParams($this->params); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if ($this->query) { |
283
|
|
|
$crud->setQuery($this->query); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $crud->count($key); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param string $columnOrder |
291
|
|
|
* @return $this |
292
|
|
|
*/ |
293
|
|
|
public function order(string $columnOrder) |
294
|
|
|
{ |
295
|
|
|
$this->order = $columnOrder; |
296
|
|
|
$this->toQuery(" ORDER BY {$columnOrder}"); |
297
|
|
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param int $limit |
302
|
|
|
* @return $this |
303
|
|
|
*/ |
304
|
|
|
public function limit(int $limit) |
305
|
|
|
{ |
306
|
|
|
$this->limit = $limit; |
307
|
|
|
$this->toQuery(" LIMIT {$this->limit}"); |
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param int $offset |
313
|
|
|
* @return $this |
314
|
|
|
*/ |
315
|
|
|
public function offset(int $offset) |
316
|
|
|
{ |
317
|
|
|
$this->offset = $offset; |
318
|
|
|
$this->toQuery(" OFFSET {$offset}"); |
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/*-----------------------------------------------------------------------------------*/ |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return CRUD |
326
|
|
|
*/ |
327
|
|
|
private function crud() |
328
|
|
|
{ |
329
|
|
|
$crud = CRUD::bootstrap($this->config) |
|
|
|
|
330
|
|
|
->setTable($this->table) |
331
|
|
|
->setQuery($this->query); |
332
|
|
|
|
333
|
|
|
if ($this->params) { |
334
|
|
|
$crud->setParams($this->params); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $crud; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
protected function raw(string $query) |
341
|
|
|
{ |
342
|
|
|
return CRUD::bootstrap($this->config)->setTable($this->table)->setQuery($query); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.