$value['value']}',"; } } $sql = rtrim($sql, ','); if ($where) { $sql .= ' WHERE '.$where; } if ($limit) { $sql .= ' LIMIT '.(int)$limit; } return (bool)$this->q($sql, $use_cache); } /** * Executes a DELETE query * * @param string $table Name of the table to delete * @param string $where WHERE clause on query * @param int $limit Number max of rows to delete * @param bool $use_cache Use cache or not * @param bool $add_prefix Add or not _DB_PREFIX_ before table name * @return bool */ public function delete($table, $where = '', $limit = 0, $use_cache = true, $add_prefix = true) { if ($add_prefix) { $table = _DB_PREFIX_.$table; } $this->result = false; $sql = 'DELETE FROM `'.bqSQL($table).'`'.($where ? ' WHERE '.$where : '').($limit ? ' LIMIT '.(int)$limit : ''); $res = $this->query($sql); if ($use_cache && $this->is_cache_enabled) { Cache::getInstance()->deleteQuery($sql); } return (bool)$res; } /** * Executes a query * * @param string|DbQuery $sql * @param bool $use_cache * @return bool */ public function execute($sql, $use_cache = true) { if ($sql instanceof DbQuery) { $sql = $sql->build(); } $this->result = $this->query($sql); if ($use_cache && $this->is_cache_enabled) { Cache::getInstance()->deleteQuery($sql); } return (bool)$this->result; } /** * Executes return the result of $sql as array * * @param string|DbQuery $sql Query to execute * @param bool $array Return an array instead of a result object (deprecated since 1.5.0.1, use query method instead) * @param bool $use_cache * @return array|false|null|mysqli_result|PDOStatement|resource * @throws PrestaShopDatabaseException */ public function executeS($sql, $array = true, $use_cache = true) { if ($sql instanceof DbQuery) { $sql = $sql->build(); } $this->result = false; $this->last_query = $sql; if ($use_cache && $this->is_cache_enabled && $array) { $this->last_query_hash = Tools::encryptIV($sql); if (($result = Cache::getInstance()->get($this->last_query_hash)) !== false) { $this->last_cached = true; return $result; } } // This method must be used only with queries which display results if (!preg_match('#^\s*\(?\s*(select|show|explain|describe|desc)\s#i', $sql)) { if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_) { throw new PrestaShopDatabaseException('Db->executeS() must be used only with select, show, explain or describe queries'); } return $this->execute($sql, $use_cache); } $this->result = $this->query($sql); if (!$this->result) { $result = false; } else { if (!$array) { $use_cache = false; $result = $this->result; } else { $result = $this->getAll($this->result); } } $this->last_cached = false; if ($use_cache && $this->is_cache_enabled && $array) { Cache::getInstance()->setQuery($sql, $result); } return $result; } /** * Returns an associative array containing the first row of the query * This function automatically adds "LIMIT 1" to the query * * @param string|DbQuery $sql the select query (without "LIMIT 1") * @param bool $use_cache Find it in cache first * @return array|bool|object|null */ public function getRow($sql, $use_cache = true) { if ($sql instanceof DbQuery) { $sql = $sql->build(); } $sql = rtrim($sql, " \t\n\r\0\x0B;").' LIMIT 1'; $this->result = false; $this->last_query = $sql; if ($use_cache && $this->is_cache_enabled) { $this->last_query_hash = Tools::encryptIV($sql); if (($result = Cache::getInstance()->get($this->last_query_hash)) !== false) { $this->last_cached = true; return $result; } } $this->result = $this->query($sql); if (!$this->result) { $result = false; } else { $result = $this->nextRow($this->result); } $this->last_cached = false; if (is_null($result)) { $result = false; } if ($use_cache && $this->is_cache_enabled) { Cache::getInstance()->setQuery($sql, $result); } return $result; } /** * Returns a value from the first row, first column of a SELECT query * * @param string|DbQuery $sql * @param bool $use_cache * @return string|false|null */ public function getValue($sql, $use_cache = true) { if ($sql instanceof DbQuery) { $sql = $sql->build(); } if (!$result = $this->getRow($sql, $use_cache)) { return false; } return array_shift($result); } /** * Get number of rows for last result * * @return int */ public function numRows() { if (!$this->last_cached && $this->result) { $nrows = $this->_numRows($this->result); if ($this->is_cache_enabled) { Cache::getInstance()->set($this->last_query_hash.'_nrows', $nrows); } return $nrows; } elseif ($this->is_cache_enabled && $this->last_cached) { return Cache::getInstance()->get($this->last_query_hash.'_nrows'); } } /** * Executes a query * * @param string|DbQuery $sql * @param bool $use_cache * @return bool|mysqli_result|PDOStatement|resource * @throws PrestaShopDatabaseException */ protected function q($sql, $use_cache = true) { if ($sql instanceof DbQuery) { $sql = $sql->build(); } $this->result = false; $result = $this->query($sql); if ($use_cache && $this->is_cache_enabled) { Cache::getInstance()->deleteQuery($sql); } if (_PS_DEBUG_SQL_) { $this->displayError($sql); } return $result; } /** * Displays last SQL error * * @param string|bool $sql * @throws PrestaShopDatabaseException */ public function displayError($sql = false) { global $webservice_call; $errno = $this->getNumberError(); if ($webservice_call && $errno) { $dbg = debug_backtrace(); WebserviceRequest::getInstance()->setError(500, '[SQL Error] '.$this->getMsgError().'. From '.(isset($dbg[3]['class']) ? $dbg[3]['class'] : '').'->'.$dbg[3]['function'].'() Query was : '.$sql, 97); } elseif (_PS_DEBUG_SQL_ && $errno && !defined('PS_INSTALLATION_IN_PROGRESS')) { if ($sql) { throw new PrestaShopDatabaseException($this->getMsgError().'

