dmBridge PHP API
DMMySQLDataStore.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 # Copyright © 2009, 2010 Board of Regents of the Nevada System of Higher
00005 # Education, on behalf of the University of Nevada, Las Vegas
00006 #
00007 
00015 class DMMySQLDataStore extends DMPDODataStore implements DMDataStore {
00016 
00017    private static $instance;
00018 
00019    protected $dbcon, $driver = 'pdo_mysql';
00020    private $dbhost, $dbport = 3306, $dbname, $dbuser, $dbpass;
00021 
00022 
00027    public static function destroyInstance() {
00028       self::$instance = null;
00029    }
00030 
00035    public static function getInstance() {
00036       if (!self::$instance instanceof self) {
00037          self::$instance = new self;
00038       }
00039       return self::$instance;
00040    }
00041 
00042    protected function __construct() {
00043       parent::__construct();
00044       DMDataStoreFactory::setDataStore('pdo_mysql');
00045       $this->dbhost = DMConfigIni::getInstance()->getString(
00046             "dmbridge.database.mysql.host");
00047       if (DMConfigIni::getInstance()->getString("dmbridge.database.mysql.port")) {
00048          $this->dbport = DMConfigIni::getInstance()->getString(
00049                "dmbridge.database.mysql.port");
00050       }
00051       $this->dbname = DMConfigIni::getInstance()->getString(
00052             "dmbridge.database.mysql.name");
00053       $this->dbuser = DMConfigIni::getInstance()->getString(
00054             "dmbridge.database.mysql.username");
00055       $this->dbpass = DMConfigIni::getInstance()->getString(
00056             "dmbridge.database.mysql.password");
00057    }
00058 
00059    public function __sleep() {
00060       return array('driver', 'dbhost', 'dbport', 'dbname', 'dbuser', 'dbpass');
00061    }
00062 
00067    public function getConnection() {
00068       if (!$this->dbcon) {
00069          try {
00070             if ($this->dbhost == "localhost" && $this->dbport = 3306) {
00071                $socket_path = ini_get("mysql.default_socket");
00072                if (!file_exists($socket_path)) {
00073                   throw new PDOException(
00074                         sprintf(
00075                            DMLocalizedString::getString("MISSING_FILE"),
00076                               $socket_path));
00077                }
00078             }
00079             $this->dbcon = @new PDO(
00080                   $this->getDSN(), $this->dbuser, $this->dbpass);
00081             $this->dbcon->setAttribute(PDO::ATTR_ERRMODE,
00082                PDO::ERRMODE_EXCEPTION);
00083             $this->dbcon->setAttribute(PDO::ATTR_TIMEOUT, 10);
00084             $this->createTables();
00085          } catch (PDOException $e) {
00086             $e = new DMPDOException($e);
00087             throw $e;
00088          }
00089       }
00090       return $this->dbcon;
00091    }
00092 
00093    private function getDSN() {
00094       $dsn = "";
00095       $params = array();
00096       $params['host'] = $this->dbhost;
00097       $params['port'] = $this->dbport;
00098       $params['dbname'] = $this->dbname;
00099       foreach ($params as $key => $value) {
00100          if ($value) {
00101             $dsn .= $key . "=" . $value . ";";
00102          }
00103       }
00104       $dsn = "mysql:" . rtrim($dsn, ";");
00105       return $dsn;
00106    }
00107 
00111    public function getType() {
00112       return DMDataStoreType::PDO_MySQL;
00113    }
00114 
00115 }
 All Data Structures Functions Variables