mysql.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import pymysql
  2. import threading
  3. import traceback
  4. from sql.base import Database, DBException, DBCloseException
  5. from typing import Optional, Union, List, Tuple, Dict
  6. class MysqlDB(Database):
  7. def __init__(self,
  8. host: Optional[str],
  9. name: Optional[str],
  10. passwd: Optional[str],
  11. port: Optional[str]):
  12. if host is None or name is None:
  13. raise DBException
  14. super(MysqlDB, self).__init__(host=host, name=name, passwd=passwd, port=port)
  15. try:
  16. self._db = pymysql.connect(user=self._name,
  17. password=self._passwd,
  18. host=self._host,
  19. port=self._port,
  20. database="HBlog")
  21. except pymysql.err.OperationalError:
  22. raise
  23. self._cursor = self._db.cursor()
  24. self._lock = threading.RLock()
  25. self.logger.info(f"MySQL({self._name}@{self._host}) connect")
  26. def close(self):
  27. if self._cursor is not None:
  28. self._cursor.close()
  29. if self._db is not None:
  30. self._db.close()
  31. self._db = None
  32. self._cursor = None
  33. self._lock = None
  34. self.logger.warning(f"MySQL({self._name}@{self._host}) connect close")
  35. def is_connect(self) -> bool:
  36. if self._cursor is None or self._db is None:
  37. return False
  38. return True
  39. def get_cursor(self) -> pymysql.cursors.Cursor:
  40. if self._cursor is None or self._db is None:
  41. raise DBCloseException
  42. return self._cursor
  43. def search(self, columns: List[str], table: str,
  44. where: Union[str, List[str]] = None,
  45. limit: Optional[int] = None,
  46. offset: Optional[int] = None,
  47. order_by: Optional[List[Tuple[str, str]]] = None,
  48. group_by: Optional[List[str]] = None,
  49. for_update: bool = False):
  50. if type(where) is list and len(where) > 0:
  51. where: str = " WHERE " + " AND ".join(f"({w})" for w in where)
  52. elif type(where) is str and len(where) > 0:
  53. where = " WHERE " + where
  54. else:
  55. where: str = ""
  56. if order_by is None:
  57. order_by: str = ""
  58. else:
  59. by = [f" {i[0]} {i[1]} " for i in order_by]
  60. order_by: str = " ORDER BY" + ", ".join(by)
  61. if limit is None or limit == 0:
  62. limit: str = ""
  63. else:
  64. limit = f" LIMIT {limit}"
  65. if offset is None:
  66. offset: str = ""
  67. else:
  68. offset = f" OFFSET {offset}"
  69. if group_by is None:
  70. group_by: str = ""
  71. else:
  72. group_by = "GROUP BY " + ", ".join(group_by)
  73. columns: str = ", ".join(columns)
  74. if for_update:
  75. for_update = "FOR UPDATE"
  76. else:
  77. for_update = ""
  78. return self.__search(f"SELECT {columns} "
  79. f"FROM {table} "
  80. f"{where} {group_by} {order_by} {limit} {offset} {for_update};")
  81. def insert(self, table: str, columns: list, values: Union[str, List[str]], not_commit: bool = False):
  82. columns: str = ", ".join(columns)
  83. if type(values) is str:
  84. values: str = f"({values})"
  85. else:
  86. values: str = ", ".join(f"{v}" for v in values)
  87. return self.__done(f"INSERT INTO {table}({columns}) VALUES {values};", not_commit=not_commit)
  88. def delete(self, table: str, where: Union[str, List[str]] = None, not_commit: bool = False):
  89. if type(where) is list and len(where) > 0:
  90. where: str = " AND ".join(f"({w})" for w in where)
  91. elif type(where) is not str or len(where) == 0: # 必须指定条件
  92. return None
  93. return self.__done(f"DELETE FROM {table} WHERE {where};", not_commit=not_commit)
  94. def update(self, table: str, kw: "Dict[str:str]", where: Union[str, List[str]] = None, not_commit: bool = False):
  95. if len(kw) == 0:
  96. return None
  97. if type(where) is list and len(where) > 0:
  98. where: str = " AND ".join(f"({w})" for w in where)
  99. elif type(where) is not str or len(where) == 0: # 必须指定条件
  100. return None
  101. kw_list = [f"{key} = {kw[key]}" for key in kw]
  102. kw_str = ", ".join(kw_list)
  103. return self.__done(f"UPDATE {table} SET {kw_str} WHERE {where};", not_commit=not_commit)
  104. def __search(self, sql) -> Union[None, pymysql.cursors.Cursor]:
  105. if self._cursor is None or self._db is None:
  106. raise DBCloseException
  107. try:
  108. self._lock.acquire() # 上锁
  109. self._cursor.execute(sql)
  110. except pymysql.MySQLError:
  111. self.logger.error(f"MySQL({self._name}@{self._host}) SQL {sql} error", exc_info=True)
  112. return None
  113. finally:
  114. self._lock.release() # 释放锁
  115. return self._cursor
  116. def __done(self, sql, not_commit: bool = False) -> Union[None, pymysql.cursors.Cursor]:
  117. if self._cursor is None or self._db is None:
  118. raise DBCloseException
  119. try:
  120. self._lock.acquire()
  121. self._cursor.execute(sql)
  122. except pymysql.MySQLError:
  123. self._db.rollback()
  124. self.logger.error(f"MySQL({self._name}@{self._host}) SQL {sql} error", exc_info=True)
  125. return None
  126. finally:
  127. if not not_commit:
  128. self._db.commit()
  129. self._lock.release()
  130. return self._cursor
  131. def commit(self):
  132. try:
  133. self._lock.acquire()
  134. self._db.commit()
  135. except pymysql.MySQLError:
  136. self.logger.error(f"MySQL({self._name}@{self._host}) commit error", exec_info=True)
  137. finally:
  138. self._lock.release()