-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseTrait.php
501 lines (453 loc) · 15.8 KB
/
DatabaseTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
namespace Plugin\Model;
use Plugin\DB\DB;
use Plugin\Cache\Cache;
use App;
/**
* Трейт реализует доступ к SQL-базе данных через функцию обертку db() (Mysql)
*/
trait DatabaseTrait {
/**
* @property int $shard_id Текущий идентификатор шарда, использующийся для коннекта
* @property string $table статическая переменная с таблицей
* @property array $fields Все поля для данной таблицы
*/
protected static
$shard_id = 0,
$table = '',
$id_field = 'id',
$fields = [],
$incremental_id = false;
/**
* Получение строки для sql-строки с параметрами, поддерживает как
* ассоциативный, так и обычный индексный массив
*
* @access protected
* @param array $params список параметров с данными
* @param string $sep разделитель при объединении параметров
* @param bool $incremental
* @return string подготовленная строка для передачи в запрос
*/
protected static function dbGetSqlStringByParams(array $params, $sep = ',', $incremental = false) {
$data = []; // массив данных для объединения
foreach ($params as $param => $value) {
if (is_string($param)) {
$data[] = '`' . $param . '` = ' . ($incremental ? '`' . $param . '` + ' : '' ) . ' :' . $param;
} else {
$data[] = '`' . $value . '`';
}
}
return implode(' ' . $sep . ' ', $data);
}
public static function table() {
if (!static::$table) {
static::$table = strtolower(str_replace(chr(92), '_', get_class_name(static::class)));
// Инициализация таблицы
if (static::table()[0] !== '`')
static::$table = '`' . static::table() . '`';
}
return static::$table;
}
/**
* @param bool $with_value Include mysql default values or no
* @return array
*/
public static function fields($with_values = false) {
static $keys = [];
static $fields = [];
if (!$fields) {
$func = function () {
$incremental_id = false;
$fields = [];
if ($data = static::dbQuery('DESCRIBE ' . static::table())) {
$fields = array_column($data, 'Default', 'Field');
if ($data[0]['Field'] === static::$id_field && $data[0]['Extra'] === 'auto_increment') {
$incremental_id = true;
}
}
return [$fields, $incremental_id];
};
list($fields, static::$incremental_id) =
App::$debug ? $func() : Cache::get('db:scheme:' . static::table(), $func);
$keys = array_keys($fields);
}
return $with_values ? $fields : $keys;
}
protected function isIncrementalId() {
return static::$incremental_id;
}
/**
* Выполнение запросов к базе данных
*
* @access protected
* @param string $query
* @param array $params
* @return mixed
* @throws Exception
*/
protected static function dbQuery($query, array $params = []) {
return DB::query($query, static::adaptParams($params), static::dbShardId($params));
}
protected static function adaptParams(array $params) {
return array_combine(
array_map(function ($key) {
if (false !== stripos($key, ':')) {
return strtok($key, ':');
}
return $key;
}, array_keys($params)),
$params
);
}
/**
* Логика определния идентификатора шарда по передаваемым параметрам в запрос
*
* @param array $params
* @return int
*/
protected static function dbShardId(array $params = null) {
return 0;
}
/**
* Выполнение запроса вставки в базу данных
*
* @uses self::dbGetSqlStringByParams()
* @uses Database::execute()
*
* @access protected
* @param array $params список параметров для передачи в запрос
* @return bool
*/
protected function dbInsert(array $params) {
$q = 'INSERT INTO ' . static::table()
. ' SET ' . self::dbGetSqlStringByParams($params, ',');
return static::dbQuery($q, $params);
}
/**
* Get last inserted id in case of auto_increment
*/
protected function dbInsertId() {
$result = static::dbQuery('SELECT LAST_INSERT_ID() AS `' . static::$id_field . '`');
if ($result && isset($result[0])) {
return $result[0][static::$id_field];
}
return 0;
}
/**
* Формирование условия WHERE по передаваемым параметрам
*
* @param array &$conditions
* @return array
*/
protected function dbGetWhere(array &$conditions) {
$where = [];
if ($conditions) {
$params = $conditions;
$i = 0;
foreach ($params as $k => $c) {
// If we have typed field
$t = '';
if (false !== stripos($k, ':')) {
[$k, $t] = explode(':', $k);
}
if (is_array($c)) {
if ($c) {
// Собриаем параметры с идентификаторами
$id_params = [];
foreach ($c as $v) {
$id_params[] = sprintf('ID%d', ++$i);
}
$conditions = array_merge($conditions, array_combine($id_params, $c));
if ($t === 'range') {
$where[] = ' (`' . $k . '` BETWEEN :' . implode(' AND :', $id_params) . ') ';
} else {
$where[] = ' `' . $k . '` IN (:' . implode(', :', $id_params) . ') ';
}
} else {
$where[] = ' `'. $k . '` = NULL ';
}
unset($conditions[$k]);
} else {
// Bash like style
// See bash scripting
$field = $k . '_' . $t;
switch ($t) {
case 'gt':
$op = '>';
break;
case 'ge':
$op = '>=';
break;
case 'lt':
$op = '<';
break;
case 'le':
$op = '<=';
break;
case 'ne':
$op = '!=';
break;
default:
$op = '=';
$field = $k;
}
$where[] = ' `' . $k . '` ' . $op . ' :' . $field . ' ';
if ($field !== $k) {
$conditions[$k . '_' . $t] = $conditions[$k . ':' . $t];
unset($conditions[$k . ':' . $t]);
}
}
}
}
return $where;
}
/**
* Выполнение SELECT-запроса из базы данных
*
* @uses self::dbGetSqlStringByParams()
* @uses Database::query()
*
* @access protected
* @param array $fields список полей для выборки
* @param array $conditions список условий
* @param array $order условия сортировки
* @param int $offset офсет для лимитной выборки
* @param int $limit лимит на выборку, если нужне, 0 - если безлимитная выборка
* @return Database::query()
*/
protected function dbSelect(array $fields, array $conditions = null, array $order = null, $offset = null, $limit = null) {
// Если нужно формировать строку сортировки
$order_string = '';
if ($order) {
foreach ($order as $field => $sort) {
$order_string .= ', `' . $field . '` ' . strtoupper($sort);
}
$order_string = trim($order_string, ', ');
}
// Строка условия - special logic :)
$where = $conditions ? $this->dbGetWhere($conditions) : null;
/*
// Данные трейта TPagination, исключаем, чтобы избавиться от зависимости
if (!isset($offset) || !isset($limit)) {
$limit = $this->limit;
$offset = $this->offset;
}
*/
$q = 'SELECT ' . self::dbGetSqlStringByParams($fields)
. ' FROM ' . static::table()
. ($where ? ' WHERE ' . implode(' AND ', $where) : '')
. ($order_string ? ' ORDER BY ' . $order_string : '')
. ($limit ? ' LIMIT ' . (int) $offset . ', ' . (int) $limit : '');
return self::dbQuery($q, $conditions);
}
/**
* Получение количества элементов, согласно выборке с условием
*
* @access protected
* @param array $conditions
* @return int
*/
protected function dbCount(array $conditions = []) {
$where = $conditions ? $this->dbGetWhere($conditions) : null;
$q = 'SELECT COUNT(*) AS `count` FROM ' . static::table()
. ($where ? ' WHERE ' . implode('AND', $where) : '')
. ' LIMIT 1';
$rows = static::dbQuery($q, $conditions);
$count = 0;
if (isset($rows[0])) {
$count = (int) $rows[0]['count'];
}
return $count;
}
/**
* Получение одной строки (LIMIT 1)
*
* @see self::dbSelect
*/
protected function dbGet(array $fields, array $conditions = null, array $order = null) {
$rows = $this->dbSelect($fields, $conditions, $order, 'AND', 0, 1);
return isset($rows[0]) ? $rows[0] : [];
}
/**
* Выполнение UPDATE-запроса в базу данных
*
* @uses self::dbGetSqlStringByParams()
* @uses Database::execute()
*
* @access protected
* @param array $params список параметров для установки в запросе
* @param array $conditions список условий
* @return Database::execute()
*/
protected function dbUpdate(array $params, array $conditions, $incremental = false) {
$q = 'UPDATE ' . static::table()
. ' SET ' . self::dbGetSqlStringByParams($params, ',', $incremental)
. ' WHERE ' . self::dbGetSqlStringByParams($conditions, ' AND ');
return static::dbQuery($q, array_merge($params, $conditions));
}
/**
* @access protected
* @param array $params
* @param array $ids
* @param bool $incremental
* @return Database::execute()
*/
protected function dbUpdateByIds(array $params, array $ids, $incremental = false) {
// Собриаем параметры с идентификаторами
$id_params = [];
$i = 0;
foreach ($ids as $id) {
$id_params[] = sprintf('ID%d', ++$i);
}
$q = 'UPDATE ' . static::table()
. ' SET ' . self::dbGetSqlStringByParams($params, ',', $incremental)
. ' WHERE `' . static::$id_field . '` IN (:' . implode(', :', $id_params) . ')';
return static::dbQuery($q, array_merge($params, array_combine($id_params, $ids)));
}
/**
* Выполнение DELETE-запроса в базе данных
*
* @uses self::dbGetSqlStringByParams()
* @uses Database::execute()
*
* @param array $conditions список условий
* @return Database::execute()
*/
protected function dbDelete(array $conditions) {
$q = 'DELETE FROM ' . static::table()
. ' WHERE ' . self::dbGetSqlStringByParams($conditions, ' AND ');
return static::dbQuery($q, $conditions);
}
/**
* Удаление по праймери
*
* @param array $ids
* @return Database::execute()
*/
protected function dbDeleteByIds(array $ids) {
return $this->dbDeleteByRowValues(static::$id_field, $ids);
}
/**
* @param string $row
* @param array $values
* @return Database::execute()
*/
protected function dbDeleteByRowValues($row, array $values) {
$q = 'DELETE FROM ' . static::table()
. ' WHERE `' . $row . '` IN (' . trim(str_repeat('?,', sizeof($values)), ',') . ')';
return static::dbQuery($q, $values);
}
/**
* @param string $row
* @param mixed $value
* @return self::dbDeleteByRowValues()
*/
protected function dbDeleteByRowValue($row, $value) {
return $this->dbDeleteByRowValues($row, [$value]);
}
/**
* Получение элементов по всем указанным ID и кэширование при необходимости
*
* @uses self::dbGetSqlStringByParams()
* @uses Database::query()
*
* @param array $fields список полей для выборки из таблицы
* @param array $ids список все необходимых ID (primary key)
* @return Database::query() результат выборки
*/
protected function dbGetByIds(array $fields, array $ids) {
return $this->dbGetByFields($fields, static::$id_field, $ids);
}
/**
* Получение данных из таблиы по одному ид
*
* @uses self::getByIds()
*
* @param array $fields поля для выборки из таблицы
* @param int $id
* @return array данные выборки
*/
protected function dbGetById(array $fields, $id) {
return $this->dbGetByField($fields, static::$id_field, $id);
}
/**
* @param array $fields
* @param string $row
* @param array $values
* @return array [id => data, ...]
*/
protected function dbGetByFields(array $fields, $row, array $values) {
assert(sizeof($values) > 0);
$q = 'SELECT ' . self::dbGetSqlStringByParams($fields)
. ' FROM ' . static::table()
. ' WHERE `' . $row . '` IN (' . trim(str_repeat('?, ', sizeof($values)), ', ') . ')';
;
return ($data = self::dbQuery($q, $values))
? array_combine(array_column($data, static::$id_field), $data)
: $data;
}
/**
* @see self::dbGetByFields()
*/
protected function dbGetByField(array $fields, $row, $value) {
$rows = $this->dbGetByFields($fields, $row, [$value]);
return array_shift($rows);
}
/**
* Переключение состояния у поля в базе
*
* @param string $field
* @param int $id
* @param int $prev_value Предыдущее значение поля
* @return int
*/
protected function dbToggleField($field, $id, $prev_value = null) {
$q = 'UPDATE ' . static::table()
. ' SET `' . $field . '` = IF (`' . $field . '` = 1, 0, 1)'
. ' WHERE `' . static::$id_field . '` = :id'
. (isset($prev_value) ? ' AND `' . $field . '` = :prev_value' : '');
$params = [static::$id_field => $id];
if (isset($prev_value))
$params['prev_value'] = $prev_value;
return static::dbQuery($q, $params);
}
protected function dbGetPaginated($query, array $params = []) {
assert(is_string($query));
$total = $this->Pagination ? $this->Pagination->getTotal() : 0;
$query = 'SELECT %s FROM ' . static::table() . ' ' . $query . ' LIMIT %d, %d';
if (!$total) {
$row = self::dbQuery(sprintf($query, ...['COUNT(*) AS `count`', 0, 1]), $params);
$total = $row ? $row[0]['count'] : 0;
}
$offset = null;
$limit = null;
if ($this->Pagination) {
$this->Pagination->setTotal($total);
$offset = $this->Pagination->getOffset();
$limit = $this->Pagination->getLimit();
}
$result = $total ? self::dbQuery(sprintf($query, ...['*', $offset, $limit]), $params) : [];
array_walk($result, [$this, 'prepare']);
return $result;
}
/**
* Получение всего списка с данными или списка по условию
*
* @see self::getList( );
*/
public function getList(array $conditions = [], array $order = []) {
$total = $this->Pagination ? $this->Pagination->getTotal() : 0;
if (!$total)
$total = $this->dbCount($conditions);
$offset = null;
$limit = null;
if ($this->Pagination) {
$this->Pagination->setTotal($total);
$offset = $this->Pagination->getOffset();
$limit = $this->Pagination->getLimit();
}
return static::getByIds(
$total ? array_column($this->dbSelect([static::$id_field], $conditions, $order, $offset, $limit), static::$id_field) : []
);
}
}