Skip to content

Commit

Permalink
完善代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris-code committed Jan 10, 2023
1 parent 64e58fc commit dde76a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions feapder/db/mysqldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,15 @@ def __init__(

@classmethod
def from_url(cls, url, **kwargs):
# mysql://username:password@ip:port/db?charset=utf8mb4
"""
Args:
url: mysql://username:password@ip:port/db?charset=utf8mb4
**kwargs:
Returns:
"""
url_parsed = parse.urlparse(url)

db_type = url_parsed.scheme.strip()
Expand Down Expand Up @@ -137,8 +145,10 @@ def get_connection(self):
return conn, cursor

def close_connection(self, conn, cursor):
cursor.close()
conn.close()
if conn:
conn.close()
if cursor:
cursor.close()

def size_of_connections(self):
"""
Expand Down Expand Up @@ -223,6 +233,7 @@ def add(self, sql, exception_callfunc=None):
"""
affect_count = None
conn, cursor = None, None

try:
conn, cursor = self.get_connection()
Expand Down Expand Up @@ -268,6 +279,7 @@ def add_batch(self, sql, datas: List[Dict]):
@result: 添加行数
"""
affect_count = None
conn, cursor = None, None

try:
conn, cursor = self.get_connection()
Expand Down Expand Up @@ -302,11 +314,12 @@ def add_batch_smart(self, table, datas: List[Dict], **kwargs):
return self.add_batch(sql, datas)

def update(self, sql):
conn, cursor = None, None

try:
conn, cursor = self.get_connection()
cursor.execute(sql)
conn.commit()

except Exception as e:
log.error(
"""
Expand Down Expand Up @@ -344,11 +357,11 @@ def delete(self, sql):
Returns: True / False
"""
conn, cursor = None, None
try:
conn, cursor = self.get_connection()
cursor.execute(sql)
conn.commit()

except Exception as e:
log.error(
"""
Expand All @@ -364,11 +377,11 @@ def delete(self, sql):
self.close_connection(conn, cursor)

def execute(self, sql):
conn, cursor = None, None
try:
conn, cursor = self.get_connection()
cursor.execute(sql)
conn.commit()

except Exception as e:
log.error(
"""
Expand Down
2 changes: 1 addition & 1 deletion feapder/dedup/litefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self):

def add(
self, keys: Union[List[str], str], *args, **kwargs
) -> Union[list[int], int]:
) -> Union[List[int], int]:
"""
Args:
Expand Down

0 comments on commit dde76a3

Please sign in to comment.