table.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. /**
  3. * 数据库表抽象类
  4. *
  5. * 定义必须实现的抽象方法
  6. * 定义可以继承的方法,提高table层的书写效率
  7. *
  8. * @createtime 2018/03/01
  9. * @author 空竹
  10. * @copyright 芝麻开发(http://www.zhimawork.com)
  11. */
  12. abstract class Table {
  13. protected $table_name = '';//表名,不带前缀,前缀在config中定义
  14. protected $table_id = '';//指定ID字段名称,必须
  15. protected $table_status = '';//指定状态字段名称,如果有
  16. protected $table_order = '';//指定排序字段名称,如果有
  17. protected $pdo;
  18. protected $table_fullname;
  19. protected $table_struct;
  20. function __construct(){
  21. //初始数据库连接PDO
  22. global $mypdo;
  23. $this->pdo = $mypdo;
  24. $table_prefix = $this->pdo->prefix;
  25. //数据库全名
  26. $this->table_fullname = $table_prefix . $this->table_name;
  27. //数据库结构
  28. $this->table_struct = $this->struct();
  29. }
  30. ////////////////////////////////////////////////////
  31. /**************抽象方法,子类必须实现**************/
  32. ////////////////////////////////////////////////////
  33. //数据库结构
  34. //完整展示数据库所有字段及其含义
  35. //映射字段名称提供给其他层使用,不对外暴露数据库字段
  36. abstract protected function struct();
  37. //增
  38. //@param $attr array -- 键值同struct()返回的数组
  39. abstract public function add($attr);
  40. ///////////////////////////////////////////////////////////////
  41. /**************非抽象方法,用于继承,也可以重载***************/
  42. ///////////////////////////////////////////////////////////////
  43. //获取详情
  44. public function getInfoById($id){
  45. //查询语句必须用sql_check_input检查参数
  46. $id = $this->pdo->sql_check_input(array('number', $id));
  47. $sql = "select * from ". $this->table_fullname ." where ". $this->table_id ." = $id limit 1";
  48. $rs = $this->pdo->sqlQuery($sql);
  49. $r = array();
  50. if($rs){
  51. foreach($rs as $key => $val){
  52. $r[$key] = $this->dataToAttr($val);
  53. }
  54. return $r[0];
  55. }else{
  56. return $r;
  57. }
  58. }
  59. //此项目每个表都有code字段,所以放在基类里面
  60. public function getInfoByCode($code){
  61. //查询语句必须用sql_check_input检查参数
  62. $code = trim($code);
  63. $code = $this->pdo->sql_check_input(array('string', $code));
  64. $sql = "select * from ". $this->table_fullname ." where ".$this->table_name."_code = $code limit 1";
  65. $rs = $this->pdo->sqlQuery($sql);
  66. $r = array();
  67. if($rs){
  68. foreach($rs as $key => $val){
  69. $r[$key] = $this->dataToAttr($val);
  70. }
  71. return $r[0];
  72. }else{
  73. return $r;
  74. }
  75. }
  76. //此项目每个表都有code字段,大部分表有date字段,放在基类里面统一处理
  77. public function getInfoByCodeAndDate($code, $date){
  78. //查询语句必须用sql_check_input检查参数
  79. $code = trim($code);
  80. $code = $this->pdo->sql_check_input(array('string', $code));
  81. //查询语句必须用sql_check_input检查参数
  82. $date = trim($date);
  83. $date = $this->pdo->sql_check_input(array('number', $date));
  84. $sql = "select * from ". $this->table_fullname ." where ".$this->table_name."_code = $code and ".$this->table_name."_date = $date limit 1";
  85. $rs = $this->pdo->sqlQuery($sql);
  86. $r = array();
  87. if($rs){
  88. foreach($rs as $key => $val){
  89. $r[$key] = $this->dataToAttr($val);
  90. }
  91. return $r[0];
  92. }else{
  93. return $r;
  94. }
  95. }
  96. //获取列表(分页)
  97. //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
  98. //
  99. //@param $filter array -- 过滤条件,格式见Table::filterToWhere
  100. //@param $count -- 0:返回列表 1:返回结果数量
  101. //@param $page -- 当前第几页
  102. //@param $pagesize -- 每页数量
  103. public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0, $order = 0){
  104. $where = $this->filterToWhere($filter);
  105. $orderByStr = (empty($order) ? 'asc' : 'desc');
  106. if($count == 0){//列表
  107. $sql = "select * from ". $this->table_fullname ." $where order by ".$this->table_id." ".$orderByStr;
  108. if($page > 0){//分页
  109. $startrow = ($page - 1) * $pagesize;
  110. $sql_limit = " limit $startrow, $pagesize";
  111. $sql .= $sql_limit;
  112. }
  113. $rs = $this->pdo->sqlQuery($sql);
  114. $r = array();
  115. if($rs){
  116. foreach($rs as $key => $val){
  117. $r[$key] = $this->dataToAttr($val);
  118. }
  119. return $r;
  120. }else{
  121. return $r;
  122. }
  123. }else{//统计
  124. $sql = "select count(*) as c from ". $this->table_fullname . " $where ";
  125. $rs = $this->pdo->sqlQuery($sql);
  126. if($rs){
  127. return $rs[0]['c'];
  128. }else{
  129. return 0;
  130. }
  131. }
  132. }
  133. //获取列表(分页)
  134. //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
  135. //
  136. //@param $filter array -- 过滤条件,格式见Table::filterToWhere
  137. //@param $count -- 0:返回列表 1:返回结果数量
  138. //@param $page -- 当前第几页
  139. //@param $pagesize -- 每页数量
  140. //本函数与 getList 区别在于,使用date字段进行排序,没有date字段的表不能使用本函数
  141. public function getListOrderByDate($filter = array(), $count = 0, $page = 0, $pagesize = 0, $order = 0){
  142. $where = $this->filterToWhere($filter);
  143. $orderByStr = (empty($order) ? 'asc' : 'desc');
  144. if($count == 0){//列表
  145. $sql = "select * from ". $this->table_fullname ." $where order by ".$this->table_name."_date ".$orderByStr;
  146. if($page > 0){//分页
  147. $startrow = ($page - 1) * $pagesize;
  148. $sql_limit = " limit $startrow, $pagesize";
  149. $sql .= $sql_limit;
  150. }
  151. $rs = $this->pdo->sqlQuery($sql);
  152. $r = array();
  153. if($rs){
  154. foreach($rs as $key => $val){
  155. $r[$key] = $this->dataToAttr($val);
  156. }
  157. return $r;
  158. }else{
  159. return $r;
  160. }
  161. }else{//统计
  162. $sql = "select count(*) as c from ". $this->table_fullname . " $where ";
  163. $rs = $this->pdo->sqlQuery($sql);
  164. if($rs){
  165. return $rs[0]['c'];
  166. }else{
  167. return 0;
  168. }
  169. }
  170. }
  171. //删
  172. public function del($id){
  173. $where = array(
  174. $this->table_id => array('number', $id)
  175. );
  176. return $this->pdo->sqldelete($this->table_fullname, $where);
  177. }
  178. //--------------------------------------------------
  179. //------------未实现的常见方法,便于重载------------
  180. //--------------------------------------------------
  181. //改
  182. //@param $attr 数组,键值参考add()
  183. public function edit($id, $attr){}
  184. //修改指定字段
  185. public function update($id, $attrs)
  186. {
  187. $params = array();
  188. foreach ($attrs as $key => $value) {
  189. //$type = self::getTypeByAttr($key);
  190. $params[$this->table_name.'_'.$key] = array('string', $value);
  191. }
  192. //where条件
  193. $where = array( $this->table_id => array("number", $id));
  194. //返回结果
  195. $r = $this->pdo->sqlupdate($this->table_fullname, $params, $where);
  196. return $r;
  197. }
  198. //code不存在则add,否则update
  199. public function addOrUpdateByCode($attrs)
  200. {
  201. if (empty($attrs) || empty($attrs['code'])) {
  202. return 0;
  203. }
  204. $rs = $this->getInfoByCode($attrs['code']);
  205. if (empty($rs)) {
  206. $id = $this->add($attrs);
  207. } else {
  208. $id = $rs['id'];
  209. $this->update($id, $attrs);
  210. }
  211. return $id;
  212. }
  213. //(code,date)为唯一标识,不存在则add,否则update
  214. public function addOrUpdateByCodeDate($attrs)
  215. {
  216. if (empty($attrs) || empty($attrs['code']) || empty($attrs['date'])) {
  217. return 0;
  218. }
  219. $rs = $this->getInfoByCodeAndDate($attrs['code'], $attrs['date']);
  220. if (empty($rs)) {
  221. $id = $this->add($attrs);
  222. } else {
  223. $id = $rs['id'];
  224. $this->update($id, $attrs);
  225. }
  226. return $id;
  227. }
  228. //--------------------------------------------------
  229. //------------依赖于字段设置的常见方法,可以重载----
  230. //--------------------------------------------------
  231. //单独修改状态
  232. public function updateStatus($id, $status){
  233. $where = array(
  234. $this->table_id => array('number', $id)
  235. );
  236. $param = array(
  237. $this->table_status => array('number', $status)
  238. );
  239. return $this->pdo->sqlupdate($this->table_fullname, $param, $where);
  240. }
  241. //单独修改排序
  242. public function updateOrder($id, $order){
  243. $where = array(
  244. $this->table_id => array('number', $id)
  245. );
  246. $param = array(
  247. $this->table_order => array('number', $order)
  248. );
  249. return $this->pdo->sqlupdate($this->table_fullname, $param, $where);
  250. }
  251. //--------------------------------------------------
  252. //------------其他方法------------------------------
  253. //--------------------------------------------------
  254. //从数据库取出的数据转化键值后输出
  255. protected function dataToAttr($data){
  256. $r = array();
  257. foreach($this->table_struct as $k => $v){
  258. $r[$k] = $data[$v];
  259. }
  260. return $r;
  261. }
  262. //把filter转化为where子句
  263. //仅支持最常见的=和like,不支持其他符号
  264. //@param $filter array 键值要符合struct()中的定义
  265. //----参数示例----
  266. # $filter = array(
  267. # 'account' => array('abc', '=s'),//精确搜索
  268. # 'title' => array('xyz', '%s'),//模糊搜索
  269. # 'desc' => 'test',//不指定则为精确搜索
  270. # 'type' => 1
  271. # )
  272. //----参数示例结束----
  273. protected function filterToWhere($filter){
  274. $struct = $this->table_struct;
  275. $where = ' where 1=1 ';
  276. foreach($filter as $k => $v){
  277. if(is_array($v)){
  278. $val = $v[0];
  279. $operator = $v[1];
  280. $extraParam = $v[2]; //仅自定义的特殊方法使用
  281. }else{
  282. $val = $v;
  283. $operator = '=s';
  284. }
  285. $field_name = $struct[$k];
  286. if($operator == '=n'){//数字
  287. $val = $this->pdo->sql_check_input(array('number', $val));
  288. $where .= " and $field_name = $val ";
  289. }
  290. if($operator == '=s'){//字符串,精确搜索
  291. $val = $this->pdo->sql_check_input(array('string', $val));
  292. $where .= " and $field_name = $val ";
  293. }
  294. if($operator == '>n'){//大于数字
  295. $val = $this->pdo->sql_check_input(array('number', $val));
  296. $where .= " and $field_name > $val ";
  297. }
  298. if($operator == '>=n'){//大于等于数字
  299. $val = $this->pdo->sql_check_input(array('number', $val));
  300. $where .= " and $field_name >= $val ";
  301. }
  302. if($operator == '<n'){//小于数字
  303. $val = $this->pdo->sql_check_input(array('number', $val));
  304. $where .= " and $field_name < $val ";
  305. }
  306. if($operator == '<=n'){//小于等于数字
  307. $val = $this->pdo->sql_check_input(array('number', $val));
  308. $where .= " and $field_name <= $val ";
  309. }
  310. if($operator == '%s'){//字符串,模糊搜索
  311. $val = '%'.$val.'%';
  312. $val = $this->pdo->sql_check_input(array('string', $val));
  313. $where .= " and $field_name like $val " ;
  314. }
  315. if($operator == '=n_arr'){//or 连接的数字数组
  316. $where .= " and (";
  317. $first = true;
  318. foreach ($val as $item) {
  319. if ($first) {
  320. $where .= " $field_name = $item ";
  321. $first = false;
  322. } else {
  323. $where .= " or $field_name = $item ";
  324. }
  325. }
  326. $where .= ") ";
  327. }
  328. if($operator == '=date'){//时间戳在当天
  329. $val = $this->pdo->sql_check_input(array('number', $val));
  330. if (empty($val)) {
  331. break;
  332. }
  333. $begin = $val; //不分时区
  334. $end = $val + 24*60*60;
  335. $where .= " and ($field_name >= $begin and $field_name < $end) ";
  336. }
  337. if($operator == 'date2date'){//时间戳在两天之间
  338. if (!empty($val[0])) {
  339. $begin = $val[0] - 8*60*60; //东8时区
  340. $where .= " and $field_name >= $begin ";
  341. }
  342. if (!empty($val[1])) {
  343. $end = $val[1] + 16*60*60;
  344. $where .= " and $field_name < $end ";
  345. }
  346. }
  347. if($operator == 'id_in_arr'){//id在数组中
  348. $where .= " and (1=0 ";
  349. foreach ($val as $tempId) {
  350. $tempId = $this->pdo->sql_check_input(array('number', $tempId));
  351. $where .= " or $field_name=$tempId ";
  352. }
  353. $where .= ") ";
  354. }
  355. //以下是本项目特殊处理方法
  356. if($operator == 'date_between'){//date在两个日期之间
  357. $val = $this->pdo->sql_check_input(array('number', $val));
  358. $extraParam = $this->pdo->sql_check_input(array('number', $extraParam));
  359. $where .= " and $field_name > $extraParam and $field_name <= $val ";
  360. }
  361. }
  362. return $where;
  363. }
  364. }
  365. ?>