Skip to content

Commit 55472d8

Browse files
authored
feat(export): set Structure, Drop or Data for a whole database or group (#3857)
1 parent 165c22b commit 55472d8

28 files changed

Lines changed: 1190 additions & 186 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// MQLExportReadsRowsTests.swift
3+
// FormatPluginTests
4+
//
5+
6+
import TableProPluginKit
7+
import Testing
8+
9+
struct MQLExportReadsRowsTests {
10+
@Test("An MQL export reads a collection's documents only with Data checked")
11+
func readsRowsOnlyWithData() {
12+
let plugin = MQLExportPlugin()
13+
#expect(plugin.readsRows(optionValues: [true, true, true], kind: .table))
14+
#expect(!plugin.readsRows(optionValues: [true, true, false], kind: .table))
15+
#expect(plugin.readsRows(optionValues: [], kind: .table))
16+
}
17+
}

‎apple/Plugins/MQLExportPlugin/MQLExportPlugin.swift‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ final class MQLExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi
1919
static let supportedDatabaseTypeIds = ["MongoDB"]
2020

2121
static let perTableOptionColumns: [PluginExportOptionColumn] = [
22+
// The DROP statement, left untranslated: the catalog's "Drop" is the verb that deletes a table.
2223
PluginExportOptionColumn(id: "drop", label: "Drop", width: 44),
23-
PluginExportOptionColumn(id: "indexes", label: "Indexes", width: 44),
24-
PluginExportOptionColumn(id: "data", label: "Data", width: 44)
24+
PluginExportOptionColumn(id: "indexes", label: String(localized: "Indexes", bundle: .main), width: 44),
25+
PluginExportOptionColumn(id: "data", label: String(localized: "Data", bundle: .main), width: 44)
2526
]
2627

2728
typealias Settings = MQLExportOptions
@@ -41,6 +42,11 @@ final class MQLExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi
4142
optionValues.contains(true)
4243
}
4344

