thomascube
2008-04-05 e70d6ea64e711096af36b1234f8545b870ea5f45
program/include/rcube_mdb2.inc
old mode 100755 new mode 100644
@@ -5,7 +5,7 @@
 | program/include/rcube_mdb2.inc                                        |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2005, RoundCube Dev. - Switzerland                      |
 | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
@@ -32,14 +32,14 @@
 *
 * This is a wrapper for the PEAR::MDB2 class
 *
 * @package    RoundCube Webmail
 * @package    Database
 * @author     David Saez Padros <david@ols.es>
 * @author     Thomas Bruederli <roundcube@gmail.com>
 * @author     Lukas Kahwe Smith <smith@pooteeweet.org>
 * @version    1.16
 * @link       http://pear.php.net/package/MDB2
 */
class rcube_db
class rcube_mdb2
  {
  var $db_dsnw;               // DSN for write operations
  var $db_dsnr;               // DSN for read operations
@@ -48,6 +48,7 @@
  var $db_handle = 0;         // Connection handle
  var $db_error = false;
  var $db_error_msg = '';
  var $debug_mode = false;
  var $a_query_results = array('dummy');
  var $last_res_id = 0;
@@ -59,14 +60,15 @@
   * @param  string  DSN for read/write operations
   * @param  string  Optional DSN for read only operations
   */
  function __construct($db_dsnw, $db_dsnr='')
  function __construct($db_dsnw, $db_dsnr='', $pconn=false)
    {
    if ($db_dsnr=='')
      $db_dsnr=$db_dsnw;
    $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'];
    }
@@ -75,7 +77,7 @@
  /**
   * PHP 4 object constructor
   *
   * @see  rcube_MDB2::__construct
   * @see  rcube_mdb2::__construct
   */
  function rcube_db($db_dsnw,$db_dsnr='')
    {
@@ -93,23 +95,29 @@
  function dsn_connect($dsn)
    {
    // Use persistent connections if available
    $dbh = MDB2::connect($dsn, array('persistent' => TRUE, 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL));
    $dbh = MDB2::connect($dsn, array(
        'persistent' => $this->db_pconn,
        'emulate_prepared' => $this->debug_mode,
        'debug' => $this->debug_mode,
        'debug_handler' => 'mdb2_debug_handler',
        'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL));
    if (PEAR::isError($dbh))
    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->getMessage()), TRUE, FALSE);
      raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__,
        'file' => __FILE__, 'message' => $dbh->getUserInfo()), TRUE, FALSE);
      }
    else if ($this->db_provider=='sqlite')
      {
      $dsn_array = MDB2::parseDSN($dsn);
      if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials))
        $this->_sqlite_create_database($dbh, $this->sqlite_initials);
      }
    else
      $dbh->setCharset('utf8');
    return $dbh;
    }
@@ -151,6 +159,21 @@
    $this->db_connected = true;
    }
  /**
   * Activate/deactivate debug mode
   *
   * @param boolean True if SQL queries should be logged
   */
  function set_debug($dbg = true)
  {
    $this->debug_mode = $dbg;
    if ($this->db_connected)
    {
      $this->db_handle->setOption('debug', $dbg);
      $this->db_handle->setOption('emulate_prepared', $dbg);
    }
  }
    
  /**
@@ -285,7 +308,7 @@
    if (!$this->db_handle)
      return FALSE;
    return $result;
    return $this->_get_result($result);
    }
@@ -409,6 +432,41 @@
    return $this->db_handle->quoteIdentifier($str);
    }
  /**
   * Escapes a string
   *
   * @param  string  The string to be escaped
   * @return string  The escaped string
   * @access public
   * @since  0.1.1
   */
  function escapeSimple($str)
    {
    if (!$this->db_handle)
      $this->db_connect('r');
    return $this->db_handle->escape($str);
    }
  /**
   * Return SQL function for current time and date
   *
   * @return string SQL function to use in query
   * @access public
   */
  function now()
    {
    switch($this->db_provider)
      {
      case 'mssql':
        return "getdate()";
      default:
        return "now()";
      }
    }
  /**
   * Return SQL statement to convert a field value into a unix timestamp
@@ -424,6 +482,9 @@
      case 'pgsql':
        return "EXTRACT (EPOCH FROM $field)";
        break;
      case 'mssql':
        return "datediff(s, '1970-01-01 00:00:00', $field)";
      default:
        return "UNIX_TIMESTAMP($field)";
@@ -543,4 +604,17 @@
  }  // end class rcube_db
?>
/* this is our own debug handler for the MDB2 connection */
function mdb2_debug_handler(&$db, $scope, $message, $context = array())
{
  if ($scope != 'prepare')
  {
    $debug_output = $scope . '('.$db->db_index.'): ';
    $debug_output .= $message . $db->getOption('log_line_break');
    write_log('sqllog', $debug_output);
  }
}
?>