内核更新
This commit is contained in:
@@ -419,11 +419,7 @@ class Query
|
||||
}
|
||||
if (isset($cache)) {
|
||||
// 缓存数据
|
||||
if (isset($cache['tag'])) {
|
||||
Cache::tag($cache['tag'])->set($key, $result, $cache['expire']);
|
||||
} else {
|
||||
Cache::set($key, $result, $cache['expire']);
|
||||
}
|
||||
$this->cacheData($key, $result, $cache);
|
||||
}
|
||||
} else {
|
||||
// 清空查询条件
|
||||
@@ -491,11 +487,7 @@ class Query
|
||||
}
|
||||
if (isset($cache) && isset($guid)) {
|
||||
// 缓存数据
|
||||
if (isset($cache['tag'])) {
|
||||
Cache::tag($cache['tag'])->set($guid, $result, $cache['expire']);
|
||||
} else {
|
||||
Cache::set($guid, $result, $cache['expire']);
|
||||
}
|
||||
$this->cacheData($guid, $result, $cache);
|
||||
}
|
||||
} else {
|
||||
// 清空查询条件
|
||||
@@ -2070,11 +2062,18 @@ class Query
|
||||
// 执行操作
|
||||
$result = $this->execute($sql, $bind);
|
||||
if ($result) {
|
||||
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
|
||||
$lastInsId = $this->getLastInsID($sequence);
|
||||
if ($lastInsId) {
|
||||
$pk = $this->getPk($options);
|
||||
$data[$pk] = $lastInsId;
|
||||
}
|
||||
$options['data'] = $data;
|
||||
$this->trigger('after_insert', $options);
|
||||
}
|
||||
if ($getLastInsID) {
|
||||
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
|
||||
return $this->getLastInsID($sequence);
|
||||
|
||||
if ($getLastInsID) {
|
||||
return $lastInsId;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
@@ -2157,8 +2156,8 @@ class Query
|
||||
$options = $this->parseExpress();
|
||||
$data = array_merge($options['data'], $data);
|
||||
$pk = $this->getPk($options);
|
||||
if (isset($options['cache']) && is_string($options['cache'])) {
|
||||
$key = $options['cache'];
|
||||
if (isset($options['cache']) && is_string($options['cache']['key'])) {
|
||||
$key = $options['cache']['key'];
|
||||
}
|
||||
|
||||
if (empty($options['where'])) {
|
||||
@@ -2187,9 +2186,10 @@ class Query
|
||||
} else {
|
||||
$options['where']['AND'] = $where;
|
||||
}
|
||||
} elseif (is_string($pk) && isset($options['where']['AND'][$pk]) && is_scalar($options['where']['AND'][$pk])) {
|
||||
$key = 'think:' . $options['table'] . '|' . $options['where']['AND'][$pk];
|
||||
} elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) {
|
||||
$key = $this->getCacheKey($options['where']['AND'][$pk], $options);
|
||||
}
|
||||
|
||||
// 生成UPDATE SQL语句
|
||||
$sql = $this->builder->update($data, $options);
|
||||
// 获取参数绑定
|
||||
@@ -2206,6 +2206,13 @@ class Query
|
||||
// 执行操作
|
||||
$result = '' == $sql ? 0 : $this->execute($sql, $bind);
|
||||
if ($result) {
|
||||
if (isset($where[$pk])) {
|
||||
$data[$pk] = $where[$pk];
|
||||
} elseif (is_string($pk) && isset($key) && strpos($key, '|')) {
|
||||
list($a, $val) = explode('|', $key);
|
||||
$data[$pk] = $val;
|
||||
}
|
||||
$options['data'] = $data;
|
||||
$this->trigger('after_update', $options);
|
||||
}
|
||||
return $result;
|
||||
@@ -2278,6 +2285,8 @@ class Query
|
||||
// 获取实际执行的SQL语句
|
||||
return $this->connection->getRealSql($sql, $bind);
|
||||
}
|
||||
|
||||
$options['data'] = $data;
|
||||
if ($resultSet = $this->trigger('before_select', $options)) {
|
||||
} else {
|
||||
// 执行查询操作
|
||||
@@ -2291,11 +2300,7 @@ class Query
|
||||
|
||||
if (isset($cache)) {
|
||||
// 缓存数据集
|
||||
if (isset($cache['tag'])) {
|
||||
Cache::tag($cache['tag'])->set($key, $resultSet, $cache['expire']);
|
||||
} else {
|
||||
Cache::set($key, $resultSet, $cache['expire']);
|
||||
}
|
||||
$this->cacheData($key, $resultSet, $cache);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2339,6 +2344,40 @@ class Query
|
||||
return $resultSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存数据
|
||||
* @access public
|
||||
* @param string $key 缓存标识
|
||||
* @param mixed $data 缓存数据
|
||||
* @param array $config 缓存参数
|
||||
*/
|
||||
protected function cacheData($key, $data, $config = [])
|
||||
{
|
||||
if (isset($config['tag'])) {
|
||||
Cache::tag($config['tag'])->set($key, $data, $config['expire']);
|
||||
} else {
|
||||
Cache::set($key, $data, $config['expire']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成缓存标识
|
||||
* @access public
|
||||
* @param mixed $value 缓存数据
|
||||
* @param array $options 缓存参数
|
||||
*/
|
||||
protected function getCacheKey($value, $options)
|
||||
{
|
||||
if (is_scalar($value)) {
|
||||
$data = $value;
|
||||
} elseif (is_array($value) && 'eq' == strtolower($value[0])) {
|
||||
$data = $value[1];
|
||||
}
|
||||
if (isset($data)) {
|
||||
return 'think:' . $options['table'] . '|' . $data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找单条记录
|
||||
* @access public
|
||||
@@ -2358,10 +2397,12 @@ class Query
|
||||
}
|
||||
// 分析查询表达式
|
||||
$options = $this->parseExpress();
|
||||
|
||||
$pk = $this->getPk($options);
|
||||
if (!is_null($data)) {
|
||||
// AR模式分析主键条件
|
||||
$this->parsePkWhere($data, $options);
|
||||
} elseif (!empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['AND'][$pk])) {
|
||||
$key = $this->getCacheKey($options['where']['AND'][$pk], $options);
|
||||
}
|
||||
|
||||
$options['limit'] = 1;
|
||||
@@ -2371,12 +2412,12 @@ class Query
|
||||
$cache = $options['cache'];
|
||||
if (true === $cache['key'] && !is_null($data) && !is_array($data)) {
|
||||
$key = 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data;
|
||||
} else {
|
||||
} elseif (!isset($key)) {
|
||||
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
|
||||
}
|
||||
$result = Cache::get($key);
|
||||
}
|
||||
if (!$result) {
|
||||
if (false === $result) {
|
||||
// 生成查询SQL
|
||||
$sql = $this->builder->select($options);
|
||||
// 获取参数绑定
|
||||
@@ -2385,56 +2426,61 @@ class Query
|
||||
// 获取实际执行的SQL语句
|
||||
return $this->connection->getRealSql($sql, $bind);
|
||||
}
|
||||
|
||||
if (is_string($pk)) {
|
||||
if (!is_array($data)) {
|
||||
if (isset($key) && strpos($key, '|')) {
|
||||
list($a, $val) = explode('|', $key);
|
||||
$item[$pk] = $val;
|
||||
} else {
|
||||
$item[$pk] = $data;
|
||||
}
|
||||
$data = $item;
|
||||
}
|
||||
}
|
||||
$options['data'] = $data;
|
||||
// 事件回调
|
||||
if ($result = $this->trigger('before_find', $options)) {
|
||||
} else {
|
||||
// 执行查询
|
||||
$result = $this->query($sql, $bind, $options['master'], $options['fetch_pdo']);
|
||||
$resultSet = $this->query($sql, $bind, $options['master'], $options['fetch_pdo']);
|
||||
|
||||
if ($result instanceof \PDOStatement) {
|
||||
if ($resultSet instanceof \PDOStatement) {
|
||||
// 返回PDOStatement对象
|
||||
return $result;
|
||||
return $resultSet;
|
||||
}
|
||||
$result = isset($resultSet[0]) ? $resultSet[0] : null;
|
||||
}
|
||||
|
||||
if (isset($cache)) {
|
||||
// 缓存数据
|
||||
if (isset($cache['tag'])) {
|
||||
Cache::tag($cache['tag'])->set($key, $result, $cache['expire']);
|
||||
} else {
|
||||
Cache::set($key, $result, $cache['expire']);
|
||||
}
|
||||
$this->cacheData($key, $result, $cache);
|
||||
}
|
||||
}
|
||||
|
||||
// 数据处理
|
||||
if (!empty($result[0])) {
|
||||
$data = $result[0];
|
||||
if (!empty($result)) {
|
||||
if (!empty($this->model)) {
|
||||
// 返回模型对象
|
||||
$model = $this->model;
|
||||
$data = new $model($data);
|
||||
$data->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null);
|
||||
$model = $this->model;
|
||||
$result = new $model($result);
|
||||
$result->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null);
|
||||
// 关联查询
|
||||
if (!empty($options['relation'])) {
|
||||
$data->relationQuery($options['relation']);
|
||||
$result->relationQuery($options['relation']);
|
||||
}
|
||||
// 预载入查询
|
||||
if (!empty($options['with'])) {
|
||||
$data->eagerlyResult($data, $options['with']);
|
||||
$result->eagerlyResult($result, $options['with']);
|
||||
}
|
||||
// 关联统计
|
||||
if (!empty($options['with_count'])) {
|
||||
$data->relationCount($data, $options['with_count']);
|
||||
$result->relationCount($result, $options['with_count']);
|
||||
}
|
||||
}
|
||||
} elseif (!empty($options['fail'])) {
|
||||
$this->throwNotFound($options);
|
||||
} else {
|
||||
$data = null;
|
||||
}
|
||||
return $data;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2565,8 +2611,9 @@ class Query
|
||||
{
|
||||
// 分析查询表达式
|
||||
$options = $this->parseExpress();
|
||||
if (isset($options['cache']) && is_string($options['cache'])) {
|
||||
$key = $options['cache'];
|
||||
$pk = $this->getPk($options);
|
||||
if (isset($options['cache']) && is_string($options['cache']['key'])) {
|
||||
$key = $options['cache']['key'];
|
||||
}
|
||||
|
||||
if (!is_null($data) && true !== $data) {
|
||||
@@ -2576,6 +2623,8 @@ class Query
|
||||
}
|
||||
// AR模式分析主键条件
|
||||
$this->parsePkWhere($data, $options);
|
||||
} elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) {
|
||||
$key = $this->getCacheKey($options['where']['AND'][$pk], $options);
|
||||
}
|
||||
|
||||
if (true !== $data && empty($options['where'])) {
|
||||
@@ -2599,6 +2648,12 @@ class Query
|
||||
// 执行操作
|
||||
$result = $this->execute($sql, $bind);
|
||||
if ($result) {
|
||||
if (!is_array($data) && is_string($pk) && isset($key) && strpos($key, '|')) {
|
||||
list($a, $val) = explode('|', $key);
|
||||
$item[$pk] = $val;
|
||||
$data = $item;
|
||||
}
|
||||
$options['data'] = $data;
|
||||
$this->trigger('after_delete', $options);
|
||||
}
|
||||
return $result;
|
||||
@@ -2711,15 +2766,15 @@ class Query
|
||||
* 触发事件
|
||||
* @access protected
|
||||
* @param string $event 事件名
|
||||
* @param mixed $options 当前查询参数
|
||||
* @param mixed $params 额外参数
|
||||
* @return bool
|
||||
*/
|
||||
protected function trigger($event, $options = [])
|
||||
protected function trigger($event, $params = [])
|
||||
{
|
||||
$result = false;
|
||||
if (isset(self::$event[$event])) {
|
||||
$callback = self::$event[$event];
|
||||
$result = call_user_func_array($callback, [$options, $this]);
|
||||
$result = call_user_func_array($callback, [$params, $this]);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user