Example #1
0
 public function __construct($config)
 {
     Db::$raw = true;
     if (!Db::getField("SHOW TABLES LIKE '?:cache'")) {
         Registry::set('runtime.database.skip_errors', true);
         Db::$raw = true;
         $res = Db::query('CREATE TABLE ?:cache (name varchar(255), company_id int(11) unsigned not null default \'0\', data mediumtext, expiry int, tags varchar(255), PRIMARY KEY(name, company_id), KEY (tags), KEY (name, company_id, expiry), KEY (company_id)) Engine=MyISAM DEFAULT CHARSET UTF8');
         Registry::set('runtime.database.skip_errors', false);
         if ($res == false) {
             throw new DatabaseException('Database cache data storage is not supported. Please choose another one.');
         }
     }
     parent::__construct($config);
     return true;
 }
Example #2
0
 $query_time = $start_time = 0;
 if (!empty($_REQUEST['sandbox'])) {
     db_query('SET AUTOCOMMIT=0');
     db_query('START TRANSACTION');
 }
 $stop_queries = array('DROP', 'CREATE', 'TRANSACTION', 'ROLLBACK');
 $stop_exec = false;
 foreach ($stop_queries as $stop_query) {
     if (stripos(trim($query), $stop_query) !== false) {
         $result = false;
         $stop_exec = true;
         break;
     }
 }
 if (!$stop_exec) {
     Database::$raw = true;
     $time_start = microtime(true);
     if (stripos(trim($query), 'SELECT') !== false) {
         $result = db_get_array($query);
         $result_columns = !empty($result[0]) ? array_keys($result[0]) : array();
     } else {
         $result = db_query($query);
     }
     $query_time = microtime(true) - $time_start;
 }
 if (strpos($query, 'SELECT') === 0) {
     $explain = db_get_array('EXPLAIN ' . $query);
 }
 if (!empty($_REQUEST['sandbox'])) {
     db_query('ROLLBACK');
 }
Example #3
0
 /**
  * Execute query
  *
  * @param string $query unparsed query
  * @param mixed ... unlimited number of variables for placeholders
  * @return mixed result set for "SELECT" statement / generated ID for an AUTO_INCREMENT field for insert statement / Affected rows count for DELETE/UPDATE statements
  */
 public function query($query)
 {
     $this->raw = $this->raw ?: Database::$raw;
     // Backward compatibility
     if (!$this->raw) {
         fn_set_hook('db_query', $query);
     }
     $args = func_get_args();
     $query = $this->process($query, array_slice($args, 1), true);
     $result = false;
     if (!empty($query)) {
         if (!$this->raw) {
             fn_set_hook('db_query_process', $query);
         }
         if (defined('DEBUG_QUERIES')) {
             fn_print_r($query);
         }
         $time_start = microtime(true);
         $result = $this->db->query($query);
         if (!$this->error($result, $query)) {
             $insert_id = $this->db->insertId();
             Debugger::set_query($query, microtime(true) - $time_start);
             if (!$this->raw) {
                 fn_set_hook('db_query_executed', $query, $result);
             }
             // "true" will be returned for Update/Delete/Insert/Replace statements. "SELECT" returns MySQLi/PDO object
             if ($result === true) {
                 $cmd = substr($query, 0, 6);
                 // Check if it was insert statement with auto_increment value and return it
                 if (!empty($insert_id)) {
                     $result = $insert_id;
                 } elseif ($cmd == 'UPDATE' || $cmd == 'DELETE' || $cmd == 'INSERT') {
                     $result = $this->db->affectedRows($result);
                 }
                 // Check if query updated data in the database and run cache handlers
                 if (!empty($result) && preg_match("/^(UPDATE|INSERT INTO|REPLACE INTO|DELETE FROM) " . $this->table_prefix . "(\\w+) /", $query, $m)) {
                     Registry::setChangedTables($m[2]);
                 }
                 // Clear table fields cache if table structure was changed
                 if (!empty($result) && preg_match("/^(ALTER( IGNORE)? TABLE) " . $this->table_prefix . "(\\w+) /", $query, $m)) {
                     $this->clearTableFieldsCache($m[3]);
                 }
             }
         } else {
             // Lost connection, try to reconnect
             if ($this->tryReconnect()) {
                 return $this->query($query);
                 // Assume that the table is broken
                 // Try to repair
             } elseif (preg_match("/'(\\S+)\\.(MYI|MYD)/", $this->db->error(), $matches)) {
                 $this->db->query("REPAIR TABLE {$matches['1']}");
                 return $this->query($query);
             }
         }
     }
     $this->raw = false;
     Database::$raw = false;
     // Backward compatibility
     return $result;
 }