From 59478e06c25303a790a0840ab2ac30662c4ef781 Mon Sep 17 00:00:00 2001 From: Hugues Hiegel Date: Tue, 5 Aug 2014 16:46:22 +0200 Subject: c'est la merde.. --- program/lib/Roundcube/rcube_db.php | 107 +++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 52 deletions(-) (limited to 'program/lib/Roundcube/rcube_db.php') diff --git a/program/lib/Roundcube/rcube_db.php b/program/lib/Roundcube/rcube_db.php index 852070073..5083a0dfe 100644 --- a/program/lib/Roundcube/rcube_db.php +++ b/program/lib/Roundcube/rcube_db.php @@ -47,7 +47,6 @@ class rcube_db 'identifier_end' => '"', ); - const DEBUG_LINE_LENGTH = 4096; /** * Factory, returns driver-specific instance of the class @@ -63,6 +62,7 @@ class rcube_db $driver = strtolower(substr($db_dsnw, 0, strpos($db_dsnw, ':'))); $driver_map = array( 'sqlite2' => 'sqlite', + 'sqlite3' => 'sqlite', 'sybase' => 'mssql', 'dblib' => 'mssql', 'mysqli' => 'mysql', @@ -100,15 +100,27 @@ class rcube_db $this->db_dsnw_array = self::parse_dsn($db_dsnw); $this->db_dsnr_array = self::parse_dsn($db_dsnr); + + // Initialize driver class + $this->init(); + } + + /** + * Initialization of the object with driver specific code + */ + protected function init() + { + // To be used by driver classes } /** * Connect to specific database * - * @param array $dsn DSN for DB connections - * @param string $mode Connection mode (r|w) + * @param array $dsn DSN for DB connections + * + * @return PDO database handle */ - protected function dsn_connect($dsn, $mode) + protected function dsn_connect($dsn) { $this->db_error = false; $this->db_error_msg = null; @@ -146,10 +158,9 @@ class rcube_db return null; } - $this->dbh = $dbh; - $this->db_mode = $mode; - $this->db_connected = true; $this->conn_configure($dsn, $dbh); + + return $dbh; } /** @@ -171,6 +182,16 @@ class rcube_db { } + /** + * Driver-specific database character set setting + * + * @param string $charset Character set name + */ + protected function set_charset($charset) + { + $this->query("SET NAMES 'utf8'"); + } + /** * Connect to appropriate database depending on the operation * @@ -198,14 +219,23 @@ class rcube_db $dsn = ($mode == 'r') ? $this->db_dsnr_array : $this->db_dsnw_array; - $this->dsn_connect($dsn, $mode); + $this->dbh = $this->dsn_connect($dsn); + $this->db_connected = is_object($this->dbh); // use write-master when read-only fails - if (!$this->db_connected && $mode == 'r' && $this->is_replicated()) { - $this->dsn_connect($this->db_dsnw_array, 'w'); + if (!$this->db_connected && $mode == 'r') { + $mode = 'w'; + $this->dbh = $this->dsn_connect($this->db_dsnw_array); + $this->db_connected = is_object($this->dbh); } - $this->conn_failure = !$this->db_connected; + if ($this->db_connected) { + $this->db_mode = $mode; + $this->set_charset('utf8'); + } + else { + $this->conn_failure = true; + } } /** @@ -226,11 +256,6 @@ class rcube_db protected function debug($query) { if ($this->options['debug_mode']) { - if (($len = strlen($query)) > self::DEBUG_LINE_LENGTH) { - $diff = $len - self::DEBUG_LINE_LENGTH; - $query = substr($query, 0, self::DEBUG_LINE_LENGTH) - . "... [truncated $diff bytes]"; - } rcube::write_log('sql', '[' . (++$this->db_index) . '] ' . $query . ';'); } } @@ -338,10 +363,8 @@ class rcube_db */ protected function _query($query, $offset, $numrows, $params) { - $query = trim($query); - // Read or write ? - $mode = preg_match('/^(select|show|set)/i', $query) ? 'r' : 'w'; + $mode = preg_match('/^(select|show)/i', ltrim($query)) ? 'r' : 'w'; $this->db_connect($mode); @@ -387,16 +410,13 @@ class rcube_db if ($result === false) { $error = $this->dbh->errorInfo(); + $this->db_error = true; + $this->db_error_msg = sprintf('[%s] %s', $error[1], $error[2]); - 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); - } + rcube::raise_error(array('code' => 500, 'type' => 'db', + 'line' => __LINE__, 'file' => __FILE__, + 'message' => $this->db_error_msg . " (SQL Query: $query)" + ), true, false); } $this->last_result = $result; @@ -683,19 +703,11 @@ class rcube_db /** * Return SQL function for current time and date * - * @param int $interval Optional interval (in seconds) to add/subtract - * * @return string SQL function to use in query */ - public function now($interval = 0) + public function now() { - if ($interval) { - $add = ' ' . ($interval > 0 ? '+' : '-') . ' INTERVAL '; - $add .= $interval > 0 ? intval($interval) : intval($interval) * -1; - $add .= ' SECOND'; - } - - return "now()" . $add; + return "now()"; } /** @@ -856,25 +868,16 @@ class rcube_db { $rcube = rcube::get_instance(); - // add prefix to the table name if configured - if ($prefix = $rcube->config->get('db_prefix')) { - return $prefix . $table; + // return table name if configured + $config_key = 'db_table_'.$table; + + if ($name = $rcube->config->get($config_key)) { + return $name; } return $table; } - /** - * Set class option value - * - * @param string $name Option name - * @param mixed $value Option value - */ - public function set_option($name, $value) - { - $this->options[$name] = $value; - } - /** * MDB2 DSN string parser * -- cgit v1.2.3