Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * Abstract class Database
- * Provide methods to handle database communication
- * @author NetanelEdri
- * @date 04/09/2011
- *
- **/
- abstract class Database
- {
- /**
- * current instance var ( singleton pattren )
- * @object
- **/
- public static $objInstance;
- /**
- * connection resource var
- * @object
- **/
- protected $connection;
- /**
- * last query var
- * @string
- **/
- protected $lastQuery;
- /**
- * connection mode
- * @bool
- **/
- protected $connectionMode;
- /**
- * get instance of the database
- * @object
- **/
- public static function getInstance($dbInfo)
- {
- if(!isset(self::$objInstance) || !is_object(self::$objInstance))
- {
- $Class = __CLASS__;
- self::$objInstance = new DatabaseDriver($dbInfo);
- }
- return self::$objInstance;
- }
- /**
- * contructror function
- **/
- protected function __construct($dbInfo)
- {
- $this->Connect($dbInfo);
- }
- /**
- * get affected rows
- * @int
- **/
- public function affRows($query = null)
- {
- return $this->connection->affected_rows;
- }
- /**
- * clone function ( singleton pattren )
- **/
- private function __clone()
- {
- die('Clone is not allowed !');
- }
- /**
- * destruct function
- **/
- public function __destruct()
- {
- if($this->connectionMode)
- $this->connection->close();
- }
- /**
- * commit the last transactions
- **/
- public function commit()
- {
- $this->connection->query("COMMIT");
- $this->connection->query("SET AUTOCOMMIT=1");
- }
- /**
- * rollback the last transactions
- **/
- public function rollback()
- {
- $this->connection->query("ROLLBACK");
- $this->connection->query("SET AUTOCOMMIT=1");
- }
- /**
- * get the last insert_id
- **/
- public function insert_id()
- {
- return $this->connection->insert_id;
- }
- /**
- * abstract functions
- **/
- abstract function Connect($dbInfo);
- abstract function query($string);
- abstract function fetch($query = null);
- abstract function update($array);
- abstract function insert($array);
- }
- class DatabaseDriver extends Database
- {
- /**
- * connect function
- **/
- private function Connect($dbInfo)
- {
- $this->connection = new mysqli($dbInfo['hostname'],$dbInfo['dbuser'],$dbInfo['dbpass'],$dbInfo['dbname']);
- if($this->connection->connect_error)
- die($this->connection->connect_error);
- else {
- $this->connection->set_charset($dbInfo['charset']);
- $this->connectionMode = true;
- }
- }
- /**
- * execute a query
- **/
- public function query($string)
- {
- $this->lastQuery = $string;
- return $this->connection->query($string);
- }
- /**
- * fetch query results
- * @object
- **/
- public function fetch($query = null)
- {
- if(is_null($query))
- {
- $q = $this->connection->query($this->lastQuery);
- return $q->fetch_object();
- }else {
- return $query->fetch_object();
- }
- }
- /**
- * execute a update query
- * @bool
- **/
- public function update($array)
- {
- $table = $array['table'];
- $where = (isset($array['where'])) ? " WHERE ".$array['where'] : null;
- $data = null;
- foreach($array as $col => $value)
- {
- if($col != "table")
- $data .= "`'".$col."'` = '" .$value. "',";
- }
- $data = substr($data,0,-1);
- $queryString = "UPDATE `".$table."` SET ".$data.$where;
- return ($this->query($queryString)) ? true : false;
- }
- /**
- * execute a insert query
- **/
- public function insert($array)
- {
- $table = $array['table'];
- $cols = null;
- $values = null;
- foreach($array as $col => $value)
- {
- if($col != "table")
- {
- $cols .= "`".$col."`,";
- $values .= "'" .$value. "',";
- }
- }
- $cols = substr($cols,0,-1);
- $values = substr($values,0,-1);
- $queryString = "INSERT INTO `".$table."` (" .$cols. ") VALUES(" .$values. ")";
- return ($this->query($queryString)) ? true : false;
- }
- }
- $DB = DatabaseDriver::getInstance(
- array(
- "hostname" => "localhost",
- "dbuser" => "",
- "dbpass" => "",
- "dbname" => "",
- "charset" => "utf8"
- )
- );
- ?>
Add Comment
Please, Sign In to add comment