'.$sql.'
'); } throw new PrestaShopDatabaseException($this->getMsgError()); } } /** * Sanitize data which will be injected into SQL query * * @param string $string SQL data which will be injected into SQL query * @param bool $html_ok Does data contain HTML code ? (optional) * @return string Sanitized data */ public function escape($string, $html_ok = false, $bq_sql = false) { if (_PS_MAGIC_QUOTES_GPC_) { $string = stripslashes($string); } if (!is_numeric($string)) { $string = $this->_escape($string); if (!$html_ok) { $string = strip_tags(Tools::nl2br($string)); } if ($bq_sql === true) { $string = str_replace('`', '\`', $string); } } return $string; } /** * Try a connection to the database * * @param string $server Server address * @param string $user Login for database connection * @param string $pwd Password for database connection * @param string $db Database name * @param bool $new_db_link * @param string|bool $engine * @param int $timeout * @return int Error code or 0 if connection was successful */ public static function checkConnection($server, $user, $pwd, $db, $new_db_link = true, $engine = null, $timeout = 5) { return call_user_func_array(array(Db::getClass(), 'tryToConnect'), array($server, $user, $pwd, $db, $new_db_link, $engine, $timeout)); } /** * Try a connection to the database and set names to UTF-8 * * @param string $server Server address * @param string $user Login for database connection * @param string $pwd Password for database connection * @return bool */ public static function checkEncoding($server, $user, $pwd) { return call_user_func_array(array(Db::getClass(), 'tryUTF8'), array($server, $user, $pwd)); } /** * Try a connection to the database and check if at least one table with same prefix exists * * @param string $server Server address * @param string $user Login for database connection * @param string $pwd Password for database connection * @param string $db Database name * @param string $prefix Tables prefix * @return bool */ public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix) { return call_user_func_array(array(Db::getClass(), 'hasTableWithSamePrefix'), array($server, $user, $pwd, $db, $prefix)); } /** * Tries to connect to the database and create a table (checking creation privileges) * * @param string $server * @param string $user * @param string $pwd * @param string $db * @param string $prefix * @param string|null $engine Table engine * @return bool|string True, false or error */ public static function checkCreatePrivilege($server, $user, $pwd, $db, $prefix, $engine = null) { return call_user_func_array(array(Db::getClass(), 'checkCreatePrivilege'), array($server, $user, $pwd, $db, $prefix, $engine)); } /** * Checks if auto increment value and offset is 1 * * @param string $server * @param string $user * @param string $pwd * @return bool */ public static function checkAutoIncrement($server, $user, $pwd) { return call_user_func_array(array(Db::getClass(), 'checkAutoIncrement'), array($server, $user, $pwd)); } /** * Executes a query * * @deprecated 1.5.0.1 * @param string|DbQuery $sql * @param bool $use_cache * @return array|bool|mysqli_result|PDOStatement|resource * @throws PrestaShopDatabaseException */ public static function s($sql, $use_cache = true) { Tools::displayAsDeprecated(); return Db::getInstance()->executeS($sql, true, $use_cache); } /** * Executes a query * * @deprecated 1.5.0.1 * @param $sql * @param int $use_cache * @return array|bool|mysqli_result|PDOStatement|resource */ public static function ps($sql, $use_cache = 1) { Tools::displayAsDeprecated(); $ret = Db::s($sql, $use_cache); return $ret; } /** * Executes a query and kills process (dies) * * @deprecated 1.5.0.1 * @param $sql * @param int $use_cache */ public static function ds($sql, $use_cache = 1) { Tools::displayAsDeprecated(); Db::s($sql, $use_cache); die(); } /** * Get used link instance * * @return PDO|mysqli|resource Resource */ public function getLink() { return $this->link; } }