Aleksander Machniak
2012-07-06 e6e5cb12f5aa93677fe8a373c56bd212a60a82ae
program/include/rcube_db.php
@@ -67,6 +67,7 @@
            'sqlite2' => 'sqlite',
            'sybase'  => 'mssql',
            'dblib'   => 'mssql',
            'mysqli'  => 'mysql',
        );
        $driver = isset($driver_map[$driver]) ? $driver_map[$driver] : $driver;
@@ -82,13 +83,12 @@
        return new $class($db_dsnw, $db_dsnr, $pconn);
    }
    /**
     * Object constructor
     *
     * @param  string $db_dsnw DSN for read/write operations
     * @param  string $db_dsnr Optional DSN for read only operations
     * @param bool   $pconn    Enables persistent connections
     * @param  string $db_dsnw  DSN for read/write operations
     * @param  string $db_dsnr  Optional DSN for read only operations
     * @param  bool   $pconn    Enables persistent connections
     */
    public function __construct($db_dsnw, $db_dsnr = '', $pconn = false)
    {
@@ -107,7 +107,9 @@
        $this->init();
    }
    /**
     * Initialization of the object with driver specific code
     */
    protected function init()
    {
        // To be used by driver classes
@@ -135,6 +137,11 @@
        // Connect
        try {
            // with this check we skip fatal error on PDO object creation
            if (!class_exists('PDO', false)) {
                throw new Exception('PDO extension not loaded. See http://php.net/manual/en/intro.pdo.php');
            }
            $this->conn_prepare($dsn);
            $dbh = new PDO($dsn_string, $dsn['username'], $dsn['password'], $dsn_options);
@@ -142,7 +149,7 @@
            // don't throw exceptions or warnings
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
        }
        catch (PDOException $e) {
        catch (Exception $e) {
            $this->db_error     = true;
            $this->db_error_msg = $e->getMessage();
@@ -158,15 +165,34 @@
        return $dbh;
    }
    /**
     * Driver-specific preparation of database connection
     *
     * @param  array $dsn  DSN for DB connections
     */
    protected function conn_prepare($dsn)
    {
    }
    /**
     * Driver-specific configuration of database connection
     *
     * @param  array $dsn  DSN for DB connections
     * @param  PDO   $dbh  Connection handler
     */
    protected function conn_configure($dsn, $dbh)
    {
    }
    /**
     * Driver-specific database character set setting
     *
     * @param  string $charset  Character set name
     */
    protected function set_charset($charset)
    {
        $this->query("SET NAMES 'utf8'");
    }
    /**
     * Connect to appropiate database depending on the operation
@@ -214,7 +240,6 @@
        }
    }
    /**
     * Activate/deactivate debug mode
     *
@@ -224,13 +249,6 @@
    {
        $this->options['debug_mode'] = $dbg;
    }
    protected function set_charset($charset)
    {
        $this->query("SET NAMES 'utf8'");
    }
    /**
     * Getter for error state
@@ -242,7 +260,6 @@
        return $this->db_error ? $this->db_error_msg : false;
    }
    /**
     * Connection state checker
     *
@@ -253,7 +270,6 @@
        return !is_object($this->dbh) ? false : $this->db_connected;
    }
    /**
     * Is database replication configured?
     * This returns true if dsnw != dsnr
@@ -262,7 +278,6 @@
    {
      return !empty($this->db_dsnr) && $this->db_dsnw != $this->db_dsnr;
    }
    /**
     * Execute a SQL query
@@ -285,7 +300,6 @@
        return $this->_query($query, 0, 0, $params);
    }
    /**
     * Execute a SQL query with limits
     *
@@ -305,7 +319,6 @@
        return $this->_query($query, $offset, $numrows, $params);
    }
    /**
     * Execute a SQL query with limits
@@ -368,7 +381,6 @@
        return $this->_add_result($query);
    }
    /**
     * Get number of affected rows for the last query
     *
@@ -383,7 +395,6 @@
        return 0;
    }
    /**
     * Get last inserted record ID
@@ -409,7 +420,6 @@
        return $id;
    }
    /**
     * Get an associative array for one row
     * If no query handle is specified, the last query will be taken as reference
@@ -424,7 +434,6 @@
        return $this->_fetch_row($result, PDO::FETCH_ASSOC);
    }
    /**
     * Get an index array for one row
     * If no query handle is specified, the last query will be taken as reference
@@ -438,7 +447,6 @@
        $result = $this->_get_result($res_id);
        return $this->_fetch_row($result, PDO::FETCH_NUM);
    }
    /**
     * Get col values for a result row
@@ -457,10 +465,14 @@
        return $result->fetch($mode);
    }
    /**
     * Adds LIMIT,OFFSET clauses to the query
     *
     * @param string $query   SQL query
     * @param int    $limit   Number of rows
     * @param int    $offset  Offset
     *
     * @return string SQL query
     */
    protected function set_limit($query, $limit = 0, $offset = 0)
    {
@@ -474,7 +486,6 @@
        return $query;
    }
    /**
     * Returns list of tables in a database
@@ -498,7 +509,6 @@
        return $this->tables;
    }
    /**
     * Returns list of columns in database table
     *
@@ -517,7 +527,6 @@
        return array();
    }
    /**
     * Formats input so it can be safely used in a query
@@ -551,7 +560,6 @@
        return 'NULL';
    }
    /**
     * Quotes a string so it can be safely used as a table or column name
     *
@@ -565,7 +573,6 @@
    {
        return $this->quote_identifier($str);
    }
    /**
     * Quotes a string so it can be safely used as a table or column name
@@ -588,7 +595,6 @@
        return  implode($name, '.');
    }
    /**
     * Return SQL function for current time and date
     *
@@ -598,7 +604,6 @@
    {
        return "now()";
    }
    /**
     * Return list of elements for use with SQL's IN clause
@@ -621,7 +626,6 @@
        return implode(',', $arr);
    }
    /**
     * Return SQL statement to convert a field value into a unix timestamp
     *
@@ -638,7 +642,6 @@
        return "UNIX_TIMESTAMP($field)";
    }
    /**
     * Return SQL statement to convert from a unix timestamp
     *
@@ -650,7 +653,6 @@
    {
        return date("'Y-m-d H:i:s'", $timestamp);
    }
    /**
     * Return SQL statement for case insensitive LIKE
@@ -664,7 +666,6 @@
    {
        return $this->quote_identifier($column).' LIKE '.$this->quote($value);
    }
    /**
     * Abstract SQL statement for value concatenation
@@ -680,7 +681,6 @@
        return '(' . join(' || ', $args) . ')';
    }
    /**
     * Encodes non-UTF-8 characters in string/array/object (recursive)
@@ -707,7 +707,6 @@
        return utf8_encode($input);
    }
    /**
     * Decodes encoded UTF-8 string/object/array (recursive)
     *
@@ -733,7 +732,6 @@
        return utf8_decode($input);
    }
    /**
     * Adds a query result and returns a handle ID
     *
@@ -749,7 +747,6 @@
        return $res_id;
    }
    /**
     * Resolves a given handle ID and returns the according query handle
@@ -772,7 +769,6 @@
        return false;
    }
    /**
     * Return correct name for a specific database table
     *
@@ -793,7 +789,6 @@
        return $table;
    }
    /**
     * Return correct name for a specific database sequence
@@ -817,9 +812,12 @@
        return $sequence;
    }
    /**
     * MDB2 DSN string parser
     *
     * @param string $sequence Secuence name
     *
     * @return array DSN parameters
     */
    public static function parse_dsn($dsn)
    {
@@ -938,7 +936,11 @@
    }
    /**
     * Returns PDO DSN string from DSN array (parse_dsn() result)
     * Returns PDO DSN string from DSN array
     *
     * @param array $dsn  DSN parameters
     *
     * @return string DSN string
     */
    protected function dsn_string($dsn)
    {
@@ -965,7 +967,11 @@
    }
    /**
     * Returns PDO driver options array from DSN array (parse_dsn() result)
     * Returns driver-specific connection options
     *
     * @param array  $dsn  DSN parameters
     *
     * @return array Connection options
     */
    protected function dsn_options($dsn)
    {