| 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_sqlite.interactions import ( |
| 8 |
execute_drop_table_statement, |
| 9 |
execute_drop_view_statement, |
| 10 |
insert_columns_at_cursor, |
| 11 |
show_describe_relation, |
| 12 |
show_relation_ddl, |
| 13 |
show_select_star, |
| 14 |
) |
| 15 |
|
| 16 |
if TYPE_CHECKING: |
| 17 |
from harlequin_sqlite.adapter import HarlequinSqliteConnection |
| 18 |
|
| 19 |
|
| 20 |
@dataclass |
| 21 |
class ColumnCatalogItem(InteractiveCatalogItem["HarlequinSqliteConnection"]): |
| 22 |
parent: "RelationCatalogItem" | None = None |
| 23 |
|
| 24 |
@classmethod |
| 25 |
def from_parent( |
| 26 |
cls, |
| 27 |
parent: "RelationCatalogItem", |
| 28 |
label: str, |
| 29 |
type_label: str, |
| 30 |
type_name: str | None, |
| 31 |
) -> "ColumnCatalogItem": |
| 32 |
column_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 33 |
column_query_name = f'"{label}"' |
| 34 |
return cls( |
| 35 |
qualified_identifier=column_qualified_identifier, |
| 36 |
query_name=column_query_name, |
| 37 |
label=label, |
| 38 |
type_label=type_label, |
| 39 |
type_name=type_name, |
| 40 |
connection=parent.connection, |
| 41 |
parent=parent, |
| 42 |
loaded=True, |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
@dataclass |
| 47 |
class RelationCatalogItem(InteractiveCatalogItem["HarlequinSqliteConnection"]): |
| 48 |
INTERACTIONS = [ |
| 49 |
("Insert Columns at Cursor", insert_columns_at_cursor), |
| 50 |
("Preview Data", show_select_star), |
| 51 |
("Describe", show_describe_relation), |
| 52 |
("Show DDL", show_relation_ddl), |
| 53 |
] |
| 54 |
parent: "DatabaseCatalogItem" | None = None |
| 55 |
|
| 56 |
def fetch_children(self) -> list[ColumnCatalogItem]: |
| 57 |
if self.parent is None is None or self.connection is None: |
| 58 |
return [] |
| 59 |
result = self.connection._get_columns(self.parent.label, self.label) |
| 60 |
return [ |
| 61 |
ColumnCatalogItem.from_parent( |
| 62 |
parent=self, |
| 63 |
label=column_name, |
| 64 |
type_label=self.connection._short_column_type(column_type), |
| 65 |
# a column can be declared without a type, and an empty string |
| 66 |
# is not one |
| 67 |
type_name=column_type or None, |
| 68 |
) |
| 69 |
for _, column_name, column_type, *_ in result |
| 70 |
] |
| 71 |
|
| 72 |
|
| 73 |
class ViewCatalogItem(RelationCatalogItem): |
| 74 |
INTERACTIONS = RelationCatalogItem.INTERACTIONS + [ |
| 75 |
("Drop View", execute_drop_view_statement), |
| 76 |
] |
| 77 |
|
| 78 |
@classmethod |
| 79 |
def from_parent( |
| 80 |
cls, |
| 81 |
parent: "DatabaseCatalogItem", |
| 82 |
label: str, |
| 83 |
type_name: str, |
| 84 |
) -> "ViewCatalogItem": |
| 85 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 86 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 87 |
return cls( |
| 88 |
qualified_identifier=relation_qualified_identifier, |
| 89 |
query_name=relation_query_name, |
| 90 |
label=label, |
| 91 |
type_label="v", |
| 92 |
type_name=type_name, |
| 93 |
connection=parent.connection, |
| 94 |
parent=parent, |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
class TableCatalogItem(RelationCatalogItem): |
| 99 |
INTERACTIONS = RelationCatalogItem.INTERACTIONS + [ |
| 100 |
("Drop Table", execute_drop_table_statement), |
| 101 |
] |
| 102 |
|
| 103 |
@classmethod |
| 104 |
def from_parent( |
| 105 |
cls, |
| 106 |
parent: "DatabaseCatalogItem", |
| 107 |
label: str, |
| 108 |
type_name: str, |
| 109 |
) -> "TableCatalogItem": |
| 110 |
relation_query_name = f'"{parent.label}"."{label}"' |
| 111 |
relation_qualified_identifier = f'{parent.qualified_identifier}."{label}"' |
| 112 |
return cls( |
| 113 |
qualified_identifier=relation_qualified_identifier, |
| 114 |
query_name=relation_query_name, |
| 115 |
label=label, |
| 116 |
type_label="t", |
| 117 |
type_name=type_name, |
| 118 |
connection=parent.connection, |
| 119 |
parent=parent, |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
class DatabaseCatalogItem(InteractiveCatalogItem["HarlequinSqliteConnection"]): |
| 124 |
INTERACTIONS = [] |
| 125 |
|
| 126 |
@classmethod |
| 127 |
def from_label( |
| 128 |
cls, label: str, connection: "HarlequinSqliteConnection" |
| 129 |
) -> "DatabaseCatalogItem": |
| 130 |
database_identifier = f'"{label}"' |
| 131 |
return cls( |
| 132 |
qualified_identifier=database_identifier, |
| 133 |
query_name=database_identifier, |
| 134 |
label=label, |
| 135 |
type_label="db", |
| 136 |
type_name="database", |
| 137 |
connection=connection, |
| 138 |
) |
| 139 |
|
| 140 |
def fetch_children(self) -> list[RelationCatalogItem]: |
| 141 |
if self.connection is None: |
| 142 |
return [] |
| 143 |
children: list[RelationCatalogItem] = [] |
| 144 |
result = self.connection._get_relations(self.label) |
| 145 |
for table_label, table_type in result: |
| 146 |
if table_type == "view": |
| 147 |
children.append( |
| 148 |
ViewCatalogItem.from_parent( |
| 149 |
parent=self, |
| 150 |
label=table_label, |
| 151 |
type_name=table_type, |
| 152 |
) |
| 153 |
) |
| 154 |
else: |
| 155 |
children.append( |
| 156 |
TableCatalogItem.from_parent( |
| 157 |
parent=self, |
| 158 |
label=table_label, |
| 159 |
type_name=table_type, |
| 160 |
) |
| 161 |
) |
| 162 |
|
| 163 |
return children |