base_db.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import abc
  2. from tool.typing import List, Union, Optional, Tuple, Dict
  3. class DBException(Exception):
  4. ...
  5. class DBDoneException(DBException):
  6. ...
  7. class DBCloseException(DBException):
  8. ...
  9. class DBBit:
  10. BIT_0 = b'\x00'
  11. BIT_1 = b'\x01'
  12. class HGSDatabase(metaclass=abc.ABCMeta):
  13. @abc.abstractmethod
  14. def __init__(self, host: str, name: str, passwd: str, port: str):
  15. self._host = str(host)
  16. self._name = str(name)
  17. self._passwd = str(passwd)
  18. if port is None:
  19. self._port = 0
  20. else:
  21. self._port = int(port)
  22. @abc.abstractmethod
  23. def close(self):
  24. """
  25. 关闭数据库, 此代码执行后任何成员函数再被调用其行为是未定义的
  26. :return:
  27. """
  28. ...
  29. @abc.abstractmethod
  30. def is_connect(self) -> bool:
  31. """
  32. :return: 是否处于连接状态
  33. """
  34. ...
  35. @abc.abstractmethod
  36. def get_cursor(self) -> any:
  37. """
  38. :return: 返回数据库游标
  39. """
  40. ...
  41. @abc.abstractmethod
  42. def search(self, columns: List[str], table: str,
  43. where: Union[str, List[str]] = None,
  44. limit: Optional[int] = None,
  45. offset: Optional[int] = None,
  46. order_by: Optional[List[Tuple[str, str]]] = None):
  47. """
  48. 执行 查询 SQL语句
  49. :param columns: 列名称
  50. :param table: 表
  51. :param where: 条件
  52. :param limit: 限制行数
  53. :param offset: 偏移
  54. :param order_by: 排序方式
  55. :return:
  56. """
  57. ...
  58. @abc.abstractmethod
  59. def insert(self, table: str, columns: list, values: Union[str, List[str]]):
  60. """
  61. 执行 插入 SQL语句, 并提交
  62. :param table: 表
  63. :param columns: 列名称
  64. :param values: 数据
  65. :return:
  66. """
  67. ...
  68. @abc.abstractmethod
  69. def delete(self, table: str, where: Union[str, List[str]] = None):
  70. """
  71. 执行 删除 SQL语句, 并提交
  72. :param table: 表
  73. :param where: 条件
  74. :return:
  75. """
  76. ...
  77. @abc.abstractmethod
  78. def update(self, table: str, kw: "Dict[str:str]", where: Union[str, List[str]] = None):
  79. """
  80. 执行 更新 SQL语句, 并提交
  81. :param table: 表
  82. :param kw: 键值对
  83. :param where: 条件
  84. :return:
  85. """
  86. ...