table.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. public function getStockCodeList($filter = array(), $count = 0, $page = 0, $pagesize = 0){
  60. $where="where 1=1";
  61. $sql = "select "."`stock_code`". "from ". $this->table_fullname ." $where order by ".$this->table_id." desc";
  62. var_dump($sql);
  63. $rs = $this->pdo->sqlQuery($sql);
  64. $r = array();
  65. if($rs){
  66. foreach($rs as $key => $val){
  67. $r[$key] = $this->dataToAttr($val);
  68. }
  69. return $r;
  70. }else{
  71. return $r;
  72. }
  73. }
  74. //获取列表(分页)
  75. //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
  76. //
  77. //@param $filter array -- 过滤条件,格式见Table::filterToWhere
  78. //@param $count -- 0:返回列表 1:返回结果数量
  79. //@param $page -- 当前第几页
  80. //@param $pagesize -- 每页数量
  81. public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0){
  82. $where = $this->filterToWhere($filter);
  83. if($count == 0){//列表
  84. $sql = "select * from ". $this->table_fullname ." $where order by ".$this->table_id." desc";
  85. if($page > 0){//分页
  86. $startrow = ($page - 1) * $pagesize;
  87. $sql_limit = " limit $startrow, $pagesize";
  88. $sql .= $sql_limit;
  89. }
  90. $rs = $this->pdo->sqlQuery($sql);
  91. $r = array();
  92. if($rs){
  93. foreach($rs as $key => $val){
  94. $r[$key] = $this->dataToAttr($val);
  95. }
  96. return $r;
  97. }else{
  98. return $r;
  99. }
  100. }else{//统计
  101. $sql = "select count(*) as c from ". $this->table_fullname . " $where ";
  102. $rs = $this->pdo->sqlQuery($sql);
  103. if($rs){
  104. return $rs[0]['c'];
  105. }else{
  106. return 0;
  107. }
  108. }
  109. }
  110. //删
  111. public function del($id){
  112. $where = array(
  113. $this->table_id => array('number', $id)
  114. );
  115. return $this->pdo->sqldelete($this->table_fullname, $where);
  116. }
  117. //--------------------------------------------------
  118. //------------未实现的常见方法,便于重载------------
  119. //--------------------------------------------------
  120. //改
  121. //@param $attr 数组,键值参考add()
  122. public function edit($id, $attr){}
  123. //修改指定字段
  124. public function update($id, $attrs)
  125. {
  126. $params = array();
  127. foreach ($attrs as $key => $value) {
  128. //$type = self::getTypeByAttr($key);
  129. $params[$this->table_name.'_'.$key] = array('string', $value);
  130. }
  131. //where条件
  132. $where = array( $this->table_id => array("number", $id));
  133. //返回结果
  134. $r = $this->pdo->sqlupdate($this->table_fullname, $params, $where);
  135. return $r;
  136. }
  137. //--------------------------------------------------
  138. //------------依赖于字段设置的常见方法,可以重载----
  139. //--------------------------------------------------
  140. //单独修改状态
  141. public function updateStatus($id, $status){
  142. $where = array(
  143. $this->table_id => array('number', $id)
  144. );
  145. $param = array(
  146. $this->table_status => array('number', $status)
  147. );
  148. return $this->pdo->sqlupdate($this->table_fullname, $param, $where);
  149. }
  150. //单独修改排序
  151. public function updateOrder($id, $order){
  152. $where = array(
  153. $this->table_id => array('number', $id)
  154. );
  155. $param = array(
  156. $this->table_order => array('number', $order)
  157. );
  158. return $this->pdo->sqlupdate($this->table_fullname, $param, $where);
  159. }
  160. //--------------------------------------------------
  161. //------------其他方法------------------------------
  162. //--------------------------------------------------
  163. //从数据库取出的数据转化键值后输出
  164. protected function dataToAttr($data){
  165. $r = array();
  166. foreach($this->table_struct as $k => $v){
  167. $r[$k] = $data[$v];
  168. }
  169. return $r;
  170. }
  171. //把filter转化为where子句
  172. //仅支持最常见的=和like,不支持其他符号
  173. //@param $filter array 键值要符合struct()中的定义
  174. //----参数示例----
  175. # $filter = array(
  176. # 'account' => array('abc', '=s'),//精确搜索
  177. # 'title' => array('xyz', '%s'),//模糊搜索
  178. # 'desc' => 'test',//不指定则为精确搜索
  179. # 'type' => 1
  180. # )
  181. //----参数示例结束----
  182. protected function filterToWhere($filter){
  183. $struct = $this->table_struct;
  184. $where = ' where 1=1 ';
  185. foreach($filter as $k => $v){
  186. if(is_array($v)){
  187. $val = $v[0];
  188. $operator = $v[1];
  189. }else{
  190. $val = $v;
  191. $operator = '=s';
  192. }
  193. $field_name = $struct[$k];
  194. if($operator == '=n'){//数字
  195. $val = $this->pdo->sql_check_input(array('number', $val));
  196. $where .= " and $field_name = $val ";
  197. }
  198. if($operator == '=s'){//字符串,精确搜索
  199. $val = $this->pdo->sql_check_input(array('string', $val));
  200. $where .= " and $field_name = $val ";
  201. }
  202. if($operator == '>num'){//大于数字
  203. $val = $this->pdo->sql_check_input(array('number', $val));
  204. $where .= " and $field_name > $val ";
  205. }
  206. if($operator == '<num'){//小于数字
  207. $val = $this->pdo->sql_check_input(array('number', $val));
  208. $where .= " and $field_name < $val ";
  209. }
  210. if($operator == '%s'){//字符串,模糊搜索
  211. $val = '%'.$val.'%';
  212. $val = $this->pdo->sql_check_input(array('string', $val));
  213. $where .= " and $field_name like $val " ;
  214. }
  215. if($operator == '=n_arr'){//or 连接的数字数组
  216. $where .= " and (";
  217. $first = true;
  218. foreach ($val as $item) {
  219. if ($first) {
  220. $where .= " $field_name = $item ";
  221. $first = false;
  222. } else {
  223. $where .= " or $field_name = $item ";
  224. }
  225. }
  226. $where .= ") ";
  227. }
  228. if($operator == '=date'){//时间戳在当天
  229. $val = $this->pdo->sql_check_input(array('number', $val));
  230. if (empty($val)) {
  231. break;
  232. }
  233. $begin = $val; //不分时区
  234. $end = $val + 24*60*60;
  235. $where .= " and ($field_name >= $begin and $field_name < $end) ";
  236. }
  237. if($operator == 'date2date'){//时间戳在两天之间
  238. if (!empty($val[0])) {
  239. $begin = $val[0] - 8*60*60; //东8时区
  240. $where .= " and $field_name >= $begin ";
  241. }
  242. if (!empty($val[1])) {
  243. $end = $val[1] + 16*60*60;
  244. $where .= " and $field_name < $end ";
  245. }
  246. }
  247. if($operator == 'id_in_arr'){//id在数组中
  248. $where .= " and (1=0 ";
  249. foreach ($val as $tempId) {
  250. $tempId = $this->pdo->sql_check_input(array('number', $tempId));
  251. $where .= " or $field_name=$tempId ";
  252. }
  253. $where .= ") ";
  254. }
  255. }
  256. return $where;
  257. }
  258. }
  259. ?>