mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进Model类和Query类 name方法纳入链式操作 查询完成后释放 原来Connection类的getTableInfo方法移入 Query类
This commit is contained in:
@@ -800,9 +800,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
self::$links[$model] = Db::connect(static::$connection);
|
self::$links[$model] = Db::connect(static::$connection);
|
||||||
}
|
}
|
||||||
// 设置当前数据表和模型名
|
// 设置当前数据表和模型名
|
||||||
self::$links[$model]->setTable(static::$table);
|
if (!empty(static::$table)) {
|
||||||
$name = basename(str_replace('\\', '/', $model));
|
self::$links[$model]->table(static::$table);
|
||||||
self::$links[$model]->name($name);
|
} else {
|
||||||
|
$name = basename(str_replace('\\', '/', $model));
|
||||||
|
self::$links[$model]->name($name);
|
||||||
|
}
|
||||||
// 设置当前模型 确保查询返回模型对象
|
// 设置当前模型 确保查询返回模型对象
|
||||||
self::$links[$model]->model($model);
|
self::$links[$model]->model($model);
|
||||||
// 返回当前数据库对象
|
// 返回当前数据库对象
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ abstract class Builder
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取绑定信息
|
// 获取绑定信息
|
||||||
$bind = $this->connection->getTableInfo($options['table'], 'bind');
|
$bind = $this->query->getTableInfo($options['table'], 'bind');
|
||||||
if ('*' == $options['field']) {
|
if ('*' == $options['field']) {
|
||||||
$fields = array_keys($bind);
|
$fields = array_keys($bind);
|
||||||
} else {
|
} else {
|
||||||
@@ -223,8 +223,8 @@ abstract class Builder
|
|||||||
|
|
||||||
$whereStr = '';
|
$whereStr = '';
|
||||||
// 获取字段信息
|
// 获取字段信息
|
||||||
$fields = $this->connection->getTableInfo($table, 'fields');
|
$fields = $this->query->getTableInfo($table, 'fields');
|
||||||
$binds = $this->connection->getTableInfo($table, 'bind');
|
$binds = $this->query->getTableInfo($table, 'bind');
|
||||||
foreach ($where as $key => $val) {
|
foreach ($where as $key => $val) {
|
||||||
$str = [];
|
$str = [];
|
||||||
foreach ($val as $field => $value) {
|
foreach ($val as $field => $value) {
|
||||||
@@ -564,7 +564,7 @@ abstract class Builder
|
|||||||
{
|
{
|
||||||
// 获取合法的字段
|
// 获取合法的字段
|
||||||
if ('*' == $options['field']) {
|
if ('*' == $options['field']) {
|
||||||
$fields = $this->connection->getTableInfo($options['table'], 'fields');
|
$fields = $this->query->getTableInfo($options['table'], 'fields');
|
||||||
} else {
|
} else {
|
||||||
$fields = is_array($options['field']) ? $options['field'] : explode(',', $options['field']);
|
$fields = is_array($options['field']) ? $options['field'] : explode(',', $options['field']);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ use think\Debug;
|
|||||||
use think\Exception;
|
use think\Exception;
|
||||||
use think\exception\DbBindParamException;
|
use think\exception\DbBindParamException;
|
||||||
use think\exception\PDOException;
|
use think\exception\PDOException;
|
||||||
use think\Loader;
|
|
||||||
use think\Log;
|
use think\Log;
|
||||||
|
|
||||||
abstract class Connection
|
abstract class Connection
|
||||||
@@ -123,42 +122,26 @@ abstract class Connection
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置当前name
|
* 对返数据表字段信息进行大小写转换出来
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $name
|
* @param array $info 字段信息
|
||||||
* @return $this
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function name($name)
|
protected function fieldCase($info)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
// 字段大小写转换
|
||||||
return $this;
|
switch ($this->params[PDO::ATTR_CASE]) {
|
||||||
}
|
case PDO::CASE_LOWER:
|
||||||
|
$info = array_change_key_case($info);
|
||||||
/**
|
break;
|
||||||
* 指定当前的数据表
|
case PDO::CASE_UPPER:
|
||||||
* @param string $table 数据表名称
|
$info = array_change_key_case($info, CASE_UPPER);
|
||||||
* @access public
|
break;
|
||||||
* @return void
|
case PDO::CASE_NATURAL:
|
||||||
*/
|
default:
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
return $tableName;
|
return $info;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -573,70 +556,6 @@ abstract class Connection
|
|||||||
$this->linkID = null;
|
$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语句
|
* 获取最近一次查询的sql语句
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
@@ -411,11 +411,11 @@ class Query
|
|||||||
}
|
}
|
||||||
if (true === $field) {
|
if (true === $field) {
|
||||||
// 获取全部字段
|
// 获取全部字段
|
||||||
$fields = $this->connection->getTableInfo($tableName, 'fields');
|
$fields = $this->getTableInfo($tableName, 'fields');
|
||||||
$field = $fields ?: '*';
|
$field = $fields ?: '*';
|
||||||
} elseif ($except) {
|
} elseif ($except) {
|
||||||
// 字段排除
|
// 字段排除
|
||||||
$fields = $this->connection->getTableInfo($tableName, 'fields');
|
$fields = $this->getTableInfo($tableName, 'fields');
|
||||||
$field = $fields ? array_diff($fields, $field) : $field;
|
$field = $fields ? array_diff($fields, $field) : $field;
|
||||||
}
|
}
|
||||||
if ($tableName) {
|
if ($tableName) {
|
||||||
@@ -832,6 +832,85 @@ class Query
|
|||||||
return $this;
|
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
|
* @access public
|
||||||
@@ -908,7 +987,7 @@ class Query
|
|||||||
if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) {
|
if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) {
|
||||||
if (0 == $i) {
|
if (0 == $i) {
|
||||||
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->options['model'])));
|
$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);
|
$this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
|
||||||
}
|
}
|
||||||
// 预载入封装
|
// 预载入封装
|
||||||
@@ -965,7 +1044,7 @@ class Query
|
|||||||
*/
|
*/
|
||||||
protected function parsePkWhere($data, &$options)
|
protected function parsePkWhere($data, &$options)
|
||||||
{
|
{
|
||||||
$pk = $this->connection->getTableInfo($options['table'], 'pk');
|
$pk = $this->getTableInfo($options['table'], 'pk');
|
||||||
// 获取当前数据表
|
// 获取当前数据表
|
||||||
if (!empty($options['alias'])) {
|
if (!empty($options['alias'])) {
|
||||||
$alias = $options['alias'];
|
$alias = $options['alias'];
|
||||||
@@ -1058,7 +1137,7 @@ class Query
|
|||||||
{
|
{
|
||||||
$options = $this->parseExpress();
|
$options = $this->parseExpress();
|
||||||
if (empty($options['where'])) {
|
if (empty($options['where'])) {
|
||||||
$pk = $this->connection->getTableInfo($options['table'], 'pk');
|
$pk = $this->getTableInfo($options['table'], 'pk');
|
||||||
// 如果存在主键数据 则自动作为更新条件
|
// 如果存在主键数据 则自动作为更新条件
|
||||||
if (is_string($pk) && isset($data[$pk])) {
|
if (is_string($pk) && isset($data[$pk])) {
|
||||||
$where[$pk] = $data[$pk];
|
$where[$pk] = $data[$pk];
|
||||||
@@ -1254,27 +1333,21 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function chunk($count, $callback, $column = null)
|
public function chunk($count, $callback, $column = null)
|
||||||
{
|
{
|
||||||
$column = $column ?: $this->connection->getTableInfo('', 'pk');
|
$column = $column ?: $this->getTableInfo('', 'pk');
|
||||||
$options = $this->getOptions();
|
$options = $this->getOptions();
|
||||||
if (empty($options['table'])) {
|
|
||||||
$table = $this->connection->getTable();
|
|
||||||
}
|
|
||||||
$resultSet = $this->limit($count)->order($column, 'asc')->select();
|
$resultSet = $this->limit($count)->order($column, 'asc')->select();
|
||||||
|
|
||||||
while (!empty($resultSet)) {
|
while (!empty($resultSet)) {
|
||||||
if (false === call_user_func($callback, $resultSet)) {
|
if (false === call_user_func($callback, $resultSet)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$end = end($resultSet);
|
$end = end($resultSet);
|
||||||
$lastId = is_array($end) ? $end[$column] : $end->$column;
|
$lastId = is_array($end) ? $end[$column] : $end->$column;
|
||||||
$this->options($options)
|
$resultSet = $this->options($options)
|
||||||
->limit($count)
|
->limit($count)
|
||||||
->where($column, '>', $lastId)
|
->where($column, '>', $lastId)
|
||||||
->order($column, 'asc');
|
->order($column, 'asc')
|
||||||
if (isset($table)) {
|
->select();
|
||||||
$this->table($table);
|
|
||||||
}
|
|
||||||
$resultSet = $this->select();
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1339,7 +1412,7 @@ class Query
|
|||||||
|
|
||||||
// 获取数据表
|
// 获取数据表
|
||||||
if (empty($options['table'])) {
|
if (empty($options['table'])) {
|
||||||
$options['table'] = $this->connection->getTable();
|
$options['table'] = $this->getTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($options['where'])) {
|
if (!isset($options['where'])) {
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ class Mysql extends Connection
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $info;
|
return $this->fieldCase($info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ class Oracle extends Connection
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $info;
|
return $this->fieldCase($info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ class Pgsql extends Connection
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $info;
|
return $this->fieldCase($info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ class Sqlite extends Connection
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $info;
|
return $this->fieldCase($info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class Sqlsrv extends Connection
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $info;
|
return $this->fieldCase($info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user