Advertisement
Guest User

databasehelper.php

a guest
Oct 29th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. <?php
  2.  
  3. class DatabaseException extends Exception
  4. {
  5.     const UNKNOWN = 0;
  6.     const BAD_CONNECT = 1;
  7.    
  8.     public function __construct($message, $code = 0, Exception $previous = null)
  9.     {
  10.         parent::__construct($message, $code, $previous);
  11.     }
  12. }
  13.  
  14. class DatabaseHelper
  15. {
  16.     private $conn = null;
  17.    
  18.     const DEFAULT_HOST = 'localhost';
  19.     const DEFAULT_PORT = 5432;
  20.     const DEFAULT_DATABASE = 'database';
  21.     const DEFAULT_USER = 'user';
  22.     const DEFAULT_PASSWORD = 'password';
  23.    
  24.     private $host;
  25.     private $port;
  26.     private $database;
  27.     private $user;
  28.     private $password;
  29.    
  30.     private $DSN;
  31.    
  32.     private static $gdbh = null;
  33.    
  34.     function __construct($host = DatabaseHelper::DEFAULT_HOST, $port = DatabaseHelper::DEFAULT_PORT,
  35.         $database = DatabaseHelper::DEFAULT_DATABASE, $user = DatabaseHelper::DEFAULT_USER, $use_password = TRUE,
  36.         $password = DatabaseHelper::DEFAULT_PASSWORD)
  37.     {
  38.         $this->host = $host;
  39.         $this->port = $port;
  40.         $this->database = $database;
  41.         $this->user = $user;
  42.         $this->password = $password;
  43.        
  44.         if ($use_password)
  45.         {
  46.             $this->DSN = sprintf("pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s",
  47.                 $this->host, $this->port, $this->database, $this->user, $this->password);
  48.         }
  49.         else
  50.         {
  51.             $this->DSN = sprintf("pgsql:host=%s;port=%d;dbname=%s;user=%s",
  52.                 $this->host, $this->port, $this->database, $this->user);       
  53.         }
  54.     }
  55.    
  56.     function __destruct()
  57.     {
  58.         $this->close();
  59.     }
  60.    
  61.     public function connect()
  62.     {
  63.         try
  64.         {
  65.             $this->conn = new PDO($this->DSN);
  66.         }
  67.         catch (PDOException $e)
  68.         {
  69.             $error_arr = $pstmt->errorInfo();
  70.             $error_str = "Could not connect to server. "
  71.                 . "SQLSTATE code: " . $error_arr[0]
  72.                 . ", Driver code: " . $error_arr[1]
  73.                 . ", Message: " . $error_arr[2];
  74.                
  75.             throw new DatabaseException($error_str, DatabaseException::BAD_CONNECT, $e);
  76.         }
  77.     }
  78.    
  79.     public function close()
  80.     {
  81.         $this->conn = null;
  82.     }
  83.    
  84.     public function getConnection()
  85.     {
  86.         if ($this->conn == null)
  87.         {
  88.             $this->connect();
  89.         }
  90.        
  91.         return $this->conn;
  92.     }
  93.    
  94.     public static function getInstance()
  95.     {
  96.         if (DatabaseHelper::$gdbh == null)
  97.         {
  98.             DatabaseHelper::$gdbh = new DatabaseHelper();
  99.         }
  100.        
  101.         return DatabaseHelper::$gdbh;
  102.     }
  103.    
  104. }
  105.  
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement