| // | Demian Turner | // | James Floyd | // +---------------------------------------------------------------------------+ $projectMnemonic = $GLOBALS['_STR']['CONF']['project']['projectMnemonic']; $GLOBALS[$projectMnemonic]['CONNECTIONS'] = array(); #require_once 'DB.php'; /** * Class for handling DB resources. * * @author Demian Turner */ class STR_DB { /** * Returns a singleton DB handle. * * example usage: * $dbh =& STR_DB::singleton(); * Warning: In order to work correctly, DB handle singleton must be * instantiated statically and by reference. * * @static * @param string $dsn The datasource details if supplied: see {@link DB::parseDSN()} for format * @return mixed Reference to DB resource or false on failure to connect */ function &singleton($dsn = null) { if (is_null($dsn)) { $dsn = STR_DB::getDsn(); } $projectMnemonic = $GLOBALS['_STR']['CONF']['project']['projectMnemonic']; $dsnMd5 = md5($dsn); $aConnections = array_keys($GLOBALS[$projectMnemonic]['CONNECTIONS']); if (!(count($aConnections)) || !(in_array($dsnMd5, $aConnections))) { $GLOBALS[$projectMnemonic]['CONNECTIONS'][$dsnMd5] = DB::connect($dsn); // If DB connect fails and we're installing, return error if (DB::isError($GLOBALS[$projectMnemonic]['CONNECTIONS'][$dsnMd5])) { die('Cannot connect to DB, check your credentials'); } $GLOBALS[$projectMnemonic]['CONNECTIONS'][$dsnMd5]->setFetchMode(DB_FETCHMODE_OBJECT); } return $GLOBALS[$projectMnemonic]['CONNECTIONS'][$dsnMd5]; } /** * Returns the default DSN specified in the global config. * * @static * @return mixed A string or array containing the data source name. */ function getDsn($excludeDbName = false) { $conf = $alternateConf = $GLOBALS['_STR']['CONF']; $dbType = $conf['db']['type']; if ($dbType == 'mysql') { $alternateConf['db']['type'] = 'mysql_SGL'; } require_once dirname(__FILE__) . '/../../lib/SGL/DB.php'; $dsn = SGL_DB::_getDsnAsString($alternateConf, $excludeDbName); // override SGL dsn with temporary testing one $GLOBALS['_SGL']['CONF']['db'] = $conf['db']; return $dsn; } } ?>