* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
*
***************************************************************************/
-/* from pear "MDB2" package. use "pear install MDB2" if you don't have this! */
-require_once('MDB2.php');
-
class PHPFSPOT_DB {
private $db;
*/
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()
/**
*/
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()
{
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;
}
{
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!");
} // db_exec()
- 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
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
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
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()