From 66773789e392305bba4cdf7ed8e6ae3b8380de51 Mon Sep 17 00:00:00 2001
From: svncommit <devs@roundcube.net>
Date: Thu, 27 Oct 2005 09:45:33 -0400
Subject: [PATCH] 

---
 program/include/rcube_db.inc |  151 ++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 122 insertions(+), 29 deletions(-)

diff --git a/program/include/rcube_db.inc b/program/include/rcube_db.inc
index fe838da..a987d2e 100755
--- a/program/include/rcube_db.inc
+++ b/program/include/rcube_db.inc
@@ -40,6 +40,9 @@
         
         $this->db_dsnw = $db_dsnw;
         $this->db_dsnr = $db_dsnr;
+        
+        $dsn_array = DB::parseDSN($db_dsnw);
+        $this->db_provider = $dsn_array['phptype'];        
     }
 
     // PHP 4 compatibility
@@ -52,23 +55,31 @@
     function dsn_connect($dsn)
     {
         // Use persistent connections if available
-        
         $dbh = DB::connect($dsn, array('persistent' => $true));
-
+        
         if (DB::isError($dbh))
             raise_error(array('code' => 500,
                         'type' => 'db',
                         'line' => __LINE__,
                         'file' => __FILE__,
                         'message' => $dbh->getMessage()), TRUE, FALSE);
+
+        else if ($this->db_provider=='sqlite')
+        {
+            $dsn_array = DB::parseDSN($dsn);
+            if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials))
+                $this->_sqlite_create_database($dbh, $this->sqlite_initials);
+        }
+        
         return $dbh;
     }
 
     // Connect to appropiate databse    
     function db_connect ($mode)
     {
+        $this->db_mode = $mode;
+
         // Already connected
-        
         if ($this->db_connected)
             {
             // no replication, current connection is ok
@@ -88,40 +99,59 @@
 
         $this->db_handle = $this->dsn_connect($dsn);
         $this->db_connected = true;
-        $this->db_mode = $mode;
     }
 
-    // Query database (read operations)
+    // Query database
     
-    function query($query)
+    function query()
+    {
+		$params = func_get_args();
+		$query = array_shift($params);
+		
+		return $this->_query($query, 0, 0, $params);
+    }
+   
+	function limitquery()
+    {
+		$params = func_get_args();
+		$query = array_shift($params);
+		$offset = array_shift($params);
+		$numrows = array_shift($params);
+		
+		return $this->_query($query, $offset, $numrows, $params);
+    }
+    
+    function _query($query, $offset, $numrows, $params)
     {
         // Read or write ?
-        
         if (strtolower(trim(substr($query,0,6)))=='select')
             $mode='r';
         else
-            {
             $mode='w';
-            }
         
-        $this->db_connect($mode);           
-    
-        $result = $this->db_handle->query($query);
+        $this->db_connect($mode);
+
+        if ($this->db_provider == 'sqlite')
+            $query = $this->_sqlite_prepare_query($query);
         
+        if ($numrows || $offset)
+			{
+			$result = $this->db_handle->limitQuery($query,$offset,$numrows,$params);
+			}
+        else    
+			$result = $this->db_handle->query($query,$params);
+
         if (DB::isError($result))
-            raise_error( array('code' => 500, 'type' => 'db', 'line' => __LINE__, 
-                         'file' => __FILE__, 
-                         'message' => $result->getMessage()), TRUE, FALSE);
-        
+			{
+            raise_error(array('code' => 500,
+                              'type' => 'db',
+                              'line' => __LINE__, 
+                              'file' => __FILE__, 
+                              'message' => $result->getMessage()), TRUE, FALSE);
+             return false;
+            }
+
         return $this->_add_result($result, $query);
-    }
-
-    function db_execute ($query)
-    {
-        db_connect('w');
-
-        $result = $this->db_handle->query($query);
-
     }
     
     function num_rows($res_id=NULL)
@@ -147,7 +177,7 @@
 
     function insert_id($sequence = '')
     {
-        if (!$this->db_link || $this->db_mode=='r')
+        if (!$this->db_handle || $this->db_mode=='r')
             return FALSE;
 
         switch($this->db_provider)
@@ -155,14 +185,21 @@
             case 'pgsql':
                 // PostgreSQL uses sequences
                 $result =& $this->db_handle->getOne("SELECT CURRVAL('$sequence')");    
-                if (DB::isError($result))
+                if (DB::isError($result)) {
                     raise_error( array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, 
                     'message' => $result->getMessage()), TRUE, TRUE);
+                }
                 return $result;
                 
             case 'mysql': // This is unfortuneate
-                return mysql_insert_id($this->db_handle);
-                
+                return mysql_insert_id($this->db_handle->connection);
+
+			case 'mysqli':
+				return mysqli_insert_id($this->db_handle->connection);
+	
+            case 'sqlite':
+                return sqlite_last_insert_rowid($this->db_handle->connection);
+
             default:
                 die("portability issue with this database, please have the developer fix");
         }
@@ -174,12 +211,35 @@
         $result = $this->_get_result($res_id);
 
         if (DB::isError($result))
+        {
             raise_error( array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__,
-                         'message' => $this->db_link->getMessage()), TRUE, TRUE);
+                         'message' => $this->db_link->getMessage()), TRUE, FALSE);
+            return FALSE;
+        }
                          
         return $result->fetchRow(DB_FETCHMODE_ASSOC);
     }
 
+	function quoteIdentifier ( $str )
+	{
+		if (!$this->db_handle)
+			$this->db_connect('r');
+			
+		return $this->db_handle->quoteIdentifier($str);
+	}
+	
+	function unixtimestamp($field)
+	{
+		switch($this->db_provider)
+			{
+			case 'pgsql':
+				return "EXTRACT (EPOCH FROM $field)";
+				break;
+			default:
+				return "UNIX_TIMESTAMP($field)";
+			}
+	}
+	
     function _add_result($res, $query)
     {
         // sql error occured
@@ -209,6 +269,39 @@
         return FALSE;
     }
 
+
+    // create a sqlite database from a file
+    function _sqlite_create_database($dbh, $fileName)
+    {
+        if (empty($fileName) || !is_string($fileName))
+            return ;
+
+        $data = '';
+        if ($fd = fopen($fileName, 'r'))
+          {
+          $data = fread($fd, filesize($fileName));
+          fclose($fd);
+          }
+
+        if (strlen($data))
+          sqlite_exec($dbh->connection, $data);
+    }
+
+    // transform a query so that it is sqlite2 compliant
+    function _sqlite_prepare_query($query)
+    {
+        if (!is_string($query))
+            return ($query);
+
+        $search = array('/NOW\(\)/',
+                        '/`/');
+        $replace = array("datetime('now')",
+                         '"');
+        $query = preg_replace($search, $replace, $query);
+
+        return ($query);
+    }
+    
 }
 
 ?>
\ No newline at end of file

--
Gitblit v1.9.1