Connection.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  6. // +----------------------------------------------------------------------
  7. // | Author: liu21st <liu21st@gmail.com>
  8. // +----------------------------------------------------------------------
  9. namespace think\mongo;
  10. use MongoDB\BSON\ObjectID;
  11. use MongoDB\Driver\BulkWrite;
  12. use MongoDB\Driver\Command;
  13. use MongoDB\Driver\Cursor;
  14. use MongoDB\Driver\Exception\AuthenticationException;
  15. use MongoDB\Driver\Exception\BulkWriteException;
  16. use MongoDB\Driver\Exception\ConnectionException;
  17. use MongoDB\Driver\Exception\InvalidArgumentException;
  18. use MongoDB\Driver\Exception\RuntimeException;
  19. use MongoDB\Driver\Manager;
  20. use MongoDB\Driver\Query as MongoQuery;
  21. use MongoDB\Driver\ReadPreference;
  22. use MongoDB\Driver\WriteConcern;
  23. use think\Collection;
  24. use think\Db;
  25. use think\Debug;
  26. use think\Exception;
  27. use think\Log;
  28. /**
  29. * Mongo数据库驱动
  30. */
  31. class Connection
  32. {
  33. protected $dbName = ''; // dbName
  34. /** @var string 当前SQL指令 */
  35. protected $queryStr = '';
  36. // 查询数据类型
  37. protected $typeMap = 'array';
  38. protected $mongo; // MongoDb Object
  39. protected $cursor; // MongoCursor Object
  40. // 监听回调
  41. protected static $event = [];
  42. /** @var PDO[] 数据库连接ID 支持多个连接 */
  43. protected $links = [];
  44. /** @var PDO 当前连接ID */
  45. protected $linkID;
  46. protected $linkRead;
  47. protected $linkWrite;
  48. // 返回或者影响记录数
  49. protected $numRows = 0;
  50. // 错误信息
  51. protected $error = '';
  52. // 查询对象
  53. protected $query = [];
  54. // 查询参数
  55. protected $options = [];
  56. // 数据库连接参数配置
  57. protected $config = [
  58. // 数据库类型
  59. 'type' => '',
  60. // 服务器地址
  61. 'hostname' => '',
  62. // 数据库名
  63. 'database' => '',
  64. // 是否是复制集
  65. 'is_replica_set' => false,
  66. // 用户名
  67. 'username' => '',
  68. // 密码
  69. 'password' => '',
  70. // 端口
  71. 'hostport' => '',
  72. // 连接dsn
  73. 'dsn' => '',
  74. // 数据库连接参数
  75. 'params' => [],
  76. // 数据库编码默认采用utf8
  77. 'charset' => 'utf8',
  78. // 主键名
  79. 'pk' => '_id',
  80. // 主键类型
  81. 'pk_type' => 'ObjectID',
  82. // 数据库表前缀
  83. 'prefix' => '',
  84. // 数据库调试模式
  85. 'debug' => false,
  86. // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
  87. 'deploy' => 0,
  88. // 数据库读写是否分离 主从式有效
  89. 'rw_separate' => false,
  90. // 读写分离后 主服务器数量
  91. 'master_num' => 1,
  92. // 指定从服务器序号
  93. 'slave_no' => '',
  94. // 是否严格检查字段是否存在
  95. 'fields_strict' => true,
  96. // 数据集返回类型
  97. 'resultset_type' => 'array',
  98. // 自动写入时间戳字段
  99. 'auto_timestamp' => false,
  100. // 时间字段取出后的默认时间格式
  101. 'datetime_format' => 'Y-m-d H:i:s',
  102. // 是否需要进行SQL性能分析
  103. 'sql_explain' => false,
  104. // 是否_id转换为id
  105. 'pk_convert_id' => false,
  106. // typeMap
  107. 'type_map' => ['root' => 'array', 'document' => 'array'],
  108. // Query对象
  109. 'query' => '\\think\\mongo\\Query',
  110. ];
  111. /**
  112. * 架构函数 读取数据库配置信息
  113. * @access public
  114. * @param array $config 数据库配置数组
  115. */
  116. public function __construct(array $config = [])
  117. {
  118. if (!class_exists('\MongoDB\Driver\Manager')) {
  119. throw new Exception('require mongodb > 1.0');
  120. }
  121. if (!empty($config)) {
  122. $this->config = array_merge($this->config, $config);
  123. }
  124. }
  125. /**
  126. * 连接数据库方法
  127. * @access public
  128. * @param array $config 连接参数
  129. * @param integer $linkNum 连接序号
  130. * @throws InvalidArgumentException
  131. * @throws RuntimeException
  132. */
  133. public function connect(array $config = [], $linkNum = 0)
  134. {
  135. if (!isset($this->links[$linkNum])) {
  136. if (empty($config)) {
  137. $config = $this->config;
  138. } else {
  139. $config = array_merge($this->config, $config);
  140. }
  141. $this->dbName = $config['database'];
  142. $this->typeMap = $config['type_map'];
  143. if ($config['pk_convert_id'] && '_id' == $config['pk']) {
  144. $this->config['pk'] = 'id';
  145. }
  146. $host = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '');
  147. if ($config['debug']) {
  148. $startTime = microtime(true);
  149. }
  150. $this->links[$linkNum] = new Manager($host, $this->config['params']);
  151. if ($config['debug']) {
  152. // 记录数据库连接信息
  153. Log::record('[ DB ] CONNECT:[ UseTime:' . number_format(microtime(true) - $startTime, 6) . 's ] ' . $config['dsn'], 'sql');
  154. }
  155. }
  156. return $this->links[$linkNum];
  157. }
  158. /**
  159. * 指定当前使用的查询对象
  160. * @access public
  161. * @param Query $query 查询对象
  162. * @return $this
  163. */
  164. public function setQuery($query, $model = 'db')
  165. {
  166. $this->query[$model] = $query;
  167. return $this;
  168. }
  169. /**
  170. * 创建指定模型的查询对象
  171. * @access public
  172. * @param string $model 模型类名称
  173. * @param string $queryClass 查询对象类名
  174. * @return Query
  175. */
  176. public function getQuery($model = 'db', $queryClass = '')
  177. {
  178. if (!isset($this->query[$model])) {
  179. $class = $queryClass ?: $this->config['query'];
  180. $this->query[$model] = new $class($this, 'db' == $model ? '' : $model);
  181. }
  182. return $this->query[$model];
  183. }
  184. /**
  185. * 调用Query类的查询方法
  186. * @access public
  187. * @param string $method 方法名称
  188. * @param array $args 调用参数
  189. * @return mixed
  190. */
  191. public function __call($method, $args)
  192. {
  193. return call_user_func_array([$this->getQuery(), $method], $args);
  194. }
  195. /**
  196. * 获取数据库的配置参数
  197. * @access public
  198. * @param string $config 配置名称
  199. * @return mixed
  200. */
  201. public function getConfig($config = '')
  202. {
  203. return $config ? $this->config[$config] : $this->config;
  204. }
  205. /**
  206. * 设置数据库的配置参数
  207. * @access public
  208. * @param string $config 配置名称
  209. * @param mixed $value 配置值
  210. * @return void
  211. */
  212. public function setConfig($config, $value)
  213. {
  214. $this->config[$config] = $value;
  215. }
  216. /**
  217. * 获取Mongo Manager对象
  218. * @access public
  219. * @return Manager|null
  220. */
  221. public function getMongo()
  222. {
  223. if (!$this->mongo) {
  224. return;
  225. } else {
  226. return $this->mongo;
  227. }
  228. }
  229. /**
  230. * 设置/获取当前操作的database
  231. * @access public
  232. * @param string $db db
  233. * @throws Exception
  234. */
  235. public function db($db = null)
  236. {
  237. if (is_null($db)) {
  238. return $this->dbName;
  239. } else {
  240. $this->dbName = $db;
  241. }
  242. }
  243. /**
  244. * 执行查询
  245. * @access public
  246. * @param string $namespace 当前查询的collection
  247. * @param MongoQuery $query 查询对象
  248. * @param ReadPreference $readPreference readPreference
  249. * @param string|bool $class 返回的数据集类型
  250. * @param string|array $typeMap 指定返回的typeMap
  251. * @return mixed
  252. * @throws AuthenticationException
  253. * @throws InvalidArgumentException
  254. * @throws ConnectionException
  255. * @throws RuntimeException
  256. */
  257. public function query($namespace, MongoQuery $query, ReadPreference $readPreference = null, $class = false, $typeMap = null)
  258. {
  259. $this->initConnect(false);
  260. Db::$queryTimes++;
  261. if (false === strpos($namespace, '.')) {
  262. $namespace = $this->dbName . '.' . $namespace;
  263. }
  264. if ($this->config['debug'] && !empty($this->queryStr)) {
  265. // 记录执行指令
  266. $this->queryStr = 'db' . strstr($namespace, '.') . '.' . $this->queryStr;
  267. }
  268. $this->debug(true);
  269. $this->cursor = $this->mongo->executeQuery($namespace, $query, $readPreference);
  270. $this->debug(false);
  271. return $this->getResult($class, $typeMap);
  272. }
  273. /**
  274. * 执行指令
  275. * @access public
  276. * @param Command $command 指令
  277. * @param string $dbName 当前数据库名
  278. * @param ReadPreference $readPreference readPreference
  279. * @param string|bool $class 返回的数据集类型
  280. * @param string|array $typeMap 指定返回的typeMap
  281. * @return mixed
  282. * @throws AuthenticationException
  283. * @throws InvalidArgumentException
  284. * @throws ConnectionException
  285. * @throws RuntimeException
  286. */
  287. public function command(Command $command, $dbName = '', ReadPreference $readPreference = null, $class = false, $typeMap)
  288. {
  289. $this->initConnect(false);
  290. Db::$queryTimes++;
  291. $this->debug(true);
  292. $dbName = $dbName ?: $this->dbName;
  293. if ($this->config['debug'] && !empty($this->queryStr)) {
  294. $this->queryStr = 'db.' . $this->queryStr;
  295. }
  296. $this->cursor = $this->mongo->executeCommand($dbName, $command, $readPreference);
  297. $this->debug(false);
  298. return $this->getResult($class, $typeMap);
  299. }
  300. /**
  301. * 获得数据集
  302. * @access protected
  303. * @param bool|string $class true 返回Mongo cursor对象 字符串用于指定返回的类名
  304. * @param string|array $typeMap 指定返回的typeMap
  305. * @return mixed
  306. */
  307. protected function getResult($class = '', $typeMap = null)
  308. {
  309. if (true === $class) {
  310. return $this->cursor;
  311. }
  312. // 设置结果数据类型
  313. if (is_null($typeMap)) {
  314. $typeMap = $this->typeMap;
  315. }
  316. $typeMap = is_string($typeMap) ? ['root' => $typeMap] : $typeMap;
  317. $this->cursor->setTypeMap($typeMap);
  318. // 获取数据集
  319. $result = $this->cursor->toArray();
  320. if ($this->getConfig('pk_convert_id')) {
  321. // 转换ObjectID 字段
  322. foreach ($result as &$data) {
  323. $this->convertObjectID($data);
  324. }
  325. }
  326. $this->numRows = count($result);
  327. return $result;
  328. }
  329. /**
  330. * ObjectID处理
  331. * @access public
  332. * @param array $data
  333. * @return void
  334. */
  335. private function convertObjectID(&$data)
  336. {
  337. if (isset($data['_id'])) {
  338. $data['id'] = $data['_id']->__toString();
  339. unset($data['_id']);
  340. }
  341. }
  342. /**
  343. * 执行写操作
  344. * @access public
  345. * @param string $namespace
  346. * @param BulkWrite $bulk
  347. * @param WriteConcern $writeConcern
  348. *
  349. * @return WriteResult
  350. * @throws AuthenticationException
  351. * @throws InvalidArgumentException
  352. * @throws ConnectionException
  353. * @throws RuntimeException
  354. * @throws BulkWriteException
  355. */
  356. public function execute($namespace, BulkWrite $bulk, WriteConcern $writeConcern = null)
  357. {
  358. $this->initConnect(true);
  359. Db::$executeTimes++;
  360. if (false === strpos($namespace, '.')) {
  361. $namespace = $this->dbName . '.' . $namespace;
  362. }
  363. if ($this->config['debug'] && !empty($this->queryStr)) {
  364. // 记录执行指令
  365. $this->queryStr = 'db' . strstr($namespace, '.') . '.' . $this->queryStr;
  366. }
  367. $this->debug(true);
  368. $writeResult = $this->mongo->executeBulkWrite($namespace, $bulk, $writeConcern);
  369. $this->debug(false);
  370. $this->numRows = $writeResult->getMatchedCount();
  371. return $writeResult;
  372. }
  373. /**
  374. * 数据库日志记录(仅供参考)
  375. * @access public
  376. * @param string $type 类型
  377. * @param mixed $data 数据
  378. * @param array $options 参数
  379. * @return void
  380. */
  381. public function log($type, $data, $options = [])
  382. {
  383. if (!$this->config['debug']) {
  384. return;
  385. }
  386. if (is_array($data)) {
  387. array_walk_recursive($data, function (&$value) {
  388. if ($value instanceof ObjectID) {
  389. $value = $value->__toString();
  390. }
  391. });
  392. }
  393. switch (strtolower($type)) {
  394. case 'aggregate':
  395. $this->queryStr = 'runCommand(' . ($data ? json_encode($data) : '') . ');';
  396. break;
  397. case 'find':
  398. $this->queryStr = $type . '(' . ($data ? json_encode($data) : '') . ')';
  399. if (isset($options['sort'])) {
  400. $this->queryStr .= '.sort(' . json_encode($options['sort']) . ')';
  401. }
  402. if (isset($options['limit'])) {
  403. $this->queryStr .= '.limit(' . $options['limit'] . ')';
  404. }
  405. $this->queryStr .= ';';
  406. break;
  407. case 'insert':
  408. case 'remove':
  409. $this->queryStr = $type . '(' . ($data ? json_encode($data) : '') . ');';
  410. break;
  411. case 'update':
  412. $this->queryStr = $type . '(' . json_encode($options) . ',' . json_encode($data) . ');';
  413. break;
  414. case 'cmd':
  415. $this->queryStr = $data . '(' . json_encode($options) . ');';
  416. break;
  417. }
  418. $this->options = $options;
  419. }
  420. /**
  421. * 获取执行的指令
  422. * @access public
  423. * @return string
  424. */
  425. public function getQueryStr()
  426. {
  427. return $this->queryStr;
  428. }
  429. /**
  430. * 监听SQL执行
  431. * @access public
  432. * @param callable $callback 回调方法
  433. * @return void
  434. */
  435. public function listen($callback)
  436. {
  437. self::$event[] = $callback;
  438. }
  439. /**
  440. * 触发SQL事件
  441. * @access protected
  442. * @param string $sql 语句
  443. * @param float $runtime 运行时间
  444. * @param array $options 参数
  445. * @return bool
  446. */
  447. protected function trigger($sql, $runtime, $options = [])
  448. {
  449. if (!empty(self::$event)) {
  450. foreach (self::$event as $callback) {
  451. if (is_callable($callback)) {
  452. call_user_func_array($callback, [$sql, $runtime, $options]);
  453. }
  454. }
  455. } else {
  456. // 未注册监听则记录到日志中
  457. Log::record('[ Mongo ] ' . $sql . ' [ RunTime:' . $runtime . 's ]', 'sql');
  458. }
  459. }
  460. /**
  461. * 数据库调试 记录当前SQL及分析性能
  462. * @access protected
  463. * @param boolean $start 调试开始标记 true 开始 false 结束
  464. * @param string $sql 执行的SQL语句 留空自动获取
  465. * @return void
  466. */
  467. protected function debug($start, $sql = '')
  468. {
  469. if (!empty($this->config['debug'])) {
  470. // 开启数据库调试模式
  471. if ($start) {
  472. Debug::remark('queryStartTime', 'time');
  473. } else {
  474. // 记录操作结束时间
  475. Debug::remark('queryEndTime', 'time');
  476. $runtime = Debug::getRangeTime('queryStartTime', 'queryEndTime');
  477. $sql = $sql ?: $this->queryStr;
  478. // SQL监听
  479. $this->trigger($sql, $runtime, $this->options);
  480. }
  481. }
  482. }
  483. /**
  484. * 释放查询结果
  485. * @access public
  486. */
  487. public function free()
  488. {
  489. $this->cursor = null;
  490. }
  491. /**
  492. * 关闭数据库
  493. * @access public
  494. */
  495. public function close()
  496. {
  497. $this->mongo = null;
  498. $this->cursor = null;
  499. $this->linkRead = null;
  500. $this->linkWrite = null;
  501. $this->links = [];
  502. }
  503. /**
  504. * 初始化数据库连接
  505. * @access protected
  506. * @param boolean $master 是否主服务器
  507. * @return void
  508. */
  509. protected function initConnect($master = true)
  510. {
  511. if (!empty($this->config['deploy'])) {
  512. // 采用分布式数据库
  513. if ($master) {
  514. if (!$this->linkWrite) {
  515. $this->linkWrite = $this->multiConnect(true);
  516. }
  517. $this->mongo = $this->linkWrite;
  518. } else {
  519. if (!$this->linkRead) {
  520. $this->linkRead = $this->multiConnect(false);
  521. }
  522. $this->mongo = $this->linkRead;
  523. }
  524. } elseif (!$this->mongo) {
  525. // 默认单数据库
  526. $this->mongo = $this->connect();
  527. }
  528. }
  529. /**
  530. * 连接分布式服务器
  531. * @access protected
  532. * @param boolean $master 主服务器
  533. * @return PDO
  534. */
  535. protected function multiConnect($master = false)
  536. {
  537. $_config = [];
  538. // 分布式数据库配置解析
  539. foreach (['username', 'password', 'hostname', 'hostport', 'database', 'dsn', 'charset'] as $name) {
  540. $_config[$name] = explode(',', $this->config[$name]);
  541. }
  542. // 主服务器序号
  543. $m = floor(mt_rand(0, $this->config['master_num'] - 1));
  544. if ($this->config['rw_separate']) {
  545. // 主从式采用读写分离
  546. if ($master) // 主服务器写入
  547. {
  548. if ($this->config['is_replica_set']) {
  549. return $this->replicaSetConnect();
  550. } else {
  551. $r = $m;
  552. }
  553. } elseif (is_numeric($this->config['slave_no'])) {
  554. // 指定服务器读
  555. $r = $this->config['slave_no'];
  556. } else {
  557. // 读操作连接从服务器 每次随机连接的数据库
  558. $r = floor(mt_rand($this->config['master_num'], count($_config['hostname']) - 1));
  559. }
  560. } else {
  561. // 读写操作不区分服务器 每次随机连接的数据库
  562. $r = floor(mt_rand(0, count($_config['hostname']) - 1));
  563. }
  564. $dbConfig = [];
  565. foreach (['username', 'password', 'hostname', 'hostport', 'database', 'dsn', 'charset'] as $name) {
  566. $dbConfig[$name] = isset($_config[$name][$r]) ? $_config[$name][$r] : $_config[$name][0];
  567. }
  568. return $this->connect($dbConfig, $r);
  569. }
  570. /**
  571. * 创建基于复制集的连接
  572. * @return Manager
  573. */
  574. public function replicaSetConnect()
  575. {
  576. $this->dbName = $this->config['database'];
  577. $this->typeMap = $this->config['type_map'];
  578. if ($this->config['debug']) {
  579. $startTime = microtime(true);
  580. }
  581. $this->config['params']['replicaSet'] = $this->config['database'];
  582. $manager = new Manager($this->buildUrl(), $this->config['params']);
  583. if ($this->config['debug']) {
  584. // 记录数据库连接信息
  585. Log::record('[ DB ] CONNECT:[ UseTime:' . number_format(microtime(true) - $startTime, 6) . 's ] ' . $this->config['dsn'], 'sql');
  586. }
  587. return $manager;
  588. }
  589. /**
  590. * 根据配置信息 生成适用于链接复制集的 URL
  591. * @return string
  592. */
  593. private function buildUrl()
  594. {
  595. $url = 'mongodb://' . ($this->config['username'] ? "{$this->config['username']}" : '') . ($this->config['password'] ? ":{$this->config['password']}@" : '');
  596. $hostList = explode(',', $this->config['hostname']);
  597. $portList = explode(',', $this->config['hostport']);
  598. for ($i = 0; $i < count($hostList); $i++) {
  599. $url = $url . $hostList[$i] . ':' . $portList[0] . ',';
  600. }
  601. return rtrim($url, ",") . '/';
  602. }
  603. /**
  604. * 析构方法
  605. * @access public
  606. */
  607. public function __destruct()
  608. {
  609. // 释放查询
  610. $this->free();
  611. // 关闭连接
  612. $this->close();
  613. }
  614. }