base.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import abc
  2. from typing import List, Dict, Tuple, Union, Optional
  3. import logging.handlers
  4. import logging
  5. from configure import conf
  6. import os
  7. class DBException(Exception):
  8. ...
  9. class DBDoneException(DBException):
  10. ...
  11. class DBCloseException(DBException):
  12. ...
  13. class DBBit:
  14. BIT_0 = b'\x00'
  15. BIT_1 = b'\x01'
  16. class Database(metaclass=abc.ABCMeta):
  17. @abc.abstractmethod
  18. def __init__(self, host: str, name: str, passwd: str, port: str):
  19. self._host = str(host)
  20. self._name = str(name)
  21. self._passwd = str(passwd)
  22. if port is None:
  23. self._port = 3306
  24. else:
  25. self._port = int(port)
  26. self.logger = logging.getLogger("main.database")
  27. self.logger.setLevel(conf["LOG_LEVEL"])
  28. if len(conf["LOG_HOME"]) > 0:
  29. handle = logging.handlers.TimedRotatingFileHandler(
  30. os.path.join(conf["LOG_HOME"], f"mysql-{name}@{host}.log"), backupCount=10)
  31. handle.setFormatter(logging.Formatter(conf["LOG_FORMAT"]))
  32. self.logger.addHandler(handle)
  33. @abc.abstractmethod
  34. def search(self, sql: str, *args, not_commit: bool = False):
  35. """
  36. 执行 查询 SQL语句
  37. :parm sql: SQL语句
  38. :return:
  39. """
  40. ...
  41. @abc.abstractmethod
  42. def insert(self, sql: str, *args, not_commit: bool = False):
  43. """
  44. 执行 插入 SQL语句, 并提交
  45. :parm sql: SQL语句
  46. :return:
  47. """
  48. ...
  49. @abc.abstractmethod
  50. def delete(self, sql: str, *args, not_commit: bool = False):
  51. """
  52. 执行 删除 SQL语句, 并提交
  53. :parm sql: SQL语句
  54. :return:
  55. """
  56. ...
  57. @abc.abstractmethod
  58. def update(self, sql: str, *args, not_commit: bool = False):
  59. """
  60. 执行 更新 SQL语句, 并提交
  61. :parm sql: SQL语句
  62. :return:
  63. """
  64. ...