|
| 1 | +// |
| 2 | +// SQLImportPlugin.swift |
| 3 | +// SQLImportPlugin |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import SwiftUI |
| 8 | +import TableProPluginKit |
| 9 | + |
| 10 | +@Observable |
| 11 | +final class SQLImportPlugin: ImportFormatPlugin { |
| 12 | + static let pluginName = "SQL Import" |
| 13 | + static let pluginVersion = "1.0.0" |
| 14 | + static let pluginDescription = "Import data from SQL files" |
| 15 | + static let formatId = "sql" |
| 16 | + static let formatDisplayName = "SQL" |
| 17 | + static let acceptedFileExtensions = ["sql", "gz"] |
| 18 | + static let iconName = "doc.text" |
| 19 | + |
| 20 | + var options = SQLImportOptions() |
| 21 | + |
| 22 | + required init() {} |
| 23 | + |
| 24 | + func optionsView() -> AnyView? { |
| 25 | + AnyView(SQLImportOptionsView(plugin: self)) |
| 26 | + } |
| 27 | + |
| 28 | + func performImport( |
| 29 | + source: any PluginImportSource, |
| 30 | + sink: any PluginImportDataSink, |
| 31 | + progress: PluginImportProgress |
| 32 | + ) async throws -> PluginImportResult { |
| 33 | + let startTime = Date() |
| 34 | + var executedCount = 0 |
| 35 | + |
| 36 | + // Estimate total from file size (~500 bytes per statement) |
| 37 | + let fileSizeBytes = source.fileSizeBytes() |
| 38 | + let estimatedTotal = max(1, Int(fileSizeBytes / 500)) |
| 39 | + progress.setEstimatedTotal(estimatedTotal) |
| 40 | + |
| 41 | + do { |
| 42 | + // Disable FK checks if enabled |
| 43 | + if options.disableForeignKeyChecks { |
| 44 | + try await sink.disableForeignKeyChecks() |
| 45 | + } |
| 46 | + |
| 47 | + // Begin transaction if enabled |
| 48 | + if options.wrapInTransaction { |
| 49 | + try await sink.beginTransaction() |
| 50 | + } |
| 51 | + |
| 52 | + // Stream and execute statements |
| 53 | + let stream = try await source.statements() |
| 54 | + |
| 55 | + for try await (statement, lineNumber) in stream { |
| 56 | + try progress.checkCancellation() |
| 57 | + |
| 58 | + do { |
| 59 | + try await sink.execute(statement: statement) |
| 60 | + executedCount += 1 |
| 61 | + progress.incrementStatement() |
| 62 | + } catch { |
| 63 | + throw PluginImportError.statementFailed( |
| 64 | + statement: statement, |
| 65 | + line: lineNumber, |
| 66 | + underlyingError: error |
| 67 | + ) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Commit transaction |
| 72 | + if options.wrapInTransaction { |
| 73 | + try await sink.commitTransaction() |
| 74 | + } |
| 75 | + |
| 76 | + // Re-enable FK checks |
| 77 | + if options.disableForeignKeyChecks { |
| 78 | + try await sink.enableForeignKeyChecks() |
| 79 | + } |
| 80 | + } catch { |
| 81 | + let importError = error |
| 82 | + |
| 83 | + // Rollback on error |
| 84 | + if options.wrapInTransaction { |
| 85 | + do { |
| 86 | + try await sink.rollbackTransaction() |
| 87 | + } catch { |
| 88 | + throw PluginImportError.rollbackFailed(underlyingError: importError) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Re-enable FK checks (best-effort) |
| 93 | + if options.disableForeignKeyChecks { |
| 94 | + try? await sink.enableForeignKeyChecks() |
| 95 | + } |
| 96 | + |
| 97 | + // Re-throw cancellation as-is, wrap others |
| 98 | + if importError is PluginImportCancellationError { |
| 99 | + throw importError |
| 100 | + } |
| 101 | + if importError is PluginImportError { |
| 102 | + throw importError |
| 103 | + } |
| 104 | + throw PluginImportError.importFailed(importError.localizedDescription) |
| 105 | + } |
| 106 | + |
| 107 | + progress.finalize() |
| 108 | + |
| 109 | + return PluginImportResult( |
| 110 | + executedStatements: executedCount, |
| 111 | + executionTime: Date().timeIntervalSince(startTime) |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
0 commit comments