内核更新

This commit is contained in:
2017-01-14 11:07:28 +08:00
parent 77c1f9c8bf
commit 109cf819be
10 changed files with 336 additions and 80 deletions

View File

@@ -95,6 +95,8 @@ abstract class Connection
'slave_no' => '',
// 是否严格检查字段是否存在
'fields_strict' => true,
// 数据返回类型
'result_type' => PDO::FETCH_ASSOC,
// 数据集返回类型
'resultset_type' => 'array',
// 自动写入时间戳字段
@@ -118,6 +120,9 @@ abstract class Connection
PDO::ATTR_EMULATE_PREPARES => false,
];
// 绑定参数
protected $bind = [];
/**
* 架构函数 读取数据库配置信息
* @access public
@@ -269,6 +274,10 @@ abstract class Connection
if (isset($config['resultset_type'])) {
$this->resultSetType = $config['resultset_type'];
}
// 数据返回类型
if (isset($config['result_type'])) {
$this->fetchType = $config['result_type'];
}
try {
if (empty($config['dsn'])) {
$config['dsn'] = $this->parseDsn($config);
@@ -319,22 +328,30 @@ abstract class Connection
/**
* 执行查询 返回数据集
* @access public
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param bool $master 是否在主服务器读操作
* @param bool $class 是否返回PDO对象
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param boolean $master 是否在主服务器读操作
* @param bool|string $class 指定返回的数据集对象
* @param bool $pdo 是否返回PDO对象
* @return mixed
* @throws BindParamException
* @throws PDOException
*/
public function query($sql, $bind = [], $master = false, $class = false)
public function query($sql, $bind = [], $master = false, $pdo = false)
{
$this->initConnect($master);
if (!$this->linkID) {
return false;
}
// 根据参数绑定组装最终的SQL语句
$this->queryStr = $this->getRealSql($sql, $bind);
// 记录SQL语句
$this->queryStr = $sql;
if ($bind) {
$this->bind = $bind;
}
//释放前次的查询结果
if (!empty($this->PDOStatement) && $this->PDOStatement->queryString != $sql) {
@@ -349,16 +366,22 @@ abstract class Connection
if (empty($this->PDOStatement)) {
$this->PDOStatement = $this->linkID->prepare($sql);
}
// 是否为存储过程调用
$procedure = in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']);
// 参数绑定
$this->bindValue($bind);
if ($procedure) {
$this->bindParam($bind);
} else {
$this->bindValue($bind);
}
// 执行查询
$this->PDOStatement->execute();
// 调试结束
$this->debug(false);
$procedure = in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']);
return $this->getResult($class, $procedure);
// 返回结果集
return $this->getResult($pdo, $procedure);
} catch (\PDOException $e) {
throw new PDOException($e, $this->config, $this->queryStr);
throw new PDOException($e, $this->config, $this->getLastsql());
}
}
@@ -377,8 +400,12 @@ abstract class Connection
if (!$this->linkID) {
return false;
}
// 根据参数绑定组装最终的SQL语句
$this->queryStr = $this->getRealSql($sql, $bind);
// 记录SQL语句
$this->queryStr = $sql;
if ($bind) {
$this->bind = $bind;
}
//释放前次的查询结果
if (!empty($this->PDOStatement) && $this->PDOStatement->queryString != $sql) {
@@ -393,8 +420,14 @@ abstract class Connection
if (empty($this->PDOStatement)) {
$this->PDOStatement = $this->linkID->prepare($sql);
}
// 参数绑定操作
$this->bindValue($bind);
// 是否为存储过程调用
$procedure = in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']);
// 参数绑定
if ($procedure) {
$this->bindParam($bind);
} else {
$this->bindValue($bind);
}
// 执行语句
$this->PDOStatement->execute();
// 调试结束
@@ -403,7 +436,7 @@ abstract class Connection
$this->numRows = $this->PDOStatement->rowCount();
return $this->numRows;
} catch (\PDOException $e) {
throw new PDOException($e, $this->config, $this->queryStr);
throw new PDOException($e, $this->config, $this->getLastsql());
}
}
@@ -416,23 +449,21 @@ abstract class Connection
*/
public function getRealSql($sql, array $bind = [])
{
if ($bind) {
foreach ($bind as $key => $val) {
$value = is_array($val) ? $val[0] : $val;
$type = is_array($val) ? $val[1] : PDO::PARAM_STR;
if (PDO::PARAM_STR == $type) {
$value = $this->quote($value);
} elseif (PDO::PARAM_INT == $type) {
$value = (float) $value;
}
// 判断占位符
$sql = is_numeric($key) ?
substr_replace($sql, $value, strpos($sql, '?'), 1) :
str_replace(
[':' . $key . ')', ':' . $key . ',', ':' . $key . ' '],
[$value . ')', $value . ',', $value . ' '],
$sql . ' ');
foreach ($bind as $key => $val) {
$value = is_array($val) ? $val[0] : $val;
$type = is_array($val) ? $val[1] : PDO::PARAM_STR;
if (PDO::PARAM_STR == $type) {
$value = $this->quote($value);
} elseif (PDO::PARAM_INT == $type) {
$value = (float) $value;
}
// 判断占位符
$sql = is_numeric($key) ?
substr_replace($sql, $value, strpos($sql, '?'), 1) :
str_replace(
[':' . $key . ')', ':' . $key . ',', ':' . $key . ' '],
[$value . ')', $value . ',', $value . ' '],
$sql . ' ');
}
return rtrim($sql);
}
@@ -444,7 +475,7 @@ abstract class Connection
* @access public
* @param array $bind 要绑定的参数列表
* @return void
* @throws \think\Exception
* @throws BindParamException
*/
protected function bindValue(array $bind = [])
{
@@ -463,7 +494,34 @@ abstract class Connection
throw new BindParamException(
"Error occurred when binding parameters '{$param}'",
$this->config,
$this->queryStr,
$this->getLastsql(),
$bind
);
}
}
}
/**
* 存储过程的输入输出参数绑定
* @access public
* @param array $bind 要绑定的参数列表
* @return void
* @throws BindParamException
*/
protected function bindParam($bind)
{
foreach ($bind as $key => $val) {
if (is_numeric($key)) {
$key = $key + 1;
}
array_unshift($val, $key);
$result = call_user_func_array([$this->PDOStatement, 'bindParam'], $val);
if (!$result) {
$param = array_shift($val);
throw new BindParamException(
"Error occurred when binding parameters '{$param}'",
$this->config,
$this->getLastsql(),
$bind
);
}
@@ -473,19 +531,19 @@ abstract class Connection
/**
* 获得数据集
* @access protected
* @param bool|string $class true 返回PDOStatement 字符串用于指定返回的类名
* @param bool $procedure 是否存储过程
* @param bool $pdo 是否返回PDOStatement
* @param bool $procedure 是否存储过程
* @return mixed
*/
protected function getResult($class = '', $procedure = false)
protected function getResult($pdo = false, $procedure = false)
{
if (true === $class) {
if ($pdo) {
// 返回PDOStatement对象处理
return $this->PDOStatement;
}
if ($procedure) {
// 存储过程返回结果
return $this->procedure($class);
return $this->procedure();
}
$result = $this->PDOStatement->fetchAll($this->fetchType);
$this->numRows = count($result);
@@ -500,14 +558,13 @@ abstract class Connection
/**
* 获得存储过程数据集
* @access protected
* @param bool|string $class true 返回PDOStatement 字符串用于指定返回的类名
* @return array
*/
protected function procedure($class)
protected function procedure()
{
$item = [];
do {
$result = $this->getResult($class);
$result = $this->getResult();
if ($result) {
$item[] = $result;
}
@@ -698,7 +755,7 @@ abstract class Connection
*/
public function getLastSql()
{
return $this->queryStr;
return $this->getRealSql($this->queryStr, $this->bind);
}
/**
@@ -736,7 +793,7 @@ abstract class Connection
$error = '';
}
if ('' != $this->queryStr) {
$error .= "\n [ SQL语句 ] : " . $this->queryStr;
$error .= "\n [ SQL语句 ] : " . $this->getLastsql();
}
return $error;
}
@@ -771,7 +828,7 @@ abstract class Connection
// 记录操作结束时间
Debug::remark('queryEndTime', 'time');
$runtime = Debug::getRangeTime('queryStartTime', 'queryEndTime');
$sql = $sql ?: $this->queryStr;
$sql = $sql ?: $this->getLastsql();
$log = $sql . ' [ RunTime:' . $runtime . 's ]';
$result = [];
// SQL性能分析