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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Fake database driver for testing purposes
*
* It has hardcoded results for given queries what makes easy to use it
* in testsuite. Feel free to include other queries which your test will
* need.
*
* @package PhpMyAdmin-DBI
* @subpackage Dummy
*/
namespace PhpMyAdmin\Dbi;
require_once 'libraries/dbi/dbi_dummy.inc.php';
/**
* Fake database driver for testing purposes
*
* It has hardcoded results for given queries what makes easy to use it
* in testsuite. Feel free to include other queries which your test will
* need.
*
* @package PhpMyAdmin-DBI
* @subpackage Dummy
*/
class DbiDummy implements DbiExtension
{
private $_queries = array();
const OFFSET_GLOBAL = 1000;
/**
* connects to the database server
*
* @param string $user mysql user name
* @param string $password mysql user password
* @param array $server host/port/socket/persistent
*
* @return mixed false on error or a mysqli object on success
*/
public function connect(
$user,
$password,
array $server = []
) {
return true;
}
/**
* selects given database
*
* @param string $dbname name of db to select
* @param resource $link mysql link resource
*
* @return bool
*/
public function selectDb($dbname, $link)
{
$GLOBALS['dummy_db'] = $dbname;
return true;
}
/**
* runs a query and returns the result
*
* @param string $query query to run
* @param resource $link mysql link resource
* @param int $options query options
*
* @return mixed
*/
public function realQuery($query, $link = null, $options = 0)
{
$query = trim(preg_replace('/ */', ' ', str_replace("\n", ' ', $query)));
for ($i = 0, $nb = count($this->_queries); $i < $nb; $i++) {
if ($this->_queries[$i]['query'] != $query) {
continue;
}
$this->_queries[$i]['pos'] = 0;
if (!is_array($this->_queries[$i]['result'])) {
return false;
}
return $i;
}
for ($i = 0, $nb = count($GLOBALS['dummy_queries']); $i < $nb; $i++) {
if ($GLOBALS['dummy_queries'][$i]['query'] != $query) {
continue;
}
$GLOBALS['dummy_queries'][$i]['pos'] = 0;
if (!is_array($GLOBALS['dummy_queries'][$i]['result'])) {
return false;
}
return $i + self::OFFSET_GLOBAL;
}
echo "Not supported query: $query\n";
return false;
}
/**
* Run the multi query and output the results
*
* @param resource $link connection object
* @param string $query multi query statement to execute
*
* @return array|bool
*/
public function realMultiQuery($link, $query)
{
return false;
}
/**
* returns result data from $result
*
* @param object $result MySQL result
*
* @return array
*/
public function fetchAny($result)
{
$query_data = &$this->getQueryData($result);
if ($query_data['pos'] >= count($query_data['result'])) {
return false;
}
$ret = $query_data['result'][$query_data['pos']];
$query_data['pos'] += 1;
return $ret;
}
/**
* returns array of rows with associative and numeric keys from $result
*
* @param object $result result MySQL result
*
* @return array
*/
public function fetchArray($result)
{
$query_data = &$this->getQueryData($result);
$data = $this->fetchAny($result);
if (!is_array($data)
|| !isset($query_data['columns'])
) {
return $data;
}
foreach ($data as $key => $val) {
$data[$query_data['columns'][$key]] = $val;
}
return $data;
}
/**
* returns array of rows with associative keys from $result
*
* @param object $result MySQL result
*
* @return array
*/
public function fetchAssoc($result)
{
$data = $this->fetchAny($result);
$query_data = &$this->getQueryData($result);
if (!is_array($data) || !isset($query_data['columns'])) {
return $data;
}
$ret = array();
foreach ($data as $key => $val) {
$ret[$query_data['columns'][$key]] = $val;
}
return $ret;
}
/**
* returns array of rows with numeric keys from $result
*
* @param object $result MySQL result
*
* @return array
*/
public function fetchRow($result)
{
$data = $this->fetchAny($result);
return $data;
}
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param object $result database result
* @param integer $offset offset to seek
*
* @return bool true on success, false on failure
*/
public function dataSeek($result, $offset)
{
$query_data = &$this->getQueryData($result);
if ($offset > count($query_data['result'])) {
return false;
}
$query_data['pos'] = $offset;
return true;
}
/**
* Frees memory associated with the result
*
* @param object $result database result
*
* @return void
*/
public function freeResult($result)
{
return;
}
/**
* Check if there are any more query results from a multi query
*
* @param resource $link the connection object
*
* @return bool false
*/
public function moreResults($link)
{
return false;
}
/**
* Prepare next result from multi_query
*
* @param resource $link the connection object
*
* @return boolean false
*/
public function nextResult($link)
{
return false;
}
/**
* Store the result returned from multi query
*
* @param resource $link the connection object
*
* @return mixed false when empty results / result set when not empty
*/
public function storeResult($link)
{
return false;
}
/**
* Returns a string representing the type of connection used
*
* @param resource $link mysql link
*
* @return string type of connection used
*/
public function getHostInfo($link)
{
return '';
}
/**
* Returns the version of the MySQL protocol used
*
* @param resource $link mysql link
*
* @return integer version of the MySQL protocol used
*/
public function getProtoInfo($link)
{
return -1;
}
/**
* returns a string that represents the client library version
*
* @return string MySQL client library version
*/
public function getClientInfo()
{
return '';
}
/**
* returns last error message or false if no errors occurred
*
* @param resource $link connection link
*
* @return string|bool $error or false
*/
public function getError($link)
{
return false;
}
/**
* returns the number of rows returned by last query
*
* @param object $result MySQL result
*
* @return string|int
*/
public function numRows($result)
{
if (is_bool($result)) {
return 0;
}
$query_data = &$this->getQueryData($result);
return count($query_data['result']);
}
/**
* returns the number of rows affected by last query
*
* @param resource $link the mysql object
* @param bool $get_from_cache whether to retrieve from cache
*
* @return string|int
*/
public function affectedRows($link = null, $get_from_cache = true)
{
return 0;
}
/**
* returns metainfo for fields in $result
*
* @param object $result result set identifier
*
* @return array meta info for fields in $result
*/
public function getFieldsMeta($result)
{
return array();
}
/**
* return number of fields in given $result
*
* @param object $result MySQL result
*
* @return int field count
*/
public function numFields($result)
{
$query_data = &$this->getQueryData($result);
if (!isset($query_data['columns'])) {
return 0;
}
return count($query_data['columns']);
}
/**
* returns the length of the given field $i in $result
*
* @param object $result result set identifier
* @param int $i field
*
* @return int length of field
*/
public function fieldLen($result, $i)
{
return -1;
}
/**
* returns name of $i. field in $result
*
* @param object $result result set identifier
* @param int $i field
*
* @return string name of $i. field in $result
*/
public function fieldName($result, $i)
{
return '';
}
/**
* returns concatenated string of human readable field flags
*
* @param object $result result set identifier
* @param int $i field
*
* @return string field flags
*/
public function fieldFlags($result, $i)
{
return '';
}
/**
* returns properly escaped string for use in MySQL queries
*
* @param mixed $link database link
* @param string $str string to be escaped
*
* @return string a MySQL escaped string
*/
public function escapeString($link, $str)
{
return $str;
}
/**
* Adds query result for testing
*
* @param string $query SQL
* @param array $result Expected result
*
* @return void
*/
public function setResult($query, $result)
{
$this->_queries[] = array(
'query' => $query,
'result' => $result,
);
}
/**
* Return query data for ID
*
* @param object $result result set identifier
*
* @return array
*/
private function &getQueryData($result)
{
if ($result >= self::OFFSET_GLOBAL) {
return $GLOBALS['dummy_queries'][$result - self::OFFSET_GLOBAL];
} else {
return $this->_queries[$result];
}
}
}