Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class BaseDBAccess
- {
- var $sDriver='';
- var $sHost='';
- var $sDatabase='';
- var $sUser='';
- var $sPassword='';
- private static $oDatabase;
- var $oPdo=null;
- var $sQuery='';
- var $oPDOStatement=null;
- /**
- * Constructor for database connection
- * @param string $sDriver
- * @param string $sHost
- * @param string $sDatabase
- * @param string $sUser
- * @param string $sPassword
- */
- private function __construct($sDriver='',$sHost='',$sDatabase='',$sUser='',$sPassword='')
- {
- try{
- $this->setDriver($sDriver);
- $this->setHost($sHost);
- $this->setDatabase($sDatabase);
- $this->setUser($sUser);
- $this->setPassword($sPassword);
- $sDrive = $this->sDriver.':dbname='.$this->sDatabase.";host=".$this->sHost;
- $this->oPdo=new PDO($sDrive, $this->sUser, $this->sPassword);
- $this->oPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }catch(PDOException $e){
- echo 'Erreur : '.$e->getMessage().'<br />';
- echo 'N° : '.$e->getCode();
- }
- return $this->oPdo;
- }
- /**
- * Method for setting all variables
- * @return unknown_type
- */
- public function setDriver($sDriver){
- $this->sDriver=$sDriver;
- }
- public function setHost($sHost){
- $this->sHost=$sHost;
- }
- public function setDatabase($sDatabase){
- $this->sDatabase=$sDatabase;
- }
- public function setUser($sUser){
- $this->sUser=$sUser;
- }
- public function setPassword($sPassword){
- $this->sPassword=$sPassword;
- }
- public function setQuery($sQuery){
- $this->sQuery=$sQuery;
- }
- protected function setPdo($oPdo){
- $this->oPdo=$oPdo;
- }
- /**
- * To get instance of Database
- *
- * @param string $sDriver
- * @param string $sHost
- * @param string $sDatabase
- * @param string $sUser
- * @param string $sPassword
- * @return Database object
- */
- public static function getInstance($sDriver='',$sHost='',$sDatabase='',$sUser='',$sPassword=''){
- if(is_null(self::$oDatabase)){
- self::$oDatabase = new BaseDBAccess($sDriver,$sHost,$sDatabase,$sUser,$sPassword);
- }
- return self::$oDatabase;
- }
- /**
- * Initiates a transaction
- * @return <bool> */
- public function beginTransaction()
- {
- return $this->oPdo->beginTransaction();
- }
- /* Commits a transaction
- @return <bool> */
- public function commit()
- {
- return $this->oPdo->commit();
- }
- /* Commits a transaction
- @return <bool> */
- public function prepared($oPDOStatement)
- {
- $this->oPDOStatement =$oPDOStatement;
- return $this->oPdo->prepare($oPDOStatement);
- }
- /* Fetch the SQLSTATE associated with the
- last operation on the database handle
- * @return <string> */
- public function errorCode()
- {
- return $this->oPdo->errorCode();
- }
- /* Fetch extended error information associated with
- the last operation on the database handle
- @return <array> */
- public function erroInfo()
- {
- return $this->oPdo->errorInfo();
- }
- /* Executes an SQL statement, return the number of affected row
- @param <String> $statement
- @return <int> */
- public function exec($statement)
- {
- return $this->oPdo->exec($statement);
- }
- /* Rolls back a transaction
- @return <bool> */
- public function rollBack()
- {
- return $this->oPdo->rollBack();
- }
- public function lastInsertId($sequence)
- {
- return $this->oPdo->lastInsertId($sequence);
- }
- //This function is specific for postgresql and will return the last insert id in insert query
- public function pgsqlLastInsertId($sqlQuery, $pdoObject)
- {
- // Checks if query is an insert and gets table name
- if( preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $sqlQuery, $tablename) )
- {
- // Gets this table's last sequence value
- $query = "SELECT currval('" . $tablename[1] . "_id_seq') AS last_value";
- $temp_q_id = $this->prepared($query);
- $temp_q_id->execute();
- if($temp_q_id)
- {
- $temp_result = $temp_q_id->fetch(PDO::FETCH_ASSOC);
- return ( $temp_result ) ? $temp_result['last_value'] : false;
- }
- }
- return false;
- }
- } /*** end of class ***/
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement