commonDao.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <?php
  2. /**
  3. * 数据层的基类,$table_name和$fields需要被实例化
  4. * @author Tinson Ho <hts@meitu.com>
  5. */
  6. class commonDao extends Dao
  7. {
  8. protected $prefix = 'ph_'; // 表前缀
  9. protected $table_name; // 表名,待实例化
  10. protected $fields; // 字段名组合,用,分隔,待实例化
  11. protected $dbType; // 多数据库服务器,不实例化时使用默认数据库
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. if (!empty($this->dbType)) {
  16. $this->init_db($this->dbType);
  17. }
  18. }
  19. /**
  20. * 获取数据库连接(多数据库)
  21. * @return dbInit
  22. */
  23. protected function getDb()
  24. {
  25. return !empty($this->dbType) ? $this->init_db($this->dbType) : $this->init_db('default');
  26. }
  27. /**
  28. * 通过ID获取单条记录
  29. * @param int $id
  30. * @param string $table_name
  31. * @return array
  32. */
  33. public function getOne($id, $table_name = null)
  34. {
  35. !$table_name ? $table_name = $this->table_name : null;
  36. return $this->getDb()->get_one($id, $table_name);
  37. }
  38. /**
  39. * 分页获取
  40. * @param array $field
  41. * @param int $num
  42. * @param int $offset
  43. * @param string $id_key
  44. * @param string $sort
  45. * @param string $table_name
  46. * @return array
  47. */
  48. public function getList($field = array(), $num = 20, $offset = 0, $id_key = 'id', $sort = 'ASC', $table_name = null)
  49. {
  50. !$table_name ? $table_name = $this->table_name : null;
  51. $id_key = !empty($this->idKey) && $id_key == 'id' ? $this->idKey : $id_key;
  52. $result = $this->getDb()->get_all($table_name, $field, $num, $offset, $id_key, $sort);
  53. return $result [0];
  54. }
  55. /**
  56. * 获取全部
  57. * @param array $field
  58. * @param string $id_key
  59. * @param string $sort
  60. * @param string $table_name
  61. * @return array
  62. */
  63. public function getAll($field = array(), $id_key = 'id', $sort = 'ASC', $table_name = null)
  64. {
  65. !$table_name ? $table_name = $this->table_name : null;
  66. $id_key = !empty($this->idKey) && $id_key == 'id' ? $this->idKey : $id_key;
  67. return $this->getAllByMutiple($field, null, array($id_key => $sort), null, null, $table_name);
  68. }
  69. /**
  70. * 计数
  71. * @param array $field
  72. * @param string $table_name
  73. * @return int
  74. */
  75. public function count($field = array(), $table_name = null)
  76. {
  77. !$table_name ? $table_name = $this->table_name : null;
  78. return $this->getDb()->get_count($table_name, $field);
  79. }
  80. /**
  81. * 添加
  82. * @param array $data
  83. * @param bool $ignore
  84. * @param string $table_name
  85. * @return int
  86. */
  87. public function add($data, $ignore = false, $table_name = null)
  88. {
  89. if (!$table_name) {
  90. $table_name = $this->table_name;
  91. $data = $this->dao->db->build_key($data, $this->fields);
  92. }
  93. return $this->getDb()->insert($data, $table_name, $ignore);
  94. }
  95. /**
  96. * 多条记录插入
  97. * @param array $data
  98. * @param bool $ignore
  99. * @param string $table_name
  100. * @return int
  101. */
  102. public function addBatch($data, $ignore = false, $table_name = null)
  103. {
  104. $fields = array();
  105. foreach ($data as $one_data) {
  106. foreach ($one_data as $key => $val) {
  107. array_push($fields, $key);
  108. }
  109. break;
  110. }
  111. !$table_name ? $table_name = $this->table_name : null;
  112. return $this->getDb()->insert_more($fields, $data, $table_name, $ignore);
  113. }
  114. /**
  115. * 更新(基于ID)
  116. * @param int $id
  117. * @param array $data
  118. * @param string $table_name
  119. * @return int
  120. */
  121. public function update($id, $data, $table_name = null)
  122. {
  123. if (!$table_name) {
  124. $table_name = $this->table_name;
  125. $data = $this->dao->db->build_key($data, $this->fields);
  126. }
  127. return $this->getDb()->update($id, $data, $table_name);
  128. }
  129. /**
  130. * 更新(基于其他字段)
  131. * @param array $data
  132. * @param array $field
  133. * @param string $table_name
  134. * @return int
  135. */
  136. public function updateByField($data, $field, $table_name = null)
  137. {
  138. if (!is_array($field)) {
  139. return false;
  140. }
  141. if (!$table_name) {
  142. $table_name = $this->table_name;
  143. $data = $this->dao->db->build_key($data, $this->fields);
  144. }
  145. return $this->getDb()->update_by_field($data, $field, $table_name);
  146. }
  147. /**
  148. * 删除(可以是ID数组,也可以是单个ID)
  149. * @param array / int $ids
  150. * @param string $table_name
  151. * @return int
  152. */
  153. public function delete($ids, $table_name = null)
  154. {
  155. !$table_name ? $table_name = $this->table_name : null;
  156. return $this->getDb()->delete($ids, $table_name);
  157. }
  158. /**
  159. * 删除(通过字段条件删除)
  160. * @param array $field
  161. * @param string $table_name
  162. * @return int
  163. */
  164. public function deleteByField($field, $table_name = null)
  165. {
  166. !$table_name ? $table_name = $this->table_name : null;
  167. return $this->getDb()->delete_by_field($field, $table_name);
  168. }
  169. /**
  170. * 通过字段查询获取第一条记录
  171. * @param array $data
  172. * @param string $table_name
  173. * @return array
  174. */
  175. public function getOneByField($data = array(), $table_name = null)
  176. {
  177. !$table_name ? $table_name = $this->table_name : null;
  178. return $this->getDb()->get_one_by_field($data, $table_name);
  179. }
  180. /**
  181. * Sql语句查询,获取全部记录
  182. * @param string $sql
  183. * @return array / bool
  184. */
  185. public function queryAll($sql)
  186. {
  187. $result = $this->getDb()->query($sql, false);
  188. if (!$result) {
  189. return false;
  190. }
  191. $arr = array();
  192. while ($row = $this->getDb()->fetch_assoc($result)) {
  193. $arr [] = $row;
  194. }
  195. return $arr;
  196. }
  197. /**
  198. * 从Sql语句查询,获取第一条记录
  199. * @param string $sql
  200. * @return array / bool
  201. */
  202. public function queryRow($sql)
  203. {
  204. $result = $this->getDb()->query($sql, false);
  205. if (!$result) {
  206. return false;
  207. }
  208. $arr = $this->getDb()->fetch_assoc($result);
  209. return $arr;
  210. }
  211. /**
  212. * SQL语句查询,获取第一条记录的第一个字段
  213. * @param string $sql
  214. * @return string / bool
  215. */
  216. public function queryOne($sql)
  217. {
  218. $arr = $this->queryRow($sql);
  219. foreach ($arr as $val) {
  220. return $val;
  221. }
  222. return false;
  223. }
  224. /**
  225. * 执行SQL语句
  226. * @param string $sql
  227. * @return mix
  228. */
  229. public function query($sql)
  230. {
  231. return $this->getDb()->query($sql);
  232. }
  233. /**
  234. * 获取表前缀
  235. * @return string
  236. */
  237. public function getPrefix()
  238. {
  239. return isset($this->prefix) ? $this->prefix : '';
  240. }
  241. /**
  242. * 获取表名
  243. * @return string
  244. */
  245. public function getTableName()
  246. {
  247. return $this->table_name;
  248. }
  249. /**
  250. * 生成WHERE子句
  251. *
  252. * @param array $map
  253. * array('field1'=>'11','field2'=>array('value1','value3'),'field3'=>array('_logic'=>'>=','value'=>1) );
  254. * @param string $tag 数据库表标识名
  255. * @return string
  256. */
  257. public function buildWhere($map, $tag = '')
  258. {
  259. if (!is_array($map) || empty($map)) {
  260. return '';
  261. }
  262. $temp = array();
  263. foreach ($map as $k => $v) {
  264. if (is_array($v)) {
  265. if (!empty($v ['_link'])) {
  266. $link = $v ['_link'];
  267. unset($v ['_link']);
  268. $str = $this->buildWhereWithLink($v, $link, $tag);
  269. !empty($str) ? $temp [] = ' ( ' . $str . ' ) ' : null;
  270. } else {
  271. $temp [] = $this->buildWhereWithLink(array(
  272. $k => $v
  273. ), 'AND', $tag);
  274. }
  275. } else {
  276. $temp [] = $this->buildWhereWithLink(array(
  277. $k => $v
  278. ), 'AND', $tag);
  279. }
  280. }
  281. return ' WHERE ' . implode(' AND ', $temp);
  282. }
  283. public function buildWhereWithLink($map, $link = 'AND', $tag = '')
  284. {
  285. if (!is_array($map) || empty($map)) {
  286. return '';
  287. }
  288. $link = strtoupper($link);
  289. if (!in_array($link, array(
  290. 'AND',
  291. 'OR'
  292. ))
  293. ) {
  294. return '';
  295. }
  296. $temp = array();
  297. $tag = $tag ? $this->dao->db->build_escape($tag, 1) . '.' : '';
  298. $fhArr = array(
  299. '<',
  300. '<=',
  301. '>',
  302. '>=',
  303. '!=',
  304. '<>',
  305. 'LIKE',
  306. '='
  307. );
  308. foreach ($map as $k => $v) {
  309. if (is_array($v)) {
  310. if (!empty($v ['_logic'])) {
  311. $v ['_logic'] = strtoupper($v ['_logic']);
  312. if (in_array($v ['_logic'], $fhArr)) {
  313. $temp [] = $tag . $this->dao->db->build_escape($k, 1) . ' ' . $v ['_logic'] . ' ' . $this->dao->db->build_escape($v ['value']) . '';
  314. }
  315. } else {
  316. $mixArr = array();
  317. foreach ($v as $ke => $va) {
  318. if (is_array($va)) {
  319. if (!empty($va ['_logic'])) {
  320. $va ['_logic'] = strtoupper($va ['_logic']);
  321. if (in_array($va ['_logic'], $fhArr)) {
  322. $temp [] = $tag . $this->dao->db->build_escape($k, 1) . ' ' . $va ['_logic'] . ' ' . $this->dao->db->build_escape($va ['value']) . '';
  323. }
  324. }
  325. } else {
  326. $mixArr [] = $this->dao->db->build_escape($va);
  327. }
  328. }
  329. !empty($mixArr) ? $temp [] = $tag . $this->dao->db->build_escape($k, 1) . 'IN (' . implode(',', $mixArr) . ') ' : null;
  330. }
  331. } else {
  332. $temp [] = $tag . $this->dao->db->build_escape($k, 1) . '=' . $this->dao->db->build_escape($v);
  333. }
  334. }
  335. return implode(' ' . $link . ' ', $temp);
  336. }
  337. /**
  338. * 生成ORDER BY字句
  339. *
  340. * @param array $orderby array('field'=>'desc');
  341. * @param string $tag 数据库表标识名
  342. * @return string
  343. */
  344. public function buildOrderBy($orderby, $tag = '')
  345. {
  346. if (!is_array($orderby) || empty($orderby)) {
  347. return '';
  348. }
  349. $tag = $tag ? $this->dao->db->build_escape($tag, 1) . '.' : '';
  350. $temp = array();
  351. foreach ($orderby as $key => $val) {
  352. $temp [] = $tag . $this->dao->db->build_escape($key, 1) . ' ' . ($val == 'desc' ? 'desc' : 'asc');
  353. }
  354. return ' ORDER BY ' . implode(',', $temp);
  355. }
  356. /**
  357. * 生成LIMIT字句
  358. *
  359. * @param array / int $limit 限制数 eg. (1)array(0,10) (2)10
  360. * @return string
  361. */
  362. public function buildLimit($limit)
  363. {
  364. if (empty($limit)) {
  365. return '';
  366. }
  367. if (is_array($limit)) {
  368. return isset($limit [1]) ? ' LIMIT ' . intval($limit [0]) . ',' . intval($limit [1]) : ' LIMIT ' . intval($limit [0]);
  369. } else {
  370. return ' LIMIT ' . intval($limit);
  371. }
  372. }
  373. /**
  374. * 生成GROUP BY字句
  375. *
  376. * @param mixed(array,string) $groupby 字段
  377. * @param string $tag 数据库表标识名
  378. * @return string
  379. */
  380. public function buildGroupBy($groupby, $tag = '')
  381. {
  382. if (empty($groupby)) {
  383. return '';
  384. }
  385. $tag = $tag ? $this->dao->db->build_escape($tag, 1) . '.' : '';
  386. if (is_array($groupby)) {
  387. foreach ($groupby as $key => $val) {
  388. $groupby [$key] = $tag . $this->dao->db->build_escape($val, 1);
  389. }
  390. return ' GROUP BY ' . implode(',', $groupby);
  391. } else {
  392. return ' GROUP BY ' . $this->dao->db->build_escape($tag . $groupby, 1);
  393. }
  394. }
  395. /**
  396. * 生成字段字句
  397. *
  398. * @param mixed(array,string) $fields 字段
  399. * @param string $tag 数据库表标识名
  400. * @return string
  401. */
  402. public function buildFields($fields, $tag = '')
  403. {
  404. $tag = $tag ? $this->dao->db->build_escape($tag, 1) . '.' : '';
  405. if (empty($fields)) {
  406. return ' ' . $tag . '* ';
  407. } else if (is_array($fields)) {
  408. $temp = array();
  409. foreach ($fields as $k => $v) {
  410. $temp [] = $tag . $this->dao->db->build_escape($v, 1);
  411. }
  412. return implode(',', $temp);
  413. } else {
  414. return $fields;
  415. }
  416. }
  417. /**
  418. * 复杂的单表查询接口
  419. *
  420. * @param array $map
  421. * 查询条件 eg. array('field1'=>'11','field2'=>array('value1','value3'),'field3'=>array('_logic'=>'>=','value'=>1) );
  422. * @param array / string $fields
  423. * 查询字段 eg. (1)array('字段1','字段2') (2) '字段1,字段2'
  424. * @param array $orderby
  425. * 排序条件 eg. array('id'=>'asc','appid'=>'desc')
  426. * @param array / int $limit
  427. * 限制查询数量 eg. (1)array(0,10) (2)10
  428. * @param array / string $groupby
  429. * Group条件 eg (1)array('sid','pid') (2)pid
  430. * @param string $table_name
  431. * @return array
  432. */
  433. public function getAllByMutiple($map = array(), $fields = array(), $orderby = array(), $limit = array(), $groupby = array(), $table_name = null)
  434. {
  435. !$table_name ? $table_name = $this->table_name : null;
  436. $fields = $this->buildFields($fields);
  437. $sql = 'SELECT ' . $fields . ' FROM `' . $table_name . '` ';
  438. $sql .= $this->buildWhere($map);
  439. $sql .= $this->buildGroupBy($groupby);
  440. $sql .= $this->buildOrderBy($orderby);
  441. $sql .= $this->buildLimit($limit);
  442. return $this->queryAll($sql);
  443. }
  444. /**
  445. * 生成update字句
  446. *
  447. * @param array $set
  448. * 更新项 array('field1'=>'value1','field2'=>array('_logic'=>'+','value'=>'value2'))
  449. * @param string $tag
  450. * @return string
  451. */
  452. public function buildSet($set, $tag = '')
  453. {
  454. if (!is_array($set) || empty($set)) {
  455. return '';
  456. }
  457. $tag = $tag ? $this->dao->db->build_escape($tag, 1) . '.' : '';
  458. $temp = array();
  459. foreach ($set as $k => $v) {
  460. if (is_array($v)) {
  461. if (!empty($v ['_logic'])) {
  462. if (in_array($v ['_logic'], array(
  463. '+',
  464. '-',
  465. '*',
  466. '/',
  467. '%'
  468. ))) {
  469. $temp [] = $tag . $this->dao->db->build_escape($k, 1) . '=' . $tag . $this->dao->db->build_escape($k, 1) . $v ['_logic'] . $v ['value'];
  470. }
  471. }
  472. } else {
  473. $temp [] = $tag . $this->dao->db->build_escape($k, 1) . '=' . $this->dao->db->build_escape($v);
  474. }
  475. }
  476. return ' SET ' . implode(',', $temp);
  477. }
  478. /**
  479. * 复杂的单表更新接口
  480. *
  481. * @param array $set
  482. * 设置字段项 eg. array('field1'=>'value1','field2'=>array('_logic'=>'+','value'=>'value2'))
  483. * @param array $map
  484. * 条件项 eg. array('field1'=>'11','field2'=>array('value1','value3'),'field3'=>array('_logic'=>'>=','value'=>1) );
  485. * @param string $table_name
  486. * @return mix(interger or bool)
  487. */
  488. public function updateByMutiple($set, $map, $table_name = null)
  489. {
  490. if (!is_array($map)) {
  491. return false;
  492. }
  493. !$table_name ? $table_name = $this->table_name : null;
  494. $sql = 'UPDATE `' . $table_name . '` ';
  495. $sql .= $this->buildSet($set);
  496. $sql .= $this->buildWhere($map);
  497. return $this->getDb()->query($sql);
  498. }
  499. /**
  500. * 复杂的计算接口
  501. *
  502. * @param array $map
  503. * 条件项 eg. array('field1'=>'11','field2'=>array('value1','value3'),'field3'=>array('_logic'=>'>=','value'=>1) );
  504. * @param string $table_name
  505. * @return int
  506. */
  507. public function countByMutiple($map = array(), $table_name = null)
  508. {
  509. !$table_name ? $table_name = $this->table_name : null;
  510. $sql = 'SELECT COUNT(*) FROM `' . $table_name . '` ';
  511. $sql .= $this->buildWhere($map);
  512. return $this->queryOne($sql);
  513. }
  514. /**
  515. * 复杂的删除语句
  516. *
  517. * @param array $map
  518. * 条件项 eg. array('field1'=>'11','field2'=>array('value1','value3'),'field3'=>array('_logic'=>'>=','value'=>1) );
  519. * @param string $table_name
  520. * @return bool
  521. */
  522. public function deleteByMutiple($map, $table_name = null)
  523. {
  524. if (!is_array($map)) {
  525. return false;
  526. }
  527. !$table_name ? $table_name = $this->table_name : null;
  528. $sql = 'DELETE FROM `' . $table_name . '` ';
  529. $sql .= $this->buildWhere($map);
  530. return $this->query($sql);
  531. }
  532. /**
  533. * 最小自增ID
  534. *
  535. * @param string $table_name 表名
  536. * @param array $map 查询条件
  537. * @return int
  538. */
  539. public function getMinId($table_name = null, $map = array())
  540. {
  541. $list = $this->getAllByMutiple($map, "MIN(id) as min_id", null, null, null, $table_name);
  542. return isset($list[0]['min_id']) ? (int)$list[0]['min_id'] : 0;
  543. }
  544. /**
  545. * 最大自增ID
  546. *
  547. * @param string $table_name 表名
  548. * @param array $map 查询条件
  549. * @return int
  550. */
  551. public function getMaxId($table_name = null, $map = array())
  552. {
  553. $list = $this->getAllByMutiple($map, "MAX(id) as max_id", null, null, null, $table_name);
  554. return isset($list[0]['max_id']) ? (int)$list[0]['max_id'] : 0;
  555. }
  556. }