/** * Get enum/set possible values in field of database * * @param string $table_name Table name * @param string $field_name Field name * @param bool $get_with_lang_vars Getting with lang vars * @param string $lang_code Lang code * @param string $lang_prefix Lang vars prefix * @return array List of elements */ function db_get_list_elements($table_name, $field_name, $get_with_lang_vars = false, $lang_code = CART_LANGUAGE, $lang_prefix = '') { $elements = Database::getListElements($table_name, $field_name); if ($elements && $get_with_lang_vars) { $lang_elements = array(); foreach ($elements as $element) { $lang_elements[$element] = __($lang_prefix . $element, array(), $lang_code); } return $lang_elements; } return $elements; }
/** * Get promotion data * * @param int $promotion_id promotion ID * @param string $lang_code code language * @return array promotion data */ function fn_get_promotion_data($promotion_id, $lang_code = DESCR_SL) { $extra_condition = ''; if (fn_allowed_for('ULTIMATE:FREE')) { $extra_condition = Database::quote(' AND p.zone = ?s', 'catalog'); } $promotion_data = db_get_row("SELECT * FROM ?:promotions as p LEFT JOIN ?:promotion_descriptions as d ON p.promotion_id = d.promotion_id AND d.lang_code = ?s WHERE p.promotion_id = ?i ?p", $lang_code, $promotion_id, $extra_condition); if (!empty($promotion_data)) { $promotion_data['conditions'] = !empty($promotion_data['conditions']) ? unserialize($promotion_data['conditions']) : array(); $promotion_data['bonuses'] = !empty($promotion_data['bonuses']) ? unserialize($promotion_data['bonuses']) : array(); if (!empty($promotion_data['conditions']['conditions'])) { foreach ($promotion_data['conditions']['conditions'] as $key => $condition) { if (!empty($condition['condition']) && $condition['condition'] == 'feature') { $condition['value_name'] = fn_get_product_feature_variant($condition['value']); $promotion_data['conditions']['conditions'][$key]['value_name'] = $condition['value_name']['variant']; } } } } return $promotion_data; }
$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'); }
/** * Connects to database * * @param string $host Database host * @param string $name Database name * @param string $user Database user * @param string $password Database password * @param string $table_prefix Database table prefix * @param string $names Database names for SET NAMES * @return bool Tue on succes connection or already connected, false otherwise */ public function connectToDB($host, $name, $user, $password, $table_prefix, $database_backend, $names = 'utf8') { $connected = true; if ($this->_db_connection == null) { Registry::set('config.table_prefix', $table_prefix); Registry::set('config.database_backend', $database_backend); $connected = Database::connect($user, $password, $host, '', array('table_prefix' => $table_prefix)); if (!$connected) { $this->setNotification('E', '', $this->t('could_not_connect_to_database'), true, 'server_configuration'); } elseif (!Database::changeDb($name)) { // CREATE TABLE SQL command will throw the Fatal error if user does not have the CREATE permissions Registry::set('runtime.database.skip_errors', true); if (!Database::createDb($name)) { $this->setNotification('E', '', $this->t('could_not_create_database'), true, 'server_configuration'); $connected = false; } else { Database::changeDb($name); } Registry::set('runtime.database.skip_errors', false); } if ($connected) { db_query("SET NAMES ?s", $names); db_query("SET sql_mode = ''"); } $this->_db_connection = $connected; } return $connected; }
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; }
/** * 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; }