-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathDbiMysqli.php
More file actions
343 lines (300 loc) · 10.6 KB
/
DbiMysqli.php
File metadata and controls
343 lines (300 loc) · 10.6 KB
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
<?php declare(strict_types = 1);
/**
* A phpMyAdmin DBI extension for the MySQL-on-SQLite driver.
*
* This implementation is based on the original PhpMyAdmin\Dbal\DbiMysqli class.
* It is modified to use the MySQL-on-SQLite driver instead of MySQLi extension.
*
* @see https://github.com/phpmyadmin/phpmyadmin/blob/962857e4f63d42e38f11ff4d63f5e722018add76/libraries/classes/Dbal/DbiMysqli.php
* @see https://github.com/phpmyadmin/phpmyadmin/blob/142c0cf3be84c346174b730b6aa3ebcf44029256/src/Dbal/MysqliResult.php
*/
namespace PhpMyAdmin\Dbal;
use Closure;
use Exception;
use Generator;
use PhpMyAdmin\FieldMetadata;
use PhpMyAdmin\Query\Utilities;
use Throwable;
use WP_SQLite_Connection;
use WP_SQLite_Driver;
// Load the SQLite driver.
global $wp_env;
$wp_env = require '/internal/shared/wp-env.php';
if ($wp_env['db']['type'] === 'sqlite') {
require_once $wp_env['db']['driver_path'];
} else {
die('Error: Unsupported database type: ' . $wp_env['db']['type']);
}
// Supress the following phpMyAdmin warning:
// "The mysqlnd extension is missing. Please check your PHP configuration."
Closure::bind(
function () {
$this->errors = array_values(
array_filter(
$this->errors,
function ($error) {
$skip = (
strpos($error->getMessage(), 'mysqlnd') !== false
&& strpos($error->getMessage(), 'extension is missing') !== false
);
return !$skip;
}
)
);
},
$GLOBALS['errorHandler'],
$GLOBALS['errorHandler']
)();
// Ensure MySQLi type constants are defined for phpMyAdmin.
if (!defined('MYSQLI_TYPE_DECIMAL')) define('MYSQLI_TYPE_DECIMAL', 0);
if (!defined('MYSQLI_TYPE_TINY')) define('MYSQLI_TYPE_TINY', 1);
if (!defined('MYSQLI_TYPE_CHAR')) define('MYSQLI_TYPE_CHAR', 1);
if (!defined('MYSQLI_TYPE_SHORT')) define('MYSQLI_TYPE_SHORT', 2);
if (!defined('MYSQLI_TYPE_LONG')) define('MYSQLI_TYPE_LONG', 3);
if (!defined('MYSQLI_TYPE_FLOAT')) define('MYSQLI_TYPE_FLOAT', 4);
if (!defined('MYSQLI_TYPE_DOUBLE')) define('MYSQLI_TYPE_DOUBLE', 5);
if (!defined('MYSQLI_TYPE_NULL')) define('MYSQLI_TYPE_NULL', 6);
if (!defined('MYSQLI_TYPE_TIMESTAMP')) define('MYSQLI_TYPE_TIMESTAMP', 7);
if (!defined('MYSQLI_TYPE_LONGLONG')) define('MYSQLI_TYPE_LONGLONG', 8);
if (!defined('MYSQLI_TYPE_INT24')) define('MYSQLI_TYPE_INT24', 9);
if (!defined('MYSQLI_TYPE_DATE')) define('MYSQLI_TYPE_DATE', 10);
if (!defined('MYSQLI_TYPE_TIME')) define('MYSQLI_TYPE_TIME', 11);
if (!defined('MYSQLI_TYPE_DATETIME')) define('MYSQLI_TYPE_DATETIME', 12);
if (!defined('MYSQLI_TYPE_YEAR')) define('MYSQLI_TYPE_YEAR', 13);
if (!defined('MYSQLI_TYPE_NEWDATE')) define('MYSQLI_TYPE_NEWDATE', 14);
if (!defined('MYSQLI_TYPE_BIT')) define('MYSQLI_TYPE_BIT', 16);
if (!defined('MYSQLI_TYPE_VECTOR')) define('MYSQLI_TYPE_VECTOR', 242);
if (!defined('MYSQLI_TYPE_JSON')) define('MYSQLI_TYPE_JSON', 245);
if (!defined('MYSQLI_TYPE_NEWDECIMAL')) define('MYSQLI_TYPE_NEWDECIMAL', 246);
if (!defined('MYSQLI_TYPE_ENUM')) define('MYSQLI_TYPE_ENUM', 247);
if (!defined('MYSQLI_TYPE_SET')) define('MYSQLI_TYPE_SET', 248);
if (!defined('MYSQLI_TYPE_TINY_BLOB')) define('MYSQLI_TYPE_TINY_BLOB', 249);
if (!defined('MYSQLI_TYPE_MEDIUM_BLOB')) define('MYSQLI_TYPE_MEDIUM_BLOB', 250);
if (!defined('MYSQLI_TYPE_LONG_BLOB')) define('MYSQLI_TYPE_LONG_BLOB', 251);
if (!defined('MYSQLI_TYPE_BLOB')) define('MYSQLI_TYPE_BLOB', 252);
if (!defined('MYSQLI_TYPE_VAR_STRING')) define('MYSQLI_TYPE_VAR_STRING', 253);
if (!defined('MYSQLI_TYPE_STRING')) define('MYSQLI_TYPE_STRING', 243);
if (!defined('MYSQLI_TYPE_GEOMETRY')) define('MYSQLI_TYPE_GEOMETRY', 255);
// Ensure MySQLi flags constants are defined for phpMyAdmin.
if (!defined('MYSQLI_NOT_NULL_FLAG')) define('MYSQLI_NOT_NULL_FLAG', 1);
if (!defined('MYSQLI_PRI_KEY_FLAG')) define('MYSQLI_PRI_KEY_FLAG', 2);
if (!defined('MYSQLI_UNIQUE_KEY_FLAG')) define('MYSQLI_UNIQUE_KEY_FLAG', 4);
if (!defined('MYSQLI_MULTIPLE_KEY_FLAG')) define('MYSQLI_MULTIPLE_KEY_FLAG', 8);
if (!defined('MYSQLI_BLOB_FLAG')) define('MYSQLI_BLOB_FLAG', 16);
if (!defined('MYSQLI_UNSIGNED_FLAG')) define('MYSQLI_UNSIGNED_FLAG', 32);
if (!defined('MYSQLI_ZEROFILL_FLAG')) define('MYSQLI_ZEROFILL_FLAG', 64);
if (!defined('MYSQLI_BINARY_FLAG')) define('MYSQLI_BINARY_FLAG', 128);
if (!defined('MYSQLI_ENUM_FLAG')) define('MYSQLI_ENUM_FLAG', 256);
if (!defined('MYSQLI_AUTO_INCREMENT_FLAG')) define('MYSQLI_AUTO_INCREMENT_FLAG', 512);
if (!defined('MYSQLI_TIMESTAMP_FLAG')) define('MYSQLI_TIMESTAMP_FLAG', 1024);
if (!defined('MYSQLI_SET_FLAG')) define('MYSQLI_SET_FLAG', 2048);
if (!defined('MYSQLI_NO_DEFAULT_VALUE_FLAG')) define('MYSQLI_NO_DEFAULT_VALUE_FLAG', 4096);
if (!defined('MYSQLI_ON_UPDATE_NOW_FLAG')) define('MYSQLI_ON_UPDATE_NOW_FLAG', 8192);
if (!defined('MYSQLI_PART_KEY_FLAG')) define('MYSQLI_PART_KEY_FLAG', 16384);
if (!defined('MYSQLI_NUM_FLAG')) define('MYSQLI_NUM_FLAG', 32768);
if (!defined('MYSQLI_GROUP_FLAG')) define('MYSQLI_GROUP_FLAG', 32768);
/**
* A custom result class for the MySQL-on-SQLite driver.
*
* This implementation is based on the original PhpMyAdmin\Dbal\MysqliResult class.
*
* @see https://github.com/phpmyadmin/phpmyadmin/blob/142c0cf3be84c346174b730b6aa3ebcf44029256/src/Dbal/MysqliResult.php
*/
class Result implements ResultInterface {
/** @var array */
private $rows = array();
/** @var array */
private $columns = array();
/** @var int */
private $row_offset = 0;
public function __construct($rows, $columns) {
$this->rows = array();
if (is_array($rows)) {
foreach ($rows as $row) {
$this->rows[] = (array) $row;
}
}
$this->columns = $columns;
}
public function fetchAllAssoc(): array {
return $this->rows;
}
public function fetchAllColumn(): array {
return array_column($this->rows, 0);
}
public function fetchAllKeyPair(): array {
return array_combine(
array_column($this->rows, 0),
array_column($this->rows, 1)
);
}
public function fetchAssoc(): array {
$row = $this->rows[$this->row_offset++] ?? false;
if ($row === false) {
return array();
}
return $row;
}
public function fetchRow(): array {
$row = $this->rows[$this->row_offset++] ?? false;
if ($row === false) {
return [];
}
return array_values($row);
}
public function fetchValue($field = 0) {
if (is_string($field)) {
$row = $this->fetchAssoc();
} else {
$row = $this->fetchRow();
}
return $row[$field] ?? false;
}
public function getFieldNames(): array {
$names = array();
foreach ($this->columns as $column) {
$names[] = $column['name'];
}
return $names;
}
public function getFieldsMeta(): array {
$meta = array();
foreach ($this->columns as $column) {
$flags = $column['flags'] ?? array();
// PhpMyAdmin expects MySQLi-like column metadata rather than PDO syntax.
// The SQLite driver provides it in "mysqli:" prefixed metadata keys.
foreach ($column as $key => $value) {
if (strpos($key, 'mysqli:') === 0) {
$column[substr($key, 7)] = $value;
}
}
// Convert PDO-style flags array to MySQLi-style integer bitmask.
// TODO: Remove this when the driver implements "mysqli:flags".
$mysqli_flags = 0;
foreach ($flags as $flag) {
switch ($flag) {
case 'primary_key':
$mysqli_flags |= \MYSQLI_PRI_KEY_FLAG;
break;
case 'unique_key':
$mysqli_flags |= \MYSQLI_UNIQUE_KEY_FLAG;
break;
case 'not_null':
$mysqli_flags |= \MYSQLI_NOT_NULL_FLAG;
break;
case 'auto_increment':
$mysqli_flags |= \MYSQLI_AUTO_INCREMENT_FLAG;
break;
}
}
$column['flags'] = $mysqli_flags;
$field = (object) $column;
$meta[] = new FieldMetadata($field->type, $field->flags, $field);
}
return $meta;
}
public function getIterator(): Generator {
$this->row_offset = 0;
foreach ($this->rows as $row) {
yield $row;
}
}
public function numFields(): int {
return count($this->columns);
}
public function numRows() {
return count($this->rows);
}
public function seek(int $offset): bool {
$this->row_offset = $offset;
if ($this->row_offset >= count($this->rows)) {
return false;
}
return true;
}
}
/**
* A custom DBI extension for the MySQL-on-SQLite driver.
*
* This implementation is based on the original PhpMyAdmin\Dbal\DbiMysqli class.
*
* @see https://github.com/phpmyadmin/phpmyadmin/blob/962857e4f63d42e38f11ff4d63f5e722018add76/libraries/classes/Dbal/DbiMysqli.php
*/
class DbiMysqli implements DbiExtension {
/** @var WP_SQLite_Driver */
private $driver;
/** @var string */
private $last_error_message = '';
/** @var int */
private $last_error_number = 0;
public function connect($user, $password, array $server) {
global $wp_env;
$this->driver = new WP_SQLite_Driver(
new WP_SQLite_Connection(array('path' => $wp_env['db']['path'])),
'wordpress'
);
return $this->driver;
}
public function selectDb($databaseName, $link): bool {
$link->query(sprintf('USE %s', $link->get_connection()->quote_identifier($databaseName)));
return true;
}
public function realQuery(string $query, $link, int $options) {
try {
$this->last_error_message = '';
$this->last_error_number = 0;
$result = $link->query($query);
} catch (Throwable $e) {
$this->last_error_message = $e->getMessage();
$this->last_error_number = $e->getCode();
return false;
}
if ($result === false) {
return false;
}
return new Result($result, $link->get_last_column_meta());
}
public function realMultiQuery($link, $query): bool {
return false; // Multi-query not implemented.
}
public function moreResults($link): bool {
return false; // Multi-query not implemented.
}
public function nextResult($link): bool {
return false; // Multi-query not implemented.
}
public function storeResult($link) {
return false; // Multi-query not implemented.
}
public function getHostInfo($link) {
return 'WorPress Playground connection';
}
public function getProtoInfo($link) {
return 10;
}
public function getClientInfo() {
return 'mysql-on-sqlite 8.0.38';
}
public function getError($link): string
{
$error_number = $this->last_error_number;
$error_message = $this->last_error_message;
$GLOBALS['errno'] = $error_number;
if ($error_number === 0 || $error_message === '') {
return '';
}
return Utilities::formatError($error_number, $error_message);
}
public function affectedRows($link) {
$value = $link->get_last_return_value();
return is_int($value) ? $value : 0;
}
public function escapeString($link, $string) {
// For some reason, using "$link->get_connection()->quote($string)"
// causes the strings to be double-quoted. Let's skip the quoting.
return $string;
}
public function prepare($link, string $query) {
throw new Exception('Not implemented');
}
}