From 31afa0a4c9505c77a5deb6854ffcf8a85bb21694 Mon Sep 17 00:00:00 2001 From: EXP <289065406@qq.com> Date: Fri, 20 Sep 2024 23:34:44 +0800 Subject: [PATCH] =?UTF-8?q?1.1.6=20=E4=BF=AE=E6=AD=A3=20dbtype=20=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.py | 2 +- src/pypdm/assist/cfg.py | 5 +++-- src/pypdm/dao/_base.py | 21 ++++++++++----------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/setup.py b/setup.py index 8518c37..4682f5e 100644 --- a/setup.py +++ b/setup.py @@ -37,7 +37,7 @@ # For a discussion on single-sourcing the version across setup.py and the # project code, see # https://packaging.python.org/en/latest/single_source_version.html - version='1.1.5', # Required + version='1.1.6', # Required # This is a one-line description or tagline of what your project does. This # corresponds to the "Summary" metadata field: diff --git a/src/pypdm/assist/cfg.py b/src/pypdm/assist/cfg.py index 315d228..8c7b8e5 100644 --- a/src/pypdm/assist/cfg.py +++ b/src/pypdm/assist/cfg.py @@ -59,6 +59,7 @@ def __repr__(self) : # DAO: @{table_name} # ------------------------------- +from pypdm.assist.cfg import MYSQL from pypdm.dao._base import BaseDao from bean.@{table_name} import @{TableName} @@ -72,8 +73,8 @@ class @{TableName}Dao(BaseDao) : SQL_UPDATE = '@{update}' SQL_SELECT = '@{select}' - def __init__(self) : - BaseDao.__init__(self) + def __init__(self, dbtype=MYSQL) : + BaseDao.__init__(self, dbtype) def _to_bean(self, row) : bean = None diff --git a/src/pypdm/dao/_base.py b/src/pypdm/dao/_base.py index 7f1e125..c2adc15 100644 --- a/src/pypdm/dao/_base.py +++ b/src/pypdm/dao/_base.py @@ -9,7 +9,7 @@ from color_log.clog import log -class BaseDao: +class BaseDao : """ Dao 基类 """ @@ -24,8 +24,8 @@ class BaseDao: SQL_SELECT = "" CHARSET = "utf8" - def __init__(self): - pass + def __init__(self, dbtype=MYSQL): + self.dbtype = dbtype def count(self, conn): @@ -54,7 +54,7 @@ def truncate(self, conn): is_ok = False try: cursor = conn.cursor() - cursor.execute(self.SQL_TRUNCATE if conn.dbtype() == MYSQL else self.SQL_DELETE) + cursor.execute(self.SQL_TRUNCATE if self.dbtype == MYSQL else self.SQL_DELETE) conn.commit() cursor.close() is_ok = True @@ -117,7 +117,7 @@ def delete(self, conn, wheres={}): is_ok = False try: cursor = conn.cursor() - sql = self._append(conn.dbtype(), self.SQL_DELETE, wheres.keys()) + sql = self._append(self.SQL_DELETE, wheres.keys()) cursor.execute(sql, list(wheres.values())) conn.commit() cursor.close() @@ -137,7 +137,7 @@ def update(self, conn, bean): is_ok = False try: cursor = conn.cursor() - sql = self._append(conn.dbtype(), self.SQL_UPDATE, [bean.i_id + ' = ']) + sql = self._append(self.SQL_UPDATE, [bean.i_id + ' = ']) params = bean.params() + (bean.id,) cursor.execute(sql, params) conn.commit() @@ -167,7 +167,7 @@ def query_some(self, conn, wheres={}): beans = [] try: cursor = conn.cursor() - sql = self._append(conn.dbtype(), self.SQL_SELECT, wheres.keys()) + sql = self._append(self.SQL_SELECT, wheres.keys()) cursor.execute(sql, list(wheres.values())) rows = cursor.fetchall() for row in rows: @@ -189,7 +189,7 @@ def query_one(self, conn, wheres={}): bean = None try: cursor = conn.cursor() - sql = self._append(conn.dbtype(), self.SQL_SELECT, wheres.keys()) + sql = self._append(self.SQL_SELECT, wheres.keys()) cursor.execute(sql, list(wheres.values())) row = cursor.fetchone() bean = self._to_bean(row) @@ -200,15 +200,14 @@ def query_one(self, conn, wheres={}): return bean - def _append(self, dbtype, sql, keys): + def _append(self, sql, keys): """ 追加 where 条件到 sql, 条件之间只为 and 关系(目的只是支持简单的数据库操作) - :param dbtype: 数据库类型 :param sql: SQL 语句 :param keys: 条件键值集合, 要求键值包含操作符,如: [ 'column1 like', 'column2 =' ] :return: 追加 where 条件后的 sql """ - placeholder = '%s' if dbtype == MYSQL else '?' + placeholder = '%s' if self.dbtype == MYSQL else '?' _sql = sql if keys: for key in keys: