mysql_helper.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import pymysql
  2. from typing import Dict, List, Any, Optional, Union, Tuple
  3. from contextlib import contextmanager
  4. from loguru import logger
  5. from .mysql_pool import mysql_pool
  6. class MySQLHelper:
  7. """MySQL数据库操作助手类"""
  8. def __init__(self):
  9. self.pool = mysql_pool
  10. @contextmanager
  11. def get_cursor(self, connection: pymysql.Connection = None):
  12. """获取游标的上下文管理器"""
  13. if connection:
  14. cursor = connection.cursor()
  15. try:
  16. yield cursor
  17. finally:
  18. cursor.close()
  19. else:
  20. with self.pool.get_connection_context() as conn:
  21. cursor = conn.cursor()
  22. try:
  23. yield cursor
  24. finally:
  25. cursor.close()
  26. def execute_query(self, sql: str, params: Optional[Union[tuple, dict]] = None,
  27. connection: pymysql.Connection = None) -> List[Dict[str, Any]]:
  28. """
  29. 执行查询操作
  30. Args:
  31. sql: SQL语句
  32. params: 参数
  33. connection: 数据库连接(可选,用于事务)
  34. Returns:
  35. 查询结果列表
  36. """
  37. try:
  38. with self.get_cursor(connection) as cursor:
  39. cursor.execute(sql, params)
  40. return cursor.fetchall()
  41. except Exception as e:
  42. logger.error(f"查询执行失败: {sql}, 参数: {params}, 错误: {e}")
  43. raise
  44. def execute_one(self, sql: str, params: Optional[Union[tuple, dict]] = None,
  45. connection: pymysql.Connection = None) -> Optional[Dict[str, Any]]:
  46. """
  47. 执行查询操作,返回单条记录
  48. Args:
  49. sql: SQL语句
  50. params: 参数
  51. connection: 数据库连接(可选,用于事务)
  52. Returns:
  53. 单条记录或None
  54. """
  55. try:
  56. with self.get_cursor(connection) as cursor:
  57. cursor.execute(sql, params)
  58. return cursor.fetchone()
  59. except Exception as e:
  60. logger.error(f"查询执行失败: {sql}, 参数: {params}, 错误: {e}")
  61. raise
  62. def execute_update(self, sql: str, params: Optional[Union[tuple, dict]] = None,
  63. connection: pymysql.Connection = None) -> int:
  64. """
  65. 执行更新操作(INSERT、UPDATE、DELETE)
  66. Args:
  67. sql: SQL语句
  68. params: 参数
  69. connection: 数据库连接(可选,用于事务)
  70. Returns:
  71. 影响的行数
  72. """
  73. try:
  74. with self.get_cursor(connection) as cursor:
  75. affected_rows = cursor.execute(sql, params)
  76. if not connection: # 如果没有传入连接,自动提交
  77. cursor.connection.commit()
  78. return affected_rows
  79. except Exception as e:
  80. if not connection:
  81. cursor.connection.rollback()
  82. logger.error(f"更新执行失败: {sql}, 参数: {params}, 错误: {e}")
  83. raise
  84. def execute_many(self, sql: str, params_list: List[Union[tuple, dict]],
  85. connection: pymysql.Connection = None) -> int:
  86. """
  87. 批量执行操作
  88. Args:
  89. sql: SQL语句
  90. params_list: 参数列表
  91. connection: 数据库连接(可选,用于事务)
  92. Returns:
  93. 影响的总行数
  94. """
  95. try:
  96. with self.get_cursor(connection) as cursor:
  97. affected_rows = cursor.executemany(sql, params_list)
  98. if not connection:
  99. cursor.connection.commit()
  100. return affected_rows
  101. except Exception as e:
  102. if not connection:
  103. cursor.connection.rollback()
  104. logger.error(f"批量执行失败: {sql}, 参数: {params_list}, 错误: {e}")
  105. raise
  106. def insert(self, table: str, data: Dict[str, Any],
  107. connection: pymysql.Connection = None) -> int:
  108. """
  109. 插入数据
  110. Args:
  111. table: 表名
  112. data: 数据字典
  113. connection: 数据库连接(可选,用于事务)
  114. Returns:
  115. 插入的记录ID
  116. """
  117. if not data:
  118. raise ValueError("插入数据不能为空")
  119. columns = list(data.keys())
  120. placeholders = ', '.join(['%s'] * len(columns))
  121. sql = f"INSERT INTO {table} ({', '.join(columns)}) VALUES ({placeholders})"
  122. params = tuple(data.values())
  123. try:
  124. with self.get_cursor(connection) as cursor:
  125. cursor.execute(sql, params)
  126. if not connection:
  127. cursor.connection.commit()
  128. return cursor.lastrowid
  129. except Exception as e:
  130. if not connection:
  131. cursor.connection.rollback()
  132. logger.error(f"插入失败: {sql}, 参数: {params}, 错误: {e}")
  133. raise
  134. def insert_many(self, table: str, data_list: List[Dict[str, Any]],
  135. connection: pymysql.Connection = None) -> int:
  136. """
  137. 批量插入数据
  138. Args:
  139. table: 表名
  140. data_list: 数据列表
  141. connection: 数据库连接(可选,用于事务)
  142. Returns:
  143. 影响的行数
  144. """
  145. if not data_list:
  146. raise ValueError("插入数据不能为空")
  147. # 使用第一条记录的键作为列名
  148. columns = list(data_list[0].keys())
  149. placeholders = ', '.join(['%s'] * len(columns))
  150. sql = f"INSERT INTO {table} ({', '.join(columns)}) VALUES ({placeholders})"
  151. # 构建参数列表
  152. params_list = [tuple(data[col] for col in columns) for data in data_list]
  153. return self.execute_many(sql, params_list, connection)
  154. def update(self, table: str, data: Dict[str, Any], where: str,
  155. where_params: Optional[Union[tuple, dict]] = None,
  156. connection: pymysql.Connection = None) -> int:
  157. """
  158. 更新数据
  159. Args:
  160. table: 表名
  161. data: 更新数据字典
  162. where: WHERE条件
  163. where_params: WHERE条件参数
  164. connection: 数据库连接(可选,用于事务)
  165. Returns:
  166. 影响的行数
  167. """
  168. if not data:
  169. raise ValueError("更新数据不能为空")
  170. set_clause = ', '.join([f"{col} = %s" for col in data.keys()])
  171. sql = f"UPDATE {table} SET {set_clause} WHERE {where}"
  172. # 合并参数
  173. params = list(data.values())
  174. if where_params:
  175. if isinstance(where_params, (tuple, list)):
  176. params.extend(where_params)
  177. elif isinstance(where_params, dict):
  178. params.extend(where_params.values())
  179. return self.execute_update(sql, tuple(params), connection)
  180. def delete(self, table: str, where: str,
  181. where_params: Optional[Union[tuple, dict]] = None,
  182. connection: pymysql.Connection = None) -> int:
  183. """
  184. 删除数据
  185. Args:
  186. table: 表名
  187. where: WHERE条件
  188. where_params: WHERE条件参数
  189. connection: 数据库连接(可选,用于事务)
  190. Returns:
  191. 影响的行数
  192. """
  193. sql = f"DELETE FROM {table} WHERE {where}"
  194. return self.execute_update(sql, where_params, connection)
  195. def select(self, table: str, columns: str = "*", where: str = "",
  196. where_params: Optional[Union[tuple, dict]] = None,
  197. order_by: str = "", limit: Optional[int] = None,
  198. connection: pymysql.Connection = None) -> List[Dict[str, Any]]:
  199. """
  200. 查询数据
  201. Args:
  202. table: 表名
  203. columns: 查询列,默认为*
  204. where: WHERE条件
  205. where_params: WHERE条件参数
  206. order_by: 排序条件
  207. limit: 限制数量
  208. connection: 数据库连接(可选,用于事务)
  209. Returns:
  210. 查询结果列表
  211. """
  212. sql = f"SELECT {columns} FROM {table}"
  213. if where:
  214. sql += f" WHERE {where}"
  215. if order_by:
  216. sql += f" ORDER BY {order_by}"
  217. if limit:
  218. sql += f" LIMIT {limit}"
  219. return self.execute_query(sql, where_params, connection)
  220. def select_one(self, table: str, columns: str = "*", where: str = "",
  221. where_params: Optional[Union[tuple, dict]] = None,
  222. connection: pymysql.Connection = None) -> Optional[Dict[str, Any]]:
  223. """
  224. 查询单条数据
  225. Args:
  226. table: 表名
  227. columns: 查询列,默认为*
  228. where: WHERE条件
  229. where_params: WHERE条件参数
  230. connection: 数据库连接(可选,用于事务)
  231. Returns:
  232. 单条记录或None
  233. """
  234. sql = f"SELECT {columns} FROM {table}"
  235. if where:
  236. sql += f" WHERE {where}"
  237. sql += " LIMIT 1"
  238. return self.execute_one(sql, where_params, connection)
  239. def count(self, table: str, where: str = "",
  240. where_params: Optional[Union[tuple, dict]] = None,
  241. connection: pymysql.Connection = None) -> int:
  242. """
  243. 统计记录数
  244. Args:
  245. table: 表名
  246. where: WHERE条件
  247. where_params: WHERE条件参数
  248. connection: 数据库连接(可选,用于事务)
  249. Returns:
  250. 记录数
  251. """
  252. sql = f"SELECT COUNT(*) as count FROM {table}"
  253. if where:
  254. sql += f" WHERE {where}"
  255. result = self.execute_one(sql, where_params, connection)
  256. return result['count'] if result else 0
  257. def exists(self, table: str, where: str,
  258. where_params: Optional[Union[tuple, dict]] = None,
  259. connection: pymysql.Connection = None) -> bool:
  260. """
  261. 检查记录是否存在
  262. Args:
  263. table: 表名
  264. where: WHERE条件
  265. where_params: WHERE条件参数
  266. connection: 数据库连接(可选,用于事务)
  267. Returns:
  268. 是否存在
  269. """
  270. return self.count(table, where, where_params, connection) > 0
  271. # 全局实例
  272. mysql_helper = MySQLHelper()