Single.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace Heanup\Frame\Package\Database;
  3. use Exception;
  4. use Heanup\Frame\Filter;
  5. use Heanup\Frame\Library\MySQL;
  6. use Heanup\Frame\Library\MySQL\Query;
  7. use Heanup\Frame\Logger;
  8. use Heanup\Frame\Package\Database;
  9. /**
  10. * 数据库操作类
  11. * 单表模式
  12. */
  13. class Single extends Database
  14. {
  15. const CONFIG_CLASS = 'MySQL\\Core';
  16. protected static $prefix = 'ph_'; // 表前缀
  17. protected static $tableName = '';
  18. protected static $fields = array();
  19. protected static $primaryKey = 'id';
  20. /**
  21. * 获取计数
  22. * @param array $whereParam
  23. * @param string $whereString
  24. * @return int
  25. * @throws Exception
  26. */
  27. public static function count($whereParam, $whereString = '')
  28. {
  29. $query = self::getQuery()->setWhere($whereParam, $whereString)->count();
  30. $result = self::getConnection()->read($query->getSQL(), $query->getData());
  31. if (!$result) {
  32. return 0;
  33. }
  34. $result = current($result);
  35. return intval($result['count']);
  36. }
  37. /**
  38. * 获取列表,limit 不传取全部
  39. * @param array $fields
  40. * @param array $whereParam
  41. * @param string $whereString
  42. * @param int $limit 不传取全部
  43. * @param int $offset
  44. * @param array $orderBy
  45. * @return array
  46. */
  47. public static function select(
  48. $fields = array(),
  49. $whereParam = array(),
  50. $whereString = '',
  51. $limit = null,
  52. $offset = 0,
  53. $orderBy = array())
  54. {
  55. $query = self::getQuery()
  56. ->setField($fields)
  57. ->setWhere($whereParam, $whereString)
  58. ->setLimit($limit, $offset)
  59. ->setOrderBy($orderBy)
  60. ->select();
  61. try {
  62. $result = self::getConnection()->read($query->getSQL(), $query->getData());
  63. } catch (Exception $e) {
  64. Logger::setLog(null,$e);
  65. }
  66. if (!$result) {
  67. return $result;
  68. }
  69. // $data = array();
  70. // $class = get_called_class();
  71. // foreach ($result as $row) {
  72. // $data[$row[$class::$primaryKey]] = $row;
  73. // }
  74. // unset($data);
  75. return $result;
  76. }
  77. /**
  78. * 获取列表数据,简单版
  79. * @param array $whereParam
  80. * @param int $limit
  81. * @param array $orderBy
  82. * @return array
  83. * @throws Exception
  84. */
  85. public static function getList($whereParam = array(), $limit = 10, $orderBy = array())
  86. {
  87. return self::select(array(), $whereParam, '', $limit, 0, $orderBy);
  88. }
  89. /**
  90. * 获取列表ID信息
  91. * @param string $field
  92. * @param array $whereParam
  93. * @param array $orderBy
  94. * @param int $limit
  95. * @return array
  96. * @throws Exception
  97. */
  98. public static function getListId($field = '', $whereParam = array(), $orderBy = array(), $limit = 2000)
  99. {
  100. $query = self::getQuery()
  101. ->setField($field)
  102. ->setWhere($whereParam)
  103. ->setLimit($limit)
  104. ->setOrderBy($orderBy)
  105. ->select();
  106. $result = self::getConnection()->read($query->getSQL(), $query->getData());
  107. if (!$result) {
  108. return $result;
  109. }
  110. $data = array();
  111. foreach ($result as $row) {
  112. $data[] = $row[$field];
  113. }
  114. unset($result);
  115. return $data;
  116. }
  117. /**
  118. * 分页获取数据
  119. * @param array $whereParam
  120. * @param string $whereString
  121. * @param int $page
  122. * @param int $pageSize
  123. * @param array $orderBy
  124. * @param array $fields
  125. * @return array
  126. * @throws Exception
  127. */
  128. public static function getPageList($whereParam = array(), $whereString='', $page = 1, $pageSize = 20, $orderBy = array(), $fields = array()){
  129. $page = $page ? $page : 1;
  130. $offset = ($page - 1) * $pageSize;
  131. //获取总条数
  132. $total_count = self::count($whereParam,$whereString);
  133. $pager = array(
  134. 'total_count' => $total_count,
  135. 'total_page' => 0,
  136. 'current_page' => $page,
  137. 'page_size' => $pageSize,
  138. 'data' => array()
  139. );
  140. if($total_count > 0){
  141. list($url,) = explode('?',$_SERVER['REQUEST_URI']);
  142. unset($_GET['page']);
  143. //链接
  144. $pager['url'] = $url;
  145. //请求参数
  146. $pager['filter'] = $_GET ? http_build_query($_GET) : '';
  147. //总页数
  148. $pager['total_page'] = ceil($total_count / $pageSize);
  149. //内容
  150. $pager['data'] = self::select($fields,$whereParam,$whereString,$pageSize,$offset,$orderBy);
  151. }
  152. return $pager;
  153. }
  154. /**
  155. * 获取单条记录
  156. * @param array $whereParam
  157. * @param string $whereString
  158. * @param array $orderBy
  159. * @return array;
  160. * @throws Exception
  161. */
  162. public static function getOne($whereParam = array(), $whereString = '', $orderBy = array())
  163. {
  164. $query = self::getQuery()
  165. ->setWhere($whereParam, $whereString)
  166. ->setOrderBy($orderBy)
  167. ->setLimit(1)
  168. ->select();
  169. $result = self::getConnection()->read($query->getSQL(), $query->getData(), false, false);
  170. return $result;
  171. }
  172. /**
  173. * 根据主键获取单条记录
  174. * @param int $id
  175. * @return array
  176. */
  177. public static function getLine($id)
  178. {
  179. if (!$id) {
  180. return array();
  181. }
  182. $class = get_called_class();
  183. $where = array($class::$primaryKey => $id);
  184. $query = self::getQuery()->setWhere($where)->setLimit(1)->select();
  185. $result = self::getConnection()->read($query->getSQL(), $query->getData(), false, false);
  186. return $result;
  187. }
  188. /**
  189. * 批量根据主键查询, 按照传入的ID进行排序
  190. * @param $idList
  191. * @return array|mixed
  192. */
  193. public static function getBatch($idList)
  194. {
  195. if (!$idList) {
  196. return array();
  197. }
  198. $class = get_called_class();
  199. $where = array($class::$primaryKey => $idList);
  200. $query = self::getQuery()->setWhere($where)->select();
  201. $result = self::getConnection()->read($query->getSQL(), $query->getData());
  202. if (!$result) {
  203. return $result;
  204. }
  205. $data = array();
  206. foreach ($result as $row) {
  207. $data[$row[$class::$primaryKey]] = $row;
  208. }
  209. // 按照传入的id顺序进行重新排序,如果id对应的value不存在则忽略
  210. $result = array();
  211. foreach ($idList as $id) {
  212. if (isset($data[$id])) {
  213. $result[$id] = $data[$id];
  214. }
  215. }
  216. unset($data);
  217. return $result;
  218. }
  219. /**
  220. * 聚合获取列表数据
  221. * @param $field
  222. * @param $whereParam
  223. * @param $whereString
  224. * @param $groupBy
  225. * @param $limit
  226. * @return array|mixed
  227. */
  228. public static function getListByGroup($field,$whereParam,$whereString,$groupBy,$limit=null){
  229. $query = self::getQuery()
  230. ->setField($field)
  231. ->setWhere($whereParam,$whereString)
  232. ->setGroupBy($groupBy)
  233. ->setLimit($limit)
  234. ->select();
  235. return self::getConnection()->read($query->getSQL(), $query->getData());
  236. }
  237. /**
  238. * 联合获取列表数据
  239. * @param $whereString
  240. * @param $join
  241. * @param $sort
  242. * @param string $field
  243. * @return array|mixed
  244. */
  245. public static function getListByJoin($whereString,$join,$sort='',$field=''){
  246. $class = get_called_class();
  247. $tableName = $class::$tableName;
  248. $field = $field ? : $tableName.'.*';
  249. $sql = 'select '.$field.' from '.$tableName.' left join '.$join[0].' on '.$tableName.'.'.$join[1].'='.$join[0].'.'.$join[2];
  250. $whereString ? $sql .= ' where '.$whereString : null;
  251. $sort ? $sql .= ' order by '.$sort : null;
  252. $sql = str_replace('SELF',$tableName,$sql);//SELF代表主表
  253. $sql = str_replace('JOIN',$join[0],$sql);//JOIN代表连接表
  254. $result = self::getConnection()->read($sql, array());
  255. $data = array();
  256. $class = get_called_class();
  257. foreach ($result as $row) {
  258. $data[$row[$class::$primaryKey]] = $row;
  259. }
  260. return $data;
  261. }
  262. /**
  263. * 创建记录
  264. * @param $data
  265. * @param bool 是否返回最后最后插入ID
  266. * @return bool|int
  267. */
  268. public static function insert($data, $returnLastInsertId = true)
  269. {
  270. $query = self::getQuery()->setData($data)->insert();
  271. $result = self::getConnection()->write($query->getSQL(), $query->getData());
  272. // 返回最后插入的id
  273. if ($result && $returnLastInsertId) {
  274. $result = self::getConnection()->getLastInsertId();
  275. }
  276. return $result;
  277. }
  278. /**
  279. * 批量插入
  280. * $data = array(
  281. * array(
  282. * 'id' => 10001,
  283. * 'target_id' => 100,
  284. * ),
  285. * array(
  286. * 'id' => 10002,
  287. * 'target_id' => 100,
  288. * ),
  289. * );
  290. * @param $data
  291. * @return bool|int
  292. */
  293. public static function insertBatch($data)
  294. {
  295. $prefix = 'prepared_';
  296. // 确定字段名
  297. $fields = implode('`, `', array_keys(current($data)));
  298. //print_r($data);
  299. // 拼SQL和数据
  300. $dataList = array();
  301. $values = array();
  302. $i = 0;
  303. foreach ($data as $k => $v) {
  304. foreach ($v as $x => $y) {
  305. $key = $prefix . $i;
  306. $dataList[$key] = $y;
  307. $v[$x] = ':' . $key;
  308. $i++;
  309. }
  310. $values[] = "(" . implode(", ", $v) . ")";
  311. }
  312. $values = implode(', ', $values);
  313. $class = get_called_class();
  314. $tableName = $class::$tableName;
  315. //print_r($values);die();
  316. $sql = "INSERT INTO `" . $tableName . "` (`$fields`) VALUES $values";
  317. //print_r($sql);
  318. $result = self::write($sql, $dataList);
  319. return $result;
  320. }
  321. /**
  322. * 更新记录
  323. * @param $data
  324. * @param $whereParam
  325. * @param $whereString
  326. * @return bool
  327. */
  328. public static function update($data, $whereParam, $whereString = '')
  329. {
  330. $query = self::getQuery()->setData($data)->setWhere($whereParam, $whereString)->update();
  331. $result = self::getConnection()->write($query->getSQL(), $query->getData());
  332. return $result;
  333. }
  334. /**
  335. * 替换输入
  336. * @param $data
  337. * @param $whereParam
  338. * @param $whereString
  339. * @return bool
  340. */
  341. public static function replace($data, $whereParam, $whereString = '')
  342. {
  343. $query = self::getQuery()->setData($data)->setWhere($whereParam, $whereString)->replace();
  344. $result = self::getConnection()->write($query->getSQL(), $query->getData());
  345. return $result;
  346. }
  347. /**
  348. * 删除逻辑
  349. * @param $whereParam
  350. * @param $whereString
  351. * @return bool
  352. */
  353. public static function delete($whereParam, $whereString = '')
  354. {
  355. $query = self::getQuery()->setWhere($whereParam, $whereString)->delete();
  356. $result = self::getConnection()->write($query->getSQL(), $query->getData());
  357. return $result;
  358. }
  359. /**
  360. * 获取一个新的SQL生成器
  361. * @return Query
  362. */
  363. protected static function getQuery()
  364. {
  365. $class = get_called_class();
  366. return Query::newInstance()
  367. ->setTableName($class::$tableName)
  368. ->setField($class::$fields)
  369. ->setPrimaryKey($class::$primaryKey);
  370. }
  371. /**
  372. * 从数据库读取数据,可以强制从主库读取
  373. * @param string $sql
  374. * @param array $data
  375. * @param bool $isMaster
  376. * @return mixed|Array
  377. */
  378. protected static function read($sql, $data, $isMaster = false)
  379. {
  380. $result = self::getConnection()->read($sql, $data, $isMaster);
  381. return $result;
  382. }
  383. /**
  384. * 写入
  385. * @param $sql
  386. * @param $data
  387. * @return bool|int
  388. */
  389. protected static function write($sql, $data)
  390. {
  391. $result = self::getConnection()->write($sql, $data);
  392. return $result;
  393. }
  394. /**
  395. * 获取连接池
  396. * @return MySQL
  397. */
  398. static function getConnection()
  399. {
  400. $class = get_called_class();
  401. $configClass = $class::CONFIG_CLASS;
  402. return new MySQL($configClass);
  403. }
  404. /**
  405. * 获取表前缀
  406. * @return string
  407. */
  408. public static function getPrefix(){
  409. return isset(self::$prefix) ? self::$prefix : '';
  410. }
  411. }