Explorar o código

feat: 更新MysqlDB依赖

SongZihuan %!s(int64=3) %!d(string=hai) anos
pai
achega
d6a6f12044
Modificáronse 1 ficheiros con 68 adicións e 1 borrados
  1. 68 1
      sql/db.py

+ 68 - 1
sql/db.py

@@ -1,3 +1,4 @@
+import abc
 import pymysql
 import threading
 from conf import MYSQL_URL, MYSQL_NAME, MYSQL_PASSWORD
@@ -25,7 +26,70 @@ class DBBit:
     BIT_1 = b'\x01'
 
 
-class DB:
+class Database(metaclass=abc.ABCMeta):
+    @abc.abstractmethod
+    def __init__(self, host: str, name: str, passwd: str):
+        pass
+
+    @abc.abstractmethod
+    def close(self):
+        """
+        关闭数据库, 此代码执行后任何成员函数再被调用其行为是未定义的
+        :return:
+        """
+        ...
+
+    @abc.abstractmethod
+    def is_connect(self) -> bool:
+        """
+        :return: 是否处于连接状态
+        """
+        ...
+
+    @abc.abstractmethod
+    def get_cursor(self) -> any:
+        """
+        :return: 返回数据库游标
+        """
+        ...
+
+    @abc.abstractmethod
+    def search(self, sql) -> any:
+        """
+        执行SQL语句, 仅SELECT
+        :param sql: SELECT的SQL语句
+        :return: 游标
+        """
+        ...
+
+    @abc.abstractmethod
+    def done(self, sql) -> any:
+        """
+        执行SQL语句, 并提交
+        :param sql: SQL语句
+        :return: 游标
+        """
+        ...
+
+    @abc.abstractmethod
+    def done_(self, sql) -> any:
+        """
+        执行SQL语句, 但暂时不提交
+        :param sql: SQL语句
+        :return: 游标
+        """
+        ...
+
+    @abc.abstractmethod
+    def done_commit(self):
+        """
+        提交由 done_ 执行的SQL语句
+        :return:
+        """
+        ...
+
+
+class MysqlDB(Database):
     def __init__(self, host: str = MYSQL_URL, name: str = MYSQL_NAME, passwd: str = MYSQL_PASSWORD):
         try:
             self._db = pymysql.connect(user=name, password=passwd, host=host, database="hgssystem")
@@ -104,6 +168,9 @@ class DB:
             self._lock.release()
 
 
+DB = MysqlDB  # DB使用MySQL
+
+
 def search_from_garbage_checker_user(columns, where, db: DB):
     if len(where) > 0:
         where = f"WHERE {where} "