| 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 |
|
| 8 |
from harlequin_postgres.interactions import ( |
| 9 |
execute_drop_database_statement, |
| 10 |
execute_drop_foreign_table_statement, |
| 11 |
execute_drop_schema_statement, |
| 12 |
execute_drop_table_statement, |
| 13 |
execute_drop_view_statement, |
| 14 |
execute_use_statement, |
| 15 |
insert_columns_at_cursor, |
| 16 |
show_describe_relation, |
| 17 |
show_describe_table_constraints, |
| 18 |
show_describe_table_indexes, |
| 19 |
show_list_indexes, |
| 20 |
show_list_objects, |
| 21 |
show_select_star, |
| 22 |
show_view_definition, |
| 23 |
) |
| 24 |
|
| 25 |
if TYPE_CHECKING: |
| 26 |
from harlequin_postgres.adapter import HarlequinPostgresConnection |
| 27 |
|
| 28 |
MATERIALIZED_VIEW = "MATERIALIZED VIEW" |
| 29 |
"""What Postgres calls a matview, which information_schema.tables does not have. |
| 30 |
|
| 31 |
`table_type` names every other kind of relation, so this stands in for it as a |
| 32 |
matview's `type_name`. |
| 33 |
""" |
| 34 |
|
| 35 |
|
| 36 |
@dataclass |
| 37 |
class ColumnCatalogItem(InteractiveCatalogItem["HarlequinPostgresConnection"]): |
| 38 |
parent: "RelationCatalogItem" | None = None |
| 39 |
|
| 40 |
@classmethod |
| 41 |
def from_parent( |
| 42 |
cls, |
| 43 |
parent: "RelationCatalogItem", |
| 44 |
label: str, |
| 45 |
type_label: str, |
| 46 |
type_name: str | None = None, |
| 47 |
) -> "ColumnCatalogItem": |
| 48 |
column_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 49 |
column_query_name = f'"{label}"' |
| 50 |
return cls( |
| 51 |
qualified_identifier=column_qualified_identifier, |
| 52 |
query_name=column_query_name, |
| 53 |
label=label, |
| 54 |
type_label=type_label, |
| 55 |
type_name=type_name, |
| 56 |
connection=parent.connection, |
| 57 |
parent=parent, |
| 58 |
loaded=True, |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
@dataclass |
| 63 |
class RelationCatalogItem(InteractiveCatalogItem["HarlequinPostgresConnection"]): |
| 64 |
INTERACTIONS = [ |
| 65 |
("Insert Columns at Cursor", insert_columns_at_cursor), |
| 66 |
("Preview Data", show_select_star), |
| 67 |
("Describe Relation (\\d+)", show_describe_relation), |
| 68 |
] |
| 69 |
parent: "SchemaCatalogItem" | None = None |
| 70 |
|
| 71 |
def fetch_children(self) -> list[ColumnCatalogItem]: |
| 72 |
if self.parent is None or self.parent.parent is None or self.connection is None: |
| 73 |
return [] |
| 74 |
result = self.connection._get_columns( |
| 75 |
self.parent.parent.label, self.parent.label, self.label |
| 76 |
) |
| 77 |
return [ |
| 78 |
ColumnCatalogItem.from_parent( |
| 79 |
parent=self, |
| 80 |
label=column_name, |
| 81 |
type_label=self.connection._short_column_type(column_type), |
| 82 |
type_name=column_type, |
| 83 |
) |
| 84 |
for column_name, column_type in result |
| 85 |
] |
| 86 |
|
| 87 |
|
| 88 |
class ViewCatalogItem(RelationCatalogItem): |
| 89 |
INTERACTIONS = RelationCatalogItem.INTERACTIONS + [ |
| 90 |
("Show View Definition", show_view_definition), |
| 91 |
("Drop View", execute_drop_view_statement), |
| 92 |
] |
| 93 |
|
| 94 |
@classmethod |
| 95 |
def from_parent( |
| 96 |
cls, |
| 97 |
parent: "SchemaCatalogItem", |
| 98 |
label: str, |
| 99 |
type_name: str | None = "VIEW", |
| 100 |
) -> "ViewCatalogItem": |
| 101 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 102 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 103 |
return cls( |
| 104 |
qualified_identifier=relation_qualified_identifier, |
| 105 |
query_name=relation_query_name, |
| 106 |
label=label, |
| 107 |
type_label="v", |
| 108 |
type_name=type_name, |
| 109 |
connection=parent.connection, |
| 110 |
parent=parent, |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
class TableCatalogItem(RelationCatalogItem): |
| 115 |
INTERACTIONS = RelationCatalogItem.INTERACTIONS + [ |
| 116 |
("Describe Indexes", show_describe_table_indexes), |
| 117 |
("Describe Constraints", show_describe_table_constraints), |
| 118 |
("Drop Table", execute_drop_table_statement), |
| 119 |
] |
| 120 |
|
| 121 |
@classmethod |
| 122 |
def from_parent( |
| 123 |
cls, |
| 124 |
parent: "SchemaCatalogItem", |
| 125 |
label: str, |
| 126 |
type_name: str | None = "BASE TABLE", |
| 127 |
) -> "TableCatalogItem": |
| 128 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 129 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 130 |
return cls( |
| 131 |
qualified_identifier=relation_qualified_identifier, |
| 132 |
query_name=relation_query_name, |
| 133 |
label=label, |
| 134 |
type_label="t", |
| 135 |
type_name=type_name, |
| 136 |
connection=parent.connection, |
| 137 |
parent=parent, |
| 138 |
) |
| 139 |
|
| 140 |
|
| 141 |
class TempTableCatalogItem(TableCatalogItem): |
| 142 |
@classmethod |
| 143 |
def from_parent( |
| 144 |
cls, |
| 145 |
parent: "SchemaCatalogItem", |
| 146 |
label: str, |
| 147 |
type_name: str | None = "LOCAL TEMPORARY", |
| 148 |
) -> "TempTableCatalogItem": |
| 149 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 150 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 151 |
return cls( |
| 152 |
qualified_identifier=relation_qualified_identifier, |
| 153 |
query_name=relation_query_name, |
| 154 |
label=label, |
| 155 |
type_label="tmp", |
| 156 |
type_name=type_name, |
| 157 |
connection=parent.connection, |
| 158 |
parent=parent, |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
class ForeignCatalogItem(TableCatalogItem): |
| 163 |
INTERACTIONS = RelationCatalogItem.INTERACTIONS + [ |
| 164 |
("Drop Table", execute_drop_foreign_table_statement), |
| 165 |
] |
| 166 |
|
| 167 |
@classmethod |
| 168 |
def from_parent( |
| 169 |
cls, |
| 170 |
parent: "SchemaCatalogItem", |
| 171 |
label: str, |
| 172 |
type_name: str | None = "FOREIGN", |
| 173 |
) -> "ForeignCatalogItem": |
| 174 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 175 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 176 |
return cls( |
| 177 |
qualified_identifier=relation_qualified_identifier, |
| 178 |
query_name=relation_query_name, |
| 179 |
label=label, |
| 180 |
type_label="f", |
| 181 |
type_name=type_name, |
| 182 |
connection=parent.connection, |
| 183 |
parent=parent, |
| 184 |
) |
| 185 |
|
| 186 |
|
| 187 |
class MaterializedViewCatalogItem(RelationCatalogItem): |
| 188 |
@classmethod |
| 189 |
def from_parent( |
| 190 |
cls, |
| 191 |
parent: "SchemaCatalogItem", |
| 192 |
label: str, |
| 193 |
type_name: str | None = MATERIALIZED_VIEW, |
| 194 |
) -> "MaterializedViewCatalogItem": |
| 195 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 196 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 197 |
return cls( |
| 198 |
qualified_identifier=relation_qualified_identifier, |
| 199 |
query_name=relation_query_name, |
| 200 |
label=label, |
| 201 |
type_label="mv", |
| 202 |
type_name=type_name, |
| 203 |
connection=parent.connection, |
| 204 |
parent=parent, |
| 205 |
) |
| 206 |
|
| 207 |
def fetch_children(self) -> list[ColumnCatalogItem]: |
| 208 |
if self.parent is None or self.parent.parent is None or self.connection is None: |
| 209 |
return [] |
| 210 |
result = self.connection._get_mv_cols(self.parent.label, self.label) |
| 211 |
return [ |
| 212 |
ColumnCatalogItem.from_parent( |
| 213 |
parent=self, |
| 214 |
label=column_name, |
| 215 |
type_label=self.connection._short_column_type(column_type), |
| 216 |
type_name=column_type, |
| 217 |
) |
| 218 |
for column_name, column_type in result |
| 219 |
] |
| 220 |
|
| 221 |
|
| 222 |
@dataclass |
| 223 |
class SchemaCatalogItem(InteractiveCatalogItem["HarlequinPostgresConnection"]): |
| 224 |
INTERACTIONS = [ |
| 225 |
("Set Search Path", execute_use_statement), |
| 226 |
("List Relations (\\d+)", show_list_objects), |
| 227 |
("List Indexes (\\di+)", show_list_indexes), |
| 228 |
("Drop Schema", execute_drop_schema_statement), |
| 229 |
] |
| 230 |
parent: "DatabaseCatalogItem" | None = None |
| 231 |
|
| 232 |
@classmethod |
| 233 |
def from_parent( |
| 234 |
cls, |
| 235 |
parent: "DatabaseCatalogItem", |
| 236 |
label: str, |
| 237 |
) -> "SchemaCatalogItem": |
| 238 |
schema_identifier = f'"{label}"' |
| 239 |
return cls( |
| 240 |
qualified_identifier=schema_identifier, |
| 241 |
query_name=schema_identifier, |
| 242 |
label=label, |
| 243 |
type_label="sch", |
| 244 |
connection=parent.connection, |
| 245 |
parent=parent, |
| 246 |
) |
| 247 |
|
| 248 |
def fetch_children(self) -> list[RelationCatalogItem]: |
| 249 |
if self.parent is None or self.connection is None: |
| 250 |
return [] |
| 251 |
children: list[RelationCatalogItem] = [] |
| 252 |
result = self.connection._get_relations(self.parent.label, self.label) |
| 253 |
for table_label, table_type in result: |
| 254 |
# the relation's own class carries its type_label; table_type is how |
| 255 |
# this database spells the same thing, so it is passed through |
| 256 |
if table_type == "VIEW": |
| 257 |
children.append( |
| 258 |
ViewCatalogItem.from_parent( |
| 259 |
parent=self, |
| 260 |
label=table_label, |
| 261 |
type_name=table_type, |
| 262 |
) |
| 263 |
) |
| 264 |
elif table_type == "LOCAL TEMPORARY": |
| 265 |
children.append( |
| 266 |
TempTableCatalogItem.from_parent( |
| 267 |
parent=self, |
| 268 |
label=table_label, |
| 269 |
type_name=table_type, |
| 270 |
) |
| 271 |
) |
| 272 |
elif table_type == "FOREIGN": |
| 273 |
children.append( |
| 274 |
ForeignCatalogItem.from_parent( |
| 275 |
parent=self, |
| 276 |
label=table_label, |
| 277 |
type_name=table_type, |
| 278 |
) |
| 279 |
) |
| 280 |
else: |
| 281 |
children.append( |
| 282 |
TableCatalogItem.from_parent( |
| 283 |
parent=self, |
| 284 |
label=table_label, |
| 285 |
type_name=table_type, |
| 286 |
) |
| 287 |
) |
| 288 |
|
| 289 |
for (mv_label,) in self.connection._get_mvs(self.label): |
| 290 |
children.append( |
| 291 |
MaterializedViewCatalogItem.from_parent( |
| 292 |
parent=self, |
| 293 |
label=mv_label, |
| 294 |
) |
| 295 |
) |
| 296 |
|
| 297 |
return children |
| 298 |
|
| 299 |
|
| 300 |
class DatabaseCatalogItem(InteractiveCatalogItem["HarlequinPostgresConnection"]): |
| 301 |
INTERACTIONS = [ |
| 302 |
("List Relations (\\d+)", show_list_objects), |
| 303 |
("List Indexes (\\di+)", show_list_indexes), |
| 304 |
("Drop Database", execute_drop_database_statement), |
| 305 |
] |
| 306 |
|
| 307 |
@classmethod |
| 308 |
def from_label( |
| 309 |
cls, label: str, connection: "HarlequinPostgresConnection" |
| 310 |
) -> "DatabaseCatalogItem": |
| 311 |
database_identifier = f'"{label}"' |
| 312 |
return cls( |
| 313 |
qualified_identifier=database_identifier, |
| 314 |
query_name=database_identifier, |
| 315 |
label=label, |
| 316 |
type_label="db", |
| 317 |
connection=connection, |
| 318 |
) |
| 319 |
|
| 320 |
def fetch_children(self) -> list[SchemaCatalogItem]: |
| 321 |
if self.connection is None: |
| 322 |
return [] |
| 323 |
schemas = self.connection._get_schemas(self.label) |
| 324 |
return [ |
| 325 |
SchemaCatalogItem.from_parent( |
| 326 |
parent=self, |
| 327 |
label=schema_label, |
| 328 |
) |
| 329 |
for (schema_label,) in schemas |
| 330 |
] |