Think/Db/Driver类 改进parseBind parseSet等方法 增加parseDsn方法

用于各个驱动单独解析dsn字符串(在没有配置dsn字符串的情况下)
This commit is contained in:
ThinkPHP
2013-04-27 12:28:34 +08:00
parent b54ee372f5
commit 2c5b98888c

View File

@@ -94,24 +94,24 @@ abstract class Driver {
if(empty($config)) $config = $this->config['connection']; if(empty($config)) $config = $this->config['connection'];
try{ try{
if(empty($config['dsn'])) { if(empty($config['dsn'])) {
$config['dsn'] = $this->config['dbms'].':dbname='.$config['database'].';host='.$config['hostname']; $config['dsn'] = $this->parseDsn($config);
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'],$this->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($this->config['charset'])) {
$this->linkID[$linkNum]->exec('SET NAMES '.$this->config['charset']);
}
} }
return $this->linkID[$linkNum]; return $this->linkID[$linkNum];
} }
/**
* 解析pdo连接的dsn信息
* @access public
* @param array $config 连接信息
* @return string
*/
protected function parseDsn($config){}
/** /**
* 释放查询结果 * 释放查询结果
* @access public * @access public
@@ -325,14 +325,13 @@ abstract class Driver {
*/ */
protected function parseSet($data) { protected function parseSet($data) {
foreach ($data as $key=>$val){ foreach ($data as $key=>$val){
$value = $this->parseValue($val); if(is_scalar($val)) {// 过滤非标量数据
if(is_scalar($value)) {// 过滤非标量数据 if(0===strpos($val,':')){
if(0===strpos($value,':')){ $set[] = $this->parseKey($key).'='.$this->parseValue($val);
$set[] = $this->parseKey($key).'='.$value;
}else{ }else{
$name = md5($key); $name = md5($key);
$set[] = $this->parseKey($key).'=:T'.$name; $set[] = $this->parseKey($key).'=:T'.$name;
$this->bindParam($name,$value); $this->bindParam($name,$val);
} }
} }
} }
@@ -726,7 +725,9 @@ abstract class Driver {
* @return array * @return array
*/ */
protected function parseBind($bind){ protected function parseBind($bind){
return array_merge($this->bind,$bind); $bind = array_merge($this->bind,$bind);
$this->bind = [];
return $bind;
} }
/** /**
@@ -741,15 +742,14 @@ abstract class Driver {
$values = $fields = []; $values = $fields = [];
$this->model = $options['model']; $this->model = $options['model'];
foreach ($data as $key=>$val){ foreach ($data as $key=>$val){
$value = $this->parseValue($val); if(is_scalar($val)) { // 过滤非标量数据
if(is_scalar($value)) { // 过滤非标量数据
$fields[] = $this->parseKey($key); $fields[] = $this->parseKey($key);
if(0===strpos($value,':')){ if(0===strpos($val,':')){
$values[] = $value; $values[] = $this->parseValue($val);
}else{ }else{
$name = md5($key); $name = md5($key);
$values[] = ':T'.$name; $values[] = ':T'.$name;
$this->bindParam($name,$value); $this->bindParam($name,$val);
} }
} }
} }