|
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 db_mysql $db |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function setDb($db) { |
|
30
|
|
|
static::$dbStatic = $db; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return db_mysql |
|
35
|
|
|
* |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function getDb() { |
|
38
|
|
|
return (!empty(static::$dbStatic) && static::$dbStatic instanceof db_mysql) || !class_exists('classSupernova', false) ? static::$dbStatic : classSupernova::$db; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string|DBStaticRecord $DBStaticRecord |
|
43
|
|
|
* |
|
44
|
|
|
* @return DbQueryConstructor |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function buildDBQ($DBStaticRecord = '') { |
|
47
|
|
|
$dbClassName = empty($DBStaticRecord) ? get_called_class() : $DBStaticRecord; |
|
48
|
|
|
$dbClassName = is_string($dbClassName) ? $dbClassName : get_class($dbClassName); |
|
49
|
|
|
|
|
50
|
|
|
return DbQueryConstructor::build($dbClassName::getDb(), $dbClassName); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get maximum ID from table |
|
55
|
|
|
* |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function getMaxId() { |
|
59
|
|
|
$stmt = static::buildDBQ()->fieldMax(static::$_idField)->selectValue(); |
|
60
|
|
|
|
|
61
|
|
|
return idval($stmt); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// /** |
|
|
|
|
|
|
65
|
|
|
// * @param string|DBStaticRecord $DBStaticRecord |
|
66
|
|
|
// */ |
|
67
|
|
|
// // TODO - IoC test |
|
68
|
|
|
// public static function getMax($DBStaticRecord) { |
|
69
|
|
|
// $result = static::buildDBQ($DBStaticRecord)->fieldMax($DBStaticRecord::$_idField)->selectValue(); |
|
70
|
|
|
// |
|
71
|
|
|
// return idval($result); |
|
72
|
|
|
// } |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string|DBStaticRecord $DBStaticRecord |
|
76
|
|
|
* @param int|string $recordId |
|
77
|
|
|
* @param mixed|array $fieldList |
|
78
|
|
|
* @param bool $forUpdate |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
// TODO - protected. Code doesn't know about fields |
|
83
|
|
|
public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) { |
|
84
|
|
|
$stmt = |
|
85
|
|
|
static::buildDBQ() |
|
86
|
|
|
->fields($fieldList) |
|
87
|
|
|
->where(static::$_idField . '=' . $recordId); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
if ($forUpdate) { |
|
90
|
|
|
$stmt->setForUpdate(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$result = $stmt->selectRow(); |
|
94
|
|
|
|
|
95
|
|
|
return $result; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array $idList |
|
100
|
|
|
* |
|
101
|
|
|
* @return DbResultIterator |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function queryExistsIdInList($idList) { |
|
104
|
|
|
if (!empty($idList) && is_array($idList)) { |
|
105
|
|
|
$query = |
|
106
|
|
|
static::buildDBQ() |
|
107
|
|
|
->fields(static::$_idField) |
|
108
|
|
|
->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")")) |
|
109
|
|
|
->selectIterator(); |
|
110
|
|
|
} else { |
|
111
|
|
|
$query = new DbEmptyIterator(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $query; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Filter list of ID by only existed IDs in table |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $idList |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function filterIdListStringRepack($idList) { |
|
126
|
|
|
// TODO - remove HelperArray caller |
|
127
|
|
|
$idList = HelperArray::stringToArrayFilterEmpty($idList); |
|
128
|
|
|
|
|
129
|
|
|
$result = array(); |
|
130
|
|
|
if (!empty($idList)) { |
|
131
|
|
|
foreach (static::queryExistsIdInList($idList) as $row) { |
|
132
|
|
|
$result[] = $row[static::$_idField]; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// TODO - remove implode |
|
137
|
|
|
return implode(',', $result); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function lockAllRecords() { |
|
144
|
|
|
static::getDb()->doStmtLockAll(static::buildDBQ()); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
//DBStaticRecord::_init(); |
|
|
|
|
|
|
150
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.