|
| 1 | +# database_schema/inspectors/kingbase.py |
| 2 | +from sqlalchemy.sql import text |
| 3 | +from sqlalchemy.engine import reflection |
| 4 | +from .base import BaseInspector |
| 5 | +from urllib.parse import quote_plus |
| 6 | + |
| 7 | +# 导入 KingbaseES 自定义方言以注册到 SQLAlchemy |
| 8 | +try: |
| 9 | + from utils.kingbase_dialect import KingbaseESDialect |
| 10 | +except ImportError: |
| 11 | + pass # 如果导入失败,使用标准 PostgreSQL 方言 |
| 12 | + |
| 13 | +class KingbaseESInspector(BaseInspector): |
| 14 | + """人大金仓数据库(KingbaseES) 元数据获取实现 |
| 15 | +
|
| 16 | + KingbaseES 兼容 PostgreSQL 协议,是国产数据库的代表之一 |
| 17 | + 主要特点: |
| 18 | + 1. 完全兼容 PostgreSQL 9.6+ 的语法和协议 |
| 19 | + 2. 使用 SCRAM-SHA-256 或 MD5 认证 |
| 20 | + 3. 默认端口为 54321 (不同于 PostgreSQL 的 5432) |
| 21 | + 4. 系统表结构与 PostgreSQL 相同 |
| 22 | + 5. 支持 schema 概念,默认 schema 为 public |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, host: str, port: int, database: str, |
| 26 | + username: str, password: str, schema_name: str = None, **kwargs): |
| 27 | + super().__init__(host, port, database, username, password, schema_name) |
| 28 | + self.schema_name = schema_name or "public" |
| 29 | + |
| 30 | + def build_conn_str(self, host: str, port: int, database: str, |
| 31 | + username: str, password: str) -> str: |
| 32 | + """构建人大金仓数据库连接字符串 |
| 33 | +
|
| 34 | + 关键参数说明: |
| 35 | + - 使用自定义的 kingbase 方言处理版本解析问题 |
| 36 | + - 默认端口: 54321 |
| 37 | + - 驱动: psycopg2 (兼容 PostgreSQL) |
| 38 | + """ |
| 39 | + encoded_password = quote_plus(password) |
| 40 | + encoded_username = quote_plus(username) |
| 41 | + |
| 42 | + # 构建基础连接字符串,使用自定义的 kingbase 方言 |
| 43 | + base_uri = f"kingbase+psycopg2://{encoded_username}:{encoded_password}@{host}:{port}/{database}" |
| 44 | + |
| 45 | + return base_uri |
| 46 | + |
| 47 | + def get_table_names(self, inspector: reflection.Inspector) -> list[str]: |
| 48 | + """获取指定 schema 下的所有表名""" |
| 49 | + return inspector.get_table_names(schema=self.schema_name) |
| 50 | + |
| 51 | + def get_table_comment(self, inspector: reflection.Inspector, |
| 52 | + table_name: str) -> str: |
| 53 | + """获取表注释 |
| 54 | +
|
| 55 | + KingbaseES 使用与 PostgreSQL 相同的系统目录结构 |
| 56 | + """ |
| 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 "" |
| 69 | + |
| 70 | + def get_column_comment(self, inspector: reflection.Inspector, |
| 71 | + table_name: str, column_name: str) -> str: |
| 72 | + """获取列注释""" |
| 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, { |
| 86 | + "schema": self.schema_name, |
| 87 | + "table": table_name, |
| 88 | + "column": column_name |
| 89 | + }).scalar() |
| 90 | + return result or "" |
| 91 | + |
| 92 | + def normalize_type(self, raw_type: str) -> str: |
| 93 | + """标准化字段类型 |
| 94 | +
|
| 95 | + KingbaseES 支持 PostgreSQL 类型 + 部分 Oracle 兼容类型 |
| 96 | + """ |
| 97 | + type_map = { |
| 98 | + # PostgreSQL 标准类型 |
| 99 | + 'jsonb': 'JSON', |
| 100 | + 'bytea': 'BLOB', |
| 101 | + 'serial': 'INTEGER', |
| 102 | + 'bigserial': 'BIGINT', |
| 103 | + 'uuid': 'UUID', |
| 104 | + 'int4': 'INTEGER', |
| 105 | + 'int8': 'BIGINT', |
| 106 | + 'int2': 'SMALLINT', |
| 107 | + 'float4': 'FLOAT', |
| 108 | + 'float8': 'DOUBLE', |
| 109 | + 'bool': 'BOOLEAN', |
| 110 | + 'timestamptz': 'TIMESTAMP WITH TIME ZONE', |
| 111 | + 'timestamp': 'TIMESTAMP', |
| 112 | + |
| 113 | + # KingbaseES 扩展类型 (Oracle 兼容) |
| 114 | + 'clob': 'TEXT', |
| 115 | + 'blob': 'BLOB', |
| 116 | + 'number': 'NUMERIC', |
| 117 | + 'raw': 'BYTEA', |
| 118 | + 'nvarchar2': 'NVARCHAR', |
| 119 | + 'varchar2': 'VARCHAR', |
| 120 | + 'long': 'TEXT', |
| 121 | + 'binary_double': 'DOUBLE', |
| 122 | + 'binary_float': 'FLOAT' |
| 123 | + } |
| 124 | + |
| 125 | + # 提取基础类型(去除长度限制) |
| 126 | + base_type = raw_type.split('(')[0].lower().strip() |
| 127 | + return type_map.get(base_type, raw_type.upper()) |
0 commit comments