/** * 取得数据库类实例 * @static * @access public * @param mixed $config 连接配置 * @return Object 返回数据库驱动类 */ public static function getInstance($config = array()) { $md5 = md5(serialize($config)); if (!isset(self::$instance[$md5])) { // 解析连接参数 支持数组和字符串 $options = self::parseConfig($config); // 兼容mysqli if ('mysqli' == $options['type']) { $options['type'] = 'mysql'; } // 如果采用lite方式 仅支持原生SQL 包括query和execute方法 $class = !empty($options['lite']) ? 'Core\\db\\Lite' : 'Core\\db\\driver\\' . ucwords(strtolower($options['type'])); if (class_exists($class)) { self::$instance[$md5] = new $class($options); } else { // 类没有定义 //TODO E(L('_NO_DB_DRIVER_').': ' . $class); } } self::$_instance = self::$instance[$md5]; return self::$_instance; }
/** * @param string $host * @param string $username * @param string $password * @param string $db * @param int $port * @param string $charset */ public function __construct($host = null, $username = null, $password = null, $db = null, $port = null, $charset = 'utf8') { $isSubQuery = false; // if params were passed as array if (is_array($host)) { foreach ($host as $key => $val) { ${$key} = $val; } } // if host were set as mysqli socket if (is_object($host)) { $this->_mysqli = $host; } else { $this->host = $host; } $this->username = $username; $this->password = $password; $this->db = $db; $this->port = $port; $this->charset = $charset; if ($isSubQuery) { $this->isSubQuery = true; return; } if (isset($prefix)) { $this->setPrefix($prefix); } self::$_instance = $this; }