db.init.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 Dao-db 常用SQL方法封装
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * $Author:zhuli
  10. * $Dtime:2013-5-29
  11. ***********************************************************************************/
  12. require_once("sqlbuild.init.php");
  13. class dbInit extends sqlbuildInit {
  14. /**
  15. * 重写MYSQL中的QUERY,对SQL语句进行监控
  16. * @param string $sql
  17. */
  18. public function query($sql, $is_set_default = true) {
  19. $this->get_link_id($sql); //link_id获取
  20. $InitPHP_conf = InitPHP::getConfig();
  21. if($InitPHP_conf['is_debug']==true) $start = microtime();
  22. $query = $this->db->query($sql);
  23. if($InitPHP_conf['is_debug']==true) $end = microtime();
  24. //sql query debug
  25. if($InitPHP_conf['is_debug']==true){
  26. $k= isset($InitPHP_conf['sqlcontrolarr']) ? count($InitPHP_conf['sqlcontrolarr']) : 0;
  27. $InitPHP_conf['sqlcontrolarr'][$k]['sql']=$sql;
  28. $costTime=substr(($end-$start),0,7);
  29. $InitPHP_conf['sqlcontrolarr'][$k]['queryTime']=$costTime;
  30. $InitPHP_conf['sqlcontrolarr'][$k]['affectedRows']=$this->affected_rows();
  31. InitPHP::setConfig('sqlcontrolarr', $InitPHP_conf['sqlcontrolarr']);
  32. }
  33. if ($this->db->error()) {
  34. InitPHP::initError($this->db->error());
  35. }
  36. if ($is_set_default) $this->set_default_link_id(); //设置默认的link_id
  37. return $query;
  38. }
  39. /**
  40. * 结果集中的行数
  41. * DAO中使用方法:$this->dao->db->result($result, $num=1)
  42. * @param $result 结果集
  43. * @return array
  44. */
  45. public function result($result, $num=1) {
  46. return $this->db->result($result, $num);
  47. }
  48. /**
  49. * 从结果集中取得一行作为关联数组
  50. * DAO中使用方法:$this->dao->db->fetch_assoc($result)
  51. * @param $result 结果集
  52. * @return array
  53. */
  54. public function fetch_assoc($result) {
  55. return $this->db->fetch_assoc($result);
  56. }
  57. /**
  58. * 从结果集中取得列信息并作为对象返回
  59. * DAO中使用方法:$this->dao->db->fetch_fields($result)
  60. * @param $result 结果集
  61. * @return array
  62. */
  63. public function fetch_fields($result) {
  64. return $this->db->fetch_fields($result);
  65. }
  66. /**
  67. * 结果集中的行数
  68. * DAO中使用方法:$this->dao->db->num_rows($result)
  69. * @param $result 结果集
  70. * @return int
  71. */
  72. public function num_rows($result) {
  73. return $this->db->num_rows($result);
  74. }
  75. /**
  76. * 结果集中的字段数量
  77. * DAO中使用方法:$this->dao->db->num_fields($result)
  78. * @param $result 结果集
  79. * @return int
  80. */
  81. public function num_fields($result) {
  82. return $this->db->num_fields($result);
  83. }
  84. /**
  85. * 释放结果内存
  86. * DAO中使用方法:$this->dao->db->free_result($result)
  87. * @param obj $result 需要释放的对象
  88. */
  89. public function free_result($result) {
  90. return $this->db->free_result($result);
  91. }
  92. /**
  93. * 获取上一INSERT的ID值
  94. * DAO中使用方法:$this->dao->db->insert_id()
  95. * @return Int
  96. */
  97. public function insert_id() {
  98. return $this->db->insert_id();
  99. }
  100. /**
  101. * 前一次操作影响的记录数
  102. * DAO中使用方法:$this->dao->db->affected_rows()
  103. * @return int
  104. */
  105. public function affected_rows() {
  106. return $this->db->affected_rows();
  107. }
  108. /**
  109. * 关闭连接
  110. * DAO中使用方法:$this->dao->db->close()
  111. * @return bool
  112. */
  113. public function close() {
  114. return $this->db->close();
  115. }
  116. /**
  117. * 错误信息
  118. * DAO中使用方法:$this->dao->db->error()
  119. * @return string
  120. */
  121. public function error() {
  122. return $this->db->error();
  123. }
  124. /**
  125. * 开始事务操作
  126. * DAO中使用方法:$this->dao->db->transaction_start()
  127. */
  128. public function transaction_start() {
  129. $this->query("START TRANSACTION");
  130. return true;
  131. }
  132. /**
  133. * 提交事务
  134. * DAO中使用方法:$this->dao->db->transaction_commit()
  135. */
  136. public function transaction_commit() {
  137. $this->query("COMMIT");
  138. return true;
  139. }
  140. /**
  141. * 回滚事务
  142. * DAO中使用方法:$this->dao->db->transaction_rollback()
  143. */
  144. public function transaction_rollback() {
  145. $this->query("ROLLBACK");
  146. return true;
  147. }
  148. /**
  149. * SQL操作-插入一条数据
  150. * DAO中使用方法:$this->dao->db->insert($data, $table_name)
  151. * @param array $data array('key值'=>'值')
  152. * @param string $table_name 表名
  153. * @return id
  154. */
  155. public function insert($data, $table_name) {
  156. if (!is_array($data) || empty($data)) return 0;
  157. $data = $this->build_insert($data);
  158. $sql = sprintf("INSERT INTO %s %s", $table_name, $data);
  159. $result = $this->query($sql, false);
  160. if (!$result) return 0;
  161. $id = $this->insert_id();
  162. $this->set_default_link_id(); //设置默认的link_id
  163. return $id;
  164. }
  165. /**
  166. * SQL操作-插入多条数据
  167. * DAO中使用方法:$this->dao->db->insert_more($field, $data, $table_name)
  168. * @param array $field 字段
  169. * @param array $data 对应的值,array(array('test1'),array('test2'))
  170. * @param string $table_name 表名
  171. * @return id
  172. */
  173. public function insert_more($field, $data, $table_name) {
  174. if (!is_array($data) || empty($data)) return false;
  175. if (!is_array($field) || empty($field)) return false;
  176. $sql = $this->build_insertmore($field, $data);
  177. $sql = sprintf("INSERT INTO %s %s", $table_name, $sql);
  178. return $this->query($sql);
  179. }
  180. /**
  181. * SQL操作-根据主键id更新数据
  182. * DAO中使用方法:$this->dao->db->update($id, $data, $table_name, $id_key = 'id')
  183. * @param int $id 主键ID
  184. * @param array $data 参数
  185. * @param string $table_name 表名
  186. * @param string $id_key 主键名
  187. * @return bool
  188. */
  189. public function update($id, $data, $table_name, $id_key = 'id') {
  190. $id = (int) $id;
  191. if ($id < 1) return false;
  192. $data = $this->build_update($data);
  193. $where = $this->build_where(array($id_key=>$id));
  194. $sql = sprintf("UPDATE %s %s %s", $table_name, $data, $where);
  195. return $this->query($sql);
  196. }
  197. /**
  198. * SQL操作-根据字段更新数据
  199. * DAO中使用方法:$this->dao->db->update_by_field($data, $field, $table_name)
  200. * @param array $data 参数
  201. * @param array $field 字段参数
  202. * @param string $table_name 表名
  203. * @return bool
  204. */
  205. public function update_by_field($data, $field, $table_name) {
  206. if (!is_array($data) || empty($data)) return false;
  207. if (!is_array($field) || empty($field)) return false;
  208. $data = $this->build_update($data);
  209. $field = $this->build_where($field);
  210. $sql = sprintf("UPDATE %s %s %s", $table_name, $data, $field);
  211. return $this->query($sql);
  212. }
  213. /**
  214. * SQL操作-删除数据
  215. * DAO中使用方法:$this->dao->db->delete($ids, $table_name, $id_key = 'id')
  216. * @param int|array $ids 单个id或者多个id
  217. * @param string $table_name 表名
  218. * @param string $id_key 主键名
  219. * @return bool
  220. */
  221. public function delete($ids, $table_name, $id_key = 'id') {
  222. if (is_array($ids)) {
  223. $ids = $this->build_in($ids);
  224. $sql = sprintf("DELETE FROM %s WHERE %s %s", $table_name, $id_key, $ids);
  225. } else {
  226. $where = $this->build_where(array($id_key=>$ids));
  227. $sql = sprintf("DELETE FROM %s %s", $table_name, $where);
  228. }
  229. return $this->query($sql);
  230. }
  231. /**
  232. * SQL操作-通过条件语句删除数据
  233. * DAO中使用方法:$this->dao->db->delete_by_field($field, $table_name)
  234. * @param array $field 条件数组
  235. * @param string $table_name 表名
  236. * @return bool
  237. */
  238. public function delete_by_field($field, $table_name) {
  239. if (!is_array($field) || empty($field)) return false;
  240. $where = $this->build_where($field);
  241. $sql = sprintf("DELETE FROM %s %s", $table_name, $where);
  242. return $this->query($sql);
  243. }
  244. /**
  245. * SQL操作-获取单条信息
  246. * DAO中使用方法:$this->dao->db->get_one($id, $table_name, $id_key = 'id')
  247. * @param int $id 主键ID
  248. * @param string $table_name 表名
  249. * @param string $id_key 主键名称,默认id
  250. * @return array
  251. */
  252. public function get_one($id, $table_name, $id_key = 'id') {
  253. $id = (int) $id;
  254. if ($id < 1) return array();
  255. $where = $this->build_where(array($id_key=>$id));
  256. $sql = sprintf("SELECT * FROM %s %s LIMIT 1", $table_name, $where);
  257. $result = $this->query($sql, false);
  258. if (!$result) return false;
  259. $r = $this->fetch_assoc($result);
  260. $this->set_default_link_id(); //设置默认的link_id
  261. return $r;
  262. }
  263. /**
  264. * SQL操作-通过条件语句获取一条信息
  265. * DAO中使用方法:$this->dao->db->get_one_by_field($field, $table_name)
  266. * @param array $field 条件数组 array('username' => 'username')
  267. * @param string $table_name 表名
  268. * @return bool
  269. */
  270. public function get_one_by_field($field, $table_name) {
  271. if (!is_array($field) || empty($field)) return array();
  272. $where = $this->build_where($field);
  273. $sql = sprintf("SELECT * FROM %s %s LIMIT 1", $table_name, $where);
  274. $result = $this->query($sql, false);
  275. if (!$result) return false;
  276. $r = $this->fetch_assoc($result);
  277. $this->set_default_link_id(); //设置默认的link_id
  278. return $r;
  279. }
  280. /**
  281. * SQL操作-获取单条信息-sql语句方式
  282. * DAO中使用方法:$this->dao->db->get_one_sql($sql)
  283. * @param string $sql 数据库语句
  284. * @return array
  285. */
  286. public function get_one_sql($sql) {
  287. $sql = trim($sql . ' ' .$this->build_limit(1));
  288. $result = $this->query($sql, false);
  289. if (!$result) return false;
  290. $r = $this->fetch_assoc($result);
  291. $this->set_default_link_id(); //设置默认的link_id
  292. return $r;
  293. }
  294. /**
  295. * SQL操作-获取全部数据
  296. * DAO中使用方法:$this->dao->db->get_all()
  297. * @param string $table_name 表名
  298. * @param array $field 条件语句
  299. * @param int $num 分页参数
  300. * @param int $offest 获取总条数
  301. * @param int $key_id KEY值
  302. * @param string $sort 排序键
  303. * @return array array(数组数据,统计数)
  304. */
  305. public function get_all($table_name, $num = NULL, $offest = 0, $field = array(), $id_key = 'id', $sort = '') {
  306. $where = $this->build_where($field);
  307. if($num!=null){
  308. $limit = $this->build_limit($offest, $num);
  309. }else {
  310. $limit="";
  311. }
  312. $sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
  313. $result = $this->query($sql, false);
  314. if (!$result) return false;
  315. $temp = array();
  316. while ($row = $this->fetch_assoc($result)) {
  317. $temp[] = $row;
  318. }
  319. $count = $this->get_count($table_name, $field);
  320. $this->set_default_link_id(); //设置默认的link_id
  321. return array($temp, $count);
  322. }
  323. /**
  324. * SQL操作-获取所有数据
  325. * DAO中使用方法:$this->dao->db->get_all_sql($sql)
  326. * @param string $sql SQL语句
  327. * @return array
  328. */
  329. public function get_all_sql($sql) {
  330. $sql = trim($sql);
  331. $result = $this->query($sql, false);
  332. if (!$result) return false;
  333. while ($row = $this->fetch_assoc($result)) {
  334. $temp[] = $row;
  335. }
  336. $this->set_default_link_id(); //设置默认的link_id
  337. return $temp;
  338. }
  339. /**
  340. * SQL操作-获取数据总数
  341. * DAO中使用方法:$this->dao->db->get_count($table_name, $field = array())
  342. * @param string $table_name 表名
  343. * @param array $field 条件语句
  344. * @return int
  345. */
  346. public function get_count($table_name, $field = array()) {
  347. $where = $this->build_where($field);
  348. $sql = sprintf("SELECT COUNT(*) as count FROM %s %s LIMIT 1", $table_name, $where);
  349. $result = $this->query($sql, false);
  350. $result = $this->fetch_assoc($result);
  351. $this->set_default_link_id(); //设置默认的link_id
  352. return $result['count'];
  353. }
  354. }