异常类规范 增加异常类 改进Query类的value和column的缓存冲突问题

This commit is contained in:
thinkphp
2016-06-12 16:35:42 +08:00
parent 659900ab91
commit b10635e22e
33 changed files with 236 additions and 79 deletions

View File

@@ -18,8 +18,8 @@ use think\Db;
use think\db\Query;
use think\Debug;
use think\Exception;
use think\exception\DbBindParamException;
use think\exception\PDOException;
use think\db\exception\BindParamException;
use think\Log;
abstract class Connection
@@ -326,7 +326,7 @@ abstract class Connection
* @param boolean $master 是否在主服务器读操作
* @param bool|string $class 指定返回的数据集对象
* @return mixed
* @throws DbBindParamException
* @throws BindParamException
* @throws PDOException
*/
public function query($sql, $bind = [], $fetch = false, $master = false, $class = false)
@@ -375,7 +375,7 @@ abstract class Connection
* @param boolean $getLastInsID 是否获取自增ID
* @param string $sequence 自增序列名
* @return int
* @throws DbBindParamException
* @throws BindParamException
* @throws PDOException
*/
public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false, $sequence = null)
@@ -465,7 +465,7 @@ abstract class Connection
$result = $this->PDOStatement->bindValue($param, $val);
}
if (!$result) {
throw new DbBindParamException(
throw new BindParamException(
"Error occurred when binding parameters '{$param}'",
$this->config,
$this->queryStr,

View File

@@ -0,0 +1,43 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
// +----------------------------------------------------------------------
namespace think\db;
use think\Exception;
/**
* Database相关异常处理类
*/
class DbException extends Exception
{
/**
* DbException constructor.
* @param string $message
* @param array $config
* @param string $sql
* @param int $code
*/
public function __construct($message, Array $config, $sql, $code = 10500)
{
$this->message = $message;
$this->code = $code;
$this->setData('Database Status', [
'Error Code' => $code,
'Error Message' => $message,
'Error SQL' => $sql
]);
$this->setData('Database Config', $config);
}
}

View File

@@ -21,6 +21,8 @@ use think\db\Connection;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\db\exception\ModelNotFoundException;
use think\db\exception\DataNotFoundException;
use think\Loader;
use think\Model;
use think\model\Relation;
@@ -379,6 +381,9 @@ class Query
if (!empty($this->options['cache'])) {
// 判断查询缓存
$cache = $this->options['cache'];
if (empty($this->options['table'])) {
$this->options['table'] = $this->getTable();
}
$key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options));
$result = Cache::get($key);
}
@@ -412,6 +417,9 @@ class Query
if (!empty($this->options['cache'])) {
// 判断查询缓存
$cache = $this->options['cache'];
if (empty($this->options['table'])) {
$this->options['table'] = $this->getTable();
}
$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options));
$result = Cache::get($guid);
}
@@ -1775,7 +1783,11 @@ class Query
}
}
} elseif (!empty($options['fail'])) {
throw new DbException('Data not Found', $options, $sql);
if(!empty($this->model)){
throw new ModelNotFoundException('Data not Found', $this->model, $options);
}else{
throw new DataNotFoundException('Data not Found', $options['table'], $options);
}
} else {
$data = false;
}

View File

@@ -0,0 +1,35 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
// +----------------------------------------------------------------------
namespace think\db\exception;
use think\db\Exception;
/**
* PDO参数绑定异常
*/
class BindParamException extends Exception
{
/**
* DbBindParamException constructor.
* @param string $message
* @param array $config
* @param string $sql
* @param array $bind
* @param int $code
*/
public function __construct($message, $config, $sql, $bind, $code = 10502)
{
$this->setData('Bind Param', $bind);
parent::__construct($message, $config, $sql, $code);
}
}

View File

@@ -0,0 +1,35 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
// +----------------------------------------------------------------------
namespace think\db\exception;
use think\db\DbException;
class DataNotFoundException extends DbException
{
protected $table;
/**
* DbException constructor.
* @param string $message
* @param string $table
* @param array $config
*/
public function __construct($message, $table = '', Array $config = [])
{
$this->message = $message;
$this->table = $table;
$this->setData('Database Config', $config);
}
}

View File

@@ -0,0 +1,38 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
// +----------------------------------------------------------------------
namespace think\db\exception;
use think\db\DbException;
class ModelNotFoundException extends DbException
{
protected $model;
/**
* 构造方法
* @param string $message
* @param string $model
*/
public function __construct($message, $model = '', Array $config = [])
{
$this->message = $message;
$this->model = $model;
$this->setData('Database Config', $config);
}
public function getModel()
{
return $this->model;
}
}