|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace voku\db; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Helper: this handles extra functions that use the "DB"-Class |
|
7
|
|
|
* |
|
8
|
|
|
* @package voku\db |
|
9
|
|
|
*/ |
|
10
|
|
|
class Helper |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Check if "mysqlnd"-driver is used. |
|
14
|
|
|
* |
|
15
|
|
|
* @return bool |
|
16
|
|
|
*/ |
|
17
|
30 |
|
public static function isMysqlndIsUsed() |
|
18
|
|
|
{ |
|
19
|
30 |
|
static $_mysqlnd_is_used = null; |
|
20
|
|
|
|
|
21
|
30 |
|
if ($_mysqlnd_is_used === null) { |
|
22
|
1 |
|
$_mysqlnd_is_used = (extension_loaded('mysqlnd') && function_exists('mysqli_fetch_all')); |
|
23
|
1 |
|
} |
|
24
|
|
|
|
|
25
|
30 |
|
return $_mysqlnd_is_used; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Check if the current environment supports "utf8mb4". |
|
30
|
|
|
* |
|
31
|
|
|
* @param DB $db |
|
32
|
|
|
* |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
5 |
|
public static function isUtf8mb4Supported(DB $db) |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/ |
|
39
|
|
|
* |
|
40
|
|
|
* - You’re currently using the utf8 character set. |
|
41
|
|
|
* - Your MySQL server is version 5.5.3 or higher (including all 10.x versions of MariaDB). |
|
42
|
|
|
* - Your MySQL client libraries are version 5.5.3 or higher. If you’re using mysqlnd, 5.0.9 or higher. |
|
43
|
|
|
* |
|
44
|
|
|
* INFO: utf8mb4 is 100% backwards compatible with utf8. |
|
45
|
|
|
*/ |
|
46
|
|
|
|
|
47
|
5 |
|
$server_version = self::get_mysql_server_version($db); |
|
48
|
5 |
|
$client_version = self::get_mysql_client_version($db); |
|
49
|
|
|
|
|
50
|
|
|
if ( |
|
51
|
|
|
$server_version >= 50503 |
|
52
|
5 |
|
&& |
|
53
|
|
|
( |
|
54
|
|
|
( |
|
55
|
5 |
|
self::isMysqlndIsUsed() === true |
|
56
|
5 |
|
&& |
|
57
|
|
|
$client_version >= 50009 |
|
58
|
5 |
|
) |
|
59
|
|
|
|| |
|
60
|
|
|
( |
|
61
|
|
|
self::isMysqlndIsUsed() === false |
|
62
|
|
|
&& |
|
63
|
|
|
$client_version >= 50503 |
|
64
|
|
|
) |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
5 |
|
) { |
|
68
|
5 |
|
return true; |
|
69
|
|
|
} else { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* A string that represents the MySQL client library version. |
|
76
|
|
|
* |
|
77
|
|
|
* @param DB $db |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
5 |
|
public static function get_mysql_client_version(DB $db) |
|
82
|
|
|
{ |
|
83
|
5 |
|
static $_mysqli_client_version = null; |
|
84
|
|
|
|
|
85
|
5 |
|
if ($_mysqli_client_version === null) { |
|
86
|
1 |
|
$_mysqli_client_version = mysqli_get_client_version($db->getLink()); |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
return $_mysqli_client_version; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns a string representing the version of the MySQL server that the MySQLi extension is connected to. |
|
95
|
|
|
* |
|
96
|
|
|
* @param DB $db |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
5 |
|
public static function get_mysql_server_version(DB $db) |
|
101
|
|
|
{ |
|
102
|
5 |
|
static $_mysqli_server_version = null; |
|
103
|
|
|
|
|
104
|
5 |
|
if ($_mysqli_server_version === null) { |
|
105
|
1 |
|
$_mysqli_server_version = mysqli_get_server_version($db->getLink()); |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
5 |
|
return $_mysqli_server_version; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Return all db-fields from a table. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $table |
|
115
|
|
|
* @param bool $useStaticCache |
|
116
|
|
|
* @param DB|null $db |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public static function getDbFields($table, $useStaticCache = true, DB $db = null) |
|
121
|
|
|
{ |
|
122
|
1 |
|
static $dbFieldsCache = array(); |
|
123
|
|
|
|
|
124
|
|
|
// use the static cache |
|
125
|
|
|
if ( |
|
126
|
|
|
$useStaticCache === true |
|
127
|
1 |
|
&& |
|
128
|
1 |
|
isset($dbFieldsCache[$table]) |
|
129
|
1 |
|
) { |
|
130
|
1 |
|
return $dbFieldsCache[$table]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// init |
|
134
|
1 |
|
$dbFields = array(); |
|
135
|
|
|
|
|
136
|
1 |
|
if ($db === null) { |
|
137
|
1 |
|
$db = DB::getInstance(); |
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
$debug = new Debug($db); |
|
141
|
1 |
|
if ($table === '') { |
|
142
|
|
|
$debug->displayError('invalid table name'); |
|
143
|
|
|
|
|
144
|
|
|
return array(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
$sql = 'SHOW COLUMNS FROM ' . $db->quote_string($table); |
|
148
|
1 |
|
$result = $db->query($sql); |
|
149
|
|
|
|
|
150
|
1 |
|
if ($result && $result->num_rows > 0) { |
|
151
|
1 |
|
foreach ($result->fetchAllArray() as $tmpResult) { |
|
152
|
1 |
|
$dbFields[] = $tmpResult['Field']; |
|
153
|
1 |
|
} |
|
154
|
1 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
// add to static cache |
|
157
|
1 |
|
$dbFieldsCache[$table] = $dbFields; |
|
158
|
|
|
|
|
159
|
1 |
|
return $dbFields; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Copy row within a DB table and making updates to the columns. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $table |
|
166
|
|
|
* @param array $whereArray |
|
167
|
|
|
* @param array $updateArray |
|
168
|
|
|
* @param array $ignoreArray |
|
169
|
|
|
* @param DB|null $db <p>Use <strong>null</strong> |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool|int "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
|
172
|
|
|
* "false" on error |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public static function copyTableRow($table, array $whereArray, array $updateArray = array(), array $ignoreArray = array(), DB $db = null) |
|
175
|
|
|
{ |
|
176
|
|
|
// init |
|
177
|
1 |
|
$table = trim($table); |
|
178
|
|
|
|
|
179
|
1 |
|
if ($db === null) { |
|
180
|
1 |
|
$db = DB::getInstance(); |
|
181
|
1 |
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
$debug = new Debug($db); |
|
184
|
1 |
|
if ($table === '') { |
|
185
|
|
|
$debug->displayError('invalid table name'); |
|
186
|
|
|
|
|
187
|
|
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
$whereSQL = ''; |
|
191
|
1 |
|
foreach ($whereArray as $key => $value) { |
|
192
|
1 |
|
$whereSQL .= ' AND ' . $db->escape($key) . ' = ' . $db->escape($value); |
|
193
|
1 |
|
} |
|
194
|
|
|
|
|
195
|
|
|
// get the row |
|
196
|
1 |
|
$query = 'SELECT * FROM ' . $db->quote_string($table) . ' |
|
197
|
|
|
WHERE 1 = 1 |
|
198
|
1 |
|
' . $whereSQL . ' |
|
199
|
1 |
|
'; |
|
200
|
1 |
|
$result = $db->query($query); |
|
201
|
|
|
|
|
202
|
|
|
// make sure the row exists |
|
203
|
1 |
|
if ($result->num_rows > 0) { |
|
204
|
|
|
|
|
205
|
1 |
|
foreach ($result->fetchAllArray() as $tmpArray) { |
|
206
|
|
|
|
|
207
|
|
|
// re-build a new DB query and ignore some field-names |
|
208
|
1 |
|
$bindings = array(); |
|
209
|
1 |
|
$insert_keys = ''; |
|
210
|
1 |
|
$insert_values = ''; |
|
211
|
|
|
|
|
212
|
1 |
|
foreach ($tmpArray as $fieldName => $value) { |
|
213
|
|
|
|
|
214
|
1 |
|
if (!in_array($fieldName, $ignoreArray, true)) { |
|
215
|
1 |
|
if (array_key_exists($fieldName, $updateArray)) { |
|
216
|
1 |
|
$insert_keys .= ',' . $fieldName; |
|
217
|
1 |
|
$insert_values .= ',?'; |
|
218
|
1 |
|
$bindings[] = $db->escape($updateArray[$fieldName]); |
|
219
|
1 |
|
} else { |
|
220
|
1 |
|
$insert_keys .= ',' . $fieldName; |
|
221
|
1 |
|
$insert_values .= ',?'; |
|
222
|
1 |
|
$bindings[] = $db->escape($value); |
|
223
|
|
|
} |
|
224
|
1 |
|
} |
|
225
|
1 |
|
} |
|
226
|
|
|
|
|
227
|
1 |
|
$insert_keys = ltrim($insert_keys, ','); |
|
228
|
1 |
|
$insert_values = ltrim($insert_values, ','); |
|
229
|
|
|
|
|
230
|
|
|
// insert the "copied" row |
|
231
|
1 |
|
$new_query = 'INSERT INTO ' . $db->quote_string($table) . ' (' . $insert_keys . ') |
|
232
|
1 |
|
VALUES (' . $insert_values . ') |
|
233
|
1 |
|
'; |
|
234
|
1 |
|
return $db->query($new_query, $bindings); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return false; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|