From a9577009f84428b1d23bf95ded350c525b0de3a2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 25 Apr 2016 13:35:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E5=92=8CQuery?= =?UTF-8?q?=E7=B1=BB=20name=E6=96=B9=E6=B3=95=E7=BA=B3=E5=85=A5=E9=93=BE?= =?UTF-8?q?=E5=BC=8F=E6=93=8D=E4=BD=9C=20=E6=9F=A5=E8=AF=A2=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=90=8E=E9=87=8A=E6=94=BE=20=E5=8E=9F=E6=9D=A5Connec?= =?UTF-8?q?tion=E7=B1=BB=E7=9A=84getTableInfo=E6=96=B9=E6=B3=95=E7=A7=BB?= =?UTF-8?q?=E5=85=A5=20Query=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 9 +- library/think/db/Builder.php | 8 +- library/think/db/Connection.php | 113 ++++---------------------- library/think/db/Query.php | 111 ++++++++++++++++++++----- library/think/db/connector/Mysql.php | 2 +- library/think/db/connector/Oracle.php | 2 +- library/think/db/connector/Pgsql.php | 2 +- library/think/db/connector/Sqlite.php | 2 +- library/think/db/connector/Sqlsrv.php | 2 +- 9 files changed, 123 insertions(+), 128 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index d9cb7b64..05f8b3cd 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -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); // 返回当前数据库对象 diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index 67b2813d..a96c2b1d 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -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']); } diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 11ccc99d..a2887745 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -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 diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 8f52ca94..4c920b51 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -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'])) { diff --git a/library/think/db/connector/Mysql.php b/library/think/db/connector/Mysql.php index 16272a94..32c75ce9 100644 --- a/library/think/db/connector/Mysql.php +++ b/library/think/db/connector/Mysql.php @@ -69,7 +69,7 @@ class Mysql extends Connection ]; } } - return $info; + return $this->fieldCase($info); } /** diff --git a/library/think/db/connector/Oracle.php b/library/think/db/connector/Oracle.php index a521278f..055aa604 100644 --- a/library/think/db/connector/Oracle.php +++ b/library/think/db/connector/Oracle.php @@ -118,7 +118,7 @@ class Oracle extends Connection ]; } } - return $info; + return $this->fieldCase($info); } /** diff --git a/library/think/db/connector/Pgsql.php b/library/think/db/connector/Pgsql.php index 5b376f9b..95c02a0b 100644 --- a/library/think/db/connector/Pgsql.php +++ b/library/think/db/connector/Pgsql.php @@ -58,7 +58,7 @@ class Pgsql extends Connection ]; } } - return $info; + return $this->fieldCase($info); } /** diff --git a/library/think/db/connector/Sqlite.php b/library/think/db/connector/Sqlite.php index f10883a0..7e85bd7b 100644 --- a/library/think/db/connector/Sqlite.php +++ b/library/think/db/connector/Sqlite.php @@ -55,7 +55,7 @@ class Sqlite extends Connection ]; } } - return $info; + return $this->fieldCase($info); } /** diff --git a/library/think/db/connector/Sqlsrv.php b/library/think/db/connector/Sqlsrv.php index 13063a52..2d107052 100644 --- a/library/think/db/connector/Sqlsrv.php +++ b/library/think/db/connector/Sqlsrv.php @@ -72,7 +72,7 @@ class Sqlsrv extends Connection ]; } } - return $info; + return $this->fieldCase($info); } /**