Connection.php 19 KB

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