This commit is contained in:
麦当苗儿
2013-04-18 21:25:38 +08:00
4 changed files with 74 additions and 65 deletions

View File

@@ -43,43 +43,30 @@ class Db {
/**
* 数据库连接参数解析
* @static
* @access public
* @access private
* @param mixed $config
* @return array
*/
static public function parseConfig($config){
static private function parseConfig($config){
if(empty($config)) {
$config = Config::get();
$config = Config::get('database');
}
if(is_string($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解析
* 格式: mysql://username:passwd@localhost:3306/DbName?param1=val1&param2=val2#utf8
* @static
* @access public
* @access private
* @param string $dsnStr
* @return array
*/
static public function parseDsn($dsnStr) {
static private function parseDsn($dsnStr) {
if( empty($dsnStr) ){return false;}
$info = parse_url($dsnStr);
if(!$info) {
@@ -87,14 +74,16 @@ class Db {
}
$dsn = [
'dbms' => $info['scheme'],
'username' => isset($info['user']) ? $info['user'] : '',
'password' => isset($info['pass']) ? $info['pass'] : '',
'hostname' => isset($info['host']) ? $info['host'] : '',
'hostport' => isset($info['port']) ? $info['port'] : '',
'database' => isset($info['path']) ? substr($info['path'],1) : '',
'charset' => isset($info['fragment'])?$info['fragment']:'',
'connection' => [
'username' => isset($info['user']) ? $info['user'] : '',
'password' => isset($info['pass']) ? $info['pass'] : '',
'hostname' => isset($info['host']) ? $info['host'] : '',
'hostport' => isset($info['port']) ? $info['port'] : '',
'database' => isset($info['path']) ? substr($info['path'],1) : '',
],
'charset' => isset($info['fragment'])?$info['fragment']:'utf8',
];
$dsn['dsn'] = ''; // 兼容配置信息数组
if(isset($info['query'])) {
parse_str($info['query'],$dsn['params']);
}else{

View File

@@ -36,7 +36,26 @@ abstract class Driver {
// 当前连接ID
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'];
// 查询表达式
@@ -60,10 +79,7 @@ abstract class Driver {
*/
public function __construct($config=''){
if(!empty($config)) {
$this->config = $config;
if(empty($this->config['params'])) {
$this->config['params'] = [];
}
$this->config = array_merge($this->config,$config);
$this->config['params'] = $this->options+$this->config['params'];
}
}
@@ -74,25 +90,23 @@ abstract class Driver {
*/
public function connect($config='',$linkNum=0) {
if ( !isset($this->linkID[$linkNum]) ) {
if(empty($config)) $config = $this->config;
if(empty($config)) $config = $this->config['connection'];
try{
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'])) {
$config['dsn'] .= ';port='.$config['hostport'];
}elseif(!empty($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) {
E($e->getMessage());
}
if(!empty($config['charset'])) {
$this->linkID[$linkNum]->exec('SET NAMES '.$config['charset']);
if(!empty($this->config['charset'])) {
$this->linkID[$linkNum]->exec('SET NAMES '.$this->config['charset']);
}
// 注销数据库连接配置信息
if(1 != $config['deploy']) $this->config = [];
}
return $this->linkID[$linkNum];
}
@@ -591,7 +605,7 @@ abstract class Driver {
}
}
//将__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;
}
@@ -902,7 +916,7 @@ abstract class Driver {
* @return void
*/
protected function initConnect($master=true) {
if(1 == $this->config['deploy'])
if(!empty($this->config['deploy']))
// 采用分布式数据库
$this->_linkID = $this->multiConnect($master);
else
@@ -920,22 +934,22 @@ abstract class Driver {
static $_config = [];
if(empty($_config)) {
// 缓存分布式数据库配置解析
foreach ($this->config as $key=>$val){
foreach ($this->config['connection'] as $key=>$val){
$_config[$key] = explode(',',$val);
}
}
// 数据库读写是否分离
if(Config::get('db_rw_separate')){
if($this->config['rw_separate']){
// 主从式采用读写分离
if($master)
// 主服务器写入
$r = floor(mt_rand(0,Config::get('db_master_num')-1));
$r = floor(mt_rand(0,$this->config['master_num']-1));
else{
if(is_numeric(Config::get('db_slave_no'))) {// 指定服务器读
$r = Config::get('db_slave_no');
if(is_numeric($this->config['slave_no'])) {// 指定服务器读
$r = $this->config['slave_no'];
}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{
@@ -949,7 +963,6 @@ abstract class Driver {
'hostport' => isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0],
'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][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);
}

View File

@@ -83,7 +83,7 @@ class Model {
// 设置表前缀
if(empty($this->tablePrefix)) {
$this->tablePrefix = is_null($this->tablePrefix)?'':C('db_prefix');
$this->tablePrefix = is_null($this->tablePrefix)?'':C('database.prefix');
}
// 数据库初始化操作

View File

@@ -37,22 +37,29 @@ return [
'var_jsonp_handler' => 'callback',
/* 错误设置 */
'error_message' => '页面错误!请稍后再试~',//错误显示信息,非调试模式有效
'error_page' => '', // 错误定向页面
'show_error_msg' => false, // 显示错误信息
'error_message' => '页面错误!请稍后再试~',//错误显示信息,非调试模式有效
'error_page' => '', // 错误定向页面
'show_error_msg' => false, // 显示错误信息
/* 数据库设置 */
'db_type' => 'mysql', // 数据库类型
'db_host' => 'localhost', // 服务器地址
'db_name' => '', // 数据库名
'db_user' => 'root', // 用户名
'db_pwd' => '', // 密码
'db_port' => '', // 端口
'db_prefix' => 'think_', // 数据库表前缀
'db_fields_cache' => true, // 启用字段缓存
'db_charset' => 'utf8', // 数据库编码默认采用utf8
'db_deploy_type' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'db_rw_separate' => false, // 数据库读写是否分离 主从式有效
'db_master_num' => 1, // 读写分离后 主服务器数量
'db_slave_no' => '', // 指定从服务器序号
'database' => [
'dbms' => 'mysql', // 数据库类型
'connection' => [
'dsn' => '', //
'hostname' => 'localhost', // 服务器地址
'database' => '', // 数据库名
'username' => 'root', // 用户名
'password' => '', // 密码
'hostport' => '', // 端口
'socket' => '',
],
'params' => [], // 数据库连接参数
'charset' => 'utf8', // 数据库编码默认采用utf8
'prefix' => '', // 数据库表前缀
'debug' => false, // 数据库调试模式
'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'rw_separate' => false, // 数据库读写是否分离 主从式有效
'master_num' => 1, // 读写分离后 主服务器数量
'slave_no' => '', // 指定从服务器序号
],
];