This commit is contained in:
尘缘
2016-05-09 16:35:46 +08:00
10 changed files with 147 additions and 65 deletions

View File

@@ -16,6 +16,12 @@ namespace think;
*/ */
class Db class Db
{ {
// 数组数据集
const RESULTSET_ARRAY = 1;
// 对象数据集
const RESULTSET_COLLECTION = 2;
// 自定义对象数据集
const RESULTSET_CLASS = 3;
// 数据库连接实例 // 数据库连接实例
private static $instances = []; private static $instances = [];
// 查询次数 // 查询次数

View File

@@ -44,7 +44,7 @@ abstract class Paginator
'fragment' => '' 'fragment' => ''
]; ];
public function __construct(Collection $items, $listRows, $currentPage = null, $simple = false, $total = null, $options = []) public function __construct($items, $listRows, $currentPage = null, $simple = false, $total = null, $options = [])
{ {
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
@@ -53,17 +53,18 @@ abstract class Paginator
$this->simple = $simple; $this->simple = $simple;
$this->listRows = $listRows; $this->listRows = $listRows;
$this->items = PaginatorCollection::make($items, $this);
if ($simple) { if ($simple) {
$this->currentPage = $this->setCurrentPage($currentPage); $this->currentPage = $this->setCurrentPage($currentPage);
$this->hasMore = count($items) > ($this->listRows); $this->hasMore = count($this->items) > ($this->listRows);
$items = $items->slice(0, $this->listRows); $this->items = $this->items->slice(0, $this->listRows);
} else { } else {
$this->lastPage = (int)ceil($total / $listRows); $this->lastPage = (int)ceil($total / $listRows);
$this->currentPage = $this->setCurrentPage($currentPage); $this->currentPage = $this->setCurrentPage($currentPage);
$this->hasMore = $this->currentPage < $this->lastPage; $this->hasMore = $this->currentPage < $this->lastPage;
} }
$this->items = PaginatorCollection::make($items, $this);
} }
public function items() public function items()
@@ -165,6 +166,7 @@ abstract class Paginator
/** /**
* 数据是否足够分页 * 数据是否足够分页
* @return boolean
*/ */
public function hasPages() public function hasPages()
{ {

View File

@@ -13,6 +13,7 @@ namespace think\db;
use PDO; use PDO;
use PDOStatement; use PDOStatement;
use think\Collection;
use think\Db; use think\Db;
use think\Debug; use think\Debug;
use think\Exception; use think\Exception;
@@ -52,6 +53,8 @@ abstract class Connection
/** @var PDO 当前连接ID */ /** @var PDO 当前连接ID */
protected $linkID; protected $linkID;
// 查询结果类型
protected $resultSetType = Db::RESULTSET_ARRAY;
// 查询结果类型 // 查询结果类型
protected $fetchType = PDO::FETCH_ASSOC; protected $fetchType = PDO::FETCH_ASSOC;
// 字段属性大小写 // 字段属性大小写
@@ -93,6 +96,8 @@ abstract class Connection
'slave_no' => '', 'slave_no' => '',
// 是否严格检查字段是否存在 // 是否严格检查字段是否存在
'fields_strict' => true, 'fields_strict' => true,
// 数据集返回类型
'resultset_type' => Db::RESULTSET_ARRAY,
]; ];
// PDO连接参数 // PDO连接参数
@@ -230,7 +235,10 @@ abstract class Connection
} }
// 记录当前字段属性大小写设置 // 记录当前字段属性大小写设置
$this->attrCase = $params[PDO::ATTR_CASE]; $this->attrCase = $params[PDO::ATTR_CASE];
// 记录数据集返回类型
if (isset($config['resultset_type'])) {
$this->resultSetType = $config['resultset_type'];
}
try { try {
if (empty($config['dsn'])) { if (empty($config['dsn'])) {
$config['dsn'] = $this->parseDsn($config); $config['dsn'] = $this->parseDsn($config);
@@ -294,12 +302,12 @@ abstract class Connection
* @param array $bind 参数绑定 * @param array $bind 参数绑定
* @param boolean $fetch 不执行只是获取SQL * @param boolean $fetch 不执行只是获取SQL
* @param boolean $master 是否在主服务器读操作 * @param boolean $master 是否在主服务器读操作
* @param bool $returnPdo 是否返回 PDOStatement 对象 * @param bool|string $class 指定返回的数据集对象
* @return mixed * @return mixed
* @throws DbBindParamException * @throws DbBindParamException
* @throws PDOException * @throws PDOException
*/ */
public function query($sql, $bind = [], $fetch = false, $master = false, $returnPdo = false) public function query($sql, $bind = [], $fetch = false, $master = false, $class = false)
{ {
$this->initConnect($master); $this->initConnect($master);
if (!$this->linkID) { if (!$this->linkID) {
@@ -329,7 +337,7 @@ abstract class Connection
$result = $this->PDOStatement->execute(); $result = $this->PDOStatement->execute();
// 调试结束 // 调试结束
$this->debug(false); $this->debug(false);
return $returnPdo ? $this->PDOStatement : $this->getResult(); return $this->getResult($class);
} catch (\PDOException $e) { } catch (\PDOException $e) {
throw new PDOException($e, $this->config, $this->queryStr); throw new PDOException($e, $this->config, $this->queryStr);
} }
@@ -443,12 +451,33 @@ abstract class Connection
/** /**
* 获得数据集 * 获得数据集
* @access protected * @access protected
* @return array * @param bool|string $class true 返回PDOStatement 字符串用于指定返回的类名
* @return mixed
*/ */
protected function getResult() protected function getResult($class = '')
{ {
if (true === $class) {
// 返回PDOStatement对象处理
return $this->PDOStatement;
}
$result = $this->PDOStatement->fetchAll($this->fetchType); $result = $this->PDOStatement->fetchAll($this->fetchType);
$this->numRows = count($result); $this->numRows = count($result);
if (!empty($class)) {
// 返回指定数据集对象类
return new $class($result);
}
switch ($this->resultSetType) {
case Db::RESULTSET_COLLECTION:
// 返回数据集Collection对象
$result = new Collection($result);
break;
case Db::RESULTSET_CLASS:
break;
case Db::RESULTSET_ARRAY:
default:
// 返回二维数组
}
return $result; return $result;
} }

View File

@@ -847,7 +847,19 @@ class Query
*/ */
public function fetchPdo($pdo = true) public function fetchPdo($pdo = true)
{ {
$this->options['fetch_pdo'] = $pdo; $this->options['fetch_class'] = $pdo;
return $this;
}
/**
* 指定数据集返回对象
* @access public
* @param string $class 指定返回的数据集对象类名
* @return $this
*/
public function fetchClass($class)
{
$this->options['fetch_class'] = $class;
return $this; return $this;
} }
@@ -1304,7 +1316,7 @@ class Query
// 生成查询SQL // 生成查询SQL
$sql = $this->builder()->select($options); $sql = $this->builder()->select($options);
// 执行查询操作 // 执行查询操作
$resultSet = $this->connection->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_pdo']); $resultSet = $this->connection->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_class']);
if (is_string($resultSet)) { if (is_string($resultSet)) {
// 返回SQL // 返回SQL
@@ -1322,7 +1334,7 @@ class Query
} }
// 返回结果处理 // 返回结果处理
if (!empty($resultSet)) { if ($resultSet) {
// 数据列表读取后的处理 // 数据列表读取后的处理
if (!empty($options['model'])) { if (!empty($options['model'])) {
@@ -1340,13 +1352,13 @@ class Query
} }
if (!empty($options['with'])) { 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'])) { } elseif (!empty($options['fail'])) {
throw new DbException('Data not Found', $options, $sql); throw new DbException('Data not Found', $options, $sql);
} }
return Collection::make($resultSet); return $resultSet;
} }
/** /**
@@ -1385,7 +1397,7 @@ class Query
// 生成查询SQL // 生成查询SQL
$sql = $this->builder()->select($options); $sql = $this->builder()->select($options);
// 执行查询 // 执行查询
$result = $this->connection->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_pdo']); $result = $this->connection->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_class']);
if (is_string($result)) { if (is_string($result)) {
// 返回SQL // 返回SQL
@@ -1404,19 +1416,19 @@ class Query
} }
// 数据处理 // 数据处理
if (!empty($result)) { if (!empty($result[0])) {
$data = $result[0]; $data = $result[0];
if (!empty($options['model'])) { if (!empty($options['model'])) {
// 返回模型对象 // 返回模型对象
$data = new $options['model']($data); $data = new $options['model']($data);
$data->isUpdate(true, $options['where']['AND']); $data->isUpdate(true, isset($options['where']['AND']) ? $options['where']['AND'] : null);
// 关联查询 // 关联查询
if (!empty($options['relation'])) { if (!empty($options['relation'])) {
$data->relationQuery($options['relation']); $data->relationQuery($options['relation']);
} }
if (!empty($options['with'])) { if (!empty($options['with'])) {
// 预载入 // 预载入
$data->eagerlyResult($data, $options['with']); $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : '');
} }
} }
} elseif (!empty($options['fail'])) { } elseif (!empty($options['fail'])) {
@@ -1539,7 +1551,7 @@ class Query
$options['strict'] = $this->connection->getConfig('fields_strict'); $options['strict'] = $this->connection->getConfig('fields_strict');
} }
foreach (['master', 'lock', 'fetch_pdo', 'fetch_sql', 'distinct'] as $name) { foreach (['master', 'lock', 'fetch_class', 'fetch_sql', 'distinct'] as $name) {
if (!isset($options[$name])) { if (!isset($options[$name])) {
$options[$name] = false; $options[$name] = false;
} }

View File

@@ -11,6 +11,7 @@
namespace think\db\connector; namespace think\db\connector;
use PDO;
use think\db\Connection; use think\db\Connection;
use think\Log; use think\Log;
@@ -54,7 +55,8 @@ class Mysql extends Connection
$tableName = str_replace('.', '`.`', $tableName); $tableName = str_replace('.', '`.`', $tableName);
} }
$sql = 'SHOW COLUMNS FROM `' . $tableName . '`'; $sql = 'SHOW COLUMNS FROM `' . $tableName . '`';
$result = $this->query($sql); $pdo = $this->linkID->query($sql);
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$info = []; $info = [];
if ($result) { if ($result) {
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
@@ -81,7 +83,8 @@ class Mysql extends Connection
public function getTables($dbName = '') public function getTables($dbName = '')
{ {
$sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES '; $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 = []; $info = [];
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
$info[$key] = current($val); $info[$key] = current($val);
@@ -98,7 +101,7 @@ class Mysql extends Connection
protected function getExplain($sql) protected function getExplain($sql)
{ {
$pdo = $this->linkID->query("EXPLAIN " . $sql); $pdo = $this->linkID->query("EXPLAIN " . $sql);
$result = $pdo->fetch(\PDO::FETCH_ASSOC); $result = $pdo->fetch(PDO::FETCH_ASSOC);
$result = array_change_key_case($result); $result = array_change_key_case($result);
if (isset($result['extra'])) { if (isset($result['extra'])) {
if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) { if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {

View File

@@ -11,6 +11,7 @@
namespace think\db\connector; namespace think\db\connector;
use PDO;
use think\Db; use think\Db;
use think\db\Connection; use think\db\Connection;
@@ -102,8 +103,9 @@ class Oracle extends Connection
public function getFields($tableName) public function getFields($tableName)
{ {
list($tableName) = explode(' ', $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 (+)"; $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 (+)";
$result = $this->query($url); $pdo = $this->linkID->query($sql);
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$info = []; $info = [];
if ($result) { if ($result) {
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
@@ -129,7 +131,8 @@ class Oracle extends Connection
*/ */
public function getTables() 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 = []; $info = [];
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
$info[$key] = current($val); $info[$key] = current($val);

View File

@@ -11,6 +11,7 @@
namespace think\db\connector; namespace think\db\connector;
use PDO;
use think\db\Connection; use think\db\Connection;
/** /**
@@ -43,7 +44,9 @@ class Pgsql extends Connection
public function getFields($tableName) public function getFields($tableName)
{ {
list($tableName) = explode(' ', $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 = []; $info = [];
if ($result) { if ($result) {
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
@@ -69,7 +72,9 @@ class Pgsql extends Connection
*/ */
public function getTables($dbName = '') 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 = []; $info = [];
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
$info[$key] = current($val); $info[$key] = current($val);

View File

@@ -11,6 +11,7 @@
namespace think\db\connector; namespace think\db\connector;
use PDO;
use think\db\Connection; use think\db\Connection;
/** /**
@@ -40,7 +41,9 @@ class Sqlite extends Connection
public function getFields($tableName) public function getFields($tableName)
{ {
list($tableName) = explode(' ', $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 = []; $info = [];
if ($result) { if ($result) {
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
@@ -66,9 +69,11 @@ class Sqlite extends Connection
*/ */
public function getTables($dbName = '') 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 " . "UNION ALL SELECT name FROM sqlite_temp_master "
. "WHERE type='table' ORDER BY name"); . "WHERE type='table' ORDER BY name";
$pdo = $this->linkID->query($sql);
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$info = []; $info = [];
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
$info[$key] = current($val); $info[$key] = current($val);

View File

@@ -51,13 +51,15 @@ class Sqlsrv extends Connection
public function getFields($tableName) public function getFields($tableName)
{ {
list($tableName) = explode(' ', $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 FROM information_schema.tables AS t
JOIN information_schema.columns AS c JOIN information_schema.columns AS c
ON t.table_catalog = c.table_catalog ON t.table_catalog = c.table_catalog
AND t.table_schema = c.table_schema AND t.table_schema = c.table_schema
AND t.table_name = c.table_name AND t.table_name = c.table_name
WHERE t.table_name = '$tableName'"); WHERE t.table_name = '$tableName'";
$pdo = $this->linkID->query($sql);
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$info = []; $info = [];
if ($result) { if ($result) {
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
@@ -83,10 +85,12 @@ class Sqlsrv extends Connection
*/ */
public function getTables($dbName = '') public function getTables($dbName = '')
{ {
$result = $this->query("SELECT TABLE_NAME $sql = "SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' WHERE TABLE_TYPE = 'BASE TABLE'
"); ";
$pdo = $this->linkID->query($sql);
$result = $pdo->fetchAll(PDO::FETCH_ASSOC);
$info = []; $info = [];
foreach ($result as $key => $val) { foreach ($result as $key => $val) {
$info[$key] = current($val); $info[$key] = current($val);

View File

@@ -11,7 +11,6 @@
namespace think\model; namespace think\model;
use think\Collection;
use think\Db; use think\Db;
use think\Exception; use think\Exception;
use think\Loader; use think\Loader;
@@ -115,9 +114,10 @@ class Relation
* @access public * @access public
* @param array $resultSet 数据集 * @param array $resultSet 数据集
* @param string $relation 关联名 * @param string $relation 关联名
* @param string $class 数据集对象名 为空表示数组
* @return array * @return array
*/ */
public function eagerlyResultSet($resultSet, $relation) public function eagerlyResultSet($resultSet, $relation, $class = '')
{ {
/** @var array $relations */ /** @var array $relations */
$relations = is_string($relation) ? explode(',', $relation) : $relation; $relations = is_string($relation) ? explode(',', $relation) : $relation;
@@ -167,7 +167,7 @@ class Relation
if (!isset($data[$result->$localKey])) { if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = []; $data[$result->$localKey] = [];
} }
$result->__set($relation, Collection::make($data[$result->$localKey])); $result->__set($relation, $this->resultSetBuild($data[$result->$localKey], $class));
} }
} }
break; break;
@@ -196,7 +196,7 @@ class Relation
$data[$result->$pk] = []; $data[$result->$pk] = [];
} }
$result->__set($relation, Collection::make($data[$result->$pk])); $result->__set($relation, $this->resultSetBuild($data[$result->$pk], $class));
} }
} }
break; break;
@@ -205,14 +205,27 @@ class Relation
return $resultSet; 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 * @access public
* @param Model $result 数据对象 * @param Model $result 数据对象
* @param string $relation 关联名 * @param string $relation 关联名
* @param string $class 数据集对象名 为空表示数组
* @return \think\Model * @return \think\Model
*/ */
public function eagerlyResult($result, $relation) public function eagerlyResult($result, $relation, $class = '')
{ {
$relations = is_string($relation) ? explode(',', $relation) : $relation; $relations = is_string($relation) ? explode(',', $relation) : $relation;
@@ -243,7 +256,7 @@ class Relation
if (!isset($data[$result->$localKey])) { if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = []; $data[$result->$localKey] = [];
} }
$result->__set($relation, Collection::make($data[$result->$localKey])); $result->__set($relation, $this->resultSetBuild($data[$result->$localKey], $class));
} }
break; break;
case self::BELONGS_TO_MANY: case self::BELONGS_TO_MANY:
@@ -257,7 +270,7 @@ class Relation
if (!isset($data[$pk])) { if (!isset($data[$pk])) {
$data[$pk] = []; $data[$pk] = [];
} }
$result->__set($relation, Collection::make($data[$pk])); $result->__set($relation, $this->resultSetBuild($data[$pk], $class));
} }
break; break;