XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
DB.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
16 class DB
17 {
18 
19  static $isSupported = FALSE;
20 
25  var $priority_dbms = array(
26  'mysqli' => 6,
27  'mysqli_innodb' => 5,
28  'mysql' => 4,
29  'mysql_innodb' => 3,
30  'cubrid' => 2,
31  'mssql' => 1
32  );
33 
38  var $count_cache_path = 'files/cache/db';
39 
44  var $cond_operation = array(
45  'equal' => '=',
46  'more' => '>=',
47  'excess' => '>',
48  'less' => '<=',
49  'below' => '<',
50  'notequal' => '<>',
51  'notnull' => 'is not null',
52  'null' => 'is null',
53  );
54 
59  var $master_db = NULL;
60 
65  var $slave_db = NULL;
66  var $result = NULL;
67 
72  var $errno = 0;
73 
78  var $errstr = '';
79 
84  var $query = '';
85  var $connection = '';
86 
91  var $elapsed_time = 0;
92 
98 
103  var $transaction_started = FALSE;
104  var $is_connected = FALSE;
105 
111  static $supported_list = array();
112 
117  var $cache_file = 'files/cache/queries/';
118 
123  var $db_type;
124 
130 
135  private $transactionNestedLevel = 0;
136 
142  function getInstance($db_type = NULL)
143  {
144  if(!$db_type)
145  {
147  }
149  {
150  return new BaseObject(-1, 'msg_db_not_setted');
151  }
152 
153  if(!isset($GLOBALS['__DB__']))
154  {
155  $GLOBALS['__DB__'] = array();
156  }
157  if(!isset($GLOBALS['__DB__'][$db_type]))
158  {
159  $class_name = 'DB' . ucfirst($db_type);
160  $class_file = _XE_PATH_ . "classes/db/$class_name.class.php";
161  if(!file_exists($class_file))
162  {
163  return new BaseObject(-1, 'msg_db_not_setted');
164  }
165 
166  // get a singletone instance of the database driver class
167  require_once($class_file);
168  $GLOBALS['__DB__'][$db_type] = call_user_func(array($class_name, 'create'));
169  $GLOBALS['__DB__'][$db_type]->db_type = $db_type;
170  }
171 
172  return $GLOBALS['__DB__'][$db_type];
173  }
174 
179  function create()
180  {
181  return new DB;
182  }
183 
188  function __construct()
189  {
190  $this->count_cache_path = _XE_PATH_ . $this->count_cache_path;
191  $this->cache_file = _XE_PATH_ . $this->cache_file;
192  }
193 
200  function getSupportedList()
201  {
202  $oDB = new DB();
203  return $oDB->_getSupportedList();
204  }
205 
211  public static function getEnableList()
212  {
213  if(!self::$supported_list)
214  {
215  $oDB = new DB();
216  self::$supported_list = $oDB->_getSupportedList();
217  }
218 
219  $enableList = array();
220  if(is_array(self::$supported_list))
221  {
222  foreach(self::$supported_list AS $key => $value)
223  {
224  if($value->enable)
225  {
226  $enableList[] = $value;
227  }
228  }
229  }
230  return $enableList;
231  }
232 
238  public static function getDisableList()
239  {
240  if(!self::$supported_list)
241  {
242  $oDB = new DB();
243  self::$supported_list = $oDB->_getSupportedList();
244  }
245 
246  $disableList = array();
247  if(is_array(self::$supported_list))
248  {
249  foreach(self::$supported_list AS $key => $value)
250  {
251  if(!$value->enable)
252  {
253  $disableList[] = $value;
254  }
255  }
256  }
257  return $disableList;
258  }
259 
265  function _getSupportedList()
266  {
267  static $get_supported_list = '';
268  if(is_array($get_supported_list))
269  {
270  self::$supported_list = $get_supported_list;
271  return self::$supported_list;
272  }
273  $get_supported_list = array();
274  $db_classes_path = _XE_PATH_ . "classes/db/";
275  $filter = "/^DB([^\.]+)\.class\.php/i";
276  $supported_list = FileHandler::readDir($db_classes_path, $filter, TRUE);
277 
278  // after creating instance of class, check is supported
279  for($i = 0; $i < count($supported_list); $i++)
280  {
282 
283  $class_name = sprintf("DB%s%s", strtoupper(substr($db_type, 0, 1)), strtolower(substr($db_type, 1)));
284  $class_file = sprintf(_XE_PATH_ . "classes/db/%s.class.php", $class_name);
285  if(!file_exists($class_file))
286  {
287  continue;
288  }
289 
290  unset($oDB);
291  require_once($class_file);
292  $oDB = new $class_name(FALSE);
293 
294  if(!$oDB)
295  {
296  continue;
297  }
298 
299  $obj = new stdClass;
300  $obj->db_type = $db_type;
301  $obj->enable = $oDB->isSupported() ? TRUE : FALSE;
302 
303  $get_supported_list[] = $obj;
304  }
305 
306  // sort
307  @usort($get_supported_list, array($this, '_sortDBMS'));
308 
309  self::$supported_list = $get_supported_list;
310  return self::$supported_list;
311  }
312 
316  function _sortDBMS($a, $b)
317  {
318  if(!isset($this->priority_dbms[$a->db_type]))
319  {
320  $priority_a = 0;
321  }
322  else
323  {
324  $priority_a = $this->priority_dbms[$a->db_type];
325  }
326 
327  if(!isset($this->priority_dbms[$b->db_type]))
328  {
329  $priority_b = 0;
330  }
331  else
332  {
333  $priority_b = $this->priority_dbms[$b->db_type];
334  }
335 
336  if($priority_a == $priority_b)
337  {
338  return 0;
339  }
340 
341  return ($priority_a > $priority_b) ? -1 : 1;
342  }
343 
349  function isSupported()
350  {
351  return self::$isSupported;
352  }
353 
360  function isConnected($type = 'master', $indx = 0)
361  {
362  if($type == 'master')
363  {
364  return $this->master_db["is_connected"] ? TRUE : FALSE;
365  }
366  else
367  {
368  return $this->slave_db[$indx]["is_connected"] ? TRUE : FALSE;
369  }
370  }
371 
377  function actStart($query)
378  {
379  $this->setError(0, 'success');
380  $this->query = $query;
381  $this->act_start = getMicroTime();
382  $this->elapsed_time = 0;
383  }
384 
389  function actFinish()
390  {
391  if(!$this->query)
392  {
393  return;
394  }
395  $this->act_finish = getMicroTime();
396  $elapsed_time = $this->act_finish - $this->act_start;
397  $this->elapsed_time = $elapsed_time;
398  $GLOBALS['__db_elapsed_time__'] += $elapsed_time;
399 
400  $site_module_info = Context::get('site_module_info');
401  $log = array();
402  $log['query'] = $this->query;
403  $log['elapsed_time'] = $elapsed_time;
404  $log['connection'] = $this->connection;
405  $log['query_id'] = $this->query_id;
406  $log['module'] = $site_module_info->module;
407  $log['act'] = Context::get('act');
408  $log['time'] = date('Y-m-d H:i:s');
409 
410  $bt = version_compare(PHP_VERSION, '5.3.6', '>=') ? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) : debug_backtrace();
411 
412  foreach($bt as $no => $call)
413  {
414  if($call['function'] == 'executeQuery' || $call['function'] == 'executeQueryArray')
415  {
416  $call_no = $no;
417  $call_no++;
418  $log['called_file'] = $bt[$call_no]['file'].':'.$bt[$call_no]['line'];
419  $log['called_file'] = str_replace(_XE_PATH_ , '', $log['called_file']);
420  $call_no++;
421  $log['called_method'] = $bt[$call_no]['class'].$bt[$call_no]['type'].$bt[$call_no]['function'];
422  break;
423  }
424  }
425 
426  // leave error log if an error occured (if __DEBUG_DB_OUTPUT__ is defined)
427  if($this->isError())
428  {
429  $log['result'] = 'Failed';
430  $log['errno'] = $this->errno;
431  $log['errstr'] = $this->errstr;
432 
433  if(__DEBUG_DB_OUTPUT__ == 1)
434  {
435  $debug_file = _XE_PATH_ . "files/_debug_db_query.php";
436  $buff = array();
437  if(!file_exists($debug_file))
438  {
439  $buff[] = '<?php exit(); ?' . '>';
440  }
441  $buff[] = print_r($log, TRUE);
442  @file_put_contents($debug_file, implode("\n", $buff) . "\n\n", FILE_APPEND|LOCK_EX);
443  }
444  }
445  else
446  {
447  $log['result'] = 'Success';
448  }
449 
450  $this->setQueryLog($log);
451 
452  $log_args = new stdClass;
453  $log_args->query = $this->query;
454  $log_args->query_id = $this->query_id;
455  $log_args->caller = $log['called_method'] . '() in ' . $log['called_file'];
456  $log_args->connection = $log['connection'];
457  writeSlowlog('query', $elapsed_time, $log_args);
458  }
459 
465  function setQueryLog($log)
466  {
467  $GLOBALS['__db_queries__'][] = $log;
468  }
469 
476  function setError($errno = 0, $errstr = 'success')
477  {
478  $this->errno = $errno;
479  $this->errstr = $errstr;
480  }
481 
486  function isError()
487  {
488  return ($this->errno !== 0);
489  }
490 
495  function getError()
496  {
497  $this->errstr = Context::convertEncodingStr($this->errstr);
498  return new BaseObject($this->errno, $this->errstr);
499  }
500 
509  function executeQuery($query_id, $args = NULL, $arg_columns = NULL, $type = NULL)
510  {
511  static $cache_file = array();
512 
513  if(!$query_id)
514  {
515  return new BaseObject(-1, 'msg_invalid_queryid');
516  }
517  if(!$this->db_type)
518  {
519  return;
520  }
521 
522  $this->actDBClassStart();
523 
524  $this->query_id = $query_id;
525 
526  if(!isset($cache_file[$query_id]) || !file_exists($cache_file[$query_id]))
527  {
528  $id_args = explode('.', $query_id);
529  if(count($id_args) == 2)
530  {
531  $target = 'modules';
532  $module = $id_args[0];
533  $id = $id_args[1];
534  }
535  elseif(count($id_args) == 3)
536  {
537  $target = $id_args[0];
538  $typeList = array('addons' => 1, 'widgets' => 1);
539  if(!isset($typeList[$target]))
540  {
541  $this->actDBClassFinish();
542  return;
543  }
544  $module = $id_args[1];
545  $id = $id_args[2];
546  }
547  if(!$target || !$module || !$id)
548  {
549  $this->actDBClassFinish();
550  return new BaseObject(-1, 'msg_invalid_queryid');
551  }
552 
553  $xml_file = sprintf('%s%s/%s/queries/%s.xml', _XE_PATH_, $target, $module, $id);
554  if(!file_exists($xml_file))
555  {
556  $this->actDBClassFinish();
557  return new BaseObject(-1, 'msg_invalid_queryid');
558  }
559 
560  // look for cache file
561  $cache_file[$query_id] = $this->checkQueryCacheFile($query_id, $xml_file);
562  }
563  $result = $this->_executeQuery($cache_file[$query_id], $args, $query_id, $arg_columns, $type);
564 
565  $this->actDBClassFinish();
566  // execute query
567  return $result;
568  }
569 
576  function checkQueryCacheFile($query_id, $xml_file)
577  {
578  // first try finding cache file
579  $cache_file = sprintf('%s%s%s.%s.%s.cache.php', _XE_PATH_, $this->cache_file, $query_id, __ZBXE_VERSION__, $this->db_type);
580 
581  $cache_time = -1;
582  if(file_exists($cache_file))
583  {
584  $cache_time = filemtime($cache_file);
585  }
586 
587  // if there is no cache file or is not new, find original xml query file and parse it
588  if($cache_time < filemtime($xml_file) || $cache_time < filemtime(_XE_PATH_ . 'classes/db/DB.class.php') || $cache_time < filemtime(_XE_PATH_ . 'classes/xml/XmlQueryParser.class.php'))
589  {
590  $oParser = new XmlQueryParser();
591  $oParser->parse($query_id, $xml_file, $cache_file);
592  }
593 
594  return $cache_file;
595  }
596 
605  function _executeQuery($cache_file, $source_args, $query_id, $arg_columns, $type)
606  {
607  global $lang;
608 
609  if(!in_array($type, array('master','slave'))) $type = 'slave';
610 
611  if(!file_exists($cache_file))
612  {
613  return new BaseObject(-1, 'msg_invalid_queryid');
614  }
615 
616  if($source_args)
617  {
618  $args = clone $source_args;
619  }
620 
621  $output = include($cache_file);
622 
623  if((is_a($output, 'BaseObject') || is_subclass_of($output, 'BaseObject')) && !$output->toBool())
624  {
625  return $output;
626  }
627 
628  // execute appropriate query
629  switch($output->getAction())
630  {
631  case 'insert' :
632  case 'insert-select' :
633  $this->resetCountCache($output->tables);
634  $output = $this->_executeInsertAct($output);
635  break;
636  case 'update' :
637  $this->resetCountCache($output->tables);
638  $output = $this->_executeUpdateAct($output);
639  break;
640  case 'delete' :
641  $this->resetCountCache($output->tables);
642  $output = $this->_executeDeleteAct($output);
643  break;
644  case 'select' :
645  $arg_columns = is_array($arg_columns) ? $arg_columns : array();
646  $output->setColumnList($arg_columns);
647  $connection = $this->_getConnection($type);
648  $output = $this->_executeSelectAct($output, $connection);
649  break;
650  }
651 
652  if($this->isError())
653  {
654  $output = $this->getError();
655  }
656  else if(!is_a($output, 'BaseObject') && !is_subclass_of($output, 'BaseObject'))
657  {
658  $output = new BaseObject();
659  }
660  $output->add('_query', $this->query);
661  $output->add('_elapsed_time', sprintf("%0.5f", $this->elapsed_time));
662 
663  return $output;
664  }
665 
672  function getCountCache($tables, $condition)
673  {
674  return FALSE;
675 /*
676  if(!$tables)
677  {
678  return FALSE;
679  }
680  if(!is_dir($this->count_cache_path))
681  {
682  return FileHandler::makeDir($this->count_cache_path);
683  }
684 
685  $condition = md5($condition);
686 
687  if(!is_array($tables))
688  {
689  $tables_str = $tables;
690  }
691  else
692  {
693  $tables_str = implode('.', $tables);
694  }
695 
696  $cache_path = sprintf('%s/%s%s', $this->count_cache_path, $this->prefix, $tables_str);
697  FileHandler::makeDir($cache_path);
698 
699  $cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition);
700  if(!file_exists($cache_filename))
701  {
702  return FALSE;
703  }
704 
705  $cache_mtime = filemtime($cache_filename);
706 
707  if(!is_array($tables))
708  {
709  $tables = array($tables);
710  }
711  foreach($tables as $alias => $table)
712  {
713  $table_filename = sprintf('%s/cache.%s%s', $this->count_cache_path, $this->prefix, $table);
714  if(!file_exists($table_filename) || filemtime($table_filename) > $cache_mtime)
715  {
716  return FALSE;
717  }
718  }
719 
720  $count = (int) FileHandler::readFile($cache_filename);
721  return $count;
722 */
723  }
724 
732  function putCountCache($tables, $condition, $count = 0)
733  {
734  return FALSE;
735 /*
736  if(!$tables)
737  {
738  return FALSE;
739  }
740  if(!is_dir($this->count_cache_path))
741  {
742  return FileHandler::makeDir($this->count_cache_path);
743  }
744 
745  $condition = md5($condition);
746 
747  if(!is_array($tables))
748  {
749  $tables_str = $tables;
750  }
751  else
752  {
753  $tables_str = implode('.', $tables);
754  }
755 
756  $cache_path = sprintf('%s/%s%s', $this->count_cache_path, $this->prefix, $tables_str);
757  FileHandler::makeDir($cache_path);
758 
759  $cache_filename = sprintf('%s/%s.%s', $cache_path, $tables_str, $condition);
760 
761  FileHandler::writeFile($cache_filename, $count);
762 */
763  }
764 
770  function resetCountCache($tables)
771  {
772  return FALSE;
773 /*
774  if(!$tables)
775  {
776  return FALSE;
777  }
778  return FileHandler::makeDir($this->count_cache_path);
779 
780  if(!is_array($tables))
781  {
782  $tables = array($tables);
783  }
784  foreach($tables as $alias => $table)
785  {
786  $filename = sprintf('%s/cache.%s%s', $this->count_cache_path, $this->prefix, $table);
787  FileHandler::removeFile($filename);
788  FileHandler::writeFile($filename, '');
789  }
790 
791  return TRUE;
792  */
793  }
794 
800  function dropTable($table_name)
801  {
802  if(!$table_name)
803  {
804  return;
805  }
806  $query = sprintf("drop table %s%s", $this->prefix, $table_name);
807  $this->_query($query);
808  }
809 
816  function getSelectSql($query, $with_values = TRUE)
817  {
818  $select = $query->getSelectString($with_values);
819  if($select == '')
820  {
821  return new BaseObject(-1, "Invalid query");
822  }
823  $select = 'SELECT ' . $select;
824 
825  $from = $query->getFromString($with_values);
826  if($from == '')
827  {
828  return new BaseObject(-1, "Invalid query");
829  }
830  $from = ' FROM ' . $from;
831 
832  $where = $query->getWhereString($with_values);
833  if($where != '')
834  {
835  $where = ' WHERE ' . $where;
836  }
837 
838  $tableObjects = $query->getTables();
839  $index_hint_list = '';
840  foreach($tableObjects as $tableObject)
841  {
842  if(is_a($tableObject, 'CubridTableWithHint'))
843  {
844  $index_hint_list .= $tableObject->getIndexHintString() . ', ';
845  }
846  }
847  $index_hint_list = substr($index_hint_list, 0, -2);
848  if($index_hint_list != '')
849  {
850  $index_hint_list = 'USING INDEX ' . $index_hint_list;
851  }
852 
853  $groupBy = $query->getGroupByString();
854  if($groupBy != '')
855  {
856  $groupBy = ' GROUP BY ' . $groupBy;
857  }
858 
859  $orderBy = $query->getOrderByString();
860  if($orderBy != '')
861  {
862  $orderBy = ' ORDER BY ' . $orderBy;
863  }
864 
865  $limit = $query->getLimitString();
866  if($limit != '')
867  {
868  $limit = ' LIMIT ' . $limit;
869  }
870 
871  return $select . ' ' . $from . ' ' . $where . ' ' . $index_hint_list . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
872  }
873 
885  function getClickCountQuery($queryObject)
886  {
887  $new_update_columns = array();
888  $click_count_columns = $queryObject->getClickCountColumns();
889  foreach($click_count_columns as $click_count_column)
890  {
891  $click_count_column_name = $click_count_column->column_name;
892 
893  $increase_by_1 = new Argument($click_count_column_name, null);
894  $increase_by_1->setColumnOperation('+');
895  $increase_by_1->ensureDefaultValue(1);
896 
897  $update_expression = new UpdateExpression($click_count_column_name, $increase_by_1);
898  $new_update_columns[] = $update_expression;
899  }
900  $queryObject->columns = $new_update_columns;
901  return $queryObject;
902  }
903 
911  function getDeleteSql($query, $with_values = TRUE, $with_priority = FALSE)
912  {
913  $sql = 'DELETE ';
914 
915  $sql .= $with_priority ? $query->getPriority() : '';
916  $tables = $query->getTables();
917 
918  $sql .= $tables[0]->getAlias();
919 
920  $from = $query->getFromString($with_values);
921  if($from == '')
922  {
923  return new BaseObject(-1, "Invalid query");
924  }
925  $sql .= ' FROM ' . $from;
926 
927  $where = $query->getWhereString($with_values);
928  if($where != '')
929  {
930  $sql .= ' WHERE ' . $where;
931  }
932 
933  return $sql;
934  }
935 
943  function getUpdateSql($query, $with_values = TRUE, $with_priority = FALSE)
944  {
945  $columnsList = $query->getUpdateString($with_values);
946  if($columnsList == '')
947  {
948  return new BaseObject(-1, "Invalid query");
949  }
950 
951  $tables = $query->getFromString($with_values);
952  if($tables == '')
953  {
954  return new BaseObject(-1, "Invalid query");
955  }
956 
957  $where = $query->getWhereString($with_values);
958  if($where != '')
959  {
960  $where = ' WHERE ' . $where;
961  }
962 
963  $priority = $with_priority ? $query->getPriority() : '';
964 
965  return "UPDATE $priority $tables SET $columnsList " . $where;
966  }
967 
975  function getInsertSql($query, $with_values = TRUE, $with_priority = FALSE)
976  {
977  $tableName = $query->getFirstTableName();
978  $values = $query->getInsertString($with_values);
979  $priority = $with_priority ? $query->getPriority() : '';
980 
981  return "INSERT $priority INTO $tableName \n $values";
982  }
983 
989  {
990  $max = count($this->slave_db);
991  $indx = rand(0, $max - 1);
992  return $indx;
993  }
994 
1001  function _getConnection($type = 'master', $indx = NULL)
1002  {
1003  if($type == 'master')
1004  {
1005  if(!$this->master_db['is_connected'])
1006  {
1007  $this->_connect($type);
1008  }
1009  $this->connection = 'Master ' . $this->master_db['db_hostname'];
1010  return $this->master_db["resource"];
1011  }
1012 
1013  if($indx === NULL)
1014  {
1015  $indx = $this->_getSlaveConnectionStringIndex($type);
1016  }
1017 
1018  if(!$this->slave_db[$indx]['is_connected'])
1019  {
1020  $this->_connect($type, $indx);
1021  }
1022 
1023  $this->connection = 'Slave ' . $this->slave_db[$indx]['db_hostname'];
1024  return $this->slave_db[$indx]["resource"];
1025  }
1026 
1031  function _dbInfoExists()
1032  {
1033  if(!$this->master_db)
1034  {
1035  return FALSE;
1036  }
1037  if(count($this->slave_db) === 0)
1038  {
1039  return FALSE;
1040  }
1041  return TRUE;
1042  }
1043 
1051  {
1052 
1053  }
1054 
1061  function close($type = 'master', $indx = 0)
1062  {
1063  if(!$this->isConnected($type, $indx))
1064  {
1065  return;
1066  }
1067 
1068  if($type == 'master')
1069  {
1071  }
1072  else
1073  {
1074  $connection = &$this->slave_db[$indx];
1075  }
1076 
1077  $this->commit();
1078  $this->_close($connection["resource"]);
1079 
1080  $connection["is_connected"] = FALSE;
1081  }
1082 
1088  function _begin($transactionLevel = 0)
1089  {
1090  return TRUE;
1091  }
1092 
1097  function begin()
1098  {
1099  if(!$this->isConnected())
1100  {
1101  return;
1102  }
1103 
1104  if($this->_begin($this->transactionNestedLevel))
1105  {
1106  $this->transaction_started = TRUE;
1107  $this->transactionNestedLevel++;
1108  }
1109  }
1110 
1116  function _rollback($transactionLevel = 0)
1117  {
1118  return TRUE;
1119  }
1120 
1125  function rollback()
1126  {
1127  if(!$this->isConnected() || !$this->transaction_started)
1128  {
1129  return;
1130  }
1131  if($this->_rollback($this->transactionNestedLevel))
1132  {
1133  $this->transactionNestedLevel--;
1134 
1135  if(!$this->transactionNestedLevel)
1136  {
1137  $this->transaction_started = FALSE;
1138  }
1139  }
1140  }
1141 
1147  function _commit()
1148  {
1149  return TRUE;
1150  }
1151 
1157  function commit($force = FALSE)
1158  {
1159  if(!$force && (!$this->isConnected() || !$this->transaction_started))
1160  {
1161  return;
1162  }
1163  if($this->transactionNestedLevel == 1 && $this->_commit())
1164  {
1165  $this->transaction_started = FALSE;
1166  $this->transactionNestedLevel = 0;
1167  }
1168  else
1169  {
1170  $this->transactionNestedLevel--;
1171  }
1172  }
1173 
1182  {
1183 
1184  }
1185 
1193  function _query($query, $connection = NULL)
1194  {
1195  if($connection == NULL)
1196  {
1197  $connection = $this->_getConnection('master');
1198  }
1199  // Notify to start a query execution
1200  $this->actStart($query);
1201 
1202  // Run the query statement
1203  $result = $this->__query($query, $connection);
1204 
1205  // Notify to complete a query execution
1206  $this->actFinish();
1207  // Return result
1208  return $result;
1209  }
1210 
1216  function _setDBInfo()
1217  {
1218  $db_info = Context::getDBInfo();
1219  $this->master_db = $db_info->master_db;
1220  if($db_info->master_db["db_hostname"] == $db_info->slave_db[0]["db_hostname"]
1221  && $db_info->master_db["db_port"] == $db_info->slave_db[0]["db_port"]
1222  && $db_info->master_db["db_userid"] == $db_info->slave_db[0]["db_userid"]
1223  && $db_info->master_db["db_password"] == $db_info->slave_db[0]["db_password"]
1224  && $db_info->master_db["db_database"] == $db_info->slave_db[0]["db_database"]
1225  )
1226  {
1227  $this->slave_db[0] = &$this->master_db;
1228  }
1229  else
1230  {
1231  $this->slave_db = $db_info->slave_db;
1232  }
1233  $this->prefix = $db_info->master_db["db_table_prefix"];
1234  $this->use_prepared_statements = $db_info->use_prepared_statements;
1235  }
1236 
1244  {
1245 
1246  }
1247 
1255  {
1256 
1257  }
1258 
1266  function _connect($type = 'master', $indx = 0)
1267  {
1268  if($this->isConnected($type, $indx))
1269  {
1270  return;
1271  }
1272 
1273  // Ignore if no DB information exists
1274  if(!$this->_dbInfoExists())
1275  {
1276  return;
1277  }
1278 
1279  if($type == 'master')
1280  {
1282  }
1283  else
1284  {
1285  $connection = &$this->slave_db[$indx];
1286  }
1287 
1288  $result = $this->__connect($connection);
1289  if($result === NULL || $result === FALSE)
1290  {
1291  $connection["is_connected"] = FALSE;
1292  return;
1293  }
1294 
1295  // Check connections
1296  $connection["resource"] = $result;
1297  $connection["is_connected"] = TRUE;
1298 
1299  // Save connection info for db logs
1300  $this->connection = ucfirst($type) . ' ' . $connection["db_hostname"];
1301 
1302  // regist $this->close callback
1303  register_shutdown_function(array($this, "close"));
1304 
1305  $this->_afterConnect($result);
1306  }
1307 
1312  function actDBClassStart()
1313  {
1314  $this->setError(0, 'success');
1315  $this->act_dbclass_start = getMicroTime();
1316  $this->elapsed_dbclass_time = 0;
1317  }
1318 
1323  function actDBClassFinish()
1324  {
1325  if(!$this->query)
1326  {
1327  return;
1328  }
1329  $this->act_dbclass_finish = getMicroTime();
1330  $elapsed_dbclass_time = $this->act_dbclass_finish - $this->act_dbclass_start;
1331  $this->elapsed_dbclass_time = $elapsed_dbclass_time;
1332  $GLOBALS['__dbclass_elapsed_time__'] += $elapsed_dbclass_time;
1333  }
1334 
1345  function getParser($force = FALSE)
1346  {
1347  static $dbParser = NULL;
1348  if(!$dbParser || $force)
1349  {
1350  $oDB = DB::getInstance();
1351  $dbParser = $oDB->getParser();
1352  }
1353 
1354  return $dbParser;
1355  }
1356 
1357 }
1358 /* End of file DB.class.php */
1359 /* Location: ./classes/db/DB.class.php */
setQueryLog($log)
Definition: DB.class.php:465
setError($errno=0, $errstr= 'success')
Definition: DB.class.php:476
_getSlaveConnectionStringIndex()
Definition: DB.class.php:988
actStart($query)
Definition: DB.class.php:377
_executeQuery($cache_file, $source_args, $query_id, $arg_columns, $type)
Definition: DB.class.php:605
getUpdateSql($query, $with_values=TRUE, $with_priority=FALSE)
Definition: DB.class.php:943
putCountCache($tables, $condition, $count=0)
Definition: DB.class.php:732
getClickCountQuery($queryObject)
Definition: DB.class.php:885
static getDisableList()
Definition: DB.class.php:238
$obj
Definition: ko.install.php:262
getMicroTime()
Definition: func.inc.php:986
actDBClassStart()
Definition: DB.class.php:1312
static $isSupported
Definition: DB.class.php:19
if(file_exists(_XE_PATH_. 'config/config.user.inc.php')) if(!defined('__DEBUG__')) if(!defined('__DEBUG_OUTPUT__')) if(!defined('__DEBUG_PROTECT__')) if(!defined('__DEBUG_PROTECT_IP__')) if(!defined('__DEBUG_DB_OUTPUT__')) if(!defined('__LOG_SLOW_QUERY__')) if(!defined('__LOG_SLOW_TRIGGER__')) if(!defined('__LOG_SLOW_ADDON__')) if(!defined('__LOG_SLOW_WIDGET__')) if(!defined('__DEBUG_QUERY__')) if(!defined('__OB_GZHANDLER_ENABLE__')) if(!defined('__ENABLE_PHPUNIT_TEST__')) if(!defined('__PROXY_SERVER__')) if(!defined('__ERROR_LOG__')) if(!defined('__DISABLE_DEFAULT_CSS__')) if(!defined('__AUTO_OPCACHE_INVALIDATE__')) if((__DEBUG_OUTPUT__==2)&&version_compare(PHP_VERSION, '6.0.0')===-1) if(version_compare(PHP_VERSION, '5.3.0') >=0) $GLOBALS['__xe_autoload_file_map']
Definition: config.inc.php:324
_getSupportedList()
Definition: DB.class.php:265
$output
Definition: ko.install.php:193
getInsertSql($query, $with_values=TRUE, $with_priority=FALSE)
Definition: DB.class.php:975
close($type= 'master', $indx=0)
Definition: DB.class.php:1061
_close($connection)
Definition: DB.class.php:1050
_dbInfoExists()
Definition: DB.class.php:1031
isConnected($type= 'master', $indx=0)
Definition: DB.class.php:360
checkQueryCacheFile($query_id, $xml_file)
Definition: DB.class.php:576
create()
Definition: DB.class.php:179
$elapsed_dbclass_time
Definition: DB.class.php:97
actDBClassFinish()
Definition: DB.class.php:1323
getError()
Definition: DB.class.php:495
getParser($force=FALSE)
Definition: DB.class.php:1345
getSupportedList()
Definition: DB.class.php:200
getDeleteSql($query, $with_values=TRUE, $with_priority=FALSE)
Definition: DB.class.php:911
$errstr
Definition: DB.class.php:78
$errno
Definition: DB.class.php:72
$query
Definition: DB.class.php:84
Definition: DB.class.php:16
executeQuery($query_id, $args=NULL, $arg_columns=NULL, $type=NULL)
Definition: DB.class.php:509
_afterConnect($connection)
Definition: DB.class.php:1254
$cond_operation
Definition: DB.class.php:44
$args
Definition: ko.install.php:185
static $supported_list
Definition: DB.class.php:111
isError()
Definition: DB.class.php:486
$slave_db
Definition: DB.class.php:65
static getEnableList()
Definition: DB.class.php:211
getInstance($db_type=NULL)
Definition: DB.class.php:142
_commit()
Definition: DB.class.php:1147
actFinish()
Definition: DB.class.php:389
resetCountCache($tables)
Definition: DB.class.php:770
const _XE_PATH_
Definition: config.inc.php:49
__query($query, $connection)
Definition: DB.class.php:1181
$cache_file
Definition: DB.class.php:117
_connect($type= 'master', $indx=0)
Definition: DB.class.php:1266
dropTable($table_name)
Definition: DB.class.php:800
_getConnection($type= 'master', $indx=NULL)
Definition: DB.class.php:1001
getSelectSql($query, $with_values=TRUE)
Definition: DB.class.php:816
_query($query, $connection=NULL)
Definition: DB.class.php:1193
$count_cache_path
Definition: DB.class.php:38
const __ZBXE_VERSION__
Definition: config.inc.php:44
convertEncodingStr($str)
$db_type
Definition: DB.class.php:123
getCountCache($tables, $condition)
Definition: DB.class.php:672
_sortDBMS($a, $b)
Definition: DB.class.php:316
readDir($path, $filter= '', $to_lower=FALSE, $concat_prefix=FALSE)
$priority_dbms
Definition: DB.class.php:25
rollback()
Definition: DB.class.php:1125
begin()
Definition: DB.class.php:1097
__connect($connection)
Definition: DB.class.php:1243
writeSlowlog($type, $elapsed_time, $obj)
Definition: func.inc.php:909
_rollback($transactionLevel=0)
Definition: DB.class.php:1116
$result
Definition: DB.class.php:66
isSupported()
Definition: DB.class.php:349
$is_connected
Definition: DB.class.php:104
$use_prepared_statements
Definition: DB.class.php:129
_setDBInfo()
Definition: DB.class.php:1216
if(isset($_REQUEST['encode'])) if(isset($_REQUEST['decode'])) $lang
Definition: example.php:23
__construct()
Definition: DB.class.php:188
commit($force=FALSE)
Definition: DB.class.php:1157
$connection
Definition: DB.class.php:85
$elapsed_time
Definition: DB.class.php:91
_begin($transactionLevel=0)
Definition: DB.class.php:1088
$master_db
Definition: DB.class.php:59
$transaction_started
Definition: DB.class.php:103