Connection类的parseSqlTable方法移动到Query类

This commit is contained in:
thinkphp
2016-06-01 12:42:51 +08:00
parent 4d00aa539f
commit c6595f23aa
3 changed files with 25 additions and 22 deletions

View File

@@ -66,7 +66,7 @@ abstract class Builder
*/ */
protected function parseSqlTable($sql) protected function parseSqlTable($sql)
{ {
return $this->connection->parseSqlTable($sql); return $this->query->parseSqlTable($sql);
} }
/** /**

View File

@@ -643,23 +643,6 @@ abstract class Connection
return true; 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 * @access public

View File

@@ -38,6 +38,8 @@ class Query
protected $table = ''; protected $table = '';
// 当前数据表名称(不含前缀) // 当前数据表名称(不含前缀)
protected $name = ''; protected $name = '';
// 当前数据表前缀
protected $prefix = '';
// 查询参数 // 查询参数
protected $options = []; protected $options = [];
// 参数绑定 // 参数绑定
@@ -53,6 +55,7 @@ class Query
{ {
$this->connection = $connection ?: Db::connect([], true); $this->connection = $connection ?: Db::connect([], true);
$this->driver = $this->connection->getDriverName(); $this->driver = $this->connection->getDriverName();
$this->prefix = $this->connection->getConfig('prefix');
$this->model = $model; $this->model = $model;
} }
@@ -126,7 +129,7 @@ class Query
{ {
if ($name || empty($this->table)) { if ($name || empty($this->table)) {
$name = $name ?: $this->name; $name = $name ?: $this->name;
$tableName = $this->getConfig('prefix'); $tableName = $this->prefix;
if ($name) { if ($name) {
$tableName .= Loader::parseName($name); $tableName .= Loader::parseName($name);
} }
@@ -136,6 +139,23 @@ class Query
return $tableName; 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 * @access public
@@ -603,7 +623,7 @@ class Query
} }
} }
} else { } else {
$prefix = $this->getConfig('prefix'); $prefix = $this->prefix;
// 传入的表名为数组 // 传入的表名为数组
if (is_array($join)) { if (is_array($join)) {
if (0 !== $key = key($join)) { if (0 !== $key = key($join)) {
@@ -622,7 +642,7 @@ class Query
} else { } else {
$join = trim($join); $join = trim($join);
if (0 === strpos($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)) { } elseif (false === strpos($join, '(') && false === strpos($join, '.') && !empty($prefix) && 0 !== strpos($join, $prefix)) {
// 传入的表名中不带有'('并且不以默认的表前缀开头时加上默认的表前缀 // 传入的表名中不带有'('并且不以默认的表前缀开头时加上默认的表前缀
$table = $prefix . $join; $table = $prefix . $join;
@@ -1210,7 +1230,7 @@ class Query
// 多表不获取字段信息 // 多表不获取字段信息
return false; return false;
} else { } else {
$tableName = $this->connection->parseSqlTable($tableName); $tableName = $this->parseSqlTable($tableName);
} }
$guid = md5($tableName); $guid = md5($tableName);