From 61f2970e221f8612e51e7802cc127819c4caa369 Mon Sep 17 00:00:00 2001 From: Andreas Unterkircher Date: Mon, 4 Jun 2007 18:32:41 +0000 Subject: initial import git-svn-id: file:///var/lib/svn/phpfspot/trunk@2 fa6a889d-dae6-447d-9e79-4ba9a3039384 --- phpfspot_db.php | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 phpfspot_db.php (limited to 'phpfspot_db.php') diff --git a/phpfspot_db.php b/phpfspot_db.php new file mode 100644 index 0000000..96648be --- /dev/null +++ b/phpfspot_db.php @@ -0,0 +1,247 @@ +parent = $parent; + $this->db_path = $db_path; + + /* We are starting disconnected */ + $this->setConnStatus(false); + + /* Connect to MySQL Database */ + $this->db_connect(); + + } // __construct() + + /** + * PHPFSPOT_DB class deconstructor + * + * This destructor will close the current database connection. + */ + public function __destruct() + { + $this->db_disconnect(); + + } // _destruct() + + /** + * PHPFSPOT_DB database connect + * + * This function will connect to the database via MDB2 + */ + private function db_connect() + { + if(($this->db = sqlite3_open($this->db_path)) === false) { + + $this->throwError("Unable to connect to database:" . $this->getLastError()); + $this->setConnStatus(false); + } + + $this->setConnStatus(true); + + } // db_connect() + + /** + * PHPFSPOT_DB database disconnect + * + * This function will disconnected an established database connection. + */ + private function db_disconnect() + { + if($this->getConnStatus()) + sqlite3_close($this->db); + + } // db_disconnect() + + /** + * PHPFSPOT_DB database query + * + * This function will execute a SQL query and return the result as + * object. + */ + public function db_query($query = "") + { + if($this->getConnStatus()) { + + if(($result = sqlite3_query($this->db, $query)) === false) + $this->trowError($this->getLastError()); + + return $result; + } + else + $this->ThrowError("Can't execute query - we are not connected!"); + + } // db_query() + + public function db_fetch_object(&$resource) + { + return sqlite3_fetch_array($resource); + + } // db_fetch_object + + /** + * PHPFSPOT_DB fetch ONE row + * + * This function will execute the given but only return the + * first result. + */ + public function db_fetchSingleRow($query = "") + { + if($this->getConnStatus()) { + + $result = $this->db_query($query); + $row = $result->fetchRow(); + return $row; + } + else + $this->ThrowError("Can't fetch row - we are not connected!"); + + } // db_fetchSingleRow() + + /** + * PHPFSPOT_DB number of affected rows + * + * This functions returns the number of affected rows but the + * given SQL query. + */ + public function db_getNumRows($query = "") + { + /* Execute query */ + $result = $this->db_query($query); + + /* Errors? */ + if(PEAR::isError($result)) + $this->throwError($result->getMessage()); + + return $result->numRows(); + + } // 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 + * + * This function checks if the given table exists in the + * database + * @param string, table name + * @return true if table found otherwise false + */ + public function db_check_table_exists($table_name = "") + { + if($this->getConnStatus()) { + $result = $this->db_query("SHOW TABLES"); + $tables_in = "Tables_in_". MYSQL_DB; + + while($row = $result->fetchRow()) { + if($row->$tables_in == $table_name) + return true; + } + return false; + } + else + $this->ThrowError("Can't check table - we are not connected!"); + + } // db_check_table_exists() + + /** + * PHPFSPOT_DB get connection status + * + * This function checks the internal state variable if already + * connected to database. + */ + private function setConnStatus($status) + { + $this->is_connected = $status; + + } // setConnStatus() + + /** + * PHPFSPOT_DB set connection status + * This function sets the internal state variable to indicate + * current database connection status. + */ + private function getConnStatus() + { + return $this->is_connected; + + } // getConnStatus() + + /** + * PHPFSPOT_DB throw error + * + * This function shows up error messages and afterwards through exceptions. + */ + private function ThrowError($string) + { + if(!defined('DB_NOERROR')) { + print "

". $string ."

\n"; + try { + throw new Exception; + } + catch(Exectpion $e) { + } + } + + $this->last_error = $string; + + } // ThrowError() + + private function getLastError() + { + + return sqlite3_error_string(sqlite3_last_error($this->db)); + + } // getLastError() + +} + +?> -- cgit v1.2.3-18-g5258