Advertisement
Guest User

Untitled

a guest
May 1st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.21 KB | None | 0 0
  1. <?php
  2. class BaseDBAccess
  3. {
  4.     var $sDriver='';
  5.     var $sHost='';
  6.     var $sDatabase='';
  7.     var $sUser='';
  8.     var $sPassword='';
  9.     private static $oDatabase;
  10.     var $oPdo=null;
  11.     var $sQuery='';
  12.     var $oPDOStatement=null;
  13.    
  14.  
  15.     /**
  16.      * Constructor for database connection
  17.      * @param string $sDriver
  18.      * @param string $sHost
  19.      * @param string $sDatabase
  20.      * @param string $sUser
  21.      * @param string $sPassword
  22.      */
  23.      private function __construct($sDriver='',$sHost='',$sDatabase='',$sUser='',$sPassword='')
  24.      {
  25.             try{
  26.                 $this->setDriver($sDriver);
  27.                 $this->setHost($sHost);
  28.                 $this->setDatabase($sDatabase);
  29.                 $this->setUser($sUser);
  30.                 $this->setPassword($sPassword);
  31.                 $sDrive = $this->sDriver.':dbname='.$this->sDatabase.";host=".$this->sHost;
  32.                 $this->oPdo=new PDO($sDrive, $this->sUser, $this->sPassword);
  33.                 $this->oPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  34.             }catch(PDOException $e){
  35.                 echo 'Erreur : '.$e->getMessage().'<br />';
  36.                 echo 'N° : '.$e->getCode();
  37.             }
  38.             return $this->oPdo;
  39.      }
  40.        
  41.        
  42.     /**
  43.      * Method for setting all variables
  44.      * @return unknown_type
  45.      */
  46.     public function setDriver($sDriver){
  47.             $this->sDriver=$sDriver;
  48.     }
  49.    
  50.     public function setHost($sHost){
  51.             $this->sHost=$sHost;
  52.     }
  53.    
  54.     public function setDatabase($sDatabase){
  55.             $this->sDatabase=$sDatabase;
  56.     }
  57.    
  58.     public function setUser($sUser){
  59.             $this->sUser=$sUser;
  60.     }
  61.    
  62.     public function setPassword($sPassword){
  63.             $this->sPassword=$sPassword;
  64.     }
  65.    
  66.     public function setQuery($sQuery){
  67.             $this->sQuery=$sQuery;
  68.     }
  69.    
  70.     protected function setPdo($oPdo){
  71.             $this->oPdo=$oPdo;
  72.     }  
  73.        
  74.     /**
  75.      * To get instance of Database
  76.      *
  77.      * @param string $sDriver
  78.      * @param string $sHost
  79.      * @param string $sDatabase
  80.      * @param string $sUser
  81.      * @param string $sPassword
  82.      * @return Database object
  83.      */
  84.     public static function getInstance($sDriver='',$sHost='',$sDatabase='',$sUser='',$sPassword=''){
  85.         if(is_null(self::$oDatabase)){
  86.             self::$oDatabase = new BaseDBAccess($sDriver,$sHost,$sDatabase,$sUser,$sPassword);
  87.         }
  88.         return self::$oDatabase;
  89.     }  
  90.        
  91.  
  92.    /**
  93.     *  Initiates a transaction
  94.     * @return <bool>  */
  95.     public function beginTransaction()
  96.     {
  97.         return $this->oPdo->beginTransaction();
  98.     }
  99.    
  100.    
  101.     /* Commits a transaction
  102.     @return <bool> */
  103.     public function commit()
  104.     {
  105.         return $this->oPdo->commit();
  106.     }
  107.    
  108.     /* Commits a transaction
  109.     @return <bool> */
  110.     public function prepared($oPDOStatement)
  111.     {
  112.         $this->oPDOStatement =$oPDOStatement;
  113.         return $this->oPdo->prepare($oPDOStatement);
  114.     }
  115.    
  116.    
  117.     /* Fetch the SQLSTATE associated with the
  118.     last operation on the database handle
  119.     * @return <string> */
  120.     public function errorCode()
  121.     {
  122.         return $this->oPdo->errorCode();
  123.     }
  124.    
  125.     /* Fetch extended error information associated with
  126.     the last operation on the database handle
  127.     @return <array> */
  128.     public function erroInfo()
  129.     {
  130.         return $this->oPdo->errorInfo();
  131.     }
  132.    
  133.     /* Executes an SQL statement, return the number of affected row
  134.     @param <String> $statement
  135.     @return <int> */
  136.     public function exec($statement)
  137.     {
  138.         return $this->oPdo->exec($statement);
  139.     }  
  140.    
  141.     /* Rolls back a transaction
  142.     @return <bool> */
  143.     public function rollBack()
  144.     {
  145.         return $this->oPdo->rollBack();
  146.     }
  147.    
  148.     public function lastInsertId($sequence)
  149.     {
  150.         return $this->oPdo->lastInsertId($sequence);
  151.     }
  152.    
  153.    
  154.     //This function is specific for postgresql and will return the last insert id in insert query
  155.     public function pgsqlLastInsertId($sqlQuery, $pdoObject)
  156.     {
  157.          // Checks if query is an insert and gets table name
  158.          if( preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $sqlQuery, $tablename) )
  159.          {
  160.                 // Gets this table's last sequence value
  161.                 $query = "SELECT currval('" . $tablename[1] . "_id_seq') AS last_value";
  162.  
  163.                 $temp_q_id = $this->prepared($query);
  164.                 $temp_q_id->execute();
  165.  
  166.                 if($temp_q_id)
  167.                 {
  168.                         $temp_result = $temp_q_id->fetch(PDO::FETCH_ASSOC);
  169.                         return ( $temp_result ) ? $temp_result['last_value'] : false;
  170.                 }
  171.         }
  172.  
  173.         return false;
  174.     }
  175.    
  176.    
  177.      
  178.  } /*** end of class ***/
  179.  
  180.  
  181. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement