From 64f2ae3df81aaef3868cb6598709036c471e9df8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 20 Jul 2016 17:01:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 69 ++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index b1451de8..f380b563 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -357,7 +357,7 @@ class Query protected function builder() { static $builder = []; - $driver = $this->driver; + $driver = $this->driver; if (!isset($builder[$driver])) { $class = '\\think\\db\\builder\\' . ucfirst($driver); $builder[$driver] = new $class($this->connection); @@ -660,7 +660,7 @@ class Query } if (count($join)) { // 有设置第二个元素则把第二元素作为表前缀 - $table = (string)current($join) . $table; + $table = (string) current($join) . $table; } elseif (false === strpos($table, '.')) { // 加上默认的表前缀 $table = $prefix . $table; @@ -975,7 +975,7 @@ class Query /** @var Paginator $class */ $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, 'getCurrentPage', ], $config['var_page']); @@ -983,7 +983,7 @@ class Query $page = $page < 1 ? 1 : $page; $config['path'] = isset($config['path']) ? $config['path'] : call_user_func([$class, 'getCurrentPath']); - + if (!$simple) { $options = $this->getOptions(); $total = $this->count(); @@ -1326,7 +1326,7 @@ class Query if (!isset($this->info[$guid])) { $info = $this->connection->getFields($tableName); $fields = array_keys($info); - $bind = $type = []; + $bind = $type = []; foreach ($info as $key => $val) { // 记录字段类型 $type[$key] = $val['type']; @@ -1444,7 +1444,7 @@ class Query $relation = $key; $with[$key] = $key; } elseif (is_string($relation) && strpos($relation, '.')) { - $with[$key] = $relation; + $with[$key] = $relation; list($relation, $subRelation) = explode('.', $relation, 2); } @@ -1479,7 +1479,7 @@ class Query if ($closure) { // 执行闭包查询 - call_user_func_array($closure, [& $this]); + call_user_func_array($closure, [ & $this]); //指定获取关联的字段 //需要在 回调中 调方法 withField 方法,如 // $query->where(['id'=>1])->withField('id,name'); @@ -1599,13 +1599,15 @@ class Query $options = $this->parseExpress(); // 生成SQL语句 $sql = $this->builder()->insert($data, $options, $replace); + // 获取参数绑定 + $bind = $this->getBind(); if ($options['fetch_sql']) { // 获取实际执行的SQL语句 - return $this->connection->getRealSql($sql, $this->bind); + return $this->connection->getRealSql($sql, $bind); } $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 = $this->builder()->insertAll($dataSet, $options); + // 获取参数绑定 + $bind = $this->getBind(); if ($options['fetch_sql']) { // 获取实际执行的SQL语句 - return $this->connection->getRealSql($sql, $this->bind); + return $this->connection->getRealSql($sql, $bind); } else { // 执行操作 - return $this->execute($sql, $this->getBind()); + return $this->execute($sql, $bind); } } @@ -1660,12 +1664,14 @@ class Query // 生成SQL语句 $table = $this->parseSqlTable($table); $sql = $this->builder()->selectInsert($fields, $table, $options); + // 获取参数绑定 + $bind = $this->getBind(); if ($options['fetch_sql']) { // 获取实际执行的SQL语句 - return $this->connection->getRealSql($sql, $this->bind); + return $this->connection->getRealSql($sql, $bind); } else { // 执行操作 - return $this->execute($sql, $this->getBind()); + return $this->execute($sql, $bind); } } @@ -1708,9 +1714,11 @@ class Query } // 生成UPDATE SQL语句 $sql = $this->builder()->update($data, $options); + // 获取参数绑定 + $bind = $this->getBind(); if ($options['fetch_sql']) { // 获取实际执行的SQL语句 - return $this->connection->getRealSql($sql, $this->bind); + return $this->connection->getRealSql($sql, $bind); } else { // 检测缓存 if (isset($key) && Cache::get($key)) { @@ -1718,7 +1726,7 @@ class Query 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) { return $data->select(); } elseif ($data instanceof \Closure) { - call_user_func_array($data, [& $this]); + call_user_func_array($data, [ & $this]); $data = null; } // 分析查询表达式 @@ -1760,12 +1768,14 @@ class Query if (!$resultSet) { // 生成查询SQL $sql = $this->builder()->select($options); + // 获取参数绑定 + $bind = $this->getBind(); if ($options['fetch_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) { // 返回PDOStatement对象 @@ -1819,7 +1829,7 @@ class Query if ($data instanceof Query) { return $data->find(); } elseif ($data instanceof \Closure) { - call_user_func_array($data, [& $this]); + call_user_func_array($data, [ & $this]); $data = null; } // 分析查询表达式 @@ -1845,12 +1855,14 @@ class Query if (!$result) { // 生成查询SQL $sql = $this->builder()->select($options); + // 获取参数绑定 + $bind = $this->getBind(); if ($options['fetch_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) { // 返回PDOStatement对象 @@ -2015,10 +2027,11 @@ class Query } // 生成删除SQL语句 $sql = $this->builder()->delete($options); - + // 获取参数绑定 + $bind = $this->getBind(); if ($options['fetch_sql']) { // 获取实际执行的SQL语句 - return $this->connection->getRealSql($sql, $this->bind); + return $this->connection->getRealSql($sql, $bind); } // 检测缓存 @@ -2027,7 +2040,7 @@ class Query Cache::rm($key); } // 执行操作 - return $this->execute($sql, $this->getBind()); + return $this->execute($sql, $bind); } /** @@ -2112,10 +2125,10 @@ class Query if (isset($options['page'])) { // 根据页数计算limit list($page, $listRows) = $options['page']; - $page = $page > 0 ? $page : 1; - $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); - $offset = $listRows * ($page - 1); - $options['limit'] = $offset . ',' . $listRows; + $page = $page > 0 ? $page : 1; + $listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20); + $offset = $listRows * ($page - 1); + $options['limit'] = $offset . ',' . $listRows; } $this->options = [];