数据库配置信息调整

This commit is contained in:
ThinkPHP
2013-04-27 21:16:25 +08:00
parent bb37ad02b6
commit dc4ed37b4c
3 changed files with 174 additions and 179 deletions

View File

@@ -33,7 +33,7 @@ class Db {
// 解析连接参数 支持数组和字符串 // 解析连接参数 支持数组和字符串
$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];
@@ -73,14 +73,12 @@ class Db {
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',
]; ];

View File

@@ -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' => 'root', // 用户名 'username' => '', // 用户名
'password' => '', // 密码 'password' => '', // 密码
'hostport' => '', // 端口 'hostport' => '', // 端口
'socket' => '',
'dsn' => '', // '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']){

View File

@@ -43,16 +43,13 @@ return [
/* 数据库设置 */ /* 数据库设置 */
'database' => [ 'database' => [
'dbms' => 'mysql', // 数据库类型 'type' => 'mysql', // 数据库类型
'connection' => [
'dsn' => '', // 'dsn' => '', //
'hostname' => 'localhost', // 服务器地址 'hostname' => 'localhost', // 服务器地址
'database' => '', // 数据库名 'database' => '', // 数据库名
'username' => 'root', // 用户名 'username' => 'root', // 用户名
'password' => '', // 密码 'password' => '', // 密码
'hostport' => '', // 端口 'hostport' => '', // 端口
'socket' => '',
],
'params' => [], // 数据库连接参数 'params' => [], // 数据库连接参数
'charset' => 'utf8', // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库编码默认采用utf8
'prefix' => '', // 数据库表前缀 'prefix' => '', // 数据库表前缀