From e818c386e25315c7bcb858a2623aa231d37b8b3a Mon Sep 17 00:00:00 2001 From: molong Date: Sun, 18 Sep 2016 22:48:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=86=85=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/helper.php | 8 ++-- core/library/think/Model.php | 23 +--------- core/library/think/db/Builder.php | 3 ++ core/library/think/db/Query.php | 51 +++++----------------- core/library/think/db/connector/Mysql.php | 1 + core/library/think/db/connector/Pgsql.php | 1 + core/library/think/db/connector/Sqlite.php | 1 + core/library/think/db/connector/Sqlsrv.php | 1 + core/library/think/view/driver/Php.php | 6 ++- core/library/think/view/driver/Think.php | 2 + 10 files changed, 29 insertions(+), 68 deletions(-) diff --git a/core/helper.php b/core/helper.php index 892e9d0e..6ab2cb34 100644 --- a/core/helper.php +++ b/core/helper.php @@ -147,7 +147,7 @@ if (!function_exists('widget')) { /** * 渲染输出Widget * @param string $name Widget名称 - * @param array $data 传人的参数 + * @param array $data 传入的参数 * @return mixed */ function widget($name, $data = []) @@ -272,8 +272,8 @@ if (!function_exists('url')) { /** * Url生成 * @param string $url 路由地址 - * @param string|array $value 变量 - * @param bool|string $suffix 前缀 + * @param string|array $vars 变量 + * @param bool|string $suffix 生成的URL后缀 * @param bool|string $domain 域名 * @return string */ @@ -361,7 +361,7 @@ if (!function_exists('cache')) { } if ('' === $value) { // 获取缓存 - return Cache::get($name); + return 0 === strpos($name, '?') ? Cache::has(substr($name, 1)) : Cache::get($name); } elseif (is_null($value)) { // 删除缓存 return Cache::rm($name); diff --git a/core/library/think/Model.php b/core/library/think/Model.php index f1c91582..30b71d52 100644 --- a/core/library/think/Model.php +++ b/core/library/think/Model.php @@ -156,24 +156,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $query->name($this->name); } - if (!empty($this->field)) { - if (true === $this->field) { - $type = $this->db()->getTableInfo('', 'type'); - } else { - $type = []; - foreach ((array) $this->field as $key => $val) { - if (is_int($key)) { - $key = $val; - $val = 'varchar'; - } - $type[$key] = $val; - } - } - $query->setFieldType($type); - $this->field = array_keys($type); - $query->allowField($this->field); - } - if (!empty($this->pk)) { $query->pk($this->pk); } @@ -631,7 +613,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 检测字段 if (!empty($this->field)) { - $this->db(); foreach ($this->data as $key => $val) { if (!in_array($key, $this->field)) { unset($this->data[$key]); @@ -781,9 +762,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function allowField($field) { if (true === $field) { - $field = $this->db()->getTableInfo('', 'type'); - $this->db()->setFieldType($field); - $field = array_keys($field); + $field = $this->db()->getTableInfo('', 'fields'); } $this->field = $field; return $this; diff --git a/core/library/think/db/Builder.php b/core/library/think/db/Builder.php index 7a531e62..15a240ad 100644 --- a/core/library/think/db/Builder.php +++ b/core/library/think/db/Builder.php @@ -300,6 +300,9 @@ abstract class Builder $bindType = isset($binds[$field]) ? $binds[$field] : PDO::PARAM_STR; if (is_scalar($value) && array_key_exists($field, $binds) && !in_array($exp, ['EXP', 'NOT NULL', 'NULL', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']) && strpos($exp, 'TIME') === false) { if (strpos($value, ':') !== 0 || !$this->query->isBind(substr($value, 1))) { + if ($this->query->isBind($bindName)) { + $bindName .= '_' . uniqid(); + } $this->query->bind($bindName, $value, $bindType); $value = ':' . $bindName; } diff --git a/core/library/think/db/Query.php b/core/library/think/db/Query.php index 786ed0c9..04a8184c 100644 --- a/core/library/think/db/Query.php +++ b/core/library/think/db/Query.php @@ -43,10 +43,6 @@ class Query protected $name = ''; // 当前数据表主键 protected $pk; - // 当前表字段类型信息 - protected $fieldType = []; - // 当前允许的字段列表 - protected $allowField = []; // 当前数据表前缀 protected $prefix = ''; // 查询参数 @@ -741,11 +737,11 @@ class Query } if (true === $field) { // 获取全部字段 - $fields = !empty($this->allowField) && ('' == $tableName || $this->getTable() == $tableName) ? $this->allowField : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); + $fields = $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); $field = $fields ?: ['*']; } elseif ($except) { // 字段排除 - $fields = !empty($this->allowField) && ('' == $tableName || $this->getTable() == $tableName) ? $this->allowField : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); + $fields = $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); $field = $fields ? array_diff($fields, $field) : $field; } if ($tableName) { @@ -1260,35 +1256,6 @@ class Query return $this; } - /** - * 设置数据表字段 - * @access public - * @param string|array $field 字段信息 - * @return $this - */ - public function allowField($field) - { - if (true === $field) { - $field = $this->getTableInfo('', 'fields'); - } elseif (is_string($field)) { - $field = explode(',', $field); - } - $this->allowField = $field; - return $this; - } - - /** - * 设置字段类型 - * @access public - * @param array $fieldType 字段类型信息 - * @return $this - */ - public function setFieldType($fieldType = []) - { - $this->fieldType = $fieldType; - return $this; - } - /** * 指定数据表主键 * @access public @@ -1375,7 +1342,12 @@ class Query list($guid) = explode(' ', $tableName); if (!isset(self::$info[$guid])) { - $info = $this->connection->getFields($tableName); + // 读取缓存 + if (is_file(RUNTIME_PATH . 'schema/' . $guid . '.php')) { + $info = include RUNTIME_PATH . 'schema/' . $guid . '.php'; + } else { + $info = $this->connection->getFields($guid); + } $fields = array_keys($info); $bind = $type = []; foreach ($info as $key => $val) { @@ -1416,13 +1388,13 @@ class Query // 获取当前数据表字段信息 public function getTableFields($options) { - return !empty($this->allowField) ? $this->allowField : $this->getTableInfo($options['table'], 'fields'); + return $this->getTableInfo($options['table'], 'fields'); } // 获取当前数据表字段类型 public function getFieldsType($options) { - return !empty($this->fieldType) ? $this->fieldType : $this->getTableInfo($options['table'], 'type'); + return $this->getTableInfo($options['table'], 'type'); } // 获取当前数据表绑定信息 @@ -1998,9 +1970,6 @@ class Query $model = $this->model; $data = new $model($data); $data->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null); - if ($this->allowField) { - $data->allowField($this->allowField); - } // 关联查询 if (!empty($options['relation'])) { $data->relationQuery($options['relation']); diff --git a/core/library/think/db/connector/Mysql.php b/core/library/think/db/connector/Mysql.php index 42a6376b..47b069fb 100644 --- a/core/library/think/db/connector/Mysql.php +++ b/core/library/think/db/connector/Mysql.php @@ -86,6 +86,7 @@ class Mysql extends Connection */ public function getTables($dbName = '') { + $this->initConnect(true); $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; // 调试开始 $this->debug(true); diff --git a/core/library/think/db/connector/Pgsql.php b/core/library/think/db/connector/Pgsql.php index 06e08f54..6d653c14 100644 --- a/core/library/think/db/connector/Pgsql.php +++ b/core/library/think/db/connector/Pgsql.php @@ -77,6 +77,7 @@ class Pgsql extends Connection */ public function getTables($dbName = '') { + $this->initConnect(true); $sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'"; // 调试开始 $this->debug(true); diff --git a/core/library/think/db/connector/Sqlite.php b/core/library/think/db/connector/Sqlite.php index 1a52905e..95b023e5 100644 --- a/core/library/think/db/connector/Sqlite.php +++ b/core/library/think/db/connector/Sqlite.php @@ -74,6 +74,7 @@ class Sqlite extends Connection */ public function getTables($dbName = '') { + $this->initConnect(true); $sql = "SELECT name FROM sqlite_master WHERE type='table' " . "UNION ALL SELECT name FROM sqlite_temp_master " . "WHERE type='table' ORDER BY name"; diff --git a/core/library/think/db/connector/Sqlsrv.php b/core/library/think/db/connector/Sqlsrv.php index a8fc5414..18148051 100644 --- a/core/library/think/db/connector/Sqlsrv.php +++ b/core/library/think/db/connector/Sqlsrv.php @@ -99,6 +99,7 @@ class Sqlsrv extends Connection */ public function getTables($dbName = '') { + $this->initConnect(true); $sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' diff --git a/core/library/think/view/driver/Php.php b/core/library/think/view/driver/Php.php index 04af6df3..06f49a6a 100644 --- a/core/library/think/view/driver/Php.php +++ b/core/library/think/view/driver/Php.php @@ -141,7 +141,11 @@ class Php */ public function config($name, $value = null) { - + if (is_array($name)) { + $this->config = array_merge($this->config, $name); + } else { + $this->config[$name] = $value; + } } } diff --git a/core/library/think/view/driver/Think.php b/core/library/think/view/driver/Think.php index f100def1..e82f5139 100644 --- a/core/library/think/view/driver/Think.php +++ b/core/library/think/view/driver/Think.php @@ -140,8 +140,10 @@ class Think { if (is_array($name)) { $this->template->config($name); + $this->config = array_merge($this->config, $name); } else { $this->template->$name = $value; + $this->config[$name] = $value; } }