public static function getInstance() { if (empty(self::$instance)) { self::$instance = new self(); } return self::$instance; }
public static function getInstance() { //判断self::$instance 是否已经被实例化 if (!self::$instance instanceof self) { self::$instance = new self(); } return self::$instance; }
public static function connect() { try { self::$db = new \PDO('mysql:' . 'host=' . \Config::$db['host'] . ';' . 'dbname=' . \Config::$db['dbname'], \Config::$db['username'], \Config::$db['password'], [\PDO::ATTR_PERSISTENT => \Config::$db['persistent']]); } catch (\PDOException $e) { http_response_code(500); exit('DB connection error: ' . $e->getMessage()); } self::$db->query('SET time_zone = "+00:00"'); }
/** * 单例模式,创建db * @param object $config 配置文件 * @return object DB连接 */ static function getDb() { if (!self::$_db instanceof self) { //提取config $config = (require_once 'Config.php'); extract($config); self::$_db = new DB($driver, $host, $dbname, $user, $password); } return self::$_db; }
public function __construct($type = null, $start = null, $offset = false, $where = 1) { self::$db = CoreDB::init(); if (is_null($type)) { throw new CESyntaxError('Select type of aggregate.'); } if (!class_exists($type)) { throw new CETypeError('Cannot find specified type of aggregate.'); } $this->type = $type; $this->load($start, $offset, $where); }
/** * Connecting with database. * * Function create an singleton object, and creates database connection. * Connection handler is stored in self::$_object->db variable. * * @param string $type database type. Only 'mysql' like for now. * @return object PDO * @throws CESyntaxError if incorrect database type * @throws CEDBError if problem with connect with database * * @access public * @static */ public static function init($type = 'mysql') { if (!isset(self::$_object)) { try { switch ($type) { case 'mysql': $dsn = sprintf('mysql:host=%s;dbname=%s', DB_HOST, DB_NAME); self::$_object = new PDO($dsn, DB_USER, DB_PASS); break; default: throw new CESyntaxError('Invalid database type.'); } self::$_object->setAttribute(PDO::ATTR_AUTOCOMMIT, true); self::$_object->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$_object->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); self::$_object->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); } catch (PDOException $e) { throw new CEDBError(sprintf('Connection failed: %s.', $e->getMessage())); } } return self::$_object; }
/** * Constructor * * Creates prepared statements with acquire bindings. * * @access private */ private function __construct() { $this->_db = CoreDB::init(); $this->_stmt[self::ST_SEL] = $this->_db->prepare(sprintf("\n SELECT\n `value`\n FROM\n %s\n WHERE\n `key` = :key", TBL_CONFIG)); $this->_stmt[self::ST_DB] = $this->_db->prepare(sprintf("\n REPLACE\n %s\n SET\n `key` = :key,\n `value` = :value", TBL_CONFIG)); $this->_stmt[self::ST_DEL] = $this->_db->prepare(sprintf("\n DELETE FROM\n %s\n WHERE\n `key` = :key", TBL_CONFIG)); }
public static function setConfig($config) { self::$config = $config; }
public static function close() { self::$__instance->close(); self::$__instance = NULL; return TRUE; }
public function &database($config = NULL, $is_return = false, $force_new_conn = false) { $core_db = self::$system['db']; $db_cfg_key = $core_db['active_group']; if (is_string($config) && !empty($config)) { //传递配置key $db_cfg = $core_db[$config]; } elseif (is_array($config)) { //传递配置 $db_cfg = $config; } else { //没有传递配置,使用默认配置 $db_cfg = $core_db[$db_cfg_key]; } if ($is_return) { return CoreDB::getInstance($db_cfg, $force_new_conn); } else { if ($force_new_conn || !is_object($this->db) || !is_null($config)) { $this->db = CoreDB::getInstance($db_cfg, $force_new_conn); } return $this->db; } }
public function __construct() { $this->db = CoreDB::connect(); }
/** * Constructor * * Initialize database connection. */ public function __construct() { self::$db = CoreDB::init(); }
public final function __destruct() { self::$instance = null; }