|
| 1 | +import re |
| 2 | + |
| 3 | +from sqlalchemy import inspect |
| 4 | + |
| 5 | +from dataherald.db import DB |
| 6 | +from dataherald.db_scanner import Scanner |
| 7 | +from dataherald.db_scanner.repository.base import TableDescriptionRepository |
| 8 | +from dataherald.repositories.database_connections import DatabaseConnectionRepository |
| 9 | +from dataherald.sql_database.base import SQLDatabase |
| 10 | +from dataherald.sql_database.models.types import DatabaseConnection |
| 11 | +from dataherald.types import DatabaseConnectionRequest |
| 12 | +from dataherald.utils.encrypt import FernetEncrypt |
| 13 | + |
| 14 | + |
| 15 | +class DatabaseConnectionService: |
| 16 | + def __init__(self, scanner: Scanner, storage: DB): |
| 17 | + self.scanner = scanner |
| 18 | + self.storage = storage |
| 19 | + |
| 20 | + def get_current_schema(self, database_connection: DatabaseConnection) -> list[str]: |
| 21 | + sql_database = SQLDatabase.get_sql_engine(database_connection, True) |
| 22 | + inspector = inspect(sql_database.engine) |
| 23 | + if inspector.default_schema_name and database_connection.dialect not in [ |
| 24 | + "mssql", |
| 25 | + "mysql", |
| 26 | + "clickhouse", |
| 27 | + "duckdb", |
| 28 | + ]: |
| 29 | + return [inspector.default_schema_name] |
| 30 | + if database_connection.dialect == "bigquery": |
| 31 | + pattern = r"([^:/]+)://([^/]+)/([^/]+)(\?[^/]+)" |
| 32 | + match = re.match(pattern, str(sql_database.engine.url)) |
| 33 | + if match: |
| 34 | + return [match.group(3)] |
| 35 | + elif database_connection.dialect == "databricks": |
| 36 | + pattern = r"&schema=([^&]*)" |
| 37 | + match = re.search(pattern, str(sql_database.engine.url)) |
| 38 | + if match: |
| 39 | + return [match.group(1)] |
| 40 | + return ["default"] |
| 41 | + |
| 42 | + def remove_schema_in_uri(self, connection_uri: str, dialect: str) -> str: |
| 43 | + if dialect in ["snowflake"]: |
| 44 | + pattern = r"([^:/]+)://([^:]+):([^@]+)@([^:/]+)(?::(\d+))?/([^/]+)" |
| 45 | + match = re.match(pattern, connection_uri) |
| 46 | + if match: |
| 47 | + return match.group(0) |
| 48 | + if dialect in ["bigquery"]: |
| 49 | + pattern = r"([^:/]+)://([^/]+)" |
| 50 | + match = re.match(pattern, connection_uri) |
| 51 | + if match: |
| 52 | + return match.group(0) |
| 53 | + elif dialect in ["databricks"]: |
| 54 | + pattern = r"&schema=[^&]*" |
| 55 | + return re.sub(pattern, "", connection_uri) |
| 56 | + elif dialect in ["postgresql"]: |
| 57 | + pattern = r"\?options=-csearch_path" r"=[^&]*" |
| 58 | + return re.sub(pattern, "", connection_uri) |
| 59 | + return connection_uri |
| 60 | + |
| 61 | + def add_schema_in_uri(self, connection_uri: str, schema: str, dialect: str) -> str: |
| 62 | + connection_uri = self.remove_schema_in_uri(connection_uri, dialect) |
| 63 | + if dialect in ["snowflake", "bigquery"]: |
| 64 | + return f"{connection_uri}/{schema}" |
| 65 | + if dialect in ["databricks"]: |
| 66 | + return f"{connection_uri}&schema={schema}" |
| 67 | + if dialect in ["postgresql"]: |
| 68 | + return f"{connection_uri}?options=-csearch_path={schema}" |
| 69 | + return connection_uri |
| 70 | + |
| 71 | + def create( |
| 72 | + self, database_connection_request: DatabaseConnectionRequest |
| 73 | + ) -> DatabaseConnection: |
| 74 | + database_connection = DatabaseConnection( |
| 75 | + alias=database_connection_request.alias, |
| 76 | + connection_uri=database_connection_request.connection_uri.strip(), |
| 77 | + schemas=database_connection_request.schemas, |
| 78 | + path_to_credentials_file=database_connection_request.path_to_credentials_file, |
| 79 | + llm_api_key=database_connection_request.llm_api_key, |
| 80 | + use_ssh=database_connection_request.use_ssh, |
| 81 | + ssh_settings=database_connection_request.ssh_settings, |
| 82 | + file_storage=database_connection_request.file_storage, |
| 83 | + metadata=database_connection_request.metadata, |
| 84 | + ) |
| 85 | + if not database_connection.schemas: |
| 86 | + database_connection.schemas = self.get_current_schema(database_connection) |
| 87 | + |
| 88 | + schemas_and_tables = {} |
| 89 | + fernet_encrypt = FernetEncrypt() |
| 90 | + |
| 91 | + if database_connection.schemas: |
| 92 | + for schema in database_connection.schemas: |
| 93 | + database_connection.connection_uri = fernet_encrypt.encrypt( |
| 94 | + self.add_schema_in_uri( |
| 95 | + database_connection_request.connection_uri.strip(), |
| 96 | + schema, |
| 97 | + str(database_connection.dialect), |
| 98 | + ) |
| 99 | + ) |
| 100 | + sql_database = SQLDatabase.get_sql_engine(database_connection, True) |
| 101 | + schemas_and_tables[schema] = sql_database.get_tables_and_views() |
| 102 | + |
| 103 | + # Connect db |
| 104 | + db_connection_repository = DatabaseConnectionRepository(self.storage) |
| 105 | + database_connection.connection_uri = fernet_encrypt.encrypt( |
| 106 | + self.remove_schema_in_uri( |
| 107 | + database_connection_request.connection_uri.strip(), |
| 108 | + str(database_connection.dialect), |
| 109 | + ) |
| 110 | + ) |
| 111 | + db_connection = db_connection_repository.insert(database_connection) |
| 112 | + |
| 113 | + scanner_repository = TableDescriptionRepository(self.storage) |
| 114 | + # Add created tables |
| 115 | + for schema, tables in schemas_and_tables.items(): |
| 116 | + self.scanner.create_tables( |
| 117 | + tables, str(db_connection.id), schema, scanner_repository |
| 118 | + ) |
| 119 | + return DatabaseConnection(**db_connection.dict()) |
0 commit comments