改进参数绑定机制 原生查询也可以支持参数绑定,并且同时支持 命名占位符和问号占位符 改进Driver类的异常处理 废除error方法

模型类中使用bind方法如下:
// 命名占位符
$this->bind('name','value');
// 问号占位符
$this->bind(1,'value');
This commit is contained in:
thinkphp
2016-01-21 17:37:10 +08:00
parent 0a323bc879
commit c3412cfb12
4 changed files with 125 additions and 149 deletions

View File

@@ -199,7 +199,7 @@ class Model
throw new Exception(' fields not exists :[' . $key . '=>' . $val . ']'); throw new Exception(' fields not exists :[' . $key . '=>' . $val . ']');
} }
unset($data[$key]); unset($data[$key]);
} elseif (is_scalar($val) && empty($this->options['bind'][':' . $key])) { } elseif (is_scalar($val) && empty($this->options['bind'][$key])) {
// 字段类型检查 // 字段类型检查
$this->_parseType($data, $key, $this->options['bind']); $this->_parseType($data, $key, $this->options['bind']);
} }
@@ -779,7 +779,7 @@ class Model
foreach ($options['where'] as $key => $val) { foreach ($options['where'] as $key => $val) {
$key = trim($key); $key = trim($key);
if (in_array($key, $fields, true)) { if (in_array($key, $fields, true)) {
if (is_scalar($val) && empty($options['bind'][':' . $key])) { if (is_scalar($val) && empty($options['bind'][$key])) {
$this->_parseType($options['where'], $key, $options['bind']); $this->_parseType($options['where'], $key, $options['bind']);
} }
} }
@@ -804,19 +804,19 @@ class Model
*/ */
protected function _parseType(&$data, $key, &$bind) protected function _parseType(&$data, $key, &$bind)
{ {
if (!isset($bind[':' . $key]) && isset($this->fields['_type'][$key])) { if (!isset($bind[$key]) && isset($this->fields['_type'][$key])) {
$fieldType = strtolower($this->fields['_type'][$key]); $fieldType = strtolower($this->fields['_type'][$key]);
if (false !== strpos($fieldType, 'enum')) { if (false !== strpos($fieldType, 'enum')) {
// 支持ENUM类型优先检测 // 支持ENUM类型优先检测
} elseif (false === strpos($fieldType, 'bigint') && false !== strpos($fieldType, 'int')) { } elseif (false === strpos($fieldType, 'bigint') && false !== strpos($fieldType, 'int')) {
$bind[':' . $key] = [$data[$key], \PDO::PARAM_INT]; $bind[$key] = [$data[$key], \PDO::PARAM_INT];
$data[$key] = ':' . $key; $data[$key] = ':' . $key;
} elseif (false !== strpos($fieldType, 'float') || false !== strpos($fieldType, 'double')) { } elseif (false !== strpos($fieldType, 'float') || false !== strpos($fieldType, 'double')) {
$bind[':' . $key] = [$data[$key], \PDO::PARAM_INT]; $bind[$key] = [$data[$key], \PDO::PARAM_INT];
$data[$key] = ':' . $key; $data[$key] = ':' . $key;
} elseif (false !== strpos($fieldType, 'bool')) { } elseif (false !== strpos($fieldType, 'bool')) {
$bind[':' . $key] = [$data[$key], \PDO::PARAM_BOOL]; $bind[$key] = [$data[$key], \PDO::PARAM_BOOL];
$data[$key] = ':' . $key; $data[$key] = ':' . $key;
} elseif (false !== strpos($fieldType, 'json') && is_array($data[$key])) { } elseif (false !== strpos($fieldType, 'json') && is_array($data[$key])) {
$data[$key] = json_encode($data[$key]); $data[$key] = json_encode($data[$key]);
} }

View File

@@ -168,14 +168,8 @@ abstract class Driver
} }
$this->queryStr = $str; $this->queryStr = $str;
if (!empty($bind)) {
$that = $this;
$this->queryStr = strtr($this->queryStr, array_map(function ($val) use ($that) {
return $that->quote(is_array($val) ? $val[0] : $val);
}, $bind));
}
if ($fetchSql) { if ($fetchSql) {
return $this->queryStr; return $this->getBindSql($this->queryStr, $bind);
} }
//释放前次的查询结果 //释放前次的查询结果
if (!empty($this->PDOStatement)) { if (!empty($this->PDOStatement)) {
@@ -183,33 +177,20 @@ abstract class Driver
} }
Db::$queryTimes++; Db::$queryTimes++;
// 调试开始
$this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str);
if (false === $this->PDOStatement) {
$this->error();
return false;
}
foreach ($bind as $key => $val) {
if (is_array($val)) {
$this->PDOStatement->bindValue($key, $val[0], $val[1]);
} else {
$this->PDOStatement->bindValue($key, $val);
}
}
try { try {
// 调试开始
$this->debug(true);
// 预处理
$this->PDOStatement = $this->_linkID->prepare($str);
// 参数绑定
$this->bindValue($bind);
// 执行查询
$result = $this->PDOStatement->execute(); $result = $this->PDOStatement->execute();
// 调试结束 // 调试结束
$this->debug(false); $this->debug(false);
if (false === $result) { return $this->getResult();
$this->error();
return false;
} else {
return $this->getResult();
}
} catch (\PDOException $e) { } catch (\PDOException $e) {
$this->error(); throw new Exception($e->getMessage());
return false;
} }
} }
@@ -229,14 +210,8 @@ abstract class Driver
} }
$this->queryStr = $str; $this->queryStr = $str;
if (!empty($bind)) {
$that = $this;
$this->queryStr = strtr($this->queryStr, array_map(function ($val) use ($that) {
return $that->quote(is_array($val) ? $val[0] : $val);
}, $bind));
}
if ($fetchSql) { if ($fetchSql) {
return $this->queryStr; return $this->getBindSql($this->queryStr, $bind);
} }
//释放前次的查询结果 //释放前次的查询结果
if (!empty($this->PDOStatement)) { if (!empty($this->PDOStatement)) {
@@ -244,36 +219,66 @@ abstract class Driver
} }
Db::$executeTimes++; Db::$executeTimes++;
// 记录开始执行时间
$this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str);
if (false === $this->PDOStatement) {
$this->error();
return false;
}
foreach ($bind as $key => $val) {
if (is_array($val)) {
$this->PDOStatement->bindValue($key, $val[0], $val[1]);
} else {
$this->PDOStatement->bindValue($key, $val);
}
}
try { try {
// 调试开始
$this->debug(true);
// 预处理
$this->PDOStatement = $this->_linkID->prepare($str);
// 参数绑定操作
$this->bindValue($bind);
// 执行语句
$result = $this->PDOStatement->execute(); $result = $this->PDOStatement->execute();
// 调试结束
$this->debug(false); $this->debug(false);
if (false === $result) {
$this->error(); $this->numRows = $this->PDOStatement->rowCount();
return false; if (preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
} else { $this->lastInsID = $this->_linkID->lastInsertId();
$this->numRows = $this->PDOStatement->rowCount();
if (preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
$this->lastInsID = $this->_linkID->lastInsertId();
}
return $this->numRows;
} }
return $this->numRows;
} catch (\PDOException $e) { } catch (\PDOException $e) {
$this->error(); throw new Exception($e->getMessage());
return false; }
}
protected function getBindSql($sql, array $bind = [])
{
if ($bind) {
foreach ($bind as $key => $val) {
// 占位符
$val = is_array($val) ? $val[0] : $val;
if (is_numeric($key)) {
// 问号占位符
$sql = substr_replace($sql, $val, strpos($sql, '?'), 1);
} else {
$sql = str_replace(':' . $key, $val, $sql);
}
}
}
return $sql;
}
/**
* 参数绑定
* 支持 ['name'=>'value','id'=>123] 对应命名占位符
* 或者 ['value',123] 对应问号占位符
* @access public
* @param array $bind 要绑定的参数列表
* @return void
*/
protected function bindValue(array $bind = [])
{
foreach ($bind as $key => $val) {
// 占位符
$param = is_numeric($key) ? $key + 1 : ':' . $key;
if (is_array($val)) {
$result = $this->PDOStatement->bindValue($param, $val[0], $val[1]);
} else {
$result = $this->PDOStatement->bindValue($param, $val);
}
if (!$result) {
throw new Exception('bind param error : [ ' . $param . '=>' . $val . ' ]');
}
} }
} }
@@ -305,11 +310,11 @@ abstract class Driver
public function commit() public function commit()
{ {
if ($this->transTimes > 0) { if ($this->transTimes > 0) {
$result = $this->_linkID->commit(); try {
$this->transTimes = 0; $result = $this->_linkID->commit();
if (!$result) { $this->transTimes = 0;
$this->error(); } catch (\PDOException $e) {
return false; throw new Exception($e->getMessage());
} }
} }
return true; return true;
@@ -323,11 +328,11 @@ abstract class Driver
public function rollback() public function rollback()
{ {
if ($this->transTimes > 0) { if ($this->transTimes > 0) {
$result = $this->_linkID->rollback(); try {
$this->transTimes = 0; $result = $this->_linkID->rollback();
if (!$result) { $this->transTimes = 0;
$this->error(); } catch (\PDOException $e) {
return false; throw new Exception($e->getMessage());
} }
} }
return true; return true;
@@ -376,33 +381,6 @@ abstract class Driver
$this->_linkID = null; $this->_linkID = null;
} }
/**
* 数据库错误信息
* 并显示当前的SQL语句
* @access public
* @return string
*/
public function error()
{
if ($this->PDOStatement) {
$error = $this->PDOStatement->errorInfo();
$this->error = $error[1] . ':' . $error[2];
} else {
$this->error = '';
}
if ('' != $this->queryStr) {
$this->error .= "\n [ SQL语句 ] : " . $this->queryStr;
}
// 记录错误日志
Log::record($this->error, 'error');
if ($this->config['debug']) {
// 开启数据库调试模式
throw new Exception($this->error);
} else {
return $this->error;
}
}
/** /**
* 设置锁机制 * 设置锁机制
* @access protected * @access protected
@@ -428,12 +406,12 @@ abstract class Driver
$set[] = $this->parseKey($key) . '=NULL'; $set[] = $this->parseKey($key) . '=NULL';
} elseif (is_scalar($val)) { } elseif (is_scalar($val)) {
// 过滤非标量数据 // 过滤非标量数据
if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) { if (0 === strpos($val, ':') && isset($this->bind[substr($val, 1)])) {
$set[] = $this->parseKey($key) . '=' . $val; $set[] = $this->parseKey($key) . '=' . $val;
} else { } else {
$name = count($this->bind); $name = count($this->bind);
$set[] = $this->parseKey($key) . '=:' . $_SERVER['REQUEST_TIME'] . '_' . $name; $set[] = $this->parseKey($key) . '=:' . $key . $_SERVER['REQUEST_TIME'] . '_' . $name;
$this->bindParam($_SERVER['REQUEST_TIME'] . '_' . $name, $val); $this->bindParam($key . $_SERVER['REQUEST_TIME'] . '_' . $name, $val);
} }
} }
} }
@@ -449,7 +427,7 @@ abstract class Driver
*/ */
protected function bindParam($name, $value) protected function bindParam($name, $value)
{ {
$this->bind[':' . $name] = $value; $this->bind[$name] = $value;
} }
/** /**
@@ -486,7 +464,7 @@ abstract class Driver
protected function parseValue($value) protected function parseValue($value)
{ {
if (is_string($value)) { if (is_string($value)) {
$value = strpos($value, ':') === 0 && in_array($value, array_keys($this->bind)) ? $value : $this->quote($value); $value = strpos($value, ':') === 0 && isset($this->bind[substr($value, 1)]) ? $value : $this->quote($value);
} elseif (isset($value[0]) && is_string($value[0]) && strtolower($value[0]) == 'exp') { } elseif (isset($value[0]) && is_string($value[0]) && strtolower($value[0]) == 'exp') {
$value = $this->quote($value[1]); $value = $this->quote($value[1]);
} elseif (is_array($value)) { } elseif (is_array($value)) {
@@ -926,8 +904,8 @@ abstract class Driver
$values[] = $val; $values[] = $val;
} else { } else {
$name = count($this->bind); $name = count($this->bind);
$values[] = ':' . $_SERVER['REQUEST_TIME'] . '_' . $name; $values[] = ':' . $key . $_SERVER['REQUEST_TIME'] . '_' . $name;
$this->bindParam($_SERVER['REQUEST_TIME'] . '_' . $name, $val); $this->bindParam($key . $_SERVER['REQUEST_TIME'] . '_' . $name, $val);
} }
} }
} }
@@ -968,8 +946,8 @@ abstract class Driver
$value[] = $val; $value[] = $val;
} else { } else {
$name = count($this->bind); $name = count($this->bind);
$value[] = ':' . $_SERVER['REQUEST_TIME'] . '_' . $name; $value[] = ':' . $key . $_SERVER['REQUEST_TIME'] . '_' . $name;
$this->bindParam($_SERVER['REQUEST_TIME'] . '_' . $name, $val); $this->bindParam($key . $_SERVER['REQUEST_TIME'] . '_' . $name, $val);
} }
} }
} }
@@ -1149,7 +1127,16 @@ abstract class Driver
*/ */
public function getError() public function getError()
{ {
return $this->error; if ($this->PDOStatement) {
$error = $this->PDOStatement->errorInfo();
$error = $error[1] . ':' . $error[2];
} else {
$error = '';
}
if ('' != $this->queryStr) {
$error .= "\n [ SQL语句 ] : " . $this->queryStr;
}
return $error;
} }
/** /**

View File

@@ -14,7 +14,6 @@ namespace think\db\driver;
use think\db\Driver; use think\db\Driver;
use think\Exception; use think\Exception;
use think\Lang; use think\Lang;
use think\Log;
/** /**
* Mongo数据库驱动 * Mongo数据库驱动
@@ -69,7 +68,7 @@ class Mongo extends Driver
$host = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '') . '/' . ($config['database'] ? "{$config['database']}" : ''); $host = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '') . '/' . ($config['database'] ? "{$config['database']}" : '');
try { try {
$this->linkID[$linkNum] = new \mongoClient($host, !empty($this->config['params'])?$this->config['params']:array()); $this->linkID[$linkNum] = new \mongoClient($host, !empty($this->config['params']) ? $this->config['params'] : array());
} catch (\MongoConnectionException $e) { } catch (\MongoConnectionException $e) {
throw new Exception($e->getmessage()); throw new Exception($e->getmessage());
} }
@@ -94,7 +93,7 @@ class Mongo extends Driver
$this->initConnect($master); $this->initConnect($master);
} }
$db = $db?$db:$this->config['database']; $db = $db ? $db : $this->config['database'];
try { try {
if (!empty($db)) { if (!empty($db)) {
@@ -191,11 +190,10 @@ class Mongo extends Driver
* @access public * @access public
* @return string * @return string
*/ */
public function error() public function getError()
{ {
$this->error = $this->_mongo->lastError(); $error = $this->_mongo->lastError();
Log::record($this->error, 'error'); return $error;
return $this->error;
} }
/** /**
@@ -482,8 +480,8 @@ class Mongo extends Driver
} }
$this->model = $options['model']; $this->model = $options['model'];
$this->queryTimes++; $this->queryTimes++;
$query = $this->parseWhere(!empty($options['where'])?$options['where']:''); $query = $this->parseWhere(!empty($options['where']) ? $options['where'] : '');
$fields = $this->parseField(!empty($options['field'])?$options['field']:''); $fields = $this->parseField(!empty($options['field']) ? $options['field'] : '');
if ($this->config['debug']) { if ($this->config['debug']) {
$this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.findOne('; $this->queryStr = $this->_dbName . '.' . $this->_collectionName . '.findOne(';
$this->queryStr .= $query ? json_encode($query) : '{}'; $this->queryStr .= $query ? json_encode($query) : '{}';
@@ -637,7 +635,7 @@ class Mongo extends Driver
*/ */
protected function parseOrder($order) protected function parseOrder($order)
{ {
if(is_array($order)) { if (is_array($order)) {
$order = join(',', $order); $order = join(',', $order);
} }
if (is_string($order)) { if (is_string($order)) {
@@ -742,14 +740,14 @@ class Mongo extends Driver
{ {
$query = []; $query = [];
switch ($key) { switch ($key) {
case '_query': // 字符串模式查询条件 case '_query': // 字符串模式查询条件
parse_str($val, $query); parse_str($val, $query);
if (isset($query['_logic']) && strtolower($query['_logic']) == 'or') { if (isset($query['_logic']) && strtolower($query['_logic']) == 'or') {
unset($query['_logic']); unset($query['_logic']);
$query['$or'] = $query; $query['$or'] = $query;
} }
break; break;
case '_string': // MongoCode查询 case '_string': // MongoCode查询
$query['$where'] = new \MongoCode($val); $query['$where'] = new \MongoCode($val);
break; break;
} }

View File

@@ -11,6 +11,7 @@
namespace think\db\driver; namespace think\db\driver;
use think\Db;
use think\db\Driver; use think\db\Driver;
/** /**
@@ -64,30 +65,20 @@ class Oracle extends Driver
$this->free(); $this->free();
} }
$this->executeTimes++; Db::$executeTimes++;
// 记录开始执行时间
$this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str);
if (false === $this->PDOStatement) {
$this->error();
return false;
}
try { try {
$result = $this->PDOStatement->execute($bind); // 记录开始执行时间
$this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str);
$result = $this->PDOStatement->execute($bind);
$this->debug(false); $this->debug(false);
if (false === $result) { $this->numRows = $this->PDOStatement->rowCount();
$this->error(); if ($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
return false; $this->lastInsID = $this->_linkID->lastInsertId();
} else {
$this->numRows = $this->PDOStatement->rowCount();
if ($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
$this->lastInsID = $this->_linkID->lastInsertId();
}
return $this->numRows;
} }
return $this->numRows;
} catch (\PDOException $e) { } catch (\PDOException $e) {
$this->error(); throw new Exception($e->getMessage());
return false;
} }
} }