|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) NAVER <http://www.navercorp.com> */ |
|
3
|
|
|
|
|
4
|
|
|
require_once('DBMysql.class.php'); |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class to use MySQLi DBMS as mysqli_* |
|
8
|
|
|
* mysql handling class |
|
9
|
|
|
* |
|
10
|
|
|
* Does not use prepared statements, since mysql driver does not support them |
|
11
|
|
|
* |
|
12
|
|
|
* @author NAVER ([email protected]) |
|
13
|
|
|
* @package /classes/db |
|
14
|
|
|
* @version 0.1 |
|
15
|
|
|
*/ |
|
16
|
|
|
class DBMysqli extends DBMysql |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
|
function DBMysqli($auto_connect = TRUE) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
$this->_setDBInfo(); |
|
26
|
|
|
if($auto_connect) $this->_connect(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create an instance of this class |
|
31
|
|
|
* @return DBMysqli return DBMysqli object instance |
|
32
|
|
|
*/ |
|
33
|
|
|
function create() |
|
34
|
|
|
{ |
|
35
|
|
|
return new DBMysqli; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* DB Connect |
|
40
|
|
|
* this method is private |
|
41
|
|
|
* @param array $connection connection's value is db_hostname, db_port, db_database, db_userid, db_password |
|
42
|
|
|
* @return resource |
|
43
|
|
|
*/ |
|
44
|
|
View Code Duplication |
function __connect($connection) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
// Attempt to connect |
|
47
|
|
|
if($connection["db_port"]) |
|
48
|
|
|
{ |
|
49
|
|
|
$result = @mysqli_connect($connection["db_hostname"] |
|
50
|
|
|
, $connection["db_userid"] |
|
51
|
|
|
, $connection["db_password"] |
|
52
|
|
|
, $connection["db_database"] |
|
53
|
|
|
, $connection["db_port"]); |
|
54
|
|
|
} |
|
55
|
|
|
else |
|
56
|
|
|
{ |
|
57
|
|
|
$result = @mysqli_connect($connection["db_hostname"] |
|
58
|
|
|
, $connection["db_userid"] |
|
59
|
|
|
, $connection["db_password"] |
|
60
|
|
|
, $connection["db_database"]); |
|
61
|
|
|
} |
|
62
|
|
|
$error = mysqli_connect_errno(); |
|
63
|
|
|
if($error) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->setError($error, mysqli_connect_error()); |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
mysqli_set_charset($result, 'utf8'); |
|
69
|
|
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* DB disconnection |
|
74
|
|
|
* this method is private |
|
75
|
|
|
* @param resource $connection |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
function _close($connection) |
|
79
|
|
|
{ |
|
80
|
|
|
mysqli_close($connection); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Handles quatation of the string variables from the query |
|
85
|
|
|
* @param string $string |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
function addQuotes($string) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
if(version_compare(PHP_VERSION, "5.4.0", "<") && get_magic_quotes_gpc()) |
|
91
|
|
|
{ |
|
92
|
|
|
$string = stripslashes(str_replace("\\", "\\\\", $string)); |
|
93
|
|
|
} |
|
94
|
|
|
if(!is_numeric($string)) |
|
95
|
|
|
{ |
|
96
|
|
|
$connection = $this->_getConnection('master'); |
|
97
|
|
|
$string = mysqli_escape_string($connection, $string); |
|
98
|
|
|
} |
|
99
|
|
|
return $string; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Execute the query |
|
104
|
|
|
* this method is private |
|
105
|
|
|
* @param string $query |
|
106
|
|
|
* @param resource $connection |
|
107
|
|
|
* @return resource |
|
108
|
|
|
*/ |
|
109
|
|
View Code Duplication |
function __query($query, $connection) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
if($this->use_prepared_statements == 'Y') |
|
112
|
|
|
{ |
|
113
|
|
|
// 1. Prepare query |
|
114
|
|
|
$stmt = mysqli_prepare($connection, $query); |
|
115
|
|
|
if($stmt) |
|
116
|
|
|
{ |
|
117
|
|
|
$types = ''; |
|
118
|
|
|
$params = array(); |
|
119
|
|
|
$this->_prepareQueryParameters($types, $params); |
|
120
|
|
|
|
|
121
|
|
|
if(!empty($params)) |
|
122
|
|
|
{ |
|
123
|
|
|
$args[0] = $stmt; |
|
|
|
|
|
|
124
|
|
|
$args[1] = $types; |
|
125
|
|
|
|
|
126
|
|
|
$i = 2; |
|
127
|
|
|
foreach($params as $key => $param) |
|
128
|
|
|
{ |
|
129
|
|
|
$copy[$key] = $param; |
|
|
|
|
|
|
130
|
|
|
$args[$i++] = &$copy[$key]; |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// 2. Bind parameters |
|
134
|
|
|
$status = call_user_func_array('mysqli_stmt_bind_param', $args); |
|
135
|
|
|
if(!$status) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true)); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
// 3. Execute query |
|
142
|
|
|
$status = mysqli_stmt_execute($stmt); |
|
143
|
|
|
|
|
144
|
|
|
if(!$status) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->setError(-1, "Prepared statement failed: $query" . mysqli_error($connection) . PHP_EOL . print_r($args, true)); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
// Return stmt for other processing - like retrieving resultset (_fetch) |
|
150
|
|
|
return $stmt; |
|
151
|
|
|
// mysqli_stmt_close($stmt); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
// Run the query statement |
|
155
|
|
|
$result = mysqli_query($connection, $query); |
|
156
|
|
|
// Error Check |
|
157
|
|
|
$error = mysqli_error($connection); |
|
158
|
|
|
if($error) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->setError(mysqli_errno($connection), $error); |
|
161
|
|
|
} |
|
162
|
|
|
// Return result |
|
163
|
|
|
return $result; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Before execute query, prepare statement |
|
168
|
|
|
* this method is private |
|
169
|
|
|
* @param string $types |
|
170
|
|
|
* @param array $params |
|
171
|
|
|
* @return void |
|
172
|
|
|
*/ |
|
173
|
|
View Code Duplication |
function _prepareQueryParameters(&$types, &$params) |
|
|
|
|
|
|
174
|
|
|
{ |
|
175
|
|
|
$types = ''; |
|
176
|
|
|
$params = array(); |
|
177
|
|
|
if(!$this->param) |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
return; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
foreach($this->param as $k => $o) |
|
183
|
|
|
{ |
|
184
|
|
|
$value = $o->getUnescapedValue(); |
|
185
|
|
|
$type = $o->getType(); |
|
186
|
|
|
|
|
187
|
|
|
// Skip column names -> this should be concatenated to query string |
|
188
|
|
|
if($o->isColumnName()) |
|
189
|
|
|
{ |
|
190
|
|
|
continue; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
switch($type) |
|
194
|
|
|
{ |
|
195
|
|
|
case 'number' : |
|
196
|
|
|
$type = 'i'; |
|
197
|
|
|
break; |
|
198
|
|
|
case 'varchar' : |
|
199
|
|
|
$type = 's'; |
|
200
|
|
|
break; |
|
201
|
|
|
default: |
|
202
|
|
|
$type = 's'; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
if(is_array($value)) |
|
206
|
|
|
{ |
|
207
|
|
|
foreach($value as $v) |
|
208
|
|
|
{ |
|
209
|
|
|
$params[] = $v; |
|
210
|
|
|
$types .= $type; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
else |
|
214
|
|
|
{ |
|
215
|
|
|
$params[] = $value; |
|
216
|
|
|
$types .= $type; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Fetch the result |
|
223
|
|
|
* @param resource $result |
|
224
|
|
|
* @param int|NULL $arrayIndexEndValue |
|
225
|
|
|
* @return array |
|
226
|
|
|
*/ |
|
227
|
|
View Code Duplication |
function _fetch($result, $arrayIndexEndValue = NULL) |
|
|
|
|
|
|
228
|
|
|
{ |
|
229
|
|
|
if($this->use_prepared_statements != 'Y') |
|
230
|
|
|
{ |
|
231
|
|
|
return parent::_fetch($result, $arrayIndexEndValue); |
|
232
|
|
|
} |
|
233
|
|
|
$output = array(); |
|
234
|
|
|
if(!$this->isConnected() || $this->isError() || !$result) |
|
235
|
|
|
{ |
|
236
|
|
|
return $output; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
// Prepared stements: bind result variable and fetch data |
|
240
|
|
|
$stmt = $result; |
|
241
|
|
|
$meta = mysqli_stmt_result_metadata($stmt); |
|
242
|
|
|
$fields = mysqli_fetch_fields($meta); |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Mysqli has a bug that causes LONGTEXT columns not to get loaded |
|
246
|
|
|
* Unless store_result is called before |
|
247
|
|
|
* MYSQLI_TYPE for longtext is 252 |
|
248
|
|
|
*/ |
|
249
|
|
|
$longtext_exists = false; |
|
250
|
|
|
foreach($fields as $field) |
|
251
|
|
|
{ |
|
252
|
|
|
if(isset($resultArray[$field->name])) // When joined tables are used and the same column name appears twice, we should add it separately, otherwise bind_result fails |
|
253
|
|
|
{ |
|
254
|
|
|
$field->name = 'repeat_' . $field->name; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
// Array passed needs to contain references, not values |
|
258
|
|
|
$row[$field->name] = ""; |
|
|
|
|
|
|
259
|
|
|
$resultArray[$field->name] = &$row[$field->name]; |
|
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
if($field->type == 252) |
|
262
|
|
|
{ |
|
263
|
|
|
$longtext_exists = true; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
$resultArray = array_merge(array($stmt), $resultArray); |
|
267
|
|
|
|
|
268
|
|
|
if($longtext_exists) |
|
269
|
|
|
{ |
|
270
|
|
|
mysqli_stmt_store_result($stmt); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
call_user_func_array('mysqli_stmt_bind_result', $resultArray); |
|
274
|
|
|
|
|
275
|
|
|
$rows = array(); |
|
276
|
|
|
while(mysqli_stmt_fetch($stmt)) |
|
277
|
|
|
{ |
|
278
|
|
|
$resultObject = new stdClass(); |
|
279
|
|
|
|
|
280
|
|
|
foreach($resultArray as $key => $value) |
|
281
|
|
|
{ |
|
282
|
|
|
if($key === 0) |
|
283
|
|
|
{ |
|
284
|
|
|
continue; // Skip stmt object |
|
285
|
|
|
} |
|
286
|
|
|
if(strpos($key, 'repeat_')) |
|
287
|
|
|
{ |
|
288
|
|
|
$key = substr($key, 6); |
|
289
|
|
|
} |
|
290
|
|
|
$resultObject->$key = $value; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
$rows[] = $resultObject; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
mysqli_stmt_close($stmt); |
|
297
|
|
|
|
|
298
|
|
|
if($arrayIndexEndValue) |
|
|
|
|
|
|
299
|
|
|
{ |
|
300
|
|
|
foreach($rows as $row) |
|
301
|
|
|
{ |
|
302
|
|
|
$output[$arrayIndexEndValue--] = $row; |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
else |
|
306
|
|
|
{ |
|
307
|
|
|
$output = $rows; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
if(count($output) == 1) |
|
311
|
|
|
{ |
|
312
|
|
|
if(isset($arrayIndexEndValue)) |
|
313
|
|
|
{ |
|
314
|
|
|
return $output; |
|
315
|
|
|
} |
|
316
|
|
|
else |
|
317
|
|
|
{ |
|
318
|
|
|
return $output[0]; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
return $output; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Handles insertAct |
|
327
|
|
|
* @param Object $queryObject |
|
328
|
|
|
* @param boolean $with_values |
|
329
|
|
|
* @return resource |
|
330
|
|
|
*/ |
|
331
|
|
|
function _executeInsertAct($queryObject, $with_values = false) |
|
332
|
|
|
{ |
|
333
|
|
|
if($this->use_prepared_statements != 'Y') |
|
334
|
|
|
{ |
|
335
|
|
|
return parent::_executeInsertAct($queryObject); |
|
336
|
|
|
} |
|
337
|
|
|
$this->param = $queryObject->getArguments(); |
|
338
|
|
|
$result = parent::_executeInsertAct($queryObject, $with_values); |
|
339
|
|
|
unset($this->param); |
|
340
|
|
|
return $result; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Handles updateAct |
|
345
|
|
|
* @param Object $queryObject |
|
346
|
|
|
* @param boolean $with_values |
|
347
|
|
|
* @return resource |
|
348
|
|
|
*/ |
|
349
|
|
|
function _executeUpdateAct($queryObject, $with_values = false) |
|
350
|
|
|
{ |
|
351
|
|
|
if($this->use_prepared_statements != 'Y') |
|
352
|
|
|
{ |
|
353
|
|
|
return parent::_executeUpdateAct($queryObject); |
|
354
|
|
|
} |
|
355
|
|
|
$this->param = $queryObject->getArguments(); |
|
356
|
|
|
$result = parent::_executeUpdateAct($queryObject, $with_values); |
|
357
|
|
|
unset($this->param); |
|
358
|
|
|
return $result; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* Handles deleteAct |
|
363
|
|
|
* @param Object $queryObject |
|
364
|
|
|
* @param boolean $with_values |
|
365
|
|
|
* @return resource |
|
366
|
|
|
*/ |
|
367
|
|
|
function _executeDeleteAct($queryObject, $with_values = false) |
|
368
|
|
|
{ |
|
369
|
|
|
if($this->use_prepared_statements != 'Y') |
|
370
|
|
|
{ |
|
371
|
|
|
return parent::_executeDeleteAct($queryObject); |
|
372
|
|
|
} |
|
373
|
|
|
$this->param = $queryObject->getArguments(); |
|
374
|
|
|
$result = parent::_executeDeleteAct($queryObject, $with_values); |
|
375
|
|
|
unset($this->param); |
|
376
|
|
|
return $result; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Handle selectAct |
|
381
|
|
|
* In order to get a list of pages easily when selecting \n |
|
382
|
|
|
* it supports a method as navigation |
|
383
|
|
|
* @param Object $queryObject |
|
384
|
|
|
* @param resource $connection |
|
385
|
|
|
* @param boolean $with_values |
|
386
|
|
|
* @return Object |
|
387
|
|
|
*/ |
|
388
|
|
View Code Duplication |
function _executeSelectAct($queryObject, $connection = null, $with_values = false) |
|
|
|
|
|
|
389
|
|
|
{ |
|
390
|
|
|
if($this->use_prepared_statements != 'Y') |
|
391
|
|
|
{ |
|
392
|
|
|
return parent::_executeSelectAct($queryObject, $connection); |
|
393
|
|
|
} |
|
394
|
|
|
$this->param = $queryObject->getArguments(); |
|
395
|
|
|
$result = parent::_executeSelectAct($queryObject, $connection, $with_values); |
|
396
|
|
|
unset($this->param); |
|
397
|
|
|
return $result; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Get the ID generated in the last query |
|
402
|
|
|
* Return next sequence from sequence table |
|
403
|
|
|
* This method use only mysql |
|
404
|
|
|
* @return int |
|
405
|
|
|
*/ |
|
406
|
|
|
function db_insert_id() |
|
407
|
|
|
{ |
|
408
|
|
|
$connection = $this->_getConnection('master'); |
|
409
|
|
|
return mysqli_insert_id($connection); |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* Fetch a result row as an object |
|
414
|
|
|
* @param resource $result |
|
415
|
|
|
* @return object |
|
416
|
|
|
*/ |
|
417
|
|
|
function db_fetch_object(&$result) |
|
418
|
|
|
{ |
|
419
|
|
|
return mysqli_fetch_object($result); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* Free result memory |
|
424
|
|
|
* @param resource $result |
|
425
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure. |
|
426
|
|
|
*/ |
|
427
|
|
|
function db_free_result(&$result) |
|
428
|
|
|
{ |
|
429
|
|
|
return mysqli_free_result($result); |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
DBMysqli::$isSupported = function_exists('mysqli_connect'); |
|
|
|
|
|
|
435
|
|
|
|
|
436
|
|
|
/* End of file DBMysqli.class.php */ |
|
437
|
|
|
/* Location: ./classes/db/DBMysqli.class.php */ |
|
438
|
|
|
|