From 5f6332f97d482caf234e2dc57eb2fa47902030cc Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 14:39:10 +0700 Subject: [PATCH 1/3] fix(macos): keep T-SQL blocks and rewinds whole, stop CSV sanitizing by default, and fix Stop, defaults and gate cancels across drivers --- .../CassandraConditionalWrite.swift | 41 +++ .../RedisCommandSplitter.swift | 11 + .../SQLParenthesizedSemicolons.swift | 42 +++ .../SQLRoutineBodyTracker.swift | 124 +++++++-- .../SQLUnterminatedStatements.swift | 25 +- .../SqlBlockStructure.swift | 37 ++- .../Sources/TableProTeradataCore/Parcel.swift | 2 + .../TeradataConnection.swift | 204 +++++++++++++- .../TeradataMessages.swift | 8 + .../CassandraConditionalWriteTests.swift | 61 +++++ .../PluginParameterEscapingTests.swift | 11 + .../RedisCommandSplitterTests.swift | 12 + .../SQLParenthesizedSemicolonTests.swift | 77 ++++++ .../SQLStatementCursorIndexTests.swift | 13 +- .../TSQLControlFlowBlockTests.swift | 153 +++++++++++ .../TeradataStopTests.swift | 249 ++++++++++++++++++ .../TextViewController+EmphasizeBracket.swift | 17 +- .../Controller/TextViewControllerTests.swift | 18 ++ .../BeancountPluginDriver.swift | 6 +- .../BigtablePluginDriver+Execution.swift | 6 +- .../CSVExportPlugin/CSVExportModels.swift | 3 +- .../CSVExportOptionsView.swift | 6 +- .../CSVExportPlugin/CSVExportPlugin.swift | 95 +++++-- .../CSVImportPlugin/CSVImportPlugin.swift | 30 ++- .../CassandraPlugin.swift | 4 +- .../CassandraRowWriter.swift | 39 +-- .../DamengDriverPlugin/DamengPlugin.swift | 7 +- .../DynamoDBPluginDriver+Execution.swift | 4 +- .../EtcdDriverPlugin/EtcdPluginDriver.swift | 8 +- .../CSVExportFormulaAndDecimalTests.swift | 174 ++++++++++++ .../ExportImportRoundTripTests.swift | 17 +- .../FormatSettingsChangedMidRunTests.swift | 185 +++++++++++++ .../JSONExportPlugin/JSONExportPlugin.swift | 13 +- .../MarkdownExportPlugin.swift | 17 +- .../MongoDBConnection+SyncHelpers.swift | 5 +- .../MongoDBPluginDriver.swift | 7 +- .../MariaDBPluginConnection.swift | 36 ++- .../MariaDBStopThreadIdTests.swift | 38 +++ .../ParquetExportPlugin.swift | 9 +- .../RedisPluginDriver+Operations.swift | 5 +- .../RedisDriverPlugin/RedisPluginDriver.swift | 6 +- .../RedisConditionalWriteTests.swift | 19 ++ .../SQLImportPlugin/SQLImportPlugin.swift | 14 +- .../PluginDatabaseDriver.swift | 10 +- .../PluginExportUtilities.swift | 66 +++++ .../TableProPluginKit/PluginRowWriters.swift | 23 +- .../TeradataAsyncConnection.swift | 18 +- .../XLSXExportPlugin/XLSXExportPlugin.swift | 30 ++- .../Plugins/XLSXExportPlugin/XLSXWriter.swift | 63 +---- .../XLSXImportPlugin/XLSXImportPlugin.swift | 24 +- .../ChangeTracking/DataChangeModels.swift | 8 +- .../Core/ChangeTracking/DefaultedCells.swift | 38 +++ .../SQLStatementGenerator.swift | 7 +- .../Core/Compare/CompareSQLLiteral.swift | 22 +- .../Core/Compare/DataDiffEngine.swift | 26 +- .../Core/Compare/StreamingRowProvider.swift | 20 +- .../Coordinators/RowEditingCoordinator.swift | 6 +- .../DataWrite/Rewind/RewindExecutor.swift | 3 +- .../Core/DataWrite/Rewind/RewindPlanner.swift | 96 +++++-- .../DataWrite/RowChangeStatementFactory.swift | 111 +++++++- .../DataWrite/RowWriteOperationBuilder.swift | 9 +- .../DatabaseManager+ContainerDDL.swift | 36 +-- .../Database/DatabaseManager+Health.swift | 42 ++- .../Database/DatabaseManager+Principals.swift | 12 +- .../Database/DatabaseManager+Schema.swift | 14 +- .../Database/DatabaseManager+Sessions.swift | 5 +- .../Database/DatabaseManager+Startup.swift | 102 ++++++- .../Core/Database/DatabaseManager.swift | 4 + .../Core/Database/TriggerEditing.swift | 19 +- apple/TablePro/Core/LSP/LSPTransport.swift | 72 ++++- .../Core/ObjectCopy/ObjectCopyRowCopier.swift | 12 +- .../Core/ObjectCopy/ObjectCopyRunner.swift | 16 +- .../Plugins/ExportDataSourceAdapter.swift | 8 +- .../TablePro/Core/Plugins/ExportRowCut.swift | 11 + .../Execution/GatedOperationFailure.swift | 39 +++ .../Query/MetadataConnectionPool.swift | 18 +- .../Services/Query/RowOperationsManager.swift | 59 +++-- .../Core/Services/Query/RowParser.swift | 10 +- .../Query/SelectionSummaryCalculator.swift | 14 +- .../Shortcuts/Shared/PayloadRowPlanner.swift | 50 +++- .../Core/Shortcuts/Shared/RowPayload.swift | 20 +- .../Core/Shortcuts/ShortcutsRowWriter.swift | 5 +- .../UsersRoles/PrincipalChangeManager.swift | 17 +- .../JavaScript/QueryStatementModel.swift | 52 +++- .../Utilities/SQL/DisplayedResultReader.swift | 13 +- .../Core/Utilities/SQL/JsonRowConverter.swift | 2 +- .../Core/Utilities/SQL/QueryClassifier.swift | 13 +- .../Utilities/SQL/ResultJsonSerializer.swift | 2 + .../SQL/SQLRowToStatementConverter.swift | 14 +- .../Core/Vim/VimTextBufferAdapter.swift | 59 ----- .../PluginCellValue+DefaultMarker.swift | 19 +- .../ClickHouse/ClickHousePartInfo.swift | 4 +- .../Models/Query/SelectionSummary.swift | 10 +- .../Schema/ClickHousePartStatements.swift | 46 ++-- .../Models/UI/MultiRowEditState.swift | 8 +- .../TablePro/Resources/Localizable.xcstrings | 41 +++ .../ViewModels/SchemaEditorViewModel.swift | 2 +- .../ViewModels/UserTypeEditorModel.swift | 2 +- .../UsersRolesViewModel+Changes.swift | 2 +- .../Views/Compare/CompareRowDiffPane.swift | 15 ++ .../DataGrid/Cells/DataGridCellContent.swift | 3 +- .../Cells/DataGridCellPresenter.swift | 2 + .../Commands/DataGridCommands+Copy.swift | 1 + .../Commands/DataGridCommands+SetValue.swift | 4 +- .../DataGrid/DataGridViewController.swift | 3 +- .../CreateDatabaseSheet.swift | 2 +- .../Views/Editor/StatementRunController.swift | 50 +++- .../Views/Main/Child/DataTabSQLExporter.swift | 10 +- .../Main/Child/MainEditorContentView.swift | 1 + .../MainContentCoordinator+Navigation.swift | 11 +- ...ntentCoordinator+SafeModePromptRetry.swift | 28 ++ .../Main/MainContentCommandActions.swift | 67 +++-- .../Views/Main/MainContentCoordinator.swift | 9 +- .../ObjectCopy/CopyObjectsResultView.swift | 5 + .../UserType/CreateUserTypeSheet.swift | 9 +- .../Views/Results/ResultsJsonView.swift | 9 + .../Views/Sidebar/EditSequenceSheet.swift | 2 +- .../Views/Sidebar/ObjectCommentSheet.swift | 4 +- .../Views/Sidebar/SetSequenceValueSheet.swift | 2 +- .../Views/Structure/ClickHousePartsView.swift | 217 +++++++++------ .../Views/Structure/CreateTableDraft.swift | 15 +- .../StructureEditingSession+Apply.swift | 51 +--- ...tructureEditingSession+ColumnReorder.swift | 4 +- .../Views/Structure/TriggerDetailView.swift | 2 +- .../Views/Structure/TriggerEditorView.swift | 18 +- .../Completion/QueryCompletionCatalog.swift | 51 +++- .../QueryCompletionCatalogTests.swift | 29 ++ .../SQLStatementGeneratorBitColumnTests.swift | 4 +- .../SQLStatementGeneratorRowMatchTests.swift | 6 +- .../SQLStatementGeneratorTests.swift | 69 ++++- .../Compare/CompareDriverRowCutTests.swift | 90 +++++++ .../Core/Compare/CompareSQLLiteralTests.swift | 27 +- .../Compare/DataSyncScriptBuilderTests.swift | 2 +- .../RowEditingCoordinatorUUIDKeyTests.swift | 2 +- .../Core/DataWrite/RewindPlannerTests.swift | 81 ++++++ ...wChangeStatementFactoryCoverageTests.swift | 4 +- ...hangeStatementFactoryExpressionTests.swift | 84 +++++- .../RowWriteOperationBuilderTests.swift | 41 ++- .../Core/Database/DatabaseManagerTests.swift | 5 +- .../Database/SchemaWriteGateDenialTests.swift | 86 ++++++ .../Database/StartupCommandWarningTests.swift | 135 ++++++++++ .../Database/TriggerInfoMappingTests.swift | 86 ++++++ .../LSP/LSPTransportBrokenPipeTests.swift | 48 ++++ .../ObjectCopy/ObjectCopyRowCutTests.swift | 107 ++++++++ .../Query/MetadataConnectionPoolTests.swift | 21 +- .../SelectionSummaryCalculatorTests.swift | 17 +- .../RowOperationsFieldAbsenceTests.swift | 4 +- .../RowOperationsManagerPasteTests.swift | 18 +- .../Services/RowOperationsManagerTests.swift | 44 +++- .../RowOperationsManagerUUIDKeyTests.swift | 25 +- .../Services/RowPasteForeignTextTests.swift | 16 +- .../PayloadRowPlannerHeaderMatchTests.swift | 59 +++++ .../Shortcuts/RowPayloadFormatTests.swift | 60 +++++ .../PrincipalApplyOpenTransactionTests.swift | 44 ++++ .../PrincipalChangeManagerTests.swift | 40 +++ .../Utilities/JsonRowConverterTests.swift | 18 +- .../SQL/QueryClassifierTSQLBlockTests.swift | 39 +++ .../SQLRowToStatementConverterTests.swift | 31 ++- .../Vim/VimTextBufferAdapterPerfTests.swift | 103 ++------ .../Helpers/StubExecutionGates.swift | 9 +- .../Models/MultiRowEditStateTests.swift | 23 +- .../ClickHousePartStatementsTests.swift | 81 ++++-- .../Editor/StatementCaretTableTests.swift | 109 ++++++++ .../TableLoadAfterSafeModePromptTests.swift | 40 +++ .../WindowCloseSaveEveryConnectionTests.swift | 129 +++++++++ .../Views/Results/ResultsJsonViewTests.swift | 24 ++ .../SelectionSummaryTrackerTests.swift | 2 +- .../Structure/ClickHousePartsGridTests.swift | 101 ++++++- .../StructureListGridAccessibilityTests.swift | 3 +- .../StructureSaveConfirmationTests.swift | 10 +- .../TriggerEditorFontSizeTests.swift | 25 ++ core/crates/core/src/export/csv.rs | 55 ++-- core/crates/core/src/export/csv_options.rs | 4 +- core/crates/core/src/export/encoder/tests.rs | 6 +- core/crates/mcp/src/export_tools.rs | 10 +- .../storage/src/query_history/export.rs | 6 +- .../src/conformance/cases/large_table.rs | 9 +- .../src/conformance/cases/round_trip.rs | 8 +- .../crates/app/src/ui/export/encoding_sink.rs | 2 +- .../app/src/ui/export/export_ledger/tests.rs | 12 +- linux/crates/platform/tests/settings.rs | 2 +- .../data/app.tablepro.TablePro.gschema.xml.in | 2 +- linux/docs/storage.md | 2 +- 183 files changed, 4958 insertions(+), 1068 deletions(-) create mode 100644 apple/Packages/TableProCore/Sources/TableProCassandraCore/CassandraConditionalWrite.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLParenthesizedSemicolons.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProCassandraCoreTests/CassandraConditionalWriteTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLParenthesizedSemicolonTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/TSQLControlFlowBlockTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataStopTests.swift create mode 100644 apple/Plugins/FormatPluginTests/CSVExportFormulaAndDecimalTests.swift create mode 100644 apple/Plugins/FormatPluginTests/FormatSettingsChangedMidRunTests.swift create mode 100644 apple/Plugins/MySQLDriverTests/MariaDBStopThreadIdTests.swift create mode 100644 apple/TablePro/Core/ChangeTracking/DefaultedCells.swift create mode 100644 apple/TablePro/Core/Services/Execution/GatedOperationFailure.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SafeModePromptRetry.swift create mode 100644 apple/TableProTests/Core/Compare/CompareDriverRowCutTests.swift create mode 100644 apple/TableProTests/Core/Database/SchemaWriteGateDenialTests.swift create mode 100644 apple/TableProTests/Core/Database/StartupCommandWarningTests.swift create mode 100644 apple/TableProTests/Core/LSP/LSPTransportBrokenPipeTests.swift create mode 100644 apple/TableProTests/Core/ObjectCopy/ObjectCopyRowCutTests.swift create mode 100644 apple/TableProTests/Core/Shortcuts/PayloadRowPlannerHeaderMatchTests.swift create mode 100644 apple/TableProTests/Core/Shortcuts/RowPayloadFormatTests.swift create mode 100644 apple/TableProTests/Core/Utilities/SQL/QueryClassifierTSQLBlockTests.swift create mode 100644 apple/TableProTests/Views/Editor/StatementCaretTableTests.swift create mode 100644 apple/TableProTests/Views/Main/TableLoadAfterSafeModePromptTests.swift create mode 100644 apple/TableProTests/Views/Main/WindowCloseSaveEveryConnectionTests.swift create mode 100644 apple/TableProTests/Views/Structure/TriggerEditorFontSizeTests.swift diff --git a/apple/Packages/TableProCore/Sources/TableProCassandraCore/CassandraConditionalWrite.swift b/apple/Packages/TableProCore/Sources/TableProCassandraCore/CassandraConditionalWrite.swift new file mode 100644 index 0000000000..a4772dda72 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProCassandraCore/CassandraConditionalWrite.swift @@ -0,0 +1,41 @@ +// +// CassandraConditionalWrite.swift +// TableProCassandraCore +// + +import Foundation + +// A grid write names its row by key and is conditioned on it: an INSERT is an upsert, and an UPDATE writes its cells +// whether or not the row is there, so either would otherwise replace or recreate a row the grid never showed. +// A condition that fails answers one row whose `[applied]` is false, which the server reports as a success. +public enum CassandraConditionalWrite { + public static let appliedColumn = "[applied]" + public static let insertCondition = "IF NOT EXISTS" + public static let updateCondition = "IF EXISTS" + + public enum Refusal: Sendable, Equatable { + case keyTaken + case rowMissing + } + + public static func insertStatement(target: String, names: [String], values: [String]) -> String { + "INSERT INTO \(target) (\(names.joined(separator: ", "))) VALUES (\(values.joined(separator: ", "))) " + + insertCondition + } + + public static func updateStatement(target: String, assignments: [String], keyCondition: String) -> String { + "UPDATE \(target) SET \(assignments.joined(separator: ", ")) WHERE \(keyCondition) \(updateCondition)" + } + + public static func refusal(statement: String, firstColumn: String?, firstValue: String?) -> Refusal? { + guard firstColumn == appliedColumn, firstValue?.lowercased() == "false" else { return nil } + let normalized = statement.uppercased() + if normalized.hasPrefix("INSERT"), normalized.hasSuffix(" " + insertCondition) { + return .keyTaken + } + if normalized.hasPrefix("UPDATE"), normalized.hasSuffix(" " + updateCondition) { + return .rowMissing + } + return nil + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisCommandSplitter.swift b/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisCommandSplitter.swift index 8d42794c8d..d92e7bc4bc 100644 --- a/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisCommandSplitter.swift +++ b/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisCommandSplitter.swift @@ -32,6 +32,17 @@ public enum RedisCommandSplitter { return nil } + // Every command with the last caret offset `command(at:in:)` gives it, for a caller that keeps them between + // caret moves. + public static func commandsWithCaretReach(in text: String) -> [(command: Command, reach: Int)] { + var lexer = RedisCommandLexer(text, endsCommands: true, decodesArguments: false) + var commands: [(command: Command, reach: Int)] = [] + while let found = lexer.next() { + commands.append((command: command(found, in: text), reach: found.reach)) + } + return commands + } + private static func command(_ found: RedisCommandLexer.Command, in text: String) -> Command { let range = NSRange(location: found.start, length: found.end - found.start) return Command(text: (text as NSString).substring(with: range), range: range) diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLParenthesizedSemicolons.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLParenthesizedSemicolons.swift new file mode 100644 index 0000000000..d5d1607083 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLParenthesizedSemicolons.swift @@ -0,0 +1,42 @@ +import Foundation + +// The scanner keeps the `;`s of a PostgreSQL rule's `DO ALSO (...; ...)` in its statement. A client that cuts at every +// `;` runs each piece, so a gate tiers them too: `DO ALSO (SELECT 1; DELETE FROM t)` by its `DELETE`. +enum SQLParenthesizedSemicolons { + static func pieces(in statement: String, grammar: SQLLexicalGrammar) -> [String] { + guard statement.utf16.contains(SqlLexer.semicolon) else { return [] } + let text = statement as NSString + let length = text.length + var cuts: [Int] = [] + var depth = 0 + var index = 0 + while index < length { + if let span = SQLNonCodeSpan.span(at: index, in: text, grammar: grammar) { + index = max(span.end, index + 1) + continue + } + switch text.character(at: index) { + case SqlLexer.openParen: + depth += 1 + case SqlLexer.closeParen: + depth = max(0, depth - 1) + case SqlLexer.semicolon where depth > 0: + cuts.append(index) + default: + break + } + index += 1 + } + guard !cuts.isEmpty else { return [] } + var pieces: [String] = [] + var start = 0 + for cut in cuts + [length] { + let piece = StatementBlank.trimming(text.substring(with: NSRange(location: start, length: cut - start))) + if !piece.isEmpty { + pieces.append(piece) + } + start = cut + 1 + } + return pieces + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLRoutineBodyTracker.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLRoutineBodyTracker.swift index f1596e7ceb..d4abc7cc10 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLRoutineBodyTracker.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLRoutineBodyTracker.swift @@ -2,13 +2,26 @@ import Foundation /// Statement boundaries for the dialects whose only `;`-holding construct is a routine body written `BEGIN ... END`. /// -/// A `BEGIN` opens a body only inside a statement that defines a routine, for the safety reason recorded on +/// Outside T-SQL a `BEGIN` opens a body only inside a statement that defines a routine, for the safety reason recorded on /// ``SqlBlockStructure/opensRoutineDefinition(_:)``. The `;` belongs to the statement only after a T-SQL `MERGE`, which /// ``SQLLexicalGrammar/terminatedMergeStatements`` asks for: these engines accept every other statement without it. /// /// `BEGIN`, `CASE` and `END` count only where a keyword can stand: an engine that does not reserve them takes them as -/// names, and a block a name opened would never close. A text left inside one is refused by the external gate. +/// names, and a block a name opened would never close. T-SQL reserves all three, so there a block also opens outside +/// a routine, as in `IF @x = 1 BEGIN ... END` and `BEGIN TRY ... END TRY BEGIN CATCH ... END CATCH`. A text left +/// inside one is refused by the external gate. +/// +/// A `;` inside parentheses ends the statement, so a stray `(` cannot swallow what follows it, except in a PostgreSQL +/// rule's action list, `DO ALSO (INSERT ...; INSERT ...)`, which holds its commands together. public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { + // A PostgreSQL `CREATE RULE`: its header, the `DO [ALSO | INSTEAD]` before the action list, and the list itself. + private enum RuleAction: Equatable { + case none + case header + case afterDo + case list(depth: Int) + } + private enum Header { case opener /// After `CREATE`, `ALTER` or `REPLACE`. `accountWords` is what is left of an unquoted `DEFINER = user@host`. @@ -25,6 +38,11 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { private static let statementIntroducers: Set = ["THEN", "ELSE", "DO", "LOOP", "REPEAT"] private let grammar: SQLLexicalGrammar + private let readsBlocksEverywhere: Bool + // T-SQL outside a routine: where a block's `BEGIN` can stand, as the statement's first word, after `IF`, `WHILE` + // or `ELSE`, or after a block the statement already opened. Anywhere else `begin` is in a name's place. + private var followsControlFlow = false + private var pendingBeginMayOpen = false private var header = Header.opener private var definesRoutine = false private var openedBlockBody = false @@ -34,6 +52,8 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { /// whatever word follows it. private var openCaseExpressions = 0 private var parenDepth = 0 + private var ruleAction = RuleAction.none + private var ruleActionHeldSemicolon = false private var pendingBegin = false private var pendingEnd = false private var followsName = false @@ -50,12 +70,15 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { public init(grammar: SQLLexicalGrammar) { self.grammar = grammar + self.readsBlocksEverywhere = grammar.contains(.unterminatedStatements) self.merge = grammar.contains(.terminatedMergeStatements) ? SQLMergeStatementTracker() : nil } public var needsWords: Bool { + // Inside parentheses no word opens or closes a block, and a `MERGE` there needs no `;`. + if readsBlocksEverywhere { return parenDepth == 0 } if case .read = header { - return definesRoutine || merge?.needsWords == true + return definesRoutine || ruleAction == .header || ruleAction == .afterDo || merge?.needsWords == true } return true } @@ -70,7 +93,7 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { // An `END` still waiting for the word after it closes a block when the text ends. public var holdsOpenBlock: Bool { - depth > (pendingEnd ? 1 : 0) + depth > (pendingEnd ? 1 : 0) || ruleActionHeldSemicolon } public mutating func observeWord(_ word: String) { @@ -87,10 +110,27 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { startsBodyStatement = false let statementPosition = atStatementPosition observeStatementPosition(after: word, wasAtStatementPosition: statementPosition) - guard !isMember, readHeader(word), definesRoutine, parenDepth == 0 else { return } + guard !isMember else { return } + var isFirstWord = false + if case .opener = header { + isFirstWord = true + } + let pastHeader = readHeader(word) + if pastHeader, parenDepth == 0 { + observeRuleWord(word) + } + guard parenDepth == 0, readsBlocksEverywhere || (pastHeader && definesRoutine) else { return } switch word { - case "BEGIN" where opensNestedBlock(atStatementPosition: statementPosition): + case "BEGIN" where readsBlocksEverywhere: + pendingBegin = true + pendingBeginMayOpen = opensNestedBlock(atStatementPosition: statementPosition, isFirstWord: isFirstWord) + case "BEGIN" where opensNestedBlock(atStatementPosition: statementPosition, isFirstWord: isFirstWord): pendingBegin = true + pendingBeginMayOpen = true + case "IF" where readsBlocksEverywhere, "WHILE" where readsBlocksEverywhere: + followsControlFlow = true + case "ELSE" where readsBlocksEverywhere && openCaseExpressions == 0: + followsControlFlow = true case "CASE" where !grammar.hasSingleBodyForm: depth += 1 openCaseExpressions += 1 @@ -103,6 +143,9 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { public mutating func observeSymbol(_ symbol: UInt16) { merge?.observeSymbol(symbol) + if ruleAction == .afterDo, symbol != SqlLexer.openParen { + ruleAction = .none + } let opensBlock = symbol == SqlLexer.openParen && !grammar.contains(.atomicRoutineBodies) settlePendingBeforeNonWord(opensBlock: opensBlock) followsDot = followsName && symbol == Self.dot @@ -112,9 +155,16 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { switch symbol { case SqlLexer.openParen: parenDepth += 1 + if ruleAction == .afterDo { + ruleAction = .list(depth: parenDepth) + } endName() case SqlLexer.closeParen: parenDepth = max(0, parenDepth - 1) + if case .list(let listDepth) = ruleAction, parenDepth < listDepth { + ruleAction = .none + ruleActionHeldSemicolon = false + } default: break } @@ -122,6 +172,9 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { public mutating func observeOpaqueToken() { merge?.observeOpaqueToken() + if ruleAction == .afterDo { + ruleAction = .none + } settlePendingBeforeNonWord(opensBlock: !grammar.contains(.atomicRoutineBodies)) followsName = true followsDot = false @@ -140,13 +193,27 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { pendingEnd = false closeBlock() } + if readsBlocksEverywhere { + // A T-SQL `CASE` is only an expression, which no `;` can stand inside, so one still open was never closed. + depth = max(0, depth - openCaseExpressions) + } openCaseExpressions = 0 - parenDepth = 0 followsName = false followsDot = false - let endsStatement = depth == 0 - startsBodyStatement = !endsStatement - atStatementPosition = !endsStatement + let heldByRuleAction: Bool + if case .list(let listDepth) = ruleAction { + heldByRuleAction = depth == 0 && parenDepth >= listDepth + } else { + heldByRuleAction = false + } + if heldByRuleAction { + ruleActionHeldSemicolon = true + } else { + parenDepth = 0 + } + let endsStatement = depth == 0 && !heldByRuleAction + startsBodyStatement = depth > 0 + atStatementPosition = depth > 0 labelMayFollow = false inDeclaration = false inHandlerDeclaration = false @@ -175,6 +242,9 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { } return false case .kind(let accountWords): + if word == "RULE", grammar.contains(.atomicRoutineBodies) { + ruleAction = .header + } if SqlBlockStructure.isRoutineKind(word, grammar: grammar) { definesRoutine = true header = .name @@ -207,11 +277,33 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { } } - private func opensNestedBlock(atStatementPosition: Bool) -> Bool { - guard depth > 0, !grammar.hasSingleBodyForm, !grammar.contains(.unterminatedStatements) else { return true } + private func opensNestedBlock(atStatementPosition: Bool, isFirstWord: Bool) -> Bool { + if readsBlocksEverywhere { + return depth > 0 || definesRoutine || isFirstWord || followsControlFlow || openedBlockBody + } + guard depth > 0, !grammar.hasSingleBodyForm else { return true } return atStatementPosition || inHandlerDeclaration } + private mutating func observeRuleWord(_ word: String) { + switch ruleAction { + case .header where word == "DO": + ruleAction = .afterDo + case .afterDo where word == "ALSO" || word == "INSTEAD": + break + case .afterDo: + ruleAction = .none + default: + break + } + } + + // `BEGIN TRY` and `BEGIN CATCH` are blocks wherever they stand, and nothing else follows a name that way. + private func beginOpensBlock(followedBy word: String) -> Bool { + if readsBlocksEverywhere, word == "TRY" || word == "CATCH" { return true } + return pendingBeginMayOpen && !SqlBlockStructure.beginStartsStatement(followedBy: word, grammar: grammar) + } + private mutating func endName() { if case .name = header { header = .read @@ -227,7 +319,7 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { openBlockBody() return true } - if !SqlBlockStructure.beginStartsTransaction(followedBy: word) { + if beginOpensBlock(followedBy: word) { openBlockBody() } } @@ -237,7 +329,7 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { closeBlock() return false } - switch SqlBlockStructure.endingFollowedBy(word, closesCaseExpression: openCaseExpressions > 0) { + switch SqlBlockStructure.endingFollowedBy(word, closesCaseExpression: openCaseExpressions > 0, grammar: grammar) { case .closesControlFlow: return true case .closesCaseStatement: @@ -254,7 +346,9 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { private mutating func settlePendingBeforeNonWord(opensBlock: Bool) { if pendingBegin { pendingBegin = false - if opensBlock { + // A T-SQL `begin(` outside a routine is a name, as in `CREATE TABLE begin(a int)`. + let opensAfterName = !readsBlocksEverywhere || definesRoutine || depth > 0 + if opensBlock, pendingBeginMayOpen, opensAfterName { openBlockBody() } } diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift index 38f254c5bd..851741b05f 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift @@ -20,11 +20,13 @@ public enum SQLUnterminatedStatements { /// Every text a gate has to tier for `statement`: the statement itself, then, when `grammar` needs no terminator, /// the text of each statement that begins inside it, less the statements nested in that one, which come on their /// own. A grammar that takes a statement in place of a value or a table adds what ``SQLEmbeddedStatements`` finds, - /// and one with a pipe operator each statement ``SQLPipeOperatorChain`` finds. + /// and one with a pipe operator each statement ``SQLPipeOperatorChain`` finds. A grammar that needs a terminator + /// also adds the pieces a `;` inside the statement's parentheses separates, from ``SQLParenthesizedSemicolons``. public static func runnable(in statement: String, grammar: SQLLexicalGrammar) -> [String] { guard grammar.contains(.unterminatedStatements) else { return [statement] + SQLEmbeddedStatements.texts(in: statement, grammar: grammar) + SQLPipeOperatorChain.statements(in: statement, grammar: grammar) + + SQLParenthesizedSemicolons.pieces(in: statement, grammar: grammar) } let text = statement as NSString let tokens = TSQLTokens.read(text, grammar: grammar) @@ -32,6 +34,27 @@ public enum SQLUnterminatedStatements { return [statement] + ownTexts(of: statementSpans(in: tokens, length: text.length), in: text) } + // A scanner statement on T-SQL keeps a `;` outside parentheses only when a block holds it, so anything after one but + // the block's `END`, an `ELSE` or another `;` is a second statement the block runs. A stored routine runs nothing. + public static func blockHoldsSeveralStatements(_ statement: String, grammar: SQLLexicalGrammar) -> Bool { + guard grammar.contains(.unterminatedStatements) else { return false } + let tokens = TSQLTokens.read(statement as NSString, grammar: grammar) + guard !definesRoutine(tokens) else { return false } + return tokens.indices.dropLast().contains { index in + guard tokens[index].kind == .terminator, tokens[index].depth == 0 else { return false } + let next = tokens[index + 1] + guard next.kind != .terminator else { return false } + switch next.word { + case "ELSE": + return false + case "END": + return tokens.indices.contains(index + 2) && tokens[index + 2].word == "CONVERSATION" + default: + return true + } + } + } + /// The reserved keywords that begin a T-SQL statement. `ELSE` and `FETCH` are left out because they also continue /// a `CASE` and an `OFFSET` clause, and the statement after an `ELSE` begins with a keyword of its own. `WITH` is /// left out because a common table expression cannot follow a statement without a `;`, and the word also opens diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SqlBlockStructure.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SqlBlockStructure.swift index 0a11e84e6d..6968b11fef 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SqlBlockStructure.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SqlBlockStructure.swift @@ -23,7 +23,8 @@ public enum SqlBlockStructure { /// What an `END` means, given the word after it. public enum EndFollower: Equatable, Sendable { - /// `END IF`, `END LOOP` and their kin close a construct that never opened a block, and the word is theirs. + /// `END IF`, `END LOOP` and their kin close a construct that never opened a block, and T-SQL's + /// `END CONVERSATION` is a statement that closes nothing. Either way the word is theirs. case closesControlFlow /// `END CASE` closes the `CASE` that opened a block, and the word is theirs. case closesCaseStatement @@ -48,6 +49,10 @@ public enum SqlBlockStructure { /// second opener. private static let controlFlowFollowers: Set = ["IF", "LOOP", "WHILE", "FOR", "REPEAT"] + // T-SQL's `BEGIN DIALOG`, `BEGIN CONVERSATION TIMER` and `END CONVERSATION` are statements, not a block's ends. + private static let serviceBrokerFollowers: Set = ["DIALOG", conversationKeyword] + private static let conversationKeyword = "CONVERSATION" + /// The keywords a statement has to open with before a `BEGIN` inside it is read as a routine body. /// /// Only ``SQLRoutineBodyTracker`` consults this, and the reason is safety rather than grammar. An anonymous @@ -56,7 +61,9 @@ public enum SqlBlockStructure { /// single statement whose first word is `BEGIN`, and `QueryClassifier` tiers a statement by its leading keyword, /// so a swallowed `DROP` would be tiered a plain write and skip the destructive confirmation. Folding cannot /// execute anything, so it takes every block. Oracle, whose anonymous blocks are the everyday form, has its own - /// grammar in ``PLSQLUnitTracker`` and a classifier that reads the whole block. + /// grammar in ``PLSQLUnitTracker`` and a classifier that reads the whole block. T-SQL reserves `BEGIN`, so its + /// blocks open outside a routine too, and the gate tiers every statement inside one through + /// ``SQLUnterminatedStatements``. private static let routineDefinitionOpeners: Set = ["CREATE", "ALTER", "REPLACE"] /// What such a statement has to define, for the same reason. `BEGIN` is not reserved on PostgreSQL, MySQL or @@ -111,6 +118,28 @@ public enum SqlBlockStructure { return transactionFollowers.contains(keyword) } + public static func beginStartsStatement(followedBy keyword: String?, grammar: SQLLexicalGrammar) -> Bool { + if beginStartsTransaction(followedBy: keyword) { return true } + return readsTSQLBlocks(grammar) && keyword.map(serviceBrokerFollowers.contains) == true + } + + // T-SQL closes every block with a bare `END`: in `END IF @x = 1 ...` the `IF` begins the next statement. + public static func endingFollowedBy( + _ keyword: String, + closesCaseExpression: Bool, + grammar: SQLLexicalGrammar + ) -> EndFollower { + guard readsTSQLBlocks(grammar) else { + return endingFollowedBy(keyword, closesCaseExpression: closesCaseExpression) + } + return !closesCaseExpression && keyword == conversationKeyword ? .closesControlFlow : .closesBlock + } + + // The grammar whose statements need no `;` between them is T-SQL's, which reserves `BEGIN`, `CASE` and `END`. + private static func readsTSQLBlocks(_ grammar: SQLLexicalGrammar) -> Bool { + grammar.contains(.unterminatedStatements) + } + /// What an `END` followed by `keyword` closes. /// /// - Parameter closesCaseExpression: whether a `CASE` is open that has held no `;`. Only the statement form can @@ -260,7 +289,7 @@ public enum SqlBlockStructure { let cursor = skipTrivia(from: offset, in: text, length: length, grammar: grammar) guard cursor < length else { return true } guard text.character(at: cursor) != SqlLexer.semicolon else { return true } - return beginStartsTransaction(followedBy: readKeyword(text, at: cursor, length: length).text) + return beginStartsStatement(followedBy: readKeyword(text, at: cursor, length: length).text, grammar: grammar) } /// Goes by the word after the `END` alone: only folding asks, and it keeps no record of which `CASE` is open. @@ -273,7 +302,7 @@ public enum SqlBlockStructure { let cursor = skipTrivia(from: offset, in: text, length: length, grammar: grammar) guard cursor < length else { return .closesBlock(resumeAt: offset) } let follower = readKeyword(text, at: cursor, length: length) - switch endingFollowedBy(follower.text, closesCaseExpression: false) { + switch endingFollowedBy(follower.text, closesCaseExpression: false, grammar: grammar) { case .closesControlFlow: return .none case .closesCaseStatement: diff --git a/apple/Packages/TableProCore/Sources/TableProTeradataCore/Parcel.swift b/apple/Packages/TableProCore/Sources/TableProTeradataCore/Parcel.swift index e00a7551a5..cb810a30f5 100644 --- a/apple/Packages/TableProCore/Sources/TableProTeradataCore/Parcel.swift +++ b/apple/Packages/TableProCore/Sources/TableProTeradataCore/Parcel.swift @@ -4,6 +4,8 @@ import TableProWireIO enum ParcelFlavor: UInt16 { case request = 1 case response = 4 + case abort = 6 + case cancel = 7 case success = 8 case failure = 9 case record = 10 diff --git a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift index ed5608fe9f..440de15bef 100644 --- a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift +++ b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift @@ -1,7 +1,8 @@ import Darwin import Foundation -public final class TeradataConnection { +// One queue drives it. Only `cancel()` comes from elsewhere, and what it reads and sends is under socketLock or wireLock. +public final class TeradataConnection: @unchecked Sendable { private let config: TeradataConnectionConfig private let socketLock = NSLock() private var _socket: (any TeradataTransport)? @@ -20,13 +21,56 @@ public final class TeradataConnection { private var gssSequence: UInt64 = 0 private var confidentialityBypassed = false + // Orders every post-logon send with the counters it consumes, since Stop sends its abort from another thread. + private let wireLock = NSLock() + // Under socketLock, which is never held across I/O. + private var _phase = RequestPhase.idle + private var _stopRequested = false + private var _operationActive = false + // The request a stop is waiting on the server for, so the stop's deadline closes nothing that came after it. + private var _stoppingRequest: UInt32? + + // 3110: "The transaction was aborted by the user." + static let userAbortCode = 3_110 + private static let abortLockWait: TimeInterval = 0.25 + // A server or network that stops answering would otherwise hold a stop for the whole 20-second receive timeout. + static let defaultStopGrace: TimeInterval = 5 + private let stopGrace: TimeInterval + private let stopDeadlineQueue = DispatchQueue(label: "com.TablePro.teradata.stop-deadline") + + private enum RequestPhase: Equatable { + case idle + case connecting + case awaitingResponse(UInt32) + case fetching(UInt32) + } + + private enum StopAction { + case nothing + case abort(UInt32) + case endOutput(UInt32) + case close + } + + private enum Exchange { + case answered([Parcel]) + case notSent + case outputEnded + } + public init(config: TeradataConnectionConfig) { self.config = config + stopGrace = Self.defaultStopGrace } - init(config: TeradataConnectionConfig, transport: any TeradataTransport) { + init( + config: TeradataConnectionConfig, + transport: any TeradataTransport, + stopGrace: TimeInterval = TeradataConnection.defaultStopGrace + ) { self.config = config self._socket = transport + self.stopGrace = stopGrace confidentialityBypassed = true } @@ -49,6 +93,7 @@ public final class TeradataConnection { socketLock.withLock { _socket = socket _hasLostConnection = false + _phase = .connecting } var established = false defer { @@ -57,6 +102,7 @@ public final class TeradataConnection { self.socket = nil connectDeadline = nil } + socketLock.withLock { _phase = .idle } } serverIP = Self.resolveAddress(config.host) ?? config.host _ = try deadline.remainingMilliseconds() @@ -91,28 +137,91 @@ public final class TeradataConnection { let body = TeradataMessages.optionsParcel(returnStatementInfo: false).encoded() + TeradataMessages.requestParcel(sql: sql).encoded() + TeradataMessages.responseParcel().encoded() - let parcels: [Parcel] + let outcome: Exchange do { - parcels = try exchange(body, requestNumber: requestNumber) + outcome = try exchange(body, requestNumber: requestNumber) } catch { + endRequest() // The reply still on its way would be read as the next request's. dropTransport() throw error } - return try TeradataResultParser.parse(parcels) + let stopped = endRequest() + guard case .answered(let parcels) = outcome else { throw TeradataWireError.cancelled } + do { + return try TeradataResultParser.parse(parcels) + } catch TeradataWireError.server(let code, _) where stopped && code == Self.userAbortCode { + // The server rolled back the transaction the request ran in; the host has no way yet to say so. + throw TeradataWireError.cancelled + } } - private func exchange(_ body: [UInt8], requestNumber: UInt32) throws -> [Parcel] { - try sendEncrypted(kind: .start, body: body, requestNumber: requestNumber) + private func exchange(_ body: [UInt8], requestNumber: UInt32) throws -> Exchange { + let sent = try wireLock.withLock { () -> Bool in + // A stop that landed before the request went out refuses it, and the session stays as it was. + let refused = socketLock.withLock { () -> Bool in + guard !_stopRequested else { return true } + _phase = .awaitingResponse(requestNumber) + return false + } + guard !refused else { return false } + try sendEncryptedLocked(kind: .start, body: body, requestNumber: requestNumber) + return true + } + guard sent else { return .notSent } var parcels: [Parcel] = [] while true { let batch = try readReply() parcels.append(contentsOf: batch) - if Self.responseIsComplete(batch) { return parcels } + if Self.responseIsComplete(batch) { return .answered(parcels) } + // Once the first reply is out an abort no longer takes effect, so a stop ends the output instead. + let stopRequested = socketLock.withLock { () -> Bool in + _phase = .fetching(requestNumber) + return _stopRequested + } + if stopRequested { + try endOutput(requestNumber: requestNumber) + return .outputEnded + } try sendContinue(requestNumber: requestNumber) } } + // The Cancel parcel in place of the Response parcel deletes the request's spool, and the server answers with + // End Request. One that does not throws, and the caller gives the transport up. + private func endOutput(requestNumber: UInt32) throws { + try sendEncrypted(kind: .cont, body: TeradataMessages.cancelParcel().encoded(), requestNumber: requestNumber) + guard Self.responseIsComplete(try readReply()) else { + throw TeradataWireError.malformed("the server kept answering a cancelled request") + } + } + + // A caller's operation, so a stop that lands before its request goes out still reaches it. + public func beginOperation() { + socketLock.withLock { + _operationActive = true + _stopRequested = false + } + } + + public func endOperation() { + socketLock.withLock { + _operationActive = false + _stopRequested = false + } + } + + @discardableResult + private func endRequest() -> Bool { + socketLock.withLock { + let stopped = _stopRequested + _phase = .idle + _stopRequested = false + _stoppingRequest = nil + return stopped + } + } + private func dropTransport() { let dropped = socketLock.withLock { let transport = _socket @@ -129,19 +238,80 @@ public final class TeradataConnection { return } if sessionNumber != 0 { - let message = LanMessage( - kind: .logoff, body: TeradataMessages.logoffParcel().encoded(), - sessionNumber: sessionNumber, byteVar: 0, - authentication: TeradataMessages.authBytes(nextMessage()), hostCharSet: charsetCode) - try? socket.send(message.encoded()) + wireLock.withLock { + let message = LanMessage( + kind: .logoff, body: TeradataMessages.logoffParcel().encoded(), + sessionNumber: sessionNumber, byteVar: 0, + authentication: TeradataMessages.authBytes(nextMessage()), hostCharSet: charsetCode) + try? socket.send(message.encoded()) + } } socket.close() self.socket = nil connectDeadline = nil } + // A request the server is still working on is aborted on the session, which rolls back its transaction, answers it + // with failure 3110 and stays logged on. Past the first reply the request's own loop ends the output instead, and a + // request not yet sent is refused. A connect has no request to stop, so it gives the transport up, as does an + // abort that cannot be sent or a server that has not ended the request within the grace. public func cancel() { - socket?.cancel() + let action: StopAction = socketLock.withLock { + switch _phase { + case .idle: + if _operationActive { _stopRequested = true } + return .nothing + case .connecting: + return .close + case .awaitingResponse(let request): + _stopRequested = true + _stoppingRequest = request + return .abort(request) + case .fetching(let request): + _stopRequested = true + _stoppingRequest = request + return .endOutput(request) + } + } + switch action { + case .nothing: + return + case .close: + socket?.cancel() + case .abort(let request): + guard sendAbort(for: request) else { + socket?.cancel() + return + } + closeUnlessEnded(request) + case .endOutput(let request): + closeUnlessEnded(request) + } + } + + private func closeUnlessEnded(_ request: UInt32) { + stopDeadlineQueue.asyncAfter(deadline: .now() + stopGrace) { [weak self] in + guard let self else { return } + // Shut down under the lock, so a request that ended meanwhile cannot hand the transport to a new one. + socketLock.withLock { + guard _stoppingRequest == request else { return } + _socket?.cancel() + } + } + } + + // A long start still going out holds the lock, and the caller may be the main thread. + private func sendAbort(for request: UInt32) -> Bool { + guard wireLock.lock(before: Date(timeIntervalSinceNow: Self.abortLockWait)) else { return false } + defer { wireLock.unlock() } + // Past the first reply the request's own loop ends the output; a request already answered needs nothing. + guard socketLock.withLock({ _phase == .awaitingResponse(request) }) else { return true } + do { + try sendEncryptedLocked(kind: .abort, body: TeradataMessages.abortParcel().encoded(), requestNumber: request) + return true + } catch { + return false + } } static func responseIsComplete(_ parcels: [Parcel]) -> Bool { @@ -252,6 +422,12 @@ public final class TeradataConnection { } private func sendEncrypted(kind: MessageKind, body: [UInt8], requestNumber: UInt32) throws { + try wireLock.withLock { + try sendEncryptedLocked(kind: kind, body: body, requestNumber: requestNumber) + } + } + + private func sendEncryptedLocked(kind: MessageKind, body: [UInt8], requestNumber: UInt32) throws { let message = LanMessage( kind: kind, body: body, sessionNumber: sessionNumber, requestNumber: requestNumber, byteVar: 0, authentication: TeradataMessages.authBytes(nextMessage()), diff --git a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift index 042633063c..d0ec9d359e 100644 --- a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift +++ b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift @@ -119,6 +119,14 @@ enum TeradataMessages { Parcel(.logoff) } + static func abortParcel() -> Parcel { + Parcel(.abort) + } + + static func cancelParcel() -> Parcel { + Parcel(.cancel) + } + static func charsetCode(fromConfigResponse body: [UInt8], name: String) -> UInt8? { let needle = Array(name.utf8) guard !needle.isEmpty else { return nil } diff --git a/apple/Packages/TableProCore/Tests/TableProCassandraCoreTests/CassandraConditionalWriteTests.swift b/apple/Packages/TableProCore/Tests/TableProCassandraCoreTests/CassandraConditionalWriteTests.swift new file mode 100644 index 0000000000..b02a53181e --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProCassandraCoreTests/CassandraConditionalWriteTests.swift @@ -0,0 +1,61 @@ +// +// CassandraConditionalWriteTests.swift +// TableProCassandraCoreTests +// + +import TableProCassandraCore +import Testing + +struct CassandraConditionalWriteTests { + private static let update = CassandraConditionalWrite.updateStatement( + target: "\"shop\".\"orders\"", assignments: ["\"note\" = ?"], keyCondition: "\"id\" = ?" + ) + private static let insert = CassandraConditionalWrite.insertStatement( + target: "\"shop\".\"orders\"", names: ["\"id\"", "\"note\""], values: ["?", "?"] + ) + + // Without the condition CQL writes the cell anyway and recreates the row with every other column null. + @Test("A grid edit only writes a row that still exists") + func updateIsConditionedOnTheRow() { + #expect(Self.update == "UPDATE \"shop\".\"orders\" SET \"note\" = ? WHERE \"id\" = ? IF EXISTS") + #expect(Self.insert == "INSERT INTO \"shop\".\"orders\" (\"id\", \"note\") VALUES (?, ?) IF NOT EXISTS") + } + + @Test("An edit of a row deleted since it was read is refused, not reported as saved") + func updateOfAMissingRowIsRefused() { + #expect(CassandraConditionalWrite.refusal( + statement: Self.update, firstColumn: "[applied]", firstValue: "false" + ) == .rowMissing) + } + + @Test("A new row whose key is taken is refused") + func insertOfATakenKeyIsRefused() { + #expect(CassandraConditionalWrite.refusal( + statement: Self.insert, firstColumn: "[applied]", firstValue: "false" + ) == .keyTaken) + } + + @Test("A write the condition let through is no refusal") + func appliedWriteIsNotRefused() { + #expect(CassandraConditionalWrite.refusal( + statement: Self.update, firstColumn: "[applied]", firstValue: "true" + ) == nil) + #expect(CassandraConditionalWrite.refusal( + statement: Self.insert, firstColumn: "[applied]", firstValue: "true" + ) == nil) + } + + // A condition on a column answers with the row's current values, which the console shows as the server sent them. + @Test("An unapplied write with a condition of its own is left to the result") + func otherConditionsAreLeftToTheResult() { + #expect(CassandraConditionalWrite.refusal( + statement: "UPDATE t SET a = 1 WHERE k = 1 IF a = 0", firstColumn: "[applied]", firstValue: "false" + ) == nil) + #expect(CassandraConditionalWrite.refusal( + statement: "DELETE FROM t WHERE k = 1 IF EXISTS", firstColumn: "[applied]", firstValue: "false" + ) == nil) + #expect(CassandraConditionalWrite.refusal( + statement: "UPDATE t SET a = 1 WHERE k = 1", firstColumn: "a", firstValue: "false" + ) == nil) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift index 4f2d558573..1f17a3ac4b 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift @@ -107,6 +107,17 @@ struct IsNumericLiteralTests { #expect(!StubDriver.isNumericLiteral("12abc")) #expect(!StubDriver.isNumericLiteral("1.2.3")) } + + // Written bare, a zip or a product code is compared as a number and matches `123` and `0123` too. + @Test("Numeric-shaped text with padding zeros is bound as a string", arguments: ["00123", "-007", "0042", "00.5"]) + func paddedNumbersAreQuoted(value: String) { + #expect(StubDriver().escapedParameterValue(value).unicodeScalars.first == "'") + } + + @Test("A number that opens with a single zero is bound bare", arguments: ["0", "0.5", "-0.5", "0e3", "+0", "100"]) + func singleLeadingZeroStaysBare(value: String) { + #expect(StubDriver().escapedParameterValue(value) == value) + } } // MARK: - escapedParameterValue diff --git a/apple/Packages/TableProCore/Tests/TableProRedisCoreTests/RedisCommandSplitterTests.swift b/apple/Packages/TableProCore/Tests/TableProRedisCoreTests/RedisCommandSplitterTests.swift index 515d4828cd..5dee906b01 100644 --- a/apple/Packages/TableProCore/Tests/TableProRedisCoreTests/RedisCommandSplitterTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProRedisCoreTests/RedisCommandSplitterTests.swift @@ -103,6 +103,18 @@ struct RedisCommandSplitterTests { } } + @Test("Each command's caret reach gives every caret the command command(at:) gives it", arguments: [ + "GET a; GET b\n\nGET c \n", "GET a\n", "GET a; ", "SET k \"a\nb\"\nGET k", " GET a ;", "SET 😀 1\r\nGET 😀", "", + ]) + func caretReachMatchesCommandAt(script: String) { + let reached = RedisCommandSplitter.commandsWithCaretReach(in: script) + #expect(reached.map { $0.command } == RedisCommandSplitter.commands(in: script)) + for caret in 0...((script as NSString).length + 1) { + let viaReach = reached.first { caret <= $0.reach }?.command + #expect(viaReach == RedisCommandSplitter.command(at: caret, in: script), "caret \(caret)") + } + } + @Test("A caret after the last command, or in text with none, has no command") func caretPastEveryCommand() { #expect(command(at: 6, in: "GET a\n") == nil) diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLParenthesizedSemicolonTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLParenthesizedSemicolonTests.swift new file mode 100644 index 0000000000..2f9cf1257c --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLParenthesizedSemicolonTests.swift @@ -0,0 +1,77 @@ +import Foundation +import TableProSQLGrammar +import Testing + +struct SQLParenthesizedSemicolonTests { + private static func grammar(_ typeId: String) -> SQLLexicalGrammar { + SQLLexicalReadings.resolve(databaseTypeId: typeId, declared: nil, session: nil).execution + } + + private func sent(_ sql: String, _ grammar: SQLLexicalGrammar) -> [String] { + SQLStatementScanner.executableStatements(in: sql, grammar: grammar).map(\.sql) + } + + @Test("A PostgreSQL rule's multi-command action list stays one statement", arguments: [ + "CREATE RULE r AS ON INSERT TO t DO ALSO (INSERT INTO a VALUES (1); INSERT INTO b VALUES (2))", + "CREATE OR REPLACE RULE r AS ON UPDATE TO t WHERE (NEW.a > 0) DO INSTEAD (UPDATE a SET n = 1; NOTIFY t)", + "CREATE RULE r AS ON DELETE TO t DO (DELETE FROM a; DELETE FROM b)", + ]) + func ruleActionStaysWhole(rule: String) { + let grammar = Self.grammar("PostgreSQL") + let sql = rule + ";\nSELECT 9;" + + #expect(sent(sql, grammar) == [rule, "SELECT 9"]) + #expect(SQLStatementScanner.navigableStatements(in: sql, grammar: grammar).count == 2) + #expect(!SQLStatementScanner.endsInsideOpenBlock(sql, grammar: grammar)) + } + + @Test("A rule whose action is one command ends at its ;") + func ruleWithoutAnActionList() { + let grammar = Self.grammar("PostgreSQL") + + #expect(sent("CREATE RULE r AS ON INSERT TO t DO ALSO INSERT INTO log VALUES (NEW.id);\nSELECT 2;", grammar).count == 2) + #expect(sent("CREATE RULE r AS ON INSERT TO t DO INSTEAD NOTHING;\nSELECT 2;", grammar).count == 2) + } + + @Test("A stray ( ends at its ; and leaves every statement after it on its own", arguments: [ + "PostgreSQL", "MySQL", "SQLite", "SQL Server", "DuckDB", "ClickHouse", + ]) + func strayParenthesisEndsAtItsSemicolon(typeId: String) { + let grammar = Self.grammar(typeId) + let sql = "SELECT 1;\nSELECT count(id FROM a;\nSELECT 2;\nSELECT 3;" + let caret = (sql as NSString).range(of: "SELECT 3").location + + #expect(sent(sql, grammar) == ["SELECT 1", "SELECT count(id FROM a", "SELECT 2", "SELECT 3"]) + #expect(SQLStatementScanner.statementAtCursor(in: sql, cursorPosition: caret, grammar: grammar) == "SELECT 3") + #expect(!SQLStatementScanner.endsInsideOpenBlock(sql, grammar: grammar)) + } + + @Test("A rule's action list left open holds the rest of the text, which the gate reads as more than one statement") + func openActionListLeavesTheTextInsideABlock() { + let grammar = Self.grammar("PostgreSQL") + let sql = "CREATE RULE r AS ON INSERT TO t DO ALSO (INSERT INTO a VALUES (1);\nDROP TABLE users;" + + #expect(sent(sql, grammar).count == 1) + #expect(SQLStatementScanner.endsInsideOpenBlock(sql, grammar: grammar)) + } + + @Test("The gate tiers each command a rule's action list holds") + func gateTiersThePieces() { + let readings = SQLLexicalReadings.resolve(databaseTypeId: "PostgreSQL", declared: nil, session: nil) + + #expect(SQLStatementClassifier.deletesEveryRow( + "CREATE RULE r AS ON INSERT TO t DO ALSO (SELECT 1; DELETE FROM audit)", + databaseTypeId: "PostgreSQL", + readings: readings + )) + #expect(SQLStatementClassifier.classify( + "SELECT (1; DROP TABLE users; SELECT 1)", databaseTypeId: "PostgreSQL", readings: readings + ).tier == .destructive) + } + + @Test("Inside a routine body a parenthesis one statement leaves open does not hide the body's END") + func bodyStatementParenthesisDoesNotHideTheEnd() { + let sql = "CREATE PROCEDURE p()\nBEGIN\n SELECT (1;\n SELECT 2;\nEND;\nSELECT 3;" + #expect(sent(sql, Self.grammar("MySQL")).last == "SELECT 3") + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLStatementCursorIndexTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLStatementCursorIndexTests.swift index c63a3d7bbe..79bcaf44ed 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLStatementCursorIndexTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLStatementCursorIndexTests.swift @@ -20,6 +20,7 @@ private let cursorIndexFixtures = [ CREATE FUNCTION f() RETURNS int AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql; /* c; */ SELECT E'x' 'y';; + CREATE RULE r AS ON INSERT TO t DO ALSO (INSERT INTO a VALUES (1); INSERT INTO b VALUES (2)); SELECT 2 """), CursorIndexFixture(typeId: "MySQL", text: """ @@ -44,6 +45,15 @@ private let cursorIndexFixtures = [ GO SELECT [a;b] GO + IF @x = 1 + BEGIN + SELECT 1; + SELECT CASE WHEN @x = 1 THEN 2 END; + END + ELSE BEGIN TRY SELECT 3; END TRY BEGIN CATCH SELECT 4; END CATCH; + SELECT (5; 6); + SET XACT_ABORT ON + BEGIN TRY SELECT 7; END TRY BEGIN CATCH THROW; END CATCH """), CursorIndexFixture(typeId: "Oracle", text: """ BEGIN @@ -70,7 +80,8 @@ private let cursorIndexFixtures = [ ] private let cursorIndexInsertions = [ - ";", "'", "\nGO\n", "\n/\n", " ", "x", "$$", "BEGIN ", "END;", "--", "/*", "*/", "\n", "GO", "/", + ";", "'", "\nGO\n", "\n/\n", " ", "x", "$$", "BEGIN ", "END;", "--", "/*", "*/", "\n", "GO", "/", "(", ")", + "CASE ", "END ", ] private let longScriptRow = "SELECT id, name FROM users WHERE active = true;\n" diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/TSQLControlFlowBlockTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/TSQLControlFlowBlockTests.swift new file mode 100644 index 0000000000..271bc67937 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/TSQLControlFlowBlockTests.swift @@ -0,0 +1,153 @@ +import Foundation +import TableProSQLGrammar +import Testing + +// T-SQL reserves BEGIN, so a BEGIN ... END block holds its `;`s wherever it stands, not only in a routine's body. +struct TSQLControlFlowBlockTests { + private static let sqlServer = SQLLexicalReadings.resolve(databaseTypeId: "SQL Server", declared: nil, session: nil) + .execution + + private static let merge = "MERGE dbo.t AS t USING dbo.s AS s ON t.id = s.id WHEN MATCHED THEN DELETE" + + private func sent(_ sql: String) -> [String] { + SQLStatementScanner.executableStatements(in: sql, grammar: Self.sqlServer).map(\.sql) + } + + private func atCursor(_ sql: String, on marker: String) -> String { + let offset = (sql as NSString).range(of: marker).location + return SQLStatementScanner.statementAtCursor(in: sql, cursorPosition: offset, grammar: Self.sqlServer) + } + + @Test("A block an IF, a WHILE or a statement of its own opens stays one statement", arguments: [ + ("IF 1=1 BEGIN SELECT 1; SELECT 2; END; SELECT 3", ["IF 1=1 BEGIN SELECT 1; SELECT 2; END", "SELECT 3"]), + ( + "WHILE @i < 3\nBEGIN\n SET @i += 1;\n PRINT @i;\nEND;\nSELECT @i;", + ["WHILE @i < 3\nBEGIN\n SET @i += 1;\n PRINT @i;\nEND", "SELECT @i"] + ), + ("BEGIN\n SELECT 1;\n SELECT 2;\nEND;\nSELECT 3;", ["BEGIN\n SELECT 1;\n SELECT 2;\nEND", "SELECT 3"]), + ( + "IF @a = 1 BEGIN IF @b = 1 BEGIN SELECT 1; END ELSE BEGIN SELECT 2; END; SELECT 3; END; SELECT 4", + ["IF @a = 1 BEGIN IF @b = 1 BEGIN SELECT 1; END ELSE BEGIN SELECT 2; END; SELECT 3; END", "SELECT 4"] + ), + ( + "BEGIN TRY\n DELETE FROM a;\nEND TRY\nBEGIN CATCH\n SELECT ERROR_MESSAGE();\nEND CATCH;\nSELECT 9;", + ["BEGIN TRY\n DELETE FROM a;\nEND TRY\nBEGIN CATCH\n SELECT ERROR_MESSAGE();\nEND CATCH", "SELECT 9"] + ), + ]) + func blockHoldsItsSemicolons(sql: String, expected: [String]) { + #expect(sent(sql) == expected) + } + + @Test("Run Current anywhere in a conditional block runs the whole block") + func caretInsideTheBlockRunsTheBlock() { + let block = "IF @env = 'dev'\nBEGIN\n DELETE FROM a;\n DELETE FROM b;\nEND" + let sql = "SELECT 0;\n\(block);\nSELECT 9;" + + #expect(atCursor(sql, on: "IF @env") == block) + #expect(atCursor(sql, on: "DELETE FROM b") == block) + #expect(SQLStatementScanner.navigableStatements(in: sql, grammar: Self.sqlServer).count == 3) + } + + @Test("An END is never followed by more of its block, so the IF after it is the next statement") + func endBeforeTheNextStatementClosesTheBlock() { + let sql = "IF @a = 1 BEGIN SELECT 1; END\nIF @b = 1 BEGIN SELECT 2; END;\nSELECT 3;" + + #expect(sent(sql) == ["IF @a = 1 BEGIN SELECT 1; END\nIF @b = 1 BEGIN SELECT 2; END", "SELECT 3"]) + #expect(!SQLStatementScanner.endsInsideOpenBlock(sql, grammar: Self.sqlServer)) + } + + @Test("A transaction's BEGIN is a statement, not a block", arguments: ["TRAN", "TRANSACTION", "DISTRIBUTED TRANSACTION"]) + func transactionBeginIsAStatement(opener: String) { + #expect(sent("BEGIN \(opener);\nUPDATE t SET a = 1;\nCOMMIT;") == ["BEGIN \(opener)", "UPDATE t SET a = 1", "COMMIT"]) + } + + @Test("Service Broker's BEGIN DIALOG, BEGIN CONVERSATION TIMER and END CONVERSATION are statements") + func serviceBrokerStatementsAreNotBlocks() { + let script = """ + BEGIN DIALOG CONVERSATION @h FROM SERVICE [s] TO SERVICE 't' ON CONTRACT [c]; + BEGIN CONVERSATION TIMER (@h) TIMEOUT = 60; + END CONVERSATION @h; + SELECT 1; + """ + #expect(sent(script).count == 4) + + let inBlock = "IF @x = 1 BEGIN END CONVERSATION @h; SELECT 1; END; SELECT 2" + #expect(sent(inBlock) == ["IF @x = 1 BEGIN END CONVERSATION @h; SELECT 1; END", "SELECT 2"]) + } + + @Test("A CASE expression closes at its own END, and one left open at a ; does not hold the next statement") + func caseExpressions() { + #expect(sent("SELECT CASE WHEN a = 1 THEN 'x' END AS c FROM t;\nSELECT 2;").count == 2) + #expect(sent("IF @x = 1 BEGIN SELECT CASE WHEN @x = 1 THEN 1 END; SELECT 2; END; SELECT 3").count == 2) + #expect(sent("SELECT CASE WHEN a = 1 THEN 1;\nSELECT 2;") == ["SELECT CASE WHEN a = 1 THEN 1", "SELECT 2"]) + } + + @Test("A MERGE inside a block keeps its ; and the block's own ; is a separator") + func mergeInsideABlock() { + let block = "IF 1 = 1 BEGIN \(Self.merge); END" + #expect(sent(block + ";\nSELECT 1;") == [block, "SELECT 1"]) + } + + @Test("A block left open runs to its GO line and no further") + func goLineEndsAnOpenBlock() { + let sql = "IF 1 = 1 BEGIN SELECT 1;\nGO\nSELECT 2" + #expect(sent(sql) == ["IF 1 = 1 BEGIN SELECT 1", "SELECT 2"]) + #expect(SQLStatementScanner.endsInsideOpenBlock("IF 1 = 1 BEGIN SELECT 1;", grammar: Self.sqlServer)) + } + + @Test("The gate still tiers every statement a block holds") + func gateTiersEachStatementInsideTheBlock() { + let readings = SQLLexicalReadings.resolve(databaseTypeId: "SQL Server", declared: nil, session: nil) + let sql = "IF @env = 'dev' BEGIN SELECT 1; DROP TABLE dbo.t; END" + + #expect(SQLStatementClassifier.classify(sql, databaseTypeId: "SQL Server", readings: readings).tier == .destructive) + #expect(SQLStatementClassifier.deletesEveryRow( + "IF 1 = 1 BEGIN DELETE FROM dbo.t; END", databaseTypeId: "SQL Server", readings: readings + )) + } + + @Test("A TRY block after a statement written without a ; is still one block") + func tryBlockAfterAnUnterminatedStatement() { + let script = "SET XACT_ABORT ON\nBEGIN TRY\n DELETE FROM a;\nEND TRY\nBEGIN CATCH\n THROW;\nEND CATCH;\nSELECT 1;" + #expect(sent(script).count == 2) + #expect(sent(script).last == "SELECT 1") + } + + @Test("A begin where a name stands opens no block", arguments: [ + ("CREATE TABLE begin(a int); DROP TABLE users", 2), + ("INSERT INTO begin VALUES (1); DROP TABLE users", 2), + ("DO BEGIN SELECT 1 FROM DUMMY; SELECT 2 FROM DUMMY; END;", 3), + ]) + func nameInBeginsPlaceOpensNothing(sql: String, count: Int) { + #expect(sent(sql).count == count) + } + + @Test("A block holding more than one statement is told apart from one statement", arguments: [ + ("IF x BEGIN UPDATE a SET c = 1; UPDATE b SET c = 1; END", true), + ("BEGIN MERGE dbo.t AS t USING dbo.s AS s ON t.id = s.id WHEN MATCHED THEN DELETE; SELECT 1; END", true), + ("IF x BEGIN SELECT 1; WITH c AS (SELECT id FROM t) DELETE FROM c; END", true), + ("IF x BEGIN SELECT 1; DISABLE TRIGGER trg ON t; END", true), + ("BEGIN TRY SELECT 1; THROW 50000, 'x', 1; END TRY BEGIN CATCH SELECT 2; END CATCH", true), + ("IF x BEGIN SELECT 1; done: SELECT 2; END", true), + ("IF x BEGIN SELECT 1; (SELECT 2); END", true), + ("IF x BEGIN SELECT 1; END CONVERSATION @h; END", true), + ("IF x SELECT 1", false), + ("IF x BEGIN SELECT 1; END", false), + ("IF x BEGIN SELECT 1;; END", false), + ("BEGIN IF x SELECT 1; ELSE SELECT 2; END", false), + ("BEGIN TRY SELECT 1; END TRY BEGIN CATCH THROW; END CATCH", false), + ("CREATE PROCEDURE p AS BEGIN SELECT 1; SELECT 2; END", false), + ]) + func blockHoldingSeveralStatements(sql: String, holdsSeveral: Bool) { + #expect(SQLUnterminatedStatements.blockHoldsSeveralStatements(sql, grammar: Self.sqlServer) == holdsSeveral) + let postgreSQL = SQLLexicalReadings.resolve(databaseTypeId: "PostgreSQL", declared: nil, session: nil).execution + #expect(!SQLUnterminatedStatements.blockHoldsSeveralStatements(sql, grammar: postgreSQL)) + } + + @Test("Other engines still read BEGIN outside a routine as a word", arguments: ["PostgreSQL", "MySQL", "SQLite"]) + func otherEnginesAreUnchanged(typeId: String) { + let grammar = SQLLexicalReadings.resolve(databaseTypeId: typeId, declared: nil, session: nil).execution + let statements = SQLStatementScanner.executableStatements(in: "BEGIN\nDELETE FROM t;\nSELECT 1;", grammar: grammar) + #expect(statements.map { $0.sql } == ["BEGIN\nDELETE FROM t", "SELECT 1"]) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataStopTests.swift b/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataStopTests.swift new file mode 100644 index 0000000000..4f19e1b292 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataStopTests.swift @@ -0,0 +1,249 @@ +import Foundation +@testable import TableProTeradataCore +import Testing + +// Replies are read in order; a hook keyed by a reply's index runs just before that reply is read, which is where a +// request is waiting on the server when Stop is pressed. +private final class StoppableTransport: TeradataTransport { + private var replies: [[UInt8]] + private var stream: [UInt8] = [] + private var repliesRead = 0 + var beforeReply: [Int: () -> Void] = [:] + private(set) var sent: [LanMessage] = [] + private(set) var shutDown = false + private(set) var closed = false + + var sentKinds: [UInt8] { + sent.map { $0.kind } + } + + init(replies: [[UInt8]]) { + self.replies = replies + } + + func send(_ bytes: [UInt8]) throws { + if shutDown || closed { throw TeradataWireError.cancelled } + sent.append(LanMessage.decode( + header: Array(bytes.prefix(LanMessage.headerLength)), + body: Array(bytes.dropFirst(LanMessage.headerLength)) + )) + } + + func receive(_ count: Int) throws -> [UInt8] { + if stream.isEmpty { + beforeReply.removeValue(forKey: repliesRead)?() + if shutDown || closed { throw TeradataWireError.cancelled } + guard !replies.isEmpty else { throw TeradataWireError.truncated("no reply") } + stream = replies.removeFirst() + repliesRead += 1 + } + guard stream.count >= count else { throw TeradataWireError.truncated("closed") } + defer { stream.removeFirst(count) } + return Array(stream.prefix(count)) + } + + func finishConnecting() {} + + func cancel() { + shutDown = true + } + + func close() { + closed = true + } +} + +// A server that never answers: a receive waits until the transport is shut down, which the stop's deadline does from +// its own queue. +private final class SilentTransport: TeradataTransport, @unchecked Sendable { + private let lock = NSLock() + private let shutDown = DispatchSemaphore(value: 0) + private var sent: [UInt8] = [] + private var shut = false + private var received = false + var onFirstReceive: (() -> Void)? + + var sentKinds: [UInt8] { lock.withLock { sent } } + var wasShutDown: Bool { lock.withLock { shut } } + + func send(_ bytes: [UInt8]) throws { + try lock.withLock { + if shut { throw TeradataWireError.cancelled } + sent.append(bytes[2]) + } + } + + func receive(_ count: Int) throws -> [UInt8] { + let first = lock.withLock { () -> Bool in + defer { received = true } + return !received + } + if first { onFirstReceive?() } + _ = shutDown.wait(timeout: .now() + 10) + throw TeradataWireError.cancelled + } + + func finishConnecting() {} + + func cancel() { + lock.withLock { shut = true } + shutDown.signal() + } + + func close() { + cancel() + } +} + +struct TeradataStopTests { + private static let config = TeradataConnectionConfig(host: "td.example", username: "dbc", password: "dbc") + private static let start = MessageKind.start.rawValue + private static let cont = MessageKind.cont.rawValue + private static let abort = MessageKind.abort.rawValue + + private static func reply(_ parcels: [Parcel]) -> [UInt8] { + LanMessage(kind: .start, body: parcels.flatMap { $0.encoded() }).encoded() + } + + private static func failure(code: Int, _ message: String) -> Parcel { + let text = Array(message.utf8) + let body = [UInt8](repeating: 0, count: 8) + + [UInt8(code >> 8), UInt8(code & 0xFF), UInt8(text.count >> 8), UInt8(text.count & 0xFF)] + text + return Parcel(.failure, body: body) + } + + private static let finished = reply([Parcel(.success), Parcel(.endRequest)]) + + private static func thrown(_ body: () throws -> Void) -> Error? { + do { + try body() + return nil + } catch { + return error + } + } + + private static func isCancelled(_ error: Error?) -> Bool { + guard let wire = error as? TeradataWireError, case .cancelled = wire else { return false } + return true + } + + @Test("Stop while the server works on a request aborts it on the session, which stays logged on") + func stopAbortsTheRequestAndKeepsTheSession() throws { + let aborted = Self.reply([Self.failure(code: 3_110, "The transaction was aborted by the user.")]) + let transport = StoppableTransport(replies: [aborted, Self.finished]) + let connection = TeradataConnection(config: Self.config, transport: transport) + transport.beforeReply[0] = { connection.cancel() } + + let error = Self.thrown { _ = try connection.execute("SELECT * FROM sales.orders CROSS JOIN sales.items") } + + #expect(Self.isCancelled(error)) + #expect(transport.sentKinds == [Self.start, Self.abort]) + #expect(transport.sent.last?.requestNumber == transport.sent.first?.requestNumber) + #expect(transport.sent.last?.body == TeradataMessages.abortParcel().encoded()) + #expect(!transport.shutDown) + #expect(!transport.closed) + #expect(connection.isConnected) + #expect(!connection.hasLostConnection) + + _ = try connection.execute("SELECT 1") + #expect(transport.sentKinds == [Self.start, Self.abort, Self.start]) + } + + @Test("A request that failed on its own before the abort landed keeps its own error") + func failureOtherThanTheAbortIsReported() throws { + let missing = Self.reply([Self.failure(code: 3_807, "Object 'sales.orderz' does not exist.")]) + let transport = StoppableTransport(replies: [missing]) + let connection = TeradataConnection(config: Self.config, transport: transport) + transport.beforeReply[0] = { connection.cancel() } + + let error = Self.thrown { _ = try connection.execute("SELECT * FROM sales.orderz") } + + guard let wire = error as? TeradataWireError, case .server(let code, _) = wire else { + Issue.record("expected the server's error, got \(String(describing: error))") + return + } + #expect(code == 3_807) + #expect(connection.isConnected) + } + + // An export Stop lands here: the abort does not take effect once the server has started answering. + @Test("Stop while a long answer is being fetched ends the output and keeps the session") + func stopDuringFetchEndsTheOutput() throws { + let batch = Self.reply([Parcel(.success)]) + let ended = Self.reply([Parcel(.endRequest)]) + let transport = StoppableTransport(replies: [batch, batch, ended, Self.finished]) + let connection = TeradataConnection(config: Self.config, transport: transport) + transport.beforeReply[1] = { connection.cancel() } + + let error = Self.thrown { _ = try connection.execute("SELECT * FROM sales.orders") } + + #expect(Self.isCancelled(error)) + #expect(transport.sentKinds == [Self.start, Self.cont, Self.cont]) + #expect(transport.sent.last?.body == TeradataMessages.cancelParcel().encoded()) + #expect(!transport.shutDown) + #expect(!transport.closed) + #expect(!connection.hasLostConnection) + + _ = try connection.execute("SELECT 1") + #expect(transport.sentKinds == [Self.start, Self.cont, Self.cont, Self.start]) + } + + @Test("An output the server keeps sending after the Cancel parcel gives the session up") + func unendedOutputGivesTheSessionUp() throws { + let batch = Self.reply([Parcel(.success)]) + let transport = StoppableTransport(replies: [batch, batch, batch]) + let connection = TeradataConnection(config: Self.config, transport: transport) + transport.beforeReply[1] = { connection.cancel() } + + #expect(Self.thrown { _ = try connection.execute("SELECT * FROM sales.orders") } != nil) + #expect(transport.closed) + #expect(connection.hasLostConnection) + } + + @Test("Stop in a running call before its request goes out refuses the request and keeps the session") + func stopBeforeTheRequestIsSentRefusesIt() throws { + let transport = StoppableTransport(replies: [Self.finished]) + let connection = TeradataConnection(config: Self.config, transport: transport) + + connection.beginOperation() + connection.cancel() + let error = Self.thrown { _ = try connection.execute("DELETE FROM sales.orders") } + connection.endOperation() + + #expect(Self.isCancelled(error)) + #expect(transport.sent.isEmpty) + #expect(connection.isConnected) + + connection.beginOperation() + _ = try connection.execute("SELECT 1") + connection.endOperation() + #expect(transport.sentKinds == [Self.start]) + } + + @Test("A stopped request the server never answers gives the session up after the grace", .timeLimit(.minutes(1))) + func unansweredStopGivesTheSessionUp() throws { + let transport = SilentTransport() + let connection = TeradataConnection(config: Self.config, transport: transport, stopGrace: 0.05) + transport.onFirstReceive = { connection.cancel() } + + let error = Self.thrown { _ = try connection.execute("SELECT * FROM sales.orders CROSS JOIN sales.items") } + + #expect(Self.isCancelled(error)) + #expect(transport.sentKinds == [Self.start, Self.abort]) + #expect(transport.wasShutDown) + #expect(connection.hasLostConnection) + } + + @Test("Stop with no request running sends nothing and keeps the session") + func stopWhileIdleDoesNothing() throws { + let transport = StoppableTransport(replies: [Self.finished]) + let connection = TeradataConnection(config: Self.config, transport: transport) + + connection.cancel() + _ = try connection.execute("SELECT 1") + + #expect(transport.sentKinds == [Self.start]) + #expect(!transport.shutDown) + } +} diff --git a/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+EmphasizeBracket.swift b/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+EmphasizeBracket.swift index 0d390a3be1..04429cdc55 100644 --- a/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+EmphasizeBracket.swift +++ b/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+EmphasizeBracket.swift @@ -37,7 +37,7 @@ extension TextViewController { pair.0, pair.1, from: range.location, - limit: min((textView.visibleTextRange ?? .zero).max + 4_096, textView.documentRange.max), + limit: min(max(range.location, (textView.visibleTextRange ?? .zero).max) + 4_096, textView.documentRange.max), reverse: false ) { emphasizeCharacter(characterIndex) @@ -52,7 +52,10 @@ extension TextViewController { pair.1, pair.0, from: range.location - 1, - limit: max((textView.visibleTextRange?.location ?? 0) - 4_096, textView.documentRange.location), + limit: max( + min(range.location - 1, textView.visibleTextRange?.location ?? 0) - 4_096, + textView.documentRange.location + ), reverse: true ) { emphasizeCharacter(characterIndex) @@ -91,12 +94,16 @@ extension TextViewController { if reverse { options = options.union(.reverse) } + // A caret outside the visible range can put the limit on the wrong side of `from`, and NSString raises + // an exception nothing can catch for a range past the text. + let length = textView.textStorage.length + let lower = max(0, min(reverse ? limit : from, length)) + let upper = max(0, min(reverse ? from : limit, length)) + guard upper > lower else { return nil } var closeCount = 0 var index: Int? textView.textStorage.mutableString.enumerateSubstrings( - in: reverse ? - NSRange(location: limit, length: from - limit) : - NSRange(location: from, length: limit - from), + in: NSRange(location: lower, length: upper - lower), options: options, using: { substring, range, _, stop in if substring == close { diff --git a/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/TextViewControllerTests.swift b/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/TextViewControllerTests.swift index ff714958bf..8542ff6869 100644 --- a/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/TextViewControllerTests.swift +++ b/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/TextViewControllerTests.swift @@ -330,6 +330,24 @@ final class TextViewControllerTests: XCTestCase { ) } + func test_findClosingPairWithALimitOnTheWrongSideOrPastTheText() { + controller.textView.string = "{ Lorem }" + + XCTAssertNil(controller.findClosingPair("{", "}", from: 5, limit: 2, reverse: false)) + XCTAssertNil(controller.findClosingPair("}", "{", from: 2, limit: 5, reverse: true)) + XCTAssertEqual(controller.findClosingPair("{", "}", from: 1, limit: 10_000, reverse: false), 8) + } + + func test_bracketEmphasisFarPastTheVisibleTextFindsThePair() { + controller.configuration.appearance.bracketPairEmphasis = .flash + controller.setText(String(repeating: "x", count: 60_000) + "{ a }") + + controller.setCursorPositions([CursorPosition(range: NSRange(location: 60_001, length: 0))]) + + XCTAssertEqual(controller.cursorPositions.first?.range.location, 60_001) + XCTAssertEqual(controller.findClosingPair("{", "}", from: 60_001, limit: 60_005, reverse: false), 60_004) + } + // MARK: Set Text func test_setText() { diff --git a/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift b/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift index 4d776c917f..661f20f618 100644 --- a/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift +++ b/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift @@ -460,11 +460,13 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } return try executeSQLite(query: query, parameters: []) } - continuation.yield(.header(PluginStreamHeader( + var header = PluginStreamHeader( columns: result.columns, columnTypeNames: result.columnTypeNames, estimatedRowCount: result.rows.count - ))) + ) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) continuation.yield(.rows(result.rows)) continuation.finish() } catch { diff --git a/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift b/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift index 1b3c890c81..e362dfd30d 100644 --- a/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift +++ b/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift @@ -156,9 +156,11 @@ extension BigtablePluginDriver { try await self.export(spec, continuation: continuation) } else { let result = try await self.execute(query: query) - continuation.yield(.header(PluginStreamHeader( + var header = PluginStreamHeader( columns: result.columns, columnTypeNames: result.columnTypeNames, estimatedRowCount: nil - ))) + ) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) if !result.rows.isEmpty { continuation.yield(.rows(result.rows)) } } continuation.finish() diff --git a/apple/Plugins/CSVExportPlugin/CSVExportModels.swift b/apple/Plugins/CSVExportPlugin/CSVExportModels.swift index 2fdbd570a2..cd7942fb4b 100644 --- a/apple/Plugins/CSVExportPlugin/CSVExportModels.swift +++ b/apple/Plugins/CSVExportPlugin/CSVExportModels.swift @@ -79,7 +79,8 @@ public struct CSVExportOptions: Equatable, Codable, Sendable { public var quoteHandling: CSVQuoteHandling = .asNeeded public var lineBreak: CSVLineBreak = .lf public var decimalFormat: CSVDecimalFormat = .period - public var sanitizeFormulas: Bool = true + // Off: RFC 4180 carries a field as written, and OWASP scopes the quote prefix to files meant for a spreadsheet. + public var sanitizeFormulas: Bool = false /// What the file's bytes are written in. UTF-8 keeps every existing export byte for byte. public var encoding: PluginTextEncoding = .utf8 diff --git a/apple/Plugins/CSVExportPlugin/CSVExportOptionsView.swift b/apple/Plugins/CSVExportPlugin/CSVExportOptionsView.swift index a35bd63f80..f38791eced 100644 --- a/apple/Plugins/CSVExportPlugin/CSVExportOptionsView.swift +++ b/apple/Plugins/CSVExportPlugin/CSVExportOptionsView.swift @@ -23,7 +23,11 @@ struct CSVExportOptionsView: View { Toggle("Sanitize formula-like values", isOn: $plugin.settings.sanitizeFormulas) .toggleStyle(.checkbox) - .help("Prevent CSV formula injection by prefixing values starting with =, +, -, @ with a single quote") + .help(""" + For a file opened in a spreadsheet. Values that would run as formulas, such as those starting \ + with =, +, - or @, get a leading single quote, which stays in the value for anything that \ + imports the file. + """) } Divider() diff --git a/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift b/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift index 6f11d384b2..f4f2bf17b3 100644 --- a/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift +++ b/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift @@ -59,6 +59,7 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi let lineBreak = options.lineBreak.value let encoding = options.encoding var report = CSVEncodingReport() + var neutralisedCount = 0 if options.writesByteOrderMark, !encoding.byteOrderMark.isEmpty { try fileHandle.write(contentsOf: Data(encoding.byteOrderMark)) @@ -80,7 +81,7 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi } var isFirstBatch = true - var columns: [String] = [] + var columnNumbers: [PluginColumnNumbers] = [] let stream = dataSource.streamRows(for: table) for try await element in stream { @@ -88,17 +89,27 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi switch element { case .header(let header): - columns = header.columns + columnNumbers = header.columnTypeNames.map { PluginColumnNumbers(typeName: $0) } if isFirstBatch && options.includeFieldNames { - let headerLine = columns - .map { escapeCSVField($0, options: options) } + if options.sanitizeFormulas { + neutralisedCount += header.columns.filter(PluginRowWriters.isFormulaLike).count + } + let headerLine = header.columns + .map { escapeCSVField($0, options: options, sanitizes: options.sanitizeFormulas) } .joined(separator: options.delimiter.actualValue) try write(headerLine + lineBreak, to: fileHandle, encoding: encoding, report: &report) } isFirstBatch = false case .rows(let rows): for row in rows { - try writeCSVRow(row, options: options, to: fileHandle, report: &report) + try writeCSVRow( + row, + columnNumbers: columnNumbers, + options: options, + to: fileHandle, + report: &report, + neutralisedCount: &neutralisedCount + ) progress.incrementRow() } } @@ -114,7 +125,22 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi try PluginExportUtilities.commitAtomicWrite(from: tempURL, to: destination) committed = true progress.finalizeTable() - return ExportFormatResult(warnings: report.warnings(for: encoding)) + return ExportFormatResult(warnings: report.warnings(for: encoding) + Self.neutralisedWarnings(neutralisedCount)) + } + + // The quote is part of the value for anything that imports the file, so the export says how many it changed. + static func neutralisedWarnings(_ count: Int) -> [String] { + guard count > 0 else { return [] } + return [String( + format: String( + localized: """ + Values a spreadsheet would read as formulas, written with a leading single quote: %lld. \ + Turn off Sanitize formula-like values to export them unchanged. + """, + bundle: .main + ), + Int64(count) + )] } // MARK: - Private @@ -143,26 +169,31 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi try fileHandle.write(contentsOf: encoded.data) } + // A text column holding "1.10" is a version or a code; a column with no usable type keeps the decimal comma. private func writeCSVRow( _ row: [PluginCellValue], + columnNumbers: [PluginColumnNumbers], options: CSVExportOptions, to fileHandle: FileHandle, - report: inout CSVEncodingReport + report: inout CSVEncodingReport, + neutralisedCount: inout Int ) throws { - let delimiter = options.delimiter.actualValue - let lineBreak = options.lineBreak.value + var fields: [String] = [] + fields.reserveCapacity(row.count) - let rowLine = row.map { cell -> String in + for (index, cell) in row.enumerated() { let val: String switch cell { case .null: - return options.convertNullToEmpty ? "" : "NULL" + fields.append(options.convertNullToEmpty ? "" : "NULL") + continue case .text(let s): val = s case .bytes(let d): val = "0x" + d.map { String(format: "%02X", $0) }.joined() } + let takesDecimalComma = index >= columnNumbers.count || columnNumbers[index] != .text var processed = val // `\r\n` is one Character, so only a scalar test sees a Windows line break. let hadLineBreaks = val.unicodeScalars.contains { $0 == "\n" || $0 == "\r" } @@ -174,40 +205,54 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi .replacingOccurrences(of: "\n", with: " ", options: .literal) } - if options.decimalFormat == .comma { + var isCommaDecimal = false + if options.decimalFormat == .comma, takesDecimalComma { let range = NSRange(processed.startIndex..., in: processed) if Self.decimalFormatRegex.firstMatch(in: processed, range: range) != nil { processed = processed.replacingOccurrences(of: ".", with: ",") + isCommaDecimal = true } } - return escapeCSVField(processed, options: options, originalHadLineBreaks: hadLineBreaks) - }.joined(separator: delimiter) + // `-1,5` is still a number, and the quote would turn it into text. + let sanitizes = options.sanitizeFormulas && !isCommaDecimal + if sanitizes, PluginRowWriters.isFormulaLike(processed) { + neutralisedCount += 1 + } + fields.append(escapeCSVField( + processed, options: options, sanitizes: sanitizes, originalHadLineBreaks: hadLineBreaks + )) + } - try write(rowLine + lineBreak, to: fileHandle, encoding: options.encoding, report: &report) + try write( + fields.joined(separator: options.delimiter.actualValue) + options.lineBreak.value, + to: fileHandle, + encoding: options.encoding, + report: &report + ) } - /// Escaping and quoting live in `PluginRowWriters`, so this format, the other export formats - /// and the MCP tool spell a value the same way. Only the option mapping is this plugin's own. - /// - /// `originalHadLineBreaks` says the value's breaks were already replaced with spaces upstream, - /// and it still forces quoting: the source text spanned lines, and a reader that splits on the - /// delimiter has no way to know the space it now sees was one. - private func escapeCSVField(_ field: String, options: CSVExportOptions, originalHadLineBreaks: Bool = false) -> String { - let escaped = PluginRowWriters.csvField(field, options: writeOptions(options)) + // `originalHadLineBreaks` still forces quoting after the breaks became spaces: the source text spanned lines. + private func escapeCSVField( + _ field: String, + options: CSVExportOptions, + sanitizes: Bool, + originalHadLineBreaks: Bool = false + ) -> String { + let escaped = PluginRowWriters.csvField(field, options: writeOptions(options, sanitizes: sanitizes)) guard originalHadLineBreaks, options.quoteHandling == .asNeeded, escaped.unicodeScalars.first != "\"" else { return escaped } return "\"\(escaped.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } - private func writeOptions(_ options: CSVExportOptions) -> PluginCsvWriteOptions { + private func writeOptions(_ options: CSVExportOptions, sanitizes: Bool) -> PluginCsvWriteOptions { PluginCsvWriteOptions( delimiter: options.delimiter.actualValue, quoteHandling: quoteHandling(options.quoteHandling), lineEnding: options.lineBreak.value, nullAsEmpty: true, - sanitizesFormulas: options.sanitizeFormulas, + sanitizesFormulas: sanitizes, flattensLineBreaks: false ) } diff --git a/apple/Plugins/CSVImportPlugin/CSVImportPlugin.swift b/apple/Plugins/CSVImportPlugin/CSVImportPlugin.swift index 0a3b2d9ae3..c9716c5229 100644 --- a/apple/Plugins/CSVImportPlugin/CSVImportPlugin.swift +++ b/apple/Plugins/CSVImportPlugin/CSVImportPlugin.swift @@ -29,13 +29,13 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi var fieldDetectionSignature: String { settings.detectionSignature } - var rowImportConfiguration: RowImportRunner.Configuration? { runConfiguration } + var rowImportConfiguration: RowImportRunner.Configuration? { Self.runConfiguration(settings) } - private var runConfiguration: RowImportRunner.Configuration { + private static func runConfiguration(_ options: CSVImportOptions) -> RowImportRunner.Configuration { RowImportRunner.Configuration( - errorHandling: settings.errorHandling, - wrapInTransaction: settings.wrapInTransaction, - deleteExistingRows: settings.deleteExistingRows + errorHandling: options.errorHandling, + wrapInTransaction: options.wrapInTransaction, + deleteExistingRows: options.deleteExistingRows ) } @@ -58,10 +58,12 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi ) async throws -> PluginImportResult { let startTime = Date() let url = source.fileURL() + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-import. + let options = settings let text: CSVImportText do { - text = try CSVImportText.contents(of: url, encoding: settings.encoding, isCancelled: { progress.isCancelled }) + text = try CSVImportText.contents(of: url, encoding: options.encoding, isCancelled: { progress.isCancelled }) } catch is TabularCancellation { throw PluginImportCancellationError() } catch { @@ -80,9 +82,9 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi } let data = text.data - let dialect = CSVImportParsing.resolveDialect(in: data, options: settings) + let dialect = CSVImportParsing.resolveDialect(in: data, options: options) let parser = CSVStreamingParser(dialect: dialect) - let hasHeader = settings.hasHeaderRow + let hasHeader = options.hasHeaderRow let (dataRanges, columnNames) = indexRowsAndNames(in: data, parser: parser, hasHeader: hasHeader) guard !columnNames.isEmpty else { @@ -93,7 +95,7 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi let lineOffset = hasHeader ? 2 : 1 var cursor = 0 - let configuration = runConfiguration + let configuration = Self.runConfiguration(options) var pendingFailure: PluginImportError? var malformedCount = 0 var malformedErrors: [PluginImportResult.ImportStatementError] = [] @@ -111,7 +113,8 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi ranges: dataRanges[cursor..>, startIndex: Int, lineOffset: Int, - columnNames: [String] + columnNames: [String], + options: CSVImportOptions ) -> (rows: [RowImportRunner.Entry], malformed: [MalformedRow]) { data.withUnsafeBytes { raw -> (rows: [RowImportRunner.Entry], malformed: [MalformedRow]) in guard let base = raw.bindMemory(to: UInt8.self).baseAddress else { return ([], []) } @@ -185,12 +189,12 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi if CSVImportParsing.isBlank(fields) { continue } let line = startIndex + offset + lineOffset if let error = CSVImportParsing.shapeError( - fields: fields, columnCount: columnNames.count, options: settings + fields: fields, columnCount: columnNames.count, options: options ) { malformed.append((line, error)) continue } - out.append((line, CSVImportParsing.row(fields: fields, columnNames: columnNames, options: settings))) + out.append((line, CSVImportParsing.row(fields: fields, columnNames: columnNames, options: options))) } return (out, malformed) } diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift index f59b0a903d..ff8717faec 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift @@ -324,7 +324,7 @@ internal final class CassandraPluginDriver: PluginDatabaseDriver, @unchecked Sen let rawResult = try await connectionActor.executeQuery( query, consistency: consistency, cancellation: cancellation ) - if let refusal = CassandraRowWriter.unappliedInsertRefusal( + if let refusal = CassandraRowWriter.unappliedWriteRefusal( statement: query, columns: rawResult.columns, rows: rawResult.rows ) { throw CassandraPluginError.queryFailed(refusal) @@ -371,7 +371,7 @@ internal final class CassandraPluginDriver: PluginDatabaseDriver, @unchecked Sen let rawResult = try await connectionActor.executePrepared( query, parameters: parameters, consistency: consistency ) - if let refusal = CassandraRowWriter.unappliedInsertRefusal( + if let refusal = CassandraRowWriter.unappliedWriteRefusal( statement: query, columns: rawResult.columns, rows: rawResult.rows ) { throw CassandraPluginError.queryFailed(refusal) diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraRowWriter.swift b/apple/Plugins/CassandraDriverPlugin/CassandraRowWriter.swift index 0c7ce00b73..ca4edf9125 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraRowWriter.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraRowWriter.swift @@ -4,16 +4,15 @@ // import Foundation +import TableProCassandraCore import TableProPluginKit -/// The CQL a grid save writes: one statement per row, each naming the row by its whole primary key. -/// -/// CQL has no `OR` and no parenthesised groups, so several rows cannot share one `DELETE`, and an `INSERT` is an -/// upsert, so a new row whose key is taken would silently replace the row already there. A new row is therefore -/// written `IF NOT EXISTS`, and anything CQL cannot do to a row is refused by name rather than sent. +// One statement per row, each naming the row by its whole primary key: CQL has no `OR` and no parenthesised groups. +// A new row is written `IF NOT EXISTS` and an edit `IF EXISTS` (CassandraConditionalWrite), and anything CQL cannot +// do to a row is refused by name rather than sent. enum CassandraRowWriter { static let defaultSentinel = "__DEFAULT__" - static let unappliedMarker = "[applied]" + static let unappliedMarker = CassandraConditionalWrite.appliedColumn static func rowWrites( keyspace: String?, @@ -54,15 +53,18 @@ enum CassandraRowWriter { } } - /// An `INSERT … IF NOT EXISTS` that found its key taken answers with `[applied] = false` and the row already - /// there, which the server reports as a success. - static func unappliedInsertRefusal(statement: String, columns: [String], rows: [[PluginCellValue]]) -> String? { - let normalized = statement.uppercased() - guard normalized.hasPrefix("INSERT"), normalized.hasSuffix("IF NOT EXISTS"), - columns.first == unappliedMarker, - rows.first?.first?.asText?.lowercased() == "false" - else { return nil } - return String(localized: "A row with this primary key already exists. Edit that row instead.") + static func unappliedWriteRefusal(statement: String, columns: [String], rows: [[PluginCellValue]]) -> String? { + let refusal = CassandraConditionalWrite.refusal( + statement: statement, firstColumn: columns.first, firstValue: rows.first?.first?.asText + ) + switch refusal { + case .keyTaken: + return String(localized: "A row with this primary key already exists. Edit that row instead.") + case .rowMissing: + return String(localized: "No row has this primary key, so nothing was updated. Another client may have deleted it.") + case nil: + return nil + } } private struct Writer { @@ -100,8 +102,7 @@ enum CassandraRowWriter { )) } } - let statement = "INSERT INTO \(target) (\(names.joined(separator: ", "))) " - + "VALUES (\(placeholders.joined(separator: ", "))) IF NOT EXISTS" + let statement = CassandraConditionalWrite.insertStatement(target: target, names: names, values: placeholders) return PluginRowWrite(statement: statement, parameters: parameters, rowIndices: [change.rowIndex]) } @@ -133,7 +134,9 @@ enum CassandraRowWriter { } } let key = try keyCondition(for: change) - let statement = "UPDATE \(target) SET \(assignments.joined(separator: ", ")) WHERE \(key.cql)" + let statement = CassandraConditionalWrite.updateStatement( + target: target, assignments: assignments, keyCondition: key.cql + ) return PluginRowWrite(statement: statement, parameters: parameters + key.values, rowIndices: [change.rowIndex]) } diff --git a/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift b/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift index cc80a87298..70b89896ec 100644 --- a/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift +++ b/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift @@ -323,10 +323,9 @@ final class DamengPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let task = Task { do { let result = try await self.execute(query: query, context: context) - continuation.yield(.header(PluginStreamHeader( - columns: result.columns, - columnTypeNames: result.columnTypeNames - ))) + var header = PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) if !result.rows.isEmpty { continuation.yield(.rows(result.rows)) } diff --git a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift index ee013d3fe9..72389cdf12 100644 --- a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift +++ b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift @@ -373,7 +373,9 @@ extension DynamoDBPluginDriver { source = try await spoolSource(for: statement, session: session) } catch DynamoDBError.invalidStatement(let message) where message == Self.notAReadMarker { let result = try await runStatement(text, parameters: [], rowCap: PluginRowLimits.emergencyMax) - continuation.yield(.header(PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames))) + var header = PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) if !result.rows.isEmpty { continuation.yield(.rows(result.rows)) } return } diff --git a/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift b/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift index e8d3c50f61..038ed7dbcc 100644 --- a/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift +++ b/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift @@ -242,11 +242,11 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation: AsyncThrowingStream.Continuation ) async throws { let matcher = try filter.matcher() - continuation.yield(.header(PluginStreamHeader( + var header = PluginStreamHeader( columns: Self.columns, columnTypeNames: Self.columnTypeNames, estimatedRowCount: nil - ))) + ) let range = Self.prefixRange(prefix) guard sortAscending else { @@ -256,6 +256,9 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { req.sortTarget = "KEY" let response = try await client.rangeRequest(req) let rows = (response.kvs ?? []).map(Self.filterRow).filter(matcher.matches).map(\.cells) + // `more` says the range held keys past the limit. + if response.more == true { header.rowLimit = rows.count } + continuation.yield(.header(header)) if !rows.isEmpty { continuation.yield(.rows(rows)) } @@ -263,6 +266,7 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { return } + continuation.yield(.header(header)) do { try await scanKeyValues(in: range, client: client) { kvs in let rows = kvs.map(Self.filterRow).filter(matcher.matches).map(\.cells) diff --git a/apple/Plugins/FormatPluginTests/CSVExportFormulaAndDecimalTests.swift b/apple/Plugins/FormatPluginTests/CSVExportFormulaAndDecimalTests.swift new file mode 100644 index 0000000000..db59df3638 --- /dev/null +++ b/apple/Plugins/FormatPluginTests/CSVExportFormulaAndDecimalTests.swift @@ -0,0 +1,174 @@ +// +// CSVExportFormulaAndDecimalTests.swift +// FormatPluginTests +// + +import Foundation +import TableProPluginKit +import Testing + +private final class TypedCSVSource: PluginExportDataSource, @unchecked Sendable { + let databaseTypeId = "PostgreSQL" + private let header: PluginStreamHeader + private let rows: [PluginRow] + + init(columns: [String], columnTypeNames: [String], rows: [PluginRow]) { + header = PluginStreamHeader(columns: columns, columnTypeNames: columnTypeNames) + self.rows = rows + } + + func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { + let header = header + let rows = rows + return AsyncThrowingStream { continuation in + continuation.yield(.header(header)) + continuation.yield(.rows(rows)) + continuation.finish() + } + } + + func fetchTableDDL(table: String, databaseName: String) async throws -> String { "" } + + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func quoteIdentifier(_ identifier: String) -> String { "\"\(identifier)\"" } + + func escapeStringLiteral(_ value: String) -> String { value } + + func fetchApproximateRowCount(table: String, databaseName: String) async throws -> Int? { nil } +} + +struct CSVExportFormulaAndDecimalTests { + private func export( + _ options: CSVExportOptions, + columns: [String], + types: [String], + rows: [PluginRow] + ) async throws -> (text: String, warnings: [String]) { + let output = try await CSVExportHarness.shared.bytes( + options: options, + tables: [PluginExportTable(name: "users", databaseName: "", tableType: "table", schema: nil, kind: .table)], + dataSource: TypedCSVSource(columns: columns, columnTypeNames: types, rows: rows) + ) + return (String(decoding: output.data, as: UTF8.self), output.result.warnings) + } + + // A database export is read back by importers, which keep the quote as part of the value. + @Test("The stock export writes a value that starts like a formula as it is") + func stockExportKeepsValues() async throws { + let output = try await export( + CSVExportOptions(), + columns: ["handle"], + types: ["text"], + rows: [["@handle"], ["+84 90 123 4567"], ["-pending"]] + ) + #expect(output.text == "handle\n@handle\n+84 90 123 4567\n-pending\n") + #expect(output.warnings.isEmpty) + } + + @Test("Sanitizing says how many values it gave a leading quote") + func sanitizingCountsWhatItChanged() async throws { + var options = CSVExportOptions() + options.sanitizeFormulas = true + let output = try await export( + options, + columns: ["handle"], + types: ["text"], + rows: [["@handle"], ["plain"], ["=1+1"], ["-5"], ["-pending"]] + ) + #expect(output.text == "handle\n\"'@handle\"\nplain\n\"'=1+1\"\n-5\n\"'-pending\"\n") + #expect(output.warnings == CSVExportPlugin.neutralisedWarnings(3)) + } + + @Test("Nothing changed means no warning about changes") + func noCountWithoutChanges() { + #expect(CSVExportPlugin.neutralisedWarnings(0).isEmpty) + } + + // A text column holding "1.10" is a version or a code, not a number. + @Test("A decimal comma leaves text columns alone and keeps columns with no usable type") + func decimalCommaSkipsTextColumns() async throws { + var options = CSVExportOptions() + options.decimalFormat = .comma + options.delimiter = .semicolon + let output = try await export( + options, + columns: ["version", "amount", "untyped"], + types: ["varchar(16)", "numeric(10,2)", ""], + rows: [["1.10", "1.10", "1.10"]] + ) + #expect(output.text == "version;amount;untyped\n1.10;1,10;1,10\n") + } + + @Test("A decimal comma reaches every engine's numeric type", arguments: [ + "Nullable(Float64)", "Nullable(Decimal(10, 2))", "money", "smallmoney", "BINARY_DOUBLE", "BINARY_FLOAT", + "BIGNUMERIC", "double precision", "NUMBER(10,2)" + ]) + func decimalCommaReachesNumericTypes(typeName: String) async throws { + var options = CSVExportOptions() + options.decimalFormat = .comma + options.delimiter = .semicolon + let output = try await export(options, columns: ["n"], types: [typeName], rows: [["1.5"]]) + #expect(output.text == "n\n1,5\n") + } + + @Test("A decimal comma never reaches a text type", arguments: [ + "varchar(16)", "String", "LowCardinality(String)", "nvarchar(10)", "TEXT", "jsonb" + ]) + func decimalCommaSkipsTextTypes(typeName: String) async throws { + var options = CSVExportOptions() + options.decimalFormat = .comma + options.delimiter = .semicolon + let output = try await export(options, columns: ["n"], types: [typeName], rows: [["1.5"]]) + #expect(output.text == "n\n1.5\n") + } + + @Test("A header name sanitizing changes is counted with the values") + func headerNamesAreCounted() async throws { + var options = CSVExportOptions() + options.sanitizeFormulas = true + let output = try await export(options, columns: ["=total"], types: ["text"], rows: [["=1"]]) + #expect(output.text == "\"'=total\"\n\"'=1\"\n") + #expect(output.warnings == CSVExportPlugin.neutralisedWarnings(2)) + } + + // `'-1,5` is text to a spreadsheet, so a negative number written with a decimal comma lost its value. + @Test("A number written with a decimal comma is never given a quote") + func commaDecimalsAreNotSanitized() async throws { + var options = CSVExportOptions() + options.sanitizeFormulas = true + options.decimalFormat = .comma + options.delimiter = .semicolon + let output = try await export( + options, + columns: ["amount", "note"], + types: ["numeric", "text"], + rows: [["-1.5", "-1.5x"]] + ) + #expect(output.text == "amount;note\n-1,5;\"'-1.5x\"\n") + #expect(output.warnings == CSVExportPlugin.neutralisedWarnings(1)) + } +} + +struct PluginRowWritersFormulaLeadTests { + // OWASP lists a leading line feed and the full-width operators, which a Japanese locale reads as formulas. + @Test("A leading line feed or a full-width operator counts as a formula lead") + func owaspLeadsAreNeutralised() { + let options = PluginCsvWriteOptions.default + #expect(PluginRowWriters.csvField("\n=1+1", options: options).hasPrefix("\"'")) + for lead in ["\u{FF1D}", "\u{FF0B}", "\u{FF0D}", "\u{FF20}"] { + #expect(PluginRowWriters.csvField("\(lead)SUM(A1)", options: options) == "\"'\(lead)SUM(A1)\"") + } + } + + @Test("Only a value that would be prefixed is formula-like") + func formulaLikeMatchesTheWriter() { + #expect(PluginRowWriters.isFormulaLike("=1+1")) + #expect(PluginRowWriters.isFormulaLike("\t=1+1")) + #expect(!PluginRowWriters.isFormulaLike("-5")) + #expect(!PluginRowWriters.isFormulaLike("plain")) + #expect(!PluginRowWriters.isFormulaLike("")) + } +} diff --git a/apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift b/apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift index 29503ea388..ed0cae7764 100644 --- a/apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift +++ b/apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift @@ -85,6 +85,14 @@ struct ExportImportRoundTripTests { try RoundTrip.expectSame(target, RoundTripFixture.table, FormatLimits.csv.carried(RoundTripFixture.rows)) } + @Test("A CSV file sanitized for a spreadsheet imports back with the quote it gave formula-like values") + func csvSanitizedImportsBackWithItsQuotes() async throws { + var options = CSVExportOptions() + options.sanitizeFormulas = true + let target = try await RoundTrip.csv(options) + try RoundTrip.expectSame(target, RoundTripFixture.table, FormatLimits.csvSanitized.carried(RoundTripFixture.rows)) + } + @Test("A CSV file written with a byte order mark imports back") func csvWithByteOrderMarkImportsBack() async throws { var options = CSVExportOptions() @@ -166,9 +174,14 @@ struct ExportImportRoundTripTests { extension FormatLimits { // CSV has no NULL and no binary: both NULL and '' are written as an empty field and read as - // NULL, and a binary value is written as hex text. A value a spreadsheet would run as a - // formula gains a leading quote. + // NULL, and a binary value is written as hex text. static let csv = FormatLimits( + emptyTextIsNull: true, + binary: { .text(FormatLimits.hex($0)) } + ) + + // Sanitizing for a spreadsheet gives a value that would run as a formula a leading quote, which the import keeps. + static let csvSanitized = FormatLimits( emptyTextIsNull: true, binary: { .text(FormatLimits.hex($0)) }, text: { FormatLimits.csvNeutralised[$0] ?? $0 } diff --git a/apple/Plugins/FormatPluginTests/FormatSettingsChangedMidRunTests.swift b/apple/Plugins/FormatPluginTests/FormatSettingsChangedMidRunTests.swift new file mode 100644 index 0000000000..f995008f40 --- /dev/null +++ b/apple/Plugins/FormatPluginTests/FormatSettingsChangedMidRunTests.swift @@ -0,0 +1,185 @@ +// +// FormatSettingsChangedMidRunTests.swift +// FormatPluginTests +// + +import Foundation +import TableProPluginKit +import Testing + +// One plugin instance serves every window, so a dialog open in another window changes `settings` while a run reads. +// Each source here changes them between its first and second batch, the way that dialog would. +private final class TwoBatchSource: PluginExportDataSource, @unchecked Sendable { + let databaseTypeId = "SQLite" + private let header: PluginStreamHeader + private let batches: [[PluginRow]] + private let betweenBatches: @Sendable () -> Void + + init(columns: [String], batches: [[PluginRow]], betweenBatches: @escaping @Sendable () -> Void) { + header = PluginStreamHeader(columns: columns, columnTypeNames: columns.map { _ in "TEXT" }) + self.batches = batches + self.betweenBatches = betweenBatches + } + + // Pulled, so the change lands only after the plugin has written the first batch. + func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { + let header = header + let batches = batches + let betweenBatches = betweenBatches + let pulls = Counter() + return AsyncThrowingStream(unfolding: { + let pull = pulls.next() + if pull == 0 { return .header(header) } + guard pull <= batches.count else { return nil } + if pull == 2 { betweenBatches() } + return .rows(batches[pull - 1]) + }) + } + + func fetchTableDDL(table: String, databaseName: String) async throws -> String { "" } + + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func quoteIdentifier(_ identifier: String) -> String { "\"\(identifier)\"" } + + func escapeStringLiteral(_ value: String) -> String { value } + + func fetchApproximateRowCount(table: String, databaseName: String) async throws -> Int? { nil } +} + +private final class Counter: @unchecked Sendable { + private let lock = NSLock() + private var value = 0 + + func next() -> Int { + lock.lock() + defer { lock.unlock() } + defer { value += 1 } + return value + } +} + +private final class ForeignKeyRecordingSink: PluginImportDataSink, @unchecked Sendable { + let databaseTypeId = "SQLite" + var onExecute: () -> Void = {} + private(set) var disabled = 0 + private(set) var enabled = 0 + + func execute(statement: String) async throws { onExecute() } + func beginTransaction() async throws {} + func commitTransaction() async throws {} + func rollbackTransaction() async throws {} + func disableForeignKeyChecks() async throws { disabled += 1 } + func enableForeignKeyChecks() async throws { enabled += 1 } +} + +private final class OneStatementSource: PluginImportSource, @unchecked Sendable { + func statements() async throws -> AsyncThrowingStream<(statement: String, lineNumber: Int), Error> { + AsyncThrowingStream { continuation in + continuation.yield((statement: "INSERT INTO t VALUES (1);", lineNumber: 1)) + continuation.finish() + } + } + + func fileURL() -> URL { URL(fileURLWithPath: "/dev/null") } + + func fileSizeBytes() -> Int64 { 0 } +} + +@Suite(.serialized) +struct FormatSettingsChangedMidRunTests { + private let table = PluginExportTable(name: "t", databaseName: "", tableType: "table", schema: nil, kind: .table) + private let rowWithNull: PluginRow = [.text("1"), .null] + + private func export( + _ plugin: any ExportFormatPlugin, + from source: TwoBatchSource, + fileExtension: String + ) async throws -> Data { + let destination = FileManager.default.temporaryDirectory + .appendingPathComponent("\(UUID().uuidString).\(fileExtension)") + defer { try? FileManager.default.removeItem(at: destination) } + _ = try await plugin.export( + tables: [table], + dataSource: source, + destination: destination, + progress: PluginExportProgress(progress: Progress(totalUnitCount: 1)) + ) + return try Data(contentsOf: destination) + } + + @Test("JSON keeps writing null keys after another window turns Include NULL values off") + func jsonKeepsItsNullChoice() async throws { + let plugin = JSONExportPlugin() + let stored = plugin.settings + defer { plugin.settings = stored } + var options = JSONExportOptions() + options.layout = .newlineDelimited + options.includeNullValues = true + plugin.settings = options + + let source = TwoBatchSource(columns: ["id", "note"], batches: [[rowWithNull], [rowWithNull]]) { + plugin.settings.includeNullValues = false + } + let text = String(decoding: try await export(plugin, from: source, fileExtension: "ndjson"), as: UTF8.self) + #expect(text == "{\"id\": \"1\", \"note\": null}\n{\"id\": \"1\", \"note\": null}\n") + } + + @Test("Markdown keeps its NULL placeholder after another window changes it") + func markdownKeepsItsPlaceholder() async throws { + let plugin = MarkdownExportPlugin() + let stored = plugin.settings + defer { plugin.settings = stored } + var options = MarkdownExportOptions() + options.alignsColumns = false + options.includesTableNames = false + options.nullPlaceholder = "NULL" + plugin.settings = options + + let source = TwoBatchSource(columns: ["id", "note"], batches: [[rowWithNull], [rowWithNull]]) { + plugin.settings.nullPlaceholder = "(none)" + } + let text = String(decoding: try await export(plugin, from: source, fileExtension: "md"), as: UTF8.self) + #expect(!text.contains("(none)")) + #expect(text.components(separatedBy: "| 1 | NULL |").count == 3) + } + + @Test("XLSX keeps writing NULL as an empty cell after another window turns the option off") + func xlsxKeepsItsNullChoice() async throws { + let plugin = XLSXExportPlugin() + let stored = plugin.settings + defer { plugin.settings = stored } + var options = XLSXExportOptions() + options.convertNullToEmpty = true + plugin.settings = options + + let source = TwoBatchSource(columns: ["id", "note"], batches: [[rowWithNull], [rowWithNull]]) { + plugin.settings.convertNullToEmpty = false + } + let written = try await export(plugin, from: source, fileExtension: "xlsx") + let text = try #require(String(bytes: written, encoding: .isoLatin1)) + #expect(!text.contains("NULL")) + } + + @Test("A SQL import turns foreign key checks back on after another window unticks the option") + func sqlImportRestoresTheChecksItTurnedOff() async throws { + let plugin = SQLImportPlugin() + let stored = plugin.settings + defer { plugin.settings = stored } + var options = SQLImportOptions() + options.disableForeignKeyChecks = true + plugin.settings = options + + let sink = ForeignKeyRecordingSink() + sink.onExecute = { plugin.settings.disableForeignKeyChecks = false } + _ = try await plugin.performImport( + source: OneStatementSource(), + sink: sink, + progress: PluginImportProgress(progress: Progress()) + ) + #expect(sink.disabled == 1) + #expect(sink.enabled == 1) + } +} diff --git a/apple/Plugins/JSONExportPlugin/JSONExportPlugin.swift b/apple/Plugins/JSONExportPlugin/JSONExportPlugin.swift index cf23a29b92..60adecb627 100644 --- a/apple/Plugins/JSONExportPlugin/JSONExportPlugin.swift +++ b/apple/Plugins/JSONExportPlugin/JSONExportPlugin.swift @@ -53,10 +53,11 @@ final class JSONExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug } } - /// NDJSON is one row per line by definition, so the pretty-print setting cannot apply to it - /// and the wrapping object has no place to go. - let isNewlineDelimited = settings.layout == .newlineDelimited - let prettyPrint = settings.prettyPrint && !isNewlineDelimited + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-export. + let options = settings + // NDJSON is one row per line by definition, so pretty-printing and the wrapping object do not apply. + let isNewlineDelimited = options.layout == .newlineDelimited + let prettyPrint = options.prettyPrint && !isNewlineDelimited let indent = prettyPrint ? " " : "" let newline = prettyPrint ? "\n" : "" @@ -104,7 +105,7 @@ final class JSONExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug for (colIndex, column) in columns.enumerated() { if colIndex < row.count { let value = row[colIndex] - if settings.includeNullValues || !value.isNull { + if options.includeNullValues || !value.isNull { if !isFirstField { rowString += ", " } @@ -117,7 +118,7 @@ final class JSONExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let jsonValue = formatJSONValue( value, columnTypeName: colTypeName, - preserveAsString: settings.preserveAllAsStrings + preserveAsString: options.preserveAllAsStrings ) rowString += "\"\(escapedKey)\": \(jsonValue)" } diff --git a/apple/Plugins/MarkdownExportPlugin/MarkdownExportPlugin.swift b/apple/Plugins/MarkdownExportPlugin/MarkdownExportPlugin.swift index 2ee2f17664..8fc08e0fd3 100644 --- a/apple/Plugins/MarkdownExportPlugin/MarkdownExportPlugin.swift +++ b/apple/Plugins/MarkdownExportPlugin/MarkdownExportPlugin.swift @@ -54,13 +54,15 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable if !committed { PluginExportUtilities.rollbackAtomicWrite(at: tempURL) } } + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-export. + let options = settings for (index, table) in tables.enumerated() { try progress.checkCancellation() progress.setCurrentTable(table.qualifiedName, index: index + 1) - if settings.includesTableNames { + if options.includesTableNames { try fileHandle.write(contentsOf: "## \(table.qualifiedName)\n\n".toUTF8Data()) } - try await writeTable(table, dataSource: dataSource, to: fileHandle, progress: progress) + try await writeTable(table, options: options, dataSource: dataSource, to: fileHandle, progress: progress) try fileHandle.write(contentsOf: "\n".toUTF8Data()) } @@ -73,6 +75,7 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable private func writeTable( _ table: PluginExportTable, + options: MarkdownExportOptions, dataSource: any PluginExportDataSource, to fileHandle: FileHandle, progress: PluginExportProgress @@ -89,9 +92,9 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable columns = header.columns case .rows(let rows): for row in rows { - let cells = row.map { cellText($0) } + let cells = row.map { cellText($0, nullPlaceholder: options.nullPlaceholder) } progress.incrementRow() - guard settings.alignsColumns, !wroteHeader else { + guard options.alignsColumns, !wroteHeader else { if !wroteHeader { try writeHeader(columns, widths: nil, to: fileHandle) wroteHeader = true @@ -115,7 +118,7 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable } if !wroteHeader { - widths = settings.alignsColumns + widths = options.alignsColumns ? MarkdownTableRenderer.widths(header: columns, sample: buffered) : nil try writeHeader(columns, widths: widths, to: fileHandle) @@ -138,10 +141,10 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable ).toUTF8Data()) } - private func cellText(_ value: PluginCellValue) -> String { + private func cellText(_ value: PluginCellValue, nullPlaceholder: String) -> String { switch value { case .null: - return MarkdownTableRenderer.cell(settings.nullPlaceholder) + return MarkdownTableRenderer.cell(nullPlaceholder) case .bytes(let data): return MarkdownTableRenderer.cell("0x\(data.map { String(format: "%02X", $0) }.joined())") case .text(let text): diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift index 3be35d7dd1..55c8df7418 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift @@ -518,7 +518,10 @@ nonisolated extension MongoDBConnection { let documents = try MongoExportDocument.read( MongoReadDocuments(dictionaries: sample, texts: texts), representation: uuidRepresentation ) - continuation.yield(.header(projection.header)) + var header = projection.header + // iterateCursorStreaming stops there, so a stream that reaches it was cut. + header.rowLimit = PluginRowLimits.emergencyMax + continuation.yield(.header(header)) if !sample.isEmpty { let rows = BsonDocumentFlattener.flatten( documents: sample, columns: projection.columns, kinds: projection.kinds, diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift index 5e10afbea4..05bfefbd69 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift @@ -1031,10 +1031,9 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se } ) if !result.columns.isEmpty { - continuation.yield(.header(PluginStreamHeader( - columns: result.columns, - columnTypeNames: result.columnTypeNames - ))) + var header = PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) } if !result.rows.isEmpty { continuation.yield(.rows(result.rows, documents: result.exportDocuments)) diff --git a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift index edea147522..ba69a63dc3 100644 --- a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift +++ b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift @@ -88,7 +88,19 @@ struct MariaDBPluginQueryResult { // MARK: - Connection Class final class MariaDBPluginConnection: @unchecked Sendable { - private var mysql: UnsafeMutablePointer? + // Under stateLock, which disconnect() clears it under. Only `queue` may call into the handle: the close is queued there. + private var _mysql: UnsafeMutablePointer? + private var _serverThreadId: UInt = 0 + + private var mysql: UnsafeMutablePointer? { + stateLock.withLock { _mysql } + } + + // Read from the snapshot taken at connect: Stop runs off `queue`, where the handle may already be freed. + private var connectedThreadId: UInt? { + stateLock.withLock { _mysql == nil ? nil : _serverThreadId } + } + private let queue = DispatchQueue(label: "com.TablePro.mariadb.plugin", qos: .userInitiated) /// Serial, and separate from `queue`, which the read being cancelled is sitting on. Serial so a @@ -342,9 +354,9 @@ final class MariaDBPluginConnection: @unchecked Sendable { if let cachePath = kerberos?.cachePath { KerberosTicket.discard(cachePath) } - let handle = mysql + let handle = _mysql let cleanupQueue = queue - mysql = nil + _mysql = nil if let handle = handle { cleanupQueue.async { mysql_close(handle) @@ -390,8 +402,10 @@ final class MariaDBPluginConnection: @unchecked Sendable { self.baselineSelectLimit = nil self.hasCapturedBaselineSelectLimit = false + let threadId = mysql_thread_id(handle) self.stateLock.lock() - self.mysql = handle + self._mysql = handle + self._serverThreadId = threadId self._isConnected = true self._errorLanguage = session.errorLanguage self._sessionEndedByKill = false @@ -600,10 +614,10 @@ final class MariaDBPluginConnection: @unchecked Sendable { KerberosTicket.discard(cachePath) } - let handle = mysql - mysql = nil - stateLock.lock() + let handle = _mysql + _mysql = nil + _serverThreadId = 0 _isConnected = false stateLock.unlock() @@ -635,7 +649,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { let target = killTarget guard mayEndSession || !target.endsSession else { return } - guard let mysql = mysql, let statement = target.statement(threadId: mysql_thread_id(mysql)) else { return } + guard let threadId = connectedThreadId, let statement = target.statement(threadId: threadId) else { return } cancelQueue.async { [self] in guard deliverKill(statement) else { return } if target.endsSession { @@ -657,11 +671,9 @@ final class MariaDBPluginConnection: @unchecked Sendable { recordKillDelivered(generation: generation) } - /// The server thread this connection is on, read before a statement goes out so a kill still - /// has somewhere to go once the handle itself is unusable. + // Read before a statement goes out, so a kill still has somewhere to go once the handle itself is unusable. internal var currentThreadId: UInt { - guard let mysql = self.mysql else { return 0 } - return mysql_thread_id(mysql) + connectedThreadId ?? 0 } /// The kill has to reach the server the query is running on, which means repeating the transport diff --git a/apple/Plugins/MySQLDriverTests/MariaDBStopThreadIdTests.swift b/apple/Plugins/MySQLDriverTests/MariaDBStopThreadIdTests.swift new file mode 100644 index 0000000000..52bb160f9a --- /dev/null +++ b/apple/Plugins/MySQLDriverTests/MariaDBStopThreadIdTests.swift @@ -0,0 +1,38 @@ +// +// MariaDBStopThreadIdTests.swift +// MySQLDriverTests +// + +import Foundation +import TableProPluginKit +import Testing + +// Stop runs off the statement queue, where a disconnect's queued close can free the handle under it, so the kill's +// thread id is the one the handshake gave, held beside the handle and cleared with it. +@Suite(.timeLimit(.minutes(1))) +struct MariaDBStopThreadIdTests { + @Test("The kill's thread id is the session's own, and none is left once the session is closed") + func threadIdFollowsTheSession() async throws { + let server = try PlaintextMySQLServer(asksForCleartext: false) + defer { server.close() } + async let session = server.serveOneSession() + let connection = MariaDBPluginConnection( + transport: .tcp(host: "127.0.0.1", port: UInt32(server.port)), + user: "app", + password: "app-password", + database: "", + sslConfig: SSLConfiguration(mode: .disabled), + enableCleartextPlugin: false + ) + + try await connection.connect() + let signedIn = connection.currentThreadId + connection.disconnect() + let closed = connection.currentThreadId + _ = await session + + // The fake server's handshake names connection 1. + #expect(signedIn == 1) + #expect(closed == 0) + } +} diff --git a/apple/Plugins/ParquetExportPlugin/ParquetExportPlugin.swift b/apple/Plugins/ParquetExportPlugin/ParquetExportPlugin.swift index 90eeda01e8..2495d396c6 100644 --- a/apple/Plugins/ParquetExportPlugin/ParquetExportPlugin.swift +++ b/apple/Plugins/ParquetExportPlugin/ParquetExportPlugin.swift @@ -59,10 +59,12 @@ final class ParquetExportPlugin: ObservableObject, ExportFormatPlugin, SettableP progress: PluginExportProgress ) async throws -> ExportFormatResult { guard !tables.isEmpty else { return ExportFormatResult() } + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-export. + let options = settings var warnings: [String] = [] try await ParquetTableFiles.write(tables, destination: destination, progress: progress) { table, fileURL in - warnings += try await writeTable(table, dataSource: dataSource, to: fileURL, progress: progress) + warnings += try await writeTable(table, options: options, dataSource: dataSource, to: fileURL, progress: progress) } if tables.count > 1 { @@ -76,6 +78,7 @@ final class ParquetExportPlugin: ObservableObject, ExportFormatPlugin, SettableP private func writeTable( _ table: PluginExportTable, + options: ParquetExportOptions, dataSource: any PluginExportDataSource, to fileURL: URL, progress: PluginExportProgress @@ -123,8 +126,8 @@ final class ParquetExportPlugin: ObservableObject, ExportFormatPlugin, SettableP } let fallbacks = try staging.copyToParquet( fileURL: fileURL, - compression: settings.compression.duckDBValue, - rowGroupSize: settings.rowGroupSize + compression: options.compression.duckDBValue, + rowGroupSize: options.rowGroupSize ) return fallbacks.map { fallback in String( diff --git a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+Operations.swift b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+Operations.swift index c2f93088de..6ac131744d 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+Operations.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+Operations.swift @@ -141,8 +141,9 @@ extension RedisPluginDriver { if opts.xx { args.append("XX".redisArgument) } if opts.keepTTL { args.append("KEEPTTL".redisArgument) } } - try await conn.run(args) - return buildStatusResult("OK", startTime: startTime) + // NX or XX that stops the write answers nil, not an error. + let reply = try await conn.run(args) + return buildStatusResult(reply.displayText, startTime: startTime) case .del(let keys): let args = ["DEL"] + keys diff --git a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift index 43d70ee2b9..4d4ffcb41b 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift @@ -561,11 +561,13 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { default: let startTime = Date() let result = try await executeOperation(operation, connection: conn, startTime: startTime) - continuation.yield(.header(PluginStreamHeader( + var header = PluginStreamHeader( columns: result.columns, columnTypeNames: result.columnTypeNames, estimatedRowCount: nil - ))) + ) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) if !result.rows.isEmpty { continuation.yield(.rows(result.rows)) } diff --git a/apple/Plugins/RedisDriverTests/RedisConditionalWriteTests.swift b/apple/Plugins/RedisDriverTests/RedisConditionalWriteTests.swift index 72e4aa3bd3..4402cf7e01 100644 --- a/apple/Plugins/RedisDriverTests/RedisConditionalWriteTests.swift +++ b/apple/Plugins/RedisDriverTests/RedisConditionalWriteTests.swift @@ -180,6 +180,25 @@ struct RedisConditionalWriteTests { #expect(result.rows.first?.first?.asText == "OK") } + @Test("A SET that NX or XX kept from writing shows nil, as the server answered") + func skippedSetShowsNil() async throws { + let channel = StubRedisChannel([.null]) + let result = try await driver().executeOperation( + try RedisCommandParser.parse("SET lock:job 1 NX"), connection: channel, startTime: Date() + ) + #expect(result.rows.first?.first?.asText == "(nil)") + #expect(channel.sentCommands == [["SET", "lock:job", "1", "NX"]]) + } + + @Test("A SET that wrote the key shows OK") + func appliedSetShowsOK() async throws { + let channel = StubRedisChannel([.status("OK")]) + let result = try await driver().executeOperation( + try RedisCommandParser.parse("SET lock:job 1 XX EX 60"), connection: channel, startTime: Date() + ) + #expect(result.rows.first?.first?.asText == "OK") + } + @Test("A conditional write the server refused outright keeps the server's message") func execKeepsServerErrorForConditionalWrite() { let reply = RedisReply.array([.error("ERR no such key")]) diff --git a/apple/Plugins/SQLImportPlugin/SQLImportPlugin.swift b/apple/Plugins/SQLImportPlugin/SQLImportPlugin.swift index 91b6a9c205..137d0a7ac3 100644 --- a/apple/Plugins/SQLImportPlugin/SQLImportPlugin.swift +++ b/apple/Plugins/SQLImportPlugin/SQLImportPlugin.swift @@ -51,15 +51,17 @@ final class SQLImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi var errors: [PluginImportResult.ImportStatementError] = [] let maxErrors = 1_000 - let errorMode = settings.errorHandling - let useTransaction = settings.wrapInTransaction && errorMode != .skipAndContinue + // One plugin instance serves every window: a checkbox unticked elsewhere mid-import left the checks off. + let options = settings + let errorMode = options.errorHandling + let useTransaction = options.wrapInTransaction && errorMode != .skipAndContinue let fileSizeBytes = source.fileSizeBytes() let estimatedTotal = max(1, Int(fileSizeBytes / 500)) progress.setEstimatedTotal(estimatedTotal) do { - if settings.disableForeignKeyChecks { + if options.disableForeignKeyChecks { try await sink.disableForeignKeyChecks() } @@ -99,7 +101,7 @@ final class SQLImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi Self.logger.warning("Failed to commit partial import") } } - if settings.disableForeignKeyChecks { + if options.disableForeignKeyChecks { do { try await sink.enableForeignKeyChecks() } catch { @@ -134,7 +136,7 @@ final class SQLImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi didCommit = true } - if settings.disableForeignKeyChecks { + if options.disableForeignKeyChecks { do { try await sink.enableForeignKeyChecks() } catch { @@ -156,7 +158,7 @@ final class SQLImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi } } - if settings.disableForeignKeyChecks { + if options.disableForeignKeyChecks { do { try await sink.enableForeignKeyChecks() } catch { diff --git a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift index dc8a667c2c..53d2af6929 100644 --- a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift +++ b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift @@ -1567,7 +1567,8 @@ public extension PluginDatabaseDriver { } func escapedParameterValue(_ value: String) -> String { - if Self.isNumericLiteral(value) { + // No numeric column reads as `00123` or `-007`, so that text is a code or a zip and stays a string. + if Self.isNumericLiteral(value), !opensWithPaddingZero(value) { return value } return PluginSQLQuoting.stringLiteral(value, escapes: PluginSQLQuoting.Escapes(features: literalFeatures)) @@ -1611,3 +1612,10 @@ extension PluginDatabaseDriver { return backslash ? [.backslashEscapesInSingleQuotes, .backslashEscapesInDoubleQuotes] : [] } } + +// `007`, `-0042`: an integer part that opens with 0 and goes on with another digit. +private func opensWithPaddingZero(_ value: String) -> Bool { + let unsigned = value.unicodeScalars.drop { $0 == "-" || $0 == "+" } + guard unsigned.first == "0", let next = unsigned.dropFirst().first else { return false } + return ("0"..."9").contains(next) +} diff --git a/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift b/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift index 726b45de69..a89463cd38 100644 --- a/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift +++ b/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift @@ -109,6 +109,72 @@ public enum PluginExportUtilities { } } +// Shared so XLSX number cells and the CSV decimal comma read a column's declared type the same way. +public enum PluginColumnNumbers: Equatable, Sendable { + case text + case exact + case approximate + case untyped + + public init(typeName: String, bitsAreStrings: Bool = false) { + let base = Self.baseName(typeName) + if base.isEmpty { + self = .untyped + } else if Self.bitTypes.contains(base) { + self = bitsAreStrings ? .text : .exact + } else if Self.approximateTypes.contains(base) { + self = .approximate + } else if Self.exactTypes.contains(base) { + self = .exact + } else if Self.textTypes.contains(base) || Self.textMarkers.contains(where: { base.contains($0) }) { + self = .text + } else { + self = .untyped + } + } + + public var isNumeric: Bool { + self == .exact || self == .approximate + } + + // `Nullable(UInt64)` and `LowCardinality(Nullable(Int32))` name the type inside. The base is the + // first word without its width: `int4`, `Int64` and `INT UNSIGNED` are all `int`. + private static func baseName(_ typeName: String) -> String { + var name = typeName.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() + while name.hasSuffix(")"), let open = name.firstIndex(of: "("), + ["nullable", "lowcardinality"].contains(String(name[.. = [ + "int", "uint", "integer", "bigint", "smallint", "tinyint", "mediumint", "hugeint", + "uinteger", "ubigint", "usmallint", "utinyint", "uhugeint", "decimal", "dec", "numeric", + "number", "bignumeric", "serial", "bigserial", "smallserial", "money", "smallmoney", "year", "oid" + ] + + private static let approximateTypes: Set = [ + "float", "double", "real", "binary_float", "binary_double" + ] + + private static let textTypes: Set = ["enum", "set", "json", "jsonb", "name"] + + private static let bitTypes: Set = ["bit", "varbit"] + + // PostgreSQL's `bit(n)` is a string of 0 and 1. SQL Server's is a flag and MySQL's reads as a number. + public static func bitsAreStrings(databaseTypeId: String) -> Bool { + enginesWithBitStrings.contains(databaseTypeId) + } + + private static let enginesWithBitStrings: Set = [ + "PostgreSQL", "Redshift", "CockroachDB", "PGlite", "AlloyDB", "Citus", "Greenplum", "KingbaseES" + ] + + private static let textMarkers = ["char", "text", "string", "clob"] +} + public extension String { func toUTF8Data() throws -> Data { guard let data = self.data(using: .utf8) else { diff --git a/apple/Plugins/TableProPluginKit/PluginRowWriters.swift b/apple/Plugins/TableProPluginKit/PluginRowWriters.swift index e92617afb2..d3c0be5caa 100644 --- a/apple/Plugins/TableProPluginKit/PluginRowWriters.swift +++ b/apple/Plugins/TableProPluginKit/PluginRowWriters.swift @@ -18,9 +18,7 @@ public struct PluginCsvWriteOptions: Sendable, Equatable { public let lineEnding: String public let nullAsEmpty: Bool - /// Prefixes a value opening with `=`, `+`, `-` or `@` with a quote. A spreadsheet treats a cell - /// starting with one of those as a formula, and the values in an export come from the database - /// rather than from the person opening the file. + // The quote stays in the value for any reader but a spreadsheet, so it suits files meant for one. public let sanitizesFormulas: Bool /// Replaces a line break inside a value with a space. Off means the value keeps its break and is @@ -73,9 +71,7 @@ public enum PluginRowWriters { .replacingOccurrences(of: "\r", with: " ", options: .literal) } var wasNeutralised = false - // Scalars, not Characters: `\r\n` is one Character, so a Character test misses a Windows line break. - if options.sanitizesFormulas, let first = processed.unicodeScalars.first, - isFormulaLead(Character(first)), !isPlainNumber(processed) { + if options.sanitizesFormulas, isFormulaLike(processed) { processed = "'" + processed wasNeutralised = true } @@ -95,12 +91,19 @@ public enum PluginRowWriters { private static let quotedScalars: Set = ["\"", "\n", "\r", "\t"] - /// A leading tab or carriage return is treated as a formula lead too: Excel strips it before - /// parsing the cell, so `\t=1+1` reaches the formula engine exactly as `=1+1` would. - private static func isFormulaLead(_ character: Character) -> Bool { - formulaPrefixes.contains(character) || character == "\t" || character == "\r" + // Public so a format plugin can count the values sanitizing changes. + public static func isFormulaLike(_ value: String) -> Bool { + // Scalars, not Characters: `\r\n` is one Character, so a Character test misses a Windows line break. + guard let first = value.unicodeScalars.first, formulaLeads.contains(first) else { return false } + return !isPlainNumber(value) } + // OWASP's list: the four operators, the whitespace Excel strips before parsing (so `\t=1+1` runs as `=1+1`), + // and the full-width operators a Japanese locale reads as formulas. The same set as the shared core's `FORMULA_LEADS`. + private static let formulaLeads: Set = [ + "=", "+", "-", "@", "\t", "\r", "\n", "\u{FF1D}", "\u{FF0B}", "\u{FF0D}", "\u{FF20}" + ] + // A number is never a formula, and `'-5` is text to every reader, a spreadsheet included. // The same rule as the Linux app's `is_plain_number`. private static func isPlainNumber(_ value: String) -> Bool { diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift b/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift index ef3166e4c1..ea42a44623 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift @@ -4,6 +4,8 @@ import TableProTeradataCore final class TeradataAsyncConnection: @unchecked Sendable { private let connection: TeradataConnection private let queue = DispatchQueue(label: "com.TablePro.teradata.connection") + private let runningLock = NSLock() + private var running: TeradataAsyncCancellation? init(config: TeradataConnectionConfig) { connection = TeradataConnection(config: config) @@ -41,6 +43,13 @@ final class TeradataAsyncConnection: @unchecked Sendable { return try await withTaskCancellationHandler { try await withCheckedThrowingContinuation { continuation in queue.async { + // Begun before the call is marked running, so a Stop either side of the mark reaches it. + self.connection.beginOperation() + self.runningLock.withLock { self.running = cancellation } + defer { + self.runningLock.withLock { self.running = nil } + self.connection.endOperation() + } do { try cancellation.throwIfCancelled() let value = try body(self.connection) @@ -53,9 +62,12 @@ final class TeradataAsyncConnection: @unchecked Sendable { } } onCancel: { cancellation.cancel() - // TeradataConnection snapshots its transport under a lock; the transport's cancel - // path uses shutdown rather than disconnecting concurrently with protocol cleanup. - connection.cancel() + // A call still queued never starts; stopping the connection would stop the request of the one running. + // Held across the stop, so the call cannot end and hand the session to the next one meanwhile. + runningLock.withLock { + guard running === cancellation else { return } + connection.cancel() + } } } } diff --git a/apple/Plugins/XLSXExportPlugin/XLSXExportPlugin.swift b/apple/Plugins/XLSXExportPlugin/XLSXExportPlugin.swift index 63a467ef35..f773973f4a 100644 --- a/apple/Plugins/XLSXExportPlugin/XLSXExportPlugin.swift +++ b/apple/Plugins/XLSXExportPlugin/XLSXExportPlugin.swift @@ -43,6 +43,8 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug destination: URL, progress: PluginExportProgress ) async throws -> ExportFormatResult { + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-export. + let options = settings let writer = XLSXWriter(databaseTypeId: dataSource.databaseTypeId) var didSplitSheets = false @@ -56,7 +58,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug var currentSheetRowCount = 0 var columns: [String] = [] var columnTypeNames: [String] = [] - let headerRowCount = settings.includeHeaderRow ? 1 : 0 + let headerRowCount = options.includeHeaderRow ? 1 : 0 let stream = dataSource.streamRows(for: table) for try await element in stream { @@ -70,8 +72,8 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug name: table.name, columns: columns, columnTypeNames: columnTypeNames, - includeHeader: settings.includeHeaderRow, - convertNullToEmpty: settings.convertNullToEmpty + includeHeader: options.includeHeaderRow, + convertNullToEmpty: options.convertNullToEmpty ) currentSheetRowCount = headerRowCount isFirstBatch = false @@ -83,7 +85,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let remaining = Self.maxRowsPerSheet - currentSheetRowCount if rowBatch.count <= remaining { try autoreleasepool { - try writer.addRows(rowBatch, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(rowBatch, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += rowBatch.count } else { @@ -91,7 +93,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let overflow = Array(rowBatch.dropFirst(remaining)) if !fitting.isEmpty { try autoreleasepool { - try writer.addRows(fitting, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(fitting, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += fitting.count } @@ -99,14 +101,14 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug baseName: table.name, columns: columns, columnTypeNames: columnTypeNames, - includeHeader: settings.includeHeaderRow, - convertNullToEmpty: settings.convertNullToEmpty + includeHeader: options.includeHeaderRow, + convertNullToEmpty: options.convertNullToEmpty ) didSplitSheets = true currentSheetRowCount = headerRowCount if !overflow.isEmpty { try autoreleasepool { - try writer.addRows(overflow, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(overflow, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += overflow.count } @@ -124,7 +126,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let remaining = Self.maxRowsPerSheet - currentSheetRowCount if rowBatch.count <= remaining { try autoreleasepool { - try writer.addRows(rowBatch, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(rowBatch, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += rowBatch.count } else { @@ -132,21 +134,21 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let overflow = Array(rowBatch.dropFirst(remaining)) if !fitting.isEmpty { try autoreleasepool { - try writer.addRows(fitting, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(fitting, convertNullToEmpty: options.convertNullToEmpty) } } try writer.continueSheet( baseName: table.name, columns: columns, columnTypeNames: columnTypeNames, - includeHeader: settings.includeHeaderRow, - convertNullToEmpty: settings.convertNullToEmpty + includeHeader: options.includeHeaderRow, + convertNullToEmpty: options.convertNullToEmpty ) didSplitSheets = true currentSheetRowCount = headerRowCount if !overflow.isEmpty { try autoreleasepool { - try writer.addRows(overflow, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(overflow, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += overflow.count } @@ -163,7 +165,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug name: table.name, columns: [], includeHeader: false, - convertNullToEmpty: settings.convertNullToEmpty + convertNullToEmpty: options.convertNullToEmpty ) try writer.finishSheet() } diff --git a/apple/Plugins/XLSXExportPlugin/XLSXWriter.swift b/apple/Plugins/XLSXExportPlugin/XLSXWriter.swift index c404199eac..cdb19eb612 100644 --- a/apple/Plugins/XLSXExportPlugin/XLSXWriter.swift +++ b/apple/Plugins/XLSXExportPlugin/XLSXWriter.swift @@ -46,66 +46,7 @@ final class XLSXWriter { case empty } - enum ColumnNumbers: Equatable { - case text - case exact - case approximate - case untyped - - init(typeName: String, bitsAreStrings: Bool = false) { - let base = Self.baseName(typeName) - if base.isEmpty { - self = .untyped - } else if Self.bitTypes.contains(base) { - self = bitsAreStrings ? .text : .exact - } else if Self.approximateTypes.contains(base) { - self = .approximate - } else if Self.exactTypes.contains(base) { - self = .exact - } else if Self.textTypes.contains(base) || Self.textMarkers.contains(where: { base.contains($0) }) { - self = .text - } else { - self = .untyped - } - } - - // `Nullable(UInt64)` and `LowCardinality(Nullable(Int32))` name the type inside. The base is the - // first word without its width: `int4`, `Int64` and `INT UNSIGNED` are all `int`. - private static func baseName(_ typeName: String) -> String { - var name = typeName.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() - while name.hasSuffix(")"), let open = name.firstIndex(of: "("), - ["nullable", "lowcardinality"].contains(String(name[.. = [ - "int", "uint", "integer", "bigint", "smallint", "tinyint", "mediumint", "hugeint", - "uinteger", "ubigint", "usmallint", "utinyint", "uhugeint", "decimal", "dec", "numeric", - "number", "bignumeric", "serial", "bigserial", "smallserial", "money", "smallmoney", "year", "oid" - ] - - private static let approximateTypes: Set = [ - "float", "double", "real", "binary_float", "binary_double" - ] - - private static let textTypes: Set = ["enum", "set", "json", "jsonb", "name"] - - private static let bitTypes: Set = ["bit", "varbit"] - - // PostgreSQL's `bit(n)` is a string of 0 and 1. SQL Server's is a flag and MySQL's reads as a number. - static func bitsAreStrings(databaseTypeId: String) -> Bool { - enginesWithBitStrings.contains(databaseTypeId) - } - - private static let enginesWithBitStrings: Set = [ - "PostgreSQL", "Redshift", "CockroachDB", "PGlite", "AlloyDB", "Citus", "Greenplum", "KingbaseES" - ] - - private static let textMarkers = ["char", "text", "string", "clob"] - } + typealias ColumnNumbers = PluginColumnNumbers // Excel keeps 15 significant digits of a number. private static let maximumExactDigits = 15 @@ -225,6 +166,8 @@ final class XLSXWriter { let isPlain = literal == text && !literal.contains("e") let digitCount = literal.filter { $0.isNumber }.count return isPlain && digitCount <= maximumExactDigits ? .number(literal) : .string(text) + @unknown default: + return .string(text) } } diff --git a/apple/Plugins/XLSXImportPlugin/XLSXImportPlugin.swift b/apple/Plugins/XLSXImportPlugin/XLSXImportPlugin.swift index 6f7592ab3a..d74bafbfba 100644 --- a/apple/Plugins/XLSXImportPlugin/XLSXImportPlugin.swift +++ b/apple/Plugins/XLSXImportPlugin/XLSXImportPlugin.swift @@ -39,13 +39,13 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug var fieldDetectionSignature: String { settings.detectionSignature } - var rowImportConfiguration: RowImportRunner.Configuration? { runConfiguration } + var rowImportConfiguration: RowImportRunner.Configuration? { Self.runConfiguration(settings) } - private var runConfiguration: RowImportRunner.Configuration { + private static func runConfiguration(_ options: XLSXImportOptions) -> RowImportRunner.Configuration { RowImportRunner.Configuration( - errorHandling: settings.errorHandling, - wrapInTransaction: settings.wrapInTransaction, - deleteExistingRows: settings.deleteExistingRows + errorHandling: options.errorHandling, + wrapInTransaction: options.wrapInTransaction, + deleteExistingRows: options.deleteExistingRows ) } @@ -78,11 +78,13 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug progress: PluginImportProgress ) async throws -> PluginImportResult { let started = Date() + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-import. + let options = settings let sheet: XLSXImportSheet do { sheet = try XLSXImportSheet.read( at: source.fileURL(), - hasHeaderRow: settings.hasHeaderRow, + hasHeaderRow: options.hasHeaderRow, isCancelled: { progress.isCancelled } ) } catch is TabularCancellation { @@ -96,7 +98,7 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug progress.setEstimatedTotal(max(1, sheet.rowCount)) var cursor = 0 let outcome = try await RowImportRunner.run( - configuration: runConfiguration, + configuration: Self.runConfiguration(options), sink: sink, progress: progress ) { @@ -108,7 +110,7 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug if filled.count < rows.count { progress.incrementStatement(by: rows.count - filled.count) } - return filled.map { (line: $0.line, row: self.row($0.values, header: header)) } + return filled.map { (line: $0.line, row: Self.row($0.values, header: header, options: options)) } } return PluginImportResult( @@ -121,7 +123,7 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug // MARK: - Private - private func row(_ values: [String?], header: [String]) -> [String: PluginCellValue] { + private static func row(_ values: [String?], header: [String], options: XLSXImportOptions) -> [String: PluginCellValue] { var mapped: [String: PluginCellValue] = [:] for (index, column) in header.enumerated() { let raw = values[safe: index] ?? nil @@ -129,10 +131,10 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug mapped[column] = .null continue } - let trimmed = settings.trimWhitespace + let trimmed = options.trimWhitespace ? raw.trimmingCharacters(in: .whitespacesAndNewlines) : raw - if settings.emptyAsNull, trimmed.isEmpty { + if options.emptyAsNull, trimmed.isEmpty { mapped[column] = .null } else { mapped[column] = .text(trimmed) diff --git a/apple/TablePro/Core/ChangeTracking/DataChangeModels.swift b/apple/TablePro/Core/ChangeTracking/DataChangeModels.swift index 7a795ff87d..f86e46a25c 100644 --- a/apple/TablePro/Core/ChangeTracking/DataChangeModels.swift +++ b/apple/TablePro/Core/ChangeTracking/DataChangeModels.swift @@ -49,8 +49,8 @@ struct CellChange: Identifiable, Equatable { let oldIsAbsent: Bool let newIsAbsent: Bool let oldIsExpression: Bool - /// The new value is `.text` holding SQL from `CellExpressionVocabulary`, written unquoted. Never set with - /// `newIsAbsent`. + /// The new value is `.text` holding SQL from `CellExpressionVocabulary`, or `PluginCellValue.columnDefault`, + /// written unquoted. Never set with `newIsAbsent`. let newIsExpression: Bool init( @@ -92,8 +92,8 @@ struct RowChange: Identifiable, Equatable { /// stands for an insert. Empty on every engine that cannot tell a missing field from NULL. var absentColumns: Set - /// An insert's cells that hold a SQL expression, as the row stands. An update keeps its own on each - /// `CellChange`. + /// An insert's cells that hold a SQL expression or the column default, as the row stands. An update keeps its own + /// on each `CellChange`. var expressionColumns: Set /// The order the user made this change in. diff --git a/apple/TablePro/Core/ChangeTracking/DefaultedCells.swift b/apple/TablePro/Core/ChangeTracking/DefaultedCells.swift new file mode 100644 index 0000000000..bdbb48fc6b --- /dev/null +++ b/apple/TablePro/Core/ChangeTracking/DefaultedCells.swift @@ -0,0 +1,38 @@ +// +// DefaultedCells.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// The cells a pending insert or edit set to the column's default, by row and data index. Only those rows can hold one. +enum DefaultedCells { + static func marked( + in rows: TableRows, + rowIDs: Set, + expressionColumns: (RowID) -> Set + ) -> [RowID: Set] { + var cells: [RowID: Set] = [:] + for rowID in rowIDs { + guard let row = rows.row(withID: rowID) else { continue } + let defaulted = expressionColumns(rowID).filter { + row.values.indices.contains($0) && row.values[$0].isColumnDefault(isExpression: true) + } + if !defaulted.isEmpty { cells[rowID] = defaulted } + } + return cells + } +} + +extension ChangeManaging { + func defaultedCells(in rows: TableRows) -> [RowID: Set] { + DefaultedCells.marked(in: rows, rowIDs: insertedRowIDs.union(modifiedCells.keys)) { expressionColumns(forRow: $0) } + } +} + +extension AnyChangeManager { + func defaultedCells(in rows: TableRows) -> [RowID: Set] { + DefaultedCells.marked(in: rows, rowIDs: insertedRowIDs.union(modifiedCells.keys)) { expressionColumns(forRow: $0) } + } +} diff --git a/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift b/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift index 17f1871fa9..21df0ea117 100644 --- a/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift +++ b/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift @@ -257,7 +257,8 @@ struct SQLStatementGenerator { var bindParameters: [Any?] = [] for (index, value) in values.enumerated() { - if case .text(let s) = value, s == "__DEFAULT__" { continue } + // Left out rather than written as DEFAULT, which SQLite does not take in VALUES. + if value.isColumnDefault(isExpression: expressionColumns.contains(index)) { continue } guard index < columns.count else { continue } let columnName = columns[index] @@ -383,7 +384,7 @@ struct SQLStatementGenerator { guard !change.cellChanges.isEmpty else { return nil } let nonDefaultChanges = change.cellChanges.filter { - $0.newValue != .text("__DEFAULT__") && !generatedColumns.contains($0.columnName) + !$0.newValue.isColumnDefault(isExpression: $0.newIsExpression) && !generatedColumns.contains($0.columnName) } guard !nonDefaultChanges.isEmpty else { return nil } @@ -419,8 +420,6 @@ struct SQLStatementGenerator { var parameters: [Any?] = [] let setClauses = writableChanges.map { cellChange -> String in switch cellChange.newValue { - case .text(let s) where s == "__DEFAULT__": - return "\(quoteIdentifierFn(cellChange.columnName)) = DEFAULT" case .text(let expression) where cellChange.newIsExpression: return "\(quoteIdentifierFn(cellChange.columnName)) = \(expression)" default: diff --git a/apple/TablePro/Core/Compare/CompareSQLLiteral.swift b/apple/TablePro/Core/Compare/CompareSQLLiteral.swift index 24cc1a74a8..40fc624f42 100644 --- a/apple/TablePro/Core/Compare/CompareSQLLiteral.swift +++ b/apple/TablePro/Core/Compare/CompareSQLLiteral.swift @@ -77,17 +77,20 @@ internal enum CompareSQLLiteral { return SQLStringLiteralPrefix.forDatabaseType(databaseType) + literal } + // Shared by Compare & Sync and Copy as SQL. `SQLExportBinaryLiteral` in the SQL export plugin holds the + // measurements behind each spelling; the plugin cannot link app code, so it keeps its own copy. internal static func binaryLiteral(for data: Data, databaseType: DatabaseType) -> String? { - let hex = data.map { String(format: "%02X", $0) }.joined() + let hex = data.uppercaseHex() switch binaryStyle(for: databaseType) { case .bitString: return "X'\(hex)'" case .postgresBytea: - return "'\\x\(hex.lowercased())'::bytea" + return postgresFamilyBinaryLiteral(hex: hex, databaseType: databaseType) case .zeroX: return "0x\(hex)" case .hexToRaw: - return "HEXTORAW('\(hex)')" + // `HEXTORAW('')` stores NULL, which a `BLOB NOT NULL` refuses. + return data.isEmpty ? "EMPTY_BLOB()" : "HEXTORAW('\(hex)')" case .unhexFunction: return "unhex('\(hex)')" case .unknown: @@ -95,6 +98,19 @@ internal enum CompareSQLLiteral { } } + // `'\x…'::bytea` follows `standard_conforming_strings`: with it off the server stores the text's bytes. KingbaseES + // documents `decode` only as Oracle's DECODE, so it keeps the bytea literal; Redshift has no bytea and reads VARBYTE. + private static func postgresFamilyBinaryLiteral(hex: String, databaseType: DatabaseType) -> String { + switch databaseType { + case .kingbasees: + return "'\\x\(hex)'::bytea" + case .redshift: + return "from_hex('\(hex)')" + default: + return "decode('\(hex)', 'hex')" + } + } + internal enum BinaryStyle { case bitString case postgresBytea diff --git a/apple/TablePro/Core/Compare/DataDiffEngine.swift b/apple/TablePro/Core/Compare/DataDiffEngine.swift index 882c746b62..df96ff5b92 100644 --- a/apple/TablePro/Core/Compare/DataDiffEngine.swift +++ b/apple/TablePro/Core/Compare/DataDiffEngine.swift @@ -30,10 +30,16 @@ internal struct DataRow: Hashable, Sendable { internal protocol DataRowProviding: AnyObject { var endedAtRowLimit: Bool { get } + // The limit the driver stopped this side's stream at, once it has ended there. + var driverRowCut: Int? { get } func nextRow() async throws -> DataRow? func drain() async throws } +internal extension DataRowProviding { + var driverRowCut: Int? { nil } +} + internal protocol OneSidedRowResolving: AnyObject { func rows(matching keys: [[PluginCellValue]], on side: ComparisonSide) async throws -> [DataRow] } @@ -111,6 +117,10 @@ internal struct DataDiffSummary: Hashable, Sendable { /// as `id > ` resumes from without skipping a key or comparing one twice. internal let resumeKey: [PluginCellValue]? + // The limit a driver stopped a side's stream at, so the answer covers only the rows read before it. + internal let sourceRowCut: Int? + internal let targetRowCut: Int? + /// One comparison's own identity. Two runs that produce the same counts and the same digest are /// still two answers, and a view that caches on the answer has to be able to tell them apart: /// matching rows are counted rather than digested, so both can move while the digest stands. @@ -130,6 +140,8 @@ internal struct DataDiffSummary: Hashable, Sendable { stoppedAtRowLimit: Bool = false, differenceDigest: String = "", resumeKey: [PluginCellValue]? = nil, + sourceRowCut: Int? = nil, + targetRowCut: Int? = nil, runIdentity: UUID = UUID() ) { self.insertCount = insertCount @@ -145,6 +157,8 @@ internal struct DataDiffSummary: Hashable, Sendable { self.stoppedAtRowLimit = stoppedAtRowLimit self.differenceDigest = differenceDigest self.resumeKey = resumeKey + self.sourceRowCut = sourceRowCut + self.targetRowCut = targetRowCut self.runIdentity = runIdentity } @@ -359,7 +373,9 @@ internal struct DataDiffEngine { return accumulator.summary( comparedKeyCount: positions, stoppedAtRowLimit: stoppedAtRowLimit, - resumeKey: stoppedAtRowLimit ? lastClassifiedKey : nil + resumeKey: stoppedAtRowLimit ? lastClassifiedKey : nil, + sourceRowCut: source.driverRowCut, + targetRowCut: target.driverRowCut ) } @@ -536,7 +552,9 @@ internal extension DataDiffEngine { func summary( comparedKeyCount: Int, stoppedAtRowLimit: Bool, - resumeKey: [PluginCellValue]? = nil + resumeKey: [PluginCellValue]? = nil, + sourceRowCut: Int? = nil, + targetRowCut: Int? = nil ) -> DataDiffSummary { DataDiffSummary( insertCount: counts[.insert] ?? 0, @@ -551,7 +569,9 @@ internal extension DataDiffEngine { comparedKeyCount: comparedKeyCount, stoppedAtRowLimit: stoppedAtRowLimit, differenceDigest: digest.finalize().map { String(format: "%02x", $0) }.joined(), - resumeKey: resumeKey + resumeKey: resumeKey, + sourceRowCut: sourceRowCut, + targetRowCut: targetRowCut ) } diff --git a/apple/TablePro/Core/Compare/StreamingRowProvider.swift b/apple/TablePro/Core/Compare/StreamingRowProvider.swift index 979cabe6fb..54a4ac107f 100644 --- a/apple/TablePro/Core/Compare/StreamingRowProvider.swift +++ b/apple/TablePro/Core/Compare/StreamingRowProvider.swift @@ -13,6 +13,7 @@ // import Foundation +import os import TableProPluginKit internal struct RowReadContext: Sendable { @@ -55,6 +56,7 @@ internal final class StreamingRowProvider: DataRowProviding { private var deliveredCount = 0 private var isFinished = false private var sawRowPastTheLimit = false + private let rowCut: OSAllocatedUnfairLock internal init( stream: AsyncThrowingStream, @@ -62,17 +64,25 @@ internal final class StreamingRowProvider: DataRowProviding { rowLimit: Int? = nil, context: RowReadContext? = nil ) { - self.iterator = stream.makeAsyncIterator() + let rowCut = OSAllocatedUnfairLock(initialState: nil) + self.rowCut = rowCut + self.iterator = ExportRowCut.reporting(stream, engineCeiling: nil) { limit in + rowCut.withLock { $0 = limit } + }.makeAsyncIterator() self.columns = columns self.rowLimit = rowLimit self.context = context } - /// The read asks for one row more than the limit and never hands that row out, so this is the - /// server's own answer to whether anything was left rather than a guess from the row count: a - /// table holding exactly the limit is not a truncated read. + // The read asks for one row past the limit, so a table holding exactly the limit is not a cut read. A stream the + // driver stopped at a limit it declares ends here too: a key only the other side holds was never read on this one. internal var endedAtRowLimit: Bool { - sawRowPastTheLimit + sawRowPastTheLimit || driverRowCut != nil + } + + internal var driverRowCut: Int? { + guard isFinished else { return nil } + return rowCut.withLock { $0 } } internal func nextRow() async throws -> DataRow? { diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift index ec3657620b..9cf2f91330 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift @@ -218,7 +218,7 @@ final class RowEditingCoordinator: ObservableObject { private static func sampleText(atColumn index: Int, in tableRows: TableRows) -> String? { for row in tableRows.rows where row.values.indices.contains(index) { - if case .text(let text) = row.values[index], text != "__DEFAULT__" { return text } + if case .text(let text) = row.values[index], row.values[index] != .columnDefault { return text } } return nil } @@ -430,10 +430,12 @@ final class RowEditingCoordinator: ObservableObject { self?.copyRowsAsJson(rowIDs) }) else { return } let model = parent.gridModel(forTab: tab.id) + let tableRows = parent.tabSessionRegistry.tableRows(for: tab.id) let output = ResultJsonSerializer.serialize( - tableRows: parent.tabSessionRegistry.tableRows(for: tab.id), + tableRows: tableRows, displayIDs: model.displayedRowIDs, selectedDisplayIndices: Set(rowIDs.compactMap { model.displayIndex(of: $0) }), + defaultedCells: parent.changeManager.defaultedCells(in: tableRows), columns: VisibleColumnProjection(indices: presentedColumnIndices(tabId: tab.id)) ) guard output.rowCount > 0 else { return } diff --git a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift index a4c255ea3b..3686d2ca50 100644 --- a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift +++ b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift @@ -134,8 +134,9 @@ struct RewindExecutor { ) } + // In the order the restore ran them, since undoing the restore runs its record backwards. static func inverseOperations(of plan: RewindPlan) -> [RowWriteOperation] { - plan.rows.filter { $0.outcome.restores }.map { row -> RowWriteOperation in + plan.rows.filter { $0.outcome.restores }.reversed().map { row -> RowWriteOperation in let source = row.operation let kind = source.kind.inverted // The same stored refusal a save through a view writes, so a build without the view flag refuses too. diff --git a/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift b/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift index 0438224191..361fb6bc40 100644 --- a/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift +++ b/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift @@ -82,6 +82,7 @@ struct RewindPlanner { currentRow = byLocator[Array(locator.utf8)] } else { currentRow = primaryKey(of: operation).flatMap { current[$0] } + ?? rowRestoredUnderKeyBefore(operation, in: current) } let outcome = classify(operation, current: currentRow) rows.append( @@ -135,7 +136,7 @@ struct RewindPlanner { } } - /// Whether the row holds what the image says on every column the save wrote: the same value, + /// Whether the row holds what the image says on every compared column: the same value, /// and the field there or missing as it was. Set NULL on a missing field and Remove Field on a /// NULL one leave the value alone, so comparing values alone reads either as already restored. /// @@ -147,7 +148,7 @@ struct RewindPlanner { absent expectedAbsent: Set?, on operation: RowWriteOperation ) -> Bool { - operation.writtenColumns.allSatisfy { column in + comparedColumns(of: operation).allSatisfy { column in guard let index = operation.columns.firstIndex(of: column), index < current.values.count, index < expected.count, current.values[index] == expected[index] @@ -157,6 +158,14 @@ struct RewindPlanner { } } + // A row found by a key the save wrote holds that key whatever row it is, so a moved row is compared on every + // column the server does not compute; a trigger's stamp then reads as changed, which leaves the row alone. + private func comparedColumns(of operation: RowWriteOperation) -> [String] { + guard keyBeforeMove(of: operation) != nil else { return operation.writtenColumns } + let generated = Set(record.generatedColumns) + return operation.columns.filter { !generated.contains($0) } + } + // MARK: - Reading private func indexByKey(_ rows: [RewindCurrentRow]) -> [RewindRowKey: RewindCurrentRow] { @@ -194,9 +203,11 @@ struct RewindPlanner { guard !keyColumns.isEmpty else { return [] } if keyColumns.count == 1, let keyColumn = keyColumns.first { - let filters = operations.compactMap { operation -> TableFilter? in - guard let value = keyValue(of: operation, column: keyColumn) else { return nil } - return TableFilter(columnName: keyColumn, filterOperator: .equal, value: value) + let filters = operations.flatMap { operation in + readImages(of: operation).compactMap { image -> TableFilter? in + guard let value = keyValue(in: image, of: operation, column: keyColumn) else { return nil } + return TableFilter(columnName: keyColumn, filterOperator: .equal, value: value) + } } guard !filters.isEmpty, let query = queryBuilder.buildFilteredQuery( @@ -211,21 +222,23 @@ struct RewindPlanner { return [query] } - return operations.compactMap { operation in - let filters = keyColumns.compactMap { column -> TableFilter? in - guard let value = keyValue(of: operation, column: column) else { return nil } - return TableFilter(columnName: column, filterOperator: .equal, value: value) + return operations.flatMap { operation in + readImages(of: operation).compactMap { image -> String? in + let filters = keyColumns.compactMap { column -> TableFilter? in + guard let value = keyValue(in: image, of: operation, column: column) else { return nil } + return TableFilter(columnName: column, filterOperator: .equal, value: value) + } + guard filters.count == keyColumns.count else { return nil } + return queryBuilder.buildFilteredQuery( + tableName: record.target.table, + schemaName: record.target.schema, + filters: filters, + logicMode: .and, + columns: columns, + selectColumns: columns, + limit: 1 + ) } - guard filters.count == keyColumns.count else { return nil } - return queryBuilder.buildFilteredQuery( - tableName: record.target.table, - schemaName: record.target.schema, - filters: filters, - logicMode: .and, - columns: columns, - selectColumns: columns, - limit: 1 - ) } } @@ -293,12 +306,40 @@ struct RewindPlanner { // MARK: - Keys + // Where the save left the row, or for a delete where it was: an edit that rewrote the key moved it. + private func savedImage(of operation: RowWriteOperation) -> [PluginCellValue]? { + operation.postImage ?? operation.preImage + } + private func primaryKey(of operation: RowWriteOperation) -> RewindRowKey? { - let source = operation.preImage ?? operation.postImage - guard let source else { return nil } + guard let source = savedImage(of: operation) else { return nil } return primaryKey(of: source, columns: operation.columns, keyColumns: operation.primaryKeyColumns) } + private func keyBeforeMove(of operation: RowWriteOperation) -> RewindRowKey? { + guard operation.kind == .update, let preImage = operation.preImage else { return nil } + let before = primaryKey(of: preImage, columns: operation.columns, keyColumns: operation.primaryKeyColumns) + return before == primaryKey(of: operation) ? nil : before + } + + // A rewind that already ran put a moved row back under its old key, where it reads as restored. + private func readImages(of operation: RowWriteOperation) -> [[PluginCellValue]] { + guard let saved = savedImage(of: operation) else { return [] } + guard keyBeforeMove(of: operation) != nil, let preImage = operation.preImage else { return [saved] } + return [saved, preImage] + } + + // Only a row holding the old values, so another row that took the old key since is not mistaken for this one. + private func rowRestoredUnderKeyBefore( + _ operation: RowWriteOperation, + in current: [RewindRowKey: RewindCurrentRow] + ) -> RewindCurrentRow? { + guard let key = keyBeforeMove(of: operation), let row = current[key], let preImage = operation.preImage, + matches(row, preImage, absent: operation.preImageAbsentColumns, on: operation) + else { return nil } + return row + } + private func primaryKey( of row: [PluginCellValue], columns: [String], @@ -319,16 +360,17 @@ struct RewindPlanner { /// compare against, and a delete would be reported as restorable with no conflict check at all. private func keyIsComparable(_ operation: RowWriteOperation) -> Bool { !operation.primaryKeyColumns.isEmpty - && operation.primaryKeyColumns.allSatisfy { keyValue(of: operation, column: $0) != nil } + && operation.primaryKeyColumns.allSatisfy { + keyValue(in: savedImage(of: operation), of: operation, column: $0) != nil + } } - private func keyValue(of operation: RowWriteOperation, column: String) -> String? { - let source = operation.preImage ?? operation.postImage - guard let source, + private func keyValue(in image: [PluginCellValue]?, of operation: RowWriteOperation, column: String) -> String? { + guard let image, let index = operation.columns.firstIndex(of: column), - index < source.count + index < image.count else { return nil } - return source[index].asText + return image[index].asText } } diff --git a/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift b/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift index 968b67c8df..19517845a3 100644 --- a/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift +++ b/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift @@ -146,7 +146,7 @@ struct RowChangeStatementFactory { try refuseUnwritableExpressions(in: changes, insertedRowData: insertedRowData) try refuseMisdimensionedVectors(in: changes, insertedRowData: insertedRowData) let insertedRowData = insertedRowData.mapValues(leavingServerOwnedColumnsToTheServer) - let changes = changes.map(droppingServerOwnedExpressions) + let changes = changes.map { markingServerOwnedDefaults($0, in: insertedRowData) } if let driverStatements = try pluginRowWrites( for: changes, insertedRowData: insertedRowData, @@ -190,7 +190,9 @@ struct RowChangeStatementFactory { private func refuseDefaultOnUpdate(in changes: [RowChange]) throws { guard !CellExpressionVocabulary.updateTakesDefault(databaseType) else { return } for change in changes where change.type == .update { - guard let defaulted = change.cellChanges.first(where: { $0.newValue.isDefaultMarker }) else { continue } + guard let defaulted = change.cellChanges.first(where: { + $0.newValue.isColumnDefault(isExpression: $0.newIsExpression) + }) else { continue } throw DataWriteError.changeRefused( table: tableName, kind: .update, @@ -209,9 +211,10 @@ struct RowChangeStatementFactory { in changes: [RowChange], insertedRowData: [RowID: [PluginCellValue]] ) throws { + // A default reaches a driver in the spelling it reads, so only SQL proper has to be SQL this engine runs. let marked = changes.flatMap { change in Self.markedCells(of: change, insertedRowData: insertedRowData, columns: columns) - } + }.filter { $0.value != .columnDefault } guard !marked.isEmpty else { return } for cell in marked { guard case .text(let sql) = cell.value, CellExpressionVocabulary.isCanonical(sql, for: databaseType) @@ -309,21 +312,58 @@ struct RowChangeStatementFactory { guard !generatedColumns.isEmpty else { return values } return values.enumerated().map { index, value in guard columns.indices.contains(index), generatedColumns.contains(columns[index]) else { return value } - return Self.defaultMarker + return .columnDefault } } - /// The cell now holds the default marker, so it is no longer SQL to write. - private func droppingServerOwnedExpressions(_ change: RowChange) -> RowChange { - guard change.type == .insert, !change.expressionColumns.isEmpty, !generatedColumns.isEmpty else { return change } - var kept = change - kept.expressionColumns = change.expressionColumns.filter { - !(columns.indices.contains($0) && generatedColumns.contains(columns[$0])) + // Whatever SQL the cell held, it now holds the default, which only the mark makes one. + private func markingServerOwnedDefaults(_ change: RowChange, in insertedRowData: [RowID: [PluginCellValue]]) -> RowChange { + guard change.type == .insert, !generatedColumns.isEmpty else { return change } + let owned = Set(columns.indices.filter { generatedColumns.contains(columns[$0]) }) + var marked = change + marked.expressionColumns.subtract(owned) + if let values = insertedRowData[change.rowID] { + marked.expressionColumns.formUnion(owned.filter { values.indices.contains($0) }) } - return kept + return marked } - private static let defaultMarker = PluginCellValue.text("__DEFAULT__") + // A driver reads its own spelling for a default, not the mark, so a default reaches it spelled that way and unmarked. + private static func spellingDefaultsForDrivers( + _ changes: [RowChange], + insertedRowData: [RowID: [PluginCellValue]] + ) -> (changes: [RowChange], insertedRowData: [RowID: [PluginCellValue]]) { + var rows = insertedRowData + let spelledChanges = changes.map { change -> RowChange in + var spelled = change + if change.type == .insert, var values = rows[change.rowID] { + for index in change.expressionColumns where values.indices.contains(index) && values[index] == .columnDefault { + values[index] = .pluginDefaultMarker + spelled.expressionColumns.remove(index) + } + rows[change.rowID] = values + } + let defaulted = change.cellChanges.filter { $0.newValue.isColumnDefault(isExpression: $0.newIsExpression) } + guard !defaulted.isEmpty else { return spelled } + spelled.cellChanges = change.cellChanges.map { cell in + guard cell.newValue.isColumnDefault(isExpression: cell.newIsExpression) else { return cell } + return CellChange( + columnIndex: cell.columnIndex, + columnName: cell.columnName, + oldValue: cell.oldValue, + newValue: .pluginDefaultMarker, + oldIsAbsent: cell.oldIsAbsent, + newIsAbsent: cell.newIsAbsent, + oldIsExpression: cell.oldIsExpression + ) + } + if change.type == .insert, rows[change.rowID] == nil { + spelled.expressionColumns.subtract(defaulted.map(\.columnIndex)) + } + return spelled + } + return (spelledChanges, rows) + } private func rowWriteContext(for keyed: PluginKeyedChanges) -> PluginRowWriteContext { var context = PluginRowWriteContext() @@ -386,6 +426,7 @@ struct RowChangeStatementFactory { }, context: restoreContext(rowCount: rows.count) ) { + try refuseRestoredTextDriversReadAsDefault(rows, restorable: restorable) try refuseUnfilteredWrites(restored.map { $0.statement }) return RestoreStatements(statements: restored.map { ParameterizedStatement(sql: $0.statement, parameters: $0.parameters.map(\.asAny)) @@ -489,9 +530,10 @@ struct RowChangeStatementFactory { insertedRowIDs: Set ) throws -> [DriverWrittenStatement]? { guard let pluginDriver else { return nil } + let spelled = Self.spellingDefaultsForDrivers(changes, insertedRowData: insertedRowData) let keyed = PluginKeyedChanges( - changes: changes, - insertedRowData: insertedRowData, + changes: spelled.changes, + insertedRowData: spelled.insertedRowData, deletedRowIDs: deletedRowIDs, insertedRowIDs: insertedRowIDs ) @@ -524,6 +566,7 @@ struct RowChangeStatementFactory { throw DataWriteError.changeRefused(table: tableName, kind: nil, reason: error.localizedDescription) } + try refuseTextDriversReadAsDefault(in: changes, insertedRowData: insertedRowData) try refuseUnfilteredWrites(writes.map(\.statement)) let unwritten = RowWriteCoverage.unwrittenChanges( changes, @@ -547,6 +590,46 @@ struct RowChangeStatementFactory { } } + // A driver may read the text `__DEFAULT__` as the column default, and none can say whether it does until PluginKit + // carries the mark beside the value, so the text is never handed to one as a value. + private func refuseTextDriversReadAsDefault( + in changes: [RowChange], + insertedRowData: [RowID: [PluginCellValue]] + ) throws { + for change in changes where change.type != .delete { + var written = change.cellChanges.filter { !$0.newIsExpression }.map { (column: $0.columnName, value: $0.newValue) } + if change.type == .insert, let stored = insertedRowData[change.rowID] { + written = zip(columns, stored).enumerated() + .filter { !change.expressionColumns.contains($0.offset) } + .map { (column: $0.element.0, value: $0.element.1) } + } + guard let literal = written.first(where: { $0.value == .pluginDefaultMarker }) else { continue } + throw textReadAsDefault(in: literal.column, kind: change.type == .insert ? .insert : .update) + } + } + + // A rewind puts a deleted row back with the values it held, which can be that text. + private func refuseRestoredTextDriversReadAsDefault(_ rows: [[PluginCellValue]], restorable: [Int]) throws { + for row in rows { + guard let index = restorable.first(where: { row.indices.contains($0) && row[$0] == .pluginDefaultMarker }) + else { continue } + throw textReadAsDefault(in: columns[index], kind: .insert) + } + } + + private func textReadAsDefault(in column: String, kind: RowWriteKind) -> DataWriteError { + .changeRefused( + table: tableName, + kind: kind, + reason: String( + format: String( + localized: "This database's driver may read the text __DEFAULT__ in %@ as the column default, so it cannot be saved as text." + ), + column + ) + ) + } + // The host's own generator never writes one, so only a driver's statements are read. private func refuseUnfilteredWrites(_ statements: [String]) throws { guard PluginManager.shared.editorLanguage(for: databaseType) == .sql else { return } diff --git a/apple/TablePro/Core/DataWrite/RowWriteOperationBuilder.swift b/apple/TablePro/Core/DataWrite/RowWriteOperationBuilder.swift index 457424223b..11cafebc8f 100644 --- a/apple/TablePro/Core/DataWrite/RowWriteOperationBuilder.swift +++ b/apple/TablePro/Core/DataWrite/RowWriteOperationBuilder.swift @@ -34,7 +34,8 @@ enum RowWriteOperationBuilder { writesThroughView: Bool = false, rowLocators: [RowID: String] = [:] ) -> [RowWriteOperation] { - changes.compactMap { change in + // A rewind undoes them newest first, and `changes` stops being in edit order once a change is cancelled. + changes.sorted { $0.sequence < $1.sequence }.compactMap { change in switch change.type { case .update: var operation = update( @@ -111,7 +112,7 @@ enum RowWriteOperationBuilder { ) -> RewindRefusal? { if containsTableOperation { return .destructiveTableOperation } if primaryKeyColumns.isEmpty || preImage == nil { return .noPrimaryKey } - if change.cellChanges.contains(where: { $0.newIsExpression || $0.newValue.isDefaultMarker }) { + if change.cellChanges.contains(where: \.newIsExpression) { return .serverComputedValue } if change.cellChanges.contains(where: { generatedColumns.contains($0.columnName) }) { @@ -172,11 +173,11 @@ enum RowWriteOperationBuilder { return .serverAssignedKey } let value = values[index] - if value.isNull || value.isDefaultMarker || change.expressionColumns.contains(index) { + if value.isNull || change.expressionColumns.contains(index) { return .serverAssignedKey } } - if values.contains(where: \.isDefaultMarker) || !change.expressionColumns.isEmpty { + if !change.expressionColumns.isEmpty { return .serverComputedValue } if values.contains(where: exceedsCapture) { return .valueTooLarge } diff --git a/apple/TablePro/Core/Database/DatabaseManager+ContainerDDL.swift b/apple/TablePro/Core/Database/DatabaseManager+ContainerDDL.swift index 147c37dfaf..f4550da094 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+ContainerDDL.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+ContainerDDL.swift @@ -59,10 +59,7 @@ extension DatabaseManager { } } - /// Runs a container operation the driver performs itself, with no statement to show. - /// - /// `kind` is the caller's to state because there is no text to classify: a drop is - /// `.destructiveQuery` and everything else is `.schemaMutation`. + // No statement to classify, so the caller states `kind`: a drop is destructive, anything else a schema mutation. func runContainerOperation( description: String, kind: OperationKind, @@ -70,6 +67,7 @@ extension DatabaseManager { databaseType: DatabaseType, event: CatalogEvent?, isConfirmationPreCleared: Bool = false, + gate: any ExecutionGate = ExecutionGateProvider.shared, perform: @Sendable @escaping (any DatabaseDriver) async throws -> Void ) async throws { try await authorizeContainerOperation( @@ -78,7 +76,8 @@ extension DatabaseManager { connectionId: scope.connectionId, databaseType: databaseType, description: description, - isConfirmationPreCleared: isConfirmationPreCleared + isConfirmationPreCleared: isConfirmationPreCleared, + gate: gate ) try await withScopedDriver( scope: scope, @@ -90,16 +89,14 @@ extension DatabaseManager { if let event { CatalogChangeService.post(event) } } - /// Runs a container operation the driver expressed as statements. - /// - /// The kind comes from the statements rather than from the action's name, so an edit that - /// revokes a privilege is authorized at the destructive tier the same way a drop is. + // The kind comes from the statements, so an edit that revokes a privilege is authorized like a drop. func runContainerStatements( _ plan: ContainerDDLPlan, description: String, scope: DatabaseScope, databaseType: DatabaseType, - event: CatalogEvent? + event: CatalogEvent?, + gate: any ExecutionGate = ExecutionGateProvider.shared ) async throws { let statements = plan.statements guard !statements.isEmpty else { return } @@ -113,7 +110,8 @@ extension DatabaseManager { connectionId: scope.connectionId, databaseType: databaseType, description: description, - isConfirmationPreCleared: false + isConfirmationPreCleared: false, + gate: gate ) let route = schemaChangeRoute(for: scope) @@ -139,21 +137,19 @@ extension DatabaseManager { if let event { CatalogChangeService.post(event) } } - /// `isConfirmationPreCleared` is for a caller that has already shown the user a destructive - /// confirmation of its own. Without it the sidebar's Drop dialog is followed by the gate's, - /// asking twice for one gesture and once per target for a batch. It clears the confirmation - /// only: Touch ID, the read-only refusal and the audit record all still apply. + // Pre-cleared skips only a confirmation the caller already showed; Touch ID, Read-Only and the audit still apply. private func authorizeContainerOperation( sql: String?, kind: OperationKind, connectionId: UUID, databaseType: DatabaseType, description: String, - isConfirmationPreCleared: Bool + isConfirmationPreCleared: Bool, + gate: any ExecutionGate ) async throws { var capabilities = CallerCapabilities.interactiveUser if isConfirmationPreCleared { capabilities.insert(.confirmationPreCleared) } - let decision = await ExecutionGateProvider.shared.authorize( + let decision = await gate.authorize( OperationRequest( connectionId: connectionId, databaseType: databaseType, @@ -164,11 +160,7 @@ extension DatabaseManager { operationDescription: description ) ) - guard case .authorized = decision else { - throw DatabaseError.queryFailed( - decision.deniedReason ?? String(localized: "This change was not authorized") - ) - } + if let denial = decision.denialError { throw denial } } private static func execute( diff --git a/apple/TablePro/Core/Database/DatabaseManager+Health.swift b/apple/TablePro/Core/Database/DatabaseManager+Health.swift index b42ed9eae7..3f9fc5feb2 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Health.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Health.swift @@ -149,10 +149,17 @@ extension DatabaseManager { result.driver.disconnect() return .abort } + let warning = openingWarning( + driver: result.driver, + startupFailures: result.startupFailures, + restoreWarning: result.restoreWarning, + for: connectionId + ) updateSession(connectionId) { session in session.driver = result.driver session.effectiveConnection = result.effectiveConnection session.status = .connected + session.connectionWarning = warning if let schemaDriver = result.driver as? SchemaSwitchable { session.browseSchema = schemaDriver.currentSchema } @@ -233,6 +240,8 @@ extension DatabaseManager { let driver: DatabaseDriver let effectiveConnection: DatabaseConnection let cachedPassword: String? + let startupFailures: [StartupCommandFailure] + let restoreWarning: String? } // Rebuilds the tunnel for a tunneled session before connecting the new driver. Tunnels are keyed by @@ -282,13 +291,13 @@ extension DatabaseManager { throw CancellationError() } - await applyTimeoutAndStartupCommands( + let startupFailures = await applyTimeoutAndStartupCommands( on: driver, queryTimeoutSeconds: session.effectiveQueryTimeoutSeconds, startupCommands: session.connection.startupCommands, connectionName: session.connection.name ) - await restoreSchemaAndDatabase( + let restoreWarning = await restoreSchemaAndDatabase( on: driver, savedSchema: session.browseSchema, savedDatabase: databaseSwitchRequiresReconnect(session.connection) ? nil : session.browseDatabase @@ -297,7 +306,9 @@ extension DatabaseManager { return ReconnectResult( driver: driver, effectiveConnection: connectionForDriver, - cachedPassword: connectResult.cachedPassword + cachedPassword: connectResult.cachedPassword, + startupFailures: startupFailures, + restoreWarning: restoreWarning ) } @@ -306,7 +317,7 @@ extension DatabaseManager { queryTimeoutSeconds: Int, startupCommands: String?, connectionName: String - ) async { + ) async -> [StartupCommandFailure] { do { try await driver.applyQueryTimeout(queryTimeoutSeconds) } catch { @@ -315,7 +326,7 @@ extension DatabaseManager { ) } - await executeStartupCommands(startupCommands, on: driver, connectionName: connectionName) + return await executeStartupCommands(startupCommands, on: driver, connectionName: connectionName) } private func databaseSwitchRequiresReconnect(_ connection: DatabaseConnection) -> Bool { @@ -323,17 +334,23 @@ extension DatabaseManager { .capabilities.requiresReconnectForDatabaseSwitch ?? false } + // The reconnected session stays on the server's default when a restore fails, which the window would not show. func restoreSchemaAndDatabase( on driver: DatabaseDriver, savedSchema: String?, savedDatabase: String? - ) async { + ) async -> String? { + var warnings: [String?] = [] // The schema lives inside the database, and a database switch can leave the driver on none. if let savedDatabase, let adapter = driver as? PluginDriverAdapter { do { try await adapter.switchDatabase(to: savedDatabase) } catch { Self.logger.warning("Failed to restore database '\(savedDatabase)' on reconnect: \(error.localizedDescription)") + warnings.append(String( + format: String(localized: "Reconnecting could not reopen the database %1$@: %2$@"), + savedDatabase, error.localizedDescription + )) } } @@ -342,8 +359,13 @@ extension DatabaseManager { try await schemaDriver.switchSchema(to: savedSchema) } catch { Self.logger.warning("Failed to restore schema '\(savedSchema)' on reconnect: \(error.localizedDescription)") + warnings.append(String( + format: String(localized: "Reconnecting could not reopen the schema %1$@: %2$@"), + savedSchema, error.localizedDescription + )) } } + return Self.joinedWarning(warnings) } internal func stopHealthMonitor(for connectionId: UUID) async { @@ -440,13 +462,13 @@ extension DatabaseManager { } let driver = connectResult.driver - await applyTimeoutAndStartupCommands( + let startupFailures = await applyTimeoutAndStartupCommands( on: driver, queryTimeoutSeconds: session.effectiveQueryTimeoutSeconds, startupCommands: session.connection.startupCommands, connectionName: session.connection.name ) - await restoreSchemaAndDatabase( + let restoreWarning = await restoreSchemaAndDatabase( on: driver, savedSchema: activeSessions[sessionId]?.browseSchema, savedDatabase: databaseSwitchRequiresReconnect(session.connection) ? nil : activeSessions[sessionId]?.browseDatabase @@ -456,10 +478,14 @@ extension DatabaseManager { throw CancellationError() } + let warning = openingWarning( + driver: driver, startupFailures: startupFailures, restoreWarning: restoreWarning, for: sessionId + ) updateSession(sessionId) { session in session.driver = driver session.status = .connected session.effectiveConnection = effectiveConnection + session.connectionWarning = warning if let schemaDriver = driver as? SchemaSwitchable { session.browseSchema = schemaDriver.currentSchema } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Principals.swift b/apple/TablePro/Core/Database/DatabaseManager+Principals.swift index 7cf505ddcd..12680f123e 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Principals.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Principals.swift @@ -14,7 +14,8 @@ extension DatabaseManager { func executePrincipalChanges( changes: [PrincipalChange], databaseType: DatabaseType, - connectionId: UUID + connectionId: UUID, + gate: any ExecutionGate = ExecutionGateProvider.shared ) async throws { guard let driver = driver(for: connectionId) else { throw DatabaseError.notConnected @@ -39,7 +40,7 @@ extension DatabaseManager { ? .destructiveQuery : .schemaMutation - let authorization = await ExecutionGateProvider.shared.authorize( + let authorization = await gate.authorize( OperationRequest( connectionId: connectionId, databaseType: databaseType, @@ -50,12 +51,7 @@ extension DatabaseManager { operationDescription: String(localized: "Apply User and Role Changes") ) ) - guard case .authorized = authorization else { - throw DatabaseError.queryFailed( - authorization.deniedReason - ?? String(localized: "User and role changes were not authorized") - ) - } + if let denial = authorization.denialError { throw denial } /// Reported whether or not every statement ran: an engine that cannot roll principal /// statements back keeps the ones before a failure, and `DROP OWNED` among them drops objects. diff --git a/apple/TablePro/Core/Database/DatabaseManager+Schema.swift b/apple/TablePro/Core/Database/DatabaseManager+Schema.swift index fd4b884352..01ab285459 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Schema.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Schema.swift @@ -226,13 +226,7 @@ extension DatabaseManager { } } - /// Run a Create Table draft's statements, on the same isolated route and in the same shape as - /// `executeSchemaChanges`. - /// - /// The route is what matters. The view used to reach for `driver(for:)`, which is the session - /// driver, so a `BEGIN` opened here joined whatever transaction a query tab had left open and - /// the matching `COMMIT` took that tab's uncommitted work with it. `schemaChangeRoute` exists - /// for exactly that, and the app's own DDL has no business on the user's connection. + // On the schema change route: a BEGIN on the session driver would join a query tab's open transaction. func executeCreateTable( statements: [String], databaseType: DatabaseType, @@ -254,11 +248,7 @@ extension DatabaseManager { operationDescription: String(localized: "Create Table") ) ) - guard case .authorized = authorization else { - throw DatabaseError.queryFailed( - authorization.deniedReason ?? String(localized: "Operation not permitted") - ) - } + if let denial = authorization.denialError { throw denial } let executionTimes: [TimeInterval] do { diff --git a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift index 2e0bfa525e..23c6f022e6 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift @@ -136,7 +136,7 @@ extension DatabaseManager { attempt: attempt, for: connection.id ) - await applyTimeoutAndStartupCommands( + let startupFailures = await applyTimeoutAndStartupCommands( on: driver, queryTimeoutSeconds: effectiveQueryTimeoutSeconds, startupCommands: resolvedConnection.startupCommands, @@ -167,7 +167,7 @@ extension DatabaseManager { session.driver = driver session.status = driver.status session.effectiveConnection = effectiveConnection - session.connectionWarning = driver.connectionWarning + session.connectionWarning = openingWarning(driver: driver, startupFailures: startupFailures, for: connection.id) // A connect answers whatever went wrong before it, including a reconnect that gave up on this entry. session.liveness = .live session.recovery = nil @@ -1172,6 +1172,7 @@ extension DatabaseManager { monitorPingedDrivers.removeValue(forKey: connectionId) sessionDriverGate.drain(connectionId: connectionId) connectionStatusVersions.removeValue(forKey: connectionId) + reportedStartupFailures.removeValue(forKey: connectionId) forgetVerification(for: connectionId) aiAccessApprovals.revoke(connectionId) AppEvents.shared.connectionStatusChanged.send( diff --git a/apple/TablePro/Core/Database/DatabaseManager+Startup.swift b/apple/TablePro/Core/Database/DatabaseManager+Startup.swift index 5f68e9c7a7..3804867891 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Startup.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Startup.swift @@ -61,26 +61,116 @@ extension DatabaseManager { return statements } - @discardableResult nonisolated internal func executeStartupCommands( _ commands: String?, on driver: DatabaseDriver, connectionName: String - ) async -> [(statement: String, error: String)] { - var failures: [(statement: String, error: String)] = [] + ) async -> [StartupCommandFailure] { + var failures: [StartupCommandFailure] = [] let databaseType = driver.connection.type let grammar = SQLLexicalResolver.readings(for: databaseType, session: driver.sessionLexicalState).execution for statement in Self.startupStatements(from: commands, databaseType: databaseType, grammar: grammar) { do { _ = try await driver.execute(query: statement) Self.startupLogger.info( - "Startup command succeeded for '\(connectionName)': \(statement)" + "Startup command succeeded for '\(connectionName, privacy: .private)': \(statement, privacy: .private)" ) } catch { Self.startupLogger.warning( - "Startup command failed for '\(connectionName)': \(statement) — \(error.localizedDescription)" + "Startup command failed for '\(connectionName, privacy: .private)': \(statement, privacy: .private): \(error.localizedDescription, privacy: .private)" ) - failures.append((statement: statement, error: error.localizedDescription)) + failures.append(StartupCommandFailure(statement: statement, error: error)) } } return failures } + + // A startup command can be a safety rail, `SET ROLE app_ro` or a read-only default, so a session that opened + // without one says so on the connection's warning rather than only in the log. + nonisolated internal static func startupWarning(for failures: [StartupCommandFailure]) -> String? { + guard let first = failures.first else { return nil } + guard failures.count > 1 else { + guard let code = first.code else { + return String( + format: String(localized: "The startup command %@ failed, so this session runs without it."), + shownStatement(first.statement) + ) + } + return String( + format: String(localized: "The startup command %1$@ failed (%2$@), so this session runs without it."), + shownStatement(first.statement), code + ) + } + let lines = failures.map { failure in + failure.code.map { "\(shownStatement(failure.statement)) (\($0))" } ?? shownStatement(failure.statement) + } + return String( + format: String(localized: "Startup commands failed, so this session runs without them:\n%@"), + lines.joined(separator: "\n") + ) + } + + // A startup command can carry a secret, a password after IDENTIFIED BY or a key in a literal, so the banner shows + // it only up to its first quote or credential clause. + nonisolated internal static func shownStatement(_ statement: String) -> String { + let quote = statement.firstIndex(where: { $0 == "'" || $0 == "\"" || $0 == "$" }) + let stops = [quote] + ["IDENTIFIED", "PASSWORD"].map { statement.range(of: $0, options: .caseInsensitive)?.lowerBound } + let cut = stops.compactMap { $0 }.min() ?? statement.endIndex + let kept = statement[.. limit else { return kept } + return String(kept.prefix(limit)) + "…" + } + + nonisolated internal static func joinedWarning(_ parts: [String?]) -> String? { + let present = parts.compactMap { $0 }.filter { !$0.isEmpty } + return present.isEmpty ? nil : present.joined(separator: "\n\n") + } + + // The warning a session opens with: the login's own, then what its setup could not do. It replaces the previous + // one, so a reconnect that ran every startup command drops a banner about the session before it, and the record + // of named failures starts over. + internal func openingWarning( + driver: DatabaseDriver, + startupFailures: [StartupCommandFailure], + restoreWarning: String? = nil, + for connectionId: UUID + ) -> String? { + reportedStartupFailures[connectionId] = Set(startupFailures.map { $0.statement }) + return Self.joinedWarning([driver.connectionWarning, Self.startupWarning(for: startupFailures), restoreWarning]) + } + + // A pool entry or a database lane opens a session of its own, which can fail a command the window's session ran. + // Each failing statement is named once per connect: the pool keeps opening entries while the window is open. + internal func reportStartupFailures(_ failures: [StartupCommandFailure], for connectionId: UUID) { + guard activeSessions[connectionId] != nil else { return } + var reported = reportedStartupFailures[connectionId] ?? [] + let unreported = failures.filter { reported.insert($0.statement).inserted } + reportedStartupFailures[connectionId] = reported + guard let warning = Self.startupWarning(for: unreported) else { return } + updateSession(connectionId) { session in + session.connectionWarning = Self.joinedWarning([session.connectionWarning, warning]) + } + } +} + +// What the banner may show of a failed startup command: the statement and the server's code, never its message, which +// can echo the values the statement carried. The message goes to the log, private. +struct StartupCommandFailure: Sendable, Equatable { + let statement: String + let code: String? + + init(statement: String, code: String?) { + self.statement = statement + self.code = code + } + + init(statement: String, error: Error) { + self.init(statement: statement, code: Self.code(of: error)) + } + + static func code(of error: Error) -> String? { + guard let driverError = error as? any PluginDriverError else { return nil } + if let state = driverError.pluginSqlState, !state.isEmpty { return state } + if let code = driverError.pluginErrorCode, code != 0 { return String(code) } + return nil + } } diff --git a/apple/TablePro/Core/Database/DatabaseManager.swift b/apple/TablePro/Core/Database/DatabaseManager.swift index 15072aca99..2dec865754 100644 --- a/apple/TablePro/Core/Database/DatabaseManager.swift +++ b/apple/TablePro/Core/Database/DatabaseManager.swift @@ -126,6 +126,10 @@ final class DatabaseManager: ObservableObject { /// for the same reason every other shared write here is generation-checked. internal var connectionStages: [UUID: ConnectionStage] = [:] + // The startup statements a connection's warning has already named since its connect, so a pool entry or lane + // opened later names only a failure the window was not told about. + internal var reportedStartupFailures: [UUID: Set] = [:] + /// Orders operations that move the shared driver, so two windows cannot interleave /// their pins and each run against the other's database. internal let sessionDriverGate = SessionDriverGate() diff --git a/apple/TablePro/Core/Database/TriggerEditing.swift b/apple/TablePro/Core/Database/TriggerEditing.swift index c3744ad7e4..8a427c1bfd 100644 --- a/apple/TablePro/Core/Database/TriggerEditing.swift +++ b/apple/TablePro/Core/Database/TriggerEditing.swift @@ -14,7 +14,6 @@ import TableProSQLGrammar enum TriggerEditingError: LocalizedError { case notConnected - case denied(String) case dropUnavailable case notCreateTrigger case definitionUnavailable @@ -23,7 +22,6 @@ enum TriggerEditingError: LocalizedError { var errorDescription: String? { switch self { case .notConnected: String(localized: "Not connected to database") - case let .denied(reason): reason case .dropUnavailable: String(localized: "This database cannot drop triggers") case .notCreateTrigger: String( @@ -121,9 +119,7 @@ enum TriggerApplyStrategy: Equatable { enum TriggerEditing { nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "TriggerEditing") - /// Runs on the schema change route, never on the session driver: the trigger's own BEGIN - /// would join a transaction a query tab left open, and its COMMIT or ROLLBACK would take - /// that tab's uncommitted work with it. + // On the schema change route: a BEGIN on the session driver would join a query tab's open transaction. static func apply( scope: DatabaseScope, connection: DatabaseConnection, @@ -162,9 +158,7 @@ enum TriggerEditing { statements: plan.gatedStatements(for: sql) ) ) - guard case .authorized = decision else { - throw TriggerEditingError.denied(decision.deniedReason ?? String(localized: "Operation not permitted")) - } + if let denial = decision.denialError { throw denial } let startedAt = Date() defer { @@ -230,10 +224,7 @@ enum TriggerEditing { name: String, gate: any ExecutionGate = ExecutionGateProvider.shared ) async throws { - /// Built on a driver in the scope that will run it, the way `apply` builds its own inside - /// the lease. The session driver is wherever the sidebar last went, and an engine that - /// qualifies its DDL with the connection's current database writes that name into a - /// statement the pooled connection then runs somewhere else. + // Built in the scope that runs it: the session driver may qualify the DDL with another database. let generated = try await DatabaseManager.shared.withMetadataDriver(scope: scope) { driver in driver.generateDropTriggerSQL(name: name, table: tableName) } @@ -252,9 +243,7 @@ enum TriggerEditing { operationDescription: String(localized: "Drop Trigger") ) ) - guard case .authorized = decision else { - throw TriggerEditingError.denied(decision.deniedReason ?? String(localized: "Operation not permitted")) - } + if let denial = decision.denialError { throw denial } let startedAt = Date() try await withSchemaChangeDriver(scope: scope) { driver in diff --git a/apple/TablePro/Core/LSP/LSPTransport.swift b/apple/TablePro/Core/LSP/LSPTransport.swift index 47e274cc6a..6677bb3748 100644 --- a/apple/TablePro/Core/LSP/LSPTransport.swift +++ b/apple/TablePro/Core/LSP/LSPTransport.swift @@ -32,6 +32,45 @@ enum LSPTransportError: Error, LocalizedError { } } +// MARK: - LSPMessageWriter + +// Writes on its own queue, so a server that stops reading blocks the queue and never the transport. +final class LSPMessageWriter: @unchecked Sendable { + private let handle: FileHandle + private let queue = DispatchQueue(label: "com.TablePro.LSPTransport.writer") + private var isClosed = false + + init(_ handle: FileHandle) throws { + // Without it, a write to a server that has exited raises SIGPIPE and ends the app. + guard fcntl(handle.fileDescriptor, F_SETNOSIGPIPE, 1) == 0 else { + throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) + } + self.handle = handle + } + + func write(_ frame: Data, onFailure: @escaping @Sendable (any Error) -> Void) { + queue.async { [self] in + guard !isClosed else { + onFailure(LSPTransportError.processNotRunning) + return + } + do { + try handle.write(contentsOf: frame) + } catch { + onFailure(error) + } + } + } + + func close() { + queue.async { [self] in + guard !isClosed else { return } + isClosed = true + try? handle.close() + } + } +} + // MARK: - LSPTransport actor LSPTransport { @@ -39,6 +78,7 @@ actor LSPTransport { private var process: Process? private var stdinPipe: Pipe? + private var writer: LSPMessageWriter? private var stdoutPipe: Pipe? private var stderrPipe: Pipe? private var stderrReader: PipeReader? @@ -66,6 +106,7 @@ actor LSPTransport { proc.standardOutput = stdout proc.standardError = stderr + self.writer = try LSPMessageWriter(stdin.fileHandleForWriting) self.stdinPipe = stdin self.stdoutPipe = stdout self.stderrPipe = stderr @@ -105,9 +146,8 @@ actor LSPTransport { readerTask?.cancel() readerTask = nil - if let stdinHandle = stdinPipe?.fileHandleForWriting { - try? stdinHandle.close() - } + writer?.close() + writer = nil if let stdoutHandle = stdoutPipe?.fileHandleForReading { try? stdoutHandle.close() } @@ -144,7 +184,7 @@ actor LSPTransport { return try await withCheckedThrowingContinuation { continuation in pendingRequests[requestID] = continuation do { - try writeMessage(data) + try writeMessage(data, failingRequest: requestID) } catch { pendingRequests.removeValue(forKey: requestID) continuation.resume(throwing: error) @@ -212,19 +252,22 @@ actor LSPTransport { // MARK: - Private - private func writeMessage(_ data: Data) throws { - guard let stdinPipe else { + private func writeMessage(_ data: Data, failingRequest requestID: Int? = nil) throws { + guard let writer else { throw LSPTransportError.processNotRunning } - - let header = "Content-Length: \(data.count)\r\n\r\n" - guard let headerData = header.data(using: .utf8) else { - throw LSPTransportError.invalidResponse + let frame = Data("Content-Length: \(data.count)\r\n\r\n".utf8) + data + writer.write(frame) { [weak self] error in + Task { [weak self] in + await self?.writeFailed(error, requestID: requestID) + } } + } - let handle = stdinPipe.fileHandleForWriting - handle.write(headerData) - handle.write(data) + private func writeFailed(_ error: any Error, requestID: Int?) { + Self.logger.warning("LSP write failed: \(error.localizedDescription)") + guard let requestID, let continuation = pendingRequests.removeValue(forKey: requestID) else { return } + continuation.resume(throwing: error) } private func runReadLoop(handle: FileHandle) async { @@ -341,6 +384,9 @@ actor LSPTransport { private func handleProcessExit(code: Int32) { Self.logger.info("LSP process exited with code \(code)") + writer?.close() + writer = nil + stdinPipe = nil let pending = pendingRequests pendingRequests.removeAll() for (_, continuation) in pending { diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift index 164f6c3613..5b6028255f 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift @@ -38,6 +38,8 @@ internal struct ObjectCopyRowCopier: Sendable { internal struct Outcome: Sendable { internal let inserted: Int internal let cancelled: Bool + // The limit the source's stream stopped at, so the table holds only its leading rows. + internal var rowCut: Int? } /// A table that failed after some of its batches were written. Outside a transaction those stay @@ -94,7 +96,13 @@ internal struct ObjectCopyRowCopier: Sendable { let generator = try makeGenerator(targetDriver: targetDriver) let budget = SQLWriteBatchBudget(columnCount: step.columns.count, generator: generator) var filler = SQLWriteBatchFiller<[PluginCellValue]>(budget: budget) - var stream = sourceDriver.streamRows(query: step.sourceQuery).makeAsyncIterator() + let cut = OSAllocatedUnfairLock(initialState: nil) + // The copy's SELECT states no engine ceiling, so only a limit the driver declares applies, as for its own export query. + var stream = ExportRowCut.reporting( + sourceDriver.streamRows(query: step.sourceQuery), engineCeiling: nil + ) { limit in + cut.withLock { $0 = limit } + }.makeAsyncIterator() var inserted = 0 @@ -121,7 +129,7 @@ internal struct ObjectCopyRowCopier: Sendable { inserted += try await write(batch, generator: generator, driver: targetDriver) onProgress(inserted) } - return Outcome(inserted: inserted, cancelled: Task.isCancelled) + return Outcome(inserted: inserted, cancelled: Task.isCancelled, rowCut: cut.withLock { $0 }) } catch is CancellationError { return Outcome(inserted: inserted, cancelled: true) } catch where inserted > 0 { diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift index 6cef58ed51..f8c04fd656 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift @@ -22,6 +22,7 @@ internal struct ObjectCopyObjectOutcome: Identifiable, Sendable { internal let selection: ObjectCopySelection internal let rowsCopied: Int internal let error: String? + internal var warning: String? internal var id: String { selection.id } internal var succeeded: Bool { error == nil } @@ -67,6 +68,8 @@ internal struct ObjectCopyRunResult: Sendable { internal var firstError: String? { outcomes.compactMap(\.error).first } + internal var warnings: [String] { outcomes.compactMap(\.warning) } + /// Every failure, each with an identity of its own. /// /// One object fails once per phase it reaches, so a table whose `CREATE` failed under Skip and @@ -507,9 +510,14 @@ internal struct ObjectCopyRunner { /// would send a retry over them. var rowsCopied = 0 - mutating func recordCopy(of step: ObjectCopyTableStep, inserted: Int) { + mutating func recordCopy(of step: ObjectCopyTableStep, inserted: Int, rowCut: Int? = nil) { rowsCopied += inserted - outcomes.append(ObjectCopyObjectOutcome(selection: step.selection, rowsCopied: inserted, error: nil)) + outcomes.append(ObjectCopyObjectOutcome( + selection: step.selection, + rowsCopied: inserted, + error: nil, + warning: rowCut.map { StreamRowCutWarning.text(table: step.selection.qualifiedName, limit: $0) } + )) } /// Stop reaching the driver mid-stream is the user stopping, not the copy failing, and the @@ -702,7 +710,7 @@ internal struct ObjectCopyRunner { result.recordStop(of: step, inserted: outcome.inserted, progress: progress) break } - result.recordCopy(of: step, inserted: outcome.inserted) + result.recordCopy(of: step, inserted: outcome.inserted, rowCut: outcome.rowCut) } catch is CancellationError { result.recordStop(of: step, inserted: 0, progress: progress) break @@ -748,7 +756,7 @@ internal struct ObjectCopyRunner { result.recordStop(of: step, inserted: outcome.inserted, progress: progress) break } - result.recordCopy(of: step, inserted: outcome.inserted) + result.recordCopy(of: step, inserted: outcome.inserted, rowCut: outcome.rowCut) } catch is CancellationError { result.recordStop(of: step, inserted: 0, progress: progress) break diff --git a/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift b/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift index 4c67711e74..5878dcda37 100644 --- a/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift +++ b/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift @@ -61,13 +61,7 @@ final class ExportDataSourceAdapter: PluginExportDataSource, @unchecked Sendable // One line per table whose rows stopped at a ceiling, so a partial copy is never reported as the whole table. var cappedTableWarnings: [String] { - cappedTables.withLock { $0 }.map { capped in - String( - format: String(localized: "%1$@: only the first %2$lld rows were read, the most this database returns from one query."), - capped.table, - capped.limit - ) - } + cappedTables.withLock { $0 }.map { StreamRowCutWarning.text(table: $0.table, limit: $0.limit) } } func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { diff --git a/apple/TablePro/Core/Plugins/ExportRowCut.swift b/apple/TablePro/Core/Plugins/ExportRowCut.swift index 04efb07dc6..a3be667953 100644 --- a/apple/TablePro/Core/Plugins/ExportRowCut.swift +++ b/apple/TablePro/Core/Plugins/ExportRowCut.swift @@ -58,6 +58,17 @@ final class ExportRowCut: @unchecked Sendable { } } +// One wording for every reader of a cut stream: export, Table Transfer, Object Copy and Data Compare. +enum StreamRowCutWarning { + static func text(table: String, limit: Int) -> String { + String( + format: String(localized: "%1$@: only the first %2$lld rows were read, the most this database returns from one query."), + table, + limit + ) + } +} + extension ExportRowCut where Element == PluginStreamElement { static func reporting( _ upstream: AsyncThrowingStream, diff --git a/apple/TablePro/Core/Services/Execution/GatedOperationFailure.swift b/apple/TablePro/Core/Services/Execution/GatedOperationFailure.swift new file mode 100644 index 0000000000..f0e6bdaeae --- /dev/null +++ b/apple/TablePro/Core/Services/Execution/GatedOperationFailure.swift @@ -0,0 +1,39 @@ +// +// GatedOperationFailure.swift +// TablePro +// + +import Foundation + +// Cancel at the gate's confirmation is the user's own choice: it shows nothing and the sheet keeps its edits. +internal enum GatedOperationFailure: Equatable { + case cancelledByUser + case refused(String) + case failed(String) + + internal init(_ error: any Error) { + guard let gateError = error as? ExecutionGateError else { + self = .failed(error.localizedDescription) + return + } + switch gateError { + case .cancelledByUser: + self = .cancelledByUser + case .denied(let reason): + self = .refused(reason) + } + } + + internal var message: String? { + switch self { + case .cancelledByUser: nil + case .refused(let reason), .failed(let reason): reason + } + } + + // A refusal and a Cancel both stop before the first statement: nothing to report or reload. + internal var reportsFailure: Bool { + if case .failed = self { return true } + return false + } +} diff --git a/apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift b/apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift index 2d85197c26..c7a274f88f 100644 --- a/apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift +++ b/apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift @@ -591,13 +591,14 @@ final class MetadataConnectionPool { ) do { try await driver.connect() - try await Self.prepareSession( + let startupFailures = try await Self.prepareSession( driver, queryTimeoutSeconds: queryTimeout, startupCommands: session.connection.startupCommands, connectionName: session.connection.name, timeoutSeconds: operationTimeoutSeconds ) + DatabaseManager.shared.reportStartupFailures(startupFailures, for: scope.connectionId) if let database = plan.switchDatabase { try await Self.switchDatabase(driver, to: database, timeoutSeconds: operationTimeoutSeconds) } @@ -680,14 +681,14 @@ final class MetadataConnectionPool { startupCommands: String?, connectionName: String, timeoutSeconds: Double? - ) async throws { + ) async throws -> [StartupCommandFailure] { try await bounded( driver: driver, timeoutSeconds: timeoutSeconds, timeoutMessage: String(localized: "Preparing the metadata connection timed out.") ) { try? await driver.applyQueryTimeout(queryTimeoutSeconds) - await DatabaseManager.shared.executeStartupCommands( + return await DatabaseManager.shared.executeStartupCommands( startupCommands, on: driver, connectionName: connectionName ) } @@ -695,18 +696,17 @@ final class MetadataConnectionPool { /// Disconnects the driver when the deadline fires so a driver call that /// ignores task cancellation still completes and the timeout can propagate. - private static func bounded( + private static func bounded( driver: DatabaseDriver, timeoutSeconds: Double?, timeoutMessage: String, - _ operation: @escaping @Sendable () async throws -> Void - ) async throws { + _ operation: @escaping @Sendable () async throws -> T + ) async throws -> T { guard let timeoutSeconds, timeoutSeconds > 0 else { - try await operation() - return + return try await operation() } do { - try await withTimeout( + return try await withTimeout( seconds: timeoutSeconds, onTimeout: { driver.disconnect() }, operation: operation diff --git a/apple/TablePro/Core/Services/Query/RowOperationsManager.swift b/apple/TablePro/Core/Services/Query/RowOperationsManager.swift index 66f5ebf5e4..2a33425139 100644 --- a/apple/TablePro/Core/Services/Query/RowOperationsManager.swift +++ b/apple/TablePro/Core/Services/Query/RowOperationsManager.swift @@ -49,10 +49,12 @@ final class RowOperationsManager { func addNewRow(tableRows: inout TableRows, uuidKeyForms: [Int: UUIDCellForm] = [:]) -> AddNewRowResult? { var newRowValues: [PluginCellValue] = [] var absentColumns: Set = [] + var defaultColumns: Set = [] var prefilled: Set = [] for (index, column) in tableRows.columns.enumerated() { if tableRows.generatedColumns.contains(column) || tableRows.serverAssignsValue(forColumn: column) { - newRowValues.append(.text("__DEFAULT__")) + newRowValues.append(.columnDefault) + defaultColumns.insert(index) } else if let form = uuidKeyForms[index] { newRowValues.append(Self.freshUUID(form)) prefilled.insert(index) @@ -62,7 +64,11 @@ final class RowOperationsManager { } } return appendInsertedRow( - values: newRowValues, absentColumns: absentColumns, prefilledColumns: prefilled, to: &tableRows + values: newRowValues, + absentColumns: absentColumns, + expressionColumns: defaultColumns, + prefilledColumns: prefilled, + to: &tableRows ) } @@ -86,14 +92,15 @@ final class RowOperationsManager { + Array(tableRows.columnIdentity.keys) for resetColumn in resetColumns { if let index = tableRows.columns.firstIndex(of: resetColumn), index < newValues.count { + absentColumns.remove(index) if let form = uuidKeyForms[index] { newValues[index] = Self.freshUUID(form) prefilled.insert(index) + expressionColumns.remove(index) } else { - newValues[index] = .text("__DEFAULT__") + newValues[index] = .columnDefault + expressionColumns.insert(index) } - absentColumns.remove(index) - expressionColumns.remove(index) } } return appendInsertedRow( @@ -406,9 +413,9 @@ final class RowOperationsManager { } if let pkIndex = schema.primaryKeyIndex, pkIndex < values.count { - values[pkIndex] = .text("__DEFAULT__") + values[pkIndex] = .columnDefault absentColumns.remove(pkIndex) - expressionColumns.remove(pkIndex) + expressionColumns.insert(pkIndex) } return ParsedRow( @@ -438,8 +445,8 @@ final class RowOperationsManager { return .entered(text, columnType: tableRows.editingColumnType(at: column)) } if let pkIndex = schema.primaryKeyIndex, pkIndex < values.count { - values[pkIndex] = .text("__DEFAULT__") - expressionColumns.remove(pkIndex) + values[pkIndex] = .columnDefault + expressionColumns.insert(pkIndex) } return ParsedRow(values: values, sourceLineNumber: index + 1, expressionColumns: expressionColumns) } @@ -447,28 +454,39 @@ final class RowOperationsManager { // A blank cell of pasted text was left unfilled, so it takes what a new row's cell does: the server's value where // the column has a default, else NULL. A NOT NULL text column with no default takes the empty string it can hold. - private static func unfilledCellValues(columns: [String], tableRows: TableRows) -> [PluginCellValue] { - columns.map { name in + private static func unfilledCellValues( + columns: [String], + tableRows: TableRows + ) -> (values: [PluginCellValue], defaults: Set) { + var defaults: Set = [] + let values = columns.enumerated().map { position, name -> PluginCellValue in guard let index = tableRows.columns.firstIndex(of: name) else { return .null } if tableRows.generatedColumns.contains(name) || tableRows.serverAssignsValue(forColumn: name) { - return .text("__DEFAULT__") + defaults.insert(position) + return .columnDefault } if tableRows.refusesNull(columnAt: index), tableRows.editingColumnType(at: index)?.hasEmptyValue == true { return .text("") } return .null } + return (values, defaults) } - private static func fillingBlankCells(_ row: ParsedRow, with unfilled: [PluginCellValue]) -> ParsedRow { + private static func fillingBlankCells( + _ row: ParsedRow, + with unfilled: (values: [PluginCellValue], defaults: Set) + ) -> ParsedRow { guard !row.blankColumns.isEmpty else { return row } var values = row.values - for index in row.blankColumns where index < values.count && index < unfilled.count { - values[index] = unfilled[index] + var expressionColumns = row.expressionColumns + for index in row.blankColumns where index < values.count && index < unfilled.values.count { + values[index] = unfilled.values[index] + if unfilled.defaults.contains(index) { expressionColumns.insert(index) } } var filled = ParsedRow(values: values, sourceLineNumber: row.sourceLineNumber) filled.absentColumns = row.absentColumns - filled.expressionColumns = row.expressionColumns + filled.expressionColumns = expressionColumns return filled } @@ -509,7 +527,7 @@ final class RowOperationsManager { return containsComma ? CSVRowParser() : TSVRowParser() } - // Every paste path resets the key to __DEFAULT__ first; an unfilled UUID key then takes a fresh UUID per row. + // Every paste path resets the key to its default first; an unfilled UUID key then takes a fresh UUID per row. private func insertParsedRows( _ parsedRows: [ParsedRow], uuidKeyForms: [Int: UUIDCellForm], @@ -531,11 +549,12 @@ final class RowOperationsManager { var absentColumns = parsedRow.absentColumns var expressionColumns = parsedRow.expressionColumns.filter { $0 < rowValues.count } for index in serverOwned where index < rowValues.count { - rowValues[index] = .text("__DEFAULT__") + rowValues[index] = .columnDefault absentColumns.remove(index) - expressionColumns.remove(index) + expressionColumns.insert(index) } - for (index, form) in uuidKeyForms where index < rowValues.count && rowValues[index] == .text("__DEFAULT__") { + for (index, form) in uuidKeyForms where index < rowValues.count + && rowValues[index].isColumnDefault(isExpression: expressionColumns.contains(index)) { rowValues[index] = Self.freshUUID(form) absentColumns.remove(index) expressionColumns.remove(index) diff --git a/apple/TablePro/Core/Services/Query/RowParser.swift b/apple/TablePro/Core/Services/Query/RowParser.swift index cdd13c09a6..64179f3039 100644 --- a/apple/TablePro/Core/Services/Query/RowParser.swift +++ b/apple/TablePro/Core/Services/Query/RowParser.swift @@ -37,13 +37,17 @@ private func fittedRow( blankColumns.insert(values.count) values.append(nil) } - if let pkIndex = schema.primaryKeyIndex, pkIndex < values.count { - values[pkIndex] = "__DEFAULT__" + var cells = values.map(PluginCellValue.fromOptional) + var defaultColumns: Set = [] + if let pkIndex = schema.primaryKeyIndex, pkIndex < cells.count { + cells[pkIndex] = .columnDefault + defaultColumns.insert(pkIndex) blankColumns.remove(pkIndex) } return ParsedRow( - values: values.map(PluginCellValue.fromOptional), + values: cells, sourceLineNumber: lineNumber, + expressionColumns: defaultColumns, blankColumns: blankColumns ) } diff --git a/apple/TablePro/Core/Services/Query/SelectionSummaryCalculator.swift b/apple/TablePro/Core/Services/Query/SelectionSummaryCalculator.swift index 7470c95bb8..4d031fd612 100644 --- a/apple/TablePro/Core/Services/Query/SelectionSummaryCalculator.swift +++ b/apple/TablePro/Core/Services/Query/SelectionSummaryCalculator.swift @@ -61,11 +61,8 @@ actor SelectionSummaryCalculator { } private struct Tally { - private static let defaultMarkerLength = PluginCellValue.defaultMarkerText.utf8.count - let emptyTextIsEmpty: Bool - let insertedRowIDs: Set - let modifiedCells: [RowID: Set] + let defaultedCells: [RowID: Set] var valueCount = 0 var emptyCount = 0 var notANumberCount = 0 @@ -73,8 +70,7 @@ private struct Tally { init(input: SelectionSummaryInput) { emptyTextIsEmpty = input.policy.emptyTextIsEmpty - insertedRowIDs = input.insertedRowIDs - modifiedCells = input.modifiedCells + defaultedCells = input.defaultedCells } mutating func add(_ value: PluginCellValue, column: Int, rule: SelectionSummaryColumnRule, rowID: RowID) { @@ -108,13 +104,9 @@ private struct Tally { return numbers.add(text.trimmingCharacters(in: .whitespaces)) } - // The default marker is the grid's placeholder only where an edit put it; elsewhere it is text the table holds. private func isEmpty(_ text: String, column: Int, rowID: RowID) -> Bool { if text.isEmpty { return emptyTextIsEmpty } - guard text.utf8.count == Self.defaultMarkerLength, text == PluginCellValue.defaultMarkerText else { - return false - } - return insertedRowIDs.contains(rowID) || modifiedCells[rowID]?.contains(column) == true + return defaultedCells[rowID]?.contains(column) == true } func summary(coversWholeColumn: Bool) -> SelectionSummary { diff --git a/apple/TablePro/Core/Shortcuts/Shared/PayloadRowPlanner.swift b/apple/TablePro/Core/Shortcuts/Shared/PayloadRowPlanner.swift index 47d4e2da17..0065a4e3d0 100644 --- a/apple/TablePro/Core/Shortcuts/Shared/PayloadRowPlanner.swift +++ b/apple/TablePro/Core/Shortcuts/Shared/PayloadRowPlanner.swift @@ -16,17 +16,17 @@ nonisolated enum PayloadRowPlanner { dropsEmptyPrimaryKey: Bool = true ) throws -> [[(column: String, value: String?)]] { guard !columns.isEmpty else { throw IntentDataError.noColumns(table) } - let columnNames = Set(columns.map(\.name)) + let columnNames = columns.map(\.name) let primaryKeys = Set(columns.filter(\.isPrimaryKey).map(\.name)) return try rows.compactMap { row in - let unknown = row.keys.filter { !columnNames.contains($0) } - guard unknown.isEmpty else { throw IntentDataError.unknownColumns(unknown.sorted(), table) } + let matched = keysByColumn(row.keys, columns: columnNames) + guard matched.unknown.isEmpty else { throw IntentDataError.unknownColumns(matched.unknown.sorted(), table) } var values: [(column: String, value: String?)] = [] for column in columns { guard !column.isServerOwned else { continue } - guard let value = row.value(for: column.name) else { continue } + guard let key = matched.byColumn[column.name], let value = row.value(for: key) else { continue } if dropsEmptyPrimaryKey, primaryKeys.contains(column.name), value.isEmptyOrNull { continue } values.append((column: column.name, value: value.sqlValue)) } @@ -34,4 +34,46 @@ nonisolated enum PayloadRowPlanner { return values } } + + // As the import sheet matches a header: its exact spelling claims a column first, then a case-insensitive match. + // There is no sheet to map a header by hand, so SQL Server's `[name]` also finds `name`. + static func keysByColumn(_ keys: [String], columns: [String]) -> (byColumn: [String: String], unknown: [String]) { + var byColumn: [String: String] = [:] + var loose: [String] = [] + let spelled = Set(columns) + for key in keys.sorted() { + if spelled.contains(key) { + byColumn[key] = key + } else { + loose.append(key) + } + } + var unknown: [String] = [] + for key in loose { + let claimed = byColumn + guard let column = matchingColumn(for: key, in: columns, unclaimed: { claimed[$0] == nil }) else { + unknown.append(key) + continue + } + byColumn[column] = key + } + return (byColumn, unknown) + } + + private static func matchingColumn(for key: String, in columns: [String], unclaimed: (String) -> Bool) -> String? { + for name in [key, unbracketed(key)].compactMap({ $0 }) { + if columns.contains(name), unclaimed(name) { return name } + let folded = columns.filter { $0.caseInsensitiveCompare(name) == .orderedSame && unclaimed($0) } + if folded.count == 1 { return folded[0] } + } + return nil + } + + // `]` inside the brackets is doubled, so a lone one means the key is not a bracketed name. + private static func unbracketed(_ key: String) -> String? { + guard key.count > 2, key.hasPrefix("["), key.hasSuffix("]") else { return nil } + let inner = String(key.dropFirst().dropLast()) + guard !inner.replacingOccurrences(of: "]]", with: "", options: .literal).contains("]") else { return nil } + return inner.replacingOccurrences(of: "]]", with: "]", options: .literal) + } } diff --git a/apple/TablePro/Core/Shortcuts/Shared/RowPayload.swift b/apple/TablePro/Core/Shortcuts/Shared/RowPayload.swift index 99fd7d2c56..95736a027e 100644 --- a/apple/TablePro/Core/Shortcuts/Shared/RowPayload.swift +++ b/apple/TablePro/Core/Shortcuts/Shared/RowPayload.swift @@ -11,15 +11,29 @@ nonisolated enum RowPayload { let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines) guard !trimmed.isEmpty else { throw IntentDataError.emptyPayload } - let rows = trimmed.hasPrefix("{") || trimmed.hasPrefix("[") - ? try parseJSON(trimmed) - : try parseCSV(raw) + let rows = try sniffedRows(raw: raw, trimmed: trimmed) guard !rows.isEmpty else { throw IntentDataError.emptyPayload } guard rows.count <= maxRows else { throw IntentDataError.tooManyRows(maxRows) } return rows } + // A CSV header can open with `[` or `{`, as SQL Server's `[id],[name]` does, so only text that parses as JSON is + // JSON. Text that is neither keeps the JSON error, which names what broke in a payload meant as JSON. So does JSON + // that still splits into even CSV records, one object per line with a trailing comma: its headers hold `":`. + private static func sniffedRows(raw: String, trimmed: String) throws -> [PayloadRow] { + guard trimmed.hasPrefix("{") || trimmed.hasPrefix("[") else { return try parseCSV(raw) } + do { + return try parseJSON(trimmed) + } catch IntentDataError.malformedPayload(let reason) { + guard let rows = try? parseCSV(raw), !rows.isEmpty, + !rows.contains(where: { row in row.keys.contains { $0.contains("\":") } }) else { + throw IntentDataError.malformedPayload(reason) + } + return rows + } + } + static func parseSingle(data: String?, file: IntentFile?) async throws -> [PayloadRow] { let rows = try await parse(data: data, file: file) guard rows.count == 1 else { throw IntentDataError.expectedSingleRow } diff --git a/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift b/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift index 1baee5a518..4336e6c51f 100644 --- a/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift +++ b/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift @@ -157,10 +157,11 @@ internal enum ShortcutsRowWriter { let rowID = RowID.inserted(UUID()) let byColumn = Dictionary(values.map { ($0.column, $0.value) }, uniquingKeysWith: { first, _ in first }) insertedRowData[rowID] = names.map { name in - guard let value = byColumn[name] else { return .text(PluginCellValue.defaultMarkerText) } + guard let value = byColumn[name] else { return .columnDefault } return value.map(PluginCellValue.text) ?? .null } - changes.append(RowChange(rowID: rowID, type: .insert, sequence: index)) + let defaulted = Set(names.indices.filter { byColumn[names[$0]] == nil }) + changes.append(RowChange(rowID: rowID, type: .insert, expressionColumns: defaulted, sequence: index)) } let factory = RowChangeStatementFactory( diff --git a/apple/TablePro/Core/UsersRoles/PrincipalChangeManager.swift b/apple/TablePro/Core/UsersRoles/PrincipalChangeManager.swift index 1a7be6130b..578cfc531f 100644 --- a/apple/TablePro/Core/UsersRoles/PrincipalChangeManager.swift +++ b/apple/TablePro/Core/UsersRoles/PrincipalChangeManager.swift @@ -432,14 +432,20 @@ final class PrincipalChangeManager: ObservableObject { // MARK: - Output + // A staged drop keeps the principal's other edits so undoing the drop brings them back, but only the DROP runs. func pendingChanges() -> [PrincipalChange] { var changes: [PrincipalChange] = pendingCreates.map { .create($0) } changes += pendingAlters.compactMap { ref, definition in - guard let original = principals.first(where: { $0.ref == ref }) else { return nil } + guard pendingDrops[ref] == nil, + let original = principals.first(where: { $0.ref == ref }) + else { return nil } return .alter(old: Self.definition(from: original), new: definition) } - changes += pendingPasswords.map { .setPassword(ref: $0.key, password: $0.value) } + changes += pendingPasswords.compactMap { ref, password in + guard pendingDrops[ref] == nil else { return nil } + return .setPassword(ref: ref, password: password) + } changes += grantChangeSets().map { .modifyGrants($0) } changes += pendingDrops.map { .drop(ref: $0.key, options: $0.value) } return changes @@ -531,11 +537,12 @@ final class PrincipalChangeManager: ObservableObject { } private func recomputeChangeCount() { - let grantChanges = grantDeltas.values.reduce(0) { $0 + $1.count } + let isLive = { (ref: PluginPrincipalRef) in self.pendingDrops[ref] == nil } + let grantChanges = grantDeltas.reduce(0) { isLive($1.key) ? $0 + $1.value.count : $0 } changeCount = pendingCreates.count + pendingDrops.count - + pendingPasswords.count - + pendingAlters.count + + pendingPasswords.keys.count(where: isLive) + + pendingAlters.keys.count(where: isLive) + grantChanges } diff --git a/apple/TablePro/Core/Utilities/JavaScript/QueryStatementModel.swift b/apple/TablePro/Core/Utilities/JavaScript/QueryStatementModel.swift index 5161eb1e38..a805e5be70 100644 --- a/apple/TablePro/Core/Utilities/JavaScript/QueryStatementModel.swift +++ b/apple/TablePro/Core/Utilities/JavaScript/QueryStatementModel.swift @@ -189,11 +189,11 @@ enum QueryStatementScanner { } } - private static func located(_ command: RedisCommandSplitter.Command) -> SQLStatementScanner.LocatedStatement { + fileprivate static func located(_ command: RedisCommandSplitter.Command) -> SQLStatementScanner.LocatedStatement { SQLStatementScanner.LocatedStatement(wholeText: command.text, offset: command.range.location) } - private static func located( + fileprivate static func located( _ statement: JavaScriptStatementScanner.Statement ) -> SQLStatementScanner.LocatedStatement { SQLStatementScanner.LocatedStatement( @@ -203,3 +203,51 @@ enum QueryStatementScanner { ) } } + +// A JavaScript or Redis document's statements, read once per text, so a caret move finds its statement by search and +// answers what `QueryStatementScanner.locatedStatementAtCursor` would. A SQL document uses `SQLStatementCursorIndex`. +struct QueryStatementCaretTable { + private let model: QueryStatementModel + private let statements: [SQLStatementScanner.LocatedStatement] + // The last caret offset each statement owns, in document order. + private let reaches: [Int] + + init?(text: String, model: QueryStatementModel) { + self.model = model + switch model { + case .sql: + return nil + case .javascript: + let found = JavaScriptStatementScanner.executableStatements(in: text) + statements = found.map(QueryStatementScanner.located) + reaches = found.map { NSMaxRange($0.range) } + case .redis: + let found = RedisCommandSplitter.commandsWithCaretReach(in: text) + statements = found.map { QueryStatementScanner.located($0.command) } + reaches = found.map { $0.reach } + } + } + + func statement(atCursor cursor: Int) -> SQLStatementScanner.LocatedStatement { + var low = 0 + var high = reaches.count + while low < high { + let middle = (low + high) / 2 + if reaches[middle] < cursor { + low = middle + 1 + } else { + high = middle + } + } + let nothing = SQLStatementScanner.LocatedStatement(sql: "", offset: 0, hasContent: false) + switch model { + case .redis: + return low < statements.count ? statements[low] : nothing + case .javascript, .sql: + // Off every statement, the caret takes the one before it, or the first. + guard !statements.isEmpty else { return nothing } + if low < statements.count, statements[low].offset <= cursor { return statements[low] } + return statements[max(0, low - 1)] + } + } +} diff --git a/apple/TablePro/Core/Utilities/SQL/DisplayedResultReader.swift b/apple/TablePro/Core/Utilities/SQL/DisplayedResultReader.swift index 869817eb65..a9463a1cb4 100644 --- a/apple/TablePro/Core/Utilities/SQL/DisplayedResultReader.swift +++ b/apple/TablePro/Core/Utilities/SQL/DisplayedResultReader.swift @@ -15,12 +15,14 @@ internal enum DisplayedResultReader { internal let skippedDeletedCount: Int } - // Empty selectedDisplayIndices means every displayed row. + // Empty selectedDisplayIndices means every displayed row. A cell in defaultedCells has no value until the server + // gives it one, so it reads as NULL. internal static func read( tableRows: TableRows, displayIDs: [RowID]?, selectedDisplayIndices: Set, deletedRowIDs: Set = [], + defaultedCells: [RowID: Set] = [:], columns projection: VisibleColumnProjection ) -> Output { let positions: [Int] @@ -38,7 +40,7 @@ internal enum DisplayedResultReader { skippedDeleted += 1 return nil } - return projection.values(Self.values(of: row)) + return projection.values(Self.values(of: row, defaulted: defaultedCells[row.id] ?? [])) } return Output( @@ -50,12 +52,15 @@ internal enum DisplayedResultReader { } // A value the page left on the server says so, rather than reading as NULL. - private static func values(of row: Row) -> [PluginCellValue] { - guard row.hasUnloadedValues else { return Array(row.values) } + private static func values(of row: Row, defaulted: Set) -> [PluginCellValue] { + guard row.hasUnloadedValues || !defaulted.isEmpty else { return Array(row.values) } var values = Array(row.values) for (column, byteCount) in row.unloadedColumns where values.indices.contains(column) { values[column] = .text(UnloadedValueText.notLoaded(byteCount: byteCount)) } + for column in defaulted where values.indices.contains(column) { + values[column] = .null + } return values } } diff --git a/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift b/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift index efc41e9320..5cb6637e19 100644 --- a/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift +++ b/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift @@ -40,7 +40,7 @@ internal struct JsonRowConverter { } let cell = row[colIdx] - if cell.isNull || cell.isDefaultMarker { + if cell.isNull { result.append("null") appendPropertySuffix(to: &result, colIdx: colIdx) continue diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift index c326a505df..19d3dc08ff 100644 --- a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift @@ -102,16 +102,21 @@ enum QueryClassifier { isMultiStatement(sql, databaseType: databaseType, readings: databaseType.lexicalReadings) } - /// Whether `sql` runs more than one statement: it holds several, a `GO n` line runs its one statement n times, or it - /// ends inside a block it opened, which an engine that reads the opener as a name runs as several statements. A - /// caller that sends one statement once cannot honour the count, and dropping it runs the statement once. + /// Whether `sql` runs more than one statement: it holds several, a `GO n` line runs its one statement n times, it + /// ends inside a block it opened, which an engine that reads the opener as a name runs as several statements, or a + /// T-SQL block in it holds several. A caller that sends one statement once cannot honour the count, and dropping it + /// runs the statement once. static func isMultiStatement(_ sql: String, databaseType: DatabaseType, readings: SQLLexicalReadings) -> Bool { let model = QueryStatementModel.forDatabaseType(databaseType) guard let lexings = readings.lexings(of: sql) else { return true } return lexings.contains { lexing in let batches = QueryBatchPlanner.batches(in: lexing.text, model: model, grammar: lexing.grammar) - return batches.flatMap(\.statements).count > 1 || batches.contains { $0.repeatCount > 1 } + let statements = batches.flatMap(\.statements) + return statements.count > 1 || batches.contains { $0.repeatCount > 1 } || SQLStatementScanner.endsInsideOpenBlock(lexing.text, grammar: lexing.grammar) + || statements.contains { statement in + SQLUnterminatedStatements.blockHoldsSeveralStatements(statement.sql, grammar: lexing.grammar) + } } } diff --git a/apple/TablePro/Core/Utilities/SQL/ResultJsonSerializer.swift b/apple/TablePro/Core/Utilities/SQL/ResultJsonSerializer.swift index c2f9ead605..1bb897caff 100644 --- a/apple/TablePro/Core/Utilities/SQL/ResultJsonSerializer.swift +++ b/apple/TablePro/Core/Utilities/SQL/ResultJsonSerializer.swift @@ -36,6 +36,7 @@ internal enum ResultJsonSerializer { displayIDs: [RowID]?, selectedDisplayIndices: Set, deletedRowIDs: Set = [], + defaultedCells: [RowID: Set] = [:], columns projection: VisibleColumnProjection ) -> Output { let read = DisplayedResultReader.read( @@ -43,6 +44,7 @@ internal enum ResultJsonSerializer { displayIDs: displayIDs, selectedDisplayIndices: selectedDisplayIndices, deletedRowIDs: deletedRowIDs, + defaultedCells: defaultedCells, columns: projection ) diff --git a/apple/TablePro/Core/Utilities/SQL/SQLRowToStatementConverter.swift b/apple/TablePro/Core/Utilities/SQL/SQLRowToStatementConverter.swift index 4eac9054f4..3c763effc6 100644 --- a/apple/TablePro/Core/Utilities/SQL/SQLRowToStatementConverter.swift +++ b/apple/TablePro/Core/Utilities/SQL/SQLRowToStatementConverter.swift @@ -228,18 +228,6 @@ internal struct SQLRowToStatementConverter { } private func formatBinaryLiteral(_ data: Data) -> String { - var hex = "" - hex.reserveCapacity(data.count * 2) - for byte in data { - hex += String(format: "%02X", byte) - } - switch databaseType { - case .postgresql, .redshift, .cockroachdb, .pglite, .kingbasees: - return "'\\x\(hex)'::bytea" - case .mssql: - return "0x\(hex)" - default: - return "X'\(hex)'" - } + CompareSQLLiteral.binaryLiteral(for: data, databaseType: databaseType) ?? "X'\(data.uppercaseHex())'" } } diff --git a/apple/TablePro/Core/Vim/VimTextBufferAdapter.swift b/apple/TablePro/Core/Vim/VimTextBufferAdapter.swift index 4d012d8074..9d8bde2d77 100644 --- a/apple/TablePro/Core/Vim/VimTextBufferAdapter.swift +++ b/apple/TablePro/Core/Vim/VimTextBufferAdapter.swift @@ -51,65 +51,6 @@ final class VimTextBufferAdapter: VimTextBuffer { cachedLineCount = nil } - /// Incrementally update the cached line count based on text change delta. - /// Avoids a full O(n) recount on every keystroke. - func textDidChange(in range: NSRange, replacementLength: Int) { - guard let textView else { - cachedLineCount = nil - return - } - - guard let cached = cachedLineCount else { return } - - let nsString = textView.string as NSString - - // Pure insertion: count newlines in the new text and apply delta - if range.length == 0 { - var addedNewlines = 0 - let end = range.location + replacementLength - if replacementLength > 0 && end <= nsString.length { - for i in range.location.. 0 && range.location + range.length <= oldNs.length { - let end = range.location + range.length - for i in range.location.. 0 && replacementEnd <= nsString.length { - for i in range.location.. NSRange { guard let textView else { return NSRange(location: 0, length: 0) } let nsString = textView.string as NSString diff --git a/apple/TablePro/Extensions/PluginCellValue+DefaultMarker.swift b/apple/TablePro/Extensions/PluginCellValue+DefaultMarker.swift index 74b11c7a5b..63f9e24d11 100644 --- a/apple/TablePro/Extensions/PluginCellValue+DefaultMarker.swift +++ b/apple/TablePro/Extensions/PluginCellValue+DefaultMarker.swift @@ -7,16 +7,15 @@ import Foundation import TableProPluginKit internal extension PluginCellValue { - /// The sentinel a pending inserted row carries for a column the server fills in, written by - /// `RowOperationsManager` and stripped again by `SQLStatementGenerator`. - /// - /// It is an internal marker, never a value, so every view that renders a cell has to recognise - /// it. The grid shows it as a placeholder; anything that renders the same rows some other way - /// has to agree, or the marker leaks out as literal text. - static let defaultMarkerText = "__DEFAULT__" + // A cell left to the column's default is the SQL keyword, marked as SQL beside the value like any expression. The + // mark decides, never the text: a stored or typed value can read the same. + static let columnDefaultKeyword = "DEFAULT" + static let columnDefault = PluginCellValue.text(columnDefaultKeyword) - var isDefaultMarker: Bool { - guard case .text(let value) = self else { return false } - return value == Self.defaultMarkerText + // The spelling driver plugins read as the default in the rows the host hands them. + static let pluginDefaultMarker = PluginCellValue.text("__DEFAULT__") + + func isColumnDefault(isExpression: Bool) -> Bool { + isExpression && self == Self.columnDefault } } diff --git a/apple/TablePro/Models/ClickHouse/ClickHousePartInfo.swift b/apple/TablePro/Models/ClickHouse/ClickHousePartInfo.swift index baa347a335..afe2544e04 100644 --- a/apple/TablePro/Models/ClickHouse/ClickHousePartInfo.swift +++ b/apple/TablePro/Models/ClickHouse/ClickHousePartInfo.swift @@ -2,15 +2,13 @@ // ClickHousePartInfo.swift // TablePro // -// Model for ClickHouse partition/part information from system.parts. -// import Foundation -/// Represents a single part from the ClickHouse system.parts table struct ClickHousePartInfo: Identifiable { let id = UUID() let partition: String + let partitionId: String let name: String let rows: UInt64 let bytesOnDisk: UInt64 diff --git a/apple/TablePro/Models/Query/SelectionSummary.swift b/apple/TablePro/Models/Query/SelectionSummary.swift index af5da02cb1..923812aa44 100644 --- a/apple/TablePro/Models/Query/SelectionSummary.swift +++ b/apple/TablePro/Models/Query/SelectionSummary.swift @@ -32,8 +32,8 @@ struct SelectionSummaryInput: Sendable { let dataColumnsByDisplayPosition: [Int] let policy: SelectionSummaryColumnPolicy let deletedRowIDs: Set - let insertedRowIDs: Set - let modifiedCells: [RowID: Set] + // The cells a pending edit set to the column's default, which hold the keyword as text. + let defaultedCells: [RowID: Set] init( selection: GridSelection, @@ -42,8 +42,7 @@ struct SelectionSummaryInput: Sendable { dataColumnsByDisplayPosition: [Int], policy: SelectionSummaryColumnPolicy, deletedRowIDs: Set = [], - insertedRowIDs: Set = [], - modifiedCells: [RowID: Set] = [:] + defaultedCells: [RowID: Set] = [:] ) { self.selection = selection self.tableRows = tableRows @@ -51,8 +50,7 @@ struct SelectionSummaryInput: Sendable { self.dataColumnsByDisplayPosition = dataColumnsByDisplayPosition self.policy = policy self.deletedRowIDs = deletedRowIDs - self.insertedRowIDs = insertedRowIDs - self.modifiedCells = modifiedCells + self.defaultedCells = defaultedCells } } diff --git a/apple/TablePro/Models/Schema/ClickHousePartStatements.swift b/apple/TablePro/Models/Schema/ClickHousePartStatements.swift index 5436cfced7..c11617c1c6 100644 --- a/apple/TablePro/Models/Schema/ClickHousePartStatements.swift +++ b/apple/TablePro/Models/Schema/ClickHousePartStatements.swift @@ -2,15 +2,10 @@ // ClickHousePartStatements.swift // TablePro // -// The statements the Parts tab issues, built from the tab's own container. -// import Foundation -/// ClickHouse carries the database as a request parameter rather than in the session, and the app -/// moves that parameter whenever any tab runs somewhere else. An unqualified name therefore names -/// whichever database the connection was last pinned to, which for `DROP PARTITION` is data loss in -/// a database the user was not looking at. +// ClickHouse takes the database from a request parameter any tab can move, so every statement names its own. internal enum ClickHousePartStatements { internal static func qualifiedName( database: String, @@ -32,42 +27,49 @@ internal enum ClickHousePartStatements { internal static func dropPartition( database: String, table: String, - partition: String, - quote: (String) -> String, - escape: (String) -> String - ) -> String { + partitionId: String, + quote: (String) -> String + ) -> String? { + guard isValidPartitionId(partitionId) else { return nil } let name = qualifiedName(database: database, table: table, quote: quote) - return "ALTER TABLE \(name) DROP PARTITION '\(escape(partition))'" + return "ALTER TABLE \(name) DROP PARTITION ID '\(partitionId)'" } internal static func detachPartition( database: String, table: String, - partition: String, - quote: (String) -> String, - escape: (String) -> String - ) -> String { + partitionId: String, + quote: (String) -> String + ) -> String? { + guard isValidPartitionId(partitionId) else { return nil } let name = qualifiedName(database: database, table: table, quote: quote) - return "ALTER TABLE \(name) DETACH PARTITION '\(escape(partition))'" + return "ALTER TABLE \(name) DETACH PARTITION ID '\(partitionId)'" + } + + // The server's own rule (MergeTreePartInfo::validatePartitionID); it also keeps quotes out of the literal. + internal static func isValidPartitionId(_ id: String) -> Bool { + !id.isEmpty && id.unicodeScalars.allSatisfy { scalar in + switch scalar { + case "0"..."9", "a"..."z", "A"..."Z", "-": true + default: false + } + } } - /// A connection saved with no database of its own has none to name, and ClickHouse then resolves - /// an unqualified request against its own configured default, which `currentDatabase()` reports. - /// Comparing against an empty string instead matches nothing and empties the tab. + // With no database of its own, ClickHouse resolves a request against its configured default. private static func databasePredicate(_ database: String, escape: (String) -> String) -> String { guard !database.isEmpty else { return "currentDatabase()" } return "'\(escape(database))'" } - /// Filtered on the named database rather than `currentDatabase()`, which answers with the - /// request parameter and so listed another database's parts under this table's name. + // `partition` renders a tuple key as `(202401,'eu')` and no key as `tuple()`, so statements name `partition_id`. internal static func parts( database: String, table: String, escape: (String) -> String ) -> String { """ - SELECT partition, name, rows, bytes_on_disk, + SELECT partition, partition_id, name, rows, bytes_on_disk, toString(modification_time) AS mod_time, active FROM system.parts WHERE database = \(databasePredicate(database, escape: escape)) AND table = '\(escape(table))' diff --git a/apple/TablePro/Models/UI/MultiRowEditState.swift b/apple/TablePro/Models/UI/MultiRowEditState.swift index 26c98ce2f2..a9c0f4ae65 100644 --- a/apple/TablePro/Models/UI/MultiRowEditState.swift +++ b/apple/TablePro/Models/UI/MultiRowEditState.swift @@ -70,7 +70,7 @@ struct FieldEditState: Identifiable { var effectiveValue: String? { if isPendingDefault { - return "__DEFAULT__" + return PluginCellValue.columnDefaultKeyword } else if isPendingNull { return nil } else { @@ -244,6 +244,8 @@ final class MultiRowEditState: ObservableObject { hasCommittedEdit = true } else if originalValue == nil { isPendingNull = true + } else if expression.first == true, originalValue == PluginCellValue.columnDefaultKeyword { + isPendingDefault = true } else { pendingValue = originalValue isPendingExpression = expression.first == true @@ -508,7 +510,7 @@ final class MultiRowEditState: ObservableObject { guard index < fields.count else { return } resetPending(at: index) fields[index].isPendingDefault = true - onFieldChanged?(index, .text("__DEFAULT__"), .discrete) + onFieldExpressionChanged?(index, PluginCellValue.columnDefaultKeyword, .discrete) } func setFieldToExpression(at index: Int, expression: CellExpression) { @@ -619,7 +621,7 @@ final class MultiRowEditState: ObservableObject { columnName: field.columnName, newValue: field.effectiveValue, removesField: field.isPendingRemoval, - isExpression: field.isPendingExpression + isExpression: field.isPendingExpression || field.isPendingDefault ) } } diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 4ba0fefbde..245084252f 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -55039,6 +55039,9 @@ } } } + }, + "Couldn't Detach Partition" : { + }, "Couldn't Discard Changes" : { "localizations" : { @@ -55052,6 +55055,9 @@ }, "Couldn't download cloud-sql-proxy. The server answered with HTTP status %lld." : { + }, + "Couldn't Drop Partition" : { + }, "Couldn't Duplicate the Theme" : { @@ -55119,6 +55125,9 @@ }, "Couldn't Move the Theme to the Trash" : { + }, + "Couldn't Optimize Table" : { + }, "Couldn't publish to the team catalog" : { "localizations" : { @@ -55254,6 +55263,9 @@ }, "Couldn't Read the DDL" : { + }, + "Couldn't Read the Parts" : { + }, "Couldn't read the referenced table" : { "localizations" : { @@ -120449,6 +120461,10 @@ } } }, + "No row has this primary key, so nothing was updated. Another client may have deleted it." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "No row holds the values it was loaded with. Another client may have changed or deleted it." : { }, @@ -145185,6 +145201,12 @@ "Reconnecting (%lld of %lld)" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Reconnecting could not reopen the database %1$@: %2$@" : { + + }, + "Reconnecting could not reopen the schema %1$@: %2$@" : { + }, "Reconnecting failed after %d attempts." : { "localizations" : { @@ -174217,6 +174239,9 @@ } } } + }, + "Startup commands failed, so this session runs without them:\n%@" : { + }, "Startup SQL" : { "comment" : "A section header for the startup SQL commands of a connection.", @@ -193166,6 +193191,9 @@ } } } + }, + "The server reported a partition ID that cannot be used in a statement, so nothing was run." : { + }, "The server reported an error." : { "localizations" : { @@ -194368,6 +194396,12 @@ } } } + }, + "The startup command %@ failed, so this session runs without it." : { + + }, + "The startup command %1$@ failed (%2$@), so this session runs without it." : { + }, "The statement before it stays applied." : { "localizations" : { @@ -198452,6 +198486,9 @@ }, "This database's driver cannot write SQL into %@. Update the driver." : { + }, + "This database's driver may read the text __DEFAULT__ in %@ as the column default, so it cannot be saved as text." : { + }, "This deletes only the queries listed here, across every connection. Queries the source, date, outcome and search filters are hiding stay. You cannot undo this." : { "comment" : "Message for the \"Delete this connection's query history\" button.", @@ -213159,6 +213196,10 @@ } } }, + "Values a spreadsheet would read as formulas, written with a leading single quote: %lld. Turn off Sanitize formula-like values to export them unchanged." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Values from %d loaded rows" : { "extractionState" : "stale", "localizations" : { diff --git a/apple/TablePro/ViewModels/SchemaEditorViewModel.swift b/apple/TablePro/ViewModels/SchemaEditorViewModel.swift index 3d86e3b1fe..5c20599902 100644 --- a/apple/TablePro/ViewModels/SchemaEditorViewModel.swift +++ b/apple/TablePro/ViewModels/SchemaEditorViewModel.swift @@ -330,7 +330,7 @@ final class SchemaEditorViewModel: ObservableObject { renameCommitted: plan.renamesFirst && committedFirst ) } catch { - failure = error.localizedDescription + failure = GatedOperationFailure(error).message return ApplyOutcome( succeeded: false, committedName: originalName ?? "", renameCommitted: false ) diff --git a/apple/TablePro/ViewModels/UserTypeEditorModel.swift b/apple/TablePro/ViewModels/UserTypeEditorModel.swift index 3d6bc764f8..ae428d77f5 100644 --- a/apple/TablePro/ViewModels/UserTypeEditorModel.swift +++ b/apple/TablePro/ViewModels/UserTypeEditorModel.swift @@ -302,7 +302,7 @@ final class UserTypeEditorModel: ObservableObject { do { outcome = try await UserTypeChangeRunner.run(plan, in: scope, connection: connection, gate: gate) } catch { - let failure = StructureApplyFailure(error) + let failure = GatedOperationFailure(error) if let message = failure.message { AlertHelper.showErrorSheet( title: String(localized: "Error Applying Changes"), diff --git a/apple/TablePro/ViewModels/UsersRolesViewModel+Changes.swift b/apple/TablePro/ViewModels/UsersRolesViewModel+Changes.swift index d54a6fc8bc..162da203eb 100644 --- a/apple/TablePro/ViewModels/UsersRolesViewModel+Changes.swift +++ b/apple/TablePro/ViewModels/UsersRolesViewModel+Changes.swift @@ -290,7 +290,7 @@ extension UsersRolesViewModel { .joined(separator: "\n") await load(forceReload: true) } catch { - applyFailure = error.localizedDescription + applyFailure = GatedOperationFailure(error).message } } diff --git a/apple/TablePro/Views/Compare/CompareRowDiffPane.swift b/apple/TablePro/Views/Compare/CompareRowDiffPane.swift index 542c2d52e9..f80cb6f1c3 100644 --- a/apple/TablePro/Views/Compare/CompareRowDiffPane.swift +++ b/apple/TablePro/Views/Compare/CompareRowDiffPane.swift @@ -208,12 +208,27 @@ internal struct CompareRowDiffPane: View { ) } + // Named by side, because the two tables usually share a name and only one database cut its stream. + private func rowCutNotices(for summary: DataDiffSummary, plan: DataComparePlan) -> [String] { + let sides: [(endpoint: DatabaseEndpoint?, cut: Int?)] = [ + (session.source, summary.sourceRowCut), (session.target, summary.targetRowCut) + ] + return sides.compactMap { side in + guard let cut = side.cut else { return nil } + let table = ([side.endpoint?.qualifiedDescription].compactMap { $0 } + [plan.table]).joined(separator: " / ") + return StreamRowCutWarning.text(table: table, limit: cut) + } + } + @ViewBuilder private func notices(for plan: DataComparePlan) -> some View { if let failure = plan.comparisonFailure { notice(failure, systemImage: "exclamationmark.octagon.fill") } if let summary = plan.summary { + ForEach(rowCutNotices(for: summary, plan: plan), id: \.self) { text in + notice(text, systemImage: "exclamationmark.triangle.fill") + } if summary.stoppedAtRowLimit { notice(rowLimitNotice(for: summary), systemImage: "info.circle.fill") } diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift index b32be5c107..1f918e38b5 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift @@ -23,16 +23,17 @@ struct DataGridCellContent { static func placeholder( for rawValue: PluginCellValue, isAbsent: Bool = false, + isExpression: Bool = false, unloadedByteCount: Int? = nil, marksEmptyText: Bool = true ) -> DataGridCellPlaceholder? { if let unloadedByteCount { return .unloaded(byteCount: unloadedByteCount) } guard !isAbsent else { return .absent } + if rawValue.isColumnDefault(isExpression: isExpression) { return .defaultMarker } switch rawValue { case .null: return .null case .text(let value): - if value == PluginCellValue.defaultMarkerText { return .defaultMarker } return value.isEmpty && marksEmptyText ? .empty : nil case .bytes: return nil diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift index 8f7745aac0..651da4002b 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift @@ -250,6 +250,7 @@ final class DataGridCellPresenter { placeholder: DataGridCellContent.placeholder( for: rawValue, isAbsent: row.isAbsent(dataIndex), + isExpression: visualState.isExpression(columnIndex: dataIndex), unloadedByteCount: row.unloadedByteCount(dataIndex), marksEmptyText: pass.marksEmptyText ) @@ -297,6 +298,7 @@ final class DataGridCellPresenter { switch DataGridCellContent.placeholder( for: rawValue, isAbsent: row.isAbsent(dataIndex), + isExpression: model.changeState?.isExpression(rowID: row.id, columnIndex: dataIndex) ?? false, unloadedByteCount: row.unloadedByteCount(dataIndex), marksEmptyText: model.configuration.marksEmptyText ) { diff --git a/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift b/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift index e27584da1e..fd99df077b 100644 --- a/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift +++ b/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift @@ -133,6 +133,7 @@ extension DataGridCommands { tableRows: model.rows, displayIDs: model.order.filteredIDs, selectedDisplayIndices: displayIndices, + defaultedCells: model.changeState?.defaultedCells(in: model.rows) ?? [:], columns: projection ) guard output.rowCount > 0 else { return } diff --git a/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+SetValue.swift b/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+SetValue.swift index cc2d571948..cdfa6e7fa8 100644 --- a/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+SetValue.swift +++ b/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+SetValue.swift @@ -78,7 +78,7 @@ extension DataGridCommands { private static func sampleText(ofColumn dataIndex: Int, in rows: TableRows) -> String? { for row in rows.rows where dataIndex < row.values.count { guard let text = row.values[dataIndex].asText, - !text.isEmpty, text != PluginCellValue.defaultMarkerText else { continue } + !text.isEmpty, row.values[dataIndex] != .columnDefault else { continue } return text } return nil @@ -91,7 +91,7 @@ extension DataGridCommands { case .null: return (value: .null, isExpression: false) case .defaultValue: - return (value: .text(PluginCellValue.defaultMarkerText), isExpression: false) + return (value: .columnDefault, isExpression: true) case .expression(let expression): return (value: .text(expression.sql), isExpression: true) case .uuid(let version): diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController.swift b/apple/TablePro/Views/DataGrid/DataGridViewController.swift index dd7176f7af..a3b65c5666 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController.swift @@ -388,8 +388,7 @@ final class DataGridViewController: NSViewController, DataGridMountedView { dataColumnsByDisplayPosition: model.columns.columns.map(\.dataIndex), policy: policy, deletedRowIDs: changeState?.deletedRowIDs ?? [], - insertedRowIDs: changeState?.insertedRowIDs ?? [], - modifiedCells: changeState?.modifiedCells ?? [:] + defaultedCells: changeState?.defaultedCells(in: rows) ?? [:] ) } } diff --git a/apple/TablePro/Views/DatabaseSwitcher/CreateDatabaseSheet.swift b/apple/TablePro/Views/DatabaseSwitcher/CreateDatabaseSheet.swift index 7cdc8a564d..1b6314b1ba 100644 --- a/apple/TablePro/Views/DatabaseSwitcher/CreateDatabaseSheet.swift +++ b/apple/TablePro/Views/DatabaseSwitcher/CreateDatabaseSheet.swift @@ -176,7 +176,7 @@ struct CreateDatabaseSheet: View { onCreated?(name) dismiss() } catch { - errorMessage = error.localizedDescription + errorMessage = GatedOperationFailure(error).message isCreating = false } } diff --git a/apple/TablePro/Views/Editor/StatementRunController.swift b/apple/TablePro/Views/Editor/StatementRunController.swift index 7909dcd81a..b7b0acef3c 100644 --- a/apple/TablePro/Views/Editor/StatementRunController.swift +++ b/apple/TablePro/Views/Editor/StatementRunController.swift @@ -17,7 +17,8 @@ enum StatementNavigationDirection { // The gutter's run controls and the caret's statement band both come from the scanner execution uses, so a control // never offers to run something other than what it is drawn beside. The band runs on every caret move, so a SQL -// document resolves it through `SQLStatementCursorIndex`, which rescans only from the last boundary before the caret. +// document resolves it through `SQLStatementCursorIndex`, which rescans only from the last boundary before the caret, +// and a JavaScript or Redis document through a `QueryStatementCaretTable` read once per text. // The controls need every statement, a full pass of 31ms at the size limit, so that refresh is debounced. @MainActor final class StatementRunController { @@ -44,11 +45,17 @@ final class StatementRunController { /// How the document divides into statements. JavaScript engines do not end a statement at every /// semicolon, so the gutter controls and the caret band have to ask the same splitter execution does. var statementModel: QueryStatementModel = .sql { - didSet { if statementModel != oldValue { caretIndex = nil } } + didSet { + if statementModel != oldValue { + caretIndex = nil + caretTable = nil + } + } } - // The band asks on every caret move, so a SQL document keeps its statement boundaries between edits. + // The band asks on every caret move, so a document keeps its statement boundaries between edits. private var caretIndex: SQLStatementCursorIndex? + private var caretTable: QueryStatementCaretTable? private weak var caretIndexStorage: NSTextStorage? private var caretIndexEdits: StorageEditObservation? @@ -56,6 +63,8 @@ final class StatementRunController { caretIndex?.unitsScanned ?? 0 } + private(set) var caretTableReads = 0 + /// Called with the SQL the pressed control stands for, read from the document at the moment of the press, and /// where that SQL starts in the document. /// @@ -213,6 +222,8 @@ final class StatementRunController { pendingRefresh?.cancel() pendingRefresh = nil caretIndex = nil + caretTable = nil + caretIndexStorage = nil caretIndexEdits = nil controller?.runnableStatements = [] controller?.highlightedStatementRange = nil @@ -267,7 +278,7 @@ final class StatementRunController { guard (text as NSString).length <= sizeLimit else { return nil } let statement: SQLStatementScanner.LocatedStatement - if statementModel == .sql, let storage = textView.textStorage { + if let storage = textView.textStorage { statement = indexedStatement(at: selection.range.location, in: text, storage: storage) } else { statement = QueryStatementScanner.locatedStatementAtCursor( @@ -286,19 +297,38 @@ final class StatementRunController { in text: String, storage: NSTextStorage ) -> SQLStatementScanner.LocatedStatement { - if caretIndex == nil || caretIndexStorage !== storage { - startCaretIndex(on: storage) + if caretIndexStorage !== storage { + observeEdits(on: storage) } // Inside an editing batch the text has changed but the storage has not posted its notification yet. if storage.editedMask.contains(.editedCharacters) { - caretIndex?.textDidChange(at: storage.editedRange.location) + textDidChange(at: storage.editedRange.location) + } + guard statementModel == .sql else { + if caretTable == nil { + caretTable = QueryStatementCaretTable(text: text, model: statementModel) + caretTableReads += 1 + } + return caretTable?.statement(atCursor: cursor) + ?? QueryStatementScanner.locatedStatementAtCursor( + in: text, cursorPosition: cursor, model: statementModel, grammar: grammar + ) + } + if caretIndex == nil { + caretIndex = SQLStatementCursorIndex(grammar: grammar) } return caretIndex?.statementAtCursor(cursor, in: text) ?? SQLStatementScanner.locatedStatementAtCursor(in: text, cursorPosition: cursor, grammar: grammar) } - private func startCaretIndex(on storage: NSTextStorage) { - caretIndex = SQLStatementCursorIndex(grammar: grammar) + private func textDidChange(at location: Int) { + caretIndex?.textDidChange(at: location) + caretTable = nil + } + + private func observeEdits(on storage: NSTextStorage) { + caretIndex = nil + caretTable = nil caretIndexStorage = storage // Every way the text changes passes through the storage, including the ones that skip the editor's delegate. let token = NotificationCenter.default.addObserver( @@ -313,7 +343,7 @@ final class StatementRunController { private func caretIndexStorageDidProcessEditing() { guard let storage = caretIndexStorage, storage.editedMask.contains(.editedCharacters) else { return } - caretIndex?.textDidChange(at: storage.editedRange.location) + textDidChange(at: storage.editedRange.location) } } diff --git a/apple/TablePro/Views/Main/Child/DataTabSQLExporter.swift b/apple/TablePro/Views/Main/Child/DataTabSQLExporter.swift index f9f0e6a53f..7e4e542b48 100644 --- a/apple/TablePro/Views/Main/Child/DataTabSQLExporter.swift +++ b/apple/TablePro/Views/Main/Child/DataTabSQLExporter.swift @@ -155,14 +155,10 @@ final class DataTabSQLExporter: DataGridSQLExporting { ) } - // Every marker in a row not saved yet, and in a saved row the cells a pending edit set to Default. + // The cells a pending insert or edit marked as the default. private func stagedDefaults(of row: Row, target: Target) -> Set { - let marker = PluginCellValue.text("__DEFAULT__") - if target.changeManager.insertedRowIDs.contains(row.id) { - return Set(row.values.indices.filter { row.values[$0] == marker }) - } - let pendingUpdate = target.changeManager.pendingUpdate(forRow: row.id) - return Set(pendingUpdate?.cellChanges.filter { $0.newValue == marker }.map(\.columnIndex) ?? []) + let marked = target.changeManager.expressionColumns(forRow: row.id) + return Set(marked.filter { row.values.indices.contains($0) && row.values[$0] == .columnDefault }) } private func nonEmpty(_ statements: String) -> String? { diff --git a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift index 81b48e98af..a246fc2a70 100644 --- a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -734,6 +734,7 @@ struct MainEditorContentView: View { selectedRowIDs: Set(model.selectedRowIDs), displayIDs: model.displayedRowIDs, deletedRowIDs: changeManager.deletedRowIDs, + defaultedCells: changeManager.defaultedCells(in: rows), valueFilter: tab.valueFilter, dataRevision: coordinator.tabSessionRegistry.session(for: tab.id)?.dataRevision ?? 0, displayRevision: coordinator.gridDisplayRevision, diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Navigation.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Navigation.swift index d7eab33615..ecd1426d4c 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Navigation.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Navigation.swift @@ -677,10 +677,13 @@ extension MainContentCoordinator { try await dropContainer(target) services.catalogChangeService.record(.containerDropped(target, connectionId: connectionId)) } catch { - navigationLogger.error( - "Failed to drop \(target.id, privacy: .public): \(error.publicLogShape, privacy: .public)" - ) - failures.append((target.name, error.localizedDescription)) + let failure = GatedOperationFailure(error) + if failure.reportsFailure { + navigationLogger.error( + "Failed to drop \(target.id, privacy: .public): \(error.publicLogShape, privacy: .public)" + ) + } + if let message = failure.message { failures.append((target.name, message)) } } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SafeModePromptRetry.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SafeModePromptRetry.swift new file mode 100644 index 0000000000..92846198f7 --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SafeModePromptRetry.swift @@ -0,0 +1,28 @@ +// +// MainContentCoordinator+SafeModePromptRetry.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + // The prompt is one per window, so a first load that meets another run's prompt waits for it to close. + func turnAwayTableLoad(tabId: UUID, trigger: TableLoadTrigger) { + traceNavigationAbandoned(tabId: tabId, outcome: .safeModePromptAlreadyOpen) + tableLoadTurnedAwayByPrompt = ParkedTableLoad(tabId: tabId, trigger: trigger) + declineTableLoad(for: tabId) + } + + func safeModePromptDidChange(wasShowing: Bool) { + guard wasShowing, !isShowingSafeModePrompt, tableLoadTurnedAwayByPrompt != nil else { return } + // Once the run that lowered it has started, so a load it starts itself is not asked for twice. + Task { @MainActor [weak self] in self?.retryTableLoadTurnedAwayByPrompt() } + } + + private func retryTableLoadTurnedAwayByPrompt() { + guard !isShowingSafeModePrompt, !isTearingDown, let turnedAway = tableLoadTurnedAwayByPrompt else { return } + tableLoadTurnedAwayByPrompt = nil + guard turnedAway.tabId == tabManager.selectedTabId else { return } + lazyLoadCurrentTabIfNeeded(trigger: turnedAway.trigger) + } +} diff --git a/apple/TablePro/Views/Main/MainContentCommandActions.swift b/apple/TablePro/Views/Main/MainContentCommandActions.swift index ec02fa09d7..0decc6483f 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions.swift @@ -66,6 +66,9 @@ final class MainContentCommandActions: ObservableObject { await RunningWorkApproval.confirm(prompt, window: window) } + // What a window close waits on while another connection's pane mounts. + var paneWaitClock: any Clock = ContinuousClock() + private var notificationTasks: [Task] = [] private var eventCancellables: Set = [] @@ -556,12 +559,7 @@ final class MainContentCommandActions: ObservableObject { // Every connection in the window: closing it closes them all, and skipping a background one is silent data loss. internal var hasUnsavedWorkInWindow: Bool { - guard let host = window?.contentViewController as? MainSplitViewController else { - return coordinator?.hasAnyUnsavedWork() ?? false - } - return host.workspaces.workspaces.contains { workspace in - workspace.sessionState?.coordinator.hasAnyUnsavedWork() == true - } + coordinatorsInWindow.contains { $0.hasAnyUnsavedWork() } } // This connection only: closing its tabs leaves another connection's pending work alone. @@ -650,13 +648,16 @@ final class MainContentCommandActions: ObservableObject { return .closed } // Work is stopped only once the close is certain: a cancelled or failed save keeps it running. - let result = await AlertHelper.confirmSaveChanges( - message: RunningWorkPrompt.saveMessage( - String(localized: "Your changes will be lost if you don't save them."), + let keepsTabsOpen = coordinatorsInWindow.contains { hosted in + hosted.tabManager.tabs.contains { hosted.savability(of: $0) == .mountedOnly } + } + let result = await confirmSaveChanges( + RunningWorkPrompt.saveMessage( + Self.unsavedWorkMessage(keepsTabsOpen: keepsTabsOpen), action: .close, alsoStopping: work ), - window: closeAnchorWindow + closeAnchorWindow ) switch result { @@ -775,13 +776,17 @@ final class MainContentCommandActions: ObservableObject { return true } + // The prompt asked about every connection in the window, so Save reaches every tab of each; one it cannot save keeps the window. private func saveAndClose(asBatchSurvivor: Bool?) async -> Bool { - guard let coordinator else { - finish(asBatchSurvivor: asBatchSurvivor) - return true + let host = window?.contentViewController as? MainSplitViewController + let shown = host?.workspaces.selectedConnectionId + for hosted in coordinatorsInWindow where hosted === coordinator || hosted.hasAnyUnsavedWork() { + // A save can raise a sheet, which has to sit over the connection it is about. + host?.workspaces.select(hosted.connectionId) + guard let actions = await shownActions(of: hosted) else { return false } + guard await actions.saveEveryTabsWork() else { return false } } - guard await applyStagedStructureEdits(in: coordinator.tabManager.tabs) else { return false } - guard await saveSelectedTabWork() else { return false } + if let shown { host?.workspaces.select(shown) } finish(asBatchSurvivor: asBatchSurvivor) return true } @@ -1407,6 +1412,38 @@ final class MainContentCommandActions: ObservableObject { } extension MainContentCommandActions { + // This connection's first. + private var coordinatorsInWindow: [MainContentCoordinator] { + guard let host = window?.contentViewController as? MainSplitViewController else { + return [coordinator].compactMap { $0 } + } + let hosted = host.workspaces.workspaces.compactMap { $0.sessionState?.coordinator } + guard let coordinator else { return hosted } + return [coordinator] + hosted.filter { $0 !== coordinator } + } + + // A connection never shown has no actions until its pane mounts, a few run loop turns after it is selected. + private func shownActions(of hosted: MainContentCoordinator) async -> MainContentCommandActions? { + if hosted === coordinator { return self } + return await Self.actions(of: hosted, waitingOn: paneWaitClock) + } + + // A cancelled close stops waiting at once rather than polling out the rest of the deadline. + private static func actions>( + of hosted: MainContentCoordinator, + waitingOn clock: C + ) async -> MainContentCommandActions? { + let deadline = clock.now.advanced(by: .seconds(2)) + while hosted.commandActions == nil, clock.now < deadline { + do { + try await clock.sleep(for: .milliseconds(20)) + } catch { + return nil + } + } + return hosted.commandActions + } + // Copy with the grid not focused takes the selected rows in the row copy format; a focused grid answers `copy:` itself. func copySelectedRowsInCopyFormat() { guard dataGridOwnsSelection, let model = coordinator?.activeGridModel else { diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index b338583e42..2c2c39815a 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -371,7 +371,12 @@ final class MainContentCoordinator: ObservableObject { // Guards re-entrant confirm dialogs (nested run loop during runModal). internal var isShowingConfirmAlert = false - internal var isShowingSafeModePrompt = false + // Lowered by whichever run raised it, so a table load it turned away is retried whatever path asked. + internal var isShowingSafeModePrompt = false { + didSet { safeModePromptDidChange(wasShowing: oldValue) } + } + + var tableLoadTurnedAwayByPrompt: ParkedTableLoad? @Published internal var rewindPlan: RewindPlan? @@ -1110,7 +1115,7 @@ final class MainContentCoordinator: ObservableObject { tab.execution.lastExecutedAt == nil { guard !isShowingSafeModePrompt else { - traceNavigationAbandoned(tabId: tab.id, outcome: .safeModePromptAlreadyOpen) + turnAwayTableLoad(tabId: tab.id, trigger: trigger) return nil } isShowingSafeModePrompt = true diff --git a/apple/TablePro/Views/ObjectCopy/CopyObjectsResultView.swift b/apple/TablePro/Views/ObjectCopy/CopyObjectsResultView.swift index 3bd997547b..38c7cf6698 100644 --- a/apple/TablePro/Views/ObjectCopy/CopyObjectsResultView.swift +++ b/apple/TablePro/Views/ObjectCopy/CopyObjectsResultView.swift @@ -41,6 +41,11 @@ internal struct CopyObjectsResultView: View { .font(.callout) .foregroundStyle(.secondary) } + ForEach(Array(result.warnings.enumerated()), id: \.offset) { _, warning in + NoticeLabel(warning, severity: .warning) + .font(.callout) + .textSelection(.enabled) + } outcomes(result) } .frame(maxWidth: .infinity, alignment: .leading) diff --git a/apple/TablePro/Views/ObjectSource/UserType/CreateUserTypeSheet.swift b/apple/TablePro/Views/ObjectSource/UserType/CreateUserTypeSheet.swift index 31526e51b4..8c0baa020c 100644 --- a/apple/TablePro/Views/ObjectSource/UserType/CreateUserTypeSheet.swift +++ b/apple/TablePro/Views/ObjectSource/UserType/CreateUserTypeSheet.swift @@ -245,11 +245,12 @@ struct CreateUserTypeSheet: View { try await UserTypeCreation.create(definition, in: scope, connection: connection) dismiss() onCreated(definition) - } catch ExecutionGateError.cancelledByUser { - return } catch { - Self.logger.error("Create Type failed: \(error.publicLogShape, privacy: .public)") - failure = error.localizedDescription + let outcome = GatedOperationFailure(error) + if outcome.reportsFailure { + Self.logger.error("Create Type failed: \(error.publicLogShape, privacy: .public)") + } + failure = outcome.message } } } diff --git a/apple/TablePro/Views/Results/ResultsJsonView.swift b/apple/TablePro/Views/Results/ResultsJsonView.swift index d8cc4d3395..6a04df3448 100644 --- a/apple/TablePro/Views/Results/ResultsJsonView.swift +++ b/apple/TablePro/Views/Results/ResultsJsonView.swift @@ -11,6 +11,7 @@ internal struct ResultsJsonView: View { let selectedRowIDs: Set let displayIDs: [RowID]? let deletedRowIDs: Set + let defaultedCells: [RowID: Set] let valueFilter: GridValueFilterState let dataRevision: Int let displayRevision: Int @@ -32,6 +33,7 @@ internal struct ResultsJsonView: View { selectedRowIDs: Set, displayIDs: [RowID]?, deletedRowIDs: Set, + defaultedCells: [RowID: Set] = [:], valueFilter: GridValueFilterState, dataRevision: Int, displayRevision: Int, @@ -41,6 +43,7 @@ internal struct ResultsJsonView: View { self.selectedRowIDs = selectedRowIDs self.displayIDs = displayIDs self.deletedRowIDs = deletedRowIDs + self.defaultedCells = defaultedCells self.valueFilter = valueFilter self.dataRevision = dataRevision self.displayRevision = displayRevision @@ -58,6 +61,7 @@ internal struct ResultsJsonView: View { let displayRevision: Int let selectedRowIDs: Set let deletedRowIDs: Set + let defaultedCells: [RowID: Set] let valueFilter: GridValueFilterState let hiddenColumns: Set let columnOrder: [String]? @@ -69,6 +73,7 @@ internal struct ResultsJsonView: View { displayRevision: displayRevision, selectedRowIDs: selectedRowIDs, deletedRowIDs: deletedRowIDs, + defaultedCells: defaultedCells, valueFilter: valueFilter, hiddenColumns: columnLayout.hiddenColumns, columnOrder: columnLayout.columnOrder @@ -202,6 +207,7 @@ internal struct ResultsJsonView: View { let ids = displayIDs let selectedIndices = Set(DataGridRowOrder(filteredIDs: ids, revision: 0).displayIndexes(of: selectedRowIDs, in: snapshot)) let deletedIDs = deletedRowIDs + let defaulted = defaultedCells let layout = columnLayout let result = await Task.detached(priority: .userInitiated) { @@ -210,6 +216,7 @@ internal struct ResultsJsonView: View { displayIDs: ids, selectedIndices: selectedIndices, deletedRowIDs: deletedIDs, + defaultedCells: defaulted, columnLayout: layout ) }.value @@ -245,6 +252,7 @@ internal struct ResultsJsonView: View { displayIDs: [RowID]?, selectedIndices: Set, deletedRowIDs: Set = [], + defaultedCells: [RowID: Set] = [:], columnLayout: ColumnLayoutState ) -> RenderedJson { let output = ResultJsonSerializer.serialize( @@ -252,6 +260,7 @@ internal struct ResultsJsonView: View { displayIDs: displayIDs, selectedDisplayIndices: selectedIndices, deletedRowIDs: deletedRowIDs, + defaultedCells: defaultedCells, columns: .fromColumnLayout(columnLayout, columns: tableRows.columns) ) return RenderedJson( diff --git a/apple/TablePro/Views/Sidebar/EditSequenceSheet.swift b/apple/TablePro/Views/Sidebar/EditSequenceSheet.swift index 11ae7629f4..c72f6f12ea 100644 --- a/apple/TablePro/Views/Sidebar/EditSequenceSheet.swift +++ b/apple/TablePro/Views/Sidebar/EditSequenceSheet.swift @@ -188,7 +188,7 @@ struct EditSequenceSheet: View { try await SequenceEditing.alter(details, to: settings, target: target, connection: connection) dismiss() } catch { - saveError = error.localizedDescription + saveError = GatedOperationFailure(error).message phase = .editing(details) } } diff --git a/apple/TablePro/Views/Sidebar/ObjectCommentSheet.swift b/apple/TablePro/Views/Sidebar/ObjectCommentSheet.swift index bdc4397a00..b73407bedf 100644 --- a/apple/TablePro/Views/Sidebar/ObjectCommentSheet.swift +++ b/apple/TablePro/Views/Sidebar/ObjectCommentSheet.swift @@ -144,10 +144,8 @@ struct ObjectCommentSheet: View { do { try await ObjectCommentEditing.setComment(draft.commentToSave, on: target, connection: connection) dismiss() - } catch ExecutionGateError.cancelledByUser { - phase = .editing } catch { - saveError = error.localizedDescription + saveError = GatedOperationFailure(error).message phase = .editing } } diff --git a/apple/TablePro/Views/Sidebar/SetSequenceValueSheet.swift b/apple/TablePro/Views/Sidebar/SetSequenceValueSheet.swift index e63e765d73..d970311881 100644 --- a/apple/TablePro/Views/Sidebar/SetSequenceValueSheet.swift +++ b/apple/TablePro/Views/Sidebar/SetSequenceValueSheet.swift @@ -176,7 +176,7 @@ struct SetSequenceValueSheet: View { try await SequenceEditing.setNextValue(nextValue, of: details, target: target, connection: connection) dismiss() } catch { - saveError = error.localizedDescription + saveError = GatedOperationFailure(error).message phase = .editing(details) } } diff --git a/apple/TablePro/Views/Structure/ClickHousePartsView.swift b/apple/TablePro/Views/Structure/ClickHousePartsView.swift index 6f37691102..f95d50739e 100644 --- a/apple/TablePro/Views/Structure/ClickHousePartsView.swift +++ b/apple/TablePro/Views/Structure/ClickHousePartsView.swift @@ -8,6 +8,56 @@ import SwiftUI import TableProPluginKit import TableProSQLGrammar +internal struct ClickHousePartsAlert: Equatable { + let title: String + let message: String +} + +// A failed or refused action never replaces the list it acted on; only a load with nothing shown does. +internal struct ClickHousePartsPaneState { + internal enum Display: Equatable { + case loading + case loadFailed(String) + case empty + case parts + } + + private(set) var parts: [ClickHousePartInfo] = [] + private(set) var isLoading = true + private(set) var loadError: String? + var alert: ClickHousePartsAlert? + + var display: Display { + guard parts.isEmpty else { return .parts } + if let loadError { return .loadFailed(loadError) } + return isLoading ? .loading : .empty + } + + mutating func beginLoad() { + isLoading = true + loadError = nil + } + + mutating func finishLoad(_ parts: [ClickHousePartInfo]) { + self.parts = parts + isLoading = false + } + + mutating func failLoad(_ message: String) { + isLoading = false + guard parts.isEmpty else { + alert = ClickHousePartsAlert(title: String(localized: "Couldn't Read the Parts"), message: message) + return + } + loadError = message + } + + mutating func actionEnded(_ failure: GatedOperationFailure, title: String) { + guard let message = failure.message else { return } + alert = ClickHousePartsAlert(title: title, message: message) + } +} + struct ClickHousePartsView: View { private static let logger = Logger(subsystem: "com.TablePro", category: "ClickHousePartsView") @@ -17,31 +67,31 @@ struct ClickHousePartsView: View { let connection: DatabaseConnection let reloadToken: Int - @State private var parts: [ClickHousePartInfo] = [] - @State private var isLoading = true - @State private var errorMessage: String? + @State private var pane = ClickHousePartsPaneState() @State private var loadedToken: Int? @StateObject private var grid = StructureListGridModel(accessibilityLabel: StructurePane.parts.displayName) @StateObject private var selection = StructureListSelection() var body: some View { VStack(spacing: 0) { - if isLoading { + switch pane.display { + case .loading: ProgressView() .frame(maxWidth: .infinity, maxHeight: .infinity) .gridStandInBackground() - } else if let error = errorMessage { - VStack(spacing: 8) { - Image(systemName: "exclamationmark.triangle") - .font(.largeTitle) - .foregroundStyle(.orange) - .accessibilityHidden(true) - RevealedTextView(error) - .foregroundStyle(.secondary) + case .loadFailed(let message): + UnavailableStateView { + Label(String(localized: "Couldn't Read the Parts"), systemImage: "exclamationmark.triangle") + } description: { + RevealedTextView(message) + } actions: { + Button(String(localized: "Try Again")) { + Task { await loadParts() } + } } .frame(maxWidth: .infinity, maxHeight: .infinity) .gridStandInBackground() - } else if parts.isEmpty { + case .empty: VStack(spacing: 8) { Image(systemName: "tray") .font(.largeTitle) @@ -52,11 +102,20 @@ struct ClickHousePartsView: View { } .frame(maxWidth: .infinity, maxHeight: .infinity) .gridStandInBackground() - } else { + case .parts: partsToolbar partsTable } } + .alert( + pane.alert?.title ?? "", + isPresented: Binding(get: { pane.alert != nil }, set: { if !$0 { pane.alert = nil } }), + presenting: pane.alert + ) { _ in + Button(String(localized: "OK"), role: .cancel) {} + } message: { alert in + Text(verbatim: RevealedText(alert.message).plainText) + } .task(id: reloadToken) { await loadPartsIfNeeded() } } @@ -92,7 +151,7 @@ struct ClickHousePartsView: View { columns: Self.columnTitles, defaultWidths: Self.columnWidths, minimumWidths: Self.columnMinimumWidths, - rows: parts.map(Self.gridRow(for:)), + rows: pane.parts.map(Self.gridRow(for:)), databaseType: connection.type, selectedIDs: selection.ids, select: { selection.ids = $0 }, @@ -130,6 +189,20 @@ struct ClickHousePartsView: View { ) } + // Column order is ClickHousePartStatements.parts. + internal static func part(from row: [PluginCellValue]) -> ClickHousePartInfo? { + guard let name = row[safe: 2]?.asText else { return nil } + return ClickHousePartInfo( + partition: row[safe: 0]?.asText ?? "", + partitionId: row[safe: 1]?.asText ?? "", + name: name, + rows: row[safe: 3]?.asText.flatMap { UInt64($0) } ?? 0, + bytesOnDisk: row[safe: 4]?.asText.flatMap { UInt64($0) } ?? 0, + modificationTime: row[safe: 5]?.asText ?? "", + active: row[safe: 6]?.asText == "1" + ) + } + // MARK: - Actions private func optimizeTable() { @@ -140,19 +213,20 @@ struct ClickHousePartsView: View { database: scope.database, table: tableName, quote: driver.quoteIdentifier ), description: String(localized: "Optimize Table"), + failureTitle: String(localized: "Couldn't Optimize Table"), kind: .maintenance ) } } private func dropSelectedPartition() { - guard let partitionValue = selectedPartitionValue() else { return } + guard let part = selectedPart() else { return } Task { @MainActor in let confirmed = await AlertHelper.confirmDestructive( title: String(localized: "Drop Partition?"), message: String( format: String(localized: "This will permanently delete all data in partition '%@'."), - partitionValue + part.partition ), confirmButton: String(localized: "Drop"), cancelButton: String(localized: "Cancel") @@ -160,27 +234,31 @@ struct ClickHousePartsView: View { guard confirmed else { return } guard let driver = DatabaseManager.shared.driver(for: scope.connectionId) else { return } - let sql = ClickHousePartStatements.dropPartition( + let failureTitle = String(localized: "Couldn't Drop Partition") + guard let sql = ClickHousePartStatements.dropPartition( database: scope.database, table: tableName, - partition: partitionValue, - quote: driver.quoteIdentifier, - escape: driver.escapeStringLiteral - ) - guard await run(sql, description: String(localized: "Drop Partition"), kind: .destructiveQuery) - else { return } + partitionId: part.partitionId, + quote: driver.quoteIdentifier + ) else { + pane.alert = ClickHousePartsAlert(title: failureTitle, message: Self.unusablePartitionIdMessage) + return + } + guard await run( + sql, description: String(localized: "Drop Partition"), failureTitle: failureTitle, kind: .destructiveQuery + ) else { return } selection.ids.removeAll() } } private func detachSelectedPartition() { - guard let partitionValue = selectedPartitionValue() else { return } + guard let part = selectedPart() else { return } Task { @MainActor in let confirmed = await AlertHelper.confirmDestructive( title: String(localized: "Detach Partition?"), message: String( format: String(localized: "This will detach partition '%@'. Data will be preserved but inaccessible until re-attached."), - partitionValue + part.partition ), confirmButton: String(localized: "Detach"), cancelButton: String(localized: "Cancel") @@ -188,22 +266,30 @@ struct ClickHousePartsView: View { guard confirmed else { return } guard let driver = DatabaseManager.shared.driver(for: scope.connectionId) else { return } - let sql = ClickHousePartStatements.detachPartition( + let failureTitle = String(localized: "Couldn't Detach Partition") + guard let sql = ClickHousePartStatements.detachPartition( database: scope.database, table: tableName, - partition: partitionValue, - quote: driver.quoteIdentifier, - escape: driver.escapeStringLiteral - ) - guard await run(sql, description: String(localized: "Detach Partition"), kind: .destructiveQuery) - else { return } + partitionId: part.partitionId, + quote: driver.quoteIdentifier + ) else { + pane.alert = ClickHousePartsAlert(title: failureTitle, message: Self.unusablePartitionIdMessage) + return + } + guard await run( + sql, description: String(localized: "Detach Partition"), failureTitle: failureTitle, kind: .destructiveQuery + ) else { return } selection.ids.removeAll() } } + private static var unusablePartitionIdMessage: String { + String(localized: "The server reported a partition ID that cannot be used in a statement, so nothing was run.") + } + @MainActor @discardableResult - private func run(_ sql: String, description: String, kind: OperationKind) async -> Bool { + private func run(_ sql: String, description: String, failureTitle: String, kind: OperationKind) async -> Bool { let scope = scope do { let decision = await ExecutionGateProvider.shared.authorize( @@ -217,10 +303,7 @@ struct ClickHousePartsView: View { operationDescription: description ) ) - guard case .authorized = decision else { - errorMessage = decision.deniedReason - return false - } + if let denial = decision.denialError { throw denial } _ = try await DatabaseManager.shared.withScopedDriver( scope: scope, route: DatabaseManager.shared.schemaChangeRoute(for: scope), @@ -228,30 +311,30 @@ struct ClickHousePartsView: View { ) { driver in try await driver.execute(query: sql) } - if kind != .maintenance { - CatalogChangeService.post( - .changed(CatalogChange(connectionId: scope.connectionId, database: scope.database, kinds: .tables)) - ) - } - await loadParts() - return true } catch { - Self.logger.error("\(description, privacy: .public) failed: \(error.publicLogShape, privacy: .public)") - errorMessage = error.localizedDescription + let failure = GatedOperationFailure(error) + if failure.reportsFailure { + Self.logger.error("\(description, privacy: .public) failed: \(error.publicLogShape, privacy: .public)") + } + pane.actionEnded(failure, title: failureTitle) return false } + if kind != .maintenance { + CatalogChangeService.post( + .changed(CatalogChange(connectionId: scope.connectionId, database: scope.database, kinds: .tables)) + ) + } + await loadParts() + return true } - private func selectedPartitionValue() -> String? { - Self.partition(of: selection.ids, in: parts) + private func selectedPart() -> ClickHousePartInfo? { + Self.selectedPart(of: selection.ids, in: pane.parts) } - internal static func partition(of selectedIDs: Set, in parts: [ClickHousePartInfo]) -> String? { - guard selectedIDs.count == 1, - let selectedId = selectedIDs.first, - let part = parts.first(where: { $0.id == selectedId }) - else { return nil } - return part.partition + internal static func selectedPart(of selectedIDs: Set, in parts: [ClickHousePartInfo]) -> ClickHousePartInfo? { + guard selectedIDs.count == 1, let selectedId = selectedIDs.first else { return nil } + return parts.first { $0.id == selectedId } } // MARK: - Data Loading @@ -263,8 +346,7 @@ struct ClickHousePartsView: View { private func loadParts() async { let token = reloadToken - isLoading = true - errorMessage = nil + pane.beginLoad() let scope = scope let tableName = tableName @@ -276,29 +358,12 @@ struct ClickHousePartsView: View { } guard !Task.isCancelled else { return } selection.ids = [] - parts = result.rows.compactMap { row -> ClickHousePartInfo? in - guard let name = row[safe: 1]?.asText else { return nil } - let partition = row[safe: 0]?.asText ?? "" - let rows = row[safe: 2]?.asText.flatMap { UInt64($0) } ?? 0 - let bytesOnDisk = row[safe: 3]?.asText.flatMap { UInt64($0) } ?? 0 - let modTime = row[safe: 4]?.asText ?? "" - let active = row[safe: 5]?.asText == "1" - return ClickHousePartInfo( - partition: partition, - name: name, - rows: rows, - bytesOnDisk: bytesOnDisk, - modificationTime: modTime, - active: active - ) - } + pane.finishLoad(result.rows.compactMap(Self.part(from:))) loadedToken = token } catch { guard !Task.isCancelled else { return } Self.logger.error("Failed to load parts: \(error.publicLogShape, privacy: .public)") - errorMessage = error.localizedDescription + pane.failLoad(error.localizedDescription) } - - isLoading = false } } diff --git a/apple/TablePro/Views/Structure/CreateTableDraft.swift b/apple/TablePro/Views/Structure/CreateTableDraft.swift index bffd4f2653..854b5fea41 100644 --- a/apple/TablePro/Views/Structure/CreateTableDraft.swift +++ b/apple/TablePro/Views/Structure/CreateTableDraft.swift @@ -469,8 +469,7 @@ internal final class CreateTableDraft: ObservableObject { coordinator: coordinator ) } catch { - Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") - reportFailure(error.localizedDescription, on: coordinator) + reportFailure(error: error, on: coordinator) } } } @@ -508,8 +507,7 @@ internal final class CreateTableDraft: ObservableObject { } catch let formError as PluginCreateTableFormError { form?.recordSubmissionError(formError) } catch { - Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") - reportFailure(error.localizedDescription, on: coordinator) + reportFailure(error: error, on: coordinator) } } } @@ -560,6 +558,15 @@ internal final class CreateTableDraft: ObservableObject { ) } + private func reportFailure(error: any Error, on coordinator: MainContentCoordinator) { + let failure = GatedOperationFailure(error) + if failure.reportsFailure { + Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") + } + guard let message = failure.message else { return } + reportFailure(message, on: coordinator) + } + private func reportFailure(_ message: String, on coordinator: MainContentCoordinator) { AlertHelper.showErrorSheet( title: String(localized: "Create Table Failed"), diff --git a/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift b/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift index b760819838..15048571f6 100644 --- a/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift +++ b/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift @@ -31,52 +31,13 @@ internal enum StructureSaveOutcome: Equatable { } } -/// How a save that did not apply ends, read from the error alone so it can be tested without -/// presenting anything. -/// -/// The gate's sheet is the one confirmation a save gets, so Cancel there is the ordinary way to -/// back out, and it ends quietly the way closing any confirmation does. Answering it with an -/// "Error Applying Changes" sheet put a second dialog in front of the user for the choice they had -/// just made. -internal enum StructureApplyFailure: Equatable { - case cancelledByUser - case refused(String) - case failed(String) - - internal init(_ error: any Error) { - guard let gateError = error as? ExecutionGateError else { - self = .failed(error.localizedDescription) - return - } - switch gateError { - case .cancelledByUser: - self = .cancelledByUser - case .denied(let reason): - self = .refused(reason) - } - } - - internal var outcome: StructureSaveOutcome { +internal extension GatedOperationFailure { + var outcome: StructureSaveOutcome { switch self { case .cancelledByUser, .refused: .refused case .failed(let message): .failed(message) } } - - /// What the error sheet says. Nil for a Cancel, which shows nothing. - internal var message: String? { - switch self { - case .cancelledByUser: nil - case .refused(let reason), .failed(let reason): reason - } - } - - /// A refusal and a Cancel both stop at the gate, before the first statement, so neither is a - /// failed operation to report or a reason to reload the catalog. - internal var reportsFailure: Bool { - if case .failed = self { return true } - return false - } } internal extension StructureEditingSession { @@ -159,7 +120,7 @@ internal extension StructureEditingSession { // A failed save that may have written; its error waits for the table to be read again. private struct AlterFailure { - let failure: StructureApplyFailure + let failure: GatedOperationFailure let startedAt: ContinuousClock.Instant } @@ -240,7 +201,7 @@ internal extension StructureEditingSession { report(.succeeded(OperationSummary()), startedAt: operationStart, coordinator: coordinator) return (.applied, nil) } catch { - let failure = StructureApplyFailure(error) + let failure = GatedOperationFailure(error) // A gate refusal and a driver's refusal both stop before the first statement. guard failure.reportsFailure, !(error is SchemaOperationRefusedError) else { present(failure, startedAt: operationStart, coordinator: coordinator) @@ -251,7 +212,7 @@ internal extension StructureEditingSession { } private func present( - _ failure: StructureApplyFailure, + _ failure: GatedOperationFailure, note: String? = nil, startedAt: ContinuousClock.Instant, coordinator: MainContentCoordinator? @@ -340,7 +301,7 @@ internal extension StructureEditingSession { } catch { changeManager.releaseHold(hold, written: false) isApplying = false - let failure = StructureApplyFailure(error) + let failure = GatedOperationFailure(error) if failure.reportsFailure { CatalogChangeService.post( .changed(CatalogChange(connectionId: connection.id, database: prepared.scope.database, kinds: .tables)) diff --git a/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift b/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift index 30c1ff93cc..47f269457e 100644 --- a/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift +++ b/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift @@ -65,7 +65,7 @@ internal extension StructureEditingSession { ) await finishColumnReorder(prepared, clearTarget: clearTarget) } catch { - if StructureApplyFailure(error).reportsFailure { + if GatedOperationFailure(error).reportsFailure { CatalogChangeService.post( .changed( CatalogChange( @@ -115,7 +115,7 @@ internal extension StructureEditingSession { } private func reportColumnReorderFailure(_ error: any Error) { - guard let message = StructureApplyFailure(error).message else { return } + guard let message = GatedOperationFailure(error).message else { return } AlertHelper.showErrorSheet( title: String(localized: "Column Reorder Failed"), message: message, diff --git a/apple/TablePro/Views/Structure/TriggerDetailView.swift b/apple/TablePro/Views/Structure/TriggerDetailView.swift index a0885aac43..34a9be327b 100644 --- a/apple/TablePro/Views/Structure/TriggerDetailView.swift +++ b/apple/TablePro/Views/Structure/TriggerDetailView.swift @@ -277,7 +277,7 @@ struct TriggerDetailView: View { name: trigger.name ) } catch { - actionError = error.localizedDescription + actionError = GatedOperationFailure(error).message } } } diff --git a/apple/TablePro/Views/Structure/TriggerEditorView.swift b/apple/TablePro/Views/Structure/TriggerEditorView.swift index 6dee7b596e..ef9b441168 100644 --- a/apple/TablePro/Views/Structure/TriggerEditorView.swift +++ b/apple/TablePro/Views/Structure/TriggerEditorView.swift @@ -29,7 +29,12 @@ struct TriggerEditorView: View { @State private var isApplying = false @State private var errorMessage: String? @State private var droppedDefinition: String? - @AppStorage("structureCodeFontSize", store: AppStorageEnvironment.shared.defaults) private var fontSize: Double = 13 + @AppStorage(TriggerEditorView.fontSizeKey, store: AppStorageEnvironment.shared.defaults) + private var fontSize = TriggerEditorView.defaultFontSize + + // The Structure tab's DDL pane writes its zoom here too. + static let fontSizeKey = "structureCodeFontSize" + static let defaultFontSize = 13.0 init( scope: DatabaseScope, @@ -45,7 +50,14 @@ struct TriggerEditorView: View { self.mode = mode self.onClose = onClose self._sql = State(wrappedValue: initialSQL) - self._editorConfiguration = State(wrappedValue: Self.makeConfiguration(fontSize: 13)) + self._editorConfiguration = State(wrappedValue: Self.initialConfiguration()) + } + + // The State is seeded once, here, so it reads the store; `fontSize` only follows later zooms. + static func initialConfiguration( + in defaults: UserDefaults = AppStorageEnvironment.shared.defaults + ) -> SourceEditorConfiguration { + makeConfiguration(fontSize: defaults.object(forKey: fontSizeKey) as? Double ?? defaultFontSize) } var body: some View { @@ -145,7 +157,7 @@ struct TriggerEditorView: View { if let dropped = (error as? TriggerEditingError)?.droppedDefinition { droppedDefinition = dropped } - errorMessage = error.localizedDescription + errorMessage = GatedOperationFailure(error).message } } } diff --git a/apple/TableProMobile/TableProMobile/Completion/QueryCompletionCatalog.swift b/apple/TableProMobile/TableProMobile/Completion/QueryCompletionCatalog.swift index e7c5b14de0..9b87a2bce5 100644 --- a/apple/TableProMobile/TableProMobile/Completion/QueryCompletionCatalog.swift +++ b/apple/TableProMobile/TableProMobile/Completion/QueryCompletionCatalog.swift @@ -21,22 +21,39 @@ actor QueryCompletionCatalog: SQLCompletionCatalog { let columns: [ColumnInfo] } - private var tables: [TableInfo] + private var tables: [TableInfo] { + didSet { + cachedTableItems = nil + cachedTableColumnItems = nil + } + } + // Every table-slot request reads all of them, so they are built once per table list. + private var cachedTableItems: [SQLCompletionItem]? private let databaseType: DatabaseType private let schema: String? private let readColumns: QueryColumnReader private let readCatalogColumns: QueryColumnReader? private var columnReadsAllowed = true private var columnGeneration = 0 - private var columns: [ColumnKey: CachedColumns] = [:] + private var columns: [ColumnKey: CachedColumns] = [:] { + didSet { cachedTableColumnItems = nil } + } + // Every column request before a FROM reads all of them, so they are built once per column cache, table list and + // namespace list. + private var cachedTableColumnItems: [SQLCompletionItem]? + private(set) var tableColumnItemsBuildCount = 0 private var reads: [ColumnKey: Task<[ColumnInfo]?, any Error>] = [:] private var sessionReads: [ColumnKey: Task<[ColumnInfo]?, any Error>] = [:] private let readSchemaTables: QuerySchemaTableReader? private let readCatalogSchemaTables: QuerySchemaTableReader? private var sessionSchemaReads: [String: Task<[TableInfo]?, any Error>] = [:] private var sessionLocalTables: Set = [] - private var schemas: [String] - private var databases: [String] + private var schemas: [String] { + didSet { cachedTableColumnItems = nil } + } + private var databases: [String] { + didSet { cachedTableColumnItems = nil } + } private var schemaTables: [String: (generation: Int, tables: [TableInfo])] = [:] private var schemaTableReads: [String: Task<[TableInfo]?, any Error>] = [:] @@ -123,19 +140,21 @@ actor QueryCompletionCatalog: SQLCompletionCatalog { } func tableCompletionItems() -> [SQLCompletionItem] { - tables.map { table in - let isView = table.type == .view || table.type == .materializedView - return SQLCompletionItem(label: table.name, kind: isView ? .view : .table, insertText: quoted(table.name)) - } + if let cachedTableItems { return cachedTableItems } + let items = tables.map { tableItem($0) } + cachedTableItems = items + return items + } + + private func tableItem(_ table: TableInfo) -> SQLCompletionItem { + let isView = table.type == .view || table.type == .materializedView + return SQLCompletionItem(label: table.name, kind: isView ? .view : .table, insertText: quoted(table.name)) } func tableCompletionItems(inSchema schema: String) async -> [SQLCompletionItem] { guard isKnownSchema(schema) else { return [] } guard !isActiveSchema(schema) else { return tableCompletionItems() } - return await tables(inSchema: schema).map { table in - let isView = table.type == .view || table.type == .materializedView - return SQLCompletionItem(label: table.name, kind: isView ? .view : .table, insertText: quoted(table.name)) - } + return await tables(inSchema: schema).map { tableItem($0) } } func schemaCompletionItems() -> [SQLCompletionItem] { @@ -317,6 +336,14 @@ actor QueryCompletionCatalog: SQLCompletionCatalog { } func allColumnsFromCachedTables() -> [SQLCompletionItem] { + if let cachedTableColumnItems { return cachedTableColumnItems } + let items = buildTableColumnItems() + tableColumnItemsBuildCount += 1 + cachedTableColumnItems = items + return items + } + + private func buildTableColumnItems() -> [SQLCompletionItem] { // Sorted, because the ranker keeps emission order between equal scores and a dictionary's order is per process. let cached = columns.sorted { lhs, rhs in (lhs.key.schema ?? "", lhs.key.table) < (rhs.key.schema ?? "", rhs.key.table) diff --git a/apple/TableProMobile/TableProMobileTests/Completion/QueryCompletionCatalogTests.swift b/apple/TableProMobile/TableProMobileTests/Completion/QueryCompletionCatalogTests.swift index f5322a5f2e..5dd5104935 100644 --- a/apple/TableProMobile/TableProMobileTests/Completion/QueryCompletionCatalogTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Completion/QueryCompletionCatalogTests.swift @@ -400,6 +400,35 @@ struct QueryCompletionCatalogTests { #expect(await source.reads == ["orders", "customers"]) } + @Test("cached column suggestions are built once, and again only after a read, a table list or a namespace list") + func cachedColumnsAreBuiltOnce() async { + let source = CatalogColumnSource(["orders": [column("id")], "customers": [column("name")]]) + let catalog = catalog(source, tables: [table("orders"), table("customers")]) + _ = await catalog.columnCompletionItems(for: "orders", schema: nil) + + let first = await catalog.allColumnsFromCachedTables() + let second = await catalog.allColumnsFromCachedTables() + #expect(labels(second) == labels(first)) + #expect(await catalog.tableColumnItemsBuildCount == 1) + + _ = await catalog.columnCompletionItems(for: "customers", schema: nil) + let afterRead = await catalog.allColumnsFromCachedTables() + #expect(labels(afterRead) == ["name", "id"]) + #expect(await catalog.tableColumnItemsBuildCount == 2) + + let listedBefore = await catalog.tableCompletionItems() + #expect(labels(listedBefore) == ["orders", "customers"]) + await catalog.replaceTables([table("orders"), table("customers"), table("refunds")]) + _ = await catalog.allColumnsFromCachedTables() + #expect(await catalog.tableColumnItemsBuildCount == 3) + let listedAfter = await catalog.tableCompletionItems() + #expect(labels(listedAfter) == ["orders", "customers", "refunds"]) + + await catalog.replaceNamespaces(schemas: ["main"], databases: []) + _ = await catalog.allColumnsFromCachedTables() + #expect(await catalog.tableColumnItemsBuildCount == 4) + } + @Test("the catalog connection reads first and is not closed by a run; the session reads only without one") func catalogConnectionFirst() async { let catalogSource = CatalogColumnSource(["orders": [column("id")]]) diff --git a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBitColumnTests.swift b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBitColumnTests.swift index d3d4c2d300..aed8b0f01c 100644 --- a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBitColumnTests.swift +++ b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBitColumnTests.swift @@ -92,8 +92,8 @@ struct SQLStatementGeneratorBitColumnTests { let rowID = RowID.inserted(UUID()) let statements = generator.generateStatements( - from: [RowChange(rowID: rowID, type: .insert)], - insertedRowData: [rowID: ["__DEFAULT__", "5", "Ann"]], + from: [RowChange(rowID: rowID, type: .insert, expressionColumns: [0])], + insertedRowData: [rowID: [.columnDefault, "5", "Ann"]], deletedRowIDs: [], insertedRowIDs: [rowID] ) diff --git a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorRowMatchTests.swift b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorRowMatchTests.swift index 44d6a863ef..ea1bbb0f87 100644 --- a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorRowMatchTests.swift +++ b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorRowMatchTests.swift @@ -62,10 +62,12 @@ struct SQLStatementGeneratorRowMatchTests { @Test("A Databend row left entirely on defaults names one column with DEFAULT") func databendAllDefaultsInsert() throws { - let change = RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil) + let change = RowChange( + rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil, expressionColumns: [0, 1, 2, 3] + ) let statements = try generator(excluding: []).generateStatements( from: [change], - insertedRowData: [.existing(0): [.text("__DEFAULT__"), .text("__DEFAULT__"), .text("__DEFAULT__"), .text("__DEFAULT__")]], + insertedRowData: [.existing(0): [.columnDefault, .columnDefault, .columnDefault, .columnDefault]], deletedRowIDs: [], insertedRowIDs: [.existing(0)] ) diff --git a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorTests.swift b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorTests.swift index edeb219dea..731615a286 100644 --- a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorTests.swift +++ b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorTests.swift @@ -89,14 +89,14 @@ struct SQLStatementGeneratorTests { #expect(statements[0].parameters[2] == nil) } - @Test("Insert skips __DEFAULT__ columns") + @Test("Insert leaves out a column marked as its default") func testInsertSkipsDefaultColumns() throws { let generator = try makeGenerator() let insertedRowData: [RowID: [PluginCellValue]] = [ - .existing(0): ["__DEFAULT__", "John", "john@example.com"] + .existing(0): [.columnDefault, "John", "john@example.com"] ] let changes: [RowChange] = [ - RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil) + RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil, expressionColumns: [0]) ] let statements = generator.generateStatements( @@ -114,11 +114,35 @@ struct SQLStatementGeneratorTests { #expect(stmt.parameters.count == 2) } + @Test("Insert binds text that only reads like the default, unmarked", arguments: ["__DEFAULT__", "DEFAULT"]) + func testInsertBindsUnmarkedDefaultText(text: String) throws { + let generator = try makeGenerator() + let insertedRowData: [RowID: [PluginCellValue]] = [ + .existing(0): [.columnDefault, .text(text), "john@example.com"] + ] + let changes: [RowChange] = [ + RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil, expressionColumns: [0]) + ] + + let statements = generator.generateStatements( + from: changes, + insertedRowData: insertedRowData, + deletedRowIDs: [], + insertedRowIDs: [.existing(0)] + ) + + #expect(statements.count == 1) + let stmt = statements[0] + #expect(!stmt.sql.contains("`id`")) + #expect(stmt.sql.contains("`name`")) + #expect(stmt.parameters.compactMap { $0 as? String } == [text, "john@example.com"]) + } + /// This used to expect no statement at all, which dropped the row from the batch while the rest /// of the save committed and reported success. A table of nothing but an identity column and /// defaults is exactly the shape that produces it. @Test( - "Insert with all __DEFAULT__ names no column", + "Insert with every column marked as its default names no column", arguments: [ (DatabaseType.mysql, "() VALUES ()"), (DatabaseType.postgresql, "DEFAULT VALUES"), @@ -128,10 +152,10 @@ struct SQLStatementGeneratorTests { func testInsertAllDefaultNamesNoColumn(databaseType: DatabaseType, expected: String) throws { let generator = try makeGenerator(databaseType: databaseType) let insertedRowData: [RowID: [PluginCellValue]] = [ - .existing(0): ["__DEFAULT__", "__DEFAULT__", "__DEFAULT__"] + .existing(0): [.columnDefault, .columnDefault, .columnDefault] ] let changes: [RowChange] = [ - RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil) + RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil, expressionColumns: [0, 1, 2]) ] let statements = generator.generateStatements( @@ -378,7 +402,7 @@ struct SQLStatementGeneratorTests { #expect(statements[0].parameters[0] == nil) } - @Test("Update with __DEFAULT__ value") + @Test("Update with a cell marked as its default") func testUpdateWithDefaultValue() throws { let generator = try makeGenerator() let changes: [RowChange] = [ @@ -386,7 +410,10 @@ struct SQLStatementGeneratorTests { rowID: .existing(0), type: .update, cellChanges: [ - CellChange(columnIndex: 1, columnName: "name", oldValue: "John", newValue: "__DEFAULT__") + CellChange( + columnIndex: 1, columnName: "name", oldValue: "John", newValue: .columnDefault, + newIsExpression: true + ) ], originalRow: ["1", "John", "john@example.com"] ) @@ -405,6 +432,32 @@ struct SQLStatementGeneratorTests { #expect(stmt.parameters.count == 1) } + @Test("Update binds typed text that only reads like the default", arguments: ["__DEFAULT__", "DEFAULT"]) + func testUpdateBindsUnmarkedDefaultText(text: String) throws { + let generator = try makeGenerator() + let changes: [RowChange] = [ + RowChange( + rowID: .existing(0), + type: .update, + cellChanges: [CellChange(columnIndex: 1, columnName: "name", oldValue: "John", newValue: .text(text))], + originalRow: ["1", "John", "john@example.com"] + ) + ] + + let statements = generator.generateStatements( + from: changes, + insertedRowData: [:], + deletedRowIDs: [], + insertedRowIDs: [] + ) + + #expect(statements.count == 1) + let stmt = statements[0] + #expect(stmt.sql.contains("`name` = ?")) + #expect(!stmt.sql.contains("DEFAULT")) + #expect(stmt.parameters.compactMap { $0 as? String } == [text, "1"]) + } + @Test("Update with a cell marked as SQL inlines it") func testUpdateWithSQLFunction() throws { let generator = try makeGenerator() diff --git a/apple/TableProTests/Core/Compare/CompareDriverRowCutTests.swift b/apple/TableProTests/Core/Compare/CompareDriverRowCutTests.swift new file mode 100644 index 0000000000..1d8668db4f --- /dev/null +++ b/apple/TableProTests/Core/Compare/CompareDriverRowCutTests.swift @@ -0,0 +1,90 @@ +// +// CompareDriverRowCutTests.swift +// TableProTests +// + +import Foundation +import TableProPluginKit +import Testing + +@testable import TablePro + +struct CompareDriverRowCutTests { + private func stream(rowLimit: Int?, ids: [Int]) -> AsyncThrowingStream { + var header = PluginStreamHeader(columns: ["id", "name"], columnTypeNames: ["integer", "text"]) + header.rowLimit = rowLimit + let rows: [PluginRow] = ids.map { [.text(String($0)), .text("n")] } + return AsyncThrowingStream { continuation in + continuation.yield(.header(header)) + continuation.yield(.rows(rows)) + continuation.finish() + } + } + + private func targetRows(_ ids: [Int]) -> [DataRow] { + ids.map { DataRow(values: ["id": .text(String($0)), "name": .text("n")]) } + } + + private func engine() -> DataDiffEngine { + DataDiffEngine( + options: DataCompareOptions(), + shape: DataComparisonShape( + keyColumns: ["id"], + keyOrders: [.numeric], + comparedColumns: ["name"], + digestColumns: ["id", "name"] + ) + ) + } + + @Test("A stream that ends at the limit its driver declares reads as cut there") + func streamEndingAtTheDeclaredLimitIsCut() async throws { + let provider = StreamingRowProvider(stream: stream(rowLimit: 2, ids: [1, 2])) + while try await provider.nextRow() != nil {} + + #expect(provider.endedAtRowLimit) + #expect(provider.driverRowCut == 2) + } + + @Test("A stream that ends short of its declared limit read every row") + func streamShortOfTheDeclaredLimitIsWhole() async throws { + let provider = StreamingRowProvider(stream: stream(rowLimit: 5, ids: [1, 2])) + while try await provider.nextRow() != nil {} + + #expect(!provider.endedAtRowLimit) + #expect(provider.driverRowCut == nil) + } + + // A HANA source stops at 5,000,000 rows: every target row past them read as a delete, and a sync removed them. + @Test("A source the driver cut short reports no deletes past the cut") + func cutSourceProducesNoDeletesPastIt() async throws { + let summary = try await engine().compare( + source: StreamingRowProvider(stream: stream(rowLimit: 2, ids: [1, 2])), + target: ArrayRowProvider(rows: targetRows([1, 2, 3, 4])) + ) + + #expect(summary.deleteCount == 0) + #expect(summary.identicalCount == 2) + #expect(summary.stoppedAtRowLimit) + #expect(summary.sourceRowCut == 2) + #expect(summary.targetRowCut == nil) + #expect(summary.resumeKey == [.text("2")]) + } + + @Test("A target the driver cut short reports no inserts past the cut") + func cutTargetProducesNoInsertsPastIt() async throws { + let summary = try await engine().compare( + source: ArrayRowProvider(rows: targetRows([1, 2, 3, 4])), + target: StreamingRowProvider(stream: stream(rowLimit: 2, ids: [1, 2])) + ) + + #expect(summary.insertCount == 0) + #expect(summary.targetRowCut == 2) + #expect(summary.stoppedAtRowLimit) + } + + @Test("The cut is named in the wording the export uses") + func cutWordingMatchesTheExport() { + #expect(StreamRowCutWarning.text(table: "orders", limit: 5_000_000).hasPrefix("orders: ")) + } +} diff --git a/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift b/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift index d6ba71ac85..04baec1f63 100644 --- a/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift +++ b/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift @@ -99,14 +99,27 @@ struct CompareSQLLiteralTests { #expect(CompareSQLLiteral.prefixed("0x89504E47", databaseType: .mssql) == "0x89504E47") } + // `'\x…'::bytea` reads its backslash by `standard_conforming_strings`, and with that off a sync stored other bytes. @Test - func postgresFamilyUsesAByteaCast() throws { - for type in [DatabaseType.postgresql, .cockroachdb, .redshift, .pglite, .kingbasees] { + func postgresFamilyDecodesHex() throws { + for type in [DatabaseType.postgresql, .cockroachdb, .pglite] { let literal = try #require(CompareSQLLiteral.binaryLiteral(for: png, databaseType: type)) - #expect(literal == "'\\x89504e47'::bytea", "\(type.rawValue) uses a bytea literal") + #expect(literal == "decode('89504E47', 'hex')", "\(type.rawValue) decodes hex") } } + // KingbaseES documents `decode` only as Oracle's DECODE; its bytea decoder is not the PostgreSQL one. + @Test + func kingbaseKeepsTheByteaLiteral() { + #expect(CompareSQLLiteral.binaryLiteral(for: png, databaseType: .kingbasees) == "'\\x89504E47'::bytea") + } + + // Redshift has no bytea, and its DECODE is the CASE-like one; a binary column there is VARBYTE. + @Test + func redshiftReadsHexIntoVarbyte() { + #expect(CompareSQLLiteral.binaryLiteral(for: png, databaseType: .redshift) == "from_hex('89504E47')") + } + @Test func sQLServerUsesZeroX() throws { let literal = try #require(CompareSQLLiteral.binaryLiteral(for: png, databaseType: .mssql)) @@ -121,6 +134,12 @@ struct CompareSQLLiteralTests { #expect(literal == "HEXTORAW('89504E47')") } + // `HEXTORAW('')` stores NULL, and a `BLOB NOT NULL` refuses it. + @Test + func oracleWritesAnEmptyValueAsAnEmptyBlob() { + #expect(CompareSQLLiteral.binaryLiteral(for: Data(), databaseType: .oracle) == "EMPTY_BLOB()") + } + /// An engine this build does not name falls back to the driver's own spelling rather than /// guessing at one, which is why the helper returns nil instead of a default. @Test @@ -132,6 +151,6 @@ struct CompareSQLLiteralTests { func emptyDataStillProducesAValidLiteral() throws { let literal = try #require(CompareSQLLiteral.binaryLiteral(for: Data(), databaseType: .postgresql)) - #expect(literal == "'\\x'::bytea") + #expect(literal == "decode('', 'hex')") } } diff --git a/apple/TableProTests/Core/Compare/DataSyncScriptBuilderTests.swift b/apple/TableProTests/Core/Compare/DataSyncScriptBuilderTests.swift index 72c7c861e1..517a89829e 100644 --- a/apple/TableProTests/Core/Compare/DataSyncScriptBuilderTests.swift +++ b/apple/TableProTests/Core/Compare/DataSyncScriptBuilderTests.swift @@ -795,7 +795,7 @@ final class DataSyncScriptBuilderColumnTests: XCTestCase { XCTAssertEqual( statements.map(\.sql), - [#"INSERT INTO "public"."files" ("id", "blob") VALUES (1, '\x8950'::bytea)"#] + [#"INSERT INTO "public"."files" ("id", "blob") VALUES (1, decode('8950', 'hex'))"#] ) } diff --git a/apple/TableProTests/Core/Coordinators/RowEditingCoordinatorUUIDKeyTests.swift b/apple/TableProTests/Core/Coordinators/RowEditingCoordinatorUUIDKeyTests.swift index 71a31a2c8f..1386171d49 100644 --- a/apple/TableProTests/Core/Coordinators/RowEditingCoordinatorUUIDKeyTests.swift +++ b/apple/TableProTests/Core/Coordinators/RowEditingCoordinatorUUIDKeyTests.swift @@ -71,7 +71,7 @@ struct RowEditingCoordinatorUUIDKeyTests { coordinator.addNewRow() - #expect(try #require(lastRow(of: coordinator)).values[0] == .text("__DEFAULT__")) + #expect(try #require(lastRow(of: coordinator)).values[0] == .columnDefault) } @Test("Add Row on an integer key leaves it as before") diff --git a/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift b/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift index fb7a319753..10d189f39a 100644 --- a/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift +++ b/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift @@ -277,6 +277,87 @@ struct RewindPlannerTests { #expect(queries.first?.contains("SELECT *") == false) } + @Test("An edit that changed the key is read and restored under the key the save gave it") + func movedKeyIsFoundUnderItsNewKey() throws { + let moved = operation(kind: .update, preImage: ["k5", "Ada"], postImage: ["k6", "Ada"], writtenColumns: ["id"]) + let rewind = planner(operations: [moved]) + + let query = try #require(rewind.readQueries().first) + #expect(query.contains("k6")) + #expect(query.contains("k5")) + + let plan = try rewind.plan(currentRows: [RewindCurrentRow(values: ["k6", "Ada"])]) + #expect(plan.rows.map { $0.outcome } == [.willRestore]) + #expect(plan.statements.first?.sql.hasPrefix("UPDATE") == true) + #expect(plan.statements.first?.parameters.compactMap { $0 as? String } == ["k5", "k6"]) + } + + @Test("A moved row a rewind already put back reads as restored, and another row on its old key does not") + func movedKeyAlreadyRestored() throws { + let moved = operation( + kind: .update, preImage: ["k5", "Ada"], postImage: ["k6", "Grace"], writtenColumns: ["id", "name"] + ) + + let restored = try planner(operations: [moved]).plan(currentRows: [RewindCurrentRow(values: ["k5", "Ada"])]) + #expect(restored.rows.map { $0.outcome } == [.alreadyRestored]) + #expect(restored.statements.isEmpty) + + let taken = try planner(operations: [moved]).plan(currentRows: [RewindCurrentRow(values: ["k5", "Bo"])]) + #expect(taken.rows.map { $0.outcome } == [.rowMissing]) + #expect(taken.statements.isEmpty) + } + + @Test("A row that took the key a save moved to is left alone, though the save wrote only the key") + func rowOnTheMovedKeyIsNotRewritten() throws { + let moved = operation(kind: .update, preImage: ["k5", "Ada"], postImage: ["k6", "Ada"], writtenColumns: ["id"]) + + let plan = try planner(operations: [moved]).plan(currentRows: [RewindCurrentRow(values: ["k6", "Bo"])]) + + #expect(plan.rows.map { $0.outcome } == [.changedSinceSave]) + #expect(plan.statements.isEmpty) + } + + @Test("A moved row is not held to a column the server computes") + func movedKeyIgnoresGeneratedColumns() throws { + let columns = ["id", "name", "updated_at"] + let moved = RowWriteOperation( + kind: .update, target: target, columns: columns, primaryKeyColumns: ["id"], + preImage: ["k5", "Ada", "2026-01-01"], postImage: ["k6", "Ada", "2026-01-01"], + writtenColumns: ["id"], refusal: nil + ) + let record = RewindRecord( + id: UUID(), historyId: nil, connectionId: UUID(), databaseType: .sqlite, + target: target, capturedAt: Date(timeIntervalSince1970: 0), + generatedColumns: ["updated_at"], identityColumns: [], operations: [moved] + ) + let planner = RewindPlanner( + record: record, + factory: RowChangeStatementFactory( + tableName: target.table, schemaName: nil, columns: columns, primaryKeyColumns: ["id"], + generatedColumns: ["updated_at"], databaseType: .sqlite, pluginDriver: nil + ), + queryBuilder: sqliteQueryBuilder + ) + + let plan = try planner.plan(currentRows: [RewindCurrentRow(values: ["k6", "Ada", "2026-06-30"])]) + + #expect(plan.rows.map { $0.outcome } == [.willRestore]) + } + + @Test("Undoing a restore takes back what the restore did last first") + func inverseRecordRunsBackwards() throws { + let deleted = operation(kind: .delete, preImage: ["7", "Ada"], postImage: nil, writtenColumns: columns) + let inserted = operation(kind: .insert, preImage: nil, postImage: ["8", "Ada"], writtenColumns: columns) + let plan = try planner(operations: [deleted, inserted]) + .plan(currentRows: [RewindCurrentRow(values: ["8", "Ada"])]) + + let inverse = RewindExecutor.inverseOperations(of: plan) + #expect(inverse.map { $0.kind } == [.delete, .insert]) + + let undo = try planner(operations: inverse).plan(currentRows: [RewindCurrentRow(values: ["7", "Ada"])]) + #expect(undo.statements.map { String($0.sql.prefix(6)) } == ["DELETE", "INSERT"]) + } + @Test("The inverse runs newest first, so a value one row freed is not taken before the other gives it up") func inverseRunsInReverseOrder() throws { let first = operation(kind: .delete, preImage: ["7", "Ada"], postImage: nil, writtenColumns: columns) diff --git a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift index e688dff4ff..570e20367b 100644 --- a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift +++ b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift @@ -255,8 +255,8 @@ struct RowChangeStatementFactoryCoverageTests { let engine = DatabaseType(rawValue: "ProbeEngine") let hostOnly = LegacyStatementStubDriver { _, _, _, _ in nil } let inserted = RowID.inserted(UUID()) - let allDefaults: [PluginCellValue] = [.text("__DEFAULT__"), .text("__DEFAULT__")] - let changes = [nameEdit(), RowChange(rowID: inserted, type: .insert)] + let allDefaults: [PluginCellValue] = [.columnDefault, .columnDefault] + let changes = [nameEdit(), RowChange(rowID: inserted, type: .insert, expressionColumns: [0, 1])] let generator = try SQLStatementGenerator( tableName: "t", columns: columns, primaryKeyColumns: ["_id"], databaseType: engine, diff --git a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryExpressionTests.swift b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryExpressionTests.swift index 93c24b8d32..0109aeb1ec 100644 --- a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryExpressionTests.swift +++ b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryExpressionTests.swift @@ -36,6 +36,21 @@ private final class ExpressionRecordingDriver: PluginDriverStub, @unchecked Send func quoteIdentifier(_ name: String) -> String { "[\(name)]" } } +private final class RestoringDriver: PluginDriverStub, @unchecked Sendable { + func generateIdentityPreservingInsert( + table: String, + schema: String?, + columns: [String], + primaryKeyColumns: [String], + rows: [[PluginCellValue]], + absentCells: [Int: Set] + ) -> [(statement: String, parameters: [PluginCellValue])]? { + rows.map { (statement: "INSERT INTO [events] VALUES (?, ?, ?)", parameters: $0) } + } + + func quoteIdentifier(_ name: String) -> String { "[\(name)]" } +} + @MainActor struct RowChangeStatementFactoryExpressionTests { private let columns = ["id", "name", "stamped_at"] @@ -192,7 +207,7 @@ struct RowChangeStatementFactoryExpressionTests { @Test("SQLite refuses DEFAULT on a saved row, naming the column") func sqliteDefaultUpdateIsRefused() { - let change = edit(to: .text(PluginCellValue.defaultMarkerText), isExpression: false, column: 1) + let change = edit(to: .columnDefault, isExpression: true, column: 1) let refused = refusal { _ = try factory(databaseType: .sqlite).statements(for: [change]) } #expect(refused?.kind == .update) #expect(refused?.reason.contains("name") == true) @@ -202,8 +217,8 @@ struct RowChangeStatementFactoryExpressionTests { func sqliteDefaultInsertIsWritten() throws { let rowID = RowID.inserted(UUID()) let statements = try factory(databaseType: .sqlite).statements( - for: [RowChange(rowID: rowID, type: .insert)], - insertedRowData: [rowID: ["1", .text(PluginCellValue.defaultMarkerText), "2026-01-01"]], + for: [RowChange(rowID: rowID, type: .insert, expressionColumns: [1])], + insertedRowData: [rowID: ["1", .columnDefault, "2026-01-01"]], insertedRowIDs: [rowID] ) #expect(statements.count == 1) @@ -212,8 +227,69 @@ struct RowChangeStatementFactoryExpressionTests { @Test("PostgreSQL takes DEFAULT on a saved row") func postgresDefaultUpdate() throws { - let change = edit(to: .text(PluginCellValue.defaultMarkerText), isExpression: false, column: 1) + let change = edit(to: .columnDefault, isExpression: true, column: 1) let statements = try factory().statements(for: [change]) #expect(statements.first?.sql.contains("\"name\" = DEFAULT") == true) } + + @Test("Text a row holds or a user typed that reads like the default is written as that text", arguments: [ + "__DEFAULT__", "DEFAULT", + ]) + func unmarkedDefaultTextIsAValue(text: String) throws { + let saved = try factory(databaseType: .sqlite).statements(for: [edit(to: .text(text), isExpression: false, column: 1)]) + #expect(saved.first?.sql.contains("DEFAULT") == false) + #expect(saved.first?.parameters.first as? String == text) + + let rowID = RowID.inserted(UUID()) + let inserted = try factory(databaseType: .sqlite).statements( + for: [RowChange(rowID: rowID, type: .insert)], + insertedRowData: [rowID: ["1", .text(text), "2026-01-01"]], + insertedRowIDs: [rowID] + ) + #expect(inserted.first?.parameters.compactMap { $0 as? String } == ["1", text, "2026-01-01"]) + } + + @Test("A driver is handed a default in the spelling it reads, with no SQL mark, whatever it declares") + func defaultReachesADriverInItsSpelling() throws { + let driver = ExpressionRecordingDriver(capabilities: []) + _ = try factory(databaseType: .mssql, driver: driver).statements( + for: [edit(to: .columnDefault, isExpression: true, column: 1)] + ) + #expect(driver.changes.first?.cellChanges.first?.newValue == .pluginDefaultMarker) + #expect(driver.changes.first?.expressionColumns == nil) + } + + // A driver may write DEFAULT for it, and none can say it will not, so every one that writes its own rows is refused. + @Test("Text spelled the way drivers read a default is refused by a driver that writes its own rows", arguments: [ + DatabaseType.mssql, DatabaseType.redis, + ]) + func textADriverMayReadAsDefaultIsRefused(databaseType: DatabaseType) { + let driver = ExpressionRecordingDriver(capabilities: []) + let refused = refusal { + _ = try factory(databaseType: databaseType, driver: driver).statements( + for: [edit(to: "__DEFAULT__", isExpression: false, column: 1)] + ) + } + #expect(refused?.kind == .update) + #expect(refused?.reason.contains("name") == true) + } + + @Test("A deleted row a driver puts back is refused when it holds that text", arguments: [ + DatabaseType.dameng, DatabaseType.redis, + ]) + func restoredTextADriverMayReadAsDefaultIsRefused(databaseType: DatabaseType) { + let refused = refusal { + _ = try factory(databaseType: databaseType, driver: RestoringDriver()) + .restoreStatements(rows: [["1", "__DEFAULT__", "2026-01-01"]]) + } + #expect(refused?.kind == .insert) + #expect(refused?.reason.contains("name") == true) + } + + @Test("A deleted row a driver puts back with no such text is restored") + func restoredRowWithoutThatTextIsWritten() throws { + let restored = try factory(databaseType: .dameng, driver: RestoringDriver()) + .restoreStatements(rows: [["1", "Ada", "2026-01-01"]]) + #expect(restored.statements.first?.parameters.compactMap { $0 as? String } == ["1", "Ada", "2026-01-01"]) + } } diff --git a/apple/TableProTests/Core/DataWrite/RowWriteOperationBuilderTests.swift b/apple/TableProTests/Core/DataWrite/RowWriteOperationBuilderTests.swift index d4c40a9899..fdf82b33af 100644 --- a/apple/TableProTests/Core/DataWrite/RowWriteOperationBuilderTests.swift +++ b/apple/TableProTests/Core/DataWrite/RowWriteOperationBuilderTests.swift @@ -78,14 +78,14 @@ struct RowWriteOperationBuilderTests { #expect(now.first?.refusal == .serverComputedValue) let marker = operations( - changes: [ - cellEdit( - column: "updated_at", index: 2, from: "2026-01-01", - to: .text(PluginCellValue.defaultMarkerText) - ), - ] + changes: [cellEdit(column: "updated_at", index: 2, from: "2026-01-01", to: .columnDefault, isExpression: true)] ) #expect(marker.first?.refusal == .serverComputedValue) + + let text = operations( + changes: [cellEdit(column: "updated_at", index: 2, from: "2026-01-01", to: "__DEFAULT__")] + ) + #expect(text.first?.refusal == nil) } @Test("A generated column cannot be written back") @@ -136,8 +136,8 @@ struct RowWriteOperationBuilderTests { func serverAssignedKeyIsRefused() { let change = RowChange(rowID: .existing(0), type: .insert) let withMarker = operations( - changes: [change], - insertedRowData: [.existing(0): [.text(PluginCellValue.defaultMarkerText), "Ada", "2026-01-01"]], + changes: [RowChange(rowID: .existing(0), type: .insert, expressionColumns: [0])], + insertedRowData: [.existing(0): [.columnDefault, "Ada", "2026-01-01"]], insertedRowIDs: [.existing(0)] ) #expect(withMarker.first?.refusal == .serverAssignedKey) @@ -184,6 +184,31 @@ struct RowWriteOperationBuilderTests { #expect(literal.first?.refusal == nil) } + @Test("The record keeps the order the rows were changed in after an earlier change is cancelled") + func recordFollowsEditOrder() { + var pending = PendingChanges() + pending.recordCellChange( + rowID: .existing(0), columnIndex: 1, columnName: "name", + oldValue: "Ada", newValue: "Grace", originalRow: ["7", "Ada", "2026-01-01"] + ) + pending.recordRowDeletion(rowID: .existing(1), originalRow: ["8", "Bo", "2026-01-01"]) + let inserted = RowID.inserted(UUID()) + pending.recordRowInsertion(rowID: inserted, values: ["9", "Bo", "2026-01-01"]) + pending.recordCellChange( + rowID: .existing(0), columnIndex: 1, columnName: "name", oldValue: "Grace", newValue: "Ada" + ) + #expect(pending.changes.map { $0.type } == [.insert, .delete]) + + let result = operations( + changes: pending.changes, + insertedRowData: pending.insertedRowData, + deletedRowIDs: pending.deletedRowIDs, + insertedRowIDs: pending.insertedRowIDs + ) + + #expect(result.map { $0.kind } == [.delete, .insert]) + } + @Test("An insert carrying its own key is reversible") func userSuppliedKeyIsReversible() { let result = operations( diff --git a/apple/TableProTests/Core/Database/DatabaseManagerTests.swift b/apple/TableProTests/Core/Database/DatabaseManagerTests.swift index 1d05998bfb..10989dcb28 100644 --- a/apple/TableProTests/Core/Database/DatabaseManagerTests.swift +++ b/apple/TableProTests/Core/Database/DatabaseManagerTests.swift @@ -412,8 +412,11 @@ struct DatabaseManagerDatabaseSwitchTests { let pluginDriver = DatabaseSwitchingDriver() let adapter = PluginDriverAdapter(connection: connection, pluginDriver: pluginDriver) - await DatabaseManager.shared.restoreSchemaAndDatabase(on: adapter, savedSchema: "idb", savedDatabase: "ice") + let warning = await DatabaseManager.shared.restoreSchemaAndDatabase( + on: adapter, savedSchema: "idb", savedDatabase: "ice" + ) #expect(pluginDriver.switches == ["database ice", "schema idb"]) + #expect(warning == nil) } } diff --git a/apple/TableProTests/Core/Database/SchemaWriteGateDenialTests.swift b/apple/TableProTests/Core/Database/SchemaWriteGateDenialTests.swift new file mode 100644 index 0000000000..5af1cd879d --- /dev/null +++ b/apple/TableProTests/Core/Database/SchemaWriteGateDenialTests.swift @@ -0,0 +1,86 @@ +// +// SchemaWriteGateDenialTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +// A denial came back as a failed statement, so Create Database, the schema editor and Create Table +// showed the user's own Cancel at the gate as an error. +@MainActor +struct SchemaWriteGateDenialTests { + private let scope = DatabaseScope(connectionId: UUID(), database: "app", schema: nil) + + private func gateFailure(of body: () async throws -> Void) async -> GatedOperationFailure? { + do { + try await body() + return nil + } catch { + return GatedOperationFailure(error) + } + } + + private func runCreateDatabase(gate: any ExecutionGate) async -> GatedOperationFailure? { + await gateFailure { + try await DatabaseManager.shared.runContainerOperation( + description: "Create database \"sales\"", + kind: .schemaMutation, + scope: scope, + databaseType: .postgresql, + event: nil, + gate: gate + ) { _ in + Issue.record("The driver was asked after the gate said no") + } + } + } + + @Test("A Cancel at the gate ends a database operation quietly, before the driver is asked") + func containerOperationCancelIsQuiet() async { + let failure = await runCreateDatabase(gate: AlwaysDenyGate.cancelling) + + #expect(failure == .cancelledByUser) + #expect(failure?.message == nil) + } + + @Test("A refused database operation says why and is not a failed statement") + func containerOperationRefusalExplainsItself() async { + let failure = await runCreateDatabase(gate: AlwaysDenyGate(reason: "Safe Mode is read-only")) + + #expect(failure == .refused("Safe Mode is read-only")) + #expect(failure?.reportsFailure == false) + } + + @Test("A Cancel at the gate ends a schema editor apply quietly") + func containerStatementsCancelIsQuiet() async { + let failure = await gateFailure { + try await DatabaseManager.shared.runContainerStatements( + .statements([SchemaStatement(sql: "CREATE SCHEMA sales", description: "Create", isDestructive: false)]), + description: "Create Schema", + scope: scope, + databaseType: .postgresql, + event: nil, + gate: AlwaysDenyGate.cancelling + ) + } + + #expect(failure == .cancelledByUser) + } + + @Test("A Cancel at the gate ends a Create Table quietly") + func createTableCancelIsQuiet() async { + let failure = await gateFailure { + try await DatabaseManager.shared.executeCreateTable( + statements: ["CREATE TABLE invoices (id INT)"], + databaseType: .postgresql, + scope: scope, + gate: AlwaysDenyGate.cancelling + ) + } + + #expect(failure == .cancelledByUser) + #expect(failure?.message == nil) + } +} diff --git a/apple/TableProTests/Core/Database/StartupCommandWarningTests.swift b/apple/TableProTests/Core/Database/StartupCommandWarningTests.swift new file mode 100644 index 0000000000..11fbac5867 --- /dev/null +++ b/apple/TableProTests/Core/Database/StartupCommandWarningTests.swift @@ -0,0 +1,135 @@ +// +// StartupCommandWarningTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private struct ServerRefusal: PluginDriverError { + let pluginErrorMessage: String + let pluginSqlState: String? +} + +// A startup command can be a safety rail, so a session that opened without one says so on the connection's warning. +@MainActor +struct StartupCommandWarningTests { + @Test("A startup command the server refused is named on the session's warning, the ones that ran are not") + func refusedStartupCommandIsNamed() async { + let driver = MockDatabaseDriver() + driver.failsStatementContaining = ("SET ROLE", DatabaseError.queryFailed("role \"app_ro\" does not exist")) + + let failures = await DatabaseManager.shared.applyTimeoutAndStartupCommands( + on: driver, + queryTimeoutSeconds: 0, + startupCommands: "SET ROLE app_ro\nSET search_path = app", + connectionName: "Reporting" + ) + let warning = DatabaseManager.startupWarning(for: failures) + + #expect(failures.map { $0.statement } == ["SET ROLE app_ro"]) + #expect(warning?.contains("SET ROLE app_ro") == true) + #expect(warning?.contains("SET search_path") == false) + } + + // PostgreSQL can quote the statement's values back in its message, so only the code reaches the screen. + @Test("The warning names the server's code, never its message") + func warningShowsTheCodeNotTheMessage() { + let refusal = ServerRefusal( + pluginErrorMessage: "invalid value for parameter \"app.api_key\": \"sk-live-123\"", pluginSqlState: "22023" + ) + let failure = StartupCommandFailure(statement: "SET app.api_key = 42", error: refusal) + let warning = DatabaseManager.startupWarning(for: [failure]) + + #expect(failure.code == "22023") + #expect(warning?.contains("SET app.api_key = 42") == true) + #expect(warning?.contains("22023") == true) + #expect(warning?.contains("sk-live-123") == false) + } + + @Test("Startup commands that all ran leave no warning") + func successfulStartupCommandsLeaveNoWarning() async { + let driver = MockDatabaseDriver() + + let failures = await DatabaseManager.shared.applyTimeoutAndStartupCommands( + on: driver, queryTimeoutSeconds: 0, startupCommands: "SET search_path = app", connectionName: "Reporting" + ) + + #expect(DatabaseManager.startupWarning(for: failures) == nil) + } + + @Test("Several failed startup commands are each named") + func severalFailuresAreEachNamed() { + let warning = DatabaseManager.startupWarning(for: [ + StartupCommandFailure(statement: "SET ROLE app_ro", code: "42704"), + StartupCommandFailure(statement: "SET default_transaction_read_only = on", code: nil), + ]) + + #expect(warning?.contains("SET ROLE app_ro (42704)") == true) + #expect(warning?.contains("SET default_transaction_read_only = on") == true) + } + + // A startup command can carry a password or a key, and the banner is on screen, screen sharing included. + @Test("A failed startup command is shown only up to its first literal or credential clause") + func shownStatementStopsBeforeSecrets() { + #expect(DatabaseManager.shownStatement("SELECT set_config('app.api_key', 'sk-live-123', false)") + == "SELECT set_config(…") + #expect(DatabaseManager.shownStatement("SET ROLE reporting IDENTIFIED BY hunter2") == "SET ROLE reporting…") + #expect(DatabaseManager.shownStatement("ALTER USER app WITH PASSWORD 'x'") == "ALTER USER app WITH…") + #expect(DatabaseManager.shownStatement("SET search_path = app") == "SET search_path = app") + + let warning = DatabaseManager.startupWarning(for: [ + StartupCommandFailure(statement: "SET ROLE reporting IDENTIFIED BY hunter2", code: "28000"), + ]) + #expect(warning?.contains("hunter2") == false) + } + + @Test("A reconnect whose startup commands all ran drops the banner about the session before it") + func cleanReconnectClearsTheStartupWarning() { + let connectionId = UUID() + let manager = DatabaseManager.shared + defer { manager.reportedStartupFailures.removeValue(forKey: connectionId) } + + let failed = manager.openingWarning( + driver: MockDatabaseDriver(), + startupFailures: [StartupCommandFailure(statement: "SET ROLE app_ro", code: "42704")], + for: connectionId + ) + let clean = manager.openingWarning(driver: MockDatabaseDriver(), startupFailures: [], for: connectionId) + + #expect(failed?.contains("SET ROLE app_ro") == true) + #expect(clean == nil) + #expect(manager.reportedStartupFailures[connectionId]?.isEmpty == true) + } + + @Test("The login's warning and the startup warning are both kept") + func warningsAreJoined() { + #expect(DatabaseManager.joinedWarning(["ORA-28002", nil, "startup"]) == "ORA-28002\n\nstartup") + #expect(DatabaseManager.joinedWarning([nil, ""]) == nil) + } + + // The pool opens entries for as long as the window is open, so a failure it was already told about stays dismissed. + @Test("A pool or lane session's failed startup command reaches the window once") + func poolFailureIsReportedOnce() { + let connection = TestFixtures.makeConnection(name: "Startup rail") + let manager = DatabaseManager.shared + manager.injectSession(ConnectionSession(connection: connection), for: connection.id) + defer { manager.removeSession(for: connection.id) } + let role = StartupCommandFailure(statement: "SET ROLE app_ro", code: "42704") + let path = StartupCommandFailure(statement: "SET search_path = app", code: "3F000") + + manager.reportStartupFailures([role], for: connection.id) + #expect(manager.session(for: connection.id)?.connectionWarning?.contains("SET ROLE app_ro") == true) + + manager.dismissConnectionWarning(for: connection.id) + manager.reportStartupFailures([role], for: connection.id) + #expect(manager.session(for: connection.id)?.connectionWarning == nil) + + manager.reportStartupFailures([role, path], for: connection.id) + let warning = manager.session(for: connection.id)?.connectionWarning + #expect(warning?.contains("SET search_path = app") == true) + #expect(warning?.contains("SET ROLE") == false) + } +} diff --git a/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift b/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift index e67a5599bd..0b19025ea9 100644 --- a/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift +++ b/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift @@ -437,3 +437,89 @@ struct TriggerEditSafetyTests { #expect(requests.isEmpty) } } + +// A Cancel in the gate's confirmation reached the trigger editor as an error row under the SQL. +@MainActor +struct TriggerGateDenialTests { + private func install() async throws -> (connection: DatabaseConnection, scope: DatabaseScope, stub: StubTriggerDriver) { + let connection = TestFixtures.makeConnection(database: "app", type: .postgresql) + let stub = StubTriggerDriver() + stub.dropToReturn = "DROP TRIGGER t" + let adapter = PluginDriverAdapter(connection: connection, pluginDriver: stub) + try await adapter.connect() + DatabaseManager.shared.injectSession(ConnectionSession(connection: connection, driver: adapter), for: connection.id) + let scope = DatabaseScope(connectionId: connection.id, database: "app", schema: nil) + MetadataConnectionPool.shared.injectEntry(adapter, scope: scope) + return (connection, scope, stub) + } + + private func uninstall(_ connection: DatabaseConnection) { + MetadataConnectionPool.shared.closeAll(connectionId: connection.id) + DatabaseManager.shared.removeSession(for: connection.id) + } + + private func failure(of body: () async throws -> Void) async -> GatedOperationFailure? { + do { + try await body() + return nil + } catch { + return GatedOperationFailure(error) + } + } + + @Test("A Cancel at the gate ends a trigger save and a trigger drop quietly, with nothing run") + func cancelIsQuiet() async throws { + let installed = try await install() + defer { uninstall(installed.connection) } + + let save = await failure { + try await TriggerEditing.apply( + scope: installed.scope, + connection: installed.connection, + tableName: "orders", + sql: "CREATE TRIGGER t", + isEdit: false, + originalName: nil, + originalDefinition: nil, + gate: AlwaysDenyGate.cancelling + ) + } + let drop = await failure { + try await TriggerEditing.drop( + scope: installed.scope, + connection: installed.connection, + tableName: "orders", + name: "t", + gate: AlwaysDenyGate.cancelling + ) + } + + #expect(save == .cancelledByUser) + #expect(drop == .cancelledByUser) + #expect(save?.message == nil) + #expect(installed.stub.executedQueries.isEmpty) + } + + @Test("A refused trigger save says why and is not a failed statement") + func refusalExplainsItself() async throws { + let installed = try await install() + defer { uninstall(installed.connection) } + + let save = await failure { + try await TriggerEditing.apply( + scope: installed.scope, + connection: installed.connection, + tableName: "orders", + sql: "CREATE TRIGGER t", + isEdit: false, + originalName: nil, + originalDefinition: nil, + gate: AlwaysDenyGate(reason: "Safe Mode is read-only") + ) + } + + #expect(save == .refused("Safe Mode is read-only")) + #expect(save?.reportsFailure == false) + #expect(installed.stub.executedQueries.isEmpty) + } +} diff --git a/apple/TableProTests/Core/LSP/LSPTransportBrokenPipeTests.swift b/apple/TableProTests/Core/LSP/LSPTransportBrokenPipeTests.swift new file mode 100644 index 0000000000..7af8c30567 --- /dev/null +++ b/apple/TableProTests/Core/LSP/LSPTransportBrokenPipeTests.swift @@ -0,0 +1,48 @@ +import Foundation +@testable import TablePro +import TableProTestSupport +import Testing + +struct LSPTransportBrokenPipeTests { + static let perl = "/usr/bin/perl" + + static var hasPerl: Bool { + FileManager.default.isExecutableFile(atPath: perl) + } + + // A server on its way out: it stops reading, asks the client something, then says it has stopped. + private static let closingServer = #""" + close STDIN; $| = 1; + for my $body ('{"jsonrpc":"2.0","id":7,"method":"workspace/configuration","params":{}}', + '{"jsonrpc":"2.0","method":"test/inputClosed"}') { + print "Content-Length: " . length($body) . "\r\n\r\n" . $body; + } + sleep 30; + """# + + @Test( + "A server that stopped reading fails the request instead of ending the app", + .enabled(if: LSPTransportBrokenPipeTests.hasPerl), + .timeLimit(.minutes(1)) + ) + func closedInputFailsTheRequest() async throws { + let transport = LSPTransport() + let inputClosed = AsyncGate() + await transport.onNotification(method: "test/inputClosed") { _ in + Task { await inputClosed.open() } + } + try await transport.start(executablePath: Self.perl, arguments: ["-e", Self.closingServer]) + await inputClosed.wait() + + let error = await #expect(throws: (any Error).self) { + _ = try await transport.sendRequest(method: "test/afterClose", params: EmptyLSPParams()) + } + await transport.stop() + + let failure = error.map { $0 as NSError } + let posix = failure?.domain == NSPOSIXErrorDomain + ? failure + : failure?.underlyingErrors.first.map { $0 as NSError } + #expect(posix?.code == Int(EPIPE)) + } +} diff --git a/apple/TableProTests/Core/ObjectCopy/ObjectCopyRowCutTests.swift b/apple/TableProTests/Core/ObjectCopy/ObjectCopyRowCutTests.swift new file mode 100644 index 0000000000..c9e9c4e532 --- /dev/null +++ b/apple/TableProTests/Core/ObjectCopy/ObjectCopyRowCutTests.swift @@ -0,0 +1,107 @@ +// +// ObjectCopyRowCutTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private final class CappedSourceDriver: PluginDriverStub, @unchecked Sendable { + let declaredLimit: Int? + let rowCount: Int + + init(declaredLimit: Int?, rowCount: Int) { + self.declaredLimit = declaredLimit + self.rowCount = rowCount + } + + func streamRows(query: String) -> AsyncThrowingStream { + var header = PluginStreamHeader(columns: ["id", "name"], columnTypeNames: []) + header.rowLimit = declaredLimit + let rows: [PluginRow] = (0 ..< rowCount).map { [.text(String($0)), .text("row\($0)")] } + return AsyncThrowingStream { continuation in + continuation.yield(.header(header)) + continuation.yield(.rows(rows)) + continuation.finish() + } + } +} + +private final class WritingTargetDriver: PluginDriverStub, @unchecked Sendable { + var parameterStyle: ParameterStyle { .questionMark } + + func executeParameterized(query: String, parameters: [PluginCellValue]) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func quoteIdentifier(_ name: String) -> String { "`\(name)`" } +} + +struct ObjectCopyRowCutTests { + private func step() -> ObjectCopyTableStep { + ObjectCopyTableStep( + selection: ObjectCopySelection(kind: .table, name: "orders", schema: "public"), + dropStatements: [], + sequenceStatements: [], + createStatements: [], + truncateStatements: [], + columns: ["id", "name"], + targetColumnTypeNames: [:], + primaryKeyColumns: ["id"], + sourceQuery: "SELECT `id`, `name` FROM `public`.`orders`", + targetTable: "orders", + targetSchema: "public", + estimatedRows: nil, + copiesData: true, + copiesIdentityColumn: false, + coercer: nil, + serverSideInsert: nil, + note: nil + ) + } + + private func copy(from source: CappedSourceDriver) async throws -> ObjectCopyRowCopier.Outcome { + try await ObjectCopyRowCopier(step: step(), sourceDatabaseType: .mysql, targetDatabaseType: .mysql) + .copy(from: source, to: WritingTargetDriver()) { _ in } + } + + // A 6,000,000-row SAP HANA table arrived as 5,000,000 rows and the copy reported success. + @Test("A copy whose source stream stopped at the driver's limit says where") + func cutStreamIsReported() async throws { + let outcome = try await copy(from: CappedSourceDriver(declaredLimit: 3, rowCount: 3)) + + #expect(outcome.inserted == 3) + #expect(outcome.rowCut == 3) + } + + @Test("A copy that read fewer rows than the driver's limit is whole") + func shortStreamIsWhole() async throws { + let outcome = try await copy(from: CappedSourceDriver(declaredLimit: 10, rowCount: 3)) + + #expect(outcome.inserted == 3) + #expect(outcome.rowCut == nil) + } + + @Test("A cut table's outcome carries the export's wording") + func cutOutcomeCarriesTheWarning() { + let selection = ObjectCopySelection(kind: .table, name: "orders", schema: "public") + let result = ObjectCopyRunResult( + outcomes: [ + ObjectCopyObjectOutcome( + selection: selection, + rowsCopied: 3, + error: nil, + warning: StreamRowCutWarning.text(table: selection.qualifiedName, limit: 3) + ) + ], + rowsCopied: 3, + cancelled: false, + createdDatabase: nil + ) + + #expect(result.warnings == [StreamRowCutWarning.text(table: selection.qualifiedName, limit: 3)]) + #expect(result.failedCount == 0) + } +} diff --git a/apple/TableProTests/Core/Services/Query/MetadataConnectionPoolTests.swift b/apple/TableProTests/Core/Services/Query/MetadataConnectionPoolTests.swift index 0a184ad353..04b349fbe9 100644 --- a/apple/TableProTests/Core/Services/Query/MetadataConnectionPoolTests.swift +++ b/apple/TableProTests/Core/Services/Query/MetadataConnectionPoolTests.swift @@ -84,7 +84,7 @@ struct MetadataConnectionPoolTests { func prepareSessionPassesThrough() async throws { let driver = MockDatabaseDriver() - try await MetadataConnectionPool.prepareSession( + let failures = try await MetadataConnectionPool.prepareSession( driver, queryTimeoutSeconds: 30, startupCommands: nil, @@ -93,6 +93,25 @@ struct MetadataConnectionPoolTests { ) #expect(driver.applyQueryTimeoutValues == [30]) + #expect(failures.isEmpty) + } + + // A pool entry or a lane whose `SET ROLE` failed would otherwise run with the login's own rights unannounced. + @Test("session preparation hands back the startup commands that failed") + func prepareSessionReturnsFailedStartupCommands() async throws { + let driver = MockDatabaseDriver() + driver.failsStatementContaining = ("SET ROLE", DatabaseError.queryFailed("role \"app_ro\" does not exist")) + + let failures = try await MetadataConnectionPool.prepareSession( + driver, + queryTimeoutSeconds: 0, + startupCommands: "SET ROLE app_ro\nSET search_path = app", + connectionName: "test", + timeoutSeconds: 1 + ) + + #expect(failures.map { $0.statement } == ["SET ROLE app_ro"]) + #expect(driver.executedQueries == ["SET ROLE app_ro", "SET search_path = app"]) } @Test("database switch rejects a driver that cannot switch") diff --git a/apple/TableProTests/Core/Services/Query/SelectionSummaryCalculatorTests.swift b/apple/TableProTests/Core/Services/Query/SelectionSummaryCalculatorTests.swift index 2f7e00976f..953b41e5c4 100644 --- a/apple/TableProTests/Core/Services/Query/SelectionSummaryCalculatorTests.swift +++ b/apple/TableProTests/Core/Services/Query/SelectionSummaryCalculatorTests.swift @@ -25,8 +25,7 @@ struct SelectionSummaryCalculatorTests { dataColumns: [Int]? = nil, policy: SelectionSummaryColumnPolicy? = nil, deleted: Set = [], - inserted: Set = [], - modified: [RowID: Set] = [:] + defaulted: [RowID: Set] = [:] ) -> SelectionSummaryInput { SelectionSummaryInput( selection: selection(rects), @@ -39,8 +38,7 @@ struct SelectionSummaryCalculatorTests { columnCount: tableRows.columns.count ), deletedRowIDs: deleted, - insertedRowIDs: inserted, - modifiedCells: modified + defaultedCells: defaulted ) } @@ -113,25 +111,24 @@ struct SelectionSummaryCalculatorTests { #expect(summary.numbers?.sum.decimal == 1) } - @Test("NULL is empty, empty text is a value, and the default marker is empty only where an edit put it") + @Test("NULL is empty, empty text is a value, and DEFAULT is empty only where an edit marked it") func emptyCellRules() async throws { let insertedID = RowID.inserted(UUID()) var table = Self.tableRows( [ [.null, .text("")], - [.text(PluginCellValue.defaultMarkerText), .text(PluginCellValue.defaultMarkerText)], - [.text("7"), .text(PluginCellValue.defaultMarkerText)] + [.text("DEFAULT"), .text("DEFAULT")], + [.text("7"), .columnDefault] ], types: [Self.integer, Self.untyped] ) - table.appendInsertedRow(id: insertedID, values: [.text(PluginCellValue.defaultMarkerText), .text("5")]) + table.appendInsertedRow(id: insertedID, values: [.columnDefault, .text("5")]) let summary = try await Self.summarize( Self.input( table, selecting: [GridRect(rows: 0...3, columns: 0...1)], - inserted: [insertedID], - modified: [.existing(2): [1]] + defaulted: [.existing(2): [1], insertedID: [0]] ) ) diff --git a/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift b/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift index a0e0ecf3b2..2823bea961 100644 --- a/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift @@ -82,7 +82,7 @@ extension GlobalStateSuites { #expect(tableRows.row(withID: nullKept.rowID)?.absentColumns.isEmpty == true) #expect(tableRows.row(withID: missingKept.rowID)?.absentColumns == [2]) #expect(changes.pending.insertedAbsentColumns(forRow: missingKept.rowID) == [2]) - #expect(missingKept.values[0] == .text("__DEFAULT__")) + #expect(missingKept.values[0] == .columnDefault) } @Test("Copying rows records which fields each lacked, in the copied column order") @@ -123,7 +123,7 @@ extension GlobalStateSuites { let (tableRows, result) = paste(clipboard, into: manager) - #expect(result.pastedRows.first?.values == [.text("__DEFAULT__"), "Ada", .null]) + #expect(result.pastedRows.first?.values == [.columnDefault, "Ada", .null]) #expect(tableRows.rows[0].absentColumns.isEmpty) } diff --git a/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift b/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift index 80819290f7..51e4d37d67 100644 --- a/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift @@ -51,7 +51,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) #expect(values.count == 3) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("Smith, John")) #expect(values[2] == .text("a@test.com")) } @@ -120,7 +120,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("Jo")) #expect(values[2] == .text("e@x.com")) } @@ -136,7 +136,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) #expect(values.count == 3) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("Jo")) #expect(values[2] == .text("e@x.com")) } @@ -151,7 +151,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("2")) #expect(values[2] == .text("3")) } @@ -170,8 +170,8 @@ struct RowOperationsManagerPasteTests { let rows = pasteRows(clipboard, manager: makeManager()) #expect(rows.count == 2) - #expect(rows[0] == [.text("__DEFAULT__"), .text("Alice"), .text("a@test.com")]) - #expect(rows[1] == [.text("__DEFAULT__"), .text("Bob"), .null]) + #expect(rows[0] == [.columnDefault, .text("Alice"), .text("a@test.com")]) + #expect(rows[1] == [.columnDefault, .text("Bob"), .null]) } @Test("Plain-text TSV with commas inside a value parses as TSV, not CSV") @@ -182,7 +182,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) #expect(values.count == 3) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("Mozilla/5.0 (KHTML, like Gecko) Safari")) #expect(values[2] == .text("a@test.com")) } @@ -257,7 +257,7 @@ struct RowOperationsManagerPasteServerOwnedTests { func resetsNonKeyIdentity() { let values = paste(payload, columnIdentity: ["code": .always]) - #expect(values[1] == .text("__DEFAULT__")) + #expect(values[1] == .columnDefault) #expect(values[2] == .text("Ada")) } @@ -265,7 +265,7 @@ struct RowOperationsManagerPasteServerOwnedTests { func resetsGeneratedColumn() { let values = paste(payload, generatedColumns: ["code"]) - #expect(values[1] == .text("__DEFAULT__")) + #expect(values[1] == .columnDefault) #expect(values[2] == .text("Ada")) } diff --git a/apple/TableProTests/Core/Services/RowOperationsManagerTests.swift b/apple/TableProTests/Core/Services/RowOperationsManagerTests.swift index 7358b7b600..c56df2acad 100644 --- a/apple/TableProTests/Core/Services/RowOperationsManagerTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsManagerTests.swift @@ -118,8 +118,8 @@ struct RowOperationsManagerTests { let result = manager.addNewRow(tableRows: &tableRows) #expect(result != nil) - #expect(result?.values[0] == "__DEFAULT__") - #expect(result?.values[2] == "__DEFAULT__") + #expect(result?.values[0] == .columnDefault) + #expect(result?.values[2] == .columnDefault) } // PostgreSQL leaves `column_default` null for an identity column, so a new row sent an explicit NULL it refuses. @@ -133,7 +133,7 @@ struct RowOperationsManagerTests { let result = manager.addNewRow(tableRows: &tableRows) - #expect(result?.values[0] == "__DEFAULT__") + #expect(result?.values[0] == .columnDefault) #expect(result?.values[1] == .null) } @@ -146,7 +146,7 @@ struct RowOperationsManagerTests { let result = manager.addNewRow(tableRows: &tableRows) - #expect(result?.values[2] == "__DEFAULT__") + #expect(result?.values[2] == .columnDefault) #expect(result?.values[1] == .null) } @@ -159,7 +159,7 @@ struct RowOperationsManagerTests { let result = manager.addNewRow(tableRows: &tableRows) #expect(result?.values[1] == .null) - #expect(result?.values[2] == "__DEFAULT__") + #expect(result?.values[2] == .columnDefault) #expect(tableRows.serverAssignsValue(forColumn: "name") == false) } @@ -246,7 +246,7 @@ struct RowOperationsManagerTests { ) #expect(result != nil) - #expect(result?.values[0] == "__DEFAULT__") + #expect(result?.values[0] == .columnDefault) if case .rowsInserted(let indices) = result?.delta { #expect(indices == IndexSet(integer: 3)) } else { @@ -263,10 +263,40 @@ struct RowOperationsManagerTests { let result = manager.duplicateRow(sourceRowIndex: 1, tableRows: &tableRows) - #expect(result?.values[2] == "__DEFAULT__") + #expect(result?.values[2] == .columnDefault) #expect(result?.values[1] == sourceValues[1]) } + @Test("The change manager names the cells a new row leaves to their default, for the JSON view and Copy as JSON") + func defaultedCellsOfANewRow() throws { + let (manager, changeManager) = makeManager() + var tableRows = emptyTableRows(columnDefaults: ["id": "auto_increment", "email": "'x'"]) + + let added = manager.addNewRow(tableRows: &tableRows) + let result = try #require(added) + + #expect(changeManager.defaultedCells(in: tableRows) == [result.rowID: [0, 2]]) + } + + @Test("A duplicate writes stored text that reads like the default as text, and leaves only its key to the server") + func duplicateRowKeepsDefaultLookingText() throws { + let (manager, changeManager) = makeManager() + var tableRows = TableRows.from( + queryRows: [["7", "__DEFAULT__", "DEFAULT"]], + columns: Self.testColumns, + columnTypes: Self.testColumnTypes + ) + + let duplicated = manager.duplicateRow(sourceRowIndex: 0, tableRows: &tableRows) + let result = try #require(duplicated) + + #expect(changeManager.expressionColumns(forRow: result.rowID) == [0]) + let statements = try changeManager.generateSQL() + #expect(statements.count == 1) + #expect(statements.first?.sql.contains("`id`") == false) + #expect(statements.first?.parameters.compactMap { $0 as? String } == ["__DEFAULT__", "DEFAULT"]) + } + @Test("duplicateRow returns nil for invalid source index") func duplicateRowReturnsNilForInvalidIndex() { let (manager, _) = makeManager() diff --git a/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift b/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift index 5601c64f35..4122e13d78 100644 --- a/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift @@ -80,7 +80,7 @@ struct RowOperationsManagerUUIDKeyTests { let result = try #require(manager.addNewRow(tableRows: &rows, uuidKeyForms: Self.uuidKey)) - #expect(result.values[0] == .text("__DEFAULT__")) + #expect(result.values[0] == .columnDefault) #expect(result.prefilledColumns.isEmpty) } @@ -99,7 +99,7 @@ struct RowOperationsManagerUUIDKeyTests { #expect(copyKey != sourceKey) #expect(copy.values[1] == .text("a")) #expect(copy.prefilledColumns == [0]) - #expect(plain.values[0] == .text("__DEFAULT__")) + #expect(plain.values[0] == .columnDefault) } @Test("Duplicating a pending row keeps the SQL staged in it, except in the columns the copy resets") @@ -118,14 +118,16 @@ struct RowOperationsManagerUUIDKeyTests { #expect(uuidText(copy.values[0]) != nil) } - @Test("Duplicating a saved row stages no SQL") + @Test("Duplicating a saved row stages no SQL but the default its reset key takes") func duplicateOfASavedRowHasNoMarks() throws { let (manager, changeManager) = makeManager() var rows = makeRows([[.text("0b1e7c9a-58f4-4b8e-9c55-1f1c86f0a001"), .text("now()"), .null]]) let copy = try #require(manager.duplicateRow(sourceRowIndex: 0, tableRows: &rows)) - #expect(changeManager.expressionColumns(forRow: copy.rowID).isEmpty) + #expect(changeManager.expressionColumns(forRow: copy.rowID) == [0]) + #expect(copy.values[0] == .columnDefault) + #expect(copy.values[1] == .text("now()")) } // MARK: - Paste rows @@ -188,7 +190,7 @@ struct RowOperationsManagerUUIDKeyTests { clipboard: clipboard ) - #expect(result.pastedRows.first?.values[0] == .text("__DEFAULT__")) + #expect(result.pastedRows.first?.values[0] == .columnDefault) } } @@ -249,8 +251,8 @@ extension GlobalStateSuites { let pasted = result.pastedRows.map(\.rowID) #expect(pasted.count == 2) - #expect(changeManager.expressionColumns(forRow: pasted[0]).isEmpty) - #expect(changeManager.expressionColumns(forRow: pasted[1]) == [2]) + #expect(changeManager.expressionColumns(forRow: pasted[0]) == [0]) + #expect(changeManager.expressionColumns(forRow: pasted[1]) == [0, 2]) #expect(result.pastedRows[0].values[2] == .text("CURRENT_TIMESTAMP")) #expect(result.pastedRows[1].values[2] == .text("now()")) } @@ -268,8 +270,9 @@ extension GlobalStateSuites { let result = try paste(manager, into: &rows) - let rowID = try #require(result.pastedRows.first?.rowID) - #expect(changeManager.expressionColumns(forRow: rowID) == [2]) + let pastedRow = try #require(result.pastedRows.first) + #expect(pastedRow.values[0] == .columnDefault) + #expect(changeManager.expressionColumns(forRow: pastedRow.rowID) == [0, 2]) } @Test("Cells the grid copied as wide as the table paste as rows with their marks") @@ -287,8 +290,8 @@ extension GlobalStateSuites { let pasted = result.pastedRows.map(\.rowID) #expect(pasted.count == 2) - #expect(changeManager.expressionColumns(forRow: pasted[0]) == [2]) - #expect(changeManager.expressionColumns(forRow: pasted[1]).isEmpty) + #expect(changeManager.expressionColumns(forRow: pasted[0]) == [0, 2]) + #expect(changeManager.expressionColumns(forRow: pasted[1]) == [0]) } } } diff --git a/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift b/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift index 9c1a9b1dd4..e886b4cf83 100644 --- a/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift +++ b/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift @@ -22,8 +22,8 @@ struct RowPasteForeignTextTests { let rows = try parse("\tAnn\tann@x.com\n\tBob\tbob@x.com") #expect(rows == [ - [.text("__DEFAULT__"), .text("Ann"), .text("ann@x.com")], - [.text("__DEFAULT__"), .text("Bob"), .text("bob@x.com")] + [.columnDefault, .text("Ann"), .text("ann@x.com")], + [.columnDefault, .text("Bob"), .text("bob@x.com")] ]) } @@ -74,7 +74,8 @@ struct RowPasteForeignTextTests { columns: Self.columns, primaryKeyColumns: ["id"], tableRows: &rows, clipboard: clipboard ) - #expect(result.pastedRows.first?.values == [.text("__DEFAULT__"), .text("Ann"), .text("ann@x.com")]) + #expect(result.pastedRows.first?.values == [.columnDefault, .text("Ann"), .text("ann@x.com")]) + #expect(result.pastedRows.first.map { changeManager.expressionColumns(forRow: $0.rowID) } == [0]) } @Test("A blank pasted cell takes the column's default, an empty string where NULL is refused, else NULL") @@ -107,11 +108,12 @@ struct RowPasteForeignTextTests { columns: columns, primaryKeyColumns: ["id"], tableRows: &rows, clipboard: clipboard ) - let unfilled: [PluginCellValue] = [.text("__DEFAULT__"), .text(""), .null, .null] + let unfilled: [PluginCellValue] = [.columnDefault, .text(""), .null, .null] #expect(result.pastedRows.map { $0.values } == [ - [.text("__DEFAULT__"), .text("Ann")] + unfilled, - [.text("__DEFAULT__"), .text("Bob")] + unfilled, + [.columnDefault, .text("Ann")] + unfilled, + [.columnDefault, .text("Bob")] + unfilled, ]) + #expect(result.pastedRows.map { changeManager.expressionColumns(forRow: $0.rowID) } == [[0, 2], [0, 2]]) } @Test("A pasted NULL stays NULL in a column with a default") @@ -136,7 +138,7 @@ struct RowPasteForeignTextTests { columns: columns, primaryKeyColumns: ["id"], tableRows: &rows, clipboard: clipboard ) - #expect(result.pastedRows.first?.values == [.text("__DEFAULT__"), .null]) + #expect(result.pastedRows.first?.values == [.columnDefault, .null]) } } diff --git a/apple/TableProTests/Core/Shortcuts/PayloadRowPlannerHeaderMatchTests.swift b/apple/TableProTests/Core/Shortcuts/PayloadRowPlannerHeaderMatchTests.swift new file mode 100644 index 0000000000..a34eb3e553 --- /dev/null +++ b/apple/TableProTests/Core/Shortcuts/PayloadRowPlannerHeaderMatchTests.swift @@ -0,0 +1,59 @@ +// +// PayloadRowPlannerHeaderMatchTests.swift +// TableProTests +// + +import Foundation +import Testing + +@testable import TablePro + +struct PayloadRowPlannerHeaderMatchTests { + private func columns(_ names: [String]) -> [PayloadColumn] { + names.map { PayloadColumn(name: $0, isPrimaryKey: false, isServerOwned: false) } + } + + private func planned(_ rows: [PayloadRow], into names: [String]) throws -> [[String]] { + try PayloadRowPlanner.values(table: "people", columns: columns(names), rows: rows) + .map { row in row.map { "\($0.column)=\($0.value ?? "NULL")" } } + } + + // SQL Server exports its headers bracketed, and Shortcuts has no sheet to map them by hand. + @Test("A CSV with bracketed headers inserts into the columns they name") + func bracketedHeadersFindTheirColumns() async throws { + let rows = try await RowPayload.parse(data: "[id],[name]\n1,Bob\n", file: nil) + #expect(try planned(rows, into: ["id", "name"]) == [["id=1", "name=Bob"]]) + } + + @Test("A doubled closing bracket inside the brackets is one bracket of the name") + func doubledBracketIsUnescaped() throws { + let row = PayloadRow(values: ["[a]]b]": .text("x")]) + #expect(try planned([row], into: ["a]b"]) == [["a]b=x"]]) + } + + @Test("A lone closing bracket inside means the key is not a bracketed name") + func loneBracketIsNotUnwrapped() { + #expect(throws: IntentDataError.unknownColumns(["[a]b]"], "people")) { + _ = try planned([PayloadRow(values: ["[a]b]": .text("x")])], into: ["a]b"]) + } + } + + @Test("A key differing only in case finds its column, as the import sheet matches headers") + func caseInsensitiveMatch() throws { + #expect(try planned([PayloadRow(values: ["Name": .text("Ada")])], into: ["name"]) == [["name=Ada"]]) + } + + @Test("A case match two columns share is refused rather than guessed") + func ambiguousCaseMatchIsUnknown() { + #expect(throws: IntentDataError.unknownColumns(["NAME"], "people")) { + _ = try planned([PayloadRow(values: ["NAME": .text("Ada")])], into: ["Name", "name"]) + } + } + + @Test("A key spelled exactly claims its column before a bracketed one can") + func exactSpellingClaimsFirst() { + #expect(throws: IntentDataError.unknownColumns(["[id]"], "people")) { + _ = try planned([PayloadRow(values: ["id": .text("1"), "[id]": .text("2")])], into: ["id"]) + } + } +} diff --git a/apple/TableProTests/Core/Shortcuts/RowPayloadFormatTests.swift b/apple/TableProTests/Core/Shortcuts/RowPayloadFormatTests.swift new file mode 100644 index 0000000000..f5f9743a48 --- /dev/null +++ b/apple/TableProTests/Core/Shortcuts/RowPayloadFormatTests.swift @@ -0,0 +1,60 @@ +// +// RowPayloadFormatTests.swift +// TableProTests +// + +import Foundation +import Testing + +@testable import TablePro + +struct RowPayloadFormatTests { + // SQL Server exports its headers bracketed, so a valid CSV opens with `[`. + @Test("CSV with bracketed headers is read as CSV") + func bracketedHeaderIsCSV() async throws { + let rows = try await RowPayload.parse(data: "[id],[name]\n1,Bob\n", file: nil) + #expect(rows == [PayloadRow(values: ["[id]": .text("1"), "[name]": .text("Bob")])]) + } + + @Test("CSV whose first header opens with a brace is read as CSV") + func braceHeaderIsCSV() async throws { + let rows = try await RowPayload.parse(data: "{key},value\na,1\n", file: nil) + #expect(rows == [PayloadRow(values: ["{key}": .text("a"), "value": .text("1")])]) + } + + @Test("A JSON object and a JSON array of objects are still read as JSON") + func jsonStaysJSON() async throws { + let object = try await RowPayload.parse(data: #"{"name":"Ada"}"#, file: nil) + #expect(object == [PayloadRow(values: ["name": .text("Ada")])]) + let array = try await RowPayload.parse(data: #"[{"name":"Ada"},{"name":"Grace"}]"#, file: nil) + #expect(array.count == 2) + } + + @Test("Malformed JSON that is no CSV either reports the JSON error") + func malformedJSONKeepsItsError() async { + await #expect { + _ = try await RowPayload.parse(data: #"{"name": tru}"#, file: nil) + } throws: { error in + guard case .malformedPayload = error as? IntentDataError else { return false } + return true + } + } + + // Each line splits into the same number of CSV fields, so the text read as CSV with `{"id": 1` for a header. + @Test("JSON with a trailing comma after each line's object keeps the JSON error") + func jsonThatSplitsEvenlyKeepsItsError() async { + await #expect { + _ = try await RowPayload.parse(data: "{\"id\": 1, \"name\": \"a\"},\n{\"id\": 2, \"name\": \"b\"},\n", file: nil) + } throws: { error in + guard case .malformedPayload = error as? IntentDataError else { return false } + return true + } + } + + @Test("A JSON array of something other than objects keeps its own error") + func jsonArrayOfValuesIsNotRetriedAsCSV() async { + await #expect(throws: IntentDataError.jsonArrayContainsNonObject) { + _ = try await RowPayload.parse(data: "[1,2]", file: nil) + } + } +} diff --git a/apple/TableProTests/Core/UsersRoles/PrincipalApplyOpenTransactionTests.swift b/apple/TableProTests/Core/UsersRoles/PrincipalApplyOpenTransactionTests.swift index 4cb996f0e4..0ee582a754 100644 --- a/apple/TableProTests/Core/UsersRoles/PrincipalApplyOpenTransactionTests.swift +++ b/apple/TableProTests/Core/UsersRoles/PrincipalApplyOpenTransactionTests.swift @@ -62,3 +62,47 @@ struct PrincipalApplyOpenTransactionTests { #expect(driver.executedQueries.isEmpty) } } + +// The review sheet showed a Cancel at the gate as a failed apply. +@MainActor +struct PrincipalApplyGateDenialTests { + private func apply(gate: any ExecutionGate) async -> (failure: GatedOperationFailure?, executed: [String]) { + let driver = TransactionHoldingPrincipalDriver(rollsBack: true) + let connection = TestFixtures.makeConnection(type: .postgresql) + DatabaseManager.shared.injectSession( + ConnectionSession(connection: connection, driver: PluginDriverAdapter(connection: connection, pluginDriver: driver)), + for: connection.id + ) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + let change = PrincipalChange.create(PluginPrincipalDefinition(ref: PluginPrincipalRef(name: "X"), password: "p")) + do { + try await DatabaseManager.shared.executePrincipalChanges( + changes: [change], + databaseType: .postgresql, + connectionId: connection.id, + gate: gate + ) + return (nil, driver.executedQueries) + } catch { + return (GatedOperationFailure(error), driver.executedQueries) + } + } + + @Test("A Cancel at the gate ends the apply quietly, with nothing run") + func cancelIsQuiet() async { + let result = await apply(gate: AlwaysDenyGate.cancelling) + + #expect(result.failure == .cancelledByUser) + #expect(result.failure?.message == nil) + #expect(result.executed.isEmpty) + } + + @Test("A refused apply says why and is not a failed statement") + func refusalExplainsItself() async { + let result = await apply(gate: AlwaysDenyGate(reason: "Safe Mode is read-only")) + + #expect(result.failure == .refused("Safe Mode is read-only")) + #expect(result.executed.isEmpty) + } +} diff --git a/apple/TableProTests/Core/UsersRoles/PrincipalChangeManagerTests.swift b/apple/TableProTests/Core/UsersRoles/PrincipalChangeManagerTests.swift index fcdd2c5053..d8c628c9eb 100644 --- a/apple/TableProTests/Core/UsersRoles/PrincipalChangeManagerTests.swift +++ b/apple/TableProTests/Core/UsersRoles/PrincipalChangeManagerTests.swift @@ -329,6 +329,46 @@ struct PrincipalChangeManagerTests { #expect(manager.changeCount == 1) } + @Test("A staged drop runs only the DROP and counts as one change") + func dropSupersedesOtherEdits() { + let manager = makeManager() + manager.stageAlter(PluginPrincipalDefinition(ref: alice, canLogin: false), for: alice) + manager.stageSetPassword("s3cret", for: alice) + manager.setGranted(true, privilege: "CREATE", scope: app, for: alice) + #expect(manager.changeCount == 3) + + manager.stageDrop(alice, options: PluginPrincipalDropOptions()) + + let changes = manager.pendingChanges() + #expect(changes.count == 1) + guard case .drop(let ref, _) = changes.first else { + Issue.record("Expected only the DROP, got \(changes)") + return + } + #expect(ref == alice) + #expect(manager.changeCount == 1) + } + + @Test("Undoing a staged drop brings the principal's other edits back") + func unstagingDropRestoresEdits() { + let manager = makeManager() + manager.stageAlter(PluginPrincipalDefinition(ref: alice, canLogin: false), for: alice) + manager.stageSetPassword("s3cret", for: alice) + manager.setGranted(true, privilege: "CREATE", scope: app, for: alice) + manager.stageDrop(alice, options: PluginPrincipalDropOptions()) + + manager.unstageDrop(alice) + + let changes = manager.pendingChanges() + let dropsAnything = changes.contains { change in + if case .drop = change { return true } + return false + } + #expect(changes.count == 3) + #expect(!dropsAnything) + #expect(manager.changeCount == 3) + } + @Test("Dropping the connected account is reported as self-impact") func reportsSelfImpact() { let manager = makeManager() diff --git a/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift b/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift index 759614a413..0e3f59c5d3 100644 --- a/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift +++ b/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift @@ -384,20 +384,16 @@ struct JsonRowConverterTests { // MARK: - Default-value marker - /// A pending inserted row carries the default marker for columns the server fills in. The grid - /// draws it as a placeholder, so JSON has to agree instead of printing the internal token. - @Test("The default-value marker renders as null, not as literal text") - func defaultMarkerRendersAsNull() { + // The default is marked beside the value, so text a table holds that spells the old marker is a value. + @Test("Stored text that spells the old default marker renders as that text") + func oldMarkerTextIsAValue() { let converter = makeConverter( - columns: ["id", "name"], - columnTypes: [.integer(rawType: nil), .text(rawType: nil)] - ) - let result = converter.generateJson( - rows: [[.text(PluginCellValue.defaultMarkerText), .text("Ada")]] + columns: ["status", "name"], + columnTypes: [.text(rawType: nil), .text(rawType: nil)] ) + let result = converter.generateJson(rows: [[.text("__DEFAULT__"), .text("Ada")]]) - #expect(!result.contains(PluginCellValue.defaultMarkerText)) - #expect(result.contains("\"id\": null")) + #expect(result.contains("\"status\": \"__DEFAULT__\"")) #expect(result.contains("\"Ada\"")) } diff --git a/apple/TableProTests/Core/Utilities/SQL/QueryClassifierTSQLBlockTests.swift b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierTSQLBlockTests.swift new file mode 100644 index 0000000000..86fc85825a --- /dev/null +++ b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierTSQLBlockTests.swift @@ -0,0 +1,39 @@ +import Foundation +@testable import TablePro +import TableProSQLGrammar +import Testing + +// A T-SQL block runs as one statement, and a caller allowed one statement at a time is still refused one holding more. +struct QueryClassifierTSQLBlockTests { + @Test("A block holding more than one statement is multi-statement", arguments: [ + "IF @x = 1 BEGIN UPDATE a SET c = 1; UPDATE b SET c = 1; END", + "BEGIN TRY DELETE FROM a WHERE id = 1; DELETE FROM b WHERE id = 1; END TRY BEGIN CATCH SELECT 1; END CATCH", + "WHILE @i < 3 BEGIN SET @i += 1; PRINT @i; END", + "IF @x = 1 BEGIN SELECT 1; WITH c AS (SELECT id FROM t) DELETE FROM c; END", + "IF @x = 1 BEGIN SELECT 1; DISABLE TRIGGER trg ON t; END", + "BEGIN TRY SELECT 1; THROW 50000, 'x', 1; END TRY BEGIN CATCH SELECT 2; END CATCH", + "IF @x = 1 BEGIN SELECT 1; done: SELECT 2; END", + "IF @x = 1 BEGIN SELECT 1; (SELECT 2); END", + ]) + func blockOfSeveralIsMulti(sql: String) { + #expect(QueryClassifier.isMultiStatement(sql, databaseType: .mssql)) + } + + @Test("A conditional statement, or a block holding one, is a single statement", arguments: [ + "IF @x = 1 SELECT 1", + "IF @x = 1 BEGIN SELECT 1; END", + "BEGIN SELECT 1 END", + "BEGIN IF @x = 1 SELECT 1; ELSE SELECT 2; END", + "BEGIN TRY SELECT 1; END TRY BEGIN CATCH THROW; END CATCH", + ]) + func singleStatementIsNotMulti(sql: String) { + #expect(!QueryClassifier.isMultiStatement(sql, databaseType: .mssql)) + } + + @Test("A stored routine's body runs nothing, so it is one statement however many it holds") + func routineBodyIsNotMulti() { + #expect(!QueryClassifier.isMultiStatement( + "CREATE PROCEDURE dbo.p AS BEGIN UPDATE a SET c = 1; UPDATE b SET c = 1; END", databaseType: .mssql + )) + } +} diff --git a/apple/TableProTests/Core/Utilities/SQLRowToStatementConverterTests.swift b/apple/TableProTests/Core/Utilities/SQLRowToStatementConverterTests.swift index b5df30bfd0..8fc75fa0d1 100644 --- a/apple/TableProTests/Core/Utilities/SQLRowToStatementConverterTests.swift +++ b/apple/TableProTests/Core/Utilities/SQLRowToStatementConverterTests.swift @@ -384,8 +384,9 @@ struct SQLRowToStatementConverterTests { #expect(lines.count == 50_000) } - @Test("PostgreSQL: binary cell renders as bytea hex literal in INSERT") - func postgresBinaryInsertEmitsByteaLiteral() throws { + // `'\x…'::bytea` reads its backslash by `standard_conforming_strings`; `decode` means the same bytes either way. + @Test("PostgreSQL: binary cell decodes hex in INSERT") + func postgresBinaryInsertDecodesHex() throws { let converter = try SQLRowToStatementConverter( tableName: "documents", columns: ["id", "payload"], @@ -396,10 +397,28 @@ struct SQLRowToStatementConverterTests { ) let bytes = Data([0xD3, 0x8C, 0xE5, 0x66]) let result = converter.generateInserts(rows: plain([[.text("1"), .bytes(bytes)]])) - #expect(result.contains("'\\xD38CE566'::bytea")) + #expect(result.contains("decode('D38CE566', 'hex')")) #expect(!result.contains("NULL")) } + // DuckDB reads `X'…'` as the text `x…` and Oracle has no such literal. + @Test("DuckDB and Oracle binary cells use their own hex functions in INSERT", arguments: [ + (DatabaseType.duckdb, "unhex('CAFE')"), (DatabaseType.oracle, "HEXTORAW('CAFE')"), + ]) + func binaryInsertUsesTheEnginesHexFunction(databaseType: DatabaseType, literal: String) throws { + let converter = try SQLRowToStatementConverter( + tableName: "documents", + columns: ["id", "payload"], + primaryKeyColumns: ["id"], + databaseType: databaseType, + quoteIdentifier: { "\"\($0)\"" }, + escapeStringLiteral: { $0.replacingOccurrences(of: "'", with: "''") } + ) + let result = converter.generateInserts(rows: plain([[.text("1"), .bytes(Data([0xCA, 0xFE]))]])) + #expect(result.contains(literal)) + #expect(!result.contains("X'CAFE'")) + } + @Test("MySQL: binary cell renders as X'...' literal in INSERT") func mysqlBinaryInsertEmitsXLiteral() throws { let converter = try makeConverter( @@ -441,7 +460,7 @@ struct SQLRowToStatementConverterTests { ) let bytes = Data([0xAB, 0xCD]) let result = converter.generateUpdates(rows: plain([[.text("42"), .bytes(bytes)]])) - #expect(result.contains("\"payload\" = '\\xABCD'::bytea")) + #expect(result.contains("\"payload\" = decode('ABCD', 'hex')")) #expect(result.contains("WHERE \"id\" = '42'")) } @@ -656,8 +675,8 @@ struct SQLRowToStatementConverterTests { databaseType: .pglite, dialect: Self.postgresDialect ) let rows = plain([[.text("1"), .bytes(Data([0xDE, 0xAD]))]]) - #expect(converter.generateInserts(rows: rows).contains("'\\xDEAD'::bytea")) - #expect(converter.generateUpdates(rows: rows).contains("\"payload\" = '\\xDEAD'::bytea")) + #expect(converter.generateInserts(rows: rows).contains("decode('DEAD', 'hex')")) + #expect(converter.generateUpdates(rows: rows).contains("\"payload\" = decode('DEAD', 'hex')")) #expect(!converter.generateInserts(rows: rows).contains("X'DEAD'")) } } diff --git a/apple/TableProTests/Core/Vim/VimTextBufferAdapterPerfTests.swift b/apple/TableProTests/Core/Vim/VimTextBufferAdapterPerfTests.swift index 6fadfaf773..48c97aac20 100644 --- a/apple/TableProTests/Core/Vim/VimTextBufferAdapterPerfTests.swift +++ b/apple/TableProTests/Core/Vim/VimTextBufferAdapterPerfTests.swift @@ -2,8 +2,7 @@ // VimTextBufferAdapterPerfTests.swift // TableProTests // -// Regression tests for VimTextBufferAdapter incremental lineCount -// and setSelectedRange guard +// Regression tests for VimTextBufferAdapter lineCount and setSelectedRange guard // import AppKit @@ -65,83 +64,29 @@ struct VimTextBufferAdapterPerfTests { #expect(adapter.lineCount == 2) } - // MARK: - textDidChange incremental (pure insertion) - - @Test("textDidChange with pure insertion updates line count incrementally") - func insertionUpdatesLineCount() { - let (adapter, textView) = makeAdapter(string: "hello") - - // Prime the cache - let initial = adapter.lineCount - #expect(initial == 1) - - // Simulate inserting "\nworld" at offset 5 - textView.replaceCharacters(in: NSRange(location: 5, length: 0), with: "\nworld") - adapter.textDidChange(in: NSRange(location: 5, length: 0), replacementLength: 6) - - #expect(adapter.lineCount == 2) - } - - @Test("textDidChange with multi-newline insertion") - func multiNewlineInsertion() { - let (adapter, textView) = makeAdapter(string: "hello") - - _ = adapter.lineCount // prime cache - - textView.replaceCharacters(in: NSRange(location: 5, length: 0), with: "\n\n\n") - adapter.textDidChange(in: NSRange(location: 5, length: 0), replacementLength: 3) - - #expect(adapter.lineCount == 4) - } - - // MARK: - textDidChange fallback on deletion - - @Test("textDidChange with deletion invalidates cache") - func deletionInvalidatesCache() { - let (adapter, textView) = makeAdapter(string: "a\nb\nc") - - let initial = adapter.lineCount - #expect(initial == 3) - - // Simulate deleting "\nb" (range.length > 0 means it's not pure insertion) - textView.replaceCharacters(in: NSRange(location: 1, length: 2), with: "") - adapter.textDidChange(in: NSRange(location: 1, length: 2), replacementLength: 0) - - // Cache should be invalidated; next access does a full recount - #expect(adapter.lineCount == 2) - } - - // MARK: - textDidChange(oldText:...) incremental replacement - - @Test("textDidChange with oldText correctly computes delta for replacement") - func oldTextReplacementDelta() { - let originalText = "a\nb\nc" - let (adapter, textView) = makeAdapter(string: originalText) - - let initial = adapter.lineCount - #expect(initial == 3) - - // Replace "b\nc" (range 2..4, contains 1 newline) with "x" (0 newlines) - textView.replaceCharacters(in: NSRange(location: 2, length: 3), with: "x") - adapter.textDidChange(oldText: originalText, in: NSRange(location: 2, length: 3), replacementLength: 1) - - // 3 original - 1 removed newline + 0 added = 2 - #expect(adapter.lineCount == 2) - } - - @Test("textDidChange with oldText adding newlines") - func oldTextAddingNewlines() { - let originalText = "abc" - let (adapter, textView) = makeAdapter(string: originalText) - - _ = adapter.lineCount // prime: 1 - - // Replace "b" with "x\ny\nz" (adding 2 newlines) - textView.replaceCharacters(in: NSRange(location: 1, length: 1), with: "x\ny\nz") - adapter.textDidChange(oldText: originalText, in: NSRange(location: 1, length: 1), replacementLength: 5) - - // 1 original - 0 removed + 2 added = 3 - #expect(adapter.lineCount == 3) + // MARK: - Line count after an edit + + // The editor drops the cache on every edit, so the next count reads the text as it is now. + @Test("After an edit the line count matches a fresh count of the new text", arguments: [ + (NSRange(location: 5, length: 0), "\nworld", "hello", 2), + (NSRange(location: 5, length: 0), "\n\n\n", "hello", 3), + (NSRange(location: 1, length: 2), "", "a\nb\nc", 2), + (NSRange(location: 2, length: 3), "x", "a\nb\nc", 2), + (NSRange(location: 1, length: 1), "x\ny\nz", "abc", 3), + (NSRange(location: 1, length: 0), "\r", "ab", 2), + ]) + func lineCountAfterAnEdit(range: NSRange, replacement: String, original: String, expected: Int) { + let (adapter, textView) = makeAdapter(string: original) + _ = adapter.lineCount + + textView.replaceCharacters(in: range, with: replacement) + adapter.invalidateLineCache() + + let counted = withExtendedLifetime(textView) { adapter.lineCount } + let (fresh, freshTextView) = makeAdapter(string: textView.string) + let freshCount = withExtendedLifetime(freshTextView) { fresh.lineCount } + #expect(counted == expected) + #expect(counted == freshCount) } // MARK: - setSelectedRange guard diff --git a/apple/TableProTests/Helpers/StubExecutionGates.swift b/apple/TableProTests/Helpers/StubExecutionGates.swift index 83dce282ed..afcfa85d48 100644 --- a/apple/TableProTests/Helpers/StubExecutionGates.swift +++ b/apple/TableProTests/Helpers/StubExecutionGates.swift @@ -27,13 +27,18 @@ internal struct AlwaysAllowGate: ExecutionGate { internal struct AlwaysDenyGate: ExecutionGate { internal let reason: String + internal let cause: OperationDenialCause - internal init(reason: String = "Read-Only connection") { + internal init(reason: String = "Read-Only connection", cause: OperationDenialCause = .policy) { self.reason = reason + self.cause = cause } + // The user answering Cancel in the gate's confirmation. + internal static let cancelling = AlwaysDenyGate(reason: "Operation cancelled by user", cause: .cancelledByUser) + internal func authorize(_ request: OperationRequest) async -> OperationDecision { - .denied(reason: reason) + .denied(reason: reason, cause: cause) } } diff --git a/apple/TableProTests/Models/MultiRowEditStateTests.swift b/apple/TableProTests/Models/MultiRowEditStateTests.swift index ce162e198a..1cdb06f345 100644 --- a/apple/TableProTests/Models/MultiRowEditStateTests.swift +++ b/apple/TableProTests/Models/MultiRowEditStateTests.swift @@ -95,14 +95,14 @@ struct MultiRowEditStateTests { #expect(field.effectiveValue == nil) } - @Test("effectiveValue returns __DEFAULT__ when isPendingDefault") + @Test("effectiveValue returns the DEFAULT keyword when isPendingDefault") func effectiveValueReturnsDefaultWhenPendingDefault() { let field = FieldEditState( columnIndex: 0, columnName: "id", columnTypeEnum: .text(rawType: nil), isLongText: false, isJson: false, originalValue: "1", hasMultipleValues: false, pendingValue: nil, isPendingNull: false, isPendingDefault: true ) - #expect(field.effectiveValue == "__DEFAULT__") + #expect(field.effectiveValue == "DEFAULT") } @Test("effectiveValue returns nil when no edit is pending") @@ -644,13 +644,14 @@ struct MultiRowEditStateTests { #expect(edited[0].newValue == nil) } - @Test("Returns __DEFAULT__ for default edit") + @Test("Returns the DEFAULT keyword marked as SQL for a default edit") func returnsDefaultForDefaultEdit() { let sut = makeSUT() sut.setFieldToDefault(at: 0) let edited = sut.getEditedFields() #expect(edited.count == 1) - #expect(edited[0].newValue == "__DEFAULT__") + #expect(edited[0].newValue == "DEFAULT") + #expect(edited[0].isExpression) } @Test("Returns empty array when no edits") @@ -774,18 +775,22 @@ struct MultiRowEditStateTests { #expect(callbackCalls[0].value == nil) } - @Test("setFieldToDefault fires callback with __DEFAULT__") + // Through `onFieldChanged` the keyword would be a literal, which a stored value can also be. + @Test("setFieldToDefault sends the DEFAULT keyword through the expression callback") func setFieldToDefaultFiresCallback() { let sut = makeSUT() - var callbackCalls: [(index: Int, value: String?)] = [] - sut.onFieldChanged = { index, value, _ in - callbackCalls.append((index, value.asText)) + var literals: [PluginCellValue] = [] + var callbackCalls: [(index: Int, sql: String)] = [] + sut.onFieldChanged = { _, value, _ in literals.append(value) } + sut.onFieldExpressionChanged = { index, sql, _ in + callbackCalls.append((index, sql)) } sut.setFieldToDefault(at: 0) + #expect(literals.isEmpty) #expect(callbackCalls.count == 1) #expect(callbackCalls[0].index == 0) - #expect(callbackCalls[0].value == "__DEFAULT__") + #expect(callbackCalls[0].sql == "DEFAULT") } // `onFieldChanged` carries a literal, so an expression sent through it would be saved as quoted text. diff --git a/apple/TableProTests/Models/Schema/ClickHousePartStatementsTests.swift b/apple/TableProTests/Models/Schema/ClickHousePartStatementsTests.swift index cc67c4e094..cdee26b95c 100644 --- a/apple/TableProTests/Models/Schema/ClickHousePartStatementsTests.swift +++ b/apple/TableProTests/Models/Schema/ClickHousePartStatementsTests.swift @@ -2,9 +2,6 @@ // ClickHousePartStatementsTests.swift // TableProTests // -// The Parts tab's statements name the tab's own database, because ClickHouse resolves an -// unqualified one from a request parameter the app moves whenever any tab runs elsewhere. -// import Foundation @testable import TablePro @@ -14,8 +11,7 @@ struct ClickHousePartStatementsTests { private let quote: (String) -> String = { "`\($0.replacingOccurrences(of: "`", with: "``"))`" } private let escape: (String) -> String = { $0.replacingOccurrences(of: "'", with: "\\'") } - /// The defect: an unqualified DROP PARTITION deleted data from whichever database the shared - /// driver was last pinned to, while the tab reported it was acting on its own. + // An unqualified DROP PARTITION deleted data from whichever database the shared driver was last pinned to. @Test("Every statement names the database it was given") func statementsCarryTheDatabase() { #expect( @@ -24,20 +20,59 @@ struct ClickHousePartStatementsTests { ) #expect( ClickHousePartStatements.dropPartition( - database: "analytics", table: "events", partition: "202401", - quote: quote, escape: escape - ) == "ALTER TABLE `analytics`.`events` DROP PARTITION '202401'" + database: "analytics", table: "events", partitionId: "202401", quote: quote + ) == "ALTER TABLE `analytics`.`events` DROP PARTITION ID '202401'" ) #expect( ClickHousePartStatements.detachPartition( - database: "analytics", table: "events", partition: "202401", - quote: quote, escape: escape - ) == "ALTER TABLE `analytics`.`events` DETACH PARTITION '202401'" + database: "analytics", table: "events", partitionId: "202401", quote: quote + ) == "ALTER TABLE `analytics`.`events` DETACH PARTITION ID '202401'" ) } - /// `currentDatabase()` answers with the request parameter, so the list showed another database's - /// parts under this table's name. + // A tuple key's rendered value `(202401,'eu')` and an unpartitioned table's `tuple()` were each + // wrapped as one string literal, which names no partition. + @Test("Drop and Detach name the partition by its ID for every key shape") + func everyKeyShapeUsesThePartitionId() { + let shapes = [ + "202401-7", + "all", + "4f8b2b7c9a1d3e5f60718293a4b5c6d7", + "1--5", + ] + for id in shapes { + #expect( + ClickHousePartStatements.dropPartition( + database: "analytics", table: "events", partitionId: id, quote: quote + ) == "ALTER TABLE `analytics`.`events` DROP PARTITION ID '\(id)'" + ) + #expect( + ClickHousePartStatements.detachPartition( + database: "analytics", table: "events", partitionId: id, quote: quote + ) == "ALTER TABLE `analytics`.`events` DETACH PARTITION ID '\(id)'" + ) + } + } + + @Test("An ID the server would refuse is never spliced into a statement") + func unusableIdBuildsNothing() { + let unusable = ["", "it's", "a\\'b", "(202401,'eu')", "tuple()", "2024 01", "202401_1", "é"] + for id in unusable { + #expect(!ClickHousePartStatements.isValidPartitionId(id)) + #expect( + ClickHousePartStatements.dropPartition( + database: "analytics", table: "events", partitionId: id, quote: quote + ) == nil + ) + #expect( + ClickHousePartStatements.detachPartition( + database: "analytics", table: "events", partitionId: id, quote: quote + ) == nil + ) + } + } + + // `currentDatabase()` answers with the request parameter, so the list showed another database's parts. @Test("The parts read filters on the named database, never the connection's current one") func partsReadNamesTheDatabase() { let sql = ClickHousePartStatements.parts( @@ -48,9 +83,13 @@ struct ClickHousePartStatementsTests { #expect(!sql.contains("currentDatabase()")) } - /// A connection saved with no database of its own has nothing to qualify against, and an empty - /// qualifier is a statement the server refuses. The parts read has to fall back too: comparing - /// against '' matches nothing, where ClickHouse's own default database is what the user sees. + @Test("The parts read selects the partition ID beside the displayed partition value") + func partsReadSelectsThePartitionId() { + let sql = ClickHousePartStatements.parts(database: "analytics", table: "events", escape: escape) + #expect(sql.contains("SELECT partition, partition_id, name, rows, bytes_on_disk,")) + } + + // With no database the qualifier is refused by the server, and comparing against '' matches nothing. @Test("With no database the name stays unqualified and the read asks the server") func emptyDatabaseLeavesTheNameBare() { #expect( @@ -66,13 +105,11 @@ struct ClickHousePartStatementsTests { #expect(!sql.contains("database = ''")) } - @Test("A quote in a partition value is escaped, and a backtick in a name is quoted") - func valuesAreEscaped() { + @Test("A backtick in a database or table name is quoted") + func namesAreQuoted() { let sql = ClickHousePartStatements.dropPartition( - database: "we`ird", table: "ev`ents", partition: "it's", - quote: quote, escape: escape + database: "we`ird", table: "ev`ents", partitionId: "202401", quote: quote ) - #expect(sql.contains("`we``ird`.`ev``ents`")) - #expect(sql.contains("'it\\'s'")) + #expect(sql == "ALTER TABLE `we``ird`.`ev``ents` DROP PARTITION ID '202401'") } } diff --git a/apple/TableProTests/Views/Editor/StatementCaretTableTests.swift b/apple/TableProTests/Views/Editor/StatementCaretTableTests.swift new file mode 100644 index 0000000000..ba5ea78102 --- /dev/null +++ b/apple/TableProTests/Views/Editor/StatementCaretTableTests.swift @@ -0,0 +1,109 @@ +import AppKit +import Foundation +@testable import TablePro +import TableProEditorKit +import TableProGrammars +import TableProSQLGrammar +import TableProTextEngine +import Testing + +private let caretTableMongoScript = """ +db.users.find({ name: "a;b" }); +// note + +for (var i = 0; i < 3; i++) { + print(i); +} +db.orders.insertOne({ total: 1 }) +db.orders.find() +// trailing +""" + +private let caretTableRedisScript = "GET a; GET b\n\nSET k \"a\nb\"\nGET c \n " + +@MainActor +struct StatementCaretTableTests { + private struct Resolved: Equatable { + let offset: Int + let sql: String + let hasContent: Bool + let contentRange: NSRange + + init(_ statement: SQLStatementScanner.LocatedStatement) { + offset = statement.offset + sql = statement.sql + hasContent = statement.hasContent + contentRange = statement.contentRange + } + } + + @Test("Every caret resolves to what a scan of the whole document resolves to", arguments: [ + (QueryStatementModel.javascript, caretTableMongoScript), + (QueryStatementModel.redis, caretTableRedisScript), + (QueryStatementModel.javascript, ""), + (QueryStatementModel.redis, ""), + ]) + func everyCaretMatchesAFullScan(model: QueryStatementModel, text: String) throws { + let table = try #require(QueryStatementCaretTable(text: text, model: model)) + for caret in -1...((text as NSString).length + 1) { + let fresh = QueryStatementScanner.locatedStatementAtCursor( + in: text, cursorPosition: caret, model: model, grammar: .ansi + ) + #expect(Resolved(table.statement(atCursor: caret)) == Resolved(fresh), "caret \(caret)") + } + } + + @Test("A SQL document has no table, since it resumes through its own index") + func sqlHasNoTable() { + #expect(QueryStatementCaretTable(text: "SELECT 1;", model: .sql) == nil) + } + + private func makeController(text: String) -> TextViewController { + let configuration = SourceEditorConfiguration( + appearance: .init( + theme: ThemeEngine.shared.makeEditorTheme(), + font: .monospacedSystemFont(ofSize: 12, weight: .regular), + wrapLines: false + ), + peripherals: EditorPeripherals.editor(lineNumbers: true, folding: true, statementRunControls: true) + ) + let controller = TextViewController(string: text, language: .sql, configuration: configuration, cursorPositions: []) + controller.loadView() + return controller + } + + private func band(at caret: Int, in controller: TextViewController, subject: StatementRunController) -> NSRange? { + controller.setCursorPositions([CursorPosition(range: NSRange(location: caret, length: 0))]) + subject.refreshHighlight(in: controller) + return controller.highlightedStatementRange + } + + private func freshBand(at caret: Int, in controller: TextViewController) -> NSRange? { + let statement = QueryStatementScanner.locatedStatementAtCursor( + in: controller.textView.string, cursorPosition: caret, model: .javascript, grammar: .ansi + ) + guard statement.hasContent, statement.contentRange.length > 0 else { return nil } + return statement.contentRange + } + + @Test("Caret moves through a MongoDB script read it once, and an edit reads it again") + func caretMovesReadTheScriptOnce() { + let script = String(repeating: "db.orders.find({ total: 1 });\n", count: 2_000) + let controller = makeController(text: script) + let subject = StatementRunController() + subject.statementModel = .javascript + subject.install(on: controller) + let readsAtInstall = subject.caretTableReads + + for caret in stride(from: 59_000, to: 58_000, by: -7) { + #expect(band(at: caret, in: controller, subject: subject) == freshBand(at: caret, in: controller)) + } + #expect(subject.caretTableReads <= readsAtInstall + 1) + + controller.textView.replaceCharacters(in: NSRange(location: 0, length: 0), with: "db.a.find();\n") + let afterEdit = subject.caretTableReads + #expect(band(at: 100, in: controller, subject: subject) == freshBand(at: 100, in: controller)) + #expect(band(at: 58_013, in: controller, subject: subject) == freshBand(at: 58_013, in: controller)) + #expect(subject.caretTableReads == afterEdit + 1) + } +} diff --git a/apple/TableProTests/Views/Main/TableLoadAfterSafeModePromptTests.swift b/apple/TableProTests/Views/Main/TableLoadAfterSafeModePromptTests.swift new file mode 100644 index 0000000000..920544485f --- /dev/null +++ b/apple/TableProTests/Views/Main/TableLoadAfterSafeModePromptTests.swift @@ -0,0 +1,40 @@ +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +struct TableLoadAfterSafeModePromptTests { + @Test("A table load turned away while a query waits for approval runs once that approval closes") + func turnedAwayLoadRunsAfterThePrompt() async throws { + let connection = DatabaseConnection( + name: "Confirm", database: "app", type: .postgresql, safeModeLevel: .alertFull + ) + let fixture = CoordinatorFixture( + connection: connection, + driver: ScriptAnsweringDriver(connection: connection, sendsBatchesWhole: false) + ) + fixture.coordinator.toolbarState.safeModeLevel = .alertFull + let gate = HeldExecutionGate() + fixture.coordinator.executionGate = gate + defer { fixture.coordinator.cancelAllQueryTasks() } + + fixture.addTab(QueryTab(title: "Query", query: "SELECT 1", tabType: .query)) + #expect(fixture.coordinator.runStatement("SELECT 1")) + try await waitUntil { gate.askedCount == 1 } + + var table = QueryTab(title: "users", query: "SELECT * FROM users LIMIT 1000", tabType: .table, tableName: "users") + table.pagination.isLoading = true + fixture.addTab(table) + #expect(fixture.coordinator.executeTableTabQueryDirectly(viewport: .firstRow) == nil) + #expect(fixture.tabManager.tabs.first { $0.id == table.id }?.pagination.isLoading == false) + + await gate.answer(allowing: true) + try await waitUntil { gate.askedCount == 2 } + await gate.answer(allowing: false) + try await waitUntil { + fixture.tabManager.tabs.first { $0.id == table.id }?.execution.errorMessage == "Refused by the test" + } + } +} diff --git a/apple/TableProTests/Views/Main/WindowCloseSaveEveryConnectionTests.swift b/apple/TableProTests/Views/Main/WindowCloseSaveEveryConnectionTests.swift new file mode 100644 index 0000000000..bb0a30c859 --- /dev/null +++ b/apple/TableProTests/Views/Main/WindowCloseSaveEveryConnectionTests.swift @@ -0,0 +1,129 @@ +import AppKit +import Foundation +import SwiftUI +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +struct WindowCloseSaveEveryConnectionTests { + private func actions( + for coordinator: MainContentCoordinator, + in window: NSWindow, + attach: Bool = true + ) -> MainContentCommandActions { + var selectedTables: Set = [] + var pendingTruncates: Set = [] + var pendingDeletes: Set = [] + var tableOperationOptions: [DatabaseTreeTableRef: TableOperationOptions] = [:] + let actions = MainContentCommandActions( + coordinator: coordinator, + connection: coordinator.connection, + selectedTables: Binding(get: { selectedTables }, set: { selectedTables = $0 }), + pendingTruncates: Binding(get: { pendingTruncates }, set: { pendingTruncates = $0 }), + pendingDeletes: Binding(get: { pendingDeletes }, set: { pendingDeletes = $0 }), + tableOperationOptions: Binding( + get: { tableOperationOptions }, + set: { tableOperationOptions = $0 } + ), + trailingPaneState: TrailingPaneState() + ) + actions.window = window + if attach { coordinator.commandActions = actions } + return actions + } + + @Test("Save on a window close keeps the window while another connection holds edits only their tab can save") + func saveKeepsTheWindowForAnotherConnectionsEdits() async throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let front = try harness.coordinator(of: harness.foreground) + let back = try harness.coordinator(of: harness.background) + let frontActions = actions(for: front, in: harness.window) + let backActions = actions(for: back, in: harness.window) + try harness.addSelectedTab(to: harness.foreground) + try harness.addSelectedTab(to: harness.background) + + var edited = QueryTab(title: "users", query: "SELECT * FROM users", tabType: .table, tableName: "users") + edited.pendingChanges.insertedRowIDs = [.inserted(UUID())] + edited.pendingChanges.columns = ["id", "name"] + edited.pendingChanges.primaryKeyColumns = ["id"] + back.tabManager.tabs.append(edited) + frontActions.confirmSaveChanges = { _, _ in .save } + + let outcome = await frontActions.closeWindowAwaiting() + + #expect(outcome == .cancelled) + #expect(back.tabManager.tabs.contains { $0.id == edited.id }) + withExtendedLifetime(backActions) {} + } + + // Its actions arrive only once its pane mounts, after Save has shown it. + @Test("Save waits for a connection that has not shown its pane yet, then saves it and closes") + func saveReachesAConnectionNotShownYet() async throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let front = try harness.coordinator(of: harness.foreground) + let back = try harness.coordinator(of: harness.background) + front.contentWindow = harness.window + let frontActions = actions(for: front, in: harness.window) + let backActions = actions(for: back, in: harness.window, attach: false) + back.commandActions = nil + try harness.addSelectedTab(to: harness.foreground) + back.toolbarState.hasCreateTablePending = true + frontActions.confirmSaveChanges = { _, _ in + Task { @MainActor in back.commandActions = backActions } + return .save + } + + let outcome = await frontActions.closeWindowAwaiting() + + #expect(outcome == .closed) + } + + @Test("A close cancelled while it waits for a connection's pane stops after that one wait and keeps the window") + func cancelledCloseStopsWaiting() async throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let front = try harness.coordinator(of: harness.foreground) + let back = try harness.coordinator(of: harness.background) + front.contentWindow = harness.window + let frontActions = actions(for: front, in: harness.window) + back.commandActions = nil + try harness.addSelectedTab(to: harness.foreground) + back.toolbarState.hasCreateTablePending = true + let clock = CountingClock() + frontActions.paneWaitClock = clock + frontActions.confirmSaveChanges = { _, _ in .save } + + let close = Task { await frontActions.closeWindowAwaiting() } + await clock.firstSleep.wait() + close.cancel() + let outcome = await close.value + + #expect(outcome == .cancelled) + #expect(clock.sleepCount == 1) + } +} + +// Time stands still, so a sleep ends only by cancellation; the first one opens a gate the test waits on. +private final class CountingClock: Clock, @unchecked Sendable { + let firstSleep = AsyncGate() + private let base = ManualClock() + private let lock = NSLock() + private var count = 0 + + var sleepCount: Int { lock.withLock { count } } + var now: ManualClock.Instant { base.now } + var minimumResolution: Duration { base.minimumResolution } + + func sleep(until deadline: ManualClock.Instant, tolerance: Duration?) async throws { + let isFirst = lock.withLock { + count += 1 + return count == 1 + } + if isFirst { await firstSleep.open() } + try await base.sleep(until: deadline, tolerance: tolerance) + } +} diff --git a/apple/TableProTests/Views/Results/ResultsJsonViewTests.swift b/apple/TableProTests/Views/Results/ResultsJsonViewTests.swift index d64d2975d0..167773b1fa 100644 --- a/apple/TableProTests/Views/Results/ResultsJsonViewTests.swift +++ b/apple/TableProTests/Views/Results/ResultsJsonViewTests.swift @@ -128,6 +128,30 @@ struct ResultsJsonViewTests { } } + @Test("A cell a pending edit set to the default renders as null, and stored text that reads DEFAULT stays text") + func pendingDefaultRendersAsNull() { + let inserted = RowID.inserted(UUID()) + let tableRows = TableRows( + rows: [ + Row(id: .existing(0), values: [.text("DEFAULT"), .text("a")]), + Row(id: inserted, values: [.columnDefault, .text("b")]), + ], + columns: ["status", "name"], + columnTypes: [.text(rawType: nil), .text(rawType: nil)] + ) + + let result = ResultsJsonView.computeJson( + tableRows: tableRows, + displayIDs: nil, + selectedIndices: [], + defaultedCells: [inserted: [0]], + columnLayout: ColumnLayoutState() + ) + + #expect(result.json.contains("\"status\": \"DEFAULT\"")) + #expect(result.json.contains("\"status\": null")) + } + @Test("an empty result renders an empty array") func emptyResultRendersEmptyArray() { let result = ResultsJsonView.computeJson( diff --git a/apple/TableProTests/Views/Results/SelectionSummaryTrackerTests.swift b/apple/TableProTests/Views/Results/SelectionSummaryTrackerTests.swift index 26110af586..926bf1f31f 100644 --- a/apple/TableProTests/Views/Results/SelectionSummaryTrackerTests.swift +++ b/apple/TableProTests/Views/Results/SelectionSummaryTrackerTests.swift @@ -176,7 +176,7 @@ struct SelectionSummaryTrackerTests { defer { grid.close() } grid.model.setSelection(Self.block(rows: 0...1, columns: 0...0)) - grid.edit(row: 1, to: .value(.text(PluginCellValue.defaultMarkerText), isExpression: false)) + grid.edit(row: 1, to: .value(.columnDefault, isExpression: true)) await grid.tracker.pendingTask?.value #expect(state.summary?.emptyCount == 1) diff --git a/apple/TableProTests/Views/Structure/ClickHousePartsGridTests.swift b/apple/TableProTests/Views/Structure/ClickHousePartsGridTests.swift index 41044fa125..fde04c7c82 100644 --- a/apple/TableProTests/Views/Structure/ClickHousePartsGridTests.swift +++ b/apple/TableProTests/Views/Structure/ClickHousePartsGridTests.swift @@ -5,12 +5,14 @@ import Foundation @testable import TablePro +import TableProPluginKit import Testing @MainActor struct ClickHousePartsGridTests { private let part = ClickHousePartInfo( partition: "202601", + partitionId: "202601", name: "202601_1_4_1", rows: 1_234_567, bytesOnDisk: 5_242_880, @@ -35,7 +37,7 @@ struct ClickHousePartsGridTests { @Test("An inactive part reads NO") func inactivePart() { let inactive = ClickHousePartInfo( - partition: "", name: "p", rows: 0, bytesOnDisk: 0, modificationTime: "", active: false + partition: "", partitionId: "", name: "p", rows: 0, bytesOnDisk: 0, modificationTime: "", active: false ) #expect(ClickHousePartsView.gridRow(for: inactive).values.last == "NO") } @@ -43,7 +45,7 @@ struct ClickHousePartsGridTests { @Test("Active reads aloud as Active or Inactive rather than YES or NO") func activeIsSpokenByName() { let inactive = ClickHousePartInfo( - partition: "", name: "p", rows: 0, bytesOnDisk: 0, modificationTime: "", active: false + partition: "", partitionId: "", name: "p", rows: 0, bytesOnDisk: 0, modificationTime: "", active: false ) let active = ClickHousePartsView.activeColumn @@ -59,23 +61,25 @@ struct ClickHousePartsGridTests { #expect(zip(ClickHousePartsView.columnMinimumWidths, ClickHousePartsView.columnWidths).allSatisfy { $0 <= $1 }) } - @Test("Drop and Detach name the one selected part's partition, and none while several are selected") + @Test("Drop and Detach act on the one selected part, and on none while several are selected") func selectedPartition() { let other = ClickHousePartInfo( - partition: "202602", name: "202602_1_1_0", rows: 1, bytesOnDisk: 1, modificationTime: "", active: true + partition: "202602", partitionId: "202602", name: "202602_1_1_0", rows: 1, bytesOnDisk: 1, + modificationTime: "", active: true ) let parts = [part, other] - #expect(ClickHousePartsView.partition(of: [other.id], in: parts) == "202602") - #expect(ClickHousePartsView.partition(of: [part.id, other.id], in: parts) == nil) - #expect(ClickHousePartsView.partition(of: [], in: parts) == nil) - #expect(ClickHousePartsView.partition(of: [UUID()], in: parts) == nil) + #expect(ClickHousePartsView.selectedPart(of: [other.id], in: parts)?.id == other.id) + #expect(ClickHousePartsView.selectedPart(of: [part.id, other.id], in: parts)?.id == nil) + #expect(ClickHousePartsView.selectedPart(of: [], in: parts)?.id == nil) + #expect(ClickHousePartsView.selectedPart(of: [UUID()], in: parts)?.id == nil) } @Test("A selected grid row names its own part") func selectedRowResolvesToItsPart() throws { let other = ClickHousePartInfo( - partition: "202602", name: "202602_1_1_0", rows: 1, bytesOnDisk: 1, modificationTime: "", active: true + partition: "202602", partitionId: "202602", name: "202602_1_1_0", rows: 1, bytesOnDisk: 1, + modificationTime: "", active: true ) let parts = [part, other] let grid = StructureListGridModel() @@ -95,7 +99,84 @@ struct ClickHousePartsGridTests { #expect(grid.id(for: second) == other.id) #expect(selected == [other.id]) - #expect(ClickHousePartsView.partition(of: selected, in: parts) == "202602") + #expect(ClickHousePartsView.selectedPart(of: selected, in: parts)?.partition == "202602") #expect(grid.model.rowID(at: 2) == nil) } + + @Test("A parts row keeps the partition ID a statement names beside the value the grid shows") + func rowCarriesThePartitionId() throws { + let row: [PluginCellValue] = [ + "(202401,'eu')", + "c1f8e6e2a7b04d4c9e1f0a2b3c4d5e6f", + "c1f8e6e2a7b04d4c9e1f0a2b3c4d5e6f_1_1_0", + "12", + "4096", + "2026-01-05 10:00:00", + "1", + ] + + let parsed = try #require(ClickHousePartsView.part(from: row)) + + #expect(parsed.partition == "(202401,'eu')") + #expect(parsed.partitionId == "c1f8e6e2a7b04d4c9e1f0a2b3c4d5e6f") + #expect(parsed.name == "c1f8e6e2a7b04d4c9e1f0a2b3c4d5e6f_1_1_0") + #expect(parsed.rows == 12) + #expect(parsed.bytesOnDisk == 4096) + #expect(parsed.modificationTime == "2026-01-05 10:00:00") + #expect(parsed.active) + #expect(ClickHousePartsView.gridRow(for: parsed).values[0] == "(202401,'eu')") + } + + // The pane used to swap the list and its toolbar for the error text until the tab was refreshed. + @Test("A refused or failed action keeps the parts list and its toolbar on screen") + func actionFailureKeepsTheList() { + var pane = ClickHousePartsPaneState() + pane.beginLoad() + pane.finishLoad([part]) + + pane.actionEnded(.refused("Safe Mode is read-only"), title: "Couldn't Drop Partition") + #expect(pane.display == .parts) + #expect(pane.alert == ClickHousePartsAlert(title: "Couldn't Drop Partition", message: "Safe Mode is read-only")) + + pane.alert = nil + pane.actionEnded(.failed("Wrong partition value"), title: "Couldn't Detach Partition") + #expect(pane.display == .parts) + #expect(pane.alert == ClickHousePartsAlert(title: "Couldn't Detach Partition", message: "Wrong partition value")) + } + + @Test("A Cancel at the gate's confirmation shows nothing") + func cancelIsQuiet() { + var pane = ClickHousePartsPaneState() + pane.finishLoad([part]) + + pane.actionEnded( + GatedOperationFailure(ExecutionGateError.cancelledByUser("Operation cancelled by user")), + title: "Couldn't Drop Partition" + ) + + #expect(pane.alert == nil) + #expect(pane.display == .parts) + } + + @Test("A load that fails with nothing shown offers Try Again, and a reload that fails keeps the list") + func loadFailure() { + var pane = ClickHousePartsPaneState() + #expect(pane.display == .loading) + + pane.failLoad("Connection refused") + #expect(pane.display == .loadFailed("Connection refused")) + #expect(pane.alert == nil) + + pane.beginLoad() + #expect(pane.display == .loading) + pane.finishLoad([]) + #expect(pane.display == .empty) + + pane.finishLoad([part]) + pane.beginLoad() + #expect(pane.display == .parts) + pane.failLoad("Timeout exceeded") + #expect(pane.display == .parts) + #expect(pane.alert?.message == "Timeout exceeded") + } } diff --git a/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift b/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift index b4e8440113..7910b8ae10 100644 --- a/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift +++ b/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift @@ -109,7 +109,8 @@ struct StructureListGridAccessibilityTests { defer { DataGridAccessibility.isActive = false } let parts = [true, false].map { active in ClickHousePartInfo( - partition: "202601", name: "p", rows: 1, bytesOnDisk: 1, modificationTime: "", active: active + partition: "202601", partitionId: "202601", name: "p", rows: 1, bytesOnDisk: 1, modificationTime: "", + active: active ) } let rows = parts.map { part in diff --git a/apple/TableProTests/Views/Structure/StructureSaveConfirmationTests.swift b/apple/TableProTests/Views/Structure/StructureSaveConfirmationTests.swift index 348093f635..fcb0310781 100644 --- a/apple/TableProTests/Views/Structure/StructureSaveConfirmationTests.swift +++ b/apple/TableProTests/Views/Structure/StructureSaveConfirmationTests.swift @@ -235,7 +235,7 @@ struct StructureSaveConfirmationTests { ).authorize(request) let error = try #require(decision.denialError) - #expect(StructureApplyFailure(error) == .cancelledByUser) + #expect(GatedOperationFailure(error) == .cancelledByUser) } @Test("A declined Touch ID is a refusal that says why, not a quiet Cancel") @@ -252,7 +252,7 @@ struct StructureSaveConfirmationTests { ).authorize(request) let error = try #require(decision.denialError) - let failure = StructureApplyFailure(error) + let failure = GatedOperationFailure(error) #expect(failure.outcome == .refused) #expect(failure.message != nil) #expect(!failure.reportsFailure) @@ -404,7 +404,7 @@ struct StructureSaveConfirmationTests { @Test("A Cancel leaves the edits staged and shows nothing") func cancelIsQuiet() { - let failure = StructureApplyFailure(ExecutionGateError.cancelledByUser("Operation cancelled by user")) + let failure = GatedOperationFailure(ExecutionGateError.cancelledByUser("Operation cancelled by user")) #expect(failure == .cancelledByUser) #expect(failure.outcome == .refused) @@ -414,7 +414,7 @@ struct StructureSaveConfirmationTests { @Test("A Safe Mode refusal leaves the edits staged and says why, and is not a failed operation") func refusalExplainsItself() { - let failure = StructureApplyFailure(ExecutionGateError.denied("Safe Mode is read-only")) + let failure = GatedOperationFailure(ExecutionGateError.denied("Safe Mode is read-only")) #expect(failure.outcome == .refused) #expect(failure.message == "Safe Mode is read-only") @@ -423,7 +423,7 @@ struct StructureSaveConfirmationTests { @Test("A statement the server rejected is a failed save") func serverRejectionIsAFailure() { - let failure = StructureApplyFailure(ServerRejection()) + let failure = GatedOperationFailure(ServerRejection()) #expect(failure.outcome == .failed("Duplicate column name 'notes'")) #expect(failure.message == "Duplicate column name 'notes'") diff --git a/apple/TableProTests/Views/Structure/TriggerEditorFontSizeTests.swift b/apple/TableProTests/Views/Structure/TriggerEditorFontSizeTests.swift new file mode 100644 index 0000000000..3745115a46 --- /dev/null +++ b/apple/TableProTests/Views/Structure/TriggerEditorFontSizeTests.swift @@ -0,0 +1,25 @@ +import AppKit +@testable import TablePro +import TableProEditorKit +import Testing + +@MainActor +struct TriggerEditorFontSizeTests { + @Test("New and Edit Trigger open at the size the DDL pane is zoomed to") + func opensAtTheStoredSize() throws { + let zoomed = try EphemeralDefaults() + zoomed.defaults.set(18.0, forKey: TriggerEditorView.fontSizeKey) + + #expect(TriggerEditorView.initialConfiguration(in: zoomed.defaults).appearance.font.pointSize == 18) + } + + @Test("Without a stored size the sheet opens at the default") + func opensAtTheDefaultSize() throws { + let fresh = try EphemeralDefaults() + + #expect( + TriggerEditorView.initialConfiguration(in: fresh.defaults).appearance.font.pointSize + == CGFloat(TriggerEditorView.defaultFontSize) + ) + } +} diff --git a/core/crates/core/src/export/csv.rs b/core/crates/core/src/export/csv.rs index f3ff38e741..55ad66485b 100644 --- a/core/crates/core/src/export/csv.rs +++ b/core/crates/core/src/export/csv.rs @@ -184,11 +184,30 @@ mod tests { } } + fn sanitized() -> CsvOptions { + CsvOptions { + sanitize_formulas: true, + ..Default::default() + } + } + + // An importer reads the quote a sanitized field gains as part of the value. #[test] - fn csv_defaults_quote_every_field_when_sanitising() { + fn csv_defaults_write_values_as_they_are() { + let columns = cols(&["id", "handle"]); + let rows = vec![ + vec![Value::Int(1), Value::Text("@handle".into())], + vec![Value::Int(2), Value::Text("-pending".into())], + ]; + let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + assert_eq!(out, "id,handle\n1,@handle\n2,-pending\n"); + } + + #[test] + fn csv_sanitising_quotes_every_field() { let columns = cols(&["id", "name"]); let rows = vec![vec![Value::Int(1), Value::Text("Alice".into())]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, "\"id\",\"name\"\n\"1\",\"Alice\"\n"); } @@ -266,7 +285,7 @@ mod tests { let rows = vec![vec![Value::Text("=cmd".into())]]; let opts = CsvOptions { quote: CsvQuote::Never, - ..Default::default() + ..sanitized() }; let out = render_csv(&columns, &rows, &opts).unwrap(); assert_eq!(out, "\"a\"\n\"'=cmd\"\n"); @@ -277,7 +296,7 @@ mod tests { let columns = cols(&["a"]); for ch in ['=', '+', '-', '@'] { let rows = vec![vec![Value::Text(format!("{ch}cmd"))]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, format!("\"a\"\n\"'{ch}cmd\"\n"), "prefix {ch} should be sanitized"); } } @@ -287,7 +306,7 @@ mod tests { let columns = cols(&["a"]); for lead in ['\t', '\r', '\n'] { let rows = vec![vec![Value::Text(format!("{lead}=cmd|'/C calc'!A0"))]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); let expected = format!("\"a\"\n\"'{lead}=cmd|'/C calc'!A0\"\n"); assert_eq!(out, expected, "lead {lead:?} should be sanitized and quoted"); } @@ -298,7 +317,7 @@ mod tests { let columns = cols(&["a"]); for lead in ['\u{FF1D}', '\u{FF0B}', '\u{FF0D}', '\u{FF20}'] { let rows = vec![vec![Value::Text(format!("{lead}SUM(A1)"))]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!( out, format!("\"a\"\n\"'{lead}SUM(A1)\"\n"), @@ -319,7 +338,7 @@ mod tests { fn csv_does_not_sanitize_non_formula_prefixes() { let columns = cols(&["a"]); let rows = vec![vec![Value::Text("plain text".into())]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, "\"a\"\n\"plain text\"\n"); } @@ -347,7 +366,7 @@ mod tests { #[test] fn csv_keeps_negative_int() { let columns = cols(&["a"]); - let out = render_csv(&columns, &[vec![Value::Int(-5)]], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[vec![Value::Int(-5)]], &sanitized()).unwrap(); assert_eq!(out, "\"a\"\n\"-5\"\n"); } @@ -355,14 +374,14 @@ mod tests { fn csv_keeps_negative_decimal() { let columns = cols(&["a"]); let rows = vec![vec![Value::Decimal("-12.30".parse().expect("a decimal"))]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, "\"a\"\n\"-12.30\"\n"); } #[test] fn csv_keeps_negative_float() { let columns = cols(&["a"]); - let out = render_csv(&columns, &[vec![Value::Float64(-1.5)]], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[vec![Value::Float64(-1.5)]], &sanitized()).unwrap(); assert_eq!(out, "\"a\"\n\"-1.5\"\n"); } @@ -371,7 +390,7 @@ mod tests { let columns = cols(&["a"]); for text in ["-12", "+1.5e3", "-.5", "1.", "-7E-3"] { let rows = vec![vec![Value::Text(text.into())]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, format!("\"a\"\n\"{text}\"\n"), "{text} is a number, not a formula"); } } @@ -381,7 +400,7 @@ mod tests { let columns = cols(&["a"]); for text in ["-cmd", "=SUM(A1)", "+1+cmd", "-", "-.", "-1e"] { let rows = vec![vec![Value::Text(text.into())]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, format!("\"a\"\n\"'{text}\"\n"), "{text} must be neutralised"); } } @@ -401,7 +420,7 @@ mod tests { other("-2+cmd"), ]; for value in neutralised { - let out = render_csv(&columns, &[vec![value.clone()]], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[vec![value.clone()]], &sanitized()).unwrap(); assert!(out.contains("\"'"), "{value:?} reached the sheet as a formula: {out}"); } let kept = [ @@ -412,7 +431,7 @@ mod tests { (other("+1.5e3"), "+1.5e3"), ]; for (value, text) in kept { - let out = render_csv(&columns, &[vec![value]], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[vec![value]], &sanitized()).unwrap(); assert_eq!(out, format!("\"a\"\n\"{text}\"\n"), "{text} is data, not a formula"); } } @@ -420,14 +439,14 @@ mod tests { #[test] fn csv_neutralises_formula_header() { let columns = cols(&["=HYPERLINK(\"x\")"]); - let out = render_csv(&columns, &[], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[], &sanitized()).unwrap(); assert_eq!(out, "\"'=HYPERLINK(\"\"x\"\")\"\n"); } #[test] fn text_csv_treats_every_field_as_free_text() { let records = vec![vec!["-cmd".to_string(), "12".to_string(), String::new()]]; - let out = render_text_csv(&["q", "n", "e"], &records, &CsvOptions::default()).unwrap(); + let out = render_text_csv(&["q", "n", "e"], &records, &sanitized()).unwrap(); assert_eq!(out, "\"q\",\"n\",\"e\"\n\"'-cmd\",\"12\",\"\"\n"); } @@ -449,11 +468,11 @@ mod tests { fn csv_null_modes() { let columns = cols(&["a"]); let rows = vec![vec![Value::Null]]; - let out_empty = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out_empty = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out_empty, "\"a\"\n\"\"\n"); let opts = CsvOptions { null_to_empty: false, - ..Default::default() + ..sanitized() }; let out_null = render_csv(&columns, &rows, &opts).unwrap(); assert_eq!(out_null, "\"a\"\n\"NULL\"\n"); diff --git a/core/crates/core/src/export/csv_options.rs b/core/crates/core/src/export/csv_options.rs index 3ff8f7adaf..409842ab0d 100644 --- a/core/crates/core/src/export/csv_options.rs +++ b/core/crates/core/src/export/csv_options.rs @@ -68,7 +68,9 @@ impl Default for CsvOptions { null_to_empty: true, line_break_to_space: false, header_row: true, - sanitize_formulas: true, + // RFC 4180 carries a field as written, and OWASP scopes the quote prefix to files meant + // for a spreadsheet: an importer keeps the quote as part of the value. + sanitize_formulas: false, delimiter: CsvDelimiter::Comma, quote: CsvQuote::IfNeeded, line_break: CsvLineBreak::Lf, diff --git a/core/crates/core/src/export/encoder/tests.rs b/core/crates/core/src/export/encoder/tests.rs index 1e65cb7aa8..6484071e73 100644 --- a/core/crates/core/src/export/encoder/tests.rs +++ b/core/crates/core/src/export/encoder/tests.rs @@ -377,7 +377,11 @@ fn csv_neutralises_text_not_negative_numbers_through_encoder() { vec![Value::Text("-12".into())], ]; - let out = streamed(CsvEncoder::new(columns, CsvOptions::default()), &rows); + let options = CsvOptions { + sanitize_formulas: true, + ..CsvOptions::default() + }; + let out = streamed(CsvEncoder::new(columns, options), &rows); assert_eq!( out, diff --git a/core/crates/mcp/src/export_tools.rs b/core/crates/mcp/src/export_tools.rs index 971b6b73a6..bdcf434f47 100644 --- a/core/crates/mcp/src/export_tools.rs +++ b/core/crates/mcp/src/export_tools.rs @@ -158,7 +158,15 @@ fn render( rows: &[Vec], ) -> Result { let encoded = match format { - ExportFormat::Csv => render_csv(columns, rows, &CsvOptions::default()), + // A tool result has no option to turn it on, so it keeps the neutralising the macOS tool result has. + ExportFormat::Csv => render_csv( + columns, + rows, + &CsvOptions { + sanitize_formulas: true, + ..CsvOptions::default() + }, + ), ExportFormat::Json => render_json(columns, rows), ExportFormat::Sql => { let dialect = session diff --git a/core/crates/storage/src/query_history/export.rs b/core/crates/storage/src/query_history/export.rs index b9d9bacdcc..a4753f5251 100644 --- a/core/crates/storage/src/query_history/export.rs +++ b/core/crates/storage/src/query_history/export.rs @@ -17,7 +17,11 @@ const HISTORY_CSV_HEADER: [&str; 10] = [ pub(super) fn history_csv(entries: &[Entry]) -> Result { let records: Vec> = entries.iter().map(history_record).collect(); - let options = tablepro_core::export::CsvOptions::default(); + // The history export has no option of its own, so it keeps neutralising rather than follow the export default. + let options = tablepro_core::export::CsvOptions { + sanitize_formulas: true, + ..tablepro_core::export::CsvOptions::default() + }; Ok(tablepro_core::export::render_text_csv( &HISTORY_CSV_HEADER, &records, diff --git a/core/crates/test-support/src/conformance/cases/large_table.rs b/core/crates/test-support/src/conformance/cases/large_table.rs index 4b77f07bfb..5dc295e763 100644 --- a/core/crates/test-support/src/conformance/cases/large_table.rs +++ b/core/crates/test-support/src/conformance/cases/large_table.rs @@ -6,7 +6,7 @@ use std::time::{Duration, Instant}; use async_trait::async_trait; use tablepro_core::browse::{ExportRequest, PageRead, PageRequest, PagingMode, build_export, read_page}; -use tablepro_core::export::{CsvEncoder, CsvOptions, RowEncoder}; +use tablepro_core::export::{CsvEncoder, CsvOptions, CsvQuote, RowEncoder}; use tablepro_core::meta::{PagePosition, RowIdentity, TableRef}; use tablepro_core::{ ColumnInfo, Connection, FilterSet, ReadStatement, ResultEvent, ResultSink, SinkFlow, UserSql, Value, WriteKind, @@ -538,7 +538,12 @@ impl ResultSink for CsvFileSink { match event { ResultEvent::ResultSetStarted { columns } => { lock(&self.tally).result_sets += 1; - let mut encoder = CsvEncoder::new(columns, CsvOptions::default()); + // `check_file` reads every field quoted. + let options = CsvOptions { + quote: CsvQuote::Always, + ..CsvOptions::default() + }; + let mut encoder = CsvEncoder::new(columns, options); encoder.begin(&mut self.buffer); self.encoder = Some(encoder); SinkFlow::Continue diff --git a/core/crates/test-support/src/conformance/cases/round_trip.rs b/core/crates/test-support/src/conformance/cases/round_trip.rs index 9e17db85c6..94f0a82d79 100644 --- a/core/crates/test-support/src/conformance/cases/round_trip.rs +++ b/core/crates/test-support/src/conformance/cases/round_trip.rs @@ -47,7 +47,7 @@ const IMPORT_DEADLINE: Duration = Duration::from_secs(120); /// that hangs. const EXPORT_DEADLINE: Duration = Duration::from_secs(120); -/// Under the export defaults these rows of 200-character labels make a +/// With every field quoted these rows of 200-character labels make a /// file just under `MAX_CSV_FILE_BYTES`. 200 characters is the widest /// label every engine's scratch table takes. const NEAR_LIMIT_ROWS: u64 = 315_000; @@ -428,7 +428,11 @@ pub async fn csv_export_near_the_import_limit_imports_back( FILL_DEADLINE, ) .await?; - let file = export_file(connection.as_ref(), &source, csv_encoder(CsvOptions::default())).await?; + let quoted = CsvOptions { + quote: CsvQuote::Always, + ..CsvOptions::default() + }; + let file = export_file(connection.as_ref(), &source, csv_encoder(quoted)).await?; let size = file.len(); expect( NEAR_LIMIT_CASE, diff --git a/linux/crates/app/src/ui/export/encoding_sink.rs b/linux/crates/app/src/ui/export/encoding_sink.rs index 947fcb8fa8..bb1b3bb509 100644 --- a/linux/crates/app/src/ui/export/encoding_sink.rs +++ b/linux/crates/app/src/ui/export/encoding_sink.rs @@ -239,7 +239,7 @@ mod tests { assert_eq!(finished, Some(1)); let text: Vec = chunks.into_iter().flat_map(|(bytes, _)| bytes).collect(); - assert_eq!(String::from_utf8(text).expect("utf-8"), "\"x\",\"y\"\n\"1\",\"2\"\n"); + assert_eq!(String::from_utf8(text).expect("utf-8"), "x,y\n1,2\n"); } #[tokio::test(start_paused = true)] diff --git a/linux/crates/app/src/ui/export/export_ledger/tests.rs b/linux/crates/app/src/ui/export/export_ledger/tests.rs index 97e830ca17..63fdf02bb8 100644 --- a/linux/crates/app/src/ui/export/export_ledger/tests.rs +++ b/linux/crates/app/src/ui/export/export_ledger/tests.rs @@ -250,8 +250,8 @@ fn entire_table_export_streams_every_row() { let text = std::fs::read_to_string(&path).expect("the export"); let lines: Vec<&str> = text.lines().collect(); assert_eq!(lines.len(), 25_001); - assert_eq!(lines[0], "\"id\",\"name\""); - assert_eq!(lines[25_000], "\"24999\",\"user 24999\""); + assert_eq!(lines[0], "id,name"); + assert_eq!(lines[25_000], "24999,user 24999"); assert_eq!( fake.connection.observed_deadlines("stream_read"), [None], @@ -298,7 +298,7 @@ fn entire_table_export_has_no_identity_columns() { "{sql}" ); let text = std::fs::read_to_string(&path).expect("the export"); - assert_eq!(text.lines().next(), Some("\"id\",\"name\"")); + assert_eq!(text.lines().next(), Some("id,name")); } #[gtk4::test] @@ -510,11 +510,7 @@ fn snapshot_export_of_10k_rows_keeps_main_loop_responsive() { assert!(ticks.get() > 0, "the main loop never ran while the export did"); let text = std::fs::read_to_string(&path).expect("the export"); assert_eq!(text.lines().count(), 10_001); - assert!( - text.starts_with("\"id\",\"name\",\"note\"\n\"0\",\"name 0\""), - "{}", - &text[..40] - ); + assert!(text.starts_with("id,name,note\n0,name 0"), "{}", &text[..40]); assert_eq!(harness.ledger.in_flight(), 0); assert_eq!(harness.hold.live(), 0, "the export kept the process held"); } diff --git a/linux/crates/platform/tests/settings.rs b/linux/crates/platform/tests/settings.rs index e95371763f..6939de58a3 100644 --- a/linux/crates/platform/tests/settings.rs +++ b/linux/crates/platform/tests/settings.rs @@ -213,7 +213,7 @@ async fn csv_options_round_trip() -> TestResult { null_to_empty: false, line_break_to_space: true, header_row: false, - sanitize_formulas: false, + sanitize_formulas: true, delimiter: CsvDelimiter::Pipe, quote: CsvQuote::Always, line_break: CsvLineBreak::CrLf, diff --git a/linux/data/app.tablepro.TablePro.gschema.xml.in b/linux/data/app.tablepro.TablePro.gschema.xml.in index 2044a4b947..af9c9f021a 100644 --- a/linux/data/app.tablepro.TablePro.gschema.xml.in +++ b/linux/data/app.tablepro.TablePro.gschema.xml.in @@ -324,7 +324,7 @@ - true + false Neutralise spreadsheet formulas Whether a CSV export prefixes values starting with =, +, - or @ so a spreadsheet does not run them. diff --git a/linux/docs/storage.md b/linux/docs/storage.md index f190f695be..ce39a839d1 100644 --- a/linux/docs/storage.md +++ b/linux/docs/storage.md @@ -378,7 +378,7 @@ The schema id is the application id and the path follows it: `app.tablepro.Table | `csv-null-to-empty` | `b` | `true` | | | `csv-line-break-to-space` | `b` | `false` | | | `csv-header-row` | `b` | `true` | | -| `csv-sanitize-formulas` | `b` | `true` | | +| `csv-sanitize-formulas` | `b` | `false` | | | `csv-delimiter` | enum | `comma` | comma, semicolon, tab, pipe | | `csv-quote` | enum | `if-needed` | always, if-needed, never | | `csv-line-break` | enum | `lf` | lf, crlf, cr | From 5b9d13a6df2e055f899b5c0d7d08d67602e7d308 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 14:52:25 +0700 Subject: [PATCH 2/3] docs(macos): add the changelog fragments for #4185 --- apple/changelog.d/4185.fixed.1.md | 1 + apple/changelog.d/4185.fixed.10.md | 1 + apple/changelog.d/4185.fixed.11.md | 1 + apple/changelog.d/4185.fixed.12.md | 1 + apple/changelog.d/4185.fixed.13.md | 1 + apple/changelog.d/4185.fixed.14.md | 1 + apple/changelog.d/4185.fixed.15.md | 1 + apple/changelog.d/4185.fixed.16.md | 1 + apple/changelog.d/4185.fixed.17.md | 1 + apple/changelog.d/4185.fixed.18.md | 1 + apple/changelog.d/4185.fixed.19.md | 1 + apple/changelog.d/4185.fixed.2.md | 1 + apple/changelog.d/4185.fixed.20.md | 1 + apple/changelog.d/4185.fixed.21.md | 1 + apple/changelog.d/4185.fixed.22.md | 1 + apple/changelog.d/4185.fixed.23.md | 1 + apple/changelog.d/4185.fixed.24.md | 1 + apple/changelog.d/4185.fixed.25.md | 1 + apple/changelog.d/4185.fixed.26.md | 1 + apple/changelog.d/4185.fixed.27.md | 1 + apple/changelog.d/4185.fixed.28.md | 1 + apple/changelog.d/4185.fixed.29.md | 1 + apple/changelog.d/4185.fixed.3.md | 1 + apple/changelog.d/4185.fixed.30.md | 1 + apple/changelog.d/4185.fixed.31.md | 1 + apple/changelog.d/4185.fixed.32.md | 1 + apple/changelog.d/4185.fixed.4.md | 1 + apple/changelog.d/4185.fixed.5.md | 1 + apple/changelog.d/4185.fixed.6.md | 1 + apple/changelog.d/4185.fixed.7.md | 1 + apple/changelog.d/4185.fixed.8.md | 1 + apple/changelog.d/4185.fixed.9.md | 1 + apple/changelog.d/4185.fixed.md | 1 + linux/changelog.d/4185.fixed.md | 1 + 34 files changed, 34 insertions(+) create mode 100644 apple/changelog.d/4185.fixed.1.md create mode 100644 apple/changelog.d/4185.fixed.10.md create mode 100644 apple/changelog.d/4185.fixed.11.md create mode 100644 apple/changelog.d/4185.fixed.12.md create mode 100644 apple/changelog.d/4185.fixed.13.md create mode 100644 apple/changelog.d/4185.fixed.14.md create mode 100644 apple/changelog.d/4185.fixed.15.md create mode 100644 apple/changelog.d/4185.fixed.16.md create mode 100644 apple/changelog.d/4185.fixed.17.md create mode 100644 apple/changelog.d/4185.fixed.18.md create mode 100644 apple/changelog.d/4185.fixed.19.md create mode 100644 apple/changelog.d/4185.fixed.2.md create mode 100644 apple/changelog.d/4185.fixed.20.md create mode 100644 apple/changelog.d/4185.fixed.21.md create mode 100644 apple/changelog.d/4185.fixed.22.md create mode 100644 apple/changelog.d/4185.fixed.23.md create mode 100644 apple/changelog.d/4185.fixed.24.md create mode 100644 apple/changelog.d/4185.fixed.25.md create mode 100644 apple/changelog.d/4185.fixed.26.md create mode 100644 apple/changelog.d/4185.fixed.27.md create mode 100644 apple/changelog.d/4185.fixed.28.md create mode 100644 apple/changelog.d/4185.fixed.29.md create mode 100644 apple/changelog.d/4185.fixed.3.md create mode 100644 apple/changelog.d/4185.fixed.30.md create mode 100644 apple/changelog.d/4185.fixed.31.md create mode 100644 apple/changelog.d/4185.fixed.32.md create mode 100644 apple/changelog.d/4185.fixed.4.md create mode 100644 apple/changelog.d/4185.fixed.5.md create mode 100644 apple/changelog.d/4185.fixed.6.md create mode 100644 apple/changelog.d/4185.fixed.7.md create mode 100644 apple/changelog.d/4185.fixed.8.md create mode 100644 apple/changelog.d/4185.fixed.9.md create mode 100644 apple/changelog.d/4185.fixed.md create mode 100644 linux/changelog.d/4185.fixed.md diff --git a/apple/changelog.d/4185.fixed.1.md b/apple/changelog.d/4185.fixed.1.md new file mode 100644 index 0000000000..625f6be5b1 --- /dev/null +++ b/apple/changelog.d/4185.fixed.1.md @@ -0,0 +1 @@ +CSV export with a decimal comma rewrote text values that looked like decimals, such as a version 1.10. diff --git a/apple/changelog.d/4185.fixed.10.md b/apple/changelog.d/4185.fixed.10.md new file mode 100644 index 0000000000..c4812cbb1b --- /dev/null +++ b/apple/changelog.d/4185.fixed.10.md @@ -0,0 +1 @@ +Rewinding a save could fail on a unique key when an earlier edit in it had been undone before saving. diff --git a/apple/changelog.d/4185.fixed.11.md b/apple/changelog.d/4185.fixed.11.md new file mode 100644 index 0000000000..03913855f0 --- /dev/null +++ b/apple/changelog.d/4185.fixed.11.md @@ -0,0 +1 @@ +Rewind reported a row as missing when the save had changed its primary key. diff --git a/apple/changelog.d/4185.fixed.12.md b/apple/changelog.d/4185.fixed.12.md new file mode 100644 index 0000000000..427ed680e8 --- /dev/null +++ b/apple/changelog.d/4185.fixed.12.md @@ -0,0 +1 @@ +A value of __DEFAULT__ was saved as the column default when duplicating a row or typing it into a cell. diff --git a/apple/changelog.d/4185.fixed.13.md b/apple/changelog.d/4185.fixed.13.md new file mode 100644 index 0000000000..f3f1e23763 --- /dev/null +++ b/apple/changelog.d/4185.fixed.13.md @@ -0,0 +1 @@ +A table opened while another query waited for Safe Mode approval stayed empty with its loading indicator. diff --git a/apple/changelog.d/4185.fixed.14.md b/apple/changelog.d/4185.fixed.14.md new file mode 100644 index 0000000000..bfaa1727f7 --- /dev/null +++ b/apple/changelog.d/4185.fixed.14.md @@ -0,0 +1 @@ +Saving while closing a window could close it over unsaved edits in another connection or tab. diff --git a/apple/changelog.d/4185.fixed.15.md b/apple/changelog.d/4185.fixed.15.md new file mode 100644 index 0000000000..157fe467ec --- /dev/null +++ b/apple/changelog.d/4185.fixed.15.md @@ -0,0 +1 @@ +Saving a Cassandra row that another client deleted recreated it with every other column empty. diff --git a/apple/changelog.d/4185.fixed.16.md b/apple/changelog.d/4185.fixed.16.md new file mode 100644 index 0000000000..9b9cdee113 --- /dev/null +++ b/apple/changelog.d/4185.fixed.16.md @@ -0,0 +1 @@ +Running SET with NX or XX in the Redis console showed OK when the key was not written. diff --git a/apple/changelog.d/4185.fixed.17.md b/apple/changelog.d/4185.fixed.17.md new file mode 100644 index 0000000000..27e55c9db4 --- /dev/null +++ b/apple/changelog.d/4185.fixed.17.md @@ -0,0 +1 @@ +Pressing Stop as a MySQL tab closed could crash the app. diff --git a/apple/changelog.d/4185.fixed.18.md b/apple/changelog.d/4185.fixed.18.md new file mode 100644 index 0000000000..18a4d89177 --- /dev/null +++ b/apple/changelog.d/4185.fixed.18.md @@ -0,0 +1 @@ +A startup command that failed, such as SET ROLE, left the session running without it and nothing said so. diff --git a/apple/changelog.d/4185.fixed.19.md b/apple/changelog.d/4185.fixed.19.md new file mode 100644 index 0000000000..e052ba407c --- /dev/null +++ b/apple/changelog.d/4185.fixed.19.md @@ -0,0 +1 @@ +Exporting more than 5,000,000 MongoDB documents wrote the first 5,000,000 and reported success. diff --git a/apple/changelog.d/4185.fixed.2.md b/apple/changelog.d/4185.fixed.2.md new file mode 100644 index 0000000000..51fe78d8f7 --- /dev/null +++ b/apple/changelog.d/4185.fixed.2.md @@ -0,0 +1 @@ +Changing export or import options in another window changed a running export or import, or left foreign keys off. diff --git a/apple/changelog.d/4185.fixed.20.md b/apple/changelog.d/4185.fixed.20.md new file mode 100644 index 0000000000..a4aa8a48a5 --- /dev/null +++ b/apple/changelog.d/4185.fixed.20.md @@ -0,0 +1 @@ +Stopping a Teradata query ended the whole session, so volatile tables were lost and the next query failed. diff --git a/apple/changelog.d/4185.fixed.21.md b/apple/changelog.d/4185.fixed.21.md new file mode 100644 index 0000000000..21106f7c75 --- /dev/null +++ b/apple/changelog.d/4185.fixed.21.md @@ -0,0 +1 @@ +A query parameter such as zip code 00123 was sent to Trino as the number 123. diff --git a/apple/changelog.d/4185.fixed.22.md b/apple/changelog.d/4185.fixed.22.md new file mode 100644 index 0000000000..05b1cf291d --- /dev/null +++ b/apple/changelog.d/4185.fixed.22.md @@ -0,0 +1 @@ +TablePro could quit when the Copilot language server exited while TablePro was sending it a message. diff --git a/apple/changelog.d/4185.fixed.23.md b/apple/changelog.d/4185.fixed.23.md new file mode 100644 index 0000000000..244113e6d1 --- /dev/null +++ b/apple/changelog.d/4185.fixed.23.md @@ -0,0 +1 @@ +Run Current and the gutter cut a SQL Server IF, WHILE or TRY...CATCH block at each semicolon inside it. diff --git a/apple/changelog.d/4185.fixed.24.md b/apple/changelog.d/4185.fixed.24.md new file mode 100644 index 0000000000..d34f707ee7 --- /dev/null +++ b/apple/changelog.d/4185.fixed.24.md @@ -0,0 +1 @@ +Run Current split a PostgreSQL rule with several commands in parentheses at each semicolon. diff --git a/apple/changelog.d/4185.fixed.25.md b/apple/changelog.d/4185.fixed.25.md new file mode 100644 index 0000000000..4ff326bcbd --- /dev/null +++ b/apple/changelog.d/4185.fixed.25.md @@ -0,0 +1 @@ +New Trigger and Edit Trigger opened at 13 pt instead of the size the Structure tab's DDL pane was zoomed to. diff --git a/apple/changelog.d/4185.fixed.26.md b/apple/changelog.d/4185.fixed.26.md new file mode 100644 index 0000000000..65af010304 --- /dev/null +++ b/apple/changelog.d/4185.fixed.26.md @@ -0,0 +1 @@ +Moving the caret in a long MongoDB or Redis query re-read the whole document every time. diff --git a/apple/changelog.d/4185.fixed.27.md b/apple/changelog.d/4185.fixed.27.md new file mode 100644 index 0000000000..f0afd7c7e2 --- /dev/null +++ b/apple/changelog.d/4185.fixed.27.md @@ -0,0 +1 @@ +Column and table suggestions on iPhone and iPad were rebuilt on every keystroke. diff --git a/apple/changelog.d/4185.fixed.28.md b/apple/changelog.d/4185.fixed.28.md new file mode 100644 index 0000000000..27178d8fa0 --- /dev/null +++ b/apple/changelog.d/4185.fixed.28.md @@ -0,0 +1 @@ +The ClickHouse Parts tab hid its list and toolbar after a cancelled or failed partition action. diff --git a/apple/changelog.d/4185.fixed.29.md b/apple/changelog.d/4185.fixed.29.md new file mode 100644 index 0000000000..0ac47956a5 --- /dev/null +++ b/apple/changelog.d/4185.fixed.29.md @@ -0,0 +1 @@ +Dropping or detaching a ClickHouse partition failed on tables partitioned by several columns or not at all. diff --git a/apple/changelog.d/4185.fixed.3.md b/apple/changelog.d/4185.fixed.3.md new file mode 100644 index 0000000000..8fa5c94f07 --- /dev/null +++ b/apple/changelog.d/4185.fixed.3.md @@ -0,0 +1 @@ +Data sync and Copy as SQL wrote PostgreSQL binary values that stored other bytes with standard_conforming_strings off. diff --git a/apple/changelog.d/4185.fixed.30.md b/apple/changelog.d/4185.fixed.30.md new file mode 100644 index 0000000000..6be6044706 --- /dev/null +++ b/apple/changelog.d/4185.fixed.30.md @@ -0,0 +1 @@ +Cancelling the Safe Mode confirmation in an object editor or sheet showed an error. diff --git a/apple/changelog.d/4185.fixed.31.md b/apple/changelog.d/4185.fixed.31.md new file mode 100644 index 0000000000..c6d66b0771 --- /dev/null +++ b/apple/changelog.d/4185.fixed.31.md @@ -0,0 +1 @@ +Applying user changes ran ALTER and password statements for a user staged for drop, and overcounted pending changes. diff --git a/apple/changelog.d/4185.fixed.32.md b/apple/changelog.d/4185.fixed.32.md new file mode 100644 index 0000000000..98e47dea15 --- /dev/null +++ b/apple/changelog.d/4185.fixed.32.md @@ -0,0 +1 @@ +Moving the caret next to a bracket far below the visible text could crash the editor. diff --git a/apple/changelog.d/4185.fixed.4.md b/apple/changelog.d/4185.fixed.4.md new file mode 100644 index 0000000000..a1b1cd6aa7 --- /dev/null +++ b/apple/changelog.d/4185.fixed.4.md @@ -0,0 +1 @@ +Copy as SQL wrote DuckDB and Oracle binary values that DuckDB stored as text and Oracle rejected. diff --git a/apple/changelog.d/4185.fixed.5.md b/apple/changelog.d/4185.fixed.5.md new file mode 100644 index 0000000000..2aea41ab0b --- /dev/null +++ b/apple/changelog.d/4185.fixed.5.md @@ -0,0 +1 @@ +Data sync wrote an empty Oracle BLOB as NULL, which a NOT NULL column refused. diff --git a/apple/changelog.d/4185.fixed.6.md b/apple/changelog.d/4185.fixed.6.md new file mode 100644 index 0000000000..77fcfcfd1f --- /dev/null +++ b/apple/changelog.d/4185.fixed.6.md @@ -0,0 +1 @@ +Data sync and Copy as SQL wrote Redshift binary values in a form Redshift refused. diff --git a/apple/changelog.d/4185.fixed.7.md b/apple/changelog.d/4185.fixed.7.md new file mode 100644 index 0000000000..2b5c350f28 --- /dev/null +++ b/apple/changelog.d/4185.fixed.7.md @@ -0,0 +1 @@ +Add Rows in Shortcuts refused CSV with bracketed headers such as [id], as SQL Server exports them. diff --git a/apple/changelog.d/4185.fixed.8.md b/apple/changelog.d/4185.fixed.8.md new file mode 100644 index 0000000000..88dc60ec1c --- /dev/null +++ b/apple/changelog.d/4185.fixed.8.md @@ -0,0 +1 @@ +Add Rows in Shortcuts refused a header that differed from its column's name only in case. diff --git a/apple/changelog.d/4185.fixed.9.md b/apple/changelog.d/4185.fixed.9.md new file mode 100644 index 0000000000..081e98b9ba --- /dev/null +++ b/apple/changelog.d/4185.fixed.9.md @@ -0,0 +1 @@ +Copy to another database and Data Compare missed rows past a database's row limit without saying so. diff --git a/apple/changelog.d/4185.fixed.md b/apple/changelog.d/4185.fixed.md new file mode 100644 index 0000000000..a90c14e4ae --- /dev/null +++ b/apple/changelog.d/4185.fixed.md @@ -0,0 +1 @@ +CSV export put a single quote before values starting with =, +, - or @ by default, which then stayed in imports. diff --git a/linux/changelog.d/4185.fixed.md b/linux/changelog.d/4185.fixed.md new file mode 100644 index 0000000000..a90c14e4ae --- /dev/null +++ b/linux/changelog.d/4185.fixed.md @@ -0,0 +1 @@ +CSV export put a single quote before values starting with =, +, - or @ by default, which then stayed in imports. From 4bd9bc7dc60094b452cd050f80a2a5d907a6ae24 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 17:26:04 +0700 Subject: [PATCH 3/3] build(pluginkit): raise the kit to 44 for the additive API --- apple/Configs/PluginKit.xcconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apple/Configs/PluginKit.xcconfig b/apple/Configs/PluginKit.xcconfig index fef1b5c3ce..207ba58516 100644 --- a/apple/Configs/PluginKit.xcconfig +++ b/apple/Configs/PluginKit.xcconfig @@ -1,3 +1,3 @@ // When to raise each, and how the build stamps them: .claude/rules/plugin-system.md, ABI. -TABLEPRO_PLUGINKIT_VERSION = 43 +TABLEPRO_PLUGINKIT_VERSION = 44 TABLEPRO_PLUGINKIT_MINIMUM_VERSION = 19