X-Git-Url: https://git.nubati.net/cgi-bin/gitweb.cgi?p=phpfspot.git;a=blobdiff_plain;f=phpfspot_db.php;h=807cf0feb36ca072307497ba4d920208b4b27b3c;hp=fba87b3dc818b4eb172067fb9a0f610709c274a2;hb=6a603f724e4ab19bf1d9626549f0e95d9570e5ab;hpb=b97020d32344f5309d0f85c55d679b0139a70a86;ds=sidebyside diff --git a/phpfspot_db.php b/phpfspot_db.php index fba87b3..807cf0f 100644 --- a/phpfspot_db.php +++ b/phpfspot_db.php @@ -28,6 +28,7 @@ class PHPFSPOT_DB { private $parent; private $is_connected; private $last_error; + private $last_query; /** * PHPFSPOT_DB class constructor @@ -42,7 +43,7 @@ class PHPFSPOT_DB { /* We are starting disconnected */ $this->setConnStatus(false); - /* Connect to MySQL Database */ + /* Connect to database */ $this->db_connect(); } // __construct() @@ -61,18 +62,34 @@ class PHPFSPOT_DB { /** * PHPFSPOT_DB database connect * - * This function will connect to the database via MDB2 + * This function will connect to the database */ 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 { + sqlite3_create_function($this->db, 'basename', 1, 'basename'); + $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() /** @@ -82,8 +99,15 @@ class PHPFSPOT_DB { */ 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() @@ -96,9 +120,26 @@ class PHPFSPOT_DB { public function db_query($query = "") { if($this->getConnStatus()) { + + $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; - if(($result = sqlite3_query($this->db, $query)) === false) - $this->ThrowError($this->getLastError()); + } return $result; } @@ -116,18 +157,36 @@ class PHPFSPOT_DB { { if($this->getConnStatus()) { - if(($result = sqlite3_exec($this->db, $query)) === false) - $this->ThrowError($this->getLastError()); - + $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; + } } 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 @@ -142,7 +201,14 @@ class PHPFSPOT_DB { if($this->getConnStatus()) { $result = $this->db_query($query); - $row = $result->fetchRow(); + switch($this->parent->cfg->db_access) { + case 'native': + $row = $this->db_fetch_object($result); + break; + case 'pdo': + $row = $result->fetch(); + break; + } return $row; } else @@ -169,19 +235,6 @@ class PHPFSPOT_DB { } // db_getNumRows() - /** - * PHPFSPOT_DB get primary key - * - * This function returns the primary key of the last - * operated insert SQL query. - */ - public function db_getid() - { - /* Get the last primary key ID from execute query */ - return mysql_insert_id($this->db->connection); - - } // db_getid() - /** * PHPFSPOT_DB check table exists * @@ -195,10 +248,21 @@ class PHPFSPOT_DB { 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 @@ -237,6 +301,7 @@ class PHPFSPOT_DB { private function ThrowError($string) { if(!defined('DB_NOERROR')) { + print "Error during query: ". $this->last_query ."

\n"; print "

". $string ."

\n"; try { throw new Exception; @@ -251,7 +316,12 @@ class PHPFSPOT_DB { 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()