Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 52 additions & 5 deletions apple/TablePro/Core/ChangeTracking/DataChangeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ final class DataChangeManager: ObservableObject, ChangeManaging {
wasExpression: pending.isExpression(rowID: rowID, columnIndex: columnIndex),
isExpression: isExpression
)
// The grid's row shows the row's other pending cells; an undo that has to stage the row again needs it as read.
var absence = absence
let originalRow = originalRow.map { shown in
let image = pending.imageAsRead(ofRow: rowID, current: shown, absentColumns: absence.originalRow)
absence.originalRow = image.absentColumns
return image.row
}
let action = coalescesWithPrevious ? typingRunAction() : commandAction ?? Self.newActionID()
objectWillChange.send()
let recorded = pending.recordCellChange(
Expand Down Expand Up @@ -571,12 +578,49 @@ final class DataChangeManager: ObservableObject, ChangeManaging {
// MARK: - Undo Operations

func undoRowDeletion(rowID: RowID) {
guard pending.isRowDeleted(rowID) else { return }
restoreDeletedRows([rowID])
}

// A step of its own, so Undo deletes the rows again rather than replaying the delete under it the wrong way round.
func restoreDeletedRows(_ rowIDs: [RowID]) {
let restored = rowIDs.compactMap { rowID -> (rowID: RowID, originalRow: [PluginCellValue], absent: Set<Int>)? in
guard let delete = pending.change(forRow: rowID, type: .delete) else { return nil }
return (rowID, completedImage(delete.originalRow ?? [], of: rowID), delete.absentColumns)
}
guard !restored.isEmpty else { return }
objectWillChange.send()
guard pending.undoRowDeletion(rowID: rowID) else { return }
for row in restored {
_ = pending.undoRowDeletion(rowID: row.rowID)
}
if restored.count == 1, let row = restored.first {
registerUndo(actionName: Self.restoreRowActionName) { target in
target.applyDataUndo(.rowDeletion(rowID: row.rowID, originalRow: row.originalRow, absentColumns: row.absent))
}
} else {
let rows = restored.map { (rowID: $0.rowID, originalRow: $0.originalRow) }
let absentColumns = Dictionary(
restored.filter { !$0.absent.isEmpty }.map { ($0.rowID, $0.absent) },
uniquingKeysWith: { first, _ in first }
)
registerUndo(actionName: Self.restoreRowsActionName) { target in
target.applyDataUndo(.batchRowDeletion(rows: rows, absentColumns: absentColumns))
}
}
hasChanges = !pending.isEmpty
}

private static var restoreRowActionName: String { String(localized: "Restore Row") }
private static var restoreRowsActionName: String { String(localized: "Restore Rows") }

// A delete and a restore replay through one step, which keeps the name of the command that made it.
private func rowDeletionStepName(rowIsDeleted: Bool, batch: Bool) -> String {
let isUndoing = undoManagerProvider?()?.isUndoing ?? false
if isUndoing == rowIsDeleted {
return batch ? String(localized: "Delete Rows") : String(localized: "Delete Row")
}
return batch ? Self.restoreRowsActionName : Self.restoreRowActionName
}

func undoBatchRowInsertion(rows: [InsertedRowLocation]) {
let validRows = rows.filter { pending.isRowInserted($0.rowID) }
guard !validRows.isEmpty else { return }
Expand Down Expand Up @@ -668,7 +712,8 @@ final class DataChangeManager: ObservableObject, ChangeManaging {
pending.reapplyCellChange(
rowID: rowID,
columnIndex: columnIndex, columnName: columnName,
originalDBValue: newValue, newValue: previousValue, originalRow: originalRow,
originalDBValue: newValue, newValue: previousValue,
originalRow: originalRow.map { completedImage($0, of: rowID) },
absence: absence.reversed, expression: expression.reversed,
action: actionID
)
Expand Down Expand Up @@ -716,7 +761,8 @@ final class DataChangeManager: ObservableObject, ChangeManaging {
) {
// The pending deletion's image, which a value loaded since the delete has completed.
let image = completedImage(pending.change(forRow: rowID, type: .delete)?.originalRow ?? originalRow, of: rowID)
registerUndo(actionName: String(localized: "Delete Row")) { target in
let stepName = rowDeletionStepName(rowIsDeleted: pending.isRowDeleted(rowID), batch: false)
registerUndo(actionName: stepName) { target in
target.applyDataUndo(.rowDeletion(rowID: rowID, originalRow: image, absentColumns: absentColumns))
}

Expand Down Expand Up @@ -744,7 +790,8 @@ final class DataChangeManager: ObservableObject, ChangeManaging {
let image = pending.change(forRow: row.rowID, type: .delete)?.originalRow ?? row.originalRow
return (rowID: row.rowID, originalRow: completedImage(image, of: row.rowID))
}
registerUndo(actionName: String(localized: "Delete Rows")) { target in
let stepName = rowDeletionStepName(rowIsDeleted: rows.contains { pending.isRowDeleted($0.rowID) }, batch: true)
registerUndo(actionName: stepName) { target in
target.applyDataUndo(.batchRowDeletion(rows: images, absentColumns: absentColumns))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,9 @@ struct SQLStatementGenerator {

var maxBindParameters: Int {
switch databaseType {
case .sqlite: 32_766
case .sqlite, .libsql, .turso: 32_766
case .mssql: 2_100
case .cloudflareD1: 100
default: 65_535
}
}
Expand Down
20 changes: 20 additions & 0 deletions apple/TablePro/Core/ChangeTracking/SQLiteRowidAlias.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// SQLiteRowidAlias.swift
// TablePro
//

import Foundation

// SQLite's INTEGER PRIMARY KEY is the rowid: an INSERT that leaves it out takes the next one. A WITHOUT ROWID table has none.
enum SQLiteRowidAlias {
static func isAlias(
column: String,
declaredType: String?,
primaryKey: [String],
family: SQLTypeFamily,
hasRowID: Bool = true
) -> Bool {
guard family == .sqlite, hasRowID, primaryKey == [column], let declaredType else { return false }
return declaredType.trimmingCharacters(in: .whitespaces).uppercased() == "INTEGER"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ extension RowEditingCoordinator {
return
}

let savingTabId = parent.tabManager.selectedTabId
/// One change manager serves the whole window, so a second save cannot start while one is
/// in flight. Say so: over the slow link this guard exists for, a Save that silently does
/// nothing is indistinguishable from a Save that is broken.
guard beginSaveInFlight() else {
guard beginSaveInFlight(tabId: savingTabId) else {
saveChangesLogger.debug("Save already in flight, refusing the repeat")
if let index = parent.tabManager.selectedTabIndex {
parent.tabManager.mutate(at: index) {
Expand All @@ -66,7 +67,6 @@ extension RowEditingCoordinator {
return
}

let savingTabId = parent.tabManager.selectedTabId
guard parent.selectedTabScope != nil else {
failSave(message: String(localized: "Not connected to database"), tabId: savingTabId, waiter: waiter)
return
Expand Down
52 changes: 36 additions & 16 deletions apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@ final class RowEditingCoordinator: ObservableObject {

// Nothing clears the pending changes until the write returns, so a second Cmd+S in the round trip would commit twice.
private(set) var isSaveInFlight = false
// The landing save clears this tab's staged edits and reloads its rows, so nothing may stage there meanwhile.
private(set) var savingTabId: UUID?

init(parent: MainContentCoordinator) {
self.parent = parent
}

func beginSaveInFlight() -> Bool {
func beginSaveInFlight(tabId: UUID?) -> Bool {
guard !isSaveInFlight else { return false }
parent.objectWillChange.send()
isSaveInFlight = true
savingTabId = tabId
return true
}

func endSaveInFlight() {
parent.objectWillChange.send()
isSaveInFlight = false
savingTabId = nil
}

func isSaving(tabId: UUID?) -> Bool {
tabId != nil && savingTabId == tabId
}

// A row command selects the row it made. The JSON view reads the selection as "show only these rows".
Expand Down Expand Up @@ -104,43 +114,52 @@ final class RowEditingCoordinator: ObservableObject {
guard parent.canEditActiveResult(.delete), let tabId = parent.tabManager.selectedTabId else { return }
let deleted = rowIDs.filter { parent.changeManager.isRowDeleted($0) }
guard !deleted.isEmpty else { return }
for rowID in deleted {
parent.changeManager.undoRowDeletion(rowID: rowID)
}
parent.changeManager.restoreDeletedRows(deleted)
parent.existingGridModel(forTab: tabId)?.repaintRows(deleted)
}

func duplicateRow(_ rowID: RowID) {
duplicateRows([rowID])
}

func duplicateRows(_ rowIDs: [RowID]) {
guard parent.canEditActiveResult(.insert),
let (tab, tabIndex) = parent.tabManager.selectedTabAndIndex,
tab.tableContext.tableName != nil else { return }

let tabId = tab.id
let tableRows = parent.tabSessionRegistry.tableRows(for: tabId)
guard tableRows.hasAuthoritativeSchema,
let storageIndex = tableRows.index(of: rowID) else { return }
guard parent.unloadedValuesAreLoaded(rowIDs: [rowID], retry: { [weak self] in
self?.duplicateRow(rowID)
let storageIndices = rowIDs.compactMap { tableRows.index(of: $0) }
guard tableRows.hasAuthoritativeSchema, !storageIndices.isEmpty else { return }
guard parent.unloadedValuesAreLoaded(rowIDs: rowIDs, retry: { [weak self] in
self?.duplicateRows(rowIDs)
}) else { return }

parent.commitActiveGridEdit()

let uuidKeys = uuidKeyForms(in: tableRows, tab: tab)
var dupResult: RowOperationsManager.AddNewRowResult?
var duplicated: [RowOperationsManager.AddNewRowResult] = []
parent.mutateActiveTableRows(for: tabId) { rows in
let result = parent.rowOperationsManager.duplicateRow(
sourceRowIndex: storageIndex,
let result = parent.rowOperationsManager.duplicateRows(
sourceRowIndices: storageIndices,
tableRows: &rows,
uuidKeyForms: uuidKeys
)
dupResult = result
return result?.delta ?? .none
duplicated = result.rows
return result.delta
}

guard let result = dupResult else { return }
guard let first = duplicated.first else { return }

parent.tabManager.mutate(at: tabIndex) { $0.hasUserInteraction = true }
selectAndEditInsertedRow(result.rowID, tabId: tabId, skipping: result.prefilledColumns)
guard duplicated.count > 1 else {
selectAndEditInsertedRow(first.rowID, tabId: tabId, skipping: first.prefilledColumns)
return
}
if selectionPointsTheGrid, let model = parent.existingGridModel(forTab: tabId) {
model.selectRows(duplicated.map(\.rowID))
model.reveal(rowID: first.rowID)
}
}

// The caret skips a key the row was given a fresh UUID for, so the first keystroke does not replace it.
Expand Down Expand Up @@ -286,7 +305,8 @@ final class RowEditingCoordinator: ObservableObject {
if continuity == .discrete {
endInspectorEditRun()
}
guard let (tab, tabIndex) = parent.tabManager.selectedTabAndIndex, !valuesByRow.isEmpty else { return }
guard !parent.isSavingSelectedTab,
let (tab, tabIndex) = parent.tabManager.selectedTabAndIndex, !valuesByRow.isEmpty else { return }
let tabId = tab.id
let tableRows = parent.tabSessionRegistry.tableRows(for: tabId)
guard tableRows.columns.indices.contains(columnIndex) else { return }
Expand Down
12 changes: 12 additions & 0 deletions apple/TablePro/Core/CrossEngine/CrossEngineDefaultValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ internal enum CrossEngineDefaultValue {
return head.trimmingCharacters(in: .whitespaces)
}

// A default built on the current date or time, UTC forms and time zone conversions included.
internal static func readsTheClock(_ value: String) -> Bool {
let upper = value.uppercased()
return clockNames.contains { upper.contains($0) }
}

private static let clockNames = [
"NOW(", "CURRENT_TIMESTAMP", "LOCALTIMESTAMP", "CURRENT_DATE", "CURDATE(", "CURTIME(", "TODAY(",
"GETDATE(", "GETUTCDATE(", "SYSDATETIME(", "SYSUTCDATETIME(", "SYSDATETIMEOFFSET(", "UTC_TIMESTAMP",
"SYSDATE", "SYSTIMESTAMP", "STATEMENT_TIMESTAMP(", "TRANSACTION_TIMESTAMP(", "CLOCK_TIMESTAMP(",
]

private static func currentTimestamp(_ upper: String, target: SQLTypeFamily) -> String? {
let names = [
"CURRENT_TIMESTAMP", "CURRENT_TIMESTAMP()", "NOW()", "GETDATE()", "GETUTCDATE()",
Expand Down
44 changes: 37 additions & 7 deletions apple/TablePro/Core/DataWrite/DataWriteExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
// affected count against the count the plan expected is what catches it, and inside a transaction
// it is caught before it lands.
//
// For a keyed write the comparison is `actual > expected`, never `actual != expected`. MySQL reports
// zero affected rows for an UPDATE that writes a value a row already holds, which is a normal save,
// not a fault.
// For a keyed write the comparison is `actual > expected`. MySQL reports zero affected rows for an
// UPDATE that writes a value a row already holds, which is a normal save, not a fault. Where the
// count is the rows the UPDATE matched, a short one means the row was not written: deleted or
// rekeyed since it was read, kept out by row-level security, or skipped by a trigger.
//
// A keyless write is held to both ends. It finds its row by matching every column against the text
// the grid read, and that match can simply find nothing: measured on MySQL 8.4.11 and OceanBase
Expand Down Expand Up @@ -169,6 +170,10 @@ enum DataWriteExecutor {
commitMode: commitMode
)
var results: [DataWriteStepResult] = []
// Through a view the count is whatever its trigger or rule reports.
let countsKeyedUpdates = DataWriteRowCounts.countsMatchedRows(for: plan.databaseType) && plan.viewTarget == nil
// A delete earlier in the save can cascade to a row a later update edits, which then matches nothing.
var ranDelete = false

do {
if owner.sendsBegin {
Expand Down Expand Up @@ -211,8 +216,10 @@ enum DataWriteExecutor {
step,
rowsAffected: result.rowsAffected,
owner: owner,
countsAreMeaningful: DataWriteRowCounts.areMeaningful(for: plan.databaseType)
countsAreMeaningful: DataWriteRowCounts.areMeaningful(for: plan.databaseType),
holdsKeyedUpdates: countsKeyedUpdates && !ranDelete
)
ranDelete = ranDelete || step.rowKind == .delete || step.kind == .tableOperation
results.append(
DataWriteStepResult(
executionTime: Date().timeIntervalSince(start),
Expand Down Expand Up @@ -301,7 +308,13 @@ enum DataWriteExecutor {
)
)
}
guard rows.count != lookup.rowCount else { return rows }
guard rows.count != lookup.rowCount else {
guard let row = rows.first, lookup.rowCount == 1,
!lookup.heldValues.allSatisfy({ row.indices.contains($0.key) && row[$0.key] == $0.value })
else { return rows }
logger.error("Lookup on '\(table, privacy: .private(mask: .hash))' found the row changed since it was read")
throw DataWriteError.changeRefused(table: table, kind: step.rowKind, reason: rowNotFoundReason)
}
logger.error(
"Lookup on '\(table, privacy: .private(mask: .hash))' found \(rows.count, privacy: .public) rows, expected \(lookup.rowCount, privacy: .public)"
)
Expand All @@ -310,7 +323,7 @@ enum DataWriteExecutor {
// A lookup that expects no row is an insert checking its key is free.
reason = String(localized: "A row with this key already exists, so the insert would not add one.")
} else if rows.count < lookup.rowCount {
reason = String(localized: "No row holds the values it was loaded with. Another client may have changed or deleted it.")
reason = rowNotFoundReason
} else {
reason = String(localized: "Other rows hold exactly the same values, so the change cannot reach this row alone.")
}
Expand All @@ -334,6 +347,10 @@ enum DataWriteExecutor {
)
}

private static var rowNotFoundReason: String {
String(localized: "No row holds the values it was loaded with. Another client may have changed or deleted it.")
}

private static func fetch(_ statement: ParameterizedStatement, on driver: DatabaseDriver) async throws -> QueryResult {
statement.parameters.isEmpty
? try await driver.execute(query: statement.sql)
Expand All @@ -344,7 +361,8 @@ enum DataWriteExecutor {
_ step: DataWriteStep,
rowsAffected: Int,
owner: WriteTransactionOwner,
countsAreMeaningful: Bool
countsAreMeaningful: Bool,
holdsKeyedUpdates: Bool
) throws {
// A row inserted between a lookup without a pin and its write can still be reached, and a real count shows it.
guard let expected = step.expectedRowCount ?? step.lookup?.rowCount else { return }
Expand All @@ -364,6 +382,18 @@ enum DataWriteExecutor {
)
throw DataWriteError.keyNoLongerMatchesOneRow(table: table, expected: expected, actual: rowsAffected)
}
if holdsKeyedUpdates, step.rowKind == .update, !step.matchesRowsWithoutKey {
logger.error(
"Keyed update on '\(table, privacy: .private(mask: .hash))' matched \(rowsAffected, privacy: .public) rows, expected \(expected, privacy: .public)"
)
throw DataWriteError.changeRefused(
table: table,
kind: .update,
reason: String(
localized: "No row took the change. Another client may have changed or deleted it, or a security policy or trigger skipped it."
)
)
}
guard step.matchesRowsWithoutKey else { return }
logger.error(
"Keyless statement on '\(table, privacy: .private(mask: .hash))' affected \(rowsAffected, privacy: .public) rows, expected \(expected, privacy: .public)"
Expand Down
Loading
Loading