Skip to content

Commit

Permalink
1.1.6 修正 dbtype 错误
Browse files Browse the repository at this point in the history
  • Loading branch information
lyy289065406 committed Sep 20, 2024
1 parent 379a8b3 commit 31afa0a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions src/pypdm/assist/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down
21 changes: 10 additions & 11 deletions src/pypdm/dao/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from color_log.clog import log


class BaseDao:
class BaseDao :
"""
Dao 基类
"""
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit 31afa0a

Please sign in to comment.