From 78ab005390d15e706f68d2beabfa0a4ad79e0703 Mon Sep 17 00:00:00 2001
From: Arun Persaud <arun@nubati.net>
Date: Thu, 24 Nov 2011 10:06:32 -0800
Subject: option to use PDO connections to sqlite when connecting via php

---
 config.ini_template |  1 +
 getjson.php         | 28 +++++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/config.ini_template b/config.ini_template
index 6a36ebf..bdbf94d 100644
--- a/config.ini_template
+++ b/config.ini_template
@@ -4,6 +4,7 @@
 fspotdb="myphoto.db"
 origdb="/home/username/.config/f-spot-photos.db"
 dbprefix=""
+usePDO=0
 
 [local]
 dirprefix=""
diff --git a/getjson.php b/getjson.php
index ce0c6e0..cbb9fee 100644
--- a/getjson.php
+++ b/getjson.php
@@ -3,6 +3,7 @@
 /* parse ini -file */
 $iniarray=parse_ini_file("config.ini");
 $DBFILE=$iniarray["fspotdb"];
+$usePDO=$iniarray["usePDO"];
 /* end parse ini-file */
 
 if (isset($_REQUEST["O"]))
@@ -15,18 +16,29 @@ if (isset($_REQUEST["N"]))
 else
   $N= 25;
 
-$DB = new SQlite3($DBFILE);
+if($usePDO)
+  $DB = new PDO("sqlite:$DBFILE");
+else
+  $DB = new SQlite3($DBFILE);
 
 $result = $DB->query("SELECT * FROM photos LIMIT $O $N");
 
-sqlite_close(DB);
-
 $row = array();
 
 $i = 0;
 
-while($res = $result->fetchArray(SQLITE3_ASSOC)){
-    
+if(!$usePDO)
+  {
+    /* convert results into array */
+    $tmp=array();
+    while($res = $result->fetchArray(SQLITE3_ASSOC)){
+      $tmp[]=$res;
+    }
+    $result=$tmp;
+  }
+
+foreach ($result as $res){
+
   $row[$i] = $res;
  
   $i++;
@@ -36,5 +48,11 @@ while($res = $result->fetchArray(SQLITE3_ASSOC)){
 
 echo json_encode($row);
 
+/* close the database */
+if($usePDO)
+  $DB=null;
+else
+  sqlite_close($DB);
+
 ?>
 
-- 
cgit v1.2.3-18-g5258