mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
数据库配置信息调整
This commit is contained in:
@@ -1,99 +1,97 @@
|
|||||||
<?php
|
<?php
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Author: liu21st <liu21st@gmail.com>
|
// | Author: liu21st <liu21st@gmail.com>
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
namespace Think;
|
namespace Think;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ThinkPHP 数据库中间层实现类
|
* ThinkPHP 数据库中间层实现类
|
||||||
*/
|
*/
|
||||||
class Db {
|
class Db {
|
||||||
|
|
||||||
static private $instance = []; // 数据库连接实例
|
static private $instance = []; // 数据库连接实例
|
||||||
static private $_instance = null; // 当前数据库连接实例
|
static private $_instance = null; // 当前数据库连接实例
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取得数据库类实例
|
* 取得数据库类实例
|
||||||
* @static
|
* @static
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $config 连接配置
|
* @param mixed $config 连接配置
|
||||||
* @param boolean $lite 是否lite方式
|
* @param boolean $lite 是否lite方式
|
||||||
* @return Object 返回数据库驱动类
|
* @return Object 返回数据库驱动类
|
||||||
*/
|
*/
|
||||||
static public function instance($config=[],$lite=false) {
|
static public function instance($config=[],$lite=false) {
|
||||||
$md5 = md5(serialize($config));
|
$md5 = md5(serialize($config));
|
||||||
if(!isset(self::$instance[$md5])) {
|
if(!isset(self::$instance[$md5])) {
|
||||||
// 解析连接参数 支持数组和字符串
|
// 解析连接参数 支持数组和字符串
|
||||||
$options = self::parseConfig($config);
|
$options = self::parseConfig($config);
|
||||||
// 如果采用lite方式 仅支持原生SQL 包括query和execute方法
|
// 如果采用lite方式 仅支持原生SQL 包括query和execute方法
|
||||||
$class = $lite? 'Think\Db\Lite' : 'Think\\Db\\Driver\\'.ucwords($options['dbms']);
|
$class = $lite? 'Think\Db\Lite' : 'Think\\Db\\Driver\\'.ucwords($options['type']);
|
||||||
self::$instance[$md5] = new $class($options);
|
self::$instance[$md5] = new $class($options);
|
||||||
}
|
}
|
||||||
self::$_instance = self::$instance[$md5];
|
self::$_instance = self::$instance[$md5];
|
||||||
return self::$_instance;
|
return self::$_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库连接参数解析
|
* 数据库连接参数解析
|
||||||
* @static
|
* @static
|
||||||
* @access private
|
* @access private
|
||||||
* @param mixed $config
|
* @param mixed $config
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
static private function parseConfig($config){
|
static private function parseConfig($config){
|
||||||
if(empty($config)) {
|
if(empty($config)) {
|
||||||
$config = Config::get('database');
|
$config = Config::get('database');
|
||||||
}
|
}
|
||||||
if(is_string($config)) {
|
if(is_string($config)) {
|
||||||
return self::parseDsn($config);
|
return self::parseDsn($config);
|
||||||
}else{
|
}else{
|
||||||
return $config;
|
return $config;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DSN解析
|
* DSN解析
|
||||||
* 格式: mysql://username:passwd@localhost:3306/DbName?param1=val1¶m2=val2#utf8
|
* 格式: mysql://username:passwd@localhost:3306/DbName?param1=val1¶m2=val2#utf8
|
||||||
* @static
|
* @static
|
||||||
* @access private
|
* @access private
|
||||||
* @param string $dsnStr
|
* @param string $dsnStr
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
static private function parseDsn($dsnStr) {
|
static private function parseDsn($dsnStr) {
|
||||||
if( empty($dsnStr) ){return false;}
|
if( empty($dsnStr) ){return false;}
|
||||||
$info = parse_url($dsnStr);
|
$info = parse_url($dsnStr);
|
||||||
if(!$info) {
|
if(!$info) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$dsn = [
|
$dsn = [
|
||||||
'dbms' => $info['scheme'],
|
'type' => $info['scheme'],
|
||||||
'connection' => [
|
'username' => isset($info['user']) ? $info['user'] : '',
|
||||||
'username' => isset($info['user']) ? $info['user'] : '',
|
'password' => isset($info['pass']) ? $info['pass'] : '',
|
||||||
'password' => isset($info['pass']) ? $info['pass'] : '',
|
'hostname' => isset($info['host']) ? $info['host'] : '',
|
||||||
'hostname' => isset($info['host']) ? $info['host'] : '',
|
'hostport' => isset($info['port']) ? $info['port'] : '',
|
||||||
'hostport' => isset($info['port']) ? $info['port'] : '',
|
'database' => isset($info['path']) ? substr($info['path'],1) : '',
|
||||||
'database' => isset($info['path']) ? substr($info['path'],1) : '',
|
'charset' => isset($info['fragment'])?$info['fragment']:'utf8',
|
||||||
],
|
];
|
||||||
'charset' => isset($info['fragment'])?$info['fragment']:'utf8',
|
|
||||||
];
|
if(isset($info['query'])) {
|
||||||
|
parse_str($info['query'],$dsn['params']);
|
||||||
if(isset($info['query'])) {
|
}else{
|
||||||
parse_str($info['query'],$dsn['params']);
|
$dsn['params'] = [];
|
||||||
}else{
|
}
|
||||||
$dsn['params'] = [];
|
return $dsn;
|
||||||
}
|
}
|
||||||
return $dsn;
|
|
||||||
}
|
// 调用驱动类的方法
|
||||||
|
static public function __callStatic($method, $params){
|
||||||
// 调用驱动类的方法
|
return call_user_func_array(array(self::$_instance, $method), $params);
|
||||||
static public function __callStatic($method, $params){
|
}
|
||||||
return call_user_func_array(array(self::$_instance, $method), $params);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -37,16 +37,13 @@ abstract class Driver {
|
|||||||
protected $_linkID = null;
|
protected $_linkID = null;
|
||||||
// 数据库连接参数配置
|
// 数据库连接参数配置
|
||||||
protected $config = [
|
protected $config = [
|
||||||
'dbms' => '', // 数据库类型
|
'type' => '', // 数据库类型
|
||||||
'connection' => [
|
'hostname' => '127.0.0.1', // 服务器地址
|
||||||
'hostname' => '127.0.0.1', // 服务器地址
|
'database' => '', // 数据库名
|
||||||
'database' => '', // 数据库名
|
'username' => '', // 用户名
|
||||||
'username' => 'root', // 用户名
|
'password' => '', // 密码
|
||||||
'password' => '', // 密码
|
'hostport' => '', // 端口
|
||||||
'hostport' => '', // 端口
|
'dsn' => '', //
|
||||||
'socket' => '',
|
|
||||||
'dsn' => '', //
|
|
||||||
],
|
|
||||||
'params' => [], // 数据库连接参数
|
'params' => [], // 数据库连接参数
|
||||||
'charset' => 'utf8', // 数据库编码默认采用utf8
|
'charset' => 'utf8', // 数据库编码默认采用utf8
|
||||||
'prefix' => '', // 数据库表前缀
|
'prefix' => '', // 数据库表前缀
|
||||||
@@ -91,12 +88,12 @@ abstract class Driver {
|
|||||||
*/
|
*/
|
||||||
public function connect($config='',$linkNum=0) {
|
public function connect($config='',$linkNum=0) {
|
||||||
if ( !isset($this->linkID[$linkNum]) ) {
|
if ( !isset($this->linkID[$linkNum]) ) {
|
||||||
if(empty($config)) $config = $this->config['connection'];
|
if(empty($config)) $config = $this->config;
|
||||||
try{
|
try{
|
||||||
if(empty($config['dsn'])) {
|
if(empty($config['dsn'])) {
|
||||||
$config['dsn'] = $this->parseDsn($config);
|
$config['dsn'] = $this->parseDsn($config);
|
||||||
}
|
}
|
||||||
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$this->config['params']);
|
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$config['params']);
|
||||||
}catch (\PDOException $e) {
|
}catch (\PDOException $e) {
|
||||||
E($e->getMessage());
|
E($e->getMessage());
|
||||||
}
|
}
|
||||||
@@ -989,9 +986,12 @@ abstract class Driver {
|
|||||||
static $_config = [];
|
static $_config = [];
|
||||||
if(empty($_config)) {
|
if(empty($_config)) {
|
||||||
// 缓存分布式数据库配置解析
|
// 缓存分布式数据库配置解析
|
||||||
foreach ($this->config['connection'] as $key=>$val){
|
$_config['username'] = explode(',',$$this->config['username']);
|
||||||
$_config[$key] = explode(',',$val);
|
$_config['password'] = explode(',',$$this->config['password']);
|
||||||
}
|
$_config['hostname'] = explode(',',$$this->config['hostname']);
|
||||||
|
$_config['hostport'] = explode(',',$$this->config['hostport']);
|
||||||
|
$_config['database'] = explode(',',$$this->config['database']);
|
||||||
|
$_config['dsn'] = explode(',',$$this->config['dsn']);
|
||||||
}
|
}
|
||||||
// 数据库读写是否分离
|
// 数据库读写是否分离
|
||||||
if($this->config['rw_separate']){
|
if($this->config['rw_separate']){
|
||||||
|
|||||||
127
convention.php
127
convention.php
@@ -1,65 +1,62 @@
|
|||||||
<?php
|
<?php
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
// | Author: liu21st <liu21st@gmail.com>
|
// | Author: liu21st <liu21st@gmail.com>
|
||||||
// +----------------------------------------------------------------------
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
// 惯例配置文件
|
// 惯例配置文件
|
||||||
return [
|
return [
|
||||||
'app_debug' => true, // 调试模式
|
'app_debug' => true, // 调试模式
|
||||||
'app_status' => 'debug',// 调试模式状态
|
'app_status' => 'debug',// 调试模式状态
|
||||||
'var_module' => 'm', // 模块变量名
|
'var_module' => 'm', // 模块变量名
|
||||||
'var_controller' => 'c', // 控制器变量名
|
'var_controller' => 'c', // 控制器变量名
|
||||||
'var_action' => 'a', // 操作变量名
|
'var_action' => 'a', // 操作变量名
|
||||||
'var_pathinfo' => 's', // PATHINFO变量名 用于兼容模式
|
'var_pathinfo' => 's', // PATHINFO变量名 用于兼容模式
|
||||||
'pathinfo_fetch' => 'ORIG_PATH_INFO,REDIRECT_PATH_INFO,REDIRECT_URL',
|
'pathinfo_fetch' => 'ORIG_PATH_INFO,REDIRECT_PATH_INFO,REDIRECT_URL',
|
||||||
'pathinfo_depr' => '/', // pathinfo分隔符
|
'pathinfo_depr' => '/', // pathinfo分隔符
|
||||||
'require_module' => true, // 是否显示模块
|
'require_module' => true, // 是否显示模块
|
||||||
'default_module' => 'index', // 默认模块名
|
'default_module' => 'index', // 默认模块名
|
||||||
'require_controller' => true, // 是否显示控制器
|
'require_controller' => true, // 是否显示控制器
|
||||||
'default_controller' => 'index', // 默认控制器名
|
'default_controller' => 'index', // 默认控制器名
|
||||||
'default_action' => 'index', // 默认操作名
|
'default_action' => 'index', // 默认操作名
|
||||||
'action_suffix' => '', // 操作方法后缀
|
'action_suffix' => '', // 操作方法后缀
|
||||||
'url_model' => 1, // URL模式
|
'url_model' => 1, // URL模式
|
||||||
'base_url' => $_SERVER["SCRIPT_NAME"], // 基础URL路径
|
'base_url' => $_SERVER["SCRIPT_NAME"], // 基础URL路径
|
||||||
'url_html_suffix' => '.html',
|
'url_html_suffix' => '.html',
|
||||||
'url_params_bind' => false, // url变量绑定
|
'url_params_bind' => false, // url变量绑定
|
||||||
'exception_tmpl' => THINK_PATH.'Tpl/think_exception.tpl',// 异常页面的模板文件
|
'exception_tmpl' => THINK_PATH.'Tpl/think_exception.tpl',// 异常页面的模板文件
|
||||||
'error_tmpl' => THINK_PATH.'Tpl/dispatch_jump.tpl', // 默认错误跳转对应的模板文件
|
'error_tmpl' => THINK_PATH.'Tpl/dispatch_jump.tpl', // 默认错误跳转对应的模板文件
|
||||||
'success_tmpl' => THINK_PATH.'Tpl/dispatch_jump.tpl', // 默认成功跳转对应的模板文件
|
'success_tmpl' => THINK_PATH.'Tpl/dispatch_jump.tpl', // 默认成功跳转对应的模板文件
|
||||||
'default_ajax_return' => 'JSON', // 默认AJAX 数据返回格式,可选JSON XML ...
|
'default_ajax_return' => 'JSON', // 默认AJAX 数据返回格式,可选JSON XML ...
|
||||||
'default_jsonp_handler' => 'jsonpReturn', // 默认JSONP格式返回的处理方法
|
'default_jsonp_handler' => 'jsonpReturn', // 默认JSONP格式返回的处理方法
|
||||||
'var_jsonp_handler' => 'callback',
|
'var_jsonp_handler' => 'callback',
|
||||||
|
|
||||||
/* 错误设置 */
|
/* 错误设置 */
|
||||||
'error_message' => '页面错误!请稍后再试~',//错误显示信息,非调试模式有效
|
'error_message' => '页面错误!请稍后再试~',//错误显示信息,非调试模式有效
|
||||||
'error_page' => '', // 错误定向页面
|
'error_page' => '', // 错误定向页面
|
||||||
'show_error_msg' => false, // 显示错误信息
|
'show_error_msg' => false, // 显示错误信息
|
||||||
|
|
||||||
/* 数据库设置 */
|
/* 数据库设置 */
|
||||||
'database' => [
|
'database' => [
|
||||||
'dbms' => 'mysql', // 数据库类型
|
'type' => 'mysql', // 数据库类型
|
||||||
'connection' => [
|
'dsn' => '', //
|
||||||
'dsn' => '', //
|
'hostname' => 'localhost', // 服务器地址
|
||||||
'hostname' => 'localhost', // 服务器地址
|
'database' => '', // 数据库名
|
||||||
'database' => '', // 数据库名
|
'username' => 'root', // 用户名
|
||||||
'username' => 'root', // 用户名
|
'password' => '', // 密码
|
||||||
'password' => '', // 密码
|
'hostport' => '', // 端口
|
||||||
'hostport' => '', // 端口
|
'params' => [], // 数据库连接参数
|
||||||
'socket' => '',
|
'charset' => 'utf8', // 数据库编码默认采用utf8
|
||||||
],
|
'prefix' => '', // 数据库表前缀
|
||||||
'params' => [], // 数据库连接参数
|
'debug' => false, // 数据库调试模式
|
||||||
'charset' => 'utf8', // 数据库编码默认采用utf8
|
'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
|
||||||
'prefix' => '', // 数据库表前缀
|
'rw_separate' => false, // 数据库读写是否分离 主从式有效
|
||||||
'debug' => false, // 数据库调试模式
|
'master_num' => 1, // 读写分离后 主服务器数量
|
||||||
'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
|
'slave_no' => '', // 指定从服务器序号
|
||||||
'rw_separate' => false, // 数据库读写是否分离 主从式有效
|
],
|
||||||
'master_num' => 1, // 读写分离后 主服务器数量
|
];
|
||||||
'slave_no' => '', // 指定从服务器序号
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|||||||
Reference in New Issue
Block a user