Skip to content

Commit 3a9445f

Browse files
committed
fix: fix SQL Server connect error
1 parent 764df77 commit 3a9445f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

backend/apps/db/db.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from decimal import Decimal
66
from typing import Optional
77

8+
import pymssql
9+
810
from apps.db.db_sql import get_table_sql, get_field_sql, get_version_sql
911
from common.error import ParseSQLResultError
1012

@@ -70,6 +72,18 @@ def get_uri_from_config(type: str, conf: DatasourceConf) -> str:
7072
return db_url
7173

7274

75+
def get_origin_connect(type: str, conf: DatasourceConf):
76+
if type == "sqlServer":
77+
return pymssql.connect(
78+
server=conf.host,
79+
port=str(conf.port),
80+
user=conf.username,
81+
password=conf.password,
82+
database=conf.database,
83+
tds_version='7.0' # options: '4.2', '7.0', '8.0' ...
84+
)
85+
86+
7387
def get_engine(ds: CoreDatasource, timeout: int = 0) -> Engine:
7488
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if ds.type != "excel" else get_engine_config()
7589
if conf.timeout is None:
@@ -87,7 +101,8 @@ def get_engine(ds: CoreDatasource, timeout: int = 0) -> Engine:
87101
connect_args={"connect_timeout": conf.timeout},
88102
pool_timeout=conf.timeout)
89103
elif ds.type == 'sqlServer':
90-
engine = create_engine(get_uri(ds), pool_timeout=conf.timeout)
104+
engine = create_engine('mssql+pymssql://', creator=lambda: get_origin_connect(ds.type, conf),
105+
pool_timeout=conf.timeout)
91106
elif ds.type == 'oracle':
92107
engine = create_engine(get_uri(ds),
93108
pool_timeout=conf.timeout)

0 commit comments

Comments
 (0)