Single.php 12 KB

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