Guest User

Untitled

a guest
Oct 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.84 KB | None | 0 0
  1. <?php
  2. /**
  3.  *
  4.  * Abstract class Database
  5.  * Provide methods to handle database communication
  6.  * @author NetanelEdri
  7.  * @date 04/09/2011
  8.  *
  9. **/
  10.  
  11. abstract class Database
  12. {
  13.     /**
  14.      * current instance var ( singleton pattren )
  15.      * @object
  16.     **/
  17.     public static $objInstance;
  18.  
  19.     /**
  20.      * connection resource var
  21.      * @object
  22.     **/
  23.     protected $connection;
  24.  
  25.     /**
  26.      * last query var
  27.      * @string
  28.     **/
  29.     protected $lastQuery;
  30.  
  31.     /**
  32.      * connection mode
  33.      * @bool
  34.     **/
  35.     protected $connectionMode;
  36.    
  37.     /**
  38.      * get instance of the database
  39.      * @object
  40.     **/
  41.     public static function getInstance($dbInfo)
  42.     {
  43.         if(!isset($this->objInstance) || !is_object($this->objInstance))
  44.         {
  45.             $Class = __CLASS__;
  46.             self::$objInstance = new $Class($dbInfo);
  47.         }
  48.         return self::$objInstance;
  49.     }
  50.    
  51.     /**
  52.      * contructror function
  53.     **/
  54.     protected function __construct($dbInfo)
  55.     {
  56.         $this->Connect($dbInfo);
  57.     }
  58.    
  59.     /**
  60.      * get affected rows
  61.      * @int
  62.     **/
  63.     public function affRows($query = null)
  64.     {
  65.         return $this->connection->affected_rows;
  66.     }
  67.    
  68.     /**
  69.      * clone function ( singleton pattren )
  70.     **/
  71.     final private function clone()
  72.     {
  73.         die('Clone is not allowed !');
  74.     }
  75.    
  76.     /**
  77.      * destruct function
  78.     **/
  79.     public function __destruct()
  80.     {
  81.         if($this->connectionMode)
  82.             $this->connection->close();
  83.     }
  84.    
  85.     /**
  86.      * commit the last transactions
  87.     **/
  88.     public function commit()
  89.     {
  90.         $this->connection->query("COMMIT");
  91.         $this->connection->query("SET AUTOCOMMIT=1");  
  92.     }
  93.    
  94.     /**
  95.      * rollback the last transactions
  96.     **/
  97.     public function rollback()
  98.     {
  99.         $this->connection->query("ROLLBACK");
  100.         $this->connection->query("SET AUTOCOMMIT=1");  
  101.     }
  102.    
  103.     /**
  104.      * get the last insert_id
  105.     **/
  106.     public function insert_id()
  107.     {
  108.         return $this->connection->insert_id;
  109.     }
  110.    
  111.     /**
  112.      * abstract functions
  113.     **/
  114.     abstract function Connect(array $dbInfo);
  115.     abstract function query(string $string);
  116.     abstract function fetch($query = null);
  117.     abstract function update(array $array);
  118.     abstract function insert(array $array);
  119. }
  120.  
  121. class DatabaseDriver extends Database
  122. {
  123.     /**
  124.      * connect function
  125.     **/
  126.     public function Connect($dbInfo)
  127.     {
  128.         $this->connection = new mysqli($dbInfo['hostname'],$dbInfo['dbuser'],$dbInfo['dbpass'],$dbInfo['dbname']);
  129.         if($this->connection->connect_error)
  130.             die($this->connection->connect_error);
  131.         else {
  132.             $this->connection->set_charset($dbInfo['charset']);
  133.             $this->connectionMode = true;
  134.         }
  135.     }
  136.    
  137.     /**
  138.      * execute a query
  139.     **/
  140.     public function query($string)
  141.     {
  142.         $this->lastQuery = $string;
  143.         return ($this->objInstance->query($string)) ? true : false;
  144.     }
  145.    
  146.     /**
  147.      * fetch query results
  148.      * @object
  149.     **/
  150.     public function fetch($query = null)
  151.     {
  152.         if(is_null($query))
  153.         {
  154.             $q = $this->query($this->lastQuery);
  155.             return $this->connection->$q->fetch_object();
  156.         }else {
  157.             return $this->connection->$query->fetch_object();
  158.         }
  159.     }
  160.    
  161.     /**
  162.      * execute a update query
  163.      * @bool
  164.     **/
  165.     public function update($array)
  166.     {
  167.         $table = $array['table'];
  168.         $where = (isset($array['where'])) ? " WHERE ".$array['where'] : null;
  169.         $data = null;
  170.         foreach($array as $col => $value)
  171.         {
  172.             if($col != "table")
  173.                 $data .= "`'".$col."'` = '" .$value. "',";
  174.         }
  175.         $data = substr($data,0,-1);
  176.         $queryString = "UPDATE `".$table."` SET ".$data.$where;
  177.         return ($this->query($queryString)) ? true : false;
  178.     }
  179.    
  180.     /**
  181.      * execute a insert query
  182.     **/
  183.     public function insert($array)
  184.     {
  185.         $table = $array['table'];
  186.         $cols = null;
  187.         $values = null;
  188.         foreach($array as $col => $value)
  189.         {
  190.             if($col != "table")
  191.             {
  192.                 $cols .= "`".$col."`,";
  193.                 $values .= "'" .$value. "',";
  194.             }
  195.         }
  196.         $cols = substr($cols,0,-1);
  197.         $values = substr($values,0,-1);
  198.         $queryString = "INSERT INTO `".$table."` (" .$cols. ") VALUES(" .$values. ")";
  199.         return ($this->query($queryString)) ? true : false;
  200.     }
  201. }
  202.  
  203.  
  204. ?>
Add Comment
Please, Sign In to add comment