update INSTALL and UPGRADE file
[phpfspot.git] / phpfspot_db.php
index 90fdb8cc17392088a0d83763b7508b940dc764ce..339e02350719fbf44ecf2aeab925886c9eac5cfe 100644 (file)
@@ -24,6 +24,7 @@
 
 /**
  * PHPFSPOT_DB class
+ *
  * @package phpfspot
  */
 class PHPFSPOT_DB {
@@ -126,32 +127,30 @@ class PHPFSPOT_DB {
     */
    public function db_query($query = "")
    {
-      if($this->getConnStatus()) {
+      if(!$this->getConnStatus())
+         return false;
    
-         $this->last_query = $query;
-
-         switch($this->parent->cfg->db_access) {
-            case 'native':
-               if(($result = sqlite3_query($this->db, $query)) === false)
-                  $this->ThrowError($this->getLastError());
-               break;
-            case 'pdo':
-               try{
-                  $result = $this->db->query($query);
-                  return $result;
-               }
-               catch (Exception $e) {
-                  $this->ThrowError($e->getMessage());
-                  $result= NULL;
-               }
-               break;
+      $this->last_query = $query;
+
+      switch($this->parent->cfg->db_access) {
+         case 'native':
+            if(($result = sqlite3_query($this->db, $query)) === false)
+               $this->ThrowError($this->getLastError());
+            break;
+         case 'pdo':
+            try{
+               $result = $this->db->query($query);
+               return $result;
+            }
+            catch (Exception $e) {
+               $this->ThrowError($e->getMessage());
+               $result= NULL;
+            }
+            break;
 
-         }
-       
-         return $result;
       }
-      else 
-         $this->ThrowError("Can't execute query - we are not connected!");
+
+      return $result;
 
    } // db_query()
 
@@ -162,27 +161,30 @@ class PHPFSPOT_DB {
     */
    public function db_exec($query = "")
    {
-      if($this->getConnStatus()) {
-
-         $this->last_query = $query;
-
-         switch($this->parent->cfg->db_access) {
-            case 'native':
-               if(($result = sqlite3_exec($this->db, $query)) === false)
-                  $this->ThrowError($this->getLastError());
-               break;
-            case 'pdo':
-               try {
-                  $result = $this->db->query($query);
-               }
-               catch (Exception $e){
-                  $this->ThrowError($e->getLMessage());
-               }
-               break;
-         }
+      if(!$this->getConnStatus())
+         return false;
+
+      $this->last_query = $query;
+
+      switch($this->parent->cfg->db_access) {
+         case 'native':
+            if(($result = sqlite3_exec($this->db, $query)) === false) {
+               $this->ThrowError($this->getLastError());
+               return false;
+            }
+            break;
+         case 'pdo':
+            try {
+               $result = $this->db->query($query);
+            }
+            catch (Exception $e){
+               $this->ThrowError($e->getLMessage());
+               return false;
+            }
+            break;
       }
-      else 
-         $this->ThrowError("Can't execute query - we are not connected!");
+
+      return true;
 
    } // db_exec()
 
@@ -205,21 +207,19 @@ class PHPFSPOT_DB {
     */
    public function db_fetchSingleRow($query = "") 
    {
-      if($this->getConnStatus()) {
-
-         $result = $this->db_query($query);
-         switch($this->parent->cfg->db_access) {
-            case 'native':
-               $row = $this->db_fetch_object($result);
-               break;
-            case 'pdo':
-               $row = $result->fetch();
-               break;
-         }
-         return $row;
+      if(!$this->getConnStatus())
+         return false;
+
+      $result = $this->db_query($query);
+      switch($this->parent->cfg->db_access) {
+         case 'native':
+            $row = $this->db_fetch_object($result);
+            break;
+         case 'pdo':
+            $row = $result->fetch();
+            break;
       }
-      else 
-         $this->ThrowError("Can't fetch row - we are not connected!");
+      return $row;
       
    } // db_fetchSingleRow()
 
@@ -252,31 +252,63 @@ class PHPFSPOT_DB {
     */
    public function db_check_table_exists($table_name = "")
    {
-      if($this->getConnStatus()) {
-
-         $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
-         switch($this->parent->cfg->db_access) {
-            case 'native':
-               while($table = $this->db_fetch_object($result)) {
-                  if($table['name'] == $table_name)
-                     return true;
-               }
-               break;
-            case 'pdo':
-               foreach($result as $table ){
-                  if($table['NAME'] == $table_name)
-                     return true;
-               }
-               break;
-          }
-
+      if(!$this->getConnStatus())
          return false;
-      }
-      else
-         $this->ThrowError("Can't check table - we are not connected!");
+
+      $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
+      switch($this->parent->cfg->db_access) {
+         case 'native':
+            while($table = $this->db_fetch_object($result)) {
+               if($table['name'] == $table_name)
+                  return true;
+            }
+            break;
+         case 'pdo':
+            foreach($result as $table ){
+               if($table['NAME'] == $table_name)
+                  return true;
+            }
+            break;
+       }
+
+      return false;
         
    } // db_check_table_exists()
 
+   /**
+    * PHPFSPOT_DB check column exist
+    *
+    * This function checks if the given column exists within
+    * the specified table.
+    */
+   public function db_check_column_exists($table_name, $column)
+   {
+      if(!$this->getConnStatus())
+         return false;
+
+      $result = $this->db_query("
+         SELECT sql
+         FROM
+            (SELECT * FROM sqlite_master UNION ALL
+             SELECT * FROM sqlite_temp_master)
+         WHERE
+            tbl_name LIKE '". $table_name ."'
+         AND type!='meta'
+         AND sql NOT NULL
+         AND name NOT LIKE 'sqlite_%'
+         ORDER BY substr(type,2,1), name
+      ");
+
+      while($row = $this->db_fetch_object($result)) {
+         /* CREATE TABLE xx ( col1 int, col2 bool, col3 ...) */
+         if(strstr($row['sql'], " ". $column ." ") !== false)
+               return true;
+      }
+
+      return false;
+
+   } // db_check_column_exists()
+
    /**
     * PHPFSPOT_DB get connection status
     *
@@ -332,6 +364,74 @@ class PHPFSPOT_DB {
 
    } // getLastError()
 
-}
+   /**
+    * start transaction
+    *
+    * this will start a transaction on ACID-supporting database
+    * systems.
+    *
+    * @return bool
+    */
+   public function db_start_transaction()
+   {
+      if(!$this->getConnStatus())
+         return false;
+
+      $result = $this->db_exec("BEGIN");
+
+      /* Errors? */
+      if(PEAR::isError($result))
+         $this->throwError($result->getMessage() .' - '. $result->getUserInfo());
+
+      return true;
+
+   } // db_start_transaction()
+
+   /**
+    * commit transaction
+    *
+    * this will commit an ongoing transaction on ACID-supporting
+    * database systems
+    *
+    * @return bool
+    */
+   public function db_commit_transaction()
+   {
+      if(!$this->getConnStatus())
+         return false;
+
+      $result = $this->db_exec("COMMIT");
+
+      /* Errors? */
+      if(PEAR::isError($result))
+         $this->throwError($result->getMessage() .' - '. $result->getUserInfo());
+
+      return true;
+
+   } // db_commit_transaction()
+
+   /**
+    * rollback transaction()
+    *
+    * this function aborts a on going transaction
+    *
+    * @return bool
+    */
+   public function db_rollback_transaction()
+   {
+      if(!$this->getConnStatus())
+         return false;
+
+      $result = $this->db_exec("ROLLBACK");
+
+      /* Errors? */
+      if(PEAR::isError($result))
+         $this->throwError($result->getMessage() .' - '. $result->getUserInfo());
+
+      return true;
+
+   } // db_rollback_transaction()
+
+} // PHPFSPOT_DB
 
 ?>