|
| 1 | +"""MotherDuck adapter for cloud DuckDB.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING, Any |
| 6 | + |
| 7 | +from sqlit.domains.connections.providers.adapters.base import TableInfo |
| 8 | +from sqlit.domains.connections.providers.duckdb.adapter import DuckDBAdapter |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from sqlit.domains.connections.domain.config import ConnectionConfig |
| 12 | + |
| 13 | + |
| 14 | +class MotherDuckAdapter(DuckDBAdapter): |
| 15 | + """Adapter for MotherDuck cloud DuckDB service.""" |
| 16 | + |
| 17 | + @property |
| 18 | + def name(self) -> str: |
| 19 | + return "MotherDuck" |
| 20 | + |
| 21 | + @property |
| 22 | + def supports_process_worker(self) -> bool: |
| 23 | + """MotherDuck handles concurrency server-side.""" |
| 24 | + return True |
| 25 | + |
| 26 | + @property |
| 27 | + def supports_multiple_databases(self) -> bool: |
| 28 | + """MotherDuck supports multiple databases.""" |
| 29 | + return True |
| 30 | + |
| 31 | + def connect(self, config: ConnectionConfig) -> Any: |
| 32 | + """Connect to MotherDuck cloud database.""" |
| 33 | + duckdb = self._import_driver_module( |
| 34 | + "duckdb", |
| 35 | + driver_name=self.name, |
| 36 | + extra_name=self.install_extra, |
| 37 | + package_name=self.install_package, |
| 38 | + ) |
| 39 | + |
| 40 | + # Get default database from options |
| 41 | + database = config.get_option("default_database", "") |
| 42 | + |
| 43 | + # Get token from tcp_endpoint.password (stored in keyring) |
| 44 | + token = "" |
| 45 | + if config.tcp_endpoint: |
| 46 | + token = config.tcp_endpoint.password or "" |
| 47 | + |
| 48 | + if not database: |
| 49 | + raise ValueError("MotherDuck connections require a database name.") |
| 50 | + if not token: |
| 51 | + raise ValueError("MotherDuck connections require an access token.") |
| 52 | + |
| 53 | + conn_str = f"md:{database}?motherduck_token={token}" |
| 54 | + |
| 55 | + duckdb_any: Any = duckdb |
| 56 | + return duckdb_any.connect(conn_str) |
| 57 | + |
| 58 | + def get_databases(self, conn: Any) -> list[str]: |
| 59 | + """List all MotherDuck databases.""" |
| 60 | + result = conn.execute("SELECT database_name FROM duckdb_databases() WHERE NOT internal") |
| 61 | + return [row[0] for row in result.fetchall()] |
| 62 | + |
| 63 | + def get_tables(self, conn: Any, database: str | None = None) -> list[TableInfo]: |
| 64 | + """Get tables from a specific MotherDuck database.""" |
| 65 | + if database: |
| 66 | + result = conn.execute( |
| 67 | + "SELECT table_schema, table_name FROM information_schema.tables " |
| 68 | + "WHERE table_catalog = ? " |
| 69 | + "AND table_type = 'BASE TABLE' " |
| 70 | + "AND table_schema NOT IN ('pg_catalog', 'information_schema') " |
| 71 | + "ORDER BY table_schema, table_name", |
| 72 | + (database,), |
| 73 | + ) |
| 74 | + else: |
| 75 | + result = conn.execute( |
| 76 | + "SELECT table_schema, table_name FROM information_schema.tables " |
| 77 | + "WHERE table_type = 'BASE TABLE' " |
| 78 | + "AND table_schema NOT IN ('pg_catalog', 'information_schema') " |
| 79 | + "ORDER BY table_schema, table_name" |
| 80 | + ) |
| 81 | + return [(row[0], row[1]) for row in result.fetchall()] |
| 82 | + |
| 83 | + def get_views(self, conn: Any, database: str | None = None) -> list[TableInfo]: |
| 84 | + """Get views from a specific MotherDuck database.""" |
| 85 | + if database: |
| 86 | + result = conn.execute( |
| 87 | + "SELECT table_schema, table_name FROM information_schema.tables " |
| 88 | + "WHERE table_catalog = ? " |
| 89 | + "AND table_type = 'VIEW' " |
| 90 | + "AND table_schema NOT IN ('pg_catalog', 'information_schema') " |
| 91 | + "ORDER BY table_schema, table_name", |
| 92 | + (database,), |
| 93 | + ) |
| 94 | + else: |
| 95 | + result = conn.execute( |
| 96 | + "SELECT table_schema, table_name FROM information_schema.tables " |
| 97 | + "WHERE table_type = 'VIEW' " |
| 98 | + "AND table_schema NOT IN ('pg_catalog', 'information_schema') " |
| 99 | + "ORDER BY table_schema, table_name" |
| 100 | + ) |
| 101 | + return [(row[0], row[1]) for row in result.fetchall()] |
| 102 | + |
| 103 | + def build_select_query( |
| 104 | + self, table: str, limit: int, database: str | None = None, schema: str | None = None |
| 105 | + ) -> str: |
| 106 | + """Build SELECT LIMIT query for MotherDuck. |
| 107 | +
|
| 108 | + MotherDuck requires three-part names: database.schema.table |
| 109 | + """ |
| 110 | + schema = schema or "main" |
| 111 | + if database: |
| 112 | + return f'SELECT * FROM "{database}"."{schema}"."{table}" LIMIT {limit}' |
| 113 | + return f'SELECT * FROM "{schema}"."{table}" LIMIT {limit}' |
0 commit comments