|
| 1 | +// |
| 2 | +// MainContentCoordinator+SidebarActions.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | +// Sidebar context menu actions for MainContentCoordinator. |
| 6 | +// |
| 7 | + |
| 8 | +import AppKit |
| 9 | +import Foundation |
| 10 | +import UniformTypeIdentifiers |
| 11 | + |
| 12 | +extension MainContentCoordinator { |
| 13 | + // MARK: - View Operations |
| 14 | + |
| 15 | + func createView() { |
| 16 | + guard !connection.safeModeLevel.blocksAllWrites else { return } |
| 17 | + |
| 18 | + let template: String |
| 19 | + switch connection.type { |
| 20 | + case .postgresql, .redshift, .duckdb: |
| 21 | + template = "CREATE OR REPLACE VIEW view_name AS\nSELECT column1, column2\nFROM table_name\nWHERE condition;" |
| 22 | + case .mysql, .mariadb, .clickhouse: |
| 23 | + template = "CREATE VIEW view_name AS\nSELECT column1, column2\nFROM table_name\nWHERE condition;" |
| 24 | + case .sqlite: |
| 25 | + template = "CREATE VIEW IF NOT EXISTS view_name AS\nSELECT column1, column2\nFROM table_name\nWHERE condition;" |
| 26 | + case .mssql: |
| 27 | + template = "CREATE OR ALTER VIEW view_name AS\nSELECT column1, column2\nFROM table_name\nWHERE condition;" |
| 28 | + case .oracle: |
| 29 | + template = "CREATE OR REPLACE VIEW view_name AS\nSELECT column1, column2\nFROM table_name\nWHERE condition;" |
| 30 | + case .mongodb: |
| 31 | + template = "db.createView(\"view_name\", \"source_collection\", [\n {\"$match\": {}},\n {\"$project\": {\"_id\": 1}}\n])" |
| 32 | + case .redis: |
| 33 | + template = "-- Redis does not support views" |
| 34 | + } |
| 35 | + |
| 36 | + let payload = EditorTabPayload( |
| 37 | + connectionId: connection.id, |
| 38 | + tabType: .query, |
| 39 | + databaseName: connection.database, |
| 40 | + initialQuery: template |
| 41 | + ) |
| 42 | + WindowOpener.shared.openNativeTab(payload) |
| 43 | + } |
| 44 | + |
| 45 | + func editViewDefinition(_ viewName: String) { |
| 46 | + Task { @MainActor in |
| 47 | + do { |
| 48 | + guard let driver = DatabaseManager.shared.driver(for: self.connection.id) else { return } |
| 49 | + let definition = try await driver.fetchViewDefinition(view: viewName) |
| 50 | + |
| 51 | + let payload = EditorTabPayload( |
| 52 | + connectionId: connection.id, |
| 53 | + tabType: .query, |
| 54 | + initialQuery: definition |
| 55 | + ) |
| 56 | + WindowOpener.shared.openNativeTab(payload) |
| 57 | + } catch { |
| 58 | + let fallbackSQL: String |
| 59 | + switch connection.type { |
| 60 | + case .postgresql, .redshift, .duckdb: |
| 61 | + fallbackSQL = "CREATE OR REPLACE VIEW \(viewName) AS\n-- Could not fetch view definition: \(error.localizedDescription)\nSELECT * FROM table_name;" |
| 62 | + case .mysql, .mariadb, .clickhouse: |
| 63 | + fallbackSQL = "ALTER VIEW \(viewName) AS\n-- Could not fetch view definition: \(error.localizedDescription)\nSELECT * FROM table_name;" |
| 64 | + case .sqlite: |
| 65 | + fallbackSQL = "-- SQLite does not support ALTER VIEW. Drop and recreate:\nDROP VIEW IF EXISTS \(viewName);\nCREATE VIEW \(viewName) AS\nSELECT * FROM table_name;" |
| 66 | + case .mssql: |
| 67 | + fallbackSQL = "CREATE OR ALTER VIEW \(viewName) AS\n-- Could not fetch view definition: \(error.localizedDescription)\nSELECT * FROM table_name;" |
| 68 | + case .oracle: |
| 69 | + fallbackSQL = "CREATE OR REPLACE VIEW \(viewName) AS\n-- Could not fetch view definition: \(error.localizedDescription)\nSELECT * FROM table_name;" |
| 70 | + case .mongodb: |
| 71 | + fallbackSQL = "db.runCommand({\"collMod\": \"\(viewName)\", \"viewOn\": \"source_collection\", \"pipeline\": [{\"$match\": {}}]})" |
| 72 | + case .redis: |
| 73 | + fallbackSQL = "-- Redis does not support views" |
| 74 | + } |
| 75 | + |
| 76 | + let payload = EditorTabPayload( |
| 77 | + connectionId: connection.id, |
| 78 | + tabType: .query, |
| 79 | + initialQuery: fallbackSQL |
| 80 | + ) |
| 81 | + WindowOpener.shared.openNativeTab(payload) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // MARK: - Export/Import |
| 87 | + |
| 88 | + func openExportDialog() { |
| 89 | + activeSheet = .exportDialog |
| 90 | + } |
| 91 | + |
| 92 | + func openImportDialog() { |
| 93 | + guard !connection.safeModeLevel.blocksAllWrites else { return } |
| 94 | + guard connection.type != .mongodb && connection.type != .redis else { |
| 95 | + let typeName = connection.type == .mongodb ? "MongoDB" : "Redis" |
| 96 | + AlertHelper.showErrorSheet( |
| 97 | + title: String(localized: "Import Not Supported"), |
| 98 | + message: String(localized: "SQL import is not supported for \(typeName) connections."), |
| 99 | + window: nil |
| 100 | + ) |
| 101 | + return |
| 102 | + } |
| 103 | + let panel = NSOpenPanel() |
| 104 | + var contentTypes: [UTType] = [] |
| 105 | + if let sqlType = UTType(filenameExtension: "sql") { |
| 106 | + contentTypes.append(sqlType) |
| 107 | + } |
| 108 | + if let gzType = UTType(filenameExtension: "gz") { |
| 109 | + contentTypes.append(gzType) |
| 110 | + } |
| 111 | + if !contentTypes.isEmpty { |
| 112 | + panel.allowedContentTypes = contentTypes |
| 113 | + } |
| 114 | + panel.allowsMultipleSelection = false |
| 115 | + panel.message = "Select SQL file to import" |
| 116 | + |
| 117 | + panel.begin { [weak self] response in |
| 118 | + guard response == .OK, let url = panel.url else { return } |
| 119 | + self?.importFileURL = url |
| 120 | + self?.activeSheet = .importDialog |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments