From 144a6132e634e2ffd5675eba063da61c8e21aa90 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 9 May 2016 11:45:00 +0800 Subject: [PATCH] =?UTF-8?q?Db=E7=B1=BB=E5=A2=9E=E5=8A=A0RESULTSET=5FARRAY?= =?UTF-8?q?=20RESULTSET=5FCOLLECTION=20=E5=92=8C=20RESULTSET=5FCLASS=20?= =?UTF-8?q?=E5=B8=B8=E9=87=8F=20=E6=95=B0=E6=8D=AE=E5=BA=93Connection?= =?UTF-8?q?=E7=B1=BB=E5=A2=9E=E5=8A=A0resultSetType=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=20=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=9B=86=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Db.php | 6 ++++++ library/think/db/Connection.php | 31 +++++++++++++++++++++------ library/think/db/Query.php | 10 ++++----- library/think/db/connector/Mysql.php | 6 ++++-- library/think/db/connector/Oracle.php | 8 ++++--- library/think/db/connector/Pgsql.php | 8 +++++-- library/think/db/connector/Sqlite.php | 12 +++++++---- library/think/db/connector/Sqlsrv.php | 16 ++++++++------ library/think/model/Relation.php | 27 +++++++++++++++++------ 9 files changed, 89 insertions(+), 35 deletions(-) diff --git a/library/think/Db.php b/library/think/Db.php index bf67da62..d329b4e8 100644 --- a/library/think/Db.php +++ b/library/think/Db.php @@ -16,6 +16,12 @@ namespace think; */ class Db { + // 数组数据集 + const RESULTSET_ARRAY = 1; + // 对象数据集 + const RESULTSET_COLLECTION = 2; + // 自定义对象数据集 + const RESULTSET_CLASS = 3; // 数据库连接实例 private static $instances = []; // 查询次数 diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 0c5880a0..810f1246 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -13,6 +13,7 @@ namespace think\db; use PDO; use PDOStatement; +use think\Collection; use think\Db; use think\Debug; use think\Exception; @@ -52,6 +53,8 @@ abstract class Connection /** @var PDO 当前连接ID */ protected $linkID; + // 查询结果类型 + protected $resultSetType = Db::RESULTSET_ARRAY; // 查询结果类型 protected $fetchType = PDO::FETCH_ASSOC; // 字段属性大小写 @@ -294,7 +297,7 @@ abstract class Connection * @param array $bind 参数绑定 * @param boolean $fetch 不执行只是获取SQL * @param boolean $master 是否在主服务器读操作 - * @param bool $returnPdo 是否返回 PDOStatement 对象 + * @param bool|string $returnPdo 是否返回 PDOStatement 对象 如果为字符串则指定类 * @return mixed * @throws DbBindParamException * @throws PDOException @@ -329,7 +332,7 @@ abstract class Connection $result = $this->PDOStatement->execute(); // 调试结束 $this->debug(false); - return $returnPdo ? $this->PDOStatement : $this->getResult(); + return true === $returnPdo ? $this->PDOStatement : $this->getResult($returnPdo); } catch (\PDOException $e) { throw new PDOException($e, $this->config, $this->queryStr); } @@ -403,8 +406,8 @@ abstract class Connection $val = $this->quote(is_array($val) ? $val[0] : $val); // 判断占位符 $sql = is_numeric($key) ? - substr_replace($sql, $val, strpos($sql, '?'), 1) : - str_replace([':' . $key . ')', ':' . $key . ' '], [$val . ')', $val . ' '], $sql . ' '); + substr_replace($sql, $val, strpos($sql, '?'), 1) : + str_replace([':' . $key . ')', ':' . $key . ' '], [$val . ')', $val . ' '], $sql . ' '); } } return $sql; @@ -443,12 +446,28 @@ abstract class Connection /** * 获得数据集 * @access protected - * @return array + * @param string $class 针对RESULTSET_CLASS 用于指定的类名 + * @return mixed */ - protected function getResult() + protected function getResult($class = '') { $result = $this->PDOStatement->fetchAll($this->fetchType); $this->numRows = count($result); + switch ($this->resultSetType) { + case Db::RESULTSET_COLLECTION: + // 返回数据集Collection对象 + $result = new Collection($result); + break; + case Db::RESULTSET_CLASS: + // 返回指定对象类 + if (!empty($class)) { + $result = new $class($result); + } + break; + case Db::RESULTSET_ARRAY: + default: + // 返回二维数组 + } return $result; } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 643a9d7d..9ae2b4c0 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1322,7 +1322,7 @@ class Query } // 返回结果处理 - if (!empty($resultSet)) { + if ($resultSet) { // 数据列表读取后的处理 if (!empty($options['model'])) { @@ -1340,13 +1340,13 @@ class Query } if (!empty($options['with'])) { // 预载入 - $resultSet = $result->eagerlyResultSet($resultSet, $options['with']); + $resultSet = $result->eagerlyResultSet($resultSet, $options['with'], is_class($resultSet) ? get_class($resultSet) : ''); } } } elseif (!empty($options['fail'])) { throw new DbException('Data not Found', $options, $sql); } - return Collection::make($resultSet); + return $resultSet; } /** @@ -1404,7 +1404,7 @@ class Query } // 数据处理 - if (!empty($result)) { + if (!empty($result[0])) { $data = $result[0]; if (!empty($options['model'])) { // 返回模型对象 @@ -1416,7 +1416,7 @@ class Query } if (!empty($options['with'])) { // 预载入 - $data->eagerlyResult($data, $options['with']); + $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : ''); } } } elseif (!empty($options['fail'])) { diff --git a/library/think/db/connector/Mysql.php b/library/think/db/connector/Mysql.php index 66b16a78..3e85d73a 100644 --- a/library/think/db/connector/Mysql.php +++ b/library/think/db/connector/Mysql.php @@ -54,7 +54,8 @@ class Mysql extends Connection $tableName = str_replace('.', '`.`', $tableName); } $sql = 'SHOW COLUMNS FROM `' . $tableName . '`'; - $result = $this->query($sql); + $pdo = $this->linkID->query($sql); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); $info = []; if ($result) { foreach ($result as $key => $val) { @@ -81,7 +82,8 @@ class Mysql extends Connection public function getTables($dbName = '') { $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; - $result = $this->query($sql); + $pdo = $this->linkID->query($sql); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); diff --git a/library/think/db/connector/Oracle.php b/library/think/db/connector/Oracle.php index 87ceaf8b..55b38327 100644 --- a/library/think/db/connector/Oracle.php +++ b/library/think/db/connector/Oracle.php @@ -102,8 +102,9 @@ class Oracle extends Connection public function getFields($tableName) { list($tableName) = explode(' ', $tableName); - $url = "select a.column_name,data_type,DECODE (nullable, 'Y', 0, 1) notnull,data_default, DECODE (A .column_name,b.column_name,1,0) pk from all_tab_columns a,(select column_name from all_constraints c, all_cons_columns col where c.constraint_name = col.constraint_name and c.constraint_type = 'P' and c.table_name = '" . strtoupper($tableName) . "' ) b where table_name = '" . strtoupper($tableName) . "' and a.column_name = b.column_name (+)"; - $result = $this->query($url); + $sql = "select a.column_name,data_type,DECODE (nullable, 'Y', 0, 1) notnull,data_default, DECODE (A .column_name,b.column_name,1,0) pk from all_tab_columns a,(select column_name from all_constraints c, all_cons_columns col where c.constraint_name = col.constraint_name and c.constraint_type = 'P' and c.table_name = '" . strtoupper($tableName) . "' ) b where table_name = '" . strtoupper($tableName) . "' and a.column_name = b.column_name (+)"; + $pdo = $this->linkID->query($sql); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); $info = []; if ($result) { foreach ($result as $key => $val) { @@ -129,7 +130,8 @@ class Oracle extends Connection */ public function getTables() { - $result = $this->query("select table_name from all_tables"); + $pdo = $this->linkID->query("select table_name from all_tables"); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); diff --git a/library/think/db/connector/Pgsql.php b/library/think/db/connector/Pgsql.php index 03406efe..f5c78d5f 100644 --- a/library/think/db/connector/Pgsql.php +++ b/library/think/db/connector/Pgsql.php @@ -43,7 +43,9 @@ class Pgsql extends Connection public function getFields($tableName) { list($tableName) = explode(' ', $tableName); - $result = $this->query('select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(' . $tableName . ');'); + $sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(' . $tableName . ');'; + $pdo = $this->linkID->query($sql); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); $info = []; if ($result) { foreach ($result as $key => $val) { @@ -69,7 +71,9 @@ class Pgsql extends Connection */ public function getTables($dbName = '') { - $result = $this->query("select tablename as Tables_in_test from pg_tables where schemaname ='public'"); + $sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'"; + $pdo = $this->linkID->query($sql); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); diff --git a/library/think/db/connector/Sqlite.php b/library/think/db/connector/Sqlite.php index 74ecc40e..9758d46f 100644 --- a/library/think/db/connector/Sqlite.php +++ b/library/think/db/connector/Sqlite.php @@ -40,7 +40,9 @@ class Sqlite extends Connection public function getFields($tableName) { list($tableName) = explode(' ', $tableName); - $result = $this->query('PRAGMA table_info( ' . $tableName . ' )'); + $sql = 'PRAGMA table_info( ' . $tableName . ' )'; + $pdo = $this->linkID->query($sql); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); $info = []; if ($result) { foreach ($result as $key => $val) { @@ -66,10 +68,12 @@ class Sqlite extends Connection */ public function getTables($dbName = '') { - $result = $this->query("SELECT name FROM sqlite_master WHERE type='table' " + $sql = "SELECT name FROM sqlite_master WHERE type='table' " . "UNION ALL SELECT name FROM sqlite_temp_master " - . "WHERE type='table' ORDER BY name"); - $info = []; + . "WHERE type='table' ORDER BY name"; + $pdo = $this->linkID->query($sql); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); + $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); } diff --git a/library/think/db/connector/Sqlsrv.php b/library/think/db/connector/Sqlsrv.php index 4d42ac1c..2c7f43e0 100644 --- a/library/think/db/connector/Sqlsrv.php +++ b/library/think/db/connector/Sqlsrv.php @@ -51,14 +51,16 @@ class Sqlsrv extends Connection public function getFields($tableName) { list($tableName) = explode(' ', $tableName); - $result = $this->query("SELECT column_name, data_type, column_default, is_nullable + $sql = "SELECT column_name, data_type, column_default, is_nullable FROM information_schema.tables AS t JOIN information_schema.columns AS c ON t.table_catalog = c.table_catalog AND t.table_schema = c.table_schema AND t.table_name = c.table_name - WHERE t.table_name = '$tableName'"); - $info = []; + WHERE t.table_name = '$tableName'"; + $pdo = $this->linkID->query($sql); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); + $info = []; if ($result) { foreach ($result as $key => $val) { $val = array_change_key_case($val); @@ -83,11 +85,13 @@ class Sqlsrv extends Connection */ public function getTables($dbName = '') { - $result = $this->query("SELECT TABLE_NAME + $sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' - "); - $info = []; + "; + $pdo = $this->linkID->query($sql); + $result = $pdo->fetchAll(\PDO::FETCH_ASSOC); + $info = []; foreach ($result as $key => $val) { $info[$key] = current($val); } diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index 22223dd2..654a7b9b 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -11,7 +11,6 @@ namespace think\model; -use think\Collection; use think\Db; use think\Exception; use think\Loader; @@ -115,9 +114,10 @@ class Relation * @access public * @param array $resultSet 数据集 * @param string $relation 关联名 + * @param string $class 数据集对象名 为空表示数组 * @return array */ - public function eagerlyResultSet($resultSet, $relation) + public function eagerlyResultSet($resultSet, $relation, $class = '') { /** @var array $relations */ $relations = is_string($relation) ? explode(',', $relation) : $relation; @@ -167,7 +167,7 @@ class Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->__set($relation, Collection::make($data[$result->$localKey])); + $result->__set($relation, $this->resultSetBuild($data[$result->$localKey], $class)); } } break; @@ -196,7 +196,7 @@ class Relation $data[$result->$pk] = []; } - $result->__set($relation, Collection::make($data[$result->$pk])); + $result->__set($relation, $this->resultSetBuild($data[$result->$pk], $class)); } } break; @@ -205,14 +205,27 @@ class Relation return $resultSet; } + /** + * 封装关联数据集 + * @access public + * @param array $resultSet 数据集 + * @param string $class 数据集类名 + * @return mixed + */ + protected function resultSetBuild($resultSet, $class = '') + { + return $class ? new $class($resultSet) : $resultSet; + } + /** * 预载入关联查询 返回模型对象 * @access public * @param Model $result 数据对象 * @param string $relation 关联名 + * @param string $class 数据集对象名 为空表示数组 * @return \think\Model */ - public function eagerlyResult($result, $relation) + public function eagerlyResult($result, $relation, $class = '') { $relations = is_string($relation) ? explode(',', $relation) : $relation; @@ -243,7 +256,7 @@ class Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->__set($relation, Collection::make($data[$result->$localKey])); + $result->__set($relation, $this->resultSetBuild($data[$result->$localKey], $class)); } break; case self::BELONGS_TO_MANY: @@ -257,7 +270,7 @@ class Relation if (!isset($data[$pk])) { $data[$pk] = []; } - $result->__set($relation, Collection::make($data[$pk])); + $result->__set($relation, $this->resultSetBuild($data[$pk], $class)); } break;