更新tp5内核
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace think\db;
|
||||
|
||||
use BadMethodCallException;
|
||||
use PDO;
|
||||
use think\Exception;
|
||||
|
||||
@@ -25,7 +26,7 @@ abstract class Builder
|
||||
protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'not like' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'exp' => 'EXP', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN', 'exists' => 'EXISTS', 'notexists' => 'NOT EXISTS', 'not exists' => 'NOT EXISTS', 'null' => 'NULL', 'notnull' => 'NOT NULL', 'not null' => 'NOT NULL', '> time' => '> TIME', '< time' => '< TIME', '>= time' => '>= TIME', '<= time' => '<= TIME', 'between time' => 'BETWEEN TIME', 'not between time' => 'NOT BETWEEN TIME', 'notbetween time' => 'NOT BETWEEN TIME'];
|
||||
|
||||
// SQL表达式
|
||||
protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%LOCK%%COMMENT%';
|
||||
protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%UNION%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT%%LOCK%%COMMENT%';
|
||||
protected $insertSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';
|
||||
protected $insertAllSql = '%INSERT% INTO %TABLE% (%FIELD%) %DATA% %COMMENT%';
|
||||
protected $updateSql = 'UPDATE %TABLE% SET %SET% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
|
||||
@@ -46,7 +47,7 @@ abstract class Builder
|
||||
/**
|
||||
* 获取当前的连接对象实例
|
||||
* @access public
|
||||
* @return void
|
||||
* @return Connection
|
||||
*/
|
||||
public function getConnection()
|
||||
{
|
||||
@@ -56,7 +57,7 @@ abstract class Builder
|
||||
/**
|
||||
* 获取当前的Query对象实例
|
||||
* @access public
|
||||
* @return void
|
||||
* @return Query
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
@@ -80,6 +81,7 @@ abstract class Builder
|
||||
* @param array $data 数据
|
||||
* @param array $options 查询参数
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function parseData($data, $options)
|
||||
{
|
||||
@@ -108,16 +110,26 @@ abstract class Builder
|
||||
}
|
||||
} elseif (is_null($val)) {
|
||||
$result[$item] = 'NULL';
|
||||
} elseif (isset($val[0]) && 'exp' == $val[0]) {
|
||||
$result[$item] = $val[1];
|
||||
} elseif (is_array($val) && !empty($val)) {
|
||||
switch ($val[0]) {
|
||||
case 'exp':
|
||||
$result[$item] = $val[1];
|
||||
break;
|
||||
case 'inc':
|
||||
$result[$item] = $this->parseKey($val[1]) . '+' . floatval($val[2]);
|
||||
break;
|
||||
case 'dec':
|
||||
$result[$item] = $this->parseKey($val[1]) . '-' . floatval($val[2]);
|
||||
break;
|
||||
}
|
||||
} elseif (is_scalar($val)) {
|
||||
// 过滤非标量数据
|
||||
if (0 === strpos($val, ':') && $this->query->isBind(substr($val, 1))) {
|
||||
$result[$item] = $val;
|
||||
} else {
|
||||
$key = str_replace('.', '_', $key);
|
||||
$this->query->bind('__data__' . $key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
|
||||
$result[$item] = ':__data__' . $key;
|
||||
$this->query->bind('data__' . $key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
|
||||
$result[$item] = ':data__' . $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -300,7 +312,7 @@ abstract class Builder
|
||||
|
||||
// 查询规则和条件
|
||||
if (!is_array($val)) {
|
||||
$val = ['=', $val];
|
||||
$val = is_null($val) ? ['null', ''] : ['=', $val];
|
||||
}
|
||||
list($exp, $value) = $val;
|
||||
|
||||
@@ -335,6 +347,11 @@ abstract class Builder
|
||||
$bindName = md5($bindName);
|
||||
}
|
||||
|
||||
if (is_object($value) && method_exists($value, '__toString')) {
|
||||
// 对象数据写入
|
||||
$value = $value->__toString();
|
||||
}
|
||||
|
||||
$bindType = isset($binds[$field]) ? $binds[$field] : PDO::PARAM_STR;
|
||||
if (is_scalar($value) && array_key_exists($field, $binds) && !in_array($exp, ['EXP', 'NOT NULL', 'NULL', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']) && strpos($exp, 'TIME') === false) {
|
||||
if (strpos($value, ':') !== 0 || !$this->query->isBind(substr($value, 1))) {
|
||||
@@ -492,7 +509,7 @@ abstract class Builder
|
||||
/**
|
||||
* limit分析
|
||||
* @access protected
|
||||
* @param mixed $lmit
|
||||
* @param mixed $limit
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLimit($limit)
|
||||
@@ -544,7 +561,11 @@ abstract class Builder
|
||||
foreach ($order as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
if ('[rand]' == $val) {
|
||||
$array[] = $this->parseRand();
|
||||
if (method_exists($this, 'parseRand')) {
|
||||
$array[] = $this->parseRand();
|
||||
} else {
|
||||
throw new BadMethodCallException('method not exists:' . get_class($this) . '-> parseRand');
|
||||
}
|
||||
} elseif (false === strpos($val, '(')) {
|
||||
$array[] = $this->parseKey($val, $options);
|
||||
} else {
|
||||
@@ -568,7 +589,7 @@ abstract class Builder
|
||||
*/
|
||||
protected function parseGroup($group)
|
||||
{
|
||||
return !empty($group) ? ' GROUP BY ' . $group : '';
|
||||
return !empty($group) ? ' GROUP BY ' . $this->parseKey($group) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -619,12 +640,12 @@ abstract class Builder
|
||||
unset($union['type']);
|
||||
foreach ($union as $u) {
|
||||
if ($u instanceof \Closure) {
|
||||
$sql[] = $type . ' ' . $this->parseClosure($u, false);
|
||||
$sql[] = $type . ' ' . $this->parseClosure($u);
|
||||
} elseif (is_string($u)) {
|
||||
$sql[] = $type . ' ' . $this->parseSqlTable($u);
|
||||
$sql[] = $type . ' ( ' . $this->parseSqlTable($u) . ' )';
|
||||
}
|
||||
}
|
||||
return implode(' ', $sql);
|
||||
return ' ' . implode(' ', $sql);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -649,12 +670,16 @@ abstract class Builder
|
||||
/**
|
||||
* 设置锁机制
|
||||
* @access protected
|
||||
* @param bool $locl
|
||||
* @param bool|string $lock
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLock($lock = false)
|
||||
{
|
||||
return $lock ? ' FOR UPDATE ' : '';
|
||||
if (is_bool($lock)) {
|
||||
return $lock ? ' FOR UPDATE ' : '';
|
||||
} elseif (is_string($lock)) {
|
||||
return ' ' . trim($lock) . ' ';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -723,8 +748,9 @@ abstract class Builder
|
||||
* @param array $options 表达式
|
||||
* @param bool $replace 是否replace
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function insertAll($dataSet, $options, $replace = false)
|
||||
public function insertAll($dataSet, $options = [], $replace = false)
|
||||
{
|
||||
// 获取合法的字段
|
||||
if ('*' == $options['field']) {
|
||||
@@ -733,7 +759,7 @@ abstract class Builder
|
||||
$fields = $options['field'];
|
||||
}
|
||||
|
||||
foreach ($dataSet as &$data) {
|
||||
foreach ($dataSet as $data) {
|
||||
foreach ($data as $key => $val) {
|
||||
if (!in_array($key, $fields, true)) {
|
||||
if ($options['strict']) {
|
||||
@@ -754,23 +780,25 @@ abstract class Builder
|
||||
}
|
||||
$value = array_values($data);
|
||||
$values[] = 'SELECT ' . implode(',', $value);
|
||||
|
||||
if (!isset($insertFields)) {
|
||||
$insertFields = array_map([$this, 'parseKey'], array_keys($data));
|
||||
}
|
||||
}
|
||||
$fields = array_map([$this, 'parseKey'], array_keys(reset($dataSet)));
|
||||
$sql = str_replace(
|
||||
|
||||
return str_replace(
|
||||
['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
|
||||
[
|
||||
$replace ? 'REPLACE' : 'INSERT',
|
||||
$this->parseTable($options['table'], $options),
|
||||
implode(' , ', $fields),
|
||||
implode(' , ', $insertFields),
|
||||
implode(' UNION ALL ', $values),
|
||||
$this->parseComment($options['comment']),
|
||||
], $this->insertAllSql);
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成slectinsert SQL
|
||||
* 生成select insert SQL
|
||||
* @access public
|
||||
* @param array $fields 数据
|
||||
* @param string $table 数据表
|
||||
@@ -791,7 +819,7 @@ abstract class Builder
|
||||
/**
|
||||
* 生成update SQL
|
||||
* @access public
|
||||
* @param array $fields 数据
|
||||
* @param array $data 数据
|
||||
* @param array $options 表达式
|
||||
* @return string
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user