DatabaseModule.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * @name eolinker ams open source,eolinker开源版本
  4. * @link https://www.eolinker.com/
  5. * @package eolinker
  6. * @author www.eolinker.com 广州银云信息科技有限公司 ©2015-2018
  7. * eoLinker是目前全球领先、国内最大的在线API接口管理平台,提供自动生成API文档、API自动化测试、Mock测试、团队协作等功能,旨在解决由于前后端分离导致的开发效率低下问题。
  8. * 如在使用的过程中有任何问题,欢迎加入用户讨论群进行反馈,我们将会以最快的速度,最好的服务态度为您解决问题。
  9. *
  10. * eoLinker AMS开源版的开源协议遵循Apache License 2.0,如需获取最新的eolinker开源版以及相关资讯,请访问:https://www.eolinker.com/#/os/download
  11. *
  12. * 官方网站:https://www.eolinker.com/
  13. * 官方博客以及社区:http://blog.eolinker.com/
  14. * 使用教程以及帮助:http://help.eolinker.com/
  15. * 商务合作邮箱:market@eolinker.com
  16. * 用户讨论QQ群:284421832
  17. */
  18. namespace RTP\Module;
  19. class DatabaseModule
  20. {
  21. private static $instance;
  22. private static $db_con;
  23. private $db_history;
  24. //上一次操作结果
  25. private $last_result;
  26. private $last_sql;
  27. /**
  28. * 获取实例
  29. */
  30. public static function getInstance()
  31. {
  32. //如果已经含有一个实例则直接返回实例
  33. if (!is_null(self::$instance))
  34. {
  35. return self::$instance;
  36. }
  37. else
  38. {
  39. //如果没有实例则新建
  40. return self::getNewInstance();
  41. }
  42. }
  43. /**
  44. * 获取一个新的实例
  45. */
  46. public static function getNewInstance()
  47. {
  48. self::$instance = null;
  49. self::$instance = new self;
  50. return self::$instance;
  51. }
  52. /**
  53. * 创建对象时自动连接数据库
  54. */
  55. protected function __construct()
  56. {
  57. self::connect();
  58. }
  59. /**
  60. * 销毁对象时自动断开数据库连接
  61. */
  62. function __destruct()
  63. {
  64. self::close();
  65. }
  66. /**
  67. * 连接主机
  68. */
  69. private function connect()
  70. {
  71. $conInfo = DB_TYPE . ':host=' . DB_URL . ';port=' . DB_PORT . ';dbname=' . DB_NAME . ';charset=utf8';
  72. //是否保持持久化链接
  73. if (DB_PERSISTENT_CONNECTION)
  74. {
  75. $option = array(
  76. \PDO::MYSQL_ATTR_INIT_COMMAND => "set names 'utf8'",
  77. \PDO::ATTR_PERSISTENT => TRUE,
  78. \PDO::ATTR_EMULATE_PREPARES => FALSE,
  79. \PDO::ATTR_STRINGIFY_FETCHES => FALSE
  80. );
  81. }
  82. else
  83. {
  84. $option = array(
  85. \PDO::MYSQL_ATTR_INIT_COMMAND => "set names 'utf8'",
  86. \PDO::ATTR_EMULATE_PREPARES => FALSE,
  87. \PDO::ATTR_STRINGIFY_FETCHES => FALSE
  88. );
  89. }
  90. //尝试连接数据库
  91. try
  92. {
  93. self::$db_con = new \PDO($conInfo, DB_USER, DB_PASSWORD, $option);
  94. }
  95. catch(\PDOException $e)
  96. {
  97. if (DEBUG)
  98. print_r($e -> getMessage());
  99. exit ;
  100. }
  101. }
  102. /**
  103. * 关闭主机连接
  104. */
  105. public function close()
  106. {
  107. self::$db_con = NULL;
  108. self::$instance = NULL;
  109. }
  110. /**
  111. * 执行无返回值的数据库操作并且返回受影响的记录条数
  112. */
  113. public function execute($sql)
  114. {
  115. $this -> last_sql = $sql;
  116. $result = self::$db_con -> exec($sql);
  117. $this -> getError();
  118. return $result;
  119. }
  120. /**
  121. * 执行操作并返回一条数据
  122. */
  123. public function query($sql)
  124. {
  125. $this -> last_sql = $sql;
  126. $this -> db_history = self::$db_con -> query($sql);
  127. $this -> getError();
  128. $this -> last_result = $this -> db_history -> fetch(\PDO::FETCH_ASSOC);
  129. return $this -> last_result;
  130. }
  131. /**
  132. * 执行操作并返回多条数据(如果可能)
  133. */
  134. public function queryAll($sql)
  135. {
  136. $this -> last_sql = $sql;
  137. $this -> db_history = self::$db_con -> query($sql);
  138. $this -> getError();
  139. $this -> last_result = $this -> db_history -> fetchAll(\PDO::FETCH_ASSOC);
  140. return $this -> last_result;
  141. }
  142. /**
  143. * prepare方式执行操作,返回一条数据,防止sql注入
  144. */
  145. public function prepareExecute($sql, $params = NULL)
  146. {
  147. $this -> last_sql = $sql;
  148. $this -> db_history = self::$db_con -> prepare($sql);
  149. $this -> getError();
  150. if (is_null($params))
  151. {
  152. $this -> db_history -> execute();
  153. }
  154. else
  155. {
  156. $this -> db_history -> execute($params);
  157. }
  158. $this -> getError();
  159. $this -> last_result = $this -> db_history -> fetch(\PDO::FETCH_ASSOC);
  160. return $this -> last_result;
  161. }
  162. /**
  163. * prepare方式执行操作,返回多条数据(如果可能),防止sql注入
  164. */
  165. public function prepareExecuteAll($sql, $params = NULL)
  166. {
  167. $this -> last_sql = $sql;
  168. $this -> db_history = self::$db_con -> prepare($sql);
  169. $this -> getError();
  170. if (is_null($params))
  171. {
  172. $this -> db_history -> execute();
  173. }
  174. else
  175. {
  176. $this -> db_history -> execute($params);
  177. }
  178. $this -> getError();
  179. $this -> last_result = $this -> db_history -> fetchAll(\PDO::FETCH_ASSOC);
  180. return $this -> last_result;
  181. }
  182. /**
  183. * prepare方式,以新的参数重新执行一次查询,返回一条数据
  184. */
  185. public function prepareRexecute($params)
  186. {
  187. $this -> db_history -> execute($params);
  188. $this -> getError();
  189. $this -> last_result = $this -> db_history -> fetch(\PDO::FETCH_ASSOC);
  190. return $this -> last_result;
  191. }
  192. /**
  193. * prepare方式,以新的参数重新执行一次查询,返回多条数据(如果可能)
  194. */
  195. public function prepareRexecuteAll($params)
  196. {
  197. $this -> db_history -> execute($params);
  198. $this -> getError();
  199. $this -> last_result = $this -> db_history -> fetchAll(\PDO::FETCH_ASSOC);
  200. return $this -> last_result;
  201. }
  202. /**
  203. * 获取上一次操作影响的行数
  204. */
  205. public function getAffectRow()
  206. {
  207. if (is_null($this -> db_history))
  208. {
  209. return 0;
  210. }
  211. else
  212. {
  213. return $this -> db_history -> rowCount();
  214. }
  215. }
  216. /**
  217. * 获取最后执行的SQL语句
  218. */
  219. public function getLastSQL()
  220. {
  221. return $this -> last_sql;
  222. }
  223. /**
  224. * 获取最后插入行的ID或序列值
  225. */
  226. public function getLastInsertID()
  227. {
  228. return self::$db_con -> lastInsertId();
  229. }
  230. /**
  231. * 获取错误信息
  232. */
  233. public function getError()
  234. {
  235. $result = self::$db_con -> errorInfo();
  236. if (DEBUG)
  237. {
  238. if ($result[0] != 00000)
  239. {
  240. $error = json_encode(self::$db_con -> errorInfo());
  241. throw new ExceptionModule(12000, "database error in:$error");
  242. }
  243. }
  244. else
  245. {
  246. if ($result[0] != 00000)
  247. {
  248. return FALSE;
  249. }
  250. else
  251. return TRUE;
  252. }
  253. }
  254. /**
  255. * 开始事务
  256. */
  257. public function beginTransaction()
  258. {
  259. self::$db_con -> beginTransaction();
  260. }
  261. /**
  262. * 回滚事务
  263. */
  264. public function rollback()
  265. {
  266. self::$db_con -> rollback();
  267. }
  268. /**
  269. * 提交事务
  270. */
  271. public function commit()
  272. {
  273. self::$db_con -> commit();
  274. }
  275. }
  276. ?>