Skip to content

Commit 412da78

Browse files
committed
fix: ODBC驱动名自动检测,不需要手动设环境变量
pyodbc.drivers() 可以读到系统注册的ODBC驱动列表, 优先匹配名字含'gaussdb'的,找不到再找PostgreSQL Unicode, 都不行才fallback到默认值。GAUSSDB_DRIVER环境变量仍可覆盖。
1 parent 0606232 commit 412da78

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

‎tests/test_config.py‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,27 @@
2121
GAUSSDB_USER = os.environ.get("GAUSSDB_USER", "")
2222
GAUSSDB_PASSWORD = os.environ.get("GAUSSDB_PASSWORD", "")
2323

24-
# ODBC driver name (varies by platform/installation)
25-
GAUSSDB_DRIVER = os.environ.get("GAUSSDB_DRIVER", "GaussDB ODBC Driver")
24+
# ODBC driver name — auto-detect if not explicitly set
25+
def _detect_odbc_driver():
26+
"""Try to find a GaussDB ODBC driver from the system."""
27+
explicit = os.environ.get("GAUSSDB_DRIVER")
28+
if explicit:
29+
return explicit
30+
try:
31+
import pyodbc
32+
for name in pyodbc.drivers():
33+
low = name.lower()
34+
if "gaussdb" in low:
35+
return name
36+
# Fallback: PostgreSQL driver also works (GaussDB is PG-compatible)
37+
for name in pyodbc.drivers():
38+
if "postgresql" in name.lower() and "unicode" in name.lower():
39+
return name
40+
except Exception:
41+
pass
42+
return "GaussDB ODBC Driver"
43+
44+
GAUSSDB_DRIVER = _detect_odbc_driver()
2645
_driver = quote_plus(GAUSSDB_DRIVER)
2746

2847
# Database names for each compatibility mode

0 commit comments

Comments
 (0)