Sharding.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace Heanup\Frame\Package\Database;
  3. /**
  4. * 数据库操作类
  5. * 分表模式
  6. */
  7. class Sharding extends \Heanup\Frame\Package\Database
  8. {
  9. protected $configClass = 'MySQL\\Core';
  10. protected $configSection = null;
  11. protected $tableName = '';
  12. protected $fields = array();
  13. protected $primaryKey = 'id';
  14. /**
  15. * 获取计数
  16. * @param Array $whereParam
  17. * @param string $whereString
  18. * @return int
  19. */
  20. public function count($whereParam, $whereString = '')
  21. {
  22. $query = $this->getQuery()->setWhere($whereParam, $whereString)->count();
  23. $result = $this->getConnection()->read($query->getSQL(), $query->getData());
  24. if (!$result) {
  25. return 0;
  26. }
  27. $result = current($result);
  28. return intval($result['count']);
  29. }
  30. /**
  31. * 获取列表
  32. * @param array $fields
  33. * @param array $whereParam
  34. * @param string $whereString
  35. * @param int $limit
  36. * @param int $offset
  37. * @param array $orderBy
  38. * @return array
  39. */
  40. public function select(
  41. $fields = array(),
  42. $whereParam = array(),
  43. $whereString = '',
  44. $limit = 10,
  45. $offset = 0,
  46. $orderBy = array())
  47. {
  48. $query = $this->getQuery()
  49. ->setField($fields)
  50. ->setWhere($whereParam, $whereString)
  51. ->setLimit($limit, $offset)
  52. ->setOrderBy($orderBy)
  53. ->select();
  54. $result = $this->getConnection()->read($query->getSQL(), $query->getData());
  55. if (!$result) {
  56. return $result;
  57. }
  58. $data = array();
  59. foreach ($result as $row) {
  60. $data[$row[$this->primaryKey]] = $row;
  61. }
  62. unset($result);
  63. return $data;
  64. }
  65. /**
  66. * 获取列表数据,简单版
  67. * @param array $whereParam
  68. * @param int $limit
  69. * @return array
  70. */
  71. public function getList($whereParam = array(), $limit = 10)
  72. {
  73. return $this->select(array(), $whereParam, '', $limit, 0, array());
  74. }
  75. /**
  76. * 获取列表ID信息
  77. * @param string $field
  78. * @param array $whereParam
  79. * @param $orderBy
  80. * @param $limit
  81. * @return array
  82. */
  83. public function getListId($field = '', $whereParam = array(), $orderBy = array(), $limit = 2000)
  84. {
  85. $query = self::getQuery()
  86. ->setField($field)
  87. ->setWhere($whereParam)
  88. ->setLimit($limit)
  89. ->setOrderBy($orderBy)
  90. ->select();
  91. $result = $this->getConnection()->read($query->getSQL(), $query->getData());
  92. if (!$result) {
  93. return $result;
  94. }
  95. $data = array();
  96. foreach ($result as $row) {
  97. $data[] = $row[$field];
  98. }
  99. unset($result);
  100. return $data;
  101. }
  102. /**
  103. * 获取单条记录
  104. * @param array $whereParam
  105. * @param string $whereString
  106. * @param array $orderBy
  107. * @return array;
  108. */
  109. public function getOne($whereParam = array(), $whereString = '', $orderBy = array())
  110. {
  111. $query = $this->getQuery()
  112. ->setWhere($whereParam, $whereString)
  113. ->setOrderBy($orderBy)
  114. ->setLimit(1)
  115. ->select();
  116. $result = $this->getConnection()->read($query->getSQL(), $query->getData(), false, false);
  117. return $result;
  118. }
  119. /**
  120. * 根据主键获取单条记录
  121. * @param int $id
  122. * @return array
  123. */
  124. public function getLine($id)
  125. {
  126. $where = array($this->primaryKey => $id);
  127. $query = $this->getQuery()->setWhere($where)->setLimit(1)->select();
  128. $result = $this->getConnection()->read($query->getSQL(), $query->getData(), false, false);
  129. return $result;
  130. }
  131. /**
  132. * 批量根据主键查询
  133. * @param $idList
  134. * @return array|mixed
  135. */
  136. public function getBatch($idList)
  137. {
  138. $where = array($this->primaryKey => $idList);
  139. $query = $this->getQuery()->setWhere($where)->select();
  140. $result = $this->getConnection()->read($query->getSQL(), $query->getData());
  141. if (!$result) {
  142. return $result;
  143. }
  144. $data = array();
  145. foreach ($result as $row) {
  146. $data[$row[$this->primaryKey]] = $row;
  147. }
  148. unset($result);
  149. return $data;
  150. }
  151. /**
  152. * 创建记录
  153. * @param $data
  154. * @param bool 是否返回最后最后插入ID
  155. * @return bool|int
  156. */
  157. public function insert($data, $returnLastInsertId = false)
  158. {
  159. $query = $this->getQuery()->setData($data)->insert();
  160. $result = $this->getConnection()->write($query->getSQL(), $query->getData());
  161. // 返回最后插入的id
  162. if ($result && $returnLastInsertId) {
  163. $result = $this->getConnection()->getLastInsertId();
  164. }
  165. return $result;
  166. }
  167. /**
  168. * 批量插入
  169. * $data = array(
  170. * array(
  171. * 'id' => 10001,
  172. * 'target_id' => 100,
  173. * ),
  174. * array(
  175. * 'id' => 10002,
  176. * 'target_id' => 100,
  177. * ),
  178. * );
  179. * @param $data
  180. * @return bool|int
  181. */
  182. public function insertBatch($data)
  183. {
  184. $prefix = 'prepared_';
  185. // 确定字段名
  186. $fields = implode('`, `', array_keys(current($data)));
  187. // 拼SQL和数据
  188. $dataList = array();
  189. $values = array();
  190. $i = 0;
  191. foreach ($data as $k => $v) {
  192. foreach ($v as $x => $y) {
  193. $key = $prefix . $i;
  194. $dataList[$key] = $y;
  195. $v[$x] = ':' . $key;
  196. $i++;
  197. }
  198. $values[] = "(" . implode(", ", $v) . ")";
  199. }
  200. $values = implode(', ', $values);
  201. $sql = "INSERT INTO `" . $this->tableName . "` (`$fields`) VALUES $values";
  202. $result = $this->write($sql, $dataList);
  203. return $result;
  204. }
  205. /**
  206. * 更新记录
  207. * @param $data
  208. * @param $whereParam
  209. * @param $whereString
  210. * @return bool
  211. */
  212. public function update($data, $whereParam, $whereString = '')
  213. {
  214. $query = $this->getQuery()->setData($data)->setWhere($whereParam, $whereString)->update();
  215. $result = $this->getConnection()->write($query->getSQL(), $query->getData());
  216. return $result;
  217. }
  218. /**
  219. * 替换输入
  220. * @param $data
  221. * @param $whereParam
  222. * @param $whereString
  223. * @return bool
  224. */
  225. public function replace($data, $whereParam, $whereString = '')
  226. {
  227. $query = $this->getQuery()->setData($data)->setWhere($whereParam, $whereString)->replace();
  228. $result = $this->getConnection()->write($query->getSQL(), $query->getData());
  229. return $result;
  230. }
  231. /**
  232. * 删除逻辑
  233. * @param $whereParam
  234. * @param $whereString
  235. * @return bool
  236. */
  237. public function delete($whereParam, $whereString = '')
  238. {
  239. $query = $this->getQuery()->setWhere($whereParam, $whereString)->delete();
  240. $result = $this->getConnection()->write($query->getSQL(), $query->getData());
  241. return $result;
  242. }
  243. /**
  244. * 获取一个新的SQL生成器
  245. * @return \Heanup\Frame\Library\MySQL\Query
  246. */
  247. protected function getQuery()
  248. {
  249. return \Heanup\Frame\Library\MySQL\Query::newInstance()
  250. ->setTableName($this->tableName)
  251. ->setField($this->fields)
  252. ->setPrimaryKey($this->primaryKey);
  253. }
  254. /**
  255. * 读取
  256. * @param $sql
  257. * @param $data
  258. * @param bool $isMaster
  259. * @return mixed|Array
  260. */
  261. protected function read($sql, $data, $isMaster = false)
  262. {
  263. $result = $this->getConnection()->read($sql, $data, $isMaster);
  264. return $result;
  265. }
  266. /**
  267. * 写入
  268. * @param $sql
  269. * @param $data
  270. * @return bool|int
  271. */
  272. protected function write($sql, $data)
  273. {
  274. $result = $this->getConnection()->write($sql, $data);
  275. return $result;
  276. }
  277. /**
  278. * 获取连接池
  279. * @return \Heanup\Frame\Library\MySQL
  280. */
  281. protected function getConnection()
  282. {
  283. return new \Heanup\Frame\Library\MySQL($this->configClass, $this->configSection);
  284. }
  285. /**
  286. * 根据数值来确定分表-分库方法
  287. * 1.
  288. * 可以自定义分表-分库的模板前缀$tbl变量
  289. * 2. 可以自定义截取长度
  290. * 3. 一般可以根据用户UID来获取分表或者分库
  291. *
  292. * @param int $num
  293. * 数值
  294. * @param string $tbl
  295. * 模板前缀
  296. * @param int $default
  297. * 默认截取长度
  298. */
  299. protected function num_identify($num, $tbl, $default = 1)
  300. {
  301. $num = (string) $num;
  302. $len = strlen($num);
  303. if ($len >= $default)
  304. $str = substr($num, $len - $default, $default);
  305. else
  306. $str = str_pad($num, $default, '0', STR_PAD_LEFT);
  307. return $tbl . '_' . $str;
  308. }
  309. /**
  310. * 求余数的方式获取分表-分库方法
  311. * 1. 求余方式余数比较少,适合小型的分表法
  312. * 2. 可以自定义求余除数
  313. * Dao中使用方法:$this->dao->db->fmod_identify($num, $tbl, $default = 7)
  314. * @param int $num
  315. * @param string $tbl
  316. * @param int $default
  317. * @return
  318. */
  319. public function fmod_identify($num, $tbl, $default = 7) {
  320. return $tbl . '_' . fmod($num/$default);
  321. }
  322. }