XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
DBMysqli_innodb.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
4 require_once('DBMysql.class.php');
5 
16 class DBMysqli_innodb extends DBMysql
17 {
18 
23  function __construct($auto_connect = TRUE)
24  {
25  $this->_setDBInfo();
26  if($auto_connect) $this->_connect();
27  }
28 
33  function create()
34  {
35  return new DBMysqli_innodb;
36  }
37 
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 
78  function _close($connection)
79  {
80  mysqli_close($connection);
81  }
82 
88  function _begin($transactionLevel = 0)
89  {
90  $connection = $this->_getConnection('master');
91 
92  if(!$transactionLevel)
93  {
94  $this->_query("begin");
95  }
96  else
97  {
98  $this->_query("SAVEPOINT SP" . $transactionLevel, $connection);
99  }
100  return true;
101  }
102 
108  function _rollback($transactionLevel = 0)
109  {
110  $connection = $this->_getConnection('master');
111 
112  $point = $transactionLevel - 1;
113 
114  if($point)
115  {
116  $this->_query("ROLLBACK TO SP" . $point, $connection);
117  }
118  else
119  {
120  mysqli_rollback($connection);
121  $this->setQueryLog( array("query"=>"rollback") );
122  }
123  return true;
124  }
125 
131  function _commit()
132  {
133  $connection = $this->_getConnection('master');
134  mysqli_commit($connection);
135  $this->setQueryLog( array("query"=>"commit") );
136  return true;
137  }
138 
139 
140 
146  function addQuotes($string)
147  {
148  if(version_compare(PHP_VERSION, "5.4.0", "<") && get_magic_quotes_gpc())
149  {
150  $string = stripslashes(str_replace("\\", "\\\\", $string));
151  }
152  if(!is_numeric($string))
153  {
154  $connection = $this->_getConnection('master');
155  $string = mysqli_escape_string($connection, $string);
156  }
157  return $string;
158  }
159 
168  {
169  if($this->use_prepared_statements == 'Y')
170  {
171  // 1. Prepare query
172  $stmt = mysqli_prepare($connection, $query);
173  if($stmt)
174  {
175  $types = '';
176  $params = array();
177  $this->_prepareQueryParameters($types, $params);
178 
179  if(!empty($params))
180  {
181  $args[0] = $stmt;
182  $args[1] = $types;
183 
184  $i = 2;
185  foreach($params as $key => $param)
186  {
187  $copy[$key] = $param;
188  $args[$i++] = &$copy[$key];
189  }
190 
191  // 2. Bind parameters
192  $status = call_user_func_array('mysqli_stmt_bind_param', $args);
193  if(!$status)
194  {
195  $this->setError(-1, "Invalid arguments: $query" . mysqli_error($connection));
196  }
197  }
198 
199  // 3. Execute query
200  $status = mysqli_stmt_execute($stmt);
201 
202  if(!$status)
203  {
204  $this->setError(-1, "Prepared statement failed: $query" . mysqli_error($connection));
205  }
206 
207  // Return stmt for other processing - like retrieving resultset (_fetch)
208  return $stmt;
209  // mysqli_stmt_close($stmt);
210  }
211  }
212  // Run the query statement
213  $result = mysqli_query($connection, $query);
214  // Error Check
215  $error = mysqli_error($connection);
216  if($error)
217  {
218  $this->setError(mysqli_errno($connection), $error);
219  }
220  // Return result
221  return $result;
222  }
223 
231  function _prepareQueryParameters(&$types, &$params)
232  {
233  $types = '';
234  $params = array();
235  if(!$this->param)
236  {
237  return;
238  }
239 
240  foreach($this->param as $k => $o)
241  {
242  $value = $o->getUnescapedValue();
243  $type = $o->getType();
244 
245  // Skip column names -> this should be concatenated to query string
246  if($o->isColumnName())
247  {
248  continue;
249  }
250 
251  switch($type)
252  {
253  case 'number' :
254  $type = 'i';
255  break;
256  case 'varchar' :
257  $type = 's';
258  break;
259  default:
260  $type = 's';
261  }
262 
263  if(is_array($value))
264  {
265  foreach($value as $v)
266  {
267  $params[] = $v;
268  $types .= $type;
269  }
270  }
271  else
272  {
273  $params[] = $value;
274  $types .= $type;
275  }
276  }
277  }
278 
285  function _fetch($result, $arrayIndexEndValue = NULL)
286  {
287  if($this->use_prepared_statements != 'Y')
288  {
289  return parent::_fetch($result, $arrayIndexEndValue);
290  }
291  $output = array();
292  if(!$this->isConnected() || $this->isError() || !$result)
293  {
294  return $output;
295  }
296 
297  // Prepared stements: bind result variable and fetch data
298  $stmt = $result;
299  $meta = mysqli_stmt_result_metadata($stmt);
300  $fields = mysqli_fetch_fields($meta);
301 
307  $longtext_exists = false;
308  foreach($fields as $field)
309  {
310  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
311  {
312  $field->name = 'repeat_' . $field->name;
313  }
314 
315  // Array passed needs to contain references, not values
316  $row[$field->name] = "";
317  $resultArray[$field->name] = &$row[$field->name];
318 
319  if($field->type == 252)
320  {
321  $longtext_exists = true;
322  }
323  }
324  $resultArray = array_merge(array($stmt), $resultArray);
325 
326  if($longtext_exists)
327  {
328  mysqli_stmt_store_result($stmt);
329  }
330 
331  call_user_func_array('mysqli_stmt_bind_result', $resultArray);
332 
333  $rows = array();
334  while(mysqli_stmt_fetch($stmt))
335  {
336  $resultObject = new stdClass();
337 
338  foreach($resultArray as $key => $value)
339  {
340  if($key === 0)
341  {
342  continue; // Skip stmt object
343  }
344  if(strpos($key, 'repeat_'))
345  {
346  $key = substr($key, 6);
347  }
348  $resultObject->$key = $value;
349  }
350 
351  $rows[] = $resultObject;
352  }
353 
354  mysqli_stmt_close($stmt);
355 
356  if($arrayIndexEndValue)
357  {
358  foreach($rows as $row)
359  {
360  $output[$arrayIndexEndValue--] = $row;
361  }
362  }
363  else
364  {
365  $output = $rows;
366  }
367 
368  if(count($output) == 1)
369  {
370  if(isset($arrayIndexEndValue))
371  {
372  return $output;
373  }
374  else
375  {
376  return $output[0];
377  }
378  }
379 
380  return $output;
381  }
382 
389  function _executeInsertAct($queryObject, $with_values = false)
390  {
391  if($this->use_prepared_statements != 'Y')
392  {
393  return parent::_executeInsertAct($queryObject);
394  }
395  $this->param = $queryObject->getArguments();
396  $result = parent::_executeInsertAct($queryObject, $with_values);
397  unset($this->param);
398  return $result;
399  }
400 
407  function _executeUpdateAct($queryObject, $with_values = false)
408  {
409  if($this->use_prepared_statements != 'Y')
410  {
411  return parent::_executeUpdateAct($queryObject);
412  }
413  $this->param = $queryObject->getArguments();
414  $result = parent::_executeUpdateAct($queryObject, $with_values);
415  unset($this->param);
416  return $result;
417  }
418 
425  function _executeDeleteAct($queryObject, $with_values = false)
426  {
427  if($this->use_prepared_statements != 'Y')
428  {
429  return parent::_executeDeleteAct($queryObject);
430  }
431  $this->param = $queryObject->getArguments();
432  $result = parent::_executeDeleteAct($queryObject, $with_values);
433  unset($this->param);
434  return $result;
435  }
436 
446  function _executeSelectAct($queryObject, $connection = null, $with_values = false)
447  {
448  if($this->use_prepared_statements != 'Y')
449  {
450  return parent::_executeSelectAct($queryObject, $connection);
451  }
452  $this->param = $queryObject->getArguments();
453  $result = parent::_executeSelectAct($queryObject, $connection, $with_values);
454  unset($this->param);
455  return $result;
456  }
457 
464  function db_insert_id()
465  {
466  $connection = $this->_getConnection('master');
467  return mysqli_insert_id($connection);
468  }
469 
476  {
477  return mysqli_fetch_object($result);
478  }
479 
486  {
487  return mysqli_free_result($result);
488  }
489 
499  function _createTable($xml_doc)
500  {
501  // xml parsing
502  $oXml = new XmlParser();
503  $xml_obj = $oXml->parse($xml_doc);
504  // Create a table schema
505  $table_name = $xml_obj->table->attrs->name;
506  if($this->isTableExists($table_name))
507  {
508  return;
509  }
510  $table_name = $this->prefix . $table_name;
511 
512  if(!is_array($xml_obj->table->column))
513  {
514  $columns[] = $xml_obj->table->column;
515  }
516  else
517  {
518  $columns = $xml_obj->table->column;
519  }
520 
521  foreach($columns as $column)
522  {
523  $name = $column->attrs->name;
524  $type = $column->attrs->type;
525  $size = $column->attrs->size;
526  $notnull = $column->attrs->notnull;
527  $primary_key = $column->attrs->primary_key;
528  $index = $column->attrs->index;
529  $unique = $column->attrs->unique;
530  $default = $column->attrs->default;
531  $auto_increment = $column->attrs->auto_increment;
532 
533  $column_schema[] = sprintf('`%s` %s%s %s %s %s', $name, $this->column_type[$type], $size ? '(' . $size . ')' : '', isset($default) ? "default '" . $default . "'" : '', $notnull ? 'not null' : '', $auto_increment ? 'auto_increment' : '');
534 
535  if($primary_key)
536  {
537  $primary_list[] = $name;
538  }
539  else if($unique)
540  {
541  $unique_list[$unique][] = $name;
542  }
543  else if($index)
544  {
545  $index_list[$index][] = $name;
546  }
547  }
548 
549  if(count($primary_list))
550  {
551  $column_schema[] = sprintf("primary key (%s)", '`' . implode($primary_list, '`,`') . '`');
552  }
553 
554  if(count($unique_list))
555  {
556  foreach($unique_list as $key => $val)
557  {
558  $column_schema[] = sprintf("unique %s (%s)", $key, '`' . implode($val, '`,`') . '`');
559  }
560  }
561 
562  if(count($index_list))
563  {
564  foreach($index_list as $key => $val)
565  {
566  $column_schema[] = sprintf("index %s (%s)", $key, '`' . implode($val, '`,`') . '`');
567  }
568  }
569 
570  $schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"), "ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci");
571 
572  $output = $this->_query($schema);
573  if(!$output)
574  {
575  return false;
576  }
577  }
578 }
579 
580 DBMysqli_innodb::$isSupported = function_exists('mysqli_connect');
581 
582 /* End of file DBMysqli.class.php */
583 /* Location: ./classes/db/DBMysqli.class.php */
setQueryLog($log)
Definition: DB.class.php:465
setError($errno=0, $errstr= 'success')
Definition: DB.class.php:476
_executeSelectAct($queryObject, $connection=null, $with_values=false)
static $isSupported
Definition: DB.class.php:19
$output
Definition: ko.install.php:193
__construct($auto_connect=TRUE)
_begin($transactionLevel=0)
isConnected($type= 'master', $indx=0)
Definition: DB.class.php:360
_executeInsertAct($queryObject, $with_values=false)
_prepareQueryParameters(&$types, &$params)
$query
Definition: DB.class.php:84
$args
Definition: ko.install.php:185
isError()
Definition: DB.class.php:486
_rollback($transactionLevel=0)
_executeUpdateAct($queryObject, $with_values=false)
_connect($type= 'master', $indx=0)
Definition: DB.class.php:1266
_getConnection($type= 'master', $indx=NULL)
Definition: DB.class.php:1001
_query($query, $connection=NULL)
Definition: DB.class.php:1193
$result
Definition: DB.class.php:66
_executeDeleteAct($queryObject, $with_values=false)
isTableExists($target_name)
_fetch($result, $arrayIndexEndValue=NULL)
_setDBInfo()
Definition: DB.class.php:1216
__query($query, $connection)
$connection
Definition: DB.class.php:85