|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class DBStaticRecord |
|
5
|
|
|
*/ |
|
6
|
|
|
class DBStaticRecord { |
|
7
|
|
|
|
|
8
|
|
|
public static $_table = '_table'; |
|
9
|
|
|
public static $_idField = 'id'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var db_mysql $dbStatic |
|
13
|
|
|
*/ |
|
14
|
|
|
protected static $dbStatic; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* DBStaticRecord constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param db_mysql|null $db |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function _init($db = null) { |
|
22
|
|
|
static::$dbStatic = (!empty($db) && $db instanceof db_mysql) || !class_exists('classSupernova', false) ? $db : classSupernova::$db; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param array $where |
|
27
|
|
|
* @param mixed|array $fieldList |
|
28
|
|
|
* Field list can be scalar - it would be converted to array and used as field name |
|
29
|
|
|
* @param bool $for_update |
|
30
|
|
|
* |
|
31
|
|
|
* @return array|null |
|
32
|
|
|
* |
|
33
|
|
|
* @see static::getRecordList |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static function getRecord($where = array(), $fieldList = '*', $for_update = false) { |
|
|
|
|
|
|
36
|
|
|
$result = static::$dbStatic->fetchOne( |
|
37
|
|
|
DbSqlStatement::build(null, get_called_class()) |
|
38
|
|
|
->select() |
|
39
|
|
|
->fields($fieldList) |
|
40
|
|
|
->where($where) |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
return $result; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get maximum ID from table |
|
48
|
|
|
* |
|
49
|
|
|
* @return int |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function getMaxId() { |
|
52
|
|
|
$maxId = static::getRecord(array(), new DbSqlLiteral('MAX(id) AS maxId')); |
|
53
|
|
|
|
|
54
|
|
|
return !empty($maxId['maxId']) ? $maxId['maxId'] : 0; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param int|string $recordId |
|
59
|
|
|
* @param mixed|array $fieldList |
|
60
|
|
|
* @param bool $forUpdate |
|
61
|
|
|
* |
|
62
|
|
|
* @return array|null |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) { |
|
65
|
|
|
// return static::getRecord(array(static::$_idField => $recordId), $fieldList, $forUpdate); |
|
|
|
|
|
|
66
|
|
|
return static::getRecord(array(static::$_idField . '=' . $recordId), $fieldList, $forUpdate); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $idList |
|
72
|
|
|
* |
|
73
|
|
|
* @return mysqli_result|null |
|
74
|
|
|
*/ |
|
75
|
|
|
/** |
|
76
|
|
|
* @param array $idList |
|
77
|
|
|
* |
|
78
|
|
|
* @return mysqli_result|null |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function queryExistsIdInList($idList) { |
|
81
|
|
|
$query = null; |
|
82
|
|
|
if (!empty($idList) && is_array($idList)) { |
|
83
|
|
|
$query = static::$dbStatic->execute( |
|
84
|
|
|
DbSqlStatement::build(null, get_called_class()) |
|
85
|
|
|
->select() |
|
86
|
|
|
->fields(static::$_idField) |
|
|
|
|
|
|
87
|
|
|
->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")")) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return !empty($query) ? $query : null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $idList |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function filterIdListStringRepack($idList) { |
|
101
|
|
|
// TODO - remove HelperArray caller |
|
102
|
|
|
$idList = HelperArray::stringToArrayFilterEmpty($idList); |
|
103
|
|
|
|
|
104
|
|
|
$result = array(); |
|
105
|
|
|
if (!empty($idList)) { |
|
106
|
|
|
$query = static::queryExistsIdInList($idList); |
|
107
|
|
|
while ($row = db_fetch($query)) { |
|
108
|
|
|
$result[] = $row[static::$_idField]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// TODO - remove implode |
|
113
|
|
|
return implode(',', $result); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
DBStaticRecord::_init(); |
|
119
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.