| | |
| | | +-----------------------------------------------------------------------+ |
| | | | program/include/rcube_mdb2.php | |
| | | | | |
| | | | This file is part of the RoundCube Webmail client | |
| | | | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland | |
| | | | This file is part of the Roundcube Webmail client | |
| | | | Copyright (C) 2005-2009, Roundcube Dev. - Switzerland | |
| | | | Licensed under the GNU GPL | |
| | | | | |
| | | | PURPOSE: | |
| | |
| | | * @author David Saez Padros <david@ols.es> |
| | | * @author Thomas Bruederli <roundcube@gmail.com> |
| | | * @author Lukas Kahwe Smith <smith@pooteeweet.org> |
| | | * @version 1.16 |
| | | * @version 1.17 |
| | | * @link http://pear.php.net/package/MDB2 |
| | | */ |
| | | class rcube_mdb2 |
| | |
| | | $this->db_dsnw = $db_dsnw; |
| | | $this->db_dsnr = $db_dsnr; |
| | | $this->db_pconn = $pconn; |
| | | |
| | | |
| | | $dsn_array = MDB2::parseDSN($db_dsnw); |
| | | $this->db_provider = $dsn_array['phptype']; |
| | | } |
| | |
| | | 'emulate_prepared' => $this->debug_mode, |
| | | 'debug' => $this->debug_mode, |
| | | 'debug_handler' => 'mdb2_debug_handler', |
| | | 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_null); |
| | | 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL); |
| | | |
| | | if ($this->db_provider == 'pgsql') { |
| | | $db_options['disable_smart_seqname'] = true; |
| | |
| | | if (MDB2::isError($dbh)) { |
| | | $this->db_error = true; |
| | | $this->db_error_msg = $dbh->getMessage(); |
| | | |
| | | |
| | | raise_error(array('code' => 500, 'type' => 'db', |
| | | 'line' => __LINE__, 'file' => __FILE__, |
| | | 'message' => $dbh->getUserInfo()), true, false); |
| | |
| | | */ |
| | | function db_connect($mode) |
| | | { |
| | | $this->db_mode = $mode; |
| | | |
| | | // Already connected |
| | | if ($this->db_connected) { |
| | | // no replication, current connection is ok |
| | | if ($this->db_dsnw == $this->db_dsnr) |
| | | // no replication, current connection is ok for read and write |
| | | if (empty($this->db_dsnr) || $this->db_dsnw == $this->db_dsnr) { |
| | | $this->db_mode = 'w'; |
| | | return; |
| | | } |
| | | |
| | | // connected to master, current connection is ok |
| | | // connected to read-write db, current connection is ok |
| | | if ($this->db_mode == 'w') |
| | | return; |
| | | |
| | |
| | | $dsn = ($mode == 'r') ? $this->db_dsnr : $this->db_dsnw; |
| | | |
| | | $this->db_handle = $this->dsn_connect($dsn); |
| | | $this->db_connected = true; |
| | | $this->db_connected = !PEAR::isError($this->db_handle); |
| | | $this->db_mode = $mode; |
| | | } |
| | | |
| | | |
| | |
| | | */ |
| | | function query() |
| | | { |
| | | if (!$this->is_connected()) |
| | | return null; |
| | | |
| | | $params = func_get_args(); |
| | | $query = array_shift($params); |
| | | |
| | |
| | | $mode = (strtolower(substr(trim($query),0,6)) == 'select') ? 'r' : 'w'; |
| | | |
| | | $this->db_connect($mode); |
| | | |
| | | // check connection before proceeding |
| | | if (!$this->is_connected()) |
| | | return null; |
| | | |
| | | if ($this->db_provider == 'sqlite') |
| | | $this->_sqlite_prepare(); |
| | |
| | | } |
| | | |
| | | $id = $this->db_handle->lastInsertID($table); |
| | | |
| | | |
| | | return $this->db_handle->isError($id) ? null : $id; |
| | | } |
| | | |
| | |
| | | { |
| | | if (!$this->db_handle) |
| | | $this->db_connect('r'); |
| | | |
| | | |
| | | return $this->db_handle->escape($str); |
| | | } |
| | | |
| | |
| | | { |
| | | if (!is_array($arr)) |
| | | return $this->quote($arr, $type); |
| | | |
| | | |
| | | foreach ($arr as $idx => $item) |
| | | $arr[$idx] = $this->quote($item, $type); |
| | | |
| | |
| | | |
| | | case 'mssql': |
| | | case 'sqlsrv': |
| | | return "DATEDIFF(second, '19700101', $field) + DATEDIFF(second, GETDATE(), GETUTCDATE())"; |
| | | return "DATEDIFF(second, '19700101', $field) + DATEDIFF(second, GETDATE(), GETUTCDATE())"; |
| | | |
| | | default: |
| | | return "UNIX_TIMESTAMP($field)"; |
| | |
| | | $this->db_error = true; |
| | | $this->db_error_msg = $res->getMessage(); |
| | | raise_error(array('code' => 500, 'type' => 'db', |
| | | 'line' => __LINE__, 'file' => __FILE__, |
| | | 'message' => $res->getMessage() . " Query: " |
| | | . substr(preg_replace('/[\r\n]+\s*/', ' ', $res->userinfo), 0, 512)), |
| | | true, false); |
| | | 'line' => __LINE__, 'file' => __FILE__, |
| | | 'message' => $res->getMessage() . " Query: " |
| | | . substr(preg_replace('/[\r\n]+\s*/', ' ', $res->userinfo), 0, 512)), |
| | | true, false); |
| | | } |
| | | |
| | | |
| | | $res_id = sizeof($this->a_query_results); |
| | | $this->last_res_id = $res_id; |
| | | $this->a_query_results[$res_id] = $res; |
| | |
| | | if (isset($this->a_query_results[$res_id])) |
| | | if (!PEAR::isError($this->a_query_results[$res_id])) |
| | | return $this->a_query_results[$res_id]; |
| | | |
| | | |
| | | return false; |
| | | } |
| | | |
| | |
| | | if (strlen($data)) |
| | | if (!sqlite_exec($dbh->connection, $data, $error) || MDB2::isError($dbh)) |
| | | raise_error(array('code' => 500, 'type' => 'db', |
| | | 'line' => __LINE__, 'file' => __FILE__, |
| | | 'line' => __LINE__, 'file' => __FILE__, |
| | | 'message' => $error), true, false); |
| | | } |
| | | |
| | |
| | | function mdb2_debug_handler(&$db, $scope, $message, $context = array()) |
| | | { |
| | | if ($scope != 'prepare') { |
| | | $debug_output = $scope . '('.$db->db_index.'): ' . $message; |
| | | $debug_output = sprintf('%s(%d): %s;', |
| | | $scope, $db->db_index, rtrim($message, ';')); |
| | | write_log('sql', $debug_output); |
| | | } |
| | | } |
| | | |