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
|
|
|
* @return DbSqlStatement |
27
|
|
|
*/ |
28
|
|
|
public static function buildSelect() { |
29
|
|
|
return DbSqlStatement::build(null)->getParamsFromStaticClass(get_called_class())->select(false); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return DbSqlStatement |
34
|
|
|
*/ |
35
|
|
|
public static function buildSelectAll() { |
36
|
|
|
return static::buildSelect()->field('*'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return DbSqlStatement |
41
|
|
|
*/ |
42
|
|
|
public static function buildSelectLock() { |
43
|
|
|
return |
44
|
|
|
static::buildSelect()->field(1) |
45
|
|
|
->forUpdate(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $where |
50
|
|
|
* @param mixed|array $fieldList |
51
|
|
|
* Field list can be scalar - it would be converted to array and used as field name |
52
|
|
|
* @param bool $for_update |
53
|
|
|
* |
54
|
|
|
* @return array|null |
55
|
|
|
* |
56
|
|
|
* @see static::getRecordList |
57
|
|
|
*/ |
58
|
|
|
protected static function getRecord($where = array(), $fieldList = '*', $for_update = false) { |
|
|
|
|
59
|
|
|
$result = static::fetchOne( |
60
|
|
|
static::buildSelect()->fields($fieldList) |
61
|
|
|
->where($where) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get maximum ID from table |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public static function getMaxId() { |
73
|
|
|
$maxId = static::getRecord(array(), DbSqlLiteral::build()->max(static::$_idField, 'maxId')); |
74
|
|
|
|
75
|
|
|
return !empty($maxId['maxId']) ? $maxId['maxId'] : 0; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int|string $recordId |
80
|
|
|
* @param mixed|array $fieldList |
81
|
|
|
* @param bool $forUpdate |
82
|
|
|
* |
83
|
|
|
* @return array|null |
84
|
|
|
*/ |
85
|
|
|
public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) { |
86
|
|
|
return static::getRecord(array(static::$_idField . '=' . $recordId), $fieldList, $forUpdate); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param DbSqlStatement $statement |
91
|
|
|
* |
92
|
|
|
* @return array|bool|mysqli_result|null |
93
|
|
|
*/ |
94
|
|
|
protected static function execute($statement) { |
95
|
|
|
return static::$dbStatic->execute((string)$statement); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param DbSqlStatement $statement |
100
|
|
|
* |
101
|
|
|
* @return array|null |
102
|
|
|
*/ |
103
|
|
|
protected static function fetchOne($statement) { |
104
|
|
|
return static::$dbStatic->fetchOne($statement); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $idList |
109
|
|
|
* |
110
|
|
|
* @return mysqli_result|null |
111
|
|
|
*/ |
112
|
|
|
/** |
113
|
|
|
* @param array $idList |
114
|
|
|
* |
115
|
|
|
* @return mysqli_result|null |
116
|
|
|
*/ |
117
|
|
|
public static function queryExistsIdInList($idList) { |
118
|
|
|
$query = null; |
119
|
|
|
if (!empty($idList) && is_array($idList)) { |
120
|
|
|
$query = static::execute( |
121
|
|
|
static::buildSelectAll() |
122
|
|
|
->fields(static::$_idField) |
123
|
|
|
->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")")) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return !empty($query) ? $query : null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $idList |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public static function filterIdListStringRepack($idList) { |
137
|
|
|
// TODO - remove HelperArray caller |
138
|
|
|
$idList = HelperArray::stringToArrayFilterEmpty($idList); |
139
|
|
|
|
140
|
|
|
$result = array(); |
141
|
|
|
if (!empty($idList)) { |
142
|
|
|
$query = static::queryExistsIdInList($idList); |
143
|
|
|
while($row = db_fetch($query)) { |
144
|
|
|
$result[] = $row[static::$_idField]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// TODO - remove implode |
149
|
|
|
return implode(',', $result); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
*/ |
155
|
|
|
public static function lockAllRecords() { |
156
|
|
|
static::execute( |
157
|
|
|
static::buildSelectLock() |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Builds and Executes prepared statement |
163
|
|
|
* |
164
|
|
|
* @param string $sqlQuery |
165
|
|
|
* @param array $values |
166
|
|
|
* |
167
|
|
|
* @return array|bool|mysqli_result|null |
168
|
|
|
*/ |
169
|
|
|
protected static function prepareExecute($sqlQuery, $values = array()) { |
170
|
|
|
return static::$dbStatic->sqlPrepareAndExecute($sqlQuery, $values); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Builds and executes prepared statement then return first record from sets |
175
|
|
|
* |
176
|
|
|
* @param string $sqlQuery |
177
|
|
|
* @param array $values |
178
|
|
|
* |
179
|
|
|
* @return array|null |
180
|
|
|
*/ |
181
|
|
|
protected static function prepareFetchOne($sqlQuery, $values = array()) { |
182
|
|
|
return static::$dbStatic->db_fetch(static::prepareExecute($sqlQuery, $values)); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Builds and executes prepared statement then fetches first record from sets and returns value from first field |
187
|
|
|
* |
188
|
|
|
* @param string $sqlQuery |
189
|
|
|
* @param array $values |
190
|
|
|
* |
191
|
|
|
* @return mixed|null |
192
|
|
|
*/ |
193
|
|
|
protected static function prepareFetchValue($sqlQuery, $values = array()) { |
194
|
|
|
$result = static::prepareFetchOne($sqlQuery, $values); |
195
|
|
|
if (empty($result) || !array($result)) { |
196
|
|
|
$result = null; |
197
|
|
|
} else { |
198
|
|
|
$result = array_pop($result); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $result; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
DBStaticRecord::_init(); |
207
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.