Skip to content

Commit cba6f2a

Browse files
committed
修正mongo驱动
1 parent 2cfa440 commit cba6f2a

File tree

2 files changed

+58
-47
lines changed

2 files changed

+58
-47
lines changed

src/db/Mongo.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,27 @@
2222
use MongoDB\Driver\ReadPreference;
2323
use MongoDB\Driver\WriteConcern;
2424
use think\Collection;
25-
use think\db\Query as BaseQuery;
25+
use think\db\connector\Mongo as Connection;
26+
use think\db\Query;
2627

27-
class Mongo extends BaseQuery
28+
class Mongo extends Query
2829
{
30+
31+
/**
32+
* 架构函数
33+
* @access public
34+
*/
35+
public function __construct(Connection $connection = null)
36+
{
37+
if (is_null($connection)) {
38+
$this->connection = Connection::instance();
39+
} else {
40+
$this->connection = $connection;
41+
}
42+
43+
$this->prefix = $this->connection->getConfig('prefix');
44+
}
45+
2946
/**
3047
* 去除某个查询条件
3148
* @access public
@@ -697,8 +714,6 @@ protected function resultToModel(&$result, $options = [], $resultSet = false)
697714

698715
}
699716

700-
701-
702717
/**
703718
* 分析表达式(可用于查询或者写入操作)
704719
* @access protected

src/db/connector/Mongo.php

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use MongoDB\Driver\ReadPreference;
2424
use MongoDB\Driver\WriteConcern;
2525
use think\Collection;
26-
use think\Container;
2726
use think\Db;
2827
use think\db\builder\Mongo as Builder;
2928
use think\db\Mongo as Query;
@@ -61,6 +60,8 @@ class Mongo
6160
protected $options = [];
6261
// 数据表信息
6362
protected static $info = [];
63+
// 数据库日志
64+
protected static $log = [];
6465
// 数据库连接参数配置
6566
protected $config = [
6667
// 数据库类型
@@ -536,7 +537,12 @@ protected function triggerSql($sql, $runtime, $options = [])
536537

537538
public function logger($log, $type = 'sql')
538539
{
539-
$this->config['debug'] && Container::get('log')->record($log, $type);
540+
$this->config['debug'] && self::$log[] = $log;
541+
}
542+
543+
public function getSqlLog()
544+
{
545+
return self::$log;
540546
}
541547

542548
/**
@@ -550,14 +556,10 @@ protected function debug($start, $sql = '')
550556
{
551557
if (!empty($this->config['debug'])) {
552558
// 开启数据库调试模式
553-
$debug = Container::get('debug');
554559
if ($start) {
555-
$debug->remark('queryStartTime', 'time');
560+
$this->queryStartTime = microtime(true);
556561
} else {
557-
// 记录操作结束时间
558-
$debug->remark('queryEndTime', 'time');
559-
560-
$runtime = $debug->getRangeTime('queryStartTime', 'queryEndTime');
562+
$runtime = number_format((microtime(true) - $this->queryStartTime), 6);
561563

562564
$sql = $sql ?: $this->queryStr;
563565

@@ -865,9 +867,9 @@ public function update(Query $query)
865867
$writeResult = $this->execute($options['table'], $bulk, $writeConcern);
866868

867869
// 检测缓存
868-
if (isset($key) && Container::get('cache')->get($key)) {
870+
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
869871
// 删除缓存
870-
Container::get('cache')->rm($key);
872+
$cache->rm($key);
871873
}
872874

873875
$result = $writeResult->getModifiedCount();
@@ -933,9 +935,9 @@ public function delete(Query $query)
933935
$writeResult = $this->execute($options['table'], $bulk, $writeConcern);
934936

935937
// 检测缓存
936-
if (isset($key) && Container::get('cache')->get($key)) {
938+
if ($cache = Db::getCacheHandler() && isset($key) && $cache->get($key)) {
937939
// 删除缓存
938-
Container::get('cache')->rm($key);
940+
$cache->rm($key);
939941
}
940942

941943
$result = $writeResult->getDeletedCount();
@@ -988,14 +990,14 @@ public function getCursor(Query $query)
988990
*/
989991
public function select(Query $query)
990992
{
991-
$options = $query->getOptions();
992-
993-
$resultSet = false;
994-
if (!empty($options['cache'])) {
993+
$options = $query->getOptions();
994+
$cacheHandler = Db::getCacheHandler();
995+
$resultSet = false;
996+
if ($cacheHandler && !empty($options['cache'])) {
995997
// 判断查询缓存
996998
$cache = $options['cache'];
997999
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
998-
$resultSet = Container::get('cache')->get($key);
1000+
$resultSet = $cacheHandler->get($key);
9991001
}
10001002

10011003
if (!$resultSet) {
@@ -1039,24 +1041,24 @@ public function select(Query $query)
10391041
public function find(Query $query)
10401042
{
10411043
// 分析查询表达式
1042-
$options = $query->getOptions();
1043-
$pk = $query->getPk($options);
1044-
$data = $options['data'];
1045-
1046-
if (!empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['$and'][$pk])) {
1044+
$options = $query->getOptions();
1045+
$pk = $query->getPk($options);
1046+
$data = $options['data'];
1047+
$cacheHandler = Db::getCacheHandler();
1048+
if ($cacheHandler && !empty($options['cache']) && true === $options['cache']['key'] && is_string($pk) && isset($options['where']['$and'][$pk])) {
10471049
$key = $this->getCacheKey($options['where']['$and'][$pk], $options);
10481050
}
10491051

10501052
$result = false;
1051-
if (!empty($options['cache'])) {
1053+
if ($cacheHandler && !empty($options['cache'])) {
10521054
// 判断查询缓存
10531055
$cache = $options['cache'];
10541056
if (true === $cache['key'] && !is_null($data) && !is_array($data)) {
10551057
$key = 'mongo:' . $options['table'] . '|' . $data;
10561058
} elseif (!isset($key)) {
10571059
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
10581060
}
1059-
$result = Container::get('cache')->get($key);
1061+
$result = $cacheHandler->get($key);
10601062
}
10611063

10621064
if (false === $result) {
@@ -1112,13 +1114,7 @@ public function find(Query $query)
11121114
*/
11131115
protected function cacheData($key, $data, $config = [])
11141116
{
1115-
$cache = Container::get('cache');
1116-
1117-
if (isset($config['tag'])) {
1118-
$cache->tag($config['tag'])->set($key, $data, $config['expire']);
1119-
} else {
1120-
$cache->set($key, $data, $config['expire']);
1121-
}
1117+
Db::getCacheHandler()->set($key, $data, $config['expire']);
11221118
}
11231119

11241120
/**
@@ -1203,14 +1199,14 @@ public function getTableInfo($tableName, $fetch = '')
12031199
*/
12041200
public function value(Query $query, $field, $default = null)
12051201
{
1206-
$options = $query->getOptions();
1207-
1208-
$result = null;
1209-
if (!empty($options['cache'])) {
1202+
$options = $query->getOptions();
1203+
$cacheHandler = Db::getCacheHandler();
1204+
$result = null;
1205+
if ($cacheHandler && !empty($options['cache'])) {
12101206
// 判断查询缓存
12111207
$cache = $options['cache'];
12121208
$key = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($options));
1213-
$result = Container::get('cache')->get($key);
1209+
$result = $cacheHandler->get($key);
12141210
}
12151211

12161212
if (!$result) {
@@ -1256,14 +1252,14 @@ public function value(Query $query, $field, $default = null)
12561252
*/
12571253
public function column(Query $query, $field, $key = '')
12581254
{
1259-
$options = $query->getOptions();
1260-
1261-
$result = false;
1262-
if (!empty($options['cache'])) {
1255+
$options = $query->getOptions();
1256+
$cacheHandler = Db::getCacheHandler();
1257+
$result = false;
1258+
if ($cacheHandler && !empty($options['cache'])) {
12631259
// 判断查询缓存
12641260
$cache = $options['cache'];
12651261
$guid = is_string($cache['key']) ? $cache['key'] : md5($field . serialize($options));
1266-
$result = Container::get('cache')->get($guid);
1262+
$result = $cacheHandler->get($guid);
12671263
}
12681264

12691265
if (!$result) {
@@ -1359,10 +1355,10 @@ public function cmd(Query $query, $command, $extra = null, $db = null)
13591355
private static function parseConfig($config)
13601356
{
13611357
if (empty($config)) {
1362-
$config = Container::get('config')->pull('database');
1358+
$config = Db::getConfig();
13631359
} elseif (is_string($config) && false === strpos($config, '/')) {
13641360
// 支持读取配置参数
1365-
$config = Container::get('config')->get('database.' . $config);
1361+
$config = Db::getConfig($config);
13661362
}
13671363

13681364
if (is_string($config)) {

0 commit comments

Comments
 (0)