Thomas Bruederli
2013-10-04 92d18cf32edab81ac77f547cfa718cf28a573c92
program/lib/Roundcube/rcube_db.php
@@ -32,6 +32,7 @@
    protected $db_connected = false;  // Already connected ?
    protected $db_mode;               // Connection mode
    protected $dbh;                   // Connection handle
    protected $dbhs = array();
    protected $db_error     = false;
    protected $db_error_msg = '';
@@ -97,6 +98,7 @@
        $this->db_dsnw  = $db_dsnw;
        $this->db_dsnr  = $db_dsnr;
        $this->db_pconn = $pconn;
        $this->db_dsnw_noread = rcube::get_instance()->config->get('db_dsnw_noread', false);
        $this->db_dsnw_array = self::parse_dsn($db_dsnw);
        $this->db_dsnr_array = self::parse_dsn($db_dsnr);
@@ -112,6 +114,13 @@
    {
        $this->db_error     = false;
        $this->db_error_msg = null;
        // return existing handle
        if ($this->dbhs[$mode]) {
            $this->dbh = $this->dbhs[$mode];
            $this->db_mode = $mode;
            return $this->dbh;
        }
        // Get database specific connection options
        $dsn_string  = $this->dsn_string($dsn);
@@ -147,6 +156,7 @@
        }
        $this->dbh          = $dbh;
        $this->dbhs[$mode]  = $dbh;
        $this->db_mode      = $mode;
        $this->db_connected = true;
        $this->conn_configure($dsn, $dbh);
@@ -190,14 +200,13 @@
        // Already connected
        if ($this->db_connected) {
            // connected to db with the same or "higher" mode
            if ($this->db_mode == 'w' || $this->db_mode == $mode) {
            // connected to db with the same or "higher" mode (if allowed)
            if ($this->db_mode == $mode || $this->db_mode == 'w' && !$this->db_dsnw_noread) {
                return;
            }
        }
        $dsn = ($mode == 'r') ? $this->db_dsnr_array : $this->db_dsnw_array;
        $this->dsn_connect($dsn, $mode);
        // use write-master when read-only fails
@@ -386,22 +395,36 @@
        $result = $this->dbh->query($query);
        if ($result === false) {
            $error = $this->dbh->errorInfo();
            if (empty($this->options['ignore_key_errors']) || $error[0] != '23000') {
                $this->db_error = true;
                $this->db_error_msg = sprintf('[%s] %s', $error[1], $error[2]);
                rcube::raise_error(array('code' => 500, 'type' => 'db',
                    'line' => __LINE__, 'file' => __FILE__,
                    'message' => $this->db_error_msg . " (SQL Query: $query)"
                    ), true, false);
            }
            $result = $this->handle_error($query);
        }
        $this->last_result = $result;
        return $result;
    }
    /**
     * Helper method to handle DB errors.
     * This by default logs the error but could be overriden by a driver implementation
     *
     * @param string Query that triggered the error
     * @return mixed Result to be stored and returned
     */
    protected function handle_error($query)
    {
        $error = $this->dbh->errorInfo();
        if (empty($this->options['ignore_key_errors']) || $error[0] != '23000') {
            $this->db_error = true;
            $this->db_error_msg = sprintf('[%s] %s', $error[1], $error[2]);
            rcube::raise_error(array('code' => 500, 'type' => 'db',
                'line' => __LINE__, 'file' => __FILE__,
                'message' => $this->db_error_msg . " (SQL Query: $query)"
                ), true, false);
        }
        return false;
    }
    /**
@@ -692,7 +715,7 @@
        if ($interval) {
            $add = ' ' . ($interval > 0 ? '+' : '-') . ' INTERVAL ';
            $add .= $interval > 0 ? intval($interval) : intval($interval) * -1;
            $add .= ' SECONDS';
            $add .= ' SECOND';
        }
        return "now()" . $add;