Connection.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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\db;
  13. use Psr\SimpleCache\CacheInterface;
  14. use think\DbManager;
  15. use think\db\CacheItem;
  16. /**
  17. * 数据库连接基础类
  18. */
  19. abstract class Connection
  20. {
  21. /**
  22. * 当前SQL指令
  23. * @var string
  24. */
  25. protected $queryStr = '';
  26. /**
  27. * 返回或者影响记录数
  28. * @var int
  29. */
  30. protected $numRows = 0;
  31. /**
  32. * 事务指令数
  33. * @var int
  34. */
  35. protected $transTimes = 0;
  36. /**
  37. * 错误信息
  38. * @var string
  39. */
  40. protected $error = '';
  41. /**
  42. * 数据库连接ID 支持多个连接
  43. * @var array
  44. */
  45. protected $links = [];
  46. /**
  47. * 当前连接ID
  48. * @var object
  49. */
  50. protected $linkID;
  51. /**
  52. * 当前读连接ID
  53. * @var object
  54. */
  55. protected $linkRead;
  56. /**
  57. * 当前写连接ID
  58. * @var object
  59. */
  60. protected $linkWrite;
  61. /**
  62. * 数据表信息
  63. * @var array
  64. */
  65. protected $info = [];
  66. /**
  67. * 查询开始时间
  68. * @var float
  69. */
  70. protected $queryStartTime;
  71. /**
  72. * Builder对象
  73. * @var Builder
  74. */
  75. protected $builder;
  76. /**
  77. * Db对象
  78. * @var Db
  79. */
  80. protected $db;
  81. /**
  82. * 是否读取主库
  83. * @var bool
  84. */
  85. protected $readMaster = false;
  86. /**
  87. * 数据库连接参数配置
  88. * @var array
  89. */
  90. protected $config = [];
  91. /**
  92. * 缓存对象
  93. * @var Cache
  94. */
  95. protected $cache;
  96. /**
  97. * 获取当前的builder实例对象
  98. * @access public
  99. * @return Builder
  100. */
  101. public function getBuilder()
  102. {
  103. return $this->builder;
  104. }
  105. /**
  106. * 设置当前的数据库Db对象
  107. * @access public
  108. * @param DbManager $db
  109. * @return void
  110. */
  111. public function setDb(DbManager $db)
  112. {
  113. $this->db = $db;
  114. }
  115. /**
  116. * 设置当前的缓存对象
  117. * @access public
  118. * @param CacheInterface $cache
  119. * @return void
  120. */
  121. public function setCache(CacheInterface $cache)
  122. {
  123. $this->cache = $cache;
  124. }
  125. /**
  126. * 获取当前的缓存对象
  127. * @access public
  128. * @return CacheInterface|null
  129. */
  130. public function getCache()
  131. {
  132. return $this->cache;
  133. }
  134. /**
  135. * 获取数据库的配置参数
  136. * @access public
  137. * @param string $config 配置名称
  138. * @return mixed
  139. */
  140. public function getConfig(string $config = '')
  141. {
  142. if ('' === $config) {
  143. return $this->config;
  144. }
  145. return $this->config[$config] ?? null;
  146. }
  147. /**
  148. * 数据库SQL监控
  149. * @access protected
  150. * @param string $sql 执行的SQL语句 留空自动获取
  151. * @param bool $master 主从标记
  152. * @return void
  153. */
  154. protected function trigger(string $sql = '', bool $master = false): void
  155. {
  156. $listen = $this->db->getListen();
  157. if (!empty($listen)) {
  158. $runtime = number_format((microtime(true) - $this->queryStartTime), 6);
  159. $sql = $sql ?: $this->getLastsql();
  160. if (empty($this->config['deploy'])) {
  161. $master = null;
  162. }
  163. foreach ($listen as $callback) {
  164. if (is_callable($callback)) {
  165. $callback($sql, $runtime, $master);
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. * 缓存数据
  172. * @access protected
  173. * @param CacheItem $cacheItem 缓存Item
  174. */
  175. protected function cacheData(CacheItem $cacheItem)
  176. {
  177. if ($cacheItem->getTag() && method_exists($this->cache, 'tag')) {
  178. $this->cache->tag($cacheItem->getTag())->set($cacheItem->getKey(), $cacheItem->get(), $cacheItem->getExpire());
  179. } else {
  180. $this->cache->set($cacheItem->getKey(), $cacheItem->get(), $cacheItem->getExpire());
  181. }
  182. }
  183. /**
  184. * 分析缓存Key
  185. * @access protected
  186. * @param BaseQuery $query 查询对象
  187. * @return string
  188. */
  189. protected function getCacheKey(BaseQuery $query): string
  190. {
  191. if (!empty($query->getOptions('key'))) {
  192. $key = 'think:' . $this->getConfig('database') . '.' . $query->getTable() . '|' . $query->getOptions('key');
  193. } else {
  194. $key = $query->getQueryGuid();
  195. }
  196. return $key;
  197. }
  198. /**
  199. * 分析缓存
  200. * @access protected
  201. * @param BaseQuery $query 查询对象
  202. * @param array $cache 缓存信息
  203. * @return CacheItem
  204. */
  205. protected function parseCache(BaseQuery $query, array $cache): CacheItem
  206. {
  207. list($key, $expire, $tag) = $cache;
  208. if ($key instanceof CacheItem) {
  209. $cacheItem = $key;
  210. } else {
  211. if (true === $key) {
  212. $key = $this->getCacheKey($query);
  213. }
  214. $cacheItem = new CacheItem($key);
  215. $cacheItem->expire($expire);
  216. $cacheItem->tag($tag);
  217. }
  218. return $cacheItem;
  219. }
  220. /**
  221. * 获取返回或者影响的记录数
  222. * @access public
  223. * @return integer
  224. */
  225. public function getNumRows(): int
  226. {
  227. return $this->numRows;
  228. }
  229. /**
  230. * 析构方法
  231. * @access public
  232. */
  233. public function __destruct()
  234. {
  235. // 关闭连接
  236. $this->close();
  237. }
  238. }