/** * Class Constructor * * @param String $connType * @param String $dbHost * @param String $dbName * @param String $dbUser * @param String $dbPass * @param Integer $dbPort * @param String $connClass Must be 'mysql', 'mysqli', or 'PDO' for now. * @return void * @throws Exception * @SuppressWarnings indentation * @SuppressWarnings cyclomaticComplexity */ public function __construct($connType = NULL, $dbHost = NULL, $dbName = NULL, $dbUser = NULL, $dbPass = NULL, $dbPort = NULL, $connClass = 'mysqli', $createDb = false) { $oConfig = new Config($connType, $dbHost, $dbPort, $dbName, $dbUser, $dbPass); $this->_oConfig = $oConfig; switch ($connClass) { case 'mysql': $this->_dbh = mysql_connect($oConfig->getDbHost() . ':' . $oConfig->getDbPort(), $oConfig->getDbUser(), $oConfig->getDbPass()); if (!$this->_dbh) { throw new Exception('Error connecting to database server(' . $oConfig->getDbHost() . ')! : ' . mysql_error()); } $dbName = Tools::coalesce(array($oConfig->getDbName(), '')); if ($dbName !== '') { if (!mysql_select_db($dbName, $this->_dbh)) { throw new Exception('Database does not exist: ', $dbName); } } break; case 'mysqli': $mysqli = mysqli_init(); if (!$mysqli) { throw new DaoException("Failed to allocate connection class!"); } if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2)) { throw new DaoException('Failed setting connection timeout.'); } $result = $mysqli->real_connect($oConfig->getDbHost(), $oConfig->getDbUser(), $oConfig->getDbPass(), null, $oConfig->getDbPort()); if (!$result || $mysqli->connect_errno) { throw new DaoException('Error connecting to database server(' . $oConfig->getDbHost() . ')! : ' . $mysqli->connect_error); } $this->_dbh = $mysqli; if ($this->_dbh->connect_error) { throw new DaoException('Error connecting to database server(' . $oConfig->getDbHost() . ')! : ' . $this->_dbh->connect_error); } $this->_dbh->query("SET @@SESSION.SQL_MODE = 'ALLOW_INVALID_DATES'"); if (!$mysqli->select_db($oConfig->getDbName())) { if ($createDb) { $this->_createdDb = true; $this->_dbh->query("CREATE DATABASE IF NOT EXISTS " . $oConfig->getDbName()); if (!$mysqli->select_db($oConfig->getDbName())) { throw new DaoException("Database: {$oConfig->getDbName()} is missing. Please use resetDb.php to install the database."); } } else { throw new DaoException("Database: {$oConfig->getDbName()} is missing. Please use resetDb.php to install the database."); } } break; case 'PDO': // May throw PDOException by itself. $this->_dbh = new PDO($oConfig->get_dsn(), $oConfig->getDbPass()); if (!$this->_dbh) { throw new DaoException('Error connecting to database server(' . $oConfig->getDbHost() . ')!'); } break; default: throw new DaoException('Unknown connection class: ' . $connClass); } // END OF switch ( $connClass ) $this->_connectionClass = $connClass; }
/** * Constructor. Get singleton instance via \iveeCore\SDE::instance() instead. * * @param \mysqli $db is an optional reference to an existing DB connection object */ protected function __construct(\mysqli $db = null) { if (!isset($db)) { $db = new \mysqli(Config::getDbHost(), Config::getDbUser(), Config::getDbPw(), Config::getDbName(), Config::getDbPort()); if ($db->connect_error) { exit('Fatal Error: ' . $db->connect_error . PHP_EOL); } } elseif (!$db instanceof \mysqli) { exit('Fatal Error: parameter given is not a mysqli object' . PHP_EOL); } $this->db = $db; //ivee uses transactions so turn off autocommit $this->db->autocommit(false); //eve runs on UTC time $this->db->query("SET time_zone='+0:00';"); }