From c6595f23aa3fe6d7fe70f13573d27758f4e38bee Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 1 Jun 2016 12:42:51 +0800 Subject: [PATCH] =?UTF-8?q?Connection=E7=B1=BB=E7=9A=84parseSqlTable?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=A7=BB=E5=8A=A8=E5=88=B0Query=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Builder.php | 2 +- library/think/db/Connection.php | 17 ----------------- library/think/db/Query.php | 28 ++++++++++++++++++++++++---- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index e9d22eff..bde2b930 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -66,7 +66,7 @@ abstract class Builder */ protected function parseSqlTable($sql) { - return $this->connection->parseSqlTable($sql); + return $this->query->parseSqlTable($sql); } /** diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index ddcfbe4b..31f3e1f0 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -643,23 +643,6 @@ abstract class Connection return true; } - /** - * 将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写) - * @access public - * @param string $sql sql语句 - * @return string - */ - public function parseSqlTable($sql) - { - if (false !== strpos($sql, '__')) { - $prefix = $this->config['prefix']; - $sql = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) { - return $prefix . strtolower($match[1]); - }, $sql); - } - return $sql; - } - /** * 获得查询次数 * @access public diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 52347859..eb738c9d 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -38,6 +38,8 @@ class Query protected $table = ''; // 当前数据表名称(不含前缀) protected $name = ''; + // 当前数据表前缀 + protected $prefix = ''; // 查询参数 protected $options = []; // 参数绑定 @@ -53,6 +55,7 @@ class Query { $this->connection = $connection ?: Db::connect([], true); $this->driver = $this->connection->getDriverName(); + $this->prefix = $this->connection->getConfig('prefix'); $this->model = $model; } @@ -126,7 +129,7 @@ class Query { if ($name || empty($this->table)) { $name = $name ?: $this->name; - $tableName = $this->getConfig('prefix'); + $tableName = $this->prefix; if ($name) { $tableName .= Loader::parseName($name); } @@ -136,6 +139,23 @@ class Query return $tableName; } + /** + * 将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写) + * @access public + * @param string $sql sql语句 + * @return string + */ + public function parseSqlTable($sql) + { + if (false !== strpos($sql, '__')) { + $prefix = $this->prefix; + $sql = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) { + return $prefix . strtolower($match[1]); + }, $sql); + } + return $sql; + } + /** * 执行查询 返回数据集 * @access public @@ -603,7 +623,7 @@ class Query } } } else { - $prefix = $this->getConfig('prefix'); + $prefix = $this->prefix; // 传入的表名为数组 if (is_array($join)) { if (0 !== $key = key($join)) { @@ -622,7 +642,7 @@ class Query } else { $join = trim($join); if (0 === strpos($join, '__')) { - $table = $this->connection->parseSqlTable($join); + $table = $this->parseSqlTable($join); } elseif (false === strpos($join, '(') && false === strpos($join, '.') && !empty($prefix) && 0 !== strpos($join, $prefix)) { // 传入的表名中不带有'('并且不以默认的表前缀开头时加上默认的表前缀 $table = $prefix . $join; @@ -1210,7 +1230,7 @@ class Query // 多表不获取字段信息 return false; } else { - $tableName = $this->connection->parseSqlTable($tableName); + $tableName = $this->parseSqlTable($tableName); } $guid = md5($tableName);