From e70d6ea64e711096af36b1234f8545b870ea5f45 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Sat, 05 Apr 2008 08:49:21 -0400
Subject: [PATCH] Apply changes from trunk to 0.1-stable

---
 program/include/rcube_db.inc |   82 +++++++++++++++++++++++++++++++++-------
 1 files changed, 67 insertions(+), 15 deletions(-)

diff --git a/program/include/rcube_db.inc b/program/include/rcube_db.inc
old mode 100755
new mode 100644
index 5731688..63c6759
--- a/program/include/rcube_db.inc
+++ b/program/include/rcube_db.inc
@@ -5,7 +5,7 @@
  | program/include/rcube_db.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:                                                              |
@@ -14,6 +14,7 @@
  |                                                                       |
  +-----------------------------------------------------------------------+
  | Author: David Saez Padros <david@ols.es>                              |
+ |         Thomas Bruederli <roundcube@gmail.com>                        |
  +-----------------------------------------------------------------------+
 
  $Id$
@@ -24,15 +25,14 @@
 /**
  * Obtain the PEAR::DB class that is used for abstraction
  */
-require_once('DB.php');
-
+require_once 'DB.php';
 
 /**
  * Database independent query interface
  *
  * This is a wrapper for the PEAR::DB class
  *
- * @package    RoundCube Webmail
+ * @package    Database
  * @author     David Saez Padros <david@ols.es>
  * @author     Thomas Bruederli <roundcube@gmail.com>
  * @version    1.17
@@ -80,7 +80,7 @@
    */
   function rcube_db($db_dsnw, $db_dsnr='', $pconn=false)
     {
-    $this->__construct($db_dsnw, $db_dsnr);
+    $this->__construct($db_dsnw, $db_dsnr, $pconn);
     }
 
 
@@ -101,7 +101,7 @@
       $this->db_error = TRUE;
       $this->db_error_msg = $dbh->getMessage();
 
-      raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__,
+      raise_error(array('code' => 603, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__,
                         'message' => $this->db_error_msg), TRUE, FALSE);
                         
       return FALSE;
@@ -153,8 +153,18 @@
     $this->db_handle = $this->dsn_connect($dsn);
     $this->db_connected = $this->db_handle ? TRUE : FALSE;
     }
+
+
+  /**
+   * Activate/deactivate debug mode
+   * (not implemented)
+   */
+  function set_debug($dbg = true)
+  {
     
-    
+  }
+
+
   /**
    * Getter for error state
    *
@@ -291,14 +301,17 @@
     switch($this->db_provider)
       {
       case 'pgsql':
-        // PostgreSQL uses sequences
         $result = &$this->db_handle->getOne("SELECT CURRVAL('$sequence')");
         if (DB::isError($result))
-          {
           raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, 
                             'message' => $result->getMessage()), TRUE, FALSE);
-          }
+        return $result;
 
+      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;
                 
       case 'mysql': // This is unfortuneate
@@ -306,7 +319,7 @@
 
       case 'mysqli':
         return mysqli_insert_id($this->db_handle->connection);
-	
+
       case 'sqlite':
         return sqlite_last_insert_rowid($this->db_handle->connection);
 
@@ -356,12 +369,14 @@
    */
   function _fetch_row($result, $mode)
     {
-    if (DB::isError($result))
+    if (!$result || DB::isError($result))
       {
       raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__,
                         'message' => $this->db_link->getMessage()), TRUE, FALSE);
       return FALSE;
       }
+      elseif (!is_object($result))
+      return FALSE;
                          
     return $result->fetchRow($mode);
     }
@@ -421,6 +436,41 @@
 
 
   /**
+   * Escapes a string
+   *
+   * @param  string  The string to be escaped
+   * @return string  The escaped string
+   * @access public
+   */
+  function escapeSimple($str)
+    {
+    if (!$this->db_handle)
+      $this->db_connect('r');
+
+    return $this->db_handle->escapeSimple($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
    *
    * @param  string  Field name
@@ -433,7 +483,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)";
@@ -455,7 +507,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);
@@ -553,4 +605,4 @@
 
   }  // end class rcube_db
 
-?>
\ No newline at end of file
+?>

--
Gitblit v1.9.1