Skip to content

Commit 6ea7e95

Browse files
committed
修正
1 parent d1bd02b commit 6ea7e95

File tree

9 files changed

+127
-13
lines changed

9 files changed

+127
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Db类用法:
1414
~~~
1515
use think\Db;
1616
// 数据库配置信息设置
17-
Db::config(['数据库配置参数(数组)']);
17+
Db::setConfig(['数据库配置参数(数组)']);
1818
// 进行CURD操作
1919
Db::table('user')->find();
2020
~~~

src/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function __construct($data = [])
8787
// 当前模型名
8888
$name = str_replace('\\', '/', static::class);
8989
$this->name = basename($name);
90-
if ($config['class_suffix']) {
90+
if (!empty($config['class_suffix'])) {
9191
$suffix = basename(dirname($name));
9292
$this->name = substr($this->name, 0, -strlen($suffix));
9393
}

src/db/Connection.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use PDOStatement;
1717
use think\Db;
1818
use think\db\exception\BindParamException;
19+
use think\db\exception\PDOException;
1920
use think\Exception;
20-
use think\exception\PDOException;
2121

2222
abstract class Connection
2323
{
@@ -54,7 +54,7 @@ abstract class Connection
5454
protected static $info = [];
5555

5656
// 数据库日志
57-
protected $log = [];
57+
protected static $log = [];
5858

5959
// 使用Builder类
6060
protected $builderClassName;
@@ -108,6 +108,10 @@ abstract class Connection
108108
'query' => '\\think\\db\\Query',
109109
// 是否需要断线重连
110110
'break_reconnect' => false,
111+
// 数据字段缓存路径
112+
'schema_path' => '',
113+
// 模型类后缀
114+
'class_suffix' => false,
111115
];
112116

113117
// PDO连接参数
@@ -174,7 +178,7 @@ public static function instance($config = [], $name = false)
174178

175179
$class = false !== strpos($options['type'], '\\') ? $options['type'] : '\\think\\db\\connector\\' . ucwords($options['type']);
176180
// 记录初始化信息
177-
$this->log[] = '[ DB ] INIT ' . $options['type'];
181+
self::$log[] = '[ DB ] INIT ' . $options['type'];
178182

179183
if (true === $name) {
180184
$name = md5(serialize($config));
@@ -1777,7 +1781,7 @@ protected function debug($start, $sql = '')
17771781
if ($start) {
17781782
$this->queryStartTime = microtime(true);
17791783
} else {
1780-
$runtime = number_format((microtime(true) - $this->queryEndTime), 6);
1784+
$runtime = number_format((microtime(true) - $this->queryStartTime), 6);
17811785
$sql = $sql ?: $this->getLastsql();
17821786
$result = [];
17831787

@@ -1831,7 +1835,7 @@ protected function triggerSql($sql, $runtime, $explain = [])
18311835

18321836
public function log($log)
18331837
{
1834-
$this->config['debug'] && $this->log[] = $log;
1838+
$this->config['debug'] && self::$log[] = $log;
18351839
}
18361840

18371841
public function getSqlLog()

src/db/Query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
use think\Db;
1717
use think\db\exception\BindParamException;
1818
use think\db\exception\DataNotFoundException;
19+
use think\db\exception\DbException;
1920
use think\db\exception\ModelNotFoundException;
21+
use think\db\exception\PDOException;
2022
use think\Exception;
21-
use think\exception\DbException;
22-
use think\exception\PDOException;
2323
use think\Model;
2424
use think\model\Relation;
2525
use think\model\relation\OneToOne;

src/db/exception/BindParamException.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace think\db\exception;
1313

14-
use think\exception\DbException;
15-
1614
/**
1715
* PDO参数绑定异常
1816
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: yunwuxin <448901948@qq.com>
10+
// +----------------------------------------------------------------------
11+
12+
namespace think\db\exception;
13+
14+
class ClassNotFoundException extends \RuntimeException
15+
{
16+
protected $class;
17+
public function __construct($message, $class = '')
18+
{
19+
$this->message = $message;
20+
$this->class = $class;
21+
}
22+
23+
/**
24+
* 获取类名
25+
* @access public
26+
* @return string
27+
*/
28+
public function getClass()
29+
{
30+
return $this->class;
31+
}
32+
}

src/db/exception/DbException.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
10+
// +----------------------------------------------------------------------
11+
12+
namespace think\db\exception;
13+
14+
use think\Exception;
15+
16+
/**
17+
* Database相关异常处理类
18+
*/
19+
class DbException extends Exception
20+
{
21+
/**
22+
* DbException constructor.
23+
* @param string $message
24+
* @param array $config
25+
* @param string $sql
26+
* @param int $code
27+
*/
28+
public function __construct($message, array $config, $sql, $code = 10500)
29+
{
30+
$this->message = $message;
31+
$this->code = $code;
32+
33+
$this->setData('Database Status', [
34+
'Error Code' => $code,
35+
'Error Message' => $message,
36+
'Error SQL' => $sql,
37+
]);
38+
39+
unset($config['username'], $config['password']);
40+
$this->setData('Database Config', $config);
41+
}
42+
43+
}

src/db/exception/ModelNotFoundException.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace think\db\exception;
1313

14-
use think\exception\DbException;
15-
1614
class ModelNotFoundException extends DbException
1715
{
1816
protected $model;

src/db/exception/PDOException.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
10+
// +----------------------------------------------------------------------
11+
12+
namespace think\db\exception;
13+
14+
/**
15+
* PDO异常处理类
16+
* 重新封装了系统的\PDOException类
17+
*/
18+
class PDOException extends DbException
19+
{
20+
/**
21+
* PDOException constructor.
22+
* @param \PDOException $exception
23+
* @param array $config
24+
* @param string $sql
25+
* @param int $code
26+
*/
27+
public function __construct(\PDOException $exception, array $config, $sql, $code = 10501)
28+
{
29+
$error = $exception->errorInfo;
30+
31+
$this->setData('PDO Error Info', [
32+
'SQLSTATE' => $error[0],
33+
'Driver Error Code' => isset($error[1]) ? $error[1] : 0,
34+
'Driver Error Message' => isset($error[2]) ? $error[2] : '',
35+
]);
36+
37+
parent::__construct($exception->getMessage(), $config, $sql, $code);
38+
}
39+
}

0 commit comments

Comments
 (0)