Procházet zdrojové kódy

pyodbc 更改为 pymssql

yue před 2 týdny
rodič
revize
8e4dc89256

+ 37 - 10
SourceCode/IntelligentRailwayCosting/app/tools/db_helper/sqlserver_helper.py

@@ -1,5 +1,5 @@
 from typing import Dict, Optional, Any, List, Tuple
-import core.configs as configs
+import core.configs as configs, tools.utils as utils
 from sqlalchemy import create_engine, text
 from sqlalchemy.engine import Engine
 from sqlalchemy.orm import sessionmaker
@@ -10,6 +10,7 @@ from .base import DBHelper
 class SQLServerHelper(DBHelper):
     def __init__(self):
         super().__init__()
+        self._logger = utils.get_logger()
         self._session_makers: Dict[str, sessionmaker] = {}
         self._default_config = {
             "driver": "ODBC Driver 17 for SQL Server",
@@ -18,20 +19,33 @@ class SQLServerHelper(DBHelper):
             "password": "",
             "trusted_connection": "yes",
         }
+        # self._pool_config = {
+        #     "pool_size": 5,  # 减少初始连接数以降低资源占用
+        #     "max_overflow": 10,  # 适当减少最大溢出连接数
+        #     "pool_timeout": 60,  # 增加池等待超时时间
+        #     "pool_recycle": 1800,  # 每30分钟回收连接
+        #     "pool_pre_ping": True,  # 启用连接健康检查
+        #     "connect_args": {
+        #         "timeout": 60,  # 连接超时时间
+        #         "login_timeout": 60,  # 登录超时时间
+        #         "connection_timeout": 60,  # 连接超时设置
+        #     },
+        # }
         self._pool_config = {
             "pool_size": 5,  # 减少初始连接数以降低资源占用
             "max_overflow": 10,  # 适当减少最大溢出连接数
             "pool_timeout": 60,  # 增加池等待超时时间
             "pool_recycle": 1800,  # 每30分钟回收连接
             "pool_pre_ping": True,  # 启用连接健康检查
-            "connect_args": {
-                "timeout": 60,  # 连接超时时间
-                "driver_connects_timeout": 60,  # 驱动连接超时
-                "connect_timeout": 60,  # ODBC连接超时
-                "connect_retries": 3,  # 连接重试次数
-                "connect_retry_interval": 10,  # 重试间隔增加到10秒
-                "connection_timeout": 60,  # 额外的连接超时设置
-            },
+            "deprecate_large_types": True,
+            # "connect_args": {
+            #     "timeout": 60,  # 连接超时时间
+            #     "driver_connects_timeout": 60,  # 驱动连接超时
+            #     "connect_timeout": 60,  # ODBC连接超时
+            #     "connect_retries": 3,  # 连接重试次数
+            #     "connect_retry_interval": 10,  # 重试间隔增加到10秒
+            #     "connection_timeout": 60,  # 额外的连接超时设置
+            # },
         }
 
         self.main_database_name = (
@@ -75,11 +89,24 @@ class SQLServerHelper(DBHelper):
 
         return conn_url
 
+    def _build_pymssql_connection_string(
+            self, database: str, config: Optional[Dict[str, str]] = None
+    ) -> str:
+        """构建连接字符串"""
+        conn_config = self._default_config.copy()
+        db_config = self.get_config_for_database(database)
+        conn_config.update(db_config)
+        if config:
+            conn_config.update(config)
+
+        conn_str = f"mssql+pymssql://{conn_config['username']}:{conn_config['password']}@{conn_config['server']}/{conn_config['database']}?charset=utf8&tds_version={conn_config['tds_version'] if 'tds_version' in conn_config else '7.0'}"
+        return conn_str
+
     def get_engine(
             self, database: str, config: Optional[Dict[str, str]] = None
     ) -> Engine:
         """获取或创建数据库引擎"""
-        conn_str = self._build_connection_string(database, config)
+        conn_str = self._build_pymssql_connection_string(database, config)
         engine = create_engine(conn_str, **self._pool_config)
         # 预热连接池
         with engine.connect() as conn:

+ 1 - 1
SourceCode/IntelligentRailwayCosting/requirements.txt

@@ -10,5 +10,5 @@ openai~=1.66.3
 pandas~=2.2.3
 requests~=2.32.3
 PyYAML~=6.0.2
-pyodbc~=5.2.0
+pymssql~=2.3.4
 cryptography==41.0.4