更新内核,API接口开发的一些尝试,后期会增加API接口开发这块

This commit is contained in:
2017-05-22 20:48:10 +08:00
parent a4d58f9f09
commit 426195eb90
47 changed files with 1339 additions and 513 deletions

View File

@@ -401,7 +401,7 @@ class Query
if (empty($this->options['table'])) {
$this->options['table'] = $this->getTable();
}
$key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options));
$key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options) . serialize($this->bind));
$result = Cache::get($key);
}
if (false === $result) {
@@ -444,14 +444,16 @@ class Query
if (empty($this->options['table'])) {
$this->options['table'] = $this->getTable();
}
$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options));
$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($this->options) . serialize($this->bind));
$result = Cache::get($guid);
}
if (false === $result) {
if (isset($this->options['field'])) {
unset($this->options['field']);
}
if ($key && '*' != $field) {
if (is_null($field)) {
$field = '*';
} elseif ($key && '*' != $field) {
$field = $key . ',' . $field;
}
$pdo = $this->field($field)->getPdo();
@@ -594,7 +596,7 @@ class Query
}
if ($lazyTime > 0) {
// 延迟写入
$guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition));
$guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition) . serialize($this->bind));
$step = $this->lazyWrite('inc', $guid, $step, $lazyTime);
if (false === $step) {
// 清空查询条件
@@ -623,7 +625,7 @@ class Query
}
if ($lazyTime > 0) {
// 延迟写入
$guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition));
$guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition) . serialize($this->bind));
$step = $this->lazyWrite('dec', $guid, $step, $lazyTime);
if (false === $step) {
// 清空查询条件
@@ -649,16 +651,16 @@ class Query
if (!Cache::has($guid . '_time')) {
// 计时开始
Cache::set($guid . '_time', $_SERVER['REQUEST_TIME'], 0);
Cache::$type($guid, $step, 0);
Cache::$type($guid, $step);
} elseif ($_SERVER['REQUEST_TIME'] > Cache::get($guid . '_time') + $lazyTime) {
// 删除缓存
$value = Cache::$type($guid, $step, 0);
$value = Cache::$type($guid, $step);
Cache::rm($guid);
Cache::rm($guid . '_time');
return 0 === $value ? false : $value;
} else {
// 更新缓存
Cache::$type($guid, $step, 0);
Cache::$type($guid, $step);
}
return false;
}
@@ -1132,6 +1134,7 @@ class Query
if ($field) {
$this->options['soft_delete'] = [$field, $condition ?: ['null', '']];
}
return $this;
}
/**
@@ -1773,21 +1776,21 @@ class Query
}
// 获取当前数据表字段信息
public function getTableFields($options)
public function getTableFields($table = '')
{
return $this->getTableInfo($options['table'], 'fields');
return $this->getTableInfo($table ?: $this->getOptions('table'), 'fields');
}
// 获取当前数据表字段类型
public function getFieldsType($options)
public function getFieldsType($table = '')
{
return $this->getTableInfo($options['table'], 'type');
return $this->getTableInfo($table ?: $this->getOptions('table'), 'type');
}
// 获取当前数据表绑定信息
public function getFieldsBind($options)
public function getFieldsBind($table = '')
{
$types = $this->getFieldsType($options);
$types = $this->getFieldsType($table);
$bind = [];
if ($types) {
foreach ($types as $key => $type) {
@@ -1900,6 +1903,9 @@ class Query
$closure = $relation;
$relation = $key;
$with[$key] = $key;
} elseif (is_array($relation)) {
$subRelation = $relation;
$relation = $key;
} elseif (is_string($relation) && strpos($relation, '.')) {
$with[$key] = $relation;
list($relation, $subRelation) = explode('.', $relation, 2);
@@ -1909,7 +1915,7 @@ class Query
$relation = Loader::parseName($relation, 1, false);
$model = $class->$relation();
if ($model instanceof OneToOne && 0 == $model->getEagerlyType()) {
$model->removeOption()->eagerly($this, $relation, $subRelation, $closure, $first);
$model->eagerly($this, $relation, $subRelation, $closure, $first);
$first = false;
} elseif ($closure) {
$with[$key] = $closure;
@@ -1997,7 +2003,7 @@ class Query
$relation = explode(',', $relation);
}
if (isset($this->options['relation'])) {
$this->options['relation'] = array_mrege($this->options['relation'], $relation);
$this->options['relation'] = array_merge($this->options['relation'], $relation);
} else {
$this->options['relation'] = $relation;
}
@@ -2111,9 +2117,10 @@ class Query
* 批量插入记录
* @access public
* @param mixed $dataSet 数据集
* @param boolean $replace 是否replace
* @return integer|string
*/
public function insertAll(array $dataSet)
public function insertAll(array $dataSet, $replace = false)
{
// 分析查询表达式
$options = $this->parseExpress();
@@ -2121,7 +2128,7 @@ class Query
return false;
}
// 生成SQL语句
$sql = $this->builder->insertAll($dataSet, $options);
$sql = $this->builder->insertAll($dataSet, $options, $replace);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
@@ -2203,7 +2210,7 @@ class Query
$options['where']['AND'] = $where;
}
} elseif (!isset($key) && is_string($pk) && isset($options['where']['AND'][$pk])) {
$key = $this->getCacheKey($options['where']['AND'][$pk], $options);
$key = $this->getCacheKey($options['where']['AND'][$pk], $options, $this->bind);
}
// 生成UPDATE SQL语句
@@ -2291,7 +2298,7 @@ class Query
// 判断查询缓存
$cache = $options['cache'];
unset($options['cache']);
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options) . serialize($this->bind));
$resultSet = Cache::get($key);
}
if (!$resultSet) {
@@ -2383,8 +2390,9 @@ class Query
* @access public
* @param mixed $value 缓存数据
* @param array $options 缓存参数
* @param array $bind 绑定参数
*/
protected function getCacheKey($value, $options)
protected function getCacheKey($value, $options, $bind = [])
{
if (is_scalar($value)) {
$data = $value;
@@ -2392,9 +2400,9 @@ class Query
$data = $value[1];
}
if (isset($data)) {
return 'think:' . $options['table'] . '|' . $data;
return 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data;
} else {
return md5(serialize($options));
return md5(serialize($options) . serialize($bind));
}
}
@@ -2422,7 +2430,7 @@ class Query
// 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);
$key = $this->getCacheKey($options['where']['AND'][$pk], $options, $this->bind);
}
$options['limit'] = 1;
@@ -2435,7 +2443,7 @@ class Query
} elseif (is_string($cache['key'])) {
$key = $cache['key'];
} elseif (!isset($key)) {
$key = md5(serialize($options));
$key = md5(serialize($options) . serialize($this->bind));
}
$result = Cache::get($key);
}
@@ -2646,7 +2654,7 @@ 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);
$key = $this->getCacheKey($options['where']['AND'][$pk], $options, $this->bind);
}
if (true !== $data && empty($options['where'])) {