/** * 取得数据库类实例 * @static * @access public * @param mixed $config 连接配置 * @param boolean $lite 是否lite方式 * @return Object 返回数据库驱动类 */ public static function instance($config = [], $lite = false) { $md5 = md5(serialize($config)); if (!isset(self::$instances[$md5])) { // 解析连接参数 支持数组和字符串 $options = self::parseConfig($config); // 如果采用lite方式 仅支持原生SQL 包括query和execute方法 $class = $lite ? '\\think\\db\\Lite' : (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\driver\\') . ucwords($options['type']); self::$instances[$md5] = new $class($options); } self::$instance = self::$instances[$md5]; return self::$instance; }
/** * 取得数据库类实例 * @static * @access public * @param mixed $config 连接配置 * @return Object 返回数据库驱动类 */ public static function instance($config = []) { $md5 = md5(serialize($config)); if (!isset(self::$instances[$md5])) { // 解析连接参数 支持数组和字符串 $options = self::parseConfig($config); if (empty($options['type'])) { throw new Exception('db type error'); } $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\driver\\') . ucwords($options['type']); self::$instances[$md5] = new $class($options); } self::$instance = self::$instances[$md5]; return self::$instance; }
/** * 数据库初始化 并取得数据库类实例 * @static * @access public * @param mixed $config 连接配置 * @return Object 返回数据库驱动类 */ public static function connect($config = []) { $md5 = md5(serialize($config)); if (!isset(self::$instances[$md5])) { // 解析连接参数 支持数组和字符串 $options = self::parseConfig($config); if (empty($options['type'])) { throw new Exception('db type error'); } $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\driver\\') . ucwords($options['type']); self::$instances[$md5] = new $class($options); // 记录初始化信息 APP_DEBUG && Log::record('[ DB ] INIT ' . $options['type'] . ':' . var_export($options, true), 'info'); } self::$instance = self::$instances[$md5]; return self::$instance; }