Skip to content

Commit f4b2884

Browse files
committed
feat: replace remaining DatabaseType switches with plugin metadata lookups (#308)
1 parent ccf10a1 commit f4b2884

25 files changed

+345
-300
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Moved identifier quoting, autocomplete statement completions, view templates, and FK disable/enable into plugin system
2222
- Removed `DatabaseType` switches from `FilterSQLGenerator`, `SQLCompletionProvider`, `ImportDataSinkAdapter`, and `MainContentCoordinator+SidebarActions`
2323
- Replaced hardcoded `DatabaseType` switches in ExportDialog, DataChangeManager, SafeModeGuard, ExportService, DataGridView, HighlightedSQLTextView, ForeignKeyPopoverContentView, QueryTab, SQLRowToStatementConverter, SessionStateFactory, ConnectionToolbarState, and DatabaseSwitcherSheet with dynamic plugin lookups (`databaseGroupingStrategy`, `immutableColumns`, `supportsReadOnlyMode`, `paginationStyle`, `editorLanguage`, `connectionMode`, `supportsSchemaSwitching`)
24+
- Replaced remaining ~40 hardcoded `DatabaseType` switches in MainContentCoordinator (main + 5 extensions) and StructureRowProvider with plugin metadata lookups (`requiresReconnectForDatabaseSwitch`, `structureColumnFields`, `defaultPrimaryKeyColumn`, `supportsQueryProgress`, `autoLimitStyle`, `allTablesMetadataSQL`)
2425

2526
### Added
2627

Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ final class ClickHousePlugin: NSObject, TableProPlugin, DriverPlugin {
4242
"Geo": ["Point", "Ring", "Polygon", "MultiPolygon"]
4343
]
4444

45+
static let structureColumnFields: [StructureColumnField] = [.name, .type, .nullable, .defaultValue, .comment]
46+
static let supportsQueryProgress = true
47+
4548
static let sqlDialect: SQLDialectDescriptor? = SQLDialectDescriptor(
4649
identifierQuote: "`",
4750
keywords: [
@@ -571,6 +574,23 @@ final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked Sendable {
571574
_ = try await execute(query: "CREATE DATABASE `\(escapedName)`")
572575
}
573576

577+
// MARK: - All Tables Metadata
578+
579+
func allTablesMetadataSQL(schema: String?) -> String? {
580+
"""
581+
SELECT
582+
database as `schema`,
583+
name,
584+
engine as kind,
585+
total_rows as estimated_rows,
586+
formatReadableSize(total_bytes) as total_size,
587+
comment
588+
FROM system.tables
589+
WHERE database = currentDatabase()
590+
ORDER BY name
591+
"""
592+
}
593+
574594
// MARK: - DML Statement Generation
575595

576596
func generateStatements(

Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,21 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
832832
return "CREATE OR REPLACE VIEW \(quoted) AS\nSELECT * FROM table_name;"
833833
}
834834

835+
// MARK: - All Tables Metadata
836+
837+
func allTablesMetadataSQL(schema: String?) -> String? {
838+
let s = schema ?? currentSchema ?? "main"
839+
return """
840+
SELECT
841+
table_schema as schema_name,
842+
table_name as name,
843+
table_type as kind
844+
FROM information_schema.tables
845+
WHERE table_schema = '\(s)'
846+
ORDER BY table_name
847+
"""
848+
}
849+
835850
// MARK: - Private Helpers
836851

837852
nonisolated private func setInterruptHandle(_ handle: duckdb_connection?) {

Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ final class MSSQLPlugin: NSObject, TableProPlugin, DriverPlugin {
8888
regexSyntax: .unsupported,
8989
booleanLiteralStyle: .numeric,
9090
likeEscapeStyle: .explicit,
91-
paginationStyle: .offsetFetch
91+
paginationStyle: .offsetFetch,
92+
autoLimitStyle: .top
9293
)
9394

9495
func createDriver(config: DriverConnectionConfig) -> any PluginDatabaseDriver {
@@ -1176,6 +1177,27 @@ final class MSSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
11761177
_ = try await execute(query: "CREATE DATABASE \(quotedName)")
11771178
}
11781179

1180+
// MARK: - All Tables Metadata
1181+
1182+
func allTablesMetadataSQL(schema: String?) -> String? {
1183+
"""
1184+
SELECT
1185+
s.name as schema_name,
1186+
t.name as name,
1187+
CASE WHEN v.object_id IS NOT NULL THEN 'VIEW' ELSE 'TABLE' END as kind,
1188+
p.rows as estimated_rows,
1189+
CAST(ROUND(SUM(a.total_pages) * 8 / 1024.0, 2) AS VARCHAR) + ' MB' as total_size
1190+
FROM sys.tables t
1191+
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
1192+
INNER JOIN sys.indexes i ON t.object_id = i.object_id AND i.index_id IN (0, 1)
1193+
INNER JOIN sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
1194+
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
1195+
LEFT JOIN sys.views v ON t.object_id = v.object_id
1196+
GROUP BY s.name, t.name, p.rows, v.object_id
1197+
ORDER BY t.name
1198+
"""
1199+
}
1200+
11791201
// MARK: - Query Building
11801202

11811203
func buildBrowseQuery(

Plugins/MongoDBDriverPlugin/MongoDBPlugin.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ final class MongoDBPlugin: NSObject, TableProPlugin, DriverPlugin {
7070
"Other": ["javascript", "minKey", "maxKey"]
7171
]
7272

73+
static let structureColumnFields: [StructureColumnField] = [.name, .type, .nullable]
74+
static let defaultPrimaryKeyColumn: String? = "_id"
75+
7376
static let sqlDialect: SQLDialectDescriptor? = nil
7477

7578
static var statementCompletions: [CompletionEntry] {

Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,29 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
627627
["SET FOREIGN_KEY_CHECKS=1"]
628628
}
629629

630+
// MARK: - All Tables Metadata
631+
632+
func allTablesMetadataSQL(schema: String?) -> String? {
633+
"""
634+
SELECT
635+
TABLE_SCHEMA as `schema`,
636+
TABLE_NAME as name,
637+
TABLE_TYPE as kind,
638+
IFNULL(CCSA.CHARACTER_SET_NAME, '') as charset,
639+
TABLE_COLLATION as collation,
640+
TABLE_ROWS as estimated_rows,
641+
CONCAT(ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2), ' MB') as total_size,
642+
CONCAT(ROUND(DATA_LENGTH / 1024 / 1024, 2), ' MB') as data_size,
643+
CONCAT(ROUND(INDEX_LENGTH / 1024 / 1024, 2), ' MB') as index_size,
644+
TABLE_COMMENT as comment
645+
FROM information_schema.TABLES
646+
LEFT JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA
647+
ON TABLE_COLLATION = CCSA.COLLATION_NAME
648+
WHERE TABLE_SCHEMA = DATABASE()
649+
ORDER BY TABLE_NAME
650+
"""
651+
}
652+
630653
// MARK: - Private Helpers
631654

632655
private func extractTableName(from query: String) -> String? {

Plugins/OracleDriverPlugin/OraclePlugin.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ final class OraclePlugin: NSObject, TableProPlugin, DriverPlugin {
8888
booleanLiteralStyle: .numeric,
8989
likeEscapeStyle: .explicit,
9090
paginationStyle: .offsetFetch,
91-
offsetFetchOrderBy: "ORDER BY 1"
91+
offsetFetchOrderBy: "ORDER BY 1",
92+
autoLimitStyle: .fetchFirst
9293
)
9394

9495
func createDriver(config: DriverConnectionConfig) -> any PluginDatabaseDriver {
@@ -745,6 +746,22 @@ final class OraclePluginDriver: PluginDatabaseDriver, @unchecked Sendable {
745746
_currentSchema = schema
746747
}
747748

749+
// MARK: - All Tables Metadata
750+
751+
func allTablesMetadataSQL(schema: String?) -> String? {
752+
let s = schema ?? currentSchema ?? "SYSTEM"
753+
return """
754+
SELECT
755+
OWNER as schema_name,
756+
TABLE_NAME as name,
757+
'TABLE' as kind,
758+
NUM_ROWS as estimated_rows
759+
FROM ALL_TABLES
760+
WHERE OWNER = '\(s)'
761+
ORDER BY TABLE_NAME
762+
"""
763+
}
764+
748765
// MARK: - Query Building
749766

750767
func buildBrowseQuery(

Plugins/PostgreSQLDriverPlugin/PostgreSQLPlugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ final class PostgreSQLPlugin: NSObject, TableProPlugin, DriverPlugin {
5050

5151
static let supportsCascadeDrop = true
5252
static let supportsForeignKeyDisable = false
53+
static let requiresReconnectForDatabaseSwitch = true
5354

5455
static let sqlDialect: SQLDialectDescriptor? = SQLDialectDescriptor(
5556
identifierQuote: "\"",

Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,26 @@ final class PostgreSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
749749
_ = try await execute(query: query)
750750
}
751751

752+
// MARK: - All Tables Metadata
753+
754+
func allTablesMetadataSQL(schema: String?) -> String? {
755+
let s = schema ?? currentSchema ?? "public"
756+
return """
757+
SELECT
758+
schemaname as schema,
759+
relname as name,
760+
'TABLE' as kind,
761+
n_live_tup as estimated_rows,
762+
pg_size_pretty(pg_total_relation_size(schemaname||'.'||relname)) as total_size,
763+
pg_size_pretty(pg_relation_size(schemaname||'.'||relname)) as data_size,
764+
pg_size_pretty(pg_indexes_size(schemaname||'.'||relname)) as index_size,
765+
obj_description((schemaname||'.'||relname)::regclass) as comment
766+
FROM pg_stat_user_tables
767+
WHERE schemaname = '\(s)'
768+
ORDER BY relname
769+
"""
770+
}
771+
752772
// MARK: - Helpers
753773

754774
private func stripLimitOffset(from query: String) -> String {

Plugins/PostgreSQLDriverPlugin/RedshiftPluginDriver.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,26 @@ final class RedshiftPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
642642
_ = try await execute(query: query)
643643
}
644644

645+
// MARK: - All Tables Metadata
646+
647+
func allTablesMetadataSQL(schema: String?) -> String? {
648+
let s = schema ?? currentSchema ?? "public"
649+
return """
650+
SELECT
651+
schema,
652+
"table" as name,
653+
'TABLE' as kind,
654+
tbl_rows as estimated_rows,
655+
size as size_mb,
656+
pct_used,
657+
unsorted,
658+
stats_off
659+
FROM svv_table_info
660+
WHERE schema = '\(s)'
661+
ORDER BY "table"
662+
"""
663+
}
664+
645665
// MARK: - Helpers
646666

647667
private func stripLimitOffset(from query: String) -> String {

0 commit comments

Comments
 (0)