改进Model类和Query类 name方法纳入链式操作 查询完成后释放 原来Connection类的getTableInfo方法移入 Query类

This commit is contained in:
thinkphp
2016-04-25 13:35:46 +08:00
parent 964248a10e
commit a9577009f8
9 changed files with 123 additions and 128 deletions

View File

@@ -800,9 +800,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
self::$links[$model] = Db::connect(static::$connection);
}
// 设置当前数据表和模型名
self::$links[$model]->setTable(static::$table);
$name = basename(str_replace('\\', '/', $model));
self::$links[$model]->name($name);
if (!empty(static::$table)) {
self::$links[$model]->table(static::$table);
} else {
$name = basename(str_replace('\\', '/', $model));
self::$links[$model]->name($name);
}
// 设置当前模型 确保查询返回模型对象
self::$links[$model]->model($model);
// 返回当前数据库对象

View File

@@ -79,7 +79,7 @@ abstract class Builder
}
// 获取绑定信息
$bind = $this->connection->getTableInfo($options['table'], 'bind');
$bind = $this->query->getTableInfo($options['table'], 'bind');
if ('*' == $options['field']) {
$fields = array_keys($bind);
} else {
@@ -223,8 +223,8 @@ abstract class Builder
$whereStr = '';
// 获取字段信息
$fields = $this->connection->getTableInfo($table, 'fields');
$binds = $this->connection->getTableInfo($table, 'bind');
$fields = $this->query->getTableInfo($table, 'fields');
$binds = $this->query->getTableInfo($table, 'bind');
foreach ($where as $key => $val) {
$str = [];
foreach ($val as $field => $value) {
@@ -564,7 +564,7 @@ abstract class Builder
{
// 获取合法的字段
if ('*' == $options['field']) {
$fields = $this->connection->getTableInfo($options['table'], 'fields');
$fields = $this->query->getTableInfo($options['table'], 'fields');
} else {
$fields = is_array($options['field']) ? $options['field'] : explode(',', $options['field']);
}

View File

@@ -19,7 +19,6 @@ use think\Debug;
use think\Exception;
use think\exception\DbBindParamException;
use think\exception\PDOException;
use think\Loader;
use think\Log;
abstract class Connection
@@ -123,42 +122,26 @@ abstract class Connection
}
/**
* 设置当前name
* 对返数据表字段信息进行大小写转换出来
* @access public
* @param string $name
* @return $this
* @param array $info 字段信息
* @return array
*/
public function name($name)
protected function fieldCase($info)
{
$this->name = $name;
return $this;
}
/**
* 指定当前的数据表
* @param string $table 数据表名称
* @access public
* @return void
*/
public function setTable($table)
{
$this->table = $table;
}
/**
* 得到当前的数据表
* @access public
* @return string
*/
public function getTable()
{
if (!$this->table) {
$tableName = $this->config['prefix'];
$tableName .= Loader::parseName($this->name);
} else {
$tableName = $this->table;
// 字段大小写转换
switch ($this->params[PDO::ATTR_CASE]) {
case PDO::CASE_LOWER:
$info = array_change_key_case($info);
break;
case PDO::CASE_UPPER:
$info = array_change_key_case($info, CASE_UPPER);
break;
case PDO::CASE_NATURAL:
default:
// 不做转换
}
return $tableName;
return $info;
}
/**
@@ -573,70 +556,6 @@ abstract class Connection
$this->linkID = null;
}
/**
* 获取数据表信息
* @access public
* @param string $fetch 获取信息类型 包括 fields type bind pk
* @param string $tableName 数据表名 留空自动获取
* @return mixed
*/
public function getTableInfo($tableName = '', $fetch = '')
{
static $_info = [];
if (!$tableName) {
$tableName = $this->getTable();
}
if (is_array($tableName)) {
$tableName = key($tableName) ?: current($tableName);
}
if (strpos($tableName, ',')) {
// 多表不获取字段信息
return false;
}
$guid = md5($tableName);
if (!isset($_info[$guid])) {
$info = $this->getFields($tableName);
// 字段大小写转换
switch ($this->params[PDO::ATTR_CASE]) {
case PDO::CASE_LOWER:
$info = array_change_key_case($info);
break;
case PDO::CASE_UPPER:
$info = array_change_key_case($info, CASE_UPPER);
break;
case PDO::CASE_NATURAL:
default:
// 不做转换
}
$fields = array_keys($info);
$bind = $type = [];
foreach ($info as $key => $val) {
// 记录字段类型
$type[$key] = $val['type'];
if (preg_match('/(int|double|float|decimal|real|numeric|serial)/is', $val['type'])) {
$bind[$key] = PDO::PARAM_INT;
} elseif (preg_match('/bool/is', $val['type'])) {
$bind[$key] = PDO::PARAM_BOOL;
} else {
$bind[$key] = PDO::PARAM_STR;
}
if (!empty($val['primary'])) {
$pk[] = $key;
}
}
if (isset($pk)) {
// 设置主键
$pk = count($pk) > 1 ? $pk : $pk[0];
} else {
$pk = null;
}
$result = ['fields' => $fields, 'type' => $type, 'bind' => $bind, 'pk' => $pk];
$_info[$guid] = $result;
}
return $fetch ? $_info[$guid][$fetch] : $_info[$guid];
}
/**
* 获取最近一次查询的sql语句
* @access public

View File

@@ -411,11 +411,11 @@ class Query
}
if (true === $field) {
// 获取全部字段
$fields = $this->connection->getTableInfo($tableName, 'fields');
$fields = $this->getTableInfo($tableName, 'fields');
$field = $fields ?: '*';
} elseif ($except) {
// 字段排除
$fields = $this->connection->getTableInfo($tableName, 'fields');
$fields = $this->getTableInfo($tableName, 'fields');
$field = $fields ? array_diff($fields, $field) : $field;
}
if ($tableName) {
@@ -832,6 +832,85 @@ class Query
return $this;
}
/**
* 设置当前name
* @access public
* @param string $name
* @return $this
*/
public function name($name)
{
$this->options['name'] = $name;
return $this;
}
/**
* 得到当前的数据表
* @access public
* @return string
*/
public function getTable()
{
if (empty($this->options['table'])) {
$tableName = $this->connection->getConfig('prefix');
$tableName .= Loader::parseName($this->options['name']);
} else {
$tableName = $this->options['table'];
}
return $tableName;
}
/**
* 获取数据表信息
* @access public
* @param string $fetch 获取信息类型 包括 fields type bind pk
* @param string $tableName 数据表名 留空自动获取
* @return mixed
*/
public function getTableInfo($tableName = '', $fetch = '')
{
static $_info = [];
if (!$tableName) {
$tableName = $this->getTable();
}
if (is_array($tableName)) {
$tableName = key($tableName) ?: current($tableName);
}
if (strpos($tableName, ',')) {
// 多表不获取字段信息
return false;
}
$guid = md5($tableName);
if (!isset($_info[$guid])) {
$info = $this->connection->getFields($tableName);
$fields = array_keys($info);
$bind = $type = [];
foreach ($info as $key => $val) {
// 记录字段类型
$type[$key] = $val['type'];
if (preg_match('/(int|double|float|decimal|real|numeric|serial)/is', $val['type'])) {
$bind[$key] = PDO::PARAM_INT;
} elseif (preg_match('/bool/is', $val['type'])) {
$bind[$key] = PDO::PARAM_BOOL;
} else {
$bind[$key] = PDO::PARAM_STR;
}
if (!empty($val['primary'])) {
$pk[] = $key;
}
}
if (isset($pk)) {
// 设置主键
$pk = count($pk) > 1 ? $pk : $pk[0];
} else {
$pk = null;
}
$result = ['fields' => $fields, 'type' => $type, 'bind' => $bind, 'pk' => $pk];
$_info[$guid] = $result;
}
return $fetch ? $_info[$guid][$fetch] : $_info[$guid];
}
/**
* 参数绑定
* @access public
@@ -908,7 +987,7 @@ class Query
if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) {
if (0 == $i) {
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->options['model'])));
$joinTable = $this->connection->getTable();
$joinTable = $this->getTable();
$this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
}
// 预载入封装
@@ -965,7 +1044,7 @@ class Query
*/
protected function parsePkWhere($data, &$options)
{
$pk = $this->connection->getTableInfo($options['table'], 'pk');
$pk = $this->getTableInfo($options['table'], 'pk');
// 获取当前数据表
if (!empty($options['alias'])) {
$alias = $options['alias'];
@@ -1058,7 +1137,7 @@ class Query
{
$options = $this->parseExpress();
if (empty($options['where'])) {
$pk = $this->connection->getTableInfo($options['table'], 'pk');
$pk = $this->getTableInfo($options['table'], 'pk');
// 如果存在主键数据 则自动作为更新条件
if (is_string($pk) && isset($data[$pk])) {
$where[$pk] = $data[$pk];
@@ -1254,27 +1333,21 @@ class Query
*/
public function chunk($count, $callback, $column = null)
{
$column = $column ?: $this->connection->getTableInfo('', 'pk');
$options = $this->getOptions();
if (empty($options['table'])) {
$table = $this->connection->getTable();
}
$column = $column ?: $this->getTableInfo('', 'pk');
$options = $this->getOptions();
$resultSet = $this->limit($count)->order($column, 'asc')->select();
while (!empty($resultSet)) {
if (false === call_user_func($callback, $resultSet)) {
return false;
}
$end = end($resultSet);
$lastId = is_array($end) ? $end[$column] : $end->$column;
$this->options($options)
$end = end($resultSet);
$lastId = is_array($end) ? $end[$column] : $end->$column;
$resultSet = $this->options($options)
->limit($count)
->where($column, '>', $lastId)
->order($column, 'asc');
if (isset($table)) {
$this->table($table);
}
$resultSet = $this->select();
->order($column, 'asc')
->select();
}
return true;
}
@@ -1339,7 +1412,7 @@ class Query
// 获取数据表
if (empty($options['table'])) {
$options['table'] = $this->connection->getTable();
$options['table'] = $this->getTable();
}
if (!isset($options['where'])) {

View File

@@ -69,7 +69,7 @@ class Mysql extends Connection
];
}
}
return $info;
return $this->fieldCase($info);
}
/**

View File

@@ -118,7 +118,7 @@ class Oracle extends Connection
];
}
}
return $info;
return $this->fieldCase($info);
}
/**

View File

@@ -58,7 +58,7 @@ class Pgsql extends Connection
];
}
}
return $info;
return $this->fieldCase($info);
}
/**

View File

@@ -55,7 +55,7 @@ class Sqlite extends Connection
];
}
}
return $info;
return $this->fieldCase($info);
}
/**

View File

@@ -72,7 +72,7 @@ class Sqlsrv extends Connection
];
}
}
return $info;
return $this->fieldCase($info);
}
/**