Skip to content

Commit f78cd23

Browse files
committed
feat:修复高斯数据库链接报错问题
1 parent ac226e0 commit f78cd23

File tree

8 files changed

+130
-267
lines changed

8 files changed

+130
-267
lines changed

CLAUDE.md

Lines changed: 0 additions & 209 deletions
This file was deleted.

database_schema/inspectors/dm.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ def build_conn_str(self, host: str, port: int, database: str,
2626
username: str, password: str) -> str:
2727
"""构建达梦数据库连接字符串
2828
29-
达梦连接格式:dm+dmPython://username:password@host:port/?schema=SCHEMANAME
29+
dmPython 不支持 SQLAlchemy 方言,改用 Oracle 兼容模式
30+
达梦数据库在协议层面兼容 Oracle,可以使用 oracledb 驱动
3031
"""
3132
encoded_username = quote_plus(username)
3233
encoded_password = quote_plus(password)
3334

34-
# 达梦数据库连接字符串
35-
# 注意:达梦的连接方式类似 Oracle
36-
return f"dm+dmPython://{encoded_username}:{encoded_password}@{host}:{port}/"
35+
# 使用 Oracle 驱动连接达梦数据库(达梦兼容 Oracle 协议)
36+
# 格式:oracle+oracledb://user:pass@host:port/?service_name=SYSDBA
37+
return f"oracle+oracledb://{encoded_username}:{encoded_password}@{host}:{port}/?service_name=SYSDBA"
3738

3839
def get_table_names(self, inspector: reflection.Inspector) -> list[str]:
3940
"""获取指定 schema 下的所有表名"""

database_schema/inspectors/gaussdb.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
from .base import BaseInspector
55
from urllib.parse import quote_plus
66

7+
# 导入 GaussDB 自定义方言以注册到 SQLAlchemy
8+
try:
9+
from utils.gaussdb_dialect import GaussDBDialect
10+
except ImportError:
11+
pass # 如果导入失败,使用标准 PostgreSQL 方言
12+
713
class GaussDBInspector(BaseInspector):
814
"""华为高斯数据库(GaussDB) 元数据获取实现
915
@@ -16,26 +22,25 @@ class GaussDBInspector(BaseInspector):
1622

1723
def __init__(self, host: str, port: int, database: str,
1824
username: str, password: str, schema_name: str = None, **kwargs):
19-
super().__init__(host, port, database, username, password)
25+
super().__init__(host, port, database, username, password, schema_name)
2026
self.schema_name = schema_name or "public"
2127

2228
def build_conn_str(self, host: str, port: int, database: str,
2329
username: str, password: str) -> str:
2430
"""构建高斯数据库连接字符串
2531
2632
关键参数说明:
27-
- sslmode=disable: 禁用 SSL(如果服务器支持可改为 prefer)
28-
- gssencmode=disable: 禁用 GSS 加密,避免 SASL 认证问题
33+
- sslmode=disable: 禁用 SSL
34+
- 保留 SCRAM-SHA-256 等认证方式的支持
2935
"""
3036
encoded_password = quote_plus(password)
3137
encoded_username = quote_plus(username)
3238

33-
# 构建基础连接字符串
34-
base_uri = f"postgresql+psycopg2://{encoded_username}:{encoded_password}@{host}:{port}/{database}"
39+
# 构建基础连接字符串,使用自定义的 gaussdb 方言
40+
base_uri = f"gaussdb+psycopg://{encoded_username}:{encoded_password}@{host}:{port}/{database}"
3541

36-
# 添加关键连接参数以解决 SASL 认证问题
37-
# gssencmode=disable 是关键,用于禁用 GSSAPI/SASL 认证
38-
conn_params = "?sslmode=disable&gssencmode=disable"
42+
# 只禁用 SSL,保留 SCRAM-SHA-256 认证支持
43+
conn_params = "?sslmode=disable"
3944

4045
return base_uri + conn_params
4146

@@ -49,41 +54,40 @@ def get_table_comment(self, inspector: reflection.Inspector,
4954
5055
GaussDB 使用与 PostgreSQL 相同的系统目录结构
5156
"""
52-
sql = """
53-
SELECT obj_description(c.oid, 'pg_class')
54-
FROM pg_catalog.pg_class c
55-
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
56-
WHERE n.nspname = :schema AND c.relname = :table
57-
"""
58-
result = self.conn.execute(
59-
text(sql),
60-
{"schema": self.schema_name, "table": table_name}
61-
).scalar()
62-
return result or ""
57+
with self.engine.connect() as conn:
58+
sql = text("""
59+
SELECT obj_description(c.oid, 'pg_class')
60+
FROM pg_catalog.pg_class c
61+
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
62+
WHERE n.nspname = :schema AND c.relname = :table
63+
""")
64+
result = conn.execute(sql, {
65+
"schema": self.schema_name,
66+
"table": table_name
67+
}).scalar()
68+
return result or ""
6369

6470
def get_column_comment(self, inspector: reflection.Inspector,
6571
table_name: str, column_name: str) -> str:
6672
"""获取列注释"""
67-
sql = """
68-
SELECT pg_catalog.col_description(c.oid, a.attnum)
69-
FROM pg_catalog.pg_class c
70-
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
71-
JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
72-
WHERE n.nspname = :schema
73-
AND c.relname = :table
74-
AND a.attname = :column
75-
AND a.attnum > 0
76-
AND NOT a.attisdropped
77-
"""
78-
result = self.conn.execute(
79-
text(sql),
80-
{
73+
with self.engine.connect() as conn:
74+
sql = text("""
75+
SELECT pg_catalog.col_description(c.oid, a.attnum)
76+
FROM pg_catalog.pg_class c
77+
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
78+
JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
79+
WHERE n.nspname = :schema
80+
AND c.relname = :table
81+
AND a.attname = :column
82+
AND a.attnum > 0
83+
AND NOT a.attisdropped
84+
""")
85+
result = conn.execute(sql, {
8186
"schema": self.schema_name,
8287
"table": table_name,
8388
"column": column_name
84-
}
85-
).scalar()
86-
return result or ""
89+
}).scalar()
90+
return result or ""
8791

8892
def normalize_type(self, raw_type: str) -> str:
8993
"""标准化字段类型

manifest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 1.0.1
1+
version: 1.0.3
22
type: plugin
33
author: jaguarliuu
44
name: rookie_text2data

requirements.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ sqlalchemy>=2.0.0
55
# 数据库驱动
66
pyodbc>=4.0.39 # SQL Server新驱动
77
pymysql>=1.1.1 # MySQL驱动
8-
psycopg2-binary>=2.9.10 # PostgreSQL驱动 (也用于GaussDB)
9-
oracledb>=2.0.0 # Oracle驱动 (推荐使用 python-oracledb,替代 cx_Oracle)
10-
dmPython>=2.3.0 # 达梦数据库驱动
8+
psycopg2-binary>=2.9.10 # PostgreSQL驱动 (也用于GaussDB,2.9+ 支持 SCRAM-SHA-256)
9+
psycopg[binary]>=3.2.0 # 为 GaussDB 使用 psycopg3 驱动
10+
oracledb>=2.0.0 # Oracle驱动 (也用于达梦数据库,达梦兼容 Oracle 协议)
11+
# dmPython>=2.3.0 # 达梦原生驱动(不支持 SQLAlchemy,已改用 oracledb)
1112

1213
# 安全相关
1314
cryptography>=41.0.0,<43.0.0 # 使用范围版本以提高平台兼容性

rookie_text2data.difypkg

-740 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)