/** * get instance of singleton * * @access public * @static * @param array $options * @throws Exception * @return zerobin_db */ public static function getInstance($options = null) { // if needed initialize the singleton if (!self::$_instance instanceof zerobin_db) { self::$_instance = new self(); } if (is_array($options)) { // set table prefix if given if (array_key_exists('tbl', $options)) { self::$_prefix = $options['tbl']; } // initialize the db connection with new options if (array_key_exists('dsn', $options) && array_key_exists('usr', $options) && array_key_exists('pwd', $options) && array_key_exists('opt', $options)) { self::$_db = new PDO($options['dsn'], $options['usr'], $options['pwd'], $options['opt']); // check if the database contains the required tables self::$_type = strtolower(substr($options['dsn'], 0, strpos($options['dsn'], ':'))); switch (self::$_type) { case 'ibm': $sql = 'SELECT tabname FROM SYSCAT.TABLES '; break; case 'informix': $sql = 'SELECT tabname FROM systables '; break; case 'mssql': $sql = "SELECT name FROM sysobjects " . "WHERE type = 'U' ORDER BY name"; break; case 'mysql': $sql = 'SHOW TABLES'; break; case 'oci': $sql = 'SELECT table_name FROM all_tables'; break; case 'pgsql': $sql = "SELECT c.relname AS table_name " . "FROM pg_class c, pg_user u " . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' " . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) " . "AND c.relname !~ '^(pg_|sql_)' " . "UNION " . "SELECT c.relname AS table_name " . "FROM pg_class c " . "WHERE c.relkind = 'r' " . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) " . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) " . "AND c.relname !~ '^pg_'"; break; case 'sqlite': $sql = "SELECT name FROM sqlite_master WHERE type='table' " . "UNION ALL SELECT name FROM sqlite_temp_master " . "WHERE type='table' ORDER BY name"; break; default: throw new Exception('PDO type ' . self::$_type . ' is currently not supported.'); } $statement = self::$_db->query($sql); $tables = $statement->fetchAll(PDO::FETCH_COLUMN, 0); // create paste table if needed if (!array_key_exists(self::$_prefix . 'paste', $tables)) { self::$_db->exec('CREATE TABLE ' . self::$_prefix . 'paste ( ' . 'dataid CHAR(16), ' . 'data TEXT, ' . 'postdate INT, ' . 'expiredate INT, ' . 'opendiscussion INT, ' . 'burnafterreading INT );'); } // create comment table if needed if (!array_key_exists(self::$_prefix . 'comment', $tables)) { self::$_db->exec('CREATE TABLE ' . self::$_prefix . 'comment ( ' . 'dataid CHAR(16), ' . 'pasteid CHAR(16), ' . 'parentid CHAR(16), ' . 'data TEXT, ' . 'nickname VARCHAR(255), ' . 'vizhash TEXT, ' . 'postdate INT );'); } } } return parent::$_instance; }
/** * get instance of singleton * * @access public * @static * @param array $options * @throws Exception * @return zerobin_db */ public static function getInstance($options = null) { // if needed initialize the singleton if (!self::$_instance instanceof zerobin_db) { self::$_instance = new self(); } if (is_array($options)) { // set table prefix if given if (array_key_exists('tbl', $options)) { self::$_prefix = $options['tbl']; } // initialize the db connection with new options if (array_key_exists('dsn', $options) && array_key_exists('usr', $options) && array_key_exists('pwd', $options) && array_key_exists('opt', $options)) { // set default options $options['opt'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; $options['opt'][PDO::ATTR_EMULATE_PREPARES] = false; $options['opt'][PDO::ATTR_PERSISTENT] = true; $db_tables_exist = true; // setup type and dabase connection self::$_type = strtolower(substr($options['dsn'], 0, strpos($options['dsn'], ':'))); $tableQuery = self::_getTableQuery(self::$_type); self::$_db = new PDO($options['dsn'], $options['usr'], $options['pwd'], $options['opt']); // check if the database contains the required tables $tables = self::$_db->query($tableQuery)->fetchAll(PDO::FETCH_COLUMN, 0); // create paste table if necessary if (!in_array(self::$_prefix . 'paste', $tables)) { self::_createPasteTable(); $db_tables_exist = false; } // create comment table if necessary if (!in_array(self::$_prefix . 'comment', $tables)) { self::_createCommentTable(); $db_tables_exist = false; } // create config table if necessary $db_version = zerobin::VERSION; if (!in_array(self::$_prefix . 'config', $tables)) { self::_createConfigTable(); // if we only needed to create the config table, the DB is older then 0.22 if ($db_tables_exist) { $db_version = '0.21'; } } else { $db_version = self::_getConfig('VERSION'); } // update database structure if necessary if (version_compare($db_version, zerobin::VERSION, '<')) { self::_upgradeDatabase($db_version); } } } return self::$_instance; }