SqlHelper.Class.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. <?php
  2. namespace Mall\Framework\Core;
  3. use Mall\Framework\Factory;
  4. abstract class SqlHelper
  5. {
  6. const FETCH_ASSOC = \PDO::FETCH_ASSOC;
  7. const FETCH_OBJ = \PDO::FETCH_OBJ;
  8. /**
  9. * 数据库连接
  10. *
  11. * @var db
  12. */
  13. protected $db;
  14. /**
  15. * 数据库名称
  16. *
  17. * @var string
  18. */
  19. protected $_table = null;
  20. /**
  21. * 数据库查询主键
  22. *
  23. * @var string
  24. */
  25. protected $_primary = null;
  26. /**
  27. * 数据表字段
  28. *
  29. * @var array
  30. */
  31. protected $_fields = array();
  32. /**
  33. * 只读字段
  34. *
  35. * @var array
  36. */
  37. protected $_readonly = array();
  38. /**
  39. * 自动填充字段定义
  40. *
  41. * @var array
  42. */
  43. protected $_create_autofill = array();
  44. /**
  45. * 自动更新字段定义
  46. *
  47. * @var array
  48. */
  49. protected $_update_autofill = array();
  50. /**
  51. * 自动过滤字段定义
  52. *
  53. * @var array
  54. */
  55. protected $_filters_input = array();
  56. /**
  57. * 自动过滤字段定义
  58. *
  59. * @var array
  60. */
  61. protected $_filters_output = array();
  62. /**
  63. * 插入时,字段验证定义
  64. *
  65. * @var array
  66. */
  67. protected $_validators = array();
  68. /**
  69. * 插入时,准备好的数据
  70. *
  71. * @var array
  72. */
  73. protected $_data = array();
  74. /**
  75. * select 查询join where limit 等定义
  76. *
  77. * @var array
  78. */
  79. protected $_options = array();
  80. /**
  81. * 数据获取方式
  82. *
  83. * @var int
  84. */
  85. protected $_fetch_style = self::FETCH_ASSOC;
  86. /**
  87. * 用户ID
  88. *
  89. * @var int
  90. */
  91. public $_userid = null;
  92. /**
  93. * 用户名称
  94. *
  95. * @var string
  96. */
  97. public $_username = null;
  98. /**
  99. * 用户组ID
  100. *
  101. * @var int
  102. */
  103. public $_groupid = null;
  104. /**
  105. * 用户角色ID
  106. *
  107. * @var int
  108. */
  109. public $_roleid = null;
  110. /**
  111. * 错误信息
  112. *
  113. * @var mixed
  114. */
  115. public $error = null;
  116. /**
  117. * mysql 操作符
  118. * @var array
  119. */
  120. public static $operator = ['=','!=','>=','<=','like','in','>','<'];
  121. /**
  122. * 初始化数据库,用户定义
  123. *
  124. */
  125. function __construct($serviceDB)
  126. {
  127. $this->db = Factory::db($serviceDB);
  128. // 表前缀自动拼接
  129. $tablePrefix = $this->db->prefix();
  130. if( !empty($tablePrefix) ){
  131. $this->_table = $tablePrefix .'_'.$this->_table;
  132. }
  133. }
  134. public function __set($name, $value)
  135. {
  136. $this->_data[$name] = $value;
  137. }
  138. public function __get($name)
  139. {
  140. return isset($this->_data[$name]) ? $this->_data[$name] : null;
  141. }
  142. public function __isset($name)
  143. {
  144. return isset($this->_data[$name]);
  145. }
  146. public function __unset($name)
  147. {
  148. unset($this->_data[$name]);
  149. }
  150. public function __call($method, $args)
  151. {
  152. if (in_array($method, array('field', 'where', 'order', 'limit', 'offset', 'having', 'group', 'distinct', 'data'), true)) {
  153. $this->_options[$method] = $args[0];
  154. return $this;
  155. } elseif (in_array($method, array('sum', 'min', 'max', 'avg'), true)) {
  156. $field = isset($args[0]) ? $args[0] : '*';
  157. return $this->get_field($method . '(' . $field . ') AS `count`');
  158. } elseif (preg_match("/^(get|gets|delete)_by_(.*)$/", $method, $matches)) {
  159. $field = $matches[2];
  160. if (in_array($field, $this->_fields, true)) {
  161. array_unshift($args, $field);
  162. return call_user_func_array(array($this, $matches[1] . '_by'), $args);
  163. }
  164. }
  165. return;
  166. }
  167. /**
  168. * 设置查询读取模式
  169. *
  170. * @param int $style
  171. */
  172. public function set_fetch_style($style)
  173. {
  174. $this->_fetch_style = $style;
  175. }
  176. /**
  177. * 查询数据
  178. *
  179. * @param string|array $where
  180. * @param string|array $fields
  181. * @param string|array $order
  182. * @param int $limit
  183. * @param int $offset
  184. * @param array $data
  185. * @param boolean $multiple
  186. * @return array|boolean
  187. */
  188. function select($where = null, $fields = '*', $order = null, $limit = null, $offset = null, $data = array(), $multiple = true, $isExport = false , $join = null)
  189. {
  190. $having = $group = NULL;
  191. if (!empty($this->_options)) {
  192. $fields = isset($this->_options['field']) ? $this->_options['field'] : $fields;
  193. $fields = isset($this->_options['distinct']) ? "distinct " . $this->_options['distinct'] : $fields;
  194. $where = isset($this->_options['where']) ? $this->_options['where'] : $where;
  195. $having = isset($this->_options['having']) ? $this->_options['having'] : null;
  196. $order = isset($this->_options['order']) ? $this->_options['order'] : $order;
  197. $group = isset($this->_options['group']) ? $this->_options['group'] : null;
  198. $limit = isset($this->_options['limit']) ? $this->_options['limit'] : $limit;
  199. $offset = isset($this->_options['offset']) ? $this->_options['offset'] : $offset;
  200. $this->_options = array();
  201. }
  202. if (is_array($fields)) $fields = '`' . implode('`,`', $fields) . '`';
  203. $where = $this->_where($where);
  204. if($where === false){
  205. return false;
  206. }
  207. if (!$this->_before_select($where)) return false;
  208. if ($join){
  209. $sql = "SELECT $fields FROM `$this->_table` as a ".$join;
  210. }else{
  211. $sql = "SELECT $fields FROM `$this->_table` ";
  212. }
  213. if ($where && is_string($where)) $sql .= " WHERE $where ";
  214. if ($order) $sql .= " ORDER BY $order ";
  215. if ($group) $sql .= " GROUP BY $group ";
  216. if ($having) $sql .= " HAVING $having ";
  217. if (is_null($limit) && !$multiple) $sql .= " LIMIT 1 ";
  218. $method = $multiple ? 'select' : 'get';
  219. if($isExport){
  220. $method = 'exportSelect';
  221. }
  222. // var_dump($sql);
  223. $result = is_null($limit)
  224. ? $this->db->$method($sql, $data, $this->_fetch_style)
  225. : $this->db->limit($sql, $limit, $offset, $data, $this->_fetch_style);
  226. if ($result === false || empty($result)) {
  227. if ($this->db->error() && $this->db->errno() != '00000') {
  228. $this->error = $this->db->error();
  229. return false;
  230. }
  231. return [];
  232. } else {
  233. $this->_data = $result;
  234. $this->_after_select($result, $multiple);
  235. return $result;
  236. }
  237. }
  238. /**
  239. * 查询前执行
  240. *
  241. * @param array|string $where
  242. * @return boolean
  243. */
  244. protected function _before_select(&$where)
  245. {
  246. return true;
  247. }
  248. /**
  249. * 查询后执行
  250. *
  251. * @param array $result 查询后的结果
  252. * @param boolean $multiple 是否多条数据
  253. */
  254. protected function _after_select(&$result, $multiple = true)
  255. {
  256. }
  257. /**
  258. * 分页查询
  259. *
  260. * @param array|string $where
  261. * @param array|string $fields
  262. * @param string $order
  263. * @param int $page
  264. * @param int $size
  265. * @param array $data
  266. * @return array
  267. */
  268. public function page($where = null, $fields = '*', $order = null, $page = 1, $size = 20, $data = array())
  269. {
  270. $offset = ($page - 1) * $size;
  271. return $this->select($where, $fields, $order, $size, $offset, $data, true);
  272. }
  273. /**
  274. * 获得单条数据
  275. *
  276. * @param array|string $where
  277. * @param array|string $fields
  278. * @param string $order
  279. * @return array
  280. */
  281. public function get($where = null, $fields = '*', $order = null)
  282. {
  283. return $this->select($where, $fields, $order, null, null, array(), false);
  284. }
  285. /**
  286. * 根据字段值获得单条数据
  287. *
  288. * @param string $field
  289. * @param mixed $value
  290. * @param array|string $fields
  291. * @param string $order
  292. * @return array
  293. */
  294. public function get_by($field, $value, $fields = '*', $order = null)
  295. {
  296. return $this->select("`$field`=?", $fields, $order, null, null, array($value), false);
  297. }
  298. /**
  299. * 根据字段值查询多条数据
  300. *
  301. * @param string $field
  302. * @param mixed $value
  303. * @param array|string $fields
  304. * @param string $order
  305. * @param int $limit
  306. * @param int $offset
  307. * @return array
  308. */
  309. public function gets_by($field, $value, $fields = '*', $order = null, $limit = null, $offset = 0)
  310. {
  311. return $this->select("`$field`=?", $fields, $order, $limit, $offset, array($value), true);
  312. }
  313. /**
  314. * 根据字段值查询多条数据
  315. *
  316. * @param string $field
  317. * @param mixed $value
  318. * @param array|string $fields
  319. * @param string $order
  320. * @param int $limit
  321. * @param int $offset
  322. * @return object
  323. */
  324. public function exportSelect($where = null, $fields = '*', $order = null, $limit = null, $value = [], $offset = 0)
  325. {
  326. return $this->select($where, $fields, $order, $limit, $offset, $value, true, true);
  327. }
  328. /**
  329. * 获取单条字段值
  330. *
  331. * @param string $field
  332. * @param array|string $where
  333. * @param array $data
  334. * @return mixed
  335. */
  336. public function get_field($field, $where = null, $data = array())
  337. {
  338. $r = $this->select($where, $field, null, null, null, $data, false);
  339. if(empty($r)){
  340. return $r;
  341. }
  342. return array_shift($r);
  343. }
  344. /**
  345. * 获取字段集合
  346. *
  347. * @param string $field
  348. * @param array|string $where
  349. * @param array $data
  350. * @return array
  351. */
  352. public function gets_field($field, $where = null, $data = array())
  353. {
  354. $result = array();
  355. $data = $this->select($where, $field, null, null, null, $data, true);
  356. foreach ($data as $r) {
  357. $result[] = array_shift($r);
  358. }
  359. return $result;
  360. }
  361. /**
  362. * 获取总数
  363. *
  364. * @param array|string $where
  365. * @param array $data
  366. * @return int
  367. */
  368. public function count($where = null, $data = array(), $join = null)
  369. {
  370. $where = $this->_where($where);
  371. if($where === false){
  372. return false;
  373. }
  374. if (!empty($where)) $where = " WHERE $where";
  375. if ($join){
  376. $sql = "SELECT count(*) as `count` FROM `$this->_table` as a $join $where";
  377. }else{
  378. $sql = "SELECT count(*) as `count` FROM `$this->_table` $where";
  379. }
  380. // var_dump($sql);
  381. $r = $this->db->get($sql, $data);
  382. return $r ? $r['count'] : false;
  383. }
  384. /**
  385. * 获取主健定义
  386. *
  387. * @return string
  388. */
  389. public function primary()
  390. {
  391. return isset($this->_primary) ? $this->_primary : $this->db->get_primary($this->_table);
  392. }
  393. /**
  394. * 是否存在符合条件的记录
  395. *
  396. * @param array|string $field
  397. * @param mixed $value
  398. * @return boolean
  399. */
  400. public function exists($field, $value)
  401. {
  402. return $this->db->get("SELECT `$field` FROM `$this->_table` WHERE `$field`=?", array($value)) ? true : false;
  403. }
  404. /**
  405. * 插入数据
  406. *
  407. * @param array $data
  408. * @param boolean $multiple 是否批量插入
  409. * @return int|boolean
  410. */
  411. protected function insert($data = array(), $multiple = false)
  412. {
  413. $this->_data($data);
  414. if (!$this->_before_insert($data)) return false;
  415. $this->_create_autofill($data, $multiple);
  416. if($multiple){
  417. foreach ($data as $key => $value){
  418. $insertData[] = array_values($value);
  419. }
  420. $id = $this->db->insert("INSERT INTO `$this->_table` (`" . implode('`,`', array_keys($data[0])) . "`) VALUES(" . implode(',', array_fill(0, count($data[0]), '?')) . ")", $insertData, $multiple);
  421. }else{
  422. $id = $this->db->insert("INSERT INTO `$this->_table` (`" . implode('`,`', array_keys($data)) . "`) VALUES(" . implode(',', array_fill(0, count($data), '?')) . ")", array_values($data), $multiple);
  423. }
  424. if ($id === false) {
  425. $this->error = $this->db->error();
  426. return $this->error;
  427. } else {
  428. $this->_after_insert($data);
  429. return $id;
  430. }
  431. }
  432. /**
  433. * 替换方式插入数据
  434. *
  435. * @param array $data
  436. * @return int|boolean
  437. */
  438. protected function replace($data = array(), $multiple = false)
  439. {
  440. $this->_data($data);
  441. if (!$this->_before_insert($data)) return false;
  442. $this->_create_autofill($data, $multiple);
  443. if($multiple){
  444. foreach ($data as $key => $value){
  445. $insertData[] = array_values($value);
  446. }
  447. $id = $this->db->insert("REPLACE INTO `$this->_table` (`" . implode('`,`', array_keys($data[0])) . "`) VALUES(" . implode(',', array_fill(0, count($data[0]), '?')) . ")", $insertData, $multiple);
  448. }else{
  449. $id = $this->db->insert("REPLACE INTO `$this->_table` (`" . implode('`,`', array_keys($data)) . "`) VALUES(" . implode(',', array_fill(0, count($data), '?')) . ")", array_values($data));
  450. }
  451. if ($id === false) {
  452. $this->error = $this->db->error();
  453. return false;
  454. } else {
  455. $this->_after_insert($data);
  456. return $id;
  457. }
  458. }
  459. /**
  460. * 插入之前执行
  461. *
  462. * @param array $data
  463. * @return boolean
  464. */
  465. protected function _before_insert(&$data)
  466. {
  467. return true;
  468. }
  469. /**
  470. * 插入之后执行
  471. *
  472. * @param array $data
  473. */
  474. protected function _after_insert(&$data)
  475. {
  476. }
  477. /**
  478. * 根据ID拷贝一份
  479. *
  480. * @param int $id
  481. * @param array $data
  482. * @return int/boolean
  483. */
  484. public function copy_by_id($id, $data = array())
  485. {
  486. $r = $this->db->get("SELECT * FROM `$this->_table` WHERE `$this->_primary`=?", array($id));
  487. if (!$r) return false;
  488. unset($r[$this->_primary]);
  489. if ($data) $r = array_merge($r, $data);
  490. return $this->insert($r);
  491. }
  492. /**
  493. * 更新
  494. *
  495. * @param array $data
  496. * @param array /string $where
  497. * @param int $limit
  498. * @param array /string $order
  499. * @return int/boolean
  500. */
  501. protected function update($data = array(), $where = null, $limit = null, $order = null)
  502. {
  503. if (!empty($this->_options)) {
  504. $where = isset($this->_options['where']) ? $this->_options['where'] : $where;
  505. $order = isset($this->_options['order']) ? $this->_options['order'] : $order;
  506. $limit = isset($this->_options['limit']) ? $this->_options['limit'] : $limit;
  507. $offset = isset($this->_options['offset']) ? $this->_options['offset'] : $offset;
  508. $this->_options = array();
  509. }
  510. $this->_data($data);
  511. $where = $this->_where($where);
  512. if($where === false){
  513. return false;
  514. }
  515. if (!$this->_before_update($data, $where)) return false;
  516. $this->_update_autofill($data);
  517. $this->_readonly($data);
  518. $sql = "UPDATE `$this->_table` SET `" . implode('`=?,`', array_keys($data)) . "`=?";
  519. if ($where) $sql .= " WHERE $where ";
  520. if ($order) $sql .= " ORDER BY $order ";
  521. if ($limit) $sql .= " LIMIT $limit ";
  522. $result = $this->db->update($sql, array_values($data));
  523. if ($result === FALSE) {
  524. $this->error = $this->db->error();
  525. return false;
  526. } else {
  527. $this->_after_update($data, $where);
  528. return $result;
  529. }
  530. }
  531. /**
  532. * query方法
  533. */
  534. protected function query($sql)
  535. {
  536. if(!empty($sql)){
  537. $result = $this->db->query($sql);
  538. if($result === false){
  539. $this->error = $this->db->error();
  540. return false;
  541. }else{
  542. return $result;
  543. }
  544. }else{
  545. $this->error = 'sql is null';
  546. return false;
  547. }
  548. }
  549. /**
  550. * query方法
  551. */
  552. protected function exportQuery($sql)
  553. {
  554. if(!empty($sql)){
  555. $result = $this->db->exportQuery($sql);
  556. if($result === false){
  557. $this->error = $this->db->error();
  558. return false;
  559. }else{
  560. return $result;
  561. }
  562. }else{
  563. $this->error = 'sql is null';
  564. return false;
  565. }
  566. }
  567. /**
  568. * 更新记录之前执行
  569. *
  570. * @param array $data
  571. * @param array|string $where
  572. * @return boolean
  573. */
  574. protected function _before_update(&$data, $where)
  575. {
  576. return true;
  577. }
  578. /**
  579. * 更新记录之后执行
  580. *
  581. * @param array $data
  582. * @param array|string $where
  583. */
  584. protected function _after_update(&$data, $where)
  585. {
  586. }
  587. /**
  588. * 更新一个字段值
  589. *
  590. * @param string $field
  591. * @param mixed $value
  592. * @param string /array $where
  593. * @return int/boolean
  594. */
  595. public function set_field($field, $value, $where = null)
  596. {
  597. return $this->update(array($field => $value), $where);
  598. }
  599. /**
  600. * 递增一个int字段值
  601. *
  602. * @param string $field
  603. * @param string /array $where
  604. * @param int $step
  605. * @param array $data
  606. * @return int/boolean
  607. */
  608. public function set_inc($field, $where = null, $step = 1, $data = array())
  609. {
  610. $where = $this->_where($where);
  611. if($where === false){
  612. return false;
  613. }
  614. if(empty($step)){
  615. $step = 0;
  616. }
  617. $sql = "UPDATE " .$this->_table." SET `$field`=IFNULL(`$field`,0)+".$step." WHERE $where";
  618. return $this->db->update($sql, $data);
  619. }
  620. /**
  621. * 递减一个int字段值
  622. *
  623. * @param string $field
  624. * @param array /string $where
  625. * @param int $step
  626. * @param array $data
  627. * @return int/boolean
  628. */
  629. public function set_dec($field, $where = null, $step = 1, $data = array())
  630. {
  631. $where = $this->_where($where);
  632. if($where === false){
  633. return false;
  634. }
  635. if(empty($step)){
  636. $step = 0;
  637. }
  638. $sql = "UPDATE ".$this->_table." SET `$field`=IFNULL(`$field`,0)-".$step." WHERE $where";
  639. return $this->db->update($sql, $data);
  640. }
  641. /**
  642. * 删除
  643. *
  644. * @param array /string $where
  645. * @param int $limit
  646. * @param array /string $order
  647. * @param array $data
  648. * @return int/boolean
  649. */
  650. protected function delete($where = null, $limit = null, $order = null, $data = array())
  651. {
  652. if (!empty($this->_options)) {
  653. $where = isset($this->_options['where']) ? $this->_options['where'] : $where;
  654. $order = isset($this->_options['order']) ? $this->_options['order'] : $order;
  655. $limit = isset($this->_options['limit']) ? $this->_options['limit'] : $limit;
  656. $offset = isset($this->_options['offset']) ? $this->_options['offset'] : $offset;
  657. $this->_options = array();
  658. }
  659. $where = $this->_where($where);
  660. if($where === false){
  661. return false;
  662. }
  663. if (!$this->_before_delete($where)) return false;
  664. $sql = "DELETE FROM `$this->_table`";
  665. $asql = $sql;
  666. if ($where) $sql .= " WHERE $where ";
  667. if ($limit) {
  668. if ($order) $sql .= " ORDER BY $order ";
  669. $sql .= " LIMIT $limit ";
  670. }
  671. if ($sql == $asql) {
  672. return false;
  673. }
  674. $result = $this->db->delete($sql, $data);
  675. if ($result === FALSE) {
  676. $this->error = $this->db->error();
  677. return false;
  678. } else {
  679. $this->_after_delete($where);
  680. return $result;
  681. }
  682. }
  683. /**
  684. * 删除前执行
  685. *
  686. * @param array|string $where
  687. * @return boolean
  688. */
  689. protected function _before_delete(&$where)
  690. {
  691. return true;
  692. }
  693. /**
  694. * 删除后执行
  695. *
  696. * @param array|string $where
  697. */
  698. protected function _after_delete(&$where)
  699. {
  700. }
  701. /**
  702. * 根据字段值查询
  703. *
  704. * @param string $field
  705. * @param mixed $value
  706. * @param int $limit
  707. * @param array|string $order
  708. * @return int|boolean
  709. */
  710. public function delete_by($field, $value, $limit = null, $order = null)
  711. {
  712. return $this->delete("`$field`=?", $limit, $order, array($value));
  713. }
  714. /**
  715. * 获得一个字段的类型
  716. *
  717. * @param string $field
  718. * @return string
  719. */
  720. protected function _fieldtype($field)
  721. {
  722. static $fields;
  723. if (is_null($fields) || !isset($fields[$this->_table])) {
  724. if ($data = $this->db->list_fields($this->_table)) {
  725. foreach ($data as $k => $v) {
  726. $fields[$v['Field']] = $v;
  727. }
  728. }
  729. }
  730. return isset($fields[$field]) && isset($fields[$field]['Type']) ? $fields[$field]['Type'] : '';
  731. }
  732. private function _create_autofill(& $data, $multiple = false)
  733. {
  734. if (empty($this->_create_autofill)) return true;
  735. if($multiple){
  736. foreach ($this->_create_autofill as $field => $val) {
  737. foreach ($data as $key => $value){
  738. if (!isset($data[$key][$field])) $data[$key][$field] = $val;
  739. }
  740. }
  741. }else{
  742. foreach ($this->_create_autofill as $field => $val) {
  743. if (!isset($data[$field])) $data[$field] = $val;
  744. }
  745. }
  746. }
  747. private function _update_autofill(& $data)
  748. {
  749. if (empty($this->_update_autofill)) return true;
  750. foreach ($this->_update_autofill as $field => $val) {
  751. if (!isset($data[$field])) $data[$field] = $val;
  752. }
  753. }
  754. private function _readonly(& $data)
  755. {
  756. if (empty($this->_readonly)) return true;
  757. foreach ($this->_readonly as $field => $val) {
  758. if (isset($data[$field])) unset($data[$field]);
  759. }
  760. }
  761. private function checkVaule($where){
  762. if(is_string($where)){
  763. return addslashes($where);
  764. }
  765. return $where;
  766. }
  767. private function _where(& $where)
  768. {
  769. if (empty($where) && isset($this->_data[$this->_primary])) $where = $this->_data[$this->_primary];
  770. if (empty($where)) return null;
  771. if (is_numeric($where)) {
  772. $where = "`$this->_primary`=$where";
  773. } elseif (is_array($where)) {
  774. $condition = array();
  775. foreach ($where as $key => $value){
  776. if($key === 0 && is_numeric($value)){
  777. $where = array_map(array(__CLASS__,'checkVaule'), $where);
  778. $ids = is_numeric($where[0]) ? implode(',', $where) : "'" . implode("','", $where) . "'";
  779. $where = "`$this->_primary` IN($ids)";
  780. break;
  781. }else{
  782. if(is_numeric($key) && count($value) == 3){
  783. // if(!in_array($value[0],$this->_fields)){
  784. // $this->error = $value[0].'字段未被映射';
  785. // return false;
  786. // }
  787. if(!in_array($value[1], self::$operator)){
  788. $this->error = $value[1].'查询不支持';
  789. return false;
  790. }
  791. if(is_array($value[2])){
  792. $sqlIn = '';
  793. $len = count($value[2]);
  794. foreach ($value[2] as $k => $v){
  795. if(!is_numeric($v)){
  796. $sqlIn .="'".$v."'";
  797. }else{
  798. $sqlIn .= $v;
  799. }
  800. if($len >1 && $k+1 != $len){
  801. $sqlIn .= ',';
  802. }
  803. }
  804. $condition[] = "`$value[0]` $value[1] (".$sqlIn.")";
  805. }else{
  806. if (strstr($value[0], '|')){
  807. $str = explode('|', $value[0]);
  808. $condition[] = "$str[0] $value[1] '".$this->checkVaule($value[2])."' OR "."$str[1] $value[1] '".$this->checkVaule($value[2])."'";
  809. }else{
  810. if ($value[1] == 'in'){
  811. $condition[] = "$value[0] $value[1] (".$this->checkVaule($value[2]).")";
  812. }else{
  813. $condition[] = "$value[0] $value[1] '".$this->checkVaule($value[2])."'";
  814. }
  815. }
  816. }
  817. $condition;
  818. }else{
  819. $where = array_map(array(__CLASS__,'checkVaule'), $where);
  820. // if (in_array($key, $this->_fields)) {
  821. if(is_array($value)){
  822. $sqlIn = '';
  823. $len = count($value);
  824. foreach ($value as $k => $v){
  825. if(!is_numeric($v)){
  826. $sqlIn .="'".$v."'";
  827. }else{
  828. $sqlIn .= $v;
  829. }
  830. if($len >1 && $k+1 != $len){
  831. $sqlIn .= ',';
  832. }
  833. }
  834. $condition[] = "$key in(".$sqlIn.")";
  835. }else{
  836. if (strstr($key, '|')){
  837. $str = explode('|', $key);
  838. $condition[] = "$str[0] ='$value'"."' OR "."$str[0] ='$value'";
  839. }else{
  840. $condition[] = "$key ='$value'";
  841. }
  842. }
  843. // }else{
  844. // $this->error = $key.'字段未被映射';
  845. // return false;
  846. // }
  847. }
  848. }
  849. }
  850. if(!empty($condition)){
  851. $where = implode(' AND ', $condition);
  852. }
  853. } elseif (preg_match("/^[0-9a-z\'\"\,\s]+$/i", $where)) {
  854. $where = strpos($where, ',') === false ? "`$this->_primary`='$where'" : "`$this->_primary` IN($where)";
  855. }
  856. $notice_flags = array('select', 'delete', 'insert', 'drop', 'update', 'benchmark', 'database');
  857. $quote = strpos($where, '"') === true ? '"' : "'";
  858. foreach ($notice_flags as $w) {
  859. $where = preg_replace('#([\s\(\)\!])' . $w . '([\s\(\)\*])#i', "$1" . substr($w, 0, 1) . $quote . '/**/' . $quote . substr($w, 1) . "$2", $where);
  860. }
  861. return $where;
  862. }
  863. private function _data(& $data)
  864. {
  865. if (empty($data)) {
  866. if (!empty($this->_options['data'])) {
  867. $data = $this->_options['data'];
  868. } elseif (!empty($this->_data)) {
  869. $data = $this->_data;
  870. } elseif (!empty($_POST)) {
  871. $data = $_POST;
  872. }
  873. }
  874. }
  875. /**
  876. * 过滤不需要的字段
  877. *
  878. * @param array $data 输入数据
  879. * @param array $keys 需要的字段定义
  880. * @return array
  881. */
  882. protected function filter_array($data, $keys)
  883. {
  884. // 合并附加字段
  885. $fields = $this->_get_extra_fileds();
  886. if ($fields) {
  887. $fields = array_filter(array_keys($fields));
  888. if ($fields) {
  889. $keys = array_unique(array_merge($keys, $fields));
  890. }
  891. }
  892. foreach ($data as $field => $v) {
  893. if (!in_array($field, $keys)) unset($data[$field]);
  894. }
  895. return $data;
  896. }
  897. /**
  898. * 获取模型操作中的错误,并返回字符串类型的错误
  899. *
  900. * @return string
  901. */
  902. public function error()
  903. {
  904. $error = $this->error;
  905. if (empty($error)) {
  906. $error = $this->db->error();
  907. }
  908. if (empty($error)) {
  909. return '';
  910. } else {
  911. if (is_array($error)) {
  912. $error = implode("*****", $error);
  913. }
  914. return $error;
  915. }
  916. }
  917. /**
  918. * 切换分表
  919. * @param $tablename
  920. */
  921. public function get_Table()
  922. {
  923. return $this->_table;
  924. }
  925. /**
  926. * 切换分表
  927. * @param $tablename
  928. */
  929. public function set_Table($tablename)
  930. {
  931. $this->_table = $tablename;
  932. }
  933. /**
  934. * 开启事务
  935. * @param bool $foreign_key_checks
  936. * @return mixed
  937. */
  938. public function beginTransaction($foreign_key_checks = false)
  939. {
  940. return $this->db->beginTransaction($foreign_key_checks)?true:false;
  941. }
  942. /**
  943. * 提交事务
  944. * @return mixed
  945. */
  946. public function commit()
  947. {
  948. return $this->db->commit();
  949. }
  950. /**
  951. * 回滚事务
  952. * @return mixed
  953. */
  954. public function rollBack()
  955. {
  956. return $this->db->rollBack();
  957. }
  958. }