|
@@ -66,7 +66,7 @@ class MySQLConnectionPool:
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
if commit:
|
|
if commit:
|
|
|
conn.rollback()
|
|
conn.rollback()
|
|
|
- self.log.error(f"Error executing query: {e}, Query: {query}, Args: {args}")
|
|
|
|
|
|
|
+ self.log.exception(f"Error executing query: {e}, Query: {query}, Args: {args}")
|
|
|
raise e
|
|
raise e
|
|
|
|
|
|
|
|
def select_one(self, query, args=None):
|
|
def select_one(self, query, args=None):
|
|
@@ -172,15 +172,17 @@ class MySQLConnectionPool:
|
|
|
# print("插入失败:重复条目", e)
|
|
# print("插入失败:重复条目", e)
|
|
|
return -1 # 返回 -1 表示重复条目被跳过
|
|
return -1 # 返回 -1 表示重复条目被跳过
|
|
|
else:
|
|
else:
|
|
|
- self.log.error(f"数据库完整性错误: {e}")
|
|
|
|
|
|
|
+ self.log.exception(f"数据库完整性错误: {e}")
|
|
|
# print("插入失败:完整性错误", e)
|
|
# print("插入失败:完整性错误", e)
|
|
|
- raise e
|
|
|
|
|
|
|
+ raise
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
- self.log.error(f"未知错误: {e}", exc_info=True)
|
|
|
|
|
|
|
+ # self.log.error(f"未知错误: {str(e)}", exc_info=True)
|
|
|
|
|
+ self.log.exception(f"未知错误: {e}") # 记录完整异常信息
|
|
|
# print("插入失败:未知错误", e)
|
|
# print("插入失败:未知错误", e)
|
|
|
- raise e
|
|
|
|
|
|
|
+ raise
|
|
|
|
|
|
|
|
- def insert_many(self, table=None, data_list=None, query=None, args_list=None, batch_size=500, commit=True, ignore=False):
|
|
|
|
|
|
|
+ def insert_many(self, table=None, data_list=None, query=None, args_list=None, batch_size=1000, commit=True,
|
|
|
|
|
+ ignore=False):
|
|
|
"""
|
|
"""
|
|
|
批量插入(支持字典列表或原始SQL)
|
|
批量插入(支持字典列表或原始SQL)
|
|
|
:param table: 表名(字典插入时必需)
|
|
:param table: 表名(字典插入时必需)
|
|
@@ -210,41 +212,34 @@ class MySQLConnectionPool:
|
|
|
total = 0
|
|
total = 0
|
|
|
for i in range(0, len(args_list), batch_size):
|
|
for i in range(0, len(args_list), batch_size):
|
|
|
batch = args_list[i:i + batch_size]
|
|
batch = args_list[i:i + batch_size]
|
|
|
- conn = None
|
|
|
|
|
try:
|
|
try:
|
|
|
- conn = self.pool.connection()
|
|
|
|
|
- with conn.cursor() as cursor:
|
|
|
|
|
- cursor.executemany(query, batch)
|
|
|
|
|
- if commit:
|
|
|
|
|
- conn.commit()
|
|
|
|
|
- total += cursor.rowcount
|
|
|
|
|
- except pymysql.Error as e:
|
|
|
|
|
- if conn:
|
|
|
|
|
- try:
|
|
|
|
|
|
|
+ with self.pool.connection() as conn:
|
|
|
|
|
+ with conn.cursor() as cursor:
|
|
|
|
|
+ cursor.executemany(query, batch)
|
|
|
if commit:
|
|
if commit:
|
|
|
- conn.rollback()
|
|
|
|
|
- except:
|
|
|
|
|
- pass
|
|
|
|
|
|
|
+ conn.commit()
|
|
|
|
|
+ total += cursor.rowcount
|
|
|
|
|
+ except pymysql.Error as e:
|
|
|
if "Duplicate entry" in str(e):
|
|
if "Duplicate entry" in str(e):
|
|
|
|
|
+ # self.log.warning(f"检测到重复条目,开始逐条插入。错误详情: {e}")
|
|
|
raise e
|
|
raise e
|
|
|
|
|
+ # rowcount = 0
|
|
|
|
|
+ # for args in batch:
|
|
|
|
|
+ # try:
|
|
|
|
|
+ # self.insert_one_or_dict(table=table, data=dict(zip(data_list[0].keys(), args)),
|
|
|
|
|
+ # commit=commit)
|
|
|
|
|
+ # rowcount += 1
|
|
|
|
|
+ # except pymysql.err.IntegrityError as e2:
|
|
|
|
|
+ # if "Duplicate entry" in str(e2):
|
|
|
|
|
+ # self.log.warning(f"跳过重复条目: {args}")
|
|
|
|
|
+ # else:
|
|
|
|
|
+ # self.log.error(f"插入失败: {e2}, 参数: {args}")
|
|
|
|
|
+ # total += rowcount
|
|
|
else:
|
|
else:
|
|
|
- self.log.error(f"数据库错误: {e}")
|
|
|
|
|
|
|
+ self.log.exception(f"数据库错误: {e}")
|
|
|
|
|
+ if commit:
|
|
|
|
|
+ conn.rollback()
|
|
|
raise e
|
|
raise e
|
|
|
- except Exception as e:
|
|
|
|
|
- if conn:
|
|
|
|
|
- try:
|
|
|
|
|
- if commit:
|
|
|
|
|
- conn.rollback()
|
|
|
|
|
- except:
|
|
|
|
|
- pass
|
|
|
|
|
- self.log.error(f"数据库错误: {e}")
|
|
|
|
|
- raise e
|
|
|
|
|
- finally:
|
|
|
|
|
- if conn:
|
|
|
|
|
- try:
|
|
|
|
|
- conn.close()
|
|
|
|
|
- except:
|
|
|
|
|
- pass
|
|
|
|
|
# 重新抛出异常,供外部捕获
|
|
# 重新抛出异常,供外部捕获
|
|
|
# 降级为单条插入
|
|
# 降级为单条插入
|
|
|
# for args in batch:
|
|
# for args in batch:
|
|
@@ -253,11 +248,14 @@ class MySQLConnectionPool:
|
|
|
# total += 1
|
|
# total += 1
|
|
|
# except Exception as e2:
|
|
# except Exception as e2:
|
|
|
# self.log.error(f"Single insert failed: {e2}")
|
|
# self.log.error(f"Single insert failed: {e2}")
|
|
|
- # continue
|
|
|
|
|
- self.log.info(f"sql insert_many, Table: {table}, Total Rows: {total}")
|
|
|
|
|
|
|
+ # continue
|
|
|
|
|
+ if table:
|
|
|
|
|
+ self.log.info(f"sql insert_many, Table: {table}, Total Rows: {total}")
|
|
|
|
|
+ else:
|
|
|
|
|
+ self.log.info(f"sql insert_many, Query: {query}, Total Rows: {total}")
|
|
|
return total
|
|
return total
|
|
|
|
|
|
|
|
- def insert_many_two(self, table=None, data_list=None, query=None, args_list=None, batch_size=500, commit=True):
|
|
|
|
|
|
|
+ def insert_many_two(self, table=None, data_list=None, query=None, args_list=None, batch_size=1000, commit=True):
|
|
|
"""
|
|
"""
|
|
|
批量插入(支持字典列表或原始SQL)
|
|
批量插入(支持字典列表或原始SQL)
|
|
|
:param table: 表名(字典插入时必需)
|
|
:param table: 表名(字典插入时必需)
|
|
@@ -317,6 +315,7 @@ class MySQLConnectionPool:
|
|
|
:param args_list: 插入参数列表
|
|
:param args_list: 插入参数列表
|
|
|
:param batch_size: 每次插入的条数
|
|
:param batch_size: 每次插入的条数
|
|
|
"""
|
|
"""
|
|
|
|
|
+ self.log.info(f"sql insert_too_many, Query: {query}, Total Rows: {len(args_list)}")
|
|
|
for i in range(0, len(args_list), batch_size):
|
|
for i in range(0, len(args_list), batch_size):
|
|
|
batch = args_list[i:i + batch_size]
|
|
batch = args_list[i:i + batch_size]
|
|
|
try:
|
|
try:
|
|
@@ -324,6 +323,7 @@ class MySQLConnectionPool:
|
|
|
with conn.cursor() as cursor:
|
|
with conn.cursor() as cursor:
|
|
|
cursor.executemany(query, batch)
|
|
cursor.executemany(query, batch)
|
|
|
conn.commit()
|
|
conn.commit()
|
|
|
|
|
+ self.log.debug(f"insert_too_many -> Total Rows: {len(batch)}")
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
self.log.error(f"insert_too_many error. Trying single insert. Error: {e}")
|
|
self.log.error(f"insert_too_many error. Trying single insert. Error: {e}")
|
|
|
# 当前批次降级为单条插入
|
|
# 当前批次降级为单条插入
|
|
@@ -560,6 +560,17 @@ class MySQLConnectionPool:
|
|
|
self.log.error(f"Connection pool health check failed: {e}")
|
|
self.log.error(f"Connection pool health check failed: {e}")
|
|
|
return False
|
|
return False
|
|
|
|
|
|
|
|
|
|
+ def close(self):
|
|
|
|
|
+ """
|
|
|
|
|
+ 关闭连接池,释放所有连接
|
|
|
|
|
+ """
|
|
|
|
|
+ try:
|
|
|
|
|
+ if hasattr(self, 'pool') and self.pool:
|
|
|
|
|
+ self.pool.close()
|
|
|
|
|
+ self.log.info("数据库连接池已关闭")
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ self.log.error(f"关闭连接池失败: {e}")
|
|
|
|
|
+
|
|
|
@staticmethod
|
|
@staticmethod
|
|
|
def _safe_identifier(name):
|
|
def _safe_identifier(name):
|
|
|
"""SQL标识符安全校验"""
|
|
"""SQL标识符安全校验"""
|
|
@@ -567,3 +578,14 @@ class MySQLConnectionPool:
|
|
|
raise ValueError(f"Invalid SQL identifier: {name}")
|
|
raise ValueError(f"Invalid SQL identifier: {name}")
|
|
|
return name
|
|
return name
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
|
+ sql_pool = MySQLConnectionPool()
|
|
|
|
|
+ data_dic = {'card_type_id': 111, 'card_type_name': '补充包 继承的意志【OPC-13】', 'card_type_position': 964,
|
|
|
|
|
+ 'card_id': 5284, 'card_name': '蒙奇·D·路飞', 'card_number': 'OP13-001', 'card_rarity': 'L',
|
|
|
|
|
+ 'card_img': 'https://source.windoent.com/OnePiecePc/Picture/1757929283612OP13-001.png',
|
|
|
|
|
+ 'card_life': '4', 'card_attribute': '打', 'card_power': '5000', 'card_attack': '-',
|
|
|
|
|
+ 'card_color': '红/绿', 'subscript': 4, 'card_features': '超新星/草帽一伙',
|
|
|
|
|
+ 'card_text_desc': '【咚!!×1】【对方的攻击时】我方处于活跃状态的咚!!不多于5张的场合,可以将我方任意张数的咚!!转为休息状态。每有1张转为休息状态的咚!!,本次战斗中,此领袖或我方最多1张拥有《草帽一伙》特征的角色力量+2000。',
|
|
|
|
|
+ 'card_offer_type': '补充包 继承的意志【OPC-13】', 'crawler_language': '简中'}
|
|
|
|
|
+ sql_pool.insert_one_or_dict(table="one_piece_record", data=data_dic)
|