DbManager.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 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. declare (strict_types = 1);
  12. namespace think;
  13. use InvalidArgumentException;
  14. use Psr\Log\LoggerInterface;
  15. use Psr\SimpleCache\CacheInterface;
  16. use think\db\BaseQuery;
  17. use think\db\ConnectionInterface;
  18. use think\db\Query;
  19. use think\db\Raw;
  20. /**
  21. * Class DbManager
  22. * @package think
  23. * @mixin BaseQuery
  24. * @mixin Query
  25. */
  26. class DbManager
  27. {
  28. /**
  29. * 数据库连接实例
  30. * @var array
  31. */
  32. protected $instance = [];
  33. /**
  34. * 数据库配置
  35. * @var array
  36. */
  37. protected $config = [];
  38. /**
  39. * Event对象或者数组
  40. * @var array|object
  41. */
  42. protected $event;
  43. /**
  44. * SQL监听
  45. * @var array
  46. */
  47. protected $listen = [];
  48. /**
  49. * SQL日志
  50. * @var array
  51. */
  52. protected $dbLog = [];
  53. /**
  54. * 查询次数
  55. * @var int
  56. */
  57. protected $queryTimes = 0;
  58. /**
  59. * 查询缓存对象
  60. * @var CacheInterface
  61. */
  62. protected $cache;
  63. /**
  64. * 查询日志对象
  65. * @var LoggerInterface
  66. */
  67. protected $log;
  68. /**
  69. * 架构函数
  70. * @access public
  71. */
  72. public function __construct()
  73. {
  74. $this->modelMaker();
  75. }
  76. /**
  77. * 注入模型对象
  78. * @access public
  79. * @return void
  80. */
  81. protected function modelMaker()
  82. {
  83. $this->triggerSql();
  84. Model::setDb($this);
  85. if (is_object($this->event)) {
  86. Model::setEvent($this->event);
  87. }
  88. Model::maker(function (Model $model) {
  89. $isAutoWriteTimestamp = $model->getAutoWriteTimestamp();
  90. if (is_null($isAutoWriteTimestamp)) {
  91. // 自动写入时间戳
  92. $model->isAutoWriteTimestamp($this->getConfig('auto_timestamp', true));
  93. }
  94. $dateFormat = $model->getDateFormat();
  95. if (is_null($dateFormat)) {
  96. // 设置时间戳格式
  97. $model->setDateFormat($this->getConfig('datetime_format', 'Y-m-d H:i:s'));
  98. }
  99. });
  100. }
  101. /**
  102. * 监听SQL
  103. * @access protected
  104. * @return void
  105. */
  106. protected function triggerSql(): void
  107. {
  108. // 监听SQL
  109. $this->listen(function ($sql, $time, $master) {
  110. if (0 === strpos($sql, 'CONNECT:')) {
  111. $this->log($sql);
  112. return;
  113. }
  114. // 记录SQL
  115. if (is_bool($master)) {
  116. // 分布式记录当前操作的主从
  117. $master = $master ? 'master|' : 'slave|';
  118. } else {
  119. $master = '';
  120. }
  121. $this->log($sql . ' [ ' . $master . 'RunTime:' . $time . 's ]');
  122. });
  123. }
  124. /**
  125. * 初始化配置参数
  126. * @access public
  127. * @param array $config 连接配置
  128. * @return void
  129. */
  130. public function setConfig($config): void
  131. {
  132. $this->config = $config;
  133. }
  134. /**
  135. * 设置缓存对象
  136. * @access public
  137. * @param CacheInterface $cache 缓存对象
  138. * @return void
  139. */
  140. public function setCache(CacheInterface $cache): void
  141. {
  142. $this->cache = $cache;
  143. }
  144. /**
  145. * 设置日志对象
  146. * @access public
  147. * @param LoggerInterface $log 日志对象
  148. * @return void
  149. */
  150. public function setLog(LoggerInterface $log): void
  151. {
  152. $this->log = $log;
  153. }
  154. /**
  155. * 记录SQL日志
  156. * @access protected
  157. * @param string $log SQL日志信息
  158. * @param string $type 日志类型
  159. * @return void
  160. */
  161. public function log(string $log, string $type = 'sql')
  162. {
  163. if ($this->log) {
  164. $this->log->log($type, $log);
  165. } else {
  166. $this->dbLog[$type][] = $log;
  167. }
  168. }
  169. /**
  170. * 获得查询日志(没有设置日志对象使用)
  171. * @access public
  172. * @param bool $clear 是否清空
  173. * @return array
  174. */
  175. public function getDbLog(bool $clear = false): array
  176. {
  177. $logs = $this->dbLog;
  178. if ($clear) {
  179. $this->dbLog = [];
  180. }
  181. return $logs;
  182. }
  183. /**
  184. * 获取配置参数
  185. * @access public
  186. * @param string $name 配置参数
  187. * @param mixed $default 默认值
  188. * @return mixed
  189. */
  190. public function getConfig(string $name = '', $default = null)
  191. {
  192. if ('' === $name) {
  193. return $this->config;
  194. }
  195. return $this->config[$name] ?? $default;
  196. }
  197. /**
  198. * 创建/切换数据库连接查询
  199. * @access public
  200. * @param string|null $name 连接配置标识
  201. * @param bool $force 强制重新连接
  202. * @return ConnectionInterface
  203. */
  204. public function connect(string $name = null, bool $force = false)
  205. {
  206. return $this->instance($name, $force);
  207. }
  208. /**
  209. * 创建数据库连接实例
  210. * @access protected
  211. * @param string|null $name 连接标识
  212. * @param bool $force 强制重新连接
  213. * @return ConnectionInterface
  214. */
  215. protected function instance(string $name = null, bool $force = false): ConnectionInterface
  216. {
  217. if (empty($name)) {
  218. $name = $this->getConfig('default', 'mysql');
  219. }
  220. if ($force || !isset($this->instance[$name])) {
  221. $this->instance[$name] = $this->createConnection($name);
  222. }
  223. return $this->instance[$name];
  224. }
  225. /**
  226. * 获取连接配置
  227. * @param string $name
  228. * @return array
  229. */
  230. protected function getConnectionConfig(string $name): array
  231. {
  232. $connections = $this->getConfig('connections');
  233. if (!isset($connections[$name])) {
  234. throw new InvalidArgumentException('Undefined db config:' . $name);
  235. }
  236. return $connections[$name];
  237. }
  238. /**
  239. * 创建连接
  240. * @param $name
  241. * @return ConnectionInterface
  242. */
  243. protected function createConnection(string $name): ConnectionInterface
  244. {
  245. $config = $this->getConnectionConfig($name);
  246. $type = !empty($config['type']) ? $config['type'] : 'mysql';
  247. if (false !== strpos($type, '\\')) {
  248. $class = $type;
  249. } else {
  250. $class = '\\think\\db\\connector\\' . ucfirst($type);
  251. }
  252. /** @var ConnectionInterface $connection */
  253. $connection = new $class($config);
  254. $connection->setDb($this);
  255. if ($this->cache) {
  256. $connection->setCache($this->cache);
  257. }
  258. return $connection;
  259. }
  260. /**
  261. * 使用表达式设置数据
  262. * @access public
  263. * @param string $value 表达式
  264. * @return Raw
  265. */
  266. public function raw(string $value): Raw
  267. {
  268. return new Raw($value);
  269. }
  270. /**
  271. * 更新查询次数
  272. * @access public
  273. * @return void
  274. */
  275. public function updateQueryTimes(): void
  276. {
  277. $this->queryTimes++;
  278. }
  279. /**
  280. * 重置查询次数
  281. * @access public
  282. * @return void
  283. */
  284. public function clearQueryTimes(): void
  285. {
  286. $this->queryTimes = 0;
  287. }
  288. /**
  289. * 获得查询次数
  290. * @access public
  291. * @return integer
  292. */
  293. public function getQueryTimes(): int
  294. {
  295. return $this->queryTimes;
  296. }
  297. /**
  298. * 监听SQL执行
  299. * @access public
  300. * @param callable $callback 回调方法
  301. * @return void
  302. */
  303. public function listen(callable $callback): void
  304. {
  305. $this->listen[] = $callback;
  306. }
  307. /**
  308. * 获取监听SQL执行
  309. * @access public
  310. * @return array
  311. */
  312. public function getListen(): array
  313. {
  314. return $this->listen;
  315. }
  316. /**
  317. * 注册回调方法
  318. * @access public
  319. * @param string $event 事件名
  320. * @param callable $callback 回调方法
  321. * @return void
  322. */
  323. public function event(string $event, callable $callback): void
  324. {
  325. $this->event[$event][] = $callback;
  326. }
  327. /**
  328. * 触发事件
  329. * @access public
  330. * @param string $event 事件名
  331. * @param mixed $params 传入参数
  332. * @return mixed
  333. */
  334. public function trigger(string $event, $params = null)
  335. {
  336. if (isset($this->event[$event])) {
  337. foreach ($this->event[$event] as $callback) {
  338. call_user_func_array($callback, [$this]);
  339. }
  340. }
  341. }
  342. public function __call($method, $args)
  343. {
  344. return call_user_func_array([$this->connect(), $method], $args);
  345. }
  346. }