ConnectionInterface.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /**
  16. * Connection interface
  17. */
  18. interface ConnectionInterface
  19. {
  20. /**
  21. * 获取当前连接器类对应的Query类
  22. * @access public
  23. * @return string
  24. */
  25. public function getQueryClass(): string;
  26. /**
  27. * 连接数据库方法
  28. * @access public
  29. * @param array $config 接参数
  30. * @param integer $linkNum 连接序号
  31. * @return mixed
  32. */
  33. public function connect(array $config = [], $linkNum = 0);
  34. /**
  35. * 设置当前的数据库Db对象
  36. * @access public
  37. * @param DbManager $db
  38. * @return void
  39. */
  40. public function setDb(DbManager $db);
  41. /**
  42. * 设置当前的缓存对象
  43. * @access public
  44. * @param CacheInterface $cache
  45. * @return void
  46. */
  47. public function setCache(CacheInterface $cache);
  48. /**
  49. * 获取数据库的配置参数
  50. * @access public
  51. * @param string $config 配置名称
  52. * @return mixed
  53. */
  54. public function getConfig(string $config = '');
  55. /**
  56. * 关闭数据库(或者重新连接)
  57. * @access public
  58. * @return $this
  59. */
  60. public function close();
  61. /**
  62. * 查找单条记录
  63. * @access public
  64. * @param BaseQuery $query 查询对象
  65. * @return array
  66. * @throws DbException
  67. * @throws ModelNotFoundException
  68. * @throws DataNotFoundException
  69. */
  70. public function find(BaseQuery $query): array;
  71. /**
  72. * 查找记录
  73. * @access public
  74. * @param BaseQuery $query 查询对象
  75. * @return array
  76. * @throws DbException
  77. * @throws ModelNotFoundException
  78. * @throws DataNotFoundException
  79. */
  80. public function select(BaseQuery $query): array;
  81. /**
  82. * 插入记录
  83. * @access public
  84. * @param BaseQuery $query 查询对象
  85. * @param boolean $getLastInsID 返回自增主键
  86. * @return mixed
  87. */
  88. public function insert(BaseQuery $query, bool $getLastInsID = false);
  89. /**
  90. * 批量插入记录
  91. * @access public
  92. * @param BaseQuery $query 查询对象
  93. * @param mixed $dataSet 数据集
  94. * @return integer
  95. * @throws \Exception
  96. * @throws \Throwable
  97. */
  98. public function insertAll(BaseQuery $query, array $dataSet = []): int;
  99. /**
  100. * 更新记录
  101. * @access public
  102. * @param BaseQuery $query 查询对象
  103. * @return integer
  104. * @throws Exception
  105. * @throws PDOException
  106. */
  107. public function update(BaseQuery $query): int;
  108. /**
  109. * 删除记录
  110. * @access public
  111. * @param BaseQuery $query 查询对象
  112. * @return int
  113. * @throws Exception
  114. * @throws PDOException
  115. */
  116. public function delete(BaseQuery $query): int;
  117. /**
  118. * 得到某个字段的值
  119. * @access public
  120. * @param BaseQuery $query 查询对象
  121. * @param string $field 字段名
  122. * @param mixed $default 默认值
  123. * @param bool $one 返回一个值
  124. * @return mixed
  125. */
  126. public function value(BaseQuery $query, string $field, $default = null);
  127. /**
  128. * 得到某个列的数组
  129. * @access public
  130. * @param BaseQuery $query 查询对象
  131. * @param string $column 字段名 多个字段用逗号分隔
  132. * @param string $key 索引
  133. * @return array
  134. */
  135. public function column(BaseQuery $query, string $column, string $key = ''): array;
  136. /**
  137. * 执行数据库事务
  138. * @access public
  139. * @param callable $callback 数据操作方法回调
  140. * @return mixed
  141. * @throws PDOException
  142. * @throws \Exception
  143. * @throws \Throwable
  144. */
  145. public function transaction(callable $callback);
  146. /**
  147. * 启动事务
  148. * @access public
  149. * @return void
  150. * @throws \PDOException
  151. * @throws \Exception
  152. */
  153. public function startTrans();
  154. /**
  155. * 用于非自动提交状态下面的查询提交
  156. * @access public
  157. * @return void
  158. * @throws PDOException
  159. */
  160. public function commit();
  161. /**
  162. * 事务回滚
  163. * @access public
  164. * @return void
  165. * @throws PDOException
  166. */
  167. public function rollback();
  168. /**
  169. * 获取最近一次查询的sql语句
  170. * @access public
  171. * @return string
  172. */
  173. public function getLastSql(): string;
  174. }