issue75, support for database access via PDO sqlite
authorAndreas Unterkircher <unki@netshadow.at>
Thu, 1 Nov 2007 16:21:32 +0000 (16:21 +0000)
committerAndreas Unterkircher <unki@netshadow.at>
Thu, 1 Nov 2007 16:21:32 +0000 (16:21 +0000)
git-svn-id: file:///var/lib/svn/phpfspot/trunk@283 fa6a889d-dae6-447d-9e79-4ba9a3039384

phpfspot_cfg.php.dist
phpfspot_db.php

index e3bb645eae480feabdd187ece73f8010aa650467..1abedc8740fb9f3fe7b63b9dbdde5d3e44cccb54 100644 (file)
@@ -33,6 +33,9 @@ class PHPFSPOT_CFG {
    var $web_path = "/phpfspot";
 
    var $theme_name = "default";
    var $web_path = "/phpfspot";
 
    var $theme_name = "default";
+   /* database access via "native" sqlite3 support or via "pdo" */
+   var $db_access = "native";
 
    /* it's enough if this database is readonly for the webserver */
    var $fspot_db = "/var/www/f-spot-dbs/photos.db";
 
    /* it's enough if this database is readonly for the webserver */
    var $fspot_db = "/var/www/f-spot-dbs/photos.db";
@@ -95,6 +98,9 @@ class PHPFSPOT_CFG {
       if(!isset($this->fspot_db) || $this->fspot_db == "")
          $this->showError("Please set \$fspot_db in phpfspot_cfg");
 
       if(!isset($this->fspot_db) || $this->fspot_db == "")
          $this->showError("Please set \$fspot_db in phpfspot_cfg");
 
+      if(!isset($this->db_access) || $this->db_access == "")
+         $this->showError("Please set \$db_access in phpfspot_cfg");
+
       if(!isset($this->phpfspot_db) || $this->phpfspot_db == "")
          $this->showError("Please set \$phpfspot_db in phpfspot_cfg");
 
       if(!isset($this->phpfspot_db) || $this->phpfspot_db == "")
          $this->showError("Please set \$phpfspot_db in phpfspot_cfg");
 
index a3f1234653201ed7aaf5c7bb26c8e3cc67eaf7f3..11ce7c8fc6c970222c06db572a64ec70ae9f9816 100644 (file)
@@ -65,14 +65,29 @@ class PHPFSPOT_DB {
     */
    private function db_connect()
    {
     */
    private function db_connect()
    {
-      if(($this->db = sqlite3_open($this->db_path)) === false) {
+      switch($this->parent->cfg->db_access) {
+         case 'native':
+            if(($this->db = sqlite3_open($this->db_path)) === false) {
+               $this->throwError("Unable to connect to database:" . $this->getLastError());
+               $this->setConnStatus(false);
+            }
+            else {
+               $this->setConnStatus(true);
+            }
+            break;
+         case 'pdo':
+            try {
+               $this->db =  new PDO("sqlite:".$this->db_path);
+               $this->setConnStatus(true);
+            }
+            catch (Exception $e) {
+               $this->throwError("Unable to connect to database:" . $e->getMessage());
+               $this->setConnStatus(false);
+            }
+            break;
 
 
-         $this->throwError("Unable to connect to database:" . $this->getLastError());
-         $this->setConnStatus(false);
       }
 
       }
 
-      $this->setConnStatus(true);
-
    } // db_connect()
 
    /**
    } // db_connect()
 
    /**
@@ -82,8 +97,15 @@ class PHPFSPOT_DB {
     */
    private function db_disconnect()
    {
     */
    private function db_disconnect()
    {
-      if($this->getConnStatus())
-         sqlite3_close($this->db);
+      switch($this->parent->cfg->db_access) {
+         case 'native':
+            if($this->getConnStatus())
+               sqlite3_close($this->db);
+            break;
+         case 'pdo':
+            $this->db = NULL;
+            break;
+      }
 
    } // db_disconnect()
 
 
    } // db_disconnect()
 
@@ -97,8 +119,23 @@ class PHPFSPOT_DB {
    {
       if($this->getConnStatus()) {
 
    {
       if($this->getConnStatus()) {
 
-         if(($result = sqlite3_query($this->db, $query)) === false)
-            $this->ThrowError($this->getLastError());
+         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;
       }
        
          return $result;
       }
@@ -116,9 +153,20 @@ class PHPFSPOT_DB {
    {
       if($this->getConnStatus()) {
 
    {
       if($this->getConnStatus()) {
 
-         if(($result = sqlite3_exec($this->db, $query)) === false)
-            $this->ThrowError($this->getLastError());
-
+         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;
+         }
       }
       else 
          $this->ThrowError("Can't execute query - we are not connected!");
       }
       else 
          $this->ThrowError("Can't execute query - we are not connected!");
@@ -127,7 +175,12 @@ class PHPFSPOT_DB {
 
    public function db_fetch_object($resource)
    {
 
    public function db_fetch_object($resource)
    {
-      return sqlite3_fetch_array($resource);
+      switch($this->parent->cfg->db_access) {
+         case 'native':
+            return sqlite3_fetch_array($resource);
+         case 'pdo':
+            return $resource->fetch();
+      }
 
    } // db_fetch_object
 
 
    } // db_fetch_object
 
@@ -142,7 +195,14 @@ class PHPFSPOT_DB {
       if($this->getConnStatus()) {
 
          $result = $this->db_query($query);
       if($this->getConnStatus()) {
 
          $result = $this->db_query($query);
-         $row = $result->fetchRow();
+         switch($this->parent->cfg->db_access) {
+            case 'native':
+               $row = $result->fetchRow();
+               break;
+            case 'pdo':
+               $row = $result[0];
+               break;
+         }
          return $row;
       }
       else 
          return $row;
       }
       else 
@@ -195,10 +255,21 @@ class PHPFSPOT_DB {
       if($this->getConnStatus()) {
 
          $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
       if($this->getConnStatus()) {
 
          $result = $this->db_query("SELECT name FROM sqlite_master WHERE type='table'");
-         while($table = $this->db_fetch_object($result)) {
-            if($table['name'] == $table_name)
-               return true;
-         }
+         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;
       }
       else
          return false;
       }
       else
@@ -251,7 +322,12 @@ class PHPFSPOT_DB {
 
    private function getLastError()
    {
 
    private function getLastError()
    {
-      return sqlite3_error($this->db);
+      switch($this->parent->cfg->db_access) {
+         case 'native':
+            return sqlite3_error($this->db);
+         case 'pdo':
+            return "". $this->db->errorInfo();
+      }
 
    } // getLastError()
 
 
    } // getLastError()