45+
// Without it, Data off still counts every collection's rows and the progress sits at 0 of N.
46+
func readsRows(optionValues: [Bool], kind: PluginExportObjectKind) -> Bool {
47+
optionValues.count <= 2 || optionValues[2]
48+
}
49+
4450
@MainActor
4551
func settingsView() -> AnyView? {
4652
AnyView(MQLExportOptionsView(plugin: self))

‎apple/Plugins/SQLExportPlugin/SQLExportPlugin.swift‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ final class SQLExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi
2020
static let excludedDatabaseTypeIds = ["MongoDB", "Redis"]
2121

2222
static let perTableOptionColumns: [PluginExportOptionColumn] = [
23-
PluginExportOptionColumn(id: "structure", label: "Structure", width: 56),
23+
PluginExportOptionColumn(id: "structure", label: String(localized: "Structure", bundle: .main), width: 56),
24+
// The DROP statement, left untranslated: the catalog's "Drop" is the verb that deletes a table.
2425
PluginExportOptionColumn(id: "drop", label: "Drop", width: 44),
25-
PluginExportOptionColumn(id: "data", label: "Data", width: 44)
26+
PluginExportOptionColumn(id: "data", label: String(localized: "Data", bundle: .main), width: 44)
2627
]
2728

2829
static let supportedObjectKinds: [PluginExportObjectKind] = [

‎apple/TablePro/Models/Export/ExportModels.swift‎

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ struct ExportDatabaseItem: Identifiable {
292292
/// Set when the container's own checkbox was turned on before its unread kinds arrived, so
293293
/// they arrive selected like everything else the click reached.
294294
var selectsLoadedObjects = false
295+
// Column values set before the unread kinds arrived, by column index; they arrive with them.
296+
var arrivingOptionValues: [Int: Bool] = [:]
295297

296298
init(
297299
id: UUID = UUID(),
@@ -313,8 +315,12 @@ struct ExportDatabaseItem: Identifiable {
313315

314316
/// An object that arrives already selected (a restored row) stays so. Any other is selected
315317
/// only when the container was ticked whole and nothing in it has been unticked since.
316-
func mergingLoadedObjects(_ loaded: [ExportObjectItem]) -> ExportDatabaseItem {
317-
let selectsArrivals = selectsLoadedObjects && objects.allSatisfy(\.isSelected)
318+
func mergingLoadedObjects(
319+
_ loaded: [ExportObjectItem],
320+
writes: (ExportObjectItem) -> Bool = { _ in true }
321+
) -> ExportDatabaseItem {
322+
// An object that writes nothing stays out of a container tick, so it does not count as unticked.
323+
let selectsArrivals = selectsLoadedObjects && objects.filter(writes).allSatisfy(\.isSelected)
318324
let incoming = loaded.filter { unloadedKinds.contains($0.kind) }
319325
// The table listing reported these sequences as tables; one relation, so the sequence replaces it.
320326
let sequenceNames = Set(incoming.filter { $0.kind == .sequence }.map(\.name))
@@ -331,9 +337,32 @@ struct ExportDatabaseItem: Identifiable {
331337
merged.unloadedKinds = []
332338
merged.isLoadingObjects = false
333339
merged.selectsLoadedObjects = false
340+
merged.arrivingOptionValues = [:]
334341
return merged
335342
}
336343

344+
// Kept values go on after normalizing, which replaces an arrival's empty list with the defaults.
345+
func mergingLoadedObjects(_ loaded: [ExportObjectItem], layout: ExportOptionLayout) -> ExportDatabaseItem? {
346+
let merged = mergingLoadedObjects(loaded, writes: layout.writesSomething)
347+
guard !merged.objects.isEmpty, let normalized = [merged].normalized(for: layout).first else { return nil }
348+
let arrivals = Set(normalized.objects.map(\.id)).subtracting(objects.map(\.id))
349+
return normalized.applyingOptionValues(arrivingOptionValues, toObjects: arrivals, layout: layout)
350+
}
351+
352+
func applyingOptionValues(
353+
_ values: [Int: Bool],
354+
toObjects ids: Set<UUID>,
355+
layout: ExportOptionLayout
356+
) -> ExportDatabaseItem {
357+
guard !values.isEmpty, !ids.isEmpty else { return self }
358+
var updated = self
359+
updated.objects = objects.map { object in
360+
guard ids.contains(object.id) else { return object }
361+
return object.settingOptions(values.filter { layout.supports(columnAt: $0.key, kind: object.kind) })
362+
}
363+
return updated
364+
}
365+
337366
func withID(_ id: UUID) -> ExportDatabaseItem {
338367
var copy = ExportDatabaseItem(
339368
id: id,
@@ -344,6 +373,7 @@ struct ExportDatabaseItem: Identifiable {
344373
)
345374
copy.isLoadingObjects = isLoadingObjects
346375
copy.selectsLoadedObjects = selectsLoadedObjects
376+
copy.arrivingOptionValues = arrivingOptionValues
347377
return copy
348378
}
349379

@@ -377,7 +407,75 @@ struct ExportDatabaseItem: Identifiable {
377407
}
378408
}
379409

410+
enum ExportKindReconciliation: Equatable {
411+
case unchanged
412+
// The kinds left after narrowing, so a wider format later reads again.
413+
case narrowed(loadedKinds: Set<PluginExportObjectKind>)
414+
case reload
415+
416+
static func next(
417+
loadedKinds: Set<PluginExportObjectKind>,
418+
wanted: Set<PluginExportObjectKind>
419+
) -> ExportKindReconciliation {
420+
guard !loadedKinds.isEmpty else { return .unchanged }
421+
guard wanted.isSubset(of: loadedKinds) else { return .reload }
422+
return wanted == loadedKinds ? .unchanged : .narrowed(loadedKinds: wanted)
423+
}
424+
}
425+
426+
// Resolved by the dialog so the tree never looks a plugin up itself.
427+
struct ExportOptionLayout {
428+
let columns: [PluginExportOptionColumn]
429+
let defaultValues: [Bool]
430+
// False for a structure-only format, which ignores a row scope.
431+
let readsRows: Bool
432+
let supports: (String, PluginExportObjectKind) -> Bool
433+
434+
static var none: ExportOptionLayout {
435+
ExportOptionLayout(columns: [], defaultValues: [], readsRows: true) { _, _ in true }
436+
}
437+
438+
func supports(columnAt index: Int, kind: PluginExportObjectKind) -> Bool {
439+
columns.indices.contains(index) && supports(columns[index].id, kind)
440+
}
441+
442+
func writesSomething(_ object: ExportObjectItem) -> Bool {
443+
columns.isEmpty || object.optionValues.contains(true)
444+
}
445+
446+
// Unread kinds count with the value they will arrive with.
447+
func values(
448+
ofColumn index: Int,
449+
in objects: [ExportObjectItem],
450+
unloadedKinds: Set<PluginExportObjectKind> = [],
451+
arriving: [Int: Bool] = [:]
452+
) -> [Bool] {
453+
guard columns.indices.contains(index) else { return [] }
454+
let fallback = defaultValues.count == columns.count ? defaultValues[index] : columns[index].defaultValue
455+
var values = objects
456+
.filter { supports(columnAt: index, kind: $0.kind) }
457+
.map { $0.optionValues[safe: index] ?? columns[index].defaultValue }
458+
if unloadedKinds.contains(where: { supports(columnAt: index, kind: $0) }) {
459+
values.append(arriving[index] ?? fallback)
460+
}
461+
return values
462+
}
463+
}
464+
380465
extension ExportObjectItem {
466+
// Never selects. A selected object left with nothing on is deselected: it would export nothing.
467+
func settingOptions(_ values: [Int: Bool]) -> ExportObjectItem {
468+
guard values.keys.contains(where: optionValues.indices.contains) else { return self }
469+
var updated = self
470+
for (index, value) in values where updated.optionValues.indices.contains(index) {
471+
updated.optionValues[index] = value
472+
}
473+
if !updated.optionValues.contains(true) {
474+
updated.isSelected = false
475+
}
476+
return updated
477+
}
478+
381479
func normalized(forOptionColumnCount optionColumnCount: Int, defaultOptionValues: [Bool]) -> ExportObjectItem {
382480
guard optionColumnCount > 0 else { return self }
383481
let fallback = defaultOptionValues.count == optionColumnCount
@@ -451,6 +549,23 @@ extension [ExportDatabaseItem] {
451549
}
452550
}
453551

552+
// Masking keeps a routine from carrying a Data flag that would count it as exportable.
553+
func normalized(for layout: ExportOptionLayout) -> [ExportDatabaseItem] {
554+
normalizingOptionValues(optionColumnCount: layout.columns.count, defaultOptionValues: layout.defaultValues)
555+
.maskingUnsupportedOptions(columns: layout.columns, supports: layout.supports)
556+
}
557+
558+
// A profile's option values belong to its format, so the tree is reset for that format first.
559+
func applyingProfile(
560+
_ profile: ExportProfile,
561+
layout: ExportOptionLayout,
562+
databaseType: DatabaseType
563+
) -> [ExportDatabaseItem] {
564+
ExportProfileStorage.apply(profile, to: self)
565+
.clearingRowScopes(unavailableOn: databaseType)
566+
.normalized(for: layout)
567+
}
568+
454569
func normalizingOptionValues(optionColumnCount: Int, defaultOptionValues: [Bool]) -> [ExportDatabaseItem] {
455570
map { database in
456571
var normalizedDatabase = database
@@ -464,6 +579,7 @@ extension [ExportDatabaseItem] {
464579
func resettingOptionValues(to values: [Bool]) -> [ExportDatabaseItem] {
465580
map { database in
466581
var resetDatabase = database
582+
resetDatabase.arrivingOptionValues = [:]
467583
resetDatabase.objects = database.objects.map { object in
468584
var resetObject = object
469585
resetObject.optionValues = values
@@ -486,6 +602,24 @@ extension [ExportDatabaseItem] {
486602
}
487603
}
488604

605+
func settingOption(
606+
_ index: Int,
607+
to value: Bool,
608+
objectIDs ids: Set<UUID>,
609+
arrivingIn databaseID: UUID? = nil
610+
) -> [ExportDatabaseItem] {
611+
map { database in
612+
var updated = database
613+
updated.objects = database.objects.map { object in
614+
ids.contains(object.id) ? object.settingOptions([index: value]) : object
615+
}
616+
if database.id == databaseID {
617+
updated.arrivingOptionValues[index] = value
618+
}
619+
return updated
620+
}
621+
}
622+
489623
func clearingRowScopes(unavailableOn databaseType: DatabaseType) -> [ExportDatabaseItem] {
490624
map { database in
491625
var clearedDatabase = database

‎apple/TablePro/Resources/Localizable.xcstrings‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57725,6 +57725,8 @@
5772557725
}
5772657726
},
5772757727
"Data" : {
57728+
"extractionState" : "manual",
57729+
"generatesSymbol" : false,
5772857730
"localizations" : {
5772957731
"fr" : {
5773057732
"stringUnit" : {
@@ -93693,6 +93695,8 @@
9369393695
}
9369493696
},
9369593697
"Indexes" : {
93698+
"extractionState" : "manual",
93699+
"generatesSymbol" : false,
9369693700
"localizations" : {
9369793701
"fr" : {
9369893702
"stringUnit" : {
@@ -173994,6 +173998,8 @@
173994173998
}
173995173999
},
173996174000
"Structure" : {
174001+
"extractionState" : "manual",
174002+
"generatesSymbol" : false,
173997174003
"localizations" : {
173998174004
"fr" : {
173999174005
"stringUnit" : {

‎apple/TablePro/Views/Backup/BackupScopeModel.swift‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,33 +115,89 @@ final class BackupScopeModel: ObservableObject {
115115
rows = Self.togglingObject(id, in: database, rows: rows)
116116
}
117117

118+
enum Row: Equatable {
119+
case database(String)
120+
case object(String, database: String)
121+
}
122+
123+
func toggle(_ selection: [Row]) {
124+
rows = Self.toggling(selection, in: rows)
125+
}
126+
127+
// One direction for every row, and an object under a selected database is left to it: toggling both flipped it back.
128+
static func toggling(_ selection: [Row], in rows: [DatabaseRow]) -> [DatabaseRow] {
129+
let databases = Set(selection.compactMap { row -> String? in
130+
guard case .database(let name) = row else { return nil }
131+
return name
132+
})
133+
let targets = selection.filter { row in
134+
guard case .object(_, let database) = row else { return true }
135+
return !databases.contains(database)
136+
}
137+
let turningOn = targets.contains { target in
138+
switch target {
139+
case .database(let name):
140+
return rows.first { $0.name == name }.map { !$0.isSelected || $0.isMixed } ?? false
141+
case .object(let id, let database):
142+
return rows.first { $0.name == database }?.objects.first { $0.id == id }.map { !$0.isSelected } ?? false
143+
}
144+
}
145+
return targets.reduce(rows) { rows, target in
146+
switch target {
147+
case .database(let name):
148+
return settingDatabase(name, selected: turningOn, in: rows)
149+
case .object(let id, let database):
150+
return settingObject(id, in: database, selected: turningOn, rows: rows)
151+
}
152+
}
153+
}
154+
118155
/// Ticking a mixed database means "all of it", which is the state a user reaches by unticking
119156
/// a few tables and then changing their mind.
120157
static func togglingDatabase(
121158
_ database: String,
122159
in rows: [DatabaseRow]
160+
) -> [DatabaseRow] {
161+
guard let row = rows.first(where: { $0.name == database }) else { return rows }
162+
return settingDatabase(database, selected: !row.isSelected || row.isMixed, in: rows)
163+
}
164+
165+
static func settingDatabase(
166+
_ database: String,
167+
selected: Bool,
168+
in rows: [DatabaseRow]
123169
) -> [DatabaseRow] {
124170
var rows = rows
125171
guard let index = rows.firstIndex(where: { $0.name == database }) else { return rows }
126-
let turningOn = !rows[index].isSelected || rows[index].isMixed
127-
rows[index].isSelected = turningOn
172+
rows[index].isSelected = selected
128173
for objectIndex in rows[index].objects.indices {
129-
rows[index].objects[objectIndex].isSelected = turningOn
174+
rows[index].objects[objectIndex].isSelected = selected
130175
}
131176
return rows
132177
}
133178

179+
static func togglingObject(
180+
_ id: String,
181+
in database: String,
182+
rows: [DatabaseRow]
183+
) -> [DatabaseRow] {
184+
guard let object = rows.first(where: { $0.name == database })?.objects.first(where: { $0.id == id })
185+
else { return rows }
186+
return settingObject(id, in: database, selected: !object.isSelected, rows: rows)
187+
}
188+
134189
/// Unticking the last object is a decision to leave the database out, not a decision to back it
135190
/// up empty. Ticking one again brings the database back carrying only that object.
136-
static func togglingObject(
191+
static func settingObject(
137192
_ id: String,
138193
in database: String,
194+
selected: Bool,
139195
rows: [DatabaseRow]
140196
) -> [DatabaseRow] {
141197
var rows = rows
142198
guard let index = rows.firstIndex(where: { $0.name == database }) else { return rows }
143199
guard let objectIndex = rows[index].objects.firstIndex(where: { $0.id == id }) else { return rows }
144-
rows[index].objects[objectIndex].isSelected.toggle()
200+
rows[index].objects[objectIndex].isSelected = selected
145201
rows[index].isSelected = rows[index].objects.contains(where: \.isSelected)
146202
return rows
147203
}

0 commit comments

Comments
 (0)