thomascube
2006-12-13 7a5c48e7f70b8bc938fcae3ffd2be0fdbeaab145
program/include/rcube_db.inc
@@ -14,6 +14,7 @@
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: David Saez Padros <david@ols.es>                              |
 |         Thomas Bruederli <roundcube@gmail.com>                        |
 +-----------------------------------------------------------------------+
 $Id$
@@ -35,7 +36,7 @@
 * @package    RoundCube Webmail
 * @author     David Saez Padros <david@ols.es>
 * @author     Thomas Bruederli <roundcube@gmail.com>
 * @version    1.16
 * @version    1.17
 * @link       http://pear.php.net/package/DB
 */
class rcube_db
@@ -45,6 +46,9 @@
  var $db_connected = false;  // Already connected ?
  var $db_mode = '';          // Connection mode
  var $db_handle = 0;         // Connection handle
  var $db_pconn = false;      // Use persistent connections
  var $db_error = false;
  var $db_error_msg = '';
  var $a_query_results = array('dummy');
  var $last_res_id = 0;
@@ -56,13 +60,14 @@
   * @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 = DB::parseDSN($db_dsnw);
    $this->db_provider = $dsn_array['phptype'];        
@@ -74,9 +79,9 @@
   *
   * @see  rcube_db::__construct
   */
  function rcube_db($db_dsnw,$db_dsnr='')
  function rcube_db($db_dsnw, $db_dsnr='', $pconn=false)
    {
    $this->__construct($db_dsnw,$db_dsnr);
    $this->__construct($db_dsnw, $db_dsnr, $pconn);
    }
@@ -90,12 +95,17 @@
  function dsn_connect($dsn)
    {
    // Use persistent connections if available
    $dbh = DB::connect($dsn, array('persistent' => TRUE));
    $dbh = DB::connect($dsn, array('persistent' => $this->db_pconn));
        
    if (DB::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);
                        'message' => $this->db_error_msg), TRUE, FALSE);
      return FALSE;
      }
    else if ($this->db_provider=='sqlite')
@@ -142,7 +152,18 @@
      $dsn = $this->db_dsnw;
    $this->db_handle = $this->dsn_connect($dsn);
    $this->db_connected = true;
    $this->db_connected = $this->db_handle ? TRUE : FALSE;
    }
  /**
   * Getter for error state
   *
   * @param  boolean  True on error
   */
  function is_error()
    {
    return $this->db_error ? $this->db_error_msg : FALSE;
    }
@@ -203,6 +224,9 @@
      $mode='w';
        
    $this->db_connect($mode);
    if (!$this->db_connected)
      return FALSE;
    if ($this->db_provider == 'sqlite')
      $this->_sqlite_prepare();
@@ -268,13 +292,14 @@
    switch($this->db_provider)
      {
      case 'pgsql':
        // PostgreSQL uses sequences
        $result = &$this->db_handle->getOne("SELECT CURRVAL('$sequence')");
      case 'mssql':
          $result = &$this->db_handle->getOne("SELECT @@IDENTITY");
        if (DB::isError($result))
          {
          raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, 
                            'message' => $result->getMessage()), TRUE, FALSE);
          }
        return $result;
                
@@ -397,6 +422,25 @@
    }
  /*
   * 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
   *
@@ -410,7 +454,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)";
@@ -432,7 +478,7 @@
      case 'mysqli':
      case 'mysql':
      case 'sqlite':
        return "FROM_UNIXTIME($timestamp)";
        return sprintf("FROM_UNIXTIME(%d)", $timestamp);
      default:
        return date("'Y-m-d H:i:s'", $timestamp);