| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from dataclasses import dataclass |
| 4 |
from typing import TYPE_CHECKING |
| 5 |
|
| 6 |
from harlequin.catalog import InteractiveCatalogItem |
| 7 |
from harlequin_duckdb.interactions import ( |
| 8 |
execute_drop_schema_statement, |
| 9 |
execute_drop_table_statement, |
| 10 |
execute_drop_view_statement, |
| 11 |
execute_use_statement, |
| 12 |
insert_columns_at_cursor, |
| 13 |
show_describe_relation, |
| 14 |
show_export_database, |
| 15 |
show_select_star, |
| 16 |
show_summarize_relation, |
| 17 |
show_table_ddl, |
| 18 |
show_view_ddl, |
| 19 |
) |
| 20 |
|
| 21 |
if TYPE_CHECKING: |
| 22 |
from harlequin_duckdb.adapter import DuckDbConnection |
| 23 |
|
| 24 |
|
| 25 |
@dataclass |
| 26 |
class ColumnCatalogItem(InteractiveCatalogItem["DuckDbConnection"]): |
| 27 |
parent: "RelationCatalogItem" | None = None |
| 28 |
|
| 29 |
@classmethod |
| 30 |
def from_parent( |
| 31 |
cls, |
| 32 |
parent: "RelationCatalogItem", |
| 33 |
label: str, |
| 34 |
type_label: str, |
| 35 |
type_name: str, |
| 36 |
) -> "ColumnCatalogItem": |
| 37 |
column_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 38 |
column_query_name = f'"{label}"' |
| 39 |
return cls( |
| 40 |
qualified_identifier=column_qualified_identifier, |
| 41 |
query_name=column_query_name, |
| 42 |
label=label, |
| 43 |
type_label=type_label, |
| 44 |
type_name=type_name, |
| 45 |
connection=parent.connection, |
| 46 |
parent=parent, |
| 47 |
loaded=True, |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
@dataclass |
| 52 |
class RelationCatalogItem(InteractiveCatalogItem["DuckDbConnection"]): |
| 53 |
INTERACTIONS = [ |
| 54 |
("Insert Columns at Cursor", insert_columns_at_cursor), |
| 55 |
("Preview Data", show_select_star), |
| 56 |
("Describe", show_describe_relation), |
| 57 |
("Summarize", show_summarize_relation), |
| 58 |
] |
| 59 |
parent: "SchemaCatalogItem" | None = None |
| 60 |
|
| 61 |
def fetch_children(self) -> list[ColumnCatalogItem]: |
| 62 |
if self.parent is None or self.parent.parent is None or self.connection is None: |
| 63 |
return [] |
| 64 |
result = self.connection._get_columns( |
| 65 |
self.parent.parent.label, self.parent.label, self.label |
| 66 |
) |
| 67 |
return [ |
| 68 |
ColumnCatalogItem.from_parent( |
| 69 |
parent=self, |
| 70 |
label=column_name, |
| 71 |
type_label=self.connection._short_column_type(column_type), |
| 72 |
type_name=column_type, |
| 73 |
) |
| 74 |
for column_name, column_type in result |
| 75 |
] |
| 76 |
|
| 77 |
|
| 78 |
class ViewCatalogItem(RelationCatalogItem): |
| 79 |
INTERACTIONS = RelationCatalogItem.INTERACTIONS + [ |
| 80 |
("Show DDL", show_view_ddl), |
| 81 |
("Drop View", execute_drop_view_statement), |
| 82 |
] |
| 83 |
|
| 84 |
@classmethod |
| 85 |
def from_parent( |
| 86 |
cls, |
| 87 |
parent: "SchemaCatalogItem", |
| 88 |
label: str, |
| 89 |
type_name: str, |
| 90 |
) -> "ViewCatalogItem": |
| 91 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 92 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 93 |
return cls( |
| 94 |
qualified_identifier=relation_qualified_identifier, |
| 95 |
query_name=relation_query_name, |
| 96 |
label=label, |
| 97 |
type_label="v", |
| 98 |
type_name=type_name, |
| 99 |
connection=parent.connection, |
| 100 |
parent=parent, |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
class TableCatalogItem(RelationCatalogItem): |
| 105 |
INTERACTIONS = RelationCatalogItem.INTERACTIONS + [ |
| 106 |
("Show DDL", show_table_ddl), |
| 107 |
("Drop Table", execute_drop_table_statement), |
| 108 |
] |
| 109 |
|
| 110 |
@classmethod |
| 111 |
def from_parent( |
| 112 |
cls, |
| 113 |
parent: "SchemaCatalogItem", |
| 114 |
label: str, |
| 115 |
type_name: str, |
| 116 |
) -> "TableCatalogItem": |
| 117 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 118 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 119 |
return cls( |
| 120 |
qualified_identifier=relation_qualified_identifier, |
| 121 |
query_name=relation_query_name, |
| 122 |
label=label, |
| 123 |
type_label="t", |
| 124 |
type_name=type_name, |
| 125 |
connection=parent.connection, |
| 126 |
parent=parent, |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
class TempTableCatalogItem(TableCatalogItem): |
| 131 |
INTERACTIONS = RelationCatalogItem.INTERACTIONS + [ |
| 132 |
("Show DDL", show_table_ddl), |
| 133 |
("Drop Table", execute_drop_table_statement), |
| 134 |
] |
| 135 |
|
| 136 |
@classmethod |
| 137 |
def from_parent( |
| 138 |
cls, |
| 139 |
parent: "SchemaCatalogItem", |
| 140 |
label: str, |
| 141 |
type_name: str, |
| 142 |
) -> "TempTableCatalogItem": |
| 143 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 144 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 145 |
return cls( |
| 146 |
qualified_identifier=relation_qualified_identifier, |
| 147 |
query_name=relation_query_name, |
| 148 |
label=label, |
| 149 |
type_label="tmp", |
| 150 |
type_name=type_name, |
| 151 |
connection=parent.connection, |
| 152 |
parent=parent, |
| 153 |
) |
| 154 |
|
| 155 |
|
| 156 |
@dataclass |
| 157 |
class SchemaCatalogItem(InteractiveCatalogItem["DuckDbConnection"]): |
| 158 |
INTERACTIONS = [ |
| 159 |
("Switch Editor Context (Use)", execute_use_statement), |
| 160 |
("Drop Schema", execute_drop_schema_statement), |
| 161 |
] |
| 162 |
parent: "DatabaseCatalogItem" | None = None |
| 163 |
|
| 164 |
@classmethod |
| 165 |
def from_parent( |
| 166 |
cls, |
| 167 |
parent: "DatabaseCatalogItem", |
| 168 |
label: str, |
| 169 |
) -> "SchemaCatalogItem": |
| 170 |
schema_identifier = f'{parent.qualified_identifier}."{label}"' |
| 171 |
return cls( |
| 172 |
qualified_identifier=schema_identifier, |
| 173 |
query_name=schema_identifier, |
| 174 |
label=label, |
| 175 |
type_label="sch", |
| 176 |
type_name="schema", |
| 177 |
connection=parent.connection, |
| 178 |
parent=parent, |
| 179 |
) |
| 180 |
|
| 181 |
def fetch_children(self) -> list[RelationCatalogItem]: |
| 182 |
if self.parent is None or self.connection is None: |
| 183 |
return [] |
| 184 |
children: list[RelationCatalogItem] = [] |
| 185 |
result = self.connection._get_tables(self.parent.label, self.label) |
| 186 |
for table_label, table_type in result: |
| 187 |
if table_type == "VIEW": |
| 188 |
children.append( |
| 189 |
ViewCatalogItem.from_parent( |
| 190 |
parent=self, |
| 191 |
label=table_label, |
| 192 |
type_name=table_type, |
| 193 |
) |
| 194 |
) |
| 195 |
elif table_type == "LOCAL TEMPORARY": |
| 196 |
children.append( |
| 197 |
TempTableCatalogItem.from_parent( |
| 198 |
parent=self, |
| 199 |
label=table_label, |
| 200 |
type_name=table_type, |
| 201 |
) |
| 202 |
) |
| 203 |
else: |
| 204 |
children.append( |
| 205 |
TableCatalogItem.from_parent( |
| 206 |
parent=self, |
| 207 |
label=table_label, |
| 208 |
type_name=table_type, |
| 209 |
) |
| 210 |
) |
| 211 |
|
| 212 |
return children |
| 213 |
|
| 214 |
|
| 215 |
class DatabaseCatalogItem(InteractiveCatalogItem["DuckDbConnection"]): |
| 216 |
INTERACTIONS = [ |
| 217 |
("Switch Editor Context (Use)", execute_use_statement), |
| 218 |
("Export Database", show_export_database), |
| 219 |
] |
| 220 |
|
| 221 |
@classmethod |
| 222 |
def from_label( |
| 223 |
cls, label: str, connection: "DuckDbConnection" |
| 224 |
) -> "DatabaseCatalogItem": |
| 225 |
database_identifier = f'"{label}"' |
| 226 |
return cls( |
| 227 |
qualified_identifier=database_identifier, |
| 228 |
query_name=database_identifier, |
| 229 |
label=label, |
| 230 |
type_label="db", |
| 231 |
type_name="database", |
| 232 |
connection=connection, |
| 233 |
) |
| 234 |
|
| 235 |
def fetch_children(self) -> list[SchemaCatalogItem]: |
| 236 |
if self.connection is None: |
| 237 |
return [] |
| 238 |
schemas = self.connection._get_schemas(self.label) |
| 239 |
return [ |
| 240 |
SchemaCatalogItem.from_parent( |
| 241 |
parent=self, |
| 242 |
label=schema_label, |
| 243 |
) |
| 244 |
for (schema_label,) in schemas |
| 245 |
] |