1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Database; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Database\Connector\Connector; |
5
|
|
|
use JayaCode\Framework\Core\Database\Connector\ConnectorMySql; |
6
|
|
|
use JayaCode\Framework\Core\Database\Query\Grammar\Grammar; |
7
|
|
|
use JayaCode\Framework\Core\Database\Query\Grammar\GrammarMySql; |
8
|
|
|
use JayaCode\Framework\Core\Database\Query\Query; |
9
|
|
|
use PDO; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Database |
13
|
|
|
* @package JayaCode\Framework\Core\Database |
14
|
|
|
*/ |
15
|
|
|
class Database |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Connector |
19
|
|
|
*/ |
20
|
|
|
protected $connector; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \PDO |
24
|
|
|
*/ |
25
|
|
|
protected $pdo; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \PDOStatement |
29
|
|
|
*/ |
30
|
|
|
protected $statement; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Query |
34
|
|
|
*/ |
35
|
|
|
protected $query; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Grammar |
39
|
|
|
*/ |
40
|
|
|
protected $grammar; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $driver = [ |
46
|
|
|
"mysql" => [ |
47
|
|
|
"connector" => ConnectorMySql::class, |
48
|
|
|
"grammar" => GrammarMySql::class, |
49
|
|
|
] |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $config = [ |
56
|
|
|
"driver" => "mysql", |
57
|
|
|
|
58
|
|
|
"host" => "", |
59
|
|
|
"username" => "", |
60
|
|
|
"password" => "", |
61
|
|
|
|
62
|
|
|
"dbname" => "", |
63
|
|
|
"options" => [ |
64
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
65
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC |
66
|
|
|
] |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $model = null; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $config |
76
|
|
|
*/ |
77
|
|
|
public function __construct($config) |
78
|
|
|
{ |
79
|
|
|
$options = isset($config["options"])? |
80
|
|
|
arr_merge_all($this->config["options"], $config["options"]) |
81
|
|
|
:$this->config["options"]; |
82
|
|
|
|
83
|
|
|
$this->config = arr_merge_all($this->config, $config); |
84
|
|
|
|
85
|
|
|
$this->config['options'] = $options; |
86
|
|
|
$this->initialize(); |
87
|
|
|
|
88
|
|
|
$this->createConnection(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $config |
93
|
|
|
* @return static |
94
|
|
|
*/ |
95
|
|
|
public static function create($config) |
96
|
|
|
{ |
97
|
|
|
return new static($config); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
*/ |
103
|
|
|
protected function initialize() |
104
|
|
|
{ |
105
|
|
|
$connectorClass = $this->driver[$this->config["driver"]]["connector"]; |
106
|
|
|
$this->connector = new $connectorClass(); |
107
|
|
|
|
108
|
|
|
$grammarClass = $this->driver[$this->config["driver"]]["grammar"]; |
109
|
|
|
$this->grammar = new $grammarClass(); |
110
|
|
|
|
111
|
|
|
$this->query = new Query(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return PDO |
116
|
|
|
*/ |
117
|
|
|
public function createConnection() |
118
|
|
|
{ |
119
|
|
|
$this->pdo = $this->connector->connect($this->config); |
120
|
|
|
return $this->pdo; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $query |
125
|
|
|
* @param null $params |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function sql($query, $params = null) |
129
|
|
|
{ |
130
|
|
|
$this->query = $this->query->sql($query, $params); |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $table |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function table($table) |
139
|
|
|
{ |
140
|
|
|
$this->query->setTable($table); |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param null $columns |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function select($columns = null) |
149
|
|
|
{ |
150
|
|
|
$this->query->select($columns); |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array $columnsVal |
156
|
|
|
* @param bool $runExecute |
157
|
|
|
* @return $this|mixed |
158
|
|
|
*/ |
159
|
|
|
public function insert(array $columnsVal, $runExecute = false) |
160
|
|
|
{ |
161
|
|
|
$this->query->insert($columnsVal); |
162
|
|
|
|
163
|
|
|
if ($runExecute) { |
164
|
|
|
$status = $this->execute(false); |
165
|
|
|
$this->clear(); |
166
|
|
|
|
167
|
|
|
return $status; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param array $columnsVal |
175
|
|
|
* @param null $primaryKey |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
|
|
public function update(array $columnsVal, $primaryKey = null) |
179
|
|
|
{ |
180
|
|
|
if ($primaryKey) { |
181
|
|
|
$keyVal = $columnsVal[$primaryKey]; |
182
|
|
|
$columnsVal = arr_exclude($columnsVal, [$primaryKey]); |
183
|
|
|
|
184
|
|
|
$this->query->update($columnsVal)->where($primaryKey, $keyVal); |
185
|
|
|
|
186
|
|
|
$status = $this->execute(false); |
187
|
|
|
$this->clear(); |
188
|
|
|
|
189
|
|
|
return $status; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->query->update($columnsVal); |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param null $primaryKey |
199
|
|
|
* @param null $primaryKeyVal |
200
|
|
|
* @return mixed |
201
|
|
|
*/ |
202
|
|
|
public function delete($primaryKey = null, $primaryKeyVal = null) |
203
|
|
|
{ |
204
|
|
|
if ($primaryKey && $primaryKeyVal) { |
205
|
|
|
$this->query->delete()->where($primaryKey, $primaryKeyVal); |
206
|
|
|
|
207
|
|
|
$status = $this->execute(false); |
208
|
|
|
$this->clear(); |
209
|
|
|
|
210
|
|
|
return $status; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$this->query->delete(); |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param $query |
219
|
|
|
* @param string $type |
220
|
|
|
* @return Query |
221
|
|
|
*/ |
222
|
|
|
public function whereQ($query, $type = "AND") |
223
|
|
|
{ |
224
|
|
|
$this->query->whereQ($query, $type); |
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param $column |
230
|
|
|
* @param $value |
231
|
|
|
* @param string $operator |
232
|
|
|
* @param string $type |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function where($column, $value, $operator = "=", $type = "AND") |
236
|
|
|
{ |
237
|
|
|
$this->query->where($column, $value, $operator, $type); |
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param $column |
243
|
|
|
* @param $value |
244
|
|
|
* @param string $operator |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
|
|
public function andWhere($column, $value, $operator = "=") |
248
|
|
|
{ |
249
|
|
|
$this->query->andWhere($column, $value, $operator); |
250
|
|
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param $column |
255
|
|
|
* @param $value |
256
|
|
|
* @param string $operator |
257
|
|
|
* @return $this |
258
|
|
|
*/ |
259
|
|
|
public function orWhere($column, $value, $operator = "=") |
260
|
|
|
{ |
261
|
|
|
$this->query->orWhere($column, $value, $operator); |
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param $column |
267
|
|
|
* @param $value |
268
|
|
|
* @param string $type |
269
|
|
|
* @return $this |
270
|
|
|
*/ |
271
|
|
|
public function like($column, $value, $type = "AND") |
272
|
|
|
{ |
273
|
|
|
$this->query->like($column, $value, $type); |
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param $column |
279
|
|
|
* @param $value |
280
|
|
|
* @return $this |
281
|
|
|
*/ |
282
|
|
|
public function andLike($column, $value) |
283
|
|
|
{ |
284
|
|
|
$this->query->andLike($column, $value); |
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param $column |
290
|
|
|
* @param $value |
291
|
|
|
* @return $this |
292
|
|
|
*/ |
293
|
|
|
public function orLike($column, $value) |
294
|
|
|
{ |
295
|
|
|
$this->query->orLike($column, $value); |
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param $column |
301
|
|
|
* @param $value |
302
|
|
|
* @param string $type |
303
|
|
|
* @return $this |
304
|
|
|
*/ |
305
|
|
|
public function between($column, $value, $type = "AND") |
306
|
|
|
{ |
307
|
|
|
$this->query->between($column, $value, $type); |
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param $column |
313
|
|
|
* @param $value |
314
|
|
|
* @return $this |
315
|
|
|
*/ |
316
|
|
|
public function andBetween($column, $value) |
317
|
|
|
{ |
318
|
|
|
$this->query->andBetween($column, $value); |
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param $column |
324
|
|
|
* @param $value |
325
|
|
|
* @return $this |
326
|
|
|
*/ |
327
|
|
|
public function orBetween($column, $value) |
328
|
|
|
{ |
329
|
|
|
$this->query->orBetween($column, $value); |
330
|
|
|
return $this; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param $column |
335
|
|
|
* @return $this |
336
|
|
|
*/ |
337
|
|
|
public function asc($column) |
338
|
|
|
{ |
339
|
|
|
$this->query->asc($column); |
340
|
|
|
return $this; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param $column |
345
|
|
|
* @return $this |
346
|
|
|
*/ |
347
|
|
|
public function desc($column) |
348
|
|
|
{ |
349
|
|
|
$this->query->desc($column); |
350
|
|
|
return $this; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param $num |
355
|
|
|
* @param null $offset |
356
|
|
|
* @return $this |
357
|
|
|
*/ |
358
|
|
|
public function limit($num, $offset = null) |
359
|
|
|
{ |
360
|
|
|
$this->query->limit($num, $offset); |
361
|
|
|
return $this; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param bool $returnThis |
366
|
|
|
* @return mixed |
367
|
|
|
*/ |
368
|
|
|
public function execute($returnThis = true) |
369
|
|
|
{ |
370
|
|
|
$qArr = $this->query->build($this->grammar); |
371
|
|
|
|
372
|
|
|
$this->statement = $this->pdo->prepare($qArr[0]); |
373
|
|
|
$status = $this->statement->execute($qArr[1]); |
374
|
|
|
|
375
|
|
|
return ($returnThis) ? $this : $status; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @return array |
380
|
|
|
* @throws \Exception |
381
|
|
|
*/ |
382
|
|
|
public function all() |
383
|
|
|
{ |
384
|
|
|
$this->executeIfNotAlreadyExecutedPreviously(); |
385
|
|
|
|
386
|
|
|
if ($this->model) { |
387
|
|
|
$dataModel = array(); |
388
|
|
|
while ($model = $this->get()) { |
389
|
|
|
$dataModel[] = $model; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
$this->clear(); |
393
|
|
|
|
394
|
|
|
return $dataModel; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
$data = $this->statement->fetchAll(); |
398
|
|
|
$this->clear(); |
399
|
|
|
|
400
|
|
|
return $data; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @return mixed |
405
|
|
|
*/ |
406
|
|
|
public function first() |
407
|
|
|
{ |
408
|
|
|
$data = $this->limit(1)->get(); |
409
|
|
|
$this->clear(); |
410
|
|
|
return $data; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* @return mixed |
415
|
|
|
* @throws \Exception |
416
|
|
|
*/ |
417
|
|
|
protected function get() |
418
|
|
|
{ |
419
|
|
|
$this->executeIfNotAlreadyExecutedPreviously(); |
420
|
|
|
|
421
|
|
|
if ($this->model) { |
422
|
|
|
$data = $this->statement->fetch(); |
423
|
|
|
return $data?new $this->model($data, false):$data; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
return $this->statement->fetch(); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* @throws \Exception |
431
|
|
|
*/ |
432
|
|
|
protected function executeIfNotAlreadyExecutedPreviously() |
433
|
|
|
{ |
434
|
|
|
if (!$this->statement) { |
435
|
|
|
$this->execute(); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Clear statement PDO and query builder |
441
|
|
|
*/ |
442
|
|
|
public function clear() |
443
|
|
|
{ |
444
|
|
|
$this->statement = null; |
445
|
|
|
$this->clearUsingModel(); |
446
|
|
|
|
447
|
|
|
$this->query->clear(); |
448
|
|
|
$this->grammar->clear(); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* |
453
|
|
|
*/ |
454
|
|
|
public function clearUsingModel() |
455
|
|
|
{ |
456
|
|
|
$this->model = null; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* @return string |
461
|
|
|
*/ |
462
|
|
|
public function getModel() |
463
|
|
|
{ |
464
|
|
|
return $this->model; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @param string $model |
469
|
|
|
* @param null $table |
470
|
|
|
* @throws \Exception |
471
|
|
|
*/ |
472
|
|
|
public function setModel($model, $table = null) |
473
|
|
|
{ |
474
|
|
|
if (!class_exists($model)) { |
475
|
|
|
throw new \Exception("class {$model} is not exist"); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
if ($table) { |
479
|
|
|
$this->table($table); |
480
|
|
|
} |
481
|
|
|
$this->model = $model; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @return string |
486
|
|
|
*/ |
487
|
|
|
public function lastInsertId() |
488
|
|
|
{ |
489
|
|
|
return $this->pdo->lastInsertId(); |
490
|
|
|
} |
491
|
|
|
} |
492
|
|
|
|