44
55from typing import TYPE_CHECKING , Any
66
7+ from sqlit .domains .connections .providers .adapters .base import TableInfo
78from sqlit .domains .connections .providers .duckdb .adapter import DuckDBAdapter
89
910if TYPE_CHECKING :
@@ -36,10 +37,8 @@ def connect(self, config: ConnectionConfig) -> Any:
3637 package_name = self .install_package ,
3738 )
3839
39- # Get database from options or tcp_endpoint
40- database = config .get_option ("database" , "" )
41- if not database and config .tcp_endpoint :
42- database = config .tcp_endpoint .database
40+ # Get default database from options
41+ database = config .get_option ("default_database" , "" )
4342
4443 # Get token from tcp_endpoint.password (stored in keyring)
4544 token = ""
@@ -56,6 +55,51 @@ def connect(self, config: ConnectionConfig) -> Any:
5655 duckdb_any : Any = duckdb
5756 return duckdb_any .connect (conn_str )
5857
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+
59103 def build_select_query (
60104 self , table : str , limit : int , database : str | None = None , schema : str | None = None
61105 ) -> str :
0 commit comments