mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
Merge branch 'master' of https://github.com/liu21st/think
This commit is contained in:
@@ -43,43 +43,30 @@ class Db {
|
|||||||
/**
|
/**
|
||||||
* 数据库连接参数解析
|
* 数据库连接参数解析
|
||||||
* @static
|
* @static
|
||||||
* @access public
|
* @access private
|
||||||
* @param mixed $config
|
* @param mixed $config
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
static public function parseConfig($config){
|
static private function parseConfig($config){
|
||||||
if(empty($config)) {
|
if(empty($config)) {
|
||||||
$config = Config::get();
|
$config = Config::get('database');
|
||||||
}
|
}
|
||||||
if(is_string($config)) {
|
if(is_string($config)) {
|
||||||
return self::parseDsn($config);
|
return self::parseDsn($config);
|
||||||
|
}else{
|
||||||
|
return $config;
|
||||||
}
|
}
|
||||||
return [
|
|
||||||
'dbms' => $config['db_type'],
|
|
||||||
'dsn' => $config['db_dsn'],
|
|
||||||
'username' => $config['db_user'],
|
|
||||||
'password' => $config['db_pwd'],
|
|
||||||
'hostname' => $config['db_host'],
|
|
||||||
'hostport' => $config['db_port'],
|
|
||||||
'database' => $config['db_name'],
|
|
||||||
'params' => $config['db_params'],
|
|
||||||
'charset' => $config['db_charset'],
|
|
||||||
'deploy' => $config['db_deploy'],
|
|
||||||
'socket' => $config['db_unix_socket'],
|
|
||||||
'debug' => $config['db_debug'],
|
|
||||||
'deploy' => $config['db_deploy'],
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 public
|
* @access private
|
||||||
* @param string $dsnStr
|
* @param string $dsnStr
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
static public 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) {
|
||||||
@@ -87,14 +74,16 @@ class Db {
|
|||||||
}
|
}
|
||||||
$dsn = [
|
$dsn = [
|
||||||
'dbms' => $info['scheme'],
|
'dbms' => $info['scheme'],
|
||||||
'username' => isset($info['user']) ? $info['user'] : '',
|
'connection' => [
|
||||||
'password' => isset($info['pass']) ? $info['pass'] : '',
|
'username' => isset($info['user']) ? $info['user'] : '',
|
||||||
'hostname' => isset($info['host']) ? $info['host'] : '',
|
'password' => isset($info['pass']) ? $info['pass'] : '',
|
||||||
'hostport' => isset($info['port']) ? $info['port'] : '',
|
'hostname' => isset($info['host']) ? $info['host'] : '',
|
||||||
'database' => isset($info['path']) ? substr($info['path'],1) : '',
|
'hostport' => isset($info['port']) ? $info['port'] : '',
|
||||||
'charset' => isset($info['fragment'])?$info['fragment']:'',
|
'database' => isset($info['path']) ? substr($info['path'],1) : '',
|
||||||
|
],
|
||||||
|
'charset' => isset($info['fragment'])?$info['fragment']:'utf8',
|
||||||
];
|
];
|
||||||
$dsn['dsn'] = ''; // 兼容配置信息数组
|
|
||||||
if(isset($info['query'])) {
|
if(isset($info['query'])) {
|
||||||
parse_str($info['query'],$dsn['params']);
|
parse_str($info['query'],$dsn['params']);
|
||||||
}else{
|
}else{
|
||||||
|
|||||||
@@ -36,7 +36,26 @@ abstract class Driver {
|
|||||||
// 当前连接ID
|
// 当前连接ID
|
||||||
protected $_linkID = null;
|
protected $_linkID = null;
|
||||||
// 数据库连接参数配置
|
// 数据库连接参数配置
|
||||||
protected $config = [];
|
protected $config = [
|
||||||
|
'dbms' => '', // 数据库类型
|
||||||
|
'connection' => [
|
||||||
|
'hostname' => '127.0.0.1', // 服务器地址
|
||||||
|
'database' => '', // 数据库名
|
||||||
|
'username' => 'root', // 用户名
|
||||||
|
'password' => '', // 密码
|
||||||
|
'hostport' => '', // 端口
|
||||||
|
'socket' => '',
|
||||||
|
'dsn' => '', //
|
||||||
|
],
|
||||||
|
'params' => [], // 数据库连接参数
|
||||||
|
'charset' => 'utf8', // 数据库编码默认采用utf8
|
||||||
|
'prefix' => '', // 数据库表前缀
|
||||||
|
'debug' => false, // 数据库调试模式
|
||||||
|
'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
|
||||||
|
'rw_separate' => false, // 数据库读写是否分离 主从式有效
|
||||||
|
'master_num' => 1, // 读写分离后 主服务器数量
|
||||||
|
'slave_no' => '', // 指定从服务器序号
|
||||||
|
];
|
||||||
// 数据库表达式
|
// 数据库表达式
|
||||||
protected $comparison = ['eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN'];
|
protected $comparison = ['eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=','notlike'=>'NOT LIKE','like'=>'LIKE','in'=>'IN','notin'=>'NOT IN'];
|
||||||
// 查询表达式
|
// 查询表达式
|
||||||
@@ -60,10 +79,7 @@ abstract class Driver {
|
|||||||
*/
|
*/
|
||||||
public function __construct($config=''){
|
public function __construct($config=''){
|
||||||
if(!empty($config)) {
|
if(!empty($config)) {
|
||||||
$this->config = $config;
|
$this->config = array_merge($this->config,$config);
|
||||||
if(empty($this->config['params'])) {
|
|
||||||
$this->config['params'] = [];
|
|
||||||
}
|
|
||||||
$this->config['params'] = $this->options+$this->config['params'];
|
$this->config['params'] = $this->options+$this->config['params'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,25 +90,23 @@ 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;
|
if(empty($config)) $config = $this->config['connection'];
|
||||||
try{
|
try{
|
||||||
if(empty($config['dsn'])) {
|
if(empty($config['dsn'])) {
|
||||||
$config['dsn'] = $config['dbms'].':dbname='.$config['database'].';host='.$config['hostname'];
|
$config['dsn'] = $this->config['dbms'].':dbname='.$config['database'].';host='.$config['hostname'];
|
||||||
if(!empty($config['hostport'])) {
|
if(!empty($config['hostport'])) {
|
||||||
$config['dsn'] .= ';port='.$config['hostport'];
|
$config['dsn'] .= ';port='.$config['hostport'];
|
||||||
}elseif(!empty($config['socket'])){
|
}elseif(!empty($config['socket'])){
|
||||||
$config['dsn'] .= ';unix_socket='.$config['socket'];
|
$config['dsn'] .= ';unix_socket='.$config['socket'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$config['params']);
|
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$this->config['params']);
|
||||||
}catch (\PDOException $e) {
|
}catch (\PDOException $e) {
|
||||||
E($e->getMessage());
|
E($e->getMessage());
|
||||||
}
|
}
|
||||||
if(!empty($config['charset'])) {
|
if(!empty($this->config['charset'])) {
|
||||||
$this->linkID[$linkNum]->exec('SET NAMES '.$config['charset']);
|
$this->linkID[$linkNum]->exec('SET NAMES '.$this->config['charset']);
|
||||||
}
|
}
|
||||||
// 注销数据库连接配置信息
|
|
||||||
if(1 != $config['deploy']) $this->config = [];
|
|
||||||
}
|
}
|
||||||
return $this->linkID[$linkNum];
|
return $this->linkID[$linkNum];
|
||||||
}
|
}
|
||||||
@@ -591,7 +605,7 @@ abstract class Driver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//将__TABLE_NAME__这样的字符串替换成正规的表名,并且带上前缀和后缀
|
//将__TABLE_NAME__这样的字符串替换成正规的表名,并且带上前缀和后缀
|
||||||
$joinStr = preg_replace("/__([A-Z_-]+)__/esU",Config::get('db_prefix').".strtolower('$1')",$joinStr);
|
$joinStr = preg_replace("/__([A-Z_-]+)__/esU",$this->config['prefix'].".strtolower('$1')",$joinStr);
|
||||||
return $joinStr;
|
return $joinStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -902,7 +916,7 @@ abstract class Driver {
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function initConnect($master=true) {
|
protected function initConnect($master=true) {
|
||||||
if(1 == $this->config['deploy'])
|
if(!empty($this->config['deploy']))
|
||||||
// 采用分布式数据库
|
// 采用分布式数据库
|
||||||
$this->_linkID = $this->multiConnect($master);
|
$this->_linkID = $this->multiConnect($master);
|
||||||
else
|
else
|
||||||
@@ -920,22 +934,22 @@ abstract class Driver {
|
|||||||
static $_config = [];
|
static $_config = [];
|
||||||
if(empty($_config)) {
|
if(empty($_config)) {
|
||||||
// 缓存分布式数据库配置解析
|
// 缓存分布式数据库配置解析
|
||||||
foreach ($this->config as $key=>$val){
|
foreach ($this->config['connection'] as $key=>$val){
|
||||||
$_config[$key] = explode(',',$val);
|
$_config[$key] = explode(',',$val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 数据库读写是否分离
|
// 数据库读写是否分离
|
||||||
if(Config::get('db_rw_separate')){
|
if($this->config['rw_separate']){
|
||||||
// 主从式采用读写分离
|
// 主从式采用读写分离
|
||||||
if($master)
|
if($master)
|
||||||
// 主服务器写入
|
// 主服务器写入
|
||||||
$r = floor(mt_rand(0,Config::get('db_master_num')-1));
|
$r = floor(mt_rand(0,$this->config['master_num']-1));
|
||||||
else{
|
else{
|
||||||
if(is_numeric(Config::get('db_slave_no'))) {// 指定服务器读
|
if(is_numeric($this->config['slave_no'])) {// 指定服务器读
|
||||||
$r = Config::get('db_slave_no');
|
$r = $this->config['slave_no'];
|
||||||
}else{
|
}else{
|
||||||
// 读操作连接从服务器
|
// 读操作连接从服务器
|
||||||
$r = floor(mt_rand(Config::get('db_master_num'),count($_config['hostname'])-1)); // 每次随机连接的数据库
|
$r = floor(mt_rand($this->config['master_num'],count($_config['hostname'])-1)); // 每次随机连接的数据库
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
@@ -949,7 +963,6 @@ abstract class Driver {
|
|||||||
'hostport' => isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0],
|
'hostport' => isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0],
|
||||||
'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0],
|
'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0],
|
||||||
'dsn' => isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0],
|
'dsn' => isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0],
|
||||||
'params' => isset($_config['params'][$r])?$_config['params'][$r]:$_config['params'][0],
|
|
||||||
];
|
];
|
||||||
return $this->connect($db_config,$r);
|
return $this->connect($db_config,$r);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ class Model {
|
|||||||
|
|
||||||
// 设置表前缀
|
// 设置表前缀
|
||||||
if(empty($this->tablePrefix)) {
|
if(empty($this->tablePrefix)) {
|
||||||
$this->tablePrefix = is_null($this->tablePrefix)?'':C('db_prefix');
|
$this->tablePrefix = is_null($this->tablePrefix)?'':C('database.prefix');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 数据库初始化操作
|
// 数据库初始化操作
|
||||||
|
|||||||
@@ -37,22 +37,29 @@ return [
|
|||||||
'var_jsonp_handler' => 'callback',
|
'var_jsonp_handler' => 'callback',
|
||||||
|
|
||||||
/* 错误设置 */
|
/* 错误设置 */
|
||||||
'error_message' => '页面错误!请稍后再试~',//错误显示信息,非调试模式有效
|
'error_message' => '页面错误!请稍后再试~',//错误显示信息,非调试模式有效
|
||||||
'error_page' => '', // 错误定向页面
|
'error_page' => '', // 错误定向页面
|
||||||
'show_error_msg' => false, // 显示错误信息
|
'show_error_msg' => false, // 显示错误信息
|
||||||
|
|
||||||
/* 数据库设置 */
|
/* 数据库设置 */
|
||||||
'db_type' => 'mysql', // 数据库类型
|
'database' => [
|
||||||
'db_host' => 'localhost', // 服务器地址
|
'dbms' => 'mysql', // 数据库类型
|
||||||
'db_name' => '', // 数据库名
|
'connection' => [
|
||||||
'db_user' => 'root', // 用户名
|
'dsn' => '', //
|
||||||
'db_pwd' => '', // 密码
|
'hostname' => 'localhost', // 服务器地址
|
||||||
'db_port' => '', // 端口
|
'database' => '', // 数据库名
|
||||||
'db_prefix' => 'think_', // 数据库表前缀
|
'username' => 'root', // 用户名
|
||||||
'db_fields_cache' => true, // 启用字段缓存
|
'password' => '', // 密码
|
||||||
'db_charset' => 'utf8', // 数据库编码默认采用utf8
|
'hostport' => '', // 端口
|
||||||
'db_deploy_type' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
|
'socket' => '',
|
||||||
'db_rw_separate' => false, // 数据库读写是否分离 主从式有效
|
],
|
||||||
'db_master_num' => 1, // 读写分离后 主服务器数量
|
'params' => [], // 数据库连接参数
|
||||||
'db_slave_no' => '', // 指定从服务器序号
|
'charset' => 'utf8', // 数据库编码默认采用utf8
|
||||||
|
'prefix' => '', // 数据库表前缀
|
||||||
|
'debug' => false, // 数据库调试模式
|
||||||
|
'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
|
||||||
|
'rw_separate' => false, // 数据库读写是否分离 主从式有效
|
||||||
|
'master_num' => 1, // 读写分离后 主服务器数量
|
||||||
|
'slave_no' => '', // 指定从服务器序号
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user