DbManager.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. Model::setDb($this);
  84. if (is_object($this->event)) {
  85. Model::setEvent($this->event);
  86. }
  87. Model::maker(function (Model $model) {
  88. $isAutoWriteTimestamp = $model->getAutoWriteTimestamp();
  89. if (is_null($isAutoWriteTimestamp)) {
  90. // 自动写入时间戳
  91. $model->isAutoWriteTimestamp($this->getConfig('auto_timestamp', true));
  92. }
  93. $dateFormat = $model->getDateFormat();
  94. if (is_null($dateFormat)) {
  95. // 设置时间戳格式
  96. $model->setDateFormat($this->getConfig('datetime_format', 'Y-m-d H:i:s'));
  97. }
  98. });
  99. }
  100. /**
  101. * 监听SQL
  102. * @access protected
  103. * @return void
  104. */
  105. public function triggerSql(): void
  106. {}
  107. /**
  108. * 初始化配置参数
  109. * @access public
  110. * @param array $config 连接配置
  111. * @return void
  112. */
  113. public function setConfig($config): void
  114. {
  115. $this->config = $config;
  116. }
  117. /**
  118. * 设置缓存对象
  119. * @access public
  120. * @param CacheInterface $cache 缓存对象
  121. * @return void
  122. */
  123. public function setCache(CacheInterface $cache): void
  124. {
  125. $this->cache = $cache;
  126. }
  127. /**
  128. * 设置日志对象
  129. * @access public
  130. * @param LoggerInterface $log 日志对象
  131. * @return void
  132. */
  133. public function setLog(LoggerInterface $log): void
  134. {
  135. $this->log = $log;
  136. }
  137. /**
  138. * 记录SQL日志
  139. * @access protected
  140. * @param string $log SQL日志信息
  141. * @param string $type 日志类型
  142. * @return void
  143. */
  144. public function log(string $log, string $type = 'sql')
  145. {
  146. if ($this->log) {
  147. $this->log->log($type, $log);
  148. } else {
  149. $this->dbLog[$type][] = $log;
  150. }
  151. }
  152. /**
  153. * 获得查询日志(没有设置日志对象使用)
  154. * @access public
  155. * @param bool $clear 是否清空
  156. * @return array
  157. */
  158. public function getDbLog(bool $clear = false): array
  159. {
  160. $logs = $this->dbLog;
  161. if ($clear) {
  162. $this->dbLog = [];
  163. }
  164. return $logs;
  165. }
  166. /**
  167. * 获取配置参数
  168. * @access public
  169. * @param string $name 配置参数
  170. * @param mixed $default 默认值
  171. * @return mixed
  172. */
  173. public function getConfig(string $name = '', $default = null)
  174. {
  175. if ('' === $name) {
  176. return $this->config;
  177. }
  178. return $this->config[$name] ?? $default;
  179. }
  180. /**
  181. * 创建/切换数据库连接查询
  182. * @access public
  183. * @param string|null $name 连接配置标识
  184. * @param bool $force 强制重新连接
  185. * @return ConnectionInterface
  186. */
  187. public function connect(string $name = null, bool $force = false)
  188. {
  189. return $this->instance($name, $force);
  190. }
  191. /**
  192. * 创建数据库连接实例
  193. * @access protected
  194. * @param string|null $name 连接标识
  195. * @param bool $force 强制重新连接
  196. * @return ConnectionInterface
  197. */
  198. protected function instance(string $name = null, bool $force = false): ConnectionInterface
  199. {
  200. if (empty($name)) {
  201. $name = $this->getConfig('default', 'mysql');
  202. }
  203. if ($force || !isset($this->instance[$name])) {
  204. $this->instance[$name] = $this->createConnection($name);
  205. }
  206. return $this->instance[$name];
  207. }
  208. /**
  209. * 获取连接配置
  210. * @param string $name
  211. * @return array
  212. */
  213. protected function getConnectionConfig(string $name): array
  214. {
  215. $connections = $this->getConfig('connections');
  216. if (!isset($connections[$name])) {
  217. throw new InvalidArgumentException('Undefined db config:' . $name);
  218. }
  219. return $connections[$name];
  220. }
  221. /**
  222. * 创建连接
  223. * @param $name
  224. * @return ConnectionInterface
  225. */
  226. protected function createConnection(string $name): ConnectionInterface
  227. {
  228. $config = $this->getConnectionConfig($name);
  229. $type = !empty($config['type']) ? $config['type'] : 'mysql';
  230. if (false !== strpos($type, '\\')) {
  231. $class = $type;
  232. } else {
  233. $class = '\\think\\db\\connector\\' . ucfirst($type);
  234. }
  235. /** @var ConnectionInterface $connection */
  236. $connection = new $class($config);
  237. $connection->setDb($this);
  238. if ($this->cache) {
  239. $connection->setCache($this->cache);
  240. }
  241. return $connection;
  242. }
  243. /**
  244. * 使用表达式设置数据
  245. * @access public
  246. * @param string $value 表达式
  247. * @return Raw
  248. */
  249. public function raw(string $value): Raw
  250. {
  251. return new Raw($value);
  252. }
  253. /**
  254. * 更新查询次数
  255. * @access public
  256. * @return void
  257. */
  258. public function updateQueryTimes(): void
  259. {
  260. $this->queryTimes++;
  261. }
  262. /**
  263. * 重置查询次数
  264. * @access public
  265. * @return void
  266. */
  267. public function clearQueryTimes(): void
  268. {
  269. $this->queryTimes = 0;
  270. }
  271. /**
  272. * 获得查询次数
  273. * @access public
  274. * @return integer
  275. */
  276. public function getQueryTimes(): int
  277. {
  278. return $this->queryTimes;
  279. }
  280. /**
  281. * 监听SQL执行
  282. * @access public
  283. * @param callable $callback 回调方法
  284. * @return void
  285. */
  286. public function listen(callable $callback): void
  287. {
  288. $this->listen[] = $callback;
  289. }
  290. /**
  291. * 获取监听SQL执行
  292. * @access public
  293. * @return array
  294. */
  295. public function getListen(): array
  296. {
  297. return $this->listen;
  298. }
  299. /**
  300. * 注册回调方法
  301. * @access public
  302. * @param string $event 事件名
  303. * @param callable $callback 回调方法
  304. * @return void
  305. */
  306. public function event(string $event, callable $callback): void
  307. {
  308. $this->event[$event][] = $callback;
  309. }
  310. /**
  311. * 触发事件
  312. * @access public
  313. * @param string $event 事件名
  314. * @param mixed $params 传入参数
  315. * @return mixed
  316. */
  317. public function trigger(string $event, $params = null)
  318. {
  319. if (isset($this->event[$event])) {
  320. foreach ($this->event[$event] as $callback) {
  321. call_user_func_array($callback, [$params]);
  322. }
  323. }
  324. }
  325. public function __call($method, $args)
  326. {
  327. return call_user_func_array([$this->connect(), $method], $args);
  328. }
  329. }