内核更新

This commit is contained in:
2017-03-16 12:03:34 +08:00
parent b355535009
commit a4d58f9f09
28 changed files with 438 additions and 217 deletions

View File

@@ -98,14 +98,18 @@ abstract class Builder
$result = [];
foreach ($data as $key => $val) {
$item = $this->parseKey($key, $options);
if (is_object($val) && method_exists($val, '__toString')) {
// 对象数据写入
$val = $val->__toString();
}
if (false === strpos($key, '.') && !in_array($key, $fields, true)) {
if ($options['strict']) {
throw new Exception('fields not exists:[' . $key . ']');
}
} elseif (isset($val[0]) && 'exp' == $val[0]) {
$result[$item] = $val[1];
} elseif (is_null($val)) {
$result[$item] = 'NULL';
} elseif (isset($val[0]) && 'exp' == $val[0]) {
$result[$item] = $val[1];
} elseif (is_scalar($val)) {
// 过滤非标量数据
if (0 === strpos($val, ':') && $this->query->isBind(substr($val, 1))) {
@@ -115,9 +119,6 @@ abstract class Builder
$this->query->bind('__data__' . $key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
$result[$item] = ':__data__' . $key;
}
} elseif (is_object($val) && method_exists($val, '__toString')) {
// 对象数据写入
$result[$item] = $val->__toString();
}
}
return $result;
@@ -279,6 +280,13 @@ abstract class Builder
$whereStr .= empty($whereStr) ? substr(implode(' ', $str), strlen($key) + 1) : implode(' ', $str);
}
if (!empty($options['soft_delete'])) {
// 附加软删除条件
list($field, $condition) = $options['soft_delete'];
$whereStr = $whereStr ? '( ' . $whereStr . ' ) AND ' : '';
$whereStr = $whereStr . $this->parseWhereItem($field, $condition, '', $options, $binds);
}
return $whereStr;
}
@@ -379,7 +387,7 @@ abstract class Builder
} else {
$zone = implode(',', $this->parseValue($value, $field));
}
$whereStr .= $key . ' ' . $exp . ' (' . $zone . ')';
$whereStr .= $key . ' ' . $exp . ' (' . (empty($zone) ? "''" : $zone) . ')';
}
} elseif (in_array($exp, ['NOT BETWEEN', 'BETWEEN'])) {
// BETWEEN 查询

View File

@@ -893,7 +893,7 @@ class Query
} else {
$name = $alias . '.' . $key;
}
$fields[] = $name . ' AS ' . $val;
$fields[$name] = $val;
$this->options['map'][$val] = $name;
}
}
@@ -1120,6 +1120,20 @@ class Query
return $this;
}
/**
* 设置软删除字段及条件
* @access public
* @param false|string $field 查询字段
* @param mixed $condition 查询条件
* @return $this
*/
public function useSoftDelete($field, $condition = null)
{
if ($field) {
$this->options['soft_delete'] = [$field, $condition ?: ['null', '']];
}
}
/**
* 分析查询表达式
* @access public
@@ -1895,7 +1909,7 @@ class Query
$relation = Loader::parseName($relation, 1, false);
$model = $class->$relation();
if ($model instanceof OneToOne && 0 == $model->getEagerlyType()) {
$model->eagerly($this, $relation, $subRelation, $closure, $first);
$model->removeOption()->eagerly($this, $relation, $subRelation, $closure, $first);
$first = false;
} elseif ($closure) {
$with[$key] = $closure;
@@ -2210,7 +2224,7 @@ class Query
// 执行操作
$result = '' == $sql ? 0 : $this->execute($sql, $bind);
if ($result) {
if (isset($where[$pk])) {
if (is_string($pk) && isset($where[$pk])) {
$data[$pk] = $where[$pk];
} elseif (is_string($pk) && isset($key) && strpos($key, '|')) {
list($a, $val) = explode('|', $key);
@@ -2302,7 +2316,7 @@ class Query
}
}
if (isset($cache)) {
if (isset($cache) && $resultSet) {
// 缓存数据集
$this->cacheData($key, $resultSet, $cache);
}
@@ -2459,7 +2473,7 @@ class Query
$result = isset($resultSet[0]) ? $resultSet[0] : null;
}
if (isset($cache)) {
if (isset($cache) && $result) {
// 缓存数据
$this->cacheData($key, $result, $cache);
}

View File

@@ -36,10 +36,11 @@ class Mysql extends Builder
$key = 'json_extract(' . $field . ', \'$.' . $name . '\')';
} elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
list($table, $key) = explode('.', $key, 2);
if ('__TABLE__' == $table) {
$table = $this->query->getTable();
}
if (isset($options['alias'][$table])) {
$table = $options['alias'][$table];
} elseif ('__TABLE__' == $table) {
$table = $this->query->getTable();
}
}
if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {

View File

@@ -55,10 +55,11 @@ class Pgsql extends Builder
$key = $field . '->>\'' . $name . '\'';
} elseif (strpos($key, '.')) {
list($table, $key) = explode('.', $key, 2);
if ('__TABLE__' == $table) {
$table = $this->query->getTable();
}
if (isset($options['alias'][$table])) {
$table = $options['alias'][$table];
} elseif ('__TABLE__' == $table) {
$table = $this->query->getTable();
}
}
if (isset($table)) {

View File

@@ -60,10 +60,11 @@ class Sqlite extends Builder
$key = trim($key);
if (strpos($key, '.')) {
list($table, $key) = explode('.', $key, 2);
if ('__TABLE__' == $table) {
$table = $this->query->getTable();
}
if (isset($options['alias'][$table])) {
$table = $options['alias'][$table];
} elseif ('__TABLE__' == $table) {
$table = $this->query->getTable();
}
}
if (isset($table)) {

View File

@@ -73,10 +73,11 @@ class Sqlsrv extends Builder
$key = trim($key);
if (strpos($key, '.') && !preg_match('/[,\'\"\(\)\[\s]/', $key)) {
list($table, $key) = explode('.', $key, 2);
if ('__TABLE__' == $table) {
$table = $this->query->getTable();
}
if (isset($options['alias'][$table])) {
$table = $options['alias'][$table];
} elseif ('__TABLE__' == $table) {
$table = $this->query->getTable();
}
}
if (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {