改进Query的参数绑定

This commit is contained in:
thinkphp
2016-07-20 17:01:18 +08:00
parent 8a5f4ff402
commit 64f2ae3df8

View File

@@ -357,7 +357,7 @@ class Query
protected function builder() protected function builder()
{ {
static $builder = []; static $builder = [];
$driver = $this->driver; $driver = $this->driver;
if (!isset($builder[$driver])) { if (!isset($builder[$driver])) {
$class = '\\think\\db\\builder\\' . ucfirst($driver); $class = '\\think\\db\\builder\\' . ucfirst($driver);
$builder[$driver] = new $class($this->connection); $builder[$driver] = new $class($this->connection);
@@ -660,7 +660,7 @@ class Query
} }
if (count($join)) { if (count($join)) {
// 有设置第二个元素则把第二元素作为表前缀 // 有设置第二个元素则把第二元素作为表前缀
$table = (string)current($join) . $table; $table = (string) current($join) . $table;
} elseif (false === strpos($table, '.')) { } elseif (false === strpos($table, '.')) {
// 加上默认的表前缀 // 加上默认的表前缀
$table = $prefix . $table; $table = $prefix . $table;
@@ -975,7 +975,7 @@ class Query
/** @var Paginator $class */ /** @var Paginator $class */
$class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']); $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']);
$page = isset($config['page']) ? (int)$config['page'] : call_user_func([ $page = isset($config['page']) ? (int) $config['page'] : call_user_func([
$class, $class,
'getCurrentPage', 'getCurrentPage',
], $config['var_page']); ], $config['var_page']);
@@ -983,7 +983,7 @@ class Query
$page = $page < 1 ? 1 : $page; $page = $page < 1 ? 1 : $page;
$config['path'] = isset($config['path']) ? $config['path'] : call_user_func([$class, 'getCurrentPath']); $config['path'] = isset($config['path']) ? $config['path'] : call_user_func([$class, 'getCurrentPath']);
if (!$simple) { if (!$simple) {
$options = $this->getOptions(); $options = $this->getOptions();
$total = $this->count(); $total = $this->count();
@@ -1326,7 +1326,7 @@ class Query
if (!isset($this->info[$guid])) { if (!isset($this->info[$guid])) {
$info = $this->connection->getFields($tableName); $info = $this->connection->getFields($tableName);
$fields = array_keys($info); $fields = array_keys($info);
$bind = $type = []; $bind = $type = [];
foreach ($info as $key => $val) { foreach ($info as $key => $val) {
// 记录字段类型 // 记录字段类型
$type[$key] = $val['type']; $type[$key] = $val['type'];
@@ -1444,7 +1444,7 @@ class Query
$relation = $key; $relation = $key;
$with[$key] = $key; $with[$key] = $key;
} elseif (is_string($relation) && strpos($relation, '.')) { } elseif (is_string($relation) && strpos($relation, '.')) {
$with[$key] = $relation; $with[$key] = $relation;
list($relation, $subRelation) = explode('.', $relation, 2); list($relation, $subRelation) = explode('.', $relation, 2);
} }
@@ -1479,7 +1479,7 @@ class Query
if ($closure) { if ($closure) {
// 执行闭包查询 // 执行闭包查询
call_user_func_array($closure, [& $this]); call_user_func_array($closure, [ & $this]);
//指定获取关联的字段 //指定获取关联的字段
//需要在 回调中 调方法 withField 方法,如 //需要在 回调中 调方法 withField 方法,如
// $query->where(['id'=>1])->withField('id,name'); // $query->where(['id'=>1])->withField('id,name');
@@ -1599,13 +1599,15 @@ class Query
$options = $this->parseExpress(); $options = $this->parseExpress();
// 生成SQL语句 // 生成SQL语句
$sql = $this->builder()->insert($data, $options, $replace); $sql = $this->builder()->insert($data, $options, $replace);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) { if ($options['fetch_sql']) {
// 获取实际执行的SQL语句 // 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind); return $this->connection->getRealSql($sql, $bind);
} }
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
// 执行操作 // 执行操作
return $this->execute($sql, $this->getBind(), $getLastInsID, $sequence); return $this->execute($sql, $bind, $getLastInsID, $sequence);
} }
/** /**
@@ -1636,12 +1638,14 @@ class Query
} }
// 生成SQL语句 // 生成SQL语句
$sql = $this->builder()->insertAll($dataSet, $options); $sql = $this->builder()->insertAll($dataSet, $options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) { if ($options['fetch_sql']) {
// 获取实际执行的SQL语句 // 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind); return $this->connection->getRealSql($sql, $bind);
} else { } else {
// 执行操作 // 执行操作
return $this->execute($sql, $this->getBind()); return $this->execute($sql, $bind);
} }
} }
@@ -1660,12 +1664,14 @@ class Query
// 生成SQL语句 // 生成SQL语句
$table = $this->parseSqlTable($table); $table = $this->parseSqlTable($table);
$sql = $this->builder()->selectInsert($fields, $table, $options); $sql = $this->builder()->selectInsert($fields, $table, $options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) { if ($options['fetch_sql']) {
// 获取实际执行的SQL语句 // 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind); return $this->connection->getRealSql($sql, $bind);
} else { } else {
// 执行操作 // 执行操作
return $this->execute($sql, $this->getBind()); return $this->execute($sql, $bind);
} }
} }
@@ -1708,9 +1714,11 @@ class Query
} }
// 生成UPDATE SQL语句 // 生成UPDATE SQL语句
$sql = $this->builder()->update($data, $options); $sql = $this->builder()->update($data, $options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) { if ($options['fetch_sql']) {
// 获取实际执行的SQL语句 // 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind); return $this->connection->getRealSql($sql, $bind);
} else { } else {
// 检测缓存 // 检测缓存
if (isset($key) && Cache::get($key)) { if (isset($key) && Cache::get($key)) {
@@ -1718,7 +1726,7 @@ class Query
Cache::rm($key); Cache::rm($key);
} }
// 执行操作 // 执行操作
return '' == $sql ? 0 : $this->execute($sql, $this->getBind()); return '' == $sql ? 0 : $this->execute($sql, $bind);
} }
} }
@@ -1736,7 +1744,7 @@ class Query
if ($data instanceof Query) { if ($data instanceof Query) {
return $data->select(); return $data->select();
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [& $this]); call_user_func_array($data, [ & $this]);
$data = null; $data = null;
} }
// 分析查询表达式 // 分析查询表达式
@@ -1760,12 +1768,14 @@ class Query
if (!$resultSet) { if (!$resultSet) {
// 生成查询SQL // 生成查询SQL
$sql = $this->builder()->select($options); $sql = $this->builder()->select($options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) { if ($options['fetch_sql']) {
// 获取实际执行的SQL语句 // 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind); return $this->connection->getRealSql($sql, $bind);
} }
// 执行查询操作 // 执行查询操作
$resultSet = $this->query($sql, $this->getBind(), $options['master'], $options['fetch_class']); $resultSet = $this->query($sql, $bind, $options['master'], $options['fetch_class']);
if ($resultSet instanceof \PDOStatement) { if ($resultSet instanceof \PDOStatement) {
// 返回PDOStatement对象 // 返回PDOStatement对象
@@ -1819,7 +1829,7 @@ class Query
if ($data instanceof Query) { if ($data instanceof Query) {
return $data->find(); return $data->find();
} elseif ($data instanceof \Closure) { } elseif ($data instanceof \Closure) {
call_user_func_array($data, [& $this]); call_user_func_array($data, [ & $this]);
$data = null; $data = null;
} }
// 分析查询表达式 // 分析查询表达式
@@ -1845,12 +1855,14 @@ class Query
if (!$result) { if (!$result) {
// 生成查询SQL // 生成查询SQL
$sql = $this->builder()->select($options); $sql = $this->builder()->select($options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) { if ($options['fetch_sql']) {
// 获取实际执行的SQL语句 // 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind); return $this->connection->getRealSql($sql, $bind);
} }
// 执行查询 // 执行查询
$result = $this->query($sql, $this->getBind(), $options['master'], $options['fetch_class']); $result = $this->query($sql, $bind, $options['master'], $options['fetch_class']);
if ($result instanceof \PDOStatement) { if ($result instanceof \PDOStatement) {
// 返回PDOStatement对象 // 返回PDOStatement对象
@@ -2015,10 +2027,11 @@ class Query
} }
// 生成删除SQL语句 // 生成删除SQL语句
$sql = $this->builder()->delete($options); $sql = $this->builder()->delete($options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) { if ($options['fetch_sql']) {
// 获取实际执行的SQL语句 // 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind); return $this->connection->getRealSql($sql, $bind);
} }
// 检测缓存 // 检测缓存
@@ -2027,7 +2040,7 @@ class Query
Cache::rm($key); Cache::rm($key);
} }
// 执行操作 // 执行操作
return $this->execute($sql, $this->getBind()); return $this->execute($sql, $bind);
} }
/** /**
@@ -2112,10 +2125,10 @@ class Query
if (isset($options['page'])) { if (isset($options['page'])) {
// 根据页数计算limit // 根据页数计算limit
list($page, $listRows) = $options['page']; list($page, $listRows) = $options['page'];
$page = $page > 0 ? $page : 1; $page = $page > 0 ? $page : 1;
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1); $offset = $listRows * ($page - 1);
$options['limit'] = $offset . ',' . $listRows; $options['limit'] = $offset . ',' . $listRows;
} }
$this->options = []; $this->options = [];