Skip to content

Instantly share code, notes, and snippets.

@Onasusweb
Forked from dmalykh/MySQL.php
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save Onasusweb/e3bba7d547bd7029d589 to your computer and use it in GitHub Desktop.

Select an option

Save Onasusweb/e3bba7d547bd7029d589 to your computer and use it in GitHub Desktop.

Revisions

  1. @dmalykh dmalykh created this gist Mar 30, 2014.
    211 changes: 211 additions & 0 deletions MySQL.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,211 @@
    <?php
    class MySQL extends Db {
    public $logRoute;

    private $_cache_list;
    private $_tables;
    private $_enableFileCache = false;
    private $_not_cached = array(
    "cart"
    );

    private function _addToCache($q, $r) {
    preg_match("/\/(.+?)\.php/", $_SERVER["REQUEST_URI"], $fname);
    if (in_array($fname[1], $this->_not_cached) && in_array($_SERVER["REQUEST_URI"], $this->_not_cached)) {
    $this->_cache_list[md5($q) ] = $r;
    if ($this->_enableFileCache) {
    file_put_contents(_PS_MYSQLCAHE_DIR_ . md5($_SERVER["REQUEST_URI"]) , serialize($this->_cache_list) , LOCK_EX);
    }
    }
    }

    private function _getCache() {
    $filename = md5($_SERVER["REQUEST_URI"]);
    if ($this->_enableFileCache && file_exists(_PS_MYSQLCAHE_DIR_ . $filename) && (filemtime(_PS_MYSQLCAHE_DIR_ . $filename) + 1800) > time())
    return unserialize(file_get_contents(_PS_MYSQLCAHE_DIR_ . $filename));
    else
    return array();
    }

    private function _getFromCache($q) {
    if (isset($this->_cache_list[md5($q)]))
    return $this->_cache_list[md5($q)];
    return false;
    }

    private function _isInCache($q) {
    return (isset($this->_cache_list[md5($q)]));
    }

    public function connect() {
    if (!defined("_PS_DEBUG_SQL_"))
    define("_PS_DEBUG_SQL_", false);

    if ($this->_link = @mysql_connect($this->_server, $this->_user, $this->_password)) {
    if (!$this->set_db($this->_database))
    die(Tools::displayError("The database selection cannot be made."));
    }
    else
    die(Tools::displayError("Link to database cannot be established."));

    /* UTF-8 support */
    if (!mysql_query("SET NAMES \"utf8\"", $this->_link))
    die(Tools::displayError("PrestaShop Fatal error: no utf-8 support. Please check your server configuration."));

    /* Disable some MySQL limitations */
    mysql_query("SET GLOBAL SQL_MODE=\"\"", $this->_link);
    //#####
    // Get cache $this->_cache_list = $this->_getCache();
    //#####
    return $this->_link;
    }

    /* do not remove, useful for some modules */
    public function set_db($db_name) {
    return mysql_select_db($db_name, $this->_link);
    }

    public function disconnect() {
    if ($this->_link) @mysql_close($this->_link);
    $this->_link = false;
    }

    public function getRow($query) {
    if ($this->_isInCache($query) && (strtoupper(substr(trim($query) , 0, 6)) == "SELECT"))
    return $this->_getFromCache($query);
    $this->logRoute[] = $query;
    $this->_result = false;
    if ($this->_link)
    if ($this->_result = mysql_query($query . " LIMIT 1", $this->_link)) {
    if (_PS_DEBUG_SQL_)
    $this->displayMySQLError($query);
    $result = mysql_fetch_assoc($this->_result);
    $this->_addToCache($query, $result);
    return $result;
    }

    if (_PS_DEBUG_SQL_) $this->displayMySQLError($query);
    return false;
    }

    public function getValue($query) {
    if ($this->_isInCache($query) && strtoupper(substr(trim($query) , 0, 6)) == "SELECT")
    return $this->_getFromCache($query);
    $this->logRoute[] = $query;
    $this->_result = false;
    if ($this->_link AND $this->_result = mysql_query($query . " LIMIT 1", $this->_link) AND is_array($tmpArray = mysql_fetch_assoc($this->_result)))
    $result = array_shift($tmpArray);
    $this->_addToCache($query, $result);
    return $result;
    return false;
    }

    public function Execute($query) {
    if ($this->_isInCache($query) && strtoupper(substr(trim($query) , 0, 6)) == "SELECT")
    return $this->_getFromCache($query);
    $this->logRoute[] = $query;
    $this->_result = false;
    if ($this->_link) {
    $this->_result = mysql_query($query, $this->_link);
    if (_PS_DEBUG_SQL_) $this->displayMySQLError($query);
    $this->_addToCache($query, $this->_result);
    return $this->_result;
    }

    if (_PS_DEBUG_SQL_) $this->displayMySQLError($query);
    return false;
    }

    public function ExecuteS($query, $array = true) {
    if ($this->_isInCache($query) && strtoupper(substr(trim($query) , 0, 6)) == "SELECT") return $this->_getFromCache($query);
    $this->logRoute[] = $query;
    $this->_result = false;
    if ($this->_link &&
    $this->_result = mysql_query($query, $this->_link)) {
    if (_PS_DEBUG_SQL_) $this->displayMySQLError($query);
    if (!$array) {
    $this->_addToCache($query, $this->_result);
    return $this->_result;
    }

    $resultArray = array();
    while ($row = mysql_fetch_assoc($this->_result)) $resultArray[] = $row;
    $this->_addToCache($query, $resultArray);
    return $resultArray;
    }

    if (_PS_DEBUG_SQL_) $this->displayMySQLError($query);
    return false;
    }

    public function nextRow($result = false) {
    return mysql_fetch_assoc($result ? $result : $this->_result);
    }

    public function delete($table, $where = false, $limit = false) {
    $this->_result = false;
    if ($this->_link) return mysql_query("DELETE FROM `" . pSQL($table) . "`" . ($where ? " WHERE " . $where : "") . ($limit ? " LIMIT " . intval($limit) : "") , $this->_link);
    return false;
    }

    public function NumRows() {
    if ($this->_link AND $this->_result)
    return mysql_num_rows($this->_result);
    }

    public function Insert_ID() {
    if ($this->_link) return mysql_insert_id($this->_link);
    return false;
    }

    public function Affected_Rows() {
    if ($this->_link) return mysql_affected_rows($this->_link);
    return false;
    }

    protected function q($query) {
    $this->logRoute[] = $query;
    $this->_result = false;
    if ($this->_link) return mysql_query($query, $this->_link);
    return false;
    }

    /** * Returns the text of the error message from previous MySQL operation *
    * @acces public
    * @return string error
    */
    public function getMsgError($query = false) {
    return mysql_error();
    }

    public function getNumberError() {
    return mysql_errno();
    }

    public function displayMySQLError($query = false) {
    if (_PS_DEBUG_SQL_ AND mysql_errno()) {
    if ($query)
    die(Tools::displayError(mysql_error() . " " . $query . ""));
    die(Tools::displayError((mysql_error())));
    }
    }

    static public function tryToConnect($server, $user, $pwd, $db) {
    if (!$link = @mysql_connect($server, $user, $pwd))
    return 1;
    if (!@mysql_select_db($db, $link))
    return 2;
    @mysql_close($link);
    return 0;
    }

    static public function tryUTF8($server, $user, $pwd) {
    $link = @mysql_connect($server, $user, $pwd);
    if (!mysql_query("SETNAMES utf8", $link))
    $ret = false;
    else
    $ret = true;
    @mysql_close($link);
    return $ret;
    }
    }