host = $host; $this->dbname = $dbname; $this->username = $username; $this->password = $password; try{ $this->pdo = new PDOWrapper('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8', $this->username, $this->password, [ \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::ATTR_TIMEOUT => 5, \PDO::ERRMODE_EXCEPTION => true ]); $this->pdo->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false); $this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); if($debug){ $this->pdo->enableDebug(); } // On signale que tout s'est bien passe $this->error = new Error(Err::Success); }catch(\Exception $e){ // On signale qu'il y a une erreur $this->error = new Error(Err::PDOConnection); } } /************************************************ **** Multiton Management (static) **** ************************************************/ /** ADDS A NEW CONNECTION * * @param String $label [optional] Database Label * * @return boolean If added successfully * */ private static function add(String $label='default') : bool{ $conf = self::conf(); /* [1] Default values =========================================================*/ /* (1) If label and no path */ if( $label !== 'default' && !isset($conf[$label]) ) return false; /* [3] Instanciates the driver =========================================================*/ try{ /* (1) If local -> instanciates with local configuration */ // if( !checkdnsrr($_SERVER['SERVER_NAME'], 'NS') ) if(!isset($conf[$label]['local']['debug'])){ $conf[$label]['local']['debug'] = false; } if(isset($_SESSION['CurrentDatabase']) && is_string($_SESSION['CurrentDatabase'])){ $conf[$label]['local']['dbname'] = $_SESSION['CurrentDatabase']; }else{ $_SESSION["CurrentDatabase"] = $conf[$label]['local']['dbname']; } self::$instance[$label] = new DatabaseDriver($conf[$label]['local']['host'], $conf[$label]['local']['dbname'], $conf[$label]['local']['user'], $conf[$label]['local']['password'],$conf[$label]['local']['debug']); return true ; }catch(\Exception $e){ /* (3) If fails */ return false; } } /** GET A DATABASE DRIVER INSTANCE * * @param String $label [optional] Driver's label * @throws \Exception * @return DatabaseDriver Multiton * */ public static function get(String $label='default') : DatabaseDriver{ $conf = self::conf(); /* [1] Checks arguments =========================================================*/ /* (1) If no label, or unknown label */ if( !isset(self::$instance[$label]) ){ /* (2.1) Try to add the configuration if exists */ if( isset($conf[$label]) ){ self::add($label); return self::get($label); } throw new \Exception('Database @label is incorrect.'); } /* [2] Returns instance =========================================================*/ return self::$instance[$label]; } /** retourne la connection statique * @param String|null $label * @throws \Exception * @return \PDO */ public static function getPDO(?String $label=null) : \PDO{ if(is_string($label)){ $instance = self::get($label); }else{ $instance = self::get(); } return $instance->pdo; } /** * @return \PDO */ public function pdo(){ return $this->pdo; } /** * @return array */ public function getConfig(){ return [ 'host' => $this->host, 'dbname' => $this->dbname, 'username' => $this->username, 'password' => $this->password ]; } /** * enable request stacking */ public function enableStacking(){ $this->pdo->enableStacking(); } /** * send all the stacked request and flush the stack */ public function flushStack(){ $this->pdo->executeStack(); } /** get all debug data * @return array */ public function getDebug() : array{ return $this->pdo->getDebug(); } /** * @return bool */ public function isDebugEnabled() : bool { return $this->pdo->isDebugEnabled(); } } ?>