MySQLConnection.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ = 'MySQL数据库连接池对象'
  5. @Time : 2020/9/26 18:36
  6. @Author : Kenny-PC
  7. @Software: PyCharm
  8. # code is far away from bugs with the god animal protecting
  9. I love animals. They taste delicious.
  10. ┏┓ ┏┓
  11. ┏┛┻━━━┛┻┓
  12. ┃ ☃ ┃
  13. ┃ ┳┛ ┗┳ ┃
  14. ┃ ┻ ┃
  15. ┗━┓ ┏━┛
  16. ┃ ┗━━━┓
  17. ┃ 神兽保佑 ┣┓
  18. ┃ 永无BUG! ┏┛
  19. ┗┓┓┏━┳┓┏┛
  20. ┃┫┫ ┃┫┫
  21. ┗┻┛ ┗┻┛
  22. """
  23. import configparser
  24. import logging
  25. import os
  26. import pymysql
  27. from DBUtils.PooledDB import PooledDB
  28. class MySQLConnection():
  29. """
  30. 数据库连接池代理对象
  31. 查询参数主要有两种类型
  32. 第一种:传入元祖类型,例如(12,13),这种方式主要是替代SQL语句中的%s展位符号
  33. 第二种: 传入字典类型,例如{"id":13},此时我们的SQL语句需要使用键来代替展位符,例如:%(name)s
  34. """
  35. def __init__(self):
  36. # 获取当前文件所在目录的上一级目录
  37. parent_dir_path = os.path.dirname(os.path.abspath('..'))
  38. db_config_path = parent_dir_path + '/dgp/tests/conf/db_config.ini'
  39. # print('数据库配置文件:', db_config_path)
  40. # 读取数据库配置信息
  41. config = configparser.ConfigParser()
  42. config.read(db_config_path, encoding='UTF-8')
  43. sections = config.sections()
  44. mysql_config_section = 'mysql_config'
  45. if mysql_config_section not in sections:
  46. print('数据库配置文件没有名为 mysql_config 的 section')
  47. exit()
  48. # 读取相关属性
  49. maxconnections = config.get(mysql_config_section, "maxconnections")
  50. mincached = config.get(mysql_config_section, "mincached")
  51. maxcached = config.get(mysql_config_section, "maxcached")
  52. host = config.get(mysql_config_section, "host")
  53. port = config.get(mysql_config_section, "port")
  54. user = config.get(mysql_config_section, "user")
  55. password = config.get(mysql_config_section, "password")
  56. database = config.get(mysql_config_section, "database")
  57. databasePooled = PooledDB(creator=pymysql,
  58. maxconnections=int(maxconnections),
  59. mincached=int(mincached),
  60. maxcached=int(maxcached),
  61. blocking=True,
  62. cursorclass=pymysql.cursors.DictCursor,
  63. host=host,
  64. port=int(port),
  65. user=user,
  66. password=password,
  67. database=database)
  68. self.connect = databasePooled.connection()
  69. self.cursor = self.connect.cursor()
  70. logging.debug("获取数据库连接对象成功,连接池对象:{}".format(str(self.connect)))
  71. def execute(self, sql, param=None):
  72. """
  73. 基础更新、插入、删除操作
  74. :param sql:
  75. :param param:
  76. :return: 受影响的行数
  77. """
  78. ret = None
  79. try:
  80. if param == None:
  81. ret = self.cursor.execute(sql)
  82. else:
  83. ret = self.cursor.execute(sql, param)
  84. except TypeError as te:
  85. logging.debug("类型错误")
  86. logging.exception(te)
  87. return ret
  88. def query(self, sql, param=None):
  89. """
  90. 查询数据库
  91. :param sql: 查询SQL语句
  92. :param param: 参数
  93. :return: 返回集合
  94. """
  95. self.cursor.execute(sql, param)
  96. result = self.cursor.fetchall()
  97. return result
  98. def queryOne(self, sql, param=None):
  99. """
  100. 查询数据返回第一条
  101. :param sql: 查询SQL语句
  102. :param param: 参数
  103. :return: 返回第一条数据的字典
  104. """
  105. result = self.query(sql, param)
  106. if result:
  107. return result[0]
  108. else:
  109. return None
  110. def listByPage(self, sql, current_page, page_size, param=None):
  111. """
  112. 分页查询当前表格数据
  113. :param sql: 查询SQL语句
  114. :param current_page: 当前页码
  115. :param page_size: 页码大小
  116. :param param:参数
  117. :return:
  118. """
  119. countSQL = "select count(*) ct from (" + sql + ") tmp "
  120. logging.debug("统计SQL:{}".format(sql))
  121. countNum = self.count(countSQL, param)
  122. offset = (current_page - 1) * page_size
  123. totalPage = int(countNum / page_size)
  124. if countNum % page_size > 0:
  125. totalPage = totalPage + 1
  126. pagination = {"current_page": current_page, "page_size": page_size, "count": countNum, "total_page": totalPage}
  127. querySql = "select * from (" + sql + ") tmp limit %s,%s"
  128. logging.debug("查询SQL:{}".format(querySql))
  129. # 判断是否有参数
  130. if param == None:
  131. # 无参数
  132. pagination["data"] = self.query(querySql, (offset, page_size))
  133. else:
  134. # 有参数的情况,此时需要判断参数是元祖还是字典
  135. if isinstance(param, dict):
  136. # 字典的情况,因此需要添加字典
  137. querySql = "select * from (" + sql + ") tmp limit %(tmp_offset)s,%(tmp_pageSize)s"
  138. param["tmp_offset"] = offset
  139. param["tmp_pageSize"] = page_size
  140. pagination["data"] = self.query(querySql, param)
  141. elif isinstance(param, tuple):
  142. # 元祖的方式
  143. listtp = list(param)
  144. listtp.append(offset)
  145. listtp.append(page_size)
  146. pagination["data"] = self.query(querySql, tuple(listtp))
  147. else:
  148. # 基础类型
  149. listtp = []
  150. listtp.append(param)
  151. listtp.append(offset)
  152. listtp.append(page_size)
  153. pagination["data"] = self.query(querySql, tuple(listtp))
  154. return pagination
  155. def count(self, sql, param=None):
  156. """
  157. 统计当前表记录行数
  158. :param sql: 统计SQL语句
  159. :param param: 参数
  160. :return: 当前记录行
  161. """
  162. ret = self.queryOne(sql, param)
  163. count = None
  164. if ret:
  165. for k, v in ret.items():
  166. count = v
  167. return count
  168. def insert(self, sql, param=None):
  169. """
  170. 数据库插入
  171. :param sql: SQL语句
  172. :param param: 参数
  173. :return: 受影响的行数
  174. """
  175. return self.execute(sql, param)
  176. def update(self, sql, param=None):
  177. """
  178. 更新操作
  179. :param sql: SQL语句
  180. :param param: 参数
  181. :return: 受影响的行数
  182. """
  183. return self.execute(sql, param)
  184. def delete(self, sql, param=None):
  185. """
  186. 删除操作
  187. :param sql: 删除SQL语句
  188. :param param: 参数
  189. :return: 受影响的行数
  190. """
  191. return self.execute(sql, param)
  192. def batch(self, sql, param=None):
  193. """
  194. 批量插入
  195. :param sql: 插入SQL语句
  196. :param param: 参数
  197. :return: 受影响的行数
  198. """
  199. return self.cursor.executemany(sql, param)
  200. def commit(self, param=None):
  201. """
  202. 提交数据库
  203. :param param:
  204. :return:
  205. """
  206. if param == None:
  207. self.connect.commit()
  208. else:
  209. self.connect.rollback()
  210. def close(self):
  211. """
  212. 关闭数据库连接
  213. :return:
  214. """
  215. if self.cursor:
  216. self.cursor.close()
  217. if self.connect:
  218. self.connect.close()
  219. logging.debug("释放数据库连接")
  220. return None