Skip to content

Commit

Permalink
改进
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 24, 2017
1 parent 2fa6eca commit ccb8aa2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 15 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"think\\": "src"
},
"files": [
"src/config.php"
]
}
}
2 changes: 1 addition & 1 deletion src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Db

public static function setConfig($config = [])
{
self::$config = $config;
self::$config = array_merge(self::$config, $config);
}

public static function getConfig($name = null)
Expand Down
4 changes: 1 addition & 3 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ public function newInstance($data = [], $isUpdate = false, $where = null)
protected function buildQuery()
{
// 设置当前模型 确保查询返回模型对象
$class = $this->query;
$query = new $class();
$query->connect($this->connection)->model($this);
$query = Db::connect($this->connection)->model($this);

// 设置当前数据表和模型名
if (!empty($this->table)) {
Expand Down
60 changes: 60 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;

Db::setConfig([
// 数据库类型
'type' => '',
// 服务器地址
'hostname' => '',
// 数据库名
'database' => '',
// 用户名
'username' => '',
// 密码
'password' => '',
// 端口
'hostport' => '',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => '',
// 数据库调试模式
'debug' => false,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 是否严格检查字段是否存在
'fields_strict' => true,
// 数据集返回类型
'resultset_type' => '',
// 自动写入时间戳字段
'auto_timestamp' => false,
// 时间字段取出后的默认时间格式
'datetime_format' => 'Y-m-d H:i:s',
// 是否需要进行SQL性能分析
'sql_explain' => false,
// Builder类
'builder' => '',
// Query类
'query' => '\\think\\db\\Query',
// 是否需要断线重连
'break_reconnect' => false,
]);
20 changes: 10 additions & 10 deletions src/db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ public function find(Query $query)

$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
}
Expand Down Expand Up @@ -882,7 +882,7 @@ public function select(Query $query)

$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
}
Expand Down Expand Up @@ -926,7 +926,7 @@ public function insert(Query $query, $replace = false, $getLastInsID = false, $s

$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
}
Expand Down Expand Up @@ -988,7 +988,7 @@ public function insertAll(Query $query, $dataSet = [], $replace = false, $limit

$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
} elseif (is_array($sql)) {
Expand Down Expand Up @@ -1021,7 +1021,7 @@ public function selectInsert(Query $query, $fields, $table)

$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
} else {
Expand Down Expand Up @@ -1088,7 +1088,7 @@ public function update(Query $query)
$sql = $this->builder->update($query);
$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
} else {
Expand Down Expand Up @@ -1150,7 +1150,7 @@ public function delete(Query $query)

$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
}
Expand Down Expand Up @@ -1217,7 +1217,7 @@ public function value(Query $query, $field, $default = null)

$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
}
Expand Down Expand Up @@ -1285,7 +1285,7 @@ public function column(Query $query, $field, $key = '')

$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
}
Expand Down Expand Up @@ -1349,7 +1349,7 @@ public function pdo(Query $query)

$bind = $query->getBind();

if ($options['fetch_sql']) {
if (!empty($options['fetch_sql'])) {
// 获取实际执行的SQL语句
return $this->getRealSql($sql, $bind);
}
Expand Down
2 changes: 1 addition & 1 deletion src/db/builder/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected function parseWhereItem(Query $query, $field, $val)
protected function parseDateTime(Query $query, $value, $key)
{
// 获取时间字段类型
$type = $this->connection->getTableInfo('', 'type');
$type = $this->connection->getFieldsType($query->getOptions('table') ?: $query->getTable());

if (isset($type[$key])) {
$value = strtotime($value) ?: $value;
Expand Down

0 comments on commit ccb8aa2

Please sign in to comment.