From 42411431c0aec36d923ae6cb42f4ad3ed562f92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Thu, 29 Oct 2026 17:34:54 +0700 Subject: [PATCH 01/85] fix(autocomplete): read a qualifier after FROM as a schema, not a table (#3874) --- .../SQLCompletionProvider.swift | 29 +++++----- .../SQLContextAnalyzer.swift | 58 ++++++++++++++++--- .../SQLClauseDetectionTests.swift | 22 +++++++ .../SQLCompletionProviderCatalogTests.swift | 29 ++++++++++ ...QLContextAnalyzerTableReferenceTests.swift | 11 ++++ apple/changelog.d/3874.fixed.md | 1 + 6 files changed, 130 insertions(+), 20 deletions(-) create mode 100644 apple/changelog.d/3874.fixed.md diff --git a/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift b/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift index a190774b8c..4c7af0ac3c 100644 --- a/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift +++ b/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift @@ -150,22 +150,25 @@ public final class SQLCompletionProvider { // A dot prefix is a table or alias first, then a schema (its tables) or a database (its schemas, or its // tables on an engine with none). The namespace fallback also covers an alias that resolves to a schema - // name parsed out of the FROM clause. + // name parsed out of the FROM clause. In a table slot it can only qualify the table being named, so a table + // that shares the name is never read for columns. if let dotPrefix = context.dotPrefix { guard let catalog else { return [] } - if let derived = context.tableReferences.first(where: { - $0.isDerived && $0.identifier.caseInsensitiveCompare(dotPrefix) == .orderedSame - }) { - let columns = await derivedColumns(of: derived) - if !columns.isEmpty { - return [columns.map { SQLCompletionItem.column($0, dataType: nil, tableName: derived.identifier) }] + if !context.expectsObjectName { + if let derived = context.tableReferences.first(where: { + $0.isDerived && $0.identifier.caseInsensitiveCompare(dotPrefix) == .orderedSame + }) { + let columns = await derivedColumns(of: derived) + if !columns.isEmpty { + return [columns.map { SQLCompletionItem.column($0, dataType: nil, tableName: derived.identifier) }] + } + } + if let tableName = await catalog.resolveAlias(dotPrefix, in: context.tableReferences) { + let schema = context.tableReferences.first { + $0.tableName.caseInsensitiveCompare(tableName) == .orderedSame + }?.schema + items = await catalog.columnCompletionItems(for: tableName, schema: schema) } - } - if let tableName = await catalog.resolveAlias(dotPrefix, in: context.tableReferences) { - let schema = context.tableReferences.first { - $0.tableName.caseInsensitiveCompare(tableName) == .orderedSame - }?.schema - items = await catalog.columnCompletionItems(for: tableName, schema: schema) } if items.isEmpty { if await catalog.isKnownSchema(dotPrefix) { diff --git a/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLContextAnalyzer.swift b/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLContextAnalyzer.swift index aadeb59b74..841ac697c2 100644 --- a/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLContextAnalyzer.swift +++ b/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLContextAnalyzer.swift @@ -209,6 +209,7 @@ public final class SQLContextAnalyzer { private static let doubleQuote = UInt16(UnicodeScalar("\"").value) private static let backtick = UInt16(UnicodeScalar("`").value) private static let openBracket = UInt16(UnicodeScalar("[").value) + private static let closeBracket = UInt16(UnicodeScalar("]").value) private static let dollar = UInt16(UnicodeScalar("$").value) private static let hash = UInt16(UnicodeScalar("#").value) @@ -260,8 +261,9 @@ public final class SQLContextAnalyzer { // and hold any character. private static let quotedSegment = "[`\"'\\[]?\\w+[`\"'\\]]?" + // A trailing dot stays in the path, so `JOIN sales.` reads as unfinished instead of naming a table `sales`. private static let tableRefRegexes: [NSRegularExpression] = { - let path = "(\(quotedSegment)(?:\\.\(quotedSegment))*)" + let path = "(\(quotedSegment)(?:\\.\(quotedSegment))*\\.?)" let alias = "(?:\\s+(?:AS\\s+)?(\(quotedSegment)))?" let patterns = [ // MySQL's STRAIGHT_JOIN has no word boundary before JOIN. @@ -282,7 +284,7 @@ public final class SQLContextAnalyzer { ) private static let alterTableRegex = compileRegex( - "(?i)\\bALTER\\s+TABLE\\s+(\(quotedSegment)(?:\\.\(quotedSegment))*)" + "(?i)\\bALTER\\s+TABLE\\s+(\(quotedSegment)(?:\\.\(quotedSegment))*\\.?)" ) private static let derivedTableParser = DerivedTableParser() @@ -855,7 +857,13 @@ public final class SQLContextAnalyzer { append(upTo: index) start = index + 1 } - append(upTo: end) + // The name after a trailing dot is still being typed: an empty last segment, so no caller reads `sales.` as + // a table named `sales`. + if start == end, end > range.location { + segments.append(PathSegment(masked: "", name: "", isQuoted: false)) + } else { + append(upTo: end) + } return segments } @@ -1048,10 +1056,6 @@ public final class SQLContextAnalyzer { dotPrefix: String?, currentFunction: String? = nil ) -> ClauseResolution { - if dotPrefix != nil { - return ClauseResolution(clause: .select, expectsObjectName: false) - } - // Bounds the regex work on a long statement. SQLContextAnalyzerWindowingTests pins the size. let windowSize = 5_000 let nsText = textBeforeCursor as NSString @@ -1059,6 +1063,10 @@ public final class SQLContextAnalyzer { ? nsText.substring(from: nsText.length - windowSize) : textBeforeCursor + if dotPrefix != nil { + return ClauseResolution(clause: .select, expectsObjectName: qualifiesTableName(after: windowedText)) + } + // Structural contexts first: `CREATE TABLE test (id ` looks like a call to `test(` but is a column definition. let range = NSRange(location: 0, length: (windowedText as NSString).length) for (regex, clause) in Self.structuralClauseRegexes { @@ -1079,6 +1087,42 @@ public final class SQLContextAnalyzer { return ClauseResolution(clause: .unknown, expectsObjectName: false) } + // `FROM sales.` qualifies the table being named. A FROM after `IS DISTINCT` or inside a call such as + // `EXTRACT(YEAR FROM o.`, and any qualifier this cannot place, stays a table or alias. + private func qualifiesTableName(after textBeforePrefix: String) -> Bool { + let tokens = tokenize(Self.removingTrailingPath(textBeforePrefix)) + guard let keyword = tokens.last, keyword.kind == .word, + ["FROM", "JOIN", "INTO"].contains(keyword.upper) else { return false } + guard keyword.upper == "FROM" else { return true } + if tokens.count >= 2, tokens[tokens.count - 2].upper == "DISTINCT" { return false } + var open: [Int] = [] + for (index, token) in tokens.enumerated() { + if token.kind == .openParen { + open.append(index) + } else if token.kind == .closeParen, !open.isEmpty { + open.removeLast() + } + } + guard let paren = open.last, paren > 0, tokens[paren - 1].kind == .word else { return true } + return Self.groupingKeywords.contains(tokens[paren - 1].upper) + } + + // A parenthesis after one of these opens a subquery or a join group, so a FROM inside it names a table. + private static let groupingKeywords: Set = [ + "FROM", "JOIN", "LATERAL", "AS", "IN", "EXISTS", "ANY", "ALL", "SOME", "UNION", "INTERSECT", "EXCEPT" + ] + + private static func removingTrailingPath(_ text: String) -> String { + let ns = text as NSString + var start = ns.length + while start > 0 { + let ch = ns.character(at: start - 1) + guard SQLTokenBoundary.isTokenChar(ch) || ch == dot || ch == openBracket || ch == closeBracket else { break } + start -= 1 + } + return ns.substring(to: start) + } + // MARK: - Linear Clause Scan private enum ScanTokenKind { diff --git a/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLClauseDetectionTests.swift b/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLClauseDetectionTests.swift index bf73b9a139..9fb3812a87 100644 --- a/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLClauseDetectionTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLClauseDetectionTests.swift @@ -132,6 +132,28 @@ struct SQLClauseDetectionTests { #expect(ctx.expectsObjectName == testCase.expects) } + @Test("A qualifier's dot sits in the table slot only right after a table-naming keyword", arguments: [ + (query: "SELECT * FROM sales.", expects: true), + (query: "SELECT * FROM a JOIN sales.", expects: true), + (query: "INSERT INTO sales.", expects: true), + (query: "SELECT * FROM t WHERE id IN (SELECT id FROM sales.", expects: true), + (query: "SELECT * FROM (WITH c AS (SELECT 1 AS id) SELECT * FROM sales.", expects: true), + (query: "SELECT * FROM (a JOIN sales.", expects: true), + (query: "SELECT o. FROM orders o", expects: false), + (query: "SELECT * FROM orders o WHERE o.", expects: false), + (query: "SELECT * FROM a, sales.", expects: false), + (query: "SELECT EXTRACT(YEAR FROM o.", expects: false), + (query: "SELECT SUBSTR(o.status FROM o.", expects: false), + (query: "SELECT * FROM t WHERE a IS DISTINCT FROM o.", expects: false), + (query: "SELECT * FROM orders o, generate_series(1, o.", expects: false), + ]) + func dotInOperandSlot(_ testCase: (query: String, expects: Bool)) { + let cursor = testCase.query.hasPrefix("SELECT o.") ? 9 : (testCase.query as NSString).length + let ctx = analyzer.analyze(query: testCase.query, cursorPosition: cursor) + #expect(ctx.dotPrefix != nil) + #expect(ctx.expectsObjectName == testCase.expects) + } + // MARK: - String and Comment Guards @Test("Clause keywords inside a string literal are ignored") diff --git a/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLCompletionProviderCatalogTests.swift b/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLCompletionProviderCatalogTests.swift index cb4c923171..b2d62173dd 100644 --- a/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLCompletionProviderCatalogTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLCompletionProviderCatalogTests.swift @@ -102,6 +102,35 @@ struct SQLCompletionProviderCatalogTests { #expect(Set(session.items.map(\.label)) == ["orders", "customers"]) } + // On Oracle the column read for a table that was never there ran first and queued the schema's table list + // behind it on one connection. + @Test("A qualifier in a table slot offers its schema's tables, even beside a table of that name") + func tableSlotQualifierIsASchema() async { + let catalog = StubCatalog( + tables: ["audit"], columns: ["audit": ["id", "action"]], schemas: ["audit"], + schemaTables: ["audit": ["events"]] + ) + let provider = SQLCompletionProvider(catalog: catalog, grammar: .engine("PostgreSQL"), vocabulary: .generic) + for text in ["SELECT * FROM audit.", "SELECT * FROM orders o JOIN audit.", "INSERT INTO audit."] { + let session = await provider.completionSession(text: text, cursorPosition: (text as NSString).length) + #expect(session.items.map(\.label) == ["events"], "\(text)") + } + let columns = await provider.completionSession(text: "SELECT audit. FROM audit", cursorPosition: 13) + #expect(Set(columns.items.map(\.label)) == ["id", "action"]) + } + + @Test("A FROM inside an expression keeps an alias's columns", arguments: [ + "SELECT EXTRACT(YEAR FROM o.|) FROM orders o", + "SELECT SUBSTRING(o.status FROM o.|) FROM orders o", + "SELECT SUBSTR(o.status FROM o.|) FROM orders o", + "SELECT * FROM orders o WHERE o.total IS DISTINCT FROM o.|", + "SELECT * FROM orders o WHERE o.total IS NOT DISTINCT FROM o.|", + "SELECT * FROM orders o, generate_series(1, o.|)" + ]) + func expressionFromKeepsAliasColumns(_ marked: String) async { + #expect(Set(await labels(marked)) == ["id", "total", "status"]) + } + @Test("UPDATE offers tables") func updateOffersTables() async { let session = await provider.completionSession(text: "UPDATE or", cursorPosition: 9) diff --git a/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLContextAnalyzerTableReferenceTests.swift b/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLContextAnalyzerTableReferenceTests.swift index f45cfea954..3db7400b72 100644 --- a/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLContextAnalyzerTableReferenceTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLContextAnalyzerTableReferenceTests.swift @@ -38,6 +38,17 @@ struct SQLContextAnalyzerTableReferenceTests { #expect(tables("ALTER TABLE invoices ADD COLUMN due DATE") == ["invoices"]) } + @Test("A path that ends in a dot names no table yet") + func unfinishedPath() { + #expect(tables("SELECT * FROM sales.").isEmpty) + #expect(tables("SELECT * FROM orders o JOIN sales.") == ["orders"]) + #expect(tables("UPDATE sales.").isEmpty) + #expect(tables("INSERT INTO sales.").isEmpty) + #expect(tables("ALTER TABLE sales.").isEmpty) + #expect(analyzer.analyze(query: "SELECT * FROM sales.", cursorPosition: 20).tableReferences.isEmpty) + #expect(tables("SELECT * FROM sales.orders") == ["orders"]) + } + @Test("A schema-qualified ALTER target keeps its schema") func qualifiedAlterTarget() { let references = analyzer.tableReferences(in: "ALTER TABLE archive.orders ADD COLUMN due DATE", grammar: .ansi) diff --git a/apple/changelog.d/3874.fixed.md b/apple/changelog.d/3874.fixed.md new file mode 100644 index 0000000000..a022de62b5 --- /dev/null +++ b/apple/changelog.d/3874.fixed.md @@ -0,0 +1 @@ +Autocomplete after a schema name and a dot could list a same-named table's columns, or time out on Oracle. From 83d61b281aa394082eaf4141c33dcc663632c654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Thu, 29 Oct 2026 19:14:53 +0700 Subject: [PATCH 02/85] feat(structure): rebuild the Structure tab as one Definition page with an inspector (#3872) --- .claude/rules/data-grid.md | 1 + apple/.swiftlint.yml | 2 +- .../Core/DataGrid/DataGridModel.swift | 7 + .../TablePro/Core/Menu/FileMenuBuilder.swift | 4 + .../CreateTableStatementComposer.swift | 35 +- .../SchemaStatementGenerator.swift | 39 +- .../StructureChangeManager.swift | 187 ++++-- ...nSplitViewController+FileMenuActions.swift | 4 + ...inSplitViewController+MenuValidation.swift | 14 +- .../Connection/ConnectionToolbarState.swift | 2 - .../Models/Query/ResultStatusModel.swift | 4 - .../Models/Query/StatusBarSnapshot.swift | 6 - .../Schema/CreateTableDraftBuilder.swift | 171 +++-- .../TablePro/Models/Schema/SchemaChange.swift | 51 ++ .../Models/Schema/SchemaDraftIssue.swift | 23 +- .../Schema/SchemaEntityOperations.swift | 11 +- ...uctureTab.swift => StructureSection.swift} | 25 +- .../Models/UI/DataGridConfiguration.swift | 6 + .../TablePro/Models/UI/FieldEditorKind.swift | 5 - .../Models/UI/InspectorFieldLayout.swift | 63 -- .../Models/UI/InspectorRowSource.swift | 37 -- .../TablePro/Models/UI/InspectorSubject.swift | 56 +- .../Models/UI/MultiRowEditState.swift | 66 +- .../Models/UI/PendingChangeKind.swift | 38 +- .../Models/UI/RowInspectorContext.swift | 51 +- .../TablePro/Resources/Localizable.xcstrings | 205 ++++++ .../DataFiles/DataFileRowDetailsView.swift | 1 - .../DataGrid/Cells/DataGridCellContent.swift | 5 +- .../Cells/DataGridCellPresenter.swift | 8 +- .../DataGrid/Cells/DataGridMetrics.swift | 10 + .../DataGrid/DataGridContainerView.swift | 31 +- .../Views/DataGrid/DataGridScrollView.swift | 57 +- .../Views/DataGrid/DataGridTableView.swift | 17 +- .../Views/DataGrid/DataGridView.swift | 7 +- .../DataGridViewController+Changes.swift | 29 + .../DataGridViewController+Selection.swift | 53 +- .../DataGrid/DataGridViewController.swift | 12 + .../Main/Child/MainEditorContentView.swift | 29 +- .../MainContentCoordinator+GridModels.swift | 2 + .../MainContentCoordinator+Protection.swift | 75 +-- .../MainContentCoordinator+Refresh.swift | 2 +- .../MainContentCoordinator+SQLPreview.swift | 14 +- .../MainContentCoordinator+Structure.swift | 25 + .../MainContentCoordinator+TabClosing.swift | 2 - ...nContentCoordinator+TableRowsRefresh.swift | 4 +- .../Extensions/MainContentView+Bindings.swift | 42 +- .../MainContentView+EventHandlers.swift | 38 +- .../Extensions/MainContentView+Helpers.swift | 16 +- .../Extensions/MainContentView+Setup.swift | 25 +- .../MainContentCommandActions+Structure.swift | 117 ++++ .../MainContentCommandActions+UndoState.swift | 78 ++- .../Main/MainContentCommandActions.swift | 90 ++- .../Views/Main/MainContentCoordinator.swift | 8 - .../TablePro/Views/Main/MainContentView.swift | 50 +- .../Views/Results/ResultStatusBar.swift | 17 - .../FieldEditors/FieldEditorContent.swift | 17 +- .../FieldEditors/FieldEditorContext.swift | 12 +- .../FieldEditors/SchemaTextFieldView.swift | 49 -- .../FieldEditors/TypePickerFieldView.swift | 44 -- .../FieldEditors/ValuePickerFieldView.swift | 101 --- .../RowInspector/InspectorFieldListView.swift | 18 +- .../RowInspector/InspectorFieldRow.swift | 64 +- .../Views/RowInspector/RowInspectorView.swift | 69 +- .../Views/RowInspector/TableInfoView.swift | 120 ++-- .../FieldEditors/FieldEditorResolver.swift | 1 - .../Views/Structure/ClickHousePartsView.swift | 2 +- .../Structure/CreateTableActionHandler.swift | 13 - .../Views/Structure/CreateTableDraft.swift | 450 +++++++++++-- .../Structure/CreateTableFormEditor.swift | 90 --- .../Structure/CreateTableGridAdapter.swift | 327 ---------- .../Views/Structure/CreateTableView.swift | 610 ++++-------------- .../Inspector/CreateTablePropertiesView.swift | 117 ++++ .../StructureDefinitionFormView.swift | 165 +++++ .../StructureInspectorControls.swift | 232 +++++++ .../Inspector/StructureInspectorForm.swift | 116 ++++ .../StructureInspectorPresentation.swift | 121 ++++ .../Inspector/StructureInspectorView.swift | 89 +++ .../StructureTablePropertiesView.swift | 439 +++++++++++++ .../Views/Structure/StructureDDLPane.swift | 133 ++++ .../Structure/StructureDefinitionEditor.swift | 360 +++++++++++ .../Structure/StructureDefinitionPage.swift | 242 +++++++ .../Views/Structure/StructureEditGate.swift | 9 +- .../Structure/StructureEditingContext.swift | 97 +++ .../StructureEditingSession+Apply.swift | 1 + .../StructureEditingSession+Baseline.swift | 44 -- ...ructureEditingSession+ColumnReorder.swift} | 15 +- .../StructureEditingSession+Commands.swift | 132 ++++ .../StructureEditingSession+Loading.swift | 298 +++++++++ .../Structure/StructureEditingSession.swift | 155 ++--- .../Structure/StructureFooterPolicy.swift | 99 +-- .../StructureGridAdapter+Inspector.swift | 28 - .../Structure/StructureGridAdapter.swift | 546 ---------------- .../Structure/StructureGridStateStore.swift | 67 +- .../Structure/StructureGridSupport.swift | 109 ++-- .../StructureInspectorRowBuilder.swift | 75 --- .../Views/Structure/StructureLoadState.swift | 64 ++ .../Structure/StructureRowProvider.swift | 82 +-- .../Views/Structure/StructureSavePlan.swift | 4 +- ...ft => StructureSectionAdapter+Menus.swift} | 136 ++-- .../Structure/StructureSectionAdapter.swift | 517 +++++++++++++++ .../Structure/StructureServerSupport.swift | 4 +- .../Structure/StructureTabAvailability.swift | 43 -- .../Structure/StructureTabDataState.swift | 41 -- .../StructureTableRebuildHandler.swift | 2 +- .../StructureViewActionHandler.swift | 23 - .../Views/Structure/TableOptionsView.swift | 281 -------- .../TableStructureView+DataLoading.swift | 259 -------- .../TableStructureView+EditGate.swift | 38 -- .../Structure/TableStructureView+Schema.swift | 187 ------ .../TableStructureView+TableComment.swift | 85 --- .../TableStructureView+TableOptions.swift | 46 -- .../Views/Structure/TableStructureView.swift | 452 +++---------- .../Views/Structure/TriggerDetailView.swift | 16 +- .../Database/TriggerInfoMappingTests.swift | 26 +- .../CreateTableCommentCompositionTests.swift | 2 +- .../IndexDecliningDriverTests.swift | 2 +- .../PrimaryKeyStatementOrderTests.swift | 86 +++ .../SchemaOperationRefusalTests.swift | 4 +- .../StructureChangeManagerSaveHoldTests.swift | 11 +- .../StructureChangeManagerStagingTests.swift | 273 ++++++++ ...uctureChangeManagerTableCommentTests.swift | 41 +- ...uctureChangeManagerTableOptionsTests.swift | 12 - .../StructureChangeManagerUndoTests.swift | 38 +- .../StructureChangeNameReuseTests.swift | 52 +- ...ctureEditingSessionTableOptionsTests.swift | 8 - .../TableCommentStatementTests.swift | 2 +- .../TableProTests/Helpers/TestFixtures.swift | 9 + .../Models/InspectorFieldLayoutTests.swift | 126 ---- .../Models/InspectorSubjectTests.swift | 39 +- .../Models/MultiRowEditStateTests.swift | 98 --- .../Models/PendingChangeKindTests.swift | 13 + .../Models/ResultStatusModelTests.swift | 15 - .../Schema/CreateTableDraftBuilderTests.swift | 68 +- .../Schema/CreateTableFormStateTests.swift | 34 +- .../DataGrid/DataGridFittingHeightTests.swift | 336 ++++++++++ .../Main/CommandActionsDispatchTests.swift | 158 ++--- .../MainContentCoordinatorRefreshTests.swift | 19 +- .../Main/ResultStatusBarLayoutTests.swift | 28 +- .../Main/StructureActionHandlerTests.swift | 220 ------- .../Main/StructureCommandRoutingTests.swift | 187 ++++++ .../Views/Main/TabCloseProtectionTests.swift | 8 +- .../Views/Main/TriggerStructTests.swift | 9 + .../Views/Main/UserTypeTabStateTests.swift | 1 - .../InspectorPopOutWindowTests.swift | 1 - .../InspectorValueThemeTests.swift | 4 +- .../SplitItemGlassBackgroundTests.swift | 2 +- .../CreateTableDraftCommandTests.swift | 145 +++++ .../CreateTableDraftCompositionTests.swift | 152 ++++- .../StructureAvailabilityTests.swift | 136 ++++ .../StructureCodeStandInThemeTests.swift | 2 +- .../StructureDefinitionEditorTests.swift | 298 +++++++++ .../Structure/StructureEditGateTests.swift | 1 - .../StructureEditingSessionTests.swift | 400 +++++++++--- ...tructureEditingSupportFieldDiffTests.swift | 39 +- .../StructureFooterPolicyTests.swift | 225 ++++--- .../StructureGridAdapterAddRowTests.swift | 204 ------ .../StructureGridAdapterInspectorTests.swift | 278 -------- .../StructureGridStateStoreTests.swift | 204 ++++-- .../StructureGridValueFilterEditTests.swift | 102 +-- .../StructureIndexTypeMenuTests.swift | 72 ++- .../StructureInspectorFormTests.swift | 228 +++++++ .../StructureInspectorPresentationTests.swift | 173 +++++ .../StructureInspectorRowBuilderTests.swift | 182 ------ .../StructureListGridAccessibilityTests.swift | 2 +- .../Structure/StructureLoadStateTests.swift | 148 +++++ .../StructureRowMenuRouteTests.swift | 111 ++-- ...uctureRowProviderBooleanOptionsTests.swift | 8 +- .../Structure/StructureRowProviderTests.swift | 2 +- .../StructureSectionAdapterTests.swift | 285 ++++++++ .../StructureServerSupportTests.swift | 4 +- .../StructureTabAvailabilityTests.swift | 69 -- .../StructureTabDataStateTests.swift | 88 --- .../SidebarAddMenuUITests.swift | 24 +- .../StructureSaveConfirmationUITests.swift | 2 +- .../TableChangeReloadUITests.swift | 2 +- apple/changelog.d/3872.added.md | 1 + apple/changelog.d/3872.changed.md | 1 + apple/changelog.d/3872.fixed.1.md | 1 + apple/changelog.d/3872.fixed.2.md | 1 + apple/changelog.d/3872.fixed.3.md | 1 + apple/changelog.d/3872.fixed.4.md | 1 + apple/changelog.d/3872.fixed.5.md | 1 + apple/changelog.d/3872.fixed.6.md | 1 + apple/changelog.d/3872.fixed.7.md | 1 + apple/changelog.d/3872.fixed.8.md | 1 + apple/changelog.d/3872.fixed.md | 1 + 186 files changed, 8940 insertions(+), 6522 deletions(-) rename apple/TablePro/Models/Schema/{StructureTab.swift => StructureSection.swift} (52%) delete mode 100644 apple/TablePro/Models/UI/InspectorFieldLayout.swift delete mode 100644 apple/TablePro/Models/UI/InspectorRowSource.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Structure.swift create mode 100644 apple/TablePro/Views/Main/MainContentCommandActions+Structure.swift delete mode 100644 apple/TablePro/Views/RowInspector/FieldEditors/SchemaTextFieldView.swift delete mode 100644 apple/TablePro/Views/RowInspector/FieldEditors/TypePickerFieldView.swift delete mode 100644 apple/TablePro/Views/RowInspector/FieldEditors/ValuePickerFieldView.swift delete mode 100644 apple/TablePro/Views/Structure/CreateTableActionHandler.swift delete mode 100644 apple/TablePro/Views/Structure/CreateTableFormEditor.swift delete mode 100644 apple/TablePro/Views/Structure/CreateTableGridAdapter.swift create mode 100644 apple/TablePro/Views/Structure/Inspector/CreateTablePropertiesView.swift create mode 100644 apple/TablePro/Views/Structure/Inspector/StructureDefinitionFormView.swift create mode 100644 apple/TablePro/Views/Structure/Inspector/StructureInspectorControls.swift create mode 100644 apple/TablePro/Views/Structure/Inspector/StructureInspectorForm.swift create mode 100644 apple/TablePro/Views/Structure/Inspector/StructureInspectorPresentation.swift create mode 100644 apple/TablePro/Views/Structure/Inspector/StructureInspectorView.swift create mode 100644 apple/TablePro/Views/Structure/Inspector/StructureTablePropertiesView.swift create mode 100644 apple/TablePro/Views/Structure/StructureDDLPane.swift create mode 100644 apple/TablePro/Views/Structure/StructureDefinitionEditor.swift create mode 100644 apple/TablePro/Views/Structure/StructureDefinitionPage.swift create mode 100644 apple/TablePro/Views/Structure/StructureEditingContext.swift delete mode 100644 apple/TablePro/Views/Structure/StructureEditingSession+Baseline.swift rename apple/TablePro/Views/Structure/{TableStructureView+ColumnReorder.swift => StructureEditingSession+ColumnReorder.swift} (93%) create mode 100644 apple/TablePro/Views/Structure/StructureEditingSession+Commands.swift create mode 100644 apple/TablePro/Views/Structure/StructureEditingSession+Loading.swift delete mode 100644 apple/TablePro/Views/Structure/StructureGridAdapter+Inspector.swift delete mode 100644 apple/TablePro/Views/Structure/StructureGridAdapter.swift delete mode 100644 apple/TablePro/Views/Structure/StructureInspectorRowBuilder.swift create mode 100644 apple/TablePro/Views/Structure/StructureLoadState.swift rename apple/TablePro/Views/Structure/{StructureGridAdapter+Menus.swift => StructureSectionAdapter+Menus.swift} (69%) create mode 100644 apple/TablePro/Views/Structure/StructureSectionAdapter.swift delete mode 100644 apple/TablePro/Views/Structure/StructureTabAvailability.swift delete mode 100644 apple/TablePro/Views/Structure/StructureTabDataState.swift delete mode 100644 apple/TablePro/Views/Structure/StructureViewActionHandler.swift delete mode 100644 apple/TablePro/Views/Structure/TableOptionsView.swift delete mode 100644 apple/TablePro/Views/Structure/TableStructureView+DataLoading.swift delete mode 100644 apple/TablePro/Views/Structure/TableStructureView+EditGate.swift delete mode 100644 apple/TablePro/Views/Structure/TableStructureView+Schema.swift delete mode 100644 apple/TablePro/Views/Structure/TableStructureView+TableComment.swift delete mode 100644 apple/TablePro/Views/Structure/TableStructureView+TableOptions.swift create mode 100644 apple/TableProTests/Core/SchemaTracking/PrimaryKeyStatementOrderTests.swift create mode 100644 apple/TableProTests/Core/SchemaTracking/StructureChangeManagerStagingTests.swift delete mode 100644 apple/TableProTests/Models/InspectorFieldLayoutTests.swift create mode 100644 apple/TableProTests/Views/DataGrid/DataGridFittingHeightTests.swift delete mode 100644 apple/TableProTests/Views/Main/StructureActionHandlerTests.swift create mode 100644 apple/TableProTests/Views/Main/StructureCommandRoutingTests.swift create mode 100644 apple/TableProTests/Views/Structure/CreateTableDraftCommandTests.swift create mode 100644 apple/TableProTests/Views/Structure/StructureAvailabilityTests.swift create mode 100644 apple/TableProTests/Views/Structure/StructureDefinitionEditorTests.swift delete mode 100644 apple/TableProTests/Views/Structure/StructureGridAdapterAddRowTests.swift delete mode 100644 apple/TableProTests/Views/Structure/StructureGridAdapterInspectorTests.swift create mode 100644 apple/TableProTests/Views/Structure/StructureInspectorFormTests.swift create mode 100644 apple/TableProTests/Views/Structure/StructureInspectorPresentationTests.swift delete mode 100644 apple/TableProTests/Views/Structure/StructureInspectorRowBuilderTests.swift create mode 100644 apple/TableProTests/Views/Structure/StructureLoadStateTests.swift create mode 100644 apple/TableProTests/Views/Structure/StructureSectionAdapterTests.swift delete mode 100644 apple/TableProTests/Views/Structure/StructureTabAvailabilityTests.swift delete mode 100644 apple/TableProTests/Views/Structure/StructureTabDataStateTests.swift create mode 100644 apple/changelog.d/3872.added.md create mode 100644 apple/changelog.d/3872.changed.md create mode 100644 apple/changelog.d/3872.fixed.1.md create mode 100644 apple/changelog.d/3872.fixed.2.md create mode 100644 apple/changelog.d/3872.fixed.3.md create mode 100644 apple/changelog.d/3872.fixed.4.md create mode 100644 apple/changelog.d/3872.fixed.5.md create mode 100644 apple/changelog.d/3872.fixed.6.md create mode 100644 apple/changelog.d/3872.fixed.7.md create mode 100644 apple/changelog.d/3872.fixed.8.md create mode 100644 apple/changelog.d/3872.fixed.md diff --git a/.claude/rules/data-grid.md b/.claude/rules/data-grid.md index 272876fd8b..ef60182664 100644 --- a/.claude/rules/data-grid.md +++ b/.claude/rules/data-grid.md @@ -30,6 +30,7 @@ Three layers, knowledge pointing one way: an owner's adapter, then `DataGridMode - **Row height, row count, reloads, inserts and removes reach both panes in one call**, so their document heights never differ. - **Vertical scroll stays in step through bounds-change notifications**: each pane's clip moves the other with `clipView.scroll(to:)` and `reflectScrolledClipView`, only when its y differs. Anywhere else scroll with `NSView.scroll(_:)`: moving a clip's x that way leaves the header behind. - **The frozen pane forwards every wheel event to the main scroll view**, so one momentum engine moves both. +- **A `fitsRows` grid is as tall as its rows and scrolls only sideways.** It publishes `model.fittingHeight` for its host to size it, hands a vertical gesture to the enclosing scroll view (the axis is decided once per gesture), and reveals a row by scrolling that page. The Structure tab's sections use it. - **Frozen columns take at most two thirds of the grid** (`DataGridMetrics.maximumFrozenShare`). Columns past that scroll, and Freeze Up to This Column is refused in the menu. - **`DataGridTableView.scrollToVisible(_:)` widens the rect leftward by the frozen width**, so a revealed column never parks under the frozen pane. - **A column moves only within its side of the freeze line.** A freeze or unfreeze rebuilds both panes' column lists from the set, writing width and position explicitly: remove-then-add alone strands a column at the end and resets its width. diff --git a/apple/.swiftlint.yml b/apple/.swiftlint.yml index c31e1bc9cd..698a5f7bcd 100644 --- a/apple/.swiftlint.yml +++ b/apple/.swiftlint.yml @@ -214,7 +214,7 @@ custom_rules: severity: error appstorage_key_namespace: name: "AppStorage Key Namespace" - regex: '@AppStorage\(\s*"(?!com\.TablePro)(?!(?:hideExportSuccessDialog|skipSchemaPreview|structureCodeFontSize)")[^"\\]+"' + regex: '@AppStorage\(\s*"(?!com\.TablePro)(?!(?:hideExportSuccessDialog|structureCodeFontSize)")[^"\\]+"' excluded: ['.*/(Packages|Plugins)/'] message: "Route an @AppStorage key through the preferences layer, under com.TablePro." severity: error diff --git a/apple/TablePro/Core/DataGrid/DataGridModel.swift b/apple/TablePro/Core/DataGrid/DataGridModel.swift index 9e9cd17f68..5e665060df 100644 --- a/apple/TablePro/Core/DataGrid/DataGridModel.swift +++ b/apple/TablePro/Core/DataGrid/DataGridModel.swift @@ -51,6 +51,8 @@ final class DataGridModel: ObservableObject { @Published private(set) var displayRevision = 0 @Published private(set) var selectionRevision = 0 @Published private(set) var editabilityRevision = 0 + // What a fitsRows grid needs to show every row; the mounted view measures it. + @Published private(set) var fittingHeight: CGFloat = 0 private(set) weak var mountedView: (any DataGridMountedView)? private var orderRevision = 0 @@ -456,6 +458,11 @@ final class DataGridModel: ObservableObject { var isMounted: Bool { mountedView != nil } + func setFittingHeight(_ height: CGFloat) { + guard height != fittingHeight else { return } + fittingHeight = height + } + func revealColumn(_ id: DataGridColumnID) { guard let position = columns.displayPosition(of: id) else { return } mountedView?.revealColumn(position) diff --git a/apple/TablePro/Core/Menu/FileMenuBuilder.swift b/apple/TablePro/Core/Menu/FileMenuBuilder.swift index 7c01c4f0f6..0c01e01829 100644 --- a/apple/TablePro/Core/Menu/FileMenuBuilder.swift +++ b/apple/TablePro/Core/Menu/FileMenuBuilder.swift @@ -74,6 +74,10 @@ enum FileMenuBuilder { shortcut: .saveChanges, keyboard: keyboard ), + MenuItemFactory.item( + String(localized: "Discard Changes…"), + action: #selector(MainSplitViewController.discardChanges(_:)) + ), MenuItemFactory.item( String(localized: "Save As…"), action: #selector(MainSplitViewController.saveDocumentAs(_:)), diff --git a/apple/TablePro/Core/SchemaTracking/CreateTableStatementComposer.swift b/apple/TablePro/Core/SchemaTracking/CreateTableStatementComposer.swift index 67550010d3..32bbab5f40 100644 --- a/apple/TablePro/Core/SchemaTracking/CreateTableStatementComposer.swift +++ b/apple/TablePro/Core/SchemaTracking/CreateTableStatementComposer.swift @@ -9,8 +9,7 @@ import TableProPluginKit struct CreateTableStatements { let statements: [String] let issues: [SchemaDraftIssue] - /// The name the statement actually creates, trimmed. The draft's own field may carry - /// surrounding whitespace, and opening the tab under that text loads a table nobody made. + // Trimmed: opening a tab under the draft's untrimmed text loads a table nobody made. let tableName: String? var preview: String { @@ -18,8 +17,7 @@ struct CreateTableStatements { } } -/// One statement per element, because a driver is only obliged to run one statement per -/// `execute(query:)`: SQLite prepares with a nil tail, so anything after the first semicolon never runs. +// One statement per element: a driver need only run one per execute, and SQLite drops anything after the first. @MainActor enum CreateTableStatementComposer { static func compose( @@ -33,12 +31,12 @@ enum CreateTableStatementComposer { var issues = plan.issues let columnRefusals = definition.columns.compactMap { driver.schemaOperationRefusal(.addColumn($0)) } - issues += columnRefusals.map { SchemaDraftIssue(tab: .columns, row: nil, message: $0) } - var refusedIndexRows: Set = [] - for (row, index) in plan.indexes.enumerated() { + issues += columnRefusals.map { SchemaDraftIssue(section: .columns, row: nil, message: $0) } + var refusedIndexes: Set = [] + for (position, index) in plan.indexes.enumerated() { guard let reason = driver.schemaOperationRefusal(.addIndex(index)) else { continue } - refusedIndexRows.insert(row) - issues.append(SchemaDraftIssue(tab: .indexes, row: row, message: reason)) + refusedIndexes.insert(position) + issues.append(SchemaDraftIssue(section: .indexes, row: plan.indexRow(at: position), message: reason)) } guard columnRefusals.isEmpty else { return CreateTableStatements(statements: [], issues: issues, tableName: definition.tableName) @@ -46,17 +44,17 @@ enum CreateTableStatementComposer { guard let createTable = driver.generateCreateTableStatements(definition: definition), !createTable.isEmpty else { issues.append(SchemaDraftIssue( - tab: .columns, row: nil, + section: .columns, row: nil, message: String(localized: "This database cannot create a table from the visual editor.") )) return CreateTableStatements(statements: [], issues: issues, tableName: definition.tableName) } var statements = createTable - for (row, index) in plan.indexes.enumerated() where !refusedIndexRows.contains(row) { + for (position, index) in plan.indexes.enumerated() where !refusedIndexes.contains(position) { guard let sql = driver.generateAddIndexSQL(table: definition.tableName, index: index) else { issues.append(SchemaDraftIssue( - tab: .indexes, row: row, + section: .indexes, row: plan.indexRow(at: position), message: String(localized: "Create Table cannot add an index on this database.") )) continue @@ -64,6 +62,17 @@ enum CreateTableStatementComposer { statements.append(sql) } + for (position, constraint) in plan.checkConstraints.enumerated() { + guard let sql = driver.generateAddCheckConstraintSQL(table: definition.tableName, constraint: constraint) else { + issues.append(SchemaDraftIssue( + section: .checkConstraints, row: plan.checkConstraintRow(at: position), + message: String(localized: "Create Table cannot add a check constraint on this database.") + )) + continue + } + statements.append(sql) + } + if let comment = plan.tableComment { if let sql = driver.objectCommentStatement( name: definition.tableName, @@ -74,7 +83,7 @@ enum CreateTableStatementComposer { statements.append(sql) } else { issues.append(SchemaDraftIssue( - tab: .columns, row: nil, + section: .columns, row: nil, message: String(localized: "This database cannot store a table comment.") )) } diff --git a/apple/TablePro/Core/SchemaTracking/SchemaStatementGenerator.swift b/apple/TablePro/Core/SchemaTracking/SchemaStatementGenerator.swift index 5ecd62935d..73082f022b 100644 --- a/apple/TablePro/Core/SchemaTracking/SchemaStatementGenerator.swift +++ b/apple/TablePro/Core/SchemaTracking/SchemaStatementGenerator.swift @@ -58,7 +58,7 @@ struct SchemaStatementGenerator { throw NSError( domain: "SchemaStatementGenerator", code: -1, - userInfo: [NSLocalizedDescriptionKey: String(format: String(localized: "Unsupported schema operation: %@"), change.description)] + userInfo: [NSLocalizedDescriptionKey: String(format: String(localized: "Unsupported schema operation: %@"), change.localizedDescription)] ) } for stmt in stmts { @@ -175,10 +175,43 @@ struct SchemaStatementGenerator { let constraintHandoffs = Self.inNameOrder(constraintModifies) let indexHandoffs = Self.inNameOrder(indexAdds) + let keyHandoff = Self.keyDropsBeforeColumnDrops(pkChanges, columnDeletes: columnDeletes) return constraintDeletes + constraintHandoffs.drops + constraintHandoffs.ordered + fkDeletes - + indexDeletes + indexHandoffs.drops + columnDeletes + characterSetChanges + columnModifies + columnAdds - + pkChanges + autoIncrementChanges + indexHandoffs.ordered + fkAdds + constraintAdds + commentChanges + + indexDeletes + indexHandoffs.drops + keyHandoff.drops + columnDeletes + characterSetChanges + + columnModifies + columnAdds + keyHandoff.adds + autoIncrementChanges + indexHandoffs.ordered + fkAdds + + constraintAdds + commentChanges + } + + // Dropping a key column takes the key with it, so the key's own DROP would then fail. A key whose every column is + // dropped is left to the database and only the new key is added; one that keeps some is dropped first. + private static func keyDropsBeforeColumnDrops( + _ keyChanges: [SchemaChange], + columnDeletes: [SchemaChange] + ) -> (drops: [SchemaChange], adds: [SchemaChange]) { + let dropped = Set(columnDeletes.compactMap { change -> String? in + guard case .deleteColumn(let column) = change else { return nil } + return column.name + }) + var drops: [SchemaChange] = [] + var adds: [SchemaChange] = [] + for change in keyChanges { + guard case .modifyPrimaryKey(let old, let new) = change, !old.isEmpty, old.contains(where: dropped.contains) else { + adds.append(change) + continue + } + if old.allSatisfy(dropped.contains) { + if !new.isEmpty { + adds.append(.modifyPrimaryKey(old: [], new: new)) + } + continue + } + drops.append(.modifyPrimaryKey(old: old, new: [])) + if !new.isEmpty { + adds.append(.modifyPrimaryKey(old: [], new: new)) + } + } + return (drops, adds) } /// Orders changes that each give up one name and take another in a single step, a rename or a diff --git a/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift b/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift index c836751365..68e54b12b5 100644 --- a/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift +++ b/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift @@ -31,7 +31,9 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { @Published var workingIndexes: [EditableIndexDefinition] = [] @Published var workingForeignKeys: [EditableForeignKeyDefinition] = [] @Published var workingCheckConstraints: [EditableCheckConstraintDefinition] = [] - @Published var workingPrimaryKey: [String] = [] + // Derived from the columns after every change: the ones that stay and are marked Primary Key, the loaded members in + // the loaded key's order, new ones after them in column order. A key nobody changed follows a rename by itself. + @Published private(set) var workingPrimaryKey: [String] = [] @Published var tableName: String? @@ -85,18 +87,41 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { return manager }() - var canUndo: Bool { !isHeldForSave && (undoManager.canUndo || hasTableCommentRun) } - var canRedo: Bool { !isHeldForSave && !hasTableCommentRun && undoManager.canRedo } + var canUndo: Bool { !isHeldForSave && (undoManager.canUndo || hasTableCommentRun || typingRun != nil) } + var canRedo: Bool { !isHeldForSave && !hasTableCommentRun && typingRun == nil && undoManager.canRedo } - /// The DDL and Triggers sub-tabs show no grid, so undo there reaches only the comment. - var undoesTableCommentNext: Bool { - hasTableCommentRun || (undoManager.canUndo && undoManager.undoActionName == Self.tableCommentActionName) + var undoMenuItemTitle: String { + if hasTableCommentRun { return undoManager.undoMenuTitle(forUndoActionName: Self.tableCommentActionName) } + if let typingRun { return undoManager.undoMenuTitle(forUndoActionName: typingRun.actionName) } + return undoManager.undoMenuItemTitle } - var redoesTableCommentNext: Bool { - !hasTableCommentRun && undoManager.canRedo && undoManager.redoActionName == Self.tableCommentActionName + // Typing in one field of one definition: every keystroke is staged, so Save, Refresh and the close prompt see it, + // and the whole run undoes as one step. Anything else that registers an undo ends it first. + private var typingRun: (key: String, actionName: String)? + private var isStagingTyping = false + + func stageTyping(in key: String, actionName: String, _ body: () -> Void) { + guard !isHeldForSave else { return } + if typingRun?.key != key { endTypingRun() } + if typingRun == nil { + endTableCommentRun() + undoManager.beginUndoGrouping() + typingRun = (key, actionName) + } + isStagingTyping = true + defer { isStagingTyping = false } + body() } + func endTypingRun() { + guard typingRun != nil else { return } + typingRun = nil + undoManager.endUndoGrouping() + } + + var redoMenuItemTitle: String { undoManager.redoMenuItemTitle } + /// Mirrors `DataChangeManager.registerUndo`. The `groupingLevel` check is what lets /// `performAsOneUndoStep` nest: inside one, a group is already open and this adds to it rather /// than closing a group the batch still needs. An open comment run ends first, so an edit made @@ -104,6 +129,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { private func registerUndo(_ actionName: String, _ handler: @escaping (StructureChangeManager) -> Void) { if !undoManager.isUndoing, !undoManager.isRedoing { endTableCommentRun() + if !isStagingTyping { endTypingRun() } } let opensOwnGroup = !undoManager.groupsByEvent && undoManager.groupingLevel == 0 if opensOwnGroup { undoManager.beginUndoGrouping() } @@ -118,6 +144,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { func performAsOneUndoStep(_ body: () -> Void) { guard !isHeldForSave else { return } endTableCommentRun() + if !isStagingTyping { endTypingRun() } undoManager.beginUndoGrouping() defer { undoManager.endUndoGrouping() } body() @@ -137,6 +164,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { autoIncrementConfirmed: Bool = true ) { guard !isHeldForSave else { return } + endTypingRun() self.tableName = tableName self.tableComment = ObjectCommentDraft(original: tableComment) tableCommentRunStart = nil @@ -155,6 +183,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { self.currentForeignKeys = EditableForeignKeyDefinition.grouping(foreignKeys).sorted { $0.name < $1.name } self.currentCheckConstraints = checkConstraints.map { EditableCheckConstraintDefinition.from($0) } self.currentPrimaryKey = primaryKey + keepIdentitiesAcrossReload() resetWorkingState() @@ -169,6 +198,34 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { reloadVersion += 1 } + // Every fetch mints fresh ids; a definition that comes back under a name it had keeps its id, so the grid + // selection and the inspector survive a refresh or a save. + private func keepIdentitiesAcrossReload() { + currentColumns = Self.keepingIDs(of: currentColumns, from: workingColumns, \.name, \.id) + currentIndexes = Self.keepingIDs(of: currentIndexes, from: workingIndexes, \.name, \.id) + currentForeignKeys = Self.keepingIDs(of: currentForeignKeys, from: workingForeignKeys, \.name, \.id) + currentCheckConstraints = Self.keepingIDs(of: currentCheckConstraints, from: workingCheckConstraints, \.name, \.id) + } + + private static func keepingIDs( + of loaded: [Entity], + from earlier: [Entity], + _ name: KeyPath, + _ id: WritableKeyPath + ) -> [Entity] { + var ids: [String: UUID] = [:] + for entity in earlier where !entity[keyPath: name].isEmpty && ids[entity[keyPath: name]] == nil { + ids[entity[keyPath: name]] = entity[keyPath: id] + } + var used = Set() + return loaded.map { entity in + guard let previous = ids[entity[keyPath: name]], used.insert(previous).inserted else { return entity } + var kept = entity + kept[keyPath: id] = previous + return kept + } + } + private func resetWorkingState() { workingColumns = currentColumns workingIndexes = currentIndexes @@ -312,7 +369,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { let key = operations.identifier(id) if let currentIndex = self[keyPath: operations.current].firstIndex(where: { $0.id == id }) { let oldEntity = self[keyPath: operations.current][currentIndex] - if oldEntity != newEntity { + if operations.needsStatement(oldEntity, newEntity) { pendingChanges[key] = operations.modification(oldEntity, newEntity) trackChangeKey(key) } else { @@ -367,7 +424,12 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { deletionUndo: { SchemaUndoAction.columnDelete(column: $0, at: $1) }, addActionName: String(localized: "Add Column"), editActionName: String(localized: "Edit Column"), - deleteActionName: String(localized: "Delete Column") + deleteActionName: String(localized: "Delete Column"), + needsStatement: { loaded, working in + var working = working + working.isPrimaryKey = loaded.isPrimaryKey + return loaded != working + } ) private static let indexOperations = SchemaEntityOperations( @@ -418,45 +480,45 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { // MARK: - Row-Specific Undo Delete - /// Clear the deletion mark for the entity at `row` in `tab`. Mirrors - /// `DataChangeManager.undoRowDeletion(rowID:)`: the global NSUndoManager - /// stack is intentionally left alone. The original `applySchemaUndo(...)` - /// handler the deletion registered remains on the stack; if global Cmd+Z - /// later invokes it, the handler finds `pendingChanges` no longer marks - /// this row as deleted and treats the redo as a no-op for this entity. The - /// row-specific affordance and the global undo stack are independent - /// affordances. The data tab uses the same separation. - func undoDelete(for tab: StructureTab, at row: Int) { + // The row's own Undo Delete leaves the global stack alone, like the data tab's; an edit made before the + // delete is staged again, since the working copy still shows it. + func undoDelete(section: StructureSection, id: UUID) { guard !isHeldForSave else { return } - let key: SchemaChangeIdentifier - switch tab { - case .columns: - guard row < workingColumns.count else { return } - key = .column(workingColumns[row].id) - case .indexes: - guard row < workingIndexes.count else { return } - key = .index(workingIndexes[row].id) - case .foreignKeys: - guard row < workingForeignKeys.count else { return } - key = .foreignKey(workingForeignKeys[row].id) - case .checkConstraints: - guard row < workingCheckConstraints.count else { return } - key = .checkConstraint(workingCheckConstraints[row].id) - case .ddl, .parts, .triggers, .options: - return + switch section { + case .columns: undoDeletion(id: id, using: Self.columnOperations) + case .indexes: undoDeletion(id: id, using: Self.indexOperations) + case .foreignKeys: undoDeletion(id: id, using: Self.foreignKeyOperations) + case .checkConstraints: undoDeletion(id: id, using: Self.checkConstraintOperations) } + } + + private func undoDeletion(id: UUID, using operations: SchemaEntityOperations) { + let key = operations.identifier(id) guard pendingChanges[key]?.isDelete == true else { return } - pendingChanges.removeValue(forKey: key) - untrackChangeKey(key) + restageEdit(id: id, using: operations) workingCopyDidChange() } + private func restageEdit(id: UUID, using operations: SchemaEntityOperations) { + let key = operations.identifier(id) + guard let loaded = self[keyPath: operations.current].first(where: { $0.id == id }), + let working = self[keyPath: operations.working].first(where: { $0.id == id }), + operations.needsStatement(loaded, working) else { + pendingChanges.removeValue(forKey: key) + untrackChangeKey(key) + return + } + pendingChanges[key] = operations.modification(loaded, working) + trackChangeKey(key) + } + // MARK: - Validation /// Runs after every change to the working copy, undo and redo included: first what the change /// decides for rows it did not touch, then validation. private func workingCopyDidChange() { settleClusteredAdditions() + restagePrimaryKey() validate() } @@ -752,6 +814,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { func discardChanges() { guard !isHeldForSave else { return } + endTypingRun() tableComment = ObjectCommentDraft(original: tableComment.original) tableCommentRunStart = nil tableOptions = TableOptionsDraft( @@ -777,6 +840,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { /// Taken before the save's first suspension, which is what refuses a second press. func holdForSave() -> StructureSaveSnapshot? { endTableCommentRun() + endTypingRun() guard heldSave == nil, hasChanges else { return nil } let snapshot = StructureSaveSnapshot(changes: getChangesArray()) heldSave = snapshot @@ -818,6 +882,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { func undo() { guard !isHeldForSave else { return } endTableCommentRun() + endTypingRun() guard undoManager.canUndo else { return } undoManager.undo() } @@ -825,6 +890,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { func redo() { guard !isHeldForSave else { return } endTableCommentRun() + endTypingRun() guard undoManager.canRedo else { return } undoManager.redo() } @@ -944,8 +1010,8 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { applyAdditionUndo(constraint, using: Self.checkConstraintOperations) case .checkConstraintDelete(let constraint, let at): applyDeletionUndo(constraint, at: at, using: Self.checkConstraintOperations) - case .primaryKeyChange(let old, _): - applyPrimaryKeyChangeUndo(old: old) + case .primaryKeyChange: + restagePrimaryKey() case .tableCommentEdit(let old, let new): applyTableCommentUndo(old: old, new: new) case .tableOptionsEdit(let old, let new): @@ -973,7 +1039,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { return } let current = self[keyPath: operations.current][currentIndex] - if old != current { + if operations.needsStatement(current, old) { pendingChanges[key] = operations.modification(current, old) trackChangeKey(key) } else { @@ -1008,8 +1074,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { } let key = operations.identifier(entity.id) if self[keyPath: operations.current].contains(where: { $0.id == entity.id }) { - pendingChanges.removeValue(forKey: key) - untrackChangeKey(key) + restageEdit(id: entity.id, using: operations) } else { if let row, row < self[keyPath: operations.working].count { self[keyPath: operations.working].insert(entity, at: row) @@ -1021,19 +1086,31 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { } } - private func applyPrimaryKeyChangeUndo(old: [String]) { - let current = workingPrimaryKey - registerUndo(String(localized: "Change Primary Key")) { target in - target.applySchemaUndo(.primaryKeyChange(old: current, new: old)) + // MARK: - Primary key + + private func restagePrimaryKey() { + let loadedIDs = currentPrimaryKey.compactMap { name in currentColumns.first { $0.name == name }?.id } + let key = SchemaChangeIdentifier.primaryKey + // A key naming a column that was not read (a failed read, an expression key) is left as it was loaded. + guard loadedIDs.count == currentPrimaryKey.count else { + workingPrimaryKey = currentPrimaryKey + pendingChanges.removeValue(forKey: key) + untrackChangeKey(key) + return } - workingPrimaryKey = old - let pkKey = SchemaChangeIdentifier.primaryKey - if workingPrimaryKey != currentPrimaryKey { - pendingChanges[pkKey] = .modifyPrimaryKey(old: currentPrimaryKey, new: workingPrimaryKey) - trackChangeKey(pkKey) + let members = workingColumns.filter { $0.isPrimaryKey && !isPendingDeletion(.column($0.id)) } + let ordered = loadedIDs.compactMap { id in members.first { $0.id == id } } + + members.filter { !loadedIDs.contains($0.id) } + workingPrimaryKey = ordered.map(\.name) + // A key that only loses columns being dropped needs no statement: the database takes them out with the column, + // and on MySQL an AUTO_INCREMENT key cannot be dropped before its column. + let survivingLoadedIDs = loadedIDs.filter { !isPendingDeletion(.column($0)) } + if ordered.map(\.id) == loadedIDs || ordered.map(\.id) == survivingLoadedIDs { + pendingChanges.removeValue(forKey: key) + untrackChangeKey(key) } else { - pendingChanges.removeValue(forKey: pkKey) - untrackChangeKey(pkKey) + pendingChanges[key] = .modifyPrimaryKey(old: currentPrimaryKey, new: workingPrimaryKey) + trackChangeKey(key) } } @@ -1045,7 +1122,7 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { /// merges the result of this method with `modifiedColumns` from /// `StructureEditingSupport` field-diff helpers to build the final /// `RowVisualState`. - func deleteInsertState(for row: Int, tab: StructureTab) -> (isDeleted: Bool, isInserted: Bool) { + func deleteInsertState(for row: Int, tab: StructureSection) -> (isDeleted: Bool, isInserted: Bool) { switch tab { case .columns: return rowState(at: row, using: Self.columnOperations) @@ -1055,8 +1132,6 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { return rowState(at: row, using: Self.foreignKeyOperations) case .checkConstraints: return rowState(at: row, using: Self.checkConstraintOperations) - case .ddl, .parts, .triggers, .options: - return (false, false) } } diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+FileMenuActions.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+FileMenuActions.swift index ba189144e1..879cd2c8c3 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+FileMenuActions.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+FileMenuActions.swift @@ -14,6 +14,10 @@ extension MainSplitViewController { commandActions?.saveFileAs() } + @objc func discardChanges(_ sender: Any?) { + commandActions?.discardChanges() + } + /// New Tab and Close Tab used to run AppKit's own `newWindowForTab:` and `performClose:`, /// which named windows because a tab was a window. They act on the tab list now, so the /// menu and the strip's own controls cannot disagree. diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift index df49ca366a..ffed17eda9 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift @@ -43,6 +43,8 @@ struct MenuValidationContext: Equatable { // A batch whose COMMIT is on the wire is executing yet unstoppable, and Cmd+. must dim rather than fire into it. var isQueryStoppable = false var hasQueryText = false + // A Create Table tab runs its statement with Execute (Cmd+Return), as its old Create Table button did. + var canCreateDraftTable = false var canRunAIQueryActions = false var canClearQuery = false var canClearResults = false @@ -50,6 +52,8 @@ struct MenuValidationContext: Equatable { // Save commits an edit still open in the grid before it looks for changes. var hasOpenGridEdit = false var hasDataPendingChanges = false + // The selected tab's staged structure edits, or its grid edits; Create Table has nothing to return to. + var canDiscardChanges = false var hasRowSelection = false // Copy with headers and Copy as JSON read the data grid's columns, so the structure grid's selection does not count. var hasDataGridRowSelection = false @@ -175,6 +179,8 @@ extension MainSplitViewController: NSMenuItemValidation { case #selector(saveDocument(_:)): return context.isConnected && !context.isReadOnly && (context.hasPendingChanges || context.hasOpenGridEdit) + case #selector(discardChanges(_:)): + return context.isConnected && context.canDiscardChanges case #selector(saveDocumentAs(_:)): return context.isConnected && context.isQueryTab case #selector(exportQueryResults(_:)): @@ -434,6 +440,7 @@ extension MainSplitViewController: NSMenuItemValidation { private static let browseContentSelectors: Set = [ #selector(refreshDatabase(_:)), #selector(saveDocument(_:)), + #selector(discardChanges(_:)), #selector(addRow(_:)), #selector(restorePreviousValues(_:)), #selector(editDocument(_:)), @@ -477,8 +484,9 @@ extension MainSplitViewController: NSMenuItemValidation { private static func isQueryCommandEnabled(_ selector: Selector, context: MenuValidationContext) -> Bool? { switch selector { - case #selector(executeQuery(_:)), - #selector(executeAllStatements(_:)), + case #selector(executeQuery(_:)): + return context.isConnected && (context.hasQueryText || context.canCreateDraftTable) + case #selector(executeAllStatements(_:)), #selector(executeQueryWithoutLimit(_:)): return context.isConnected && context.hasQueryText case #selector(formatQuery(_:)): @@ -582,12 +590,14 @@ extension MainSplitViewController: NSMenuItemValidation { isQueryExecuting: actions.isQueryExecuting, isQueryStoppable: actions.isQueryStoppable, hasQueryText: actions.hasQueryText, + canCreateDraftTable: actions.canCreateDraftTable, canRunAIQueryActions: actions.aiQueryActionAvailability.isEnabled, canClearQuery: actions.canClearQuery, canClearResults: actions.canClearResults, hasPendingChanges: actions.hasPendingChanges, hasOpenGridEdit: actions.hasOpenGridEdit, hasDataPendingChanges: actions.hasDataPendingChanges, + canDiscardChanges: actions.canDiscardChanges, hasRowSelection: actions.hasRowSelection, hasDataGridRowSelection: actions.hasDataGridRowSelection, hasTableSelection: actions.hasTableSelection, diff --git a/apple/TablePro/Models/Connection/ConnectionToolbarState.swift b/apple/TablePro/Models/Connection/ConnectionToolbarState.swift index 38ee3fb4a8..ed8460e957 100644 --- a/apple/TablePro/Models/Connection/ConnectionToolbarState.swift +++ b/apple/TablePro/Models/Connection/ConnectionToolbarState.swift @@ -130,8 +130,6 @@ final class ConnectionToolbarState: ObservableObject { /// Whether there are pending data grid changes (for SQL preview button) @Published var hasDataPendingChanges: Bool = false - /// Whether the structure view has pending schema changes - @Published var hasStructureChanges: Bool = false // Bumped when a diagram's staged edits come or go; the pending state reads the diagram itself. @Published var diagramEditsRevision = 0 diff --git a/apple/TablePro/Models/Query/ResultStatusModel.swift b/apple/TablePro/Models/Query/ResultStatusModel.swift index 6e9925fe0f..665075df18 100644 --- a/apple/TablePro/Models/Query/ResultStatusModel.swift +++ b/apple/TablePro/Models/Query/ResultStatusModel.swift @@ -48,9 +48,6 @@ struct ResultStatusControls: Equatable { /// First, Previous, Next, Last and the page number, which an engine that cannot skip rows has /// no use for. The rows-per-page menu stays, because it still sets how many leading rows load. var showsPageNavigation = false - /// The structure editor's add and remove pair, which is this bar's trailing cluster while the - /// structure editor is the content. - var showsStructureActions = false /// Only the data grid has cells to sum. JSON and Map report the row selection, not cells. var showsSelectionSummary = false } @@ -95,7 +92,6 @@ struct ResultStatusModel: Equatable { let isTable = snapshot.tabType == .table controls.showsModeSwitcher = snapshot.availableModes.count > 1 - controls.showsStructureActions = viewMode == .structure && snapshot.hasStructureActions controls.showsExecution = viewMode.reportsExecution /// A plan keeps the bar so it stays choosable and pinnable, and gives up everything the bar diff --git a/apple/TablePro/Models/Query/StatusBarSnapshot.swift b/apple/TablePro/Models/Query/StatusBarSnapshot.swift index 3202cbd7e7..d241272bc2 100644 --- a/apple/TablePro/Models/Query/StatusBarSnapshot.swift +++ b/apple/TablePro/Models/Query/StatusBarSnapshot.swift @@ -21,8 +21,6 @@ struct StatusBarSnapshot: Equatable { let hasTableName: Bool /// The modes this tab can switch between, which the bar's leading control offers. let availableModes: [ResultsViewMode] - /// Whether the structure editor has published an add/remove pair for this tab. - let hasStructureActions: Bool let pagination: PaginationState let statusMessage: String? let paginationCapability: PaginationCapability @@ -40,7 +38,6 @@ struct StatusBarSnapshot: Equatable { isValueFiltered: Bool = false, hasTableName: Bool, availableModes: [ResultsViewMode] = [], - hasStructureActions: Bool = false, isQueryPlan: Bool = false, pagination: PaginationState, statusMessage: String?, @@ -55,7 +52,6 @@ struct StatusBarSnapshot: Equatable { self.isValueFiltered = isValueFiltered self.hasTableName = hasTableName self.availableModes = availableModes - self.hasStructureActions = hasStructureActions self.isQueryPlan = isQueryPlan self.pagination = pagination self.statusMessage = statusMessage @@ -72,7 +68,6 @@ struct StatusBarSnapshot: Equatable { tableRows: TableRows?, displayRowCount: Int? = nil, isFetching: Bool = false, - hasStructureActions: Bool = false, isQueryPlan: Bool = false, paginationCapability: PaginationCapability = .offset, hasServerOutput: Bool = false, @@ -98,7 +93,6 @@ struct StatusBarSnapshot: Equatable { hasSpatialColumn: !(tab?.display.spatialColumns.isEmpty ?? true), hasServerOutput: hasServerOutput ), - hasStructureActions: hasStructureActions, isQueryPlan: isQueryPlan, pagination: pagination, // A run of several statements leaves each one's message on its own result, not on the tab. diff --git a/apple/TablePro/Models/Schema/CreateTableDraftBuilder.swift b/apple/TablePro/Models/Schema/CreateTableDraftBuilder.swift index 6f62620de5..783d9ab282 100644 --- a/apple/TablePro/Models/Schema/CreateTableDraftBuilder.swift +++ b/apple/TablePro/Models/Schema/CreateTableDraftBuilder.swift @@ -6,56 +6,43 @@ import Foundation import TableProPluginKit -/// What a Create Table draft comes to: the statement plan it will run, and every row that is not -/// ready to run. +// The statements a Create Table draft will run, and every row that is not ready to. struct CreateTablePlan { - /// Never carries indexes. An index is always its own statement, see `CreateTableDraftBuilder`. + // Never carries indexes or check constraints: each is its own statement after the CREATE. let definition: PluginCreateTableDefinition? let indexes: [PluginIndexDefinition] let issues: [SchemaDraftIssue] - /// Nil writes no comment. Set after the `CREATE TABLE`, as its own statement. + // Nil writes no comment. Set after the CREATE, as its own statement. var tableComment: String? + var checkConstraints: [PluginCheckConstraintDefinition] = [] + // Working-list rows of `indexes` and `checkConstraints`, since blank and incomplete rows are skipped. + var indexRows: [Int] = [] + var checkConstraintRows: [Int] = [] var isReadyToCreate: Bool { definition != nil && issues.isEmpty } + + func indexRow(at position: Int) -> Int { + indexRows.indices.contains(position) ? indexRows[position] : position + } + + func checkConstraintRow(at position: Int) -> Int { + checkConstraintRows.indices.contains(position) ? checkConstraintRows[position] : position + } } -/// Turns the Create Table editor's working rows into a statement plan. -/// -/// This exists because the view used to do it inline, in three filters that each demanded a -/// non-empty `name` and then dropped whatever failed: -/// -/// foreignKeys: workingForeignKeys.filter { !$0.name.isEmpty && !$0.columns.isEmpty && … } -/// -/// A new foreign key row's name is always empty and nothing in the grid fills it, so every foreign -/// key the user entered was deleted on the way to the driver, with no message and an SQL Preview -/// that agreed. No dialect requires the name, three drivers discard it, and the filter left out the -/// predicate that does matter: an empty referenced column list renders `REFERENCES "t" ()`, which -/// SQLite, PostgreSQL and DuckDB all reject. -/// -/// So the split is: this type answers whether a row is **complete**, the driver answers how to -/// **spell** it, and `ForeignKeyDialect` answers what the engine's grammar takes. Three row states, -/// and the middle one is the whole point: -/// -/// - **blank**: nothing typed. Ignored, because the editor seeds a blank column and `+` seeds blank -/// rows, and neither is something the user asked to create. -/// - **incomplete**: begun and unusable. An issue the user is shown. Never a silent drop. -/// - **complete**: emitted. +// Turns the working rows into a plan. This type says whether a row is complete, the driver how to spell it, and +// `ForeignKeyDialect` what the engine's grammar takes. A blank row is ignored (the editor seeds them), a begun one +// that cannot run is an issue, never a silent drop: requiring a foreign key's name once dropped every key (#2691). @MainActor enum CreateTableDraftBuilder { - /// Indexes are never folded into the `CREATE TABLE` statement, on any engine. - /// - /// PostgreSQL and DuckDB used to append their `CREATE INDEX` statements to the same returned - /// string, and that only ran because `PQexec` and `duckdb_query` take a whole batch. SQLite's - /// driver prepares with `sqlite3_prepare_v2(db, sql, -1, &stmt, nil)` and steps once, so a - /// second statement in the string is compiled by nobody: the index would vanish exactly the way - /// the foreign keys did. Every index therefore goes through `generateAddIndexSQL` as its own - /// statement, which is also what the existing-table path in `SchemaStatementGenerator` does. + // Indexes and checks are never folded into the CREATE: SQLite steps one statement per execute. static func plan( tableName: String, options: CreateTableOptions, columns: [EditableColumnDefinition], indexes: [EditableIndexDefinition], foreignKeys: [EditableForeignKeyDefinition], + checkConstraints: [EditableCheckConstraintDefinition] = [], dialect: ForeignKeyDialect, includesEngineOptions: Bool, defaults: CreateTableOptionDefaults = CreateTableOptionDefaults(), @@ -69,16 +56,17 @@ enum CreateTableDraftBuilder { let resolvedForeignKeys = resolveForeignKeys( foreignKeys, columnNames: columnNames, dialect: dialect, into: &issues ) + let resolvedChecks = resolveCheckConstraints(checkConstraints, into: &issues) let trimmedName = tableName.trimmingCharacters(in: .whitespaces) if trimmedName.isEmpty { issues.append(SchemaDraftIssue( - tab: .columns, row: nil, message: String(localized: "The table needs a name.") + section: .columns, row: nil, message: String(localized: "The table needs a name.") )) } if resolvedColumns.isEmpty, !suppliesItsOwnKey { issues.append(SchemaDraftIssue( - tab: .columns, row: nil, + section: .columns, row: nil, message: String(localized: "The table needs at least one column with a name and a type.") )) } @@ -103,18 +91,18 @@ enum CreateTableDraftBuilder { return CreateTablePlan( definition: definition, - indexes: resolvedIndexes.map { $0.toPlugin() }, + indexes: resolvedIndexes.map { $0.definition.toPlugin() }, issues: issues, - tableComment: ObjectCommentDraft.normalized(options.comment) + tableComment: ObjectCommentDraft.normalized(options.comment), + checkConstraints: resolvedChecks.map { $0.definition.toPlugin() }, + indexRows: resolvedIndexes.map(\.row), + checkConstraintRows: resolvedChecks.map(\.row) ) } // MARK: - Columns - /// A column the user marked auto-increment without ticking Primary Key is promoted here rather - /// than in each driver. The old code passed the promotion through `primaryKeyColumns` only, and - /// SQLite reads `isPrimaryKey` alone, so on SQLite that column got neither a primary key nor - /// `AUTOINCREMENT`. Reconciling the two once leaves every driver reading one answer. + // An auto-increment column with no primary key ticked is promoted here, once: SQLite reads `isPrimaryKey` alone. private static func resolveColumns( _ columns: [EditableColumnDefinition], into issues: inout [SchemaDraftIssue] @@ -130,19 +118,19 @@ enum CreateTableDraftBuilder { } if name.isEmpty { issues.append(SchemaDraftIssue( - tab: .columns, row: row, message: String(localized: "This column needs a name.") + section: .columns, row: row, message: String(localized: "This column needs a name.") )) continue } if type.isEmpty { issues.append(SchemaDraftIssue( - tab: .columns, row: row, message: String(localized: "This column needs a type.") + section: .columns, row: row, message: String(localized: "This column needs a type.") )) continue } if !column.isNullable, column.hasNullDefault { issues.append(SchemaDraftIssue( - tab: .columns, row: row, + section: .columns, row: row, message: String(localized: "This column does not allow NULL, so its default cannot be NULL.") )) } @@ -163,7 +151,7 @@ enum CreateTableDraftBuilder { for position in duplicatePositions(of: resolved.map(\.name)) { issues.append(SchemaDraftIssue( - tab: .columns, row: sourceRows[position], + section: .columns, row: sourceRows[position], message: String( format: String(localized: "Another column is already named %@."), resolved[position].name ) @@ -175,15 +163,13 @@ enum CreateTableDraftBuilder { // MARK: - Indexes - /// An index does need a name, unlike a foreign key: `CREATE INDEX` has nowhere to put an - /// anonymous one. So the missing name is reported rather than required in silence. + // Unlike a foreign key, an index needs a name: CREATE INDEX has nowhere to put an anonymous one. private static func resolveIndexes( _ indexes: [EditableIndexDefinition], columnNames: Set, into issues: inout [SchemaDraftIssue] - ) -> [EditableIndexDefinition] { - var resolved: [EditableIndexDefinition] = [] - var sourceRows: [Int] = [] + ) -> [Resolved] { + var resolved: [Resolved] = [] for (row, index) in indexes.enumerated() { let name = index.name.trimmingCharacters(in: .whitespaces) @@ -193,14 +179,14 @@ enum CreateTableDraftBuilder { } if columns.isEmpty { issues.append(SchemaDraftIssue( - tab: .indexes, row: row, + section: .indexes, row: row, message: String(localized: "This index needs at least one column.") )) continue } if name.isEmpty { issues.append(SchemaDraftIssue( - tab: .indexes, row: row, message: String(localized: "This index needs a name.") + section: .indexes, row: row, message: String(localized: "This index needs a name.") )) continue } @@ -209,22 +195,22 @@ enum CreateTableDraftBuilder { normalized.columns = columns if let unknown = normalized.referencedColumnNames.first(where: { !columnNames.contains($0) }) { issues.append(SchemaDraftIssue( - tab: .indexes, row: row, + section: .indexes, row: row, message: String( format: String(localized: "The table has no column named %@."), unknown ) )) continue } - resolved.append(normalized) - sourceRows.append(row) + resolved.append(Resolved(definition: normalized, row: row)) } - for position in duplicatePositions(of: resolved.map(\.name)) { + for position in duplicatePositions(of: resolved.map(\.definition.name)) { issues.append(SchemaDraftIssue( - tab: .indexes, row: sourceRows[position], + section: .indexes, row: resolved[position].row, message: String( - format: String(localized: "Another index is already named %@."), resolved[position].name + format: String(localized: "Another index is already named %@."), + resolved[position].definition.name ) )) } @@ -254,7 +240,7 @@ enum CreateTableDraftBuilder { let names = resolved.map(\.name) for position in duplicatePositions(of: names) where !names[position].isEmpty { issues.append(SchemaDraftIssue( - tab: .foreignKeys, row: sourceRows[position], + section: .foreignKeys, row: sourceRows[position], message: String( format: String(localized: "Another foreign key is already named %@."), resolved[position].name @@ -265,9 +251,7 @@ enum CreateTableDraftBuilder { return resolved } - /// The constraint name is deliberately absent from every test here. It is optional in every - /// dialect TablePro speaks, three drivers discard it, and demanding it is what deleted the - /// user's foreign keys. + // No test here demands the name: it is optional in every dialect, and demanding it deleted the user's keys. private static func resolveForeignKey( _ foreignKey: EditableForeignKeyDefinition, row: Int, @@ -294,7 +278,7 @@ enum CreateTableDraftBuilder { } func report(_ message: String) { - issues.append(SchemaDraftIssue(tab: .foreignKeys, row: row, message: message)) + issues.append(SchemaDraftIssue(section: .foreignKeys, row: row, message: message)) } if normalized.columns.isEmpty { @@ -344,8 +328,61 @@ enum CreateTableDraftBuilder { return normalized } + // MARK: - Check constraints + + // Every driver writes a check as ADD CONSTRAINT CHECK (), so both are required. + private static func resolveCheckConstraints( + _ constraints: [EditableCheckConstraintDefinition], + into issues: inout [SchemaDraftIssue] + ) -> [Resolved] { + var resolved: [Resolved] = [] + + for (row, constraint) in constraints.enumerated() { + let name = constraint.name.trimmingCharacters(in: .whitespaces) + let expression = constraint.expression.trimmingCharacters(in: .whitespacesAndNewlines) + if name.isEmpty, expression.isEmpty { + continue + } + if expression.isEmpty { + issues.append(SchemaDraftIssue( + section: .checkConstraints, row: row, + message: String(localized: "This check constraint needs an expression.") + )) + continue + } + if name.isEmpty { + issues.append(SchemaDraftIssue( + section: .checkConstraints, row: row, + message: String(localized: "This check constraint needs a name.") + )) + continue + } + var normalized = constraint + normalized.name = name + normalized.expression = expression + resolved.append(Resolved(definition: normalized, row: row)) + } + + for position in duplicatePositions(of: resolved.map(\.definition.name)) { + issues.append(SchemaDraftIssue( + section: .checkConstraints, row: resolved[position].row, + message: String( + format: String(localized: "Another check constraint is already named %@."), + resolved[position].definition.name + ) + )) + } + + return resolved + } + // MARK: - Helpers + private struct Resolved { + let definition: Definition + let row: Int + } + private static func trimmedOrNil(_ value: String?) -> String? { guard let trimmed = value?.trimmingCharacters(in: .whitespaces), !trimmed.isEmpty else { return nil } return trimmed @@ -355,12 +392,8 @@ enum CreateTableDraftBuilder { values.map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty } } - /// Every position after the first that repeats a name already seen, so the row the user is most - /// likely still editing is the one flagged. - /// - /// These are positions in the resolved list, not grid rows. A row skipped for being blank or - /// incomplete shortens the list, so the caller maps them back through its own `sourceRows` or - /// the message points at a different row than the duplicate. + // Positions in the resolved list after the first that repeat a name, so the row likely still being edited is + // flagged. The caller maps them back to working rows: skipped rows shorten the list. private static func duplicatePositions(of names: [String]) -> [Int] { var seen: Set = [] var duplicates: [Int] = [] diff --git a/apple/TablePro/Models/Schema/SchemaChange.swift b/apple/TablePro/Models/Schema/SchemaChange.swift index ef68b9bbd2..e99da8c518 100644 --- a/apple/TablePro/Models/Schema/SchemaChange.swift +++ b/apple/TablePro/Models/Schema/SchemaChange.swift @@ -88,6 +88,57 @@ enum SchemaChange: Hashable, Equatable { } } + // What an error or a review shows a person; `description` stays English for logs. + var localizedDescription: String { + switch self { + case .addColumn(let col): + return String(format: String(localized: "Add column “%@”"), col.name) + case .modifyColumn(let old, let new): + return old.name == new.name + ? String(format: String(localized: "Change column “%@”"), new.name) + : String(format: String(localized: "Change column “%@” to “%@”"), old.name, new.name) + case .deleteColumn(let col): + return String(format: String(localized: "Delete column “%@”"), col.name) + case .addIndex(let idx): + return String(format: String(localized: "Add index “%@”"), idx.name) + case .modifyIndex(let old, let new): + return old.name == new.name + ? String(format: String(localized: "Change index “%@”"), new.name) + : String(format: String(localized: "Change index “%@” to “%@”"), old.name, new.name) + case .deleteIndex(let idx): + return String(format: String(localized: "Delete index “%@”"), idx.name) + case .addForeignKey(let fk): + return String(format: String(localized: "Add foreign key “%@”"), fk.name) + case .modifyForeignKey(let old, let new): + return old.name == new.name + ? String(format: String(localized: "Change foreign key “%@”"), new.name) + : String(format: String(localized: "Change foreign key “%@” to “%@”"), old.name, new.name) + case .deleteForeignKey(let fk): + return String(format: String(localized: "Delete foreign key “%@”"), fk.name) + case .addCheckConstraint(let constraint): + return String(format: String(localized: "Add check constraint “%@”"), constraint.name) + case .modifyCheckConstraint(let old, let new): + return old.name == new.name + ? String(format: String(localized: "Change check constraint “%@”"), new.name) + : String(format: String(localized: "Change check constraint “%@” to “%@”"), old.name, new.name) + case .deleteCheckConstraint(let constraint): + return String(format: String(localized: "Delete check constraint “%@”"), constraint.name) + case .modifyPrimaryKey(_, let new): + return new.isEmpty + ? String(localized: "Remove the primary key") + : String(format: String(localized: "Set the primary key to %@"), new.joined(separator: ", ")) + case .modifyTableComment: + return String(localized: "Change the table comment") + case .modifyTableCharacterSet(_, let new): + let target = [new.characterSet, new.collation].compactMap { $0 }.joined(separator: " ") + return new.convertsColumns + ? String(format: String(localized: "Convert the table to %@"), target) + : String(format: String(localized: "Set the table's default character set to %@"), target) + case .modifyAutoIncrement(_, let new): + return String(format: String(localized: "Set the next AUTO_INCREMENT value to %@"), String(new)) + } + } + var description: String { switch self { case .addColumn(let col): diff --git a/apple/TablePro/Models/Schema/SchemaDraftIssue.swift b/apple/TablePro/Models/Schema/SchemaDraftIssue.swift index 66dc0be6ff..ec986d6188 100644 --- a/apple/TablePro/Models/Schema/SchemaDraftIssue.swift +++ b/apple/TablePro/Models/Schema/SchemaDraftIssue.swift @@ -5,30 +5,23 @@ import Foundation -/// A row the user began and did not finish, named where the user can act on it. -/// -/// The distinction this type exists to draw is between a row that is blank, which is the editor's -/// own placeholder and carries no intent, and a row that holds some of what a constraint needs and -/// not the rest. The first is ignored. The second used to be dropped from the generated statement -/// with no message at all, which is how a filled-in foreign key came to vanish between the grid and -/// the SQL Preview (#2691). +// A row the user began and did not finish. A blank row carries no intent and is ignored; a begun one is named +// here, never dropped from the statement in silence (#2691). struct SchemaDraftIssue: Equatable, Identifiable, Sendable { - let tab: StructureTab - /// Zero-based position in the tab's grid, or nil for an issue about the draft as a whole. + let section: StructureSection + // Zero-based position in the section's working list, or nil for an issue about the draft as a whole. let row: Int? let message: String - var id: String { "\(tab.rawValue)-\(row.map(String.init) ?? "-")-\(message)" } + var id: String { "\(section.rawValue)-\(row.map(String.init) ?? "-")-\(message)" } - /// The message with its row and tab in front, for a reader who is looking at another tab. - /// - /// Rows are numbered from one here because that is how the grid's row gutter numbers them. + // Numbered from one, the way the grid's row gutter counts. var qualifiedMessage: String { guard let row else { - return String(format: String(localized: "%1$@: %2$@"), tab.displayName, message) + return String(format: String(localized: "%1$@: %2$@"), section.displayName, message) } return String( - format: String(localized: "%1$@ row %2$lld: %3$@"), tab.displayName, Int64(row + 1), message + format: String(localized: "%1$@ row %2$lld: %3$@"), section.displayName, Int64(row + 1), message ) } } diff --git a/apple/TablePro/Models/Schema/SchemaEntityOperations.swift b/apple/TablePro/Models/Schema/SchemaEntityOperations.swift index ef8919da49..36415b38b5 100644 --- a/apple/TablePro/Models/Schema/SchemaEntityOperations.swift +++ b/apple/TablePro/Models/Schema/SchemaEntityOperations.swift @@ -2,17 +2,10 @@ // SchemaEntityOperations.swift // TablePro // -// Describes one structure-editor entity kind to the generic staging path. -// import Foundation -/// What distinguishes one structure entity from another when it is staged. -/// -/// Columns, indexes, foreign keys and check constraints are staged by an identical sequence: -/// register an undo, record a pending change under the entity's identifier, then write the -/// working array. Only the arrays, the enum cases and the undo action names differ, so those -/// are supplied here and the sequence itself lives once in `StructureChangeManager`. +// One structure entity kind, for the staging sequence `StructureChangeManager` runs once for all four. struct SchemaEntityOperations where Entity.ID == UUID { let working: ReferenceWritableKeyPath let current: KeyPath @@ -26,4 +19,6 @@ struct SchemaEntityOperations where Entity.ID let addActionName: String let editActionName: String let deleteActionName: String + // Whether the working copy needs a statement; a column's key flag is the table's key change, not the column's. + var needsStatement: (_ loaded: Entity, _ working: Entity) -> Bool = { $0 != $1 } } diff --git a/apple/TablePro/Models/Schema/StructureTab.swift b/apple/TablePro/Models/Schema/StructureSection.swift similarity index 52% rename from apple/TablePro/Models/Schema/StructureTab.swift rename to apple/TablePro/Models/Schema/StructureSection.swift index e652c0c8ed..1591be0043 100644 --- a/apple/TablePro/Models/Schema/StructureTab.swift +++ b/apple/TablePro/Models/Schema/StructureSection.swift @@ -1,28 +1,37 @@ // -// StructureTab.swift +// StructureSection.swift // TablePro // import Foundation -enum StructureTab: String, CaseIterable, Hashable { +// The kinds of definition a table's structure stages edits for. Read-only panes are StructurePane. +enum StructureSection: String, CaseIterable, Hashable { case columns case indexes case foreignKeys case checkConstraints - case triggers - case options - case ddl - case parts var displayName: String { switch self { case .columns: String(localized: "Columns") case .indexes: String(localized: "Indexes") case .foreignKeys: String(localized: "Foreign Keys") - case .checkConstraints: String(localized: "Constraints") + case .checkConstraints: String(localized: "Check Constraints") + } + } +} + +enum StructurePane: String, CaseIterable, Hashable { + case definition + case triggers + case ddl + case parts + + var displayName: String { + switch self { + case .definition: String(localized: "Definition") case .triggers: String(localized: "Triggers") - case .options: String(localized: "Options") case .ddl: "DDL" case .parts: "Parts" } diff --git a/apple/TablePro/Models/UI/DataGridConfiguration.swift b/apple/TablePro/Models/UI/DataGridConfiguration.swift index e6313ae64d..3f05e7c2b8 100644 --- a/apple/TablePro/Models/UI/DataGridConfiguration.swift +++ b/apple/TablePro/Models/UI/DataGridConfiguration.swift @@ -45,4 +45,10 @@ struct DataGridConfiguration: Equatable { // VoiceOver's name for the grid ("Active Sessions"); nil reads "Data grid". var accessibilityLabel: String? + + // As tall as its rows, inside a page that scrolls vertically for it; the grid scrolls only sideways. + var fitsRows = false + + // Off where an empty cell means "none set", as a structure list's Default or Comment does, not an empty string. + var marksEmptyText = true } diff --git a/apple/TablePro/Models/UI/FieldEditorKind.swift b/apple/TablePro/Models/UI/FieldEditorKind.swift index 5de538e35e..4386adc89b 100644 --- a/apple/TablePro/Models/UI/FieldEditorKind.swift +++ b/apple/TablePro/Models/UI/FieldEditorKind.swift @@ -21,11 +21,6 @@ internal enum FieldEditorKind: Equatable { /// a one-line value or a JSON document, which are two different editors over the same list. case arrayElements(element: ArrayElementEditor, values: [String]) case vector - case typePicker - /// A value with suggestions the user may ignore: the menu writes correct SQL, typing writes - /// your own. `enumPicker` cannot serve, because its list is the whole set of legal values. - case valuePicker(options: [GridMenuOption]) - case schemaText case multiLine case singleLine } diff --git a/apple/TablePro/Models/UI/InspectorFieldLayout.swift b/apple/TablePro/Models/UI/InspectorFieldLayout.swift deleted file mode 100644 index 5119772e49..0000000000 --- a/apple/TablePro/Models/UI/InspectorFieldLayout.swift +++ /dev/null @@ -1,63 +0,0 @@ -// -// InspectorFieldLayout.swift -// TablePro -// - -import Foundation - -/// How a field arranges its label against its editor. -/// -/// The arrangement follows whose labels these are, not how big the editor is. A data row's label is -/// a column name, which is user data of no bounded length: measured against a realistic 400-column -/// set, the widest renders at 259.8pt, wider than the whole pane at its 270pt minimum. No label lane -/// can hold that, and a lane wide enough to try takes the width the value needs, so at the minimum a -/// side-by-side data row truncates the name *and* the value. A schema row's label comes from a -/// closed, authored vocabulary (`StructureColumnField.displayName` plus the index, foreign-key and -/// check-constraint headers), whose widest member is "Ref Columns" at 66.4pt, so the lane Apple's own -/// inspectors use is both correct and achievable there. -/// -/// Nothing here can be derived at runtime from the rows themselves. Measured: a `PreferenceKey` -/// max-reduce inside a `List` sees only the realized rows, 39pt against a true widest of 296pt, so a -/// content-derived lane would change width as the user scrolls. A custom `AlignmentID` does not -/// resolve across a `List`'s rows at all (145pt of spread; the same guide in one `VStack` gives 0). -/// `Form(.columns)` and `Grid` do align, and both forfeit the laziness `InspectorFieldListView` -/// depends on. -/// -/// Both switches are exhaustive on purpose rather than `default`-armed. A new editor kind has to be -/// classified for each provenance instead of inheriting whichever arm was written first; two -/// `default`-armed switches over `FieldEditorKind` are what let `.typePicker` escape the value-font -/// domain and render a structure row's Name and Type in two different fonts. -internal enum InspectorFieldLayout: Equatable { - /// Label in a fixed trailing-aligned lane, editor beside it, one line high. - case inline - /// Label and type above, editor spanning the full width below. - case stacked - - internal static func resolve(for kind: FieldEditorKind, isSchemaField: Bool) -> InspectorFieldLayout { - isSchemaField ? schemaLayout(for: kind) : dataLayout(for: kind) - } - - /// A data field always stacks, whatever it holds. Choosing per row from the value's own length - /// would give the pane two label lanes and two value lanes alternating down the list, drop the - /// type badge from exactly the rows too narrow to carry it, and reflow a row from one shape to - /// the other while the user is typing into it. - private static func dataLayout(for kind: FieldEditorKind) -> InspectorFieldLayout { - switch kind { - case .singleLine, .boolean, .enumPicker, .setPicker, .arrayElements, .vector, .schemaText, .typePicker, - .valuePicker, .multiLine, .json, .phpSerialized, .binaryValue, .blobHex, .image, .geometry: - return .stacked - } - } - - /// A schema field's label is short and authored, so the lane holds it and the row stays one line - /// high. The editors that need the pane's width still take it. - private static func schemaLayout(for kind: FieldEditorKind) -> InspectorFieldLayout { - switch kind { - case .singleLine, .boolean, .enumPicker, .setPicker, .arrayElements, .vector, .schemaText, .typePicker, - .valuePicker: - return .inline - case .multiLine, .json, .phpSerialized, .binaryValue, .blobHex, .image, .geometry: - return .stacked - } - } -} diff --git a/apple/TablePro/Models/UI/InspectorRowSource.swift b/apple/TablePro/Models/UI/InspectorRowSource.swift deleted file mode 100644 index e819603517..0000000000 --- a/apple/TablePro/Models/UI/InspectorRowSource.swift +++ /dev/null @@ -1,37 +0,0 @@ -// -// InspectorRowSource.swift -// TablePro -// -// Lets the grid that owns the current selection supply the inspector's -// fields and receive its edits, instead of the inspector assuming every -// selection indexes the data tab's rows. -// - -import Foundation - -internal struct InspectorRowField: Equatable { - let name: String - let value: String? - let editor: FieldEditorKind - let isModified: Bool - let isEditable: Bool - - init(name: String, value: String?, editor: FieldEditorKind, isModified: Bool = false, isEditable: Bool = true) { - self.name = name - self.value = value - self.editor = editor - self.isModified = isModified - self.isEditable = isEditable - } -} - -internal struct InspectorRow: Equatable { - let fields: [InspectorRowField] - let isEditable: Bool -} - -@MainActor -internal protocol InspectorRowSource: AnyObject { - func inspectorRow(atDisplayRow displayRow: Int) -> InspectorRow? - func commitInspectorField(displayRow: Int, fieldIndex: Int, value: String?) -} diff --git a/apple/TablePro/Models/UI/InspectorSubject.swift b/apple/TablePro/Models/UI/InspectorSubject.swift index cb1df8b970..71403965d7 100644 --- a/apple/TablePro/Models/UI/InspectorSubject.swift +++ b/apple/TablePro/Models/UI/InspectorSubject.swift @@ -5,22 +5,13 @@ import Foundation -/// What the inspector is currently inspecting, and the two lines it draws for it above its content. -/// -/// The pane had no subject at all before: it multiplexed three unrelated tabs, so there was nothing -/// one title could name and the header carried a picker instead. Naming the subject is what lets -/// the inspector say which row of which table is on screen, which is the first thing a reader of an -/// inspector needs and the thing the old pane never showed. -/// -/// A schema grid is a first-class case rather than an afterthought. The structure and create-table -/// editors feed the same pane through `InspectorRowSource`, and their selection is a column -/// definition, not a row of data: it has no position in a result and no row identity, so a subject -/// that assumed one would render "Row 0 of 0" over a perfectly good column. +// What the inspector is inspecting, as the two lines it draws above its content. A structure definition has no +// position in a result, so it is named by what it is and whose it is, never "Row 0 of 0". internal enum InspectorSubject: Equatable { case empty case tableRow(table: String, position: RowPosition?) case multipleRows(table: String, count: Int) - case columnDefinition(column: String, table: String?) + case definition(name: String, section: StructureSection, table: String?) case tableOnly(table: String) internal struct RowPosition: Equatable { @@ -33,20 +24,18 @@ internal enum InspectorSubject: Equatable { } } - /// The bold first line: the thing being inspected. internal var title: String? { switch self { case .empty: return nil case .tableRow(let table, _), .multipleRows(let table, _), .tableOnly(let table): return table - case .columnDefinition(let column, _): - return column + case .definition(let name, let section, _): + return name.isEmpty ? Self.newDefinitionTitle(section) : name } } - /// The secondary second line: where the thing sits. Nil where there is nothing useful to add, - /// so the header collapses to one line rather than drawing an empty one. + // Nil where there is nothing to add, so the header collapses to one line. internal var subtitle: String? { switch self { case .empty: @@ -60,11 +49,38 @@ internal enum InspectorSubject: Equatable { ) case .multipleRows(_, let count): return String(format: String(localized: "%d rows selected"), count) - case .columnDefinition(_, let table): - guard let table else { return String(localized: "Column") } - return String(format: String(localized: "Column of %@"), table) + case .definition(_, let section, let table): + guard let table else { return Self.kindName(section) } + return Self.kindName(section, of: table) case .tableOnly: return String(localized: "Table") } } + + private static func newDefinitionTitle(_ section: StructureSection) -> String { + switch section { + case .columns: String(localized: "New Column") + case .indexes: String(localized: "New Index") + case .foreignKeys: String(localized: "New Foreign Key") + case .checkConstraints: String(localized: "New Check Constraint") + } + } + + private static func kindName(_ section: StructureSection) -> String { + switch section { + case .columns: String(localized: "Column") + case .indexes: String(localized: "Index") + case .foreignKeys: String(localized: "Foreign Key") + case .checkConstraints: String(localized: "Check Constraint") + } + } + + private static func kindName(_ section: StructureSection, of table: String) -> String { + switch section { + case .columns: String(format: String(localized: "Column of %@"), table) + case .indexes: String(format: String(localized: "Index of %@"), table) + case .foreignKeys: String(format: String(localized: "Foreign Key of %@"), table) + case .checkConstraints: String(format: String(localized: "Check Constraint of %@"), table) + } + } } diff --git a/apple/TablePro/Models/UI/MultiRowEditState.swift b/apple/TablePro/Models/UI/MultiRowEditState.swift index 294d697f48..b4c2c95517 100644 --- a/apple/TablePro/Models/UI/MultiRowEditState.swift +++ b/apple/TablePro/Models/UI/MultiRowEditState.swift @@ -21,15 +21,9 @@ struct FieldEditState: Identifiable { var isPrimaryKey: Bool = false var isForeignKey: Bool = false - // Set when the owning grid dictates the editor instead of the column type. - var editor: FieldEditorKind? - // Resolved once: it costs a JSONSerialization and a PHP-serialized parse, and view bodies re-parsed it on every hover. var resolvedEditor: FieldEditorKind? - // A schema field has no data type, so it offers no type badge and no NULL or DEFAULT state. - var isSchemaField: Bool = false - // Shown without an editor: refusing the edit further down left a pending value here that Save never writes. var isServerOwned: Bool = false @@ -46,7 +40,7 @@ struct FieldEditState: Identifiable { var declaredType: String? - // Recorded by the grid owning a schema row, or the change manager for rows that disagree, which no pending value can hold. + // Recorded by the change manager for rows that disagree, which no pending value can hold. var hasCommittedEdit: Bool = false var originalValue: String? @@ -124,8 +118,6 @@ final class MultiRowEditState: ObservableObject { // The rows an edit is staged against, captured when the selection was configured, never a display position. @Published private(set) var rowIDs: [RowID] = [] - // The schema row the fields came from, for a grid whose rows have no identity. - private var schemaDisplayRow: Int? @Published private(set) var allRows: [[String?]] = [] // Per row of allRows: the columns it has no field for. @@ -177,9 +169,8 @@ final class MultiRowEditState: ObservableObject { cellWriter: CellWriterSupport = .none ) { let columnsChanged = self.columns != columns - let selectionChanged = self.rowIDs != rowIDs || schemaDisplayRow != nil + let selectionChanged = self.rowIDs != rowIDs - schemaDisplayRow = nil self.rowIDs = rowIDs self.allRows = allRows self.absentCells = absentCells @@ -332,7 +323,7 @@ final class MultiRowEditState: ObservableObject { let reveals = AppSettingsManager.shared.dataGrid.showInvisibleCharacters guard reveals != revealsInvisibleCharacters else { return } revealsInvisibleCharacters = reveals - for index in fields.indices where !fields[index].isSchemaField && fields[index].editor == nil { + for index in fields.indices { let field = fields[index] let resolved = FieldEditorResolver.resolve( for: field.columnTypeEnum, @@ -348,54 +339,6 @@ final class MultiRowEditState: ObservableObject { } } - // Field ids survive a reconfigure of the same row so a commit does not rebuild the editors and drop focus. - func configure(schemaFields: [InspectorRowField], displayRow: Int) { - let names = schemaFields.map(\.name) - let reusedIds = schemaDisplayRow == displayRow && columns == names ? fields.map(\.id) : [] - - schemaDisplayRow = displayRow - rowIDs = [] - absentCells = [] - unloadedCells = [] - expressionCells = [] - databaseType = nil - cellWriter = .none - rowsAreInserted = false - columns = names - columnTypes = Array(repeating: .text(rawType: nil), count: names.count) - allRows = [schemaFields.map(\.value)] - - fields = schemaFields.enumerated().map { index, field in - var state = FieldEditState( - columnIndex: index, - columnName: field.name, - columnTypeEnum: .text(rawType: nil), - isLongText: false, - isJson: false, - editor: field.editor, - isSchemaField: true, - hasCommittedEdit: field.isModified, - originalValue: field.value, - hasMultipleValues: false, - pendingValue: nil, - isPendingNull: false, - isPendingDefault: false - ) - if index < reusedIds.count { - state.id = reusedIds[index] - } - if field.editor == nil { - state.resolvedEditor = FieldEditorResolver.resolve( - for: .text(rawType: nil), - isLongText: false, - originalValue: field.value - ) - } - state.isServerOwned = !field.isEditable - return state - } - } - // Read from the store, not a copy an earlier render captured; a field's value binding answers with this. func currentText(at index: Int) -> String { guard fields.indices.contains(index) else { return "" } @@ -408,7 +351,7 @@ final class MultiRowEditState: ObservableObject { } func setValueContext(for field: FieldEditState, isWritable: Bool) -> SetValueContext? { - guard let databaseType, !field.isSchemaField else { return nil } + guard let databaseType else { return nil } return SetValueContext( columnType: field.columnTypeEnum, isNullable: field.isNullable, @@ -647,7 +590,6 @@ final class MultiRowEditState: ObservableObject { onFieldReverted = nil onFieldRemoved = nil onDetachedFieldChanged = nil - schemaDisplayRow = nil rowIDs = [] allRows = [] absentCells = [] diff --git a/apple/TablePro/Models/UI/PendingChangeKind.swift b/apple/TablePro/Models/UI/PendingChangeKind.swift index df375dd97d..93e2626fc7 100644 --- a/apple/TablePro/Models/UI/PendingChangeKind.swift +++ b/apple/TablePro/Models/UI/PendingChangeKind.swift @@ -5,17 +5,8 @@ import Foundation -/// What the window's Save command would commit, which is also whether it can commit at all. -/// -/// One value rather than five booleans read in five places. `updateToolbarPendingState()` folded -/// four of the five sources into `hasPendingChanges` and never read the fifth, so a Users & Roles -/// tab with staged principals left both the toolbar's commit button and Cmd+S dim while -/// `saveChanges()` already carried the branch that would have applied them. -/// -/// The tab decides the kind, because two kinds can be staged at once and only one of them is the -/// one the user is looking at. It decides nothing the user reads: the commit control's label is -/// `ToolbarContextResolver.commitVerb(for:)`, from the tab kind alone, because this value comes and -/// goes with every edit and a label that followed it moved the titlebar while the user typed. +// What the window's Save would commit, and whether it can commit at all. The tab decides the kind, since two kinds +// can be staged at once; the commit label comes from the tab kind alone, so it does not move while the user types. internal enum PendingChangeKind: Equatable, Hashable, Sendable { case data case structure @@ -23,7 +14,7 @@ internal enum PendingChangeKind: Equatable, Hashable, Sendable { case principals case file - /// Nil when nothing is staged for this tab, which is what leaves the commit control dim. + // Nil when nothing is staged for this tab, which leaves the commit control dim. internal static func resolve( tabType: TabType?, hasDataChanges: Bool, @@ -41,8 +32,7 @@ internal enum PendingChangeKind: Equatable, Hashable, Sendable { } switch tabType { case .createTable: - /// A definition that is not yet committable is not a pending change: the tab's own - /// validity gate is what `hasCreateTablePending` already answers. + // A definition that is not yet committable is not a pending change. return hasCreateTablePending ? .createTable : nil case .usersRoles: return hasPrincipalChanges ? .principals : nil @@ -56,8 +46,24 @@ internal enum PendingChangeKind: Equatable, Hashable, Sendable { } } - /// Structure outranks data, and data outranks a dirty file, because a structure edit rewrites - /// the table the staged rows are going into and has to be named first. + // Narrower than the commit: a dirty file and staged principals have no SQL to show, and a structure session + // previews only from its own Structure view, so staged ALTERs behind the Data view leave Preview with nothing. + internal static func offersPreview( + of kind: PendingChangeKind?, + hasDataChanges: Bool, + previewsSchemaChanges: Bool + ) -> Bool { + switch kind { + case .data, .createTable: + return true + case .structure: + return hasDataChanges || previewsSchemaChanges + case .principals, .file, nil: + return false + } + } + + // A structure edit rewrites the table the staged rows go into, so it is named before data, and data before a file. private static func contentKind( hasDataChanges: Bool, hasStructureChanges: Bool, diff --git a/apple/TablePro/Models/UI/RowInspectorContext.swift b/apple/TablePro/Models/UI/RowInspectorContext.swift index 7a1eb23bd1..8ebe1f73d2 100644 --- a/apple/TablePro/Models/UI/RowInspectorContext.swift +++ b/apple/TablePro/Models/UI/RowInspectorContext.swift @@ -5,41 +5,25 @@ import Foundation -/// Everything the inspector draws, and nothing else. -/// -/// This used to be one `InspectorContext` carrying the row, the JSON snapshot and a ten-row text -/// summary of the grid for the assistant, so a selection change rebuilt the assistant's context and -/// an assistant change rebuilt the row's. Splitting them along the surfaces that read them is what -/// lets each be `Equatable`: the old struct held an untyped `[(column:value:type:)]` tuple array, -/// which no equality could be written for, so every rebuild was a change whether or not anything -/// had moved. -/// -/// The fields themselves are not here. They live in `MultiRowEditState`, which both the data grid -/// and the schema grid already configure, and duplicating them into a second array is what made the -/// old tuple path dead computation: it ran the display-format and blob-format pipeline over every -/// cell on every tick and the view read only whether it was nil. +// Everything the inspector draws. The assistant's context is separate, so a selection change never rebuilds it. +// The row's fields are not here: they live in `MultiRowEditState`. internal struct RowInspectorContext: Equatable { internal let subject: InspectorSubject - /// Whether a row is selected at all, which the mode choice turns on. Separate from the subject - /// because a subject exists for a table with no row selected. + // Separate from the subject: a subject exists for a table with no row selected. internal let hasRow: Bool internal let isEditable: Bool internal let isRowDeleted: Bool - /// Present only on a tab that actually has a table. The old context handed over the - /// coordinator's one metadata slot unconditionally, latest-wins and cleared only on teardown, so - /// a query tab, a dashboard or an ER diagram showed the statistics of whichever table had been - /// opened last, including after its tab was closed. + // Only on a tab that has a table, so a query tab never shows the statistics of the last table opened. internal let tableMetadata: TableMetadata? - /// The same row the fields mode shows, as raw cell values, because the JSON mode decides from - /// the column's own type whether a value prints quoted. + // Raw cell values, because the JSON rendering quotes a value by its column's own type. internal let jsonRow: JSONRowSnapshot? - /// The table a structure row's type picker offers user-defined types for. Nil outside one. - internal let userDefinedTypeScope: DatabaseScope? + // Set while a Structure view or a Create Table tab is on screen; the inspector then edits its definitions. + internal var structure: StructureInspectorSource? internal static let empty = RowInspectorContext( subject: .empty, @@ -48,14 +32,25 @@ internal struct RowInspectorContext: Equatable { isRowDeleted: false, tableMetadata: nil, jsonRow: nil, - userDefinedTypeScope: nil + structure: nil ) } -/// What the assistant needs to know about the window it is docked in. -/// -/// Kept apart from the row context so that moving the selection does not rebuild it, and so that a -/// window whose assistant was never revealed never pays for the grid summary at all. +// The structure editor on screen, compared by identity. The inspector observes the editor itself, so a selection +// or an edit redraws it without a new context. +internal enum StructureInspectorSource: Equatable { + case table(StructureEditingSession) + case createTable(CreateTableDraft) + + internal static func == (lhs: StructureInspectorSource, rhs: StructureInspectorSource) -> Bool { + switch (lhs, rhs) { + case (.table(let left), .table(let right)): left === right + case (.createTable(let left), .createTable(let right)): left === right + default: false + } + } +} + internal struct AssistantContext: Equatable { internal let currentQuery: String? internal let queryResults: String? diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index faf51f7f82..f9c6cadf13 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -455,6 +455,9 @@ }, "No foreign key links %@ to the tables before it, so it is cross joined." : { + }, + "No Foreign Keys" : { + }, "No row holds the values it was loaded with. Another client may have changed or deleted it." : { @@ -699,6 +702,9 @@ }, "The changes were saved, but the type could not be read back. Reload before editing it again." : { + }, + "The changes you have not saved will be lost." : { + }, "The client certificate and key could not be written to a temporary file." : { "extractionState" : "manual", @@ -997,6 +1003,9 @@ } } } + }, + "-- Error generating SQL: %@" : { + }, "-- Once the rows are in" : { "localizations" : { @@ -4039,6 +4048,9 @@ } } } + }, + "%@ DDL" : { + }, "%@ does not accept this column change." : { "localizations" : { @@ -9387,9 +9399,15 @@ } } } + }, + "%d changes to this table's structure will be lost." : { + }, "%d characters" : { + }, + "%d Check Constraints Selected" : { + }, "%d columns" : { "localizations" : { @@ -9430,6 +9448,9 @@ } } } + }, + "%d Columns Selected" : { + }, "%d connected" : { "localizations" : { @@ -9876,6 +9897,9 @@ } } } + }, + "%d Foreign Keys Selected" : { + }, "%d included" : { "localizations" : { @@ -9916,6 +9940,9 @@ } } } + }, + "%d Indexes Selected" : { + }, "%d item(s) affected" : { "extractionState" : "manual", @@ -19973,6 +20000,9 @@ } } } + }, + "Add check constraint “%@”" : { + }, "Add Column" : { "localizations" : { @@ -20013,6 +20043,9 @@ } } } + }, + "Add column “%@”" : { + }, "Add Column…" : { "localizations" : { @@ -20496,6 +20529,9 @@ } } } + }, + "Add foreign key “%@”" : { + }, "Add Global Index" : { "extractionState" : "manual", @@ -20590,6 +20626,9 @@ } } } + }, + "Add index “%@”" : { + }, "Add indexes to improve query performance on frequently searched columns" : { "localizations" : { @@ -25520,6 +25559,9 @@ } } } + }, + "Another check constraint is already named %@." : { + }, "Another column is already named %@." : { "localizations" : { @@ -35750,6 +35792,32 @@ } } }, + "Change check constraint “%@”" : { + + }, + "Change check constraint “%@” to “%@”" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "new", + "value" : "Change check constraint “%1$@” to “%2$@”" + } + } + } + }, + "Change column “%@”" : { + + }, + "Change column “%@” to “%@”" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "new", + "value" : "Change column “%1$@” to “%2$@”" + } + } + } + }, "Change File" : { "extractionState" : "stale", "localizations" : { @@ -35831,6 +35899,19 @@ } } }, + "Change foreign key “%@”" : { + + }, + "Change foreign key “%@” to “%@”" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "new", + "value" : "Change foreign key “%1$@” to “%2$@”" + } + } + } + }, "Change Host to a name the certificate covers." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -35843,6 +35924,19 @@ } } }, + "Change index “%@”" : { + + }, + "Change index “%@” to “%@”" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "new", + "value" : "Change index “%1$@” to “%2$@”" + } + } + } + }, "Change Not Saved" : { "localizations" : { "fr" : { @@ -36092,6 +36186,9 @@ } } } + }, + "Change the table comment" : { + }, "Change this in Settings > Integrations." : { "extractionState" : "stale", @@ -37177,6 +37274,9 @@ } } } + }, + "Check Constraint" : { + }, "Check constraint must have a name and an expression" : { "localizations" : { @@ -37187,6 +37287,12 @@ } } } + }, + "Check Constraint of %@" : { + + }, + "Check Constraints" : { + }, "Check constraints need %@ or later." : { "extractionState" : "manual", @@ -50210,6 +50316,9 @@ } } } + }, + "Convert the table to %@" : { + }, "Converting the table's character set rewrites every text column and loses characters the new one cannot hold." : { @@ -54912,6 +55021,9 @@ }, "Couldn't read the character sets" : { + }, + "Couldn't Read the DDL" : { + }, "Couldn't read the referenced table" : { "localizations" : { @@ -54931,6 +55043,9 @@ }, "Couldn't read the table options" : { + }, + "Couldn't Read the Triggers" : { + }, "Couldn't Refresh Materialized View" : { "localizations" : { @@ -56038,6 +56153,9 @@ } } } + }, + "Create Table cannot add a check constraint on this database." : { + }, "Create Table cannot add an index on this database." : { "localizations" : { @@ -60872,6 +60990,9 @@ } } } + }, + "Decrease Font Size" : { + }, "Decrypt" : { "localizations" : { @@ -62152,6 +62273,9 @@ } } } + }, + "Delete check constraint “%@”" : { + }, "Delete Column" : { "localizations" : { @@ -62192,6 +62316,9 @@ } } } + }, + "Delete column “%@”" : { + }, "Delete Columns" : { "localizations" : { @@ -62465,6 +62592,9 @@ } } } + }, + "Delete foreign key “%@”" : { + }, "Delete Group" : { "localizations" : { @@ -62555,6 +62685,9 @@ } } } + }, + "Delete index “%@”" : { + }, "Delete is off by default, so a first run cannot remove a row from the target." : { "localizations" : { @@ -84442,6 +84575,9 @@ } } } + }, + "Foreign Key" : { + }, "Foreign key %@ matches %@ on the target but the names differ." : { "localizations" : { @@ -84502,6 +84638,9 @@ } } } + }, + "Foreign Key of %@" : { + }, "Foreign key points at a column that does not exist: %@" : { "localizations" : { @@ -93548,6 +93687,9 @@ } } } + }, + "Increase Font Size" : { + }, "Increment" : { @@ -93763,6 +93905,9 @@ } } } + }, + "Index of %@" : { + }, "Index proposal on %@" : { "localizations" : { @@ -106717,6 +106862,9 @@ } } } + }, + "Marked for deletion. Undo or Undo Delete brings it back." : { + }, "Match" : { "localizations" : { @@ -113381,6 +113529,12 @@ } } } + }, + "New Check Constraint" : { + + }, + "New Column" : { + }, "New Connection" : { "localizations" : { @@ -113764,6 +113918,9 @@ } } } + }, + "New Foreign Key" : { + }, "New Group" : { "localizations" : { @@ -113859,6 +114016,9 @@ } } } + }, + "New Index" : { + }, "New Jump Host" : { "localizations" : { @@ -115473,6 +115633,9 @@ } } } + }, + "No Columns" : { + }, "No Columns Defined" : { "localizations" : { @@ -116456,6 +116619,9 @@ } } } + }, + "No DDL Available" : { + }, "No DDL found for %@." : { "extractionState" : "manual", @@ -117073,6 +117239,9 @@ } } } + }, + "No Indexes" : { + }, "No Indexes Defined" : { "localizations" : { @@ -144607,6 +144776,9 @@ } } } + }, + "Refreshing reads the table structure again and discards the changes you have not saved." : { + }, "Refreshing will discard all unsaved changes." : { "localizations" : { @@ -146799,6 +146971,9 @@ } } } + }, + "Remove the primary key" : { + }, "Remove the second entry." : { "extractionState" : "manual", @@ -154727,6 +154902,9 @@ } } } + }, + "Safe Mode is read-only for this connection." : { + }, "Safe mode level" : { "localizations" : { @@ -159750,6 +159928,9 @@ }, "Select a column" : { + }, + "Select a definition to edit it" : { + }, "Select a dump file this engine's own tool wrote." : { "localizations" : { @@ -160484,6 +160665,9 @@ } } } + }, + "Select one to edit it" : { + }, "Select Plugin" : { "localizations" : { @@ -162967,6 +163151,15 @@ } } } + }, + "Set the next AUTO_INCREMENT value to %@" : { + + }, + "Set the primary key to %@" : { + + }, + "Set the table's default character set to %@" : { + }, "Set to NULL" : { "localizations" : { @@ -183718,6 +183911,9 @@ } } } + }, + "The change to this table's structure will be lost." : { + }, "The changes were edited while the save was checking other tables. Save again to include them." : { @@ -191621,6 +191817,9 @@ } } } + }, + "The table is being read again after the save." : { + }, "The table is no longer reachable." : { "localizations" : { @@ -193791,6 +193990,12 @@ } } } + }, + "This check constraint needs a name." : { + + }, + "This check constraint needs an expression." : { + }, "This client does not have access to that connection." : { "localizations" : { diff --git a/apple/TablePro/Views/DataFiles/DataFileRowDetailsView.swift b/apple/TablePro/Views/DataFiles/DataFileRowDetailsView.swift index f7d8bc992e..20ae8e73fa 100644 --- a/apple/TablePro/Views/DataFiles/DataFileRowDetailsView.swift +++ b/apple/TablePro/Views/DataFiles/DataFileRowDetailsView.swift @@ -31,7 +31,6 @@ struct DataFileRowDetailsView: View { editState: editState, isEditable: controller.isEditable && !controller.isBusy, databaseType: Self.databaseType, - userDefinedTypeScope: nil, offersDatabaseValues: false ) } diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift index be6494909c..b32be5c107 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift @@ -23,7 +23,8 @@ struct DataGridCellContent { static func placeholder( for rawValue: PluginCellValue, isAbsent: Bool = false, - unloadedByteCount: Int? = nil + unloadedByteCount: Int? = nil, + marksEmptyText: Bool = true ) -> DataGridCellPlaceholder? { if let unloadedByteCount { return .unloaded(byteCount: unloadedByteCount) } guard !isAbsent else { return .absent } @@ -32,7 +33,7 @@ struct DataGridCellContent { return .null case .text(let value): if value == PluginCellValue.defaultMarkerText { return .defaultMarker } - return value.isEmpty ? .empty : nil + 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 4db4c80598..8f7745aac0 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift @@ -16,6 +16,7 @@ final class DataGridCellPresenter { let findMatch: GridFindState.Match? let isEditable: Bool let checkboxColumns: Set + let marksEmptyText: Bool let palette: DataGridCellPalette let scope: ThemeScope let databaseType: DatabaseType? @@ -55,6 +56,7 @@ final class DataGridCellPresenter { findMatch: model.find.currentMatch, isEditable: model.configuration.isEditable, checkboxColumns: model.configuration.checkboxColumns, + marksEmptyText: model.configuration.marksEmptyText, palette: registry.palette, scope: model.themeScope, databaseType: model.configuration.traits.databaseType, @@ -248,7 +250,8 @@ final class DataGridCellPresenter { placeholder: DataGridCellContent.placeholder( for: rawValue, isAbsent: row.isAbsent(dataIndex), - unloadedByteCount: row.unloadedByteCount(dataIndex) + unloadedByteCount: row.unloadedByteCount(dataIndex), + marksEmptyText: pass.marksEmptyText ) ), state: DataGridCellState( @@ -294,7 +297,8 @@ final class DataGridCellPresenter { switch DataGridCellContent.placeholder( for: rawValue, isAbsent: row.isAbsent(dataIndex), - unloadedByteCount: row.unloadedByteCount(dataIndex) + unloadedByteCount: row.unloadedByteCount(dataIndex), + marksEmptyText: model.configuration.marksEmptyText ) { case .null: return String(localized: "NULL") case .empty: return String(localized: "Empty") diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridMetrics.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridMetrics.swift index 05091ede89..54adc3e2e6 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridMetrics.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridMetrics.swift @@ -21,4 +21,14 @@ enum DataGridMetrics { static func rowHeight(_ setting: DataGridRowHeight, font: NSFont) -> CGFloat { max(CGFloat(setting.rawValue), (font.ascender - font.descender + font.leading).rounded(.up)) } + + // An overlay scroller floats over the last row, so only a legacy one adds height. + static func fittingHeight( + headerHeight: CGFloat, + rowCount: Int, + rowPitch: CGFloat, + horizontalScrollerHeight: CGFloat + ) -> CGFloat { + headerHeight + CGFloat(max(rowCount, 0)) * rowPitch + horizontalScrollerHeight + } } diff --git a/apple/TablePro/Views/DataGrid/DataGridContainerView.swift b/apple/TablePro/Views/DataGrid/DataGridContainerView.swift index 836a407943..a7aa4aa40f 100644 --- a/apple/TablePro/Views/DataGrid/DataGridContainerView.swift +++ b/apple/TablePro/Views/DataGrid/DataGridContainerView.swift @@ -17,6 +17,15 @@ final class DataGridContainerView: NSView { private var firstResponderObservation: NSKeyValueObservation? private var windowObservers: [NSObjectProtocol] = [] private var scrollObservers: [NSObjectProtocol] = [] + private var pageScrollObserver: NSObjectProtocol? + + // A grid sized to its rows moves only with its page, so the page's scroll stands in for its own. + var followsPageScroll = false { + didSet { + guard followsPageScroll != oldValue else { return } + observePageScroll() + } + } var frozenPaneWidth: CGFloat = 0 { didSet { @@ -33,7 +42,10 @@ final class DataGridContainerView: NSView { addSubview(frozen.scrollView) frozen.scrollView.isHidden = true frozen.scrollView.wheelTarget = main.scrollView - main.scrollView.onTile = { [weak self] in self?.layoutFrozenPane() } + main.scrollView.onTile = { [weak self] in + self?.layoutFrozenPane() + self?.controller?.updateFittingHeight() + } observeScrolling() } @@ -127,9 +139,25 @@ final class DataGridContainerView: NSView { isSyncingScroll = false } + private func observePageScroll() { + if let pageScrollObserver { + NotificationCenter.default.removeObserver(pageScrollObserver) + } + pageScrollObserver = nil + guard followsPageScroll, window != nil, let clip = enclosingScrollView?.contentView else { return } + pageScrollObserver = NotificationCenter.default.addObserver( + forName: NSView.boundsDidChangeNotification, + object: clip, + queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.controller?.mainViewportDidMove() } + } + } + func tearDown() { scrollObservers.forEach(NotificationCenter.default.removeObserver) scrollObservers.removeAll() + followsPageScroll = false stopObservingWindow() } @@ -138,6 +166,7 @@ final class DataGridContainerView: NSView { override func viewDidMoveToWindow() { super.viewDidMoveToWindow() stopObservingWindow() + observePageScroll() guard let window else { refreshFocus() return diff --git a/apple/TablePro/Views/DataGrid/DataGridScrollView.swift b/apple/TablePro/Views/DataGrid/DataGridScrollView.swift index 99afcdf289..ec8e8cee6c 100644 --- a/apple/TablePro/Views/DataGrid/DataGridScrollView.swift +++ b/apple/TablePro/Views/DataGrid/DataGridScrollView.swift @@ -11,6 +11,16 @@ final class DataGridScrollView: NSScrollView { var onTile: (() -> Void)? // The frozen pane hands every wheel event to the main pane, so one momentum engine moves both. weak var wheelTarget: NSScrollView? + private var wheelRouting = DataGridWheelRouting() + + // The page the grid sits in scrolls vertically for it, so the main pane keeps only its sideways scroll. + var fitsRows = false { + didSet { + guard fitsRows != oldValue, paneKind == .main else { return } + hasVerticalScroller = !fitsRows + verticalScrollElasticity = fitsRows ? .none : .automatic + } + } init(kind: DataGridPane.Kind) { paneKind = kind @@ -43,11 +53,23 @@ final class DataGridScrollView: NSScrollView { } override func scrollWheel(with event: NSEvent) { - guard let wheelTarget else { - super.scrollWheel(with: event) + if let wheelTarget { + wheelTarget.scrollWheel(with: event) return } - wheelTarget.scrollWheel(with: event) + if fitsRows, let page = superview?.enclosingScrollView { + let axis = wheelRouting.axis( + phase: event.phase, + momentumPhase: event.momentumPhase, + deltaX: event.scrollingDeltaX, + deltaY: event.scrollingDeltaY + ) + if axis == .vertical { + page.scrollWheel(with: event) + return + } + } + super.scrollWheel(with: event) } // The frozen pane repeats cells the main table already publishes, so VoiceOver sees one table. @@ -61,3 +83,32 @@ final class DataGridScrollView: NSScrollView { return scroller.frame.height } } + +// One axis per trackpad gesture, momentum included, so a swipe never splits between the grid and its page. +// A wheel with no phases is a mouse, and each notch decides alone. +struct DataGridWheelRouting { + enum Axis { + case vertical + case horizontal + } + + private var gestureAxis: Axis? + + mutating func axis(phase: NSEvent.Phase, momentumPhase: NSEvent.Phase, deltaX: CGFloat, deltaY: CGFloat) -> Axis { + if phase.isEmpty, momentumPhase.isEmpty { + return Self.axis(deltaX: deltaX, deltaY: deltaY) ?? .vertical + } + if phase.contains(.began) || phase.contains(.mayBegin) { + gestureAxis = Self.axis(deltaX: deltaX, deltaY: deltaY) + } else if gestureAxis == nil, !phase.isEmpty { + // A gesture can begin with no movement; its first move decides. + gestureAxis = Self.axis(deltaX: deltaX, deltaY: deltaY) + } + return gestureAxis ?? .vertical + } + + static func axis(deltaX: CGFloat, deltaY: CGFloat) -> Axis? { + guard deltaX != 0 || deltaY != 0 else { return nil } + return abs(deltaY) >= abs(deltaX) ? .vertical : .horizontal + } +} diff --git a/apple/TablePro/Views/DataGrid/DataGridTableView.swift b/apple/TablePro/Views/DataGrid/DataGridTableView.swift index d1edf45a3e..618010def0 100644 --- a/apple/TablePro/Views/DataGrid/DataGridTableView.swift +++ b/apple/TablePro/Views/DataGrid/DataGridTableView.swift @@ -51,7 +51,22 @@ final class DataGridTableView: NSTableView { // A revealed column must never park under the frozen pane, so the rect widens leftward by its width. override func scrollToVisible(_ rect: NSRect) -> Bool { guard paneKind == .main, let controller else { return super.scrollToVisible(rect) } - return super.scrollToVisible(controller.revealRect(for: rect, in: self)) + let widened = controller.revealRect(for: rect, in: self) + guard controller.model.configuration.fitsRows else { return super.scrollToVisible(widened) } + // Only sideways. AppKit calls this itself while the pane resizes, with rects spanning the header, so the page + // moves only for a reveal the grid starts (`scrollRowToVisible`, `reveal(row:displayColumn:)`). + let visible = visibleRect + return super.scrollToVisible(NSRect(x: widened.minX, y: visible.midY, width: widened.width, height: 1)) + } + + // Up, Down and type-select reveal through here; a table that fits its rows has nothing to scroll, so the page moves. + override func scrollRowToVisible(_ row: Int) { + guard paneKind == .main, let controller, controller.model.configuration.fitsRows else { + super.scrollRowToVisible(row) + return + } + guard row >= 0, row < numberOfRows else { return } + controller.revealOnPage(rect(ofRow: row)) } // MARK: - Keyboard diff --git a/apple/TablePro/Views/DataGrid/DataGridView.swift b/apple/TablePro/Views/DataGrid/DataGridView.swift index 1ed325eb32..8f66e7123c 100644 --- a/apple/TablePro/Views/DataGrid/DataGridView.swift +++ b/apple/TablePro/Views/DataGrid/DataGridView.swift @@ -16,7 +16,12 @@ struct DataGridView: View { var body: some View { VStack(spacing: 0) { DataGridFindBarSlot(model: model, find: model.find) - DataGridRepresentable(model: model) + if model.configuration.fitsRows { + DataGridRepresentable(model: model) + .frame(height: model.fittingHeight) + } else { + DataGridRepresentable(model: model) + } } } } diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift index ab5f2b8bd8..9553566c2e 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift @@ -41,6 +41,7 @@ extension DataGridViewController { applyRowNumberWidth() syncSortIndicators() redrawVisibleRows() + updateFittingHeight() } // MARK: - Structural changes @@ -60,6 +61,7 @@ extension DataGridViewController { updateHighlightedColumns(model.selection) syncSortIndicators() startBackgroundPrewarm() + updateFittingHeight() } private func columnsDidChange() { @@ -88,6 +90,7 @@ extension DataGridViewController { } applyRowNumberWidth() startBackgroundPrewarm() + updateFittingHeight() } private func removeRows(_ indexes: IndexSet) { @@ -102,6 +105,7 @@ extension DataGridViewController { pane.tableView.removeRows(at: indexes, withAnimation: []) } applyRowNumberWidth() + updateFittingHeight() } private func updateRows(_ rows: IndexSet, columns displayColumns: IndexSet?) { @@ -169,6 +173,31 @@ extension DataGridViewController { pane.tableView.tile() } } + updateFittingHeight() + } + + // The table's own header, rows and legacy scroller: what a fitsRows grid needs to show every row. + var measuredFittingHeight: CGFloat { + let table = main.tableView + return DataGridMetrics.fittingHeight( + headerHeight: table.headerView?.frame.height ?? 0, + rowCount: table.numberOfRows, + rowPitch: table.rowHeight + table.intercellSpacing.height, + horizontalScrollerHeight: main.scrollView.legacyHorizontalScrollerHeight + ) + } + + // Published on the next turn: this runs inside SwiftUI updates and layout passes, which refuse a model change. + func updateFittingHeight() { + guard model.configuration.fitsRows, !isFittingHeightScheduled, + measuredFittingHeight != model.fittingHeight else { return } + isFittingHeightScheduled = true + DispatchQueue.main.async { [weak self] in + guard let self else { return } + self.isFittingHeightScheduled = false + guard self.model.mountedView === self, self.model.configuration.fitsRows else { return } + self.model.setFittingHeight(self.measuredFittingHeight) + } } // The clip view keeps its offset in points, so a new row height would otherwise bring other rows into view. diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift index 7728930d6a..f3fb692a25 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift @@ -180,10 +180,13 @@ extension DataGridViewController { func reveal(row: Int, displayColumn: Int?) { let table = main.tableView guard row >= 0, row < table.numberOfRows else { return } - table.scrollRowToVisible(row) - if let displayColumn { - revealColumn(displayColumn) + revealingInGrid { + table.scrollRowToVisible(row) + if let displayColumn { + revealColumn(displayColumn) + } } + revealOnPage(table.rect(ofRow: row)) } // Horizontal only; a frozen column is on screen at every offset. @@ -193,7 +196,46 @@ extension DataGridViewController { let index = main.tableIndex(forDisplayPosition: displayColumn, columns: model.columns) else { return } let columnRect = table.rect(ofColumn: index) let visible = table.visibleRect - _ = table.scrollToVisible(NSRect(x: columnRect.minX, y: visible.midY, width: columnRect.width, height: 1)) + revealingInGrid { + _ = table.scrollToVisible(NSRect(x: columnRect.minX, y: visible.midY, width: columnRect.width, height: 1)) + } + } + + private func revealingInGrid(_ body: () -> Void) { + let wasRevealing = isRevealingInGrid + isRevealingInGrid = true + body() + isRevealingInGrid = wasRevealing + } + + // A grid sized to its rows shows them all, so a row it reveals is brought into view by the page it sits in. + // Kept until the grid has grown to a row it just took, since the page lays out after this turn. + // A reveal the grid cannot show within two turns is dropped, so a later layout never scrolls the page on its own. + func revealOnPage(_ rectInTable: NSRect) { + guard model.configuration.fitsRows, !isRevealingInGrid else { return } + pendingPageReveal = rectInTable + performPageReveal() + guard pendingPageReveal != nil else { return } + DispatchQueue.main.async { [weak self] in + DispatchQueue.main.async { [weak self] in + self?.performPageReveal() + self?.pendingPageReveal = nil + } + } + } + + func performPageReveal() { + guard let rect = pendingPageReveal else { return } + let target = container.convert(rect, from: main.tableView) + let bounds = container.bounds + guard target.maxY <= bounds.maxY + 1 else { return } + pendingPageReveal = nil + let visible = container.visibleRect + guard target.minY < visible.minY || target.maxY > visible.maxY else { return } + // Kept within the grid's width: the grid already moved its columns, so the page moves only vertically. + let minX = min(max(target.minX, bounds.minX), bounds.maxX) + let maxX = max(min(target.maxX, bounds.maxX), minX) + _ = container.scrollToVisible(NSRect(x: minX, y: target.minY, width: maxX - minX, height: target.height)) } func revealActiveCell() { @@ -299,6 +341,9 @@ extension DataGridViewController { while let event = window.nextEvent(matching: [.leftMouseDragged, .leftMouseUp]) { if event.type == .leftMouseUp { break } _ = main.tableView.autoscroll(with: event) + if model.configuration.fitsRows { + _ = container.autoscroll(with: event) + } guard let cell = dragCell(at: event.locationInWindow, rowsOnly: drag.mode == .rows) else { continue } if drag.mode == .rows ? cell.row != start.row : cell != start { moved = true diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController.swift b/apple/TablePro/Views/DataGrid/DataGridViewController.swift index 3776585b3e..ea5af37b7b 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController.swift @@ -36,6 +36,9 @@ final class DataGridViewController: NSViewController, DataGridMountedView { var prewarmTask: Task? var prewarmResumeTask: Task? var reorderToolTipOwner: NSString? + var isFittingHeightScheduled = false + var pendingPageReveal: NSRect? + var isRevealingInGrid = false private var observers: [NSObjectProtocol] = [] private var cancellables: Set = [] private var themeCancellable: AnyCancellable? @@ -109,6 +112,7 @@ final class DataGridViewController: NSViewController, DataGridMountedView { knownWidths = [:] knownWidthsSawRows = false savedWidthIDs = [] + pendingPageReveal = nil mount() } @@ -124,6 +128,7 @@ final class DataGridViewController: NSViewController, DataGridMountedView { prewarmTask = nil prewarmResumeTask?.cancel() prewarmResumeTask = nil + pendingPageReveal = nil observers.forEach(NotificationCenter.default.removeObserver) observers.removeAll() cancellables.removeAll() @@ -153,6 +158,11 @@ final class DataGridViewController: NSViewController, DataGridMountedView { needsViewportRestore = false applyStagedViewport() } + updateFittingHeight() + if pendingPageReveal != nil { + // The page scrolls once its own layout is done, so its document already holds the grid's new height. + DispatchQueue.main.async { [weak self] in self?.performPageReveal() } + } } // MARK: - DataGridMountedView @@ -297,6 +307,8 @@ final class DataGridViewController: NSViewController, DataGridMountedView { applyRowReorder(configuration.rowReorder, to: table) } main.tableView.setAccessibilityLabel(configuration.accessibilityLabel ?? String(localized: "Data grid")) + main.scrollView.fitsRows = configuration.fitsRows + container.followsPageScroll = configuration.fitsRows applyRowHeight() } diff --git a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift index a201566f7c..e96674225f 100644 --- a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -553,15 +553,15 @@ struct MainEditorContentView: View { private func createTableContent(tab: QueryTab) -> some View { Group { if let draft = coordinator.createTableDrafts[tab.id] { - CreateTableView( - connection: connection, - scope: structureScope(for: tab), - coordinator: coordinator, - draft: draft - ) + CreateTableView(draft: draft, coordinator: coordinator) } else { Color.clear - .onAppear { coordinator.createTableDrafts[tab.id] = CreateTableDraft() } + .onAppear { + coordinator.createTableDrafts[tab.id] = CreateTableDraft( + connection: connection, + scope: structureScope(for: tab) + ) + } } } .id(tab.id) @@ -576,13 +576,9 @@ struct MainEditorContentView: View { Group { if let session = coordinator.structureSessions[tab.id], session.identity == identity { TableStructureView( - tableName: tableName, - connection: connection, - databaseName: scope?.database ?? "", - schemaName: scope?.schema, + session: session, toolbarState: coordinator.toolbarState, - coordinator: coordinator, - session: session + coordinator: coordinator ) } else { Color.clear @@ -880,14 +876,12 @@ struct MainEditorContentView: View { private func statusBar(tab: QueryTab, selectedRowCount: Int) -> some View { let resolvedRows = resolvedTableRows(for: tab) - let structureFooter = coordinator.structureSessions[tab.id]?.footer ?? StructureFooterCapability() let isExecuting = coordinator.tabExecution.isBusy(tab.id) let snapshot = StatusBarSnapshot( tab: tab, tableRows: resolvedRows, displayRowCount: coordinator.displayIDs(forTab: tab.id)?.count, isFetching: isExecuting, - hasStructureActions: structureFooter.isActive, isQueryPlan: tab.display.activeExplainResult != nil, paginationCapability: coordinator.paginationCapability, hasServerOutput: !(tab.display.activeResultSet?.serverOutput.isEmpty ?? true), @@ -935,7 +929,6 @@ struct MainEditorContentView: View { onGoToPage: onGoToPage, onRequestExactCount: { coordinator.paginationCoordinator.requestExactRowCount() } ), - structureFooter: structureFooter, execution: ExecutionReadout( tabId: tab.id, execution: coordinator.tabExecution, @@ -956,9 +949,7 @@ struct MainEditorContentView: View { } }, onToggleFilters: { coordinator.toggleFilterPanel() }, - onFetchAll: { coordinator.fetchAllRows() }, - onStructureAdd: { coordinator.structureActions?.addRow?() }, - onStructureRemove: { coordinator.structureActions?.removeRow?() } + onFetchAll: { coordinator.fetchAllRows() } ) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridModels.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridModels.swift index ad9c2ffc2a..be655cf9ae 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridModels.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridModels.swift @@ -143,6 +143,8 @@ extension MainContentCoordinator { if let schemaModel = activeGridModelProvider(for: tab)?.activeGridModel { committed = schemaModel.commitEditing() && committed } + // A run of typing in the structure inspector ends, so the command's undo step stands apart from it. + activeStructureEditor?.commitPendingEdits() return committed } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Protection.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Protection.swift index 1be85f35cf..f7b85bde40 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Protection.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Protection.swift @@ -1,9 +1,7 @@ import AppKit import Foundation -/// The single answer to "would closing or quitting destroy something the user cannot get back". -/// The close path (one tab) and the quit path (every tab of every connection) previously each -/// carried their own partial version of this and had drifted apart. +// The one answer to "would closing or quitting destroy something the user cannot get back", for close and quit alike. extension MainContentCoordinator { var hasPendingDestructiveTableOps: Bool { guard let session = DatabaseManager.shared.session(for: connectionId) else { return false } @@ -14,62 +12,34 @@ extension MainContentCoordinator { trailingPaneState?.inspector.editState.hasEdits ?? false } - /// Only the selected tab's editors are mounted, so only the selected tab has live state on the - /// coordinator to read. + // Only the selected tab's editors are mounted, so only it has live state on the coordinator. func isSelectedTab(_ tab: QueryTab) -> Bool { tabManager.selectedTabId == tab.id } - /// Work in one tab that only saving can recover. - /// - /// Where that work lives depends on whether the tab is selected, because a tab's editors are - /// mounted only while it is. The selected tab's live edits sit on the coordinator, in - /// `changeManager` and `toolbarState`; a background tab carries whatever was snapshotted into - /// `pendingChanges` the last time it was switched away from. Reading the snapshot for the - /// selected tab answers "no" for the gesture users make most, editing a cell and closing the - /// tab they are looking at, because nothing has switched away to write the snapshot yet. - /// - /// Connection-scoped work is deliberately absent. A staged TRUNCATE or an unsaved sidebar edit - /// belongs to the connection rather than to any one tab, so letting it gate a tab's close would - /// pose a question neither Save nor Don't Save could answer for the tab being closed. - /// - /// A scratch query tab is absent too, and that one is a decision rather than an omission: its - /// text is persisted with the tab and filed in `RecentlyClosedTabStore` on close, so it comes - /// back. Grid edits do not. `TabChangeSnapshot` is not `Codable` and `PersistedTab` carries no - /// change fields, so closing a table tab is the one gesture that destroys them for good. + // Connection-scoped work (a staged TRUNCATE, a sidebar edit) is left out: no tab's Save or Don't Save answers it. + // A scratch query's text is left out too: it is persisted and comes back from Reopen Closed Tab. func hasUnsavedWork(in tab: QueryTab?) -> Bool { guard let tab else { return false } return savability(of: tab) != .nothingAtRisk } - /// Whether one tab, named by id, is holding work a move would lose. - /// - /// Detaching a tab into its own window carries the `QueryTab` and nothing the coordinator holds - /// beside it, so this is what the command reads before offering itself. An id that names no tab - /// answers true: refusing a move is recoverable, performing one on a tab this window cannot see - /// is not. + // An unknown id answers true: refusing a move is recoverable, moving a tab this window cannot see is not. func hasUnsavedWork(forTab id: UUID) -> Bool { guard let tab = tabManager.tabs.first(where: { $0.id == id }) else { return true } return savability(of: tab) != .nothingAtRisk } - /// What a save can actually do for one tab, which is not the same question as whether it holds - /// work. A batch close asks both: it saves what it can reach and leaves the rest open, so the - /// two answers have to come from one switch. Reading the categories apart, in a second - /// classifier beside this one, is how the alert and the save came to disagree about what Save - /// covers. + // One switch answers both "holds work" and "can Save reach it", so a batch close's alert and save cannot disagree. enum TabWorkSavability: Equatable { case nothingAtRisk - /// Reachable without the tab on screen: its file, and the ALTERs staged in its own session. + // Reachable without the tab on screen: its file, and the ALTERs staged in its own session. case saveable - /// Only the tab's own editors can save it: grid edits, staged principals, a table draft. + // Only the tab's own editors can save it: grid edits, staged principals, a table draft. case mountedOnly } - /// The unreachable kinds are asked about first. A tab can hold more than one: a query opened - /// from a file, edited, with unsaved cell edits in its result. Answering `.saveable` for the file - /// would let a close write the file and destroy the grid edits beside it, which is the loss this - /// category exists to prevent. + // Unreachable kinds first: answering `.saveable` for a dirty file would let a close drop the grid edits beside it. func savability(of tab: QueryTab) -> TabWorkSavability { guard isSelectedTab(tab) else { switch tab.tabType { @@ -90,12 +60,7 @@ extension MainContentCoordinator { tab.content.isFileDirty ? .saveable : .nothingAtRisk } - /// The selected tab's editors are mounted, so everything they hold is reachable and saving the - /// selected tab covers all of it. A deselected tab left its editors behind, which is why - /// `savability` answers for it from what they wrote down before they went: users and roles keeps - /// its own record because its view model outlives the view while `usersRolesActions` does not, - /// so the staged principals survive a deselect even though nothing on the coordinator can still - /// reach them to apply. + // Users & Roles keeps its own record because its view model outlives `usersRolesActions`. private func liveUnsavedWork(in tab: QueryTab) -> Bool { switch tab.tabType { case .usersRoles: @@ -104,15 +69,12 @@ extension MainContentCoordinator { return toolbarState.hasCreateTablePending || hasTableDraftWork(in: tab) default: return changeManager.hasChanges - || toolbarState.hasStructureChanges || hasStagedStructureEdits(in: tab) || hasStagedTypeChanges(in: tab) } } - /// Staged ALTERs read from the tab's own session rather than from `toolbarState`, which only - /// ever describes the tab on screen. A background tab keeps its session, so this is the only - /// answer that holds once the user has switched away from it. + // Read from the tab's session, mounted or not, so it holds in Data view and in a background tab. func hasStagedStructureEdits(in tab: QueryTab) -> Bool { (structureSessions[tab.id]?.changeManager.hasChanges ?? false) || (erDiagramModels[tab.id]?.holdsUnsavedWork ?? false) @@ -131,19 +93,12 @@ extension MainContentCoordinator { createTableDrafts[tab.id]?.holdsWork ?? false } - /// The dot on the tab and in the window's close button. Deliberately broader than - /// `hasUnsavedWork(in:)`: a scratch query tab shows the dot because its text is unsaved, yet - /// closing it asks nothing because the text comes back. It is never narrower, so anything that - /// would raise the save prompt is marked before the user reaches for the close button. + // Never narrower than `hasUnsavedWork(in:)`; broader for a scratch tab, whose text is unsaved but comes back. func showsUnsavedIndicator(for tab: QueryTab) -> Bool { tab.showsUnsavedIndicator || hasUnsavedWork(in: tab) } - /// Pushes the selected tab's answer onto the window's close button. - /// - /// The editor's text binding does this on every keystroke, which covers typing. A command that - /// changes the text without going through that binding has to say so itself, or the dot - /// describes the tab as it was before the command ran. + // For changes that bypass the editor's text binding, which pushes the dot on every keystroke itself. func refreshUnsavedIndicator() { guard let tab = tabManager.selectedTab, let window = contentWindow else { return } let showsIndicator = showsUnsavedIndicator(for: tab) @@ -156,13 +111,11 @@ extension MainContentCoordinator { changeManager.hasChanges || hasPendingDestructiveTableOps || hasSidebarEdits - || toolbarState.hasStructureChanges || toolbarState.hasCreateTablePending || tabManager.tabs.contains { hasUnsavedWork(in: $0) } } - /// Tabs resolved against live view state (cursor offset, sort column names) so a reopened tab - /// comes back where the user left it. + // Resolved against live view state (cursor, sort column names) so a reopened tab comes back where it was left. func tabsForRecoveryCapture() -> [QueryTab] { tabManager.tabs.map { enrichedForPersistence($0) } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift index 244e8e2069..3bc6fa3447 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift @@ -58,7 +58,7 @@ extension MainContentCoordinator { return } if tab.display.resultsViewMode == .structure { - structureActions?.refresh?() + activeStructureSession?.refresh() return } reloadActiveTableData(hasPendingTableOps: hasPendingTableOps, onDiscard: onDiscard) diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SQLPreview.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SQLPreview.swift index ff9cafde52..3d10617af6 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SQLPreview.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SQLPreview.swift @@ -11,7 +11,7 @@ import TableProPluginKit extension MainContentCoordinator { // MARK: - SQL Preview - /// Routes SQL preview request to the appropriate handler based on current tab mode + // A Structure view with nothing staged previews the grid edits, which is what its Save would write. func handlePreviewSQL( pendingTruncates: Set, pendingDeletes: Set, @@ -24,9 +24,10 @@ extension MainContentCoordinator { } if let tabId = tabManager.selectedTabId, userTypeEditors[tabId]?.hasChanges == true { previewUserTypeChanges(tabId: tabId) - } else if tabManager.selectedTab?.display.resultsViewMode == .structure { - // Structure view handles its own preview via direct call - structureActions?.previewSQL?() + } else if tabManager.selectedTab?.tabType == .createTable { + activeCreateTableDraft?.previewSQL(coordinator: self) + } else if let session = activeStructureSession, session.hasChanges { + session.previewChanges() } else { generatePreviewSQL( pendingTruncates: pendingTruncates, @@ -36,7 +37,6 @@ extension MainContentCoordinator { } } - /// Generate SQL preview of all pending changes with inlined parameters func generatePreviewSQL( pendingTruncates: Set, pendingDeletes: Set, @@ -50,7 +50,9 @@ extension MainContentCoordinator { ) toolbarState.previewStatements = plan.displayStatements } catch { - toolbarState.previewStatements = ["-- Error generating SQL: \(error.localizedDescription)"] + toolbarState.previewStatements = [ + String(format: String(localized: "-- Error generating SQL: %@"), error.localizedDescription) + ] } activeSheet = .sqlPreview } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Structure.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Structure.swift new file mode 100644 index 0000000000..c1eef9e90e --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Structure.swift @@ -0,0 +1,25 @@ +// +// MainContentCoordinator+Structure.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + // The selected tab's structure session while its Structure view is on screen. Commands act on it; a session + // behind the Data view keeps its staged edits for the close prompt but takes no commands. + var activeStructureSession: StructureEditingSession? { + guard let tab = tabManager.selectedTab, tab.display.resultsViewMode == .structure else { return nil } + return structureSessions[tab.id] + } + + var activeCreateTableDraft: CreateTableDraft? { + guard let tab = tabManager.selectedTab, tab.tabType == .createTable else { return nil } + return createTableDrafts[tab.id] + } + + // Whichever structure editor is on screen, for the inspector. + var activeStructureEditor: StructureDefinitionEditor? { + activeStructureSession?.editor ?? activeCreateTableDraft?.editor + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift index 876e19e03a..b2bb6f89cc 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift @@ -95,7 +95,6 @@ extension MainContentCoordinator { createTableDrafts.removeValue(forKey: tabId) erDiagramModels.removeValue(forKey: tabId) tabsWithStagedPrincipals.remove(tabId) - toolbarState.hasStructureChanges = false toolbarState.hasCreateTablePending = false toolbarState.hasPrincipalChanges = false toolbarState.hasTypeChanges = false @@ -113,7 +112,6 @@ extension MainContentCoordinator { tableMetadataCache.removeValue(forKey: tab.id) guard isSelectedTab(tab) else { return } changeManager.clearChangesAndUndoHistory() - toolbarState.hasStructureChanges = false toolbarState.hasCreateTablePending = false toolbarState.hasPrincipalChanges = false toolbarState.hasTypeChanges = false diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsRefresh.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsRefresh.swift index 66afcadb7a..b87f556999 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsRefresh.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsRefresh.swift @@ -76,8 +76,8 @@ extension MainContentCoordinator { for tab in tabs { guard let session = structureSessions[tab.id] else { continue } let isOnScreen = tab.id == selectedId && tab.display.resultsViewMode == .structure - if isOnScreen, !session.changeManager.hasChanges, let refresh = structureActions?.refresh { - refresh() + if isOnScreen, !session.hasChanges { + session.refresh() } else { session.markStructureStale() } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentView+Bindings.swift b/apple/TablePro/Views/Main/Extensions/MainContentView+Bindings.swift index 86bd465bca..3c82d37c5b 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentView+Bindings.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentView+Bindings.swift @@ -2,9 +2,6 @@ // MainContentView+Bindings.swift // TablePro // -// Extension containing computed bindings for MainContentView. -// Extracted to reduce main view complexity. -// import SwiftUI import TableProPluginKit @@ -12,7 +9,8 @@ import TableProPluginKit extension MainContentView { // MARK: - Inspector Selection - // Whose rows the active grid model's selection indexes: the data tab's, a schema grid's, or nobody's. + // Whose rows the active grid model's selection indexes: the data tab's, a schema grid's, or nobody's. A schema + // grid's selection is drawn by the structure inspector, which observes its editor itself. var gridSelectionOwner: GridSelectionOwner { GridSelectionOwner.resolve( tabType: coordinator.tabManager.selectedTab?.tabType, @@ -24,41 +22,27 @@ extension MainContentView { gridSelectionOwner == .dataGrid ? coordinator.activeGridModel : nil } - var selectedInspectorRow: InspectorRow? { - guard gridSelectionOwner == .schemaGrid, - let displayRow = coordinator.activeGridModel?.selection.affectedRows.first else { return nil } - return coordinator.inspectorRowSource?.inspectorRow(atDisplayRow: displayRow) + var structureInspectorSource: StructureInspectorSource? { + if let session = coordinator.activeStructureSession { return .table(session) } + if let draft = coordinator.activeCreateTableDraft { return .createTable(draft) } + return nil } // MARK: - Inspector Subject var hasInspectableRow: Bool { - switch gridSelectionOwner { - case .schemaGrid: return selectedInspectorRow != nil - case .dataGrid: return dataGridModel?.selection.isEmpty == false - case .none: return false - } + guard gridSelectionOwner == .dataGrid else { return false } + return dataGridModel?.selection.isEmpty == false } - // A schema grid's selection is a column definition with no position, so it is not named "Row 0 of 0". var inspectorSubject: InspectorSubject { switch gridSelectionOwner { - case .schemaGrid: - guard let row = selectedInspectorRow else { - guard let table = currentTab?.tableContext.tableName else { return .empty } - return .tableOnly(table: table) - } - let name = row.fields.first?.value ?? "" - return .columnDefinition( - column: name.isEmpty ? String(localized: "Column") : name, - table: currentTab?.tableContext.tableName - ) case .dataGrid: guard let table = qualifiedTableName else { return dataRowSubject(table: String(localized: "Result")) ?? .empty } return dataRowSubject(table: table) ?? .tableOnly(table: table) - case .none: + case .schemaGrid, .none: // A chart, a dashboard or a diagram owns no grid; the table is still the subject of its statistics. guard let table = qualifiedTableName else { return .empty } return .tableOnly(table: table) @@ -109,9 +93,6 @@ extension MainContentView { // MARK: - Sidebar Edit State var isSidebarEditable: Bool { - if gridSelectionOwner == .schemaGrid { - return selectedInspectorRow?.isEditable ?? false - } guard let model = dataGridModel, let tab = coordinator.tabManager.selectedTab, tab.tabType == .table || tab.tableContext.tableName != nil, @@ -176,7 +157,8 @@ extension MainContentView { metadataVersion: currentTab?.metadataVersion ?? -1, resultsViewMode: currentTab?.display.resultsViewMode ?? .data, inspectorRowSourceRevision: coordinator.inspectorRowSourceRevision, - gridDisplayRevision: coordinator.gridDisplayRevision + gridDisplayRevision: coordinator.gridDisplayRevision, + structureEditor: coordinator.activeStructureEditor.map { ObjectIdentifier($0) } ) } } @@ -188,6 +170,8 @@ struct InspectorTrigger: Equatable { let resultsViewMode: ResultsViewMode let inspectorRowSourceRevision: Int let gridDisplayRevision: Int + // A structure session or Create Table draft appears after its view first mounts, which moves nothing else here. + var structureEditor: ObjectIdentifier? } enum InspectorValueDisplayFormatResolver { diff --git a/apple/TablePro/Views/Main/Extensions/MainContentView+EventHandlers.swift b/apple/TablePro/Views/Main/Extensions/MainContentView+EventHandlers.swift index e7c1f83bff..2278ef0f22 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentView+EventHandlers.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentView+EventHandlers.swift @@ -147,11 +147,9 @@ extension MainContentView { func updateSidebarEditState() { // A selection change, refresh or undo rebuilds the fields, so the typed run ends here, not in another row's step. coordinator.endInspectorEditRun() + // A structure definition is edited by the structure inspector, not the row fields. switch gridSelectionOwner { - case .schemaGrid: - updateSchemaSidebarEditState() - return - case .none: + case .schemaGrid, .none: clearSidebarEditState() return case .dataGrid: @@ -307,36 +305,4 @@ extension MainContentView { trailingPaneState.inspector.editState.onFieldRemoved = nil trailingPaneState.inspector.editState.onDetachedFieldChanged = nil } - - // Edits go back to the owning grid so they land in its change manager with one undo step, like an inline edit. - private func updateSchemaSidebarEditState() { - guard let displayRow = coordinator.activeGridModel?.selection.affectedRows.first, - let row = selectedInspectorRow - else { - clearSidebarEditState() - return - } - - trailingPaneState.inspector.editState.configure(schemaFields: row.fields, displayRow: displayRow) - - guard row.isEditable else { - clearSidebarEditHandlers() - return - } - - let capturedCoordinator = coordinator - trailingPaneState.inspector.editState.onFieldExpressionChanged = nil - trailingPaneState.inspector.editState.onFieldValuesChanged = nil - trailingPaneState.inspector.editState.onFieldReverted = nil - trailingPaneState.inspector.editState.onFieldRemoved = nil - trailingPaneState.inspector.editState.onDetachedFieldChanged = nil - trailingPaneState.inspector.editState.onFieldChanged = { fieldIndex, newValue, _ in - capturedCoordinator.inspectorRowSource?.commitInspectorField( - displayRow: displayRow, - fieldIndex: fieldIndex, - value: newValue.asText - ) - capturedCoordinator.inspectorRowSourceRevision += 1 - } - } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentView+Helpers.swift b/apple/TablePro/Views/Main/Extensions/MainContentView+Helpers.swift index 5e148f871e..961b683705 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentView+Helpers.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentView+Helpers.swift @@ -2,9 +2,6 @@ // MainContentView+Helpers.swift // TablePro // -// Extension containing helper methods and inspector context -// for MainContentView. Extracted to reduce main view complexity. -// import SwiftUI import TableProPluginKit @@ -75,7 +72,7 @@ extension MainContentView { isRowDeleted: isSelectedRowDeleted, tableMetadata: tableMetadataForCurrentTab, jsonRow: jsonRowSnapshotForSidebar, - userDefinedTypeScope: structureTypeScope + structure: structureInspectorSource ) updateAssistantContext() } @@ -96,17 +93,6 @@ extension MainContentView { currentTab.flatMap { coordinator.currentTableMetadata(for: $0) } } - // The tab's own database and schema, never the sidebar's: a tab bound to another database edits that one. - private var structureTypeScope: DatabaseScope? { - guard let tab = currentTab, tab.tabType == .table || tab.tabType == .createTable else { return nil } - let database = tab.tableContext.databaseName ?? coordinator.browseDatabaseName - return DatabaseScope( - connectionId: coordinator.connection.id, - database: database, - schema: tab.tableContext.schemaName - ) - } - private func cachedQueryResultsSummary() -> String? { guard let tab = currentTab else { return nil } if let cache = queryResultsSummaryCache, diff --git a/apple/TablePro/Views/Main/Extensions/MainContentView+Setup.swift b/apple/TablePro/Views/Main/Extensions/MainContentView+Setup.swift index 84b9a6e042..9388979b8f 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentView+Setup.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentView+Setup.swift @@ -76,19 +76,32 @@ extension MainContentView { // One resolution of what is staged, so commit, its verb and Preview SQL agree; staged principals once left Cmd+S dim. func updateToolbarPendingState() { + let selectedTab = tabManager.selectedTab + let hasDataChanges = changeManager.hasChanges || !pendingTruncates.isEmpty || !pendingDeletes.isEmpty + // The session, not the mounted view: leaving Structure for Data used to dim Save over staged edits. + let hasStagedStructure = selectedStructureSession?.hasChanges ?? false + let previewsSchemaChanges = toolbarState.hasTypeChanges || coordinator.selectedDiagramHasStagedEdits + || (hasStagedStructure && selectedTab?.display.resultsViewMode == .structure) let kind = PendingChangeKind.resolve( - tabType: tabManager.selectedTab?.tabType, - hasDataChanges: changeManager.hasChanges || !pendingTruncates.isEmpty || !pendingDeletes.isEmpty, - hasStructureChanges: toolbarState.hasStructureChanges || toolbarState.hasTypeChanges + tabType: selectedTab?.tabType, + hasDataChanges: hasDataChanges, + hasStructureChanges: hasStagedStructure || toolbarState.hasTypeChanges || coordinator.selectedDiagramHasStagedEdits, hasCreateTablePending: toolbarState.hasCreateTablePending, hasPrincipalChanges: toolbarState.hasPrincipalChanges, - isFileDirty: tabManager.selectedTab?.content.isFileDirty ?? false + isFileDirty: selectedTab?.content.isFileDirty ?? false ) toolbarState.pendingChange = kind toolbarState.hasPendingChanges = kind != nil - // Narrower than the commit: a dirty query file and staged principals raise the commit but have no grid SQL to show. - toolbarState.hasDataPendingChanges = kind == .data || kind == .structure + toolbarState.hasDataPendingChanges = PendingChangeKind.offersPreview( + of: kind, + hasDataChanges: hasDataChanges, + previewsSchemaChanges: previewsSchemaChanges + ) + } + + var selectedStructureSession: StructureEditingSession? { + tabManager.selectedTabId.flatMap { coordinator.structureSessions[$0] } } // The edited dot is written directly: the unsaved work it reports stays in the window under an agent conversation. diff --git a/apple/TablePro/Views/Main/MainContentCommandActions+Structure.swift b/apple/TablePro/Views/Main/MainContentCommandActions+Structure.swift new file mode 100644 index 0000000000..d6fb86f73c --- /dev/null +++ b/apple/TablePro/Views/Main/MainContentCommandActions+Structure.swift @@ -0,0 +1,117 @@ +// +// MainContentCommandActions+Structure.swift +// TablePro +// + +import Foundation + +// Structure commands go to the selected tab's own session or Create Table draft, which hold their edits mounted or not. +extension MainContentCommandActions { + // MARK: - Save and discard + + // From Data, only when no grid edit is ahead of it: Save stays live after the user leaves Structure. + var stagedStructureSession: StructureEditingSession? { + guard let coordinator, + let tab = coordinator.tabManager.selectedTab, + let session = coordinator.structureSessions[tab.id], + session.hasChanges else { return nil } + if tab.display.resultsViewMode == .structure { return session } + return hasDataWork ? nil : session + } + + var canDiscardChanges: Bool { discardTarget != nil } + + func discardChanges() { + switch discardTarget { + case .structure(let session): + session.discardChangesAfterConfirming() + case .data: + Task { await discardDataWorkAfterConfirming() } + case nil: + break + } + } + + private enum DiscardTarget { + case structure(StructureEditingSession) + case data + } + + // Create Table has no saved state to return to. + private var discardTarget: DiscardTarget? { + guard let tab = coordinator?.tabManager.selectedTab, + tab.tabType == .table || tab.tabType == .query else { return nil } + if let session = stagedStructureSession { return .structure(session) } + return hasDataWork ? .data : nil + } + + private func discardDataWorkAfterConfirming() async { + let confirmed = await AlertHelper.confirmDestructive( + title: String(localized: "Discard Changes?"), + message: String(localized: "The changes you have not saved will be lost."), + confirmButton: String(localized: "Discard Changes"), + cancelButton: String(localized: "Cancel"), + window: closeAnchorWindow + ) + guard confirmed else { return } + discardDataWork() + } + + // MARK: - Rows + + var canAddSchemaRow: Bool { + if let session = coordinator?.activeStructureSession { return session.canAddRow } + return coordinator?.activeCreateTableDraft?.canAddRow ?? false + } + + var canRemoveSchemaRows: Bool { + if let session = coordinator?.activeStructureSession { return session.canRemoveRows } + return coordinator?.activeCreateTableDraft?.canRemoveRows ?? false + } + + var canPasteSchemaRows: Bool { + if let session = coordinator?.activeStructureSession { return session.canPasteRows } + guard let draft = coordinator?.activeCreateTableDraft, draft.canAddRow else { return false } + return draft.editor.canPaste + } + + func addSchemaRow() { + if let session = coordinator?.activeStructureSession { + session.addRow() + } else { + coordinator?.activeCreateTableDraft?.addRow() + } + } + + func removeSchemaRows() { + if let session = coordinator?.activeStructureSession { + session.removeSelectedRows() + } else { + coordinator?.activeCreateTableDraft?.removeSelectedRows() + } + } + + func duplicateSchemaRows() { + if let session = coordinator?.activeStructureSession { + session.duplicateSelectedRows() + } else if let draft = coordinator?.activeCreateTableDraft, draft.canAddRow { + draft.editor.duplicateSelection() + } + } + + func copySchemaRows() { + if let session = coordinator?.activeStructureSession { + session.copySelectedRows() + } else { + coordinator?.activeCreateTableDraft?.editor.copySelection() + } + } + + func pasteSchemaRows() { + if let session = coordinator?.activeStructureSession { + session.pasteRows() + } else if canPasteSchemaRows { + coordinator?.activeCreateTableDraft?.editor.paste() + } + } +} diff --git a/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift b/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift index 76dd50c879..f35e03c609 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift @@ -5,18 +5,37 @@ import AppKit -/// A connection hosts three undo domains, picked by the active tab: Users & Roles and -/// the structure editor keep their own histories, and everything else registers on -/// the window's `UndoManager`, which `TabWindowController` resolves to the selected -/// connection's own history. Availability and the menu title both have to follow -/// whichever one is live, so they resolve through the same branch the commands do. +// The structure editor's own history, which the window's undo manager never sees. +@MainActor +protocol StructureUndoHistory: AnyObject { + var canUndo: Bool { get } + var canRedo: Bool { get } + var undoMenuItemTitle: String { get } + var redoMenuItemTitle: String { get } + func undo() + func redo() +} + +extension StructureEditingSession: StructureUndoHistory {} + +extension CreateTableDraft: StructureUndoHistory {} + +// Users & Roles, the diagram and the structure editor keep their own histories; everything else registers on the +// window's undo manager. Availability, title and command resolve through the same branch so they cannot disagree. extension MainContentCommandActions { private var windowUndoManager: UndoManager? { coordinator?.contentWindow?.undoManager } - private var isStructureMode: Bool { - coordinator?.tabManager.selectedTab?.display.resultsViewMode == .structure + // A Create Table tab keeps its view mode at Data, so it is asked for by tab type. + private var showsStructureEditor: Bool { + guard let tab = coordinator?.tabManager.selectedTab else { return false } + return tab.tabType == .createTable || tab.display.resultsViewMode == .structure + } + + private var structureHistory: (any StructureUndoHistory)? { + if let session = coordinator?.activeStructureSession { return session } + return coordinator?.activeCreateTableDraft } // The diagram stages on one session per table, so it keeps its own order across them. @@ -31,12 +50,11 @@ extension MainContentCommandActions { coordinator?.tabManager.selectedTab?.tabType == .erDiagram } - /// A field still being typed into has an undo step waiting rather than registered, and the menu - /// has to offer it: the command ends the run before it reaches the undo manager. + // A field still being typed into has its step waiting rather than registered; the command ends the run first. var canUndo: Bool { if isUsersRolesTab { return coordinator?.usersRolesActions?.canUndo() ?? false } if isDiagramTab { return selectedDiagram?.canUndoStagedEdit ?? false } - if isStructureMode { return coordinator?.structureActions?.undo != nil } + if showsStructureEditor { return structureHistory?.canUndo ?? false } if coordinator?.changeManager.hasCoalescedUndoRun == true { return true } return windowUndoManager?.canUndo ?? false } @@ -44,13 +62,17 @@ extension MainContentCommandActions { var canRedo: Bool { if isUsersRolesTab { return coordinator?.usersRolesActions?.canRedo() ?? false } if isDiagramTab { return selectedDiagram?.canRedoStagedEdit ?? false } - if isStructureMode { return coordinator?.structureActions?.redo != nil } + if showsStructureEditor { return structureHistory?.canRedo ?? false } return windowUndoManager?.canRedo ?? false } var resolvedUndoTitle: String { if isUsersRolesTab { return undoMenuTitle } if isDiagramTab { return String(localized: "Undo") } + if showsStructureEditor { + guard let history = structureHistory, history.canUndo else { return String(localized: "Undo") } + return history.undoMenuItemTitle + } if coordinator?.changeManager.hasCoalescedUndoRun == true { return String(localized: "Undo Edit Cell") } @@ -61,61 +83,47 @@ extension MainContentCommandActions { var resolvedRedoTitle: String { if isUsersRolesTab { return redoMenuTitle } if isDiagramTab { return String(localized: "Redo") } + if showsStructureEditor { + guard let history = structureHistory, history.canRedo else { return String(localized: "Redo") } + return history.redoMenuItemTitle + } guard let manager = windowUndoManager, manager.canRedo else { return String(localized: "Redo") } return manager.redoMenuItemTitle } // MARK: - Commands - /// A Create Table tab keeps its `resultsViewMode` at `.data`, so it needs its own arm. Without - /// one, Cmd+Z in the visual table editor reached the window's undo manager, which owns none of - /// the draft, and the grid's own undo had no caller at all. func undoChange() { coordinator?.endInspectorEditRun() - endTableCommentRun() if isUsersRolesTab { coordinator?.usersRolesActions?.undo() return } - if coordinator?.tabManager.selectedTab?.tabType == .createTable { - coordinator?.createTableActions?.undo?() - return - } if isDiagramTab { selectedDiagram?.undoStagedEdit() return } - if coordinator?.tabManager.selectedTab?.display.resultsViewMode == .structure { - coordinator?.structureActions?.undo?() + if showsStructureEditor { + structureHistory?.undo() return } - coordinator?.contentWindow?.undoManager?.undo() + windowUndoManager?.undo() } func redoChange() { coordinator?.endInspectorEditRun() - endTableCommentRun() if isUsersRolesTab { coordinator?.usersRolesActions?.redo() return } - if coordinator?.tabManager.selectedTab?.tabType == .createTable { - coordinator?.createTableActions?.redo?() - return - } if isDiagramTab { selectedDiagram?.redoStagedEdit() return } - if coordinator?.tabManager.selectedTab?.display.resultsViewMode == .structure { - coordinator?.structureActions?.redo?() + if showsStructureEditor { + structureHistory?.redo() return } - coordinator?.contentWindow?.undoManager?.redo() - } - - private func endTableCommentRun() { - guard let coordinator, let tabId = coordinator.tabManager.selectedTabId else { return } - coordinator.structureSessions[tabId]?.changeManager.endTableCommentRun() + windowUndoManager?.redo() } } diff --git a/apple/TablePro/Views/Main/MainContentCommandActions.swift b/apple/TablePro/Views/Main/MainContentCommandActions.swift index a58475ef42..5a3f62be3f 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions.swift @@ -171,9 +171,8 @@ final class MainContentCommandActions: ObservableObject { // MARK: - Row Operations (Group A: Called Directly) func addNewRow() { - // Structure inserts a column, index or FK row by sub-tab; the data path adds data rows only. switch selectionOwner { - case .schemaGrid: coordinator?.structureActions?.addRow?() + case .schemaGrid: addSchemaRow() case .dataGrid: coordinator?.addNewRow() case .none: break } @@ -185,13 +184,13 @@ final class MainContentCommandActions: ObservableObject { return coordinator?.activeGridModel?.selectedRowIDs ?? [] } - // A Create Table tab has no structure handler; claiming its selection would make commands inert and shadow sidebar delete. - private var selectionOwner: GridSelectionOwner { + // A structure grid with no session or draft behind it takes no commands; claiming it would shadow sidebar delete. + var selectionOwner: GridSelectionOwner { let owner = GridSelectionOwner.resolve( tabType: coordinator?.tabManager.selectedTab?.tabType, resultsViewMode: coordinator?.tabManager.selectedTab?.display.resultsViewMode ) - guard owner == .schemaGrid, coordinator?.structureActions == nil else { return owner } + guard owner == .schemaGrid, coordinator?.activeStructureEditor == nil else { return owner } return .none } @@ -205,8 +204,9 @@ final class MainContentCommandActions: ObservableObject { } func deleteSelectedRows() { - if selectionOwner == .schemaGrid { - coordinator?.structureActions?.removeRow?() + // A selected definition that cannot be removed refuses here; Delete never reaches the sidebar's tables. + if selectsSchemaDefinition { + removeSchemaRows() return } @@ -220,13 +220,17 @@ final class MainContentCommandActions: ObservableObject { } func duplicateRow() { + if selectionOwner == .schemaGrid { + duplicateSchemaRows() + return + } guard let rowID = singleSelectedDataGridRowID else { return } coordinator?.duplicateRow(rowID) } func copySelectedRows() { switch selectionOwner { - case .schemaGrid: coordinator?.structureActions?.copyRows?() + case .schemaGrid: copySchemaRows() case .dataGrid: coordinator?.copyRowsToClipboard(selectedDataRowIDs) case .none: break } @@ -244,7 +248,7 @@ final class MainContentCommandActions: ObservableObject { func pasteRows() { switch selectionOwner { - case .schemaGrid: coordinator?.structureActions?.pasteRows?() + case .schemaGrid: pasteSchemaRows() case .dataGrid: coordinator?.pasteRows() case .none: break } @@ -375,15 +379,20 @@ final class MainContentCommandActions: ObservableObject { SharedSidebarState.forConnection(connection.id).sidebarLayout = layout } + // Not the data tab's state in Structure: Add Row there used to light from the rows' editability. var isCurrentTabEditable: Bool { - guard let coordinator, coordinator.tabManager.selectedTab != nil, selectionOwner != .none else { - return false + guard let coordinator, coordinator.tabManager.selectedTab != nil else { return false } + switch selectionOwner { + case .schemaGrid: return canAddSchemaRow || canRemoveSchemaRows + case .dataGrid: return coordinator.canEditActiveResult + case .none: return false } - return coordinator.canEditActiveResult } + // Add and Duplicate Row gate on this; a structure grid adds only where its section allows it. var isCurrentTabSchemaResolved: Bool { guard let coordinator, let tabId = coordinator.tabManager.selectedTabId else { return false } + if selectionOwner == .schemaGrid { return canAddSchemaRow } return coordinator.tabSessionRegistry.tableRows(for: tabId).hasAuthoritativeSchema } @@ -431,7 +440,7 @@ final class MainContentCommandActions: ObservableObject { } switch selectionOwner { case .schemaGrid: - return coordinator?.structureActions?.pasteRows != nil && TableStructureView.canPasteStructureRows + return canPasteSchemaRows case .dataGrid: return tab.tabType == .table && isCurrentTabEditable && isCurrentTabSchemaResolved && ClipboardService.shared.hasText @@ -480,7 +489,12 @@ final class MainContentCommandActions: ObservableObject { // Same check as the sidebar's Delete item, which the menu bar used to skip on engines with no statement for it. var canDropSelectedTables: Bool { - TableOperationEligibility.canDrop(selectedTables.wrappedValue, context: tableOperationEligibility) + guard !selectsSchemaDefinition else { return false } + return TableOperationEligibility.canDrop(selectedTables.wrappedValue, context: tableOperationEligibility) + } + + private var selectsSchemaDefinition: Bool { + selectionOwner == .schemaGrid && coordinator?.activeStructureEditor?.focusedSection != nil } private var tableOperationEligibility: TableOperationEligibility.Context { @@ -522,10 +536,6 @@ final class MainContentCommandActions: ObservableObject { coordinator?.isEditingActiveGrid == true } - var hasStructureChanges: Bool { - coordinator?.toolbarState.hasStructureChanges ?? false - } - // MARK: - Unsaved Changes Check // Every connection in the window: closing it closes them all, and skipping a background one is silent data loss. @@ -777,10 +787,7 @@ final class MainContentCommandActions: ObservableObject { guard await applySelectedTypeChanges() else { return false } // Data grid changes or pending table operations take priority - let hasDataChanges = coordinator.changeManager.hasChanges - || !pendingTruncates.wrappedValue.isEmpty - || !pendingDeletes.wrappedValue.isEmpty - if hasDataChanges { + if hasDataWork { return await withCheckedContinuation { continuation in coordinator.saveCompletionContinuation = continuation saveChanges() @@ -879,13 +886,19 @@ final class MainContentCommandActions: ObservableObject { // MARK: - Data Operations (Group A: Called Directly) - // Any Structure sub-tab saves: DDL, Parts and Triggers are read-only views, and refusing left Cmd+S silently inert. - private func applyStagedStructureChanges() { - guard let coordinator, - let tabId = coordinator.tabManager.selectedTabId, - let session = coordinator.structureSessions[tabId], - session.changeManager.hasChanges else { return } - Task { _ = await session.applyStagedChanges(coordinator: coordinator) } + var hasDataWork: Bool { + coordinator?.changeManager.hasChanges == true + || !pendingTruncates.wrappedValue.isEmpty + || !pendingDeletes.wrappedValue.isEmpty + } + + // What Save would write for the grid: cell edits and the staged TRUNCATE and DROP. + func discardDataWork() { + var truncates = pendingTruncates.wrappedValue + var deletes = pendingDeletes.wrappedValue + coordinator?.handleDiscard(pendingTruncates: &truncates, pendingDeletes: &deletes) + pendingTruncates.wrappedValue = truncates + pendingDeletes.wrappedValue = deletes } func restorePreviousValues() { @@ -899,18 +912,17 @@ final class MainContentCommandActions: ObservableObject { return } if coordinator?.tabManager.selectedTab?.tabType == .createTable { - coordinator?.createTableActions?.createTable?() + guard let coordinator, let draft = coordinator.activeCreateTableDraft else { return } + draft.create(coordinator: coordinator) return } if saveSelectedDiagram() { return } if saveSelectedTypeChanges() { return } - if coordinator?.tabManager.selectedTab?.display.resultsViewMode == .structure { - applyStagedStructureChanges() - } else if coordinator?.changeManager.hasChanges == true - || !pendingTruncates.wrappedValue.isEmpty - || !pendingDeletes.wrappedValue.isEmpty { + if let session = stagedStructureSession, let coordinator { + Task { _ = await session.applyStagedChanges(coordinator: coordinator) } + } else if hasDataWork { // Data grid first: its edits sync to the sidebar editState, and its path uses the plugin driver for statements. var truncates = pendingTruncates.wrappedValue var deletes = pendingDeletes.wrappedValue @@ -1099,9 +1111,17 @@ final class MainContentCommandActions: ObservableObject { } func runQuery() { + if let coordinator, let draft = coordinator.activeCreateTableDraft { + draft.create(coordinator: coordinator) + return + } coordinator?.runQuery(viewport: .keepPlace) } + var canCreateDraftTable: Bool { + coordinator?.activeCreateTableDraft?.isReadyToCreate ?? false + } + func runQueryWithoutLimit() { coordinator?.runQuery(viewport: .keepPlace, bypassRowLimit: true) } diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index 69ff360f71..1e4fd80455 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -191,11 +191,6 @@ final class MainContentCoordinator: ObservableObject { weak var sidebarViewModel: SidebarViewModel? - weak var structureActions: StructureViewActionHandler? - - // Lets Save Changes (Cmd+S) route to table creation. Set by `CreateTableView` on appear. - weak var createTableActions: CreateTableActionHandler? - weak var usersRolesActions: UsersRolesActionHandler? // Not in the view: it dies on every tab and Data/Structure switch, and the close gate must see unseen tabs' staged work. @@ -212,9 +207,6 @@ final class MainContentCoordinator: ObservableObject { // `saveCompletionContinuation` is one slot: a second close gesture would overwrite it and strand the first task forever. internal var tabClosesInFlight: Set = [] - // The non-data grid that owns the selection, read by the inspector. Set by `TableStructureView` and `CreateTableView`. - weak var inspectorRowSource: (any InspectorRowSource)? - // Bumped when a published schema row changes, so the inspector re-reads it. @Published var inspectorRowSourceRevision: Int = 0 diff --git a/apple/TablePro/Views/Main/MainContentView.swift b/apple/TablePro/Views/Main/MainContentView.swift index fdc7b3f76a..3de7ffcf9c 100644 --- a/apple/TablePro/Views/Main/MainContentView.swift +++ b/apple/TablePro/Views/Main/MainContentView.swift @@ -266,11 +266,7 @@ struct MainContentView: View { } ) case .sqlPreview: - SQLReviewSheet( - isPresented: dismissBinding, - statements: coordinator.toolbarState.previewStatements, - databaseType: coordinator.toolbarState.databaseType - ) + sqlPreviewSheet(dismiss: dismissBinding) case .rewind: if let plan = coordinator.rewindPlan { RewindReviewSheet(plan: plan) { @@ -306,12 +302,32 @@ struct MainContentView: View { } } + // A structure session's preview offers its Save; a grid preview only shows what Save would write. + private func sqlPreviewSheet(dismiss: Binding) -> SQLReviewSheet { + SQLReviewSheet( + isPresented: dismiss, + statements: coordinator.toolbarState.previewStatements, + databaseType: coordinator.toolbarState.databaseType, + primaryAction: coordinator.activeStructureSession.flatMap { session -> SQLReviewSheet.PrimaryAction? in + guard session.hasChanges else { return nil } + return SQLReviewSheet.PrimaryAction( + title: String(localized: "Save Changes"), + isDestructive: false, + work: .immediate { + coordinator.activeSheet = nil + session.save() + } + ) + } + ) + } + private var pendingChangeTrigger: PendingChangeTrigger { PendingChangeTrigger( hasDataChanges: changeManager.hasChanges, pendingTruncates: pendingTruncates, pendingDeletes: pendingDeletes, - hasStructureChanges: toolbarState.hasStructureChanges, + hasStructureChanges: selectedStructureSession?.hasChanges ?? false, diagramEditsRevision: toolbarState.diagramEditsRevision, selectedTabId: tabManager.selectedTabId, isFileDirty: tabManager.selectedTab?.content.isFileDirty ?? false, @@ -354,6 +370,15 @@ struct MainContentView: View { .id(ObjectIdentifier(model)) } } + .background { + if let session = selectedStructureSession { + StructureChangeWatcher(session: session) { + updateToolbarPendingState() + coordinator.refreshUnsavedIndicator() + } + .id(ObjectIdentifier(session)) + } + } .onReceive(coordinator.gridCellsEdited) { _ in scheduleInspectorUpdate() } @@ -525,3 +550,16 @@ struct MainContentView: View { ) } } + +// No view of this window observes a session whose Structure view is not on screen, yet Discard and Save change it there. +private struct StructureChangeWatcher: View { + @ObservedObject var session: StructureEditingSession + let onChange: () -> Void + + var body: some View { + Color.clear + .frame(width: 0, height: 0) + .accessibilityHidden(true) + .onChange(of: session.hasChanges) { _ in onChange() } + } +} diff --git a/apple/TablePro/Views/Results/ResultStatusBar.swift b/apple/TablePro/Views/Results/ResultStatusBar.swift index 07980fcdec..170346bdc0 100644 --- a/apple/TablePro/Views/Results/ResultStatusBar.swift +++ b/apple/TablePro/Views/Results/ResultStatusBar.swift @@ -38,7 +38,6 @@ struct ResultStatusBar: View { let columnState: StatusBarColumnState let highlightState: StatusBarHighlightState let paginationCallbacks: PaginationCallbacks - let structureFooter: StructureFooterCapability let execution: ExecutionReadout /// Held, not observed: only the readout inside the report slot re-renders when it changes. let selectionSummary: SelectionSummaryState @@ -54,8 +53,6 @@ struct ResultStatusBar: View { let onCloseOtherResultSets: (UUID) -> Void let onToggleFilters: () -> Void let onFetchAll: (() -> Void)? - let onStructureAdd: () -> Void - let onStructureRemove: () -> Void @State private var showColumnPopover = false @State private var showHighlightPopover = false @@ -301,20 +298,6 @@ struct ResultStatusBar: View { if model.controls.showsPagination { paginationControls(presentation) } - if model.controls.showsStructureActions { - AddRemoveControlGroup( - addLabel: structureFooter.addLabel, - removeLabel: structureFooter.removeLabel, - canAdd: structureFooter.canAdd, - canRemove: structureFooter.canRemove, - addHelp: structureFooter.unavailableReason, - removeHelp: structureFooter.unavailableReason, - addIdentifier: "structure-footer-add", - removeIdentifier: "structure-footer-remove", - onAdd: onStructureAdd, - onRemove: onStructureRemove - ) - } } .fixedSize() } diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/FieldEditorContent.swift b/apple/TablePro/Views/RowInspector/FieldEditors/FieldEditorContent.swift index 0950b8e615..b8094dbc0c 100644 --- a/apple/TablePro/Views/RowInspector/FieldEditors/FieldEditorContent.swift +++ b/apple/TablePro/Views/RowInspector/FieldEditors/FieldEditorContent.swift @@ -10,7 +10,6 @@ import SwiftUI internal struct FieldEditorContent: View { internal let context: FieldEditorContext internal let kind: FieldEditorKind - internal let databaseType: DatabaseType internal var onSetNull: (() -> Void)? internal var onSetDefault: (() -> Void)? internal var onPopOut: ((String) -> Void)? @@ -29,8 +28,8 @@ internal struct FieldEditorContent: View { /// take away the control that changes it back. private var isPicker: Bool { switch kind { - case .boolean, .enumPicker, .setPicker, .arrayElements, .vector, .typePicker, .valuePicker: return true - case .json, .phpSerialized, .binaryValue, .image, .geometry, .blobHex, .schemaText, .multiLine, .singleLine: + case .boolean, .enumPicker, .setPicker, .arrayElements, .vector: return true + case .json, .phpSerialized, .binaryValue, .image, .geometry, .blobHex, .multiLine, .singleLine: return false } } @@ -42,8 +41,7 @@ internal struct FieldEditorContent: View { switch kind { case .json, .phpSerialized, .binaryValue, .multiLine: return true case .geometry(let descriptor): return canExpand(kind: descriptor.textEditorKind, state: state) - case .image, .blobHex, .boolean, .enumPicker, .setPicker, .arrayElements, .vector, .typePicker, - .valuePicker, .schemaText, .singleLine: + case .image, .blobHex, .boolean, .enumPicker, .setPicker, .arrayElements, .vector, .singleLine: return false } } @@ -57,8 +55,7 @@ internal struct FieldEditorContent: View { case .blobHex: return 60 case .multiLine: return ResizableFieldMetrics.defaultTextHeight case .geometry(let descriptor): return pillHeight(for: descriptor.textEditorKind) - case .boolean, .enumPicker, .setPicker, .arrayElements, .vector, .typePicker, .valuePicker, - .schemaText, .singleLine: + case .boolean, .enumPicker, .setPicker, .arrayElements, .vector, .singleLine: return nil } } @@ -100,12 +97,6 @@ internal struct FieldEditorContent: View { ) case .vector: VectorFieldEditorView(context: context, onSetNull: onSetNull, onSetDefault: onSetDefault) - case .typePicker: - TypePickerFieldView(context: context, databaseType: databaseType) - case .valuePicker(let options): - ValuePickerFieldView(context: context, options: options) - case .schemaText: - SchemaTextFieldView(context: context) case .multiLine: MultiLineEditorView(context: context, onPopOut: onPopOut, isExpanded: isExpanded) case .singleLine: diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/FieldEditorContext.swift b/apple/TablePro/Views/RowInspector/FieldEditors/FieldEditorContext.swift index 2fdf5ea681..46174a22b3 100644 --- a/apple/TablePro/Views/RowInspector/FieldEditors/FieldEditorContext.swift +++ b/apple/TablePro/Views/RowInspector/FieldEditors/FieldEditorContext.swift @@ -23,12 +23,8 @@ internal struct FieldEditorContext { /// column type and the value only when this is nil, and resolving costs a parse of the value. internal let editor: FieldEditorKind? - /// A schema field has no NULL or DEFAULT state and no data type to badge. + // False where the store has no NULL or DEFAULT to write, as in a data file. internal let allowsNullAndDefault: Bool - internal let showsTypeBadge: Bool - - /// The table a column type picker offers user-defined types for. Nil outside a structure row. - internal let userDefinedTypeScope: DatabaseScope? internal init( columnName: String, @@ -40,9 +36,7 @@ internal struct FieldEditorContext { isReadOnly: Bool, commitBytes: ((Data) -> Void)? = nil, editor: FieldEditorKind? = nil, - allowsNullAndDefault: Bool = true, - showsTypeBadge: Bool = true, - userDefinedTypeScope: DatabaseScope? = nil + allowsNullAndDefault: Bool = true ) { self.columnName = columnName self.columnType = columnType @@ -54,8 +48,6 @@ internal struct FieldEditorContext { self.commitBytes = commitBytes self.editor = editor self.allowsNullAndDefault = allowsNullAndDefault - self.showsTypeBadge = showsTypeBadge - self.userDefinedTypeScope = userDefinedTypeScope } internal var hasMultipleValues: Bool { valueState == .multipleValues } diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/SchemaTextFieldView.swift b/apple/TablePro/Views/RowInspector/FieldEditors/SchemaTextFieldView.swift deleted file mode 100644 index 130bac1db9..0000000000 --- a/apple/TablePro/Views/RowInspector/FieldEditors/SchemaTextFieldView.swift +++ /dev/null @@ -1,49 +0,0 @@ -// -// SchemaTextFieldView.swift -// TablePro -// - -import SwiftUI - -/// Text editor for a schema field. Commits on Return or when focus leaves, the way -/// the structure grid's cell editor does, so one edit records one schema change -/// instead of one per keystroke. -internal struct SchemaTextFieldView: View { - let context: FieldEditorContext - - @State private var draft: String = "" - @State private var hasSeededDraft = false - @FocusState private var isFocused: Bool - - var body: some View { - TextField(context.placeholderText, text: $draft) - .textFieldStyle(.roundedBorder) - .autocorrectionDisabled(true) - .focused($isFocused) - .disabled(context.isReadOnly) - .onAppear(perform: seedDraftOnFirstAppearance) - .onChange(of: context.value.wrappedValue) { newValue in - guard !isFocused else { return } - draft = newValue - } - .onChange(of: isFocused) { focused in - guard !focused else { return } - commit() - } - .onSubmit { commit() } - } - - /// A connection switch or a trailing-pane swap removes the field and re-adds it with the draft - /// intact. Reseeding there overwrote typing not yet committed, and the blur that follows then - /// found nothing to commit. A real change of the stored value still arrives through `onChange`. - private func seedDraftOnFirstAppearance() { - guard !hasSeededDraft else { return } - hasSeededDraft = true - draft = context.value.wrappedValue - } - - private func commit() { - guard draft != context.value.wrappedValue else { return } - context.value.wrappedValue = draft - } -} diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/TypePickerFieldView.swift b/apple/TablePro/Views/RowInspector/FieldEditors/TypePickerFieldView.swift deleted file mode 100644 index 0fac5c11eb..0000000000 --- a/apple/TablePro/Views/RowInspector/FieldEditors/TypePickerFieldView.swift +++ /dev/null @@ -1,44 +0,0 @@ -// -// TypePickerFieldView.swift -// TablePro -// - -import SwiftUI - -/// Column type editor for a schema field: free-form text for lengths and precision, -/// plus the same searchable type list the structure grid opens from its cell chevron. -internal struct TypePickerFieldView: View { - let context: FieldEditorContext - let databaseType: DatabaseType - - @State private var isPickerPresented = false - - var body: some View { - HStack(spacing: 4) { - SchemaTextFieldView(context: context) - - Button { - isPickerPresented = true - } label: { - Image(systemName: "chevron.down") - .font(.caption) - .foregroundStyle(.secondary) - } - .buttonStyle(.borderless) - .disabled(context.isReadOnly) - .help(String(localized: "Choose Type")) - .accessibilityLabel(String(localized: "Choose Type")) - .popover(isPresented: $isPickerPresented) { - UserDefinedTypeAwarePicker(scope: context.userDefinedTypeScope) { userDefinedTypes in - TypePickerContentView( - databaseType: databaseType, - currentValue: context.value.wrappedValue, - userDefinedTypes: userDefinedTypes, - onCommit: { context.value.wrappedValue = $0 }, - onDismiss: { isPickerPresented = false } - ) - } - } - } - } -} diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/ValuePickerFieldView.swift b/apple/TablePro/Views/RowInspector/FieldEditors/ValuePickerFieldView.swift deleted file mode 100644 index e75f110ec1..0000000000 --- a/apple/TablePro/Views/RowInspector/FieldEditors/ValuePickerFieldView.swift +++ /dev/null @@ -1,101 +0,0 @@ -// -// ValuePickerFieldView.swift -// TablePro -// - -import SwiftUI - -/// A field with suggestions the user may ignore: the same menu the structure grid opens from its -/// cell chevron, over the same free-form text field every other schema field uses. -/// -/// `EnumPickerView` cannot serve here. Its list is the whole set of legal values, so it has no way to -/// hold a value that is not in it, and a column default is an open-ended SQL expression. -internal struct ValuePickerFieldView: View { - internal let context: FieldEditorContext - internal let options: [GridMenuOption] - - @State private var isCustomPresented = false - - var body: some View { - HStack(spacing: 4) { - SchemaTextFieldView(context: context) - - Menu { - ForEach(Array(options.enumerated()), id: \.offset) { _, option in - menuEntry(option) - } - } label: { - Label { Text("Choose Value") } icon: { EmptyView() } - } - .labelStyle(.iconOnly) - .menuStyle(.button) - .buttonStyle(.borderless) - .menuIndicator(.visible) - .fixedSize() - .disabled(context.isReadOnly) - /// The tooltip only. The name already comes from the label's title, and an accessibility - /// label modifier on a `Menu` replaces that name with nothing rather than adding one. - .help(String(localized: "Choose Value")) - /// A SwiftUI `Menu` is an `NSPopUpButton` on macOS, so it publishes `menuButton` or - /// `popUpButton` and never `button`, and no suite has ever resolved one by its label. - /// The identifier is the hook `InspectorFieldRow`'s own value menu already carries, and - /// unlike the label it does not move with the display language. - .accessibilityIdentifier("column-default-menu") - .popover(isPresented: $isCustomPresented) { - CustomValueContentView( - initialValue: context.value.wrappedValue, - escapeStringLiteral: escapeStringLiteral, - stringLiteralPrefix: stringLiteralPrefix, - onCommit: { context.value.wrappedValue = $0 }, - onDismiss: { isCustomPresented = false } - ) - } - } - } - - @ViewBuilder - private func menuEntry(_ option: GridMenuOption) -> some View { - switch option { - case .sectionHeader(let title): - Divider() - Text(title) - case .value(let title, let sql): - Button { - context.value.wrappedValue = sql - } label: { - if sql == context.value.wrappedValue { - Label(title, systemImage: "checkmark") - } else { - Text(title) - } - } - case .clear(let title): - Button(title) { context.value.wrappedValue = "" } - case .custom(let title): - Divider() - Button(title) { isCustomPresented = true } - } - } - - /// The connected driver's own escaping, the same seam the grid's chevron menu uses, so the two - /// surfaces cannot escape one value two ways. It falls back to the shared helper only where the - /// field has no connection behind it, which is every field outside a structure row. - private var escapeStringLiteral: (String) -> String { - guard let connectionId = context.userDefinedTypeScope?.connectionId, - let driver = DatabaseManager.shared.driver(for: connectionId) else { - return SQLEscaping.escapeStringLiteral - } - return driver.escapeStringLiteral - } - - /// From the same connection the escaping comes from, so a default typed here is written the - /// way the engine reads it back. No connection means no prefix, which is what every engine but - /// SQL Server wants anyway. - private var stringLiteralPrefix: String { - guard let connectionId = context.userDefinedTypeScope?.connectionId, - let driver = DatabaseManager.shared.driver(for: connectionId) else { - return "" - } - return SQLStringLiteralPrefix.forDatabaseType(driver.connection.type) - } -} diff --git a/apple/TablePro/Views/RowInspector/InspectorFieldListView.swift b/apple/TablePro/Views/RowInspector/InspectorFieldListView.swift index fcee8688a1..8328ab0a09 100644 --- a/apple/TablePro/Views/RowInspector/InspectorFieldListView.swift +++ b/apple/TablePro/Views/RowInspector/InspectorFieldListView.swift @@ -17,7 +17,6 @@ internal struct InspectorFieldListView: View { @ObservedObject internal var editState: MultiRowEditState internal let isEditable: Bool internal let databaseType: DatabaseType - internal let userDefinedTypeScope: DatabaseScope? internal var offersDatabaseValues = true private var offersFieldRemoval: Bool { @@ -93,14 +92,12 @@ internal struct InspectorFieldListView: View { case .phpSerialized, .binaryValue: return false case .json, .image, .geometry, .blobHex, .boolean, .enumPicker, .setPicker, .arrayElements, .vector, - .typePicker, .valuePicker, .schemaText, .multiLine, .singleLine: + .multiLine, .singleLine: return true } } - /// A structure row's edits are recorded by its own grid, so `hasEdits` stays false while - /// `hasCommittedEdit` is true. Gating on pending edits alone left the filter permanently - /// unusable on exactly the rows it lists as modified. + // Rows that disagree carry an edit made elsewhere as `hasCommittedEdit`, with no pending value. private var hasAnyModification: Bool { editState.fields.contains { $0.hasEdit || $0.hasCommittedEdit } } @@ -156,19 +153,15 @@ internal struct InspectorFieldListView: View { let commands = editState.setValueCommands(for: field, isWritable: editable && offersDatabaseValues) InspectorFieldRow( context: context(for: field, kind: kind, isEditable: editable), - layout: InspectorFieldLayout.resolve(for: kind, isSchemaField: field.isSchemaField), kind: kind, isModified: field.hasEdit || field.hasCommittedEdit, isPrimaryKey: field.isPrimaryKey, isForeignKey: field.isForeignKey, - databaseType: databaseType, isExpanded: expandedFieldID == field.id, commands: commands, isPendingExpression: field.isPendingExpression, onSetValue: { editState.setValue($0, at: field.columnIndex) }, - onRemoveField: offersFieldRemoval && !field.isSchemaField - ? { editState.removeField(at: field.columnIndex) } - : nil, + onRemoveField: offersFieldRemoval ? { editState.removeField(at: field.columnIndex) } : nil, onToggleExpand: FieldEditorContent.canExpand(kind: kind, state: FieldValueState.resolve(field)) ? { expandedFieldID = expandedFieldID == field.id ? nil : field.id } : nil, @@ -208,9 +201,7 @@ internal struct InspectorFieldListView: View { ? { editState.setFieldToBytes(at: field.columnIndex, data: $0) } : nil, editor: kind, - allowsNullAndDefault: offersDatabaseValues && !field.isSchemaField, - showsTypeBadge: !field.isSchemaField, - userDefinedTypeScope: field.isSchemaField ? userDefinedTypeScope : nil + allowsNullAndDefault: offersDatabaseValues ) } @@ -251,7 +242,6 @@ internal struct InspectorFieldListView: View { offersDatabaseValues, let focusedField, let field = editState.fields.first(where: { $0.id == focusedField }), - !field.isSchemaField, Self.isFieldEditable(field, kind: FieldEditorResolver.resolve(field: field), rowIsEditable: isEditable), let command = Self.stateShortcutCommand( for: key, offered: editState.setValueCommands(for: field, isWritable: true) diff --git a/apple/TablePro/Views/RowInspector/InspectorFieldRow.swift b/apple/TablePro/Views/RowInspector/InspectorFieldRow.swift index c9c57b6e31..20ca42288f 100644 --- a/apple/TablePro/Views/RowInspector/InspectorFieldRow.swift +++ b/apple/TablePro/Views/RowInspector/InspectorFieldRow.swift @@ -10,12 +10,10 @@ import SwiftUI internal struct InspectorFieldRow: View { @ObservedObject private var themeEngine = ThemeEngine.shared internal let context: FieldEditorContext - internal let layout: InspectorFieldLayout internal let kind: FieldEditorKind internal let isModified: Bool internal let isPrimaryKey: Bool internal let isForeignKey: Bool - internal let databaseType: DatabaseType internal let isExpanded: Bool internal let commands: [SetValueCommand] internal var isPendingExpression = false @@ -29,51 +27,24 @@ internal struct InspectorFieldRow: View { internal let fieldID: UUID var body: some View { - Group { - switch layout { - case .inline: inlineRow - case .stacked: stackedRow - } - } - .contentShape(Rectangle()) - .contextMenu { menuContent } - .accessibilityElement(children: .contain) - .accessibilityLabel(accessibilityLabel) + row + .contentShape(Rectangle()) + .contextMenu { menuContent } + .accessibilityElement(children: .contain) + .accessibilityLabel(accessibilityLabel) } - // MARK: - Layouts - - /// A schema row: the label in a fixed trailing-aligned lane, the editor filling what is left. - /// The lane is a constant because the label vocabulary is closed and authored; its widest member - /// renders at 66.4pt, so 96 carries every one of them with room for a longer translation. - private var inlineRow: some View { - HStack(alignment: .firstTextBaseline, spacing: 6) { - HStack(spacing: 3) { - statusGlyphs - fieldLabel - } - .frame(width: Self.schemaLabelLaneWidth, alignment: .trailing) - editor - .frame(maxWidth: .infinity, alignment: .leading) - modifiedGlyph - valueMenu - } - .padding(.vertical, InspectorMetrics.betweenFields / 2) - } + // MARK: - Layout - /// A data row: name and type on their own line, the value at full width beneath. Every value in - /// the pane then starts at the same x and gets the whole content width, against the 24 to 31pt a - /// long column name used to leave it. - private var stackedRow: some View { + // Always stacked: a column name has no length bound, so a label lane beside the value truncated both. + private var row: some View { VStack(alignment: .leading, spacing: InspectorMetrics.labelToValue) { HStack(spacing: 4) { statusGlyphs fieldLabel Spacer(minLength: 4) modifiedGlyph - if context.showsTypeBadge { - TypeBadge(context.columnType.badgeLabel) - } + TypeBadge(context.columnType.badgeLabel) if let onToggleExpand { Button( isExpanded ? String(localized: "Collapse") : String(localized: "Expand"), @@ -146,7 +117,7 @@ internal struct InspectorFieldRow: View { var parts = [context.columnName] if isPrimaryKey { parts.append(String(localized: "Primary key")) } if isForeignKey { parts.append(String(localized: "Foreign key")) } - if context.showsTypeBadge { parts.append(context.columnType.badgeLabel) } + parts.append(context.columnType.badgeLabel) if isModified { parts.append(String(localized: "Modified")) } return parts.joined(separator: ", ") } @@ -198,7 +169,6 @@ internal struct InspectorFieldRow: View { FieldEditorContent( context: context, kind: kind, - databaseType: databaseType, onSetNull: context.canMutate && commands.contains(.null) ? { onSetValue(.null) } : nil, onSetDefault: context.canMutate && commands.contains(.defaultValue) ? { onSetValue(.defaultValue) } : nil, onPopOut: onPopOut, @@ -218,20 +188,8 @@ internal struct InspectorFieldRow: View { return nil case .geometry: return nil - case .blobHex, .boolean, .enumPicker, .setPicker, .arrayElements, .vector, .typePicker, .valuePicker, - .schemaText, .multiLine, .singleLine: + case .blobHex, .boolean, .enumPicker, .setPicker, .arrayElements, .vector, .multiLine, .singleLine: return ThemeEngine.shared.valueFontSwiftUI } } - - - /// The lane a schema row's label sits in. The vocabulary is closed and authored, and its widest - /// member, "Ref Columns", renders at 66.4pt in `.subheadline`, so this carries every label with - /// 45% left over for a longer translation. A label that still overruns truncates in the middle - /// and keeps its tooltip rather than taking the width from its own value. - /// - /// It is a constant, not a measurement. A lane derived from the rows on screen would change - /// width as the user scrolls, because a `PreferenceKey` max-reduce inside a `List` only ever - /// sees the realized rows. - private static let schemaLabelLaneWidth: CGFloat = 96 } diff --git a/apple/TablePro/Views/RowInspector/RowInspectorView.swift b/apple/TablePro/Views/RowInspector/RowInspectorView.swift index a9a534f818..03a8555d7f 100644 --- a/apple/TablePro/Views/RowInspector/RowInspectorView.swift +++ b/apple/TablePro/Views/RowInspector/RowInspectorView.swift @@ -5,12 +5,9 @@ import SwiftUI -/// The window's inspector: the currently selected row, in one of two renderings. -/// -/// It replaces a view that carried four modes plus two full-panel takeovers, each takeover drawing -/// its own back button over the rest of the row. There is no navigation here: a structured value -/// grows in place and the pop-out windows take anything larger, so the fields around it never go -/// away. +// The window's inspector: the selected row in one of two renderings, or a structure definition's form while a +// Structure view or a Create Table tab is on screen. No navigation: a structured value grows in place and the +// pop-out windows take anything larger. internal struct RowInspectorView: View { @ObservedObject private var state: RowInspectorState private let paneState: TrailingPaneState @@ -42,8 +39,12 @@ internal struct RowInspectorView: View { ) { section in menuSection(section) } - InspectorSubjectView(subject: context.subject) - content + if let structure = context.structure { + StructureInspectorView(source: structure, tableMetadata: context.tableMetadata) + } else { + InspectorSubjectView(subject: context.subject) + content + } } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) } @@ -60,9 +61,7 @@ internal struct RowInspectorView: View { } } - /// Both renderings are views of the same selection, which is the case Apple's inspector guidance - /// covers. The header offers this only while `offeredRendering` has a value, and there the stored - /// mode is the one drawn, so the item it checks is the one on screen. + // Offered only while `offeredRendering` has a value, so the item it checks is the rendering on screen. private var renderingPicker: some View { Picker(String(localized: "Inspector View"), selection: $state.viewMode) { ForEach(InspectorViewMode.allCases, id: \.self) { mode in @@ -73,22 +72,14 @@ internal struct RowInspectorView: View { .labelsHidden() } - /// The rendering on screen, for a selection that can be drawn both ways. A schema grid's - /// selection is a column definition with no types and no foreign keys to follow, and a pane with - /// no row draws table info or nothing, so neither offers a choice: the stored mode would be - /// checked there over a pane drawing something else. + // Only a data row can be drawn both ways; a structure definition or a pane with no row offers no choice. private var offeredRendering: InspectorViewMode? { - guard context.hasRow, context.jsonRow != nil else { return nil } + guard context.structure == nil, context.hasRow, context.jsonRow != nil else { return nil } return showsFields ? .fields : .json } - /// The field list stays mounted and is hidden rather than rebuilt. - /// - /// It owns its search term, its edited-only setting, which field is expanded, which field has - /// focus and the `List`'s scroll position, and none of that survives leaving the hierarchy. - /// Rebuilding it also rebuilds a row per column, which is what scales on a wide table. The JSON - /// rendering is cheap to rebuild and keeps its own reader state in `jsonViewModel`, so it stays - /// conditional. + // The field list stays mounted and hidden: its search, expansion, focus and scroll position would not survive + // a rebuild, and a rebuild makes a row per column. The JSON rendering keeps its state in `jsonViewModel`. @ViewBuilder private var content: some View { if context.hasRow { @@ -96,10 +87,8 @@ internal struct RowInspectorView: View { fieldsMode .opacity(showsFields ? 1 : 0) .allowsHitTesting(showsFields) - /// Neither opacity nor hit-testing stops the keyboard. Without this a field - /// still holding first responder when the mode changed goes on accepting text - /// and the list's Ctrl-Option-N and Ctrl-Option-D behind the JSON view, and - /// each of those stages a real cell change that Save later commits unseen. + // Neither opacity nor hit-testing stops the keyboard: a focused field behind the JSON view + // would go on staging cell changes that Save commits unseen. .disabled(!showsFields) .accessibilityHidden(!showsFields) @@ -114,8 +103,6 @@ internal struct RowInspectorView: View { } } - /// The JSON rendering only exists for a data-grid row, because a schema grid's selection is a - /// column definition with no types and no foreign keys to follow. private var showsFields: Bool { state.viewMode == .fields || context.jsonRow == nil } @@ -125,7 +112,6 @@ internal struct RowInspectorView: View { editState: state.editState, isEditable: context.isEditable && !context.isRowDeleted, databaseType: connection.type, - userDefinedTypeScope: context.userDefinedTypeScope, onPopOut: popOut ) } @@ -140,9 +126,7 @@ internal struct RowInspectorView: View { ) } - /// The inspector's own glyph rather than `sidebar.right`, which is the pane and which the pane's - /// not-connected state drew too: a row not being selected and a connection being down read the - /// same. + // The inspector's own glyph, not `sidebar.right`, which the not-connected state draws. private var emptyState: some View { UnavailableStateView( String(localized: "No Row Selected"), @@ -152,12 +136,9 @@ internal struct RowInspectorView: View { .frame(maxWidth: .infinity, maxHeight: .infinity) } - /// The pop-out windows are the escape for a value too large for the pane, and the only escape: - /// no competitor ships an in-panel takeover and neither does this any more. private func popOut(field: FieldEditState, text: String, kind: FieldEditorKind) { let isEditable = context.isEditable && !context.isRowDeleted && !field.isServerOwned - /// Captured, not looked up per keystroke: the field's id is reissued on every selection - /// change, and a window left open over a new selection dropped what was typed into it. + // Captured, not looked up per keystroke: the field's id is reissued on every selection change. let columnIndex = field.columnIndex let rowIDs = state.editState.rowIDs let commit: ((String) -> Void)? = isEditable @@ -189,8 +170,7 @@ internal struct RowInspectorView: View { } } -/// The window a field's text opens in. A geometry field's map opens its own window from the field; -/// what reaches here is its Text segment, which goes where that editor's kind would. +// A geometry field's map opens its own window; its Text segment goes where that editor's kind would. internal enum InspectorPopOutWindow: Equatable { case json case php @@ -207,19 +187,14 @@ internal enum InspectorPopOutWindow: Equatable { return .binary(format) case .geometry(let descriptor): return resolve(for: descriptor.textEditorKind) - case .multiLine, .singleLine, .schemaText, .blobHex, .image, .boolean, - .enumPicker, .setPicker, .arrayElements, .vector, .typePicker, .valuePicker: + case .multiLine, .singleLine, .blobHex, .image, .boolean, .enumPicker, .setPicker, .arrayElements, .vector: return .text } } } -/// The JSON rendering's own commands, in the pane header's menu while that rendering is on screen. -/// -/// They were an ellipsis of their own at the end of the JSON filter field, which put two ellipsis -/// menus one above the other in the same column once the pane's header gained one. Its own view so -/// it observes the reader's model: the inspector does not, and a menu built from it would go on -/// showing Always Expand Foreign Keys in the state it had when the pane last redrew. +// The JSON rendering's commands in the pane header's menu. Its own view so it observes the reader's model, which +// the inspector does not. private struct JSONReadingCommands: View { @ObservedObject var viewModel: JSONRowInspectorViewModel diff --git a/apple/TablePro/Views/RowInspector/TableInfoView.swift b/apple/TablePro/Views/RowInspector/TableInfoView.swift index 5baf7bf061..1927087b00 100644 --- a/apple/TablePro/Views/RowInspector/TableInfoView.swift +++ b/apple/TablePro/Views/RowInspector/TableInfoView.swift @@ -5,79 +5,83 @@ import SwiftUI -/// The table's own statistics, shown when the inspector has a table but no row selected. -/// -/// Not a `Form`. `.formStyle(.grouped)` insets its content 30pt per side and there is no way to ask -/// it for less: measured, that 30 holds at 250, 270, 300, 340, 420 and 600pt, so it is not a margin -/// that gives way on a narrow pane. At the inspector's 270pt minimum it left a 116pt value column, -/// which truncated the collation on every engine whose collation has a name (`utf8mb4_0900_ai_ci` -/// needs 126pt, `SQL_Latin1_General_CP1_CI_AS` needs 196), and the previous author reached for a -/// `.help` tooltip on that row rather than the width. It also disagreed with the field list beside -/// it: selecting a row moved every value 20pt sideways. -/// -/// So the sections are drawn here, on `InspectorMetrics.horizontalInset` like everything else in -/// the pane, and a value that still does not fit wraps rather than eliding. Wrapping is what -/// Finder's Get Info does with a long value at this width, measured. +// The table's own statistics, when the inspector has a table but no row selected. Not a grouped `Form`: its 30pt +// insets hold at every pane width and left a 116pt value column at 270pt, which truncated collation names. internal struct TableInfoView: View { - @ObservedObject private var settingsManager = AppSettingsManager.shared internal let metadata: TableMetadata var body: some View { ScrollView { - VStack(alignment: .leading, spacing: 14) { - if settingsManager.general.showObjectComments, - let comment = metadata.comment, !comment.isEmpty { - section(String(localized: "Comment")) { - Text(comment) - .font(.subheadline) - .fixedSize(horizontal: false, vertical: true) - .textSelection(.enabled) - .frame(maxWidth: .infinity, alignment: .leading) - } - } + TableInfoSections(metadata: metadata) + .padding(.horizontal, InspectorMetrics.horizontalInset) + .padding(.vertical, 12) + .frame(maxWidth: .infinity, alignment: .leading) + } + } +} + +// The statistics alone, for a pane that scrolls them with its own content. +internal struct TableInfoSections: View { + @ObservedObject private var settingsManager = AppSettingsManager.shared + internal let metadata: TableMetadata + internal var includesComment = true + + internal init(metadata: TableMetadata, includesComment: Bool = true) { + self.metadata = metadata + self.includesComment = includesComment + } - section(String(localized: "Size")) { - row(String(localized: "Data"), TableMetadata.formatSize(metadata.dataSize)) - row(String(localized: "Indexes"), TableMetadata.formatSize(metadata.indexSize)) - row(String(localized: "Total"), TableMetadata.formatSize(metadata.totalSize)) + var body: some View { + VStack(alignment: .leading, spacing: 14) { + if includesComment, settingsManager.general.showObjectComments, + let comment = metadata.comment, !comment.isEmpty { + section(String(localized: "Comment")) { + Text(comment) + .font(.subheadline) + .fixedSize(horizontal: false, vertical: true) + .textSelection(.enabled) + .frame(maxWidth: .infinity, alignment: .leading) } + } + + section(String(localized: "Size")) { + row(String(localized: "Data"), TableMetadata.formatSize(metadata.dataSize)) + row(String(localized: "Indexes"), TableMetadata.formatSize(metadata.indexSize)) + row(String(localized: "Total"), TableMetadata.formatSize(metadata.totalSize)) + } - if metadata.rowCount != nil || metadata.avgRowLength != nil { - section(String(localized: "Statistics")) { - if let rows = metadata.rowCount { - row(String(localized: "Rows"), rows.formatted(.number)) - } - if let averageLength = metadata.avgRowLength { - row(String(localized: "Average Row"), TableMetadata.formatSize(averageLength)) - } + if metadata.rowCount != nil || metadata.avgRowLength != nil { + section(String(localized: "Statistics")) { + if let rows = metadata.rowCount { + row(String(localized: "Rows"), rows.formatted(.number)) + } + if let averageLength = metadata.avgRowLength { + row(String(localized: "Average Row"), TableMetadata.formatSize(averageLength)) } } + } - if metadata.engine != nil || metadata.collation != nil { - section(String(localized: "Metadata")) { - if let engine = metadata.engine { - row(String(localized: "Engine"), engine) - } - if let collation = metadata.collation { - row(String(localized: "Collation"), collation) - } + if metadata.engine != nil || metadata.collation != nil { + section(String(localized: "Metadata")) { + if let engine = metadata.engine { + row(String(localized: "Engine"), engine) + } + if let collation = metadata.collation { + row(String(localized: "Collation"), collation) } } + } - if metadata.createTime != nil || metadata.updateTime != nil { - section(String(localized: "Timestamps")) { - if let created = metadata.createTime { - row(String(localized: "Created"), Self.date(created)) - } - if let updated = metadata.updateTime { - row(String(localized: "Updated"), Self.date(updated)) - } + if metadata.createTime != nil || metadata.updateTime != nil { + section(String(localized: "Timestamps")) { + if let created = metadata.createTime { + row(String(localized: "Created"), Self.date(created)) + } + if let updated = metadata.updateTime { + row(String(localized: "Updated"), Self.date(updated)) } } } - .padding(.horizontal, InspectorMetrics.horizontalInset) - .padding(.vertical, 12) - .frame(maxWidth: .infinity, alignment: .leading) } .accessibilityElement(children: .contain) .accessibilityIdentifier("table-info") @@ -93,9 +97,7 @@ internal struct TableInfoView: View { } } - /// The label takes what it needs and the value takes the rest, wrapping when it has to. A fixed - /// label lane would be wrong here for the same reason it is wrong on a data field: these labels - /// are localized, so their widest member is not knowable from the English. + // The label takes what it needs and the value the rest, wrapping: labels are localized, so no fixed lane fits. private func row(_ label: String, _ value: String) -> some View { HStack(alignment: .firstTextBaseline, spacing: 8) { Text(label) diff --git a/apple/TablePro/Views/Shared/FieldEditors/FieldEditorResolver.swift b/apple/TablePro/Views/Shared/FieldEditors/FieldEditorResolver.swift index 7e31ec969b..2976e16b9f 100644 --- a/apple/TablePro/Views/Shared/FieldEditors/FieldEditorResolver.swift +++ b/apple/TablePro/Views/Shared/FieldEditors/FieldEditorResolver.swift @@ -12,7 +12,6 @@ internal enum FieldEditorResolver { /// Answers from the field's own resolution when it has one. Only a field built before /// `MultiRowEditState` filled it in pays the detectors here. static func resolve(field: FieldEditState) -> FieldEditorKind { - if let editor = field.editor { return editor } if let resolved = field.resolvedEditor { return resolved } return resolve( for: field.columnTypeEnum, diff --git a/apple/TablePro/Views/Structure/ClickHousePartsView.swift b/apple/TablePro/Views/Structure/ClickHousePartsView.swift index 0e9e2e5f2e..6f37691102 100644 --- a/apple/TablePro/Views/Structure/ClickHousePartsView.swift +++ b/apple/TablePro/Views/Structure/ClickHousePartsView.swift @@ -21,7 +21,7 @@ struct ClickHousePartsView: View { @State private var isLoading = true @State private var errorMessage: String? @State private var loadedToken: Int? - @StateObject private var grid = StructureListGridModel(accessibilityLabel: StructureTab.parts.displayName) + @StateObject private var grid = StructureListGridModel(accessibilityLabel: StructurePane.parts.displayName) @StateObject private var selection = StructureListSelection() var body: some View { diff --git a/apple/TablePro/Views/Structure/CreateTableActionHandler.swift b/apple/TablePro/Views/Structure/CreateTableActionHandler.swift deleted file mode 100644 index 9cc91dd862..0000000000 --- a/apple/TablePro/Views/Structure/CreateTableActionHandler.swift +++ /dev/null @@ -1,13 +0,0 @@ -// -// CreateTableActionHandler.swift -// TablePro -// - -import Foundation - -@MainActor -final class CreateTableActionHandler { - var createTable: (() -> Void)? - var undo: (() -> Void)? - var redo: (() -> Void)? -} diff --git a/apple/TablePro/Views/Structure/CreateTableDraft.swift b/apple/TablePro/Views/Structure/CreateTableDraft.swift index 2877426dab..bffd4f2653 100644 --- a/apple/TablePro/Views/Structure/CreateTableDraft.swift +++ b/apple/TablePro/Views/Structure/CreateTableDraft.swift @@ -17,13 +17,25 @@ internal struct CreateTableCompositionKey: Hashable { let columns: [EditableColumnDefinition] let indexes: [EditableIndexDefinition] let foreignKeys: [EditableForeignKeyDefinition] + let checkConstraints: [EditableCheckConstraintDefinition] } +internal enum CreateTablePane: Hashable, CaseIterable { + case definition + case ddl +} + +// One Create Table tab: the definition it drafts over an empty baseline, its table options, and the commands the +// toolbar and menus send it. The coordinator holds it by tab id, so the work outlives the view. @MainActor internal final class CreateTableDraft: ObservableObject { private static let logger = Logger(subsystem: "com.TablePro", category: "CreateTableDraft") + internal let connection: DatabaseConnection + // The tab's own database and schema: the table is created there, and the reference menus read there. + internal let scope: DatabaseScope? internal let changeManager = StructureChangeManager() + internal let editor: StructureDefinitionEditor @Published internal var tableName = "" { didSet { @@ -33,37 +45,85 @@ internal final class CreateTableDraft: ObservableObject { } @Published internal var tableOptions = CreateTableOptions() @Published internal var form: CreateTableFormState? + @Published internal var pane: CreateTablePane = .definition + @Published internal private(set) var hasResolvedForm = false + @Published internal private(set) var isCreating = false - // Server-wide: the character sets, collations and engines the pickers and the column cells offer. + // Server-wide: the character sets, collations and engines the inspector and the column cells offer. @Published internal private(set) var optionsCatalog: MetadataLoadState = .idle - // Per target database, so a draft compares a pick with the defaults of the database it creates the table in. + // Per target database, so a pick is compared with the defaults of the database the table goes in. @Published internal private(set) var optionDefaults = CreateTableOptionDefaults() - private var optionDefaultsScope: DatabaseScope? - private var optionsScope: DatabaseScope? - private var optionsCatalogRead: Task? - @Published internal private(set) var hasResolvedForm = false - - internal func resolveForm(from spec: @autoclosure () -> PluginCreateTableFormSpec?) { - guard !hasResolvedForm else { return } - hasResolvedForm = true - form = spec().map(CreateTableFormState.init(spec:)) - } @Published internal private(set) var composed: CreateTableStatements? @Published internal private(set) var compositionFailure: String? - // Made on the first mount, which knows the connection; held here so the grid outlives the view. - internal private(set) var gridAdapter: CreateTableGridAdapter? + internal var executionGate: any ExecutionGate = ExecutionGateProvider.shared + private var focusesNameOnAppear = true + private var optionDefaultsScope: DatabaseScope? + private var optionsCatalogRead: Task? private var composedKey: CreateTableCompositionKey? private var compositionGeneration = 0 - private var changeManagerForwarding: AnyCancellable? + private var cancellables: Set = [] - internal init() { - changeManagerForwarding = changeManager.objectWillChange + internal init(connection: DatabaseConnection, scope: DatabaseScope?) { + self.connection = connection + self.scope = scope + editor = StructureDefinitionEditor( + manager: changeManager, + context: .newTable( + connection: connection, + databaseName: scope?.database ?? connection.database, + schemaName: scope?.schema + ) + ) + editor.characterSetContextProvider = { [weak self] in + ColumnCharacterSetMenus.Context( + catalog: self?.optionsCatalog ?? .idle, + tableCharacterSet: self?.tableOptions.charset + ) + } + editor.characterSetCatalogReader = { [weak self] in + self?.readOptionsCatalogIfNeeded() + } + changeManager.objectWillChange .sink { [weak self] in self?.objectWillChange.send() } + .store(in: &cancellables) + $optionsCatalog + .dropFirst() + .receive(on: RunLoop.main) + .sink { [weak self] _ in self?.editor.refresh() } + .store(in: &cancellables) } + // MARK: - Setup + + // Runs on every appearance; each step skips what is done. + internal func prepare(coordinator: MainContentCoordinator?) { + editor.coordinator = coordinator + editor.serverSupport = StructureServerSupport.forConnection(connection.id) + resolveForm(from: DatabaseManager.shared.driver(for: connection.id)?.createTableFormSpec(schema: scope?.schema)) + } + + internal func resolveForm(from spec: @autoclosure () -> PluginCreateTableFormSpec?) { + guard !hasResolvedForm else { return } + hasResolvedForm = true + form = spec().map(CreateTableFormState.init(spec:)) + if form == nil, changeManager.workingColumns.isEmpty { + changeManager.addNewColumn() + editor.refresh() + } + } + + // The Name field takes focus when the tab first shows, not on every return to it. + internal func claimNameFocus() -> Bool { + guard focusesNameOnAppear else { return false } + focusesNameOnAppear = false + return true + } + + // MARK: - State + internal var holdsWork: Bool { !tableName.isEmpty || ObjectCommentDraft.normalized(tableOptions.comment) != nil @@ -73,16 +133,91 @@ internal final class CreateTableDraft: ObservableObject { || changeManager.workingForeignKeys.contains { !$0.name.isEmpty || !$0.columns.isEmpty || !$0.referencedTable.isEmpty } + || changeManager.workingCheckConstraints.contains { !$0.name.isEmpty || !$0.expression.isEmpty } + } + + internal var hasChanges: Bool { holdsWork } + + // The composed statement adds what the driver refuses; until it catches up with the draft, the plan's own. + internal var issues: [SchemaDraftIssue] { + currentComposedIssues ?? plan().issues } - internal func gridAdapter(for connection: DatabaseConnection) -> CreateTableGridAdapter { - if let gridAdapter { return gridAdapter } - let adapter = CreateTableGridAdapter(structureChangeManager: changeManager, connection: connection) - adapter.draft = self - gridAdapter = adapter - return adapter + internal var isReadyToCreate: Bool { + guard hasResolvedForm, !isCreating else { return false } + if let form { + return form.issues(tableName: tableName).isEmpty + } + let plan = plan() + return plan.definition != nil && (currentComposedIssues ?? plan.issues).isEmpty + } + + private var currentComposedIssues: [SchemaDraftIssue]? { + guard let composed, let composedKey, composedKey == compositionKey else { return nil } + return composed.issues + } + + // The definition the issue names, when it names one row. + internal func entityID(for issue: SchemaDraftIssue) -> UUID? { + guard let row = issue.row else { return nil } + let ids = StructureGridEdits.entityIDs(in: issue.section, manager: changeManager) + return ids.indices.contains(row) ? ids[row] : nil + } + + internal func reveal(_ issue: SchemaDraftIssue) { + guard let id = entityID(for: issue) else { return } + pane = .definition + editor.reveal(section: issue.section, entity: id) + } + + // MARK: - Commands + + // The section commands act on: the one holding the selection, else Columns. + internal var commandSection: StructureSection { editor.commandSection } + + private var editsDefinition: Bool { hasResolvedForm && form == nil } + + internal var canUndo: Bool { editsDefinition && changeManager.canUndo } + internal var canRedo: Bool { editsDefinition && changeManager.canRedo } + internal var undoMenuItemTitle: String { changeManager.undoMenuItemTitle } + internal var redoMenuItemTitle: String { changeManager.redoMenuItemTitle } + + internal func undo() { + guard canUndo else { return } + commitOpenEdits() + changeManager.undo() + editor.refresh() + } + + internal func redo() { + guard canRedo else { return } + commitOpenEdits() + changeManager.redo() + editor.refresh() + } + + internal var canAddRow: Bool { editsDefinition && pane == .definition && editor.canAdd(commandSection) } + + internal func addRow() { + guard canAddRow else { return } + editor.add(commandSection) } + internal var canRemoveRows: Bool { + editsDefinition && pane == .definition && editor.canRemoveSelection(in: commandSection) + } + + internal func removeSelectedRows() { + guard canRemoveRows else { return } + editor.removeSelection(in: commandSection) + } + + internal func commitOpenEdits() { + editor.commitPendingEdits() + } + + // MARK: - Table options + // The driver's catalog decides, never the engine's type: a driver without one has no table options. internal var catalog: TableOptionsCatalog? { guard case .loaded(let catalog) = optionsCatalog else { return nil } @@ -91,6 +226,13 @@ internal final class CreateTableDraft: ObservableObject { internal var offersEngineOptions: Bool { catalog != nil } + // A driver's own form names everything its table takes. + internal var offersTableComment: Bool { + guard editsDefinition else { return false } + let support = DatabaseObjectToolEligibility.Support.of(DatabaseManager.shared.driver(for: connection.id)) + return support.commentableTypes.contains(.table) + } + internal func selectCharacterSet(_ charset: String?) { guard charset != tableOptions.charset else { return } tableOptions = tableOptions.selectingCharacterSet( @@ -100,7 +242,8 @@ internal final class CreateTableDraft: ObservableObject { } // Idempotent: a remount skips what is loaded, and a read a switch cancelled runs again on return. - internal func loadTableOptions(scope: DatabaseScope, provider: any ScopedMetadataProviding = DatabaseManager.shared) async { + internal func loadTableOptions(provider: any ScopedMetadataProviding = DatabaseManager.shared) async { + guard let scope else { return } let loader = TableStructureLoader(scope: scope, tableName: "", provider: provider) await loadOptionsCatalog(using: loader) guard catalog != nil, optionDefaultsScope != scope else { return } @@ -121,13 +264,16 @@ internal final class CreateTableDraft: ObservableObject { } // Opening a column's Charset or Collation menu is the retry. - internal func retryOptionsCatalog() { - guard case .failed = optionsCatalog, let optionsScope else { return } - Task { await loadTableOptions(scope: optionsScope) } + private func readOptionsCatalogIfNeeded() { + switch optionsCatalog { + case .idle, .failed: + Task { await loadTableOptions() } + case .loading, .loaded: + break + } } private func loadOptionsCatalog(using loader: TableStructureLoader) async { - optionsScope = loader.scope if case .loaded = optionsCatalog { return } // The draft owns the read, so a view task cancelled by a tab switch leaves it running for the view that returns. if let optionsCatalogRead { @@ -156,22 +302,26 @@ internal final class CreateTableDraft: ObservableObject { await read.value } - internal func plan(for databaseType: DatabaseType) -> CreateTablePlan { + // MARK: - Composition + + internal func plan() -> CreateTablePlan { CreateTableDraftBuilder.plan( tableName: tableName, options: tableOptions, columns: changeManager.workingColumns, indexes: changeManager.workingIndexes, foreignKeys: changeManager.workingForeignKeys, - dialect: ForeignKeyDialect.forType(databaseType), + checkConstraints: changeManager.workingCheckConstraints, + dialect: ForeignKeyDialect.forType(connection.type), includesEngineOptions: offersEngineOptions, defaults: optionDefaults, - suppliesItsOwnKey: PluginManager.shared.defaultPrimaryKeyColumn(for: databaseType) != nil + suppliesItsOwnKey: PluginManager.shared.defaultPrimaryKeyColumn(for: connection.type) != nil ) } - internal func compositionKey(scope: DatabaseScope) -> CreateTableCompositionKey { - CreateTableCompositionKey( + internal var compositionKey: CreateTableCompositionKey? { + guard let scope else { return nil } + return CreateTableCompositionKey( scope: scope, tableName: tableName, options: tableOptions, @@ -179,21 +329,19 @@ internal final class CreateTableDraft: ObservableObject { optionDefaults: optionDefaults, columns: changeManager.workingColumns, indexes: changeManager.workingIndexes, - foreignKeys: changeManager.workingForeignKeys + foreignKeys: changeManager.workingForeignKeys, + checkConstraints: changeManager.workingCheckConstraints ) } - internal func recompose(databaseType: DatabaseType, scope: DatabaseScope) async { - let key = compositionKey(scope: scope) - guard key != composedKey else { return } + // Keeps the last good statement on screen until the next one lands, and through a failure. + internal func recompose() async { + guard let scope, let key = compositionKey, key != composedKey else { return } compositionGeneration += 1 let generation = compositionGeneration do { - let statements = try await DatabaseManager.shared.createTableStatements( - plan: plan(for: databaseType), - scope: scope - ) + let statements = try await DatabaseManager.shared.createTableStatements(plan: plan(), scope: scope) guard generation == compositionGeneration else { return } composed = statements composedKey = key @@ -205,12 +353,226 @@ internal final class CreateTableDraft: ObservableObject { compositionFailure = error.localizedDescription } } + + internal func formStatements(for request: PluginCreateTableRequest) throws -> [String] { + guard let driver = DatabaseManager.shared.driver(for: connection.id) else { + throw PluginCreateTableFormError(message: String(localized: "Not connected to database")) + } + return try driver.createTableStatements(for: request, schema: scope?.schema) + } + + internal var formPreview: CreateTableFormState.Preview? { + form?.preview(tableName: tableName) { request in try formStatements(for: request) } + } + + // MARK: - Preview SQL + + // Refuses what Create Table refuses, so the statements shown are the ones it would run. + internal func previewSQL(coordinator: MainContentCoordinator) { + guard hasResolvedForm, coordinator.activeSheet == nil else { return } + commitOpenEdits() + if let formState = form { + let formIssues = formState.issues(tableName: tableName) + guard formIssues.isEmpty else { + reportIncomplete(formIssues.map(\.qualifiedMessage), on: coordinator) + return + } + do { + let statements = try formStatements(for: formState.request(tableName: tableName)) + presentPreview(statements, on: coordinator) + } catch { + reportPreviewFailure(error, on: coordinator) + } + return + } + + let plan = plan() + guard plan.issues.isEmpty else { + reportIncomplete(plan.issues.map(\.qualifiedMessage), on: coordinator) + return + } + guard let scope else { return } + Task { + do { + let composed = try await DatabaseManager.shared.createTableStatements(plan: plan, scope: scope) + guard composed.issues.isEmpty, !composed.statements.isEmpty else { + reportIncomplete(composed.issues.map(\.qualifiedMessage), on: coordinator) + return + } + presentPreview(composed.statements, on: coordinator) + } catch { + reportPreviewFailure(error, on: coordinator) + } + } + } + + private func presentPreview(_ statements: [String], on coordinator: MainContentCoordinator) { + guard coordinator.activeSheet == nil, coordinator.activeCreateTableDraft === self else { return } + coordinator.toolbarState.previewStatements = statements + coordinator.activeSheet = .sqlPreview + } + + private func reportIncomplete(_ messages: [String], on coordinator: MainContentCoordinator) { + AlertHelper.showErrorSheet( + title: String(localized: "Some Changes Are Incomplete"), + message: messages.joined(separator: "\n"), + window: coordinator.contentWindow + ) + } + + private func reportPreviewFailure(_ error: any Error, on coordinator: MainContentCoordinator) { + Self.logger.error("Create Table preview failed: \(error.publicLogShape, privacy: .public)") + AlertHelper.showErrorSheet( + title: String(localized: "Preview SQL"), + message: error.localizedDescription, + window: coordinator.contentWindow + ) + } + + // MARK: - Create + + // What Cmd+S and the toolbar's Create Table run. + internal func create(coordinator: MainContentCoordinator) { + guard hasResolvedForm, !isCreating else { return } + commitOpenEdits() + if form != nil { + createFromForm(coordinator: coordinator) + } else { + createFromDefinition(coordinator: coordinator) + } + } + + private func createFromDefinition(coordinator: MainContentCoordinator) { + let plan = plan() + guard plan.issues.isEmpty else { + reportFailure(plan.issues.map(\.qualifiedMessage).joined(separator: "\n"), on: coordinator) + return + } + guard let scope else { + reportFailure(String(localized: "Not connected to database"), on: coordinator) + return + } + + isCreating = true + Task { + defer { isCreating = false } + do { + let composed = try await DatabaseManager.shared.createTableStatements(plan: plan, scope: scope) + guard composed.issues.isEmpty, !composed.statements.isEmpty else { + reportFailure(composed.issues.map(\.qualifiedMessage).joined(separator: "\n"), on: coordinator) + return + } + try await run( + composed.statements, + creating: composed.tableName ?? tableName, + in: scope, + coordinator: coordinator + ) + } catch { + Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") + reportFailure(error.localizedDescription, on: coordinator) + } + } + } + + private func createFromForm(coordinator: MainContentCoordinator) { + guard let formState = form else { return } + let formIssues = formState.issues(tableName: tableName) + guard formIssues.isEmpty else { + reportFailure(formIssues.map(\.qualifiedMessage).joined(separator: "\n"), on: coordinator) + return + } + guard let scope else { + reportFailure(String(localized: "Not connected to database"), on: coordinator) + return + } + + let request = formState.request(tableName: tableName) + let schema = scope.schema + isCreating = true + form?.clearSubmissionError() + + Task { + defer { isCreating = false } + do { + let statements = try await DatabaseManager.shared.withMetadataDriver(scope: scope) { driver in + try driver.createTableStatements(for: request, schema: schema) + } + guard !statements.isEmpty else { + form?.recordSubmissionError(PluginCreateTableFormError( + message: String(localized: "The form produced no statements to run.") + )) + return + } + try await run(statements, creating: request.tableName, in: scope, coordinator: coordinator) + } catch let formError as PluginCreateTableFormError { + form?.recordSubmissionError(formError) + } catch { + Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") + reportFailure(error.localizedDescription, on: coordinator) + } + } + } + + // The draft keeps its work after a create, so the tab stays open; the new table opens in its own tab. + private func run( + _ statements: [String], + creating name: String, + in scope: DatabaseScope, + coordinator: MainContentCoordinator + ) async throws { + do { + try await DatabaseManager.shared.executeCreateTable( + statements: statements, + databaseType: connection.type, + scope: scope, + gate: executionGate + ) + } catch let incomplete as CreateTableIncompleteError { + openCreatedTable(name, in: scope, showStructure: true, coordinator: coordinator) + AlertHelper.showErrorSheet( + title: String(localized: "Table Created with Errors"), + message: incomplete.message, + recoverySuggestion: String( + localized: "The table exists, but a statement after CREATE TABLE failed. Finish the change in its Structure tab." + ), + window: coordinator.contentWindow + ) + return + } + openCreatedTable(name, in: scope, showStructure: false, coordinator: coordinator) + } + + private func openCreatedTable( + _ name: String, + in scope: DatabaseScope, + showStructure: Bool, + coordinator: MainContentCoordinator + ) { + coordinator.openTableTab( + name, + schema: scope.schema, + database: scope.database.nilIfEmpty, + showStructure: showStructure + ) + AppCommands.shared.objectChanged.send( + DatabaseObjectChange(connectionId: connection.id, scope: scope, name: name, kind: .rows) + ) + } + + private func reportFailure(_ message: String, on coordinator: MainContentCoordinator) { + AlertHelper.showErrorSheet( + title: String(localized: "Create Table Failed"), + message: message, + window: coordinator.contentWindow + ) + } } extension CreateTableDraft: ActiveGridModelProviding { - // The definition grid, unless the driver supplied a form of its own. + // The section holding the selection while the Definition page is on screen; a driver's form has no grid. var activeGridModel: DataGridModel? { - guard hasResolvedForm, form == nil else { return nil } - return gridAdapter?.model + guard editsDefinition, pane == .definition else { return nil } + return editor.focusedModel ?? editor.adapter(for: .columns).model } } diff --git a/apple/TablePro/Views/Structure/CreateTableFormEditor.swift b/apple/TablePro/Views/Structure/CreateTableFormEditor.swift deleted file mode 100644 index 0c44badea8..0000000000 --- a/apple/TablePro/Views/Structure/CreateTableFormEditor.swift +++ /dev/null @@ -1,90 +0,0 @@ -// -// CreateTableFormEditor.swift -// TablePro -// - -import SwiftUI -import TableProPluginKit - -private enum CreateTableFormPane: CaseIterable { - case form - case preview - - var displayName: String { - switch self { - case .form: String(localized: "Form") - case .preview: String(localized: "Preview") - } - } -} - -internal struct CreateTableFormEditor: View { - @ObservedObject internal var draft: CreateTableDraft - internal let databaseType: DatabaseType - internal let isCreating: Bool - internal let generateStatements: (PluginCreateTableRequest) throws -> [String] - internal let onCreate: () -> Void - - @State private var pane: CreateTableFormPane = .form - - internal var body: some View { - if let form = draft.form { - let issues = form.issues(tableName: draft.tableName) - let messages = [form.submissionError?.message].compactMap { $0 } + issues.map(\.qualifiedMessage) - VStack(spacing: 0) { - PaneTabBar( - title: String(localized: "View"), - selection: $pane, - identifier: "create-table-form-tabs" - ) { - ForEach(CreateTableFormPane.allCases, id: \.self) { tab in - Text(tab.displayName).tag(tab) - } - } - - Divider() - - Group { - switch pane { - case .form: - CreateTableFormView(draft: draft) - case .preview: - CreateTableFormPreview( - preview: form.preview(tableName: draft.tableName, generate: generateStatements), - databaseType: databaseType - ) - } - } - .frame(maxWidth: .infinity, maxHeight: .infinity) - - Divider() - - HStack(spacing: 8) { - if let first = messages.first { - Label(first, systemImage: "exclamationmark.triangle.fill") - .font(.callout) - .foregroundStyle(.secondary) - .lineLimit(1) - .truncationMode(.tail) - .help(messages.joined(separator: "\n")) - .accessibilityIdentifier("create-table-validation") - } - - Spacer(minLength: 12) - - Button( - isCreating ? String(localized: "Creating…") : String(localized: "Create Table"), - action: onCreate - ) - .buttonStyle(.borderedProminent) - .tint(.accentColor) - .disabled(!issues.isEmpty || isCreating) - .keyboardShortcut(.return, modifiers: .command) - .accessibilityIdentifier("create-table-commit") - } - .padding(.horizontal) - .padding(.vertical, 8) - } - } - } -} diff --git a/apple/TablePro/Views/Structure/CreateTableGridAdapter.swift b/apple/TablePro/Views/Structure/CreateTableGridAdapter.swift deleted file mode 100644 index 599cc720d7..0000000000 --- a/apple/TablePro/Views/Structure/CreateTableGridAdapter.swift +++ /dev/null @@ -1,327 +0,0 @@ -// -// CreateTableGridAdapter.swift -// TablePro -// - -import AppKit -import TableProPluginKit - -// The Create Table grid: every row is a definition the draft holds, edited by the definition a row -// names. Held by the draft, so the grid's state survives the tab being switched away and back. -@MainActor -final class CreateTableGridAdapter { - let model: DataGridModel - let store: StructureGridStateStore - let structureChangeManager: StructureChangeManager - let connection: DatabaseConnection - let referenceMenus: ForeignKeyReferenceMenus - - weak var coordinator: MainContentCoordinator? - // Holds the catalog the Charset and Collation cells pick from, and the table's own character set. - weak var draft: CreateTableDraft? - var serverSupport = StructureServerSupport.unrestricted - private(set) var structureTab: StructureTab = .columns - - var schemaName: String? { - get { referenceMenus.schemaName } - set { referenceMenus.schemaName = newValue } - } - - private(set) var provider: StructureRowProvider - private var sortDescriptor: StructureSortDescriptor? - private var snapshot = StructureGridSnapshot() - private let changeTracking: StructureGridChangeState - private let changeState: AnyChangeManager - - init(structureChangeManager: StructureChangeManager, connection: DatabaseConnection) { - self.structureChangeManager = structureChangeManager - self.connection = connection - let store = StructureGridStateStore() - self.store = store - self.model = DataGridModel(store: store) - self.referenceMenus = ForeignKeyReferenceMenus(connectionId: connection.id, databaseType: connection.type) - self.changeTracking = StructureGridChangeState(manager: structureChangeManager) - self.changeState = AnyChangeManager(changeTracking) - self.provider = StructureRowProvider( - changeManager: structureChangeManager, - tab: .columns, - databaseType: connection.type, - additionalFields: [.primaryKey], - serverSupport: .unrestricted - ) - referenceMenus.onListsChanged = { [weak self] in - self?.coordinator?.inspectorRowSourceRevision += 1 - } - model.dataSource = self - model.actions = self - } - - func select(_ tab: StructureTab) { - guard tab != structureTab else { return } - model.commitEditing() - structureTab = tab - sortDescriptor = nil - store.switchSubTab(to: tab) - store.sortState = SortState() - refresh() - } - - var characterSetContext: ColumnCharacterSetMenus.Context { - ColumnCharacterSetMenus.Context( - catalog: draft?.optionsCatalog ?? .idle, - tableCharacterSet: draft?.tableOptions.charset - ) - } - - func characterSetCatalogDidChange() { - refresh() - coordinator?.inspectorRowSourceRevision += 1 - } - - func refresh() { - changeTracking.tab = structureTab - changeTracking.rowCount = StructureGridEdits.rowCount(on: structureTab, manager: structureChangeManager) - let provider = StructureRowProvider( - changeManager: structureChangeManager, - tab: structureTab, - databaseType: connection.type, - additionalFields: [.primaryKey], - serverSupport: serverSupport, - sortDescriptor: sortDescriptor, - picksCharacterSets: ColumnCharacterSetMenus.offersMenus(characterSetContext.catalog) - ) - self.provider = provider - let rows = provider.asTableRows() - let states = rows.rows.map { row in - StructureGridEdits.visualState( - sourceIndex: StructureRowProvider.sourceIndex(of: row.id) ?? -1, - tab: structureTab, - manager: structureChangeManager, - orderedFields: provider.orderedColumnFields, - tintsModifiedFields: false - ) - } - model.configuration = makeConfiguration(provider: provider) - let previous = snapshot - snapshot = StructureGridSnapshot(rows: rows, states: states) - model.applyHeldSnapshot(from: previous, to: snapshot) - if previous.rows.rows != rows.rows || previous.states != states || previous.rows.columns != rows.columns { - coordinator?.inspectorRowSourceRevision += 1 - } - } - - private func makeConfiguration(provider: StructureRowProvider) -> DataGridConfiguration { - var configuration = DataGridConfiguration() - configuration.dropdownColumns = provider.dropdownColumns - configuration.typePickerColumns = provider.typePickerColumns - configuration.customDropdownOptions = provider.customDropdownOptions - configuration.isEditable = true - configuration.traits = DataGridEngineTraits( - databaseType: connection.type, - connectionScope: DatabaseScope( - connectionId: connection.id, - database: DatabaseManager.shared.browseDatabaseName(for: connection), - schema: schemaName - ), - looksUpUserDefinedTypes: true - ) - configuration.commands = [.addRow, .deleteRows, .valueFilter, .freezeColumns] - return configuration - } - - // MARK: - Commands - - func menuOptions(sourceIndex: Int, field: Int) -> [GridMenuOption]? { - switch structureTab { - case .indexes: - guard structureChangeManager.workingIndexes.indices.contains(sourceIndex) else { return nil } - return StructureRowProvider.indexMenuOptions( - columnIndex: field, - index: structureChangeManager.workingIndexes[sourceIndex], - serverSupport: serverSupport - ) - case .foreignKeys: - guard structureChangeManager.workingForeignKeys.indices.contains(sourceIndex) else { return nil } - return referenceMenus.options( - columnIndex: field, - foreignKey: structureChangeManager.workingForeignKeys[sourceIndex], - tableColumns: structureChangeManager.workingColumns.map(\.name) - ) - case .columns: - guard structureChangeManager.workingColumns.indices.contains(sourceIndex), - provider.orderedColumnFields.indices.contains(field) else { return nil } - return ColumnCharacterSetMenus.options( - for: provider.orderedColumnFields[field], - column: structureChangeManager.workingColumns[sourceIndex], - context: characterSetContext - ) - case .checkConstraints, .options, .ddl, .parts, .triggers: - return nil - } - } - - func characterSetLockedFields(sourceIndex: Int) -> Set { - guard structureTab == .columns, structureChangeManager.workingColumns.indices.contains(sourceIndex) else { return [] } - return ColumnCharacterSetMenus.lockedFieldIndices( - orderedFields: provider.orderedColumnFields, - column: structureChangeManager.workingColumns[sourceIndex], - catalog: characterSetContext.catalog - ) - } - - @discardableResult - func applyEdit(rowID: RowID, field: Int, text: String) -> Bool { - guard let sourceIndex = StructureRowProvider.sourceIndex(of: rowID) else { return false } - let applied = write(text, field: field, sourceIndex: sourceIndex) - refresh() - return applied - } - - private func write(_ text: String, field: Int, sourceIndex: Int) -> Bool { - let characterSets = characterSetContext - guard field >= 0, - !StructureGridEdits.refuses( - text, - field: field, - sourceIndex: sourceIndex, - tab: structureTab, - manager: structureChangeManager, - orderedFields: provider.orderedColumnFields, - characterSets: characterSets - ) else { return false } - let changedReference = StructureGridEdits.apply( - text, - field: field, - sourceIndex: sourceIndex, - tab: structureTab, - manager: structureChangeManager, - orderedFields: provider.orderedColumnFields, - connection: connection, - characterSets: characterSets - ) - if let changedReference { - referenceMenus.prefetchReferencedColumns( - of: changedReference.referencedTable, schema: changedReference.referencedSchema - ) - } - return true - } - - func addRow() { - StructureGridEdits.addRow(on: structureTab, manager: structureChangeManager) - refresh() - } - - func deleteRows(_ rowIDs: [RowID]) { - let indices = rowIDs.compactMap(StructureRowProvider.sourceIndex(of:)) - guard !indices.isEmpty else { return } - let displayRows = IndexSet(rowIDs.compactMap { model.displayIndex(of: $0) }) - StructureGridEdits.delete(sourceIndices: indices, tab: structureTab, manager: structureChangeManager) - refresh() - let remaining = model.rowCount - guard remaining > 0, let minRow = displayRows.first, let maxRow = displayRows.last else { - model.clearSelection() - return - } - let target = maxRow < remaining ? maxRow : (minRow > 0 ? minRow - 1 : 0) - if let rowID = model.rowID(at: target) { - model.selectRows([rowID]) - } else { - model.clearSelection() - } - } - - func undo() { - model.commitEditing() - structureChangeManager.undo() - refresh() - } - - func redo() { - model.commitEditing() - structureChangeManager.redo() - refresh() - } -} - -extension CreateTableGridAdapter: DataGridDataSource { - var gridRows: TableRows { snapshot.rows } - var gridChangeState: AnyChangeManager { changeState } - - func gridVisualState(for rowID: RowID) -> RowVisualState? { - snapshot.state(for: rowID) - } - - func gridMenuOptions(for rowID: RowID, column: DataGridColumn) -> [GridMenuOption]? { - guard let sourceIndex = StructureRowProvider.sourceIndex(of: rowID) else { return nil } - let options = menuOptions(sourceIndex: sourceIndex, field: column.dataIndex) - if structureTab == .columns, provider.orderedColumnFields.indices.contains(column.dataIndex), - ColumnCharacterSetMenus.isCharacterSetField(provider.orderedColumnFields[column.dataIndex]) { - draft?.retryOptionsCatalog() - } - return options - } -} - -extension CreateTableGridAdapter: DataGridActions { - func gridCommit(_ edits: [DataGridResolvedEdit], actionName: String) -> Bool { - let writes = edits.compactMap { edit -> (Int, Int, String)? in - StructureRowProvider.sourceIndex(of: edit.rowID).map { ($0, edit.dataIndex, edit.newValue.asText ?? "") } - } - guard !writes.isEmpty else { return false } - var applied = false - let writeAll = { - for (sourceIndex, field, text) in writes { - let wrote = self.write(text, field: field, sourceIndex: sourceIndex) - applied = applied || wrote - } - } - if writes.count > 1 { - structureChangeManager.performAsOneUndoStep(writeAll) - } else { - writeAll() - } - refresh() - return applied - } - - func gridDeleteRows(_ rowIDs: [RowID]) { - deleteRows(rowIDs) - } - - func gridAddRow() { - addRow() - } - - func gridSortDidChange(_ sort: SortState) { - if let primary = sort.columns.first { - sortDescriptor = StructureSortDescriptor(column: primary.columnIndex, ascending: primary.direction == .ascending) - store.sortState = SortState(columns: [primary], source: .user) - } else { - sortDescriptor = nil - store.sortState = SortState(columns: [], source: .user) - } - refresh() - } -} - -extension CreateTableGridAdapter: InspectorRowSource { - func inspectorRow(atDisplayRow displayRow: Int) -> InspectorRow? { - guard let rowID = model.rowID(at: displayRow), - let sourceIndex = StructureRowProvider.sourceIndex(of: rowID), - let providerRow = provider.displayRow(ofSourceIndex: sourceIndex) else { return nil } - return StructureInspectorRowBuilder.row( - atDisplayRow: providerRow, - tab: structureTab, - provider: provider, - canEditSchema: true, - lockedFieldIndices: characterSetLockedFields(sourceIndex: sourceIndex), - rowOptions: { menuOptions(sourceIndex: sourceIndex, field: $0) } - ) - } - - func commitInspectorField(displayRow: Int, fieldIndex: Int, value: String?) { - guard let rowID = model.rowID(at: displayRow) else { return } - applyEdit(rowID: rowID, field: fieldIndex, text: value ?? "") - } -} diff --git a/apple/TablePro/Views/Structure/CreateTableView.swift b/apple/TablePro/Views/Structure/CreateTableView.swift index ffdb61b7ac..1d626da883 100644 --- a/apple/TablePro/Views/Structure/CreateTableView.swift +++ b/apple/TablePro/Views/Structure/CreateTableView.swift @@ -3,548 +3,208 @@ // TablePro // -import AppKit -import Combine -import os import SwiftUI import TableProPluginKit -private enum CreateTableTab: CaseIterable { - case columns - case indexes - case foreignKeys - case sqlPreview - - var displayName: String { - switch self { - case .columns: String(localized: "Columns") - case .indexes: String(localized: "Indexes") - case .foreignKeys: String(localized: "Foreign Keys") - case .sqlPreview: String(localized: "SQL Preview") - } - } -} - +// A Create Table tab: the Structure tab's Definition page over an empty table, with the name on top, and the +// CREATE statement it composes. Create Table runs from the toolbar and Cmd+S; the table options are in the inspector. struct CreateTableView: View { - private static let logger = Logger(subsystem: "com.TablePro", category: "CreateTableView") - - let connection: DatabaseConnection - - let scope: DatabaseScope? - var coordinator: MainContentCoordinator? - - @Environment(\.appServices) private var services - @ObservedObject var draft: CreateTableDraft - @ObservedObject private var gridModel: DataGridModel - private let gridAdapter: CreateTableGridAdapter - - private var structureChangeManager: StructureChangeManager { draft.changeManager } + let coordinator: MainContentCoordinator? - @State private var selectedTab: CreateTableTab = .columns - @State private var isCreating = false - @State private var errorMessage: String? - @State private var showError = false - @State private var actionHandler = CreateTableActionHandler() + @AppStorage("structureCodeFontSize", store: AppStorageEnvironment.shared.defaults) private var ddlFontSize: Double = 13 - init( - connection: DatabaseConnection, - scope: DatabaseScope?, - coordinator: MainContentCoordinator?, - draft: CreateTableDraft - ) { - self.connection = connection - self.scope = scope - self.coordinator = coordinator - self.draft = draft - let adapter = draft.gridAdapter(for: connection) - self.gridAdapter = adapter - self.gridModel = adapter.model - } + private static let compositionPause: Duration = .milliseconds(150) var body: some View { VStack(spacing: 0) { - configBar - Divider() - editorContent + if draft.hasResolvedForm { + paneBar + Divider() + paneContent + .frame(maxWidth: .infinity, maxHeight: .infinity) + } else { + Color.clear + } } .navigationTitle(String(localized: "Create Table")) .onAppear { - draft.resolveForm( - from: DatabaseManager.shared.driver(for: connection.id)?.createTableFormSpec(schema: scope?.schema) - ) - actionHandler.createTable = { createTable() } - coordinator?.createTableActions = actionHandler - if draft.form == nil { - attachStructureGrid() - } - coordinator?.toolbarState.hasCreateTablePending = isReadyToCreate + draft.prepare(coordinator: coordinator) + publishReadiness() } .onDisappear { - // Guarded by identity: the outgoing view's onDisappear can land after the incoming one's onAppear. - if coordinator?.createTableActions === actionHandler { - coordinator?.createTableActions = nil - coordinator?.toolbarState.hasCreateTablePending = false - } - if coordinator?.inspectorRowSource === gridAdapter { - coordinator?.inspectorRowSource = nil - } - } - .onChange(of: selectedTab) { _ in gridAdapter.select(structureTab) } - .onChange(of: structureChangeManager.reloadVersion) { _ in gridAdapter.refresh() } - .onChange(of: structureChangeManager.hasChanges) { _ in gridAdapter.refresh() } - .onChange(of: isReadyToCreate) { _ in updateCreateTablePendingState() } - .onChange(of: draft.optionsCatalog) { _ in gridAdapter.characterSetCatalogDidChange() } - .task(id: scope) { await loadTableOptions() } - .task(id: compositionKey) { await recomposeAfterPause() } - .alert(String(localized: "Create Table Failed"), isPresented: $showError) { - Button("OK") {} - } message: { - Text(verbatim: RevealedText(errorMessage ?? "").plainText) - } - } - - @ViewBuilder - private var editorContent: some View { - if draft.form != nil { - CreateTableFormEditor( - draft: draft, - databaseType: connection.type, - isCreating: isCreating, - generateStatements: { request in try formStatements(for: request) }, - onCreate: { createTable() } - ) - } else if draft.hasResolvedForm { - tabBar - Divider() - tabContent - .frame(maxWidth: .infinity, maxHeight: .infinity) - Divider() - commitBar - } else { - Color.clear - } - } - - private func attachStructureGrid() { - coordinator?.inspectorRowSource = gridAdapter - gridAdapter.coordinator = coordinator - gridAdapter.serverSupport = StructureServerSupport.forConnection(connection.id) - gridAdapter.schemaName = coordinator?.toolbarState.currentSchema - if structureChangeManager.workingColumns.isEmpty { - structureChangeManager.addNewColumn() - } - gridAdapter.select(structureTab) - gridAdapter.refresh() - actionHandler.undo = { gridAdapter.undo() } - actionHandler.redo = { gridAdapter.redo() } - } - - // MARK: - Config Bar - - private var configBar: some View { - HStack(spacing: 12) { - Text("Table Name:") - .font(.body.weight(.medium)) - - TextField("Enter table name", text: $draft.tableName) - .textFieldStyle(.roundedBorder) - .autocorrectionDisabled(true) - .frame(maxWidth: 300) - .accessibilityLabel(String(localized: "Table Name")) - .accessibilityIdentifier("create-table-name") - - if offersTableComment { - Text("Comment:") - .font(.body.weight(.medium)) - - TextField("Optional", text: $draft.tableOptions.comment) - .textFieldStyle(.roundedBorder) - .frame(maxWidth: 300) - .accessibilityLabel(String(localized: "Comment")) - .accessibilityIdentifier("create-table-comment") - } - - if let catalog = draft.catalog, draft.hasResolvedForm, draft.form == nil { - Divider() - .frame(height: 20) - - engineOptions(catalog) - } - - Spacer() + // Guarded: the outgoing view's onDisappear can land after the incoming one's onAppear. + if let active = coordinator?.activeCreateTableDraft, active !== draft { return } + coordinator?.toolbarState.hasCreateTablePending = false } - .padding() - .paneBarBackground() + .onChange(of: draft.isReadyToCreate) { _ in publishReadiness() } + .task(id: draft.scope) { await draft.loadTableOptions() } + .task(id: draft.compositionKey) { await recomposeAfterPause() } } - // Lists from the server's catalog, started from the target database's defaults. Nil writes no clause. - @ViewBuilder - private func engineOptions(_ catalog: TableOptionsCatalog) -> some View { - let options = draft.tableOptions - if !catalog.engines.isEmpty || options.engine != nil { - Picker("Engine:", selection: $draft.tableOptions.engine) { - if options.engine == nil { - Text("Default").tag(String?.none) - } - ForEach(TableOptionsForm.engines(in: catalog, keeping: options.engine)) { choice in - Text(choice.title).tag(Optional(choice.name)) - } - } - .fixedSize() - .accessibilityIdentifier("create-table-engine") - } - - Picker("Charset:", selection: characterSetBinding) { - if options.charset == nil { - Text("Default").tag(String?.none) - } - ForEach(TableOptionsForm.characterSets(in: catalog, keeping: options.charset), id: \.self) { name in - Text(name).tag(Optional(name)) - } - } - .fixedSize() - .accessibilityIdentifier("create-table-charset") - - Picker("Collation:", selection: $draft.tableOptions.collation) { - if options.collation == nil { - Text("Default").tag(String?.none) - } - ForEach( - TableOptionsForm.collations(in: catalog, characterSet: options.charset, keeping: options.collation) - ) { choice in - Text(choice.title).tag(Optional(choice.name)) - } - } - .fixedSize() - .disabled(options.charset == nil) - .accessibilityIdentifier("create-table-collation") + private func publishReadiness() { + coordinator?.toolbarState.hasCreateTablePending = draft.isReadyToCreate } - // One whole-value write: the collation follows the charset, so a second write would resolve against a stale draft. - private var characterSetBinding: Binding { - Binding( - get: { draft.tableOptions.charset }, - set: { draft.selectCharacterSet($0) } - ) - } - - private func loadTableOptions() async { - guard let scope else { return } - await draft.loadTableOptions(scope: scope) - } - - private var offersTableComment: Bool { - guard draft.hasResolvedForm, draft.form == nil else { return false } - let support = DatabaseObjectToolEligibility.Support.of(DatabaseManager.shared.driver(for: connection.id)) - return support.commentableTypes.contains(.table) + private func recomposeAfterPause() async { + try? await Task.sleep(for: Self.compositionPause) + guard !Task.isCancelled else { return } + await draft.recompose() } - // MARK: - Tabs + // MARK: - Panes - private var availableTabs: [CreateTableTab] { - var tabs = CreateTableTab.allCases - if !connection.type.supportsForeignKeys { - tabs = tabs.filter { $0 != .foreignKeys } - } - return tabs - } - - private var tabBar: some View { + private var paneBar: some View { PaneTabBar( title: String(localized: "Structure"), - selection: $selectedTab.committingEdit(of: gridModel), + selection: paneSelection, identifier: "create-table-tabs" ) { - ForEach(availableTabs, id: \.self) { tab in - Text(tab.displayName).tag(tab) + ForEach(CreateTablePane.allCases, id: \.self) { pane in + Text(title(of: pane)).tag(pane) } } } - // One grid serves the three grid tabs: the adapter repoints it as the selection moves. - @ViewBuilder - private var tabContent: some View { - switch selectedTab { - case .columns, .indexes, .foreignKeys: - structureGridPane - case .sqlPreview: - sqlPreviewView - } + // An open cell edit lands before the pane changes, so the DDL includes it. + private var paneSelection: Binding { + Binding( + get: { draft.pane }, + set: { pane in + draft.commitOpenEdits() + draft.pane = pane + } + ) } - // MARK: - Commit Bar - - private var commitBar: some View { - let issues = draft.composed?.issues ?? draft.plan(for: connection.type).issues - - return HStack(spacing: 8) { - issueMessage(issues) - - Spacer(minLength: 12) - - Button(isCreating ? String(localized: "Creating…") : String(localized: "Create Table")) { - createTable() + private func title(of pane: CreateTablePane) -> String { + switch pane { + case .definition: + if draft.form == nil { + return String(localized: "Definition") } - .buttonStyle(.borderedProminent) - .tint(.accentColor) - .disabled(!issues.isEmpty || isCreating) - .keyboardShortcut(.return, modifiers: .command) - .accessibilityIdentifier("create-table-commit") + return String(localized: "Form") + case .ddl: + return "DDL" } - .padding(.horizontal) - .padding(.vertical, 8) } @ViewBuilder - private func issueMessage(_ issues: [SchemaDraftIssue]) -> some View { - if let first = issues.first { - Label(messageText(first), systemImage: "exclamationmark.triangle.fill") - .font(.callout) - .foregroundStyle(.secondary) - .lineLimit(1) - .truncationMode(.tail) - .help(issues.map(\.qualifiedMessage).joined(separator: "\n")) - .accessibilityIdentifier("create-table-validation") - } - } - - private func messageText(_ issue: SchemaDraftIssue) -> String { - issue.tab == structureTab ? issue.message : issue.qualifiedMessage - } - - // MARK: - Structure Grid - - private var structureTab: StructureTab { - switch selectedTab { - case .columns: return .columns - case .indexes: return .indexes - case .foreignKeys: return .foreignKeys - case .sqlPreview: return .columns - } - } - - private var structureGridPane: some View { - VStack(spacing: 0) { - DataGridView(model: gridModel) - Divider() - rowButtons(identifier: rowButtonsIdentifier) - } - } - - private var rowButtonsIdentifier: String { - switch selectedTab { - case .columns, .sqlPreview: return "create-table-columns" - case .indexes: return "create-table-indexes" - case .foreignKeys: return "create-table-foreign-keys" - } - } - - // Add and Delete act on the shown tab's rows, since the adapter follows the selected tab. - private func rowButtons(identifier: String) -> some View { - HStack(spacing: 8) { - Button(action: { gridAdapter.addRow() }) { - Image(systemName: "plus") - .frame(width: 24, height: 24) - } - .help(String(localized: "Add Row")) - .accessibilityLabel(String(localized: "Add Row")) - .accessibilityIdentifier("\(identifier)-add") - - Button(action: { gridAdapter.deleteRows(gridModel.selectedRowIDs) }) { - Image(systemName: "minus") - .frame(width: 24, height: 24) + private var paneContent: some View { + switch draft.pane { + case .definition: + if draft.form != nil { + VStack(spacing: 0) { + CreateTableNameHeader(draft: draft) + Divider() + CreateTableFormView(draft: draft) + } + } else { + StructureDefinitionPage( + editor: draft.editor, + loadState: StructureLoadState(), + header: AnyView(CreateTableNameHeader(draft: draft)) + ) } - .help(String(localized: "Delete Selected")) - .accessibilityLabel(String(localized: "Delete Selected")) - .disabled(gridModel.selection.isEmpty) - .accessibilityIdentifier("\(identifier)-remove") - - Spacer() + case .ddl: + ddlPane } - .padding(8) } - // MARK: - SQL Preview - @ViewBuilder - private var sqlPreviewView: some View { - if let composed = draft.composed { + private var ddlPane: some View { + if let preview = draft.formPreview { + CreateTableFormPreview(preview: preview, databaseType: draft.connection.type) + } else if let composed = draft.composed { if composed.statements.isEmpty { CreateTableSQLPreviewStandIn(content: .message(composed.issues.first?.qualifiedMessage)) } else { - DDLTextView(ddl: composed.preview, fontSize: .constant(13), databaseType: connection.type) + DDLTextView(ddl: composed.preview, fontSize: $ddlFontSize, databaseType: draft.connection.type) } } else if let failure = draft.compositionFailure { CreateTableSQLPreviewStandIn(content: .message(failure)) } else { - uncomposedPreview(plan: draft.plan(for: connection.type)) - } - } - - private func uncomposedPreview(plan: CreateTablePlan) -> some View { - CreateTableSQLPreviewStandIn( - content: plan.definition == nil ? .message(plan.issues.first?.qualifiedMessage) : .composing - ) - } - - // MARK: - SQL Generation - - private static let compositionPause: Duration = .milliseconds(150) - - private var compositionKey: CreateTableCompositionKey? { - scope.map { draft.compositionKey(scope: $0) } - } - - private func recomposeAfterPause() async { - guard let scope else { return } - try? await Task.sleep(for: Self.compositionPause) - guard !Task.isCancelled else { return } - await draft.recompose(databaseType: connection.type, scope: scope) - } - - // MARK: - Create Table - - private var isReadyToCreate: Bool { - guard !isCreating else { return false } - if let form = draft.form { - return form.issues(tableName: draft.tableName).isEmpty - } - guard let composed = draft.composed else { return false } - return composed.issues.isEmpty && !composed.statements.isEmpty - } - - private func formStatements(for request: PluginCreateTableRequest) throws -> [String] { - guard let driver = DatabaseManager.shared.driver(for: connection.id) else { - throw PluginCreateTableFormError(message: String(localized: "Not connected to database")) + let plan = draft.plan() + CreateTableSQLPreviewStandIn( + content: plan.definition == nil ? .message(plan.issues.first?.qualifiedMessage) : .composing + ) } - return try driver.createTableStatements(for: request, schema: scope?.schema) } +} - private func updateCreateTablePendingState() { - coordinator?.toolbarState.hasCreateTablePending = isReadyToCreate - } +// The table's name and what still stands between the draft and Create Table. An issue that names a row reveals it. +struct CreateTableNameHeader: View { + @ObservedObject var draft: CreateTableDraft + @FocusState private var isNameFocused: Bool - private func createTable() { - guard draft.form != nil else { - createTableFromStructureGrid() - return - } - createTableFromForm() + private struct Row: Identifiable { + let id: String + let message: String + let reveal: (() -> Void)? } - private func createTableFromStructureGrid() { - guard !isCreating else { return } - let plan = draft.plan(for: connection.type) - guard plan.issues.isEmpty else { - errorMessage = plan.issues.map(\.qualifiedMessage).joined(separator: "\n") - showError = true - return - } - guard let scope else { - errorMessage = String(localized: "Not connected to database") - showError = true - return - } - - isCreating = true - errorMessage = nil - updateCreateTablePendingState() - - Task { - defer { isCreating = false } - do { - let composed = try await DatabaseManager.shared.createTableStatements(plan: plan, scope: scope) - guard composed.issues.isEmpty, !composed.statements.isEmpty else { - errorMessage = composed.issues.map(\.qualifiedMessage).joined(separator: "\n") - showError = true - return + var body: some View { + VStack(alignment: .leading, spacing: 8) { + HStack(spacing: 8) { + Text("Table Name:") + TextField(String(localized: "Table Name"), text: $draft.tableName, prompt: Text("Required")) + .textFieldStyle(.roundedBorder) + .autocorrectionDisabled(true) + .frame(maxWidth: 300) + .focused($isNameFocused) + .accessibilityIdentifier("create-table-name") + } + let issueRows = rows + if !issueRows.isEmpty { + VStack(alignment: .leading, spacing: 4) { + ForEach(issueRows) { row in + issueRow(row) + } } - let createdName = composed.tableName ?? draft.tableName - try await runCreateTable(statements: composed.statements, createdName: createdName, in: scope) - } catch { - Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") - errorMessage = error.localizedDescription - showError = true + .accessibilityElement(children: .contain) + .accessibilityIdentifier("create-table-validation") } } - } - - private func createTableFromForm() { - guard !isCreating, let form = draft.form else { return } - let issues = form.issues(tableName: draft.tableName) - guard issues.isEmpty else { - errorMessage = issues.map(\.qualifiedMessage).joined(separator: "\n") - showError = true - return - } - guard let scope else { - errorMessage = String(localized: "Not connected to database") - showError = true - return + .padding(.horizontal, 10) + .padding(.vertical, 10) + .frame(maxWidth: .infinity, alignment: .leading) + .onAppear { + if draft.claimNameFocus() { + isNameFocused = true + } } + } - let request = form.request(tableName: draft.tableName) - let schema = scope.schema - isCreating = true - errorMessage = nil - draft.form?.clearSubmissionError() - updateCreateTablePendingState() - - Task { - defer { isCreating = false } - do { - let statements = try await DatabaseManager.shared.withMetadataDriver(scope: scope) { driver in - try driver.createTableStatements(for: request, schema: schema) - } - guard !statements.isEmpty else { - draft.form?.recordSubmissionError(PluginCreateTableFormError( - message: String(localized: "The form produced no statements to run.") - )) - return - } - try await runCreateTable(statements: statements, createdName: request.tableName, in: scope) - } catch let formError as PluginCreateTableFormError { - draft.form?.recordSubmissionError(formError) - } catch { - Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") - errorMessage = error.localizedDescription - showError = true + private func issueRow(_ row: Row) -> some View { + HStack(alignment: .firstTextBaseline, spacing: 6) { + Image(systemName: "exclamationmark.triangle.fill") + .foregroundStyle(.secondary) + .accessibilityHidden(true) + if let reveal = row.reveal { + Button(row.message, action: reveal) + .buttonStyle(.link) + } else { + Text(row.message) + .foregroundStyle(.secondary) } } + .font(.callout) + .fixedSize(horizontal: false, vertical: true) } - private func runCreateTable(statements: [String], createdName: String, in scope: DatabaseScope) async throws { - do { - try await DatabaseManager.shared.executeCreateTable( - statements: statements, - databaseType: connection.type, - scope: scope - ) - } catch let incomplete as CreateTableIncompleteError { - openCreatedTable(createdName, in: scope, showStructure: true) - AlertHelper.showErrorSheet( - title: String(localized: "Table Created with Errors"), - message: incomplete.message, - recoverySuggestion: String( - localized: "The table exists, but a statement after CREATE TABLE failed. Finish the change in its Structure tab." - ), - window: coordinator?.contentWindow + private var rows: [Row] { + if let form = draft.form { + let messages = [form.submissionError?.message].compactMap { $0 } + + form.issues(tableName: draft.tableName).map(\.qualifiedMessage) + return messages.enumerated().map { Row(id: "\($0.offset)", message: $0.element, reveal: nil) } + } + return draft.issues.enumerated().map { offset, issue in + Row( + id: "\(offset)-\(issue.id)", + message: issue.row == nil ? issue.message : issue.qualifiedMessage, + reveal: draft.entityID(for: issue) == nil ? nil : { draft.reveal(issue) } ) - return } - openCreatedTable(createdName, in: scope, showStructure: false) - } - - private func openCreatedTable(_ name: String, in scope: DatabaseScope, showStructure: Bool) { - let created = DatabaseObjectChange(connectionId: connection.id, scope: scope, name: name, kind: .rows) - coordinator?.openTableTab( - name, - schema: scope.schema, - database: scope.database.nilIfEmpty, - showStructure: showStructure - ) - AppCommands.shared.objectChanged.send(created) } } diff --git a/apple/TablePro/Views/Structure/Inspector/CreateTablePropertiesView.swift b/apple/TablePro/Views/Structure/Inspector/CreateTablePropertiesView.swift new file mode 100644 index 0000000000..09d4303321 --- /dev/null +++ b/apple/TablePro/Views/Structure/Inspector/CreateTablePropertiesView.swift @@ -0,0 +1,117 @@ +// +// CreateTablePropertiesView.swift +// TablePro +// + +import SwiftUI + +// The drafted table's comment and engine options, when nothing on the Definition page is selected. The lists come +// from the server's catalog and start from the target database's defaults; nil writes no clause. +struct CreateTablePropertiesView: View { + @ObservedObject var draft: CreateTableDraft + + private var catalog: TableOptionsCatalog? { + guard case .loaded(let catalog) = draft.optionsCatalog else { return nil } + return catalog + } + + private var offersComment: Bool { + let driver = DatabaseManager.shared.driver(for: draft.editor.context.connection.id) + return DatabaseObjectToolEligibility.Support.of(driver).commentableTypes.contains(.table) + } + + var body: some View { + let offersComment = draft.form == nil && self.offersComment + let catalog = draft.form == nil ? self.catalog : nil + if !offersComment, catalog == nil { + UnavailableStateView( + String(localized: "No Selection"), + systemImage: TrailingPaneSurface.inspector.symbolName, + description: Text(String(localized: "Select a definition to edit it")) + ) + } else { + ScrollView { + VStack(alignment: .leading, spacing: 6) { + StructureInspectorSectionHeader(title: String(localized: "Table")) + VStack(alignment: .leading, spacing: 10) { + if offersComment { + StructureInspectorRow(label: String(localized: "Comment")) { + TextField(String(localized: "Optional"), text: $draft.tableOptions.comment, axis: .vertical) + .lineLimit(2...6) + .textFieldStyle(.squareBorder) + .accessibilityLabel(String(localized: "Comment")) + .accessibilityIdentifier("create-table-comment") + } + } + if let catalog { + engineRows(catalog) + } + } + } + .padding(.horizontal, InspectorMetrics.horizontalInset) + .padding(.vertical, 12) + .frame(maxWidth: .infinity, alignment: .leading) + } + .font(.subheadline) + .controlSize(.small) + .disabled(draft.editor.isReadOnly) + } + } + + @ViewBuilder + private func engineRows(_ catalog: TableOptionsCatalog) -> some View { + let options = draft.tableOptions + if !catalog.engines.isEmpty || options.engine != nil { + StructureInspectorRow(label: String(localized: "Engine")) { + Picker(String(localized: "Engine"), selection: $draft.tableOptions.engine) { + if options.engine == nil { + Text(String(localized: "Default")).tag(String?.none) + } + ForEach(TableOptionsForm.engines(in: catalog, keeping: options.engine)) { choice in + Text(choice.title).tag(Optional(choice.name)) + } + } + .labelsHidden() + .accessibilityIdentifier("create-table-engine") + } + } + + StructureInspectorRow(label: String(localized: "Character Set")) { + Picker(String(localized: "Character Set"), selection: characterSetBinding) { + if options.charset == nil { + Text(String(localized: "Default")).tag(String?.none) + } + ForEach(TableOptionsForm.characterSets(in: catalog, keeping: options.charset), id: \.self) { name in + Text(name).tag(Optional(name)) + } + } + .labelsHidden() + .accessibilityIdentifier("create-table-charset") + } + + StructureInspectorRow(label: String(localized: "Collation")) { + Picker(String(localized: "Collation"), selection: $draft.tableOptions.collation) { + if options.collation == nil { + Text(String(localized: "Default")).tag(String?.none) + } + ForEach( + TableOptionsForm.collations(in: catalog, characterSet: options.charset, keeping: options.collation) + ) { choice in + Text(choice.title).tag(Optional(choice.name)) + } + } + .labelsHidden() + .disabled(options.charset == nil) + .accessibilityIdentifier("create-table-collation") + } + } + + // One whole-value write: the collation follows the character set, so a second write would resolve against a + // stale draft. + private var characterSetBinding: Binding { + Binding( + get: { draft.tableOptions.charset }, + set: { draft.selectCharacterSet($0) } + ) + } +} diff --git a/apple/TablePro/Views/Structure/Inspector/StructureDefinitionFormView.swift b/apple/TablePro/Views/Structure/Inspector/StructureDefinitionFormView.swift new file mode 100644 index 0000000000..ec0123cf4f --- /dev/null +++ b/apple/TablePro/Views/Structure/Inspector/StructureDefinitionFormView.swift @@ -0,0 +1,165 @@ +// +// StructureDefinitionFormView.swift +// TablePro +// + +import SwiftUI + +// The selected column, index, foreign key or check constraint as a form: each field's name above a control the pane's +// width, the layout the Data view's inspector uses, since a label column leaves a 270pt pane too little room. +struct StructureDefinitionFormView: View { + let editor: StructureDefinitionEditor + let form: StructureInspectorForm + let revision: Int + + var body: some View { + ScrollView { + VStack(alignment: .leading, spacing: 12) { + if form.isDeleted { + Label( + String(localized: "Marked for deletion. Undo or Undo Delete brings it back."), + systemImage: "trash" + ) + .font(.callout) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + .accessibilityIdentifier("structure-inspector-deleted-note") + } + + VStack(alignment: .leading, spacing: 10) { + ForEach(form.fields) { field in + if case .checkbox = StructureInspectorControl.resolve(field) { + control(for: field) + } else { + StructureInspectorRow(label: field.label, isModified: field.isModified) { + control(for: field) + } + } + } + } + } + .padding(.horizontal, InspectorMetrics.horizontalInset) + .padding(.vertical, 12) + .frame(maxWidth: .infinity, alignment: .leading) + } + .font(.subheadline) + .controlSize(.small) + .onAppear { + // Reading the character sets is what a Charset or Collation menu opening does in the grid. + guard form.section == .columns, form.isEditable else { return } + editor.readCharacterSetCatalogIfNeeded() + } + } + + @ViewBuilder + private func control(for field: StructureInspectorForm.Field) -> some View { + let identifier = StructureInspectorPresentation.controlIdentifier(section: form.section, field: field.id) + let value = field.value ?? "" + switch StructureInspectorControl.resolve(field) { + case .text: + StructureInspectorTextField( + label: field.label, value: value, revision: revision, + onType: { type($0, into: field) }, onEnd: { editor.endInspectorTyping() } + ) + .accessibilityLabel(field.label) + .accessibilityIdentifier(identifier) + case .multiline: + StructureInspectorTextField( + label: field.label, value: value, revision: revision, isMultiline: true, + onType: { type($0, into: field) }, onEnd: { editor.endInspectorTyping() } + ) + .accessibilityLabel(field.label) + .accessibilityIdentifier(identifier) + case .checkbox: + Toggle( + isOn: Binding( + get: { value == StructureRowProvider.booleanOptions[0] }, + set: { isOn in + commit(isOn ? StructureRowProvider.booleanOptions[0] : StructureRowProvider.booleanOptions[1], to: field) + } + ) + ) { + Text(field.label) + .fontWeight(field.isModified ? .semibold : .regular) + } + .toggleStyle(.checkbox) + .accessibilityLabel(field.label) + .accessibilityIdentifier(identifier) + case .popUp(let choices): + popUp(choices, field: field) + .accessibilityLabel(field.label) + .accessibilityIdentifier(identifier) + case .comboBox(let choices): + StructureInspectorSuggestionField( + label: field.label, + value: value, + revision: revision, + identifier: identifier, + groups: choices.groups, + onType: { type($0, into: field) }, + onPick: { commit($0, to: field) }, + onEnd: { editor.endInspectorTyping() } + ) + case .typePicker: + StructureInspectorTypeField( + value: value, + revision: revision, + label: field.label, + identifier: identifier, + databaseType: editor.context.databaseType, + scope: editor.context.scope, + onType: { type($0, into: field) }, + onPick: { commit($0, to: field) }, + onEnd: { editor.endInspectorTyping() } + ) + case .readOnly(let text): + Text(text) + .textSelection(.enabled) + .fixedSize(horizontal: false, vertical: true) + .accessibilityLabel(field.label) + .accessibilityValue(text) + .accessibilityIdentifier(identifier) + } + } + + private func popUp(_ choices: StructureInspectorChoices, field: StructureInspectorForm.Field) -> some View { + Picker( + selection: Binding( + get: { choices.selectedID ?? Self.noSelection }, + set: { id in + guard let value = choices.value(of: id) else { return } + commit(value, to: field) + } + ) + ) { + ForEach(choices.groups) { group in + if let header = group.header { + Section { + ForEach(group.items) { Text($0.title).tag($0.id) } + } header: { + Text(header) + } + } else { + ForEach(group.items) { Text($0.title).tag($0.id) } + } + } + } label: { + Text(field.label) + } + .pickerStyle(.menu) + .labelsHidden() + .fixedSize(horizontal: false, vertical: true) + } + + // A tag no item carries, so a value the menu does not list shows nothing selected rather than another item. + private static let noSelection = -1 + + private func type(_ value: String, into field: StructureInspectorForm.Field) { + editor.typeInspectorField(section: form.section, id: form.entityID, field: field.id, value: value) + } + + private func commit(_ value: String, to field: StructureInspectorForm.Field) { + guard value != (field.value ?? "") else { return } + editor.commitInspectorField(section: form.section, id: form.entityID, field: field.id, value: value) + } +} diff --git a/apple/TablePro/Views/Structure/Inspector/StructureInspectorControls.swift b/apple/TablePro/Views/Structure/Inspector/StructureInspectorControls.swift new file mode 100644 index 0000000000..f9846b16f5 --- /dev/null +++ b/apple/TablePro/Views/Structure/Inspector/StructureInspectorControls.swift @@ -0,0 +1,232 @@ +// +// StructureInspectorControls.swift +// TablePro +// + +import SwiftUI + +// A definition field's text. Every keystroke is staged at once, the way the table comment's field is, so Save, Refresh +// and the close prompt always see what is typed; the editor folds the run into one undo step, ended by Return or by +// focus leaving. +struct StructureInspectorTextField: View { + let label: String + let value: String + let revision: Int + var isMultiline = false + // Moved by a pick from a menu beside the field: the field shows the picked value even while it has focus. + var resetToken = 0 + let onType: (String) -> Void + let onEnd: () -> Void + + @State private var draft = "" + // What the field last wrote or was synced from, so only typing writes, never a value the model moved under it. + @State private var base = "" + // The stored value, so leaving a field whose write was refused puts it back. + @State private var latest = "" + @State private var hasSeeded = false + @FocusState private var isFocused: Bool + + private struct Sync: Equatable { + let value: String + let revision: Int + } + + var body: some View { + field + .textFieldStyle(.squareBorder) + .autocorrectionDisabled(true) + .focused($isFocused) + .onAppear(perform: seed) + .onChange(of: Sync(value: value, revision: revision)) { sync in + latest = sync.value + guard !isFocused else { return } + draft = sync.value + base = sync.value + } + .onChange(of: draft) { text in + guard hasSeeded, isFocused, text != base else { return } + base = text + onType(text) + } + .onChange(of: isFocused) { focused in + guard !focused else { return } + onEnd() + draft = latest + base = latest + } + .onChange(of: resetToken) { _ in + draft = value + base = value + latest = value + } + .onSubmit(onEnd) + .onDisappear(perform: onEnd) + } + + // The field's name is above it, so an empty field shows no placeholder. + @ViewBuilder + private var field: some View { + if isMultiline { + TextField(text: $draft, prompt: Text(verbatim: ""), axis: .vertical) { Text(label) } + .lineLimit(2...6) + .labelsHidden() + } else { + TextField(text: $draft, prompt: Text(verbatim: "")) { Text(label) } + .labelsHidden() + } + } + + private func seed() { + guard !hasSeeded else { return } + hasSeeded = true + draft = value + base = value + latest = value + } +} + +// Free text with the engine's types and the database's own types to pick from, grouped as the grid's type picker +// groups them. +struct StructureInspectorTypeField: View { + let value: String + let revision: Int + let label: String + let identifier: String + let databaseType: DatabaseType + let scope: DatabaseScope? + let onType: (String) -> Void + let onPick: (String) -> Void + let onEnd: () -> Void + + var body: some View { + UserDefinedTypeAwarePicker(scope: scope) { userDefinedTypes in + StructureInspectorSuggestionField( + label: label, + value: value, + revision: revision, + identifier: identifier, + groups: Self.groups(userDefinedTypes: userDefinedTypes, databaseType: databaseType), + onType: onType, + onPick: onPick, + onEnd: onEnd + ) + } + } + + static func groups(userDefinedTypes: [String], databaseType: DatabaseType) -> [StructureInspectorChoices.ItemGroup] { + var categories = PluginManager.shared.columnTypesByCategory(for: databaseType) + .sorted { $0.key < $1.key } + .map { (name: $0.key, types: $0.value) } + if !userDefinedTypes.isEmpty { + categories.insert((name: String(localized: "User-Defined"), types: userDefinedTypes), at: 0) + } + var nextID = 0 + var groups: [StructureInspectorChoices.ItemGroup] = [] + for (offset, category) in categories.enumerated() { + var items: [StructureInspectorChoices.Item] = [] + for type in category.types { + items.append(StructureInspectorChoices.Item(id: nextID, title: type, value: type)) + nextID += 1 + } + groups.append(StructureInspectorChoices.ItemGroup(id: offset, header: category.name, items: items)) + } + return groups + } +} + +// A text field with its common values one click away, the way a combo box offers them. A SwiftUI field and a +// pull-down menu, so it draws like every other field in the pane; an AppKit combo box keeps the pre-glass bezel. +struct StructureInspectorSuggestionField: View { + let label: String + let value: String + let revision: Int + let identifier: String + let groups: [StructureInspectorChoices.ItemGroup] + let onType: (String) -> Void + let onPick: (String) -> Void + let onEnd: () -> Void + + @State private var pickRevision = 0 + + var body: some View { + HStack(spacing: 4) { + StructureInspectorTextField( + label: label, value: value, revision: revision, resetToken: pickRevision, onType: onType, onEnd: onEnd + ) + .accessibilityLabel(label) + .accessibilityIdentifier(identifier) + Menu { + ForEach(groups) { group in + if let header = group.header { + Section(header) { items(group) } + } else { + items(group) + } + } + } label: { + Label(String(format: String(localized: "Choose %@"), label), systemImage: "chevron.down") + .labelStyle(.iconOnly) + } + .menuStyle(.borderlessButton) + .menuIndicator(.hidden) + .fixedSize() + .help(String(format: String(localized: "Choose %@"), label)) + .accessibilityIdentifier("\(identifier)-choices") + } + } + + // A pick is its own undo step and replaces whatever is typed in the field. + private func items(_ group: StructureInspectorChoices.ItemGroup) -> some View { + ForEach(group.items) { item in + Button(item.title) { + pickRevision &+= 1 + guard item.value != value else { return } + onPick(item.value) + } + } + } +} + +// One field: its name above, the control the pane's full width below, so every field shares the pane's leading edge +// as the Data view's inspector and the statistics do. Modified settings read bold, the way Xcode marks them. +struct StructureInspectorRow: View { + let label: String + var isModified = false + @ViewBuilder let control: () -> Control + + var body: some View { + VStack(alignment: .leading, spacing: 3) { + Text(label) + .fontWeight(isModified ? .semibold : .regular) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + .accessibilityHidden(true) + control() + .frame(maxWidth: .infinity, alignment: .leading) + } + } +} + +// The inspector's section title, the style the table statistics below it use. +struct StructureInspectorSectionHeader: View { + let title: String + + var body: some View { + Text(title) + .font(.caption.weight(.semibold)) + .foregroundStyle(.secondary) + .accessibilityAddTraits(.isHeader) + } +} + +struct StructureInspectorFootnote: View { + let text: String + + var body: some View { + Text(text) + .font(.caption) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + .frame(maxWidth: .infinity, alignment: .leading) + } +} diff --git a/apple/TablePro/Views/Structure/Inspector/StructureInspectorForm.swift b/apple/TablePro/Views/Structure/Inspector/StructureInspectorForm.swift new file mode 100644 index 0000000000..6ae7fb2cd5 --- /dev/null +++ b/apple/TablePro/Views/Structure/Inspector/StructureInspectorForm.swift @@ -0,0 +1,116 @@ +// +// StructureInspectorForm.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// One definition's fields as the inspector edits them. Built from the same provider and menus the grid uses, so +// the two never disagree, and written back through the grid's own edit path, one undo step per field. +struct StructureInspectorForm: Equatable { + struct Field: Equatable, Identifiable { + enum Kind: Equatable { + case text + case multiline + case toggle + case menu([GridMenuOption]) + case combo([GridMenuOption]) + case typePicker + } + + let id: Int + let label: String + let value: String? + let kind: Kind + let isModified: Bool + let isEditable: Bool + } + + let section: StructureSection + let entityID: UUID + let name: String + let fields: [Field] + let isEditable: Bool + let isDeleted: Bool + let isNew: Bool + + @MainActor + static func build(adapter: StructureSectionAdapter, sourceIndex: Int, entityID: UUID) -> StructureInspectorForm? { + let provider = StructureRowProvider( + changeManager: adapter.manager, + tab: adapter.section, + databaseType: adapter.context.databaseType, + additionalFields: [.primaryKey], + serverSupport: adapter.serverSupport, + picksCharacterSets: ColumnCharacterSetMenus.offersMenus(adapter.characterSetContext.catalog) + ) + guard let values = provider.row(at: sourceIndex) else { return nil } + let names = provider.columns + let state = adapter.manager.deleteInsertState(for: sourceIndex, tab: adapter.section) + let modified = provider.modifiedFieldIndices(atDisplay: sourceIndex) + let locked = adapter.lockedFieldIndices.union(adapter.characterSetLockedFields(sourceIndex: sourceIndex)) + let isEditable = adapter.canEdit && !state.isDeleted + let fields = names.indices.map { index in + Field( + id: index, + label: names[index], + value: index < values.count ? values[index] : nil, + kind: kind( + at: index, + section: adapter.section, + orderedFields: provider.orderedColumnFields, + options: adapter.menuOptions(sourceIndex: sourceIndex, field: index) ?? provider.customDropdownOptions[index], + typePickerColumns: provider.typePickerColumns + ), + isModified: !state.isInserted && modified.contains(index), + isEditable: isEditable && !locked.contains(index) + ) + } + return StructureInspectorForm( + section: adapter.section, + entityID: entityID, + name: (values.first ?? nil) ?? "", + fields: fields, + isEditable: isEditable, + isDeleted: state.isDeleted, + isNew: state.isInserted + ) + } + + @MainActor + static func kind( + at index: Int, + section: StructureSection, + orderedFields: [StructureColumnField], + options: [GridMenuOption]?, + typePickerColumns: Set + ) -> Field.Kind { + if typePickerColumns.contains(index) { + return .typePicker + } + if let options, !options.isEmpty { + if options.compactMap(\.sql) == StructureRowProvider.booleanOptions { + return .toggle + } + if options.contains(where: { if case .custom = $0 { true } else { false } }) { + return .combo(options) + } + return .menu(options) + } + switch section { + case .columns: + guard orderedFields.indices.contains(index) else { return .text } + switch orderedFields[index] { + case .comment, .generationExpression: return .multiline + default: return .text + } + case .checkConstraints: + return index == 1 ? .multiline : .text + case .indexes: + return index == 4 ? .multiline : .text + case .foreignKeys: + return .text + } + } +} diff --git a/apple/TablePro/Views/Structure/Inspector/StructureInspectorPresentation.swift b/apple/TablePro/Views/Structure/Inspector/StructureInspectorPresentation.swift new file mode 100644 index 0000000000..fe896e4a8e --- /dev/null +++ b/apple/TablePro/Views/Structure/Inspector/StructureInspectorPresentation.swift @@ -0,0 +1,121 @@ +// +// StructureInspectorPresentation.swift +// TablePro +// + +import Foundation + +enum StructureInspectorPresentation { + @MainActor + static func subject( + for target: StructureDefinitionEditor.InspectorTarget, + definitionName: String?, + tableName: String + ) -> InspectorSubject { + let table = tableName.isEmpty ? nil : tableName + switch target { + case .entity(let section, _): + return .definition(name: definitionName ?? "", section: section, table: table) + case .table, .several: + return .tableOnly(table: table ?? String(localized: "New Table")) + } + } + + static func severalTitle(_ section: StructureSection, count: Int) -> String { + switch section { + case .columns: String(format: String(localized: "%d Columns Selected"), count) + case .indexes: String(format: String(localized: "%d Indexes Selected"), count) + case .foreignKeys: String(format: String(localized: "%d Foreign Keys Selected"), count) + case .checkConstraints: String(format: String(localized: "%d Check Constraints Selected"), count) + } + } + + static func controlIdentifier(section: StructureSection, field: Int) -> String { + "structure-inspector-\(section.rawValue)-\(field)" + } +} + +// The control a definition field gets in the inspector. A field the form cannot edit is plain text. +enum StructureInspectorControl: Equatable { + case text + case multiline + case checkbox + case popUp(StructureInspectorChoices) + case comboBox(StructureInspectorChoices) + case typePicker + case readOnly(String) + + static func resolve(_ field: StructureInspectorForm.Field) -> StructureInspectorControl { + let value = field.value ?? "" + switch field.kind { + case .text: + return field.isEditable ? .text : .readOnly(value) + case .multiline: + return field.isEditable ? .multiline : .readOnly(value) + case .toggle: + return field.isEditable ? .checkbox : .readOnly(value) + case .typePicker: + return field.isEditable ? .typePicker : .readOnly(value) + case .menu(let options): + let choices = StructureInspectorChoices(options: options, current: value) + return field.isEditable ? .popUp(choices) : .readOnly(choices.selectedTitle ?? value) + case .combo(let options): + return field.isEditable ? .comboBox(StructureInspectorChoices(options: options, current: value)) : .readOnly(value) + } + } +} + +// A grid menu as a pop-up button's items. The value a field holds is always an item, listed or not, since a pop-up +// whose selection matches no tag shows nothing selected. +struct StructureInspectorChoices: Equatable { + struct Item: Equatable, Identifiable { + let id: Int + let title: String + let value: String + } + + struct ItemGroup: Equatable, Identifiable { + let id: Int + let header: String? + var items: [Item] + } + + let groups: [ItemGroup] + let selectedID: Int? + + var items: [Item] { groups.flatMap(\.items) } + + var selectedTitle: String? { + items.first { $0.id == selectedID }?.title + } + + func value(of id: Int) -> String? { + items.first { $0.id == id }?.value + } + + init(options: [GridMenuOption], current: String) { + var groups = [ItemGroup(id: options.count + 1, header: nil, items: [])] + for (index, option) in options.enumerated() { + switch option { + case .sectionHeader(let title): + groups.append(ItemGroup(id: index, header: title, items: [])) + case .value(let title, let sql): + groups[groups.count - 1].items.append(Item(id: index, title: title, value: sql)) + case .clear(let title): + groups[groups.count - 1].items.append(Item(id: index, title: title, value: "")) + case .custom: + continue + } + } + let listed = groups.flatMap(\.items) + var selected = listed.first { $0.value == current }?.id + ?? listed.first { $0.value.caseInsensitiveCompare(current) == .orderedSame }?.id + if selected == nil, !current.isEmpty { + let kept = Item(id: options.count, title: current, value: current) + groups[0].items.insert(kept, at: 0) + selected = kept.id + } + self.groups = groups.filter { $0.header != nil || !$0.items.isEmpty } + selectedID = selected + } +} diff --git a/apple/TablePro/Views/Structure/Inspector/StructureInspectorView.swift b/apple/TablePro/Views/Structure/Inspector/StructureInspectorView.swift new file mode 100644 index 0000000000..6a71080f21 --- /dev/null +++ b/apple/TablePro/Views/Structure/Inspector/StructureInspectorView.swift @@ -0,0 +1,89 @@ +// +// StructureInspectorView.swift +// TablePro +// + +import SwiftUI + +// The window inspector while a Structure view or a Create Table tab is on screen: the selected definition's form, +// or the table's own properties when nothing is selected. +struct StructureInspectorView: View { + let source: StructureInspectorSource + let tableMetadata: TableMetadata? + + var body: some View { + switch source { + case .table(let session): + StructureInspectorContent(editor: session.editor, tableName: session.tableName) { + StructureTablePropertiesView( + session: session, + changeManager: session.changeManager, + tableMetadata: tableMetadata + ) + } + case .createTable(let draft): + CreateTableInspectorContent(draft: draft) + } + } +} + +private struct CreateTableInspectorContent: View { + @ObservedObject var draft: CreateTableDraft + + var body: some View { + StructureInspectorContent(editor: draft.editor, tableName: draft.tableName) { + CreateTablePropertiesView(draft: draft) + } + } +} + +private struct StructureInspectorContent: View { + @ObservedObject var editor: StructureDefinitionEditor + let tableName: String + @ViewBuilder let tableProperties: () -> TableProperties + + var body: some View { + let target = editor.inspectorTarget + let form = selectedForm(target) + VStack(spacing: 0) { + InspectorSubjectView( + subject: StructureInspectorPresentation.subject( + for: target, + definitionName: form?.name, + tableName: tableName + ) + ) + content(target: target, form: form) + .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) + } + .accessibilityElement(children: .contain) + .accessibilityIdentifier("structure-inspector") + } + + private func selectedForm(_ target: StructureDefinitionEditor.InspectorTarget) -> StructureInspectorForm? { + guard case .entity(let section, let id) = target else { return nil } + return editor.inspectorForm(for: section, id: id) + } + + @ViewBuilder + private func content( + target: StructureDefinitionEditor.InspectorTarget, + form: StructureInspectorForm? + ) -> some View { + switch target { + case .table: + tableProperties() + case .entity: + if let form { + StructureDefinitionFormView(editor: editor, form: form, revision: editor.revision) + .id(form.entityID) + } + case .several(let section, let count): + UnavailableStateView( + StructureInspectorPresentation.severalTitle(section, count: count), + systemImage: TrailingPaneSurface.inspector.symbolName, + description: Text(String(localized: "Select one to edit it")) + ) + } + } +} diff --git a/apple/TablePro/Views/Structure/Inspector/StructureTablePropertiesView.swift b/apple/TablePro/Views/Structure/Inspector/StructureTablePropertiesView.swift new file mode 100644 index 0000000000..1cfce1caf4 --- /dev/null +++ b/apple/TablePro/Views/Structure/Inspector/StructureTablePropertiesView.swift @@ -0,0 +1,439 @@ +// +// StructureTablePropertiesView.swift +// TablePro +// + +import SwiftUI + +// What the inspector shows for a table with no definition selected: its comment, its default character set and +// AUTO_INCREMENT counter, and the read-only statistics. +struct StructureTablePropertiesView: View { + @ObservedObject var session: StructureEditingSession + @ObservedObject var changeManager: StructureChangeManager + let tableMetadata: TableMetadata? + + // A session-bound table belongs to one connection's session, so its definition is not this window's to change. + private var isReadOnly: Bool { + session.editor.isReadOnly || session.isSessionBound + } + + var body: some View { + let offersComment = session.offersTableComment + let offersOptions = session.offersTableOptionsTab + if !offersComment, !offersOptions, tableMetadata == nil { + UnavailableStateView( + String(localized: "No Selection"), + systemImage: TrailingPaneSurface.inspector.symbolName, + description: Text(String(localized: "Select a definition to edit it")) + ) + } else { + ScrollView { + VStack(alignment: .leading, spacing: 18) { + if offersComment { + StructureTableCommentSection( + session: session, changeManager: changeManager, isReadOnly: isReadOnly + ) + } + if offersOptions { + StructureTableOptionsSection( + session: session, changeManager: changeManager, isReadOnly: isReadOnly + ) + } + if let tableMetadata { + TableInfoSections(metadata: tableMetadata, includesComment: false) + } + } + .padding(.horizontal, InspectorMetrics.horizontalInset) + .padding(.vertical, 12) + .frame(maxWidth: .infinity, alignment: .leading) + } + .font(.subheadline) + .controlSize(.small) + } + } +} + +// MARK: - Comment + +private struct StructureTableCommentSection: View { + @ObservedObject var session: StructureEditingSession + @ObservedObject var changeManager: StructureChangeManager + let isReadOnly: Bool + + @FocusState private var isFocused: Bool + + private var field: StructureTableCommentPolicy.Field? { + StructureTableCommentPolicy.resolve( + objectKind: session.objectKind, + support: DatabaseObjectToolEligibility.Support.of(DatabaseManager.shared.driver(for: session.connection.id)), + isReadOnly: isReadOnly, + load: session.tableComment.erased, + isSaving: changeManager.isHeldForSave || session.editor.isAwaitingBaseline + ) + } + + // One model write per keystroke, so a save from the keyboard never misses what is still in the field. + private var text: Binding { + Binding( + get: { changeManager.tableComment.text }, + set: { changeManager.stageTableComment($0) } + ) + } + + var body: some View { + if let field { + VStack(alignment: .leading, spacing: 6) { + StructureInspectorSectionHeader(title: String(localized: "Table")) + VStack(alignment: .leading, spacing: 10) { + StructureInspectorRow( + label: String(localized: "Comment"), + isModified: changeManager.tableComment.hasChanges + ) { + TextField(field.placeholder, text: text, axis: .vertical) + .lineLimit(2...6) + .textFieldStyle(.squareBorder) + .disabled(!field.isEditable) + .focused($isFocused) + .onSubmit { changeManager.endTableCommentRun() } + .help(field.unavailableReason ?? "") + .accessibilityLabel(String(localized: "Comment")) + .accessibilityIdentifier("structure-table-comment") + } + } + } + .onChange(of: isFocused) { focused in + guard !focused else { return } + changeManager.endTableCommentRun() + } + .onDisappear { changeManager.endTableCommentRun() } + } + } +} + +// MARK: - Options + +// Every edit is one whole-value write to the change manager, so the coupled character set and collation pick +// lands as one staged change and one undo step. +private struct StructureTableOptionsSection: View { + @ObservedObject var session: StructureEditingSession + @ObservedObject var changeManager: StructureChangeManager + let isReadOnly: Bool + + @State private var counterText = "" + @FocusState private var isCounterFocused: Bool + + var body: some View { + VStack(alignment: .leading, spacing: 6) { + StructureInspectorSectionHeader(title: String(localized: "Options")) + if let options = displayedOptions { + form(options) + } else { + stateView + } + } + .task { await session.readTableOptionsCatalog() } + .onAppear { counterText = Self.text(displayedCounter) } + .onChange(of: displayedCounter) { counter in + guard !isCounterFocused else { return } + counterText = Self.text(counter) + } + // Text that is not a valid counter stays in the field beside its error, since it holds Save until fixed. + .onChange(of: isCounterFocused) { focused in + guard !focused, entry(counterText)?.message == nil else { return } + counterText = Self.text(displayedCounter) + } + } + + // MARK: State + + private var draft: TableOptions? { changeManager.tableOptions.current } + + // A read that landed while edits were staged has not become the baseline yet, so it shows read-only. + private var displayedOptions: TableOptions? { + draft ?? (session.tableOptions.value ?? nil) + } + + private var isCounterConfirmed: Bool { + session.autoIncrementRead == .confirmed && changeManager.tableOptions.isAutoIncrementConfirmed + } + + // An unconfirmed counter may be one the server moved past; a read deferred behind staged edits shows read-only. + private var displayedCounter: UInt64? { + guard session.autoIncrementRead == .confirmed else { return nil } + guard changeManager.tableOptions.isAutoIncrementConfirmed else { return session.loadedTableOptions?.autoIncrement } + return displayedOptions?.autoIncrement + } + + // The draft's column, not the loaded one: the counter is set after the column work in the same save. + private var autoIncrementMaximum: UInt64? { + changeManager.autoIncrementColumn.map { AutoIncrementEntry.maximum(for: $0) } + } + + private var editability: StructureTableOptionsPolicy.Editability { + StructureTableOptionsPolicy.resolve( + hasBaseline: draft != nil, + isReadOnly: isReadOnly, + isSaving: changeManager.isHeldForSave || session.editor.isAwaitingBaseline + ) + } + + private var catalog: TableOptionsCatalog? { + guard case .loaded(let catalog) = session.tableOptionsCatalog else { return nil } + return catalog + } + + private func entry(_ text: String) -> AutoIncrementEntry? { + autoIncrementMaximum.map { + AutoIncrementEntry(text, maximum: $0, original: changeManager.tableOptions.original?.autoIncrement) + } + } + + private static func text(_ counter: UInt64?) -> String { + counter.map { String($0) } ?? "" + } + + // A retried read lands only in the session; the baseline moves unless edits are staged. + private func retryOptions() { + Task { + await session.readTableOptions() + session.rebaselineUnlessEdited() + } + } + + @ViewBuilder + private var stateView: some View { + switch session.tableOptions { + case .idle, .loading: + ProgressView() + .controlSize(.small) + .frame(maxWidth: .infinity, alignment: .leading) + case .failed(let message): + VStack(alignment: .leading, spacing: 6) { + Label(String(localized: "Couldn't read the table options"), systemImage: "exclamationmark.triangle") + RevealedTextView(message) + .foregroundStyle(.secondary) + Button(String(localized: "Try Again"), action: retryOptions) + .accessibilityIdentifier("structure-options-retry") + } + case .loaded: + StructureInspectorFootnote(text: String(localized: "This table has no options to change.")) + } + } + + // MARK: Form + + private func form(_ options: TableOptions) -> some View { + let editability = self.editability + let original = changeManager.tableOptions.original + return VStack(alignment: .leading, spacing: 8) { + VStack(alignment: .leading, spacing: 10) { + StructureInspectorRow( + label: String(localized: "Character Set"), + isModified: draft != nil && options.characterSet != original?.characterSet + ) { + Picker(String(localized: "Character Set"), selection: characterSetBinding) { + if options.characterSet == nil { + Text(String(localized: "Unknown")).tag(String?.none) + } + ForEach(TableOptionsForm.characterSets(in: catalog, keeping: options.characterSet), id: \.self) { + Text($0).tag(Optional($0)) + } + } + .labelsHidden() + .disabled(!editability.isEditable || catalog == nil) + .accessibilityIdentifier("structure-options-charset") + } + + StructureInspectorRow( + label: String(localized: "Collation"), + isModified: draft != nil && options.collation != original?.collation + ) { + Picker(String(localized: "Collation"), selection: collationBinding) { + if options.collation == nil { + Text(String(localized: "Unknown")).tag(String?.none) + } + ForEach(collationChoices(options)) { choice in + Text(choice.title).tag(Optional(choice.name)) + } + } + .labelsHidden() + .disabled(!editability.isEditable || catalog == nil) + .accessibilityIdentifier("structure-options-collation") + } + + Group { + VStack(alignment: .leading, spacing: 4) { + Toggle(String(localized: "Convert existing columns"), isOn: convertBinding) + .toggleStyle(.checkbox) + .fontWeight(options.convertsColumns ? .semibold : .regular) + .disabled(!editability.isEditable) + .accessibilityIdentifier("structure-options-convert") + StructureInspectorFootnote(text: Self.convertFootnote(options.convertsColumns)) + } + } + + catalogStatusRow + + if let maximum = autoIncrementMaximum { + counterRows(maximum: maximum, isEditable: editability.isEditable, original: original) + } + } + + if let reason = editability.unavailableReason, !changeManager.isHeldForSave { + Label(reason, systemImage: "lock") + .font(.caption) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + .accessibilityIdentifier("structure-options-unavailable-note") + } + } + } + + private static func convertFootnote(_ converts: Bool) -> String { + guard converts else { + return String(localized: "Applies to columns added later. Existing columns keep their character set.") + } + return String( + localized: "Rewrites every text column to this character set. TEXT can grow to MEDIUMTEXT, and the table is copied while it runs." + ) + } + + private func collationChoices(_ options: TableOptions) -> [TableOptionsForm.Choice] { + TableOptionsForm.collations(in: catalog, characterSet: options.characterSet, keeping: options.collation) + } + + @ViewBuilder + private var catalogStatusRow: some View { + switch session.tableOptionsCatalog { + case .idle, .loading: + Group { + HStack(spacing: 6) { + ProgressView() + .controlSize(.mini) + StructureInspectorFootnote(text: String(localized: "Reading the server's character sets…")) + } + } + case .failed: + Group { + VStack(alignment: .leading, spacing: 4) { + Label(String(localized: "Couldn't read the server's character sets."), systemImage: "exclamationmark.triangle") + .font(.caption) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + Button(String(localized: "Try Again")) { + Task { await session.readTableOptionsCatalog() } + } + .accessibilityIdentifier("structure-options-catalog-retry") + } + } + case .loaded: + EmptyView() + } + } + + @ViewBuilder + private func counterRows(maximum: UInt64, isEditable: Bool, original: TableOptions?) -> some View { + StructureInspectorRow( + label: String(localized: "Next Auto Increment"), + isModified: draft != nil && draft?.autoIncrement != original?.autoIncrement + ) { + TextField(String(localized: "Next Auto Increment"), text: counterBinding) + .labelsHidden() + .textFieldStyle(.squareBorder) + .focused($isCounterFocused) + .disabled(!isEditable || !isCounterConfirmed) + .accessibilityLabel(String(localized: "Next Auto Increment")) + .accessibilityIdentifier("structure-options-auto-increment") + } + // The server raises a value below the next free one, so a saved counter is read back, never shown as typed. + switch session.autoIncrementRead { + case .rereading: + Group { + HStack(spacing: 6) { + ProgressView() + .controlSize(.mini) + StructureInspectorFootnote(text: String(localized: "Reading the saved value…")) + } + } + case .unconfirmed: + Group { + VStack(alignment: .leading, spacing: 4) { + Label(String(localized: "Couldn't read the saved value."), systemImage: "exclamationmark.triangle") + .font(.caption) + .foregroundStyle(.secondary) + Button(String(localized: "Try Again"), action: retryOptions) + .accessibilityIdentifier("structure-options-auto-increment-retry") + } + } + case .confirmed: + EmptyView() + } + if let message = AutoIncrementEntry(counterText, maximum: maximum, original: original?.autoIncrement).message { + Group { + Text(message) + .font(.caption) + .foregroundStyle(.red) + .fixedSize(horizontal: false, vertical: true) + .accessibilityIdentifier("structure-options-auto-increment-error") + } + } + Group { + StructureInspectorFootnote(text: String(localized: "A value at or below the highest existing ID becomes that ID plus one.")) + } + } + + // MARK: Bindings + + private var characterSetBinding: Binding { + Binding( + get: { displayedOptions?.characterSet }, + set: { name in + guard let name, let current = draft, name != current.characterSet else { return } + changeManager.stageTableOptions(TableOptionsForm.selectingCharacterSet(name, in: current, catalog: catalog)) + } + ) + } + + private var collationBinding: Binding { + Binding( + get: { displayedOptions?.collation }, + set: { name in + guard let name, var next = draft, name != next.collation else { return } + next.collation = name + changeManager.stageTableOptions(next) + } + ) + } + + private var convertBinding: Binding { + Binding( + get: { displayedOptions?.convertsColumns ?? false }, + set: { converts in + guard var next = draft, converts != next.convertsColumns else { return } + next.convertsColumns = converts + changeManager.stageTableOptions(next) + } + ) + } + + // Staged per keystroke, as the comment is, so a save from the keyboard never misses a value still in the field. + // Text that is not a valid counter stages the loaded one and holds Save until it is fixed or cleared. + private var counterBinding: Binding { + Binding( + get: { counterText }, + set: { text in + counterText = text + guard var next = draft, let entry = entry(text) else { return } + switch entry { + case .value(let value): + next.autoIncrement = value + case .empty, .invalid: + next.autoIncrement = changeManager.tableOptions.original?.autoIncrement + } + changeManager.noteAutoIncrementEntry(invalid: entry.message) + guard next != draft else { return } + changeManager.stageTableOptions(next) + } + ) + } +} diff --git a/apple/TablePro/Views/Structure/StructureDDLPane.swift b/apple/TablePro/Views/Structure/StructureDDLPane.swift new file mode 100644 index 0000000000..c6b0e7a544 --- /dev/null +++ b/apple/TablePro/Views/Structure/StructureDDLPane.swift @@ -0,0 +1,133 @@ +// +// StructureDDLPane.swift +// TablePro +// + +import AppKit +import os +import SwiftUI +import UniformTypeIdentifiers + +// The table's CREATE statement as the database reports it, read-only, with the ways to take it elsewhere. +struct StructureDDLPane: View { + private static let logger = Logger(subsystem: "com.TablePro", category: "StructureDDLPane") + + let ddl: String + let tableName: String + let databaseType: DatabaseType + let failure: String? + let isLoading: Bool + let coordinator: MainContentCoordinator? + let onRetry: () -> Void + + @AppStorage("structureCodeFontSize", store: AppStorageEnvironment.shared.defaults) private var fontSize: Double = 13 + + var body: some View { + VStack(spacing: 0) { + HStack(spacing: 8) { + ControlGroup { + Button { + fontSize = max(10, fontSize - 1) + } label: { + Label(String(localized: "Decrease Font Size"), systemImage: "textformat.size.smaller") + } + Button { + fontSize = min(24, fontSize + 1) + } label: { + Label(String(localized: "Increase Font Size"), systemImage: "textformat.size.larger") + } + } + .controlGroupStyle(.navigation) + .labelStyle(.iconOnly) + .fixedSize() + + Spacer() + + Button(String(localized: "Open in Editor"), action: openInEditor) + .disabled(ddl.isEmpty) + Button(String(localized: "Copy"), action: copy) + .disabled(ddl.isEmpty) + Button(String(localized: "Export…"), action: export) + .disabled(ddl.isEmpty) + } + .controlSize(.small) + .padding(.horizontal, 10) + .padding(.vertical, 6) + .paneBarBackground() + + Divider() + + content + } + } + + @ViewBuilder + private var content: some View { + if let failure, ddl.isEmpty { + UnavailableStateView { + Label(String(localized: "Couldn't Read the DDL"), systemImage: "exclamationmark.triangle") + } description: { + Text(failure) + } actions: { + Button(String(localized: "Try Again"), action: onRetry) + } + .frame(maxWidth: .infinity, maxHeight: .infinity) + .valueStandInBackground() + } else if ddl.isEmpty { + StructureDDLStandIn(isLoading: isLoading) + } else { + DDLTextView(ddl: ddl, fontSize: $fontSize, databaseType: databaseType) + } + } + + private func openInEditor() { + guard !ddl.isEmpty else { return } + coordinator?.tabManager.addTab(initialQuery: ddl, title: String(format: String(localized: "%@ DDL"), tableName)) + } + + private func copy() { + ClipboardService.shared.writeText(ddl) + } + + private func export() { + let savePanel = NSSavePanel() + if let sqlType = UTType(filenameExtension: "sql") { + savePanel.allowedContentTypes = [sqlType] + } + savePanel.nameFieldStringValue = "\(tableName).sql" + + guard let window = coordinator?.contentWindow else { return } + let ddl = ddl + savePanel.beginSheetModal(for: window) { response in + guard response == .OK, let url = savePanel.url else { return } + do { + try ddl.write(to: url, atomically: true, encoding: .utf8) + } catch { + Self.logger.error("Failed to export: \(error.publicLogShape, privacy: .public)") + AlertHelper.showErrorSheet( + title: String(localized: "Could not export the schema"), + message: error.localizedDescription, + window: window + ) + } + } + } +} + +// Where the statement would be, on the editor's background. +struct StructureDDLStandIn: View { + var isLoading = false + + var body: some View { + Group { + if isLoading { + ProgressView().controlSize(.small) + } else { + Text(String(localized: "No DDL Available")) + .foregroundStyle(.secondary) + } + } + .frame(maxWidth: .infinity, maxHeight: .infinity) + .valueStandInBackground() + } +} diff --git a/apple/TablePro/Views/Structure/StructureDefinitionEditor.swift b/apple/TablePro/Views/Structure/StructureDefinitionEditor.swift new file mode 100644 index 0000000000..545560b2fc --- /dev/null +++ b/apple/TablePro/Views/Structure/StructureDefinitionEditor.swift @@ -0,0 +1,360 @@ +// +// StructureDefinitionEditor.swift +// TablePro +// + +import Combine +import Foundation +import TableProPluginKit + +// The Definition page's model, shared by the Structure tab and Create Table: one grid per section over one change +// manager, one filter, and what the inspector shows. A row selected in one section clears the others, so the +// inspector always names one thing. +@MainActor +final class StructureDefinitionEditor: ObservableObject { + enum InspectorTarget: Equatable { + case table + case entity(StructureSection, UUID) + case several(StructureSection, Int) + } + + let manager: StructureChangeManager + let context: StructureEditingContext + let referenceMenus: ForeignKeyReferenceMenus + private(set) var adapters: [StructureSection: StructureSectionAdapter] = [:] + + @Published var searchText = "" { + didSet { + guard searchText != oldValue else { return } + refresh() + } + } + @Published private(set) var inspectorTarget: InspectorTarget = .table + @Published private(set) var collapsedSections: Set + @Published var serverSupport = StructureServerSupport.unrestricted { + didSet { + guard serverSupport != oldValue else { return } + // A section the server withdraws leaves no selection behind for the inspector or the commands. + for section in StructureSection.allCases where !offers(section) { + adapter(for: section).model.clearSelection() + } + refresh() + } + } + @Published var isReadOnly = false { + didSet { + guard isReadOnly != oldValue else { return } + refresh() + } + } + // After a save, until the table is read again: an edit made now would stage against what the save replaced. + @Published var isAwaitingBaseline = false { + didSet { + guard isAwaitingBaseline != oldValue else { return } + refresh() + } + } + @Published private(set) var revision = 0 + + var readOnlyReason: String? + + weak var coordinator: MainContentCoordinator? + var moveColumnHandler: ((Int, Int) -> Void)? + var characterSetContextProvider: () -> ColumnCharacterSetMenus.Context = { + ColumnCharacterSetMenus.Context(catalog: .idle, tableCharacterSet: nil) + } + var characterSetCatalogReader: () -> Void = {} + + private let defaults: UserDefaults + private var cancellables: Set = [] + private var isSyncingSelection = false + + init( + manager: StructureChangeManager, + context: StructureEditingContext, + defaults: UserDefaults = AppStorageEnvironment.shared.defaults + ) { + self.manager = manager + self.context = context + self.defaults = defaults + let menus = ForeignKeyReferenceMenus(connectionId: context.connection.id, databaseType: context.databaseType) + menus.schemaName = context.schemaName + referenceMenus = menus + let stored = defaults.stringArray(forKey: Self.collapsedSectionsKey) ?? [] + collapsedSections = Set(stored.compactMap(StructureSection.init(rawValue:))) + for section in StructureSection.allCases { + let adapter = StructureSectionAdapter( + section: section, manager: manager, context: context, referenceMenus: menus, defaults: defaults + ) + adapters[section] = adapter + adapter.editor = self + adapter.model.$selectionRevision + .dropFirst() + .sink { [weak self] _ in self?.selectionDidChange(in: section) } + .store(in: &cancellables) + } + menus.onListsChanged = { [weak self] in self?.revision &+= 1 } + manager.$reloadVersion + .dropFirst() + .receive(on: RunLoop.main) + .sink { [weak self] _ in self?.refresh() } + .store(in: &cancellables) + // A save holds every edit while it runs; the grids close on the hold and open again when it ends. + manager.$heldSave + .map { $0 != nil } + .removeDuplicates() + .dropFirst() + .receive(on: RunLoop.main) + .sink { [weak self] _ in self?.refresh() } + .store(in: &cancellables) + } + + static let collapsedSectionsKey = "structure.collapsedSections" + + // MARK: - Sections + + var sections: [StructureSection] { + StructureSection.allCases.filter(offers) + } + + func offers(_ section: StructureSection) -> Bool { + switch section { + case .columns, .indexes: + return true + case .foreignKeys: + return context.databaseType.supportsForeignKeys + case .checkConstraints: + guard serverSupport.offers(.checkConstraints) else { return false } + guard !context.isCreatingTable else { return context.allows(.addCheckConstraint) } + return context.databaseType.supportsCheckConstraints + } + } + + func adapter(for section: StructureSection) -> StructureSectionAdapter { + guard let adapter = adapters[section] else { + preconditionFailure("Every section has an adapter") + } + return adapter + } + + func isExpanded(_ section: StructureSection) -> Bool { + !collapsedSections.contains(section) || !searchText.isEmpty + } + + func setExpanded(_ section: StructureSection, _ isExpanded: Bool) { + if isExpanded { + collapsedSections.remove(section) + } else { + collapsedSections.insert(section) + adapter(for: section).model.clearSelection() + } + defaults.set(collapsedSections.map(\.rawValue).sorted(), forKey: Self.collapsedSectionsKey) + } + + func count(of section: StructureSection) -> Int { + let ids = StructureGridEdits.entityIDs(in: section, manager: manager) + return ids.indices.filter { !manager.deleteInsertState(for: $0, tab: section).isDeleted }.count + } + + var characterSetContext: ColumnCharacterSetMenus.Context { characterSetContextProvider() } + + func readCharacterSetCatalogIfNeeded() { + characterSetCatalogReader() + } + + // MARK: - Refresh + + func refresh() { + for section in StructureSection.allCases { + adapter(for: section).refresh() + } + settleInspectorTarget() + revision &+= 1 + } + + func definitionsDidChange() { + refresh() + } + + // One field of one definition changed, which no other section shows: the cheap refresh a keystroke can afford. + func definitionsDidChange(in section: StructureSection) { + adapter(for: section).refresh() + settleInspectorTarget() + revision &+= 1 + } + + // MARK: - Selection + + var focusedSection: StructureSection? { + switch inspectorTarget { + case .table: nil + case .entity(let section, _), .several(let section, _): section + } + } + + var focusedModel: DataGridModel? { + focusedSection.map { adapter(for: $0).model } + } + + private func selectionDidChange(in section: StructureSection) { + guard !isSyncingSelection else { return } + let model = adapter(for: section).model + if !model.selection.isEmpty { + isSyncingSelection = true + for other in StructureSection.allCases where other != section { + adapter(for: other).model.clearSelection() + } + isSyncingSelection = false + } + settleInspectorTarget() + } + + private func settleInspectorTarget() { + var target = InspectorTarget.table + for section in StructureSection.allCases { + let ids = adapter(for: section).selectedEntityIDs + guard !ids.isEmpty else { continue } + target = ids.count == 1 ? .entity(section, ids[0]) : .several(section, ids.count) + break + } + if target != inspectorTarget { + inspectorTarget = target + } + } + + func clearSelection() { + for section in StructureSection.allCases { + adapter(for: section).model.clearSelection() + } + } + + // A filter that hides the definition is cleared first, or the selection would land on nothing. + func reveal(section: StructureSection, entity id: UUID) { + if collapsedSections.contains(section) { + setExpanded(section, true) + } + let adapter = adapter(for: section) + if adapter.model.displayIndex(of: adapter.rowID(forEntity: id)) == nil { + if !searchText.isEmpty { searchText = "" } + if adapter.store.valueFilter.isActive { adapter.model.setValueFilter(GridValueFilterState()) } + } + adapter.select(entity: id) + // The section may have just expanded, so its grid scrolls the page once it is on screen. + let rowID = adapter.rowID(forEntity: id) + DispatchQueue.main.async { adapter.model.reveal(rowID: rowID) } + } + + // MARK: - Commands + + func add(_ section: StructureSection) { + adapter(for: section).addRow() + } + + func canAdd(_ section: StructureSection) -> Bool { + let adapter = adapter(for: section) + return adapter.canEdit && adapter.canAdd + } + + func canRemoveSelection(in section: StructureSection) -> Bool { + let adapter = adapter(for: section) + return adapter.canEdit && adapter.canRemove && !adapter.model.selection.isEmpty + } + + func removeSelection(in section: StructureSection) { + let adapter = adapter(for: section) + let rowIDs = adapter.model.selection.affectedRows.compactMap { adapter.model.rowID(at: $0) } + adapter.deleteRows(rowIDs) + } + + func refusal(adding section: StructureSection) -> String? { + let adapter = adapter(for: section) + if let reason = adapter.editRefusal { return reason } + return context.refusal(context.addOperation(for: section)) + } + + // The section the menu bar's row commands act on: the one holding the selection, else Columns. + var commandSection: StructureSection { focusedSection ?? .columns } + + func selectedRowIDs(in section: StructureSection) -> [RowID] { + let model = adapter(for: section).model + return model.selection.affectedRows.compactMap { model.rowID(at: $0) } + } + + var canDuplicateSelection: Bool { + guard let section = focusedSection else { return false } + return canAdd(section) && !selectedRowIDs(in: section).isEmpty + } + + func duplicateSelection() { + guard canDuplicateSelection, let section = focusedSection else { return } + adapter(for: section).duplicateRows(selectedRowIDs(in: section)) + } + + func copySelection() { + guard let section = focusedSection else { return } + let rowIDs = selectedRowIDs(in: section) + guard !rowIDs.isEmpty else { return } + adapter(for: section).copyRows(rowIDs) + } + + var canPaste: Bool { + canAdd(commandSection) && StructurePasteboard.canPaste + } + + func paste() { + guard canPaste else { return } + adapter(for: commandSection).pasteRows() + } + + func moveColumn(from sourceIndex: Int, to destination: Int) { + moveColumnHandler?(sourceIndex, destination) + } + + // MARK: - Open edits + + // Commits an edit open in a grid cell, and ends a run of typing in the inspector, before a command acts. + func commitPendingEdits() { + manager.endTypingRun() + for section in StructureSection.allCases { + adapter(for: section).model.commitEditing() + } + } + + // MARK: - Inspector + + func entity(_ section: StructureSection, id: UUID) -> Int? { + StructureGridEdits.sourceIndex(of: id, in: section, manager: manager) + } + + func inspectorForm(for section: StructureSection, id: UUID) -> StructureInspectorForm? { + let adapter = adapter(for: section) + guard let sourceIndex = entity(section, id: id) else { return nil } + return StructureInspectorForm.build(adapter: adapter, sourceIndex: sourceIndex, entityID: id) + } + + // A pick, a checkbox or a menu choice: one change, one undo step. + func commitInspectorField(section: StructureSection, id: UUID, field: Int, value: String) { + manager.endTypingRun() + adapter(for: section).applyEdit(entity: id, field: field, text: value) + } + + // A keystroke in an inspector field, staged at once and undone with the rest of its run. + func typeInspectorField(section: StructureSection, id: UUID, field: Int, value: String) { + manager.stageTyping(in: "\(section.rawValue).\(id.uuidString).\(field)", actionName: Self.editActionName(section)) { + adapter(for: section).applyEdit(entity: id, field: field, text: value) + } + } + + func endInspectorTyping() { + manager.endTypingRun() + } + + private static func editActionName(_ section: StructureSection) -> String { + switch section { + case .columns: String(localized: "Edit Column") + case .indexes: String(localized: "Edit Index") + case .foreignKeys: String(localized: "Edit Foreign Key") + case .checkConstraints: String(localized: "Edit Check Constraint") + } + } +} diff --git a/apple/TablePro/Views/Structure/StructureDefinitionPage.swift b/apple/TablePro/Views/Structure/StructureDefinitionPage.swift new file mode 100644 index 0000000000..542df40dd0 --- /dev/null +++ b/apple/TablePro/Views/Structure/StructureDefinitionPage.swift @@ -0,0 +1,242 @@ +// +// StructureDefinitionPage.swift +// TablePro +// + +import AppKit +import SwiftUI + +// Columns, indexes, foreign keys and check constraints on one scrolling page, each section with its own add and +// remove beneath it, the way Xcode's model editor stacks an entity's parts. The selected definition's full form is +// in the inspector. +struct StructureDefinitionPage: View { + @ObservedObject var editor: StructureDefinitionEditor + let loadState: StructureLoadState + var isLoading = false + var notes: [StructureSection: [StructureSectionNote]] = [:] + var header: AnyView? + var onRetry: (StructureSection) -> Void = { _ in } + + var body: some View { + VStack(spacing: 0) { + filterBar + Divider() + if isLoading { + ProgressView() + .controlSize(.small) + .frame(maxWidth: .infinity, maxHeight: .infinity) + .gridStandInBackground() + } else { + ScrollView(.vertical) { + VStack(alignment: .leading, spacing: 0) { + if let header { + header + } + ForEach(editor.sections, id: \.self) { section in + StructureSectionView( + section: section, + editor: editor, + model: editor.adapter(for: section).model, + failure: loadState.failure(StructureLoadState.Item(section)), + hasData: editor.context.isCreatingTable || loadState.hasData(StructureLoadState.Item(section)), + notes: notes[section] ?? [], + onRetry: { onRetry(section) } + ) + } + } + .frame(maxWidth: .infinity, alignment: .leading) + } + .gridStandInBackground() + } + } + } + + private var filterBar: some View { + NativeSearchField( + text: $editor.searchText, + placeholder: String(localized: "Filter"), + accessibilityIdentifier: "structure-filter" + ) + .padding(.horizontal, 8) + .padding(.vertical, 6) + } +} + +struct StructureSectionNote: Equatable, Identifiable { + let systemImage: String + let text: String + let identifier: String + + var id: String { identifier } +} + +struct StructureSectionView: View { + let section: StructureSection + @ObservedObject var editor: StructureDefinitionEditor + @ObservedObject var model: DataGridModel + let failure: String? + let hasData: Bool + let notes: [StructureSectionNote] + let onRetry: () -> Void + + private var adapter: StructureSectionAdapter { editor.adapter(for: section) } + private var isExpanded: Bool { editor.isExpanded(section) } + + var body: some View { + VStack(alignment: .leading, spacing: 0) { + sectionHeader + if isExpanded { + content + } + Divider() + } + .accessibilityElement(children: .contain) + .accessibilityIdentifier("structure-section-\(section.rawValue)") + } + + private var sectionHeader: some View { + HStack(spacing: 6) { + DisclosureTriangle( + isExpanded: Binding( + get: { isExpanded }, + set: { editor.setExpanded(section, $0) } + ), + label: section.displayName + ) + Text(section.displayName) + .font(.headline) + if hasData { + Text(editor.count(of: section), format: .number) + .foregroundStyle(.secondary) + .monospacedDigit() + } + Spacer(minLength: 0) + } + .padding(.horizontal, 10) + .padding(.top, 10) + .padding(.bottom, 6) + .contentShape(Rectangle()) + .onTapGesture { editor.setExpanded(section, !isExpanded) } + .accessibilityElement(children: .combine) + .accessibilityAddTraits(.isHeader) + } + + @ViewBuilder + private var content: some View { + if let failure, !hasData { + failureRow(failure) + } else { + if let failure { + failureRow(failure) + } + if adapter.visibleRowCount > 0 { + DataGridView(model: model) + } else { + emptyRow + } + ForEach(notes) { note in + Label(note.text, systemImage: note.systemImage) + .font(.callout) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + .padding(.horizontal, 10) + .padding(.top, 6) + .accessibilityIdentifier(note.identifier) + } + footer + } + } + + private var emptyRow: some View { + Text(emptyText) + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, alignment: .leading) + .padding(.horizontal, 10) + .padding(.vertical, 6) + } + + private var emptyText: String { + guard editor.searchText.isEmpty else { return String(localized: "No Matches") } + switch section { + case .columns: return String(localized: "No Columns") + case .indexes: return String(localized: "No Indexes") + case .foreignKeys: return String(localized: "No Foreign Keys") + case .checkConstraints: return String(localized: "No Check Constraints") + } + } + + private func failureRow(_ message: String) -> some View { + HStack(alignment: .firstTextBaseline, spacing: 8) { + Image(systemName: "exclamationmark.triangle") + .foregroundStyle(.secondary) + .accessibilityHidden(true) + Text(message) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + .textSelection(.enabled) + Spacer(minLength: 0) + Button(String(localized: "Try Again"), action: onRetry) + .controlSize(.small) + } + .padding(.horizontal, 10) + .padding(.vertical, 6) + } + + private var footer: some View { + let labels = StructureFooterPolicy.labels(for: section) + let reason = editor.refusal(adding: section) + return AddRemoveControlGroup( + addLabel: labels.add, + removeLabel: labels.remove, + canAdd: editor.canAdd(section), + canRemove: editor.canRemoveSelection(in: section), + addHelp: reason, + removeHelp: reason, + addIdentifier: "structure-\(section.rawValue)-add", + removeIdentifier: "structure-\(section.rawValue)-remove", + onAdd: { editor.add(section) }, + onRemove: { editor.removeSelection(in: section) } + ) + .padding(.horizontal, 10) + .padding(.top, 6) + .padding(.bottom, 10) + } +} + +// The stock disclosure triangle, the control the HIG names for showing and hiding a section's details. +struct DisclosureTriangle: NSViewRepresentable { + @Binding var isExpanded: Bool + let label: String + + func makeNSView(context: Context) -> NSButton { + let button = NSButton(title: "", target: context.coordinator, action: #selector(Coordinator.toggle(_:))) + button.bezelStyle = .disclosure + button.setButtonType(.pushOnPushOff) + button.imagePosition = .imageOnly + button.setContentHuggingPriority(.required, for: .horizontal) + return button + } + + func updateNSView(_ button: NSButton, context: Context) { + context.coordinator.parent = self + button.state = isExpanded ? .on : .off + button.setAccessibilityLabel(label) + } + + func makeCoordinator() -> Coordinator { + Coordinator(parent: self) + } + + @MainActor + final class Coordinator: NSObject { + var parent: DisclosureTriangle + + init(parent: DisclosureTriangle) { + self.parent = parent + } + + @objc func toggle(_ sender: NSButton) { + parent.isExpanded = sender.state == .on + } + } +} diff --git a/apple/TablePro/Views/Structure/StructureEditGate.swift b/apple/TablePro/Views/Structure/StructureEditGate.swift index 21878ef68f..c8ac61e248 100644 --- a/apple/TablePro/Views/Structure/StructureEditGate.swift +++ b/apple/TablePro/Views/Structure/StructureEditGate.swift @@ -117,21 +117,18 @@ struct StructureEditGate { && TriggerEditEligibility.kindAcceptsTriggers(objectKind) } - func locksField(at index: Int, on tab: StructureTab, orderedFields: [StructureColumnField]) -> Bool { + func locksField(at index: Int, on tab: StructureSection, orderedFields: [StructureColumnField]) -> Bool { switch tab { case .columns: guard orderedFields.indices.contains(index) else { return false } return !editableColumnFields.contains(orderedFields[index]) case .indexes, .foreignKeys, .checkConstraints: - guard let adding = StructureFooterPolicy.operation(forAdding: tab) else { return false } - return !allows(adding) - case .options, .ddl, .parts, .triggers: - return false + return !allows(StructureFooterPolicy.operation(forAdding: tab)) } } func lockedFieldIndices( - on tab: StructureTab, + on tab: StructureSection, orderedFields: [StructureColumnField], fieldCount: Int ) -> Set { diff --git a/apple/TablePro/Views/Structure/StructureEditingContext.swift b/apple/TablePro/Views/Structure/StructureEditingContext.swift new file mode 100644 index 0000000000..34aa6bf230 --- /dev/null +++ b/apple/TablePro/Views/Structure/StructureEditingContext.swift @@ -0,0 +1,97 @@ +// +// StructureEditingContext.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// The table a structure editor works on: an existing one, or the one Create Table is drafting. +@MainActor +struct StructureEditingContext { + let connection: DatabaseConnection + let databaseName: String + let schemaName: String? + let tableName: String + let objectKind: TableInfo.TableType + let isSessionBound: Bool + let isCreatingTable: Bool + + static func existingTable( + connection: DatabaseConnection, + databaseName: String, + schemaName: String?, + tableName: String, + objectKind: TableInfo.TableType, + isSessionBound: Bool + ) -> StructureEditingContext { + StructureEditingContext( + connection: connection, + databaseName: databaseName, + schemaName: schemaName, + tableName: tableName, + objectKind: objectKind, + isSessionBound: isSessionBound, + isCreatingTable: false + ) + } + + static func newTable(connection: DatabaseConnection, databaseName: String, schemaName: String?) -> StructureEditingContext { + StructureEditingContext( + connection: connection, + databaseName: databaseName, + schemaName: schemaName, + tableName: "", + objectKind: .table, + isSessionBound: false, + isCreatingTable: true + ) + } + + var databaseType: DatabaseType { connection.type } + + var scope: DatabaseScope { + DatabaseScope(connectionId: connection.id, database: databaseName, schema: schemaName, isSessionBound: isSessionBound) + } + + var editGate: StructureEditGate { + StructureEditGate(databaseType: connection.type, objectKind: objectKind, schema: schemaName, isSessionBound: isSessionBound) + } + + // A drafted table is created in one run, so every definition it can hold is open; an existing one asks the gate. + // Its checks are added after the CREATE, so they need an engine that adds one to an existing table. + func allows(_ operation: StructureEditOperation) -> Bool { + guard isCreatingTable else { return editGate.allows(operation) } + switch operation { + case .addForeignKey, .dropForeignKey: + return connection.type.supportsForeignKeys + case .addCheckConstraint, .dropCheckConstraint: + return connection.type.supportsCheckConstraintEditing + default: + return true + } + } + + func refusal(_ operation: StructureEditOperation) -> String? { + guard !isCreatingTable else { return nil } + return editGate.resolve(operation).unavailableReason + } + + func addOperation(for section: StructureSection) -> StructureEditOperation { + switch section { + case .columns: .addColumn + case .indexes: .addIndex + case .foreignKeys: .addForeignKey + case .checkConstraints: .addCheckConstraint + } + } + + func removeOperation(for section: StructureSection) -> StructureEditOperation { + switch section { + case .columns: .dropColumn + case .indexes: .dropIndex + case .foreignKeys: .dropForeignKey + case .checkConstraints: .dropCheckConstraint + } + } +} diff --git a/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift b/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift index 441fe58031..c2f9c83325 100644 --- a/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift +++ b/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift @@ -88,6 +88,7 @@ internal extension StructureEditingSession { /// mounted structure view ever fills. Answering Save from anywhere else ran nothing, reported /// success, and closed the tab over the work. func applyStagedChanges(coordinator: MainContentCoordinator?) async -> StructureSaveOutcome { + editor.commitPendingEdits() /// Asked before Safe Mode, so a tab with nothing staged is never refused. `.refused` stands /// the close down, and refusing a save that had no work to do would leave the user unable to /// close the tab through Save at all. diff --git a/apple/TablePro/Views/Structure/StructureEditingSession+Baseline.swift b/apple/TablePro/Views/Structure/StructureEditingSession+Baseline.swift deleted file mode 100644 index 746544fc03..0000000000 --- a/apple/TablePro/Views/Structure/StructureEditingSession+Baseline.swift +++ /dev/null @@ -1,44 +0,0 @@ -// -// StructureEditingSession+Baseline.swift -// TablePro -// - -import Foundation -import TableProPluginKit - -internal extension StructureEditingSession { - // PostgreSQL leaves `isPrimaryKey` false on every column, so the primary index decides first. - func adoptLoadedBaseline() { - serverSupport = StructureServerSupport.forConnection(connection.id) - let pkFromIndexes = indexes.first(where: { $0.isPrimary })?.columns ?? [] - let pkFromColumns = columns.filter { $0.isPrimaryKey }.map { $0.name } - let primaryKey = pkFromIndexes.isEmpty ? pkFromColumns : pkFromIndexes - - changeManager.loadSchema( - tableName: tableName, - columns: columns, - indexes: indexes, - foreignKeys: foreignKeys, - checkConstraints: checkConstraints, - primaryKey: primaryKey, - tableComment: loadedTableComment, - tableOptions: loadedTableOptions, - autoIncrementConfirmed: autoIncrementRead == .confirmed - ) - } - - // Reads what the Structure tab's first mount reads, so an edit staged without the tab composes - // the statements the tab would. - func loadEditingBaseline(provider: any ScopedMetadataProviding = DatabaseManager.shared) async throws { - let loader = TableStructureLoader(scope: scope, tableName: tableName, provider: provider) - serverSupport = StructureServerSupport.forConnection(connection.id) - columns = try await loader.columns() - indexes = try await loader.indexes() - foreignKeys = try await loader.foreignKeys() - if availableTabs.contains(.checkConstraints) { - checkConstraints = try await loader.checkConstraints() - } - adoptLoadedBaseline() - hasLoaded = true - } -} diff --git a/apple/TablePro/Views/Structure/TableStructureView+ColumnReorder.swift b/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift similarity index 93% rename from apple/TablePro/Views/Structure/TableStructureView+ColumnReorder.swift rename to apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift index 8ae123566d..30c1ff93cc 100644 --- a/apple/TablePro/Views/Structure/TableStructureView+ColumnReorder.swift +++ b/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift @@ -1,17 +1,16 @@ // -// TableStructureView+ColumnReorder.swift +// StructureEditingSession+ColumnReorder.swift // TablePro // -import Combine import Foundation -import SwiftUI import TableProPluginKit import TableProSQLGrammar -extension TableStructureView { +// Applies now, not staged: a reorder is offered only while nothing else is staged. +internal extension StructureEditingSession { func beginColumnReorder(fromIndex: Int, toIndex: Int) { - let columnsSnapshot = structureChangeManager.workingColumns + let columnsSnapshot = changeManager.workingColumns let clearTarget = coordinator?.selectedColumnLayoutClearTarget() let reorderScope = scope @@ -59,7 +58,7 @@ extension TableStructureView { action: TableRebuildReviewRequest.Action( title: String(localized: "Rebuild Table"), operationDescription: StructureColumnReorderHandler.operationDescription - ) { + ) { [self] in do { try await StructureColumnReorderHandler.execute( prepared, tableName: tableName, databaseType: connection.type, isConfirmationPreCleared: true @@ -88,7 +87,7 @@ extension TableStructureView { _ prepared: StructureColumnReorderHandler.PreparedReorder, clearTarget: ColumnLayoutClearTarget? ) async { - await services.queryHistoryManager.record( + await QueryHistoryManager.shared.record( QueryHistoryRecordRequest( query: prepared.plan.scriptStatements .map { $0.hasSuffix(";") ? $0 : $0 + ";" } @@ -103,7 +102,7 @@ extension TableStructureView { ) ) isReloadingAfterSave = true - await loadColumns() + await fetch(.columns) adoptFetchedBaseline() isReloadingAfterSave = false if let clearTarget { diff --git a/apple/TablePro/Views/Structure/StructureEditingSession+Commands.swift b/apple/TablePro/Views/Structure/StructureEditingSession+Commands.swift new file mode 100644 index 0000000000..66f98a8b10 --- /dev/null +++ b/apple/TablePro/Views/Structure/StructureEditingSession+Commands.swift @@ -0,0 +1,132 @@ +// +// StructureEditingSession+Commands.swift +// TablePro +// + +import AppKit +import Foundation + +// What the menu bar, the toolbar and the keyboard ask of a structure tab. The coordinator calls these on the +// selected tab's session, so they work whether or not a view is mounted. +internal extension StructureEditingSession { + var hasChanges: Bool { changeManager.hasChanges } + + var pendingChangeCount: Int { changeManager.getChangesArray().count } + + // MARK: - Undo + + var canUndo: Bool { changeManager.canUndo } + var canRedo: Bool { changeManager.canRedo } + var undoMenuItemTitle: String { changeManager.undoMenuItemTitle } + var redoMenuItemTitle: String { changeManager.redoMenuItemTitle } + + func undo() { + guard canUndo else { return } + commitOpenEdits() + changeManager.undo() + editor.refresh() + } + + func redo() { + guard canRedo else { return } + commitOpenEdits() + changeManager.redo() + editor.refresh() + } + + private func commitOpenEdits() { + editor.commitPendingEdits() + } + + // MARK: - Rows + + private var takesRowCommands: Bool { pane == .definition && hasBaseline } + + var canAddRow: Bool { takesRowCommands && editor.canAdd(editor.commandSection) } + var canRemoveRows: Bool { takesRowCommands && editor.canRemoveSelection(in: editor.commandSection) } + var canPasteRows: Bool { takesRowCommands && editor.canPaste } + + func addRow() { + guard canAddRow else { return } + editor.add(editor.commandSection) + } + + func removeSelectedRows() { + guard canRemoveRows else { return } + editor.removeSelection(in: editor.commandSection) + } + + func duplicateSelectedRows() { + guard takesRowCommands else { return } + editor.duplicateSelection() + } + + func copySelectedRows() { + guard pane == .definition else { return } + editor.copySelection() + } + + func pasteRows() { + guard canPasteRows else { return } + editor.paste() + } + + // MARK: - Discard + + func discardChanges() { + for section in StructureSection.allCases { + editor.adapter(for: section).model.discardEditing() + } + changeManager.discardChanges() + editor.refresh() + if settleOwedRefetch() { + Task { await load() } + } + } + + func discardChangesAfterConfirming() { + guard hasChanges else { return } + let count = pendingChangeCount + Task { + let message = count == 1 + ? String(localized: "The change to this table's structure will be lost.") + : String(format: String(localized: "%d changes to this table's structure will be lost."), count) + guard await confirmDiscard(message: message) else { return } + discardChanges() + } + } + + func confirmDiscard(message: String) async -> Bool { + await AlertHelper.confirmDestructive( + title: String(localized: "Discard Changes?"), + message: message, + confirmButton: String(localized: "Discard Changes"), + cancelButton: String(localized: "Cancel"), + window: coordinator?.contentWindow + ) + } + + // MARK: - Preview and save + + // Preview checks what Save checks, so the statements it shows are the ones Save would run. + func previewChanges() { + editor.commitPendingEdits() + guard hasChanges, let coordinator else { + coordinator?.toolbarState.previewStatements = [] + return + } + guard changeManager.canCommit else { + AlertHelper.showErrorSheet( + title: String(localized: "Some Changes Are Incomplete"), + message: changeManager.validationSummary, + window: coordinator.contentWindow + ) + return + } + Task { await previewStagedChanges(coordinator: coordinator) } + } + + func save() { + Task { _ = await applyStagedChanges(coordinator: coordinator) } + } +} diff --git a/apple/TablePro/Views/Structure/StructureEditingSession+Loading.swift b/apple/TablePro/Views/Structure/StructureEditingSession+Loading.swift new file mode 100644 index 0000000000..7b08238b0c --- /dev/null +++ b/apple/TablePro/Views/Structure/StructureEditingSession+Loading.swift @@ -0,0 +1,298 @@ +// +// StructureEditingSession+Loading.swift +// TablePro +// + +import Foundation +import os +import TableProPluginKit + +internal extension StructureEditingSession { + var loader: TableStructureLoader { + TableStructureLoader(scope: scope, tableName: tableName) + } + + var offersTableComment: Bool { + DatabaseObjectToolEligibility.Support.of(DatabaseManager.shared.driver(for: connection.id)) + .commentableTypes.contains(objectKind) + } + + // A failed read ends the wait, so its section can show the error and Try Again; edits stay closed until a baseline. + var isLoadingFirstTime: Bool { + !hasBaseline && !editor.sections.contains { loadState.failure(StructureLoadState.Item($0)) != nil } + } + + // Idempotent: a pane re-added after a connection switch restarts this, and only what is missing is read. + func load() async { + settleOwedRefetch() + guard !hasLoaded else { + await reloadConcurrentRefreshAvailability() + await readTableOptionsCatalogIfNeeded() + await loadPaneIfNeeded(pane) + return + } + serverSupport = StructureServerSupport.forConnection(connection.id) + await fetchDefinitionsNeedingFetch() + guard !Task.isCancelled else { return } + await loadTableComment() + guard !Task.isCancelled else { return } + await readTableOptions() + guard !Task.isCancelled else { return } + adoptIfCurrent() + await reloadConcurrentRefreshAvailability() + await readTableOptionsCatalogIfNeeded() + await loadPaneIfNeeded(pane) + } + + // Every offered section read since the last change; a baseline built from less would stage against a guess. + var definitionsAreCurrent: Bool { + editor.sections.allSatisfy { !loadState.needsFetch(StructureLoadState.Item($0)) } + } + + func fetchDefinitionsNeedingFetch() async { + for section in editor.sections where loadState.needsFetch(StructureLoadState.Item(section)) { + guard !Task.isCancelled else { return } + await fetch(StructureLoadState.Item(section)) + } + } + + // A partial read still shows what it got, read-only, so a failed section never hides the ones that loaded. + func adoptIfCurrent() { + guard definitionsAreCurrent else { + if !changeManager.hasChanges { loadDefinitionsIntoManager() } + return + } + hasLoaded = true + adoptFetchedBaseline() + } + + func loadPaneIfNeeded(_ pane: StructurePane) async { + switch pane { + case .triggers: + guard loadState.needsFetch(.triggers) else { return } + await fetch(.triggers) + case .ddl: + guard loadState.needsFetch(.ddl) else { return } + await fetch(.ddl) + case .definition, .parts: + return + } + } + + func fetch(_ item: StructureLoadState.Item) async { + do { + switch item { + case .columns: + columns = try await loader.columns() + case .indexes: + indexes = try await loader.indexes() + case .foreignKeys: + foreignKeys = try await loader.foreignKeys() + case .checkConstraints: + checkConstraints = try await loader.checkConstraints() + case .triggers: + triggers = try await loader.triggers() + case .ddl: + let table = tableName + let schema = scope.schema + ddlStatement = try await loader.perform { driver in + try await TableDDLComposer.fetchDDL( + for: table, using: driver, includesDependencies: true, schema: schema + ) + } + } + loadState.markFetched(item) + } catch { + guard let message = StructureFetchFailure(error, taskIsCancelled: Task.isCancelled).message else { return } + Self.logger.error("Failed to load \(String(describing: item), privacy: .public): \(error.publicLogShape, privacy: .public)") + loadState.markFailed(item, message: message) + } + } + + func retry(_ item: StructureLoadState.Item) { + Task { + switch item { + case .columns, .indexes, .foreignKeys, .checkConstraints: + await fetchDefinitionsNeedingFetch() + adoptIfCurrent() + case .triggers, .ddl: + await fetch(item) + } + } + } + + func loadTableComment() async { + guard offersTableComment else { return } + beginTableCommentLoad() + let outcome: MetadataFetchOutcome + do { + outcome = .fetched(try await loader.tableComment()) + } catch { + switch StructureFetchFailure(error, taskIsCancelled: Task.isCancelled) { + case .cancelled: + outcome = .cancelled + case .failed(let message): + Self.logger.error("Failed to load the table comment: \(error.publicLogShape, privacy: .public)") + outcome = .failed(message) + } + } + settleTableComment(outcome) + } + + func readTableOptionsCatalogIfNeeded() async { + guard needsTableOptionsCatalog else { return } + await readTableOptionsCatalog() + } + + // MARK: - Baseline + + func adoptLoadedBaseline() { + loadDefinitionsIntoManager() + hasBaseline = true + editor.isAwaitingBaseline = false + } + + // PostgreSQL leaves `isPrimaryKey` false on every column, so the primary index decides first. + private func loadDefinitionsIntoManager() { + let pkFromIndexes = indexes.first(where: { $0.isPrimary })?.columns ?? [] + let pkFromColumns = columns.filter { $0.isPrimaryKey }.map { $0.name } + let primaryKey = pkFromIndexes.isEmpty ? pkFromColumns : pkFromIndexes + + changeManager.loadSchema( + tableName: tableName, + columns: columns, + indexes: indexes, + foreignKeys: foreignKeys, + checkConstraints: checkConstraints, + primaryKey: primaryKey, + tableComment: loadedTableComment, + tableOptions: loadedTableOptions, + autoIncrementConfirmed: autoIncrementRead == .confirmed + ) + editor.refresh() + } + + func adoptFetchedBaseline() { + guard !changeManager.hasChanges else { + markStructureStale() + return + } + adoptLoadedBaseline() + } + + func rebaselineUnlessEdited() { + guard !isReloadingAfterSave, hasLoaded else { return } + adoptFetchedBaseline() + } + + // Reads what the Structure tab's first mount reads, so an edit staged without the tab composes the statements + // the tab would. + func loadEditingBaseline(provider: any ScopedMetadataProviding = DatabaseManager.shared) async throws { + let loader = TableStructureLoader(scope: scope, tableName: tableName, provider: provider) + serverSupport = StructureServerSupport.forConnection(connection.id) + columns = try await loader.columns() + indexes = try await loader.indexes() + foreignKeys = try await loader.foreignKeys() + if editor.offers(.checkConstraints) { + checkConstraints = try await loader.checkConstraints() + } + adoptLoadedBaseline() + hasLoaded = true + } + + // MARK: - Reload + + func reloadDefinitions() async { + let includesForeignKeys = editor.offers(.foreignKeys) + let offersTableOptions = offersTableOptionsTab + do { + let reloaded = try await loader.coreTabs( + includingForeignKeys: includesForeignKeys, + includingTableComment: offersTableComment, + includingTableOptions: offersTableOptions + ) + if let comment = reloaded.tableComment { + settleTableComment(comment) + } + // A save never adopts the counter it wrote: the server raises a low value, so this read is what shows. + if let options = reloaded.tableOptions { + settleTableOptions(options) + } + columns = reloaded.columns + indexes = reloaded.indexes + loadState.markFetched(.columns) + loadState.markFetched(.indexes) + if includesForeignKeys { + foreignKeys = reloaded.foreignKeys + loadState.markFetched(.foreignKeys) + } + } catch { + if offersTableOptions { + settleTableOptions(.cancelled) + } + if let message = StructureFetchFailure(error, taskIsCancelled: Task.isCancelled).message { + Self.logger.error("Failed to reload structure: \(error.publicLogShape, privacy: .public)") + loadState.markFailed(.columns, message: message) + } + } + if editor.offers(.checkConstraints) { + await fetch(.checkConstraints) + } + await reloadConcurrentRefreshAvailability() + } + + // A failed read keeps the save's tables closed to edits until Try Again reads them. + func refreshAfterApply() async { + isReloadingAfterSave = true + await reloadDefinitions() + isReloadingAfterSave = false + adoptIfCurrent() + await loadPaneIfNeeded(pane) + } + + // Refresh reads everything again; staged edits are discarded first, after asking. + func refresh() { + guard !isReloadingAfterSave else { return } + let justSaved = lastAppliedAt.map { Date().timeIntervalSince($0) < 2.0 } ?? false + guard changeManager.hasChanges, !justSaved else { + Task { await reloadEverything() } + return + } + Task { + guard await confirmDiscard( + message: String(localized: "Refreshing reads the table structure again and discards the changes you have not saved.") + ) else { return } + discardChanges() + await reloadEverything() + } + } + + private func reloadEverything() async { + markEveryTabStale() + editor.referenceMenus.invalidateTableLists() + partsReloadToken &+= 1 + await reloadDefinitions() + adoptIfCurrent() + await loadPaneIfNeeded(pane) + } +} + +internal enum StructureFetchFailure: Equatable { + case cancelled + case failed(String) + + internal init(_ error: any Error, taskIsCancelled: Bool) { + if error is CancellationError || taskIsCancelled { + self = .cancelled + } else { + self = .failed(error.localizedDescription) + } + } + + internal var message: String? { + switch self { + case .cancelled: nil + case .failed(let message): message + } + } +} diff --git a/apple/TablePro/Views/Structure/StructureEditingSession.swift b/apple/TablePro/Views/Structure/StructureEditingSession.swift index 5ff284e764..5e4cd04c59 100644 --- a/apple/TablePro/Views/Structure/StructureEditingSession.swift +++ b/apple/TablePro/Views/Structure/StructureEditingSession.swift @@ -8,6 +8,8 @@ import Foundation import os import TableProPluginKit +// One table tab's structure: what was read, what is staged, and the commands that act on it. The coordinator +// holds it by tab id, so staged edits, the close prompt and Save work with no view mounted. @MainActor internal final class StructureEditingSession: ObservableObject { static let logger = Logger(subsystem: "com.TablePro", category: "StructureEditingSession") @@ -23,12 +25,7 @@ internal final class StructureEditingSession: ObservableObject { internal let objectKind: TableInfo.TableType internal let changeManager = StructureChangeManager() - - // Owned here, not by the view, so the grid the user types into and the edits the close prompt - // reports are one object, and its model outlives every remount. - internal let gridAdapter: StructureGridAdapter - - internal var gridModel: DataGridModel { gridAdapter.model } + internal let editor: StructureDefinitionEditor internal var scope: DatabaseScope { DatabaseScope( @@ -42,7 +39,7 @@ internal final class StructureEditingSession: ObservableObject { @Published internal var checkConstraints: [CheckConstraintInfo] = [] @Published internal var triggers: [TriggerInfo] = [] @Published internal var ddlStatement: String = "" - @Published internal var tabData = StructureTabDataState() + @Published internal var loadState = StructureLoadState() @Published internal var concurrentRefresh: MetadataLoadState = .idle private var concurrentRefreshRequest = 0 @@ -55,56 +52,51 @@ internal final class StructureEditingSession: ObservableObject { internal var loadedTableOptions: TableOptions? { tableOptions.value ?? nil } - /// Leaves `.confirmed` when a save writes the counter and returns only on a read that lands, since the server may raise it. + // Leaves `.confirmed` when a save writes the counter and returns only on a read that lands, since the server may raise it. @Published internal private(set) var autoIncrementRead: AutoIncrementReadState = .confirmed @Published internal private(set) var tableOptionsCatalog: MetadataLoadState = .idle internal var tableOptionsCatalogRead: Task? - @Published internal var selectedTab: StructureTab = .columns - @Published internal var searchText = "" - @Published internal var sortState = SortState() - @Published internal var sortDescriptor: StructureSortDescriptor? - @Published internal var columnLayouts: [StructureTab: ColumnLayoutState] = [:] + @Published internal var pane: StructurePane = .definition + @Published internal var partsReloadToken = 0 @Published internal var serverSupport = StructureServerSupport.unrestricted { didSet { - guard !availableTabs.contains(selectedTab) else { return } - selectedTab = .columns + editor.serverSupport = serverSupport + if !availablePanes.contains(pane) { + pane = .definition + } } } - internal var availableTabs: [StructureTab] { - StructureTabAvailability.tabs( - for: connection.type, - serverSupport: serverSupport, - offersTableOptions: offersTableOptionsTab - ) - } - - internal var tabsFetchedOnMount: [StructureTab] { - var tabs: [StructureTab] = [.columns, .indexes, .foreignKeys] - if availableTabs.contains(.checkConstraints) { - tabs.append(.checkConstraints) - } - if !tabs.contains(selectedTab) { - tabs.append(selectedTab) + internal var availablePanes: [StructurePane] { + StructurePane.allCases.filter { pane in + switch pane { + case .definition, .ddl: true + case .triggers: connection.type.supportsTriggers + case .parts: connection.type == .clickhouse + } } - return tabs } - @Published internal var footer = StructureFooterCapability() - @Published internal var hasLoaded = false - + @Published internal var hasBaseline = false + // The ER diagram's sessions watch this to learn a save landed. @Published internal private(set) var appliedVersion = 0 - @Published internal var isApplying = false - @Published internal var lastAppliedAt: Date? + internal var isReloadingAfterSave = false + + weak var coordinator: MainContentCoordinator? { + didSet { editor.coordinator = coordinator } + } + internal var executionGate: any ExecutionGate = ExecutionGateProvider.shared + private var cancellables: Set = [] + internal init( identity: String, connection: DatabaseConnection, @@ -122,24 +114,59 @@ internal final class StructureEditingSession: ObservableObject { self.isSessionBound = isSessionBound self.tableName = tableName self.objectKind = objectKind - self.serverSupport = serverSupport - gridAdapter = StructureGridAdapter( - structureChangeManager: changeManager, - connection: connection, - tableName: tableName, - databaseName: databaseName, - schemaName: schemaName, - objectKind: objectKind, - isSessionBound: isSessionBound + editor = StructureDefinitionEditor( + manager: changeManager, + context: .existingTable( + connection: connection, + databaseName: databaseName, + schemaName: schemaName, + tableName: tableName, + objectKind: objectKind, + isSessionBound: isSessionBound + ) ) - gridAdapter.session = self - gridAdapter.store.session = self + self.serverSupport = serverSupport + editor.serverSupport = serverSupport + // Nothing is editable before the table's definitions are read. + editor.isAwaitingBaseline = true + editor.characterSetContextProvider = { [weak self] in + ColumnCharacterSetMenus.Context( + catalog: self?.tableOptionsCatalog ?? .idle, + tableCharacterSet: self?.changeManager.tableOptions.current?.characterSet + ) + } + editor.characterSetCatalogReader = { [weak self] in + guard let self else { return } + switch tableOptionsCatalog { + case .idle, .failed: + Task { await self.readTableOptionsCatalog() } + case .loading, .loaded: + break + } + } + editor.moveColumnHandler = { [weak self] from, to in + self?.beginColumnReorder(fromIndex: from, toIndex: to) + } + changeManager.objectWillChange + .sink { [weak self] in self?.objectWillChange.send() } + .store(in: &cancellables) + $tableOptionsCatalog + .dropFirst() + .receive(on: RunLoop.main) + .sink { [weak self] _ in self?.editor.refresh() } + .store(in: &cancellables) } + // A tab's session reads the table again at once; a diagram's has no view to show it and is dropped instead. internal func markApplied() { appliedVersion += 1 + guard coordinator != nil else { return } + editor.isAwaitingBaseline = true + Task { await refreshAfterApply() } } + // MARK: - Staleness + internal private(set) var owesRefetch = false internal func markStructureStale() { @@ -159,10 +186,12 @@ internal final class StructureEditingSession: ObservableObject { } internal func markEveryTabStale() { - tabData.markAllStale() + loadState.markAllStale() owesRefetch = false } + // MARK: - Table comment and options + internal func beginTableCommentLoad() { tableComment = tableComment.enteringLoad } @@ -238,41 +267,23 @@ internal final class StructureEditingSession: ObservableObject { concurrentRefresh = concurrentRefresh.settled(by: outcome, discardingValue: true) } - // The mounted view installs closures that capture it, and it holds this session; called wherever a - // session is dropped, never from `onDisappear`. + // Called wherever a session is dropped, never from `onDisappear`. internal func releaseViewWiring() { - gridAdapter.releaseViewWiring() + coordinator = nil } } internal enum AutoIncrementReadState: Equatable { case confirmed case rereading - /// The re-read failed or was cancelled, so the loaded counter may be one the server has moved past. + // The re-read failed or was cancelled, so the loaded counter may be one the server has moved past. case unconfirmed } extension StructureEditingSession: ActiveGridModelProviding { - // The schema grid while a sub-tab shows one; Parts, Triggers and Options draw views of their own. + // The section that holds the selection while the Definition page is on screen; the other panes draw no grid. var activeGridModel: DataGridModel? { - switch selectedTab { - case .columns, .indexes, .foreignKeys, .checkConstraints: - return gridModel - case .ddl, .parts, .triggers, .options: - return nil - } - } -} - -internal struct StructureFooterCapability: Equatable { - internal var canAdd = false - internal var canRemove = false - internal var addLabel = "" - internal var removeLabel = "" - - internal var unavailableReason: String? - - internal var isActive: Bool { - !addLabel.isEmpty + guard pane == .definition else { return nil } + return editor.focusedModel ?? editor.adapter(for: .columns).model } } diff --git a/apple/TablePro/Views/Structure/StructureFooterPolicy.swift b/apple/TablePro/Views/Structure/StructureFooterPolicy.swift index 65d8be1e2f..b42c3ffd7e 100644 --- a/apple/TablePro/Views/Structure/StructureFooterPolicy.swift +++ b/apple/TablePro/Views/Structure/StructureFooterPolicy.swift @@ -2,104 +2,45 @@ // StructureFooterPolicy.swift // TablePro // -// What the add/remove pair under a structure list offers, and why it is dimmed when it is. -// import Foundation import TableProPluginKit -/// The single decision behind the "+" and "-" under the Structure tab's list. -/// -/// Pure and SwiftUI-free so the rule is testable, and one function so the label, the enabled state -/// and the tooltip can never disagree. They did: the labels came from one switch, the enabled state -/// from a second that read engine capability flags alone, and the tooltip from a third that returned -/// nil for every tab but Foreign Keys. So a view offered an enabled "Add Column" over an -/// `ALTER TABLE … ADD COLUMN` PostgreSQL always refuses, with nothing to explain it. (#2726) +// The add and remove pair beneath each section's table: what it is called and which edit it stages. enum StructureFooterPolicy { - /// Nil for a tab with nothing to add: DDL is text, Parts is read-only, and a trigger is created - /// through its own editor rather than by typing a row. - static func operation(forAdding tab: StructureTab) -> StructureEditOperation? { - switch tab { - case .columns: return .addColumn - case .indexes: return .addIndex - case .foreignKeys: return .addForeignKey - case .checkConstraints: return .addCheckConstraint - case .options, .ddl, .parts, .triggers: return nil + static func operation(forAdding section: StructureSection) -> StructureEditOperation { + switch section { + case .columns: .addColumn + case .indexes: .addIndex + case .foreignKeys: .addForeignKey + case .checkConstraints: .addCheckConstraint } } - static func operation(forRemoving tab: StructureTab) -> StructureEditOperation? { - switch tab { - case .columns: return .dropColumn - case .indexes: return .dropIndex - case .foreignKeys: return .dropForeignKey - case .checkConstraints: return .dropCheckConstraint - case .options, .ddl, .parts, .triggers: return nil + static func operation(forRemoving section: StructureSection) -> StructureEditOperation { + switch section { + case .columns: .dropColumn + case .indexes: .dropIndex + case .foreignKeys: .dropForeignKey + case .checkConstraints: .dropCheckConstraint } } - /// Why the grid and the pair under it take no edits while a save runs. + // Why the tables and the pairs under them take no edits while a save runs. static var savingReason: String { String(localized: "The staged changes are being saved.") } - static func labels(for tab: StructureTab) -> (add: String, remove: String)? { - switch tab { + static func labels(for section: StructureSection) -> (add: String, remove: String) { + switch section { case .columns: - return (String(localized: "Add Column"), String(localized: "Remove Column")) + (String(localized: "Add Column"), String(localized: "Remove Column")) case .indexes: - return (String(localized: "Add Index"), String(localized: "Remove Index")) + (String(localized: "Add Index"), String(localized: "Remove Index")) case .foreignKeys: - return (String(localized: "Add Foreign Key"), String(localized: "Remove Foreign Key")) + (String(localized: "Add Foreign Key"), String(localized: "Remove Foreign Key")) case .checkConstraints: - return (String(localized: "Add Check Constraint"), String(localized: "Remove Check Constraint")) - case .options, .ddl, .parts, .triggers: - return nil - } - } - - /// - Parameter resolve: The full availability of one operation, which the caller supplies because - /// it owns the connection the engine half of the answer comes from. - /// - /// A refusing object kind keeps the pair on screen and dims it with the reason, the way the - /// Foreign Keys tab already did. An engine that cannot edit structure at all hides it instead: - /// there is nothing to explain per object when the whole tab is read-only. - static func resolve( - tab: StructureTab, - canEditSchema: Bool, - hasSelection: Bool, - isSaving: Bool, - resolve: (StructureEditOperation) -> StructureEditAvailability - ) -> StructureFooterCapability { - guard canEditSchema, - let labels = labels(for: tab), - let adding = operation(forAdding: tab), - let removing = operation(forRemoving: tab) - else { - return StructureFooterCapability() + (String(localized: "Add Check Constraint"), String(localized: "Remove Check Constraint")) } - - /// A save holds what is staged until it ends, so nothing can be added or removed meanwhile. - guard !isSaving else { - return StructureFooterCapability( - addLabel: labels.add, - removeLabel: labels.remove, - unavailableReason: savingReason - ) - } - - let addAvailability = resolve(adding) - let removeAvailability = resolve(removing) - - return StructureFooterCapability( - canAdd: addAvailability.isAvailable, - canRemove: hasSelection && removeAvailability.isAvailable, - addLabel: labels.add, - removeLabel: labels.remove, - /// Never the absence of a selection. The pair dims while nothing is selected on every - /// tab and on every engine, which needs no sentence; only a refusal the user could not - /// have predicted does. - unavailableReason: addAvailability.unavailableReason ?? removeAvailability.unavailableReason - ) } } diff --git a/apple/TablePro/Views/Structure/StructureGridAdapter+Inspector.swift b/apple/TablePro/Views/Structure/StructureGridAdapter+Inspector.swift deleted file mode 100644 index b988b70087..0000000000 --- a/apple/TablePro/Views/Structure/StructureGridAdapter+Inspector.swift +++ /dev/null @@ -1,28 +0,0 @@ -// -// StructureGridAdapter+Inspector.swift -// TablePro -// - -import Foundation - -// The inspector names a row by its position in the grid, the same position the grid's selection uses. -extension StructureGridAdapter: InspectorRowSource { - func inspectorRow(atDisplayRow displayRow: Int) -> InspectorRow? { - guard let rowID = model.rowID(at: displayRow), - let sourceIndex = StructureRowProvider.sourceIndex(of: rowID), - let providerRow = provider.displayRow(ofSourceIndex: sourceIndex) else { return nil } - return StructureInspectorRowBuilder.row( - atDisplayRow: providerRow, - tab: selectedTab, - provider: provider, - canEditSchema: canEdit, - lockedFieldIndices: lockedFieldIndices.union(characterSetLockedFields(sourceIndex: sourceIndex)), - rowOptions: { menuOptions(sourceIndex: sourceIndex, field: $0) } - ) - } - - func commitInspectorField(displayRow: Int, fieldIndex: Int, value: String?) { - guard let rowID = model.rowID(at: displayRow) else { return } - applyEdit(rowID: rowID, field: fieldIndex, text: value ?? "") - } -} diff --git a/apple/TablePro/Views/Structure/StructureGridAdapter.swift b/apple/TablePro/Views/Structure/StructureGridAdapter.swift deleted file mode 100644 index cd22297687..0000000000 --- a/apple/TablePro/Views/Structure/StructureGridAdapter.swift +++ /dev/null @@ -1,546 +0,0 @@ -// -// StructureGridAdapter.swift -// TablePro -// - -import AppKit -import TableProPluginKit - -// Feeds the Structure tab's grid from the session's working definitions and writes its edits back, -// always by the definition a row names, never by where the row sits on screen. -@MainActor -final class StructureGridAdapter { - let model: DataGridModel - let store: StructureGridStateStore - let structureChangeManager: StructureChangeManager - let connection: DatabaseConnection - let tableName: String - let databaseName: String - let schemaName: String? - let objectKind: TableInfo.TableType - let isSessionBound: Bool - let referenceMenus: ForeignKeyReferenceMenus - - weak var session: StructureEditingSession? - weak var coordinator: MainContentCoordinator? - // Installed by the mounted view while a column may be moved; it owns the reorder flow and its sheets. - var moveRowHandler: ((Int, Int) -> Void)? - - private(set) var provider: StructureRowProvider - private var snapshot = StructureGridSnapshot() - private let changeTracking: StructureGridChangeState - private let changeState: AnyChangeManager - - init( - structureChangeManager: StructureChangeManager, - connection: DatabaseConnection, - tableName: String, - databaseName: String, - schemaName: String?, - objectKind: TableInfo.TableType, - isSessionBound: Bool - ) { - self.structureChangeManager = structureChangeManager - self.connection = connection - self.tableName = tableName - self.databaseName = databaseName - self.schemaName = schemaName - self.objectKind = objectKind - self.isSessionBound = isSessionBound - let store = StructureGridStateStore() - self.store = store - self.model = DataGridModel(store: store) - self.referenceMenus = ForeignKeyReferenceMenus(connectionId: connection.id, databaseType: connection.type) - self.changeTracking = StructureGridChangeState(manager: structureChangeManager) - self.changeState = AnyChangeManager(changeTracking) - self.provider = StructureRowProvider( - changeManager: structureChangeManager, - tab: .columns, - databaseType: connection.type, - additionalFields: [.primaryKey], - serverSupport: .unrestricted - ) - referenceMenus.schemaName = schemaName - // Read when a list lands, not captured: the view hands the coordinator over on every appearance. - referenceMenus.onListsChanged = { [weak self] in - self?.coordinator?.inspectorRowSourceRevision += 1 - } - model.dataSource = self - model.actions = self - } - - // MARK: - State - - // The sub-tab whose rows the grid shows. The session's selection moves first and refresh() follows, so every - // write, menu and command reads this, never the incoming sub-tab (an open edit commits into the rows it was on). - var selectedTab: StructureTab { store.subTab } - private var serverSupport: StructureServerSupport { session?.serverSupport ?? .unrestricted } - private var searchText: String { session?.searchText ?? "" } - private var sortDescriptor: StructureSortDescriptor? { session?.sortDescriptor } - - var characterSetContext: ColumnCharacterSetMenus.Context { - ColumnCharacterSetMenus.Context( - catalog: session?.tableOptionsCatalog ?? .idle, - tableCharacterSet: structureChangeManager.tableOptions.current?.characterSet - ) - } - - var editGate: StructureEditGate { - StructureEditGate( - databaseType: connection.type, objectKind: objectKind, schema: schemaName, isSessionBound: isSessionBound - ) - } - - var canEditForeignKeys: Bool { - editGate.allows(.addForeignKey) - } - - var canStageAddForSelectedTab: Bool { - guard let adding = StructureFooterPolicy.operation(forAdding: selectedTab) else { return false } - return editGate.allows(adding) - } - - var canStageRemoveForSelectedTab: Bool { - guard let removing = StructureFooterPolicy.operation(forRemoving: selectedTab) else { return false } - return editGate.allows(removing) - } - - var canEdit: Bool { - editGate.allowsAnyEdit && !structureChangeManager.isHeldForSave - } - - var lockedFieldIndices: Set { - editGate.lockedFieldIndices( - on: selectedTab, - orderedFields: provider.orderedColumnFields, - fieldCount: provider.columns.count - ) - } - - var editRefusal: String? { - if structureChangeManager.isHeldForSave { return StructureFooterPolicy.savingReason } - guard !editGate.allowsAnyEdit else { return nil } - return editGate.resolve(.renameColumn).unavailableReason - } - - var columnReorderAvailability: ColumnReorderAvailability { - ColumnReorderPolicy.resolve( - support: PluginManager.shared.columnReorderSupport(for: connection.type), - engineName: connection.type.displayName, - isColumnsTab: selectedTab == .columns, - kindRefusal: editGate.kindRefusal(.reorderColumns), - canEditSchema: editGate.canEditSchema, - hasStagedChanges: structureChangeManager.hasChanges, - isRearranged: !searchText.isEmpty || sortDescriptor != nil || store.valueFilter.isActive - ) - } - - var showsGrid: Bool { - switch selectedTab { - case .columns, .indexes, .foreignKeys, .checkConstraints: true - case .triggers, .options, .ddl, .parts: false - } - } - - // MARK: - Refresh - - // Called whenever the working definitions, the sub-tab, the search, the sort or the edit gate moved. - func refresh() { - let tab = session?.selectedTab ?? store.subTab - if store.subTab != tab { - model.commitEditing() - store.switchSubTab(to: tab) - } - changeTracking.tab = tab - changeTracking.rowCount = StructureGridEdits.rowCount(on: tab, manager: structureChangeManager) - - let provider = StructureRowProvider( - changeManager: structureChangeManager, - tab: tab, - databaseType: connection.type, - additionalFields: [.primaryKey], - serverSupport: serverSupport, - filterText: searchText.isEmpty ? nil : searchText, - sortDescriptor: sortDescriptor, - picksCharacterSets: ColumnCharacterSetMenus.offersMenus(characterSetContext.catalog) - ) - self.provider = provider - let rows = provider.asTableRows() - let states = rows.rows.map { row in - StructureGridEdits.visualState( - sourceIndex: StructureRowProvider.sourceIndex(of: row.id) ?? -1, - tab: tab, - manager: structureChangeManager, - orderedFields: provider.orderedColumnFields, - tintsModifiedFields: true - ) - } - let wasEditable = model.configuration.isEditable - model.configuration = makeConfiguration(provider: provider) - let previous = snapshot - snapshot = StructureGridSnapshot(rows: rows, states: states) - model.applyHeldSnapshot(from: previous, to: snapshot) - // A save hold changes only the editability, and the inspector's fields have to follow it. - if previous.rows.rows != rows.rows || previous.states != states || previous.rows.columns != rows.columns - || wasEditable != model.configuration.isEditable { - coordinator?.inspectorRowSourceRevision += 1 - } - } - - private func makeConfiguration(provider: StructureRowProvider) -> DataGridConfiguration { - var configuration = DataGridConfiguration() - let options = provider.customDropdownOptions - configuration.dropdownColumns = provider.dropdownColumns - configuration.typePickerColumns = provider.typePickerColumns - configuration.customDropdownOptions = options.isEmpty ? nil : options - configuration.lockedColumns = lockedColumns(provider: provider) - configuration.editRefusalMessage = editRefusal - configuration.isEditable = canEdit - configuration.traits = DataGridEngineTraits( - databaseType: connection.type, - connectionScope: DatabaseScope(connectionId: connection.id, database: databaseName, schema: schemaName), - looksUpUserDefinedTypes: true - ) - let reorder = columnReorderAvailability - configuration.rowReorder = DataGridRowReorder( - isEnabled: reorder.isAvailable, - unavailableReason: reorder.unavailableReason - ) - configuration.commands = commands(reorderAvailable: reorder.isAvailable) - return configuration - } - - private func commands(reorderAvailable: Bool) -> Set { - var commands: Set = [.valueFilter, .freezeColumns] - guard showsGrid, !structureChangeManager.isHeldForSave else { return commands } - if canStageAddForSelectedTab { - commands.formUnion([.addRow, .duplicateRows, .pasteRows]) - } - if canStageRemoveForSelectedTab { - commands.formUnion([.deleteRows, .undoDelete]) - } - if reorderAvailable { - commands.insert(.moveRows) - } - return commands - } - - private func lockedColumns(provider: StructureRowProvider) -> Set { - let headings = provider.columns - let locked = editGate.lockedFieldIndices( - on: selectedTab, - orderedFields: provider.orderedColumnFields, - fieldCount: headings.count - ) - return Set(locked.compactMap { headings.indices.contains($0) ? headings[$0] : nil }) - } - - // MARK: - Rows - - func sourceIndices(of rowIDs: [RowID]) -> [Int] { - rowIDs.compactMap(StructureRowProvider.sourceIndex(of:)) - } - - func menuOptions(sourceIndex: Int, field: Int) -> [GridMenuOption]? { - switch selectedTab { - case .indexes: - guard structureChangeManager.workingIndexes.indices.contains(sourceIndex) else { return nil } - return StructureRowProvider.indexMenuOptions( - columnIndex: field, - index: structureChangeManager.workingIndexes[sourceIndex], - serverSupport: serverSupport - ) - case .foreignKeys: - guard canEditForeignKeys, - structureChangeManager.workingForeignKeys.indices.contains(sourceIndex) else { return nil } - referenceMenus.origin = coordinator?.selectedTabScope - return referenceMenus.options( - columnIndex: field, - foreignKey: structureChangeManager.workingForeignKeys[sourceIndex], - tableColumns: structureChangeManager.workingColumns.map(\.name) - ) - case .columns: - guard structureChangeManager.workingColumns.indices.contains(sourceIndex), - provider.orderedColumnFields.indices.contains(field) else { return nil } - return ColumnCharacterSetMenus.options( - for: provider.orderedColumnFields[field], - column: structureChangeManager.workingColumns[sourceIndex], - context: characterSetContext - ) - case .checkConstraints, .options, .ddl, .parts, .triggers: - return nil - } - } - - // Read-only per row, which the column-wide lock cannot say: a type with no character set, or a catalog not read. - func characterSetLockedFields(sourceIndex: Int) -> Set { - guard selectedTab == .columns, structureChangeManager.workingColumns.indices.contains(sourceIndex) else { return [] } - return ColumnCharacterSetMenus.lockedFieldIndices( - orderedFields: provider.orderedColumnFields, - column: structureChangeManager.workingColumns[sourceIndex], - catalog: characterSetContext.catalog - ) - } - - // Opening the menu is the retry, as it is for the reference menus. The inspector reads the options on every - // rebuild, so it never starts a read. - private func readCharacterSetCatalogIfNeeded(field: Int) { - guard selectedTab == .columns, provider.orderedColumnFields.indices.contains(field), - ColumnCharacterSetMenus.isCharacterSetField(provider.orderedColumnFields[field]) else { return } - switch characterSetContext.catalog { - case .idle, .failed: - Task { await session?.readTableOptionsCatalog() } - case .loading, .loaded: - break - } - } - - func characterSetCatalogDidChange() { - refresh() - coordinator?.inspectorRowSourceRevision += 1 - } - - // MARK: - Commands - - @discardableResult - func applyEdit(rowID: RowID, field: Int, text: String) -> Bool { - guard canEdit, let sourceIndex = StructureRowProvider.sourceIndex(of: rowID) else { return false } - let applied = write(text, field: field, sourceIndex: sourceIndex) - refresh() - return applied - } - - private func write(_ text: String, field: Int, sourceIndex: Int) -> Bool { - let tab = selectedTab - let characterSets = characterSetContext - guard field >= 0, - !editGate.locksField(at: field, on: tab, orderedFields: provider.orderedColumnFields), - !structureChangeManager.deleteInsertState(for: sourceIndex, tab: tab).isDeleted, - !StructureGridEdits.refuses( - text, - field: field, - sourceIndex: sourceIndex, - tab: tab, - manager: structureChangeManager, - orderedFields: provider.orderedColumnFields, - characterSets: characterSets - ) else { return false } - let changedReference = StructureGridEdits.apply( - text, - field: field, - sourceIndex: sourceIndex, - tab: tab, - manager: structureChangeManager, - orderedFields: provider.orderedColumnFields, - connection: connection, - characterSets: characterSets - ) - if let changedReference { - referenceMenus.origin = coordinator?.selectedTabScope - referenceMenus.prefetchReferencedColumns( - of: changedReference.referencedTable, schema: changedReference.referencedSchema - ) - } - return true - } - - func deleteRows(_ rowIDs: [RowID]) { - guard !rowIDs.isEmpty, showsGrid else { - model.clearSelection() - return - } - let allowed: Bool - switch selectedTab { - case .columns: allowed = editGate.allows(.dropColumn) - case .indexes: allowed = editGate.allows(.dropIndex) - case .foreignKeys: allowed = canEditForeignKeys - case .checkConstraints: allowed = editGate.allows(.dropCheckConstraint) - case .options, .ddl, .parts, .triggers: allowed = false - } - guard allowed else { return } - let displayRows = IndexSet(rowIDs.compactMap { model.displayIndex(of: $0) }) - let countBefore = model.rowCount - StructureGridEdits.delete( - sourceIndices: sourceIndices(of: rowIDs), tab: selectedTab, manager: structureChangeManager - ) - refresh() - selectAfterDeleting(displayRows, countBefore: countBefore) - } - - private func selectAfterDeleting(_ displayRows: IndexSet, countBefore: Int) { - let remaining = countBefore - displayRows.count - guard remaining > 0, let minRow = displayRows.first, let maxRow = displayRows.last else { - model.clearSelection() - return - } - let target = maxRow < remaining ? maxRow : (minRow > 0 ? minRow - 1 : 0) - guard let rowID = model.rowID(at: target) else { - model.clearSelection() - return - } - model.selectRows([rowID]) - } - - func addRow() { - guard canStageAddForSelectedTab else { return } - StructureGridEdits.addRow(on: selectedTab, manager: structureChangeManager) - refresh() - } - - func duplicateRows(_ rowIDs: [RowID]) { - guard canStageAddForSelectedTab else { return } - StructureGridEdits.duplicate( - sourceIndices: sourceIndices(of: rowIDs), tab: selectedTab, manager: structureChangeManager - ) - refresh() - } - - func undoDelete(_ rowIDs: [RowID]) { - for index in sourceIndices(of: rowIDs) { - structureChangeManager.undoDelete(for: selectedTab, at: index) - } - refresh() - } - - // Options stages its edits through the same manager, so its undo is the structure's. - private var undoesStructureEdits: Bool { - switch selectedTab { - case .columns, .indexes, .foreignKeys, .checkConstraints, .options, .parts: true - case .ddl, .triggers: false - } - } - - // The comment bar shows on every sub-tab, so its step undoes even where no grid is on screen. - func undo() { - guard undoesStructureEdits || structureChangeManager.undoesTableCommentNext else { return } - model.commitEditing() - structureChangeManager.undo() - refresh() - } - - func redo() { - guard undoesStructureEdits || structureChangeManager.redoesTableCommentNext else { return } - model.commitEditing() - structureChangeManager.redo() - refresh() - } - - func navigateToForeignKey(sourceIndex: Int) { - guard structureChangeManager.workingForeignKeys.indices.contains(sourceIndex), let coordinator else { return } - let foreignKey = structureChangeManager.workingForeignKeys[sourceIndex] - guard let origin = coordinator.selectedTabScope else { - coordinator.openTableTab(foreignKey.referencedTable, schema: foreignKey.referencedSchema) - return - } - let target = ForeignKeyTargetScope.resolve( - origin: origin, referencedSchema: foreignKey.referencedSchema, databaseType: connection.type - ) - coordinator.openTableTab(foreignKey.referencedTable, schema: target.schema, database: target.database) - } - - func releaseViewWiring() { - moveRowHandler = nil - coordinator = nil - } -} - -// MARK: - DataGridDataSource - -extension StructureGridAdapter: DataGridDataSource { - var gridRows: TableRows { snapshot.rows } - var gridChangeState: AnyChangeManager { changeState } - - func gridVisualState(for rowID: RowID) -> RowVisualState? { - snapshot.state(for: rowID) - } - - func gridMenuOptions(for rowID: RowID, column: DataGridColumn) -> [GridMenuOption]? { - guard let sourceIndex = StructureRowProvider.sourceIndex(of: rowID) else { return nil } - let options = menuOptions(sourceIndex: sourceIndex, field: column.dataIndex) - readCharacterSetCatalogIfNeeded(field: column.dataIndex) - return options - } -} - -// MARK: - DataGridActions - -extension StructureGridAdapter: DataGridActions { - func gridCommit(_ edits: [DataGridResolvedEdit], actionName: String) -> Bool { - guard canEdit, !edits.isEmpty else { return false } - var applied = false - let writeAll = { - for edit in edits { - guard let sourceIndex = StructureRowProvider.sourceIndex(of: edit.rowID) else { continue } - if self.write(edit.newValue.asText ?? "", field: edit.dataIndex, sourceIndex: sourceIndex) { - applied = true - } - } - } - if edits.count > 1 { - structureChangeManager.performAsOneUndoStep(writeAll) - } else { - writeAll() - } - refresh() - return applied - } - - func gridDeleteRows(_ rowIDs: [RowID]) { - deleteRows(rowIDs) - } - - func gridUndoDelete(_ rowIDs: [RowID]) { - undoDelete(rowIDs) - } - - func gridAddRow() { - addRow() - } - - func gridDuplicateRows(_ rowIDs: [RowID]) { - duplicateRows(rowIDs) - } - - func gridCanPasteRows() -> Bool { - canStageAddForSelectedTab && TableStructureView.canPasteStructureRows - } - - func gridPasteRows() { - pasteRows() - } - - // A move is offered only while nothing filters or sorts the list, so the drop index is a working index. - func gridMoveRow(from source: RowID, toDisplayIndex destination: Int) { - guard let sourceIndex = StructureRowProvider.sourceIndex(of: source) else { return } - moveRowHandler?(sourceIndex, destination) - } - - // The provider sorts by one column, so a secondary key the header appended is dropped. - func gridSortDidChange(_ sort: SortState) { - guard let session else { return } - if let primary = sort.columns.first { - session.sortDescriptor = StructureSortDescriptor( - column: primary.columnIndex, ascending: primary.direction == .ascending - ) - store.sortState = SortState(columns: [primary], source: .user) - } else { - session.sortDescriptor = nil - store.sortState = SortState(columns: [], source: .user) - } - refresh() - } - - // A value filter rearranges the list, which withdraws the column move. - func gridConfirmDisplayOrderChange(then apply: @escaping @MainActor () -> Void) { - apply() - model.configuration = makeConfiguration(provider: provider) - } - - func gridRowMenuItems(for rowIDs: [RowID], clickedRow: RowID) -> [NSMenuItem] { - structureRowMenuItems(for: rowIDs, clickedRow: clickedRow) - } - - func gridEmptySpaceMenu() -> NSMenu? { - makeEmptySpaceMenu() - } -} diff --git a/apple/TablePro/Views/Structure/StructureGridStateStore.swift b/apple/TablePro/Views/Structure/StructureGridStateStore.swift index 26cc6933b1..2a07bf9734 100644 --- a/apple/TablePro/Views/Structure/StructureGridStateStore.swift +++ b/apple/TablePro/Views/Structure/StructureGridStateStore.swift @@ -4,54 +4,55 @@ // import Foundation +import TableProPluginKit -// The Structure grid's view state lives on the tab's session, one column layout per sub-tab, and never -// reaches a table's saved layout. +// One section's grid state. It never reaches a table's saved layout; only the columns a person hides are kept, +// per section, for every table. @MainActor final class StructureGridStateStore: DataGridStateStore { - weak var session: StructureEditingSession? - private(set) var subTab: StructureTab = .columns + let section: StructureSection + private let defaults: UserDefaults + var sortState = SortState() var valueFilter = GridValueFilterState() var displayFormats: [ValueDisplayFormat?] = [] var selectionSnapshot = GridSelectionSnapshot.empty var viewportAnchor: GridViewportPlacement? let layoutScope: DataGridLayoutScope = .ephemeral - private var detachedLayouts: [StructureTab: ColumnLayoutState] = [:] - private var detachedSort = SortState() - - var sortState: SortState { - get { session?.sortState ?? detachedSort } - set { - guard let session else { - detachedSort = newValue - return - } - guard session.sortState != newValue else { return } - session.sortState = newValue + var columnLayout: ColumnLayoutState { + didSet { + guard columnLayout.hiddenColumns != oldValue.hiddenColumns else { return } + defaults.set(Array(columnLayout.hiddenColumns).sorted(), forKey: Self.hiddenColumnsKey(section)) } } - var columnLayout: ColumnLayoutState { - get { session?.columnLayouts[subTab] ?? detachedLayouts[subTab] ?? ColumnLayoutState() } - set { - guard let session else { - detachedLayouts[subTab] = newValue - return - } - guard session.columnLayouts[subTab] != newValue else { return } - session.columnLayouts[subTab] = newValue + init(section: StructureSection, defaults: UserDefaults = AppStorageEnvironment.shared.defaults) { + self.section = section + self.defaults = defaults + var layout = ColumnLayoutState() + if let stored = defaults.stringArray(forKey: Self.hiddenColumnsKey(section)) { + layout.hiddenColumns = Set(stored) + } else { + layout.hiddenColumns = Self.defaultHiddenColumns(section) } + columnLayout = layout } - // Search, sort, filter and selection start over on another sub-tab; the column layout is kept per sub-tab. - func switchSubTab(to tab: StructureTab) { - guard tab != subTab else { return } - subTab = tab - valueFilter = GridValueFilterState() - displayFormats = [] - selectionSnapshot = .empty - viewportAnchor = nil + static func hiddenColumnsKey(_ section: StructureSection) -> String { + "structure.hiddenColumns.\(section.rawValue)" + } + + // The inspector edits every field; the table keeps the ones read at a glance. + static func defaultHiddenColumns(_ section: StructureSection) -> Set { + switch section { + case .columns: + let hidden: [StructureColumnField] = [.onUpdate, .autoIncrement, .charset, .collation, .generated, .generationExpression] + return Set(hidden.map(\.displayName)) + case .foreignKeys: + return [String(localized: "Ref Schema")] + case .indexes, .checkConstraints: + return [] + } } } diff --git a/apple/TablePro/Views/Structure/StructureGridSupport.swift b/apple/TablePro/Views/Structure/StructureGridSupport.swift index 16ffdc1dbb..bc603c1a01 100644 --- a/apple/TablePro/Views/Structure/StructureGridSupport.swift +++ b/apple/TablePro/Views/Structure/StructureGridSupport.swift @@ -17,7 +17,7 @@ enum StructureGridEdits { _ text: String, field: Int, sourceIndex: Int, - tab: StructureTab, + tab: StructureSection, manager: StructureChangeManager, orderedFields: [StructureColumnField], connection: DatabaseConnection, @@ -62,8 +62,6 @@ enum StructureGridEdits { var constraint = manager.workingCheckConstraints[sourceIndex] StructureEditingSupport.updateCheckConstraint(&constraint, at: field, with: text) manager.updateCheckConstraint(id: constraint.id, with: constraint) - case .options, .ddl, .parts, .triggers: - break } return nil } @@ -73,7 +71,7 @@ enum StructureGridEdits { _ text: String, field: Int, sourceIndex: Int, - tab: StructureTab, + tab: StructureSection, manager: StructureChangeManager, orderedFields: [StructureColumnField], characterSets: ColumnCharacterSetMenus.Context? @@ -95,7 +93,7 @@ enum StructureGridEdits { } // One Cmd+Z brings the whole selection back: the manager registers an undo per row. - static func delete(sourceIndices: [Int], tab: StructureTab, manager: StructureChangeManager) { + static func delete(sourceIndices: [Int], tab: StructureSection, manager: StructureChangeManager) { manager.performAsOneUndoStep { for index in sourceIndices.sorted(by: >) { switch tab { @@ -111,59 +109,73 @@ enum StructureGridEdits { case .checkConstraints: guard manager.workingCheckConstraints.indices.contains(index) else { continue } manager.deleteCheckConstraint(id: manager.workingCheckConstraints[index].id) - case .options, .ddl, .parts, .triggers: - break } } } } - static func duplicate(sourceIndices: [Int], tab: StructureTab, manager: StructureChangeManager) { - for index in sourceIndices.sorted() { - switch tab { - case .columns: - guard manager.workingColumns.indices.contains(index) else { continue } - manager.addColumn(manager.workingColumns[index].withNewIdentity()) - case .indexes: - guard manager.workingIndexes.indices.contains(index) else { continue } - var copy = manager.workingIndexes[index].withNewIdentity() - copy.isPrimary = false - manager.addIndex(copy) - case .foreignKeys: - guard manager.workingForeignKeys.indices.contains(index) else { continue } - manager.addForeignKey(manager.workingForeignKeys[index].withNewIdentity()) - case .checkConstraints: - guard manager.workingCheckConstraints.indices.contains(index) else { continue } - manager.addCheckConstraint(manager.workingCheckConstraints[index].withNewIdentity()) - case .options, .ddl, .parts, .triggers: - break + static func duplicate(sourceIndices: [Int], tab: StructureSection, manager: StructureChangeManager) { + manager.performAsOneUndoStep { + for index in sourceIndices.sorted() { + switch tab { + case .columns: + guard manager.workingColumns.indices.contains(index) else { continue } + manager.addColumn(manager.workingColumns[index].withNewIdentity()) + case .indexes: + guard manager.workingIndexes.indices.contains(index) else { continue } + var copy = manager.workingIndexes[index].withNewIdentity() + copy.isPrimary = false + manager.addIndex(copy) + case .foreignKeys: + guard manager.workingForeignKeys.indices.contains(index) else { continue } + manager.addForeignKey(manager.workingForeignKeys[index].withNewIdentity()) + case .checkConstraints: + guard manager.workingCheckConstraints.indices.contains(index) else { continue } + manager.addCheckConstraint(manager.workingCheckConstraints[index].withNewIdentity()) + } } } } - static func addRow(on tab: StructureTab, manager: StructureChangeManager) { + static func addRow(on tab: StructureSection, manager: StructureChangeManager) { switch tab { case .columns: manager.addNewColumn() case .indexes: manager.addNewIndex() case .foreignKeys: manager.addNewForeignKey() case .checkConstraints: manager.addNewCheckConstraint() - case .options, .ddl, .parts, .triggers: break } } - static func rowCount(on tab: StructureTab, manager: StructureChangeManager) -> Int { + static func entityIDs(in section: StructureSection, manager: StructureChangeManager) -> [UUID] { + switch section { + case .columns: manager.workingColumns.map(\.id) + case .indexes: manager.workingIndexes.map(\.id) + case .foreignKeys: manager.workingForeignKeys.map(\.id) + case .checkConstraints: manager.workingCheckConstraints.map(\.id) + } + } + + static func sourceIndex(of id: UUID, in section: StructureSection, manager: StructureChangeManager) -> Int? { + switch section { + case .columns: manager.workingColumns.firstIndex { $0.id == id } + case .indexes: manager.workingIndexes.firstIndex { $0.id == id } + case .foreignKeys: manager.workingForeignKeys.firstIndex { $0.id == id } + case .checkConstraints: manager.workingCheckConstraints.firstIndex { $0.id == id } + } + } + + static func rowCount(on tab: StructureSection, manager: StructureChangeManager) -> Int { switch tab { case .columns: manager.workingColumns.count case .indexes: manager.workingIndexes.count case .foreignKeys: manager.workingForeignKeys.count case .checkConstraints: manager.workingCheckConstraints.count - case .options, .ddl, .parts, .triggers: 0 } } static func visualState( sourceIndex: Int, - tab: StructureTab, + tab: StructureSection, manager: StructureChangeManager, orderedFields: [StructureColumnField], tintsModifiedFields: Bool @@ -182,7 +194,7 @@ enum StructureGridEdits { private static func modifiedFields( sourceIndex: Int, - tab: StructureTab, + tab: StructureSection, manager: StructureChangeManager, orderedFields: [StructureColumnField] ) -> Set { @@ -207,23 +219,24 @@ enum StructureGridEdits { let working = manager.workingCheckConstraints[sourceIndex] guard let original = manager.currentCheckConstraints.first(where: { $0.id == working.id }) else { return [] } return StructureEditingSupport.checkConstraintModifiedIndices(old: original, new: working) - case .options, .ddl, .parts, .triggers: - return [] } } } -// The schema manager has no row identities of its own; this answers the grid's row questions by the -// source-index RowIDs `StructureRowProvider` hands out. +// The schema manager has no row identities of its own; this answers the grid's row questions through the RowIDs +// the section's adapter minted, one per working definition in order. @MainActor final class StructureGridChangeState: ChangeManaging { let manager: StructureChangeManager - var tab: StructureTab - var rowCount: Int = 0 + let section: StructureSection + var rowIDs: [RowID] = [] { + didSet { sourceIndexByRowID = Dictionary(uniqueKeysWithValues: rowIDs.enumerated().map { ($1, $0) }) } + } + private var sourceIndexByRowID: [RowID: Int] = [:] - init(manager: StructureChangeManager, tab: StructureTab = .columns) { + init(manager: StructureChangeManager, section: StructureSection) { self.manager = manager - self.tab = tab + self.section = section } var hasChanges: Bool { manager.hasChanges } @@ -232,18 +245,16 @@ final class StructureGridChangeState: ChangeManaging { var rowChanges: [RowChange] { [] } var insertedRowIDs: Set { - Set((0.. { - Set((0.. Bool { - guard let index = StructureRowProvider.sourceIndex(of: rowID), index >= 0 else { return false } - return manager.deleteInsertState(for: index, tab: tab).isDeleted + guard let index = sourceIndexByRowID[rowID] else { return false } + return manager.deleteInsertState(for: index, tab: section).isDeleted } func recordCellChange( @@ -256,8 +267,10 @@ final class StructureGridChangeState: ChangeManaging { ) {} func undoRowDeletion(rowID: RowID) { - guard let index = StructureRowProvider.sourceIndex(of: rowID) else { return } - manager.undoDelete(for: tab, at: index) + guard let index = sourceIndexByRowID[rowID] else { return } + let ids = StructureGridEdits.entityIDs(in: section, manager: manager) + guard ids.indices.contains(index) else { return } + manager.undoDelete(section: section, id: ids[index]) } } diff --git a/apple/TablePro/Views/Structure/StructureInspectorRowBuilder.swift b/apple/TablePro/Views/Structure/StructureInspectorRowBuilder.swift deleted file mode 100644 index cbbc261b7d..0000000000 --- a/apple/TablePro/Views/Structure/StructureInspectorRowBuilder.swift +++ /dev/null @@ -1,75 +0,0 @@ -// -// StructureInspectorRowBuilder.swift -// TablePro -// -// Builds the inspector payload for a structure grid row. Field names, values, -// and editors all come from the same provider the grid renders, so the panel -// and the grid can never disagree about what a row holds. -// - -import Foundation - -@MainActor -internal enum StructureInspectorRowBuilder { - static func row( - atDisplayRow displayRow: Int, - tab: StructureTab, - provider: StructureRowProvider, - canEditSchema: Bool, - lockedFieldIndices: Set = [], - rowOptions: (Int) -> [GridMenuOption]? = { _ in nil } - ) -> InspectorRow? { - switch tab { - case .columns, .indexes, .foreignKeys, .checkConstraints: - break - case .options, .ddl, .parts, .triggers: - return nil - } - - guard let values = provider.row(at: displayRow) else { return nil } - - let names = provider.columns - let dropdownOptions = provider.customDropdownOptions - let typePickerColumns = provider.typePickerColumns - let modified = provider.modifiedFieldIndices(atDisplay: displayRow) - - let fields = names.indices.map { index -> InspectorRowField in - InspectorRowField( - name: names[index], - value: index < values.count ? values[index] : nil, - editor: editor( - at: index, - options: rowOptions(index) ?? dropdownOptions[index], - typePickerColumns: typePickerColumns - ), - isModified: modified.contains(index), - isEditable: !lockedFieldIndices.contains(index) - ) - } - - return InspectorRow( - fields: fields, - isEditable: canEditSchema && !provider.isPendingDelete(atDisplay: displayRow) - ) - } - - /// An option list that carries a `Custom…` entry is an open vocabulary, so the field keeps its - /// text editor and puts the list beside it. A closed list becomes the picker it already was; - /// giving the Default field one would take away the only way to type an expression. - private static func editor( - at index: Int, - options: [GridMenuOption]?, - typePickerColumns: Set - ) -> FieldEditorKind { - if let options, !options.isEmpty { - if options.contains(where: { if case .custom = $0 { return true } else { return false } }) { - return .valuePicker(options: options) - } - return .enumPicker(values: options.compactMap(\.sql)) - } - if typePickerColumns.contains(index) { - return .typePicker - } - return .schemaText - } -} diff --git a/apple/TablePro/Views/Structure/StructureLoadState.swift b/apple/TablePro/Views/Structure/StructureLoadState.swift new file mode 100644 index 0000000000..d63d7700ee --- /dev/null +++ b/apple/TablePro/Views/Structure/StructureLoadState.swift @@ -0,0 +1,64 @@ +// +// StructureLoadState.swift +// TablePro +// + +import Foundation + +// What a structure tab has read, what a refresh made stale, and which read failed, per part. A failed read keeps +// what was loaded before it and is reported where that part shows, never over the whole tab. +struct StructureLoadState: Equatable { + enum Item: Hashable, CaseIterable { + case columns + case indexes + case foreignKeys + case checkConstraints + case triggers + case ddl + + init(_ section: StructureSection) { + switch section { + case .columns: self = .columns + case .indexes: self = .indexes + case .foreignKeys: self = .foreignKeys + case .checkConstraints: self = .checkConstraints + } + } + } + + private var loaded: Set = [] + private var stale: Set = [] + private(set) var failures: [Item: String] = [:] + + func hasData(_ item: Item) -> Bool { + loaded.contains(item) + } + + func needsFetch(_ item: Item) -> Bool { + !loaded.contains(item) || stale.contains(item) || failures[item] != nil + } + + func failure(_ item: Item) -> String? { + failures[item] + } + + mutating func markAllStale() { + stale = loaded + } + + mutating func markFetched(_ item: Item) { + loaded.insert(item) + stale.remove(item) + failures.removeValue(forKey: item) + } + + mutating func markFailed(_ item: Item, message: String) { + failures[item] = message + } + + mutating func reset() { + loaded.removeAll() + stale.removeAll() + failures.removeAll() + } +} diff --git a/apple/TablePro/Views/Structure/StructureRowProvider.swift b/apple/TablePro/Views/Structure/StructureRowProvider.swift index 1bc918aa9a..784b0f3da2 100644 --- a/apple/TablePro/Views/Structure/StructureRowProvider.swift +++ b/apple/TablePro/Views/Structure/StructureRowProvider.swift @@ -18,7 +18,7 @@ final class StructureRowProvider { ] private let changeManager: StructureChangeManager - private let tab: StructureTab + private let tab: StructureSection private let databaseType: DatabaseType private let additionalFields: Set private let serverSupport: StructureServerSupport @@ -42,13 +42,13 @@ final class StructureRowProvider { case .columns: return orderedColumnFields.map { $0.displayName } case .indexes: - return [ + let headings = [ String(localized: "Name"), String(localized: "Columns"), String(localized: "Type"), - String(localized: "Unique"), - String(localized: "Condition") + String(localized: "Unique") ] + return Self.offersIndexCondition(databaseType) ? headings + [String(localized: "Condition")] : headings case .foreignKeys: return [ String(localized: "Name"), @@ -65,8 +65,6 @@ final class StructureRowProvider { String(localized: "Expression"), String(localized: "Columns") ] - case .options, .ddl, .parts, .triggers: - return [] } } @@ -87,11 +85,21 @@ final class StructureRowProvider { picksCharacterSets ? Set(orderedColumnFields.indices.filter { ColumnCharacterSetMenus.isCharacterSetField(orderedColumnFields[$0]) }) : [] - case .checkConstraints, .options, .ddl, .parts, .triggers: [] + case .checkConstraints: [] } } static let indexTypeColumn = 2 + static let indexUniqueColumn = 3 + + // Yes-or-no fields, drawn as checkboxes in the grid and the inspector. + var booleanColumns: Set { + switch tab { + case .columns: Set(Self.booleanFields.compactMap { orderedColumnFields.firstIndex(of: $0) }) + case .indexes: [Self.indexUniqueColumn] + case .foreignKeys, .checkConstraints: [] + } + } static func indexMenuOptions( columnIndex: Int, @@ -127,12 +135,11 @@ final class StructureRowProvider { result[index] = ColumnDefaultVocabulary.options(for: databaseType) } return result - case .checkConstraints, .options, .ddl, .parts, .triggers: + case .checkConstraints: return [:] } } - static let booleanOptions = ["YES", "NO"] static let notGeneratedOption = String(localized: "Not generated") @@ -144,7 +151,7 @@ final class StructureRowProvider { case .columns: if let i = orderedColumnFields.firstIndex(of: .type) { return [i] } return [] - case .indexes, .foreignKeys, .checkConstraints, .options, .ddl, .parts, .triggers: + case .indexes, .foreignKeys, .checkConstraints: return [] } } @@ -155,13 +162,14 @@ final class StructureRowProvider { init( changeManager: StructureChangeManager, - tab: StructureTab, + tab: StructureSection, databaseType: DatabaseType = .mysql, additionalFields: Set = [], serverSupport: StructureServerSupport, filterText: String? = nil, sortDescriptor: StructureSortDescriptor? = nil, - picksCharacterSets: Bool = false + picksCharacterSets: Bool = false, + keepingSourceIndices: Set = [] ) { self.changeManager = changeManager self.tab = tab @@ -178,10 +186,10 @@ final class StructureRowProvider { ) let allRows = Self.buildAllRows( - tab: tab, changeManager: changeManager, orderedColumnFields: self.orderedColumnFields + tab: tab, changeManager: changeManager, orderedColumnFields: self.orderedColumnFields, databaseType: databaseType ) self.cachedRows = Self.applyFilterAndSort( - allRows, filterText: filterText, sortDescriptor: sortDescriptor + allRows, filterText: filterText, sortDescriptor: sortDescriptor, keeping: keepingSourceIndices ) } @@ -233,7 +241,7 @@ final class StructureRowProvider { guard sourceIndex < changeManager.workingIndexes.count else { return nil } let id = changeManager.workingIndexes[sourceIndex].id guard let original = changeManager.currentIndexes.first(where: { $0.id == id }) else { return nil } - return Self.row(for: original) + return Self.row(for: original, offersCondition: Self.offersIndexCondition(databaseType)) case .foreignKeys: guard sourceIndex < changeManager.workingForeignKeys.count else { return nil } let id = changeManager.workingForeignKeys[sourceIndex].id @@ -246,8 +254,6 @@ final class StructureRowProvider { return nil } return Self.row(for: original) - case .options, .ddl, .parts, .triggers: - return nil } } @@ -259,9 +265,10 @@ final class StructureRowProvider { } private static func buildAllRows( - tab: StructureTab, + tab: StructureSection, changeManager: StructureChangeManager, - orderedColumnFields: [StructureColumnField] + orderedColumnFields: [StructureColumnField], + databaseType: DatabaseType ) -> [IndexedRow] { switch tab { case .columns: @@ -270,7 +277,7 @@ final class StructureRowProvider { } case .indexes: return changeManager.workingIndexes.enumerated().map { index, indexInfo in - IndexedRow(sourceIndex: index, row: row(for: indexInfo)) + IndexedRow(sourceIndex: index, row: row(for: indexInfo, offersCondition: offersIndexCondition(databaseType))) } case .foreignKeys: return changeManager.workingForeignKeys.enumerated().map { index, fk in @@ -280,8 +287,6 @@ final class StructureRowProvider { return changeManager.workingCheckConstraints.enumerated().map { index, constraint in IndexedRow(sourceIndex: index, row: row(for: constraint)) } - case .options, .ddl, .parts, .triggers: - return [] } } @@ -316,20 +321,25 @@ final class StructureRowProvider { } } - private static func row(for index: EditableIndexDefinition) -> [String?] { + // MySQL and MariaDB have no partial indexes and their DDL drops a WHERE clause, so none is offered. + static func offersIndexCondition(_ type: DatabaseType) -> Bool { + SQLTypeFamily.of(type) != .mysql + } + + private static func row(for index: EditableIndexDefinition, offersCondition: Bool) -> [String?] { let columnsStr = index.columns.map { col in if let prefix = index.columnPrefixes[col] { return "\(col)(\(prefix))" } return col }.joined(separator: ", ") - return [ + let values: [String?] = [ index.name, columnsStr, index.type.rawValue, - index.isUnique ? "YES" : "NO", - index.whereClause ?? "" + index.isUnique ? "YES" : "NO" ] + return offersCondition ? values + [index.whereClause ?? ""] : values } private static func row(for fk: EditableForeignKeyDefinition) -> [String?] { @@ -344,15 +354,18 @@ final class StructureRowProvider { ] } + // `keeping` stays listed whatever the filter says: a definition being edited never vanishes under the editor. private static func applyFilterAndSort( _ rows: [IndexedRow], filterText: String?, - sortDescriptor: StructureSortDescriptor? + sortDescriptor: StructureSortDescriptor?, + keeping: Set = [] ) -> [IndexedRow] { var result = rows if let filterText, !filterText.isEmpty { result = result.filter { indexed in + if keeping.contains(indexed.sourceIndex) { return true } guard let name = indexed.row.first ?? nil else { return false } return name.localizedCaseInsensitiveContains(filterText) } @@ -372,27 +385,16 @@ final class StructureRowProvider { } extension StructureRowProvider { - // A row is named by its index in the working list, so a search, a sort or the grid's value filter - // never changes which definition an edit lands on. - static func rowID(forSourceIndex index: Int) -> RowID { - .existing(index) - } - - static func sourceIndex(of rowID: RowID) -> Int? { - guard case .existing(let index) = rowID, index >= 0 else { return nil } - return index - } - func displayRow(ofSourceIndex sourceIndex: Int) -> Int? { cachedRows.firstIndex { $0.sourceIndex == sourceIndex } } - func asTableRows() -> TableRows { + func asTableRows(rowID: (Int) -> RowID) -> TableRows { var rows = ContiguousArray() rows.reserveCapacity(cachedRows.count) for indexed in cachedRows { rows.append(Row( - id: Self.rowID(forSourceIndex: indexed.sourceIndex), + id: rowID(indexed.sourceIndex), values: ContiguousArray(indexed.row.map(PluginCellValue.fromOptional)) )) } diff --git a/apple/TablePro/Views/Structure/StructureSavePlan.swift b/apple/TablePro/Views/Structure/StructureSavePlan.swift index 1141f328d4..6f2f4950bc 100644 --- a/apple/TablePro/Views/Structure/StructureSavePlan.swift +++ b/apple/TablePro/Views/Structure/StructureSavePlan.swift @@ -47,7 +47,9 @@ internal extension StructureEditingSession { plan = try await stagedSavePlan(for: changes) } catch { guard mayPresentPreview(on: coordinator) else { return } - coordinator.toolbarState.previewStatements = ["-- Error generating SQL: \(error.localizedDescription)"] + coordinator.toolbarState.previewStatements = [ + String(format: String(localized: "-- Error generating SQL: %@"), error.localizedDescription) + ] coordinator.activeSheet = .sqlPreview return } diff --git a/apple/TablePro/Views/Structure/StructureGridAdapter+Menus.swift b/apple/TablePro/Views/Structure/StructureSectionAdapter+Menus.swift similarity index 69% rename from apple/TablePro/Views/Structure/StructureGridAdapter+Menus.swift rename to apple/TablePro/Views/Structure/StructureSectionAdapter+Menus.swift index 65447cc5ee..553d944eb5 100644 --- a/apple/TablePro/Views/Structure/StructureGridAdapter+Menus.swift +++ b/apple/TablePro/Views/Structure/StructureSectionAdapter+Menus.swift @@ -1,20 +1,20 @@ // -// StructureGridAdapter+Menus.swift +// StructureSectionAdapter+Menus.swift // TablePro // import AppKit import TableProPluginKit -extension StructureGridAdapter { +extension StructureSectionAdapter { // The grid adds Copy, Duplicate, Delete and Undo Delete from its commands; these are the schema's own. // The copies act on every targeted row, Open and the moves on the clicked one; nil when that is not known. - func structureRowMenuItems(for rowIDs: [RowID], clickedRow: RowID?) -> [NSMenuItem] { - guard showsGrid, !rowIDs.isEmpty else { return [] } + func rowMenuItems(for rowIDs: [RowID], clickedRow: RowID?) -> [NSMenuItem] { + guard !rowIDs.isEmpty else { return [] } let indices = sourceIndices(of: rowIDs) - let tab = selectedTab - let clicked = clickedRow.flatMap(StructureRowProvider.sourceIndex(of:)) - if let clicked, structureChangeManager.deleteInsertState(for: clicked, tab: tab).isDeleted { + let tab = section + let clicked = clickedRow.flatMap(sourceIndex(of:)) + if let clicked, manager.deleteInsertState(for: clicked, tab: tab).isDeleted { return [] } @@ -24,8 +24,8 @@ extension StructureGridAdapter { copyAsItem(indices), ] - if tab == .foreignKeys, let clicked, structureChangeManager.workingForeignKeys.indices.contains(clicked) { - let referenced = structureChangeManager.workingForeignKeys[clicked].referencedTable + if tab == .foreignKeys, let clicked, manager.workingForeignKeys.indices.contains(clicked) { + let referenced = manager.workingForeignKeys[clicked].referencedTable if !referenced.isEmpty { items.append(.separator()) items.append(menuItem(String(format: String(localized: "Open %@"), referenced)) { [weak self] in @@ -44,10 +44,10 @@ extension StructureGridAdapter { // Moving by menu gives the keyboard and VoiceOver the reorder a drag gives the pointer. private func columnMoveItems(movingRow row: Int?) -> [NSMenuItem] { - guard selectedTab == .columns else { return [] } + guard section == .columns, !context.isCreatingTable else { return [] } let reorder = model.configuration.rowReorder guard reorder.isEnabled || reorder.unavailableReason != nil else { return [] } - let count = structureChangeManager.workingColumns.count + let count = manager.workingColumns.count var items = [ columnMoveItem(String(localized: "Move Column Up"), row: row, .up, count: count, isEnabled: reorder.isEnabled), columnMoveItem(String(localized: "Move Column Down"), row: row, .down, count: count, isEnabled: reorder.isEnabled), @@ -70,7 +70,7 @@ extension StructureGridAdapter { return NSMenuItem(title: title, action: nil, keyEquivalent: "") } return menuItem(title) { [weak self] in - self?.moveRowHandler?(row, ColumnMove.dropIndex(movingRow: row, direction)) + self?.editor?.moveColumn(from: row, to: ColumnMove.dropIndex(movingRow: row, direction)) } } @@ -84,30 +84,6 @@ extension StructureGridAdapter { return item } - func makeEmptySpaceMenu() -> NSMenu? { - guard showsGrid, editGate.canEditSchema else { return nil } - let label: String - switch selectedTab { - case .columns: - guard editGate.allows(.addColumn) else { return nil } - label = String(localized: "Add Column") - case .indexes: - guard editGate.allows(.addIndex) else { return nil } - label = String(localized: "Add Index") - case .foreignKeys: - guard canEditForeignKeys else { return nil } - label = String(localized: "Add Foreign Key") - case .checkConstraints: - guard editGate.allows(.addCheckConstraint) else { return nil } - label = String(localized: "Add Check Constraint") - case .options, .ddl, .parts, .triggers: - return nil - } - let menu = NSMenu() - menu.addItem(menuItem(label) { [weak self] in self?.addRow() }) - return menu - } - private func menuItem(_ title: String, action: @escaping () -> Void) -> NSMenuItem { let target = StructureMenuTarget(action: action) let item = NSMenuItem(title: title, action: #selector(StructureMenuTarget.runAction), keyEquivalent: "") @@ -120,11 +96,11 @@ extension StructureGridAdapter { private func unsortedProvider(includingPrimaryKey: Bool) -> StructureRowProvider { StructureRowProvider( - changeManager: structureChangeManager, - tab: selectedTab, - databaseType: connection.type, + changeManager: manager, + tab: section, + databaseType: context.databaseType, additionalFields: includingPrimaryKey ? [.primaryKey] : [], - serverSupport: session?.serverSupport ?? .unrestricted + serverSupport: serverSupport ) } @@ -136,33 +112,31 @@ extension StructureGridAdapter { } func copyDefinitions(_ indices: [Int]) { - guard let driver = DatabaseManager.shared.driver(for: connection.id) else { return } + guard let driver = DatabaseManager.shared.driver(for: context.connection.id) else { return } var definitions: [String] = [] for index in indices.sorted() { - switch selectedTab { + switch section { case .columns: - guard structureChangeManager.workingColumns.indices.contains(index), - let sql = driver.generateColumnDefinitionSQL(column: structureChangeManager.workingColumns[index].toPlugin()) + guard manager.workingColumns.indices.contains(index), + let sql = driver.generateColumnDefinitionSQL(column: manager.workingColumns[index].toPlugin()) else { continue } definitions.append(sql) case .indexes: - guard structureChangeManager.workingIndexes.indices.contains(index), + guard manager.workingIndexes.indices.contains(index), let sql = driver.generateIndexDefinitionSQL( - index: structureChangeManager.workingIndexes[index].toPlugin(), tableName: tableName + index: manager.workingIndexes[index].toPlugin(), tableName: context.tableName ) else { continue } definitions.append(sql) case .foreignKeys: - guard structureChangeManager.workingForeignKeys.indices.contains(index), - let sql = driver.generateForeignKeyDefinitionSQL(fk: structureChangeManager.workingForeignKeys[index].toPlugin()) + guard manager.workingForeignKeys.indices.contains(index), + let sql = driver.generateForeignKeyDefinitionSQL(fk: manager.workingForeignKeys[index].toPlugin()) else { continue } definitions.append(sql) case .checkConstraints: - guard structureChangeManager.workingCheckConstraints.indices.contains(index) else { continue } - let constraint = structureChangeManager.workingCheckConstraints[index] + guard manager.workingCheckConstraints.indices.contains(index) else { continue } + let constraint = manager.workingCheckConstraints[index] definitions.append("CONSTRAINT \(driver.quoteIdentifier(constraint.name)) CHECK (\(constraint.expression))") - case .options, .ddl, .parts, .triggers: - break } } guard !definitions.isEmpty else { return } @@ -199,22 +173,14 @@ extension StructureGridAdapter { ClipboardService.shared.writeText(json) } - // The grid's own Copy reaches the definitions too, so a paste in this tab stages them again. - func gridCopyRows(_ rowIDs: [RowID]) -> Bool { - guard showsGrid, !rowIDs.isEmpty else { return false } - copyRows(rowIDs) - return true - } - // The definitions ride on the structure pasteboard type, so Paste can stage them again. - func copyRows(_ rowIDs: [RowID]) { - guard showsGrid, !rowIDs.isEmpty else { return } + func copyRows(_ rowIDs: [RowID], to pasteboard: NSPasteboard = .general) { + guard !rowIDs.isEmpty else { return } let ordered = rowIDs.sorted { (model.displayIndex(of: $0) ?? .max) < (model.displayIndex(of: $1) ?? .max) } let indices = sourceIndices(of: ordered) - let manager = structureChangeManager let encoder = JSONEncoder() let encoded: Data? - switch selectedTab { + switch section { case .columns: encoded = try? encoder.encode(indices.sorted().compactMap { manager.workingColumns.indices.contains($0) ? manager.workingColumns[$0] : nil }) case .indexes: @@ -227,8 +193,6 @@ extension StructureGridAdapter { encoded = try? encoder.encode(indices.sorted().compactMap { manager.workingCheckConstraints.indices.contains($0) ? manager.workingCheckConstraints[$0] : nil }) - case .options, .ddl, .parts, .triggers: - encoded = nil } let rows = model.rows @@ -238,23 +202,30 @@ extension StructureGridAdapter { let item = NSPasteboardItem() if let encoded, let json = String(data: encoded, encoding: .utf8) { - item.setString(json, forType: TableStructureView.structurePasteboardType) - item.setString(connection.type.rawValue, forType: TableStructureView.structureSourceTypePasteboardType) + item.setString(json, forType: StructurePasteboard.definitionsType) + item.setString(context.databaseType.rawValue, forType: StructurePasteboard.sourceDatabaseType) } if !lines.isEmpty { item.setString(lines.joined(separator: "\n"), forType: .string) } guard !item.types.isEmpty else { return } - NSPasteboard.general.clearContents() - NSPasteboard.general.writeObjects([item]) + pasteboard.clearContents() + pasteboard.writeObjects([item]) + } + + // Pasting several definitions is one undo step. + func pasteRows(from pasteboard: NSPasteboard = .general) { + guard canEdit, canAdd, let data = pasteboard.data(forType: StructurePasteboard.definitionsType) else { return } + let source = pasteboard.string(forType: StructurePasteboard.sourceDatabaseType).map(DatabaseType.init(rawValue:)) + manager.performAsOneUndoStep { + stagePasted(data, from: source) + } + editor?.definitionsDidChange() } - func pasteRows() { - guard canStageAddForSelectedTab, - let data = NSPasteboard.general.data(forType: TableStructureView.structurePasteboardType) else { return } + private func stagePasted(_ data: Data, from source: DatabaseType?) { let decoder = JSONDecoder() - let manager = structureChangeManager - switch selectedTab { + switch section { case .columns: guard let columns = try? decoder.decode([EditableColumnDefinition].self, from: data) else { return } for column in columns { @@ -262,11 +233,8 @@ extension StructureGridAdapter { } case .indexes: guard let indexes = try? decoder.decode([EditableIndexDefinition].self, from: data) else { return } - let source = NSPasteboard.general - .string(forType: TableStructureView.structureSourceTypePasteboardType) - .map(DatabaseType.init(rawValue:)) for index in indexes { - manager.addIndex(index.pasted(from: source, into: connection.type)) + manager.addIndex(index.pasted(from: source, into: context.databaseType)) } case .foreignKeys: guard let foreignKeys = try? decoder.decode([EditableForeignKeyDefinition].self, from: data) else { return } @@ -278,10 +246,16 @@ extension StructureGridAdapter { for constraint in constraints { manager.addCheckConstraint(constraint.withNewIdentity()) } - case .options, .ddl, .parts, .triggers: - return } - refresh() + } +} + +enum StructurePasteboard { + static let definitionsType = NSPasteboard.PasteboardType("com.TablePro.structure") + static let sourceDatabaseType = NSPasteboard.PasteboardType("com.TablePro.structure.database-type") + + static var canPaste: Bool { + NSPasteboard.general.data(forType: definitionsType) != nil } } diff --git a/apple/TablePro/Views/Structure/StructureSectionAdapter.swift b/apple/TablePro/Views/Structure/StructureSectionAdapter.swift new file mode 100644 index 0000000000..fb6f4082a3 --- /dev/null +++ b/apple/TablePro/Views/Structure/StructureSectionAdapter.swift @@ -0,0 +1,517 @@ +// +// StructureSectionAdapter.swift +// TablePro +// + +import AppKit +import TableProPluginKit + +// Feeds one section's grid from the working definitions and writes its edits back. A row is named by its +// definition's id, so a reload, a sort, a filter or a removed row above it never moves the selection. +@MainActor +final class StructureSectionAdapter { + let section: StructureSection + let model: DataGridModel + let store: StructureGridStateStore + let manager: StructureChangeManager + let context: StructureEditingContext + let referenceMenus: ForeignKeyReferenceMenus + + weak var editor: StructureDefinitionEditor? + + private(set) var provider: StructureRowProvider + private var snapshot = StructureGridSnapshot() + private var identities = StructureRowIdentities() + private var sortDescriptor: StructureSortDescriptor? + private let changeTracking: StructureGridChangeState + private let changeState: AnyChangeManager + + init( + section: StructureSection, + manager: StructureChangeManager, + context: StructureEditingContext, + referenceMenus: ForeignKeyReferenceMenus, + defaults: UserDefaults = AppStorageEnvironment.shared.defaults + ) { + self.section = section + self.manager = manager + self.context = context + self.referenceMenus = referenceMenus + let store = StructureGridStateStore(section: section, defaults: defaults) + self.store = store + self.model = DataGridModel(store: store) + self.changeTracking = StructureGridChangeState(manager: manager, section: section) + self.changeState = AnyChangeManager(changeTracking) + self.provider = StructureRowProvider( + changeManager: manager, + tab: section, + databaseType: context.databaseType, + additionalFields: [.primaryKey], + serverSupport: .unrestricted + ) + model.dataSource = self + model.actions = self + } + + // MARK: - State + + var serverSupport: StructureServerSupport { editor?.serverSupport ?? .unrestricted } + private var searchText: String { editor?.searchText ?? "" } + private var coordinator: MainContentCoordinator? { editor?.coordinator } + + var characterSetContext: ColumnCharacterSetMenus.Context { + editor?.characterSetContext ?? ColumnCharacterSetMenus.Context(catalog: .idle, tableCharacterSet: nil) + } + + var canAdd: Bool { context.allows(context.addOperation(for: section)) } + var canRemove: Bool { context.allows(context.removeOperation(for: section)) } + + var canEdit: Bool { + guard !manager.isHeldForSave, editor?.isReadOnly != true, editor?.isAwaitingBaseline != true else { return false } + return context.isCreatingTable || context.editGate.allowsAnyEdit + } + + var editRefusal: String? { + if manager.isHeldForSave { return StructureFooterPolicy.savingReason } + if editor?.isAwaitingBaseline == true { return String(localized: "The table is being read again after the save.") } + if editor?.isReadOnly == true { return editor?.readOnlyReason } + guard !context.isCreatingTable, !context.editGate.allowsAnyEdit else { return nil } + return context.editGate.resolve(.renameColumn).unavailableReason + } + + var lockedFieldIndices: Set { + guard !context.isCreatingTable else { return [] } + return context.editGate.lockedFieldIndices( + on: section, + orderedFields: provider.orderedColumnFields, + fieldCount: provider.columns.count + ) + } + + var columnReorderAvailability: ColumnReorderAvailability { + ColumnReorderPolicy.resolve( + support: PluginManager.shared.columnReorderSupport(for: context.databaseType), + engineName: context.databaseType.displayName, + isColumnsTab: section == .columns && !context.isCreatingTable, + kindRefusal: context.editGate.kindRefusal(.reorderColumns), + canEditSchema: context.editGate.canEditSchema && editor?.isReadOnly != true, + hasStagedChanges: manager.hasChanges, + isRearranged: !searchText.isEmpty || sortDescriptor != nil || store.valueFilter.isActive + ) + } + + var rowCount: Int { StructureGridEdits.rowCount(on: section, manager: manager) } + + var visibleRowCount: Int { snapshot.rows.rows.count } + + // MARK: - Identity + + func rowID(forEntity id: UUID) -> RowID { + identities.rowID(for: id) + } + + func entityID(of rowID: RowID) -> UUID? { + identities.entityID(of: rowID) + } + + func sourceIndex(of rowID: RowID) -> Int? { + guard let id = identities.entityID(of: rowID) else { return nil } + return StructureGridEdits.sourceIndex(of: id, in: section, manager: manager) + } + + func sourceIndices(of rowIDs: [RowID]) -> [Int] { + rowIDs.compactMap(sourceIndex(of:)) + } + + var selectedEntityIDs: [UUID] { + model.selection.affectedRows.compactMap { model.rowID(at: $0) }.compactMap(entityID(of:)) + } + + func select(entity id: UUID) { + model.selectRows([rowID(forEntity: id)]) + } + + // MARK: - Refresh + + func refresh() { + let entityIDs = StructureGridEdits.entityIDs(in: section, manager: manager) + let rowIDs = entityIDs.map { identities.rowID(for: $0) } + changeTracking.rowIDs = rowIDs + let selected = Set(selectedEntityIDs) + let keptIndices = Set(entityIDs.indices.filter { selected.contains(entityIDs[$0]) }) + + let provider = StructureRowProvider( + changeManager: manager, + tab: section, + databaseType: context.databaseType, + additionalFields: [.primaryKey], + serverSupport: serverSupport, + filterText: searchText.isEmpty ? nil : searchText, + sortDescriptor: sortDescriptor, + picksCharacterSets: ColumnCharacterSetMenus.offersMenus(characterSetContext.catalog), + keepingSourceIndices: keptIndices + ) + self.provider = provider + let rows = provider.asTableRows { rowIDs.indices.contains($0) ? rowIDs[$0] : .existing(-1) } + let states = provider.filteredToSourceMap.map { sourceIndex in + StructureGridEdits.visualState( + sourceIndex: sourceIndex, + tab: section, + manager: manager, + orderedFields: provider.orderedColumnFields, + tintsModifiedFields: !context.isCreatingTable + ) + } + model.configuration = makeConfiguration(provider: provider) + let previous = snapshot + snapshot = StructureGridSnapshot(rows: rows, states: states) + model.applyHeldSnapshot(from: previous, to: snapshot) + } + + private func makeConfiguration(provider: StructureRowProvider) -> DataGridConfiguration { + var configuration = DataGridConfiguration() + let checkboxes = provider.booleanColumns + let options = provider.customDropdownOptions.filter { !checkboxes.contains($0.key) } + configuration.checkboxColumns = checkboxes + configuration.dropdownColumns = provider.dropdownColumns.subtracting(checkboxes) + configuration.typePickerColumns = provider.typePickerColumns + configuration.customDropdownOptions = options.isEmpty ? nil : options + configuration.marksEmptyText = false + configuration.lockedColumns = lockedColumns(provider: provider) + configuration.editRefusalMessage = editRefusal + configuration.isEditable = canEdit + configuration.fitsRows = true + configuration.traits = DataGridEngineTraits( + databaseType: context.databaseType, + connectionScope: DatabaseScope( + connectionId: context.connection.id, database: context.databaseName, schema: context.schemaName + ), + looksUpUserDefinedTypes: true + ) + let reorder = columnReorderAvailability + configuration.rowReorder = DataGridRowReorder( + isEnabled: reorder.isAvailable, + unavailableReason: section == .columns && !context.isCreatingTable ? reorder.unavailableReason : nil + ) + configuration.commands = commands(reorderAvailable: reorder.isAvailable) + return configuration + } + + private func commands(reorderAvailable: Bool) -> Set { + // Hide Column and Show All Columns bring back the fields a section starts with hidden. + var commands: Set = [.valueFilter, .hideColumns] + guard canEdit else { return commands } + if canAdd { + commands.formUnion([.addRow, .duplicateRows, .pasteRows]) + } + if canRemove { + commands.formUnion([.deleteRows, .undoDelete]) + } + if reorderAvailable { + commands.insert(.moveRows) + } + return commands + } + + private func lockedColumns(provider: StructureRowProvider) -> Set { + let headings = provider.columns + return Set(lockedFieldIndices.compactMap { headings.indices.contains($0) ? headings[$0] : nil }) + } + + // MARK: - Menus + + func menuOptions(sourceIndex: Int, field: Int) -> [GridMenuOption]? { + switch section { + case .indexes: + guard manager.workingIndexes.indices.contains(sourceIndex) else { return nil } + return StructureRowProvider.indexMenuOptions( + columnIndex: field, + index: manager.workingIndexes[sourceIndex], + serverSupport: serverSupport + ) + case .foreignKeys: + guard context.allows(.addForeignKey), manager.workingForeignKeys.indices.contains(sourceIndex) else { return nil } + referenceMenus.origin = context.scope + return referenceMenus.options( + columnIndex: field, + foreignKey: manager.workingForeignKeys[sourceIndex], + tableColumns: manager.workingColumns.map(\.name) + ) + case .columns: + guard manager.workingColumns.indices.contains(sourceIndex), + provider.orderedColumnFields.indices.contains(field) else { return nil } + return ColumnCharacterSetMenus.options( + for: provider.orderedColumnFields[field], + column: manager.workingColumns[sourceIndex], + context: characterSetContext + ) + case .checkConstraints: + return nil + } + } + + // Read-only per row, which the column-wide lock cannot say: a type with no character set, or a catalog not read. + func characterSetLockedFields(sourceIndex: Int) -> Set { + guard section == .columns, manager.workingColumns.indices.contains(sourceIndex) else { return [] } + return ColumnCharacterSetMenus.lockedFieldIndices( + orderedFields: provider.orderedColumnFields, + column: manager.workingColumns[sourceIndex], + catalog: characterSetContext.catalog + ) + } + + // Opening the menu is the retry, as it is for the reference menus. + func readCharacterSetCatalogIfNeeded(field: Int) { + guard section == .columns, provider.orderedColumnFields.indices.contains(field), + ColumnCharacterSetMenus.isCharacterSetField(provider.orderedColumnFields[field]) else { return } + editor?.readCharacterSetCatalogIfNeeded() + } + + // MARK: - Edits + + @discardableResult + func applyEdit(rowID: RowID, field: Int, text: String) -> Bool { + guard canEdit, let sourceIndex = sourceIndex(of: rowID) else { return false } + let applied = write(text, field: field, sourceIndex: sourceIndex) + editor?.definitionsDidChange(in: section) + return applied + } + + @discardableResult + func applyEdit(entity id: UUID, field: Int, text: String) -> Bool { + applyEdit(rowID: rowID(forEntity: id), field: field, text: text) + } + + private func write(_ text: String, field: Int, sourceIndex: Int) -> Bool { + let characterSets = characterSetContext + guard field >= 0, + !lockedFieldIndices.contains(field), + !manager.deleteInsertState(for: sourceIndex, tab: section).isDeleted, + !StructureGridEdits.refuses( + text, + field: field, + sourceIndex: sourceIndex, + tab: section, + manager: manager, + orderedFields: provider.orderedColumnFields, + characterSets: characterSets + ) else { return false } + let changedReference = StructureGridEdits.apply( + text, + field: field, + sourceIndex: sourceIndex, + tab: section, + manager: manager, + orderedFields: provider.orderedColumnFields, + connection: context.connection, + characterSets: characterSets + ) + if let changedReference { + referenceMenus.origin = context.scope + referenceMenus.prefetchReferencedColumns( + of: changedReference.referencedTable, schema: changedReference.referencedSchema + ) + } + return true + } + + func addRow() { + guard canEdit, canAdd else { return } + StructureGridEdits.addRow(on: section, manager: manager) + editor?.definitionsDidChange() + if let id = StructureGridEdits.entityIDs(in: section, manager: manager).last { + editor?.reveal(section: section, entity: id) + } + } + + func deleteRows(_ rowIDs: [RowID]) { + guard canEdit, canRemove, !rowIDs.isEmpty else { return } + let displayRows = IndexSet(rowIDs.compactMap { model.displayIndex(of: $0) }) + let countBefore = model.rowCount + StructureGridEdits.delete(sourceIndices: sourceIndices(of: rowIDs), tab: section, manager: manager) + editor?.definitionsDidChange() + selectAfterDeleting(displayRows, countBefore: countBefore) + } + + private func selectAfterDeleting(_ displayRows: IndexSet, countBefore: Int) { + let remaining = model.rowCount + guard remaining > 0, let minRow = displayRows.first, let maxRow = displayRows.last else { + model.clearSelection() + return + } + // Deleting an existing definition keeps its row (struck through); the selection stays on it. + guard remaining < countBefore else { return } + let removed = countBefore - remaining + let target = maxRow - removed + 1 < remaining ? maxRow - removed + 1 : (minRow > 0 ? minRow - 1 : 0) + guard let rowID = model.rowID(at: min(max(target, 0), remaining - 1)) else { + model.clearSelection() + return + } + model.selectRows([rowID]) + } + + func duplicateRows(_ rowIDs: [RowID]) { + guard canEdit, canAdd else { return } + StructureGridEdits.duplicate(sourceIndices: sourceIndices(of: rowIDs), tab: section, manager: manager) + editor?.definitionsDidChange() + } + + func undoDelete(_ rowIDs: [RowID]) { + let ids = rowIDs.compactMap(entityID(of:)) + manager.performAsOneUndoStep { + for id in ids { + manager.undoDelete(section: section, id: id) + } + } + editor?.definitionsDidChange() + } + + func navigateToForeignKey(sourceIndex: Int) { + guard manager.workingForeignKeys.indices.contains(sourceIndex), let coordinator else { return } + let foreignKey = manager.workingForeignKeys[sourceIndex] + let target = ForeignKeyTargetScope.resolve( + origin: context.scope, referencedSchema: foreignKey.referencedSchema, databaseType: context.databaseType + ) + coordinator.openTableTab(foreignKey.referencedTable, schema: target.schema, database: target.database) + } + + func sortDidChange(_ sort: SortState) { + if let primary = sort.columns.first { + sortDescriptor = StructureSortDescriptor(column: primary.columnIndex, ascending: primary.direction == .ascending) + store.sortState = SortState(columns: [primary], source: .user) + } else { + sortDescriptor = nil + store.sortState = SortState(columns: [], source: .user) + } + refresh() + } +} + +// MARK: - DataGridDataSource + +extension StructureSectionAdapter: DataGridDataSource { + var gridRows: TableRows { snapshot.rows } + var gridChangeState: AnyChangeManager { changeState } + + func gridVisualState(for rowID: RowID) -> RowVisualState? { + snapshot.state(for: rowID) + } + + func gridCheckboxState(for rowID: RowID, column: DataGridColumn) -> Bool? { + guard provider.booleanColumns.contains(column.dataIndex), + let row = snapshot.rows.row(withID: rowID), column.dataIndex < row.values.count else { return nil } + return row.values[column.dataIndex].asText == "YES" + } + + func gridMenuOptions(for rowID: RowID, column: DataGridColumn) -> [GridMenuOption]? { + guard let sourceIndex = sourceIndex(of: rowID) else { return nil } + let options = menuOptions(sourceIndex: sourceIndex, field: column.dataIndex) + readCharacterSetCatalogIfNeeded(field: column.dataIndex) + return options + } +} + +// MARK: - DataGridActions + +extension StructureSectionAdapter: DataGridActions { + func gridCommit(_ edits: [DataGridResolvedEdit], actionName: String) -> Bool { + guard canEdit, !edits.isEmpty else { return false } + var applied = false + manager.performAsOneUndoStep { + for edit in edits { + guard let sourceIndex = sourceIndex(of: edit.rowID) else { continue } + if write(edit.newValue.asText ?? "", field: edit.dataIndex, sourceIndex: sourceIndex) { + applied = true + } + } + } + editor?.definitionsDidChange() + return applied + } + + func gridSetCheckbox(_ isOn: Bool, rowIDs: [RowID], column: DataGridColumn) { + guard canEdit, provider.booleanColumns.contains(column.dataIndex) else { return } + manager.performAsOneUndoStep { + for rowID in rowIDs { + guard let sourceIndex = sourceIndex(of: rowID) else { continue } + _ = write(isOn ? "YES" : "NO", field: column.dataIndex, sourceIndex: sourceIndex) + } + } + editor?.definitionsDidChange() + } + + func gridDeleteRows(_ rowIDs: [RowID]) { + deleteRows(rowIDs) + } + + func gridUndoDelete(_ rowIDs: [RowID]) { + undoDelete(rowIDs) + } + + func gridAddRow() { + addRow() + } + + func gridDuplicateRows(_ rowIDs: [RowID]) { + duplicateRows(rowIDs) + } + + func gridCanPasteRows() -> Bool { + canEdit && canAdd && StructurePasteboard.canPaste + } + + func gridPasteRows() { + pasteRows() + } + + func gridCopyRows(_ rowIDs: [RowID]) -> Bool { + guard !rowIDs.isEmpty else { return false } + copyRows(rowIDs) + return true + } + + // A move is offered only while nothing filters or sorts the list, so the drop index is a working index. + func gridMoveRow(from source: RowID, toDisplayIndex destination: Int) { + guard let sourceIndex = sourceIndex(of: source) else { return } + editor?.moveColumn(from: sourceIndex, to: destination) + } + + // The provider sorts by one column, so a secondary key the header appended is dropped. + func gridSortDidChange(_ sort: SortState) { + sortDidChange(sort) + } + + // A value filter rearranges the list, which withdraws the column move. + func gridConfirmDisplayOrderChange(then apply: @escaping @MainActor () -> Void) { + apply() + model.configuration = makeConfiguration(provider: provider) + } + + func gridRowMenuItems(for rowIDs: [RowID], clickedRow: RowID) -> [NSMenuItem] { + rowMenuItems(for: rowIDs, clickedRow: clickedRow) + } + + func gridEmptySpaceMenu() -> NSMenu? { + nil + } +} + +// Mints one RowID per definition id and keeps it for the adapter's lifetime. +struct StructureRowIdentities { + private var rowIDs: [UUID: RowID] = [:] + private var entityIDs: [RowID: UUID] = [:] + private var next = 0 + + mutating func rowID(for id: UUID) -> RowID { + if let existing = rowIDs[id] { return existing } + let rowID = RowID.existing(next) + next += 1 + rowIDs[id] = rowID + entityIDs[rowID] = id + return rowID + } + + func entityID(of rowID: RowID) -> UUID? { + entityIDs[rowID] + } +} diff --git a/apple/TablePro/Views/Structure/StructureServerSupport.swift b/apple/TablePro/Views/Structure/StructureServerSupport.swift index 8a35217ea4..7029f3f3d5 100644 --- a/apple/TablePro/Views/Structure/StructureServerSupport.swift +++ b/apple/TablePro/Views/Structure/StructureServerSupport.swift @@ -49,11 +49,11 @@ struct StructureServerSupport: Equatable, Sendable { } /// Exhaustive on purpose: a new tab has to state whether this server can refuse it. - func offers(_ tab: StructureTab) -> Bool { + func offers(_ tab: StructureSection) -> Bool { switch tab { case .checkConstraints: return checkConstraintRefusal == nil - case .columns, .indexes, .foreignKeys, .triggers, .options, .ddl, .parts: + case .columns, .indexes, .foreignKeys: return true } } diff --git a/apple/TablePro/Views/Structure/StructureTabAvailability.swift b/apple/TablePro/Views/Structure/StructureTabAvailability.swift deleted file mode 100644 index 155b53f9a8..0000000000 --- a/apple/TablePro/Views/Structure/StructureTabAvailability.swift +++ /dev/null @@ -1,43 +0,0 @@ -// -// StructureTabAvailability.swift -// TablePro -// - -import Foundation - -/// Which tabs the Structure editor offers, from what the engine can do and what the connected -/// server answers for. -/// -/// Both halves matter. `supportsCheckConstraints` is the engine at its newest release; the server -/// in front of the user may be older than the release that gained them, and MySQL before 8.0.16 -/// and MariaDB before 10.2.1 answer `Query OK` to a `CHECK` clause and throw it away. Offering the -/// tab there is offering an edit that reports success and changes nothing. -enum StructureTabAvailability { - // Table options are the driver's answer for this object (`offersTableOptionsTab`), never the engine's type. - static func tabs( - for type: DatabaseType, - serverSupport: StructureServerSupport, - offersTableOptions: Bool = false - ) -> [StructureTab] { - StructureTab.allCases.filter { tab in - engineOffers(tab, on: type, offersTableOptions: offersTableOptions) && serverSupport.offers(tab) - } - } - - private static func engineOffers(_ tab: StructureTab, on type: DatabaseType, offersTableOptions: Bool) -> Bool { - switch tab { - case .foreignKeys: - return type.supportsForeignKeys - case .parts: - return type == .clickhouse - case .triggers: - return type.supportsTriggers - case .checkConstraints: - return type.supportsCheckConstraints - case .options: - return offersTableOptions - case .columns, .indexes, .ddl: - return true - } - } -} diff --git a/apple/TablePro/Views/Structure/StructureTabDataState.swift b/apple/TablePro/Views/Structure/StructureTabDataState.swift deleted file mode 100644 index e1b436835b..0000000000 --- a/apple/TablePro/Views/Structure/StructureTabDataState.swift +++ /dev/null @@ -1,41 +0,0 @@ -// -// StructureTabDataState.swift -// TablePro -// - -import Foundation - -/// Tracks which structure tabs hold data and which of them need re-fetching. -/// Keeping the two apart lets a refresh mark every tab stale without dropping the -/// counts already on screen, so the segmented picker never reflows mid-reload. -struct StructureTabDataState: Equatable { - private var loaded: Set = [] - private var stale: Set = [] - - func hasData(_ tab: StructureTab) -> Bool { - loaded.contains(tab) - } - - func needsFetch(_ tab: StructureTab) -> Bool { - !loaded.contains(tab) || stale.contains(tab) - } - - mutating func markAllStale() { - stale = loaded - } - - mutating func markFetched(_ tab: StructureTab) { - loaded.insert(tab) - stale.remove(tab) - } - - mutating func reset() { - loaded.removeAll() - stale.removeAll() - } - - static func label(for tab: StructureTab, count: Int?) -> String { - guard let count else { return tab.displayName } - return "\(tab.displayName) (\(count))" - } -} diff --git a/apple/TablePro/Views/Structure/StructureTableRebuildHandler.swift b/apple/TablePro/Views/Structure/StructureTableRebuildHandler.swift index 629952ae49..e9909116b1 100644 --- a/apple/TablePro/Views/Structure/StructureTableRebuildHandler.swift +++ b/apple/TablePro/Views/Structure/StructureTableRebuildHandler.swift @@ -101,7 +101,7 @@ enum StructureTableRebuildHandler { ) async throws -> StructureRebuildPlanRunner.Prepared { let partition = partition(changes) guard partition.unsupported.isEmpty else { - throw RebuildError.cannotExpress(partition.unsupported.map(\.description)) + throw RebuildError.cannotExpress(partition.unsupported.map(\.localizedDescription)) } let respecification = respecification(from: partition.rebuilt) diff --git a/apple/TablePro/Views/Structure/StructureViewActionHandler.swift b/apple/TablePro/Views/Structure/StructureViewActionHandler.swift deleted file mode 100644 index eadc44ff64..0000000000 --- a/apple/TablePro/Views/Structure/StructureViewActionHandler.swift +++ /dev/null @@ -1,23 +0,0 @@ -// -// StructureViewActionHandler.swift -// TablePro -// - -import Foundation - -/// The structure actions that need the grid that is on screen. -/// -/// Saving is deliberately absent. The staged edits belong to the tab's `StructureEditingSession`, -/// and the close prompt offers to save them from tabs that have no mounted structure view at all, -/// so `applyStagedChanges` lives on the session and every caller goes there instead. -@MainActor -final class StructureViewActionHandler { - var previewSQL: (() -> Void)? - var copyRows: (() -> Void)? - var pasteRows: (() -> Void)? - var undo: (() -> Void)? - var redo: (() -> Void)? - var addRow: (() -> Void)? - var removeRow: (() -> Void)? - var refresh: (() -> Void)? -} diff --git a/apple/TablePro/Views/Structure/TableOptionsView.swift b/apple/TablePro/Views/Structure/TableOptionsView.swift deleted file mode 100644 index 2309b09190..0000000000 --- a/apple/TablePro/Views/Structure/TableOptionsView.swift +++ /dev/null @@ -1,281 +0,0 @@ -// -// TableOptionsView.swift -// TablePro -// - -import SwiftUI - -// The Structure tab's Options sub-tab: the table's default character set and collation, and its AUTO_INCREMENT -// counter. Every edit is one whole-value write to the change manager, so the coupled charset and collation pick -// lands as one staged change and one undo step. -internal struct TableOptionsView: View { - @ObservedObject var session: StructureEditingSession - @ObservedObject var changeManager: StructureChangeManager - let isReadOnly: Bool - // The draft AUTO_INCREMENT column's largest value, nil when the table has none. - let autoIncrementMaximum: UInt64? - let onRetry: () -> Void - let onRetryCatalog: () -> Void - - @State private var counterText = "" - @FocusState private var isCounterFocused: Bool - - var body: some View { - Group { - if let options = displayedOptions { - form(options) - } else { - stateView - } - } - .frame(maxWidth: .infinity, maxHeight: .infinity) - .onAppear { syncCounterText() } - .onChange(of: displayedCounter) { _ in - guard !isCounterFocused else { return } - syncCounterText() - } - .onChange(of: isCounterFocused) { isFocused in - if !isFocused { syncCounterText() } - } - } - - // MARK: - State - - private var draft: TableOptions? { changeManager.tableOptions.current } - - // A read that landed while edits were staged has not become the baseline yet, so it shows read-only. - private var displayedOptions: TableOptions? { - draft ?? (session.tableOptions.value ?? nil) - } - - private var isCounterConfirmed: Bool { - session.autoIncrementRead == .confirmed && changeManager.tableOptions.isAutoIncrementConfirmed - } - - // An unconfirmed counter may be one the server moved past; a read deferred behind staged edits shows read-only. - private var displayedCounter: UInt64? { - guard session.autoIncrementRead == .confirmed else { return nil } - guard changeManager.tableOptions.isAutoIncrementConfirmed else { return session.loadedTableOptions?.autoIncrement } - return displayedOptions?.autoIncrement - } - - private var editability: StructureTableOptionsPolicy.Editability { - StructureTableOptionsPolicy.resolve( - hasBaseline: draft != nil, - isReadOnly: isReadOnly, - isSaving: changeManager.isHeldForSave - ) - } - - private var catalog: TableOptionsCatalog? { - guard case .loaded(let catalog) = session.tableOptionsCatalog else { return nil } - return catalog - } - - @ViewBuilder - private var stateView: some View { - switch session.tableOptions { - case .idle, .loading: - ProgressView() - .controlSize(.small) - case .failed(let message): - VStack(spacing: 8) { - Image(systemName: "exclamationmark.triangle") - .font(.largeTitle) - .foregroundStyle(.orange) - .accessibilityHidden(true) - Text("Couldn't read the table options") - RevealedTextView(message) - .foregroundStyle(.secondary) - Button("Try Again", action: onRetry) - .accessibilityIdentifier("structure-options-retry") - } - .padding() - case .loaded: - Text("This table has no options to change.") - .foregroundStyle(.secondary) - } - } - - // MARK: - Form - - private func form(_ options: TableOptions) -> some View { - let editability = self.editability - return Form { - Section { - Picker("Character Set", selection: characterSetBinding) { - if options.characterSet == nil { - Text("Unknown").tag(String?.none) - } - ForEach(TableOptionsForm.characterSets(in: catalog, keeping: options.characterSet), id: \.self) { - Text($0).tag(Optional($0)) - } - } - .disabled(!editability.isEditable || catalog == nil) - .accessibilityIdentifier("structure-options-charset") - - Picker("Collation", selection: collationBinding) { - if options.collation == nil { - Text("Unknown").tag(String?.none) - } - ForEach(collationChoices(options)) { choice in - Text(choice.title).tag(Optional(choice.name)) - } - } - .disabled(!editability.isEditable || catalog == nil) - .accessibilityIdentifier("structure-options-collation") - - Toggle("Convert existing columns", isOn: convertBinding) - .disabled(!editability.isEditable) - .accessibilityIdentifier("structure-options-convert") - - catalogStatusRow - } footer: { - if options.convertsColumns { - Text("Rewrites every text column to this character set. TEXT can grow to MEDIUMTEXT, and the table is copied while it runs.") - } else { - Text("Applies to columns added later. Existing columns keep their character set.") - } - } - - if let maximum = autoIncrementMaximum { - counterSection(maximum: maximum, isEditable: editability.isEditable) - } - } - .formStyle(.grouped) - .safeAreaInset(edge: .bottom, spacing: 0) { - if let reason = editability.unavailableReason, !changeManager.isHeldForSave { - StructureTabNoteView(systemImage: "lock", text: reason, identifier: "structure-options-unavailable-note") - } - } - } - - private func collationChoices(_ options: TableOptions) -> [TableOptionsForm.Choice] { - TableOptionsForm.collations(in: catalog, characterSet: options.characterSet, keeping: options.collation) - } - - @ViewBuilder - private var catalogStatusRow: some View { - switch session.tableOptionsCatalog { - case .idle, .loading: - HStack { - Text("Reading the server's character sets…") - .foregroundStyle(.secondary) - Spacer() - ProgressView() - .controlSize(.small) - } - case .failed: - HStack { - Label("Couldn't read the server's character sets.", systemImage: "exclamationmark.triangle") - .foregroundStyle(.secondary) - Spacer() - Button("Try Again", action: onRetryCatalog) - .accessibilityIdentifier("structure-options-catalog-retry") - } - case .loaded: - EmptyView() - } - } - - private func counterSection(maximum: UInt64, isEditable: Bool) -> some View { - Section { - TextField("Next Auto Increment", text: counterBinding) - .focused($isCounterFocused) - .disabled(!isEditable || !isCounterConfirmed) - .accessibilityIdentifier("structure-options-auto-increment") - // The server raises a value below the next free one, so a saved counter is read back, never shown as typed. - switch session.autoIncrementRead { - case .rereading: - HStack { - Text("Reading the saved value…") - .foregroundStyle(.secondary) - Spacer() - ProgressView() - .controlSize(.small) - } - case .unconfirmed: - HStack { - Label("Couldn't read the saved value.", systemImage: "exclamationmark.triangle") - .foregroundStyle(.secondary) - Spacer() - Button("Try Again", action: onRetry) - .accessibilityIdentifier("structure-options-auto-increment-retry") - } - case .confirmed: - EmptyView() - } - if let message = AutoIncrementEntry( - counterText, maximum: maximum, original: changeManager.tableOptions.original?.autoIncrement - ).message { - Text(message) - .foregroundStyle(.red) - .accessibilityIdentifier("structure-options-auto-increment-error") - } - } footer: { - Text("A value at or below the highest existing ID becomes that ID plus one.") - } - } - - // MARK: - Bindings - - private var characterSetBinding: Binding { - Binding( - get: { displayedOptions?.characterSet }, - set: { name in - guard let name, let current = draft, name != current.characterSet else { return } - changeManager.stageTableOptions(TableOptionsForm.selectingCharacterSet(name, in: current, catalog: catalog)) - } - ) - } - - private var collationBinding: Binding { - Binding( - get: { displayedOptions?.collation }, - set: { name in - guard let name, var next = draft, name != next.collation else { return } - next.collation = name - changeManager.stageTableOptions(next) - } - ) - } - - private var convertBinding: Binding { - Binding( - get: { displayedOptions?.convertsColumns ?? false }, - set: { converts in - guard var next = draft, converts != next.convertsColumns else { return } - next.convertsColumns = converts - changeManager.stageTableOptions(next) - } - ) - } - - // Staged per keystroke, as the comment is, so a save from the keyboard never misses a value still in the field. - // Text that is not a valid counter stages the loaded one and holds Save until it is fixed or cleared. - private var counterBinding: Binding { - Binding( - get: { counterText }, - set: { text in - counterText = text - guard var next = draft, let maximum = autoIncrementMaximum else { return } - let entry = AutoIncrementEntry( - text, maximum: maximum, original: changeManager.tableOptions.original?.autoIncrement - ) - switch entry { - case .value(let value): - next.autoIncrement = value - case .empty, .invalid: - next.autoIncrement = changeManager.tableOptions.original?.autoIncrement - } - changeManager.noteAutoIncrementEntry(invalid: entry.message) - guard next != draft else { return } - changeManager.stageTableOptions(next) - } - ) - } - - private func syncCounterText() { - counterText = displayedCounter.map { String($0) } ?? "" - } -} diff --git a/apple/TablePro/Views/Structure/TableStructureView+DataLoading.swift b/apple/TablePro/Views/Structure/TableStructureView+DataLoading.swift deleted file mode 100644 index d41c293577..0000000000 --- a/apple/TablePro/Views/Structure/TableStructureView+DataLoading.swift +++ /dev/null @@ -1,259 +0,0 @@ -// -// TableStructureView+DataLoading.swift -// TablePro -// - -import AppKit -import Combine -import os -import SwiftUI -import TableProPluginKit -import UniformTypeIdentifiers - -// MARK: - Data Loading - -extension TableStructureView { - @Sendable - func loadInitialData() async { - session.settleOwedRefetch() - guard !session.hasLoaded else { - isInitialLoading = false - isLoading = false - await session.reloadConcurrentRefreshAvailability() - await loadTableOptionsCatalogIfNeeded() - return - } - await loadColumns() - for tab in session.tabsFetchedOnMount where tab != .columns { - guard !Task.isCancelled else { break } - await loadTabDataIfNeeded(tab) - } - // A workspace switch cancels this task and restarts it on return, so a cut-short load must not count. - guard !Task.isCancelled else { return } - await loadTableComment() - guard !Task.isCancelled else { return } - await session.readTableOptions() - guard !Task.isCancelled else { return } - adoptFetchedBaseline() - session.hasLoaded = true - isInitialLoading = false - // After the baseline, so the columns are editable while the catalog is read. - await loadTableOptionsCatalogIfNeeded() - } - - func loadColumns() async { - isLoading = true - errorMessage = nil - - do { - columns = try await structureLoader.columns() - tabData.markFetched(.columns) - } catch { - if let message = fetchFailure(error).message { - errorMessage = message - } - } - - isLoading = false - } - - func loadTabDataIfNeeded(_ tab: StructureTab) async { - guard tabData.needsFetch(tab) else { return } - await fetchTabData(tab) - } - - func fetchTabData(_ tab: StructureTab) async { - do { - switch tab { - case .columns: - columns = try await structureLoader.columns() - case .indexes: - indexes = try await structureLoader.indexes() - await session.reloadConcurrentRefreshAvailability() - case .foreignKeys: - foreignKeys = try await structureLoader.foreignKeys() - case .checkConstraints: - checkConstraints = try await structureLoader.checkConstraints() - case .ddl: - let table = tableName - let schema = structureLoader.scope.schema - ddlStatement = try await structureLoader.perform { driver in - try await TableDDLComposer.fetchDDL( - for: table, using: driver, includesDependencies: true, schema: schema - ) - } - case .triggers: - do { - triggers = try await structureLoader.triggers() - } catch { - guard fetchFailure(error) != .cancelled else { return } - Self.logger.error("Failed to load triggers: \(error.publicLogShape, privacy: .public)") - triggers = [] - } - case .parts, .options: - return - } - tabData.markFetched(tab) - } catch { - guard let message = fetchFailure(error).message else { return } - Self.logger.error("Failed to load \(tab.rawValue, privacy: .public): \(error.publicLogShape, privacy: .public)") - errorMessage = message - } - } - - private func fetchFailure(_ error: any Error) -> StructureFetchFailure { - StructureFetchFailure(error, taskIsCancelled: Task.isCancelled) - } - - func loadSchemaForEditing() { - session.adoptLoadedBaseline() - } - - // MARK: - Lifecycle Callbacks - - func onSelectedTabChanged(_ new: StructureTab) { - searchText = "" - structureSortDescriptor = nil - sortState = SortState() - gridAdapter.refresh() - Task { - await loadTabDataIfNeeded(new) - } - } - - func adoptFetchedBaseline() { - guard !structureChangeManager.hasChanges else { - session.markStructureStale() - return - } - loadSchemaForEditing() - } - - func rebaselineUnlessEdited() { - guard !isReloadingAfterSave, !isInitialLoading else { return } - adoptFetchedBaseline() - } - - func onColumnsChanged() { - rebaselineUnlessEdited() - } - - func onIndexesChanged() { - rebaselineUnlessEdited() - } - - func onCheckConstraintsChanged() { - rebaselineUnlessEdited() - } - - func onForeignKeysChanged() { - rebaselineUnlessEdited() - } - - func onRefreshData() { - guard !isReloadingAfterSave else { - Self.logger.debug("Ignoring refresh notification - currently reloading after save") - return - } - - let justSaved = lastSaveTime.map { Date().timeIntervalSince($0) < 2.0 } ?? false - - if structureChangeManager.hasChanges && !justSaved { - Task { @MainActor in - let window = coordinator?.contentWindow - let confirmed = await AlertHelper.confirmDestructive( - title: String(localized: "Discard Changes?"), - message: String(localized: "You have unsaved changes to the table structure. Refreshing will discard these changes."), - confirmButton: String(localized: "Discard"), - cancelButton: String(localized: "Cancel"), - window: window - ) - - if confirmed { - discardChanges() - await reloadAllTabs() - } - } - } else { - Task { @MainActor in - await reloadAllTabs() - } - } - } - - private func reloadAllTabs() async { - session.markEveryTabStale() - gridAdapter.referenceMenus.invalidateTableLists() - partsReloadToken += 1 - await reloadCoreTabs() - switch selectedTab { - case .ddl: - await fetchTabData(.ddl) - case .triggers where connection.type.supportsTriggers: - await fetchTabData(.triggers) - case .columns, .indexes, .foreignKeys, .checkConstraints, .triggers, .options, .parts: - break - } - } - - func reloadCoreTabs() async { - isLoading = true - errorMessage = nil - defer { isLoading = false } - - let includesForeignKeys = connection.type.supportsForeignKeys - let offersTableOptions = session.offersTableOptionsTab - do { - let reloaded = try await structureLoader.coreTabs( - includingForeignKeys: includesForeignKeys, - includingTableComment: offersTableComment, - includingTableOptions: offersTableOptions - ) - - if let comment = reloaded.tableComment { - session.settleTableComment(comment) - } - // A save never adopts the counter it wrote: the server raises a low value, so this read is what shows. - if let options = reloaded.tableOptions { - session.settleTableOptions(options) - } - columns = reloaded.columns - indexes = reloaded.indexes - tabData.markFetched(.columns) - tabData.markFetched(.indexes) - if includesForeignKeys { - foreignKeys = reloaded.foreignKeys - tabData.markFetched(.foreignKeys) - } - await session.reloadConcurrentRefreshAvailability() - } catch { - // A counter re-read the save started ends unconfirmed, keeping the options already on screen. - if offersTableOptions { - session.settleTableOptions(.cancelled) - } - guard let message = fetchFailure(error).message else { return } - Self.logger.error("Failed to reload structure: \(error.publicLogShape, privacy: .public)") - errorMessage = message - } - } -} - -internal enum StructureFetchFailure: Equatable { - case cancelled - case failed(String) - - internal init(_ error: any Error, taskIsCancelled: Bool) { - if error is CancellationError || taskIsCancelled { - self = .cancelled - } else { - self = .failed(error.localizedDescription) - } - } - - internal var message: String? { - switch self { - case .cancelled: nil - case .failed(let message): message - } - } -} diff --git a/apple/TablePro/Views/Structure/TableStructureView+EditGate.swift b/apple/TablePro/Views/Structure/TableStructureView+EditGate.swift deleted file mode 100644 index 92fdacfe69..0000000000 --- a/apple/TablePro/Views/Structure/TableStructureView+EditGate.swift +++ /dev/null @@ -1,38 +0,0 @@ -// -// TableStructureView+EditGate.swift -// TablePro -// - -import Foundation -import TableProPluginKit - -extension TableStructureView { - var editGate: StructureEditGate { - StructureEditGate( - databaseType: connection.type, objectKind: objectKind, schema: schemaName, isSessionBound: isSessionBound - ) - } - - var foreignKeyEditAvailability: ForeignKeyEditAvailability { - ForeignKeyEditPolicy.resolve( - support: PluginManager.shared.foreignKeyEditSupport(for: connection.type), - engineName: connection.type.displayName, - kindRefusal: editGate.kindRefusal(.addForeignKey), - canEditSchema: editGate.canEditSchema - ) - } - - func publishFooterCapability() { - let gate = editGate - let footer = StructureFooterPolicy.resolve( - tab: selectedTab, - canEditSchema: gate.canEditSchema, - hasSelection: !session.gridModel.selection.isEmpty, - isSaving: structureChangeManager.isHeldForSave, - resolve: { gate.resolve($0) } - ) - // Published on every write, and this runs on every selection change. - guard session.footer != footer else { return } - session.footer = footer - } -} diff --git a/apple/TablePro/Views/Structure/TableStructureView+Schema.swift b/apple/TablePro/Views/Structure/TableStructureView+Schema.swift deleted file mode 100644 index e76b921e7e..0000000000 --- a/apple/TablePro/Views/Structure/TableStructureView+Schema.swift +++ /dev/null @@ -1,187 +0,0 @@ -// -// TableStructureView+Schema.swift -// TablePro -// - -import AppKit -import Combine -import os -import SwiftUI -import TableProPluginKit -import UniformTypeIdentifiers - -// MARK: - Schema Operations - -extension TableStructureView { - func generateStructurePreviewSQL() { - let changes = structureChangeManager.getChangesArray() - guard !changes.isEmpty else { - // An undo back to a clean state would otherwise leave the last preview's SQL behind. - toolbarState.previewStatements = [] - return - } - - if skipSchemaPreview { - Task { _ = await session.applyStagedChanges(coordinator: coordinator) } - return - } - - guard let coordinator else { return } - Task { await session.previewStagedChanges(coordinator: coordinator) } - } - - func refreshAfterApply() async { - isReloadingAfterSave = true - await reloadCoreTabs() - adoptFetchedBaseline() - await loadTabDataIfNeeded(selectedTab) - - gridAdapter.refresh() - - // Left false, the next mount re-baselines the change manager and drops anything staged since. - session.hasLoaded = true - isReloadingAfterSave = false - } - - func discardChanges() { - session.gridModel.discardEditing() - structureChangeManager.discardChanges() - gridAdapter.refresh() - } - - // MARK: - DDL View - - var ddlView: some View { - VStack(spacing: 0) { - HStack(spacing: 12) { - HStack(spacing: 4) { - Button(action: { ddlFontSize = max(10, ddlFontSize - 1) }) { - Image(systemName: "textformat.size.smaller") - .frame(width: 24, height: 24) - } - .accessibilityLabel(String(localized: "Decrease font size")) - Text("\(Int(ddlFontSize))") - .font(.caption) - .foregroundStyle(.secondary) - .frame(width: 24) - Button(action: { ddlFontSize = min(24, ddlFontSize + 1) }) { - Image(systemName: "textformat.size.larger") - .frame(width: 24, height: 24) - } - .accessibilityLabel(String(localized: "Increase font size")) - } - .buttonStyle(.borderless) - - Spacer() - - if showCopyConfirmation { - HStack { - Image(systemName: "checkmark.circle.fill") - .foregroundStyle(.green) - Text("Copied!") - } - .transition(.opacity) - } - - Button(action: openInEditor) { - Label("Open in Editor", systemImage: "square.and.pencil") - } - .buttonStyle(.bordered) - .disabled(ddlStatement.isEmpty) - - Button(action: copyDDL) { - Label("Copy", systemImage: "doc.on.doc") - } - .buttonStyle(.bordered) - - Button(action: exportDDL) { - Label("Export", systemImage: "square.and.arrow.up") - } - .buttonStyle(.bordered) - } - .padding() - .paneBarBackground() - - Divider() - - if ddlStatement.isEmpty { - StructureDDLUnavailableView() - } else { - DDLTextView(ddl: ddlStatement, fontSize: $ddlFontSize, databaseType: connection.type) - } - } - } - - // MARK: - DDL Actions - - private func openInEditor() { - guard !ddlStatement.isEmpty else { return } - coordinator?.tabManager.addTab( - initialQuery: ddlStatement, - title: "\(tableName) DDL" - ) - } - - func openTriggerInEditor(_ trigger: TriggerInfo) { - guard !trigger.statement.isEmpty else { return } - coordinator?.tabManager.addTab( - initialQuery: trigger.statement, - title: trigger.name - ) - } - - private func copyDDL() { - ClipboardService.shared.writeText(ddlStatement) - - withMotion { - showCopyConfirmation = true - } - - copyResetTask?.cancel() - copyResetTask = Task { @MainActor in - try? await Task.sleep(for: .milliseconds(1_500)) - guard !Task.isCancelled else { return } - withMotion { - showCopyConfirmation = false - } - } - } - - private func exportDDL() { - let savePanel = NSSavePanel() - if let sqlType = UTType(filenameExtension: "sql") { - savePanel.allowedContentTypes = [sqlType] - } - savePanel.nameFieldStringValue = "\(tableName).sql" - - guard let window = coordinator?.contentWindow else { return } - savePanel.beginSheetModal(for: window) { response in - guard response == .OK, let url = savePanel.url else { return } - do { - try ddlStatement.write(to: url, atomically: true, encoding: .utf8) - } catch { - Self.logger.error("Failed to export: \(error.publicLogShape, privacy: .public)") - AlertHelper.showErrorSheet( - title: String(localized: "Could not export the schema"), - message: error.localizedDescription, - window: window - ) - } - } - } -} - -internal struct StructureDDLUnavailableView: View { - internal var body: some View { - VStack(spacing: 8) { - Image(systemName: "tray") - .font(.largeTitle) - .foregroundStyle(.secondary) - .accessibilityHidden(true) - Text(String(localized: "No DDL available")) - .foregroundStyle(.secondary) - } - .frame(maxWidth: .infinity, maxHeight: .infinity) - .valueStandInBackground() - } -} diff --git a/apple/TablePro/Views/Structure/TableStructureView+TableComment.swift b/apple/TablePro/Views/Structure/TableStructureView+TableComment.swift deleted file mode 100644 index 0a1fb9e5de..0000000000 --- a/apple/TablePro/Views/Structure/TableStructureView+TableComment.swift +++ /dev/null @@ -1,85 +0,0 @@ -// -// TableStructureView+TableComment.swift -// TablePro -// - -import AppKit -import os -import SwiftUI - -internal extension TableStructureView { - var tableCommentSupport: DatabaseObjectToolEligibility.Support { - DatabaseObjectToolEligibility.Support.of(DatabaseManager.shared.driver(for: connection.id)) - } - - var offersTableComment: Bool { - tableCommentSupport.commentableTypes.contains(objectKind) - } - - var tableCommentField: StructureTableCommentPolicy.Field? { - StructureTableCommentPolicy.resolve( - objectKind: objectKind, - support: tableCommentSupport, - isReadOnly: toolbarState.safeModeLevel.blocksAllWrites || isSessionBound, - load: session.tableComment.erased, - isSaving: structureChangeManager.isHeldForSave - ) - } - - /// One model write per keystroke, so the staged change and its undo run never see a stale value. - private var tableCommentBinding: Binding { - Binding( - get: { structureChangeManager.tableComment.text }, - set: { structureChangeManager.stageTableComment($0) } - ) - } - - @ViewBuilder - var tableCommentBar: some View { - if let field = tableCommentField { - HStack(alignment: .firstTextBaseline, spacing: 12) { - Text("Comment:") - .font(.body.weight(.medium)) - - TextField(field.placeholder, text: tableCommentBinding, axis: .vertical) - .lineLimit(1...4) - .textFieldStyle(.roundedBorder) - .frame(maxWidth: .infinity) - .disabled(!field.isEditable) - .focused($isTableCommentFocused) - .onSubmit { structureChangeManager.endTableCommentRun() } - .help(field.unavailableReason ?? "") - .accessibilityLabel(String(localized: "Comment")) - .accessibilityIdentifier("structure-table-comment") - } - .padding() - .paneBarBackground() - - Divider() - } - } - - func loadTableComment() async { - guard offersTableComment else { return } - session.beginTableCommentLoad() - let outcome: MetadataFetchOutcome - do { - outcome = .fetched(try await structureLoader.tableComment()) - } catch { - switch StructureFetchFailure(error, taskIsCancelled: Task.isCancelled) { - case .cancelled: - outcome = .cancelled - case .failed(let message): - Self.logger.error("Failed to load the table comment: \(error.publicLogShape, privacy: .public)") - outcome = .failed(message) - } - } - session.settleTableComment(outcome) - } - - /// A comment changed elsewhere refetches columns identical to the ones loaded, so the columns' - /// own handler never fires and the baseline has to move here. - func onTableCommentChanged() { - rebaselineUnlessEdited() - } -} diff --git a/apple/TablePro/Views/Structure/TableStructureView+TableOptions.swift b/apple/TablePro/Views/Structure/TableStructureView+TableOptions.swift deleted file mode 100644 index c79144bf01..0000000000 --- a/apple/TablePro/Views/Structure/TableStructureView+TableOptions.swift +++ /dev/null @@ -1,46 +0,0 @@ -// -// TableStructureView+TableOptions.swift -// TablePro -// - -import SwiftUI - -internal extension TableStructureView { - var tableOptionsView: some View { - TableOptionsView( - session: session, - changeManager: structureChangeManager, - isReadOnly: toolbarState.safeModeLevel.blocksAllWrites || isSessionBound, - autoIncrementMaximum: autoIncrementMaximum, - onRetry: { Task { await session.readTableOptions() } }, - onRetryCatalog: { Task { await session.readTableOptionsCatalog() } } - ) - .task { await session.readTableOptionsCatalog() } - } - - // The draft's column, not the loaded one: the counter is set after the column work in the same save. - private var autoIncrementMaximum: UInt64? { - let columns = structureChangeManager.workingColumns - let index = columns.indices.first { index in - columns[index].autoIncrement - && !structureChangeManager.deleteInsertState(for: index, tab: .columns).isDeleted - } - return index.map { AutoIncrementEntry.maximum(for: columns[$0]) } - } - - func loadTableOptionsCatalogIfNeeded() async { - guard session.needsTableOptionsCatalog else { return } - await session.readTableOptionsCatalog() - } - - // Like the comment: options changed elsewhere refetch columns identical to the loaded ones. - func onTableOptionsChanged() { - rebaselineUnlessEdited() - } - - // A retried read can return the options already loaded, which moves only this. - func onAutoIncrementReadChanged(_ read: AutoIncrementReadState) { - guard read == .confirmed else { return } - rebaselineUnlessEdited() - } -} diff --git a/apple/TablePro/Views/Structure/TableStructureView.swift b/apple/TablePro/Views/Structure/TableStructureView.swift index c169d8763e..3ca0325335 100644 --- a/apple/TablePro/Views/Structure/TableStructureView.swift +++ b/apple/TablePro/Views/Structure/TableStructureView.swift @@ -3,252 +3,61 @@ // TablePro // -import AppKit -import Combine -import os import SwiftUI -import TableProPluginKit -import UniformTypeIdentifiers +// The Structure view of a table tab: Definition, Triggers, DDL and, on ClickHouse, Parts. It draws the session and +// hands it the window's state; loading, staging and saving live in the session. struct TableStructureView: View { - static let logger = Logger(subsystem: "com.TablePro", category: "TableStructureView") - static let structurePasteboardType = NSPasteboard.PasteboardType("com.TablePro.structure") - static let structureSourceTypePasteboardType = NSPasteboard.PasteboardType("com.TablePro.structure.database-type") - - static var canPasteStructureRows: Bool { - NSPasteboard.general.data(forType: structurePasteboardType) != nil - } - let tableName: String - let connection: DatabaseConnection - let databaseName: String - let schemaName: String? - + @ObservedObject var session: StructureEditingSession @ObservedObject var toolbarState: ConnectionToolbarState let coordinator: MainContentCoordinator? - @Environment(\.appServices) var services - - var scope: DatabaseScope { - DatabaseScope( - connectionId: connection.id, database: databaseName, schema: schemaName, isSessionBound: isSessionBound - ) - } - - var isSessionBound: Bool { session.isSessionBound } - - var structureLoader: TableStructureLoader { - TableStructureLoader(scope: scope, tableName: tableName) - } - - @ObservedObject var session: StructureEditingSession - - var objectKind: TableInfo.TableType { session.objectKind } - - var selectedTab: StructureTab { - get { session.selectedTab } - nonmutating set { session.selectedTab = newValue } - } - - var searchText: String { - get { session.searchText } - nonmutating set { session.searchText = newValue } - } - - var sortState: SortState { - get { session.sortState } - nonmutating set { session.sortState = newValue } - } - - var structureSortDescriptor: StructureSortDescriptor? { - get { session.sortDescriptor } - nonmutating set { session.sortDescriptor = newValue } - } - - var isReloadingAfterSave: Bool { - get { session.isApplying } - nonmutating set { session.isApplying = newValue } - } - - var lastSaveTime: Date? { - get { session.lastAppliedAt } - nonmutating set { session.lastAppliedAt = newValue } - } - - var gridAdapter: StructureGridAdapter { session.gridAdapter } - - var columns: [ColumnInfo] { - get { session.columns } - nonmutating set { session.columns = newValue } - } - - var indexes: [IndexInfo] { - get { session.indexes } - nonmutating set { session.indexes = newValue } - } - - var foreignKeys: [ForeignKeyInfo] { - get { session.foreignKeys } - nonmutating set { session.foreignKeys = newValue } - } - - var checkConstraints: [CheckConstraintInfo] { - get { session.checkConstraints } - nonmutating set { session.checkConstraints = newValue } - } - - var triggers: [TriggerInfo] { - get { session.triggers } - nonmutating set { session.triggers = newValue } - } - - var ddlStatement: String { - get { session.ddlStatement } - nonmutating set { session.ddlStatement = newValue } - } - - var tabData: StructureTabDataState { - get { session.tabData } - nonmutating set { session.tabData = newValue } - } - - // Observed on its own: a change inside it fires its publisher, never the session's. - @ObservedObject var structureChangeManager: StructureChangeManager - - @AppStorage("structureCodeFontSize", store: AppStorageEnvironment.shared.defaults) var ddlFontSize: Double = 13 - @State var showCopyConfirmation = false - @State var copyResetTask: Task? - @State var isLoading = true - @State var isInitialLoading = true - @State var errorMessage: String? - @State var partsReloadToken = 0 - @AppStorage("skipSchemaPreview", store: AppStorageEnvironment.shared.defaults) var skipSchemaPreview = false - - @State var actionHandler = StructureViewActionHandler() - @FocusState var isTableCommentFocused: Bool - - init( - tableName: String, - connection: DatabaseConnection, - databaseName: String, - schemaName: String?, - toolbarState: ConnectionToolbarState, - coordinator: MainContentCoordinator?, - session: StructureEditingSession - ) { - self.tableName = tableName - self.connection = connection - self.databaseName = databaseName - self.schemaName = schemaName - self.toolbarState = toolbarState - self.coordinator = coordinator - self.session = session - self.structureChangeManager = session.changeManager - } - var body: some View { VStack(spacing: 0) { - tableCommentBar - tabBar + paneBar Divider() - contentArea + paneContent .frame(maxWidth: .infinity, maxHeight: .infinity) } - .task(loadInitialData) - .onReceive(session.gridModel.$selectionRevision) { _ in - publishFooterCapability() - } - .onChange(of: selectedTab) { newValue in - onSelectedTabChanged(newValue) - publishFooterCapability() - } - .onChange(of: columns) { _ in onColumnsChanged() } - .onChange(of: indexes) { _ in onIndexesChanged() } - .onChange(of: foreignKeys) { _ in onForeignKeysChanged() } - .onChange(of: checkConstraints) { _ in onCheckConstraintsChanged() } - .onChange(of: session.tableComment) { _ in onTableCommentChanged() } - .onChange(of: session.tableOptions) { _ in onTableOptionsChanged() } - .onChange(of: session.autoIncrementRead) { read in onAutoIncrementReadChanged(read) } - .onChange(of: session.tableOptionsCatalog) { _ in gridAdapter.characterSetCatalogDidChange() } - .onChange(of: isTableCommentFocused) { isFocused in - if !isFocused { structureChangeManager.endTableCommentRun() } + .task { await session.load() } + .onChange(of: session.pane) { pane in + Task { await session.loadPaneIfNeeded(pane) } } - .onChange(of: searchText) { _ in gridAdapter.refresh() } - .onChange(of: session.serverSupport) { _ in gridAdapter.refresh() } .onAppear { - coordinator?.toolbarState.hasStructureChanges = structureChangeManager.hasChanges - coordinator?.inspectorRowSource = gridAdapter - - gridAdapter.coordinator = coordinator - gridAdapter.moveRowHandler = { [self] fromIndex, toIndex in - beginColumnReorder(fromIndex: fromIndex, toIndex: toIndex) - } - gridAdapter.refresh() - - actionHandler.previewSQL = { self.generateStructurePreviewSQL() } - actionHandler.copyRows = { self.gridAdapter.copyRows(self.session.gridModel.selectedRowIDs) } - actionHandler.pasteRows = { self.gridAdapter.pasteRows() } - actionHandler.undo = { self.gridAdapter.undo() } - actionHandler.redo = { self.gridAdapter.redo() } - actionHandler.addRow = { self.gridAdapter.addRow() } - actionHandler.removeRow = { self.gridAdapter.deleteRows(self.session.gridModel.selectedRowIDs) } - actionHandler.refresh = { self.onRefreshData() } - coordinator?.structureActions = actionHandler - publishFooterCapability() - } - .onDisappear { - structureChangeManager.endTableCommentRun() - // Guarded by identity: SwiftUI does not order the outgoing view's onDisappear before the - // incoming one's onAppear, so an unguarded clear can nil the wiring another tab installed. - if coordinator?.structureActions === actionHandler { - coordinator?.structureActions = nil - coordinator?.toolbarState.hasStructureChanges = false - } - if coordinator?.inspectorRowSource === gridAdapter { - coordinator?.inspectorRowSource = nil - } + session.coordinator = coordinator + syncReadOnly() } - .onChange(of: structureChangeManager.hasChanges) { newValue in - coordinator?.toolbarState.hasStructureChanges = newValue - gridAdapter.refresh() - if !newValue, session.settleOwedRefetch() { - Task { await loadInitialData() } + .onChange(of: session.hasChanges) { hasChanges in + if !hasChanges, session.settleOwedRefetch() { + Task { await session.load() } } } - .onChange(of: structureChangeManager.isHeldForSave) { _ in - publishFooterCapability() - gridAdapter.refresh() - } - .onChange(of: session.appliedVersion) { _ in - Task { await refreshAfterApply() } - } - // A mutation that leaves hasChanges as it was, such as a second added row, moves only this. - .onChange(of: structureChangeManager.reloadVersion) { _ in - gridAdapter.refresh() - } + .onChange(of: toolbarState.safeModeLevel) { _ in syncReadOnly() } } - // MARK: - Tabs - - private var availableTabs: [StructureTab] { - session.availableTabs + private func syncReadOnly() { + let blocksWrites = toolbarState.safeModeLevel.blocksAllWrites + session.editor.readOnlyReason = blocksWrites + ? String(localized: "Safe Mode is read-only for this connection.") + : nil + session.editor.isReadOnly = blocksWrites } - private var tabBar: some View { + private var paneBar: some View { PaneTabBar( title: String(localized: "Structure"), - selection: $session.selectedTab.committingEdit(of: session.gridModel), + selection: $session.pane, identifier: "structure-tab-picker" ) { - ForEach(availableTabs, id: \.self) { tab in - Text(tabLabel(for: tab)).tag(tab) + ForEach(session.availablePanes, id: \.self) { pane in + Text(pane.displayName).tag(pane) } } - .monospacedDigit() .overlay(alignment: .trailing) { - if structureChangeManager.isHeldForSave { + if session.changeManager.isHeldForSave { HStack(spacing: 6) { ProgressView().controlSize(.small) - Text("Saving Changes…") + Text(String(localized: "Saving Changes…")) .font(.callout) .foregroundStyle(.secondary) } @@ -259,182 +68,67 @@ struct TableStructureView: View { } } - // MARK: - Tab Label with Count Badge - - private func tabLabel(for tab: StructureTab) -> String { - StructureTabDataState.label(for: tab, count: loadedCount(for: tab)) - } - - private func loadedCount(for tab: StructureTab) -> Int? { - guard tabData.hasData(tab) else { return nil } - switch tab { - case .columns: return columns.count - case .indexes: return indexes.count - case .foreignKeys: return foreignKeys.count - case .triggers: return triggers.count - case .checkConstraints: return checkConstraints.count - case .options, .ddl, .parts: return nil - } - } - - // MARK: - Content Area - - @ViewBuilder - private var contentArea: some View { - if let error = errorMessage { - errorView(error) - .gridStandInBackground() - } else { - tabContent - } - } - @ViewBuilder - private var tabContent: some View { - switch selectedTab { - case .columns: - structureGrid - case .indexes: - Group { - if shouldShowIndexesEmptyState { - EmptyStateView.indexes { gridAdapter.addRow() } - .gridStandInBackground() - } else { - structureGrid - } - } - .safeAreaInset(edge: .bottom, spacing: 0) { - indexesTabNotes - } - case .foreignKeys: - if shouldShowForeignKeysEmptyState { - EmptyStateView.foreignKeys { gridAdapter.addRow() } - .gridStandInBackground() - } else { - structureGrid - } - case .checkConstraints: - if shouldShowCheckConstraintsEmptyState { - EmptyStateView.checkConstraints { gridAdapter.addRow() } - .gridStandInBackground() - } else { - structureGrid - } + private var paneContent: some View { + switch session.pane { + case .definition: + StructureDefinitionPage( + editor: session.editor, + loadState: session.loadState, + isLoading: session.isLoadingFirstTime, + notes: [.indexes: indexNotes], + onRetry: { session.retry(StructureLoadState.Item($0)) } + ) case .triggers: TriggerDetailView( - triggers: triggers, - scope: scope, - connection: connection, - tableName: tableName, - isLoading: !tabData.hasData(.triggers), - canEdit: editGate.allowsTriggerEditing, + triggers: session.triggers, + scope: session.scope, + connection: session.connection, + tableName: session.tableName, + isLoading: !session.loadState.hasData(.triggers), + failure: session.loadState.failure(.triggers), + onRetry: { session.retry(.triggers) }, + canEdit: session.editor.context.editGate.allowsTriggerEditing, onOpenInEditor: openTriggerInEditor ) - case .options: - tableOptionsView case .ddl: - ddlView + StructureDDLPane( + ddl: session.ddlStatement, + tableName: session.tableName, + databaseType: session.connection.type, + failure: session.loadState.failure(.ddl), + isLoading: !session.loadState.hasData(.ddl), + coordinator: coordinator, + onRetry: { session.retry(.ddl) } + ) case .parts: ClickHousePartsView( - tableName: tableName, - scope: scope, - connection: connection, - reloadToken: partsReloadToken + tableName: session.tableName, + scope: session.scope, + connection: session.connection, + reloadToken: session.partsReloadToken ) } } - private var indexesTabNotes: some View { - VStack(spacing: 0) { - if let note = InvalidIndexNote(indexes: indexes) { - StructureTabNoteView( - systemImage: note.systemImage, - text: note.text, - identifier: "structure-invalid-index-note" - ) - } - if objectKind == .materializedView, - let note = MaterializedViewConcurrentRefreshNote(state: session.concurrentRefresh) { - StructureTabNoteView( - systemImage: note.systemImage, - text: note.text, - identifier: "structure-concurrent-refresh-note" - ) - } + private var indexNotes: [StructureSectionNote] { + var notes: [StructureSectionNote] = [] + if let note = InvalidIndexNote(indexes: session.indexes) { + notes.append(StructureSectionNote( + systemImage: note.systemImage, text: note.text, identifier: "structure-invalid-index-note" + )) } - } - - private var shouldShowIndexesEmptyState: Bool { - tabData.hasData(.indexes) - && structureChangeManager.workingIndexes.isEmpty - && editGate.allows(.addIndex) - } - - private var shouldShowForeignKeysEmptyState: Bool { - tabData.hasData(.foreignKeys) - && structureChangeManager.workingForeignKeys.isEmpty - && connection.type.supportsForeignKeys - && editGate.allows(.addForeignKey) - } - - private var shouldShowCheckConstraintsEmptyState: Bool { - tabData.hasData(.checkConstraints) - && structureChangeManager.workingCheckConstraints.isEmpty - && editGate.allows(.addCheckConstraint) - } - - // MARK: - Structure Grid - - private var structureGrid: some View { - DataGridView(model: session.gridModel) - .safeAreaInset(edge: .top, spacing: 0) { - VStack(spacing: 0) { - NativeSearchField(text: $session.searchText, placeholder: String(localized: "Filter")) - .padding(.horizontal, 6) - .padding(.vertical, 4) - Divider() - } - } - } - - // MARK: - Helper Views - - func errorView(_ message: String) -> some View { - VStack(spacing: 8) { - Image(systemName: "exclamationmark.triangle") - .font(.largeTitle) - .foregroundStyle(.orange) - .accessibilityHidden(true) - RevealedTextView(message) - .foregroundStyle(.secondary) + if session.objectKind == .materializedView, + let note = MaterializedViewConcurrentRefreshNote(state: session.concurrentRefresh) { + notes.append(StructureSectionNote( + systemImage: note.systemImage, text: note.text, identifier: "structure-concurrent-refresh-note" + )) } - .frame(maxWidth: .infinity, maxHeight: .infinity) + return notes } -} -#Preview { - let connection = DatabaseConnection( - name: "Test", - host: "localhost", - port: 3_306, - database: "test", - username: "root", - type: .mysql - ) - return TableStructureView( - tableName: "users", - connection: connection, - databaseName: "test", - schemaName: nil, - toolbarState: ConnectionToolbarState(), - coordinator: nil, - session: StructureEditingSession( - identity: "test.users", - connection: connection, - databaseName: "test", - schemaName: nil, - tableName: "users" - ) - ) - .frame(width: 800, height: 600) + private func openTriggerInEditor(_ trigger: TriggerInfo) { + guard !trigger.statement.isEmpty else { return } + coordinator?.tabManager.addTab(initialQuery: trigger.statement, title: trigger.name) + } } diff --git a/apple/TablePro/Views/Structure/TriggerDetailView.swift b/apple/TablePro/Views/Structure/TriggerDetailView.swift index 7820902422..fb869f481a 100644 --- a/apple/TablePro/Views/Structure/TriggerDetailView.swift +++ b/apple/TablePro/Views/Structure/TriggerDetailView.swift @@ -108,6 +108,8 @@ struct TriggerDetailView: View { let connection: DatabaseConnection let tableName: String let isLoading: Bool + var failure: String? + var onRetry: () -> Void = {} let canEdit: Bool let onOpenInEditor: (TriggerInfo) -> Void @@ -117,7 +119,17 @@ struct TriggerDetailView: View { @State private var actionError: String? var body: some View { - if isLoading { + if let failure, triggers.isEmpty { + UnavailableStateView { + Label(String(localized: "Couldn't Read the Triggers"), systemImage: "exclamationmark.triangle") + } description: { + Text(failure) + } actions: { + Button(String(localized: "Try Again"), action: onRetry) + } + .frame(maxWidth: .infinity, maxHeight: .infinity) + .gridStandInBackground() + } else if isLoading { ProgressView() .frame(maxWidth: .infinity, maxHeight: .infinity) .gridStandInBackground() @@ -279,7 +291,7 @@ internal struct TriggerListPane: View { @ObservedObject internal var state: TriggerInspectorState @StateObject private var grid = StructureListGridModel( - accessibilityLabel: StructureTab.triggers.displayName + accessibilityLabel: StructurePane.triggers.displayName ) private var showsEnabled: Bool { triggers.contains { $0.enabled != nil } } diff --git a/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift b/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift index 7aeb16248d..c47fc806ea 100644 --- a/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift +++ b/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift @@ -100,15 +100,15 @@ struct TriggerInfoMappingTests { } } -struct StructureTabTriggersTests { - @Test("Triggers tab is part of the canonical tab set") +struct StructurePaneTriggersTests { + @Test("Triggers is one of the Structure view's panes") func triggersInAllCases() { - #expect(StructureTab.allCases.contains(.triggers)) + #expect(StructurePane.allCases.contains(.triggers)) } - @Test("Triggers tab has a localized display name") + @Test("The Triggers pane has a localized display name") func triggersDisplayName() { - #expect(StructureTab.triggers.displayName == "Triggers") + #expect(StructurePane.triggers.displayName == "Triggers") } } @@ -221,13 +221,8 @@ struct TriggerApplyExecutionTests { #expect(stub.executedQueries == ["DROP TRIGGER t", "CREATE TRIGGER t", "RESTORE t"]) } - /// The session driver holds whatever transaction a query tab left open, so a trigger edit - /// with a BEGIN of its own ran inside it and committed or rolled back the tab's work. - /// - /// The gate is supplied rather than taken from `ExecutionGateProvider`. A drop is a - /// `.destructiveQuery`, which the real gate confirms with an `NSAlert`, and an alert raised - /// with no window runs application-modal: on a CI runner nobody answers it, so the whole - /// unit job stops there and is killed by its timeout rather than failing. + // A trigger edit's own BEGIN ran inside the transaction a query tab left open on the session driver. + // The gate is injected: the real one confirms a drop with an NSAlert nobody answers on CI. @Test("Apply and drop run on the pooled connection and leave the session driver alone") func applyAndDropRunOnThePooledConnection() async throws { let connection = TestFixtures.makeConnection(database: "app", type: .postgresql) @@ -241,12 +236,7 @@ struct TriggerApplyExecutionTests { let scope = DatabaseScope(connectionId: connection.id, database: "app", schema: "public") let pooledStub = StubTriggerDriver() pooledStub.transactionalDDL = true - /// The drop statement is built on the driver that will run it, because an engine that - /// qualifies its DDL with the connection's current database would otherwise write the - /// session's database into a statement the pooled connection runs somewhere else. Both - /// stubs can produce it, so the assertions below are what say which one was asked: a - /// regression back to the session driver shows up as a query it executed rather than as a - /// statement nobody could generate. + // Both stubs build the drop, so only the queries each one executed show which driver was asked. pooledStub.dropToReturn = "DROP TRIGGER t" let pooledAdapter = PluginDriverAdapter(connection: connection, pluginDriver: pooledStub) try await pooledAdapter.connect() diff --git a/apple/TableProTests/Core/SchemaTracking/CreateTableCommentCompositionTests.swift b/apple/TableProTests/Core/SchemaTracking/CreateTableCommentCompositionTests.swift index 48c482d890..cc0b03a075 100644 --- a/apple/TableProTests/Core/SchemaTracking/CreateTableCommentCompositionTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/CreateTableCommentCompositionTests.swift @@ -88,7 +88,7 @@ struct CreateTableCommentCompositionTests { ) #expect(composed.issues.map(\.message) == ["This database cannot store a table comment."]) - #expect(composed.issues.first?.tab == .columns) + #expect(composed.issues.first?.section == .columns) } @Test("Every statement the driver creates the table with arrives in order, ahead of the indexes") diff --git a/apple/TableProTests/Core/SchemaTracking/IndexDecliningDriverTests.swift b/apple/TableProTests/Core/SchemaTracking/IndexDecliningDriverTests.swift index 552e7e95e6..e55082428c 100644 --- a/apple/TableProTests/Core/SchemaTracking/IndexDecliningDriverTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/IndexDecliningDriverTests.swift @@ -67,7 +67,7 @@ struct IndexDecliningDriverTests { #expect(composed.statements.count == 1) #expect(composed.issues.map(\.message) == ["Create Table cannot add an index on this database."]) - #expect(composed.issues.first?.tab == .indexes) + #expect(composed.issues.first?.section == .indexes) #expect(composed.issues.first?.row == 0) } } diff --git a/apple/TableProTests/Core/SchemaTracking/PrimaryKeyStatementOrderTests.swift b/apple/TableProTests/Core/SchemaTracking/PrimaryKeyStatementOrderTests.swift new file mode 100644 index 0000000000..d79b454133 --- /dev/null +++ b/apple/TableProTests/Core/SchemaTracking/PrimaryKeyStatementOrderTests.swift @@ -0,0 +1,86 @@ +// +// PrimaryKeyStatementOrderTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private final class KeyDDLDriver: PluginDriverStub, @unchecked Sendable { + var isConnected: Bool { true } + var currentSchema: String? { "public" } + + func ping() async throws {} + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func generateDropColumnSQL(table: String, columnName: String) -> String? { + "ALTER TABLE \(table) DROP COLUMN \(columnName)" + } + + func generateAddColumnSQL(table: String, column: PluginColumnDefinition) -> String? { + "ALTER TABLE \(table) ADD COLUMN \(column.name)" + } + + func generateModifyPrimaryKeySQL(table: String, oldColumns: [String], newColumns: [String], constraintName: String?) -> [String]? { + var statements: [String] = [] + if !oldColumns.isEmpty { statements.append("ALTER TABLE \(table) DROP CONSTRAINT \(constraintName ?? "pk")") } + if !newColumns.isEmpty { statements.append("ALTER TABLE \(table) ADD PRIMARY KEY (\(newColumns.joined(separator: ", ")))") } + return statements + } +} + +struct PrimaryKeyStatementOrderTests { + private func column(_ name: String) -> EditableColumnDefinition { + EditableColumnDefinition.from(ColumnInfo(name: name, dataType: "INT", isNullable: false, isPrimaryKey: false)) + } + + // The column's drop takes the key with it, and on MySQL an AUTO_INCREMENT key cannot be dropped ahead of its column. + @Test("A key whose every column is dropped is left to the drop, and the new key is added after the columns") + func fullyDroppedKeyIsNotDroppedAgain() throws { + let statements = try SchemaStatementGenerator(tableName: "orders", pluginDriver: KeyDDLDriver()) + .generate(changes: [ + .deleteColumn(column("a")), + .addColumn(column("c")), + .modifyPrimaryKey(old: ["a"], new: ["b", "c"]), + ]) + .map(\.sql) + + #expect(statements == [ + "ALTER TABLE orders DROP COLUMN a;", + "ALTER TABLE orders ADD COLUMN c;", + "ALTER TABLE orders ADD PRIMARY KEY (b, c);", + ]) + } + + @Test("A key that keeps some columns is dropped before a dropped member goes") + func partlyDroppedKeyIsDroppedFirst() throws { + let statements = try SchemaStatementGenerator(tableName: "orders", pluginDriver: KeyDDLDriver()) + .generate(changes: [ + .deleteColumn(column("a")), + .modifyPrimaryKey(old: ["a", "b"], new: ["b", "c"]), + ]) + .map(\.sql) + + #expect(statements == [ + "ALTER TABLE orders DROP CONSTRAINT pk;", + "ALTER TABLE orders DROP COLUMN a;", + "ALTER TABLE orders ADD PRIMARY KEY (b, c);", + ]) + } + + @Test("A key change that drops no key column stays one change after the columns") + func keyChangeWithoutDroppedColumnStaysWhole() throws { + let statements = try SchemaStatementGenerator(tableName: "orders", pluginDriver: KeyDDLDriver()) + .generate(changes: [.modifyPrimaryKey(old: ["a"], new: ["a", "b"])]) + .map(\.sql) + + #expect(statements == [ + "ALTER TABLE orders DROP CONSTRAINT pk;", + "ALTER TABLE orders ADD PRIMARY KEY (a, b);", + ]) + } +} diff --git a/apple/TableProTests/Core/SchemaTracking/SchemaOperationRefusalTests.swift b/apple/TableProTests/Core/SchemaTracking/SchemaOperationRefusalTests.swift index f92693eab3..22637772ec 100644 --- a/apple/TableProTests/Core/SchemaTracking/SchemaOperationRefusalTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/SchemaOperationRefusalTests.swift @@ -334,7 +334,7 @@ struct SchemaOperationRefusalTests { let composed = CreateTableStatementComposer.compose(plan: plan, driver: legacyDriver(), schema: nil) #expect(composed.statements.isEmpty) #expect(composed.issues.map(\.message) == [Self.generatedReason]) - #expect(composed.issues.first?.tab == .columns) + #expect(composed.issues.first?.section == .columns) } @Test("Create Table flags a refused index on its own row and still creates the table") @@ -351,7 +351,7 @@ struct SchemaOperationRefusalTests { let composed = CreateTableStatementComposer.compose(plan: plan, driver: legacyDriver(), schema: nil) #expect(composed.statements == ["CREATE TABLE orders (...)", "CREATE INDEX ix_btree ON orders"]) #expect(composed.issues.count == 1) - #expect(composed.issues.first?.tab == .indexes) + #expect(composed.issues.first?.section == .indexes) #expect(composed.issues.first?.row == 1) #expect(composed.issues.first?.message == Self.brinReason) } diff --git a/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerSaveHoldTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerSaveHoldTests.swift index 16ba8d3a07..3560f1f28c 100644 --- a/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerSaveHoldTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerSaveHoldTests.swift @@ -8,10 +8,8 @@ import Foundation import TableProPluginKit import Testing -/// A save writes the edits staged when it was pressed and clears them when it lands. Staging stayed -/// open in between, so on MongoDB, where the save reads every document it changes before writing, -/// an edit made during that read was left out of the script and then cleared with the edits that -/// did run. +// Staging stayed open during a save, so on MongoDB an edit made while the save read the documents was left out +// of the script and then cleared with the edits that ran. @MainActor struct StructureChangeManagerSaveHoldTests { private func makeManager() -> StructureChangeManager { @@ -75,7 +73,7 @@ struct StructureChangeManagerSaveHoldTests { rename(manager, at: 2, to: "mail") manager.deleteColumn(id: manager.workingColumns[2].id) manager.performAsOneUndoStep { manager.deleteColumn(id: manager.workingColumns[0].id) } - manager.undoDelete(for: .columns, at: 2) + manager.undoDelete(section: .columns, id: manager.workingColumns[2].id) manager.stageTableComment("Users of the shop") manager.undo() manager.redo() @@ -120,8 +118,7 @@ struct StructureChangeManagerSaveHoldTests { #expect(manager.getChangesArray().count == 2) } - /// A completion that arrives for a hold that has already ended must not clear what was staged - /// after it. Only the hold a save still owns can clear, and only the edits it took. + // Only the hold a save still owns can clear, and only the edits it took. @Test("An ended hold cannot clear edits staged after it") func endedHoldClearsNothing() throws { let manager = makeManager() diff --git a/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerStagingTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerStagingTests.swift new file mode 100644 index 0000000000..257d6dbda8 --- /dev/null +++ b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerStagingTests.swift @@ -0,0 +1,273 @@ +// +// StructureChangeManagerStagingTests.swift +// TableProTests +// + +import Foundation +import TableProPluginKit +import Testing +@testable import TablePro + +@MainActor +struct StructureChangeManagerStagingTests { + private func loadedManager() -> StructureChangeManager { + let manager = StructureChangeManager() + manager.loadSchema( + tableName: "orders", + columns: [ + ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil), + ColumnInfo(name: "email", dataType: "VARCHAR(255)", isNullable: true, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil) + ], + indexes: [IndexInfo(name: "PRIMARY", columns: ["id"], isUnique: true, isPrimary: true, type: "BTREE")], + foreignKeys: [], + primaryKey: ["id"] + ) + return manager + } + + private func column(_ name: String, in manager: StructureChangeManager) -> EditableColumnDefinition { + manager.workingColumns.first { $0.name == name }! + } + + @Test("Making a column part of the key stages the key change the drivers write, not a column change") + func primaryKeyCellStagesKeyChange() { + let manager = loadedManager() + var email = column("email", in: manager) + email.isPrimaryKey = true + manager.updateColumn(id: email.id, with: email) + + let changes = manager.getChangesArray() + #expect(changes.contains(.modifyPrimaryKey(old: ["id"], new: ["id", "email"]))) + #expect(!changes.contains { if case .modifyColumn = $0 { true } else { false } }) + #expect(manager.workingPrimaryKey == ["id", "email"]) + } + + @Test("Taking the only key column out stages an empty key") + func removingKeyColumnStagesEmptyKey() { + let manager = loadedManager() + var id = column("id", in: manager) + id.isPrimaryKey = false + manager.updateColumn(id: id.id, with: id) + + #expect(manager.getChangesArray().contains(.modifyPrimaryKey(old: ["id"], new: []))) + } + + @Test("One undo reverts the key change and the column's flag together") + func primaryKeyChangeUndoesAsOneStep() { + let manager = loadedManager() + var email = column("email", in: manager) + email.isPrimaryKey = true + manager.updateColumn(id: email.id, with: email) + + manager.undo() + + #expect(manager.workingPrimaryKey == ["id"]) + #expect(column("email", in: manager).isPrimaryKey == false) + #expect(!manager.hasChanges) + } + + @Test("A key toggle together with another edit keeps the column change") + func keyToggleKeepsOtherColumnEdits() { + let manager = loadedManager() + var email = column("email", in: manager) + email.isNullable = false + manager.updateColumn(id: email.id, with: email) + email.isPrimaryKey = true + manager.updateColumn(id: email.id, with: email) + + let changes = manager.getChangesArray() + #expect(changes.contains { if case .modifyColumn = $0 { true } else { false } }) + #expect(changes.contains { if case .modifyPrimaryKey = $0 { true } else { false } }) + } + + @Test("Undo Delete on an edited column stages its edit again") + func undoDeleteRestagesEdit() { + let manager = loadedManager() + var email = column("email", in: manager) + email.dataType = "TEXT" + manager.updateColumn(id: email.id, with: email) + manager.deleteColumn(id: email.id) + + manager.undoDelete(section: .columns, id: email.id) + + guard case .modifyColumn(_, let new)? = manager.pendingChanges[.column(email.id)] else { + Issue.record("the edit made before the delete is no longer staged") + return + } + #expect(new.dataType == "TEXT") + } + + @Test("Undoing a delete through the undo stack stages the earlier edit again") + func undoStackDeleteRestagesEdit() { + let manager = loadedManager() + var email = column("email", in: manager) + email.dataType = "TEXT" + manager.updateColumn(id: email.id, with: email) + manager.deleteColumn(id: email.id) + + manager.undo() + + guard case .modifyColumn? = manager.pendingChanges[.column(email.id)] else { + Issue.record("the edit made before the delete is no longer staged") + return + } + } + + @Test("The Edit menu title names the change it undoes") + func undoMenuTitleNamesTheChange() { + let manager = loadedManager() + manager.deleteColumn(id: column("email", in: manager).id) + + #expect(manager.undoMenuItemTitle.contains(String(localized: "Delete Column"))) + } + + @Test("Redoing a key toggle stages only the key change") + func redoKeepsKeyOnlyChangeOffColumns() { + let manager = loadedManager() + var id = column("id", in: manager) + id.isPrimaryKey = false + manager.updateColumn(id: id.id, with: id) + manager.undo() + manager.redo() + + let changes = manager.getChangesArray() + #expect(changes.contains(.modifyPrimaryKey(old: ["id"], new: []))) + #expect(!changes.contains { if case .modifyColumn = $0 { true } else { false } }) + } + + @Test("Renaming a column the staged key names renames it in the key") + func renameFollowsStagedKey() { + let manager = loadedManager() + var email = column("email", in: manager) + email.isPrimaryKey = true + manager.updateColumn(id: email.id, with: email) + email.name = "address" + manager.updateColumn(id: email.id, with: email) + + #expect(manager.workingPrimaryKey == ["id", "address"]) + #expect(manager.getChangesArray().contains(.modifyPrimaryKey(old: ["id"], new: ["id", "address"]))) + #expect(manager.canCommit) + } + + @Test("A rename leaves an unchanged key alone") + func renameWithoutStagedKeyStagesNoKeyChange() { + let manager = loadedManager() + var id = column("id", in: manager) + id.name = "order_id" + manager.updateColumn(id: id.id, with: id) + + #expect(!manager.getChangesArray().contains { if case .modifyPrimaryKey = $0 { true } else { false } }) + } + + @Test("Removing a column the staged key names takes it out of the key") + func deletingStagedKeyMemberUpdatesKey() { + let manager = loadedManager() + manager.addNewColumn() + var added = manager.workingColumns.last! + added.name = "tenant_id" + added.dataType = "INT" + manager.updateColumn(id: added.id, with: added) + added.isPrimaryKey = true + manager.updateColumn(id: added.id, with: added) + #expect(manager.workingPrimaryKey == ["id", "tenant_id"]) + + manager.deleteColumn(id: added.id) + + #expect(manager.workingPrimaryKey == ["id"]) + #expect(!manager.getChangesArray().contains { if case .modifyPrimaryKey = $0 { true } else { false } }) + } + + @Test("Undo Delete on a column the staged key named puts it back in the key") + func undoDeleteRestoresKeyMembership() { + let manager = loadedManager() + var email = column("email", in: manager) + email.isNullable = false + manager.updateColumn(id: email.id, with: email) + email.isPrimaryKey = true + manager.updateColumn(id: email.id, with: email) + manager.deleteColumn(id: email.id) + #expect(manager.workingPrimaryKey == ["id"]) + + manager.undoDelete(section: .columns, id: email.id) + + #expect(manager.workingPrimaryKey == ["id", "email"]) + #expect(manager.getChangesArray().contains(.modifyPrimaryKey(old: ["id"], new: ["id", "email"]))) + } + + @Test("A key change after a rename names the renamed column") + func keyChangeAfterRenameUsesNewNames() { + let manager = loadedManager() + var id = column("id", in: manager) + id.name = "order_id" + manager.updateColumn(id: id.id, with: id) + var email = column("email", in: manager) + email.isPrimaryKey = true + manager.updateColumn(id: email.id, with: email) + + #expect(manager.workingPrimaryKey == ["order_id", "email"]) + #expect(manager.canCommit) + } + + @Test("Undo Delete puts a key column back where it was in the key") + func undoDeleteKeepsKeyOrder() { + let manager = StructureChangeManager() + manager.loadSchema( + tableName: "t", + columns: ["a", "b", "c"].map { + ColumnInfo(name: $0, dataType: "INT", isNullable: false, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil) + }, + indexes: [], + foreignKeys: [], + primaryKey: [] + ) + for name in ["a", "b", "c"] { + var column = manager.workingColumns.first { $0.name == name }! + column.isPrimaryKey = true + manager.updateColumn(id: column.id, with: column) + } + let a = manager.workingColumns.first { $0.name == "a" }! + manager.deleteColumn(id: a.id) + #expect(manager.workingPrimaryKey == ["b", "c"]) + + manager.undoDelete(section: .columns, id: a.id) + + #expect(manager.workingPrimaryKey == ["a", "b", "c"]) + } + + @Test("Deleting the old key column, then marking another, stages a key that names only surviving columns") + func deleteThenMarkStagesSurvivingKey() { + let manager = loadedManager() + manager.deleteColumn(id: column("id", in: manager).id) + var email = column("email", in: manager) + email.isPrimaryKey = true + manager.updateColumn(id: email.id, with: email) + + #expect(manager.workingPrimaryKey == ["email"]) + #expect(manager.getChangesArray().contains(.modifyPrimaryKey(old: ["id"], new: ["email"]))) + #expect(manager.canCommit) + } + + @Test("Renaming a key column without changing the key stages no key change") + func renameOfKeyColumnStagesNoKeyChange() { + let manager = loadedManager() + var id = column("id", in: manager) + id.name = "order_id" + manager.updateColumn(id: id.id, with: id) + manager.deleteColumn(id: id.id) + manager.undoDelete(section: .columns, id: id.id) + + #expect(!manager.getChangesArray().contains { if case .modifyPrimaryKey = $0 { true } else { false } }) + #expect(manager.workingPrimaryKey == ["order_id"]) + } + + @Test("Deleting the key column alone stages no key change, as the database drops it with the column") + func deletingKeyColumnAloneStagesNoKeyChange() { + let manager = loadedManager() + manager.deleteColumn(id: column("id", in: manager).id) + + #expect(!manager.getChangesArray().contains { if case .modifyPrimaryKey = $0 { true } else { false } }) + } +} diff --git a/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerTableCommentTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerTableCommentTests.swift index f393fe706c..a368e99759 100644 --- a/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerTableCommentTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerTableCommentTests.swift @@ -36,21 +36,6 @@ struct StructureChangeManagerTableCommentTests { } } - private func structureAdapter(_ manager: StructureChangeManager, on tab: StructureTab) -> StructureGridAdapter { - let adapter = StructureGridAdapter( - structureChangeManager: manager, - connection: TestFixtures.makeConnection(type: .postgresql), - tableName: "orders", - databaseName: "shop", - schemaName: nil, - objectKind: .table, - isSessionBound: false - ) - adapter.store.switchSubTab(to: tab) - adapter.refresh() - return adapter - } - private func rename(_ manager: StructureChangeManager, at row: Int, to name: String) { var column = manager.workingColumns[row] column.name = name @@ -196,31 +181,11 @@ struct StructureChangeManagerTableCommentTests { #expect(manager.canUndo) } - @Test("Undo on the DDL sub-tab reaches the comment step and nothing behind it") - func undoOnDDLReachesOnlyTheComment() { + @Test("The Edit menu names the comment while its run is open") + func undoTitleNamesTheOpenRun() { let manager = makeManager() - rename(manager, at: 1, to: "amount") manager.stageTableComment("Orders") - let adapter = structureAdapter(manager, on: .ddl) - - adapter.undo() - #expect(manager.tableComment.text == "Customer orders") - - adapter.undo() - #expect(manager.workingColumns[1].name == "amount") - - adapter.redo() - #expect(manager.tableComment.text == "Orders") - } - - @Test("Undo on the Triggers sub-tab leaves a column edit alone") - func undoOnTriggersSkipsColumnEdits() { - let manager = makeManager() - rename(manager, at: 1, to: "amount") - let adapter = structureAdapter(manager, on: .triggers) - - adapter.undo() - #expect(manager.workingColumns[1].name == "amount") + #expect(manager.undoMenuItemTitle.contains(String(localized: "Edit Comment"))) } } diff --git a/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerTableOptionsTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerTableOptionsTests.swift index 80422ba881..f78a37302b 100644 --- a/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerTableOptionsTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerTableOptionsTests.swift @@ -345,16 +345,4 @@ struct StructureChangeManagerTableOptionsTests { #expect(defaultOnly.validationErrors.isEmpty) #expect(defaultOnly.canCommit) } - - @Test("The Options sub-tab has no rows to mark") - func optionsTabHasNoRowState() { - let manager = makeManager() - manager.deleteColumn(id: manager.workingColumns[1].id) - - let state = manager.deleteInsertState(for: 1, tab: .options) - #expect(!state.isDeleted) - #expect(!state.isInserted) - manager.undoDelete(for: .options, at: 1) - #expect(manager.hasChanges) - } } diff --git a/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerUndoTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerUndoTests.swift index f6e4007a47..cbb89741c5 100644 --- a/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerUndoTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/StructureChangeManagerUndoTests.swift @@ -244,10 +244,7 @@ struct StructureChangeManagerUndoTests { #expect(manager.workingColumns.contains(where: { $0.id == newCol.id })) } - /// The other half of the grouping contract, and the one with no test before this. The structure - /// grid deletes a multi-row selection by calling `deleteColumn` once per row, so without an - /// explicit group a five-row delete would take five undos. `performAsOneUndoStep` is what the - /// two grid delegates wrap those loops in. + // The grid deletes a multi-row selection one `deleteColumn` per row; without a group, five rows took five undos. @Test("A batch wrapped in performAsOneUndoStep undoes as one step") @MainActor func batchedDeletesUndoTogether() { let manager = makeManager() @@ -271,6 +268,39 @@ struct StructureChangeManagerUndoTests { #expect(manager.canUndo == false) } + @Test("Duplicating several rows is one undo step") + @MainActor func duplicatedRowsUndoTogether() { + let manager = makeManager() + loadSampleSchema(manager) + let names = manager.workingColumns.map(\.name) + + StructureGridEdits.duplicate(sourceIndices: [2, 1], tab: .columns, manager: manager) + #expect(manager.workingColumns.count == names.count + 2) + #expect(manager.workingColumns.suffix(2).map(\.name) == ["name", "email"]) + + manager.undo() + + #expect(manager.workingColumns.map(\.name) == names) + #expect(manager.hasChanges == false) + #expect(manager.canUndo == false) + } + + @Test("Duplicating a primary index stages a plain copy, and one undo removes it") + @MainActor func duplicatedPrimaryIndexIsNotPrimary() throws { + let manager = makeManager() + loadSampleSchema(manager) + + StructureGridEdits.duplicate(sourceIndices: [0], tab: .indexes, manager: manager) + let copy = try #require(manager.workingIndexes.last) + #expect(manager.workingIndexes.count == 2) + #expect(!copy.isPrimary) + + manager.undo() + + #expect(manager.workingIndexes.count == 1) + #expect(manager.hasChanges == false) + } + // MARK: - Discard Clears Undo @Test("Discard changes clears undo stack") diff --git a/apple/TableProTests/Core/SchemaTracking/StructureChangeNameReuseTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureChangeNameReuseTests.swift index d4e02f007c..63343c06b1 100644 --- a/apple/TableProTests/Core/SchemaTracking/StructureChangeNameReuseTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/StructureChangeNameReuseTests.swift @@ -7,12 +7,8 @@ import Foundation @testable import TablePro import Testing -/// A name belongs to the rows the table keeps after the save. -/// -/// The save drops every index and check constraint before it adds or renames one, so a name a row -/// is being deleted under is free by the time anything takes it. The duplicate check still counted -/// the deleted row, and "delete idx_a, add a new idx_a" was refused as a duplicate of the row on -/// its way out. +// The save drops indexes and check constraints before it adds one, so a deleted row's name is free; the duplicate +// check still counted it and refused "delete idx_a, add a new idx_a". @MainActor struct StructureChangeNameReuseTests { private func manager( @@ -69,8 +65,8 @@ struct StructureChangeNameReuseTests { // MARK: - Indexes - /// Measured on SQLite 3.54, PostgreSQL 17.11 and MariaDB 13.0.2: `CREATE INDEX idx_a` fails while - /// `idx_a` exists, and succeeds straight after `DROP INDEX idx_a` in the same save. + // Measured on SQLite 3.54, PostgreSQL 17.11 and MariaDB 13.0.2: `CREATE INDEX idx_a` fails while + // `idx_a` exists, and succeeds straight after `DROP INDEX idx_a` in the same save. @Test("Deleting an index frees its name for a new one in the same save") func deletingAnIndexFreesItsNameForANewOne() throws { let manager = manager(indexes: [loadedIndex("idx_a", on: "body")]) @@ -93,8 +89,8 @@ struct StructureChangeNameReuseTests { #expect(manager.validationErrors.isEmpty) } - /// The row menu's **Duplicate** stages a copy under the same name, which is the everyday way to - /// build a replacement before the original goes. + // The row menu's **Duplicate** stages a copy under the same name, which is the everyday way to + // build a replacement before the original goes. @Test("Duplicating an index and then deleting the original saves") func duplicatingAnIndexThenDeletingTheOriginalSaves() throws { let manager = manager(indexes: [loadedIndex("idx_a", on: "body")]) @@ -116,7 +112,7 @@ struct StructureChangeNameReuseTests { manager.deleteIndex(id: original.id) #expect(manager.canCommit) - manager.undoDelete(for: .indexes, at: 0) + manager.undoDelete(section: .indexes, id: original.id) #expect(!manager.canCommit) #expect(manager.validationErrors.count == 2) #expect(errors(in: manager) == ["Duplicate index name: idx_a"]) @@ -156,8 +152,8 @@ struct StructureChangeNameReuseTests { // MARK: - Check constraints - /// Measured on SQLite 3.54 and PostgreSQL 17.11: `DROP CONSTRAINT c` then `ADD CONSTRAINT c` - /// saves, and `ADD CONSTRAINT c` alone fails while `c` exists. + // Measured on SQLite 3.54 and PostgreSQL 17.11: `DROP CONSTRAINT c` then `ADD CONSTRAINT c` + // saves, and `ADD CONSTRAINT c` alone fails while `c` exists. @Test("Deleting a check constraint frees its name for a new one in the same save") func deletingACheckConstraintFreesItsName() throws { let manager = manager(checkConstraints: [CheckConstraintInfo(name: "c", expression: "a > 0")]) @@ -192,8 +188,8 @@ struct StructureChangeNameReuseTests { #expect(errors(in: manager) == ["Duplicate constraint name: c"]) } - /// Measured on SQLite 3.54: `ADD CONSTRAINT "C"` beside `c` fails with "constraint C already - /// exists", and MariaDB 13.0.2 refuses it with ERROR 1826. + // Measured on SQLite 3.54: `ADD CONSTRAINT "C"` beside `c` fails with "constraint C already + // exists", and MariaDB 13.0.2 refuses it with ERROR 1826. @Test("A check constraint added beside one whose name differs only in case is a duplicate") func addingAConstraintUnderACaseVariantOfAKeptNameIsADuplicate() { let manager = manager(checkConstraints: [CheckConstraintInfo(name: "c", expression: "a > 0")]) @@ -205,8 +201,8 @@ struct StructureChangeNameReuseTests { // MARK: - Two loaded check constraints under one name - /// SQLite 3.54 accepts `CONSTRAINT c CHECK (a > 0), CONSTRAINT c CHECK (a < 10)` in one - /// `CREATE TABLE`, and the Structure tab lists both. + // SQLite 3.54 accepts `CONSTRAINT c CHECK (a > 0), CONSTRAINT c CHECK (a < 10)` in one + // `CREATE TABLE`, and the Structure tab lists both. private func tableWithTwoConstraintsNamedC(secondName: String = "c") -> StructureChangeManager { manager(checkConstraints: [ CheckConstraintInfo(name: "c", expression: "a > 0"), @@ -225,8 +221,8 @@ struct StructureChangeNameReuseTests { #expect(manager.validationErrors.isEmpty) } - /// Measured on SQLite 3.54: `DROP CONSTRAINT c` removes the first `c` in the table's text, - /// whichever row was picked. + // Measured on SQLite 3.54: `DROP CONSTRAINT c` removes the first `c` in the table's text, + // whichever row was picked. @Test("Deleting one of two check constraints under one name is refused") func droppingOneOfTwoSameNamedConstraintsIsRefused() throws { let manager = tableWithTwoConstraintsNamedC() @@ -261,7 +257,7 @@ struct StructureChangeNameReuseTests { #expect(manager.validationErrors.count == 1) } - /// Measured on SQLite 3.54: `DROP CONSTRAINT "C"` on a table holding `c` and `C` drops `c`. + // Measured on SQLite 3.54: `DROP CONSTRAINT "C"` on a table holding `c` and `C` drops `c`. @Test("Check constraint names that differ only in case are one name") func caseVariantConstraintNamesAreShared() throws { let manager = tableWithTwoConstraintsNamedC(secondName: "C") @@ -284,8 +280,8 @@ struct StructureChangeNameReuseTests { #expect(manager.validationErrors.isEmpty) } - /// Measured on SQLite 3.54: after `DROP CONSTRAINT c` twice and `ADD CONSTRAINT c`, the add of - /// `C` fails with "constraint C already exists". + // Measured on SQLite 3.54: after `DROP CONSTRAINT c` twice and `ADD CONSTRAINT c`, the add of + // `C` fails with "constraint C already exists". @Test("Replacing both check constraints under a shared name with c and C is a duplicate") func replacingBothWithCaseVariantsIsADuplicate() { let manager = tableWithTwoConstraintsNamedC() @@ -299,8 +295,8 @@ struct StructureChangeNameReuseTests { #expect(errors(in: manager) == ["Duplicate constraint name: c", "Duplicate constraint name: C"]) } - /// SQLite has no `RENAME CONSTRAINT`, so each rename is a drop by the shared name and an add of - /// the row's own definition. Whichever `c` each drop takes, both go and both come back right. + // SQLite has no `RENAME CONSTRAINT`, so each rename is a drop by the shared name and an add of + // the row's own definition. Whichever `c` each drop takes, both go and both come back right. @Test("Renaming every check constraint under a shared name saves") func renamingBothSameNamedConstraintsSaves() throws { let manager = tableWithTwoConstraintsNamedC() @@ -314,8 +310,8 @@ struct StructureChangeNameReuseTests { #expect(manager.validationErrors.isEmpty) } - /// Measured on SQLite 3.54: `DROP CONSTRAINT c; DROP CONSTRAINT c; ADD CONSTRAINT c CHECK (a < 20)` - /// leaves exactly the rewritten constraint. + // Measured on SQLite 3.54: `DROP CONSTRAINT c; DROP CONSTRAINT c; ADD CONSTRAINT c CHECK (a < 20)` + // leaves exactly the rewritten constraint. @Test("Deleting one check constraint under a shared name and rewriting the other saves") func deletingOneAndRewritingTheOtherSaves() throws { let manager = tableWithTwoConstraintsNamedC() @@ -328,8 +324,8 @@ struct StructureChangeNameReuseTests { #expect(manager.validationErrors.isEmpty) } - /// Both would be added back as `c`, and the second `ADD CONSTRAINT c` fails on SQLite 3.54 with - /// "constraint c already exists". + // Both would be added back as `c`, and the second `ADD CONSTRAINT c` fails on SQLite 3.54 with + // "constraint c already exists". @Test("Rewriting both check constraints under their shared name is a duplicate") func rewritingBothUnderTheSharedNameIsADuplicate() throws { let manager = tableWithTwoConstraintsNamedC() diff --git a/apple/TableProTests/Core/SchemaTracking/StructureEditingSessionTableOptionsTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureEditingSessionTableOptionsTests.swift index c5937e68a0..550dafb911 100644 --- a/apple/TableProTests/Core/SchemaTracking/StructureEditingSessionTableOptionsTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/StructureEditingSessionTableOptionsTests.swift @@ -164,12 +164,4 @@ struct StructureEditingSessionTableOptionsTests { #expect(session.tableOptionsCatalog == .loaded(Self.catalog)) } - - @Test("The Options sub-tab shows no grid") - func optionsTabHasNoGrid() { - let session = makeSession() - session.selectedTab = .options - - #expect(session.activeGridModel == nil) - } } diff --git a/apple/TableProTests/Core/SchemaTracking/TableCommentStatementTests.swift b/apple/TableProTests/Core/SchemaTracking/TableCommentStatementTests.swift index 5fa16cdebe..9ac049ba09 100644 --- a/apple/TableProTests/Core/SchemaTracking/TableCommentStatementTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/TableCommentStatementTests.swift @@ -117,7 +117,7 @@ struct TableCommentStatementTests { _ = try generator.generate(changes: [.modifyTableComment(old: nil, new: "Orders")]) Issue.record("Expected the save to be refused") } catch { - #expect(error.localizedDescription.contains("Change comment on table")) + #expect(error.localizedDescription.contains(String(localized: "Change the table comment"))) } } diff --git a/apple/TableProTests/Helpers/TestFixtures.swift b/apple/TableProTests/Helpers/TestFixtures.swift index d36bec3889..c982ab1466 100644 --- a/apple/TableProTests/Helpers/TestFixtures.swift +++ b/apple/TableProTests/Helpers/TestFixtures.swift @@ -276,3 +276,12 @@ enum TestFixtures { ) } } + +@MainActor +extension StructureEditingSession { + // What a finished load leaves behind, for a test that stages straight into the change manager. + func markBaselineAdopted() { + hasBaseline = true + editor.isAwaitingBaseline = false + } +} diff --git a/apple/TableProTests/Models/InspectorFieldLayoutTests.swift b/apple/TableProTests/Models/InspectorFieldLayoutTests.swift deleted file mode 100644 index 99cdc123a1..0000000000 --- a/apple/TableProTests/Models/InspectorFieldLayoutTests.swift +++ /dev/null @@ -1,126 +0,0 @@ -// -// InspectorFieldLayoutTests.swift -// TableProTests -// - -import Foundation -@testable import TablePro -import Testing - -@MainActor -struct InspectorFieldLayoutTests { - private static let everyKind: [FieldEditorKind] = [ - .singleLine, - .boolean, - .schemaText, - .typePicker, - .enumPicker(values: ["a", "b"]), - .setPicker(values: ["a", "b"]), - .arrayElements(element: .scalar, values: []), - .arrayElements(element: .json, values: []), - .valuePicker(options: []), - .multiLine, - .json, - .phpSerialized, - .blobHex, - .image(.raster("public.png")), - .geometry(GeometryFieldDescriptor(textEditor: .multiLine, source: .spatialColumn)), - .geometry(GeometryFieldDescriptor(textEditor: .json, source: .jsonColumn)), - .geometry(GeometryFieldDescriptor(textEditor: .hex, source: .binary)) - ] - - /// A column name is user data of no bounded length, so no label lane can hold every one of them - /// and a lane wide enough to try takes the width the value needs. Measured at the pane's 270pt - /// minimum, a side-by-side data row truncates the name *and* the value; stacking shows both. - @Test("Every data field stacks, whatever editor it resolves to") - func dataFieldsAlwaysStack() { - for kind in Self.everyKind { - #expect( - InspectorFieldLayout.resolve(for: kind, isSchemaField: false) == .stacked, - "\(kind) should stack on a data row" - ) - } - } - - /// A schema label comes from a closed authored vocabulary whose widest member renders at 66.4pt, - /// so the lane holds it and the row stays one line high. - @Test("A schema scalar takes one line") - func schemaScalarsAreInline() { - #expect(InspectorFieldLayout.resolve(for: .singleLine, isSchemaField: true) == .inline) - #expect(InspectorFieldLayout.resolve(for: .boolean, isSchemaField: true) == .inline) - #expect(InspectorFieldLayout.resolve(for: .schemaText, isSchemaField: true) == .inline) - #expect(InspectorFieldLayout.resolve(for: .typePicker, isSchemaField: true) == .inline) - #expect(InspectorFieldLayout.resolve(for: .enumPicker(values: ["a"]), isSchemaField: true) == .inline) - #expect(InspectorFieldLayout.resolve(for: .setPicker(values: ["a"]), isSchemaField: true) == .inline) - #expect( - InspectorFieldLayout.resolve(for: .arrayElements(element: .json, values: []), isSchemaField: true) - == .inline - ) - } - - /// The editors that need the pane's width still take it, on a schema row as on a data row. - @Test("A schema editor that needs room spans the width") - func schemaLargeEditorsAreStacked() { - #expect(InspectorFieldLayout.resolve(for: .multiLine, isSchemaField: true) == .stacked) - #expect(InspectorFieldLayout.resolve(for: .json, isSchemaField: true) == .stacked) - #expect(InspectorFieldLayout.resolve(for: .phpSerialized, isSchemaField: true) == .stacked) - #expect(InspectorFieldLayout.resolve(for: .blobHex, isSchemaField: true) == .stacked) - #expect(InspectorFieldLayout.resolve(for: .image(.raster("public.png")), isSchemaField: true) == .stacked) - } - - /// A schema row never resolves to a geometry field, and the switch still has to place it: a map - /// needs the pane's width wherever it is shown. - @Test("A geometry field spans the width on either kind of row") - func geometryFieldsAreStacked() { - let kinds: [FieldEditorKind] = [ - .geometry(GeometryFieldDescriptor(textEditor: .multiLine, source: .spatialColumn)), - .geometry(GeometryFieldDescriptor(textEditor: .json, source: .jsonColumn)), - .geometry(GeometryFieldDescriptor(textEditor: .hex, source: .binary)) - ] - for kind in kinds { - #expect(InspectorFieldLayout.resolve(for: kind, isSchemaField: false) == .stacked) - #expect(InspectorFieldLayout.resolve(for: kind, isSchemaField: true) == .stacked) - } - } - - @Test("A geometry value reaches the stacked shape through its editor") - func aGeometryValueStacksThroughItsEditor() { - let kind = FieldEditorResolver.resolve( - for: .spatial(rawType: "POINT"), - isLongText: false, - originalValue: "POINT(1 2)" - ) - #expect(kind == .geometry(GeometryFieldDescriptor(textEditor: .multiLine, source: .spatialColumn))) - #expect(InspectorFieldLayout.resolve(for: kind, isSchemaField: false) == .stacked) - } - - /// `.enumPicker` is the kind for a data row's ENUM column *and* for a structure row's dropdown - /// (On Delete, Unique, Nullable), so the editor kind alone cannot say which shape a row takes. - /// Resolving on the kind by itself is what put a structure dropdown and an ENUM value in the - /// same geometry. - @Test("One editor kind resolves differently by provenance") - func provenanceDecidesWhereTheKindIsShared() { - let dropdown = FieldEditorKind.enumPicker(values: ["CASCADE", "RESTRICT"]) - #expect(InspectorFieldLayout.resolve(for: dropdown, isSchemaField: true) == .inline) - #expect(InspectorFieldLayout.resolve(for: dropdown, isSchemaField: false) == .stacked) - } - - /// A long value still reaches its own editor through `FieldEditorResolver`; the layout no longer - /// asks the value a second time, so the two can never drift apart. - @Test("A long value resolves to the multi-line editor") - func aLongValueBecomesMultiLineThroughItsEditor() { - let long = String(repeating: "x", count: FieldEditorResolver.multiLineValueThreshold + 1) - let kind = FieldEditorResolver.resolve(for: .text(rawType: "TEXT"), isLongText: false, originalValue: long) - #expect(kind == .multiLine) - #expect(InspectorFieldLayout.resolve(for: kind, isSchemaField: false) == .stacked) - } - - /// A short data value gets the same shape as a long one. Choosing per row from the value's own - /// length would reflow a field from one shape to the other while the user typed into it. - @Test("A short data value keeps the same shape as a long one") - func aShortValueKeepsTheStackedShape() { - let kind = FieldEditorResolver.resolve(for: .text(rawType: "TEXT"), isLongText: false, originalValue: "ok") - #expect(kind == .singleLine) - #expect(InspectorFieldLayout.resolve(for: kind, isSchemaField: false) == .stacked) - } -} diff --git a/apple/TableProTests/Models/InspectorSubjectTests.swift b/apple/TableProTests/Models/InspectorSubjectTests.swift index 252a8ed4b3..e922c364a1 100644 --- a/apple/TableProTests/Models/InspectorSubjectTests.swift +++ b/apple/TableProTests/Models/InspectorSubjectTests.swift @@ -19,7 +19,7 @@ struct InspectorSubjectTests { #expect(subject.subtitle?.contains("500") == true) } - /// A result of several thousand rows reads as a quantity, the way the grid's own counter does. + // A result of several thousand rows reads as a quantity, the way the grid's own counter does. @Test("A large row count is grouped") func largeCountsAreGrouped() { let subject = InspectorSubject.tableRow( @@ -44,20 +44,39 @@ struct InspectorSubjectTests { #expect(subject.subtitle?.contains("3") == true) } - /// A schema grid's selection is a column definition, not a row of a result: it has no position - /// and no identity, so a subject that assumed one would render "Row 0 of 0" over a good column. - @Test("A column definition names the column, not a row") + @Test("A column definition names the column and its table") func columnDefinitionNamesTheColumn() { - let subject = InspectorSubject.columnDefinition(column: "user_id", table: "public.users") + let subject = InspectorSubject.definition(name: "user_id", section: .columns, table: "users") #expect(subject.title == "user_id") - #expect(subject.subtitle?.contains("public.users") == true) + #expect(subject.subtitle == String(format: String(localized: "Column of %@"), "users")) } - @Test("A column with no known table still names itself") - func columnWithoutTable() { - let subject = InspectorSubject.columnDefinition(column: "user_id", table: nil) + @Test("An index names itself as an index, not a column") + func indexIsNotAColumn() { + let subject = InspectorSubject.definition(name: "idx_email", section: .indexes, table: "users") + #expect(subject.title == "idx_email") + #expect(subject.subtitle == String(format: String(localized: "Index of %@"), "users")) + } + + @Test("Foreign keys and check constraints say what they are") + func otherDefinitionKinds() { + let foreignKey = InspectorSubject.definition(name: "fk_user", section: .foreignKeys, table: "orders") + #expect(foreignKey.subtitle == String(format: String(localized: "Foreign Key of %@"), "orders")) + let check = InspectorSubject.definition(name: "positive_total", section: .checkConstraints, table: "orders") + #expect(check.subtitle == String(format: String(localized: "Check Constraint of %@"), "orders")) + } + + @Test("A definition with no name yet reads as new") + func unnamedDefinitionIsNew() { + #expect(InspectorSubject.definition(name: "", section: .columns, table: "t").title == String(localized: "New Column")) + #expect(InspectorSubject.definition(name: "", section: .indexes, table: "t").title == String(localized: "New Index")) + } + + @Test("A definition with no known table still names its kind") + func definitionWithoutTable() { + let subject = InspectorSubject.definition(name: "user_id", section: .foreignKeys, table: nil) #expect(subject.title == "user_id") - #expect(subject.subtitle != nil) + #expect(subject.subtitle == String(localized: "Foreign Key")) } @Test("A table with no row selected names the table") diff --git a/apple/TableProTests/Models/MultiRowEditStateTests.swift b/apple/TableProTests/Models/MultiRowEditStateTests.swift index 311c93b45d..ce162e198a 100644 --- a/apple/TableProTests/Models/MultiRowEditStateTests.swift +++ b/apple/TableProTests/Models/MultiRowEditStateTests.swift @@ -1029,104 +1029,6 @@ struct MultiRowEditStateTests { } } - @MainActor @Suite("Schema fields") - struct SchemaFields { - private func schemaFields() -> [InspectorRowField] { - [ - InspectorRowField(name: "Name", value: "email", editor: .schemaText), - InspectorRowField(name: "Type", value: "VARCHAR(255)", editor: .typePicker), - InspectorRowField(name: "Nullable", value: "YES", editor: .enumPicker(values: ["YES", "NO"]), - isModified: true) - ] - } - - @Test("Schema fields keep their editor, values, and committed-edit marker") - func configureCarriesFieldDescription() { - let sut = MultiRowEditState() - sut.configure(schemaFields: schemaFields(), displayRow: 2) - - #expect(sut.fields.map(\.columnName) == ["Name", "Type", "Nullable"]) - #expect(sut.fields.map(\.originalValue) == ["email", "VARCHAR(255)", "YES"]) - #expect(sut.fields[1].editor == .typePicker) - #expect(sut.fields.allSatisfy { $0.isSchemaField }) - #expect(sut.fields[2].hasCommittedEdit) - #expect(sut.fields[0].hasCommittedEdit == false) - #expect(sut.rowIDs.isEmpty) - } - - @Test("A field the owning grid locks is shown without an editor") - func lockedSchemaFieldIsReadOnly() { - let sut = MultiRowEditState() - sut.configure( - schemaFields: [ - InspectorRowField(name: "Name", value: "email", editor: .schemaText), - InspectorRowField(name: "Type", value: "text", editor: .typePicker, isEditable: false) - ], - displayRow: 0 - ) - - #expect(sut.fields[0].isServerOwned == false) - #expect(sut.fields[1].isServerOwned) - #expect(InspectorFieldListView.isFieldEditable(sut.fields[0], kind: .schemaText, rowIsEditable: true)) - #expect(!InspectorFieldListView.isFieldEditable(sut.fields[1], kind: .typePicker, rowIsEditable: true)) - } - - @Test("A committed schema edit is not a pending sidebar edit") - func schemaFieldsAreNotPendingEdits() { - let sut = MultiRowEditState() - sut.configure(schemaFields: schemaFields(), displayRow: 0) - - #expect(sut.hasEdits == false) - #expect(sut.getEditedFields().isEmpty) - } - - @Test("Reconfiguring the same row keeps field identity so editors keep focus") - func sameRowKeepsFieldIdentity() { - let sut = MultiRowEditState() - sut.configure(schemaFields: schemaFields(), displayRow: 1) - let ids = sut.fields.map(\.id) - - sut.configure(schemaFields: schemaFields(), displayRow: 1) - #expect(sut.fields.map(\.id) == ids) - } - - @Test("Selecting another row gives fresh editors") - func differentRowResetsFieldIdentity() { - let sut = MultiRowEditState() - sut.configure(schemaFields: schemaFields(), displayRow: 1) - let ids = sut.fields.map(\.id) - - sut.configure(schemaFields: schemaFields(), displayRow: 2) - #expect(sut.fields.map(\.id) != ids) - } - - @Test("Editing a schema field reports the new value to the owning grid") - func editReportsNewValue() { - let sut = MultiRowEditState() - sut.configure(schemaFields: schemaFields(), displayRow: 0) - - var committed: [(Int, String?)] = [] - sut.onFieldChanged = { index, value, _ in committed.append((index, value.asText)) } - sut.updateField(at: 0, value: "user_email") - - #expect(committed.count == 1) - #expect(committed.first?.0 == 0) - #expect(committed.first?.1 == "user_email") - } - - @Test("Retyping the existing value reports nothing") - func unchangedValueReportsNothing() { - let sut = MultiRowEditState() - sut.configure(schemaFields: schemaFields(), displayRow: 0) - - var committed = 0 - sut.onFieldChanged = { _, _, _ in committed += 1 } - sut.updateField(at: 0, value: "email") - - #expect(committed == 0) - } - } - // MARK: - Binary columns @MainActor @Suite("Binary columns") diff --git a/apple/TableProTests/Models/PendingChangeKindTests.swift b/apple/TableProTests/Models/PendingChangeKindTests.swift index a3efc3eeb9..f1fad5ca9b 100644 --- a/apple/TableProTests/Models/PendingChangeKindTests.swift +++ b/apple/TableProTests/Models/PendingChangeKindTests.swift @@ -166,4 +166,17 @@ struct PendingChangeKindTests { ) == nil ) } + + // A session previews only from its Structure view; lit over ALTERs staged behind Data, Preview showed nothing. + @Test("Preview SQL needs statements it can show") + func previewNeedsStatementsItCanShow() { + #expect(!PendingChangeKind.offersPreview(of: .structure, hasDataChanges: false, previewsSchemaChanges: false)) + #expect(PendingChangeKind.offersPreview(of: .structure, hasDataChanges: false, previewsSchemaChanges: true)) + #expect(PendingChangeKind.offersPreview(of: .structure, hasDataChanges: true, previewsSchemaChanges: false)) + #expect(PendingChangeKind.offersPreview(of: .data, hasDataChanges: true, previewsSchemaChanges: false)) + #expect(PendingChangeKind.offersPreview(of: .createTable, hasDataChanges: false, previewsSchemaChanges: false)) + #expect(!PendingChangeKind.offersPreview(of: .file, hasDataChanges: false, previewsSchemaChanges: false)) + #expect(!PendingChangeKind.offersPreview(of: .principals, hasDataChanges: false, previewsSchemaChanges: false)) + #expect(!PendingChangeKind.offersPreview(of: nil, hasDataChanges: false, previewsSchemaChanges: false)) + } } diff --git a/apple/TableProTests/Models/ResultStatusModelTests.swift b/apple/TableProTests/Models/ResultStatusModelTests.swift index a4f5471df5..f5a1cc3f23 100644 --- a/apple/TableProTests/Models/ResultStatusModelTests.swift +++ b/apple/TableProTests/Models/ResultStatusModelTests.swift @@ -17,7 +17,6 @@ struct ResultStatusModelTests { isValueFiltered: Bool = false, hasColumns: Bool? = nil, hasTableName: Bool = true, - hasStructureActions: Bool = false, pagination: PaginationState = PaginationState(), statusMessage: String? = nil ) -> StatusBarSnapshot { @@ -35,7 +34,6 @@ struct ResultStatusModelTests { hasTableName: hasTableName, hasColumns: hasColumns ?? (rowCount > 0) ), - hasStructureActions: hasStructureActions, pagination: pagination, statusMessage: statusMessage ) @@ -396,18 +394,6 @@ struct ResultStatusModelTests { #expect(result.controls.showsColumns) } - /// The pair belongs to the structure list, so it appears only while that list is the content and - /// only once the structure editor has said what it can do. - @Test("The structure pair appears only in Structure mode, and only when published") - func structureActionsFollowTheMode() { - let published = makeSnapshot(rowCount: 9, hasStructureActions: true) - #expect(model(published, viewMode: .structure).controls.showsStructureActions) - #expect(!model(published, viewMode: .data).controls.showsStructureActions) - - let unpublished = makeSnapshot(rowCount: 9, hasStructureActions: false) - #expect(!model(unpublished, viewMode: .structure).controls.showsStructureActions) - } - @Test("A status message rides with the readout") func statusMessageFollowsReadout() { let withReadout = model(makeSnapshot(rowCount: 5, statusMessage: "OK")) @@ -470,7 +456,6 @@ struct ResultStatusModelTests { controls.showsFilters, controls.showsPagination, controls.showsFetchAll, - controls.showsStructureActions, controls.showsSelectionSummary, ] } diff --git a/apple/TableProTests/Models/Schema/CreateTableDraftBuilderTests.swift b/apple/TableProTests/Models/Schema/CreateTableDraftBuilderTests.swift index dbabf4b9b5..77ec1727e8 100644 --- a/apple/TableProTests/Models/Schema/CreateTableDraftBuilderTests.swift +++ b/apple/TableProTests/Models/Schema/CreateTableDraftBuilderTests.swift @@ -47,10 +47,15 @@ struct CreateTableDraftBuilderTests { ) } + private func check(_ name: String, _ expression: String) -> EditableCheckConstraintDefinition { + EditableCheckConstraintDefinition(id: UUID(), name: name, expression: expression, columns: [], isValidated: true) + } + private func plan( columns: [EditableColumnDefinition] = [], indexes: [EditableIndexDefinition] = [], foreignKeys: [EditableForeignKeyDefinition] = [], + checkConstraints: [EditableCheckConstraintDefinition] = [], databaseType: DatabaseType = .sqlite, tableName: String = "child" ) -> CreateTablePlan { @@ -60,6 +65,7 @@ struct CreateTableDraftBuilderTests { columns: columns.isEmpty ? [column("id"), column("parent_id")] : columns, indexes: indexes, foreignKeys: foreignKeys, + checkConstraints: checkConstraints, dialect: ForeignKeyDialect.forType(databaseType), includesEngineOptions: false ) @@ -118,7 +124,7 @@ struct CreateTableDraftBuilderTests { func unnamedIndexIsReported() { let result = plan(indexes: [index(name: "")]) #expect(result.indexes.isEmpty) - #expect(result.issues.contains { $0.tab == .indexes }) + #expect(result.issues.contains { $0.section == .indexes }) #expect(!result.isReadyToCreate) } @@ -158,14 +164,14 @@ struct CreateTableDraftBuilderTests { @Test("a foreign key with no referenced table is reported") func missingReferencedTableIsReported() { let result = plan(foreignKeys: [foreignKey(referencedTable: "")]) - #expect(result.issues.contains { $0.tab == .foreignKeys }) + #expect(result.issues.contains { $0.section == .foreignKeys }) #expect(result.definition?.foreignKeys.isEmpty == true) } @Test("a foreign key naming a column the table lacks is reported") func unknownColumnIsReported() { let result = plan(foreignKeys: [foreignKey(columns: ["nope"])]) - #expect(result.issues.contains { $0.tab == .foreignKeys }) + #expect(result.issues.contains { $0.section == .foreignKeys }) } @Test("mismatched composite column counts are reported") @@ -174,13 +180,13 @@ struct CreateTableDraftBuilderTests { columns: [column("id"), column("a"), column("b")], foreignKeys: [foreignKey(columns: ["a", "b"], referencedColumns: ["id"])] ) - #expect(result.issues.contains { $0.tab == .foreignKeys }) + #expect(result.issues.contains { $0.section == .foreignKeys }) } @Test("a duplicate column name is reported") func duplicateColumnIsReported() { let result = plan(columns: [column("id"), column("id")]) - #expect(result.issues.contains { $0.tab == .columns }) + #expect(result.issues.contains { $0.section == .columns }) } @Test("a table with no name is reported") @@ -203,13 +209,13 @@ struct CreateTableDraftBuilderTests { @Test("MySQL reports a foreign key with no referenced columns") func mysqlRequiresReferencedColumns() { let result = plan(foreignKeys: [foreignKey(referencedColumns: [])], databaseType: .mysql) - #expect(result.issues.contains { $0.tab == .foreignKeys }) + #expect(result.issues.contains { $0.section == .foreignKeys }) } @Test("DuckDB reports ON DELETE CASCADE") func duckdbRejectsCascade() { let result = plan(foreignKeys: [foreignKey(onDelete: .cascade)], databaseType: .duckdb) - #expect(result.issues.contains { $0.tab == .foreignKeys }) + #expect(result.issues.contains { $0.section == .foreignKeys }) } @Test("SQLite accepts ON DELETE CASCADE") @@ -221,7 +227,7 @@ struct CreateTableDraftBuilderTests { @Test("SQLite reports a referenced table in another schema") func sqliteRejectsQualifiedReference() { let result = plan(foreignKeys: [foreignKey(referencedSchema: "other")], databaseType: .sqlite) - #expect(result.issues.contains { $0.tab == .foreignKeys }) + #expect(result.issues.contains { $0.section == .foreignKeys }) } /// Every foreign key starts at `.noAction`, and Oracle, Dameng and Teradata declare an empty @@ -430,4 +436,50 @@ struct CreateTableDraftBuilderTests { #expect(seeded.collation == "latin1_bin") #expect(seeded.engine == "MyISAM") } + + // MARK: - Check constraints + + @Test("A named check with an expression is planned, trimmed, after the CREATE") + func completeCheckIsPlanned() { + let result = plan(checkConstraints: [check(" positive_id ", " id > 0\n")]) + #expect(result.issues.isEmpty) + #expect(result.checkConstraints.map { $0.name } == ["positive_id"]) + #expect(result.checkConstraints.map { $0.expression } == ["id > 0"]) + } + + @Test("A blank check row is ignored") + func blankCheckIsIgnored() { + let result = plan(checkConstraints: [check("", " ")]) + #expect(result.issues.isEmpty) + #expect(result.checkConstraints.isEmpty) + } + + @Test("A begun check names what it lacks on its own row", arguments: [("", "id > 0"), ("positive_id", "")]) + func incompleteCheckIsReported(name: String, expression: String) { + let result = plan(checkConstraints: [check("", ""), check(name, expression)]) + #expect(result.checkConstraints.isEmpty) + #expect(result.issues.map { $0.section } == [.checkConstraints]) + #expect(result.issues.first?.row == 1) + #expect(!result.isReadyToCreate) + } + + @Test("A second check with the same name is flagged on its own row") + func duplicateCheckNameIsReported() { + let result = plan(checkConstraints: [check("positive", "id > 0"), check("", ""), check("positive", "id < 9")]) + #expect(result.issues.count == 1) + #expect(result.issues.first?.section == .checkConstraints) + #expect(result.issues.first?.row == 2) + } + + @Test("Planned indexes and checks keep the working rows they came from") + func plannedRowsMapToWorkingRows() { + let result = plan( + indexes: [index(name: "", columns: []), index(name: "idx_parent")], + checkConstraints: [check("", ""), check("", ""), check("positive", "id > 0")] + ) + #expect(result.indexRows == [1]) + #expect(result.indexRow(at: 0) == 1) + #expect(result.checkConstraintRows == [2]) + #expect(result.checkConstraintRow(at: 0) == 2) + } } diff --git a/apple/TableProTests/Models/Schema/CreateTableFormStateTests.swift b/apple/TableProTests/Models/Schema/CreateTableFormStateTests.swift index dc691bfe1d..f5ee9187f0 100644 --- a/apple/TableProTests/Models/Schema/CreateTableFormStateTests.swift +++ b/apple/TableProTests/Models/Schema/CreateTableFormStateTests.swift @@ -409,9 +409,17 @@ struct CreateTableFormStateTests { @MainActor struct CreateTableDraftFormTests { + private func makeDraft() -> CreateTableDraft { + let connection = TestFixtures.makeConnection(type: .postgresql) + return CreateTableDraft( + connection: connection, + scope: DatabaseScope(connectionId: connection.id, database: "shop", schema: nil) + ) + } + @Test("A draft resolves its form once and keeps it") func resolvesFormOnce() { - let draft = CreateTableDraft() + let draft = makeDraft() #expect(!draft.hasResolvedForm) draft.resolveForm(from: FormFixture.spec) @@ -425,7 +433,7 @@ struct CreateTableDraftFormTests { @Test("Renaming the table clears what the driver said about the last attempt") func renameClearsSubmissionError() { - let draft = CreateTableDraft() + let draft = makeDraft() draft.resolveForm(from: FormFixture.spec) draft.tableName = "a" draft.form?.recordSubmissionError(PluginCreateTableFormError(message: "A table name is too short")) @@ -437,19 +445,35 @@ struct CreateTableDraftFormTests { #expect(draft.form?.submissionError == nil) } - @Test("A driver without a form leaves the draft on the column grid") + @Test("A driver without a form leaves the draft on the definition, seeded with one blank column") func noSpecMeansGrid() { - let draft = CreateTableDraft() + let draft = makeDraft() draft.resolveForm(from: nil) #expect(draft.hasResolvedForm) #expect(draft.form == nil) + #expect(draft.changeManager.workingColumns.count == 1) + #expect(draft.editor.adapter(for: .columns).visibleRowCount == 1) + #expect(!draft.holdsWork) + } + + @Test("A driver's form takes no definition commands and seeds no column") + func formTakesNoDefinitionCommands() { + let draft = makeDraft() + draft.resolveForm(from: FormFixture.spec) + + draft.addRow() + + #expect(!draft.canAddRow) + #expect(!draft.canUndo) + #expect(draft.changeManager.workingColumns.isEmpty) + #expect(draft.activeGridModel == nil) } @Test("An edited form counts as work worth protecting") func editedFormHoldsWork() { - let draft = CreateTableDraft() + let draft = makeDraft() draft.resolveForm(from: FormFixture.spec) #expect(!draft.holdsWork) diff --git a/apple/TableProTests/Views/DataGrid/DataGridFittingHeightTests.swift b/apple/TableProTests/Views/DataGrid/DataGridFittingHeightTests.swift new file mode 100644 index 0000000000..6793212aac --- /dev/null +++ b/apple/TableProTests/Views/DataGrid/DataGridFittingHeightTests.swift @@ -0,0 +1,336 @@ +// +// DataGridFittingHeightTests.swift +// TableProTests +// + +import AppKit +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +private final class RecordingPageScrollView: NSScrollView { + private(set) var wheelEvents: [NSEvent] = [] + + override func scrollWheel(with event: NSEvent) { + wheelEvents.append(event) + } +} + +@MainActor +private final class FlippedPageDocument: NSView { + override var isFlipped: Bool { true } +} + +// A fitsRows grid laid on a page that scrolls vertically, sized the way DataGridView sizes it. +@MainActor +private final class PagedGrid { + let grid: HostedDataGrid + let page = RecordingPageScrollView(frame: .zero) + private let document = FlippedPageDocument(frame: .zero) + private let gridTop: CGFloat + + init(rows: TableRows, gridTop: CGFloat = 0) { + var configuration = DataGridConfiguration() + configuration.fitsRows = true + grid = HostedDataGrid(rows: rows, configuration: configuration) + self.gridTop = gridTop + page.hasVerticalScroller = true + page.documentView = document + let gridView = grid.controller.view + gridView.removeFromSuperview() + gridView.autoresizingMask = [] + document.addSubview(gridView) + grid.window.contentView = page + fit() + } + + // What SwiftUI does with the published height, applied now. + func fit() { + for _ in 0..<2 { + let height = grid.controller.measuredFittingHeight + grid.controller.view.frame = NSRect(x: 0, y: gridTop, width: page.bounds.width, height: height) + document.frame = NSRect(x: 0, y: 0, width: page.bounds.width, height: gridTop + height + page.bounds.height) + grid.layout() + } + } + + func rowRectOnPage(_ row: Int) -> NSRect { + document.convert(grid.main.rect(ofRow: row), from: grid.main) + } + + func showsOnPage(row: Int) -> Bool { + let rect = rowRectOnPage(row) + let visible = page.documentVisibleRect + return rect.minY >= visible.minY - 0.5 && rect.maxY <= visible.maxY + 0.5 + } + + var gridOffset: CGFloat { + grid.mainScrollView.contentView.bounds.origin.y + grid.controller.headerInset + } + + func close() { + grid.close() + } +} + +@MainActor +struct DataGridFittingHeightTests { + private static var fitsRows: DataGridConfiguration { + var configuration = DataGridConfiguration() + configuration.fitsRows = true + return configuration + } + + private func nextMainTurn() async { + await withCheckedContinuation { continuation in + DispatchQueue.main.async { continuation.resume() } + } + } + + private struct WheelStep { + var phase: NSEvent.Phase = [] + var momentum: NSEvent.Phase = [] + var dx: CGFloat = 0 + var dy: CGFloat = 0 + } + + private static func routedAxes(_ steps: [WheelStep]) -> [DataGridWheelRouting.Axis] { + var routing = DataGridWheelRouting() + return steps.map { routing.axis(phase: $0.phase, momentumPhase: $0.momentum, deltaX: $0.dx, deltaY: $0.dy) } + } + + private func wheel(dx: Int32 = 0, dy: Int32 = 0, phase: CGScrollPhase? = nil, momentum: CGMomentumScrollPhase? = nil) throws -> NSEvent { + let event = try #require(CGEvent(scrollWheelEvent2Source: nil, units: .pixel, wheelCount: 2, wheel1: dy, wheel2: dx, wheel3: 0)) + if let phase { + event.setIntegerValueField(.scrollWheelEventScrollPhase, value: Int64(phase.rawValue)) + } + if let momentum { + event.setIntegerValueField(.scrollWheelEventMomentumPhase, value: Int64(momentum.rawValue)) + } + return try #require(NSEvent(cgEvent: event)) + } + + // MARK: - Height + + @Test("A grid is as tall as its header and rows") + func heightIsHeaderPlusRows() { + let height = DataGridMetrics.fittingHeight(headerHeight: 28, rowCount: 5, rowPitch: 24, horizontalScrollerHeight: 0) + + #expect(height == 148) + } + + @Test("A legacy horizontal scroller adds its height below the rows") + func legacyScrollerAddsItsHeight() { + let height = DataGridMetrics.fittingHeight(headerHeight: 28, rowCount: 5, rowPitch: 24, horizontalScrollerHeight: 15) + + #expect(height == 163) + } + + @Test("A grid with no rows is as tall as its header") + func emptyGridIsItsHeader() { + #expect(DataGridMetrics.fittingHeight(headerHeight: 28, rowCount: 0, rowPitch: 24, horizontalScrollerHeight: 0) == 28) + #expect(DataGridMetrics.fittingHeight(headerHeight: 28, rowCount: -1, rowPitch: 24, horizontalScrollerHeight: 0) == 28) + } + + @Test("A grid scrolls vertically itself unless it is told to fit its rows") + func fitsRowsIsOffByDefault() { + #expect(!DataGridConfiguration().fitsRows) + } + + @Test("A grid that fits its rows publishes their height and follows inserts and removes") + func publishedHeightFollowsTheRows() async throws { + let grid = HostedDataGrid(rows: GridFixtureRows.people, configuration: Self.fitsRows) + defer { grid.close() } + let pitch = grid.main.rowHeight + grid.main.intercellSpacing.height + let header = try #require(grid.main.headerView).frame.height + + try await waitUntil { grid.model.fittingHeight == header + 5 * pitch + grid.mainScrollView.legacyHorizontalScrollerHeight } + let fiveRows = grid.model.fittingHeight + + grid.model.apply(grid.owner.tableRows.appendInsertedRow(values: [.text("5"), .text("fay"), .text("29")])) + try await waitUntil { grid.model.fittingHeight == fiveRows + pitch } + + grid.model.apply(grid.owner.tableRows.remove(rowIDs: [.existing(0), .existing(1)])) + try await waitUntil { grid.model.fittingHeight == fiveRows - pitch } + } + + @Test("A grid that fits its rows drops its vertical scroller and bounce and keeps its horizontal scroll") + func fitsRowsScrollsOnlySideways() { + let grid = HostedDataGrid(rows: GridFixtureRows.people, configuration: Self.fitsRows) + defer { grid.close() } + + #expect(!grid.mainScrollView.hasVerticalScroller) + #expect(grid.mainScrollView.verticalScrollElasticity == .none) + #expect(grid.mainScrollView.hasHorizontalScroller) + #expect(grid.mainScrollView.horizontalScrollElasticity != .none) + } + + @Test("A grid that scrolls itself keeps its scrollers and never publishes a height") + func defaultGridIsUnchanged() async { + let grid = HostedDataGrid(rows: GridFixtureRows.people) + defer { grid.close() } + + await nextMainTurn() + + #expect(grid.mainScrollView.hasVerticalScroller) + #expect(grid.mainScrollView.verticalScrollElasticity == .automatic) + #expect(grid.model.fittingHeight == 0) + } + + @Test("Turning fitsRows on for a mounted grid removes its vertical scroller and publishes its height") + func turningFitsRowsOnApplies() async throws { + let grid = HostedDataGrid(rows: GridFixtureRows.people) + defer { grid.close() } + + grid.model.configuration.fitsRows = true + + #expect(!grid.mainScrollView.hasVerticalScroller) + try await waitUntil { grid.model.fittingHeight == grid.controller.measuredFittingHeight } + #expect(grid.model.fittingHeight > 0) + } + + // MARK: - Wheel axis + + @Test("A mouse wheel decides its axis notch by notch") + func mouseWheelDecidesPerEvent() { + let axes = Self.routedAxes([ + WheelStep(dy: -10), + WheelStep(dx: 12, dy: 1), + WheelStep(dx: 1, dy: 6), + ]) + + #expect(axes == [.vertical, .horizontal, .vertical]) + } + + @Test("A sideways swipe stays sideways through its later moves and momentum") + func horizontalGestureKeepsItsAxis() { + let axes = Self.routedAxes([ + WheelStep(phase: .began, dx: 9, dy: 2), + WheelStep(phase: .changed, dx: 1, dy: 9), + WheelStep(phase: .ended), + WheelStep(momentum: .began, dx: 1, dy: 9), + WheelStep(momentum: .changed, dy: 9), + WheelStep(momentum: .ended), + ]) + + #expect(axes == Array(repeating: .horizontal, count: 6)) + } + + @Test("A vertical swipe stays vertical through its later moves") + func verticalGestureKeepsItsAxis() { + let axes = Self.routedAxes([ + WheelStep(phase: .began, dx: 1, dy: 8), + WheelStep(phase: .changed, dx: 9, dy: 1), + WheelStep(momentum: .began, dx: 9), + ]) + + #expect(axes == [.vertical, .vertical, .vertical]) + } + + @Test("Each swipe decides again, and a resting touch leaves the decision to the first move") + func eachGestureDecidesAgain() { + let axes = Self.routedAxes([ + WheelStep(phase: .began, dx: 1, dy: 8), + WheelStep(phase: .ended), + WheelStep(phase: .mayBegin), + WheelStep(phase: .began), + WheelStep(phase: .changed, dx: 7, dy: 1), + WheelStep(phase: .changed, dy: 7), + ]) + + #expect(axes == [.vertical, .vertical, .vertical, .vertical, .horizontal, .horizontal]) + } + + // MARK: - Page + + @Test("A vertical wheel over a grid that fits its rows scrolls the page, a sideways one the grid") + func verticalWheelGoesToThePage() throws { + let paged = PagedGrid(rows: GridFixtureRows.wide(rows: 30, columns: 3)) + defer { paged.close() } + + paged.grid.mainScrollView.scrollWheel(with: try wheel(dy: -10)) + #expect(paged.page.wheelEvents.count == 1) + + paged.grid.mainScrollView.scrollWheel(with: try wheel(dx: -12, dy: 1)) + #expect(paged.page.wheelEvents.count == 1) + + paged.grid.frozenScrollView.scrollWheel(with: try wheel(dy: 10)) + #expect(paged.page.wheelEvents.count == 2) + } + + @Test("A swipe goes to one owner for its whole length") + func gestureIsNotSplit() throws { + let paged = PagedGrid(rows: GridFixtureRows.wide(rows: 30, columns: 3)) + defer { paged.close() } + let scrollView = paged.grid.mainScrollView + + scrollView.scrollWheel(with: try wheel(dx: -9, dy: 2, phase: .began)) + scrollView.scrollWheel(with: try wheel(dx: -1, dy: 9, phase: .changed)) + scrollView.scrollWheel(with: try wheel(phase: .ended)) + scrollView.scrollWheel(with: try wheel(dx: -1, dy: 9, momentum: .begin)) + #expect(paged.page.wheelEvents.isEmpty) + + scrollView.scrollWheel(with: try wheel(dx: 1, dy: -8, phase: .began)) + scrollView.scrollWheel(with: try wheel(dx: -9, dy: 1, phase: .changed)) + scrollView.scrollWheel(with: try wheel(dx: -9, dy: 1, momentum: .continuous)) + #expect(paged.page.wheelEvents.count == 3) + } + + @Test("Revealing a row scrolls the page to it and leaves the grid at its first row") + func revealScrollsThePage() { + let paged = PagedGrid(rows: GridFixtureRows.wide(rows: 30, columns: 3)) + defer { paged.close() } + #expect(!paged.showsOnPage(row: 29)) + + paged.grid.model.reveal(rowID: .existing(29)) + + #expect(paged.showsOnPage(row: 29)) + #expect(paged.gridOffset == 0) + } + + // AppKit asks the table to keep a header-spanning rect in view while the pane resizes; that moved the page before. + @Test("A resize's own scrollToVisible on the table leaves the page where it is") + func appKitScrollToVisibleLeavesThePage() { + let paged = PagedGrid(rows: GridFixtureRows.wide(rows: 30, columns: 3), gridTop: 80) + defer { paged.close() } + let before = paged.page.documentVisibleRect.minY + + _ = paged.grid.main.scrollToVisible(NSRect(x: 0, y: -28, width: 600, height: 292)) + + #expect(paged.page.documentVisibleRect.minY == before) + } + + @Test("A row the arrow keys move to is brought onto the page") + func arrowKeyScrollsThePage() throws { + let paged = PagedGrid(rows: GridFixtureRows.wide(rows: 30, columns: 3)) + defer { paged.close() } + paged.grid.focus() + paged.grid.model.selectCell(GridCoord(row: 25, displayColumn: 0)) + #expect(!paged.showsOnPage(row: 26)) + + let downArrow = try #require(UnicodeScalar(NSDownArrowFunctionKey)) + try #require(paged.grid.press(String(Character(downArrow)), keyCode: KeyCode.downArrow.rawValue)) + + #expect(paged.grid.main.selectedRow == 26) + #expect(paged.showsOnPage(row: 26)) + #expect(paged.gridOffset == 0) + } + + @Test("A row the grid just took is revealed once the page has grown to it") + func revealWaitsForTheGridToGrow() async throws { + let paged = PagedGrid(rows: GridFixtureRows.wide(rows: 30, columns: 3)) + defer { paged.close() } + + paged.grid.model.apply(paged.grid.owner.tableRows.appendInsertedRow(values: Array(repeating: PluginCellValue.text("new"), count: 3))) + let inserted = try #require(paged.grid.model.rowID(at: 30)) + paged.grid.model.reveal(rowID: inserted) + #expect(paged.gridOffset == 0) + + paged.fit() + + try await waitUntil { paged.showsOnPage(row: 30) } + #expect(paged.gridOffset == 0) + } +} diff --git a/apple/TableProTests/Views/Main/CommandActionsDispatchTests.swift b/apple/TableProTests/Views/Main/CommandActionsDispatchTests.swift index ccc647cc31..0e44f56a86 100644 --- a/apple/TableProTests/Views/Main/CommandActionsDispatchTests.swift +++ b/apple/TableProTests/Views/Main/CommandActionsDispatchTests.swift @@ -116,111 +116,81 @@ extension GlobalStateSuites { #expect(coordinator.tabManager.selectedTab?.content.query == "SELECT 2") } - // MARK: - copySelectedRows (structure mode) + // MARK: - Structure mode - @Test("copySelectedRows in structure mode calls structureActions.copyRows") - func copySelectedRows_structureMode_callsStructureActions() { - let (actions, coordinator) = makeSUT() + private func showStructure( + of coordinator: MainContentCoordinator + ) throws -> StructureEditingSession { coordinator.tabManager.addTab(databaseName: "testdb") - - // Enable structure mode on the selected tab - if let idx = coordinator.tabManager.selectedTabIndex { - coordinator.tabManager.tabs[idx].display.resultsViewMode = .structure - } - - // Install a spy handler - let handler = StructureViewActionHandler() - var copyRowsCalled = false - handler.copyRows = { copyRowsCalled = true } - coordinator.structureActions = handler - - actions.copySelectedRows() - - #expect(copyRowsCalled) + let index = try #require(coordinator.tabManager.selectedTabIndex) + coordinator.tabManager.tabs[index].display.resultsViewMode = .structure + let session = TestFixtures.makeStructureSession() + session.changeManager.loadSchema( + tableName: "users", columns: [], indexes: [], foreignKeys: [], primaryKey: [] + ) + session.markBaselineAdopted() + coordinator.structureSessions[coordinator.tabManager.tabs[index].id] = session + return session } - // MARK: - pasteRows (structure mode) - - @Test("pasteRows in structure mode calls structureActions.pasteRows") - func pasteRows_structureMode_callsStructureActions() { + @Test("Add Row in Structure stages a column on the tab's session") + func addNewRow_structureMode_stagesOnTheSession() throws { let (actions, coordinator) = makeSUT() - coordinator.tabManager.addTab(databaseName: "testdb") - - // Enable structure mode on the selected tab - if let idx = coordinator.tabManager.selectedTabIndex { - coordinator.tabManager.tabs[idx].display.resultsViewMode = .structure - } - - // Install a spy handler - let handler = StructureViewActionHandler() - var pasteRowsCalled = false - handler.pasteRows = { pasteRowsCalled = true } - coordinator.structureActions = handler + let session = try showStructure(of: coordinator) + #expect(actions.selectionOwner == .schemaGrid) + #expect(actions.isCurrentTabEditable) + #expect(actions.isCurrentTabSchemaResolved) - actions.pasteRows() + actions.addNewRow() - #expect(pasteRowsCalled) + #expect(session.changeManager.workingColumns.count == 1) + #expect(session.hasChanges) } - // MARK: - addNewRow (structure mode) - - @Test("addNewRow in structure mode calls structureActions.addRow") - func addNewRow_structureMode_callsStructureActions() { + // Cmd+Shift+D reached only the data grid, so it did nothing in Structure. + @Test("Duplicate Row in Structure duplicates the selected column") + func duplicateRow_structureMode_duplicatesTheSelection() throws { let (actions, coordinator) = makeSUT() - coordinator.tabManager.addTab(databaseName: "testdb") + let session = try showStructure(of: coordinator) + actions.addNewRow() + try #require(session.editor.focusedSection == .columns) - if let idx = coordinator.tabManager.selectedTabIndex { - coordinator.tabManager.tabs[idx].display.resultsViewMode = .structure - } + actions.duplicateRow() - let handler = StructureViewActionHandler() - var addRowCalled = false - handler.addRow = { addRowCalled = true } - coordinator.structureActions = handler + #expect(session.changeManager.workingColumns.count == 2) + } + @Test("Delete in Structure removes the selected column") + func deleteSelectedRows_structureMode_removesTheSelection() throws { + let (actions, coordinator) = makeSUT() + let session = try showStructure(of: coordinator) actions.addNewRow() + try #require(actions.hasRowSelection) - #expect(addRowCalled) - } + actions.deleteSelectedRows() - // MARK: - deleteSelectedRows (structure mode) + #expect(session.changeManager.workingColumns.isEmpty) + } - @Test("deleteSelectedRows in structure mode calls structureActions.removeRow") - func deleteSelectedRows_structureMode_callsStructureActions() { + @Test("A Structure view with no session takes no row commands") + func structureModeWithoutASession_ownsNoSelection() throws { let (actions, coordinator) = makeSUT() coordinator.tabManager.addTab(databaseName: "testdb") + let index = try #require(coordinator.tabManager.selectedTabIndex) + coordinator.tabManager.tabs[index].display.resultsViewMode = .structure - if let idx = coordinator.tabManager.selectedTabIndex { - coordinator.tabManager.tabs[idx].display.resultsViewMode = .structure - } - - let handler = StructureViewActionHandler() - var removeRowCalled = false - handler.removeRow = { removeRowCalled = true } - coordinator.structureActions = handler - - actions.deleteSelectedRows() - - #expect(removeRowCalled) + #expect(actions.selectionOwner == .none) + #expect(!actions.isCurrentTabEditable) } // MARK: - saveChanges (createTable tab) - // The structure arm below would otherwise apply the staged edits of whatever session the tab id carries. - @Test("saveChanges dispatches createTableActions when the selected tab is createTable") - func saveChanges_createTableTab_callsCreateTableAction() { + // The structure arm would otherwise apply the staged edits of whatever session the tab id carries. + @Test("saveChanges on a createTable tab never applies a structure session's edits") + func saveChanges_createTableTab_leavesStructureSessionAlone() throws { let (actions, coordinator) = makeSUT() coordinator.tabManager.addCreateTableTab(databaseName: "testdb") - guard let id = coordinator.tabManager.selectedTabId else { - Issue.record("expected a selected tab") - return - } - - let createHandler = CreateTableActionHandler() - var createCalled = false - createHandler.createTable = { createCalled = true } - coordinator.createTableActions = createHandler - + let id = try #require(coordinator.tabManager.selectedTabId) let session = TestFixtures.makeStructureSession() coordinator.structureSessions[id] = session session.changeManager.loadSchema( @@ -230,25 +200,31 @@ extension GlobalStateSuites { actions.saveChanges() - #expect(createCalled) #expect(session.appliedVersion == 0) #expect(session.changeManager.hasChanges) } - @Test("saveChanges without createTableActions is a no-op for a createTable tab") - func saveChanges_createTableTab_withoutAction_doesNothing() { + // Leaving Structure for Data used to dim Save while the session still held its ALTERs. + @Test("From the Data view, Save reaches staged structure edits only when no grid edit is ahead of them") + func stagedStructureSession_followsTheDataView() throws { let (actions, coordinator) = makeSUT() - coordinator.tabManager.addCreateTableTab(databaseName: "testdb") - guard let id = coordinator.tabManager.selectedTabId else { - Issue.record("expected a selected tab") - return - } - let session = TestFixtures.makeStructureSession() - coordinator.structureSessions[id] = session + let session = try showStructure(of: coordinator) + session.changeManager.addNewColumn() + let index = try #require(coordinator.tabManager.selectedTabIndex) + coordinator.tabManager.tabs[index].display.resultsViewMode = .data - actions.saveChanges() + #expect(actions.stagedStructureSession === session) - #expect(session.appliedVersion == 0) + coordinator.changeManager.configureForTable( + tableName: "users", + columns: ["id"], + primaryKeyColumns: ["id"], + databaseType: .mysql, + generatedColumns: [] + ) + coordinator.changeManager.recordRowDeletion(rowID: .existing(0), originalRow: [.text("1")]) + + #expect(actions.stagedStructureSession == nil) } // MARK: - Row selection resolution diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift index 6c0c822cd1..b1056c17ac 100644 --- a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift @@ -4,6 +4,7 @@ import Combine import Foundation +import TableProTestSupport import Testing @testable import TablePro @@ -298,27 +299,21 @@ struct MainContentCoordinatorRefreshTests { } } - @Test("Refresh in structure view dispatches to the structure refresh handler") - func refreshInStructureViewDispatchesToHandler() { + @Test("Refresh in structure view reloads the tab's structure session, not its rows") + func refreshInStructureViewReloadsTheSession() async throws { let (coordinator, tabManager) = makeCoordinator() let tabId = addTableTab(to: tabManager) - guard let idx = tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { - Issue.record("expected tab to exist") - return - } + let idx = try #require(tabManager.tabs.firstIndex(where: { $0.id == tabId })) tabManager.tabs[idx].display.resultsViewMode = .structure - - let handler = StructureViewActionHandler() - var refreshCalled = false - handler.refresh = { refreshCalled = true } - coordinator.structureActions = handler + let session = TestFixtures.makeStructureSession() + coordinator.structureSessions[tabId] = session let initialEpoch = coordinator.tabExecution.contentEpoch(for: tabId) coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) - #expect(refreshCalled == true) #expect(coordinator.tabExecution.contentEpoch(for: tabId) == initialEpoch) #expect(coordinator.queryTasks.hasTask(for: tabId) == false) + try await waitUntil { session.partsReloadToken == 1 } } @Test("requestRefresh fires immediately and coalesces a rapid second call") diff --git a/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift b/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift index 589d44e259..e043f49c1f 100644 --- a/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift +++ b/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift @@ -28,7 +28,6 @@ struct ResultStatusBarLayoutTests { viewMode: ResultsViewMode, pagination: PaginationState = PaginationState(), statusMessage: String? = nil, - structureFooter: StructureFooterCapability = StructureFooterCapability(), selectionSummary: SelectionSummaryState? = nil, lastTiming: PluginQueryTiming? = nil ) -> ResultStatusBar { @@ -44,7 +43,6 @@ struct ResultStatusBarLayoutTests { hasTableName: tabType == .table, hasColumns: hasColumns ), - hasStructureActions: structureFooter.isActive, pagination: pagination, statusMessage: statusMessage ) @@ -82,7 +80,6 @@ struct ResultStatusBarLayoutTests { onGoToPage: { _ in }, onRequestExactCount: {} ), - structureFooter: structureFooter, execution: ExecutionReadout( tabId: UUID(), execution: TabExecutionRegistry(), @@ -99,9 +96,7 @@ struct ResultStatusBarLayoutTests { onCloseResultSet: { _ in }, onCloseOtherResultSets: { _ in }, onToggleFilters: {}, - onFetchAll: {}, - onStructureAdd: {}, - onStructureRemove: {} + onFetchAll: {} ) } @@ -290,13 +285,7 @@ struct ResultStatusBarLayoutTests { rowCount: 1_000, hasColumns: true, tabType: .table, - viewMode: .structure, - structureFooter: StructureFooterCapability( - canAdd: true, - canRemove: true, - addLabel: "Add Column", - removeLabel: "Remove Column" - ) + viewMode: .structure ) let host = NSHostingView(rootView: bar) host.sizingOptions = [] @@ -357,7 +346,6 @@ struct ResultStatusBarLayoutTests { onGoToPage: { _ in }, onRequestExactCount: {} ), - structureFooter: StructureFooterCapability(), execution: ExecutionReadout(tabId: tab.id, execution: registry, lastTiming: timing, onCancel: {}), selectionSummary: SelectionSummaryState(), isRefreshingSchema: false, @@ -369,9 +357,7 @@ struct ResultStatusBarLayoutTests { onCloseResultSet: { _ in }, onCloseOtherResultSets: { _ in }, onToggleFilters: {}, - onFetchAll: {}, - onStructureAdd: {}, - onStructureRemove: {} + onFetchAll: {} ) } @@ -438,13 +424,7 @@ struct ResultStatusBarLayoutTests { rowCount: 1_000, hasColumns: true, tabType: .table, - viewMode: .structure, - structureFooter: StructureFooterCapability( - canAdd: true, - canRemove: true, - addLabel: "Add Column", - removeLabel: "Remove Column" - ) + viewMode: .structure ) expectFills(bar, at: width) } diff --git a/apple/TableProTests/Views/Main/StructureActionHandlerTests.swift b/apple/TableProTests/Views/Main/StructureActionHandlerTests.swift deleted file mode 100644 index be1632f54d..0000000000 --- a/apple/TableProTests/Views/Main/StructureActionHandlerTests.swift +++ /dev/null @@ -1,220 +0,0 @@ -// -// StructureActionHandlerTests.swift -// TableProTests -// -// Tests for StructureViewActionHandler closure dispatch and coordinator integration. -// - -import Foundation -@testable import TablePro -import TableProPluginKit -import Testing - -@MainActor -struct StructureActionHandlerTests { - // MARK: - Helpers - - private func makeCoordinator() -> MainContentCoordinator { - let connection = TestFixtures.makeConnection() - let state = SessionStateFactory.create(connection: connection, payload: nil) - return state.coordinator - } - - // MARK: - Individual Closure Dispatch - - @Test("refresh closure fires when invoked") - func refresh_fires() { - let handler = StructureViewActionHandler() - var count = 0 - handler.refresh = { count += 1 } - - handler.refresh?() - - #expect(count == 1) - } - - @Test("previewSQL closure fires when invoked") - func previewSQL_fires() { - let handler = StructureViewActionHandler() - var count = 0 - handler.previewSQL = { count += 1 } - - handler.previewSQL?() - - #expect(count == 1) - } - - @Test("copyRows closure fires when invoked") - func copyRows_fires() { - let handler = StructureViewActionHandler() - var count = 0 - handler.copyRows = { count += 1 } - - handler.copyRows?() - - #expect(count == 1) - } - - @Test("pasteRows closure fires when invoked") - func pasteRows_fires() { - let handler = StructureViewActionHandler() - var count = 0 - handler.pasteRows = { count += 1 } - - handler.pasteRows?() - - #expect(count == 1) - } - - @Test("undo closure fires when invoked") - func undo_fires() { - let handler = StructureViewActionHandler() - var count = 0 - handler.undo = { count += 1 } - - handler.undo?() - - #expect(count == 1) - } - - @Test("redo closure fires when invoked") - func redo_fires() { - let handler = StructureViewActionHandler() - var count = 0 - handler.redo = { count += 1 } - - handler.redo?() - - #expect(count == 1) - } - - @Test("addRow closure fires when invoked") - func addRow_fires() { - let handler = StructureViewActionHandler() - var count = 0 - handler.addRow = { count += 1 } - - handler.addRow?() - - #expect(count == 1) - } - - @Test("removeRow closure fires when invoked") - func removeRow_fires() { - let handler = StructureViewActionHandler() - var count = 0 - handler.removeRow = { count += 1 } - - handler.removeRow?() - - #expect(count == 1) - } - - // MARK: - All Closures Fire Independently - - @Test("all closures fire independently without cross-talk") - func allClosures_fireIndependently() { - let handler = StructureViewActionHandler() - var counts = [String: Int]() - - handler.refresh = { counts["refresh", default: 0] += 1 } - handler.previewSQL = { counts["previewSQL", default: 0] += 1 } - handler.copyRows = { counts["copyRows", default: 0] += 1 } - handler.pasteRows = { counts["pasteRows", default: 0] += 1 } - handler.undo = { counts["undo", default: 0] += 1 } - handler.redo = { counts["redo", default: 0] += 1 } - handler.addRow = { counts["addRow", default: 0] += 1 } - handler.removeRow = { counts["removeRow", default: 0] += 1 } - - handler.refresh?() - handler.previewSQL?() - handler.copyRows?() - handler.pasteRows?() - handler.undo?() - handler.redo?() - handler.addRow?() - handler.removeRow?() - - #expect(counts["refresh"] == 1) - #expect(counts["previewSQL"] == 1) - #expect(counts["copyRows"] == 1) - #expect(counts["pasteRows"] == 1) - #expect(counts["undo"] == 1) - #expect(counts["redo"] == 1) - #expect(counts["addRow"] == 1) - #expect(counts["removeRow"] == 1) - } - - // MARK: - Coordinator Integration - - @Test("coordinator.structureActions dispatches refresh closure") - func coordinator_dispatchesSaveChanges() { - let coordinator = makeCoordinator() - defer { coordinator.teardown() } - - let handler = StructureViewActionHandler() - var count = 0 - handler.refresh = { count += 1 } - coordinator.structureActions = handler - - coordinator.structureActions?.refresh?() - - #expect(count == 1) - } - - @Test("coordinator.structureActions dispatches all closures") - func coordinator_dispatchesAllClosures() { - let coordinator = makeCoordinator() - defer { coordinator.teardown() } - - let handler = StructureViewActionHandler() - var counts = [String: Int]() - - handler.refresh = { counts["refresh", default: 0] += 1 } - handler.previewSQL = { counts["previewSQL", default: 0] += 1 } - handler.copyRows = { counts["copyRows", default: 0] += 1 } - handler.pasteRows = { counts["pasteRows", default: 0] += 1 } - handler.undo = { counts["undo", default: 0] += 1 } - handler.redo = { counts["redo", default: 0] += 1 } - - coordinator.structureActions = handler - - coordinator.structureActions?.refresh?() - coordinator.structureActions?.previewSQL?() - coordinator.structureActions?.copyRows?() - coordinator.structureActions?.pasteRows?() - coordinator.structureActions?.undo?() - coordinator.structureActions?.redo?() - - #expect(counts["refresh"] == 1) - #expect(counts["previewSQL"] == 1) - #expect(counts["copyRows"] == 1) - #expect(counts["pasteRows"] == 1) - #expect(counts["undo"] == 1) - #expect(counts["redo"] == 1) - } - - // MARK: - Weak Reference Nil-Out - - @Test("closures no longer fire after coordinator.structureActions is set to nil") - func coordinatorNilOut_closuresNoLongerFire() { - let coordinator = makeCoordinator() - defer { coordinator.teardown() } - - let handler = StructureViewActionHandler() - var count = 0 - handler.refresh = { count += 1 } - coordinator.structureActions = handler - - // Verify it works before nil-out - coordinator.structureActions?.refresh?() - #expect(count == 1) - - // Nil out the weak reference - coordinator.structureActions = nil - - // Calling through coordinator should be a no-op now - coordinator.structureActions?.refresh?() - #expect(count == 1) - } -} diff --git a/apple/TableProTests/Views/Main/StructureCommandRoutingTests.swift b/apple/TableProTests/Views/Main/StructureCommandRoutingTests.swift new file mode 100644 index 0000000000..f1298e52f6 --- /dev/null +++ b/apple/TableProTests/Views/Main/StructureCommandRoutingTests.swift @@ -0,0 +1,187 @@ +// +// StructureCommandRoutingTests.swift +// TableProTests +// + +import AppKit +import Foundation +import SwiftUI +@testable import TablePro +import TableProPluginKit +import Testing + +@MainActor +struct StructureCommandRoutingTests { + private func makeSUT() -> (MainContentCommandActions, MainContentCoordinator) { + let connection = TestFixtures.makeConnection() + let coordinator = SessionStateFactory.create(connection: connection, payload: nil).coordinator + var selectedTables: Set = [] + var pendingTruncates: Set = [] + var pendingDeletes: Set = [] + var tableOperationOptions: [DatabaseTreeTableRef: TableOperationOptions] = [:] + let actions = MainContentCommandActions( + coordinator: coordinator, + connection: 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() + ) + return (actions, coordinator) + } + + private func showStructure(of coordinator: MainContentCoordinator) throws -> StructureEditingSession { + coordinator.tabManager.addTab(databaseName: "testdb") + let index = try #require(coordinator.tabManager.selectedTabIndex) + coordinator.tabManager.tabs[index].display.resultsViewMode = .structure + let session = TestFixtures.makeStructureSession() + session.changeManager.loadSchema( + tableName: "users", columns: [], indexes: [], foreignKeys: [], primaryKey: [] + ) + coordinator.structureSessions[coordinator.tabManager.tabs[index].id] = session + return session + } + + private func showCreateTable(in coordinator: MainContentCoordinator) throws -> CreateTableDraft { + coordinator.tabManager.addCreateTableTab(databaseName: "testdb") + let id = try #require(coordinator.tabManager.selectedTabId) + let draft = CreateTableDraft(connection: TestFixtures.makeConnection(), scope: nil) + draft.resolveForm(from: nil) + coordinator.createTableDrafts[id] = draft + return draft + } + + private func stageCellEdit(on coordinator: MainContentCoordinator) { + coordinator.changeManager.configureForTable( + tableName: "users", + columns: ["id", "name"], + primaryKeyColumns: ["id"], + databaseType: .mysql, + generatedColumns: [] + ) + coordinator.changeManager.recordRowDeletion(rowID: .existing(0), originalRow: [.text("1"), .text("ada")]) + } + + // MARK: - Undo + + // The menu used to stay lit in Structure whenever the view was mounted, with nothing to undo. + @Test("Undo in Structure is dim and plainly titled with nothing staged") + func structureUndoIsDimWithNothingStaged() throws { + let (actions, coordinator) = makeSUT() + _ = try showStructure(of: coordinator) + + #expect(!actions.canUndo) + #expect(!actions.canRedo) + #expect(actions.resolvedUndoTitle == String(localized: "Undo")) + #expect(actions.resolvedRedoTitle == String(localized: "Redo")) + } + + @Test("Undo in Structure is named and run by the session's own history") + func structureUndoFollowsTheSession() throws { + let (actions, coordinator) = makeSUT() + let session = try showStructure(of: coordinator) + session.changeManager.addNewColumn() + + #expect(actions.canUndo) + #expect(actions.resolvedUndoTitle == session.undoMenuItemTitle) + #expect(actions.resolvedUndoTitle != String(localized: "Undo")) + + actions.undoChange() + + #expect(!session.hasChanges) + #expect(!actions.canUndo) + #expect(actions.canRedo) + #expect(actions.resolvedRedoTitle == session.redoMenuItemTitle) + + actions.redoChange() + + #expect(session.hasChanges) + } + + @Test("A session behind the Data view takes no Undo") + func sessionBehindTheDataViewKeepsItsHistory() throws { + let (actions, coordinator) = makeSUT() + let session = try showStructure(of: coordinator) + session.changeManager.addNewColumn() + let index = try #require(coordinator.tabManager.selectedTabIndex) + coordinator.tabManager.tabs[index].display.resultsViewMode = .data + + actions.undoChange() + + #expect(session.hasChanges) + } + + // A Create Table tab keeps its view mode at Data, so Undo read the window's history, which holds none of the draft. + @Test("Undo on a Create Table tab is enabled, named and run by the draft") + func createTableUndoFollowsTheDraft() throws { + let (actions, coordinator) = makeSUT() + let draft = try showCreateTable(in: coordinator) + draft.changeManager.addNewColumn() + + #expect(actions.canUndo == draft.canUndo) + #expect(actions.canUndo) + #expect(actions.resolvedUndoTitle == draft.undoMenuItemTitle) + + actions.undoChange() + + #expect(actions.canRedo) + #expect(actions.resolvedRedoTitle == draft.redoMenuItemTitle) + } + + // MARK: - Discard Changes + + @Test("Discard Changes follows the session's staged edits") + func discardFollowsStagedStructureEdits() throws { + let (actions, coordinator) = makeSUT() + let session = try showStructure(of: coordinator) + #expect(!actions.canDiscardChanges) + + session.changeManager.addNewColumn() + #expect(actions.canDiscardChanges) + + session.discardChanges() + #expect(!actions.canDiscardChanges) + } + + @Test("Discard Changes clears grid edits") + func discardClearsGridEdits() { + let (actions, coordinator) = makeSUT() + coordinator.tabManager.addTab(databaseName: "testdb") + #expect(!actions.canDiscardChanges) + + stageCellEdit(on: coordinator) + #expect(actions.canDiscardChanges) + + actions.discardDataWork() + + #expect(!coordinator.changeManager.hasChanges) + #expect(!actions.canDiscardChanges) + } + + @Test("Discard Changes is not offered on a Create Table tab") + func discardIsNotOfferedForCreateTable() throws { + let (actions, coordinator) = makeSUT() + let draft = try showCreateTable(in: coordinator) + draft.tableName = "invoices" + + #expect(draft.holdsWork) + #expect(!actions.canDiscardChanges) + } + + @Test("Discard Changes needs a connection and something to discard") + func discardValidation() { + let selector = #selector(MainSplitViewController.discardChanges(_:)) + var context = MenuValidationContext() + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == false) + + context.isConnected = true + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == false) + + context.canDiscardChanges = true + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == true) + + context.isAgentMode = true + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == false) + } +} diff --git a/apple/TableProTests/Views/Main/TabCloseProtectionTests.swift b/apple/TableProTests/Views/Main/TabCloseProtectionTests.swift index ae9ea440f1..5a6d165dc8 100644 --- a/apple/TableProTests/Views/Main/TabCloseProtectionTests.swift +++ b/apple/TableProTests/Views/Main/TabCloseProtectionTests.swift @@ -228,7 +228,7 @@ struct TabCloseProtectionTests { var tab = QueryTab(query: "") tab.tabType = .createTable - let draft = CreateTableDraft() + let draft = CreateTableDraft(connection: TestFixtures.makeConnection(), scope: nil) draft.changeManager.addNewColumn() coordinator.createTableDrafts[tab.id] = draft @@ -244,7 +244,7 @@ struct TabCloseProtectionTests { var tab = QueryTab(query: "") tab.tabType = .createTable - let draft = CreateTableDraft() + let draft = CreateTableDraft(connection: TestFixtures.makeConnection(), scope: nil) draft.tableName = "invoices" coordinator.createTableDrafts[tab.id] = draft @@ -260,7 +260,7 @@ struct TabCloseProtectionTests { var tab = QueryTab(query: "") tab.tabType = .createTable - let draft = CreateTableDraft() + let draft = CreateTableDraft(connection: TestFixtures.makeConnection(), scope: nil) draft.changeManager.addNewColumn() coordinator.createTableDrafts[tab.id] = draft @@ -382,7 +382,7 @@ struct TabCloseProtectionTests { return } coordinator.structureSessions[id] = TestFixtures.makeStructureSession() - coordinator.createTableDrafts[id] = CreateTableDraft() + coordinator.createTableDrafts[id] = CreateTableDraft(connection: TestFixtures.makeConnection(), scope: nil) coordinator.closeTabsByUser(ids: [id]) diff --git a/apple/TableProTests/Views/Main/TriggerStructTests.swift b/apple/TableProTests/Views/Main/TriggerStructTests.swift index 0609029a78..fc31f9b8fc 100644 --- a/apple/TableProTests/Views/Main/TriggerStructTests.swift +++ b/apple/TableProTests/Views/Main/TriggerStructTests.swift @@ -79,6 +79,15 @@ struct InspectorTriggerTests { func differentGridDisplayRevision() { #expect(trigger(gridDisplayRevision: 0) != trigger(gridDisplayRevision: 1)) } + + // A Structure view's session appears after its view first mounts, and the inspector has to pick it up. + @Test("A structure editor appearing produces unequal triggers") + func structureEditorAppearing() { + let editor = NSObject() + var withEditor = trigger(resultsViewMode: .structure) + withEditor.structureEditor = ObjectIdentifier(editor) + #expect(trigger(resultsViewMode: .structure) != withEditor) + } } // MARK: - PendingChangeTrigger Tests diff --git a/apple/TableProTests/Views/Main/UserTypeTabStateTests.swift b/apple/TableProTests/Views/Main/UserTypeTabStateTests.swift index c0cf1eb172..f78aa251c2 100644 --- a/apple/TableProTests/Views/Main/UserTypeTabStateTests.swift +++ b/apple/TableProTests/Views/Main/UserTypeTabStateTests.swift @@ -30,7 +30,6 @@ struct UserTypeTabStateTests { model.stage(.addEnumLabel("glad", placement: nil)) fixture.coordinator.publishUserTypeEditorState(tabId: tab.id, hasChanges: model.hasChanges) - fixture.coordinator.toolbarState.hasStructureChanges = false #expect(fixture.coordinator.toolbarState.hasTypeChanges) } diff --git a/apple/TableProTests/Views/RowInspector/InspectorPopOutWindowTests.swift b/apple/TableProTests/Views/RowInspector/InspectorPopOutWindowTests.swift index 1d60b3681c..14bbd4b405 100644 --- a/apple/TableProTests/Views/RowInspector/InspectorPopOutWindowTests.swift +++ b/apple/TableProTests/Views/RowInspector/InspectorPopOutWindowTests.swift @@ -33,6 +33,5 @@ struct InspectorPopOutWindowTests { #expect(InspectorPopOutWindow.resolve(for: .binaryValue(.protobuf)) == .binary(.protobuf)) #expect(InspectorPopOutWindow.resolve(for: .multiLine) == .text) #expect(InspectorPopOutWindow.resolve(for: .singleLine) == .text) - #expect(InspectorPopOutWindow.resolve(for: .schemaText) == .text) } } diff --git a/apple/TableProTests/Views/RowInspector/InspectorValueThemeTests.swift b/apple/TableProTests/Views/RowInspector/InspectorValueThemeTests.swift index b60f847094..dae89e5df5 100644 --- a/apple/TableProTests/Views/RowInspector/InspectorValueThemeTests.swift +++ b/apple/TableProTests/Views/RowInspector/InspectorValueThemeTests.swift @@ -33,7 +33,7 @@ struct InspectorValueThemeTests { } private func field(_ value: String, kind: FieldEditorKind, state: FieldValueState? = nil) -> FieldEditorContent { - FieldEditorContent(context: context(value, state: state), kind: kind, databaseType: .sqlite) + FieldEditorContent(context: context(value, state: state), kind: kind) } private func pixel(of view: some View, size: CGSize, x: Int, fromBottom: Int? = nil) throws -> NSColor { @@ -85,7 +85,7 @@ struct InspectorValueThemeTests { valueState: .value(value.wrappedValue), isReadOnly: false ) - return FieldEditorContent(context: context, kind: kind, databaseType: .sqlite) + return FieldEditorContent(context: context, kind: kind) } private func editableField(_ value: String, kind: FieldEditorKind = .singleLine) -> FieldEditorContent { diff --git a/apple/TableProTests/Views/RowInspector/SplitItemGlassBackgroundTests.swift b/apple/TableProTests/Views/RowInspector/SplitItemGlassBackgroundTests.swift index 807aa7c5af..be514ed669 100644 --- a/apple/TableProTests/Views/RowInspector/SplitItemGlassBackgroundTests.swift +++ b/apple/TableProTests/Views/RowInspector/SplitItemGlassBackgroundTests.swift @@ -31,7 +31,7 @@ struct SplitItemGlassBackgroundTests { columnTypes: [.integer(rawType: "INTEGER"), .text(rawType: "TEXT")], externallyModifiedColumns: [] ) - return InspectorFieldListView(editState: state, isEditable: true, databaseType: .sqlite, userDefinedTypeScope: nil) + return InspectorFieldListView(editState: state, isEditable: true, databaseType: .sqlite) } @Test("A plain list keeps its material where split items are not glass and drops it where they are") diff --git a/apple/TableProTests/Views/Structure/CreateTableDraftCommandTests.swift b/apple/TableProTests/Views/Structure/CreateTableDraftCommandTests.swift new file mode 100644 index 0000000000..3b8e0e5329 --- /dev/null +++ b/apple/TableProTests/Views/Structure/CreateTableDraftCommandTests.swift @@ -0,0 +1,145 @@ +// +// CreateTableDraftCommandTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +@MainActor +struct CreateTableDraftCommandTests { + private func makeDraft(database: String = "app", schema: String? = "sales") -> CreateTableDraft { + let connection = TestFixtures.makeConnection(database: "postgres", type: .postgresql) + let draft = CreateTableDraft( + connection: connection, + scope: DatabaseScope(connectionId: connection.id, database: database, schema: schema) + ) + draft.resolveForm(from: nil) + return draft + } + + @Test("Add Row adds a column while nothing is selected") + func addRowDefaultsToColumns() { + let draft = makeDraft() + + #expect(draft.canAddRow) + draft.addRow() + + #expect(draft.changeManager.workingColumns.count == 2) + #expect(draft.editor.inspectorTarget == .entity(.columns, draft.changeManager.workingColumns[1].id)) + } + + @Test("Add Row adds to the section holding the selection") + func addRowFollowsTheFocusedSection() { + let draft = makeDraft() + draft.editor.add(.indexes) + + draft.addRow() + + #expect(draft.changeManager.workingIndexes.count == 2) + #expect(draft.changeManager.workingColumns.count == 1) + #expect(draft.commandSection == .indexes) + } + + @Test("Add Row does nothing while the DDL is on screen") + func addRowNeedsTheDefinition() { + let draft = makeDraft() + draft.pane = .ddl + + draft.addRow() + + #expect(!draft.canAddRow) + #expect(draft.changeManager.workingColumns.count == 1) + } + + @Test("Remove deletes the selected definition, and Undo brings it back") + func removeThenUndo() { + let draft = makeDraft() + draft.addRow() + let added = draft.changeManager.workingColumns[1].id + + #expect(draft.canRemoveRows) + draft.removeSelectedRows() + #expect(draft.changeManager.workingColumns.map { $0.id }.contains(added) == false) + + #expect(draft.canUndo) + draft.undo() + #expect(draft.changeManager.workingColumns.map { $0.id }.contains(added)) + #expect(draft.editor.adapter(for: .columns).visibleRowCount == 2) + } + + @Test("Undo takes back an added row and Redo restores it") + func undoThenRedo() { + let draft = makeDraft() + draft.editor.add(.checkConstraints) + #expect(draft.changeManager.workingCheckConstraints.count == 1) + + draft.undo() + #expect(draft.changeManager.workingCheckConstraints.isEmpty) + #expect(draft.editor.adapter(for: .checkConstraints).visibleRowCount == 0) + + #expect(draft.canRedo) + draft.redo() + #expect(draft.changeManager.workingCheckConstraints.count == 1) + } + + @Test("A typed check counts as work worth protecting") + func checkConstraintHoldsWork() { + let draft = makeDraft() + draft.editor.add(.checkConstraints) + #expect(!draft.holdsWork) + + draft.changeManager.workingCheckConstraints[0].expression = "total > 0" + + #expect(draft.holdsWork) + #expect(draft.hasChanges) + } + + @Test("An issue on a row reveals that row in its section") + func revealingAnIssueSelectsItsRow() throws { + let draft = makeDraft() + draft.tableName = "orders" + draft.changeManager.workingColumns[0].name = "id" + draft.editor.refresh() + draft.pane = .ddl + + let issue = try #require(draft.issues.first { $0.row == 0 }) + draft.reveal(issue) + + #expect(issue.section == .columns) + #expect(draft.pane == .definition) + #expect(draft.editor.inspectorTarget == .entity(.columns, draft.changeManager.workingColumns[0].id)) + } + + @Test("A named table with a typed column is ready to create before the composition lands") + func readyFromThePlan() { + let draft = makeDraft() + #expect(!draft.isReadyToCreate) + + draft.tableName = "orders" + draft.changeManager.workingColumns[0].name = "id" + draft.changeManager.workingColumns[0].dataType = "INT" + + #expect(draft.issues.isEmpty) + #expect(draft.isReadyToCreate) + } + + // The sidebar can browse another database while the tab creates its table in its own (#10 in the verify lane). + @Test("The editor works on the tab's database and schema, never the sidebar's") + func editorUsesTheTabScope() { + let draft = makeDraft(database: "app", schema: "sales") + let scope = DatabaseScope(connectionId: draft.connection.id, database: "app", schema: "sales") + + #expect(draft.editor.context.isCreatingTable) + #expect(draft.editor.context.scope == scope) + #expect(draft.editor.referenceMenus.schemaName == "sales") + #expect(draft.editor.adapter(for: .columns).model.configuration.traits.connectionScope?.database == "app") + + draft.editor.add(.foreignKeys) + _ = draft.editor.adapter(for: .foreignKeys).menuOptions(sourceIndex: 0, field: 1) + + #expect(draft.editor.referenceMenus.origin == scope) + } +} diff --git a/apple/TableProTests/Views/Structure/CreateTableDraftCompositionTests.swift b/apple/TableProTests/Views/Structure/CreateTableDraftCompositionTests.swift index 42c28febea..1ee6ef2e42 100644 --- a/apple/TableProTests/Views/Structure/CreateTableDraftCompositionTests.swift +++ b/apple/TableProTests/Views/Structure/CreateTableDraftCompositionTests.swift @@ -8,14 +8,16 @@ import Testing private final class CompositionDriver: PluginDriverStub, @unchecked Sendable { private(set) var createTableRequests = 0 private var schema: String? + private let addsCheckConstraints: Bool var supportsSchemas: Bool { true } var supportsTransactions: Bool { false } var serverVersion: String? { nil } var currentSchema: String? { schema } - init(currentSchema: String?) { + init(currentSchema: String?, addsCheckConstraints: Bool = true) { self.schema = currentSchema + self.addsCheckConstraints = addsCheckConstraints } func execute(query: String) async throws -> PluginQueryResult { @@ -37,14 +39,23 @@ private final class CompositionDriver: PluginDriverStub, @unchecked Sendable { func objectCommentStatement(name: String, objectType: String, schema: String?, comment: String?) -> String? { "COMMENT ON \(objectType) \"\(schema ?? "")\".\"\(name)\" IS '\(comment ?? "")'" } + + func generateAddCheckConstraintSQL(table: String, constraint: PluginCheckConstraintDefinition) -> String? { + guard addsCheckConstraints else { return nil } + return "ALTER TABLE \"\(schema ?? "")\".\"\(table)\" ADD CONSTRAINT \"\(constraint.name)\" CHECK (\(constraint.expression))" + } } @Suite("Create Table draft composition", .serialized) @MainActor struct CreateTableDraftCompositionTests { - private static func inject(type: DatabaseType, sessionSchema: String) -> (DatabaseConnection, CompositionDriver) { + private static func inject( + type: DatabaseType, + sessionSchema: String, + addsCheckConstraints: Bool = true + ) -> (DatabaseConnection, CompositionDriver) { let connection = TestFixtures.makeConnection(database: "shop", type: type) - let driver = CompositionDriver(currentSchema: sessionSchema) + let driver = CompositionDriver(currentSchema: sessionSchema, addsCheckConstraints: addsCheckConstraints) var session = ConnectionSession( connection: connection, driver: PluginDriverAdapter(connection: connection, pluginDriver: driver) @@ -71,8 +82,12 @@ struct CreateTableDraftCompositionTests { DatabaseManager.shared.removeSession(for: connection.id) } - private static func makeDraft(tableName: String) -> CreateTableDraft { - let draft = CreateTableDraft() + private static func makeDraft( + tableName: String, + connection: DatabaseConnection, + scope: DatabaseScope + ) -> CreateTableDraft { + let draft = CreateTableDraft(connection: connection, scope: scope) draft.tableName = tableName var column = EditableColumnDefinition.placeholder() column.name = "id" @@ -87,9 +102,9 @@ struct CreateTableDraftCompositionTests { defer { Self.tearDown(connection) } let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") _ = try await Self.seedPooledDriver(connection, scope: scope) - let draft = Self.makeDraft(tableName: "sales") + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) - await draft.recompose(databaseType: .postgresql, scope: scope) + await draft.recompose() #expect(draft.composed?.statements == ["CREATE TABLE \"reporting\".\"sales\" (\"id\" INT)"]) #expect(draft.compositionFailure == nil) @@ -101,8 +116,8 @@ struct CreateTableDraftCompositionTests { let (connection, _) = Self.inject(type: .pglite, sessionSchema: "reporting") defer { Self.tearDown(connection) } let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") - let draft = Self.makeDraft(tableName: "sales") - await draft.recompose(databaseType: .pglite, scope: scope) + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) + await draft.recompose() #expect(draft.composed?.tableName == "sales") let acquired = AsyncGate() @@ -117,7 +132,7 @@ struct CreateTableDraftCompositionTests { draft.tableName = "invoices" let recomposition = Task { @MainActor in - await draft.recompose(databaseType: .pglite, scope: scope) + await draft.recompose() } _ = try? await waitUntil { DatabaseManager.shared.sessionDriverGate.waiterCount(for: connection.id) >= 1 } @@ -136,12 +151,12 @@ struct CreateTableDraftCompositionTests { let (connection, _) = Self.inject(type: .pglite, sessionSchema: "reporting") defer { Self.tearDown(connection) } let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") - let draft = Self.makeDraft(tableName: "sales") - await draft.recompose(databaseType: .pglite, scope: scope) + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) + await draft.recompose() DatabaseManager.shared.removeSession(for: connection.id) draft.tableName = "invoices" - await draft.recompose(databaseType: .pglite, scope: scope) + await draft.recompose() #expect(draft.composed?.statements == ["CREATE TABLE \"reporting\".\"sales\" (\"id\" INT)"]) #expect(draft.compositionFailure != nil) @@ -152,31 +167,30 @@ struct CreateTableDraftCompositionTests { let (connection, driver) = Self.inject(type: .pglite, sessionSchema: "reporting") defer { Self.tearDown(connection) } let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") - let draft = Self.makeDraft(tableName: "sales") + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) - await draft.recompose(databaseType: .pglite, scope: scope) - await draft.recompose(databaseType: .pglite, scope: scope) + await draft.recompose() + await draft.recompose() #expect(driver.createTableRequests == 1) } @Test( "A grid edit reaches whoever observes the draft, so the preview and Create Table follow it", - arguments: [StructureTab.columns, StructureTab.indexes, StructureTab.foreignKeys] + arguments: StructureSection.allCases ) - func gridEditPublishesTheDraft(tab: StructureTab) throws { + func gridEditPublishesTheDraft(section: StructureSection) throws { let connection = TestFixtures.makeConnection(type: .postgresql) let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") - let draft = Self.makeDraft(tableName: "sales") + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) draft.changeManager.addNewIndex() draft.changeManager.addNewForeignKey() - let adapter = draft.gridAdapter(for: connection) - adapter.select(tab) - adapter.refresh() - let model = adapter.model + draft.changeManager.addNewCheckConstraint() + draft.editor.refresh() + let model = draft.editor.adapter(for: section).model let rowID = try #require(model.rowID(at: 0)) let name = try #require(model.columns.column(at: 0)) - let keyBefore = draft.compositionKey(scope: scope) + let keyBefore = draft.compositionKey var published = 0 let observation = draft.objectWillChange.sink { published += 1 } @@ -188,7 +202,7 @@ struct CreateTableDraftCompositionTests { #expect(committed) #expect(published > 0) - #expect(draft.compositionKey(scope: scope) != keyBefore) + #expect(draft.compositionKey != keyBefore) } @Test("A typed comment is composed after the CREATE, on the tab's schema") @@ -197,10 +211,10 @@ struct CreateTableDraftCompositionTests { defer { Self.tearDown(connection) } let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") _ = try await Self.seedPooledDriver(connection, scope: scope) - let draft = Self.makeDraft(tableName: "sales") + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) draft.tableOptions.comment = "Daily sales" - await draft.recompose(databaseType: .postgresql, scope: scope) + await draft.recompose() #expect(draft.composed?.statements == [ "CREATE TABLE \"reporting\".\"sales\" (\"id\" INT)", @@ -213,27 +227,95 @@ struct CreateTableDraftCompositionTests { let (connection, driver) = Self.inject(type: .pglite, sessionSchema: "reporting") defer { Self.tearDown(connection) } let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") - let draft = Self.makeDraft(tableName: "sales") - await draft.recompose(databaseType: .pglite, scope: scope) - let keyBefore = draft.compositionKey(scope: scope) + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) + await draft.recompose() + let keyBefore = draft.compositionKey draft.tableOptions.comment = "Daily sales" - await draft.recompose(databaseType: .pglite, scope: scope) + await draft.recompose() - #expect(draft.compositionKey(scope: scope) != keyBefore) + #expect(draft.compositionKey != keyBefore) #expect(driver.createTableRequests == 2) #expect(draft.composed?.statements.last == "COMMENT ON TABLE \"reporting\".\"sales\" IS 'Daily sales'") } @Test("A draft with nothing to create names what is missing without a connection") func emptyDraftNeedsNoConnection() async { - let scope = DatabaseScope(connectionId: UUID(), database: "shop", schema: nil) - let draft = CreateTableDraft() + let connection = TestFixtures.makeConnection(type: .postgresql) + let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: nil) + let draft = CreateTableDraft(connection: connection, scope: scope) - await draft.recompose(databaseType: .postgresql, scope: scope) + await draft.recompose() #expect(draft.composed?.statements.isEmpty == true) #expect(draft.composed?.issues.isEmpty == false) #expect(draft.compositionFailure == nil) } + + private static func check(_ name: String, _ expression: String) -> EditableCheckConstraintDefinition { + var constraint = EditableCheckConstraintDefinition.placeholder() + constraint.name = name + constraint.expression = expression + return constraint + } + + @Test("A check constraint is added right after the CREATE, on the tab's schema") + func checkConstraintIsComposed() async throws { + let (connection, _) = Self.inject(type: .postgresql, sessionSchema: "public") + defer { Self.tearDown(connection) } + let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") + _ = try await Self.seedPooledDriver(connection, scope: scope) + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) + draft.changeManager.workingCheckConstraints = [Self.check("positive_id", "id > 0")] + draft.tableOptions.comment = "Daily sales" + + await draft.recompose() + + #expect(draft.composed?.statements == [ + "CREATE TABLE \"reporting\".\"sales\" (\"id\" INT)", + "ALTER TABLE \"reporting\".\"sales\" ADD CONSTRAINT \"positive_id\" CHECK (id > 0)", + "COMMENT ON TABLE \"reporting\".\"sales\" IS 'Daily sales'" + ]) + #expect(draft.composed?.issues.isEmpty == true) + } + + @Test("A check the driver cannot add is named on its own row, never dropped") + func refusedCheckConstraintIsAnIssue() async throws { + let (connection, _) = Self.inject(type: .pglite, sessionSchema: "reporting", addsCheckConstraints: false) + defer { Self.tearDown(connection) } + let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) + draft.changeManager.workingCheckConstraints = [ + EditableCheckConstraintDefinition.placeholder(), + Self.check("positive_id", "id > 0") + ] + + await draft.recompose() + + let composed = try #require(draft.composed) + #expect(composed.statements == ["CREATE TABLE \"reporting\".\"sales\" (\"id\" INT)"]) + let issue = try #require(composed.issues.first) + #expect(issue.section == .checkConstraints) + #expect(issue.row == 1) + #expect(draft.entityID(for: issue) == draft.changeManager.workingCheckConstraints[1].id) + } + + @Test("An index the driver cannot add is named on its working row, past a blank one above it") + func refusedIndexNamesItsWorkingRow() async throws { + let (connection, _) = Self.inject(type: .pglite, sessionSchema: "reporting") + defer { Self.tearDown(connection) } + let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: "reporting") + let draft = Self.makeDraft(tableName: "sales", connection: connection, scope: scope) + var index = EditableIndexDefinition.placeholder() + index.name = "sales_id" + index.columns = ["id"] + draft.changeManager.workingIndexes = [EditableIndexDefinition.placeholder(), index] + + await draft.recompose() + + let composed = try #require(draft.composed) + let issue = try #require(composed.issues.first { $0.section == .indexes }) + #expect(issue.row == 1) + #expect(draft.entityID(for: issue) == index.id) + } } diff --git a/apple/TableProTests/Views/Structure/StructureAvailabilityTests.swift b/apple/TableProTests/Views/Structure/StructureAvailabilityTests.swift new file mode 100644 index 0000000000..55605cb377 --- /dev/null +++ b/apple/TableProTests/Views/Structure/StructureAvailabilityTests.swift @@ -0,0 +1,136 @@ +// +// StructureAvailabilityTests.swift +// TableProTests +// +// Which sections the Definition page lists and which panes the Structure view offers. +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct StructureAvailabilityTests { + private static let legacyMySQL = StructureServerSupport( + unsupportedColumnFields: [], + unsupportedIndexTypes: [], + checkConstraintRefusal: "Check constraints need MySQL 8.0.16 or later." + ) + + private let suite: EphemeralDefaults + + init() throws { + suite = try EphemeralDefaults() + } + + private func makeEditor( + _ type: DatabaseType, + support: StructureServerSupport = .unrestricted + ) -> StructureDefinitionEditor { + let editor = StructureDefinitionEditor( + manager: StructureChangeManager(), + context: .existingTable( + connection: TestFixtures.makeConnection(type: type), + databaseName: "db", + schemaName: nil, + tableName: "t", + objectKind: .table, + isSessionBound: false + ), + defaults: suite.defaults + ) + editor.serverSupport = support + return editor + } + + private func makeSession(_ type: DatabaseType) -> StructureEditingSession { + StructureEditingSession( + identity: "db.t", + connection: TestFixtures.makeConnection(database: "db", type: type), + databaseName: "db", + schemaName: nil, + tableName: "t" + ) + } + + // MARK: - Sections + + @Test("A MySQL server that discards a CHECK clause is not offered the section") + func legacyServerHidesConstraints() { + let editor = makeEditor(.mysql, support: Self.legacyMySQL) + #expect(!editor.offers(.checkConstraints)) + #expect(editor.sections == [.columns, .indexes, .foreignKeys]) + } + + @Test("A server that keeps them is offered the section, on every engine that has them") + func modernServerShowsConstraints() { + #expect(makeEditor(.mysql).offers(.checkConstraints)) + #expect(makeEditor(.postgresql).offers(.checkConstraints)) + } + + @Test("An engine with no check constraints at all leaves the section out either way") + func engineWithoutChecksStaysHidden() { + for support in [StructureServerSupport.unrestricted, Self.legacyMySQL] { + #expect(!makeEditor(.redis, support: support).offers(.checkConstraints)) + } + } + + @Test("Foreign Keys follows the engine") + func foreignKeysFollowTheEngine() { + #expect(makeEditor(.postgresql).offers(.foreignKeys)) + #expect(!makeEditor(.clickhouse).offers(.foreignKeys)) + } + + @Test("Columns and Indexes are listed on every engine, first and in that order") + func columnsAndIndexesEverywhere() { + for type in [DatabaseType.mysql, .postgresql, .sqlite, .redis, .clickhouse] { + let sections = makeEditor(type).sections + #expect(Array(sections.prefix(2)) == [.columns, .indexes], "\(type.rawValue)") + } + #expect(makeEditor(.postgresql).sections == [.columns, .indexes, .foreignKeys, .checkConstraints]) + } + + @Test("A server that withdraws check constraints later takes the section off the page") + func withdrawingSupportUpdatesTheSections() { + let editor = makeEditor(.mysql) + #expect(editor.sections.contains(.checkConstraints)) + + editor.serverSupport = Self.legacyMySQL + + #expect(!editor.sections.contains(.checkConstraints)) + } + + // MARK: - Panes + + @Test("Definition and DDL are offered on every engine, Definition first") + func definitionAndDDLEverywhere() { + for type in [DatabaseType.mysql, .postgresql, .redis, .clickhouse] { + let panes = makeSession(type).availablePanes + #expect(panes.first == .definition, "\(type.rawValue)") + #expect(panes.contains(.ddl), "\(type.rawValue)") + } + } + + @Test("Triggers follows the engine") + func triggersFollowTheEngine() { + #expect(makeSession(.mysql).availablePanes.contains(.triggers)) + #expect(makeSession(.postgresql).availablePanes.contains(.triggers)) + #expect(!makeSession(.redis).availablePanes.contains(.triggers)) + } + + @Test("Parts is ClickHouse only, and the server refusal does not reach it") + func partsIsClickHouseOnly() { + let clickhouse = makeSession(.clickhouse) + #expect(clickhouse.availablePanes.contains(.parts)) + #expect(!makeSession(.mysql).availablePanes.contains(.parts)) + + clickhouse.serverSupport = Self.legacyMySQL + #expect(clickhouse.availablePanes.contains(.parts)) + } + + @Test("Panes keep one order: Definition, Triggers, DDL, Parts") + func panesKeepTheirOrder() { + #expect(makeSession(.mysql).availablePanes == [.definition, .triggers, .ddl]) + #expect(makeSession(.clickhouse).availablePanes == [.definition, .ddl, .parts]) + } +} diff --git a/apple/TableProTests/Views/Structure/StructureCodeStandInThemeTests.swift b/apple/TableProTests/Views/Structure/StructureCodeStandInThemeTests.swift index 82cb841267..05b0702b84 100644 --- a/apple/TableProTests/Views/Structure/StructureCodeStandInThemeTests.swift +++ b/apple/TableProTests/Views/Structure/StructureCodeStandInThemeTests.swift @@ -38,7 +38,7 @@ struct StructureCodeStandInThemeTests { @Test("No DDL available stands in for the DDL view on the editor background") func missingDDL() throws { - try expectEditorStandIn(StructureDDLUnavailableView()) + try expectEditorStandIn(StructureDDLStandIn()) } @Test("The Create Table SQL Preview placeholder sits on the editor background") diff --git a/apple/TableProTests/Views/Structure/StructureDefinitionEditorTests.swift b/apple/TableProTests/Views/Structure/StructureDefinitionEditorTests.swift new file mode 100644 index 0000000000..01f56ac935 --- /dev/null +++ b/apple/TableProTests/Views/Structure/StructureDefinitionEditorTests.swift @@ -0,0 +1,298 @@ +// +// StructureDefinitionEditorTests.swift +// TableProTests +// + +import AppKit +import Foundation +import TableProPluginKit +import TableProTestSupport +import Testing +@testable import TablePro + +@MainActor +struct StructureDefinitionEditorTests { + private let suite: EphemeralDefaults + + init() throws { + suite = try EphemeralDefaults() + } + + private func makeEditor(type: DatabaseType = .postgresql) -> StructureDefinitionEditor { + let manager = StructureChangeManager() + manager.loadSchema( + tableName: "orders", + columns: [ + ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil), + ColumnInfo(name: "customer_id", dataType: "INT", isNullable: false, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil), + ColumnInfo(name: "status", dataType: "TEXT", isNullable: true, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil) + ], + indexes: [IndexInfo(name: "orders_pkey", columns: ["id"], isUnique: true, isPrimary: true, type: "BTREE")], + foreignKeys: [], + primaryKey: ["id"] + ) + let editor = StructureDefinitionEditor( + manager: manager, + context: .existingTable( + connection: TestFixtures.makeConnection(type: type), + databaseName: "shop", + schemaName: "public", + tableName: "orders", + objectKind: .table, + isSessionBound: false + ), + defaults: suite.defaults + ) + editor.refresh() + return editor + } + + private func id(of name: String, in editor: StructureDefinitionEditor) -> UUID { + editor.manager.workingColumns.first { $0.name == name }!.id + } + + @Test("Nothing selected puts the table in the inspector") + func startsOnTable() { + #expect(makeEditor().inspectorTarget == .table) + } + + @Test("Selecting a column names it for the inspector") + func selectingColumnTargetsIt() { + let editor = makeEditor() + let status = id(of: "status", in: editor) + editor.adapter(for: .columns).select(entity: status) + + #expect(editor.inspectorTarget == .entity(.columns, status)) + #expect(editor.focusedSection == .columns) + } + + @Test("Selecting in one section clears the others") + func selectionIsExclusiveAcrossSections() { + let editor = makeEditor() + editor.adapter(for: .columns).select(entity: id(of: "status", in: editor)) + let index = editor.manager.workingIndexes[0].id + editor.adapter(for: .indexes).select(entity: index) + + #expect(editor.adapter(for: .columns).model.selection.isEmpty) + #expect(editor.inspectorTarget == .entity(.indexes, index)) + } + + @Test("A row keeps its identity when a sort reorders the section") + func rowIdentitySurvivesSort() { + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + let status = id(of: "status", in: editor) + adapter.select(entity: status) + + adapter.sortDidChange(SortState(columns: [SortColumn(columnIndex: 0, direction: .descending)], source: .user)) + + #expect(adapter.selectedEntityIDs == [status]) + #expect(editor.inspectorTarget == .entity(.columns, status)) + } + + @Test("Removing a new column above keeps the selection on the same definition") + func rowIdentitySurvivesRemovalAbove() { + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + adapter.addRow() + let added = editor.manager.workingColumns.last!.id + let moved = editor.manager.workingColumns.removeLast() + editor.manager.workingColumns.insert(moved, at: 0) + editor.refresh() + let status = id(of: "status", in: editor) + adapter.select(entity: status) + + editor.manager.deleteColumn(id: added) + editor.refresh() + + #expect(adapter.selectedEntityIDs == [status]) + } + + @Test("Adding a column selects it, so the inspector opens on it") + func addingRevealsTheNewRow() { + let editor = makeEditor() + editor.add(.columns) + let added = editor.manager.workingColumns.last!.id + + #expect(editor.inspectorTarget == .entity(.columns, added)) + } + + @Test("Revealing a definition the filter hides clears the filter and selects it") + func revealClearsAHidingFilter() { + let editor = makeEditor() + editor.searchText = "stat" + let id = id(of: "customer_id", in: editor) + + editor.reveal(section: .columns, entity: id) + + #expect(editor.searchText.isEmpty) + #expect(editor.inspectorTarget == .entity(.columns, id)) + } + + @Test("A definition renamed out of the filter stays listed and selected while it is edited") + func renamedDefinitionStaysUnderTheFilter() { + let editor = makeEditor() + editor.searchText = "stat" + let status = id(of: "status", in: editor) + editor.adapter(for: .columns).select(entity: status) + + editor.typeInspectorField(section: .columns, id: status, field: 0, value: "phase") + + #expect(editor.inspectorTarget == .entity(.columns, status)) + #expect(editor.adapter(for: .columns).visibleRowCount == 1) + } + + @Test("A reload keeps the selected definition, under the name the save gave it") + func reloadKeepsTheSelection() async throws { + let editor = makeEditor() + let status = id(of: "status", in: editor) + let index = editor.manager.workingIndexes[0].id + editor.adapter(for: .columns).select(entity: status) + editor.commitInspectorField(section: .columns, id: status, field: 0, value: "phase") + let version = editor.manager.reloadVersion + + editor.manager.loadSchema( + tableName: "orders", + columns: [ + ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil), + ColumnInfo(name: "customer_id", dataType: "INT", isNullable: false, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil), + ColumnInfo(name: "phase", dataType: "TEXT", isNullable: true, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil) + ], + indexes: [IndexInfo(name: "orders_pkey", columns: ["id"], isUnique: true, isPrimary: true, type: "BTREE")], + foreignKeys: [], + primaryKey: ["id"] + ) + try await waitUntil { editor.manager.reloadVersion > version && editor.adapter(for: .columns).rowCount == 3 } + + #expect(id(of: "phase", in: editor) == status) + #expect(editor.manager.workingIndexes[0].id == index) + #expect(Set(editor.manager.workingColumns.map(\.id)).count == 3) + #expect(editor.inspectorTarget == .entity(.columns, status)) + } + + @Test("A save's hold closes the grids and its end opens them again") + func saveHoldTogglesEditing() async throws { + let editor = makeEditor() + editor.manager.deleteColumn(id: id(of: "status", in: editor)) + let hold = try #require(editor.manager.holdForSave()) + try await waitUntil { !editor.adapter(for: .columns).model.configuration.isEditable } + + _ = editor.manager.releaseHold(hold, written: false) + + try await waitUntil { editor.adapter(for: .columns).model.configuration.isEditable } + } + + @Test("A collapsed section stays collapsed for the next table") + func collapsedSectionsPersist() { + let editor = makeEditor() + editor.setExpanded(.indexes, false) + + #expect(!makeEditor().isExpanded(.indexes)) + } + + @Test("A filter shows a collapsed section's matches") + func filterExpandsCollapsedSections() { + let editor = makeEditor() + editor.setExpanded(.columns, false) + editor.searchText = "stat" + + #expect(editor.isExpanded(.columns)) + #expect(editor.adapter(for: .columns).visibleRowCount == 1) + } + + @Test("Counts leave out definitions marked for deletion") + func countsSkipDeletions() { + let editor = makeEditor() + editor.manager.deleteColumn(id: id(of: "status", in: editor)) + + #expect(editor.count(of: .columns) == 2) + } + + @Test("MySQL tables offer no index condition, since their DDL drops it") + func mysqlHasNoIndexCondition() { + let editor = makeEditor(type: .mysql) + #expect(!editor.adapter(for: .indexes).provider.columns.contains(String(localized: "Condition"))) + #expect(makeEditor().adapter(for: .indexes).provider.columns.contains(String(localized: "Condition"))) + } + + @Test("A pick after typing is its own undo step") + func pickAfterTypingIsItsOwnStep() { + let editor = makeEditor() + let status = id(of: "status", in: editor) + editor.typeInspectorField(section: .columns, id: status, field: 0, value: "stat") + editor.typeInspectorField(section: .columns, id: status, field: 0, value: "state") + editor.commitInspectorField(section: .columns, id: status, field: 1, value: "VARCHAR(20)") + + editor.manager.undo() + #expect(editor.manager.workingColumns.contains { $0.name == "state" && $0.dataType != "VARCHAR(20)" }) + editor.manager.undo() + #expect(editor.manager.workingColumns.contains { $0.name == "status" }) + } + + @Test("Nothing can be edited while the table is read again after a save") + func awaitingBaselineBlocksEdits() { + let editor = makeEditor() + editor.isAwaitingBaseline = true + + #expect(!editor.canAdd(.columns)) + editor.add(.columns) + #expect(editor.manager.workingColumns.count == 3) + let status = id(of: "status", in: editor) + #expect(editor.inspectorForm(for: .columns, id: status)?.isEditable == false) + } + + @Test("Several rows selected says how many instead of picking one") + func severalSelected() { + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + adapter.model.selectRows([ + adapter.rowID(forEntity: id(of: "id", in: editor)), + adapter.rowID(forEntity: id(of: "status", in: editor)) + ]) + + #expect(editor.inspectorTarget == .several(.columns, 2)) + } +} + +@MainActor +struct StructureDefinitionPasteTests { + @Test("Pasting several columns is one undo step") + func pasteIsOneUndoStep() throws { + let suite = try EphemeralDefaults() + let manager = StructureChangeManager() + manager.loadSchema( + tableName: "t", + columns: [ + ColumnInfo(name: "a", dataType: "INT", isNullable: true, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil), + ColumnInfo(name: "b", dataType: "INT", isNullable: true, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil) + ], + indexes: [], + foreignKeys: [], + primaryKey: [] + ) + let editor = StructureDefinitionEditor( + manager: manager, + context: .newTable(connection: TestFixtures.makeConnection(type: .postgresql), databaseName: "db", schemaName: nil), + defaults: suite.defaults + ) + editor.refresh() + let adapter = editor.adapter(for: .columns) + let pasteboard = NSPasteboard(name: NSPasteboard.Name("structure-paste-\(UUID().uuidString)")) + defer { pasteboard.releaseGlobally() } + adapter.copyRows(manager.workingColumns.map { adapter.rowID(forEntity: $0.id) }, to: pasteboard) + + adapter.pasteRows(from: pasteboard) + #expect(manager.workingColumns.count == 4) + + manager.undo() + #expect(manager.workingColumns.count == 2) + } +} diff --git a/apple/TableProTests/Views/Structure/StructureEditGateTests.swift b/apple/TableProTests/Views/Structure/StructureEditGateTests.swift index 8db3e43eff..8b9617dccb 100644 --- a/apple/TableProTests/Views/Structure/StructureEditGateTests.swift +++ b/apple/TableProTests/Views/Structure/StructureEditGateTests.swift @@ -119,7 +119,6 @@ struct StructureEditGateTests { #expect(gate(.materializedView).locksField(at: 0, on: .foreignKeys, orderedFields: [])) #expect(gate(.materializedView).locksField(at: 0, on: .checkConstraints, orderedFields: [])) #expect(!gate(.table).locksField(at: 0, on: .indexes, orderedFields: [])) - #expect(!gate(.table).locksField(at: 0, on: .ddl, orderedFields: [])) } @Test("The Columns list locks field by field") diff --git a/apple/TableProTests/Views/Structure/StructureEditingSessionTests.swift b/apple/TableProTests/Views/Structure/StructureEditingSessionTests.swift index 286b5b4328..d1e40cd7a6 100644 --- a/apple/TableProTests/Views/Structure/StructureEditingSessionTests.swift +++ b/apple/TableProTests/Views/Structure/StructureEditingSessionTests.swift @@ -38,6 +38,45 @@ private final class StructureSessionDriver: PluginDriverStub, @unchecked Sendabl } } +// Records the reads a load makes, in order. +private final class StructureLoadDriver: PluginDriverStub, @unchecked Sendable { + private let lock = NSLock() + private var log: [String] = [] + + var supportsSchemas: Bool { false } + var supportsTransactions: Bool { false } + var currentSchema: String? { nil } + var serverVersion: String? { nil } + + var reads: [String] { lock.withLock { log } } + + private func record(_ read: String) { + lock.withLock { log.append(read) } + } + + func switchDatabase(to database: String) async throws {} + + func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { + record("columns") + return [PluginColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true)] + } + + func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { + record("indexes") + return [] + } + + func fetchForeignKeys(table: String, schema: String?) async throws -> [PluginForeignKeyInfo] { + record("foreignKeys") + return [] + } + + func fetchTableDDL(table: String, schema: String?) async throws -> String { + record("ddl") + return "CREATE TABLE `\(table)` (`id` INT)" + } +} + @Suite("Structure editing session", .serialized) @MainActor struct StructureEditingSessionTests { @@ -64,6 +103,7 @@ struct StructureEditingSessionTests { foreignKeys: [], primaryKey: [] ) + session.markBaselineAdopted() manager.addNewColumn() guard var column = manager.workingColumns.last else { return } column.name = "notes" @@ -90,68 +130,85 @@ struct StructureEditingSessionTests { } // A grid owner seeded once into a view's `@State` kept writing into the first session to mount. - @Test("A session's grid adapter writes into that session's change manager") - func gridAdapterBelongsToItsSession() { + @Test("A session's editor and its section grids write into that session's change manager") + func editorBelongsToItsSession() { let connection = TestFixtures.makeConnection() let first = Self.makeSession(connection: connection) let second = Self.makeSession(connection: connection) - #expect(first.gridAdapter !== second.gridAdapter) - #expect(first.gridModel !== second.gridModel) - #expect(first.gridAdapter.structureChangeManager === first.changeManager) - #expect(second.gridAdapter.structureChangeManager === second.changeManager) - #expect(first.gridAdapter.session === first) - #expect(first.gridAdapter.store.session === first) + #expect(first.editor !== second.editor) + #expect(first.editor.manager === first.changeManager) + #expect(second.editor.manager === second.changeManager) + for section in StructureSection.allCases { + let adapter = first.editor.adapter(for: section) + #expect(adapter !== second.editor.adapter(for: section), "\(section.rawValue)") + #expect(adapter.model !== second.editor.adapter(for: section).model, "\(section.rawValue)") + #expect(adapter.manager === first.changeManager, "\(section.rawValue)") + #expect(adapter.editor === first.editor, "\(section.rawValue)") + } } - @Test("A session's grid model survives the view: the adapter, not a mount, owns it") + @Test("A section's grid model survives the view: the editor, not a mount, owns it") func gridModelOutlivesTheView() { let session = Self.makeSession(connection: TestFixtures.makeConnection()) Self.stageAColumn(on: session) - session.gridAdapter.refresh() - let model = session.gridModel + session.editor.refresh() + let adapter = session.editor.adapter(for: .columns) + let model = adapter.model #expect(!model.isMounted) #expect(model.rowCount == 1) - #expect(session.gridModel === model) - #expect(model.dataSource === session.gridAdapter) + #expect(session.editor.adapter(for: .columns).model === model) + #expect(model.dataSource === adapter) } - @Test("The session answers the grid on screen only while a sub-tab shows one") - func activeGridModelFollowsTheSubTab() { + @Test("The session answers the grid holding the selection, and no grid on a pane that draws none") + func activeGridModelFollowsThePane() { let session = Self.makeSession(connection: TestFixtures.makeConnection()) + session.changeManager.loadSchema( + tableName: "users", + columns: [ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true)], + indexes: [IndexInfo(name: "ix_id", columns: ["id"], isUnique: false, isPrimary: false, type: "BTREE")], + foreignKeys: [], + primaryKey: ["id"] + ) + session.markBaselineAdopted() + session.editor.refresh() + let indexes = session.editor.adapter(for: .indexes) - for tab in [StructureTab.columns, .indexes, .foreignKeys, .checkConstraints] { - session.selectedTab = tab - #expect(session.activeGridModel === session.gridModel, "\(tab.rawValue)") - } - for tab in [StructureTab.options, .ddl, .parts, .triggers] { - session.selectedTab = tab - #expect(session.activeGridModel == nil, "\(tab.rawValue)") + #expect(session.activeGridModel === session.editor.adapter(for: .columns).model) + + indexes.select(entity: session.changeManager.workingIndexes[0].id) + #expect(session.activeGridModel === indexes.model) + + for pane in [StructurePane.triggers, .ddl, .parts] { + session.pane = pane + #expect(session.activeGridModel == nil, "\(pane.rawValue)") } } - // Sharing the place moved one tab to Indexes when the other did, and lost the sub-tab on a Data trip. + // Sharing the place moved one tab to Indexes when the other did, and lost the pane on a Data trip. @Test("Each session keeps its own place in the editor") func editorPlaceIsPerSession() { let connection = TestFixtures.makeConnection() let first = Self.makeSession(connection: connection) let second = Self.makeSession(connection: connection) - first.selectedTab = .indexes - first.searchText = "created" + first.pane = .ddl + first.editor.searchText = "created" - #expect(second.selectedTab == .columns) - #expect(second.searchText.isEmpty) + #expect(second.pane == .definition) + #expect(second.editor.searchText.isEmpty) } - // MARK: - Sub-tab switch + // MARK: - Open edits - // The switch used to commit an open edit into the incoming sub-tab's rows. - @Test("An edit open when the sub-tab changes commits into the column it was typed on") - func openEditCommitsIntoItsOwnSubTab() throws { + // A switch used to commit an open edit into the incoming sub-tab's rows; each section now owns its grid. + @Test("An edit open when Undo runs lands in the column it was typed on, and Undo takes that edit back") + func openEditCommitsIntoItsOwnSection() throws { let session = Self.makeSession(connection: TestFixtures.makeConnection(database: "testdb", type: .mysql)) - session.changeManager.loadSchema( + let manager = session.changeManager + manager.loadSchema( tableName: "users", columns: ["id", "email"].map { ColumnInfo(name: $0, dataType: "VARCHAR(64)", isNullable: true, isPrimaryKey: false) @@ -162,8 +219,13 @@ struct StructureEditingSessionTests { foreignKeys: [], primaryKey: [] ) - session.gridAdapter.refresh() - let controller = DataGridViewController(model: session.gridModel) + session.markBaselineAdopted() + var index = manager.workingIndexes[0] + index.name = "ix_a" + manager.updateIndex(id: index.id, with: index) + session.editor.refresh() + let model = session.editor.adapter(for: .columns).model + let controller = DataGridViewController(model: model) let window = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 800, height: 400), styleMask: [.titled], @@ -180,12 +242,139 @@ struct StructureEditingSessionTests { let editor = try #require(controller.editingState.editingCellView.field.currentEditor() as? NSTextView) editor.string = "mail" - session.selectedTab = .indexes - session.gridAdapter.refresh() + session.undo() + + #expect(manager.workingColumns.map(\.name) == ["id", "email"]) + #expect(manager.workingIndexes.map(\.name) == ["ix_a", "ix_email"]) + #expect(!model.isEditing) + #expect(session.canRedo) + + session.redo() + #expect(manager.workingColumns.map(\.name) == ["id", "mail"]) + } + + // MARK: - Row commands + + @Test("Row commands do nothing on a pane that shows no grid", arguments: [StructurePane.triggers, .ddl, .parts]) + func rowCommandsNeedTheDefinitionPane(pane: StructurePane) { + let session = Self.makeSession(connection: TestFixtures.makeConnection()) + Self.stageAColumn(on: session) + session.editor.refresh() + session.editor.adapter(for: .columns).select(entity: session.changeManager.workingColumns[0].id) + let staged = session.changeManager.getChangesArray() + let columns = session.changeManager.workingColumns + session.pane = pane + + #expect(!session.canAddRow) + #expect(!session.canRemoveRows) + session.addRow() + session.removeSelectedRows() + session.duplicateSelectedRows() + + #expect(session.changeManager.getChangesArray() == staged) + #expect(session.changeManager.workingColumns == columns) + } + + @Test("A failed section still shows the sections that loaded, read-only") + func partialReadShowsLoadedSectionsReadOnly() { + let session = Self.makeSession(connection: TestFixtures.makeConnection()) + session.columns = [ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true)] + session.loadState.markFetched(.columns) + session.loadState.markFailed(.indexes, message: "timed out") + + session.adoptIfCurrent() + + #expect(session.changeManager.workingColumns.map(\.name) == ["id"]) + #expect(!session.hasBaseline) + #expect(!session.canAddRow) + #expect(!session.editor.canAdd(.columns)) + } + + @Test("Text typed in the inspector is staged as it is typed, and its run undoes as one step") + func inspectorTypingIsStagedAndUndoesOnce() { + let session = Self.makeSession(connection: TestFixtures.makeConnection()) + session.changeManager.loadSchema( + tableName: "users", + columns: [ColumnInfo(name: "email", dataType: "TEXT", isNullable: true, isPrimaryKey: false)], + indexes: [], + foreignKeys: [], + primaryKey: [] + ) + session.markBaselineAdopted() + session.editor.refresh() + let id = session.changeManager.workingColumns[0].id + for text in ["e", "em", "ema", "mail"] { + session.editor.typeInspectorField(section: .columns, id: id, field: 0, value: text) + } + + #expect(session.hasChanges) + #expect(session.changeManager.workingColumns[0].name == "mail") + #expect(session.undoMenuItemTitle.contains(String(localized: "Edit Column"))) - #expect(session.changeManager.workingColumns.map(\.name) == ["id", "mail"]) - #expect(session.changeManager.workingIndexes.map(\.name) == ["ix_id", "ix_email"]) - #expect(!session.gridModel.isEditing) + session.undo() + + #expect(session.changeManager.workingColumns[0].name == "email") + #expect(!session.hasChanges) + } + + @Test("Row commands wait for the definitions the table was read with") + func rowCommandsWaitForTheBaseline() { + let session = Self.makeSession(connection: TestFixtures.makeConnection()) + #expect(!session.canAddRow) + session.addRow() + #expect(session.changeManager.workingColumns.isEmpty) + } + + @Test("Add and Remove act on the section holding the selection") + func rowCommandsFollowTheFocusedSection() { + let session = Self.makeSession(connection: TestFixtures.makeConnection()) + session.changeManager.loadSchema( + tableName: "users", + columns: [ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true)], + indexes: [IndexInfo(name: "ix_id", columns: ["id"], isUnique: false, isPrimary: false, type: "BTREE")], + foreignKeys: [], + primaryKey: ["id"] + ) + session.markBaselineAdopted() + session.editor.refresh() + session.editor.adapter(for: .indexes).select(entity: session.changeManager.workingIndexes[0].id) + + #expect(session.canAddRow) + session.addRow() + + #expect(session.changeManager.workingIndexes.count == 2) + #expect(session.changeManager.workingColumns.count == 1) + } + + // Cmd+Shift+D on several rows used to register one undo per row. + @Test("Duplicating several selected rows is one undo step") + func duplicateIsOneUndoStep() { + let session = Self.makeSession(connection: TestFixtures.makeConnection()) + let manager = session.changeManager + manager.loadSchema( + tableName: "users", + columns: ["id", "email", "created_at"].map { + ColumnInfo(name: $0, dataType: "VARCHAR(64)", isNullable: true, isPrimaryKey: false) + }, + indexes: [], + foreignKeys: [], + primaryKey: [] + ) + session.markBaselineAdopted() + session.editor.refresh() + let adapter = session.editor.adapter(for: .columns) + let selected = manager.workingColumns.prefix(2).map { adapter.rowID(forEntity: $0.id) } + adapter.model.selectRows(selected) + + session.duplicateSelectedRows() + #expect(manager.workingColumns.count == 5) + #expect(manager.workingColumns.suffix(2).map(\.name) == ["id", "email"]) + + session.undo() + + #expect(manager.workingColumns.map(\.name) == ["id", "email", "created_at"]) + #expect(!session.hasChanges) + #expect(!session.canUndo) } // MARK: - Applying without a mounted view @@ -382,77 +571,109 @@ struct StructureEditingSessionTests { return driver } - // A withdrawn tab cannot stay selected: no segment would match it. - @Test("A session on Constraints moves to Columns when the server withdraws the tab") - func withdrawnTabMovesSelection() { - let connection = DatabaseConnection(name: "MySQL", type: .mysql) - let session = Self.makeSession(connection: connection) - session.selectedTab = .checkConstraints - #expect(session.availableTabs.contains(.checkConstraints)) + private static let legacyMySQL = StructureServerSupport( + unsupportedColumnFields: [], + unsupportedIndexTypes: [], + checkConstraintRefusal: "Check constraints need MySQL 8.0.16 or later." + ) - session.serverSupport = StructureServerSupport( - unsupportedColumnFields: [], - unsupportedIndexTypes: [], - checkConstraintRefusal: "Check constraints need MySQL 8.0.16 or later." - ) + @Test("A server that withdraws check constraints takes the section off the page and leaves the pane alone") + func withdrawnSectionLeavesThePage() { + let session = Self.makeSession(connection: DatabaseConnection(name: "MySQL", type: .mysql)) + session.pane = .ddl + #expect(session.editor.sections.contains(.checkConstraints)) - #expect(!session.availableTabs.contains(.checkConstraints)) - #expect(session.selectedTab == .columns) + session.serverSupport = Self.legacyMySQL + + #expect(!session.editor.offers(.checkConstraints)) + #expect(!session.editor.sections.contains(.checkConstraints)) + #expect(session.editor.serverSupport == Self.legacyMySQL) + #expect(session.pane == .ddl) } - @Test("A server change that withdraws nothing leaves the selection alone") - func unchangedSupportKeepsSelection() { - let connection = DatabaseConnection(name: "MySQL", type: .mysql) - let session = Self.makeSession(connection: connection) - session.selectedTab = .indexes + // A pane no segment matches cannot stay selected. + @Test("A pane the engine does not offer falls back to Definition when the server's support settles") + func unofferedPaneFallsBackToDefinition() { + let session = Self.makeSession(connection: DatabaseConnection(name: "MySQL", type: .mysql)) + session.pane = .parts + #expect(!session.availablePanes.contains(.parts)) + + session.serverSupport = StructureServerSupport(unsupportedColumnFields: [.generated], unsupportedIndexTypes: []) + + #expect(session.pane == .definition) + } + + @Test("A server change that withdraws nothing leaves the pane and the sections alone") + func unchangedSupportKeepsThePane() { + let session = Self.makeSession(connection: DatabaseConnection(name: "MySQL", type: .mysql)) + session.pane = .triggers session.serverSupport = StructureServerSupport( unsupportedColumnFields: [.generated], unsupportedIndexTypes: ["BRIN"] ) - #expect(session.selectedTab == .indexes) - #expect(session.availableTabs.contains(.checkConstraints)) + #expect(session.pane == .triggers) + #expect(session.editor.offers(.checkConstraints)) } - @Test("A session built for a server with no check constraints never offers the tab") + @Test("A session built for a server with no check constraints never offers the section") func sessionSeededWithServerSupport() { - let connection = DatabaseConnection(name: "MySQL", type: .mysql) let session = StructureEditingSession( identity: "testdb.users", - connection: connection, + connection: DatabaseConnection(name: "MySQL", type: .mysql), databaseName: "testdb", schemaName: nil, tableName: "users", - serverSupport: StructureServerSupport( - unsupportedColumnFields: [], - unsupportedIndexTypes: [], - checkConstraintRefusal: "Check constraints need MySQL 8.0.16 or later." - ) + serverSupport: Self.legacyMySQL ) - #expect(!session.availableTabs.contains(.checkConstraints)) - #expect(session.selectedTab == .columns) + #expect(!session.editor.offers(.checkConstraints)) + #expect(session.editor.sections == [.columns, .indexes, .foreignKeys]) + #expect(session.pane == .definition) } - // A stale session left on DDL or Triggers mounts without a selection change, so nothing else fetches that sub-tab. - @Test("A mount fetches the sub-tab the user left selected, once, after the ones it baselines from") - func mountFetchesTheSelectedSubTab() { - let session = Self.makeSession(connection: TestFixtures.makeConnection()) - #expect(session.tabsFetchedOnMount.first == .columns) - #expect(!session.tabsFetchedOnMount.contains(.ddl)) + // A stale session left on DDL or Triggers mounts without a pane change, so nothing else fetches that pane. + @Test("A load fetches the pane the user left selected, once, after the definitions it baselines from") + func loadFetchesTheSelectedPane() async throws { + let connection = TestFixtures.makeConnection(database: "testdb") + var connectionSession = ConnectionSession( + connection: connection, + driver: PluginDriverAdapter(connection: connection, pluginDriver: StructureLoadDriver()) + ) + connectionSession.browseDatabase = "testdb" + DatabaseManager.shared.injectSession(connectionSession, for: connection.id) - session.selectedTab = .ddl - session.tabData.markFetched(.ddl) - session.hasLoaded = true - session.markStructureStale() + let session = Self.makeSession(connection: connection) + let driver = StructureLoadDriver() + let adapter = PluginDriverAdapter(connection: connection, pluginDriver: driver) + try await adapter.connect() + MetadataConnectionPool.shared.injectEntry(adapter, scope: session.scope) + defer { + MetadataConnectionPool.shared.closeAll(connectionId: connection.id) + DatabaseManager.shared.removeSession(for: connection.id) + } - #expect(!session.hasLoaded) - #expect(session.tabData.needsFetch(.ddl)) - #expect(session.tabsFetchedOnMount.last == .ddl) + await session.load() + #expect(session.hasLoaded) + #expect(driver.reads.first == "columns") + #expect(!driver.reads.contains("ddl")) + #expect(session.changeManager.workingColumns.map(\.name) == ["id"]) - session.selectedTab = .indexes - #expect(session.tabsFetchedOnMount.filter { $0 == .indexes }.count == 1) - #expect(!session.tabsFetchedOnMount.contains(.ddl)) + session.markStructureStale() + #expect(!session.hasLoaded) + session.pane = .ddl + let before = driver.reads.count + await session.load() + + let reload = Array(driver.reads.dropFirst(before)) + #expect(reload.first == "columns") + #expect(reload.last == "ddl") + #expect(reload.filter { $0 == "ddl" }.count == 1) + #expect(session.loadState.hasData(.ddl)) + #expect(!session.loadState.needsFetch(.ddl)) + + await session.load() + #expect(driver.reads.filter { $0 == "ddl" }.count == 1) } // A refetch with edits staged would re-baseline and drop them, and dropping the refetch left an undone session showing old columns. @@ -460,14 +681,14 @@ struct StructureEditingSessionTests { func aChangeOwedWhileEditingIsFetchedOnceTheEditsAreGone() { let session = Self.makeSession(connection: TestFixtures.makeConnection()) Self.stageAColumn(on: session) - session.tabData.markFetched(.columns) + session.loadState.markFetched(.columns) session.hasLoaded = true session.markStructureStale() #expect(session.changeManager.hasChanges, "The staged edit survives the change") #expect(session.hasLoaded) - #expect(!session.tabData.needsFetch(.columns)) + #expect(!session.loadState.needsFetch(.columns)) #expect(!session.settleOwedRefetch(), "Nothing is fetched while the edit is still staged") while session.changeManager.canUndo { @@ -477,7 +698,7 @@ struct StructureEditingSessionTests { #expect(session.settleOwedRefetch()) #expect(!session.hasLoaded) - #expect(session.tabData.needsFetch(.columns)) + #expect(session.loadState.needsFetch(.columns)) #expect(!session.settleOwedRefetch()) } @@ -530,6 +751,7 @@ struct StructureEditingSessionTests { primaryKey: ["id"], tableComment: "Customer orders" ) + session.markBaselineAdopted() session.changeManager.stageTableComment("Paid orders") session.markStructureStale() diff --git a/apple/TableProTests/Views/Structure/StructureEditingSupportFieldDiffTests.swift b/apple/TableProTests/Views/Structure/StructureEditingSupportFieldDiffTests.swift index 0d6d731b6e..2caa8bb017 100644 --- a/apple/TableProTests/Views/Structure/StructureEditingSupportFieldDiffTests.swift +++ b/apple/TableProTests/Views/Structure/StructureEditingSupportFieldDiffTests.swift @@ -243,7 +243,7 @@ struct StructureEditingSupportFieldDiffTests { } } -// MARK: - undoDelete(for:at:) +// MARK: - undoDelete(section:id:) @MainActor struct StructureChangeManagerUndoDeleteTests { @@ -272,7 +272,7 @@ struct StructureChangeManagerUndoDeleteTests { manager.deleteColumn(id: emailColumn.id) #expect(manager.deleteInsertState(for: 1, tab: .columns).isDeleted) - manager.undoDelete(for: .columns, at: 1) + manager.undoDelete(section: .columns, id: emailColumn.id) #expect(!manager.deleteInsertState(for: 1, tab: .columns).isDeleted) #expect(manager.pendingChanges[.column(emailColumn.id)] == nil) } @@ -285,29 +285,44 @@ struct StructureChangeManagerUndoDeleteTests { manager.updateColumn(id: renamed.id, with: renamed) let beforeChanges = manager.pendingChanges - manager.undoDelete(for: .columns, at: 1) + manager.undoDelete(section: .columns, id: renamed.id) #expect(manager.pendingChanges == beforeChanges) } - @Test("undoDelete bounds-checks the row index") - func undoDeleteOutOfRange() { + @Test("undoDelete ignores an id no definition has") + func undoDeleteUnknownID() { let manager = makeManagerWithSchema() - manager.undoDelete(for: .columns, at: 99) - manager.undoDelete(for: .indexes, at: 0) - manager.undoDelete(for: .foreignKeys, at: 0) + manager.undoDelete(section: .columns, id: UUID()) + manager.undoDelete(section: .indexes, id: UUID()) + manager.undoDelete(section: .foreignKeys, id: UUID()) + manager.undoDelete(section: .checkConstraints, id: UUID()) #expect(manager.pendingChanges.isEmpty) } - @Test("undoDelete on .ddl / .parts tabs is a no-op") - func undoDeleteDDLAndParts() { + @Test("undoDelete names a definition in its own section only") + func undoDeleteStaysInItsSection() { let manager = makeManagerWithSchema() let emailColumn = manager.workingColumns[1] manager.deleteColumn(id: emailColumn.id) - manager.undoDelete(for: .ddl, at: 1) - manager.undoDelete(for: .parts, at: 1) + manager.undoDelete(section: .indexes, id: emailColumn.id) + manager.undoDelete(section: .checkConstraints, id: emailColumn.id) #expect(manager.deleteInsertState(for: 1, tab: .columns).isDeleted) } + + @Test("undoDelete leaves the undo stack alone, and the delete it skipped undoes to nothing") + func undoDeleteLeavesTheStack() { + let manager = makeManagerWithSchema() + let emailColumn = manager.workingColumns[1] + manager.deleteColumn(id: emailColumn.id) + + manager.undoDelete(section: .columns, id: emailColumn.id) + #expect(manager.canUndo) + + manager.undo() + #expect(!manager.deleteInsertState(for: 1, tab: .columns).isDeleted) + #expect(!manager.hasChanges) + } } diff --git a/apple/TableProTests/Views/Structure/StructureFooterPolicyTests.swift b/apple/TableProTests/Views/Structure/StructureFooterPolicyTests.swift index d2c3e38272..42ad20122a 100644 --- a/apple/TableProTests/Views/Structure/StructureFooterPolicyTests.swift +++ b/apple/TableProTests/Views/Structure/StructureFooterPolicyTests.swift @@ -5,134 +5,147 @@ import Foundation @testable import TablePro +import TableProPluginKit import Testing -/// The "+" and "-" under a structure list used to take their label, their enabled state and their -/// tooltip from three different switches, and only the Foreign Keys one asked about the object at -/// all. So a view offered an enabled "Add Column" over a statement PostgreSQL always refuses, with -/// nothing to explain it. (#2726) +// The +/- beneath each section takes its label from the policy and its state and help from the editor, so a view +// that PostgreSQL refuses an added column on dims the pair and says why (#2726). +@MainActor struct StructureFooterPolicyTests { - private func resolve( - tab: StructureTab, + private let suite: EphemeralDefaults + + init() throws { + suite = try EphemeralDefaults() + } + + private func makeEditor( kind: TableInfo.TableType, - matrix: StructureObjectEditMatrix = .postgreSQL, - canEditSchema: Bool = true, - hasSelection: Bool = true, - isSaving: Bool = false - ) -> StructureFooterCapability { - StructureFooterPolicy.resolve( - tab: tab, - canEditSchema: canEditSchema, - hasSelection: hasSelection, - isSaving: isSaving, - resolve: { operation in - StructureEditEligibility.resolve( - operation, - on: kind, - matrix: matrix, - engineAllows: true, - engineName: "PostgreSQL", - canEditSchema: canEditSchema - ) - } + type: DatabaseType = .postgresql, + isSessionBound: Bool = false + ) -> StructureDefinitionEditor { + let manager = StructureChangeManager() + manager.loadSchema( + tableName: "t", + columns: [ColumnInfo(name: "id", dataType: "integer", isNullable: false, isPrimaryKey: true)], + indexes: [IndexInfo(name: "t_pkey", columns: ["id"], isUnique: true, isPrimary: true, type: "BTREE")], + foreignKeys: [], + primaryKey: ["id"] ) + let editor = StructureDefinitionEditor( + manager: manager, + context: .existingTable( + connection: TestFixtures.makeConnection(type: type), + databaseName: "db", + schemaName: "public", + tableName: "t", + objectKind: kind, + isSessionBound: isSessionBound + ), + defaults: suite.defaults + ) + editor.refresh() + return editor } - @Test("A table offers both buttons on all four editable tabs") + // Selects the section's first row, staging a placeholder where the table has none. + private func selectFirst(_ section: StructureSection, in editor: StructureDefinitionEditor) { + if StructureGridEdits.entityIDs(in: section, manager: editor.manager).isEmpty { + StructureGridEdits.addRow(on: section, manager: editor.manager) + editor.refresh() + } + let id = StructureGridEdits.entityIDs(in: section, manager: editor.manager)[0] + editor.adapter(for: section).select(entity: id) + } + + @Test("A table offers both buttons in all four sections") func tableOffersEverything() { - for tab in [StructureTab.columns, .indexes, .foreignKeys, .checkConstraints] { - let capability = resolve(tab: tab, kind: .table) - #expect(capability.canAdd, "\(tab.rawValue)") - #expect(capability.canRemove, "\(tab.rawValue)") - #expect(capability.unavailableReason == nil, "\(tab.rawValue)") + let editor = makeEditor(kind: .table) + for section in StructureSection.allCases { + selectFirst(section, in: editor) + #expect(editor.canAdd(section), "\(section.rawValue)") + #expect(editor.canRemoveSelection(in: section), "\(section.rawValue)") + #expect(editor.refusal(adding: section) == nil, "\(section.rawValue)") } } - /// A save holds what is staged from the press until it ends, so the pair cannot stage more - /// meanwhile, and says why rather than going grey in silence. - @Test("A save in progress dims both buttons on every editable tab and says why") - func savingDimsThePair() { - for tab in [StructureTab.columns, .indexes, .foreignKeys, .checkConstraints] { - let capability = resolve(tab: tab, kind: .table, isSaving: true) - #expect(!capability.canAdd, "\(tab.rawValue)") - #expect(!capability.canRemove, "\(tab.rawValue)") - #expect(capability.isActive, "\(tab.rawValue)") - #expect(capability.unavailableReason == StructureFooterPolicy.savingReason, "\(tab.rawValue)") + // A save holds what is staged from the press until it ends, so the pair cannot stage more meanwhile. + @Test("A save in progress dims both buttons in every section and says why") + func savingDimsThePair() throws { + let editor = makeEditor(kind: .table) + selectFirst(.columns, in: editor) + editor.manager.stageTableComment("Orders") + _ = try #require(editor.manager.holdForSave()) + editor.refresh() + #expect(!editor.adapter(for: .columns).model.selection.isEmpty) + + for section in StructureSection.allCases { + #expect(!editor.canAdd(section), "\(section.rawValue)") + #expect(!editor.canRemoveSelection(in: section), "\(section.rawValue)") + #expect(editor.refusal(adding: section) == StructureFooterPolicy.savingReason, "\(section.rawValue)") } - #expect(!resolve(tab: .ddl, kind: .table, isSaving: true).isActive) } - /// Shown and dimmed, not hidden. The pair disappearing would read as "this tab has no columns", - /// and the tooltip is the only place the refusal can be stated. - @Test("A view keeps the Columns pair on screen, dimmed, with a reason") + // Dimmed, not hidden: the pair disappearing would read as "this table has no columns". + @Test("A view keeps the Columns pair, dimmed, with a reason") func viewDimsTheColumnsPair() { - let capability = resolve(tab: .columns, kind: .view) - #expect(!capability.canAdd) - #expect(!capability.canRemove) - #expect(capability.isActive) - #expect(capability.addLabel.isEmpty == false) - #expect(capability.unavailableReason?.isEmpty == false) + let editor = makeEditor(kind: .view) + selectFirst(.columns, in: editor) + + #expect(!editor.canAdd(.columns)) + #expect(!editor.canRemoveSelection(in: .columns)) + #expect(!StructureFooterPolicy.labels(for: .columns).add.isEmpty) + #expect(editor.refusal(adding: .columns)?.isEmpty == false) } @Test("A materialized view offers an index and refuses a column") - func materializedViewSplitsByTab() { - let indexes = resolve(tab: .indexes, kind: .materializedView) - #expect(indexes.canAdd) - #expect(indexes.canRemove) - #expect(indexes.unavailableReason == nil) - - let columns = resolve(tab: .columns, kind: .materializedView) - #expect(!columns.canAdd) - #expect(columns.unavailableReason?.isEmpty == false) + func materializedViewSplitsBySection() { + let editor = makeEditor(kind: .materializedView) + selectFirst(.indexes, in: editor) + #expect(editor.canAdd(.indexes)) + #expect(editor.canRemoveSelection(in: .indexes)) + #expect(editor.refusal(adding: .indexes) == nil) + + #expect(!editor.canAdd(.columns)) + #expect(editor.refusal(adding: .columns)?.isEmpty == false) } @Test("A foreign table offers a check constraint and refuses an index") - func foreignTableSplitsByTab() { - let checks = resolve(tab: .checkConstraints, kind: .foreignTable) - #expect(checks.canAdd) + func foreignTableSplitsBySection() { + let editor = makeEditor(kind: .foreignTable) + #expect(editor.canAdd(.checkConstraints)) - let indexes = resolve(tab: .indexes, kind: .foreignTable) - #expect(!indexes.canAdd) - #expect(indexes.unavailableReason?.isEmpty == false) + #expect(!editor.canAdd(.indexes)) + #expect(editor.refusal(adding: .indexes)?.isEmpty == false) } @Test("A foreign table refuses a foreign key, which is the constraint PostgreSQL rejects on one") func foreignTableRefusesForeignKeys() { - let keys = resolve(tab: .foreignKeys, kind: .foreignTable) - #expect(!keys.canAdd) - #expect(keys.unavailableReason?.isEmpty == false) + let editor = makeEditor(kind: .foreignTable) + #expect(!editor.canAdd(.foreignKeys)) + #expect(editor.refusal(adding: .foreignKeys)?.isEmpty == false) } - /// Today's behaviour, preserved on purpose: an engine that cannot edit structure at all hides the - /// pair rather than showing two dead buttons on every tab. - @Test("An engine that cannot edit structure hides the pair entirely") - func readOnlyEngineHidesThePair() { - let capability = resolve(tab: .columns, kind: .table, canEditSchema: false) - #expect(!capability.isActive) - #expect(capability.addLabel.isEmpty) - #expect(!capability.canAdd) - #expect(!capability.canRemove) + // The pair stays on screen beneath every section; a table whose structure cannot be edited dims it. + @Test("A table the connection cannot edit offers neither button") + func uneditableTableDimsThePair() { + let editor = makeEditor(kind: .table, isSessionBound: true) + for section in StructureSection.allCases { + selectFirst(section, in: editor) + #expect(!editor.canAdd(section), "\(section.rawValue)") + #expect(!editor.canRemoveSelection(in: section), "\(section.rawValue)") + } } @Test("Nothing selected dims Remove without dimming Add and without inventing a reason") func emptySelectionOnlyDimsRemove() { - let capability = resolve(tab: .columns, kind: .table, hasSelection: false) - #expect(capability.canAdd) - #expect(!capability.canRemove) - #expect(capability.unavailableReason == nil) - } - - @Test("The tabs with nothing to add have no operation and no pair") - func nonEditableTabsHaveNoPair() { - for tab in [StructureTab.options, .ddl, .parts, .triggers] { - #expect(StructureFooterPolicy.operation(forAdding: tab) == nil, "\(tab.rawValue)") - #expect(StructureFooterPolicy.operation(forRemoving: tab) == nil, "\(tab.rawValue)") - #expect(!resolve(tab: tab, kind: .table).isActive, "\(tab.rawValue)") - } + let editor = makeEditor(kind: .table) + #expect(editor.canAdd(.columns)) + #expect(!editor.canRemoveSelection(in: .columns)) + #expect(editor.refusal(adding: .columns) == nil) } - @Test("Each editable tab names the add and the drop of its own object") - func operationsMatchTheirTabs() { + @Test("Each section names the add and the drop of its own object") + func operationsMatchTheirSections() { #expect(StructureFooterPolicy.operation(forAdding: .columns) == .addColumn) #expect(StructureFooterPolicy.operation(forRemoving: .columns) == .dropColumn) #expect(StructureFooterPolicy.operation(forAdding: .indexes) == .addIndex) @@ -143,13 +156,25 @@ struct StructureFooterPolicyTests { #expect(StructureFooterPolicy.operation(forRemoving: .checkConstraints) == .dropCheckConstraint) } - /// An engine nobody has measured falls back to tables only, so every non-table dims rather than - /// offering an edit its server may refuse. + @Test("Each section labels both buttons, and no two sections share a label") + func everySectionHasItsOwnLabels() { + let labels = StructureSection.allCases.map { StructureFooterPolicy.labels(for: $0) } + for (section, label) in zip(StructureSection.allCases, labels) { + #expect(!label.add.isEmpty, "\(section.rawValue)") + #expect(!label.remove.isEmpty, "\(section.rawValue)") + #expect(label.add != label.remove, "\(section.rawValue)") + } + let adds = Set(labels.map { $0.add }) + let removes = Set(labels.map { $0.remove }) + #expect(adds.count == StructureSection.allCases.count) + #expect(removes.count == StructureSection.allCases.count) + } + + // An engine nobody has measured falls back to tables only, so every non-table dims. @Test("An uncurated engine dims the pair on a view") func uncuratedEngineDimsNonTables() { - let capability = resolve(tab: .columns, kind: .view, matrix: .tablesOnly) - #expect(!capability.canAdd) - #expect(capability.isActive) - #expect(capability.unavailableReason?.isEmpty == false) + let editor = makeEditor(kind: .view, type: .mysql) + #expect(!editor.canAdd(.columns)) + #expect(editor.refusal(adding: .columns)?.isEmpty == false) } } diff --git a/apple/TableProTests/Views/Structure/StructureGridAdapterAddRowTests.swift b/apple/TableProTests/Views/Structure/StructureGridAdapterAddRowTests.swift deleted file mode 100644 index a013912f7e..0000000000 --- a/apple/TableProTests/Views/Structure/StructureGridAdapterAddRowTests.swift +++ /dev/null @@ -1,204 +0,0 @@ -// -// StructureGridAdapterAddRowTests.swift -// TableProTests -// -// Add and Delete per sub-tab, the contract the footer +/- buttons, Cmd+Shift+N and the row menus share. -// - -import Foundation -@testable import TablePro -import TableProPluginKit -import Testing - -@MainActor -struct StructureGridAdapterAddRowTests { - private func makeAdapter( - selectedTab: StructureTab = .columns, - type: DatabaseType = .mysql, - objectKind: TableInfo.TableType = .table, - manager: StructureChangeManager = StructureChangeManager() - ) -> (StructureGridAdapter, StructureChangeManager) { - let adapter = StructureGridAdapter( - structureChangeManager: manager, - connection: TestFixtures.makeConnection(type: type), - tableName: "t", - databaseName: "db", - schemaName: nil, - objectKind: objectKind, - isSessionBound: false - ) - adapter.store.switchSubTab(to: selectedTab) - adapter.refresh() - return (adapter, manager) - } - - private func row(_ sourceIndex: Int) -> RowID { - StructureRowProvider.rowID(forSourceIndex: sourceIndex) - } - - private func counts(_ manager: StructureChangeManager) -> [Int] { - [manager.workingColumns.count, manager.workingIndexes.count, manager.workingForeignKeys.count] - } - - @Test("Columns sub-tab: Add Row appends a placeholder column") - func columnsTabAddsColumn() { - let (adapter, manager) = makeAdapter(selectedTab: .columns) - let before = manager.workingColumns.count - adapter.gridAddRow() - #expect(manager.workingColumns.count == before + 1) - } - - @Test("Indexes sub-tab: Add Row appends a placeholder index") - func indexesTabAddsIndex() { - let (adapter, manager) = makeAdapter(selectedTab: .indexes) - let before = manager.workingIndexes.count - adapter.gridAddRow() - #expect(manager.workingIndexes.count == before + 1) - } - - @Test("Foreign keys sub-tab: Add Row appends a placeholder foreign key") - func foreignKeysTabAddsForeignKey() { - let (adapter, manager) = makeAdapter(selectedTab: .foreignKeys) - let before = manager.workingForeignKeys.count - adapter.gridAddRow() - #expect(manager.workingForeignKeys.count == before + 1) - } - - @Test("Add Row does nothing on a sub-tab without a grid", arguments: [StructureTab.options, .ddl, .parts, .triggers]) - func gridlessTabAddsNothing(tab: StructureTab) { - let (adapter, manager) = makeAdapter(selectedTab: tab) - let before = counts(manager) - adapter.gridAddRow() - #expect(counts(manager) == before) - } - - @Test("Delete does nothing on a sub-tab without a grid", arguments: [StructureTab.options, .ddl, .parts, .triggers]) - func gridlessTabDeletesNothing(tab: StructureTab) { - let (adapter, manager) = makeAdapter(selectedTab: tab) - let before = counts(manager) - adapter.gridDeleteRows([row(0)]) - #expect(counts(manager) == before) - } - - // SQLite's curated snapshot overrides neither supportsAddIndex nor supportsDropIndex, so both default to true. - @Test("Indexes sub-tab on SQLite: Add Row appends an index") - func sqliteIndexesAddsAnIndex() { - let (adapter, manager) = makeAdapter(selectedTab: .indexes, type: .sqlite) - let before = manager.workingIndexes.count - adapter.gridAddRow() - #expect(manager.workingIndexes.count == before + 1) - } - - @Test("Columns sub-tab: Delete removes the column the row names") - func columnsTabDeleteRemovesColumn() { - let (adapter, manager) = makeAdapter(selectedTab: .columns) - adapter.gridAddRow() - let after = manager.workingColumns.count - #expect(after > 0) - adapter.gridDeleteRows([row(after - 1)]) - #expect(manager.workingColumns.count == after - 1) - } - - @Test("Indexes sub-tab: Delete removes the index the row names") - func indexesTabDeleteRemovesIndex() { - let (adapter, manager) = makeAdapter(selectedTab: .indexes) - adapter.gridAddRow() - let after = manager.workingIndexes.count - #expect(after > 0) - adapter.gridDeleteRows([row(after - 1)]) - #expect(manager.workingIndexes.count == after - 1) - } - - @Test("Foreign keys sub-tab: Delete removes the foreign key the row names") - func foreignKeysTabDeleteRemovesForeignKey() { - let (adapter, manager) = makeAdapter(selectedTab: .foreignKeys) - adapter.gridAddRow() - let after = manager.workingForeignKeys.count - #expect(after > 0) - adapter.gridDeleteRows([row(after - 1)]) - #expect(manager.workingForeignKeys.count == after - 1) - } - - @Test("Indexes sub-tab on SQLite: Delete removes an index") - func sqliteIndexesDeletesAnIndex() { - let manager = StructureChangeManager() - manager.addIndex(.placeholder()) - let (adapter, _) = makeAdapter(selectedTab: .indexes, type: .sqlite, manager: manager) - let before = manager.workingIndexes.count - - adapter.gridDeleteRows([row(before - 1)]) - - #expect(manager.workingIndexes.count == before - 1) - } - - @Test("Deleting the last row selects the row above it") - func deletingTheLastRowSelectsTheOneAbove() { - let manager = StructureChangeManager() - for _ in 0..<3 { - manager.addColumn(.placeholder()) - } - let (adapter, _) = makeAdapter(selectedTab: .columns, manager: manager) - adapter.model.selectRows([row(2)]) - - adapter.gridDeleteRows([row(2)]) - - #expect(manager.workingColumns.count == 2) - #expect(adapter.model.selectedRowIDs == [row(1)]) - } - - // MARK: - Per object kind - - // Cmd+Shift+N, the row menus and the footer all reach the adapter, so it refuses for itself (#2726). - @Test("A PostgreSQL view refuses an added column, whatever reaches the adapter") - func viewRefusesAnAddedColumn() { - let (adapter, manager) = makeAdapter(selectedTab: .columns, type: .postgresql, objectKind: .view) - adapter.gridAddRow() - #expect(manager.workingColumns.isEmpty) - #expect(!adapter.model.configuration.commands.contains(.addRow)) - } - - @Test("A PostgreSQL view refuses an added index and a materialized view accepts one") - func indexesFollowTheObjectKind() { - let (view, viewManager) = makeAdapter(selectedTab: .indexes, type: .postgresql, objectKind: .view) - view.gridAddRow() - #expect(viewManager.workingIndexes.isEmpty) - - let (matview, matviewManager) = makeAdapter( - selectedTab: .indexes, type: .postgresql, objectKind: .materializedView - ) - matview.gridAddRow() - #expect(matviewManager.workingIndexes.count == 1) - } - - @Test("A PostgreSQL materialized view refuses a foreign key") - func materializedViewRefusesAForeignKey() { - let (adapter, manager) = makeAdapter( - selectedTab: .foreignKeys, type: .postgresql, objectKind: .materializedView - ) - adapter.gridAddRow() - #expect(manager.workingForeignKeys.isEmpty) - } - - @Test("A view refuses to drop a column its own grid is showing") - func viewRefusesADroppedColumn() { - let manager = StructureChangeManager() - manager.addColumn(.placeholder()) - #expect(manager.workingColumns.count == 1) - let (view, _) = makeAdapter(selectedTab: .columns, type: .postgresql, objectKind: .view, manager: manager) - - view.gridDeleteRows([row(0)]) - - #expect(manager.workingColumns.count == 1) - #expect(!view.model.configuration.commands.contains(.deleteRows)) - } - - @Test("A system table refuses every add", arguments: [StructureTab.columns, .indexes, .foreignKeys, .checkConstraints]) - func systemTableRefusesEveryAdd(tab: StructureTab) { - let (adapter, manager) = makeAdapter(selectedTab: tab, type: .postgresql, objectKind: .systemTable) - adapter.gridAddRow() - #expect(manager.workingColumns.isEmpty) - #expect(manager.workingIndexes.isEmpty) - #expect(manager.workingForeignKeys.isEmpty) - #expect(manager.workingCheckConstraints.isEmpty) - } -} diff --git a/apple/TableProTests/Views/Structure/StructureGridAdapterInspectorTests.swift b/apple/TableProTests/Views/Structure/StructureGridAdapterInspectorTests.swift deleted file mode 100644 index 8137a54eb2..0000000000 --- a/apple/TableProTests/Views/Structure/StructureGridAdapterInspectorTests.swift +++ /dev/null @@ -1,278 +0,0 @@ -// -// StructureGridAdapterInspectorTests.swift -// TableProTests -// - -import Foundation -@testable import TablePro -import TableProPluginKit -import Testing - -@MainActor -struct StructureGridAdapterInspectorTests { - private func connection(_ type: DatabaseType = .mysql) -> DatabaseConnection { - DatabaseConnection( - name: "Test", - host: "localhost", - port: type == .postgresql ? 5_432 : 3_306, - database: "test", - username: "root", - type: type - ) - } - - private func loadSchema(into manager: StructureChangeManager) { - manager.loadSchema( - tableName: "users", - columns: [ - ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true, - defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil), - ColumnInfo(name: "email", dataType: "VARCHAR(255)", isNullable: true, isPrimaryKey: false, - defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil) - ], - indexes: [], - foreignKeys: [], - primaryKey: ["id"] - ) - } - - // The session owns the adapter in the app, and its search text is what filters the provider. - private func makeSession( - type: DatabaseType = .mysql, - objectKind: TableInfo.TableType = .table, - tab: StructureTab = .columns, - searchText: String = "" - ) -> StructureEditingSession { - let session = StructureEditingSession( - identity: "test.users", - connection: connection(type), - databaseName: "test", - schemaName: nil, - tableName: "users", - objectKind: objectKind - ) - loadSchema(into: session.changeManager) - session.selectedTab = tab - session.searchText = searchText - session.gridAdapter.refresh() - return session - } - - private func fieldIndex(_ adapter: StructureGridAdapter, _ field: StructureColumnField) throws -> Int { - try #require(adapter.provider.orderedColumnFields.firstIndex(of: field)) - } - - @Test("A row is read-only while a save holds the edits, and editable again once it lets go") - func heldSaveMakesTheRowReadOnly() throws { - let session = makeSession() - let manager = session.changeManager - let adapter = session.gridAdapter - var email = try #require(manager.workingColumns.first { $0.name == "email" }) - email.comment = "Contact" - manager.updateColumn(id: email.id, with: email) - adapter.refresh() - - let snapshot = try #require(manager.holdForSave()) - #expect(adapter.inspectorRow(atDisplayRow: 1)?.isEditable == false) - - manager.releaseHold(snapshot, written: false) - #expect(adapter.inspectorRow(atDisplayRow: 1)?.isEditable == true) - } - - // A hold changes no row, so the inspector kept the fields it built before the save and dropped what was typed. - @Test("A save hold and its release each rebuild the inspector") - func heldSaveRebuildsTheInspector() throws { - let fixture = CoordinatorFixture(connection: connection()) - let session = makeSession() - let manager = session.changeManager - let adapter = session.gridAdapter - adapter.coordinator = fixture.coordinator - var email = try #require(manager.workingColumns.first { $0.name == "email" }) - email.comment = "Contact" - manager.updateColumn(id: email.id, with: email) - adapter.refresh() - let beforeHold = fixture.coordinator.inspectorRowSourceRevision - - let snapshot = try #require(manager.holdForSave()) - adapter.refresh() - #expect(fixture.coordinator.inspectorRowSourceRevision == beforeHold + 1) - - manager.releaseHold(snapshot, written: false) - adapter.refresh() - #expect(fixture.coordinator.inspectorRowSourceRevision == beforeHold + 2) - } - - @Test("The published row describes the structure grid, not the data grid") - func publishedRowDescribesStructure() throws { - let session = makeSession() - let adapter = session.gridAdapter - let row = try #require(adapter.inspectorRow(atDisplayRow: 1)) - - #expect(row.fields.map(\.name) == adapter.provider.orderedColumnFields.map(\.displayName)) - #expect(row.fields.first?.value == "email") - } - - @Test("An inspector edit lands on the column the display row points at under a search") - func editResolvesFilteredDisplayRow() throws { - let session = makeSession(searchText: "email") - let adapter = session.gridAdapter - #expect(adapter.model.rowCount == 1) - - adapter.commitInspectorField( - displayRow: 0, - fieldIndex: try fieldIndex(adapter, .name), - value: "user_email" - ) - - #expect(session.changeManager.workingColumns[0].name == "id") - #expect(session.changeManager.workingColumns[1].name == "user_email") - } - - @Test("An inspector edit records a pending schema change") - func editRecordsPendingChange() throws { - let session = makeSession() - let adapter = session.gridAdapter - - adapter.commitInspectorField( - displayRow: 1, - fieldIndex: try fieldIndex(adapter, .type), - value: "TEXT" - ) - - #expect(session.changeManager.hasChanges) - #expect(session.changeManager.workingColumns[1].dataType == "TEXT") - } - - @Test("A flag field commits the value the dropdown offers") - func flagEditCommitsBooleanValue() throws { - let session = makeSession() - let adapter = session.gridAdapter - - adapter.commitInspectorField( - displayRow: 1, - fieldIndex: try fieldIndex(adapter, .nullable), - value: "NO" - ) - - #expect(session.changeManager.workingColumns[1].isNullable == false) - } - - // A Foreign Keys inspector built before a reference list arrived holds only "Loading…" until it rebuilds. - @Test("A reference list landing rebuilds the structure inspector") - func referenceListArrivalRebuildsInspector() { - let fixture = CoordinatorFixture(connection: connection()) - let session = makeSession(tab: .foreignKeys) - session.gridAdapter.coordinator = fixture.coordinator - let revision = fixture.coordinator.inspectorRowSourceRevision - - session.gridAdapter.referenceMenus.onListsChanged?() - - #expect(fixture.coordinator.inspectorRowSourceRevision == revision + 1) - } - - @Test("Before its first refresh the adapter publishes nothing") - func beforeRefreshPublishesNothing() { - let manager = StructureChangeManager() - loadSchema(into: manager) - let adapter = StructureGridAdapter( - structureChangeManager: manager, - connection: connection(), - tableName: "users", - databaseName: "test", - schemaName: nil, - objectKind: .table, - isSessionBound: false - ) - - #expect(adapter.inspectorRow(atDisplayRow: 0) == nil) - } - - @Test("The new-table grid publishes its own rows and takes edits") - func createTableAdapterPublishesRows() throws { - let manager = StructureChangeManager() - manager.addNewColumn() - let adapter = CreateTableGridAdapter(structureChangeManager: manager, connection: connection()) - adapter.refresh() - - let row = try #require(adapter.inspectorRow(atDisplayRow: 0)) - #expect(row.isEditable) - - let nameIndex = try #require(adapter.provider.orderedColumnFields.firstIndex(of: .name)) - adapter.commitInspectorField(displayRow: 0, fieldIndex: nameIndex, value: "sku") - #expect(manager.workingColumns[0].name == "sku") - } - - // MARK: - A kind that locks fields - - // The grid locked a materialized view's Type, Nullable and Default while the inspector offered all three. - private func makeMaterializedView(tab: StructureTab = .columns) -> StructureEditingSession { - makeSession(type: .postgresql, objectKind: .materializedView, tab: tab) - } - - @Test("A materialized view's inspector locks the fields its grid locks") - func materializedViewInspectorLocksFields() throws { - let adapter = makeMaterializedView().gridAdapter - let row = try #require(adapter.inspectorRow(atDisplayRow: 1)) - - #expect(row.isEditable) - #expect(row.fields[try fieldIndex(adapter, .name)].isEditable) - #expect(row.fields[try fieldIndex(adapter, .comment)].isEditable) - #expect(!row.fields[try fieldIndex(adapter, .type)].isEditable) - #expect(!row.fields[try fieldIndex(adapter, .nullable)].isEditable) - #expect(!row.fields[try fieldIndex(adapter, .defaultValue)].isEditable) - } - - @Test("The grid locks the same headings the inspector locks") - func gridLocksTheSameHeadings() throws { - let adapter = makeMaterializedView().gridAdapter - let headings = adapter.provider.columns - let locked = adapter.model.configuration.lockedColumns - - #expect(locked.contains(headings[try fieldIndex(adapter, .type)])) - #expect(locked.contains(headings[try fieldIndex(adapter, .nullable)])) - #expect(!locked.contains(headings[try fieldIndex(adapter, .name)])) - } - - @Test("A locked field refuses the edit even when it arrives through the inspector") - func lockedFieldRefusesTheCommit() throws { - let session = makeMaterializedView() - let adapter = session.gridAdapter - - adapter.commitInspectorField(displayRow: 1, fieldIndex: try fieldIndex(adapter, .type), value: "TEXT") - adapter.commitInspectorField(displayRow: 1, fieldIndex: try fieldIndex(adapter, .defaultValue), value: "'x'") - - #expect(!session.changeManager.hasChanges) - #expect(session.changeManager.workingColumns[1].dataType == "VARCHAR(255)") - #expect(session.changeManager.workingColumns[1].defaultValue == nil) - } - - @Test("An unlocked field on the same object still takes the edit") - func unlockedFieldTakesTheCommit() throws { - let session = makeMaterializedView() - let adapter = session.gridAdapter - - adapter.commitInspectorField(displayRow: 1, fieldIndex: try fieldIndex(adapter, .name), value: "mail") - - #expect(session.changeManager.workingColumns[1].name == "mail") - } - - @Test("A table locks nothing, so every field stays open") - func tableLocksNothing() { - let adapter = makeSession().gridAdapter - #expect(adapter.lockedFieldIndices.isEmpty) - #expect(adapter.model.configuration.lockedColumns.isEmpty) - } - - @Test("The row commands offer Delete only where the object accepts the drop") - func rowCommandsFollowTheGate() { - let columns = makeMaterializedView(tab: .columns).gridAdapter - #expect(!columns.canStageRemoveForSelectedTab) - #expect(!columns.canStageAddForSelectedTab) - #expect(columns.model.configuration.commands.isDisjoint(with: [.deleteRows, .addRow, .duplicateRows])) - - let indexes = makeMaterializedView(tab: .indexes).gridAdapter - #expect(indexes.canStageRemoveForSelectedTab) - #expect(indexes.canStageAddForSelectedTab) - #expect(indexes.model.configuration.commands.isSuperset(of: [.deleteRows, .addRow, .duplicateRows])) - } -} diff --git a/apple/TableProTests/Views/Structure/StructureGridStateStoreTests.swift b/apple/TableProTests/Views/Structure/StructureGridStateStoreTests.swift index ee853acd11..73bca4489b 100644 --- a/apple/TableProTests/Views/Structure/StructureGridStateStoreTests.swift +++ b/apple/TableProTests/Views/Structure/StructureGridStateStoreTests.swift @@ -12,15 +12,15 @@ import Testing @MainActor struct StructureGridStateStoreTests { - private func makeSession(table: String = "orders_\(UUID().uuidString.prefix(8))") -> StructureEditingSession { - let session = StructureEditingSession( - identity: "shop.\(table)", - connection: TestFixtures.makeConnection(database: "shop", type: .mysql), - databaseName: "shop", - schemaName: nil, - tableName: table - ) - session.changeManager.loadSchema( + private let suite: EphemeralDefaults + + init() throws { + suite = try EphemeralDefaults() + } + + private func makeEditor(table: String = "orders_\(UUID().uuidString.prefix(8))") -> StructureDefinitionEditor { + let manager = StructureChangeManager() + manager.loadSchema( tableName: table, columns: [ ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true), @@ -30,108 +30,172 @@ struct StructureGridStateStoreTests { foreignKeys: [], primaryKey: ["id"] ) - session.gridAdapter.refresh() - return session + let editor = StructureDefinitionEditor( + manager: manager, + context: .existingTable( + connection: TestFixtures.makeConnection(database: "shop", type: .mysql), + databaseName: "shop", + schemaName: nil, + tableName: table, + objectKind: .table, + isSessionBound: false + ), + defaults: suite.defaults + ) + editor.refresh() + return editor } - private func nameHeading(_ session: StructureEditingSession) throws -> String { - try #require(session.gridModel.columns.column(at: 0)).name + private func nameHeading(_ model: DataGridModel) throws -> String { + try #require(model.columns.column(at: 0)).name } - @Test("A column resized in the Structure grid stays on the session and never reaches the table's saved layout") + @Test("A column resized in a section's grid stays on its store and never reaches the table's saved layout") func resizeNeverWritesTheTableLayout() throws { - let session = makeSession() - let model = session.gridModel - let name = try nameHeading(session) + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + let model = adapter.model + let name = try nameHeading(model) let tableKey = ColumnLayoutTableKey( - connectionId: session.connection.id, + connectionId: editor.context.connection.id, databaseName: "shop", schemaName: nil, - tableName: session.tableName + tableName: editor.context.tableName ) model.columnGeometryDidChange(widths: [name: 260], order: nil) #expect(model.store.layoutScope == .ephemeral) - #expect(session.columnLayouts[.columns]?.columnWidths[name] == 260) - #expect(model.store.columnLayout.columnWidths[name] == 260) + #expect(adapter.store.columnLayout.columnWidths[name] == 260) #expect(FileColumnLayoutPersister.shared.load(for: tableKey) == nil) } - @Test("Each sub-tab keeps its own column layout") - func layoutIsPerSubTab() throws { - let session = makeSession() - let model = session.gridModel - let name = try nameHeading(session) - model.columnGeometryDidChange(widths: [name: 260], order: nil) + @Test("Each section keeps its own column layout") + func layoutIsPerSection() throws { + let editor = makeEditor() + let columns = editor.adapter(for: .columns) + let indexes = editor.adapter(for: .indexes) + let name = try nameHeading(columns.model) - session.selectedTab = .indexes - session.gridAdapter.refresh() - #expect(model.store.columnLayout.columnWidths[name] == nil) - model.columnGeometryDidChange(widths: [name: 180], order: nil) + columns.model.columnGeometryDidChange(widths: [name: 260], order: nil) + #expect(indexes.store.columnLayout.columnWidths[name] == nil) + indexes.model.columnGeometryDidChange(widths: [name: 180], order: nil) - session.selectedTab = .columns - session.gridAdapter.refresh() - #expect(model.store.columnLayout.columnWidths[name] == 260) - #expect(session.columnLayouts[.indexes]?.columnWidths[name] == 180) + #expect(columns.store.columnLayout.columnWidths[name] == 260) + #expect(indexes.store.columnLayout.columnWidths[name] == 180) } - @Test("Another sub-tab starts with no value filter and no selection") - func subTabSwitchStartsTheViewOver() throws { - let session = makeSession() - let model = session.gridModel - let name = try nameHeading(session) + @Test("A value filter in one section leaves the others unfiltered") + func valueFilterIsPerSection() throws { + let editor = makeEditor() + let columns = editor.adapter(for: .columns) + let indexes = editor.adapter(for: .indexes) + let name = try nameHeading(columns.model) var filter = GridValueFilterState() filter.set(ColumnValueFilter(selectedValues: ["id"], includesNull: false), columnName: name, forColumn: 0) - model.setValueFilter(filter) - model.selectRows([StructureRowProvider.rowID(forSourceIndex: 0)]) - #expect(model.store.valueFilter.isActive) - #expect(!model.selection.isEmpty) - - session.selectedTab = .indexes - session.gridAdapter.refresh() - - #expect(!model.store.valueFilter.isActive) - #expect(model.store.selectionSnapshot.isEmpty) - #expect(model.selection.isEmpty) - #expect(model.rowCount == 1) + + columns.model.setValueFilter(filter) + editor.refresh() + + #expect(columns.store.valueFilter.isActive) + #expect(columns.model.rowCount == 1) + #expect(!indexes.store.valueFilter.isActive) + #expect(indexes.model.rowCount == 1) } - @Test("The sort lives on the session, which is what orders the provider's rows") - func sortLivesOnTheSession() { - let session = makeSession() - let model = session.gridModel + @Test("The sort lives on the section's store, which is what orders the provider's rows") + func sortLivesOnTheSectionStore() { + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + let model = adapter.model let descending = SortState(columns: [SortColumn(columnIndex: 0, direction: .descending)], source: .user) model.setSort(descending) - #expect(session.sortState == descending) - #expect(session.sortDescriptor?.column == 0) - #expect(session.sortDescriptor?.ascending == false) + #expect(adapter.store.sortState == descending) #expect(model.rows.rows.compactMap { $0.values.first?.asText } == ["total", "id"]) + #expect(editor.adapter(for: .indexes).store.sortState.columns.isEmpty) + } + + @Test("A sort survives a refresh of the definitions") + func sortSurvivesRefresh() { + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + adapter.model.setSort(SortState(columns: [SortColumn(columnIndex: 0, direction: .descending)], source: .user)) + + var amount = EditableColumnDefinition.placeholder() + amount.name = "amount" + amount.dataType = "INT" + editor.manager.addColumn(amount) + editor.refresh() + + #expect(adapter.model.rows.rows.compactMap { $0.values.first?.asText } == ["total", "id", "amount"]) } // The provider sorts by one key, so a secondary key the header appended is dropped (changed on purpose). @Test("A secondary sort key is dropped, since the provider sorts by one column") func secondarySortKeyIsDropped() { - let session = makeSession() - let model = session.gridModel + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) let primary = SortColumn(columnIndex: 1, direction: .ascending) - model.setSort(SortState(columns: [primary, SortColumn(columnIndex: 0, direction: .descending)], source: .user)) + adapter.model.setSort(SortState(columns: [primary, SortColumn(columnIndex: 0, direction: .descending)], source: .user)) - #expect(session.sortState.columns == [primary]) - #expect(model.store.sortState.columns == [primary]) + #expect(adapter.store.sortState.columns == [primary]) + #expect(adapter.model.store.sortState.columns == [primary]) } - @Test("Create Table's grid is ephemeral too") + @Test("Create Table's grids are ephemeral too") func createTableIsEphemeral() { - let adapter = CreateTableGridAdapter( - structureChangeManager: StructureChangeManager(), - connection: TestFixtures.makeConnection(type: .postgresql) + let editor = StructureDefinitionEditor( + manager: StructureChangeManager(), + context: .newTable(connection: TestFixtures.makeConnection(type: .postgresql), databaseName: "shop", schemaName: "public"), + defaults: suite.defaults ) - adapter.refresh() + editor.refresh() + + for section in StructureSection.allCases { + #expect(editor.adapter(for: section).model.store.layoutScope == .ephemeral, "\(section.rawValue)") + } + } + + // MARK: - Hidden columns + + @Test("A section starts with its lean default columns hidden") + func defaultHiddenColumns() { + let columns = StructureGridStateStore(section: .columns, defaults: suite.defaults) + #expect(columns.columnLayout.hiddenColumns == StructureGridStateStore.defaultHiddenColumns(.columns)) + #expect(columns.columnLayout.hiddenColumns.contains(StructureColumnField.collation.displayName)) + #expect(!columns.columnLayout.hiddenColumns.contains(StructureColumnField.name.displayName)) + #expect(StructureGridStateStore(section: .indexes, defaults: suite.defaults).columnLayout.hiddenColumns.isEmpty) + } + + @Test("Columns a person hides stay hidden for the next table, per section") + func hiddenColumnsPersistPerSection() { + let comment = StructureColumnField.comment.displayName + let store = StructureGridStateStore(section: .columns, defaults: suite.defaults) + store.columnLayout.hiddenColumns = [comment] + + #expect(StructureGridStateStore(section: .columns, defaults: suite.defaults).columnLayout.hiddenColumns == [comment]) + #expect( + StructureGridStateStore(section: .foreignKeys, defaults: suite.defaults).columnLayout.hiddenColumns + == StructureGridStateStore.defaultHiddenColumns(.foreignKeys) + ) + } + + @Test("Showing every column is kept too, so the defaults do not come back") + func showingEveryColumnPersists() { + let store = StructureGridStateStore(section: .columns, defaults: suite.defaults) + store.columnLayout.hiddenColumns = [] + + #expect(StructureGridStateStore(section: .columns, defaults: suite.defaults).columnLayout.hiddenColumns.isEmpty) + } + + @Test("A width change writes nothing to the defaults") + func widthsAreNotPersisted() { + let store = StructureGridStateStore(section: .indexes, defaults: suite.defaults) + store.columnLayout.columnWidths["Name"] = 200 - #expect(adapter.model.store.layoutScope == .ephemeral) + #expect(suite.defaults.object(forKey: StructureGridStateStore.hiddenColumnsKey(.indexes)) == nil) } } diff --git a/apple/TableProTests/Views/Structure/StructureGridValueFilterEditTests.swift b/apple/TableProTests/Views/Structure/StructureGridValueFilterEditTests.swift index 38612e4914..7e341d2510 100644 --- a/apple/TableProTests/Views/Structure/StructureGridValueFilterEditTests.swift +++ b/apple/TableProTests/Views/Structure/StructureGridValueFilterEditTests.swift @@ -3,7 +3,7 @@ // TableProTests // // D2: under the grid's value filter the Structure and Create Table grids mapped a display row to the wrong -// definition. Every edit now names its row by RowID, the definition's index in the working list. +// definition. Every edit now names its row by RowID, which the section's adapter mints per definition. // import Foundation @@ -14,16 +14,15 @@ import Testing @MainActor struct StructureGridValueFilterEditTests { private let columnNames = ["id", "email", "created_at"] + private let suite: EphemeralDefaults - private func makeSession() -> StructureEditingSession { - let session = StructureEditingSession( - identity: "shop.users", - connection: TestFixtures.makeConnection(database: "shop", type: .mysql), - databaseName: "shop", - schemaName: nil, - tableName: "users" - ) - session.changeManager.loadSchema( + init() throws { + suite = try EphemeralDefaults() + } + + private func makeEditor() -> StructureDefinitionEditor { + let manager = StructureChangeManager() + manager.loadSchema( tableName: "users", columns: columnNames.map { ColumnInfo(name: $0, dataType: "VARCHAR(64)", isNullable: true, isPrimaryKey: false) @@ -32,8 +31,30 @@ struct StructureGridValueFilterEditTests { foreignKeys: [], primaryKey: [] ) - session.gridAdapter.refresh() - return session + let editor = StructureDefinitionEditor( + manager: manager, + context: .existingTable( + connection: TestFixtures.makeConnection(database: "shop", type: .mysql), + databaseName: "shop", + schemaName: nil, + tableName: "users", + objectKind: .table, + isSessionBound: false + ), + defaults: suite.defaults + ) + editor.refresh() + return editor + } + + private func makeCreateTableEditor(_ manager: StructureChangeManager) -> StructureDefinitionEditor { + let editor = StructureDefinitionEditor( + manager: manager, + context: .newTable(connection: TestFixtures.makeConnection(type: .postgresql), databaseName: "shop", schemaName: "public"), + defaults: suite.defaults + ) + editor.refresh() + return editor } // Keeps only the rows whose Name is `value`. @@ -52,40 +73,43 @@ struct StructureGridValueFilterEditTests { @Test("A Structure edit on the one row a value filter leaves lands on that column") func structureEditUnderFilter() throws { - let session = makeSession() - let model = session.gridModel + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + let model = adapter.model try filterName(to: "created_at", on: model) try #require(model.rowCount == 1) - #expect(model.rowID(at: 0) == StructureRowProvider.rowID(forSourceIndex: 2)) + #expect(model.rowID(at: 0) == adapter.rowID(forEntity: editor.manager.workingColumns[2].id)) #expect(try commitName("created_on", atDisplayRow: 0, on: model)) - #expect(session.changeManager.workingColumns.map(\.name) == ["id", "email", "created_on"]) + #expect(editor.manager.workingColumns.map(\.name) == ["id", "email", "created_on"]) } @Test("A Structure inspector edit under a value filter lands on the row the grid shows") func structureInspectorEditUnderFilter() throws { - let session = makeSession() - let adapter = session.gridAdapter - try filterName(to: "email", on: session.gridModel) + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + try filterName(to: "email", on: adapter.model) + let shown = try #require(adapter.model.rowID(at: 0)) + let id = try #require(adapter.entityID(of: shown)) let nameField = try #require(adapter.provider.orderedColumnFields.firstIndex(of: .name)) - #expect(adapter.inspectorRow(atDisplayRow: 0)?.fields.first?.value == "email") - adapter.commitInspectorField(displayRow: 0, fieldIndex: nameField, value: "mail") + #expect(editor.inspectorForm(for: .columns, id: id)?.name == "email") + editor.commitInspectorField(section: .columns, id: id, field: nameField, value: "mail") - #expect(session.changeManager.workingColumns.map(\.name) == ["id", "mail", "created_at"]) + #expect(editor.manager.workingColumns.map(\.name) == ["id", "mail", "created_at"]) } @Test("A Structure delete under a value filter marks the column the grid shows") func structureDeleteUnderFilter() throws { - let session = makeSession() - let model = session.gridModel - try filterName(to: "email", on: model) - let shown = try #require(model.rowID(at: 0)) + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + try filterName(to: "email", on: adapter.model) + let shown = try #require(adapter.model.rowID(at: 0)) - session.gridAdapter.gridDeleteRows([shown]) + adapter.gridDeleteRows([shown]) - let manager = session.changeManager + let manager = editor.manager #expect(manager.deleteInsertState(for: 1, tab: .columns).isDeleted) #expect(!manager.deleteInsertState(for: 0, tab: .columns).isDeleted) #expect(!manager.deleteInsertState(for: 2, tab: .columns).isDeleted) @@ -93,23 +117,24 @@ struct StructureGridValueFilterEditTests { @Test("A Structure edit under a sort and a value filter still lands on the row it names") func structureEditUnderSortAndFilter() throws { - let session = makeSession() - let model = session.gridModel + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + let model = adapter.model model.setSort(SortState(columns: [SortColumn(columnIndex: 0, direction: .ascending)], source: .user)) #expect(model.rows.rows.compactMap { $0.values.first?.asText } == ["created_at", "email", "id"]) try filterName(to: "id", on: model) - #expect(model.rowID(at: 0) == StructureRowProvider.rowID(forSourceIndex: 0)) + #expect(model.rowID(at: 0) == adapter.rowID(forEntity: editor.manager.workingColumns[0].id)) #expect(try commitName("user_id", atDisplayRow: 0, on: model)) - #expect(session.changeManager.workingColumns.map(\.name) == ["user_id", "email", "created_at"]) + #expect(editor.manager.workingColumns.map(\.name) == ["user_id", "email", "created_at"]) } // A value filter rearranges the list, so the drag that moves a column is withdrawn (changed on purpose). @Test("A value filter withdraws the column move") func valueFilterWithdrawsTheColumnMove() throws { - let session = makeSession() - let model = session.gridModel + let editor = makeEditor() + let model = editor.adapter(for: .columns).model try filterName(to: "email", on: model) #expect(!model.configuration.rowReorder.isEnabled) @@ -125,9 +150,8 @@ struct StructureGridValueFilterEditTests { column.dataType = "TEXT" manager.addColumn(column) } - let adapter = CreateTableGridAdapter(structureChangeManager: manager, connection: TestFixtures.makeConnection(type: .postgresql)) - adapter.refresh() - let model = adapter.model + let editor = makeCreateTableEditor(manager) + let model = editor.adapter(for: .columns).model try filterName(to: "email", on: model) try #require(model.rowCount == 1) @@ -144,8 +168,8 @@ struct StructureGridValueFilterEditTests { column.name = name manager.addColumn(column) } - let adapter = CreateTableGridAdapter(structureChangeManager: manager, connection: TestFixtures.makeConnection(type: .postgresql)) - adapter.refresh() + let editor = makeCreateTableEditor(manager) + let adapter = editor.adapter(for: .columns) try filterName(to: "created_at", on: adapter.model) let shown = try #require(adapter.model.rowID(at: 0)) diff --git a/apple/TableProTests/Views/Structure/StructureIndexTypeMenuTests.swift b/apple/TableProTests/Views/Structure/StructureIndexTypeMenuTests.swift index 3855217c90..6d4b813901 100644 --- a/apple/TableProTests/Views/Structure/StructureIndexTypeMenuTests.swift +++ b/apple/TableProTests/Views/Structure/StructureIndexTypeMenuTests.swift @@ -13,6 +13,12 @@ import Testing @MainActor struct StructureIndexTypeMenuTests { + private let suite: EphemeralDefaults + + init() throws { + suite = try EphemeralDefaults() + } + private func connection() -> DatabaseConnection { DatabaseConnection(name: "Test", host: "localhost", port: 5_432, database: "test", username: "u", type: .postgresql) } @@ -31,34 +37,30 @@ struct StructureIndexTypeMenuTests { return manager } - private func structureAdapter(_ manager: StructureChangeManager) -> StructureGridAdapter { - let adapter = StructureGridAdapter( - structureChangeManager: manager, - connection: connection(), - tableName: "items", - databaseName: "test", - schemaName: nil, - objectKind: .table, - isSessionBound: false - ) - adapter.store.switchSubTab(to: .indexes) - adapter.refresh() - return adapter + private func makeEditor(_ manager: StructureChangeManager, creatingTable: Bool = false) -> StructureDefinitionEditor { + let context: StructureEditingContext = creatingTable + ? .newTable(connection: connection(), databaseName: "test", schemaName: nil) + : .existingTable( + connection: connection(), + databaseName: "test", + schemaName: nil, + tableName: "items", + objectKind: .table, + isSessionBound: false + ) + let editor = StructureDefinitionEditor(manager: manager, context: context, defaults: suite.defaults) + editor.refresh() + return editor } private func column(_ model: DataGridModel, dataIndex: Int) throws -> DataGridColumn { try #require(model.columns.columns.first { $0.dataIndex == dataIndex }) } - private func options( - _ source: any DataGridDataSource, - model: DataGridModel, - sourceIndex: Int, - dataIndex: Int - ) throws -> [String]? { - source.gridMenuOptions( - for: StructureRowProvider.rowID(forSourceIndex: sourceIndex), - column: try column(model, dataIndex: dataIndex) + private func options(_ adapter: StructureSectionAdapter, sourceIndex: Int, dataIndex: Int) throws -> [String]? { + adapter.gridMenuOptions( + for: adapter.rowID(forEntity: adapter.manager.workingIndexes[sourceIndex].id), + column: try column(adapter.model, dataIndex: dataIndex) )?.compactMap(\.sql) } @@ -66,30 +68,30 @@ struct StructureIndexTypeMenuTests { @Test("A bloom row offers BLOOM after the known types, and a b-tree row offers the known types alone") func rowOffersItsOwnType() throws { - let adapter = structureAdapter(manager(indexTypes: ["bloom", "btree"])) + let editor = makeEditor(manager(indexTypes: ["bloom", "btree"])) + let adapter = editor.adapter(for: .indexes) let typeColumn = StructureRowProvider.indexTypeColumn - #expect(try options(adapter, model: adapter.model, sourceIndex: 0, dataIndex: typeColumn) == Self.knownNames + ["BLOOM"]) - #expect(try options(adapter, model: adapter.model, sourceIndex: 1, dataIndex: typeColumn) == Self.knownNames) - #expect(try options(adapter, model: adapter.model, sourceIndex: 0, dataIndex: 3) == nil) + #expect(try options(adapter, sourceIndex: 0, dataIndex: typeColumn) == Self.knownNames + ["BLOOM"]) + #expect(try options(adapter, sourceIndex: 1, dataIndex: typeColumn) == Self.knownNames) + #expect(try options(adapter, sourceIndex: 0, dataIndex: 3) == nil) } - @Test("The inspector's Type picker for a CLUSTERED row holds the value it shows") - func inspectorPickerHoldsTheRowType() throws { - let adapter = structureAdapter(manager(indexTypes: ["CLUSTERED"])) - let row = try #require(adapter.inspectorRow(atDisplayRow: 0)) - let typeField = row.fields[StructureRowProvider.indexTypeColumn] + @Test("The inspector's Type menu for a CLUSTERED row holds the value it shows") + func inspectorMenuHoldsTheRowType() throws { + let editor = makeEditor(manager(indexTypes: ["CLUSTERED"])) + let form = try #require(editor.inspectorForm(for: .indexes, id: editor.manager.workingIndexes[0].id)) + let typeField = form.fields[StructureRowProvider.indexTypeColumn] #expect(typeField.value == "CLUSTERED") - #expect(typeField.editor == .enumPicker(values: Self.knownNames + ["CLUSTERED"])) + #expect(typeField.kind == .menu(GridMenuOption.values(Self.knownNames + ["CLUSTERED"]))) } @Test("The new-table grid offers a pasted index its own type the same way") func createTableOffersItsOwnType() throws { - let adapter = CreateTableGridAdapter(structureChangeManager: manager(indexTypes: ["hnsw"]), connection: connection()) - adapter.select(.indexes) + let editor = makeEditor(manager(indexTypes: ["hnsw"]), creatingTable: true) - #expect(try options(adapter, model: adapter.model, sourceIndex: 0, dataIndex: StructureRowProvider.indexTypeColumn) + #expect(try options(editor.adapter(for: .indexes), sourceIndex: 0, dataIndex: StructureRowProvider.indexTypeColumn) == Self.knownNames + ["HNSW"]) } diff --git a/apple/TableProTests/Views/Structure/StructureInspectorFormTests.swift b/apple/TableProTests/Views/Structure/StructureInspectorFormTests.swift new file mode 100644 index 0000000000..94ca1adf4c --- /dev/null +++ b/apple/TableProTests/Views/Structure/StructureInspectorFormTests.swift @@ -0,0 +1,228 @@ +// +// StructureInspectorFormTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +// The inspector's form for one definition, as the editor builds it and takes its edits back. +@MainActor +struct StructureInspectorFormTests { + private func makeSession( + type: DatabaseType = .mysql, + objectKind: TableInfo.TableType = .table + ) -> StructureEditingSession { + let session = StructureEditingSession( + identity: "test.users", + connection: TestFixtures.makeConnection(database: "test", type: type), + databaseName: "test", + schemaName: nil, + tableName: "users", + objectKind: objectKind + ) + session.changeManager.loadSchema( + tableName: "users", + columns: [ + ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil), + ColumnInfo(name: "email", dataType: "VARCHAR(255)", isNullable: true, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil) + ], + indexes: [], + foreignKeys: [], + primaryKey: ["id"] + ) + session.markBaselineAdopted() + session.editor.refresh() + return session + } + + private func emailID(_ session: StructureEditingSession) throws -> UUID { + try #require(session.changeManager.workingColumns.first { $0.name == "email" }).id + } + + private func emailForm(_ session: StructureEditingSession) throws -> StructureInspectorForm { + let id = try emailID(session) + return try #require(session.editor.inspectorForm(for: .columns, id: id)) + } + + private func field( + _ form: StructureInspectorForm, + _ field: StructureColumnField + ) throws -> StructureInspectorForm.Field { + try #require(form.fields.first { $0.label == field.displayName }) + } + + private func commit(_ session: StructureEditingSession, _ field: StructureColumnField, _ value: String) throws { + let form = try emailForm(session) + let index = try self.field(form, field).id + session.editor.commitInspectorField(section: .columns, id: form.entityID, field: index, value: value) + } + + // MARK: - Content + + @Test("The form names the definition and lists the grid's own fields") + func formDescribesTheDefinition() throws { + let session = makeSession() + let form = try emailForm(session) + + #expect(form.name == "email") + #expect(form.section == .columns) + #expect(form.isEditable) + #expect(!form.isNew) + #expect(!form.isDeleted) + let labels = form.fields.map { $0.label } + let headings = session.editor.adapter(for: .columns).provider.orderedColumnFields.map { $0.displayName } + #expect(labels == headings) + #expect(try field(form, .type).value == "VARCHAR(255)") + } + + @Test("Each field gets the control its grid cell has") + func controlsFollowTheGrid() throws { + let form = try emailForm(makeSession()) + + #expect(StructureInspectorControl.resolve(try field(form, .name)) == .text) + #expect(StructureInspectorControl.resolve(try field(form, .type)) == .typePicker) + #expect(StructureInspectorControl.resolve(try field(form, .nullable)) == .checkbox) + #expect(StructureInspectorControl.resolve(try field(form, .comment)) == .multiline) + } + + @Test("An edited field reads as modified and the others do not") + func editedFieldIsModified() throws { + let session = makeSession() + var email = try #require(session.changeManager.workingColumns.first { $0.name == "email" }) + email.dataType = "TEXT" + session.changeManager.updateColumn(id: email.id, with: email) + + let form = try emailForm(session) + #expect(try field(form, .type).isModified) + #expect(try !field(form, .name).isModified) + } + + // MARK: - Read-only + + @Test("A definition marked for deletion is read-only") + func deletedDefinitionIsReadOnly() throws { + let session = makeSession() + session.changeManager.deleteColumn(id: try emailID(session)) + + let form = try emailForm(session) + #expect(form.isDeleted) + #expect(!form.isEditable) + #expect(form.fields.allSatisfy { !$0.isEditable }) + } + + @Test("A save holding the edits makes the form read-only until it lets go") + func heldSaveIsReadOnly() throws { + let session = makeSession() + let manager = session.changeManager + var email = try #require(manager.workingColumns.first { $0.name == "email" }) + email.comment = "Contact" + manager.updateColumn(id: email.id, with: email) + + let snapshot = try #require(manager.holdForSave()) + #expect(try !emailForm(session).isEditable) + + manager.releaseHold(snapshot, written: false) + #expect(try emailForm(session).isEditable) + } + + @Test("Safe Mode makes the form read-only") + func safeModeIsReadOnly() throws { + let session = makeSession() + session.editor.isReadOnly = true + + let form = try emailForm(session) + #expect(!form.isEditable) + #expect(form.fields.allSatisfy { !$0.isEditable }) + } + + @Test("A materialized view locks the fields its grid locks") + func materializedViewLocksFields() throws { + let form = try emailForm(makeSession(type: .postgresql, objectKind: .materializedView)) + + #expect(form.isEditable) + #expect(try field(form, .name).isEditable) + #expect(try field(form, .comment).isEditable) + #expect(try !field(form, .type).isEditable) + #expect(try !field(form, .nullable).isEditable) + #expect(try !field(form, .defaultValue).isEditable) + } + + // MARK: - Commits + + @Test("A commit stages one schema change on the definition") + func commitStagesAChange() throws { + let session = makeSession() + try commit(session, .type, "TEXT") + + #expect(session.changeManager.hasChanges) + #expect(session.changeManager.canUndo) + #expect(session.changeManager.workingColumns[1].dataType == "TEXT") + } + + @Test("A commit lands on the definition it names, whatever the filter shows") + func commitFollowsIdentityUnderAFilter() throws { + let session = makeSession() + session.editor.searchText = "email" + try commit(session, .name, "user_email") + + #expect(session.changeManager.workingColumns[0].name == "id") + #expect(session.changeManager.workingColumns[1].name == "user_email") + } + + @Test("The checkbox writes the value the grid's menu offers") + func checkboxWritesTheGridValue() throws { + let session = makeSession() + try commit(session, .nullable, StructureRowProvider.booleanOptions[1]) + + #expect(session.changeManager.workingColumns[1].isNullable == false) + } + + @Test("A locked field refuses a commit from the inspector") + func lockedFieldRefusesTheCommit() throws { + let session = makeSession(type: .postgresql, objectKind: .materializedView) + try commit(session, .type, "TEXT") + try commit(session, .defaultValue, "'x'") + + #expect(!session.changeManager.hasChanges) + #expect(session.changeManager.workingColumns[1].dataType == "VARCHAR(255)") + #expect(session.changeManager.workingColumns[1].defaultValue == nil) + } + + @Test("A commit refreshes the editor, so the form is read again") + func commitBumpsTheRevision() throws { + let session = makeSession() + let revision = session.editor.revision + try commit(session, .comment, "Contact") + + #expect(session.editor.revision != revision) + #expect(try field(emailForm(session), .comment).value == "Contact") + } + + // MARK: - Create Table + + @Test("A drafted table's new column is editable and takes a name") + func createTableFormTakesEdits() throws { + let manager = StructureChangeManager() + manager.addNewColumn() + let editor = StructureDefinitionEditor( + manager: manager, + context: .newTable( + connection: TestFixtures.makeConnection(database: "test"), + databaseName: "test", + schemaName: nil + ) + ) + let id = try #require(manager.workingColumns.first).id + let form = try #require(editor.inspectorForm(for: .columns, id: id)) + #expect(form.isEditable) + + let name = try #require(form.fields.first { $0.label == StructureColumnField.name.displayName }) + editor.commitInspectorField(section: .columns, id: id, field: name.id, value: "sku") + #expect(manager.workingColumns[0].name == "sku") + } +} diff --git a/apple/TableProTests/Views/Structure/StructureInspectorPresentationTests.swift b/apple/TableProTests/Views/Structure/StructureInspectorPresentationTests.swift new file mode 100644 index 0000000000..baadf47579 --- /dev/null +++ b/apple/TableProTests/Views/Structure/StructureInspectorPresentationTests.swift @@ -0,0 +1,173 @@ +// +// StructureInspectorPresentationTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct StructureInspectorPresentationTests { + private func field( + _ kind: StructureInspectorForm.Field.Kind, + value: String? = "x", + isEditable: Bool = true + ) -> StructureInspectorForm.Field { + StructureInspectorForm.Field(id: 0, label: "Field", value: value, kind: kind, isModified: false, isEditable: isEditable) + } + + // MARK: - Subject + + @Test("A selected index is named as an index of its table") + func entitySubjectNamesItsKind() { + let subject = StructureInspectorPresentation.subject( + for: .entity(.indexes, UUID()), + definitionName: "idx_email", + tableName: "users" + ) + #expect(subject == .definition(name: "idx_email", section: .indexes, table: "users")) + #expect(subject.subtitle == String(format: String(localized: "Index of %@"), "users")) + } + + @Test("An unnamed definition in a table not yet named reads as new, with no table") + func newDefinitionInNewTable() { + let subject = StructureInspectorPresentation.subject( + for: .entity(.columns, UUID()), + definitionName: "", + tableName: "" + ) + #expect(subject.title == String(localized: "New Column")) + #expect(subject.subtitle == String(localized: "Column")) + } + + @Test("Nothing selected, or several, names the table") + func tableSubject() { + #expect(StructureInspectorPresentation.subject(for: .table, definitionName: nil, tableName: "users") + == .tableOnly(table: "users")) + #expect(StructureInspectorPresentation.subject(for: .several(.columns, 3), definitionName: nil, tableName: "users") + == .tableOnly(table: "users")) + } + + @Test("A table Create Table has not named yet reads as a new table") + func unnamedTable() { + let subject = StructureInspectorPresentation.subject(for: .table, definitionName: nil, tableName: "") + #expect(subject.title == String(localized: "New Table")) + } + + @Test("Several selected say how many of which kind") + func severalTitle() { + #expect(StructureInspectorPresentation.severalTitle(.columns, count: 3) + == String(format: String(localized: "%d Columns Selected"), 3)) + #expect(StructureInspectorPresentation.severalTitle(.foreignKeys, count: 2) + == String(format: String(localized: "%d Foreign Keys Selected"), 2)) + #expect(StructureInspectorPresentation.severalTitle(.checkConstraints, count: 4).contains("4")) + } + + @Test("Each control is identified by its section and field index") + func controlIdentifier() { + #expect(StructureInspectorPresentation.controlIdentifier(section: .foreignKeys, field: 3) + == "structure-inspector-foreignKeys-3") + } + + // MARK: - Controls + + @Test("Each editable field kind gets its control") + func editableControls() { + #expect(StructureInspectorControl.resolve(field(.text)) == .text) + #expect(StructureInspectorControl.resolve(field(.multiline)) == .multiline) + #expect(StructureInspectorControl.resolve(field(.toggle, value: "YES")) == .checkbox) + #expect(StructureInspectorControl.resolve(field(.typePicker)) == .typePicker) + + let menu = [GridMenuOption.value(title: "BTREE", sql: "BTREE"), .value(title: "HASH", sql: "HASH")] + guard case .popUp(let choices) = StructureInspectorControl.resolve(field(.menu(menu), value: "HASH")) else { + Issue.record("A menu field is a pop-up button") + return + } + #expect(choices.selectedTitle == "HASH") + + let combo = [GridMenuOption.value(title: "NULL", sql: "NULL"), .custom(title: "Custom…")] + guard case .comboBox = StructureInspectorControl.resolve(field(.combo(combo), value: "0")) else { + Issue.record("A combo field is a combo box") + return + } + } + + @Test("A field the form cannot edit is plain text, whatever its kind") + func readOnlyControls() { + #expect(StructureInspectorControl.resolve(field(.text, value: "email", isEditable: false)) == .readOnly("email")) + #expect(StructureInspectorControl.resolve(field(.toggle, value: "NO", isEditable: false)) == .readOnly("NO")) + #expect(StructureInspectorControl.resolve(field(.typePicker, value: nil, isEditable: false)) == .readOnly("")) + #expect(StructureInspectorControl.resolve(field(.combo([]), value: "''", isEditable: false)) == .readOnly("''")) + } + + @Test("A read-only menu field shows the title the menu gives its value") + func readOnlyMenuShowsTitle() { + let options = [GridMenuOption.value(title: "Empty string", sql: "''")] + #expect(StructureInspectorControl.resolve(field(.menu(options), value: "''", isEditable: false)) + == .readOnly("Empty string")) + } + + // MARK: - Choices + + @Test("The item whose SQL the field holds is selected, and picking an item writes its SQL") + func selectsBySQL() throws { + let choices = StructureInspectorChoices( + options: [.value(title: "Empty string", sql: "''"), .value(title: "NULL", sql: "NULL")], + current: "''" + ) + #expect(choices.selectedTitle == "Empty string") + let null = try #require(choices.items.first { $0.title == "NULL" }) + #expect(choices.value(of: null.id) == "NULL") + } + + @Test("A value the menu does not list is kept as the first item and selected") + func keepsUnlistedValue() { + let choices = StructureInspectorChoices(options: GridMenuOption.values(["utf8mb4", "latin1"]), current: "utf8") + #expect(choices.items.first?.title == "utf8") + #expect(choices.selectedTitle == "utf8") + #expect(choices.items.count == 3) + } + + @Test("A value that differs only in case selects the listed item rather than adding one") + func matchesCaseInsensitively() { + let choices = StructureInspectorChoices(options: GridMenuOption.values(["utf8mb4"]), current: "UTF8MB4") + #expect(choices.items.count == 1) + #expect(choices.selectedTitle == "utf8mb4") + } + + @Test("An empty value selects the item that clears, and nothing when there is none") + func emptyValue() { + let clearing = StructureInspectorChoices( + options: [.clear(title: "No Default"), .value(title: "NULL", sql: "NULL")], + current: "" + ) + #expect(clearing.selectedTitle == "No Default") + + let plain = StructureInspectorChoices(options: GridMenuOption.values(["CASCADE", "RESTRICT"]), current: "") + #expect(plain.selectedID == nil) + #expect(plain.items.count == 2) + } + + @Test("A section header starts a group and the custom entry is left to free text") + func groupsAndCustom() { + let choices = StructureInspectorChoices( + options: [ + .value(title: "NULL", sql: "NULL"), + .sectionHeader("Functions"), + .value(title: "CURRENT_TIMESTAMP", sql: "CURRENT_TIMESTAMP"), + .custom(title: "Custom…"), + ], + current: "NULL" + ) + #expect(choices.groups.map { $0.header } == [nil, "Functions"]) + #expect(choices.groups.map { group in group.items.map { $0.title } } == [["NULL"], ["CURRENT_TIMESTAMP"]]) + #expect(!choices.items.contains { $0.title == "Custom…" }) + } + + @Test("A header with nothing under it still shows, since it reports the menu's state") + func lonelyHeaderStays() { + let choices = StructureInspectorChoices(options: [.sectionHeader("Loading…")], current: "") + #expect(choices.groups.map { $0.header } == ["Loading…"]) + } +} diff --git a/apple/TableProTests/Views/Structure/StructureInspectorRowBuilderTests.swift b/apple/TableProTests/Views/Structure/StructureInspectorRowBuilderTests.swift deleted file mode 100644 index f9b86cebfb..0000000000 --- a/apple/TableProTests/Views/Structure/StructureInspectorRowBuilderTests.swift +++ /dev/null @@ -1,182 +0,0 @@ -// -// StructureInspectorRowBuilderTests.swift -// TableProTests -// - -import Foundation -@testable import TablePro -import TableProPluginKit -import Testing - -@MainActor -struct StructureInspectorRowBuilderTests { - private func loadedManager() -> StructureChangeManager { - let manager = StructureChangeManager() - manager.loadSchema( - tableName: "users", - columns: [ - ColumnInfo(name: "id", dataType: "INT", isNullable: false, isPrimaryKey: true, - defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil), - ColumnInfo(name: "email", dataType: "VARCHAR(255)", isNullable: true, isPrimaryKey: false, - defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil) - ], - indexes: [], - foreignKeys: [], - primaryKey: ["id"] - ) - return manager - } - - private func provider( - _ manager: StructureChangeManager, - tab: StructureTab = .columns, - filterText: String? = nil, - sortDescriptor: StructureSortDescriptor? = nil - ) -> StructureRowProvider { - StructureRowProvider( - changeManager: manager, - tab: tab, - databaseType: .mysql, - additionalFields: [.primaryKey], - serverSupport: .unrestricted, - filterText: filterText, - sortDescriptor: sortDescriptor - ) - } - - private func value(_ row: InspectorRow, named name: String) -> String? { - row.fields.first { $0.name == name }?.value - } - - @Test("Fields carry the structure row's own names and values") - func fieldsComeFromTheStructureRow() throws { - let manager = loadedManager() - let row = try #require(StructureInspectorRowBuilder.row( - atDisplayRow: 1, - tab: .columns, - provider: provider(manager), - canEditSchema: true - )) - - #expect(value(row, named: String(localized: "Name")) == "email") - #expect(value(row, named: String(localized: "Type")) == "VARCHAR(255)") - #expect(value(row, named: String(localized: "Nullable")) == "YES") - #expect(row.isEditable) - } - - @Test("A filtered grid resolves the display row to the right column") - func filteredDisplayRowResolvesToSourceEntity() throws { - let manager = loadedManager() - let row = try #require(StructureInspectorRowBuilder.row( - atDisplayRow: 0, - tab: .columns, - provider: provider(manager, filterText: "email"), - canEditSchema: true - )) - - #expect(value(row, named: String(localized: "Name")) == "email") - } - - @Test("A sorted grid resolves the display row to the right column") - func sortedDisplayRowResolvesToSourceEntity() throws { - let manager = loadedManager() - let row = try #require(StructureInspectorRowBuilder.row( - atDisplayRow: 0, - tab: .columns, - provider: provider(manager, sortDescriptor: StructureSortDescriptor(column: 0, ascending: false)), - canEditSchema: true - )) - - #expect(value(row, named: String(localized: "Name")) == "id") - } - - @Test("Flag fields offer the same YES and NO list the grid offers") - func flagFieldsUseDropdownEditors() throws { - let manager = loadedManager() - let row = try #require(StructureInspectorRowBuilder.row( - atDisplayRow: 0, - tab: .columns, - provider: provider(manager), - canEditSchema: true - )) - - let nullable = try #require(row.fields.first { $0.name == String(localized: "Nullable") }) - #expect(nullable.editor == .enumPicker(values: StructureRowProvider.booleanOptions)) - - let type = try #require(row.fields.first { $0.name == String(localized: "Type") }) - #expect(type.editor == .typePicker) - - let name = try #require(row.fields.first { $0.name == String(localized: "Name") }) - #expect(name.editor == .schemaText) - } - - @Test("An edited field is reported as modified") - func editedFieldIsMarkedModified() throws { - let manager = loadedManager() - var column = manager.workingColumns[1] - column.dataType = "TEXT" - manager.updateColumn(id: column.id, with: column) - - let row = try #require(StructureInspectorRowBuilder.row( - atDisplayRow: 1, - tab: .columns, - provider: provider(manager), - canEditSchema: true - )) - - let type = try #require(row.fields.first { $0.name == String(localized: "Type") }) - #expect(type.isModified) - let name = try #require(row.fields.first { $0.name == String(localized: "Name") }) - #expect(!name.isModified) - } - - @Test("A row pending deletion is read-only") - func pendingDeleteRowIsReadOnly() throws { - let manager = loadedManager() - manager.deleteColumn(id: manager.workingColumns[1].id) - - let row = try #require(StructureInspectorRowBuilder.row( - atDisplayRow: 1, - tab: .columns, - provider: provider(manager), - canEditSchema: true - )) - - #expect(!row.isEditable) - } - - @Test("An engine without schema editing gets a read-only row") - func withoutSchemaEditingRowIsReadOnly() throws { - let manager = loadedManager() - let row = try #require(StructureInspectorRowBuilder.row( - atDisplayRow: 0, - tab: .columns, - provider: provider(manager), - canEditSchema: false - )) - - #expect(!row.isEditable) - } - - @Test("Tabs without a row grid supply nothing", arguments: [StructureTab.options, .ddl, .parts, .triggers]) - func nonGridTabsSupplyNothing(tab: StructureTab) { - let manager = loadedManager() - #expect(StructureInspectorRowBuilder.row( - atDisplayRow: 0, - tab: tab, - provider: provider(manager, tab: tab), - canEditSchema: true - ) == nil) - } - - @Test("A display row past the end supplies nothing") - func outOfRangeDisplayRowSuppliesNothing() { - let manager = loadedManager() - #expect(StructureInspectorRowBuilder.row( - atDisplayRow: 9, - tab: .columns, - provider: provider(manager), - canEditSchema: true - ) == nil) - } -} diff --git a/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift b/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift index 6a51846885..b4e8440113 100644 --- a/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift +++ b/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift @@ -162,7 +162,7 @@ struct StructureListGridAccessibilityTests { } let table = try #require(Self.mainTable(in: window.contentView)) - #expect(table.accessibilityLabel() == StructureTab.triggers.displayName) + #expect(table.accessibilityLabel() == StructurePane.triggers.displayName) } private static func mainTable(in view: NSView?) -> DataGridTableView? { diff --git a/apple/TableProTests/Views/Structure/StructureLoadStateTests.swift b/apple/TableProTests/Views/Structure/StructureLoadStateTests.swift new file mode 100644 index 0000000000..a8377c2eaa --- /dev/null +++ b/apple/TableProTests/Views/Structure/StructureLoadStateTests.swift @@ -0,0 +1,148 @@ +// +// StructureLoadStateTests.swift +// TableProTests +// + +import Foundation +import Testing + +@testable import TablePro + +struct StructureLoadStateTests { + @Test("A fresh state has no data and needs every part fetched") + func freshStateNeedsFetch() { + let state = StructureLoadState() + + for item in StructureLoadState.Item.allCases { + #expect(!state.hasData(item)) + #expect(state.needsFetch(item)) + #expect(state.failure(item) == nil) + } + } + + @Test("markFetched records data and clears the fetch requirement") + func markFetchedRecordsData() { + var state = StructureLoadState() + state.markFetched(.indexes) + + #expect(state.hasData(.indexes)) + #expect(!state.needsFetch(.indexes)) + #expect(!state.hasData(.columns)) + } + + @Test("markAllStale keeps counts on screen while requiring a refetch") + func markAllStaleKeepsData() { + var state = StructureLoadState() + state.markFetched(.columns) + state.markFetched(.indexes) + state.markFetched(.foreignKeys) + + state.markAllStale() + + for item in [StructureLoadState.Item.columns, .indexes, .foreignKeys] { + #expect(state.hasData(item)) + #expect(state.needsFetch(item)) + } + } + + @Test("Refetching a stale part clears only that part's staleness") + func refetchClearsSinglePart() { + var state = StructureLoadState() + state.markFetched(.columns) + state.markFetched(.ddl) + state.markAllStale() + + state.markFetched(.columns) + + #expect(!state.needsFetch(.columns)) + #expect(state.needsFetch(.ddl)) + #expect(state.hasData(.ddl)) + } + + @Test("reset drops the data and the failures so a different table starts cold") + func resetDropsData() { + var state = StructureLoadState() + state.markFetched(.columns) + state.markFailed(.triggers, message: "permission denied") + state.markAllStale() + + state.reset() + + #expect(!state.hasData(.columns)) + #expect(state.needsFetch(.columns)) + #expect(state.failures.isEmpty) + } + + // One failed read used to blank every sub-tab, and a failed triggers read showed as "No Triggers". + @Test("A failed read is reported for its own part only, and keeps what that part had loaded") + func failureIsPerItem() { + var state = StructureLoadState() + state.markFetched(.columns) + state.markFetched(.triggers) + + state.markFailed(.triggers, message: "permission denied") + + #expect(state.failure(.triggers) == "permission denied") + #expect(state.hasData(.triggers)) + #expect(state.needsFetch(.triggers)) + #expect(state.failure(.columns) == nil) + #expect(!state.needsFetch(.columns)) + #expect(state.failures == [.triggers: "permission denied"]) + } + + @Test("A first read that fails has no data, says why, and is fetched again") + func firstReadFailure() { + var state = StructureLoadState() + + state.markFailed(.ddl, message: "timed out") + + #expect(!state.hasData(.ddl)) + #expect(state.failure(.ddl) == "timed out") + #expect(state.needsFetch(.ddl)) + } + + @Test("Several parts can fail at once, each with its own message") + func failuresAreIndependent() { + var state = StructureLoadState() + + state.markFailed(.indexes, message: "no index access") + state.markFailed(.checkConstraints, message: "no catalog access") + + #expect(state.failure(.indexes) == "no index access") + #expect(state.failure(.checkConstraints) == "no catalog access") + #expect(state.failure(.foreignKeys) == nil) + } + + @Test("A read that lands clears that part's failure") + func fetchClearsFailure() { + var state = StructureLoadState() + state.markFailed(.foreignKeys, message: "timed out") + state.markFailed(.indexes, message: "timed out") + + state.markFetched(.foreignKeys) + + #expect(state.failure(.foreignKeys) == nil) + #expect(!state.needsFetch(.foreignKeys)) + #expect(state.failure(.indexes) == "timed out") + } + + @Test("Marking everything stale keeps the failures on screen") + func staleKeepsFailures() { + var state = StructureLoadState() + state.markFetched(.columns) + state.markFailed(.columns, message: "timed out") + + state.markAllStale() + + #expect(state.failure(.columns) == "timed out") + #expect(state.hasData(.columns)) + } + + @Test("Each section reads into its own part") + func sectionMapsToItsItem() { + #expect(StructureLoadState.Item(StructureSection.columns) == .columns) + #expect(StructureLoadState.Item(StructureSection.indexes) == .indexes) + #expect(StructureLoadState.Item(StructureSection.foreignKeys) == .foreignKeys) + #expect(StructureLoadState.Item(StructureSection.checkConstraints) == .checkConstraints) + } +} diff --git a/apple/TableProTests/Views/Structure/StructureRowMenuRouteTests.swift b/apple/TableProTests/Views/Structure/StructureRowMenuRouteTests.swift index ca55e1a0a9..c3312a8a20 100644 --- a/apple/TableProTests/Views/Structure/StructureRowMenuRouteTests.swift +++ b/apple/TableProTests/Views/Structure/StructureRowMenuRouteTests.swift @@ -12,31 +12,38 @@ import Testing // Every row menu, from any click and either pane, is the grid's commands plus the owner's `gridRowMenuItems`. @MainActor struct StructureRowMenuRouteTests { - private func makeAdapter( - tab: StructureTab = .columns, + private let suite: EphemeralDefaults + + init() throws { + suite = try EphemeralDefaults() + } + + private func makeEditor( type: DatabaseType = .mysql, objectKind: TableInfo.TableType = .table, manager: StructureChangeManager = StructureChangeManager() - ) -> StructureGridAdapter { + ) -> StructureDefinitionEditor { if manager.workingColumns.isEmpty { manager.addColumn(.placeholder()) } - let adapter = StructureGridAdapter( - structureChangeManager: manager, - connection: TestFixtures.makeConnection(type: type), - tableName: "orders", - databaseName: "shop", - schemaName: nil, - objectKind: objectKind, - isSessionBound: false + let editor = StructureDefinitionEditor( + manager: manager, + context: .existingTable( + connection: TestFixtures.makeConnection(type: type), + databaseName: "shop", + schemaName: nil, + tableName: "orders", + objectKind: objectKind, + isSessionBound: false + ), + defaults: suite.defaults ) - adapter.store.switchSubTab(to: tab) - adapter.refresh() - return adapter + editor.refresh() + return editor } - private func row(_ sourceIndex: Int) -> RowID { - StructureRowProvider.rowID(forSourceIndex: sourceIndex) + private func row(_ sourceIndex: Int, in adapter: StructureSectionAdapter) -> RowID { + adapter.rowID(forEntity: StructureGridEdits.entityIDs(in: adapter.section, manager: adapter.manager)[sourceIndex]) } private func titles(_ items: [NSMenuItem]) -> [String] { @@ -45,8 +52,9 @@ struct StructureRowMenuRouteTests { @Test("A column row's menu is the schema's own items, with no result commands") func columnRowsGetTheStructureMenu() { - let adapter = makeAdapter() - let items = titles(adapter.gridRowMenuItems(for: [row(0)], clickedRow: row(0))) + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + let items = titles(adapter.gridRowMenuItems(for: [row(0, in: adapter)], clickedRow: row(0, in: adapter))) #expect(items.contains(String(localized: "Copy Name"))) #expect(items.contains(String(localized: "Copy Definition"))) @@ -54,15 +62,6 @@ struct StructureRowMenuRouteTests { #expect(commands.isDisjoint(with: [.exportResults, .showRowsAsJSON, .clearResults, .copyAsSQL, .filterWithColumn])) } - @Test("A DDL tab row offers no row items and no row commands") - func theDdlTabRaisesNoMenu() { - let adapter = makeAdapter(tab: .ddl) - - #expect(adapter.gridRowMenuItems(for: [row(0)], clickedRow: row(0)).isEmpty) - #expect(adapter.gridEmptySpaceMenu() == nil) - #expect(adapter.model.configuration.commands.isDisjoint(with: [.deleteRows, .duplicateRows, .addRow, .undoDelete])) - } - @Test("A row pending deletion offers none of the schema's items") func deletedRowOffersNothing() throws { let manager = StructureChangeManager() @@ -73,21 +72,25 @@ struct StructureRowMenuRouteTests { foreignKeys: [], primaryKey: ["id"] ) - let adapter = makeAdapter(manager: manager) - adapter.gridDeleteRows([row(0)]) + let editor = makeEditor(manager: manager) + let adapter = editor.adapter(for: .columns) + let id = row(0, in: adapter) + adapter.gridDeleteRows([id]) try #require(manager.deleteInsertState(for: 0, tab: .columns).isDeleted) - #expect(adapter.gridRowMenuItems(for: [row(0)], clickedRow: row(0)).isEmpty) + #expect(adapter.gridRowMenuItems(for: [id], clickedRow: id).isEmpty) } // A materialized view refuses DROP COLUMN and ADD COLUMN; the menu used to offer both and do nothing. @Test("Delete and Duplicate are offered only where the object accepts them") func rowEditsFollowTheGate() { - let refused = makeAdapter(type: .postgresql, objectKind: .materializedView) + let refusedEditor = makeEditor(type: .postgresql, objectKind: .materializedView) + let refused = refusedEditor.adapter(for: .columns) #expect(refused.model.configuration.commands.isDisjoint(with: [.deleteRows, .duplicateRows])) - #expect(refused.gridRowMenuItems(for: [row(0)], clickedRow: row(0)).last?.isSeparatorItem == false) + #expect(refused.gridRowMenuItems(for: [row(0, in: refused)], clickedRow: row(0, in: refused)).last?.isSeparatorItem == false) - let accepted = makeAdapter() + let acceptedEditor = makeEditor() + let accepted = acceptedEditor.adapter(for: .columns) #expect(accepted.model.configuration.commands.isSuperset(of: [.deleteRows, .duplicateRows, .undoDelete])) } @@ -100,13 +103,14 @@ struct StructureRowMenuRouteTests { second.referencedTable = "products" manager.addForeignKey(first) manager.addForeignKey(second) - let adapter = makeAdapter(tab: .foreignKeys, manager: manager) + let editor = makeEditor(manager: manager) + let adapter = editor.adapter(for: .foreignKeys) let openCustomers = String(format: String(localized: "Open %@"), "customers") let openProducts = String(format: String(localized: "Open %@"), "products") - #expect(titles(adapter.gridRowMenuItems(for: [row(0)], clickedRow: row(0))).contains(openCustomers)) + #expect(titles(adapter.gridRowMenuItems(for: [row(0, in: adapter)], clickedRow: row(0, in: adapter))).contains(openCustomers)) - let selection = titles(adapter.structureRowMenuItems(for: [row(0), row(1)], clickedRow: row(1))) + let selection = titles(adapter.rowMenuItems(for: [row(0, in: adapter), row(1, in: adapter)], clickedRow: row(1, in: adapter))) #expect(selection.contains(openProducts)) #expect(!selection.contains(openCustomers)) } @@ -125,12 +129,16 @@ struct StructureRowMenuRouteTests { foreignKeys: [], primaryKey: ["id"] ) - let adapter = makeAdapter(manager: manager) + let editor = makeEditor(manager: manager) + let adapter = editor.adapter(for: .columns) try #require(adapter.model.configuration.rowReorder.isEnabled) var moved: (from: Int, to: Int)? - adapter.moveRowHandler = { moved = ($0, $1) } + editor.moveColumnHandler = { moved = ($0, $1) } - let items = adapter.structureRowMenuItems(for: [row(0), row(1), row(2)], clickedRow: row(2)) + let items = adapter.rowMenuItems( + for: [row(0, in: adapter), row(1, in: adapter), row(2, in: adapter)], + clickedRow: row(2, in: adapter) + ) let up = try #require(items.first { $0.title == String(localized: "Move Column Up") }) let down = try #require(items.first { $0.title == String(localized: "Move Column Down") }) #expect(up.action != nil) @@ -156,28 +164,17 @@ struct StructureRowMenuRouteTests { foreignKeys: [], primaryKey: ["id"] ) - let adapter = makeAdapter(manager: manager) - adapter.gridDeleteRows([row(1)]) + let editor = makeEditor(manager: manager) + let adapter = editor.adapter(for: .columns) + let rows = [row(0, in: adapter), row(1, in: adapter), row(2, in: adapter)] + adapter.gridDeleteRows([rows[1]]) try #require(manager.deleteInsertState(for: 1, tab: .columns).isDeleted) - let selection = [row(0), row(1), row(2)] - let items = titles(adapter.gridRowMenuItems(for: selection, clickedRow: row(0))) + let items = titles(adapter.gridRowMenuItems(for: rows, clickedRow: rows[0])) #expect(items.contains(String(localized: "Copy Name"))) #expect(items.contains(String(localized: "Copy Definition"))) #expect(items.contains(String(localized: "Copy As"))) - #expect(titles(adapter.structureRowMenuItems(for: selection, clickedRow: row(2))).contains(String(localized: "Copy Name"))) - #expect(adapter.structureRowMenuItems(for: selection, clickedRow: row(1)).isEmpty) - } - - @Test("The empty space below the rows offers the sub-tab's own Add") - func emptySpaceOffersAdd() { - let columns = makeAdapter(tab: .columns) - #expect(titles(columns.gridEmptySpaceMenu()?.items ?? []) == [String(localized: "Add Column")]) - - let indexes = makeAdapter(tab: .indexes) - #expect(titles(indexes.gridEmptySpaceMenu()?.items ?? []) == [String(localized: "Add Index")]) - - let view = makeAdapter(type: .postgresql, objectKind: .view) - #expect(view.gridEmptySpaceMenu() == nil) + #expect(titles(adapter.rowMenuItems(for: rows, clickedRow: rows[2])).contains(String(localized: "Copy Name"))) + #expect(adapter.rowMenuItems(for: rows, clickedRow: rows[1]).isEmpty) } } diff --git a/apple/TableProTests/Views/Structure/StructureRowProviderBooleanOptionsTests.swift b/apple/TableProTests/Views/Structure/StructureRowProviderBooleanOptionsTests.swift index 6d909ff121..6e3278759f 100644 --- a/apple/TableProTests/Views/Structure/StructureRowProviderBooleanOptionsTests.swift +++ b/apple/TableProTests/Views/Structure/StructureRowProviderBooleanOptionsTests.swift @@ -36,7 +36,7 @@ struct StructureRowProviderBooleanOptionsTests { return manager } - private func makeProvider(tab: StructureTab = .columns) -> StructureRowProvider { + private func makeProvider(tab: StructureSection = .columns) -> StructureRowProvider { StructureRowProvider( changeManager: makeManager(), tab: tab, @@ -62,9 +62,7 @@ struct StructureRowProviderBooleanOptionsTests { } } - /// A closed vocabulary has to contain whatever the grid drew, or the menu cannot show the - /// current value as the selected one. The Default column is deliberately not closed: it holds - /// an open-ended SQL expression, which is what its `Custom…` entry marks it as. + // A menu can show the current value as selected only if it holds what the grid drew. Default is open: `Custom…`. @Test("A closed column's options are exactly what the grid draws") func optionsMatchRenderedValues() { let provider = makeProvider() @@ -109,7 +107,7 @@ struct StructureRowProviderBooleanOptionsTests { @Test("No structure column reports itself as nullable, so Set NULL is never offered") func structureColumnsAreNeverNullable() { let provider = makeProvider() - let rows = provider.asTableRows() + let rows = provider.asTableRows { RowID.existing($0) } #expect(!rows.columnNullable.isEmpty) for column in provider.columns { #expect(rows.columnNullable[column] == false) diff --git a/apple/TableProTests/Views/Structure/StructureRowProviderTests.swift b/apple/TableProTests/Views/Structure/StructureRowProviderTests.swift index e899cdce4a..647cd754ff 100644 --- a/apple/TableProTests/Views/Structure/StructureRowProviderTests.swift +++ b/apple/TableProTests/Views/Structure/StructureRowProviderTests.swift @@ -141,7 +141,7 @@ struct StructureRowProviderChangeStateTests { private func provider( _ manager: StructureChangeManager, - tab: StructureTab = .columns, + tab: StructureSection = .columns, filterText: String? = nil ) -> StructureRowProvider { StructureRowProvider( diff --git a/apple/TableProTests/Views/Structure/StructureSectionAdapterTests.swift b/apple/TableProTests/Views/Structure/StructureSectionAdapterTests.swift new file mode 100644 index 0000000000..52de660f26 --- /dev/null +++ b/apple/TableProTests/Views/Structure/StructureSectionAdapterTests.swift @@ -0,0 +1,285 @@ +// +// StructureSectionAdapterTests.swift +// TableProTests +// +// Add and Delete per section, the contract the +/- beneath each table, Cmd+Shift+N and the row menus share. +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +@MainActor +struct StructureSectionAdapterTests { + private let suite: EphemeralDefaults + + init() throws { + suite = try EphemeralDefaults() + } + + private func makeEditor( + type: DatabaseType = .mysql, + objectKind: TableInfo.TableType = .table, + manager: StructureChangeManager = StructureChangeManager() + ) -> StructureDefinitionEditor { + let editor = StructureDefinitionEditor( + manager: manager, + context: .existingTable( + connection: TestFixtures.makeConnection(type: type), + databaseName: "db", + schemaName: nil, + tableName: "t", + objectKind: objectKind, + isSessionBound: false + ), + defaults: suite.defaults + ) + editor.refresh() + return editor + } + + private func row(_ sourceIndex: Int, in adapter: StructureSectionAdapter) -> RowID { + adapter.rowID(forEntity: StructureGridEdits.entityIDs(in: adapter.section, manager: adapter.manager)[sourceIndex]) + } + + private func columns(named names: [String]) -> StructureChangeManager { + let manager = StructureChangeManager() + for name in names { + var column = EditableColumnDefinition.placeholder() + column.name = name + column.dataType = "TEXT" + manager.addColumn(column) + } + return manager + } + + @Test("Columns: Add Row appends a placeholder column") + func columnsAddsColumn() { + let editor = makeEditor() + let before = editor.manager.workingColumns.count + editor.adapter(for: .columns).gridAddRow() + #expect(editor.manager.workingColumns.count == before + 1) + } + + @Test("Indexes: Add Row appends a placeholder index") + func indexesAddsIndex() { + let editor = makeEditor() + let before = editor.manager.workingIndexes.count + editor.adapter(for: .indexes).gridAddRow() + #expect(editor.manager.workingIndexes.count == before + 1) + } + + @Test("Foreign keys: Add Row appends a placeholder foreign key") + func foreignKeysAddsForeignKey() { + let editor = makeEditor() + let before = editor.manager.workingForeignKeys.count + editor.adapter(for: .foreignKeys).gridAddRow() + #expect(editor.manager.workingForeignKeys.count == before + 1) + } + + @Test("Each section adds only its own kind of definition", arguments: StructureSection.allCases) + func addStaysInItsSection(section: StructureSection) { + let editor = makeEditor(type: .postgresql) + editor.adapter(for: section).gridAddRow() + + for other in StructureSection.allCases { + let count = StructureGridEdits.rowCount(on: other, manager: editor.manager) + #expect(count == (other == section ? 1 : 0), "\(other.rawValue)") + } + } + + // SQLite's curated snapshot overrides neither supportsAddIndex nor supportsDropIndex, so both default to true. + @Test("Indexes on SQLite: Add Row appends an index") + func sqliteIndexesAddsAnIndex() { + let editor = makeEditor(type: .sqlite) + let before = editor.manager.workingIndexes.count + editor.adapter(for: .indexes).gridAddRow() + #expect(editor.manager.workingIndexes.count == before + 1) + } + + @Test("Columns: Delete removes the column the row names") + func columnsDeleteRemovesColumn() { + let editor = makeEditor() + let adapter = editor.adapter(for: .columns) + adapter.gridAddRow() + let after = editor.manager.workingColumns.count + #expect(after > 0) + adapter.gridDeleteRows([row(after - 1, in: adapter)]) + #expect(editor.manager.workingColumns.count == after - 1) + } + + @Test("Indexes: Delete removes the index the row names") + func indexesDeleteRemovesIndex() { + let editor = makeEditor() + let adapter = editor.adapter(for: .indexes) + adapter.gridAddRow() + let after = editor.manager.workingIndexes.count + #expect(after > 0) + adapter.gridDeleteRows([row(after - 1, in: adapter)]) + #expect(editor.manager.workingIndexes.count == after - 1) + } + + @Test("Foreign keys: Delete removes the foreign key the row names") + func foreignKeysDeleteRemovesForeignKey() { + let editor = makeEditor() + let adapter = editor.adapter(for: .foreignKeys) + adapter.gridAddRow() + let after = editor.manager.workingForeignKeys.count + #expect(after > 0) + adapter.gridDeleteRows([row(after - 1, in: adapter)]) + #expect(editor.manager.workingForeignKeys.count == after - 1) + } + + @Test("Indexes on SQLite: Delete removes an index") + func sqliteIndexesDeletesAnIndex() { + let manager = StructureChangeManager() + manager.addIndex(.placeholder()) + let editor = makeEditor(type: .sqlite, manager: manager) + let adapter = editor.adapter(for: .indexes) + let before = manager.workingIndexes.count + + adapter.gridDeleteRows([row(before - 1, in: adapter)]) + + #expect(manager.workingIndexes.count == before - 1) + } + + @Test("Deleting the last row selects the row above it") + func deletingTheLastRowSelectsTheOneAbove() { + let manager = StructureChangeManager() + for _ in 0..<3 { + manager.addColumn(.placeholder()) + } + let editor = makeEditor(manager: manager) + let adapter = editor.adapter(for: .columns) + let above = row(1, in: adapter) + adapter.model.selectRows([row(2, in: adapter)]) + + adapter.gridDeleteRows([row(2, in: adapter)]) + + #expect(manager.workingColumns.count == 2) + #expect(adapter.model.selectedRowIDs == [above]) + } + + // MARK: - Row identity + + // A RowID was the definition's index, so removing one above moved the selection onto its neighbour. + @Test("The selection stays on its definition across a refresh after a definition above it is removed") + func selectionSurvivesRemovalAbove() { + let manager = columns(named: ["a", "b", "c"]) + let editor = makeEditor(manager: manager) + let adapter = editor.adapter(for: .columns) + let c = manager.workingColumns[2].id + let rowID = adapter.rowID(forEntity: c) + adapter.select(entity: c) + #expect(adapter.sourceIndex(of: rowID) == 2) + + manager.deleteColumn(id: manager.workingColumns[0].id) + editor.refresh() + + #expect(manager.workingColumns.map(\.name) == ["b", "c"]) + #expect(adapter.rowID(forEntity: c) == rowID) + #expect(adapter.entityID(of: rowID) == c) + #expect(adapter.sourceIndex(of: rowID) == 1) + #expect(adapter.model.selectedRowIDs == [rowID]) + #expect(adapter.selectedEntityIDs == [c]) + #expect(editor.inspectorTarget == .entity(.columns, c)) + } + + @Test("A sort reorders the rows without renaming them") + func rowIDSurvivesSort() { + let manager = columns(named: ["a", "b", "c"]) + let editor = makeEditor(manager: manager) + let adapter = editor.adapter(for: .columns) + let a = manager.workingColumns[0].id + + adapter.sortDidChange(SortState(columns: [SortColumn(columnIndex: 0, direction: .descending)], source: .user)) + + #expect(adapter.model.rowID(at: 2) == adapter.rowID(forEntity: a)) + #expect(adapter.sourceIndex(of: adapter.rowID(forEntity: a)) == 0) + } + + // MARK: - Several rows + + @Test("Duplicating several rows is one undo step") + func duplicateIsOneUndoStep() { + let manager = columns(named: ["a", "b", "c"]) + let editor = makeEditor(manager: manager) + let adapter = editor.adapter(for: .columns) + + adapter.gridDuplicateRows([row(0, in: adapter), row(2, in: adapter)]) + #expect(manager.workingColumns.map(\.name) == ["a", "b", "c", "a", "c"]) + + manager.undo() + + #expect(manager.workingColumns.map(\.name) == ["a", "b", "c"]) + } + + @Test("Deleting several rows is one undo step") + func deleteIsOneUndoStep() { + let manager = columns(named: ["a", "b", "c"]) + let editor = makeEditor(manager: manager) + let adapter = editor.adapter(for: .columns) + + adapter.gridDeleteRows([row(0, in: adapter), row(2, in: adapter)]) + #expect(manager.workingColumns.map(\.name) == ["b"]) + + manager.undo() + + #expect(manager.workingColumns.map(\.name) == ["a", "b", "c"]) + } + + // MARK: - Per object kind + + // Cmd+Shift+N, the row menus and the +/- all reach the adapter, so it refuses for itself (#2726). + @Test("A PostgreSQL view refuses an added column, whatever reaches the adapter") + func viewRefusesAnAddedColumn() { + let editor = makeEditor(type: .postgresql, objectKind: .view) + let adapter = editor.adapter(for: .columns) + adapter.gridAddRow() + #expect(editor.manager.workingColumns.isEmpty) + #expect(!adapter.model.configuration.commands.contains(.addRow)) + } + + @Test("A PostgreSQL view refuses an added index and a materialized view accepts one") + func indexesFollowTheObjectKind() { + let view = makeEditor(type: .postgresql, objectKind: .view) + view.adapter(for: .indexes).gridAddRow() + #expect(view.manager.workingIndexes.isEmpty) + + let matview = makeEditor(type: .postgresql, objectKind: .materializedView) + matview.adapter(for: .indexes).gridAddRow() + #expect(matview.manager.workingIndexes.count == 1) + } + + @Test("A PostgreSQL materialized view refuses a foreign key") + func materializedViewRefusesAForeignKey() { + let editor = makeEditor(type: .postgresql, objectKind: .materializedView) + editor.adapter(for: .foreignKeys).gridAddRow() + #expect(editor.manager.workingForeignKeys.isEmpty) + } + + @Test("A view refuses to drop a column its own grid is showing") + func viewRefusesADroppedColumn() { + let manager = StructureChangeManager() + manager.addColumn(.placeholder()) + #expect(manager.workingColumns.count == 1) + let editor = makeEditor(type: .postgresql, objectKind: .view, manager: manager) + let view = editor.adapter(for: .columns) + + view.gridDeleteRows([row(0, in: view)]) + + #expect(manager.workingColumns.count == 1) + #expect(!view.model.configuration.commands.contains(.deleteRows)) + } + + @Test("A system table refuses every add", arguments: StructureSection.allCases) + func systemTableRefusesEveryAdd(section: StructureSection) { + let editor = makeEditor(type: .postgresql, objectKind: .systemTable) + editor.adapter(for: section).gridAddRow() + #expect(editor.manager.workingColumns.isEmpty) + #expect(editor.manager.workingIndexes.isEmpty) + #expect(editor.manager.workingForeignKeys.isEmpty) + #expect(editor.manager.workingCheckConstraints.isEmpty) + } +} diff --git a/apple/TableProTests/Views/Structure/StructureServerSupportTests.swift b/apple/TableProTests/Views/Structure/StructureServerSupportTests.swift index ae7bc59834..915945b6c2 100644 --- a/apple/TableProTests/Views/Structure/StructureServerSupportTests.swift +++ b/apple/TableProTests/Views/Structure/StructureServerSupportTests.swift @@ -57,7 +57,7 @@ struct StructureServerSupportTests { return manager } - private func provider(tab: StructureTab, support: StructureServerSupport) -> StructureRowProvider { + private func provider(tab: StructureSection, support: StructureServerSupport) -> StructureRowProvider { StructureRowProvider( changeManager: makeManager(), tab: tab, @@ -154,7 +154,7 @@ struct StructureServerSupportTests { let support = StructureServerSupport(driver: adapter) #expect(support.checkConstraintRefusal == "Check constraints need MySQL 8.0.16 or later.") #expect(!support.offers(.checkConstraints)) - for tab in StructureTab.allCases where tab != .checkConstraints { + for tab in StructureSection.allCases where tab != .checkConstraints { #expect(support.offers(tab), "\(tab)") } #expect(StructureServerSupport.unrestricted.offers(.checkConstraints)) diff --git a/apple/TableProTests/Views/Structure/StructureTabAvailabilityTests.swift b/apple/TableProTests/Views/Structure/StructureTabAvailabilityTests.swift deleted file mode 100644 index 77308cd14b..0000000000 --- a/apple/TableProTests/Views/Structure/StructureTabAvailabilityTests.swift +++ /dev/null @@ -1,69 +0,0 @@ -// -// StructureTabAvailabilityTests.swift -// TableProTests -// - -import Foundation -@testable import TablePro -import Testing - -struct StructureTabAvailabilityTests { - private static let legacyMySQL = StructureServerSupport( - unsupportedColumnFields: [], - unsupportedIndexTypes: [], - checkConstraintRefusal: "Check constraints need MySQL 8.0.16 or later." - ) - - @Test("A MySQL server that discards a CHECK clause is not offered the tab") - func legacyServerHidesConstraints() { - let tabs = StructureTabAvailability.tabs(for: .mysql, serverSupport: Self.legacyMySQL) - #expect(!tabs.contains(.checkConstraints)) - #expect(tabs.contains(.columns)) - #expect(tabs.contains(.indexes)) - #expect(tabs.contains(.foreignKeys)) - #expect(tabs.contains(.ddl)) - } - - @Test("A server that keeps them is offered the tab, on every engine that has them") - func modernServerShowsConstraints() { - #expect(StructureTabAvailability.tabs(for: .mysql, serverSupport: .unrestricted) - .contains(.checkConstraints)) - #expect(StructureTabAvailability.tabs(for: .postgresql, serverSupport: .unrestricted) - .contains(.checkConstraints)) - } - - @Test("An engine with no check constraints at all stays hidden either way") - func engineWithoutChecksStaysHidden() { - for support in [StructureServerSupport.unrestricted, Self.legacyMySQL] { - let tabs = StructureTabAvailability.tabs(for: .redis, serverSupport: support) - #expect(!tabs.contains(.checkConstraints)) - } - } - - // The gate is the driver's statement probe for the object, so any engine whose driver answers gets the tab. - @Test("Options follows the driver's answer, not the engine type") - func optionsFollowsTheDriver() { - #expect(StructureTabAvailability.tabs(for: .postgresql, serverSupport: .unrestricted, offersTableOptions: true) - .contains(.options)) - #expect(!StructureTabAvailability.tabs(for: .mysql, serverSupport: .unrestricted, offersTableOptions: false) - .contains(.options)) - #expect(!StructureTabAvailability.tabs(for: .mysql, serverSupport: .unrestricted).contains(.options)) - } - - @Test("Options sits after the editable tabs and before DDL") - func optionsSitsBeforeDDL() throws { - let tabs = StructureTabAvailability.tabs(for: .mysql, serverSupport: .unrestricted, offersTableOptions: true) - let options = try #require(tabs.firstIndex(of: .options)) - let ddl = try #require(tabs.firstIndex(of: .ddl)) - let columns = try #require(tabs.firstIndex(of: .columns)) - #expect(columns < options) - #expect(options < ddl) - } - - @Test("Parts is ClickHouse only, and the server refusal does not reach it") - func partsIsClickHouseOnly() { - #expect(StructureTabAvailability.tabs(for: .clickhouse, serverSupport: .unrestricted).contains(.parts)) - #expect(!StructureTabAvailability.tabs(for: .mysql, serverSupport: .unrestricted).contains(.parts)) - #expect(StructureTabAvailability.tabs(for: .clickhouse, serverSupport: Self.legacyMySQL).contains(.parts)) - } -} diff --git a/apple/TableProTests/Views/Structure/StructureTabDataStateTests.swift b/apple/TableProTests/Views/Structure/StructureTabDataStateTests.swift deleted file mode 100644 index f24bf763b1..0000000000 --- a/apple/TableProTests/Views/Structure/StructureTabDataStateTests.swift +++ /dev/null @@ -1,88 +0,0 @@ -// -// StructureTabDataStateTests.swift -// TableProTests -// - -import Foundation -import Testing - -@testable import TablePro - -struct StructureTabDataStateTests { - @Test("a fresh state has no data and needs every tab fetched") - func freshStateNeedsFetch() { - let state = StructureTabDataState() - - for tab in StructureTab.allCases { - #expect(!state.hasData(tab)) - #expect(state.needsFetch(tab)) - } - } - - @Test("markFetched records data and clears the fetch requirement") - func markFetchedRecordsData() { - var state = StructureTabDataState() - state.markFetched(.indexes) - - #expect(state.hasData(.indexes)) - #expect(!state.needsFetch(.indexes)) - #expect(!state.hasData(.columns)) - } - - @Test("markAllStale keeps counts on screen while requiring a refetch") - func markAllStaleKeepsData() { - var state = StructureTabDataState() - state.markFetched(.columns) - state.markFetched(.indexes) - state.markFetched(.foreignKeys) - - state.markAllStale() - - for tab in [StructureTab.columns, .indexes, .foreignKeys] { - #expect(state.hasData(tab)) - #expect(state.needsFetch(tab)) - } - } - - @Test("refetching a stale tab clears only that tab's staleness") - func refetchClearsSingleTab() { - var state = StructureTabDataState() - state.markFetched(.columns) - state.markFetched(.ddl) - state.markAllStale() - - state.markFetched(.columns) - - #expect(!state.needsFetch(.columns)) - #expect(state.needsFetch(.ddl)) - #expect(state.hasData(.ddl)) - } - - @Test("reset drops the data so a different table starts cold") - func resetDropsData() { - var state = StructureTabDataState() - state.markFetched(.columns) - state.markAllStale() - - state.reset() - - #expect(!state.hasData(.columns)) - #expect(state.needsFetch(.columns)) - } - - @Test("a tab with data renders its count, a tab without renders a bare name") - func labelIncludesCountOnlyWhenLoaded() { - let withCount = StructureTabDataState.label(for: .indexes, count: 4) - let withoutCount = StructureTabDataState.label(for: .indexes, count: nil) - - #expect(withCount == "\(StructureTab.indexes.displayName) (4)") - #expect(withoutCount == StructureTab.indexes.displayName) - #expect(withCount != withoutCount) - } - - @Test("a zero count still renders, so the label never collapses to a bare name") - func labelRendersZeroCount() { - #expect(StructureTabDataState.label(for: .foreignKeys, count: 0) - == "\(StructureTab.foreignKeys.displayName) (0)") - } -} diff --git a/apple/TableProUITests/SidebarAddMenuUITests.swift b/apple/TableProUITests/SidebarAddMenuUITests.swift index 51ff5a494c..d5314221a4 100644 --- a/apple/TableProUITests/SidebarAddMenuUITests.swift +++ b/apple/TableProUITests/SidebarAddMenuUITests.swift @@ -14,16 +14,11 @@ final class SidebarAddMenuUITests: UITestCase { XCTAssertTrue(newTable.waitToExist(timeout: 10), "The add button offers New Table…") newTable.click() - // By identifier: the editor tab named Create Table also matches a "Create Table" button query. - let commit = window.buttons["create-table-commit"].firstMatch - XCTAssertTrue(commit.waitToExist(timeout: 30), "New Table… from the add button opens the Create Table tab") - XCTAssertTrue( - window.staticTexts.matching(identifier: "create-table-validation").firstMatch.waitToExist(timeout: 10), - "A draft that cannot run yet must say what is missing" - ) - XCTAssertFalse(commit.isEnabled, "An empty draft has no name and no column, so Create Table stays dimmed") - let name = window.textFields["create-table-name"] + XCTAssertTrue(name.waitToExist(timeout: 30), "New Table… from the add button opens the Create Table tab") + let validation = window.descendants(matching: .any).matching(identifier: "create-table-validation").firstMatch + XCTAssertTrue(validation.waitToExist(timeout: 10), "A draft that cannot run yet must say what is missing") + XCTAssertTrue(waitUntilHittable(name, timeout: 10)) name.click() name.typeText("playlist") @@ -31,10 +26,11 @@ final class SidebarAddMenuUITests: UITestCase { fillFirstColumn(named: "title", type: "TEXT", in: window, app: app) XCTAssertTrue( - waitForPredicate(timeout: 10) { commit.isEnabled }, - "A named table with a named, typed column can be created" + waitForPredicate(timeout: 10) { !validation.exists }, + "A named table with a named, typed column has nothing left to fix" ) - commit.click() + // Create Table has no button of its own: the toolbar's commit item and Cmd+S run it. + app.typeShortcut(.save) XCTAssertTrue( objectBrowserRow("playlist", in: window).waitToExist(timeout: 30), @@ -50,7 +46,9 @@ final class SidebarAddMenuUITests: UITestCase { // Cells are drawn, not mounted, so the row is reached through a point and edited from the keyboard: // Left until the cursor stops on the first column, Return opens its editor, Tab commits and opens the next. private func fillFirstColumn(named column: String, type: String, in window: XCUIElement, app: XCUIApplication) { - let grid = dataGrid(in: window) + let columns = window.descendants(matching: .any).matching(identifier: "structure-section-columns").firstMatch + XCTAssertTrue(columns.waitToExist(timeout: 10), "The Definition page lists the draft's columns") + let grid = dataGrid(in: columns) XCTAssertTrue(waitForClickableRows(in: grid), "The draft starts with one empty column row") let firstRow = grid.tableRows.firstMatch gridPoint(in: grid, of: window, dy: firstRow.frame.midY - grid.frame.minY).click() diff --git a/apple/TableProUITests/StructureSaveConfirmationUITests.swift b/apple/TableProUITests/StructureSaveConfirmationUITests.swift index 17cab44c64..eb34d4e539 100644 --- a/apple/TableProUITests/StructureSaveConfirmationUITests.swift +++ b/apple/TableProUITests/StructureSaveConfirmationUITests.swift @@ -21,7 +21,7 @@ final class StructureSaveConfirmationUITests: UITestCase { waitForPredicate(timeout: 10) { grid.element.tableRows.element(boundBy: 1).isSelected }, "The click must select the tag row" ) - let remove = window.buttons["structure-footer-remove"].firstMatch + let remove = window.buttons["structure-columns-remove"].firstMatch XCTAssertTrue(remove.waitToExist(timeout: 20), "The Columns tab must offer a remove button") XCTAssertTrue(waitForPredicate(timeout: 10) { remove.isEnabled }, "Removing the selected column must be offered") // On the 1024pt runner the structure editor overflows its pane and this button ends past the window edge. diff --git a/apple/TableProUITests/TableChangeReloadUITests.swift b/apple/TableProUITests/TableChangeReloadUITests.swift index b0bcd2ae1b..a74ed34f06 100644 --- a/apple/TableProUITests/TableChangeReloadUITests.swift +++ b/apple/TableProUITests/TableChangeReloadUITests.swift @@ -62,7 +62,7 @@ final class TableChangeReloadUITests: UITestCase { XCTAssertTrue(grid.header("label").waitToExist(timeout: 30), "The Data view must show document's columns") showStructure(in: app, window: window) - let add = window.buttons["structure-footer-add"].firstMatch + let add = window.buttons["structure-columns-add"].firstMatch XCTAssertTrue(add.waitToExist(timeout: 20), "The Columns tab must offer an add button") XCTAssertTrue(waitForPredicate(timeout: 10) { add.isEnabled }, "SQLite adds a column with ALTER TABLE") add.click() diff --git a/apple/changelog.d/3872.added.md b/apple/changelog.d/3872.added.md new file mode 100644 index 0000000000..32b25ffe58 --- /dev/null +++ b/apple/changelog.d/3872.added.md @@ -0,0 +1 @@ +File > Discard Changes… discards staged structure or data edits after asking. diff --git a/apple/changelog.d/3872.changed.md b/apple/changelog.d/3872.changed.md new file mode 100644 index 0000000000..736ff69800 --- /dev/null +++ b/apple/changelog.d/3872.changed.md @@ -0,0 +1 @@ +The Structure tab shows columns, indexes, foreign keys and checks on one page, with the inspector editing them. diff --git a/apple/changelog.d/3872.fixed.1.md b/apple/changelog.d/3872.fixed.1.md new file mode 100644 index 0000000000..d6c7ffe35b --- /dev/null +++ b/apple/changelog.d/3872.fixed.1.md @@ -0,0 +1 @@ +Undo Delete on an edited column in the Structure tab dropped the edit made before the delete. diff --git a/apple/changelog.d/3872.fixed.2.md b/apple/changelog.d/3872.fixed.2.md new file mode 100644 index 0000000000..cb1ae238d3 --- /dev/null +++ b/apple/changelog.d/3872.fixed.2.md @@ -0,0 +1 @@ +Refreshing the Structure tab, or saving in it, did not read check constraints again. diff --git a/apple/changelog.d/3872.fixed.3.md b/apple/changelog.d/3872.fixed.3.md new file mode 100644 index 0000000000..8781032e6f --- /dev/null +++ b/apple/changelog.d/3872.fixed.3.md @@ -0,0 +1 @@ +The selection in the Structure tab moved to another column after a reload or a sort. diff --git a/apple/changelog.d/3872.fixed.4.md b/apple/changelog.d/3872.fixed.4.md new file mode 100644 index 0000000000..51ec28cb4a --- /dev/null +++ b/apple/changelog.d/3872.fixed.4.md @@ -0,0 +1 @@ +One failed read blanked every Structure tab, and a failed Triggers read showed No Triggers. diff --git a/apple/changelog.d/3872.fixed.5.md b/apple/changelog.d/3872.fixed.5.md new file mode 100644 index 0000000000..67568e0a3b --- /dev/null +++ b/apple/changelog.d/3872.fixed.5.md @@ -0,0 +1 @@ +MySQL and MariaDB indexes offered a Condition the server drops. diff --git a/apple/changelog.d/3872.fixed.6.md b/apple/changelog.d/3872.fixed.6.md new file mode 100644 index 0000000000..f07eb45db5 --- /dev/null +++ b/apple/changelog.d/3872.fixed.6.md @@ -0,0 +1 @@ +Create Table read foreign key targets from the sidebar's database instead of the tab's. diff --git a/apple/changelog.d/3872.fixed.7.md b/apple/changelog.d/3872.fixed.7.md new file mode 100644 index 0000000000..6f63932edb --- /dev/null +++ b/apple/changelog.d/3872.fixed.7.md @@ -0,0 +1 @@ +Edit > Undo in the Structure tab did not name the change and stayed enabled with nothing to undo. diff --git a/apple/changelog.d/3872.fixed.8.md b/apple/changelog.d/3872.fixed.8.md new file mode 100644 index 0000000000..f51bbc6ec7 --- /dev/null +++ b/apple/changelog.d/3872.fixed.8.md @@ -0,0 +1 @@ +Switching a table from Structure to Data dimmed Save Changes while structure edits were still staged. diff --git a/apple/changelog.d/3872.fixed.md b/apple/changelog.d/3872.fixed.md new file mode 100644 index 0000000000..9e7465aad4 --- /dev/null +++ b/apple/changelog.d/3872.fixed.md @@ -0,0 +1 @@ +Making a column part of the primary key in the Structure tab did not change the table's key. From 25001cc3e4e09f424f917d134fe441253aab39ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Thu, 29 Oct 2026 19:43:12 +0700 Subject: [PATCH 03/85] fix(structure): keep a saved definition selected and auto-show the inspector from any section (#3875) --- .../StructureChangeManager.swift | 11 +++++++ .../TablePro/Views/Main/MainContentView.swift | 28 +++++++++++++++- .../StructureDefinitionEditorTests.swift | 32 +++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift b/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift index 68e54b12b5..dc271f8c80 100644 --- a/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift +++ b/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift @@ -873,10 +873,21 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { } } tableOptions = TableOptionsDraft(original: writtenOptions, isAutoIncrementConfirmed: isAutoIncrementConfirmed) + adoptWrittenDefinitions() discardChanges() return true } + // The written definitions stand in until the re-read lands, so a renamed or new definition keeps its id, its + // selection and the name the re-read is matched by. + private func adoptWrittenDefinitions() { + currentColumns = workingColumns.filter { !isPendingDeletion(.column($0.id)) } + currentIndexes = workingIndexes.filter { !isPendingDeletion(.index($0.id)) } + currentForeignKeys = workingForeignKeys.filter { !isPendingDeletion(.foreignKey($0.id)) } + currentCheckConstraints = workingCheckConstraints.filter { !isPendingDeletion(.checkConstraint($0.id)) } + currentPrimaryKey = workingPrimaryKey + } + // MARK: - Undo/Redo Operations func undo() { diff --git a/apple/TablePro/Views/Main/MainContentView.swift b/apple/TablePro/Views/Main/MainContentView.swift index 3de7ffcf9c..e19b1ee85a 100644 --- a/apple/TablePro/Views/Main/MainContentView.swift +++ b/apple/TablePro/Views/Main/MainContentView.swift @@ -361,7 +361,14 @@ struct MainContentView: View { await coordinator.loadInspectorRowValues(tabId: tabId, rowID: rowID) }) .background { - if let model = coordinator.activeGridModel { + if let editor = coordinator.activeStructureEditor { + StructureSelectionWatcher( + editor: editor, + onAttach: { scheduleInspectorUpdate() }, + onChange: { rowsChanged in handleGridSelectionChange(rowsChanged: rowsChanged) } + ) + .id(ObjectIdentifier(editor)) + } else if let model = coordinator.activeGridModel { GridSelectionWatcher( model: model, onAttach: { scheduleInspectorUpdate() }, @@ -552,6 +559,25 @@ struct MainContentView: View { } // No view of this window observes a session whose Structure view is not on screen, yet Discard and Save change it there. +// The selection moves between four grids, and the window reads only the one holding it. +private struct StructureSelectionWatcher: View { + @ObservedObject var editor: StructureDefinitionEditor + let onAttach: () -> Void + let onChange: (_ rowsChanged: Bool) -> Void + + var body: some View { + let model = editor.focusedModel ?? editor.adapter(for: .columns).model + Color.clear + .frame(width: 0, height: 0) + .accessibilityHidden(true) + .background { + GridSelectionWatcher(model: model, onAttach: onAttach, onChange: onChange) + .id(ObjectIdentifier(model)) + } + .onChange(of: editor.focusedSection) { _ in onChange(true) } + } +} + private struct StructureChangeWatcher: View { @ObservedObject var session: StructureEditingSession let onChange: () -> Void diff --git a/apple/TableProTests/Views/Structure/StructureDefinitionEditorTests.swift b/apple/TableProTests/Views/Structure/StructureDefinitionEditorTests.swift index 01f56ac935..8392e38fdd 100644 --- a/apple/TableProTests/Views/Structure/StructureDefinitionEditorTests.swift +++ b/apple/TableProTests/Views/Structure/StructureDefinitionEditorTests.swift @@ -176,6 +176,38 @@ struct StructureDefinitionEditorTests { #expect(editor.inspectorTarget == .entity(.columns, status)) } + @Test("A saved rename and a saved new column keep their ids and the selection through the re-read") + func saveKeepsRenamedAndNewDefinitions() async throws { + let editor = makeEditor() + let status = id(of: "status", in: editor) + editor.commitInspectorField(section: .columns, id: status, field: 0, value: "phase") + editor.manager.addNewColumn() + let added = try #require(editor.manager.workingColumns.last).id + editor.commitInspectorField(section: .columns, id: added, field: 0, value: "note") + editor.adapter(for: .columns).select(entity: added) + let hold = try #require(editor.manager.holdForSave()) + + #expect(editor.manager.releaseHold(hold, written: true)) + try await waitUntil { editor.adapter(for: .columns).model.configuration.isEditable } + #expect(editor.inspectorTarget == .entity(.columns, added)) + + editor.manager.loadSchema( + tableName: "orders", + columns: ["id", "customer_id", "phase", "note"].map { + ColumnInfo(name: $0, dataType: "INT", isNullable: true, isPrimaryKey: $0 == "id", + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil) + }, + indexes: [IndexInfo(name: "orders_pkey", columns: ["id"], isUnique: true, isPrimary: true, type: "BTREE")], + foreignKeys: [], + primaryKey: ["id"] + ) + try await waitUntil { editor.adapter(for: .columns).rowCount == 4 } + + #expect(id(of: "phase", in: editor) == status) + #expect(id(of: "note", in: editor) == added) + #expect(editor.inspectorTarget == .entity(.columns, added)) + } + @Test("A save's hold closes the grids and its end opens them again") func saveHoldTogglesEditing() async throws { let editor = makeEditor() From cb449271eebeec02dcbb9ace55bd891481a4e6ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Thu, 29 Oct 2026 21:26:32 +0700 Subject: [PATCH 04/85] perf(sidebar): stop redrawing the editor on every sidebar filter keystroke (#3876) --- .../Services/Infrastructure/MainSplitViewController.swift | 1 - .../Views/Main/Extensions/MainContentView+Modifiers.swift | 1 - apple/TablePro/Views/Main/MainContentView.swift | 3 --- apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift | 5 ++++- apple/TablePro/Views/Sidebar/FavoritesOutlineNode.swift | 4 +++- apple/changelog.d/3876.changed.md | 1 + 6 files changed, 8 insertions(+), 7 deletions(-) create mode 100644 apple/changelog.d/3876.changed.md diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift index 041c30a137..f05c1dfc92 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift @@ -973,7 +973,6 @@ internal final class MainSplitViewController: NSSplitViewController { windowTitle: windowTitleBinding(for: workspace), windowSubtitle: windowSubtitleBinding(for: workspace), windowRepresentedURL: windowRepresentedURLBinding(for: workspace), - sidebarState: SharedSidebarState.forConnection(session.connection.id), pendingTruncates: sessionBinding(for: workspace, get: { $0.pendingTruncates }, set: { $0.pendingTruncates = $1 }, defaultValue: []), pendingDeletes: sessionBinding(for: workspace, get: { $0.pendingDeletes }, set: { $0.pendingDeletes = $1 }, defaultValue: []), tableOperationOptions: sessionBinding(for: workspace, get: { $0.tableOperationOptions }, set: { $0.tableOperationOptions = $1 }, defaultValue: [:]), diff --git a/apple/TablePro/Views/Main/Extensions/MainContentView+Modifiers.swift b/apple/TablePro/Views/Main/Extensions/MainContentView+Modifiers.swift index 8c80a72b19..148cceb37f 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentView+Modifiers.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentView+Modifiers.swift @@ -21,7 +21,6 @@ import SwiftUI windowTitle: .constant("SQL Query"), windowSubtitle: .constant(""), windowRepresentedURL: .constant(nil), - sidebarState: SharedSidebarState(), pendingTruncates: .constant([]), pendingDeletes: .constant([]), tableOperationOptions: .constant([:]), diff --git a/apple/TablePro/Views/Main/MainContentView.swift b/apple/TablePro/Views/Main/MainContentView.swift index e19b1ee85a..3954b0d3e5 100644 --- a/apple/TablePro/Views/Main/MainContentView.swift +++ b/apple/TablePro/Views/Main/MainContentView.swift @@ -22,7 +22,6 @@ struct MainContentView: View { @Binding var windowSubtitle: String @Binding var windowRepresentedURL: URL? @ObservedObject var schemaService = SchemaService.shared - @ObservedObject var sidebarState: SharedSidebarState @Binding var pendingTruncates: Set @Binding var pendingDeletes: Set @Binding var tableOperationOptions: [DatabaseTreeTableRef: TableOperationOptions] @@ -59,7 +58,6 @@ struct MainContentView: View { windowTitle: Binding, windowSubtitle: Binding, windowRepresentedURL: Binding, - sidebarState: SharedSidebarState, pendingTruncates: Binding>, pendingDeletes: Binding>, tableOperationOptions: Binding<[DatabaseTreeTableRef: TableOperationOptions]>, @@ -74,7 +72,6 @@ struct MainContentView: View { self._windowTitle = windowTitle self._windowSubtitle = windowSubtitle self._windowRepresentedURL = windowRepresentedURL - self.sidebarState = sidebarState self._pendingTruncates = pendingTruncates self._pendingDeletes = pendingDeletes self._tableOperationOptions = tableOperationOptions diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift index a179675a9d..0a8aaa293c 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift @@ -64,7 +64,9 @@ internal struct DatabaseTreeSidePhases: Equatable { } } -final class DatabaseTreeNode: SidebarOutlineNode { +// NSObject so the outline's item lookups hash the pointer: a plain Swift class looks up a Hashable conformance on +// every hash, about a quarter of opening a section of 17,000 tables. +final class DatabaseTreeNode: NSObject, SidebarOutlineNode { enum Status: Equatable { case loading case empty @@ -110,6 +112,7 @@ final class DatabaseTreeNode: SidebarOutlineNode { init(id: String, kind: Kind) { self.id = id self.kind = kind + super.init() } var isExpandable: Bool { diff --git a/apple/TablePro/Views/Sidebar/FavoritesOutlineNode.swift b/apple/TablePro/Views/Sidebar/FavoritesOutlineNode.swift index 0bfa034b9f..150ce581b1 100644 --- a/apple/TablePro/Views/Sidebar/FavoritesOutlineNode.swift +++ b/apple/TablePro/Views/Sidebar/FavoritesOutlineNode.swift @@ -10,7 +10,8 @@ import Foundation /// A reference type because `NSOutlineView` tracks rows by object identity, which is what lets a /// reload keep the expansion and selection a user set. The coordinator caches these by `id` and /// mutates the cached instance rather than building a new one, exactly as the object tree does. -internal final class FavoritesOutlineNode: SidebarOutlineNode { +/// An `NSObject` for the same cheap hashing as `DatabaseTreeNode`. +internal final class FavoritesOutlineNode: NSObject, SidebarOutlineNode { internal enum Kind { case header(String) case databaseEnvironment(FavoriteDatabaseGroup) @@ -26,6 +27,7 @@ internal final class FavoritesOutlineNode: SidebarOutlineNode { internal init(id: String, kind: Kind) { self.id = id self.kind = kind + super.init() } internal var isExpandable: Bool { diff --git a/apple/changelog.d/3876.changed.md b/apple/changelog.d/3876.changed.md new file mode 100644 index 0000000000..f46be23006 --- /dev/null +++ b/apple/changelog.d/3876.changed.md @@ -0,0 +1 @@ +Typing in the sidebar filter no longer redraws the editor and results on every keystroke. From 79a72062d98be4b2ad06044ec34cea9ea41eda49 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:23:46 +0700 Subject: [PATCH 05/85] ci(deps): bump the actions group across 1 directory with 3 updates (#3879) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/build-linux.yml | 16 ++++++++-------- .github/workflows/release.yml | 2 +- .github/workflows/repo-hygiene.yml | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build-linux.yml b/.github/workflows/build-linux.yml index f96745925a..19c94df8fd 100644 --- a/.github/workflows/build-linux.yml +++ b/.github/workflows/build-linux.yml @@ -115,7 +115,7 @@ jobs: components: rustfmt, clippy # Meson points Cargo at _build/cargo-home and _build/cargo-target, # so those are what the cache keeps. - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: workspaces: linux -> _build/cargo-target cache-directories: linux/_build/cargo-home @@ -268,7 +268,7 @@ jobs: - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # master with: toolchain: "1.98" - - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + - uses: taiki-e/install-action@5488ac5a951645ce95062214902ac4b6bc1a1037 # v2.87.32 with: tool: cargo-deny@${{ env.CARGO_DENY_VERSION }},cargo-shear@${{ env.CARGO_SHEAR_VERSION }},cargo-about@${{ env.CARGO_ABOUT_VERSION }},zizmor@${{ env.ZIZMOR_VERSION }} - name: Advisories, bans, licenses and sources @@ -489,11 +489,11 @@ jobs: - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # master with: toolchain: "1.98" - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: workspaces: linux key: ${{ matrix.package }} - - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + - uses: taiki-e/install-action@5488ac5a951645ce95062214902ac4b6bc1a1037 # v2.87.32 with: tool: cargo-nextest@${{ env.CARGO_NEXTEST_VERSION }} - name: Verify docker is available @@ -564,11 +564,11 @@ jobs: - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # master with: toolchain: "1.98" - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: workspaces: linux key: tablepro-session - - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + - uses: taiki-e/install-action@5488ac5a951645ce95062214902ac4b6bc1a1037 # v2.87.32 with: tool: cargo-nextest@${{ env.CARGO_NEXTEST_VERSION }} - name: Verify docker is available @@ -634,11 +634,11 @@ jobs: - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # master with: toolchain: "1.98" - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: workspaces: linux key: tablepro-app - - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + - uses: taiki-e/install-action@5488ac5a951645ce95062214902ac4b6bc1a1037 # v2.87.32 with: tool: cargo-nextest@${{ env.CARGO_NEXTEST_VERSION }} - name: Verify docker is available diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9b1b4d8bf2..bbd31b8db5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -37,7 +37,7 @@ jobs: - name: Set up Python 3.9 id: python - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: "3.9" update-environment: false diff --git a/.github/workflows/repo-hygiene.yml b/.github/workflows/repo-hygiene.yml index 2ecf1f6edf..4b633d147c 100644 --- a/.github/workflows/repo-hygiene.yml +++ b/.github/workflows/repo-hygiene.yml @@ -55,7 +55,7 @@ jobs: # runner's Python, which has PyYAML. - name: Set up Python 3.9 for the release tooling id: python - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: "3.9" update-environment: false From 627c06a60c209997f8f2c28b85ea22bffaa088e2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:42:54 +0700 Subject: [PATCH 06/85] ci(deps): bump zip from 8.6.0 to 9.0.5 in /linux (#3884) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- linux/Cargo.lock | 2 +- linux/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/linux/Cargo.lock b/linux/Cargo.lock index 1714d8d671..39e40f47c2 100644 --- a/linux/Cargo.lock +++ b/linux/Cargo.lock @@ -5636,7 +5636,7 @@ dependencies = [ "tokio-util", "unicode-width", "uuid", - "zip 8.6.0", + "zip 9.0.5", ] [[package]] diff --git a/linux/Cargo.toml b/linux/Cargo.toml index 63737f6de8..3a4816f4c0 100644 --- a/linux/Cargo.toml +++ b/linux/Cargo.toml @@ -93,7 +93,7 @@ unicode-width = "0.2" # the build free of another C dependency. Constant memory lets a sheet # spill its rows to a temporary file instead of holding them. rust_xlsxwriter = { version = "0.99", default-features = false, features = ["chrono", "constant_memory"] } -zip = { version = "8.3", default-features = false, features = ["deflate"] } +zip = { version = "9.0", default-features = false, features = ["deflate"] } futures = "0.3" futures-util = { version = "0.3", default-features = false, features = ["std"] } bytes = "1" From c43fab2b32cdd7b938cc033fa0bb43b7f26edb8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 00:16:00 +0700 Subject: [PATCH 07/85] fix(analytics): record the first successful editor query again (#3886) --- .../MainContentCoordinator+History.swift | 3 ++ .../Views/Main/FirstQueryAnalyticsTests.swift | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 apple/TableProTests/Views/Main/FirstQueryAnalyticsTests.swift diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+History.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+History.swift index 5415915038..8caead141f 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+History.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+History.swift @@ -10,6 +10,9 @@ extension MainContentCoordinator { if #available(macOS 14.0, *) { FeatureTipSignals.editorQueryRan() } + if request.wasSuccessful { + MacAnalyticsProvider.shared.markFirstQueryExecuted() + } } let recorder = services.queryHistoryManager return Task(priority: .utility) { diff --git a/apple/TableProTests/Views/Main/FirstQueryAnalyticsTests.swift b/apple/TableProTests/Views/Main/FirstQueryAnalyticsTests.swift new file mode 100644 index 0000000000..05ae4ddff7 --- /dev/null +++ b/apple/TableProTests/Views/Main/FirstQueryAnalyticsTests.swift @@ -0,0 +1,41 @@ +import Foundation +import Testing + +@testable import TablePro + +/// The heartbeat's first-query timestamp lost its only caller when `DatabaseManager.execute(query:)` +/// was removed, so it stayed nil on every Mac. Recording an editor run's history is where it is set now. +@MainActor +struct FirstQueryAnalyticsTests { + private func request(_ source: QueryHistorySource, succeeded: Bool, connectionId: UUID) -> QueryHistoryRecordRequest { + QueryHistoryRecordRequest( + query: "SELECT 1", + connectionId: connectionId, + databaseName: "db", + databaseType: .postgresql, + source: source, + executionTime: 0.01, + rowCount: 1, + wasSuccessful: succeeded, + errorMessage: succeeded ? nil : "syntax error" + ) + } + + // The mark is written before recordHistory returns its task, so every assertion runs before the + // first suspension and no other suite can touch the shared defaults in between. + @Test("Only a successful editor query records the first-query timestamp") + func successfulEditorQueryMarksFirstQuery() async { + AppStorageEnvironment.shared.defaults.removeObject(forKey: MacAnalyticsProvider.Keys.firstQueryExecutedAt) + let fixture = CoordinatorFixture() + let connectionId = fixture.connection.id + + let failed = fixture.coordinator.recordHistory(request(.editor, succeeded: false, connectionId: connectionId)) + let browsed = fixture.coordinator.recordHistory(request(.tableBrowse, succeeded: true, connectionId: connectionId)) + #expect(MacAnalyticsProvider.shared.firstQueryExecutedAt == nil) + + let ran = fixture.coordinator.recordHistory(request(.editor, succeeded: true, connectionId: connectionId)) + #expect(MacAnalyticsProvider.shared.firstQueryExecutedAt != nil) + + _ = await (failed.value, browsed.value, ran.value) + } +} From 1c2b523ed26a39ee93ff533a466f9fbaa4e78f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 00:35:43 +0700 Subject: [PATCH 08/85] build(linux): bump rust_xlsxwriter to 0.102 so zip 8.6.0 leaves the build (#3885) --- linux/Cargo.lock | 24 +++++------------------ linux/Cargo.toml | 2 +- linux/build-aux/debian/copyright | 4 ++-- linux/crates/core/src/export/xlsx.rs | 2 +- linux/data/licenses/THIRD-PARTY-NOTICE.md | 4 ++-- linux/data/licenses/third-party.json | 8 ++++---- linux/deny.toml | 3 --- 7 files changed, 15 insertions(+), 32 deletions(-) diff --git a/linux/Cargo.lock b/linux/Cargo.lock index 39e40f47c2..75cc32a4cb 100644 --- a/linux/Cargo.lock +++ b/linux/Cargo.lock @@ -3176,7 +3176,7 @@ dependencies = [ "tar", "ureq", "vcpkg", - "zip 9.0.5", + "zip", ] [[package]] @@ -4465,13 +4465,13 @@ dependencies = [ [[package]] name = "rust_xlsxwriter" -version = "0.99.1" +version = "0.102.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34f2d2cda7223168a14fb86bc7411dbc3379bd7d49749ecc53b91ed98585b6b0" +checksum = "6ca3d0e879a578c1313c2800ab0c6c029d38eed18098675e50341a30d32372ed" dependencies = [ "chrono", "tempfile", - "zip 8.6.0", + "zip", ] [[package]] @@ -5636,7 +5636,7 @@ dependencies = [ "tokio-util", "unicode-width", "uuid", - "zip 9.0.5", + "zip", ] [[package]] @@ -7400,20 +7400,6 @@ dependencies = [ "syn 3.0.5", ] -[[package]] -name = "zip" -version = "8.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d04a6b5381502aa6087c94c669499eb1602eb9c5e8198e534de571f7154809b" -dependencies = [ - "crc32fast", - "flate2", - "indexmap 2.14.2", - "memchr", - "typed-path", - "zopfli", -] - [[package]] name = "zip" version = "9.0.5" diff --git a/linux/Cargo.toml b/linux/Cargo.toml index 3a4816f4c0..49e09aac94 100644 --- a/linux/Cargo.toml +++ b/linux/Cargo.toml @@ -92,7 +92,7 @@ unicode-width = "0.2" # No zlib feature: it pulls libz-sys, and the pure-Rust deflate keeps # the build free of another C dependency. Constant memory lets a sheet # spill its rows to a temporary file instead of holding them. -rust_xlsxwriter = { version = "0.99", default-features = false, features = ["chrono", "constant_memory"] } +rust_xlsxwriter = { version = "0.102", default-features = false, features = ["chrono", "constant_memory"] } zip = { version = "9.0", default-features = false, features = ["deflate"] } futures = "0.3" futures-util = { version = "0.3", default-features = false, features = ["std"] } diff --git a/linux/build-aux/debian/copyright b/linux/build-aux/debian/copyright index d48e3c1655..ed284968cd 100644 --- a/linux/build-aux/debian/copyright +++ b/linux/build-aux/debian/copyright @@ -6321,7 +6321,7 @@ License: Apache-2.0 and Expat OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -Files: vendor/rust_xlsxwriter-0.99.1/* +Files: vendor/rust_xlsxwriter-0.102.0/* Copyright: 2022-2026 John McNamara License: Apache-2.0 or Expat MIT License @@ -8262,7 +8262,7 @@ License: Apache-2.0 or Expat IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -Files: vendor/zip-8.6.0/* +Files: vendor/zip-9.0.5/* Copyright: 2014 Mathijs van de Nes License: Expat The MIT License (MIT) diff --git a/linux/crates/core/src/export/xlsx.rs b/linux/crates/core/src/export/xlsx.rs index 6f551a9cdd..5ee065d76d 100644 --- a/linux/crates/core/src/export/xlsx.rs +++ b/linux/crates/core/src/export/xlsx.rs @@ -76,7 +76,7 @@ impl XlsxSheetWriter { }); } let mut workbook = Workbook::new(); - let sheet = workbook.add_worksheet_with_constant_memory(); + let sheet = workbook.add_worksheet_with_constant_memory().map_err(xlsx_error)?; sheet.set_name(sheet_title(sheet_name)).map_err(xlsx_error)?; let header = Format::new().set_bold(); for (index, column) in columns.iter().enumerate() { diff --git a/linux/data/licenses/THIRD-PARTY-NOTICE.md b/linux/data/licenses/THIRD-PARTY-NOTICE.md index c2555623ea..69686a7235 100644 --- a/linux/data/licenses/THIRD-PARTY-NOTICE.md +++ b/linux/data/licenses/THIRD-PARTY-NOTICE.md @@ -8615,7 +8615,7 @@ SOFTWARE. Used by: -- rust_xlsxwriter 0.99.1 +- rust_xlsxwriter 0.102.0 ~~~~text MIT License @@ -9082,7 +9082,7 @@ SOFTWARE. Used by: - tracing-core 0.1.36 -- zip 8.6.0 +- zip 9.0.5 ~~~~text The MIT License (MIT) diff --git a/linux/data/licenses/third-party.json b/linux/data/licenses/third-party.json index 6c14ece54b..0a05162944 100644 --- a/linux/data/licenses/third-party.json +++ b/linux/data/licenses/third-party.json @@ -1524,7 +1524,7 @@ "name": "MIT License", "text": "MIT License\n\nCopyright 2022-2026 John McNamara \n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n", "used_by": [ - { "crate": { "name": "rust_xlsxwriter", "version": "0.99.1" } } + { "crate": { "name": "rust_xlsxwriter", "version": "0.102.0" } } ] }, { @@ -1718,7 +1718,7 @@ "text": "The MIT License (MIT)\n\nCopyright (c) 2014 Mathijs van de Nes\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n", "used_by": [ { "crate": { "name": "tracing-core", "version": "0.1.36" } }, - { "crate": { "name": "zip", "version": "8.6.0" } } + { "crate": { "name": "zip", "version": "9.0.5" } } ] }, { @@ -2558,7 +2558,7 @@ { "name": "resolv-conf", "version": "0.7.6", "license": "MIT OR Apache-2.0" }, { "name": "rmcp", "version": "3.6.0", "license": "Apache-2.0 AND MIT" }, { "name": "rsa", "version": "0.10.0-rc.18", "license": "MIT OR Apache-2.0" }, - { "name": "rust_xlsxwriter", "version": "0.99.1", "license": "MIT OR Apache-2.0" }, + { "name": "rust_xlsxwriter", "version": "0.102.0", "license": "MIT OR Apache-2.0" }, { "name": "rustc_version", "version": "0.4.1", "license": "MIT OR Apache-2.0" }, { "name": "rustc_version_runtime", "version": "0.3.0", "license": "MIT" }, { "name": "rustix", "version": "0.38.44", "license": "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT" }, @@ -2700,7 +2700,7 @@ { "name": "zerotrie", "version": "0.2.5", "license": "Unicode-3.0" }, { "name": "zerovec", "version": "0.11.8", "license": "Unicode-3.0" }, { "name": "zerovec-derive", "version": "0.11.6", "license": "Unicode-3.0" }, - { "name": "zip", "version": "8.6.0", "license": "MIT" }, + { "name": "zip", "version": "9.0.5", "license": "MIT" }, { "name": "zlib-rs", "version": "0.6.7", "license": "Zlib" }, { "name": "zmij", "version": "1.0.23", "license": "MIT" }, { "name": "zopfli", "version": "0.8.3", "license": "Apache-2.0" }, diff --git a/linux/deny.toml b/linux/deny.toml index a0809ee733..fee74fc4d7 100644 --- a/linux/deny.toml +++ b/linux/deny.toml @@ -9,9 +9,6 @@ yanked = "deny" unmaintained = "workspace" ignore = [ { id = "RUSTSEC-2023-0071", reason = "rsa is reached only through sqlx-mysql, which encrypts the MySQL password with the server's public key; no private-key operation runs in process, and OpenSSH signs SSH identities. The rsa ban below holds that scope. See docs/decisions/0009-crypto-provider-and-sources.md." }, - { id = "RUSTSEC-2026-0367", reason = "zip 8.6.0 comes only through rust_xlsxwriter 0.99.1, which writes archives and never reads one; the advisory is in ZipArchive::extract and ZipStreamReader::extract, which nothing here calls (core's export test reads entries by name). Drop it once a rust_xlsxwriter release takes zip 9 (https://github.com/jmcnamara/rust_xlsxwriter/issues/205)." }, - { id = "RUSTSEC-2026-0368", reason = "zip 8.6.0 comes only through rust_xlsxwriter 0.99.1, which writes archives and never reads one; the advisory is in ZipArchive::extract and ZipStreamReader::extract, which nothing here calls (core's export test reads entries by name). Drop it once a rust_xlsxwriter release takes zip 9 (https://github.com/jmcnamara/rust_xlsxwriter/issues/205)." }, - { id = "RUSTSEC-2026-0369", reason = "zip 8.6.0 comes only through rust_xlsxwriter 0.99.1, which writes archives and never reads one; the advisory is in ZipArchive::extract and ZipStreamReader::extract, which nothing here calls (core's export test reads entries by name). Drop it once a rust_xlsxwriter release takes zip 9 (https://github.com/jmcnamara/rust_xlsxwriter/issues/205)." }, ] [licenses] From 83edbf0ebef287af3ae59540c8771a458d84687d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 01:27:37 +0700 Subject: [PATCH 09/85] docs: add a trademark policy for the TablePro name and icon (#3887) --- README.ko.md | 2 +- README.md | 2 +- README.vi.md | 2 +- README.zh.md | 2 +- TRADEMARKS.md | 24 ++++++++++++++++++++++++ 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 TRADEMARKS.md diff --git a/README.ko.md b/README.ko.md index d066b55520..a976414762 100644 --- a/README.ko.md +++ b/README.ko.md @@ -171,4 +171,4 @@ TablePro에 기여해 주신 모든 분께 감사드립니다. ## 라이선스 -이 프로젝트는 [GNU Affero General Public License v3.0(AGPLv3)](LICENSE)에 따라 라이선스가 부여됩니다. +이 프로젝트는 [GNU Affero General Public License v3.0(AGPLv3)](LICENSE)에 따라 라이선스가 부여됩니다. TablePro 이름과 아이콘은 이 라이선스에 포함되지 않으며, 사용 방법은 [TRADEMARKS.md](TRADEMARKS.md)에 있습니다. diff --git a/README.md b/README.md index 3c07e7197e..c2ed042266 100644 --- a/README.md +++ b/README.md @@ -182,5 +182,5 @@ Want to join them? Read [CONTRIBUTING.md](CONTRIBUTING.md). ## License -This project is licensed under the [GNU Affero General Public License v3.0 (AGPLv3)](LICENSE). +This project is licensed under the [GNU Affero General Public License v3.0 (AGPLv3)](LICENSE). The TablePro name and icon are not part of that license: [TRADEMARKS.md](TRADEMARKS.md) says how you can use them. diff --git a/README.vi.md b/README.vi.md index 4ea88ccfab..3886e7a2c8 100644 --- a/README.vi.md +++ b/README.vi.md @@ -146,5 +146,5 @@ Muốn tham gia? Đọc [CONTRIBUTING.md](CONTRIBUTING.md). ## Bản quyền -Dự án này cấp phép theo [GNU Affero General Public License v3.0 (AGPLv3)](LICENSE). +Dự án này cấp phép theo [GNU Affero General Public License v3.0 (AGPLv3)](LICENSE). Tên và biểu tượng TablePro không thuộc giấy phép đó: cách dùng được ghi trong [TRADEMARKS.md](TRADEMARKS.md). diff --git a/README.zh.md b/README.zh.md index 7fb23a2c33..fd8d3c0fdd 100644 --- a/README.zh.md +++ b/README.zh.md @@ -146,5 +146,5 @@ TablePro 由用户和以下赞助者支持。[成为赞助者](https://tablepro. ## 许可证 -本项目采用 [GNU Affero General Public License v3.0 (AGPLv3)](LICENSE) 许可。 +本项目采用 [GNU Affero General Public License v3.0 (AGPLv3)](LICENSE) 许可。TablePro 名称和图标不在该许可范围内,使用方式见 [TRADEMARKS.md](TRADEMARKS.md)。 diff --git a/TRADEMARKS.md b/TRADEMARKS.md new file mode 100644 index 0000000000..35e46b4862 --- /dev/null +++ b/TRADEMARKS.md @@ -0,0 +1,24 @@ +# Trademark policy + +The code in this repository is licensed under the [AGPLv3](LICENSE). That license covers the code. It does not cover the name **TablePro** or the TablePro icon and logo, which identify the builds this project publishes. Section 7(e) of the AGPL lets a project keep those out of the license, and this file says what you can do with them. + +The aim is simple: anyone who downloads something called TablePro gets the app this project ships, not a build someone else changed. + +## What you can do + +- Build TablePro from this source for your own use, changed or not. The AGPL allows that, including a build with the license check removed. +- Redistribute an official release unchanged, for example through a package manager, under the name TablePro. +- Package the unchanged source for a Linux distribution or a package manager under the name TablePro, with only the changes packaging needs. +- Say that your work is based on TablePro, works with TablePro, or is a fork of TablePro, in plain words that do not suggest it is TablePro. +- Use the name and icon to write about TablePro, review it, teach it, or link to it. + +## What needs a different name and icon + +- A build with any other change, such as new or removed features, a removed license check, or other branding. Rename it and replace the icon before you share it. +- A product, company, domain, app store listing, or package name that people could take for TablePro or for something made by this project. +- Anything that suggests this project endorses, sponsors, or supports your work when it does not. +- Selling copies, or builds with the paid features unlocked, under the TablePro name. + +## Questions + +For anything this file does not cover, or to ask for permission, write to [hello@tablepro.app](mailto:hello@tablepro.app). From 7cdfa2d710084ddace89b4b98868317e3852a3b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 01:49:53 +0700 Subject: [PATCH 10/85] perf(quick-switcher): cut the main-thread stalls of opening Open Quickly (#3888) --- .../ViewModels/QuickSwitcherViewModel.swift | 39 +++++++++++++- ...MainContentCoordinator+QuickSwitcher.swift | 11 +++- .../QuickSwitcher/QuickSwitcherPanel.swift | 41 +++++++++++---- .../QuickSwitcherPanelView.swift | 51 +++++++++++++++---- .../DatabaseTreeOutlineCoordinator.swift | 3 +- .../Sidebar/DatabaseTreeRenderInputs.swift | 7 ++- .../QuickSwitcherViewModelTests.swift | 18 +++++++ .../QuickSwitcherPanelControllerTests.swift | 37 ++++++++++++++ .../Sidebar/DatabaseTreeReconcileTests.swift | 12 +++-- apple/changelog.d/3888.changed.1.md | 1 + apple/changelog.d/3888.changed.md | 1 + 11 files changed, 188 insertions(+), 33 deletions(-) create mode 100644 apple/changelog.d/3888.changed.1.md create mode 100644 apple/changelog.d/3888.changed.md diff --git a/apple/TablePro/ViewModels/QuickSwitcherViewModel.swift b/apple/TablePro/ViewModels/QuickSwitcherViewModel.swift index dc065991a3..0fbec7e3a0 100644 --- a/apple/TablePro/ViewModels/QuickSwitcherViewModel.swift +++ b/apple/TablePro/ViewModels/QuickSwitcherViewModel.swift @@ -121,6 +121,8 @@ internal final class QuickSwitcherViewModel: ObservableObject { private var activeCrossConnectionQueryLoadId = UUID() private var loadedCrossConnectionVersion: CrossConnectionCatalogVersion? private var loadedCrossConnectionQueryVersion: CrossConnectionQueryVersion? + // The retained Open Quickly panel keeps showing this model until its next presentation. + private var isReleased = false @Published private(set) var groups: [Group] = [] @Published private(set) var isLoading = true @@ -252,6 +254,8 @@ internal final class QuickSwitcherViewModel: ObservableObject { openTables: Set = [], browseSchema: String? = nil ) async { + // A task that starts after the panel already closed would set up the observers again. + guard !isReleased, !Task.isCancelled else { return } isLoading = true let loadId = UUID() @@ -520,7 +524,7 @@ internal final class QuickSwitcherViewModel: ObservableObject { /// task that drives this, and the refresh it just ran has already moved the version, so /// the panel refreshes that connection forever. func loadCrossConnectionItems() async { - guard scope.usesCrossConnectionCatalog else { return } + guard !isReleased, scope.usesCrossConnectionCatalog else { return } guard loadedCrossConnectionVersion != crossConnectionCatalogVersion else { return } let loadId = UUID() @@ -554,7 +558,7 @@ internal final class QuickSwitcherViewModel: ObservableObject { } func loadCrossConnectionQueryItems() async { - guard scope.usesCrossConnectionQueries else { return } + guard !isReleased, scope.usesCrossConnectionQueries else { return } let version = crossConnectionQueryVersion guard loadedCrossConnectionQueryVersion != version else { return } @@ -716,6 +720,36 @@ internal final class QuickSwitcherViewModel: ObservableObject { return covered } + /// Run when the panel closes. Loads still in flight see the bumped ids and drop what they bring. + func releaseResults() { + isReleased = true + activeLoadId = UUID() + activeCrossConnectionLoadId = UUID() + activeCrossConnectionQueryLoadId = UUID() + listingRequestId = nil + tableSourceObservations = [] + tableSource = nil + tableInputs = nil + pendingTableBuild?.task.cancel() + pendingTableBuild = nil + filterTask?.cancel() + filterTask = nil + baseItems = nil + tableItems = [] + tableItemKeys = [] + allItems = [] + crossConnectionItems = [] + crossConnectionQueryItems = [] + groups = [] + selectedItemId = nil + isFiltering = false + isLoadingBrowseCatalog = false + isListingInFlight = false + isSharedListingLoading = false + isLoadingCrossConnections = false + isLoadingCrossConnectionQueries = false + } + func canOpenStructure(_ item: QuickSwitcherItem) -> Bool { guard let target = item.target else { return true } return target.connectionId == connectionId @@ -762,6 +796,7 @@ internal final class QuickSwitcherViewModel: ObservableObject { /// build off the main actor so neither can stall the panel while it is being typed into. private func scheduleFilter(debounced: Bool) { filterTask?.cancel() + guard !isReleased else { return } let query = searchText.trimmingCharacters(in: .whitespaces) let items = scopedItems() let scope = scope diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QuickSwitcher.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QuickSwitcher.swift index 44ec500a21..5e1c694871 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QuickSwitcher.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QuickSwitcher.swift @@ -39,15 +39,22 @@ extension MainContentCoordinator { ) } ) + let viewModel = QuickSwitcherViewModel(connectionId: connectionId) let panelView = QuickSwitcherPanelView( - connectionId: connectionId, + viewModel: viewModel, databaseType: connection.type, openTables: openTables, browseSchema: browseSchema, onSelect: { [weak self] item, intent in self?.handleQuickSwitcherSelection(item, intent: intent) }, onDismiss: { [weak self] in self?.quickSwitcherPanel?.dismiss() } ) - quickSwitcherPanel.present(panelView, over: contentWindow, identity: Self.openQuicklyPanelIdentity) + quickSwitcherPanel.present( + panelView, + over: contentWindow, + identity: Self.openQuicklyPanelIdentity, + retainsPanel: true, + onClose: { [weak viewModel] in viewModel?.releaseResults() } + ) InteractionTracer.shared.end( .quickOpenPanel, key: connectionId.uuidString, after: quickSwitcherPanel.presentedWindow ) diff --git a/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanel.swift b/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanel.swift index c9fff03a8d..ce96a30661 100644 --- a/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanel.swift +++ b/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanel.swift @@ -114,7 +114,13 @@ internal final class QuickSwitcherPanelController: NSObject, NSWindowDelegate { private static let topOffsetRatio: CGFloat = 0.20 + private struct RetainedPanel { + let panel: QuickSwitcherPanel + let hostingController: NSHostingController + } + private var panel: QuickSwitcherPanel? + private var retainedPanels: [String: RetainedPanel] = [:] private var anchor: Anchor? private var presentedIdentity: String? private var onClose: (() -> Void)? @@ -132,32 +138,43 @@ internal final class QuickSwitcherPanelController: NSObject, NSWindowDelegate { /// `onClose` runs however the panel goes, including when another presentation replaces it, /// which is how a caller that keeps state of its own alongside the panel learns to drop it. + /// `retainsPanel` keeps the window and view tree for the next presentation under the same identity, + /// so content must take its model from the caller: a `@StateObject` would survive the root swap. func present( _ content: some View, over parentWindow: NSWindow?, identity: String? = nil, focus: QuickSwitcherPanelFocus = .key, accessibilityIdentifier: String = "quick-switcher-panel", + retainsPanel: Bool = false, onClose: (() -> Void)? = nil ) { dismiss() presentedIdentity = identity self.onClose = onClose - let sizeReportingContent = content.onGeometryChange(for: CGSize.self) { proxy in + let root = AnyView(content.onGeometryChange(for: CGSize.self) { proxy in proxy.size } action: { [weak self] size in self?.contentSizeDidChange(size) + }) + let panel: QuickSwitcherPanel + if retainsPanel, let identity, let retained = retainedPanels[identity] { + retained.hostingController.rootView = root + panel = retained.panel + } else { + let hostingController = NSHostingController(rootView: root) + panel = QuickSwitcherPanel( + hostingController: hostingController, + surfaceCornerRadius: QuickSwitcherMetrics.cornerRadius, + focus: focus, + accessibilityIdentifier: accessibilityIdentifier + ) + panel.delegate = self + if retainsPanel, let identity { + retainedPanels[identity] = RetainedPanel(panel: panel, hostingController: hostingController) + } } - let hostingController = NSHostingController(rootView: sizeReportingContent) - - let panel = QuickSwitcherPanel( - hostingController: hostingController, - surfaceCornerRadius: QuickSwitcherMetrics.cornerRadius, - focus: focus, - accessibilityIdentifier: accessibilityIdentifier - ) - panel.delegate = self self.panel = panel let reference = parentWindow?.frame @@ -182,7 +199,9 @@ internal final class QuickSwitcherPanelController: NSObject, NSWindowDelegate { func windowWillClose(_ notification: Notification) { panel?.keyCommandHandler = nil - panel?.contentViewController = nil + if let panel, !retainedPanels.values.contains(where: { $0.panel === panel }) { + panel.contentViewController = nil + } panel = nil anchor = nil presentedIdentity = nil diff --git a/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanelView.swift b/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanelView.swift index 4cfc871dd8..e2a2721207 100644 --- a/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanelView.swift +++ b/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanelView.swift @@ -7,30 +7,28 @@ import AppKit import SwiftUI struct QuickSwitcherPanelView: View { - let connectionId: UUID let databaseType: DatabaseType let openTables: Set let browseSchema: String? let onSelect: (QuickSwitcherItem, QuickSwitcherCommitIntent) -> Void let onDismiss: () -> Void - @StateObject private var viewModel: QuickSwitcherViewModel + @ObservedObject private var viewModel: QuickSwitcherViewModel init( - connectionId: UUID, + viewModel: QuickSwitcherViewModel, databaseType: DatabaseType, openTables: Set = [], browseSchema: String? = nil, onSelect: @escaping (QuickSwitcherItem, QuickSwitcherCommitIntent) -> Void, onDismiss: @escaping () -> Void ) { - self.connectionId = connectionId self.databaseType = databaseType self.openTables = openTables self.browseSchema = browseSchema self.onSelect = onSelect self.onDismiss = onDismiss - self._viewModel = StateObject(wrappedValue: QuickSwitcherViewModel(connectionId: connectionId)) + self.viewModel = viewModel } var body: some View { @@ -39,17 +37,17 @@ struct QuickSwitcherPanelView: View { onSelect(item, intent) onDismiss() } - .task { + .task(id: ObjectIdentifier(viewModel)) { await viewModel.loadItems( databaseType: databaseType, openTables: openTables, browseSchema: browseSchema ) } - .task(id: viewModel.crossConnectionLoadVersion) { + .task(id: ModelTaskKey(model: ObjectIdentifier(viewModel), version: viewModel.crossConnectionLoadVersion)) { await viewModel.loadCrossConnectionItems() } - .task(id: viewModel.crossConnectionQueryLoadVersion) { + .task(id: ModelTaskKey(model: ObjectIdentifier(viewModel), version: viewModel.crossConnectionQueryLoadVersion)) { await viewModel.loadCrossConnectionQueryItems() } .onReceive(AppEvents.shared.queryHistoryDidUpdate) { _ in @@ -61,6 +59,39 @@ struct QuickSwitcherPanelView: View { } } +// The retained panel keeps this view and swaps its model, so each load is keyed by the model as well. +private struct ModelTaskKey: Equatable { + let model: ObjectIdentifier + let version: Version +} + +// Installed on every update, not only when joining the window: the retained panel swaps in a new model. +private struct PanelKeyCommandInstaller: NSViewRepresentable { + let handler: (NSEvent) -> Bool + + func makeNSView(context: Context) -> InstallerView { + InstallerView() + } + + func updateNSView(_ nsView: InstallerView, context: Context) { + nsView.handler = handler + nsView.install() + } + + final class InstallerView: NSView { + var handler: ((NSEvent) -> Bool)? + + override func viewDidMoveToWindow() { + super.viewDidMoveToWindow() + install() + } + + func install() { + (window as? QuickSwitcherPanel)?.keyCommandHandler = handler + } + } +} + struct QuickSwitcherPanelContent: View { @Environment(\.colorSchemeContrast) private var colorSchemeContrast @@ -89,9 +120,7 @@ struct QuickSwitcherPanelContent: View { .quickSwitcherSurface(cornerRadius: QuickSwitcherMetrics.cornerRadius) } .background { - WindowAccessor { window in - (window as? QuickSwitcherPanel)?.keyCommandHandler = handleKeyCommand - } + PanelKeyCommandInstaller(handler: handleKeyCommand) } } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift index 593654982c..af9b8bcbac 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift @@ -300,7 +300,8 @@ final class DatabaseTreeOutlineCoordinator: NSObject, NSTextFieldDelegate { schemaService: schemaService, temporaryTables: temporaryTables, sidebarState: sidebarState, - rootShape: rootShape + rootShape: rootShape, + searchText: searchText ) } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift index c3560b069a..6dbbfc1048 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift @@ -97,7 +97,8 @@ struct DatabaseTreeRenderInputs: Equatable { schemaService: SchemaService, temporaryTables: TemporaryTableStore, sidebarState: SharedSidebarState?, - rootShape: SidebarRootShape + rootShape: SidebarRootShape, + searchText: String ) { let ownsObjects = { (key: ObjectsKey) in key.connectionId == connectionId } let tables = Self.slice(treeMetadata.tablesState, where: ownsObjects) { Rows($0) } @@ -114,7 +115,9 @@ struct DatabaseTreeRenderInputs: Equatable { ) } var listings: [DatabaseKey: Listing] = [:] - for (key, state) in treeMetadata.allSchemaTablesState where key.connectionId == connectionId { + // The outline reads the listing only under a search, and Open Quickly loads it with none: landing + // then reloaded every row. A search starting refreshes the outline anyway. + for (key, state) in treeMetadata.allSchemaTablesState where key.connectionId == connectionId && !searchText.isEmpty { let listing = Listing( state, covered: treeMetadata.allSchemaTablesSelection(connectionId: connectionId, database: key.database) ) diff --git a/apple/TableProTests/ViewModels/QuickSwitcherViewModelTests.swift b/apple/TableProTests/ViewModels/QuickSwitcherViewModelTests.swift index e08caec4f6..a5bc1c9767 100644 --- a/apple/TableProTests/ViewModels/QuickSwitcherViewModelTests.swift +++ b/apple/TableProTests/ViewModels/QuickSwitcherViewModelTests.swift @@ -1340,6 +1340,24 @@ struct QuickSwitcherViewModelTests { #expect(groups.isEmpty) } + @Test("A released model drops its results and ignores a later search") + func releasedModelDropsResults() async { + let vm = makeViewModel(items: sampleItems()) + vm.scope = .tables + await vm.flushPendingFilter() + #expect(!vm.groups.isEmpty) + + vm.releaseResults() + await vm.loadItems(databaseType: .mysql) + vm.searchText = "users" + await vm.flushPendingFilter() + + #expect(vm.allItems.isEmpty) + #expect(vm.groups.isEmpty) + #expect(vm.isFiltering == false) + #expect(vm.selectedItemId == nil) + } + @Test("A scope change the next one superseded stops listing and returns nothing") func cancelledBrowseStopsListing() async { let items = tableItems((0..<5_000).map { "orders_\($0)" }) diff --git a/apple/TableProTests/Views/QuickSwitcherPanelControllerTests.swift b/apple/TableProTests/Views/QuickSwitcherPanelControllerTests.swift index 399065e871..aeda5bd07c 100644 --- a/apple/TableProTests/Views/QuickSwitcherPanelControllerTests.swift +++ b/apple/TableProTests/Views/QuickSwitcherPanelControllerTests.swift @@ -182,6 +182,43 @@ struct QuickSwitcherPanelControllerTests { try await waitUntil { released == nil } } + @Test("A retained panel comes back as the same window, answering for the model it was given last") + func retainedPanelSwapsItsModel() async throws { + let controller = QuickSwitcherPanelController() + defer { controller.dismiss() } + weak var released: QuickSwitcherViewModel? + weak var firstPanel: QuickSwitcherPanel? + do { + let viewModel = QuickSwitcherViewModel(connectionId: UUID()) + released = viewModel + controller.present( + QuickSwitcherPanelContent(viewModel: viewModel) { _, _ in }, + over: nil, + identity: "open-quickly", + retainsPanel: true + ) + let panel = try #require(controller.presentedWindow as? QuickSwitcherPanel) + firstPanel = panel + try await waitUntil { panel.keyCommandHandler != nil } + } + controller.dismiss() + + let viewModel = QuickSwitcherViewModel(connectionId: UUID()) + controller.present( + QuickSwitcherPanelContent(viewModel: viewModel) { _, _ in }, + over: nil, + identity: "open-quickly", + retainsPanel: true + ) + let panel = try #require(controller.presentedWindow as? QuickSwitcherPanel) + #expect(panel === firstPanel) + try await waitUntil { panel.keyCommandHandler != nil } + try await waitUntil { released == nil } + + #expect(panel.performKeyEquivalent(with: try Self.key("2", modifiers: .command, window: panel))) + #expect(viewModel.scope == QuickSwitcherScope.allCases[1]) + } + @Test("The panel answers its own commands ahead of the search field and passes the rest on") func panelAnswersItsCommands() async throws { let controller = QuickSwitcherPanelController() diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeReconcileTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeReconcileTests.swift index 7711d8e089..7446b88e1c 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeReconcileTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeReconcileTests.swift @@ -162,7 +162,7 @@ struct DatabaseTreeReconcileTests { await tearDown(fixture) } - @Test("An all-schema listing starting to load changes nothing the outline draws, and its rows landing do") + @Test("An all-schema listing starting to load changes nothing the outline draws, and its rows landing do only under a search") func listingStartingToLoadIsNoChange() async throws { let connection = TestFixtures.makeConnection(database: "shop", type: .pglite) let driver = MockDatabaseDriver(connection: connection) @@ -172,17 +172,19 @@ struct DatabaseTreeReconcileTests { session.status = .connected DatabaseManager.shared.injectSession(session, for: connection.id) let service = DatabaseTreeMetadataService.shared - func inputs() -> DatabaseTreeRenderInputs { + func inputs(searchText: String = "time") -> DatabaseTreeRenderInputs { DatabaseTreeRenderInputs( connectionId: connection.id, treeMetadata: service, schemaService: .shared, temporaryTables: .shared, sidebarState: nil, - rootShape: .flat + rootShape: .flat, + searchText: searchText ) } let unread = inputs() + let unsearched = inputs(searchText: "") let load = Task { await service.loadAllSchemaTables(connectionId: connection.id, database: "shop") } try await waitUntil { @@ -196,6 +198,7 @@ struct DatabaseTreeReconcileTests { await load.value #expect(service.allSchemaTablesLoadState(connectionId: connection.id, database: "shop").value != nil) #expect(inputs() != unread) + #expect(inputs(searchText: "") == unsearched) await service.handleDisconnect(connectionId: connection.id) DatabaseManager.shared.removeSession(for: connection.id) } @@ -216,7 +219,8 @@ struct DatabaseTreeReconcileTests { schemaService: .shared, temporaryTables: .shared, sidebarState: nil, - rootShape: shape + rootShape: shape, + searchText: "" ) } let flat = inputs(.flat) diff --git a/apple/changelog.d/3888.changed.1.md b/apple/changelog.d/3888.changed.1.md new file mode 100644 index 0000000000..afd7f77347 --- /dev/null +++ b/apple/changelog.d/3888.changed.1.md @@ -0,0 +1 @@ +Open Quickly no longer redraws the whole sidebar when it finishes listing the tables of every schema. diff --git a/apple/changelog.d/3888.changed.md b/apple/changelog.d/3888.changed.md new file mode 100644 index 0000000000..b871197013 --- /dev/null +++ b/apple/changelog.d/3888.changed.md @@ -0,0 +1 @@ +Open Quickly opens faster after the first time in a window. From 1a80a0bca16efd729520e17769a07cb597f17791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 05:36:08 +0700 Subject: [PATCH 11/85] perf(sidebar): group the schema listing once per listing and draw Return as a symbol (#3889) --- .../QuickSwitcherPanelView.swift | 8 +- .../QuickSwitcherRowChrome.swift | 37 +++++--- .../Views/Results/ColumnJumpPanelView.swift | 4 +- .../Views/Sidebar/DatabaseTreeFilter.swift | 86 +++++++++++++++---- .../Sidebar/DatabaseTreeFilterMemoTests.swift | 23 +++++ .../Sidebar/DatabaseTreeFilterTests.swift | 22 +++++ apple/changelog.d/3889.changed.1.md | 1 + apple/changelog.d/3889.changed.md | 1 + 8 files changed, 151 insertions(+), 31 deletions(-) create mode 100644 apple/changelog.d/3889.changed.1.md create mode 100644 apple/changelog.d/3889.changed.md diff --git a/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanelView.swift b/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanelView.swift index e2a2721207..73b026d8f4 100644 --- a/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanelView.swift +++ b/apple/TablePro/Views/QuickSwitcher/QuickSwitcherPanelView.swift @@ -398,14 +398,14 @@ struct QuickSwitcherPanelContent: View { /// on screen saying so, that first press reads as a key that did nothing. private var footer: some View { HStack(spacing: 14) { - QuickSwitcherKeyHint(symbol: "\u{21A9}", label: String(localized: "Open")) - QuickSwitcherKeyHint(symbol: "\u{2318}\u{21A9}", label: String(localized: "New Tab")) + QuickSwitcherKeyHint(keys: [.returnKey], label: String(localized: "Open")) + QuickSwitcherKeyHint(keys: [.text("\u{2318}"), .returnKey], label: String(localized: "New Tab")) Spacer(minLength: 0) - QuickSwitcherKeyHint(symbol: "\u{2318}1\u{2013}5", label: String(localized: "Scope")) + QuickSwitcherKeyHint(keys: [.text("\u{2318}1\u{2013}5")], label: String(localized: "Scope")) QuickSwitcherKeyHint( - symbol: "\u{238B}", + keys: [.text("\u{238B}")], label: escapeDismissesPanel ? String(localized: "Close") : String(localized: "Clear") ) } diff --git a/apple/TablePro/Views/QuickSwitcher/QuickSwitcherRowChrome.swift b/apple/TablePro/Views/QuickSwitcher/QuickSwitcherRowChrome.swift index 6aa2616a88..a40ef8b2b6 100644 --- a/apple/TablePro/Views/QuickSwitcher/QuickSwitcherRowChrome.swift +++ b/apple/TablePro/Views/QuickSwitcher/QuickSwitcherRowChrome.swift @@ -22,20 +22,37 @@ internal enum QuickSwitcherRowChrome { } internal struct QuickSwitcherKeyHint: View { - let symbol: String + enum Glyph { + case text(String) + case symbol(String) + + // The system font has no U+21A9: its first draw in a process loads a fallback font, about 10 ms. + static let returnKey = Glyph.symbol("return") + } + + let keys: [Glyph] let label: String var body: some View { HStack(spacing: 4) { - Text(symbol) - .font(.caption.weight(.medium)) - .foregroundStyle(.secondary) - .padding(.horizontal, 5) - .frame(minWidth: 20, minHeight: 17) - .background( - RoundedRectangle(cornerRadius: 4, style: .continuous) - .fill(Color(nsColor: .quaternaryFill)) - ) + HStack(spacing: 1) { + ForEach(keys.indices, id: \.self) { index in + switch keys[index] { + case .text(let text): + Text(text) + case .symbol(let name): + Image(systemName: name) + } + } + } + .font(.caption.weight(.medium)) + .foregroundStyle(.secondary) + .padding(.horizontal, 5) + .frame(minWidth: 20, minHeight: 17) + .background( + RoundedRectangle(cornerRadius: 4, style: .continuous) + .fill(Color(nsColor: .quaternaryFill)) + ) Text(label) .font(.caption) .foregroundStyle(.secondary) diff --git a/apple/TablePro/Views/Results/ColumnJumpPanelView.swift b/apple/TablePro/Views/Results/ColumnJumpPanelView.swift index 90dd8bc4df..d5404f6b96 100644 --- a/apple/TablePro/Views/Results/ColumnJumpPanelView.swift +++ b/apple/TablePro/Views/Results/ColumnJumpPanelView.swift @@ -241,12 +241,12 @@ struct ColumnJumpPanelContent: View { private var footer: some View { HStack(spacing: 14) { - QuickSwitcherKeyHint(symbol: "\u{21A9}", label: String(localized: "Jump")) + QuickSwitcherKeyHint(keys: [.returnKey], label: String(localized: "Jump")) Spacer(minLength: 0) QuickSwitcherKeyHint( - symbol: "\u{238B}", + keys: [.text("\u{238B}")], label: escapeDismissesPanel ? String(localized: "Close") : String(localized: "Clear") ) } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift index b3b5bb2dcb..adbf4d9282 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift @@ -175,7 +175,23 @@ enum DatabaseTreeFilter { /// each object's own schema, and the database only when the search names one. static func filteredTables(_ tables: [TableInfo], searchText: String, database: String? = nil) -> [TableInfo] { let matched = SidebarSearch(searchText).ranked(tables, name: { $0.name }, database: database, schema: { $0.schema }) - return deduplicated(matched, by: \.id) + return deduplicated(matched, by: TableIdentity.init) + } + + // The fields `TableInfo.id` is built from, without escaping and joining a string for every row. + // The id reads an empty schema as none, so the key does too. + private struct TableIdentity: Hashable { + let name: String + let schema: String? + let type: TableInfo.TableType + let isTemporary: Bool + + init(_ table: TableInfo) { + name = table.name + schema = table.schema?.isEmpty == false ? table.schema : nil + type = table.type + isTemporary = table.isTemporary + } } static func filteredRoutines( @@ -390,23 +406,27 @@ enum DatabaseTreeFilter { let rowsBySchema: [String: [TableInfo]] init(listing: CatalogTableListing.Result, database: String, searchText: String, covered: Set? = nil) { + self.init( + groups: SchemaListingGroups(tables: listing.tables), + unlisted: listing.unlistedSchemas, + database: database, + searchText: searchText, + covered: covered + ) + } + + init(groups: SchemaListingGroups, unlisted: Set, database: String, searchText: String, covered: Set?) { let search = SidebarSearch(searchText) - var listed: Set = [] var matched: Set = [] - var rowsBySchema: [String: [TableInfo]] = [:] - for table in listing.tables { - guard let schema = table.schema else { continue } - listed.insert(schema) - rowsBySchema[schema, default: []].append(table) - guard !matched.contains(schema), - search.matchesObject(named: table.name, database: database, schema: schema) else { continue } + for (schema, rows) in groups.rowsBySchema + where rows.contains(where: { search.matchesObject(named: $0.name, database: database, schema: schema) }) { matched.insert(schema) } - self.listed = listed + self.listed = Set(groups.rowsBySchema.keys) self.matched = matched - self.unlisted = listing.unlistedSchemas + self.unlisted = unlisted self.covered = covered - self.rowsBySchema = rowsBySchema + self.rowsBySchema = groups.rowsBySchema } func answers(for schema: String) -> Bool { @@ -414,6 +434,21 @@ enum DatabaseTreeFilter { } } + // A keystroke changes which schemas match, never which rows each schema lists, so the grouping + // waits for the next listing. Grouping 50,000 rows again on every keystroke copied each of them. + struct SchemaListingGroups { + let rowsBySchema: [String: [TableInfo]] + + init(tables: [TableInfo]) { + var rowsBySchema: [String: [TableInfo]] = [:] + for table in tables { + guard let schema = table.schema else { continue } + rowsBySchema[schema, default: []].append(table) + } + self.rowsBySchema = rowsBySchema + } + } + // Redraws and body evaluations share one scan per database until the listing, search or coverage moves. struct SchemaListingMatchesMemo { private struct Entry { @@ -424,7 +459,13 @@ enum DatabaseTreeFilter { let matches: SchemaListingMatches } + private struct GroupsEntry { + let tables: SameRows + let groups: SchemaListingGroups + } + private var entries: [String: Entry] = [:] + private var groupsEntries: [String: GroupsEntry] = [:] mutating func matches( listing: CatalogTableListing.Result, @@ -432,13 +473,27 @@ enum DatabaseTreeFilter { searchText: String, covered: Set? ) -> SchemaListingMatches { + let tables = SameRows(rows: listing.tables) if let entry = entries[database], entry.searchText == searchText, entry.covered == covered, - entry.unlisted == listing.unlistedSchemas, entry.tables == SameRows(rows: listing.tables) { + entry.unlisted == listing.unlistedSchemas, entry.tables == tables { return entry.matches } - let matches = SchemaListingMatches(listing: listing, database: database, searchText: searchText, covered: covered) + let groups: SchemaListingGroups + if let entry = groupsEntries[database], entry.tables == tables { + groups = entry.groups + } else { + groups = SchemaListingGroups(tables: listing.tables) + groupsEntries[database] = GroupsEntry(tables: tables, groups: groups) + } + let matches = SchemaListingMatches( + groups: groups, + unlisted: listing.unlistedSchemas, + database: database, + searchText: searchText, + covered: covered + ) entries[database] = Entry( - tables: SameRows(rows: listing.tables), + tables: tables, unlisted: listing.unlistedSchemas, searchText: searchText, covered: covered, @@ -449,6 +504,7 @@ enum DatabaseTreeFilter { mutating func removeAll() { entries.removeAll() + groupsEntries.removeAll() } } diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterMemoTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterMemoTests.swift index 5d3a75d61e..b06d1d8826 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterMemoTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterMemoTests.swift @@ -105,6 +105,29 @@ struct DatabaseTreeFilterMemoTests { #expect(other.matched == ["sales"]) } + @Test("A new search over the same listing keeps its grouping, and a new listing groups again") + func listingGroupingOutlivesTheSearch() { + var memo = DatabaseTreeFilter.SchemaListingMatchesMemo() + let listing = CatalogTableListing.Result( + tables: [table("timesheet", "attendance"), table("orders", "sales"), table("refunds", "sales")], + unlistedSchemas: [] + ) + + let first = memo.matches(listing: listing, database: "shop", searchText: "time", covered: nil) + let refined = memo.matches(listing: listing, database: "shop", searchText: "ref", covered: nil) + let reread = memo.matches( + listing: CatalogTableListing.Result(tables: listing.tables.map { $0 }, unlistedSchemas: []), + database: "shop", searchText: "ref", covered: nil + ) + + #expect(first.matched == ["attendance"]) + #expect(refined.matched == ["sales"]) + #expect(refined.listed == ["attendance", "sales"]) + #expect(SameRows(rows: refined.rowsBySchema["sales"] ?? []) == SameRows(rows: first.rowsBySchema["sales"] ?? [])) + #expect(SameRows(rows: reread.rowsBySchema["sales"] ?? []) != SameRows(rows: refined.rowsBySchema["sales"] ?? [])) + #expect(reread.matched == ["sales"]) + } + // MARK: - Outline private struct Fixture { diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift index e1f5323fca..21e52a2e21 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift @@ -31,6 +31,28 @@ struct DatabaseTreeFilterTests { #expect(DatabaseTreeFilter.filteredTables(tables, searchText: "c").count == 2) } + @Test("filteredTables keeps a table apart from a view or a temporary table of the same name") + func filteredTablesKeepsKindsApart() { + let tables = [ + TableInfo(name: "users", type: .table, rowCount: 0, schema: "public"), + TableInfo(name: "users", type: .view, rowCount: 0, schema: "public"), + TableInfo(name: "users", type: .table, rowCount: 0, schema: "public", isTemporary: true), + TableInfo(name: "users", type: .table, rowCount: 12, schema: "public") + ] + let result = DatabaseTreeFilter.filteredTables(tables, searchText: "use") + #expect(result.map(\.id) == tables.dropLast().map(\.id)) + } + + @Test("filteredTables reads an empty schema as none, the way a table's id does") + func filteredTablesTreatsEmptySchemaAsNone() { + let tables = [ + TableInfo(name: "users", type: .table, rowCount: 0, schema: nil), + TableInfo(name: "users", type: .table, rowCount: 0, schema: "") + ] + #expect(tables[0].id == tables[1].id) + #expect(DatabaseTreeFilter.filteredTables(tables, searchText: "").count == 1) + } + @Test("filteredRoutines and filteredUserTypes keep objects whose schema holds a period") func filteredRoutinesAndTypesKeepDottedNamesApart() { let routines = [ diff --git a/apple/changelog.d/3889.changed.1.md b/apple/changelog.d/3889.changed.1.md new file mode 100644 index 0000000000..ac7cf24a7c --- /dev/null +++ b/apple/changelog.d/3889.changed.1.md @@ -0,0 +1 @@ +Open Quickly opens faster the first time after launch. diff --git a/apple/changelog.d/3889.changed.md b/apple/changelog.d/3889.changed.md new file mode 100644 index 0000000000..b6257b68ce --- /dev/null +++ b/apple/changelog.d/3889.changed.md @@ -0,0 +1 @@ +The sidebar search responds faster on a database with many schemas. From 20b34663e73b3d72742bbec50764b5b1c5f03ef6 Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 02:18:32 +0100 Subject: [PATCH 12/85] feat(linux-grid): set a time or a timestamp's time from a clock in Row Details (#3881) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3881.added.md | 1 + .../src/ui/data_grid/row_detail/calendar.rs | 30 +- .../app/src/ui/data_grid/row_detail/clock.rs | 172 ++++++++++ .../app/src/ui/data_grid/row_detail/imp.rs | 13 + .../app/src/ui/data_grid/row_detail/mod.rs | 3 + .../app/src/ui/data_grid/row_detail/tests.rs | 297 +++++++++++++++++- .../ui/data_grid/row_detail/value_editor.rs | 4 + linux/crates/app/src/ui/icons.rs | 2 + linux/data/resources/ui/row-detail-pane.ui | 104 ++++++ linux/po/POTFILES.in | 1 + 10 files changed, 610 insertions(+), 17 deletions(-) create mode 100644 linux/changelog.d/3881.added.md create mode 100644 linux/crates/app/src/ui/data_grid/row_detail/clock.rs diff --git a/linux/changelog.d/3881.added.md b/linux/changelog.d/3881.added.md new file mode 100644 index 0000000000..c10bc9f100 --- /dev/null +++ b/linux/changelog.d/3881.added.md @@ -0,0 +1 @@ +Row Details sets a time, or a timestamp's time, from a clock beside the calendar. diff --git a/linux/crates/app/src/ui/data_grid/row_detail/calendar.rs b/linux/crates/app/src/ui/data_grid/row_detail/calendar.rs index ee4cc47678..2298906675 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/calendar.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/calendar.rs @@ -1,4 +1,4 @@ -use chrono::{Datelike, FixedOffset, Local, NaiveDate, NaiveTime, Offset, TimeDelta, TimeZone}; +use chrono::{Datelike, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeDelta, TimeZone}; use gtk4::prelude::*; use gtk4::subclass::prelude::*; use gtk4::{self as gtk, glib}; @@ -30,18 +30,16 @@ pub fn with_day(text: &str, day: NaiveDate, zone: Option<&TemporalZone>, offset: } } -/// The offset `zone` has as `day` starts. Where a clock change skips -/// midnight, the day starts at the change, which is midnight at the -/// offset before it. pub fn day_start_offset(zone: &Tz, day: NaiveDate) -> FixedOffset { - let midnight = day.and_time(NaiveTime::MIN); - [midnight, midnight - TimeDelta::hours(1)] + offset_at(zone, day.and_time(NaiveTime::MIN)) +} + +// A wall-clock time a clock change skips takes the offset before the change. +pub fn offset_at(zone: &Tz, local: NaiveDateTime) -> FixedOffset { + [local, local - TimeDelta::hours(1)] .iter() .find_map(|local| zone.from_local_datetime(local).earliest()) - .map_or_else( - || zone.offset_from_utc_datetime(&midnight).fix(), - |at| at.offset().fix(), - ) + .map_or_else(|| zone.offset_from_utc_datetime(&local).fix(), |at| at.offset().fix()) } impl RowDetailPane { @@ -50,10 +48,7 @@ impl RowDetailPane { // Dropped below a button halfway down the pane, the calendar fits // neither below nor above it on a short screen and GTK closes it; // opened toward the rows, it slides up or down to fit. - imp.calendar_button.set_direction(match self.direction() { - gtk::TextDirection::Rtl => gtk::ArrowType::Right, - _ => gtk::ArrowType::Left, - }); + imp.calendar_button.set_direction(self.toward_rows()); imp.calendar_popover.connect_show(glib::clone!( #[weak(rename_to = pane)] self, @@ -99,6 +94,13 @@ impl RowDetailPane { imp.calendar.add_controller(keys); } + pub(super) fn toward_rows(&self) -> gtk::ArrowType { + match self.direction() { + gtk::TextDirection::Rtl => gtk::ArrowType::Right, + _ => gtk::ArrowType::Left, + } + } + /// The calendar opens on the value's day, else on today, for the row /// and field shown now. fn calendar_opens(&self) { diff --git a/linux/crates/app/src/ui/data_grid/row_detail/clock.rs b/linux/crates/app/src/ui/data_grid/row_detail/clock.rs new file mode 100644 index 0000000000..02e36ac66f --- /dev/null +++ b/linux/crates/app/src/ui/data_grid/row_detail/clock.rs @@ -0,0 +1,172 @@ +use std::ops::Range; + +use chrono::{Local, NaiveDate, NaiveTime, TimeZone, Timelike}; +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use gtk4::{self as gtk, glib}; +use tablepro_core::column::{ColumnKind, TemporalZone, TimeRange}; + +use super::RowDetailPane; +use super::calendar::offset_at; +use super::value_editor::Commit; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ClockField { + pub dated: bool, + pub with_offset: bool, +} + +impl ClockField { + // A duration such as MySQL's TIME runs past a day, so it gets no clock. + pub fn of(kind: &ColumnKind) -> Option { + match kind { + ColumnKind::Time { + with_offset, + range: TimeRange::TimeOfDay, + .. + } => Some(Self { + dated: false, + with_offset: *with_offset, + }), + ColumnKind::Timestamp { zone, .. } => Some(Self { + dated: true, + with_offset: *zone == TemporalZone::Offset, + }), + _ => None, + } + } +} + +// By the text's own form, as chrono reads it for the commit: SQLite keeps any date or time text in a +// DATE, TIME or DATETIME column, and chrono takes whitespace before a number and one digit for two. +fn clock_span(text: &str) -> Option> { + let after_day = day_len(text).map_or(0, |day| day + usize::from(text.as_bytes().get(day) == Some(&b'T'))); + let start = after_day + spaces(text.get(after_day..)?); + let minute = number(text, literal(text, number(text, start, 2)?, b':')?, 2)?; + let end = literal(text, minute, b':').and_then(|second| number(text, second, 2)); + Some(start..end.unwrap_or(minute)) +} + +// A year may run past 9999, as PostgreSQL writes one, and carry a sign. +fn day_len(text: &str) -> Option { + let lead = spaces(text); + let year = lead + usize::from(matches!(text.as_bytes().get(lead), Some(b'-' | b'+'))); + let month = number(text, literal(text, number(text, year, usize::MAX)?, b'-')?, 2)?; + number(text, literal(text, month, b'-')?, 2) +} + +// The end of the number at `at`, past any whitespace before it. +fn number(text: &str, at: usize, most: usize) -> Option { + let start = at + spaces(text.get(at..)?); + let run = text + .get(start..)? + .bytes() + .take(most) + .take_while(u8::is_ascii_digit) + .count(); + (run > 0).then_some(start + run) +} + +fn literal(text: &str, at: usize, byte: u8) -> Option { + (text.as_bytes().get(at) == Some(&byte)).then_some(at + 1) +} + +fn spaces(text: &str) -> usize { + text.len() - text.trim_start().len() +} + +pub fn time_of(text: &str) -> Option { + let mut parts = text + .get(clock_span(text)?)? + .split(':') + .map(|part| part.trim().parse::()); + let (hour, minute) = (parts.next()?.ok()?, parts.next()?.ok()?); + let second = parts.next().transpose().ok()?.unwrap_or(0); + NaiveTime::from_hms_opt(hour, minute, second) +} + +// Only the clock changes; text with no day or clock, such as NULL or infinity, becomes a new value. +pub fn with_time(text: &str, time: NaiveTime, field: ClockField, today: NaiveDate, zone: &Tz) -> String { + let mut clock = time.format("%H:%M:%S").to_string(); + let mut text = text.to_owned(); + if let Some(span) = clock_span(&text) { + text.replace_range(span, &clock); + return text; + } + let day_end = day_len(&text); + let day = day_end + .and_then(|end| NaiveDate::parse_from_str(text.get(..end)?.trim_start(), "%Y-%m-%d").ok()) + .unwrap_or(today); + if field.with_offset { + clock.push_str(&offset_at(zone, day.and_time(time)).to_string()); + } + match day_end { + Some(end) => { + text.insert_str(end, &format!(" {clock}")); + text + } + None if field.dated => format!("{} {clock}", today.format("%Y-%m-%d")), + None => clock, + } +} + +impl RowDetailPane { + pub(super) fn connect_clock(&self) { + let imp = self.imp(); + // Centred below a button at the window's edge, the clock would run past it. + imp.clock_button.set_direction(self.toward_rows()); + imp.clock_popover.connect_show(glib::clone!( + #[weak(rename_to = pane)] + self, + move |_| pane.clock_opens() + )); + imp.clock_set.connect_clicked(glib::clone!( + #[weak(rename_to = pane)] + self, + move |_| pane.pick_time() + )); + for spin in [imp.hour.get(), imp.minute.get(), imp.second.get()] { + spin.connect_output(|spin| { + spin.set_text(&format!("{:02}", spin.value_as_int())); + glib::Propagation::Stop + }); + } + } + + fn clock_field(&self) -> Option { + self.field_column() + .and_then(|column| ClockField::of(&column.value_type().kind)) + } + + fn clock_opens(&self) { + let imp = self.imp(); + imp.clock_target.replace(self.current_pin().zip(imp.field.get())); + let time = time_of(&self.typed_text()).unwrap_or_else(|| Local::now().time()); + imp.hour.set_value(f64::from(time.hour())); + imp.minute.set_value(f64::from(time.minute())); + imp.second.set_value(f64::from(time.second())); + } + + // Written as if typed, and only into the row and field the clock opened on. + fn pick_time(&self) { + let imp = self.imp(); + let target = imp.clock_target.take(); + imp.clock_popover.popdown(); + if target.is_none() || target != self.current_pin().zip(imp.field.get()) || !imp.value_view.is_editable() { + return; + } + let part = |spin: >k::SpinButton| u32::try_from(spin.value_as_int()).ok(); + let (Some(field), Some(time)) = ( + self.clock_field(), + part(&imp.hour) + .zip(part(&imp.minute)) + .zip(part(&imp.second)) + .and_then(|((hour, minute), second)| NaiveTime::from_hms_opt(hour, minute, second)), + ) else { + return; + }; + let text = with_time(&self.typed_text(), time, field, Local::now().date_naive(), &Local); + imp.value_buffer.set_text(&text); + self.commit(Commit::Keep); + } +} diff --git a/linux/crates/app/src/ui/data_grid/row_detail/imp.rs b/linux/crates/app/src/ui/data_grid/row_detail/imp.rs index 1ae83b565a..d8882ecf2b 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/imp.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/imp.rs @@ -48,6 +48,18 @@ pub struct RowDetailPane { #[template_child] pub calendar: TemplateChild, #[template_child] + pub clock_button: TemplateChild, + #[template_child] + pub clock_popover: TemplateChild, + #[template_child] + pub hour: TemplateChild, + #[template_child] + pub minute: TemplateChild, + #[template_child] + pub second: TemplateChild, + #[template_child] + pub clock_set: TemplateChild, + #[template_child] pub value_menu_button: TemplateChild, #[template_child] pub value_view: TemplateChild, @@ -81,6 +93,7 @@ pub struct RowDetailPane { pub quiet_calendar: Cell, /// The row and field the open calendar picks a day for. pub calendar_target: RefCell>, + pub clock_target: RefCell>, pub sync_queued: Cell, /// The field rows bound now, which redraw when the row changes. pub bound: RefCell>, diff --git a/linux/crates/app/src/ui/data_grid/row_detail/mod.rs b/linux/crates/app/src/ui/data_grid/row_detail/mod.rs index d40b030092..3f0fb18c4f 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/mod.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/mod.rs @@ -2,6 +2,7 @@ //! editor for the whole value of one of them. mod calendar; +mod clock; mod fields; mod imp; #[cfg(test)] @@ -40,6 +41,7 @@ impl RowDetailPane { self.connect_value_editor(grid); self.connect_value_menu(grid); self.connect_calendar(); + self.connect_clock(); self.connect_close(grid); self.watch_details(grid); self.watch_rows(grid); @@ -145,6 +147,7 @@ impl RowDetailPane { }; if !same { self.imp().calendar_popover.popdown(); + self.imp().clock_popover.popdown(); self.commit(Commit::Teardown); self.watch_row(row.as_ref()); } diff --git a/linux/crates/app/src/ui/data_grid/row_detail/tests.rs b/linux/crates/app/src/ui/data_grid/row_detail/tests.rs index 653dfb4429..135979f0ca 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/tests.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/tests.rs @@ -2,17 +2,18 @@ use std::cell::RefCell; use std::rc::Rc; use std::time::Duration; -use chrono::NaiveDate; +use chrono::{NaiveDate, NaiveTime}; use gtk4::prelude::*; use gtk4::subclass::prelude::*; use gtk4::{self as gtk, gdk}; -use tablepro_core::column::TemporalZone; +use tablepro_core::column::{TemporalZone, TimeRange}; use tablepro_core::edit::CellInput; -use tablepro_core::value::{JsonText, Temporal}; +use tablepro_core::value::{JsonText, SqlTime, Temporal}; use tablepro_core::{ColumnInfo, Value}; use super::RowDetailPane; use super::calendar::{day_of, day_start_offset, with_day}; +use super::clock::{ClockField, time_of, with_time}; use super::fields::field_order; use super::value_editor::value_content; use crate::services::change_tracker::RowKey; @@ -871,3 +872,293 @@ fn alt_return_closes_row_details_from_the_pane() { }) .expect("Alt+Return to close Row Details"); } + +/// Open the pane's clock until it shows, as `open_calendar` does. +fn open_clock(pane: &RowDetailPane) { + for _ in 0..5 { + pane.imp().clock_button.popup(); + if wait_until(Duration::from_secs(2), || pane.imp().hour.is_mapped()).is_ok() { + return; + } + pane.imp().clock_button.popdown(); + drain_main_context(); + } + panic!("the clock never showed"); +} + +fn set_clock(pane: &RowDetailPane, (hour, minute, second): (u32, u32, u32)) { + pane.imp().hour.set_value(f64::from(hour)); + pane.imp().minute.set_value(f64::from(minute)); + pane.imp().second.set_value(f64::from(second)); +} + +fn clock_text(pane: &RowDetailPane) -> [String; 3] { + let imp = pane.imp(); + [&imp.hour, &imp.minute, &imp.second].map(|spin| spin.text().to_string()) +} + +fn a_time(hour: u32, minute: u32, second: u32) -> NaiveTime { + NaiveTime::from_hms_opt(hour, minute, second).expect("a time") +} + +fn a_timestamp(day: (i32, u32, u32), time: NaiveTime) -> Value { + let (year, month, date) = day; + Value::Timestamp(Temporal::Finite( + NaiveDate::from_ymd_opt(year, month, date) + .expect("a day") + .and_time(time), + )) +} + +const NAIVE: ClockField = ClockField { + dated: true, + with_offset: false, +}; +const ZONED: ClockField = ClockField { + dated: true, + with_offset: true, +}; +const TIME: ClockField = ClockField { + dated: false, + with_offset: false, +}; +const TIMETZ: ClockField = ClockField { + dated: false, + with_offset: true, +}; + +fn set_at_nine(text: &str, field: ClockField) -> String { + let today = NaiveDate::from_ymd_opt(2026, 10, 29).expect("a day"); + let plus_two = chrono::FixedOffset::east_opt(2 * 3600).expect("an offset"); + with_time(text, a_time(9, 5, 30), field, today, &plus_two) +} + +#[test] +fn the_clock_sets_only_the_time_a_value_shows() { + assert_eq!( + set_at_nine("2024-02-29 23:59:58.125000", NAIVE), + "2024-02-29 09:05:30.125000" + ); + assert_eq!(set_at_nine("2024-02-29T23:59:58", NAIVE), "2024-02-29T09:05:30"); + assert_eq!(set_at_nine("2024-02-29 23:59", NAIVE), "2024-02-29 09:05:30"); + assert_eq!( + set_at_nine("2024-02-29 23:59:58+05:30", ZONED), + "2024-02-29 09:05:30+05:30" + ); + assert_eq!(set_at_nine("0044-03-15 12:00:00 BC", NAIVE), "0044-03-15 09:05:30 BC"); + assert_eq!( + set_at_nine("12000-01-01 10:00:00", NAIVE), + "12000-01-01 09:05:30", + "a year past 9999 lost its day" + ); + assert_eq!(set_at_nine("13:45:07.5+01", TIMETZ), "09:05:30.5+01"); + assert_eq!( + set_at_nine("2024-03-10T02:30:00.125-08:00", TIME), + "2024-03-10T09:05:30.125-08:00", + "SQLite keeps a timestamp in a TIME column" + ); + assert_eq!(set_at_nine("10:00", NAIVE), "09:05:30", "a time in a DATETIME column"); + assert_eq!(set_at_nine(" 2024-01-01 10:00:00", NAIVE), " 2024-01-01 09:05:30"); + assert_eq!( + set_at_nine("2024-01-01 12:34:56.789+02:00", ZONED), + "2024-01-01 09:05:30.789+02:00" + ); + assert_eq!(set_at_nine("-0001-01-01 12:34:56", NAIVE), "-0001-01-01 09:05:30"); + assert_eq!( + set_at_nine("2024-02-29T 12:34:56.125+05:30", ZONED), + "2024-02-29T 09:05:30.125+05:30" + ); + assert_eq!(set_at_nine("2024-2-9 9:5:3.5", NAIVE), "2024-2-9 09:05:30.5"); + assert_eq!(set_at_nine("2024- 2-29 12: 34", NAIVE), "2024- 2-29 09:05:30"); + assert_eq!( + set_at_nine("2024-\u{a0}02-29\u{a0}12:34:56", NAIVE), + "2024-\u{a0}02-29\u{a0}09:05:30" + ); + assert_eq!(set_at_nine("24:00:00+01", TIMETZ), "09:05:30+01"); +} + +#[test] +fn the_clock_starts_a_value_that_has_no_time() { + assert_eq!(set_at_nine("", NAIVE), "2026-10-29 09:05:30"); + assert_eq!(set_at_nine("infinity", NAIVE), "2026-10-29 09:05:30"); + assert_eq!(set_at_nine("2024-02-29", NAIVE), "2024-02-29 09:05:30"); + assert_eq!(set_at_nine(" 2024-02-29", NAIVE), " 2024-02-29 09:05:30"); + assert_eq!(set_at_nine("4713-01-01 BC", NAIVE), "4713-01-01 09:05:30 BC"); + assert_eq!(set_at_nine("4713-01-01 BC", ZONED), "4713-01-01 09:05:30+02:00 BC"); + assert_eq!(set_at_nine("12000-01-01", NAIVE), "12000-01-01 09:05:30"); + assert_eq!( + set_at_nine("2024-02-29", TIME), + "2024-02-29 09:05:30", + "SQLite keeps a date in a TIME column" + ); + assert_eq!(set_at_nine("", TIME), "09:05:30"); + let zoned = set_at_nine("", ZONED); + assert_eq!(zoned, "2026-10-29 09:05:30+02:00"); + assert!(tablepro_core::edit::parse_timestamp_with_offset(&zoned).is_ok()); + let timetz = set_at_nine("", TIMETZ); + assert_eq!(timetz, "09:05:30+02:00"); + assert!(tablepro_core::edit::parse_time_with_offset(&timetz).is_ok()); +} + +#[test] +fn a_new_zoned_time_takes_the_offset_of_its_own_moment() { + let hours = |hours: i32| chrono::FixedOffset::east_opt(hours * 3600).expect("an offset"); + // Sydney moves to +11 at 02:00 local on 2026-10-04. + let sydney = OneChange { + before: hours(10), + after: hours(11), + change: chrono::NaiveDateTime::parse_from_str("2026-10-03 16:00", "%Y-%m-%d %H:%M").expect("a UTC time"), + }; + let day = NaiveDate::from_ymd_opt(2026, 10, 4).expect("a day"); + let set = |text: &str, time: NaiveTime, field: ClockField| with_time(text, time, field, day, &sydney); + + assert_eq!(set("", a_time(9, 5, 0), ZONED), "2026-10-04 09:05:00+11:00"); + assert_eq!(set("", a_time(1, 30, 0), ZONED), "2026-10-04 01:30:00+10:00"); + assert_eq!( + set("", a_time(2, 30, 0), ZONED), + "2026-10-04 02:30:00+10:00", + "a time the change skips" + ); + assert_eq!(set("", a_time(9, 5, 0), TIMETZ), "09:05:00+11:00"); + let earlier = NaiveDate::from_ymd_opt(2026, 7, 1).expect("a day"); + assert_eq!( + with_time("2026-10-4", a_time(9, 5, 0), ZONED, earlier, &sydney), + "2026-10-4 09:05:00+11:00", + "a one-digit day took today's offset" + ); + assert_eq!( + with_time("2026-10-04", a_time(9, 5, 0), ZONED, earlier, &sydney), + "2026-10-04 09:05:00+11:00", + "the offset was today's, not the value's day" + ); +} + +#[test] +fn the_clock_reads_the_time_a_value_shows() { + assert_eq!(time_of("2024-02-29 23:59:58.125"), Some(a_time(23, 59, 58))); + assert_eq!(time_of("12000-01-01 10:00:00"), Some(a_time(10, 0, 0))); + assert_eq!(time_of("2024-02-29 10:30"), Some(a_time(10, 30, 0))); + assert_eq!(time_of("13:45:07.5+01"), Some(a_time(13, 45, 7))); + assert_eq!(time_of(" 2024-02-29T10:00:00Z"), Some(a_time(10, 0, 0))); + assert_eq!(time_of("2024-2-9 9:5:3"), Some(a_time(9, 5, 3))); + assert_eq!(time_of("2024- 2-29 12: 34"), Some(a_time(12, 34, 0))); + assert_eq!(time_of("24:00:00"), None); + assert_eq!(time_of("2024-02-29"), None); + assert_eq!(time_of("2024-02-2é 10:00:00"), None, "a cut inside a character"); + assert_eq!(time_of("not a time"), None); +} + +#[test] +fn a_clock_is_offered_where_a_time_of_day_can_be_typed() { + let saved = GridRowId::Persisted(RowKey::from_pk_values(&[Value::Int(1)]).expect("a key")); + let clock = |data_type: &str, lock: Option| { + value_content( + &column_of(named("t", data_type, false)), + &saved, + lock, + &CellInput::Value(Value::Null), + ) + .clock + }; + + assert!(clock("timestamp", None)); + assert!(clock("timestamptz", None)); + assert!(clock("time", None)); + assert!(!clock("date", None)); + assert!(!clock("interval", None)); + assert!( + !clock("timestamp", Some(CellLock::PendingDelete)), + "a locked cell offered a clock" + ); + let time = |range| tablepro_core::column::ColumnKind::Time { + fractional_digits: None, + with_offset: false, + range, + }; + assert_eq!(ClockField::of(&time(TimeRange::TimeOfDay)), Some(TIME)); + assert_eq!( + ClockField::of(&time(TimeRange::Duration)), + None, + "MySQL TIME runs past a day" + ); + assert_eq!(ClockField::of(&time(TimeRange::LongDuration)), None); +} + +#[gtk4::test] +fn the_clock_opens_on_the_value_and_sets_its_time() { + let (grid, pane) = opened( + vec![named("id", "integer", true), named("at", "timestamp", false)], + vec![vec![Value::Int(1), a_timestamp((2024, 2, 29), a_time(23, 59, 8))]], + ); + take_keyboard(&grid); + grid.focus(0, 1); + assert!(pane.imp().clock_button.is_visible(), "a timestamp has no clock"); + assert_eq!( + pane.imp().clock_button.direction(), + gtk::ArrowType::Left, + "a clock centred below the pane's button runs past the window" + ); + + open_clock(&pane); + assert_eq!( + clock_text(&pane), + ["23", "59", "08"], + "the clock did not open on the value" + ); + set_clock(&pane, (9, 5, 30)); + assert_eq!(clock_text(&pane), ["09", "05", "30"], "a clock shows two digits"); + pane.imp().clock_set.emit_clicked(); + + assert_eq!( + pending(&grid, 1, 1), + Some(CellInput::Value(a_timestamp((2024, 2, 29), a_time(9, 5, 30)))) + ); + assert!(!pane.imp().clock_popover.is_visible()); +} + +#[gtk4::test] +fn enter_in_a_clock_field_sets_the_time() { + let (grid, pane) = opened( + vec![named("id", "integer", true), named("at", "time", false)], + vec![vec![Value::Int(1), Value::Null]], + ); + take_keyboard(&grid); + grid.focus(0, 1); + open_clock(&pane); + set_clock(&pane, (9, 5, 30)); + + let text = descendants(&pane.imp().second.get()) + .into_iter() + .find_map(|widget| widget.downcast::().ok()) + .expect("the field's text"); + text.emit_by_name::<()>("activate", &[]); + // A button activated from the keyboard clicks once its pressed look has shown. + wait_until(WAIT, || pending(&grid, 1, 1).is_some()).expect("Enter to set the time"); + + let set = SqlTime::from_time_of_day(a_time(9, 5, 30)); + assert_eq!(pending(&grid, 1, 1), Some(CellInput::Value(Value::Time(set)))); +} + +#[gtk4::test] +fn a_page_that_takes_the_row_closes_its_clock_and_writes_nothing() { + let (grid, pane) = opened( + vec![named("id", "integer", true), named("at", "time", false)], + vec![vec![Value::Int(1), Value::Null], vec![Value::Int(2), Value::Null]], + ); + take_keyboard(&grid); + grid.focus(0, 1); + open_clock(&pane); + + grid.grid + .replace_rows(persisted(&[vec![Value::Int(2), Value::Null]]), 0); + drain_main_context(); + pane.imp().clock_set.emit_clicked(); + drain_main_context(); + + assert_eq!( + grid.tracker.pending_count(), + 0, + "the clock wrote into the row that took the slot" + ); + assert!(!pane.imp().clock_popover.is_visible()); +} diff --git a/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs b/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs index 457e316e58..df4fb6cff7 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs @@ -79,6 +79,7 @@ pub struct ValueContent { pub emptied_placeholder: Option, pub language: Option<&'static str>, pub calendar: bool, + pub clock: bool, } pub fn value_content(column: &GridColumn, row: &GridRowId, lock: Option, input: &CellInput) -> ValueContent { @@ -106,6 +107,7 @@ pub fn value_content(column: &GridColumn, row: &GridRowId, lock: Option None, }, calendar: editable && matches!(kind, ColumnKind::Date | ColumnKind::Timestamp { .. }), + clock: editable && super::clock::ClockField::of(kind).is_some(), message, } } @@ -339,6 +341,7 @@ impl RowDetailPane { emptied_placeholder: None, language: None, calendar: false, + clock: false, }; self.show_value("", &content); self.imp().value_view.set_editable(false); @@ -373,6 +376,7 @@ impl RowDetailPane { .update_property(&[gtk::accessible::Property::Label(name)]); self.show_message(content.message.as_deref(), false); imp.calendar_button.set_visible(content.calendar); + imp.clock_button.set_visible(content.clock); self.update_placeholder(); } diff --git a/linux/crates/app/src/ui/icons.rs b/linux/crates/app/src/ui/icons.rs index 055cdab1c9..c13f5ac819 100644 --- a/linux/crates/app/src/ui/icons.rs +++ b/linux/crates/app/src/ui/icons.rs @@ -38,6 +38,7 @@ pub const OPEN_MENU: &str = "open-menu-symbolic"; pub const PACKAGE: &str = "package-x-generic-symbolic"; pub const PAN_DOWN: &str = "pan-down-symbolic"; pub const PREFERENCES_SYSTEM: &str = "preferences-system-symbolic"; +pub const PREFERENCES_SYSTEM_TIME: &str = "preferences-system-time-symbolic"; pub const PROCESS_STOP: &str = "process-stop-symbolic"; pub const SECURITY_HIGH: &str = "security-high-symbolic"; pub const STARRED: &str = "starred-symbolic"; @@ -116,6 +117,7 @@ pub const ALL: &[&str] = &[ PACKAGE, PAN_DOWN, PREFERENCES_SYSTEM, + PREFERENCES_SYSTEM_TIME, PROCESS_STOP, SECURITY_HIGH, SIDEBAR_SHOW, diff --git a/linux/data/resources/ui/row-detail-pane.ui b/linux/data/resources/ui/row-detail-pane.ui index 6c49dfaf69..5e266afad8 100644 --- a/linux/data/resources/ui/row-detail-pane.ui +++ b/linux/data/resources/ui/row-detail-pane.ui @@ -184,6 +184,110 @@ + + + preferences-system-time-symbolic + false + Pick a Time + + Pick a Time + + + + + clock_set + + + 6 + 6 + 6 + 6 + 6 + + + vertical + true + true + 0.5 + true + + + 23 + 1 + 6 + + + + Hour + + + + + + : + + + + + vertical + true + true + 0.5 + true + + + 59 + 1 + 10 + + + + Minute + + + + + + : + + + + + vertical + true + true + 0.5 + true + + + 59 + 1 + 10 + + + + Second + + + + + + Set + center + 6 + + + + + + + + + view-more-symbolic diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index af8dd18f8f..471b784245 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -260,6 +260,7 @@ crates/app/src/ui/data_grid/references/mod.rs crates/app/src/ui/data_grid/references/tests.rs crates/app/src/ui/data_grid/reversed_list.rs crates/app/src/ui/data_grid/row_detail/calendar.rs +crates/app/src/ui/data_grid/row_detail/clock.rs crates/app/src/ui/data_grid/row_detail/fields.rs crates/app/src/ui/data_grid/row_detail/imp.rs crates/app/src/ui/data_grid/row_detail/mod.rs From 42a97219a33cf75cdb0095343feb417f69463fac Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 02:54:28 +0100 Subject: [PATCH 13/85] feat(linux-grid): sum, average and count the selected rows' focused column (#3877) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/Cargo.lock | 2 + linux/changelog.d/3877.added.md | 1 + linux/changelog.d/3877.fixed.md | 1 + linux/crates/app/Cargo.toml | 4 + .../crates/app/src/services/change_tracker.rs | 89 ++-- linux/crates/app/src/services/grid/mod.rs | 2 + .../src/services/grid/selection_summary.rs | 441 ++++++++++++++++++ .../app/src/services/grid/value_order.rs | 2 +- .../app/src/services/tab_tracker_handle.rs | 2 +- .../app/src/ui/browse_tab/grid_handlers.rs | 1 + linux/crates/app/src/ui/browse_tab/imp.rs | 2 + linux/crates/app/src/ui/browse_tab/layout.rs | 1 + .../app/src/ui/browse_tab/tests/load.rs | 29 ++ linux/crates/app/src/ui/data_grid/columns.rs | 1 + linux/crates/app/src/ui/data_grid/find_bar.rs | 34 +- .../app/src/ui/data_grid/grid_actions.rs | 1 + linux/crates/app/src/ui/data_grid/imp.rs | 5 + linux/crates/app/src/ui/data_grid/mod.rs | 2 + .../app/src/ui/data_grid/selection_summary.rs | 416 +++++++++++++++++ .../crates/app/src/ui/data_grid/tests/mod.rs | 1 + .../app/src/ui/data_grid/tests/summary.rs | 309 ++++++++++++ .../app/src/ui/data_grid/tracked_values.rs | 25 + linux/crates/app/src/ui/format/mod.rs | 2 +- linux/crates/app/src/ui/format/thousands.rs | 32 ++ linux/crates/app/src/ui/key_tab/mod.rs | 1 + linux/crates/app/src/ui/key_tab/tests.rs | 38 ++ linux/crates/app/src/ui/result_pane/mod.rs | 7 +- .../app/src/ui/structure_tab/save_handlers.rs | 9 +- .../crates/app/src/ui/structure_tab/tests.rs | 18 + linux/crates/core/src/value/decimal.rs | 25 + linux/data/resources/ui/browse-tab.ui | 2 +- linux/po/POTFILES.in | 3 + 32 files changed, 1455 insertions(+), 53 deletions(-) create mode 100644 linux/changelog.d/3877.added.md create mode 100644 linux/changelog.d/3877.fixed.md create mode 100644 linux/crates/app/src/services/grid/selection_summary.rs create mode 100644 linux/crates/app/src/ui/data_grid/selection_summary.rs create mode 100644 linux/crates/app/src/ui/data_grid/tests/summary.rs diff --git a/linux/Cargo.lock b/linux/Cargo.lock index 75cc32a4cb..cb92249d06 100644 --- a/linux/Cargo.lock +++ b/linux/Cargo.lock @@ -5549,8 +5549,10 @@ dependencies = [ "gtk4", "icu_decimal", "icu_locale_core", + "indexmap 2.14.2", "libadwaita", "log", + "num-bigint", "proptest", "pulldown-cmark", "relm4", diff --git a/linux/changelog.d/3877.added.md b/linux/changelog.d/3877.added.md new file mode 100644 index 0000000000..480bbe9b6e --- /dev/null +++ b/linux/changelog.d/3877.added.md @@ -0,0 +1 @@ +Selecting rows shows the sum, average and count of the focused column, with every figure ready to copy. diff --git a/linux/changelog.d/3877.fixed.md b/linux/changelog.d/3877.fixed.md new file mode 100644 index 0000000000..821acdeb90 --- /dev/null +++ b/linux/changelog.d/3877.fixed.md @@ -0,0 +1 @@ +Discarding structure changes on a table that changed elsewhere left them on screen while it was read again. diff --git a/linux/crates/app/Cargo.toml b/linux/crates/app/Cargo.toml index 414dd20bde..c340f035ec 100644 --- a/linux/crates/app/Cargo.toml +++ b/linux/crates/app/Cargo.toml @@ -100,6 +100,10 @@ gettext-rs = { version = "0.8", features = ["gettext-system"] } # Counts grouped the way the user's locale groups digits. icu_decimal = { version = "2.3", features = ["alloc"] } icu_locale_core = { version = "2.3", features = ["alloc"] } +# Pending drafts, found by id and kept in order. +indexmap = "2.14" +# A selection's sum and average, exact past what i128 holds. +num-bigint.workspace = true # The assistant's replies are Markdown; the HTML writer stays off. pulldown-cmark = { version = "0.13", default-features = false } sqlformat = "0.5" diff --git a/linux/crates/app/src/services/change_tracker.rs b/linux/crates/app/src/services/change_tracker.rs index 68d0deabc1..7ca6368097 100644 --- a/linux/crates/app/src/services/change_tracker.rs +++ b/linux/crates/app/src/services/change_tracker.rs @@ -5,6 +5,8 @@ use std::collections::{HashMap, HashSet, VecDeque}; use std::sync::Arc; +use indexmap::IndexMap; +use indexmap::map::Slice; use tablepro_core::dml::BuildSqlError; use tablepro_core::edit::CellInput; use tablepro_core::meta::RowIdentity; @@ -128,7 +130,7 @@ struct SetAside { #[derive(Debug, Default)] pub struct TabChangeTracker { - drafts: Vec, + drafts: IndexMap, deletes: HashMap, updates: HashMap<(RowKey, usize), CellEdit>, undo: VecDeque, @@ -198,8 +200,8 @@ impl TabChangeTracker { fn draft_in_flight(&self, draft_id: u64) -> bool { self.saving.as_ref().is_some_and(|save| { self.drafts - .iter() - .any(|draft| draft.draft_id == draft_id && save.snapshot.includes(draft.touched)) + .get(&draft_id) + .is_some_and(|draft| save.snapshot.includes(draft.touched)) }) } @@ -317,9 +319,7 @@ impl TabChangeTracker { // The draft's undo and redo entries go with it, so Ctrl+Z cannot // bring it back half-edited. fn drop_draft(&mut self, draft_id: u64) -> bool { - let before = self.drafts.len(); - self.drafts.retain(|d| d.draft_id != draft_id); - if self.drafts.len() == before { + if self.drafts.shift_remove(&draft_id).is_none() { return false; } self.updates @@ -348,7 +348,7 @@ impl TabChangeTracker { } fn draft_cell(&self, draft_id: u64, col: usize) -> Option<(CellInput, Touch)> { - let draft = self.drafts.iter().find(|d| d.draft_id == draft_id)?; + let draft = self.drafts.get(&draft_id)?; Some((draft.cells.get(col)?.clone(), draft.touched)) } @@ -390,7 +390,8 @@ impl TabChangeTracker { fn put_drafts(&mut self, drafts: Vec) -> TrackerDelta { let draft_ids: Vec = drafts.iter().map(|draft| draft.draft_id).collect(); - self.drafts.extend(drafts); + self.drafts + .extend(drafts.into_iter().map(|draft| (draft.draft_id, draft))); TrackerDelta { inserted_drafts: draft_ids.clone(), ..self.delta(draft_ids.into_iter().map(RowKey::Draft).collect()) @@ -399,7 +400,8 @@ impl TabChangeTracker { fn remove_drafts(&mut self, drafts: &[DraftRow]) -> TrackerDelta { let draft_ids: Vec = drafts.iter().map(|draft| draft.draft_id).collect(); - self.drafts.retain(|d| !draft_ids.contains(&d.draft_id)); + let removed: HashSet = draft_ids.iter().copied().collect(); + self.drafts.retain(|draft_id, _| !removed.contains(draft_id)); TrackerDelta { removed_drafts: draft_ids.clone(), ..self.delta(draft_ids.into_iter().map(RowKey::Draft).collect()) @@ -407,7 +409,7 @@ impl TabChangeTracker { } fn put_draft_cell(&mut self, draft_id: u64, col: usize, input: CellInput, touched: Touch) -> TrackerDelta { - let Some(draft) = self.drafts.iter_mut().find(|d| d.draft_id == draft_id) else { + let Some(draft) = self.drafts.get_mut(&draft_id) else { return self.delta(Vec::new()); }; let Some(cell) = draft.cells.get_mut(col) else { @@ -487,10 +489,10 @@ impl TabChangeTracker { .map(|(row_key, _)| row_key) .chain(self.deletes.keys()) .cloned() - .chain(self.drafts.iter().map(|draft| RowKey::Draft(draft.draft_id))) + .chain(self.drafts.keys().map(|draft_id| RowKey::Draft(*draft_id))) .filter(|row_key| seen.insert(row_key.clone())) .collect(); - let removed_drafts = self.drafts.iter().map(|draft| draft.draft_id).collect(); + let removed_drafts = self.drafts.keys().copied().collect(); self.drafts.clear(); self.deletes.clear(); self.updates.clear(); @@ -570,8 +572,8 @@ impl TabChangeTracker { } } - pub fn drafts(&self) -> &[DraftRow] { - &self.drafts + pub fn drafts(&self) -> &Slice { + self.drafts.as_slice() } /// Mark the row a failed save points at. The row marked before, if @@ -595,39 +597,29 @@ impl TabChangeTracker { /// A draft's values, with a cell left to the server as NULL. pub fn draft_values(&self, draft_id: u64) -> Option> { - self.drafts - .iter() - .find(|draft| draft.draft_id == draft_id) - .map(|draft| { - draft - .cells - .iter() - .map(|input| input.value().cloned().unwrap_or(Value::Null)) - .collect() - }) + self.drafts.get(&draft_id).map(|draft| { + draft + .cells + .iter() + .map(|input| input.value().cloned().unwrap_or(Value::Null)) + .collect() + }) } pub fn draft_inputs(&self, draft_id: u64) -> Option> { - self.drafts - .iter() - .find(|draft| draft.draft_id == draft_id) - .map(|draft| draft.cells.clone()) + self.drafts.get(&draft_id).map(|draft| draft.cells.clone()) } pub fn draft_input(&self, draft_id: u64, col: usize) -> Option { self.drafts - .iter() - .find(|draft| draft.draft_id == draft_id) + .get(&draft_id) .and_then(|draft| draft.cells.get(col)) .cloned() } /// A draft's cells as its INSERT carries them. pub fn draft_cells(&self, draft_id: u64, columns: &[ColumnInfo]) -> Option> { - self.drafts - .iter() - .find(|draft| draft.draft_id == draft_id) - .map(|draft| insert_cells(columns, draft)) + self.drafts.get(&draft_id).map(|draft| insert_cells(columns, draft)) } /// What the user changed, as the change set a save is planned from. @@ -642,7 +634,7 @@ impl TabChangeTracker { /// one, as does any column the server fills. pub fn change_set(&self, columns: &[ColumnInfo]) -> Result { let mut rows: Vec<(Touch, RowChange, StatementSource)> = Vec::new(); - for draft in &self.drafts { + for draft in self.drafts.values() { rows.push(( draft.touched, RowChange::Insert { @@ -750,11 +742,11 @@ impl TabChangeTracker { } rows.push(row_key); } - for mut draft in std::mem::take(&mut self.drafts) { + for (draft_id, mut draft) in std::mem::take(&mut self.drafts) { match layout.draft(&draft.cells) { Ok(cells) => { draft.cells = cells; - self.drafts.push(draft); + self.drafts.insert(draft_id, draft); } Err(misfit) => { removed_drafts.push(draft.draft_id); @@ -948,7 +940,7 @@ impl TabChangeTracker { !saved }); self.set_aside.retain(|change| !snapshot.includes(change.touched)); - self.drafts.retain(|draft| { + self.drafts.retain(|_, draft| { let saved = snapshot.includes(draft.touched); if saved { rows.push(RowKey::Draft(draft.draft_id)); @@ -1687,6 +1679,27 @@ mod tests { assert!(t.can_undo()); } + #[test] + fn the_largest_paste_reads_back_and_undoes_without_a_scan_per_draft() { + let mut t = TabChangeTracker::default(); + let rows = (0..tablepro_core::import::MAX_PASTED_ROWS) + .map(|row| vec![CellInput::Value(Value::Int(row as i64))]) + .collect(); + let drafts = t.track_insert(rows).inserted_drafts; + + let started = std::time::Instant::now(); + let read = drafts + .iter() + .filter(|draft_id| t.draft_values(**draft_id).is_some()) + .count(); + assert!(t.undo().is_some()); + let elapsed = started.elapsed(); + + assert_eq!(read, tablepro_core::import::MAX_PASTED_ROWS); + assert!(t.drafts().is_empty()); + assert!(elapsed < std::time::Duration::from_secs(2), "took {elapsed:?}"); + } + #[test] fn one_delete_over_many_rows_is_one_undo_step() { let mut t = TabChangeTracker::default(); diff --git a/linux/crates/app/src/services/grid/mod.rs b/linux/crates/app/src/services/grid/mod.rs index 69b283298e..2252854d55 100644 --- a/linux/crates/app/src/services/grid/mod.rs +++ b/linux/crates/app/src/services/grid/mod.rs @@ -19,6 +19,7 @@ mod grid_column; mod grid_row_id; mod row_key_builder; mod row_snapshot; +mod selection_summary; #[cfg(test)] mod test_columns; pub mod value_order; @@ -43,3 +44,4 @@ pub use self::grid_column::{CellDisplay, GridColumn, grid_columns}; pub use self::grid_row_id::GridRowId; pub use self::row_key_builder::RowKeyBuilder; pub use self::row_snapshot::RowSnapshot; +pub use self::selection_summary::{SelectionSummary, SummaryRule, summarize}; diff --git a/linux/crates/app/src/services/grid/selection_summary.rs b/linux/crates/app/src/services/grid/selection_summary.rs new file mode 100644 index 0000000000..9796f922ff --- /dev/null +++ b/linux/crates/app/src/services/grid/selection_summary.rs @@ -0,0 +1,441 @@ +use std::borrow::Cow; +use std::cmp::Ordering; + +use num_bigint::BigInt; +use tablepro_core::Value; +use tablepro_core::value::SqlDecimal; + +use super::value_order::decimal_of; +use super::{CellDisplay, GridColumn}; + +const AVERAGE_EXTRA_DIGITS: u32 = 4; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SummaryRule { + /// `scale` is the column's declared one, which its cells are padded to. + /// A value that is not a number, such as NaN, is counted as one. + Numeric { + scale: u32, + }, + /// No declared type, so text that reads as a number is one. Typed text + /// holds identifiers like ZIP codes and is never summed. + NumericIfParsable, + CountOnly, +} + +impl SummaryRule { + pub fn for_column(column: &GridColumn) -> Self { + let kind = &column.column_type.kind; + if kind.is_untyped() { + return Self::NumericIfParsable; + } + match column.display() { + CellDisplay::Number => Self::Numeric { + scale: kind.decimal_scale().unwrap_or(0), + }, + _ => Self::CountOnly, + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SelectionSummary { + pub value_count: u64, + pub empty_count: u64, + pub not_a_number_count: u64, + pub numbers: Option, +} + +/// Plain decimal text with no grouping, as SQL and a spreadsheet read it +/// in any locale. Sum, minimum and maximum keep the widest scale, so cents +/// stay cents; the average carries four more digits. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct NumberFigures { + pub count: u64, + pub sum: String, + pub average: String, + pub minimum: String, + pub maximum: String, +} + +/// `values` as their cells show them, with a cell left to its default as +/// NULL. `None` once `cancelled` says so. It asks before every value: one +/// of 100,000 digits takes milliseconds alone. +pub fn summarize<'a>( + values: impl IntoIterator, + rule: SummaryRule, + cancelled: &dyn Fn() -> bool, +) -> Option { + let mut tally = Tally::default(); + for value in values { + if cancelled() { + return None; + } + tally.add(value, rule); + } + Some(tally.finish(rule)) +} + +#[derive(Default)] +struct Tally { + value_count: u64, + empty_count: u64, + not_a_number_count: u64, + count: u64, + sum: Exact, + minimum: Option, + maximum: Option, +} + +impl Tally { + fn add(&mut self, value: &Value, rule: SummaryRule) { + if value.is_null() { + self.empty_count += 1; + return; + } + self.value_count += 1; + if rule == SummaryRule::CountOnly { + return; + } + match number_of(value) { + Some(number) => self.add_number(number), + None if rule != SummaryRule::NumericIfParsable => self.not_a_number_count += 1, + None => {} + } + } + + fn add_number(&mut self, number: Exact) { + self.count += 1; + self.sum = self.sum.plus(&number); + if self + .minimum + .as_ref() + .is_none_or(|minimum| number.compare(minimum) == Ordering::Less) + { + self.minimum = Some(number.clone()); + } + if self + .maximum + .as_ref() + .is_none_or(|maximum| number.compare(maximum) == Ordering::Greater) + { + self.maximum = Some(number); + } + } + + fn finish(self, rule: SummaryRule) -> SelectionSummary { + let declared = match rule { + SummaryRule::Numeric { scale } => scale, + SummaryRule::NumericIfParsable | SummaryRule::CountOnly => 0, + }; + let numbers = match (self.minimum, self.maximum) { + (Some(minimum), Some(maximum)) => { + let scale = self.sum.scale.max(declared); + let average = self.sum.divided(self.count, scale + AVERAGE_EXTRA_DIGITS); + Some(NumberFigures { + count: self.count, + sum: self.sum.text(scale), + average: average.trimmed().text(0), + minimum: minimum.text(scale), + maximum: maximum.text(scale), + }) + } + _ => None, + }; + SelectionSummary { + value_count: self.value_count, + empty_count: self.empty_count, + not_a_number_count: self.not_a_number_count, + numbers, + } + } +} + +/// A number type's value, or text that reads as a number, at its exact +/// decimal. A float is the decimal its shortest text spells, which is +/// what its cell shows, so `0.1 + 0.2` is `0.3`. +fn number_of(value: &Value) -> Option { + let decimal = match value { + Value::Int(n) => return Some(Exact::integer(BigInt::from(*n))), + Value::UInt(n) => return Some(Exact::integer(BigInt::from(*n))), + Value::Text(text) => Cow::Owned(text.parse::().ok()?), + Value::Other(other) => Cow::Owned(other.text.parse::().ok()?), + _ => decimal_of(value)?, + }; + let (negative, digits, scale) = decimal.parts()?; + let magnitude = BigInt::parse_bytes(digits.as_bytes(), 10)?; + Some(Exact { + mantissa: if negative { -magnitude } else { magnitude }, + scale, + }) +} + +/// `mantissa × 10^-scale`, with the arithmetic `SqlDecimal` leaves out. +#[derive(Debug, Clone, Default)] +struct Exact { + mantissa: BigInt, + scale: u32, +} + +impl Exact { + fn integer(mantissa: BigInt) -> Self { + Self { mantissa, scale: 0 } + } + + fn rescaled(&self, scale: u32) -> BigInt { + match scale.checked_sub(self.scale) { + Some(0) | None => self.mantissa.clone(), + Some(more) => &self.mantissa * pow10(more), + } + } + + fn plus(&self, other: &Self) -> Self { + let scale = self.scale.max(other.scale); + Self { + mantissa: self.rescaled(scale) + other.rescaled(scale), + scale, + } + } + + fn compare(&self, other: &Self) -> Ordering { + let scale = self.scale.max(other.scale); + self.rescaled(scale).cmp(&other.rescaled(scale)) + } + + /// `self ÷ count` to `scale` digits, half away from zero. + fn divided(&self, count: u64, scale: u32) -> Self { + let numerator = self.rescaled(scale); + let denominator = BigInt::from(count); + let quotient = &numerator / &denominator; + let remainder = &numerator % &denominator; + let rounds_away = remainder.magnitude() * 2u32 >= *denominator.magnitude(); + let mantissa = match rounds_away { + true if numerator < BigInt::ZERO => quotient - 1, + true => quotient + 1, + false => quotient, + }; + Self { mantissa, scale } + } + + fn trimmed(&self) -> Self { + let digits = self.mantissa.magnitude().to_string(); + let zeros = digits.bytes().rev().take_while(|digit| *digit == b'0').count(); + let drop = u32::try_from(zeros).unwrap_or(u32::MAX).min(self.scale); + Self { + mantissa: &self.mantissa / pow10(drop), + scale: self.scale - drop, + } + } + + /// The number with at least `scale` fraction digits. + fn text(&self, scale: u32) -> String { + let scale = scale.max(self.scale); + let mantissa = self.rescaled(scale); + let digits = mantissa.magnitude().to_string(); + let sign = if mantissa < BigInt::ZERO { "-" } else { "" }; + let scale = scale as usize; + if scale == 0 { + return format!("{sign}{digits}"); + } + let digits = format!("{digits:0>width$}", width = scale + 1); + let (whole, fraction) = digits.split_at(digits.len() - scale); + format!("{sign}{whole}.{fraction}") + } +} + +fn pow10(exponent: u32) -> BigInt { + BigInt::from(10u32).pow(exponent) +} + +#[cfg(test)] +mod tests { + use std::cell::Cell; + + use tablepro_core::Value; + use tablepro_core::column::{ColumnKind, IntegerKind, IntegerWidth, TextLength}; + use tablepro_core::value::{OtherValue, SqlDecimal}; + + use super::{NumberFigures, SelectionSummary, SummaryRule, summarize}; + use crate::services::grid::test_columns::result_only; + + fn summed(values: &[Value], rule: SummaryRule) -> SelectionSummary { + summarize(values, rule, &|| false).expect("a run nobody cancelled") + } + + fn numbers(values: &[Value], rule: SummaryRule) -> NumberFigures { + summed(values, rule).numbers.expect("numbers") + } + + fn decimal(text: &str) -> Value { + Value::Decimal(text.parse::().expect("a decimal")) + } + + const NUMERIC: SummaryRule = SummaryRule::Numeric { scale: 0 }; + + #[test] + fn decimals_add_exactly_and_keep_their_scale() { + let figures = numbers(&[decimal("0.10"), decimal("0.20"), decimal("19.99")], NUMERIC); + + assert_eq!(figures.count, 3); + assert_eq!(figures.sum, "20.29"); + assert_eq!(figures.minimum, "0.10"); + assert_eq!(figures.maximum, "19.99"); + assert_eq!(figures.average, "6.763333", "four digits past the cents"); + } + + #[test] + fn the_declared_scale_pads_the_figures_as_it_pads_the_cells() { + let figures = numbers(&[decimal("5"), decimal("6")], SummaryRule::Numeric { scale: 2 }); + + assert_eq!(figures.sum, "11.00"); + assert_eq!(figures.minimum, "5.00"); + assert_eq!(figures.average, "5.5"); + } + + #[test] + fn floats_add_as_the_text_the_cell_shows() { + let figures = numbers(&[Value::Float64(0.1), Value::Float64(0.2)], NUMERIC); + assert_eq!(figures.sum, "0.3"); + assert_eq!(figures.average, "0.15"); + + let figures = numbers(&[Value::Float32(0.1), Value::Float32(0.2)], NUMERIC); + assert_eq!(figures.sum, "0.3", "a float32 at its own width, not widened"); + } + + #[test] + fn the_average_rounds_half_away_from_zero_and_drops_its_zeros() { + assert_eq!( + numbers(&[Value::Int(2), Value::Int(4), Value::Int(-9)], NUMERIC).average, + "-1" + ); + assert_eq!(numbers(&[Value::Int(-1), Value::Int(-2)], NUMERIC).average, "-1.5"); + assert_eq!( + numbers(&[Value::Int(1), Value::Int(1), Value::Int(0)], NUMERIC).average, + "0.6667" + ); + assert_eq!( + numbers(&[Value::Int(-1), Value::Int(-1), Value::Int(0)], NUMERIC).average, + "-0.6667" + ); + } + + #[test] + fn integers_of_any_width_add_past_i128() { + let wide = "170141183460469231731687303715884105727" + .parse() + .map(Value::WideInt) + .expect("an i128::MAX"); + let figures = numbers(&[wide.clone(), wide, Value::UInt(u64::MAX)], NUMERIC); + + assert_eq!(figures.sum, "340282366920938463481821351505477763069"); + } + + #[test] + fn a_decimal_wider_than_a_thousand_digits_is_still_a_number() { + let wide = format!("1{}", "0".repeat(1_500)); + let summary = summed(&[decimal(&wide), Value::Int(1)], NUMERIC); + + assert_eq!(summary.not_a_number_count, 0); + assert_eq!( + summary.numbers.expect("numbers").sum, + format!("1{}1", "0".repeat(1_499)) + ); + } + + #[test] + fn nulls_are_empty_and_a_nan_is_not_a_number() { + let values = [ + Value::Int(5), + Value::Null, + Value::Null, + Value::Decimal(SqlDecimal::nan()), + Value::Float64(f64::INFINITY), + ]; + let summary = summed(&values, NUMERIC); + + assert_eq!(summary.value_count, 3); + assert_eq!(summary.empty_count, 2); + assert_eq!(summary.not_a_number_count, 2); + assert_eq!(summary.numbers.expect("numbers").count, 1); + } + + #[test] + fn text_is_summed_only_in_a_column_of_no_declared_type() { + let other = Value::Other(Box::new(OtherValue { + type_name: "Variant(UInt32, String)".to_owned(), + text: "4".to_owned(), + })); + let values = [ + Value::Text("12".into()), + Value::Text("\u{a0}3.5 ".into()), + Value::Text("n/a".into()), + other, + ]; + + let untyped = summed(&values, SummaryRule::NumericIfParsable); + assert_eq!(untyped.value_count, 4); + assert_eq!( + untyped.not_a_number_count, 0, + "free text in an untyped column is just text" + ); + assert_eq!(untyped.numbers.expect("numbers").sum, "19.5"); + + let typed = summed(&values, SummaryRule::CountOnly); + assert_eq!(typed.value_count, 4); + assert!(typed.numbers.is_none()); + } + + #[test] + fn a_column_of_text_only_counts() { + let summary = summed(&[Value::Text("a".into()), Value::Null], SummaryRule::NumericIfParsable); + + assert_eq!((summary.value_count, summary.empty_count), (1, 1)); + assert!(summary.numbers.is_none()); + } + + #[test] + fn the_rule_follows_the_column_type() { + let integer = ColumnKind::Integer(IntegerKind { + width: IntegerWidth::W64, + signed: true, + }); + let cents = ColumnKind::Decimal { + precision: Some(10), + scale: Some(2), + }; + let zip = ColumnKind::Text { + length: TextLength::Varying(5), + }; + + assert_eq!( + SummaryRule::for_column(&result_only("n", integer)), + SummaryRule::Numeric { scale: 0 } + ); + assert_eq!( + SummaryRule::for_column(&result_only("price", cents)), + SummaryRule::Numeric { scale: 2 } + ); + assert_eq!( + SummaryRule::for_column(&result_only("zip", zip)), + SummaryRule::CountOnly + ); + assert_eq!( + SummaryRule::for_column(&result_only("x", ColumnKind::Other)), + SummaryRule::NumericIfParsable + ); + } + + #[test] + fn a_cancelled_run_answers_nothing() { + let asked = Cell::new(0); + let values = vec![Value::Int(1); 10]; + + let answer = summarize(&values, NUMERIC, &|| { + asked.set(asked.get() + 1); + asked.get() > 3 + }); + + assert_eq!(answer, None); + assert_eq!(asked.get(), 4, "it stops before the next value"); + } +} diff --git a/linux/crates/app/src/services/grid/value_order.rs b/linux/crates/app/src/services/grid/value_order.rs index 94c3b88c3d..d466aeb52b 100644 --- a/linux/crates/app/src/services/grid/value_order.rs +++ b/linux/crates/app/src/services/grid/value_order.rs @@ -215,7 +215,7 @@ fn integer_of(value: &Value) -> Option { /// The value as an exact decimal, for comparing numbers of different /// kinds. A float is taken at the decimal its shortest form spells. A /// decimal is borrowed, since a sort compares each one many times. -fn decimal_of(value: &Value) -> Option> { +pub(crate) fn decimal_of(value: &Value) -> Option> { let owned = match value { Value::Decimal(decimal) => return Some(Cow::Borrowed(decimal)), Value::Float32(_) | Value::Float64(_) => { diff --git a/linux/crates/app/src/services/tab_tracker_handle.rs b/linux/crates/app/src/services/tab_tracker_handle.rs index 3a82b7b5f6..ba8f576cbf 100644 --- a/linux/crates/app/src/services/tab_tracker_handle.rs +++ b/linux/crates/app/src/services/tab_tracker_handle.rs @@ -72,7 +72,7 @@ impl TabTrackerHandle { } pub fn drafts(&self) -> Vec { - self.0.borrow().drafts().to_vec() + self.0.borrow().drafts().values().cloned().collect() } pub fn draft_values(&self, draft_id: u64) -> Option> { diff --git a/linux/crates/app/src/ui/browse_tab/grid_handlers.rs b/linux/crates/app/src/ui/browse_tab/grid_handlers.rs index c6179d3bf3..1709abcab2 100644 --- a/linux/crates/app/src/ui/browse_tab/grid_handlers.rs +++ b/linux/crates/app/src/ui/browse_tab/grid_handlers.rs @@ -72,6 +72,7 @@ impl BrowseModel { let label = &self.widgets.selection_label; label.set_label(hint.as_deref().unwrap_or_default()); label.set_visible(hint.is_some()); + self.grid.selection_summary().set_visible(self.shown_page().is_some()); let ids: Vec = rows.iter().map(GridRow::id).collect(); let blocked = match delete_availability(&ids, self.read_only) { DeleteAvailability::Unavailable(block) => block.explanation(), diff --git a/linux/crates/app/src/ui/browse_tab/imp.rs b/linux/crates/app/src/ui/browse_tab/imp.rs index 0cbacd6ce8..b6c8158638 100644 --- a/linux/crates/app/src/ui/browse_tab/imp.rs +++ b/linux/crates/app/src/ui/browse_tab/imp.rs @@ -46,6 +46,8 @@ pub struct BrowseTab { #[template_child] pub selection_label: TemplateChild, #[template_child] + pub action_bar: TemplateChild, + #[template_child] pub more_button: TemplateChild, #[template_child] pub export_button: TemplateChild, diff --git a/linux/crates/app/src/ui/browse_tab/layout.rs b/linux/crates/app/src/ui/browse_tab/layout.rs index 20a97fe76a..46fb075649 100644 --- a/linux/crates/app/src/ui/browse_tab/layout.rs +++ b/linux/crates/app/src/ui/browse_tab/layout.rs @@ -152,6 +152,7 @@ pub(super) fn grid(tab: &BrowseTab, init: GridInit) -> DataGrid { grid.set_hexpand(true); let imp = tab.imp(); imp.grid_holder.append(&grid); + imp.action_bar.pack_start(&grid.selection_summary()); let _ = imp.grid.set(grid.clone()); connect_grid(tab, &grid); grid diff --git a/linux/crates/app/src/ui/browse_tab/tests/load.rs b/linux/crates/app/src/ui/browse_tab/tests/load.rs index ab04ce809b..e281dfc373 100644 --- a/linux/crates/app/src/ui/browse_tab/tests/load.rs +++ b/linux/crates/app/src/ui/browse_tab/tests/load.rs @@ -250,6 +250,35 @@ fn a_failed_page_turn_hands_the_focus_to_try_again() { assert_eq!(focus().as_ref(), Some(retry.upcast_ref())); } +/// The figures are of rows the error page hides, so the readout goes with +/// the selected-row count. +#[gtk4::test] +fn the_selection_summary_hides_behind_the_error_page() { + let mut script = super::paging(full_page(0), rows(&["unused"])); + script.query_bound = Default::default(); + script + .query_bound + .once(FakeReply::ok(full_page(0))) + .always(FakeReply::err(DriverError::server("denied"))); + let harness = Harness::open(script, []); + wait_until(WAIT, || harness.shown().len() == 100).expect("the first page"); + let _shown = Shown::new(harness.tab()); + let readout = harness + .grid() + .selection_summary() + .downcast::() + .expect("the readout"); + wait_until(WAIT, || harness.grid().focus_cell(0, 0).is_some()).expect("a cell takes the focus"); + harness.select(0..3); + wait_until(WAIT, || readout.reveals_child()).expect("the readout shows the selection"); + assert!(readout.is_visible()); + + harness.tab().send(PagingInput::NextPage); + + wait_until(WAIT, || harness.visible_page().as_deref() == Some("error")).expect("the error page"); + assert!(!readout.is_visible(), "the readout summed rows the error page hides"); +} + fn error_text(harness: &Harness) -> String { harness .tab() diff --git a/linux/crates/app/src/ui/data_grid/columns.rs b/linux/crates/app/src/ui/data_grid/columns.rs index f341c6a432..ee0b7f5043 100644 --- a/linux/crates/app/src/ui/data_grid/columns.rs +++ b/linux/crates/app/src/ui/data_grid/columns.rs @@ -497,6 +497,7 @@ impl DataGrid { if let Some(column) = self.data_view_column(index) { column.set_visible(visible); } + self.refresh_summary(); } /// Every column back where the result has it, at its opening width, diff --git a/linux/crates/app/src/ui/data_grid/find_bar.rs b/linux/crates/app/src/ui/data_grid/find_bar.rs index 13f985eea0..551e7ee0eb 100644 --- a/linux/crates/app/src/ui/data_grid/find_bar.rs +++ b/linux/crates/app/src/ui/data_grid/find_bar.rs @@ -36,6 +36,14 @@ struct Found { answered: bool, } +impl FindState { + pub(super) fn stop(&self) { + if let Some(running) = self.running.take() { + running.store(true, Ordering::Relaxed); + } + } +} + impl Found { fn label(&self) -> String { if !self.answered { @@ -180,9 +188,7 @@ impl DataGrid { /// answers. fn start_search(&self, query: String, moving: bool) { let state = &self.imp().find; - if let Some(running) = state.running.take() { - running.store(true, Ordering::Relaxed); - } + state.stop(); let search = state.searches.get().wrapping_add(1); state.searches.set(search); state.moving.set(moving); @@ -382,11 +388,11 @@ mod tests { use gtk4::prelude::*; use gtk4::subclass::prelude::*; - use gtk4::{self as gtk, gdk}; + use gtk4::{self as gtk, gdk, glib}; use tablepro_core::Value; use super::super::test_grid::{Grid, GridSpec, WAIT, focus_in_grid, named, results}; - use crate::test_support::{KeyInjector, wait_until}; + use crate::test_support::{KeyInjector, drain_main_context, wait_until}; fn text(value: &str) -> Value { Value::Text(value.to_owned()) @@ -558,6 +564,24 @@ mod tests { assert_eq!(on_match(&grid), Some((42, 1))); } + #[gtk4::test] + fn closing_the_grid_stops_the_search_still_running() { + let grid = orchard(); + grid.activate("find"); + // The answer lands on a context nothing runs, so the search is still + // out when the grid goes. + let parked = glib::MainContext::new(); + parked + .with_thread_default(|| grid.grid.start_search("apple".to_owned(), true)) + .expect("the context is free"); + let running = grid.grid.imp().find.running.borrow().clone().expect("the search runs"); + + drop(grid); + drain_main_context(); + + assert!(running.load(std::sync::atomic::Ordering::Relaxed)); + } + #[gtk4::test] fn rows_that_change_are_searched_again() { let grid = orchard(); diff --git a/linux/crates/app/src/ui/data_grid/grid_actions.rs b/linux/crates/app/src/ui/data_grid/grid_actions.rs index 201f64c4a2..71cd586a8b 100644 --- a/linux/crates/app/src/ui/data_grid/grid_actions.rs +++ b/linux/crates/app/src/ui/data_grid/grid_actions.rs @@ -176,6 +176,7 @@ impl DataGrid { self.edit().refresh_actions(self, target.as_ref()); copy::refresh_actions(self, target.as_ref()); references::refresh_actions(self, target.as_ref()); + self.refresh_summary(); find_bar::refresh_actions(self); } } diff --git a/linux/crates/app/src/ui/data_grid/imp.rs b/linux/crates/app/src/ui/data_grid/imp.rs index 28a482d82a..e689b876f8 100644 --- a/linux/crates/app/src/ui/data_grid/imp.rs +++ b/linux/crates/app/src/ui/data_grid/imp.rs @@ -16,6 +16,7 @@ use super::references::Armed; use super::reversed_list::ReversedList; use super::row_detail::RowDetailPane; use super::selection::Target; +use super::selection_summary::SummaryState; use super::{GridConfig, GridStore}; /// The grid's selection model, made with the grid and given its rows once @@ -64,6 +65,7 @@ pub struct DataGrid { pub columns: RefCell, pub actions: gio::SimpleActionGroup, pub menus: OnceCell, + pub summary: SummaryState, pub edit: EditController, pub find: FindState, /// What a menu was opened on, until it closes. @@ -113,6 +115,9 @@ impl ObjectImpl for DataGrid { } fn dispose(&self) { + // A run still out on the task pool would go on over every row. + self.find.stop(); + self.summary.stop(); self.obj().stop_watching_focus(); if let Some(menus) = self.menus.get() { menus.unparent(); diff --git a/linux/crates/app/src/ui/data_grid/mod.rs b/linux/crates/app/src/ui/data_grid/mod.rs index eba768f692..47e2ca9bde 100644 --- a/linux/crates/app/src/ui/data_grid/mod.rs +++ b/linux/crates/app/src/ui/data_grid/mod.rs @@ -24,6 +24,7 @@ mod row_detail; mod row_json_dialog; mod row_number_column; mod selection; +mod selection_summary; mod server_sorter; mod table_keys; #[cfg(test)] @@ -179,6 +180,7 @@ impl DataGrid { pub fn set_columns(&self, columns: Arc<[GridColumn]>, sorting: GridSorting, rules: Option) { self.commit_editors(); self.store().clear(); + self.imp().summary.forget_column(); self.edit().set_rules(self, rules); self.rebuild_columns(columns, sorting); self.show_rows_or_empty(); diff --git a/linux/crates/app/src/ui/data_grid/selection_summary.rs b/linux/crates/app/src/ui/data_grid/selection_summary.rs new file mode 100644 index 0000000000..7a3bba69a5 --- /dev/null +++ b/linux/crates/app/src/ui/data_grid/selection_summary.rs @@ -0,0 +1,416 @@ +use std::cell::{Cell, OnceCell, RefCell}; +use std::rc::Rc; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; + +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use gtk4::{self as gtk, glib}; + +use super::DataGrid; +use crate::i18n::{gettext, gettext_f}; +use crate::services::grid::{RowSnapshot, SelectionSummary, SummaryRule, summarize}; +use crate::ui::format::{format_decimal, format_thousands}; +use crate::ui::icons; + +/// A summary of one row says no more than its cell. +const MIN_ROWS: usize = 2; + +#[derive(Default)] +pub struct SummaryState { + readout: OnceCell, + /// The last data column a cell had the focus in. It stays while the + /// focus is in the readout's popover or the detail pane. + column: Cell>, + queued: Cell, + /// Counts runs, so an answer to an older one is dropped. + runs: Cell, + pub(super) running: RefCell>>, +} + +impl SummaryState { + pub(super) fn forget_column(&self) { + self.column.set(None); + } + + pub(super) fn stop(&self) { + if let Some(running) = self.running.take() { + running.store(true, Ordering::Relaxed); + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Shown { + column: String, + summary: SelectionSummary, +} + +/// A menu button in the host's bottom bar whose popover lists every +/// figure with a Copy button. The revealer keeps the bar's height while +/// it is hidden. +struct Readout { + revealer: gtk::Revealer, + button: gtk::MenuButton, + heading: gtk::Label, + separator: gtk::Separator, + figures: Vec, + shown: RefCell>, +} + +impl Readout { + fn new(grid: &DataGrid) -> Self { + let heading = gtk::Label::builder() + .xalign(0.0) + .ellipsize(gtk::pango::EllipsizeMode::End) + .build(); + heading.add_css_class("heading"); + let table = gtk::Grid::builder().row_spacing(4).column_spacing(12).build(); + let separator = gtk::Separator::new(gtk::Orientation::Horizontal); + let mut figures = Vec::with_capacity(Figure::ALL.len()); + let mut line = 0; + for figure in Figure::ALL { + if figure == Figure::Sum { + table.attach(&separator, 0, line, 3, 1); + line += 1; + } + let row = FigureRow::new(grid, figure); + table.attach(&row.title, 0, line, 1, 1); + table.attach(&row.value, 1, line, 1, 1); + table.attach(&row.copy, 2, line, 1, 1); + figures.push(row); + line += 1; + } + let content = gtk::Box::builder() + .orientation(gtk::Orientation::Vertical) + .spacing(8) + .margin_top(6) + .margin_bottom(6) + .margin_start(6) + .margin_end(6) + .build(); + content.append(&heading); + content.append(&table); + let button = gtk::MenuButton::builder() + .popover(>k::Popover::builder().child(&content).build()) + .direction(gtk::ArrowType::Up) + .can_shrink(true) + .build(); + button.add_css_class("flat"); + button.add_css_class("caption"); + button.add_css_class("numeric"); + let revealer = gtk::Revealer::builder() + .transition_type(gtk::RevealerTransitionType::SlideRight) + .child(&button) + .build(); + Self { + revealer, + button, + heading, + separator, + figures, + shown: RefCell::default(), + } + } + + /// While a run is out, Copy would take figures of rows no longer + /// selected. + fn set_current(&self, current: bool) { + for row in &self.figures { + row.copy.set_sensitive(current); + } + } + + fn show(&self, shown: Option) { + self.set_current(true); + if *self.shown.borrow() == shown { + return; + } + match &shown { + Some(shown) => { + let sentence = sentence(&shown.summary); + self.button.set_label(&sentence); + // The label ellipsizes in a narrow bar; the tooltip keeps every digit. + self.button.set_tooltip_text(Some(&sentence)); + self.heading.set_label(&shown.column); + for row in &self.figures { + row.show(row.figure.of(&shown.summary)); + } + self.separator.set_visible(shown.summary.numbers.is_some()); + } + None => self.button.popdown(), + } + self.revealer.set_reveal_child(shown.is_some()); + self.shown.replace(shown); + } +} + +fn sentence(summary: &SelectionSummary) -> String { + let count = format_thousands(summary.value_count); + match &summary.numbers { + Some(numbers) => gettext_f( + "Sum {sum}, Average {average}, Count {count}", + &[ + ("sum", &format_decimal(&numbers.sum)), + ("average", &format_decimal(&numbers.average)), + ("count", &count), + ], + ), + None => gettext_f("Count {count}", &[("count", &count)]), + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Figure { + Count, + Numbers, + Empty, + NotANumber, + Sum, + Average, + Minimum, + Maximum, +} + +impl Figure { + const ALL: [Self; 8] = [ + Self::Count, + Self::Numbers, + Self::Empty, + Self::NotANumber, + Self::Sum, + Self::Average, + Self::Minimum, + Self::Maximum, + ]; + + fn title(self) -> String { + match self { + Self::Count => gettext("Count"), + // Translators: how many of the summed cells hold a number. + Self::Numbers => gettext("Numbers"), + Self::Empty => gettext("Empty"), + Self::NotANumber => gettext("Not a Number"), + Self::Sum => gettext("Sum"), + Self::Average => gettext("Average"), + Self::Minimum => gettext("Minimum"), + Self::Maximum => gettext("Maximum"), + } + } + + fn copy_title(self) -> String { + match self { + Self::Count => gettext("Copy Count"), + Self::Numbers => gettext("Copy Number Count"), + Self::Empty => gettext("Copy Empty Count"), + Self::NotANumber => gettext("Copy Not a Number Count"), + Self::Sum => gettext("Copy Sum"), + Self::Average => gettext("Copy Average"), + Self::Minimum => gettext("Copy Minimum"), + Self::Maximum => gettext("Copy Maximum"), + } + } + + /// The figure as the locale writes it and as Copy puts it on the + /// clipboard, when the summary has it. + fn of(self, summary: &SelectionSummary) -> Option<(String, String)> { + let count = |n: u64| (format_thousands(n), n.to_string()); + let number = |plain: &String| (format_decimal(plain), plain.clone()); + let numbers = summary.numbers.as_ref(); + match self { + Self::Count => Some(count(summary.value_count)), + Self::Numbers => Some(count(numbers.map_or(0, |numbers| numbers.count))), + Self::Empty => Some(count(summary.empty_count)), + Self::NotANumber => (summary.not_a_number_count > 0).then(|| count(summary.not_a_number_count)), + Self::Sum => numbers.map(|numbers| number(&numbers.sum)), + Self::Average => numbers.map(|numbers| number(&numbers.average)), + Self::Minimum => numbers.map(|numbers| number(&numbers.minimum)), + Self::Maximum => numbers.map(|numbers| number(&numbers.maximum)), + } + } +} + +struct FigureRow { + figure: Figure, + title: gtk::Label, + value: gtk::Label, + copy: gtk::Button, + copied: Rc>, +} + +impl FigureRow { + fn new(grid: &DataGrid, figure: Figure) -> Self { + let title = gtk::Label::builder().label(figure.title()).xalign(0.0).build(); + title.add_css_class("dimmed"); + // A sum can run to thousands of digits; Copy still takes every one. + let value = gtk::Label::builder() + .xalign(1.0) + .hexpand(true) + .ellipsize(gtk::pango::EllipsizeMode::Middle) + .max_width_chars(32) + .build(); + value.add_css_class("numeric"); + value.update_relation(&[gtk::accessible::Relation::LabelledBy(&[title.upcast_ref()])]); + let copy_title = figure.copy_title(); + let copy = gtk::Button::builder() + .icon_name(icons::EDIT_COPY) + .tooltip_text(©_title) + .valign(gtk::Align::Center) + .build(); + copy.add_css_class("flat"); + copy.update_property(&[gtk::accessible::Property::Label(©_title)]); + let copied: Rc> = Rc::default(); + let text = copied.clone(); + let grid = grid.downgrade(); + copy.connect_clicked(move |_| { + if let Some(grid) = grid.upgrade() { + crate::ui::clipboard::copy_text(&grid, text.borrow().clone(), |message| grid.toast(message)); + } + }); + Self { + figure, + title, + value, + copy, + copied, + } + } + + fn show(&self, figure: Option<(String, String)>) { + let shown = figure.is_some(); + if let Some((text, copied)) = figure { + self.value.set_label(&text); + self.copied.replace(copied); + } + self.title.set_visible(shown); + self.value.set_visible(shown); + self.copy.set_visible(shown); + } +} + +impl DataGrid { + /// The readout for a host to place in its bottom bar. A grid sums + /// nothing until one is placed. + pub(crate) fn selection_summary(&self) -> gtk::Widget { + let state = &self.imp().summary; + let placed = state.readout.get().is_some(); + let widget = state + .readout + .get_or_init(|| Readout::new(self)) + .revealer + .clone() + .upcast(); + if !placed { + self.refresh_summary(); + } + widget + } + + /// Sum again once the main loop is idle, so a key press that moves + /// both the focus and the selection sums once. + pub(super) fn refresh_summary(&self) { + let state = &self.imp().summary; + if let Some(cell) = self.focused_cell() { + state.column.set(Some(cell.column())); + } + if state.readout.get().is_none() || state.queued.replace(true) { + return; + } + let grid = self.downgrade(); + glib::idle_add_local_once(move || { + if let Some(grid) = grid.upgrade() { + grid.imp().summary.queued.set(false); + grid.start_summary(); + } + }); + } + + /// Sum the values off the GTK thread: a page holds up to 100,000 rows. + /// A newer run stops this one. + pub(super) fn start_summary(&self) { + let state = &self.imp().summary; + let Some(readout) = state.readout.get() else { + return; + }; + state.stop(); + let run = state.runs.get().wrapping_add(1); + state.runs.set(run); + let tasks = self.config().map(|config| config.tasks.clone()); + let (Some(tasks), Some(input)) = (tasks, self.summary_input()) else { + readout.show(None); + return; + }; + let SummaryInput { + column, + index, + rule, + rows, + } = input; + let cancelled = Arc::new(AtomicBool::new(false)); + state.running.replace(Some(cancelled.clone())); + readout.set_current(false); + let grid = self.downgrade(); + glib::spawn_future_local(async move { + let answer = tasks + .spawn_blocking_task(move || { + let values = rows.iter().map(|row| row.value(index)); + summarize(values, rule, &|| cancelled.load(Ordering::Relaxed)) + }) + .await; + let Some(grid) = grid.upgrade() else { + return; + }; + let state = &grid.imp().summary; + if state.runs.get() != run { + return; + } + state.running.take(); + let Some(readout) = state.readout.get() else { + return; + }; + match answer { + Ok(Some(summary)) => readout.show(Some(Shown { column, summary })), + Ok(None) => {} + Err(failure) => { + tracing::warn!(%failure, "the selection could not be summed"); + readout.show(None); + } + } + }); + } + + /// The selected rows as the cells show them, sharing the cells the page + /// read. Rows marked for deletion are left out, also once a save failed + /// on them. + fn summary_input(&self) -> Option { + let rows = self.selected_rows(); + if rows.len() < MIN_ROWS { + return None; + } + let index = self.imp().summary.column.get()?; + if !self.shown_columns().contains(&index) { + return None; + } + let columns = self.grid_columns(); + let column = columns.iter().find(|column| column.index == index)?; + let tracked = self.tracked(); + let rows = rows + .iter() + .filter(|row| !tracked.mark(row, index).row_deleted) + .map(|row| tracked.column_snapshot(row, index)) + .collect(); + Some(SummaryInput { + column: column.name.clone(), + index, + rule: SummaryRule::for_column(column), + rows, + }) + } +} + +struct SummaryInput { + column: String, + index: usize, + rule: SummaryRule, + rows: Vec, +} diff --git a/linux/crates/app/src/ui/data_grid/tests/mod.rs b/linux/crates/app/src/ui/data_grid/tests/mod.rs index d0d46eec1f..c218bade72 100644 --- a/linux/crates/app/src/ui/data_grid/tests/mod.rs +++ b/linux/crates/app/src/ui/data_grid/tests/mod.rs @@ -6,6 +6,7 @@ mod layout; mod menus; mod selection; mod sorting; +mod summary; mod values; mod view; diff --git a/linux/crates/app/src/ui/data_grid/tests/summary.rs b/linux/crates/app/src/ui/data_grid/tests/summary.rs new file mode 100644 index 0000000000..3b26c54766 --- /dev/null +++ b/linux/crates/app/src/ui/data_grid/tests/summary.rs @@ -0,0 +1,309 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use gtk4 as gtk; +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use tablepro_core::Value; +use tablepro_core::value::SqlDecimal; + +use super::super::GridRequest; +use super::super::test_grid::{Grid, WAIT, key, named}; +use crate::services::tracker_edit::TrackerEdit; +use crate::test_support::{descendants, drain_main_context, finish_on_main, wait_until}; + +fn text(value: &str) -> Value { + Value::Text(value.to_owned()) +} + +/// Amounts 10, 32 and NULL, keyed 1 to 3. +fn amounts() -> Grid { + Grid::open( + vec![ + named("id", "integer", true), + named("amount", "integer", false), + named("name", "text", false), + ], + vec![ + vec![Value::Int(1), Value::Int(10), text("a")], + vec![Value::Int(2), Value::Int(32), text("b")], + vec![Value::Int(3), Value::Null, text("c")], + ], + ) +} + +fn readout(grid: &Grid) -> (gtk::Revealer, gtk::MenuButton) { + let revealer = grid + .grid + .selection_summary() + .downcast::() + .expect("the readout is a revealer"); + let button = revealer + .child() + .and_downcast::() + .expect("the readout's menu button"); + (revealer, button) +} + +fn wait_for_label(button: >k::MenuButton, expected: &str) { + wait_until(WAIT, || button.label().as_deref() == Some(expected)) + .unwrap_or_else(|_| panic!("the readout said {:?}, not {expected:?}", button.label())); +} + +fn wait_hidden(revealer: >k::Revealer) { + wait_until(WAIT, || !revealer.reveals_child()).expect("the readout hides"); +} + +#[gtk4::test] +fn sums_the_focused_column_over_the_selected_rows() { + let grid = amounts(); + let (revealer, button) = readout(&grid); + drain_main_context(); + assert!(!revealer.reveals_child(), "nothing is selected yet"); + + grid.focus(0, 1); + grid.grid.selection().select_all(); + + wait_for_label(&button, "Sum 42, Average 21, Count 2"); + assert!(revealer.reveals_child()); + assert_eq!(button.tooltip_text().as_deref(), Some("Sum 42, Average 21, Count 2")); +} + +#[gtk4::test] +fn a_text_column_only_counts() { + let grid = amounts(); + let (_, button) = readout(&grid); + + grid.focus(0, 2); + grid.grid.selection().select_all(); + + wait_for_label(&button, "Count 3"); +} + +#[gtk4::test] +fn a_single_row_shows_no_summary() { + let grid = amounts(); + let (revealer, button) = readout(&grid); + grid.focus(0, 1); + grid.grid.selection().select_all(); + wait_for_label(&button, "Sum 42, Average 21, Count 2"); + + grid.grid.selection().select_item(1, true); + + wait_hidden(&revealer); +} + +#[gtk4::test] +fn an_edit_counts_as_the_cell_shows_it_while_the_focus_is_elsewhere() { + let grid = amounts(); + let (revealer, button) = readout(&grid); + grid.focus(0, 1); + grid.grid.selection().select_all(); + wait_for_label(&button, "Sum 42, Average 21, Count 2"); + + gtk::prelude::GtkWindowExt::set_focus(&grid.window, None::<>k::Widget>); + grid.grid.apply_delta(&grid.tracker.apply(TrackerEdit::Cell { + row_key: key(1), + col: 1, + original: Value::Int(10), + value: Value::Int(20), + })); + + wait_for_label(&button, "Sum 52, Average 26, Count 2"); + assert!(revealer.reveals_child(), "the focus leaving the cells hid the readout"); +} + +#[gtk4::test] +fn rows_marked_for_deletion_are_left_out_also_after_a_failed_save() { + let grid = amounts(); + let (_, button) = readout(&grid); + grid.focus(0, 1); + grid.grid.selection().select_all(); + wait_for_label(&button, "Sum 42, Average 21, Count 2"); + + grid.grid.apply_delta(&grid.tracker.apply(TrackerEdit::DeleteRows { + persisted: vec![(key(2), vec![Value::Int(2), Value::Int(32), text("b")].into())], + drafts: Vec::new(), + })); + wait_for_label(&button, "Sum 10, Average 10, Count 1"); + + grid.grid.apply_delta(&grid.tracker.mark_error_row(key(2))); + grid.grid.apply_delta(&grid.tracker.apply(TrackerEdit::Cell { + row_key: key(1), + col: 1, + original: Value::Int(10), + value: Value::Int(11), + })); + wait_for_label(&button, "Sum 11, Average 11, Count 1"); +} + +#[gtk4::test] +fn a_hidden_column_is_not_summed() { + let grid = amounts(); + let (revealer, button) = readout(&grid); + grid.focus(0, 1); + grid.grid.selection().select_all(); + wait_for_label(&button, "Sum 42, Average 21, Count 2"); + + grid.grid.set_column_visible(1, false); + + // GTK may move the focus to a cell still shown, whose column is summed then. + wait_until(WAIT, || { + !revealer.reveals_child() || button.label().as_deref() != Some("Sum 42, Average 21, Count 2") + }) + .expect("the hidden column is still summed"); +} + +#[gtk4::test] +fn the_readout_keeps_its_height_while_hidden() { + let grid = amounts(); + let (revealer, button) = readout(&grid); + let footer = gtk::Box::new(gtk::Orientation::Horizontal, 6); + footer.append(>k::Label::new(Some("3 rows"))); + footer.append(&revealer); + let hidden = footer.measure(gtk::Orientation::Vertical, -1).1; + + grid.focus(0, 1); + grid.grid.selection().select_all(); + wait_for_label(&button, "Sum 42, Average 21, Count 2"); + + assert_eq!(footer.measure(gtk::Orientation::Vertical, -1).1, hidden); +} + +#[gtk4::test] +fn the_popover_lists_each_figure_and_copies_it_without_grouping() { + let decimal = |text: &str| Value::Decimal(text.parse::().expect("a decimal")); + let grid = Grid::open( + vec![named("id", "integer", true), named("price", "numeric(10,2)", false)], + vec![ + vec![Value::Int(1), decimal("1234.5")], + vec![Value::Int(2), decimal("0.25")], + vec![Value::Int(3), decimal("NaN")], + ], + ); + let toasts: Rc>> = Rc::default(); + let log = toasts.clone(); + grid.grid.connect_request(move |_, request| { + if let GridRequest::Toast(text) = request { + log.borrow_mut().push(text); + } + }); + let (_, button) = readout(&grid); + grid.focus(0, 1); + grid.grid.selection().select_all(); + // CI runs in the C locale, which groups as English does. + wait_for_label(&button, "Sum 1,234.75, Average 617.375, Count 3"); + + let popover = button.popover().expect("the readout's popover"); + let labels: Vec = descendants(&popover) + .into_iter() + .filter_map(|widget| widget.downcast::().ok()) + .filter(|label| label.get_visible()) + .map(|label| label.label().to_string()) + .collect(); + assert_eq!( + labels, + [ + "price", + "Count", + "3", + "Numbers", + "2", + "Empty", + "0", + "Not a Number", + "1", + "Sum", + "1,234.75", + "Average", + "617.375", + "Minimum", + "0.25", + "Maximum", + "1,234.50", + ] + ); + + let copy_maximum = descendants(&popover) + .into_iter() + .filter_map(|widget| widget.downcast::().ok()) + .find(|copy| copy.tooltip_text().as_deref() == Some("Copy Maximum")) + .expect("Copy Maximum"); + assert!(copy_maximum.is_sensitive(), "Copy stayed off after the figures came in"); + copy_maximum.emit_clicked(); + wait_until(WAIT, || !toasts.borrow().is_empty()).expect("the copy lands"); + let clipboard = grid.grid.clipboard(); + let copied = finish_on_main(WAIT, async move { clipboard.read_text_future().await }) + .expect("the clipboard to answer") + .expect("text on the clipboard"); + assert_eq!(copied.as_deref(), Some("1234.50")); +} + +#[gtk4::test] +fn a_long_figure_keeps_its_popover_row_on_one_line() { + let grid = amounts(); + let (_, button) = readout(&grid); + grid.focus(0, 1); + grid.grid.selection().select_all(); + wait_for_label(&button, "Sum 42, Average 21, Count 2"); + let popover = button.popover().expect("the readout's popover"); + let height = || { + popover + .child() + .expect("the popover's content") + .measure(gtk::Orientation::Vertical, -1) + .1 + }; + let short = height(); + + let wide = format!("1{}", "0".repeat(1_500)) + .parse::() + .expect("a decimal"); + grid.grid.apply_delta(&grid.tracker.apply(TrackerEdit::Cell { + row_key: key(1), + col: 1, + original: Value::Int(10), + value: Value::Decimal(wide), + })); + wait_until(WAIT, || button.label().is_some_and(|label| label.len() > 1_000)).expect("the wide sum"); + + assert_eq!(height(), short, "a long figure made the popover taller"); +} + +#[gtk4::test] +fn the_readout_goes_with_its_grid() { + let grid = amounts(); + let (revealer, button) = readout(&grid); + grid.focus(0, 1); + grid.grid.selection().select_all(); + wait_for_label(&button, "Sum 42, Average 21, Count 2"); + let (revealer_ref, button_ref) = (revealer.downgrade(), button.downgrade()); + drop((revealer, button)); + + drop(grid); + drain_main_context(); + + assert!(revealer_ref.upgrade().is_none(), "the readout outlived its grid"); + assert!(button_ref.upgrade().is_none(), "the menu button outlived its grid"); +} + +#[gtk4::test] +fn closing_the_grid_stops_the_run_still_out() { + let grid = amounts(); + readout(&grid); + grid.focus(0, 1); + grid.grid.selection().select_all(); + drain_main_context(); + // The answer lands on a context nothing runs, so the run is still out + // when the grid goes. + let parked = gtk::glib::MainContext::new(); + parked + .with_thread_default(|| grid.grid.start_summary()) + .expect("the context is free"); + let running = grid.grid.imp().summary.running.borrow().clone().expect("the run"); + + drop(grid); + drain_main_context(); + + assert!(running.load(std::sync::atomic::Ordering::Relaxed)); +} diff --git a/linux/crates/app/src/ui/data_grid/tracked_values.rs b/linux/crates/app/src/ui/data_grid/tracked_values.rs index 0bf12b3739..8e9aac6670 100644 --- a/linux/crates/app/src/ui/data_grid/tracked_values.rs +++ b/linux/crates/app/src/ui/data_grid/tracked_values.rs @@ -217,6 +217,17 @@ impl TrackedValues { RowSnapshot::new(cells, overrides) } + /// `snapshot` that reads only `column` through the tracker, for a + /// caller that reads no other column of the row. + pub fn column_snapshot(&self, row: &GridRow, column: usize) -> RowSnapshot { + let cells = row.cells().unwrap_or_else(|| Arc::from([])); + let pending = self + .tracked_key(row) + .and_then(|(tracker, key)| tracker.cell_overlay(&key, column).pending) + .map(|input| (column, input.value().cloned().unwrap_or(Value::Null))); + RowSnapshot::new(cells, pending.into_iter().collect()) + } + /// Like `snapshot`, except a draft's cell left to the server stays /// `Default`. pub fn inputs(&self, row: &GridRow) -> Vec { @@ -279,11 +290,25 @@ mod tests { }); assert!(values.with_value(&row, 1, |value, _| *value == Value::Int(6))); + assert_eq!(values.column_snapshot(&row, 1).value(1), &Value::Int(6)); assert_eq!(values.edit_input(&row, 1), CellInput::Value(Value::Int(6))); assert_eq!(values.row_state(&row), RowPendingState::Modified); assert_eq!(values.mark(&row, 1).state, CellState::Modified); } + #[test] + fn a_column_snapshot_of_a_draft_reads_its_cell_and_a_default_as_null() { + let tracker = TabTrackerHandle::default(); + let values = TrackedValues::tracked(tracker.clone(), keyed()); + let delta = tracker.apply(TrackerEdit::InsertDrafts { + rows: vec![vec![CellInput::Default, CellInput::Value(Value::Int(7))]], + }); + let row = GridRow::new(RowData::draft(delta.inserted_drafts[0])); + + assert_eq!(values.column_snapshot(&row, 1).value(1), &Value::Int(7)); + assert_eq!(values.column_snapshot(&row, 0).value(0), &Value::Null); + } + #[test] fn every_pending_state_has_an_icon_and_a_sentence() { for state in [ diff --git a/linux/crates/app/src/ui/format/mod.rs b/linux/crates/app/src/ui/format/mod.rs index ade92482ee..b05034fe7f 100644 --- a/linux/crates/app/src/ui/format/mod.rs +++ b/linux/crates/app/src/ui/format/mod.rs @@ -4,7 +4,7 @@ mod thousands; #[cfg(test)] pub(crate) use self::thousands::Thousands; -pub(crate) use self::thousands::format_thousands; +pub(crate) use self::thousands::{format_decimal, format_thousands}; /// The count gettext picks a plural form by. It takes a u32, and a /// result past that is rare enough for the largest u32 to stand in. diff --git a/linux/crates/app/src/ui/format/thousands.rs b/linux/crates/app/src/ui/format/thousands.rs index b3395e50df..a77c58fd6d 100644 --- a/linux/crates/app/src/ui/format/thousands.rs +++ b/linux/crates/app/src/ui/format/thousands.rs @@ -36,6 +36,17 @@ impl Thousands { None => n.to_string(), } } + + /// `plain`, a decimal number written with a `.` and no grouping, + /// grouped and with the locale's decimal separator. Every fraction + /// digit stays, trailing zeros included, so cents read as cents. + pub(crate) fn format_decimal(&self, plain: &str) -> String { + let parsed = Decimal::try_from_str(plain); + match (&self.formatter, parsed) { + (Some(formatter), Ok(decimal)) => formatter.format_to_string(&decimal), + _ => plain.to_owned(), + } + } } // A cache, not state: every window formats with the process's locale, @@ -57,6 +68,11 @@ pub(crate) fn format_thousands(n: u64) -> String { USER.with(|thousands| thousands.format(n)) } +/// `plain` decimal text as the user's locale writes numbers. +pub(crate) fn format_decimal(plain: &str) -> String { + USER.with(|thousands| thousands.format_decimal(plain)) +} + #[cfg(test)] mod tests { use super::*; @@ -97,4 +113,20 @@ mod tests { assert_eq!(Thousands::for_posix_locale("").format(1_000), "1,000"); assert_eq!(Thousands::for_posix_locale("not a locale").format(1_000), "1,000"); } + + #[test] + fn a_decimal_keeps_its_fraction_digits_and_the_locale_separator() { + let english = Thousands::for_posix_locale("en_US.UTF-8"); + let italian = Thousands::for_posix_locale("it_IT.UTF-8"); + + assert_eq!(english.format_decimal("1234567.50"), "1,234,567.50"); + assert_eq!(english.format_decimal("-0.025"), "-0.025"); + assert_eq!(italian.format_decimal("12345.5"), "12.345,5"); + assert_eq!( + italian.format_decimal("1234.5"), + "1234,5", + "four digits stay together in Italian" + ); + assert_eq!(english.format_decimal("not a number"), "not a number"); + } } diff --git a/linux/crates/app/src/ui/key_tab/mod.rs b/linux/crates/app/src/ui/key_tab/mod.rs index c30341e809..441d877497 100644 --- a/linux/crates/app/src/ui/key_tab/mod.rs +++ b/linux/crates/app/src/ui/key_tab/mod.rs @@ -344,6 +344,7 @@ fn content_page(grid: &DataGrid) -> (gtk::Box, ContentParts) { .build(); more.add_css_class("flat"); footer_bar.append(&footer); + footer_bar.append(&grid.selection_summary()); footer_bar.append(&more); page.append(&header); page.append(>k::Separator::new(gtk::Orientation::Horizontal)); diff --git a/linux/crates/app/src/ui/key_tab/tests.rs b/linux/crates/app/src/ui/key_tab/tests.rs index c6afe185a1..bb491aac75 100644 --- a/linux/crates/app/src/ui/key_tab/tests.rs +++ b/linux/crates/app/src/ui/key_tab/tests.rs @@ -198,6 +198,44 @@ fn a_refresh_keeps_the_grid_and_its_sort() { assert_eq!(shown_elements(&tab), ["element2", "element1", "element0"]); } +#[gtk4::test] +fn the_footer_sums_the_selected_entries() { + let mut script = ConnectionScript::default(); + script.read_key.always(FakeReply::ok(Some(list_page(0, 3, None)))); + let (tab, _fake, _runtime) = opened(script); + let window = crate::test_support::OwnedWindow::presented(gtk::Window::builder().child(tab.widget()).build()); + wait_until(WAIT, || tab.rows() == 3 && tab.grid().width() > 0).expect("the key, laid out"); + let readout = tab + .grid() + .selection_summary() + .downcast::() + .expect("the readout"); + assert!( + readout.is_ancestor(tab.widget()), + "the readout is not in the key's footer" + ); + + wait_until(WAIT, || tab.grid().focus_cell(0, 0).is_some()).expect("a cell takes the focus"); + tab.grid().selection().select_all(); + + let button = readout + .child() + .and_downcast::() + .expect("the menu button"); + wait_until(WAIT, || button.label().as_deref() == Some("Sum 3, Average 1, Count 3")).expect("the index summed"); + + button.popup(); + + // The figures are in place before the popover opens and takes the focus. + let first_copy = || { + gtk::prelude::GtkWindowExt::focus(&*window) + .and_downcast::() + .and_then(|focused| focused.tooltip_text()) + .is_some_and(|tooltip| tooltip == "Copy Count") + }; + wait_until(WAIT, first_copy).expect("the popover's first Copy button takes the focus"); +} + /// The header's Row Details toggle reaches the tab's grid. #[gtk4::test] fn row_details_reaches_the_key_s_grid() { diff --git a/linux/crates/app/src/ui/result_pane/mod.rs b/linux/crates/app/src/ui/result_pane/mod.rs index fa6a62603d..d0046c16a0 100644 --- a/linux/crates/app/src/ui/result_pane/mod.rs +++ b/linux/crates/app/src/ui/result_pane/mod.rs @@ -69,7 +69,10 @@ pub(crate) fn result_set_page( let page = gtk::Box::builder().orientation(gtk::Orientation::Vertical).build(); page.append(banner.widget()); page.append(&grid); - page.append(&row_count_footer(view.row_count() as u64)); + let footer = gtk::Box::builder().spacing(6).build(); + footer.append(&row_count_footer(view.row_count() as u64)); + footer.append(&grid.selection_summary()); + page.append(&footer); (page.upcast(), grid) } @@ -87,7 +90,7 @@ fn row_count_footer(shown: u64) -> gtk::Label { .margin_top(4) .margin_bottom(4) .build(); - footer.add_css_class("dim-label"); + footer.add_css_class("dimmed"); footer.add_css_class("caption"); footer } diff --git a/linux/crates/app/src/ui/structure_tab/save_handlers.rs b/linux/crates/app/src/ui/structure_tab/save_handlers.rs index 03ed1318dd..a28efa38c0 100644 --- a/linux/crates/app/src/ui/structure_tab/save_handlers.rs +++ b/linux/crates/app/src/ui/structure_tab/save_handlers.rs @@ -37,10 +37,6 @@ impl StructureModel { SaveInput::Discard => { // This also lets go of a save whose outcome was unknown. self.tracker.clear(); - if self.mode() == StructureMode::Edit && self.definition_stale { - fx.input(SaveInput::Reload); - return; - } // New mode clears everything but the name, since a new // table starts empty. let restored = match self.context.borrow().original() { @@ -49,6 +45,11 @@ impl StructureModel { }; self.show_draft(restored); self.recompute_dirty_state(fx); + // The read keeps the restored definition on screen while it + // runs and through a failure, never the discarded edits. + if self.mode() == StructureMode::Edit && self.definition_stale { + fx.input(SaveInput::Reload); + } } SaveInput::SaveCompleted { new_table_name } => self.save_completed(new_table_name, fx), SaveInput::SaveFailed(message) => { diff --git a/linux/crates/app/src/ui/structure_tab/tests.rs b/linux/crates/app/src/ui/structure_tab/tests.rs index c3c1fd3ea2..3c28fa2a04 100644 --- a/linux/crates/app/src/ui/structure_tab/tests.rs +++ b/linux/crates/app/src/ui/structure_tab/tests.rs @@ -1110,6 +1110,24 @@ fn a_discard_after_a_save_found_the_table_changed_reads_it_again() { assert_eq!(reads_after_discard(Some(SaveInput::TableChangedBeforeSave.into())), 2); } +#[gtk4::test] +fn a_discard_whose_read_fails_leaves_the_discarded_column_out() { + let mut script = postgres_script(); + script + .fetch_table_definition + .once(FakeReply::ok(definition("id"))) + .always(FakeReply::err(DriverError::server("denied"))); + let harness = Harness::open(StructureMode::Edit, script, []); + wait_until(WAIT, || harness.page().as_deref() == Some("editor")).expect("the structure"); + harness.tab().send(ColumnsInput::AddColumn); + harness.tab().send(SaveInput::TableChangedBeforeSave); + + harness.tab().send(SaveInput::Discard); + + wait_until(WAIT, || banner(&harness).is_revealed()).expect("the failed read was not shown"); + assert_eq!(harness.column_names(), ["id"]); +} + /// The titles of the add buttons the tab shows under `container`. fn add_buttons(container: >k::Box) -> Vec { descendants(container) diff --git a/linux/crates/core/src/value/decimal.rs b/linux/crates/core/src/value/decimal.rs index afb4306746..e00e8bd20e 100644 --- a/linux/crates/core/src/value/decimal.rs +++ b/linux/crates/core/src/value/decimal.rs @@ -132,6 +132,18 @@ impl SqlDecimal { Some((if *negative { -magnitude } else { magnitude }, *scale)) } + /// What `from_parts` takes: the sign, the digits and the scale. + pub fn parts(&self) -> Option<(bool, &str, u32)> { + match &self.0 { + DecimalRepr::Finite { + negative, + coefficient, + scale, + } => Some((*negative, &**coefficient, *scale)), + _ => None, + } + } + pub fn fits(&self, precision: u32, scale: u32) -> bool { let DecimalRepr::Finite { coefficient, @@ -442,6 +454,19 @@ mod tests { assert_eq!(SqlDecimal::from_scaled_i128(-125, 2).to_string(), "-1.25"); } + #[test] + fn parts_round_trip_through_from_parts() { + let wide = format!("-{}.5", "9".repeat(60)); + let digits = format!("{}5", "9".repeat(60)); + assert_eq!(parse(&wide).parts(), Some((true, digits.as_str(), 1))); + assert_eq!(parse("0.10").parts(), Some((false, "10", 2))); + let cents = parse("-12.30"); + let (negative, coefficient, scale) = cents.parts().unwrap(); + assert_eq!(SqlDecimal::from_parts(negative, coefficient, scale), Ok(cents.clone())); + assert_eq!(SqlDecimal::nan().parts(), None); + assert_eq!(SqlDecimal::infinity(true).parts(), None); + } + #[test] fn cmp_numeric_orders_across_scales_and_specials() { let ordered = [ diff --git a/linux/data/resources/ui/browse-tab.ui b/linux/data/resources/ui/browse-tab.ui index a997e126c5..53d43a0eb7 100644 --- a/linux/data/resources/ui/browse-tab.ui +++ b/linux/data/resources/ui/browse-tab.ui @@ -95,7 +95,7 @@ - + list-add-symbolic diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index 471b784245..f56ea2ca6a 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -91,6 +91,7 @@ crates/app/src/services/grid/grid_row_id.rs crates/app/src/services/grid/mod.rs crates/app/src/services/grid/row_key_builder.rs crates/app/src/services/grid/row_snapshot.rs +crates/app/src/services/grid/selection_summary.rs crates/app/src/services/grid/test_columns.rs crates/app/src/services/grid/value_order.rs crates/app/src/services/history_availability.rs @@ -269,6 +270,7 @@ crates/app/src/ui/data_grid/row_detail/value_editor.rs crates/app/src/ui/data_grid/row_json_dialog.rs crates/app/src/ui/data_grid/row_number_column.rs crates/app/src/ui/data_grid/selection.rs +crates/app/src/ui/data_grid/selection_summary.rs crates/app/src/ui/data_grid/server_sorter.rs crates/app/src/ui/data_grid/table_keys.rs crates/app/src/ui/data_grid/test_grid.rs @@ -281,6 +283,7 @@ crates/app/src/ui/data_grid/tests/menus.rs crates/app/src/ui/data_grid/tests/mod.rs crates/app/src/ui/data_grid/tests/selection.rs crates/app/src/ui/data_grid/tests/sorting.rs +crates/app/src/ui/data_grid/tests/summary.rs crates/app/src/ui/data_grid/tests/values.rs crates/app/src/ui/data_grid/tests/view.rs crates/app/src/ui/data_grid/tracked_values.rs From 6d218504d394cd6ec014ca0a17fc183f8f0b23d0 Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 05:25:37 +0100 Subject: [PATCH 14/85] feat(whats-new): announce an update on Linux and stay quiet after a downgrade in every app (#3878) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- .../Storage/Preferences/PreferenceKeys.swift | 7 +- .../Views/WhatsNew/WhatsNewNotice.swift | 16 +- .../Onboarding/FirstRunPlan.swift | 7 +- .../Platform/OnboardingPreferences.swift | 3 +- .../Onboarding/FirstRunPlanTests.swift | 24 ++ .../OnboardingPreferencesTests.swift | 21 ++ .../Views/WhatsNewContentTests.swift | 70 ++++- apple/changelog.d/3878.fixed.1.md | 1 + apple/changelog.d/3878.fixed.md | 1 + linux/Cargo.lock | 1 + linux/changelog.d/3878.added.md | 1 + linux/crates/app/Cargo.toml | 1 + .../app/src/application/about_dialog.rs | 36 ++- .../app/src/application/crash_notice.rs | 62 ---- linux/crates/app/src/application/imp.rs | 5 +- .../app/src/application/launch_notice.rs | 135 +++++++++ linux/crates/app/src/application/mod.rs | 4 +- linux/crates/app/src/application/tests.rs | 57 ++++ linux/crates/app/src/ui/mod.rs | 2 +- .../app/src/ui/session_window/crash_notice.rs | 159 ----------- linux/crates/app/src/ui/session_window/imp.rs | 5 +- .../src/ui/session_window/launch_notice.rs | 265 ++++++++++++++++++ linux/crates/app/src/ui/session_window/mod.rs | 5 +- linux/crates/storage/src/settings/keys.rs | 1 + linux/crates/storage/src/settings/mod.rs | 8 + linux/crates/storage/tests/settings.rs | 11 + .../data/app.tablepro.TablePro.gschema.xml.in | 6 + linux/docs/storage.md | 3 +- linux/po/POTFILES.in | 4 +- 29 files changed, 659 insertions(+), 262 deletions(-) create mode 100644 apple/changelog.d/3878.fixed.1.md create mode 100644 apple/changelog.d/3878.fixed.md create mode 100644 linux/changelog.d/3878.added.md delete mode 100644 linux/crates/app/src/application/crash_notice.rs create mode 100644 linux/crates/app/src/application/launch_notice.rs delete mode 100644 linux/crates/app/src/ui/session_window/crash_notice.rs create mode 100644 linux/crates/app/src/ui/session_window/launch_notice.rs diff --git a/apple/TablePro/Core/Storage/Preferences/PreferenceKeys.swift b/apple/TablePro/Core/Storage/Preferences/PreferenceKeys.swift index eac9c00c55..a6514424f8 100644 --- a/apple/TablePro/Core/Storage/Preferences/PreferenceKeys.swift +++ b/apple/TablePro/Core/Storage/Preferences/PreferenceKeys.swift @@ -13,8 +13,10 @@ enum PreferenceKeys { static let queryPlanRawFontSize = DefaultsKey("com.TablePro.queryPlan.rawFontSize") static let queryPlanBarMetric = DefaultsKey("com.TablePro.queryPlan.barMetric") static let lastBackupDirectory = DefaultsKey("com.TablePro.backup.lastDirectory") - /// The app version this Mac last showed the welcome window for. Device-local: it records what - /// has been shown here, not a preference, so it must not sync to another Mac that has not. + /// The newest build this Mac has run, for the Welcome window's What's New link. Device-local: it + /// records what has been shown here, not a preference, so it must not sync to another Mac. + static let lastSeenAppBuild = DefaultsKey("com.TablePro.welcome.lastSeenAppBuild") + /// What a build before `lastSeenAppBuild` recorded instead, read once to carry it over. static let lastSeenAppVersion = DefaultsKey("com.TablePro.welcome.lastSeenAppVersion") static let connectionListSortMode = DefaultsKey("com.TablePro.connectionList.sortMode") static let connectionListFavoritesOrder = DefaultsKey<[String]>("com.TablePro.connectionList.favoritesOrder") @@ -39,6 +41,7 @@ enum PreferenceKeys { queryPlanRawFontSize.name, queryPlanBarMetric.name, lastBackupDirectory.name, + lastSeenAppBuild.name, lastSeenAppVersion.name, receivesBetaUpdates.name, ] diff --git a/apple/TablePro/Views/WhatsNew/WhatsNewNotice.swift b/apple/TablePro/Views/WhatsNew/WhatsNewNotice.swift index 015b08ea77..4157ad3e60 100644 --- a/apple/TablePro/Views/WhatsNew/WhatsNewNotice.swift +++ b/apple/TablePro/Views/WhatsNew/WhatsNewNotice.swift @@ -7,10 +7,18 @@ internal struct WhatsNewNotice { func recordLaunch() -> Bool { // A build without notes records nothing, so a Debug run never marks a release as seen. guard WhatsNewContent.load(from: bundle) != nil else { return false } - let key = PreferenceKeys.lastSeenAppVersion.name - let current = bundle.appVersion - let previous = defaults.string(forKey: key) + // Sparkle orders releases by CFBundleVersion, which RELEASING.md numbers so a beta sorts below its stable. + let current = bundle.buildNumber + let key = PreferenceKeys.lastSeenAppBuild.name + guard let seen = defaults.object(forKey: key) as? Int else { + defaults.set(current, forKey: key) + // Every build that recorded only the version is older than this one. + let legacy = defaults.string(forKey: PreferenceKeys.lastSeenAppVersion.name) + return legacy.map { $0 != bundle.appVersion } ?? false + } + // An older build writes nothing, so going back to the newer one does not offer its notes again. + guard seen < current else { return false } defaults.set(current, forKey: key) - return previous.map { $0 != current } ?? false + return true } } diff --git a/apple/TableProMobile/TableProMobile/Onboarding/FirstRunPlan.swift b/apple/TableProMobile/TableProMobile/Onboarding/FirstRunPlan.swift index a04718ab8a..064eb5b518 100644 --- a/apple/TableProMobile/TableProMobile/Onboarding/FirstRunPlan.swift +++ b/apple/TableProMobile/TableProMobile/Onboarding/FirstRunPlan.swift @@ -45,6 +45,11 @@ nonisolated struct FirstRunPlan: Sendable { private var isUpgrade: Bool { guard let lastSeenVersion else { return false } - return lastSeenVersion != currentVersion + return Self.isNewer(currentVersion, than: lastSeenVersion) + } + + // The App Store takes only period-separated integers as a version, which a numeric comparison orders. + static func isNewer(_ version: String, than seen: String) -> Bool { + seen.compare(version, options: .numeric) == .orderedAscending } } diff --git a/apple/TableProMobile/TableProMobile/Platform/OnboardingPreferences.swift b/apple/TableProMobile/TableProMobile/Platform/OnboardingPreferences.swift index 82626d932f..fe009431d2 100644 --- a/apple/TableProMobile/TableProMobile/Platform/OnboardingPreferences.swift +++ b/apple/TableProMobile/TableProMobile/Platform/OnboardingPreferences.swift @@ -46,8 +46,9 @@ final class OnboardingPreferences { defaults.set(true, forKey: Self.hasSeenWelcomeKey) } + // An older build writes nothing, so going back to the newer one does not show its highlights again. func recordLaunch(version: String) { - guard lastSeenVersion != version else { return } + if let lastSeenVersion, !FirstRunPlan.isNewer(version, than: lastSeenVersion) { return } lastSeenVersion = version defaults.set(version, forKey: Self.lastSeenVersionKey) } diff --git a/apple/TableProMobile/TableProMobileTests/Onboarding/FirstRunPlanTests.swift b/apple/TableProMobile/TableProMobileTests/Onboarding/FirstRunPlanTests.swift index 76d3d12afa..333104fe22 100644 --- a/apple/TableProMobile/TableProMobileTests/Onboarding/FirstRunPlanTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Onboarding/FirstRunPlanTests.swift @@ -88,4 +88,28 @@ struct FirstRunPlanTests { let presentation = plan(hasSeenWelcome: true, syncChoice: true, lastSeenVersion: "1.0") #expect(presentation == .firstRun([.usageData])) } + + @Test("An older version shows nothing") + func olderVersionShowsNothing() { + let presentation = plan( + hasSeenWelcome: true, + syncChoice: true, + usageDataChoice: false, + lastSeenVersion: "1.1", + currentVersion: "1.0" + ) + #expect(presentation == .none) + } + + @Test("Versions compare as numbers, not as text") + func versionsCompareAsNumbers() { + let presentation = plan( + hasSeenWelcome: true, + syncChoice: true, + usageDataChoice: false, + lastSeenVersion: "1.9", + currentVersion: "1.10" + ) + #expect(presentation == .whatsNew(version: "1.10")) + } } diff --git a/apple/TableProMobile/TableProMobileTests/Onboarding/OnboardingPreferencesTests.swift b/apple/TableProMobile/TableProMobileTests/Onboarding/OnboardingPreferencesTests.swift index 54a079fa29..64e1c801f9 100644 --- a/apple/TableProMobile/TableProMobileTests/Onboarding/OnboardingPreferencesTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Onboarding/OnboardingPreferencesTests.swift @@ -58,4 +58,25 @@ struct OnboardingPreferencesTests { #expect(reloaded.hasSeenWelcome) #expect(reloaded.lastSeenVersion == "1.0") } + + @Test("An older version records nothing, so the newer one stays recorded") + func olderVersionKeepsNewerRecorded() { + let preferences = OnboardingPreferences(defaults: defaults) + preferences.recordLaunch(version: "1.1") + + preferences.recordLaunch(version: "1.0") + + #expect(preferences.lastSeenVersion == "1.1") + #expect(OnboardingPreferences(defaults: defaults).lastSeenVersion == "1.1") + } + + @Test("A newer version is recorded, compared as numbers") + func newerVersionIsRecorded() { + let preferences = OnboardingPreferences(defaults: defaults) + preferences.recordLaunch(version: "1.9") + + preferences.recordLaunch(version: "1.10") + + #expect(OnboardingPreferences(defaults: defaults).lastSeenVersion == "1.10") + } } diff --git a/apple/TableProTests/Views/WhatsNewContentTests.swift b/apple/TableProTests/Views/WhatsNewContentTests.swift index 28d152fab4..59e32a5d1c 100644 --- a/apple/TableProTests/Views/WhatsNewContentTests.swift +++ b/apple/TableProTests/Views/WhatsNewContentTests.swift @@ -84,39 +84,89 @@ struct WhatsNewContentTests { } } - @Test("With notes a new version shows the Welcome link and is recorded as seen") - func notesShowLinkForNewVersion() throws { + @Test("With notes a newer build shows the Welcome link and is recorded as seen") + func notesShowLinkForNewerBuild() throws { let suite = try EphemeralDefaults() - suite.defaults.set("0.81.0", forKey: PreferenceKeys.lastSeenAppVersion.name) + suite.defaults.set(810_099, forKey: PreferenceKeys.lastSeenAppBuild.name) - try withBundle(version: "0.82.0", notes: "# TablePro 0.82.0\n") { bundle in + try withBundle(version: "0.82.0", build: 820_099, notes: "# TablePro 0.82.0\n") { bundle in let showsLink = WhatsNewNotice(bundle: bundle, defaults: suite.defaults).recordLaunch() #expect(showsLink) - #expect(suite.defaults.string(forKey: PreferenceKeys.lastSeenAppVersion.name) == "0.82.0") + #expect(suite.defaults.integer(forKey: PreferenceKeys.lastSeenAppBuild.name) == 820_099) } } - @Test("A first launch records the version without the Welcome link") + @Test("A first launch records the build without the Welcome link") func firstLaunchHasNoLink() throws { let suite = try EphemeralDefaults() - try withBundle(version: "0.82.0", notes: "# TablePro 0.82.0\n") { bundle in + try withBundle(version: "0.82.0", build: 820_099, notes: "# TablePro 0.82.0\n") { bundle in + let showsLink = WhatsNewNotice(bundle: bundle, defaults: suite.defaults).recordLaunch() + + #expect(!showsLink) + #expect(suite.defaults.integer(forKey: PreferenceKeys.lastSeenAppBuild.name) == 820_099) + } + } + + @Test("An older build shows no link and leaves the newer build recorded") + func olderBuildIsQuiet() throws { + let suite = try EphemeralDefaults() + suite.defaults.set(830_099, forKey: PreferenceKeys.lastSeenAppBuild.name) + suite.defaults.set("0.83.0", forKey: PreferenceKeys.lastSeenAppVersion.name) + + try withBundle(version: "0.82.0", build: 820_099, notes: "# TablePro 0.82.0\n") { bundle in let showsLink = WhatsNewNotice(bundle: bundle, defaults: suite.defaults).recordLaunch() #expect(!showsLink) - #expect(suite.defaults.string(forKey: PreferenceKeys.lastSeenAppVersion.name) == "0.82.0") + #expect(suite.defaults.integer(forKey: PreferenceKeys.lastSeenAppBuild.name) == 830_099) + #expect(suite.defaults.string(forKey: PreferenceKeys.lastSeenAppVersion.name) == "0.83.0") + } + } + + @Test("The stable after its beta shows the link, and a relaunch does not") + func stableAfterBetaShowsLinkOnce() throws { + let suite = try EphemeralDefaults() + suite.defaults.set(830_002, forKey: PreferenceKeys.lastSeenAppBuild.name) + + try withBundle(version: "0.83.0", build: 830_099, notes: "# TablePro 0.83.0\n") { bundle in + let notice = WhatsNewNotice(bundle: bundle, defaults: suite.defaults) + + #expect(notice.recordLaunch()) + #expect(!notice.recordLaunch()) + } + } + + @Test("The first build to record a build number shows the link after an older version") + func legacyVersionRecordCarriesOver() throws { + let suite = try EphemeralDefaults() + suite.defaults.set("0.81.0", forKey: PreferenceKeys.lastSeenAppVersion.name) + + try withBundle(version: "0.82.0", build: 820_099, notes: "# TablePro 0.82.0\n") { bundle in + let showsLink = WhatsNewNotice(bundle: bundle, defaults: suite.defaults).recordLaunch() + + #expect(showsLink) + #expect(suite.defaults.integer(forKey: PreferenceKeys.lastSeenAppBuild.name) == 820_099) } } - private func withBundle(version: String, notes: String?, _ body: (Bundle) throws -> Void) throws { + private func withBundle( + version: String, + build: Int = 1, + notes: String?, + _ body: (Bundle) throws -> Void + ) throws { let root = FileManager.default.temporaryDirectory .appendingPathComponent("WhatsNewContentTests-\(UUID().uuidString)", isDirectory: true) defer { try? FileManager.default.removeItem(at: root) } let bundleURL = root.appendingPathComponent("Probe.bundle", isDirectory: true) let resources = bundleURL.appendingPathComponent("Contents/Resources", isDirectory: true) try FileManager.default.createDirectory(at: resources, withIntermediateDirectories: true) - let info = ["CFBundleIdentifier": "com.TablePro.WhatsNewProbe", "CFBundleShortVersionString": version] + let info = [ + "CFBundleIdentifier": "com.TablePro.WhatsNewProbe", + "CFBundleShortVersionString": version, + "CFBundleVersion": String(build), + ] let plist = try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0) try plist.write(to: bundleURL.appendingPathComponent("Contents/Info.plist")) if let notes { diff --git a/apple/changelog.d/3878.fixed.1.md b/apple/changelog.d/3878.fixed.1.md new file mode 100644 index 0000000000..81c7f304b3 --- /dev/null +++ b/apple/changelog.d/3878.fixed.1.md @@ -0,0 +1 @@ +What’s New opening on iPhone and iPad after installing an older TestFlight build. diff --git a/apple/changelog.d/3878.fixed.md b/apple/changelog.d/3878.fixed.md new file mode 100644 index 0000000000..341f893b32 --- /dev/null +++ b/apple/changelog.d/3878.fixed.md @@ -0,0 +1 @@ +The Welcome window offering What’s New after opening an older version, and again after going back to the newer one. diff --git a/linux/Cargo.lock b/linux/Cargo.lock index cb92249d06..bacb9da7e7 100644 --- a/linux/Cargo.lock +++ b/linux/Cargo.lock @@ -5558,6 +5558,7 @@ dependencies = [ "relm4", "rustix 1.1.4", "secrecy", + "semver", "serde", "serde_json", "sourceview5", diff --git a/linux/changelog.d/3878.added.md b/linux/changelog.d/3878.added.md new file mode 100644 index 0000000000..43343b91ed --- /dev/null +++ b/linux/changelog.d/3878.added.md @@ -0,0 +1 @@ +The first launch after an update says so, with a What’s New button that opens the release notes in About. diff --git a/linux/crates/app/Cargo.toml b/linux/crates/app/Cargo.toml index c340f035ec..cc3f705740 100644 --- a/linux/crates/app/Cargo.toml +++ b/linux/crates/app/Cargo.toml @@ -106,6 +106,7 @@ indexmap = "2.14" num-bigint.workspace = true # The assistant's replies are Markdown; the HTML writer stays off. pulldown-cmark = { version = "0.13", default-features = false } +semver = "1" sqlformat = "0.5" [dev-dependencies] diff --git a/linux/crates/app/src/application/about_dialog.rs b/linux/crates/app/src/application/about_dialog.rs index 4627e18f2d..dd89ec15dc 100644 --- a/linux/crates/app/src/application/about_dialog.rs +++ b/linux/crates/app/src/application/about_dialog.rs @@ -33,15 +33,29 @@ fn metainfo_resource() -> String { format!("{}/metainfo.xml", config::RESOURCE_BASE_PATH) } -/// A development version has no release of its own in the metainfo, and -/// libadwaita reports a version it cannot find as a critical. -fn release_notes_version(profile: Profile) -> Option<&'static str> { +// A development version has no release in the metainfo, and libadwaita logs a critical for a missing one. +pub(super) fn release_notes_version(profile: Profile) -> Option<&'static str> { match profile { Profile::Default => Some(config::VERSION), Profile::Development => None, } } +// Read the way the About dialog reads them, so the update toast never points to a What's New page it lacks. +pub(super) fn has_release_notes(version: &str) -> bool { + !adw::AboutDialog::from_appdata(&metainfo_resource(), Some(version)) + .release_notes() + .is_empty() +} + +#[cfg(test)] +pub(super) fn embedded_metainfo() -> String { + crate::register_test_resources(); + let bytes = gio::resources_lookup_data(&metainfo_resource(), gio::ResourceLookupFlags::NONE) + .expect("the metainfo is embedded at the resource base path"); + String::from_utf8(bytes.to_vec()).expect("the metainfo is UTF-8") +} + #[cfg(test)] mod tests { use gtk4 as gtk; @@ -49,13 +63,6 @@ mod tests { use super::*; use crate::application::metainfo_release::newest_release; - fn embedded_metainfo() -> String { - crate::register_test_resources(); - let bytes = gio::resources_lookup_data(&metainfo_resource(), gio::ResourceLookupFlags::NONE) - .expect("the metainfo is embedded at the resource base path"); - String::from_utf8(bytes.to_vec()).expect("the metainfo is UTF-8") - } - /// The text between the element `open` starts and the next `close`. fn metainfo_text<'a>(xml: &'a str, open: &str, close: &str) -> &'a str { let start = xml.find(open).unwrap_or_else(|| panic!("{open} is in the metainfo")); @@ -191,6 +198,15 @@ mod tests { } } + #[gtk4::test] + fn the_newest_release_has_notes_for_whats_new() { + adw::init().expect("libadwaita initialises under the test backend"); + let xml = embedded_metainfo(); + let version = newest_release(&xml).expect("the metainfo lists a release"); + + assert!(has_release_notes(version)); + } + #[test] fn release_notes_version_absent_in_development_profile() { assert_eq!(release_notes_version(Profile::Development), None); diff --git a/linux/crates/app/src/application/crash_notice.rs b/linux/crates/app/src/application/crash_notice.rs deleted file mode 100644 index 29ff4b724c..0000000000 --- a/linux/crates/app/src/application/crash_notice.rs +++ /dev/null @@ -1,62 +0,0 @@ -use std::path::PathBuf; - -use gtk4::glib; -use gtk4::prelude::*; -use gtk4::subclass::prelude::*; - -use super::TableProApplication; -use crate::ui::SessionWindow; - -impl TableProApplication { - /// Looks off the GTK thread for a report the last run left when it - /// crashed, and has a window say so. - pub(super) fn look_for_crash_report(&self) { - let Some(services) = self.services() else { - return; - }; - let dir = services.storage.paths().crashes_dir(); - let looking = services - .tasks - .spawn_blocking_task(move || crate::crash_report::take_unannounced(&dir)); - let app = self.downgrade(); - glib::spawn_future_local(async move { - let report = match looking.await { - Ok(Ok(Some(report))) => report, - Ok(Ok(None)) => return, - Ok(Err(error)) => { - tracing::warn!(%error, "could not look for a crash report"); - return; - } - Err(failure) => { - tracing::warn!(%failure, "the crash report lookup failed"); - return; - } - }; - if let Some(app) = app.upgrade() { - app.announce_crash(report); - } - }); - } - - /// The window in front says it, or the first one to open when the - /// windows of a restored session are still on their way. - fn announce_crash(&self, report: PathBuf) { - let window = self.active_window().and_downcast::().or_else(|| { - self.windows() - .into_iter() - .find_map(|window| window.downcast::().ok()) - }); - match window { - Some(window) => window.announce_crash(report), - None => { - self.imp().crash_report.replace(Some(report)); - } - } - } - - pub(super) fn hand_over_crash_report(&self, window: &SessionWindow) { - if let Some(report) = self.imp().crash_report.take() { - window.announce_crash(report); - } - } -} diff --git a/linux/crates/app/src/application/imp.rs b/linux/crates/app/src/application/imp.rs index 16e33e1515..8ced134848 100644 --- a/linux/crates/app/src/application/imp.rs +++ b/linux/crates/app/src/application/imp.rs @@ -33,8 +33,8 @@ pub struct TableProApplication { /// Files handed over while the record is read, opened after the /// windows it restores. pub(super) pending_files: RefCell>, - /// A crash report found before any window opened. - pub(super) crash_report: RefCell>, + /// What to say at launch, found before any window opened. + pub(super) launch_notices: RefCell>, /// Answers the MCP server's questions about windows and sessions. pub(super) mcp_requests: RefCell>>, } @@ -91,6 +91,7 @@ impl ApplicationImpl for TableProApplication { services.start(); app.start_mcp(); app.look_for_crash_report(); + app.note_launch_version(); self.session_signals .replace(Some(SessionSignals::start(&services.sessions))); } diff --git a/linux/crates/app/src/application/launch_notice.rs b/linux/crates/app/src/application/launch_notice.rs new file mode 100644 index 0000000000..39323388c2 --- /dev/null +++ b/linux/crates/app/src/application/launch_notice.rs @@ -0,0 +1,135 @@ +use std::cmp::Ordering; + +use gtk4::glib; +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use semver::Version; + +use super::TableProApplication; +use super::about_dialog::{has_release_notes, release_notes_version}; +use crate::config; +use crate::ui::{LaunchNotice, SessionWindow}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Launch { + First, + Again, + Update, + Downgrade, +} + +// No release writes a version semver cannot read, so one counts as none. +fn launch_after(previous: &str, current: &Version) -> Launch { + match Version::parse(previous).map(|previous| previous.cmp(current)) { + Err(_) => Launch::First, + Ok(Ordering::Less) => Launch::Update, + Ok(Ordering::Equal) => Launch::Again, + Ok(Ordering::Greater) => Launch::Downgrade, + } +} + +impl TableProApplication { + pub(super) fn look_for_crash_report(&self) { + let Some(services) = self.services() else { + return; + }; + let dir = services.storage.paths().crashes_dir(); + let looking = services + .tasks + .spawn_blocking_task(move || crate::crash_report::take_unannounced(&dir)); + let app = self.downgrade(); + glib::spawn_future_local(async move { + let report = match looking.await { + Ok(Ok(Some(report))) => report, + Ok(Ok(None)) => return, + Ok(Err(error)) => { + tracing::warn!(%error, "could not look for a crash report"); + return; + } + Err(failure) => { + tracing::warn!(%failure, "the crash report lookup failed"); + return; + } + }; + if let Some(app) = app.upgrade() { + app.announce(LaunchNotice::Crash(report)); + } + }); + } + + // A development build has no release of its own, so it never marks one as seen. + pub(super) fn note_launch_version(&self) { + if let Some(version) = release_notes_version(config::profile()) { + self.note_launch_of(version); + } + } + + pub(super) fn note_launch_of(&self, version: &str) { + let Some(services) = self.services() else { + return; + }; + let current = match Version::parse(version) { + Ok(current) => current, + Err(error) => { + tracing::warn!(%error, version, "the running version is not a release version"); + return; + } + }; + let launch = launch_after(&services.settings.last_seen_version(), ¤t); + // An older build never writes over what a newer one stored (docs/releasing.md, Downgrades). + if matches!(launch, Launch::Again | Launch::Downgrade) { + return; + } + // Unrecorded, the toast would come back at every launch. + if let Err(error) = services.settings.set_last_seen_version(version) { + tracing::warn!(%error, "could not record the running version"); + return; + } + if launch == Launch::Update && has_release_notes(version) { + self.announce(LaunchNotice::Update(version.to_owned())); + } + } + + // The windows of a restored session may still be on their way, so the first one to open says it. + fn announce(&self, notice: LaunchNotice) { + match self.front_session_window() { + Some(window) => window.announce(notice), + None => self.imp().launch_notices.borrow_mut().push(notice), + } + } + + pub(super) fn hand_over_launch_notices(&self, window: &SessionWindow) { + for notice in self.imp().launch_notices.take() { + window.announce(notice); + } + } + + fn front_session_window(&self) -> Option { + self.active_window().and_downcast::().or_else(|| { + self.windows() + .into_iter() + .find_map(|window| window.downcast::().ok()) + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn launch(previous: &str, current: &str) -> Launch { + launch_after(previous, &Version::parse(current).expect("a release version")) + } + + #[test] + fn only_a_launch_after_an_older_release_is_an_update() { + assert_eq!(launch("0.1.0", "0.2.0"), Launch::Update); + assert_eq!(launch("0.9.0", "0.10.0"), Launch::Update, "not compared as text"); + assert_eq!(launch("0.2.0-rc.1", "0.2.0"), Launch::Update); + assert_eq!(launch("0.2.0", "0.2.0"), Launch::Again); + assert_eq!(launch("0.3.0", "0.2.0"), Launch::Downgrade); + assert_eq!(launch("0.2.0", "0.2.0-rc.1"), Launch::Downgrade); + assert_eq!(launch("", "0.2.0"), Launch::First); + assert_eq!(launch("not a version", "0.2.0"), Launch::First); + } +} diff --git a/linux/crates/app/src/application/mod.rs b/linux/crates/app/src/application/mod.rs index c733364ac0..a03767408e 100644 --- a/linux/crates/app/src/application/mod.rs +++ b/linux/crates/app/src/application/mod.rs @@ -8,9 +8,9 @@ mod accelerators; mod app_actions; mod app_services; mod app_window_host; -mod crash_notice; mod debug_info; mod imp; +mod launch_notice; mod legal_section; mod mcp_bridge; #[cfg(test)] @@ -106,7 +106,7 @@ impl TableProApplication { let host: Rc = Rc::new(AppWindowHost::new(self)); self.imp().windows.insert(window.id(), window.clone()); window.start(SessionWindowInit { services, host, launch }); - self.hand_over_crash_report(&window); + self.hand_over_launch_notices(&window); window.present(); Some(window.upcast()) } diff --git a/linux/crates/app/src/application/tests.rs b/linux/crates/app/src/application/tests.rs index 77777854cc..8a4067ff99 100644 --- a/linux/crates/app/src/application/tests.rs +++ b/linux/crates/app/src/application/tests.rs @@ -804,6 +804,63 @@ fn new_window_action_opens_another_window() { assert_eq!(test.app.imp().windows.len(), 2); } +fn released_version() -> String { + let xml = super::about_dialog::embedded_metainfo(); + super::metainfo_release::newest_release(&xml) + .expect("the metainfo lists a release") + .to_owned() +} + +fn said_at_launch(test: &TestApp) -> Vec { + test.app.imp().launch_notices.borrow().clone() +} + +#[gtk4::test] +fn the_first_launch_after_an_older_release_says_so_once() { + let test = TestApp::started("LaunchUpdate"); + let settings = test.services().settings.clone(); + let released = released_version(); + settings.set_last_seen_version("0.0.1").expect("store a version"); + + test.app.note_launch_of(&released); + + assert_eq!(settings.last_seen_version(), released); + assert_eq!( + said_at_launch(&test), + [crate::ui::LaunchNotice::Update(released.clone())] + ); + test.window(); + assert!(said_at_launch(&test).is_empty(), "the first window did not take it"); + + test.app.note_launch_of(&released); + + assert!(said_at_launch(&test).is_empty(), "the same release was said twice"); +} + +#[gtk4::test] +fn a_launch_after_a_newer_release_says_nothing_and_keeps_its_version() { + let test = TestApp::started("LaunchDowngrade"); + let settings = test.services().settings.clone(); + settings.set_last_seen_version("99.0.0").expect("store a version"); + + test.app.note_launch_of(&released_version()); + + assert_eq!(settings.last_seen_version(), "99.0.0"); + assert!(said_at_launch(&test).is_empty()); +} + +#[gtk4::test] +fn a_first_launch_records_the_version_and_says_nothing() { + let test = TestApp::started("LaunchFirst"); + let settings = test.services().settings.clone(); + let released = released_version(); + + test.app.note_launch_of(&released); + + assert_eq!(settings.last_seen_version(), released); + assert!(said_at_launch(&test).is_empty()); +} + #[gtk4::test] fn a_window_before_startup_is_refused() { let test = TestApp::new("Early"); diff --git a/linux/crates/app/src/ui/mod.rs b/linux/crates/app/src/ui/mod.rs index a2238b3571..081744b61c 100644 --- a/linux/crates/app/src/ui/mod.rs +++ b/linux/crates/app/src/ui/mod.rs @@ -55,5 +55,5 @@ pub(crate) mod write_confirmation; pub(crate) use menu_label::menu_label; pub(crate) use plain_toast::plain_toast; pub(crate) use preferences_dialog::PreferencesDialog; -pub(crate) use session_window::{SessionWindow, SessionWindowInit, SessionWindowInput}; +pub(crate) use session_window::{LaunchNotice, SessionWindow, SessionWindowInit, SessionWindowInput}; pub(crate) use structure_tab::review_dialog; diff --git a/linux/crates/app/src/ui/session_window/crash_notice.rs b/linux/crates/app/src/ui/session_window/crash_notice.rs deleted file mode 100644 index b35ec65431..0000000000 --- a/linux/crates/app/src/ui/session_window/crash_notice.rs +++ /dev/null @@ -1,159 +0,0 @@ -use std::path::{Path, PathBuf}; - -use gtk4::prelude::*; -use gtk4::subclass::prelude::*; -use gtk4::{gio, glib}; -use libadwaita as adw; - -use super::SessionWindow; -use crate::i18n::gettext; -use crate::ui::file_reveal::reveal_failure_toast; - -/// The report the last run left when it crashed, said until the user or -/// the toast's timeout dismisses it. -pub(crate) struct CrashNotice { - report: PathBuf, - shown: Option<(adw::Toast, glib::SignalHandlerId)>, -} - -impl SessionWindow { - pub(crate) fn announce_crash(&self, report: PathBuf) { - self.withdraw_crash_toast(); - self.imp() - .crash_notice - .replace(Some(CrashNotice { report, shown: None })); - self.show_crash_notice(); - } - - /// A toast lives in the overlay of the page it was added to, so a - /// restored session, which goes from the picker through connecting - /// to its workspace, would take an unread one away. Each page shown - /// gets it again until it is dismissed. - pub(super) fn show_crash_notice(&self) { - self.withdraw_crash_toast(); - let Some(report) = self - .imp() - .crash_notice - .borrow() - .as_ref() - .map(|notice| notice.report.clone()) - else { - return; - }; - let toast = adw::Toast::builder() - .title(gettext("TablePro quit unexpectedly last time")) - .button_label(gettext("Show Report")) - .use_markup(false) - .build(); - let window = self.downgrade(); - toast.connect_button_clicked(move |_| { - if let Some(window) = window.upgrade() { - window.reveal_crash_report(&report); - } - }); - let window = self.downgrade(); - let dismissed = toast.connect_dismissed(move |_| { - if let Some(window) = window.upgrade() { - window.imp().crash_notice.take(); - } - }); - if let Some(notice) = self.imp().crash_notice.borrow_mut().as_mut() { - notice.shown = Some((toast.clone(), dismissed)); - } - self.add_toast(toast); - } - - /// Takes the toast off the page it is on without counting that as - /// the user dismissing it. An overlay that goes away dismisses its - /// toasts, so this runs before the old page can. - fn withdraw_crash_toast(&self) { - let shown = self - .imp() - .crash_notice - .borrow_mut() - .as_mut() - .and_then(|notice| notice.shown.take()); - if let Some((toast, dismissed)) = shown { - toast.disconnect(dismissed); - toast.dismiss(); - } - } - - fn reveal_crash_report(&self, report: &Path) { - let Some(state) = self.imp().state.get() else { - return; - }; - let revealing = state - .services - .file_revealer - .reveal(gio::File::for_path(report), Some(self.clone().upcast())); - let window = self.downgrade(); - glib::spawn_future_local(async move { - let revealed = revealing.await; - if let (Some(toast), Some(window)) = (reveal_failure_toast(&revealed), window.upgrade()) { - window.add_toast(toast); - } - }); - } -} - -#[cfg(test)] -mod tests { - use std::rc::Rc; - - use super::super::SessionWindowInput; - use super::super::tests::{Script, TestWindow, WAIT}; - use super::*; - use crate::test_support::{descendants, drain_main_context, wait_until}; - use crate::ui::file_reveal::RecordingRevealer; - - const TITLE: &str = "TablePro quit unexpectedly last time"; - - fn said(window: &SessionWindow) -> usize { - window - .imp() - .toasts - .borrow() - .iter() - .filter(|toast| toast.title().as_deref() == Some(TITLE)) - .count() - } - - fn show_report(window: &SessionWindow) -> Option { - descendants(window) - .into_iter() - .filter_map(|widget| widget.downcast::().ok()) - .find(|button| button.label().as_deref() == Some("Show Report") && button.is_mapped()) - } - - #[gtk4::test] - fn a_crash_report_is_said_on_each_page_until_the_user_opens_it() { - let revealer = Rc::new(RecordingRevealer::succeeding()); - let test = TestWindow::with_script( - "CrashNotice", - Script { - revealer: Some(revealer.clone()), - ..Script::default() - }, - ); - let report = test.dir().join("crash-20261017T000000Z-1.txt"); - - test.window.announce_crash(report.clone()); - assert_eq!(said(&test.window), 1); - - test.window - .send(SessionWindowInput::OpenSaved(Box::new(test.saved.clone()))); - test.wait_for_page("workspace"); - assert!(said(&test.window) > 1, "the toast stayed on the picker"); - wait_until(WAIT, || show_report(&test.window).is_some()).expect("the toast on the workspace"); - - show_report(&test.window).expect("the button").emit_clicked(); - drain_main_context(); - - assert_eq!(revealer.revealed(), [report]); - assert!(test.window.imp().crash_notice.borrow().is_none()); - let before = said(&test.window); - test.window.show_crash_notice(); - assert_eq!(said(&test.window), before, "a dismissed notice came back"); - } -} diff --git a/linux/crates/app/src/ui/session_window/imp.rs b/linux/crates/app/src/ui/session_window/imp.rs index ff2ce2d8b2..42d4bd21d8 100644 --- a/linux/crates/app/src/ui/session_window/imp.rs +++ b/linux/crates/app/src/ui/session_window/imp.rs @@ -12,7 +12,7 @@ use tablepro_session::{ConnectSource, SessionKey}; use uuid::Uuid; use super::connections::LevelSave; -use super::crash_notice::CrashNotice; +use super::launch_notice::Said; use super::locate_file::LocateChooser; use super::picker_page::PickerPage; use super::session_page::{FailedConnect, OpenSession, PendingConnect, SessionPage}; @@ -88,8 +88,7 @@ pub struct SessionWindow { /// looked for. pub(super) dialog_opening: Cell, pub(super) shut_down: Cell, - /// The crash the last run ended in, until its toast is dismissed. - pub(super) crash_notice: RefCell>, + pub(super) launch_notices: RefCell>, /// Every toast shown, which the overlay does not list. #[cfg(test)] pub(super) toasts: RefCell>, diff --git a/linux/crates/app/src/ui/session_window/launch_notice.rs b/linux/crates/app/src/ui/session_window/launch_notice.rs new file mode 100644 index 0000000000..1ea3cfcbf9 --- /dev/null +++ b/linux/crates/app/src/ui/session_window/launch_notice.rs @@ -0,0 +1,265 @@ +use std::path::{Path, PathBuf}; + +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use gtk4::{gio, glib}; +use libadwaita as adw; + +use super::SessionWindow; +use crate::i18n::{gettext, gettext_f}; +use crate::ui::file_reveal::reveal_failure_toast; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum LaunchNotice { + Crash(PathBuf), + Update(String), +} + +impl LaunchNotice { + fn same_kind(&self, other: &Self) -> bool { + std::mem::discriminant(self) == std::mem::discriminant(other) + } +} + +type ShownToast = (adw::Toast, glib::SignalHandlerId); + +pub(super) struct Said { + notice: LaunchNotice, + toast: Option, +} + +impl SessionWindow { + // A later notice replaces the earlier one of its kind and leaves the other kind's toast alone. + pub(crate) fn announce(&self, notice: LaunchNotice) { + let replaced = { + let mut notices = self.imp().launch_notices.borrow_mut(); + let at = notices.iter().position(|said| said.notice.same_kind(¬ice)); + at.and_then(|at| notices.remove(at).toast) + }; + if let Some(toast) = replaced { + withdraw(toast); + } + let toast = self.say(¬ice); + self.imp().launch_notices.borrow_mut().push(Said { + notice, + toast: Some(toast), + }); + } + + // A toast lives in the overlay of the page it was added to, and a restored session goes from + // the picker through connecting to its workspace, so each page shown says what is unread. + pub(super) fn show_launch_notices(&self) { + let (shown, notices): (Vec<_>, Vec<_>) = self + .imp() + .launch_notices + .borrow_mut() + .iter_mut() + .map(|said| (said.toast.take(), said.notice.clone())) + .unzip(); + shown.into_iter().flatten().for_each(withdraw); + for notice in notices { + let toast = self.say(¬ice); + if let Some(said) = self + .imp() + .launch_notices + .borrow_mut() + .iter_mut() + .find(|said| said.notice == notice) + { + said.toast = Some(toast); + } + } + } + + fn say(&self, notice: &LaunchNotice) -> ShownToast { + let toast = match notice { + LaunchNotice::Crash(report) => { + let toast = adw::Toast::builder() + .title(gettext("TablePro quit unexpectedly last time")) + .button_label(gettext("Show Report")) + .use_markup(false) + .build(); + let window = self.downgrade(); + let report = report.clone(); + toast.connect_button_clicked(move |_| { + if let Some(window) = window.upgrade() { + window.reveal_crash_report(&report); + } + }); + toast + } + LaunchNotice::Update(version) => adw::Toast::builder() + // Translators: {version} is the TablePro version this launch updated to, such as 0.2.0. + .title(gettext_f("TablePro was updated to {version}", &[("version", version)])) + .button_label(gettext("What’s New")) + .action_name("app.about") + .use_markup(false) + .build(), + }; + let window = self.downgrade(); + let notice = notice.clone(); + let dismissed = toast.connect_dismissed(move |_| { + if let Some(window) = window.upgrade() { + window + .imp() + .launch_notices + .borrow_mut() + .retain(|said| said.notice != notice); + } + }); + self.add_toast(toast.clone()); + (toast, dismissed) + } + + fn reveal_crash_report(&self, report: &Path) { + let Some(state) = self.imp().state.get() else { + return; + }; + let revealing = state + .services + .file_revealer + .reveal(gio::File::for_path(report), Some(self.clone().upcast())); + let window = self.downgrade(); + glib::spawn_future_local(async move { + let revealed = revealing.await; + if let (Some(toast), Some(window)) = (reveal_failure_toast(&revealed), window.upgrade()) { + window.add_toast(toast); + } + }); + } +} + +// An overlay that goes away dismisses its toasts, which must not count as the user dismissing one. +fn withdraw((toast, dismissed): ShownToast) { + toast.disconnect(dismissed); + toast.dismiss(); +} + +#[cfg(test)] +mod tests { + use std::rc::Rc; + + use libadwaita::prelude::*; + + use super::super::SessionWindowInput; + use super::super::tests::{Script, TestWindow, WAIT}; + use super::*; + use crate::test_support::{descendants, drain_main_context, wait_until}; + use crate::ui::file_reveal::RecordingRevealer; + + const CRASHED: &str = "TablePro quit unexpectedly last time"; + const UPDATED: &str = "TablePro was updated to 2.0"; + + fn said(window: &SessionWindow, title: &str) -> usize { + window + .imp() + .toasts + .borrow() + .iter() + .filter(|toast| toast.title().as_deref() == Some(title)) + .count() + } + + fn mapped_button(window: &SessionWindow, label: &str) -> Option { + descendants(window) + .into_iter() + .filter_map(|widget| widget.downcast::().ok()) + .find(|button| button.label().as_deref() == Some(label) && button.is_mapped()) + } + + #[gtk4::test] + fn a_crash_report_is_said_on_each_page_until_the_user_opens_it() { + let revealer = Rc::new(RecordingRevealer::succeeding()); + let test = TestWindow::with_script( + "CrashNotice", + Script { + revealer: Some(revealer.clone()), + ..Script::default() + }, + ); + let report = test.dir().join("crash-20261017T000000Z-1.txt"); + + test.window.announce(LaunchNotice::Crash(report.clone())); + assert_eq!(said(&test.window, CRASHED), 1); + + test.window + .send(SessionWindowInput::OpenSaved(Box::new(test.saved.clone()))); + test.wait_for_page("workspace"); + assert!(said(&test.window, CRASHED) > 1, "the toast stayed on the picker"); + wait_until(WAIT, || mapped_button(&test.window, "Show Report").is_some()).expect("the toast on the workspace"); + + mapped_button(&test.window, "Show Report") + .expect("the button") + .emit_clicked(); + drain_main_context(); + + assert_eq!(revealer.revealed(), [report]); + assert!(test.window.imp().launch_notices.borrow().is_empty()); + let before = said(&test.window, CRASHED); + test.window.show_launch_notices(); + assert_eq!(said(&test.window, CRASHED), before, "a dismissed notice came back"); + } + + #[gtk4::test] + fn an_update_and_a_crash_are_each_said_on_each_page() { + let test = TestWindow::picker("UpdateBesideCrash"); + let report = test.dir().join("crash-20261017T000000Z-1.txt"); + + test.window.announce(LaunchNotice::Update("2.0".to_owned())); + test.window.announce(LaunchNotice::Crash(report)); + assert_eq!(said(&test.window, UPDATED), 1); + assert_eq!(said(&test.window, CRASHED), 1); + + test.window + .send(SessionWindowInput::OpenSaved(Box::new(test.saved.clone()))); + test.wait_for_page("workspace"); + + assert!(said(&test.window, UPDATED) > 1, "the update toast stayed on the picker"); + assert!(said(&test.window, CRASHED) > 1, "the crash toast stayed on the picker"); + assert_eq!(test.window.imp().launch_notices.borrow().len(), 2); + } + + #[gtk4::test] + fn a_second_notice_of_a_kind_replaces_only_its_own_toast() { + let test = TestWindow::picker("UpdateReplaced"); + let report = test.dir().join("crash-20261017T000000Z-1.txt"); + test.window.announce(LaunchNotice::Crash(report.clone())); + test.window.announce(LaunchNotice::Update("1.0".to_owned())); + + test.window.announce(LaunchNotice::Update("2.0".to_owned())); + + let notices: Vec = test + .window + .imp() + .launch_notices + .borrow() + .iter() + .map(|said| said.notice.clone()) + .collect(); + assert_eq!( + notices, + [LaunchNotice::Crash(report), LaunchNotice::Update("2.0".to_owned())] + ); + assert_eq!(said(&test.window, CRASHED), 1, "the crash toast was said again"); + } + + #[gtk4::test] + fn whats_new_opens_the_about_dialog_and_the_notice_stays_read() { + let test = TestWindow::picker("UpdateWhatsNew"); + test.window.announce(LaunchNotice::Update("2.0".to_owned())); + wait_until(WAIT, || mapped_button(&test.window, "What’s New").is_some()).expect("the toast's button"); + + mapped_button(&test.window, "What’s New") + .expect("the button") + .emit_clicked(); + drain_main_context(); + + let dialog = test.window.visible_dialog().expect("a dialog over the window"); + assert!(dialog.is::(), "{}", dialog.type_().name()); + assert!(test.window.imp().launch_notices.borrow().is_empty()); + dialog.force_close(); + let before = said(&test.window, UPDATED); + test.window.show_launch_notices(); + assert_eq!(said(&test.window, UPDATED), before, "a read notice came back"); + } +} diff --git a/linux/crates/app/src/ui/session_window/mod.rs b/linux/crates/app/src/ui/session_window/mod.rs index 6ee2823d74..1f7865151c 100644 --- a/linux/crates/app/src/ui/session_window/mod.rs +++ b/linux/crates/app/src/ui/session_window/mod.rs @@ -14,9 +14,9 @@ mod connect_failure_view; mod connect_flow; mod connecting_page; mod connections; -mod crash_notice; mod file_location_dialog; mod imp; +mod launch_notice; mod locate_file; mod notification_request; mod notification_sink; @@ -61,6 +61,7 @@ pub(super) use self::connect_failed_page::ConnectFailedPage; use self::connect_failure_view::ConnectFailureView; pub(super) use self::connecting_page::ConnectingPage; use self::file_location_dialog::FileCheck; +pub(crate) use self::launch_notice::LaunchNotice; pub(super) use self::picker_page::PickerPage; use self::resolve_file_identity::{FileResolveError, FileTarget, ResolvedFile, resolve_file_blocking}; use self::session_page::{FailedConnect, OpenSession, PendingConnect, SessionPage, source_key}; @@ -616,7 +617,7 @@ impl SessionWindow { imp.opened_for_file.set(false); } imp.page_toasts.replace(toasts); - self.show_crash_notice(); + self.show_launch_notices(); // A Cancel or Retry left as the default would take Enter on a // page that no longer shows it. self.set_default_widget(None::<>k::Widget>); diff --git a/linux/crates/storage/src/settings/keys.rs b/linux/crates/storage/src/settings/keys.rs index 6cdfc3cbfe..9dea2b0ba7 100644 --- a/linux/crates/storage/src/settings/keys.rs +++ b/linux/crates/storage/src/settings/keys.rs @@ -9,6 +9,7 @@ pub const STYLE_SCHEME: &str = "style-scheme"; pub const VIM_KEYBINDINGS: &str = "vim-keybindings"; pub const CONNECT_DIALOG_DRIVER: &str = "connect-dialog-driver"; pub const RESTORE_SESSION: &str = "restore-session"; +pub const LAST_SEEN_VERSION: &str = "last-seen-version"; pub const MCP_SERVER_ENABLED: &str = "mcp-server-enabled"; pub const MCP_SERVER_PORT: &str = "mcp-server-port"; pub const AI_PROVIDER: &str = "ai-provider"; diff --git a/linux/crates/storage/src/settings/mod.rs b/linux/crates/storage/src/settings/mod.rs index b4479eb70d..ab020b8b7f 100644 --- a/linux/crates/storage/src/settings/mod.rs +++ b/linux/crates/storage/src/settings/mod.rs @@ -165,6 +165,14 @@ impl AppSettings { self.set_boolean(keys::RESTORE_SESSION, restore) } + pub fn last_seen_version(&self) -> String { + self.settings.string(keys::LAST_SEEN_VERSION).into() + } + + pub fn set_last_seen_version(&self, version: &str) -> Result<(), SettingsError> { + self.set_string(keys::LAST_SEEN_VERSION, version) + } + /// Whether the MCP server runs. Off until the user turns it on. pub fn mcp_server_enabled(&self) -> bool { self.settings.boolean(keys::MCP_SERVER_ENABLED) diff --git a/linux/crates/storage/tests/settings.rs b/linux/crates/storage/tests/settings.rs index 26d4a0eccf..1051e02c71 100644 --- a/linux/crates/storage/tests/settings.rs +++ b/linux/crates/storage/tests/settings.rs @@ -130,6 +130,17 @@ fn restore_session_round_trip() -> TestResult { Ok(()) } +#[test] +fn last_seen_version_starts_empty_and_round_trips() -> TestResult { + let fixture = Fixture::new()?; + assert_eq!(fixture.settings.last_seen_version(), ""); + + fixture.settings.set_last_seen_version("0.2.0")?; + + assert_eq!(fixture.settings.last_seen_version(), "0.2.0"); + Ok(()) +} + #[test] fn confirm_bulk_delete_round_trip() -> TestResult { let fixture = Fixture::new()?; diff --git a/linux/data/app.tablepro.TablePro.gschema.xml.in b/linux/data/app.tablepro.TablePro.gschema.xml.in index 4a1f3ae70f..e1d082c737 100644 --- a/linux/data/app.tablepro.TablePro.gschema.xml.in +++ b/linux/data/app.tablepro.TablePro.gschema.xml.in @@ -115,6 +115,12 @@ Whether the connections open when TablePro quit are opened again at the next launch. + + '' + Newest version launched + The newest TablePro release that has run, so the first launch of a later one can point to its release notes. Empty before the first launch. + + false Run the MCP server diff --git a/linux/docs/storage.md b/linux/docs/storage.md index 51dac77c4e..e90415956b 100644 --- a/linux/docs/storage.md +++ b/linux/docs/storage.md @@ -271,7 +271,7 @@ Turning the setting off keeps what is already there; Clear removes it. ## App preferences with `gio::Settings` -`data/app.tablepro.TablePro.gschema.xml.in` holds every app-wide preference and the window geometry. Meson installs it to `$datadir/glib-2.0/schemas` and compiles it; see [ADR 0007](decisions/0007-gsettings-preferences.md). +`data/app.tablepro.TablePro.gschema.xml.in` holds every app-wide preference, the window geometry and the newest release that has run. Meson installs it to `$datadir/glib-2.0/schemas` and compiles it; see [ADR 0007](decisions/0007-gsettings-preferences.md). The schema id is the application id and the path follows it: `app.tablepro.TablePro` at `/app/tablepro/TablePro/` for an installed build, `app.tablepro.TablePro.Devel` at `/app/tablepro/TablePro/Devel/` for a development one. Neither reads the other's settings. @@ -283,6 +283,7 @@ The schema id is the application id and the path follows it: `app.tablepro.Table | `default-page-size` | `u` | `1000` | 100 to 100000 | | `confirm-bulk-delete` | `b` | `true` | | | `restore-session` | `b` | `true` | | +| `last-seen-version` | `s` | empty | | | `mcp-server-enabled` | `b` | `false` | | | `mcp-server-port` | `u` | `23508` | 1024 to 65535 | | `ai-provider` | enum | `none` | `none`, `anthropic`, `openai`, `openai-compatible` | diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index f56ea2ca6a..4c1e116d14 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -5,9 +5,9 @@ crates/app/src/application/accelerators.rs crates/app/src/application/app_actions.rs crates/app/src/application/app_services.rs crates/app/src/application/app_window_host.rs -crates/app/src/application/crash_notice.rs crates/app/src/application/debug_info.rs crates/app/src/application/imp.rs +crates/app/src/application/launch_notice.rs crates/app/src/application/legal_section.rs crates/app/src/application/mcp_bridge.rs crates/app/src/application/metainfo_release.rs @@ -461,9 +461,9 @@ crates/app/src/ui/session_window/connect_failure_view.rs crates/app/src/ui/session_window/connect_flow.rs crates/app/src/ui/session_window/connecting_page.rs crates/app/src/ui/session_window/connections.rs -crates/app/src/ui/session_window/crash_notice.rs crates/app/src/ui/session_window/file_location_dialog.rs crates/app/src/ui/session_window/imp.rs +crates/app/src/ui/session_window/launch_notice.rs crates/app/src/ui/session_window/locate_file.rs crates/app/src/ui/session_window/mod.rs crates/app/src/ui/session_window/notification_request.rs From b97715ed571f220920acf6c839fd2c7511fd81cb Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 05:25:48 +0100 Subject: [PATCH 15/85] feat(linux-grid): pick an enum or set cell's labels from a menu (#3880) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3880.added.md | 1 + linux/changelog.d/3880.fixed.1.md | 1 + linux/changelog.d/3880.fixed.md | 1 + .../app/src/services/grid/cell_editor_kind.rs | 57 +++- .../app/src/ui/data_grid/edit/actions.rs | 7 +- .../app/src/ui/data_grid/edit/cell_editor.rs | 83 ++++-- .../app/src/ui/data_grid/edit/choice_menu.rs | 264 +++++++++++++++++ linux/crates/app/src/ui/data_grid/edit/mod.rs | 6 +- .../app/src/ui/data_grid/edit/row_edits.rs | 5 +- .../src/ui/data_grid/edit/tests/choices.rs | 279 ++++++++++++++++++ .../app/src/ui/data_grid/edit/tests/mod.rs | 1 + .../crates/app/src/ui/data_grid/grid_menu.rs | 29 +- linux/crates/app/src/ui/data_grid/imp.rs | 3 + .../app/src/ui/data_grid/row_detail/imp.rs | 2 + .../app/src/ui/data_grid/row_detail/mod.rs | 1 + .../app/src/ui/data_grid/row_detail/tests.rs | 44 +++ .../ui/data_grid/row_detail/value_editor.rs | 44 ++- .../crates/app/src/ui/data_grid/selection.rs | 30 +- .../app/src/ui/data_grid/tests/menus.rs | 18 ++ linux/crates/core/src/column/kind.rs | 17 +- linux/crates/core/src/edit/parse_edit_text.rs | 28 ++ linux/data/resources/ui/row-detail-pane.ui | 13 + linux/po/POTFILES.in | 2 + 23 files changed, 893 insertions(+), 43 deletions(-) create mode 100644 linux/changelog.d/3880.added.md create mode 100644 linux/changelog.d/3880.fixed.1.md create mode 100644 linux/changelog.d/3880.fixed.md create mode 100644 linux/crates/app/src/ui/data_grid/edit/choice_menu.rs create mode 100644 linux/crates/app/src/ui/data_grid/edit/tests/choices.rs diff --git a/linux/changelog.d/3880.added.md b/linux/changelog.d/3880.added.md new file mode 100644 index 0000000000..7f4b0f104e --- /dev/null +++ b/linux/changelog.d/3880.added.md @@ -0,0 +1 @@ +Enum and set cells pick from a menu of the column's labels, in the grid and in Row Details. diff --git a/linux/changelog.d/3880.fixed.1.md b/linux/changelog.d/3880.fixed.1.md new file mode 100644 index 0000000000..bb69738598 --- /dev/null +++ b/linux/changelog.d/3880.fixed.1.md @@ -0,0 +1 @@ +Closing a cell's menu moved the keyboard focus off the grid. diff --git a/linux/changelog.d/3880.fixed.md b/linux/changelog.d/3880.fixed.md new file mode 100644 index 0000000000..836050baaa --- /dev/null +++ b/linux/changelog.d/3880.fixed.md @@ -0,0 +1 @@ +Clearing a MySQL SET cell wrote NULL instead of the empty set, and Set to Empty was not offered. diff --git a/linux/crates/app/src/services/grid/cell_editor_kind.rs b/linux/crates/app/src/services/grid/cell_editor_kind.rs index 7788d2ef73..9f2796dda8 100644 --- a/linux/crates/app/src/services/grid/cell_editor_kind.rs +++ b/linux/crates/app/src/services/grid/cell_editor_kind.rs @@ -11,6 +11,10 @@ pub const INLINE_EDIT_MAX_BYTES: usize = 4096; /// details editor lays out every line on the GTK thread and would stall. pub const EDIT_MAX_BYTES: usize = 1_000_000; +/// Past this many labels an enum edits as text: a menu lays out a row +/// per label on the GTK thread, measured at half a second for 400. +pub const CHOICE_MAX_LABELS: usize = 100; + /// The editor a cell opens with. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum CellEditorKind { @@ -19,6 +23,8 @@ pub enum CellEditorKind { purpose: TextPurpose, }, Toggle, + /// A menu of the column's labels. + Choice, Details(DetailsEditor), ReadOnly(CellLock), } @@ -52,6 +58,7 @@ impl CellEditorKind { }; match kind { ColumnKind::Bool(_) => Self::Toggle, + ColumnKind::Enum { labels, multiple } if picks_from(labels, *multiple) => Self::Choice, ColumnKind::Json { .. } => Self::Details(DetailsEditor::Json), ColumnKind::Xml => Self::Details(DetailsEditor::Xml), ColumnKind::Date => Self::Details(DetailsEditor::Date), @@ -93,6 +100,13 @@ impl CellEditorKind { } } +/// Whether a menu can offer an enum's labels. Without labels from the +/// catalog there is nothing to offer, and a set with an empty label reads +/// the same with that member and without it. +fn picks_from(labels: &[String], multiple: bool) -> bool { + (1..=CHOICE_MAX_LABELS).contains(&labels.len()) && !(multiple && labels.iter().any(String::is_empty)) +} + #[cfg(test)] mod tests { use tablepro_core::column::{ @@ -101,7 +115,7 @@ mod tests { }; use tablepro_core::edit::{EditText, ReadOnlyReason}; - use super::{CellEditorKind, DetailsEditor, INLINE_EDIT_MAX_BYTES, TextPurpose}; + use super::{CHOICE_MAX_LABELS, CellEditorKind, DetailsEditor, INLINE_EDIT_MAX_BYTES, TextPurpose}; use crate::services::grid::CellLock; use crate::services::grid::test_columns::grid_of_kind; @@ -170,7 +184,7 @@ mod tests { ), ( ColumnKind::Enum { - labels: vec!["a".to_owned()], + labels: Vec::new(), multiple: false, }, free(false), @@ -291,4 +305,43 @@ mod tests { CellEditorKind::ReadOnly(CellLock::Value(ReadOnlyReason::Bytes)) ); } + + #[test] + fn an_enum_or_set_with_labels_opens_a_menu_of_them() { + for multiple in [false, true] { + let labelled = ColumnKind::Enum { + labels: vec!["a".to_owned(), "b".to_owned()], + multiple, + }; + assert_eq!( + CellEditorKind::for_cell(&grid_of_kind("e", labelled), Ok(())), + CellEditorKind::Choice + ); + } + let many = ColumnKind::Enum { + labels: (0..=CHOICE_MAX_LABELS).map(|label| label.to_string()).collect(), + multiple: false, + }; + assert_eq!(CellEditorKind::for_cell(&grid_of_kind("e", many), Ok(())), free(false)); + let set_with_empty = ColumnKind::Enum { + labels: vec!["a".to_owned(), String::new()], + multiple: true, + }; + assert_eq!( + CellEditorKind::for_cell(&grid_of_kind("s", set_with_empty), Ok(())), + free(false), + "a set whose empty member cannot be read back offered a menu" + ); + let labelled = grid_of_kind( + "e", + ColumnKind::Enum { + labels: vec!["a".to_owned()], + multiple: false, + }, + ); + assert_eq!( + CellEditorKind::for_cell(&labelled, Err(CellLock::KeyColumn)), + CellEditorKind::ReadOnly(CellLock::KeyColumn) + ); + } } diff --git a/linux/crates/app/src/ui/data_grid/edit/actions.rs b/linux/crates/app/src/ui/data_grid/edit/actions.rs index 5571d2f97c..81cd815f35 100644 --- a/linux/crates/app/src/ui/data_grid/edit/actions.rs +++ b/linux/crates/app/src/ui/data_grid/edit/actions.rs @@ -61,7 +61,12 @@ pub(super) fn refresh(controller: &EditController, grid: &DataGrid, target: Opti let edits = !typing && matches!( kind, - Some(CellEditorKind::Inline { .. } | CellEditorKind::Toggle | CellEditorKind::Details(_)) + Some( + CellEditorKind::Inline { .. } + | CellEditorKind::Toggle + | CellEditorKind::Choice + | CellEditorKind::Details(_) + ) ); enable(grid, "edit", edits); enable(grid, "toggle", !typing && kind == Some(CellEditorKind::Toggle)); diff --git a/linux/crates/app/src/ui/data_grid/edit/cell_editor.rs b/linux/crates/app/src/ui/data_grid/edit/cell_editor.rs index e96b245adf..b7963faaea 100644 --- a/linux/crates/app/src/ui/data_grid/edit/cell_editor.rs +++ b/linux/crates/app/src/ui/data_grid/edit/cell_editor.rs @@ -36,6 +36,12 @@ pub(super) struct OpenEdit { /// What editing a cell comes to. pub(super) enum Plan { Inline(Box), + /// A menu of the column's labels. Typing, Tab and a new row edit its + /// text in place instead, where it fits a one-line editor. + Choice { + start: Box, + in_place: bool, + }, Toggle, /// The value is edited in the detail pane. Details, @@ -45,14 +51,26 @@ pub(super) enum Plan { } pub(super) struct InlineStart { - column: GridColumn, - row: GridRow, - start: EditStart, + pub(super) column: GridColumn, + pub(super) row: GridRow, + pub(super) start: EditStart, text: String, accepts_line_breaks: bool, purpose: TextPurpose, } +impl Plan { + /// Where the cell's text can be edited in place. + pub(super) fn in_place(self) -> Option> { + match self { + Self::Inline(start) | Self::Choice { start, in_place: true } => Some(start), + Self::Choice { in_place: false, .. } | Self::Toggle | Self::Details | Self::Refused(_) | Self::Locked => { + None + } + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Opened { /// The whole value, selected, so the first key replaces it. @@ -123,6 +141,10 @@ impl EditController { self.open_inline(&grid, cell, start, Opened::ToEdit); true } + Plan::Choice { start, .. } => { + self.open_choice(&grid, cell, &start); + true + } } } @@ -135,12 +157,13 @@ impl EditController { return None; } match self.plan(&grid, &cell.row()?, cell.column()) { - Plan::Inline(start) => Some(self.open_inline(&grid, &cell, start, Opened::ByTyping)), Plan::Refused(why) => { grid.toast(why); None } - Plan::Toggle | Plan::Details | Plan::Locked => None, + plan => plan + .in_place() + .map(|start| self.open_inline(&grid, &cell, start, Opened::ByTyping)), } } @@ -214,6 +237,7 @@ impl EditController { CellEditorKind::ReadOnly(_) => return Plan::Locked, CellEditorKind::Toggle => return Plan::Toggle, CellEditorKind::Details(_) => return Plan::Details, + CellEditorKind::Choice => (false, TextPurpose::FreeForm), CellEditorKind::Inline { accepts_line_breaks, purpose, @@ -226,34 +250,55 @@ impl EditController { CellInput::Value(value) => edit_text(value, column.value_type()), CellInput::Default => EditText::Editable(String::new()), }; - match &text { + let refusal = match &text { EditText::ReadOnly(reason) => { return Plan::Refused(edit_parse_message(&EditParseError::NotEditable(*reason))); } // A GTK editor holds text only up to a NUL, and committing it // would cut the value there. EditText::Editable(editable) if editable.contains('\0') => { - return Plan::Refused(gettext("Values with a NUL character can't be edited here")); - } - EditText::Editable(editable) if editable.len() > EDIT_MAX_BYTES => { - return Plan::Refused(too_large_to_edit()); + Some(gettext("Values with a NUL character can't be edited here")) } - EditText::Editable(_) => {} - } - if !matches!(kind.for_text(&text), CellEditorKind::Inline { .. }) { - return Plan::Details; - } + EditText::Editable(editable) if editable.len() > EDIT_MAX_BYTES => Some(too_large_to_edit()), + EditText::Editable(_) => None, + }; + let one_line = CellEditorKind::Inline { + accepts_line_breaks, + purpose, + }; + let in_place = refusal.is_none() && matches!(one_line.for_text(&text), CellEditorKind::Inline { .. }); let EditText::Editable(text) = text else { return Plan::Locked; }; - Plan::Inline(Box::new(InlineStart { + let start = Box::new(InlineStart { column, row: row.clone(), start, text, accepts_line_breaks, purpose, - })) + }); + match (kind, refusal) { + // The menu holds no text, so any value opens it. + (CellEditorKind::Choice, _) => Plan::Choice { start, in_place }, + (_, Some(why)) => Plan::Refused(why), + _ if in_place => Plan::Inline(start), + _ => Plan::Details, + } + } + + /// Edit `cell`'s text in place, as Tab and a new row do, a cell that + /// picks from a menu included. + pub(super) fn edit_in_place(&self, cell: &GridCell) { + let (Some(grid), Some(row)) = (self.grid.upgrade(), cell.row()) else { + return; + }; + if self.open_in(cell).is_some() { + return; + } + if let Some(start) = self.plan(&grid, &row, cell.column()).in_place() { + self.open_inline(&grid, cell, start, Opened::ToEdit); + } } fn open_inline(&self, grid: &DataGrid, cell: &GridCell, plan: Box, opened: Opened) -> gtk::Text { @@ -476,7 +521,7 @@ impl EditController { shown .get(at) .zip(selection.item(position).and_downcast::()) - .is_some_and(|(column, row)| matches!(self.plan(&grid, &row, *column), Plan::Inline(_))) + .is_some_and(|(column, row)| self.plan(&grid, &row, *column).in_place().is_some()) }; let target = next_edit_target( opens_inline, @@ -490,7 +535,7 @@ impl EditController { match target { Some((position, column)) => { if let Some(cell) = grid.focus_cell(position, column) { - self.edit_cell(&cell); + self.edit_in_place(&cell); } } None => { diff --git a/linux/crates/app/src/ui/data_grid/edit/choice_menu.rs b/linux/crates/app/src/ui/data_grid/edit/choice_menu.rs new file mode 100644 index 0000000000..7dd4b5d4ee --- /dev/null +++ b/linux/crates/app/src/ui/data_grid/edit/choice_menu.rs @@ -0,0 +1,264 @@ +use std::rc::Rc; + +use gtk4::prelude::*; +use gtk4::{self as gtk, gio, glib}; +use tablepro_core::Value; +use tablepro_core::column::ColumnKind; +use tablepro_core::edit::{CellInput, EditText, edit_text}; + +use super::cell_editor::{InlineStart, Plan}; +use super::{EditController, edit_start}; +use crate::i18n::pgettext; +use crate::services::grid::GridColumn; +use crate::ui::data_grid::grid_menu::presets_section; +use crate::ui::data_grid::selection::Target; +use crate::ui::data_grid::{DataGrid, GridCell, PinnedRow}; +use crate::ui::menu_label; + +/// The prefix a choice menu's actions are inserted under. +pub const CHOICE_ACTIONS: &str = "choice"; + +const LABEL: &str = "label"; + +/// MySQL lists a set's members in the column's order, apart by commas, +/// which no member can hold. +const SEPARATOR: char = ','; + +/// A cell's labels as menu items, radio items for an enum and check items +/// for a set, with the actions they act through on the row and column +/// they were made for. +pub struct ChoiceMenu { + pub section: gio::Menu, + pub actions: gio::SimpleActionGroup, +} + +impl EditController { + /// The labels the cell at `row` and `column` picks from, as it stands + /// now. `None` for a cell that does not pick from labels. + pub fn choice_menu(&self, row: &PinnedRow, column: usize) -> Option { + let grid = self.grid.upgrade()?; + match self.plan(&grid, row.get()?, column) { + Plan::Choice { start, .. } => build_menu(&grid, &start), + _ => None, + } + } + + /// The menu of `cell`'s labels, with the presets the cell takes. + pub(super) fn open_choice(&self, grid: &DataGrid, cell: &GridCell, start: &InlineStart) { + let Some(popover) = grid.menus().and_then(|menus| menus.choices.clone()) else { + return; + }; + let Some(menu) = build_menu(grid, start) else { + return; + }; + let model = gio::Menu::new(); + model.append_section(None, &menu.section); + model.append_section(None, &presets_section()); + popover.insert_action_group(CHOICE_ACTIONS, Some(&menu.actions)); + popover.set_menu_model(Some(&model)); + // The header that names the column takes no focus. + popover.update_property(&[gtk::accessible::Property::Label(&start.column.name)]); + let pin = start.row.pin(); + grid.hold_menu_target(Target { + cell: Some((pin.clone(), start.column.index)), + rows: vec![pin], + }); + grid.refresh_actions(); + grid.show_popover(&popover, grid.bounds_in_view(cell.upcast_ref())); + } + + /// Write what `pick` makes of the cell's text now. Returns the text + /// the cell holds after it, `None` for NULL or DEFAULT. A row that left + /// its slot or a cell locked since takes nothing. + fn choose( + &self, + grid: &DataGrid, + row: &PinnedRow, + column: usize, + pick: impl FnOnce(Option<&str>) -> String, + ) -> Option { + let row = row.get()?.clone(); + let grid_column = self.unlocked_column(grid, &row, column)?; + grid.commit_editors(); + let start = edit_start(&self.tracked(), &row, column); + let next = pick(chosen(&grid_column, &start.original).as_deref()); + self.commit_input(grid, &grid_column, &start, CellInput::Value(Value::Text(next))); + chosen(&grid_column, &self.tracked().edit_input(&row, column)) + } +} + +fn build_menu(grid: &DataGrid, start: &InlineStart) -> Option { + let ColumnKind::Enum { labels, multiple } = &start.column.value_type().kind else { + return None; + }; + let pin = start.row.pin(); + let column = start.column.index; + let current = chosen(&start.column, &start.start.original); + let menu = ChoiceMenu { + section: gio::Menu::new(), + actions: gio::SimpleActionGroup::new(), + }; + let labels: Rc<[String]> = labels.as_slice().into(); + match multiple { + false => enum_items(&menu, grid, pin, column, labels, current.as_deref()), + true => set_items(&menu, grid, &pin, column, labels, current.as_deref()), + } + Some(menu) +} + +/// One radio item per label, on one action whose state is the index of +/// the label the cell holds. An index, since a GVariant string ends at a +/// NUL and ClickHouse labels can hold one. +fn enum_items( + menu: &ChoiceMenu, + grid: &DataGrid, + pin: PinnedRow, + column: usize, + labels: Rc<[String]>, + current: Option<&str>, +) { + let action = gio::SimpleAction::new_stateful( + LABEL, + Some(Option::::static_variant_type().as_ref()), + &label_index(&labels, current).to_variant(), + ); + let weak = grid.downgrade(); + let picked = labels.clone(); + action.connect_activate(move |action, parameter| { + let label = parameter + .and_then(glib::Variant::get::>) + .flatten() + .and_then(|index| picked.get(usize::try_from(index).ok()?).cloned()); + let (Some(grid), Some(label)) = (weak.upgrade(), label) else { + return; + }; + let now = grid.edit().choose(&grid, &pin, column, |_| label); + action.set_state(&label_index(&picked, now.as_deref()).to_variant()); + }); + menu.actions.add_action(&action); + for (index, label) in labels.iter().enumerate() { + let item = gio::MenuItem::new(Some(&item_label(label)), None); + item.set_action_and_target_value( + Some(&format!("{CHOICE_ACTIONS}.{LABEL}")), + Some(&u32::try_from(index).ok().to_variant()), + ); + menu.section.append_item(&item); + } +} + +/// One check item per label, each on its own action, since a check item +/// reads its state from an action without a target. +fn set_items( + menu: &ChoiceMenu, + grid: &DataGrid, + pin: &PinnedRow, + column: usize, + labels: Rc<[String]>, + current: Option<&str>, +) { + let held = members(current); + for (index, label) in labels.iter().enumerate() { + let name = format!("member-{index}"); + let action = gio::SimpleAction::new_stateful(&name, None, &held.contains(&label.as_str()).to_variant()); + let (weak, pin, labels) = (grid.downgrade(), pin.clone(), labels.clone()); + action.connect_activate(move |action, _| { + let Some(grid) = weak.upgrade() else { + return; + }; + let now = grid + .edit() + .choose(&grid, &pin, column, |current| toggled_member(&labels, current, index)); + let member = labels + .get(index) + .is_some_and(|label| members(now.as_deref()).contains(&label.as_str())); + action.set_state(&member.to_variant()); + }); + menu.actions.add_action(&action); + let item = gio::MenuItem::new(Some(&item_label(label)), Some(&format!("{CHOICE_ACTIONS}.{name}"))); + menu.section.append_item(&item); + } +} + +fn label_index(labels: &[String], text: Option<&str>) -> Option { + let index = labels.iter().position(|label| Some(label.as_str()) == text)?; + u32::try_from(index).ok() +} + +/// A NUL shows as U+2400, as in a cell, since GTK's text ends at one. +fn item_label(label: &str) -> String { + match label.is_empty() { + // Translators: an enum label that is the empty string, as an item in the menu of a cell's labels. + true => pgettext("enum label", "Empty"), + false => menu_label(&label.replace('\0', "\u{2400}")), + } +} + +/// The cell's value as text, `None` for NULL or DEFAULT. +fn chosen(column: &GridColumn, input: &CellInput) -> Option { + match input { + CellInput::Default | CellInput::Value(Value::Null) => None, + CellInput::Value(value) => match edit_text(value, column.value_type()) { + EditText::Editable(text) => Some(text), + EditText::ReadOnly(_) => None, + }, + } +} + +fn members(text: Option<&str>) -> Vec<&str> { + text.map_or_else(Vec::new, |text| { + text.split(SEPARATOR).filter(|member| !member.is_empty()).collect() + }) +} + +/// The set with `labels[index]` added or taken out: the labels in the +/// column's order, then any member the labels do not name, as it was. +fn toggled_member(labels: &[String], current: Option<&str>, index: usize) -> String { + let held = members(current); + let known = labels.iter().enumerate().filter(|(at, label)| { + let holds = held.contains(&label.as_str()); + if *at == index { !holds } else { holds } + }); + let unknown = held + .iter() + .copied() + .filter(|member| !labels.iter().any(|label| label == member)); + known + .map(|(_, label)| label.as_str()) + .chain(unknown) + .collect::>() + .join(&SEPARATOR.to_string()) +} + +#[cfg(test)] +mod tests { + use super::{members, toggled_member}; + + fn labels(names: &[&str]) -> Vec { + names.iter().map(|name| (*name).to_owned()).collect() + } + + #[test] + fn a_toggled_member_keeps_the_columns_order_and_members_it_does_not_know() { + let tags = labels(&["new", "sale", "gift"]); + + assert_eq!(toggled_member(&tags, Some("gift"), 0), "new,gift"); + assert_eq!(toggled_member(&tags, Some("new,gift"), 2), "new"); + assert_eq!( + toggled_member(&tags, Some("new"), 0), + "", + "the last member out is the empty set" + ); + assert_eq!(toggled_member(&tags, None, 1), "sale", "NULL holds no member"); + assert_eq!( + toggled_member(&tags, Some("old,sale"), 0), + "new,sale,old", + "a member the labels do not name was dropped" + ); + assert_eq!( + members(Some(" lead,x")), + [" lead", "x"], + "a member's spaces are its own" + ); + assert!(members(Some("")).is_empty()); + } +} diff --git a/linux/crates/app/src/ui/data_grid/edit/mod.rs b/linux/crates/app/src/ui/data_grid/edit/mod.rs index 42fe0ad852..1e70bee0ce 100644 --- a/linux/crates/app/src/ui/data_grid/edit/mod.rs +++ b/linux/crates/app/src/ui/data_grid/edit/mod.rs @@ -1,9 +1,10 @@ -//! Every change a grid makes to its rows: the inline editor, toggles, -//! presets, new and duplicated rows, deletes, pastes, undo and redo. +//! Every change a grid makes to its rows: the inline editor, label menus, +//! toggles, presets, new and duplicated rows, deletes, pastes, undo and redo. //! Each goes through the tracker, and each delta reaches the store here. mod actions; mod cell_editor; +mod choice_menu; mod deletion; mod paste; mod row_edits; @@ -21,6 +22,7 @@ use libadwaita::prelude::*; use tablepro_core::{ColumnInfo, Value}; use self::cell_editor::OpenEdit; +pub(crate) use self::choice_menu::CHOICE_ACTIONS; pub(crate) use self::deletion::BULK_DELETE_CONFIRM_THRESHOLD; use super::selection::Resolved; use super::{DataGrid, EditRules, GridCell, GridRequest, GridRow, PinnedRow, RowData, TrackedValues}; diff --git a/linux/crates/app/src/ui/data_grid/edit/row_edits.rs b/linux/crates/app/src/ui/data_grid/edit/row_edits.rs index a08ded7b72..1de5c5a0af 100644 --- a/linux/crates/app/src/ui/data_grid/edit/row_edits.rs +++ b/linux/crates/app/src/ui/data_grid/edit/row_edits.rs @@ -4,7 +4,6 @@ use tablepro_core::Value; use tablepro_core::column::{BoolStorage, ColumnKind}; use tablepro_core::edit::{CellInput, EditParseError, new_draft_cell, parse_edit_text}; -use super::cell_editor::Plan; use super::{EditController, edit_start}; use crate::i18n::gettext; use crate::services::change_tracker::RowKey; @@ -173,9 +172,9 @@ impl EditController { let column = grid .shown_columns() .into_iter() - .find(|column| matches!(self.plan(grid, &row, *column), Plan::Inline(_))); + .find(|column| self.plan(grid, &row, *column).in_place().is_some()); if let Some(cell) = column.and_then(|column| grid.focus_cell(position, column)) { - self.edit_cell(&cell); + self.edit_in_place(&cell); } } diff --git a/linux/crates/app/src/ui/data_grid/edit/tests/choices.rs b/linux/crates/app/src/ui/data_grid/edit/tests/choices.rs new file mode 100644 index 0000000000..3be79b201e --- /dev/null +++ b/linux/crates/app/src/ui/data_grid/edit/tests/choices.rs @@ -0,0 +1,279 @@ +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use gtk4::{self as gtk, gdk, gio}; +use tablepro_core::Value; +use tablepro_core::column::ColumnKind; +use tablepro_core::edit::CellInput; + +use super::{editing, editor, none, pending, press_in_editor, type_into}; +use crate::test_support::{assert_labelled, descendants, drain_main_context, test_backend_records_properties}; +use crate::ui::data_grid::test_grid::{Grid, named, text}; + +fn sizes_and_tags() -> Grid { + Grid::open( + vec![ + named("id", "integer", true), + named("name", "text", false), + named("size", "enum('small','medium','in_stock','')", false), + named("tags", "set('new','sale','gift')", false), + ], + vec![vec![ + Value::Int(1), + Value::Text("a".into()), + Value::Text("small".into()), + Value::Text("new".into()), + ]], + ) +} + +fn choices(grid: &Grid) -> gtk::PopoverMenu { + grid.grid + .menus() + .and_then(|menus| menus.choices.clone()) + .expect("an editing grid has a choice menu") +} + +/// The menu's item labelled `label`. A menu item is a GtkModelButton, +/// which GTK keeps private and gives no action name. +fn find_item(grid: &Grid, label: &str) -> Option { + descendants(&choices(grid)).into_iter().find(|widget| { + widget.type_().name() == "GtkModelButton" && widget.property::>("text").as_deref() == Some(label) + }) +} + +fn item(grid: &Grid, label: &str) -> gtk::Widget { + find_item(grid, label).expect("the menu has the item") +} + +/// GTK takes an item hidden while its action is disabled out of the menu. +fn shown(grid: &Grid, label: &str) -> bool { + find_item(grid, label).is_some_and(|item| item.is_visible()) +} + +fn checked(item: >k::Widget) -> bool { + item.property::("active") +} + +fn labels(menu: &gio::MenuModel) -> Vec { + (0..menu.n_items()) + .flat_map(|position| { + let label = menu + .item_attribute_value(position, "label", None) + .and_then(|label| label.get::()); + let inner = menu.item_link(position, "section").map(|section| labels(§ion)); + label.into_iter().chain(inner.unwrap_or_default()) + }) + .collect() +} + +fn open_on(grid: &Grid, column: usize) { + let cell = grid.focus(0, column); + assert!(grid.grid.edit().edit_cell(&cell)); + assert!(choices(grid).is_visible(), "the cell opened no menu"); + assert!(editor(grid).is_none(), "the cell opened a text editor"); +} + +#[gtk4::test] +fn an_enum_cell_marks_its_label_and_takes_the_one_picked() { + let grid = sizes_and_tags(); + open_on(&grid, 2); + assert!(checked(&item(&grid, "small")), "the cell's own label is not marked"); + assert!(!checked(&item(&grid, "medium"))); + + item(&grid, "medium").emit_by_name::<()>("clicked", &[]); + drain_main_context(); + + assert_eq!(pending(&grid, 1, 2), Some(text("medium"))); + assert!(!choices(&grid).is_visible(), "a picked label left the menu open"); + assert_eq!( + grid.focused_cell().map(|cell| (cell.position(), cell.column())), + Some((Some(0), 2)), + "the keyboard left the cell" + ); +} + +#[gtk4::test] +fn labels_keep_their_underscores_and_the_empty_label_has_a_name() { + let grid = sizes_and_tags(); + open_on(&grid, 2); + let model = choices(&grid).menu_model().expect("a menu"); + + assert_eq!(&labels(&model)[..4], ["small", "medium", "in__stock", "Empty"]); + assert!(shown(&grid, "Set to NULL"), "a nullable cell offered no NULL"); + assert!(shown(&grid, "Set to Empty"), "the empty label is not offered as Empty"); + assert!(!shown(&grid, "Set to DEFAULT"), "a saved row offered DEFAULT"); + if test_backend_records_properties() { + assert_labelled(&choices(&grid)).expect("the menu is named after its column"); + } +} + +/// ClickHouse labels can hold a NUL, where GTK and GVariant strings end. +#[gtk4::test] +fn a_label_with_a_nul_is_a_choice_of_its_own() { + let mut state = named("state", "text", false); + state.column_type.kind = ColumnKind::Enum { + labels: vec!["x".to_owned(), "x\0y".to_owned()], + multiple: false, + }; + let grid = Grid::open( + vec![named("id", "integer", true), state], + vec![vec![Value::Int(1), Value::Text("x".into())]], + ); + open_on(&grid, 1); + assert!(checked(&item(&grid, "x"))); + assert!(!checked(&item(&grid, "x\u{2400}y"))); + + item(&grid, "x\u{2400}y").emit_by_name::<()>("clicked", &[]); + drain_main_context(); + assert_eq!(pending(&grid, 1, 1), Some(text("x\0y"))); + + open_on(&grid, 1); + assert!( + checked(&item(&grid, "x\u{2400}y")), + "a value no text editor can hold did not reopen its labels" + ); +} + +/// A value a one-line editor would mangle still opens the labels, and Tab +/// goes past it as it goes past any cell edited elsewhere. +#[gtk4::test] +fn tab_passes_a_value_the_one_line_editor_cannot_hold() { + let grid = Grid::open( + vec![ + named("id", "integer", true), + named("name", "text", false), + named("size", "enum('small','large')", false), + named("note", "text", false), + ], + vec![vec![ + Value::Int(1), + Value::Text("a".into()), + Value::Text("two\nlines".into()), + Value::Text("n".into()), + ]], + ); + let name = grid.focus(0, 1); + type_into(&grid, &name, "b"); + assert!(press_in_editor( + &editor(&grid).expect("an editor"), + gdk::Key::Tab, + none() + )); + let note = editing(&grid); + assert_eq!( + note.column(), + 3, + "Tab opened a one-line editor on a value with a line break" + ); + + open_on(&grid, 2); +} + +/// GTK gives the focus a closed menu held to the first control an ancestor +/// of the grid takes, below the rows. +#[gtk4::test] +fn the_cell_takes_the_keyboard_back_when_the_labels_close() { + let grid = sizes_and_tags(); + open_on(&grid, 2); + gtk::prelude::GtkWindowExt::set_focus(&grid.window, None::<>k::Widget>); + + choices(&grid).popdown(); + drain_main_context(); + + assert_eq!( + grid.focused_cell().map(|cell| (cell.position(), cell.column())), + Some((Some(0), 2)), + "the keyboard did not come back to the cell" + ); +} + +/// Edit Cell in the cell menu opens the labels while the cell menu lets go +/// of its target, which GTK has it do before the item runs. +#[gtk4::test] +fn edit_cell_from_the_cell_menu_keeps_the_labels_target() { + let grid = sizes_and_tags(); + let cell = grid.focus(0, 2); + assert!(grid.grid.open_cell_menu(&cell, None)); + grid.grid.menus().expect("menus").cell.popdown(); + grid.grid.release_menu_target_soon(); + + grid.activate("edit"); + + assert!(choices(&grid).is_visible(), "Edit Cell opened no labels"); + assert!( + grid.grid.imp().menu_target.borrow().is_some(), + "the cell menu's close let go of the labels' target" + ); +} + +#[gtk4::test] +fn set_to_null_from_the_menu_writes_null_into_the_cell() { + let grid = sizes_and_tags(); + open_on(&grid, 2); + + item(&grid, "Set to NULL").emit_by_name::<()>("clicked", &[]); + drain_main_context(); + + assert_eq!(pending(&grid, 1, 2), Some(CellInput::Value(Value::Null))); +} + +/// Space on a check item toggles it in place, as activating its action +/// does, so several members change while the menu stays open. +#[gtk4::test] +fn set_members_toggle_one_at_a_time_in_the_columns_order() { + let grid = sizes_and_tags(); + open_on(&grid, 3); + let menu = choices(&grid); + assert!(checked(&item(&grid, "new")), "the cell's member is not checked"); + assert!(!checked(&item(&grid, "gift"))); + + menu.activate_action("choice.member-2", None).expect("gift"); + drain_main_context(); + assert_eq!(pending(&grid, 1, 3), Some(text("new,gift"))); + assert!(checked(&item(&grid, "gift"))); + + menu.activate_action("choice.member-1", None).expect("sale"); + menu.activate_action("choice.member-0", None).expect("new"); + menu.activate_action("choice.member-2", None).expect("gift"); + drain_main_context(); + assert_eq!(pending(&grid, 1, 3), Some(text("sale"))); + + menu.activate_action("choice.member-1", None).expect("sale"); + drain_main_context(); + assert_eq!( + pending(&grid, 1, 3), + Some(text("")), + "the empty set was written as something else" + ); + assert!(menu.is_visible(), "a toggled member closed the menu"); +} + +#[gtk4::test] +fn a_set_cell_takes_the_empty_set_as_empty() { + let grid = sizes_and_tags(); + grid.focus(0, 3); + grid.grid.refresh_actions(); + assert!(grid.is_enabled("set-empty"), "a set cannot be set to the empty set"); + + grid.activate("set-empty"); + + assert_eq!(pending(&grid, 1, 3), Some(text(""))); +} + +#[gtk4::test] +fn tab_and_typing_edit_an_enum_cells_text() { + let grid = sizes_and_tags(); + let name = grid.focus(0, 1); + type_into(&grid, &name, "b"); + + assert!(press_in_editor( + &editor(&grid).expect("an editor"), + gdk::Key::Tab, + none() + )); + + let size = editing(&grid); + assert_eq!((size.position(), size.column()), (Some(0), 2), "Tab skipped the enum"); + assert_eq!(editor(&grid).expect("a text editor").text(), "small"); + assert!(!choices(&grid).is_visible()); +} diff --git a/linux/crates/app/src/ui/data_grid/edit/tests/mod.rs b/linux/crates/app/src/ui/data_grid/edit/tests/mod.rs index 5ee5354047..9191e074d8 100644 --- a/linux/crates/app/src/ui/data_grid/edit/tests/mod.rs +++ b/linux/crates/app/src/ui/data_grid/edit/tests/mod.rs @@ -1,3 +1,4 @@ +mod choices; mod paste; mod rows; diff --git a/linux/crates/app/src/ui/data_grid/grid_menu.rs b/linux/crates/app/src/ui/data_grid/grid_menu.rs index 5ee7b99765..0247373849 100644 --- a/linux/crates/app/src/ui/data_grid/grid_menu.rs +++ b/linux/crates/app/src/ui/data_grid/grid_menu.rs @@ -18,6 +18,8 @@ pub struct GridMenus { pub references: gio::Menu, /// The keys a column is in, for Ctrl+J to choose from. pub reference_chooser: gtk::PopoverMenu, + /// An enum or set cell's labels, for a grid that edits. + pub choices: Option, } /// An item shown only while its action is enabled, so a cell it does not @@ -32,6 +34,15 @@ fn item(label: &str, action: &str) -> gio::MenuItem { crate::application::menu_item(label, action) } +/// Set to Empty, NULL or DEFAULT, each shown where the cell takes it. +pub(crate) fn presets_section() -> gio::Menu { + section([ + hidden_while_disabled(&gettext("Set to Empty"), "grid.set-empty"), + hidden_while_disabled(&gettext("Set to NULL"), "grid.set-null"), + hidden_while_disabled(&gettext("Set to DEFAULT"), "grid.set-default"), + ]) +} + fn section(items: impl IntoIterator) -> gio::Menu { let menu = gio::Menu::new(); for item in items { @@ -55,11 +66,13 @@ impl GridMenus { ) }); let reference_chooser = popover(grid, &gio::Menu::new()); + let choices = editable.then(|| popover(grid, &gio::Menu::new())); Self { cell, empty, references, reference_chooser, + choices, } } @@ -71,6 +84,9 @@ impl GridMenus { empty.unparent(); } self.reference_chooser.unparent(); + if let Some(choices) = &self.choices { + choices.unparent(); + } } } @@ -82,6 +98,7 @@ fn popover(grid: &DataGrid, model: &gio::Menu) -> gtk::PopoverMenu { let weak = grid.downgrade(); popover.connect_closed(move |_| { if let Some(grid) = weak.upgrade() { + grid.return_focus_from_menu(); grid.release_menu_target_soon(); } }); @@ -94,15 +111,9 @@ fn cell_menu(grid: &DataGrid, references: &gio::Menu) -> gio::Menu { let sorts = config.is_some_and(|config| config.sorting != GridSorting::None); let menu = gio::Menu::new(); if editable { - menu.append_section( - None, - §ion([ - hidden_while_disabled(&gettext("Edit Cell"), "grid.edit"), - hidden_while_disabled(&gettext("Set to Empty"), "grid.set-empty"), - hidden_while_disabled(&gettext("Set to NULL"), "grid.set-null"), - hidden_while_disabled(&gettext("Set to DEFAULT"), "grid.set-default"), - ]), - ); + let edits = presets_section(); + edits.prepend_item(&hidden_while_disabled(&gettext("Edit Cell"), "grid.edit")); + menu.append_section(None, &edits); } let copy_as = section([ item(&gettext("Rows"), "grid.copy-rows"), diff --git a/linux/crates/app/src/ui/data_grid/imp.rs b/linux/crates/app/src/ui/data_grid/imp.rs index e689b876f8..9720e464cd 100644 --- a/linux/crates/app/src/ui/data_grid/imp.rs +++ b/linux/crates/app/src/ui/data_grid/imp.rs @@ -70,6 +70,9 @@ pub struct DataGrid { pub find: FindState, /// What a menu was opened on, until it closes. pub menu_target: RefCell>, + /// How many targets menus have held, so a closing menu lets go of its + /// own and not of one a later menu holds. + pub menu_targets_held: Cell, /// The key choices the latest menu or chooser offered, which its items /// act on whatever the keys or rows are by then. pub armed_references: RefCell, diff --git a/linux/crates/app/src/ui/data_grid/row_detail/imp.rs b/linux/crates/app/src/ui/data_grid/row_detail/imp.rs index d8882ecf2b..64e1d765a2 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/imp.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/imp.rs @@ -42,6 +42,8 @@ pub struct RowDetailPane { #[template_child] pub field_label: TemplateChild, #[template_child] + pub choice_button: TemplateChild, + #[template_child] pub calendar_button: TemplateChild, #[template_child] pub calendar_popover: TemplateChild, diff --git a/linux/crates/app/src/ui/data_grid/row_detail/mod.rs b/linux/crates/app/src/ui/data_grid/row_detail/mod.rs index 3f0fb18c4f..afa81b1291 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/mod.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/mod.rs @@ -40,6 +40,7 @@ impl RowDetailPane { self.connect_fields(grid); self.connect_value_editor(grid); self.connect_value_menu(grid); + self.connect_choices(); self.connect_calendar(); self.connect_clock(); self.connect_close(grid); diff --git a/linux/crates/app/src/ui/data_grid/row_detail/tests.rs b/linux/crates/app/src/ui/data_grid/row_detail/tests.rs index 135979f0ca..f9a49e542f 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/tests.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/tests.rs @@ -412,6 +412,37 @@ fn every_control_in_the_pane_is_named() { } } +#[gtk4::test] +fn an_enum_field_picks_from_its_labels() { + let (grid, pane) = opened( + vec![ + named("id", "integer", true), + named("size", "enum('small','large')", false), + named("note", "text", false), + ], + vec![vec![ + Value::Int(1), + Value::Text("small".into()), + Value::Text("x".into()), + ]], + ); + take_keyboard(&grid); + let button = pane.imp().choice_button.get(); + grid.focus(0, 2); + assert!(!button.is_visible(), "a text field offered labels"); + grid.focus(0, 1); + assert!(button.is_visible(), "an enum field offered no labels"); + + button.popup(); + button + .activate_action("choice.label", Some(&Some(1u32).to_variant())) + .expect("the labels' action"); + drain_main_context(); + + assert_eq!(pending(&grid, 1, 1), Some(text("large"))); + assert_eq!(pane.typed_text(), "large"); +} + /// The middle of the first of the calendar's labels that `matches`, /// once the popover has laid it out. fn calendar_label(calendar: >k::Calendar, matches: impl Fn(>k::Widget) -> bool) -> (f64, f64) { @@ -697,6 +728,19 @@ fn what_the_value_editor_shows_for_a_cell() { ); assert!(!locked.calendar, "a locked cell offered a calendar"); assert!(value_content(&day, &saved, None, &value(a_date(2024, 1, 1))).calendar); + let size = column_of(named("size", "enum('s','l')", false)); + let small = value(Value::Text("s".into())); + assert!(value_content(&size, &saved, None, &small).choice); + assert!( + !value_content(&size, &saved, Some(CellLock::PendingDelete), &small).choice, + "a locked cell offered its labels" + ); + assert!(!value_content(¬e, &saved, None, &value(Value::Null)).choice); + let nul = value_content(&size, &saved, None, &value(Value::Text("s\0".into()))); + assert!( + nul.message.is_some() && nul.choice, + "a value the editor cannot hold hid the labels" + ); assert_eq!( value_content(&doc, &saved, None, &value(Value::Null)).language, Some("json") diff --git a/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs b/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs index df4fb6cff7..12a30c9990 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs @@ -2,7 +2,7 @@ use std::rc::{Rc, Weak}; use gtk4::prelude::*; use gtk4::subclass::prelude::*; -use gtk4::{self as gtk, gdk, glib}; +use gtk4::{self as gtk, gdk, gio, glib}; use libadwaita as adw; use sourceview5::prelude::*; use tablepro_core::Value; @@ -13,9 +13,10 @@ use super::RowDetailPane; use crate::application::component_matches; use crate::i18n::{gettext, gettext_f}; use crate::services::grid::{ - CellLock, CommitResult, EDIT_MAX_BYTES, EditStart, GridColumn, GridRowId, auto_filled_sentinel, display_text, - empty_placeholder, + CellEditorKind, CellLock, CommitResult, EDIT_MAX_BYTES, EditStart, GridColumn, GridRowId, auto_filled_sentinel, + display_text, empty_placeholder, }; +use crate::ui::data_grid::edit::CHOICE_ACTIONS; use crate::ui::data_grid::selection::Target; use crate::ui::data_grid::{DataGrid, GridRow, PinnedRow}; use crate::ui::error_text::edit_parse_message; @@ -79,6 +80,8 @@ pub struct ValueContent { pub emptied_placeholder: Option, pub language: Option<&'static str>, pub calendar: bool, + /// The column's labels to pick from. + pub choice: bool, pub clock: bool, } @@ -88,6 +91,10 @@ pub fn value_content(column: &GridColumn, row: &GridRowId, lock: Option typed_form(column, value), CellInput::Default => (String::new(), None), }; + let decodable = match input { + CellInput::Value(value) => matches!(edit_text(value, column.value_type()), EditText::Editable(_)), + CellInput::Default => true, + }; let message = lock.map(|lock| lock.explanation(edit_parse_message)).or(refusal); let editable = message.is_none(); let emptied_placeholder = editable.then(|| empty_placeholder(column, row)).flatten(); @@ -107,6 +114,9 @@ pub fn value_content(column: &GridColumn, row: &GridRowId, lock: Option None, }, calendar: editable && matches!(kind, ColumnKind::Date | ColumnKind::Timestamp { .. }), + // The menu holds no text, so a value too long or with a NUL for the + // editor still opens it. + choice: lock.is_none() && decodable && CellEditorKind::for_cell(column, Ok(())) == CellEditorKind::Choice, clock: editable && super::clock::ClockField::of(kind).is_some(), message, } @@ -341,6 +351,7 @@ impl RowDetailPane { emptied_placeholder: None, language: None, calendar: false, + choice: false, clock: false, }; self.show_value("", &content); @@ -376,6 +387,7 @@ impl RowDetailPane { .update_property(&[gtk::accessible::Property::Label(name)]); self.show_message(content.message.as_deref(), false); imp.calendar_button.set_visible(content.calendar); + imp.choice_button.set_visible(content.choice); imp.clock_button.set_visible(content.clock); self.update_placeholder(); } @@ -433,6 +445,32 @@ impl RowDetailPane { } } + /// The labels of the field shown, filled as the menu opens so they act + /// on that row and field. + pub(super) fn connect_choices(&self) { + let button = self.imp().choice_button.get(); + button.set_menu_model(Some(&gio::Menu::new())); + button.set_create_popup_func(glib::clone!( + #[weak(rename_to = pane)] + self, + move |button| pane.fill_choices(button) + )); + } + + fn fill_choices(&self, button: >k::MenuButton) { + let Some(model) = button.menu_model().and_downcast::() else { + return; + }; + model.remove_all(); + let (Some(grid), Some(pin), Some(column)) = (self.grid(), self.current_pin(), self.imp().field.get()) else { + return; + }; + if let Some(menu) = grid.edit().choice_menu(&pin, column) { + button.insert_action_group(CHOICE_ACTIONS, Some(&menu.actions)); + model.append_section(None, &menu.section); + } + } + fn arm_value_menu(&self) { let Some(grid) = self.grid() else { return; diff --git a/linux/crates/app/src/ui/data_grid/selection.rs b/linux/crates/app/src/ui/data_grid/selection.rs index 463a02b80b..a676ddd190 100644 --- a/linux/crates/app/src/ui/data_grid/selection.rs +++ b/linux/crates/app/src/ui/data_grid/selection.rs @@ -137,15 +137,39 @@ impl DataGrid { /// Hold `target` for the actions a menu offers, until it closes. pub(crate) fn hold_menu_target(&self, target: Target) { - self.imp().menu_target.replace(Some(target)); + let imp = self.imp(); + imp.menu_target.replace(Some(target)); + imp.menu_targets_held.set(imp.menu_targets_held.get().wrapping_add(1)); + } + + /// GTK hands the focus a closed popover held to the first control an + /// ancestor of the grid takes, so the cell the menu was opened on takes + /// it back. The item's action runs after this and may move it on. + pub(crate) fn return_focus_from_menu(&self) { + let cell = self + .imp() + .menu_target + .borrow() + .as_ref() + .and_then(|target| target.cell.clone()); + let Some((row, column)) = cell else { + return; + }; + if let Some(position) = row.get().and_then(|row| self.view_position(row)) { + self.focus_cell(position, column); + } } /// Let go of the menu's target once the item it activated, if any, - /// has run: GTK closes a menu before its item's action runs. + /// has run: GTK closes a menu before its item's action runs. A menu + /// that item opened, as Edit Cell opens a cell's labels, keeps its own. pub(crate) fn release_menu_target_soon(&self) { let grid = self.downgrade(); + let held = self.imp().menu_targets_held.get(); glib::idle_add_local_once(move || { - if let Some(grid) = grid.upgrade() { + if let Some(grid) = grid.upgrade() + && grid.imp().menu_targets_held.get() == held + { grid.imp().menu_target.take(); grid.refresh_actions(); } diff --git a/linux/crates/app/src/ui/data_grid/tests/menus.rs b/linux/crates/app/src/ui/data_grid/tests/menus.rs index b210c0b632..c1e324efe1 100644 --- a/linux/crates/app/src/ui/data_grid/tests/menus.rs +++ b/linux/crates/app/src/ui/data_grid/tests/menus.rs @@ -250,6 +250,24 @@ fn a_page_under_a_closed_menu_then_a_focus_in_keeps_the_focus_in_the_grid() { assert!(focus_in_grid(&grid), "the focus left the grid"); } +/// GTK gives the focus a closed menu held to the first control an ancestor +/// of the grid takes, so Escape or an item left the keyboard off the rows. +#[gtk4::test] +fn a_closed_cell_menu_gives_the_keyboard_back_to_its_cell() { + let grid = two_text_rows(); + let cell = grid.cell_of(2, 1); + assert!(grid.grid.open_cell_menu(&cell, None)); + gtk::prelude::GtkWindowExt::set_focus(&grid.window, None::<>k::Widget>); + + close_menus(&grid); + + assert_eq!( + grid.focused_cell(), + Some(cell), + "the keyboard did not come back to the cell" + ); +} + /// The header gives the rows the keys that act on a selection, wherever /// the focus was. #[gtk4::test] diff --git a/linux/crates/core/src/column/kind.rs b/linux/crates/core/src/column/kind.rs index b2edf23312..6355249c93 100644 --- a/linux/crates/core/src/column/kind.rs +++ b/linux/crates/core/src/column/kind.rs @@ -88,7 +88,8 @@ impl ColumnKind { pub fn accepts_empty_string(&self) -> bool { match self { Self::Text { .. } | Self::Other => true, - Self::Enum { labels, .. } => labels.iter().any(String::is_empty), + // A set with no members is the empty string. + Self::Enum { labels, multiple } => *multiple || labels.iter().any(String::is_empty), _ => false, } } @@ -236,6 +237,20 @@ mod tests { } } + #[test] + fn a_set_with_no_members_is_the_empty_string() { + let set = ColumnKind::Enum { + labels: vec!["a".to_owned()], + multiple: true, + }; + + assert!(set.accepts_empty_string()); + assert!( + !enumeration(&["a"]).accepts_empty_string(), + "an enum without an empty label" + ); + } + #[test] fn declared_precision_comes_from_the_kind() { let stamp = ColumnKind::Timestamp { diff --git a/linux/crates/core/src/edit/parse_edit_text.rs b/linux/crates/core/src/edit/parse_edit_text.rs index 32836c9b64..30910262a6 100644 --- a/linux/crates/core/src/edit/parse_edit_text.rs +++ b/linux/crates/core/src/edit/parse_edit_text.rs @@ -82,6 +82,34 @@ mod tests { ); } + #[test] + fn an_empty_set_is_a_value_and_an_empty_enum_is_not() { + let set = column( + ColumnKind::Enum { + labels: vec!["a".to_owned(), "b".to_owned()], + multiple: true, + }, + true, + ); + let enumeration = column( + ColumnKind::Enum { + labels: vec!["a".to_owned(), "b".to_owned()], + multiple: false, + }, + true, + ); + + assert_eq!( + parse_edit_text("", &set, EditContext::UpdateRow), + Ok(CellInput::Value(Value::Text(String::new()))), + "clearing a set wrote NULL instead of the empty set" + ); + assert_eq!( + parse_edit_text("", &enumeration, EditContext::UpdateRow), + Ok(CellInput::Value(Value::Null)) + ); + } + #[test] fn an_empty_draft_cell_asks_for_the_servers_default() { let mut defaulted = integer(false); diff --git a/linux/data/resources/ui/row-detail-pane.ui b/linux/data/resources/ui/row-detail-pane.ui index 5e266afad8..17c41799c4 100644 --- a/linux/data/resources/ui/row-detail-pane.ui +++ b/linux/data/resources/ui/row-detail-pane.ui @@ -160,6 +160,19 @@ + + + + false + Pick a Value + + Pick a Value + + + + x-office-calendar-symbolic diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index 4c1e116d14..ba56cfe121 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -237,10 +237,12 @@ crates/app/src/ui/data_grid/copy/mod.rs crates/app/src/ui/data_grid/copy/tests.rs crates/app/src/ui/data_grid/edit/actions.rs crates/app/src/ui/data_grid/edit/cell_editor.rs +crates/app/src/ui/data_grid/edit/choice_menu.rs crates/app/src/ui/data_grid/edit/deletion.rs crates/app/src/ui/data_grid/edit/mod.rs crates/app/src/ui/data_grid/edit/paste.rs crates/app/src/ui/data_grid/edit/row_edits.rs +crates/app/src/ui/data_grid/edit/tests/choices.rs crates/app/src/ui/data_grid/edit/tests/mod.rs crates/app/src/ui/data_grid/edit/tests/paste.rs crates/app/src/ui/data_grid/edit/tests/rows.rs From ddc50ed29c32c972f380911bb07a4d76e247f32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 11:26:18 +0700 Subject: [PATCH 16/85] fix(linux-grid): keep a timestamp's time when Row Details picks its day (#3890) --- linux/changelog.d/3890.fixed.md | 1 + .../src/ui/data_grid/row_detail/calendar.rs | 60 +++++-- .../app/src/ui/data_grid/row_detail/clock.rs | 63 +------ .../app/src/ui/data_grid/row_detail/tests.rs | 68 ++++++++ linux/crates/core/src/edit/mod.rs | 2 + .../core/src/edit/parse_literal_text.rs | 17 +- linux/crates/core/src/edit/temporal_parts.rs | 162 ++++++++++++++++++ 7 files changed, 294 insertions(+), 79 deletions(-) create mode 100644 linux/changelog.d/3890.fixed.md create mode 100644 linux/crates/core/src/edit/temporal_parts.rs diff --git a/linux/changelog.d/3890.fixed.md b/linux/changelog.d/3890.fixed.md new file mode 100644 index 0000000000..e8d1028c92 --- /dev/null +++ b/linux/changelog.d/3890.fixed.md @@ -0,0 +1 @@ +Picking a day in Row Details reset a timestamp's time to midnight when its year or spelling was not the plain form. diff --git a/linux/crates/app/src/ui/data_grid/row_detail/calendar.rs b/linux/crates/app/src/ui/data_grid/row_detail/calendar.rs index 2298906675..f5e554e888 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/calendar.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/calendar.rs @@ -3,30 +3,46 @@ use gtk4::prelude::*; use gtk4::subclass::prelude::*; use gtk4::{self as gtk, glib}; use tablepro_core::column::{ColumnKind, TemporalZone}; +use tablepro_core::edit::{parse_timestamp_with_offset, temporal_parts}; use super::RowDetailPane; use super::value_editor::Commit; use crate::application::component_matches; -/// The day a value's text starts with, when it starts with one the -/// calendar can show. +// GtkCalendar shows the years 1 to 9999 only, so it opens on today for any other. pub fn day_of(text: &str) -> Option { - let head = text.get(..10)?; - NaiveDate::parse_from_str(head, "%Y-%m-%d") - .ok() - .filter(|day| (1..=9999).contains(&day.year())) + temporal_parts(text).date.filter(|day| (1..=9999).contains(&day.year())) } -/// The value's text with `day` picked: a date, or a timestamp in `zone`, -/// which keeps the time it had. A new zoned timestamp starts the day at -/// `offset`, as one without an offset is refused. +// Only the day changes, and the era with it, since a picked day is AD. Text with no day takes one +// before its clock, or becomes a new value; a zoned timestamp lacking an offset takes `offset`. pub fn with_day(text: &str, day: NaiveDate, zone: Option<&TemporalZone>, offset: FixedOffset) -> String { let date = day.format("%Y-%m-%d").to_string(); - match (zone, day_of(text)) { - (None, _) => date, - (Some(_), Some(_)) => format!("{date}{}", text.get(10..).unwrap_or_default()), - (Some(TemporalZone::Offset), None) => format!("{date} 00:00:00{offset}"), - (Some(_), None) => format!("{date} 00:00:00"), + let parts = temporal_parts(text); + let mut text = text.to_owned(); + if let Some(era) = parts.era { + text.replace_range(era, ""); + } + match (parts.day, parts.clock, zone) { + (Some(old), _, _) => { + text.replace_range(old, &date); + text + } + (None, Some(clock), Some(zone)) => { + text.insert_str(clock.start, &format!("{date} ")); + let (body, tail) = text.split_at(text.trim_end().len()); + let zoned = format!("{body}{offset}{tail}"); + match zone == &TemporalZone::Offset + && parse_timestamp_with_offset(&text).is_err() + && parse_timestamp_with_offset(&zoned).is_ok() + { + true => zoned, + false => text, + } + } + (None, _, None) => date, + (None, None, Some(TemporalZone::Offset)) => format!("{date} 00:00:00{offset}"), + (None, None, Some(_)) => format!("{date} 00:00:00"), } } @@ -36,9 +52,12 @@ pub fn day_start_offset(zone: &Tz, day: NaiveDate) -> FixedOffset // A wall-clock time a clock change skips takes the offset before the change. pub fn offset_at(zone: &Tz, local: NaiveDateTime) -> FixedOffset { - [local, local - TimeDelta::hours(1)] - .iter() - .find_map(|local| zone.from_local_datetime(local).earliest()) + zone.from_local_datetime(&local) + .earliest() + .or_else(|| { + let before = local.checked_sub_signed(TimeDelta::hours(1))?; + zone.from_local_datetime(&before).earliest() + }) .map_or_else(|| zone.offset_from_utc_datetime(&local).fix(), |at| at.offset().fix()) } @@ -145,7 +164,12 @@ impl RowDetailPane { ColumnKind::Timestamp { zone, .. } => Some(zone.clone()), _ => None, }); - let text = with_day(&self.typed_text(), day, zone.as_ref(), day_start_offset(&Local, day)); + let typed = self.typed_text(); + let offset = temporal_parts(&typed).time.map_or_else( + || day_start_offset(&Local, day), + |time| offset_at(&Local, day.and_time(time)), + ); + let text = with_day(&typed, day, zone.as_ref(), offset); imp.calendar_popover.popdown(); imp.value_buffer.set_text(&text); self.commit(Commit::Keep); diff --git a/linux/crates/app/src/ui/data_grid/row_detail/clock.rs b/linux/crates/app/src/ui/data_grid/row_detail/clock.rs index 02e36ac66f..b3c1b8b507 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/clock.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/clock.rs @@ -1,10 +1,9 @@ -use std::ops::Range; - use chrono::{Local, NaiveDate, NaiveTime, TimeZone, Timelike}; use gtk4::prelude::*; use gtk4::subclass::prelude::*; use gtk4::{self as gtk, glib}; use tablepro_core::column::{ColumnKind, TemporalZone, TimeRange}; +use tablepro_core::edit::temporal_parts; use super::RowDetailPane; use super::calendar::offset_at; @@ -37,72 +36,26 @@ impl ClockField { } } -// By the text's own form, as chrono reads it for the commit: SQLite keeps any date or time text in a -// DATE, TIME or DATETIME column, and chrono takes whitespace before a number and one digit for two. -fn clock_span(text: &str) -> Option> { - let after_day = day_len(text).map_or(0, |day| day + usize::from(text.as_bytes().get(day) == Some(&b'T'))); - let start = after_day + spaces(text.get(after_day..)?); - let minute = number(text, literal(text, number(text, start, 2)?, b':')?, 2)?; - let end = literal(text, minute, b':').and_then(|second| number(text, second, 2)); - Some(start..end.unwrap_or(minute)) -} - -// A year may run past 9999, as PostgreSQL writes one, and carry a sign. -fn day_len(text: &str) -> Option { - let lead = spaces(text); - let year = lead + usize::from(matches!(text.as_bytes().get(lead), Some(b'-' | b'+'))); - let month = number(text, literal(text, number(text, year, usize::MAX)?, b'-')?, 2)?; - number(text, literal(text, month, b'-')?, 2) -} - -// The end of the number at `at`, past any whitespace before it. -fn number(text: &str, at: usize, most: usize) -> Option { - let start = at + spaces(text.get(at..)?); - let run = text - .get(start..)? - .bytes() - .take(most) - .take_while(u8::is_ascii_digit) - .count(); - (run > 0).then_some(start + run) -} - -fn literal(text: &str, at: usize, byte: u8) -> Option { - (text.as_bytes().get(at) == Some(&byte)).then_some(at + 1) -} - -fn spaces(text: &str) -> usize { - text.len() - text.trim_start().len() -} - pub fn time_of(text: &str) -> Option { - let mut parts = text - .get(clock_span(text)?)? - .split(':') - .map(|part| part.trim().parse::()); - let (hour, minute) = (parts.next()?.ok()?, parts.next()?.ok()?); - let second = parts.next().transpose().ok()?.unwrap_or(0); - NaiveTime::from_hms_opt(hour, minute, second) + temporal_parts(text).time } // Only the clock changes; text with no day or clock, such as NULL or infinity, becomes a new value. pub fn with_time(text: &str, time: NaiveTime, field: ClockField, today: NaiveDate, zone: &Tz) -> String { let mut clock = time.format("%H:%M:%S").to_string(); + let parts = temporal_parts(text); let mut text = text.to_owned(); - if let Some(span) = clock_span(&text) { + if let Some(span) = parts.clock { text.replace_range(span, &clock); return text; } - let day_end = day_len(&text); - let day = day_end - .and_then(|end| NaiveDate::parse_from_str(text.get(..end)?.trim_start(), "%Y-%m-%d").ok()) - .unwrap_or(today); if field.with_offset { + let day = parts.date.unwrap_or(today); clock.push_str(&offset_at(zone, day.and_time(time)).to_string()); } - match day_end { - Some(end) => { - text.insert_str(end, &format!(" {clock}")); + match parts.day { + Some(day) => { + text.insert_str(day.end, &format!(" {clock}")); text } None if field.dated => format!("{} {clock}", today.format("%Y-%m-%d")), diff --git a/linux/crates/app/src/ui/data_grid/row_detail/tests.rs b/linux/crates/app/src/ui/data_grid/row_detail/tests.rs index f9a49e542f..8b02914e82 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/tests.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/tests.rs @@ -620,6 +620,50 @@ fn a_picked_day_keeps_a_timestamp_time() { assert_eq!(day_of("2024-02-2é"), None, "a cut inside a character"); } +#[test] +fn a_picked_day_keeps_the_time_of_any_timestamp_the_commit_reads() { + let day = NaiveDate::from_ymd_opt(2024, 3, 1).expect("a day"); + let plus_two = chrono::FixedOffset::east_opt(2 * 3600).expect("an offset"); + let pick = |text: &str| with_day(text, day, Some(&TemporalZone::Naive), plus_two); + + assert_eq!( + pick("12024-01-05 10:00:00"), + "2024-03-01 10:00:00", + "PostgreSQL writes a far year in full" + ); + assert_eq!( + pick("2024-1-5 10:00"), + "2024-03-01 10:00", + "SQLite keeps the text as typed" + ); + assert_eq!(pick(" 2024-01-05 10:00"), " 2024-03-01 10:00"); + assert_eq!( + pick("0044-03-15 12:00:00 BC"), + "2024-03-01 12:00:00", + "an AD day kept the BC era" + ); + assert_eq!(pick("10:00"), "2024-03-01 10:00"); + let zoned = |text: &str| with_day(text, day, Some(&TemporalZone::Offset), plus_two); + assert_eq!( + zoned("10:00"), + "2024-03-01 10:00+02:00", + "a zoned timestamp took no offset" + ); + assert_eq!(zoned("10:00+05"), "2024-03-01 10:00+05"); + assert_eq!( + with_day("2024-01-05 10:00", day, None, plus_two), + "2024-03-01 10:00", + "SQLite keeps a timestamp in a DATE column" + ); + assert_eq!(day_of("2024-1-5 10:00"), NaiveDate::from_ymd_opt(2024, 1, 5)); + assert_eq!(day_of(" 2024-01-05"), NaiveDate::from_ymd_opt(2024, 1, 5)); + assert_eq!( + day_of("0044-03-15 12:00:00 BC"), + None, + "the calendar opened on an AD day" + ); +} + /// A zone whose offset moves from `before` to `after` at `change`, in UTC. #[derive(Clone)] struct OneChange { @@ -1077,6 +1121,30 @@ fn a_new_zoned_time_takes_the_offset_of_its_own_moment() { ); } +#[test] +fn a_clock_on_the_first_day_chrono_holds_takes_an_offset() { + let first = NaiveDate::MIN.format("%Y-%m-%d").to_string(); + let today = NaiveDate::from_ymd_opt(2026, 10, 29).expect("a day"); + let plus_two = chrono::FixedOffset::east_opt(2 * 3600).expect("an offset"); + + assert_eq!( + with_time(&first, a_time(0, 30, 0), ZONED, today, &plus_two), + format!("{first} 00:30:00+02:00") + ); +} + +#[test] +fn a_picker_replaces_a_part_out_of_range() { + let day = NaiveDate::from_ymd_opt(2024, 3, 1).expect("a day"); + let plus_two = chrono::FixedOffset::east_opt(2 * 3600).expect("an offset"); + + assert_eq!(set_at_nine("2024-01-01 12:34:99", NAIVE), "2024-01-01 09:05:30"); + assert_eq!( + with_day("2024-02-30 10:00", day, Some(&TemporalZone::Naive), plus_two), + "2024-03-01 10:00" + ); +} + #[test] fn the_clock_reads_the_time_a_value_shows() { assert_eq!(time_of("2024-02-29 23:59:58.125"), Some(a_time(23, 59, 58))); diff --git a/linux/crates/core/src/edit/mod.rs b/linux/crates/core/src/edit/mod.rs index 715989f0f4..bbd1ae4f37 100644 --- a/linux/crates/core/src/edit/mod.rs +++ b/linux/crates/core/src/edit/mod.rs @@ -10,6 +10,7 @@ mod is_unchanged; mod new_draft_cell; mod parse_edit_text; mod parse_literal_text; +mod temporal_parts; pub use cell_input::CellInput; pub use edit_context::EditContext; @@ -22,3 +23,4 @@ pub use parse_edit_text::parse_edit_text; pub use parse_literal_text::{ parse_filter_text, parse_literal_text, parse_time_with_offset, parse_timestamp_with_offset, }; +pub use temporal_parts::{TemporalParts, temporal_parts}; diff --git a/linux/crates/core/src/edit/parse_literal_text.rs b/linux/crates/core/src/edit/parse_literal_text.rs index 3e3ba424ea..9bae8c469f 100644 --- a/linux/crates/core/src/edit/parse_literal_text.rs +++ b/linux/crates/core/src/edit/parse_literal_text.rs @@ -343,7 +343,7 @@ fn parse_timestamp(input: &str) -> Result { /// `+10000` and `-4712`: there is no year 0 between 1 BC and AD 1, so /// 1 BC is chrono's 0. `None` for a ` BC` value with no year to count /// back from. -fn chrono_year(input: &str) -> Option> { +pub(super) fn chrono_year(input: &str) -> Option> { let (dated, before_christ) = match input.strip_suffix(" BC").or_else(|| input.strip_suffix(" bc")) { Some(dated) => (dated, true), None => (input, false), @@ -378,11 +378,7 @@ fn far_temporal( purpose: Purpose, parse: fn(&str) -> Result, ) -> Option> { - let year_digits = input.bytes().take_while(u8::is_ascii_digit).count(); - let (year, rest) = input.split_at(year_digits); - let last_year = u64::try_from(NaiveDate::MAX.year()).ok()?; - let year = year.parse::().ok().filter(|year| *year > last_year)?; - let stand_in = parse(&format!("{}{rest}", 2000 + year % 400)).ok()?; + let stand_in = parse(&far_year_stand_in(input)?).ok()?; if purpose == Purpose::Store && let Err(error) = check_declared_fraction(&stand_in, column.kind.fractional_digits()) { @@ -394,6 +390,15 @@ fn far_temporal( })))) } +// The text with a year past chrono's last moved into its range, keeping the rest as written. +pub(super) fn far_year_stand_in(input: &str) -> Option { + let year_digits = input.bytes().take_while(u8::is_ascii_digit).count(); + let (year, rest) = input.split_at(year_digits); + let last_year = u64::try_from(NaiveDate::MAX.year()).ok()?; + let year = year.parse::().ok().filter(|year| *year > last_year)?; + Some(format!("{}{rest}", 2000 + year % 400)) +} + fn infinite_temporal(input: &str) -> Option> { match input.to_ascii_lowercase().as_str() { "infinity" | "inf" => Some(Temporal::Infinity), diff --git a/linux/crates/core/src/edit/temporal_parts.rs b/linux/crates/core/src/edit/temporal_parts.rs new file mode 100644 index 0000000000..8f0588900f --- /dev/null +++ b/linux/crates/core/src/edit/temporal_parts.rs @@ -0,0 +1,162 @@ +use std::ops::Range; + +use chrono::{NaiveDate, NaiveTime}; + +use super::parse_literal_text::{chrono_year, far_year_stand_in}; + +// Byte ranges in the text, so a picker can replace one part and keep the rest as written. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct TemporalParts { + pub day: Option>, + // None for a day out of range or a year past the last chrono holds. + pub date: Option, + pub clock: Option>, + // None for a clock out of range, PostgreSQL's 24:00:00 among them. + pub time: Option, + pub era: Option>, +} + +// The day, clock and ` BC` a date or time text spells, read as `parse_literal_text` reads them: +// the same year forms, a space or `T` between day and clock, seconds optional. +pub fn temporal_parts(text: &str) -> TemporalParts { + // A 1 for every digit keeps the shape and drops the values, so a part out of range still has + // its place for a picker to replace. + let shape: String = text + .chars() + .map(|char| if char.is_ascii_digit() { '1' } else { char }) + .collect(); + let mut parts = spans(&shape); + let era = parts.era.clone().and_then(|era| text.get(era)).unwrap_or_default(); + parts.date = parts.day.clone().and_then(|day| { + let day = chrono_year(&format!("{}{era}", text.get(day)?.trim_start()))?.into_owned(); + NaiveDate::parse_from_str(&day, "%Y-%m-%d").ok() + }); + parts.time = parts.clock.clone().and_then(|clock| { + let clock = text.get(clock)?; + ["%H:%M:%S", "%H:%M"] + .into_iter() + .find_map(|format| NaiveTime::parse_from_str(clock, format).ok()) + }); + parts +} + +fn spans(text: &str) -> TemporalParts { + let start = text.len() - text.trim_start().len(); + let trimmed = text.trim(); + let written = trimmed + .strip_suffix(" BC") + .or_else(|| trimmed.strip_suffix(" bc")) + .unwrap_or(trimmed); + let Some(read) = chrono_year(trimmed) else { + return TemporalParts::default(); + }; + // `read` differs from the text only in its year and era, so a suffix of it is one of the text. + let end = start + written.len(); + let at = |rest: &str| end - rest.len(); + let mut parts = TemporalParts { + era: (written.len() < trimmed.len()).then_some(end..start + trimmed.len()), + ..TemporalParts::default() + }; + let mut clock_text = read.as_ref(); + if let Some(rest) = day_rest(&read, written) { + let after_t = rest.strip_prefix('T').unwrap_or(rest); + let joined = rest.is_empty() || rest.starts_with(char::is_whitespace) || rest.starts_with('T'); + if joined || parse_clock(after_t).is_some() { + parts.day = Some(start..at(rest)); + clock_text = after_t; + } + } + if let Some((_, rest)) = parse_clock(clock_text) { + parts.clock = Some(at(clock_text.trim_start())..at(rest)); + } + parts +} + +fn day_rest<'a>(read: &'a str, written: &str) -> Option<&'a str> { + if let Ok((_, rest)) = NaiveDate::parse_and_remainder(read, "%Y-%m-%d") { + return Some(rest); + } + let stand_in = far_year_stand_in(written)?; + let (_, rest) = NaiveDate::parse_and_remainder(&stand_in, "%Y-%m-%d").ok()?; + read.get(read.len() - rest.len()..) +} + +fn parse_clock(text: &str) -> Option<(NaiveTime, &str)> { + ["%H:%M:%S", "%H:%M"] + .into_iter() + .find_map(|format| NaiveTime::parse_and_remainder(text, format).ok()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn day(text: &str) -> Option<&str> { + text.get(temporal_parts(text).day?) + } + + fn clock(text: &str) -> Option<&str> { + text.get(temporal_parts(text).clock?) + } + + #[test] + fn the_parts_are_read_as_the_commit_reads_them() { + assert_eq!(day(" 2024-1-5T 9:05:30.25+02 "), Some("2024-1-5")); + assert_eq!(clock(" 2024-1-5T 9:05:30.25+02 "), Some("9:05:30")); + assert_eq!( + clock("2024-01-0512:00"), + Some("12:00"), + "chrono reads no space as a space" + ); + assert_eq!(day("12024-01-05 10:00:00"), Some("12024-01-05")); + assert_eq!(clock("13:45+05:30"), Some("13:45")); + assert_eq!(temporal_parts("13:45").time, NaiveTime::from_hms_opt(13, 45, 0)); + } + + #[test] + fn a_year_chrono_cannot_hold_has_a_day_and_no_date() { + let parts = temporal_parts("5874897-12-31 23:59:59"); + + assert_eq!(parts.day, Some(0..13)); + assert_eq!(parts.date, None); + assert_eq!(parts.clock, Some(14..22)); + } + + #[test] + fn a_bc_value_keeps_its_era_apart() { + let text = "0044-03-15 12:00:00 BC"; + let parts = temporal_parts(text); + + assert_eq!(day(text), Some("0044-03-15")); + assert_eq!(parts.date, NaiveDate::from_ymd_opt(-43, 3, 15)); + assert_eq!(clock(text), Some("12:00:00")); + assert_eq!(parts.era.and_then(|era| text.get(era)), Some(" BC")); + } + + #[test] + fn the_end_of_the_day_is_a_clock_with_no_time() { + let parts = temporal_parts("24:00:00+01"); + + assert_eq!(parts.clock, Some(0..8)); + assert_eq!(parts.time, None); + assert_eq!(temporal_parts("24:30:00").clock, Some(0..8)); + } + + #[test] + fn text_the_commit_refuses_has_no_parts() { + assert_eq!(temporal_parts("2024-02-2é 10:00:00").day, None); + assert_eq!(temporal_parts("infinity"), TemporalParts::default()); + assert_eq!(temporal_parts(""), TemporalParts::default()); + } + + #[test] + fn a_part_out_of_range_has_its_place_and_no_value() { + let text = "2024-02-30 12:34:99"; + let parts = temporal_parts(text); + + assert_eq!(day(text), Some("2024-02-30")); + assert_eq!(parts.date, None); + assert_eq!(clock(text), Some("12:34:99")); + assert_eq!(parts.time, None); + } +} From fe1c22c2b63d4e0937b6cbcf4007067db18d72a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 12:23:12 +0700 Subject: [PATCH 17/85] fix(data-grid): mount accessibility cells for rows a read prepared before they came on screen (#3891) --- .claude/rules/data-grid.md | 2 +- .../DataGridAccessibilityCellView.swift | 27 +++- .../Views/DataGrid/DataGridTableView.swift | 10 +- .../DataGridViewController+Changes.swift | 1 - .../DataGrid/DataGridViewController.swift | 3 +- .../DataGridAccessibilityMountTests.swift | 139 ++++++++++++++++++ 6 files changed, 170 insertions(+), 12 deletions(-) create mode 100644 apple/TableProTests/Views/DataGrid/DataGridAccessibilityMountTests.swift diff --git a/.claude/rules/data-grid.md b/.claude/rules/data-grid.md index ef60182664..e86d304b26 100644 --- a/.claude/rules/data-grid.md +++ b/.claude/rules/data-grid.md @@ -66,4 +66,4 @@ Three layers, knowledge pointing one way: an owner's adapter, then `DataGridMode - **AppKit builds the AXCell tree from cell views only**, so while `DataGridAccessibility.isActive` the main table mounts a `DataGridAccessibilityCellView` (identifier `data-grid-cell`) for each on-screen cell. It draws nothing and takes no clicks. Keep that path working whenever row drawing changes. - **A label reads "Row N, column M: value"**: N is the row number on screen, page offset included, and M the display position. UI tests find cells by it (`.claude/rules/ui-tests.md`, The grid). - **The frozen pane is hidden from accessibility.** A frozen column's stand-in in the main table reports the frame the frozen pane shows it at. -- **Only the viewport mounts.** Mounting for every row starved the main thread behind a 1,000-row page (#2392); rows scrolled into view remount. +- **Only the viewport mounts.** Mounting for every row starved the main thread behind a 1,000-row page (#2392). A client's read prepares the rows it walks, off-screen ones without a stand-in, and AppKit never asks for a prepared row again. So each on-screen row still without one remounts after a scroll, a layout of the grid and a read of the table: a fitted grid grows to its rows, and a page moves a grid, with no scroll. diff --git a/apple/TablePro/Views/DataGrid/DataGridAccessibilityCellView.swift b/apple/TablePro/Views/DataGrid/DataGridAccessibilityCellView.swift index 71e665d3b0..c74cde2a24 100644 --- a/apple/TablePro/Views/DataGrid/DataGridAccessibilityCellView.swift +++ b/apple/TablePro/Views/DataGrid/DataGridAccessibilityCellView.swift @@ -156,19 +156,32 @@ extension DataGridViewController { return cell } - // A row prepared off screen was answered with no view and is never asked again, so revealed rows reload. + // A row prepared off screen was answered with no view and is never asked again, so an on-screen row without + // its stand-in reloads. Asked of the rows themselves: a client's read prepares rows the grid never showed. func remountAccessibilityCells(force: Bool = false) { guard DataGridAccessibility.isActive, !isEditingCell else { return } let table = main.tableView let visible = table.rows(in: table.visibleRect) - guard visible.length > 0, force || !NSEqualRanges(visible, accessibilityMountedRows) else { return } let upperBound = min(visible.location + visible.length, table.numberOfRows) guard visible.location >= 0, upperBound > visible.location, table.numberOfColumns > 0 else { return } - accessibilityMountedRows = visible - table.reloadData( - forRowIndexes: IndexSet(integersIn: visible.location.. [Any]? { guard paneKind == .main else { return [] } - DataGridAccessibility.markActive() + noteAccessibilityRead() return super.accessibilityChildren() } override func accessibilityCell(forColumn column: Int, row: Int) -> Any? { guard paneKind == .main else { return nil } - DataGridAccessibility.markActive() + noteAccessibilityRead() return view(atColumn: column, row: row, makeIfNecessary: false) as? DataGridAccessibilityCellView ?? super.accessibilityCell(forColumn: column, row: row) } + // A read prepares every row it walks, off screen ones without a stand-in, and some reach the screen with no scroll. + private func noteAccessibilityRead() { + DataGridAccessibility.markActive() + controller?.scheduleAccessibilityRemount() + } + override func accessibilitySelectedCells() -> [Any]? { guard paneKind == .main else { return [] } return controller?.accessibilitySelectedCells() ?? super.accessibilitySelectedCells() diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift index 9553566c2e..ec17de9e21 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift @@ -54,7 +54,6 @@ extension DataGridViewController { for pane in panes { pane.tableView.reloadData() } - accessibilityMountedRows = NSRange(location: 0, length: 0) drawnSelection = model.selection // A view-based reloadData() drops the selection without telling anyone (probe P2), so it is written back. mirrorSelection() diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController.swift b/apple/TablePro/Views/DataGrid/DataGridViewController.swift index ea5af37b7b..dd7176f7af 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController.swift @@ -29,7 +29,7 @@ final class DataGridViewController: NSViewController, DataGridMountedView { var knownWidths: [DataGridColumnID: CGFloat] = [:] var knownWidthsSawRows = false var savedWidthIDs: Set = [] - var accessibilityMountedRows = NSRange(location: 0, length: 0) + var isAccessibilityRemountScheduled = false var needsViewportRestore = false var pendingFocus = false var lastDataGridSettings = AppSettingsManager.shared.dataGrid @@ -159,6 +159,7 @@ final class DataGridViewController: NSViewController, DataGridMountedView { applyStagedViewport() } updateFittingHeight() + scheduleAccessibilityRemount() if pendingPageReveal != nil { // The page scrolls once its own layout is done, so its document already holds the grid's new height. DispatchQueue.main.async { [weak self] in self?.performPageReveal() } diff --git a/apple/TableProTests/Views/DataGrid/DataGridAccessibilityMountTests.swift b/apple/TableProTests/Views/DataGrid/DataGridAccessibilityMountTests.swift new file mode 100644 index 0000000000..411c6357ad --- /dev/null +++ b/apple/TableProTests/Views/DataGrid/DataGridAccessibilityMountTests.swift @@ -0,0 +1,139 @@ +// +// DataGridAccessibilityMountTests.swift +// TableProTests +// + +import AppKit +@testable import TablePro +import TableProTestSupport +import Testing + +@MainActor +private final class FlippedPage: NSView { + override var isFlipped: Bool { true } +} + +// A fitsRows grid on a page that scrolls, placed by hand the way the Definition page places it. +@MainActor +private final class FittedGridOnPage { + let grid: HostedDataGrid + private let page = NSScrollView(frame: NSRect(x: 0, y: 0, width: 800, height: 400)) + private let document = FlippedPage(frame: .zero) + + init(rows: TableRows, top: CGFloat, height: CGFloat) { + var configuration = DataGridConfiguration() + configuration.fitsRows = true + grid = HostedDataGrid(rows: rows, configuration: configuration) + page.hasVerticalScroller = true + page.documentView = document + grid.window.contentView = page + let gridView = grid.controller.view + gridView.removeFromSuperview() + gridView.autoresizingMask = [] + document.addSubview(gridView) + place(top: top, height: height) + // HostedDataGrid first laid the grid out in a plain window; a reload starts it over where it now sits. + grid.model.reloadRows() + grid.layout() + } + + var fittedHeight: CGFloat { grid.controller.measuredFittingHeight } + + func place(top: CGFloat, height: CGFloat) { + document.frame = NSRect(x: 0, y: 0, width: page.bounds.width, height: max(page.bounds.height, top + height)) + grid.controller.view.frame = NSRect(x: 0, y: top, width: page.bounds.width, height: height) + grid.layout() + } + + // What an assistive client does on its first look: down to each cell's children, which prepares the cell's row. + func readEveryRow() { + let rows = grid.main.accessibilityAttributeValue(.children) as? [NSObject] ?? [] + for row in rows { + let cells = row.accessibilityAttributeValue(.children) as? [NSObject] ?? [] + for cell in cells { + _ = cell.accessibilityAttributeValue(.children) + } + } + } + + var preparedRows: IndexSet { + IndexSet((0.. Date: Fri, 30 Oct 2026 09:51:26 +0100 Subject: [PATCH 18/85] feat(linux): switch to a recently shown tab with Ctrl+E (#3883) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3883.added.1.md | 1 + linux/changelog.d/3883.added.md | 1 + .../app/src/application/accelerators.rs | 4 + .../app/src/test_support/key_injector.rs | 30 ++ linux/crates/app/src/ui/workspace/mod.rs | 1 + .../app/src/ui/workspace/recent_tabs.rs | 503 ++++++++++++++++++ .../app/src/ui/workspace/tests/shell.rs | 27 + .../app/src/ui/workspace/workspace_actions.rs | 39 +- .../app/src/ui/workspace/workspace_shell.rs | 6 + .../src/ui/workspace/workspace_shell_imp.rs | 4 + linux/data/resources/shortcuts-dialog.ui | 13 + linux/po/POTFILES.in | 1 + 12 files changed, 628 insertions(+), 2 deletions(-) create mode 100644 linux/changelog.d/3883.added.1.md create mode 100644 linux/changelog.d/3883.added.md create mode 100644 linux/crates/app/src/ui/workspace/recent_tabs.rs diff --git a/linux/changelog.d/3883.added.1.md b/linux/changelog.d/3883.added.1.md new file mode 100644 index 0000000000..84654e9141 --- /dev/null +++ b/linux/changelog.d/3883.added.1.md @@ -0,0 +1 @@ +Ctrl+Shift+O opens the overview of the window's tabs, where typing searches them. diff --git a/linux/changelog.d/3883.added.md b/linux/changelog.d/3883.added.md new file mode 100644 index 0000000000..2d9810e22f --- /dev/null +++ b/linux/changelog.d/3883.added.md @@ -0,0 +1 @@ +Ctrl+E goes back to the tab shown before; hold Ctrl and press E again to go further back. diff --git a/linux/crates/app/src/application/accelerators.rs b/linux/crates/app/src/application/accelerators.rs index bd9a9bcc61..cf68840238 100644 --- a/linux/crates/app/src/application/accelerators.rs +++ b/linux/crates/app/src/application/accelerators.rs @@ -83,6 +83,10 @@ pub const ACCELERATORS: &[Accelerator] = &[ application("win.new-editor-tab", &["t"], Tabs), application("win.close-current", &["w"], Tabs), application("win.reopen-closed-tab", &["t"], Tabs), + // Ctrl+Tab stays the tab view's next tab, as across GNOME. + application("win.switch-recent-tab", &["e"], Tabs), + // As in GNOME Web and Console. + application("win.show-tabs", &["o"], Tabs), // AdwTabView's own controller, which runs in the capture phase. The // workspace removes its Ctrl+Home and Ctrl+End pairs, so the lists // and the editor inside the tabs keep those keys. diff --git a/linux/crates/app/src/test_support/key_injector.rs b/linux/crates/app/src/test_support/key_injector.rs index b068cee045..cc52b9a88e 100644 --- a/linux/crates/app/src/test_support/key_injector.rs +++ b/linux/crates/app/src/test_support/key_injector.rs @@ -72,6 +72,23 @@ impl KeyInjector { Ok(()) } + /// Keep `keyval` down until the guard drops, so a failed test does + /// not leave it held for the next one. + pub(crate) fn hold(&self, keyval: gdk::Key) -> Result, KeyInjectionError> { + let keycode = self.keycode(keyval)?; + self.fake(KEY_PRESS_EVENT, keycode)?; + self.connection.flush()?; + self.connection.get_input_focus()?.reply()?; + Ok(Held { keys: self, keycode }) + } + + fn let_go(&self, keycode: u8) -> Result<(), KeyInjectionError> { + self.fake(KEY_RELEASE_EVENT, keycode)?; + self.connection.flush()?; + self.connection.get_input_focus()?.reply()?; + Ok(()) + } + /// Click the primary button `count` times at `at` in `widget`'s own /// coordinates, in the window that has the keyboard. A widget not laid /// out yet sits at the window's corner, so it is refused. @@ -141,6 +158,19 @@ impl KeyInjector { } } +pub(crate) struct Held<'a> { + keys: &'a KeyInjector, + keycode: u8, +} + +impl Drop for Held<'_> { + fn drop(&mut self) { + if let Err(error) = self.keys.let_go(self.keycode) { + tracing::error!(%error, "a held key was not let go"); + } + } +} + fn require_x11(display_type: &str) -> Result<(), KeyInjectionError> { if display_type == X11_DISPLAY { Ok(()) diff --git a/linux/crates/app/src/ui/workspace/mod.rs b/linux/crates/app/src/ui/workspace/mod.rs index e2846f0a4d..200a2f17ad 100644 --- a/linux/crates/app/src/ui/workspace/mod.rs +++ b/linux/crates/app/src/ui/workspace/mod.rs @@ -14,6 +14,7 @@ mod health_banner; mod imp; mod import; mod import_ledger; +mod recent_tabs; mod references; mod saves; mod session_watches; diff --git a/linux/crates/app/src/ui/workspace/recent_tabs.rs b/linux/crates/app/src/ui/workspace/recent_tabs.rs new file mode 100644 index 0000000000..a67b6aee15 --- /dev/null +++ b/linux/crates/app/src/ui/workspace/recent_tabs.rs @@ -0,0 +1,503 @@ +//! Ctrl+E walks back through the tabs in the order they were shown, the +//! tab bar showing where it is. Ctrl+Tab stays the next tab, as in GNOME. + +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use gtk4::{self as gtk, gdk, glib}; +use libadwaita as adw; + +use super::WorkspaceShell; + +const WALK_KEYS: &str = "tablepro-recent-walk-keys"; +const SWITCH_RECENT_TAB: &str = "win.switch-recent-tab"; + +pub(super) struct RecentWalk { + // Frozen at the first press, so the tabs it passes do not reorder it. + order: Vec>, + at: usize, +} + +impl RecentWalk { + fn step(&mut self, view: &adw::TabView) -> Option { + let count = self.order.len(); + (1..count).find_map(|offset| { + let at = (self.at + offset) % count; + let page = self.order[at].upgrade().filter(|page| holds(view, page))?; + self.at = at; + Some(page) + }) + } + + fn start(&self, view: &adw::TabView) -> Option { + self.order + .first() + .and_then(glib::WeakRef::upgrade) + .filter(|page| holds(view, page)) + } +} + +pub(super) struct WalkHooks { + window: glib::WeakRef, + keys: gtk::EventControllerKey, + active: glib::SignalHandlerId, +} + +impl WalkHooks { + pub(super) fn release(self) { + if let Some(window) = self.window.upgrade() { + window.remove_controller(&self.keys); + window.disconnect(self.active); + } + } +} + +impl WorkspaceShell { + pub(super) fn note_shown_tab(&self) { + let imp = self.imp(); + // A walk records only the tab it ends on. + if imp.recent_walk.borrow().is_some() { + return; + } + let Some(page) = imp.tab_view.selected_page() else { + return; + }; + let mut recent = imp.recent_tabs.borrow_mut(); + recent.retain(|held| held.upgrade().is_some_and(|held| held != page)); + recent.insert(0, page.downgrade()); + } + + pub(super) fn recent_pages(&self) -> Vec { + let view = self.imp().tab_view.get(); + let open: Vec = (0..view.n_pages()).map(|at| view.nth_page(at)).collect(); + let mut pages: Vec = self + .imp() + .recent_tabs + .borrow() + .iter() + .filter_map(glib::WeakRef::upgrade) + .filter(|page| open.contains(page)) + .collect(); + for page in open { + if !pages.contains(&page) { + pages.push(page); + } + } + pages + } + + pub(super) fn switch_recent_tab(&self) { + let imp = self.imp(); + if imp.recent_walk.borrow().is_none() { + let order = self.recent_pages().iter().map(ObjectExt::downgrade).collect(); + imp.recent_walk.replace(Some(RecentWalk { order, at: 0 })); + } + let view = imp.tab_view.get(); + let next = imp.recent_walk.borrow_mut().as_mut().and_then(|walk| walk.step(&view)); + if let Some(page) = next { + view.set_selected_page(&page); + } + // No release to wait for, as GNOME Shell's switcher checks. + if !control_held(&view) { + self.end_recent_walk(true); + } + } + + pub(super) fn end_recent_walk(&self, keep: bool) { + let Some(walk) = self.imp().recent_walk.take() else { + return; + }; + let view = self.imp().tab_view.get(); + if !keep && let Some(start) = walk.start(&view) { + view.set_selected_page(&start); + } + self.note_shown_tab(); + } + + pub(super) fn hook_recent_walk(&self, window: &adw::ApplicationWindow) -> WalkHooks { + // On the window and past the event boundary of a dialog or a + // popover, so the release reaches it wherever the focus is. + let keys = gtk::EventControllerKey::new(); + keys.set_name(Some(WALK_KEYS)); + keys.set_propagation_phase(gtk::PropagationPhase::Capture); + keys.set_propagation_limit(gtk::PropagationLimit::None); + let shell = self.downgrade(); + keys.connect_key_pressed(move |controller, keyval, _, _| { + let Some(shell) = shell.upgrade() else { + return glib::Propagation::Proceed; + }; + let walking = shell.imp().recent_walk.borrow().is_some(); + if !walking { + return glib::Propagation::Proceed; + } + if keyval == gdk::Key::Escape { + shell.end_recent_walk(false); + return glib::Propagation::Stop; + } + let Some(event) = controller + .current_event() + .and_then(|event| event.downcast::().ok()) + else { + return glib::Propagation::Proceed; + }; + // GTK's own match, so Ctrl+E steps on a layout without E too. + let steps = crate::application::component_trigger(SWITCH_RECENT_TAB) + .is_some_and(|trigger| trigger.trigger(&event, false) != gdk::KeyMatch::None); + // This controller runs before the window's accelerators, so + // another command acts on the tab the walk reached. + if !event.is_modifier() && !steps { + shell.end_recent_walk(true); + } + glib::Propagation::Proceed + }); + let shell = self.downgrade(); + keys.connect_key_released(move |_, keyval, _, _| { + if matches!(keyval, gdk::Key::Control_L | gdk::Key::Control_R) + && let Some(shell) = shell.upgrade() + { + shell.end_recent_walk(true); + } + }); + window.add_controller(keys.clone()); + // The release goes to another window once this one loses the keyboard. + let shell = self.downgrade(); + let active = window.connect_is_active_notify(move |window| { + if !window.is_active() + && let Some(shell) = shell.upgrade() + { + shell.end_recent_walk(true); + } + }); + WalkHooks { + window: window.downgrade(), + keys, + active, + } + } +} + +fn holds(view: &adw::TabView, page: &adw::TabPage) -> bool { + (0..view.n_pages()).any(|at| &view.nth_page(at) == page) +} + +fn control_held(widget: &impl IsA) -> bool { + widget + .display() + .default_seat() + .and_then(|seat| seat.keyboard()) + .is_some_and(|keyboard| keyboard.modifier_state().contains(gdk::ModifierType::CONTROL_MASK)) +} + +#[cfg(test)] +mod tests { + use gtk4::prelude::*; + use gtk4::subclass::prelude::*; + use libadwaita as adw; + + use crate::ui::workspace::WorkspaceShell; + + fn shell_with_tabs(titles: &[&str]) -> (WorkspaceShell, Vec) { + crate::register_test_resources(); + let shell: WorkspaceShell = gtk4::glib::Object::new(); + let view = shell.tab_view(); + let pages = titles + .iter() + .map(|title| { + let page = view.append(>k4::Label::new(Some(title))); + page.set_title(title); + page + }) + .collect(); + (shell, pages) + } + + fn titles(pages: &[adw::TabPage]) -> Vec { + pages.iter().map(|page| page.title().to_string()).collect() + } + + fn selected(shell: &WorkspaceShell) -> String { + shell + .tab_view() + .selected_page() + .map(|page| page.title().to_string()) + .unwrap_or_default() + } + + fn show(shell: &WorkspaceShell, pages: &[adw::TabPage], order: &[usize]) { + for at in order { + shell.tab_view().set_selected_page(&pages[*at]); + shell.note_shown_tab(); + } + } + + #[gtk4::test] + fn the_tabs_come_most_recently_shown_first() { + let (shell, pages) = shell_with_tabs(&["users", "orders", "query"]); + show(&shell, &pages, &[0, 2, 1]); + + assert_eq!(titles(&shell.recent_pages()), ["orders", "query", "users"]); + } + + #[gtk4::test] + fn a_tab_never_shown_comes_after_the_shown_ones() { + let (shell, pages) = shell_with_tabs(&["users", "orders", "query"]); + show(&shell, &pages, &[2]); + + assert_eq!(titles(&shell.recent_pages()), ["query", "users", "orders"]); + } + + #[gtk4::test] + fn a_closed_tab_leaves_the_order() { + let (shell, pages) = shell_with_tabs(&["users", "orders", "query"]); + show(&shell, &pages, &[0, 1, 2]); + + shell.tab_view().close_page(&pages[1]); + + assert_eq!(titles(&shell.recent_pages()), ["query", "users"]); + } + + #[gtk4::test] + fn without_ctrl_held_each_switch_goes_to_the_tab_shown_before() { + let (shell, pages) = shell_with_tabs(&["users", "orders", "query"]); + show(&shell, &pages, &[0, 1, 2]); + + shell.switch_recent_tab(); + assert_eq!(selected(&shell), "orders"); + assert!(shell.imp().recent_walk.borrow().is_none(), "a walk waits for a release"); + shell.switch_recent_tab(); + assert_eq!(selected(&shell), "query"); + assert_eq!(titles(&shell.recent_pages()), ["query", "orders", "users"]); + } + + #[gtk4::test] + fn a_walk_records_only_the_tab_it_ends_on() { + let (shell, pages) = shell_with_tabs(&["users", "orders", "query"]); + show(&shell, &pages, &[0, 1, 2]); + let imp = shell.imp(); + imp.recent_walk.replace(Some(super::RecentWalk { + order: shell.recent_pages().iter().map(ObjectExt::downgrade).collect(), + at: 0, + })); + + for _ in 0..2 { + let next = imp + .recent_walk + .borrow_mut() + .as_mut() + .and_then(|walk| walk.step(&shell.tab_view())); + shell.tab_view().set_selected_page(&next.expect("a tab to go to")); + shell.note_shown_tab(); + } + assert_eq!(selected(&shell), "users"); + assert_eq!(titles(&shell.recent_pages()), ["query", "orders", "users"]); + + shell.end_recent_walk(true); + assert_eq!(titles(&shell.recent_pages()), ["users", "query", "orders"]); + } + + #[gtk4::test] + fn a_walk_skips_a_tab_closed_on_the_way_and_escape_returns_to_the_start() { + let (shell, pages) = shell_with_tabs(&["users", "orders", "query"]); + show(&shell, &pages, &[0, 1, 2]); + let imp = shell.imp(); + imp.recent_walk.replace(Some(super::RecentWalk { + order: shell.recent_pages().iter().map(ObjectExt::downgrade).collect(), + at: 0, + })); + shell.tab_view().close_page(&pages[1]); + + let next = imp + .recent_walk + .borrow_mut() + .as_mut() + .and_then(|walk| walk.step(&shell.tab_view())); + assert_eq!(next.as_ref(), Some(&pages[0]), "the walk went to a closed tab"); + shell.tab_view().set_selected_page(&pages[0]); + + shell.end_recent_walk(false); + assert_eq!(selected(&shell), "query"); + assert_eq!(titles(&shell.recent_pages()), ["query", "users"]); + } + + #[gtk4::test] + fn the_window_lets_go_of_the_walk_keys_with_the_shell() { + use crate::ui::SessionWindowInput; + use crate::ui::session_window::tests::TestWindow; + let test = TestWindow::connected("RecentWalkKeys"); + let walk_keys = || { + let controllers = test.window.observe_controllers(); + (0..controllers.n_items()) + .filter_map(|at| controllers.item(at).and_downcast::()) + .filter(|controller| controller.name().as_deref() == Some(super::WALK_KEYS)) + .count() + }; + assert_eq!(walk_keys(), 1); + + test.window.send(SessionWindowInput::Disconnect); + test.wait_for_page("picker"); + + assert_eq!(walk_keys(), 0, "the picker kept the walk's keys"); + } + + /// These press real keys on the X display every test shares; nextest + /// runs them with no other test beside them. + mod real_keys { + use gtk4::gdk; + use gtk4::prelude::*; + use gtk4::subclass::prelude::*; + use libadwaita as adw; + use libadwaita::prelude::*; + + use crate::test_support::{KeyInjector, wait_until}; + use crate::ui::session_window::tests::{TestWindow, WAIT}; + use crate::ui::workspace::WorkspaceShell; + use crate::ui::workspace::tests::{open_editor, shell_of}; + + struct Walking { + test: TestWindow, + shell: WorkspaceShell, + pages: Vec, + keys: KeyInjector, + } + + /// Three editor tabs, opened and so shown in order: the recent + /// order is the third, the second, the first. + fn walking(name: &str) -> Walking { + let test = TestWindow::connected(name); + let workspace = test.workspace(); + let shell = shell_of(&workspace); + let pages: Vec = (0..3).map(|_| open_editor(&workspace)).collect(); + assert_eq!( + shell.recent_pages(), + [pages[2].clone(), pages[1].clone(), pages[0].clone()] + ); + KeyInjector::focus_only(&test.window); + wait_until(WAIT, || test.window.is_active()).expect("the window to take the focus"); + let keys = KeyInjector::connect().expect("an X11 display"); + Walking { + test, + shell, + pages, + keys, + } + } + + impl Walking { + fn selected(&self) -> Option { + self.shell.tab_view().selected_page() + } + + fn wait_for(&self, page: usize, what: &str) { + wait_until(WAIT, || self.selected().as_ref() == Some(&self.pages[page])).expect(what); + } + + fn press_e(&self) { + self.keys + .press(gdk::Key::e, gdk::ModifierType::empty()) + .expect("the key press"); + } + + fn is_walking(&self) -> bool { + self.shell.imp().recent_walk.borrow().is_some() + } + } + + #[gtk4::test] + fn holding_ctrl_walks_back_and_letting_go_keeps_the_tab() { + let walk = walking("RecentWalkKeep"); + { + let _ctrl = walk.keys.hold(gdk::Key::Control_L).expect("Ctrl held"); + wait_until(WAIT, || super::super::control_held(&walk.test.window)).expect("GDK to see Ctrl down"); + walk.press_e(); + walk.wait_for(1, "Ctrl+E to show the tab shown before"); + walk.press_e(); + walk.wait_for(0, "E again to go one further back"); + assert!(walk.is_walking(), "the walk ended with Ctrl still held"); + } + wait_until(WAIT, || !walk.is_walking()).expect("letting go of Ctrl to end the walk"); + + assert_eq!(walk.selected().as_ref(), Some(&walk.pages[0])); + let order = [walk.pages[0].clone(), walk.pages[2].clone(), walk.pages[1].clone()]; + assert_eq!( + walk.shell.recent_pages(), + order, + "the tabs passed on the way were recorded" + ); + + walk.keys + .press(gdk::Key::e, gdk::ModifierType::CONTROL_MASK) + .expect("the key press"); + walk.wait_for(2, "a tap of Ctrl+E to go back to the tab before"); + } + + #[gtk4::test] + fn another_command_ends_the_walk_on_the_tab_it_reached() { + let walk = walking("RecentWalkCommand"); + let workspace = walk.test.workspace(); + { + let _ctrl = walk.keys.hold(gdk::Key::Control_L).expect("Ctrl held"); + wait_until(WAIT, || super::super::control_held(&walk.test.window)).expect("GDK to see Ctrl down"); + walk.press_e(); + walk.wait_for(1, "Ctrl+E to show the tab shown before"); + walk.keys + .press(gdk::Key::t, gdk::ModifierType::empty()) + .expect("the key press"); + wait_until(WAIT, || { + workspace.imp().index.get().map_or(0, |index| index.pages().len()) == 4 + }) + .expect("Ctrl+T to open a tab"); + assert!(!walk.is_walking(), "the walk went on past Ctrl+T"); + } + + let new = walk.selected().expect("the new tab in front"); + assert!(!walk.pages.contains(&new)); + let order = [new, walk.pages[1].clone(), walk.pages[2].clone(), walk.pages[0].clone()]; + assert_eq!( + walk.shell.recent_pages(), + order, + "the tab the walk reached was not recorded" + ); + } + + #[gtk4::test] + fn letting_go_of_ctrl_in_a_dialog_ends_the_walk() { + let walk = walking("RecentWalkDialog"); + let dialog = adw::AlertDialog::new(Some("Connection Lost"), None); + dialog.add_response("close", "Close"); + { + let _ctrl = walk.keys.hold(gdk::Key::Control_L).expect("Ctrl held"); + wait_until(WAIT, || super::super::control_held(&walk.test.window)).expect("GDK to see Ctrl down"); + walk.press_e(); + walk.wait_for(1, "Ctrl+E to show the tab shown before"); + dialog.present(Some(&walk.test.window)); + wait_until(WAIT, || { + gtk4::prelude::RootExt::focus(&walk.test.window).is_some_and(|focus| focus.is_ancestor(&dialog)) + }) + .expect("the dialog to take the focus"); + } + wait_until(WAIT, || !walk.is_walking()).expect("letting go of Ctrl in the dialog to end the walk"); + + let order = [walk.pages[1].clone(), walk.pages[2].clone(), walk.pages[0].clone()]; + assert_eq!(walk.shell.recent_pages(), order); + dialog.close(); + } + + #[gtk4::test] + fn escape_ends_a_walk_on_the_tab_it_started_on() { + let walk = walking("RecentWalkEscape"); + { + let _ctrl = walk.keys.hold(gdk::Key::Control_L).expect("Ctrl held"); + wait_until(WAIT, || super::super::control_held(&walk.test.window)).expect("GDK to see Ctrl down"); + walk.press_e(); + walk.wait_for(1, "Ctrl+E to show the tab shown before"); + walk.keys + .press(gdk::Key::Escape, gdk::ModifierType::empty()) + .expect("the key press"); + walk.wait_for(2, "Escape to go back to the start"); + assert!(!walk.is_walking(), "Escape left the walk on"); + } + + let order = [walk.pages[2].clone(), walk.pages[1].clone(), walk.pages[0].clone()]; + assert_eq!(walk.shell.recent_pages(), order); + } + } +} diff --git a/linux/crates/app/src/ui/workspace/tests/shell.rs b/linux/crates/app/src/ui/workspace/tests/shell.rs index a266b853f8..8197dfbeb4 100644 --- a/linux/crates/app/src/ui/workspace/tests/shell.rs +++ b/linux/crates/app/src/ui/workspace/tests/shell.rs @@ -142,6 +142,33 @@ fn show_sidebar_action_toggles_split_view() { assert!(split.shows_sidebar()); } +#[gtk4::test] +fn show_tabs_opens_and_closes_the_overview_while_a_tab_is_open() { + let test = TestWindow::connected("ShowTabs"); + let workspace = test.workspace(); + let shell = shell_of(&workspace); + let overview = shell.imp().tab_overview.get(); + let enabled = |name: &str| { + test.window + .lookup_action(name) + .is_some_and(|action| action.is_enabled()) + }; + assert_eq!(shell.tab_view().n_pages(), 0); + assert!(!enabled("show-tabs"), "the overview warns and stays shut with no tab"); + assert!(!enabled("switch-recent-tab")); + + super::open_editor(&workspace); + assert!(enabled("show-tabs")); + assert!(!enabled("switch-recent-tab"), "one tab has no other to go to"); + WidgetExt::activate_action(&test.window, "win.show-tabs", None).expect("win.show-tabs"); + assert!(overview.is_open()); + WidgetExt::activate_action(&test.window, "win.show-tabs", None).expect("win.show-tabs"); + assert!(!overview.is_open()); + + super::open_editor(&workspace); + assert!(enabled("switch-recent-tab")); +} + #[gtk4::test] fn sidebar_toggle_visible_only_when_collapsed_or_hidden() { let test = TestWindow::connected("SidebarToggle"); diff --git a/linux/crates/app/src/ui/workspace/workspace_actions.rs b/linux/crates/app/src/ui/workspace/workspace_actions.rs index d3809c32bf..5a724c5ea9 100644 --- a/linux/crates/app/src/ui/workspace/workspace_actions.rs +++ b/linux/crates/app/src/ui/workspace/workspace_actions.rs @@ -2,11 +2,12 @@ //! them once and each one goes to the session in front, so a change of //! front changes where they go without taking any off the window. Their //! keys are application accelerators; a tab's own keys belong to the tab. -//! Reopen Closed Tab is the shell's own: the tab closed last may be any -//! session's. +//! Reopen Closed Tab, Switch to Recent Tab and View Open Tabs are the +//! shell's own: the tabs they name may be any session's. use gtk4::glib; use gtk4::prelude::*; +use gtk4::subclass::prelude::*; use libadwaita as adw; use super::{WorkspaceInput, WorkspaceShell}; @@ -31,6 +32,8 @@ const ENTRIES: [(&str, ActionInput); 6] = [ ]; const REOPEN_CLOSED_TAB: &str = "reopen-closed-tab"; +const SWITCH_RECENT_TAB: &str = "switch-recent-tab"; +const SHOW_TABS: &str = "show-tabs"; /// Add the actions to the window's own action map, which GTK exports as /// `win`. Each sends its input to the session in front when activated; @@ -59,6 +62,34 @@ pub(super) fn install(window: &adw::ApplicationWindow, shell: &WorkspaceShell) { } }) .build()]); + let switching = shell.downgrade(); + window.add_action_entries([gio::ActionEntry::::builder(SWITCH_RECENT_TAB) + .activate(move |_, _, _| { + if let Some(shell) = switching.upgrade() { + shell.switch_recent_tab(); + } + }) + .build()]); + let overview = shell.imp().tab_overview.downgrade(); + window.add_action_entries([gio::ActionEntry::::builder(SHOW_TABS) + .activate(move |_, _, _| { + if let Some(overview) = overview.upgrade() { + overview.set_open(!overview.is_open()); + } + }) + .build()]); + // The overview refuses to open or close with no tab, and with one tab + // Ctrl+E goes on to the tab. + for (name, least) in [(SWITCH_RECENT_TAB, 2), (SHOW_TABS, 1)] { + if let Some(action) = window.lookup_action(name) { + shell + .tab_view() + .bind_property("n-pages", &action, "enabled") + .transform_to(move |_, pages: i32| Some(pages >= least)) + .sync_create() + .build(); + } + } // Insensitive until the query database has opened or failed to. for name in HISTORY_ACTIONS { set_enabled(window, name, false); @@ -71,6 +102,8 @@ pub(super) fn remove(window: &adw::ApplicationWindow) { window.remove_action(name); } window.remove_action(REOPEN_CLOSED_TAB); + window.remove_action(SWITCH_RECENT_TAB); + window.remove_action(SHOW_TABS); } fn set_enabled(window: &adw::ApplicationWindow, name: &str, enabled: bool) { @@ -148,5 +181,7 @@ mod tests { remove(&window); assert!(window.lookup_action("new-editor-tab").is_none()); assert!(window.lookup_action(REOPEN_CLOSED_TAB).is_none()); + assert!(window.lookup_action(SWITCH_RECENT_TAB).is_none()); + assert!(window.lookup_action(SHOW_TABS).is_none()); } } diff --git a/linux/crates/app/src/ui/workspace/workspace_shell.rs b/linux/crates/app/src/ui/workspace/workspace_shell.rs index 2e42ffca70..c6d16a2802 100644 --- a/linux/crates/app/src/ui/workspace/workspace_shell.rs +++ b/linux/crates/app/src/ui/workspace/workspace_shell.rs @@ -105,6 +105,7 @@ impl WorkspaceShell { )); follow_sidebar(&imp.split_view, &imp.sidebar_toggle); window.add_action(&assistant_action(&shell)); + imp.walk_hooks.replace(Some(shell.hook_recent_walk(window))); imp.tab_view .bind_property("n-pages", &imp.tab_stack.get(), "visible-child-name") @@ -453,6 +454,7 @@ impl WorkspaceShell { let Some(page) = self.imp().tab_view.selected_page() else { return; }; + self.note_shown_tab(); if let Some((owner, _)) = self.owner_of(&page) { self.set_front(Some(&owner)); } @@ -630,6 +632,10 @@ impl WorkspaceShell { } } imp.closed_tabs.borrow_mut().clear(); + imp.recent_walk.take(); + if let Some(hooks) = imp.walk_hooks.take() { + hooks.release(); + } if let Some(window) = imp.window.upgrade() { window.remove_action(SHOW_SIDEBAR); window.remove_action(SHOW_ASSISTANT); diff --git a/linux/crates/app/src/ui/workspace/workspace_shell_imp.rs b/linux/crates/app/src/ui/workspace/workspace_shell_imp.rs index fd19a093bc..b0759fce2d 100644 --- a/linux/crates/app/src/ui/workspace/workspace_shell_imp.rs +++ b/linux/crates/app/src/ui/workspace/workspace_shell_imp.rs @@ -115,6 +115,10 @@ pub struct WorkspaceShell { pub(super) menu_target: RefCell>, /// The tabs closed last, the newest at the back. pub(super) closed_tabs: RefCell>, + /// The tabs the window showed, the latest first. + pub(super) recent_tabs: RefCell>>, + pub(super) recent_walk: RefCell>, + pub(super) walk_hooks: RefCell>, /// Flips the window's history actions as the query database opens. pub(super) history_gate: RefCell>>, /// What follows the session in front, let go of at shutdown. diff --git a/linux/data/resources/shortcuts-dialog.ui b/linux/data/resources/shortcuts-dialog.ui index 63bef131a5..083348479f 100644 --- a/linux/data/resources/shortcuts-dialog.ui +++ b/linux/data/resources/shortcuts-dialog.ui @@ -86,6 +86,19 @@ win.reopen-closed-tab + + + Switch to Recent Tab + Hold Ctrl and press E again to go further back + win.switch-recent-tab + + + + + View Open Tabs + win.show-tabs + + Next Tab diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index ba56cfe121..5fab1859c7 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -554,6 +554,7 @@ crates/app/src/ui/workspace/imp.rs crates/app/src/ui/workspace/import.rs crates/app/src/ui/workspace/import_ledger.rs crates/app/src/ui/workspace/mod.rs +crates/app/src/ui/workspace/recent_tabs.rs crates/app/src/ui/workspace/references.rs crates/app/src/ui/workspace/saves.rs crates/app/src/ui/workspace/session_watches.rs From e33aed5465361f873495497d6ecd4c163c8e662b Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 10:19:09 +0100 Subject: [PATCH 19/85] feat(linux-grid): show a binary value's bytes as a hex dump and an image as a picture in Row Details (#3882) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- .github/workflows/build-linux.yml | 9 +- .github/workflows/release-linux.yml | 2 +- .oss-scanner/Dockerfile | 3 +- .oss-scanner/threat_model.md | 2 +- linux/Cargo.lock | 361 +++++++- linux/Cargo.toml | 2 + linux/README.md | 18 +- linux/build-aux/debian/control | 4 + linux/build-aux/debian/copyright | 772 ++++++++++++++++- linux/build-aux/rpm/tablepro.spec | 7 + linux/changelog.d/3882.added.md | 1 + linux/crates/app/Cargo.toml | 1 + .../app/src/services/grid/display_text.rs | 16 +- .../crates/app/src/services/grid/hex_dump.rs | 93 +++ linux/crates/app/src/services/grid/mod.rs | 2 + .../app/src/ui/data_grid/row_detail/imp.rs | 9 + .../app/src/ui/data_grid/row_detail/mod.rs | 1 + .../src/ui/data_grid/row_detail/picture.rs | 117 +++ .../app/src/ui/data_grid/row_detail/tests.rs | 132 +++ .../ui/data_grid/row_detail/value_editor.rs | 24 +- linux/data/licenses/THIRD-PARTY-NOTICE.md | 785 ++++++++++++++++++ linux/data/licenses/third-party.json | 170 +++- linux/data/resources/ui/row-detail-pane.ui | 12 +- .../decisions/0029-images-decode-in-glycin.md | 32 + linux/docs/decisions/README.md | 1 + linux/meson.build | 3 + linux/po/POTFILES.in | 2 + 27 files changed, 2527 insertions(+), 54 deletions(-) create mode 100644 linux/changelog.d/3882.added.md create mode 100644 linux/crates/app/src/services/grid/hex_dump.rs create mode 100644 linux/crates/app/src/ui/data_grid/row_detail/picture.rs create mode 100644 linux/docs/decisions/0029-images-decode-in-glycin.md diff --git a/.github/workflows/build-linux.yml b/.github/workflows/build-linux.yml index 19c94df8fd..77ded5c235 100644 --- a/.github/workflows/build-linux.yml +++ b/.github/workflows/build-linux.yml @@ -82,7 +82,7 @@ jobs: # install, then the meson toolchain, the -dev packages the # workspace links, the runtime pieces the test suites need (xvfb, # dbus, gnome-keyring for the Secret Service step, openssh-client - # for the SSH transport). + # for the SSH transport, glycin-loaders for a value's picture). run: | apt-get install -y --no-install-recommends \ build-essential \ @@ -100,9 +100,12 @@ jobs: libgtksourceview-5-dev \ libsqlite3-dev \ libkrb5-dev \ + liblcms2-dev \ + libseccomp-dev \ clang \ libclang-dev \ adwaita-icon-theme \ + glycin-loaders \ locales \ gnome-keyring \ openssh-client \ @@ -334,7 +337,7 @@ jobs: build-essential pkgconf meson ninja-build gettext \ desktop-file-utils appstream libglib2.0-bin libglib2.0-dev libxml2-utils \ libgtk-4-dev libadwaita-1-dev libgtksourceview-5-dev \ - libsqlite3-dev libkrb5-dev clang libclang-dev cargo rustc + libsqlite3-dev libkrb5-dev liblcms2-dev libseccomp-dev clang libclang-dev cargo rustc - name: POTFILES.in is current run: | git ls-files 'crates/app/src/*.rs' 'data/resources/*.ui' 'data/*.desktop.in.in' \ @@ -626,6 +629,8 @@ jobs: libgtksourceview-5-dev \ libsqlite3-dev \ libkrb5-dev \ + liblcms2-dev \ + libseccomp-dev \ clang \ libclang-dev \ xvfb \ diff --git a/.github/workflows/release-linux.yml b/.github/workflows/release-linux.yml index 416e184d6f..83a6ed42d8 100644 --- a/.github/workflows/release-linux.yml +++ b/.github/workflows/release-linux.yml @@ -115,7 +115,7 @@ jobs: build-essential pkgconf meson ninja-build gettext \ desktop-file-utils appstream libglib2.0-bin libglib2.0-dev libxml2-utils \ libgtk-4-dev libadwaita-1-dev libgtksourceview-5-dev \ - libsqlite3-dev libkrb5-dev clang libclang-dev cargo rustc + libsqlite3-dev libkrb5-dev liblcms2-dev libseccomp-dev clang libclang-dev cargo rustc working-directory: / - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: diff --git a/.oss-scanner/Dockerfile b/.oss-scanner/Dockerfile index 88b83578d0..09a6864eef 100644 --- a/.oss-scanner/Dockerfile +++ b/.oss-scanner/Dockerfile @@ -12,7 +12,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ ca-certificates curl git \ build-essential pkgconf meson ninja-build gettext desktop-file-utils appstream libglib2.0-bin \ libxml2-utils libgtk-4-dev libadwaita-1-dev libgtksourceview-5-dev libsqlite3-dev libkrb5-dev \ - clang libclang-dev adwaita-icon-theme locales gnome-keyring openssh-client xvfb xauth dbus-daemon \ + liblcms2-dev libseccomp-dev clang libclang-dev adwaita-icon-theme glycin-loaders locales gnome-keyring \ + openssh-client xvfb xauth dbus-daemon \ && rm -rf /var/lib/apt/lists/* \ && localedef -i en_US -f UTF-8 en_US.UTF-8 diff --git a/.oss-scanner/threat_model.md b/.oss-scanner/threat_model.md index 9e67d73ebe..4fe2a19276 100644 --- a/.oss-scanner/threat_model.md +++ b/.oss-scanner/threat_model.md @@ -32,7 +32,7 @@ The user is trusted: the SQL they type, the connections they create, the plugins 7. Secrets: `apple/TablePro/Core/Storage/KeychainHelper.swift`, `apple/TablePro/Core/Sync/SyncRecordMapper.swift` (passwords must never reach CloudKit), `linux/crates/storage/src/secrets`, `TableProLogRedaction`, the MCP error redactor. 8. SQL the app builds from data: `apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift`, `apple/TablePro/Core/Database/FilterSQLGenerator.swift`, `SQLEscaping.swift`, `DialectQuoteHelper.swift`, each driver's `quoteIdentifier`; on Linux `linux/crates/core/src/dml`, `linux/crates/core/src/filter/validate_raw_predicate.rs` and each driver's `dialect.rs`. A crafted table or column name that escapes quoting runs SQL with the user's privileges. 9. SSH and TLS: `apple/TablePro/Core/SSH/` (libssh2, host key prompts), `apple/Packages/TableProMacCore/Sources/TableProSSHConfig`, `linux/crates/ssh` (drives the system `ssh`, so watch for option injection from connection fields), `linux/crates/net/src/tls`. -10. Rendering of cell values: the out-of-process image renderer (`apple/TablePro/CLI/ImageRenderMain.swift`), `apple/TablePro/Core/Services/Formatting/PhpSerializeParser.swift`, the MongoDB shell's JavaScriptCore runtime. +10. Rendering of cell values: the out-of-process image renderer (`apple/TablePro/CLI/ImageRenderMain.swift`), the Linux Row Details picture, which glycin decodes in a sandbox (`linux/crates/app/src/ui/data_grid/row_detail/picture.rs`), `apple/TablePro/Core/Services/Formatting/PhpSerializeParser.swift`, the MongoDB shell's JavaScriptCore runtime. Matter least: layout, theming and localization code, `linux/crates/test-fixtures`, `linux/crates/test-support`, `apple/TableProUITests`, `scripts/`, `.github/`, docs. diff --git a/linux/Cargo.lock b/linux/Cargo.lock index bacb9da7e7..bfbb57eb5d 100644 --- a/linux/Cargo.lock +++ b/linux/Cargo.lock @@ -323,6 +323,18 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "async-channel" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + [[package]] name = "async-lock" version = "3.4.2" @@ -367,6 +379,12 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "async-task" +version = "4.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" + [[package]] name = "async-trait" version = "0.1.92" @@ -625,6 +643,19 @@ dependencies = [ "generic-array", ] +[[package]] +name = "blocking" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a70e4329df6cb94385eed412ec92375c3cdd8a6e502493d1229b6414e4036dfa" +dependencies = [ + "async-channel", + "async-task", + "futures-io", + "futures-lite", + "piper", +] + [[package]] name = "bnum" version = "0.13.0" @@ -753,6 +784,12 @@ version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "bytemuck" +version = "1.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797" + [[package]] name = "byteorder" version = "1.5.0" @@ -984,6 +1021,15 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "connection-string" version = "0.2.0" @@ -1126,6 +1172,16 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + [[package]] name = "crossbeam-epoch" version = "0.9.21" @@ -1505,6 +1561,15 @@ dependencies = [ "syn 3.0.5", ] +[[package]] +name = "dlib" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab8ecd87370524b461f8557c119c405552c396ed91fc0a8eec68679eab26f94a" +dependencies = [ + "libloading", +] + [[package]] name = "docker_credential" version = "1.4.0" @@ -1602,6 +1667,27 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "env_filter" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900d271a03799a1ee8d1ca9b19893b48ca674a9284fefcfb85f05e74ed314217" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de671bd27a75a797dc9ae289ba1e77276e75e2026408aab65185384e2d5cd3f6" +dependencies = [ + "env_filter", + "jiff", + "log", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -1770,6 +1856,33 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5190182e6915eb873ddbc16e23b711b6eb1f9c00a0d0a3a91b5f6228475225" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "foreign-types-shared" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" + [[package]] name = "form_urlencoded" version = "1.2.2" @@ -2164,6 +2277,84 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" +[[package]] +name = "glycin" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "923674c1f2b1a28f69ebdc0f572672e63874e17ce0e60c96b8bfaff47a8ef4e9" +dependencies = [ + "futures-channel", + "futures-util", + "gdk4", + "gio", + "glib", + "glycin-common", + "glycin-utils", + "gufo-common", + "gufo-exif", + "lcms2", + "lcms2-sys", + "libc", + "libseccomp", + "memfd", + "memmap2", + "nix 0.30.1", + "static_assertions", + "thiserror", + "tokio", + "tokio-stream", + "tracing", + "yeslogic-fontconfig-sys", + "zbus", +] + +[[package]] +name = "glycin-common" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db28cec888051ccbf68f9f352d57ba2a292179863524a0769e54dbb72d152a03" +dependencies = [ + "bitflags 2.13.2", + "gufo-common", + "half", + "memmap2", + "nix 0.30.1", + "paste", + "rmp-serde", + "serde", + "thiserror", + "zerocopy", + "zvariant", +] + +[[package]] +name = "glycin-utils" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b49cd2451bced7416341a5e89d74f547e92b85b67e8f66c8723a7505930a50ab" +dependencies = [ + "async-lock", + "bitflags 2.13.2", + "blocking", + "env_logger", + "futures-util", + "glycin-common", + "gufo-common", + "half", + "libc", + "libseccomp", + "log", + "memmap2", + "nix 0.30.1", + "paste", + "rayon", + "serde", + "thiserror", + "tokio", + "zbus", + "zerocopy", +] + [[package]] name = "gobject-sys" version = "0.22.9" @@ -2279,6 +2470,29 @@ dependencies = [ "system-deps 7.0.8", ] +[[package]] +name = "gufo-common" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f23bc0d0e791259ce4fe703c4d988c64317e33477bc4d1d7526ab8eb9fcaab6" +dependencies = [ + "paste", + "serde", + "thiserror", + "zvariant", +] + +[[package]] +name = "gufo-exif" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad61fccc591119491879e7bee63ebbfbd27811b35367b4728a857119fde78843" +dependencies = [ + "gufo-common", + "thiserror", + "tracing", +] + [[package]] name = "h2" version = "0.4.19" @@ -3069,6 +3283,29 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +[[package]] +name = "lcms2" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80205450f4d8b4de92f18111de879f3df4a6b728915e89b73c38f7a59a81ad90" +dependencies = [ + "bytemuck", + "foreign-types", + "lcms2-sys", +] + +[[package]] +name = "lcms2-sys" +version = "4.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "264db0b78119c5a37d78bb41fb355daab29b3b29430b53cd92e3da51f0ab06cc" +dependencies = [ + "cc", + "dunce", + "libc", + "pkg-config", +] + [[package]] name = "lexical-core" version = "1.0.6" @@ -3225,6 +3462,24 @@ dependencies = [ "libc", ] +[[package]] +name = "libseccomp" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e5310a2c5b6ffbc094b5f70a2ca7b79ed36ad90e6f90994b166489a1bce3fcc" +dependencies = [ + "bitflags 2.13.2", + "libc", + "libseccomp-sys", + "pkg-config", +] + +[[package]] +name = "libseccomp-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60276e2d41bbb68b323e566047a1bfbf952050b157d8b5cdc74c07c1bf4ca3b6" + [[package]] name = "libsqlite3-sys" version = "0.37.0" @@ -3389,6 +3644,24 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "memfd" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57804b2c9b69967f1536a56f86297e367a33b19e98852ed624b84551cdbc0d90" +dependencies = [ + "rustix 1.1.4", +] + +[[package]] +name = "memmap2" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0ca1155a9ecc25aa12e8313a119f32edf0255fea2f44839c4ac0a12b2ac157" +dependencies = [ + "libc", +] + [[package]] name = "memoffset" version = "0.9.1" @@ -3557,6 +3830,18 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27b02d87554356db9e9a873add8782d4ea6e3e58ea071a9adb9a2e8ddb884a8b" +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags 2.13.2", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nix" version = "0.31.3" @@ -3874,6 +4159,12 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pastey" version = "0.2.3" @@ -3969,6 +4260,17 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +[[package]] +name = "piper" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c835479a4443ded371d6c535cbfd8d31ad92c5d23ae9770a61bc155e4992a3c1" +dependencies = [ + "atomic-waker", + "fastrand", + "futures-io", +] + [[package]] name = "pkcs1" version = "0.8.0-rc.4" @@ -4270,6 +4572,26 @@ dependencies = [ "rand_core 0.9.5", ] +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "rcgen" version = "0.14.10" @@ -4445,6 +4767,25 @@ dependencies = [ "uuid", ] +[[package]] +name = "rmp" +version = "0.8.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ba8be72d372b2c9b35542551678538b562e7cf86c3315773cae48dfbfe7790c" +dependencies = [ + "num-traits", +] + +[[package]] +name = "rmp-serde" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f81bee8c8ef9b577d1681a70ebbc962c232461e397b22c208c43c04b67a155" +dependencies = [ + "rmp", + "serde", +] + [[package]] name = "rsa" version = "0.10.0-rc.18" @@ -5380,6 +5721,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "stringprep" version = "0.1.5" @@ -5546,6 +5893,7 @@ dependencies = [ "gio-unix", "glib", "glib-build-tools", + "glycin", "gtk4", "icu_decimal", "icu_locale_core", @@ -7219,6 +7567,17 @@ dependencies = [ "time", ] +[[package]] +name = "yeslogic-fontconfig-sys" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d8b8abf912b9a29ff112e1671c97c33636903d13a69712037190e6805af4f76" +dependencies = [ + "dlib", + "once_cell", + "pkg-config", +] + [[package]] name = "yoke" version = "0.8.3" @@ -7413,7 +7772,7 @@ dependencies = [ "flate2", "indexmap 2.14.2", "memchr", - "nix", + "nix 0.31.3", "typed-path", "zopfli", ] diff --git a/linux/Cargo.toml b/linux/Cargo.toml index 49e09aac94..429fcaa9c7 100644 --- a/linux/Cargo.toml +++ b/linux/Cargo.toml @@ -133,6 +133,8 @@ gtk4 = { version = "0.11", features = ["gnome_50"] } cairo-rs = { version = "0.22", features = ["png", "svg"] } libadwaita = { version = "0.9", features = ["v1_9"] } sourceview5 = { version = "0.11", features = ["v5_18"] } +# 3.x is the GNOME 50 release, which speaks the loaders' 2+ protocol. +glycin = { version = "3.1", default-features = false, features = ["gdk4", "tokio"] } gio = { version = "0.22", features = ["v2_88"] } gio-unix = { version = "0.22", features = ["v2_84"] } glib = { version = "0.22", features = ["v2_88"] } diff --git a/linux/README.md b/linux/README.md index 890e7df3a0..e088060e3f 100644 --- a/linux/README.md +++ b/linux/README.md @@ -85,28 +85,29 @@ System dependencies: ```bash # Ubuntu 26.04 -sudo apt install -y build-essential pkgconf meson ninja-build gettext desktop-file-utils appstream libglib2.0-bin libxml2-utils libgtk-4-dev libadwaita-1-dev libgtksourceview-5-dev libsqlite3-dev libkrb5-dev clang libclang-dev openssh-client +sudo apt install -y build-essential pkgconf meson ninja-build gettext desktop-file-utils appstream libglib2.0-bin libxml2-utils libgtk-4-dev libadwaita-1-dev libgtksourceview-5-dev libsqlite3-dev libkrb5-dev liblcms2-dev libseccomp-dev clang libclang-dev openssh-client # Fedora 44 -sudo dnf install -y gcc gcc-c++ pkgconf meson ninja-build gettext desktop-file-utils appstream glib2-devel libxml2 gtk4-devel libadwaita-devel gtksourceview5-devel sqlite-devel krb5-devel clang-devel openssh-clients +sudo dnf install -y gcc gcc-c++ pkgconf meson ninja-build gettext desktop-file-utils appstream glib2-devel libxml2 gtk4-devel libadwaita-devel gtksourceview5-devel sqlite-devel krb5-devel lcms2-devel libseccomp-devel clang-devel openssh-clients # Arch (not covered by CI) -sudo pacman -S --needed base-devel pkgconf meson ninja gettext desktop-file-utils appstream libxml2 gtk4 libadwaita gtksourceview5 sqlite krb5 clang openssh +sudo pacman -S --needed base-devel pkgconf meson ninja gettext desktop-file-utils appstream libxml2 gtk4 libadwaita gtksourceview5 sqlite krb5 lcms2 libseccomp clang openssh ``` The Ubuntu list is the .deb's build dependencies in `build-aux/debian/control` and the Fedora list the RPM's in `build-aux/rpm/tablepro.spec`, less the ones only their tests need. CI builds both. The test suites need a few more: an X server for the GTK tests, a session bus, the icon theme, a generated `en_US.UTF-8` locale for the catalogue -test, and gnome-keyring for the Secret Service tests. +test, gnome-keyring for the Secret Service tests and the glycin loaders +for the Row Details picture tests. ```bash # Ubuntu 26.04 -sudo apt install -y xvfb xauth dbus-daemon adwaita-icon-theme locales gnome-keyring +sudo apt install -y xvfb xauth dbus-daemon adwaita-icon-theme locales gnome-keyring glycin-loaders sudo localedef -i en_US -f UTF-8 en_US.UTF-8 # Fedora 44 -sudo dnf install -y xorg-x11-server-Xvfb xorg-x11-xauth dbus-daemon adwaita-icon-theme glibc-langpack-en gnome-keyring +sudo dnf install -y xorg-x11-server-Xvfb xorg-x11-xauth dbus-daemon adwaita-icon-theme glibc-langpack-en gnome-keyring glycin-loaders ``` Check the installed versions against the floors in @@ -147,6 +148,11 @@ link the system SQLite through sqlx's `sqlite-unbundled` feature rather than compiling a second copy into the binary. `libclang-dev` lets `libsqlite3-sys` run bindgen against the installed `sqlite3.h`. +`libseccomp-dev` and `liblcms2-dev` are there for glycin, which decodes +the picture of a binary value in Row Details in a sandbox +([ADR 0029](docs/decisions/0029-images-decode-in-glycin.md)). At run time +it needs the glycin loaders, which GNOME installs. + A C++ compiler is there for DuckDB, which the `duckdb` crate compiles from its own sources: `build-essential` brings g++ on Ubuntu, `gcc-c++` on Fedora. The first build of it takes several minutes. diff --git a/linux/build-aux/debian/control b/linux/build-aux/debian/control index 74d0eafa15..ccd124351b 100644 --- a/linux/build-aux/debian/control +++ b/linux/build-aux/debian/control @@ -14,6 +14,8 @@ Build-Depends: libgtk-4-dev (>= 4.22), libgtksourceview-5-dev (>= 5.18), libkrb5-dev, + liblcms2-dev, + libseccomp-dev, libsqlite3-dev (>= 3.46), libxml2-utils, meson (>= 1.4), @@ -23,6 +25,7 @@ Build-Depends: appstream , ca-certificates , dbus-daemon , + glycin-loaders , locales-all , openssh-client , xauth , @@ -41,6 +44,7 @@ Depends: ${shlibs:Depends}, Recommends: ca-certificates, + glycin-loaders, gnome-keyring | kwallet6 | keepassxc, Suggests: krb5-user, diff --git a/linux/build-aux/debian/copyright b/linux/build-aux/debian/copyright index ed284968cd..f7baa1d5f4 100644 --- a/linux/build-aux/debian/copyright +++ b/linux/build-aux/debian/copyright @@ -741,6 +741,8 @@ Files: vendor/aho-corasick-1.1.5/* vendor/byteorder-1.5.0/* vendor/csv-1.4.0/* vendor/csv-core-0.1.13/* + vendor/jiff-0.2.37/* + vendor/jiff-core-0.1.1/* vendor/memchr-2.8.3/* Copyright: 2015 Andrew Gallant License: Expat or Unlicense @@ -999,11 +1001,15 @@ License: Apache-2.0 or Expat OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -Files: vendor/async-lock-3.4.2/* +Files: vendor/async-channel-2.5.0/* + vendor/async-lock-3.4.2/* + vendor/async-task-4.7.1/* vendor/async-trait-0.1.92/* + vendor/blocking-1.7.0/* vendor/clickhouse-0.15.2/* vendor/clickhouse-macros-0.3.0/* vendor/clickhouse-types-0.1.3/* + vendor/concurrent-queue-2.5.0/* vendor/derive-syn-parse-0.2.0/* vendor/displaydoc-0.2.7/* vendor/dyn-clone-1.0.20/* @@ -1020,8 +1026,10 @@ Files: vendor/async-lock-3.4.2/* vendor/once_cell-1.21.4/* vendor/ordered-stream-0.2.0/* vendor/parking-2.2.1/* + vendor/paste-1.0.15/* vendor/pastey-0.2.3/* vendor/pin-project-lite-0.2.17/* + vendor/piper-0.2.5/* vendor/portable-atomic-1.15.0/* vendor/proc-macro-crate-3.5.0/* vendor/proc-macro2-1.0.107/* @@ -1048,16 +1056,17 @@ Files: vendor/async-lock-3.4.2/* vendor/typed-builder-0.22.0/* vendor/typed-builder-macro-0.22.0/* vendor/typed-path-0.12.3/* -Copyright: the authors of async-lock, async-trait, clickhouse, clickhouse-macros, - clickhouse-types, derive-syn-parse, displaydoc, dyn-clone, +Copyright: the authors of async-channel, async-lock, async-task, async-trait, + blocking, clickhouse, clickhouse-macros, clickhouse-types, + concurrent-queue, derive-syn-parse, displaydoc, dyn-clone, event-listener, event-listener-strategy, fastrand, itoa, lexical-core, lexical-parse-float, lexical-parse-integer, lexical-util, lexical-write-float, lexical-write-integer, once_cell, ordered-stream, - parking, pastey, pin-project-lite, portable-atomic, proc-macro-crate, - proc-macro2, quote, ref-cast, ref-cast-impl, relm4, relm4-macros, - rustversion, semver, serde, serde_bytes, serde_core, serde_derive, - serde_derive_internals, serde_json, serde_repr, sqlformat, syn, - thiserror, thiserror-impl, typed-builder, typed-builder-macro, + parking, paste, pastey, pin-project-lite, piper, portable-atomic, + proc-macro-crate, proc-macro2, quote, ref-cast, ref-cast-impl, relm4, + relm4-macros, rustversion, semver, serde, serde_bytes, serde_core, + serde_derive, serde_derive_internals, serde_json, serde_repr, sqlformat, + syn, thiserror, thiserror-impl, typed-builder, typed-builder-macro, typed-path License: Apache-2.0 or Expat Permission is hereby granted, free of charge, to any @@ -1926,6 +1935,19 @@ License: Apache-2.0 or Expat IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/bytemuck-1.25.2/* +Copyright: 2019 Daniel "Lokathor" Gee. +License: Apache-2.0 or MIT or Zlib + MIT License + . + Copyright (c) 2019 Daniel "Lokathor" Gee. + . + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + Files: vendor/bytes-1.12.1/* Copyright: 2018 Carl Lerche License: Expat @@ -2575,6 +2597,7 @@ License: Apache-2.0 or Expat DEALINGS IN THE SOFTWARE. Files: vendor/crossbeam-channel-0.5.17/* + vendor/crossbeam-deque-0.8.8/* vendor/crossbeam-epoch-0.9.21/* vendor/crossbeam-queue-0.3.14/* vendor/crossbeam-utils-0.8.23/* @@ -3056,6 +3079,29 @@ License: Apache-2.0 or Expat IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/dlib-0.5.3/* +Copyright: 2015 Victor Berger +License: Expat + Copyright (c) 2015 Victor Berger + . + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + Files: vendor/dotenvy-0.15.7/* Copyright: 2014 Santiago Lapresta and contributors License: Expat @@ -3323,6 +3369,33 @@ License: Apache-2.0 or Expat IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/env_filter-2.0.0/* + vendor/env_logger-0.11.11/* + vendor/toml_datetime-1.1.1+spec-1.1.0/* + vendor/toml_edit-0.25.15+spec-1.1.0/* + vendor/toml_parser-1.1.3+spec-1.1.0/* +Copyright: Individual contributors +License: Apache-2.0 or Expat + Copyright (c) Individual contributors + . + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + Files: vendor/equivalent-1.0.2/* Copyright: 2016--2023 License: Apache-2.0 or Expat @@ -3578,6 +3651,31 @@ License: Zlib . 3. This notice may not be removed or altered from any source distribution. +Files: vendor/foreign-types-0.5.0/* + vendor/foreign-types-macros-0.2.4/* + vendor/foreign-types-shared-0.3.1/* +Copyright: 2017 The foreign-types Developers +License: Apache-2.0 or Expat + Copyright (c) 2017 The foreign-types Developers + . + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + Files: vendor/form_urlencoded-1.2.2/* Copyright: 2013-2016 The rust-url developers License: Apache-2.0 or Expat @@ -3844,6 +3942,388 @@ License: Expat OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/glycin-3.1.0/* + vendor/glycin-common-1.0.4/* + vendor/glycin-utils-4.1.0/* + vendor/gufo-common-1.1.0/* + vendor/gufo-exif-0.4.0/* +Copyright: the authors of glycin, glycin-common, glycin-utils, gufo-common, + gufo-exif +License: LGPL-2.1+ or MPL-2.0 + Mozilla Public License Version 2.0 + ================================== + . + 1. Definitions + -------------- + . + 1.1. "Contributor" + means each individual or legal entity that creates, contributes to + the creation of, or owns Covered Software. + . + 1.2. "Contributor Version" + means the combination of the Contributions of others (if any) used + by a Contributor and that particular Contributor's Contribution. + . + 1.3. "Contribution" + means Covered Software of a particular Contributor. + . + 1.4. "Covered Software" + means Source Code Form to which the initial Contributor has attached + the notice in Exhibit A, the Executable Form of such Source Code + Form, and Modifications of such Source Code Form, in each case + including portions thereof. + . + 1.5. "Incompatible With Secondary Licenses" + means + . + (a) that the initial Contributor has attached the notice described + in Exhibit B to the Covered Software; or + . + (b) that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the + terms of a Secondary License. + . + 1.6. "Executable Form" + means any form of the work other than Source Code Form. + . + 1.7. "Larger Work" + means a work that combines Covered Software with other material, in + a separate file or files, that is not Covered Software. + . + 1.8. "License" + means this document. + . + 1.9. "Licensable" + means having the right to grant, to the maximum extent possible, + whether at the time of the initial grant or subsequently, any and + all of the rights conveyed by this License. + . + 1.10. "Modifications" + means any of the following: + . + (a) any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered + Software; or + . + (b) any new file in Source Code Form that contains any Covered + Software. + . + 1.11. "Patent Claims" of a Contributor + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the + License, by the making, using, selling, offering for sale, having + made, import, or transfer of either its Contributions or its + Contributor Version. + . + 1.12. "Secondary License" + means either the GNU General Public License, Version 2.0, the GNU + Lesser General Public License, Version 2.1, the GNU Affero General + Public License, Version 3.0, or any later versions of those + licenses. + . + 1.13. "Source Code Form" + means the form of the work preferred for making modifications. + . + 1.14. "You" (or "Your") + means an individual or a legal entity exercising rights under this + License. For legal entities, "You" includes any entity that + controls, is controlled by, or is under common control with You. For + purposes of this definition, "control" means (a) the power, direct + or indirect, to cause the direction or management of such entity, + whether by contract or otherwise, or (b) ownership of more than + fifty percent (50%) of the outstanding shares or beneficial + ownership of such entity. + . + 2. License Grants and Conditions + -------------------------------- + . + 2.1. Grants + . + Each Contributor hereby grants You a world-wide, royalty-free, + non-exclusive license: + . + (a) under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and + . + (b) under Patent Claims of such Contributor to make, use, sell, offer + for sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + . + 2.2. Effective Date + . + The licenses granted in Section 2.1 with respect to any Contribution + become effective for each Contribution on the date the Contributor first + distributes such Contribution. + . + 2.3. Limitations on Grant Scope + . + The licenses granted in this Section 2 are the only rights granted under + this License. No additional rights or licenses will be implied from the + distribution or licensing of Covered Software under this License. + Notwithstanding Section 2.1(b) above, no patent license is granted by a + Contributor: + . + (a) for any code that a Contributor has removed from Covered Software; + or + . + (b) for infringements caused by: (i) Your and any other third party's + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + . + (c) under Patent Claims infringed by Covered Software in the absence of + its Contributions. + . + This License does not grant any rights in the trademarks, service marks, + or logos of any Contributor (except as may be necessary to comply with + the notice requirements in Section 3.4). + . + 2.4. Subsequent Licenses + . + No Contributor makes additional grants as a result of Your choice to + distribute the Covered Software under a subsequent version of this + License (see Section 10.2) or under the terms of a Secondary License (if + permitted under the terms of Section 3.3). + . + 2.5. Representation + . + Each Contributor represents that the Contributor believes its + Contributions are its original creation(s) or it has sufficient rights + to grant the rights to its Contributions conveyed by this License. + . + 2.6. Fair Use + . + This License is not intended to limit any rights You have under + applicable copyright doctrines of fair use, fair dealing, or other + equivalents. + . + 2.7. Conditions + . + Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted + in Section 2.1. + . + 3. Responsibilities + ------------------- + . + 3.1. Distribution of Source Form + . + All distribution of Covered Software in Source Code Form, including any + Modifications that You create or to which You contribute, must be under + the terms of this License. You must inform recipients that the Source + Code Form of the Covered Software is governed by the terms of this + License, and how they can obtain a copy of this License. You may not + attempt to alter or restrict the recipients' rights in the Source Code + Form. + . + 3.2. Distribution of Executable Form + . + If You distribute Covered Software in Executable Form then: + . + (a) such Covered Software must also be made available in Source Code + Form, as described in Section 3.1, and You must inform recipients of + the Executable Form how they can obtain a copy of such Source Code + Form by reasonable means in a timely manner, at a charge no more + than the cost of distribution to the recipient; and + . + (b) You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter + the recipients' rights in the Source Code Form under this License. + . + 3.3. Distribution of a Larger Work + . + You may create and distribute a Larger Work under terms of Your choice, + provided that You also comply with the requirements of this License for + the Covered Software. If the Larger Work is a combination of Covered + Software with a work governed by one or more Secondary Licenses, and the + Covered Software is not Incompatible With Secondary Licenses, this + License permits You to additionally distribute such Covered Software + under the terms of such Secondary License(s), so that the recipient of + the Larger Work may, at their option, further distribute the Covered + Software under the terms of either this License or such Secondary + License(s). + . + 3.4. Notices + . + You may not remove or alter the substance of any license notices + (including copyright notices, patent notices, disclaimers of warranty, + or limitations of liability) contained within the Source Code Form of + the Covered Software, except that You may alter any license notices to + the extent required to remedy known factual inaccuracies. + . + 3.5. Application of Additional Terms + . + You may choose to offer, and to charge a fee for, warranty, support, + indemnity or liability obligations to one or more recipients of Covered + Software. However, You may do so only on Your own behalf, and not on + behalf of any Contributor. You must make it absolutely clear that any + such warranty, support, indemnity, or liability obligation is offered by + You alone, and You hereby agree to indemnify every Contributor for any + liability incurred by such Contributor as a result of warranty, support, + indemnity or liability terms You offer. You may include additional + disclaimers of warranty and limitations of liability specific to any + jurisdiction. + . + 4. Inability to Comply Due to Statute or Regulation + --------------------------------------------------- + . + If it is impossible for You to comply with any of the terms of this + License with respect to some or all of the Covered Software due to + statute, judicial order, or regulation then You must: (a) comply with + the terms of this License to the maximum extent possible; and (b) + describe the limitations and the code they affect. Such description must + be placed in a text file included with all distributions of the Covered + Software under this License. Except to the extent prohibited by statute + or regulation, such description must be sufficiently detailed for a + recipient of ordinary skill to be able to understand it. + . + 5. Termination + -------------- + . + 5.1. The rights granted under this License will terminate automatically + if You fail to comply with any of its terms. However, if You become + compliant, then the rights granted under this License from a particular + Contributor are reinstated (a) provisionally, unless and until such + Contributor explicitly and finally terminates Your grants, and (b) on an + ongoing basis, if such Contributor fails to notify You of the + non-compliance by some reasonable means prior to 60 days after You have + come back into compliance. Moreover, Your grants from a particular + Contributor are reinstated on an ongoing basis if such Contributor + notifies You of the non-compliance by some reasonable means, this is the + first time You have received notice of non-compliance with this License + from such Contributor, and You become compliant prior to 30 days after + Your receipt of the notice. + . + 5.2. If You initiate litigation against any entity by asserting a patent + infringement claim (excluding declaratory judgment actions, + counter-claims, and cross-claims) alleging that a Contributor Version + directly or indirectly infringes any patent, then the rights granted to + You by any and all Contributors for the Covered Software under Section + 2.1 of this License shall terminate. + . + 5.3. In the event of termination under Sections 5.1 or 5.2 above, all + end user license agreements (excluding distributors and resellers) which + have been validly granted by You or Your distributors under this License + prior to termination shall survive termination. + . + ************************************************************************ + * * + * 6. Disclaimer of Warranty * + * ------------------------- * + * * + * Covered Software is provided under this License on an "as is" * + * basis, without warranty of any kind, either expressed, implied, or * + * statutory, including, without limitation, warranties that the * + * Covered Software is free of defects, merchantable, fit for a * + * particular purpose or non-infringing. The entire risk as to the * + * quality and performance of the Covered Software is with You. * + * Should any Covered Software prove defective in any respect, You * + * (not any Contributor) assume the cost of any necessary servicing, * + * repair, or correction. This disclaimer of warranty constitutes an * + * essential part of this License. No use of any Covered Software is * + * authorized under this License except under this disclaimer. * + * * + ************************************************************************ + . + ************************************************************************ + * * + * 7. Limitation of Liability * + * -------------------------- * + * * + * Under no circumstances and under no legal theory, whether tort * + * (including negligence), contract, or otherwise, shall any * + * Contributor, or anyone who distributes Covered Software as * + * permitted above, be liable to You for any direct, indirect, * + * special, incidental, or consequential damages of any character * + * including, without limitation, damages for lost profits, loss of * + * goodwill, work stoppage, computer failure or malfunction, or any * + * and all other commercial damages or losses, even if such party * + * shall have been informed of the possibility of such damages. This * + * limitation of liability shall not apply to liability for death or * + * personal injury resulting from such party's negligence to the * + * extent applicable law prohibits such limitation. Some * + * jurisdictions do not allow the exclusion or limitation of * + * incidental or consequential damages, so this exclusion and * + * limitation may not apply to You. * + * * + ************************************************************************ + . + 8. Litigation + ------------- + . + Any litigation relating to this License may be brought only in the + courts of a jurisdiction where the defendant maintains its principal + place of business and such litigation shall be governed by laws of that + jurisdiction, without reference to its conflict-of-law provisions. + Nothing in this Section shall prevent a party's ability to bring + cross-claims or counter-claims. + . + 9. Miscellaneous + ---------------- + . + This License represents the complete agreement concerning the subject + matter hereof. If any provision of this License is held to be + unenforceable, such provision shall be reformed only to the extent + necessary to make it enforceable. Any law or regulation which provides + that the language of a contract shall be construed against the drafter + shall not be used to construe this License against a Contributor. + . + 10. Versions of the License + --------------------------- + . + 10.1. New Versions + . + Mozilla Foundation is the license steward. Except as provided in Section + 10.3, no one other than the license steward has the right to modify or + publish new versions of this License. Each version will be given a + distinguishing version number. + . + 10.2. Effect of New Versions + . + You may distribute the Covered Software under the terms of the version + of the License under which You originally received the Covered Software, + or under the terms of any subsequent version published by the license + steward. + . + 10.3. Modified Versions + . + If you create software not governed by this License, and you want to + create a new license for such software, you may create and use a + modified version of this License if you rename the license and remove + any references to the name of the license steward (except to note that + such modified license differs from this License). + . + 10.4. Distributing Source Code Form that is Incompatible With Secondary + Licenses + . + If You choose to distribute Source Code Form that is Incompatible With + Secondary Licenses under the terms of this version of the License, the + notice described in Exhibit B of this License must be attached. + . + Exhibit A - Source Code Form License Notice + ------------------------------------------- + . + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at https://mozilla.org/MPL/2.0/. + . + If it is not possible or desirable to put the notice in a particular + file, then You may include the notice in a location (such as a LICENSE + file in a relevant directory) where a recipient would be likely to look + for such a notice. + . + You may add additional accurate notices of copyright ownership. + . + Exhibit B - "Incompatible With Secondary Licenses" Notice + --------------------------------------------------------- + . + This Source Code Form is "Incompatible With Secondary Licenses", as + defined by the Mozilla Public License, v. 2.0. + Files: vendor/half-2.7.1/* vendor/ident_case-1.0.1/* Copyright: the authors of half, ident_case @@ -4360,6 +4840,8 @@ License: Apache-2.0 or Expat THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Files: vendor/lazy_static-1.5.0/* + vendor/rayon-1.12.0/* + vendor/rayon-core-1.13.0/* Copyright: 2010 The Rust Project Developers License: Apache-2.0 or Expat Copyright (c) 2010 The Rust Project Developers @@ -4388,6 +4870,55 @@ License: Apache-2.0 or Expat IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/lcms2-6.2.0/* +Copyright: Kornel Lesiński +License: Expat + Copyright (c) Kornel Lesiński + . + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + +Files: vendor/lcms2-sys-4.0.7/* +Copyright: 2023 Marti Maria Saguer +License: Expat + MIT License + . + Copyright (c) 2023 Marti Maria Saguer + . + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject + to the following conditions: + . + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + Files: vendor/libc-0.2.189/* Copyright: The Rust Project Developers License: Apache-2.0 or Expat @@ -5308,6 +5839,22 @@ License: Expat OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/libloading-0.8.9/* +Copyright: 2015, Simonas Kazlauskas +License: ISC + Copyright © 2015, Simonas Kazlauskas + . + Permission to use, copy, modify, and/or distribute this software for any purpose with or without + fee is hereby granted, provided that the above copyright notice and this permission notice appear + in all copies. + . + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE + AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + Files: vendor/libm-0.2.16/* Copyright: 2018 Jorge Aparicio 2005-2020 Rich Felker, et al. @@ -5403,6 +5950,32 @@ License: Expat . Copyright notices are retained in src/* files where relevant. +Files: vendor/libseccomp-0.4.0/* + vendor/libseccomp-sys-0.3.0/* +Copyright: 2021 Sony Group Corporation +License: Apache-2.0 or Expat + MIT License + . + Copyright (c) 2021 Sony Group Corporation + . + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + Files: vendor/libsqlite3-sys-0.37.0/* Copyright: 2014 The rusqlite developers License: Expat @@ -5663,6 +6236,57 @@ License: Apache-2.0 or Expat IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/memfd-0.6.6/* +Copyright: the authors of memfd +License: Apache-2.0 or Expat + Copyright + . + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + associated documentation files (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, publish, distribute, + sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in all copies or + substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT + OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Files: vendor/memmap2-0.9.12/* +Copyright: 2020 Yevhenii Reizner + 2015 Dan Burkert +License: Apache-2.0 or Expat + Copyright (c) 2020 Yevhenii Reizner + Copyright (c) 2015 Dan Burkert + . + Permission is hereby granted, free of charge, to any + person obtaining a copy of this software and associated + documentation files (the "Software"), to deal in the + Software without restriction, including without + limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of + the Software, and to permit persons to whom the Software + is furnished to do so, subject to the following + conditions: + . + The above copyright notice and this permission notice + shall be included in all copies or substantial portions + of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF + ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT + SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR + IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + Files: vendor/memoffset-0.9.1/* Copyright: 2017 Gilad Naaman License: Expat @@ -5814,6 +6438,31 @@ License: Apache-2.0 or Expat IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/nix-0.30.1/* +Copyright: 2015 Carl Lerche + nix-rust Authors +License: Expat + The MIT License (MIT) + . + Copyright (c) 2015 Carl Lerche + nix-rust Authors + . + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + Files: vendor/nu-ansi-term-0.50.3/* Copyright: 2014 Benjamin Sago 2021-2022 The Nushell Project Developers @@ -6321,6 +6970,32 @@ License: Apache-2.0 and Expat OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/rmp-0.8.15/* + vendor/rmp-serde-1.3.1/* +Copyright: 2017 Evgeny Safronov +License: Expat + MIT License + . + Copyright (c) 2017 Evgeny Safronov + . + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + Files: vendor/rust_xlsxwriter-0.102.0/* Copyright: 2022-2026 John McNamara License: Apache-2.0 or Expat @@ -7052,6 +7727,31 @@ License: Apache-2.0 or Expat IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/static_assertions-1.1.0/* +Copyright: 2017 Nikolai Vazquez +License: Apache-2.0 or Expat + MIT License + . + Copyright (c) 2017 Nikolai Vazquez + . + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + Files: vendor/stringprep-0.1.5/* Copyright: 2017 The rust-stringprep Developers License: Apache-2.0 or Expat @@ -7576,31 +8276,6 @@ License: Apache-2.0 or Expat IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -Files: vendor/toml_datetime-1.1.1+spec-1.1.0/* - vendor/toml_edit-0.25.15+spec-1.1.0/* - vendor/toml_parser-1.1.3+spec-1.1.0/* -Copyright: Individual contributors -License: Apache-2.0 or Expat - Copyright (c) Individual contributors - . - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - . - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - . - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - Files: vendor/tower-service-0.3.3/* Copyright: 2019 Tower Contributors License: Expat @@ -8141,6 +8816,37 @@ License: BSL-1.0 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Files: vendor/yeslogic-fontconfig-sys-6.0.1/* +Copyright: 2019 YesLogic Pty. Ltd. + 2016 Manuel Reinhardt + 2014 Austin Bonander + 2013 The Servo Project Developers. +License: Expat + The MIT License (MIT) + . + Copyright 2019 YesLogic Pty. Ltd. + Copyright 2016 Manuel Reinhardt + Copyright 2014 Austin Bonander + Copyright 2013 The Servo Project Developers. + . + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + Files: vendor/zbus-5.19.0/* vendor/zbus_macros-5.19.0/* vendor/zbus_names-4.3.4/* diff --git a/linux/build-aux/rpm/tablepro.spec b/linux/build-aux/rpm/tablepro.spec index 15d8da05c6..90f7a41bf3 100644 --- a/linux/build-aux/rpm/tablepro.spec +++ b/linux/build-aux/rpm/tablepro.spec @@ -39,6 +39,7 @@ License: %{shrink: BSL-1.0 AND CDLA-Permissive-2.0 AND ISC AND + (LGPL-2.1-or-later OR MPL-2.0) AND MIT AND (MIT OR Unlicense) AND Unicode-3.0 AND @@ -73,6 +74,9 @@ BuildRequires: pkgconfig(gtksourceview-5) >= 5.18 # security fixes arrive through Fedora's sqlite package. BuildRequires: pkgconfig(sqlite3) >= 3.46 BuildRequires: pkgconfig(mit-krb5-gssapi) +# glycin, which decodes a value's picture in a sandbox. +BuildRequires: pkgconfig(libseccomp) +BuildRequires: pkgconfig(lcms2) %if %{with check} # The gtk suite opens windows on a display and owns D-Bus names, and the # catalogue test needs a real locale. @@ -82,6 +86,7 @@ BuildRequires: /usr/bin/dbus-run-session BuildRequires: adwaita-icon-theme BuildRequires: glibc-langpack-en BuildRequires: openssh-clients >= 8.7 +BuildRequires: glycin-loaders %endif Requires: hicolor-icon-theme @@ -99,6 +104,8 @@ Requires: openssh-clients >= 8.7 # Saved passwords live in the Secret Service. Without a provider every # connect asks for the password and Remember is hidden. Recommends: (gnome-keyring or kf6-kwallet or keepassxc) +# Without them Row Details shows an image's bytes but not its picture. +Recommends: glycin-loaders # kinit, for SQL Server sign-in with Kerberos. Suggests: krb5-workstation diff --git a/linux/changelog.d/3882.added.md b/linux/changelog.d/3882.added.md new file mode 100644 index 0000000000..072f81d259 --- /dev/null +++ b/linux/changelog.d/3882.added.md @@ -0,0 +1 @@ +Row Details shows a binary value as a hex dump, and an image as a picture above it. diff --git a/linux/crates/app/Cargo.toml b/linux/crates/app/Cargo.toml index cc3f705740..42d002c708 100644 --- a/linux/crates/app/Cargo.toml +++ b/linux/crates/app/Cargo.toml @@ -87,6 +87,7 @@ cairo-rs.workspace = true libadwaita.workspace = true sourceview5.workspace = true glib.workspace = true +glycin.workspace = true relm4.workspace = true # CLOCK_BOOTTIME, which std does not expose, tells a suspend apart. rustix = { workspace = true, features = ["time"] } diff --git a/linux/crates/app/src/services/grid/display_text.rs b/linux/crates/app/src/services/grid/display_text.rs index a0ee4209b2..a6c9b17205 100644 --- a/linux/crates/app/src/services/grid/display_text.rs +++ b/linux/crates/app/src/services/grid/display_text.rs @@ -160,12 +160,7 @@ fn cut_in_place(scratch: &mut String) -> DisplayText<'_> { fn finish_cut(scratch: &mut String, truncated_bytes: usize) -> DisplayText<'_> { if truncated_bytes > 0 { - scratch.push_str(&crate::i18n::ngettext_f( - "… ({count} more byte)", - "… ({count} more bytes)", - plural_count(truncated_bytes), - &[("count", &truncated_bytes.to_string())], - )); + scratch.push_str(&more_bytes(truncated_bytes)); } DisplayText { text: scratch, @@ -174,6 +169,15 @@ fn finish_cut(scratch: &mut String, truncated_bytes: usize) -> DisplayText<'_> { } } +pub fn more_bytes(count: usize) -> String { + crate::i18n::ngettext_f( + "… ({count} more byte)", + "… ({count} more bytes)", + plural_count(count), + &[("count", &count.to_string())], + ) +} + /// Where the cell cuts `text`, on a char boundary. A string of no more /// bytes than the cap has no more chars either, so it is not walked. fn cut_at(text: &str) -> Option { diff --git a/linux/crates/app/src/services/grid/hex_dump.rs b/linux/crates/app/src/services/grid/hex_dump.rs new file mode 100644 index 0000000000..30f283ab55 --- /dev/null +++ b/linux/crates/app/src/services/grid/hex_dump.rs @@ -0,0 +1,93 @@ +use std::fmt::Write; + +use super::EDIT_MAX_BYTES; +use super::display_text::more_bytes; + +const PER_LINE: usize = 16; +const DIGITS: &[u8; 16] = b"0123456789abcdef"; +// The offset and its gap, three columns a byte and the middle gap, then " |", the text and "|\n". +const LINE_LEN: usize = 10 + PER_LINE * 3 + 1 + 2 + PER_LINE + 2; +// The pane lays the dump out whole on the GTK thread, as it does a value it opens for an edit. +const HEX_DUMP_MAX_BYTES: usize = EDIT_MAX_BYTES / LINE_LEN * PER_LINE; + +pub fn hex_dump(bytes: &[u8]) -> String { + let shown = bytes.get(..HEX_DUMP_MAX_BYTES).unwrap_or(bytes); + let mut dump = String::with_capacity(shown.len().div_ceil(PER_LINE) * LINE_LEN + LINE_LEN); + for (line, chunk) in shown.chunks(PER_LINE).enumerate() { + if line > 0 { + dump.push('\n'); + } + let _ = write!(dump, "{:08x} ", line * PER_LINE); + for at in 0..PER_LINE { + match chunk.get(at) { + Some(byte) => { + dump.push(char::from(DIGITS[usize::from(byte >> 4)])); + dump.push(char::from(DIGITS[usize::from(byte & 0x0f)])); + dump.push(' '); + } + None => dump.push_str(" "), + } + if at == PER_LINE / 2 - 1 { + dump.push(' '); + } + } + dump.push_str(" |"); + dump.extend(chunk.iter().map(|&byte| match byte { + 0x20..=0x7e => char::from(byte), + _ => '.', + })); + dump.push('|'); + } + if shown.len() < bytes.len() { + dump.push('\n'); + dump.push_str(&more_bytes(bytes.len() - shown.len())); + } + dump +} + +#[cfg(test)] +mod tests { + use super::{EDIT_MAX_BYTES, HEX_DUMP_MAX_BYTES, LINE_LEN, PER_LINE, hex_dump}; + + #[test] + fn a_dump_lays_out_offset_hex_and_text() { + assert_eq!( + hex_dump(b"Hello, TablePro!\x00\x01\xff \x7f~"), + "00000000 48 65 6c 6c 6f 2c 20 54 61 62 6c 65 50 72 6f 21 |Hello, TablePro!|\n\ + 00000010 00 01 ff 20 7f 7e |... .~|" + ); + } + + #[test] + fn a_full_line_is_as_long_as_the_budget_counts() { + let dump = hex_dump(&[0x41; PER_LINE * 2]); + + assert_eq!(dump.len() + 1, 2 * LINE_LEN, "{dump}"); + } + + #[test] + fn a_dump_past_its_cap_stays_within_the_editor_budget_and_says_what_it_left_out() { + let dump = hex_dump(&vec![0u8; HEX_DUMP_MAX_BYTES + 5]); + let note = "\n… (5 more bytes)"; + + assert!( + dump.ends_with(&format!("|................|{note}")), + "{}", + &dump[dump.len() - 120..] + ); + assert!(dump.len() - note.len() <= EDIT_MAX_BYTES, "{} bytes", dump.len()); + assert_eq!(dump.lines().count(), HEX_DUMP_MAX_BYTES / PER_LINE + 1); + } + + #[test] + fn a_value_at_the_cap_is_whole() { + let dump = hex_dump(&vec![0u8; HEX_DUMP_MAX_BYTES]); + + assert!(dump.ends_with('|'), "{}", &dump[dump.len() - 80..]); + } + + #[test] + fn an_empty_value_dumps_to_nothing() { + assert_eq!(hex_dump(&[]), ""); + } +} diff --git a/linux/crates/app/src/services/grid/mod.rs b/linux/crates/app/src/services/grid/mod.rs index 2252854d55..894c05f799 100644 --- a/linux/crates/app/src/services/grid/mod.rs +++ b/linux/crates/app/src/services/grid/mod.rs @@ -17,6 +17,7 @@ mod edit_policy; mod find_matches; mod grid_column; mod grid_row_id; +mod hex_dump; mod row_key_builder; mod row_snapshot; mod selection_summary; @@ -42,6 +43,7 @@ pub use self::edit_policy::{EditPolicy, EditRefusal, edit_policy, selection_hint pub use self::find_matches::{FindMatch, find_matches}; pub use self::grid_column::{CellDisplay, GridColumn, grid_columns}; pub use self::grid_row_id::GridRowId; +pub use self::hex_dump::hex_dump; pub use self::row_key_builder::RowKeyBuilder; pub use self::row_snapshot::RowSnapshot; pub use self::selection_summary::{SelectionSummary, SummaryRule, summarize}; diff --git a/linux/crates/app/src/ui/data_grid/row_detail/imp.rs b/linux/crates/app/src/ui/data_grid/row_detail/imp.rs index 64e1d765a2..32623cfecd 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/imp.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/imp.rs @@ -7,6 +7,7 @@ use libadwaita as adw; use libadwaita::subclass::prelude::*; use super::Shown; +use super::picture::PictureLoad; use super::value_editor::{Loaded, OpenEdit, Placeholders}; use crate::ui::data_grid::{DataGrid, PinnedRow}; use crate::ui::handler_guard::HandlerGuard; @@ -50,6 +51,8 @@ pub struct RowDetailPane { #[template_child] pub calendar: TemplateChild, #[template_child] + pub value_picture: TemplateChild, + #[template_child] pub clock_button: TemplateChild, #[template_child] pub clock_popover: TemplateChild, @@ -64,6 +67,8 @@ pub struct RowDetailPane { #[template_child] pub value_menu_button: TemplateChild, #[template_child] + pub value_scroller: TemplateChild, + #[template_child] pub value_view: TemplateChild, #[template_child] pub value_buffer: TemplateChild, @@ -84,6 +89,7 @@ pub struct RowDetailPane { pub loaded: RefCell>, pub placeholders: RefCell, pub edit: RefCell>, + pub picture_load: RefCell>, /// Set while the pane writes the buffer itself, which is no edit. pub loading: Cell, /// Set while the pane selects a field itself, which moves no focus. @@ -122,6 +128,9 @@ impl ObjectSubclass for RowDetailPane { impl ObjectImpl for RowDetailPane { fn dispose(&self) { + if let Some(load) = self.picture_load.take() { + load.stop(); + } self.guards.take(); self.obj().stop_watching_columns(); self.obj().watch_row(None); diff --git a/linux/crates/app/src/ui/data_grid/row_detail/mod.rs b/linux/crates/app/src/ui/data_grid/row_detail/mod.rs index afa81b1291..7804cc47ca 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/mod.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/mod.rs @@ -5,6 +5,7 @@ mod calendar; mod clock; mod fields; mod imp; +mod picture; #[cfg(test)] mod tests; mod value_editor; diff --git a/linux/crates/app/src/ui/data_grid/row_detail/picture.rs b/linux/crates/app/src/ui/data_grid/row_detail/picture.rs new file mode 100644 index 0000000000..b792a49e69 --- /dev/null +++ b/linux/crates/app/src/ui/data_grid/row_detail/picture.rs @@ -0,0 +1,117 @@ +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use gtk4::{gdk, gio, glib}; +use tablepro_session::runtime::Tasks; + +use super::RowDetailPane; + +// The head glycin guesses the type from. +const SNIFF_BYTES: usize = u16::MAX as usize; +// A preview has no use for more, and the texture lives in this process: 256 MB at 8 bits a channel. +const MAX_PIXELS: u64 = 8192 * 8192; + +// Spares copying a blob that is no image: glycin loads only `image/` types. +pub(super) fn may_be_image(bytes: &[u8]) -> bool { + let head = bytes.get(..SNIFF_BYTES).unwrap_or(bytes); + let (content_type, _) = gio::content_type_guess(None::<&str>, head); + gio::content_type_get_mime_type(&content_type).is_some_and(|mime| mime.starts_with("image/")) +} + +#[derive(Debug)] +pub struct PictureLoad { + pub(super) cancellable: gio::Cancellable, + tasks: Tasks, +} + +impl PictureLoad { + // glycin's cancel handler spawns on the runtime, which the GTK thread has not entered. + pub fn stop(self) { + let Self { cancellable, tasks } = self; + drop(tasks.spawn_task(async move { cancellable.cancel() })); + } +} + +impl RowDetailPane { + // Database bytes are untrusted, so they decode in glycin's sandbox, never in this process. + pub(super) fn show_picture(&self, bytes: Option>) { + let imp = self.imp(); + if let Some(superseded) = imp.picture_load.take() { + superseded.stop(); + } + let picture = imp.value_picture.get(); + picture.set_paintable(None::<&gdk::Paintable>); + picture.set_visible(false); + let Some(bytes) = bytes.filter(|bytes| may_be_image(bytes)) else { + return; + }; + let Some(tasks) = self + .grid() + .and_then(|grid| grid.config().map(|config| config.tasks.clone())) + else { + return; + }; + let cancellable = gio::Cancellable::new(); + let decode = tasks.spawn_task(decode(glib::Bytes::from_owned(bytes), cancellable.clone())); + imp.picture_load.replace(Some(PictureLoad { + cancellable: cancellable.clone(), + tasks, + })); + let pane = self.downgrade(); + glib::spawn_future_local(async move { + let decoded = decode.await; + let Some(pane) = pane.upgrade() else { + return; + }; + // The stop of a superseded decode lands on the runtime later than this may. + let current = pane + .imp() + .picture_load + .borrow() + .as_ref() + .is_some_and(|load| load.cancellable == cancellable); + if !current { + return; + } + pane.imp().picture_load.take(); + let texture = match decoded { + Ok(Ok(texture)) => texture, + Ok(Err(error)) => { + tracing::debug!(%error, "the value is no image glycin can show"); + return; + } + Err(failure) => { + tracing::warn!(%failure, "the value's picture could not be decoded"); + return; + } + }; + let picture = pane.imp().value_picture.get(); + picture.set_paintable(texture.as_ref()); + picture.set_visible(texture.is_some()); + }); + } +} + +async fn decode( + bytes: glib::Bytes, + cancellable: gio::Cancellable, +) -> Result, Box> { + let mechanism = glycin::SandboxSelector::Auto.determine_sandbox_mechanism().await; + let Some(sandbox) = sandbox(mechanism, cfg!(test)) else { + tracing::debug!("no sandbox to decode a value's picture in"); + return Ok(None); + }; + let mut loader = glycin::Loader::new_bytes(bytes); + loader.cancellable(cancellable).sandbox_selector(sandbox); + let image = loader.load().await.map_err(Box::new)?; + let details = image.details(); + if u64::from(details.width()) * u64::from(details.height()) > MAX_PIXELS { + return Ok(None); + } + Ok(Some(image.next_frame().await.map_err(Box::new)?.texture())) +} + +// glycin's Auto falls back to no sandbox where bwrap cannot make namespaces, and database bytes must +// never reach a decoder outside one. Tests allow it: CI runs them in containers. +pub(super) fn sandbox(mechanism: glycin::SandboxMechanism, unsandboxed: bool) -> Option { + (mechanism != glycin::SandboxMechanism::NotSandboxed || unsandboxed).then(|| mechanism.into_selector()) +} diff --git a/linux/crates/app/src/ui/data_grid/row_detail/tests.rs b/linux/crates/app/src/ui/data_grid/row_detail/tests.rs index 8b02914e82..c2aad31786 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/tests.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/tests.rs @@ -793,6 +793,27 @@ fn what_the_value_editor_shows_for_a_cell() { assert_eq!(draft.open_placeholder.as_deref(), Some("DEFAULT")); } +#[test] +fn a_binary_value_shows_as_a_read_only_dump() { + let blob = column_of(named("blob", "bytea", false)); + let saved = GridRowId::Persisted(RowKey::from_pk_values(&[Value::Int(1)]).expect("a key")); + let shown = |bytes: Vec| value_content(&blob, &saved, None, &CellInput::Value(Value::Bytes(bytes))); + + let dump = shown(vec![0xab, 0x41]); + assert!(dump.hex_dump); + assert_eq!( + dump.text, + "00000000 ab 41 |.A|" + ); + assert!(dump.message.is_some(), "a dump opened for typing"); + let empty = shown(Vec::new()); + assert_eq!( + (empty.text.as_str(), empty.open_placeholder.as_deref()), + ("", Some("<0 bytes>")), + "an empty value read as no value at all" + ); +} + #[gtk4::test] fn opening_the_calendar_and_closing_it_records_no_edit() { let (grid, pane) = date_grid(a_date(2024, 2, 29)); @@ -961,6 +982,117 @@ fn alt_return_closes_row_details_from_the_pane() { .expect("Alt+Return to close Row Details"); } +fn png_bytes(width: i32, height: i32) -> Vec { + let stride = 4 * width as usize; + let pixels = gtk4::glib::Bytes::from_owned(vec![255u8; stride * height as usize]); + let texture = gdk::MemoryTexture::new(width, height, gdk::MemoryFormat::R8g8b8a8, &pixels, stride); + texture.save_to_png_bytes().to_vec() +} + +// The first decode also starts glycin's loader process. +const DECODE_WAIT: Duration = Duration::from_secs(30); + +fn picture_size(pane: &RowDetailPane) -> Option<(i32, i32)> { + let picture = pane.imp().value_picture.get(); + let paintable = picture.paintable().filter(|_| picture.is_visible())?; + Some((paintable.intrinsic_width(), paintable.intrinsic_height())) +} + +fn binary_grid() -> (Grid, RowDetailPane) { + opened( + vec![ + named("id", "integer", true), + named("photo", "bytea", false), + named("blob", "bytea", false), + named("note", "text", false), + ], + vec![vec![ + Value::Int(1), + Value::Bytes(png_bytes(2, 1)), + Value::Bytes(b"Hi!\x00".to_vec()), + Value::Text("plain".into()), + ]], + ) +} + +#[gtk4::test] +fn a_binary_value_shows_its_bytes_unwrapped_and_an_image_its_picture() { + let (grid, pane) = binary_grid(); + + grid.focus(0, 2); + drain_main_context(); + let text = pane.typed_text(); + assert!(text.starts_with("00000000 48 69 21 00"), "{text}"); + assert!(text.ends_with("|Hi!.|"), "{text}"); + assert_eq!(pane.imp().value_view.wrap_mode(), gtk::WrapMode::None); + assert_eq!( + pane.imp().value_scroller.hscrollbar_policy(), + gtk::PolicyType::Automatic + ); + assert_eq!(picture_size(&pane), None, "bytes that are no image showed a picture"); + + grid.focus(0, 1); + wait_until(DECODE_WAIT, || picture_size(&pane).is_some()).expect("the image's picture"); + assert_eq!(picture_size(&pane), Some((2, 1))); + let (_, natural, _, _) = pane.imp().value_picture.measure(gtk::Orientation::Vertical, 300); + assert!(natural < 20, "a 1-pixel-high image asked for {natural} pixels"); + assert!(pane.typed_text().starts_with("00000000 89 50 4e 47")); + + grid.focus(0, 3); + drain_main_context(); + assert_eq!(picture_size(&pane), None, "a text field kept the picture"); + assert_eq!(pane.imp().value_view.wrap_mode(), gtk::WrapMode::WordChar); + assert_eq!(pane.imp().value_scroller.hscrollbar_policy(), gtk::PolicyType::Never); +} + +#[gtk4::test] +fn a_picture_the_pane_moved_away_from_never_lands() { + let (grid, pane) = binary_grid(); + + grid.focus(0, 1); + let first = pane + .imp() + .picture_load + .borrow() + .as_ref() + .expect("a decode under way") + .cancellable + .clone(); + grid.focus(0, 2); + + wait_until(DECODE_WAIT, || first.is_cancelled()).expect("the decode of the field left behind stops"); + assert!( + wait_until(Duration::from_secs(2), || picture_size(&pane).is_some()).is_err(), + "the picture of the field left behind landed" + ); +} + +#[gtk4::test] +fn only_an_image_type_goes_to_the_decoder() { + assert!(super::picture::may_be_image(&png_bytes(1, 1))); + assert!(super::picture::may_be_image( + br#""# + )); + assert!(!super::picture::may_be_image(b"Hi!\x00")); + assert!(!super::picture::may_be_image(b"%PDF-1.7\n")); + assert!(!super::picture::may_be_image(&[])); +} + +#[test] +fn a_picture_never_decodes_outside_a_sandbox() { + use glycin::{SandboxMechanism, SandboxSelector}; + + assert!(super::picture::sandbox(SandboxMechanism::NotSandboxed, false).is_none()); + assert!(matches!( + super::picture::sandbox(SandboxMechanism::Bwrap, false), + Some(SandboxSelector::Bwrap) + )); + assert!(matches!( + super::picture::sandbox(SandboxMechanism::FlatpakSpawn, false), + Some(SandboxSelector::FlatpakSpawn) + )); +} + /// Open the pane's clock until it shows, as `open_calendar` does. fn open_clock(pane: &RowDetailPane) { for _ in 0..5 { diff --git a/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs b/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs index 12a30c9990..fe64ce43ed 100644 --- a/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs +++ b/linux/crates/app/src/ui/data_grid/row_detail/value_editor.rs @@ -14,7 +14,7 @@ use crate::application::component_matches; use crate::i18n::{gettext, gettext_f}; use crate::services::grid::{ CellEditorKind, CellLock, CommitResult, EDIT_MAX_BYTES, EditStart, GridColumn, GridRowId, auto_filled_sentinel, - display_text, empty_placeholder, + display_text, empty_placeholder, hex_dump, }; use crate::ui::data_grid::edit::CHOICE_ACTIONS; use crate::ui::data_grid::selection::Target; @@ -80,6 +80,8 @@ pub struct ValueContent { pub emptied_placeholder: Option, pub language: Option<&'static str>, pub calendar: bool, + /// A hex dump lines up only unwrapped. + pub hex_dump: bool, /// The column's labels to pick from. pub choice: bool, pub clock: bool, @@ -102,6 +104,12 @@ pub fn value_content(column: &GridColumn, row: &GridRowId, lock: Option Some("NULL".to_owned()), CellInput::Default if column.auto_filled() => Some(auto_filled_sentinel()), CellInput::Default => Some("DEFAULT".to_owned()), + // An empty dump would read as no value at all. + CellInput::Value(value @ Value::Bytes(bytes)) if bytes.is_empty() => Some( + display_text(value, &column.column_type, &mut String::new()) + .text + .to_owned(), + ), CellInput::Value(_) => emptied_placeholder.clone(), }; ValueContent { @@ -114,6 +122,7 @@ pub fn value_content(column: &GridColumn, row: &GridRowId, lock: Option None, }, calendar: editable && matches!(kind, ColumnKind::Date | ColumnKind::Timestamp { .. }), + hex_dump: matches!(input, CellInput::Value(Value::Bytes(_))), // The menu holds no text, so a value too long or with a NUL for the // editor still opens it. choice: lock.is_none() && decodable && CellEditorKind::for_cell(column, Ok(())) == CellEditorKind::Choice, @@ -136,6 +145,7 @@ fn typed_form(column: &GridColumn, value: &Value) -> (String, Option) { }; let text = match value { Value::Null => String::new(), + Value::Bytes(bytes) => hex_dump(bytes), value => { let mut scratch = String::new(); display_text(value, &column.column_type, &mut scratch).text.to_owned() @@ -340,6 +350,10 @@ impl RowDetailPane { let input = tracked.edit_input(&row, column.index); let content = value_content(&column, &row.id(), tracked.lock(&column, &row), &input); self.show_value(&column.name, &content); + self.show_picture(match input { + CellInput::Value(Value::Bytes(bytes)) => Some(bytes), + _ => None, + }); imp.loaded.replace(Some(loaded)); } @@ -351,10 +365,12 @@ impl RowDetailPane { emptied_placeholder: None, language: None, calendar: false, + hex_dump: false, choice: false, clock: false, }; self.show_value("", &content); + self.show_picture(None); self.imp().value_view.set_editable(false); self.imp().loaded.take(); } @@ -387,6 +403,12 @@ impl RowDetailPane { .update_property(&[gtk::accessible::Property::Label(name)]); self.show_message(content.message.as_deref(), false); imp.calendar_button.set_visible(content.calendar); + let (wrap, hscroll) = match content.hex_dump { + true => (gtk::WrapMode::None, gtk::PolicyType::Automatic), + false => (gtk::WrapMode::WordChar, gtk::PolicyType::Never), + }; + view.set_wrap_mode(wrap); + imp.value_scroller.set_hscrollbar_policy(hscroll); imp.choice_button.set_visible(content.choice); imp.clock_button.set_visible(content.clock); self.update_placeholder(); diff --git a/linux/data/licenses/THIRD-PARTY-NOTICE.md b/linux/data/licenses/THIRD-PARTY-NOTICE.md index 69686a7235..3815fa2845 100644 --- a/linux/data/licenses/THIRD-PARTY-NOTICE.md +++ b/linux/data/licenses/THIRD-PARTY-NOTICE.md @@ -11,6 +11,7 @@ TablePro is licensed under the GNU Affero General Public License, version 3 or l - Apache License 2.0 (Apache-2.0) - BSD 3-Clause "New" or "Revised" License (BSD-3-Clause) - ISC License (ISC) +- Mozilla Public License 2.0 (MPL-2.0) - zlib License (Zlib) - Boost Software License 1.0 (BSL-1.0) - Creative Commons Zero v1.0 Universal (CC0-1.0) @@ -2918,6 +2919,28 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI Used by: +- libloading 0.8.9 + +~~~~text +Copyright © 2015, Simonas Kazlauskas + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without +fee is hereby granted, provided that the above copyright notice and this permission notice appear +in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS +SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF +THIS SOFTWARE. + +~~~~ + +### ISC License + +Used by: + - rustls-webpki 0.103.16 ~~~~text @@ -3715,6 +3738,8 @@ DEALINGS IN THE SOFTWARE. Used by: - lazy_static 1.5.0 +- rayon-core 1.13.0 +- rayon 1.12.0 ~~~~text Copyright (c) 2010 The Rust Project Developers @@ -4505,6 +4530,34 @@ SOFTWARE. Used by: +- dlib 0.5.3 + +~~~~text +Copyright (c) 2015 Victor Berger + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +~~~~ + +### MIT License + +Used by: + - resolv-conf 0.7.6 ~~~~text @@ -5169,6 +5222,37 @@ DEALINGS IN THE SOFTWARE. Used by: +- foreign-types-macros 0.2.4 +- foreign-types-shared 0.3.1 +- foreign-types 0.5.0 + +~~~~text +Copyright (c) 2017 The foreign-types Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +~~~~ + +### MIT License + +Used by: + - stringprep 0.1.5 ~~~~text @@ -6379,6 +6463,42 @@ DEALINGS IN THE SOFTWARE. Used by: +- memmap2 0.9.12 + +~~~~text +Copyright (c) 2020 Yevhenii Reizner +Copyright (c) 2015 Dan Burkert + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +~~~~ + +### MIT License + +Used by: + - const-oid 0.9.6 - der_derive 0.7.3 @@ -7172,6 +7292,8 @@ DEALINGS IN THE SOFTWARE. Used by: +- env_filter 2.0.0 +- env_logger 0.11.11 - toml_datetime 1.1.1+spec-1.1.0 - toml_edit 0.25.15+spec-1.1.0 - toml_parser 1.1.3+spec-1.1.0 @@ -7257,6 +7379,36 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +~~~~ + +### MIT License + +Used by: + +- lcms2 6.2.0 + +~~~~text +Copyright (c) Kornel Lesiński + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + ~~~~ ### MIT License @@ -7603,6 +7755,32 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +~~~~ + +### MIT License + +Used by: + +- memfd 0.6.6 + +~~~~text +Copyright + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or +substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT +OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + ~~~~ ### MIT License @@ -7739,6 +7917,69 @@ SOFTWARE. Used by: +- rmp-serde 1.3.1 +- rmp 0.8.15 + +~~~~text +MIT License + +Copyright (c) 2017 Evgeny Safronov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +~~~~ + +### MIT License + +Used by: + +- static_assertions 1.1.0 + +~~~~text +MIT License + +Copyright (c) 2017 Nikolai Vazquez + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +~~~~ + +### MIT License + +Used by: + - darling 0.24.1 - darling_core 0.24.1 - darling_macro 0.24.1 @@ -8022,6 +8263,25 @@ SOFTWARE. Used by: +- bytemuck 1.25.2 + +~~~~text +MIT License + +Copyright (c) 2019 Daniel "Lokathor" Gee. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +~~~~ + +### MIT License + +Used by: + - schemars 1.2.2 - schemars_derive 1.2.2 @@ -8366,6 +8626,37 @@ SOFTWARE. Used by: +- libseccomp-sys 0.3.0 +- libseccomp 0.4.0 + +~~~~text +MIT License + +Copyright (c) 2021 Sony Group Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +~~~~ + +### MIT License + +Used by: + - rustls-platform-verifier 0.7.0 ~~~~text @@ -8458,6 +8749,38 @@ SOFTWARE. Used by: +- lcms2-sys 4.0.7 + +~~~~text +MIT License + +Copyright (c) 2023 Marti Maria Saguer + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject +to the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +~~~~ + +### MIT License + +Used by: + - sse-stream 0.2.6 ~~~~text @@ -8771,12 +9094,16 @@ DEALINGS IN THE SOFTWARE. Used by: +- async-channel 2.5.0 - async-lock 3.4.2 +- async-task 4.7.1 - async-trait 0.1.92 - atomic-waker 1.1.2 +- blocking 1.7.0 - clickhouse-macros 0.3.0 - clickhouse-types 0.1.3 - clickhouse 0.15.2 +- concurrent-queue 2.5.0 - derive-syn-parse 0.2.0 - displaydoc 0.2.7 - dyn-clone 1.0.20 @@ -8797,8 +9124,10 @@ Used by: - once_cell 1.21.4 - ordered-stream 0.2.0 - parking 2.2.1 +- paste 1.0.15 - pastey 0.2.3 - pin-project-lite 0.2.17 +- piper 0.2.5 - portable-atomic 1.15.0 - proc-macro-crate 3.5.0 - proc-macro2 1.0.107 @@ -9240,6 +9569,8 @@ Used by: - byteorder 1.5.0 - csv-core 0.1.13 - csv 1.4.0 +- jiff-core 0.1.1 +- jiff 0.2.37 - memchr 2.8.3 ~~~~text @@ -9271,6 +9602,37 @@ THE SOFTWARE. Used by: +- nix 0.30.1 + +~~~~text +The MIT License (MIT) + +Copyright (c) 2015 Carl Lerche + nix-rust Authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +~~~~ + +### MIT License + +Used by: + - strsim 0.11.1 ~~~~text @@ -9660,6 +10022,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Used by: - crossbeam-channel 0.5.17 +- crossbeam-deque 0.8.8 - crossbeam-epoch 0.9.21 - crossbeam-queue 0.3.14 - crossbeam-utils 0.8.23 @@ -9817,6 +10180,41 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +~~~~ + +### MIT License + +Used by: + +- yeslogic-fontconfig-sys 6.0.1 + +~~~~text +The MIT License (MIT) + +Copyright 2019 YesLogic Pty. Ltd. +Copyright 2016 Manuel Reinhardt +Copyright 2014 Austin Bonander +Copyright 2013 The Servo Project Developers. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + ~~~~ ### MIT License @@ -10217,6 +10615,393 @@ Copyright notices are retained in src/* files where relevant. ~~~~ +### Mozilla Public License 2.0 + +Used by: + +- glycin-common 1.0.4 +- glycin-utils 4.1.0 +- glycin 3.1.0 +- gufo-common 1.1.0 +- gufo-exif 0.4.0 + +~~~~text +Mozilla Public License Version 2.0 +================================== + +1. Definitions +-------------- + +1.1. "Contributor" + means each individual or legal entity that creates, contributes to + the creation of, or owns Covered Software. + +1.2. "Contributor Version" + means the combination of the Contributions of others (if any) used + by a Contributor and that particular Contributor's Contribution. + +1.3. "Contribution" + means Covered Software of a particular Contributor. + +1.4. "Covered Software" + means Source Code Form to which the initial Contributor has attached + the notice in Exhibit A, the Executable Form of such Source Code + Form, and Modifications of such Source Code Form, in each case + including portions thereof. + +1.5. "Incompatible With Secondary Licenses" + means + + (a) that the initial Contributor has attached the notice described + in Exhibit B to the Covered Software; or + + (b) that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the + terms of a Secondary License. + +1.6. "Executable Form" + means any form of the work other than Source Code Form. + +1.7. "Larger Work" + means a work that combines Covered Software with other material, in + a separate file or files, that is not Covered Software. + +1.8. "License" + means this document. + +1.9. "Licensable" + means having the right to grant, to the maximum extent possible, + whether at the time of the initial grant or subsequently, any and + all of the rights conveyed by this License. + +1.10. "Modifications" + means any of the following: + + (a) any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered + Software; or + + (b) any new file in Source Code Form that contains any Covered + Software. + +1.11. "Patent Claims" of a Contributor + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the + License, by the making, using, selling, offering for sale, having + made, import, or transfer of either its Contributions or its + Contributor Version. + +1.12. "Secondary License" + means either the GNU General Public License, Version 2.0, the GNU + Lesser General Public License, Version 2.1, the GNU Affero General + Public License, Version 3.0, or any later versions of those + licenses. + +1.13. "Source Code Form" + means the form of the work preferred for making modifications. + +1.14. "You" (or "Your") + means an individual or a legal entity exercising rights under this + License. For legal entities, "You" includes any entity that + controls, is controlled by, or is under common control with You. For + purposes of this definition, "control" means (a) the power, direct + or indirect, to cause the direction or management of such entity, + whether by contract or otherwise, or (b) ownership of more than + fifty percent (50%) of the outstanding shares or beneficial + ownership of such entity. + +2. License Grants and Conditions +-------------------------------- + +2.1. Grants + +Each Contributor hereby grants You a world-wide, royalty-free, +non-exclusive license: + +(a) under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and + +(b) under Patent Claims of such Contributor to make, use, sell, offer + for sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + +2.2. Effective Date + +The licenses granted in Section 2.1 with respect to any Contribution +become effective for each Contribution on the date the Contributor first +distributes such Contribution. + +2.3. Limitations on Grant Scope + +The licenses granted in this Section 2 are the only rights granted under +this License. No additional rights or licenses will be implied from the +distribution or licensing of Covered Software under this License. +Notwithstanding Section 2.1(b) above, no patent license is granted by a +Contributor: + +(a) for any code that a Contributor has removed from Covered Software; + or + +(b) for infringements caused by: (i) Your and any other third party's + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + +(c) under Patent Claims infringed by Covered Software in the absence of + its Contributions. + +This License does not grant any rights in the trademarks, service marks, +or logos of any Contributor (except as may be necessary to comply with +the notice requirements in Section 3.4). + +2.4. Subsequent Licenses + +No Contributor makes additional grants as a result of Your choice to +distribute the Covered Software under a subsequent version of this +License (see Section 10.2) or under the terms of a Secondary License (if +permitted under the terms of Section 3.3). + +2.5. Representation + +Each Contributor represents that the Contributor believes its +Contributions are its original creation(s) or it has sufficient rights +to grant the rights to its Contributions conveyed by this License. + +2.6. Fair Use + +This License is not intended to limit any rights You have under +applicable copyright doctrines of fair use, fair dealing, or other +equivalents. + +2.7. Conditions + +Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted +in Section 2.1. + +3. Responsibilities +------------------- + +3.1. Distribution of Source Form + +All distribution of Covered Software in Source Code Form, including any +Modifications that You create or to which You contribute, must be under +the terms of this License. You must inform recipients that the Source +Code Form of the Covered Software is governed by the terms of this +License, and how they can obtain a copy of this License. You may not +attempt to alter or restrict the recipients' rights in the Source Code +Form. + +3.2. Distribution of Executable Form + +If You distribute Covered Software in Executable Form then: + +(a) such Covered Software must also be made available in Source Code + Form, as described in Section 3.1, and You must inform recipients of + the Executable Form how they can obtain a copy of such Source Code + Form by reasonable means in a timely manner, at a charge no more + than the cost of distribution to the recipient; and + +(b) You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter + the recipients' rights in the Source Code Form under this License. + +3.3. Distribution of a Larger Work + +You may create and distribute a Larger Work under terms of Your choice, +provided that You also comply with the requirements of this License for +the Covered Software. If the Larger Work is a combination of Covered +Software with a work governed by one or more Secondary Licenses, and the +Covered Software is not Incompatible With Secondary Licenses, this +License permits You to additionally distribute such Covered Software +under the terms of such Secondary License(s), so that the recipient of +the Larger Work may, at their option, further distribute the Covered +Software under the terms of either this License or such Secondary +License(s). + +3.4. Notices + +You may not remove or alter the substance of any license notices +(including copyright notices, patent notices, disclaimers of warranty, +or limitations of liability) contained within the Source Code Form of +the Covered Software, except that You may alter any license notices to +the extent required to remedy known factual inaccuracies. + +3.5. Application of Additional Terms + +You may choose to offer, and to charge a fee for, warranty, support, +indemnity or liability obligations to one or more recipients of Covered +Software. However, You may do so only on Your own behalf, and not on +behalf of any Contributor. You must make it absolutely clear that any +such warranty, support, indemnity, or liability obligation is offered by +You alone, and You hereby agree to indemnify every Contributor for any +liability incurred by such Contributor as a result of warranty, support, +indemnity or liability terms You offer. You may include additional +disclaimers of warranty and limitations of liability specific to any +jurisdiction. + +4. Inability to Comply Due to Statute or Regulation +--------------------------------------------------- + +If it is impossible for You to comply with any of the terms of this +License with respect to some or all of the Covered Software due to +statute, judicial order, or regulation then You must: (a) comply with +the terms of this License to the maximum extent possible; and (b) +describe the limitations and the code they affect. Such description must +be placed in a text file included with all distributions of the Covered +Software under this License. Except to the extent prohibited by statute +or regulation, such description must be sufficiently detailed for a +recipient of ordinary skill to be able to understand it. + +5. Termination +-------------- + +5.1. The rights granted under this License will terminate automatically +if You fail to comply with any of its terms. However, if You become +compliant, then the rights granted under this License from a particular +Contributor are reinstated (a) provisionally, unless and until such +Contributor explicitly and finally terminates Your grants, and (b) on an +ongoing basis, if such Contributor fails to notify You of the +non-compliance by some reasonable means prior to 60 days after You have +come back into compliance. Moreover, Your grants from a particular +Contributor are reinstated on an ongoing basis if such Contributor +notifies You of the non-compliance by some reasonable means, this is the +first time You have received notice of non-compliance with this License +from such Contributor, and You become compliant prior to 30 days after +Your receipt of the notice. + +5.2. If You initiate litigation against any entity by asserting a patent +infringement claim (excluding declaratory judgment actions, +counter-claims, and cross-claims) alleging that a Contributor Version +directly or indirectly infringes any patent, then the rights granted to +You by any and all Contributors for the Covered Software under Section +2.1 of this License shall terminate. + +5.3. In the event of termination under Sections 5.1 or 5.2 above, all +end user license agreements (excluding distributors and resellers) which +have been validly granted by You or Your distributors under this License +prior to termination shall survive termination. + +************************************************************************ +* * +* 6. Disclaimer of Warranty * +* ------------------------- * +* * +* Covered Software is provided under this License on an "as is" * +* basis, without warranty of any kind, either expressed, implied, or * +* statutory, including, without limitation, warranties that the * +* Covered Software is free of defects, merchantable, fit for a * +* particular purpose or non-infringing. The entire risk as to the * +* quality and performance of the Covered Software is with You. * +* Should any Covered Software prove defective in any respect, You * +* (not any Contributor) assume the cost of any necessary servicing, * +* repair, or correction. This disclaimer of warranty constitutes an * +* essential part of this License. No use of any Covered Software is * +* authorized under this License except under this disclaimer. * +* * +************************************************************************ + +************************************************************************ +* * +* 7. Limitation of Liability * +* -------------------------- * +* * +* Under no circumstances and under no legal theory, whether tort * +* (including negligence), contract, or otherwise, shall any * +* Contributor, or anyone who distributes Covered Software as * +* permitted above, be liable to You for any direct, indirect, * +* special, incidental, or consequential damages of any character * +* including, without limitation, damages for lost profits, loss of * +* goodwill, work stoppage, computer failure or malfunction, or any * +* and all other commercial damages or losses, even if such party * +* shall have been informed of the possibility of such damages. This * +* limitation of liability shall not apply to liability for death or * +* personal injury resulting from such party's negligence to the * +* extent applicable law prohibits such limitation. Some * +* jurisdictions do not allow the exclusion or limitation of * +* incidental or consequential damages, so this exclusion and * +* limitation may not apply to You. * +* * +************************************************************************ + +8. Litigation +------------- + +Any litigation relating to this License may be brought only in the +courts of a jurisdiction where the defendant maintains its principal +place of business and such litigation shall be governed by laws of that +jurisdiction, without reference to its conflict-of-law provisions. +Nothing in this Section shall prevent a party's ability to bring +cross-claims or counter-claims. + +9. Miscellaneous +---------------- + +This License represents the complete agreement concerning the subject +matter hereof. If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent +necessary to make it enforceable. Any law or regulation which provides +that the language of a contract shall be construed against the drafter +shall not be used to construe this License against a Contributor. + +10. Versions of the License +--------------------------- + +10.1. New Versions + +Mozilla Foundation is the license steward. Except as provided in Section +10.3, no one other than the license steward has the right to modify or +publish new versions of this License. Each version will be given a +distinguishing version number. + +10.2. Effect of New Versions + +You may distribute the Covered Software under the terms of the version +of the License under which You originally received the Covered Software, +or under the terms of any subsequent version published by the license +steward. + +10.3. Modified Versions + +If you create software not governed by this License, and you want to +create a new license for such software, you may create and use a +modified version of this License if you rename the license and remove +any references to the name of the license steward (except to note that +such modified license differs from this License). + +10.4. Distributing Source Code Form that is Incompatible With Secondary +Licenses + +If You choose to distribute Source Code Form that is Incompatible With +Secondary Licenses under the terms of this version of the License, the +notice described in Exhibit B of this License must be attached. + +Exhibit A - Source Code Form License Notice +------------------------------------------- + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at https://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular +file, then You may include the notice in a location (such as a LICENSE +file in a relevant directory) where a recipient would be likely to look +for such a notice. + +You may add additional accurate notices of copyright ownership. + +Exhibit B - "Incompatible With Secondary Licenses" Notice +--------------------------------------------------------- + + This Source Code Form is "Incompatible With Secondary Licenses", as + defined by the Mozilla Public License, v. 2.0. + +~~~~ + ### Unicode License v3 Used by: diff --git a/linux/data/licenses/third-party.json b/linux/data/licenses/third-party.json index 0a05162944..4fc842561c 100644 --- a/linux/data/licenses/third-party.json +++ b/linux/data/licenses/third-party.json @@ -165,6 +165,14 @@ { "crate": { "name": "aws-lc-sys", "version": "0.45.0" } } ] }, + { + "id": "ISC", + "name": "ISC License", + "text": "Copyright © 2015, Simonas Kazlauskas\n\nPermission to use, copy, modify, and/or distribute this software for any purpose with or without\nfee is hereby granted, provided that the above copyright notice and this permission notice appear\nin all copies.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS\nSOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE\nAUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\nWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,\nNEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF\nTHIS SOFTWARE.\n", + "used_by": [ + { "crate": { "name": "libloading", "version": "0.8.9" } } + ] + }, { "id": "ISC", "name": "ISC License", @@ -241,7 +249,9 @@ "name": "MIT License", "text": "Copyright (c) 2010 The Rust Project Developers\n\nPermission is hereby granted, free of charge, to any\nperson obtaining a copy of this software and associated\ndocumentation files (the \"Software\"), to deal in the\nSoftware without restriction, including without\nlimitation the rights to use, copy, modify, merge,\npublish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software\nis furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice\nshall be included in all copies or substantial portions\nof the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF\nANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\nTO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\nPARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\nSHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR\nIN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\nDEALINGS IN THE SOFTWARE.\n", "used_by": [ - { "crate": { "name": "lazy_static", "version": "1.5.0" } } + { "crate": { "name": "lazy_static", "version": "1.5.0" } }, + { "crate": { "name": "rayon-core", "version": "1.13.0" } }, + { "crate": { "name": "rayon", "version": "1.12.0" } } ] }, { @@ -449,6 +459,14 @@ { "crate": { "name": "unicode_categories", "version": "0.1.1" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "Copyright (c) 2015 Victor Berger\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.", + "used_by": [ + { "crate": { "name": "dlib", "version": "0.5.3" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -625,6 +643,16 @@ { "crate": { "name": "stable_deref_trait", "version": "1.2.1" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "Copyright (c) 2017 The foreign-types Developers\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n", + "used_by": [ + { "crate": { "name": "foreign-types-macros", "version": "0.2.4" } }, + { "crate": { "name": "foreign-types-shared", "version": "0.3.1" } }, + { "crate": { "name": "foreign-types", "version": "0.5.0" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -913,6 +941,14 @@ { "crate": { "name": "arcstr", "version": "1.2.0" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "Copyright (c) 2020 Yevhenii Reizner\nCopyright (c) 2015 Dan Burkert\n\nPermission is hereby granted, free of charge, to any\nperson obtaining a copy of this software and associated\ndocumentation files (the \"Software\"), to deal in the\nSoftware without restriction, including without\nlimitation the rights to use, copy, modify, merge,\npublish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software\nis furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice\nshall be included in all copies or substantial portions\nof the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF\nANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\nTO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\nPARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\nSHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR\nIN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\nDEALINGS IN THE SOFTWARE.\n", + "used_by": [ + { "crate": { "name": "memmap2", "version": "0.9.12" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -1114,6 +1150,8 @@ "name": "MIT License", "text": "Copyright (c) Individual contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n", "used_by": [ + { "crate": { "name": "env_filter", "version": "2.0.0" } }, + { "crate": { "name": "env_logger", "version": "0.11.11" } }, { "crate": { "name": "toml_datetime", "version": "1.1.1+spec-1.1.0" } }, { "crate": { "name": "toml_edit", "version": "0.25.15+spec-1.1.0" } }, { "crate": { "name": "toml_parser", "version": "1.1.3+spec-1.1.0" } } @@ -1137,6 +1175,14 @@ { "crate": { "name": "time", "version": "0.3.55" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "Copyright (c) Kornel Lesiński\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n", + "used_by": [ + { "crate": { "name": "lcms2", "version": "6.2.0" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -1253,6 +1299,14 @@ { "crate": { "name": "zerocopy", "version": "0.8.57" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "Copyright \n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and\nassociated documentation files (the \"Software\"), to deal in the Software without restriction,\nincluding without limitation the rights to use, copy, modify, merge, publish, distribute,\nsublicense, and/or sell copies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or\nsubstantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT\nNOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\nDAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT\nOF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n", + "used_by": [ + { "crate": { "name": "memfd", "version": "0.6.6" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -1287,6 +1341,23 @@ { "crate": { "name": "tap", "version": "1.0.1" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "MIT License\n\nCopyright (c) 2017 Evgeny Safronov\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n", + "used_by": [ + { "crate": { "name": "rmp-serde", "version": "1.3.1" } }, + { "crate": { "name": "rmp", "version": "0.8.15" } } + ] + }, + { + "id": "MIT", + "name": "MIT License", + "text": "MIT License\n\nCopyright (c) 2017 Nikolai Vazquez\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n", + "used_by": [ + { "crate": { "name": "static_assertions", "version": "1.1.0" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -1362,6 +1433,14 @@ { "crate": { "name": "comfy-table", "version": "7.1.4" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "MIT License\n\nCopyright (c) 2019 Daniel \"Lokathor\" Gee.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n", + "used_by": [ + { "crate": { "name": "bytemuck", "version": "1.25.2" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -1452,6 +1531,15 @@ { "crate": { "name": "derive-where", "version": "1.7.0" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "MIT License\n\nCopyright (c) 2021 Sony Group Corporation\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.", + "used_by": [ + { "crate": { "name": "libseccomp-sys", "version": "0.3.0" } }, + { "crate": { "name": "libseccomp", "version": "0.4.0" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -1476,6 +1564,14 @@ { "crate": { "name": "bnum", "version": "0.13.0" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "MIT License\n\nCopyright (c) 2023 Marti Maria Saguer\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the \n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to \npermit persons to whom the Software is furnished to do so, subject\nto the following conditions:\n\nThe above copyright notice and this permission notice shall be \nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF \nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE \nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n", + "used_by": [ + { "crate": { "name": "lcms2-sys", "version": "4.0.7" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -1568,12 +1664,16 @@ "name": "MIT License", "text": "Permission is hereby granted, free of charge, to any\nperson obtaining a copy of this software and associated\ndocumentation files (the \"Software\"), to deal in the\nSoftware without restriction, including without\nlimitation the rights to use, copy, modify, merge,\npublish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software\nis furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice\nshall be included in all copies or substantial portions\nof the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF\nANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\nTO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\nPARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\nSHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR\nIN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\nDEALINGS IN THE SOFTWARE.\n", "used_by": [ + { "crate": { "name": "async-channel", "version": "2.5.0" } }, { "crate": { "name": "async-lock", "version": "3.4.2" } }, + { "crate": { "name": "async-task", "version": "4.7.1" } }, { "crate": { "name": "async-trait", "version": "0.1.92" } }, { "crate": { "name": "atomic-waker", "version": "1.1.2" } }, + { "crate": { "name": "blocking", "version": "1.7.0" } }, { "crate": { "name": "clickhouse-macros", "version": "0.3.0" } }, { "crate": { "name": "clickhouse-types", "version": "0.1.3" } }, { "crate": { "name": "clickhouse", "version": "0.15.2" } }, + { "crate": { "name": "concurrent-queue", "version": "2.5.0" } }, { "crate": { "name": "derive-syn-parse", "version": "0.2.0" } }, { "crate": { "name": "displaydoc", "version": "0.2.7" } }, { "crate": { "name": "dyn-clone", "version": "1.0.20" } }, @@ -1594,8 +1694,10 @@ { "crate": { "name": "once_cell", "version": "1.21.4" } }, { "crate": { "name": "ordered-stream", "version": "0.2.0" } }, { "crate": { "name": "parking", "version": "2.2.1" } }, + { "crate": { "name": "paste", "version": "1.0.15" } }, { "crate": { "name": "pastey", "version": "0.2.3" } }, { "crate": { "name": "pin-project-lite", "version": "0.2.17" } }, + { "crate": { "name": "piper", "version": "0.2.5" } }, { "crate": { "name": "portable-atomic", "version": "1.15.0" } }, { "crate": { "name": "proc-macro-crate", "version": "3.5.0" } }, { "crate": { "name": "proc-macro2", "version": "1.0.107" } }, @@ -1763,9 +1865,19 @@ { "crate": { "name": "byteorder", "version": "1.5.0" } }, { "crate": { "name": "csv-core", "version": "0.1.13" } }, { "crate": { "name": "csv", "version": "1.4.0" } }, + { "crate": { "name": "jiff-core", "version": "0.1.1" } }, + { "crate": { "name": "jiff", "version": "0.2.37" } }, { "crate": { "name": "memchr", "version": "2.8.3" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "The MIT License (MIT)\n\nCopyright (c) 2015 Carl Lerche + nix-rust Authors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n", + "used_by": [ + { "crate": { "name": "nix", "version": "0.30.1" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -1874,6 +1986,7 @@ "text": "The MIT License (MIT)\n\nCopyright (c) 2019 The Crossbeam Project Developers\n\nPermission is hereby granted, free of charge, to any\nperson obtaining a copy of this software and associated\ndocumentation files (the \"Software\"), to deal in the\nSoftware without restriction, including without\nlimitation the rights to use, copy, modify, merge,\npublish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software\nis furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice\nshall be included in all copies or substantial portions\nof the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF\nANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\nTO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\nPARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\nSHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR\nIN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\nDEALINGS IN THE SOFTWARE.\n", "used_by": [ { "crate": { "name": "crossbeam-channel", "version": "0.5.17" } }, + { "crate": { "name": "crossbeam-deque", "version": "0.8.8" } }, { "crate": { "name": "crossbeam-epoch", "version": "0.9.21" } }, { "crate": { "name": "crossbeam-queue", "version": "0.3.14" } }, { "crate": { "name": "crossbeam-utils", "version": "0.8.23" } } @@ -1912,6 +2025,14 @@ { "crate": { "name": "crunchy", "version": "0.2.4" } } ] }, + { + "id": "MIT", + "name": "MIT License", + "text": "The MIT License (MIT)\n\nCopyright 2019 YesLogic Pty. Ltd.\nCopyright 2016 Manuel Reinhardt\nCopyright 2014 Austin Bonander\nCopyright 2013 The Servo Project Developers.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n", + "used_by": [ + { "crate": { "name": "yeslogic-fontconfig-sys", "version": "6.0.1" } } + ] + }, { "id": "MIT", "name": "MIT License", @@ -1953,6 +2074,18 @@ { "crate": { "name": "libm", "version": "0.2.16" } } ] }, + { + "id": "MPL-2.0", + "name": "Mozilla Public License 2.0", + "text": "Mozilla Public License Version 2.0\n==================================\n\n1. Definitions\n--------------\n\n1.1. \"Contributor\"\n means each individual or legal entity that creates, contributes to\n the creation of, or owns Covered Software.\n\n1.2. \"Contributor Version\"\n means the combination of the Contributions of others (if any) used\n by a Contributor and that particular Contributor's Contribution.\n\n1.3. \"Contribution\"\n means Covered Software of a particular Contributor.\n\n1.4. \"Covered Software\"\n means Source Code Form to which the initial Contributor has attached\n the notice in Exhibit A, the Executable Form of such Source Code\n Form, and Modifications of such Source Code Form, in each case\n including portions thereof.\n\n1.5. \"Incompatible With Secondary Licenses\"\n means\n\n (a) that the initial Contributor has attached the notice described\n in Exhibit B to the Covered Software; or\n\n (b) that the Covered Software was made available under the terms of\n version 1.1 or earlier of the License, but not also under the\n terms of a Secondary License.\n\n1.6. \"Executable Form\"\n means any form of the work other than Source Code Form.\n\n1.7. \"Larger Work\"\n means a work that combines Covered Software with other material, in\n a separate file or files, that is not Covered Software.\n\n1.8. \"License\"\n means this document.\n\n1.9. \"Licensable\"\n means having the right to grant, to the maximum extent possible,\n whether at the time of the initial grant or subsequently, any and\n all of the rights conveyed by this License.\n\n1.10. \"Modifications\"\n means any of the following:\n\n (a) any file in Source Code Form that results from an addition to,\n deletion from, or modification of the contents of Covered\n Software; or\n\n (b) any new file in Source Code Form that contains any Covered\n Software.\n\n1.11. \"Patent Claims\" of a Contributor\n means any patent claim(s), including without limitation, method,\n process, and apparatus claims, in any patent Licensable by such\n Contributor that would be infringed, but for the grant of the\n License, by the making, using, selling, offering for sale, having\n made, import, or transfer of either its Contributions or its\n Contributor Version.\n\n1.12. \"Secondary License\"\n means either the GNU General Public License, Version 2.0, the GNU\n Lesser General Public License, Version 2.1, the GNU Affero General\n Public License, Version 3.0, or any later versions of those\n licenses.\n\n1.13. \"Source Code Form\"\n means the form of the work preferred for making modifications.\n\n1.14. \"You\" (or \"Your\")\n means an individual or a legal entity exercising rights under this\n License. For legal entities, \"You\" includes any entity that\n controls, is controlled by, or is under common control with You. For\n purposes of this definition, \"control\" means (a) the power, direct\n or indirect, to cause the direction or management of such entity,\n whether by contract or otherwise, or (b) ownership of more than\n fifty percent (50%) of the outstanding shares or beneficial\n ownership of such entity.\n\n2. License Grants and Conditions\n--------------------------------\n\n2.1. Grants\n\nEach Contributor hereby grants You a world-wide, royalty-free,\nnon-exclusive license:\n\n(a) under intellectual property rights (other than patent or trademark)\n Licensable by such Contributor to use, reproduce, make available,\n modify, display, perform, distribute, and otherwise exploit its\n Contributions, either on an unmodified basis, with Modifications, or\n as part of a Larger Work; and\n\n(b) under Patent Claims of such Contributor to make, use, sell, offer\n for sale, have made, import, and otherwise transfer either its\n Contributions or its Contributor Version.\n\n2.2. Effective Date\n\nThe licenses granted in Section 2.1 with respect to any Contribution\nbecome effective for each Contribution on the date the Contributor first\ndistributes such Contribution.\n\n2.3. Limitations on Grant Scope\n\nThe licenses granted in this Section 2 are the only rights granted under\nthis License. No additional rights or licenses will be implied from the\ndistribution or licensing of Covered Software under this License.\nNotwithstanding Section 2.1(b) above, no patent license is granted by a\nContributor:\n\n(a) for any code that a Contributor has removed from Covered Software;\n or\n\n(b) for infringements caused by: (i) Your and any other third party's\n modifications of Covered Software, or (ii) the combination of its\n Contributions with other software (except as part of its Contributor\n Version); or\n\n(c) under Patent Claims infringed by Covered Software in the absence of\n its Contributions.\n\nThis License does not grant any rights in the trademarks, service marks,\nor logos of any Contributor (except as may be necessary to comply with\nthe notice requirements in Section 3.4).\n\n2.4. Subsequent Licenses\n\nNo Contributor makes additional grants as a result of Your choice to\ndistribute the Covered Software under a subsequent version of this\nLicense (see Section 10.2) or under the terms of a Secondary License (if\npermitted under the terms of Section 3.3).\n\n2.5. Representation\n\nEach Contributor represents that the Contributor believes its\nContributions are its original creation(s) or it has sufficient rights\nto grant the rights to its Contributions conveyed by this License.\n\n2.6. Fair Use\n\nThis License is not intended to limit any rights You have under\napplicable copyright doctrines of fair use, fair dealing, or other\nequivalents.\n\n2.7. Conditions\n\nSections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted\nin Section 2.1.\n\n3. Responsibilities\n-------------------\n\n3.1. Distribution of Source Form\n\nAll distribution of Covered Software in Source Code Form, including any\nModifications that You create or to which You contribute, must be under\nthe terms of this License. You must inform recipients that the Source\nCode Form of the Covered Software is governed by the terms of this\nLicense, and how they can obtain a copy of this License. You may not\nattempt to alter or restrict the recipients' rights in the Source Code\nForm.\n\n3.2. Distribution of Executable Form\n\nIf You distribute Covered Software in Executable Form then:\n\n(a) such Covered Software must also be made available in Source Code\n Form, as described in Section 3.1, and You must inform recipients of\n the Executable Form how they can obtain a copy of such Source Code\n Form by reasonable means in a timely manner, at a charge no more\n than the cost of distribution to the recipient; and\n\n(b) You may distribute such Executable Form under the terms of this\n License, or sublicense it under different terms, provided that the\n license for the Executable Form does not attempt to limit or alter\n the recipients' rights in the Source Code Form under this License.\n\n3.3. Distribution of a Larger Work\n\nYou may create and distribute a Larger Work under terms of Your choice,\nprovided that You also comply with the requirements of this License for\nthe Covered Software. If the Larger Work is a combination of Covered\nSoftware with a work governed by one or more Secondary Licenses, and the\nCovered Software is not Incompatible With Secondary Licenses, this\nLicense permits You to additionally distribute such Covered Software\nunder the terms of such Secondary License(s), so that the recipient of\nthe Larger Work may, at their option, further distribute the Covered\nSoftware under the terms of either this License or such Secondary\nLicense(s).\n\n3.4. Notices\n\nYou may not remove or alter the substance of any license notices\n(including copyright notices, patent notices, disclaimers of warranty,\nor limitations of liability) contained within the Source Code Form of\nthe Covered Software, except that You may alter any license notices to\nthe extent required to remedy known factual inaccuracies.\n\n3.5. Application of Additional Terms\n\nYou may choose to offer, and to charge a fee for, warranty, support,\nindemnity or liability obligations to one or more recipients of Covered\nSoftware. However, You may do so only on Your own behalf, and not on\nbehalf of any Contributor. You must make it absolutely clear that any\nsuch warranty, support, indemnity, or liability obligation is offered by\nYou alone, and You hereby agree to indemnify every Contributor for any\nliability incurred by such Contributor as a result of warranty, support,\nindemnity or liability terms You offer. You may include additional\ndisclaimers of warranty and limitations of liability specific to any\njurisdiction.\n\n4. Inability to Comply Due to Statute or Regulation\n---------------------------------------------------\n\nIf it is impossible for You to comply with any of the terms of this\nLicense with respect to some or all of the Covered Software due to\nstatute, judicial order, or regulation then You must: (a) comply with\nthe terms of this License to the maximum extent possible; and (b)\ndescribe the limitations and the code they affect. Such description must\nbe placed in a text file included with all distributions of the Covered\nSoftware under this License. Except to the extent prohibited by statute\nor regulation, such description must be sufficiently detailed for a\nrecipient of ordinary skill to be able to understand it.\n\n5. Termination\n--------------\n\n5.1. The rights granted under this License will terminate automatically\nif You fail to comply with any of its terms. However, if You become\ncompliant, then the rights granted under this License from a particular\nContributor are reinstated (a) provisionally, unless and until such\nContributor explicitly and finally terminates Your grants, and (b) on an\nongoing basis, if such Contributor fails to notify You of the\nnon-compliance by some reasonable means prior to 60 days after You have\ncome back into compliance. Moreover, Your grants from a particular\nContributor are reinstated on an ongoing basis if such Contributor\nnotifies You of the non-compliance by some reasonable means, this is the\nfirst time You have received notice of non-compliance with this License\nfrom such Contributor, and You become compliant prior to 30 days after\nYour receipt of the notice.\n\n5.2. If You initiate litigation against any entity by asserting a patent\ninfringement claim (excluding declaratory judgment actions,\ncounter-claims, and cross-claims) alleging that a Contributor Version\ndirectly or indirectly infringes any patent, then the rights granted to\nYou by any and all Contributors for the Covered Software under Section\n2.1 of this License shall terminate.\n\n5.3. In the event of termination under Sections 5.1 or 5.2 above, all\nend user license agreements (excluding distributors and resellers) which\nhave been validly granted by You or Your distributors under this License\nprior to termination shall survive termination.\n\n************************************************************************\n* *\n* 6. Disclaimer of Warranty *\n* ------------------------- *\n* *\n* Covered Software is provided under this License on an \"as is\" *\n* basis, without warranty of any kind, either expressed, implied, or *\n* statutory, including, without limitation, warranties that the *\n* Covered Software is free of defects, merchantable, fit for a *\n* particular purpose or non-infringing. The entire risk as to the *\n* quality and performance of the Covered Software is with You. *\n* Should any Covered Software prove defective in any respect, You *\n* (not any Contributor) assume the cost of any necessary servicing, *\n* repair, or correction. This disclaimer of warranty constitutes an *\n* essential part of this License. No use of any Covered Software is *\n* authorized under this License except under this disclaimer. *\n* *\n************************************************************************\n\n************************************************************************\n* *\n* 7. Limitation of Liability *\n* -------------------------- *\n* *\n* Under no circumstances and under no legal theory, whether tort *\n* (including negligence), contract, or otherwise, shall any *\n* Contributor, or anyone who distributes Covered Software as *\n* permitted above, be liable to You for any direct, indirect, *\n* special, incidental, or consequential damages of any character *\n* including, without limitation, damages for lost profits, loss of *\n* goodwill, work stoppage, computer failure or malfunction, or any *\n* and all other commercial damages or losses, even if such party *\n* shall have been informed of the possibility of such damages. This *\n* limitation of liability shall not apply to liability for death or *\n* personal injury resulting from such party's negligence to the *\n* extent applicable law prohibits such limitation. Some *\n* jurisdictions do not allow the exclusion or limitation of *\n* incidental or consequential damages, so this exclusion and *\n* limitation may not apply to You. *\n* *\n************************************************************************\n\n8. Litigation\n-------------\n\nAny litigation relating to this License may be brought only in the\ncourts of a jurisdiction where the defendant maintains its principal\nplace of business and such litigation shall be governed by laws of that\njurisdiction, without reference to its conflict-of-law provisions.\nNothing in this Section shall prevent a party's ability to bring\ncross-claims or counter-claims.\n\n9. Miscellaneous\n----------------\n\nThis License represents the complete agreement concerning the subject\nmatter hereof. If any provision of this License is held to be\nunenforceable, such provision shall be reformed only to the extent\nnecessary to make it enforceable. Any law or regulation which provides\nthat the language of a contract shall be construed against the drafter\nshall not be used to construe this License against a Contributor.\n\n10. Versions of the License\n---------------------------\n\n10.1. New Versions\n\nMozilla Foundation is the license steward. Except as provided in Section\n10.3, no one other than the license steward has the right to modify or\npublish new versions of this License. Each version will be given a\ndistinguishing version number.\n\n10.2. Effect of New Versions\n\nYou may distribute the Covered Software under the terms of the version\nof the License under which You originally received the Covered Software,\nor under the terms of any subsequent version published by the license\nsteward.\n\n10.3. Modified Versions\n\nIf you create software not governed by this License, and you want to\ncreate a new license for such software, you may create and use a\nmodified version of this License if you rename the license and remove\nany references to the name of the license steward (except to note that\nsuch modified license differs from this License).\n\n10.4. Distributing Source Code Form that is Incompatible With Secondary\nLicenses\n\nIf You choose to distribute Source Code Form that is Incompatible With\nSecondary Licenses under the terms of this version of the License, the\nnotice described in Exhibit B of this License must be attached.\n\nExhibit A - Source Code Form License Notice\n-------------------------------------------\n\n This Source Code Form is subject to the terms of the Mozilla Public\n License, v. 2.0. If a copy of the MPL was not distributed with this\n file, You can obtain one at https://mozilla.org/MPL/2.0/.\n\nIf it is not possible or desirable to put the notice in a particular\nfile, then You may include the notice in a location (such as a LICENSE\nfile in a relevant directory) where a recipient would be likely to look\nfor such a notice.\n\nYou may add additional accurate notices of copyright ownership.\n\nExhibit B - \"Incompatible With Secondary Licenses\" Notice\n---------------------------------------------------------\n\n This Source Code Form is \"Incompatible With Secondary Licenses\", as\n defined by the Mozilla Public License, v. 2.0.\n", + "used_by": [ + { "crate": { "name": "glycin-common", "version": "1.0.4" } }, + { "crate": { "name": "glycin-utils", "version": "4.1.0" } }, + { "crate": { "name": "glycin", "version": "3.1.0" } }, + { "crate": { "name": "gufo-common", "version": "1.1.0" } }, + { "crate": { "name": "gufo-exif", "version": "0.4.0" } } + ] + }, { "id": "Unicode-3.0", "name": "Unicode License v3", @@ -2272,8 +2405,10 @@ { "name": "arrow-string", "version": "58.4.0", "license": "Apache-2.0" }, { "name": "ashpd", "version": "0.13.13", "license": "MIT" }, { "name": "async-broadcast", "version": "0.7.2", "license": "MIT OR Apache-2.0" }, + { "name": "async-channel", "version": "2.5.0", "license": "Apache-2.0 OR MIT" }, { "name": "async-lock", "version": "3.4.2", "license": "Apache-2.0 OR MIT" }, { "name": "async-recursion", "version": "1.1.1", "license": "MIT OR Apache-2.0" }, + { "name": "async-task", "version": "4.7.1", "license": "Apache-2.0 OR MIT" }, { "name": "async-trait", "version": "0.1.92", "license": "MIT OR Apache-2.0" }, { "name": "asynchronous-codec", "version": "0.7.0", "license": "MIT" }, { "name": "atoi", "version": "2.0.0", "license": "MIT" }, @@ -2288,10 +2423,12 @@ { "name": "block-buffer", "version": "0.10.4", "license": "MIT OR Apache-2.0" }, { "name": "block-buffer", "version": "0.12.1", "license": "MIT OR Apache-2.0" }, { "name": "block-padding", "version": "0.3.3", "license": "MIT OR Apache-2.0" }, + { "name": "blocking", "version": "1.7.0", "license": "Apache-2.0 OR MIT" }, { "name": "bnum", "version": "0.13.0", "license": "MIT OR Apache-2.0" }, { "name": "bson", "version": "3.1.0", "license": "MIT" }, { "name": "bstr", "version": "1.13.1", "license": "MIT OR Apache-2.0" }, { "name": "bumpalo", "version": "3.20.3", "license": "MIT OR Apache-2.0" }, + { "name": "bytemuck", "version": "1.25.2", "license": "Zlib OR Apache-2.0 OR MIT" }, { "name": "byteorder", "version": "1.5.0", "license": "Unlicense OR MIT" }, { "name": "bytes", "version": "1.12.1", "license": "MIT" }, { "name": "cairo-rs", "version": "0.22.9", "license": "MIT" }, @@ -2308,6 +2445,7 @@ { "name": "cmov", "version": "0.5.4", "license": "Apache-2.0 OR MIT" }, { "name": "combine", "version": "4.6.8", "license": "MIT" }, { "name": "comfy-table", "version": "7.1.4", "license": "MIT" }, + { "name": "concurrent-queue", "version": "2.5.0", "license": "Apache-2.0 OR MIT" }, { "name": "connection-string", "version": "0.2.0", "license": "MIT OR Apache-2.0" }, { "name": "const-oid", "version": "0.9.6", "license": "Apache-2.0 OR MIT" }, { "name": "const-oid", "version": "0.10.2", "license": "Apache-2.0 OR MIT" }, @@ -2323,6 +2461,7 @@ { "name": "crc32fast", "version": "1.5.2", "license": "MIT OR Apache-2.0" }, { "name": "critical-section", "version": "1.2.0", "license": "MIT OR Apache-2.0" }, { "name": "crossbeam-channel", "version": "0.5.17", "license": "MIT OR Apache-2.0" }, + { "name": "crossbeam-deque", "version": "0.8.8", "license": "MIT OR Apache-2.0" }, { "name": "crossbeam-epoch", "version": "0.9.21", "license": "MIT OR Apache-2.0" }, { "name": "crossbeam-queue", "version": "0.3.14", "license": "MIT OR Apache-2.0" }, { "name": "crossbeam-utils", "version": "0.8.23", "license": "MIT OR Apache-2.0" }, @@ -2354,6 +2493,7 @@ { "name": "digest", "version": "0.10.7", "license": "MIT OR Apache-2.0" }, { "name": "digest", "version": "0.11.3", "license": "MIT OR Apache-2.0" }, { "name": "displaydoc", "version": "0.2.7", "license": "MIT OR Apache-2.0" }, + { "name": "dlib", "version": "0.5.3", "license": "MIT" }, { "name": "dotenvy", "version": "0.15.7", "license": "MIT" }, { "name": "duckdb", "version": "1.10506.1", "license": "MIT" }, { "name": "dyn-clone", "version": "1.0.20", "license": "MIT OR Apache-2.0" }, @@ -2362,6 +2502,8 @@ { "name": "endi", "version": "1.1.1", "license": "MIT" }, { "name": "enumflags2", "version": "0.7.12", "license": "MIT OR Apache-2.0" }, { "name": "enumflags2_derive", "version": "0.7.12", "license": "MIT OR Apache-2.0" }, + { "name": "env_filter", "version": "2.0.0", "license": "MIT OR Apache-2.0" }, + { "name": "env_logger", "version": "0.11.11", "license": "MIT OR Apache-2.0" }, { "name": "equivalent", "version": "1.0.2", "license": "Apache-2.0 OR MIT" }, { "name": "errno", "version": "0.3.14", "license": "MIT OR Apache-2.0" }, { "name": "event-listener", "version": "5.4.2", "license": "Apache-2.0 OR MIT" }, @@ -2377,6 +2519,9 @@ { "name": "flume", "version": "0.12.0", "license": "Apache-2.0 OR MIT" }, { "name": "foldhash", "version": "0.1.5", "license": "Zlib" }, { "name": "foldhash", "version": "0.2.0", "license": "Zlib" }, + { "name": "foreign-types", "version": "0.5.0", "license": "MIT OR Apache-2.0" }, + { "name": "foreign-types-macros", "version": "0.2.4", "license": "MIT OR Apache-2.0" }, + { "name": "foreign-types-shared", "version": "0.3.1", "license": "MIT OR Apache-2.0" }, { "name": "form_urlencoded", "version": "1.2.2", "license": "MIT OR Apache-2.0" }, { "name": "fragile", "version": "2.1.0", "license": "Apache-2.0" }, { "name": "funty", "version": "2.0.0", "license": "MIT" }, @@ -2409,6 +2554,9 @@ { "name": "glib", "version": "0.22.9", "license": "MIT" }, { "name": "glib-macros", "version": "0.22.9", "license": "MIT" }, { "name": "glib-sys", "version": "0.22.9", "license": "MIT" }, + { "name": "glycin", "version": "3.1.0", "license": "MPL-2.0 OR LGPL-2.1-or-later" }, + { "name": "glycin-common", "version": "1.0.4", "license": "MPL-2.0 OR LGPL-2.1-or-later" }, + { "name": "glycin-utils", "version": "4.1.0", "license": "MPL-2.0 OR LGPL-2.1-or-later" }, { "name": "gobject-sys", "version": "0.22.9", "license": "MIT" }, { "name": "graphene-rs", "version": "0.22.8", "license": "MIT" }, { "name": "graphene-sys", "version": "0.22.9", "license": "MIT" }, @@ -2417,6 +2565,8 @@ { "name": "gtk4", "version": "0.11.4", "license": "MIT" }, { "name": "gtk4-macros", "version": "0.11.4", "license": "MIT" }, { "name": "gtk4-sys", "version": "0.11.4", "license": "MIT" }, + { "name": "gufo-common", "version": "1.1.0", "license": "MPL-2.0 OR LGPL-2.1-or-later" }, + { "name": "gufo-exif", "version": "0.4.0", "license": "MPL-2.0 OR LGPL-2.1-or-later" }, { "name": "half", "version": "2.7.1", "license": "MIT OR Apache-2.0" }, { "name": "hashbrown", "version": "0.14.5", "license": "MIT OR Apache-2.0" }, { "name": "hashbrown", "version": "0.15.5", "license": "MIT OR Apache-2.0" }, @@ -2461,7 +2611,11 @@ { "name": "ipnet", "version": "2.12.2", "license": "MIT OR Apache-2.0" }, { "name": "itertools", "version": "0.15.0", "license": "MIT OR Apache-2.0" }, { "name": "itoa", "version": "1.0.18", "license": "MIT OR Apache-2.0" }, + { "name": "jiff", "version": "0.2.37", "license": "Unlicense OR MIT" }, + { "name": "jiff-core", "version": "0.1.1", "license": "Unlicense OR MIT" }, { "name": "lazy_static", "version": "1.5.0", "license": "MIT OR Apache-2.0" }, + { "name": "lcms2", "version": "6.2.0", "license": "MIT" }, + { "name": "lcms2-sys", "version": "4.0.7", "license": "MIT" }, { "name": "lexical-core", "version": "1.0.6", "license": "MIT OR Apache-2.0" }, { "name": "lexical-parse-float", "version": "1.0.6", "license": "MIT OR Apache-2.0" }, { "name": "lexical-parse-integer", "version": "1.0.6", "license": "MIT OR Apache-2.0" }, @@ -2474,7 +2628,10 @@ { "name": "libduckdb-sys", "version": "1.10506.1", "license": "Apache-2.0 AND (Apache-2.0 OR GPL-2.0-or-later) AND BSD-2-Clause AND (BSD-2-Clause OR BSL-1.0) AND BSD-3-Clause AND BSL-1.0 AND MIT AND Unicode-DFS-2015 AND Zlib" }, { "name": "libgssapi", "version": "0.11.0", "license": "MIT" }, { "name": "libgssapi-sys", "version": "0.3.4", "license": "MIT" }, + { "name": "libloading", "version": "0.8.9", "license": "ISC" }, { "name": "libm", "version": "0.2.16", "license": "MIT" }, + { "name": "libseccomp", "version": "0.4.0", "license": "MIT OR Apache-2.0" }, + { "name": "libseccomp-sys", "version": "0.3.0", "license": "MIT OR Apache-2.0" }, { "name": "libsqlite3-sys", "version": "0.37.0", "license": "MIT" }, { "name": "linux-raw-sys", "version": "0.4.15", "license": "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT" }, { "name": "linux-raw-sys", "version": "0.12.1", "license": "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT" }, @@ -2491,6 +2648,8 @@ { "name": "md-5", "version": "0.10.6", "license": "MIT OR Apache-2.0" }, { "name": "md-5", "version": "0.11.0", "license": "MIT OR Apache-2.0" }, { "name": "memchr", "version": "2.8.3", "license": "Unlicense OR MIT" }, + { "name": "memfd", "version": "0.6.6", "license": "MIT OR Apache-2.0" }, + { "name": "memmap2", "version": "0.9.12", "license": "MIT OR Apache-2.0" }, { "name": "memoffset", "version": "0.9.1", "license": "MIT" }, { "name": "mio", "version": "1.2.3", "license": "MIT" }, { "name": "moka", "version": "0.12.16", "license": "(MIT OR Apache-2.0) AND Apache-2.0" }, @@ -2499,6 +2658,7 @@ { "name": "multiversion", "version": "0.9.0", "license": "MIT OR Apache-2.0" }, { "name": "multiversion-macros", "version": "0.9.0", "license": "MIT OR Apache-2.0" }, { "name": "multiversion_no_op", "version": "1.0.0", "license": "Apache-2.0 OR MIT" }, + { "name": "nix", "version": "0.30.1", "license": "MIT" }, { "name": "nu-ansi-term", "version": "0.50.3", "license": "MIT" }, { "name": "num", "version": "0.4.3", "license": "MIT OR Apache-2.0" }, { "name": "num-bigint", "version": "0.4.8", "license": "MIT OR Apache-2.0" }, @@ -2518,6 +2678,7 @@ { "name": "parking", "version": "2.2.1", "license": "Apache-2.0 OR MIT" }, { "name": "parking_lot", "version": "0.12.5", "license": "MIT OR Apache-2.0" }, { "name": "parking_lot_core", "version": "0.9.12", "license": "MIT OR Apache-2.0" }, + { "name": "paste", "version": "1.0.15", "license": "MIT OR Apache-2.0" }, { "name": "pastey", "version": "0.2.3", "license": "MIT OR Apache-2.0" }, { "name": "pbkdf2", "version": "0.12.2", "license": "MIT OR Apache-2.0" }, { "name": "pbkdf2", "version": "0.13.0", "license": "MIT OR Apache-2.0" }, @@ -2526,6 +2687,7 @@ { "name": "phf", "version": "0.13.1", "license": "MIT" }, { "name": "phf_shared", "version": "0.13.1", "license": "MIT" }, { "name": "pin-project-lite", "version": "0.2.17", "license": "Apache-2.0 OR MIT" }, + { "name": "piper", "version": "0.2.5", "license": "MIT OR Apache-2.0" }, { "name": "pkcs1", "version": "0.8.0-rc.4", "license": "Apache-2.0 OR MIT" }, { "name": "pkcs8", "version": "0.11.0", "license": "Apache-2.0 OR MIT" }, { "name": "portable-atomic", "version": "1.15.0", "license": "Apache-2.0 OR MIT" }, @@ -2547,6 +2709,8 @@ { "name": "rand_core", "version": "0.9.5", "license": "MIT OR Apache-2.0" }, { "name": "rand_core", "version": "0.10.1", "license": "MIT OR Apache-2.0" }, { "name": "rand_pcg", "version": "0.9.0", "license": "MIT OR Apache-2.0" }, + { "name": "rayon", "version": "1.12.0", "license": "MIT OR Apache-2.0" }, + { "name": "rayon-core", "version": "1.13.0", "license": "MIT OR Apache-2.0" }, { "name": "redis", "version": "1.7.1", "license": "BSD-3-Clause" }, { "name": "ref-cast", "version": "1.0.27", "license": "MIT OR Apache-2.0" }, { "name": "ref-cast-impl", "version": "1.0.27", "license": "MIT OR Apache-2.0" }, @@ -2557,6 +2721,8 @@ { "name": "relm4-macros", "version": "0.11.0", "license": "Apache-2.0 OR MIT" }, { "name": "resolv-conf", "version": "0.7.6", "license": "MIT OR Apache-2.0" }, { "name": "rmcp", "version": "3.6.0", "license": "Apache-2.0 AND MIT" }, + { "name": "rmp", "version": "0.8.15", "license": "MIT" }, + { "name": "rmp-serde", "version": "1.3.1", "license": "MIT" }, { "name": "rsa", "version": "0.10.0-rc.18", "license": "MIT OR Apache-2.0" }, { "name": "rust_xlsxwriter", "version": "0.102.0", "license": "MIT OR Apache-2.0" }, { "name": "rustc_version", "version": "0.4.1", "license": "MIT OR Apache-2.0" }, @@ -2617,6 +2783,7 @@ { "name": "sqlx-sqlite", "version": "0.9.0", "license": "MIT OR Apache-2.0" }, { "name": "sse-stream", "version": "0.2.6", "license": "MIT OR Apache-2.0" }, { "name": "stable_deref_trait", "version": "1.2.1", "license": "MIT OR Apache-2.0" }, + { "name": "static_assertions", "version": "1.1.0", "license": "MIT OR Apache-2.0" }, { "name": "stringprep", "version": "0.1.5", "license": "MIT OR Apache-2.0" }, { "name": "strsim", "version": "0.11.1", "license": "MIT" }, { "name": "strum", "version": "0.27.2", "license": "MIT" }, @@ -2685,6 +2852,7 @@ { "name": "wyz", "version": "0.5.1", "license": "MIT" }, { "name": "x509-cert", "version": "0.2.5", "license": "Apache-2.0 OR MIT" }, { "name": "xxhash-rust", "version": "0.8.19", "license": "BSL-1.0" }, + { "name": "yeslogic-fontconfig-sys", "version": "6.0.1", "license": "MIT" }, { "name": "yoke", "version": "0.8.3", "license": "Unicode-3.0" }, { "name": "yoke-derive", "version": "0.8.2", "license": "Unicode-3.0" }, { "name": "zbus", "version": "5.19.0", "license": "MIT" }, diff --git a/linux/data/resources/ui/row-detail-pane.ui b/linux/data/resources/ui/row-detail-pane.ui index 17c41799c4..cc1c2f8c1d 100644 --- a/linux/data/resources/ui/row-detail-pane.ui +++ b/linux/data/resources/ui/row-detail-pane.ui @@ -316,13 +316,23 @@ + + + false + scale-down + 6 + 12 + 12 + Image preview + + true - + never 120 diff --git a/linux/docs/decisions/0029-images-decode-in-glycin.md b/linux/docs/decisions/0029-images-decode-in-glycin.md new file mode 100644 index 0000000000..270ea82b05 --- /dev/null +++ b/linux/docs/decisions/0029-images-decode-in-glycin.md @@ -0,0 +1,32 @@ +# 0029: Images from the database decode in glycin's sandbox + +- **Status**: Accepted +- **Date**: 2026-10-29 + +## Context + +Row Details shows the picture of a binary value that holds an image. Those bytes are written by whoever else uses the database, so they are untrusted input (`.oss-scanner/threat_model.md`). GTK's own loaders run in the app's process, and `gdk_texture_new_from_bytes` says it "should not be used with untrusted data. Use a proper image loading framework such as libglycin" (GTK 4.22, `gdk/gdktexture.c`). GNOME 50 ships glycin: gdk-pixbuf 2.44 loads through it, and Loupe and Fractal decode untrusted images with it. + +## Decision + +The app decodes a value's picture with the glycin crate 3.x, the GNOME 50 release, which runs each decode in a sandboxed loader process. + +## Rationale + +- **Out of process.** A loader that a crafted image crashes or takes over runs under bubblewrap, or `flatpak-spawn --sandbox` in Flatpak, with seccomp filters, a memory limit and no network. +- **The platform's loaders.** The formats are the ones the installed glycin loaders read, the same set the rest of the desktop shows. +- **Fits the workspace.** The crate's `tokio` feature runs on the app's runtime and the zbus it already links; `gdk4` hands back a `gdk::Texture`. + +## Consequences + +- The build links libseccomp and lcms2 (`meson.build`, `build-aux/debian/control`, `build-aux/rpm/tablepro.spec`). The GNOME SDK has both, since it builds libglycin. +- The picture needs the glycin loaders at run time: in the GNOME runtime, a Recommends of the .deb and the .rpm, and a test dependency of both, since the picture tests decode. Without them Row Details shows the bytes and no picture. +- Where no sandbox can be made, as in a container that blocks namespaces or a Flatpak run by `flatpak-builder`, glycin's own default decodes without one. The pane shows no picture there instead. The tests allow it, since CI runs them in containers. +- glycin 3.x speaks the loaders' `2+` protocol. Moving to glycin 4 follows the platform floor ([0015](0015-gnome-50-platform-floor.md)). + +## Alternatives considered + +- **`gdk::Texture::from_bytes`**: decodes in process, which GTK documents against for untrusted data. +- **gdk-pixbuf**: sandboxed only where the distribution builds it with glycin, and its texture conversion is deprecated since GTK 4.20. +- **The image crate in process**: memory-safe decoders, but no process boundary or memory limit, and a second set of formats beside the desktop's. +- **No picture**: leaves the bytes of an image unreadable in Row Details. diff --git a/linux/docs/decisions/README.md b/linux/docs/decisions/README.md index 4c44a55441..2c26060651 100644 --- a/linux/docs/decisions/README.md +++ b/linux/docs/decisions/README.md @@ -76,6 +76,7 @@ A fact the repository keeps elsewhere, such as a workflow trigger, a branch, a v | [0026](0026-documents-in-the-table-tab.md) | Document collections open in the table tab, and their statements are shell calls | Accepted | A document engine states `DataModel::Document`, lists collections as tables, opens them in the table tab, and every builder writes a mongosh call with bound values through a document arm chosen by the grammar. | | [0027](0027-one-data-grid.md) | One data grid on GtkColumnView, with rows and a focused cell | Accepted | Every surface shows rows in one `DataGrid`: row selection plus a focused cell, stable row slots, a row number column, values as text and a row detail pane. | | [0028](0028-sessions-share-a-window.md) | A window holds several sessions in one tab view | Accepted | A `WorkspaceShell` per window shares one tab view among its sessions, each with its own workspace; the sidebar is a tree of the saved connections with each open session's schemas and tables. | +| [0029](0029-images-decode-in-glycin.md) | Images from the database decode in glycin's sandbox | Accepted | Row Details decodes a value's picture with the glycin crate 3.x, in a sandboxed loader process, never with GTK's in-process loaders. | 0011 is not used. It was reserved for the DDL decision, which was recorded as 0010, and docs cite ADRs by number, so the later ones keep theirs. diff --git a/linux/meson.build b/linux/meson.build index f75a9b87ae..02fa04de3a 100644 --- a/linux/meson.build +++ b/linux/meson.build @@ -40,6 +40,9 @@ dependency('gtk4', version: '>= 4.22') dependency('libadwaita-1', version: '>= 1.9') dependency('gtksourceview-5', version: '>= 5.18') dependency('sqlite3', version: '>= 3.46') +# glycin, which decodes a value's picture in a sandbox. +dependency('libseccomp') +dependency('lcms2') if get_option('kerberos') dependency('mit-krb5-gssapi') diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index 5fab1859c7..bde86fdf4b 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -88,6 +88,7 @@ crates/app/src/services/grid/edit_policy.rs crates/app/src/services/grid/find_matches.rs crates/app/src/services/grid/grid_column.rs crates/app/src/services/grid/grid_row_id.rs +crates/app/src/services/grid/hex_dump.rs crates/app/src/services/grid/mod.rs crates/app/src/services/grid/row_key_builder.rs crates/app/src/services/grid/row_snapshot.rs @@ -267,6 +268,7 @@ crates/app/src/ui/data_grid/row_detail/clock.rs crates/app/src/ui/data_grid/row_detail/fields.rs crates/app/src/ui/data_grid/row_detail/imp.rs crates/app/src/ui/data_grid/row_detail/mod.rs +crates/app/src/ui/data_grid/row_detail/picture.rs crates/app/src/ui/data_grid/row_detail/tests.rs crates/app/src/ui/data_grid/row_detail/value_editor.rs crates/app/src/ui/data_grid/row_json_dialog.rs From 20644df392a8a100cae849b8284eb207e98ed8f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 16:36:21 +0700 Subject: [PATCH 20/85] fix(linux): read SQL as each engine does and preview connection imports (#3892) --- linux/ARCHITECTURE.md | 2 +- linux/changelog.d/3892.added.md | 1 + linux/changelog.d/3892.fixed.md | 1 + linux/changelog.d/3892.security.1.md | 1 + linux/changelog.d/3892.security.2.md | 1 + linux/changelog.d/3892.security.md | 1 + .../app/src/ui/editor/safe_mode_gate.rs | 17 + linux/crates/app/src/ui/editor/sql_file.rs | 16 +- linux/crates/app/src/ui/import_preview.rs | 135 +++++ linux/crates/app/src/ui/mod.rs | 1 + .../app/src/ui/session_window/connections.rs | 103 +++- linux/crates/app/src/ui/session_window/mod.rs | 1 + .../ui/session_window/session_window_input.rs | 2 + linux/crates/core/src/dialect/test_dialect.rs | 1 + linux/crates/core/src/lib.rs | 1 + .../core/src/sql_syntax/credential_rules.rs | 71 ++- .../core/src/sql_syntax/history_recording.rs | 134 ++++- linux/crates/core/src/sql_syntax/mod.rs | 3 +- .../core/src/sql_syntax/script/lexer.rs | 86 ++- .../src/sql_syntax/script/script_rules.rs | 22 + .../sql_syntax/state_changing_functions.rs | 200 +++++++ .../core/src/sql_syntax/statement_access.rs | 527 ++++++++++++++++-- linux/crates/core/src/unicode_blank.rs | 50 ++ linux/crates/core/tests/script_corpus.rs | 118 ++++ .../crates/drivers/cassandra/src/cql_type.rs | 64 ++- linux/crates/drivers/cassandra/src/dialect.rs | 1 + linux/crates/drivers/cassandra/src/rows.rs | 3 +- .../drivers/clickhouse/src/clickhouse_type.rs | 68 +++ .../crates/drivers/clickhouse/src/dialect.rs | 1 + linux/crates/drivers/duckdb/src/dialect.rs | 1 + linux/crates/drivers/mongodb/src/catalog.rs | 5 +- .../crates/drivers/mongodb/src/cursor_rows.rs | 5 +- linux/crates/drivers/mongodb/src/execute.rs | 2 +- linux/crates/drivers/mongodb/src/lib.rs | 1 + .../drivers/mongodb/src/nested_document.rs | 84 +++ linux/crates/drivers/mongodb/src/rows.rs | 9 +- linux/crates/drivers/mssql/src/dialect.rs | 57 ++ linux/crates/drivers/mysql/src/dialect.rs | 1 + linux/crates/drivers/mysql/src/stream_read.rs | 34 +- .../drivers/mysql/tests/integration/main.rs | 1 + .../mysql/tests/integration/user_read.rs | 46 ++ .../postgres/src/catalog/pg_column_row.rs | 16 +- .../postgres/src/catalog/table_definition.rs | 30 +- .../postgres/src/codec/array_decode.rs | 63 ++- .../drivers/postgres/src/codec/decode.rs | 25 +- .../crates/drivers/postgres/src/codec/mod.rs | 2 + .../drivers/postgres/src/codec/text_array.rs | 76 +++ .../drivers/postgres/src/commit_resolution.rs | 4 +- linux/crates/drivers/postgres/src/ddl.rs | 36 +- linux/crates/drivers/postgres/src/dialect.rs | 1 + .../drivers/postgres/src/metadata/estimate.rs | 8 +- .../postgres/src/metadata/foreign_keys.rs | 13 +- .../drivers/postgres/src/metadata/identity.rs | 15 +- .../drivers/postgres/src/metadata/indexes.rs | 6 +- .../drivers/postgres/src/metadata/mod.rs | 79 ++- .../drivers/postgres/src/metadata/objects.rs | 14 +- .../drivers/postgres/src/stream_read.rs | 15 + .../postgres/tests/integration/metadata.rs | 79 +++ .../postgres/tests/integration/read_only.rs | 69 ++- linux/crates/drivers/sqlite/src/connection.rs | 21 +- linux/crates/drivers/sqlite/src/dialect.rs | 1 + linux/crates/drivers/sqlite/src/lib.rs | 1 + linux/crates/drivers/sqlite/src/read_guard.rs | 123 ++++ .../sqlite/tests/integration/sessions.rs | 44 ++ linux/crates/mcp/src/query_tools.rs | 8 + linux/crates/storage/src/connections.rs | 292 +++++++++- linux/crates/storage/src/error.rs | 3 + linux/crates/storage/src/lib.rs | 4 +- linux/crates/storage/src/name_rules.rs | 34 ++ linux/crates/test-support/src/fake_dialect.rs | 1 + linux/docs/drivers/mysql.md | 2 +- linux/docs/drivers/postgresql.md | 3 +- linux/docs/drivers/sqlite.md | 2 + linux/docs/storage.md | 11 +- linux/po/POTFILES.in | 1 + 75 files changed, 2767 insertions(+), 213 deletions(-) create mode 100644 linux/changelog.d/3892.added.md create mode 100644 linux/changelog.d/3892.fixed.md create mode 100644 linux/changelog.d/3892.security.1.md create mode 100644 linux/changelog.d/3892.security.2.md create mode 100644 linux/changelog.d/3892.security.md create mode 100644 linux/crates/app/src/ui/import_preview.rs create mode 100644 linux/crates/core/src/sql_syntax/state_changing_functions.rs create mode 100644 linux/crates/core/src/unicode_blank.rs create mode 100644 linux/crates/drivers/mongodb/src/nested_document.rs create mode 100644 linux/crates/drivers/mysql/tests/integration/user_read.rs create mode 100644 linux/crates/drivers/postgres/src/codec/text_array.rs create mode 100644 linux/crates/drivers/sqlite/src/read_guard.rs diff --git a/linux/ARCHITECTURE.md b/linux/ARCHITECTURE.md index c834704b6d..1ad217eadf 100644 --- a/linux/ARCHITECTURE.md +++ b/linux/ARCHITECTURE.md @@ -348,7 +348,7 @@ A restore deletes the drafts no saved or open tab names, except while a moved-as The app's `McpService` (`crates/app/src/services/mcp_service.rs`) follows the `mcp-server-enabled` and `mcp-server-port` keys on the GTK thread and starts, stops and moves the server on the runtime. Every change takes one lock in turn, so a stop has freed the port before the next start binds it. Its status, a `watch` of `McpStatus`, drives the Preferences page. -A tool reads the saved connections from the `ConnectionStore` on each call, so an External Clients level changed a moment ago applies, and leaves a Blocked one out as if it did not exist. It reaches a connection through `McpHost`, which the app's `GtkMcpHost` answers on the GTK thread: the `Session` the registry holds for the saved connection's key, a window opened for it the way the picker opens one, and the write approval, which `confirm_until` shows in that window with Safe Mode's dialog and takes down when the client goes away. `ExternalAccess::gate` decides each statement from the level, the session's Safe Mode level and `statement_access`. A read streams through `Connection::stream_read` on a read-only connection, cut at the row limit by the sink; a write runs on an editor session of its own, opened for the one statement. Both go into the query history through `HistoryService::record`, under the editor's rules. +A tool reads the saved connections from the `ConnectionStore` on each call, so an External Clients level changed a moment ago applies, and leaves a Blocked one out as if it did not exist. It reaches a connection through `McpHost`, which the app's `GtkMcpHost` answers on the GTK thread: the `Session` the registry holds for the saved connection's key, a window opened for it the way the picker opens one, and the write approval, which `confirm_until` shows in that window with Safe Mode's dialog and takes down when the client goes away. `ExternalAccess::gate` decides each statement from the level, the session's Safe Mode level and `statement_access`. A read streams through `Connection::stream_read` on a read-only connection, cut at the row limit by the sink, and the engine confirms it is one statement (PostgreSQL's Parse, a SQLite prepare with no tail, a MySQL prepare), so text the gate read as one statement never runs as two; a SQLite read connection also attaches no file and sets no pragma that changes anything; a write runs on an editor session of its own, opened for the one statement. Both go into the query history through `HistoryService::record`, under the editor's rules. ## AI assistant diff --git a/linux/changelog.d/3892.added.md b/linux/changelog.d/3892.added.md new file mode 100644 index 0000000000..164f11e973 --- /dev/null +++ b/linux/changelog.d/3892.added.md @@ -0,0 +1 @@ +Importing connections first lists each one with its host, and nothing is saved until you choose Import. diff --git a/linux/changelog.d/3892.fixed.md b/linux/changelog.d/3892.fixed.md new file mode 100644 index 0000000000..3d588d618e --- /dev/null +++ b/linux/changelog.d/3892.fixed.md @@ -0,0 +1 @@ +Query history no longer keeps SQL Server password procedures or passwords run as dynamic SQL. diff --git a/linux/changelog.d/3892.security.1.md b/linux/changelog.d/3892.security.1.md new file mode 100644 index 0000000000..596254b9ca --- /dev/null +++ b/linux/changelog.d/3892.security.1.md @@ -0,0 +1 @@ +Browsing PostgreSQL no longer runs look-alike functions or operators that another user put in a shared schema. diff --git a/linux/changelog.d/3892.security.2.md b/linux/changelog.d/3892.security.2.md new file mode 100644 index 0000000000..0bad4585fc --- /dev/null +++ b/linux/changelog.d/3892.security.2.md @@ -0,0 +1 @@ +A hostile server can no longer crash the app with an oversized array or a deeply nested type or document. diff --git a/linux/changelog.d/3892.security.md b/linux/changelog.d/3892.security.md new file mode 100644 index 0000000000..a3d009dc96 --- /dev/null +++ b/linux/changelog.d/3892.security.md @@ -0,0 +1 @@ +MCP Read Only and Safe Mode no longer miss a statement hidden in a SQLite parameter, a continued string or a label. diff --git a/linux/crates/app/src/ui/editor/safe_mode_gate.rs b/linux/crates/app/src/ui/editor/safe_mode_gate.rs index 663c570341..229cc0411b 100644 --- a/linux/crates/app/src/ui/editor/safe_mode_gate.rs +++ b/linux/crates/app/src/ui/editor/safe_mode_gate.rs @@ -91,6 +91,23 @@ mod tests { assert!(drop.destructive); } + #[test] + fn a_byte_order_mark_does_not_hide_a_sqlite_statement_from_the_question() { + // SQLite reads U+FEFF as a space where a token starts, so both + // statements run as written without the mark. + let drop = gated("\u{feff}DROP TABLE t", SqlGrammar::Sqlite, SafeModeLevel::Off); + let delete = gated( + "WITH stale AS (SELECT 1) \u{feff}DELETE FROM t", + SqlGrammar::Sqlite, + SafeModeLevel::ConfirmWrites, + ); + + assert_eq!(drop.confirm.len(), 1); + assert!(drop.destructive); + assert_eq!(delete.confirm, ["WITH stale AS (SELECT 1) \u{feff}DELETE FROM t"]); + assert!(delete.destructive); + } + #[test] fn a_repeated_batch_shows_how_many_times_it_runs() { let run = gated( diff --git a/linux/crates/app/src/ui/editor/sql_file.rs b/linux/crates/app/src/ui/editor/sql_file.rs index dbb243e3a1..582552b652 100644 --- a/linux/crates/app/src/ui/editor/sql_file.rs +++ b/linux/crates/app/src/ui/editor/sql_file.rs @@ -20,7 +20,12 @@ pub async fn load_text(file: &gio::File) -> Result { } Err(FileReadError::Io(error)) => return Err(unreadable(error)), }; - String::from_utf8(bytes).map_err(|_| crate::i18n::gettext("That file is not UTF-8 text.")) + let text = String::from_utf8(bytes).map_err(|_| crate::i18n::gettext("That file is not UTF-8 text."))?; + // A byte-order mark says the file is UTF-8; it is not part of the SQL. + Ok(match text.strip_prefix('\u{feff}') { + Some(rest) => rest.to_owned(), + None => text, + }) } fn unreadable(error: glib::Error) -> String { @@ -74,6 +79,15 @@ mod tests { assert!(refused.contains("too large"), "{refused}"); } + #[gtk4::test] + fn a_byte_order_mark_is_not_part_of_the_text() { + let root = tempfile::tempdir().expect("tempdir"); + let path = root.path().join("bom.sql"); + std::fs::write(&path, "\u{feff}DROP TABLE t;\n").expect("write"); + + assert_eq!(load(gio::File::for_path(&path)).as_deref(), Ok("DROP TABLE t;\n")); + } + #[gtk4::test] fn a_small_file_is_read_as_text() { let root = tempfile::tempdir().expect("tempdir"); diff --git a/linux/crates/app/src/ui/import_preview.rs b/linux/crates/app/src/ui/import_preview.rs new file mode 100644 index 0000000000..1e6a24681b --- /dev/null +++ b/linux/crates/app/src/ui/import_preview.rs @@ -0,0 +1,135 @@ +use gtk4 as gtk; +use gtk4::{gio, glib}; +use libadwaita as adw; +use libadwaita::prelude::*; +use tablepro_core::{AiPolicy, DriverRegistry, ExternalAccess}; +use tablepro_storage::{ImportChange, ImportEntry, ImportPreview}; + +use crate::services::saved_connection_object::subtitle_for; + +pub(crate) const IMPORT: &str = "import"; +const CANCEL: &str = "cancel"; + +/// The list a file can bring is long only when the user exported a long +/// one, so it scrolls past this height rather than growing the dialog. +const MAX_LIST_HEIGHT: i32 = 320; + +/// The question before an import writes anything. Each connection shows +/// where it connects, so a name that matches a saved one but points at +/// another host is seen before it is saved. +pub(crate) fn import_preview_dialog(preview: &ImportPreview, drivers: &DriverRegistry) -> adw::AlertDialog { + let count = preview.entries().len(); + let dialog = adw::AlertDialog::new( + Some(&crate::i18n::ngettext_f( + "Import {n} Connection?", + "Import {n} Connections?", + u32::try_from(count).unwrap_or(u32::MAX), + &[("n", &count.to_string())], + )), + Some(&crate::i18n::gettext( + "Check where each one connects. A file you did not export yourself can point anywhere.", + )), + ); + let rows = gio::ListStore::new::(); + for entry in preview.entries() { + rows.append(&glib::BoxedAnyObject::new(entry_text(entry, drivers))); + } + // A row is built only while it is on screen, so a file of thousands + // of connections opens the dialog as fast as one of three. + let list = gtk::ListView::builder() + .model(>k::NoSelection::new(Some(rows))) + .factory(&row_factory()) + .css_classes(["boxed-list"]) + .build(); + let scrolled = gtk::ScrolledWindow::builder() + .hscrollbar_policy(gtk::PolicyType::Never) + .propagate_natural_height(true) + .max_content_height(MAX_LIST_HEIGHT) + .child(&list) + .build(); + dialog.set_extra_child(Some(&scrolled)); + dialog.add_response(CANCEL, &crate::i18n::gettext("Cancel")); + dialog.add_response(IMPORT, &crate::i18n::gettext("Import")); + dialog.set_response_appearance(IMPORT, adw::ResponseAppearance::Suggested); + dialog.set_default_response(Some(CANCEL)); + dialog.set_close_response(CANCEL); + dialog +} + +/// What one row shows. +struct EntryText { + title: String, + subtitle: String, +} + +fn entry_text(entry: &ImportEntry, drivers: &DriverRegistry) -> EntryText { + let saved = &entry.connection; + let file_based = drivers + .get(&saved.driver_id) + .is_some_and(|driver| driver.capabilities().is_file_based()); + let change = match entry.change { + ImportChange::Added => crate::i18n::gettext("New"), + ImportChange::Updated => crate::i18n::gettext("Updated"), + }; + let mut lines = vec![format!("{change} · {}", subtitle_for(saved, file_based, None))]; + // An updated entry keeps its own settings; only the list's labels + // come from the file. + if entry.change == ImportChange::Added { + lines.extend(looser_settings(saved.external_access, saved.ai_policy)); + } + EntryText { + title: saved.name.clone(), + subtitle: lines.join("\n"), + } +} + +fn row_factory() -> gtk::SignalListItemFactory { + let factory = gtk::SignalListItemFactory::new(); + factory.connect_setup(|_, object| { + if let Some(item) = object.downcast_ref::() { + // A name and a host are the file's text, never markup. + let row = adw::ActionRow::builder().use_markup(false).build(); + item.set_activatable(false); + item.set_child(Some(&row)); + } + }); + factory.connect_bind(|_, object| { + let Some(item) = object.downcast_ref::() else { + return; + }; + let (Some(row), Some(text)) = ( + item.child().and_downcast::(), + item.item().and_downcast::(), + ) else { + return; + }; + let text = text.borrow::(); + row.set_title(&text.title); + row.set_subtitle(&text.subtitle); + }); + factory +} + +/// What the file allows beyond what a new connection starts with. +fn looser_settings(access: ExternalAccess, policy: AiPolicy) -> Vec { + let mut lines = Vec::new(); + if access == ExternalAccess::ReadWrite { + lines.push(crate::i18n::gettext("MCP clients can ask to write")); + } + if policy == AiPolicy::Allow { + lines.push(crate::i18n::gettext("The AI assistant reads without asking")); + } + lines +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_settings_looser_than_a_new_connections_are_called_out() { + assert!(looser_settings(ExternalAccess::ReadOnly, AiPolicy::Ask).is_empty()); + assert!(looser_settings(ExternalAccess::Blocked, AiPolicy::Never).is_empty()); + assert_eq!(looser_settings(ExternalAccess::ReadWrite, AiPolicy::Allow).len(), 2); + } +} diff --git a/linux/crates/app/src/ui/mod.rs b/linux/crates/app/src/ui/mod.rs index 081744b61c..9f04d9deb5 100644 --- a/linux/crates/app/src/ui/mod.rs +++ b/linux/crates/app/src/ui/mod.rs @@ -28,6 +28,7 @@ mod host_key_failure; mod host_key_review; pub(crate) mod icons; mod import_dialog; +pub(crate) mod import_preview; mod key_tab; mod menu_label; mod name_entry; diff --git a/linux/crates/app/src/ui/session_window/connections.rs b/linux/crates/app/src/ui/session_window/connections.rs index ef070d1abd..2590b8b080 100644 --- a/linux/crates/app/src/ui/session_window/connections.rs +++ b/linux/crates/app/src/ui/session_window/connections.rs @@ -698,6 +698,7 @@ impl SessionWindow { }); } + /// Work out what the file would do, writing nothing yet. pub(super) fn on_connections_file_read(&self, path: std::path::PathBuf, bytes: Vec) { let (Some(state), Some(input)) = (self.state(), self.input()) else { return; @@ -705,21 +706,71 @@ impl SessionWindow { let connections = state.services.storage.connections().clone(); let tasks = state.services.tasks.clone(); let file = path.clone(); + crate::runtime::spawn_held(&state.services.tasks, &*state.services.hold(), async move { + let previewed = tasks + .spawn_blocking_task(move || connections.preview_import_blocking(&path, &bytes)) + .await; + let message = match previewed { + Ok(Ok(preview)) if preview.entries().is_empty() => { + crate::i18n::gettext("That file has no connections in it.") + } + Ok(Ok(preview)) => { + input.deliver(SessionWindowInput::ImportPreviewed(Box::new(preview))); + return; + } + Ok(Err(error)) => { + tracing::warn!(%error, "could not read the connections to import"); + import_failure_message(error, &file) + } + Err(error) => { + tracing::warn!(%error, "the import task did not finish"); + crate::i18n::gettext("The connections could not be imported.") + } + }; + input.deliver(SessionWindowInput::ShowToast(message)); + }); + } + + /// Ask before writing, showing where each connection goes. + pub(super) fn on_import_previewed(&self, preview: tablepro_storage::ImportPreview) { + let Some(state) = self.state() else { + return; + }; + let dialog = crate::ui::import_preview::import_preview_dialog(&preview, &state.services.drivers); + let window = self.downgrade(); + dialog.connect_response(None, move |_, response| { + if response == crate::ui::import_preview::IMPORT + && let Some(window) = window.upgrade() + { + window.apply_import(preview.clone()); + } + }); + dialog.present(Some(self)); + } + + fn apply_import(&self, preview: tablepro_storage::ImportPreview) { + let (Some(state), Some(input)) = (self.state(), self.input()) else { + return; + }; + let connections = state.services.storage.connections().clone(); + let tasks = state.services.tasks.clone(); crate::runtime::spawn_held(&state.services.tasks, &*state.services.hold(), async move { let imported = tasks - .spawn_blocking_task(move || connections.import_blocking(&path, &bytes)) + .spawn_blocking_task(move || connections.apply_import_blocking(&preview)) .await; let message = match imported { - Ok(Ok(report)) if report.total() == 0 => crate::i18n::gettext("That file has no connections in it."), Ok(Ok(report)) => crate::i18n::ngettext_f( "Imported {n} connection.", "Imported {n} connections.", u32::try_from(report.total()).unwrap_or(u32::MAX), &[("n", &report.total().to_string())], ), + Ok(Err(tablepro_storage::StorageError::ImportOutdated)) => crate::i18n::gettext( + "The saved connections changed while you were deciding. Import the file again.", + ), Ok(Err(error)) => { tracing::warn!(%error, "could not import the connections"); - import_failure_message(error, &file) + crate::ui::error_text::saved_connection_error_message(&SavedConnectionError::Storage(error)) } Err(error) => { tracing::warn!(%error, "the import task did not finish"); @@ -1104,6 +1155,52 @@ mod tests { ); } + /// A file whose entry takes a saved connection's name but opens + /// another file: the dialog shows where it goes and the suffixed name + /// before anything is written, and only Import writes it. + #[gtk4::test] + fn an_import_shows_where_each_connection_goes_before_writing() { + let test = TestWindow::picker("ImportPreview"); + wait_for_list(&test.window); + let lookalike = SavedConnection { + id: Uuid::new_v4(), + database: "/srv/elsewhere.sqlite3".to_owned(), + ..test.saved.clone() + }; + let entry = serde_json::to_string(&lookalike).expect("the entry"); + let bytes = format!(r#"{{"version": 2, "connections": [{entry}]}}"#).into_bytes(); + + test.window.send(SessionWindowInput::ConnectionsFileRead { + path: "/tmp/connections.json".into(), + bytes, + }); + let alert = wait_for_alert(&test.window); + let list = alert.extra_child().expect("the dialog's rows"); + let mut rows: Vec = Vec::new(); + wait_until(WAIT, || { + rows = crate::test_support::descendants(&list) + .into_iter() + .filter_map(|widget| widget.downcast::().ok()) + .filter(|row| !row.title().is_empty()) + .collect(); + !rows.is_empty() + }) + .expect("the rows"); + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].title(), format!("{} (imported)", test.saved.name)); + assert!( + rows[0] + .subtitle() + .is_some_and(|subtitle| subtitle.contains("elsewhere.sqlite3")), + "{:?}", + rows[0].subtitle() + ); + assert!(!listed(&test, lookalike.id), "the preview wrote the list"); + + respond(&alert, crate::ui::import_preview::IMPORT); + wait_until(WAIT, || listed(&test, lookalike.id)).expect("the import to land"); + } + #[gtk4::test] fn a_confirmed_delete_removes_the_entry_and_its_secrets() { let test = TestWindow::picker("DeleteConfirmed"); diff --git a/linux/crates/app/src/ui/session_window/mod.rs b/linux/crates/app/src/ui/session_window/mod.rs index 1f7865151c..f577d2cb9b 100644 --- a/linux/crates/app/src/ui/session_window/mod.rs +++ b/linux/crates/app/src/ui/session_window/mod.rs @@ -522,6 +522,7 @@ impl SessionWindow { SessionWindowInput::ExportConnections => self.on_export_connections(), SessionWindowInput::ImportConnections => self.on_import_connections(), SessionWindowInput::ConnectionsFileRead { path, bytes } => self.on_connections_file_read(path, bytes), + SessionWindowInput::ImportPreviewed(preview) => self.on_import_previewed(*preview), SessionWindowInput::ShowToast(message) => self.show_toast(&message), } } diff --git a/linux/crates/app/src/ui/session_window/session_window_input.rs b/linux/crates/app/src/ui/session_window/session_window_input.rs index ea95bba177..debea7282d 100644 --- a/linux/crates/app/src/ui/session_window/session_window_input.rs +++ b/linux/crates/app/src/ui/session_window/session_window_input.rs @@ -162,5 +162,7 @@ pub(crate) enum SessionWindowInput { path: std::path::PathBuf, bytes: Vec, }, + /// What importing that file would do, for the user to agree to. + ImportPreviewed(Box), ShowToast(String), } diff --git a/linux/crates/core/src/dialect/test_dialect.rs b/linux/crates/core/src/dialect/test_dialect.rs index aa0728058e..fd9d7d934a 100644 --- a/linux/crates/core/src/dialect/test_dialect.rs +++ b/linux/crates/core/src/dialect/test_dialect.rs @@ -295,6 +295,7 @@ impl SqlDialect for TestDialect { statement_prefixes: &[&["CREATE", "USER"], &["ALTER", "USER"]], conditional_prefixes: &[], carriers: &[], + secret_parameters: &[], }; classify_for_history(self.grammar, &RULES, sql) } diff --git a/linux/crates/core/src/lib.rs b/linux/crates/core/src/lib.rs index 1ff28881a5..fe29020dec 100644 --- a/linux/crates/core/src/lib.rs +++ b/linux/crates/core/src/lib.rs @@ -81,6 +81,7 @@ mod transport_class; mod transport_error; mod transport_kind; mod transport_route; +pub mod unicode_blank; mod unsupported_feature; mod user_sql; pub mod value; diff --git a/linux/crates/core/src/sql_syntax/credential_rules.rs b/linux/crates/core/src/sql_syntax/credential_rules.rs index 2d23876692..f3398bc985 100644 --- a/linux/crates/core/src/sql_syntax/credential_rules.rs +++ b/linux/crates/core/src/sql_syntax/credential_rules.rs @@ -19,6 +19,11 @@ pub struct CredentialRules { /// Functions, procedures and table engines whose arguments can hold /// a login or a connection string. pub carriers: &'static [&'static str], + /// Words that mark a named argument or variable as a secret wherever + /// they stand in its name, such as PASSWORD in T-SQL's + /// `@monitor_server_password`. No list of procedures is complete, and + /// a password passed by name is one whatever the procedure is called. + pub secret_parameters: &'static [&'static str], } /// The word before a name being declared or written to, where a carrier @@ -47,23 +52,40 @@ const INDEX_MODIFIERS: [&str; 11] = [ impl CredentialRules { pub(crate) fn matches(&self, grammar: SqlGrammar, tokens: &[StatementToken<'_>]) -> bool { (0..tokens.len()).any(|at| { - self.prefix_at(tokens, at) || self.conditional_at(tokens, at) || self.carrier_at(grammar, tokens, at) + self.prefix_at(tokens, at) + || self.conditional_at(grammar, tokens, at) + || self.carrier_at(grammar, tokens, at) + || self.secret_parameter_at(tokens, at) }) } + fn secret_parameter_at(&self, tokens: &[StatementToken<'_>], at: usize) -> bool { + let Some(StatementToken::Word(word)) = tokens.get(at) else { + return false; + }; + let Some(name) = word.strip_prefix('@') else { + return false; + }; + let name = name.to_ascii_uppercase(); + self.secret_parameters + .iter() + .any(|secret| name.contains(&secret.to_ascii_uppercase())) + } + fn prefix_at(&self, tokens: &[StatementToken<'_>], at: usize) -> bool { self.statement_prefixes .iter() .any(|prefix| words_at(tokens, at, prefix).is_some()) } - fn conditional_at(&self, tokens: &[StatementToken<'_>], at: usize) -> bool { + fn conditional_at(&self, grammar: SqlGrammar, tokens: &[StatementToken<'_>], at: usize) -> bool { self.conditional_prefixes.iter().any(|(prefix, triggers)| { words_at(tokens, at, prefix).is_some_and(|end| { - tokens.get(end..).unwrap_or_default().iter().any(|token| { - token - .name() - .is_some_and(|name| triggers.iter().any(|trigger| trigger.eq_ignore_ascii_case(name))) + (end..tokens.len()).any(|at| { + (decodes_unicode_names(grammar) && spelled_in_escapes(tokens, at)) + || tokens[at] + .name() + .is_some_and(|name| triggers.iter().any(|trigger| trigger.eq_ignore_ascii_case(name))) }) }) }) @@ -76,6 +98,10 @@ impl CredentialRules { let Some(name) = token.name() else { return false; }; + if decodes_unicode_names(grammar) && spelled_in_escapes(tokens, at) { + return tokens.get(after_uescape(tokens, at + 1)) == Some(&StatementToken::OpenParen) + && !names_an_object(tokens, qualified_name_start(tokens, at - 2)); + } if !self.carriers.iter().any(|carrier| carrier.eq_ignore_ascii_case(name)) { return false; } @@ -97,6 +123,39 @@ impl CredentialRules { } } +/// The grammars whose scanner is PostgreSQL's, which decodes the escapes +/// of a `U&"…"` name before it looks the name up, so such a name is +/// compared with nothing and may be any carrier. +fn decodes_unicode_names(grammar: SqlGrammar) -> bool { + matches!(grammar, SqlGrammar::PostgreSql | SqlGrammar::DuckDb) +} + +/// A `U&"…"` name, whose prefix the lexer reads as the word `U` and `&`, +/// written with nothing between them, since `u & "f"(1)` is an AND. +fn spelled_in_escapes(tokens: &[StatementToken<'_>], at: usize) -> bool { + let (Some(StatementToken::QuotedName(name)), Some(StatementToken::Word(prefix))) = + (tokens.get(at), at.checked_sub(2).and_then(|before| tokens.get(before))) + else { + return false; + }; + tokens[at - 1].is_punct('&') + && prefix.eq_ignore_ascii_case("U") + && prefix.as_ptr() as usize + 3 == name.as_ptr() as usize +} + +/// The index past a `UESCAPE '!'` at `at`, or `at` where there is none. +/// The string can go on in another on the next line, so it is as many +/// tokens as stand there. +fn after_uescape(tokens: &[StatementToken<'_>], at: usize) -> usize { + if !tokens.get(at).is_some_and(|token| token.is_word("UESCAPE")) { + return at; + } + let strings = tokens[at + 1..] + .iter() + .take_while(|token| matches!(token, StatementToken::StringLiteral(_) | StatementToken::DollarBody(_))); + at + 1 + strings.count() +} + fn starts_statement(tokens: &[StatementToken<'_>], start: usize) -> bool { start == 0 || tokens.get(start - 1).is_some_and(|token| token.is_punct(';')) } diff --git a/linux/crates/core/src/sql_syntax/history_recording.rs b/linux/crates/core/src/sql_syntax/history_recording.rs index 32c811ebb6..80cc5aab42 100644 --- a/linux/crates/core/src/sql_syntax/history_recording.rs +++ b/linux/crates/core/src/sql_syntax/history_recording.rs @@ -87,18 +87,38 @@ fn carries_credentials( depth < MAX_BODY_DEPTH && carries_credentials(grammar, rules, &lex_statements(body, grammar).statements, depth + 1) }; + let runs_text = runs_text_as_sql(grammar, statements); statements.iter().any(|tokens| { rules.matches(grammar, tokens) || tokens.iter().enumerate().any(|(at, token)| match token { StatementToken::DollarBody(body) => body_carries(body), - StatementToken::StringLiteral(text) if quoted_routine_body(grammar, tokens, at) => { - body_carries(&string_value(text)) + StatementToken::StringLiteral(text) if runs_text || quoted_routine_body(grammar, tokens, at) => { + body_carries(&string_value(text, grammar)) } _ => false, }) }) } +/// Whether the script hands the server text to run as a statement: +/// T-SQL `EXEC ('...')` and `sp_executesql`, MySQL `PREPARE ... FROM`, +/// MariaDB `EXECUTE IMMEDIATE`, PL/pgSQL `EXECUTE`. The text can reach +/// them through a variable or a concatenation, so every string of such a +/// script is read for the statements it holds. +fn runs_text_as_sql(grammar: SqlGrammar, statements: &[Vec>]) -> bool { + let runners: &[&str] = match grammar { + SqlGrammar::MsSql => &["EXEC", "EXECUTE", "sp_executesql"], + SqlGrammar::MySql => &["PREPARE", "EXECUTE"], + SqlGrammar::PostgreSql => &["EXECUTE"], + _ => return false, + }; + statements.iter().flatten().any(|token| { + token + .name() + .is_some_and(|name| runners.iter().any(|runner| runner.eq_ignore_ascii_case(name))) + }) +} + /// PostgreSQL also takes a DO block or a function body as a plain /// string: `DO 'BEGIN ... END'`, `CREATE FUNCTION f() ... AS 'SELECT 1'`. fn quoted_routine_body(grammar: SqlGrammar, tokens: &[StatementToken<'_>], at: usize) -> bool { @@ -120,24 +140,35 @@ fn defines_routine(tokens: &[StatementToken<'_>]) -> bool { }) } -/// The text a PostgreSQL string literal stands for: `'it''s'` and -/// `E'it\'s'` both give `it's`. -fn string_value(text: &str) -> String { +/// The text a string literal stands for: `'it''s'` and PostgreSQL's +/// `E'it\'s'` both give `it's`, and so does MySQL's `"it's"`. A string +/// continued on the next line, `E'a'` then `'b'`, gives `ab`. MySQL +/// escapes with a backslash in every string. +fn string_value(text: &str, grammar: SqlGrammar) -> String { let (escapes, quoted) = match text.strip_prefix(['E', 'e']) { - Some(rest) => (true, rest), - None => (false, text), + Some(rest) if rest.starts_with('\'') => (true, rest), + _ => (grammar == SqlGrammar::MySql, text), }; + let quote = if quoted.starts_with('"') { '"' } else { '\'' }; let inner = quoted - .strip_prefix('\'') - .and_then(|rest| rest.strip_suffix('\'')) + .strip_prefix(quote) + .and_then(|rest| rest.strip_suffix(quote)) .unwrap_or_default(); let mut value = String::with_capacity(inner.len()); - let mut chars = inner.chars(); + let mut chars = inner.chars().peekable(); while let Some(c) = chars.next() { match c { - '\'' => { + _ if c == quote && chars.peek() == Some("e) => { chars.next(); - value.push('\''); + value.push(quote); + } + // A piece ends here and the next one starts at its quote. + _ if c == quote => { + for skipped in chars.by_ref() { + if skipped == quote { + break; + } + } } '\\' if escapes => match chars.next() { Some('n') => value.push('\n'), @@ -166,6 +197,7 @@ mod tests { ], conditional_prefixes: &[(&["GRANT"], &["IDENTIFIED"])], carriers: &["dblink_connect", "sp_addlogin", "s3", "OPENROWSET"], + secret_parameters: &[], }; const WITHHELD: HistoryRecording = HistoryRecording::Withhold(WithholdReason::CredentialStatement); @@ -464,4 +496,82 @@ mod tests { prop_assert_eq!(classify(SqlGrammar::PostgreSql, &sql), WITHHELD); } } + + #[test] + fn a_call_of_a_name_spelled_in_escapes_may_be_a_carrier() { + for sql in [ + r#"SELECT U&"dblink_connec\0074"('password=x')"#, + r#"SELECT public.u&"dblink_connec\+000074" ('password=x')"#, + r#"SELECT U&"dblink_connec!0074" UESCAPE '!' ('password=x')"#, + r#"SELECT U&"dblink_connec!0074" UESCAPE $$!$$ ('password=x')"#, + "SELECT U&\"dblink_connec!0074\" UESCAPE '!'\n'' ('password=x')", + r#"GRANT SELECT ON t TO u U&"IDENTIFIE\0044" BY 'p'"#, + ] { + assert_eq!(classify(SqlGrammar::PostgreSql, sql), WITHHELD, "{sql}"); + } + for sql in [ + r#"SELECT U&"d\0061ta", u & "mask" FROM orders"#, + r#"SELECT U&"d!0061ta" UESCAPE '!' FROM orders WHERE id IN (1, 2)"#, + r#"INSERT INTO public.U&"d\0061ta" (id) VALUES (1)"#, + r#"CREATE TABLE U&"s\0033" (id int)"#, + ] { + assert_eq!(classify(SqlGrammar::PostgreSql, sql), HistoryRecording::Record, "{sql}"); + } + assert_eq!( + classify(SqlGrammar::Sqlite, r#"SELECT u & "f"('x') FROM t"#), + HistoryRecording::Record + ); + assert_eq!( + classify(SqlGrammar::PostgreSql, r#"SELECT u & "abs"(-3) FROM t"#), + HistoryRecording::Record + ); + } + + #[test] + fn text_run_as_dynamic_sql_is_read_for_statements() { + for (grammar, sql) in [ + (SqlGrammar::MsSql, "EXEC('CREATE USER u WITH PASSWORD = ''p''')"), + ( + SqlGrammar::MsSql, + "EXEC sp_executesql N'CREATE USER u WITH PASSWORD = ''p'''", + ), + ( + SqlGrammar::MsSql, + "DECLARE @s nvarchar(99) = N'CREATE USER u WITH PASSWORD = ''p'''; EXEC (@s)", + ), + (SqlGrammar::MySql, "PREPARE s FROM 'CREATE USER u IDENTIFIED BY ''p'''"), + (SqlGrammar::MySql, "PREPARE s FROM \"CREATE USER u IDENTIFIED BY 'p'\""), + (SqlGrammar::MySql, r"PREPARE s FROM 'CREATE USER u IDENTIFIED BY \'p\''"), + ( + SqlGrammar::MySql, + "EXECUTE IMMEDIATE 'CREATE USER u IDENTIFIED BY ''p'''", + ), + ( + SqlGrammar::MySql, + "SET @s = 'CREATE USER u IDENTIFIED BY ''p'''; PREPARE s FROM @s", + ), + ( + SqlGrammar::PostgreSql, + "DO $$ BEGIN EXECUTE 'CREATE USER u PASSWORD ''p'''; END $$", + ), + ( + SqlGrammar::PostgreSql, + "DO $$ BEGIN EXECUTE format('CREATE USER %I PASSWORD %L', 'u', 'p'); END $$", + ), + ( + SqlGrammar::PostgreSql, + "DO $$ BEGIN EXECUTE E'CREATE USER u '\n'PASSWORD ''p'''; END $$", + ), + ] { + assert_eq!(classify(grammar, sql), WITHHELD, "{sql}"); + } + for (grammar, sql) in [ + (SqlGrammar::MsSql, "EXEC sp_who 'active'"), + (SqlGrammar::MySql, "PREPARE s FROM 'SELECT ''CREATE USER u'''"), + (SqlGrammar::PostgreSql, "DO $$ BEGIN EXECUTE 'SELECT 1'; END $$"), + (SqlGrammar::Sqlite, "SELECT 'CREATE USER u' AS execute"), + ] { + assert_eq!(classify(grammar, sql), HistoryRecording::Record, "{sql}"); + } + } } diff --git a/linux/crates/core/src/sql_syntax/mod.rs b/linux/crates/core/src/sql_syntax/mod.rs index 36acc3d9a3..50e8f531d4 100644 --- a/linux/crates/core/src/sql_syntax/mod.rs +++ b/linux/crates/core/src/sql_syntax/mod.rs @@ -12,6 +12,7 @@ mod parse_type; pub mod redis; mod reserved_words; pub mod script; +mod state_changing_functions; mod statement_access; mod statement_tokens; @@ -24,4 +25,4 @@ pub use history_recording::{HistoryRecording, WithholdReason, classify_for_histo pub use literal_token::LiteralToken; pub use parse_expression::{MAX_EXPRESSION_LEN, ValidatedExpression, parse_expression}; pub use parse_type::{MAX_TYPE_LEN, TypeShape, ValidatedType, parse_type}; -pub use statement_access::{StatementAccess, statement_access}; +pub use statement_access::{StatementAccess, sqlite_pragma_reports, statement_access}; diff --git a/linux/crates/core/src/sql_syntax/script/lexer.rs b/linux/crates/core/src/sql_syntax/script/lexer.rs index c10883d5b2..26ce81caf2 100644 --- a/linux/crates/core/src/sql_syntax/script/lexer.rs +++ b/linux/crates/core/src/sql_syntax/script/lexer.rs @@ -10,6 +10,9 @@ use crate::sql_syntax::script::{LexicalSettings, OpenConstruct, ScriptTokenKind} /// MariaDB's MSSQL mode ends a `[name]` at a `]` that is not doubled. const MARIADB_BRACKETS: BracketIdentifier = BracketIdentifier::Doubling; +/// U+FEFF as UTF-8. +const BYTE_ORDER_MARK: &[u8] = b"\xEF\xBB\xBF"; + #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct LexedToken { pub kind: ScriptTokenKind, @@ -85,8 +88,16 @@ impl<'a> Lexer<'a> { fn lex_at(&mut self, start: usize, first: u8, delimiter: Option<&str>) -> LexedToken { let next = self.bytes.get(start + 1).copied(); + if self.syntax.tcl_parameters + && let Some(end) = self.tcl_parameter_end(start, first) + { + return self.plain(ScriptTokenKind::Word, end); + } match first { b if is_space(b) => self.plain(ScriptTokenKind::Whitespace, self.whitespace_end(start)), + 0xEF if self.syntax.byte_order_mark_is_space && self.bytes[start..].starts_with(BYTE_ORDER_MARK) => { + self.plain(ScriptTokenKind::Whitespace, start + BYTE_ORDER_MARK.len()) + } b'-' if next == Some(b'-') && self.dash_comment_starts(start) => self.line_comment(start), b'#' if self.hash_comment_starts(next) => self.line_comment(start), b'/' if next == Some(b'*') => self.comment(start), @@ -264,10 +275,43 @@ impl<'a> Lexer<'a> { None } + /// An `E'...'` string, with every `'...'` that continues it: the + /// server reads those by the rules of the first piece, so a backslash + /// escapes in them whatever `standard_conforming_strings` says. fn escape_string(&self, start: usize) -> LexedToken { - match self.quote_end(start + 2, b'\'', true) { - Some(end) => self.plain(ScriptTokenKind::StringLiteral, end), - None => self.unterminated(start, OpenConstruct::StringLiteral), + let mut from = start + 2; + loop { + let Some(end) = self.quote_end(from, b'\'', true) else { + return self.unterminated(start, OpenConstruct::StringLiteral); + }; + match self.continuing_quote(end) { + Some(quote) => from = quote + 1, + None => return self.plain(ScriptTokenKind::StringLiteral, end), + } + } + } + + /// The quote that carries on the string that closed just before + /// `from`, as PostgreSQL's `quotecontinue` rule finds it: only blanks + /// and `--` comments in between, a line break among them. Before the + /// first line break only a space, tab or form feed counts as a blank. + fn continuing_quote(&self, from: usize) -> Option { + let mut index = from; + let mut line_broke = false; + loop { + match *self.bytes.get(index)? { + b'\n' | b'\r' => { + line_broke = true; + index += 1; + } + b' ' | b'\t' | 0x0c => index += 1, + 0x0b if line_broke => index += 1, + b'-' if self.bytes.get(index + 1) == Some(&b'-') => { + index += self.bytes[index..].iter().position(|&b| b == b'\n' || b == b'\r')?; + } + b'\'' if line_broke => return Some(index), + _ => return None, + } } } @@ -337,6 +381,42 @@ impl<'a> Lexer<'a> { self.plain(ScriptTokenKind::Punct, start + 1) } + /// SQLite reads `$space::name(…)` as one token, and the same after `@`, + /// `:` and `#`, as `sqlite3GetToken` reads it: a parameter whose name + /// runs from the parenthesis to the next `)` whatever stands between, + /// a quote or a `;` included. White space or the end of the text ends + /// it first, as a token SQLite refuses. `None` for a name that is one + /// word, which needs no rule of its own. + fn tcl_parameter_end(&self, start: usize, first: u8) -> Option { + if !matches!(first, b'$' | b'@' | b':' | b'#') { + return None; + } + let mut named = false; + let mut qualified = false; + let mut index = start + 1; + while let Some(&b) = self.bytes.get(index) { + match b { + b if self.is_word_part(b) => { + named = true; + index += 1; + } + b':' if self.bytes.get(index + 1) == Some(&b':') => { + qualified = true; + index += 2; + } + b'(' if named => { + let stop = self.bytes[index..] + .iter() + .position(|&b| matches!(b, b')' | 0) || is_space(b)) + .map_or(self.bytes.len(), |offset| index + offset); + return Some(stop + usize::from(self.bytes.get(stop) == Some(&b')'))); + } + _ => break, + } + } + qualified.then_some(index) + } + /// MySQL 8.1 and later quote text in `$tag$ … $tag$` where a token /// starts, as `get_dollar_quoted_text` reads it. A `$` that breaks a /// partial match of the closer cannot start the next one. diff --git a/linux/crates/core/src/sql_syntax/script/script_rules.rs b/linux/crates/core/src/sql_syntax/script/script_rules.rs index b36f3d9ed0..2cd7913559 100644 --- a/linux/crates/core/src/sql_syntax/script/script_rules.rs +++ b/linux/crates/core/src/sql_syntax/script/script_rules.rs @@ -82,6 +82,14 @@ pub(crate) struct ScriptSyntax { /// `1_000.5e1_0`: PostgreSQL 16, SQLite 3.46 and ClickHouse read it /// so. MySQL reads `1_0e` as a name. pub digit_separators: bool, + /// A parameter is named as a Tcl variable is, so after `$`, `@`, `:` + /// or `#` the name can hold `::` and end in `(…)`, which runs to the + /// next `)` whatever it holds: SQLite's `$space::name(index)`. + pub tcl_parameters: bool, + /// A UTF-8 byte-order mark where a token starts is a space, as + /// SQLite's `sqlite3GetToken` reads it. Inside a name it stays a name + /// character, there as elsewhere. + pub byte_order_mark_is_space: bool, pub word_start_extra: &'static [u8], pub word_part_extra: &'static [u8], } @@ -118,6 +126,8 @@ const POSTGRESQL: ScriptRules = ScriptRules { executable_comments: false, dollar_quote: DollarQuote::Tagged, digit_separators: true, + tcl_parameters: false, + byte_order_mark_is_space: false, word_start_extra: b"", word_part_extra: b"$", }, @@ -142,6 +152,8 @@ const MYSQL: ScriptRules = ScriptRules { executable_comments: true, dollar_quote: DollarQuote::None, digit_separators: false, + tcl_parameters: false, + byte_order_mark_is_space: false, word_start_extra: b"$", word_part_extra: b"$", }, @@ -166,6 +178,8 @@ const SQLITE: ScriptRules = ScriptRules { executable_comments: false, dollar_quote: DollarQuote::None, digit_separators: true, + tcl_parameters: true, + byte_order_mark_is_space: true, word_start_extra: b"$", word_part_extra: b"$", }, @@ -190,6 +204,8 @@ const MSSQL: ScriptRules = ScriptRules { executable_comments: false, dollar_quote: DollarQuote::None, digit_separators: false, + tcl_parameters: false, + byte_order_mark_is_space: false, word_start_extra: b"@#", word_part_extra: b"@#$", }, @@ -214,6 +230,8 @@ const CLICKHOUSE: ScriptRules = ScriptRules { executable_comments: false, dollar_quote: DollarQuote::Heredoc, digit_separators: true, + tcl_parameters: false, + byte_order_mark_is_space: false, word_start_extra: b"", word_part_extra: b"", }, @@ -238,6 +256,8 @@ const CQL: ScriptRules = ScriptRules { executable_comments: false, dollar_quote: DollarQuote::Untagged, digit_separators: false, + tcl_parameters: false, + byte_order_mark_is_space: false, word_start_extra: b"", word_part_extra: b"", }, @@ -265,6 +285,8 @@ const DUCKDB: ScriptRules = ScriptRules { executable_comments: false, dollar_quote: DollarQuote::Tagged, digit_separators: true, + tcl_parameters: false, + byte_order_mark_is_space: false, word_start_extra: b"", word_part_extra: b"$", }, diff --git a/linux/crates/core/src/sql_syntax/state_changing_functions.rs b/linux/crates/core/src/sql_syntax/state_changing_functions.rs new file mode 100644 index 0000000000..4b3c5102d6 --- /dev/null +++ b/linux/crates/core/src/sql_syntax/state_changing_functions.rs @@ -0,0 +1,200 @@ +//! Built-in functions that change state from inside a SELECT, by family. +//! +//! A read-only transaction stops none of them on PostgreSQL 15 and +//! earlier: they signal other sessions, take locks, run SQL text on +//! another connection or write large objects. A function the user wrote +//! is beyond what the words show. + +const SERVER_SIGNALS: &[&str] = &[ + "pg_cancel_backend", + "pg_terminate_backend", + "pg_reload_conf", + "pg_rotate_logfile", + "pg_log_backend_memory_contexts", +]; + +const SESSION_CONTROL: &[&str] = &[ + "system$abort_session", + "system$cancel_query", + "system$cancel_all_queries", + "system$abort_transaction", + "abortsessions", +]; + +const RECOVERY_AND_BACKUP: &[&str] = &[ + "pg_promote", + "pg_wal_replay_pause", + "pg_wal_replay_resume", + "pg_backup_start", + "pg_backup_stop", + "pg_switch_wal", + "pg_create_restore_point", + "pg_log_standby_snapshot", + "pg_start_backup", + "pg_stop_backup", + "pg_switch_xlog", + "pg_xlog_replay_pause", + "pg_xlog_replay_resume", +]; + +const REPLICATION: &[&str] = &[ + "pg_create_physical_replication_slot", + "pg_create_logical_replication_slot", + "pg_copy_physical_replication_slot", + "pg_copy_logical_replication_slot", + "pg_drop_replication_slot", + "pg_replication_slot_advance", + "pg_sync_replication_slots", + "pg_logical_slot_get_changes", + "pg_logical_slot_get_binary_changes", + "pg_logical_emit_message", + "pg_replication_origin_create", + "pg_replication_origin_drop", + "pg_replication_origin_advance", + "pg_replication_origin_session_setup", + "pg_replication_origin_session_reset", + "pg_replication_origin_xact_setup", + "pg_replication_origin_xact_reset", +]; + +const SEQUENCES: &[&str] = &["nextval", "setval"]; + +const ADVISORY_LOCKS: &[&str] = &[ + "pg_advisory_lock", + "pg_advisory_lock_shared", + "pg_advisory_unlock", + "pg_advisory_unlock_shared", + "pg_advisory_unlock_all", + "pg_advisory_xact_lock", + "pg_advisory_xact_lock_shared", + "pg_try_advisory_lock", + "pg_try_advisory_lock_shared", + "pg_try_advisory_xact_lock", + "pg_try_advisory_xact_lock_shared", + "get_lock", + "release_lock", + "release_all_locks", +]; + +const SETTINGS_AND_STATISTICS: &[&str] = &[ + "set_config", + "pg_notify", + "pg_stat_reset", + "pg_stat_reset_shared", + "pg_stat_reset_single_table_counters", + "pg_stat_reset_single_function_counters", + "pg_stat_reset_slru", + "pg_stat_reset_replication_slot", + "pg_stat_reset_subscription_stats", + "pg_stat_statements_reset", + "pg_restore_relation_stats", + "pg_restore_attribute_stats", + "pg_clear_relation_stats", + "pg_clear_attribute_stats", +]; + +const LARGE_OBJECTS: &[&str] = &[ + "lo_create", + "lo_creat", + "lo_unlink", + "lo_from_bytea", + "lo_put", + "lowrite", + "lo_truncate", + "lo_truncate64", + "lo_import", + "lo_export", +]; + +const INDEX_MAINTENANCE: &[&str] = &[ + "brin_summarize_new_values", + "brin_summarize_range", + "brin_desummarize_range", + "gin_clean_pending_list", +]; + +/// Functions that run SQL text: what that text does is not in the words. +const SQL_TEXT_RUNNERS: &[&str] = &[ + "query_to_xml", + "query_to_xml_and_xmlschema", + "ts_stat", + "ts_rewrite", + "dblink", + "dblink_exec", + "dblink_send_query", + "dblink_open", +]; + +const EXTENSIONS: &[&str] = &["load_extension"]; + +const FAMILIES: [&[&str]; 11] = [ + SERVER_SIGNALS, + SESSION_CONTROL, + RECOVERY_AND_BACKUP, + REPLICATION, + SEQUENCES, + ADVISORY_LOCKS, + SETTINGS_AND_STATISTICS, + LARGE_OBJECTS, + INDEX_MAINTENANCE, + SQL_TEXT_RUNNERS, + EXTENSIONS, +]; + +/// Whether a call of `name` can change state, compared without regard +/// to case. +pub(crate) fn changes_state(name: &str) -> bool { + FAMILIES + .iter() + .flat_map(|family| family.iter()) + .any(|known| name.eq_ignore_ascii_case(known)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_family_is_matched_whatever_the_case() { + for name in [ + "PG_TERMINATE_BACKEND", + "dblink", + "DBLINK_EXEC", + "lo_creat", + "lowrite", + "query_to_xml", + "ts_stat", + "pg_notify", + "pg_advisory_lock", + "GET_LOCK", + "pg_logical_slot_get_changes", + "pg_replication_origin_create", + "gin_clean_pending_list", + "load_extension", + ] { + assert!(changes_state(name), "{name}"); + } + for name in [ + "currval", + "pg_backend_pid", + "lo_get", + "count", + "dblink_build_sql_insert", + ] { + assert!(!changes_state(name), "{name}"); + } + } + + #[test] + fn no_name_is_listed_twice() { + let mut names: Vec = FAMILIES + .iter() + .flat_map(|family| family.iter()) + .map(|name| name.to_ascii_lowercase()) + .collect(); + let listed = names.len(); + names.sort(); + names.dedup(); + assert_eq!(names.len(), listed); + } +} diff --git a/linux/crates/core/src/sql_syntax/statement_access.rs b/linux/crates/core/src/sql_syntax/statement_access.rs index 7cf97dd081..d2cfaee6e9 100644 --- a/linux/crates/core/src/sql_syntax/statement_access.rs +++ b/linux/crates/core/src/sql_syntax/statement_access.rs @@ -2,6 +2,8 @@ use std::collections::HashMap; use crate::sql_syntax::SqlGrammar; use crate::sql_syntax::script::{ScriptPlan, ScriptToken, ScriptTokenKind}; +use crate::sql_syntax::state_changing_functions::changes_state; +use crate::unicode_blank::is_blank; /// What running a piece of SQL can do, from its own words. Ordered, so /// the strictest of several statements is their maximum. @@ -20,6 +22,11 @@ const READ_VERBS: [&str; 8] = [ "SELECT", "WITH", "VALUES", "TABLE", "SHOW", "EXPLAIN", "DESCRIBE", "DESC", ]; +/// T-SQL has no SHOW, EXPLAIN, DESCRIBE, TABLE or VALUES statement: such +/// a word is a name there, `SHOW:` a label in front of any statement, and +/// `SHOW 'x'` opening a batch runs a procedure called SHOW. +const TSQL_READ_VERBS: [&str; 2] = ["SELECT", "WITH"]; + /// Words that make a statement a write wherever they stand: a CTE that /// deletes, `SELECT … INTO`, `FOR UPDATE`. `REPLACE` and `COPY` are left /// out because they are also function and column names in a read. @@ -88,35 +95,6 @@ const STATEMENT_VERBS: [&str; 10] = [ "DELETE", "UPDATE", "DROP", "TRUNCATE", "SELECT", "INSERT", "MERGE", "EXEC", "EXECUTE", "ALTER", ]; -/// Built-in functions that change state from inside a SELECT: sequences, -/// settings, other sessions, large objects, replication. A function the -/// user wrote is beyond what the words show; on PostgreSQL and MySQL a -/// Read-Only connection's read-only transaction still refuses its write. -const MUTATING_FUNCTIONS: [&str; 22] = [ - "SETVAL", - "NEXTVAL", - "SET_CONFIG", - "PG_TERMINATE_BACKEND", - "PG_CANCEL_BACKEND", - "PG_RELOAD_CONF", - "PG_ROTATE_LOGFILE", - "PG_SWITCH_WAL", - "PG_PROMOTE", - "PG_CREATE_RESTORE_POINT", - "PG_CREATE_PHYSICAL_REPLICATION_SLOT", - "PG_CREATE_LOGICAL_REPLICATION_SLOT", - "PG_DROP_REPLICATION_SLOT", - "PG_STAT_RESET", - "LO_CREATE", - "LO_IMPORT", - "LO_EXPORT", - "LO_UNLINK", - "LO_PUT", - "LO_FROM_BYTEA", - "DBLINK_EXEC", - "LOAD_EXTENSION", -]; - /// Statement verbs that MySQL also has as functions: `TRUNCATE(x, 2)` /// rounds and `INSERT(s, 1, 2, 'x')` splices, both inside a read. const ALSO_FUNCTIONS: [&str; 2] = ["TRUNCATE", "INSERT"]; @@ -147,6 +125,43 @@ const CLAUSE_WORDS: [&str; 15] = [ "ON", "THEN", "FOR", "OF", "AFTER", "BEFORE", "INSTEAD", "DO", "KEY", "GRANT", "REVOKE", "DENY", "AND", "OR", "NOT", ]; +/// The clause words T-SQL reserves, so none of them can be a name that +/// ends a statement. ON and KEY are reserved but also end one (`SET +/// NOCOUNT ON`, `CLOSE MASTER KEY`), so ON leads in only before a foreign +/// key's referential action, and KEY never does. BEFORE, AFTER, INSTEAD +/// and DO are names there outside a trigger, whose body never runs on +/// creation anyway. +const TSQL_CLAUSE_WORDS: [&str; 9] = ["THEN", "FOR", "OF", "GRANT", "REVOKE", "DENY", "AND", "OR", "NOT"]; + +/// What a T-SQL DROP statement drops and an ALTER's DROP clause never +/// does, so `DROP TABLE` after an ALTER with no `;` starts a statement. +const TSQL_DROPPED_OBJECTS: [&str; 24] = [ + "TABLE", + "VIEW", + "PROCEDURE", + "PROC", + "FUNCTION", + "TRIGGER", + "INDEX", + "SCHEMA", + "DATABASE", + "TYPE", + "SEQUENCE", + "SYNONYM", + "USER", + "LOGIN", + "ROLE", + "ASSEMBLY", + "AGGREGATE", + "DEFAULT", + "RULE", + "STATISTICS", + "EXTERNAL", + "FULLTEXT", + "XML", + "SECURITY", +]; + /// The strictest access among the statements of `sql`. /// /// A statement reads only when its words show it does, so anything @@ -209,12 +224,26 @@ fn access_in(sql: &str, grammar: SqlGrammar, plan: &ScriptPlan) -> StatementAcce .fold(unreadable, StatementAccess::max) } +/// A server may take a Unicode blank inside a word for a space or for +/// part of the name, so both readings count and the stricter stands. fn access_of(sql: &str, grammar: SqlGrammar, tokens: &[ScriptToken]) -> StatementAccess { let code = code_of(sql, tokens); - if code.iter().all(|token| token.kind == Kind::End) || plans_only(&code, grammar) { + let whole = access_of_code(&code, grammar); + let blanks_inside = code + .iter() + .any(|token| token.kind == Kind::Word && token.text.contains(is_blank)); + match blanks_inside { + true => whole.max(access_of_code(&split_at_blanks(&code), grammar)), + false => whole, + } +} + +fn access_of_code(code: &[Code<'_>], grammar: SqlGrammar) -> StatementAccess { + let code = explained_pragma(code, grammar); + if code.iter().all(|token| token.kind == Kind::End) || plans_only(code, grammar) { return StatementAccess::Read; } - if destructive(&code, grammar) { + if destructive(code, grammar) { return StatementAccess::Destructive; } // A SQL Server batch is one statement here, so each part between @@ -238,13 +267,51 @@ fn plans_only(code: &[Code<'_>], grammar: SqlGrammar) -> bool { .first() .is_some_and(|first| first.is_word(&["EXPLAIN", "DESCRIBE", "DESC"])) && !code.iter().any(|token| token.is_word(&["ANALYZE", "ANALYSE"])) + && !quotes_an_option(code, grammar) && code.iter().rev().skip(1).all(|token| token.kind != Kind::End) } +/// PostgreSQL takes a quoted name for an option too, so +/// `EXPLAIN ("analyze")` runs the statement, and `U&"\0061nalyze"` +/// spells the same name. No option needs quotes, so a list that has them +/// may run what it explains. A parenthesis that opens on a query is that +/// query, not a list. +fn quotes_an_option(code: &[Code<'_>], grammar: SqlGrammar) -> bool { + code.get(1).is_some_and(|second| second.kind == Kind::Open) + && !code.get(2..).is_some_and(|rest| opens_as_read(rest, grammar)) + && code + .iter() + .skip(2) + .take_while(|token| token.kind != Kind::Close || token.depth > 0) + .any(|token| token.kind == Kind::Quoted) +} + +/// SQLite applies a pragma such as `foreign_keys = OFF` while it compiles +/// the statement, and EXPLAIN compiles what it explains, so an explained +/// pragma is read as the pragma itself. +fn explained_pragma<'code, 'sql>(code: &'code [Code<'sql>], grammar: SqlGrammar) -> &'code [Code<'sql>] { + if grammar != SqlGrammar::Sqlite { + return code; + } + let explained = match code { + [explain, query, plan, rest @ ..] + if explain.is_word(&["EXPLAIN"]) && query.is_word(&["QUERY"]) && plan.is_word(&["PLAN"]) => + { + rest + } + [explain, rest @ ..] if explain.is_word(&["EXPLAIN"]) => rest, + _ => return code, + }; + match explained.first().is_some_and(|first| first.is_word(&["PRAGMA"])) { + true => explained, + false => code, + } +} + /// Whether a word in `part` writes wherever it stands. Every SHOW only /// reports, `SHOW CREATE TABLE` included. fn writes(part: &[Code<'_>], grammar: SqlGrammar) -> bool { - if part.first().is_some_and(|first| first.is_word(&["SHOW"])) { + if grammar != SqlGrammar::MsSql && part.first().is_some_and(|first| first.is_word(&["SHOW"])) { return false; } let follows = |index: usize, set: &[&str]| index > 0 && part[index - 1].is_word(set); @@ -256,16 +323,51 @@ fn writes(part: &[Code<'_>], grammar: SqlGrammar) -> bool { || (is(token.text, &["SHARE"]) && follows(index, &["FOR", "KEY", "IN"])) // `NEXT VALUE FOR` takes a number from a sequence. || (is(token.text, &["VALUE"]) && follows(index, &["NEXT"])) - || calls_mutator(part, index)) + || calls_mutator(part, index, grammar)) } - Kind::Quoted => calls_mutator(part, index), + Kind::Quoted => calls_mutator(part, index, grammar), _ => false, }) } /// A call of a built-in that changes state, its name quoted or not. -fn calls_mutator(part: &[Code<'_>], index: usize) -> bool { - is(part[index].text, &MUTATING_FUNCTIONS) && part.get(index + 1).is_some_and(|next| next.kind == Kind::Open) +/// PostgreSQL decodes the escapes in `U&"set\0076al"` before it looks the +/// name up, under an escape character `UESCAPE` may change, so a name +/// spelled that way is compared with nothing and may be any of them. +fn calls_mutator(part: &[Code<'_>], index: usize, grammar: SqlGrammar) -> bool { + let arguments = match decodes_unicode_names(grammar) && spelled_in_escapes(part, index) { + true => after_uescape(part, index + 1), + false if changes_state(part[index].text) => index + 1, + false => return false, + }; + part.get(arguments).is_some_and(|next| next.kind == Kind::Open) +} + +/// The grammars whose scanner is PostgreSQL's, which reads `U&"…"`. +fn decodes_unicode_names(grammar: SqlGrammar) -> bool { + matches!(grammar, SqlGrammar::PostgreSql | SqlGrammar::DuckDb) +} + +/// A `U&"…"` name, whose prefix the lexer reads as the word `U` and `&`, +/// written with nothing between them, since `u & "f"(1)` is an AND. +fn spelled_in_escapes(part: &[Code<'_>], index: usize) -> bool { + part[index].kind == Kind::Quoted + && index >= 2 + && part[index - 1].kind == Kind::Other + && part[index - 1].text == "&" + && part[index - 2].is_word(&["U"]) + && part[index - 2].text.as_ptr() as usize + 3 == part[index].text.as_ptr() as usize +} + +/// The index past a `UESCAPE '!'` at `index`, or `index` where there is +/// none. A string can go on in another on the next line, so it is as +/// many tokens as stand there. +fn after_uescape(part: &[Code<'_>], index: usize) -> usize { + if !part.get(index).is_some_and(|word| word.is_word(&["UESCAPE"])) { + return index; + } + let strings = part[index + 1..].iter().take_while(|token| token.kind == Kind::Literal); + index + 1 + strings.count() } /// A verb MySQL also has as a function, called as one. @@ -280,6 +382,7 @@ fn opens_as_read(part: &[Code<'_>], grammar: SqlGrammar) -> bool { match first.kind { Kind::Open => true, Kind::Word if is(first.text, &["PRAGMA"]) => grammar == SqlGrammar::Sqlite && reporting_pragma(part), + Kind::Word if grammar == SqlGrammar::MsSql => is(first.text, &TSQL_READ_VERBS), // CQL's LIST ROLES, LIST USERS and LIST PERMISSIONS report. Kind::Word => is(first.text, &READ_VERBS) || (grammar == SqlGrammar::Cql && is(first.text, &["LIST"])), _ => false, @@ -297,6 +400,8 @@ enum Kind { End, /// A quoted name, its text without the quotes. Quoted, + /// A string or a dollar-quoted body. + Literal, Other, } @@ -341,6 +446,7 @@ fn code_of<'a>(sql: &'a str, tokens: &[ScriptToken]) -> Vec> { ScriptTokenKind::Semicolon if depth == 0 => Kind::End, ScriptTokenKind::Word => Kind::Word, ScriptTokenKind::QuotedIdentifier => Kind::Quoted, + ScriptTokenKind::StringLiteral | ScriptTokenKind::DollarQuoted => Kind::Literal, ScriptTokenKind::Punct if text == "," => Kind::Comma, ScriptTokenKind::Punct if text == "=" => Kind::Equals, _ => Kind::Other, @@ -358,6 +464,24 @@ fn code_of<'a>(sql: &'a str, tokens: &[ScriptToken]) -> Vec> { .collect() } +/// `code` with each word cut where it holds a Unicode blank. +fn split_at_blanks<'a>(code: &[Code<'a>]) -> Vec> { + code.iter() + .flat_map(|token| { + let pieces: Vec> = match token.kind { + Kind::Word => token + .text + .split(is_blank) + .filter(|piece| !piece.is_empty()) + .map(|text| Code { text, ..*token }) + .collect(), + _ => vec![*token], + }; + pieces + }) + .collect() +} + /// Whether some statement in `code` drops, empties, or changes every row /// of a table. Once a routine or trigger is being defined, the rest is /// its body, which does not run now. @@ -393,7 +517,7 @@ fn defines_routine(statement: &[Code<'_>]) -> bool { /// Server batch written without `;`; one in parentheses is a /// data-modifying CTE. fn statement_destroys(code: &[Code<'_>], grammar: SqlGrammar) -> bool { - let starts = statement_starts(code); + let starts = statement_starts(code, grammar); // Each start's segment runs to the next start at its depth, found in // one pass from the end. let mut next_at_depth: HashMap = HashMap::new(); @@ -411,7 +535,7 @@ fn statement_destroys(code: &[Code<'_>], grammar: SqlGrammar) -> bool { .any(|(&start, end)| segment_destroys(&code[start..end], grammar)) } -fn statement_starts(code: &[Code<'_>]) -> Vec { +fn statement_starts(code: &[Code<'_>], grammar: SqlGrammar) -> Vec { let mut starts = Vec::new(); let mut lead: Option<&str> = None; for (index, token) in code.iter().enumerate() { @@ -420,8 +544,10 @@ fn statement_starts(code: &[Code<'_>]) -> Vec { None => token.kind == Kind::Word, Some(before) if token.is_word(&["DELETE", "UPDATE"]) && before.kind == Kind::Open => true, Some(before) if token.depth == 0 && token.is_word(&STATEMENT_VERBS) => { - let led_in = before.is_word(&CLAUSE_WORDS) || before.kind == Kind::Comma; - let alters = lead.is_some_and(|lead| is(lead, &["ALTER"])) && token.is_word(&["DROP", "ALTER"]); + let led_in = leads_in(before, &code[index..], grammar) || before.kind == Kind::Comma; + let alters = lead.is_some_and(|lead| is(lead, &["ALTER"])) + && token.is_word(&["DROP", "ALTER"]) + && !(grammar == SqlGrammar::MsSql && drops_an_object(&code[index..])); !led_in && !alters } Some(_) => false, @@ -436,6 +562,27 @@ fn statement_starts(code: &[Code<'_>]) -> Vec { starts } +/// Whether `before` keeps the verb at `code[0]` inside its statement. +fn leads_in(before: &Code<'_>, code: &[Code<'_>], grammar: SqlGrammar) -> bool { + if grammar != SqlGrammar::MsSql { + return before.is_word(&CLAUSE_WORDS); + } + if !before.is_word(&["ON"]) { + return before.is_word(&TSQL_CLAUSE_WORDS); + } + let word = |at: usize, set: &[&str]| code.get(at).is_some_and(|token| token.is_word(set)); + word(0, &["DELETE", "UPDATE"]) + && (word(1, &["CASCADE"]) + || (word(1, &["SET"]) && word(2, &["NULL", "DEFAULT"])) + || (word(1, &["NO"]) && word(2, &["ACTION"]))) +} + +/// A DROP at `code[0]` that names a kind of object only a DROP statement +/// takes. +fn drops_an_object(code: &[Code<'_>]) -> bool { + code[0].is_word(&["DROP"]) && code.get(1).is_some_and(|kind| kind.is_word(&TSQL_DROPPED_OBJECTS)) +} + /// The statement that starts at `code[0]` and runs to the end of `code` /// or the parenthesis that closes it. fn segment_destroys(code: &[Code<'_>], grammar: SqlGrammar) -> bool { @@ -469,7 +616,13 @@ fn reporting_pragma(code: &[Code<'_>]) -> bool { .filter(|token| token.kind == Kind::Word) .last(); let assigns = code.iter().any(|token| token.kind == Kind::Equals); - !assigns && name.is_some_and(|name| is(name.text, &REPORTING_PRAGMAS)) + !assigns && name.is_some_and(|name| sqlite_pragma_reports(name.text)) +} + +/// Whether the SQLite pragma `name` only reports, whatever argument it +/// is given. Any other pragma can change the database or the connection. +pub fn sqlite_pragma_reports(name: &str) -> bool { + is(name, &REPORTING_PRAGMAS) } fn is(word: &str, set: &[&str]) -> bool { @@ -494,7 +647,6 @@ mod tests { "/* UPDATE orders */ SELECT 1", "(SELECT 1) UNION (SELECT 2)", "WITH recent AS (SELECT * FROM orders) SELECT * FROM recent", - "EXPLAIN SELECT * FROM orders", "SELECT replace(name, 'a', 'b') FROM orders", "", "-- only a note", @@ -502,6 +654,16 @@ mod tests { assert_eq!(access(sql, grammar), StatementAccess::Read, "{grammar:?}: {sql}"); } } + for grammar in SqlGrammar::SQL + .into_iter() + .filter(|grammar| *grammar != SqlGrammar::MsSql) + { + assert_eq!( + access("EXPLAIN SELECT * FROM orders", grammar), + StatementAccess::Read, + "{grammar:?}" + ); + } } #[test] @@ -623,7 +785,6 @@ mod tests { "UPDATE orders SET total = 0 WHERE id = 1", "ALTER TABLE orders DROP COLUMN note", "CREATE TABLE lines (order_id int REFERENCES orders ON DELETE CASCADE)", - "INSERT INTO orders VALUES (1) ON CONFLICT (id) DO UPDATE SET total = 0", "SELECT * FROM orders FOR UPDATE", "GRANT SELECT, UPDATE, DELETE ON orders TO someone", "CREATE TRIGGER t AFTER UPDATE OF total ON orders FOR EACH ROW EXECUTE FUNCTION f()", @@ -632,6 +793,14 @@ mod tests { assert_ne!(access(sql, grammar), StatementAccess::Destructive, "{grammar:?}: {sql}"); } } + // T-SQL has no ON CONFLICT, and DO is a name there. + for grammar in SqlGrammar::SQL + .into_iter() + .filter(|grammar| *grammar != SqlGrammar::MsSql) + { + let sql = "INSERT INTO orders VALUES (1) ON CONFLICT (id) DO UPDATE SET total = 0"; + assert_ne!(access(sql, grammar), StatementAccess::Destructive, "{grammar:?}: {sql}"); + } } #[test] @@ -932,4 +1101,276 @@ mod tests { assert_eq!(access(sql, SqlGrammar::MySql), StatementAccess::Destructive); assert_eq!(access(sql, SqlGrammar::PostgreSql), StatementAccess::Destructive); } + + #[test] + fn a_quote_inside_a_sqlite_parameter_opens_no_string() { + for sql in [ + "WITH c AS (SELECT $1(')) DELETE FROM orders --'", + "WITH c AS (SELECT $a::b(')) DELETE FROM orders --'", + "WITH c AS (SELECT @a(')) DELETE FROM orders --'", + "WITH c AS (SELECT :a(')) DELETE FROM orders --'", + "WITH c AS (SELECT #a(')) DELETE FROM orders --'", + ] { + assert_eq!(access(sql, SqlGrammar::Sqlite), StatementAccess::Destructive, "{sql}"); + } + for sql in [ + "SELECT $1('); ATTACH DATABASE 'other.db' AS other; --'", + "SELECT $1('); VACUUM INTO 'copy.db'; --'", + ] { + assert_eq!(access(sql, SqlGrammar::Sqlite), StatementAccess::Write, "{sql}"); + } + for sql in [ + "SELECT $1, $name, :name, @name FROM orders", + "SELECT $1( '; DELETE FROM orders; --')", + "SELECT a$1('; DELETE FROM orders; --')", + ] { + assert_eq!(access(sql, SqlGrammar::Sqlite), StatementAccess::Read, "{sql}"); + } + } + + #[test] + fn a_string_that_continues_an_escape_string_is_read_as_one() { + // The server carries `E'x'` on into a string on the next line and + // reads that with backslash escapes on, so the first `\'` is an + // escaped quote and the COMMIT is code. + for sql in [ + "SELECT E'x'\n'\\' || ' || '\\' ; COMMIT; DELETE FROM orders; --'", + "SELECT e'x' -- note\r\n -- note\n\t'\\' || ' || '\\' ; COMMIT; DELETE FROM orders; --'", + "SELECT E'x'\n'y'\n'\\' || ' || '\\' ; COMMIT; DELETE FROM orders; --'", + ] { + for grammar in [SqlGrammar::PostgreSql, SqlGrammar::DuckDb] { + assert_eq!( + access(sql, grammar), + StatementAccess::Destructive, + "{grammar:?}: {sql:?}" + ); + } + } + for sql in [ + "SELECT E'x'\n'\\'; DELETE FROM orders; --'", + "SELECT E'a\\'b'\n'c\\'d' FROM orders", + ] { + assert_eq!(access(sql, SqlGrammar::PostgreSql), StatementAccess::Read, "{sql:?}"); + } + } + + #[test] + fn a_unicode_blank_before_a_keyword_does_not_hide_it() { + for (sql, expected) in [ + ("\u{feff}DROP TABLE t", StatementAccess::Destructive), + ("SELECT 1;\u{feff}DELETE FROM t", StatementAccess::Destructive), + ( + "WITH x AS (SELECT 1) \u{feff}DELETE FROM t", + StatementAccess::Destructive, + ), + ( + "WITH x AS (SELECT 1) \u{feff}INSERT \u{feff}INTO t VALUES (1)", + StatementAccess::Write, + ), + ("\u{feff}SELECT 1", StatementAccess::Read), + ] { + assert_eq!(access(sql, SqlGrammar::Sqlite), expected, "{sql:?}"); + } + // Whether SQL Server or ClickHouse skip such a character is not + // known here, so the stricter reading stands. + for grammar in [SqlGrammar::MsSql, SqlGrammar::ClickHouse] { + for sql in [ + "SELECT 1\n\u{feff}DROP TABLE t", + "SELECT 1\n\u{200b}DROP TABLE t", + "SELECT 1\n\u{a0}DROP TABLE t", + ] { + assert_eq!( + access(sql, grammar), + StatementAccess::Destructive, + "{grammar:?}: {sql:?}" + ); + } + assert!( + access("SEL\u{feff}ECT 1", grammar) >= StatementAccess::Write, + "{grammar:?}" + ); + } + } + + #[test] + fn a_sql_server_batch_that_opens_with_the_name_show_is_not_a_read() { + for sql in [ + "SHOW: SELECT 1", + "SHOW: UPDATE orders SET total = 0 WHERE id = 1", + "SHOW: COMMIT TRANSACTION UPDATE orders SET total = 0 WHERE id = 1 BEGIN TRANSACTION", + "SHOW: EXEC('DROP TABLE orders')", + "SHOW: EXEC xp_cmdshell 'whoami'", + "SELECT 1; show : BACKUP DATABASE shop TO DISK = 'x'", + "SHOW INSERT INTO orders VALUES (1)", + "EXPLAIN 'x'", + "DESCRIBE orders", + "VALUES (1)", + "TABLE orders", + ] { + assert_eq!(access(sql, SqlGrammar::MsSql), StatementAccess::Write, "{sql}"); + } + assert_eq!( + access("SHOW: DELETE FROM orders", SqlGrammar::MsSql), + StatementAccess::Destructive + ); + } + + #[test] + fn an_explain_with_a_quoted_option_may_run_what_it_explains() { + for (sql, expected) in [ + ("EXPLAIN (\"analyze\") DELETE FROM orders", StatementAccess::Destructive), + ( + "EXPLAIN (COSTS off, \"analyze\" true) DELETE FROM orders", + StatementAccess::Destructive, + ), + ( + "EXPLAIN (U&\"\\0061nalyze\") DELETE FROM orders", + StatementAccess::Destructive, + ), + ( + "EXPLAIN (\"analyze\") SELECT pg_terminate_backend(42)", + StatementAccess::Write, + ), + ] { + assert_eq!(access(sql, SqlGrammar::PostgreSql), expected, "{sql}"); + } + for sql in [ + "EXPLAIN (COSTS false) DELETE FROM \"analyze\"", + "EXPLAIN (FORMAT 'json') DELETE FROM orders", + "EXPLAIN (WITH gone AS (SELECT * FROM \"orders\") SELECT * FROM gone)", + ] { + assert_eq!(access(sql, SqlGrammar::PostgreSql), StatementAccess::Read, "{sql}"); + } + } + + #[test] + fn an_explained_pragma_is_read_as_the_pragma_on_sqlite() { + for sql in [ + "EXPLAIN PRAGMA foreign_keys = OFF", + "explain pragma main.trusted_schema = ON", + "EXPLAIN QUERY PLAN PRAGMA writable_schema(ON)", + "EXPLAIN /* plan */ PRAGMA hard_heap_limit = 1", + "EXPLAIN QUERY PLAN PRAGMA recursive_triggers = ON", + "EXPLAIN PRAGMA optimize", + ] { + assert_eq!(access(sql, SqlGrammar::Sqlite), StatementAccess::Write, "{sql}"); + } + for sql in [ + "EXPLAIN PRAGMA table_info(orders)", + "EXPLAIN QUERY PLAN PRAGMA main.index_list('orders')", + "EXPLAIN QUERY PLAN SELECT * FROM orders", + "EXPLAIN DELETE FROM orders", + ] { + assert_eq!(access(sql, SqlGrammar::Sqlite), StatementAccess::Read, "{sql}"); + } + } + + #[test] + fn a_function_that_runs_sql_text_or_signals_writes() { + for sql in [ + "SELECT dblink('host=db', 'DELETE FROM orders')", + "SELECT * FROM dblink('host=db', 'DELETE FROM orders') AS t(n int)", + "SELECT dblink_send_query('c', 'DELETE FROM orders')", + "SELECT dblink_open('c', 'cur', 'SELECT 1')", + "SELECT query_to_xml('DELETE FROM orders RETURNING *', true, false, '')", + "SELECT ts_stat('DELETE FROM orders RETURNING note::tsvector')", + "SELECT lo_creat(-1)", + "SELECT lowrite(0, 'x')", + "SELECT lo_truncate(0, 0)", + "SELECT pg_notify('jobs', 'run')", + "SELECT pg_advisory_lock(1)", + "SELECT pg_logical_slot_get_changes('s', NULL, NULL)", + "SELECT pg_replication_origin_create('o')", + "SELECT pg_stat_reset_shared('bgwriter')", + "SELECT pg_wal_replay_pause()", + "SELECT gin_clean_pending_list('i'::regclass)", + ] { + assert_eq!(access(sql, SqlGrammar::PostgreSql), StatementAccess::Write, "{sql}"); + } + for sql in ["SELECT GET_LOCK('l', 1)", "SELECT release_all_locks()"] { + assert_eq!(access(sql, SqlGrammar::MySql), StatementAccess::Write, "{sql}"); + } + assert_eq!( + access( + "SELECT dblink_build_sql_insert('t', '1', 1, '{1}', '{2}')", + SqlGrammar::PostgreSql + ), + StatementAccess::Read + ); + } + + #[test] + fn a_call_of_a_name_spelled_in_escapes_writes() { + for sql in [ + r#"SELECT U&"set\0076al"('orders_id_seq', 1, false)"#, + r#"SELECT pg_catalog.u&"pg_terminate_backen\0064"(pid) FROM pg_stat_activity"#, + r#"SELECT U&"pg_terminate_backen\+000064" /* all */ (pid) FROM pg_stat_activity"#, + r#"SELECT U&"setv!0061l" UESCAPE '!' ('orders_id_seq', 1, false)"#, + r#"SELECT U&"setv!0061l" uescape E'!'('orders_id_seq', 1, false)"#, + r#"SELECT U&"setv!0061l" UESCAPE $$!$$ ('orders_id_seq', 1, false)"#, + "SELECT U&\"setv!0061l\" UESCAPE '!'\n'' ('orders_id_seq', 1, false)", + ] { + assert_eq!(access(sql, SqlGrammar::PostgreSql), StatementAccess::Write, "{sql}"); + } + for sql in [ + r#"SELECT U&"d\0061ta", u & "mask" FROM orders"#, + r#"SELECT U&"d!0061ta" UESCAPE '!' FROM orders WHERE id IN (1, 2)"#, + r#"SELECT U&'d\0061ta', count(*) FROM orders"#, + r#"SELECT U&"pg_terminate_backen\0064" FROM pg_proc"#, + ] { + assert_eq!(access(sql, SqlGrammar::PostgreSql), StatementAccess::Read, "{sql}"); + } + assert_eq!( + access( + r#"SELECT U&"price" UESCAPE '!' + (1) FROM items"#, + SqlGrammar::PostgreSql + ), + StatementAccess::Read + ); + // No other grammar decodes a name, and a spaced `&` is an AND. + assert_eq!( + access(r#"SELECT U & "f"(1) FROM t"#, SqlGrammar::Sqlite), + StatementAccess::Read + ); + assert_eq!( + access( + r#"SELECT u & "abs"(-3) FROM (VALUES (7)) AS t(u)"#, + SqlGrammar::PostgreSql + ), + StatementAccess::Read + ); + } + + #[test] + fn a_sql_server_statement_that_ends_in_a_clause_word_still_ends() { + for sql in [ + "SET NOCOUNT ON\nDROP TABLE orders", + "SET XACT_ABORT ON\nTRUNCATE TABLE orders", + "SET IDENTITY_INSERT orders ON\nDELETE FROM orders", + "SET IDENTITY_INSERT orders ON\nDELETE orders", + "SET NOCOUNT ON\nUPDATE orders SET total = 0", + "SELECT 1\nSET NOCOUNT ON -- quiet\nDROP TABLE orders", + "CLOSE MASTER KEY\nDROP TABLE orders", + "SELECT * FROM before\nDROP TABLE orders", + "SELECT * FROM after\nDELETE FROM orders", + "SELECT * FROM instead\nTRUNCATE TABLE orders", + "SELECT * FROM do\nUPDATE orders SET total = 0", + "ALTER TABLE t ADD x int\nDROP TABLE y", + "ALTER TABLE t ADD x int\nDELETE FROM y", + ] { + assert_eq!(access(sql, SqlGrammar::MsSql), StatementAccess::Destructive, "{sql}"); + } + for sql in [ + "ALTER TABLE lines ADD CONSTRAINT fk FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE", + "ALTER TABLE lines ADD CONSTRAINT fk FOREIGN KEY (order_id) REFERENCES orders (id) ON UPDATE SET NULL", + "ALTER TABLE lines ADD CONSTRAINT fk FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE NO ACTION", + "SET NOCOUNT ON\nDELETE FROM orders WHERE id = 1", + "ALTER TABLE orders DROP COLUMN note", + "ALTER TABLE orders DROP CONSTRAINT orders_pk", + "MERGE INTO t USING s ON t.id = s.id WHEN MATCHED THEN DELETE", + "DECLARE c CURSOR FOR SELECT id FROM orders FOR UPDATE", + ] { + assert_eq!(access(sql, SqlGrammar::MsSql), StatementAccess::Write, "{sql}"); + } + } } diff --git a/linux/crates/core/src/unicode_blank.rs b/linux/crates/core/src/unicode_blank.rs new file mode 100644 index 0000000000..ee6d8ecce6 --- /dev/null +++ b/linux/crates/core/src/unicode_blank.rs @@ -0,0 +1,50 @@ +//! Characters that show as nothing, which a reader cannot tell apart +//! from a space or from no character at all. + +/// Unicode's Default_Ignorable_Code_Point: U+200B, U+FEFF and the rest a +/// renderer draws as nothing. +pub fn is_default_ignorable(c: char) -> bool { + matches!( + u32::from(c), + 0x00AD + | 0x034F + | 0x061C + | 0x115F..=0x1160 + | 0x17B4..=0x17B5 + | 0x180B..=0x180F + | 0x200B..=0x200F + | 0x202A..=0x202E + | 0x2060..=0x206F + | 0x3164 + | 0xFE00..=0xFE0F + | 0xFEFF + | 0xFFA0 + | 0xFFF0..=0xFFF8 + | 0x1BCA0..=0x1BCA3 + | 0x1D173..=0x1D17A + | 0xE0000..=0xE0FFF + ) +} + +/// White space, or a character that shows as nothing. +pub fn is_blank(c: char) -> bool { + c.is_whitespace() || is_default_ignorable(c) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn invisible_characters_are_blank_and_letters_are_not() { + for c in [ + '\u{feff}', '\u{200b}', '\u{00ad}', '\u{2060}', '\u{a0}', ' ', '\u{3000}', + ] { + assert!(is_blank(c), "{:04x}", u32::from(c)); + } + for c in ['a', 'é', '-', '_', '\u{4e00}'] { + assert!(!is_blank(c), "{c}"); + } + assert!(!is_default_ignorable('\u{a0}')); + } +} diff --git a/linux/crates/core/tests/script_corpus.rs b/linux/crates/core/tests/script_corpus.rs index 755f0cfde7..843cffbc63 100644 --- a/linux/crates/core/tests/script_corpus.rs +++ b/linux/crates/core/tests/script_corpus.rs @@ -73,6 +73,124 @@ fn escape_string_backslash_quote() { ); } +/// A string that follows `E'...'` after a line break continues it, and +/// the server reads it with backslash escapes on, so `\'` does not end it. +#[test] +fn escape_string_continues_past_a_line_break() { + for grammar in [SqlGrammar::PostgreSql, SqlGrammar::DuckDb] { + for continued in [ + "SELECT E'a'\n'\\'; b'", + "SELECT E'a'\r'\\'; b'", + "SELECT E'a' \t\x0c-- note\n\x0b -- note\r\n '\\'; b'", + "SELECT E'a'\n'b'\n'\\'; c'", + ] { + let text = format!("{continued}; SELECT 2;"); + assert_eq!( + statements(grammar, &text), + [continued, "SELECT 2"], + "{grammar:?}: {continued:?}" + ); + } + } + // Without a line break, past a block comment, past anything but a + // blank or a `--` comment, or after a vertical tab before the line + // break, the server ends the string and so must the lexer. + for apart in [ + "SELECT E'a' '\\'", + "SELECT E'a' /* note */\n'\\'", + "SELECT E'a'\n, '\\'", + "SELECT E'a'\x0b\n'\\'", + ] { + let text = format!("{apart}; SELECT 2;"); + assert_eq!( + statements(SqlGrammar::PostgreSql, &text), + [apart, "SELECT 2"], + "{apart:?}" + ); + } + assert_eq!( + plan(SqlGrammar::PostgreSql, "SELECT E'a'\n'b").diagnostics(), + [ScriptDiagnostic::Unterminated { + construct: OpenConstruct::StringLiteral, + start: 7, + }] + ); +} + +#[test] +fn a_sqlite_parameter_runs_to_its_closing_parenthesis() { + for parameter in [ + "$1(')", + "$a::b(;)", + "@a(\")", + ":a(`)", + "#a([)", + "$a(\u{1b}')", + ":::a(')", + ] { + let text = format!("SELECT {parameter}; SELECT 2; --'"); + assert_eq!( + statements(SqlGrammar::Sqlite, &text), + [format!("SELECT {parameter}").as_str(), "SELECT 2"], + "{parameter}" + ); + } + // White space ends it early, and one of no name is none. + for text in [ + "SELECT $1( '; SELECT 2; --')", + "SELECT $1(\n'; SELECT 2; --')", + "SELECT $('; SELECT 2; --')", + "SELECT $::('; SELECT 2; --')", + "SELECT a$1('; SELECT 2; --')", + "SELECT ?1('; SELECT 2; --')", + ] { + assert_eq!(statements(SqlGrammar::Sqlite, text), [text], "{text}"); + } + for grammar in [SqlGrammar::PostgreSql, SqlGrammar::MySql, SqlGrammar::MsSql] { + let text = "SELECT $1('); SELECT 2; --')"; + assert_eq!(statements(grammar, text), [text], "{grammar:?}"); + } + + let text = "SELECT $a::b, $a::b(c;d), $e("; + let words: Vec<&str> = plan(SqlGrammar::Sqlite, text) + .tokens(0..text.len()) + .filter(|token| token.kind == ScriptTokenKind::Word) + .map(|token| &text[token.range.clone()]) + .collect(); + assert_eq!(words, ["SELECT", "$a::b", "$a::b(c;d)", "$e("]); +} + +#[test] +fn a_byte_order_mark_is_a_space_where_sqlite_starts_a_token() { + let words = |grammar: SqlGrammar, text: &str| -> Vec { + plan(grammar, text) + .tokens(0..text.len()) + .filter(|token| token.kind == ScriptTokenKind::Word) + .map(|token| text[token.range.clone()].to_owned()) + .collect() + }; + + assert_eq!( + words(SqlGrammar::Sqlite, "\u{feff}DROP \u{feff}\u{feff}TABLE t"), + ["DROP", "TABLE", "t"] + ); + assert_eq!( + words(SqlGrammar::Sqlite, "DROP\u{feff} TABLE t"), + ["DROP\u{feff}", "TABLE", "t"] + ); + assert_eq!( + statements(SqlGrammar::Sqlite, "SELECT 1;\u{feff}DELETE FROM t;"), + ["SELECT 1", "DELETE FROM t"] + ); + for grammar in [SqlGrammar::PostgreSql, SqlGrammar::MySql] { + assert_eq!( + words(grammar, "\u{feff}DROP TABLE t"), + ["\u{feff}DROP", "TABLE", "t"], + "{grammar:?}" + ); + } +} + #[test] fn backslash_plain_string_when_enabled_vs_disabled() { let text = r"SELECT 'a\'; SELECT 'b';"; diff --git a/linux/crates/drivers/cassandra/src/cql_type.rs b/linux/crates/drivers/cassandra/src/cql_type.rs index 1e630a494c..8bdd79074a 100644 --- a/linux/crates/drivers/cassandra/src/cql_type.rs +++ b/linux/crates/drivers/cassandra/src/cql_type.rs @@ -213,20 +213,26 @@ impl CqlType { /// The type a result's metadata gives a column. pub(crate) fn from_wire(wire: &WireType<'_>) -> Self { + Self::from_wire_at(wire, 0) + } + + fn from_wire_at(wire: &WireType<'_>, depth: usize) -> Self { + if depth > MAX_NESTING { + return Self::Custom("a type nested too deep".to_owned()); + } + let inner = |wire: &WireType<'_>| Box::new(Self::from_wire_at(wire, depth + 1)); match wire { WireType::Native(native) => Self::Native(native_of(native)), WireType::Collection { frozen, typ } => { let collection = match typ { - CollectionType::List(element) => Self::List(Box::new(Self::from_wire(element))), - CollectionType::Set(element) => Self::Set(Box::new(Self::from_wire(element))), - CollectionType::Map(key, value) => { - Self::Map(Box::new(Self::from_wire(key)), Box::new(Self::from_wire(value))) - } + CollectionType::List(element) => Self::List(inner(element)), + CollectionType::Set(element) => Self::Set(inner(element)), + CollectionType::Map(key, value) => Self::Map(inner(key), inner(value)), _ => Self::Custom(format!("{typ:?}")), }; frozen_if(*frozen, collection) } - WireType::Vector { typ, dimensions } => Self::Vector(Box::new(Self::from_wire(typ)), *dimensions), + WireType::Vector { typ, dimensions } => Self::Vector(inner(typ), *dimensions), WireType::UserDefinedType { frozen, definition } => { let udt = Self::Udt { keyspace: None, @@ -234,9 +240,12 @@ impl CqlType { }; frozen_if(*frozen, udt) } - WireType::Tuple(elements) => { - Self::Frozen(Box::new(Self::Tuple(elements.iter().map(Self::from_wire).collect()))) - } + WireType::Tuple(elements) => Self::Frozen(Box::new(Self::Tuple( + elements + .iter() + .map(|element| Self::from_wire_at(element, depth + 1)) + .collect(), + ))), other => Self::Custom(format!("{other:?}")), } } @@ -244,7 +253,7 @@ impl CqlType { /// The type `system_schema.columns` writes, such as /// `frozen>`. `None` for text no type grammar reads. pub(crate) fn parse(text: &str) -> Option { - let mut parser = Parser { text, at: 0 }; + let mut parser = Parser { text, at: 0, depth: 0 }; let parsed = parser.cql_type()?; parser.skip_space(); (parser.at == text.len()).then_some(parsed) @@ -325,9 +334,15 @@ fn quote_type_name(name: &str) -> String { } } +/// Deeper than any type a schema declares, and shallow enough that the +/// walkers over a type, its drop included, stay well inside a worker's +/// stack whatever the server sends. +const MAX_NESTING: usize = 32; + struct Parser<'a> { text: &'a str, at: usize, + depth: usize, } impl Parser<'_> { @@ -377,6 +392,16 @@ impl Parser<'_> { } fn cql_type(&mut self) -> Option { + if self.depth >= MAX_NESTING { + return None; + } + self.depth += 1; + let parsed = self.nested_type(); + self.depth -= 1; + parsed + } + + fn nested_type(&mut self) -> Option { self.skip_space(); if self.text[self.at..].starts_with('\'') { let rest = &self.text[self.at + 1..]; @@ -544,4 +569,23 @@ mod tests { assert!(parsed("frozen>").is_opaque_composite()); assert!(!parsed("frozen>").is_opaque_composite()); } + + #[test] + fn a_type_nested_past_the_limit_is_not_read() { + let deep = format!("{}int{}", "list<".repeat(100_000), ">".repeat(100_000)); + assert_eq!(CqlType::parse(&deep), None); + + let worker = std::thread::Builder::new().stack_size(2 << 20); + let handle = worker + .spawn(|| { + // The element is a level too. + let levels = MAX_NESTING - 1; + let limit = format!("{}int{}", "list<".repeat(levels), ">".repeat(levels)); + let parsed = CqlType::parse(&limit); + assert!(parsed.is_some()); + assert_eq!(parsed.map(|parsed| parsed.to_string()), Some(limit)); + }) + .unwrap(); + handle.join().unwrap(); + } } diff --git a/linux/crates/drivers/cassandra/src/dialect.rs b/linux/crates/drivers/cassandra/src/dialect.rs index 7a4850f713..ced163ac1f 100644 --- a/linux/crates/drivers/cassandra/src/dialect.rs +++ b/linux/crates/drivers/cassandra/src/dialect.rs @@ -457,6 +457,7 @@ const CREDENTIAL_RULES: CredentialRules = CredentialRules { (&["ALTER", "ROLE"], &["PASSWORD", "HASHED"]), ], carriers: &[], + secret_parameters: &[], }; #[cfg(test)] diff --git a/linux/crates/drivers/cassandra/src/rows.rs b/linux/crates/drivers/cassandra/src/rows.rs index 8608ca1d2f..9d2441e3a3 100644 --- a/linux/crates/drivers/cassandra/src/rows.rs +++ b/linux/crates/drivers/cassandra/src/rows.rs @@ -50,7 +50,8 @@ impl ColumnsRead { let iter = rows .rows::() .map_err(|error| DriverError::Protocol(error.to_string()))?; - let mut decoded = Vec::with_capacity(rows.rows_num()); + // Not sized by the count the server declares: the rows read bound it. + let mut decoded = Vec::new(); for row in iter { let row = row.map_err(|error| DriverError::Protocol(error.to_string()))?; decoded.push( diff --git a/linux/crates/drivers/clickhouse/src/clickhouse_type.rs b/linux/crates/drivers/clickhouse/src/clickhouse_type.rs index 59b8bbd22b..92a3a1a1f3 100644 --- a/linux/crates/drivers/clickhouse/src/clickhouse_type.rs +++ b/linux/crates/drivers/clickhouse/src/clickhouse_type.rs @@ -80,13 +80,41 @@ impl ClickHouseType { /// clickhouse-types slices a name at fixed byte offsets and at its last /// byte, which land on character boundaries only when the name is ASCII /// or ends in the closing parenthesis the server always prints. +/// +/// It also calls itself once per wrapper, on the stack, as do `kind_of` +/// and `CellPlan::of` on what it builds, so a name with more wrappers +/// than [`MAX_WRAPPERS`] is left to the shared classifier, which bounds +/// its own nesting. fn parse_node(text: &str) -> Option { if !text.is_ascii() && !text.ends_with(')') { return None; } + let wrappers: usize = WRAPPERS.iter().map(|name| text.matches(name).count()).sum(); + if wrappers > MAX_WRAPPERS { + return None; + } DataTypeNode::new(text).ok() } +/// The names clickhouse-types parses by calling itself on what they +/// wrap. It goes by the name and not by the parentheses around the inner +/// type (`Array!UInt8!` parses as `Array(UInt8)`), so the names are what +/// bounds how deep it recurses. +const WRAPPERS: [&str; 8] = [ + "Nullable", + "LowCardinality", + "Array", + "Map", + "Tuple", + "Variant", + "SimpleAggregateFunction", + "JSON", +]; + +/// More wrappers than any column declares, and few enough that a hostile +/// name cannot exhaust the stack of a runtime worker. +const MAX_WRAPPERS: usize = 64; + /// The type a value of this column is bound as: the wrappers change how /// it is stored, not what text the server parses for it, and a NULL is /// written as the literal rather than bound. @@ -315,4 +343,44 @@ mod tests { ); assert!(!array.nullable, "the element's NULL was read as the column's"); } + + #[test] + fn a_name_with_too_many_wrappers_is_read_as_server_text() { + // clickhouse-types takes `Array!UInt8!` for `Array(UInt8)`. + for (open, close) in [ + ("Array(", ")"), + ("Array!", "!"), + ("Map(String, ", ")"), + ("Nullable(", ")"), + ] { + let name = format!("{}UInt8{}", open.repeat(100_000), close.repeat(100_000)); + + assert_eq!(parsed(&name).plan, CellPlan::Raw { type_name: name }); + } + } + + #[test] + fn a_name_at_the_wrapper_limit_parses_on_a_worker_stack() { + // A runtime worker has the 2 MiB default stack. + let worker = std::thread::Builder::new().stack_size(2 << 20); + let handle = worker + .spawn(|| { + for (open, close) in [ + ("Array(", ")"), + ("Nullable(", ")"), + ("LowCardinality(", ")"), + ("Map(String, ", ")"), + ("Tuple(", ")"), + ("Variant(", ")"), + ("SimpleAggregateFunction(any, ", ")"), + ] { + let name = format!("{}UInt8{}", open.repeat(MAX_WRAPPERS), close.repeat(MAX_WRAPPERS)); + + assert!(parse_node(&name).is_some(), "{open}"); + assert_eq!(parsed(&name).column_type.name, name); + } + }) + .unwrap(); + handle.join().unwrap(); + } } diff --git a/linux/crates/drivers/clickhouse/src/dialect.rs b/linux/crates/drivers/clickhouse/src/dialect.rs index fc978cd8ab..e2ef2b7b74 100644 --- a/linux/crates/drivers/clickhouse/src/dialect.rs +++ b/linux/crates/drivers/clickhouse/src/dialect.rs @@ -424,6 +424,7 @@ const CREDENTIAL_RULES: CredentialRules = CredentialRules { "aes_encrypt_mysql", "aes_decrypt_mysql", ], + secret_parameters: &[], }; #[cfg(test)] diff --git a/linux/crates/drivers/duckdb/src/dialect.rs b/linux/crates/drivers/duckdb/src/dialect.rs index 5f04727449..316b34a906 100644 --- a/linux/crates/drivers/duckdb/src/dialect.rs +++ b/linux/crates/drivers/duckdb/src/dialect.rs @@ -377,6 +377,7 @@ const CREDENTIAL_RULES: CredentialRules = CredentialRules { "mysql_execute", "add_parquet_key", ], + secret_parameters: &[], }; #[cfg(test)] diff --git a/linux/crates/drivers/mongodb/src/catalog.rs b/linux/crates/drivers/mongodb/src/catalog.rs index 7d80aae8d3..74acb83b84 100644 --- a/linux/crates/drivers/mongodb/src/catalog.rs +++ b/linux/crates/drivers/mongodb/src/catalog.rs @@ -59,10 +59,7 @@ impl Reader<'_> { let mut documents = Vec::new(); loop { match cursor.advance(self.session).await { - Ok(true) => documents.push(cursor.deserialize_current().map_err(|error| DriverError::Decode { - column: String::new(), - detail: error.to_string(), - })?), + Ok(true) => documents.push(crate::nested_document::current_document(&cursor)?), Ok(false) => return Ok(documents), Err(error) => return Err(self.endpoint.explain(&error, LossPhase::Statement).await), } diff --git a/linux/crates/drivers/mongodb/src/cursor_rows.rs b/linux/crates/drivers/mongodb/src/cursor_rows.rs index 3867e0c715..07b716758e 100644 --- a/linux/crates/drivers/mongodb/src/cursor_rows.rs +++ b/linux/crates/drivers/mongodb/src/cursor_rows.rs @@ -140,10 +140,7 @@ async fn every( } fn current(cursor: &SessionCursor) -> Result { - cursor.deserialize_current().map_err(|error| DriverError::Decode { - column: String::new(), - detail: error.to_string(), - }) + crate::nested_document::current_document(cursor) } async fn finish(sink: &mut dyn ResultSink, retained: u64, total_rows: Option, truncation: Truncation) -> SinkFlow { diff --git a/linux/crates/drivers/mongodb/src/execute.rs b/linux/crates/drivers/mongodb/src/execute.rs index 1257116440..c476d3811a 100644 --- a/linux/crates/drivers/mongodb/src/execute.rs +++ b/linux/crates/drivers/mongodb/src/execute.rs @@ -73,7 +73,7 @@ pub(crate) async fn start( }); } let first = match cursor.advance(session).await { - Ok(true) => cursor.deserialize_current().ok(), + Ok(true) => crate::nested_document::current_document(&cursor).ok(), Ok(false) => None, Err(error) => return Err(endpoint.explain(&error, LossPhase::Statement).await), }; diff --git a/linux/crates/drivers/mongodb/src/lib.rs b/linux/crates/drivers/mongodb/src/lib.rs index b0a234b049..98745b90fb 100644 --- a/linux/crates/drivers/mongodb/src/lib.rs +++ b/linux/crates/drivers/mongodb/src/lib.rs @@ -25,6 +25,7 @@ mod errors; mod execute; mod extended_json; mod field_types; +mod nested_document; mod relay; mod rows; mod server_version; diff --git a/linux/crates/drivers/mongodb/src/nested_document.rs b/linux/crates/drivers/mongodb/src/nested_document.rs new file mode 100644 index 0000000000..7b35ba1118 --- /dev/null +++ b/linux/crates/drivers/mongodb/src/nested_document.rs @@ -0,0 +1,84 @@ +//! A cursor's documents, taken out only once their nesting is known. +//! +//! bson turns raw bytes into a `Document` by calling itself once per +//! level, and writing one out and dropping it do the same, so a document +//! nested deep enough would overflow the stack of the thread that reads +//! it. + +use bson::Document; +use bson::raw::{RawBsonRef, RawDocument}; +use mongodb::SessionCursor; +use tablepro_core::DriverError; + +/// Deeper than MongoDB stores a document (100 levels), so only a hostile +/// server sends one. +const MAX_DEPTH: usize = 128; + +/// The cursor's current document, refused when it nests deeper than +/// [`MAX_DEPTH`]. +pub(crate) fn current_document(cursor: &SessionCursor) -> Result { + if !within_depth(cursor.current()) { + return Err(DriverError::Decode { + column: String::new(), + detail: format!("the document nests deeper than {MAX_DEPTH} levels"), + }); + } + cursor.deserialize_current().map_err(|error| DriverError::Decode { + column: String::new(), + detail: error.to_string(), + }) +} + +/// Walks the levels with a stack of its own, so the walk itself stays +/// off the thread's stack. A malformed element is left for bson to +/// report. +fn within_depth(document: &RawDocument) -> bool { + let mut open = vec![document.iter_elements()]; + while let Some(elements) = open.last_mut() { + let Some(element) = elements.next() else { + open.pop(); + continue; + }; + let nested = match element.and_then(|element| element.value()) { + Ok(RawBsonRef::Document(inner)) => inner.iter_elements(), + Ok(RawBsonRef::Array(inner)) => inner.iter_elements(), + Ok(RawBsonRef::JavaScriptCodeWithScope(code)) => code.scope.iter_elements(), + _ => continue, + }; + if open.len() >= MAX_DEPTH { + return false; + } + open.push(nested); + } + true +} + +#[cfg(test)] +mod tests { + use bson::{Bson, RawDocumentBuf, doc}; + + use super::*; + + /// A document `levels` containers deep, the outer document included, + /// arrays and documents in turn. + fn nested(levels: usize) -> RawDocumentBuf { + let mut value = Bson::Int32(1); + for level in 1..levels { + value = match level % 2 { + 0 => Bson::Document(doc! { "inner": value }), + _ => Bson::Array(vec![value]), + }; + } + RawDocumentBuf::try_from(&doc! { "root": value }).unwrap() + } + + #[test] + fn a_document_at_the_limit_is_read() { + assert!(within_depth(&nested(MAX_DEPTH))); + } + + #[test] + fn a_document_past_the_limit_is_refused() { + assert!(!within_depth(&nested(MAX_DEPTH + 1))); + } +} diff --git a/linux/crates/drivers/mongodb/src/rows.rs b/linux/crates/drivers/mongodb/src/rows.rs index 8b12315761..0024d16a53 100644 --- a/linux/crates/drivers/mongodb/src/rows.rs +++ b/linux/crates/drivers/mongodb/src/rows.rs @@ -124,7 +124,7 @@ pub(crate) async fn collect( loop { match cursor.advance(session).await { Ok(true) if (documents.len() as u64) < limit.get() => { - documents.push(cursor.deserialize_current().map_err(|error| decode(&error))?); + documents.push(crate::nested_document::current_document(&cursor)?); } Ok(true) => { truncated = true; @@ -158,13 +158,6 @@ pub(crate) async fn collect( Ok(QueryResult::new(layout.columns(), rows).truncated(truncated)) } -fn decode(error: &mongodb::error::Error) -> DriverError { - DriverError::Decode { - column: String::new(), - detail: error.to_string(), - } -} - /// Stream a source into `sink`, keeping at most `limit` rows when there /// is one. pub(crate) async fn emit( diff --git a/linux/crates/drivers/mssql/src/dialect.rs b/linux/crates/drivers/mssql/src/dialect.rs index 7b470b9519..93c83b3324 100644 --- a/linux/crates/drivers/mssql/src/dialect.rs +++ b/linux/crates/drivers/mssql/src/dialect.rs @@ -465,7 +465,49 @@ const CREDENTIAL_RULES: CredentialRules = CredentialRules { "DECRYPTBYKEYAUTOCERT", "SIGNBYASYMKEY", "SIGNBYCERT", + "CERTPRIVATEKEY", + "PWDCOMPARE", + "PWDENCRYPT", + "sp_xp_cmdshell_proxy_account", + "sysmail_add_account_sp", + "sysmail_update_account_sp", + // Replication and log shipping: the distributor_admin, Publisher, + // Subscriber, agent job and monitor passwords, as arguments or as + // a property value. + "sp_adddistributor", + "sp_changedistributor_password", + "sp_adddistributiondb", + "sp_adddistpublisher", + "sp_changedistpublisher", + "sp_addsubscriber", + "sp_changesubscriber", + "sp_addlogreader_agent", + "sp_changelogreader_agent", + "sp_addqreader_agent", + "sp_changeqreader_agent", + "sp_addpublication_snapshot", + "sp_changepublication_snapshot", + "sp_addpushsubscription_agent", + "sp_addpullsubscription_agent", + "sp_addmergepushsubscription_agent", + "sp_addmergepullsubscription_agent", + "sp_changesubscription", + "sp_changemergesubscription", + "sp_changemergepullsubscription", + "sp_change_subscription_properties", + "sp_changereplicationserverpasswords", + "sp_adddynamicsnapshot_job", + "sp_changedynamicsnapshot_job", + "sp_attachsubscription", + "sp_link_publication", + "sp_add_log_shipping_primary_database", + "sp_change_log_shipping_primary_database", + "sp_add_log_shipping_secondary_primary", + "sp_change_log_shipping_secondary_primary", + "sp_add_log_shipping_secondary_database", + "sp_change_log_shipping_secondary_database", ], + secret_parameters: &["PASSWORD", "PWD"], }; #[cfg(test)] @@ -779,6 +821,21 @@ mod tests { "SELECT DECRYPTBYPASSPHRASE('passphrase', secret) FROM t", "EXEC sp_addlinkedserver @server = 's', @srvproduct = '', @provider = 'MSOLEDBSQL', @provstr = 'Server=h;Uid=u;Pwd=p;'", "SELECT u.id FROM users u JOIN logins l ON DECRYPTBYPASSPHRASE('secret', l.hash) = u.token", + "EXEC sp_xp_cmdshell_proxy_account 'CORP\\svc-sql', 'p'", + "EXEC msdb.dbo.sysmail_add_account_sp @account_name = 'ops', @username = 'ops', @password = 'p'", + "EXEC msdb.dbo.sysmail_update_account_sp @account_name = 'ops', @password = 'p'", + "EXEC sp_adddistributor @distributor = 'd', @password = 'p'", + "EXEC sp_changedistributor_password @password = 'p'", + "EXEC sp_changedistpublisher @publisher = 'pub', @property = 'password', @value = 'p'", + "EXEC sp_addpullsubscription_agent @publisher = 'pub', @publisher_db = 'd', @publication = 'p', @distributor_password = 'p'", + "EXEC sp_add_log_shipping_primary_database @database = 'd', @monitor_server_password = 'p'", + "EXEC dbo.any_procedure @login = 'l', @pwd = 'p'", + "SELECT name FROM sys.sql_logins WHERE PWDCOMPARE('p', password_hash) = 1", + "SELECT PWDENCRYPT('p')", + "SELECT CERTPRIVATEKEY(CERT_ID('c'), 'encryption password', 'decryption password')", + "EXEC('CREATE LOGIN l WITH PASSWORD = ''p''')", + "EXEC sp_executesql N'CREATE USER u WITH PASSWORD = ''p'''", + "DECLARE @s nvarchar(99) = N'CREATE LOGIN l WITH PASSWORD = ''p'''; EXEC (@s)", ] { assert_eq!( dialect().history_recording(sql), diff --git a/linux/crates/drivers/mysql/src/dialect.rs b/linux/crates/drivers/mysql/src/dialect.rs index dedd423ca4..4a8e94b0ef 100644 --- a/linux/crates/drivers/mysql/src/dialect.rs +++ b/linux/crates/drivers/mysql/src/dialect.rs @@ -432,6 +432,7 @@ const CREDENTIAL_RULES: CredentialRules = CredentialRules { "ENCODE", "DECODE", ], + secret_parameters: &[], }; #[cfg(test)] diff --git a/linux/crates/drivers/mysql/src/stream_read.rs b/linux/crates/drivers/mysql/src/stream_read.rs index 1c0b125ea8..d3989ba11a 100644 --- a/linux/crates/drivers/mysql/src/stream_read.rs +++ b/linux/crates/drivers/mysql/src/stream_read.rs @@ -1,8 +1,8 @@ use std::sync::Arc; use futures_util::TryStreamExt; -use sqlx::mysql::MySqlConnection; -use sqlx::{AssertSqlSafe, Either, Executor as _, Row as _}; +use sqlx::mysql::{MySqlConnection, MySqlDatabaseError}; +use sqlx::{AssertSqlSafe, Either, Executor as _, Row as _, SqlSafeStr as _}; use tablepro_core::column::ResultColumn; use tablepro_core::sql_syntax::{SqlGrammar, StatementAccess, statement_access}; use tablepro_core::{ @@ -30,6 +30,7 @@ pub(crate) async fn stream( ) -> Result { if let ReadStatement::User(sql) = read { refuse_writes(sql.as_str())?; + refuse_more_than_one(connection, flavour, sql.as_str()).await?; } connection .execute("START TRANSACTION READ ONLY") @@ -85,6 +86,35 @@ pub(crate) fn refuse_writes(sql: &str) -> Result<(), DriverError> { } } +/// MySQL's ER_UNSUPPORTED_PS: the statement parsed, as one, but the +/// prepared-statement protocol does not run its kind. +const UNSUPPORTED_IN_PREPARE: u16 = 1295; + +/// The read runs over the text protocol, which runs every statement a +/// text holds. A prepare parses the text the way the server will, with +/// multiple statements off, so a text the server reads as more than one +/// fails here whatever the client's lexer made of it. +async fn refuse_more_than_one( + connection: &mut MySqlConnection, + flavour: MySqlFlavour, + sql: &str, +) -> Result<(), DriverError> { + let prepared = (&mut *connection) + .prepare(AssertSqlSafe(sql.to_owned()).into_sql_str()) + .await; + match prepared { + Ok(_) => Ok(()), + Err(sqlx::Error::Database(error)) + if error + .try_downcast_ref::() + .is_some_and(|mysql| mysql.number() == UNSUPPORTED_IN_PREPARE) => + { + Ok(()) + } + Err(error) => Err(map_error(&error, flavour)), + } +} + struct Emitter<'a> { sink: &'a mut dyn ResultSink, batch: Vec>, diff --git a/linux/crates/drivers/mysql/tests/integration/main.rs b/linux/crates/drivers/mysql/tests/integration/main.rs index 02b6234b7b..b916b3de6f 100644 --- a/linux/crates/drivers/mysql/tests/integration/main.rs +++ b/linux/crates/drivers/mysql/tests/integration/main.rs @@ -20,5 +20,6 @@ mod row_limit; mod server_time_zone; mod support; mod tls; +mod user_read; mod value_corpus; mod write_batch; diff --git a/linux/crates/drivers/mysql/tests/integration/user_read.rs b/linux/crates/drivers/mysql/tests/integration/user_read.rs new file mode 100644 index 0000000000..678a63e7d0 --- /dev/null +++ b/linux/crates/drivers/mysql/tests/integration/user_read.rs @@ -0,0 +1,46 @@ +//! A read of user SQL runs one statement, as the server parses it. + +use tablepro_core::{Connection, DriverError, ReadStatement, StreamSummary, UserSql}; + +use crate::support::{CALL, EventSink, Events, FLAVOURS, connect, setup, start, within}; + +async fn read(connection: &dyn Connection, sql: &str) -> (Result, Events) { + let events = Events::default(); + let outcome = connection + .stream_read( + &ReadStatement::User(UserSql::new(sql)), + Box::new(EventSink::new(&events)), + within(CALL), + ) + .await; + (outcome, events) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn a_read_runs_one_statement_and_takes_any_kind_the_server_parses() { + for flavour in FLAVOURS { + let fixture = start(flavour).await; + let connection = connect(&fixture, false).await; + setup( + connection.as_ref(), + &["CREATE TABLE t (id int)", "INSERT INTO t VALUES (1)"], + ) + .await; + + let (two, events) = read(connection.as_ref(), "SELECT 1; SELECT 2").await; + assert!(two.is_err(), "{flavour:?}: {two:?}"); + assert!(events.rows().is_empty(), "{flavour:?}"); + + for sql in [ + "SELECT id FROM t", + "SHOW TABLES", + "DESCRIBE t", + "EXPLAIN SELECT * FROM t", + ] { + let (outcome, events) = read(connection.as_ref(), sql).await; + assert!(outcome.is_ok(), "{flavour:?}: {sql}: {outcome:?}"); + assert!(!events.rows().is_empty(), "{flavour:?}: {sql}"); + } + } +} diff --git a/linux/crates/drivers/postgres/src/catalog/pg_column_row.rs b/linux/crates/drivers/postgres/src/catalog/pg_column_row.rs index aa618dc005..7918695b7d 100644 --- a/linux/crates/drivers/postgres/src/catalog/pg_column_row.rs +++ b/linux/crates/drivers/postgres/src/catalog/pg_column_row.rs @@ -2,6 +2,7 @@ use tablepro_core::DriverError; use tokio_postgres::Row; use super::type_facts::{TypeFacts, TypeShape}; +use crate::codec::TextArray; /// One column of a table, as the catalogue query returns it. Every /// projected type is built in, so the read never needs a type lookup. @@ -19,7 +20,7 @@ macro_rules! columns_sql { WITH RECURSIVE resolved AS ( SELECT a.attnum, a.atttypid AS type_oid, a.atttypmod AS typmod, 0 AS depth FROM pg_catalog.pg_attribute a - WHERE a.attrelid = $1::text::regclass AND a.attnum > 0 AND NOT a.attisdropped + WHERE a.attrelid = $1::text::regclass::oid AND a.attnum > 0 AND NOT a.attisdropped UNION ALL SELECT r.attnum, t.typbasetype, CASE WHEN r.typmod = -1 THEN t.typtypmod ELSE r.typmod END, @@ -43,7 +44,7 @@ SELECT a.attname::text, et.oid::int8, et.typtype::text, et.typname::text, - (SELECT array_agg(e.enumlabel::text ORDER BY e.enumsortorder) + (SELECT pg_catalog.array_agg(e.enumlabel::text ORDER BY e.enumsortorder) FROM pg_catalog.pg_enum e WHERE e.enumtypid = COALESCE(et.oid, bt.oid)), a.attnotnull, @@ -52,8 +53,8 @@ SELECT a.attname::text, a.attidentity::text, a.attgenerated::text, pg_catalog.col_description(a.attrelid, a.attnum::int4)::text, - CASE WHEN a.attcollation <> 0 AND a.attcollation <> at.typcollation THEN cn.nspname::text END, - CASE WHEN a.attcollation <> 0 AND a.attcollation <> at.typcollation THEN co.collname::text END, + CASE WHEN a.attcollation <> 0::oid AND a.attcollation <> at.typcollation THEN cn.nspname::text END, + CASE WHEN a.attcollation <> 0::oid AND a.attcollation <> at.typcollation THEN co.collname::text END, CASE WHEN a.attstorage <> at.typstorage THEN a.attstorage::text END, ", $compression, @@ -67,7 +68,7 @@ LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attn LEFT JOIN pg_catalog.pg_constraint pk ON pk.conrelid = a.attrelid AND pk.contype = 'p' LEFT JOIN pg_catalog.pg_collation co ON co.oid = a.attcollation LEFT JOIN pg_catalog.pg_namespace cn ON cn.oid = co.collnamespace -WHERE a.attrelid = $1::text::regclass AND a.attnum > 0 AND NOT a.attisdropped +WHERE a.attrelid = $1::text::regclass::oid AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum" ) }; @@ -108,7 +109,10 @@ impl PgColumnRow { let element_oid: Option = row.try_get(7).map_err(field)?; let element_shape: Option = row.try_get(8).map_err(field)?; let element_name: Option = row.try_get(9).map_err(field)?; - let enum_labels: Option> = row.try_get(10).map_err(field)?; + let enum_labels = row + .try_get::<_, Option>(10) + .map_err(field)? + .map(|labels| labels.0); let enum_labels = enum_labels.unwrap_or_default(); let element = match element_oid { Some(element_oid) => Some(Box::new(TypeFacts { diff --git a/linux/crates/drivers/postgres/src/catalog/table_definition.rs b/linux/crates/drivers/postgres/src/catalog/table_definition.rs index 82018fb354..056bb6d06e 100644 --- a/linux/crates/drivers/postgres/src/catalog/table_definition.rs +++ b/linux/crates/drivers/postgres/src/catalog/table_definition.rs @@ -3,15 +3,17 @@ use tablepro_core::ddl::InboundForeignKey; use tablepro_core::meta::TableRef; use tokio_postgres::Row; +use crate::codec::TextArray; + /// The primary key constraint's name and its columns in key order. pub(crate) const PRIMARY_KEY_NAME_SQL: &str = "\ SELECT c.conname::text, ARRAY(SELECT a.attname::text - FROM unnest(c.conkey) WITH ORDINALITY AS k(attnum, ord) + FROM pg_catalog.unnest(c.conkey) WITH ORDINALITY AS k(attnum, ord) JOIN pg_catalog.pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = k.attnum ORDER BY k.ord) FROM pg_catalog.pg_constraint c -WHERE c.conrelid = $1::text::regclass AND c.contype = 'p'"; +WHERE c.conrelid = $1::text::regclass::oid AND c.contype = 'p'"; /// Foreign keys on other tables that point at this one. A partition of /// a partitioned table that points here holds a clone of its parent's @@ -22,18 +24,18 @@ SELECT c.conname::text, n.nspname::text, t.relname::text, ARRAY(SELECT a.attname::text - FROM unnest(c.conkey) WITH ORDINALITY AS k(attnum, ord) + FROM pg_catalog.unnest(c.conkey) WITH ORDINALITY AS k(attnum, ord) JOIN pg_catalog.pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = k.attnum ORDER BY k.ord), ARRAY(SELECT a.attname::text - FROM unnest(c.confkey) WITH ORDINALITY AS k(attnum, ord) + FROM pg_catalog.unnest(c.confkey) WITH ORDINALITY AS k(attnum, ord) JOIN pg_catalog.pg_attribute a ON a.attrelid = c.confrelid AND a.attnum = k.attnum ORDER BY k.ord), {child_compares_alike} FROM pg_catalog.pg_constraint c JOIN pg_catalog.pg_class t ON t.oid = c.conrelid JOIN pg_catalog.pg_namespace n ON n.oid = t.relnamespace -WHERE c.confrelid = $1::text::regclass AND c.contype = 'f' +WHERE c.confrelid = $1::text::regclass::oid AND c.contype = 'f' AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_constraint p WHERE p.oid = c.conparentid AND p.confrelid = c.confrelid) ORDER BY n.nspname, t.relname, c.conname"; @@ -52,22 +54,22 @@ pub(crate) fn inbound_foreign_keys_sql() -> String { /// no such column. `{narrowing}` is the integer condition, in `ct` /// (child) and `pt` (parent) widths. const COMPARES_ALIKE_SQL: &str = "\ -NOT coalesce((to_jsonb(c) ->> 'conperiod')::boolean, false) +NOT coalesce((pg_catalog.to_jsonb(c) OPERATOR(pg_catalog.->>) 'conperiod')::boolean, false) AND NOT EXISTS ( SELECT 1 - FROM unnest(c.conkey, c.confkey) AS k(child, parent) + FROM ROWS FROM (pg_catalog.unnest(c.conkey), pg_catalog.unnest(c.confkey)) AS k(child, parent) JOIN pg_catalog.pg_attribute ca ON ca.attrelid = c.conrelid AND ca.attnum = k.child JOIN pg_catalog.pg_attribute pa ON pa.attrelid = c.confrelid AND pa.attnum = k.parent JOIN pg_catalog.pg_type ct ON ct.oid = ca.atttypid JOIN pg_catalog.pg_type pt ON pt.oid = pa.atttypid WHERE NOT (ca.attcollation = pa.attcollation - OR (SELECT bool_and(l.collisdeterministic) FROM pg_catalog.pg_collation l + OR (SELECT pg_catalog.bool_and(l.collisdeterministic) FROM pg_catalog.pg_collation l WHERE l.oid IN (ca.attcollation, pa.attcollation))) OR NOT (ca.atttypid = pa.atttypid - OR (ca.atttypid IN ('pg_catalog.text'::regtype, 'pg_catalog.varchar'::regtype) - AND pa.atttypid IN ('pg_catalog.text'::regtype, 'pg_catalog.varchar'::regtype)) - OR (ca.atttypid IN ('pg_catalog.int2'::regtype, 'pg_catalog.int4'::regtype, 'pg_catalog.int8'::regtype) - AND pa.atttypid IN ('pg_catalog.int2'::regtype, 'pg_catalog.int4'::regtype, 'pg_catalog.int8'::regtype) + OR (ca.atttypid IN ('pg_catalog.text'::regtype::oid, 'pg_catalog.varchar'::regtype::oid) + AND pa.atttypid IN ('pg_catalog.text'::regtype::oid, 'pg_catalog.varchar'::regtype::oid)) + OR (ca.atttypid IN ('pg_catalog.int2'::regtype::oid, 'pg_catalog.int4'::regtype::oid, 'pg_catalog.int8'::regtype::oid) + AND pa.atttypid IN ('pg_catalog.int2'::regtype::oid, 'pg_catalog.int4'::regtype::oid, 'pg_catalog.int8'::regtype::oid) AND {narrowing})))"; /// For a filter on the parent: it holds the child's values. @@ -89,8 +91,8 @@ pub(crate) fn inbound_foreign_key(row: &Row) -> Result(3).map_err(field)?.0, + referenced_columns: row.try_get::<_, TextArray>(4).map_err(field)?.0, child_compares_alike: row.try_get(5).map_err(field)?, }) } diff --git a/linux/crates/drivers/postgres/src/codec/array_decode.rs b/linux/crates/drivers/postgres/src/codec/array_decode.rs index 0d64f9cdfb..e61015d17a 100644 --- a/linux/crates/drivers/postgres/src/codec/array_decode.rs +++ b/linux/crates/drivers/postgres/src/codec/array_decode.rs @@ -26,24 +26,30 @@ pub(crate) fn decode_array(ty: &Type, element: &Type, raw: &[u8]) -> Result MAX_DIMENSIONS { return Err(WireError::Malformed); } + // The server sends an empty array with no dimensions, so a dimension + // of length 0 beside others could only make `nest` build the others' + // empty arrays from a few bytes. let lengths: Vec = dimensions .iter() - .map(|dimension| usize::try_from(dimension.len).map_err(|_| WireError::Malformed)) + .map(|dimension| match usize::try_from(dimension.len) { + Ok(0) | Err(_) => Err(WireError::Malformed), + Ok(length) => Ok(length), + }) .collect::>()?; let total = lengths .iter() .try_fold(1usize, |total, length| total.checked_mul(*length)) .ok_or(WireError::Malformed)?; - let values: Vec = array - .values() - .map(|value| { - Ok(match value { - Some(bytes) => decode_binary(element, bytes), - None => Value::Null, - }) - }) - .collect() - .map_err(|_| WireError::Malformed)?; + // Not `collect`, which would reserve room for the header's count + // before reading an element: the cell's bytes bound the allocation. + let mut elements = array.values(); + let mut values = Vec::new(); + while let Some(value) = elements.next().map_err(|_| WireError::Malformed)? { + values.push(match value { + Some(bytes) => decode_binary(element, bytes), + None => Value::Null, + }); + } if values.len() != total { return Err(WireError::Malformed); } @@ -118,6 +124,41 @@ mod tests { out } + #[test] + fn a_declared_count_the_bytes_cannot_hold_is_malformed() { + let raw = int4_array( + vec![ArrayDimension { + len: i32::MAX, + lower_bound: 1, + }], + &[], + ); + + assert_eq!( + decode_array(&Type::INT4_ARRAY, &Type::INT4, &raw), + Err(WireError::Malformed) + ); + } + + #[test] + fn a_dimension_of_length_zero_is_malformed() { + let raw = int4_array( + vec![ + ArrayDimension { + len: i32::MAX, + lower_bound: 1, + }, + ArrayDimension { len: 0, lower_bound: 1 }, + ], + &[], + ); + + assert_eq!( + decode_array(&Type::INT4_ARRAY, &Type::INT4, &raw), + Err(WireError::Malformed) + ); + } + #[test] fn a_two_dimensional_array_nests_by_dimension() { let dimensions = vec![ diff --git a/linux/crates/drivers/postgres/src/codec/decode.rs b/linux/crates/drivers/postgres/src/codec/decode.rs index 2e66aedacd..b265d4ede4 100644 --- a/linux/crates/drivers/postgres/src/codec/decode.rs +++ b/linux/crates/drivers/postgres/src/codec/decode.rs @@ -249,11 +249,12 @@ fn path_text(raw: &[u8]) -> Result { use fallible_iterator::FallibleIterator; let path = wire::path_from_sql(raw).map_err(malformed)?; - let points: Vec = path - .points() - .map(|point| Ok(point_text(point.x(), point.y()))) - .collect() - .map_err(malformed)?; + // Not `collect`, which would reserve room for the header's count. + let mut wire_points = path.points(); + let mut points = Vec::new(); + while let Some(point) = wire_points.next().map_err(malformed)? { + points.push(point_text(point.x(), point.y())); + } let (open, close) = if path.closed() { ('(', ')') } else { ('[', ']') }; Ok(format!("{open}{}{close}", points.join(","))) } @@ -350,6 +351,20 @@ mod tests { out } + #[test] + fn a_path_declaring_points_it_does_not_carry_is_undecodable() { + for declared in [i32::MAX, -1] { + let mut lying = BytesMut::new(); + lying.extend_from_slice(&[1]); + lying.extend_from_slice(&declared.to_be_bytes()); + + assert!( + matches!(decode_binary(&Type::PATH, &lying), Value::Undecodable(_)), + "{declared}" + ); + } + } + #[test] fn date_timestamp_infinity_sentinels() { let infinity = encoded(i32::MAX, &Type::INT4); diff --git a/linux/crates/drivers/postgres/src/codec/mod.rs b/linux/crates/drivers/postgres/src/codec/mod.rs index 091b0fe874..d4ddc45988 100644 --- a/linux/crates/drivers/postgres/src/codec/mod.rs +++ b/linux/crates/drivers/postgres/src/codec/mod.rs @@ -10,9 +10,11 @@ pub(crate) mod oid; mod pg_param; mod raw_cell; mod temporal_wire; +mod text_array; mod wire_error; pub(crate) use decode::decodes_natively; pub(crate) use pg_param::typed; pub(crate) use raw_cell::RawCell; +pub(crate) use text_array::TextArray; pub(crate) use wire_error::WireError; diff --git a/linux/crates/drivers/postgres/src/codec/text_array.rs b/linux/crates/drivers/postgres/src/codec/text_array.rs new file mode 100644 index 0000000000..4a511a8285 --- /dev/null +++ b/linux/crates/drivers/postgres/src/codec/text_array.rs @@ -0,0 +1,76 @@ +use std::error::Error; + +use fallible_iterator::FallibleIterator; +use postgres_protocol::types::array_from_sql; +use postgres_types::{FromSql, Kind, Type}; + +/// A one-dimensional array of text from the catalog, read element by +/// element. `Vec`'s own `FromSql` collects, which reserves room +/// for the count the header claims before reading anything, so a +/// hostile server could ask for any amount of memory in a few bytes. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub(crate) struct TextArray(pub(crate) Vec); + +impl<'a> FromSql<'a> for TextArray { + fn from_sql(ty: &Type, raw: &'a [u8]) -> Result> { + let Kind::Array(member) = ty.kind() else { + return Err(format!("{ty} is not an array").into()); + }; + let array = array_from_sql(raw)?; + if array.dimensions().count()? > 1 { + return Err("the array has more than one dimension".into()); + } + let mut elements = array.values(); + let mut values = Vec::new(); + while let Some(element) = elements.next()? { + values.push(String::from_sql_nullable(member, element)?); + } + Ok(Self(values)) + } + + fn accepts(ty: &Type) -> bool { + matches!(ty.kind(), Kind::Array(member) if ::accepts(member)) + } +} + +#[cfg(test)] +mod tests { + use bytes::BytesMut; + use postgres_protocol::IsNull; + use postgres_protocol::types::{ArrayDimension, array_to_sql}; + + use super::*; + + fn text_array(len: i32, elements: &[&str]) -> Vec { + let mut raw = BytesMut::new(); + array_to_sql( + [ArrayDimension { len, lower_bound: 1 }], + Type::TEXT.oid(), + elements.iter(), + |element, buf| { + buf.extend_from_slice(element.as_bytes()); + Ok(IsNull::No) + }, + &mut raw, + ) + .unwrap(); + raw.to_vec() + } + + #[test] + fn an_array_reads_its_elements() { + let raw = text_array(2, &["id", "email"]); + + assert_eq!( + TextArray::from_sql(&Type::TEXT_ARRAY, &raw).unwrap(), + TextArray(vec!["id".to_owned(), "email".to_owned()]) + ); + } + + #[test] + fn a_count_the_bytes_do_not_hold_is_an_error() { + let raw = text_array(i32::MAX, &["id"]); + + assert!(TextArray::from_sql(&Type::TEXT_ARRAY, &raw).is_err()); + } +} diff --git a/linux/crates/drivers/postgres/src/commit_resolution.rs b/linux/crates/drivers/postgres/src/commit_resolution.rs index b35a7fbe44..c07049ff8a 100644 --- a/linux/crates/drivers/postgres/src/commit_resolution.rs +++ b/linux/crates/drivers/postgres/src/commit_resolution.rs @@ -9,9 +9,9 @@ pub(crate) fn resolve_op(token: &CommitToken) -> Result return Ok(read_op(|_| Box::pin(async { Ok(CommitResolution::Unresolvable) }))); }; let sql = if *legacy_txid { - "SELECT txid_status($1::text::int8)::text" + "SELECT pg_catalog.txid_status($1::text::int8)::text" } else { - "SELECT pg_xact_status($1::text::xid8)::text" + "SELECT pg_catalog.pg_xact_status($1::text::xid8)::text" }; let xid = xid.to_string(); Ok(read_op(move |client| { diff --git a/linux/crates/drivers/postgres/src/ddl.rs b/linux/crates/drivers/postgres/src/ddl.rs index 50695e763d..2f1e78645d 100644 --- a/linux/crates/drivers/postgres/src/ddl.rs +++ b/linux/crates/drivers/postgres/src/ddl.rs @@ -341,7 +341,10 @@ impl PgDialect { /// default dropped earlier, which `pg_get_serial_sequence` may return /// instead, so the identity's sequence is found by its internal /// dependency on the column. The table goes in quoted, since - /// `regclass` parses it as SQL, and the column name as it is. + /// `regclass` parses it as SQL, and the column name as it is. It runs + /// in the user's session, so every function and every operator between + /// two types is named in `pg_catalog`, which a lookalike on the + /// search_path cannot outbid. fn identity_restart(&self, table: &TableRef, column: &str) -> Result { let text = |text: String| { SqlDialect::literal(self, &Value::Text(text), &ColumnType::untyped_text("text")).map_err(|error| { @@ -353,12 +356,16 @@ impl PgDialect { }; let qualified = self.qualify(table); Ok(format!( - "SELECT setval((SELECT d.objid::regclass FROM pg_catalog.pg_depend d \ - JOIN pg_catalog.pg_attribute a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid \ - JOIN pg_catalog.pg_class s ON s.oid = d.objid \ - WHERE d.classid = 'pg_catalog.pg_class'::regclass AND d.refclassid = 'pg_catalog.pg_class'::regclass \ - AND d.deptype = 'i' AND s.relkind = 'S' AND d.refobjid = {}::regclass AND a.attname = {}), \ - GREATEST(COALESCE(MAX({})::bigint, 0), 0) + 1, false) FROM {qualified}", + "SELECT pg_catalog.setval((SELECT d.objid::pg_catalog.regclass FROM pg_catalog.pg_depend d \ + JOIN pg_catalog.pg_attribute a ON a.attrelid OPERATOR(pg_catalog.=) d.refobjid \ + AND a.attnum OPERATOR(pg_catalog.=) d.refobjsubid \ + JOIN pg_catalog.pg_class s ON s.oid OPERATOR(pg_catalog.=) d.objid \ + WHERE d.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass \ + AND d.refclassid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass \ + AND d.deptype OPERATOR(pg_catalog.=) 'i' AND s.relkind OPERATOR(pg_catalog.=) 'S' \ + AND d.refobjid OPERATOR(pg_catalog.=) {}::pg_catalog.regclass AND a.attname OPERATOR(pg_catalog.=) {}), \ + GREATEST(COALESCE(pg_catalog.max({})::pg_catalog.int8, 0), 0) OPERATOR(pg_catalog.+) 1, false) \ + FROM {qualified}", text(qualified.clone())?, text(column.to_owned())?, self.quote_identifier(column) @@ -1105,12 +1112,15 @@ mod tests { "ALTER TABLE \"App\".\"Users\" DROP CONSTRAINT \"users_pkey\", ALTER COLUMN \"Status\" SET NOT \ NULL, ALTER COLUMN \"Status\" ADD GENERATED BY DEFAULT AS IDENTITY, ADD PRIMARY KEY (\"id\", \ \"Status\")", - "SELECT setval((SELECT d.objid::regclass FROM pg_catalog.pg_depend d JOIN \ - pg_catalog.pg_attribute a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid JOIN \ - pg_catalog.pg_class s ON s.oid = d.objid WHERE d.classid = 'pg_catalog.pg_class'::regclass AND \ - d.refclassid = 'pg_catalog.pg_class'::regclass AND d.deptype = 'i' AND s.relkind = 'S' AND \ - d.refobjid = '\"App\".\"Users\"'::regclass AND a.attname = 'Status'), \ - GREATEST(COALESCE(MAX(\"Status\")::bigint, 0), 0) + 1, false) FROM \"App\".\"Users\"", + "SELECT pg_catalog.setval((SELECT d.objid::pg_catalog.regclass FROM pg_catalog.pg_depend d \ + JOIN pg_catalog.pg_attribute a ON a.attrelid OPERATOR(pg_catalog.=) d.refobjid AND a.attnum \ + OPERATOR(pg_catalog.=) d.refobjsubid JOIN pg_catalog.pg_class s ON s.oid OPERATOR(pg_catalog.=) \ + d.objid WHERE d.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass AND \ + d.refclassid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass AND d.deptype \ + OPERATOR(pg_catalog.=) 'i' AND s.relkind OPERATOR(pg_catalog.=) 'S' AND d.refobjid \ + OPERATOR(pg_catalog.=) '\"App\".\"Users\"'::pg_catalog.regclass AND a.attname \ + OPERATOR(pg_catalog.=) 'Status'), GREATEST(COALESCE(pg_catalog.max(\"Status\")::pg_catalog.int8, \ + 0), 0) OPERATOR(pg_catalog.+) 1, false) FROM \"App\".\"Users\"", ] ); } diff --git a/linux/crates/drivers/postgres/src/dialect.rs b/linux/crates/drivers/postgres/src/dialect.rs index f9dfaabd0d..981432e6f6 100644 --- a/linux/crates/drivers/postgres/src/dialect.rs +++ b/linux/crates/drivers/postgres/src/dialect.rs @@ -432,6 +432,7 @@ const CREDENTIAL_RULES: CredentialRules = CredentialRules { "decrypt_iv", "hmac", ], + secret_parameters: &[], }; #[cfg(test)] diff --git a/linux/crates/drivers/postgres/src/metadata/estimate.rs b/linux/crates/drivers/postgres/src/metadata/estimate.rs index d3916caefb..abb8ab3894 100644 --- a/linux/crates/drivers/postgres/src/metadata/estimate.rs +++ b/linux/crates/drivers/postgres/src/metadata/estimate.rs @@ -9,16 +9,16 @@ use super::{decode_error, text_rows}; /// analysed has `reltuples` -1 from PostgreSQL 14 on (0 before), which is /// no estimate at all. The heap size in blocks is what sizes a ctid /// window. -const ESTIMATE_SQL: &str = "\ +pub(super) const ESTIMATE_SQL: &str = "\ WITH RECURSIVE tree AS ( SELECT $1::text::regclass::oid AS oid UNION SELECT i.inhrelid FROM pg_catalog.pg_inherits i JOIN tree t ON i.inhparent = t.oid ) -SELECT COALESCE(SUM(GREATEST(c.reltuples, 0)), 0)::float8, - COALESCE(bool_and(c.reltuples < 0), true), +SELECT COALESCE(pg_catalog.sum(GREATEST(c.reltuples, 0)), 0)::float8, + COALESCE(pg_catalog.bool_and(c.reltuples < 0::float4), true), (pg_catalog.pg_relation_size($1::text::regclass) - / current_setting('block_size')::int8)::int8 + / pg_catalog.current_setting('block_size')::int8)::int8 FROM tree JOIN pg_catalog.pg_class c ON c.oid = tree.oid"; diff --git a/linux/crates/drivers/postgres/src/metadata/foreign_keys.rs b/linux/crates/drivers/postgres/src/metadata/foreign_keys.rs index cd2e268404..6365270e75 100644 --- a/linux/crates/drivers/postgres/src/metadata/foreign_keys.rs +++ b/linux/crates/drivers/postgres/src/metadata/foreign_keys.rs @@ -4,6 +4,7 @@ use tokio_postgres::{Client, Row}; use super::{decode_error, text_rows}; use crate::catalog::{PARENT_HOLDS_CHILD, compares_alike_sql}; +use crate::codec::TextArray; /// A key to a partitioned table has a clone on the same table for each /// partition it reaches, made and dropped with it, so only the key the @@ -11,13 +12,13 @@ use crate::catalog::{PARENT_HOLDS_CHILD, compares_alike_sql}; const FOREIGN_KEYS_SQL: &str = "\ SELECT c.conname::text, ARRAY(SELECT a.attname::text - FROM unnest(c.conkey) WITH ORDINALITY AS k(attnum, ord) + FROM pg_catalog.unnest(c.conkey) WITH ORDINALITY AS k(attnum, ord) JOIN pg_catalog.pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = k.attnum ORDER BY k.ord), n.nspname::text, t.relname::text, ARRAY(SELECT a.attname::text - FROM unnest(c.confkey) WITH ORDINALITY AS k(attnum, ord) + FROM pg_catalog.unnest(c.confkey) WITH ORDINALITY AS k(attnum, ord) JOIN pg_catalog.pg_attribute a ON a.attrelid = c.confrelid AND a.attnum = k.attnum ORDER BY k.ord), c.confdeltype::text, @@ -26,12 +27,12 @@ SELECT c.conname::text, FROM pg_catalog.pg_constraint c JOIN pg_catalog.pg_class t ON t.oid = c.confrelid JOIN pg_catalog.pg_namespace n ON n.oid = t.relnamespace -WHERE c.conrelid = $1::text::regclass AND c.contype = 'f' +WHERE c.conrelid = $1::text::regclass::oid AND c.contype = 'f' AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_constraint p WHERE p.oid = c.conparentid AND p.conrelid = c.conrelid) ORDER BY c.conname"; -fn foreign_keys_sql() -> String { +pub(super) fn foreign_keys_sql() -> String { FOREIGN_KEYS_SQL.replace("{parent_compares_alike}", &compares_alike_sql(PARENT_HOLDS_CHILD)) } @@ -61,10 +62,10 @@ fn outbound_foreign_key(row: &Row) -> Result { Ok(OutboundForeignKey { key: ForeignKeyInfo { name: row.try_get(0).map_err(&field)?, - columns: row.try_get(1).map_err(&field)?, + columns: row.try_get::<_, TextArray>(1).map_err(&field)?.0, ref_schema: Some(row.try_get(2).map_err(&field)?), ref_table: row.try_get(3).map_err(&field)?, - ref_columns: row.try_get(4).map_err(&field)?, + ref_columns: row.try_get::<_, TextArray>(4).map_err(&field)?.0, on_delete: referential_action(&on_delete), on_update: referential_action(&on_update), }, diff --git a/linux/crates/drivers/postgres/src/metadata/identity.rs b/linux/crates/drivers/postgres/src/metadata/identity.rs index 8006c2b6ed..9c8234142e 100644 --- a/linux/crates/drivers/postgres/src/metadata/identity.rs +++ b/linux/crates/drivers/postgres/src/metadata/identity.rs @@ -3,15 +3,16 @@ use tablepro_core::meta::{EngineRowId, RowIdentity}; use tokio_postgres::Client; use super::{decode_error, text_rows}; +use crate::codec::TextArray; /// The relation's kind, its primary key, and the first unique index that /// can stand in for one: no predicate, no expressions, and every key /// column NOT NULL, so it names every row exactly once. -const IDENTITY_SQL: &str = "\ +pub(super) const IDENTITY_SQL: &str = "\ SELECT c.relkind::text, ARRAY(SELECT a.attname::text FROM pg_catalog.pg_constraint p - CROSS JOIN LATERAL unnest(p.conkey) WITH ORDINALITY AS k(attnum, ord) + CROSS JOIN LATERAL pg_catalog.unnest(p.conkey) WITH ORDINALITY AS k(attnum, ord) JOIN pg_catalog.pg_attribute a ON a.attrelid = p.conrelid AND a.attnum = k.attnum WHERE p.conrelid = c.oid AND p.contype = 'p' ORDER BY k.ord), @@ -26,18 +27,18 @@ SELECT c.relkind::text, AND i.indexprs IS NULL AND NOT EXISTS ( SELECT 1 - FROM unnest(i.indkey::int2[]) WITH ORDINALITY AS u(attnum, ord) + FROM pg_catalog.unnest(i.indkey::int2[]) WITH ORDINALITY AS u(attnum, ord) JOIN pg_catalog.pg_attribute ua ON ua.attrelid = i.indrelid AND ua.attnum = u.attnum WHERE u.ord <= i.indnkeyatts AND NOT ua.attnotnull) ORDER BY ic.relname LIMIT 1) best - CROSS JOIN LATERAL unnest(best.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord) + CROSS JOIN LATERAL pg_catalog.unnest(best.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord) JOIN pg_catalog.pg_attribute a ON a.attrelid = best.indrelid AND a.attnum = k.attnum WHERE k.ord <= best.indnkeyatts ORDER BY k.ord) FROM pg_catalog.pg_class c -WHERE c.oid = $1::text::regclass"; +WHERE c.oid = $1::text::regclass::oid"; pub(crate) async fn row_identity(client: &Client, relation: &str) -> Result { let field = decode_error("pg_class"); @@ -46,8 +47,8 @@ pub(crate) async fn row_identity(client: &Client, relation: &str) -> Result = row.try_get(1).map_err(&field)?; - let unique_key: Vec = row.try_get(2).map_err(&field)?; + let TextArray(primary_key) = row.try_get(1).map_err(&field)?; + let TextArray(unique_key) = row.try_get(2).map_err(&field)?; Ok(identity(&relkind, primary_key, unique_key)) } diff --git a/linux/crates/drivers/postgres/src/metadata/indexes.rs b/linux/crates/drivers/postgres/src/metadata/indexes.rs index d286a2e576..f22abce8d1 100644 --- a/linux/crates/drivers/postgres/src/metadata/indexes.rs +++ b/linux/crates/drivers/postgres/src/metadata/indexes.rs @@ -7,7 +7,7 @@ use super::{decode_error, text_rows}; /// One row per key column, the INCLUDE columns past `indnkeyatts` left /// out. Attribute number 0 marks an expression, which only /// `pg_get_indexdef` can print. -const INDEXES_SQL: &str = "\ +pub(super) const INDEXES_SQL: &str = "\ SELECT ic.relname::text, i.indisunique, i.indisprimary, @@ -17,9 +17,9 @@ SELECT ic.relname::text, COALESCE(pg_catalog.pg_index_column_has_property(i.indexrelid, k.ord::int4, 'desc'), false) FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class ic ON ic.oid = i.indexrelid -CROSS JOIN LATERAL unnest(i.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord) +CROSS JOIN LATERAL pg_catalog.unnest(i.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord) LEFT JOIN pg_catalog.pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = k.attnum AND k.attnum > 0 -WHERE i.indrelid = $1::text::regclass AND k.ord <= i.indnkeyatts +WHERE i.indrelid = $1::text::regclass::oid AND k.ord <= i.indnkeyatts ORDER BY ic.relname, k.ord"; pub(crate) async fn fetch_indexes(client: &Client, relation: &str) -> Result, DriverError> { diff --git a/linux/crates/drivers/postgres/src/metadata/mod.rs b/linux/crates/drivers/postgres/src/metadata/mod.rs index 930ef17be2..767638bd5c 100644 --- a/linux/crates/drivers/postgres/src/metadata/mod.rs +++ b/linux/crates/drivers/postgres/src/metadata/mod.rs @@ -25,6 +25,7 @@ use tokio_postgres::{Client, Row}; use crate::catalog::{ PRIMARY_KEY_NAME_SQL, PgColumnRow, column_definition, column_info, inbound_foreign_key, inbound_foreign_keys_sql, }; +use crate::codec::TextArray; use crate::errors::map_error; /// Rows of a catalogue query whose parameters are all text. @@ -101,7 +102,7 @@ pub(crate) async fn fetch_table_definition( let field = decode_error("pg_constraint"); Ok(PrimaryKeyConstraint { name: Some(row.try_get(0).map_err(&field)?), - columns: row.try_get(1).map_err(&field)?, + columns: row.try_get::<_, TextArray>(1).map_err(&field)?.0, }) }) .transpose()?; @@ -117,3 +118,79 @@ pub(crate) async fn fetch_table_definition( options: None, }) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::catalog::COLUMNS_SQL_BEFORE_14; + + /// Words that take a parenthesis without calling a function by name. + const SYNTAX: [&str; 14] = [ + "AS", "ARRAY", "COALESCE", "EXISTS", "FROM", "IN", "ANY", "NULLIF", "GREATEST", "ON", "AND", "OR", "NOT", + "OPERATOR", + ]; + + /// Names called without `pg_catalog.` in front, which the session's + /// search_path would resolve. An alias's column list after AS is no + /// call, and neither is text in a string. + fn bare_calls(sql: &str) -> Vec { + let mut code = String::with_capacity(sql.len()); + let mut quoted = false; + for c in sql.chars() { + if c == '\'' { + quoted = !quoted; + } + code.push(if quoted { ' ' } else { c }); + } + let mut words: Vec<(usize, usize)> = Vec::new(); + let mut start = None; + for (index, c) in code.char_indices().chain([(code.len(), ' ')]) { + match (start, c.is_ascii_alphanumeric() || c == '_') { + (None, true) => start = Some(index), + (Some(from), false) => { + words.push((from, index)); + start = None; + } + _ => {} + } + } + let mut calls = Vec::new(); + for (nth, &(from, to)) in words.iter().enumerate() { + let word = &code[from..to]; + let calls_it = code[to..].trim_start().starts_with('('); + let qualified = code[..from].ends_with('.'); + let after_as = nth > 0 && code[words[nth - 1].0..words[nth - 1].1].eq_ignore_ascii_case("AS"); + let syntax = SYNTAX.iter().any(|keyword| keyword.eq_ignore_ascii_case(word)); + if calls_it && !qualified && !after_as && !syntax { + calls.push(word.to_owned()); + } + } + calls + } + + #[test] + fn every_catalog_read_names_its_functions_in_pg_catalog() { + for (name, sql) in [ + ("schemas", objects::SCHEMAS_SQL.to_owned()), + ("objects", objects::OBJECTS_SQL.to_owned()), + ("identity", identity::IDENTITY_SQL.to_owned()), + ("indexes", indexes::INDEXES_SQL.to_owned()), + ("estimate", estimate::ESTIMATE_SQL.to_owned()), + ("foreign keys", foreign_keys::foreign_keys_sql()), + ("inbound foreign keys", inbound_foreign_keys_sql()), + ("primary key", PRIMARY_KEY_NAME_SQL.to_owned()), + ("columns", crate::catalog::COLUMNS_SQL.to_owned()), + ("columns before 14", COLUMNS_SQL_BEFORE_14.to_owned()), + ] { + assert_eq!(bare_calls(&sql), Vec::::new(), "{name}"); + } + } + + #[test] + fn a_bare_call_is_found_and_a_qualified_one_or_an_alias_is_not() { + assert_eq!( + bare_calls("SELECT unnest(a), pg_catalog.unnest(b), COALESCE(x, 'f(y)') FROM t AS k(a, b)"), + ["unnest"] + ); + } +} diff --git a/linux/crates/drivers/postgres/src/metadata/objects.rs b/linux/crates/drivers/postgres/src/metadata/objects.rs index 39e2a8e26a..4282d7982f 100644 --- a/linux/crates/drivers/postgres/src/metadata/objects.rs +++ b/linux/crates/drivers/postgres/src/metadata/objects.rs @@ -6,16 +6,16 @@ use super::{decode_error, text_rows}; /// Schemas this session can use: no TOAST schemas, and no other /// session's temporary schema. -const SCHEMAS_SQL: &str = "\ +pub(super) const SCHEMAS_SQL: &str = "\ SELECT n.nspname::text, - COALESCE(n.nspname = current_schema(), false), - n.nspname = 'information_schema' OR starts_with(n.nspname, 'pg_') + COALESCE(n.nspname = pg_catalog.current_schema(), false), + n.nspname = 'information_schema' OR pg_catalog.starts_with(n.nspname, 'pg_') FROM pg_catalog.pg_namespace n -WHERE NOT starts_with(n.nspname, 'pg_toast') +WHERE NOT pg_catalog.starts_with(n.nspname, 'pg_toast') AND NOT pg_catalog.pg_is_other_temp_schema(n.oid) ORDER BY n.nspname"; -const OBJECTS_SQL: &str = "\ +pub(super) const OBJECTS_SQL: &str = "\ SELECT DISTINCT n.nspname::text, o.name, o.kind FROM ( SELECT c.relnamespace AS namespace, c.relname::text AS name, c.relkind::text AS kind @@ -29,9 +29,9 @@ FROM ( JOIN pg_catalog.pg_namespace n ON n.oid = o.namespace WHERE NOT pg_catalog.pg_is_other_temp_schema(n.oid) AND CASE $1 - WHEN 'default' THEN n.nspname = current_schema() + WHEN 'default' THEN n.nspname = pg_catalog.current_schema() WHEN 'schema' THEN n.nspname = $2 - ELSE n.nspname <> 'information_schema' AND NOT starts_with(n.nspname, 'pg_') + ELSE n.nspname <> 'information_schema' AND NOT pg_catalog.starts_with(n.nspname, 'pg_') END ORDER BY 1, 2, 3"; diff --git a/linux/crates/drivers/postgres/src/stream_read.rs b/linux/crates/drivers/postgres/src/stream_read.rs index 714d882ba8..6adb14a93a 100644 --- a/linux/crates/drivers/postgres/src/stream_read.rs +++ b/linux/crates/drivers/postgres/src/stream_read.rs @@ -53,6 +53,7 @@ pub(crate) async fn stream( emitter.binary(client, statement, rows).await } ReadStatement::User(sql) => { + refuse_more_than_one(client, sql.as_str()).await?; let messages = client .simple_query_raw(sql.as_str()) .await @@ -88,6 +89,20 @@ fn refuse_transaction_control(sql: &str, conforming: bool) -> Result<(), DriverE Ok(()) } +/// The extended protocol's Parse refuses a text that holds more than one +/// statement, and the simple query that follows splits the text with the +/// same parser, so nothing can follow the read inside its transaction +/// whatever the client's lexer made of the text. The simple query keeps +/// every value as the server's text. The statement is prepared under a +/// name, which the read's own connection allows. +async fn refuse_more_than_one(client: &Client, sql: &str) -> Result<(), DriverError> { + client + .prepare_typed(sql, &[]) + .await + .map(drop) + .map_err(|error| map_error(&error)) +} + struct Emitter<'a> { sink: &'a mut dyn ResultSink, batch: Vec>, diff --git a/linux/crates/drivers/postgres/tests/integration/metadata.rs b/linux/crates/drivers/postgres/tests/integration/metadata.rs index 34613a1258..373af7ae0c 100644 --- a/linux/crates/drivers/postgres/tests/integration/metadata.rs +++ b/linux/crates/drivers/postgres/tests/integration/metadata.rs @@ -736,3 +736,82 @@ async fn alter_add_column_then_query_bound_on_all_four_pooled_connections() { assert_eq!(read.rows[0][1], Value::Text("x".to_owned())); } } + +/// Another user's functions and operators in `public` whose signatures +/// match the catalog reads' arguments better than the built-ins do. A +/// catalog read never runs one, and a table named without its schema is +/// still found on the session's own path. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn lookalikes_in_public_never_run_in_a_catalog_read() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + let trap = |name: &str, arguments: &str, returns: &str| { + format!( + "CREATE FUNCTION public.{name}({arguments}) RETURNS {returns} LANGUAGE plpgsql AS \ + $$ BEGIN RAISE EXCEPTION 'public.{name} ran as %', current_user; END $$" + ) + }; + let statements = [ + "CREATE TYPE mood AS ENUM ('sad', 'ok')".to_owned(), + "CREATE TABLE accounts (id int PRIMARY KEY, state mood)".to_owned(), + "CREATE TABLE orders (id int PRIMARY KEY, account int REFERENCES accounts (id))".to_owned(), + "CREATE INDEX orders_account ON orders (account)".to_owned(), + trap("starts_with", "name, text", "boolean"), + trap("unnest", "int2[]", "SETOF int2"), + trap("to_jsonb", "pg_catalog.pg_constraint", "jsonb"), + trap("array_agg_step", "text[], text", "text[]"), + "CREATE AGGREGATE public.array_agg(text) (SFUNC = public.array_agg_step, STYPE = text[])".to_owned(), + trap("oid_is_regclass", "oid, regclass", "boolean"), + "CREATE OPERATOR public.= (LEFTARG = oid, RIGHTARG = regclass, FUNCTION = public.oid_is_regclass)".to_owned(), + trap("oid_is_regtype", "oid, regtype", "boolean"), + "CREATE OPERATOR public.= (LEFTARG = oid, RIGHTARG = regtype, FUNCTION = public.oid_is_regtype)".to_owned(), + trap("jsonb_field", "jsonb, text", "text"), + "CREATE OPERATOR public.->> (LEFTARG = jsonb, RIGHTARG = text, FUNCTION = public.jsonb_field)".to_owned(), + trap("oid_is_not_int", "oid, integer", "boolean"), + "CREATE OPERATOR public.<> (LEFTARG = oid, RIGHTARG = integer, FUNCTION = public.oid_is_not_int)".to_owned(), + trap("real_below_int", "real, integer", "boolean"), + "CREATE OPERATOR public.< (LEFTARG = real, RIGHTARG = integer, FUNCTION = public.real_below_int)".to_owned(), + ]; + let statements: Vec<&str> = statements.iter().map(String::as_str).collect(); + setup(connection.as_ref(), &statements).await; + + let schemas = connection.list_schemas(within(CALL)).await.unwrap(); + assert!( + schemas + .iter() + .any(|schema| schema.name == "public" && schema.is_default) + ); + connection + .list_objects(&ObjectScope::DefaultSchema, within(CALL)) + .await + .unwrap(); + connection + .list_objects(&ObjectScope::AllSchemas, within(CALL)) + .await + .unwrap(); + for name in ["orders", "accounts"] { + connection.fetch_columns(&table(name), within(CALL)).await.unwrap(); + connection.fetch_indexes(&table(name), within(CALL)).await.unwrap(); + connection.fetch_foreign_keys(&table(name), within(CALL)).await.unwrap(); + connection + .fetch_inbound_foreign_keys(&table(name), within(CALL)) + .await + .unwrap(); + connection + .fetch_table_definition(&table(name), within(CALL)) + .await + .unwrap(); + connection.row_identity(&table(name), within(CALL)).await.unwrap(); + connection.table_estimate(&table(name), within(CALL)).await.unwrap(); + } + let unqualified = TableRef::new(None, "orders"); + let columns = connection.fetch_columns(&unqualified, within(CALL)).await.unwrap(); + assert_eq!(columns.len(), 2); + assert!( + connection + .fetch_columns(&TableRef::new(None, "missing"), within(CALL)) + .await + .is_err() + ); +} diff --git a/linux/crates/drivers/postgres/tests/integration/read_only.rs b/linux/crates/drivers/postgres/tests/integration/read_only.rs index 6fb8cc2d11..0d31733a37 100644 --- a/linux/crates/drivers/postgres/tests/integration/read_only.rs +++ b/linux/crates/drivers/postgres/tests/integration/read_only.rs @@ -152,6 +152,20 @@ async fn stream_read_user_sql_cannot_turn_the_transaction_writable() { let owner = connect(&fixture, false).await; setup(owner.as_ref(), &["CREATE TABLE t (id int)", "INSERT INTO t VALUES (1)"]).await; + for sql in [ + "SET TRANSACTION READ WRITE", + "SELECT set_config('transaction_read_only', 'off', true)", + "DELETE FROM t", + ] { + let outcome = stream_user_sql(owner.as_ref(), sql).await; + + assert!( + matches!(outcome, Err(DriverError::ReadOnly(ReadOnlyRefusal::ServerRejected(_)))), + "{sql}: {outcome:?}" + ); + } + // A read is one statement, so the server refuses a second before it + // runs the first. for sql in [ "SET TRANSACTION READ WRITE; DELETE FROM t", "SET transaction_read_only = off; DELETE FROM t", @@ -161,14 +175,61 @@ async fn stream_read_user_sql_cannot_turn_the_transaction_writable() { ] { let outcome = stream_user_sql(owner.as_ref(), sql).await; - assert!( - matches!(outcome, Err(DriverError::ReadOnly(ReadOnlyRefusal::ServerRejected(_)))), - "{sql}: {outcome:?}" - ); + assert!(outcome.is_err(), "{sql}: {outcome:?}"); } assert_eq!(scalar(owner.as_ref(), "SELECT count(*) FROM t").await, Value::Int(1)); } +/// A string on the line after `E'x'` continues it, so the server reads +/// its `\'` as an escaped quote whatever `standard_conforming_strings` +/// says, and the COMMIT is a statement of its own. The read is refused +/// whichever way the text is split. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn stream_read_refuses_a_continued_escape_string_that_hides_a_commit() { + let fixture = start().await; + let owner = connect(&fixture, false).await; + setup(owner.as_ref(), &["CREATE TABLE t (id int)", "INSERT INTO t VALUES (1)"]).await; + + let outcome = stream_user_sql( + owner.as_ref(), + "SELECT E'x'\n'\\' || ' || '\\' ; COMMIT; DELETE FROM t; --'", + ) + .await; + + assert!(outcome.is_err(), "{outcome:?}"); + assert_eq!(scalar(owner.as_ref(), "SELECT count(*) FROM t").await, Value::Int(1)); +} + +/// The rows of user SQL come back typed, and a statement with no rows +/// still has its columns. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn stream_read_of_user_sql_keeps_its_columns() { + let fixture = start().await; + let owner = connect(&fixture, false).await; + setup(owner.as_ref(), &["CREATE TABLE t (id int, note text)"]).await; + + let events = Events::default(); + let outcome = owner + .stream_read( + &ReadStatement::User(UserSql::new("SELECT id, note FROM t WHERE id > 0")), + Box::new(EventSink::new(&events)), + within(CALL), + ) + .await; + + assert!(outcome.is_ok(), "{outcome:?}"); + assert!( + events.all().iter().any(|event| matches!( + event, + tablepro_core::ResultEvent::ResultSetStarted { columns } if columns.len() == 2 + )), + "{:?}", + events.all() + ); +} + /// With `standard_conforming_strings` off the server reads `\'` as an /// escaped quote, so the COMMIT is a statement of its own and the guard /// has to see it that way too. diff --git a/linux/crates/drivers/sqlite/src/connection.rs b/linux/crates/drivers/sqlite/src/connection.rs index 941bf5ce47..020871d974 100644 --- a/linux/crates/drivers/sqlite/src/connection.rs +++ b/linux/crates/drivers/sqlite/src/connection.rs @@ -29,7 +29,7 @@ use crate::errors::{map_error, map_open_error}; use crate::interrupt::{InterruptFlag, SqliteStop}; use crate::metadata; use crate::open::connect_options; -use crate::result_stream::{collect, count, run_bound, run_script}; +use crate::result_stream::{StreamOutcome, collect, count, run_bound, run_script}; use crate::sqlite_pooled::SqlitePooled; use crate::sqlite_standalone::SqliteStandalone; use crate::write_batch; @@ -228,13 +228,28 @@ impl SqliteConnection { }), opened = SqlxConnection::connect_with(&read_only) => { let mut connection = opened.map_err(|error| map_open_error(error, &self.path))?; - crate::attach_guard::install(&mut connection).await?; + crate::read_guard::install(&mut connection).await?; Ok(connection) } } } } +/// User SQL on a read connection, run only once SQLite reads it as one +/// statement. +async fn read_user_sql( + connection: &mut SqlxConnection, + flag: &InterruptFlag, + lock_timeout: std::time::Duration, + sql: &str, + sink: &mut dyn ResultSink, +) -> Result { + crate::read_guard::refuse_more_than_one(connection, sql).await?; + // The count took the busy handler off. + flag.install(connection, lock_timeout).await?; + run_script(connection, sql, None, sink, || false).await +} + /// Read the file once, so a file that is not a database fails the /// connect rather than the first browse, and refuse a library older /// than the catalogue queries need. @@ -441,7 +456,7 @@ impl Connection for SqliteConnection { run_bound(held.connection(), statement, sink.as_mut()).await } ReadStatement::User(sql) => { - run_script(held.connection(), sql.as_str(), None, sink.as_mut(), || false).await + read_user_sql(held.connection(), &flag, lock_timeout, sql.as_str(), sink.as_mut()).await } }, Err(error) => Err(error), diff --git a/linux/crates/drivers/sqlite/src/dialect.rs b/linux/crates/drivers/sqlite/src/dialect.rs index 2602b8c7cf..1751c5730c 100644 --- a/linux/crates/drivers/sqlite/src/dialect.rs +++ b/linux/crates/drivers/sqlite/src/dialect.rs @@ -253,6 +253,7 @@ const CREDENTIAL_RULES: CredentialRules = CredentialRules { (&["ATTACH"], &["KEY"]), ], carriers: &[], + secret_parameters: &[], }; #[cfg(test)] diff --git a/linux/crates/drivers/sqlite/src/lib.rs b/linux/crates/drivers/sqlite/src/lib.rs index 5d4eaf16d2..8450200cd6 100644 --- a/linux/crates/drivers/sqlite/src/lib.rs +++ b/linux/crates/drivers/sqlite/src/lib.rs @@ -21,6 +21,7 @@ mod interrupt; mod lock_wait; mod metadata; mod open; +mod read_guard; mod result_stream; mod sqlite_pooled; mod sqlite_standalone; diff --git a/linux/crates/drivers/sqlite/src/read_guard.rs b/linux/crates/drivers/sqlite/src/read_guard.rs new file mode 100644 index 0000000000..42db95e37d --- /dev/null +++ b/linux/crates/drivers/sqlite/src/read_guard.rs @@ -0,0 +1,123 @@ +//! What a connection that runs user SQL only to read refuses beyond what +//! SQLite's read-only open does. A read-only connection can still attach +//! another file, and `VACUUM INTO` writes a copy of the database through +//! an attach of its own; a pragma such as `foreign_keys` or +//! `hard_heap_limit` changes the connection or the process, and SQLite +//! applies it while compiling, so `EXPLAIN PRAGMA` does too. + +use std::ffi::{CStr, c_char, c_int, c_void}; + +use sqlx::sqlite::SqliteConnection as SqlxConnection; +use sqlx::{AssertSqlSafe, Executor as _, SqlSafeStr as _}; +use tablepro_core::sql_syntax::sqlite_pragma_reports; +use tablepro_core::{BusyReason, DriverError, ReadOnlyRefusal}; + +use crate::errors::map_error; + +/// Allow no attached database and check every statement prepared from +/// now on. +pub(crate) async fn install(connection: &mut SqlxConnection) -> Result<(), DriverError> { + let mut handle = connection.lock_handle().await.map_err(map_error)?; + let raw = handle.as_raw_handle().as_ptr(); + // SAFETY: `raw` comes from the locked handle, so the connection is + // open and nothing runs on it meanwhile. The callback takes no user + // data. + let code = unsafe { + libsqlite3_sys::sqlite3_limit(raw, libsqlite3_sys::SQLITE_LIMIT_ATTACHED, 0); + libsqlite3_sys::sqlite3_set_authorizer(raw, Some(authorize), std::ptr::null_mut()) + }; + match code { + libsqlite3_sys::SQLITE_OK => Ok(()), + code => Err(DriverError::Protocol(format!( + "SQLite refused the read guard with code {code}" + ))), + } +} + +/// Refuse text SQLite reads as more than one statement: sqlx runs every +/// statement a text holds, and what judged it saw one. +/// +/// sqlx compiles the whole text first, on its own worker and with the +/// call's busy handler, so a lock is waited out where a stop can end it, +/// the schema is loaded, and a refused pragma or a syntax error ends the +/// read here. The count then prepares on this thread with no busy +/// handler: with the schema loaded there is no lock to wait for, and a +/// wait here would hold up the task that delivers the stop. The caller +/// installs its busy handler again. +pub(crate) async fn refuse_more_than_one(connection: &mut SqlxConnection, sql: &str) -> Result<(), DriverError> { + (&mut *connection) + .prepare(AssertSqlSafe(sql.to_owned()).into_sql_str()) + .await + .map_err(map_error)?; + let mut handle = connection.lock_handle().await.map_err(map_error)?; + let raw = handle.as_raw_handle().as_ptr(); + // SAFETY: `raw` is the locked, open connection. + unsafe { libsqlite3_sys::sqlite3_busy_timeout(raw, 0) }; + let mut rest = sql.as_bytes(); + let mut statements = 0usize; + while !rest.is_empty() { + let length = c_int::try_from(rest.len()) + .map_err(|_| DriverError::Protocol("the statement is too long for SQLite".to_owned()))?; + let mut statement = std::ptr::null_mut(); + let mut tail: *const c_char = std::ptr::null(); + // SAFETY: `raw` is the locked, open connection; `rest` outlives + // the call and `length` is its length, so SQLite reads inside it + // and points `tail` into it. A prepared statement is finalized + // before the next prepare. + let code = unsafe { + let code = libsqlite3_sys::sqlite3_prepare_v2(raw, rest.as_ptr().cast(), length, &mut statement, &mut tail); + if !statement.is_null() { + libsqlite3_sys::sqlite3_finalize(statement); + } + code + }; + match code { + libsqlite3_sys::SQLITE_OK => {} + libsqlite3_sys::SQLITE_BUSY | libsqlite3_sys::SQLITE_LOCKED => { + return Err(DriverError::Busy(BusyReason::FileLocked)); + } + // sqlx compiled the same text a moment ago, so only a schema + // another connection changed in between gets here; the run + // reports SQLite's own error. + _ => return Ok(()), + } + if !statement.is_null() { + statements += 1; + } + if statements > 1 { + return Err(DriverError::ReadOnly(ReadOnlyRefusal::ClientGuard)); + } + // SAFETY: SQLite sets `tail` to a point inside `rest` or to its end. + let consumed = usize::try_from(unsafe { tail.offset_from(rest.as_ptr().cast()) }).unwrap_or(0); + if consumed == 0 { + break; + } + rest = rest.get(consumed..).unwrap_or_default(); + } + Ok(()) +} + +/// SQLite's authorizer: no ATTACH, and a pragma only when it reports. +unsafe extern "C" fn authorize( + _data: *mut c_void, + action: c_int, + first: *const c_char, + _second: *const c_char, + _database: *const c_char, + _trigger: *const c_char, +) -> c_int { + match action { + libsqlite3_sys::SQLITE_ATTACH => libsqlite3_sys::SQLITE_DENY, + libsqlite3_sys::SQLITE_PRAGMA if first.is_null() => libsqlite3_sys::SQLITE_DENY, + libsqlite3_sys::SQLITE_PRAGMA => { + // SAFETY: SQLite hands the pragma's name as a NUL-terminated + // string that stays valid for the call. + let name = unsafe { CStr::from_ptr(first) }; + match name.to_str().is_ok_and(sqlite_pragma_reports) { + true => libsqlite3_sys::SQLITE_OK, + false => libsqlite3_sys::SQLITE_DENY, + } + } + _ => libsqlite3_sys::SQLITE_OK, + } +} diff --git a/linux/crates/drivers/sqlite/tests/integration/sessions.rs b/linux/crates/drivers/sqlite/tests/integration/sessions.rs index 97467a25eb..4a54e8a165 100644 --- a/linux/crates/drivers/sqlite/tests/integration/sessions.rs +++ b/linux/crates/drivers/sqlite/tests/integration/sessions.rs @@ -442,3 +442,47 @@ async fn stream_read_sends_every_row_and_refuses_writes() { assert_eq!(rows.rows[0][0], Value::Int(10)); connection.close().await.unwrap(); } + +/// A read-only open still lets SQLite attach another file, write a copy +/// of the database with `VACUUM INTO`, and set pragmas, and sqlx runs +/// every statement a text holds. +#[tokio::test] +async fn a_read_attaches_nothing_sets_no_pragma_and_runs_one_statement() { + let file = TestFile::new().await; + let connection = file.open().await; + apply(connection.as_ref(), &["CREATE TABLE t (a)", "INSERT INTO t VALUES (1)"]) + .await + .unwrap(); + let copy = file.directory().join("copy.db"); + let copy_name = copy.display().to_string(); + let attach = format!("ATTACH '{}' AS other", file.path.display()); + let vacuum = format!("VACUUM INTO '{copy_name}'"); + let hidden = format!("SELECT $1('); VACUUM INTO '{copy_name}'; --'"); + + for sql in [ + vacuum.as_str(), + attach.as_str(), + hidden.as_str(), + "PRAGMA foreign_keys = OFF", + "EXPLAIN PRAGMA hard_heap_limit = 1", + "PRAGMA user_version = 7", + "SELECT 1; SELECT 2", + ] { + let collected = Collected::default(); + let refused = connection + .stream_read(&ReadStatement::User(UserSql::new(sql)), collected.sink(), call()) + .await; + assert!(refused.is_err(), "{sql}: {refused:?}"); + } + assert!(!copy.exists()); + + for sql in ["PRAGMA table_info(t)", "SELECT a FROM t", "SELECT a FROM t; -- note"] { + let collected = Collected::default(); + let read = connection + .stream_read(&ReadStatement::User(UserSql::new(sql)), collected.sink(), call()) + .await; + assert!(read.is_ok(), "{sql}: {read:?}"); + assert!(!collected.rows().is_empty(), "{sql}"); + } + connection.close().await.unwrap(); +} diff --git a/linux/crates/mcp/src/query_tools.rs b/linux/crates/mcp/src/query_tools.rs index 784fac6a4f..912f253774 100644 --- a/linux/crates/mcp/src/query_tools.rs +++ b/linux/crates/mcp/src/query_tools.rs @@ -437,6 +437,14 @@ mod tests { assert!(single_batch("SELECT 1; SELECT 2", SqlGrammar::PostgreSql).is_err()); assert!(single_batch("", SqlGrammar::PostgreSql).is_err()); assert!(single_batch("SELECT 'open", SqlGrammar::PostgreSql).is_err()); + assert!(single_batch("SELECT $1('); VACUUM INTO 'copy.db'; --'", SqlGrammar::Sqlite).is_err()); + assert!( + single_batch( + "SELECT E'x'\n'\\' || ' || '\\' ; COMMIT; DELETE FROM orders; --'", + SqlGrammar::PostgreSql + ) + .is_err() + ); } #[test] diff --git a/linux/crates/storage/src/connections.rs b/linux/crates/storage/src/connections.rs index 766caa4b28..f24cc70911 100644 --- a/linux/crates/storage/src/connections.rs +++ b/linux/crates/storage/src/connections.rs @@ -17,6 +17,9 @@ use crate::{ConnectionTag, ConnectionTagName}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SavedConnection { pub id: Uuid, + /// As [`crate::name_rules::shown`] gives it, so a file cannot bring a + /// name that looks like a saved one and compares as another. + #[serde(deserialize_with = "crate::name_rules::deserialize_shown")] pub name: String, pub driver_id: String, pub host: String, @@ -187,7 +190,7 @@ pub enum SavedSshAuth { } /// The saved-connection list as it sits on disk. -#[derive(Debug, Clone, Default, Serialize, Deserialize)] +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] struct ConnectionsDocument { connections: Vec, #[serde(default, skip_serializing_if = "Vec::is_empty")] @@ -198,7 +201,8 @@ impl ConnectionsDocument { /// A tag named twice keeps its first entry and a connection names a /// tag at most once, whatever a hand-edited or crafted file says. A /// tag whose name another one holds becomes that one, so a connection - /// keeps the label it showed. + /// keeps the label it showed. Every write passes here, so a name is + /// stored as the list shows it however it was set. fn tidy(&mut self) { let mut kept: Vec = Vec::with_capacity(self.tags.len()); let mut same: HashMap = HashMap::with_capacity(self.tags.len()); @@ -220,6 +224,7 @@ impl ConnectionsDocument { } self.tags = kept; for connection in &mut self.connections { + connection.name = crate::name_rules::shown(&connection.name); let mut seen = HashSet::with_capacity(connection.tags.len()); connection.tags = std::mem::take(&mut connection.tags) .iter() @@ -333,6 +338,49 @@ impl ImportReport { } } +/// What an import will do, before it is written. +#[derive(Debug, Clone)] +pub struct ImportPreview { + entries: Vec, + before: ConnectionsDocument, + after: ConnectionsDocument, +} + +impl ImportPreview { + /// Each connection of the file as it will be saved, in file order. + pub fn entries(&self) -> &[ImportEntry] { + &self.entries + } + + pub fn report(&self) -> ImportReport { + let added = self + .entries + .iter() + .filter(|entry| entry.change == ImportChange::Added) + .count(); + ImportReport { + added, + updated: self.entries.len() - added, + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ImportEntry { + pub connection: SavedConnection, + pub change: ImportChange, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ImportChange { + /// A connection the list did not have, including one whose id was + /// taken by a different connection and was given its own. + Added, + /// A connection already in the list that the file only renamed, + /// recoloured, regrouped, retagged or gave another icon. + Updated, +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum RemoveOutcome { Removed, @@ -524,7 +572,17 @@ impl ConnectionStore { Ok(crate::document::encode_document(&document)?) } - /// Merge an exported document back into the list. + /// Merge an exported document back into the list, as + /// [`Self::preview_import_blocking`] and [`Self::apply_import_blocking`] + /// do it in two steps. + pub fn import_blocking(&self, path: &Path, bytes: &[u8]) -> Result { + let preview = self.preview_import_blocking(path, bytes)?; + self.apply_import_blocking(&preview) + } + + /// What merging an exported document would do to the list, worked + /// out without writing anything, so the user can see every host and + /// setting the file brings before it is saved. /// /// Connections are matched by id, so re-importing a file the user /// exported from this machine updates the entries it came from @@ -538,53 +596,83 @@ impl ConnectionStore { /// starts with no secrets, so a crafted file cannot point a saved /// connection's password at another host. /// - /// An imported entry never takes a name another entry holds, and an - /// added one never takes the file's last-opened time, so a file + /// An imported entry never takes a name another entry holds or held + /// when the import began, whatever order the file lists them in, and + /// an added one never takes the file's last-opened time, so a file /// cannot pass one host off as another saved connection or sort it /// above one. /// /// Tags are matched by id first and then by name, so importing a /// file twice, or a file from a machine whose `production` tag has /// another id, leaves one tag of each name. - pub fn import_blocking(&self, path: &Path, bytes: &[u8]) -> Result { + pub fn preview_import_blocking(&self, path: &Path, bytes: &[u8]) -> Result { let mut incoming = crate::document::decode_document::(path, bytes) .map_err(StorageError::DocumentUnavailable)?; incoming.tidy(); let mut state = self.lock(); - let mut document = self.ensure_loaded(&mut state)?; - let mut report = ImportReport::default(); + let before = self.ensure_loaded(&mut state)?; + let mut document = before.clone(); + let held: HashMap = document + .connections + .iter() + .map(|saved| (saved.name.clone(), saved.id)) + .collect(); + let mut entries = Vec::with_capacity(incoming.connections.len()); let same = merge_tags(&mut document.tags, incoming.tags); for mut connection in incoming.connections { connection.tags = connection.tags.iter().filter_map(|id| same.get(id).copied()).collect(); let list = &mut document.connections; match list.iter().position(|saved| saved.id == connection.id) { Some(index) if list[index].connects_like(&connection) => { - let name = unclaimed_name(list, &connection.name, Some(connection.id)); + let name = unclaimed_name(list, &held, &connection.name, Some(connection.id)); let existing = &mut list[index]; existing.name = name; existing.color = connection.color; existing.group = connection.group; existing.tags = connection.tags; existing.icon = connection.icon; - report.updated += 1; + entries.push(ImportEntry { + connection: existing.clone(), + change: ImportChange::Updated, + }); } Some(_) => { connection.id = Uuid::new_v4(); - add_imported(list, connection); - report.added += 1; + entries.push(ImportEntry { + connection: add_imported(list, &held, connection), + change: ImportChange::Added, + }); } None => { - add_imported(list, connection); - report.added += 1; + entries.push(ImportEntry { + connection: add_imported(list, &held, connection), + change: ImportChange::Added, + }); } } } + Ok(ImportPreview { + entries, + before, + after: document, + }) + } + + /// Write what a preview showed. A list that changed since then is + /// refused, since the user agreed to that preview and not another. + pub fn apply_import_blocking(&self, preview: &ImportPreview) -> Result { + let report = preview.report(); + let mut state = self.lock(); + let current = self.ensure_loaded(&mut state)?; + if current != preview.before { + return Err(StorageError::ImportOutdated); + } // Nothing to write for an empty file, and writing anyway would // rewrite the list for no reason. if report.total() == 0 { return Ok(report); } - self.commit(&mut state, document)?; + self.commit(&mut state, preview.after.clone())?; Ok(report) } @@ -730,18 +818,25 @@ fn merge_tags(local: &mut Vec, incoming: Vec) -> H /// Add an entry the import brought. Two rows of one name, one of them /// pointing somewhere else, would leave the user guessing which is /// theirs, and a file's last-opened time is no open on this machine. -fn add_imported(list: &mut Vec, mut connection: SavedConnection) { - connection.name = unclaimed_name(list, &connection.name, None); +fn add_imported( + list: &mut Vec, + held: &HashMap, + mut connection: SavedConnection, +) -> SavedConnection { + connection.name = unclaimed_name(list, held, &connection.name, None); connection.last_opened_at = None; - list.push(connection); + list.push(connection.clone()); + connection } /// `name`, or a suffixed one when an entry other than `owner` holds it. -fn unclaimed_name(list: &[SavedConnection], name: &str, owner: Option) -> String { +fn unclaimed_name(list: &[SavedConnection], held: &HashMap, name: &str, owner: Option) -> String { let taken: Vec<&str> = list .iter() - .filter(|saved| Some(saved.id) != owner) - .map(|saved| saved.name.as_str()) + .map(|saved| (saved.name.as_str(), saved.id)) + .chain(held.iter().map(|(name, id)| (name.as_str(), *id))) + .filter(|(_, id)| Some(*id) != owner) + .map(|(name, _)| name) .collect(); if taken.contains(&name) { suffixed_name(name, "imported", &taken) @@ -1453,6 +1548,161 @@ mod tests { assert_eq!(added.last_opened_at, None, "the file's last-opened time came along"); } + #[test] + fn a_rename_in_the_file_does_not_free_the_name_for_an_added_entry() { + let root = TempDir::new().expect("tempdir"); + let store = store(&root); + let mine = saved("production"); + store.upsert_blocking(mine.clone()).expect("upsert"); + let renamed = SavedConnection { + name: "staging".to_owned(), + ..mine.clone() + }; + let lookalike = SavedConnection { + host: "db.attacker.example".to_owned(), + ..saved("production") + }; + let bytes = crate::document::encode_document(&ConnectionsDocument { + connections: vec![renamed, lookalike.clone()], + ..ConnectionsDocument::default() + }) + .expect("encode"); + + let report = store.import_blocking(Path::new("/tmp/c.json"), &bytes).expect("import"); + + assert_eq!(report, ImportReport { added: 1, updated: 1 }); + let list = store.load_blocking().expect("load"); + let kept = list.iter().find(|saved| saved.id == mine.id).expect("the saved entry"); + assert_eq!(kept.name, "staging"); + let added = list.iter().find(|saved| saved.id == lookalike.id).expect("the import"); + assert_eq!(added.name, "production (imported)"); + } + + #[test] + fn a_rename_in_the_file_does_not_free_the_name_for_another_saved_entry() { + let root = TempDir::new().expect("tempdir"); + let store = store(&root); + let production = saved("production"); + let staging = SavedConnection { + host: "staging.internal".to_owned(), + ..saved("staging") + }; + store.upsert_blocking(production.clone()).expect("upsert"); + store.upsert_blocking(staging.clone()).expect("upsert"); + let swapped = vec![ + SavedConnection { + name: "staging".to_owned(), + ..production.clone() + }, + SavedConnection { + name: "production".to_owned(), + ..staging.clone() + }, + ]; + let bytes = crate::document::encode_document(&ConnectionsDocument { + connections: swapped, + ..ConnectionsDocument::default() + }) + .expect("encode"); + + let report = store.import_blocking(Path::new("/tmp/c.json"), &bytes).expect("import"); + + assert_eq!(report, ImportReport { added: 0, updated: 2 }); + let list = store.load_blocking().expect("load"); + let first = list.iter().find(|saved| saved.id == production.id).expect("production"); + assert_eq!(first.name, "staging (imported)"); + let second = list.iter().find(|saved| saved.id == staging.id).expect("staging"); + assert_eq!(second.name, "production (imported)"); + } + + #[test] + fn a_name_that_only_looks_like_a_saved_one_is_suffixed() { + for lookalike in [ + "production ", + " production", + "production\u{a0}", + "produ\u{200b}ction", + "\u{feff}production", + "production\u{2060}", + ] { + let root = TempDir::new().expect("tempdir"); + let store = store(&root); + store.upsert_blocking(saved("production")).expect("upsert"); + let imported = SavedConnection { + host: "db.attacker.example".to_owned(), + ..saved(lookalike) + }; + let bytes = crate::document::encode_document(&ConnectionsDocument { + connections: vec![imported.clone()], + ..ConnectionsDocument::default() + }) + .expect("encode"); + + store.import_blocking(Path::new("/tmp/c.json"), &bytes).expect("import"); + + let list = store.load_blocking().expect("load"); + let added = list.iter().find(|saved| saved.id == imported.id).expect("the import"); + assert_eq!(added.name, "production (imported)", "{lookalike:?}"); + } + } + + #[test] + fn a_rename_is_stored_as_the_list_shows_it() { + let root = TempDir::new().expect("tempdir"); + let store = store(&root); + let mine = saved("staging"); + store.upsert_blocking(mine.clone()).expect("upsert"); + + let updated = store + .update_blocking(mine.id, |saved| saved.name = "pro\u{200b}duction ".to_owned()) + .expect("update") + .expect("the entry"); + + assert_eq!(updated.name, "production"); + let list = store.load_blocking().expect("load"); + assert_eq!(list[0].name, "production"); + } + + #[test] + fn a_preview_writes_nothing_and_is_refused_once_the_list_changed() { + let root = TempDir::new().expect("tempdir"); + let store = store(&root); + store.upsert_blocking(saved("mine")).expect("upsert"); + let theirs = SavedConnection { + host: "db.elsewhere.example".to_owned(), + ..saved("theirs") + }; + let bytes = crate::document::encode_document(&ConnectionsDocument { + connections: vec![theirs.clone()], + ..ConnectionsDocument::default() + }) + .expect("encode"); + + let preview = store + .preview_import_blocking(Path::new("/tmp/c.json"), &bytes) + .expect("preview"); + + assert_eq!(preview.entries().len(), 1); + assert_eq!(preview.entries()[0].change, ImportChange::Added); + assert_eq!(preview.entries()[0].connection.host, "db.elsewhere.example"); + assert_eq!(store.load_blocking().expect("load").len(), 1); + + store.upsert_blocking(saved("another")).expect("upsert"); + assert!(matches!( + store.apply_import_blocking(&preview), + Err(StorageError::ImportOutdated) + )); + + let fresh = store + .preview_import_blocking(Path::new("/tmp/c.json"), &bytes) + .expect("preview"); + assert_eq!( + store.apply_import_blocking(&fresh).expect("apply"), + ImportReport { added: 1, updated: 0 } + ); + assert_eq!(store.load_blocking().expect("load").len(), 3); + } + #[test] fn an_import_leaves_the_connections_the_file_does_not_mention() { let root = TempDir::new().expect("tempdir"); diff --git a/linux/crates/storage/src/error.rs b/linux/crates/storage/src/error.rs index c558cac8aa..afda34e0e6 100644 --- a/linux/crates/storage/src/error.rs +++ b/linux/crates/storage/src/error.rs @@ -67,4 +67,7 @@ pub enum StorageError { #[error("{} is not valid UTF-8 from byte {offset}", .path.display())] DraftNotUtf8 { path: std::path::PathBuf, offset: usize }, + + #[error("the saved connections changed after the import was previewed")] + ImportOutdated, } diff --git a/linux/crates/storage/src/lib.rs b/linux/crates/storage/src/lib.rs index 49c82d5750..f86688e6d1 100644 --- a/linux/crates/storage/src/lib.rs +++ b/linux/crates/storage/src/lib.rs @@ -26,8 +26,8 @@ pub use connection_icon::ConnectionIcon; pub use connection_tag::ConnectionTag; pub use connection_tag_name::ConnectionTagName; pub use connections::{ - ConnectionList, ConnectionListSnapshot, ConnectionListState, ConnectionStore, ImportReport, RemoveOutcome, - SavedConnection, SavedSshAuth, SavedSshConfig, + ConnectionList, ConnectionListSnapshot, ConnectionListState, ConnectionStore, ImportChange, ImportEntry, + ImportPreview, ImportReport, RemoveOutcome, SavedConnection, SavedSshAuth, SavedSshConfig, }; pub use credentials::{ CredentialDemand, CredentialError, KeyringCredentials, KeyringError, ResolvedSecret, SecretLabeler, diff --git a/linux/crates/storage/src/name_rules.rs b/linux/crates/storage/src/name_rules.rs index 6da5f1ed61..b81ee033d8 100644 --- a/linux/crates/storage/src/name_rules.rs +++ b/linux/crates/storage/src/name_rules.rs @@ -32,10 +32,44 @@ pub(crate) fn cleaned(value: &str, max: usize) -> String { shown.trim().chars().take(max).collect() } +/// A connection's name as the list shows it: control characters as +/// spaces, characters that show as nothing dropped, and no blanks around +/// it. Two names that read the same have the same form, so one cannot +/// pass for the other. +pub(crate) fn shown(value: &str) -> String { + let visible: String = value + .chars() + .filter(|c| !tablepro_core::unicode_blank::is_default_ignorable(*c)) + .map(|c| if c.is_control() { ' ' } else { c }) + .collect(); + visible.trim().to_owned() +} + +/// A stored name, read as [`shown`] gives it. +pub(crate) fn deserialize_shown<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result { + let name = ::deserialize(deserializer)?; + Ok(shown(&name)) +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn a_shown_name_drops_what_cannot_be_seen() { + for lookalike in [ + "production", + " production ", + "production\u{a0}", + "pro\u{200b}duction", + "\u{feff}production", + "production\u{2060}\t", + ] { + assert_eq!(shown(lookalike), "production", "{lookalike:?}"); + } + assert_eq!(shown("prod\nuction"), "prod uction"); + } + #[test] fn a_name_is_trimmed_and_checked_in_characters() { assert_eq!(checked(" Work ", 4), Ok("Work")); diff --git a/linux/crates/test-support/src/fake_dialect.rs b/linux/crates/test-support/src/fake_dialect.rs index 8f6137224c..e6c8a78327 100644 --- a/linux/crates/test-support/src/fake_dialect.rs +++ b/linux/crates/test-support/src/fake_dialect.rs @@ -108,6 +108,7 @@ const CREDENTIAL_RULES: CredentialRules = CredentialRules { ], conditional_prefixes: &[], carriers: &[], + secret_parameters: &[], }; /// The parameter type that carries a value as it is, so a fake bind diff --git a/linux/docs/drivers/mysql.md b/linux/docs/drivers/mysql.md index 0fd7c9ce52..3009052af2 100644 --- a/linux/docs/drivers/mysql.md +++ b/linux/docs/drivers/mysql.md @@ -96,7 +96,7 @@ A call that timed out or lost its connection is checked before the link is decla `ReadOnlyEnforcement::SessionDefault`: `SET SESSION TRANSACTION READ ONLY` on every session. Saves are refused before anything is sent, and a write through generated SQL fails with 1792. At Safe Mode Read-Only the app refuses every editor statement that does not read before it is sent, `SET TRANSACTION READ WRITE` included; a function a `SELECT` calls is the server's to refuse. For a guarantee, connect with a login that has no write privileges. -An export, on any connection, runs in `START TRANSACTION READ ONLY` and refuses user SQL that Safe Mode would not count as a read (`tablepro_core::sql_syntax::statement_access`). +An export, on any connection, runs in `START TRANSACTION READ ONLY` and refuses user SQL that Safe Mode would not count as a read (`tablepro_core::sql_syntax::statement_access`). The text protocol runs every statement a text holds, so user SQL is first prepared, which the server parses with multiple statements off: a text it reads as two fails there, and one it parses but does not prepare (1295, such as some `SHOW`) still runs. Safe Mode reads text every way the client or a server may read it, and the strictest reading stands (`tablepro_core::sql_syntax::script::ScriptPlan::server_readings`): diff --git a/linux/docs/drivers/postgresql.md b/linux/docs/drivers/postgresql.md index a8bca11a92..e65ccd2687 100644 --- a/linux/docs/drivers/postgresql.md +++ b/linux/docs/drivers/postgresql.md @@ -34,13 +34,14 @@ COMMIT - Reads always run in a `READ ONLY` transaction, also on a read-write connection. - `statement_timeout` is the nearer of the call's deadline and the workspace's 60 s. `lock_timeout` is 10 s, so a blocked edit reports the lock instead of a generic timeout. - `SET LOCAL` ends with the transaction, so nothing is left on a server connection a pooler shares. +- A catalog read runs under the session's own search_path, so names print as the user's session sees them, and resolves nothing through it: every function is written `pg_catalog.`, and every operator compares two values of one built-in type, cast where they differ (`attrelid = $1::text::regclass::oid`), or is written `OPERATOR(pg_catalog.)`. A lookalike another user creates on the path can then only tie with the built-in, and `pg_catalog`, searched first, wins the tie. `bare_calls` in `metadata/mod.rs` checks every catalog statement for an unqualified call. - Generated SQL runs on the unnamed statement (`query_typed_raw`, `execute_typed`) with typed parameters. Nothing is prepared under a name. - A connection whose flight did not end with an acknowledged COMMIT or ROLLBACK is taken out of the pool, because deadpool's recycle check (`RecyclingMethod::Verified`) passes inside a half-finished transaction. - A read that returns no rows still has its columns: the statement runs once more as a subquery right-joined to one row on a false condition, in a read-only transaction of its own. - A column of a type tokio-postgres does not know (an enum, `citext`, a domain) would make it prepare a type lookup under a name. Pages project such columns as text, so a read never does. - chrono ends at 262143 AD, and the server keeps dates to 5874897 AD and timestamps to 294276 AD. A value past chrono's end reads as the text the server would print for it, so the cell shows it rather than saying it cannot. A cell edit or a CSV import takes such text back, and it binds as text cast to the column's type. -`probe` takes a pool slot without waiting; when every slot is busy it opens a connection of its own and runs `SELECT 1`. `stream_read` opens a connection of its own, runs `START TRANSACTION READ ONLY` with no statement timeout, and refuses user SQL that would end that transaction (transaction control, `CALL`, `DO`) before sending anything. +`probe` takes a pool slot without waiting; when every slot is busy it opens a connection of its own and runs `SELECT 1`. `stream_read` opens a connection of its own, runs `START TRANSACTION READ ONLY` with no statement timeout, and refuses user SQL that would end that transaction (transaction control, `CALL`, `DO`) before sending anything. User SQL is first parsed with the extended protocol, whose Parse refuses a text that holds more than one statement, and then runs as a simple query, which splits text with the same parser and keeps every value as the server's text, so nothing can follow the read inside its transaction. The Parse prepares under a name, which is safe because the connection is the call's own. ## Stops and timeouts diff --git a/linux/docs/drivers/sqlite.md b/linux/docs/drivers/sqlite.md index a989064003..f5db580130 100644 --- a/linux/docs/drivers/sqlite.md +++ b/linux/docs/drivers/sqlite.md @@ -65,6 +65,8 @@ The workspace statement timeout (60 s) becomes a deadline the flag enforces. The `ReadOnlyEnforcement::EngineEnforced`: the file is opened read-only, so SQLite refuses every write, whatever the SQL (`SQLITE_READONLY`, reported as `ReadOnly(ServerRejected)`). Saves on a read-only connection are refused before anything is sent. Exports always open their connection read-only. +A read-only open still lets SQLite attach another file and write a copy with `VACUUM INTO`, and a pragma such as `foreign_keys` or `hard_heap_limit` changes the connection while SQLite compiles it, `EXPLAIN PRAGMA` included. The connection `stream_read` opens for user SQL (exports and MCP reads) therefore allows no attached database (`SQLITE_LIMIT_ATTACHED` 0), its authorizer denies ATTACH and every pragma but the reporting ones, and the text must prepare to one statement with nothing but comments after it, since sqlx runs every statement a text holds. + ## Writes `WriteAtomicity::Transactional` for row and schema changes. A save is one transaction that starts with `BEGIN IMMEDIATE`, which takes the write lock before the first probe reads anything. Grid saves, structure changes and imports all go through it. Another connection holding the lock shows up in one of two places, each as `Busy(FileLocked)` after the lock timeout, and the file keeps none of the save: diff --git a/linux/docs/storage.md b/linux/docs/storage.md index e90415956b..d83da66845 100644 --- a/linux/docs/storage.md +++ b/linux/docs/storage.md @@ -17,7 +17,7 @@ The app's data goes to the store its shape calls for. The `storage` crate owns t `ConnectionStore` holds the saved-connection list, one store per storage root: -- `load_blocking`, `upsert_blocking`, `update_blocking`, `remove_blocking`, `touch_last_opened_blocking`, `import_blocking` and `export_blocking` run off the GTK thread. A change holds the store's lock across read, change and write, so two writers never lose an entry. `update_blocking` changes one entry as the list holds it at the write, so a last-opened stamp from another window in between is kept. +- `load_blocking`, `upsert_blocking`, `update_blocking`, `remove_blocking`, `touch_last_opened_blocking`, `preview_import_blocking`, `apply_import_blocking` and `export_blocking` run off the GTK thread. A change holds the store's lock across read, change and write, so two writers never lose an entry. `update_blocking` changes one entry as the list holds it at the write, so a last-opened stamp from another window in between is kept. - A write goes through `write_private_blocking` first and the cached list changes only once it landed, so a failed write leaves both as they were. - `subscribe` hands out a watch of `ConnectionListSnapshot`, so every picker sees every writer's change. - A file that cannot be read, or that another version wrote, leaves the store `Unavailable`, and it refuses every write until the user picks Reset (`reset_unreadable_blocking`), which moves the file aside. @@ -29,6 +29,8 @@ The app's data goes to the store its shape calls for. The `storage` crate owns t - The file's `tags` list holds every `ConnectionTag`: an `id`, a `name` (`ConnectionTagName`: trimmed, 1 to 32 characters, no control characters, cleaned on read as a group's is) and a `color` from the same palette as `color`. No two tags share a name in any case: `create_tag_blocking` and `update_tag_blocking` refuse one with `TagNameTaken`. - A connection's `tags` are ids from that list. Every write drops an id the list lacks and a repeated one, so `delete_tag_blocking` takes the tag off every connection in the same write, and a connection saved with a tag deleted meanwhile loses it. - `icon` is one of the `ConnectionIcon` names. The app draws each with a symbolic icon from the theme (`crates/app/src/ui/icons.rs`). +- A connection's `name` is stored and compared as the list shows it: control characters as spaces, characters that show as nothing (U+200B, U+FEFF) dropped, no blanks around it, so a name that looks like a saved one is the same name. +- An import is previewed first: `preview_import_blocking` works out every entry as it will be saved and writes nothing, the app shows each one with its host, and `apply_import_blocking` writes that result, or refuses with `ImportOutdated` when the list changed in between. An imported entry never takes a name another entry holds or held before the import, whatever order the file lists them in. - On import, a tag is matched by id, then by name in any case, and added only when neither matches, so importing a file twice leaves one tag of each name. A tag matched by id takes the file's colour, and its name unless another tag has it. A connection updated by an import takes the file's tags and icon. - An edit, a copy or a move to another group goes through the app's `SavedConnectionEditor`, which writes the keyring first. A refused secret stops before the list changes, and the secrets of a copy whose entry did not land are deleted again. Edits and copies run one at a time, so two windows editing one connection never leave the host of one with the password of the other. @@ -248,7 +250,12 @@ script lexer: only with a given word (`GRANT ... IDENTIFIED`), and functions or engines that take a password (`dblink`, `OPENROWSET`, `s3`). A routine body is read for the statements inside it, whether dollar-quoted or, on - PostgreSQL, a plain string after `DO` or `AS`. The text is read every + PostgreSQL, a plain string after `DO` or `AS`. So is every string of a + script that hands the server text to run (T-SQL `EXEC` and + `sp_executesql`, MySQL `PREPARE ... FROM` and `EXECUTE IMMEDIATE`, + PL/pgSQL `EXECUTE`), since the text may reach them through a variable. + On SQL Server a named argument whose name holds `password` or `pwd` + withholds the entry whatever the procedure. The text is read every way the server may read it: with and without backslash escapes, and with each MySQL `/*!` comment run or skipped as each server version would. diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index bde86fdf4b..a3af478f42 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -430,6 +430,7 @@ crates/app/src/ui/host_key_failure.rs crates/app/src/ui/host_key_review.rs crates/app/src/ui/icons.rs crates/app/src/ui/import_dialog.rs +crates/app/src/ui/import_preview.rs crates/app/src/ui/key_tab/key_summary.rs crates/app/src/ui/key_tab/mod.rs crates/app/src/ui/key_tab/tests.rs From c40c3561e0bb513e5a8dbeba620a52a27e2fb428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 16:36:43 +0700 Subject: [PATCH 21/85] fix(sidebar): show the loaded table list when its spinner started in the same turn (#3893) --- .../Extensions/NSWindow+FirstFrame.swift | 13 ++++++++----- apple/TablePro/Views/Sidebar/SidebarView.swift | 3 ++- .../Extensions/NSWindowNextFrameTests.swift | 18 +++++++++++++++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/apple/TablePro/Extensions/NSWindow+FirstFrame.swift b/apple/TablePro/Extensions/NSWindow+FirstFrame.swift index eb851bc4ed..2b78ba91d6 100644 --- a/apple/TablePro/Extensions/NSWindow+FirstFrame.swift +++ b/apple/TablePro/Extensions/NSWindow+FirstFrame.swift @@ -61,11 +61,14 @@ internal extension NSWindow { /// Work that must not share a run loop turn with the frame this turn builds. @MainActor internal enum CommittedTurn { - /// Runs `body` after this turn's frame is committed, in a later turn. A main-queue hop alone can run - /// before the run loop waits, so the hang would still be one: both pieces between two waits. - static func after(_ body: @escaping @MainActor () -> Void) { - CATransaction.setCompletionBlock { - DispatchQueue.main.async { MainActor.assumeIsolated(body) } + /// Runs `body` once `window` has shown its next frame, or after `nextFrame`'s limit; with no window, on the + /// next main-actor turn. A main-queue hop alone can run before the run loop waits, so the hang would still be + /// one: both pieces between two waits. A transaction's completion block is no end either: it waits for every + /// animation the turn adds, and a spinning progress indicator's never finishes. + static func after(in window: NSWindow?, _ body: @escaping @MainActor () -> Void) { + Task { @MainActor in + await window?.nextFrame() + body() } } } diff --git a/apple/TablePro/Views/Sidebar/SidebarView.swift b/apple/TablePro/Views/Sidebar/SidebarView.swift index 1d9b0e002f..f3d3bc0ddd 100644 --- a/apple/TablePro/Views/Sidebar/SidebarView.swift +++ b/apple/TablePro/Views/Sidebar/SidebarView.swift @@ -210,7 +210,8 @@ struct SidebarView: View { showsLoadedCatalog = false return } - CommittedTurn.after { showsLoadedCatalog = true } + // A refresh can put the catalog back to loading before the frame, and a stale true skips the next hold. + CommittedTurn.after(in: coordinator?.contentWindow) { showsLoadedCatalog = isCatalogLoaded } } .onChange(of: viewModel.showOperationDialog) { isPresented in guard isPresented else { return } diff --git a/apple/TableProTests/Extensions/NSWindowNextFrameTests.swift b/apple/TableProTests/Extensions/NSWindowNextFrameTests.swift index ae48456d27..d5b5fa7913 100644 --- a/apple/TableProTests/Extensions/NSWindowNextFrameTests.swift +++ b/apple/TableProTests/Extensions/NSWindowNextFrameTests.swift @@ -53,11 +53,27 @@ struct NSWindowNextFrameTests { @Test("Work after the committed turn never runs in the turn that asked for it") func committedTurnRunsLater() async throws { + let window = Self.window() var ran = false - CommittedTurn.after { ran = true } + CommittedTurn.after(in: window) { ran = true } #expect(!ran) try await waitUntil { ran } } + + // The sidebar held its list behind this while its own spinner started in the same turn, and kept the spinner. + @Test("Work after the committed turn runs even when that turn starts a spinner that never stops") + func committedTurnOutlastsASpinner() async throws { + let window = Self.window() + var ran = false + + CommittedTurn.after(in: window) { ran = true } + let spinner = NSProgressIndicator(frame: NSRect(x: 0, y: 0, width: 32, height: 32)) + spinner.style = .spinning + window.contentView?.addSubview(spinner) + spinner.startAnimation(nil) + + try await waitUntil { ran } + } } From 5efd8ecc955fef610ad4f11d1b70488749e8be9d Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 13:21:11 +0100 Subject: [PATCH 22/85] docs(linux): record how a query tab reaches another database (#3901) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- ...0030-a-database-is-a-session-of-its-own.md | 38 +++++++++++++++++++ linux/docs/decisions/README.md | 1 + 2 files changed, 39 insertions(+) create mode 100644 linux/docs/decisions/0030-a-database-is-a-session-of-its-own.md diff --git a/linux/docs/decisions/0030-a-database-is-a-session-of-its-own.md b/linux/docs/decisions/0030-a-database-is-a-session-of-its-own.md new file mode 100644 index 0000000000..f2a1d5d0b0 --- /dev/null +++ b/linux/docs/decisions/0030-a-database-is-a-session-of-its-own.md @@ -0,0 +1,38 @@ +# 0030: A query tab's database, and another database as a session of its own + +- **Status**: Accepted +- **Date**: 2026-10-30 + +## Context + +A session is one connection to one database ([0028](0028-sessions-share-a-window.md)). A query tab can run in another of the server's databases, through the editor bar or a `USE` the user types. Engines get there in different ways: MySQL, MariaDB and SQL Server move a live session with `USE`, ClickHouse names the database in every request, Cassandra moves its keyspace, MongoDB's shell keeps a current database, and PostgreSQL binds the database when the connection opens. The window's sidebar and the completion catalog read the database the connection opened. + +[0012](0012-window-per-session.md) rejected a session that changes in place under its parts, and 0028 keeps a tab on the session it was built with. + +## Decision + +A query tab keeps its database, and its own editor session goes there. `EditorSession::use_database` moves a live session by the engine's own means; where the engine binds the database at connect, the driver refuses it and the tab gets an editor session opened in that database (`EditorSessionOptions::database`). A session reports the database a `USE` left it in, and the tab follows. The tab never changes session: its workspace, Safe Mode and teardown stay its connection's. + +What the window shows of another database is a sibling session, `Session::open_in_database`: the same server and login, opened as a reconnect is. It joins the window with a workspace and a sidebar row of its own, from Open Database… on a connection's row, or when a tab moves to that database. Completion in a moved tab reads the sibling's catalog. + +## Rationale + +- **A tab keeps what it built.** A `USE` keeps the tab's temporary tables and settings. The database menu waits while the tab holds a transaction, so a move never rolls one back. +- **The tab's session does not wait on the window's.** A PostgreSQL tab opens in its database over its own connection, with the secret the connection already holds, so a restored tab or a lost connection reopens there without a sibling. +- **The catalog is the database's own.** A sibling lists its own database, so the sidebar and completion are right on engines whose databases hold schemas. Until it opens, completion reads the connection's catalog where the server's databases are its schemas (MySQL, ClickHouse, Cassandra, MongoDB) and offers no tables where they are not. +- **No session changes under its parts.** A sibling is a session like any other, so 0012 and 0028 hold. + +## Consequences + +- A sibling opens with the stored secret and never prompts; one that cannot open says so in a toast and the tab still runs in its database. +- The registry keys a sibling by its connection and database, so a database opens once per window. It goes when its connection disconnects. +- A tab names its database in its title when it is not the connection's. +- The workspace record keeps a tab's database, and a restore reopens a connection's open databases beside it once it has joined. + +## Alternatives considered + +**One session that follows the tab in front.** Every part reading the session would reset at each tab switch, which 0012 rejected. + +**Every move opens a new editor session.** One path for every engine, but a `USE` engine would lose its temporary tables and settings at each move. + +**A PostgreSQL tab runs on its sibling's connection.** The tab would depend on a window session the user can close, and a password typed only for the connection could not open it. diff --git a/linux/docs/decisions/README.md b/linux/docs/decisions/README.md index 2c26060651..734ee04abe 100644 --- a/linux/docs/decisions/README.md +++ b/linux/docs/decisions/README.md @@ -77,6 +77,7 @@ A fact the repository keeps elsewhere, such as a workflow trigger, a branch, a v | [0027](0027-one-data-grid.md) | One data grid on GtkColumnView, with rows and a focused cell | Accepted | Every surface shows rows in one `DataGrid`: row selection plus a focused cell, stable row slots, a row number column, values as text and a row detail pane. | | [0028](0028-sessions-share-a-window.md) | A window holds several sessions in one tab view | Accepted | A `WorkspaceShell` per window shares one tab view among its sessions, each with its own workspace; the sidebar is a tree of the saved connections with each open session's schemas and tables. | | [0029](0029-images-decode-in-glycin.md) | Images from the database decode in glycin's sandbox | Accepted | Row Details decodes a value's picture with the glycin crate 3.x, in a sandboxed loader process, never with GTK's in-process loaders. | +| [0030](0030-a-database-is-a-session-of-its-own.md) | A query tab's database, and another database as a session of its own | Accepted | A query tab's own editor session moves to its database through the driver, or opens there where the engine binds it at connect; another database the window shows is a sibling session with its own workspace and sidebar row. | 0011 is not used. It was reserved for the DDL decision, which was recorded as 0010, and docs cite ADRs by number, so the later ones keep theirs. From bd9181a81dfa9bc693aaad72d8c8fb7be628dd11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 19:44:06 +0700 Subject: [PATCH 23/85] fix(filters): escape a backslash in Snowflake filter values (#3906) --- .../TableProQuery/FilterSQLGenerator.swift | 52 +++++++------- .../FilterSQLGeneratorTests.swift | 36 ++++++++++ .../Core/Database/FilterSQLGenerator.swift | 59 +++++----------- .../TableProMobile/Helpers/SQLBuilder.swift | 26 +------ .../SQLBuilderFilterLiteralTests.swift | 25 +++++++ .../Database/FilterSQLGeneratorTests.swift | 67 +++++++++++++++++++ apple/changelog.d/3906.fixed.md | 1 + apple/changelog.d/3906.security.md | 1 + 8 files changed, 173 insertions(+), 94 deletions(-) create mode 100644 apple/changelog.d/3906.fixed.md create mode 100644 apple/changelog.d/3906.security.md diff --git a/apple/Packages/TableProCore/Sources/TableProQuery/FilterSQLGenerator.swift b/apple/Packages/TableProCore/Sources/TableProQuery/FilterSQLGenerator.swift index 93db218345..a3f252e54b 100644 --- a/apple/Packages/TableProCore/Sources/TableProQuery/FilterSQLGenerator.swift +++ b/apple/Packages/TableProCore/Sources/TableProQuery/FilterSQLGenerator.swift @@ -87,13 +87,13 @@ public struct FilterSQLGenerator: Sendable { let escapedSecond = escapeValue(filter.secondValue, kind: kind) return "\(quotedColumn) BETWEEN \(escapedValue) AND \(escapedSecond)" case .contains: - let pattern = escapeLikePattern(filter.value) + let pattern = likePatternBody(filter.value) return likeCondition(operand, "'%\(pattern)%'", negated: false, folding: folding) case .startsWith: - let pattern = escapeLikePattern(filter.value) + let pattern = likePatternBody(filter.value) return likeCondition(operand, "'\(pattern)%'", negated: false, folding: folding) case .endsWith: - let pattern = escapeLikePattern(filter.value) + let pattern = likePatternBody(filter.value) return likeCondition(operand, "'%\(pattern)'", negated: false, folding: folding) } } @@ -159,7 +159,7 @@ public struct FilterSQLGenerator: Sendable { _ column: String, _ pattern: String, negated: Bool, folding: PluginSQLCaseFolding ) -> String { let keyword = negated ? folding.notLikeKeyword : folding.likeKeyword - return "\(folding.foldingLikeOperand(column)) \(keyword) \(folding.foldingLikeOperand(pattern))\(likeEscape)" + return "\(folding.foldingLikeOperand(column)) \(keyword) \(folding.foldingLikeOperand(pattern))\(likeEscapeClause)" } private func listCondition( @@ -178,7 +178,7 @@ public struct FilterSQLGenerator: Sendable { return "\(subject) \(keyword) (\(rendered.joined(separator: ", ")))" } - private var likeEscape: String { + public var likeEscapeClause: String { switch dialect.likeEscapeStyle { case .explicit: return " ESCAPE '!'" @@ -187,6 +187,17 @@ public struct FilterSQLGenerator: Sendable { } } + // The LIKE layer first, with the escape `likeEscapeClause` names, then the literal rule over the whole pattern. + public func likePatternBody(_ value: String) -> String { + let escape = dialect.likeEscapeStyle == .implicit ? "\\" : "!" + // `[` opens a character class in a SQL Server LIKE pattern. + let metacharacters = [escape, "%", "_"] + (databaseType == .mssql ? ["["] : []) + let pattern = metacharacters.reduce(value) { pattern, metacharacter in + pattern.replacingOccurrences(of: metacharacter, with: escape + metacharacter, options: .literal) + } + return literalBody(pattern) + } + private func quoteIdentifier(_ name: String) -> String { if let identifierQuoter { return identifierQuoter(name) } let open = dialect.identifierQuote @@ -199,32 +210,15 @@ public struct FilterSQLGenerator: Sendable { if FilterColumnKind.isBareNumber(value, kind: kind) { return value } - var escaped = value.replacingOccurrences(of: "\0", with: "") - if dialect.requiresBackslashEscaping { - escaped = escaped.replacingOccurrences(of: "\\", with: "\\\\") - } - escaped = escaped.replacingOccurrences(of: "'", with: "''") - return "'\(escaped)'" + return "'\(literalBody(value))'" } - private func escapeLikePattern(_ value: String) -> String { - var result = value - .replacingOccurrences(of: "'", with: "''") - .replacingOccurrences(of: "\0", with: "") - switch dialect.likeEscapeStyle { - case .explicit: - result = result - .replacingOccurrences(of: "!", with: "!!") - .replacingOccurrences(of: "%", with: "!%") - .replacingOccurrences(of: "_", with: "!_") - case .implicit: - if dialect.requiresBackslashEscaping { - result = result.replacingOccurrences(of: "\\", with: "\\\\") - } - result = result - .replacingOccurrences(of: "%", with: "\\%") - .replacingOccurrences(of: "_", with: "\\_") + // `.literal` compares code units: the default compares grapheme clusters and skips a quote a combining mark follows. + private func literalBody(_ value: String) -> String { + var body = value.replacingOccurrences(of: "\0", with: "", options: .literal) + if dialect.requiresBackslashEscaping { + body = body.replacingOccurrences(of: "\\", with: "\\\\", options: .literal) } - return result + return body.replacingOccurrences(of: "'", with: "''", options: .literal) } } diff --git a/apple/Packages/TableProCore/Tests/TableProQueryTests/FilterSQLGeneratorTests.swift b/apple/Packages/TableProCore/Tests/TableProQueryTests/FilterSQLGeneratorTests.swift index 0e64785a6e..1e693abbaa 100644 --- a/apple/Packages/TableProCore/Tests/TableProQueryTests/FilterSQLGeneratorTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProQueryTests/FilterSQLGeneratorTests.swift @@ -198,6 +198,42 @@ struct FilterSQLGeneratorTests { #expect(clause("code", "a\\' OR 1=1 -- ", dialect: mysql) == "WHERE `code` = 'a\\\\'' OR 1=1 -- '") } + // Snowflake takes an ESCAPE clause for LIKE and still reads `\'` inside a literal as a quote. + @Test("A LIKE pattern doubles a backslash where the engine reads it as an escape, whatever the LIKE style") + func likeDoublesBackslashForTheLiteral() { + let explicitBackslash = SQLDialectDescriptor( + identifierQuote: "\"", keywords: [], functions: [], dataTypes: [], + likeEscapeStyle: .explicit, requiresBackslashEscaping: true + ) + #expect( + clause("code", #"\' OR 1=1 -- "#, op: .contains, dialect: explicitBackslash) + == #"WHERE "code" LIKE '%\\'' OR 1=1 -- %' ESCAPE '!'"# + ) + } + + @Test("A MySQL LIKE keeps a backslash literal through the string and the pattern") + func mysqlLikeBackslashIsLiteral() { + let mysql = makeDialect(quote: "`", backslashEscaping: true) + #expect(clause("code", #"a\b%"#, op: .contains, dialect: mysql) == #"WHERE `code` LIKE '%a\\\\b\\%%'"#) + } + + @Test("A SQL Server LIKE escapes an opening bracket") + func mssqlLikeEscapesBracket() { + let generator = FilterSQLGenerator(dialect: makeDialect(quote: "["), databaseType: .mssql) + let filter = TableFilter(columnName: "code", filterOperator: .contains, value: "[a]") + #expect(generator.generateWhereClause(from: [filter], logicMode: .and) == "WHERE [code] LIKE '%![a]%' ESCAPE '!'") + } + + // A combining mark joins the quote into one grapheme cluster. + @Test("A quote fused with a combining mark is still escaped") + func fusedQuoteIsEscaped() { + #expect(clause("code", "x'\u{0301} OR 1=1 --") == "WHERE \"code\" = 'x''\u{0301} OR 1=1 --'") + #expect( + clause("code", "x'\u{0301})", op: .contains) + == "WHERE \"code\" LIKE '%x''\u{0301})%' ESCAPE '!'" + ) + } + @Test("A backslash stays single where the engine reads it as a character") func backslashStaysSingle() { #expect(clause("path", "C:\\temp") == "WHERE \"path\" = 'C:\\temp'") diff --git a/apple/TablePro/Core/Database/FilterSQLGenerator.swift b/apple/TablePro/Core/Database/FilterSQLGenerator.swift index 6f0f1b97f0..96b8a39e75 100644 --- a/apple/TablePro/Core/Database/FilterSQLGenerator.swift +++ b/apple/TablePro/Core/Database/FilterSQLGenerator.swift @@ -160,7 +160,7 @@ struct FilterSQLGenerator { case .regex: let operand = patternOperand(quotedColumn, columnType: columnType) guard dialect.regexSyntax != .unsupported else { - let pattern = quotedLiteral("%\(escapeSQLQuote(filter.value))%") + let pattern = quotedLiteral("%\(escapeStringValue(filter.value))%") return "\(folding.foldingLikeOperand(operand)) \(folding.likeKeyword) " + folding.foldingLikeOperand(pattern) } @@ -322,9 +322,7 @@ struct FilterSQLGenerator { // MARK: - LIKE Conditions - /// Database-specific ESCAPE clause for LIKE patterns. - /// Implicit style (MySQL/MariaDB): backslash is the default LIKE escape, no clause needed. - /// Explicit style: requires an ESCAPE declaration. + // An implicit-style engine escapes LIKE with a backslash by default and takes no clause. private var likeEscapeClause: String { if dialect.likeEscapeStyle == .implicit { return "" } return " ESCAPE '!'" @@ -364,7 +362,7 @@ struct FilterSQLGenerator { negated: Bool, folding: PluginSQLCaseFolding ) -> String { - let quotedPattern = quotedLiteral(escapeSQLQuote(pattern)) + let quotedPattern = quotedLiteral(escapeStringValue(pattern)) let keyword = negated ? folding.notLikeKeyword : folding.likeKeyword let operand = folding.foldingLikeOperand(column) return "\(operand) \(keyword) \(folding.foldingLikeOperand(quotedPattern))\(likeEscapeClause)" @@ -523,50 +521,27 @@ struct FilterSQLGenerator { return isTrue ? "1" : "0" } - /// Escape only single quotes for SQL string literal context. - /// Used for LIKE patterns where wildcards are already escaped - /// by escapeLikeWildcards for the ESCAPE clause. - private func escapeSQLQuote(_ value: String) -> String { - guard value.contains("'") else { return value } - return value.replacingOccurrences(of: "'", with: "''") + // Snowflake takes `ESCAPE '!'` yet reads `\'` as a quote; BigQuery and Spanner read `\` without declaring it. + private var literalsReadBackslash: Bool { + dialect.requiresBackslashEscaping || dialect.likeEscapeStyle == .implicit } - /// Escape special characters in string values + // `.literal` compares code units: the default compares grapheme clusters and skips a quote a combining mark follows. private func escapeStringValue(_ value: String) -> String { - // Fast path: most values have no special chars - if dialect.likeEscapeStyle == .implicit { - // MySQL/MariaDB/ClickHouse: backslash is significant in string literals - guard value.contains("\\") || value.contains("'") else { return value } - return value - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "'", with: "''") - } else { - // ANSI SQL: only single-quote needs escaping - guard value.contains("'") else { return value } - return value.replacingOccurrences(of: "'", with: "''") - } + let body = literalsReadBackslash + ? value.replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + : value + return body.replacingOccurrences(of: "'", with: "''", options: .literal) } + // Only the LIKE layer: escapeStringValue then spells the pattern as a literal. private func escapeLikeWildcards(_ value: String) -> String { - if dialect.likeEscapeStyle == .implicit { - guard value.contains("\\") || value.contains("%") || value.contains("_") else { return value } - // MySQL uses \ as both string escape and default LIKE escape. - // Need double backslash in SQL string so string layer yields single \ - // which LIKE then uses as escape char. - return value - .replacingOccurrences(of: "\\", with: "\\\\\\\\") - .replacingOccurrences(of: "%", with: "\\\\%") - .replacingOccurrences(of: "_", with: "\\\\_") - } + let escape = dialect.likeEscapeStyle == .implicit ? "\\" : "!" // `[` opens a character class in a SQL Server LIKE pattern. - let escapesBracket = family == .mssql - guard value.contains("!") || value.contains("%") || value.contains("_") - || escapesBracket && value.contains("[") else { return value } - let escaped = value - .replacingOccurrences(of: "!", with: "!!") - .replacingOccurrences(of: "%", with: "!%") - .replacingOccurrences(of: "_", with: "!_") - return escapesBracket ? escaped.replacingOccurrences(of: "[", with: "![") : escaped + let metacharacters = [escape, "%", "_"] + (family == .mssql ? ["["] : []) + return metacharacters.reduce(value) { pattern, metacharacter in + pattern.replacingOccurrences(of: metacharacter, with: escape + metacharacter, options: .literal) + } } } diff --git a/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift b/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift index 1111957986..cc0065f44b 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift @@ -338,15 +338,14 @@ nonisolated enum SQLBuilder { let trimmed = searchText.trimmingCharacters(in: .whitespacesAndNewlines) guard !trimmed.isEmpty, !columns.isEmpty else { return "" } - let dialect = dialectDescriptor(for: type) - let pattern = escapeLikePattern(trimmed, dialect: dialect) - let likeEscape: String = dialect.likeEscapeStyle == .explicit ? " ESCAPE '!'" : "" + let generator = filterGenerator(dialect: dialectDescriptor(for: type), type: type) + let pattern = generator.likePatternBody(trimmed) let conditions = columns.map { col -> String in let quotedCol = quoteIdentifier(col.name, for: type) let castExpr = searchCastExpression(quotedCol, for: type) let likeOp = (type == .postgresql || type == .redshift) ? "ILIKE" : "LIKE" - return "\(castExpr) \(likeOp) '%\(pattern)%'\(likeEscape)" + return "\(castExpr) \(likeOp) '%\(pattern)%'\(generator.likeEscapeClause)" } return "(\(conditions.joined(separator: " OR ")))" @@ -370,25 +369,6 @@ nonisolated enum SQLBuilder { } } - private static func escapeLikePattern(_ value: String, dialect: SQLDialectDescriptor) -> String { - var result = value - .replacingOccurrences(of: "'", with: "''") - .replacingOccurrences(of: "\0", with: "") - if dialect.likeEscapeStyle == .explicit { - result = result - .replacingOccurrences(of: "!", with: "!!") - .replacingOccurrences(of: "%", with: "!%") - .replacingOccurrences(of: "_", with: "!_") - } else { - if dialect.requiresBackslashEscaping { - result = result.replacingOccurrences(of: "\\", with: "\\\\") - } - result = result.replacingOccurrences(of: "%", with: "\\%") - result = result.replacingOccurrences(of: "_", with: "\\_") - } - return result - } - private static func buildOrderByClause(_ sortState: SortState, for type: DatabaseType) -> String { guard sortState.isSorting else { return "" } let clauses = sortState.columns.map { col in diff --git a/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderFilterLiteralTests.swift b/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderFilterLiteralTests.swift index dab720d898..bf48bbc4c6 100644 --- a/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderFilterLiteralTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderFilterLiteralTests.swift @@ -70,6 +70,31 @@ struct SQLBuilderFilterLiteralTests { ) == "SELECT COUNT(*) FROM `files` WHERE `path` = 'C:\\\\temp\\\\'") } + private func search(_ type: DatabaseType, _ text: String) -> String { + SQLBuilder.buildSearchCount( + table: "t", schema: nil, type: type, searchText: text, searchColumns: [ColumnInfo(name: "name", typeName: "TEXT")] + ) + } + + @Test("A MySQL search keeps a backslash literal through the string and the pattern") + func mysqlSearchBackslashIsLiteral() { + #expect(search(.mysql, #"a\b"#) == #"SELECT COUNT(*) FROM `t` WHERE (CAST(`name` AS CHAR) LIKE '%a\\\\b%')"#) + } + + @Test("A SQL Server search escapes an opening bracket") + func mssqlSearchEscapesBracket() { + #expect(search(.mssql, "[a]") == "SELECT COUNT(*) FROM [t] WHERE (CAST([name] AS NVARCHAR(MAX)) LIKE '%![a]%' ESCAPE '!')") + } + + // A combining mark joins the quote into one grapheme cluster. + @Test("A search quote fused with a combining mark is still escaped") + func searchFusedQuoteIsEscaped() { + #expect( + search(.postgresql, "x'\u{0301})") + == "SELECT COUNT(*) FROM \"t\" WHERE (CAST(\"name\" AS TEXT) ILIKE '%x''\u{0301})%' ESCAPE '!')" + ) + } + // ClickHouse reads backslash escapes inside backticks, so a name ending in a backslash would leave the quote open. @Test("A ClickHouse filter quotes its column the way the table and the writes do") func clickHouseColumnEndingInABackslash() { diff --git a/apple/TableProTests/Core/Database/FilterSQLGeneratorTests.swift b/apple/TableProTests/Core/Database/FilterSQLGeneratorTests.swift index 9126b678f0..34f8a47c9f 100644 --- a/apple/TableProTests/Core/Database/FilterSQLGeneratorTests.swift +++ b/apple/TableProTests/Core/Database/FilterSQLGeneratorTests.swift @@ -56,6 +56,13 @@ struct FilterSQLGeneratorTests { offsetFetchOrderBy: "" ) + private static let snowflakeDialect = SQLDialectDescriptor( + identifierQuote: "\"", keywords: [], functions: [], dataTypes: [], + regexSyntax: .regexpLike, booleanLiteralStyle: .truefalse, + likeEscapeStyle: .explicit, paginationStyle: .limit, + requiresBackslashEscaping: true + ) + // MARK: - Per-Operator Tests (MySQL) @Test("Equal operator generates correct condition") @@ -1038,4 +1045,64 @@ struct FilterSQLGeneratorTests { let result = generator.generateCondition(from: filter) #expect(result == "`status` IS NULL") } + + // MARK: - Literal Escaping + + private func condition( + _ dialect: SQLDialectDescriptor, _ filterOperator: FilterOperator, _ value: String, column: String = "code" + ) -> String? { + FilterSQLGenerator(dialect: dialect).generateCondition(from: TableFilter( + columnName: column, filterOperator: filterOperator, value: value, isCaseSensitive: true + )) + } + + // Snowflake takes an ESCAPE clause for LIKE and still reads `\'` inside a literal as a quote. + @Test("A Snowflake filter value doubles its backslash, whatever the operator", arguments: [ + (FilterOperator.equal, #""CODE" = '\\''; DROP TABLE AUDIT_LOG; --'"#), + (.inList, #""CODE" IN ('\\''; DROP TABLE AUDIT_LOG; --')"#), + (.contains, #""CODE" LIKE '%\\''; DROP TABLE AUDIT!_LOG; --%' ESCAPE '!'"#), + (.startsWith, #""CODE" LIKE '\\''; DROP TABLE AUDIT!_LOG; --%' ESCAPE '!'"#), + (.regex, #"REGEXP_LIKE("CODE", '\\''; DROP TABLE AUDIT_LOG; --')"#) + ]) + func snowflakeDoublesBackslash(filterOperator: FilterOperator, expected: String) { + #expect(condition(Self.snowflakeDialect, filterOperator, #"\'; DROP TABLE AUDIT_LOG; --"#, column: "CODE") == expected) + } + + @Test("A Snowflake LIKE pattern reads a backslash as a character, not as its escape") + func snowflakeLikeBackslashIsLiteral() { + #expect(condition(Self.snowflakeDialect, .endsWith, #"C:\temp\"#) == #""code" LIKE '%C:\\temp\\' ESCAPE '!'"#) + } + + @Test("The LIKE a regex falls back to doubles a backslash where the engine reads it as an escape") + func regexFallbackDoublesBackslash() { + let implicitWithoutRegex = SQLDialectDescriptor( + identifierQuote: "`", keywords: [], functions: [], dataTypes: [], + regexSyntax: .unsupported, booleanLiteralStyle: .truefalse, + likeEscapeStyle: .implicit, paginationStyle: .limit + ) + #expect(condition(implicitWithoutRegex, .regex, #"\' OR 1=1 -- "#) == #"`code` LIKE '%\\'' OR 1=1 -- %'"#) + #expect(condition(Self.sqliteDialect, .regex, #"\' OR 1=1 -- "#) == #"`code` LIKE '%\'' OR 1=1 -- %'"#) + } + + @Test("A MySQL LIKE keeps a backslash literal through the string and the pattern") + func mysqlLikeBackslash() { + #expect(condition(Self.mysqlDialect, .contains, #"a\b%"#) == #"`code` LIKE '%a\\\\b\\%%'"#) + } + + // A combining mark or a Prepend scalar joins the quote or backslash into one grapheme cluster. + @Test("A quote or backslash fused with its neighbour is still escaped", arguments: [ + ("x'\u{0301} OR 1=1 --", #"`code` = 'x''"# + "\u{0301}" + #" OR 1=1 --'"#), + ("\u{0600}\\' OR 1=1 --", "`code` = '\u{0600}" + #"\\'' OR 1=1 --'"#) + ]) + func fusedQuoteIsEscaped(value: String, expected: String) { + #expect(condition(Self.mysqlDialect, .equal, value) == expected) + } + + @Test("A quote fused with a combining mark is escaped in a LIKE pattern") + func fusedQuoteInLikeIsEscaped() { + #expect( + condition(Self.postgresqlDialect, .contains, "x'\u{0301})") + == "\"code\" LIKE '%x''\u{0301})%' ESCAPE '!'" + ) + } } diff --git a/apple/changelog.d/3906.fixed.md b/apple/changelog.d/3906.fixed.md new file mode 100644 index 0000000000..a8bc15a191 --- /dev/null +++ b/apple/changelog.d/3906.fixed.md @@ -0,0 +1 @@ +On iOS, a search or filter with a backslash on MySQL or ClickHouse, or with `[` on SQL Server, matched the wrong rows. diff --git a/apple/changelog.d/3906.security.md b/apple/changelog.d/3906.security.md new file mode 100644 index 0000000000..5ffab5534c --- /dev/null +++ b/apple/changelog.d/3906.security.md @@ -0,0 +1 @@ +A backslash in a Snowflake filter value could end its string literal and run the rest as SQL. From cf29cb54acaa595b0c18fde5b2ba4229d3c1a0fd Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 14:17:58 +0100 Subject: [PATCH 24/85] feat(linux-editor): name the tab's database and schema in the editor bar (#3896) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3896.added.md | 1 + .../app/src/ui/editor/editor_session_slot.rs | 5 + linux/crates/app/src/ui/editor/imp.rs | 4 + linux/crates/app/src/ui/editor/mod.rs | 3 + linux/crates/app/src/ui/editor/tab_scope.rs | 113 ++++++++++++++++++ linux/crates/app/src/ui/editor/tests.rs | 113 ++++++++++++++++++ .../core/src/dialect/database_layout.rs | 13 ++ linux/crates/core/src/dialect/mod.rs | 2 + linux/crates/core/src/dialect/sql_dialect.rs | 8 +- linux/crates/core/src/editor_session.rs | 7 ++ .../core/src/sql_syntax/catalog_change.rs | 28 +++++ linux/crates/core/src/sql_syntax/mod.rs | 2 +- linux/crates/drivers/cassandra/src/dialect.rs | 8 +- linux/crates/drivers/cassandra/src/editor.rs | 6 + .../crates/drivers/clickhouse/src/dialect.rs | 8 +- .../drivers/clickhouse/src/editor_session.rs | 6 + linux/crates/drivers/mongodb/src/dialect.rs | 8 +- .../drivers/mongodb/src/editor_session.rs | 4 + linux/crates/drivers/mssql/src/dialect.rs | 8 +- linux/crates/drivers/mssql/src/editor_run.rs | 10 ++ .../drivers/mssql/src/editor_session.rs | 27 ++++- .../mssql/tests/integration/sessions.rs | 5 + linux/crates/drivers/mysql/src/dialect.rs | 8 +- .../drivers/mysql/src/editor_session.rs | 48 +++++++- .../mysql/tests/integration/editor_session.rs | 29 +++++ linux/crates/drivers/postgres/src/dialect.rs | 7 +- .../test-support/src/fake_editor_session.rs | 11 ++ linux/data/resources/ui/sql-editor.ui | 23 ++++ linux/po/POTFILES.in | 1 + 29 files changed, 494 insertions(+), 22 deletions(-) create mode 100644 linux/changelog.d/3896.added.md create mode 100644 linux/crates/app/src/ui/editor/tab_scope.rs create mode 100644 linux/crates/core/src/dialect/database_layout.rs diff --git a/linux/changelog.d/3896.added.md b/linux/changelog.d/3896.added.md new file mode 100644 index 0000000000..325194bc2f --- /dev/null +++ b/linux/changelog.d/3896.added.md @@ -0,0 +1 @@ +The SQL editor's bar names the database the tab's statements run in, and its schema where a database holds schemas. diff --git a/linux/crates/app/src/ui/editor/editor_session_slot.rs b/linux/crates/app/src/ui/editor/editor_session_slot.rs index 5e18a149e8..58d5019343 100644 --- a/linux/crates/app/src/ui/editor/editor_session_slot.rs +++ b/linux/crates/app/src/ui/editor/editor_session_slot.rs @@ -139,6 +139,11 @@ impl EditorSessionSlot { } } + /// Where the tab's session says it is, once it says. + pub fn database(&self) -> Option { + lock(&self.state).open.as_ref().and_then(|open| open.editor.database()) + } + /// Where the transaction of `editor` stood after its last run. pub fn transaction(&self, editor: &Arc) -> Option { let state = lock(&self.state); diff --git a/linux/crates/app/src/ui/editor/imp.rs b/linux/crates/app/src/ui/editor/imp.rs index bc9ccfddf1..c89247faa3 100644 --- a/linux/crates/app/src/ui/editor/imp.rs +++ b/linux/crates/app/src/ui/editor/imp.rs @@ -29,6 +29,10 @@ pub struct RunningScript { #[derive(Default, gtk4::CompositeTemplate)] #[template(resource = "/app/tablepro/TablePro/ui/sql-editor.ui")] pub struct SqlEditor { + #[template_child] + pub scope_box: TemplateChild, + #[template_child] + pub scope_label: TemplateChild, #[template_child] pub cursor_info: TemplateChild, #[template_child] diff --git a/linux/crates/app/src/ui/editor/mod.rs b/linux/crates/app/src/ui/editor/mod.rs index 25bfae0b0b..d994114567 100644 --- a/linux/crates/app/src/ui/editor/mod.rs +++ b/linux/crates/app/src/ui/editor/mod.rs @@ -26,6 +26,7 @@ mod sidebar_policy; mod significant_tokens; mod sql_file; mod statement_cursor; +mod tab_scope; #[cfg(test)] mod tests; mod transaction_dirty; @@ -152,6 +153,7 @@ impl SqlEditor { editor.install_drop_target(); editor.connect_buttons(); editor.connect_grid(); + editor.show_scope(); editor } @@ -689,6 +691,7 @@ impl SqlEditor { self.emit_by_name::<()>("transaction-changed", &[&dirty]); } self.record_history(entry, dialect); + self.show_scope(); self.emit_by_name::<()>("run-finished", &[&outcome]); self.emit_by_name::<()>("run-state-changed", &[&false]); } diff --git a/linux/crates/app/src/ui/editor/tab_scope.rs b/linux/crates/app/src/ui/editor/tab_scope.rs new file mode 100644 index 0000000000..e4789ef638 --- /dev/null +++ b/linux/crates/app/src/ui/editor/tab_scope.rs @@ -0,0 +1,113 @@ +//! The database the tab works on, named in the editor bar. + +use gtk4::glib; +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use tablepro_core::dialect::DatabaseLayout; +use tablepro_session::Session; + +use super::SqlEditor; +use crate::i18n::gettext_f; + +/// The bar's text, dot-qualified as SQL spells a qualified name, and what +/// a screen reader says for it. +pub(super) fn scope_text(database: &str, schema: Option<&str>) -> (String, String) { + match schema.filter(|schema| !schema.is_empty()) { + Some(schema) => ( + format!("{database}.{schema}"), + gettext_f( + "{database}, schema {schema}", + &[("database", database), ("schema", schema)], + ), + ), + None => (database.to_owned(), database.to_owned()), + } +} + +impl SqlEditor { + /// Name where the tab's next statement runs and, on an engine whose + /// databases hold schemas, the schema completion resolves names in. + pub(super) fn show_scope(&self) { + let Some((session, tasks)) = self + .deps() + .and_then(|deps| deps.session.get().map(|session| (session, deps.tasks.clone()))) + else { + self.set_scope(None); + return; + }; + let Some((database, layout)) = self.scope_database(&session) else { + self.set_scope(None); + return; + }; + self.set_scope(Some(scope_text(&database, None))); + // The catalog describes the connection's own database only. + if layout != DatabaseLayout::WithSchemas || session.database() != Some(database.as_str()) { + return; + } + let reading = { + let session = session.clone(); + tasks.spawn_task(async move { + session.schemas().await.ok().and_then(|schemas| { + schemas + .iter() + .find(|schema| schema.is_default) + .map(|schema| schema.name.clone()) + }) + }) + }; + let editor = self.downgrade(); + glib::spawn_future_local(async move { + let Some(schema) = reading.await.ok().flatten() else { + return; + }; + if let Some(editor) = editor.upgrade() + && editor.scope_database(&session).is_some_and(|(now, _)| now == database) + { + editor.set_scope(Some(scope_text(&database, Some(&schema)))); + } + }); + } + + /// The database the tab's session last reported, else the + /// connection's. `None` where a connection reaches one database. + fn scope_database(&self, session: &Session) -> Option<(String, DatabaseLayout)> { + let layout = session.current_dialect()?.database_layout(); + if layout == DatabaseLayout::Single { + return None; + } + let database = self + .imp() + .slot + .database() + .or_else(|| session.database().map(str::to_owned)) + .filter(|name| !name.is_empty())?; + Some((database, layout)) + } + + fn set_scope(&self, scope: Option<(String, String)>) { + let imp = self.imp(); + let shown = scope.is_some(); + if let Some((text, spoken)) = scope { + imp.scope_label.set_label(&text); + imp.scope_box + .update_property(&[gtk4::accessible::Property::Label(&spoken)]); + imp.scope_box.set_tooltip_text(Some(&spoken)); + } + imp.scope_box.set_visible(shown); + } +} + +#[cfg(test)] +mod tests { + use super::scope_text; + + #[test] + fn the_scope_names_the_database_then_its_schema() { + assert_eq!( + scope_text("shop", Some("public")), + ("shop.public".to_owned(), "shop, schema public".to_owned()) + ); + assert_eq!(scope_text("shop", None), ("shop".to_owned(), "shop".to_owned())); + assert_eq!(scope_text("shop", Some("")), ("shop".to_owned(), "shop".to_owned())); + } +} diff --git a/linux/crates/app/src/ui/editor/tests.rs b/linux/crates/app/src/ui/editor/tests.rs index 6e4ffb6e2e..f0a2d0dec2 100644 --- a/linux/crates/app/src/ui/editor/tests.rs +++ b/linux/crates/app/src/ui/editor/tests.rs @@ -2618,3 +2618,116 @@ fn a_command_editor_has_none_of_the_sql_tools() { "the document's words and the engine's own snippets complete" ); } + +/// An editor on a connection to `shop`, whose catalog lists `schemas` +/// with the first as the default. +fn scoped_editor(dialect: Arc, schemas: &[&str]) -> Harness { + crate::register_test_resources(); + let runtime = crate::test_support::test_runtime(); + let fake = Arc::new(FakeEditorSession::new(answering())); + let mut request = FakeConnector::network_request("db.example", 5432).expect("a request"); + if let tablepro_session::connect::RequestTarget::Network { database, .. } = &mut request.target { + *database = Some("shop".to_owned()); + } + let mut script = ConnectionScript { + dialect, + ..ConnectionScript::default() + }; + let editor_session: Arc = fake.clone(); + script.open_editor_session.always(FakeReply::ok(editor_session)); + let (pending, _) = FakeConnector::pending_session(request, script); + let session = Session::start( + pending, + tablepro_session::SessionDeps { + connector: Arc::new(FakeConnector::new([])), + credentials: Arc::new(FakeCredentialSource::new()), + deadlines: Arc::new(DefaultDeadlines::new(tablepro_core::LivenessPolicy::DESKTOP)), + tasks: runtime.tasks(), + prompter: None, + }, + ); + let listed = schemas + .iter() + .enumerate() + .map(|(index, name)| tablepro_core::meta::SchemaInfo { + name: (*name).to_owned(), + is_default: index == 0, + is_system: false, + }) + .collect(); + session.catalog().commit(listed, Vec::new()); + let active = ActiveSession::new(); + active.replace(session); + let settings = MemorySettings::new(); + let editor = SqlEditor::new(SqlEditorInit { + initial_query: Some("SELECT 1".to_owned()), + settings: settings.get().clone(), + history: None, + tasks: runtime.tasks(), + session: active.clone(), + hold: Rc::new(tablepro_session::runtime::CountingHold::new()), + close_timeout: Duration::from_secs(5), + snippets: crate::test_support::builtin_snippets_only(), + }); + Harness { + editor, + fake, + history: None, + active, + settings, + runtime, + } +} + +fn postgres_dialect() -> Arc { + Arc::new(tablepro_driver_postgres::PgDialect::new( + &tablepro_core::ServerInfo::new("PostgreSQL", "18.0", 18, 0, 0), + )) +} + +fn scope_shown(harness: &Harness) -> Option { + let imp = harness.editor.imp(); + imp.scope_box.is_visible().then(|| imp.scope_label.label().to_string()) +} + +#[gtk4::test] +fn the_bar_names_the_database_and_the_schema_names_resolve_in() { + let harness = scoped_editor(postgres_dialect(), &["public", "audit"]); + + wait_until(WAIT, || scope_shown(&harness).as_deref() == Some("shop.public")) + .unwrap_or_else(|_| panic!("the bar says {:?}", scope_shown(&harness))); + assert_eq!( + harness.editor.imp().scope_box.tooltip_text().as_deref(), + Some("shop, schema public") + ); +} + +/// Where the server's databases are the catalog's schemas, the default +/// schema is the database and adds nothing. +#[gtk4::test] +fn the_bar_names_a_database_that_is_its_own_schema_once() { + let harness = scoped_editor(Arc::new(tablepro_driver_mongodb::MongoDialect), &["shop", "analytics"]); + + crate::test_support::drain_main_context(); + assert_eq!(scope_shown(&harness).as_deref(), Some("shop")); +} + +#[gtk4::test] +fn an_engine_of_one_database_names_none() { + let harness = scoped_editor(Arc::new(tablepro_test_support::FakeDialect), &["main"]); + + crate::test_support::drain_main_context(); + assert_eq!(scope_shown(&harness), None); +} + +/// A `USE` in the script moves the session, and the bar goes with it. +#[gtk4::test] +fn the_bar_follows_a_use_the_tab_ran() { + let harness = scoped_editor(postgres_dialect(), &["public"]); + wait_until(WAIT, || scope_shown(&harness).as_deref() == Some("shop.public")).expect("the scope"); + harness.fake.set_database(Some("analytics")); + + run_and_wait(&harness); + + assert_eq!(scope_shown(&harness).as_deref(), Some("analytics")); +} diff --git a/linux/crates/core/src/dialect/database_layout.rs b/linux/crates/core/src/dialect/database_layout.rs new file mode 100644 index 0000000000..33067c5843 --- /dev/null +++ b/linux/crates/core/src/dialect/database_layout.rs @@ -0,0 +1,13 @@ +/// How the server's databases stand to the catalog a connection reads. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum DatabaseLayout { + /// A connection reaches one database: a file, or numbered databases + /// of keys. + Single, + /// The server's databases are the schemas the catalog lists, so one + /// connection reads every one of them. + AsSchemas, + /// Each database holds schemas of its own, and a connection's catalog + /// describes only the database it opened. + WithSchemas, +} diff --git a/linux/crates/core/src/dialect/mod.rs b/linux/crates/core/src/dialect/mod.rs index 08c8769e7b..92eed0f063 100644 --- a/linux/crates/core/src/dialect/mod.rs +++ b/linux/crates/core/src/dialect/mod.rs @@ -3,6 +3,7 @@ mod bind_error; mod bind_target; +mod database_layout; mod dialect_capabilities; mod insert_batch_limits; mod keyset_direction; @@ -16,6 +17,7 @@ mod test_dialect; pub use bind_error::BindError; pub use bind_target::BindTarget; +pub use database_layout::DatabaseLayout; pub use dialect_capabilities::{DialectCapabilities, InsertConflict}; pub use insert_batch_limits::InsertBatchLimits; pub use keyset_direction::KeysetDirection; diff --git a/linux/crates/core/src/dialect/sql_dialect.rs b/linux/crates/core/src/dialect/sql_dialect.rs index 79b7e00948..b02f232434 100644 --- a/linux/crates/core/src/dialect/sql_dialect.rs +++ b/linux/crates/core/src/dialect/sql_dialect.rs @@ -6,8 +6,8 @@ use crate::sql_syntax::{ExpressionSyntaxError, HistoryRecording, SqlGrammar, Typ use crate::value::Value; use super::{ - BindError, BindTarget, DialectCapabilities, InsertBatchLimits, KeysetDirection, LikeCase, LikeForm, LiteralError, - Placeholder, TablePaging, + BindError, BindTarget, DatabaseLayout, DialectCapabilities, InsertBatchLimits, KeysetDirection, LikeCase, LikeForm, + LiteralError, Placeholder, TablePaging, }; /// How one engine spells SQL. @@ -31,6 +31,10 @@ pub trait SqlDialect: Send + Sync + std::fmt::Debug { /// whatever it contains. fn quote_identifier(&self, name: &str) -> String; + fn database_layout(&self) -> DatabaseLayout { + DatabaseLayout::Single + } + /// Bind one value, giving back the SQL that names it. /// /// `ordinal` is the number of parameters the statement already diff --git a/linux/crates/core/src/editor_session.rs b/linux/crates/core/src/editor_session.rs index 20660f092d..5e29af3596 100644 --- a/linux/crates/core/src/editor_session.rs +++ b/linux/crates/core/src/editor_session.rs @@ -71,5 +71,12 @@ pub trait EditorSession: Send + Sync { async fn transaction_state(&self, options: CallOptions) -> Result; + /// The database an unqualified name resolves to now, as the server + /// last said. `None` where the driver does not follow it: the session + /// is still in the database it opened in. + fn database(&self) -> Option { + None + } + async fn close(&self); } diff --git a/linux/crates/core/src/sql_syntax/catalog_change.rs b/linux/crates/core/src/sql_syntax/catalog_change.rs index 3a7806743b..2fc62739eb 100644 --- a/linux/crates/core/src/sql_syntax/catalog_change.rs +++ b/linux/crates/core/src/sql_syntax/catalog_change.rs @@ -1,5 +1,6 @@ use super::SqlGrammar; use super::script::{ScriptPlan, ScriptTokenKind}; +use super::statement_tokens::lex_statements; /// Words that add, rename or remove what the sidebar lists. They count /// anywhere in a statement: a SQL Server batch needs no `;` between its @@ -27,6 +28,17 @@ pub fn changes_catalog(text: &str, grammar: SqlGrammar) -> bool { ScriptPlan::server_readings(text, grammar).is_none_or(|mut plans| plans.any(|plan| plan_changes(text, &plan))) } +/// Whether running `text` can leave the session in another database. A +/// `USE` anywhere counts, since a SQL Server batch needs no `;` between +/// its statements; an index hint's `USE` only costs a question. +pub fn may_change_database(text: &str, grammar: SqlGrammar) -> bool { + lex_statements(text, grammar) + .statements + .iter() + .flatten() + .any(|token| token.is_word("USE")) +} + fn plan_changes(text: &str, plan: &ScriptPlan) -> bool { let is = |word: &str, set: &[&str]| set.iter().any(|verb| word.eq_ignore_ascii_case(verb)); plan.statements().iter().any(|statement| { @@ -48,6 +60,22 @@ fn plan_changes(text: &str, plan: &ScriptPlan) -> bool { mod tests { use super::*; + #[test] + fn a_use_can_change_the_database() { + for grammar in SqlGrammar::SQL { + for sql in [ + "USE analytics", + "SELECT 1; use `a b`", + "SELECT 1 USE analytics SELECT 2", + ] { + assert!(may_change_database(sql, grammar), "{grammar:?}: {sql}"); + } + for sql in ["SELECT 'USE x'", "-- USE x\nSELECT 1", "SELECT used FROM t"] { + assert!(!may_change_database(sql, grammar), "{grammar:?}: {sql}"); + } + } + } + #[test] fn ddl_changes_the_catalog_in_every_grammar() { for grammar in SqlGrammar::SQL { diff --git a/linux/crates/core/src/sql_syntax/mod.rs b/linux/crates/core/src/sql_syntax/mod.rs index 50e8f531d4..a3e14a8a91 100644 --- a/linux/crates/core/src/sql_syntax/mod.rs +++ b/linux/crates/core/src/sql_syntax/mod.rs @@ -17,7 +17,7 @@ mod statement_access; mod statement_tokens; pub use builtin_snippet::{BuiltinSnippet, builtin_snippets}; -pub use catalog_change::changes_catalog; +pub use catalog_change::{changes_catalog, may_change_database}; pub use credential_rules::CredentialRules; pub use errors::{ExpressionSyntaxError, TypeSyntaxError}; pub use grammar::SqlGrammar; diff --git a/linux/crates/drivers/cassandra/src/dialect.rs b/linux/crates/drivers/cassandra/src/dialect.rs index ced163ac1f..6c67351359 100644 --- a/linux/crates/drivers/cassandra/src/dialect.rs +++ b/linux/crates/drivers/cassandra/src/dialect.rs @@ -1,8 +1,8 @@ use tablepro_core::column::{ColumnInfo, ColumnType, SqlExpression}; use tablepro_core::ddl::DdlDialect; use tablepro_core::dialect::{ - BindError, BindTarget, DialectCapabilities, InsertBatchLimits, InsertConflict, KeysetDirection, LikeForm, - LiteralError, Placeholder, SqlDialect, TablePaging, + BindError, BindTarget, DatabaseLayout, DialectCapabilities, InsertBatchLimits, InsertConflict, KeysetDirection, + LikeForm, LiteralError, Placeholder, SqlDialect, TablePaging, }; use tablepro_core::filter::{Combinator, FilterOp}; use tablepro_core::meta::{EngineRowId, EngineRowIdPart, TableRef}; @@ -141,6 +141,10 @@ const DURATION: &[FilterOp] = &[FilterOp::Eq]; const CASSANDRA_IN_OUTSIDE_KEY: (u32, u32) = (5, 0); impl SqlDialect for CqlDialect { + fn database_layout(&self) -> DatabaseLayout { + DatabaseLayout::AsSchemas + } + fn capabilities(&self) -> DialectCapabilities { match self.conditional_writes { true => DialectCapabilities { diff --git a/linux/crates/drivers/cassandra/src/editor.rs b/linux/crates/drivers/cassandra/src/editor.rs index 5292545157..97da762cd6 100644 --- a/linux/crates/drivers/cassandra/src/editor.rs +++ b/linux/crates/drivers/cassandra/src/editor.rs @@ -74,6 +74,12 @@ impl EditorSession for CqlEditorSession { self.id } + /// The driver moves the session itself when a `USE` answers. + fn database(&self) -> Option { + let link = self.link.get().ok()?; + link.session().get_keyspace().map(|keyspace| keyspace.to_string()) + } + fn state(&self) -> EditorSessionState { match self.state.load(Ordering::SeqCst) { READY => EditorSessionState::Ready, diff --git a/linux/crates/drivers/clickhouse/src/dialect.rs b/linux/crates/drivers/clickhouse/src/dialect.rs index e2ef2b7b74..9e017e0cd4 100644 --- a/linux/crates/drivers/clickhouse/src/dialect.rs +++ b/linux/crates/drivers/clickhouse/src/dialect.rs @@ -4,8 +4,8 @@ use tablepro_core::column::{ }; use tablepro_core::ddl::DdlDialect; use tablepro_core::dialect::{ - BindError, BindTarget, DialectCapabilities, InsertBatchLimits, InsertConflict, KeysetDirection, LikeCase, LikeForm, - LiteralError, Placeholder, SqlDialect, + BindError, BindTarget, DatabaseLayout, DialectCapabilities, InsertBatchLimits, InsertConflict, KeysetDirection, + LikeCase, LikeForm, LiteralError, Placeholder, SqlDialect, }; use tablepro_core::export::value_to_text; use tablepro_core::meta::{EngineRowId, EngineRowIdPart, TableRef}; @@ -53,6 +53,10 @@ impl ClickHouseDialect { } impl SqlDialect for ClickHouseDialect { + fn database_layout(&self) -> DatabaseLayout { + DatabaseLayout::AsSchemas + } + fn capabilities(&self) -> DialectCapabilities { // A mutation reports nothing useful about how many rows it // touched, so a write proves itself with a probe instead. diff --git a/linux/crates/drivers/clickhouse/src/editor_session.rs b/linux/crates/drivers/clickhouse/src/editor_session.rs index 0a3b2ccf21..4d00aacd94 100644 --- a/linux/crates/drivers/clickhouse/src/editor_session.rs +++ b/linux/crates/drivers/clickhouse/src/editor_session.rs @@ -76,6 +76,12 @@ impl EditorSession for ClickHouseEditorSession { self.id } + /// Every request names the database, which a `USE` in the session + /// does not override. + fn database(&self) -> Option { + self.session.database().map(str::to_owned) + } + fn state(&self) -> EditorSessionState { self.status.state() } diff --git a/linux/crates/drivers/mongodb/src/dialect.rs b/linux/crates/drivers/mongodb/src/dialect.rs index 8bcb36fda9..9fa2d50bd4 100644 --- a/linux/crates/drivers/mongodb/src/dialect.rs +++ b/linux/crates/drivers/mongodb/src/dialect.rs @@ -1,7 +1,7 @@ use tablepro_core::column::{ColumnType, SqlExpression}; use tablepro_core::dialect::{ - BindError, BindTarget, DialectCapabilities, InsertBatchLimits, InsertConflict, KeysetDirection, LiteralError, - Placeholder, SqlDialect, + BindError, BindTarget, DatabaseLayout, DialectCapabilities, InsertBatchLimits, InsertConflict, KeysetDirection, + LiteralError, Placeholder, SqlDialect, }; use tablepro_core::meta::{EngineRowId, EngineRowIdPart}; use tablepro_core::sql_syntax::{ExpressionSyntaxError, HistoryRecording, SqlGrammar, TypeSyntaxError}; @@ -17,6 +17,10 @@ use tablepro_core::value::Value; pub struct MongoDialect; impl SqlDialect for MongoDialect { + fn database_layout(&self) -> DatabaseLayout { + DatabaseLayout::AsSchemas + } + /// A write reports how many documents it matched, and `_id` is /// unique, so a count proves a save touched the document it meant to, /// and an insert with an `_id` already taken fails with E11000. diff --git a/linux/crates/drivers/mongodb/src/editor_session.rs b/linux/crates/drivers/mongodb/src/editor_session.rs index 96f6946fd0..be9f35e515 100644 --- a/linux/crates/drivers/mongodb/src/editor_session.rs +++ b/linux/crates/drivers/mongodb/src/editor_session.rs @@ -64,6 +64,10 @@ impl EditorSession for MongoEditorSession { self.id } + fn database(&self) -> Option { + Some(self.database.lock().unwrap_or_else(PoisonError::into_inner).clone()) + } + fn state(&self) -> EditorSessionState { self.status.state() } diff --git a/linux/crates/drivers/mssql/src/dialect.rs b/linux/crates/drivers/mssql/src/dialect.rs index 93c83b3324..3d4e638b59 100644 --- a/linux/crates/drivers/mssql/src/dialect.rs +++ b/linux/crates/drivers/mssql/src/dialect.rs @@ -4,8 +4,8 @@ use tablepro_core::column::{ }; use tablepro_core::ddl::DdlDialect; use tablepro_core::dialect::{ - BindError, BindTarget, DialectCapabilities, InsertBatchLimits, InsertConflict, KeysetDirection, LikeCase, LikeForm, - LiteralError, Placeholder, SqlDialect, + BindError, BindTarget, DatabaseLayout, DialectCapabilities, InsertBatchLimits, InsertConflict, KeysetDirection, + LikeCase, LikeForm, LiteralError, Placeholder, SqlDialect, }; use tablepro_core::meta::{EngineRowId, EngineRowIdPart}; use tablepro_core::sql_syntax::{ @@ -42,6 +42,10 @@ impl MssqlDialect { } impl SqlDialect for MssqlDialect { + fn database_layout(&self) -> DatabaseLayout { + DatabaseLayout::WithSchemas + } + /// SQL Server adds the rows its triggers touch to a statement's /// count, so a write proves itself with a probe instead. fn capabilities(&self) -> DialectCapabilities { diff --git a/linux/crates/drivers/mssql/src/editor_run.rs b/linux/crates/drivers/mssql/src/editor_run.rs index 4223ab45a0..cbcaf2f7c1 100644 --- a/linux/crates/drivers/mssql/src/editor_run.rs +++ b/linux/crates/drivers/mssql/src/editor_run.rs @@ -247,6 +247,16 @@ pub(crate) async fn transaction_state(client: &mut MssqlClient) -> Result Result, DriverError> { + let stream = client.simple_query("SELECT DB_NAME()").await.map_err(map_error)?; + let row = stream.into_row().await.map_err(map_error)?; + Ok(row.and_then(|row| match decode_row(&row).into_iter().next() { + Some(Value::Text(name)) => Some(name), + _ => None, + })) +} + /// XACT_STATE is -1 when an error has doomed the transaction: it can /// only be rolled back. fn state_of(transaction_count: Option, xact_state: Option) -> TransactionState { diff --git a/linux/crates/drivers/mssql/src/editor_session.rs b/linux/crates/drivers/mssql/src/editor_session.rs index 4ec3c2850e..d7145794de 100644 --- a/linux/crates/drivers/mssql/src/editor_session.rs +++ b/linux/crates/drivers/mssql/src/editor_session.rs @@ -3,6 +3,7 @@ use std::sync::{Arc, Mutex, PoisonError}; use std::time::Duration; use async_trait::async_trait; +use tablepro_core::sql_syntax::{SqlGrammar, may_change_database}; use tablepro_core::{ CallOptions, DriverError, EditorSession, EditorSessionId, EditorSessionOptions, EditorSessionState, LossPhase, ResultSink, RunEnd, RunOptions, RunSummary, StopReason, StopRequest, TimeoutPhase, TransactionState, UserSql, @@ -11,7 +12,7 @@ use tokio::sync::OwnedMutexGuard; use tokio::time::Instant; use tokio_util::sync::CancellationToken; -use crate::editor_run::{EditorRun, ScriptOutcome, transaction_state}; +use crate::editor_run::{EditorRun, ScriptOutcome, current_database, transaction_state}; use crate::endpoint::MssqlEndpoint; use crate::errors::ends_connection; use crate::in_band_request::StopPolicy; @@ -38,6 +39,8 @@ struct Shared { lost: AtomicBool, /// The running statement's stop, so closing the tab stops it. current: Mutex>, + /// Where a `USE` left the session, once one ran. + database: Mutex>, statement_timeout: Option, policy: StopPolicy, cancel_grace: Duration, @@ -59,6 +62,7 @@ impl MssqlEditorSession { running: AtomicBool::new(false), lost: AtomicBool::new(false), current: Mutex::new(None), + database: Mutex::new(None), statement_timeout: options.statement_timeout, policy: StopPolicy::within(liveness.cancel_grace), cancel_grace: liveness.cancel_grace, @@ -103,7 +107,8 @@ impl Shared { let supervised = self.supervise(link, &request, &sql, &options, sink.as_mut()).await; let (end, transaction_state, keep) = match supervised { Some(Ok(outcome)) if outcome.reusable => { - let (state, keep) = self.state_after_run(link).await; + let follow = may_change_database(sql.as_str(), SqlGrammar::MsSql); + let (state, keep) = self.state_after_run(link, follow).await; (Ok(outcome.end), state, keep) } // The run ended, but somewhere the connection cannot go on @@ -175,7 +180,15 @@ impl Shared { /// Where the transaction stands after a run, within the cancel grace, /// and unknown rather than guessed when the answer does not come. - async fn state_after_run(&self, link: &mut MssqlLink) -> (TransactionState, bool) { + async fn state_after_run(&self, link: &mut MssqlLink, follow_database: bool) -> (TransactionState, bool) { + if follow_database { + match tokio::time::timeout(self.cancel_grace, current_database(&mut link.client)).await { + Ok(Ok(current)) => *self.database.lock().unwrap_or_else(PoisonError::into_inner) = current, + // A session in a database nothing knows ends rather than + // run the next script in the wrong one. + Ok(Err(_)) | Err(_) => return (TransactionState::Unknown, false), + } + } match tokio::time::timeout(self.cancel_grace, transaction_state(&mut link.client)).await { Ok(Ok(state)) => (state, true), Ok(Err(error)) => (TransactionState::Unknown, !ends_connection(&error)), @@ -198,6 +211,14 @@ impl EditorSession for MssqlEditorSession { self.id } + fn database(&self) -> Option { + self.shared + .database + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } + fn state(&self) -> EditorSessionState { self.shared.state() } diff --git a/linux/crates/drivers/mssql/tests/integration/sessions.rs b/linux/crates/drivers/mssql/tests/integration/sessions.rs index 01874bd9ae..ae1e0008a0 100644 --- a/linux/crates/drivers/mssql/tests/integration/sessions.rs +++ b/linux/crates/drivers/mssql/tests/integration/sessions.rs @@ -17,6 +17,11 @@ async fn use_database_persists_across_runs() { let database = run_ok(session.as_ref(), "SELECT DB_NAME()").await.rows(); assert_eq!(database, vec![vec![Value::Text("tempdb".to_owned())]]); + assert_eq!( + session.database().as_deref(), + Some("tempdb"), + "where the session says it is" + ); } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] diff --git a/linux/crates/drivers/mysql/src/dialect.rs b/linux/crates/drivers/mysql/src/dialect.rs index 4a8e94b0ef..89cd87eea7 100644 --- a/linux/crates/drivers/mysql/src/dialect.rs +++ b/linux/crates/drivers/mysql/src/dialect.rs @@ -5,8 +5,8 @@ use tablepro_core::column::{ }; use tablepro_core::ddl::DdlDialect; use tablepro_core::dialect::{ - BindError, BindTarget, DialectCapabilities, InsertConflict, KeysetDirection, LikeCase, LikeForm, LiteralError, - Placeholder, SqlDialect, + BindError, BindTarget, DatabaseLayout, DialectCapabilities, InsertConflict, KeysetDirection, LikeCase, LikeForm, + LiteralError, Placeholder, SqlDialect, }; use tablepro_core::export::value_to_text; use tablepro_core::meta::{EngineRowId, EngineRowIdPart}; @@ -61,6 +61,10 @@ impl MySqlDialect { } impl SqlDialect for MySqlDialect { + fn database_layout(&self) -> DatabaseLayout { + DatabaseLayout::AsSchemas + } + fn capabilities(&self) -> DialectCapabilities { // CLIENT_FOUND_ROWS is on, so an UPDATE reports the rows it // matched, including ones it left unchanged. diff --git a/linux/crates/drivers/mysql/src/editor_session.rs b/linux/crates/drivers/mysql/src/editor_session.rs index 7907f61351..d2a5587a7a 100644 --- a/linux/crates/drivers/mysql/src/editor_session.rs +++ b/linux/crates/drivers/mysql/src/editor_session.rs @@ -1,22 +1,26 @@ -use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; use std::time::Duration; use async_trait::async_trait; use sqlx::mysql::MySqlConnection; use sqlx::{AssertSqlSafe, Connection as _, Executor as _}; +use tablepro_core::sql_syntax::{SqlGrammar, may_change_database}; use tablepro_core::{ CallOptions, DriverError, EditorSession, EditorSessionId, EditorSessionOptions, EditorSessionState, LivenessPolicy, - LossPhase, ResultSink, RowLimit, RunEnd, RunOptions, RunSummary, TimeoutPhase, TransactionState, UserSql, + LossPhase, ResultSink, RowLimit, RunEnd, RunOptions, RunSummary, TimeoutPhase, TransactionState, UserSql, Value, }; use tokio::sync::OwnedMutexGuard; use tokio::time::Instant; use tokio_util::sync::CancellationToken; use crate::cancel::ControlLink; +use crate::decode::decode_row; use crate::editor_run::{RunOutcome, run_script}; use crate::errors::map_error; use crate::flavour::MySqlFlavour; +use crate::mysql_timestamp_mode::MySqlTimestampMode; +use crate::mysql_wire_format::MySqlWireFormat; use crate::physical::{MysqlEndpoint, SessionKind}; use crate::session_statements::{SessionLimits, select_limit}; use crate::transaction_probe::TransactionProbe; @@ -47,6 +51,8 @@ struct Shared { flavour: MySqlFlavour, control: ControlLink, probe: TransactionProbe, + /// Where a `USE` left the session, once one ran. + database: Mutex>, running: AtomicBool, lost: AtomicBool, /// Cancelled when the session closes, so a running statement stops. @@ -85,6 +91,7 @@ impl MysqlEditorSession { flavour: physical.flavour, control: ControlLink::new(endpoint), probe: TransactionProbe::new(physical.flavour), + database: Mutex::new(None), running: AtomicBool::new(false), lost: AtomicBool::new(false), closing: CancellationToken::new(), @@ -124,8 +131,16 @@ impl Shared { let started = Instant::now(); self.running.store(true, Ordering::SeqCst); let supervised = self.supervise(held, &sql, &options, sink.as_mut()).await; + // A session in a database nothing knows would run the next script + // in the wrong one, so it ends instead. + let placed = match &supervised { + Ok((_, true)) if may_change_database(sql.as_str(), SqlGrammar::MySql) => { + self.follow_database(&mut held.connection).await + } + _ => true, + }; let transaction_state = match &supervised { - Ok((_, true)) => { + Ok((_, true)) if placed => { let asking = self.probe.state(&mut held.connection); tokio::time::timeout(self.liveness.cancel_grace, asking) .await @@ -135,8 +150,8 @@ impl Shared { }; self.running.store(false, Ordering::SeqCst); let end = match supervised { - Ok((end, true)) => end, - Ok((end, false)) => { + Ok((end, true)) if placed => end, + Ok((end, _)) => { self.lost.store(true, Ordering::SeqCst); *guard = None; end @@ -154,6 +169,21 @@ impl Shared { }) } + /// Ask where a `USE` left the session. False when the answer did not + /// come. + async fn follow_database(&self, connection: &mut MySqlConnection) -> bool { + let asking = connection.fetch_one(sqlx::raw_sql("SELECT DATABASE()")); + let Ok(Ok(row)) = tokio::time::timeout(self.liveness.cancel_grace, asking).await else { + return false; + }; + let current = match decode_row(&row, MySqlWireFormat::Text, MySqlTimestampMode::SessionLocal).first() { + Some(Value::Text(name)) => Some(name.clone()), + _ => None, + }; + *self.database.lock().unwrap_or_else(PoisonError::into_inner) = current; + true + } + /// Run the script, stopping it on a cancel, at the deadline, when /// the session closes or when the sink stops reading. A stop is KILL /// QUERY on a connection of the session's own; the statement then @@ -317,6 +347,14 @@ impl EditorSession for MysqlEditorSession { outcome } + fn database(&self) -> Option { + self.shared + .database + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } + async fn transaction_state(&self, options: CallOptions) -> Result { let shared = &self.shared; let asking = async { diff --git a/linux/crates/drivers/mysql/tests/integration/editor_session.rs b/linux/crates/drivers/mysql/tests/integration/editor_session.rs index 236d599ca2..6701d7132d 100644 --- a/linux/crates/drivers/mysql/tests/integration/editor_session.rs +++ b/linux/crates/drivers/mysql/tests/integration/editor_session.rs @@ -259,3 +259,32 @@ async fn a_deadline_reaches_a_run_still_setting_its_row_limit() { editor.close().await; connection.close().await.expect("a close"); } + +/// The session says where a `USE` left it, so the editor bar can follow. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn a_use_moves_the_session_and_it_says_where() { + for flavour in FLAVOURS { + let fixture = start(flavour).await; + let root = connect_root(&fixture).await; + let grant = format!( + "GRANT ALL ON analytics.* TO '{}'@'%'", + fixture.password_credentials().username + ); + setup( + root.as_ref(), + &["CREATE DATABASE IF NOT EXISTS analytics", grant.as_str()], + ) + .await; + root.close().await.expect("a close"); + let connection = connect(&fixture, false).await; + let editor = session(connection.as_ref()).await; + + run_ok(editor.as_ref(), "SELECT 1").await; + assert_eq!(editor.database(), None, "{flavour:?}: no USE ran"); + run_ok(editor.as_ref(), "USE analytics").await; + assert_eq!(editor.database().as_deref(), Some("analytics"), "{flavour:?}"); + editor.close().await; + connection.close().await.expect("a close"); + } +} diff --git a/linux/crates/drivers/postgres/src/dialect.rs b/linux/crates/drivers/postgres/src/dialect.rs index 981432e6f6..8ecb1469be 100644 --- a/linux/crates/drivers/postgres/src/dialect.rs +++ b/linux/crates/drivers/postgres/src/dialect.rs @@ -6,7 +6,8 @@ use tablepro_core::column::{ }; use tablepro_core::ddl::DdlDialect; use tablepro_core::dialect::{ - BindError, BindTarget, DialectCapabilities, InsertConflict, KeysetDirection, LiteralError, Placeholder, SqlDialect, + BindError, BindTarget, DatabaseLayout, DialectCapabilities, InsertConflict, KeysetDirection, LiteralError, + Placeholder, SqlDialect, }; use tablepro_core::export::value_to_text; use tablepro_core::meta::{EngineRowId, EngineRowIdPart}; @@ -41,6 +42,10 @@ impl PgDialect { } impl SqlDialect for PgDialect { + fn database_layout(&self) -> DatabaseLayout { + DatabaseLayout::WithSchemas + } + fn capabilities(&self) -> DialectCapabilities { DialectCapabilities { exact_row_counts: true, diff --git a/linux/crates/test-support/src/fake_editor_session.rs b/linux/crates/test-support/src/fake_editor_session.rs index b53d4666f8..1dc4ddfee8 100644 --- a/linux/crates/test-support/src/fake_editor_session.rs +++ b/linux/crates/test-support/src/fake_editor_session.rs @@ -25,6 +25,7 @@ pub struct FakeEditorSession { texts: Mutex>, state: Mutex, transaction_state: Mutex, + database: Mutex>, close_behaviour: CloseBehaviour, closes: AtomicUsize, closed: AtomicBool, @@ -44,6 +45,7 @@ impl FakeEditorSession { texts: Mutex::new(Vec::new()), state: Mutex::new(EditorSessionState::Ready), transaction_state: Mutex::new(TransactionState::Idle), + database: Mutex::new(None), close_behaviour, closes: AtomicUsize::new(0), closed: AtomicBool::new(false), @@ -75,6 +77,11 @@ impl FakeEditorSession { *lock(&self.transaction_state) = state; } + /// Where the session says it is, as after a `USE`. + pub fn set_database(&self, database: Option<&str>) { + *lock(&self.database) = database.map(str::to_owned); + } + /// The connection goes while no run is on it, as when the server /// ends a session left idle in a transaction. pub fn lose(&self) { @@ -188,6 +195,10 @@ impl EditorSession for FakeEditorSession { Ok(*lock(&self.transaction_state)) } + fn database(&self) -> Option { + lock(&self.database).clone() + } + async fn close(&self) { let first = self.closes.fetch_add(1, Ordering::SeqCst) == 0; self.closing.cancel(); diff --git a/linux/data/resources/ui/sql-editor.ui b/linux/data/resources/ui/sql-editor.ui index 17eba7d890..f520d2d01d 100644 --- a/linux/data/resources/ui/sql-editor.ui +++ b/linux/data/resources/ui/sql-editor.ui @@ -92,6 +92,29 @@ 8 8 8 + + + false + 6 + 4 + label + + + + drive-harddisk-symbolic + presentation + + + + + middle + presentation + + + + true diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index a3af478f42..a58c0d8161 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -354,6 +354,7 @@ crates/app/src/ui/editor/sidebar_policy.rs crates/app/src/ui/editor/significant_tokens.rs crates/app/src/ui/editor/sql_file.rs crates/app/src/ui/editor/statement_cursor.rs +crates/app/src/ui/editor/tab_scope.rs crates/app/src/ui/editor/tests.rs crates/app/src/ui/editor/transaction_dirty.rs crates/app/src/ui/editor/vim_mode.rs From 8f67259d52fc3ed08a2e4b90442d795d1d9c4acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Fri, 30 Oct 2026 20:52:31 +0700 Subject: [PATCH 25/85] fix: confirm more destructive SQL and fix five driver bugs (#3907) --- apple/Packages/TableProCore/Package.swift | 5 + .../Data/BigtableRow.swift | 8 +- .../Wire/BigtableStatement.swift | 31 ++--- .../R2SQLResponseDecoder.swift | 3 +- .../R2SQLURLSessionTransport.swift | 11 ++ .../JavaScript/MongoStatementClassifier.swift | 7 +- .../SQLUnfilteredWrite.swift | 100 ++++++++++------ .../SQLUnterminatedStatements.swift | 3 + .../TeradataConnection.swift | 49 ++++++-- .../Wire/WeaviateTransport.swift | 11 ++ .../BigtableRowTests.swift | 2 + .../BigtableStatementTests.swift | 17 +++ .../MongoStatementClassifierTests.swift | 17 +++ .../SQLUnfilteredWriteTests.swift | 55 +++++++++ .../SQLUnterminatedStatementsTests.swift | 12 ++ .../TeradataConnectionLossTests.swift | 77 +++++++++++++ .../BigQueryPluginDriver.swift | 1 + .../ClickHousePlugin.swift | 9 +- .../ClickHousePluginDriver+Http.swift | 2 +- .../ClickHouseHTTPStub.swift | 6 + .../ClickHouseRedirectTests.swift | 21 ++++ .../D1HttpClient.swift | 4 +- .../DuckDBDriverPlugin/DuckDBAccessMode.swift | 19 ++-- .../DuckDBDriverPlugin/DuckDBFileKinds.swift | 30 ++++- .../ElasticsearchConnection.swift | 13 ++- .../EtcdDriverPlugin/EtcdHttpClient.swift | 19 ++-- .../LibSQLDriverPlugin/HranaHttpClient.swift | 4 +- .../PluginRedirectRefusingDelegate.swift | 15 +++ .../SnowflakeConnection.swift | 8 +- .../SpannerPluginDriver.swift | 2 +- .../SurrealRPCClient.swift | 16 ++- .../TeradataAsyncConnection.swift | 3 + .../TeradataDriverPlugin/TeradataPlugin.swift | 3 +- .../TeradataPluginDriver+Editing.swift | 7 ++ .../TypesenseConnection.swift | 13 ++- .../DatabaseManager+Verification.swift | 3 +- .../Core/Utilities/SQL/QueryClassifier.swift | 73 +++++++++++- apple/TableProMobile/project.yml | 1 + .../ConnectionVerificationTests.swift | 13 +++ .../QueryClassifierCreateOrReplaceTests.swift | 56 +++++++++ .../SQL/QueryClassifierHardeningTests.swift | 29 +++++ ...QueryClassifierUnfilteredDeleteTests.swift | 40 +++++++ apple/changelog.d/3907.fixed.1.md | 1 + apple/changelog.d/3907.fixed.2.md | 1 + apple/changelog.d/3907.fixed.3.md | 1 + apple/changelog.d/3907.fixed.4.md | 1 + apple/changelog.d/3907.fixed.5.md | 1 + apple/changelog.d/3907.fixed.6.md | 1 + apple/changelog.d/3907.fixed.7.md | 1 + apple/changelog.d/3907.fixed.md | 1 + apple/project.yml | 1 + .../probes/check-duckdb-access-mode.sh | 107 ++++++++++++++++++ 52 files changed, 822 insertions(+), 112 deletions(-) create mode 100644 apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataConnectionLossTests.swift create mode 100644 apple/Plugins/ClickHouseDriverTests/ClickHouseRedirectTests.swift create mode 100644 apple/Plugins/Shared/PluginRedirectRefusingDelegate.swift create mode 100644 apple/TableProTests/Core/Utilities/SQL/QueryClassifierCreateOrReplaceTests.swift create mode 100644 apple/changelog.d/3907.fixed.1.md create mode 100644 apple/changelog.d/3907.fixed.2.md create mode 100644 apple/changelog.d/3907.fixed.3.md create mode 100644 apple/changelog.d/3907.fixed.4.md create mode 100644 apple/changelog.d/3907.fixed.5.md create mode 100644 apple/changelog.d/3907.fixed.6.md create mode 100644 apple/changelog.d/3907.fixed.7.md create mode 100644 apple/changelog.d/3907.fixed.md create mode 100755 apple/scripts/probes/check-duckdb-access-mode.sh diff --git a/apple/Packages/TableProCore/Package.swift b/apple/Packages/TableProCore/Package.swift index 34aae47217..95b0b558d8 100644 --- a/apple/Packages/TableProCore/Package.swift +++ b/apple/Packages/TableProCore/Package.swift @@ -278,6 +278,11 @@ let package = Package( dependencies: ["TableProSpannerCore"], path: "Tests/TableProSpannerCoreTests" ), + .testTarget( + name: "TableProTeradataCoreTests", + dependencies: ["TableProTeradataCore"], + path: "Tests/TableProTeradataCoreTests" + ), .testTarget( name: "TableProTrinoCoreTests", dependencies: ["TableProTrinoCore"], diff --git a/apple/Packages/TableProCore/Sources/TableProBigtableCore/Data/BigtableRow.swift b/apple/Packages/TableProCore/Sources/TableProBigtableCore/Data/BigtableRow.swift index d96c5be4f0..bc2ce87ab9 100644 --- a/apple/Packages/TableProCore/Sources/TableProBigtableCore/Data/BigtableRow.swift +++ b/apple/Packages/TableProCore/Sources/TableProBigtableCore/Data/BigtableRow.swift @@ -15,14 +15,16 @@ public struct BigtableColumn: Hashable, Sendable, Comparable { family + ":" + BigtableBytes.display(qualifier) } + // By scalar: a qualifier that starts with a combining mark joins the colon into one Character. public init(name: String) throws { - guard let colon = name.firstIndex(of: ":"), colon != name.startIndex else { + guard let colon = name.unicodeScalars.firstIndex(of: ":"), colon != name.startIndex else { throw BigtableError.configuration(String( format: String(localized: "\"%@\" is not a column. Name it as family:qualifier."), name )) } - self.family = String(name[.. Bool { diff --git a/apple/Packages/TableProCore/Sources/TableProBigtableCore/Wire/BigtableStatement.swift b/apple/Packages/TableProCore/Sources/TableProBigtableCore/Wire/BigtableStatement.swift index 08b8966201..c05591c4f0 100644 --- a/apple/Packages/TableProCore/Sources/TableProBigtableCore/Wire/BigtableStatement.swift +++ b/apple/Packages/TableProCore/Sources/TableProBigtableCore/Wire/BigtableStatement.swift @@ -157,7 +157,7 @@ public struct BigtableRequest: Sendable, Equatable { /// Text that would not read back as one bare word goes in JSON quotes. static func token(_ text: String, reserved: String) -> String { - let plain = !text.isEmpty && !text.hasPrefix("\"") && text.unicodeScalars.allSatisfy { scalar in + let plain = !text.isEmpty && text.unicodeScalars.first != "\"" && text.unicodeScalars.allSatisfy { scalar in !CharacterSet.whitespacesAndNewlines.contains(scalar) && !reserved.unicodeScalars.contains(scalar) } if plain { return text } @@ -173,22 +173,23 @@ public struct BigtableRequest: Sendable, Equatable { static let missingTable = BigtableError.configuration(String(localized: "Name the table before the key, as in GET events/user#42.")) } -/// Reads a request line one token at a time. +// Read by scalar, as `token` writes: a key that starts with a combining mark joins the quote or slash before it +// into one Character. private struct RequestScanner { - private var text: Substring + private var text: Substring.UnicodeScalarView private var query: [String: String] = [:] init(_ text: String) { - self.text = Substring(text) + self.text = Substring(text).unicodeScalars } var isAtEnd: Bool { - text.allSatisfy(\.isWhitespace) + text.allSatisfy(\.properties.isWhitespace) } - mutating func word(stoppingAt stop: Character? = nil) -> String? { + mutating func word(stoppingAt stop: Unicode.Scalar? = nil) -> String? { skipSpace() - let word = text.prefix { !$0.isWhitespace && $0 != stop } + let word = text.prefix { !$0.properties.isWhitespace && $0 != stop } guard !word.isEmpty else { return nil } text = text.dropFirst(word.count) return String(word) @@ -196,7 +197,7 @@ private struct RequestScanner { mutating func rest() -> String { let remaining = String(text).trimmingCharacters(in: .whitespacesAndNewlines) - text = "" + text.removeAll() return remaining } @@ -204,7 +205,7 @@ private struct RequestScanner { /// no slash is a table alone, as in `SCAN events`. mutating func target(tableOnly: Bool) throws -> (table: String, key: Data?) { skipSpace() - let head = text.prefix { !$0.isWhitespace && $0 != "\"" && $0 != "?" } + let head = text.prefix { !$0.properties.isWhitespace && $0 != "\"" && $0 != "?" } let table: String if let slash = head.firstIndex(of: "/") { table = String(text[.. Void + ) { + completionHandler(nil) + } + func finish() { guard let task = lock.withLock({ task }) else { return } transport?.unregister(task) diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoStatementClassifier.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoStatementClassifier.swift index 0272332c25..2abb610a77 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoStatementClassifier.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/JavaScript/MongoStatementClassifier.swift @@ -58,11 +58,15 @@ public enum MongoStatementClassifier { // Server-side JavaScript, plus the two stages that write to a collection the statement names. private static let codeExecutionKeys: Set = ["$where", "$function", "$accumulator", "$out", "$merge"] + // The server accepts its own operators from any client, and rewrites `mapReduce` into two of them that run a + // function: `$_internalJsEmit` and `$_internalJsReduce`. Nothing a person writes names one. + private static let internalOperatorPrefix = "$_internal" + private static let codeExecutionCalls: Set = ["mapreduce", "eval"] // For text the grammar rejects, where a false alarm only stops an external client sending code nobody could read. private static let codeExecutionMarkers: [String] = [ - "$where", "$function", "$accumulator", "mapreduce", ".eval(", "$out", "$merge" + "$where", "$function", "$accumulator", "$_internal", "mapreduce", ".eval(", "$out", "$merge" ] private static func statementVerdict( @@ -78,6 +82,7 @@ public enum MongoStatementClassifier { private static func verdict(of statement: MongoStatement) -> MongoStatementVerdict { let reachesCode = !statement.objectKeys.isDisjoint(with: codeExecutionKeys) + || statement.objectKeys.contains { $0.hasPrefix(internalOperatorPrefix) } || statement.calls.contains { call in codeExecutionCalls.contains(call.name.lowercased()) || call.commandName.map { codeExecutionCalls.contains($0.lowercased()) } == true diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnfilteredWrite.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnfilteredWrite.swift index 835d11631c..9582c3d733 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnfilteredWrite.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnfilteredWrite.swift @@ -16,6 +16,8 @@ public enum SQLUnfilteredWrite: Sendable, Equatable { private static let period = UInt16(UnicodeScalar(".").value) + private static let whereLength = "WHERE".utf16.count + private static let servedStatements: Set = ["SELECT", "INSERT", "UPDATE", "DELETE", "MERGE"] // `:where` and `$where` are bind parameters on SQLite. @@ -30,58 +32,65 @@ public enum SQLUnfilteredWrite: Sendable, Equatable { } let depth = tokens[start].depth var found: [SQLUnfilteredWrite] = [] - let verb: Int? + let verbs: [Int] switch first { case "UPDATE", "DELETE": - verb = start + verbs = [start] case "WITH": - // A CTE may be named `delete` or `merge`, and on PostgreSQL its body may itself write. A list that does - // not parse is searched whole, which can only find a write that is not there. - let list = commonTableExpressions(in: tokens, with: start) - for body in list?.bodies ?? [] { + // A CTE may be named `delete` or `merge`, and on PostgreSQL its body may itself write. Every body is read + // whether or not the list around it can be followed. When it cannot, every word at the statement's depth + // is a candidate, which can only find a write that is not there. + for body in commonTableExpressionBodies(in: tokens, depth: depth) { found += writes(inStatement: source.substring(with: body) as NSString, grammar: grammar) } - verb = tokens.indices.dropFirst(list?.main ?? start + 1).first { index in - tokens[index].depth == depth && servedStatements.contains(tokens[index].word ?? "") - } + verbs = mainStatement(in: tokens, with: start).map { [$0] } + ?? tokens.indices.dropFirst(start + 1).filter { tokens[$0].depth == depth } case "ALTER": // ClickHouse writes rows with `ALTER TABLE t UPDATE ... WHERE` and `ALTER TABLE t DELETE WHERE`. guard tokens.indices.contains(start + 1), tokens[start + 1].word == "TABLE" else { return [] } - verb = tokens.indices.dropFirst(start + 2).first { index in + let verb = tokens.indices.dropFirst(start + 2).first { index in let word = tokens[index].word return tokens[index].depth == depth && (word == "UPDATE" || word == "DELETE") && tokens[index - 1].word != "ON" } + verbs = verb.map { [$0] } ?? [] default: - verb = nil + verbs = [] } - if let verb, let write = write(at: verb, in: tokens), !hasOwnWhere(tokens, depth: depth, in: source) { - found.append(write) + let writes = verbs.compactMap { write(at: $0, in: tokens) } + if !writes.isEmpty, !hasOwnWhere(tokens, depth: depth, in: source) { + found += writes } return found } - private static func commonTableExpressions( - in tokens: [TSQLTokens.Token], - with start: Int - ) -> (bodies: [NSRange], main: Int)? { - let depth = tokens[start].depth + private static let bodyOpeners: Set = ["AS", "MATERIALIZED"] + + // Found by scanning rather than by walking the list, so a body is read when a name is several tokens, as + // `U&"gone"` is, or a SEARCH or CYCLE clause follows an earlier body. + private static func commonTableExpressionBodies(in tokens: [TSQLTokens.Token], depth: Int) -> [NSRange] { + tokens.indices.dropFirst().compactMap { index in + guard tokens[index].depth == depth, bodyOpeners.contains(tokens[index - 1].word ?? ""), + let close = close(of: index, in: tokens) + else { return nil } + let inside = tokens[index].location + 1 + return NSRange(location: inside, length: tokens[close].location - inside) + } + } + + // Nil when no statement begins right after the last body: a name T-SQL reads as several words where the engine + // reads one, as at a euro sign, or PostgreSQL's `SEARCH ... SET name` and `CYCLE ... USING name` after a body. + private static func mainStatement(in tokens: [TSQLTokens.Token], with start: Int) -> Int? { var index = start + 1 - var bodies: [NSRange] = [] func word(_ expected: String) -> Bool { guard tokens.indices.contains(index), tokens[index].word == expected else { return false } index += 1 return true } - func parentheses() -> NSRange? { - guard tokens.indices.contains(index), tokens[index].kind == .open, - let close = tokens.indices.dropFirst(index + 1).first(where: { - tokens[$0].kind == .close && tokens[$0].depth == depth - }) - else { return nil } - let inside = tokens[index].location + 1 + func parentheses() -> Bool { + guard let close = close(of: index, in: tokens) else { return false } index = close + 1 - return NSRange(location: inside, length: tokens[close].location - inside) + return true } _ = word("RECURSIVE") while tokens.indices.contains(index) { @@ -90,12 +99,19 @@ public enum SQLUnfilteredWrite: Sendable, Equatable { guard word("AS") else { return nil } _ = word("NOT") _ = word("MATERIALIZED") - guard let body = parentheses() else { return nil } - bodies.append(body) + guard parentheses() else { return nil } guard tokens.indices.contains(index), tokens[index].kind == .comma else { break } index += 1 } - return tokens.indices.contains(index) ? (bodies, index) : nil + guard tokens.indices.contains(index), servedStatements.contains(tokens[index].word ?? "") else { return nil } + return index + } + + private static func close(of open: Int, in tokens: [TSQLTokens.Token]) -> Int? { + guard tokens.indices.contains(open), tokens[open].kind == .open else { return nil } + return tokens.indices.dropFirst(open + 1).first { + tokens[$0].kind == .close && tokens[$0].depth == tokens[open].depth + } } private static func write(at index: Int, in tokens: [TSQLTokens.Token]) -> SQLUnfilteredWrite? { @@ -115,13 +131,23 @@ public enum SQLUnfilteredWrite: Sendable, Equatable { private static func hasOwnWhere(_ tokens: [TSQLTokens.Token], depth: Int, in source: NSString) -> Bool { tokens.indices.contains { index in - guard tokens[index].depth == depth, tokens[index].word == "WHERE" else { return false } - guard index > 0 else { return true } - let location = tokens[index].location - if location > 0, parameterPrefixes.contains(source.character(at: location - 1)) { return false } - // `t.where` is a column. - let previous = tokens[index - 1] - return previous.kind != .other || source.character(at: previous.location) != period + tokens[index].depth == depth && tokens[index].word == "WHERE" && !isName(tokens, at: index, in: source) } } + + // The tokens end a word where T-SQL does. SQLite, PostgreSQL and MySQL read every character outside ASCII as part + // of a name, a no-break space included, and MySQL lets a digit begin one. Taking a real filter for a name only + // asks about a statement that had one. + private static func isName(_ tokens: [TSQLTokens.Token], at index: Int, in source: NSString) -> Bool { + guard index > 0 else { return false } + let location = tokens[index].location + let before = source.character(at: location - 1) + if parameterPrefixes.contains(before) || SqlDollarQuote.isIdentifierContinuation(before) { return true } + let end = location + whereLength + if end < source.length, source.character(at: end) >= 0x80 { return true } + // `t.where` is a column, and so is PostgreSQL's `RETURNING id AS where`. + let previous = tokens[index - 1] + if previous.word == "AS" { return true } + return previous.kind == .other && source.character(at: previous.location) == period + } } diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift index 8ed1b5ee0a..576e9b5f0a 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift @@ -73,6 +73,9 @@ public enum SQLUnterminatedStatements { return tokens[index].depth == 0 || (next?.word != "HINT" && next?.word != "PLAN") case "END": return next?.word == "CONVERSATION" + case "BULK": + // `ADMINISTER BULK OPERATIONS` is a permission, and neither `BULK INSERT` nor `OPENROWSET(BULK ...)` goes on so. + return next?.word != "OPERATIONS" default: return true } diff --git a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift index ddc18c1b2c..ed5608fe9f 100644 --- a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift +++ b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift @@ -5,6 +5,7 @@ public final class TeradataConnection { private let config: TeradataConnectionConfig private let socketLock = NSLock() private var _socket: (any TeradataTransport)? + private var _hasLostConnection = false private var socket: (any TeradataTransport)? { get { socketLock.withLock { _socket } } set { socketLock.withLock { _socket = newValue } } @@ -23,8 +24,17 @@ public final class TeradataConnection { self.config = config } + init(config: TeradataConnectionConfig, transport: any TeradataTransport) { + self.config = config + self._socket = transport + confidentialityBypassed = true + } + public var isConnected: Bool { socket != nil } + // Set when a request failed partway, so the session it ran on was given up rather than ended. + public var hasLostConnection: Bool { socketLock.withLock { _hasLostConnection } } + public func connect() throws { try validateLogMech() let deadline = TeradataConnectDeadline(milliseconds: config.connectTimeoutMilliseconds) @@ -36,7 +46,10 @@ public final class TeradataConnection { connectDeadline = nil throw error } - self.socket = socket + socketLock.withLock { + _socket = socket + _hasLostConnection = false + } var established = false defer { if !established { @@ -78,20 +91,36 @@ public final class TeradataConnection { let body = TeradataMessages.optionsParcel(returnStatementInfo: false).encoded() + TeradataMessages.requestParcel(sql: sql).encoded() + TeradataMessages.responseParcel().encoded() - try sendEncrypted(kind: .start, body: body, requestNumber: requestNumber) + let parcels: [Parcel] + do { + parcels = try exchange(body, requestNumber: requestNumber) + } catch { + // The reply still on its way would be read as the next request's. + dropTransport() + throw error + } + return try TeradataResultParser.parse(parcels) + } + private func exchange(_ body: [UInt8], requestNumber: UInt32) throws -> [Parcel] { + try sendEncrypted(kind: .start, body: body, requestNumber: requestNumber) var parcels: [Parcel] = [] - var responseComplete = false - while !responseComplete { + while true { let batch = try readReply() parcels.append(contentsOf: batch) - if Self.responseIsComplete(batch) { - responseComplete = true - } else { - try sendContinue(requestNumber: requestNumber) - } + if Self.responseIsComplete(batch) { return parcels } + try sendContinue(requestNumber: requestNumber) } - return try TeradataResultParser.parse(parcels) + } + + private func dropTransport() { + let dropped = socketLock.withLock { + let transport = _socket + _socket = nil + _hasLostConnection = transport != nil + return transport + } + dropped?.close() } public func disconnect() { diff --git a/apple/Packages/TableProCore/Sources/TableProWeaviateCore/Wire/WeaviateTransport.swift b/apple/Packages/TableProCore/Sources/TableProWeaviateCore/Wire/WeaviateTransport.swift index e737c16353..70cd2da0a6 100644 --- a/apple/Packages/TableProCore/Sources/TableProWeaviateCore/Wire/WeaviateTransport.swift +++ b/apple/Packages/TableProCore/Sources/TableProWeaviateCore/Wire/WeaviateTransport.swift @@ -167,6 +167,17 @@ private final class TaskTracker: NSObject, URLSessionTaskDelegate, @unchecked Se transport?.register(task) } + // A redirect would carry the API key to wherever it points. + func urlSession( + _ session: URLSession, + task: URLSessionTask, + willPerformHTTPRedirection response: HTTPURLResponse, + newRequest request: URLRequest, + completionHandler: @escaping (URLRequest?) -> Void + ) { + completionHandler(nil) + } + func finish() { guard let task = lock.withLock({ task }) else { return } transport?.unregister(task) diff --git a/apple/Packages/TableProCore/Tests/TableProBigtableCoreTests/BigtableRowTests.swift b/apple/Packages/TableProCore/Tests/TableProBigtableCoreTests/BigtableRowTests.swift index 36757a8d44..7442a5c272 100644 --- a/apple/Packages/TableProCore/Tests/TableProBigtableCoreTests/BigtableRowTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProBigtableCoreTests/BigtableRowTests.swift @@ -116,6 +116,8 @@ struct BigtableBytesTests { let column = try BigtableColumn(name: "cf:a:b") #expect(column.family == "cf" && column.qualifier == Data("a:b".utf8)) #expect(try BigtableColumn(name: "cf:b64:/wA=").qualifier == Data([0xFF, 0x00])) + let marked = try BigtableColumn(name: "cf:\u{301}a:b") + #expect(marked.family == "cf" && marked.qualifier == Data("\u{301}a:b".utf8)) #expect(throws: BigtableError.self) { try BigtableColumn(name: "nocolon") } #expect(throws: BigtableError.self) { try BigtableColumn(name: ":q") } } diff --git a/apple/Packages/TableProCore/Tests/TableProBigtableCoreTests/BigtableStatementTests.swift b/apple/Packages/TableProCore/Tests/TableProBigtableCoreTests/BigtableStatementTests.swift index f8bc8ba3f0..e9bf7ce67b 100644 --- a/apple/Packages/TableProCore/Tests/TableProBigtableCoreTests/BigtableStatementTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProBigtableCoreTests/BigtableStatementTests.swift @@ -72,6 +72,23 @@ struct BigtableStatementTests { #expect(try BigtableStatement.parse(BigtableStatement.encode(spec)) == .browse(spec)) } + @Test("A key or column that starts or ends with a joining scalar reads back from its own text") + func roundTripBesideJoiningScalars() throws { + for text in ["\u{301}audit?", "\u{301}a/b", "\u{301}audit cf:a", "\u{301}audit", "\"\u{301}audit", "a b\u{600}", "a\u{600}", "x \u{600}\\"] { + let key = Data(text.utf8) + for request in [ + BigtableRequest(verb: .delete, table: "events", key: key), + BigtableRequest(verb: .delete, table: "events", key: key, column: "cf:" + text), + BigtableRequest(verb: .put, table: "events", key: key, body: #"{"cf:a":"v"}"#), + BigtableRequest(verb: .scan, table: "events", key: key, limit: 5) + ] { + #expect(try BigtableRequest.parse(request.text) == request, "\(request.text)") + } + } + let column = BigtableRequest(verb: .delete, table: "events", key: Data("k".utf8), column: "\u{200D}") + #expect(try BigtableRequest.parse(column.text) == column) + } + @Test("A write body sets cells, deletes nulled ones under PUT, and writes numbers and booleans as text") func mutations() throws { let put = BigtableRequest(verb: .put, table: "t", key: Data("k".utf8), body: #"{"cf:name": null, "cf:n": 42, "cf:on": true, "cf:raw": "b64:AAE="}"#) diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/MongoStatementClassifierTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/MongoStatementClassifierTests.swift index 4ea8d18902..45b98fab3a 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/MongoStatementClassifierTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/MongoStatementClassifierTests.swift @@ -63,6 +63,23 @@ struct MongoStatementClassifierTests { #expect(!verdict(#"db.users.find({where: "this.a > 1"})"#).reachesCode) } + @Test("The server's own operators reach code, the two it rewrites mapReduce into among them", arguments: [ + #"db.users.aggregate([{$project: {e: {$_internalJsEmit: {this: "$$ROOT", eval: "function () {}"}}}}])"#, + #"db.users.aggregate([{$group: {_id: null, v: {$_internalJsReduce: {data: {k: 1, v: "$a"}, eval: "function () {}"}}}}])"#, + #"db.users.aggregate([{$project: {e: {"\u0024_internalJsEmit": {this: "$$ROOT", eval: "function () {}"}}}}])"#, + #"db.users.find({$expr: {$_internalJsEmit: {this: "$$ROOT", eval: "function () {}"}}})"# + ]) + func internalOperatorsReachCode(text: String) { + #expect(verdict(text) == MongoStatementVerdict(tier: .read, reachesCode: true)) + } + + @Test("An internal operator reaches code in text the grammar rejects, and only as a key in text it accepts") + func internalOperatorSpelling() { + #expect(verdict(#"var p = [{$project: {e: {$_internalJsEmit: {this: "$$ROOT", eval: "function () {}"}}}}]"#) + == MongoStatementVerdict(tier: .write, reachesCode: true)) + #expect(!verdict(#"db.users.find({_internal: "$_internalJsEmit"})"#).reachesCode) + } + @Test("Several statements take the worst tier, and any one reaching code marks the text") func worstStatementWins() { #expect(tier("db.a.find({})\ndb.b.insertOne({})") == .write) diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLUnfilteredWriteTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLUnfilteredWriteTests.swift index 1bf04160cc..c6bf2af551 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLUnfilteredWriteTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLUnfilteredWriteTests.swift @@ -80,6 +80,61 @@ struct SQLUnfilteredWriteTests { #expect(first("WITH a AS (SELECT 1), b AS (UPDATE t SET c = 1 RETURNING c) SELECT * FROM b") == .update) } + private static let walk = "WITH RECURSIVE a(id) AS (SELECT 1 UNION ALL SELECT id + 1 FROM a WHERE id < 2)" + + @Test("A writing common table expression is read wherever it stands in the list", arguments: [ + "WITH U&\"gone\" AS (DELETE FROM items RETURNING *) SELECT 1", + "WITH gone\u{20AC} AS (DELETE FROM items RETURNING *) SELECT 1", + "WITH \u{20AC}as (id) AS (DELETE FROM items RETURNING id) SELECT 1", + "\(walk) SEARCH DEPTH FIRST BY id SET ord, gone AS (DELETE FROM items RETURNING *) SELECT 1", + "\(walk) CYCLE id SET looped USING path, gone AS MATERIALIZED (DELETE FROM items RETURNING *) SELECT 1", + ]) + func writingExpressionAnywhereInTheList(sql: String) { + #expect(first(sql) == .delete) + } + + @Test("A name the engine reads as one word and T-SQL as several is not the statement", arguments: [ + "WITH merge\u{20AC} AS (SELECT 1) DELETE FROM t", + "WITH update\u{20AC} AS (SELECT 1) DELETE FROM t", + "WITH c\u{A0}select AS (SELECT 1) DELETE FROM t", + "WITH \u{20AC}as AS (SELECT 1), merge AS (SELECT 1) DELETE FROM t", + "WITH 1merge AS (SELECT 1) DELETE FROM t", + ]) + func nameOfSeveralTokens(sql: String) { + #expect(SQLUnfilteredWrite.writes(in: sql, grammar: Self.grammar("SQLite")).contains(.delete)) + #expect(SQLUnfilteredWrite.writes(in: sql, grammar: Self.mySQL).contains(.delete)) + #expect(first(sql + " WHERE id = 1", Self.grammar("SQLite")) == nil) + } + + @Test("A SEARCH or CYCLE clause is not the statement") + func searchAndCycleClauses() { + #expect(first("\(Self.walk) SEARCH BREADTH FIRST BY id SET merge DELETE FROM items") == .delete) + #expect(first("\(Self.walk) CYCLE id SET c USING merge DELETE FROM items") == .delete) + #expect(first("\(Self.walk) CYCLE id SET c USING p DELETE FROM items WHERE id IN (SELECT id FROM a)") == nil) + #expect(first("\(Self.walk) SEARCH DEPTH FIRST BY id SET ord SELECT * FROM a") == nil) + } + + @Test("A WHERE the engine reads as a name, or as part of one, is not the statement's", arguments: [ + ("DELETE FROM orders AS o\u{A0}WHERE\u{A0}id\u{A0}IS\u{A0}NULL", "SQLite"), + ("DELETE FROM t AS \u{20AC}where", "SQLite"), + ("DELETE FROM t AS where\u{20AC}", "SQLite"), + ("DELETE FROM t x\u{200B}where", "PostgreSQL"), + ("DELETE FROM t RETURNING id AS where", "PostgreSQL"), + ("DELETE FROM t RETURNING id AS /* label */ where", "PostgreSQL"), + ("DELETE FROM t AS 1where", "MySQL"), + ("DELETE x\u{660}where FROM t AS x\u{660}where", "SQL Server"), + ]) + func whereInsideAName(sql: String, typeId: String) { + #expect(first(sql, Self.grammar(typeId)) == .delete) + } + + @Test("A name outside ASCII that ends before the WHERE leaves it the statement's own") + func whereAfterANameOutsideASCII() { + #expect(first("DELETE FROM t AS \u{20AC} WHERE id = 1", Self.grammar("SQLite")) == nil) + #expect(first("DELETE FROM \"\u{20AC}\"WHERE id = 1 RETURNING id AS where") == nil) + #expect(first("UPDATE t SET a = 1, b = 2 WHERE \u{E9}t\u{E9} = 1") == nil) + } + @Test("Only T-SQL's maintenance command is spelled UPDATE STATISTICS without a SET") func tableNamedStatistics() { #expect(first("UPDATE statistics SET value = 1") == .update) diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLUnterminatedStatementsTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLUnterminatedStatementsTests.swift index c2c461f661..ef2a4f647d 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLUnterminatedStatementsTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLUnterminatedStatementsTests.swift @@ -64,6 +64,7 @@ struct SQLUnterminatedStatementsTests { ("DELETE FROM t SELECT 1 WHERE 1 = 1", ["DELETE FROM t", "SELECT 1 WHERE 1 = 1"]), ("CREATE TYPE dbo.t FROM int DROP TABLE x", ["CREATE TYPE dbo.t FROM int", "DROP TABLE x"]), ("SELECT 1 LINENO 5 DELETE FROM t", ["SELECT 1", "LINENO 5", "DELETE FROM t"]), + ("SELECT 1 BULK INSERT t FROM 'f'", ["SELECT 1", "BULK", "INSERT t FROM 'f'"]), ]) func juxtaposedStatements(sql: String, expected: [String]) { #expect(inner(sql) == expected) @@ -159,6 +160,8 @@ struct SQLUnterminatedStatementsTests { ), ("CREATE TABLE t (a int REFERENCES p (id) ON DELETE SET NULL ON UPDATE SET DEFAULT)", "CREATE"), ("GRANT SELECT, UPDATE ON [t] TO [u]", "GRANT"), + ("GRANT ADMINISTER BULK OPERATIONS TO u", "GRANT"), + ("DENY ADMINISTER DATABASE BULK OPERATIONS TO u", "DENY"), ("ALTER TABLE [t] ADD FOREIGN KEY ([a]) REFERENCES [p] ([id]) ON DELETE CASCADE ON UPDATE SET NULL", "ALTER"), ( "MERGE t USING s ON t.id = s.id WHEN MATCHED THEN DELETE WHEN NOT MATCHED THEN INSERT (id) VALUES (s.id);", @@ -170,6 +173,7 @@ struct SQLUnterminatedStatementsTests { #expect(found.first?.uppercased().hasPrefix(keyword) == true) #expect(!found.contains { $0.uppercased().hasPrefix("DELETE") }) #expect(!found.contains { $0.uppercased().hasPrefix("INSERT") }) + #expect(!found.contains { $0.uppercased().hasPrefix("BULK") }) } @Test("A procedure, function or trigger runs nothing written after its header", arguments: [ @@ -190,6 +194,14 @@ struct SQLUnterminatedStatementsTests { #expect(inner(sql) == ["INSERT INTO log", "SELECT id FROM () AS d", "DELETE FROM t OUTPUT deleted.id"]) } + @Test("The BULK of an OPENROWSET comes on its own, whatever stands before it", arguments: [ + "SELECT BulkColumn FROM OPENROWSET(BULK 'f', SINGLE_CLOB) AS d", + "SELECT BulkColumn FROM OPENROWSET ( /* */ bulk N'f', SINGLE_CLOB) AS d", + ]) + func openRowsetBulkComesOnItsOwn(sql: String) { + #expect(begins("BULK", in: sql)) + } + @Test("A subquery does not end the statement around it, so its WHERE stays with it") func subqueryKeepsTheOuterWhere() { let sql = "DELETE t FROM t JOIN (SELECT id FROM s) AS x ON x.id = t.id WHERE x.id > 1" diff --git a/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataConnectionLossTests.swift b/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataConnectionLossTests.swift new file mode 100644 index 0000000000..f006e0246d --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataConnectionLossTests.swift @@ -0,0 +1,77 @@ +@testable import TableProTeradataCore +import Testing + +private final class ScriptedTransport: TeradataTransport { + private var replies: [[UInt8]] + private var timeouts: [Bool] + private var stream: [UInt8] = [] + private(set) var sent = 0 + private(set) var closed = false + + // A nil reply is a receive that times out, as `recv` does after 20 seconds. + init(replies: [[UInt8]?]) { + self.replies = replies.map { $0 ?? [] } + timeouts = replies.map { $0 == nil } + } + + func send(_ bytes: [UInt8]) throws { + sent += 1 + } + + func receive(_ count: Int) throws -> [UInt8] { + if stream.isEmpty, !replies.isEmpty { + let timesOut = timeouts.removeFirst() + stream = replies.removeFirst() + if timesOut { throw TeradataWireError.truncated("recv errno 35") } + } + guard stream.count >= count else { throw TeradataWireError.truncated("closed") } + defer { stream.removeFirst(count) } + return Array(stream.prefix(count)) + } + + func finishConnecting() {} + + func cancel() {} + + func close() { + closed = true + } +} + +struct TeradataConnectionLossTests { + private static let config = TeradataConnectionConfig(host: "td.example", username: "dbc", password: "dbc") + + private static func reply(_ parcels: [Parcel]) -> [UInt8] { + LanMessage(kind: .start, body: parcels.flatMap { $0.encoded() }).encoded() + } + + @Test("A request that times out gives the session up, so its late reply is never read as the next one's") + func timeoutGivesTheSessionUp() throws { + let lateReply = Self.reply([Parcel(.success), Parcel(.endRequest)]) + let transport = ScriptedTransport(replies: [nil, lateReply]) + let connection = TeradataConnection(config: Self.config, transport: transport) + + #expect(throws: TeradataWireError.self) { try connection.execute("SELECT * FROM mallory.notes") } + #expect(transport.closed) + #expect(!connection.isConnected) + #expect(connection.hasLostConnection) + + #expect(throws: TeradataWireError.self) { try connection.execute("SELECT iban FROM finance.payees") } + #expect(transport.sent == 1) + } + + @Test("An error the server answers with ends the request and keeps the session") + func serverErrorKeepsTheSession() throws { + let failure = Parcel(.failure, body: [UInt8](repeating: 0, count: 8) + [0x0E, 0x3F, 0x00, 0x01, 0x78]) + let transport = ScriptedTransport(replies: [Self.reply([failure]), Self.reply([Parcel(.endRequest)])]) + let connection = TeradataConnection(config: Self.config, transport: transport) + + #expect(throws: TeradataWireError.self) { try connection.execute("SELECT * FROM missing") } + #expect(connection.isConnected) + #expect(!connection.hasLostConnection) + + _ = try connection.execute("SELECT 1") + #expect(transport.sent == 2) + #expect(!transport.closed) + } +} diff --git a/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver.swift b/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver.swift index 75b0173664..5bbd4314cd 100644 --- a/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver.swift +++ b/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver.swift @@ -70,6 +70,7 @@ internal final class BigQueryPluginDriver: PluginDatabaseDriver, @unchecked Send .dataCompare, .cellExpressions, .uuidCellText, + .rowWrites, ] } diff --git a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift index 0d5acfefb4..3a26349f24 100644 --- a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift +++ b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift @@ -285,12 +285,9 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked ) urlConfig.timeoutIntervalForResource = HttpQueryTimeout.sessionResourceTimeout + let delegate = tlsDelegate ?? PluginRedirectRefusingDelegate() lock.withLock { - if let tlsDelegate { - session = URLSession(configuration: urlConfig, delegate: tlsDelegate, delegateQueue: nil) - } else { - session = URLSession(configuration: urlConfig) - } + session = URLSession(configuration: urlConfig, delegate: delegate, delegateQueue: nil) } let versionText: String? @@ -925,7 +922,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked // MARK: - TLS Delegate -nonisolated final class ClickHouseTLSDelegate: NSObject, URLSessionDelegate, @unchecked Sendable { +nonisolated final class ClickHouseTLSDelegate: PluginRedirectRefusingDelegate, @unchecked Sendable { private enum Strategy { case systemTrust case skipVerify diff --git a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift index a0c432ad4c..c491bb2332 100644 --- a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift +++ b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift @@ -136,7 +136,7 @@ nonisolated extension ClickHousePluginDriver { /// With `send_progress_in_http_headers` the status goes out with the first progress header, so /// a query that fails after it answers HTTP 200 and names the failure in this header. static func isFailure(_ response: HTTPURLResponse) -> Bool { - response.statusCode >= 400 || response.value(forHTTPHeaderField: exceptionCodeHeader) != nil + !(200 ..< 300).contains(response.statusCode) || response.value(forHTTPHeaderField: exceptionCodeHeader) != nil } static func headerFields(_ response: HTTPURLResponse?) -> [String: String] { diff --git a/apple/Plugins/ClickHouseDriverTests/ClickHouseHTTPStub.swift b/apple/Plugins/ClickHouseDriverTests/ClickHouseHTTPStub.swift index c026a3ab9f..dd2826ae15 100644 --- a/apple/Plugins/ClickHouseDriverTests/ClickHouseHTTPStub.swift +++ b/apple/Plugins/ClickHouseDriverTests/ClickHouseHTTPStub.swift @@ -59,6 +59,12 @@ final class ClickHouseHTTPStubProtocol: URLProtocol, @unchecked Sendable { headerFields: reply.headers ) else { return } + // As URLSession's own loader does: offer the redirect, then hand over the 3xx if the delegate keeps it. + if (300 ..< 400).contains(reply.statusCode), let location = reply.headers["Location"].flatMap(URL.init) { + var redirected = request + redirected.url = location + client?.urlProtocol(self, wasRedirectedTo: redirected, redirectResponse: response) + } client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) client?.urlProtocol(self, didLoad: Data(reply.body.utf8)) client?.urlProtocolDidFinishLoading(self) diff --git a/apple/Plugins/ClickHouseDriverTests/ClickHouseRedirectTests.swift b/apple/Plugins/ClickHouseDriverTests/ClickHouseRedirectTests.swift new file mode 100644 index 0000000000..4312e7a225 --- /dev/null +++ b/apple/Plugins/ClickHouseDriverTests/ClickHouseRedirectTests.swift @@ -0,0 +1,21 @@ +import Foundation +import TableProPluginKit +import Testing + +struct ClickHouseRedirectTests { + @Test("A redirect to another server is refused, so the connection and its credentials stay where they point") + func redirectElsewhereIsRefused() async throws { + let elsewhere = ClickHouseStubServer() + let target = "http://\(elsewhere.host):8123/" + let server = ClickHouseStubServer { _ in + ClickHouseStubReply(statusCode: 307, body: "", headers: ["Location": target]) + } + let driver = server.unconnectedDriver() + defer { driver.disconnect() } + + try? await driver.connect() + await #expect(throws: (any Error).self) { try await driver.execute(query: "SELECT 1") } + #expect(!server.requests.isEmpty) + #expect(elsewhere.requests.isEmpty) + } +} diff --git a/apple/Plugins/CloudflareD1DriverPlugin/D1HttpClient.swift b/apple/Plugins/CloudflareD1DriverPlugin/D1HttpClient.swift index f3ef39574f..1a5edb6df6 100644 --- a/apple/Plugins/CloudflareD1DriverPlugin/D1HttpClient.swift +++ b/apple/Plugins/CloudflareD1DriverPlugin/D1HttpClient.swift @@ -354,7 +354,7 @@ final class D1HttpClient: @unchecked Sendable { throw D1HttpError(message: "Invalid response from server") } - if httpResponse.statusCode >= 400 { + if !(200 ..< 300).contains(httpResponse.statusCode) { try handleHttpError(statusCode: httpResponse.statusCode, data: data, response: httpResponse) } @@ -404,7 +404,7 @@ final class D1HttpClient: @unchecked Sendable { } } -private final class D1TaskTracker: NSObject, URLSessionTaskDelegate, @unchecked Sendable { +private final class D1TaskTracker: PluginRedirectRefusingDelegate, @unchecked Sendable { private weak var client: D1HttpClient? private let lock = NSLock() private var task: URLSessionTask? diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBAccessMode.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBAccessMode.swift index bc85c0febe..5bbdd843dd 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBAccessMode.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBAccessMode.swift @@ -2,8 +2,7 @@ // DuckDBAccessMode.swift // DuckDBDriverPlugin // -// Which access mode a connection opens its file with. No CDuckDB import, so TableProTests -// can exercise the gate without loading the plugin bundle. +// Which access mode a connection opens its file with. // import Foundation @@ -25,18 +24,20 @@ enum DuckDBAccessMode: String { extension DuckDBAccessMode { static let fieldId = "duckdbReadOnly" - /// Read-only applies to DuckDB's own storage and nothing else. Measured: `access_mode` of - /// `READ_ONLY` against `:memory:`, or against the Parquet, CSV and JSON readers that - /// `DuckDBFileKinds.readOnlyData` covers, fails the open outright with - /// `Catalog Error: Cannot launch in-memory database in read-only mode!`. Those paths hold no - /// write lock to give up in the first place, and DuckDB never writes back to them, so the - /// setting is ignored for them rather than turned into a connection that will not open. + /// Read-only applies to a database file, whatever it is called. Measured: `access_mode` of + /// `READ_ONLY` against an empty path, `:memory:`, or a Parquet, CSV or JSON reader fails the + /// open outright with `Catalog Error: Cannot launch in-memory database in read-only mode!`. + /// Those paths hold no write lock to give up in the first place, and DuckDB never writes back + /// to them, so the setting is ignored for them rather than turned into a connection that will + /// not open. static func resolve(fields: [String: String], path: String) -> DuckDBAccessMode { guard fields[fieldId] == "true", carriesAccessMode(path: path) else { return .readWrite } return .readOnly } static func carriesAccessMode(path: String) -> Bool { - DuckDBFileKinds.database.contains((path as NSString).pathExtension.lowercased()) + // `:memory:x` is a file named so. + guard !path.isEmpty, path != ":memory:" else { return false } + return DuckDBFileKinds.holdsDatabase(atPath: path) || !DuckDBFileKinds.opensAsReader(path) } } diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBFileKinds.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBFileKinds.swift index 2dd7d0cfed..bd6f3cdf5a 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBFileKinds.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBFileKinds.swift @@ -27,6 +27,34 @@ enum DuckDBFileKinds { /// or `.csv` path is a typo, and DuckDB would answer it by creating an empty database /// under that name, which reads as a working connection to nothing. static func canBeCreated(atPath path: String) -> Bool { - !readOnlyData.contains((path as NSString).pathExtension.lowercased()) + !opensAsReader(path) + } + + // Measured on the shipped libduckdb (scripts/probes/check-duckdb-access-mode.sh): DuckDB picks a reader by name, + // in any case and with or without gzip or zstd around it, and opens every other path as a database. + static func opensAsReader(_ path: String) -> Bool { + var name = (path as NSString).lastPathComponent.lowercased() as NSString + if compressionExtensions.contains(name.pathExtension) { + name = name.deletingPathExtension as NSString + } + return readerExtensions.contains(name.pathExtension) + } + + private static let readerExtensions = Set(readOnlyData + ["jsonl"]) + + private static let compressionExtensions: Set = ["gz", "zst"] + + private static let duckDBMagic = Data("DUCK".utf8) + private static let duckDBMagicOffset = 8 + private static let sqliteMagic = Data("SQLite format 3\u{0}".utf8) + + // DuckDB opens its own storage and a SQLite file by their header, whatever they are called. + static func holdsDatabase(atPath path: String) -> Bool { + guard let handle = FileHandle(forReadingAtPath: path) else { return false } + defer { try? handle.close() } + guard let header = try? handle.read(upToCount: sqliteMagic.count) else { return false } + if header.starts(with: sqliteMagic) { return true } + let magicRange = duckDBMagicOffset..<(duckDBMagicOffset + duckDBMagic.count) + return header.count >= magicRange.upperBound && header.subdata(in: magicRange) == duckDBMagic } } diff --git a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchConnection.swift b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchConnection.swift index 6802e13f9e..347a2ba7fa 100644 --- a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchConnection.swift +++ b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchConnection.swift @@ -459,7 +459,7 @@ internal final class ElasticsearchConnection: NSObject, @unchecked Sendable { } } -extension ElasticsearchConnection: URLSessionDelegate { +extension ElasticsearchConnection: URLSessionTaskDelegate { func urlSession( _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, @@ -474,4 +474,15 @@ extension ElasticsearchConnection: URLSessionDelegate { } completionHandler(.useCredential, URLCredential(trust: trust)) } + + // A redirect would carry the credentials to wherever it points. + func urlSession( + _ session: URLSession, + task: URLSessionTask, + willPerformHTTPRedirection response: HTTPURLResponse, + newRequest request: URLRequest, + completionHandler: @escaping (URLRequest?) -> Void + ) { + completionHandler(nil) + } } diff --git a/apple/Plugins/EtcdDriverPlugin/EtcdHttpClient.swift b/apple/Plugins/EtcdDriverPlugin/EtcdHttpClient.swift index e02ff6437d..ecf8bf1dc5 100644 --- a/apple/Plugins/EtcdDriverPlugin/EtcdHttpClient.swift +++ b/apple/Plugins/EtcdDriverPlugin/EtcdHttpClient.swift @@ -373,21 +373,17 @@ internal final class EtcdHttpClient: @unchecked Sendable { ) urlConfig.timeoutIntervalForResource = HttpQueryTimeout.sessionResourceTimeout - let delegate: URLSessionDelegate? + let delegate: PluginRedirectRefusingDelegate switch tlsMode { case "Required": // Encryption without certificate verification — matches UI "Required (skip verify)" delegate = InsecureTlsDelegate() default: - delegate = try verifyingDelegate(tlsMode: tlsMode) + delegate = try verifyingDelegate(tlsMode: tlsMode) ?? PluginRedirectRefusingDelegate() } lock.withLock { - if let delegate { - session = URLSession(configuration: urlConfig, delegate: delegate, delegateQueue: nil) - } else { - session = URLSession(configuration: urlConfig) - } + session = URLSession(configuration: urlConfig, delegate: delegate, delegateQueue: nil) } do { @@ -414,7 +410,7 @@ internal final class EtcdHttpClient: @unchecked Sendable { !config.username.isEmpty } - private func verifyingDelegate(tlsMode: String) throws -> URLSessionDelegate? { + private func verifyingDelegate(tlsMode: String) throws -> PluginRedirectRefusingDelegate? { let caPath = config.additionalFields["etcdCaCertPath"] guard let serverTrust = try EtcdServerTrust.make(tlsMode: tlsMode, caCertificatePath: caPath) else { return nil @@ -821,7 +817,8 @@ internal final class EtcdHttpClient: @unchecked Sendable { guard let httpResponse = response as? HTTPURLResponse else { throw EtcdError.serverError(String(localized: "Invalid response type")) } - guard httpResponse.statusCode >= 400 else { return data } + // A refused redirect arrives as its 3xx. + guard !(200 ..< 300).contains(httpResponse.statusCode) else { return data } let fault = EtcdServerFault.decode(httpStatus: httpResponse.statusCode, body: data) let recovery = EtcdRequestRecovery.action( @@ -1082,7 +1079,7 @@ internal final class EtcdHttpClient: @unchecked Sendable { // MARK: - TLS Delegates - private final class InsecureTlsDelegate: NSObject, URLSessionDelegate { + private final class InsecureTlsDelegate: PluginRedirectRefusingDelegate { func urlSession( _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, @@ -1097,7 +1094,7 @@ internal final class EtcdHttpClient: @unchecked Sendable { } } - private final class EtcdTlsDelegate: NSObject, URLSessionDelegate { + private final class EtcdTlsDelegate: PluginRedirectRefusingDelegate { private let serverTrust: EtcdServerTrust private let clientCredential: URLCredential? diff --git a/apple/Plugins/LibSQLDriverPlugin/HranaHttpClient.swift b/apple/Plugins/LibSQLDriverPlugin/HranaHttpClient.swift index 0354ba0970..1d49a8696f 100644 --- a/apple/Plugins/LibSQLDriverPlugin/HranaHttpClient.swift +++ b/apple/Plugins/LibSQLDriverPlugin/HranaHttpClient.swift @@ -298,7 +298,7 @@ final class HranaHttpClient: @unchecked Sendable { throw HranaHttpError(message: "Invalid response from server") } - if httpResponse.statusCode >= 400 { + if !(200 ..< 300).contains(httpResponse.statusCode) { try handleHttpError(statusCode: httpResponse.statusCode, data: data, response: httpResponse) } @@ -357,7 +357,7 @@ final class HranaHttpClient: @unchecked Sendable { } } -private final class HranaTaskTracker: NSObject, URLSessionTaskDelegate, @unchecked Sendable { +private final class HranaTaskTracker: PluginRedirectRefusingDelegate, @unchecked Sendable { private weak var client: HranaHttpClient? private let lock = NSLock() private var task: URLSessionTask? diff --git a/apple/Plugins/Shared/PluginRedirectRefusingDelegate.swift b/apple/Plugins/Shared/PluginRedirectRefusingDelegate.swift new file mode 100644 index 0000000000..21beb34d79 --- /dev/null +++ b/apple/Plugins/Shared/PluginRedirectRefusingDelegate.swift @@ -0,0 +1,15 @@ +import Foundation + +// URLSession repeats a driver's credential headers to wherever a redirect points. No engine a driver speaks to +// redirects, so the redirect comes back as the response, as in DynamoDB, BigQuery, Spanner and Trino. +nonisolated class PluginRedirectRefusingDelegate: NSObject, URLSessionTaskDelegate, @unchecked Sendable { + func urlSession( + _ session: URLSession, + task: URLSessionTask, + willPerformHTTPRedirection response: HTTPURLResponse, + newRequest request: URLRequest, + completionHandler: @escaping (URLRequest?) -> Void + ) { + completionHandler(nil) + } +} diff --git a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeConnection.swift b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeConnection.swift index b3b1838a9e..925e4c7fd6 100644 --- a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeConnection.swift +++ b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeConnection.swift @@ -82,11 +82,15 @@ final class SnowflakeConnection: @unchecked Sendable { let configuration = URLSessionConfiguration.ephemeral configuration.timeoutIntervalForRequest = Self.requestTimeout configuration.timeoutIntervalForResource = 600 - self.session = URLSession(configuration: configuration) + self.session = URLSession( + configuration: configuration, delegate: PluginRedirectRefusingDelegate(), delegateQueue: nil + ) let connectConfiguration = URLSessionConfiguration.ephemeral connectConfiguration.timeoutIntervalForRequest = TimeInterval(PluginConnectTimeout.maximumMilliseconds) / 1_000 connectConfiguration.timeoutIntervalForResource = TimeInterval(PluginConnectTimeout.maximumMilliseconds) / 1_000 - self.connectSession = URLSession(configuration: connectConfiguration) + self.connectSession = URLSession( + configuration: connectConfiguration, delegate: PluginRedirectRefusingDelegate(), delegateQueue: nil + ) self._currentDatabase = params.database.isEmpty ? nil : params.database self._currentSchema = params.schema.isEmpty ? nil : params.schema diff --git a/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver.swift b/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver.swift index 67b75b893c..cd645015f0 100644 --- a/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver.swift +++ b/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver.swift @@ -37,7 +37,7 @@ internal final class SpannerPluginDriver: PluginDatabaseDriver, @unchecked Senda } var capabilities: PluginCapabilities { - [.multiSchema, .cancelQuery, .transactions, .truncateTable, .cellExpressions, .uuidCellText] + [.multiSchema, .cancelQuery, .transactions, .truncateTable, .cellExpressions, .uuidCellText, .rowWrites] } var serverVersion: String? { diff --git a/apple/Plugins/SurrealDBDriverPlugin/SurrealRPCClient.swift b/apple/Plugins/SurrealDBDriverPlugin/SurrealRPCClient.swift index 5635b7b81b..57ac7e3fa3 100644 --- a/apple/Plugins/SurrealDBDriverPlugin/SurrealRPCClient.swift +++ b/apple/Plugins/SurrealDBDriverPlugin/SurrealRPCClient.swift @@ -63,10 +63,9 @@ public final class SurrealRPCClient: NSObject, @unchecked Sendable { let configuration = URLSessionConfiguration.ephemeral configuration.httpAdditionalHeaders = [:] configuration.timeoutIntervalForRequest = deadline.remainingSeconds() - let delegate = config.skipTLSVerify ? self : nil lock.withLock { connectDeadline = deadline - session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil) + session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil) } } @@ -331,7 +330,7 @@ public final class SurrealRPCClient: NSObject, @unchecked Sendable { } } -extension SurrealRPCClient: URLSessionDelegate { +extension SurrealRPCClient: URLSessionTaskDelegate { public func urlSession( _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, @@ -345,4 +344,15 @@ extension SurrealRPCClient: URLSessionDelegate { } completionHandler(.useCredential, URLCredential(trust: trust)) } + + // A redirect would carry the credentials to wherever it points. + public func urlSession( + _ session: URLSession, + task: URLSessionTask, + willPerformHTTPRedirection response: HTTPURLResponse, + newRequest request: URLRequest, + completionHandler: @escaping (URLRequest?) -> Void + ) { + completionHandler(nil) + } } diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift b/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift index ecb0231e7e..ef3166e4c1 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift @@ -13,6 +13,9 @@ final class TeradataAsyncConnection: @unchecked Sendable { queue.sync { connection.isConnected } } + // Read outside the queue, so asking never waits behind a running statement. + var hasLostConnection: Bool { connection.hasLostConnection } + func connect() async throws { try await run { try $0.connect() } } diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift b/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift index e15f0bfdb7..b7ee9b526c 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift @@ -127,11 +127,12 @@ final class TeradataPluginDriver: PluginDatabaseDriver, @unchecked Sendable { static let logger = Logger(subsystem: "com.TablePro", category: "TeradataPluginDriver") var serverVersion: String? { _serverVersion } + var hasLostConnection: Bool { connection?.hasLostConnection ?? false } var supportsSchemas: Bool { false } var supportsTransactions: Bool { true } var capabilities: PluginCapabilities { - [.cancelQuery, .transactions, .alterTableDDL, .dataCompare, .cellExpressions, .uuidCellText] + [.cancelQuery, .transactions, .alterTableDDL, .dataCompare, .cellExpressions, .uuidCellText, .rowWrites] } init(config: DriverConnectionConfig) { diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataPluginDriver+Editing.swift b/apple/Plugins/TeradataDriverPlugin/TeradataPluginDriver+Editing.swift index 9700105918..e9c842a0f7 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataPluginDriver+Editing.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataPluginDriver+Editing.swift @@ -62,6 +62,13 @@ extension TeradataPluginDriver { return statements.isEmpty ? nil : statements } + func generateIdentityPreservingInsert( + table: String, schema: String?, columns: [String], primaryKeyColumns: [String], rows: [[PluginCellValue]] + ) -> [(statement: String, parameters: [PluginCellValue])]? { + let target = TeradataSchemaQueries.qualifiedName(database: effectiveDatabaseForSchema(schema), table: table) + return rows.map { (TeradataRowEditSQL.insert(target: target, columns: columns, values: $0), []) } + } + private func quote(_ name: String) -> String { TeradataSchemaQueries.quoteIdentifier(name) } diff --git a/apple/Plugins/TypesenseDriverPlugin/TypesenseConnection.swift b/apple/Plugins/TypesenseDriverPlugin/TypesenseConnection.swift index 24e1cc8e7e..3ac366d3a6 100644 --- a/apple/Plugins/TypesenseDriverPlugin/TypesenseConnection.swift +++ b/apple/Plugins/TypesenseDriverPlugin/TypesenseConnection.swift @@ -342,7 +342,7 @@ internal final class TypesenseConnection: NSObject, @unchecked Sendable { } } -extension TypesenseConnection: URLSessionDelegate { +extension TypesenseConnection: URLSessionTaskDelegate { func urlSession( _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, @@ -357,4 +357,15 @@ extension TypesenseConnection: URLSessionDelegate { } completionHandler(.useCredential, URLCredential(trust: trust)) } + + // A redirect would carry the API key to wherever it points. + func urlSession( + _ session: URLSession, + task: URLSessionTask, + willPerformHTTPRedirection response: HTTPURLResponse, + newRequest request: URLRequest, + completionHandler: @escaping (URLRequest?) -> Void + ) { + completionHandler(nil) + } } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Verification.swift b/apple/TablePro/Core/Database/DatabaseManager+Verification.swift index 5a5d04fe93..4c199d5042 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Verification.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Verification.swift @@ -64,7 +64,8 @@ extension DatabaseManager { /// and stamps the connection fresh each time. internal func verifyBeforeUse(_ connectionId: UUID) async { guard let session = activeSessions[connectionId], let driver = session.driver else { return } - guard supportsHealthChecks(connectionId) else { return } + // A driver that reports its connection gone answered unasked, so an opted-out engine is reconnected too. + guard supportsHealthChecks(connectionId) || driver.hasLostConnection else { return } /// A session already known to be in trouble has a reconnect of its own, either the /// monitor's or the one `ensureConnected` is about to run. Checking it again would only /// race them. diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift index 0669a1d8ae..51a4b8763d 100644 --- a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift @@ -199,6 +199,15 @@ enum QueryClassifier { if SQLProceduralBlock.runs(statement, grammar: grammar) { return proceduralBlockDeletesEverything(statement, grammar: grammar) } + // With ANALYZE the engine runs what it explains, so the explained statement is read as the tier reads it. + let projection = StatementProjection(statement: statement, grammar: grammar) + let keyword = SQLLeadingComments.leadingCodeKeyword(projection.body) + if explainPrefixes.contains(keyword) { + guard let inner = explainedInnerStatement(projection, keyword: keyword), inner.executesStatement else { + return false + } + return statementDeletesEverything(inner.statement, grammar: grammar, databaseType: databaseType) + } return SQLUnfilteredWrite.writes(in: statement, grammar: grammar).contains(.delete) } @@ -240,7 +249,22 @@ private extension QueryClassifier { static let executingExplainOptions: [String] = ["ANALYZE", "ANALYSE"] static func explainExecutesStatement(_ options: String) -> Bool { - executingExplainOptions.contains { options.contains($0) } + executingExplainOptions.contains { option in + options.components(separatedBy: option).dropFirst().contains { !turnsOptionOff($0) } + } + } + + static let optionOffValues = ["FALSE", "OFF", "0"] + + // PostgreSQL turns an option off with FALSE, OFF or 0 after it, so `EXPLAIN (ANALYZE FALSE)` only plans. + static func turnsOptionOff(_ rest: String) -> Bool { + let value = StatementBlank.trimmingLeading(rest[...]) + guard value.startIndex != rest.startIndex else { return false } + return optionOffValues.contains { off in + guard value.hasPrefix(off) else { return false } + guard let next = value.dropFirst(off.count).first else { return true } + return !(next.isLetter || next.isNumber || next == "_" || next == "$") + } } static let whereClauseRegex = try? NSRegularExpression(pattern: "\\sWHERE\\s", options: []) @@ -275,7 +299,7 @@ private extension QueryClassifier { static let filesystemOrCodeKeywords: Set = [ "COPY", "ATTACH", "DETACH", "DO", "LOAD", "INSTALL", "IMPORT", "EXPORT", - "BACKUP", "RESTORE", "DUMP", "SOURCE", "UNLOAD" + "BACKUP", "RESTORE", "DUMP", "SOURCE", "UNLOAD", "BULK" ] static let writeKeywords: Set = [ @@ -305,7 +329,8 @@ private extension QueryClassifier { "LOAD_FILE(", "PG_READ_FILE(", "PG_READ_BINARY_FILE(", "PG_LS_DIR(", "SYS_READ_FILE(", "SYS_READ_BINARY_FILE(", "SYS_LS_DIR(", "LO_IMPORT(", "LO_EXPORT(", "PG_FILE_WRITE(", "LOAD_EXTENSION(", - "READFILE(", "WRITEFILE(", "FN_GET_AUDIT_FILE(", "XP_CMDSHELL", "SP_OACREATE" + "READFILE(", "WRITEFILE(", "FN_GET_AUDIT_FILE(", "XP_CMDSHELL", "SP_OACREATE", + "OPENROWSET(", "OPENDATASOURCE(" ] // A comment, a line break or a run of spaces between two words, or a space before `(`, is nothing to the server: @@ -396,6 +421,10 @@ private extension QueryClassifier { return QueryClassification(tier: .destructive, reachesFilesystemOrExecutesCode: false) } + if keyword == "CREATE" || keyword == "REPLACE", replacesStoredData(projection.code) { + return QueryClassification(tier: .destructive, reachesFilesystemOrExecutesCode: false) + } + if keyword == "ALTER" { // `ALTER TABLE t TRUNCATE PARTITION ALL` empties the table, measured on MySQL 8.4. let dropsData = body.range(of: " DROP ", options: .literal) != nil @@ -450,6 +479,44 @@ private extension QueryClassifier { private static let routineDefinitionLookahead = 3 + private static let replacedDataKinds: Set = ["TABLE", "SCHEMA", "DATABASE"] + + private static let replacedObjectKinds: Set = [ + "VIEW", "FUNCTION", "PROCEDURE", "PROC", "TRIGGER", "PACKAGE", "TYPE", "SEQUENCE", "MACRO", "INDEX", "SYNONYM", + ] + + // Modifiers such as `GLOBAL TEMPORARY` or DuckDB's `UNLOGGED` stand before the kind, two words at most. + private static let replacedKindLookahead = 3 + + /// `CREATE OR REPLACE TABLE t` drops `t` and its rows before it makes the new one on Snowflake, BigQuery, DuckDB, + /// MariaDB, ClickHouse and Databricks, and a replaced schema or database takes every table in it. ClickHouse's + /// `REPLACE TABLE` is the same statement. A replaced view or routine loses no rows. + static func replacesStoredData(_ code: String) -> Bool { + let code = code as NSString + var cursor = skipBlanks(in: code, from: 0) + func nextWord() -> String? { + let end = endOfWord(in: code, from: cursor) + guard end > cursor else { return nil } + let word = code.substring(with: NSRange(location: cursor, length: end - cursor)).uppercased() + cursor = skipBlanks(in: code, from: end) + return word + } + switch nextWord() { + case "CREATE": + guard nextWord() == "OR", nextWord() == "REPLACE" else { return false } + case "REPLACE": + break + default: + return false + } + for _ in 0 ..< replacedKindLookahead { + guard let word = nextWord() else { return false } + if replacedDataKinds.contains(word) { return true } + if replacedObjectKinds.contains(word) { return false } + } + return false + } + static func commonTableExpressionClassification(_ body: String) -> QueryClassification { for keyword in ["DROP", "TRUNCATE"] where containsWord(body, keyword) { return QueryClassification(tier: .destructive, reachesFilesystemOrExecutesCode: false) diff --git a/apple/TableProMobile/project.yml b/apple/TableProMobile/project.yml index 173932e730..e8f2e043cc 100644 --- a/apple/TableProMobile/project.yml +++ b/apple/TableProMobile/project.yml @@ -74,6 +74,7 @@ targets: excludes: - Info.plist - ../Plugins/Shared/PluginConnectTimeout.swift + - ../Plugins/Shared/PluginRedirectRefusingDelegate.swift # The MongoDB plugin driver runs behind PluginBackedDriver too; the iOS CLibMongoc bridge stands in for the # plugin's own, and the principal class stays on the Mac. - path: ../Plugins/MongoDBDriverPlugin diff --git a/apple/TableProTests/Core/Database/ConnectionVerificationTests.swift b/apple/TableProTests/Core/Database/ConnectionVerificationTests.swift index b22550e6e9..b8a2cf01a7 100644 --- a/apple/TableProTests/Core/Database/ConnectionVerificationTests.swift +++ b/apple/TableProTests/Core/Database/ConnectionVerificationTests.swift @@ -184,6 +184,19 @@ struct ConnectionVerificationTests { await cleanUp(connection.id) } + @Test("an engine that opts out of health checks is still checked once its driver reports the connection lost") + func optedOutEngineWithALostDriverIsChecked() async { + let driver = MockDatabaseDriver() + driver.hasLostConnection = true + let connection = makeSession(driver: driver, typeId: DatabaseType.sqlite.rawValue) + DatabaseManager.shared.markSessionVerified(connection.id) + + await DatabaseManager.shared.verifyBeforeUse(connection.id) + + #expect(driver.pingCallCount == 1) + await cleanUp(connection.id) + } + @Test("a check that fails reconnects the connection") func aFailedCheckReconnects() async { let driver = MockDatabaseDriver() diff --git a/apple/TableProTests/Core/Utilities/SQL/QueryClassifierCreateOrReplaceTests.swift b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierCreateOrReplaceTests.swift new file mode 100644 index 0000000000..18a8d60320 --- /dev/null +++ b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierCreateOrReplaceTests.swift @@ -0,0 +1,56 @@ +import Foundation +@testable import TablePro +import Testing + +struct QueryClassifierCreateOrReplaceTests { + @Test("Replacing a table, schema or database is destructive", arguments: [ + (DatabaseType.snowflake, "CREATE OR REPLACE TABLE orders (id INT)"), + (.snowflake, "create or replace table orders as select * from orders where 1 = 0"), + (.snowflake, "CREATE OR REPLACE TRANSIENT TABLE orders (id INT)"), + (.snowflake, "CREATE OR REPLACE SCHEMA sales"), + (.snowflake, "CREATE OR REPLACE DATABASE prod"), + (.bigQuery, "CREATE OR REPLACE TABLE shop.orders (id INT64)"), + (.duckdb, "CREATE OR REPLACE TEMP TABLE orders (id INT)"), + (.duckdb, "CREATE OR REPLACE UNLOGGED TABLE orders (id INT)"), + (.duckdb, "CREATE OR REPLACE TABLE function (id INT)"), + (.snowflake, "CREATE OR REPLACE GLOBAL TEMPORARY TABLE orders (id INT)"), + (.mariadb, "CREATE OR REPLACE TABLE orders (id INT)"), + (.mariadb, "CREATE /* swap */ OR REPLACE\nTABLE orders (id INT)"), + (.mariadb, "CREATE OR REPLACE DATABASE shop"), + (.clickhouse, "CREATE OR REPLACE TABLE orders (id Int8) ENGINE = Memory"), + (.clickhouse, "REPLACE TABLE orders (id Int8) ENGINE = Memory"), + ]) + func replacingStoredDataIsDestructive(databaseType: DatabaseType, sql: String) { + #expect(QueryClassifier.classifyTier(sql, databaseType: databaseType) == .destructive) + #expect(QueryClassifier.isDangerousQuery(sql, databaseType: databaseType)) + } + + @Test("Replacing a view or a routine, or creating a table, stays a write", arguments: [ + (DatabaseType.postgresql, "CREATE OR REPLACE VIEW v AS SELECT 1"), + (.postgresql, "CREATE OR REPLACE FUNCTION f() RETURNS int AS $$ SELECT 1 $$ LANGUAGE sql"), + (.snowflake, "CREATE OR REPLACE VIEW \"TABLE\" AS SELECT 1"), + (.snowflake, "CREATE TABLE orders (id INT)"), + (.snowflake, "CREATE OR REPLACE STREAM s ON TABLE orders"), + (.oracle, "CREATE OR REPLACE TYPE ids AS TABLE OF NUMBER"), + (.duckdb, "CREATE OR REPLACE MACRO m(x) AS TABLE SELECT x"), + (.mysql, "REPLACE INTO orders (id) VALUES (1)"), + ]) + func losingNoRowsStaysAWrite(databaseType: DatabaseType, sql: String) { + #expect(QueryClassifier.classifyTier(sql, databaseType: databaseType) == .write) + } + + @Test("A caller that may not send DROP TABLE may not send CREATE OR REPLACE TABLE") + func gateRefusesItWithoutTheDestructiveConfirmation() { + let statement = ExternalStatementGate.Statement( + sql: "CREATE OR REPLACE TABLE orders (id INT)", + connectionId: UUID(), + databaseType: .snowflake, + externalAccess: .readWrite, + loadsExtensions: false, + allowsDestructive: false + ) + #expect(throws: ExternalStatementGateError.self) { + try ExternalStatementGate.classify(statement) + } + } +} diff --git a/apple/TableProTests/Core/Utilities/SQL/QueryClassifierHardeningTests.swift b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierHardeningTests.swift index aeb52f8d45..15c1c9ec33 100644 --- a/apple/TableProTests/Core/Utilities/SQL/QueryClassifierHardeningTests.swift +++ b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierHardeningTests.swift @@ -120,6 +120,35 @@ struct QueryClassifierFailClosedTests { #expect(QueryClassifier.isWriteQuery(statement, databaseType: .mysql)) } + @Test("T-SQL's BULK loads a file the server can reach", arguments: [ + "BULK INSERT dbo.staging FROM '\\\\198.51.100.7\\share\\rows.txt'", + "SELECT 1 BULK INSERT dbo.staging FROM 'C:\\rows.txt'", + "SELECT BulkColumn FROM OPENROWSET(BULK 'C:\\inetpub\\wwwroot\\web.config', SINGLE_CLOB) AS f", + "INSERT INTO dbo.staging SELECT BulkColumn FROM OPENROWSET ( BULK 'C:\\web.config', SINGLE_BLOB) AS f", + ]) + func bulkLoadsAreFlagged(statement: String) { + #expect(QueryClassifier.reachesFilesystemOrExecutesCode(statement, databaseType: .mssql)) + #expect(QueryClassifier.isWriteQuery(statement, databaseType: .mssql)) + } + + @Test("T-SQL's ad hoc data sources open the file or the server the statement names", arguments: [ + "SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Text;Database=C:\\logs', 'SELECT * FROM app.txt')", + "SELECT * FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0', 'Data Source=C:\\a.xlsx')...[Sheet1$]", + ]) + func adHocDataSourcesAreFlagged(statement: String) { + #expect(QueryClassifier.reachesFilesystemOrExecutesCode(statement, databaseType: .mssql)) + } + + @Test("A word that only resembles T-SQL's BULK reaches no file", arguments: [ + "GRANT ADMINISTER BULK OPERATIONS TO loader", + "ALTER DATABASE app SET RECOVERY BULK_LOGGED", + "SELECT [bulk], bulk_id, 'BULK INSERT' FROM dbo.orders", + "SELECT [openrowset] FROM dbo.orders", + ]) + func bulkLookalikesAreNotFlagged(statement: String) { + #expect(!QueryClassifier.reachesFilesystemOrExecutesCode(statement, databaseType: .mssql)) + } + @Test("Ordinary reads stay safe and never claim to reach the filesystem") func readsStaySafe() { let reads = [ diff --git a/apple/TableProTests/Core/Utilities/SQL/QueryClassifierUnfilteredDeleteTests.swift b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierUnfilteredDeleteTests.swift index f36a93b4a9..e9a8bbfbae 100644 --- a/apple/TableProTests/Core/Utilities/SQL/QueryClassifierUnfilteredDeleteTests.swift +++ b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierUnfilteredDeleteTests.swift @@ -7,6 +7,9 @@ struct QueryClassifierUnfilteredDeleteTests { "WITH old AS (SELECT id FROM archive WHERE id > 0) DELETE FROM orders", "DELETE FROM orders USING (SELECT id FROM archive WHERE id > 0) a", "DELETE FROM orders RETURNING (SELECT 1 WHERE true)", + "WITH merge\u{20AC} AS (SELECT 1) DELETE FROM orders", + "WITH U&\"gone\" AS (DELETE FROM orders RETURNING *) SELECT count(*) FROM gone", + "WITH RECURSIVE walk(id) AS (SELECT 1) SEARCH DEPTH FIRST BY id SET ord, gone AS (DELETE FROM orders) SELECT 1", ]) func deleteWithoutItsOwnWhereIsDangerous(sql: String) { #expect(QueryClassifier.isDangerousQuery(sql, databaseType: .postgresql)) @@ -21,6 +24,43 @@ struct QueryClassifierUnfilteredDeleteTests { #expect(!QueryClassifier.isDangerousQuery(sql, databaseType: .postgresql)) } + @Test("A DELETE whose WHERE the engine reads as a name still asks first") + func whereReadAsANameIsDangerous() { + let alias = "DELETE FROM orders AS o\u{A0}WHERE\u{A0}id\u{A0}IS\u{A0}NULL" + #expect(QueryClassifier.isDangerousQuery(alias, databaseType: .sqlite)) + #expect(QueryClassifier.isDangerousQuery("DELETE FROM orders RETURNING id AS where", databaseType: .postgresql)) + #expect(QueryClassifier.isDangerousQuery("DELETE FROM orders AS 1where", databaseType: .mysql)) + #expect(!QueryClassifier.isDangerousQuery("DELETE FROM orders AS o WHERE id IS NULL", databaseType: .sqlite)) + } + + @Test("An EXPLAIN that runs a DELETE with no WHERE asks first, as the DELETE does", arguments: [ + ("EXPLAIN ANALYZE DELETE FROM orders", DatabaseType.postgresql), + ("EXPLAIN ANALYSE DELETE FROM orders", .postgresql), + ("EXPLAIN (ANALYZE, FORMAT JSON) DELETE FROM orders", .postgresql), + ("EXPLAIN ANALYZE WITH gone AS (DELETE FROM orders RETURNING *) SELECT * FROM gone", .postgresql), + ("ANALYZE DELETE FROM orders", .mariadb), + ("ANALYZE FORMAT=JSON DELETE FROM orders", .mariadb), + ("EXPLAIN (ANALYZE TRUE) DELETE FROM orders", .postgresql), + ("EXPLAIN (ANALYZE OFF, ANALYZE) DELETE FROM orders", .postgresql), + ("EXPLAIN (ANALYZE OFFSET) DELETE FROM orders", .postgresql), + ]) + func explainThatRunsAnUnfilteredDeleteIsDangerous(sql: String, databaseType: DatabaseType) { + #expect(QueryClassifier.isDangerousQuery(sql, databaseType: databaseType)) + } + + @Test("An EXPLAIN that only plans the DELETE, or runs one with a WHERE, does not", arguments: [ + "EXPLAIN DELETE FROM orders", + "EXPLAIN (FORMAT JSON) DELETE FROM orders", + "EXPLAIN ANALYZE DELETE FROM orders WHERE id = 1", + "EXPLAIN (ANALYZE FALSE) DELETE FROM orders", + "EXPLAIN (ANALYZE OFF, FORMAT JSON) DELETE FROM orders", + "EXPLAIN (ANALYZE 0) DELETE FROM orders", + "ANALYZE orders", + ]) + func explainThatDeletesNothingUnfilteredIsNotDangerous(sql: String) { + #expect(!QueryClassifier.isDangerousQuery(sql, databaseType: .postgresql)) + } + @Test("On SQL Server a WHERE of the statement after the DELETE is not the DELETE's") func sqlServerFollowingStatementWhere() { #expect(QueryClassifier.isDangerousQuery("DELETE FROM [orders] SELECT 1 WHERE 1 = 1", databaseType: .mssql)) diff --git a/apple/changelog.d/3907.fixed.1.md b/apple/changelog.d/3907.fixed.1.md new file mode 100644 index 0000000000..5da836bb9a --- /dev/null +++ b/apple/changelog.d/3907.fixed.1.md @@ -0,0 +1 @@ +CREATE OR REPLACE TABLE, SCHEMA or DATABASE replacing data without the confirmation DROP gets. diff --git a/apple/changelog.d/3907.fixed.2.md b/apple/changelog.d/3907.fixed.2.md new file mode 100644 index 0000000000..1329b70e54 --- /dev/null +++ b/apple/changelog.d/3907.fixed.2.md @@ -0,0 +1 @@ +T-SQL BULK INSERT and OPENROWSET, and MongoDB $_internalJs operators, passing the external file and code refusals. diff --git a/apple/changelog.d/3907.fixed.3.md b/apple/changelog.d/3907.fixed.3.md new file mode 100644 index 0000000000..133900ff8a --- /dev/null +++ b/apple/changelog.d/3907.fixed.3.md @@ -0,0 +1 @@ +DuckDB's Open the File Read-Only being ignored for a database not named .duckdb or .ddb. diff --git a/apple/changelog.d/3907.fixed.4.md b/apple/changelog.d/3907.fixed.4.md new file mode 100644 index 0000000000..544e89f5b0 --- /dev/null +++ b/apple/changelog.d/3907.fixed.4.md @@ -0,0 +1 @@ +Spanner, BigQuery and Teradata saving a grid change they could not key to the default schema's table. diff --git a/apple/changelog.d/3907.fixed.5.md b/apple/changelog.d/3907.fixed.5.md new file mode 100644 index 0000000000..02abc9831d --- /dev/null +++ b/apple/changelog.d/3907.fixed.5.md @@ -0,0 +1 @@ +HTTP drivers such as Typesense following a server's redirect to another address with the connection's key. diff --git a/apple/changelog.d/3907.fixed.6.md b/apple/changelog.d/3907.fixed.6.md new file mode 100644 index 0000000000..ff30f90824 --- /dev/null +++ b/apple/changelog.d/3907.fixed.6.md @@ -0,0 +1 @@ +Bigtable saving a grid change to another row when the key starts with a combining mark. diff --git a/apple/changelog.d/3907.fixed.7.md b/apple/changelog.d/3907.fixed.7.md new file mode 100644 index 0000000000..20cbef3d28 --- /dev/null +++ b/apple/changelog.d/3907.fixed.7.md @@ -0,0 +1 @@ +Teradata showing each result one statement late after a statement hit the 20-second receive timeout. diff --git a/apple/changelog.d/3907.fixed.md b/apple/changelog.d/3907.fixed.md new file mode 100644 index 0000000000..09d461ca54 --- /dev/null +++ b/apple/changelog.d/3907.fixed.md @@ -0,0 +1 @@ +A DELETE with no WHERE running unconfirmed behind EXPLAIN ANALYZE, an unusual CTE, or a WHERE glued to a name. diff --git a/apple/project.yml b/apple/project.yml index df769c7e51..8807ce036b 100644 --- a/apple/project.yml +++ b/apple/project.yml @@ -91,6 +91,7 @@ targetTemplates: - Plugins/Shared/PluginConnectTimeout.swift - Plugins/Shared/PluginQueryTimeout.swift - Plugins/Shared/PluginURLSessionTaskBox.swift + - Plugins/Shared/PluginRedirectRefusingDelegate.swift dependencies: - target: TableProPluginKit embed: false diff --git a/apple/scripts/probes/check-duckdb-access-mode.sh b/apple/scripts/probes/check-duckdb-access-mode.sh new file mode 100755 index 0000000000..8b75e25807 --- /dev/null +++ b/apple/scripts/probes/check-duckdb-access-mode.sh @@ -0,0 +1,107 @@ +#!/usr/bin/env bash +# +# Measures which paths libduckdb opens with access_mode=READ_ONLY, the facts DuckDBFileKinds +# and DuckDBAccessMode hard-code. A database file honours it whatever it is called; a reader +# (CSV, TSV, JSON, JSON Lines, Parquet, also gzip or zstd compressed), an empty path and +# :memory: refuse it. Run after bumping libduckdb. +# +set -euo pipefail + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +HEADERS="$ROOT/Plugins/DuckDBDriverPlugin/CDuckDB/include" +LIB="$ROOT/Libs/libduckdb.a" + +if [ ! -f "$LIB" ]; then + echo "error: $LIB is missing. Run scripts/download-libs.sh first." >&2 + exit 1 +fi + +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +cat > "$WORK/probe.c" <<'PROBE' +#include +#include +#include "duckdb.h" + +static int open_db(const char *path, int read_only, duckdb_database *db, char **error) { + duckdb_config config = NULL; + if (read_only) { + duckdb_create_config(&config); + duckdb_set_config(config, "access_mode", "READ_ONLY"); + } + duckdb_state state = duckdb_open_ext(path, db, config, error); + if (config) duckdb_destroy_config(&config); + return state == DuckDBSuccess; +} + +int main(int argc, char **argv) { + if (argc > 2 && strcmp(argv[1], "create") == 0) { + duckdb_database db; + duckdb_connection con; + char *error = NULL; + if (!open_db(argv[2], 0, &db, &error)) return 2; + duckdb_connect(db, &con); + duckdb_query(con, "CREATE TABLE t AS SELECT 1 AS a", NULL); + duckdb_query(con, "COPY (SELECT 1 AS a) TO 'data.parquet' (FORMAT parquet)", NULL); + duckdb_disconnect(&con); + duckdb_close(&db); + return 0; + } + for (int i = 1; i < argc; i++) { + duckdb_database db; + char *error = NULL; + if (!open_db(argv[i], 1, &db, &error)) { + printf("%s\trefused\n", argv[i]); + if (error) duckdb_free(error); + continue; + } + duckdb_connection con; + duckdb_connect(db, &con); + duckdb_result result; + int wrote = duckdb_query(con, "CREATE TABLE written(a INTEGER)", &result) == DuckDBSuccess; + duckdb_destroy_result(&result); + printf("%s\t%s\n", argv[i], wrote ? "WRITABLE" : "read-only"); + duckdb_disconnect(&con); + duckdb_close(&db); + } + return 0; +} +PROBE + +cc -O1 -I "$HEADERS" "$WORK/probe.c" "$LIB" -lc++ -o "$WORK/probe" + +cd "$WORK" +./probe create warehouse.duckdb +cp warehouse.duckdb warehouse.db +cp warehouse.duckdb warehouse +cp warehouse.duckdb warehouse.duckdb.bak +/usr/bin/sqlite3 app.sqlite "CREATE TABLE s(a); INSERT INTO s VALUES (1);" +cp app.sqlite app.db +printf 'a,b\n1,2\n' > rows.csv +printf 'a\tb\n1\t2\n' > rows.tsv +printf '[{"a":1}]' > rows.json +printf '{"a":1}\n' > rows.jsonl +cp rows.jsonl rows.ndjson +cp rows.csv UPPER.CSV +gzip -k rows.csv +gzip -k rows.jsonl + +databases=(warehouse.duckdb warehouse.db warehouse warehouse.duckdb.bak app.sqlite app.db) +readers=(rows.csv rows.tsv rows.json rows.jsonl rows.ndjson data.parquet UPPER.CSV rows.csv.gz rows.jsonl.gz "" ":memory:") + +./probe "${databases[@]}" "${readers[@]}" > measured.tsv +cat measured.tsv + +failed=0 +for path in "${databases[@]}"; do + grep -qxF "$path read-only" measured.tsv || { echo "MISMATCH: $path should open read-only" >&2; failed=1; } +done +for path in "${readers[@]}"; do + grep -qxF "$path refused" measured.tsv || { echo "MISMATCH: $path should refuse READ_ONLY" >&2; failed=1; } +done +if [ "$failed" -ne 0 ]; then + echo "FAIL: update DuckDBFileKinds.opensAsReader and DuckDBAccessMode.carriesAccessMode." >&2 + exit 1 +fi +echo "OK: libduckdb honours READ_ONLY on every database file and refuses it on every reader." From b73f289272fdc4c064b7a1fc8ab613c778fab3e7 Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 15:22:05 +0100 Subject: [PATCH 26/85] fix(linux-grid): carry the header and rows to the grid's edge and let the last column resize (#3894) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3894.fixed.1.md | 1 + linux/changelog.d/3894.fixed.md | 1 + linux/crates/app/src/ui/data_grid/columns.rs | 45 +++++++++++++++++ linux/crates/app/src/ui/data_grid/pointer.rs | 19 ++++++- .../app/src/ui/data_grid/tests/columns.rs | 50 +++++++++++++++++++ .../app/src/ui/data_grid/tests/layout.rs | 29 ++++++++++- .../app/src/ui/data_grid/tests/sorting.rs | 2 +- .../crates/app/src/ui/data_grid/tests/view.rs | 26 +++++++++- 8 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 linux/changelog.d/3894.fixed.1.md create mode 100644 linux/changelog.d/3894.fixed.md diff --git a/linux/changelog.d/3894.fixed.1.md b/linux/changelog.d/3894.fixed.1.md new file mode 100644 index 0000000000..906d7c8190 --- /dev/null +++ b/linux/changelog.d/3894.fixed.1.md @@ -0,0 +1 @@ +The grid's last column could not be resized by dragging or double-clicking the edge of its header. diff --git a/linux/changelog.d/3894.fixed.md b/linux/changelog.d/3894.fixed.md new file mode 100644 index 0000000000..63e4793ff3 --- /dev/null +++ b/linux/changelog.d/3894.fixed.md @@ -0,0 +1 @@ +In a window wider than the table, the grid's header, row lines and selection stopped at the last column. diff --git a/linux/crates/app/src/ui/data_grid/columns.rs b/linux/crates/app/src/ui/data_grid/columns.rs index ee0b7f5043..f4c625a633 100644 --- a/linux/crates/app/src/ui/data_grid/columns.rs +++ b/linux/crates/app/src/ui/data_grid/columns.rs @@ -56,6 +56,7 @@ pub struct ColumnSet { view_columns: Vec, paints: Vec>, row_numbers: Option, + filler: Option, sorting: Option, /// The order, visibility and widths as the user left them. layout: ColumnLayout, @@ -161,6 +162,9 @@ impl DataGrid { view.append_column(&column); } } + let filler = filler_column(); + view.append_column(&filler); + built.filler = Some(filler); self.imp().columns.replace(built); self.size_unsized_columns(); self.watch_layout_changes(); @@ -376,10 +380,36 @@ impl DataGrid { /// A column moved, went or came back: the layout keeps it, and a /// search counts only the columns on screen, in their order. fn columns_moved(&self) { + self.keep_filler_last(); self.record_layout(); self.find_rows_changed(); } + // GTK drops a header dragged onto the filler after it, and lets the + // filler's own header be dragged. + fn keep_filler_last(&self) { + let Some(filler) = self.imp().columns.borrow().filler.clone() else { + return; + }; + let view = self.column_view(); + if !out_of_place(&view, &filler) { + return; + } + // GTK places the moved column's widgets after the list announces + // the move, so a move from in here would leave them out of order. + glib::idle_add_local_once(glib::clone!( + #[weak] + view, + #[weak] + filler, + move || { + if out_of_place(&view, &filler) { + view.insert_column(view.columns().n_items().saturating_sub(1), &filler); + } + } + )); + } + /// Fold what the view shows into the stored layout. A column the rows /// lack this time keeps its place, width and hidden flag for when it /// is back. @@ -650,6 +680,21 @@ impl DataGrid { } } +// GTK makes the header and rows only as wide as the columns unless one +// expands, and never resizes the last column; this one takes both. +fn filler_column() -> gtk::ColumnViewColumn { + let column = gtk::ColumnViewColumn::builder().expand(true).build(); + column.set_id(Some("filler")); + column +} + +// The idle can run after a rebuild dropped this filler from the view. +fn out_of_place(view: >k::ColumnView, filler: >k::ColumnViewColumn) -> bool { + let columns = view.columns(); + let last = columns.item(columns.n_items().saturating_sub(1)); + filler.column_view().as_ref() == Some(view) && last.as_ref() != Some(filler.upcast_ref()) +} + fn same_names(layout: &ColumnLayout, columns: &[GridColumn]) -> bool { let names: Vec<&str> = layout .order diff --git a/linux/crates/app/src/ui/data_grid/pointer.rs b/linux/crates/app/src/ui/data_grid/pointer.rs index a0274b900c..defc278e3e 100644 --- a/linux/crates/app/src/ui/data_grid/pointer.rs +++ b/linux/crates/app/src/ui/data_grid/pointer.rs @@ -6,7 +6,8 @@ use super::{DataGrid, GridCell, GridRow}; /// Where a click landed in the view. enum Hit { Cell(GridCell), - /// A row outside its data cells: the row number column. + /// A row outside its data cells: the row number column or the space + /// after the last column. Row(GridRow, u32), /// The list below its last row. Empty, @@ -168,6 +169,22 @@ mod tests { (f64::from(at.x()), f64::from(at.y())) } + /// A right click there opens the row's menu, as one on its number does. + #[gtk4::test] + fn a_click_after_the_last_column_is_on_the_row() { + let grid = Grid::open(two_text_columns(), two_text_values()); + let row = grid + .cell_of(2, 1) + .parent() + .and_then(|cell| cell.parent()) + .expect("the row"); + let after = row.last_child().expect("the row's last cell"); + + let (x, y) = point_in(&grid, &after); + + assert!(matches!(hit_at(&grid.grid, x, y), Hit::Row(_, 1))); + } + /// GtkText's own menu cuts, copies and pastes in the open editor. #[gtk4::test] fn a_right_click_in_the_open_editor_is_left_to_it() { diff --git a/linux/crates/app/src/ui/data_grid/tests/columns.rs b/linux/crates/app/src/ui/data_grid/tests/columns.rs index 0094797561..a97bfebcc1 100644 --- a/linux/crates/app/src/ui/data_grid/tests/columns.rs +++ b/linux/crates/app/src/ui/data_grid/tests/columns.rs @@ -542,3 +542,53 @@ fn a_cleared_width_in_an_empty_table_returns_to_its_types_width() { wait_until(WAIT, || column.fixed_width() > 0).expect("the grid gives the column a width again"); assert_eq!(column.fixed_width(), typed); } + +/// These click on the X display every test shares; nextest runs them +/// with no other test beside them. +mod real_keys { + use gtk4 as gtk; + use gtk4::prelude::*; + + use super::super::{Layouts, data_column, laid_out, settled_width}; + use super::saved_to; + use crate::test_support::{KeyInjector, descendants, wait_until}; + use crate::ui::data_grid::test_grid::WAIT; + + fn header_of(header: >k::Widget, name: &str) -> gtk::Widget { + std::iter::successors(header.first_child(), |title| title.next_sibling()) + .find(|title| { + descendants(title).iter().any(|widget| { + widget + .downcast_ref::() + .is_some_and(|label| label.label() == name) + }) + }) + .expect("the column's header") + } + + /// GTK resizes no column from the view's last edge, which was the last + /// data column's. + #[gtk4::test] + fn a_double_click_on_the_last_columns_edge_gives_it_the_grids_width() { + let layouts = Layouts::new(); + let grid = laid_out(saved_to(&layouts)); + KeyInjector::focus_only(&grid.window); + wait_until(WAIT, || grid.window.is_active()).expect("the window to take the focus"); + let mouse = KeyInjector::connect().expect("an X11 display"); + let column = data_column(&grid, 1); + column.set_fixed_width(300); + settled_width(&grid, 1); + let header = grid.grid.column_view().first_child().expect("the header"); + let bounds = header_of(&header, "name") + .compute_bounds(&header) + .expect("the column's header bounds"); + + // Where GTK looks for the edge: the header's origin plus the + // column's width. + let edge = (bounds.x() + 300.0, bounds.y() + bounds.height() / 2.0); + mouse.click(&header, edge, 2).expect("the double click"); + + wait_until(WAIT, || column.fixed_width() > 0 && column.fixed_width() != 300).expect("the grid's width"); + assert_eq!(layouts.saved("t").widths.get("name"), None); + } +} diff --git a/linux/crates/app/src/ui/data_grid/tests/layout.rs b/linux/crates/app/src/ui/data_grid/tests/layout.rs index e3320609a5..1a56de6417 100644 --- a/linux/crates/app/src/ui/data_grid/tests/layout.rs +++ b/linux/crates/app/src/ui/data_grid/tests/layout.rs @@ -6,8 +6,8 @@ use tablepro_core::{ColumnInfo, Value}; use super::{Layouts, data_column, text}; use crate::services::grid::{ColumnLayout, LayoutSlot}; -use crate::test_support::drain_main_context; -use crate::ui::data_grid::test_grid::{Grid, GridSpec, named, persisted, two_text_columns, two_text_values}; +use crate::test_support::{drain_main_context, wait_until}; +use crate::ui::data_grid::test_grid::{Grid, GridSpec, WAIT, named, persisted, two_text_columns, two_text_values}; fn users() -> Vec { vec![ @@ -126,6 +126,31 @@ fn a_column_missing_from_one_read_keeps_its_entry() { assert_eq!(data_column(&all, 2).fixed_width(), 222); } +/// GTK drops a header let go over the space after the last column after +/// the filler that holds that space, and lets the filler be dragged too. +#[gtk4::test] +fn the_space_after_the_last_column_stays_last() { + let layouts = Layouts::new(); + let grid = open_users(&layouts, "users"); + let view = grid.grid.column_view(); + let last = || { + view.columns() + .item(view.columns().n_items() - 1) + .and_downcast::() + .expect("a column") + }; + let filler = last(); + + view.insert_column(position_in_view(&view, &filler), &data_column(&grid, 0)); + wait_until(WAIT, || last() == filler).expect("the filler back at the end"); + assert_eq!(grid.grid.shown_columns(), [1, 2, 0]); + assert_eq!(layouts.saved("users").order.last(), Some(&slot("id"))); + + view.insert_column(1, &filler); + wait_until(WAIT, || last() == filler).expect("the filler back at the end"); + assert_eq!(grid.grid.shown_columns(), [1, 2, 0]); +} + /// A grid of no columns has nothing to focus and no header to bring one /// back from. #[gtk4::test] diff --git a/linux/crates/app/src/ui/data_grid/tests/sorting.rs b/linux/crates/app/src/ui/data_grid/tests/sorting.rs index 029b23173b..aa166d6164 100644 --- a/linux/crates/app/src/ui/data_grid/tests/sorting.rs +++ b/linux/crates/app/src/ui/data_grid/tests/sorting.rs @@ -327,7 +327,7 @@ fn no_header_sorts_a_table_in_the_engines_order() { .map(|column| column.sorter().is_some()) .collect(); - assert_eq!(sorters, [false, false, false]); + assert_eq!(sorters, [false; 4]); } /// D4: a field no sort can name, as a document's nested field, sorts by diff --git a/linux/crates/app/src/ui/data_grid/tests/view.rs b/linux/crates/app/src/ui/data_grid/tests/view.rs index e0ba1a8099..fcc9d4fab6 100644 --- a/linux/crates/app/src/ui/data_grid/tests/view.rs +++ b/linux/crates/app/src/ui/data_grid/tests/view.rs @@ -4,7 +4,7 @@ use gtk4::prelude::*; use gtk4::subclass::prelude::ObjectSubclassIsExt; use tablepro_core::Value; -use super::{keyed, text}; +use super::{keyed, settled_width, text}; use crate::test_support::wait_until; use crate::ui::data_grid::GridCell; use crate::ui::data_grid::test_grid::{Grid, GridSpec, WAIT, two_text_columns}; @@ -56,6 +56,30 @@ fn the_empty_page_shows_over_the_view_and_goes_with_the_first_row() { assert_eq!(empty.description().as_deref(), Some("Change the filter")); } +/// GTK makes the header and rows only as wide as the columns unless one +/// expands, so a selection stopped at the last column. +#[gtk4::test] +fn the_header_and_rows_reach_the_grids_edge_and_the_columns_keep_their_width() { + let grid = Grid::build(GridSpec::table(two_text_columns()), keyed(&[1, 2])); + let view = grid.grid.column_view(); + let header = view.first_child().expect("the header"); + let row = grid + .cell_of(1, 1) + .parent() + .and_then(|cell| cell.parent()) + .expect("the row"); + let reaches_edge = |widget: >k::Widget| { + widget + .compute_bounds(&view) + .is_some_and(|bounds| (bounds.x() + bounds.width() - view.width() as f32).abs() < 1.0) + }; + + wait_until(WAIT, || reaches_edge(&header) && reaches_edge(&row)).expect("the header and rows as wide as the grid"); + for index in 0..2 { + settled_width(&grid, index); + } +} + /// The tooltip quotes the whole text, up to 300 characters; GTK keeps /// no text a test can read back, so this checks only that it shows. #[gtk4::test] From 2bb09bc7402d1b2a16f6be33fbe257380eba4cbc Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 15:31:33 +0100 Subject: [PATCH 27/85] feat(linux-session): open a connection's session in another of the server's databases (#3900) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- .../session/src/connect/connect_request.rs | 12 ++ linux/crates/session/src/session/session.rs | 114 +++++++++++++++++- 2 files changed, 122 insertions(+), 4 deletions(-) diff --git a/linux/crates/session/src/connect/connect_request.rs b/linux/crates/session/src/connect/connect_request.rs index 6063d1a2da..d2ca81631f 100644 --- a/linux/crates/session/src/connect/connect_request.rs +++ b/linux/crates/session/src/connect/connect_request.rs @@ -25,6 +25,18 @@ impl ConnectRequest { self.workspace_settings.read_only = true; } + /// The same server, user and settings opening another of the server's + /// databases, for a window that follows a query tab there. `None` for + /// a file, which is the one database it opens. + pub fn in_database(&self, database: &str) -> Option { + let mut request = self.for_reconnect(); + match &mut request.target { + RequestTarget::Network { database: opened, .. } => *opened = Some(database.to_owned()), + RequestTarget::File(_) => return None, + } + Some(request) + } + /// The request a reconnect sends: the same server, user and /// settings, never a retarget. pub fn for_reconnect(&self) -> Self { diff --git a/linux/crates/session/src/session/session.rs b/linux/crates/session/src/session/session.rs index 8c53bbef2b..a2edaf40f1 100644 --- a/linux/crates/session/src/session/session.rs +++ b/linux/crates/session/src/session/session.rs @@ -2,18 +2,21 @@ use std::future::Future; use std::sync::{Arc, Mutex, PoisonError}; use std::time::Duration; +use tablepro_core::credentials::CredentialInteraction; use tablepro_core::dialect::SqlDialect; use tablepro_core::meta::{ObjectInfo, ObjectScope, SchemaInfo}; use tablepro_core::{ - CallOptions, ColumnInfo, EditorSession, EditorSessionOptions, SafeModeLevel, TableRef, WorkspaceSettings, + CallOptions, ColumnInfo, DriverError, EditorSession, EditorSessionOptions, SafeModeLevel, TableRef, + UnsupportedFeature, WorkspaceSettings, }; +use tablepro_storage::CredentialDemand; use tokio::sync::watch; use tokio_util::sync::{CancellationToken, DropGuard}; use tokio_util::task::AbortOnDropHandle; use uuid::Uuid; use super::{ObjectListSnapshot, SchemaCatalog, SessionDeps}; -use crate::connect::{ConnectError, ConnectionMetadata, PendingSession, RequestTarget}; +use crate::connect::{ConnectError, ConnectRequest, ConnectionMetadata, PendingSession, RequestTarget}; use crate::health::{ LinkGeneration, MonitorContext, MonitorMailbox, MonitorSignal, ReachabilityTarget, ReconnectPlan, ReviewedHostKey, SessionHealth, run_monitor, @@ -41,6 +44,9 @@ pub struct Session { /// The database the connection was asked to open, as the user named /// it. database: Option, + /// What opens this connection's sessions in the server's other + /// databases: its request and the app's parts, as a reconnect has them. + origin: SessionOrigin, runner: OperationRunner, mailbox: Arc, health: watch::Receiver, @@ -53,9 +59,20 @@ pub struct Session { _monitor: AbortOnDropHandle<()>, } +/// What a session was opened with, kept to open its siblings in the +/// server's other databases. +struct SessionOrigin { + request: ConnectRequest, + deps: SessionDeps, +} + impl Session { pub fn start(pending: PendingSession, deps: SessionDeps) -> Arc { let parts = pending.into_parts(); + let origin = SessionOrigin { + request: parts.request.for_reconnect(), + deps: deps.clone(), + }; let close_timeout = deps .deadlines .deadline(OperationKind::Close) @@ -116,6 +133,7 @@ impl Session { trusted_host_key, ssh, database, + origin, runner, mailbox, health, @@ -263,6 +281,41 @@ impl Session { self.database.as_deref() } + /// A session of this connection in another of the server's databases, + /// for a window that follows a query tab there. Opened as a reconnect + /// is: the stored secret, never a prompt. A file has no other + /// database. + pub async fn open_in_database( + &self, + database: &str, + cancel: CancellationToken, + ) -> Result, ConnectError> { + let request = + self.origin + .request + .in_database(database) + .ok_or(ConnectError::Driver(DriverError::Unsupported { + feature: UnsupportedFeature::NetworkConnection, + }))?; + let refreshed = self + .origin + .deps + .credentials + .refresh(request, CredentialInteraction::Unattended, CredentialDemand::default()) + .await?; + let pending = self + .origin + .deps + .connector + .establish(refreshed.request, CredentialInteraction::Unattended, cancel) + .await?; + let sibling = Session::start(pending, self.origin.deps.clone()); + // The level the user chose since the connection opened, which a + // read-only connection's sibling shares by opening read-only too. + sibling.set_safe_mode(self.safe_mode()); + Ok(sibling) + } + /// The SSH tunnel the connection was made through, if any. pub fn ssh_config(&self) -> Option<&tablepro_ssh::SshConfig> { self.ssh.as_ref() @@ -369,7 +422,7 @@ mod tests { }; use super::*; - use crate::fakes::{FakeConnector, FakeCredentialSource}; + use crate::fakes::{FakeConnector, FakeCredentialSource, FakeEstablish}; use crate::operation::DefaultDeadlines; use crate::test_support::paused_tasks; @@ -429,7 +482,8 @@ mod tests { let session = Session::start(pending, deps(credentials.clone())); let weak: Weak = Arc::downgrade(&session); let mut health = session.health(); - assert_eq!(Arc::strong_count(&credentials), 2, "the monitor holds no plan"); + // The test's, the monitor's, and the session's own for its siblings. + assert_eq!(Arc::strong_count(&credentials), 3, "the monitor holds no plan"); drop(session); for _ in 0..10 { @@ -468,6 +522,58 @@ mod tests { assert_eq!(count, 7); } + /// A window following a query tab to another database opens a sibling + /// session there: the same server and user, the other database, the + /// stored secret and no prompt. + #[tokio::test(start_paused = true)] + async fn a_sibling_session_opens_in_another_database() { + let connector = FakeConnector::new([]).always(FakeEstablish::Connect(ConnectionScript::default())); + let mut request = FakeConnector::network_request("db.example", 5432).unwrap(); + if let RequestTarget::Network { database, .. } = &mut request.target { + *database = Some("shop".to_owned()); + } + let (pending, _) = FakeConnector::pending_session(request, ConnectionScript::default()); + let session = Session::start( + pending, + SessionDeps { + connector: Arc::new(connector.clone()), + ..deps(Arc::new(FakeCredentialSource::new())) + }, + ); + + assert!(session.set_safe_mode(SafeModeLevel::ConfirmWrites)); + + let sibling = session + .open_in_database("analytics", CancellationToken::new()) + .await + .expect("a session in analytics"); + + assert_eq!( + sibling.safe_mode(), + SafeModeLevel::ConfirmWrites, + "the sibling dropped the level" + ); + assert_eq!(session.database(), Some("shop")); + assert_eq!(sibling.database(), Some("analytics")); + let call = connector.calls().pop().expect("an attempt"); + assert!(!call.attended, "the sibling asked for a secret"); + assert_eq!(call.request.metadata.id, session.id(), "another connection's request"); + } + + #[tokio::test(start_paused = true)] + async fn a_file_has_no_other_database_to_open() { + let (session, _) = start(ConnectionScript::default()); + + let refused = session.open_in_database("analytics", CancellationToken::new()).await; + + assert!(matches!( + refused, + Err(ConnectError::Driver(DriverError::Unsupported { + feature: UnsupportedFeature::NetworkConnection + })) + )); + } + #[tokio::test(start_paused = true)] async fn read_only_and_dialect_follow_the_current_connection() { let (session, _) = start(ConnectionScript { From a81886fabfab1bea6d312e062362fef91cf76c99 Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 16:46:34 +0100 Subject: [PATCH 28/85] fix(linux-grid): draw values in the monospace font and measure columns in the fonts they draw in (#3895) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/Cargo.lock | 1 + linux/changelog.d/3895.changed.md | 1 + linux/changelog.d/3895.fixed.1.md | 1 + linux/changelog.d/3895.fixed.md | 1 + linux/crates/app/Cargo.toml | 2 + .../app/src/services/grid/column_width.rs | 200 ++++++++++++------ linux/crates/app/src/services/grid/mod.rs | 2 +- linux/crates/app/src/ui/data_grid/columns.rs | 63 ++++-- .../app/src/ui/data_grid/edit/cell_editor.rs | 5 + .../app/src/ui/data_grid/edit/tests/mod.rs | 18 ++ .../crates/app/src/ui/data_grid/grid_cell.rs | 2 + .../crates/app/src/ui/data_grid/test_grid.rs | 3 + .../app/src/ui/data_grid/tests/columns.rs | 97 ++++++++- .../crates/app/src/ui/data_grid/tests/mod.rs | 5 +- 14 files changed, 314 insertions(+), 87 deletions(-) create mode 100644 linux/changelog.d/3895.changed.md create mode 100644 linux/changelog.d/3895.fixed.1.md create mode 100644 linux/changelog.d/3895.fixed.md diff --git a/linux/Cargo.lock b/linux/Cargo.lock index bfbb57eb5d..15b9a70649 100644 --- a/linux/Cargo.lock +++ b/linux/Cargo.lock @@ -5936,6 +5936,7 @@ dependencies = [ "tracing", "tracing-log", "tracing-subscriber", + "unicode-width", "uuid", "x11rb", ] diff --git a/linux/changelog.d/3895.changed.md b/linux/changelog.d/3895.changed.md new file mode 100644 index 0000000000..0cafa19e4b --- /dev/null +++ b/linux/changelog.d/3895.changed.md @@ -0,0 +1 @@ +The data grid draws values in the desktop's monospace font, so codes, times and numbers line up. diff --git a/linux/changelog.d/3895.fixed.1.md b/linux/changelog.d/3895.fixed.1.md new file mode 100644 index 0000000000..06c9a432f6 --- /dev/null +++ b/linux/changelog.d/3895.fixed.1.md @@ -0,0 +1 @@ +Editing a number in the data grid moved it from the right edge of its cell to the left. diff --git a/linux/changelog.d/3895.fixed.md b/linux/changelog.d/3895.fixed.md new file mode 100644 index 0000000000..e855ebc2c5 --- /dev/null +++ b/linux/changelog.d/3895.fixed.md @@ -0,0 +1 @@ +A data grid column could open too narrow for a value in Chinese or Japanese, and cut it. diff --git a/linux/crates/app/Cargo.toml b/linux/crates/app/Cargo.toml index 42d002c708..80417db3bb 100644 --- a/linux/crates/app/Cargo.toml +++ b/linux/crates/app/Cargo.toml @@ -96,6 +96,8 @@ tokio.workspace = true tokio-util.workspace = true tracing.workspace = true tracing-subscriber.workspace = true +# Cells a value takes in the grid's monospace font. +unicode-width.workspace = true uuid.workspace = true gettext-rs = { version = "0.8", features = ["gettext-system"] } # Counts grouped the way the user's locale groups digits. diff --git a/linux/crates/app/src/services/grid/column_width.rs b/linux/crates/app/src/services/grid/column_width.rs index c50660bfad..a0ce376e48 100644 --- a/linux/crates/app/src/services/grid/column_width.rs +++ b/linux/crates/app/src/services/grid/column_width.rs @@ -1,5 +1,6 @@ use tablepro_core::Value; use tablepro_core::column::{ColumnKind, TemporalZone, TextLength}; +use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; use super::display_text::display_text; use super::{CellDisplay, GridColumn}; @@ -14,24 +15,24 @@ const TYPICAL_CHARS: u32 = 20; /// A number of a type that sets no precision. const TYPICAL_DIGITS: u32 = 10; -/// A column's starting width in characters: its header or its widest -/// sampled value. Text is capped so one long value does not push the rest -/// off screen; a number or a date is never cut. +/// The monospace cells a column's values open at: its widest sampled +/// value. Text is capped so one long value does not push the rest off +/// screen; a number or a date is never cut. The header, drawn in another +/// font, is the caller's to measure. pub fn default_width_chars<'a>(column: &GridColumn, sample: impl IntoIterator) -> u32 { - let (header, widest) = header_and_widest(column, sample); - match column.display() { - CellDisplay::Bool => header.max(BOOL_CHARS), - CellDisplay::Number => header.max(widest), - CellDisplay::Temporal => header - .max(widest) - .max(spelled_chars(&column.column_type.kind).unwrap_or(0)), - CellDisplay::Text | CellDisplay::Json | CellDisplay::Binary => header.max(widest).min(TEXT_MAX_CHARS), - } + let widest = widest_cells(column, sample); + let cells = match column.display() { + CellDisplay::Bool => BOOL_CHARS, + CellDisplay::Number => widest, + CellDisplay::Temporal => widest.max(spelled_chars(&column.column_type.kind).unwrap_or(0)), + CellDisplay::Text | CellDisplay::Json | CellDisplay::Binary => widest.min(TEXT_MAX_CHARS), + }; + cells.max(MIN_CHARS) } -/// A column's starting width in characters in a table that holds no -/// rows: its header or a value its type leads one to expect. A bounded -/// type gives its widest value, text no wider than a name. +/// The monospace cells a column's values open at in a table that holds +/// no rows: a value its type leads one to expect. A bounded type gives +/// its widest value, text no wider than a name. pub fn typical_width_chars(column: &GridColumn) -> u32 { let kind = &column.column_type.kind; let typical = match kind { @@ -47,65 +48,67 @@ pub fn typical_width_chars(column: &GridColumn) -> u32 { } => (*length).min(TYPICAL_CHARS), _ => spelled_chars(kind).unwrap_or(TYPICAL_CHARS), }; - header_chars(column).max(typical).min(TEXT_MAX_CHARS) + typical.clamp(MIN_CHARS, TEXT_MAX_CHARS) } /// The values the column's type spells out, cut at the cap, for the -/// caller to measure in tabular figures: a count of average characters -/// runs short of digits and capitals. +/// caller to measure. pub fn typical_texts(column: &GridColumn) -> Vec { - let cap = usize::try_from(TEXT_MAX_CHARS).unwrap_or(usize::MAX); spelled(&column.column_type.kind) .into_iter() - .map(|text| text.chars().take(cap).collect()) + .map(|text| within_cap(text).to_owned()) .collect() } /// The width of a column that turned out to hold only numbers, as an /// untyped PostgreSQL result does: a number is never cut. pub fn number_width_chars<'a>(column: &GridColumn, sample: impl IntoIterator) -> u32 { - let (header, widest) = header_and_widest(column, sample); - header.max(widest) + widest_cells(column, sample).max(MIN_CHARS) } -fn header_chars(column: &GridColumn) -> u32 { - char_count(&column.name).clamp(MIN_CHARS, TEXT_MAX_CHARS) +/// The column's name as its header draws it, cut at the cap. +pub fn header_text(column: &GridColumn) -> &str { + within_cap(&column.name) } -fn header_and_widest<'a>(column: &GridColumn, sample: impl IntoIterator) -> (u32, u32) { +fn widest_cells<'a>(column: &GridColumn, sample: impl IntoIterator) -> u32 { let mut scratch = String::new(); - let widest = sample + sample .into_iter() .take(SAMPLE_ROWS) .filter(|value| !value.is_null()) - .map(|value| char_count(display_text(value, &column.column_type, &mut scratch).text)) + .map(|value| cells(display_text(value, &column.column_type, &mut scratch).text)) .max() - .unwrap_or(0); - (header_chars(column), widest) + .unwrap_or(0) } -/// The sampled value with the most characters, as the cell draws it and -/// cut at the cap: a count of average characters runs short of capitals -/// and digits, so the caller measures this one. A number column is -/// measured in tabular digits instead. -pub fn widest_text<'a>(column: &GridColumn, sample: impl IntoIterator) -> Option { +/// The sampled values for the caller to measure, as the cells draw them +/// and cut at the cap: of the plain ASCII ones, which a monospace font +/// draws one cell each, the one that takes the most cells, and every +/// other one, since its characters may draw in a fallback font. A number +/// column is measured in tabular digits instead. +pub fn sampled_texts<'a>(column: &GridColumn, sample: impl IntoIterator) -> Vec { if column.display() == CellDisplay::Number { - return None; + return Vec::new(); } - let cap = usize::try_from(TEXT_MAX_CHARS).unwrap_or(usize::MAX); let mut scratch = String::new(); - sample - .into_iter() - .take(SAMPLE_ROWS) - .filter(|value| !value.is_null()) - .map(|value| { - display_text(value, &column.column_type, &mut scratch) - .text - .chars() - .take(cap) - .collect::() - }) - .max_by_key(|text| char_count(text)) + let mut widest_plain: Option<(u32, String)> = None; + let mut others = Vec::new(); + for value in sample.into_iter().take(SAMPLE_ROWS).filter(|value| !value.is_null()) { + let text = within_cap(display_text(value, &column.column_type, &mut scratch).text); + if !is_plain(text) { + others.push(text.to_owned()); + continue; + } + let width = cells(text); + if widest_plain.as_ref().is_none_or(|(most, _)| width > *most) { + widest_plain = Some((width, text.to_owned())); + } + } + others.sort_unstable(); + others.dedup(); + others.extend(widest_plain.map(|(_, text)| text)); + others } /// The values of a type that holds nothing wider: a date or a time @@ -132,11 +135,32 @@ fn spelled(kind: &ColumnKind) -> Vec<&str> { } fn spelled_chars(kind: &ColumnKind) -> Option { - spelled(kind).into_iter().map(char_count).max() + spelled(kind).into_iter().map(cells).max() } -fn char_count(text: &str) -> u32 { - u32::try_from(text.chars().count()).unwrap_or(u32::MAX) +/// The cells `text` takes in a monospace font, where a Chinese or +/// Japanese character takes two. +fn cells(text: &str) -> u32 { + u32::try_from(text.width()).unwrap_or(u32::MAX) +} + +/// Printable ASCII, which a monospace font draws one cell a character. +fn is_plain(text: &str) -> bool { + text.chars() + .all(|character| character == ' ' || character.is_ascii_graphic()) +} + +/// The start of `text` that fits in the cap's cells. +fn within_cap(text: &str) -> &str { + let cap = usize::try_from(TEXT_MAX_CHARS).unwrap_or(usize::MAX); + let mut taken = 0; + for (at, character) in text.char_indices() { + taken += character.width().unwrap_or(0); + if taken > cap { + return &text[..at]; + } + } + text } #[cfg(test)] @@ -145,7 +169,7 @@ mod tests { use tablepro_core::column::{BoolStorage, ColumnKind, IntegerKind, IntegerWidth, TemporalZone, TextLength}; use tablepro_core::value::{SqlDecimal, Temporal}; - use super::{default_width_chars, number_width_chars, typical_texts, typical_width_chars, widest_text}; + use super::{default_width_chars, number_width_chars, sampled_texts, typical_texts, typical_width_chars}; use crate::services::grid::test_columns::result_only; fn text() -> ColumnKind { @@ -159,7 +183,7 @@ mod tests { } #[test] - fn text_widths_take_the_header_or_the_widest_value_within_bounds() { + fn text_widths_take_the_widest_value_within_bounds() { let note = result_only("note", text()); assert_eq!(default_width_chars(¬e, &texts(&["a", "bb"])), 4, "the minimum"); @@ -171,7 +195,8 @@ mod tests { ); assert_eq!( default_width_chars(&result_only("a_rather_long_column_name", text()), &texts(&["a"])), - 25 + 4, + "the header is drawn in another font" ); assert_eq!( default_width_chars(¬e, &[Value::Null, Value::Null]), @@ -181,7 +206,7 @@ mod tests { } #[test] - fn the_widest_text_is_the_longest_value_within_the_cap() { + fn the_longest_plain_value_is_measured_within_the_cap() { let note = result_only("note", text()); let integer = ColumnKind::Integer(IntegerKind { width: IntegerWidth::W64, @@ -189,18 +214,65 @@ mod tests { }); assert_eq!( - widest_text( + sampled_texts( ¬e, &[Value::Text("CF-1001".into()), Value::Null, Value::Text("a".into())] ), - Some("CF-1001".to_owned()) + ["CF-1001"] + ); + assert_eq!( + sampled_texts(¬e, &texts(&["x".repeat(500).as_str()])) + .iter() + .map(|text| text.chars().count()) + .collect::>(), + [40] + ); + assert!(sampled_texts(¬e, &[Value::Null]).is_empty()); + assert!(sampled_texts(&result_only("id", integer), &[Value::Int(7)]).is_empty()); + } + + /// A character a monospace font may lack draws in a fallback font, as + /// wide as that font draws it, so every such value is measured. + #[test] + fn every_value_beyond_plain_ascii_is_measured() { + let note = result_only("note", text()); + + assert_eq!( + sampled_texts( + ¬e, + &texts(&["Marunouchi 12", "कखगघ", "東京", "a", "कखगघ", "tab\tbed"]) + ), + ["tab\tbed", "कखगघ", "東京", "Marunouchi 12"] ); + } + + /// A Chinese or Japanese character takes two cells of a monospace font. + #[test] + fn a_wide_character_takes_two_cells() { + let note = result_only("note", text()); + + assert_eq!(default_width_chars(¬e, &texts(&["東京都千代田区"])), 14); assert_eq!( - widest_text(¬e, &texts(&["x".repeat(500).as_str()])).map(|text| text.chars().count()), - Some(40) + sampled_texts(¬e, &texts(&["東".repeat(500).as_str()])) + .iter() + .map(|text| text.chars().count()) + .collect::>(), + [20], + "the cap counts cells" + ); + assert_eq!( + typical_texts(&result_only( + "state", + ColumnKind::Enum { + labels: vec!["東".repeat(30)], + multiple: false, + } + )) + .iter() + .map(|text| text.chars().count()) + .collect::>(), + [20] ); - assert_eq!(widest_text(¬e, &[Value::Null]), None); - assert_eq!(widest_text(&result_only("id", integer), &[Value::Int(7)]), None); } #[test] @@ -255,7 +327,7 @@ mod tests { default_width_chars(&result_only("ok", flag.clone()), &[Value::Bool(true)]), 5 ); - assert_eq!(default_width_chars(&result_only("is_active", flag), &[]), 9); + assert_eq!(default_width_chars(&result_only("is_active", flag), &[]), 5); } #[test] @@ -329,8 +401,8 @@ mod tests { ); assert_eq!( typical_width_chars(&result_only("a_column_with_a_name_far_longer_than_it_holds", text())), - 40, - "the header, within the cap" + 20, + "the header is drawn in another font" ); } diff --git a/linux/crates/app/src/services/grid/mod.rs b/linux/crates/app/src/services/grid/mod.rs index 894c05f799..55121c084d 100644 --- a/linux/crates/app/src/services/grid/mod.rs +++ b/linux/crates/app/src/services/grid/mod.rs @@ -32,7 +32,7 @@ pub use self::cell_lock::CellLock; pub use self::cell_preset::{CellPreset, CellPresets, cell_presets}; pub use self::column_layout::{ColumnLayout, LayoutSlot, Placement, merge_order}; pub use self::column_width::{ - default_width_chars, number_width_chars, typical_texts, typical_width_chars, widest_text, + default_width_chars, header_text, number_width_chars, sampled_texts, typical_texts, typical_width_chars, }; pub use self::copy_scope::{CopyScope, copy_scope}; pub use self::delete_availability::{DeleteAvailability, delete_availability}; diff --git a/linux/crates/app/src/ui/data_grid/columns.rs b/linux/crates/app/src/ui/data_grid/columns.rs index f4c625a633..65d7fd4200 100644 --- a/linux/crates/app/src/ui/data_grid/columns.rs +++ b/linux/crates/app/src/ui/data_grid/columns.rs @@ -5,14 +5,16 @@ use std::sync::Arc; use gtk4::prelude::*; use gtk4::subclass::prelude::*; use gtk4::{self as gtk, glib, pango}; +use libadwaita as adw; use tablepro_core::Value; use super::grid_cell::{GridCell, TextAttributes}; use super::server_sorter::ServerSorter; use super::{DataGrid, GridRow, GridSorting, TrackedValues, client_sorter, grid_menu, row_number_column}; +use crate::services::grid::display_text::editable_null_sentinel; use crate::services::grid::{ - CellDisplay, ColumnLayout, GridColumn, LayoutSlot, Placement, default_width_chars, merge_order, number_width_chars, - typical_texts, typical_width_chars, widest_text, + CellDisplay, ColumnLayout, GridColumn, LayoutSlot, Placement, default_width_chars, header_text, merge_order, + number_width_chars, sampled_texts, typical_texts, typical_width_chars, }; /// Rows sampled for a column's opening width. @@ -20,8 +22,8 @@ const WIDTH_SAMPLE: usize = 200; /// Cell padding Adwaita gives a data table's cell, both sides, and the /// column separator. const CELL_CHROME_PX: i32 = 13; -/// A resize step, in digits of the grid's font, so a step is the same -/// number of characters at any text size. +/// A resize step, in digits of the font the cells draw in, so a step is +/// the same number of characters at any text size. const STEP_DIGITS: i32 = 2; /// What a column's cells read to draw a row. The column makes one and @@ -244,8 +246,10 @@ impl DataGrid { let sample: Vec>> = self.store().rows().take(WIDTH_SAMPLE).map(|row| row.cells()).collect(); let by_type = set.pages == Pages::Empty; let view = self.column_view(); - let metrics = view.pango_context().metrics(None, None); + let font = value_font(&view); + let metrics = view.pango_context().metrics(Some(&font), None); let char_px = pango::units_to_double(metrics.approximate_char_width()); + let null = editable_null_sentinel(); let mut widths = Vec::new(); for (paint, view_column) in set.paints.iter().zip(&set.view_columns) { let index = paint.column.index; @@ -264,26 +268,33 @@ impl DataGrid { (false, false) => default_width_chars(column, values.iter().copied()), }; let estimate = (f64::from(chars) * char_px).ceil() as i32; - // A number is never cut, and its digits in tabular figures run - // wider than an average character. Nor is a date, held or not, - // nor with no rows a value its type spells out, as a UUID or an - // enum's label, so those are measured in tabular figures too. + // A number is never cut. Nor is a date, held or not, nor with + // no rows a value its type spells out, as a UUID or an enum's + // label, so those are measured as the cell draws them. let sampled = match (by_type, paint.numbers.get()) { - (_, true) => tabular_width(&view, &"0".repeat(usize::try_from(chars).unwrap_or_default())), + (_, true) => tabular_width(&view, &font, &"0".repeat(usize::try_from(chars).unwrap_or_default())), (true, false) => 0, - (false, false) => { - widest_text(column, values.iter().copied()).map_or(0, |widest| drawn_width(&view, &widest)) - } + // One line each, so one layout measures them all. + (false, false) => drawn_width(&view, &font, &sampled_texts(column, values.iter().copied()).join("\n")), }; let spelled = match by_type || column.display() == CellDisplay::Temporal { true => typical_texts(column) .iter() - .map(|typical| tabular_width(&view, typical)) + .map(|typical| tabular_width(&view, &font, typical)) .max() .unwrap_or(0), false => 0, }; - let text = estimate.max(sampled).max(spelled); + // The header draws its title smaller than the body font, so the + // body font's width holds it. + let header = view.create_pango_layout(Some(header_text(column))).pixel_size().0; + // A NULL the user can replace draws as ``, wider than the + // fewest cells a column takes. + let placeholder = match paint.tracked.tracks_changes() && values.iter().any(|value| value.is_null()) { + true => drawn_width(&view, &font, &null), + false => 0, + }; + let text = estimate.max(sampled).max(spelled).max(header).max(placeholder); let width = text + CELL_CHROME_PX; if auto.is_none_or(|auto| width > auto) { widths.push((index, view_column.clone(), width)); @@ -481,7 +492,8 @@ impl DataGrid { let Some(column) = column else { return; }; - let metrics = self.column_view().pango_context().metrics(None, None); + let view = self.column_view(); + let metrics = view.pango_context().metrics(Some(&value_font(&view)), None); let step = (pango::units_to_double(metrics.approximate_digit_width()) * f64::from(STEP_DIGITS)).ceil() as i32; let width = column.fixed_width(); let width = match wider { @@ -707,16 +719,29 @@ fn same_names(layout: &ColumnLayout, columns: &[GridColumn]) -> bool { names.len() == columns.len() && names.iter().zip(columns).all(|(name, column)| *name == column.name) } +/// The font a cell draws its value in: libadwaita's `monospace` class +/// sets only the family and size of the desktop's monospace font. +fn value_font(view: >k::ColumnView) -> pango::FontDescription { + let mut monospace = pango::FontDescription::from_string(&adw::StyleManager::default().monospace_font_name()); + monospace.unset_fields(!(pango::FontMask::FAMILY | pango::FontMask::SIZE)); + let mut font = view.pango_context().font_description().unwrap_or_default(); + font.merge(Some(&monospace), true); + font +} + /// How wide `text` is in the tabular figures libadwaita's `numeric` /// class draws. -fn tabular_width(widget: &impl IsA, text: &str) -> i32 { +fn tabular_width(widget: &impl IsA, font: &pango::FontDescription, text: &str) -> i32 { let layout = widget.create_pango_layout(Some(text)); + layout.set_font_description(Some(font)); let tabular = pango::AttrList::new(); tabular.insert(pango::AttrFontFeatures::new("tnum")); layout.set_attributes(Some(&tabular)); layout.pixel_size().0 } -fn drawn_width(widget: &impl IsA, text: &str) -> i32 { - widget.create_pango_layout(Some(text)).pixel_size().0 +fn drawn_width(widget: &impl IsA, font: &pango::FontDescription, text: &str) -> i32 { + let layout = widget.create_pango_layout(Some(text)); + layout.set_font_description(Some(font)); + layout.pixel_size().0 } diff --git a/linux/crates/app/src/ui/data_grid/edit/cell_editor.rs b/linux/crates/app/src/ui/data_grid/edit/cell_editor.rs index b7963faaea..b5256e0456 100644 --- a/linux/crates/app/src/ui/data_grid/edit/cell_editor.rs +++ b/linux/crates/app/src/ui/data_grid/edit/cell_editor.rs @@ -305,6 +305,11 @@ impl EditController { self.commit_open(Refusal::Drop, CloseWhen::Now); let text = gtk::Text::new(); text.set_hexpand(true); + // Drawn as the cell draws the value, so it does not jump as the edit opens. + text.add_css_class("monospace"); + if cell.paint().is_some_and(|paint| paint.numbers.get()) { + text.set_alignment(1.0); + } text.set_truncate_multiline(!plan.accepts_line_breaks); text.set_input_purpose(input_purpose(plan.purpose)); text.set_placeholder_text(empty_placeholder(&plan.column, &plan.start.row).as_deref()); diff --git a/linux/crates/app/src/ui/data_grid/edit/tests/mod.rs b/linux/crates/app/src/ui/data_grid/edit/tests/mod.rs index 9191e074d8..ccdd66fb77 100644 --- a/linux/crates/app/src/ui/data_grid/edit/tests/mod.rs +++ b/linux/crates/app/src/ui/data_grid/edit/tests/mod.rs @@ -562,6 +562,24 @@ fn the_cell_editor_is_named_by_its_column() { assert_eq!(assert_labelled(&editor), Ok(())); } +/// The editor draws the value in the cell's font and on the cell's side, +/// so the value does not jump as the edit opens. +#[gtk4::test] +fn the_editor_draws_the_value_where_and_as_its_cell_did() { + for grid in [number_row(), name_rows(Value::Text("A".into()))] { + let cell = grid.cell_of(1, 1); + let shown = cell.display().cloned().expect("the cell's text"); + + let editor = type_into(&grid, &cell, "5"); + + assert!(editor.has_css_class("monospace")); + assert_eq!(editor.alignment(), shown.xalign()); + let family = |widget: >k::Widget| widget.pango_context().font_description().and_then(|font| font.family()); + wait_until(WAIT, || family(editor.upcast_ref()) == family(shown.upcast_ref())) + .expect("the editor draws in the cell's font"); + } +} + #[gtk4::test] fn a_value_too_large_to_lay_out_does_not_open_and_says_why() { let large = "x".repeat(EDIT_MAX_BYTES + 1); diff --git a/linux/crates/app/src/ui/data_grid/grid_cell.rs b/linux/crates/app/src/ui/data_grid/grid_cell.rs index 6f5f353f99..8e16a2e32d 100644 --- a/linux/crates/app/src/ui/data_grid/grid_cell.rs +++ b/linux/crates/app/src/ui/data_grid/grid_cell.rs @@ -154,6 +154,8 @@ impl GridCell { display.set_valign(gtk::Align::Center); display.set_text_overflow(gtk::InscriptionOverflow::EllipsizeEnd); display.set_min_chars(1); + // A column of codes or times lines up character for character. + display.add_css_class("monospace"); display.set_parent(&cell); let _ = cell.imp().display.set(display); let _ = cell.imp().paint.set(paint); diff --git a/linux/crates/app/src/ui/data_grid/test_grid.rs b/linux/crates/app/src/ui/data_grid/test_grid.rs index 1c2d10d237..9cf0a78518 100644 --- a/linux/crates/app/src/ui/data_grid/test_grid.rs +++ b/linux/crates/app/src/ui/data_grid/test_grid.rs @@ -166,6 +166,9 @@ impl Grid { pub(crate) fn build(spec: GridSpec, rows: Vec) -> Self { crate::register_test_resources(); + // The stylesheet the app draws the grid with, cell padding and the + // monospace font included. + libadwaita::init().expect("libadwaita initialises under the test backend"); let runtime = crate::test_support::test_runtime(); let settings = MemorySettings::new(); let tracker = TabTrackerHandle::default(); diff --git a/linux/crates/app/src/ui/data_grid/tests/columns.rs b/linux/crates/app/src/ui/data_grid/tests/columns.rs index a97bfebcc1..38c5338391 100644 --- a/linux/crates/app/src/ui/data_grid/tests/columns.rs +++ b/linux/crates/app/src/ui/data_grid/tests/columns.rs @@ -9,7 +9,8 @@ use tablepro_core::value::Temporal; use tablepro_core::{ColumnInfo, Value}; use super::{Layouts, close_menus, data_column, keyed, laid_out, settled_width, shown_width, step, text, untyped}; -use crate::test_support::{drain_main_context, wait_until}; +use crate::services::grid::display_text::editable_null_sentinel; +use crate::test_support::{descendants, drain_main_context, wait_until}; use crate::ui::data_grid::columns::ColumnPaint; use crate::ui::data_grid::test_grid::{ Grid, GridSpec, WAIT, named, persisted, results, two_text_columns, two_text_values, @@ -371,6 +372,100 @@ fn a_short_code_in_a_text_column_shows_whole() { assert!(whole.is_ok(), "CF-1001 was cut"); } +/// Every value of one length draws as wide as the others in the +/// desktop's monospace font, whichever one the column was sized from. +#[gtk4::test] +fn values_of_one_length_draw_whole_in_the_monospace_font() { + let grid = laid_out(Grid::open( + vec![named("id", "integer", true), named("at", "text", false)], + vec![ + vec![Value::Int(1), text("08:30:00")], + vec![Value::Int(2), text("10:00:00")], + vec![Value::Int(3), text("11:11:11")], + ], + )); + let monospace = pango::FontDescription::from_string(&libadwaita::StyleManager::default().monospace_font_name()); + + let shown = drawn(&grid, &[1]); + assert_eq!(shown.len(), 3); + for display in &shown { + assert!(display.has_css_class("monospace")); + assert_eq!( + display + .pango_context() + .font_description() + .and_then(|font| font.family()), + monospace.family(), + "a value drawn in another font" + ); + wait_until(WAIT, || shows_whole(display)).unwrap_or_else(|_| panic!("{:?} cut", display.text())); + } +} + +/// A Chinese or Japanese character takes two cells of a monospace font, +/// and a script the font lacks draws in a fallback font, so the value +/// with the most characters is not the widest, in either order. +#[gtk4::test] +fn a_value_in_another_script_shows_whole() { + let narrow = "Marunouchi 12"; + for wide in ["東京都千代田区丸の内一丁", "कखगघङचछजझञटठ"] { + for values in [[wide, narrow], [narrow, wide]] { + let grid = laid_out(Grid::open( + vec![named("id", "integer", true), named("area", "text", false)], + vec![ + vec![Value::Int(1), text(values[0])], + vec![Value::Int(2), text(values[1])], + ], + )); + let shown = drawn(&grid, &[1]); + assert_eq!(shown.len(), 2); + assert!( + tabular_width(&shown[0], wide) > tabular_width(&shown[0], narrow), + "the font draws {wide} no wider than {narrow}" + ); + + for display in &shown { + wait_until(WAIT, || shows_whole(display)).unwrap_or_else(|_| panic!("{:?} cut", display.text())); + } + } + } +} + +/// The header draws its title in the interface font, not the cells'. +#[gtk4::test] +fn a_header_of_wide_letters_shows_whole() { + let grid = laid_out(Grid::open( + vec![named("id", "integer", true), named("WWWWWWWW", "text", false)], + vec![vec![Value::Int(1), text("a")]], + )); + let title = descendants(&grid.grid.column_view()) + .into_iter() + .filter_map(|widget| widget.downcast::().ok()) + .find(|label| label.label() == "WWWWWWWW") + .expect("the header's title"); + let header = title + .parent() + .and_then(|title_box| title_box.parent()) + .expect("the header button"); + let (_, needs, _, _) = title.measure(gtk::Orientation::Horizontal, -1); + + wait_until(WAIT, || header.width() >= needs).expect("the header's title shows whole"); +} + +/// A NULL the user can replace draws as ``, which a column of short +/// values would cut. +#[gtk4::test] +fn a_null_shows_its_placeholder_whole() { + let grid = laid_out(Grid::open( + vec![named("id", "integer", true), named("x", "text", false)], + vec![vec![Value::Int(1), Value::Null]], + )); + let display = grid.cell_of(1, 1).display().cloned().expect("the placeholder's text"); + assert_eq!(display.text().as_deref(), Some(editable_null_sentinel().as_str())); + + wait_until(WAIT, || shows_whole(&display)).expect("the placeholder shows whole"); +} + #[gtk4::test] fn adding_a_draft_keeps_the_width_the_page_measured() { let columns = shop_columns(0); diff --git a/linux/crates/app/src/ui/data_grid/tests/mod.rs b/linux/crates/app/src/ui/data_grid/tests/mod.rs index c218bade72..2784729d4f 100644 --- a/linux/crates/app/src/ui/data_grid/tests/mod.rs +++ b/linux/crates/app/src/ui/data_grid/tests/mod.rs @@ -232,9 +232,10 @@ fn data_column(grid: &Grid, index: usize) -> gtk::ColumnViewColumn { grid.grid.data_view_column(index).expect("the column") } -/// A resize step: two digits of the grid's font. +/// A resize step: two digits of the font a value draws in. fn step(grid: &Grid) -> i32 { - let metrics = grid.grid.column_view().pango_context().metrics(None, None); + let display = grid.cell_at(0, 1).display().cloned().expect("a value's text"); + let metrics = display.pango_context().metrics(None, None); (pango::units_to_double(metrics.approximate_digit_width()) * 2.0).ceil() as i32 } From b1a1ff555f26698cd4497d1d569297d40124aa24 Mon Sep 17 00:00:00 2001 From: Cristian Date: Fri, 30 Oct 2026 19:39:49 +0100 Subject: [PATCH 29/85] feat(linux-editor): switch a query tab to another MySQL or MariaDB database from the editor bar (#3897) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3897.added.md | 1 + .../app/src/services/connection_deletion.rs | 6 +- .../app/src/ui/assistant/assistant_access.rs | 17 +- .../app/src/ui/assistant/schema_reader.rs | 40 ++- .../src/ui/editor/completion/catalog_read.rs | 106 +++++-- .../app/src/ui/editor/completion/mod.rs | 2 +- .../completion/sql_completion_provider.rs | 10 +- .../completion/sql_completion_provider_imp.rs | 1 + .../app/src/ui/editor/editor_actions.rs | 16 +- .../app/src/ui/editor/editor_session_slot.rs | 72 ++++- linux/crates/app/src/ui/editor/imp.rs | 15 + linux/crates/app/src/ui/editor/mod.rs | 16 +- .../crates/app/src/ui/editor/script_runner.rs | 24 +- linux/crates/app/src/ui/editor/tab_scope.rs | 266 ++++++++++++++++-- linux/crates/app/src/ui/editor/tests.rs | 200 ++++++++++++- .../app/src/ui/error_text/unsupported.rs | 2 + .../crates/app/src/ui/workspace/assistant.rs | 18 ++ linux/crates/app/src/ui/workspace/mod.rs | 4 + .../app/src/ui/workspace/tab_factory.rs | 11 +- linux/crates/app/src/ui/workspace/tab_spec.rs | 2 + .../app/src/ui/workspace/tests/assistant.rs | 1 + .../app/src/ui/workspace/tests/close_tab.rs | 2 + .../app/src/ui/workspace/tests/tab_title.rs | 2 + .../src/ui/workspace/workspace_persistence.rs | 2 + .../app/src/ui/workspace/workspace_tabs.rs | 8 +- linux/crates/app/src/workspace/tab_record.rs | 6 +- .../app/src/workspace/workspace_record.rs | 4 + .../src/workspace/workspace_record_builder.rs | 1 + .../app/src/workspace/workspace_store.rs | 2 + linux/crates/core/src/connection.rs | 8 + linux/crates/core/src/editor_session.rs | 9 + linux/crates/core/src/unsupported_feature.rs | 3 + linux/crates/drivers/mysql/src/connection.rs | 8 + linux/crates/drivers/mysql/src/dialect.rs | 6 +- .../drivers/mysql/src/editor_session.rs | 53 ++++ linux/crates/drivers/mysql/src/metadata.rs | 35 +++ .../mysql/tests/integration/metadata.rs | 36 ++- .../crates/test-support/src/call_counters.rs | 1 + .../test-support/src/connection_script.rs | 2 + .../test-support/src/fake_connection.rs | 13 + .../test-support/src/fake_editor_session.rs | 23 ++ linux/data/resources/ui/sql-editor.ui | 27 ++ 42 files changed, 1008 insertions(+), 73 deletions(-) create mode 100644 linux/changelog.d/3897.added.md diff --git a/linux/changelog.d/3897.added.md b/linux/changelog.d/3897.added.md new file mode 100644 index 0000000000..07ce4b367e --- /dev/null +++ b/linux/changelog.d/3897.added.md @@ -0,0 +1 @@ +On MySQL and MariaDB, the SQL editor's database name is a menu that moves the tab to another of the server's databases. diff --git a/linux/crates/app/src/services/connection_deletion.rs b/linux/crates/app/src/services/connection_deletion.rs index 8eaa79c90f..8344ae13eb 100644 --- a/linux/crates/app/src/services/connection_deletion.rs +++ b/linux/crates/app/src/services/connection_deletion.rs @@ -248,7 +248,11 @@ mod tests { &key, WorkspaceRecord { active_tab: Some(0), - tabs: vec![TabRecord::Editor { draft, file: None }], + tabs: vec![TabRecord::Editor { + draft, + file: None, + database: None, + }], }, ) .expect("scheduled"); diff --git a/linux/crates/app/src/ui/assistant/assistant_access.rs b/linux/crates/app/src/ui/assistant/assistant_access.rs index 67f888c59c..9e79516cda 100644 --- a/linux/crates/app/src/ui/assistant/assistant_access.rs +++ b/linux/crates/app/src/ui/assistant/assistant_access.rs @@ -14,6 +14,7 @@ use tablepro_core::{AiPolicy, AiProvider}; use crate::services::active_session::ActiveSession; use crate::services::ai_service::AiService; +use crate::ui::editor::CatalogHome; const CANCEL: &str = "cancel"; const ALLOW: &str = "allow"; @@ -54,6 +55,9 @@ pub(crate) struct AssistantAccess { /// Each Time. granted: RefCell>, asking: Cell, + /// Where the editor tab in front resolves names: its database, when + /// it moved off the connection's. + front: RefCell CatalogHome>>>, } impl AssistantAccess { @@ -72,9 +76,15 @@ impl AssistantAccess { policy: Box::new(policy), granted: RefCell::new(None), asking: Cell::new(false), + front: RefCell::new(None), }) } + /// Describe the database of the editor tab `front` says is in front. + pub(crate) fn follow_front(&self, front: impl Fn() -> CatalogHome + 'static) { + self.front.replace(Some(Box::new(front))); + } + pub(crate) fn ai(&self) -> &AiService { &self.ai } @@ -196,10 +206,15 @@ impl AssistantAccess { }; }; let engine = self.engine.clone(); + let home = self + .front + .borrow() + .as_ref() + .map_or_else(CatalogHome::default, |front| front()); let read = self .ai .tasks() - .spawn_task(async move { super::schema_reader::read(session, engine, text).await }) + .spawn_task(async move { super::schema_reader::read(session, engine, text, home).await }) .await; read.unwrap_or_else(|failure| { tracing::warn!(%failure, "the schema for the assistant could not be read"); diff --git a/linux/crates/app/src/ui/assistant/schema_reader.rs b/linux/crates/app/src/ui/assistant/schema_reader.rs index c694bc6dfd..186927125c 100644 --- a/linux/crates/app/src/ui/assistant/schema_reader.rs +++ b/linux/crates/app/src/ui/assistant/schema_reader.rs @@ -10,12 +10,12 @@ use tablepro_core::sql_syntax::script::LexicalSettings; use tablepro_core::{ColumnInfo, TableRef}; use tablepro_session::Session; -use crate::ui::editor::{CompletionScope, read_catalog, tables_named}; +use crate::ui::editor::{CatalogHome, CompletionScope, read_catalog, tables_named}; /// The schema the user is in, its tables, and the structure of the /// tables `text` names. Shares the catalog's reads with the sidebar and /// completion, so it rarely reaches the server. -pub(crate) async fn read(session: Arc, engine: String, text: String) -> SchemaContext { +pub(crate) async fn read(session: Arc, engine: String, text: String, home: CatalogHome) -> SchemaContext { let grammar = session .current_dialect() .map_or(SqlGrammar::PostgreSql, |dialect| dialect.grammar()); @@ -24,9 +24,22 @@ pub(crate) async fn read(session: Arc, engine: String, text: String) -> } else { QueryLanguage::Commands }; + // Nothing the connection's catalog holds is in that database. + if let CatalogHome::Elsewhere(database) = home { + return SchemaContext { + engine, + language, + database: Some(database), + ..SchemaContext::default() + }; + } let mut named = tables_named(&text, grammar, LexicalSettings::default_for(grammar)); // A question in words names tables as words, which no SQL parse sees. - if let Ok(objects) = session.objects(ObjectScope::DefaultSchema).await { + let listed = match &home { + CatalogHome::Schema(database) => ObjectScope::Schema(database.clone()), + CatalogHome::Connection | CatalogHome::Elsewhere(_) => ObjectScope::DefaultSchema, + }; + if let Ok(objects) = session.objects(listed).await { for table in named_in_words(&text, &objects) { if !named.contains(&table) { named.push(table); @@ -39,14 +52,18 @@ pub(crate) async fn read(session: Arc, engine: String, text: String) -> CompletionScope::General { tables_in_statement: named, }, + home.clone(), ) .await; - let schema = session.schemas().await.ok().and_then(|schemas| { - schemas - .iter() - .find(|schema| schema.is_default) - .map(|schema| schema.name.clone()) - }); + let schema = match &home { + CatalogHome::Schema(database) => Some(database.clone()), + CatalogHome::Connection | CatalogHome::Elsewhere(_) => session.schemas().await.ok().and_then(|schemas| { + schemas + .iter() + .find(|schema| schema.is_default) + .map(|schema| schema.name.clone()) + }), + }; let tables = answer .objects .map(|objects| { @@ -66,7 +83,10 @@ pub(crate) async fn read(session: Arc, engine: String, text: String) -> SchemaContext { engine, language, - database: session.database().map(str::to_owned), + database: match home { + CatalogHome::Schema(database) => Some(database), + CatalogHome::Connection | CatalogHome::Elsewhere(_) => session.database().map(str::to_owned), + }, schema, tables, described, diff --git a/linux/crates/app/src/ui/editor/completion/catalog_read.rs b/linux/crates/app/src/ui/editor/completion/catalog_read.rs index 1c2eede515..a0e91a4d00 100644 --- a/linux/crates/app/src/ui/editor/completion/catalog_read.rs +++ b/linux/crates/app/src/ui/editor/completion/catalog_read.rs @@ -14,51 +14,94 @@ pub struct CatalogAnswer { pub columns: Vec<(TableRef, Arc<[ColumnInfo]>)>, } +/// Where an unqualified name resolves for the tab. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub enum CatalogHome { + /// The connection's default schema. + #[default] + Connection, + /// Another of the server's databases, which the catalog lists as a + /// schema. + Schema(String), + /// A database the catalog does not describe, which nothing offered + /// from it would be right for. + Elsewhere(String), +} + /// Read what `scope` offers. The catalog shares each read with every /// other view and keeps what it read, so typing does not reach the /// server again; a list that cannot be read is left out. -pub async fn read(session: Arc, scope: CompletionScope) -> CatalogAnswer { - let defaults = listed(session.objects(ObjectScope::DefaultSchema).await); +pub async fn read(session: Arc, scope: CompletionScope, home: CatalogHome) -> CatalogAnswer { + let defaults = match &home { + CatalogHome::Connection => listed(session.objects(ObjectScope::DefaultSchema).await), + CatalogHome::Schema(name) => listed(session.objects(ObjectScope::Schema(name.clone())).await), + CatalogHome::Elsewhere(_) => return CatalogAnswer::default(), + }; + let home = Home { home, defaults }; match scope { CompletionScope::Objects { schema: None } => CatalogAnswer { schemas: Some(listed(session.schemas().await)), - objects: Some(defaults), + objects: Some(home.defaults.clone()), columns: Vec::new(), }, CompletionScope::Objects { schema: Some(name) } => { // `users.` typed before the FROM that names users. - let table = relation_named(&defaults, &name); + let table = relation_named(&home.defaults, &name); CatalogAnswer { schemas: None, - objects: schema_objects(&session, &name).await, + objects: schema_objects(&session, &home, &name).await, columns: columns_of(&session, table.into_iter().collect()).await, } } CompletionScope::Qualified { qualifier, name } => { let table = TableRef::new(Some(qualifier), name.clone()); - match known_table(&session, &defaults, &table).await { + match known_table(&session, &home, &table).await { Some(table) => CatalogAnswer { columns: columns_of(&session, vec![table]).await, ..CatalogAnswer::default() }, None => CatalogAnswer { - objects: schema_objects(&session, &name).await, + objects: schema_objects(&session, &home, &name).await, ..CatalogAnswer::default() }, } } CompletionScope::Columns { tables } => CatalogAnswer { - columns: columns_of(&session, known_tables(&session, &defaults, &tables).await).await, + columns: columns_of(&session, known_tables(&session, &home, &tables).await).await, ..CatalogAnswer::default() }, CompletionScope::General { tables_in_statement } => CatalogAnswer { schemas: None, - columns: columns_of(&session, known_tables(&session, &defaults, &tables_in_statement).await).await, - objects: Some(defaults), + columns: columns_of(&session, known_tables(&session, &home, &tables_in_statement).await).await, + objects: Some(home.defaults), }, } } +/// The schema unqualified names resolve in and what it holds. +struct Home { + home: CatalogHome, + defaults: Arc<[ObjectInfo]>, +} + +impl Home { + /// Whether `schema` names the home schema, whose objects are read. + fn is(&self, schema: &SchemaInfo) -> bool { + match &self.home { + CatalogHome::Connection => schema.is_default, + CatalogHome::Schema(name) => schema.name == *name, + CatalogHome::Elsewhere(_) => false, + } + } + + /// A name for the home database the catalog does not list as a + /// schema, as MySQL lists only the connection's. Exact, since + /// `Analytics` and `analytics` can be two databases. + fn named(&self, name: &str) -> bool { + matches!(&self.home, CatalogHome::Schema(home) if home == name) + } +} + async fn columns_of(session: &Session, tables: Vec) -> Vec<(TableRef, Arc<[ColumnInfo]>)> { let reads = tables.into_iter().map(|table| async move { let columns = listed(session.columns(table.clone()).await); @@ -76,7 +119,10 @@ fn listed(read: Result, Arc>) -> Arc<[T]> { /// The objects of the schema called `name`, or `None` when the catalog /// has no such schema. -async fn schema_objects(session: &Session, name: &str) -> Option> { +async fn schema_objects(session: &Session, home: &Home, name: &str) -> Option> { + if home.named(name) { + return Some(home.defaults.clone()); + } let schemas = listed(session.schemas().await); let schema = schema_named(&schemas, name)?; Some(listed(session.objects(ObjectScope::Schema(schema.name.clone())).await)) @@ -85,10 +131,10 @@ async fn schema_objects(session: &Session, name: &str) -> Option Vec { +async fn known_tables(session: &Session, home: &Home, tables: &[TableRef]) -> Vec { let mut known = Vec::new(); for table in tables { - if let Some(table) = known_table(session, defaults, table).await + if let Some(table) = known_table(session, home, table).await && !known.contains(&table) { known.push(table); @@ -99,14 +145,17 @@ async fn known_tables(session: &Session, defaults: &[ObjectInfo], tables: &[Tabl /// The catalog's own name for `table`, so the read is the one the /// table's other views share. -async fn known_table(session: &Session, defaults: &[ObjectInfo], table: &TableRef) -> Option { +async fn known_table(session: &Session, home: &Home, table: &TableRef) -> Option { let Some(schema) = &table.schema else { - return relation_named(defaults, &table.name); + return relation_named(&home.defaults, &table.name); }; + if home.named(schema) { + return relation_named(&home.defaults, &table.name); + } let schemas = listed(session.schemas().await); let schema = schema_named(&schemas, schema)?; - if schema.is_default { - return relation_named(defaults, &table.name); + if home.is(schema) { + return relation_named(&home.defaults, &table.name); } let objects = listed(session.objects(ObjectScope::Schema(schema.name.clone())).await); relation_named(&objects, &table.name) @@ -149,6 +198,29 @@ mod tests { } } + /// A tab moved to `analytics` reads `shop.orders` from `shop`, the + /// connection's default schema, not from its own. + #[test] + fn only_the_tabs_own_database_is_read_from_its_home() { + let shop = SchemaInfo { + name: "shop".to_owned(), + is_default: true, + is_system: false, + }; + let moved = Home { + home: CatalogHome::Schema("analytics".to_owned()), + defaults: Arc::from([]), + }; + assert!(!moved.is(&shop)); + assert!(moved.named("analytics")); + let staying = Home { + home: CatalogHome::Connection, + defaults: Arc::from([]), + }; + assert!(staying.is(&shop)); + assert!(!staying.named("shop"), "the catalog lists the connection's own"); + } + #[test] fn an_unqualified_table_takes_the_catalogs_schema() { let defaults = [object("users", ObjectKind::Table), object("Orders", ObjectKind::Table)]; diff --git a/linux/crates/app/src/ui/editor/completion/mod.rs b/linux/crates/app/src/ui/editor/completion/mod.rs index f5e2c4c98d..a131338771 100644 --- a/linux/crates/app/src/ui/editor/completion/mod.rs +++ b/linux/crates/app/src/ui/editor/completion/mod.rs @@ -17,6 +17,6 @@ mod tests; #[cfg(test)] pub(super) use tests::accept_builtin_snippet; -pub(crate) use catalog_read::read as read_catalog; +pub(crate) use catalog_read::{CatalogHome, read as read_catalog}; pub(crate) use completion_scope::{CompletionScope, tables_named}; pub use sql_completion_provider::SqlCompletionProvider; diff --git a/linux/crates/app/src/ui/editor/completion/sql_completion_provider.rs b/linux/crates/app/src/ui/editor/completion/sql_completion_provider.rs index 9daab4c91b..bebf5a2564 100644 --- a/linux/crates/app/src/ui/editor/completion/sql_completion_provider.rs +++ b/linux/crates/app/src/ui/editor/completion/sql_completion_provider.rs @@ -14,7 +14,7 @@ use tablepro_core::sql_syntax::script::{LexicalSettings, text_offsets}; use tablepro_session::Session; use tablepro_session::runtime::Tasks; -use super::catalog_read::{self, CatalogAnswer}; +use super::catalog_read::{self, CatalogAnswer, CatalogHome}; use super::completion_scope::{self, CompletionScope}; use super::fuzzy; use super::proposal_kind::ProposalKind; @@ -44,6 +44,11 @@ pub struct ProviderDeps { } impl SqlCompletionProvider { + /// Resolve unqualified names in `home`, the tab's database. + pub fn set_home(&self, home: CatalogHome) { + self.imp().home.replace(home); + } + pub fn new(session: ActiveSession, tasks: Tasks, snippets: Rc) -> Self { let provider: Self = glib::Object::new(); let _ = provider.imp().deps.set(ProviderDeps { @@ -67,11 +72,12 @@ impl SqlCompletionProvider { let ranking = match (request.session.clone(), tasks.clone()) { (Some(session), Some(tasks)) if request.grammar.is_sql() => { let fallback = request.clone(); + let home = self.imp().home.borrow().clone(); let ranked_for = needle.clone(); let blocking = tasks.clone(); tasks .spawn_task(async move { - let answer = catalog_read::read(session, request.scope.clone()).await; + let answer = catalog_read::read(session, request.scope.clone(), home).await; blocking .spawn_blocking_task(move || Ranking::new(request.candidates(&answer).into(), &ranked_for)) .await diff --git a/linux/crates/app/src/ui/editor/completion/sql_completion_provider_imp.rs b/linux/crates/app/src/ui/editor/completion/sql_completion_provider_imp.rs index 9ceb7307b7..710220ea4c 100644 --- a/linux/crates/app/src/ui/editor/completion/sql_completion_provider_imp.rs +++ b/linux/crates/app/src/ui/editor/completion/sql_completion_provider_imp.rs @@ -19,6 +19,7 @@ const PRIORITY: i32 = 100; #[derive(Default)] pub struct SqlCompletionProvider { pub deps: OnceCell, + pub home: std::cell::RefCell, } #[glib::object_subclass] diff --git a/linux/crates/app/src/ui/editor/editor_actions.rs b/linux/crates/app/src/ui/editor/editor_actions.rs index a32aaa051d..fecf7ccd06 100644 --- a/linux/crates/app/src/ui/editor/editor_actions.rs +++ b/linux/crates/app/src/ui/editor/editor_actions.rs @@ -1,6 +1,6 @@ -use gtk4::gio; use gtk4::prelude::*; use gtk4::subclass::prelude::*; +use gtk4::{gio, glib}; use libadwaita as adw; use libadwaita::prelude::*; @@ -68,6 +68,18 @@ impl SqlEditor { // Hidden until a workspace attaches an assistant that may send. set_enabled(&actions, name, false); } + let editor = self.downgrade(); + let database = gio::SimpleAction::new_stateful( + super::tab_scope::MOVE_ACTION, + Some(glib::VariantTy::STRING), + &"".to_variant(), + ); + database.connect_change_state(move |_, value| { + if let (Some(editor), Some(name)) = (editor.upgrade(), value.and_then(|value| value.str())) { + editor.move_to_database(name); + } + }); + actions.add_action(&database); sync_run_actions(&actions, false, None); sync_caret_actions(&actions, &self.imp().source_view); // Commands have no comments, and no layout to format them into. @@ -238,6 +250,8 @@ fn close_view_popups(view: &impl IsA) -> bool { /// its completion popup. The session takes one run at a time, so a /// commit or rollback waits for the run in flight. fn sync_run_actions(actions: &gio::SimpleActionGroup, running: bool, open: Option) { + // A tab moves between runs, and never away from its transaction. + set_enabled(actions, super::tab_scope::MOVE_ACTION, !running && open.is_none()); set_enabled(actions, "cancel", running); set_enabled(actions, "roll-back", !running && open.is_some()); set_enabled(actions, "commit", !running && open == Some(OpenTransaction::Open)); diff --git a/linux/crates/app/src/ui/editor/editor_session_slot.rs b/linux/crates/app/src/ui/editor/editor_session_slot.rs index 58d5019343..e0be235a77 100644 --- a/linux/crates/app/src/ui/editor/editor_session_slot.rs +++ b/linux/crates/app/src/ui/editor/editor_session_slot.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak}; use std::time::Duration; -use tablepro_core::{EditorSession, EditorSessionOptions, EditorSessionState, TransactionState}; +use tablepro_core::{CallOptions, EditorSession, EditorSessionOptions, EditorSessionState, TransactionState}; use tablepro_session::{OperationError, Session}; use tokio_util::sync::CancellationToken; @@ -28,6 +28,9 @@ struct SlotState { /// told yet. rolled_back: bool, closed: bool, + /// The database the tab works in once it left the connection's: picked + /// from the editor bar, restored, or where a `USE` left its session. + database: Option, } struct OpenEditor { @@ -61,6 +64,12 @@ pub enum AcquireError { /// The session's connection was lost with a transaction open, which /// the server rolled back. Nothing was opened in its place. TransactionRolledBack, + /// The tab could not get to its database, so it stays where its + /// session is. + Moved { + database: String, + reason: String, + }, Failed(String), } @@ -79,8 +88,9 @@ impl EditorSessionSlot { close_timeout: Duration, cancel: &CancellationToken, ) -> Result, AcquireError> { + let deadline = statement_timeout.map(|timeout| tokio::time::Instant::now() + timeout); let stale = match self.lookup(session) { - Lookup::Ready(editor) => return Ok(editor), + Lookup::Ready(editor) => return self.reach_database(session, editor, deadline, cancel).await, Lookup::Closed => return Err(AcquireError::Cancelled), Lookup::RolledBack { stale } => { if let Some(stale) = stale { @@ -96,7 +106,7 @@ impl EditorSessionSlot { let options = EditorSessionOptions { statement_timeout }; match session.open_editor_session(options, cancel).await { Ok(editor) => match self.adopt(session, editor.clone()) { - Ok(()) => Ok(editor), + Ok(()) => self.reach_database(session, editor, deadline, cancel).await, Err(orphan) => { close_quietly(orphan, close_timeout).await; Err(AcquireError::Cancelled) @@ -110,6 +120,57 @@ impl EditorSessionSlot { } } + /// Move the tab to `database` from its next run on. + pub fn set_database(&self, database: String) { + lock(&self.state).database = Some(database); + } + + /// The tab's database, `None` while it is the connection's. + pub fn database(&self) -> Option { + lock(&self.state).database.clone() + } + + /// Take where a run left `editor`, which a `USE` in it may have moved. + pub fn follow(&self, editor: &Arc) { + let mut state = lock(&self.state); + let held = state.open.as_ref().is_some_and(|open| open.editor.id() == editor.id()); + if let Some(database) = editor.database().filter(|_| held) { + state.database = Some(database); + } + } + + /// Put `editor` in the tab's database unless it is there. A session + /// that cannot get there leaves the tab where the session is. + async fn reach_database( + &self, + session: &Arc, + editor: Arc, + deadline: Option, + cancel: &CancellationToken, + ) -> Result, AcquireError> { + let Some(wanted) = self.database() else { + return Ok(editor); + }; + let here = editor.database().or_else(|| session.database().map(str::to_owned)); + if here.as_deref() == Some(wanted.as_str()) { + return Ok(editor); + } + match editor + .use_database(&wanted, CallOptions::new(deadline, cancel.clone())) + .await + { + Ok(()) => Ok(editor), + Err(tablepro_core::DriverError::Cancelled) => Err(AcquireError::Cancelled), + Err(error) => { + lock(&self.state).database = here; + Err(AcquireError::Moved { + database: wanted, + reason: crate::ui::error_text::driver_message(&error), + }) + } + } + } + fn lookup(&self, owner: &Arc) -> Lookup { let mut state = lock(&self.state); if state.closed { @@ -139,11 +200,6 @@ impl EditorSessionSlot { } } - /// Where the tab's session says it is, once it says. - pub fn database(&self) -> Option { - lock(&self.state).open.as_ref().and_then(|open| open.editor.database()) - } - /// Where the transaction of `editor` stood after its last run. pub fn transaction(&self, editor: &Arc) -> Option { let state = lock(&self.state); diff --git a/linux/crates/app/src/ui/editor/imp.rs b/linux/crates/app/src/ui/editor/imp.rs index c89247faa3..7cf2bfe202 100644 --- a/linux/crates/app/src/ui/editor/imp.rs +++ b/linux/crates/app/src/ui/editor/imp.rs @@ -34,6 +34,10 @@ pub struct SqlEditor { #[template_child] pub scope_label: TemplateChild, #[template_child] + pub scope_button: TemplateChild, + #[template_child] + pub scope_button_label: TemplateChild, + #[template_child] pub cursor_info: TemplateChild, #[template_child] pub menu_button: TemplateChild, @@ -60,6 +64,14 @@ pub struct SqlEditor { /// This tab's own connection, which keeps its transaction and /// temporary tables between runs. pub slot: Arc, + /// The database the bar names, to tell the workspace when it changes. + pub named_database: RefCell>, + /// Where completion resolves unqualified names, which the assistant + /// describes too. + pub catalog_home: RefCell, + /// The server's name for the connection's database, for one opened + /// without a name. + pub listed_own_database: RefCell>, pub running: RefCell>, pub log: RefCell>, /// Whether the last run left a transaction open, as last reported. @@ -136,6 +148,9 @@ impl ObjectImpl for SqlEditor { .param_types([bool::static_type()]) .build(), Signal::builder("file-changed").build(), + // The tab is in another database: picked from the bar, or + // where a `USE` took its session. + Signal::builder("database-changed").build(), // Vim's :q, which closes the tab through its close prompts. Signal::builder("close-requested").build(), Signal::builder("assistant-requested") diff --git a/linux/crates/app/src/ui/editor/mod.rs b/linux/crates/app/src/ui/editor/mod.rs index d994114567..457426700f 100644 --- a/linux/crates/app/src/ui/editor/mod.rs +++ b/linux/crates/app/src/ui/editor/mod.rs @@ -49,7 +49,7 @@ use tablepro_session::runtime::{ProcessHold, Tasks}; use tokio_util::sync::CancellationToken; use self::buffer_edit::{CursorAnchor, replace_all_undoable}; -pub(crate) use self::completion::{CompletionScope, read_catalog, tables_named}; +pub(crate) use self::completion::{CatalogHome, CompletionScope, read_catalog, tables_named}; pub(crate) use self::editor_file::file_name; use self::editor_view::EditorView; use self::imp::RunningScript; @@ -154,6 +154,7 @@ impl SqlEditor { editor.connect_buttons(); editor.connect_grid(); editor.show_scope(); + editor.list_databases(); editor } @@ -692,6 +693,9 @@ impl SqlEditor { } self.record_history(entry, dialect); self.show_scope(); + if imp.changes_catalog.get() { + self.list_databases(); + } self.emit_by_name::<()>("run-finished", &[&outcome]); self.emit_by_name::<()>("run-state-changed", &[&false]); } @@ -867,8 +871,11 @@ impl SqlEditor { let EntryKind::ResultSet(result) = &entry.kind else { return None; }; - Some(ExportOffer { - origin: ExportOrigin::Editor { + // All Rows runs the statement again on the connection, which is in + // another database than this tab. + let origin = match self.tab_database() { + Some(_) => ExportOrigin::Shown { rows: shown }, + None => ExportOrigin::Editor { shown, query: ExportQuery::Editor { sql: result.statement.clone(), @@ -876,6 +883,9 @@ impl SqlEditor { shown_rows: result.row_count() as u64, truncated: result.summary.is_truncated(), }, + }; + Some(ExportOffer { + origin, session: self.deps().and_then(|deps| deps.session.get()), name: export_name_for_query(&entry.preview), target: None, diff --git a/linux/crates/app/src/ui/editor/script_runner.rs b/linux/crates/app/src/ui/editor/script_runner.rs index 922d106e62..4d0d1356cd 100644 --- a/linux/crates/app/src/ui/editor/script_runner.rs +++ b/linux/crates/app/src/ui/editor/script_runner.rs @@ -126,7 +126,8 @@ pub struct AfterRun { } /// Send each batch in turn, reporting each one as it ends. `transaction` -/// is where the session's transaction stood before the run. +/// is where the session's transaction stood before the run. The caller +/// says `Finished` once the tab knows where the run left it. pub async fn run_batches( editor: &Arc, run: &ScriptRun, @@ -201,7 +202,6 @@ pub async fn run_batches( } } } - let _ = events.send(RunEvent::Finished).await; after } @@ -243,6 +243,14 @@ pub async fn run_in_tab( refuse(&events, message, true).await; return; } + Err(AcquireError::Moved { database, reason }) => { + let message = crate::i18n::gettext_f( + "Could not move this tab to the database “{database}”: {reason}", + &[("database", &database), ("reason", &reason)], + ); + refuse(&events, message, false).await; + return; + } Err(AcquireError::Failed(message)) => { refuse(&events, message, false).await; return; @@ -250,14 +258,18 @@ pub async fn run_in_tab( }; let before = slot.transaction(&editor); let after = run_batches(&editor, &run, &cancel, &events, before).await; + slot.follow(&editor); if editor.state() != EditorSessionState::Lost { slot.note_transaction(&editor, after.transaction); + let _ = events.send(RunEvent::Finished).await; return; } let generation = *session.link_changes().borrow(); session.signal(MonitorSignal::ProbeRequested { generation }); let refuse_next = transaction_open(after.transaction) && !after.rollback_told; - if slot.forget(&editor, refuse_next) { + let forgotten = slot.forget(&editor, refuse_next); + let _ = events.send(RunEvent::Finished).await; + if forgotten { close_quietly(editor, close_timeout).await; } } @@ -390,7 +402,11 @@ mod tests { } log }; - let (_, log) = tokio::join!(run_batches(&session, run, cancel, &events, None), collect); + let running = async { + run_batches(&session, run, cancel, &events, None).await; + let _ = events.send(RunEvent::Finished).await; + }; + let (_, log) = tokio::join!(running, collect); log } diff --git a/linux/crates/app/src/ui/editor/tab_scope.rs b/linux/crates/app/src/ui/editor/tab_scope.rs index e4789ef638..c3db3f867c 100644 --- a/linux/crates/app/src/ui/editor/tab_scope.rs +++ b/linux/crates/app/src/ui/editor/tab_scope.rs @@ -1,13 +1,21 @@ -//! The database the tab works on, named in the editor bar. +//! The database the tab works on, named in the editor bar, and the menu +//! that moves the tab to another of the server's databases. -use gtk4::glib; use gtk4::prelude::*; use gtk4::subclass::prelude::*; +use gtk4::{gio, glib}; use tablepro_core::dialect::DatabaseLayout; +use tablepro_core::meta::SchemaInfo; use tablepro_session::Session; +use tablepro_session::operation::OperationKind; use super::SqlEditor; -use crate::i18n::gettext_f; +use super::completion::{CatalogHome, SqlCompletionProvider}; +use crate::i18n::{gettext, gettext_f}; + +/// The `editor` action a database menu item names, with the database as +/// its target. +pub(super) const MOVE_ACTION: &str = "database"; /// The bar's text, dot-qualified as SQL spells a qualified name, and what /// a screen reader says for it. @@ -24,6 +32,39 @@ pub(super) fn scope_text(database: &str, schema: Option<&str>) -> (String, Strin } } +/// The user's databases, then the engine's own after a separator. `None` +/// when there is no database to move to but the one the tab is in. +pub(super) fn database_menu(databases: &[SchemaInfo]) -> Option { + if !databases.iter().any(|database| !database.is_default) { + return None; + } + let menu = gio::Menu::new(); + for system in [false, true] { + let section = gio::Menu::new(); + for database in databases.iter().filter(|database| database.is_system == system) { + let item = gio::MenuItem::new(Some(&crate::ui::menu_label(&database.name)), None); + item.set_action_and_target_value( + Some(&format!("editor.{MOVE_ACTION}")), + Some(&database.name.to_variant()), + ); + section.append_item(&item); + } + if section.n_items() > 0 { + menu.append_section(None, §ion); + } + } + Some(menu) +} + +/// Where completion resolves unqualified names for a tab in `database`. +pub(super) fn catalog_home(layout: DatabaseLayout, own: Option<&str>, database: &str) -> CatalogHome { + match layout { + _ if own == Some(database) => CatalogHome::Connection, + DatabaseLayout::AsSchemas => CatalogHome::Schema(database.to_owned()), + DatabaseLayout::WithSchemas | DatabaseLayout::Single => CatalogHome::Elsewhere(database.to_owned()), + } +} + impl SqlEditor { /// Name where the tab's next statement runs and, on an engine whose /// databases hold schemas, the schema completion resolves names in. @@ -36,12 +77,26 @@ impl SqlEditor { return; }; let Some((database, layout)) = self.scope_database(&session) else { - self.set_scope(None); + // A connection in no database yet can still pick one. + let pick = self.imp().scope_button.menu_model().is_some().then(|| { + let none = gettext("No Database"); + (none.clone(), none) + }); + self.set_scope(pick); return; }; self.set_scope(Some(scope_text(&database, None))); + let own = self.own_database(&session); + self.follow_in_completion(catalog_home(layout, own.as_deref(), &database)); + let moved = self.imp().named_database.replace(Some(database.clone())) != Some(database.clone()); + if moved { + self.emit_by_name::<()>("database-changed", &[]); + } + if let Some(action) = super::imp_action(self, MOVE_ACTION) { + action.set_state(&database.to_variant()); + } // The catalog describes the connection's own database only. - if layout != DatabaseLayout::WithSchemas || session.database() != Some(database.as_str()) { + if layout != DatabaseLayout::WithSchemas || own.as_deref() != Some(database.as_str()) { return; } let reading = { @@ -68,8 +123,8 @@ impl SqlEditor { }); } - /// The database the tab's session last reported, else the - /// connection's. `None` where a connection reaches one database. + /// The tab's database, else the connection's. `None` where a + /// connection reaches one database. fn scope_database(&self, session: &Session) -> Option<(String, DatabaseLayout)> { let layout = session.current_dialect()?.database_layout(); if layout == DatabaseLayout::Single { @@ -79,27 +134,139 @@ impl SqlEditor { .imp() .slot .database() - .or_else(|| session.database().map(str::to_owned)) + .or_else(|| self.own_database(session)) .filter(|name| !name.is_empty())?; Some((database, layout)) } + /// The database the connection opened in: the name it was given, else + /// the one the server listed as its own. + fn own_database(&self, session: &Session) -> Option { + session + .database() + .filter(|name| !name.is_empty()) + .map(str::to_owned) + .or_else(|| self.imp().listed_own_database.borrow().clone()) + } + + /// Ask the server for its databases, for the bar's menu. A server + /// that cannot list them leaves the bar a label. + pub(super) fn list_databases(&self) { + let Some((session, tasks)) = self + .deps() + .and_then(|deps| deps.session.get().map(|session| (session, deps.tasks.clone()))) + else { + return; + }; + let several = session + .current_dialect() + .is_some_and(|dialect| dialect.database_layout() != DatabaseLayout::Single); + if !several { + return; + } + let listing = tasks.spawn_task(async move { + session + .run(OperationKind::Metadata, |lease, options| async move { + Ok(lease.list_databases(options).await?) + }) + .await + }); + let editor = self.downgrade(); + glib::spawn_future_local(async move { + let databases = match listing.await { + Ok(Ok(databases)) => databases, + Ok(Err(error)) => { + tracing::debug!(%error, "the server's databases could not be listed for the editor bar"); + return; + } + Err(failure) => { + tracing::debug!(%failure, "listing the server's databases failed"); + return; + } + }; + if let Some(editor) = editor.upgrade() { + let imp = editor.imp(); + let own = databases.iter().find(|database| database.is_default); + imp.listed_own_database + .replace(own.map(|database| database.name.clone())); + imp.scope_button.set_menu_model(database_menu(&databases).as_ref()); + editor.show_scope(); + } + }); + } + + /// Point the tab at `database`: its next run goes there, completion + /// resolves names there, and the bar names it. + pub(crate) fn move_to_database(&self, database: &str) { + if self.imp().slot.database().as_deref() == Some(database) { + return; + } + self.imp().slot.set_database(database.to_owned()); + self.show_scope(); + } + + /// The database a restore puts the tab back in: `None` while it is + /// the connection's own. + pub(crate) fn tab_database(&self) -> Option { + let database = self.imp().slot.database()?; + let own = self + .deps() + .and_then(|deps| deps.session.get()) + .and_then(|session| self.own_database(&session)); + (own.as_deref() != Some(database.as_str())).then_some(database) + } + + pub(crate) fn connect_database_changed(&self, f: F) -> glib::SignalHandlerId { + self.connect_closure( + "database-changed", + false, + glib::closure_local!(move |editor: &Self| f(editor)), + ) + } + + /// Where unqualified names in this tab resolve, for what the assistant + /// says about it. + pub(crate) fn catalog_home(&self) -> CatalogHome { + self.imp().catalog_home.borrow().clone() + } + + fn follow_in_completion(&self, home: CatalogHome) { + self.imp().catalog_home.replace(home.clone()); + for provider in self.imp().completion_providers.borrow().iter() { + if let Some(provider) = provider.downcast_ref::() { + provider.set_home(home.clone()); + } + } + } + fn set_scope(&self, scope: Option<(String, String)>) { let imp = self.imp(); - let shown = scope.is_some(); - if let Some((text, spoken)) = scope { - imp.scope_label.set_label(&text); - imp.scope_box - .update_property(&[gtk4::accessible::Property::Label(&spoken)]); - imp.scope_box.set_tooltip_text(Some(&spoken)); - } - imp.scope_box.set_visible(shown); + let switchable = imp.scope_button.menu_model().is_some(); + let Some((text, spoken)) = scope else { + imp.scope_box.set_visible(false); + imp.scope_button.set_visible(false); + return; + }; + imp.scope_label.set_label(&text); + imp.scope_button_label.set_label(&text); + imp.scope_box + .update_property(&[gtk4::accessible::Property::Label(&spoken)]); + imp.scope_box.set_tooltip_text(Some(&spoken)); + imp.scope_button + .update_property(&[gtk4::accessible::Property::Label(&spoken)]); + imp.scope_box.set_visible(!switchable); + imp.scope_button.set_visible(switchable); } } #[cfg(test)] mod tests { - use super::scope_text; + use gtk4::gio; + use gtk4::prelude::*; + use tablepro_core::dialect::DatabaseLayout; + use tablepro_core::meta::SchemaInfo; + + use super::{CatalogHome, catalog_home, database_menu, scope_text}; #[test] fn the_scope_names_the_database_then_its_schema() { @@ -110,4 +277,69 @@ mod tests { assert_eq!(scope_text("shop", None), ("shop".to_owned(), "shop".to_owned())); assert_eq!(scope_text("shop", Some("")), ("shop".to_owned(), "shop".to_owned())); } + + fn database(name: &str, is_system: bool) -> SchemaInfo { + SchemaInfo { + name: name.to_owned(), + is_default: false, + is_system, + } + } + + fn labels(menu: &gio::Menu, section: i32) -> Vec { + let section = menu.item_link(section, "section").expect("a section"); + (0..section.n_items()) + .filter_map(|item| { + section + .item_attribute_value(item, "label", None) + .and_then(|label| label.str().map(str::to_owned)) + }) + .collect() + } + + #[test] + fn the_menu_lists_the_users_databases_then_the_engines_own() { + let menu = database_menu(&[ + database("mysql", true), + database("shop", false), + database("analytics", false), + database("information_schema", true), + ]) + .expect("a menu"); + + assert_eq!(menu.n_items(), 2, "two sections"); + assert_eq!(labels(&menu, 0), ["shop", "analytics"]); + assert_eq!( + labels(&menu, 1), + ["mysql", "information__schema"], + "an underscore is no mnemonic" + ); + let own = SchemaInfo { + is_default: true, + ..database("shop", false) + }; + assert!(database_menu(&[own]).is_none(), "nothing to move to"); + assert!( + database_menu(&[database("shop", false)]).is_some(), + "a connection in no database can pick its one" + ); + } + + /// A database the catalog does not describe offers no tables, rather + /// than the connection's under the wrong database. + #[test] + fn completion_resolves_where_the_catalog_reaches() { + assert_eq!( + catalog_home(DatabaseLayout::AsSchemas, Some("shop"), "shop"), + CatalogHome::Connection + ); + assert_eq!( + catalog_home(DatabaseLayout::AsSchemas, Some("shop"), "analytics"), + CatalogHome::Schema("analytics".to_owned()) + ); + assert_eq!( + catalog_home(DatabaseLayout::WithSchemas, Some("shop"), "analytics"), + CatalogHome::Elsewhere("analytics".to_owned()) + ); + } } diff --git a/linux/crates/app/src/ui/editor/tests.rs b/linux/crates/app/src/ui/editor/tests.rs index f0a2d0dec2..a960c9fe1e 100644 --- a/linux/crates/app/src/ui/editor/tests.rs +++ b/linux/crates/app/src/ui/editor/tests.rs @@ -2622,9 +2622,18 @@ fn a_command_editor_has_none_of_the_sql_tools() { /// An editor on a connection to `shop`, whose catalog lists `schemas` /// with the first as the default. fn scoped_editor(dialect: Arc, schemas: &[&str]) -> Harness { + scoped_editor_with(dialect, schemas, answering(), |_, _| {}) +} + +fn scoped_editor_with( + dialect: Arc, + schemas: &[&str], + runs: Replies, + setup: impl FnOnce(&mut ConnectionScript, &mut tablepro_session::connect::ConnectRequest), +) -> Harness { crate::register_test_resources(); let runtime = crate::test_support::test_runtime(); - let fake = Arc::new(FakeEditorSession::new(answering())); + let fake = Arc::new(FakeEditorSession::new(runs)); let mut request = FakeConnector::network_request("db.example", 5432).expect("a request"); if let tablepro_session::connect::RequestTarget::Network { database, .. } = &mut request.target { *database = Some("shop".to_owned()); @@ -2633,6 +2642,7 @@ fn scoped_editor(dialect: Arc, schemas: dialect, ..ConnectionScript::default() }; + setup(&mut script, &mut request); let editor_session: Arc = fake.clone(); script.open_editor_session.always(FakeReply::ok(editor_session)); let (pending, _) = FakeConnector::pending_session(request, script); @@ -2731,3 +2741,191 @@ fn the_bar_follows_a_use_the_tab_ran() { assert_eq!(scope_shown(&harness).as_deref(), Some("analytics")); } + +fn mysql_dialect() -> Arc { + Arc::new(tablepro_driver_mysql::MySqlDialect::new( + tablepro_driver_mysql::MySqlFlavour::MySql, + &tablepro_core::ServerInfo::new("MySQL", "8.4.0", 8, 4, 0), + )) +} + +/// A MySQL connection to `shop` on a server that also holds `analytics`. +fn databases_editor(runs: Replies, read_only: bool) -> Harness { + let database = |name: &str, is_default: bool, is_system: bool| tablepro_core::meta::SchemaInfo { + name: name.to_owned(), + is_default, + is_system, + }; + let harness = scoped_editor_with(mysql_dialect(), &["shop"], runs, |script, _| { + script.list_databases.always(FakeReply::ok(vec![ + database("analytics", false, false), + database("shop", true, false), + database("mysql", false, true), + ])); + script.read_only = read_only; + }); + let button = harness.editor.imp().scope_button.get(); + wait_until(WAIT, || button.is_visible()).expect("the database menu"); + harness +} + +fn pick_database(harness: &Harness, name: &str) { + WidgetExt::activate_action(&harness.editor, "editor.database", Some(&name.to_variant())).expect("editor.database"); +} + +/// Picking a database moves the tab: the bar names it at once, and the +/// next run moves the session there before its statements. +#[gtk4::test] +fn picking_a_database_moves_the_tab_and_its_next_run() { + let harness = databases_editor(answering(), false); + let changed = SignalLog::connect(&harness.editor, "database-changed", |_| ()); + + pick_database(&harness, "analytics"); + assert_eq!(harness.editor.imp().scope_button_label.label(), "analytics"); + assert_eq!(changed.count(), 1, "the workspace keeps where the tab is"); + + run_and_wait(&harness); + assert_eq!(harness.fake.moves(), ["analytics"]); + assert_eq!( + sent(&harness.fake), + ["SELECT 1"], + "the move is the session's, not a statement" + ); + assert_eq!(harness.editor.tab_database().as_deref(), Some("analytics")); + + pick_database(&harness, "shop"); + run_and_wait(&harness); + assert_eq!( + harness.fake.moves(), + ["analytics", "shop"], + "back to the connection's own" + ); + assert_eq!(harness.editor.tab_database(), None); +} + +/// A connection opened without a database name is in the one the server +/// calls its own, which the bar names and the menu checks. +#[gtk4::test] +fn a_connection_without_a_database_name_names_the_servers() { + let database = |name: &str, is_default: bool| tablepro_core::meta::SchemaInfo { + name: name.to_owned(), + is_default, + is_system: false, + }; + let harness = scoped_editor_with(mysql_dialect(), &[], answering(), |script, request| { + if let tablepro_session::connect::RequestTarget::Network { database, .. } = &mut request.target { + *database = None; + } + script + .list_databases + .always(FakeReply::ok(vec![database("analytics", false), database("app", true)])); + }); + let button = harness.editor.imp().scope_button.get(); + wait_until(WAIT, || button.is_visible()).expect("the database menu"); + + assert_eq!(harness.editor.imp().scope_button_label.label(), "app"); + pick_database(&harness, "app"); + assert_eq!(harness.editor.tab_database(), None, "the connection's own"); +} + +/// A connection in no database yet still offers the server's. +#[gtk4::test] +fn a_connection_in_no_database_offers_one() { + let database = |name: &str| tablepro_core::meta::SchemaInfo { + name: name.to_owned(), + is_default: false, + is_system: false, + }; + let harness = scoped_editor_with(mysql_dialect(), &[], answering(), |script, request| { + if let tablepro_session::connect::RequestTarget::Network { database, .. } = &mut request.target { + *database = None; + } + script.list_databases.always(FakeReply::ok(vec![database("analytics")])); + }); + let button = harness.editor.imp().scope_button.get(); + wait_until(WAIT, || button.is_visible()).expect("the database menu"); + + assert_eq!(harness.editor.imp().scope_button_label.label(), "No Database"); + pick_database(&harness, "analytics"); + assert_eq!(harness.editor.imp().scope_button_label.label(), "analytics"); +} + +/// All Rows runs the statement again on the connection, in another +/// database than a moved tab's, so only the rows shown are offered. +#[gtk4::test] +fn a_moved_tab_exports_the_rows_it_shows_only() { + let mut runs = Replies::default(); + runs.always(result_set(3, Some(40), Truncation::ReadPastLimit)); + let harness = databases_editor(runs, false); + let offers = SignalLog::connect(&harness.editor, "export-results", |values| { + values[1] + .get::() + .expect("an offer") + .borrow::() + .clone() + }); + pick_database(&harness, "analytics"); + run_and_wait(&harness); + + shown_banner(&harness).emit_by_name::<()>("button-clicked", &[]); + + let offers = offers.take(); + let [offer] = offers.as_slice() else { + panic!("one offer, not {}", offers.len()); + }; + assert!(matches!(offer.origin, ExportOrigin::Shown { .. }), "{:?}", offer.origin); +} + +/// The assistant describes the database the tab in front is in. +#[gtk4::test] +fn the_assistant_reads_the_tabs_database() { + let harness = databases_editor(answering(), false); + assert_eq!(harness.editor.catalog_home(), CatalogHome::Connection); + + pick_database(&harness, "analytics"); + + assert_eq!( + harness.editor.catalog_home(), + CatalogHome::Schema("analytics".to_owned()) + ); +} + +/// A database the server will not move to leaves the tab where its +/// session is, and the run says why. +#[gtk4::test] +fn a_refused_move_leaves_the_tab_where_its_session_is() { + let harness = databases_editor(answering(), false); + harness + .fake + .refuse_moves(DriverError::server("Access denied to database 'analytics'")); + + pick_database(&harness, "analytics"); + run_and_wait(&harness); + + assert!(sent(&harness.fake).is_empty(), "the script ran in the wrong database"); + assert_eq!(harness.editor.imp().scope_button_label.label(), "shop"); + let log = harness.editor.imp().log.borrow(); + let said = log.as_ref().and_then(|log| log.first_failure()).unwrap_or_default(); + assert!(said.contains("analytics") && said.contains("Access denied"), "{said}"); +} + +/// A `USE` is no write, so a read-only connection moves too. +#[gtk4::test] +fn a_read_only_connection_offers_its_databases() { + let harness = databases_editor(answering(), true); + + pick_database(&harness, "analytics"); + run_and_wait(&harness); + + assert_eq!(harness.fake.moves(), ["analytics"]); +} + +/// A tab never leaves its transaction behind in another database. +#[gtk4::test] +fn a_tab_holding_a_transaction_stays_where_it_is() { + let harness = databases_editor(transaction_runs(), false); + run_and_wait(&harness); + let action = imp_action(&harness.editor, "database").expect("the action"); + + assert!(!action.is_enabled(), "the tab could leave its transaction"); +} diff --git a/linux/crates/app/src/ui/error_text/unsupported.rs b/linux/crates/app/src/ui/error_text/unsupported.rs index cd7aae5416..c61ae79ef1 100644 --- a/linux/crates/app/src/ui/error_text/unsupported.rs +++ b/linux/crates/app/src/ui/error_text/unsupported.rs @@ -47,6 +47,7 @@ pub(super) fn unsupported_message(feature: UnsupportedFeature) -> String { UnsupportedFeature::Tables => gettext("This database holds keys, not tables."), UnsupportedFeature::Keys => gettext("This database holds tables, not keys."), UnsupportedFeature::CharacterSets => gettext("This database has no character sets to pick from."), + UnsupportedFeature::DatabaseSwitching => gettext("This connection cannot switch to another database."), UnsupportedFeature::EditorCommand { command } => gettext_f( "{command} cannot run in the editor: it turns the connection into a stream the editor cannot show.", &[("command", command)], @@ -84,6 +85,7 @@ mod tests { UnsupportedFeature::Tables, UnsupportedFeature::Keys, UnsupportedFeature::CharacterSets, + UnsupportedFeature::DatabaseSwitching, UnsupportedFeature::EditorCommand { command: "MONITOR" }, ]; diff --git a/linux/crates/app/src/ui/workspace/assistant.rs b/linux/crates/app/src/ui/workspace/assistant.rs index a4c788ba03..9bbcc91590 100644 --- a/linux/crates/app/src/ui/workspace/assistant.rs +++ b/linux/crates/app/src/ui/workspace/assistant.rs @@ -19,6 +19,7 @@ use crate::application::AppServices; use crate::runtime::DeliverInput; use crate::services::active_session::ActiveSession; use crate::ui::assistant::{AssistantAccess, AssistantPane, AssistantRequest, Availability, PaneHooks}; +use crate::ui::editor::CatalogHome; use crate::ui::handler_guard::HandlerGuard; pub(super) struct AssistantColumn { @@ -52,6 +53,10 @@ pub(super) fn install(parts: &WorkspaceTabParts, input: &WorkspaceSender) -> Ass parts.index.clone(), parts.tab_view.downgrade(), ); + { + let (tabs, index, view) = (tabs.clone(), index.clone(), view.clone()); + access.follow_front(move || active_editor_home(&tabs, &index, &view)); + } let pane: AssistantPane = glib::Object::new(); pane.bind(PaneHooks { access: access.clone(), @@ -81,6 +86,19 @@ pub(super) fn install(parts: &WorkspaceTabParts, input: &WorkspaceSender) -> Ass } } +/// Where this session's editor tab on screen resolves names: its +/// database, when it moved off the connection's. +fn active_editor_home(tabs: &WorkspaceTabs, index: &TabIndex, view: &glib::WeakRef) -> CatalogHome { + let id = view.upgrade().and_then(|view| index.id_of(&view.selected_page()?)); + match id.and_then(|id| match tabs.borrow().get(&id) { + Some(WorkspaceTab::Editor(slot)) => Some(slot.editor.catalog_home()), + _ => None, + }) { + Some(home) => home, + None => CatalogHome::Connection, + } +} + /// The text of this session's editor tab while it is the one on screen. fn active_editor_text(tabs: &WorkspaceTabs, index: &TabIndex, view: &glib::WeakRef) -> Option { let id = index.id_of(&view.upgrade()?.selected_page()?)?; diff --git a/linux/crates/app/src/ui/workspace/mod.rs b/linux/crates/app/src/ui/workspace/mod.rs index 200a2f17ad..0bfa005a70 100644 --- a/linux/crates/app/src/ui/workspace/mod.rs +++ b/linux/crates/app/src/ui/workspace/mod.rs @@ -283,6 +283,7 @@ pub enum ClosedTabDescriptor { Editor { query: String, file: Option, + database: Option, }, Table { schema: Option, @@ -499,6 +500,8 @@ pub(crate) enum WorkspaceInput { /// An editor tab was saved, opened a file, or its text came to /// differ from its file's or match it again. EditorTabFileChanged(Uuid), + /// An editor tab moved to another database, which its record keeps. + EditorTabDatabaseChanged, /// The session's saved tabs and their editor text, read off the GTK /// thread. WorkspaceLoaded(workspace_tabs::LoadedWorkspace), @@ -1263,6 +1266,7 @@ impl WorkspaceModel { } WorkspaceInput::EditorTabContentChanged(id) => self.on_editor_tab_content_changed(id), WorkspaceInput::EditorTabFileChanged(id) => self.on_editor_tab_file_changed(id), + WorkspaceInput::EditorTabDatabaseChanged => self.persist_workspace_state(), WorkspaceInput::WorkspaceLoaded(loaded) => self.on_workspace_loaded(loaded), WorkspaceInput::ResetWorkspaceLayout => self.on_reset_workspace_layout(), WorkspaceInput::SafeModeChanged => self.refresh_title(), diff --git a/linux/crates/app/src/ui/workspace/tab_factory.rs b/linux/crates/app/src/ui/workspace/tab_factory.rs index 021e835596..49a33e49fb 100644 --- a/linux/crates/app/src/ui/workspace/tab_factory.rs +++ b/linux/crates/app/src/ui/workspace/tab_factory.rs @@ -100,7 +100,8 @@ impl TabFactory { initial_query, draft, file, - } => self.editor(id, initial_query, draft, file), + database, + } => self.editor(id, initial_query, draft, file, database), TabSpec::Table { table, page, filter } => self.table( id, TableOpening { @@ -128,7 +129,7 @@ impl TabFactory { /// The overview's new-tab button needs its page back at once. pub(super) fn create_editor(&self) -> adw::TabPage { let id = Uuid::new_v4(); - let slot = self.editor(id, None, None, None); + let slot = self.editor(id, None, None, None, None); self.insert(id, slot) } @@ -167,6 +168,7 @@ impl TabFactory { initial_query: Option, draft: Option, file: Option, + database: Option, ) -> WorkspaceTab { let parts = &self.parts; // Such a tab shows the editor's sample query, which names nothing. @@ -181,6 +183,9 @@ impl TabFactory { close_timeout: parts.context.close_timeout(), snippets: parts.snippets.clone(), }); + if let Some(database) = database { + editor.move_to_database(&database); + } connect_editor(&editor, id, &parts.input, &parts.toasts); editor.attach_assistant(parts.assistant.clone()); // After the signals are connected, so the workspace marks a tab @@ -491,6 +496,8 @@ pub(super) fn connect_editor( editor.connect_content_changed(move |_| content.deliver(WorkspaceInput::EditorTabContentChanged(tab_id))); let file = input.clone(); editor.connect_file_changed(move |_| file.deliver(WorkspaceInput::EditorTabFileChanged(tab_id))); + let database = input.clone(); + editor.connect_database_changed(move |_| database.deliver(WorkspaceInput::EditorTabDatabaseChanged)); let transaction = input.clone(); editor.connect_transaction_changed(move |_, _| transaction.deliver(WorkspaceInput::EditorTransactionChanged)); let toast = input.clone(); diff --git a/linux/crates/app/src/ui/workspace/tab_spec.rs b/linux/crates/app/src/ui/workspace/tab_spec.rs index 3872f9995c..ad6bdbf233 100644 --- a/linux/crates/app/src/ui/workspace/tab_spec.rs +++ b/linux/crates/app/src/ui/workspace/tab_spec.rs @@ -16,6 +16,8 @@ pub(crate) enum TabSpec { draft: Option, /// The `.sql` file a restored or reopened tab is saved to. file: Option, + /// The database the editor bar moved a restored tab to. + database: Option, }, Table { table: TableRef, diff --git a/linux/crates/app/src/ui/workspace/tests/assistant.rs b/linux/crates/app/src/ui/workspace/tests/assistant.rs index 49da7d47d9..10c9a07f6a 100644 --- a/linux/crates/app/src/ui/workspace/tests/assistant.rs +++ b/linux/crates/app/src/ui/workspace/tests/assistant.rs @@ -93,6 +93,7 @@ fn insert_goes_to_the_editor_in_front_or_a_new_one() { initial_query: Some("-- ".to_owned()), draft: None, file: None, + database: None, }) }); input.deliver(WorkspaceInput::InsertFromAssistant("SELECT 2;".to_owned())); diff --git a/linux/crates/app/src/ui/workspace/tests/close_tab.rs b/linux/crates/app/src/ui/workspace/tests/close_tab.rs index d26cb97d06..39bb3ad8a1 100644 --- a/linux/crates/app/src/ui/workspace/tests/close_tab.rs +++ b/linux/crates/app/src/ui/workspace/tests/close_tab.rs @@ -189,6 +189,7 @@ fn editor_on_file(test: &TestWindow, path: &std::path::Path) -> (Uuid, crate::ui uri: gtk::gio::File::for_path(path).uri().to_string(), modified: true, }), + database: None, }) }); let id = workspace.selected_tab().expect("the editor in front"); @@ -287,6 +288,7 @@ fn an_editor_asking_to_close_closes_its_own_tab_not_the_one_in_front() { initial_query: Some(query.to_owned()), draft: None, file: None, + database: None, }) }); let page = shell_of(&workspace) diff --git a/linux/crates/app/src/ui/workspace/tests/tab_title.rs b/linux/crates/app/src/ui/workspace/tests/tab_title.rs index ee27913289..92cd1ed168 100644 --- a/linux/crates/app/src/ui/workspace/tests/tab_title.rs +++ b/linux/crates/app/src/ui/workspace/tests/tab_title.rs @@ -184,6 +184,7 @@ fn restored_workspace_selects_saved_active_tab() { TabRecord::Editor { draft: DraftId::new(), file: None, + database: None, }, orders, ], @@ -229,6 +230,7 @@ fn a_restored_editor_tab_keeps_its_file_and_its_unsaved_changes() { uri: gio::File::for_path(&path).uri().to_string(), modified: true, }), + database: None, }], }, ) diff --git a/linux/crates/app/src/ui/workspace/workspace_persistence.rs b/linux/crates/app/src/ui/workspace/workspace_persistence.rs index 5eabf1fddf..ec2000f63e 100644 --- a/linux/crates/app/src/ui/workspace/workspace_persistence.rs +++ b/linux/crates/app/src/ui/workspace/workspace_persistence.rs @@ -191,6 +191,7 @@ fn tab_record(tab: &WorkspaceTab) -> Option { WorkspaceTab::Editor(slot) => Some(TabRecord::Editor { draft: slot.draft, file: super::workspace_tabs::editor_file_record(&slot.editor), + database: slot.editor.tab_database(), }), WorkspaceTab::Structure(slot) => match slot.mode { // A draft for a table that does not exist yet has nothing to @@ -504,6 +505,7 @@ mod tests { TabRecord::Editor { draft: DraftId::new(), file: None, + database: None, }, ], }; diff --git a/linux/crates/app/src/ui/workspace/workspace_tabs.rs b/linux/crates/app/src/ui/workspace/workspace_tabs.rs index cf2de5f6e5..4b2f884211 100644 --- a/linux/crates/app/src/ui/workspace/workspace_tabs.rs +++ b/linux/crates/app/src/ui/workspace/workspace_tabs.rs @@ -131,10 +131,11 @@ impl WorkspaceModel { TabRecord::Diagram { schema, places } => TabSpec::Diagram { schema, places }, // Its text is on disk already, or there is none to keep, so // nothing is written until the user types. - TabRecord::Editor { draft, file } => TabSpec::Editor { + TabRecord::Editor { draft, file, database } => TabSpec::Editor { initial_query: Some(drafts.remove(&draft).unwrap_or_default()), draft: Some(draft), file, + database, }, TabRecord::Key { key } => TabSpec::Key { key }, }; @@ -188,6 +189,7 @@ impl WorkspaceModel { initial_query, draft: None, file: None, + database: None, }); } @@ -662,6 +664,7 @@ fn describe_closed_tab(slot: &WorkspaceTab) -> Option { WorkspaceTab::Editor(s) => Some(ClosedTabDescriptor::Editor { query: whole_text(&s.editor.buffer()), file: editor_file_record(&s.editor), + database: s.editor.tab_database(), }), WorkspaceTab::Table(s) => Some(ClosedTabDescriptor::Table { schema: s.schema.clone(), @@ -693,12 +696,13 @@ impl WorkspaceModel { /// Open again a tab this session closed, which the shell kept. pub(super) fn on_reopen_closed_tab(&mut self, descriptor: ClosedTabDescriptor) { match descriptor { - ClosedTabDescriptor::Editor { query, file } => { + ClosedTabDescriptor::Editor { query, file, database } => { let initial = if query.is_empty() { None } else { Some(query) }; self.open_editor(TabSpec::Editor { initial_query: initial, draft: None, file, + database, }); } ClosedTabDescriptor::Table { diff --git a/linux/crates/app/src/workspace/tab_record.rs b/linux/crates/app/src/workspace/tab_record.rs index fa4b714d69..6717b8b47c 100644 --- a/linux/crates/app/src/workspace/tab_record.rs +++ b/linux/crates/app/src/workspace/tab_record.rs @@ -44,6 +44,10 @@ pub enum TabRecord { draft: DraftId, #[serde(default, skip_serializing_if = "Option::is_none")] file: Option, + /// The database the editor bar moved the tab to. `None` keeps the + /// connection's own, as in a record written before tabs kept one. + #[serde(default, skip_serializing_if = "Option::is_none")] + database: Option, }, /// A key-value engine's key, by the name the sidebar lists it as. Key { @@ -106,7 +110,7 @@ impl TabRecord { } Self::Structure { schema, table } => (!table.is_empty()).then_some(Self::Structure { schema, table }), Self::Diagram { schema, places } => Some(Self::Diagram { schema, places }), - Self::Editor { draft, file } => Some(Self::Editor { draft, file }), + Self::Editor { draft, file, database } => Some(Self::Editor { draft, file, database }), // A key's name can be empty, as Redis lets one be. Self::Key { key } => Some(Self::Key { key }), } diff --git a/linux/crates/app/src/workspace/workspace_record.rs b/linux/crates/app/src/workspace/workspace_record.rs index a433588b51..06f994c9e6 100644 --- a/linux/crates/app/src/workspace/workspace_record.rs +++ b/linux/crates/app/src/workspace/workspace_record.rs @@ -120,6 +120,7 @@ mod tests { TabRecord::Editor { draft: DraftId::new(), file: None, + database: None, }, TabRecord::Diagram { schema: Some("sales".to_owned()), @@ -182,6 +183,7 @@ mod tests { TabRecord::Editor { draft: DraftId::from_uuid(draft), file: None, + database: None, }, TabRecord::Structure { schema: Some("sales".to_owned()), @@ -231,6 +233,7 @@ mod tests { TabRecord::Editor { draft: DraftId::from_uuid(draft), file: None, + database: None, }, TabRecord::Structure { schema: Some("sales".to_owned()), @@ -262,6 +265,7 @@ mod tests { 0 => TabRecord::Editor { draft: DraftId::new(), file: None, + database: None, }, _ => table(&format!("t{index}")), }) diff --git a/linux/crates/app/src/workspace/workspace_record_builder.rs b/linux/crates/app/src/workspace/workspace_record_builder.rs index 7e81155c91..0fa4922ef7 100644 --- a/linux/crates/app/src/workspace/workspace_record_builder.rs +++ b/linux/crates/app/src/workspace/workspace_record_builder.rs @@ -67,6 +67,7 @@ mod tests { page(Some(TabRecord::Editor { draft: DraftId::new(), file: None, + database: None, })), page(None), ]; diff --git a/linux/crates/app/src/workspace/workspace_store.rs b/linux/crates/app/src/workspace/workspace_store.rs index fd01fedd2a..954e1e52ba 100644 --- a/linux/crates/app/src/workspace/workspace_store.rs +++ b/linux/crates/app/src/workspace/workspace_store.rs @@ -536,6 +536,7 @@ mod tests { tabs: vec![TabRecord::Editor { draft: DraftId::new(), file: None, + database: None, }], }; @@ -684,6 +685,7 @@ mod tests { .map(|_| TabRecord::Editor { draft: DraftId::new(), file: None, + database: None, }) .collect(), }; diff --git a/linux/crates/core/src/connection.rs b/linux/crates/core/src/connection.rs index 32b3f44884..43ea64fd27 100644 --- a/linux/crates/core/src/connection.rs +++ b/linux/crates/core/src/connection.rs @@ -103,6 +103,14 @@ pub trait Connection: Send + Sync { }) } + /// The server's databases the login can open, the connection's own + /// marked as the default and the engine's own as system. + async fn list_databases(&self, _options: CallOptions) -> Result, DriverError> { + Err(DriverError::Unsupported { + feature: UnsupportedFeature::DatabaseSwitching, + }) + } + async fn row_identity(&self, table: &TableRef, options: CallOptions) -> Result; async fn table_estimate(&self, table: &TableRef, options: CallOptions) -> Result; diff --git a/linux/crates/core/src/editor_session.rs b/linux/crates/core/src/editor_session.rs index 5e29af3596..ea2d6d1662 100644 --- a/linux/crates/core/src/editor_session.rs +++ b/linux/crates/core/src/editor_session.rs @@ -78,5 +78,14 @@ pub trait EditorSession: Send + Sync { None } + /// Move the session to the server's database `database`, keeping + /// what it holds. Unsupported where the engine binds a session's + /// database when it connects. + async fn use_database(&self, _database: &str, _options: CallOptions) -> Result<(), DriverError> { + Err(DriverError::Unsupported { + feature: crate::UnsupportedFeature::DatabaseSwitching, + }) + } + async fn close(&self); } diff --git a/linux/crates/core/src/unsupported_feature.rs b/linux/crates/core/src/unsupported_feature.rs index 6eff4bed47..d42e81a6b2 100644 --- a/linux/crates/core/src/unsupported_feature.rs +++ b/linux/crates/core/src/unsupported_feature.rs @@ -48,6 +48,8 @@ pub enum UnsupportedFeature { /// A list of character sets and collations to pick a column's or a /// table's from. CharacterSets, + /// Another of the server's databases for a query tab. + DatabaseSwitching, /// A command the editor cannot run, because it stops the one reply /// per command the editor reads, such as `SUBSCRIBE`. EditorCommand { @@ -79,6 +81,7 @@ impl fmt::Display for UnsupportedFeature { Self::Tables => f.write_str("tables"), Self::Keys => f.write_str("keys"), Self::CharacterSets => f.write_str("character sets"), + Self::DatabaseSwitching => f.write_str("database switching"), Self::EditorCommand { command } => write!(f, "{command} in the editor"), } } diff --git a/linux/crates/drivers/mysql/src/connection.rs b/linux/crates/drivers/mysql/src/connection.rs index 5137000983..3a83c808f0 100644 --- a/linux/crates/drivers/mysql/src/connection.rs +++ b/linux/crates/drivers/mysql/src/connection.rs @@ -271,6 +271,14 @@ impl Connection for MysqlConnection { .await } + async fn list_databases(&self, options: CallOptions) -> Result, DriverError> { + self.read( + options, + read_op(|connection, flavour| Box::pin(metadata::list_databases(connection, flavour))), + ) + .await + } + async fn list_objects(&self, scope: &ObjectScope, options: CallOptions) -> Result, DriverError> { let scope = scope.clone(); self.read( diff --git a/linux/crates/drivers/mysql/src/dialect.rs b/linux/crates/drivers/mysql/src/dialect.rs index 89cd87eea7..0cd78210fe 100644 --- a/linux/crates/drivers/mysql/src/dialect.rs +++ b/linux/crates/drivers/mysql/src/dialect.rs @@ -60,6 +60,10 @@ impl MySqlDialect { } } +pub(crate) fn quote(name: &str) -> String { + format!("`{}`", name.replace('`', "``")) +} + impl SqlDialect for MySqlDialect { fn database_layout(&self) -> DatabaseLayout { DatabaseLayout::AsSchemas @@ -79,7 +83,7 @@ impl SqlDialect for MySqlDialect { } fn quote_identifier(&self, name: &str) -> String { - format!("`{}`", name.replace('`', "``")) + quote(name) } fn placeholder(&self, _ordinal: usize, value: Value, target: BindTarget<'_>) -> Result { diff --git a/linux/crates/drivers/mysql/src/editor_session.rs b/linux/crates/drivers/mysql/src/editor_session.rs index d2a5587a7a..681243cc05 100644 --- a/linux/crates/drivers/mysql/src/editor_session.rs +++ b/linux/crates/drivers/mysql/src/editor_session.rs @@ -16,6 +16,7 @@ use tokio_util::sync::CancellationToken; use crate::cancel::ControlLink; use crate::decode::decode_row; +use crate::dialect::quote; use crate::editor_run::{RunOutcome, run_script}; use crate::errors::map_error; use crate::flavour::MySqlFlavour; @@ -355,6 +356,58 @@ impl EditorSession for MysqlEditorSession { .clone() } + /// `USE` carries any name: a backtick in it is doubled. + async fn use_database(&self, database: &str, options: CallOptions) -> Result<(), DriverError> { + let shared = &self.shared; + let stopped = || match options.cancel().is_cancelled() { + true => DriverError::Cancelled, + false => DriverError::Timeout { + phase: TimeoutPhase::Statement, + server_cancelled: false, + }, + }; + let mut guard = tokio::select! { + biased; + () = options.cancel().cancelled() => return Err(stopped()), + () = elapsed(options.deadline()) => return Err(stopped()), + guard = shared.held.lock() => guard, + }; + let Some(held) = guard.as_mut().filter(|_| !shared.lost.load(Ordering::SeqCst)) else { + return Err(DriverError::ConnectionLost { + during: LossPhase::Idle, + }); + }; + let statement = format!("USE {}", quote(database)); + let using = held.connection.execute(sqlx::raw_sql(AssertSqlSafe(statement))); + let used = tokio::select! { + biased; + () = options.cancel().cancelled() => None, + () = elapsed(options.deadline()) => None, + used = using => Some(used), + }; + match used { + Some(Ok(_)) => { + *shared.database.lock().unwrap_or_else(PoisonError::into_inner) = Some(database.to_owned()); + Ok(()) + } + Some(Err(error)) => { + let error = map_error(&error, shared.flavour); + if error.category() == tablepro_core::ErrorCategory::ConnectionLost { + shared.lost.store(true, Ordering::SeqCst); + *guard = None; + } + Err(error) + } + // The answer is still on the wire, so the connection is in no + // state anything knows. + None => { + shared.lost.store(true, Ordering::SeqCst); + *guard = None; + Err(stopped()) + } + } + } + async fn transaction_state(&self, options: CallOptions) -> Result { let shared = &self.shared; let asking = async { diff --git a/linux/crates/drivers/mysql/src/metadata.rs b/linux/crates/drivers/mysql/src/metadata.rs index 2392af170e..ca91863308 100644 --- a/linux/crates/drivers/mysql/src/metadata.rs +++ b/linux/crates/drivers/mysql/src/metadata.rs @@ -85,6 +85,41 @@ pub(crate) async fn list_schemas( .collect()) } +/// The databases MySQL and MariaDB keep for themselves. +const SYSTEM_DATABASES: [&str; 4] = ["information_schema", "mysql", "performance_schema", "sys"]; + +/// Every database the login can see, the connection's own the default. +pub(crate) async fn list_databases( + connection: &mut MySqlConnection, + flavour: MySqlFlavour, +) -> Result, DriverError> { + let current = list_schemas(connection, flavour) + .await? + .into_iter() + .next() + .map(|schema| schema.name); + let rows = catalog_rows( + connection, + flavour, + "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA ORDER BY SCHEMA_NAME", + "SCHEMATA", + &[], + ) + .await?; + let mut databases = Vec::with_capacity(rows.len()); + for row in rows { + let Some(name) = row.text(0)? else { + continue; + }; + databases.push(SchemaInfo { + is_default: current.as_deref() == Some(name.as_str()), + is_system: SYSTEM_DATABASES.contains(&name.as_str()), + name, + }); + } + Ok(databases) +} + const TABLES_IN_SCHEMA_SQL: &str = "\ SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = COALESCE(?, DATABASE()) ORDER BY TABLE_NAME"; diff --git a/linux/crates/drivers/mysql/tests/integration/metadata.rs b/linux/crates/drivers/mysql/tests/integration/metadata.rs index e5c0b7123c..41581b0d72 100644 --- a/linux/crates/drivers/mysql/tests/integration/metadata.rs +++ b/linux/crates/drivers/mysql/tests/integration/metadata.rs @@ -8,7 +8,7 @@ use tablepro_core::statement::StatementBuilder; use tablepro_core::{Connection, RowLimit, Value}; use tablepro_test_fixtures::MysqlFlavour; -use crate::support::{CALL, FLAVOURS, connect, rows, setup, start, table, within}; +use crate::support::{CALL, FLAVOURS, connect, connect_root, rows, setup, start, table, within}; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] @@ -496,3 +496,37 @@ async fn insensitive_filter_matches_mixed_case() { assert_eq!(insensitive, ["Alpha"]); connection.close().await.expect("a close"); } + +/// The databases a query tab can move to: the login's own marked as the +/// default, another one it can see, and the server's own marked as +/// system, which the editor bar lists after the user's. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn the_servers_databases_are_listed_for_a_query_tab() { + for flavour in FLAVOURS { + let fixture = start(flavour).await; + let root = connect_root(&fixture).await; + let grant = format!( + "GRANT ALL ON analytics.* TO '{}'@'%'", + fixture.password_credentials().username + ); + setup( + root.as_ref(), + &["CREATE DATABASE IF NOT EXISTS analytics", grant.as_str()], + ) + .await; + root.close().await.expect("a close"); + let connection = connect(&fixture, false).await; + + let databases = connection.list_databases(within(CALL)).await.expect("the databases"); + + let find = |name: &str| databases.iter().find(|database| database.name == name); + let own = find("tablepro").expect("the login's database"); + assert!(own.is_default && !own.is_system, "{flavour:?}: {own:?}"); + let other = find("analytics").expect("another database"); + assert!(!other.is_default && !other.is_system, "{flavour:?}: {other:?}"); + let system = find("information_schema").expect("information_schema"); + assert!(system.is_system, "{flavour:?}: {system:?}"); + connection.close().await.expect("a close"); + } +} diff --git a/linux/crates/test-support/src/call_counters.rs b/linux/crates/test-support/src/call_counters.rs index 9bf60773b7..9c8683e484 100644 --- a/linux/crates/test-support/src/call_counters.rs +++ b/linux/crates/test-support/src/call_counters.rs @@ -7,6 +7,7 @@ use std::sync::atomic::{AtomicUsize, Ordering}; pub struct CallCounters { pub probe: Arc, pub list_schemas: Arc, + pub list_databases: Arc, pub list_objects: Arc, pub fetch_columns: Arc, pub fetch_indexes: Arc, diff --git a/linux/crates/test-support/src/connection_script.rs b/linux/crates/test-support/src/connection_script.rs index c47947a703..920bc05b24 100644 --- a/linux/crates/test-support/src/connection_script.rs +++ b/linux/crates/test-support/src/connection_script.rs @@ -25,6 +25,7 @@ pub struct ConnectionScript { pub close: CloseBehaviour, pub probe: Replies>, pub list_schemas: Replies>>, + pub list_databases: Replies>>, pub list_objects: Replies>>, pub fetch_columns: Replies>>, pub fetch_indexes: Replies>>, @@ -59,6 +60,7 @@ impl Default for ConnectionScript { close: CloseBehaviour::Immediate, probe: Replies::default(), list_schemas: Replies::default(), + list_databases: Replies::default(), list_objects: Replies::default(), fetch_columns: Replies::default(), fetch_indexes: Replies::default(), diff --git a/linux/crates/test-support/src/fake_connection.rs b/linux/crates/test-support/src/fake_connection.rs index dcb16a3637..22f2ebee53 100644 --- a/linux/crates/test-support/src/fake_connection.rs +++ b/linux/crates/test-support/src/fake_connection.rs @@ -189,6 +189,19 @@ impl Connection for FakeConnection { .await } + async fn list_databases(&self, options: CallOptions) -> Result, DriverError> { + let counter = &self.counters.list_databases; + self.answer( + "list_databases", + counter, + |s| &mut s.list_databases, + None, + &options, + TimeoutPhase::Statement, + ) + .await + } + async fn list_schemas(&self, options: CallOptions) -> Result, DriverError> { let counter = &self.counters.list_schemas; self.answer( diff --git a/linux/crates/test-support/src/fake_editor_session.rs b/linux/crates/test-support/src/fake_editor_session.rs index 1dc4ddfee8..9f0235f719 100644 --- a/linux/crates/test-support/src/fake_editor_session.rs +++ b/linux/crates/test-support/src/fake_editor_session.rs @@ -26,6 +26,8 @@ pub struct FakeEditorSession { state: Mutex, transaction_state: Mutex, database: Mutex>, + moves: Mutex>, + refused_moves: Mutex>, close_behaviour: CloseBehaviour, closes: AtomicUsize, closed: AtomicBool, @@ -46,6 +48,8 @@ impl FakeEditorSession { state: Mutex::new(EditorSessionState::Ready), transaction_state: Mutex::new(TransactionState::Idle), database: Mutex::new(None), + moves: Mutex::new(Vec::new()), + refused_moves: Mutex::new(None), close_behaviour, closes: AtomicUsize::new(0), closed: AtomicBool::new(false), @@ -82,6 +86,16 @@ impl FakeEditorSession { *lock(&self.database) = database.map(str::to_owned); } + /// The databases `use_database` moved the session to, in order. + pub fn moves(&self) -> Vec { + lock(&self.moves).clone() + } + + /// Every later `use_database` fails with `error`. + pub fn refuse_moves(&self, error: DriverError) { + *lock(&self.refused_moves) = Some(error); + } + /// The connection goes while no run is on it, as when the server /// ends a session left idle in a transaction. pub fn lose(&self) { @@ -199,6 +213,15 @@ impl EditorSession for FakeEditorSession { lock(&self.database).clone() } + async fn use_database(&self, database: &str, _options: CallOptions) -> Result<(), DriverError> { + if let Some(error) = lock(&self.refused_moves).clone() { + return Err(error); + } + lock(&self.moves).push(database.to_owned()); + *lock(&self.database) = Some(database.to_owned()); + Ok(()) + } + async fn close(&self) { let first = self.closes.fetch_add(1, Ordering::SeqCst) == 0; self.closing.cancel(); diff --git a/linux/data/resources/ui/sql-editor.ui b/linux/data/resources/ui/sql-editor.ui index f520d2d01d..49da15b076 100644 --- a/linux/data/resources/ui/sql-editor.ui +++ b/linux/data/resources/ui/sql-editor.ui @@ -115,6 +115,33 @@ + + + false + true + Database + + + + 6 + + + drive-harddisk-symbolic + presentation + + + + + middle + presentation + + + + + + true From f2573349e106889d73837b4da22addb56f0413c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sat, 31 Oct 2026 02:29:04 +0700 Subject: [PATCH 30/85] feat(support): ask for a license on the 30th day of use (#3922) --- apple/TablePro/AppDelegate.swift | 1 + .../Services/Licensing/SupportLinks.swift | 3 + .../Support/SupportAskCoordinator.swift | 169 ++ .../Services/Support/SupportAskSchedule.swift | 45 + .../Services/Support/UsageDayCounter.swift | 46 + .../TablePro/Resources/Localizable.xcstrings | 2254 +++++++++-------- .../MainContentCoordinator+Registry.swift | 4 + .../Support/SupportAskSheetPresenter.swift | 84 + .../Views/Support/SupportAskView.swift | 64 + .../TablePro/Views/Support/SupportView.swift | 5 +- .../Licensing/SupportLinksTests.swift | 2 +- .../Support/SupportAskCoordinatorTests.swift | 224 ++ .../Support/SupportAskScheduleTests.swift | 77 + .../Support/UsageDayCounterTests.swift | 71 + apple/changelog.d/3922.added.md | 1 + 15 files changed, 1963 insertions(+), 1087 deletions(-) create mode 100644 apple/TablePro/Core/Services/Support/SupportAskCoordinator.swift create mode 100644 apple/TablePro/Core/Services/Support/SupportAskSchedule.swift create mode 100644 apple/TablePro/Core/Services/Support/UsageDayCounter.swift create mode 100644 apple/TablePro/Views/Support/SupportAskSheetPresenter.swift create mode 100644 apple/TablePro/Views/Support/SupportAskView.swift create mode 100644 apple/TableProTests/Core/Services/Support/SupportAskCoordinatorTests.swift create mode 100644 apple/TableProTests/Core/Services/Support/SupportAskScheduleTests.swift create mode 100644 apple/TableProTests/Core/Services/Support/UsageDayCounterTests.swift create mode 100644 apple/changelog.d/3922.added.md diff --git a/apple/TablePro/AppDelegate.swift b/apple/TablePro/AppDelegate.swift index 60145aca2b..b44751e201 100644 --- a/apple/TablePro/AppDelegate.swift +++ b/apple/TablePro/AppDelegate.swift @@ -145,6 +145,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { SyncCoordinator.shared.start() LinkedFolderWatcher.shared.start() TeamLibrarySyncCoordinator.shared.start() + SupportAskCoordinator.shared.start() Task { LicenseManager.shared.startPeriodicValidation() diff --git a/apple/TablePro/Core/Services/Licensing/SupportLinks.swift b/apple/TablePro/Core/Services/Licensing/SupportLinks.swift index 047650c1fb..d8729f13d0 100644 --- a/apple/TablePro/Core/Services/Licensing/SupportLinks.swift +++ b/apple/TablePro/Core/Services/Licensing/SupportLinks.swift @@ -16,6 +16,7 @@ internal enum SupportReferrer: Equatable { case aboutPanel case licenseSettings case activationSheet + case ask case featureGate(ProFeature) internal var parameterValue: String { @@ -28,6 +29,8 @@ internal enum SupportReferrer: Equatable { return "app-settings" case .activationSheet: return "app-activation" + case .ask: + return "app-ask" case .featureGate(let feature): return "app-gate-" + Self.slug(for: feature) } diff --git a/apple/TablePro/Core/Services/Support/SupportAskCoordinator.swift b/apple/TablePro/Core/Services/Support/SupportAskCoordinator.swift new file mode 100644 index 0000000000..e59b64135c --- /dev/null +++ b/apple/TablePro/Core/Services/Support/SupportAskCoordinator.swift @@ -0,0 +1,169 @@ +// +// SupportAskCoordinator.swift +// TablePro +// + +import AppKit +import Combine +import os + +/// Counts days of use and puts the license ask on the window in use when `SupportAskSchedule` +/// says it is due and the moment is quiet. +/// +/// It looks again whenever a window becomes key, the app becomes active, or the launch quiet +/// period ends, rather than polling: one of those happens soon after any moment the ask could use. +@MainActor +internal final class SupportAskCoordinator { + struct Environment { + var hasLicense: () -> Bool + var isQueryRunning: () -> Bool + var keyWindow: () -> NSWindow? + var isModalSessionRunning: () -> Bool + var openPricing: (URL) -> Void + var now: () -> Date + } + + enum Keys { + static let lastAskedAt = "com.TablePro.support.lastAskedAt" + static let lastAnswer = "com.TablePro.support.lastAnswer" + } + + private static let logger = Logger(subsystem: "com.TablePro", category: "SupportAsk") + + private let counter: UsageDayCounter + private let defaults: UserDefaults + private let presenter: any SupportAskPresenting + private let environment: Environment + private let calendar: Calendar + private let clock: any Clock + + private var launchedAt: Date? + private var subscriptions: Set = [] + private var quietPeriodTask: Task? + + init( + counter: UsageDayCounter, + defaults: UserDefaults, + presenter: any SupportAskPresenting, + environment: Environment, + calendar: Calendar = .autoupdatingCurrent, + clock: any Clock = ContinuousClock() + ) { + self.counter = counter + self.defaults = defaults + self.presenter = presenter + self.environment = environment + self.calendar = calendar + self.clock = clock + } + + func start() { + guard launchedAt == nil else { return } + launchedAt = environment.now() + counter.recordUse(at: environment.now()) + + let center = NotificationCenter.default + center.publisher(for: NSApplication.didBecomeActiveNotification) + .sink { [weak self] _ in self?.appBecameActive() } + .store(in: &subscriptions) + // Posted when the date rolls over while the app runs, from whichever thread noticed. + center.publisher(for: .NSCalendarDayChanged) + .receive(on: DispatchQueue.main) + .sink { [weak self] _ in self?.dayChanged() } + .store(in: &subscriptions) + center.publisher(for: NSWindow.didBecomeKeyNotification) + .sink { [weak self] _ in self?.askIfDue() } + .store(in: &subscriptions) + + quietPeriodTask = Task { [weak self, clock] in + do { + try await clock.sleep(for: .seconds(SupportAskSchedule.launchQuietPeriod)) + } catch { + return + } + self?.askIfDue() + } + } + + var schedule: SupportAskSchedule { + SupportAskSchedule( + usageDays: counter.days, + hasLicense: environment.hasLicense(), + lastAskedAt: defaults.object(forKey: Keys.lastAskedAt) as? Date, + lastAnswer: defaults.string(forKey: Keys.lastAnswer).flatMap(SupportAskAnswer.init(rawValue:)) + ) + } + + /// Presents the ask when everything allows it. Each check is cheap, and the first that fails + /// ends the look. + func askIfDue() { + guard let launchedAt, !presenter.isPresenting else { return } + let now = environment.now() + guard schedule.isDue(at: now, calendar: calendar) else { return } + guard SupportAskSchedule.isQuietMoment( + launchedAt: launchedAt, + now: now, + isQueryRunning: environment.isQueryRunning() + ) else { return } + guard !environment.isModalSessionRunning(), + let window = environment.keyWindow(), + Self.canHostAsk(window) else { return } + + let presented = presenter.present(usageDays: counter.days, on: window) { [weak self] answer in + self?.record(answer) + } + guard presented else { return } + // Recorded on showing rather than on answering, so quitting with the sheet open still + // counts as an ask and the next launch does not repeat it. + defaults.set(now, forKey: Keys.lastAskedAt) + defaults.removeObject(forKey: Keys.lastAnswer) + Self.logger.info("Showed the license ask after \(self.counter.days, privacy: .public) days of use") + } + + /// Only a window someone works in carries the ask: a connection, a data file or the welcome + /// window. Settings, the Support window and other utility windows never do. + static func canHostAsk(_ window: NSWindow) -> Bool { + let identifier = window.identifier?.rawValue + let isWorkWindow = ConnectionWindowIdentity.isPrimaryWindow(identifier) + || ConnectionWindowIdentity.isDataFileWindow(identifier) + || ConnectionWindowIdentity.isWelcomeWindow(identifier) + return isWorkWindow + && window.isVisible + && !window.isMiniaturized + && window.attachedSheet == nil + && window.sheetParent == nil + } + + private func record(_ answer: SupportAskAnswer) { + defaults.set(answer.rawValue, forKey: Keys.lastAnswer) + if answer == .buy { + environment.openPricing(SupportLinks.pricing(.ask)) + } + } + + private func appBecameActive() { + counter.recordUse(at: environment.now()) + askIfDue() + } + + private func dayChanged() { + guard NSApp.isActive else { return } + counter.recordUse(at: environment.now()) + } +} + +extension SupportAskCoordinator { + static let shared = SupportAskCoordinator( + counter: .shared, + defaults: AppStorageEnvironment.shared.defaults, + presenter: SupportAskSheetPresenter(), + environment: Environment( + hasLicense: { LicenseManager.shared.supportAudience == .supporter }, + isQueryRunning: { MainContentCoordinator.isAnyQueryRunning }, + keyWindow: { NSApp.keyWindow }, + isModalSessionRunning: { NSApp.modalWindow != nil }, + openPricing: { NSWorkspace.shared.open($0) }, + now: { Date() } + ) + ) +} diff --git a/apple/TablePro/Core/Services/Support/SupportAskSchedule.swift b/apple/TablePro/Core/Services/Support/SupportAskSchedule.swift new file mode 100644 index 0000000000..77160abdd5 --- /dev/null +++ b/apple/TablePro/Core/Services/Support/SupportAskSchedule.swift @@ -0,0 +1,45 @@ +// +// SupportAskSchedule.swift +// TablePro +// + +import Foundation + +/// How someone answered the license ask. The raw values are stored, so they never change. +internal enum SupportAskAnswer: String, Equatable, Sendable { + case buy + case personal + case notNow +} + +/// When the app may ask someone without a license to buy one. +/// +/// The 30th day of use opens it. Each ask closes it again for 90 days, or for a year once the +/// person says they use TablePro personally, and a license closes it for good. Kept pure, so every +/// rule is tested without a window, a clock or a license. +internal struct SupportAskSchedule: Equatable, Sendable { + static let firstAskDay = 30 + static let daysBetweenAsks = 90 + static let daysAfterPersonalUse = 365 + /// Someone who just launched the app came to do something; the ask waits until they have. + static let launchQuietPeriod: TimeInterval = 5 * 60 + + var usageDays: Int + var hasLicense: Bool + var lastAskedAt: Date? + var lastAnswer: SupportAskAnswer? + + func isDue(at now: Date, calendar: Calendar) -> Bool { + guard !hasLicense, usageDays >= Self.firstAskDay else { return false } + guard let lastAskedAt else { return true } + let waitDays = lastAnswer == .personal ? Self.daysAfterPersonalUse : Self.daysBetweenAsks + guard let nextAsk = calendar.date(byAdding: .day, value: waitDays, to: lastAskedAt) else { return false } + return now >= nextAsk + } + + /// Whether this is a moment the ask may interrupt: past the launch quiet period, and with no + /// query running, so a sheet never lands on a result someone is waiting for. + static func isQuietMoment(launchedAt: Date, now: Date, isQueryRunning: Bool) -> Bool { + !isQueryRunning && now.timeIntervalSince(launchedAt) >= launchQuietPeriod + } +} diff --git a/apple/TablePro/Core/Services/Support/UsageDayCounter.swift b/apple/TablePro/Core/Services/Support/UsageDayCounter.swift new file mode 100644 index 0000000000..15a47a3289 --- /dev/null +++ b/apple/TablePro/Core/Services/Support/UsageDayCounter.swift @@ -0,0 +1,46 @@ +// +// UsageDayCounter.swift +// TablePro +// + +import Foundation + +/// Counts the distinct local days TablePro was in use, so the license ask can tell someone trying +/// the app from someone who has made it part of their week. +/// +/// The count stays in this Mac's defaults and is never sent anywhere. A day is stored as its +/// calendar date rather than a timestamp, so recording it twice is harmless however often the app +/// becomes active. +@MainActor +internal final class UsageDayCounter { + static let shared = UsageDayCounter(defaults: AppStorageEnvironment.shared.defaults) + + enum Keys { + static let days = "com.TablePro.support.usageDays" + static let lastDay = "com.TablePro.support.lastUsageDay" + } + + private let defaults: UserDefaults + private let calendar: Calendar + + init(defaults: UserDefaults, calendar: Calendar = .autoupdatingCurrent) { + self.defaults = defaults + self.calendar = calendar + } + + var days: Int { + defaults.integer(forKey: Keys.days) + } + + func recordUse(at date: Date) { + let day = dayStamp(of: date) + guard defaults.string(forKey: Keys.lastDay) != day else { return } + defaults.set(day, forKey: Keys.lastDay) + defaults.set(days + 1, forKey: Keys.days) + } + + private func dayStamp(of date: Date) -> String { + let parts = calendar.dateComponents([.year, .month, .day], from: date) + return String(format: "%04d-%02d-%02d", parts.year ?? 0, parts.month ?? 0, parts.day ?? 0) + } +} diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index f9c6cadf13..4a395d5fb4 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -74,792 +74,6 @@ } } } - }, - "\"%@\" is a name the database keeps for itself. Choose another name." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "\"%@\" is an attached database. Change its tables' structure from a connection that opens it." : { - - }, - "%1$@ column of %2$@" : { - - }, - "%1$d only left, %2$d only right, %3$d changed, %4$d same" : { - - }, - "%@" : { - - }, - "%@ connections do not support adding rows from Shortcuts." : { - - }, - "%@ has no column named %@." : { - "localizations" : { - "en" : { - "stringUnit" : { - "state" : "new", - "value" : "%1$@ has no column named %2$@." - } - } - } - }, - "%@ holds values that cannot be compared, so the row cannot be found again." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "%@ is read-only, so rows cannot be added." : { - - }, - "%@, without checking stored values" : { - - }, - "A check needs an expression." : { - - }, - "A dictionary for each row, keyed by column" : { - - }, - "A field needs a type." : { - - }, - "A foreign key named “%@” already exists." : { - - }, - "A foreign key staged in this diagram uses “%1$@.%2$@”. Save or undo that key before renaming the column." : { - - }, - "A label cannot be empty." : { - - }, - "A label cannot be removed or moved once it exists." : { - - }, - "A larger binary value stays on the server until you open, copy or export it. Applies to tables with a primary key." : { - - }, - "A name cannot be empty." : { - - }, - "A row still holds the values the change was made on. Refresh the table before saving again." : { - - }, - "A transaction is open on this connection. Commit or roll it back in TablePro, then run the shortcut again." : { - - }, - "AI Policy controls in-app AI agents. External Clients controls Raycast, Cursor, Claude Desktop, other MCP clients, AppleScript, and Shortcuts. Effective scope is the minimum of the requesting token's scope and the External Clients level." : { - - }, - "Add Check" : { - - }, - "Add Field" : { - - }, - "Add Foreign Key…" : { - - }, - "Add Row to Table" : { - - }, - "Add Row to Table expects one row. Use Add Rows to Table for multiple rows." : { - - }, - "Add Rows to Table" : { - - }, - "Add a row to ${table}" : { - - }, - "Add check %@" : { - - }, - "Add check “%1$@”: %2$@" : { - - }, - "Add field “%1$@” %2$@" : { - - }, - "Add label “%1$@” after “%2$@”" : { - - }, - "Add label “%1$@” before “%2$@”" : { - - }, - "Add label “%@” at the end" : { - - }, - "Add multiple rows to a table on a saved connection. Provide the rows as a JSON array, CSV text, or a file." : { - - }, - "Add one row to a table on a saved connection. Provide the row as a JSON object or a CSV row." : { - - }, - "Add plugin_debugger to shared_preload_libraries and restart the server." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "Add rows to ${table}" : { - - }, - "Added %lld rows to %@." : { - "localizations" : { - "en" : { - "stringUnit" : { - "state" : "new", - "value" : "Added %1$lld rows to %2$@." - } - } - } - }, - "Added one row to %@." : { - - }, - "After" : { - - }, - "Allow NULL" : { - - }, - "Also drop the columns, functions and other objects that use this type" : { - - }, - "Ask the SSH server's administrator to turn on a method TablePro supports." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "At the End" : { - - }, - "Attach" : { - - }, - "Attach Database" : { - - }, - "Attach Database…" : { - - }, - "Attached Databases" : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "Attached databases open only for a database file on this Mac, not for a database on a server." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "Attached each time the connection opens. Reach a table as name.table, and browse each database in the sidebar." : { - - }, - "Base Type" : { - - }, - "Before" : { - - }, - "Binary values limit must be between 1 and 1,048,576 KB." : { - - }, - "Both results stopped at the row cap and are compared on the rows they hold. A row past the cap can show as only on one side." : { - - }, - "Build Join Query" : { - - }, - "Build Join Query…" : { - - }, - "Build a join query from the selected tables" : { - - }, - "Cannot Compare" : { - - }, - "Change field “%1$@” to %2$@" : { - - }, - "Check" : { - - }, - "Check the values already stored" : { - - }, - "Check the values already stored against the selected constraint" : { - - }, - "Checks" : { - - }, - "Choose a Result" : { - - }, - "Choose a Type" : { - - }, - "Choose the base type." : { - - }, - "Choose the database files to attach each time this connection opens." : { - - }, - "Compare Result With…" : { - - }, - "Compare Results" : { - - }, - "Compare With" : { - - }, - "Comparing" : { - - }, - "Comparing…" : { - - }, - "Constraint Name" : { - - }, - "Controls how external clients (Raycast, Cursor, Claude Desktop, AppleScript, Shortcuts) access this connection. Tokens cannot exceed this level even with full-access scope." : { - - }, - "Could Not Load Value" : { - - }, - "Could Not Refresh the Diagram" : { - - }, - "Could Not Stage the Change" : { - - }, - "Could not attach \"%@\": %@" : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "Could not connect to the database: %@" : { - - }, - "Could not read the columns of %@." : { - - }, - "Couldn't Rename Type" : { - - }, - "Create Type" : { - - }, - "Database or Schema" : { - - }, - "Delete Foreign Key %@" : { - - }, - "Drop Constraint" : { - - }, - "Drop Field" : { - - }, - "Drop constraint “%@”" : { - - }, - "Drop field “%@”" : { - - }, - "Drop type “%@”?" : { - - }, - "Dropped" : { - - }, - "Edit Field" : { - - }, - "Every field needs a name." : { - - }, - "First Value" : { - - }, - "Inner Join" : { - - }, - "It supports these host key types: %@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "Joins" : { - - }, - "Jump host %1$@ failed: %2$@" : { - - }, - "KB" : { - - }, - "Key %@ matches more than one row in the left result. Choose key columns that identify a single row, or match whole rows." : { - - }, - "Key %@ matches more than one row in the right result. Choose key columns that identify a single row, or match whole rows." : { - - }, - "Left" : { - - }, - "Left Join" : { - - }, - "Line %lld of the file is not valid %@ text." : { - "localizations" : { - "en" : { - "stringUnit" : { - "state" : "new", - "value" : "Line %1$lld of the file is not valid %2$@ text." - } - } - } - }, - "Load binary values up to" : { - - }, - "Match Whole Rows" : { - - }, - "More than one attached database is named \"%@\"." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "Name the type." : { - - }, - "Named by the server" : { - - }, - "New Name" : { - - }, - "New Type" : { - - }, - "No Attached Databases" : { - - }, - "No Columns in Common" : { - - }, - "No Result to Compare With" : { - - }, - "No data was provided to add." : { - - }, - "No foreign key links %@ to the tables before it, so it is cross joined." : { - - }, - "No Foreign Keys" : { - - }, - "No row holds the values it was loaded with. Another client may have changed or deleted it." : { - - }, - "No tables picked" : { - - }, - "None (Cross Join)" : { - - }, - "Not Null" : { - - }, - "Not loaded, %@" : { - - }, - "Not validated" : { - - }, - "Only Left" : { - - }, - "Only PL/pgSQL functions and procedures can be debugged." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "Only Right" : { - - }, - "Only in the left result, not compared: %@" : { - - }, - "Only in the right result, not compared: %@" : { - - }, - "Open in Query Tab" : { - - }, - "Opens a database connection in TablePro" : { - - }, - "Other rows hold exactly the same values, so the change cannot reach this row alone." : { - - }, - "Pick the result to compare this one with." : { - - }, - "Pick the tables to join. Their foreign keys become the join conditions." : { - - }, - "Position" : { - - }, - "Provide a JSON object or an array of objects." : { - - }, - "References" : { - - }, - "Refuse NULL" : { - - }, - "Remove Attached Database" : { - - }, - "Remove the comment" : { - - }, - "Remove the default" : { - - }, - "Remove this change from the queue" : { - - }, - "Remove “%@”" : { - - }, - "Rename Label" : { - - }, - "Rename Type" : { - - }, - "Rename field “%1$@” to “%2$@”" : { - - }, - "Rename label “%1$@” to “%2$@”" : { - - }, - "Rename the selected label" : { - - }, - "Rename to “%@”" : { - - }, - "Result %1$d: %2$@" : { - - }, - "Result Closed" : { - - }, - "Result Format" : { - - }, - "Right" : { - - }, - "Row (JSON or CSV)" : { - - }, - "Row Limit" : { - - }, - "Rows (JSON or CSV)" : { - - }, - "Rows with the same values in these columns are compared. With none chosen, whole rows are matched." : { - - }, - "Run ${query} on ${connection}" : { - - }, - "Run Query" : { - - }, - "Run a SQL statement on a saved connection and pass the result to the next action." : { - - }, - "Run another query in this tab, or pin a result in another tab." : { - - }, - "Save Type Changes" : { - - }, - "Second value" : { - - }, - "Section" : { - - }, - "Set the comment" : { - - }, - "Set the default to %@" : { - - }, - "Shortcuts" : { - - }, - "Shortcuts wants to add %1$lld rows to \"%2$@\"" : { - - }, - "Shortcuts wants to add a row to \"%@\"" : { - - }, - "Show Structure of %@" : { - - }, - "Some rows are no longer in the table, so their values could not be read." : { - - }, - "Statement" : { - - }, - "Subtype" : { - - }, - "Table Data" : { - - }, - "TablePro and the SSH server could not agree on how to encrypt the connection." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro and the SSH server have no cipher in common." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro and the SSH server have no key exchange or host key method in common." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro and the SSH server have no MAC algorithm in common." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro and the SSH server have no signature algorithm in common for this key." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro can't pass any value of type %2$@ to %1$@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro can't read %@. Check that this account can see its source." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro can't read this unit's source." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro can't turn text into a %2$@ value for %1$@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro signs RSA keys with rsa-sha2-256 or rsa-sha2-512, not with ssh-rsa, which uses SHA-1." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro supports these ciphers: %@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro supports these key exchange methods: %@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "TablePro supports these MAC algorithms: %@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "That connection no longer exists in TablePro." : { - - }, - "That result is no longer open. Choose another one." : { - - }, - "The CSV data has no header row." : { - - }, - "The JSON array must contain only objects." : { - - }, - "The attached database \"%@\" has no file." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The attached file \"%@\" has no name." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The changes were saved, but the type could not be read back. Reload before editing it again." : { - - }, - "The changes you have not saved will be lost." : { - - }, - "The client certificate and key could not be written to a temporary file." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The column “%1$@” is no longer in “%2$@”." : { - - }, - "The data could not be read: %@" : { - - }, - "The data has no values to insert into %@." : { - - }, - "The diagram is saving its changes. Make this change once the save has finished." : { - - }, - "The first column of the first row" : { - - }, - "The foreign key “%1$@” is no longer on “%2$@”." : { - - }, - "The left result stopped at the row cap and is compared on the %@ rows it holds. A row past the cap can show as only on the right." : { - - }, - "The name a statement uses for this database, as in name.table." : { - - }, - "The request timed out." : { - - }, - "The right result stopped at the row cap and is compared on the %@ rows it holds. A row past the cap can show as only on the left." : { - - }, - "The row could not be looked up: %@" : { - - }, - "The row has no loaded values to be found by." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The row's loaded values are missing, so it cannot be found again." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The saved list could not be read. Adding a database replaces it." : { - - }, - "The saved list of attached databases is not in a format TablePro can read." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The schema changes staged in this diagram will be lost." : { - - }, - "The server refused a change, so nothing was saved." : { - - }, - "The server refused a change. The changes before it were saved." : { - - }, - "The server offers only ciphers TablePro no longer supports: %@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The server offers only host key types TablePro no longer supports: %@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The server offers only key exchange methods TablePro no longer supports: %@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The server offers only MAC algorithms TablePro no longer supports: %@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The server runs %@." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The SSH server closed the connection during the handshake." : { - - }, - "The SSH server ended the connection during the handshake: %@" : { - - }, - "The structure of a table in an attached database cannot be changed here." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "The text encoding is invalid." : { - - }, - "The two results share no column name, so there is nothing to compare." : { - - }, - "The type changed on the server since it was read. Reload it, then save again." : { - - }, - "This Tab" : { - - }, - "This cannot be undone. The drop fails while a column or other object still uses it." : { - - }, - "This cannot be undone. Without Cascade, the drop fails while a column, function or other type still uses it." : { - - }, - "This change does not apply to this kind of type." : { - - }, - "This _id reads as more than one value type in this collection, such as text and an ObjectId, so TablePro cannot tell which document the row names. Use a query." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, - "This connection's startup commands leave a transaction open, so rows added from Shortcuts could not be committed." : { - - }, - "This database cannot make the change “%@”." : { - - }, - "This database reports no tables to join." : { - - }, - "This server cannot rename a label." : { - - }, - "Too many rows. Add up to %lld rows at a time." : { - - }, - "Turn off to add the check now and validate the stored values later." : { - - }, - "Validate" : { - - }, - "Validate constraint “%@”" : { - - }, - "Validated on save" : { - - }, - "Was %@" : { - - }, - "Was “%@”" : { - - }, - "Whole row" : { - - }, - "^[%lld change](inflect: true)" : { - }, "__name__ is the document's ID in this collection, such as alice." : { "extractionState" : "manual", @@ -1016,45 +230,6 @@ } } } - }, - "connection" : { - - }, - "database" : { - - }, - "import" : { - - }, - "insert" : { - - }, - "left value, differs" : { - - }, - "only in the left result" : { - - }, - "only in the right result" : { - - }, - "open" : { - - }, - "query" : { - - }, - "right value, differs" : { - - }, - "row" : { - - }, - "rows" : { - - }, - "select" : { - }, "—" : { "extractionState" : "stale", @@ -1226,33 +401,6 @@ }, ":%@" : { "shouldTranslate" : false - }, - "“%@” already has a column with that name." : { - - }, - "“%@” is already a constraint." : { - - }, - "“%@” is already a field." : { - - }, - "“%@” is already a label." : { - - }, - "“%@” is already validated." : { - - }, - "“%@” is listed twice." : { - - }, - "“%@” is not a constraint any more." : { - - }, - "“%@” is not a field any more." : { - - }, - "“%@” is not a label any more." : { - }, "…and %d more lines" : { "localizations" : { @@ -1581,6 +729,9 @@ } } } + }, + "“%@” already has a column with that name." : { + }, "“%@” and its token are removed from this Mac. Sessions lose its tools." : { "localizations" : { @@ -1597,10 +748,29 @@ }, "“%@” holds no file to import." : { + }, + "\"%@\" is a name the database keeps for itself. Choose another name." : { + "extractionState" : "manual", + "generatesSymbol" : false }, "\"%@\" is a reserved theme identifier." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "“%@” is already a constraint." : { + + }, + "“%@” is already a field." : { + + }, + "“%@” is already a label." : { + + }, + "“%@” is already validated." : { + + }, + "\"%@\" is an attached database. Change its tables' structure from a connection that opens it." : { + }, "“%@” is compressed." : { "localizations" : { @@ -1635,6 +805,9 @@ } } } + }, + "“%@” is listed twice." : { + }, "“%@” is no longer at that location." : { "localizations" : { @@ -1661,6 +834,9 @@ "\"%@\" is not a column. Name it as family:qualifier." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "“%@” is not a constraint any more." : { + }, "\"%@\" is not a DynamoDB action this editor runs" : { "extractionState" : "manual", @@ -1685,6 +861,9 @@ } } } + }, + "“%@” is not a field any more." : { + }, "\"%@\" is not a file." : { "extractionState" : "manual", @@ -1716,6 +895,9 @@ }, "“%@” is not a host or host:port" : { + }, + "“%@” is not a label any more." : { + }, "\"%@\" is not a library." : { "localizations" : { @@ -2511,12 +1693,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "(%lld instructions)" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -3037,6 +2213,9 @@ } } } + }, + "%@" : { + }, "%@\nPreview tab. Double-click to keep it open." : { "localizations" : { @@ -3941,6 +3120,9 @@ } } } + }, + "%@ connections do not support adding rows from Shortcuts." : { + }, "%@ connections open a server, not a database file, so there is nothing to fetch over SSH." : { "localizations" : { @@ -4322,6 +3504,16 @@ } } }, + "%@ has no column named %@." : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "new", + "value" : "%1$@ has no column named %2$@." + } + } + } + }, "%@ has no column order of its own to change." : { "localizations" : { "fr" : { @@ -4391,6 +3583,10 @@ } } }, + "%@ holds values that cannot be compared, so the row cannot be found again." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "%@ in the workbook expands to far more data than it holds, so it was not read." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -4828,6 +4024,9 @@ } } } + }, + "%@ is read-only, so rows cannot be added." : { + }, "%@ is required" : { "localizations" : { @@ -6619,6 +5818,9 @@ } } } + }, + "%@, without checking stored values" : { + }, "%@: %@" : { "extractionState" : "manual", @@ -7555,12 +6757,6 @@ "formatSpecifier" : "d", "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%arg points" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -7625,6 +6821,9 @@ } } } + }, + "%1$@ column of %2$@" : { + }, "%1$@ could not be read again before generating the script. %2$@" : { "localizations" : { @@ -8606,6 +7805,9 @@ } } } + }, + "%1$d only left, %2$d only right, %3$d changed, %4$d same" : { + }, "%1$d rows match. The first %2$d are listed." : { "localizations" : { @@ -9225,12 +8427,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%d octets" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -9347,12 +8543,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%d modifications" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -10628,12 +9818,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%d lignes contiennent NULL dans une colonne de clé et ont été exclues. Choisissez une clé sans NULL pour les comparer." - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -10715,12 +9899,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%d lignes correspondent. Les lignes identiques sont comptées sans être listées, pour qu’aucune différence ne soit noyée dans cette liste." - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -11758,12 +10936,6 @@ "formatSpecifier" : "lld", "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%arg lignes" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -11826,12 +10998,6 @@ "formatSpecifier" : "lld", "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%arg lignes" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -12726,12 +11892,6 @@ "formatSpecifier" : "lld", "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%arg lignes" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -12794,12 +11954,6 @@ "formatSpecifier" : "lld", "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%arg lignes" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -13010,18 +12164,15 @@ } } } + }, + "^[%lld change](inflect: true)" : { + }, "^[%lld column](inflect: true)" : { "localizations" : { "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%lld colonnes" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -13074,12 +12225,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%lld connexions utilisent ce profil. Elles en conservent les identifiants." - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -13115,12 +12260,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%lld lignes" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -13143,12 +12282,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%lld lignes" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -13201,12 +12334,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%lld lignes à exporter" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -13229,12 +12356,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%lld tables à exporter" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -15778,6 +14899,9 @@ } } } + }, + "A check needs an expression." : { + }, "A client certificate needs its client key. On the connection's Network tab, choose the client key, or clear the client certificate." : { "extractionState" : "manual", @@ -16251,6 +15375,9 @@ } } } + }, + "A dictionary for each row, keyed by column" : { + }, "A different file already exists at %@." : { "extractionState" : "stale", @@ -16384,6 +15511,9 @@ } } } + }, + "A field needs a type." : { + }, "A file" : { "localizations" : { @@ -16451,6 +15581,9 @@ } } } + }, + "A foreign key named “%@” already exists." : { + }, "A foreign key needs as many referenced columns as it has columns." : { "localizations" : { @@ -16461,6 +15594,9 @@ } } } + }, + "A foreign key staged in this diagram uses “%1$@.%2$@”. Save or undo that key before renaming the column." : { + }, "A foreign table" : { "localizations" : { @@ -16648,6 +15784,15 @@ } } } + }, + "A label cannot be empty." : { + + }, + "A label cannot be removed or moved once it exists." : { + + }, + "A larger binary value stays on the server until you open, copy or export it. Applies to tables with a primary key." : { + }, "A local secondary index is part of its table and is removed only with the table." : { "extractionState" : "manual", @@ -16740,6 +15885,9 @@ } } } + }, + "A name cannot be empty." : { + }, "A new child needs at least one value. The Realtime Database does not store empty nodes." : { "extractionState" : "manual", @@ -17170,6 +16318,9 @@ "A row needs a key. Bigtable has no empty row key." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "A row still holds the values the change was made on. Refresh the table before saving again." : { + }, "A row the filter hides holds %@ in column “%@”, which %@ cannot store. Clear the filter to find it, or choose Save As and pick UTF-8." : { "localizations" : { @@ -17942,6 +17093,9 @@ }, "A transaction is open on this connection, and its uncommitted rows aren't in these counts." : { + }, + "A transaction is open on this connection. Commit or roll it back in TablePro, then run the shortcut again." : { + }, "A tunnel forwards one address, and the node Sentinel names is on the server's own network, so Sentinel mode cannot run through one. Connect to a data node directly, or turn the tunnel off." : { "extractionState" : "manual", @@ -19865,6 +19019,9 @@ } } } + }, + "Add a row to ${table}" : { + }, "Add a saved query to watch its result here." : { @@ -19960,6 +19117,15 @@ } } } + }, + "Add Check" : { + + }, + "Add check “%1$@”: %2$@" : { + + }, + "Add check %@" : { + }, "Add Check Constraint" : { "localizations" : { @@ -20329,6 +19495,12 @@ } } } + }, + "Add Field" : { + + }, + "Add field “%1$@” %2$@" : { + }, "Add filter" : { "localizations" : { @@ -20532,6 +19704,9 @@ }, "Add foreign key “%@”" : { + }, + "Add Foreign Key…" : { + }, "Add Global Index" : { "extractionState" : "manual", @@ -20711,7 +19886,6 @@ } }, "Add Label" : { - "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -20720,6 +19894,15 @@ } } } + }, + "Add label “%@” at the end" : { + + }, + "Add label “%1$@” after “%2$@”" : { + + }, + "Add label “%1$@” before “%2$@”" : { + }, "Add Label After…" : { "extractionState" : "stale", @@ -20797,6 +19980,16 @@ } } }, + "Add multiple rows to a table on a saved connection. Provide the rows as a JSON array, CSV text, or a file." : { + + }, + "Add one row to a table on a saved connection. Provide the row as a JSON object or a CSV row." : { + + }, + "Add plugin_debugger to shared_preload_libraries and restart the server." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Add Provider" : { "extractionState" : "stale", "localizations" : { @@ -20927,6 +20120,15 @@ } } } + }, + "Add Row to Table" : { + + }, + "Add Row to Table expects one row. Use Add Rows to Table for multiple rows." : { + + }, + "Add rows to ${table}" : { + }, "Add rows to it" : { "localizations" : { @@ -20937,6 +20139,9 @@ } } } + }, + "Add Rows to Table" : { + }, "Add Rule" : { "localizations" : { @@ -21214,6 +20419,19 @@ } } } + }, + "Added %lld rows to %@." : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "new", + "value" : "Added %1$lld rows to %2$@." + } + } + } + }, + "Added one row to %@." : { + }, "Adding check constraint %@ scans the table and fails if any existing row violates it." : { "localizations" : { @@ -21752,9 +20970,15 @@ } } } + }, + "After" : { + }, "After keyword expansion, Tab moves through fields such as %1$@, and %2$@ marks where the cursor ends up." : { + }, + "After the values already in %@" : { + }, "Agent" : { "localizations" : { @@ -22300,6 +21524,7 @@ } }, "AI Policy controls in-app AI agents. External Clients controls Raycast, Cursor, Claude Desktop, other MCP clients, and AppleScript. Effective scope is the minimum of the requesting token's scope and the External Clients level." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -22338,6 +21563,9 @@ } } } + }, + "AI Policy controls in-app AI agents. External Clients controls Raycast, Cursor, Claude Desktop, other MCP clients, AppleScript, and Shortcuts. Effective scope is the minimum of the requesting token's scope and the External Clients level." : { + }, "AI Provider" : { "extractionState" : "stale", @@ -22539,9 +21767,6 @@ } } } - }, - "After the values already in %@" : { - }, "Airplane" : { "extractionState" : "manual", @@ -22849,12 +22074,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "Toutes les %lld lignes sélectionnées" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -23861,6 +23080,9 @@ } } } + }, + "Allow NULL" : { + }, "Allowed Connections" : { "localizations" : { @@ -24075,6 +23297,9 @@ }, "already saved" : { + }, + "Also drop the columns, functions and other objects that use this type" : { + }, "Also handles" : { "localizations" : { @@ -28613,6 +27838,10 @@ } } }, + "Ask the SSH server's administrator to turn on a method TablePro supports." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Ask your administrator to register postgres/host.domain.com in the realm, and connect by hostname rather than IP address." : { "extractionState" : "manual", "generatesSymbol" : false @@ -28820,6 +28049,9 @@ } } } + }, + "At the End" : { + }, "At the end of the line" : { @@ -28860,6 +28092,9 @@ "Atom" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Attach" : { + }, "Attach a connection to this conversation first." : { "localizations" : { @@ -28910,6 +28145,12 @@ } } } + }, + "Attach Database" : { + + }, + "Attach Database…" : { + }, "Attached %1$d of %2$d files. %3$@" : { "localizations" : { @@ -28920,6 +28161,17 @@ } } } + }, + "Attached Databases" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Attached databases open only for a database file on this Mac, not for a database on a server." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Attached each time the connection opens. Reach a table as name.table, and browse each database in the sidebar." : { + }, "Attachments" : { "extractionState" : "stale", @@ -31477,6 +30729,9 @@ "Bar Chart" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Base Type" : { + }, "Base URL" : { "localizations" : { @@ -31684,6 +30939,9 @@ } } } + }, + "Before" : { + }, "BEFORE, AFTER, or INSTEAD OF" : { "localizations" : { @@ -31988,6 +31246,9 @@ }, "Binary File" : { + }, + "Binary values limit must be between 1 and 1,048,576 KB." : { + }, "Block" : { "extractionState" : "stale", @@ -32372,6 +31633,9 @@ } } } + }, + "Both results stopped at the row cap and are compared on the rows they hold. A row past the cap can show as only on one side." : { + }, "Both sides are one connection, so the server copies the rows itself. There is no row-by-row progress, and Stop cannot interrupt it." : { "localizations" : { @@ -32768,6 +32032,15 @@ "Bug" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Build a join query from the selected tables" : { + + }, + "Build Join Query" : { + + }, + "Build Join Query…" : { + }, "Build the SQL that brings the target in line" : { "localizations" : { @@ -34387,6 +33660,9 @@ }, "Cannot be NULL." : { + }, + "Cannot Compare" : { + }, "Cannot connect to Ollama at %@. Is Ollama running?" : { "localizations" : { @@ -35817,6 +35093,9 @@ } } } + }, + "Change field “%1$@” to %2$@" : { + }, "Change File" : { "extractionState" : "stale", @@ -36108,6 +35387,7 @@ } }, "Change Primary Key" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -36824,6 +36104,7 @@ } }, "Charset:" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -37184,6 +36465,9 @@ } } } + }, + "Check" : { + }, "Check a query for correctness, cost, and risk against the live schema before running it" : { "localizations" : { @@ -37591,6 +36875,12 @@ } } } + }, + "Check the values already stored" : { + + }, + "Check the values already stored against the selected constraint" : { + }, "Checkered Flag" : { "extractionState" : "manual", @@ -37741,6 +37031,9 @@ "Checkmark" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Checks" : { + }, "Chinook (Sample)" : { "localizations" : { @@ -38144,6 +37437,7 @@ }, "Choose a location to save the diagram as PNG." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -38292,6 +37586,9 @@ } } } + }, + "Choose a Result" : { + }, "Choose a SAP HANA schema first." : { "extractionState" : "manual", @@ -38631,6 +37928,9 @@ } } } + }, + "Choose a Type" : { + }, "Choose AI provider and model" : { "localizations" : { @@ -39047,6 +38347,9 @@ } } } + }, + "Choose the base type." : { + }, "Choose the client certificate and key on the connection's Network tab." : { "extractionState" : "manual", @@ -39099,6 +38402,9 @@ } } } + }, + "Choose the database files to attach each time this connection opens." : { + }, "Choose the extension's file again in Edit Connection." : { "extractionState" : "manual", @@ -39135,6 +38441,7 @@ } }, "Choose Type" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -39175,6 +38482,7 @@ } }, "Choose Value" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -43480,6 +42788,7 @@ } }, "Collation:" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -45346,9 +44655,6 @@ } } } - }, - "Comment:" : { - }, "Comments" : { "localizations" : { @@ -45827,6 +45133,12 @@ } } } + }, + "Compare Result With…" : { + + }, + "Compare Results" : { + }, "Compare structure or data" : { "localizations" : { @@ -45997,6 +45309,9 @@ } } } + }, + "Compare With" : { + }, "Compared %@ keys in key order. Rows past the limit were not read on either side, and rows with NULL in a key column are not read under a limit." : { "localizations" : { @@ -46169,6 +45484,9 @@ } } } + }, + "Comparing" : { + }, "Comparing only. Changes applied earlier stay in the target." : { "localizations" : { @@ -46219,6 +45537,9 @@ } } } + }, + "Comparing…" : { + }, "Comparison cancelled." : { "localizations" : { @@ -46705,6 +46026,7 @@ }, "Configure an active provider to enable inline suggestions." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -47159,12 +46481,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "Connecter %lld connexions" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -48062,6 +47378,9 @@ } } } + }, + "connection" : { + }, "Connection" : { "localizations" : { @@ -49764,6 +49083,9 @@ } } } + }, + "Constraint Name" : { + }, "Constraints" : { "localizations" : { @@ -50110,8 +49432,12 @@ } } } + }, + "Controls how external clients (Raycast, Cursor, Claude Desktop, AppleScript, Shortcuts) access this connection. Tokens cannot exceed this level even with full-access scope." : { + }, "Controls how external clients (Raycast, Cursor, Claude Desktop, AppleScript) access this connection. Tokens cannot exceed this level even with full-access scope." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -50944,9 +50270,6 @@ } } } - }, - "Copy CREATE INDEX" : { - }, "Copy Cell Value" : { @@ -51113,6 +50436,9 @@ }, "Copy Count" : { + }, + "Copy CREATE INDEX" : { + }, "Copy DDL" : { "localizations" : { @@ -52603,6 +51929,10 @@ } } }, + "Could not attach \"%@\": %@" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Could not attach %1$d files. %2$@" : { "localizations" : { "fr" : { @@ -52744,6 +52074,7 @@ }, "Could not build the column reorder for this table. If this engine's driver was installed before column reorder shipped, update it in Settings > Plugins." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -52800,6 +52131,7 @@ }, "Could not build the table rebuild for this change. If this engine's driver was installed before foreign key editing shipped, update it in Settings > Plugins." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -53016,6 +52348,9 @@ } } } + }, + "Could not connect to the database: %@" : { + }, "Could not create %@." : { "localizations" : { @@ -53666,6 +53001,9 @@ } } } + }, + "Could Not Load Value" : { + }, "Could not make another value for (%@) that does not repeat. Widen its range or generate fewer rows." : { @@ -54176,6 +53514,9 @@ } } } + }, + "Could not read the columns of %@." : { + }, "Could not read the file. It may have been deleted or moved." : { "localizations" : { @@ -54338,6 +53679,9 @@ }, "Could not read which rows are already in the table" : { + }, + "Could Not Refresh the Diagram" : { + }, "Could not release the file" : { "localizations" : { @@ -54541,6 +53885,9 @@ } } } + }, + "Could Not Stage the Change" : { + }, "Could not start a new TablePro instance. Quit and reopen manually." : { "localizations" : { @@ -55059,6 +54406,9 @@ }, "Couldn't Rename the Theme" : { + }, + "Couldn't Rename Type" : { + }, "Couldn't Restore Version" : { "localizations" : { @@ -56256,6 +55606,9 @@ } } } + }, + "Create Type" : { + }, "Create your own slash commands. Use {{query}}, {{schema}}, {{database}}, or {{body}} in the template to insert chat context at runtime." : { "localizations" : { @@ -56430,6 +55783,7 @@ } }, "Creating…" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -58494,6 +57848,9 @@ } } } + }, + "database" : { + }, "Database" : { "extractionState" : "manual", @@ -59494,6 +58851,9 @@ } } } + }, + "Database or Schema" : { + }, "Database Schema" : { "extractionState" : "manual", @@ -62131,12 +61491,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "Supprimer %lld connexions" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -62595,6 +61949,9 @@ }, "Delete foreign key “%@”" : { + }, + "Delete Foreign Key %@" : { + }, "Delete Group" : { "localizations" : { @@ -63038,6 +62395,7 @@ } }, "Delete Selected" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -67611,6 +66969,12 @@ } } } + }, + "Drop Constraint" : { + + }, + "Drop constraint “%@”" : { + }, "Drop Database" : { "extractionState" : "stale", @@ -67772,6 +67136,12 @@ } } } + }, + "Drop Field" : { + + }, + "Drop field “%@”" : { + }, "Drop Foreign Table" : { "localizations" : { @@ -68291,6 +67661,9 @@ }, "Drop Type" : { + }, + "Drop type “%@”?" : { + }, "Drop Type…" : { @@ -68374,6 +67747,9 @@ } } } + }, + "Dropped" : { + }, "dropping %@ '%@'" : { "localizations" : { @@ -69934,6 +69310,9 @@ } } } + }, + "Edit Field" : { + }, "Edit Foreign Key" : { "localizations" : { @@ -70192,16 +69571,16 @@ "Edit Search" : { }, - "Edit Settings" : { + "Edit Sequence" : { }, - "Edit Table Options" : { + "Edit Sequence…" : { }, - "Edit Sequence" : { + "Edit Settings" : { }, - "Edit Sequence…" : { + "Edit Table Options" : { }, "Edit the connection and choose its database type." : { @@ -70918,6 +70297,7 @@ } }, "Embedded analytical SQL" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -72130,6 +71510,7 @@ } }, "Engine:" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -72530,6 +71911,7 @@ } }, "Enter table name" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -73817,6 +73199,13 @@ } } }, + "Estimated impact %.1f%%" : { + + }, + "Estimated Plan" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Estimated row count" : { "localizations" : { "fr" : { @@ -74057,13 +73446,6 @@ } } }, - "Estimated impact %.1f%%" : { - - }, - "Estimated Plan" : { - "extractionState" : "manual", - "generatesSymbol" : false - }, "etcd cannot filter with %@." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -74418,6 +73800,9 @@ } } } + }, + "Every field needs a name." : { + }, "Every file in this folder matches the last commit." : { @@ -75891,12 +75276,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%lld instructions exécutées" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -77257,6 +76636,7 @@ } }, "Export as PNG" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -80499,6 +79879,7 @@ } }, "Failed to render the diagram image." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -82965,12 +82346,6 @@ "formatSpecifier" : "lld", "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%arg lignes" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -83253,6 +82628,7 @@ } }, "Find in file" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -83263,6 +82639,7 @@ } }, "Find in loaded rows" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -83853,6 +83230,9 @@ } } } + }, + "First Value" : { + }, "Fish" : { "extractionState" : "manual", @@ -85241,6 +84621,7 @@ }, "Formatter error: %@" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -85412,10 +84793,10 @@ } } }, - "From" : { + "Freeze Up to This Column" : { }, - "Freeze Up to This Column" : { + "From" : { }, "From %@…" : { @@ -90462,6 +89843,46 @@ } } }, + "I Use It Personally" : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Je l’utilise à titre personnel" + } + }, + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "개인적으로 사용합니다" + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Kişisel Olarak Kullanıyorum" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Tôi dùng cá nhân" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "我个人使用" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "我個人使用" + } + } + } + }, "iCloud asked TablePro to wait. It tries again on its own shortly." : { }, @@ -90508,9 +89929,6 @@ }, "iCloud could not complete the sync. TablePro tries again on the next sync." : { - }, - "iCloud Full" : { - }, "iCloud Is Not Ready" : { @@ -90565,6 +89983,7 @@ } }, "iCloud Sync is active" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -90878,6 +90297,46 @@ } } }, + "If TablePro is part of your work now, please buy a license. Licenses are what pay for the next release." : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Si TablePro fait désormais partie de votre travail, merci d’acheter une licence. Ce sont les licences qui financent la prochaine version." + } + }, + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro가 이제 업무의 일부라면 라이선스를 구매해 주세요. 다음 릴리스는 라이선스로 만들어집니다." + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro artık işinizin bir parçasıysa lütfen bir lisans satın alın. Bir sonraki sürümü lisanslar finanse ediyor." + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Nếu TablePro đã là một phần công việc của bạn, hãy mua một giấy phép. Giấy phép là thứ chi trả cho bản phát hành tiếp theo." + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "如果 TablePro 已经是你工作的一部分,请购买许可证。许可证支付了下一个版本的开发。" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "如果 TablePro 已經是你工作的一部分,請購買授權。授權支付了下一個版本的開發。" + } + } + } + }, "If the connection is already there" : { "localizations" : { "fr" : { @@ -91207,6 +90666,9 @@ } } } + }, + "import" : { + }, "Import" : { "extractionState" : "manual", @@ -94430,6 +93892,12 @@ }, "Inline suggestions and Review, Explain, Optimize and Fix run on this Mac and send nothing over the network." : { + }, + "Inner Join" : { + + }, + "insert" : { + }, "Insert" : { "extractionState" : "manual", @@ -97327,11 +96795,15 @@ "It stays on this Mac and never syncs, because it can name a command to run." : { }, - "It stopped after adding ^[%lld row](inflect: true)." : { + "It stays on this Mac and never syncs, because it names where your Vault token is sent." : { }, - "It stays on this Mac and never syncs, because it names where your Vault token is sent." : { + "It stopped after adding ^[%lld row](inflect: true)." : { + }, + "It supports these host key types: %@." : { + "extractionState" : "manual", + "generatesSymbol" : false }, "It was changed outside TablePro and no longer loads. Settings > Appearance lists it under Not Loaded with the reason." : { @@ -97521,6 +96993,9 @@ }, "Join condition:" : { + }, + "Joins" : { + }, "Joins this column with the one to its right." : { "localizations" : { @@ -97797,6 +97272,9 @@ } } } + }, + "Jump host %1$@ failed: %2$@" : { + }, "Jump host configuration is invalid" : { "localizations" : { @@ -98051,6 +97529,9 @@ } } } + }, + "KB" : { + }, "Keep a Table Open" : { "localizations" : { @@ -98673,6 +98154,12 @@ } } } + }, + "Key %@ matches more than one row in the left result. Choose key columns that identify a single row, or match whole rows." : { + + }, + "Key %@ matches more than one row in the right result. Choose key columns that identify a single row, or match whole rows." : { + }, "Key %@ was not found in the secret JSON." : { "localizations" : { @@ -99404,13 +98891,6 @@ } } }, - "This sequence's settings could not be read, so they cannot be changed here." : { - - }, - "This server can't show the default value of %@, so enter a value for it." : { - "extractionState" : "manual", - "generatesSymbol" : false - }, "keyword: %@" : { "localizations" : { "fr" : { @@ -99726,7 +99206,6 @@ } }, "Labels" : { - "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -100441,6 +99920,7 @@ } }, "Last synced %@" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -100954,13 +100434,7 @@ "extractionState" : "manual", "generatesSymbol" : false }, - "Set Next Value" : { - - }, - "Start" : { - - }, - "The increment cannot be zero." : { + "Left" : { }, "left curly bracket" : { @@ -101003,6 +100477,9 @@ } } } + }, + "Left Join" : { + }, "Left out" : { "localizations" : { @@ -101101,6 +100578,9 @@ } } } + }, + "left value, differs" : { + }, "Legacy UUID Encoding" : { "extractionState" : "manual", @@ -101441,6 +100921,7 @@ } }, "License expired, sync paused" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -101728,6 +101209,7 @@ } }, "License not verified in 30 days, sync paused. Click to check again." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -102491,6 +101973,16 @@ }, "Line %d" : { + }, + "Line %lld of the file is not valid %@ text." : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "new", + "value" : "Line %1$lld of the file is not valid %2$@ text." + } + } + } }, "Line break" : { "extractionState" : "manual", @@ -104669,6 +104161,9 @@ } } } + }, + "Load binary values up to" : { + }, "Load Extensions" : { "localizations" : { @@ -107304,6 +106799,9 @@ } } } + }, + "Match Whole Rows" : { + }, "Matched name" : { "localizations" : { @@ -110017,12 +109515,6 @@ } } } - }, - "Missing Index" : { - - }, - "Missing Indexes" : { - }, "Missing argument: %@" : { "extractionState" : "manual", @@ -110065,6 +109557,12 @@ } } } + }, + "Missing Index" : { + + }, + "Missing Indexes" : { + }, "Missing Profile" : { "localizations" : { @@ -110792,6 +110290,10 @@ } } }, + "More than one attached database is named \"%@\"." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "More than one check constraint is named %@. Change or delete all of them in the same save." : { "localizations" : { "fr" : { @@ -112741,6 +112243,9 @@ "Name the table before the key, as in GET events/user#42." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Name the type." : { + }, "Name this selection" : { "localizations" : { @@ -112791,6 +112296,9 @@ } } } + }, + "Named by the server" : { + }, "Namespace" : { "extractionState" : "manual", @@ -113318,7 +112826,6 @@ } }, "New" : { - "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -114001,9 +113508,6 @@ } } } - }, - "New Sequence…" : { - }, "New image" : { "extractionState" : "manual", @@ -114059,6 +113563,9 @@ } } } + }, + "New Name" : { + }, "New password:" : { "localizations" : { @@ -114192,6 +113699,9 @@ }, "New Seed" : { + }, + "New Sequence…" : { + }, "New Session" : { "localizations" : { @@ -114405,7 +113915,6 @@ } }, "New Table" : { - "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -114610,9 +114119,11 @@ } } } + }, + "New Type" : { + }, "New Type…" : { - "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -115297,6 +114808,9 @@ } } } + }, + "No Attached Databases" : { + }, "No available local port for SSH tunnel" : { "localizations" : { @@ -115676,6 +115190,9 @@ } } } + }, + "No Columns in Common" : { + }, "No columns in common." : { "localizations" : { @@ -116259,6 +115776,9 @@ } } } + }, + "No data was provided to add." : { + }, "No database connection" : { "localizations" : { @@ -116581,6 +116101,7 @@ } }, "No DDL available" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -117039,6 +116560,12 @@ } } } + }, + "No foreign key links %@ to the tables before it, so it is cross joined." : { + + }, + "No Foreign Keys" : { + }, "No Foreign Keys Yet" : { "localizations" : { @@ -117201,6 +116728,7 @@ } }, "No iCloud" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -119233,6 +118761,9 @@ } } } + }, + "No Result to Compare With" : { + }, "No Results" : { "localizations" : { @@ -119343,6 +118874,9 @@ } } } + }, + "No row holds the values it was loaded with. Another client may have changed or deleted it." : { + }, "No Row Selected" : { "localizations" : { @@ -119935,6 +119469,9 @@ } } } + }, + "No sequences" : { + }, "No servers yet." : { "localizations" : { @@ -120257,7 +119794,6 @@ } }, "No Tables" : { - "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -120346,6 +119882,9 @@ } } } + }, + "No tables picked" : { + }, "No tables selected for export" : { "localizations" : { @@ -120728,9 +120267,6 @@ } } } - }, - "No sequences" : { - }, "No valid rows found in clipboard data." : { "localizations" : { @@ -121190,6 +120726,9 @@ } } } + }, + "None (Cross Join)" : { + }, "None (Top Level)" : { "localizations" : { @@ -122390,6 +121929,9 @@ } } } + }, + "Not loaded, %@" : { + }, "Not Now" : { "localizations" : { @@ -122430,6 +121972,9 @@ } } } + }, + "Not Null" : { + }, "NOT NULL" : { "extractionState" : "stale", @@ -122992,6 +122537,9 @@ } } } + }, + "Not validated" : { + }, "Not visible" : { "localizations" : { @@ -126120,6 +125668,18 @@ } } } + }, + "only in the left result" : { + + }, + "Only in the left result, not compared: %@" : { + + }, + "only in the right result" : { + + }, + "Only in the right result, not compared: %@" : { + }, "only in the source" : { "localizations" : { @@ -126140,6 +125700,16 @@ } } } + }, + "Only Left" : { + + }, + "Only PL/pgSQL functions and procedures can be debugged." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Only Right" : { + }, "Only the first %1$lld nodes were loaded, so this value was not searched in full. Switch to %2$@ to search all of it." : { "localizations" : { @@ -126306,6 +125876,9 @@ } } } + }, + "open" : { + }, "Open" : { "localizations" : { @@ -127415,6 +126988,9 @@ }, "Open in Query Editor takes up to %@ rows." : { + }, + "Open in Query Tab" : { + }, "Open in Window" : { "localizations" : { @@ -128441,6 +128017,9 @@ } } } + }, + "Opens a database connection in TablePro" : { + }, "Opens an AWS Systems Manager port forwarding session through a target instance." : { "localizations" : { @@ -129497,6 +129076,9 @@ } } } + }, + "Other rows hold exactly the same values, so the change cannot reach this row alone." : { + }, "Other sessions keep reading the view. Slower, because the new rows are compared with the old ones." : { "localizations" : { @@ -129920,12 +129502,6 @@ } } } - }, - "Owned by" : { - - }, - "Owned by %@" : { - }, "output_path must end in .%@ for this format." : { "localizations" : { @@ -130066,6 +129642,12 @@ } } } + }, + "Owned by" : { + + }, + "Owned by %@" : { + }, "Owned objects" : { "localizations" : { @@ -133260,6 +132842,12 @@ } } } + }, + "Pick the result to compare this one with." : { + + }, + "Pick the tables to join. Their foreign keys become the join conditions." : { + }, "Pick the type of database you want to connect to." : { "localizations" : { @@ -134443,6 +134031,7 @@ } }, "Plugin requires app version %@ or later, but current version is %@" : { + "extractionState" : "stale", "localizations" : { "en" : { "stringUnit" : { @@ -134969,6 +134558,9 @@ } } } + }, + "Position" : { + }, "Position in the parent's declared order" : { "extractionState" : "manual", @@ -135242,6 +134834,7 @@ }, "Pre-connect script timed out after 10 seconds" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -135282,6 +134875,7 @@ } }, "Pre-connect script was cancelled" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -136932,6 +136526,7 @@ } }, "Priority %d" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -138488,6 +138083,9 @@ } } } + }, + "Provide a JSON object or an array of objects." : { + }, "Providers" : { "localizations" : { @@ -139462,6 +139060,9 @@ } } } + }, + "query" : { + }, "Query" : { "localizations" : { @@ -142616,9 +142217,6 @@ }, "Read the live server panels TablePro shows: active sessions, server metrics, and slow queries. Available on every engine that has a Server Dashboard in TablePro." : { - }, - "Read the live server panels TablePro shows: active sessions, server metrics, and slow queries. Available on PostgreSQL, MySQL, SQL Server, ClickHouse, and Typesense." : { - }, "Read the live server panels TablePro shows: active sessions, server metrics, and slow queries. Available on PostgreSQL, MySQL, SQL Server, ClickHouse, DuckDB, and SQLite." : { "extractionState" : "stale", @@ -144471,6 +144069,9 @@ }, "Referenced Value" : { + }, + "References" : { + }, "references %@" : { "localizations" : { @@ -144819,6 +144420,9 @@ } } } + }, + "Refuse NULL" : { + }, "Refuse the statement unless it only reads, even where the connection allows writes." : { @@ -145676,6 +145280,9 @@ } } } + }, + "Remove “%@”" : { + }, "Remove “%@”?" : { "localizations" : { @@ -145839,6 +145446,9 @@ } } } + }, + "Remove Attached Database" : { + }, "Remove attachment" : { "localizations" : { @@ -146959,6 +146569,12 @@ } } } + }, + "Remove the comment" : { + + }, + "Remove the default" : { + }, "Remove the extensions from this connection in Edit Connection." : { "extractionState" : "manual", @@ -146986,6 +146602,9 @@ } } } + }, + "Remove this change from the queue" : { + }, "Remove this rule" : { "localizations" : { @@ -147307,6 +146926,9 @@ } } } + }, + "Rename field “%1$@” to “%2$@”" : { + }, "Rename Folder" : { "localizations" : { @@ -147317,9 +146939,23 @@ } } } + }, + "Rename Label" : { + + }, + "Rename label “%1$@” to “%2$@”" : { + + }, + "Rename the selected label" : { + + }, + "Rename to “%@”" : { + + }, + "Rename Type" : { + }, "Rename…" : { - "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -147933,6 +147569,7 @@ } }, "Replace…" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -149010,13 +148647,14 @@ } } }, - "Resolve the conflict in a Git client before committing this file." : { + "Resident Memory" : { }, - "Resident Memory" : { + "Resolve the conflict in a Git client before committing this file." : { }, "Resolved on every connect. It stays on this Mac and never syncs, because it can name a command to run." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -150310,6 +149948,9 @@ } } } + }, + "Result %1$d: %2$@" : { + }, "Result %d" : { "localizations" : { @@ -150390,6 +150031,12 @@ } } } + }, + "Result Closed" : { + + }, + "Result Format" : { + }, "Result Options" : { "localizations" : { @@ -151400,6 +151047,9 @@ }, "Rewrites every text column to this character set. TEXT can grow to MEDIUMTEXT, and the table is copied while it runs." : { + }, + "Right" : { + }, "right curly bracket" : { "extractionState" : "manual", @@ -151523,6 +151173,9 @@ } } } + }, + "right value, differs" : { + }, "Right-click a cell and choose Highlight to color rows by value." : { "localizations" : { @@ -151986,6 +151639,9 @@ } } } + }, + "row" : { + }, "Row" : { "localizations" : { @@ -152026,6 +151682,9 @@ } } } + }, + "Row (JSON or CSV)" : { + }, "Row %@ already exists. Edit that row, or give the new one another key." : { "extractionState" : "manual", @@ -152630,6 +152289,9 @@ } } } + }, + "Row Limit" : { + }, "Row no longer exists" : { "localizations" : { @@ -152844,6 +152506,9 @@ } } } + }, + "rows" : { + }, "Rows" : { "localizations" : { @@ -152884,6 +152549,9 @@ } } } + }, + "Rows (JSON or CSV)" : { + }, "Rows %lld-%lld" : { "localizations" : { @@ -152985,9 +152653,6 @@ } } } - }, - "Rows in %1$@ break the foreign key %2$@." : { - }, "Rows in %@ changed after they were compared. Compare again before generating the script." : { "localizations" : { @@ -153001,6 +152666,9 @@ }, "Rows in %@ may block the delete (not counted)" : { + }, + "Rows in %1$@ break the foreign key %2$@." : { + }, "Rows in this export" : { "localizations" : { @@ -153519,6 +153187,9 @@ } } } + }, + "Rows with the same values in these columns are compared. With none chosen, whole rows are matched." : { + }, "Rows, each an array aligned with columns" : { "extractionState" : "manual", @@ -153705,6 +153376,9 @@ } } } + }, + "Run ${query} on ${connection}" : { + }, "Run a maintenance operation the engine supports, on one table or on the whole database. TablePro generates the statements; the user approves them. Needs tools:write." : { "localizations" : { @@ -153748,6 +153422,9 @@ }, "Run a saved query on the Server Dashboard" : { + }, + "Run a SQL statement on a saved connection and pass the result to the next action." : { + }, "Run All Statements" : { "localizations" : { @@ -153758,6 +153435,9 @@ } } } + }, + "Run another query in this tab, or pin a result in another tab." : { + }, "Run button beside each statement" : { "localizations" : { @@ -154080,6 +153760,9 @@ } } } + }, + "Run Query" : { + }, "Run Script" : { "localizations" : { @@ -156159,6 +155842,9 @@ } } } + }, + "Save Type Changes" : { + }, "Save…" : { "localizations" : { @@ -159326,6 +159012,9 @@ } } } + }, + "Second value" : { + }, "Second value is required for BETWEEN" : { "localizations" : { @@ -159590,6 +159279,9 @@ }, "Secret ID File" : { + }, + "Section" : { + }, "Section Header" : { "extractionState" : "stale", @@ -159723,6 +159415,9 @@ }, "Seed %@ made these rows. The next run starts from a new seed." : { + }, + "select" : { + }, "Select" : { "localizations" : { @@ -162810,9 +162505,6 @@ } } } - }, - "Set Next Value…" : { - }, "Set as Active" : { "localizations" : { @@ -163016,6 +162708,12 @@ }, "Set in connections.json." : { + }, + "Set Next Value" : { + + }, + "Set Next Value…" : { + }, "Set NULL" : { "localizations" : { @@ -163099,6 +162797,9 @@ } } } + }, + "Set the comment" : { + }, "Set the connection's username to the IAM principal (a user email, or `name@project.iam` for a service account). The database password is not used." : { "localizations" : { @@ -163139,6 +162840,9 @@ } } } + }, + "Set the default to %@" : { + }, "Set the Microsoft Entra ID application (client) ID for this connection." : { "extractionState" : "manual", @@ -163162,6 +162866,7 @@ }, "Set to NULL" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -163699,6 +163404,7 @@ } }, "Shell script to run before connecting. Non-zero exit aborts connection." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -163821,6 +163527,15 @@ } } } + }, + "Shortcuts" : { + + }, + "Shortcuts wants to add %1$lld rows to \"%2$@\"" : { + + }, + "Shortcuts wants to add a row to \"%@\"" : { + }, "Shortest" : { @@ -165498,6 +165213,9 @@ } } } + }, + "Show Structure of %@" : { + }, "Show system databases and schemas" : { "localizations" : { @@ -165857,12 +165575,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "%lld lignes affichées" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -168813,6 +168525,9 @@ } } } + }, + "Some rows are no longer in the table, so their values could not be read." : { + }, "Some rows couldn't be counted, so more rows may change than shown." : { @@ -169283,6 +168998,7 @@ } }, "Sorted ascending" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -169327,6 +169043,7 @@ "generatesSymbol" : false }, "Sorted descending" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -172074,6 +171791,9 @@ } } } + }, + "Start" : { + }, "Start one to ask about this connection." : { "localizations" : { @@ -172477,6 +172197,9 @@ } } } + }, + "Statement" : { + }, "Statement %1$d of %2$d failed: %3$@" : { "extractionState" : "manual", @@ -174298,7 +174021,6 @@ "generatesSymbol" : false }, "Stopping…" : { - "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -174973,6 +174695,9 @@ } } } + }, + "Subtype" : { + }, "Succeeded" : { "localizations" : { @@ -176506,11 +176231,9 @@ } } } - }, - "Sync Delayed" : { - }, "Sync Error" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -176767,6 +176490,7 @@ } }, "Synced" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -176853,6 +176577,7 @@ } }, "Syncing with iCloud…" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -177851,6 +177576,9 @@ }, "Table Created with Errors" : { + }, + "Table Data" : { + }, "Table DDL" : { "extractionState" : "manual", @@ -179110,7 +178838,6 @@ }, "TablePro" : { - "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -179150,6 +178877,42 @@ } } }, + "TablePro and the SSH server could not agree on how to encrypt the connection." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro and the SSH server have no cipher in common." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro and the SSH server have no key exchange or host key method in common." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro and the SSH server have no MAC algorithm in common." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro and the SSH server have no signature algorithm in common for this key." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro can't pass any value of type %2$@ to %1$@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro can't read %@. Check that this account can see its source." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro can't read this unit's source." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro can't turn text into a %2$@ value for %1$@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "TablePro cannot sign an RDS token for \"%@\". Enter the RDS Endpoint (for example mydb.abc123.us-east-1.rds.amazonaws.com:5432) when you connect through a port forward or bastion." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -179907,6 +179670,46 @@ } } }, + "TablePro is free for everyone, at work too, and none of it will become paid." : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro est gratuit pour tout le monde, au travail aussi, et rien ne deviendra payant." + } + }, + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro는 업무용을 포함해 누구에게나 무료이며, 어떤 기능도 유료로 바뀌지 않습니다." + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro iş yerinde de dahil herkes için ücretsizdir ve hiçbir kısmı ücretli olmayacak." + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro miễn phí cho mọi người, kể cả khi dùng cho công việc, và không phần nào sẽ chuyển sang trả phí." + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro 对所有人免费,工作中使用也一样,而且任何部分都不会变成付费。" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro 對所有人免費,工作中使用也一樣,而且任何部分都不會變成付費。" + } + } + } + }, "TablePro is reading or writing rows on this connection with settings of its own." : { "extractionState" : "manual", "generatesSymbol" : false @@ -180055,6 +179858,10 @@ } } }, + "TablePro signs RSA keys with rsa-sha2-256 or rsa-sha2-512, not with ssh-rsa, which uses SHA-1." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "TablePro supports Oracle Database 11.1 and later. This server reports an older release (10g or earlier)." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -180067,6 +179874,18 @@ } } }, + "TablePro supports these ciphers: %@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro supports these key exchange methods: %@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "TablePro supports these MAC algorithms: %@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "TablePro takes no funding and sells no data. If you want to do more, sponsorship goes to the same place." : { "localizations" : { "fr" : { @@ -182313,6 +182132,9 @@ } } } + }, + "That connection no longer exists in TablePro." : { + }, "That destination is not valid for this engine." : { "extractionState" : "stale", @@ -182687,6 +182509,9 @@ } } } + }, + "That result is no longer open. Choose another one." : { + }, "That tab is not open." : { "localizations" : { @@ -183278,7 +183103,48 @@ } } }, + "The app stays free, all of it, at work too. If TablePro is part of your work, please buy a license. It is a request, not a condition, and it pays for the next release." : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "L’app reste gratuite, dans son intégralité, au travail aussi. Si TablePro fait partie de votre travail, merci d’acheter une licence. C’est une demande, pas une condition, et elle finance la prochaine version." + } + }, + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "앱 전체가 업무용을 포함해 계속 무료입니다. TablePro가 업무의 일부라면 라이선스를 구매해 주세요. 조건이 아니라 부탁이며, 라이선스가 다음 릴리스를 만듭니다." + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Uygulamanın tamamı, iş yerinde de ücretsiz kalır. TablePro işinizin bir parçasıysa lütfen bir lisans satın alın. Bu bir koşul değil, bir rica; bir sonraki sürümü de lisanslar finanse ediyor." + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Toàn bộ ứng dụng vẫn miễn phí, kể cả khi dùng cho công việc. Nếu TablePro là một phần công việc của bạn, hãy mua một giấy phép. Đây là lời đề nghị, không phải điều kiện, và nó chi trả cho bản phát hành tiếp theo." + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "整个 App 一直免费,工作中使用也一样。如果 TablePro 是你工作的一部分,请购买许可证。这是请求,不是条件,它支付了下一个版本的开发。" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "整個 App 一直免費,工作中使用也一樣。如果 TablePro 是你工作的一部分,請購買授權。這是請求,不是條件,它支付了下一個版本的開發。" + } + } + } + }, "The app stays free, all of it. If TablePro is useful to you, a license is what pays for the next release." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -183331,6 +183197,14 @@ } } }, + "The attached database \"%@\" has no file." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "The attached file \"%@\" has no name." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "The authenticity of host '%@' can't be established.\n\n%@ key fingerprint is:\n%@\n\nAre you sure you want to continue connecting?" : { "localizations" : { "en" : { @@ -183839,6 +183713,9 @@ } } } + }, + "The cache must be at least 1." : { + }, "The catalog location is not a folder: %@" : { "localizations" : { @@ -183917,6 +183794,12 @@ }, "The changes were edited while the save was checking other tables. Save again to include them." : { + }, + "The changes were saved, but the type could not be read back. Reload before editing it again." : { + + }, + "The changes you have not saved will be lost." : { + }, "The chat and Agent mode keep the active provider." : { @@ -184004,12 +183887,13 @@ "The CHECK on column %@ names column %@, which this save renames. Save the rename on its own first." : { "extractionState" : "manual", "generatesSymbol" : false - }, - "The cache must be at least 1." : { - }, "The chosen theme is not on this Mac, so %@ is used." : { + }, + "The client certificate and key could not be written to a temporary file." : { + "extractionState" : "manual", + "generatesSymbol" : false }, "The client certificate at %@ could not be read as a PEM or DER certificate." : { "extractionState" : "manual", @@ -184549,6 +184433,9 @@ } } } + }, + "The column “%1$@” is no longer in “%2$@”." : { + }, "The column keeps %1$lld of its %2$lld digits after the point, so a longer fraction is rounded." : { "localizations" : { @@ -185326,6 +185213,9 @@ } } } + }, + "The CSV data has no header row." : { + }, "The Dameng connection is closed." : { "extractionState" : "manual", @@ -185390,6 +185280,12 @@ "The data changed after it was read." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "The data could not be read: %@" : { + + }, + "The data has no values to insert into %@." : { + }, "The database “%@” has an open transaction. Closing rolls it back and discards its uncommitted changes." : { "localizations" : { @@ -185802,6 +185698,7 @@ }, "The diagram could not be converted to a PNG image." : { "comment" : "Error message when the ER diagram could not be exported as PNG.", + "extractionState" : "stale", "isCommentAutoGenerated" : true, "localizations" : { "fr" : { @@ -185844,6 +185741,9 @@ }, "The diagram could not be rendered as %@." : { + }, + "The diagram is saving its changes. Make this change once the save has finished." : { + }, "The disconnect was cancelled." : { "localizations" : { @@ -186530,6 +186430,9 @@ } } } + }, + "The first column of the first row" : { + }, "The first sheet has no header row to map columns from." : { "extractionState" : "manual", @@ -186679,6 +186582,9 @@ } } } + }, + "The foreign key “%1$@” is no longer on “%2$@”." : { + }, "The form produced no statements to run." : { "localizations" : { @@ -186778,6 +186684,9 @@ } } } + }, + "The increment cannot be zero." : { + }, "The index %@ is left out" : { "localizations" : { @@ -186813,9 +186722,6 @@ }, "The installed %1$@ does not support %2$@. Update it in Settings > Extensions." : { - }, - "The installed %1$@ does not support %2$@. Update it in Settings > Plugins." : { - }, "The instance connection name must be in the form project:region:instance." : { "localizations" : { @@ -186883,6 +186789,9 @@ } } } + }, + "The JSON array must contain only objects." : { + }, "The JSON is nested more deeply than DynamoDB allows" : { "extractionState" : "manual", @@ -187032,6 +186941,9 @@ } } } + }, + "The left result stopped at the row cap and is compared on the %@ rows it holds. A row past the cap can show as only on the right." : { + }, "The license has been suspended." : { "localizations" : { @@ -187703,6 +187615,9 @@ } } } + }, + "The minimum must be less than the maximum." : { + }, "The MongoDB client library will not insert a field whose name is empty. Remove the field, then set it on the saved document." : { "extractionState" : "manual", @@ -187727,6 +187642,9 @@ } } } + }, + "The name a statement uses for this database, as in name.table." : { + }, "The name of a DIRECTORY object, not a path. The DBA who created it chose where it points." : { "extractionState" : "stale", @@ -189316,6 +189234,9 @@ } } } + }, + "The request timed out." : { + }, "The request to BigQuery timed out." : { "extractionState" : "manual", @@ -189388,6 +189309,9 @@ } } } + }, + "The right result stopped at the row cap and is compared on the %@ rows it holds. A row past the cap can show as only on the left." : { + }, "The rollback did not undo everything. A statement in the file, such as COMMIT, CREATE, ALTER or DROP, committed the import's transaction as it ran, so some of the statements before the failure were kept." : { @@ -189418,6 +189342,9 @@ "The routine was stopped, and its uncommitted changes were rolled back." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "The row could not be looked up: %@" : { + }, "The row filter on %@ was not a single expression, so every row was exported." : { "extractionState" : "manual", @@ -189443,6 +189370,14 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "The row has no loaded values to be found by." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "The row's loaded values are missing, so it cannot be found again." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "The row's primary key is not loaded, so the row cannot be found." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -189601,9 +189536,6 @@ } } } - }, - "The minimum must be less than the maximum." : { - }, "The save also truncated or dropped a table." : { "localizations" : { @@ -189783,6 +189715,9 @@ } } } + }, + "The saved list could not be read. Adding a database replaces it." : { + }, "The saved list could not be read. Adding an extension replaces it." : { "localizations" : { @@ -189794,6 +189729,10 @@ } } }, + "The saved list of attached databases is not in a format TablePro can read." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "The saved Microsoft Entra ID sign-in expired. Sign in again." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -189903,6 +189842,9 @@ } } } + }, + "The schema changes staged in this diagram will be lost." : { + }, "The script already ran. Compare again to see where the target stands." : { "localizations" : { @@ -190346,6 +190288,22 @@ } } }, + "The server offers only ciphers TablePro no longer supports: %@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "The server offers only host key types TablePro no longer supports: %@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "The server offers only key exchange methods TablePro no longer supports: %@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "The server offers only MAC algorithms TablePro no longer supports: %@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "The server redirected the request to %@. TablePro does not follow redirects, and the coordinator never sends one, so a proxy in front of it did. Check the host, port and SSL Mode." : { "extractionState" : "manual", "generatesSymbol" : false @@ -190373,6 +190331,12 @@ } } } + }, + "The server refused a change, so nothing was saved." : { + + }, + "The server refused a change. The changes before it were saved." : { + }, "The server rejected TablePro's credential." : { "localizations" : { @@ -190442,6 +190406,10 @@ } } }, + "The server runs %@." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "The server sent an unexpected message and the connection was reset. Run the query again." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -191139,6 +191107,9 @@ } } } + }, + "The SSH server closed the connection during the handshake." : { + }, "The SSH server closed the connection while the agent's keys were being offered. Servers accept a limited number of tries, 6 by default, and an agent holding more keys than that runs out before the right one. Name the key with IdentityFile and IdentitiesOnly yes for this host in ~/.ssh/config, or set Identity File on this connection." : { "localizations" : { @@ -191331,6 +191302,9 @@ } } } + }, + "The SSH server ended the connection during the handshake: %@" : { + }, "The SSH server is asking for additional verification." : { "localizations" : { @@ -191547,6 +191521,7 @@ } }, "The statement that was sent, with its prefix" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -191679,6 +191654,10 @@ } } }, + "The structure of a table in an attached database cannot be changed here." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "The structure of the tables this query uses is read when the request is sent." : { "localizations" : { "fr" : { @@ -192424,6 +192403,9 @@ } } } + }, + "The text encoding is invalid." : { + }, "The text is not valid JSON." : { "extractionState" : "manual", @@ -192788,6 +192770,12 @@ } } } + }, + "The two results share no column name, so there is nothing to compare." : { + + }, + "The type changed on the server since it was read. Reload it, then save again." : { + }, "The type's shape, such as enum, composite, domain, range, aliasType, tableType or clrType" : { "localizations" : { @@ -193769,6 +193757,10 @@ } } }, + "This _id reads as more than one value type in this collection, such as text and an ObjectId, so TablePro cannot tell which document the row names. Use a query." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "This account signs in with Kerberos. Set Authentication to Kerberos (GSSAPI), then reconnect." : { "extractionState" : "manual", "generatesSymbol" : false @@ -193970,6 +193962,15 @@ "This call matches more than one overload of %@, so Oracle can't run it (PLS-00307)." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "This cannot be undone. The drop fails while a column or other object still uses it." : { + + }, + "This cannot be undone. Without Cascade, the drop fails while a column, function or other type still uses it." : { + + }, + "This change does not apply to this kind of type." : { + }, "This change needs %@ to be recreated, and it cannot be recreated from the definition the database stored for it. Virtual tables, such as full-text search tables, are the usual reason." : { "localizations" : { @@ -194793,6 +194794,9 @@ } } } + }, + "This connection's startup commands leave a transaction open, so rows added from Shortcuts could not be committed." : { + }, "This conversation is too long to continue. Start a new conversation and send your message there." : { "localizations" : { @@ -194964,6 +194968,9 @@ } } } + }, + "This database cannot make the change “%@”." : { + }, "This database cannot reference a table in another schema." : { "localizations" : { @@ -195310,6 +195317,9 @@ } } } + }, + "This database reports no tables to join." : { + }, "This database reports no tables to search." : { @@ -198163,6 +198173,7 @@ } }, "This plugin requires TablePro %@ or later" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -198567,6 +198578,16 @@ } } } + }, + "This sequence's settings could not be read, so they cannot be changed here." : { + + }, + "This server can't show the default value of %@, so enter a value for it." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "This server cannot rename a label." : { + }, "This server did not identify as Databend. Check the host and port of its MySQL handler." : { "extractionState" : "manual", @@ -199013,6 +199034,9 @@ } } } + }, + "This Tab" : { + }, "This tab is on %@. Switch the connection to that database to run it." : { "localizations" : { @@ -201544,6 +201568,9 @@ } } } + }, + "Too many rows. Add up to %lld rows at a time." : { + }, "Tool" : { "localizations" : { @@ -203441,6 +203468,7 @@ }, "Try again, or install the plugin from Settings > Plugins." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -203609,6 +203637,9 @@ } } } + }, + "Turn off to add the check now and validate the stored values later." : { + }, "Turn on Apple Intelligence in System Settings > Apple Intelligence & Siri." : { @@ -203771,6 +203802,7 @@ }, "Type %@ in the query to set where the cursor lands after keyword expansion." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -206655,9 +206687,6 @@ } } } - }, - "Update Needed" : { - }, "Update Now" : { "localizations" : { @@ -206703,6 +206732,7 @@ }, "Update or reinstall the plugin in Settings > Plugins." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -206888,6 +206918,7 @@ }, "Update the database driver in Settings > Plugins to run a batch more than once with GO." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -206898,6 +206929,7 @@ } }, "Update the database driver in Settings > Plugins to run several statements in one call." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -209108,6 +209140,15 @@ } } } + }, + "Validate" : { + + }, + "Validate constraint “%@”" : { + + }, + "Validated on save" : { + }, "Validation Failed" : { "localizations" : { @@ -209819,12 +209860,6 @@ "fr" : { "variations" : { "plural" : { - "many" : { - "stringUnit" : { - "state" : "translated", - "value" : "Valeurs de %lld lignes chargées" - } - }, "one" : { "stringUnit" : { "state" : "translated", @@ -211120,6 +211155,7 @@ } }, "Vim mode" : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -211448,6 +211484,12 @@ }, "Warnings: %d" : { + }, + "Was “%@”" : { + + }, + "Was %@" : { + }, "Watch shared folders for connection files." : { "localizations" : { @@ -213518,6 +213560,9 @@ } } } + }, + "Whole row" : { + }, "Whole Words" : { "localizations" : { @@ -215040,6 +215085,7 @@ } }, "You have unsaved changes to the table structure. Refreshing will discard these changes." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -215079,6 +215125,46 @@ } } }, + "You've used TablePro on %lld days" : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Vous avez utilisé TablePro pendant %lld jours" + } + }, + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro를 %lld일 동안 사용하셨습니다" + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro'yu %lld gün kullandınız" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Bạn đã dùng TablePro được %lld ngày" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "你已经使用 TablePro %lld 天了" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "你已經使用 TablePro %lld 天了" + } + } + } + }, "Your account is not allowed to read the source of %@" : { "extractionState" : "manual", "generatesSymbol" : false, diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Registry.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Registry.swift index b9f7e9bfc0..7c88e6920e 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Registry.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Registry.swift @@ -40,6 +40,10 @@ extension MainContentCoordinator { .contains { $0.hasAnyUnsavedWork() } } + static var isAnyQueryRunning: Bool { + activeCoordinators.values.contains { $0.tabExecution.isAnyExecuting } + } + static func hasRunningQuery(forConnection connectionId: UUID) -> Bool { activeCoordinators.values .filter { $0.connectionId == connectionId } diff --git a/apple/TablePro/Views/Support/SupportAskSheetPresenter.swift b/apple/TablePro/Views/Support/SupportAskSheetPresenter.swift new file mode 100644 index 0000000000..733123e62f --- /dev/null +++ b/apple/TablePro/Views/Support/SupportAskSheetPresenter.swift @@ -0,0 +1,84 @@ +// +// SupportAskSheetPresenter.swift +// TablePro +// + +import AppKit +import SwiftUI + +@MainActor +internal protocol SupportAskPresenting: AnyObject { + var isPresenting: Bool { get } + + /// Attaches the ask to `parent`. Returns false, and calls nothing, when it cannot. + func present(usageDays: Int, on parent: NSWindow, answer: @escaping (SupportAskAnswer) -> Void) -> Bool +} + +/// Runs the ask as a sheet on the window in use, the shape `ExportSheetPresenter` uses. +/// +/// Every way the sheet can go reports exactly one answer: a button, Escape, which is Not Now, or +/// the window closing under it, which counts as Not Now too. +@MainActor +internal final class SupportAskSheetPresenter: SupportAskPresenting { + private var sheetWindow: NSWindow? + private weak var parentWindow: NSWindow? + private var report: ((SupportAskAnswer) -> Void)? + private var parentCloseObserver: NSObjectProtocol? + + internal init() {} + + internal var isPresenting: Bool { + sheetWindow != nil + } + + internal func present( + usageDays: Int, + on parent: NSWindow, + answer: @escaping (SupportAskAnswer) -> Void + ) -> Bool { + guard sheetWindow == nil, parent.attachedSheet == nil else { return false } + + let view = SupportAskView(usageDays: usageDays) { [weak self] choice in + self?.finish(with: choice) + } + let hosting = NSHostingController(rootView: view) + hosting.sizingOptions = [.preferredContentSize] + let sheet = NSWindow(contentViewController: hosting) + sheet.isReleasedWhenClosed = false + + sheetWindow = sheet + parentWindow = parent + report = answer + parentCloseObserver = NotificationCenter.default.addObserver( + forName: NSWindow.willCloseNotification, + object: parent, + queue: .main + ) { [weak self] _ in + MainActor.assumeIsolated { self?.finish(with: .notNow) } + } + parent.beginSheet(sheet) + return true + } + + private func finish(with choice: SupportAskAnswer) { + guard let sheet = sheetWindow else { return } + let reportAnswer = report + let parent = parentWindow + + sheetWindow = nil + parentWindow = nil + report = nil + if let parentCloseObserver { + NotificationCenter.default.removeObserver(parentCloseObserver) + } + parentCloseObserver = nil + + if let parent, parent.attachedSheet === sheet { + parent.endSheet(sheet) + } else { + sheet.orderOut(nil) + } + sheet.contentViewController = nil + reportAnswer?(choice) + } +} diff --git a/apple/TablePro/Views/Support/SupportAskView.swift b/apple/TablePro/Views/Support/SupportAskView.swift new file mode 100644 index 0000000000..c8c58d3f53 --- /dev/null +++ b/apple/TablePro/Views/Support/SupportAskView.swift @@ -0,0 +1,64 @@ +// +// SupportAskView.swift +// TablePro +// + +import AppKit +import SwiftUI + +/// The license ask, shown as a sheet once someone has used TablePro on 30 days. +/// +/// It says what the Support window says, once, and takes any answer: buying, saying TablePro is +/// for personal use, which quiets it for a year, or not now. Nothing is withheld whichever is +/// chosen. +struct SupportAskView: View { + let usageDays: Int + let answer: (SupportAskAnswer) -> Void + + var body: some View { + VStack(alignment: .leading, spacing: 20) { + HStack(alignment: .top, spacing: 16) { + Image(nsImage: NSApp.applicationIconImage) + .resizable() + .scaledToFit() + .frame(width: 64, height: 64) + .accessibilityHidden(true) + + VStack(alignment: .leading, spacing: 10) { + Text("You've used TablePro on \(usageDays) days") + .font(.headline) + + Text("TablePro is free for everyone, at work too, and none of it will become paid.") + .fixedSize(horizontal: false, vertical: true) + + Text( + "If TablePro is part of your work now, please buy a license. Licenses are what pay for the next release." + ) + .fixedSize(horizontal: false, vertical: true) + } + } + + HStack(spacing: 12) { + Button("I Use It Personally") { answer(.personal) } + .accessibilityIdentifier("support-ask-personal") + + Spacer() + + Button("Not Now") { answer(.notNow) } + .keyboardShortcut(.cancelAction) + .accessibilityIdentifier("support-ask-not-now") + + Button("Buy a License") { answer(.buy) } + .keyboardShortcut(.defaultAction) + .accessibilityIdentifier("support-ask-buy") + } + } + .padding(20) + .frame(width: 480) + .fixedSize(horizontal: false, vertical: true) + } +} + +#Preview { + SupportAskView(usageDays: 30) { _ in } +} diff --git a/apple/TablePro/Views/Support/SupportView.swift b/apple/TablePro/Views/Support/SupportView.swift index dfdb2288af..97bb257a44 100644 --- a/apple/TablePro/Views/Support/SupportView.swift +++ b/apple/TablePro/Views/Support/SupportView.swift @@ -63,8 +63,9 @@ struct SupportView: View { ), String( localized: """ - The app stays free, all of it. If TablePro is useful to you, a license is what \ - pays for the next release. + The app stays free, all of it, at work too. If TablePro is part of your work, \ + please buy a license. It is a request, not a condition, and it pays for the \ + next release. """ ) ] diff --git a/apple/TableProTests/Core/Services/Licensing/SupportLinksTests.swift b/apple/TableProTests/Core/Services/Licensing/SupportLinksTests.swift index b46b758778..caf724177e 100644 --- a/apple/TableProTests/Core/Services/Licensing/SupportLinksTests.swift +++ b/apple/TableProTests/Core/Services/Licensing/SupportLinksTests.swift @@ -12,7 +12,7 @@ import Testing struct SupportLinksTests { private static let everyReferrer: [SupportReferrer] = - [.supportWindow, .aboutPanel, .licenseSettings, .activationSheet] + [.supportWindow, .aboutPanel, .licenseSettings, .activationSheet, .ask] + ProFeature.allCases.map { .featureGate($0) } private static func components(_ url: URL) -> URLComponents? { diff --git a/apple/TableProTests/Core/Services/Support/SupportAskCoordinatorTests.swift b/apple/TableProTests/Core/Services/Support/SupportAskCoordinatorTests.swift new file mode 100644 index 0000000000..373180029a --- /dev/null +++ b/apple/TableProTests/Core/Services/Support/SupportAskCoordinatorTests.swift @@ -0,0 +1,224 @@ +// +// SupportAskCoordinatorTests.swift +// TableProTests +// + +import AppKit +import Foundation +@testable import TablePro +import TableProTestSupport +import Testing + +@MainActor +struct SupportAskCoordinatorTests { + @MainActor + final class RecordingPresenter: SupportAskPresenting { + private(set) var shownDays: [Int] = [] + private var reply: ((SupportAskAnswer) -> Void)? + + var isPresenting: Bool { + reply != nil + } + + func present(usageDays: Int, on parent: NSWindow, answer: @escaping (SupportAskAnswer) -> Void) -> Bool { + shownDays.append(usageDays) + reply = answer + return true + } + + func choose(_ answer: SupportAskAnswer) { + let pending = reply + reply = nil + pending?(answer) + } + } + + @MainActor + final class Harness { + let suite: EphemeralDefaults + let presenter = RecordingPresenter() + let window: NSWindow + let launchedAt = Date(timeIntervalSince1970: 1_793_000_000) + var now: Date + var hasLicense = false + var isQueryRunning = false + private(set) var openedURLs: [URL] = [] + private(set) var coordinator: SupportAskCoordinator! + + init(daysBeforeLaunch: Int, windowIdentifier: String = WindowIdentifier.connection) throws { + suite = try EphemeralDefaults() + now = launchedAt + var calendar = Calendar(identifier: .gregorian) + calendar.timeZone = .gmt + + // The launch itself counts as one more day of use. + suite.defaults.set(daysBeforeLaunch, forKey: UsageDayCounter.Keys.days) + suite.defaults.set("2026-10-01", forKey: UsageDayCounter.Keys.lastDay) + + window = NSWindow( + contentRect: NSRect(x: -20_000, y: -20_000, width: 320, height: 200), + styleMask: [.titled], + backing: .buffered, + defer: true + ) + window.isReleasedWhenClosed = false + window.identifier = NSUserInterfaceItemIdentifier(windowIdentifier) + window.orderFront(nil) + + let counter = UsageDayCounter(defaults: suite.defaults, calendar: calendar) + coordinator = SupportAskCoordinator( + counter: counter, + defaults: suite.defaults, + presenter: presenter, + environment: .init( + hasLicense: { [unowned self] in hasLicense }, + isQueryRunning: { [unowned self] in isQueryRunning }, + keyWindow: { [unowned self] in window }, + isModalSessionRunning: { false }, + openPricing: { [unowned self] in openedURLs.append($0) }, + now: { [unowned self] in now } + ), + calendar: calendar, + clock: ManualClock() + ) + coordinator.start() + } + + func advance(days: Int, seconds: TimeInterval = 0) { + now = now.addingTimeInterval(TimeInterval(days) * 86_400 + seconds) + } + + func tearDown() { + window.orderOut(nil) + } + } + + @Test("On the 30th day of use, past the launch quiet period, the ask shows on the window in use") + func asksOnTheThirtiethDay() throws { + let harness = try Harness(daysBeforeLaunch: 29) + defer { harness.tearDown() } + + harness.advance(days: 0, seconds: 360) + harness.coordinator.askIfDue() + + #expect(harness.presenter.shownDays == [30]) + #expect(harness.suite.defaults.object(forKey: SupportAskCoordinator.Keys.lastAskedAt) as? Date == harness.now) + } + + @Test("The 29th day of use asks nothing") + func waitsBeforeTheThirtiethDay() throws { + let harness = try Harness(daysBeforeLaunch: 28) + defer { harness.tearDown() } + + harness.advance(days: 0, seconds: 360) + harness.coordinator.askIfDue() + + #expect(harness.presenter.shownDays.isEmpty) + } + + @Test("Inside the launch quiet period, or while a query runs, nothing shows") + func quietMomentsOnly() throws { + let harness = try Harness(daysBeforeLaunch: 29) + defer { harness.tearDown() } + + harness.advance(days: 0, seconds: 60) + harness.coordinator.askIfDue() + #expect(harness.presenter.shownDays.isEmpty) + + harness.advance(days: 0, seconds: 600) + harness.isQueryRunning = true + harness.coordinator.askIfDue() + #expect(harness.presenter.shownDays.isEmpty) + + harness.isQueryRunning = false + harness.coordinator.askIfDue() + #expect(harness.presenter.shownDays == [30]) + } + + @Test("A license holds the ask however many days of use") + func licenseHoldsIt() throws { + let harness = try Harness(daysBeforeLaunch: 200) + defer { harness.tearDown() } + harness.hasLicense = true + + harness.advance(days: 0, seconds: 600) + harness.coordinator.askIfDue() + + #expect(harness.presenter.shownDays.isEmpty) + } + + @Test("A connection window opened after the first, whose identifier carries a suffix, carries the ask") + func suffixedConnectionWindowHostsIt() throws { + let harness = try Harness(daysBeforeLaunch: 29, windowIdentifier: "\(WindowIdentifier.connection)-2") + defer { harness.tearDown() } + + harness.advance(days: 0, seconds: 600) + harness.coordinator.askIfDue() + + #expect(harness.presenter.shownDays == [30]) + } + + @Test("Settings and other utility windows never carry the ask") + func onlyWorkWindowsHostIt() throws { + let harness = try Harness(daysBeforeLaunch: 29, windowIdentifier: WindowIdentifier.settings) + defer { harness.tearDown() } + + harness.advance(days: 0, seconds: 600) + harness.coordinator.askIfDue() + + #expect(harness.presenter.shownDays.isEmpty) + } + + @Test("Buying opens the pricing page tagged app-ask, and the next ask waits 90 days") + func buyOpensPricingAndWaits() throws { + let harness = try Harness(daysBeforeLaunch: 29) + defer { harness.tearDown() } + harness.advance(days: 0, seconds: 600) + harness.coordinator.askIfDue() + + harness.presenter.choose(.buy) + + let opened = try #require(harness.openedURLs.first) + let query = URLComponents(url: opened, resolvingAgainstBaseURL: false)?.queryItems + #expect(query == [URLQueryItem(name: "ref", value: "app-ask")]) + + harness.advance(days: 89) + harness.coordinator.askIfDue() + #expect(harness.presenter.shownDays == [30]) + + harness.advance(days: 1) + harness.coordinator.askIfDue() + #expect(harness.presenter.shownDays == [30, 30]) + } + + @Test("Personal use quiets the ask for a year") + func personalUseWaitsAYear() throws { + let harness = try Harness(daysBeforeLaunch: 29) + defer { harness.tearDown() } + harness.advance(days: 0, seconds: 600) + harness.coordinator.askIfDue() + + harness.presenter.choose(.personal) + + harness.advance(days: 364) + harness.coordinator.askIfDue() + #expect(harness.presenter.shownDays == [30]) + #expect(harness.openedURLs.isEmpty) + + harness.advance(days: 1) + harness.coordinator.askIfDue() + #expect(harness.presenter.shownDays == [30, 30]) + } + + @Test("While the sheet is open, no second ask is attempted") + func oneAskAtATime() throws { + let harness = try Harness(daysBeforeLaunch: 29) + defer { harness.tearDown() } + harness.advance(days: 0, seconds: 600) + + harness.coordinator.askIfDue() + harness.coordinator.askIfDue() + + #expect(harness.presenter.shownDays == [30]) + } +} diff --git a/apple/TableProTests/Core/Services/Support/SupportAskScheduleTests.swift b/apple/TableProTests/Core/Services/Support/SupportAskScheduleTests.swift new file mode 100644 index 0000000000..4ae7df5c74 --- /dev/null +++ b/apple/TableProTests/Core/Services/Support/SupportAskScheduleTests.swift @@ -0,0 +1,77 @@ +// +// SupportAskScheduleTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct SupportAskScheduleTests { + private let calendar: Calendar = { + var calendar = Calendar(identifier: .gregorian) + calendar.timeZone = .gmt + return calendar + }() + + private let askedAt = Date(timeIntervalSince1970: 1_793_000_000) + + private func day(_ offset: Int) -> Date { + askedAt.addingTimeInterval(TimeInterval(offset) * 86_400) + } + + @Test("Nothing is asked before the 30th day of use") + func waitsForTheThirtiethDay() { + let before = SupportAskSchedule(usageDays: 29, hasLicense: false, lastAskedAt: nil, lastAnswer: nil) + let on = SupportAskSchedule(usageDays: 30, hasLicense: false, lastAskedAt: nil, lastAnswer: nil) + + #expect(!before.isDue(at: askedAt, calendar: calendar)) + #expect(on.isDue(at: askedAt, calendar: calendar)) + } + + @Test("A license closes the ask whatever the count") + func licenseClosesIt() { + let schedule = SupportAskSchedule(usageDays: 400, hasLicense: true, lastAskedAt: nil, lastAnswer: nil) + + #expect(!schedule.isDue(at: askedAt, calendar: calendar)) + } + + @Test( + "After any answer but personal use, the next ask waits 90 days", + arguments: [nil, SupportAskAnswer.buy, .notNow] as [SupportAskAnswer?] + ) + func ninetyDaysAfterAnAsk(answer: SupportAskAnswer?) { + let schedule = SupportAskSchedule(usageDays: 45, hasLicense: false, lastAskedAt: askedAt, lastAnswer: answer) + + #expect(!schedule.isDue(at: day(89), calendar: calendar)) + #expect(schedule.isDue(at: day(90), calendar: calendar)) + } + + @Test("Personal use quiets the ask for a year") + func personalUseWaitsAYear() { + let schedule = SupportAskSchedule(usageDays: 45, hasLicense: false, lastAskedAt: askedAt, lastAnswer: .personal) + + #expect(!schedule.isDue(at: day(90), calendar: calendar)) + #expect(!schedule.isDue(at: day(364), calendar: calendar)) + #expect(schedule.isDue(at: day(365), calendar: calendar)) + } + + @Test("The ask waits out the first five minutes after launch") + func launchQuietPeriod() { + let launchedAt = askedAt + + #expect(!SupportAskSchedule.isQuietMoment( + launchedAt: launchedAt, now: launchedAt.addingTimeInterval(299), isQueryRunning: false + )) + #expect(SupportAskSchedule.isQuietMoment( + launchedAt: launchedAt, now: launchedAt.addingTimeInterval(300), isQueryRunning: false + )) + } + + @Test("A running query holds the ask") + func runningQueryHoldsIt() { + #expect(!SupportAskSchedule.isQuietMoment( + launchedAt: askedAt, now: askedAt.addingTimeInterval(3_600), isQueryRunning: true + )) + } +} diff --git a/apple/TableProTests/Core/Services/Support/UsageDayCounterTests.swift b/apple/TableProTests/Core/Services/Support/UsageDayCounterTests.swift new file mode 100644 index 0000000000..a23cc7fea5 --- /dev/null +++ b/apple/TableProTests/Core/Services/Support/UsageDayCounterTests.swift @@ -0,0 +1,71 @@ +// +// UsageDayCounterTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct UsageDayCounterTests { + /// UTC+7, so a UTC timestamp near midnight lands on a different local day than its UTC date. + private let calendar: Calendar = { + var calendar = Calendar(identifier: .gregorian) + calendar.timeZone = TimeZone(secondsFromGMT: 7 * 3_600) ?? .gmt + return calendar + }() + + private func date(_ text: String) throws -> Date { + try #require(ISO8601DateFormatter().date(from: text)) + } + + @Test("A day counts once however often the app is used that day") + func sameDayCountsOnce() throws { + let suite = try EphemeralDefaults() + let counter = UsageDayCounter(defaults: suite.defaults, calendar: calendar) + + try counter.recordUse(at: date("2026-11-02T01:00:00Z")) + try counter.recordUse(at: date("2026-11-02T09:30:00Z")) + try counter.recordUse(at: date("2026-11-02T16:59:00Z")) + + #expect(counter.days == 1) + } + + @Test("Each new day of use adds one, however many days were skipped") + func newDaysAdd() throws { + let suite = try EphemeralDefaults() + let counter = UsageDayCounter(defaults: suite.defaults, calendar: calendar) + + try counter.recordUse(at: date("2026-11-02T03:00:00Z")) + try counter.recordUse(at: date("2026-11-03T03:00:00Z")) + try counter.recordUse(at: date("2026-11-20T03:00:00Z")) + + #expect(counter.days == 3) + } + + @Test("The day turns at local midnight, not at UTC midnight") + func dayTurnsAtLocalMidnight() throws { + let suite = try EphemeralDefaults() + let counter = UsageDayCounter(defaults: suite.defaults, calendar: calendar) + + // 23:59 and 00:01 local, both on 2 November in UTC. + try counter.recordUse(at: date("2026-11-02T16:59:00Z")) + try counter.recordUse(at: date("2026-11-02T17:01:00Z")) + + #expect(counter.days == 2) + } + + @Test("The count is read back by a new counter on the same defaults") + func countPersists() throws { + let suite = try EphemeralDefaults() + let first = UsageDayCounter(defaults: suite.defaults, calendar: calendar) + try first.recordUse(at: date("2026-11-02T03:00:00Z")) + try first.recordUse(at: date("2026-11-03T03:00:00Z")) + + let second = UsageDayCounter(defaults: suite.defaults, calendar: calendar) + try second.recordUse(at: date("2026-11-03T08:00:00Z")) + + #expect(second.days == 2) + } +} diff --git a/apple/changelog.d/3922.added.md b/apple/changelog.d/3922.added.md new file mode 100644 index 0000000000..aa1b7aa7e4 --- /dev/null +++ b/apple/changelog.d/3922.added.md @@ -0,0 +1 @@ +After 30 days of use without a license, a sheet asks once whether you would buy one, then at most every 90 days. From 99e1653f8e9975c8d1fd188a0c7624e255d7e807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sat, 31 Oct 2026 04:21:52 +0700 Subject: [PATCH 31/85] feat(about): thank license holders by name in the About panel (#3923) --- .../Core/Menu/AboutPanelCredits.swift | 79 +++++++++++++++++++ .../Menu/AppDelegate+MainMenuActions.swift | 32 -------- .../TablePro/Resources/Localizable.xcstrings | 40 ++++++++++ apple/TablePro/Resources/Supporters.json | 3 + .../Core/Menu/AboutPanelCreditsTests.swift | 44 +++++++++++ apple/changelog.d/3923.added.md | 1 + 6 files changed, 167 insertions(+), 32 deletions(-) create mode 100644 apple/TablePro/Core/Menu/AboutPanelCredits.swift create mode 100644 apple/TablePro/Resources/Supporters.json create mode 100644 apple/TableProTests/Core/Menu/AboutPanelCreditsTests.swift create mode 100644 apple/changelog.d/3923.added.md diff --git a/apple/TablePro/Core/Menu/AboutPanelCredits.swift b/apple/TablePro/Core/Menu/AboutPanelCredits.swift new file mode 100644 index 0000000000..8492b87d06 --- /dev/null +++ b/apple/TablePro/Core/Menu/AboutPanelCredits.swift @@ -0,0 +1,79 @@ +// +// AboutPanelCredits.swift +// TablePro +// + +import AppKit +import os + +/// The About panel's credits: the project links, then the license holders who asked to be thanked +/// by name. +enum AboutPanelCredits { + static func attributedString(supporters: [String] = SupporterList.load()) -> NSAttributedString { + let style: [NSAttributedString.Key: Any] = [ + .font: NSFont.systemFont(ofSize: 11), + .foregroundColor: NSColor.secondaryLabelColor + ] + let credits = NSMutableAttributedString() + let links: [(String, String)] = [ + ("Website", MainMenuLink.website), + ("GitHub", MainMenuLink.repository), + (String(localized: "Documentation"), MainMenuLink.documentation), + (String(localized: "Sponsor"), SupportLinks.sponsors(.aboutPanel).absoluteString) + ] + for (index, link) in links.enumerated() { + if index > 0 { + credits.append(NSAttributedString(string: " | ", attributes: style)) + } + let attributed = NSMutableAttributedString(string: link.0, attributes: style) + if let url = URL(string: link.1) { + attributed.addAttribute(.link, value: url, range: NSRange(location: 0, length: attributed.length)) + } + credits.append(attributed) + } + + if !supporters.isEmpty { + let heading = String(localized: "Thanks to these license holders") + credits.append(NSAttributedString(string: "\n\n\(heading)\n", attributes: style)) + credits.append(NSAttributedString(string: supporters.joined(separator: ", "), attributes: style)) + } + + let centered = NSMutableParagraphStyle() + centered.alignment = .center + credits.addAttribute(.paragraphStyle, value: centered, range: NSRange(location: 0, length: credits.length)) + return credits + } +} + +/// License holders who asked, by email, to be named in the About panel. +/// +/// `Supporters.json` in the app's resources is the only copy of the list. A name goes in, or comes +/// out, through a pull request, and only after its owner said yes, using the spelling they gave. +enum SupporterList { + static let resourceName = "Supporters" + + private struct File: Decodable { + let supporters: [String] + } + + private static let logger = Logger(subsystem: "com.TablePro", category: "SupporterList") + + static func load(from bundle: Bundle = .main) -> [String] { + guard let url = bundle.url(forResource: resourceName, withExtension: "json") else { + logger.error("Supporters.json is missing from the app bundle") + return [] + } + do { + return try decode(Data(contentsOf: url)) + } catch { + logger.error("Could not read Supporters.json: \(error.publicLogShape, privacy: .public)") + return [] + } + } + + static func decode(_ data: Data) throws -> [String] { + try JSONDecoder().decode(File.self, from: data).supporters + .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } + .filter { !$0.isEmpty } + } +} diff --git a/apple/TablePro/Core/Menu/AppDelegate+MainMenuActions.swift b/apple/TablePro/Core/Menu/AppDelegate+MainMenuActions.swift index 352d9e93a3..7f81715475 100644 --- a/apple/TablePro/Core/Menu/AppDelegate+MainMenuActions.swift +++ b/apple/TablePro/Core/Menu/AppDelegate+MainMenuActions.swift @@ -156,35 +156,3 @@ enum MainMenuLink { static let repository = "https://github.com/TableProApp/TablePro" static let issues = "https://github.com/TableProApp/TablePro/issues" } - -/// The About panel's credits line, kept out of the delegate so the menu action stays -/// a single call. -enum AboutPanelCredits { - static func attributedString() -> NSAttributedString { - let style: [NSAttributedString.Key: Any] = [ - .font: NSFont.systemFont(ofSize: 11), - .foregroundColor: NSColor.secondaryLabelColor - ] - let credits = NSMutableAttributedString() - let links: [(String, String)] = [ - ("Website", MainMenuLink.website), - ("GitHub", MainMenuLink.repository), - (String(localized: "Documentation"), MainMenuLink.documentation), - (String(localized: "Sponsor"), SupportLinks.sponsors(.aboutPanel).absoluteString) - ] - for (index, link) in links.enumerated() { - if index > 0 { - credits.append(NSAttributedString(string: " | ", attributes: style)) - } - let attributed = NSMutableAttributedString(string: link.0, attributes: style) - if let url = URL(string: link.1) { - attributed.addAttribute(.link, value: url, range: NSRange(location: 0, length: attributed.length)) - } - credits.append(attributed) - } - let centered = NSMutableParagraphStyle() - centered.alignment = .center - credits.addAttribute(.paragraphStyle, value: centered, range: NSRange(location: 0, length: credits.length)) - return credits - } -} diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 4a395d5fb4..6be3a1889a 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -182083,6 +182083,46 @@ } } }, + "Thanks to these license holders" : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Merci à ces détenteurs de licence" + } + }, + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "라이선스를 구매해 주신 분들께 감사드립니다" + } + }, + "tr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Bu lisans sahiplerine teşekkürler" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Cảm ơn những người đã mua giấy phép" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "感谢这些许可证持有者" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "感謝這些授權持有者" + } + } + } + }, "Thanks. Your license is what pays for the next release." : { "localizations" : { "fr" : { diff --git a/apple/TablePro/Resources/Supporters.json b/apple/TablePro/Resources/Supporters.json new file mode 100644 index 0000000000..75d509d60e --- /dev/null +++ b/apple/TablePro/Resources/Supporters.json @@ -0,0 +1,3 @@ +{ + "supporters": [] +} diff --git a/apple/TableProTests/Core/Menu/AboutPanelCreditsTests.swift b/apple/TableProTests/Core/Menu/AboutPanelCreditsTests.swift new file mode 100644 index 0000000000..0dbdad099f --- /dev/null +++ b/apple/TableProTests/Core/Menu/AboutPanelCreditsTests.swift @@ -0,0 +1,44 @@ +// +// AboutPanelCreditsTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct AboutPanelCreditsTests { + @Test("Named license holders follow the project links") + func listsSupporters() throws { + let credits = AboutPanelCredits.attributedString(supporters: ["Ada Lovelace", "Grace Hopper"]).string + + #expect(credits.contains("Thanks to these license holders")) + #expect(credits.contains("Ada Lovelace, Grace Hopper")) + let links = try #require(credits.range(of: "GitHub")) + let names = try #require(credits.range(of: "Ada Lovelace")) + #expect(links.lowerBound < names.lowerBound) + } + + @Test("With nobody listed, the credits stay the project links alone") + func emptyListAddsNothing() { + let credits = AboutPanelCredits.attributedString(supporters: []).string + + #expect(!credits.contains("Thanks to these license holders")) + #expect(!credits.contains("\n")) + } + + @Test("Blank entries and stray spaces in the list never reach the panel") + func decodeTrimsAndDropsBlanks() throws { + let data = Data(#"{"supporters": [" Ada Lovelace ", "", " ", "Grace Hopper"]}"#.utf8) + + #expect(try SupporterList.decode(data) == ["Ada Lovelace", "Grace Hopper"]) + } + + @Test("The list the app ships decodes") + func bundledListDecodes() throws { + let url = try #require(Bundle.main.url(forResource: SupporterList.resourceName, withExtension: "json")) + + _ = try SupporterList.decode(Data(contentsOf: url)) + } +} diff --git a/apple/changelog.d/3923.added.md b/apple/changelog.d/3923.added.md new file mode 100644 index 0000000000..09d17d7b1c --- /dev/null +++ b/apple/changelog.d/3923.added.md @@ -0,0 +1 @@ +The About panel thanks, by name, license holders who asked to be listed. From 3933e08bd061e118e204016d668314531a9b7a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sat, 31 Oct 2026 11:26:14 +0700 Subject: [PATCH 32/85] feat(results): show two or more result sets as tabs above the grid (#3924) --- .../QueryExecutionCoordinator+Batches.swift | 13 +- .../QueryExecutionCoordinator+Helpers.swift | 2 +- ...QueryExecutionCoordinator+Parameters.swift | 11 +- ...inSplitViewController+MenuValidation.swift | 20 ++- .../Query/QueryResultPresentation.swift | 30 ++-- .../Models/Query/QueryTab+Errors.swift | 34 ++++ .../TablePro/Models/Query/QueryTabState.swift | 11 +- apple/TablePro/Models/Query/ResultSet.swift | 2 + .../Models/Query/ResultSetMenuModel.swift | 74 +++++---- .../Models/Query/StatementAnchor.swift | 4 +- .../TablePro/Resources/Localizable.xcstrings | 9 ++ .../Main/Child/MainEditorContentView.swift | 57 ++++--- ...ainContentCoordinator+AIQueryActions.swift | 4 +- .../MainContentCoordinator+Alerts.swift | 4 + ...nContentCoordinator+ResultComparison.swift | 13 +- ...ainContentCoordinator+SidebarActions.swift | 118 +++++++++++--- .../Main/MainContentCommandActions.swift | 40 +++-- .../Views/Main/MainContentCoordinator.swift | 2 + .../Views/Results/InlineErrorBanner.swift | 15 +- .../Views/Results/ResultSetMenu.swift | 35 ++-- .../Views/Results/ResultSetTabBar.swift | 119 ++++++++++++++ .../Views/Results/ResultStatusBar.swift | 13 +- .../Autocomplete/SQLSchemaProviderTests.swift | 5 + .../Query/QueryResultPresentationTests.swift | 75 +++++++++ .../Query/ResultSetMenuModelTests.swift | 53 ++++++- .../Models/Query/ResultSetPolicyTests.swift | 7 + .../Models/Query/StatementAnchorTests.swift | 10 ++ .../Views/Main/ResultPinningTests.swift | 150 ++++++++++++++++++ .../Main/ResultStatusBarLayoutTests.swift | 12 +- .../Views/Main/ScriptErrorResultTests.swift | 57 +++++++ .../Results/ResultSetTabBarLayoutTests.swift | 140 ++++++++++++++++ apple/TableProUITests/QueryRunUITests.swift | 10 ++ .../Support/UITestCase+EditorTabs.swift | 12 ++ apple/changelog.d/3924.added.md | 1 + apple/changelog.d/3924.fixed.1.md | 1 + apple/changelog.d/3924.fixed.2.md | 1 + apple/changelog.d/3924.fixed.3.md | 1 + apple/changelog.d/3924.fixed.4.md | 1 + apple/changelog.d/3924.fixed.5.md | 1 + apple/changelog.d/3924.fixed.6.md | 1 + apple/changelog.d/3924.fixed.md | 1 + 41 files changed, 1004 insertions(+), 165 deletions(-) create mode 100644 apple/TablePro/Models/Query/QueryTab+Errors.swift create mode 100644 apple/TablePro/Views/Results/ResultSetTabBar.swift create mode 100644 apple/TableProTests/Views/Main/ScriptErrorResultTests.swift create mode 100644 apple/TableProTests/Views/Results/ResultSetTabBarLayoutTests.swift create mode 100644 apple/changelog.d/3924.added.md create mode 100644 apple/changelog.d/3924.fixed.1.md create mode 100644 apple/changelog.d/3924.fixed.2.md create mode 100644 apple/changelog.d/3924.fixed.3.md create mode 100644 apple/changelog.d/3924.fixed.4.md create mode 100644 apple/changelog.d/3924.fixed.5.md create mode 100644 apple/changelog.d/3924.fixed.6.md create mode 100644 apple/changelog.d/3924.fixed.md diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift index d29e638be4..9b13914ee1 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift @@ -414,6 +414,7 @@ extension QueryExecutionCoordinator { let errorResult = ResultSet(label: report.resultLabel) errorResult.errorMessage = message + errorResult.failedQuery = report.failedSQL errorResult.statementAnchor = failedBatch?.batch.anchor guard parent.tabExecution.settle(claim) else { return } @@ -431,8 +432,9 @@ extension QueryExecutionCoordinator { let timing = PluginQueryTiming(total: outputs.reduce(0) { $0 + $1.executionTime }) parent.flushBufferToActiveResult(tabId: tabId, pinnedOnly: true) parent.tabManager.mutate(tabId: tabId) { tab in - tab.execution.errorMessage = message - tab.execution.errorQuery = report.failedSQL + // The Error result carries the failure, so the banner stays on it rather than over every other result. + tab.execution.errorMessage = nil + tab.execution.errorQuery = nil tab.execution.executionTime = timing.total tab.execution.lastExecutedAt = Date() tab.display.replaceUnpinnedResults( @@ -491,6 +493,7 @@ extension QueryExecutionCoordinator { ) -> [ResultSet] { let databaseType = parent.connection.type let grammar = parent.lexicalGrammar + let firstNumber = parent.tabManager.tabs.first { $0.id == tabId }?.display.pinnedResultCount ?? 0 var resultSets: [ResultSet] = [] for (entry, output) in zip(prepared, outputs) { let isPlainQuery = { (sql: String) in @@ -510,7 +513,7 @@ extension QueryExecutionCoordinator { return makeStatementResultSet( result: result, sql: statement.sql, - index: resultSets.count, + index: firstNumber + resultSets.count, baseQuery: replayable ? statement.sql : nil, tabId: tabId, anchor: StatementAnchor(statement) @@ -518,11 +521,11 @@ extension QueryExecutionCoordinator { } } else if output.resultSets.isEmpty { produced = output.errors.isEmpty - ? [countResultSet(output, anchor: entry.batch.anchor, index: resultSets.count)] + ? [countResultSet(output, anchor: entry.batch.anchor, index: firstNumber + resultSets.count)] : [] } else { produced = output.resultSets.enumerated().map { offset, result in - numberedResultSet(result, anchor: entry.batch.anchor, index: resultSets.count + offset) + numberedResultSet(result, anchor: entry.batch.anchor, index: firstNumber + resultSets.count + offset) } } produced.last?.serverOutput = output.serverOutput diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift index 241ff38ad0..7ed1294821 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift @@ -284,7 +284,7 @@ extension QueryExecutionCoordinator { } let rs = ResultSet( - label: ResultSet.label(tableName: tableName, anchor: anchor, index: 0), + label: ResultSet.label(tableName: tableName, anchor: anchor, index: tab.display.pinnedResultCount), tableRows: newTableRows ) rs.statementAnchor = anchor diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift index f861379ac9..dc863a5f69 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift @@ -589,12 +589,13 @@ extension QueryExecutionCoordinator { } private func statementResultSets(_ history: ExecutedStatementHistory, tabId: UUID) -> [ResultSet] { - zip(history.prepared, history.results).enumerated().map { index, pair in + let firstNumber = parent.tabManager.tabs.first { $0.id == tabId }?.display.pinnedResultCount ?? 0 + return zip(history.prepared, history.results).enumerated().map { index, pair in let (statement, result) = pair return makeStatementResultSet( result: result, sql: statement.originalSQL, - index: index, + index: firstNumber + index, baseQuery: statement.executableSQL, baseQueryParameterValues: statement.parameterValues?.map { $0 as? String }, namedParameterStatement: statement.parameterValues == nil @@ -726,6 +727,7 @@ extension QueryExecutionCoordinator { let errorRS = ResultSet(label: report.resultLabel) errorRS.errorMessage = contextMsg + errorRS.failedQuery = report.failedSQL errorRS.statementAnchor = report.failedStatementIndex .flatMap { statements.indices.contains($0) ? statements[$0] : nil } .map(StatementAnchor.init) @@ -746,8 +748,9 @@ extension QueryExecutionCoordinator { parent.flushBufferToActiveResult(tabId: tabId, pinnedOnly: true) parent.tabManager.mutate(tabId: tabId) { tab in - tab.execution.errorMessage = contextMsg - tab.execution.errorQuery = failedStatementSQL + // The Error result carries the failure, so the banner stays on it rather than over every other result. + tab.execution.errorMessage = nil + tab.execution.errorQuery = nil tab.execution.executionTime = cumulativeTime tab.execution.lastExecutedAt = Date() diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift index ffed17eda9..f480747647 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift @@ -80,6 +80,9 @@ struct MenuValidationContext: Equatable { var hasOtherWindowTabs = false var canPinResultTab = false var canCompareResult = false + var canShowPreviousResult = false + var canShowNextResult = false + var canCloseResultTab = false // Separate flags: Back and Forward run out independently and a disabled item has to say which one it is. var canNavigateBack = false var canNavigateForward = false @@ -154,9 +157,6 @@ extension MainSplitViewController: NSMenuItemValidation { #selector(refreshDatabase(_:)), #selector(openQuickSwitcher(_:)), #selector(toggleQueryHistory(_:)), - #selector(showPreviousResult(_:)), - #selector(showNextResult(_:)), - #selector(closeResultTab(_:)), #selector(focusSidebarFilter(_:)), #selector(showERDiagram(_:)): return context.isConnected @@ -296,6 +296,12 @@ extension MainSplitViewController: NSMenuItemValidation { return context.canPinResultTab case #selector(compareResult(_:)): return context.canCompareResult + case #selector(showPreviousResult(_:)): + return context.canShowPreviousResult + case #selector(showNextResult(_:)): + return context.canShowNextResult + case #selector(closeResultTab(_:)): + return context.canCloseResultTab case #selector(navigateBack(_:)): return context.isConnected && context.canNavigateBack case #selector(navigateForward(_:)): @@ -461,6 +467,11 @@ extension MainSplitViewController: NSMenuItemValidation { #selector(moveTabToConnection(_:)), #selector(searchDatabase(_:)), #selector(buildJoinQuery(_:)), + #selector(pinResult(_:)), + #selector(compareResult(_:)), + #selector(showPreviousResult(_:)), + #selector(showNextResult(_:)), + #selector(closeResultTab(_:)), ] // A command this window does not own stays enabled, which keeps `performClose:` and the system's own items working. @@ -614,6 +625,9 @@ extension MainSplitViewController: NSMenuItemValidation { hasOtherWindowTabs: hasOtherWindowTabs, canPinResultTab: actions.canPinResultTab, canCompareResult: actions.canCompareResult, + canShowPreviousResult: actions.canShowPreviousResult, + canShowNextResult: actions.canShowNextResult, + canCloseResultTab: actions.canCloseResultTab, canNavigateBack: actions.canNavigateBack, canNavigateForward: actions.canNavigateForward, canNavigatePages: actions.canNavigatePages, diff --git a/apple/TablePro/Models/Query/QueryResultPresentation.swift b/apple/TablePro/Models/Query/QueryResultPresentation.swift index d960d8b15d..89c93b7d56 100644 --- a/apple/TablePro/Models/Query/QueryResultPresentation.swift +++ b/apple/TablePro/Models/Query/QueryResultPresentation.swift @@ -32,6 +32,13 @@ enum QueryResultContent: Equatable { case unavailable(mode: ResultsViewMode) } +// Where the result sets are chosen: the bar's pull-down for one, a tab row above the result for more. +enum ResultSetChooserPlacement: Equatable { + case hidden + case statusBar + case tabRow +} + // A plain struct, not a `QueryTab`, so tests reach every state without a tab manager, coordinator or session registry. struct QueryResultInputs: Equatable { var tabType: TabType = .query @@ -63,23 +70,25 @@ struct QueryResultInputs: Equatable { // Pure: every branch is decidable from `QueryResultInputs` alone, like `ResultStatusModel` and `ResultSetPolicy`. struct QueryResultPresentation: Equatable { let content: QueryResultContent - let showsResultSetSelector: Bool + let resultSetChooser: ResultSetChooserPlacement let showsFilterChrome: Bool // Always: a plan without the bar can't be switched away from or pinned. Its row readout hides via `StatusBarSnapshot.isQueryPlan`. let showsStatusBar: Bool let showsErrorBanner: Bool + var showsResultSetSelector: Bool { resultSetChooser != .hidden } + init(inputs: QueryResultInputs) { content = Self.resolveContent(inputs) - showsResultSetSelector = Self.resolvesResultSetSelector(inputs) + resultSetChooser = Self.resolveResultSetChooser(inputs) showsFilterChrome = Self.resolvesFilterChrome(inputs) showsStatusBar = true showsErrorBanner = Self.resolvedError(inputs) != nil } - // The active result's message wins since it describes the result on screen; the tab's covers a failure with no result set. + // Same order as `QueryTab.shownError`: the tab's message is the newer failure. static func resolvedError(_ inputs: QueryResultInputs) -> String? { - inputs.activeResultErrorMessage ?? inputs.executionErrorMessage + inputs.executionErrorMessage ?? inputs.activeResultErrorMessage } private static func resolveContent(_ inputs: QueryResultInputs) -> QueryResultContent { @@ -167,11 +176,14 @@ struct QueryResultPresentation: Equatable { return .noRows(executionTime: inputs.activeResultExecutionTime ?? inputs.executionTime) } - // Shown even for one result: Pin, Unpin and Close live in its menu, and it costs no height inside the status bar. - private static func resolvesResultSetSelector(_ inputs: QueryResultInputs) -> Bool { - guard inputs.tabType == .query else { return false } - guard inputs.viewMode != .structure else { return false } - return inputs.resultSetCount > 0 + // One result still gets the pull-down: Pin, Unpin and Close live in its menu, and it costs no height inside the bar. + private static func resolveResultSetChooser(_ inputs: QueryResultInputs) -> ResultSetChooserPlacement { + guard inputs.tabType == .query, inputs.viewMode != .structure else { return .hidden } + switch inputs.resultSetCount { + case 0: return .hidden + case 1: return .statusBar + default: return .tabRow + } } private static func resolvesFilterChrome(_ inputs: QueryResultInputs) -> Bool { diff --git a/apple/TablePro/Models/Query/QueryTab+Errors.swift b/apple/TablePro/Models/Query/QueryTab+Errors.swift new file mode 100644 index 0000000000..46d0027656 --- /dev/null +++ b/apple/TablePro/Models/Query/QueryTab+Errors.swift @@ -0,0 +1,34 @@ +// +// QueryTab+Errors.swift +// TablePro +// + +import Foundation + +struct QueryTabError: Equatable { + let message: String + let query: String? + // Only the tab's own message can be dismissed: an Error result's message is that result. + let isDismissible: Bool +} + +extension QueryTab { + // The tab's message is the newer failure, so it shows ahead of the one the active result recorded when it ran. + @MainActor + var shownError: QueryTabError? { + if let message = execution.errorMessage { + return QueryTabError(message: message, query: execution.errorQuery, isDismissible: true) + } + guard let active = display.activeResultSet, let message = active.errorMessage else { return nil } + return QueryTabError(message: message, query: active.failedQuery, isDismissible: false) + } + + // What Fix with AI works from while another result is on screen: the failure still belongs to this tab. + @MainActor + var latestError: QueryTabError? { + if let shownError { return shownError } + guard let failed = display.resultSets.last(where: { $0.errorMessage != nil }), + let message = failed.errorMessage else { return nil } + return QueryTabError(message: message, query: failed.failedQuery, isDismissible: false) + } +} diff --git a/apple/TablePro/Models/Query/QueryTabState.swift b/apple/TablePro/Models/Query/QueryTabState.swift index 76f2957417..731d95864a 100644 --- a/apple/TablePro/Models/Query/QueryTabState.swift +++ b/apple/TablePro/Models/Query/QueryTabState.swift @@ -668,10 +668,17 @@ struct TabDisplayState: Equatable { leaveOutputModeWithoutOutput() } + // New results join after the pinned ones, so a counted name starts there and never repeats a pinned one. @MainActor - mutating func removeUnpinnedResults() { + var pinnedResultCount: Int { + resultSets.filter(\.isPinned).count + } + + @MainActor + mutating func removeUnpinnedResults(keepingActive: Bool = false) { + let activeId = activeResultSet?.id resultSets = resultSets.filter { $0.isPinned } - activeResultSetId = resultSets.last?.id + activeResultSetId = keepingActive && resultSets.contains { $0.id == activeId } ? activeId : resultSets.last?.id leaveOutputModeWithoutOutput() } diff --git a/apple/TablePro/Models/Query/ResultSet.swift b/apple/TablePro/Models/Query/ResultSet.swift index 3b8fa4bdef..96319f8afa 100644 --- a/apple/TablePro/Models/Query/ResultSet.swift +++ b/apple/TablePro/Models/Query/ResultSet.swift @@ -31,6 +31,8 @@ final class ResultSet: ObservableObject, Identifiable { @Published var executionTime: TimeInterval? @Published var rowsAffected: Int = 0 @Published var errorMessage: String? + // The statement an Error result reports on, so Fix with AI sends that statement rather than the whole script. + @Published var failedQuery: String? @Published var statusMessage: String? /// What the statement printed on the server, such as Oracle's `DBMS_OUTPUT`, read right after it ran. @Published var serverOutput: PluginServerOutput = .none diff --git a/apple/TablePro/Models/Query/ResultSetMenuModel.swift b/apple/TablePro/Models/Query/ResultSetMenuModel.swift index a8d74cc890..65399f008e 100644 --- a/apple/TablePro/Models/Query/ResultSetMenuModel.swift +++ b/apple/TablePro/Models/Query/ResultSetMenuModel.swift @@ -16,52 +16,63 @@ struct ResultSetMenuEntry: Equatable, Identifiable { let isComparable: Bool } -// A pull-down rather than tabs: macOS has no native tab control that closes or pins anything but windows. +// Backs both the tab row and the pull-down, so the two never disagree on names or on what can close. struct ResultSetMenuModel: Equatable { let entries: [ResultSetMenuEntry] let activeOrdinal: Int let total: Int + // HIG, Tab views: "Avoid providing more than six tabs". Past that the pull-down alone lists them. + static let maximumTabCount = 6 + var isEmpty: Bool { entries.isEmpty } - /// The button's own words. A closed menu hides the count that the strip showed at a glance, so - /// the title carries it: this is the whole mitigation for the one thing the strip did better. + var offersTabs: Bool { + (2 ... Self.maximumTabCount).contains(total) + } + + // A position, not a name: a counted name such as "Result 3" keeps its number after an earlier result closes. var title: String { - guard total > 1 else { return entries.first?.label ?? "" } - return String( - format: String(localized: "Result %1$d of %2$d"), - activeOrdinal, - total - ) + guard total > 1 else { return entries.first.map(displayLabel(of:)) ?? "" } + return String(format: String(localized: "%1$d of %2$d"), activeOrdinal, total) } - /// The same count in figures, for the tiers where the bar has no room for the sentence. The - /// chooser never leaves the bar entirely, because Pin and Close have no other one-click route. - /// - /// A single result is named rather than counted, and a result is named after its table or its - /// leading comment, so the name is as long as the identifier. The chooser is `fixedSize`, so an - /// unbounded name here sets the bar's own floor and pushes the grid out of a narrow pane. + // The chooser is `fixedSize`, so an unbounded name here would set the bar's floor and push the grid out of a narrow pane. var compactTitle: String { - guard total > 1 else { return Self.truncated(entries.first?.label ?? "") } + guard total > 1 else { return Self.truncated(title, limit: Self.compactTitleCharacterLimit) } return "\(activeOrdinal)/\(total)" } - /// Long enough to tell two results apart, short enough that no name decides the bar's width. - /// The full name stays on the menu entry and on the control's accessibility label. - private static func truncated(_ label: String) -> String { - let value = label as NSString - guard value.length > compactTitleCharacterLimit else { return label } - return value.substring(to: compactTitleCharacterLimit) + "…" + var activeEntry: ResultSetMenuEntry? { + entries.first { $0.isActive } } - private static let compactTitleCharacterLimit = 16 + func displayLabel(of entry: ResultSetMenuEntry) -> String { + guard let index = entries.firstIndex(where: { $0.id == entry.id }) else { return entry.label } + return Self.displayLabel(at: index, in: entries.map(\.label)) + } - var activeEntry: ResultSetMenuEntry? { - entries.first { $0.isActive } + // The name is cut before the occurrence is added, so "(2)" always survives. + func tabTitle(of entry: ResultSetMenuEntry) -> String { + guard let index = entries.firstIndex(where: { $0.id == entry.id }) else { return entry.label } + let labels = entries.map(\.label) + let name = Self.truncated(entry.label, limit: Self.tabTitleCharacterLimit) + guard let occurrence = Self.occurrence(at: index, in: labels) else { return name } + return String(format: String(localized: "%1$@ (%2$d)"), name, occurrence) } - /// Whether closing this entry is offered. A pinned result is pinned precisely so that nothing - /// takes it away, which the strip enforced by withholding its close button. + // Two results read from one table share a name; the later ones carry their occurrence so two never look alike. + static func displayLabel(at index: Int, in labels: [String]) -> String { + guard let occurrence = occurrence(at: index, in: labels) else { return labels[index] } + return String(format: String(localized: "%1$@ (%2$d)"), labels[index], occurrence) + } + + private static func occurrence(at index: Int, in labels: [String]) -> Int? { + let earlier = labels[.. 0 ? earlier + 1 : nil + } + + // A pinned result is pinned precisely so that nothing takes it away. func canClose(_ entry: ResultSetMenuEntry) -> Bool { !entry.isPinned } @@ -73,4 +84,13 @@ struct ResultSetMenuModel: Equatable { func canCompare(_ entry: ResultSetMenuEntry) -> Bool { entry.isComparable } + + // Counts Characters, so an emoji or a combining mark is never cut into a replacement character. + static func truncated(_ label: String, limit: Int) -> String { + guard label.count > limit else { return label } + return String(label.prefix(limit)).trimmingCharacters(in: .whitespaces) + "…" + } + + private static let compactTitleCharacterLimit = 16 + private static let tabTitleCharacterLimit = 24 } diff --git a/apple/TablePro/Models/Query/StatementAnchor.swift b/apple/TablePro/Models/Query/StatementAnchor.swift index ac67c32deb..d7e893029e 100644 --- a/apple/TablePro/Models/Query/StatementAnchor.swift +++ b/apple/TablePro/Models/Query/StatementAnchor.swift @@ -48,9 +48,7 @@ struct StatementAnchor: Equatable { let collapsed = source.split(whereSeparator: \.isWhitespace).joined(separator: " ") guard !collapsed.isEmpty else { return "" } - let text = collapsed as NSString - guard text.length > Self.labelLimit else { return collapsed } - return text.substring(to: Self.labelLimit).trimmingCharacters(in: .whitespaces) + "…" + return ResultSetMenuModel.truncated(collapsed, limit: Self.labelLimit) } private static let labelLimit = 28 diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 6be3a1889a..2400d04c20 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -39851,6 +39851,9 @@ } } } + }, + "Clearing the results will discard all unsaved changes." : { + }, "CLI Connection Name" : { "extractionState" : "manual", @@ -41495,6 +41498,9 @@ } } } + }, + "Closing this result will discard all unsaved changes." : { + }, "Cloud" : { "extractionState" : "manual", @@ -133071,6 +133077,9 @@ } } } + }, + "Pinned" : { + }, "Pipe" : { "localizations" : { diff --git a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift index e96674225f..1fdd1164aa 100644 --- a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -526,19 +526,20 @@ struct MainEditorContentView: View { @ViewBuilder private func executionErrorBanner(tab: QueryTab) -> some View { - if let error = tab.display.activeResultSet?.errorMessage ?? tab.execution.errorMessage { + if let error = tab.shownError { InlineErrorBanner( - message: error, + message: error.message, onFixWithAI: coordinator.aiQueryActionAvailability(for: tab).isVisible - ? { coordinator.fixErrorWithAI(query: tab.execution.errorQuery ?? tab.content.query, error: error) } + ? { coordinator.fixErrorWithAI(query: error.query ?? tab.content.query, error: error.message) } : nil, - onDismiss: { - tabManager.mutate(tabId: tab.id) { - $0.display.activeResultSet?.errorMessage = nil - $0.execution.errorMessage = nil - $0.execution.errorQuery = nil + onDismiss: error.isDismissible + ? { + tabManager.mutate(tabId: tab.id) { + $0.execution.errorMessage = nil + $0.execution.errorQuery = nil + } } - } + : nil ) Divider() } @@ -606,6 +607,13 @@ struct MainEditorContentView: View { let presentation = resultPresentation(for: tab, rows: rows) VStack(spacing: 0) { + if presentation.resultSetChooser == .tabRow { + ResultSetTabBar( + model: resultSetMenuModel(for: tab, placedIn: .tabRow), + actions: resultSetActions(for: tab) + ) + } + if presentation.showsErrorBanner { executionErrorBanner(tab: tab) } @@ -938,28 +946,35 @@ struct MainEditorContentView: View { selectionSummary: coordinator.selectionSummary, isRefreshingSchema: schemaService.isRefreshing(connectionId: connectionId), viewMode: resultsViewModeBinding(for: tab), - resultSetMenu: resultSetMenuModel(for: tab), - onActivateResultSet: { coordinator.switchActiveResultSet(to: $0, in: tab.id) }, - onToggleResultSetPin: { coordinator.togglePinResultSet(id: $0) }, - onCompareResultSet: { coordinator.compareResultSet(id: $0) }, - onCloseResultSet: { coordinator.closeResultSet(id: $0) }, - onCloseOtherResultSets: { keptId in - for other in tab.display.resultSets where other.id != keptId && !other.isPinned { - coordinator.closeResultSet(id: other.id) - } - }, + resultSetMenu: resultSetMenuModel(for: tab, placedIn: .statusBar), + resultSetActions: resultSetActions(for: tab), onToggleFilters: { coordinator.toggleFilterPanel() }, onFetchAll: { coordinator.fetchAllRows() } ) } + private func resultSetActions(for tab: QueryTab) -> ResultSetActions { + let tabId = tab.id + return ResultSetActions( + activate: { coordinator.switchActiveResultSet(to: $0, in: tabId) }, + togglePin: { coordinator.togglePinResultSet(id: $0) }, + compare: { coordinator.compareResultSet(id: $0) }, + close: { coordinator.closeResultSet(id: $0) }, + closeOthers: { coordinator.closeOtherResultSets(keeping: $0) } + ) + } + // Only the three deciding fields, not a full `resultPresentation`, which would pull the whole row buffer for a row-free answer. - private func resultSetMenuModel(for tab: QueryTab) -> ResultSetMenuModel { + private func resultSetChooser(for tab: QueryTab) -> ResultSetChooserPlacement { var selectorInputs = QueryResultInputs() selectorInputs.tabType = tab.tabType selectorInputs.viewMode = tab.display.resultsViewMode selectorInputs.resultSetCount = tab.display.resultSets.count - guard QueryResultPresentation(inputs: selectorInputs).showsResultSetSelector else { + return QueryResultPresentation(inputs: selectorInputs).resultSetChooser + } + + private func resultSetMenuModel(for tab: QueryTab, placedIn placement: ResultSetChooserPlacement) -> ResultSetMenuModel { + guard resultSetChooser(for: tab) == placement else { return ResultSetMenuModel(entries: [], activeOrdinal: 0, total: 0) } let activeId = tab.display.activeResultSet?.id diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift index c9998b929f..98aa44d8da 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift @@ -43,8 +43,8 @@ extension MainContentCoordinator { target: AssistantEditorTarget( tabId: tab.id, scope: scope(for: tab), - errorMessage: tab.display.activeResultSet?.errorMessage ?? tab.execution.errorMessage, - errorQuery: tab.execution.errorQuery + errorMessage: tab.latestError?.message, + errorQuery: tab.latestError?.query ) ) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Alerts.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Alerts.swift index b99c3504b0..b3aacae28f 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Alerts.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Alerts.swift @@ -43,6 +43,10 @@ extension MainContentCoordinator { return String(localized: "Applying or clearing filters will reload data and discard all unsaved changes.") case .resultSwitch: return String(localized: "Showing another result will discard all unsaved changes.") + case .resultClose: + return String(localized: "Closing this result will discard all unsaved changes.") + case .clearResults: + return String(localized: "Clearing the results will discard all unsaved changes.") /// Worded for both directions, because Forward reaches the same guard. Naming Back here /// misstated the operation the user was approving whenever they stepped forward. case .navigation: diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultComparison.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultComparison.swift index e147e7bbb7..0a683dbc8a 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultComparison.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultComparison.swift @@ -41,8 +41,10 @@ extension MainContentCoordinator { return position > 0 ? ids[position - 1] : nil } - static func comparisonTitle(label: String, ordinal: Int) -> String { - String(format: String(localized: "Result %1$d: %2$@"), ordinal, label) + // The chooser's own name for the result, so a counted name never sits beside a different count. + static func comparisonTitle(of resultSet: ResultSet, in display: TabDisplayState) -> String { + guard let index = display.resultSets.firstIndex(where: { $0.id == resultSet.id }) else { return resultSet.label } + return ResultSetMenuModel.displayLabel(at: index, in: display.resultSets.map(\.label)) } // Every result of the tab the comparison starts from, and every pinned result of any other query tab. @@ -53,13 +55,13 @@ extension MainContentCoordinator { for tab in coordinator.tabManager.tabs where tab.tabType == .query { let isHome = tab.id == homeTabId let tabId = tab.id - for (index, resultSet) in tab.display.resultSets.enumerated() + for resultSet in tab.display.resultSets where resultSet.id != leftId && (isHome || resultSet.isPinned) && coordinator.isComparable(resultSet, in: tab) { let resultId = resultSet.id let candidate = ResultComparisonCandidate( id: resultId, - title: comparisonTitle(label: resultSet.label, ordinal: index + 1), + title: comparisonTitle(of: resultSet, in: tab.display), group: isHome ? String(localized: "This Tab") : coordinator.comparisonDetail(for: tab), isPinned: resultSet.isPinned, read: { [weak coordinator] in @@ -87,7 +89,6 @@ extension MainContentCoordinator { guard !resultSet.isExplainResult else { return nil } let rows = comparisonRows(of: resultSet, in: tab) guard !rows.columns.isEmpty else { return nil } - let ordinal = (tab.display.resultSets.firstIndex { $0.id == resultSet.id } ?? 0) + 1 let primaryKey: [String] if let origin = resultSet.origin, origin.keysResolved, origin.tableName?.isEmpty == false { primaryKey = origin.primaryKeyColumns @@ -95,7 +96,7 @@ extension MainContentCoordinator { primaryKey = [] } return ResultComparisonSide( - title: Self.comparisonTitle(label: resultSet.label, ordinal: ordinal), + title: Self.comparisonTitle(of: resultSet, in: tab.display), detail: comparisonDetail(for: tab), columns: rows.columns, columnTypes: rows.columnTypes, diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SidebarActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SidebarActions.swift index ae3635b24c..7b37d1a066 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SidebarActions.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SidebarActions.swift @@ -26,28 +26,82 @@ extension MainContentCoordinator { tabManager.mutate(at: tabIdx) { $0.display.togglePin(resultSetId: id) } } + // Closing and stepping need what pinning needs: a query tab showing a result, not the structure editor. + var canCloseActiveResultSet: Bool { + guard canPinActiveResultSet, let active = tabManager.selectedTab?.display.activeResultSet else { return false } + return !active.isPinned + } + + func canStepActiveResultSet(by offset: Int) -> Bool { + guard canPinActiveResultSet, let tab = tabManager.selectedTab, + let activeId = tab.display.activeResultSet?.id, + let index = tab.display.resultSets.firstIndex(where: { $0.id == activeId }) else { return false } + return tab.display.resultSets.indices.contains(index + offset) + } + + // A table tab's one result is its rows; closing it would blank the table until a reload. func closeResultSet(id: UUID) { - guard let tabIdx = tabManager.selectedTabIndex else { return } - let rs = tabManager.tabs[tabIdx].display.resultSets.first { $0.id == id } - guard rs?.isPinned != true else { return } - let tabId = tabManager.tabs[tabIdx].id - tabManager.mutate(at: tabIdx) { $0.display.resultSets.removeAll { $0.id == id } } - if tabManager.tabs[tabIdx].display.activeResultSetId == id { - let newActiveId = tabManager.tabs[tabIdx].display.resultSets.last?.id - applyResultSetSwitch(to: newActiveId, in: tabId) + guard let (tab, tabIdx) = tabManager.selectedTabAndIndex, tab.tabType == .query, + tab.display.resultSets.contains(where: { $0.id == id && !$0.isPinned }) else { return } + removeResultSets([id], at: tabIdx) { display in + // The result that slides into the closed one's place, as with editor tabs. + guard let index = display.resultSets.firstIndex(where: { $0.id == id }) else { return display.activeResultSet?.id } + let remaining = display.resultSets.filter { $0.id != id } + return remaining.indices.contains(index) ? remaining[index].id : remaining.last?.id } - if tabManager.tabs[tabIdx].display.resultSets.isEmpty { - setActiveTableRows(TableRows(), for: tabId) + } + + // One removal rather than a close per result, so the active result is replaced once and by the one kept. + func closeOtherResultSets(keeping keptId: UUID) { + guard let (tab, tabIdx) = tabManager.selectedTabAndIndex, tab.tabType == .query, + tab.display.resultSets.contains(where: { $0.id == keptId }) else { return } + let closing = Set(tab.display.resultSets.filter { $0.id != keptId && !$0.isPinned }.map(\.id)) + guard !closing.isEmpty else { return } + removeResultSets(closing, at: tabIdx) { _ in keptId } + } + + private func removeResultSets( + _ ids: Set, + at tabIdx: Int, + replacement: @escaping (TabDisplayState) -> UUID? + ) { + let tabId = tabManager.tabs[tabIdx].id + let activeId = tabManager.tabs[tabIdx].display.activeResultSet?.id + guard let activeId, ids.contains(activeId) else { tabManager.mutate(at: tabIdx) { tab in - tab.execution.errorMessage = nil - tab.execution.rowsAffected = 0 - tab.execution.executionTime = nil - tab.execution.statusMessage = nil - tab.schemaVersion += 1 - tab.display.isResultsCollapsed = true + tab.display.resultSets.removeAll { ids.contains($0.id) && !$0.isPinned } + tab.display.activeResultSetId = activeId } - toolbarState.isResultsCollapsed = true + return + } + // The active result's unsaved edits leave with it, so this asks first, as switching does. + confirmDiscardChangesIfNeeded(action: .resultClose) { [weak self] confirmed in + guard confirmed, let self, + let tabIdx = self.tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { return } + let replacementId = replacement(self.tabManager.tabs[tabIdx].display) + self.tabManager.mutate(at: tabIdx) { tab in + tab.display.resultSets.removeAll { ids.contains($0.id) && !$0.isPinned } + } + if self.tabManager.tabs[tabIdx].display.resultSets.isEmpty { + self.clearEmptiedResults(at: tabIdx) + } else { + self.applyResultSetSwitch(to: replacementId, in: tabId) + } + } + } + + private func clearEmptiedResults(at tabIdx: Int) { + setActiveTableRows(TableRows(), for: tabManager.tabs[tabIdx].id) + tabManager.mutate(at: tabIdx) { tab in + tab.display.activeResultSetId = nil + tab.execution.errorMessage = nil + tab.execution.rowsAffected = 0 + tab.execution.executionTime = nil + tab.execution.statusMessage = nil + tab.schemaVersion += 1 + tab.display.isResultsCollapsed = true } + toolbarState.isResultsCollapsed = true } var canClearActiveQueryResults: Bool { @@ -55,17 +109,31 @@ extension MainContentCoordinator { return !tabSessionRegistry.tableRows(for: tab.id).rows.isEmpty || tab.execution.lastExecutedAt != nil } + // A pinned result on screen stays; any other goes with its unsaved edits, so that case asks first. func clearActiveQueryResults() { - guard let tabIdx = tabManager.selectedTabIndex else { return } - let tabId = tabManager.tabs[tabIdx].id + guard let tab = tabManager.selectedTab else { return } + let tabId = tab.id + guard tab.display.activeResultSet?.isPinned != true else { + clearUnpinnedResults(in: tabId) + return + } + confirmDiscardChangesIfNeeded(action: .clearResults) { [weak self] confirmed in + if confirmed { self?.clearUnpinnedResults(in: tabId) } + } + } - if let lastPinned = tabManager.tabs[tabIdx].display.resultSets.last(where: \.isPinned) { - applyResultSetSwitch(to: lastPinned.id, in: tabId) + private func clearUnpinnedResults(in tabId: UUID) { + guard let tabIdx = tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { return } + let display = tabManager.tabs[tabIdx].display + + if let lastPinned = display.resultSets.last(where: \.isPinned) { + let keepsActive = display.activeResultSet?.isPinned == true + if !keepsActive { + applyResultSetSwitch(to: lastPinned.id, in: tabId) + } tabManager.mutate(at: tabIdx) { tab in - tab.display.removeUnpinnedResults() - /// A failed execution records its message on the tab rather than on a result set, - /// so returning here without clearing it left the banner over the pinned result the - /// switch just revealed, and only the banner's own Dismiss could take it away. + tab.display.removeUnpinnedResults(keepingActive: keepsActive) + // A failed run's message sits on the tab, and would otherwise stay over the pinned result. tab.execution.errorMessage = nil tab.execution.errorQuery = nil } diff --git a/apple/TablePro/Views/Main/MainContentCommandActions.swift b/apple/TablePro/Views/Main/MainContentCommandActions.swift index 5a3f62be3f..7622d46b33 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions.swift @@ -1231,23 +1231,27 @@ final class MainContentCommandActions: ObservableObject { } func previousResultTab() { - guard let coordinator, - let (tab, _) = coordinator.tabManager.selectedTabAndIndex else { return } - guard tab.display.resultSets.count > 1, - let currentId = tab.display.activeResultSetId ?? tab.display.resultSets.last?.id, - let currentIndex = tab.display.resultSets.firstIndex(where: { $0.id == currentId }), - currentIndex > 0 else { return } - coordinator.switchActiveResultSet(to: tab.display.resultSets[currentIndex - 1].id, in: tab.id) + stepResultTab(by: -1) } func nextResultTab() { - guard let coordinator, - let (tab, _) = coordinator.tabManager.selectedTabAndIndex else { return } - guard tab.display.resultSets.count > 1, - let currentId = tab.display.activeResultSetId ?? tab.display.resultSets.last?.id, - let currentIndex = tab.display.resultSets.firstIndex(where: { $0.id == currentId }), - currentIndex < tab.display.resultSets.count - 1 else { return } - coordinator.switchActiveResultSet(to: tab.display.resultSets[currentIndex + 1].id, in: tab.id) + stepResultTab(by: 1) + } + + var canShowPreviousResult: Bool { + coordinator?.canStepActiveResultSet(by: -1) ?? false + } + + var canShowNextResult: Bool { + coordinator?.canStepActiveResultSet(by: 1) ?? false + } + + private func stepResultTab(by offset: Int) { + guard let coordinator, coordinator.canStepActiveResultSet(by: offset), + let (tab, _) = coordinator.tabManager.selectedTabAndIndex, + let activeId = tab.display.activeResultSet?.id, + let index = tab.display.resultSets.firstIndex(where: { $0.id == activeId }) else { return } + coordinator.switchActiveResultSet(to: tab.display.resultSets[index + offset].id, in: tab.id) } var canPinResultTab: Bool { @@ -1272,10 +1276,12 @@ final class MainContentCommandActions: ObservableObject { coordinator?.compareActiveResult() } + var canCloseResultTab: Bool { + coordinator?.canCloseActiveResultSet ?? false + } + func closeResultTab() { - guard let coordinator else { return } - let tab = coordinator.tabManager.selectedTab - guard let activeId = tab?.display.activeResultSetId ?? tab?.display.resultSets.last?.id else { return } + guard let coordinator, let activeId = coordinator.tabManager.selectedTab?.display.activeResultSet?.id else { return } coordinator.closeResultSet(id: activeId) } diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index 1e4fd80455..4677384b43 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -20,6 +20,8 @@ enum DiscardAction { case pagination case filter case resultSwitch + case resultClose + case clearResults case navigation case displayOrder case columnVisibility diff --git a/apple/TablePro/Views/Results/InlineErrorBanner.swift b/apple/TablePro/Views/Results/InlineErrorBanner.swift index 510ea60822..c688976a06 100644 --- a/apple/TablePro/Views/Results/InlineErrorBanner.swift +++ b/apple/TablePro/Views/Results/InlineErrorBanner.swift @@ -14,9 +14,11 @@ struct InlineErrorBanner: View { var onDismiss: (() -> Void)? private let maxMessageHeight: CGFloat = 96 + private let minMessageHeight: CGFloat = 16 @State private var messageHeight: CGFloat = 0 + @State private var shownHeight: CGFloat = 0 - private var messageFits: Bool { messageHeight <= maxMessageHeight } + private var messageFits: Bool { messageHeight <= shownHeight + 0.5 } var body: some View { HStack(alignment: .top, spacing: 8) { @@ -34,7 +36,16 @@ struct InlineErrorBanner: View { messageHeight = height } } - .frame(height: min(messageHeight, maxMessageHeight)) + // Gives way down to one line in a short pane, so the bars above and below it stay inside the pane. + .frame( + minHeight: min(messageHeight, minMessageHeight), + maxHeight: min(messageHeight, maxMessageHeight) + ) + .onGeometryChange(for: CGFloat.self) { proxy in + proxy.size.height + } action: { height in + shownHeight = height + } .scrollDisabled(messageFits) .scrollBounceBasedOnSize() if let onFixWithAI { diff --git a/apple/TablePro/Views/Results/ResultSetMenu.swift b/apple/TablePro/Views/Results/ResultSetMenu.swift index 99b74fa10c..a14e2445d1 100644 --- a/apple/TablePro/Views/Results/ResultSetMenu.swift +++ b/apple/TablePro/Views/Results/ResultSetMenu.swift @@ -5,27 +5,32 @@ import SwiftUI -/// A pull-down in the status bar rather than a strip of its own, which spent 32pt in every query tab, even with one result. +struct ResultSetActions { + let activate: (UUID) -> Void + let togglePin: (UUID) -> Void + let compare: (UUID) -> Void + let close: (UUID) -> Void + let closeOthers: (UUID) -> Void + + static var none: ResultSetActions { + ResultSetActions(activate: { _ in }, togglePin: { _ in }, compare: { _ in }, close: { _ in }, closeOthers: { _ in }) + } +} + +// Lists every result and acts on the current one. A tab row cannot do the second: no stock control tells which segment was right-clicked. struct ResultSetMenu: View { let model: ResultSetMenuModel - /// Spelled out where the bar has room, counted in figures where it does not. let isSpelledOut: Bool - let onActivate: (UUID) -> Void - let onTogglePin: (UUID) -> Void - let onCompare: (UUID) -> Void - let onClose: (UUID) -> Void - let onCloseOthers: (UUID) -> Void + let actions: ResultSetActions var body: some View { Menu { ForEach(model.entries) { entry in Button { - onActivate(entry.id) + actions.activate(entry.id) } label: { - /// A checkmark on the active one and a pin glyph on the pinned ones, which is - /// the menu vocabulary for "this is current" and "this is held". Label { - Text(entry.label) + Text(model.displayLabel(of: entry)) } icon: { if entry.isActive { Image(systemName: "checkmark") @@ -43,16 +48,16 @@ struct ResultSetMenu: View { ? String(localized: "Unpin Result") : String(localized: "Pin Result") ) { - onTogglePin(active.id) + actions.togglePin(active.id) } - Button(String(localized: "Compare Result With…")) { onCompare(active.id) } + Button(String(localized: "Compare Result With…")) { actions.compare(active.id) } .disabled(!model.canCompare(active)) - Button(String(localized: "Close Result")) { onClose(active.id) } + Button(String(localized: "Close Result")) { actions.close(active.id) } .disabled(!model.canClose(active)) - Button(String(localized: "Close Other Results")) { onCloseOthers(active.id) } + Button(String(localized: "Close Other Results")) { actions.closeOthers(active.id) } .disabled(!model.canCloseOthers(active)) } } label: { diff --git a/apple/TablePro/Views/Results/ResultSetTabBar.swift b/apple/TablePro/Views/Results/ResultSetTabBar.swift new file mode 100644 index 0000000000..7950eb4b07 --- /dev/null +++ b/apple/TablePro/Views/Results/ResultSetTabBar.swift @@ -0,0 +1,119 @@ +// +// ResultSetTabBar.swift +// TablePro +// + +import AppKit +import SwiftUI + +// Two or more results: a tab per result for one-click switching, and the pull-down that lists them all and acts on the current one. +struct ResultSetTabBar: View { + let model: ResultSetMenuModel + let actions: ResultSetActions + + var body: some View { + VStack(spacing: 0) { + ViewThatFits(in: .horizontal) { + if model.offersTabs { + row(showsTabs: true, isSpelledOut: true) + row(showsTabs: true, isSpelledOut: false) + } + row(showsTabs: false, isSpelledOut: true) + row(showsTabs: false, isSpelledOut: false) + } + .padding(.horizontal, StatusBarChrome.horizontalPadding) + .frame(maxWidth: .infinity, alignment: .leading) + .frame(height: StatusBarChrome.height) + .paneBarBackground() + Divider() + } + } + + private func row(showsTabs: Bool, isSpelledOut: Bool) -> some View { + HStack(spacing: StatusBarChrome.clusterSpacing) { + if showsTabs { + // SwiftUI sets a hosted control's size from its environment, so it is set here and not in the control. + ResultSetTabs(model: model, onActivate: actions.activate) + .controlSize(.small) + } + Spacer(minLength: 0) + ResultSetMenu(model: model, isSpelledOut: isSpelledOut, actions: actions) + } + } +} + +struct ResultSetTabs: NSViewRepresentable { + let model: ResultSetMenuModel + let onActivate: (UUID) -> Void + + func makeNSView(context: Context) -> ResultSetTabsControl { + ResultSetTabsControl() + } + + func updateNSView(_ control: ResultSetTabsControl, context: Context) { + control.onActivate = onActivate + control.apply(model) + } + + // Always the width every tab needs, so `ViewThatFits` gives up the tabs rather than squeezing them. + func sizeThatFits(_ proposal: ProposedViewSize, nsView control: ResultSetTabsControl, context: Context) -> CGSize? { + control.intrinsicContentSize + } +} + +// AppKit on purpose: a SwiftUI segmented or `.tabs` picker drops a segment's image, and the pin is what a tab must show (measured on macOS 27). +@MainActor +final class ResultSetTabsControl: NSSegmentedControl { + var onActivate: ((UUID) -> Void)? + private var resultIds: [UUID] = [] + private var activeIndex = -1 + + init() { + super.init(frame: .zero) + trackingMode = .selectOne + #if SDK_MACOS_27 + if #available(macOS 27.0, *) { + role = .tabs + } + #endif + target = self + action = #selector(segmentChosen(_:)) + setAccessibilityLabel(String(localized: "Results")) + setAccessibilityIdentifier("result-set-tabs") + } + + @available(*, unavailable) + required init?(coder: NSCoder) { + fatalError("ResultSetTabsControl does not support NSCoder init") + } + + func apply(_ model: ResultSetMenuModel) { + if segmentCount != model.entries.count { + segmentCount = model.entries.count + } + for (index, entry) in model.entries.enumerated() { + let title = model.tabTitle(of: entry) + if label(forSegment: index) != title { + setLabel(title, forSegment: index) + } + setImage(entry.isPinned ? Self.pinImage : nil, forSegment: index) + setToolTip(model.displayLabel(of: entry), forSegment: index) + } + resultIds = model.entries.map(\.id) + activeIndex = model.entries.firstIndex(where: \.isActive) ?? -1 + selectedSegment = activeIndex + } + + @objc private func segmentChosen(_ sender: NSSegmentedControl) { + let chosen = selectedSegment + // The model decides which tab is lit, so a switch the discard prompt cancels leaves the current one selected. + selectedSegment = activeIndex + guard resultIds.indices.contains(chosen), chosen != activeIndex else { return } + onActivate?(resultIds[chosen]) + } + + private static let pinImage = NSImage( + systemSymbolName: "pin.fill", + accessibilityDescription: String(localized: "Pinned") + )?.withSymbolConfiguration(NSImage.SymbolConfiguration(scale: .small)) +} diff --git a/apple/TablePro/Views/Results/ResultStatusBar.swift b/apple/TablePro/Views/Results/ResultStatusBar.swift index 170346bdc0..46496d04f7 100644 --- a/apple/TablePro/Views/Results/ResultStatusBar.swift +++ b/apple/TablePro/Views/Results/ResultStatusBar.swift @@ -45,12 +45,9 @@ struct ResultStatusBar: View { /// this window is. It had no surface at all between the centred toolbar item going and this. let isRefreshingSchema: Bool @Binding var viewMode: ResultsViewMode + // Empty unless the tab holds exactly one result: two or more are chosen from the tab row above the result. let resultSetMenu: ResultSetMenuModel - let onActivateResultSet: (UUID) -> Void - let onToggleResultSetPin: (UUID) -> Void - let onCompareResultSet: (UUID) -> Void - let onCloseResultSet: (UUID) -> Void - let onCloseOtherResultSets: (UUID) -> Void + let resultSetActions: ResultSetActions let onToggleFilters: () -> Void let onFetchAll: (() -> Void)? @@ -111,11 +108,7 @@ struct ResultStatusBar: View { ResultSetMenu( model: resultSetMenu, isSpelledOut: presentation.resultSetMenuIsSpelledOut, - onActivate: onActivateResultSet, - onTogglePin: onToggleResultSetPin, - onCompare: onCompareResultSet, - onClose: onCloseResultSet, - onCloseOthers: onCloseOtherResultSets + actions: resultSetActions ) } if model.controls.showsReadout || model.controls.showsExecution { diff --git a/apple/TableProTests/Core/Autocomplete/SQLSchemaProviderTests.swift b/apple/TableProTests/Core/Autocomplete/SQLSchemaProviderTests.swift index 2856e120f5..ad9c23b09f 100644 --- a/apple/TableProTests/Core/Autocomplete/SQLSchemaProviderTests.swift +++ b/apple/TableProTests/Core/Autocomplete/SQLSchemaProviderTests.swift @@ -127,8 +127,12 @@ final class MockDatabaseDriver: DatabaseDriver, SchemaSwitchable, @unchecked Sen applyQueryTimeoutValues.append(seconds) } + // A statement holding this text fails, the way a server rejects one statement of a script. + var failsStatementContaining: (text: String, error: any Error)? + func execute(query: String) async throws -> QueryResult { executedQueries.append(query) + if let failure = failsStatementContaining, query.contains(failure.text) { throw failure.error } if executeDelaySeconds > 0 { try await Task.sleep(nanoseconds: UInt64(executeDelaySeconds * 1_000_000_000)) } @@ -179,6 +183,7 @@ final class MockDatabaseDriver: DatabaseDriver, SchemaSwitchable, @unchecked Sen } func executeUserQuery(query: String, rowCap: Int?, parameters: [Any?]?) async throws -> QueryResult { + if let failure = failsStatementContaining, query.contains(failure.text) { throw failure.error } if pausesNextUserQuery { pausesNextUserQuery = false await withCheckedContinuation { continuation in diff --git a/apple/TableProTests/Models/Query/QueryResultPresentationTests.swift b/apple/TableProTests/Models/Query/QueryResultPresentationTests.swift index 339f6a15d2..ee978180f7 100644 --- a/apple/TableProTests/Models/Query/QueryResultPresentationTests.swift +++ b/apple/TableProTests/Models/Query/QueryResultPresentationTests.swift @@ -297,6 +297,81 @@ struct QueryResultPresentationTests { #expect(QueryResultPresentation(inputs: inputs).showsResultSetSelector == false) } + @Test("One result is chosen from the bar, two or more from a tab row") + func chooserPlacementFollowsTheCount() { + var inputs = QueryResultInputs() + let placements = (0 ... 8).map { count -> ResultSetChooserPlacement in + inputs.resultSetCount = count + return QueryResultPresentation(inputs: inputs).resultSetChooser + } + + #expect(placements == [.hidden, .statusBar, .tabRow, .tabRow, .tabRow, .tabRow, .tabRow, .tabRow, .tabRow]) + } + + @Test("A table tab and the structure editor place no chooser whatever the count") + func chooserPlacementNeedsAQueryResult() { + var table = QueryResultInputs() + table.tabType = .table + table.resultSetCount = 3 + #expect(QueryResultPresentation(inputs: table).resultSetChooser == .hidden) + + var structure = QueryResultInputs() + structure.viewMode = .structure + structure.tableName = "orders" + structure.resultSetCount = 3 + #expect(QueryResultPresentation(inputs: structure).resultSetChooser == .hidden) + } + + // Dismissing the Error result's own message used to turn it into "Query executed successfully". + @Test("An Error result's banner cannot be dismissed, the tab's own message can") + @MainActor + func onlyTheTabsMessageIsDismissible() throws { + let failed = ResultSet(label: "Error") + failed.errorMessage = "relation \"nope\" does not exist" + failed.failedQuery = "SELECT * FROM nope" + var tab = QueryTab(title: "Query", query: "SELECT 1;\nSELECT * FROM nope;", tabType: .query) + tab.display.resultSets = [ResultSet(label: "Result 1"), failed] + tab.display.activeResultSetId = failed.id + + let resultError = try #require(tab.shownError) + #expect(resultError.message == "relation \"nope\" does not exist") + #expect(resultError.query == "SELECT * FROM nope") + #expect(resultError.isDismissible == false) + + tab.execution.errorMessage = "syntax error" + tab.execution.errorQuery = "SELEC 2" + let newer = try #require(tab.shownError) + #expect(newer.message == "syntax error") + #expect(newer.query == "SELEC 2") + #expect(newer.isDismissible) + } + + @Test("A sibling of an Error result shows no banner, and Fix with AI still finds the failure") + @MainActor + func siblingOfAnErrorResultHasNoBanner() throws { + let first = ResultSet(label: "Result 1") + let failed = ResultSet(label: "Error") + failed.errorMessage = "relation \"nope\" does not exist" + failed.failedQuery = "SELECT * FROM nope" + var tab = QueryTab(title: "Query", query: "SELECT 1;\nSELECT * FROM nope;", tabType: .query) + tab.display.resultSets = [first, failed] + tab.display.activeResultSetId = first.id + + #expect(tab.shownError == nil) + #expect(try #require(tab.latestError).query == "SELECT * FROM nope") + } + + @Test("The tab's newer message shows ahead of the active result's own") + func tabMessageShowsFirst() { + var inputs = QueryResultInputs() + inputs.hasExecuted = true + inputs.hasActiveResultSet = true + inputs.activeResultErrorMessage = "older" + inputs.executionErrorMessage = "newer" + + #expect(QueryResultPresentation.resolvedError(inputs) == "newer") + } + @Test("Filter chrome follows the mode that can be filtered") func filterChromeFollowsMode() { var inputs = QueryResultInputs() diff --git a/apple/TableProTests/Models/Query/ResultSetMenuModelTests.swift b/apple/TableProTests/Models/Query/ResultSetMenuModelTests.swift index 00192fe223..105d0d2c51 100644 --- a/apple/TableProTests/Models/Query/ResultSetMenuModelTests.swift +++ b/apple/TableProTests/Models/Query/ResultSetMenuModelTests.swift @@ -8,13 +8,12 @@ import Foundation import Testing struct ResultSetMenuModelTests { - /// The count is what the deleted strip showed at a glance and a closed menu cannot. Carrying it - /// in the button's own title is the whole mitigation, so it is worth a test. + // A position rather than a name: "Result 2 of 4" read as the name of a result counted "Result 3" after a close. @Test("The title counts the results when there is more than one") func titleCountsResults() { let model = Self.make(count: 4, activeIndex: 1) - #expect(model.title == "Result 2 of 4") + #expect(model.title == "2 of 4") } @Test("A single result is named rather than counted") @@ -81,6 +80,54 @@ struct ResultSetMenuModelTests { #expect(model.activeEntry == nil) } + @Test("Tabs are offered from two results up to six, the HIG's limit for a tab view") + func tabsNeedTwoToSixResults() { + #expect(Self.make(count: 1, activeIndex: 0).offersTabs == false) + #expect(Self.make(count: 2, activeIndex: 0).offersTabs) + #expect(Self.make(count: 6, activeIndex: 0).offersTabs) + #expect(Self.make(count: 7, activeIndex: 0).offersTabs == false) + } + + @Test("Two results from one table get names that tell them apart") + func duplicateLabelsAreNumbered() { + let model = Self.make(labels: ["users", "orders", "users", "users"], activeIndex: 0) + + #expect(model.entries.map(model.displayLabel(of:)) == ["users", "orders", "users (2)", "users (3)"]) + } + + @Test("A long name is cut on a whole character, never inside an emoji") + func truncationKeepsWholeCharacters() { + let label = "Monthly revenue per region 📈 for Q3" + let cut = ResultSetMenuModel.truncated(label, limit: 28) + + #expect(cut == "Monthly revenue per region 📈…") + #expect(cut.unicodeScalars.contains("\u{FFFD}") == false) + #expect(ResultSetMenuModel.truncated("orders", limit: 28) == "orders") + } + + @Test("A tab's title is the disambiguated name, cut to fit") + func tabTitleIsBounded() throws { + let model = Self.make(labels: ["a_table_name_long_enough_to_need_cutting"], activeIndex: 0) + let entry = try #require(model.entries.first) + + #expect(model.tabTitle(of: entry).count <= 25) + #expect(model.tabTitle(of: entry).hasSuffix("…")) + } + + private static func make(labels: [String], activeIndex: Int) -> ResultSetMenuModel { + let entries = labels.enumerated().map { index, label in + ResultSetMenuEntry( + id: UUID(), + label: label, + isPinned: false, + isActive: index == activeIndex, + ordinal: index + 1, + isComparable: true + ) + } + return ResultSetMenuModel(entries: entries, activeOrdinal: activeIndex + 1, total: labels.count) + } + private static func make( count: Int, activeIndex: Int, diff --git a/apple/TableProTests/Models/Query/ResultSetPolicyTests.swift b/apple/TableProTests/Models/Query/ResultSetPolicyTests.swift index e965e67729..d195e0a28d 100644 --- a/apple/TableProTests/Models/Query/ResultSetPolicyTests.swift +++ b/apple/TableProTests/Models/Query/ResultSetPolicyTests.swift @@ -99,6 +99,13 @@ struct ResultSetPolicyTests { display.resultsViewMode = mode states.append(display) + for count in 2 ... 8 { + var several = display + several.resultSets = (1 ... count).map { ResultSet(label: "Result \($0)") } + several.activeResultSetId = several.resultSets.first?.id + states.append(several) + } + var withoutResults = display withoutResults.resultSets = [] withoutResults.activeResultSetId = nil diff --git a/apple/TableProTests/Models/Query/StatementAnchorTests.swift b/apple/TableProTests/Models/Query/StatementAnchorTests.swift index 5cb22c377e..a8e39607ad 100644 --- a/apple/TableProTests/Models/Query/StatementAnchorTests.swift +++ b/apple/TableProTests/Models/Query/StatementAnchorTests.swift @@ -118,6 +118,16 @@ struct StatementAnchorTests { #expect(subject.label.count <= 29) } + @Test("A comment cut at the limit keeps an emoji whole") + func labelIsCutOnWholeCharacters() { + let subject = StatementAnchor( + range: NSRange(location: 0, length: 8), + preview: "-- Monthly revenue per region 📈 for Q3\nSELECT 1" + ) + #expect(subject.label == "Monthly revenue per region 📈…") + #expect(subject.label.unicodeScalars.contains("\u{FFFD}") == false) + } + // MARK: - What a result is called @Test("The table wins when there is one") diff --git a/apple/TableProTests/Views/Main/ResultPinningTests.swift b/apple/TableProTests/Views/Main/ResultPinningTests.swift index 05249d6bed..2d0552c968 100644 --- a/apple/TableProTests/Views/Main/ResultPinningTests.swift +++ b/apple/TableProTests/Views/Main/ResultPinningTests.swift @@ -141,6 +141,156 @@ struct ResultPinningTests { #expect(coordinator.tabManager.selectedTab?.display.resultSets.map(\.id) == [pinned.id]) } + @Test("Closing the active result selects the one that takes its place") + @MainActor + func closeSelectsTheNeighbour() throws { + let coordinator = Self.makeCoordinator() + defer { coordinator.teardown() } + let results = try Self.seed(coordinator, labels: ["a", "b", "c", "d"], activeIndex: 1) + + coordinator.closeResultSet(id: results[1].id) + + let display = try #require(coordinator.tabManager.selectedTab?.display) + #expect(display.resultSets.map(\.id) == [results[0].id, results[2].id, results[3].id]) + #expect(display.activeResultSet?.id == results[2].id) + } + + @Test("Closing the last result selects the one before it") + @MainActor + func closeLastSelectsThePrevious() throws { + let coordinator = Self.makeCoordinator() + defer { coordinator.teardown() } + let results = try Self.seed(coordinator, labels: ["a", "b", "c"], activeIndex: 2) + + coordinator.closeResultSet(id: results[2].id) + + #expect(coordinator.tabManager.selectedTab?.display.activeResultSet?.id == results[1].id) + } + + @Test("Closing a result that is not shown keeps the one that is") + @MainActor + func closeBackgroundResultKeepsActive() throws { + let coordinator = Self.makeCoordinator() + defer { coordinator.teardown() } + let results = try Self.seed(coordinator, labels: ["a", "b", "c"], activeIndex: 0) + + coordinator.closeResultSet(id: results[2].id) + + let display = try #require(coordinator.tabManager.selectedTab?.display) + #expect(display.resultSets.map(\.id) == [results[0].id, results[1].id]) + #expect(display.activeResultSet?.id == results[0].id) + } + + @Test("Close Others keeps the pinned results and shows the one it kept") + @MainActor + func closeOthersShowsTheKeptResult() throws { + let coordinator = Self.makeCoordinator() + defer { coordinator.teardown() } + let results = try Self.seed(coordinator, labels: ["a", "b", "c", "d"], activeIndex: 3, pinnedIndices: [0]) + + coordinator.closeOtherResultSets(keeping: results[1].id) + + let display = try #require(coordinator.tabManager.selectedTab?.display) + #expect(display.resultSets.map(\.id) == [results[0].id, results[1].id]) + #expect(display.activeResultSet?.id == results[1].id) + } + + @Test("Closing the active result waits for the answer about its unsaved edits") + @MainActor + func closeAsksBeforeDroppingEdits() throws { + let coordinator = Self.makeCoordinator() + defer { coordinator.teardown() } + let results = try Self.seed(coordinator, labels: ["a", "b"], activeIndex: 1) + Self.stageEdit(on: coordinator) + coordinator.isShowingConfirmAlert = true + + coordinator.closeResultSet(id: results[1].id) + coordinator.closeOtherResultSets(keeping: results[0].id) + + let display = try #require(coordinator.tabManager.selectedTab?.display) + #expect(display.resultSets.map(\.id) == [results[0].id, results[1].id]) + #expect(coordinator.changeManager.hasChanges) + } + + @Test("A table tab's rows cannot be closed as a result") + @MainActor + func tableTabRefusesClose() throws { + let coordinator = Self.makeCoordinator() + defer { coordinator.teardown() } + try coordinator.tabManager.addTableTab(tableName: "users") + let index = try #require(coordinator.tabManager.selectedTabIndex) + let rows = Self.makeResultSet(label: "users") + coordinator.tabManager.mutate(at: index) { tab in + tab.display.resultSets = [rows] + tab.display.activeResultSetId = rows.id + } + + #expect(coordinator.canCloseActiveResultSet == false) + #expect(coordinator.canStepActiveResultSet(by: 1) == false) + coordinator.closeResultSet(id: rows.id) + + #expect(coordinator.tabManager.selectedTab?.display.resultSets.map(\.id) == [rows.id]) + } + + @Test("The View menu offers only the result steps and close that would do something") + @MainActor + func resultCommandsMatchWhatTheyWouldDo() throws { + let coordinator = Self.makeCoordinator() + defer { coordinator.teardown() } + _ = try Self.seed(coordinator, labels: ["a", "b", "c"], activeIndex: 0, pinnedIndices: [0]) + + #expect(coordinator.canStepActiveResultSet(by: -1) == false) + #expect(coordinator.canStepActiveResultSet(by: 1)) + #expect(coordinator.canCloseActiveResultSet == false) + + let index = try #require(coordinator.tabManager.selectedTabIndex) + let last = try #require(coordinator.tabManager.selectedTab?.display.resultSets.last) + coordinator.tabManager.mutate(at: index) { $0.display.activeResultSetId = last.id } + + #expect(coordinator.canStepActiveResultSet(by: -1)) + #expect(coordinator.canStepActiveResultSet(by: 1) == false) + #expect(coordinator.canCloseActiveResultSet) + } + + @MainActor + private static func seed( + _ coordinator: MainContentCoordinator, + labels: [String], + activeIndex: Int, + pinnedIndices: Set = [] + ) throws -> [ResultSet] { + coordinator.tabManager.addTab(databaseName: "db") + let index = try #require(coordinator.tabManager.selectedTabIndex) + let results = labels.enumerated().map { offset, label in + makeResultSet(label: label, isPinned: pinnedIndices.contains(offset)) + } + coordinator.tabManager.mutate(at: index) { tab in + tab.display.resultSets = results + tab.display.activeResultSetId = results[activeIndex].id + } + return results + } + + @MainActor + private static func stageEdit(on coordinator: MainContentCoordinator) { + coordinator.changeManager.configureForTable( + tableName: "users", + columns: ["id", "name"], + primaryKeyColumns: ["id"], + databaseType: .mysql, + generatedColumns: [], + triggerReload: false + ) + coordinator.changeManager.recordCellChange( + rowID: .existing(0), + columnIndex: 1, + columnName: "name", + oldValue: "Alice", + newValue: "Bob", + originalRow: ["1", "Alice"] + ) + } + @Test("Toggling pin flips the flag on the addressed result") @MainActor func togglePinFlipsFlag() throws { diff --git a/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift b/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift index e043f49c1f..6afd854e6a 100644 --- a/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift +++ b/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift @@ -90,11 +90,7 @@ struct ResultStatusBarLayoutTests { isRefreshingSchema: false, viewMode: .constant(viewMode), resultSetMenu: ResultSetMenuModel(entries: [], activeOrdinal: 0, total: 0), - onActivateResultSet: { _ in }, - onToggleResultSetPin: { _ in }, - onCompareResultSet: { _ in }, - onCloseResultSet: { _ in }, - onCloseOtherResultSets: { _ in }, + resultSetActions: .none, onToggleFilters: {}, onFetchAll: {} ) @@ -351,11 +347,7 @@ struct ResultStatusBarLayoutTests { isRefreshingSchema: false, viewMode: .constant(.data), resultSetMenu: ResultSetMenuModel(entries: [], activeOrdinal: 0, total: 0), - onActivateResultSet: { _ in }, - onToggleResultSetPin: { _ in }, - onCompareResultSet: { _ in }, - onCloseResultSet: { _ in }, - onCloseOtherResultSets: { _ in }, + resultSetActions: .none, onToggleFilters: {}, onFetchAll: {} ) diff --git a/apple/TableProTests/Views/Main/ScriptErrorResultTests.swift b/apple/TableProTests/Views/Main/ScriptErrorResultTests.swift new file mode 100644 index 0000000000..9479a92e54 --- /dev/null +++ b/apple/TableProTests/Views/Main/ScriptErrorResultTests.swift @@ -0,0 +1,57 @@ +import Foundation +import TableProTestSupport +import Testing + +@testable import TablePro + +@MainActor +struct ScriptErrorResultTests { + // The tab used to keep a copy of the message, which then sat over every other result of the run. + @Test("A failed script keeps its error on the Error result, not over the results that succeeded") + func failureStaysOnTheErrorResult() async throws { + let connection = TestFixtures.makeConnection() + let driver = MockDatabaseDriver(connection: connection) + driver.failsStatementContaining = ( + "nope", + NSError(domain: "test", code: 1, userInfo: [NSLocalizedDescriptionKey: "relation \"nope\" does not exist"]) + ) + var session = ConnectionSession(connection: connection, driver: driver) + session.status = .connected + session.browseDatabase = connection.database + DatabaseManager.shared.injectSession(session, for: connection.id) + let tabManager = QueryTabManager() + let coordinator = MainContentCoordinator( + connection: connection, + tabManager: tabManager, + changeManager: DataChangeManager(), + toolbarState: ConnectionToolbarState() + ) + defer { + coordinator.cancelAllQueryTasks() + coordinator.teardown() + DatabaseManager.shared.removeSession(for: connection.id) + } + let tab = QueryTab(title: "Query", query: "SELECT 1;\nSELECT * FROM nope;", tabType: .query) + tabManager.tabs.append(tab) + tabManager.selectedTabId = tab.id + + coordinator.runAllStatements() + try await waitUntil { + !coordinator.tabExecution.isExecuting(tab.id) + && tabManager.tabs.first?.display.resultSets.last?.errorMessage != nil + } + + let settled = try #require(tabManager.tabs.first) + let failed = try #require(settled.display.resultSets.last) + #expect(settled.display.resultSets.count == 2) + #expect(settled.execution.errorMessage == nil) + #expect(failed.failedQuery?.contains("nope") == true) + #expect(settled.shownError?.isDismissible == false) + + let first = try #require(settled.display.resultSets.first) + tabManager.mutate(tabId: tab.id) { $0.display.activeResultSetId = first.id } + let sibling = try #require(tabManager.tabs.first) + #expect(sibling.shownError == nil) + #expect(sibling.latestError?.message.contains("nope") == true) + } +} diff --git a/apple/TableProTests/Views/Results/ResultSetTabBarLayoutTests.swift b/apple/TableProTests/Views/Results/ResultSetTabBarLayoutTests.swift new file mode 100644 index 0000000000..e7c9715950 --- /dev/null +++ b/apple/TableProTests/Views/Results/ResultSetTabBarLayoutTests.swift @@ -0,0 +1,140 @@ +import AppKit +import Foundation +import SwiftUI +import Testing + +@testable import TablePro + +private let tabBarHostWidths: [CGFloat] = [1_400, 900, 600, 400, 320] + +@MainActor +struct ResultSetTabBarLayoutTests { + @Test("Tabs show when every name fits the pane") + func tabsShowWhenTheyFit() { + let host = host(ResultSetTabBar(model: Self.model(labels: ["users", "orders", "Result 3"]), actions: .none), width: 900) + + #expect(tabControls(in: host).count == 1) + #expect(tabControls(in: host).first?.segmentCount == 3) + } + + @Test("A pane too narrow for the tabs keeps only the pull-down") + func narrowPaneFallsBackToThePullDown() { + let labels = (1 ... 6).map { "monthly_revenue_by_region_\($0)" } + let host = host(ResultSetTabBar(model: Self.model(labels: labels), actions: .none), width: 400) + + #expect(tabControls(in: host).isEmpty) + } + + @Test("More than six results are listed by the pull-down alone") + func moreThanSixHaveNoTabs() { + let labels = (1 ... 7).map { "r\($0)" } + let host = host(ResultSetTabBar(model: Self.model(labels: labels), actions: .none), width: 1_400) + + #expect(tabControls(in: host).isEmpty) + } + + @Test("A pinned result's tab carries the pin and selection follows the active result") + func tabsMirrorTheModel() throws { + let host = host( + ResultSetTabBar(model: Self.model(labels: ["users", "orders"], activeIndex: 1, pinnedIndices: [0]), actions: .none), + width: 900 + ) + let tabs = try #require(tabControls(in: host).first) + + #expect(tabs.image(forSegment: 0) != nil) + #expect(tabs.image(forSegment: 1) == nil) + #expect(tabs.selectedSegment == 1) + #expect(tabs.label(forSegment: 0) == "users") + } + + @Test("The tabs never reach past the pane", arguments: tabBarHostWidths) + func tabsStayInsideThePane(width: CGFloat) { + let labels = ["users", "orders", "invoice_line_items", "Result 4"] + let host = host(ResultSetTabBar(model: Self.model(labels: labels), actions: .none), width: width) + + for tabs in tabControls(in: host) { + let frame = tabs.convert(tabs.bounds, to: host.view) + #expect(frame.minX >= 0 && frame.maxX <= width, "tabs at \(frame) in a \(width)pt pane") + } + } + + // The results pane can be dragged down to 150pt; with a long error the banner gives way, never the bars. + @Test("At the pane's minimum height the tab row and the status bar both stay inside it") + func shortPaneKeepsBothBars() throws { + let box = BarFrames() + let message = Array(repeating: "ERROR: relation \"nope\" does not exist at character 15", count: 8) + .joined(separator: "\n") + let stack = VStack(spacing: 0) { + ResultSetTabBar(model: Self.model(labels: ["Result 1", "Error"]), actions: .none) + .onGeometryChange(for: CGRect.self) { $0.frame(in: .named(Self.pane)) } action: { box.top = $0 } + InlineErrorBanner(message: message) + Color.clear.frame(maxHeight: .infinity) + Color.clear + .frame(height: StatusBarChrome.height + 1) + .onGeometryChange(for: CGRect.self) { $0.frame(in: .named(Self.pane)) } action: { box.bottom = $0 } + } + let height = CollapsibleSplitController.defaultBottomMinimumThickness + // An oversized stack is centred in its frame, so an overflow shows as a bar outside the pane's bounds. + let pane = stack + .frame(width: 600, height: height) + .coordinateSpace(name: Self.pane) + _ = host(pane, width: 600, height: height) + + let top = try #require(box.top) + let bottom = try #require(box.bottom) + #expect(top.minY >= 0, "the tab row starts at \(top.minY)") + #expect(bottom.maxY <= height, "the status bar ends at \(bottom.maxY) in a \(height)pt pane") + } + + private static let pane = "pane" + + private func host(_ content: some View, width: CGFloat, height: CGFloat = 40) -> HostedView { + let host = NSHostingView(rootView: AnyView(content)) + host.sizingOptions = [] + let window = NSWindow( + contentRect: NSRect(x: 0, y: 0, width: width, height: height), + styleMask: [.titled], + backing: .buffered, + defer: false + ) + window.isReleasedWhenClosed = false + window.contentView = host + host.frame = NSRect(x: 0, y: 0, width: width, height: height) + host.layoutSubtreeIfNeeded() + return HostedView(view: host, window: window) + } + + private func tabControls(in hosted: HostedView) -> [ResultSetTabsControl] { + tabControls(in: hosted.view) + } + + private func tabControls(in view: NSView) -> [ResultSetTabsControl] { + let own = (view as? ResultSetTabsControl).map { [$0] } ?? [] + return own + view.subviews.flatMap { tabControls(in: $0) }.filter { !$0.isHiddenOrHasHiddenAncestor } + } + + private static func model(labels: [String], activeIndex: Int = 0, pinnedIndices: Set = []) -> ResultSetMenuModel { + let entries = labels.enumerated().map { index, label in + ResultSetMenuEntry( + id: UUID(), + label: label, + isPinned: pinnedIndices.contains(index), + isActive: index == activeIndex, + ordinal: index + 1, + isComparable: true + ) + } + return ResultSetMenuModel(entries: entries, activeOrdinal: activeIndex + 1, total: labels.count) + } +} + +private struct HostedView { + let view: NSView + let window: NSWindow +} + +@MainActor +private final class BarFrames { + var top: CGRect? + var bottom: CGRect? +} diff --git a/apple/TableProUITests/QueryRunUITests.swift b/apple/TableProUITests/QueryRunUITests.swift index 6c8b1fffe8..661e5c373a 100644 --- a/apple/TableProUITests/QueryRunUITests.swift +++ b/apple/TableProUITests/QueryRunUITests.swift @@ -67,5 +67,15 @@ final class QueryRunUITests: UITestCase { XCTAssertTrue(names.contains { $0.hasPrefix("SELECT 2") }, "A bare statement is named after its text, got \(names)") XCTAssertFalse(names.contains { $0.hasPrefix("Result ") }, "No result may fall back to its position, got \(names)") app.typeKey(.escape, modifierFlags: []) + + let tabs = resultSetTabs(in: window) + XCTAssertTrue(tabs.waitToExist(timeout: 10), "Two results must be offered as tabs") + let firstResult = resultSetTab(containing: "monthly totals", in: tabs) + XCTAssertTrue(waitUntilHittable(firstResult, timeout: 10), "Each result must have its own tab") + firstResult.click() + XCTAssertTrue( + waitForPredicate(timeout: 10) { chooser.title.hasPrefix("1 of 2") || chooser.title.hasSuffix("1/2") }, + "One click on a tab must show that result, got \(chooser.title)" + ) } } diff --git a/apple/TableProUITests/Support/UITestCase+EditorTabs.swift b/apple/TableProUITests/Support/UITestCase+EditorTabs.swift index 5a52ddd39a..e63ccee6db 100644 --- a/apple/TableProUITests/Support/UITestCase+EditorTabs.swift +++ b/apple/TableProUITests/Support/UITestCase+EditorTabs.swift @@ -36,6 +36,18 @@ extension UITestCase { window.descendants(matching: .any).matching(identifier: "result-set-menu").firstMatch } + internal func resultSetTabs(in window: XCUIElement) -> XCUIElement { + window.descendants(matching: .any).matching(identifier: "result-set-tabs").firstMatch + } + + // A tab is a `Tab` element under the tabs role on macOS 27 and a radio button before it, like a view switcher's. + internal func resultSetTab(containing text: String, in tabs: XCUIElement) -> XCUIElement { + let types = [XCUIElement.ElementType.tab, .radioButton].map(\.rawValue) + return tabs.descendants(matching: .any) + .matching(NSPredicate(format: "label CONTAINS %@ AND elementType IN %@", text, types)) + .firstMatch + } + internal func gridCell(showing text: String, in grid: XCUIElement) -> XCUIElement { grid.staticTexts.matching(NSPredicate(format: "value == %@", text)).firstMatch } diff --git a/apple/changelog.d/3924.added.md b/apple/changelog.d/3924.added.md new file mode 100644 index 0000000000..eb382a2cd9 --- /dev/null +++ b/apple/changelog.d/3924.added.md @@ -0,0 +1 @@ +Two to six query results show as tabs above the grid, so switching takes one click. diff --git a/apple/changelog.d/3924.fixed.1.md b/apple/changelog.d/3924.fixed.1.md new file mode 100644 index 0000000000..bd290c69aa --- /dev/null +++ b/apple/changelog.d/3924.fixed.1.md @@ -0,0 +1 @@ +Closing or clearing a result dropped its unsaved cell edits without asking. diff --git a/apple/changelog.d/3924.fixed.2.md b/apple/changelog.d/3924.fixed.2.md new file mode 100644 index 0000000000..67ffaba29a --- /dev/null +++ b/apple/changelog.d/3924.fixed.2.md @@ -0,0 +1 @@ +Close Result Tab emptied a table tab, and Previous Result and Next Result stayed enabled with nowhere to go. diff --git a/apple/changelog.d/3924.fixed.3.md b/apple/changelog.d/3924.fixed.3.md new file mode 100644 index 0000000000..64c84ece5f --- /dev/null +++ b/apple/changelog.d/3924.fixed.3.md @@ -0,0 +1 @@ +Dismissing a failed statement's error turned its result into a success message. diff --git a/apple/changelog.d/3924.fixed.4.md b/apple/changelog.d/3924.fixed.4.md new file mode 100644 index 0000000000..577b088cf9 --- /dev/null +++ b/apple/changelog.d/3924.fixed.4.md @@ -0,0 +1 @@ +A script's error banner stayed over the results of the statements that succeeded. diff --git a/apple/changelog.d/3924.fixed.5.md b/apple/changelog.d/3924.fixed.5.md new file mode 100644 index 0000000000..5205b4b701 --- /dev/null +++ b/apple/changelog.d/3924.fixed.5.md @@ -0,0 +1 @@ +Long result names were cut through an emoji. diff --git a/apple/changelog.d/3924.fixed.6.md b/apple/changelog.d/3924.fixed.6.md new file mode 100644 index 0000000000..8ed46333c6 --- /dev/null +++ b/apple/changelog.d/3924.fixed.6.md @@ -0,0 +1 @@ +A new run numbered its results from 1 even when pinned results already used those numbers. diff --git a/apple/changelog.d/3924.fixed.md b/apple/changelog.d/3924.fixed.md new file mode 100644 index 0000000000..f9352de880 --- /dev/null +++ b/apple/changelog.d/3924.fixed.md @@ -0,0 +1 @@ +Closing a result jumped to the last result instead of the one beside it. From 56c39b6cf02782b3629eb7993e286105e7227708 Mon Sep 17 00:00:00 2001 From: Cristian Date: Sat, 31 Oct 2026 05:33:48 +0100 Subject: [PATCH 33/85] feat(linux-editor): switch a query tab's database on SQL Server, ClickHouse, Cassandra and MongoDB (#3898) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3898.added.md | 1 + linux/changelog.d/3898.fixed.md | 1 + .../core/src/sql_syntax/catalog_change.rs | 26 ++++ linux/crates/core/src/sql_syntax/mod.rs | 2 +- .../drivers/cassandra/src/connection.rs | 6 + linux/crates/drivers/cassandra/src/editor.rs | 58 ++++++++- linux/crates/drivers/cassandra/src/errors.rs | 2 +- .../drivers/clickhouse/src/connection.rs | 6 + .../drivers/clickhouse/src/editor_session.rs | 114 ++++++++++++++++-- .../crates/drivers/mongodb/src/connection.rs | 6 + .../drivers/mongodb/src/editor_session.rs | 11 ++ linux/crates/drivers/mssql/src/connection.rs | 8 ++ linux/crates/drivers/mssql/src/dialect.rs | 11 +- linux/crates/drivers/mssql/src/editor_run.rs | 9 ++ .../drivers/mssql/src/editor_session.rs | 51 +++++++- linux/crates/drivers/mssql/src/metadata.rs | 23 ++++ .../mssql/tests/integration/metadata.rs | 24 ++++ .../mssql/tests/integration/sessions.rs | 27 +++++ 18 files changed, 368 insertions(+), 18 deletions(-) create mode 100644 linux/changelog.d/3898.added.md create mode 100644 linux/changelog.d/3898.fixed.md diff --git a/linux/changelog.d/3898.added.md b/linux/changelog.d/3898.added.md new file mode 100644 index 0000000000..80506f53f1 --- /dev/null +++ b/linux/changelog.d/3898.added.md @@ -0,0 +1 @@ +The SQL editor's database menu also moves a tab on SQL Server, ClickHouse, Cassandra, ScyllaDB and MongoDB. diff --git a/linux/changelog.d/3898.fixed.md b/linux/changelog.d/3898.fixed.md new file mode 100644 index 0000000000..bdbc531689 --- /dev/null +++ b/linux/changelog.d/3898.fixed.md @@ -0,0 +1 @@ +On ClickHouse, a USE in the SQL editor now applies to the statements that follow it. diff --git a/linux/crates/core/src/sql_syntax/catalog_change.rs b/linux/crates/core/src/sql_syntax/catalog_change.rs index 2fc62739eb..8154324b4c 100644 --- a/linux/crates/core/src/sql_syntax/catalog_change.rs +++ b/linux/crates/core/src/sql_syntax/catalog_change.rs @@ -39,6 +39,22 @@ pub fn may_change_database(text: &str, grammar: SqlGrammar) -> bool { .any(|token| token.is_word("USE")) } +/// The database the last `USE ` in `text` names, for an engine +/// whose client, not its server, keeps the database a request runs in. A +/// name that would need unescaping is left out. +pub fn used_database(text: &str, grammar: SqlGrammar) -> Option { + lex_statements(text, grammar) + .statements + .iter() + .rev() + .find_map(|statement| match statement.as_slice() { + [first, name] if first.is_word("USE") => name.name(), + _ => None, + }) + .filter(|name| !name.is_empty() && !name.contains(['`', '"', '\\'])) + .map(str::to_owned) +} + fn plan_changes(text: &str, plan: &ScriptPlan) -> bool { let is = |word: &str, set: &[&str]| set.iter().any(|verb| word.eq_ignore_ascii_case(verb)); plan.statements().iter().any(|statement| { @@ -60,6 +76,16 @@ fn plan_changes(text: &str, plan: &ScriptPlan) -> bool { mod tests { use super::*; + #[test] + fn a_use_names_its_database() { + let grammar = SqlGrammar::ClickHouse; + assert_eq!(used_database("USE analytics", grammar).as_deref(), Some("analytics")); + assert_eq!(used_database("use `my db`;", grammar).as_deref(), Some("my db")); + assert_eq!(used_database("USE a; USE b", grammar).as_deref(), Some("b")); + assert_eq!(used_database("SELECT 1", grammar), None); + assert_eq!(used_database("USE `a``b`", grammar), None, "a name to unescape"); + } + #[test] fn a_use_can_change_the_database() { for grammar in SqlGrammar::SQL { diff --git a/linux/crates/core/src/sql_syntax/mod.rs b/linux/crates/core/src/sql_syntax/mod.rs index a3e14a8a91..fd979ac0e5 100644 --- a/linux/crates/core/src/sql_syntax/mod.rs +++ b/linux/crates/core/src/sql_syntax/mod.rs @@ -17,7 +17,7 @@ mod statement_access; mod statement_tokens; pub use builtin_snippet::{BuiltinSnippet, builtin_snippets}; -pub use catalog_change::{changes_catalog, may_change_database}; +pub use catalog_change::{changes_catalog, may_change_database, used_database}; pub use credential_rules::CredentialRules; pub use errors::{ExpressionSyntaxError, TypeSyntaxError}; pub use grammar::SqlGrammar; diff --git a/linux/crates/drivers/cassandra/src/connection.rs b/linux/crates/drivers/cassandra/src/connection.rs index 1d1fe242df..599e39f40d 100644 --- a/linux/crates/drivers/cassandra/src/connection.rs +++ b/linux/crates/drivers/cassandra/src/connection.rs @@ -243,6 +243,12 @@ impl Connection for CqlConnection { outcome } + /// The schemas this engine lists are the server's databases, which a + /// query tab can move between. + async fn list_databases(&self, options: CallOptions) -> Result, DriverError> { + self.list_schemas(options).await + } + async fn list_schemas(&self, options: CallOptions) -> Result, DriverError> { let default = self.endpoint.keyspace().map(str::to_owned); self.read(options, move |link, _| async move { diff --git a/linux/crates/drivers/cassandra/src/editor.rs b/linux/crates/drivers/cassandra/src/editor.rs index 97da762cd6..84c6351594 100644 --- a/linux/crates/drivers/cassandra/src/editor.rs +++ b/linux/crates/drivers/cassandra/src/editor.rs @@ -4,8 +4,8 @@ //! only that tab, as cqlsh moves only its own connection. CQL keeps no //! other state on a connection: no transaction, no temporary table. -use std::sync::Arc; use std::sync::atomic::{AtomicU8, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; use std::time::Duration; use async_trait::async_trait; @@ -21,7 +21,7 @@ use tablepro_core::{ use tokio::time::Instant; use tokio_util::sync::CancellationToken; -use crate::errors::execution_error; +use crate::errors::{execution_error, use_keyspace_error}; use crate::rows::{ColumnsRead, MAX_PAGE_ROWS, next_state, page_size}; use crate::session::{CqlLink, LinkSlot}; @@ -40,12 +40,17 @@ pub(crate) struct CqlEditorSession { statement_timeout: Option, state: Arc, shutdown: CancellationToken, + /// The keyspace the server confirmed. The driver records one it is + /// still moving to, or failed to. + keyspace: Arc>>, } impl CqlEditorSession { pub(crate) fn new(link: CqlLink, read_only: bool, statement_timeout: Option) -> Self { + let keyspace = link.session().get_keyspace().map(|keyspace| keyspace.to_string()); Self { id: EditorSessionId::new(), + keyspace: Arc::new(Mutex::new(keyspace)), link: LinkSlot::new(link), read_only, statement_timeout, @@ -74,10 +79,39 @@ impl EditorSession for CqlEditorSession { self.id } - /// The driver moves the session itself when a `USE` answers. fn database(&self) -> Option { - let link = self.link.get().ok()?; - link.session().get_keyspace().map(|keyspace| keyspace.to_string()) + self.keyspace.lock().unwrap_or_else(PoisonError::into_inner).clone() + } + + /// The driver checks the name, which CQL takes as letters, digits and + /// `_` alone, and moves every connection of the session. A move that + /// does not finish leaves them in keyspaces nothing knows, so the + /// session ends. + async fn use_database(&self, database: &str, options: CallOptions) -> Result<(), DriverError> { + let link = self.link.get()?; + self.begin()?; + let using = link.session().use_keyspace(database, true); + let used = tokio::select! { + biased; + () = options.cancel().cancelled() => Err(DriverError::Cancelled), + () = until(options.deadline()) => Err(DriverError::Timeout { + phase: tablepro_core::TimeoutPhase::Statement, + server_cancelled: false, + }), + used = using => used.map_err(|error| use_keyspace_error(error, LossPhase::Statement)), + }; + match used { + Ok(()) => { + *self.keyspace.lock().unwrap_or_else(PoisonError::into_inner) = Some(database.to_owned()); + self.state.store(READY, Ordering::SeqCst); + Ok(()) + } + Err(error) => { + self.state.store(LOST, Ordering::SeqCst); + self.link.release(); + Err(error) + } + } } fn state(&self) -> EditorSessionState { @@ -106,8 +140,15 @@ impl EditorSession for CqlEditorSession { shutdown: self.shutdown.clone(), }; let state = self.state.clone(); + let (keyspace, link) = (self.keyspace.clone(), run.link.clone()); + let follows = tablepro_core::sql_syntax::may_change_database(sql.as_str(), SqlGrammar::Cql); let running = tokio::spawn(async move { let outcome = run.run(sql, options, sink).await; + // The driver records a keyspace once the server confirms it. + if outcome.is_ok() && follows { + *keyspace.lock().unwrap_or_else(PoisonError::into_inner) = + link.session().get_keyspace().map(|keyspace| keyspace.to_string()); + } let next = match &outcome { Ok(_) => READY, Err(_) => LOST, @@ -375,3 +416,10 @@ fn lost(error: &DriverError) -> bool { ErrorCategory::Network | ErrorCategory::ConnectionLost | ErrorCategory::Tls ) } + +async fn until(deadline: Option) { + match deadline { + Some(at) => tokio::time::sleep_until(at).await, + None => std::future::pending().await, + } +} diff --git a/linux/crates/drivers/cassandra/src/errors.rs b/linux/crates/drivers/cassandra/src/errors.rs index 0750d4f0b6..1098400562 100644 --- a/linux/crates/drivers/cassandra/src/errors.rs +++ b/linux/crates/drivers/cassandra/src/errors.rs @@ -88,7 +88,7 @@ fn pool_error(error: ConnectionPoolError, during: LossPhase) -> DriverError { } } -fn use_keyspace_error(error: UseKeyspaceError, during: LossPhase) -> DriverError { +pub(crate) fn use_keyspace_error(error: UseKeyspaceError, during: LossPhase) -> DriverError { match error { UseKeyspaceError::RequestError(attempt) => attempt_error(attempt, during), other => DriverError::server(other.to_string()), diff --git a/linux/crates/drivers/clickhouse/src/connection.rs b/linux/crates/drivers/clickhouse/src/connection.rs index a80b148d3e..abe7c291e4 100644 --- a/linux/crates/drivers/clickhouse/src/connection.rs +++ b/linux/crates/drivers/clickhouse/src/connection.rs @@ -261,6 +261,12 @@ impl Connection for ClickHouseConnection { outcome } + /// The schemas this engine lists are the server's databases, which a + /// query tab can move between. + async fn list_databases(&self, options: CallOptions) -> Result, DriverError> { + self.list_schemas(options).await + } + async fn list_schemas(&self, options: CallOptions) -> Result, DriverError> { self.workspace_read(options, |calls| async move { metadata::list_schemas(&calls).await }) .await diff --git a/linux/crates/drivers/clickhouse/src/editor_session.rs b/linux/crates/drivers/clickhouse/src/editor_session.rs index 4d00aacd94..894506f6f3 100644 --- a/linux/crates/drivers/clickhouse/src/editor_session.rs +++ b/linux/crates/drivers/clickhouse/src/editor_session.rs @@ -1,10 +1,10 @@ -use std::sync::Arc; +use std::sync::{Arc, Mutex, PoisonError}; use std::time::Duration; use async_trait::async_trait; use tablepro_core::{ CallOptions, DriverError, EditorSession, EditorSessionId, EditorSessionOptions, EditorSessionState, LossPhase, - ResultSink, RunOptions, RunSummary, TimeoutPhase, TransactionState, UserSql, + ResultSink, RunEnd, RunOptions, RunSummary, TimeoutPhase, TransactionState, UserSql, }; use tokio_util::sync::CancellationToken; use uuid::Uuid; @@ -24,7 +24,9 @@ use crate::session_status::SessionStatus; /// takes no scripts. pub(crate) struct ClickHouseEditorSession { id: EditorSessionId, - session: clickhouse::Client, + /// Every request names its database, which overrides a `USE` the + /// session ran, so the tab's database lives in the client. + session: Arc>, control: clickhouse::Client, statement_timeout: Option, grace: Duration, @@ -34,6 +36,10 @@ pub(crate) struct ClickHouseEditorSession { } impl ClickHouseEditorSession { + fn client(&self) -> clickhouse::Client { + self.session.lock().unwrap_or_else(PoisonError::into_inner).clone() + } + /// Open the session with one trivial question, so a session the /// server will not create fails here rather than on the first run. pub(crate) async fn open( @@ -59,7 +65,7 @@ impl ClickHouseEditorSession { id: EditorSessionId::new(), // Without the check, a run on a session the server let expire // quietly starts a new, empty one under the same id. - session: session.with_setting("session_check", "1"), + session: Arc::new(Mutex::new(session.with_setting("session_check", "1"))), control: endpoint.control().clone(), statement_timeout: options.statement_timeout, grace: liveness.cancel_grace, @@ -76,10 +82,26 @@ impl EditorSession for ClickHouseEditorSession { self.id } - /// Every request names the database, which a `USE` in the session - /// does not override. fn database(&self) -> Option { - self.session.database().map(str::to_owned) + self.client().database().map(str::to_owned) + } + + /// The next request names `database`, once a request there answers. + async fn use_database(&self, database: &str, options: CallOptions) -> Result<(), DriverError> { + let moved = self.client().with_database(database); + let asking = drain(moved.query_raw("SELECT 1")); + let answered = tokio::select! { + biased; + () = options.cancel().cancelled() => return Err(DriverError::Cancelled), + () = until(options.deadline()) => return Err(DriverError::Timeout { + phase: TimeoutPhase::Statement, + server_cancelled: false, + }), + answered = asking => answered, + }; + answered?; + *self.session.lock().unwrap_or_else(PoisonError::into_inner) = moved; + Ok(()) } fn state(&self) -> EditorSessionState { @@ -97,17 +119,27 @@ impl EditorSession for ClickHouseEditorSession { ) -> Result { self.status.begin()?; let guard = options.cancel.clone().drop_guard(); + let used = + tablepro_core::sql_syntax::used_database(sql.as_str(), tablepro_core::sql_syntax::SqlGrammar::ClickHouse); let run = EditorRun { - session: self.session.clone(), + session: self.client(), control: self.control.clone(), statement_timeout: self.statement_timeout, grace: self.grace, shutdown: self.shutdown.clone(), lingering: self.lingering.clone(), }; - let status = self.status.clone(); + let (status, client) = (self.status.clone(), self.session.clone()); let running = tokio::spawn(async move { let outcome = run.run(sql, options, sink).await; + // The server took the `USE`; the requests after it name that + // database, as the session would have. + if let (Ok(summary), Some(database)) = (&outcome, used) + && summary.end == RunEnd::Completed + { + let mut client = client.lock().unwrap_or_else(PoisonError::into_inner); + *client = client.clone().with_database(database); + } match &outcome { // The server still held the session for a stopped query: // the session itself is fine. @@ -143,6 +175,13 @@ impl EditorSession for ClickHouseEditorSession { } } +async fn until(deadline: Option) { + match deadline { + Some(at) => tokio::time::sleep_until(at).await, + None => std::future::pending().await, + } +} + #[cfg(test)] mod tests { use async_trait::async_trait; @@ -171,6 +210,63 @@ mod tests { .await } + struct Collect(Arc>>>); + + #[async_trait] + impl ResultSink for Collect { + async fn accept(&mut self, event: ResultEvent) -> SinkFlow { + if let ResultEvent::Rows(rows) = event { + self.0.lock().unwrap_or_else(PoisonError::into_inner).extend(rows); + } + SinkFlow::Continue + } + } + + async fn current_database(session: &ClickHouseEditorSession) -> Vec> { + let rows = Arc::new(Mutex::new(Vec::new())); + session + .run( + UserSql::new("SELECT currentDatabase()"), + RunOptions::new(CancellationToken::new()), + Box::new(Collect(rows.clone())), + ) + .await + .expect("a run"); + rows.lock().unwrap_or_else(PoisonError::into_inner).clone() + } + + /// Every request names its database, so a `USE` lasts only because + /// the session's next requests name the database it chose. + #[tokio::test] + #[ignore = "requires docker"] + async fn a_session_runs_in_the_database_it_moved_to() { + let fixture = ClickHouseFixture::start().await.expect("a ClickHouse container"); + let endpoint = ClickHouseEndpoint::for_fixture(&fixture, test_liveness()); + let session = + ClickHouseEditorSession::open(&endpoint, EditorSessionOptions::default(), CancellationToken::new()) + .await + .expect("an editor session"); + let text = |name: &str| vec![vec![tablepro_core::Value::Text(name.to_owned())]]; + + session + .use_database("system", CallOptions::new(None, CancellationToken::new())) + .await + .expect("a move to system"); + assert_eq!(current_database(&session).await, text("system")); + assert_eq!(session.database().as_deref(), Some("system")); + + let used = run(&session, "USE default").await; + assert_eq!(used.map(|summary| summary.end), Ok(RunEnd::Completed)); + assert_eq!(current_database(&session).await, text("default")); + assert_eq!(session.database().as_deref(), Some("default")); + + let refused = session + .use_database("no_such_database", CallOptions::new(None, CancellationToken::new())) + .await; + assert!(refused.is_err(), "{refused:?}"); + assert_eq!(session.database().as_deref(), Some("default"), "a refused move moved"); + } + #[tokio::test] #[ignore = "requires docker"] async fn a_session_the_server_let_expire_is_lost_not_replaced() { diff --git a/linux/crates/drivers/mongodb/src/connection.rs b/linux/crates/drivers/mongodb/src/connection.rs index 170848f1d5..a5d55fccf7 100644 --- a/linux/crates/drivers/mongodb/src/connection.rs +++ b/linux/crates/drivers/mongodb/src/connection.rs @@ -282,6 +282,12 @@ impl Connection for MongoConnection { outcome } + /// The schemas this engine lists are the server's databases, which a + /// query tab can move between. + async fn list_databases(&self, options: CallOptions) -> Result, DriverError> { + self.list_schemas(options).await + } + async fn list_schemas(&self, options: CallOptions) -> Result, DriverError> { let default = self.database.clone(); self.read(options, move |context, session| { diff --git a/linux/crates/drivers/mongodb/src/editor_session.rs b/linux/crates/drivers/mongodb/src/editor_session.rs index be9f35e515..121b1f6026 100644 --- a/linux/crates/drivers/mongodb/src/editor_session.rs +++ b/linux/crates/drivers/mongodb/src/editor_session.rs @@ -68,6 +68,17 @@ impl EditorSession for MongoEditorSession { Some(self.database.lock().unwrap_or_else(PoisonError::into_inner).clone()) } + /// As the shell's `use`: the next statements name the database. + async fn use_database(&self, database: &str, _options: CallOptions) -> Result<(), DriverError> { + if self.status.state() == EditorSessionState::Lost { + return Err(DriverError::ConnectionLost { + during: LossPhase::Idle, + }); + } + *self.database.lock().unwrap_or_else(PoisonError::into_inner) = database.to_owned(); + Ok(()) + } + fn state(&self) -> EditorSessionState { self.status.state() } diff --git a/linux/crates/drivers/mssql/src/connection.rs b/linux/crates/drivers/mssql/src/connection.rs index 2aebc5c9b4..d88cd13e86 100644 --- a/linux/crates/drivers/mssql/src/connection.rs +++ b/linux/crates/drivers/mssql/src/connection.rs @@ -254,6 +254,14 @@ impl Connection for MssqlConnection { .await } + async fn list_databases(&self, options: CallOptions) -> Result, DriverError> { + self.read( + options, + read_op(|client, watch| Box::pin(metadata::list_databases(client, watch))), + ) + .await + } + async fn list_objects(&self, scope: &ObjectScope, options: CallOptions) -> Result, DriverError> { let scope = scope.clone(); self.read( diff --git a/linux/crates/drivers/mssql/src/dialect.rs b/linux/crates/drivers/mssql/src/dialect.rs index 3d4e638b59..15ec2a7f9e 100644 --- a/linux/crates/drivers/mssql/src/dialect.rs +++ b/linux/crates/drivers/mssql/src/dialect.rs @@ -41,6 +41,10 @@ impl MssqlDialect { } } +pub(crate) fn quote(name: &str) -> String { + format!("[{}]", name.replace(']', "]]")) +} + impl SqlDialect for MssqlDialect { fn database_layout(&self) -> DatabaseLayout { DatabaseLayout::WithSchemas @@ -60,7 +64,7 @@ impl SqlDialect for MssqlDialect { } fn quote_identifier(&self, name: &str) -> String { - format!("[{}]", name.replace(']', "]]")) + quote(name) } fn placeholder(&self, ordinal: usize, value: Value, target: BindTarget<'_>) -> Result { @@ -526,6 +530,11 @@ mod tests { MssqlDialect::new(16) } + #[test] + fn a_bracket_in_a_name_is_doubled() { + assert_eq!(quote("sales ]2026"), "[sales ]]2026]"); + } + fn column(declared: &str) -> ColumnInfo { ColumnInfo { name: "c".to_owned(), diff --git a/linux/crates/drivers/mssql/src/editor_run.rs b/linux/crates/drivers/mssql/src/editor_run.rs index cbcaf2f7c1..6e7a9df7de 100644 --- a/linux/crates/drivers/mssql/src/editor_run.rs +++ b/linux/crates/drivers/mssql/src/editor_run.rs @@ -247,6 +247,15 @@ pub(crate) async fn transaction_state(client: &mut MssqlClient) -> Result Result<(), DriverError> { + let statement = format!("USE {}", crate::dialect::quote(database)); + let stream = client.simple_query(statement).await.map_err(map_error)?; + stream.into_results().await.map_err(map_error)?; + Ok(()) +} + /// The database the session is in, from the server itself. pub(crate) async fn current_database(client: &mut MssqlClient) -> Result, DriverError> { let stream = client.simple_query("SELECT DB_NAME()").await.map_err(map_error)?; diff --git a/linux/crates/drivers/mssql/src/editor_session.rs b/linux/crates/drivers/mssql/src/editor_session.rs index d7145794de..01ce460613 100644 --- a/linux/crates/drivers/mssql/src/editor_session.rs +++ b/linux/crates/drivers/mssql/src/editor_session.rs @@ -12,7 +12,7 @@ use tokio::sync::OwnedMutexGuard; use tokio::time::Instant; use tokio_util::sync::CancellationToken; -use crate::editor_run::{EditorRun, ScriptOutcome, current_database, transaction_state}; +use crate::editor_run::{EditorRun, ScriptOutcome, current_database, transaction_state, use_database}; use crate::endpoint::MssqlEndpoint; use crate::errors::ends_connection; use crate::in_band_request::StopPolicy; @@ -248,6 +248,55 @@ impl EditorSession for MssqlEditorSession { outcome } + async fn use_database(&self, database: &str, options: CallOptions) -> Result<(), DriverError> { + let shared = &self.shared; + let mut held = tokio::select! { + biased; + () = options.cancel().cancelled() => return Err(DriverError::Cancelled), + () = elapsed(options.deadline()) => { + return Err(DriverError::Timeout { phase: TimeoutPhase::Statement, server_cancelled: false }); + } + held = shared.link.lock() => held, + }; + let Some(link) = held.as_mut().filter(|_| !shared.lost.load(Ordering::SeqCst)) else { + return Err(DriverError::ConnectionLost { + during: LossPhase::Idle, + }); + }; + let using = use_database(&mut link.client, database); + let used = tokio::select! { + biased; + () = options.cancel().cancelled() => None, + () = elapsed(options.deadline()) => None, + used = using => Some(used), + }; + match used { + Some(Ok(())) => { + *shared.database.lock().unwrap_or_else(PoisonError::into_inner) = Some(database.to_owned()); + Ok(()) + } + Some(Err(error)) => { + if ends_connection(&error) { + shared.lost.store(true, Ordering::SeqCst); + *held = None; + } + Err(error) + } + // Cut off mid-request, the connection is out of step. + None => { + shared.lost.store(true, Ordering::SeqCst); + *held = None; + Err(match options.cancel().is_cancelled() { + true => DriverError::Cancelled, + false => DriverError::Timeout { + phase: TimeoutPhase::Statement, + server_cancelled: false, + }, + }) + } + } + } + async fn transaction_state(&self, options: CallOptions) -> Result { let shared = &self.shared; let mut held = tokio::select! { diff --git a/linux/crates/drivers/mssql/src/metadata.rs b/linux/crates/drivers/mssql/src/metadata.rs index c93ec84c41..f098234f89 100644 --- a/linux/crates/drivers/mssql/src/metadata.rs +++ b/linux/crates/drivers/mssql/src/metadata.rs @@ -24,6 +24,14 @@ const SCHEMAS_SQL: &str = "SELECT s.name, \ THEN 1 ELSE 0 END AS int) \ FROM sys.schemas s ORDER BY s.name"; +/// The online databases the login may open, the connection's own the +/// default and the four the server keeps for itself (`database_id` 1 to +/// 4) as system ones. +const DATABASES_SQL: &str = "SELECT d.name, \ + CAST(CASE WHEN d.name = DB_NAME() THEN 1 ELSE 0 END AS int), \ + CAST(CASE WHEN d.database_id <= 4 THEN 1 ELSE 0 END AS int) \ + FROM sys.databases d WHERE d.state = 0 AND HAS_DBACCESS(d.name) = 1 ORDER BY d.name"; + const OBJECTS_SQL: &str = "SELECT SCHEMA_NAME(o.schema_id), o.name, RTRIM(o.type) \ FROM sys.objects o \ WHERE o.type IN ('U', 'V', 'SO', 'FN', 'IF', 'TF', 'FS', 'FT', 'P', 'PC') AND o.is_ms_shipped = 0"; @@ -80,6 +88,21 @@ pub(crate) async fn list_schemas(client: &mut MssqlClient, watch: &Watch) -> Res .map_err(Interrupt::Failed) } +pub(crate) async fn list_databases(client: &mut MssqlClient, watch: &Watch) -> Result, Interrupt> { + let rows = catalogue_rows(client, watch, DATABASES_SQL, &[]).await?; + rows.iter() + .map(|row| { + let cells = Cells::new(row, "sys.databases"); + Ok(SchemaInfo { + name: cells.required_text(0)?, + is_default: cells.flag(1), + is_system: cells.flag(2), + }) + }) + .collect::>() + .map_err(Interrupt::Failed) +} + pub(crate) async fn list_objects( client: &mut MssqlClient, watch: &Watch, diff --git a/linux/crates/drivers/mssql/tests/integration/metadata.rs b/linux/crates/drivers/mssql/tests/integration/metadata.rs index c1f3baf099..9ccc4402d0 100644 --- a/linux/crates/drivers/mssql/tests/integration/metadata.rs +++ b/linux/crates/drivers/mssql/tests/integration/metadata.rs @@ -114,6 +114,30 @@ async fn lists_objects_and_names_rows_by_key() { assert!(schemas.iter().any(|schema| schema.name == "sys" && schema.is_system)); } +/// The databases a query tab can move to: the login's own marked as the +/// default and the server's four as system ones. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn the_servers_databases_are_listed_for_a_query_tab() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + + let databases = connection.list_databases(within(CALL)).await.expect("the databases"); + + let find = |name: &str| databases.iter().find(|database| database.name == name); + assert!( + databases.iter().filter(|database| database.is_default).count() == 1, + "{databases:?}" + ); + let master = find("master").expect("master"); + assert!(master.is_system, "{master:?}"); + let own = databases + .iter() + .find(|database| database.is_default) + .expect("the login's database"); + assert!(!own.is_system || own.name == "master", "{own:?}"); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] async fn fetch_table_definition_reports_pk_name_collation_comment() { diff --git a/linux/crates/drivers/mssql/tests/integration/sessions.rs b/linux/crates/drivers/mssql/tests/integration/sessions.rs index ae1e0008a0..4df5fae92b 100644 --- a/linux/crates/drivers/mssql/tests/integration/sessions.rs +++ b/linux/crates/drivers/mssql/tests/integration/sessions.rs @@ -24,6 +24,33 @@ async fn use_database_persists_across_runs() { ); } +/// The editor bar's move: a bracketed name carries a `]`, and a database +/// the server refuses leaves the session where it was. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn a_session_moves_to_a_database_by_its_name() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + let session = session(connection.as_ref()).await; + // CREATE DATABASE runs outside a transaction, as the editor runs it. + run_ok( + session.as_ref(), + "IF DB_ID(N'odd]name') IS NULL CREATE DATABASE [odd]]name]", + ) + .await; + let options = || tablepro_core::CallOptions::new(None, CancellationToken::new()); + + session.use_database("odd]name", options()).await.expect("a move"); + let database = run_ok(session.as_ref(), "SELECT DB_NAME()").await.rows(); + assert_eq!(database, vec![vec![Value::Text("odd]name".to_owned())]]); + assert_eq!(session.database().as_deref(), Some("odd]name")); + + let refused = session.use_database("no_such_database", options()).await; + assert!(refused.is_err(), "{refused:?}"); + let database = run_ok(session.as_ref(), "SELECT DB_NAME()").await.rows(); + assert_eq!(database, vec![vec![Value::Text("odd]name".to_owned())]]); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] async fn begin_tran_insert_big_select_commit_persists() { From 0c210873daa9b999b6ce58dbdae2ec805fa22501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sat, 31 Oct 2026 11:35:26 +0700 Subject: [PATCH 34/85] revert(about): stop naming license holders in the About panel (#3926) --- .../Core/Menu/AboutPanelCredits.swift | 79 ------------------- .../Menu/AppDelegate+MainMenuActions.swift | 32 ++++++++ .../TablePro/Resources/Localizable.xcstrings | 40 ---------- apple/TablePro/Resources/Supporters.json | 3 - .../Core/Menu/AboutPanelCreditsTests.swift | 44 ----------- apple/changelog.d/3923.added.md | 1 - 6 files changed, 32 insertions(+), 167 deletions(-) delete mode 100644 apple/TablePro/Core/Menu/AboutPanelCredits.swift delete mode 100644 apple/TablePro/Resources/Supporters.json delete mode 100644 apple/TableProTests/Core/Menu/AboutPanelCreditsTests.swift delete mode 100644 apple/changelog.d/3923.added.md diff --git a/apple/TablePro/Core/Menu/AboutPanelCredits.swift b/apple/TablePro/Core/Menu/AboutPanelCredits.swift deleted file mode 100644 index 8492b87d06..0000000000 --- a/apple/TablePro/Core/Menu/AboutPanelCredits.swift +++ /dev/null @@ -1,79 +0,0 @@ -// -// AboutPanelCredits.swift -// TablePro -// - -import AppKit -import os - -/// The About panel's credits: the project links, then the license holders who asked to be thanked -/// by name. -enum AboutPanelCredits { - static func attributedString(supporters: [String] = SupporterList.load()) -> NSAttributedString { - let style: [NSAttributedString.Key: Any] = [ - .font: NSFont.systemFont(ofSize: 11), - .foregroundColor: NSColor.secondaryLabelColor - ] - let credits = NSMutableAttributedString() - let links: [(String, String)] = [ - ("Website", MainMenuLink.website), - ("GitHub", MainMenuLink.repository), - (String(localized: "Documentation"), MainMenuLink.documentation), - (String(localized: "Sponsor"), SupportLinks.sponsors(.aboutPanel).absoluteString) - ] - for (index, link) in links.enumerated() { - if index > 0 { - credits.append(NSAttributedString(string: " | ", attributes: style)) - } - let attributed = NSMutableAttributedString(string: link.0, attributes: style) - if let url = URL(string: link.1) { - attributed.addAttribute(.link, value: url, range: NSRange(location: 0, length: attributed.length)) - } - credits.append(attributed) - } - - if !supporters.isEmpty { - let heading = String(localized: "Thanks to these license holders") - credits.append(NSAttributedString(string: "\n\n\(heading)\n", attributes: style)) - credits.append(NSAttributedString(string: supporters.joined(separator: ", "), attributes: style)) - } - - let centered = NSMutableParagraphStyle() - centered.alignment = .center - credits.addAttribute(.paragraphStyle, value: centered, range: NSRange(location: 0, length: credits.length)) - return credits - } -} - -/// License holders who asked, by email, to be named in the About panel. -/// -/// `Supporters.json` in the app's resources is the only copy of the list. A name goes in, or comes -/// out, through a pull request, and only after its owner said yes, using the spelling they gave. -enum SupporterList { - static let resourceName = "Supporters" - - private struct File: Decodable { - let supporters: [String] - } - - private static let logger = Logger(subsystem: "com.TablePro", category: "SupporterList") - - static func load(from bundle: Bundle = .main) -> [String] { - guard let url = bundle.url(forResource: resourceName, withExtension: "json") else { - logger.error("Supporters.json is missing from the app bundle") - return [] - } - do { - return try decode(Data(contentsOf: url)) - } catch { - logger.error("Could not read Supporters.json: \(error.publicLogShape, privacy: .public)") - return [] - } - } - - static func decode(_ data: Data) throws -> [String] { - try JSONDecoder().decode(File.self, from: data).supporters - .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } - .filter { !$0.isEmpty } - } -} diff --git a/apple/TablePro/Core/Menu/AppDelegate+MainMenuActions.swift b/apple/TablePro/Core/Menu/AppDelegate+MainMenuActions.swift index 7f81715475..352d9e93a3 100644 --- a/apple/TablePro/Core/Menu/AppDelegate+MainMenuActions.swift +++ b/apple/TablePro/Core/Menu/AppDelegate+MainMenuActions.swift @@ -156,3 +156,35 @@ enum MainMenuLink { static let repository = "https://github.com/TableProApp/TablePro" static let issues = "https://github.com/TableProApp/TablePro/issues" } + +/// The About panel's credits line, kept out of the delegate so the menu action stays +/// a single call. +enum AboutPanelCredits { + static func attributedString() -> NSAttributedString { + let style: [NSAttributedString.Key: Any] = [ + .font: NSFont.systemFont(ofSize: 11), + .foregroundColor: NSColor.secondaryLabelColor + ] + let credits = NSMutableAttributedString() + let links: [(String, String)] = [ + ("Website", MainMenuLink.website), + ("GitHub", MainMenuLink.repository), + (String(localized: "Documentation"), MainMenuLink.documentation), + (String(localized: "Sponsor"), SupportLinks.sponsors(.aboutPanel).absoluteString) + ] + for (index, link) in links.enumerated() { + if index > 0 { + credits.append(NSAttributedString(string: " | ", attributes: style)) + } + let attributed = NSMutableAttributedString(string: link.0, attributes: style) + if let url = URL(string: link.1) { + attributed.addAttribute(.link, value: url, range: NSRange(location: 0, length: attributed.length)) + } + credits.append(attributed) + } + let centered = NSMutableParagraphStyle() + centered.alignment = .center + credits.addAttribute(.paragraphStyle, value: centered, range: NSRange(location: 0, length: credits.length)) + return credits + } +} diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 2400d04c20..7712f107eb 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -182092,46 +182092,6 @@ } } }, - "Thanks to these license holders" : { - "localizations" : { - "fr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Merci à ces détenteurs de licence" - } - }, - "ko" : { - "stringUnit" : { - "state" : "translated", - "value" : "라이선스를 구매해 주신 분들께 감사드립니다" - } - }, - "tr" : { - "stringUnit" : { - "state" : "translated", - "value" : "Bu lisans sahiplerine teşekkürler" - } - }, - "vi" : { - "stringUnit" : { - "state" : "translated", - "value" : "Cảm ơn những người đã mua giấy phép" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "感谢这些许可证持有者" - } - }, - "zh-Hant" : { - "stringUnit" : { - "state" : "translated", - "value" : "感謝這些授權持有者" - } - } - } - }, "Thanks. Your license is what pays for the next release." : { "localizations" : { "fr" : { diff --git a/apple/TablePro/Resources/Supporters.json b/apple/TablePro/Resources/Supporters.json deleted file mode 100644 index 75d509d60e..0000000000 --- a/apple/TablePro/Resources/Supporters.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "supporters": [] -} diff --git a/apple/TableProTests/Core/Menu/AboutPanelCreditsTests.swift b/apple/TableProTests/Core/Menu/AboutPanelCreditsTests.swift deleted file mode 100644 index 0dbdad099f..0000000000 --- a/apple/TableProTests/Core/Menu/AboutPanelCreditsTests.swift +++ /dev/null @@ -1,44 +0,0 @@ -// -// AboutPanelCreditsTests.swift -// TableProTests -// - -import Foundation -@testable import TablePro -import Testing - -@MainActor -struct AboutPanelCreditsTests { - @Test("Named license holders follow the project links") - func listsSupporters() throws { - let credits = AboutPanelCredits.attributedString(supporters: ["Ada Lovelace", "Grace Hopper"]).string - - #expect(credits.contains("Thanks to these license holders")) - #expect(credits.contains("Ada Lovelace, Grace Hopper")) - let links = try #require(credits.range(of: "GitHub")) - let names = try #require(credits.range(of: "Ada Lovelace")) - #expect(links.lowerBound < names.lowerBound) - } - - @Test("With nobody listed, the credits stay the project links alone") - func emptyListAddsNothing() { - let credits = AboutPanelCredits.attributedString(supporters: []).string - - #expect(!credits.contains("Thanks to these license holders")) - #expect(!credits.contains("\n")) - } - - @Test("Blank entries and stray spaces in the list never reach the panel") - func decodeTrimsAndDropsBlanks() throws { - let data = Data(#"{"supporters": [" Ada Lovelace ", "", " ", "Grace Hopper"]}"#.utf8) - - #expect(try SupporterList.decode(data) == ["Ada Lovelace", "Grace Hopper"]) - } - - @Test("The list the app ships decodes") - func bundledListDecodes() throws { - let url = try #require(Bundle.main.url(forResource: SupporterList.resourceName, withExtension: "json")) - - _ = try SupporterList.decode(Data(contentsOf: url)) - } -} diff --git a/apple/changelog.d/3923.added.md b/apple/changelog.d/3923.added.md deleted file mode 100644 index 09d17d7b1c..0000000000 --- a/apple/changelog.d/3923.added.md +++ /dev/null @@ -1 +0,0 @@ -The About panel thanks, by name, license holders who asked to be listed. From b937c1f3025f512189e1475e3b024fefa1edafbf Mon Sep 17 00:00:00 2001 From: Cristian Date: Sat, 31 Oct 2026 08:27:49 +0100 Subject: [PATCH 35/85] feat(linux-editor): move a PostgreSQL query tab to another database by opening its session there (#3899) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3899.added.md | 1 + .../app/src/ui/editor/editor_session_slot.rs | 160 +++++++++++++++++- linux/crates/core/src/editor_session.rs | 4 + .../crates/drivers/postgres/src/connection.rs | 13 +- .../postgres/src/editor/editor_session.rs | 8 + .../drivers/postgres/src/metadata/mod.rs | 2 +- .../drivers/postgres/src/metadata/objects.rs | 26 +++ linux/crates/drivers/postgres/src/physical.rs | 16 ++ .../tests/integration/editor_session.rs | 44 +++++ linux/crates/mcp/src/query_tools.rs | 1 + .../src/conformance/cases/document.rs | 1 + .../src/conformance/cases/keyspace.rs | 1 + 12 files changed, 270 insertions(+), 7 deletions(-) create mode 100644 linux/changelog.d/3899.added.md diff --git a/linux/changelog.d/3899.added.md b/linux/changelog.d/3899.added.md new file mode 100644 index 0000000000..2a9eb85047 --- /dev/null +++ b/linux/changelog.d/3899.added.md @@ -0,0 +1 @@ +On PostgreSQL, the SQL editor's database menu moves a tab to another of the server's databases by opening its connection there. diff --git a/linux/crates/app/src/ui/editor/editor_session_slot.rs b/linux/crates/app/src/ui/editor/editor_session_slot.rs index e0be235a77..941cc6ef90 100644 --- a/linux/crates/app/src/ui/editor/editor_session_slot.rs +++ b/linux/crates/app/src/ui/editor/editor_session_slot.rs @@ -88,9 +88,12 @@ impl EditorSessionSlot { close_timeout: Duration, cancel: &CancellationToken, ) -> Result, AcquireError> { - let deadline = statement_timeout.map(|timeout| tokio::time::Instant::now() + timeout); let stale = match self.lookup(session) { - Lookup::Ready(editor) => return self.reach_database(session, editor, deadline, cancel).await, + Lookup::Ready(editor) => { + return self + .reach_database(session, editor, (statement_timeout, close_timeout), cancel) + .await; + } Lookup::Closed => return Err(AcquireError::Cancelled), Lookup::RolledBack { stale } => { if let Some(stale) = stale { @@ -103,10 +106,16 @@ impl EditorSessionSlot { if let Some(stale) = stale { close_quietly(stale, close_timeout).await; } - let options = EditorSessionOptions { statement_timeout }; + let options = EditorSessionOptions { + statement_timeout, + database: self.database(), + }; match session.open_editor_session(options, cancel).await { Ok(editor) => match self.adopt(session, editor.clone()) { - Ok(()) => self.reach_database(session, editor, deadline, cancel).await, + Ok(()) => { + self.reach_database(session, editor, (statement_timeout, close_timeout), cancel) + .await + } Err(orphan) => { close_quietly(orphan, close_timeout).await; Err(AcquireError::Cancelled) @@ -145,9 +154,10 @@ impl EditorSessionSlot { &self, session: &Arc, editor: Arc, - deadline: Option, + (statement_timeout, close_timeout): (Option, Duration), cancel: &CancellationToken, ) -> Result, AcquireError> { + let deadline = statement_timeout.map(|timeout| tokio::time::Instant::now() + timeout); let Some(wanted) = self.database() else { return Ok(editor); }; @@ -161,6 +171,17 @@ impl EditorSessionSlot { { Ok(()) => Ok(editor), Err(tablepro_core::DriverError::Cancelled) => Err(AcquireError::Cancelled), + // The engine binds a session's database when it connects. + Err(tablepro_core::DriverError::Unsupported { .. }) => { + self.reopen_in( + session, + editor, + (wanted, here), + (statement_timeout, close_timeout), + cancel, + ) + .await + } Err(error) => { lock(&self.state).database = here; Err(AcquireError::Moved { @@ -171,6 +192,58 @@ impl EditorSessionSlot { } } + /// Give up `editor` for a session opened in `wanted`. One holding a + /// transaction stays: closing it would roll the transaction back. + async fn reopen_in( + &self, + session: &Arc, + editor: Arc, + (wanted, here): (String, Option), + (statement_timeout, close_timeout): (Option, Duration), + cancel: &CancellationToken, + ) -> Result, AcquireError> { + let holds_transaction = lock(&self.state) + .open + .as_ref() + .is_some_and(|open| open.editor.id() == editor.id() && open.held_transaction()); + if holds_transaction { + lock(&self.state).database = here; + return Err(AcquireError::Moved { + database: wanted, + reason: crate::i18n::gettext("Commit or roll back the transaction first."), + }); + } + // The session in the old database stays until one opens in the + // new, so a move that fails keeps what the tab built there. + let options = EditorSessionOptions { + statement_timeout, + database: Some(wanted.clone()), + }; + let opened = match session.open_editor_session(options, cancel).await { + Ok(opened) => opened, + Err(error) => { + lock(&self.state).database = here; + return Err(match error { + OperationError::Cancelled => AcquireError::Cancelled, + error => AcquireError::Moved { + database: wanted, + reason: crate::ui::error_text::operation_error_message(&error), + }, + }); + } + }; + match self.adopt(session, opened.clone()) { + Ok(()) => { + close_quietly(editor, close_timeout).await; + Ok(opened) + } + Err(orphan) => { + close_quietly(orphan, close_timeout).await; + Err(AcquireError::Cancelled) + } + } + } + fn lookup(&self, owner: &Arc) -> Lookup { let mut state = lock(&self.state); if state.closed { @@ -329,6 +402,83 @@ pub(super) mod tests { const CLOSE: Duration = Duration::from_secs(10); + /// A session in `database` that cannot move while it lives, as on + /// PostgreSQL. + fn bound_to(database: &str) -> Arc { + let editor = answering(); + editor.set_database(Some(database)); + editor.refuse_moves(tablepro_core::DriverError::Unsupported { + feature: tablepro_core::UnsupportedFeature::DatabaseSwitching, + }); + editor + } + + /// A session that cannot open in the new database leaves the tab in its + /// own, on the session it had and what it built there. + #[tokio::test] + async fn a_move_that_cannot_open_keeps_the_session_it_had() { + let shop = bound_to("shop"); + let (session, _) = session_opening(vec![shop.clone()]); + let slot = EditorSessionSlot::new(); + let first = slot + .acquire(&session, None, CLOSE, &CancellationToken::new()) + .await + .expect("a session in shop"); + + slot.set_database("analytics".to_owned()); + let refused = slot.acquire(&session, None, CLOSE, &CancellationToken::new()).await; + assert!( + matches!(refused, Err(AcquireError::Moved { .. })), + "{:?}", + refused.err() + ); + assert_eq!(shop.close_count(), 0, "the session in shop closed"); + assert_eq!(slot.database().as_deref(), Some("shop")); + + let again = slot + .acquire(&session, None, CLOSE, &CancellationToken::new()) + .await + .expect("the session in shop"); + assert_eq!(again.id(), first.id()); + } + + /// Where a live session cannot move, the tab gets a session opened in + /// its database, unless the one it has holds a transaction. + #[tokio::test] + async fn a_session_that_cannot_move_gives_way_unless_it_holds_a_transaction() { + let (shop, analytics) = (bound_to("shop"), bound_to("analytics")); + let (session, connection) = session_opening(vec![shop.clone(), analytics.clone()]); + let slot = EditorSessionSlot::new(); + let first = slot + .acquire(&session, None, CLOSE, &CancellationToken::new()) + .await + .expect("a session in shop"); + slot.note_transaction(&first, Some(TransactionState::InTransaction)); + + slot.set_database("analytics".to_owned()); + let refused = slot.acquire(&session, None, CLOSE, &CancellationToken::new()).await; + assert!( + matches!(refused, Err(AcquireError::Moved { .. })), + "{:?}", + refused.err() + ); + assert_eq!(slot.database().as_deref(), Some("shop"), "the tab left its transaction"); + assert_eq!(shop.close_count(), 0); + + slot.note_transaction(&first, Some(TransactionState::Idle)); + slot.set_database("analytics".to_owned()); + let moved = slot + .acquire(&session, None, CLOSE, &CancellationToken::new()) + .await + .expect("a session in analytics"); + assert_eq!(moved.database().as_deref(), Some("analytics")); + assert_eq!(shop.close_count(), 1, "the session in shop stayed open"); + assert_eq!( + tablepro_test_support::CallCounters::get(&connection.counters().open_editor_session), + 2 + ); + } + #[tokio::test] async fn a_tab_opens_its_editor_session_once_and_reuses_it() { let editor = answering(); diff --git a/linux/crates/core/src/editor_session.rs b/linux/crates/core/src/editor_session.rs index ea2d6d1662..670d05dabb 100644 --- a/linux/crates/core/src/editor_session.rs +++ b/linux/crates/core/src/editor_session.rs @@ -43,6 +43,10 @@ pub enum EditorSessionState { #[derive(Debug, Clone, Default)] pub struct EditorSessionOptions { pub statement_timeout: Option, + /// The database to open the session in, where the engine binds a + /// session's database when it connects. Elsewhere the session opens in + /// the connection's and `EditorSession::use_database` moves it. + pub database: Option, } /// One editor tab's own connection to the server. diff --git a/linux/crates/drivers/postgres/src/connection.rs b/linux/crates/drivers/postgres/src/connection.rs index 44045d4b30..015d920bf8 100644 --- a/linux/crates/drivers/postgres/src/connection.rs +++ b/linux/crates/drivers/postgres/src/connection.rs @@ -254,6 +254,11 @@ impl Connection for PgConnection { outcome } + async fn list_databases(&self, options: CallOptions) -> Result, DriverError> { + self.read(options, read_op(|client| Box::pin(metadata::list_databases(client)))) + .await + } + async fn list_schemas(&self, options: CallOptions) -> Result, DriverError> { self.read(options, read_op(|client| Box::pin(metadata::list_schemas(client)))) .await @@ -462,7 +467,13 @@ impl Connection for PgConnection { cancel: CancellationToken, ) -> Result, DriverError> { self.ensure_open()?; - let session = PgEditorSession::open(self.endpoint.clone(), self.settings.read_only, options, cancel).await?; + // A connection stays in the database it opened in, so a tab moved + // to another one gets a session opened there. + let endpoint = match options.database.as_deref() { + Some(database) => Arc::new(self.endpoint.in_database(database)), + None => self.endpoint.clone(), + }; + let session = PgEditorSession::open(endpoint, self.settings.read_only, options, cancel).await?; Ok(session) } diff --git a/linux/crates/drivers/postgres/src/editor/editor_session.rs b/linux/crates/drivers/postgres/src/editor/editor_session.rs index 44afb469f1..7df7283131 100644 --- a/linux/crates/drivers/postgres/src/editor/editor_session.rs +++ b/linux/crates/drivers/postgres/src/editor/editor_session.rs @@ -24,6 +24,8 @@ use crate::physical::PgEndpoint; /// are enforced with cancel requests. pub(crate) struct PgEditorSession { id: EditorSessionId, + /// A connection stays in the database it opened in. + database: Option, commands: mpsc::Sender, status: Arc, shutdown: CancellationToken, @@ -39,6 +41,7 @@ impl PgEditorSession { cancel: CancellationToken, ) -> Result, DriverError> { let (client, connection) = endpoint.open(&cancel).await?; + let database = endpoint.database().map(str::to_owned); let close_timeout = endpoint.liveness().close_timeout; let (notice_sender, notices) = mpsc::unbounded_channel(); // PostgreSQL has defaulted to on since 9.1; the server reports @@ -68,6 +71,7 @@ impl PgEditorSession { )); Ok(Arc::new(Self { id: EditorSessionId::new(), + database, commands, status, shutdown, @@ -87,6 +91,10 @@ impl EditorSession for PgEditorSession { self.status.state() } + fn database(&self) -> Option { + self.database.clone() + } + async fn run( &self, sql: UserSql, diff --git a/linux/crates/drivers/postgres/src/metadata/mod.rs b/linux/crates/drivers/postgres/src/metadata/mod.rs index 767638bd5c..b7ad20caac 100644 --- a/linux/crates/drivers/postgres/src/metadata/mod.rs +++ b/linux/crates/drivers/postgres/src/metadata/mod.rs @@ -12,7 +12,7 @@ pub(crate) use estimate::table_estimate; pub(crate) use foreign_keys::{fetch_foreign_keys, fetch_outbound_foreign_keys}; pub(crate) use identity::row_identity; pub(crate) use indexes::fetch_indexes; -pub(crate) use objects::{list_objects, list_schemas}; +pub(crate) use objects::{list_databases, list_objects, list_schemas}; use futures_util::TryStreamExt; use postgres_types::{ToSql, Type}; diff --git a/linux/crates/drivers/postgres/src/metadata/objects.rs b/linux/crates/drivers/postgres/src/metadata/objects.rs index 4282d7982f..bd08c91b1b 100644 --- a/linux/crates/drivers/postgres/src/metadata/objects.rs +++ b/linux/crates/drivers/postgres/src/metadata/objects.rs @@ -50,6 +50,32 @@ pub(crate) async fn list_schemas(client: &Client) -> Result, Dri .collect() } +/// The databases the login may connect to, the connection's own the +/// default. `template0` takes no connections; `template1` and `postgres` +/// are the server's own. +const DATABASES_SQL: &str = "\ +SELECT d.datname::text, + d.datname = current_database(), + d.datistemplate OR d.datname = 'postgres' +FROM pg_catalog.pg_database d +WHERE d.datallowconn AND pg_catalog.has_database_privilege(d.datname, 'CONNECT') +ORDER BY d.datname"; + +pub(crate) async fn list_databases(client: &Client) -> Result, DriverError> { + let field = decode_error("pg_database"); + text_rows(client, DATABASES_SQL, &[]) + .await? + .iter() + .map(|row| { + Ok(SchemaInfo { + name: row.try_get(0).map_err(&field)?, + is_default: row.try_get(1).map_err(&field)?, + is_system: row.try_get(2).map_err(&field)?, + }) + }) + .collect() +} + pub(crate) async fn list_objects(client: &Client, scope: &ObjectScope) -> Result, DriverError> { let (mode, schema) = match scope { ObjectScope::DefaultSchema => ("default", ""), diff --git a/linux/crates/drivers/postgres/src/physical.rs b/linux/crates/drivers/postgres/src/physical.rs index 0a03010a40..216443a778 100644 --- a/linux/crates/drivers/postgres/src/physical.rs +++ b/linux/crates/drivers/postgres/src/physical.rs @@ -56,6 +56,22 @@ impl PgEndpoint { }) } + /// The same server, login and TLS, opening connections in `database`. + pub(crate) fn in_database(&self, database: &str) -> Self { + let mut config = self.config.clone(); + config.dbname(database); + Self { + transport: self.transport.clone(), + config, + tls: self.tls.clone(), + liveness: self.liveness, + } + } + + pub(crate) fn database(&self) -> Option<&str> { + self.config.get_dbname() + } + pub(crate) fn liveness(&self) -> &LivenessPolicy { &self.liveness } diff --git a/linux/crates/drivers/postgres/tests/integration/editor_session.rs b/linux/crates/drivers/postgres/tests/integration/editor_session.rs index 058eca484d..63eb936270 100644 --- a/linux/crates/drivers/postgres/tests/integration/editor_session.rs +++ b/linux/crates/drivers/postgres/tests/integration/editor_session.rs @@ -526,3 +526,47 @@ async fn editor_deadline_times_out_with_the_server_acknowledging() { assert_eq!(session.state(), EditorSessionState::Ready); run_ok(session.as_ref(), "SELECT 1").await; } + +/// A tab moved to another database: the server lists it, and a session +/// opened there runs in it, since a connection never leaves the database +/// it opened in. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn a_session_opened_in_another_database_runs_there() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + + let databases = connection.list_databases(within(CALL)).await.expect("the databases"); + let own = databases + .iter() + .find(|database| database.is_default) + .expect("the connection's database"); + assert!(!own.is_system, "{own:?}"); + let postgres = databases + .iter() + .find(|database| database.name == "postgres") + .expect("postgres"); + assert!(postgres.is_system, "{postgres:?}"); + assert!( + databases.iter().all(|database| database.name != "template0"), + "{databases:?}" + ); + + let options = tablepro_core::EditorSessionOptions { + statement_timeout: None, + database: Some("postgres".to_owned()), + }; + let elsewhere = connection + .open_editor_session(options, CancellationToken::new()) + .await + .expect("a session in postgres"); + let (_, events) = run(elsewhere.as_ref(), "SELECT current_database()").await; + assert_eq!(texts(&events), ["postgres"]); + assert_eq!(elsewhere.database().as_deref(), Some("postgres")); + let moved = elsewhere.use_database("tablepro", within(CALL)).await; + assert!( + matches!(moved, Err(tablepro_core::DriverError::Unsupported { .. })), + "a live session moved: {moved:?}" + ); + elsewhere.close().await; +} diff --git a/linux/crates/mcp/src/query_tools.rs b/linux/crates/mcp/src/query_tools.rs index 912f253774..bbdb656970 100644 --- a/linux/crates/mcp/src/query_tools.rs +++ b/linux/crates/mcp/src/query_tools.rs @@ -184,6 +184,7 @@ async fn run_read(session: &Session, text: &str, limit: usize, timeout: Duration async fn run_write(session: &Session, text: &str, limit: usize, timeout: Duration, cancel: &CancellationToken) -> Ran { let options = EditorSessionOptions { statement_timeout: Some(timeout), + database: None, }; let editor = session .open_editor_session(options, cancel) diff --git a/linux/crates/test-support/src/conformance/cases/document.rs b/linux/crates/test-support/src/conformance/cases/document.rs index eec2476dc2..a38fab090b 100644 --- a/linux/crates/test-support/src/conformance/cases/document.rs +++ b/linux/crates/test-support/src/conformance/cases/document.rs @@ -605,6 +605,7 @@ pub async fn a_timed_out_statement_is_stopped_by_the_server( let table = filled(TIMEOUT_CASE, &engine, connection.as_ref(), 50).await?; let options = EditorSessionOptions { statement_timeout: Some(SETTLE), + database: None, }; let session = connection .open_editor_session(options, CancellationToken::new()) diff --git a/linux/crates/test-support/src/conformance/cases/keyspace.rs b/linux/crates/test-support/src/conformance/cases/keyspace.rs index ea14571853..f0edded84d 100644 --- a/linux/crates/test-support/src/conformance/cases/keyspace.rs +++ b/linux/crates/test-support/src/conformance/cases/keyspace.rs @@ -340,6 +340,7 @@ pub async fn a_timed_out_command_is_stopped_by_the_server( let connection = connect(TIMEOUT_CASE, &engine, writable()).await?; let options = EditorSessionOptions { statement_timeout: Some(SETTLE), + database: None, }; let session = editor(connection.as_ref(), options).await?; From 0038cba311d605f6db1010c4508753726e6fd7d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sat, 31 Oct 2026 14:46:25 +0700 Subject: [PATCH 36/85] chore(ios): bump to 1.1.1 (24) (#3929) --- apple/Configs/Version-iOS.xcconfig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apple/Configs/Version-iOS.xcconfig b/apple/Configs/Version-iOS.xcconfig index 705cfb6c43..09f58d5e70 100644 --- a/apple/Configs/Version-iOS.xcconfig +++ b/apple/Configs/Version-iOS.xcconfig @@ -1,5 +1,5 @@ // Single source of truth for the iOS app version. // The app and its widget extension must ship the same values, so both read this file. -MARKETING_VERSION = 1.1 -CURRENT_PROJECT_VERSION = 23 +MARKETING_VERSION = 1.1.1 +CURRENT_PROJECT_VERSION = 24 From 5fd3a352df4293201c9aa2b1bfca79192938443d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sat, 31 Oct 2026 14:57:44 +0700 Subject: [PATCH 37/85] fix(linux-grid): count the sort arrow in a header's width and measure columns again when a font changes (#3927) --- linux/changelog.d/3927.fixed.1.md | 1 + linux/changelog.d/3927.fixed.md | 1 + linux/crates/app/src/ui/data_grid/columns.rs | 125 +++++++++++++--- linux/crates/app/src/ui/data_grid/imp.rs | 14 ++ linux/crates/app/src/ui/data_grid/mod.rs | 1 + .../app/src/ui/data_grid/row_number_column.rs | 4 +- .../app/src/ui/data_grid/tests/columns.rs | 134 +++++++++++++++++- 7 files changed, 257 insertions(+), 23 deletions(-) create mode 100644 linux/changelog.d/3927.fixed.1.md create mode 100644 linux/changelog.d/3927.fixed.md diff --git a/linux/changelog.d/3927.fixed.1.md b/linux/changelog.d/3927.fixed.1.md new file mode 100644 index 0000000000..2c49b076f6 --- /dev/null +++ b/linux/changelog.d/3927.fixed.1.md @@ -0,0 +1 @@ +Changing the font or text size while a data grid was open left its columns at their old widths. diff --git a/linux/changelog.d/3927.fixed.md b/linux/changelog.d/3927.fixed.md new file mode 100644 index 0000000000..6650bd703d --- /dev/null +++ b/linux/changelog.d/3927.fixed.md @@ -0,0 +1 @@ +A data grid column sized by its header could cut the header's sort arrow. diff --git a/linux/crates/app/src/ui/data_grid/columns.rs b/linux/crates/app/src/ui/data_grid/columns.rs index 65d7fd4200..a634886d12 100644 --- a/linux/crates/app/src/ui/data_grid/columns.rs +++ b/linux/crates/app/src/ui/data_grid/columns.rs @@ -16,6 +16,7 @@ use crate::services::grid::{ CellDisplay, ColumnLayout, GridColumn, LayoutSlot, Placement, default_width_chars, header_text, merge_order, number_width_chars, sampled_texts, typical_texts, typical_width_chars, }; +use crate::ui::handler_guard::HandlerGuard; /// Rows sampled for a column's opening width. const WIDTH_SAMPLE: usize = 200; @@ -25,6 +26,21 @@ const CELL_CHROME_PX: i32 = 13; /// A resize step, in digits of the font the cells draw in, so a step is /// the same number of characters at any text size. const STEP_DIGITS: i32 = 2; +/// Adwaita draws a header's title in `.caption-heading`: bold, at 82 % of +/// the grid's font. +const HEADER_SCALE: f64 = 0.82; +/// GTK keeps a sort indicator beside the title of every sortable column, +/// sorted or not: 16 px wide in Adwaita, 6 px from the title. +const SORT_INDICATOR_PX: i32 = 16 + 6; + +/// Whether measuring may narrow a column the grid sized itself. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Measure { + /// More rows only widen it, so it holds still as pages arrive. + Widen, + /// A font changed, so the width it had means nothing now. + Again, +} /// What a column's cells read to draw a row. The column makes one and /// every cell it builds shares it. @@ -66,6 +82,8 @@ pub struct ColumnSet { /// keep: a better guess from other rows should still reach them. auto_widths: std::collections::HashMap, pages: Pages, + /// The fonts the grid last sized its columns in. + measured_in: Option, handlers: Vec<(glib::WeakRef, glib::SignalHandlerId)>, } @@ -234,20 +252,55 @@ impl DataGrid { /// the first rows, and keeps it while rows scroll by, since an /// automatic width measures every row GTK builds and shifts. fn size_unsized_columns(&self) { - let widths = self.opening_widths(); + self.size_columns(Measure::Widen); + } + + /// The monospace font, the interface font or the text size changed: + /// the columns the grid sized itself take the new measure, and a width + /// the user gave stays. + pub(super) fn fonts_changed(&self) { + self.size_columns(Measure::Again); + row_number_column::fit(self); + } + + /// A grid out of any window hears of no font change, and one sized + /// before it had a window measured in no theme's font. + pub(super) fn rooted(&self) { + let measured_in = self.imp().columns.borrow().measured_in.clone(); + if measured_in.is_some_and(|fonts| fonts != fonts_now(&self.column_view())) { + self.fonts_changed(); + } + } + + /// The interface font and the text size reach the grid as GTK system + /// settings; the monospace font is libadwaita's. + pub(super) fn watch_fonts(&self) { + let style = adw::StyleManager::default(); + let handler = style.connect_monospace_font_name_notify(glib::clone!( + #[weak(rename_to = grid)] + self, + move |_| grid.fonts_changed() + )); + self.imp().font_watch.replace(Some(HandlerGuard::new(&style, handler))); + } + + fn size_columns(&self, measure: Measure) { + let widths = self.opening_widths(measure); + self.imp().columns.borrow_mut().measured_in = Some(fonts_now(&self.column_view())); for (index, view_column, width) in widths { self.imp().columns.borrow_mut().auto_widths.insert(index, width); view_column.set_fixed_width(width); } } - fn opening_widths(&self) -> Vec<(usize, gtk::ColumnViewColumn, i32)> { + fn opening_widths(&self, measure: Measure) -> Vec<(usize, gtk::ColumnViewColumn, i32)> { let set = self.imp().columns.borrow(); let sample: Vec>> = self.store().rows().take(WIDTH_SAMPLE).map(|row| row.cells()).collect(); let by_type = set.pages == Pages::Empty; let view = self.column_view(); - let font = value_font(&view); - let metrics = view.pango_context().metrics(Some(&font), None); + let context = view.create_pango_context(); + let font = value_font(&context); + let metrics = context.metrics(Some(&font), None); let char_px = pango::units_to_double(metrics.approximate_char_width()); let null = editable_null_sentinel(); let mut widths = Vec::new(); @@ -272,31 +325,37 @@ impl DataGrid { // no rows a value its type spells out, as a UUID or an enum's // label, so those are measured as the cell draws them. let sampled = match (by_type, paint.numbers.get()) { - (_, true) => tabular_width(&view, &font, &"0".repeat(usize::try_from(chars).unwrap_or_default())), + (_, true) => tabular_width(&context, &font, &"0".repeat(usize::try_from(chars).unwrap_or_default())), (true, false) => 0, // One line each, so one layout measures them all. - (false, false) => drawn_width(&view, &font, &sampled_texts(column, values.iter().copied()).join("\n")), + (false, false) => drawn_width( + &context, + &font, + &sampled_texts(column, values.iter().copied()).join("\n"), + ), }; let spelled = match by_type || column.display() == CellDisplay::Temporal { true => typical_texts(column) .iter() - .map(|typical| tabular_width(&view, &font, typical)) + .map(|typical| tabular_width(&context, &font, typical)) .max() .unwrap_or(0), false => 0, }; - // The header draws its title smaller than the body font, so the - // body font's width holds it. - let header = view.create_pango_layout(Some(header_text(column))).pixel_size().0; + let indicator = match view_column.sorter() { + Some(_) => SORT_INDICATOR_PX, + None => 0, + }; + let header = header_width(&context, header_text(column)) + indicator; // A NULL the user can replace draws as ``, wider than the // fewest cells a column takes. let placeholder = match paint.tracked.tracks_changes() && values.iter().any(|value| value.is_null()) { - true => drawn_width(&view, &font, &null), + true => drawn_width(&context, &font, &null), false => 0, }; let text = estimate.max(sampled).max(spelled).max(header).max(placeholder); let width = text + CELL_CHROME_PX; - if auto.is_none_or(|auto| width > auto) { + if measure == Measure::Again || auto.is_none_or(|auto| width > auto) { widths.push((index, view_column.clone(), width)); } } @@ -493,7 +552,8 @@ impl DataGrid { return; }; let view = self.column_view(); - let metrics = view.pango_context().metrics(Some(&value_font(&view)), None); + let context = view.create_pango_context(); + let metrics = context.metrics(Some(&value_font(&context)), None); let step = (pango::units_to_double(metrics.approximate_digit_width()) * f64::from(STEP_DIGITS)).ceil() as i32; let width = column.fixed_width(); let width = match wider { @@ -721,18 +781,44 @@ fn same_names(layout: &ColumnLayout, columns: &[GridColumn]) -> bool { /// The font a cell draws its value in: libadwaita's `monospace` class /// sets only the family and size of the desktop's monospace font. -fn value_font(view: >k::ColumnView) -> pango::FontDescription { +fn value_font(context: &pango::Context) -> pango::FontDescription { let mut monospace = pango::FontDescription::from_string(&adw::StyleManager::default().monospace_font_name()); monospace.unset_fields(!(pango::FontMask::FAMILY | pango::FontMask::SIZE)); - let mut font = view.pango_context().font_description().unwrap_or_default(); + let mut font = context.font_description().unwrap_or_default(); font.merge(Some(&monospace), true); font } +/// How wide a header draws `title`. The scaled size rounds up, since GTK +/// scales the size before it rounds and the view's font is rounded. +fn header_width(context: &pango::Context, title: &str) -> i32 { + let mut font = context.font_description().unwrap_or_default(); + font.set_weight(pango::Weight::Bold); + let size = (f64::from(font.size()) * HEADER_SCALE).ceil(); + match font.is_size_absolute() { + true => font.set_absolute_size(size), + false => font.set_size(size as i32), + } + drawn_width(context, &font, title) +} + +/// The grid's font, the text size in it, and the monospace font. A +/// widget's own context follows a change only at the next frame, so a +/// fresh one is read. +fn fonts_now(view: >k::ColumnView) -> String { + let grid = view + .create_pango_context() + .font_description() + .map(|font| font.to_string()) + .unwrap_or_default(); + format!("{grid}; {}", adw::StyleManager::default().monospace_font_name()) +} + /// How wide `text` is in the tabular figures libadwaita's `numeric` /// class draws. -fn tabular_width(widget: &impl IsA, font: &pango::FontDescription, text: &str) -> i32 { - let layout = widget.create_pango_layout(Some(text)); +fn tabular_width(context: &pango::Context, font: &pango::FontDescription, text: &str) -> i32 { + let layout = pango::Layout::new(context); + layout.set_text(text); layout.set_font_description(Some(font)); let tabular = pango::AttrList::new(); tabular.insert(pango::AttrFontFeatures::new("tnum")); @@ -740,8 +826,9 @@ fn tabular_width(widget: &impl IsA, font: &pango::FontDescription, layout.pixel_size().0 } -fn drawn_width(widget: &impl IsA, font: &pango::FontDescription, text: &str) -> i32 { - let layout = widget.create_pango_layout(Some(text)); +fn drawn_width(context: &pango::Context, font: &pango::FontDescription, text: &str) -> i32 { + let layout = pango::Layout::new(context); + layout.set_text(text); layout.set_font_description(Some(font)); layout.pixel_size().0 } diff --git a/linux/crates/app/src/ui/data_grid/imp.rs b/linux/crates/app/src/ui/data_grid/imp.rs index 9720e464cd..d3c16c7de9 100644 --- a/linux/crates/app/src/ui/data_grid/imp.rs +++ b/linux/crates/app/src/ui/data_grid/imp.rs @@ -18,6 +18,7 @@ use super::row_detail::RowDetailPane; use super::selection::Target; use super::selection_summary::SummaryState; use super::{GridConfig, GridStore}; +use crate::ui::handler_guard::HandlerGuard; /// The grid's selection model, made with the grid and given its rows once /// the grid knows how it sorts. @@ -85,6 +86,7 @@ pub struct DataGrid { pub editing: Cell, /// The window's focus handler, while the grid is in a window. pub focus_watch: RefCell, glib::SignalHandlerId)>>, + pub(super) font_watch: RefCell>, } #[glib::object_subclass] @@ -122,6 +124,7 @@ impl ObjectImpl for DataGrid { self.find.stop(); self.summary.stop(); self.obj().stop_watching_focus(); + drop(self.font_watch.take()); if let Some(menus) = self.menus.get() { menus.unparent(); } @@ -134,12 +137,23 @@ impl WidgetImpl for DataGrid { fn root(&self) { self.parent_root(); self.obj().watch_focus(); + self.obj().rooted(); } fn unroot(&self) { self.obj().stop_watching_focus(); self.parent_unroot(); } + + fn system_setting_changed(&self, setting: >k4::SystemSetting) { + self.parent_system_setting_changed(setting); + if matches!( + setting, + gtk4::SystemSetting::Dpi | gtk4::SystemSetting::FontName | gtk4::SystemSetting::FontConfig + ) { + self.obj().fonts_changed(); + } + } } impl BinImpl for DataGrid {} diff --git a/linux/crates/app/src/ui/data_grid/mod.rs b/linux/crates/app/src/ui/data_grid/mod.rs index 47e2ca9bde..4f445f4c57 100644 --- a/linux/crates/app/src/ui/data_grid/mod.rs +++ b/linux/crates/app/src/ui/data_grid/mod.rs @@ -87,6 +87,7 @@ impl DataGrid { find_bar::install(&grid); imp.detail_pane.attach(&grid); grid.set_columns(columns, sorting, rules); + grid.watch_fonts(); grid } diff --git a/linux/crates/app/src/ui/data_grid/row_number_column.rs b/linux/crates/app/src/ui/data_grid/row_number_column.rs index 6edba56555..3e31735c1d 100644 --- a/linux/crates/app/src/ui/data_grid/row_number_column.rs +++ b/linux/crates/app/src/ui/data_grid/row_number_column.rs @@ -139,7 +139,9 @@ pub fn fit(grid: &DataGrid) { }; let largest = grid.row_base() + u64::from(grid.store().len()); let digits = largest.max(1).ilog10() as i32 + 1; - let metrics = grid.column_view().pango_context().metrics(None, None); + // A fresh context, since the view's own follows a font change only at + // the next frame. + let metrics = grid.column_view().create_pango_context().metrics(None, None); let digit_px = pango::units_to_double(metrics.approximate_digit_width()); column.set_fixed_width((f64::from(digits.max(2)) * digit_px).ceil() as i32 + CHROME_PX); } diff --git a/linux/crates/app/src/ui/data_grid/tests/columns.rs b/linux/crates/app/src/ui/data_grid/tests/columns.rs index 38c5338391..ec11131ac6 100644 --- a/linux/crates/app/src/ui/data_grid/tests/columns.rs +++ b/linux/crates/app/src/ui/data_grid/tests/columns.rs @@ -438,18 +438,146 @@ fn a_header_of_wide_letters_shows_whole() { vec![named("id", "integer", true), named("WWWWWWWW", "text", false)], vec![vec![Value::Int(1), text("a")]], )); + let (header, title) = header_title(&grid, "WWWWWWWW"); + let (_, needs, _, _) = title.measure(gtk::Orientation::Horizontal, -1); + + wait_until(WAIT, || header.width() >= needs).expect("the header's title shows whole"); +} + +/// GTK keeps a sort indicator beside the title of every sortable column, +/// so a column as wide as its title alone cuts the arrow once it sorts. +#[gtk4::test] +fn a_sorted_columns_header_shows_its_title_and_arrow_whole() { + let grid = laid_out(Grid::open( + vec![named("id", "integer", true), named("level", "text", false)], + vec![vec![Value::Int(1), text("a")]], + )); + grid.grid.sort_by(Some((1, true))); + let (header, title) = header_title(&grid, "level"); + let title_and_arrow = title.parent().expect("the title's box"); + let (_, needs, _, _) = title_and_arrow.measure(gtk::Orientation::Horizontal, -1); + + wait_until(WAIT, || header.width() >= needs).expect("the title and its sort arrow show whole"); +} + +/// The text size reaches the grid as a GTK setting: a column it sized +/// takes the new measure both ways, and a width the user gave stays. +#[gtk4::test] +fn a_text_size_change_measures_the_grids_columns_again() { + let grid = laid_out(Grid::open( + vec![ + named("id", "integer", true), + named("code", "text", false), + named("note", "text", false), + ], + vec![vec![Value::Int(1), text("CF-1001"), text("Ships in two days")]], + )); + let code = data_column(&grid, 1); + let opened = code.fixed_width(); + data_column(&grid, 2).set_fixed_width(300); + let scaled = TextScale::times(1.5); + + wait_until(WAIT, || code.fixed_width() > opened).expect("the column widened with the text"); + let display = grid.cell_of(1, 1).display().cloned().expect("the code's text"); + wait_until(WAIT, || shows_whole(&display)).expect("the code shows whole at the larger text"); + assert_eq!( + data_column(&grid, 2).fixed_width(), + 300, + "the width the user gave changed" + ); + let widened = code.fixed_width(); + + drop(scaled); + wait_until(WAIT, || code.fixed_width() < widened).expect("the column narrowed with the text"); + assert_eq!( + data_column(&grid, 2).fixed_width(), + 300, + "the width the user gave changed" + ); +} + +/// A grid out of its window hears of no text size change, so it measures +/// again when it is back if its fonts are not the ones it measured in. +#[gtk4::test] +fn a_grid_back_in_its_window_takes_a_text_size_it_missed() { + let grid = laid_out(Grid::open( + vec![ + named("id", "integer", true), + named("code", "text", false), + named("note", "text", false), + ], + vec![vec![Value::Int(1), text("CF-1001"), text("Ships in two days")]], + )); + let code = data_column(&grid, 1); + let opened = code.fixed_width(); + data_column(&grid, 2).set_fixed_width(300); + + grid.window.set_child(None::<>k::Widget>); + let _scaled = TextScale::times(1.5); + grid.window.set_child(Some(&grid.grid)); + + wait_until(WAIT, || code.fixed_width() > opened).expect("the column widened with the text"); + assert_eq!( + data_column(&grid, 2).fixed_width(), + 300, + "the width the user gave changed" + ); +} + +/// libadwaita announces a new monospace font; the grid measures the +/// columns it sized again, from the rows it holds now. +#[gtk4::test] +fn a_monospace_font_change_measures_the_grids_columns_again() { + let grid = laid_out(Grid::open( + vec![named("id", "integer", true), named("note", "text", false)], + vec![vec![Value::Int(1), text(&"x".repeat(30))]], + )); + let column = data_column(&grid, 1); + let wide = column.fixed_width(); + grid.grid.replace_rows(persisted(&[vec![Value::Int(1), text("a")]]), 0); + assert_eq!(column.fixed_width(), wide, "a page of shorter rows narrowed the column"); + + libadwaita::StyleManager::default().notify("monospace-font-name"); + + wait_until(WAIT, || column.fixed_width() < wide).expect("the column measured again"); +} + +/// The header of the column named `name`, and its title. +fn header_title(grid: &Grid, name: &str) -> (gtk::Widget, gtk::Label) { let title = descendants(&grid.grid.column_view()) .into_iter() .filter_map(|widget| widget.downcast::().ok()) - .find(|label| label.label() == "WWWWWWWW") + .find(|label| label.label() == name) .expect("the header's title"); let header = title .parent() .and_then(|title_box| title_box.parent()) .expect("the header button"); - let (_, needs, _, _) = title.measure(gtk::Orientation::Horizontal, -1); + (header, title) +} - wait_until(WAIT, || header.width() >= needs).expect("the header's title shows whole"); +/// The display's text scaled for one test, put back after even when the +/// test fails. +struct TextScale { + settings: gtk::Settings, + dpi: i32, +} + +impl TextScale { + fn times(factor: f64) -> Self { + let settings = gtk::Settings::default().expect("the display's settings"); + let dpi = settings.gtk_xft_dpi(); + // GTK reads -1 as 96 dots per inch, which it keeps in 1024ths. + let base = if dpi > 0 { dpi } else { 96 * 1024 }; + settings.set_gtk_xft_dpi((f64::from(base) * factor).round() as i32); + Self { settings, dpi } + } +} + +impl Drop for TextScale { + fn drop(&mut self) { + self.settings.set_gtk_xft_dpi(self.dpi); + } } /// A NULL the user can replace draws as ``, which a column of short From ab0578d5b4abdcdb0d257dfdef389d1f2d747ffa Mon Sep 17 00:00:00 2001 From: Cristian Date: Sat, 31 Oct 2026 09:07:15 +0100 Subject: [PATCH 38/85] feat(linux-workspace): name a tab's database in its title when not the connection's own (#3904) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3904.added.md | 1 + linux/crates/app/src/ui/editor/tab_scope.rs | 13 +++- linux/crates/app/src/ui/editor/tests.rs | 35 +++++++++++ linux/crates/app/src/ui/workspace/mod.rs | 17 +++-- .../app/src/ui/workspace/tab_factory.rs | 3 +- .../crates/app/src/ui/workspace/tab_title.rs | 63 ++++++++++++++----- .../app/src/ui/workspace/tests/tab_title.rs | 19 ++++++ .../app/src/ui/workspace/workspace_tabs.rs | 7 +++ 8 files changed, 136 insertions(+), 22 deletions(-) create mode 100644 linux/changelog.d/3904.added.md diff --git a/linux/changelog.d/3904.added.md b/linux/changelog.d/3904.added.md new file mode 100644 index 0000000000..71d759adee --- /dev/null +++ b/linux/changelog.d/3904.added.md @@ -0,0 +1 @@ +A query tab in another of the connection's databases names it in its title, as "Query 1 (analytics)". diff --git a/linux/crates/app/src/ui/editor/tab_scope.rs b/linux/crates/app/src/ui/editor/tab_scope.rs index c3db3f867c..3a796c89bc 100644 --- a/linux/crates/app/src/ui/editor/tab_scope.rs +++ b/linux/crates/app/src/ui/editor/tab_scope.rs @@ -186,11 +186,18 @@ impl SqlEditor { }; if let Some(editor) = editor.upgrade() { let imp = editor.imp(); - let own = databases.iter().find(|database| database.is_default); - imp.listed_own_database - .replace(own.map(|database| database.name.clone())); + let own = databases + .iter() + .find(|database| database.is_default) + .map(|database| database.name.clone()); + let own_changed = imp.listed_own_database.replace(own.clone()) != own; imp.scope_button.set_menu_model(database_menu(&databases).as_ref()); editor.show_scope(); + // A tab already in the database the server now calls the + // connection's own is no longer a moved one. + if own_changed { + editor.emit_by_name::<()>("database-changed", &[]); + } } }); } diff --git a/linux/crates/app/src/ui/editor/tests.rs b/linux/crates/app/src/ui/editor/tests.rs index a960c9fe1e..7217fc22f4 100644 --- a/linux/crates/app/src/ui/editor/tests.rs +++ b/linux/crates/app/src/ui/editor/tests.rs @@ -2850,6 +2850,41 @@ fn a_connection_in_no_database_offers_one() { assert_eq!(harness.editor.imp().scope_button_label.label(), "analytics"); } +/// A tab in the database the server names as the connection's own once +/// it lists them again is no longer a moved one, for its title and its +/// record. +#[gtk4::test] +fn a_tab_in_the_database_the_server_then_calls_its_own_is_not_moved() { + let database = |name: &str, is_default: bool| tablepro_core::meta::SchemaInfo { + name: name.to_owned(), + is_default, + is_system: false, + }; + let harness = scoped_editor_with(mysql_dialect(), &[], answering(), |script, request| { + if let tablepro_session::connect::RequestTarget::Network { database, .. } = &mut request.target { + *database = None; + } + script + .list_databases + .once(FakeReply::ok(vec![ + database("analytics", false), + database("app", false), + ])) + .always(FakeReply::ok(vec![database("analytics", false), database("app", true)])); + }); + let button = harness.editor.imp().scope_button.get(); + wait_until(WAIT, || button.is_visible()).expect("the database menu"); + let changed = SignalLog::connect(&harness.editor, "database-changed", |_| ()); + pick_database(&harness, "app"); + assert_eq!(harness.editor.tab_database().as_deref(), Some("app")); + + harness.editor.buffer().set_text("CREATE DATABASE scratch"); + run_and_wait(&harness); + + wait_until(WAIT, || changed.count() == 2).expect("the workspace to hear the tab is in its own database"); + assert_eq!(harness.editor.tab_database(), None); +} + /// All Rows runs the statement again on the connection, in another /// database than a moved tab's, so only the rows shown are offered. #[gtk4::test] diff --git a/linux/crates/app/src/ui/workspace/mod.rs b/linux/crates/app/src/ui/workspace/mod.rs index 0bfa005a70..b2fcf23602 100644 --- a/linux/crates/app/src/ui/workspace/mod.rs +++ b/linux/crates/app/src/ui/workspace/mod.rs @@ -412,6 +412,14 @@ impl WorkspaceTab { } } + /// The database an editor tab moved to off the connection's. + pub(crate) fn moved_to(&self) -> Option { + match self { + WorkspaceTab::Editor(slot) => slot.editor.tab_database(), + _ => None, + } + } + /// What the tab's title is made from. pub(crate) fn identity(&self) -> tab_identity::TabIdentity { use tab_identity::TabIdentity; @@ -500,8 +508,9 @@ pub(crate) enum WorkspaceInput { /// An editor tab was saved, opened a file, or its text came to /// differ from its file's or match it again. EditorTabFileChanged(Uuid), - /// An editor tab moved to another database, which its record keeps. - EditorTabDatabaseChanged, + /// An editor tab moved to another database, which its record keeps + /// and its title names. + EditorTabDatabaseChanged(Uuid), /// The session's saved tabs and their editor text, read off the GTK /// thread. WorkspaceLoaded(workspace_tabs::LoadedWorkspace), @@ -1122,7 +1131,7 @@ impl WorkspaceModel { return; }; if let Some(tab) = self.workspace_tabs.borrow().get(&id) { - tab_title::apply_tab_title(tab.page(), &title, &self.label); + tab_title::apply_tab_title(tab.page(), &title, &self.label, tab.moved_to().as_deref()); } if self.index.last_selected() == Some(id) { self.refresh_title(); @@ -1266,7 +1275,7 @@ impl WorkspaceModel { } WorkspaceInput::EditorTabContentChanged(id) => self.on_editor_tab_content_changed(id), WorkspaceInput::EditorTabFileChanged(id) => self.on_editor_tab_file_changed(id), - WorkspaceInput::EditorTabDatabaseChanged => self.persist_workspace_state(), + WorkspaceInput::EditorTabDatabaseChanged(id) => self.on_editor_tab_database_changed(id), WorkspaceInput::WorkspaceLoaded(loaded) => self.on_workspace_loaded(loaded), WorkspaceInput::ResetWorkspaceLayout => self.on_reset_workspace_layout(), WorkspaceInput::SafeModeChanged => self.refresh_title(), diff --git a/linux/crates/app/src/ui/workspace/tab_factory.rs b/linux/crates/app/src/ui/workspace/tab_factory.rs index 49a33e49fb..1fb9152af4 100644 --- a/linux/crates/app/src/ui/workspace/tab_factory.rs +++ b/linux/crates/app/src/ui/workspace/tab_factory.rs @@ -139,6 +139,7 @@ impl TabFactory { &page, &resolve_tab_title(&slot.identity(), self.parts.naming.get()), &self.parts.label, + slot.moved_to().as_deref(), ); self.parts.tabs.borrow_mut().insert(id, slot); self.parts.input.deliver(WorkspaceInput::WorkspaceTabsChanged); @@ -497,7 +498,7 @@ pub(super) fn connect_editor( let file = input.clone(); editor.connect_file_changed(move |_| file.deliver(WorkspaceInput::EditorTabFileChanged(tab_id))); let database = input.clone(); - editor.connect_database_changed(move |_| database.deliver(WorkspaceInput::EditorTabDatabaseChanged)); + editor.connect_database_changed(move |_| database.deliver(WorkspaceInput::EditorTabDatabaseChanged(tab_id))); let transaction = input.clone(); editor.connect_transaction_changed(move |_, _| transaction.deliver(WorkspaceInput::EditorTransactionChanged)); let toast = input.clone(); diff --git a/linux/crates/app/src/ui/workspace/tab_title.rs b/linux/crates/app/src/ui/workspace/tab_title.rs index 12f4ea8c75..191d246902 100644 --- a/linux/crates/app/src/ui/workspace/tab_title.rs +++ b/linux/crates/app/src/ui/workspace/tab_title.rs @@ -56,6 +56,13 @@ impl ConnectionLabel { self.shares_bar.get() } + /// What a tab names after its title: the connection while the bar is + /// shared, and the tab's database when it moved off the connection's. + fn suffix(&self, moved_to: Option<&str>) -> Option { + let connection = self.shares_bar().then(|| self.name()); + suffix(connection, moved_to) + } + /// True when that changed, and the tabs have to be titled again. pub(crate) fn set_shares_bar(&self, shares: bool) -> bool { self.shares_bar.replace(shares) != shares @@ -67,13 +74,17 @@ impl ConnectionLabel { /// what the unsaved icon shows, since a screen reader reads a tab's /// tooltip and not its icon, and it is escaped: AdwTab shows it as Pango /// markup. -pub(crate) fn apply_tab_title(page: &adw::TabPage, title: &TabTitle, connection: &ConnectionLabel) { - let name = connection.name(); +pub(crate) fn apply_tab_title( + page: &adw::TabPage, + title: &TabTitle, + connection: &ConnectionLabel, + moved_to: Option<&str>, +) { // The overview's search finds a connection's tabs by its name. - page.set_keyword(&name); - let title = match connection.shares_bar() { - true => name_connection(title.clone(), &name), - false => title.clone(), + page.set_keyword(&connection.name()); + let title = match connection.suffix(moved_to) { + Some(suffix) => name_place(title.clone(), &suffix), + None => title.clone(), }; page.set_title(&title.label); let unsaved = page.icon().is_some().then(|| crate::i18n::gettext("Unsaved changes")); @@ -84,16 +95,29 @@ pub(crate) fn apply_tab_title(page: &adw::TabPage, title: &TabTitle, connection: page.set_tooltip(&glib::markup_escape_text(&lines.join("\n"))); } -/// A tab beside another connection's names its own after its title, -/// which stays the useful part once ellipsized. The tooltip names it on a -/// line of its own, which a cut label loses. -fn name_connection(title: TabTitle, connection: &str) -> TabTitle { - let tooltip = [title.tooltip.as_deref().unwrap_or(&title.label), connection].join("\n"); +/// The connection, the database, or both, as "prod, analytics". +fn suffix(connection: Option, database: Option<&str>) -> Option { + match (connection, database) { + // Translators: a connection and one of its databases, after a tab's title. + (Some(connection), Some(database)) => Some(crate::i18n::gettext_f( + "{connection}, {database}", + &[("connection", &connection), ("database", database)], + )), + (connection, database) => connection.or_else(|| database.map(str::to_owned)), + } +} + +/// A tab beside another connection's names its own after its title, and +/// a tab in another database names that, since the title stays the +/// useful part once ellipsized. The tooltip names them on a line of its +/// own, which a cut label loses. +fn name_place(title: TabTitle, place: &str) -> TabTitle { + let tooltip = [title.tooltip.as_deref().unwrap_or(&title.label), place].join("\n"); TabTitle { - // Translators: a tab's title and its connection, while several connections share the tab bar. + // Translators: a tab's title and its connection while several connections share the tab bar, or the database it runs in when not the connection's. label: crate::i18n::gettext_f( "{title} ({connection})", - &[("title", &title.label), ("connection", connection)], + &[("title", &title.label), ("connection", place)], ), tooltip: Some(tooltip), } @@ -274,7 +298,7 @@ mod tests { #[test] fn a_tab_beside_another_connections_names_its_own_after_its_title() { - let title = name_connection( + let title = name_place( resolve_tab_title(&TabIdentity::Table(orders()), TableNaming::Bare), "Shop", ); @@ -284,6 +308,17 @@ mod tests { assert!(!title.label.contains('·')); } + #[test] + fn a_tab_names_its_database_and_the_connection_it_shares_the_bar_with() { + assert_eq!(suffix(None, Some("analytics")).as_deref(), Some("analytics")); + assert_eq!( + suffix(Some("Shop".to_owned()), Some("analytics")).as_deref(), + Some("Shop, analytics") + ); + assert_eq!(suffix(Some("Shop".to_owned()), None).as_deref(), Some("Shop")); + assert_eq!(suffix(None, None), None); + } + #[test] fn a_script_its_label_shows_whole_has_no_tooltip() { let title = resolve_tab_title( diff --git a/linux/crates/app/src/ui/workspace/tests/tab_title.rs b/linux/crates/app/src/ui/workspace/tests/tab_title.rs index 92cd1ed168..59155295bb 100644 --- a/linux/crates/app/src/ui/workspace/tests/tab_title.rs +++ b/linux/crates/app/src/ui/workspace/tests/tab_title.rs @@ -385,3 +385,22 @@ fn a_renamed_connection_retitles_its_tabs_and_the_window() { assert_eq!(page.keyword().as_deref(), Some("Shop")); assert_eq!(test.workspace().connection_name(), "Shop"); } + +/// A tab moved off the connection's database names the one it runs in. +#[gtk4::test] +fn a_tab_moved_to_another_database_names_it() { + let test = TestWindow::connected_with("MovedTabTitle", |script| { + script.dialect = Arc::new(tablepro_driver_mysql::MySqlDialect::new( + tablepro_driver_mysql::MySqlFlavour::MySql, + &tablepro_core::ServerInfo::new("MySQL", "8.4.0", 8, 4, 0), + )); + }); + let page = open_editor(&test.workspace()); + let editor = editors(&test.window).pop().expect("the editor"); + + WidgetExt::activate_action(&editor, "editor.database", Some(&"analytics".to_variant())).expect("editor.database"); + + wait_until(WAIT, || page.title() == "Query 1 (analytics)").expect("the tab to name its database"); + let tooltip = page.tooltip().map(|tooltip| tooltip.to_string()).unwrap_or_default(); + assert_eq!(tooltip, "Query 1\nanalytics"); +} diff --git a/linux/crates/app/src/ui/workspace/workspace_tabs.rs b/linux/crates/app/src/ui/workspace/workspace_tabs.rs index 4b2f884211..2775d02249 100644 --- a/linux/crates/app/src/ui/workspace/workspace_tabs.rs +++ b/linux/crates/app/src/ui/workspace/workspace_tabs.rs @@ -530,6 +530,13 @@ impl WorkspaceModel { self.persist_workspace_state(); } + /// A tab that moved to another database is restored there and names + /// it in its title. + pub(super) fn on_editor_tab_database_changed(&self, id: Uuid) { + self.retitle_tab(id); + self.persist_workspace_state(); + } + pub(super) fn on_editor_tab_run_state_changed(&self, id: Uuid, running: bool) { if let Some(WorkspaceTab::Editor(slot)) = self.workspace_tabs.borrow().get(&id) { slot.page.set_loading(running); From e2227bc810a1f337f71062b0d07897f886add668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sat, 31 Oct 2026 16:40:08 +0700 Subject: [PATCH 39/85] build(duckdb): bump libduckdb to 2.0.0 (#3928) --- apple/Libs/checksums.sha256 | 8 +- apple/Libs/ios/checksums.sha256 | 12 +- apple/Libs/release | 2 +- .../CDuckDB/include/duckdb.h | 13971 ++++++++++------ .../DuckDBDriverPlugin/DuckDBConnection.swift | 22 +- .../DuckDBStorageCompatibility.swift | 13 + .../Core/Database/NativeDumpRegistry.swift | 7 +- .../TablePro/Resources/Localizable.xcstrings | 8 + .../Resources/ThirdPartyLicenses/licenses.yml | 21 +- .../ThirdPartyLicenses/texts/duckdb.txt | 2 +- .../Acknowledgements/Acknowledgements.json | 4 +- .../TableProMobile/CBridges/CDuckDB/CDuckDB.h | 1 + .../TableProMobile/Drivers/DuckDBDriver.swift | 25 +- .../Drivers/DuckDBDriverTests.swift | 30 +- apple/changelog.d/3928.changed.md | 1 + apple/changelog.d/3928.fixed.md | 1 + apple/scripts/build-duckdb-ios.sh | 234 +- apple/scripts/build-duckdb.sh | 196 +- apple/scripts/duckdb-ios-extensions.cmake | 29 +- .../probes/check-column-type-corpus.sh | 4 +- .../probes/check-duckdb-access-mode.sh | 2 +- .../scripts/probes/check-duckdb-extensions.sh | 2 +- .../probes/check-duckdb-offline-metadata.sh | 25 +- .../probes/check-duckdb-storage-version.sh | 112 + .../check-duckdb-table-listing-parity.sh | 2 +- .../scripts/probes/check-duckdb-value-api.sh | 2 +- linux/Cargo.lock | 184 +- linux/Cargo.toml | 8 +- linux/README.md | 2 +- .../about/bundled/libduckdb-sys/duckdb.txt | 2 +- .../bundled/libduckdb-sys/libpg_query.txt | 28 - .../about/bundled/libduckdb-sys/manifest.json | 5 +- linux/build-aux/debian/copyright | 175 +- linux/changelog.d/3928.changed.md | 1 + linux/crates/core/src/export/text_table.rs | 11 +- .../crates/drivers/duckdb/src/create_file.rs | 4 +- .../drivers/duckdb/src/decode/nested.rs | 9 +- .../crates/drivers/duckdb/src/decode/text.rs | 7 +- linux/crates/drivers/duckdb/src/errors.rs | 12 - linux/crates/drivers/duckdb/src/instance.rs | 7 +- .../drivers/duckdb/src/result_stream.rs | 114 +- .../duckdb/tests/integration/catalog.rs | 4 +- .../tests/integration/open_semantics.rs | 73 +- linux/data/licenses/NOTICE | 8 +- linux/data/licenses/THIRD-PARTY-NOTICE.md | 408 +- linux/data/licenses/third-party.json | 162 +- linux/docs/adding-drivers.md | 2 +- linux/docs/decisions/0025-bundled-duckdb.md | 2 +- linux/docs/decisions/0031-duckdb-2.md | 32 + linux/docs/decisions/README.md | 3 +- linux/docs/drivers/duckdb.md | 7 +- 51 files changed, 10398 insertions(+), 5608 deletions(-) create mode 100644 apple/Plugins/DuckDBDriverPlugin/DuckDBStorageCompatibility.swift create mode 100644 apple/changelog.d/3928.changed.md create mode 100644 apple/changelog.d/3928.fixed.md create mode 100755 apple/scripts/probes/check-duckdb-storage-version.sh delete mode 100644 linux/build-aux/about/bundled/libduckdb-sys/libpg_query.txt create mode 100644 linux/changelog.d/3928.changed.md create mode 100644 linux/docs/decisions/0031-duckdb-2.md diff --git a/apple/Libs/checksums.sha256 b/apple/Libs/checksums.sha256 index 58559a0baf..228b6a3f62 100644 --- a/apple/Libs/checksums.sha256 +++ b/apple/Libs/checksums.sha256 @@ -10,10 +10,10 @@ c47c8406cdb817e121a89ad5171f6931fd73c4bce6c73c3809e626257afd7374 Libs/libcrypto e274f77ed3feaee3d4f8f6ac297376d56a1369388f037599a90ea440390349b4 Libs/libcrypto_universal.a ec04bc233ea917de16a48b130d1467c68f5b95d469f423b7b9054d0e45bb4e16 Libs/libcrypto_x86_64.a e274f77ed3feaee3d4f8f6ac297376d56a1369388f037599a90ea440390349b4 Libs/libcrypto.a -f833d1edba062e03a5a7f7b5e40fbfe0f015c081158b8dd455d6129bf4515c12 Libs/libduckdb_arm64.a -c1aef28aa225d34bb4d8be853000f4d2640a77366ff4b2cfa61f53648290c357 Libs/libduckdb_universal.a -e971a4c84689626699482ba5e2f148345b50e53fc1aed57b6e37fbb90c85db9c Libs/libduckdb_x86_64.a -c1aef28aa225d34bb4d8be853000f4d2640a77366ff4b2cfa61f53648290c357 Libs/libduckdb.a +09b2deb312d03840dda422a6931a9918ad81c308941c774556adb1386d6ab589 Libs/libduckdb_arm64.a +9749f1be288f03cc4362b15fbc0137edd8aee75542f52f7d7ca8d563586a1e77 Libs/libduckdb_universal.a +3183445676921ea6e4ea4cb7f0689703fb3ceba3d1699b21e88a2e36f12e0e2e Libs/libduckdb_x86_64.a +9749f1be288f03cc4362b15fbc0137edd8aee75542f52f7d7ca8d563586a1e77 Libs/libduckdb.a 5017c3fecadf4955fad1db923a8358fc9943fa17e8a5d54288db6a28fc5a3aa8 Libs/libhiredis_arm64.a 58974eb81d5fefca9fb6732bd15921f1cf12a23f9a2402b1b099ec6c089f1e35 Libs/libhiredis_ssl_arm64.a 09c8cb4acb62a54c09c4f2605bbc464b93d9e2ea5b2ae89a1d2165f8adaa9b9c Libs/libhiredis_ssl_universal.a diff --git a/apple/Libs/ios/checksums.sha256 b/apple/Libs/ios/checksums.sha256 index bd457322f7..f6e64e4c7a 100644 --- a/apple/Libs/ios/checksums.sha256 +++ b/apple/Libs/ios/checksums.sha256 @@ -1,8 +1,10 @@ -840741c66e9ee257f97eb7cc16407c37960aa49faa4b110e56ccc0f8dc3a6db2 ./DuckDB.xcframework/Info.plist -c8dd688cf14559c744255d1bb3879330d6ae3523268e8904fa7b885e140030aa ./DuckDB.xcframework/ios-arm64/Headers/duckdb.h -f4073989393e739b0b5ad68c7f47f7750715d2d46e46141e81b1bc49cf782620 ./DuckDB.xcframework/ios-arm64/libduckdb-device.a -c8dd688cf14559c744255d1bb3879330d6ae3523268e8904fa7b885e140030aa ./DuckDB.xcframework/ios-arm64_x86_64-simulator/Headers/duckdb.h -75523877523153f63249a39eb4ec0d7f1bb05db3a4dbb5e6c35ee848b09afb71 ./DuckDB.xcframework/ios-arm64_x86_64-simulator/libduckdb-sim.a +fcfb2160ac0c32d40fd3241bb0ec0ec157238a5db6ef68a5d98e93a616956cf6 ./DuckDB.xcframework/Info.plist +8dbc9c71a10f0a75fcacc1a3f4f7c76093a84d232fc152222ee5b6e512ab4560 ./DuckDB.xcframework/ios-arm64-simulator/Headers/duckdb.h +a90f28cbe2662c69fc73f726832479ae7b5d043566b83eb1ac95a8ab35d3e37d ./DuckDB.xcframework/ios-arm64-simulator/Headers/duckdb_static_extensions.h +842bd80d7f54472c4161434474af155675e0d7b19c7751e1d1046d8e144a19b6 ./DuckDB.xcframework/ios-arm64-simulator/libduckdb-sim.a +8dbc9c71a10f0a75fcacc1a3f4f7c76093a84d232fc152222ee5b6e512ab4560 ./DuckDB.xcframework/ios-arm64/Headers/duckdb.h +a90f28cbe2662c69fc73f726832479ae7b5d043566b83eb1ac95a8ab35d3e37d ./DuckDB.xcframework/ios-arm64/Headers/duckdb_static_extensions.h +c667055fe35aa1e47ca8f2dd36b8328743429872022a0bb25701b593d9a7d1f0 ./DuckDB.xcframework/ios-arm64/libduckdb-device.a 0481f79db3949ba16425ffd8e15e640757a208ca05728d98cdbfb4bc617e06c3 ./FreeTDS.xcframework/Info.plist 734d8502be13513e974a621557b1a4776d9f605b5a48039e28a3597f27f704ce ./FreeTDS.xcframework/ios-arm64-simulator/Headers/sybdb.h eaa21732fa7cfb538b3bb9bcca7342b99b407d2e838c09e5f3d07398c21406a3 ./FreeTDS.xcframework/ios-arm64-simulator/Headers/sybfront.h diff --git a/apple/Libs/release b/apple/Libs/release index edf700ee23..f05581e49b 100644 --- a/apple/Libs/release +++ b/apple/Libs/release @@ -1 +1 @@ -libs-5 +libs-6 diff --git a/apple/Plugins/DuckDBDriverPlugin/CDuckDB/include/duckdb.h b/apple/Plugins/DuckDBDriverPlugin/CDuckDB/include/duckdb.h index 56ee87b782..517bb60b16 100644 --- a/apple/Plugins/DuckDBDriverPlugin/CDuckDB/include/duckdb.h +++ b/apple/Plugins/DuckDBDriverPlugin/CDuckDB/include/duckdb.h @@ -2,34 +2,41 @@ // // DuckDB // -// duckdb.h -// // //===----------------------------------------------------------------------===// // // !!!!!!! -// WARNING: this file is autogenerated by scripts/generate_c_api.py, manual changes will be overwritten +// WARNING: this file is autogenerated, manual changes will be overwritten // !!!!!!! #pragma once +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + #ifndef DUCKDB_C_API #ifdef _WIN32 #ifdef DUCKDB_STATIC_BUILD #define DUCKDB_C_API -#else -#if defined(DUCKDB_BUILD_LIBRARY) && !defined(DUCKDB_BUILD_LOADABLE_EXTENSION) +#elif defined(DUCKDB_BUILD_LIBRARY) && !defined(DUCKDB_BUILD_LOADABLE_EXTENSION) #define DUCKDB_C_API __declspec(dllexport) #else #define DUCKDB_C_API __declspec(dllimport) #endif -#endif +#else +#if defined(__GNUC__) || defined(__clang__) +#define DUCKDB_C_API __attribute__((visibility("default"))) #else #define DUCKDB_C_API #endif #endif +#endif -//! duplicate of duckdb/main/winapi.hpp #ifndef DUCKDB_EXTENSION_API #ifdef _WIN32 #ifdef DUCKDB_STATIC_BUILD @@ -38,122 +45,267 @@ #define DUCKDB_EXTENSION_API __declspec(dllexport) #endif #else +#if defined(__GNUC__) || defined(__clang__) #define DUCKDB_EXTENSION_API __attribute__((visibility("default"))) +#else +#define DUCKDB_EXTENSION_API +#endif #endif #endif -#include -#include -#include +#if defined(__GNUC__) || defined(__clang__) +#define DUCKDB_DEPRECATED \ + __attribute__((deprecated("This function is deprecated and will be removed in a future version."))) +#elif defined(_MSC_VER) +#define DUCKDB_DEPRECATED __declspec(deprecated("This function is deprecated and will be removed in a future version.")) +#else +#define DUCKDB_DEPRECATED +#endif -#ifdef __cplusplus -extern "C" { +//===--------------------------------------------------------------------===// +// API version +//===--------------------------------------------------------------------===// +//! The API version this translation unit targets. Define all three to compile against an +//! older surface than the one this header describes. +#if !defined(DUCKDB_API_VERSION_MAJOR) && !defined(DUCKDB_API_VERSION_MINOR) && !defined(DUCKDB_API_VERSION_PATCH) +#define DUCKDB_API_VERSION_MAJOR 1 +#define DUCKDB_API_VERSION_MINOR 5 +#define DUCKDB_API_VERSION_PATCH 6 +#elif !(defined(DUCKDB_API_VERSION_MAJOR) && defined(DUCKDB_API_VERSION_MINOR) && defined(DUCKDB_API_VERSION_PATCH)) +#error "define all or none of the DUCKDB_API_VERSION_MAJOR / _MINOR / _PATCH macros" +#endif + +//! True when the targeted version is x.y.z or newer. +#define DUCKDB_API_VERSION_AT_LEAST(x, y, z) \ + (DUCKDB_API_VERSION_MAJOR > (x) || \ + (DUCKDB_API_VERSION_MAJOR == (x) && \ + (DUCKDB_API_VERSION_MINOR > (y) || (DUCKDB_API_VERSION_MINOR == (y) && DUCKDB_API_VERSION_PATCH >= (z))))) + +//! True when the targeted version is older than x.y.z. +#define DUCKDB_API_VERSION_BELOW(x, y, z) (!DUCKDB_API_VERSION_AT_LEAST((x), (y), (z))) + +//===--------------------------------------------------------------------===// +// Surface switches +//===--------------------------------------------------------------------===// +//! Set to 1 to compile the deprecated surface, 0 to omit it. Defaults from the +//! older DUCKDB_API_NO_DEPRECATED macro when that is what the consumer defines. +#if !defined(DUCKDB_API_ALLOW_DEPRECATED) +#ifdef DUCKDB_API_NO_DEPRECATED +#define DUCKDB_API_ALLOW_DEPRECATED 0 +#else +#define DUCKDB_API_ALLOW_DEPRECATED 1 +#endif +#endif + +//! Set to 1 to compile the unstable surface, 0 to omit it. Defaults from the +//! older DUCKDB_EXTENSION_API_VERSION_UNSTABLE macro when that is what the consumer defines. +#if !defined(DUCKDB_API_ALLOW_UNSTABLE) +#ifdef DUCKDB_EXTENSION_API_VERSION_UNSTABLE +#define DUCKDB_API_ALLOW_UNSTABLE 1 +#else +#define DUCKDB_API_ALLOW_UNSTABLE 0 +#endif +#endif + +//! The unstable surface is not a promise, so it is not part of any released +//! version: its constructs may change shape or vanish. Targeting an older version +//! while opting into it would hand back today's shapes under names that version +//! never promised, so the two are mutually exclusive. +#if DUCKDB_API_ALLOW_UNSTABLE && !DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +#error "the unstable surface requires targeting the newest API version" #endif //===--------------------------------------------------------------------===// -// Enums +// General type definitions //===--------------------------------------------------------------------===// -//! WARNING: The numbers of these enums should not be changed, as changing the numbers breaks ABI compatibility. -//! Always add enums at the END of the enum +//! The internal representation of a VARCHAR (string_t). If the VARCHAR does not +//! exceed 12 characters, then we inline it. Otherwise, we inline a four-byte prefix for faster +//! string comparisons and store a pointer to the remaining characters. This is a non- +//! owning structure, i.e., it does not have to be freed. +typedef struct { + union { + struct { + uint32_t length; + char prefix[4]; + char *ptr; + } pointer; + struct { + uint32_t length; + char inlined[12]; + } inlined; + } value; +} duckdb_string_t; + +//! The returned state of different functions. Legacy naming: values use DuckDB prefix without underscore. +typedef enum duckdb_state { DuckDBSuccess = 0, DuckDBError = 1 } duckdb_state; + +struct ArrowSchema; +struct ArrowArray; + +typedef uint64_t idx_t; + +/* ============================================================================ + * MODULE: common_enums + * ============================================================================ */ + +/* --- Enums for common_enums --- */ -//! An enum over DuckDB's internal types. +/*! + * An enum over DuckDB's internal types. + * + * history: + * - stable: v0.1.0 + */ typedef enum DUCKDB_TYPE { DUCKDB_TYPE_INVALID = 0, - // bool + + //! bool DUCKDB_TYPE_BOOLEAN = 1, - // int8_t + + //! int8_t DUCKDB_TYPE_TINYINT = 2, - // int16_t + + //! int16_t DUCKDB_TYPE_SMALLINT = 3, - // int32_t + + //! int32_t DUCKDB_TYPE_INTEGER = 4, - // int64_t + + //! int64_t DUCKDB_TYPE_BIGINT = 5, - // uint8_t + + //! uint8_t DUCKDB_TYPE_UTINYINT = 6, - // uint16_t + + //! uint16_t DUCKDB_TYPE_USMALLINT = 7, - // uint32_t + + //! uint32_t DUCKDB_TYPE_UINTEGER = 8, - // uint64_t + + //! uint64_t DUCKDB_TYPE_UBIGINT = 9, - // float + + //! float DUCKDB_TYPE_FLOAT = 10, - // double + + //! double DUCKDB_TYPE_DOUBLE = 11, - // duckdb_timestamp (microseconds) + + //! duckdb_timestamp (microseconds) DUCKDB_TYPE_TIMESTAMP = 12, - // duckdb_date + + //! duckdb_date DUCKDB_TYPE_DATE = 13, - // duckdb_time + + //! duckdb_time DUCKDB_TYPE_TIME = 14, - // duckdb_interval + + //! duckdb_interval DUCKDB_TYPE_INTERVAL = 15, - // duckdb_hugeint + + //! duckdb_hugeint DUCKDB_TYPE_HUGEINT = 16, - // duckdb_uhugeint - DUCKDB_TYPE_UHUGEINT = 32, - // const char* + + //! const char* DUCKDB_TYPE_VARCHAR = 17, - // duckdb_blob + + //! duckdb_blob DUCKDB_TYPE_BLOB = 18, - // duckdb_decimal + + //! duckdb_decimal DUCKDB_TYPE_DECIMAL = 19, - // duckdb_timestamp_s (seconds) + + //! duckdb_timestamp_s (seconds) DUCKDB_TYPE_TIMESTAMP_S = 20, - // duckdb_timestamp_ms (milliseconds) + + //! duckdb_timestamp_ms (milliseconds) DUCKDB_TYPE_TIMESTAMP_MS = 21, - // duckdb_timestamp_ns (nanoseconds) + + //! duckdb_timestamp_ns (nanoseconds) DUCKDB_TYPE_TIMESTAMP_NS = 22, - // enum type, only useful as logical type + + //! enum type, only useful as logical type DUCKDB_TYPE_ENUM = 23, - // list type, only useful as logical type + + //! list type, only useful as logical type DUCKDB_TYPE_LIST = 24, - // struct type, only useful as logical type + + //! struct type, only useful as logical type DUCKDB_TYPE_STRUCT = 25, - // map type, only useful as logical type + + //! map type, only useful as logical type DUCKDB_TYPE_MAP = 26, - // duckdb_array, only useful as logical type - DUCKDB_TYPE_ARRAY = 33, - // duckdb_hugeint + + //! duckdb_hugeint DUCKDB_TYPE_UUID = 27, - // union type, only useful as logical type + + //! union type, only useful as logical type DUCKDB_TYPE_UNION = 28, - // duckdb_bit + + //! duckdb_bit DUCKDB_TYPE_BIT = 29, - // duckdb_time_tz + + //! duckdb_time_tz DUCKDB_TYPE_TIME_TZ = 30, - // duckdb_timestamp (microseconds) + + //! duckdb_timestamp (microseconds) DUCKDB_TYPE_TIMESTAMP_TZ = 31, - // enum type, only useful as logical type + + //! duckdb_uhugeint + DUCKDB_TYPE_UHUGEINT = 32, + + //! duckdb_array, only useful as logical type + DUCKDB_TYPE_ARRAY = 33, + + //! enum type, only useful as logical type DUCKDB_TYPE_ANY = 34, - // duckdb_bignum + + //! duckdb_bignum DUCKDB_TYPE_BIGNUM = 35, - // enum type, only useful as logical type + + //! enum type, only useful as logical type DUCKDB_TYPE_SQLNULL = 36, - // enum type, only useful as logical type + + //! enum type, only useful as logical type DUCKDB_TYPE_STRING_LITERAL = 37, - // enum type, only useful as logical type + + //! enum type, only useful as logical type DUCKDB_TYPE_INTEGER_LITERAL = 38, - // duckdb_time_ns (nanoseconds) + + //! duckdb_time_ns (nanoseconds) DUCKDB_TYPE_TIME_NS = 39, - // GEOMETRY type, WKB blob + + //! GEOMETRY type, WKB blob DUCKDB_TYPE_GEOMETRY = 40, -} duckdb_type; -//! An enum over the returned state of different functions. -typedef enum duckdb_state { DuckDBSuccess = 0, DuckDBError = 1 } duckdb_state; + //! VARIANT type + DUCKDB_TYPE_VARIANT = 41, -//! An enum over the pending state of a pending query result. + //! duckdb_timestamp (nanoseconds) + DUCKDB_TYPE_TIMESTAMP_TZ_NS = 42, +} DUCKDB_TYPE; + +/*! + * An enum over the pending state of a pending query result. + * + * history: + * - stable: v0.5.0 + */ typedef enum duckdb_pending_state { DUCKDB_PENDING_RESULT_READY = 0, DUCKDB_PENDING_RESULT_NOT_READY = 1, DUCKDB_PENDING_ERROR = 2, - DUCKDB_PENDING_NO_TASKS_AVAILABLE = 3 + DUCKDB_PENDING_NO_TASKS_AVAILABLE = 3, } duckdb_pending_state; -//! An enum over DuckDB's different result types. +/*! + * An enum over DuckDB's different result types. + * + * history: + * - stable: v0.10.0 + */ typedef enum duckdb_result_type { DUCKDB_RESULT_TYPE_INVALID = 0, DUCKDB_RESULT_TYPE_CHANGED_ROWS = 1, @@ -161,7 +313,12 @@ typedef enum duckdb_result_type { DUCKDB_RESULT_TYPE_QUERY_RESULT = 3, } duckdb_result_type; -//! An enum over DuckDB's different statement types. +/*! + * An enum over DuckDB's different statement types. + * + * history: + * - stable: v0.10.0 + */ typedef enum duckdb_statement_type { DUCKDB_STATEMENT_TYPE_INVALID = 0, DUCKDB_STATEMENT_TYPE_SELECT = 1, @@ -191,9 +348,17 @@ typedef enum duckdb_statement_type { DUCKDB_STATEMENT_TYPE_ATTACH = 25, DUCKDB_STATEMENT_TYPE_DETACH = 26, DUCKDB_STATEMENT_TYPE_MULTI = 27, + DUCKDB_STATEMENT_TYPE_COPY_DATABASE = 28, + DUCKDB_STATEMENT_TYPE_UPDATE_EXTENSIONS = 29, + DUCKDB_STATEMENT_TYPE_MERGE_INTO = 30, } duckdb_statement_type; -//! An enum over DuckDB's different error types. +/*! + * An enum over DuckDB's different error types. + * + * history: + * - stable: v1.1.0 + */ typedef enum duckdb_error_type { DUCKDB_ERROR_INVALID = 0, DUCKDB_ERROR_OUT_OF_RANGE = 1, @@ -237,42 +402,75 @@ typedef enum duckdb_error_type { DUCKDB_ERROR_MISSING_EXTENSION = 39, DUCKDB_ERROR_AUTOLOAD = 40, DUCKDB_ERROR_SEQUENCE = 41, - DUCKDB_INVALID_CONFIGURATION = 42 + DUCKDB_ERROR_DATA_CORRUPTION = 43, + DUCKDB_INVALID_CONFIGURATION = 42, + DUCKDB_ERROR_RESOURCE_IN_USE = 44, } duckdb_error_type; -//! An enum over DuckDB's different cast modes. -typedef enum duckdb_cast_mode { DUCKDB_CAST_NORMAL = 0, DUCKDB_CAST_TRY = 1 } duckdb_cast_mode; +/*! + * An enum over DuckDB's different cast modes. + * + * history: + * - stable: v1.1.0 + */ +typedef enum duckdb_cast_mode { + DUCKDB_CAST_NORMAL = 0, + DUCKDB_CAST_TRY = 1, +} duckdb_cast_mode; +/*! + * Flags for opening files. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef enum duckdb_file_flag { DUCKDB_FILE_FLAG_INVALID = 0, - // Open the file with "read" capabilities. + + //! Open the file with read capabilities. DUCKDB_FILE_FLAG_READ = 1, - // Open the file with "write" capabilities. + + //! Open the file with write capabilities. DUCKDB_FILE_FLAG_WRITE = 2, - // Create a new file, or open if it already exists. + + //! Create a new file, or open if it already exists. DUCKDB_FILE_FLAG_CREATE = 3, - // Create a new file, or fail if it already exists. + + //! Create a new file, or fail if it already exists. DUCKDB_FILE_FLAG_CREATE_NEW = 4, - // Open the file in "append" mode. + + //! Open the file in append mode. DUCKDB_FILE_FLAG_APPEND = 5, } duckdb_file_flag; -//! An enum over DuckDB's configuration option scopes. -//! This enum can be used to specify the default scope when creating a custom configuration option, -//! but it is also be used to determine the scope in which a configuration option is set when it is -//! changed or retrieved. +/*! + * An enum over DuckDB's configuration option scopes. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef enum duckdb_config_option_scope { DUCKDB_CONFIG_OPTION_SCOPE_INVALID = 0, - // The option is set for the duration of the current transaction only. - // !! CURRENTLY NOT IMPLEMENTED !! + + //! The option is set for the duration of the current transaction only. CURRENTLY NOT IMPLEMENTED. DUCKDB_CONFIG_OPTION_SCOPE_LOCAL = 1, - // The option is set for the current session/connection only. + + //! The option is set for the current session/connection only. DUCKDB_CONFIG_OPTION_SCOPE_SESSION = 2, - // Set the option globally for all sessions/connections. + + //! Set the option globally for all sessions/connections. DUCKDB_CONFIG_OPTION_SCOPE_GLOBAL = 3, } duckdb_config_option_scope; -//! An enum over DuckDB's catalog entry types. +/*! + * An enum over DuckDB's catalog entry types. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef enum duckdb_catalog_entry_type { DUCKDB_CATALOG_ENTRY_TYPE_INVALID = 0, DUCKDB_CATALOG_ENTRY_TYPE_TABLE = 1, @@ -286,5993 +484,9906 @@ typedef enum duckdb_catalog_entry_type { DUCKDB_CATALOG_ENTRY_TYPE_DATABASE = 9, } duckdb_catalog_entry_type; -//===--------------------------------------------------------------------===// -// General type definitions -//===--------------------------------------------------------------------===// +/* --- Struct forward declarations for common_enums --- */ -//! DuckDB's index type. -typedef uint64_t idx_t; +/* --- Types for common_enums --- */ -//! Type definition for the data pointers of selection vectors. -typedef uint32_t sel_t; +/*! + * Alias for the DUCKDB_TYPE enum. + * + * history: + * - stable: v0.1.0 + */ +typedef DUCKDB_TYPE duckdb_type; -//! The callback to destroy data, e.g., -//! bind data (if any), init data (if any), extra data for replacement scans (if any), etc. -typedef void (*duckdb_delete_callback_t)(void *data); +/* --- Constants for common_enums --- */ -//! The callback to copy data, e.g., bind data (if any). -typedef void *(*duckdb_copy_callback_t)(void *data); +/* --- Function pointer typedefs for common_enums --- */ -//! Used for threading, contains a task state. -//! Must be destroyed with `duckdb_destroy_task_state`. -typedef void *duckdb_task_state; +/* --- Functions for common_enums --- */ -//===--------------------------------------------------------------------===// -// Types (no explicit freeing) -//===--------------------------------------------------------------------===// +/* --- Struct definitions for common_enums --- */ -//! DATE is stored as days since 1970-01-01. -//! Use the `duckdb_from_date` and `duckdb_to_date` functions to extract individual information. -typedef struct { +/* ============================================================================ + * MODULE: common_types + * ============================================================================ */ + +/* --- Enums for common_types --- */ + +/* --- Struct forward declarations for common_types --- */ + +typedef struct duckdb_date duckdb_date; + +typedef struct duckdb_date_struct duckdb_date_struct; + +typedef struct duckdb_time duckdb_time; + +typedef struct duckdb_time_struct duckdb_time_struct; + +typedef struct duckdb_time_ns duckdb_time_ns; + +typedef struct duckdb_time_tz duckdb_time_tz; + +typedef struct duckdb_time_tz_struct duckdb_time_tz_struct; + +typedef struct duckdb_timestamp duckdb_timestamp; + +typedef struct duckdb_timestamp_struct duckdb_timestamp_struct; + +typedef struct duckdb_timestamp_s duckdb_timestamp_s; + +typedef struct duckdb_timestamp_ms duckdb_timestamp_ms; + +typedef struct duckdb_timestamp_ns duckdb_timestamp_ns; + +typedef struct duckdb_interval duckdb_interval; + +typedef struct duckdb_hugeint duckdb_hugeint; + +typedef struct duckdb_uhugeint duckdb_uhugeint; + +typedef struct duckdb_decimal duckdb_decimal; + +typedef struct duckdb_query_progress_type duckdb_query_progress_type; + +typedef struct duckdb_list_entry duckdb_list_entry; + +typedef struct duckdb_column duckdb_column; + +typedef struct duckdb_string duckdb_string; + +typedef struct duckdb_blob duckdb_blob; + +typedef struct duckdb_bit duckdb_bit; + +typedef struct duckdb_bignum duckdb_bignum; + +typedef struct duckdb_result duckdb_result; + +/* --- Types for common_types --- */ + +/* --- Constants for common_types --- */ + +/* --- Function pointer typedefs for common_types --- */ + +/* --- Functions for common_types --- */ + +/* --- Struct definitions for common_types --- */ + +/*! + * DATE is stored as days since 1970-01-01. + * + * history: + * - stable: v0.1.0 + */ +struct duckdb_date { int32_t days; -} duckdb_date; +}; -typedef struct { +/*! + * Decomposed date components. + * + * history: + * - stable: v0.2.9 + */ +struct duckdb_date_struct { int32_t year; int8_t month; int8_t day; -} duckdb_date_struct; +}; -//! TIME is stored as microseconds since 00:00:00. -//! Use the `duckdb_from_time` and `duckdb_to_time` functions to extract individual information. -typedef struct { +/*! + * TIME is stored as microseconds since 00:00:00. + * + * history: + * - stable: v0.1.0 + */ +struct duckdb_time { int64_t micros; -} duckdb_time; +}; -typedef struct { +/*! + * Decomposed time components. + * + * history: + * - stable: v0.2.9 + */ +struct duckdb_time_struct { int8_t hour; int8_t min; int8_t sec; int32_t micros; -} duckdb_time_struct; +}; -//! TIME_NS is stored as nanoseconds since 00:00:00. -typedef struct { +/*! + * TIME_NS is stored as nanoseconds since 00:00:00. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + */ +struct duckdb_time_ns { int64_t nanos; -} duckdb_time_ns; +}; -//! TIME_TZ is stored as 40 bits for the int64_t microseconds, and 24 bits for the int32_t offset. -//! Use the `duckdb_from_time_tz` function to extract individual information. -typedef struct { +/*! + * TIME_TZ stored as 40 bits for microseconds and 24 bits for offset. + * + * history: + * - stable: v0.10.0 + */ +struct duckdb_time_tz { uint64_t bits; -} duckdb_time_tz; +}; -typedef struct { +/*! + * Decomposed TIME_TZ components. + * + * history: + * - stable: v0.10.0 + */ +struct duckdb_time_tz_struct { duckdb_time_struct time; int32_t offset; -} duckdb_time_tz_struct; +}; -//! TIMESTAMP is stored as microseconds since 1970-01-01. -//! Use the `duckdb_from_timestamp` and `duckdb_to_timestamp` functions to extract individual information. -typedef struct { +/*! + * TIMESTAMP is stored as microseconds since 1970-01-01. + * + * history: + * - stable: v0.1.0 + */ +struct duckdb_timestamp { int64_t micros; -} duckdb_timestamp; +}; -typedef struct { +/*! + * Decomposed timestamp components. + * + * history: + * - stable: v0.2.9 + */ +struct duckdb_timestamp_struct { duckdb_date_struct date; duckdb_time_struct time; -} duckdb_timestamp_struct; +}; -//! TIMESTAMP_S is stored as seconds since 1970-01-01. -typedef struct { +/*! + * TIMESTAMP_S is stored as seconds since 1970-01-01. + * + * history: + * - stable: v1.2.0 + */ +struct duckdb_timestamp_s { int64_t seconds; -} duckdb_timestamp_s; +}; -//! TIMESTAMP_MS is stored as milliseconds since 1970-01-01. -typedef struct { +/*! + * TIMESTAMP_MS is stored as milliseconds since 1970-01-01. + * + * history: + * - stable: v1.2.0 + */ +struct duckdb_timestamp_ms { int64_t millis; -} duckdb_timestamp_ms; +}; -//! TIMESTAMP_NS is stored as nanoseconds since 1970-01-01. -typedef struct { +/*! + * TIMESTAMP_NS is stored as nanoseconds since 1970-01-01. + * + * history: + * - stable: v1.2.0 + */ +struct duckdb_timestamp_ns { int64_t nanos; -} duckdb_timestamp_ns; +}; -//! INTERVAL is stored in months, days, and micros. -typedef struct { +/*! + * INTERVAL is stored in months, days, and micros. + * + * history: + * - stable: v0.2.1 + */ +struct duckdb_interval { int32_t months; int32_t days; int64_t micros; -} duckdb_interval; +}; -//! HUGEINT is composed of a lower and upper component. -//! Its value is upper * 2^64 + lower. -//! For simplified usage, use `duckdb_hugeint_to_double` and `duckdb_double_to_hugeint`. -typedef struct { +/*! + * HUGEINT is composed of a lower and upper component. Value is upper * 2^64 + lower. + * + * history: + * - stable: v0.2.1 + */ +struct duckdb_hugeint { uint64_t lower; int64_t upper; -} duckdb_hugeint; +}; -//! UHUGEINT is composed of a lower and upper component. -//! Its value is upper * 2^64 + lower. -//! For simplified usage, use `duckdb_uhugeint_to_double` and `duckdb_double_to_uhugeint`. -typedef struct { +/*! + * UHUGEINT is composed of a lower and upper component. Value is upper * 2^64 + lower. + * + * history: + * - stable: v0.10.0 + */ +struct duckdb_uhugeint { uint64_t lower; uint64_t upper; -} duckdb_uhugeint; +}; -//! DECIMAL is composed of a width and a scale. -//! Their value is stored in a HUGEINT. -typedef struct { +/*! + * DECIMAL is composed of a width and a scale. Value is stored in a HUGEINT. + * + * history: + * - stable: v0.3.3 + */ +struct duckdb_decimal { uint8_t width; uint8_t scale; duckdb_hugeint value; -} duckdb_decimal; +}; -//! A type holding information about the query execution progress. -typedef struct { +/*! + * A type holding information about the query execution progress. + * + * history: + * - stable: v0.10.0 + */ +struct duckdb_query_progress_type { double percentage; uint64_t rows_processed; uint64_t total_rows_to_process; -} duckdb_query_progress_type; - -//! The internal representation of a VARCHAR (string_t). If the VARCHAR does not -//! exceed 12 characters, then we inline it. Otherwise, we inline a four-byte prefix for faster -//! string comparisons and store a pointer to the remaining characters. This is a non- -//! owning structure, i.e., it does not have to be freed. -typedef struct { - union { - struct { - uint32_t length; - char prefix[4]; - char *ptr; - } pointer; - struct { - uint32_t length; - char inlined[12]; - } inlined; - } value; -} duckdb_string_t; +}; -//! DuckDB's LISTs are composed of a 'parent' vector holding metadata of each list, -//! and a child vector holding the entries of the lists. -//! The `duckdb_list_entry` struct contains the internal representation of a LIST metadata entry. -//! A metadata entry contains the length of the list, and its offset in the child vector. -typedef struct { +/*! + * Internal representation of a LIST metadata entry. + * + * history: + * - stable: v0.7.0 + */ +struct duckdb_list_entry { uint64_t offset; uint64_t length; -} duckdb_list_entry; +}; -//! A column consists of a pointer to its internal data. Don't operate on this type directly. -//! Instead, use functions such as `duckdb_column_data`, `duckdb_nullmask_data`, -//! `duckdb_column_type`, and `duckdb_column_name`. -typedef struct { - // Deprecated, use `duckdb_column_data`. +/*! + * A column in a result. Use accessor functions rather than accessing fields directly. + * + * history: + * - stable: v0.1.0 + */ +struct duckdb_column { void *deprecated_data; - // Deprecated, use `duckdb_nullmask_data`. bool *deprecated_nullmask; - // Deprecated, use `duckdb_column_type`. duckdb_type deprecated_type; - // Deprecated, use `duckdb_column_name`. char *deprecated_name; void *internal_data; -} duckdb_column; +}; -//! 1. A standalone vector that must be destroyed, or -//! 2. A vector to a column in a data chunk that lives as long as the data chunk lives. -typedef struct _duckdb_vector { - void *internal_ptr; -} * duckdb_vector; +/*! + * A DuckDB string (char* + size). Free data with duckdb_free. + * + * history: + * - stable: v0.6.0 + */ +struct duckdb_string { + char *data; + idx_t size; +}; -//! A selection vector is a vector of indices, which usually refer to values in a vector. -//! Can be used to slice vectors, changing their length and the order of their entries. -//! Standalone selection vectors must be destroyed. -typedef struct _duckdb_selection_vector { - void *internal_ptr; -} * duckdb_selection_vector; - -//===--------------------------------------------------------------------===// -// Types (explicit freeing/destroying) -//===--------------------------------------------------------------------===// - -//! Strings are composed of a `char` pointer and a size. -//! You must free `string.data` with `duckdb_free`. -typedef struct { - char *data; - idx_t size; -} duckdb_string; - -//! BLOBs are composed of a byte pointer and a size. -//! You must free `blob.data` with `duckdb_free`. -typedef struct { +/*! + * A DuckDB BLOB (void* + size). Free data with duckdb_free. + * + * history: + * - stable: v0.2.5 + */ +struct duckdb_blob { void *data; idx_t size; -} duckdb_blob; +}; -//! BITs are composed of a byte pointer and a size. -//! BIT byte data has 0 to 7 bits of padding. -//! The first byte contains the number of padding bits. -//! The padding bits of the second byte are set to 1, starting from the MSB. -//! You must free `data` with `duckdb_free`. -typedef struct { +/*! + * A DuckDB BIT (uint8_t* + size). Free data with duckdb_free. + * + * history: + * - stable: v1.2.0 + */ +struct duckdb_bit { uint8_t *data; idx_t size; -} duckdb_bit; +}; -//! BIGNUMs are composed of a byte pointer, a size, and an `is_negative` bool. -//! The absolute value of the number is stored in `data` in little endian format. -//! You must free `data` with `duckdb_free`. -typedef struct { +/*! + * A DuckDB BIGNUM (uint8_t* + size + is_negative). The absolute value is stored in data in big endian format. Free data + * with duckdb_free. + * + * history: + * - stable: v1.2.0 + */ +struct duckdb_bignum { uint8_t *data; idx_t size; bool is_negative; -} duckdb_bignum; +}; -//! A query result consists of a pointer to its internal data. -//! Must be freed with 'duckdb_destroy_result'. -typedef struct { - // Deprecated, use `duckdb_column_count`. +/*! + * A query result. Must be freed with duckdb_destroy_result. + * + * history: + * - stable: v0.1.0 + */ +struct duckdb_result { idx_t deprecated_column_count; - // Deprecated, use `duckdb_row_count`. idx_t deprecated_row_count; - // Deprecated, use `duckdb_rows_changed`. idx_t deprecated_rows_changed; - // Deprecated, use `duckdb_column_*`-family of functions. duckdb_column *deprecated_columns; - // Deprecated, use `duckdb_result_error`. char *deprecated_error_message; void *internal_data; -} duckdb_result; +}; + +/* ============================================================================ + * MODULE: common_handles + * ============================================================================ */ + +/* --- Enums for common_handles --- */ + +/* --- Struct forward declarations for common_handles --- */ -//! A database instance cache object. Must be destroyed with `duckdb_destroy_instance_cache`. +/* --- Types for common_handles --- */ + +/*! + * A database instance cache. Must be destroyed with duckdb_destroy_instance_cache. + * + * history: + * - unstable: v1.2.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_instance_cache { void *internal_ptr; } * duckdb_instance_cache; -//! A database object. Must be closed with `duckdb_close`. +/*! + * A database object. Must be closed with duckdb_close. + * + * history: + * - stable: v0.1.0 + */ typedef struct _duckdb_database { void *internal_ptr; } * duckdb_database; -//! A connection to a duckdb database. Must be closed with `duckdb_disconnect`. +/*! + * A connection to a duckdb database. Must be closed with duckdb_disconnect. + * + * history: + * - stable: v0.1.0 + */ typedef struct _duckdb_connection { void *internal_ptr; } * duckdb_connection; -//! A client context of a duckdb connection. Must be destroyed with `duckdb_destroy_context`. +/*! + * A client context of a duckdb connection. Must be destroyed with duckdb_destroy_client_context. + * + * history: + * - unstable: v1.3.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_client_context { void *internal_ptr; } * duckdb_client_context; -//! A prepared statement is a parameterized query that allows you to bind parameters to it. -//! Must be destroyed with `duckdb_destroy_prepare`. +/*! + * A prepared statement. Must be destroyed with duckdb_destroy_prepare. + * + * history: + * - stable: v0.1.0 + */ typedef struct _duckdb_prepared_statement { void *internal_ptr; } * duckdb_prepared_statement; -//! Extracted statements. Must be destroyed with `duckdb_destroy_extracted`. +/*! + * Extracted statements. Must be destroyed with duckdb_destroy_extracted. + * + * history: + * - stable: v0.7.0 + */ typedef struct _duckdb_extracted_statements { void *internal_ptr; } * duckdb_extracted_statements; -//! The pending result represents an intermediate structure for a query that is not yet fully executed. -//! Must be destroyed with `duckdb_destroy_pending`. +/*! + * A pending query result. Must be destroyed with duckdb_destroy_pending. + * + * history: + * - stable: v0.5.0 + */ typedef struct _duckdb_pending_result { void *internal_ptr; } * duckdb_pending_result; -//! The appender enables fast data loading into DuckDB. -//! Must be destroyed with `duckdb_appender_destroy`. +/*! + * The appender enables fast data loading into DuckDB. Must be destroyed with duckdb_appender_destroy. + * + * history: + * - stable: v0.2.5 + */ typedef struct _duckdb_appender { void *internal_ptr; } * duckdb_appender; -//! The table description allows querying information about the table. -//! Must be destroyed with `duckdb_table_description_destroy`. +/*! + * The table description allows querying information about the table. Must be destroyed with + * duckdb_table_description_destroy. + * + * history: + * - stable: v1.1.0 + */ typedef struct _duckdb_table_description { void *internal_ptr; } * duckdb_table_description; -//! The configuration can be used to provide start-up options for a database. -//! Must be destroyed with `duckdb_destroy_config`. +/*! + * The configuration for opening a database. Must be destroyed with duckdb_destroy_config. + * + * history: + * - stable: v0.2.8 + */ typedef struct _duckdb_config { void *internal_ptr; } * duckdb_config; -//! A custom configuration option instance. Used to register custom options that can be set on a duckdb_config. -//! or by the user in SQL using `SET = `. +/*! + * A custom configuration option instance. Must be destroyed with duckdb_destroy_config_option. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_config_option { void *internal_ptr; } * duckdb_config_option; -//! A logical type. -//! Must be destroyed with `duckdb_destroy_logical_type`. +/*! + * A logical type. Must be destroyed with duckdb_destroy_logical_type. + * + * history: + * - stable: v0.3.3 + */ typedef struct _duckdb_logical_type { void *internal_ptr; } * duckdb_logical_type; -//! Holds extra information to register a custom logical type. -//! Reserved for future use. +/*! + * Holds extra information to register a custom logical type. Reserved for future use. + * + * history: + * - stable: v1.1.0 + */ typedef struct _duckdb_create_type_info { void *internal_ptr; } * duckdb_create_type_info; -//! Contains a data chunk of a duckdb_result. -//! Must be destroyed with `duckdb_destroy_data_chunk`. +/*! + * Contains a data chunk of a duckdb_result. Must be destroyed with duckdb_destroy_data_chunk. + * + * history: + * - stable: v0.3.3 + */ typedef struct _duckdb_data_chunk { void *internal_ptr; } * duckdb_data_chunk; -//! A value of a logical type. -//! Must be destroyed with `duckdb_destroy_value`. +/*! + * A value of a logical type. Must be destroyed with duckdb_destroy_value. + * + * history: + * - stable: v0.3.3 + */ typedef struct _duckdb_value { void *internal_ptr; } * duckdb_value; -//! Holds a recursive tree containing profiling metrics. -//! The tree matches the query plan, and has a top-level node. +/*! + * Holds a recursive tree containing profiling metrics. + * + * history: + * - stable: v1.1.0 + */ typedef struct _duckdb_profiling_info { void *internal_ptr; } * duckdb_profiling_info; -//! Holds error data. -//! Must be destroyed with `duckdb_destroy_error_data`. +/*! + * Holds error data. Must be destroyed with duckdb_destroy_error_data. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_error_data { void *internal_ptr; } * duckdb_error_data; -//! Holds a bound expression. -//! Must be destroyed with `duckdb_destroy_expression`. +/*! + * Holds a bound expression. Must be destroyed with duckdb_destroy_expression. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_expression { void *internal_ptr; } * duckdb_expression; -//===--------------------------------------------------------------------===// -// C API extension information -//===--------------------------------------------------------------------===// +/*! + * A vector, either standalone or a column in a data chunk. + * + * history: + * - stable: v0.3.3 + */ +typedef struct _duckdb_vector { + void *internal_ptr; +} * duckdb_vector; -//! Holds the state of the C API extension initialization process. -typedef struct _duckdb_extension_info { +/*! + * A selection vector defining a selection on top of a vector. Must be destroyed with duckdb_destroy_selection_vector. + * + * history: + * - unstable: v1.3.0 + * - stable: v1.5.6 + */ +typedef struct _duckdb_selection_vector { void *internal_ptr; -} * duckdb_extension_info; +} * duckdb_selection_vector; -//===--------------------------------------------------------------------===// -// Function types -//===--------------------------------------------------------------------===// +/*! + * Arrow options for transforming DuckDB schema/chunks to Arrow. Must be destroyed with duckdb_destroy_arrow_options. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + */ +typedef struct _duckdb_arrow_options { + void *internal_ptr; +} * duckdb_arrow_options; -//! Additional function info. -//! When setting this info, it is necessary to pass a destroy-callback function. -typedef struct _duckdb_function_info { +/*! + * An arrow result set. Must be destroyed with duckdb_destroy_arrow. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.5.6 + */ +typedef struct _duckdb_arrow { void *internal_ptr; -} * duckdb_function_info; +} * duckdb_arrow; -//! The bind info of a function. -//! When setting this info, it is necessary to pass a destroy-callback function. -typedef struct _duckdb_bind_info { +/*! + * An arrow stream wrapper. Must be destroyed with duckdb_destroy_arrow_stream. + * + * history: + * - stable: v0.9.0 + * - deprecated: v1.5.6 + */ +typedef struct _duckdb_arrow_stream { void *internal_ptr; -} * duckdb_bind_info; +} * duckdb_arrow_stream; -//! Additional function initialization info. -//! When setting this info, it is necessary to pass a destroy-callback function. -typedef struct _duckdb_init_info { +/*! + * An arrow schema wrapper. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.5.6 + */ +typedef struct _duckdb_arrow_schema { void *internal_ptr; -} * duckdb_init_info; +} * duckdb_arrow_schema; -//===--------------------------------------------------------------------===// -// Scalar function types -//===--------------------------------------------------------------------===// +/*! + * An arrow array wrapper. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.5.6 + */ +typedef struct _duckdb_arrow_array { + void *internal_ptr; +} * duckdb_arrow_array; -//! A scalar function. Must be destroyed with `duckdb_destroy_scalar_function`. -typedef struct _duckdb_scalar_function { +/*! + * Holds a converted Arrow schema. Must be destroyed with duckdb_destroy_arrow_converted_schema. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + */ +typedef struct _duckdb_arrow_converted_schema { void *internal_ptr; -} * duckdb_scalar_function; +} * duckdb_arrow_converted_schema; -//! A scalar function set. Must be destroyed with `duckdb_destroy_scalar_function_set`. -typedef struct _duckdb_scalar_function_set { +/*! + * File open options. Must be destroyed with duckdb_destroy_file_open_options. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef struct _duckdb_file_open_options { void *internal_ptr; -} * duckdb_scalar_function_set; +} * duckdb_file_open_options; -//! The bind function callback of the scalar function. -typedef void (*duckdb_scalar_function_bind_t)(duckdb_bind_info info); +/*! + * A file system instance. Must be destroyed with duckdb_destroy_file_system. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef struct _duckdb_file_system { + void *internal_ptr; +} * duckdb_file_system; -//! The thread-local initialization function of the scalar function. -typedef void (*duckdb_scalar_function_init_t)(duckdb_init_info info); +/*! + * A file handle. Must be destroyed with duckdb_destroy_file_handle. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef struct _duckdb_file_handle { + void *internal_ptr; +} * duckdb_file_handle; -//! The function to execute the scalar function on an input chunk. -typedef void (*duckdb_scalar_function_t)(duckdb_function_info info, duckdb_data_chunk input, duckdb_vector output); +/*! + * A handle to a database catalog. Must be destroyed with duckdb_destroy_catalog. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef struct _duckdb_catalog { + void *internal_ptr; +} * duckdb_catalog; -//===--------------------------------------------------------------------===// -// Aggregate function types -//===--------------------------------------------------------------------===// +/*! + * A handle to a catalog entry. Must be destroyed with duckdb_destroy_catalog_entry. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef struct _duckdb_catalog_entry { + void *internal_ptr; +} * duckdb_catalog_entry; -//! An aggregate function. Must be destroyed with `duckdb_destroy_aggregate_function`. +/*! + * Used for threading, contains a task state. Must be destroyed with duckdb_destroy_task_state. + * + * history: + * - stable: v0.5.0 + */ +typedef void *duckdb_task_state; + +/*! + * A scalar function. Must be destroyed with duckdb_destroy_scalar_function. + * + * history: + * - stable: v1.1.0 + */ +typedef struct _duckdb_scalar_function { + void *internal_ptr; +} * duckdb_scalar_function; + +/*! + * A scalar function set. Must be destroyed with duckdb_destroy_scalar_function_set. + * + * history: + * - stable: v1.1.0 + */ +typedef struct _duckdb_scalar_function_set { + void *internal_ptr; +} * duckdb_scalar_function_set; + +/*! + * An aggregate function. Must be destroyed with duckdb_destroy_aggregate_function. + * + * history: + * - stable: v1.1.0 + */ typedef struct _duckdb_aggregate_function { void *internal_ptr; } * duckdb_aggregate_function; -//! A aggregate function set. Must be destroyed with `duckdb_destroy_aggregate_function_set`. +/*! + * An aggregate function set. Must be destroyed with duckdb_destroy_aggregate_function_set. + * + * history: + * - stable: v1.1.0 + */ typedef struct _duckdb_aggregate_function_set { void *internal_ptr; } * duckdb_aggregate_function_set; -//! The state of an aggregate function. +/*! + * The state of an aggregate function. + * + * history: + * - stable: v1.1.0 + */ typedef struct _duckdb_aggregate_state { void *internal_ptr; } * duckdb_aggregate_state; -//! A function to return the aggregate state's size. -typedef idx_t (*duckdb_aggregate_state_size)(duckdb_function_info info); - -//! A function to initialize an aggregate state. -typedef void (*duckdb_aggregate_init_t)(duckdb_function_info info, duckdb_aggregate_state state); - -//! An optional function to destroy an aggregate state. -typedef void (*duckdb_aggregate_destroy_t)(duckdb_aggregate_state *states, idx_t count); - -//! A function to update a set of aggregate states with new values. -typedef void (*duckdb_aggregate_update_t)(duckdb_function_info info, duckdb_data_chunk input, - duckdb_aggregate_state *states); - -//! A function to combine aggregate states. -typedef void (*duckdb_aggregate_combine_t)(duckdb_function_info info, duckdb_aggregate_state *source, - duckdb_aggregate_state *target, idx_t count); - -//! A function to finalize aggregate states into a result vector. -typedef void (*duckdb_aggregate_finalize_t)(duckdb_function_info info, duckdb_aggregate_state *source, - duckdb_vector result, idx_t count, idx_t offset); - -//===--------------------------------------------------------------------===// -// Table function types -//===--------------------------------------------------------------------===// - -//! A table function. Must be destroyed with `duckdb_destroy_table_function`. +/*! + * A table function. Must be destroyed with duckdb_destroy_table_function. + * + * history: + * - stable: v0.3.3 + */ typedef struct _duckdb_table_function { void *internal_ptr; } * duckdb_table_function; -//! The bind function of the table function. -typedef void (*duckdb_table_function_bind_t)(duckdb_bind_info info); - -//! The possibly thread-local initialization function of the table function. -typedef void (*duckdb_table_function_init_t)(duckdb_init_info info); - -//! The function to generate an output chunk during table function execution. -typedef void (*duckdb_table_function_t)(duckdb_function_info info, duckdb_data_chunk output); - -//===--------------------------------------------------------------------===// -// Copy function types -//===--------------------------------------------------------------------===// +/*! + * A cast function. Must be destroyed with duckdb_destroy_cast_function. + * + * history: + * - stable: v1.1.0 + */ +typedef struct _duckdb_cast_function { + void *internal_ptr; +} * duckdb_cast_function; -//! A COPY function. Must be destroyed with `duckdb_destroy_copy_function`. +/*! + * A COPY function. Must be destroyed with duckdb_destroy_copy_function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_copy_function { void *internal_ptr; } * duckdb_copy_function; -//! Info for the bind function of a COPY function. +/*! + * Info for the bind function of a COPY function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_copy_function_bind_info { void *internal_ptr; } * duckdb_copy_function_bind_info; -//! Info for the global initialization function of a COPY function. +/*! + * Info for the global initialization function of a COPY function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_copy_function_global_init_info { void *internal_ptr; } * duckdb_copy_function_global_init_info; -//! Info for the sink function of a COPY function. +/*! + * Info for the sink function of a COPY function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_copy_function_sink_info { void *internal_ptr; } * duckdb_copy_function_sink_info; -//! Info for the finalize function of a COPY function. +/*! + * Info for the finalize function of a COPY function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef struct _duckdb_copy_function_finalize_info { void *internal_ptr; } * duckdb_copy_function_finalize_info; -//! The bind function to use when binding a COPY ... TO function. -typedef void (*duckdb_copy_function_bind_t)(duckdb_copy_function_bind_info info); - -//! The initialization function to use when initializing a COPY ... TO function. -typedef void (*duckdb_copy_function_global_init_t)(duckdb_copy_function_global_init_info info); - -//! The function to sink an input chunk into during execution of a COPY ... TO function. -typedef void (*duckdb_copy_function_sink_t)(duckdb_copy_function_sink_info info, duckdb_data_chunk input); - -//! The function to finalize the COPY ... TO function execution. -typedef void (*duckdb_copy_function_finalize_t)(duckdb_copy_function_finalize_info info); - -//===--------------------------------------------------------------------===// -// Cast types -//===--------------------------------------------------------------------===// - -//! A cast function. Must be destroyed with `duckdb_destroy_cast_function`. -typedef struct _duckdb_cast_function { - void *internal_ptr; -} * duckdb_cast_function; - -//! The function to cast from an input vector to an output vector. -typedef bool (*duckdb_cast_function_t)(duckdb_function_info info, idx_t count, duckdb_vector input, - duckdb_vector output); - -//===--------------------------------------------------------------------===// -// Replacement scan types -//===--------------------------------------------------------------------===// - -//! Additional replacement scan info. When setting this info, it is necessary to pass a destroy-callback function. +/*! + * Info passed to a replacement scan callback. + * + * history: + * - stable: v0.3.3 + */ typedef struct _duckdb_replacement_scan_info { void *internal_ptr; } * duckdb_replacement_scan_info; -//! A replacement scan function. -typedef void (*duckdb_replacement_callback_t)(duckdb_replacement_scan_info info, const char *table_name, void *data); - -//===--------------------------------------------------------------------===// -// Arrow-related types -//===--------------------------------------------------------------------===// - -//! Forward declare Arrow structs -//! It is important to notice that these structs are not defined by DuckDB but are actually Arrow external objects. -//! They're defined by the C Data Interface Arrow spec: https://arrow.apache.org/docs/format/CDataInterface.html - -struct ArrowArray; - -struct ArrowSchema; - -//! Holds an arrow query result. Must be destroyed with `duckdb_destroy_arrow`. -typedef struct _duckdb_arrow { +/*! + * A log storage instance. Must be destroyed with duckdb_destroy_log_storage. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef struct _duckdb_log_storage { void *internal_ptr; -} * duckdb_arrow; +} * duckdb_log_storage; -//! Holds an arrow array stream. Must be destroyed with `duckdb_destroy_arrow_stream`. -typedef struct _duckdb_arrow_stream { +/*! + * Additional function info. + * + * history: + * - stable: v0.3.3 + */ +typedef struct _duckdb_function_info { void *internal_ptr; -} * duckdb_arrow_stream; +} * duckdb_function_info; -//! Holds an arrow schema. Remember to release the respective ArrowSchema object. -typedef struct _duckdb_arrow_schema { +/*! + * The bind info of a function. + * + * history: + * - stable: v0.3.3 + */ +typedef struct _duckdb_bind_info { void *internal_ptr; -} * duckdb_arrow_schema; +} * duckdb_bind_info; -//! Holds an arrow converted schema (i.e., duckdb::ArrowTableSchema). -//! In practice, this object holds the information necessary to do proper conversion between Arrow Types and DuckDB -//! Types. Check duckdb/function/table/arrow/arrow_duck_schema.hpp for more details! Must be destroyed with -//! `duckdb_destroy_arrow_converted_schema` -typedef struct _duckdb_arrow_converted_schema { +/*! + * Additional function initialization info. + * + * history: + * - stable: v0.3.3 + */ +typedef struct _duckdb_init_info { void *internal_ptr; -} * duckdb_arrow_converted_schema; +} * duckdb_init_info; -//! Holds an arrow array. Remember to release the respective ArrowSchema object. -typedef struct _duckdb_arrow_array { +/*! + * Holds the state of the C API extension initialization process. + * + * history: + * - stable: v1.1.0 + */ +typedef struct _duckdb_extension_info { void *internal_ptr; -} * duckdb_arrow_array; +} * duckdb_extension_info; -//! The arrow options used when transforming the DuckDB schema and datachunks into Arrow schema and arrays. -//! Used in `duckdb_to_arrow_schema` and `duckdb_data_chunk_to_arrow` -typedef struct _duckdb_arrow_options { - void *internal_ptr; -} * duckdb_arrow_options; +/*! + * Type definition for the data pointers of selection vectors. + * + * history: + * - unstable: v1.3.0 + * - stable: v1.5.6 + */ +#ifndef DUCKDB_TYPEDEF_SEL_T +#define DUCKDB_TYPEDEF_SEL_T +typedef uint32_t sel_t; +#endif -//===--------------------------------------------------------------------===// -// Virtual File System Access -//===--------------------------------------------------------------------===// +/* --- Constants for common_handles --- */ -typedef struct _duckdb_file_open_options { - void *internal_ptr; -} * duckdb_file_open_options; +/* --- Function pointer typedefs for common_handles --- */ -typedef struct _duckdb_file_system { - void *internal_ptr; -} * duckdb_file_system; +/*! + * The callback to destroy data. + * + * history: + * - stable: v0.3.3 + */ +typedef void (*duckdb_delete_callback_t)(void *data); -typedef struct _duckdb_file_handle { - void *internal_ptr; -} * duckdb_file_handle; +/*! + * The callback to copy data. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + */ +typedef void *(*duckdb_copy_callback_t)(void *data); -//===--------------------------------------------------------------------===// -// Catalog Interface -//===--------------------------------------------------------------------===// +/*! + * The bind function of a table function. + * + * history: + * - stable: v0.3.3 + */ +typedef void (*duckdb_table_function_bind_t)(duckdb_bind_info info); -//! A handle to a database catalog. -//! Must be destroyed with `duckdb_destroy_catalog`. -typedef struct _duckdb_catalog { - void *internal_ptr; -} * duckdb_catalog; +/*! + * The init function of a table function. + * + * history: + * - stable: v0.3.3 + */ +typedef void (*duckdb_table_function_init_t)(duckdb_init_info info); -//! A handle to a catalog entry (e.g., table, view, index, etc.). -//! Must be destroyed with `duckdb_destroy_catalog_entry`. -typedef struct _duckdb_catalog_entry { - void *internal_ptr; -} * duckdb_catalog_entry; +/*! + * The main function of a table function. + * + * history: + * - stable: v0.3.3 + */ +typedef void (*duckdb_table_function_t)(duckdb_function_info info, duckdb_data_chunk output); -//===--------------------------------------------------------------------===// -// Logging Types -//===--------------------------------------------------------------------===// - -//! Holds a log storage object. -typedef struct _duckdb_log_storage { - void *internal_ptr; -} * duckdb_log_storage; +/*! + * The callback function for replacement scans. + * + * history: + * - stable: v0.3.3 + */ +typedef void (*duckdb_replacement_callback_t)(duckdb_replacement_scan_info info, const char *table_name, void *data); -//! This function is missing the logging context, which will be added later. +/*! + * The callback to write a log entry. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ typedef void (*duckdb_logger_write_log_entry_t)(void *extra_data, duckdb_timestamp *timestamp, const char *level, const char *log_type, const char *log_message); -//===--------------------------------------------------------------------===// -// DuckDB extension access -//===--------------------------------------------------------------------===// - -//! Passed to C API extension as a parameter to the entrypoint. -struct duckdb_extension_access { - //! Indicate that an error has occurred. - void (*set_error)(duckdb_extension_info info, const char *error); - //! Fetch the database on which to register the extension. - duckdb_database *(*get_database)(duckdb_extension_info info); - //! Fetch the API struct pointer. - const void *(*get_api)(duckdb_extension_info info, const char *version); -}; - -#ifndef DUCKDB_API_EXCLUDE_FUNCTIONS - -//===--------------------------------------------------------------------===// -// Functions -//===--------------------------------------------------------------------===// - -//---------------------------------------------------------------------------------------------------------------------- -// Open Connect -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to operate on the instance cache, databases, connections, as well as some metadata functions. -//---------------------------------------------------------------------------------------------------------------------- - /*! -Creates a new database instance cache. -The instance cache is necessary if a client/program (re)opens multiple databases to the same file within the same -process. Must be destroyed with 'duckdb_destroy_instance_cache'. - -* @return The database instance cache. -*/ -DUCKDB_C_API duckdb_instance_cache duckdb_create_instance_cache(); - -/*! -Creates a new database instance in the instance cache, or retrieves an existing database instance. -Must be closed with 'duckdb_close'. - -* @param instance_cache The instance cache in which to create the database, or from which to take the database. -* @param path Path to the database file on disk. Both `nullptr` and `:memory:` open or retrieve an in-memory database. -* @param out_database The resulting cached database. -* @param config (Optional) configuration used to create the database. -* @param out_error If set and the function returns `DuckDBError`, this contains the error message. -Note that the error message must be freed using `duckdb_free`. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_get_or_create_from_cache(duckdb_instance_cache instance_cache, const char *path, - duckdb_database *out_database, duckdb_config config, - char **out_error); + * The sink function of a copy function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef void (*duckdb_copy_function_sink_t)(duckdb_copy_function_sink_info info, duckdb_data_chunk input); /*! -Destroys an existing database instance cache and de-allocates its memory. - -* @param instance_cache The instance cache to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_instance_cache(duckdb_instance_cache *instance_cache); + * The finalize function of a copy function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef void (*duckdb_copy_function_finalize_t)(duckdb_copy_function_finalize_info info); /*! -Creates a new database or opens an existing database file stored at the given path. -If no path is given a new in-memory database is created instead. -The database must be closed with 'duckdb_close'. - -* @param path Path to the database file on disk. Both `nullptr` and `:memory:` open an in-memory database. -* @param out_database The result database object. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_open(const char *path, duckdb_database *out_database); + * A function to get the size of an aggregate state. + * + * history: + * - stable: v1.1.0 + */ +typedef idx_t (*duckdb_aggregate_state_size)(duckdb_function_info info); /*! -Extended version of duckdb_open. Creates a new database or opens an existing database file stored at the given path. -The database must be closed with 'duckdb_close'. - -* @param path Path to the database file on disk. Both `nullptr` and `:memory:` open an in-memory database. -* @param out_database The result database object. -* @param config (Optional) configuration used to start up the database. -* @param out_error If set and the function returns `DuckDBError`, this contains the error message. -Note that the error message must be freed using `duckdb_free`. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_open_ext(const char *path, duckdb_database *out_database, duckdb_config config, - char **out_error); + * A function to initialize an aggregate state. + * + * history: + * - stable: v1.1.0 + */ +typedef void (*duckdb_aggregate_init_t)(duckdb_function_info info, duckdb_aggregate_state state); /*! -Closes the specified database and de-allocates all memory allocated for that database. -This should be called after you are done with any database allocated through `duckdb_open` or `duckdb_open_ext`. -Note that failing to call `duckdb_close` (in case of e.g. a program crash) will not cause data corruption. -Still, it is recommended to always correctly close a database object after you are done with it. - -* @param database The database object to shut down. -*/ -DUCKDB_C_API void duckdb_close(duckdb_database *database); + * An optional function to destroy an aggregate state. + * + * history: + * - stable: v1.1.0 + */ +typedef void (*duckdb_aggregate_destroy_t)(duckdb_aggregate_state *states, idx_t count); /*! -Opens a connection to a database. Connections are required to query the database, and store transactional state -associated with the connection. -The instantiated connection should be closed using 'duckdb_disconnect'. - -* @param database The database file to connect to. -* @param out_connection The result connection object. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_connect(duckdb_database database, duckdb_connection *out_connection); + * A function to update a set of aggregate states with new values. + * + * history: + * - stable: v1.1.0 + */ +typedef void (*duckdb_aggregate_update_t)(duckdb_function_info info, duckdb_data_chunk input, + duckdb_aggregate_state *states); /*! -Interrupt running query - -* @param connection The connection to interrupt -*/ -DUCKDB_C_API void duckdb_interrupt(duckdb_connection connection); + * A function to combine aggregate states. + * + * history: + * - stable: v1.1.0 + */ +typedef void (*duckdb_aggregate_combine_t)(duckdb_function_info info, duckdb_aggregate_state *source, + duckdb_aggregate_state *target, idx_t count); /*! -Get the progress of the running query. - -* @param connection The connection running the query. -* @return The query progress type containing progress information. -*/ -DUCKDB_C_API duckdb_query_progress_type duckdb_query_progress(duckdb_connection connection); + * A function to finalize aggregate states into a result vector. + * + * history: + * - stable: v1.1.0 + */ +typedef void (*duckdb_aggregate_finalize_t)(duckdb_function_info info, duckdb_aggregate_state *source, + duckdb_vector result, idx_t count, idx_t offset); /*! -Closes the specified connection and de-allocates all memory allocated for that connection. - -* @param connection The connection to close. -*/ -DUCKDB_C_API void duckdb_disconnect(duckdb_connection *connection); + * The bind function of a scalar function. + * + * history: + * - unstable: v1.3.0 + * - stable: v1.5.6 + */ +typedef void (*duckdb_scalar_function_bind_t)(duckdb_bind_info info); /*! -Retrieves the client context of the connection. - -* @param connection The connection. -* @param out_context The client context of the connection. Must be destroyed with `duckdb_destroy_client_context`. -*/ -DUCKDB_C_API void duckdb_connection_get_client_context(duckdb_connection connection, - duckdb_client_context *out_context); + * The init function of a scalar function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef void (*duckdb_scalar_function_init_t)(duckdb_init_info info); /*! -Retrieves the arrow options of the connection. - -* @param connection The connection. -*/ -DUCKDB_C_API void duckdb_connection_get_arrow_options(duckdb_connection connection, - duckdb_arrow_options *out_arrow_options); + * The main function of a scalar function. + * + * history: + * - stable: v1.1.0 + */ +typedef void (*duckdb_scalar_function_t)(duckdb_function_info info, duckdb_data_chunk input, duckdb_vector output); /*! -Returns the connection id of the client context. - -* @param context The client context. -* @return The connection id of the client context. -*/ -DUCKDB_C_API idx_t duckdb_client_context_get_connection_id(duckdb_client_context context); + * The function to cast from an input vector to an output vector. + * + * history: + * - stable: v1.1.0 + */ +typedef bool (*duckdb_cast_function_t)(duckdb_function_info info, idx_t count, duckdb_vector input, + duckdb_vector output); /*! -Destroys the client context and deallocates its memory. - -* @param context The client context to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_client_context(duckdb_client_context *context); + * The bind function of a copy function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef void (*duckdb_copy_function_bind_t)(duckdb_copy_function_bind_info info); /*! -Destroys the arrow options and deallocates its memory. - -* @param arrow_options The arrow options to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_arrow_options(duckdb_arrow_options *arrow_options); + * The global initialization function of a copy function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + */ +typedef void (*duckdb_copy_function_global_init_t)(duckdb_copy_function_global_init_info info); -/*! -Returns the version of the linked DuckDB, with a version postfix for dev versions +/* --- Functions for common_handles --- */ -Usually used for developing C extensions that must return this for a compatibility check. -*/ -DUCKDB_C_API const char *duckdb_library_version(); +/* --- Struct definitions for common_handles --- */ -/*! -Get the list of (fully qualified) table names of the query. +/* ============================================================================ + * MODULE: datetime_helpers + * ============================================================================ */ -* @param connection The connection for which to get the table names. -* @param query The query for which to get the table names. -* @param qualified Returns fully qualified table names (catalog.schema.table), if set to true, else only the (not -escaped) table names. -* @return A duckdb_value of type VARCHAR[] containing the (fully qualified) table names of the query. Must be destroyed -with duckdb_destroy_value. -*/ -DUCKDB_C_API duckdb_value duckdb_get_table_names(duckdb_connection connection, const char *query, bool qualified); +/* --- Enums for datetime_helpers --- */ -//---------------------------------------------------------------------------------------------------------------------- -// Configuration -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to interact with a `duckdb_config`, which is the configuration parameter for opening a database. -//---------------------------------------------------------------------------------------------------------------------- +/* --- Struct forward declarations for datetime_helpers --- */ -/*! -Initializes an empty configuration object that can be used to provide start-up options for the DuckDB instance -through `duckdb_open_ext`. -The duckdb_config must be destroyed using 'duckdb_destroy_config' +/* --- Types for datetime_helpers --- */ -This will always succeed unless there is a malloc failure. +/* --- Constants for datetime_helpers --- */ -Note that `duckdb_destroy_config` should always be called on the resulting config, even if the function returns -`DuckDBError`. +/* --- Function pointer typedefs for datetime_helpers --- */ -* @param out_config The result configuration object. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_create_config(duckdb_config *out_config); +/* --- Functions for datetime_helpers --- */ +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) /*! -This returns the total amount of configuration options available for usage with `duckdb_get_config_flag`. - -This should not be called in a loop as it internally loops over all the options. - -* @return The amount of config options available. -*/ -DUCKDB_C_API size_t duckdb_config_count(); + * Decompose a `duckdb_date` object into year, month and date (stored as `duckdb_date_struct`). + * + * history: + * - stable: v0.2.9 + * + * @param date The date object, as obtained from a `DUCKDB_TYPE_DATE` column. + * @return duckdb_date_struct + */ +DUCKDB_C_API duckdb_date_struct duckdb_from_date(duckdb_date date); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) /*! -Obtains a human-readable name and description of a specific configuration option. This can be used to e.g. -display configuration options. This will succeed unless `index` is out of range (i.e. `>= duckdb_config_count`). - -The result name or description MUST NOT be freed. - -* @param index The index of the configuration option (between 0 and `duckdb_config_count`) -* @param out_name A name of the configuration flag. -* @param out_description A description of the configuration flag. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_get_config_flag(size_t index, const char **out_name, const char **out_description); + * Re-compose a `duckdb_date` from year, month and date (`duckdb_date_struct`). + * + * history: + * - stable: v0.2.9 + * + * @param date The year, month and date stored in a `duckdb_date_struct`. + * @return duckdb_date + */ +DUCKDB_C_API duckdb_date duckdb_to_date(duckdb_date_struct date); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) /*! -Sets the specified option for the specified configuration. The configuration option is indicated by name. -To obtain a list of config options, see `duckdb_get_config_flag`. - -In the source code, configuration options are defined in `config.cpp`. - -This can fail if either the name is invalid, or if the value provided for the option is invalid. - -* @param config The configuration object to set the option on. -* @param name The name of the configuration flag to set. -* @param option The value to set the configuration flag to. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_set_config(duckdb_config config, const char *name, const char *option); + * Test a `duckdb_date` to see if it is a finite value. + * + * history: + * - stable: v0.10.0 + * + * @param date The date object, as obtained from a `DUCKDB_TYPE_DATE` column. + * @return bool + */ +DUCKDB_C_API bool duckdb_is_finite_date(duckdb_date date); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) /*! -Destroys the specified configuration object and de-allocates all memory allocated for the object. + * Decompose a `duckdb_time` object into hour, minute, second and microsecond (stored as `duckdb_time_struct`). + * + * history: + * - stable: v0.2.9 + * + * @param time The time object, as obtained from a `DUCKDB_TYPE_TIME` column. + * @return duckdb_time_struct + */ +DUCKDB_C_API duckdb_time_struct duckdb_from_time(duckdb_time time); +#endif -* @param config The configuration object to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_config(duckdb_config *config); +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) +/*! + * Create a `duckdb_time_tz` object from micros and a timezone offset. + * + * history: + * - stable: v0.10.0 + * + * @param micros The microsecond component of the time. + * @param offset The timezone offset component of the time. + * @return duckdb_time_tz + */ +DUCKDB_C_API duckdb_time_tz duckdb_create_time_tz(int64_t micros, int32_t offset); +#endif -//---------------------------------------------------------------------------------------------------------------------- -// Error Data -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to operate on `duckdb_error_data`, which contains, for example, the error type and message. Please use this -// interface for all new C API functions, as it supersedes previous error handling approaches. -//---------------------------------------------------------------------------------------------------------------------- +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) +/*! + * Decompose a TIME_TZ objects into micros and a timezone offset. + * + * Use `duckdb_from_time` to further decompose the micros into hour, minute, second and microsecond. + * + * history: + * - stable: v0.10.0 + * + * @param micros The time object, as obtained from a `DUCKDB_TYPE_TIME_TZ` column. + * @return duckdb_time_tz_struct + */ +DUCKDB_C_API duckdb_time_tz_struct duckdb_from_time_tz(duckdb_time_tz micros); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) /*! -Creates duckdb_error_data. -Must be destroyed with `duckdb_destroy_error_data`. - -* @param type The error type. -* @param message The error message. -* @return The error data. -*/ -DUCKDB_C_API duckdb_error_data duckdb_create_error_data(duckdb_error_type type, const char *message); + * Re-compose a `duckdb_time` from hour, minute, second and microsecond (`duckdb_time_struct`). + * + * history: + * - stable: v0.2.9 + * + * @param time The hour, minute, second and microsecond in a `duckdb_time_struct`. + * @return duckdb_time + */ +DUCKDB_C_API duckdb_time duckdb_to_time(duckdb_time_struct time); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) /*! -Destroys the error data and deallocates its memory. - -* @param error_data The error data to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_error_data(duckdb_error_data *error_data); + * Decompose a `duckdb_timestamp` object into a `duckdb_timestamp_struct`. + * + * history: + * - stable: v0.2.9 + * + * @param ts The ts object, as obtained from a `DUCKDB_TYPE_TIMESTAMP` column. + * @return duckdb_timestamp_struct + */ +DUCKDB_C_API duckdb_timestamp_struct duckdb_from_timestamp(duckdb_timestamp ts); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) /*! -Returns the duckdb_error_type of the error data. - -* @param error_data The error data. -* @return The error type. -*/ -DUCKDB_C_API duckdb_error_type duckdb_error_data_error_type(duckdb_error_data error_data); + * Re-compose a `duckdb_timestamp` from a duckdb_timestamp_struct. + * + * history: + * - stable: v0.2.9 + * + * @param ts The de-composed elements in a `duckdb_timestamp_struct`. + * @return duckdb_timestamp + */ +DUCKDB_C_API duckdb_timestamp duckdb_to_timestamp(duckdb_timestamp_struct ts); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) /*! -Returns the error message of the error data. Must not be freed. - -* @param error_data The error data. -* @return The error message. -*/ -DUCKDB_C_API const char *duckdb_error_data_message(duckdb_error_data error_data); + * Test a `duckdb_timestamp` to see if it is a finite value. + * + * history: + * - stable: v0.10.0 + * + * @param ts The duckdb_timestamp object, as obtained from a `DUCKDB_TYPE_TIMESTAMP` column. + * @return bool + */ +DUCKDB_C_API bool duckdb_is_finite_timestamp(duckdb_timestamp ts); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 2, 0) /*! -Returns whether the error data contains an error or not. - -* @param error_data The error data. -* @return True, if the error data contains an exception, else false. -*/ -DUCKDB_C_API bool duckdb_error_data_has_error(duckdb_error_data error_data); - -//---------------------------------------------------------------------------------------------------------------------- -// Query Execution -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to obtain a `duckdb_result` and to retrieve metadata from it. -//---------------------------------------------------------------------------------------------------------------------- + * Test a `duckdb_timestamp_s` to see if it is a finite value. + * + * history: + * - stable: v1.2.0 + * + * @param ts The duckdb_timestamp_s object, as obtained from a `DUCKDB_TYPE_TIMESTAMP_S` column. + * @return bool + */ +DUCKDB_C_API bool duckdb_is_finite_timestamp_s(duckdb_timestamp_s ts); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 2, 0) /*! -Executes a SQL query within a connection and stores the full (materialized) result in the out_result pointer. -If the query fails to execute, DuckDBError is returned and the error message can be retrieved by calling -`duckdb_result_error`. - -Note that after running `duckdb_query`, `duckdb_destroy_result` must be called on the result object even if the -query fails, otherwise the error stored within the result will not be freed correctly. + * Test a `duckdb_timestamp_ms` to see if it is a finite value. + * + * history: + * - stable: v1.2.0 + * + * @param ts The duckdb_timestamp_ms object, as obtained from a `DUCKDB_TYPE_TIMESTAMP_MS` column. + * @return bool + */ +DUCKDB_C_API bool duckdb_is_finite_timestamp_ms(duckdb_timestamp_ms ts); +#endif -* @param connection The connection to perform the query in. -* @param query The SQL query to run. -* @param out_result The query result. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_query(duckdb_connection connection, const char *query, duckdb_result *out_result); +#if DUCKDB_API_VERSION_AT_LEAST(1, 2, 0) +/*! + * Test a `duckdb_timestamp_ns` to see if it is a finite value. + * + * history: + * - stable: v1.2.0 + * + * @param ts The duckdb_timestamp_ns object, as obtained from a `DUCKDB_TYPE_TIMESTAMP_NS` column. + * @return bool + */ +DUCKDB_C_API bool duckdb_is_finite_timestamp_ns(duckdb_timestamp_ns ts); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) /*! -Closes the result and de-allocates all memory allocated for that result. + * Converts a duckdb_hugeint object (as obtained from a `DUCKDB_TYPE_HUGEINT` column) into a double. + * + * history: + * - stable: v0.2.9 + * + * @param val The hugeint value. + * @return double + */ +DUCKDB_C_API double duckdb_hugeint_to_double(duckdb_hugeint val); +#endif -* @param result The result to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_result(duckdb_result *result); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) +/*! + * Converts a double value to a duckdb_hugeint object. + * + * If the conversion fails because the double value is too big the result will be 0. + * + * history: + * - stable: v0.2.9 + * + * @param val The double value. + * @return duckdb_hugeint + */ +DUCKDB_C_API duckdb_hugeint duckdb_double_to_hugeint(double val); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) /*! -Returns the column name of the specified column. The result should not need to be freed; the column names will -automatically be destroyed when the result is destroyed. + * Converts a duckdb_uhugeint object (as obtained from a `DUCKDB_TYPE_UHUGEINT` column) into a double. + * + * history: + * - stable: v0.10.0 + * + * @param val The uhugeint value. + * @return double + */ +DUCKDB_C_API double duckdb_uhugeint_to_double(duckdb_uhugeint val); +#endif -Returns `NULL` if the column is out of range. +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) +/*! + * Converts a double value to a duckdb_uhugeint object. + * + * If the conversion fails because the double value is too big the result will be 0. + * + * history: + * - stable: v0.10.0 + * + * @param val The double value. + * @return duckdb_uhugeint + */ +DUCKDB_C_API duckdb_uhugeint duckdb_double_to_uhugeint(double val); +#endif -* @param result The result object to fetch the column name from. -* @param col The column index. -* @return The column name of the specified column. -*/ -DUCKDB_C_API const char *duckdb_column_name(duckdb_result *result, idx_t col); +#if DUCKDB_API_VERSION_AT_LEAST(0, 6, 0) +/*! + * Converts a double value to a duckdb_decimal object. + * + * If the conversion fails because the double value is too big, or the width/scale are invalid the result will be 0. + * + * history: + * - stable: v0.6.0 + * + * @param val The double value. + * @param width + * @param scale + * @return duckdb_decimal + */ +DUCKDB_C_API duckdb_decimal duckdb_double_to_decimal(double val, uint8_t width, uint8_t scale); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Returns the column type of the specified column. + * Converts a duckdb_decimal object (as obtained from a `DUCKDB_TYPE_DECIMAL` column) into a double. + * + * history: + * - stable: v0.3.3 + * + * @param val The decimal value. + * @return double + */ +DUCKDB_C_API double duckdb_decimal_to_double(duckdb_decimal val); +#endif -Returns `DUCKDB_TYPE_INVALID` if the column is out of range. +/* --- Struct definitions for datetime_helpers --- */ -* @param result The result object to fetch the column type from. -* @param col The column index. -* @return The column type of the specified column. -*/ -DUCKDB_C_API duckdb_type duckdb_column_type(duckdb_result *result, idx_t col); +/* ============================================================================ + * MODULE: aggregate_function + * ============================================================================ */ -/*! -Returns the statement type of the statement that was executed +/* --- Enums for aggregate_function --- */ -* @param result The result object to fetch the statement type from. -* @return duckdb_statement_type value or DUCKDB_STATEMENT_TYPE_INVALID -*/ -DUCKDB_C_API duckdb_statement_type duckdb_result_statement_type(duckdb_result result); +/* --- Struct forward declarations for aggregate_function --- */ -/*! -Returns the logical column type of the specified column. +/* --- Types for aggregate_function --- */ -The return type of this call should be destroyed with `duckdb_destroy_logical_type`. +/* --- Constants for aggregate_function --- */ -Returns `NULL` if the column is out of range. +/* --- Function pointer typedefs for aggregate_function --- */ -* @param result The result object to fetch the column type from. -* @param col The column index. -* @return The logical column type of the specified column. -*/ -DUCKDB_C_API duckdb_logical_type duckdb_column_logical_type(duckdb_result *result, idx_t col); +/* --- Functions for aggregate_function --- */ +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -Returns the arrow options associated with the given result. These options are definitions of how the arrow arrays/schema -should be produced. -* @param result The result object to fetch arrow options from. -* @return The arrow options associated with the given result. This must be destroyed with -`duckdb_destroy_arrow_options`. -*/ -DUCKDB_C_API duckdb_arrow_options duckdb_result_get_arrow_options(duckdb_result *result); + * Creates a new empty aggregate function. + * + * The return value should be destroyed with `duckdb_destroy_aggregate_function`. + * + * history: + * - stable: v1.1.0 + * + * @return duckdb_aggregate_function + */ +DUCKDB_C_API duckdb_aggregate_function duckdb_create_aggregate_function(void); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -Returns the number of columns present in a the result object. - -* @param result The result object. -* @return The number of columns present in the result object. -*/ -DUCKDB_C_API idx_t duckdb_column_count(duckdb_result *result); + * Destroys the given aggregate function object. + * + * history: + * - stable: v1.1.0 + * + * @param aggregate_function + * @return void + */ +DUCKDB_C_API void duckdb_destroy_aggregate_function(duckdb_aggregate_function *aggregate_function); +#endif -#ifndef DUCKDB_API_NO_DEPRECATED -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Sets the name of the given aggregate function. + * + * history: + * - stable: v1.1.0 + * + * @param aggregate_function The aggregate function + * @param name The name of the aggregate function + * @return void + */ +DUCKDB_C_API void duckdb_aggregate_function_set_name(duckdb_aggregate_function aggregate_function, const char *name); +#endif -Returns the number of rows present in the result object. +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Adds a parameter to the aggregate function. + * + * history: + * - stable: v1.1.0 + * + * @param aggregate_function The aggregate function. + * @param type The parameter type. Cannot contain INVALID. + * @return void + */ +DUCKDB_C_API void duckdb_aggregate_function_add_parameter(duckdb_aggregate_function aggregate_function, + duckdb_logical_type type); +#endif -* @param result The result object. -* @return The number of rows present in the result object. -*/ -DUCKDB_C_API idx_t duckdb_row_count(duckdb_result *result); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Sets the return type of the aggregate function. + * + * history: + * - stable: v1.1.0 + * + * @param aggregate_function The aggregate function. + * @param type The return type. Cannot contain INVALID or ANY. + * @return void + */ +DUCKDB_C_API void duckdb_aggregate_function_set_return_type(duckdb_aggregate_function aggregate_function, + duckdb_logical_type type); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Sets the main functions of the aggregate function. + * + * history: + * - stable: v1.1.0 + * + * @param aggregate_function The aggregate function + * @param state_size state size + * @param state_init state init function + * @param update update states + * @param combine combine states + * @param finalize finalize states + * @return void + */ +DUCKDB_C_API void duckdb_aggregate_function_set_functions(duckdb_aggregate_function aggregate_function, + duckdb_aggregate_state_size state_size, + duckdb_aggregate_init_t state_init, + duckdb_aggregate_update_t update, + duckdb_aggregate_combine_t combine, + duckdb_aggregate_finalize_t finalize); #endif -/*! -Returns the number of rows changed by the query stored in the result. This is relevant only for INSERT/UPDATE/DELETE -queries. For other queries the rows_changed will be 0. +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Sets the state destructor callback of the aggregate function (optional) + * + * history: + * - stable: v1.1.0 + * + * @param aggregate_function The aggregate function + * @param destroy state destroy callback + * @return void + */ +DUCKDB_C_API void duckdb_aggregate_function_set_destructor(duckdb_aggregate_function aggregate_function, + duckdb_aggregate_destroy_t destroy); +#endif -* @param result The result object. -* @return The number of rows changed. -*/ -DUCKDB_C_API idx_t duckdb_rows_changed(duckdb_result *result); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Register the aggregate function object within the given connection. + * + * The function requires at least a name, functions and a return type. + * + * If the function is incomplete or a function with this name already exists DuckDBError is returned. + * + * history: + * - stable: v1.1.0 + * + * @param con The connection to register it in. + * @param aggregate_function + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_register_aggregate_function(duckdb_connection con, + duckdb_aggregate_function aggregate_function); +#endif -#ifndef DUCKDB_API_NO_DEPRECATED +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -**DEPRECATED**: Prefer using `duckdb_result_get_chunk` instead. + * Sets the NULL handling of the aggregate function to SPECIAL_HANDLING. + * + * history: + * - stable: v1.1.0 + * + * @param aggregate_function The aggregate function + * @return void + */ +DUCKDB_C_API void duckdb_aggregate_function_set_special_handling(duckdb_aggregate_function aggregate_function); +#endif -Returns the data of a specific column of a result in columnar format. +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Assigns extra information to the scalar function that can be fetched during binding, etc. + * + * history: + * - stable: v1.1.0 + * + * @param aggregate_function The aggregate function + * @param extra_info The extra information + * @param destroy The callback that will be called to destroy the extra information (if any) + * @return void + */ +DUCKDB_C_API void duckdb_aggregate_function_set_extra_info(duckdb_aggregate_function aggregate_function, + void *extra_info, duckdb_delete_callback_t destroy); +#endif -The function returns a dense array which contains the result data. The exact type stored in the array depends on the -corresponding duckdb_type (as provided by `duckdb_column_type`). For the exact type by which the data should be -accessed, see the comments in [the types section](types) or the `DUCKDB_TYPE` enum. +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Retrieves the extra info of the function as set in `duckdb_aggregate_function_set_extra_info`. + * + * history: + * - stable: v1.1.0 + * + * @param info The info object + * @return void* + */ +DUCKDB_C_API void *duckdb_aggregate_function_get_extra_info(duckdb_function_info info); +#endif -For example, for a column of type `DUCKDB_TYPE_INTEGER`, rows can be accessed in the following manner: -```c -int32_t *data = (int32_t *) duckdb_column_data(&result, 0); -printf("Data for row %d: %d\n", row, data[row]); -``` +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Report that an error has occurred while executing the aggregate function. + * + * history: + * - stable: v1.1.0 + * + * @param info The info object + * @param error The error message + * @return void + */ +DUCKDB_C_API void duckdb_aggregate_function_set_error(duckdb_function_info info, const char *error); +#endif -* @param result The result object to fetch the column data from. -* @param col The column index. -* @return The column data of the specified column. -*/ -DUCKDB_C_API void *duckdb_column_data(duckdb_result *result, idx_t col); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Creates a new empty aggregate function set. + * + * The return value should be destroyed with `duckdb_destroy_aggregate_function_set`. + * + * history: + * - stable: v1.1.0 + * + * @param name + * @return duckdb_aggregate_function_set + */ +DUCKDB_C_API duckdb_aggregate_function_set duckdb_create_aggregate_function_set(const char *name); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -**DEPRECATED**: Prefer using `duckdb_result_get_chunk` instead. + * Destroys the given aggregate function set object. + * + * history: + * - stable: v1.1.0 + * + * @param aggregate_function_set + * @return void + */ +DUCKDB_C_API void duckdb_destroy_aggregate_function_set(duckdb_aggregate_function_set *aggregate_function_set); +#endif -Returns the nullmask of a specific column of a result in columnar format. The nullmask indicates for every row -whether or not the corresponding row is `NULL`. If a row is `NULL`, the values present in the array provided -by `duckdb_column_data` are undefined. +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Adds the aggregate function as a new overload to the aggregate function set. + * + * Returns DuckDBError if the function could not be added, for example if the overload already exists. + * + * history: + * - stable: v1.1.0 + * + * @param set The aggregate function set + * @param function The function to add + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_add_aggregate_function_to_set(duckdb_aggregate_function_set set, + duckdb_aggregate_function function); +#endif -```c -int32_t *data = (int32_t *) duckdb_column_data(&result, 0); -bool *nullmask = duckdb_nullmask_data(&result, 0); -if (nullmask[row]) { - printf("Data for row %d: NULL\n", row); -} else { - printf("Data for row %d: %d\n", row, data[row]); -} -``` +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Register the aggregate function set within the given connection. + * + * The set requires at least a single valid overload. + * + * If the set is incomplete or a function with this name already exists DuckDBError is returned. + * + * history: + * - stable: v1.1.0 + * + * @param con The connection to register it in. + * @param set The function set to register + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_register_aggregate_function_set(duckdb_connection con, + duckdb_aggregate_function_set set); +#endif -* @param result The result object to fetch the nullmask from. -* @param col The column index. -* @return The nullmask of the specified column. -*/ -DUCKDB_C_API bool *duckdb_nullmask_data(duckdb_result *result, idx_t col); +/* --- Struct definitions for aggregate_function --- */ -#endif +/* ============================================================================ + * MODULE: appender + * ============================================================================ */ -/*! -Returns the error message contained within the result. The error is only set if `duckdb_query` returns `DuckDBError`. +/* --- Enums for appender --- */ -The result of this function must not be freed. It will be cleaned up when `duckdb_destroy_result` is called. +/* --- Struct forward declarations for appender --- */ -* @param result The result object to fetch the error from. -* @return The error of the result. -*/ -DUCKDB_C_API const char *duckdb_result_error(duckdb_result *result); +/* --- Types for appender --- */ -/*! -Returns the result error type contained within the result. The error is only set if `duckdb_query` returns -`DuckDBError`. +/* --- Constants for appender --- */ -* @param result The result object to fetch the error from. -* @return The error type of the result. -*/ -DUCKDB_C_API duckdb_error_type duckdb_result_error_type(duckdb_result *result); +/* --- Function pointer typedefs for appender --- */ -#ifndef DUCKDB_API_NO_DEPRECATED +/* --- Functions for appender --- */ + +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) /*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * Creates an appender object. + * + * Note that the object must be destroyed with `duckdb_appender_destroy`. + * + * history: + * - stable: v0.2.5 + * + * @param connection The connection context to create the appender in. + * @param schema The schema of the table to append to, or `nullptr` for the default schema. + * @param table The table name to append to. + * @param out_appender The resulting appender object. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_create(duckdb_connection connection, const char *schema, const char *table, + duckdb_appender *out_appender); +#endif -Fetches a data chunk from the duckdb_result. This function should be called repeatedly until the result is exhausted. +#if DUCKDB_API_VERSION_AT_LEAST(1, 2, 0) +/*! + * Creates an appender object. + * + * Note that the object must be destroyed with `duckdb_appender_destroy`. + * + * history: + * - stable: v1.2.0 + * + * @param connection The connection context to create the appender in. + * @param catalog The catalog of the table to append to, or `nullptr` for the default catalog. + * @param schema The schema of the table to append to, or `nullptr` for the default schema. + * @param table The table name to append to. + * @param out_appender The resulting appender object. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_create_ext(duckdb_connection connection, const char *catalog, + const char *schema, const char *table, + duckdb_appender *out_appender); +#endif -The result must be destroyed with `duckdb_destroy_data_chunk`. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Creates an appender object that executes the given query with any data appended to it. + * + * Note that the object must be destroyed with `duckdb_appender_destroy`. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param connection The connection context to create the appender in. + * @param query The query to execute, can be an INSERT, DELETE, UPDATE or MERGE INTO statement. + * @param column_count The number of columns to append. + * @param types The types of the columns to append. + * @param table_name (optionally) the table name used to refer to the appended data, defaults to "appended_data". + * @param column_names (optionally) the list of column names, defaults to "col1", "col2", ... + * @param out_appender The resulting appender object. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_create_query(duckdb_connection connection, const char *query, + idx_t column_count, duckdb_logical_type *types, + const char *table_name, const char **column_names, + duckdb_appender *out_appender); +#endif -This function supersedes all `duckdb_value` functions, as well as the `duckdb_column_data` and `duckdb_nullmask_data` -functions. It results in significantly better performance, and should be preferred in newer code-bases. +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) +/*! + * Returns the number of columns that belong to the appender. If there is no active column list, then this equals the + * table's physical columns. + * + * history: + * - stable: v0.10.0 + * + * @param appender The appender to get the column count from. + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_appender_column_count(duckdb_appender appender); +#endif -If this function is used, none of the other result functions can be used and vice versa (i.e. this function cannot be -mixed with the legacy result functions). +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) +/*! + * Returns the type of the column at the specified index. This is either a type in the active column list, or the same + * type as a column in the receiving table. + * + * Note: The resulting type must be destroyed with `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.10.0 + * + * @param appender The appender to get the column type from. + * @param col_idx The index of the column to get the type of. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_appender_column_type(duckdb_appender appender, idx_t col_idx); +#endif -Use `duckdb_result_chunk_count` to figure out how many chunks there are in the result. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Returns the error data associated with the appender. Must be destroyed with duckdb_destroy_error_data. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param appender The appender to get the error data from. + * @return duckdb_error_data + */ +DUCKDB_C_API duckdb_error_data duckdb_appender_error_data(duckdb_appender appender); +#endif -* @param result The result object to fetch the data chunk from. -* @param chunk_index The chunk index to fetch from. -* @return The resulting data chunk. Returns `NULL` if the chunk index is out of bounds. -*/ -DUCKDB_C_API duckdb_data_chunk duckdb_result_get_chunk(duckdb_result result, idx_t chunk_index); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Flush the appender to the table, forcing the cache of the appender to be cleared. Requires a connection with no open + * result. If flushing the data triggers a constraint violation or any other error, then all data is invalidated, and + * this function returns DuckDBError. It is not possible to append more values. Call duckdb_appender_error_data to + * obtain the error data followed by duckdb_appender_destroy to destroy the invalidated appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender The appender to flush. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_flush(duckdb_appender appender); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Clears all buffered data from the appender without flushing it to the table. This discards any data that has been + * appended but not yet written. The appender can continue to be used after clearing. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param appender The appender to clear. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_clear(duckdb_appender appender); +#endif -Checks if the type of the internal result is StreamQueryResult. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Closes the appender by flushing all intermediate states and closing it for further appends. If flushing the data + * triggers a constraint violation or any other error, then all data is invalidated, and this function returns + * DuckDBError. Call duckdb_appender_error_data to obtain the error data followed by duckdb_appender_destroy to destroy + * the invalidated appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender The appender to flush and close. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_close(duckdb_appender appender); +#endif -* @param result The result object to check. -* @return Whether or not the result object is of the type StreamQueryResult -*/ -DUCKDB_C_API bool duckdb_result_is_streaming(duckdb_result result); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Closes the appender by flushing all intermediate states to the table and destroying it. By destroying it, this + * function de-allocates all memory associated with the appender. If flushing the data triggers a constraint violation, + * then all data is invalidated, and this function returns DuckDBError. Due to the destruction of the appender, it is no + * longer possible to obtain the specific error message with duckdb_appender_error. Therefore, call + * duckdb_appender_close before destroying the appender, if you need insights into the specific error. + * + * history: + * - stable: v0.2.5 + * + * @param appender The appender to flush, close and destroy. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_destroy(duckdb_appender *appender); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(1, 2, 0) +/*! + * Appends a column to the active column list of the appender. Immediately flushes all previous data. + * + * The active column list specifies all columns that are expected when flushing the data. Any non-active columns are + * filled with their default values, or NULL. + * + * history: + * - stable: v1.2.0 + * + * @param appender The appender to add the column to. + * @param name + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_add_column(duckdb_appender appender, const char *name); +#endif -Returns the number of data chunks present in the result. +#if DUCKDB_API_VERSION_AT_LEAST(1, 2, 0) +/*! + * Removes all columns from the active column list of the appender, resetting the appender to treat all columns as + * active. Immediately flushes all previous data. + * + * history: + * - stable: v1.2.0 + * + * @param appender The appender to clear the columns from. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_clear_columns(duckdb_appender appender); +#endif -* @param result The result object -* @return Number of data chunks present in the result. -*/ -DUCKDB_C_API idx_t duckdb_result_chunk_count(duckdb_result result); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * A nop function, provided for backwards compatibility reasons. Does nothing. Only `duckdb_appender_end_row` is + * required. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_begin_row(duckdb_appender appender); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Finish the current row of appends. After end_row is called, the next row can be appended. + * + * history: + * - stable: v0.2.5 + * + * @param appender The appender. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_appender_end_row(duckdb_appender appender); #endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -Returns the return_type of the given result, or DUCKDB_RETURN_TYPE_INVALID on error + * Append a DEFAULT value (NULL if DEFAULT not available for column) to the appender. + * + * history: + * - stable: v1.1.0 + * + * @param appender + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_default(duckdb_appender appender); +#endif -* @param result The result object -* @return The return_type -*/ -DUCKDB_C_API duckdb_result_type duckdb_result_return_type(duckdb_result result); - -//---------------------------------------------------------------------------------------------------------------------- -// Safe Fetch Functions -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Deprecated functions to interact with a `duckdb_result`. -// -// DEPRECATION NOTICE: -// This function group is deprecated and scheduled for removal. -// -// USE INSTEAD: -// To access the values in a result, use `duckdb_fetch_chunk` repeatedly. For each chunk, use the `duckdb_data_chunk` -// interface to access any columns and their values. -//---------------------------------------------------------------------------------------------------------------------- +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Append a DEFAULT value, at the specified row and column, (NULL if DEFAULT not available for column) to the chunk + * created from the specified appender. The default value of the column must be a constant value. Non-deterministic + * expressions like nextval('seq') or random() are not supported. + * + * history: + * - unstable: v1.2.0 + * - stable: v1.5.6 + * + * @param appender The appender to get the default value from. + * @param chunk The data chunk to append the default value to. + * @param col The chunk column index to append the default value to. + * @param row The chunk row index to append the default value to. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_default_to_chunk(duckdb_appender appender, duckdb_data_chunk chunk, idx_t col, + idx_t row); +#endif -#ifndef DUCKDB_API_NO_DEPRECATED -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a bool value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_bool(duckdb_appender appender, bool value); +#endif -* @return The boolean value at the specified location, or false if the value cannot be converted. -*/ -DUCKDB_C_API bool duckdb_value_boolean(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append an int8_t value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_int8(duckdb_appender appender, int8_t value); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append an int16_t value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_int16(duckdb_appender appender, int16_t value); +#endif -* @return The int8_t value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API int8_t duckdb_value_int8(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append an int32_t value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_int32(duckdb_appender appender, int32_t value); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append an int64_t value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_int64(duckdb_appender appender, int64_t value); +#endif -* @return The int16_t value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API int16_t duckdb_value_int16(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) +/*! + * Append a duckdb_hugeint value to the appender. + * + * history: + * - stable: v0.2.9 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_hugeint(duckdb_appender appender, duckdb_hugeint value); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a uint8_t value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_uint8(duckdb_appender appender, uint8_t value); +#endif -* @return The int32_t value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API int32_t duckdb_value_int32(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a uint16_t value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_uint16(duckdb_appender appender, uint16_t value); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a uint32_t value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_uint32(duckdb_appender appender, uint32_t value); +#endif -* @return The int64_t value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API int64_t duckdb_value_int64(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a uint64_t value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_uint64(duckdb_appender appender, uint64_t value); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) +/*! + * Append a duckdb_uhugeint value to the appender. + * + * history: + * - stable: v0.10.0 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_uhugeint(duckdb_appender appender, duckdb_uhugeint value); +#endif -* @return The duckdb_hugeint value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API duckdb_hugeint duckdb_value_hugeint(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a float value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_float(duckdb_appender appender, float value); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a double value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_double(duckdb_appender appender, double value); +#endif -* @return The duckdb_uhugeint value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API duckdb_uhugeint duckdb_value_uhugeint(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) +/*! + * Append a duckdb_date value to the appender. + * + * history: + * - stable: v0.2.9 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_date(duckdb_appender appender, duckdb_date value); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) +/*! + * Append a duckdb_time value to the appender. + * + * history: + * - stable: v0.2.9 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_time(duckdb_appender appender, duckdb_time value); +#endif -* @return The duckdb_decimal value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API duckdb_decimal duckdb_value_decimal(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) +/*! + * Append a duckdb_timestamp value to the appender. + * + * history: + * - stable: v0.2.9 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_timestamp(duckdb_appender appender, duckdb_timestamp value); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) +/*! + * Append a duckdb_interval value to the appender. + * + * history: + * - stable: v0.2.9 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_interval(duckdb_appender appender, duckdb_interval value); +#endif -* @return The uint8_t value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API uint8_t duckdb_value_uint8(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a varchar value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param val + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_varchar(duckdb_appender appender, const char *val); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a varchar value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param val + * @param length + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_varchar_length(duckdb_appender appender, const char *val, idx_t length); +#endif -* @return The uint16_t value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API uint16_t duckdb_value_uint16(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) +/*! + * Append a blob value to the appender. + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @param data + * @param length + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_blob(duckdb_appender appender, const void *data, idx_t length); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 5) /*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * Append a NULL value to the appender (of any type). + * + * history: + * - stable: v0.2.5 + * + * @param appender + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_null(duckdb_appender appender); +#endif -* @return The uint32_t value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API uint32_t duckdb_value_uint32(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(1, 2, 0) +/*! + * Append a duckdb_value to the appender. + * + * history: + * - stable: v1.2.0 + * + * @param appender + * @param value + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_value(duckdb_appender appender, duckdb_value value); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Appends a pre-filled data chunk to the specified appender. Attempts casting, if the data chunk types do not match the + * active appender types. + * + * history: + * - stable: v0.3.3 + * + * @param appender The appender to append to. + * @param chunk The data chunk to append. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_append_data_chunk(duckdb_appender appender, duckdb_data_chunk chunk); +#endif -* @return The uint64_t value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API uint64_t duckdb_value_uint64(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 9) && (DUCKDB_API_VERSION_BELOW(1, 4, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. Use duckdb_appender_error_data + * instead. + * + * Returns the error message associated with the appender. If the appender has no error message, this returns `nullptr` + * instead. + * + * The error message should not be freed. It will be de-allocated when `duckdb_appender_destroy` is called. + * + * history: + * - stable: v0.2.9 + * - deprecated: v1.4.0 + * + * @param appender The appender to get the error from. + * @return const char* + */ +DUCKDB_C_API const char *duckdb_appender_error(duckdb_appender appender); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +/* --- Struct definitions for appender --- */ -* @return The float value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API float duckdb_value_float(duckdb_result *result, idx_t col, idx_t row); +/* ============================================================================ + * MODULE: arrow + * ============================================================================ */ -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +/* --- Enums for arrow --- */ -* @return The double value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API double duckdb_value_double(duckdb_result *result, idx_t col, idx_t row); +/* --- Struct forward declarations for arrow --- */ -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +/* --- Types for arrow --- */ -* @return The duckdb_date value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API duckdb_date duckdb_value_date(duckdb_result *result, idx_t col, idx_t row); +/* --- Constants for arrow --- */ -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +/* --- Function pointer typedefs for arrow --- */ -* @return The duckdb_time value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API duckdb_time duckdb_value_time(duckdb_result *result, idx_t col, idx_t row); +/* --- Functions for arrow --- */ +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) /*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. - -* @return The duckdb_timestamp value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API duckdb_timestamp duckdb_value_timestamp(duckdb_result *result, idx_t col, idx_t row); + * Transforms a DuckDB Schema into an Arrow Schema + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param arrow_options The Arrow settings used to produce arrow. + * @param types The DuckDB logical types for each column in the schema. + * @param names The names for each column in the schema. + * @param column_count The number of columns that exist in the schema. + * @param out_schema The resulting arrow schema. Must be destroyed with `out_schema->release(out_schema)`. + * @return duckdb_error_data + */ +DUCKDB_C_API duckdb_error_data duckdb_to_arrow_schema(duckdb_arrow_options arrow_options, duckdb_logical_type *types, + const char **names, idx_t column_count, + struct ArrowSchema *out_schema); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Transforms a DuckDB data chunk into an Arrow array. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param arrow_options The Arrow settings used to produce arrow. + * @param chunk The DuckDB data chunk to convert. + * @param out_arrow_array The output Arrow structure that will hold the converted data. Must be released with + * `out_arrow_array->release(out_arrow_array)` + * @return duckdb_error_data + */ +DUCKDB_C_API duckdb_error_data duckdb_data_chunk_to_arrow(duckdb_arrow_options arrow_options, duckdb_data_chunk chunk, + struct ArrowArray *out_arrow_array); +#endif -* @return The duckdb_interval value at the specified location, or 0 if the value cannot be converted. -*/ -DUCKDB_C_API duckdb_interval duckdb_value_interval(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Transforms an Arrow Schema into a DuckDB Schema. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param connection The connection to get the transformation settings from. + * @param schema The input Arrow schema. Must be released with `schema->release(schema)`. + * @param out_types The Arrow converted schema with extra information about the arrow types. Must be destroyed with + * `duckdb_destroy_arrow_converted_schema`. + * @return duckdb_error_data + */ +DUCKDB_C_API duckdb_error_data duckdb_schema_from_arrow(duckdb_connection connection, struct ArrowSchema *schema, + duckdb_arrow_converted_schema *out_types); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Transforms an Arrow array into a DuckDB data chunk. The data chunk will retain ownership of the underlying Arrow + * data. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param connection The connection to get the transformation settings from. + * @param arrow_array The input Arrow array. Data ownership is passed on to DuckDB's DataChunk, the underlying object + * does not need to be released and won't have ownership of the data. + * @param converted_schema The Arrow converted schema with extra information about the arrow types. + * @param out_chunk The resulting DuckDB data chunk. Must be destroyed by duckdb_destroy_data_chunk. + * @return duckdb_error_data + */ +DUCKDB_C_API duckdb_error_data duckdb_data_chunk_from_arrow(duckdb_connection connection, + struct ArrowArray *arrow_array, + duckdb_arrow_converted_schema converted_schema, + duckdb_data_chunk *out_chunk); +#endif -* @return The text value at the specified location as a null-terminated string, or nullptr if the value cannot be -converted. The result must be freed with `duckdb_free`. -*/ -DUCKDB_C_API char *duckdb_value_varchar(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the arrow converted schema and de-allocates all memory allocated for that arrow converted schema. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param arrow_converted_schema The arrow converted schema to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_arrow_converted_schema(duckdb_arrow_converted_schema *arrow_converted_schema); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Executes a SQL query within a connection and stores the full (materialized) result in an arrow structure. If the + * query fails to execute, DuckDBError is returned and the error message can be retrieved by calling + * `duckdb_query_arrow_error`. + * + * Note that after running `duckdb_query_arrow`, `duckdb_destroy_arrow` must be called on the result object even if the + * query fails, otherwise the error stored within the result will not be freed correctly. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.0.0 + * + * @param connection The connection to perform the query in. + * @param query The SQL query to run. + * @param out_result The query result. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_query_arrow(duckdb_connection connection, const char *query, duckdb_arrow *out_result); +#endif -* @return The string value at the specified location. Attempts to cast the result value to string. -*/ -DUCKDB_C_API duckdb_string duckdb_value_string(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Fetch the internal arrow schema from the arrow result. Remember to call release on the respective ArrowSchema object. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.0.0 + * + * @param result The result to fetch the schema from. + * @param out_schema The output schema. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_query_arrow_schema(duckdb_arrow result, duckdb_arrow_schema *out_schema); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 9, 0) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Fetch the internal arrow schema from the prepared statement. Remember to call release on the respective ArrowSchema + * object. + * + * history: + * - stable: v0.9.0 + * - deprecated: v1.0.0 + * + * @param prepared The prepared statement to fetch the schema from. + * @param out_schema The output schema. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_prepared_arrow_schema(duckdb_prepared_statement prepared, + duckdb_arrow_schema *out_schema); +#endif -* @return The char* value at the specified location. ONLY works on VARCHAR columns and does not auto-cast. -If the column is NOT a VARCHAR column this function will return NULL. +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Convert a data chunk into an arrow struct array. Remember to call release on the respective ArrowArray object. + * + * history: + * - stable: v0.10.0 + * - deprecated: v1.0.0 + * + * @param result The result object the data chunk have been fetched from. + * @param chunk The data chunk to convert. + * @param out_array The output array. + * @return void + */ +DUCKDB_C_API void duckdb_result_arrow_array(duckdb_result result, duckdb_data_chunk chunk, + duckdb_arrow_array *out_array); +#endif -The result must NOT be freed. -*/ -DUCKDB_C_API char *duckdb_value_varchar_internal(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Fetch an internal arrow struct array from the arrow result. Remember to call release on the respective ArrowArray + * object. + * + * This function can be called multiple time to get next chunks, which will free the previous out_array. So consume the + * out_array before calling this function again. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.0.0 + * + * @param result The result to fetch the array from. + * @param out_array The output array. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_query_arrow_array(duckdb_arrow result, duckdb_arrow_array *out_array); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Returns the number of columns present in the arrow result object. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.0.0 + * + * @param result The result object. + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_arrow_column_count(duckdb_arrow result); +#endif -* @return The char* value at the specified location. ONLY works on VARCHAR columns and does not auto-cast. -If the column is NOT a VARCHAR column this function will return NULL. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Returns the number of rows present in the arrow result object. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.0.0 + * + * @param result The result object. + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_arrow_row_count(duckdb_arrow result); +#endif -The result must NOT be freed. -*/ -DUCKDB_C_API duckdb_string duckdb_value_string_internal(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Returns the number of rows changed by the query stored in the arrow result. This is relevant only for + * INSERT/UPDATE/DELETE queries. For other queries the rows_changed will be 0. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.0.0 + * + * @param result The result object. + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_arrow_rows_changed(duckdb_arrow result); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Returns the error message contained within the result. The error is only set if `duckdb_query_arrow` returns + * `DuckDBError`. + * + * The error message should not be freed. It will be de-allocated when `duckdb_destroy_arrow` is called. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.0.0 + * + * @param result The result object to fetch the error from. + * @return const char* + */ +DUCKDB_C_API const char *duckdb_query_arrow_error(duckdb_arrow result); +#endif -* @return The duckdb_blob value at the specified location. Returns a blob with blob.data set to nullptr if the -value cannot be converted. The resulting field "blob.data" must be freed with `duckdb_free.` -*/ -DUCKDB_C_API duckdb_blob duckdb_value_blob(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Closes the result and de-allocates all memory allocated for the arrow result. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.0.0 + * + * @param result The result to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_arrow(duckdb_arrow *result); +#endif -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Releases the arrow array stream and de-allocates its memory. + * + * history: + * - stable: v0.10.0 + * - deprecated: v1.0.0 + * + * @param stream_p The arrow array stream to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_arrow_stream(duckdb_arrow_stream *stream_p); +#endif -* @return Returns true if the value at the specified index is NULL, and false otherwise. -*/ -DUCKDB_C_API bool duckdb_value_is_null(duckdb_result *result, idx_t col, idx_t row); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Executes the prepared statement with the given bound parameters, and returns an arrow query result. Note that after + * running `duckdb_execute_prepared_arrow`, `duckdb_destroy_arrow` must be called on the result object. + * + * history: + * - stable: v0.2.8 + * - deprecated: v1.0.0 + * + * @param prepared_statement The prepared statement to execute. + * @param out_result The query result. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_execute_prepared_arrow(duckdb_prepared_statement prepared_statement, + duckdb_arrow *out_result); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 9, 0) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Scans the Arrow stream and creates a view with the given name. + * + * history: + * - stable: v0.9.0 + * - deprecated: v1.0.0 + * + * @param connection The connection on which to execute the scan. + * @param table_name Name of the temporary view to create. + * @param arrow Arrow stream wrapper. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_arrow_scan(duckdb_connection connection, const char *table_name, + duckdb_arrow_stream arrow); #endif -//---------------------------------------------------------------------------------------------------------------------- -// Helpers -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Generic and `duckdb_string_t` helper functions. -//---------------------------------------------------------------------------------------------------------------------- +#if DUCKDB_API_VERSION_AT_LEAST(0, 9, 0) && (DUCKDB_API_VERSION_BELOW(1, 0, 0) || DUCKDB_API_ALLOW_DEPRECATED) +/*! + * **DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * + * Scans the Arrow array and creates a view with the given name. Note that after running `duckdb_arrow_array_scan`, + * `duckdb_destroy_arrow_stream` must be called on the out stream. + * + * history: + * - stable: v0.9.0 + * - deprecated: v1.0.0 + * + * @param connection The connection on which to execute the scan. + * @param table_name Name of the temporary view to create. + * @param arrow_schema Arrow schema wrapper. + * @param arrow_array Arrow array wrapper. + * @param out_stream Output array stream that wraps around the passed schema, for releasing/deleting once done. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_arrow_array_scan(duckdb_connection connection, const char *table_name, + duckdb_arrow_schema arrow_schema, duckdb_arrow_array arrow_array, + duckdb_arrow_stream *out_stream); +#endif -/*! -Allocate `size` bytes of memory using the duckdb internal malloc function. Any memory allocated in this manner -should be freed using `duckdb_free`. +/* --- Struct definitions for arrow --- */ -* @param size The number of bytes to allocate. -* @return A pointer to the allocated memory region. -*/ -DUCKDB_C_API void *duckdb_malloc(size_t size); +/* ============================================================================ + * MODULE: cast_function + * ============================================================================ */ -/*! -Free a value returned from `duckdb_malloc`, `duckdb_value_varchar`, `duckdb_value_blob`, or -`duckdb_value_string`. +/* --- Enums for cast_function --- */ -* @param ptr The memory region to de-allocate. -*/ -DUCKDB_C_API void duckdb_free(void *ptr); +/* --- Struct forward declarations for cast_function --- */ -/*! -The internal vector size used by DuckDB. -This is the amount of tuples that will fit into a data chunk created by `duckdb_create_data_chunk`. +/* --- Types for cast_function --- */ -* @return The vector size. -*/ -DUCKDB_C_API idx_t duckdb_vector_size(); +/* --- Constants for cast_function --- */ -/*! -Whether or not the duckdb_string_t value is inlined. -This means that the data of the string does not have a separate allocation. +/* --- Function pointer typedefs for cast_function --- */ -*/ -DUCKDB_C_API bool duckdb_string_is_inlined(duckdb_string_t string); +/* --- Functions for cast_function --- */ +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -Get the string length of a string_t - -* @param string The string to get the length of. -* @return The length. -*/ -DUCKDB_C_API uint32_t duckdb_string_t_length(duckdb_string_t string); + * Creates a new cast function object. + * + * history: + * - stable: v1.1.0 + * + * @return duckdb_cast_function + */ +DUCKDB_C_API duckdb_cast_function duckdb_create_cast_function(void); +#endif -/*! -Get a pointer to the string data of a string_t +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Sets the source type of the cast function. + * + * history: + * - stable: v1.1.0 + * + * @param cast_function The cast function object. + * @param source_type The source type to set. + * @return void + */ +DUCKDB_C_API void duckdb_cast_function_set_source_type(duckdb_cast_function cast_function, + duckdb_logical_type source_type); +#endif -* @param string The string to get the pointer to. -* @return The pointer. -*/ -DUCKDB_C_API const char *duckdb_string_t_data(duckdb_string_t *string); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Sets the target type of the cast function. + * + * history: + * - stable: v1.1.0 + * + * @param cast_function The cast function object. + * @param target_type The target type to set. + * @return void + */ +DUCKDB_C_API void duckdb_cast_function_set_target_type(duckdb_cast_function cast_function, + duckdb_logical_type target_type); +#endif -/*! -Checks if a string is valid UTF-8. +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Sets the "cost" of implicitly casting the source type to the target type using this function. + * + * history: + * - stable: v1.1.0 + * + * @param cast_function The cast function object. + * @param cost The cost to set. + * @return void + */ +DUCKDB_C_API void duckdb_cast_function_set_implicit_cast_cost(duckdb_cast_function cast_function, int64_t cost); +#endif -* @param str The string to check -* @param len The length of the string (in bytes) -* @return nullptr if the string is valid UTF-8. Otherwise, a duckdb_error_data containing error information. Must be -destroyed with `duckdb_destroy_error_data`. -*/ -DUCKDB_C_API duckdb_error_data duckdb_valid_utf8_check(const char *str, idx_t len); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Sets the actual cast function to use. + * + * history: + * - stable: v1.1.0 + * + * @param cast_function The cast function object. + * @param function The function to set. + * @return void + */ +DUCKDB_C_API void duckdb_cast_function_set_function(duckdb_cast_function cast_function, + duckdb_cast_function_t function); +#endif -//---------------------------------------------------------------------------------------------------------------------- -// Date Time Timestamp Helpers -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to convert from and to `duckdb_[date, time, time_tz, timestamp]`. -// `duckdb_is_finite_timestamp[_s, _ms, _ns]` helper functions. -//---------------------------------------------------------------------------------------------------------------------- +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Assigns extra information to the cast function that can be fetched during execution, etc. + * + * history: + * - stable: v1.1.0 + * + * @param cast_function + * @param extra_info The extra information + * @param destroy The callback that will be called to destroy the extra information (if any) + * @return void + */ +DUCKDB_C_API void duckdb_cast_function_set_extra_info(duckdb_cast_function cast_function, void *extra_info, + duckdb_delete_callback_t destroy); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -Decompose a `duckdb_date` object into year, month and date (stored as `duckdb_date_struct`). - -* @param date The date object, as obtained from a `DUCKDB_TYPE_DATE` column. -* @return The `duckdb_date_struct` with the decomposed elements. -*/ -DUCKDB_C_API duckdb_date_struct duckdb_from_date(duckdb_date date); + * Retrieves the extra info of the function as set in `duckdb_cast_function_set_extra_info`. + * + * history: + * - stable: v1.1.0 + * + * @param info The info object. + * @return void* + */ +DUCKDB_C_API void *duckdb_cast_function_get_extra_info(duckdb_function_info info); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -Re-compose a `duckdb_date` from year, month and date (`duckdb_date_struct`). + * Get the cast execution mode from the given function info. + * + * history: + * - stable: v1.1.0 + * + * @param info The info object. + * @return duckdb_cast_mode + */ +DUCKDB_C_API duckdb_cast_mode duckdb_cast_function_get_cast_mode(duckdb_function_info info); +#endif -* @param date The year, month and date stored in a `duckdb_date_struct`. -* @return The `duckdb_date` element. -*/ -DUCKDB_C_API duckdb_date duckdb_to_date(duckdb_date_struct date); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Report that an error has occurred while executing the cast function. + * + * history: + * - stable: v1.1.0 + * + * @param info The info object. + * @param error The error message. + * @return void + */ +DUCKDB_C_API void duckdb_cast_function_set_error(duckdb_function_info info, const char *error); +#endif -/*! -Test a `duckdb_date` to see if it is a finite value. +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Report that an error has occurred while executing the cast function, setting the corresponding output row to NULL. + * + * history: + * - stable: v1.1.0 + * + * @param info The info object. + * @param error The error message. + * @param row The index of the row within the output vector to set to NULL. + * @param output The output vector. + * @return void + */ +DUCKDB_C_API void duckdb_cast_function_set_row_error(duckdb_function_info info, const char *error, idx_t row, + duckdb_vector output); +#endif -* @param date The date object, as obtained from a `DUCKDB_TYPE_DATE` column. -* @return True if the date is finite, false if it is ±infinity. -*/ -DUCKDB_C_API bool duckdb_is_finite_date(duckdb_date date); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Registers a cast function within the given connection. + * + * history: + * - stable: v1.1.0 + * + * @param con The connection to use. + * @param cast_function The cast function to register. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_register_cast_function(duckdb_connection con, duckdb_cast_function cast_function); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -Decompose a `duckdb_time` object into hour, minute, second and microsecond (stored as `duckdb_time_struct`). + * Destroys the cast function object. + * + * history: + * - stable: v1.1.0 + * + * @param cast_function The cast function object. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_cast_function(duckdb_cast_function *cast_function); +#endif -* @param time The time object, as obtained from a `DUCKDB_TYPE_TIME` column. -* @return The `duckdb_time_struct` with the decomposed elements. -*/ -DUCKDB_C_API duckdb_time_struct duckdb_from_time(duckdb_time time); +/* --- Struct definitions for cast_function --- */ -/*! -Create a `duckdb_time_tz` object from micros and a timezone offset. +/* ============================================================================ + * MODULE: catalog + * ============================================================================ */ -* @param micros The microsecond component of the time. -* @param offset The timezone offset component of the time. -* @return The `duckdb_time_tz` element. -*/ -DUCKDB_C_API duckdb_time_tz duckdb_create_time_tz(int64_t micros, int32_t offset); +/* --- Enums for catalog --- */ -/*! -Decompose a TIME_TZ objects into micros and a timezone offset. +/* --- Struct forward declarations for catalog --- */ -Use `duckdb_from_time` to further decompose the micros into hour, minute, second and microsecond. +/* --- Types for catalog --- */ -* @param micros The time object, as obtained from a `DUCKDB_TYPE_TIME_TZ` column. -*/ -DUCKDB_C_API duckdb_time_tz_struct duckdb_from_time_tz(duckdb_time_tz micros); +/* --- Constants for catalog --- */ -/*! -Re-compose a `duckdb_time` from hour, minute, second and microsecond (`duckdb_time_struct`). +/* --- Function pointer typedefs for catalog --- */ -* @param time The hour, minute, second and microsecond in a `duckdb_time_struct`. -* @return The `duckdb_time` element. -*/ -DUCKDB_C_API duckdb_time duckdb_to_time(duckdb_time_struct time); +/* --- Functions for catalog --- */ +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) /*! -Decompose a `duckdb_timestamp` object into a `duckdb_timestamp_struct`. + * Retrieve a database catalog instance by name. This function can only be called from within the context of an active + * transaction, e.g. during execution of a registered function callback. Otherwise returns `nullptr`. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param context The client context. + * @param catalog_name The name of the catalog. + * @return duckdb_catalog + */ +DUCKDB_C_API duckdb_catalog duckdb_client_context_get_catalog(duckdb_client_context context, const char *catalog_name); +#endif -* @param ts The ts object, as obtained from a `DUCKDB_TYPE_TIMESTAMP` column. -* @return The `duckdb_timestamp_struct` with the decomposed elements. -*/ -DUCKDB_C_API duckdb_timestamp_struct duckdb_from_timestamp(duckdb_timestamp ts); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieve the "type name" of the given catalog. E.g. for a DuckDB database, this returns 'duckdb'. The returned string + * is owned by the catalog and remains valid until the catalog is destroyed. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param catalog The catalog. + * @return const char* + */ +DUCKDB_C_API const char *duckdb_catalog_get_type_name(duckdb_catalog catalog); +#endif -/*! -Re-compose a `duckdb_timestamp` from a duckdb_timestamp_struct. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieve a catalog entry from the given catalog by type, schema name and entry name. The returned catalog entry + * remains valid for the duration of the current transaction. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param catalog The catalog. + * @param context The client context. + * @param entry_type The type of the catalog entry to retrieve. + * @param schema_name The schema name of the catalog entry. + * @param entry_name The name of the catalog entry. + * @return duckdb_catalog_entry + */ +DUCKDB_C_API duckdb_catalog_entry duckdb_catalog_get_entry(duckdb_catalog catalog, duckdb_client_context context, + duckdb_catalog_entry_type entry_type, + const char *schema_name, const char *entry_name); +#endif -* @param ts The de-composed elements in a `duckdb_timestamp_struct`. -* @return The `duckdb_timestamp` element. -*/ -DUCKDB_C_API duckdb_timestamp duckdb_to_timestamp(duckdb_timestamp_struct ts); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the given catalog instance. + * + * Note that this does not actually "drop" the contents of the catalog; it merely frees the C API handle. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param catalog The catalog instance to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_catalog(duckdb_catalog *catalog); +#endif -/*! -Test a `duckdb_timestamp` to see if it is a finite value. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Get the type of the given catalog entry. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param entry The catalog entry. + * @return duckdb_catalog_entry_type + */ +DUCKDB_C_API duckdb_catalog_entry_type duckdb_catalog_entry_get_type(duckdb_catalog_entry entry); +#endif -* @param ts The duckdb_timestamp object, as obtained from a `DUCKDB_TYPE_TIMESTAMP` column. -* @return True if the timestamp is finite, false if it is ±infinity. -*/ -DUCKDB_C_API bool duckdb_is_finite_timestamp(duckdb_timestamp ts); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Get the name of the given catalog entry. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param entry The catalog entry. + * @return const char* + */ +DUCKDB_C_API const char *duckdb_catalog_entry_get_name(duckdb_catalog_entry entry); +#endif -/*! -Test a `duckdb_timestamp_s` to see if it is a finite value. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the given catalog entry instance. + * + * Note that this does not actually "drop" the catalog entry from the database catalog; it merely frees the C API + * handle. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param entry The catalog entry instance to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_catalog_entry(duckdb_catalog_entry *entry); +#endif -* @param ts The duckdb_timestamp_s object, as obtained from a `DUCKDB_TYPE_TIMESTAMP_S` column. -* @return True if the timestamp is finite, false if it is ±infinity. -*/ -DUCKDB_C_API bool duckdb_is_finite_timestamp_s(duckdb_timestamp_s ts); +/* --- Struct definitions for catalog --- */ -/*! -Test a `duckdb_timestamp_ms` to see if it is a finite value. +/* ============================================================================ + * MODULE: config_option + * ============================================================================ */ -* @param ts The duckdb_timestamp_ms object, as obtained from a `DUCKDB_TYPE_TIMESTAMP_MS` column. -* @return True if the timestamp is finite, false if it is ±infinity. -*/ -DUCKDB_C_API bool duckdb_is_finite_timestamp_ms(duckdb_timestamp_ms ts); +/* --- Enums for config_option --- */ -/*! -Test a `duckdb_timestamp_ns` to see if it is a finite value. +/* --- Struct forward declarations for config_option --- */ -* @param ts The duckdb_timestamp_ns object, as obtained from a `DUCKDB_TYPE_TIMESTAMP_NS` column. -* @return True if the timestamp is finite, false if it is ±infinity. -*/ -DUCKDB_C_API bool duckdb_is_finite_timestamp_ns(duckdb_timestamp_ns ts); +/* --- Types for config_option --- */ -//---------------------------------------------------------------------------------------------------------------------- -// Hugeint and Uhugeint Helpers -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to convert from and to `duckdb_[hugeint, uhugeint]`. -//---------------------------------------------------------------------------------------------------------------------- +/* --- Constants for config_option --- */ -/*! -Converts a duckdb_hugeint object (as obtained from a `DUCKDB_TYPE_HUGEINT` column) into a double. +/* --- Function pointer typedefs for config_option --- */ -* @param val The hugeint value. -* @return The converted `double` element. -*/ -DUCKDB_C_API double duckdb_hugeint_to_double(duckdb_hugeint val); +/* --- Functions for config_option --- */ +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) /*! -Converts a double value to a duckdb_hugeint object. - -If the conversion fails because the double value is too big the result will be 0. + * Creates a configuration option instance. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @return duckdb_config_option + */ +DUCKDB_C_API duckdb_config_option duckdb_create_config_option(void); +#endif -* @param val The double value. -* @return The converted `duckdb_hugeint` element. -*/ -DUCKDB_C_API duckdb_hugeint duckdb_double_to_hugeint(double val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the given configuration option instance. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param option The configuration option instance to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_config_option(duckdb_config_option *option); +#endif -/*! -Converts a duckdb_uhugeint object (as obtained from a `DUCKDB_TYPE_UHUGEINT` column) into a double. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the name of the configuration option. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param option The configuration option instance. + * @param name The name to set. + * @return void + */ +DUCKDB_C_API void duckdb_config_option_set_name(duckdb_config_option option, const char *name); +#endif -* @param val The uhugeint value. -* @return The converted `double` element. -*/ -DUCKDB_C_API double duckdb_uhugeint_to_double(duckdb_uhugeint val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the type of the configuration option. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param option The configuration option instance. + * @param type The type to set. + * @return void + */ +DUCKDB_C_API void duckdb_config_option_set_type(duckdb_config_option option, duckdb_logical_type type); +#endif -/*! -Converts a double value to a duckdb_uhugeint object. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the default value of the configuration option. If the type of this option has already been set with + * `duckdb_config_option_set_type`, the value is cast to the type. Otherwise, the type is inferred from the value. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param option The configuration option instance. + * @param default_value The default value to set. + * @return void + */ +DUCKDB_C_API void duckdb_config_option_set_default_value(duckdb_config_option option, duckdb_value default_value); +#endif -If the conversion fails because the double value is too big the result will be 0. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the default scope of the configuration option. If not set, this defaults to + * `DUCKDB_CONFIG_OPTION_SCOPE_SESSION`. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param option The configuration option instance. + * @param default_scope The default scope to set. + * @return void + */ +DUCKDB_C_API void duckdb_config_option_set_default_scope(duckdb_config_option option, + duckdb_config_option_scope default_scope); +#endif -* @param val The double value. -* @return The converted `duckdb_uhugeint` element. -*/ -DUCKDB_C_API duckdb_uhugeint duckdb_double_to_uhugeint(double val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the description of the configuration option. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param option The configuration option instance. + * @param description The description to set. + * @return void + */ +DUCKDB_C_API void duckdb_config_option_set_description(duckdb_config_option option, const char *description); +#endif -//---------------------------------------------------------------------------------------------------------------------- -// Decimal Helpers -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to convert from and to `duckdb_decimal`. -//---------------------------------------------------------------------------------------------------------------------- +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Registers the given configuration option on the specified connection. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param connection The connection to register the option on. + * @param option The configuration option instance to register. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_register_config_option(duckdb_connection connection, duckdb_config_option option); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the value of a configuration option by name from the given client context. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param context The client context. + * @param name The name of the configuration option to retrieve. + * @param out_scope Output parameter to optionally store the scope that the configuration option was retrieved from. If + * this is `nullptr`, the scope is not returned. If the requested option does not exist the scope is set to + * `DUCKDB_CONFIG_OPTION_SCOPE_INVALID`. + * @return duckdb_value + */ +DUCKDB_C_API duckdb_value duckdb_client_context_get_config_option(duckdb_client_context context, const char *name, + duckdb_config_option_scope *out_scope); +#endif + +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -Converts a double value to a duckdb_decimal object. + * Returns the root node of the profiling information. Returns nullptr, if profiling is not enabled. + * + * history: + * - stable: v1.1.0 + * + * @param connection A connection object. + * @return duckdb_profiling_info + */ +DUCKDB_C_API duckdb_profiling_info duckdb_get_profiling_info(duckdb_connection connection); +#endif -If the conversion fails because the double value is too big, or the width/scale are invalid the result will be 0. +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Returns the value of the metric of the current profiling info node. Returns nullptr, if the metric does not exist or + * is not enabled. Currently, the value holds a string, and you can retrieve the string by calling the corresponding + * function: char *duckdb_get_varchar(duckdb_value value). + * + * history: + * - stable: v1.1.0 + * + * @param info A profiling information object. + * @param key The name of the requested metric. + * @return duckdb_value + */ +DUCKDB_C_API duckdb_value duckdb_profiling_info_get_value(duckdb_profiling_info info, const char *key); +#endif -* @param val The double value. -* @return The converted `duckdb_decimal` element. -*/ -DUCKDB_C_API duckdb_decimal duckdb_double_to_decimal(double val, uint8_t width, uint8_t scale); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Returns the key-value metric map of this profiling node as a MAP duckdb_value. The individual elements are accessible + * via the duckdb_value MAP functions. + * + * history: + * - stable: v1.1.0 + * + * @param info A profiling information object. + * @return duckdb_value + */ +DUCKDB_C_API duckdb_value duckdb_profiling_info_get_metrics(duckdb_profiling_info info); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) /*! -Converts a duckdb_decimal object (as obtained from a `DUCKDB_TYPE_DECIMAL` column) into a double. + * Returns the number of children in the current profiling info node. + * + * history: + * - stable: v1.1.0 + * + * @param info A profiling information object. + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_profiling_info_get_child_count(duckdb_profiling_info info); +#endif -* @param val The decimal value. -* @return The converted `double` element. -*/ -DUCKDB_C_API double duckdb_decimal_to_double(duckdb_decimal val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Returns the child node at the specified index. + * + * history: + * - stable: v1.1.0 + * + * @param info A profiling information object. + * @param index The index of the child node. + * @return duckdb_profiling_info + */ +DUCKDB_C_API duckdb_profiling_info duckdb_profiling_info_get_child(duckdb_profiling_info info, idx_t index); +#endif -//---------------------------------------------------------------------------------------------------------------------- -// Prepared Statements -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// A prepared statement is a parameterized query, and you can bind parameters to it. Prepared statements are commonly -// used to easily supply parameters to functions and avoid SQL injection attacks. They also speed up queries that are -// executed repeatedly with different parameters. That is because the query is only parsed, bound, optimized and planned -// once during the prepare stage, rather than once per execution, if it is possible to resolve all parameter types. -// -// For example: -// SELECT * FROM tbl WHERE id = ? -// Or a query with multiple parameters: -// SELECT * FROM tbl WHERE id = $1 OR name = $2 -//---------------------------------------------------------------------------------------------------------------------- +/* --- Struct definitions for config_option --- */ -/*! -Create a prepared statement object from a query. +/* ============================================================================ + * MODULE: connection + * ============================================================================ */ -Note that after calling `duckdb_prepare`, the prepared statement should always be destroyed using -`duckdb_destroy_prepare`, even if the prepare fails. +/* --- Enums for connection --- */ -If the prepare fails, `duckdb_prepare_error` can be called to obtain the reason why the prepare failed. +/* --- Struct forward declarations for connection --- */ -* @param connection The connection object -* @param query The SQL query to prepare -* @param out_prepared_statement The resulting prepared statement object -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_prepare(duckdb_connection connection, const char *query, - duckdb_prepared_statement *out_prepared_statement); +/* --- Types for connection --- */ -/*! -Closes the prepared statement and de-allocates all memory allocated for the statement. +/* --- Constants for connection --- */ -* @param prepared_statement The prepared statement to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_prepare(duckdb_prepared_statement *prepared_statement); +/* --- Function pointer typedefs for connection --- */ + +/* --- Functions for connection --- */ /*! -Returns the error message associated with the given prepared statement. -If the prepared statement has no error message, this returns `nullptr` instead. + * Opens a connection to a database. Connections are required to query the database, and store transactional state + * associated with the connection. The instantiated connection should be closed using 'duckdb_disconnect'. + * + * history: + * - stable: v0.1.0 + * + * @param database The database file to connect to. + * @param out_connection The result connection object. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_connect(duckdb_database database, duckdb_connection *out_connection); -The error message should not be freed. It will be de-allocated when `duckdb_destroy_prepare` is called. +#if DUCKDB_API_VERSION_AT_LEAST(0, 9, 0) +/*! + * Interrupt running query + * + * history: + * - stable: v0.9.0 + * + * @param connection The connection to interrupt + * @return void + */ +DUCKDB_C_API void duckdb_interrupt(duckdb_connection connection); +#endif -* @param prepared_statement The prepared statement to obtain the error from. -* @return The error message, or `nullptr` if there is none. -*/ -DUCKDB_C_API const char *duckdb_prepare_error(duckdb_prepared_statement prepared_statement); +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) +/*! + * Get the progress of the running query. + * + * history: + * - stable: v0.10.0 + * + * @param connection The connection running the query. + * @return duckdb_query_progress_type + */ +DUCKDB_C_API duckdb_query_progress_type duckdb_query_progress(duckdb_connection connection); +#endif /*! -Returns the number of parameters that can be provided to the given prepared statement. + * Closes the specified connection and de-allocates all memory allocated for that connection. + * + * history: + * - stable: v0.1.0 + * + * @param connection The connection to close. + * @return void + */ +DUCKDB_C_API void duckdb_disconnect(duckdb_connection *connection); -Returns 0 if the query was not successfully prepared. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the client context of the connection. + * + * history: + * - unstable: v1.3.0 + * - stable: v1.5.6 + * + * @param connection The connection. + * @param out_context The client context of the connection. Must be destroyed with `duckdb_destroy_client_context`. + * @return void + */ +DUCKDB_C_API void duckdb_connection_get_client_context(duckdb_connection connection, + duckdb_client_context *out_context); +#endif -* @param prepared_statement The prepared statement to obtain the number of parameters for. -*/ -DUCKDB_C_API idx_t duckdb_nparams(duckdb_prepared_statement prepared_statement); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the arrow options of the connection. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param connection The connection. + * @param out_arrow_options + * @return void + */ +DUCKDB_C_API void duckdb_connection_get_arrow_options(duckdb_connection connection, + duckdb_arrow_options *out_arrow_options); +#endif -/*! -Returns the name used to identify the parameter -The returned string should be freed using `duckdb_free`. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Returns the connection id of the client context. + * + * history: + * - unstable: v1.3.0 + * - stable: v1.5.6 + * + * @param context The client context. + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_client_context_get_connection_id(duckdb_client_context context); +#endif -Returns NULL if the index is out of range for the provided prepared statement. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the client context and deallocates its memory. + * + * history: + * - unstable: v1.3.0 + * - stable: v1.5.6 + * + * @param context The client context to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_client_context(duckdb_client_context *context); +#endif -* @param prepared_statement The prepared statement for which to get the parameter name from. -*/ -DUCKDB_C_API const char *duckdb_parameter_name(duckdb_prepared_statement prepared_statement, idx_t index); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the arrow options and deallocates its memory. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param arrow_options The arrow options to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_arrow_options(duckdb_arrow_options *arrow_options); +#endif -/*! -Returns the parameter type for the parameter at the given index. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Get the list of (fully qualified) table names of the query. + * + * history: + * - unstable: v1.3.0 + * - stable: v1.5.6 + * + * @param connection The connection for which to get the table names. + * @param query The query for which to get the table names. + * @param qualified Returns fully qualified table names (catalog.schema.table), if set to true, else only the (not + * escaped) table names. + * @return duckdb_value + */ +DUCKDB_C_API duckdb_value duckdb_get_table_names(duckdb_connection connection, const char *query, bool qualified); +#endif -Returns `DUCKDB_TYPE_INVALID` if the parameter index is out of range or the statement was not successfully prepared. +/* --- Struct definitions for connection --- */ -* @param prepared_statement The prepared statement. -* @param param_idx The parameter index. -* @return The parameter type -*/ -DUCKDB_C_API duckdb_type duckdb_param_type(duckdb_prepared_statement prepared_statement, idx_t param_idx); +/* ============================================================================ + * MODULE: copy_function + * ============================================================================ */ -/*! -Returns the logical type for the parameter at the given index. +/* --- Enums for copy_function --- */ -Returns `nullptr` if the parameter index is out of range or the statement was not successfully prepared. +/* --- Struct forward declarations for copy_function --- */ -The return type of this call should be destroyed with `duckdb_destroy_logical_type`. +/* --- Types for copy_function --- */ -* @param prepared_statement The prepared statement. -* @param param_idx The parameter index. -* @return The logical type of the parameter -*/ -DUCKDB_C_API duckdb_logical_type duckdb_param_logical_type(duckdb_prepared_statement prepared_statement, - idx_t param_idx); +/* --- Constants for copy_function --- */ -/*! -Clear the params bind to the prepared statement. -*/ -DUCKDB_C_API duckdb_state duckdb_clear_bindings(duckdb_prepared_statement prepared_statement); +/* --- Function pointer typedefs for copy_function --- */ +/* --- Functions for copy_function --- */ + +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) /*! -Returns the statement type of the statement to be executed + * Creates a new empty copy function. + * + * The return value must be destroyed with `duckdb_destroy_copy_function`. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @return duckdb_copy_function + */ +DUCKDB_C_API duckdb_copy_function duckdb_create_copy_function(void); +#endif -* @param statement The prepared statement. -* @return duckdb_statement_type value or DUCKDB_STATEMENT_TYPE_INVALID -*/ -DUCKDB_C_API duckdb_statement_type duckdb_prepared_statement_type(duckdb_prepared_statement statement); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the name of the copy function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param copy_function The copy function + * @param name The name to set + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_set_name(duckdb_copy_function copy_function, const char *name); +#endif -/*! -Returns the number of columns present in a the result of the prepared statement. If any of the column types are invalid, -the result will be 1. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the extra info pointer of the copy function, which can be used to store arbitrary data. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param copy_function The copy function + * @param extra_info The extra info pointer + * @param destructor A destructor function to call to destroy the extra info + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_set_extra_info(duckdb_copy_function copy_function, void *extra_info, + duckdb_delete_callback_t destructor); +#endif -* @param prepared_statement The prepared statement. -* @return The number of columns present in the result of the prepared statement. -*/ -DUCKDB_C_API idx_t duckdb_prepared_statement_column_count(duckdb_prepared_statement prepared_statement); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Registers the given copy function on the database connection under the specified name. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param connection The database connection + * @param copy_function The copy function to register + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_register_copy_function(duckdb_connection connection, + duckdb_copy_function copy_function); +#endif -/*! -Returns the name of the specified column of the result of the prepared_statement. -The returned string should be freed using `duckdb_free`. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the given copy function object. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param copy_function The copy function to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_copy_function(duckdb_copy_function *copy_function); +#endif -Returns `nullptr` if the column is out of range. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the bind function of the copy function, to use when binding `COPY ... TO`. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param copy_function + * @param bind The bind function + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_set_bind(duckdb_copy_function copy_function, duckdb_copy_function_bind_t bind); +#endif -* @param prepared_statement The prepared statement. -* @param col_idx The column index. -* @return The column name of the specified column. -*/ -DUCKDB_C_API const char *duckdb_prepared_statement_column_name(duckdb_prepared_statement prepared_statement, - idx_t col_idx); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Report that an error occurred during the binding-phase of a `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The bind info provided to the bind function + * @param error The error message + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_bind_set_error(duckdb_copy_function_bind_info info, const char *error); +#endif -/*! -Returns the column type of the specified column of the result of the prepared_statement. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the extra info pointer of the copy function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The bind info provided to the bind function + * @return void* + */ +DUCKDB_C_API void *duckdb_copy_function_bind_get_extra_info(duckdb_copy_function_bind_info info); +#endif -Returns `DUCKDB_TYPE_INVALID` if the column is out of range. -The return type of this call should be destroyed with `duckdb_destroy_logical_type`. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the client context of the current connection binding the `COPY ... TO` function. + * + * Must be destroyed with `duckdb_destroy_client_context` + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The bind info provided to the bind function + * @return duckdb_client_context + */ +DUCKDB_C_API duckdb_client_context duckdb_copy_function_bind_get_client_context(duckdb_copy_function_bind_info info); +#endif -* @param prepared_statement The prepared statement to fetch the column type from. -* @param col_idx The column index. -* @return The logical type of the specified column. -*/ -DUCKDB_C_API duckdb_logical_type -duckdb_prepared_statement_column_logical_type(duckdb_prepared_statement prepared_statement, idx_t col_idx); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the number of columns that will be provided to the `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The bind info provided to the bind function + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_copy_function_bind_get_column_count(duckdb_copy_function_bind_info info); +#endif -/*! -Returns the column type of the specified column of the result of the prepared_statement. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the type of a column that will be provided to the `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The bind info provided to the bind function + * @param col_idx The index of the column to retrieve the type for + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_copy_function_bind_get_column_type(duckdb_copy_function_bind_info info, + idx_t col_idx); +#endif -Returns `DUCKDB_TYPE_INVALID` if the column is out of range. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves all values for the given options provided to the `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The bind info provided to the bind function + * @return duckdb_value + */ +DUCKDB_C_API duckdb_value duckdb_copy_function_bind_get_options(duckdb_copy_function_bind_info info); +#endif -* @param prepared_statement The prepared statement to fetch the column type from. -* @param col_idx The column index. -* @return The type of the specified column. -*/ -DUCKDB_C_API duckdb_type duckdb_prepared_statement_column_type(duckdb_prepared_statement prepared_statement, - idx_t col_idx); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the bind data of the copy function, to be provided to the init, sink and finalize functions. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The bind info provided to the bind function + * @param bind_data The bind data pointer + * @param destructor A destructor function to call to destroy the bind data + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_bind_set_bind_data(duckdb_copy_function_bind_info info, void *bind_data, + duckdb_delete_callback_t destructor); +#endif -//---------------------------------------------------------------------------------------------------------------------- -// Bind Values to Prepared Statements -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to bind values to prepared statements. Try to use `duckdb_bind_value` and the `duckdb_create_...` interface -// for all types. -//---------------------------------------------------------------------------------------------------------------------- +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the initialization function of the copy function, called right before executing `COPY ... TO`. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param copy_function + * @param init The init function + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_set_global_init(duckdb_copy_function copy_function, + duckdb_copy_function_global_init_t init); +#endif -/*! -Binds a value to the prepared statement at the specified index. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Report that an error occurred during the initialization-phase of a `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The init info provided to the init function + * @param error The error message + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_global_init_set_error(duckdb_copy_function_global_init_info info, + const char *error); +#endif -Supersedes all type-specific bind functions (e.g., `duckdb_bind_varchar`, `duckdb_bind_int64`, etc.). -*/ -DUCKDB_C_API duckdb_state duckdb_bind_value(duckdb_prepared_statement prepared_statement, idx_t param_idx, - duckdb_value val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the extra info pointer of the copy function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The init info provided to the init function + * @return void* + */ +DUCKDB_C_API void *duckdb_copy_function_global_init_get_extra_info(duckdb_copy_function_global_init_info info); +#endif -/*! -Retrieve the index of the parameter for the prepared statement, identified by name -*/ -DUCKDB_C_API duckdb_state duckdb_bind_parameter_index(duckdb_prepared_statement prepared_statement, - idx_t *param_idx_out, const char *name); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the client context of the current connection initializing the `COPY ... TO` function. + * + * Must be destroyed with `duckdb_destroy_client_context` + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The init info provided to the init function + * @return duckdb_client_context + */ +DUCKDB_C_API duckdb_client_context +duckdb_copy_function_global_init_get_client_context(duckdb_copy_function_global_init_info info); +#endif -/*! -Binds a bool value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_boolean(duckdb_prepared_statement prepared_statement, idx_t param_idx, bool val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the bind data provided during the binding-phase of a `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The init info provided to the init function + * @return void* + */ +DUCKDB_C_API void *duckdb_copy_function_global_init_get_bind_data(duckdb_copy_function_global_init_info info); +#endif -/*! -Binds an int8_t value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_int8(duckdb_prepared_statement prepared_statement, idx_t param_idx, int8_t val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the file path provided to the `COPY ... TO` function. + * + * Lives for the duration of the initialization callback, must not be destroyed. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The init info provided to the init function + * @return const char* + */ +DUCKDB_C_API const char *duckdb_copy_function_global_init_get_file_path(duckdb_copy_function_global_init_info info); +#endif -/*! -Binds an int16_t value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_int16(duckdb_prepared_statement prepared_statement, idx_t param_idx, int16_t val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the global state of the copy function, to be provided to all subsequent local init, sink and finalize functions. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The init info provided to the init function + * @param global_state The global state pointer + * @param destructor A destructor function to call to destroy the global state + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_global_init_set_global_state(duckdb_copy_function_global_init_info info, + void *global_state, + duckdb_delete_callback_t destructor); +#endif -/*! -Binds an int32_t value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_int32(duckdb_prepared_statement prepared_statement, idx_t param_idx, int32_t val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the sink function of the copy function, called during `COPY ... TO`. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param copy_function + * @param function The sink function + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_set_sink(duckdb_copy_function copy_function, + duckdb_copy_function_sink_t function); +#endif -/*! -Binds an int64_t value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_int64(duckdb_prepared_statement prepared_statement, idx_t param_idx, int64_t val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Report that an error occurred during the sink-phase of a `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The sink info provided to the sink function + * @param error The error message + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_sink_set_error(duckdb_copy_function_sink_info info, const char *error); +#endif -/*! -Binds a duckdb_hugeint value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_hugeint(duckdb_prepared_statement prepared_statement, idx_t param_idx, - duckdb_hugeint val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the extra info pointer of the copy function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The sink info provided to the sink function + * @return void* + */ +DUCKDB_C_API void *duckdb_copy_function_sink_get_extra_info(duckdb_copy_function_sink_info info); +#endif -/*! -Binds a duckdb_uhugeint value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_uhugeint(duckdb_prepared_statement prepared_statement, idx_t param_idx, - duckdb_uhugeint val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the client context of the current connection during the sink-phase of the `COPY ... TO` function. + * + * Must be destroyed with `duckdb_destroy_client_context` + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The sink info provided to the sink function + * @return duckdb_client_context + */ +DUCKDB_C_API duckdb_client_context duckdb_copy_function_sink_get_client_context(duckdb_copy_function_sink_info info); +#endif -/*! -Binds a duckdb_decimal value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_decimal(duckdb_prepared_statement prepared_statement, idx_t param_idx, - duckdb_decimal val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the bind data provided during the binding-phase of a `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The sink info provided to the sink function + * @return void* + */ +DUCKDB_C_API void *duckdb_copy_function_sink_get_bind_data(duckdb_copy_function_sink_info info); +#endif -/*! -Binds a uint8_t value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_uint8(duckdb_prepared_statement prepared_statement, idx_t param_idx, uint8_t val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the global state provided during the init-phase of a `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The sink info provided to the sink function + * @return void* + */ +DUCKDB_C_API void *duckdb_copy_function_sink_get_global_state(duckdb_copy_function_sink_info info); +#endif -/*! -Binds a uint16_t value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_uint16(duckdb_prepared_statement prepared_statement, idx_t param_idx, - uint16_t val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the finalize function of the copy function, called at the end of `COPY ... TO`. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param copy_function + * @param finalize The finalize function + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_set_finalize(duckdb_copy_function copy_function, + duckdb_copy_function_finalize_t finalize); +#endif -/*! -Binds a uint32_t value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_uint32(duckdb_prepared_statement prepared_statement, idx_t param_idx, - uint32_t val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Report that an error occurred during the finalize-phase of a `COPY ... TO` function + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The finalize info provided to the finalize function + * @param error The error message + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_finalize_set_error(duckdb_copy_function_finalize_info info, const char *error); +#endif -/*! -Binds a uint64_t value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_uint64(duckdb_prepared_statement prepared_statement, idx_t param_idx, - uint64_t val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the extra info pointer of the copy function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The finalize info provided to the finalize function + * @return void* + */ +DUCKDB_C_API void *duckdb_copy_function_finalize_get_extra_info(duckdb_copy_function_finalize_info info); +#endif -/*! -Binds a float value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_float(duckdb_prepared_statement prepared_statement, idx_t param_idx, float val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the client context of the current connection during the finalize-phase of the `COPY ... TO` function. + * + * Must be destroyed with `duckdb_destroy_client_context` + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The finalize info provided to the finalize function + * @return duckdb_client_context + */ +DUCKDB_C_API duckdb_client_context +duckdb_copy_function_finalize_get_client_context(duckdb_copy_function_finalize_info info); +#endif -/*! -Binds a double value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_double(duckdb_prepared_statement prepared_statement, idx_t param_idx, double val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the bind data provided during the binding-phase of a `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The finalize info provided to the finalize function + * @return void* + */ +DUCKDB_C_API void *duckdb_copy_function_finalize_get_bind_data(duckdb_copy_function_finalize_info info); +#endif -/*! -Binds a duckdb_date value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_date(duckdb_prepared_statement prepared_statement, idx_t param_idx, - duckdb_date val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the global state provided during the init-phase of a `COPY ... TO` function. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param info The finalize info provided to the finalize function + * @return void* + */ +DUCKDB_C_API void *duckdb_copy_function_finalize_get_global_state(duckdb_copy_function_finalize_info info); +#endif -/*! -Binds a duckdb_time value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_time(duckdb_prepared_statement prepared_statement, idx_t param_idx, - duckdb_time val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the table function to use when executing a `COPY ... FROM (...)` statement with this copy function. + * + * The table function must have a `duckdb_table_function_bind_t`, `duckdb_table_function_init_t` and + * `duckdb_table_function_t` set. + * + * The table function must take a single VARCHAR parameter (the file path). + * + * Options passed to the `COPY ... FROM (...)` statement are forwarded as named parameters to the table function. + * + * Since `COPY ... FROM` copies into an already existing table, the table function should not define its own result + * columns using `duckdb_bind_add_result_column` when binding . Instead use + * `duckdb_table_function_bind_get_result_column_count` and related functions in the bind callback of the table function + * to retrieve the schema of the target table of the `COPY ... FROM` statement. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param copy_function The copy function + * @param table_function The table function to use for `COPY ... FROM` + * @return void + */ +DUCKDB_C_API void duckdb_copy_function_set_copy_from_function(duckdb_copy_function copy_function, + duckdb_table_function table_function); +#endif -/*! -Binds a duckdb_timestamp value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_timestamp(duckdb_prepared_statement prepared_statement, idx_t param_idx, - duckdb_timestamp val); +/* --- Struct definitions for copy_function --- */ -/*! -Binds a duckdb_timestamp value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_timestamp_tz(duckdb_prepared_statement prepared_statement, idx_t param_idx, - duckdb_timestamp val); +/* ============================================================================ + * MODULE: data_chunk + * ============================================================================ */ -/*! -Binds a duckdb_interval value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_interval(duckdb_prepared_statement prepared_statement, idx_t param_idx, - duckdb_interval val); +/* --- Enums for data_chunk --- */ -/*! -Binds a null-terminated varchar value to the prepared statement at the specified index. +/* --- Struct forward declarations for data_chunk --- */ -Superseded by `duckdb_bind_value`. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_varchar(duckdb_prepared_statement prepared_statement, idx_t param_idx, - const char *val); +/* --- Types for data_chunk --- */ -/*! -Binds a varchar value to the prepared statement at the specified index. +/* --- Constants for data_chunk --- */ -Superseded by `duckdb_bind_value`. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_varchar_length(duckdb_prepared_statement prepared_statement, idx_t param_idx, - const char *val, idx_t length); +/* --- Function pointer typedefs for data_chunk --- */ -/*! -Binds a blob value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_blob(duckdb_prepared_statement prepared_statement, idx_t param_idx, - const void *data, idx_t length); +/* --- Functions for data_chunk --- */ +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Binds a NULL value to the prepared statement at the specified index. -*/ -DUCKDB_C_API duckdb_state duckdb_bind_null(duckdb_prepared_statement prepared_statement, idx_t param_idx); - -//---------------------------------------------------------------------------------------------------------------------- -// Execute Prepared Statements -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to execute a prepared statement. -//---------------------------------------------------------------------------------------------------------------------- + * Creates an empty data chunk with the specified column types. The result must be destroyed with + * `duckdb_destroy_data_chunk`. + * + * history: + * - stable: v0.3.3 + * + * @param types An array of column types. Column types can not contain ANY and INVALID types. + * @param column_count The number of columns. + * @return duckdb_data_chunk + */ +DUCKDB_C_API duckdb_data_chunk duckdb_create_data_chunk(duckdb_logical_type *types, idx_t column_count); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Executes the prepared statement with the given bound parameters, and returns a materialized query result. + * Destroys the data chunk and de-allocates all memory allocated for that chunk. + * + * history: + * - stable: v0.3.3 + * + * @param chunk The data chunk to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_data_chunk(duckdb_data_chunk *chunk); +#endif -This method can be called multiple times for each prepared statement, and the parameters can be modified -between calls to this function. +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Resets a data chunk, clearing the validity masks and setting the cardinality of the data chunk to 0. After calling + * this method, you must call `duckdb_vector_get_validity` and `duckdb_vector_get_data` to obtain current data and + * validity pointers + * + * history: + * - stable: v0.3.3 + * + * @param chunk The data chunk to reset. + * @return void + */ +DUCKDB_C_API void duckdb_data_chunk_reset(duckdb_data_chunk chunk); +#endif -Note that the result must be freed with `duckdb_destroy_result`. +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Retrieves the number of columns in a data chunk. + * + * history: + * - stable: v0.3.3 + * + * @param chunk The data chunk to get the data from + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_data_chunk_get_column_count(duckdb_data_chunk chunk); +#endif -* @param prepared_statement The prepared statement to execute. -* @param out_result The query result. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_execute_prepared(duckdb_prepared_statement prepared_statement, - duckdb_result *out_result); +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Retrieves the vector at the specified column index in the data chunk. + * + * The pointer to the vector is valid for as long as the chunk is alive. It does NOT need to be destroyed. + * + * history: + * - stable: v0.3.3 + * + * @param chunk The data chunk to get the data from + * @param col_idx + * @return duckdb_vector + */ +DUCKDB_C_API duckdb_vector duckdb_data_chunk_get_vector(duckdb_data_chunk chunk, idx_t col_idx); +#endif -#ifndef DUCKDB_API_NO_DEPRECATED +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. + * Retrieves the current number of tuples in a data chunk. + * + * history: + * - stable: v0.3.3 + * + * @param chunk The data chunk to get the data from + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_data_chunk_get_size(duckdb_data_chunk chunk); +#endif -Executes the prepared statement with the given bound parameters, and returns an optionally-streaming query result. -To determine if the resulting query was in fact streamed, use `duckdb_result_is_streaming` +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Sets the current number of tuples in a data chunk. + * + * history: + * - stable: v0.3.3 + * + * @param chunk The data chunk to set the size in + * @param size The number of tuples in the data chunk + * @return void + */ +DUCKDB_C_API void duckdb_data_chunk_set_size(duckdb_data_chunk chunk, idx_t size); +#endif -This method can be called multiple times for each prepared statement, and the parameters can be modified -between calls to this function. +/* --- Struct definitions for data_chunk --- */ -Note that the result must be freed with `duckdb_destroy_result`. +/* ============================================================================ + * MODULE: database + * ============================================================================ */ -* @param prepared_statement The prepared statement to execute. -* @param out_result The query result. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_execute_prepared_streaming(duckdb_prepared_statement prepared_statement, - duckdb_result *out_result); +/* --- Enums for database --- */ -#endif +/* --- Struct forward declarations for database --- */ -//---------------------------------------------------------------------------------------------------------------------- -// Extract Statements -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// A query string can be extracted into multiple SQL statements. Each statement should be prepared and executed -// separately. -//---------------------------------------------------------------------------------------------------------------------- +/* --- Types for database --- */ -/*! -Extract all statements from a query. -Note that after calling `duckdb_extract_statements`, the extracted statements should always be destroyed using -`duckdb_destroy_extracted`, even if no statements were extracted. +/* --- Constants for database --- */ -If the extract fails, `duckdb_extract_statements_error` can be called to obtain the reason why the extract failed. +/* --- Function pointer typedefs for database --- */ -* @param connection The connection object -* @param query The SQL query to extract -* @param out_extracted_statements The resulting extracted statements object -* @return The number of extracted statements or 0 on failure. -*/ -DUCKDB_C_API idx_t duckdb_extract_statements(duckdb_connection connection, const char *query, - duckdb_extracted_statements *out_extracted_statements); +/* --- Functions for database --- */ +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) /*! -Prepare an extracted statement. -Note that after calling `duckdb_prepare_extracted_statement`, the prepared statement should always be destroyed using -`duckdb_destroy_prepare`, even if the prepare fails. + * Creates a new database instance cache. The instance cache is necessary if a client/program (re)opens multiple + * databases to the same file within the same process. Must be destroyed with 'duckdb_destroy_instance_cache'. + * + * history: + * - unstable: v1.2.0 + * - stable: v1.5.6 + * + * @return duckdb_instance_cache + */ +DUCKDB_C_API duckdb_instance_cache duckdb_create_instance_cache(void); +#endif -If the prepare fails, `duckdb_prepare_error` can be called to obtain the reason why the prepare failed. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Creates a new database instance in the instance cache, or retrieves an existing database instance. Must be closed + * with 'duckdb_close'. + * + * history: + * - unstable: v1.2.0 + * - stable: v1.5.6 + * + * @param instance_cache The instance cache in which to create the database, or from which to take the database. + * @param path Path to the database file on disk. Both `nullptr` and `:memory:` open or retrieve an in-memory database. + * @param out_database The resulting cached database. + * @param config (Optional) configuration used to create the database. + * @param out_error If set and the function returns `DuckDBError`, this contains the error message. Note that the error + * message must be freed using `duckdb_free`. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_get_or_create_from_cache(duckdb_instance_cache instance_cache, const char *path, + duckdb_database *out_database, duckdb_config config, + char **out_error); +#endif -* @param connection The connection object -* @param extracted_statements The extracted statements object -* @param index The index of the extracted statement to prepare -* @param out_prepared_statement The resulting prepared statement object -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_prepare_extracted_statement(duckdb_connection connection, - duckdb_extracted_statements extracted_statements, - idx_t index, - duckdb_prepared_statement *out_prepared_statement); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys an existing database instance cache and de-allocates its memory. + * + * history: + * - unstable: v1.2.0 + * - stable: v1.5.6 + * + * @param instance_cache The instance cache to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_instance_cache(duckdb_instance_cache *instance_cache); +#endif /*! -Returns the error message contained within the extracted statements. -The result of this function must not be freed. It will be cleaned up when `duckdb_destroy_extracted` is called. + * Creates a new database or opens an existing database file stored at the given path. If no path is given a new + * in-memory database is created instead. The database must be closed with 'duckdb_close'. + * + * history: + * - stable: v0.1.0 + * + * @param path Path to the database file on disk. Both `nullptr` and `:memory:` open an in-memory database. + * @param out_database The result database object. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_open(const char *path, duckdb_database *out_database); -* @param extracted_statements The extracted statements to fetch the error from. -* @return The error of the extracted statements. -*/ -DUCKDB_C_API const char *duckdb_extract_statements_error(duckdb_extracted_statements extracted_statements); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) +/*! + * Extended version of duckdb_open. Creates a new database or opens an existing database file stored at the given path. + * The database must be closed with 'duckdb_close'. + * + * history: + * - stable: v0.2.8 + * + * @param path Path to the database file on disk. Both `nullptr` and `:memory:` open an in-memory database. + * @param out_database The result database object. + * @param config (Optional) configuration used to start up the database. + * @param out_error If set and the function returns `DuckDBError`, this contains the error message. Note that the error + * message must be freed using `duckdb_free`. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_open_ext(const char *path, duckdb_database *out_database, duckdb_config config, + char **out_error); +#endif /*! -De-allocates all memory allocated for the extracted statements. -* @param extracted_statements The extracted statements to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_extracted(duckdb_extracted_statements *extracted_statements); + * Closes the specified database and de-allocates all memory allocated for that database. This should be called after + * you are done with any database allocated through `duckdb_open` or `duckdb_open_ext`. Note that failing to call + * `duckdb_close` (in case of e.g. a program crash) will not cause data corruption. Still, it is recommended to always + * correctly close a database object after you are done with it. + * + * history: + * - stable: v0.1.0 + * + * @param database The database object to shut down. + * @return void + */ +DUCKDB_C_API void duckdb_close(duckdb_database *database); -//---------------------------------------------------------------------------------------------------------------------- -// Pending Result Interface -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to interact with a pending result. First, prepare a pending result, and then execute it. -//---------------------------------------------------------------------------------------------------------------------- +#if DUCKDB_API_VERSION_AT_LEAST(0, 6, 0) +/*! + * Returns the version of the linked DuckDB, with a version postfix for dev versions + * + * Usually used for developing C extensions that must return this for a compatibility check. + * + * history: + * - stable: v0.6.0 + * + * @return const char* + */ +DUCKDB_C_API const char *duckdb_library_version(void); +#endif -/*! -Executes the prepared statement with the given bound parameters, and returns a pending result. -The pending result represents an intermediate structure for a query that is not yet fully executed. -The pending result can be used to incrementally execute a query, returning control to the client between tasks. - -Note that after calling `duckdb_pending_prepared`, the pending result should always be destroyed using -`duckdb_destroy_pending`, even if this function returns DuckDBError. - -* @param prepared_statement The prepared statement to execute. -* @param out_result The pending query result. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_pending_prepared(duckdb_prepared_statement prepared_statement, - duckdb_pending_result *out_result); - -#ifndef DUCKDB_API_NO_DEPRECATED -/*! -**DEPRECATION NOTICE**: This method is scheduled for removal in a future release. - -Executes the prepared statement with the given bound parameters, and returns a pending result. -This pending result will create a streaming duckdb_result when executed. -The pending result represents an intermediate structure for a query that is not yet fully executed. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) +/*! + * Initializes an empty configuration object that can be used to provide start-up options for the DuckDB instance + * through `duckdb_open_ext`. The duckdb_config must be destroyed using 'duckdb_destroy_config' + * + * This will always succeed unless there is a malloc failure. + * + * Note that `duckdb_destroy_config` should always be called on the resulting config, even if the function returns + * `DuckDBError`. + * + * history: + * - stable: v0.2.8 + * + * @param out_config The result configuration object. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_create_config(duckdb_config *out_config); +#endif -Note that after calling `duckdb_pending_prepared_streaming`, the pending result should always be destroyed using -`duckdb_destroy_pending`, even if this function returns DuckDBError. +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) +/*! + * This returns the total amount of configuration options available for usage with `duckdb_get_config_flag`. + * + * This should not be called in a loop as it internally loops over all the options. + * + * history: + * - stable: v0.2.8 + * + * @return size_t + */ +DUCKDB_C_API size_t duckdb_config_count(void); +#endif -* @param prepared_statement The prepared statement to execute. -* @param out_result The pending query result. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_pending_prepared_streaming(duckdb_prepared_statement prepared_statement, - duckdb_pending_result *out_result); +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) +/*! + * Obtains a human-readable name and description of a specific configuration option. This can be used to e.g. display + * configuration options. This will succeed unless `index` is out of range (i.e. `>= duckdb_config_count`). + * + * The result name or description MUST NOT be freed. + * + * history: + * - stable: v0.2.8 + * + * @param index The index of the configuration option (between 0 and `duckdb_config_count`) + * @param out_name A name of the configuration flag. + * @param out_description A description of the configuration flag. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_get_config_flag(size_t index, const char **out_name, const char **out_description); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) +/*! + * Sets the specified option for the specified configuration. The configuration option is indicated by name. To obtain a + * list of config options, see `duckdb_get_config_flag`. + * + * In the source code, configuration options are defined in `config.cpp`. + * + * This can fail if either the name is invalid, or if the value provided for the option is invalid. + * + * history: + * - stable: v0.2.8 + * + * @param config The configuration object to set the option on. + * @param name The name of the configuration flag to set. + * @param option The value to set the configuration flag to. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_set_config(duckdb_config config, const char *name, const char *option); #endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 2, 8) /*! -Closes the pending result and de-allocates all memory allocated for the result. + * Destroys the specified configuration object and de-allocates all memory allocated for the object. + * + * history: + * - stable: v0.2.8 + * + * @param config The configuration object to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_config(duckdb_config *config); +#endif -* @param pending_result The pending result to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_pending(duckdb_pending_result *pending_result); +/* --- Struct definitions for database --- */ -/*! -Returns the error message contained within the pending result. +/* ============================================================================ + * MODULE: expression + * ============================================================================ */ -The result of this function must not be freed. It will be cleaned up when `duckdb_destroy_pending` is called. +/* --- Enums for expression --- */ -* @param pending_result The pending result to fetch the error from. -* @return The error of the pending result. -*/ -DUCKDB_C_API const char *duckdb_pending_error(duckdb_pending_result pending_result); +/* --- Struct forward declarations for expression --- */ -/*! -Executes a single task within the query, returning whether or not the query is ready. +/* --- Types for expression --- */ -If this returns DUCKDB_PENDING_RESULT_READY, the duckdb_execute_pending function can be called to obtain the result. -If this returns DUCKDB_PENDING_RESULT_NOT_READY, the duckdb_pending_execute_task function should be called again. -If this returns DUCKDB_PENDING_ERROR, an error occurred during execution. +/* --- Constants for expression --- */ -The error message can be obtained by calling duckdb_pending_error on the pending_result. +/* --- Function pointer typedefs for expression --- */ -* @param pending_result The pending result to execute a task within. -* @return The state of the pending result after the execution. -*/ -DUCKDB_C_API duckdb_pending_state duckdb_pending_execute_task(duckdb_pending_result pending_result); +/* --- Functions for expression --- */ +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) /*! -If this returns DUCKDB_PENDING_RESULT_READY, the duckdb_execute_pending function can be called to obtain the result. -If this returns DUCKDB_PENDING_RESULT_NOT_READY, the duckdb_pending_execute_check_state function should be called again. -If this returns DUCKDB_PENDING_ERROR, an error occurred during execution. + * Destroys the expression and de-allocates its memory. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param expr A pointer to the expression. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_expression(duckdb_expression *expr); +#endif -The error message can be obtained by calling duckdb_pending_error on the pending_result. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Returns the return type of an expression. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param expr The expression. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_expression_return_type(duckdb_expression expr); +#endif -* @param pending_result The pending result. -* @return The state of the pending result. -*/ -DUCKDB_C_API duckdb_pending_state duckdb_pending_execute_check_state(duckdb_pending_result pending_result); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Returns whether the expression is foldable into a value or not. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param expr The expression. + * @return bool + */ +DUCKDB_C_API bool duckdb_expression_is_foldable(duckdb_expression expr); +#endif -/*! -Fully execute a pending query result, returning the final query result. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Folds an expression creating a folded value. + * + * history: + * - unstable: v1.4.0 + * - stable: v1.5.6 + * + * @param context The client context. + * @param expr The expression. Must be foldable. + * @param out_value The folded value, if folding was successful. Must be destroyed with `duckdb_destroy_value`. + * @return duckdb_error_data + */ +DUCKDB_C_API duckdb_error_data duckdb_expression_fold(duckdb_client_context context, duckdb_expression expr, + duckdb_value *out_value); +#endif -If duckdb_pending_execute_task has been called until DUCKDB_PENDING_RESULT_READY was returned, this will return fast. -Otherwise, all remaining tasks must be executed first. +/* --- Struct definitions for expression --- */ -Note that the result must be freed with `duckdb_destroy_result`. +/* ============================================================================ + * MODULE: file_system + * ============================================================================ */ -* @param pending_result The pending result to execute. -* @param out_result The result object. -* @return `DuckDBSuccess` on success or `DuckDBError` on failure. -*/ -DUCKDB_C_API duckdb_state duckdb_execute_pending(duckdb_pending_result pending_result, duckdb_result *out_result); +/* --- Enums for file_system --- */ -/*! -Returns whether a duckdb_pending_state is finished executing. For example if `pending_state` is -DUCKDB_PENDING_RESULT_READY, this function will return true. +/* --- Struct forward declarations for file_system --- */ -* @param pending_state The pending state on which to decide whether to finish execution. -* @return Boolean indicating pending execution should be considered finished. -*/ -DUCKDB_C_API bool duckdb_pending_execution_is_finished(duckdb_pending_state pending_state); +/* --- Types for file_system --- */ -//---------------------------------------------------------------------------------------------------------------------- -// Value Interface -//---------------------------------------------------------------------------------------------------------------------- -// DESCRIPTION: -// Functions to create a `duckdb_value` for each of DuckDB's supported data types, and to access the contents of a -// `duckdb_value`. The `duckdb_value` wrapper allows handling of primitive and arbitrarily (nested) types through the -// same interface. -//---------------------------------------------------------------------------------------------------------------------- +/* --- Constants for file_system --- */ -/*! -Destroys the value and de-allocates all memory allocated for that type. +/* --- Function pointer typedefs for file_system --- */ -* @param value The value to destroy. -*/ -DUCKDB_C_API void duckdb_destroy_value(duckdb_value *value); +/* --- Functions for file_system --- */ +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) /*! -Creates a value from a null-terminated string. Returns nullptr if the string is not valid UTF-8 or other invalid input. - -Superseded by `duckdb_create_varchar_length`. + * Get a file system instance associated with the given client context. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param context The client context. + * @return duckdb_file_system + */ +DUCKDB_C_API duckdb_file_system duckdb_client_context_get_file_system(duckdb_client_context context); +#endif -* @param text The null-terminated string -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_varchar(const char *text); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the given file system instance. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_system The file system instance to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_file_system(duckdb_file_system *file_system); +#endif -/*! -Creates a value from a string. Returns nullptr if the string is not valid UTF-8 or other invalid input. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the last error that occurred on the given file system instance. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_system The file system instance. + * @return duckdb_error_data + */ +DUCKDB_C_API duckdb_error_data duckdb_file_system_error_data(duckdb_file_system file_system); +#endif -* @param text The text -* @param length The length of the text -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_varchar_length(const char *text, idx_t length); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Opens a file at the given path with the specified options. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_system The file system instance. + * @param path The path to the file. + * @param options The file open options specifying how to open the file. + * @param out_file The resulting file handle instance, or `nullptr` if the open failed. Must be destroyed with + * `duckdb_destroy_file_handle`. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_file_system_open(duckdb_file_system file_system, const char *path, + duckdb_file_open_options options, duckdb_file_handle *out_file); +#endif -/*! -Creates a value from a boolean +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Creates a new file open options instance with blank settings. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @return duckdb_file_open_options + */ +DUCKDB_C_API duckdb_file_open_options duckdb_create_file_open_options(void); +#endif -* @param input The boolean value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_bool(bool input); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets a specific flag in the file open options. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param options The file open options instance. + * @param flag The flag to set (e.g., read, write). + * @param value If the flag is enabled or disabled. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_file_open_options_set_flag(duckdb_file_open_options options, duckdb_file_flag flag, + bool value); +#endif -/*! -Creates a value from an int8_t (a tinyint) +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the given file open options instance. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param options The file open options instance to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_file_open_options(duckdb_file_open_options *options); +#endif -* @param input The tinyint value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_int8(int8_t input); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys the given file handle and deallocates all associated resources. This will also close the file if it is still + * open. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_handle The file handle to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_file_handle(duckdb_file_handle *file_handle); +#endif -/*! -Creates a value from a uint8_t (a utinyint) +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Retrieves the last error that occurred on the given file handle. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_handle The file handle. + * @return duckdb_error_data + */ +DUCKDB_C_API duckdb_error_data duckdb_file_handle_error_data(duckdb_file_handle file_handle); +#endif -* @param input The utinyint value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_uint8(uint8_t input); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Reads data from the file into the buffer. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_handle The file handle to read from. + * @param buffer The buffer to read data into. + * @param size The number of bytes to read. + * @return int64_t + */ +DUCKDB_C_API int64_t duckdb_file_handle_read(duckdb_file_handle file_handle, void *buffer, int64_t size); +#endif -/*! -Creates a value from an int16_t (a smallint) +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Writes data from the buffer to the file. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_handle The file handle to write to. + * @param buffer The buffer containing data to write. + * @param size The number of bytes to write. + * @return int64_t + */ +DUCKDB_C_API int64_t duckdb_file_handle_write(duckdb_file_handle file_handle, const void *buffer, int64_t size); +#endif -* @param input The smallint value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_int16(int16_t input); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Tells the current position in the file. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_handle The file handle to tell the position of. + * @return int64_t + */ +DUCKDB_C_API int64_t duckdb_file_handle_tell(duckdb_file_handle file_handle); +#endif -/*! -Creates a value from a uint16_t (a usmallint) +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Gets the size of the file. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_handle The file handle to get the size of. + * @return int64_t + */ +DUCKDB_C_API int64_t duckdb_file_handle_size(duckdb_file_handle file_handle); +#endif -* @param input The usmallint value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_uint16(uint16_t input); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Seeks to a specific position in the file. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_handle The file handle to seek in. + * @param position + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_file_handle_seek(duckdb_file_handle file_handle, int64_t position); +#endif -/*! -Creates a value from an int32_t (an integer) +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Synchronizes the file's state with the underlying storage. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_handle The file handle to synchronize. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_file_handle_sync(duckdb_file_handle file_handle); +#endif -* @param input The integer value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_int32(int32_t input); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Closes the given file handle. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param file_handle The file handle to close. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_file_handle_close(duckdb_file_handle file_handle); +#endif -/*! -Creates a value from a uint32_t (a uinteger) +/* --- Struct definitions for file_system --- */ -* @param input The uinteger value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_uint32(uint32_t input); +/* ============================================================================ + * MODULE: log_storage + * ============================================================================ */ -/*! -Creates a value from a uint64_t (a ubigint) +/* --- Enums for log_storage --- */ -* @param input The ubigint value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_uint64(uint64_t input); +/* --- Struct forward declarations for log_storage --- */ -/*! -Creates a value from an int64 +/* --- Types for log_storage --- */ -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_int64(int64_t val); +/* --- Constants for log_storage --- */ -/*! -Creates a value from a hugeint +/* --- Function pointer typedefs for log_storage --- */ -* @param input The hugeint value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_hugeint(duckdb_hugeint input); +/* --- Functions for log_storage --- */ +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) /*! -Creates a value from a uhugeint + * Creates a new log storage object. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @return duckdb_log_storage + */ +DUCKDB_C_API duckdb_log_storage duckdb_create_log_storage(void); +#endif -* @param input The uhugeint value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_uhugeint(duckdb_uhugeint input); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Destroys a log storage object. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param log_storage The log storage object to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_log_storage(duckdb_log_storage *log_storage); +#endif -/*! -Creates a BIGNUM value from a duckdb_bignum +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the callback function for writing log entries. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param log_storage The log storage object. + * @param function The function to call. + * @return void + */ +DUCKDB_C_API void duckdb_log_storage_set_write_log_entry(duckdb_log_storage log_storage, + duckdb_logger_write_log_entry_t function); +#endif -* @param input The duckdb_bignum value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_bignum(duckdb_bignum input); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the extra data of the custom log storage. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param log_storage The log storage object. + * @param extra_data The extra data that is passed back into the callbacks. + * @param delete_callback + * @return void + */ +DUCKDB_C_API void duckdb_log_storage_set_extra_data(duckdb_log_storage log_storage, void *extra_data, + duckdb_delete_callback_t delete_callback); +#endif -/*! -Creates a DECIMAL value from a duckdb_decimal +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Sets the name of the log storage. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param log_storage The log storage object. + * @param name The name of the log storage. + * @return void + */ +DUCKDB_C_API void duckdb_log_storage_set_name(duckdb_log_storage log_storage, const char *name); +#endif -* @param input The duckdb_decimal value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_decimal(duckdb_decimal input); +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Registers a custom log storage for the logger. + * + * history: + * - unstable: v1.5.0 + * - stable: v1.5.6 + * + * @param database A database object. + * @param log_storage The log storage object. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_register_log_storage(duckdb_database database, duckdb_log_storage log_storage); +#endif -/*! -Creates a value from a float +/* --- Struct definitions for log_storage --- */ -* @param input The float value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_float(float input); +/* ============================================================================ + * MODULE: logical_type + * ============================================================================ */ -/*! -Creates a value from a double +/* --- Enums for logical_type --- */ -* @param input The double value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_double(double input); +/* --- Struct forward declarations for logical_type --- */ -/*! -Creates a value from a date +/* --- Types for logical_type --- */ -* @param input The date value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_date(duckdb_date input); +/* --- Constants for logical_type --- */ -/*! -Creates a value from a time +/* --- Function pointer typedefs for logical_type --- */ -* @param input The time value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_time(duckdb_time input); +/* --- Functions for logical_type --- */ +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Creates a value from a time_ns - -* @param input The time value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_time_ns(duckdb_time_ns input); + * Creates a `duckdb_logical_type` from a primitive type. The resulting logical type must be destroyed with + * `duckdb_destroy_logical_type`. + * + * Returns an invalid logical type, if type is: `DUCKDB_TYPE_INVALID`, `DUCKDB_TYPE_DECIMAL`, `DUCKDB_TYPE_ENUM`, + * `DUCKDB_TYPE_LIST`, `DUCKDB_TYPE_STRUCT`, `DUCKDB_TYPE_MAP`, `DUCKDB_TYPE_ARRAY`, or `DUCKDB_TYPE_UNION`. + * + * history: + * - stable: v0.3.3 + * + * @param type The primitive type to create. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_create_logical_type(duckdb_type type); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) /*! -Creates a value from a time_tz. -Not to be confused with `duckdb_create_time_tz`, which creates a duckdb_time_tz_t. + * Returns the alias of a duckdb_logical_type, if set, else `nullptr`. The result must be destroyed with `duckdb_free`. + * + * history: + * - stable: v0.10.0 + * + * @param type The logical type + * @return char* + */ +DUCKDB_C_API char *duckdb_logical_type_get_alias(duckdb_logical_type type); +#endif -* @param value The time_tz value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_time_tz_value(duckdb_time_tz value); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Sets the alias of a duckdb_logical_type. + * + * history: + * - stable: v1.1.0 + * + * @param type The logical type + * @param alias The alias to set + * @return void + */ +DUCKDB_C_API void duckdb_logical_type_set_alias(duckdb_logical_type type, const char *alias); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 4, 0) /*! -Creates a TIMESTAMP value from a duckdb_timestamp - -* @param input The duckdb_timestamp value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_timestamp(duckdb_timestamp input); + * Creates a LIST type from its child type. The return type must be destroyed with `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.4.0 + * + * @param type The child type of the list + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_create_list_type(duckdb_logical_type type); +#endif -/*! -Creates a TIMESTAMP_TZ value from a duckdb_timestamp +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 1) +/*! + * Creates an ARRAY type from its child type. The return type must be destroyed with `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.10.1 + * + * @param type The child type of the array. + * @param array_size The number of elements in the array. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_create_array_type(duckdb_logical_type type, idx_t array_size); +#endif -* @param input The duckdb_timestamp value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_timestamp_tz(duckdb_timestamp input); +#if DUCKDB_API_VERSION_AT_LEAST(0, 4, 0) +/*! + * Creates a MAP type from its key type and value type. The return type must be destroyed with + * `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.4.0 + * + * @param key_type The map's key type. + * @param value_type The map's value type. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_create_map_type(duckdb_logical_type key_type, duckdb_logical_type value_type); +#endif -/*! -Creates a TIMESTAMP_S value from a duckdb_timestamp_s +#if DUCKDB_API_VERSION_AT_LEAST(0, 7, 0) +/*! + * Creates a UNION type from the passed arrays. The return type must be destroyed with `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.7.0 + * + * @param member_types The array of union member types. + * @param member_names The union member names. + * @param member_count The number of union members. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_create_union_type(duckdb_logical_type *member_types, const char **member_names, + idx_t member_count); +#endif -* @param input The duckdb_timestamp_s value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_timestamp_s(duckdb_timestamp_s input); +#if DUCKDB_API_VERSION_AT_LEAST(0, 9, 0) +/*! + * Creates a STRUCT type based on the member types and names. The resulting type must be destroyed with + * `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.9.0 + * + * @param member_types The array of types of the struct members. + * @param member_names The array of names of the struct members. + * @param member_count The number of members of the struct. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_create_struct_type(duckdb_logical_type *member_types, const char **member_names, + idx_t member_count); +#endif -/*! -Creates a TIMESTAMP_MS value from a duckdb_timestamp_ms +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) +/*! + * Creates an ENUM type from the passed member name array. The resulting type should be destroyed with + * `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.10.0 + * + * @param member_names The array of names that the enum should consist of. + * @param member_count The number of elements that were specified in the array. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_create_enum_type(const char **member_names, idx_t member_count); +#endif -* @param input The duckdb_timestamp_ms value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_timestamp_ms(duckdb_timestamp_ms input); +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Creates a DECIMAL type with the specified width and scale. The resulting type should be destroyed with + * `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.3.3 + * + * @param width The width of the decimal type + * @param scale The scale of the decimal type + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_create_decimal_type(uint8_t width, uint8_t scale); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Creates a TIMESTAMP_NS value from a duckdb_timestamp_ns - -* @param input The duckdb_timestamp_ns value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_timestamp_ns(duckdb_timestamp_ns input); + * Retrieves the enum `duckdb_type` of a `duckdb_logical_type`. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type. + * @return duckdb_type + */ +DUCKDB_C_API duckdb_type duckdb_get_type_id(duckdb_logical_type type); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Creates a value from an interval + * Retrieves the width of a decimal type. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type object + * @return uint8_t + */ +DUCKDB_C_API uint8_t duckdb_decimal_width(duckdb_logical_type type); +#endif -* @param input The interval value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_interval(duckdb_interval input); +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Retrieves the scale of a decimal type. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type object + * @return uint8_t + */ +DUCKDB_C_API uint8_t duckdb_decimal_scale(duckdb_logical_type type); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Creates a value from a blob + * Retrieves the internal storage type of a decimal type. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type object + * @return duckdb_type + */ +DUCKDB_C_API duckdb_type duckdb_decimal_internal_type(duckdb_logical_type type); +#endif -* @param data The blob data -* @param length The length of the blob data -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_blob(const uint8_t *data, idx_t length); +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Retrieves the internal storage type of an enum type. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type object + * @return duckdb_type + */ +DUCKDB_C_API duckdb_type duckdb_enum_internal_type(duckdb_logical_type type); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Creates a BIT value from a duckdb_bit + * Retrieves the dictionary size of the enum type. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type object + * @return uint32_t + */ +DUCKDB_C_API uint32_t duckdb_enum_dictionary_size(duckdb_logical_type type); +#endif -* @param input The duckdb_bit value -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_bit(duckdb_bit input); +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Retrieves the dictionary value at the specified position from the enum. + * + * The result must be freed with `duckdb_free`. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type object + * @param index The index in the dictionary + * @return char* + */ +DUCKDB_C_API char *duckdb_enum_dictionary_value(duckdb_logical_type type, idx_t index); +#endif -/*! -Creates a UUID value from a uhugeint +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Retrieves the child type of the given LIST type. Also accepts MAP types. The result must be freed with + * `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type, either LIST or MAP. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_list_type_child_type(duckdb_logical_type type); +#endif -* @param input The duckdb_uhugeint containing the UUID -* @return The value. This must be destroyed with `duckdb_destroy_value`. -*/ -DUCKDB_C_API duckdb_value duckdb_create_uuid(duckdb_uhugeint input); +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 1) +/*! + * Retrieves the child type of the given ARRAY type. + * + * The result must be freed with `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.10.1 + * + * @param type The logical type. Must be ARRAY. + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_array_type_child_type(duckdb_logical_type type); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 1) /*! -Returns the boolean value of the given value. + * Retrieves the array size of the given array type. + * + * history: + * - stable: v0.10.1 + * + * @param type The logical type object + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_array_type_array_size(duckdb_logical_type type); +#endif -* @param val A duckdb_value containing a boolean -* @return A boolean, or false if the value cannot be converted -*/ -DUCKDB_C_API bool duckdb_get_bool(duckdb_value val); +#if DUCKDB_API_VERSION_AT_LEAST(0, 4, 0) +/*! + * Retrieves the key type of the given map type. + * + * The result must be freed with `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.4.0 + * + * @param type The logical type object + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_map_type_key_type(duckdb_logical_type type); +#endif + +#if DUCKDB_API_VERSION_AT_LEAST(0, 4, 0) +/*! + * Retrieves the value type of the given map type. + * + * The result must be freed with `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.4.0 + * + * @param type The logical type object + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_map_type_value_type(duckdb_logical_type type); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Returns the int8_t value of the given value. + * Returns the number of children of a struct type. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type object + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_struct_type_child_count(duckdb_logical_type type); +#endif -* @param val A duckdb_value containing a tinyint -* @return A int8_t, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API int8_t duckdb_get_int8(duckdb_value val); +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Retrieves the name of the struct child. + * + * The result must be freed with `duckdb_free`. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type object + * @param index The child index + * @return char* + */ +DUCKDB_C_API char *duckdb_struct_type_child_name(duckdb_logical_type type, idx_t index); +#endif + +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) +/*! + * Retrieves the child type of the given struct type at the specified index. + * + * The result must be freed with `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type object + * @param index The child index + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_struct_type_child_type(duckdb_logical_type type, idx_t index); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 7, 0) /*! -Returns the uint8_t value of the given value. + * Returns the number of members that the union type has. + * + * history: + * - stable: v0.7.0 + * + * @param type The logical type (union) object + * @return idx_t + */ +DUCKDB_C_API idx_t duckdb_union_type_member_count(duckdb_logical_type type); +#endif -* @param val A duckdb_value containing a utinyint -* @return A uint8_t, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API uint8_t duckdb_get_uint8(duckdb_value val); +#if DUCKDB_API_VERSION_AT_LEAST(0, 7, 0) +/*! + * Retrieves the name of the union member. + * + * The result must be freed with `duckdb_free`. + * + * history: + * - stable: v0.7.0 + * + * @param type The logical type object + * @param index The child index + * @return char* + */ +DUCKDB_C_API char *duckdb_union_type_member_name(duckdb_logical_type type, idx_t index); +#endif + +#if DUCKDB_API_VERSION_AT_LEAST(0, 7, 0) +/*! + * Retrieves the child type of the given union member at the specified index. + * + * The result must be freed with `duckdb_destroy_logical_type`. + * + * history: + * - stable: v0.7.0 + * + * @param type The logical type object + * @param index The child index + * @return duckdb_logical_type + */ +DUCKDB_C_API duckdb_logical_type duckdb_union_type_member_type(duckdb_logical_type type, idx_t index); +#endif +#if DUCKDB_API_VERSION_AT_LEAST(0, 3, 3) /*! -Returns the int16_t value of the given value. + * Destroys the logical type and de-allocates all memory allocated for that type. + * + * history: + * - stable: v0.3.3 + * + * @param type The logical type to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_logical_type(duckdb_logical_type *type); +#endif -* @param val A duckdb_value containing a smallint -* @return A int16_t, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API int16_t duckdb_get_int16(duckdb_value val); +#if DUCKDB_API_VERSION_AT_LEAST(1, 1, 0) +/*! + * Registers a custom type within the given connection. The type must have an alias + * + * history: + * - stable: v1.1.0 + * + * @param con The connection to use + * @param type The custom type to register + * @param info + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_register_logical_type(duckdb_connection con, duckdb_logical_type type, + duckdb_create_type_info info); +#endif -/*! -Returns the uint16_t value of the given value. +#if DUCKDB_API_VERSION_AT_LEAST(1, 5, 6) +/*! + * Gets the CRS (Coordinate Reference System) of a GEOMETRY type. Result must be freed with `duckdb_free`. + * + * history: + * - unstable: v1.5.2 + * - stable: v1.5.6 + * + * @param type The GEOMETRY type. + * @return char* + */ +DUCKDB_C_API char *duckdb_geometry_type_get_crs(duckdb_logical_type type); +#endif -* @param val A duckdb_value containing a usmallint -* @return A uint16_t, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API uint16_t duckdb_get_uint16(duckdb_value val); +/* --- Struct definitions for logical_type --- */ -/*! -Returns the int32_t value of the given value. +/* ============================================================================ + * MODULE: pending + * ============================================================================ */ -* @param val A duckdb_value containing an integer -* @return A int32_t, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API int32_t duckdb_get_int32(duckdb_value val); +/* --- Enums for pending --- */ -/*! -Returns the uint32_t value of the given value. +/* --- Struct forward declarations for pending --- */ -* @param val A duckdb_value containing a uinteger -* @return A uint32_t, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API uint32_t duckdb_get_uint32(duckdb_value val); +/* --- Types for pending --- */ -/*! -Returns the int64_t value of the given value. +/* --- Constants for pending --- */ -* @param val A duckdb_value containing a bigint -* @return A int64_t, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API int64_t duckdb_get_int64(duckdb_value val); +/* --- Function pointer typedefs for pending --- */ + +/* --- Functions for pending --- */ +#if DUCKDB_API_VERSION_AT_LEAST(0, 5, 0) /*! -Returns the uint64_t value of the given value. + * Executes the prepared statement with the given bound parameters, and returns a pending result. The pending result + * represents an intermediate structure for a query that is not yet fully executed. The pending result can be used to + * incrementally execute a query, returning control to the client between tasks. + * + * Note that after calling `duckdb_pending_prepared`, the pending result should always be destroyed using + * `duckdb_destroy_pending`, even if this function returns DuckDBError. + * + * history: + * - stable: v0.5.0 + * + * @param prepared_statement The prepared statement to execute. + * @param out_result The pending query result. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_pending_prepared(duckdb_prepared_statement prepared_statement, + duckdb_pending_result *out_result); +#endif -* @param val A duckdb_value containing a ubigint -* @return A uint64_t, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API uint64_t duckdb_get_uint64(duckdb_value val); +#if DUCKDB_API_VERSION_AT_LEAST(0, 5, 0) +/*! + * Closes the pending result and de-allocates all memory allocated for the result. + * + * Destroying a pending result whose statement has not completed aborts the statement. On autocommit, its writes are + * rolled back. Inside a transaction, a statement that may write invalidates the transaction, and a read-only statement + * just stops. Until the statement has completed, preparing or running another statement on the connection fails with + * `DUCKDB_ERROR_RESOURCE_IN_USE` and leaves it untouched. `duckdb_pending_execution_is_finished` reporting true does + * not mean that the statement completed. To make sure a statement takes effect, complete it before destroying the + * pending result: call `duckdb_execute_pending`, and fetch a streaming result until `duckdb_fetch_chunk` returns NULL. + * A NULL chunk is also returned on an error, so check `duckdb_result_error` afterwards. + * + * history: + * - stable: v0.5.0 + * + * @param pending_result The pending result to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_pending(duckdb_pending_result *pending_result); +#endif -/*! -Returns the hugeint value of the given value. +#if DUCKDB_API_VERSION_AT_LEAST(0, 5, 0) +/*! + * Returns the error message contained within the pending result. + * + * The result of this function must not be freed. It will be cleaned up when `duckdb_destroy_pending` is called. + * + * history: + * - stable: v0.5.0 + * + * @param pending_result The pending result to fetch the error from. + * @return const char* + */ +DUCKDB_C_API const char *duckdb_pending_error(duckdb_pending_result pending_result); +#endif -* @param val A duckdb_value containing a hugeint -* @return A duckdb_hugeint, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API duckdb_hugeint duckdb_get_hugeint(duckdb_value val); +#if DUCKDB_API_VERSION_AT_LEAST(0, 5, 0) +/*! + * Executes a single task within the query, returning whether or not the query is ready. + * + * If this returns DUCKDB_PENDING_RESULT_READY, the duckdb_execute_pending function can be called to obtain the result. + * If this returns DUCKDB_PENDING_RESULT_NOT_READY, the duckdb_pending_execute_task function should be called again. If + * this returns DUCKDB_PENDING_ERROR, an error occurred during execution. + * + * The error message can be obtained by calling duckdb_pending_error on the pending_result. + * + * history: + * - stable: v0.5.0 + * + * @param pending_result The pending result to execute a task within. + * @return duckdb_pending_state + */ +DUCKDB_C_API duckdb_pending_state duckdb_pending_execute_task(duckdb_pending_result pending_result); +#endif -/*! -Returns the uhugeint value of the given value. +#if DUCKDB_API_VERSION_AT_LEAST(0, 10, 0) +/*! + * If this returns DUCKDB_PENDING_RESULT_READY, the duckdb_execute_pending function can be called to obtain the result. + * If this returns DUCKDB_PENDING_RESULT_NOT_READY, the duckdb_pending_execute_check_state function should be called + * again. If this returns DUCKDB_PENDING_ERROR, an error occurred during execution. + * + * The error message can be obtained by calling duckdb_pending_error on the pending_result. + * + * history: + * - stable: v0.10.0 + * + * @param pending_result The pending result. + * @return duckdb_pending_state + */ +DUCKDB_C_API duckdb_pending_state duckdb_pending_execute_check_state(duckdb_pending_result pending_result); +#endif -* @param val A duckdb_value containing a uhugeint -* @return A duckdb_uhugeint, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API duckdb_uhugeint duckdb_get_uhugeint(duckdb_value val); +#if DUCKDB_API_VERSION_AT_LEAST(0, 5, 0) +/*! + * Fully execute a pending query result, returning the final query result. + * + * If duckdb_pending_execute_task has been called until DUCKDB_PENDING_RESULT_READY was returned, this will return fast. + * Otherwise, all remaining tasks must be executed first. + * + * Note that the result must be freed with `duckdb_destroy_result`. + * + * history: + * - stable: v0.5.0 + * + * @param pending_result The pending result to execute. + * @param out_result The result object. + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_execute_pending(duckdb_pending_result pending_result, duckdb_result *out_result); +#endif -/*! -Returns the duckdb_bignum value of the given value. -The `data` field must be destroyed with `duckdb_free`. +#if DUCKDB_API_VERSION_AT_LEAST(0, 9, 0) +/*! + * Returns whether a duckdb_pending_state is finished executing. For example if `pending_state` is + * DUCKDB_PENDING_RESULT_READY, this function will return true. + * + * history: + * - stable: v0.9.0 + * + * @param pending_state The pending state on which to decide whether to finish execution. + * @return bool + */ +DUCKDB_C_API bool duckdb_pending_execution_is_finished(duckdb_pending_state pending_state); +#endif -* @param val A duckdb_value containing a BIGNUM -* @return A duckdb_bignum. The `data` field must be destroyed with `duckdb_free`. -*/ -DUCKDB_C_API duckdb_bignum duckdb_get_bignum(duckdb_value val); +/* --- Struct definitions for pending --- */ -/*! -Returns the duckdb_decimal value of the given value. +/* ============================================================================ + * MODULE: prepared_statement + * ============================================================================ */ -* @param val A duckdb_value containing a DECIMAL -* @return A duckdb_decimal, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API duckdb_decimal duckdb_get_decimal(duckdb_value val); +/* --- Enums for prepared_statement --- */ -/*! -Returns the float value of the given value. +/* --- Struct forward declarations for prepared_statement --- */ -* @param val A duckdb_value containing a float -* @return A float, or NAN if the value cannot be converted -*/ -DUCKDB_C_API float duckdb_get_float(duckdb_value val); +/* --- Types for prepared_statement --- */ -/*! -Returns the double value of the given value. +/* --- Constants for prepared_statement --- */ -* @param val A duckdb_value containing a double -* @return A double, or NAN if the value cannot be converted -*/ -DUCKDB_C_API double duckdb_get_double(duckdb_value val); +/* --- Function pointer typedefs for prepared_statement --- */ -/*! -Returns the date value of the given value. +/* --- Functions for prepared_statement --- */ -* @param val A duckdb_value containing a date -* @return A duckdb_date, or MinValue if the value cannot be converted -*/ -DUCKDB_C_API duckdb_date duckdb_get_date(duckdb_value val); +/*! + * Create a prepared statement object from a query. + * + * Note that after calling `duckdb_prepare`, the prepared statement should always be destroyed using + * `duckdb_destroy_prepare`, even if the prepare fails. + * + * If the prepare fails, `duckdb_prepare_error` can be called to obtain the reason why the prepare failed. + * + * history: + * - stable: v0.1.0 + * + * @param connection The connection object + * @param query The SQL query to prepare + * @param out_prepared_statement The resulting prepared statement object + * @return duckdb_state + */ +DUCKDB_C_API duckdb_state duckdb_prepare(duckdb_connection connection, const char *query, + duckdb_prepared_statement *out_prepared_statement); /*! -Returns the time value of the given value. + * Closes the prepared statement and de-allocates all memory allocated for the statement. + * + * history: + * - stable: v0.1.0 + * + * @param prepared_statement The prepared statement to destroy. + * @return void + */ +DUCKDB_C_API void duckdb_destroy_prepare(duckdb_prepared_statement *prepared_statement); -* @param val A duckdb_value containing a time -* @return A duckdb_time, or MinValue + + + Show Alternate Row Backgrounds + + Confirm Before Deleting Rows diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index a58c0d8161..4720abdb1c 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -276,6 +276,7 @@ crates/app/src/ui/data_grid/row_number_column.rs crates/app/src/ui/data_grid/selection.rs crates/app/src/ui/data_grid/selection_summary.rs crates/app/src/ui/data_grid/server_sorter.rs +crates/app/src/ui/data_grid/stripes.rs crates/app/src/ui/data_grid/table_keys.rs crates/app/src/ui/data_grid/test_grid.rs crates/app/src/ui/data_grid/tests/cells.rs From 9949acd90adec52a4f32e497124a81f56e704350 Mon Sep 17 00:00:00 2001 From: Cristian Date: Sat, 31 Oct 2026 11:58:50 +0100 Subject: [PATCH 41/85] fix(linux-workspace): make the tab menu items work and add Close All Tabs to the tab overview (#3930) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3930.added.md | 1 + linux/changelog.d/3930.fixed.md | 1 + .../app/src/ui/workspace/tests/close_tab.rs | 23 ++++ .../app/src/ui/workspace/tests/sessions.rs | 117 +++++++++++++++++- .../app/src/ui/workspace/workspace_actions.rs | 17 ++- .../app/src/ui/workspace/workspace_shell.rs | 49 ++++++-- .../src/ui/workspace/workspace_shell_imp.rs | 2 + linux/data/resources/gtk/menus.ui | 4 + 8 files changed, 203 insertions(+), 11 deletions(-) create mode 100644 linux/changelog.d/3930.added.md create mode 100644 linux/changelog.d/3930.fixed.md diff --git a/linux/changelog.d/3930.added.md b/linux/changelog.d/3930.added.md new file mode 100644 index 0000000000..6a53fe1e83 --- /dev/null +++ b/linux/changelog.d/3930.added.md @@ -0,0 +1 @@ +The tab overview's menu has Close All Tabs. diff --git a/linux/changelog.d/3930.fixed.md b/linux/changelog.d/3930.fixed.md new file mode 100644 index 0000000000..14754c4699 --- /dev/null +++ b/linux/changelog.d/3930.fixed.md @@ -0,0 +1 @@ +The tab menu's close items were always dimmed and could not be chosen. diff --git a/linux/crates/app/src/ui/workspace/tests/close_tab.rs b/linux/crates/app/src/ui/workspace/tests/close_tab.rs index 39bb3ad8a1..b98e455209 100644 --- a/linux/crates/app/src/ui/workspace/tests/close_tab.rs +++ b/linux/crates/app/src/ui/workspace/tests/close_tab.rs @@ -311,3 +311,26 @@ fn an_editor_asking_to_close_closes_its_own_tab_not_the_one_in_front() { assert!(tab_is_open(&test, front)); assert_eq!(workspace.selected_tab(), Some(front)); } + +/// Close All Tabs asks about each tab as its close button would. +#[gtk4::test] +fn close_all_tabs_asks_before_closing_unsaved_work() { + let test = TestWindow::connected("CloseAllUnsavedFile"); + let path = test.dir().join("daily.sql"); + crate::test_support::write_text_file(&path, "SELECT 1;"); + let (id, _editor) = editor_on_file(&test, &path); + let workspace = test.workspace(); + super::open_editor(&workspace); + let view = shell_of(&workspace).tab_view(); + + WidgetExt::activate_action(&test.window, "win.close-all-tabs", None).expect("win.close-all-tabs"); + + wait_until(WAIT, || { + visible_alert(&test.window).is_some_and(|alert| alert.heading().as_deref() == Some("Save Changes?")) + }) + .expect("the close asks about the file"); + assert!(answer(&test.window, "cancel")); + wait_until(WAIT, || view.n_pages() == 1).expect("the tabs with nothing to lose to close"); + assert!(tab_is_open(&test, id), "the tab with unsaved changes closed"); + assert_eq!(crate::test_support::read_text_file(&path), "SELECT 1;"); +} diff --git a/linux/crates/app/src/ui/workspace/tests/sessions.rs b/linux/crates/app/src/ui/workspace/tests/sessions.rs index 046eb2f954..c350b90838 100644 --- a/linux/crates/app/src/ui/workspace/tests/sessions.rs +++ b/linux/crates/app/src/ui/workspace/tests/sessions.rs @@ -5,7 +5,7 @@ use gtk4::prelude::*; use gtk4::subclass::prelude::*; use super::{open_editor, two_sessions}; -use crate::test_support::wait_until; +use crate::test_support::{descendants, wait_until}; use crate::ui::session_window::tests::WAIT; #[gtk4::test] @@ -154,3 +154,118 @@ fn the_tab_menu_closes_later_tabs_of_any_session_and_ends_with_close() { } } } + +/// libadwaita parents the tab menu to a tab box in the bar and to a +/// tab grid in the overview, never to the view. +#[gtk4::test] +fn the_tab_menu_reaches_its_actions_from_the_bar_and_the_overview() { + let sessions = two_sessions("TabMenuReach", "Other"); + let shell = &sessions.shell; + open_editor(&sessions.first); + let parents: Vec = descendants(&shell.imp().tab_overview.get()) + .into_iter() + .filter(|widget| ["AdwTabBox", "AdwTabGrid"].contains(&widget.type_().name())) + .collect(); + assert!( + parents.iter().any(|widget| widget.type_().name() == "AdwTabBox") + && parents.iter().any(|widget| widget.type_().name() == "AdwTabGrid"), + "no tab box or tab grid to open the menu from" + ); + for parent in &parents { + for action in ["close-others", "close-after", "close"] { + assert!( + parent.activate_action(&format!("tab.{action}"), None).is_ok(), + "tab.{action} is out of reach of {}", + parent.type_().name() + ); + } + } +} + +#[gtk4::test] +fn the_tab_menu_dims_what_would_close_nothing() { + let sessions = two_sessions("TabMenuDims", "Other"); + let shell = &sessions.shell; + let view = shell.tab_view(); + let only = open_editor(&sessions.first); + let enabled = |action: &str| { + shell + .imp() + .tab_actions + .get() + .is_some_and(|group| group.is_action_enabled(action)) + }; + + view.emit_by_name::<()>("setup-menu", &[&Some(only.clone())]); + assert!( + !enabled("close-others") && !enabled("close-after"), + "one tab has nothing else to close" + ); + assert!(enabled("close")); + + let last = open_editor(&sessions.second); + view.emit_by_name::<()>("setup-menu", &[&Some(only)]); + assert!(enabled("close-others") && enabled("close-after")); + view.emit_by_name::<()>("setup-menu", &[&Some(last)]); + assert!( + enabled("close-others") && !enabled("close-after"), + "the last tab has none after it" + ); +} + +#[gtk4::test] +fn close_all_tabs_closes_the_tabs_of_every_session() { + let sessions = two_sessions("CloseAllAcross", "Other"); + let (shell, first, second) = (&sessions.shell, &sessions.first, &sessions.second); + let view = shell.tab_view(); + open_editor(first); + open_editor(second); + open_editor(first); + + WidgetExt::activate_action(&sessions.test.window, "win.close-all-tabs", None).expect("win.close-all-tabs"); + + wait_until(WAIT, || view.n_pages() == 0).expect("every tab to close"); + assert!(!first.has_tabs() && !second.has_tabs()); +} + +/// As in GNOME Web: the overview lists every tab, and the tab menu +/// holds what acts on its own tab. +#[gtk4::test] +fn close_all_tabs_is_in_the_overview_menu_and_off_with_no_tab() { + let sessions = two_sessions("CloseAllInOverview", "Other"); + let window = &sessions.test.window; + let actions_of = |menu: &gio::MenuModel| -> Vec { + (0..menu.n_items()) + .filter_map(|section| menu.item_link(section, "section")) + .flat_map(|items| { + (0..items.n_items()) + .filter_map(|item| { + items + .item_attribute_value(item, "action", None) + .and_then(|value| value.get::()) + }) + .collect::>() + }) + .collect() + }; + let overview_menu = sessions + .shell + .imp() + .tab_overview + .secondary_menu() + .expect("the overview's menu"); + assert!(actions_of(&overview_menu).contains(&"win.close-all-tabs".to_owned())); + let tab_menu = sessions.shell.tab_view().menu_model().expect("the tab menu"); + assert!(!actions_of(&tab_menu).iter().any(|action| action.contains("close-all"))); + let enabled = || { + window + .lookup_action("close-all-tabs") + .is_some_and(|action| action.is_enabled()) + }; + + assert!(!enabled(), "Close All Tabs is on with no tab to close"); + let only = open_editor(&sessions.first); + assert!(enabled(), "Close All Tabs is off with a tab open"); + sessions.shell.tab_view().close_page(&only); + wait_until(WAIT, || !enabled()).expect("Close All Tabs to go off with the last tab"); +} diff --git a/linux/crates/app/src/ui/workspace/workspace_actions.rs b/linux/crates/app/src/ui/workspace/workspace_actions.rs index 5a724c5ea9..c4abf6075e 100644 --- a/linux/crates/app/src/ui/workspace/workspace_actions.rs +++ b/linux/crates/app/src/ui/workspace/workspace_actions.rs @@ -2,8 +2,8 @@ //! them once and each one goes to the session in front, so a change of //! front changes where they go without taking any off the window. Their //! keys are application accelerators; a tab's own keys belong to the tab. -//! Reopen Closed Tab, Switch to Recent Tab and View Open Tabs are the -//! shell's own: the tabs they name may be any session's. +//! Reopen Closed Tab, Close All Tabs, Switch to Recent Tab and View Open +//! Tabs are the shell's own: the tabs they name may be any session's. use gtk4::glib; use gtk4::prelude::*; @@ -32,6 +32,7 @@ const ENTRIES: [(&str, ActionInput); 6] = [ ]; const REOPEN_CLOSED_TAB: &str = "reopen-closed-tab"; +const CLOSE_ALL_TABS: &str = "close-all-tabs"; const SWITCH_RECENT_TAB: &str = "switch-recent-tab"; const SHOW_TABS: &str = "show-tabs"; @@ -62,6 +63,14 @@ pub(super) fn install(window: &adw::ApplicationWindow, shell: &WorkspaceShell) { } }) .build()]); + let closing = shell.downgrade(); + window.add_action_entries([gio::ActionEntry::::builder(CLOSE_ALL_TABS) + .activate(move |_, _, _| { + if let Some(shell) = closing.upgrade() { + shell.close_all_tabs(); + } + }) + .build()]); let switching = shell.downgrade(); window.add_action_entries([gio::ActionEntry::::builder(SWITCH_RECENT_TAB) .activate(move |_, _, _| { @@ -80,7 +89,7 @@ pub(super) fn install(window: &adw::ApplicationWindow, shell: &WorkspaceShell) { .build()]); // The overview refuses to open or close with no tab, and with one tab // Ctrl+E goes on to the tab. - for (name, least) in [(SWITCH_RECENT_TAB, 2), (SHOW_TABS, 1)] { + for (name, least) in [(SWITCH_RECENT_TAB, 2), (SHOW_TABS, 1), (CLOSE_ALL_TABS, 1)] { if let Some(action) = window.lookup_action(name) { shell .tab_view() @@ -102,6 +111,7 @@ pub(super) fn remove(window: &adw::ApplicationWindow) { window.remove_action(name); } window.remove_action(REOPEN_CLOSED_TAB); + window.remove_action(CLOSE_ALL_TABS); window.remove_action(SWITCH_RECENT_TAB); window.remove_action(SHOW_TABS); } @@ -181,6 +191,7 @@ mod tests { remove(&window); assert!(window.lookup_action("new-editor-tab").is_none()); assert!(window.lookup_action(REOPEN_CLOSED_TAB).is_none()); + assert!(window.lookup_action(CLOSE_ALL_TABS).is_none()); assert!(window.lookup_action(SWITCH_RECENT_TAB).is_none()); assert!(window.lookup_action(SHOW_TABS).is_none()); } diff --git a/linux/crates/app/src/ui/workspace/workspace_shell.rs b/linux/crates/app/src/ui/workspace/workspace_shell.rs index c6d16a2802..a44182b629 100644 --- a/linux/crates/app/src/ui/workspace/workspace_shell.rs +++ b/linux/crates/app/src/ui/workspace/workspace_shell.rs @@ -170,12 +170,6 @@ impl WorkspaceShell { /// says left or right, as the GNOME HIG asks. fn connect_tab_menu(&self) { let imp = self.imp(); - let target = self.downgrade(); - imp.tab_view.connect_setup_menu(move |_, page| { - if let Some(shell) = target.upgrade() { - shell.imp().menu_target.replace(page.cloned()); - } - }); let group = gio::SimpleActionGroup::new(); let menu_action = |name: &str, close: fn(&adw::TabView, &adw::TabPage)| { let action = gio::SimpleAction::new(name, None); @@ -194,7 +188,20 @@ impl WorkspaceShell { group.add_action(&menu_action("close-others", |view, page| view.close_other_pages(page))); group.add_action(&menu_action("close-after", |view, page| view.close_pages_after(page))); group.add_action(&menu_action("close", |view, page| view.close_page(page))); - imp.tab_view.insert_action_group("tab", Some(&group)); + // The bar and the overview parent the menu's popover, and the view + // is an ancestor of neither. + self.insert_action_group("tab", Some(&group)); + let _ = imp.tab_actions.set(group); + let target = self.downgrade(); + imp.tab_view.connect_setup_menu(move |view, page| { + let Some(shell) = target.upgrade() else { + return; + }; + shell.imp().menu_target.replace(page.cloned()); + if let Some(page) = page { + shell.dim_tab_menu(view, page); + } + }); let others = gio::Menu::new(); others.append(Some(&gettext("Close Other Tabs")), Some("tab.close-others")); others.append(Some(&gettext("Close Tabs After This One")), Some("tab.close-after")); @@ -206,6 +213,34 @@ impl WorkspaceShell { imp.tab_view.set_menu_model(Some(&menu)); } + /// Dim what would close nothing for `page`, whose menu opens. + fn dim_tab_menu(&self, view: &adw::TabView, page: &adw::TabPage) { + let Some(group) = self.imp().tab_actions.get() else { + return; + }; + let last = view.n_pages() - 1; + for (name, enabled) in [ + ("close-others", last > 0), + ("close-after", view.page_position(page) < last), + ] { + if let Some(action) = group.lookup_action(name).and_downcast::() { + action.set_enabled(enabled); + } + } + } + + /// Close every session's tabs, each through its own session's + /// prompts. + pub(super) fn close_all_tabs(&self) { + let view = self.tab_view(); + // The selected page goes last, so the view does not select each + // neighbour in turn and bring its session forward. + if let Some(selected) = view.selected_page() { + view.close_other_pages(&selected); + view.close_page(&selected); + } + } + fn connect_view(&self) { let view = self.imp().tab_view.get(); let shell = self.downgrade(); diff --git a/linux/crates/app/src/ui/workspace/workspace_shell_imp.rs b/linux/crates/app/src/ui/workspace/workspace_shell_imp.rs index b0759fce2d..da9d8cd4ec 100644 --- a/linux/crates/app/src/ui/workspace/workspace_shell_imp.rs +++ b/linux/crates/app/src/ui/workspace/workspace_shell_imp.rs @@ -113,6 +113,8 @@ pub struct WorkspaceShell { pub(super) sender: OnceCell>, /// The tab page whose menu is open: the view names it only then. pub(super) menu_target: RefCell>, + /// The tab menu's actions, which dim for the page the menu opens on. + pub(super) tab_actions: OnceCell, /// The tabs closed last, the newest at the back. pub(super) closed_tabs: RefCell>, /// The tabs the window showed, the latest first. diff --git a/linux/data/resources/gtk/menus.ui b/linux/data/resources/gtk/menus.ui index cf47648688..6b9a30ab68 100644 --- a/linux/data/resources/gtk/menus.ui +++ b/linux/data/resources/gtk/menus.ui @@ -65,6 +65,10 @@ Reopen Closed Tab win.reopen-closed-tab + + Close All Tabs + win.close-all-tabs + From 95be80eec1f54a8c753ebbeb08b3a083e14c52d6 Mon Sep 17 00:00:00 2001 From: Cristian Date: Sat, 31 Oct 2026 11:59:49 +0100 Subject: [PATCH 42/85] fix(linux-workspace): offer New Connection in the primary menu (#3931) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3931.fixed.md | 1 + .../app/src/ui/workspace/tests/shell.rs | 53 ++++++++++++++++++- linux/data/resources/gtk/menus.ui | 4 ++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 linux/changelog.d/3931.fixed.md diff --git a/linux/changelog.d/3931.fixed.md b/linux/changelog.d/3931.fixed.md new file mode 100644 index 0000000000..af4b0e0d39 --- /dev/null +++ b/linux/changelog.d/3931.fixed.md @@ -0,0 +1 @@ +A window showing a connection had no way to add a new connection. diff --git a/linux/crates/app/src/ui/workspace/tests/shell.rs b/linux/crates/app/src/ui/workspace/tests/shell.rs index 8197dfbeb4..33d5447b98 100644 --- a/linux/crates/app/src/ui/workspace/tests/shell.rs +++ b/linux/crates/app/src/ui/workspace/tests/shell.rs @@ -12,7 +12,7 @@ use libadwaita::prelude::*; use super::{served_table, shell_of}; use crate::test_support::{descendants, wait_until}; use crate::ui::SessionWindowInput; -use crate::ui::session_window::tests::{Script, TestWindow}; +use crate::ui::session_window::tests::{Script, TestWindow, WAIT}; use crate::ui::workspace::{WorkspaceShell, WorkspaceTab}; fn overview_new_tab_button(overview: &adw::TabOverview) -> gtk::Button { @@ -418,3 +418,54 @@ fn the_overview_opens_a_tab_in_a_session_that_came_forward_from_its_own_input() "the new tab went to a session behind" ); } + +/// Choose New Connection… in the main menu under `header`, as a click +/// does, and wait for the connection dialog. +fn choose_new_connection(test: &TestWindow, header: &impl IsA) { + let button = descendants(header) + .into_iter() + .filter_map(|widget| widget.downcast::().ok()) + .find(gtk::MenuButton::is_primary) + .expect("the main menu button"); + button.popup(); + let menu = button.popover().expect("the main menu"); + wait_until(WAIT, || menu.is_visible()).expect("the main menu to open"); + // GTK keeps a menu item private and gives it no action name. + let item = descendants(&menu) + .into_iter() + .find(|widget| { + widget.type_().name() == "GtkModelButton" + && widget.property::>("text").as_deref() == Some("New Connection…") + }) + .expect("New Connection… in the main menu"); + wait_until(WAIT, || item.is_sensitive()).expect("New Connection… to be enabled"); + + item.emit_by_name::<()>("clicked", &[]); + + wait_until(WAIT, || test.window.visible_dialog().is_some()).expect("the connection dialog"); +} + +#[gtk4::test] +fn the_main_menu_adds_a_connection_beside_an_open_one() { + let test = TestWindow::connected("MainMenuNewConnection"); + // The overview's own menu button is a primary one too. + let header = shell_of(&test.workspace()).imp().sidebar_header.get(); + choose_new_connection(&test, &header); +} + +#[gtk4::test] +fn the_main_menu_adds_a_connection_with_none_open() { + let test = TestWindow::picker("PickerMenuNewConnection"); + let menu = test.app.menu_by_id("primary_menu").expect("the primary menu"); + let first = menu.item_link(0, "section").expect("a first section"); + assert_eq!( + first + .item_attribute_value(0, "action", None) + .and_then(|value| value.get::()) + .as_deref(), + Some("win.new-connection"), + "New Connection… does not lead the menu" + ); + + choose_new_connection(&test, &test.window.content().expect("the picker")); +} diff --git a/linux/data/resources/gtk/menus.ui b/linux/data/resources/gtk/menus.ui index 6b9a30ab68..9517677366 100644 --- a/linux/data/resources/gtk/menus.ui +++ b/linux/data/resources/gtk/menus.ui @@ -4,6 +4,10 @@ startup; the windows read the menus with menu_by_id. -->
+ + New Connection… + win.new-connection + New Window app.new-window From c554e5c5cc7d33d0ca3f8672be678494c744edd8 Mon Sep 17 00:00:00 2001 From: Cristian Date: Sat, 31 Oct 2026 12:23:29 +0100 Subject: [PATCH 43/85] feat(linux-workspace): open another of a connection's databases beside it in the window (#3902) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3902.added.md | 1 + .../crates/app/src/application/mcp_bridge.rs | 4 +- .../app/src/services/connection_deletion.rs | 59 ++++ .../app/src/services/file_resolution.rs | 2 +- linux/crates/app/src/ui/session_target.rs | 16 +- .../app/src/ui/session_window/connections.rs | 76 +++-- linux/crates/app/src/ui/session_window/mod.rs | 118 ++++--- .../src/ui/session_window/other_databases.rs | 311 ++++++++++++++++++ .../app/src/ui/session_window/session_page.rs | 29 ++ .../src/ui/session_window/session_restore.rs | 3 + .../src/ui/session_window/session_teardown.rs | 31 +- .../ui/session_window/session_window_input.rs | 3 + .../app/src/ui/session_window/tests/mod.rs | 1 + .../session_window/tests/other_databases.rs | 184 +++++++++++ .../src/ui/session_window/window_actions.rs | 3 +- .../crates/app/src/ui/workspace/assistant.rs | 4 +- linux/crates/app/src/ui/workspace/mod.rs | 4 +- .../ui/workspace/sidebar/connection_tree.rs | 64 +++- .../workspace/sidebar/sidebar_affordances.rs | 8 +- .../src/ui/workspace/sidebar/sidebar_menu.rs | 2 + .../app/src/ui/workspace/sidebar/tests.rs | 33 +- .../app/src/ui/workspace/sidebar/tree_menu.rs | 7 +- .../app/src/ui/workspace/tab_factory.rs | 10 +- .../crates/app/src/ui/workspace/tab_title.rs | 11 + .../crates/app/src/workspace/close_guards.rs | 41 ++- .../app/src/workspace/session_key_record.rs | 10 + .../app/src/workspace/workspace_file_name.rs | 12 + .../app/src/workspace/workspace_store.rs | 148 ++++++++- linux/crates/app/src/workspace/write_job.rs | 8 + .../app/src/workspace/writes_in_flight.rs | 10 + .../session/src/registry/session_key.rs | 9 + ...0030-a-database-is-a-session-of-its-own.md | 5 +- linux/po/POTFILES.in | 2 + 33 files changed, 1111 insertions(+), 118 deletions(-) create mode 100644 linux/changelog.d/3902.added.md create mode 100644 linux/crates/app/src/ui/session_window/other_databases.rs create mode 100644 linux/crates/app/src/ui/session_window/tests/other_databases.rs diff --git a/linux/changelog.d/3902.added.md b/linux/changelog.d/3902.added.md new file mode 100644 index 0000000000..84a39c3c09 --- /dev/null +++ b/linux/changelog.d/3902.added.md @@ -0,0 +1 @@ +Open Database… on a connection's row opens another of the server's databases beside it in the window, with its own tables and tabs. diff --git a/linux/crates/app/src/application/mcp_bridge.rs b/linux/crates/app/src/application/mcp_bridge.rs index fc35e500c5..f4fd0e552f 100644 --- a/linux/crates/app/src/application/mcp_bridge.rs +++ b/linux/crates/app/src/application/mcp_bridge.rs @@ -138,7 +138,9 @@ fn answer(app: &TableProApplication, request: McpRequest) { .into_iter() .filter_map(|(key, _)| match key { SessionKey::Saved(id) => Some(id), - SessionKey::File(_) => None, + // Another database of an open connection is no other + // connection. + SessionKey::SavedIn { .. } | SessionKey::File(_) => None, }) .collect(); let _ = reply.send(open); diff --git a/linux/crates/app/src/services/connection_deletion.rs b/linux/crates/app/src/services/connection_deletion.rs index 8344ae13eb..723ca09658 100644 --- a/linux/crates/app/src/services/connection_deletion.rs +++ b/linux/crates/app/src/services/connection_deletion.rs @@ -61,6 +61,9 @@ impl ConnectionDeletion { if let Err(error) = self.workspaces.purge(&SessionKey::Saved(id)).await { report.purge_failures.push(PurgeFailure::Workspace(error)); } + if let Err(error) = self.workspaces.purge_databases_of(id).await { + report.purge_failures.push(PurgeFailure::Workspace(error)); + } let Some(history) = self.history.clone() else { let unavailable = || "the query history did not open".to_owned(); report.purge_failures.push(PurgeFailure::SavedQueries(unavailable())); @@ -357,6 +360,62 @@ mod tests { } } + /// Another database opened beside the connection keeps its tabs and + /// drafts under a name of its own, which goes with the connection. + #[tokio::test] + async fn the_tabs_of_its_other_databases_go_with_the_connection() { + let fixture = fixture().await; + let (database, other_database) = ( + SessionKey::SavedIn { + id: fixture.id, + database: "analytics".to_owned(), + }, + SessionKey::SavedIn { + id: fixture.other, + database: "analytics".to_owned(), + }, + ); + let names = [&database, &other_database].map(WorkspaceFileName::for_key); + for (key, name) in [&database, &other_database].into_iter().zip(&names) { + let draft = DraftId::new(); + fixture + .drafts + .write_blocking(&name.draft_scope(), draft, "SELECT 1") + .expect("a draft"); + fixture + .workspaces + .schedule( + key, + WorkspaceRecord { + active_tab: Some(0), + tabs: vec![TabRecord::Editor { + draft, + file: None, + database: None, + }], + }, + ) + .expect("scheduled"); + fixture.workspaces.flush(key).await.expect("the saved tabs"); + } + let workspaces_dir = fixture.paths.workspaces_dir(); + let drafts_dir = |name: &WorkspaceFileName| fixture.drafts.root().join(name.draft_scope().as_str()); + + fixture + .deletion() + .run(fixture.id, DeletionOptions { delete_history: false }) + .await + .expect("the delete"); + + assert!(!workspaces_dir.join(names[0].file_name()).exists(), "its tabs stayed"); + assert!(!drafts_dir(&names[0]).exists(), "its drafts stayed"); + assert!( + workspaces_dir.join(names[1].file_name()).exists(), + "another connection's went" + ); + assert!(drafts_dir(&names[1]).exists()); + } + #[tokio::test] async fn secret_failure_keeps_entry_and_skips_purge() { let fixture = fixture().await; diff --git a/linux/crates/app/src/services/file_resolution.rs b/linux/crates/app/src/services/file_resolution.rs index 698ddbd161..4a9120ff42 100644 --- a/linux/crates/app/src/services/file_resolution.rs +++ b/linux/crates/app/src/services/file_resolution.rs @@ -77,7 +77,7 @@ pub fn resolve_file_blocking( Some(connection) => FileTarget::Saved(Box::new(connection)), None => FileTarget::File(identity), }, - SessionKey::File(_) => FileTarget::File(identity), + SessionKey::SavedIn { .. } | SessionKey::File(_) => FileTarget::File(identity), } }); ResolvedFile { uri, target } diff --git a/linux/crates/app/src/ui/session_target.rs b/linux/crates/app/src/ui/session_target.rs index ec039278bd..11fe1afc59 100644 --- a/linux/crates/app/src/ui/session_target.rs +++ b/linux/crates/app/src/ui/session_target.rs @@ -7,12 +7,15 @@ use tablepro_session::SessionKey; use uuid::Uuid; const SAVED: &str = "saved:"; +const SAVED_IN: &str = "saved-in:"; const FILE: &str = "file:"; -/// `saved:` or `file:`. +/// `saved:`, `saved-in:/` or `file:`. A uuid +/// holds no `/`, so the database after it may hold any. pub(crate) fn to_variant(key: &SessionKey) -> glib::Variant { match key { SessionKey::Saved(id) => format!("{SAVED}{id}"), + SessionKey::SavedIn { id, database } => format!("{SAVED_IN}{id}/{database}"), SessionKey::File(uri) => format!("{FILE}{uri}"), } .to_variant() @@ -24,6 +27,13 @@ pub(crate) fn from_variant(value: &glib::Variant) -> Option { if let Some(id) = text.strip_prefix(SAVED) { return Uuid::parse_str(id).ok().map(SessionKey::Saved); } + if let Some(rest) = text.strip_prefix(SAVED_IN) { + let (id, database) = rest.split_once('/')?; + return Uuid::parse_str(id).ok().map(|id| SessionKey::SavedIn { + id, + database: database.to_owned(), + }); + } text.strip_prefix(FILE) .filter(|uri| glib::Uri::peek_scheme(uri).is_some()) .map(|uri| SessionKey::File(uri.to_owned())) @@ -37,6 +47,10 @@ mod tests { fn a_key_round_trips() { let keys = [ SessionKey::Saved(Uuid::new_v4()), + SessionKey::SavedIn { + id: Uuid::new_v4(), + database: "sales/2026".to_owned(), + }, SessionKey::File("file:///home/u/R%26D/sales.db".to_owned()), SessionKey::File("file:///srv/caf%C3%A9.sqlite3".to_owned()), ]; diff --git a/linux/crates/app/src/ui/session_window/connections.rs b/linux/crates/app/src/ui/session_window/connections.rs index 2590b8b080..82c6346880 100644 --- a/linux/crates/app/src/ui/session_window/connections.rs +++ b/linux/crates/app/src/ui/session_window/connections.rs @@ -481,19 +481,27 @@ impl SessionWindow { /// connection opened again, so the window reconnects once the tabs /// may go; until then the saved level waits for the next connect. pub(super) fn on_set_safe_mode(&self, key: &SessionKey, level: SafeModeLevel) { - let Some(session) = self - .imp() - .sessions - .borrow() - .iter() - .find(|open| open.claim.key() == key) - .map(|open| open.session.clone()) - else { + // The level is the connection's, which its other databases took + // when they opened (0030). + let key = match key { + SessionKey::SavedIn { id, .. } => SessionKey::Saved(*id), + SessionKey::Saved(_) | SessionKey::File(_) => key.clone(), + }; + let Some(sessions) = self.find_open(|open| { + (open.claim.key() == &key).then(|| { + open.with_databases() + .map(|open| (open.claim.key().clone(), open.session.clone())) + .collect::>() + }) + }) else { + return; + }; + let Some((_, session)) = sessions.first() else { return; }; - let saved = match key { + let saved = match &key { SessionKey::Saved(id) => self.stored_connection(*id), - SessionKey::File(_) => None, + SessionKey::SavedIn { .. } | SessionKey::File(_) => None, }; // The saved level can differ from the session's after a reconnect // was declined, and a level still being written can land after @@ -504,7 +512,9 @@ impl SessionWindow { let differs = session.safe_mode() != level || writing || saved.as_ref().is_some_and(|saved| saved.safe_mode != level); if differs { - let applied = session.safe_mode() == level || session.set_safe_mode(level); + let applied = sessions + .iter() + .all(|(_, session)| session.safe_mode() == level || session.set_safe_mode(level)); match (saved.map(|saved| saved.id), applied) { (Some(id), _) => self.save_safe_mode(LevelSave { id, @@ -517,7 +527,9 @@ impl SessionWindow { )), } if applied { - self.deliver_to(key, WorkspaceInput::SafeModeChanged); + for (key, _) in &sessions { + self.deliver_to(key, WorkspaceInput::SafeModeChanged); + } } } self.show_safe_mode(); @@ -571,20 +583,20 @@ impl SessionWindow { /// Ask the tabs, as Disconnect does, then connect again. fn reopen_after_teardown(&self, id: Uuid) { - let Some(workspace) = self.session_workspace(&SessionKey::Saved(id)) else { - return; - }; - let (Some(handle), Some(guards), Some(input)) = (workspace.prompt_handle(), self.close_guards(), self.input()) - else { + let (Some(handles), Some(guards), Some(input)) = ( + self.teardown_handles(&SessionKey::Saved(id)), + self.close_guards(), + self.input(), + ) else { return; }; guards.begin_teardown(); - if guards.may_tear_down(&handle) { + if guards.may_tear_down(&handles) { input.deliver(SessionWindowInput::ReopenApproved(id)); return; } let window = self.downgrade(); - guards.ask_tear_down(self.upcast_ref(), &handle, move |decision| match decision { + guards.ask_tear_down(self.upcast_ref(), &handles, move |decision| match decision { CloseDecision::Approved => input.deliver(SessionWindowInput::ReopenApproved(id)), _ => { if let Some(window) = window.upgrade() { @@ -595,17 +607,31 @@ impl SessionWindow { } /// Tear the session down and connect it again through the window's - /// one connect path, at the level just saved. The window's other - /// sessions and connects go on. + /// one connect path, at the level just saved, with the other + /// databases it had open. The window's other sessions and connects + /// go on. pub(super) fn on_reopen_approved(&self, id: Uuid) { + let key = SessionKey::Saved(id); let stored = self.stored_connection(id); - if !self.tear_down_session(&SessionKey::Saved(id)) { + let databases = self + .find_open(|open| (open.claim.key() == &key).then(|| open.databases.names())) + .unwrap_or_default(); + if !self.tear_down_session(&key) { return; } - match stored { - Some(saved) => self.connect(ConnectSource::Saved(Box::new(saved)), CredentialDemand::default()), - None => self.show_toast(&crate::i18n::gettext("The connection was deleted.")), + let Some(saved) = stored else { + self.show_toast(&crate::i18n::gettext("The connection was deleted.")); + return; + }; + let source = ConnectSource::Saved(Box::new(saved)); + let Some(claim) = self.claim_for(&key) else { + return; + }; + if let Some(mut pending) = self.spawn_connect(claim, source, None, CredentialDemand::default()) { + pending.databases = databases; + self.imp().connects.borrow_mut().push(pending); } + self.connects_changed(); } /// Tick the level of the session in front in the main menu. diff --git a/linux/crates/app/src/ui/session_window/mod.rs b/linux/crates/app/src/ui/session_window/mod.rs index f577d2cb9b..dd8f494994 100644 --- a/linux/crates/app/src/ui/session_window/mod.rs +++ b/linux/crates/app/src/ui/session_window/mod.rs @@ -21,6 +21,7 @@ mod locate_file; mod notification_request; mod notification_sink; mod notifications; +mod other_databases; mod picker_page; mod resolve_file_identity; mod secret_retry; @@ -333,10 +334,14 @@ impl SessionWindow { } fn holds_session(&self, key: &SessionKey) -> bool { - self.imp() - .sessions - .try_borrow() - .is_ok_and(|sessions| sessions.iter().any(|open| open.claim.key() == key)) + self.find_open(|open| (open.claim.key() == key).then_some(())).is_some() + } + + /// What `pick` finds first among the window's sessions, the other + /// databases open beside them included. + fn find_open(&self, pick: impl FnMut(&OpenSession) -> Option) -> Option { + let sessions = self.imp().sessions.try_borrow().ok()?; + sessions.iter().flat_map(OpenSession::with_databases).find_map(pick) } fn is_connecting(&self, key: &SessionKey) -> bool { @@ -356,7 +361,13 @@ impl SessionWindow { pub(crate) fn workspaces(&self) -> Vec { self.imp().sessions.try_borrow().map_or_else( |_| Vec::new(), - |sessions| sessions.iter().map(|open| open.workspace.clone()).collect(), + |sessions| { + sessions + .iter() + .flat_map(OpenSession::with_databases) + .map(|open| open.workspace.clone()) + .collect() + }, ) } @@ -374,21 +385,13 @@ impl SessionWindow { /// The workspace of the session under `key`. fn session_workspace(&self, key: &SessionKey) -> Option { - let sessions = self.imp().sessions.try_borrow().ok()?; - sessions - .iter() - .find(|open| open.claim.key() == key) - .map(|open| open.workspace.clone()) + self.find_open(|open| (open.claim.key() == key).then(|| open.workspace.clone())) } /// The key and session of the session in front. pub(super) fn front_session(&self) -> Option<(SessionKey, Arc)> { let front = self.workspace()?; - let sessions = self.imp().sessions.try_borrow().ok()?; - sessions - .iter() - .find(|open| open.workspace == front) - .map(|open| (open.claim.key().clone(), open.session.clone())) + self.find_open(|open| (open.workspace == front).then(|| (open.claim.key().clone(), open.session.clone()))) } /// The shell for the window's first session, which the later ones @@ -465,6 +468,7 @@ impl SessionWindow { SessionWindowInput::ConnectionSaved(id) => self.on_connection_saved(id), SessionWindowInput::Disconnect => self.on_disconnect_front(), SessionWindowInput::DisconnectSession(key) => self.on_disconnect(key), + SessionWindowInput::ChooseDatabaseIn(key) => self.choose_database_in(key), SessionWindowInput::SessionTeardownApproved(key) => self.on_session_teardown_approved(&key), SessionWindowInput::ShowSession(key) => self.show_session(&key), SessionWindowInput::NewQueryIn(key) => self.deliver_to(&key, WorkspaceInput::NewEditorTab), @@ -659,12 +663,24 @@ impl SessionWindow { self.ask_about_location(id, kind); return; } - let PendingConnect { claim, source, .. } = self.imp().connects.borrow_mut().remove(position); + let PendingConnect { + claim, + source, + databases, + .. + } = self.imp().connects.borrow_mut().remove(position); match result { Ok(connected) => { // The tree never shows a key both connecting and open. self.sync_tree(); + let reopens = match claim.key() { + SessionKey::Saved(id) => Some(*id), + SessionKey::SavedIn { .. } | SessionKey::File(_) => None, + }; self.adopt(claim, source, connected); + for (id, database) in databases.into_iter().filter_map(|database| Some((reopens?, database))) { + self.open_database(id, database); + } } Err(AttemptError::Failed(failure)) => self.on_connect_failed(claim, source, failure), // A location question was asked above, so this is a stop. @@ -759,7 +775,7 @@ impl SessionWindow { warnings, session_only, } = connected; - let (Some(state), Some(guards), Some(input)) = (self.state(), self.close_guards(), self.input()) else { + let (Some(state), Some(_), Some(input)) = (self.state(), self.close_guards(), self.input()) else { self.discard_pending(pending); return; }; @@ -777,21 +793,14 @@ impl SessionWindow { tracing::warn!(%error, "could not remember the connect dialog driver"); } let shell = self.shell().unwrap_or_else(|| self.new_shell(state, &input)); - let workspace = Workspace::new(WorkspaceInit { - key: key.clone(), - session: session.clone(), - services: state.services.clone(), - context: state.context.clone(), - window: self.clone().upcast(), - close_guards: guards.clone(), - shell: shell.clone(), - }); - self.connect_workspace(&workspace, &input); - self.imp().sessions.borrow_mut().push(OpenSession { - claim, - session, - workspace: workspace.clone(), - }); + let Some(workspace) = self.new_workspace(key.clone(), &session, &shell) else { + drop(session.begin_shutdown()); + return; + }; + self.imp() + .sessions + .borrow_mut() + .push(OpenSession::new(claim, session, workspace.clone())); self.render(); shell.add_session(&workspace); workspace.start(); @@ -804,6 +813,25 @@ impl SessionWindow { } } + /// The workspace that shows `session` in `shell`, wired to the + /// window. + fn new_workspace(&self, key: SessionKey, session: &Arc, shell: &WorkspaceShell) -> Option { + let (Some(state), Some(guards), Some(input)) = (self.state(), self.close_guards(), self.input()) else { + return None; + }; + let workspace = Workspace::new(WorkspaceInit { + key, + session: session.clone(), + services: state.services.clone(), + context: state.context.clone(), + window: self.clone().upcast(), + close_guards: guards.clone(), + shell: shell.clone(), + }); + self.connect_workspace(&workspace, &input); + Some(workspace) + } + fn connect_workspace(&self, workspace: &Workspace, input: &InputSender) { let dirty = input.clone(); workspace.connect_dirty_changed(move |_, _| dirty.deliver(SessionWindowInput::DirtyChanged)); @@ -1033,6 +1061,7 @@ impl SessionWindow { source, cancel, demand, + databases: Vec::new(), }) } @@ -1262,22 +1291,21 @@ impl SessionWindow { } /// Close the session under `key` through its own prompts, then tear - /// it down. The window's other sessions and connects go on. + /// it down. A connection's other databases go with it, under one + /// question for all of them. The window's other sessions and connects + /// go on. fn on_disconnect(&self, key: SessionKey) { - let Some(workspace) = self.session_workspace(&key) else { - return; - }; - let (Some(handle), Some(guards)) = (workspace.prompt_handle(), self.close_guards()) else { + let (Some(handles), Some(guards)) = (self.teardown_handles(&key), self.close_guards()) else { return; }; let approved = move || SessionWindowInput::SessionTeardownApproved(key.clone()); guards.begin_teardown(); - if guards.may_tear_down(&handle) { + if guards.may_tear_down(&handles) { self.send(approved()); return; } let input = self.input(); - guards.ask_tear_down(self.upcast_ref(), &handle, move |decision| { + guards.ask_tear_down(self.upcast_ref(), &handles, move |decision| { if decision == CloseDecision::Approved && let Some(input) = input { @@ -1286,6 +1314,18 @@ impl SessionWindow { }); } + /// The prompt handles of the session under `key` and of the other + /// databases open beside it, which a teardown of it takes too. + fn teardown_handles(&self, key: &SessionKey) -> Option> { + self.find_open(|open| { + (open.claim.key() == key).then(|| { + open.with_databases() + .filter_map(|open| open.workspace.prompt_handle()) + .collect() + }) + }) + } + /// Ctrl+W: the tab on screen closes through its session's prompts, /// and with no tab on screen the window closes through its own. fn on_close_current(&self) { diff --git a/linux/crates/app/src/ui/session_window/other_databases.rs b/linux/crates/app/src/ui/session_window/other_databases.rs new file mode 100644 index 0000000000..8d41ac01fc --- /dev/null +++ b/linux/crates/app/src/ui/session_window/other_databases.rs @@ -0,0 +1,311 @@ +//! A saved connection's other databases, open beside it in the window +//! that holds it ([0030]). The connection's open session owns them: its +//! teardown takes them, and a database still opening holds its claim +//! there, so none outlives the connection or keeps its key claimed. +//! +//! [0030]: ../../../../../docs/decisions/0030-a-database-is-a-session-of-its-own.md + +use std::sync::Arc; + +use gtk4::prelude::*; +use gtk4::subclass::prelude::*; +use gtk4::{self as gtk, glib}; +use libadwaita as adw; +use libadwaita::prelude::*; +use tablepro_core::meta::SchemaInfo; +use tablepro_session::operation::OperationKind; +use tablepro_session::{ConnectError, Session, SessionClaim, SessionKey}; +use tokio_util::sync::{CancellationToken, DropGuard}; +use uuid::Uuid; + +use super::SessionWindow; +use super::session_page::OpenSession; +use crate::i18n::{gettext, gettext_f}; +use crate::ui::error_text::{connect_error_message, operation_error_message}; + +/// The response of the chooser that opens the database picked. +const OPEN: &str = "open"; + +#[derive(Default)] +pub(crate) struct OtherDatabases { + pub(super) open: Vec, + opening: Vec, +} + +/// A database still opening. Dropping it stops the open and lets its +/// key go. +struct Opening { + database: String, + claim: SessionClaim, + _stop: DropGuard, +} + +impl OtherDatabases { + /// The databases open or opening, by name. + pub(super) fn names(&self) -> Vec { + let open = self.open.iter().filter_map(|open| match open.claim.key() { + SessionKey::SavedIn { database, .. } => Some(database.clone()), + SessionKey::Saved(_) | SessionKey::File(_) => None, + }); + open.chain(self.opening.iter().map(|opening| opening.database.clone())) + .collect() + } + + /// Take the database open under `key` out of the connection. + pub(super) fn take(&mut self, key: &SessionKey) -> Option { + let position = self.open.iter().position(|open| open.claim.key() == key)?; + Some(self.open.remove(position)) + } + + /// Stop every open still running and hand back the databases open. + pub(super) fn close(self) -> Vec { + drop(self.opening); + self.open + } + + fn take_opening(&mut self, database: &str) -> Option { + let position = self.opening.iter().position(|opening| opening.database == database)?; + Some(self.opening.remove(position).claim) + } +} + +/// The databases to offer: neither the connection's own nor one open +/// already, the user's first and the server's own after, as the editor +/// bar's menu lists them. +pub(super) fn offered(databases: &[SchemaInfo], open: &[String]) -> Vec { + let mut offered: Vec<&SchemaInfo> = databases + .iter() + .filter(|database| !database.is_default && !open.contains(&database.name)) + .collect(); + offered.sort_by_key(|database| database.is_system); + offered.into_iter().map(|database| database.name.clone()).collect() +} + +impl SessionWindow { + /// Open the connection `id`'s database `database` beside it, with + /// the stored secret and the connection's Safe Mode level. One open + /// or opening already is left as it is. + pub(crate) fn open_database(&self, id: Uuid, database: String) { + let connection = SessionKey::Saved(id); + let Some(state) = self.state() else { + return; + }; + let origin = self.find_open(|open| { + let wanted = open.claim.key() == &connection + && open.session.database() != Some(database.as_str()) + && !open.databases.names().contains(&database); + wanted.then(|| open.session.clone()) + }); + let Some(origin) = origin else { + return; + }; + let Some(claim) = self.claim(SessionKey::SavedIn { + id, + database: database.clone(), + }) else { + return; + }; + let stop = CancellationToken::new(); + { + let mut sessions = self.imp().sessions.borrow_mut(); + let Some(open) = sessions.iter_mut().find(|open| open.claim.key() == &connection) else { + return; + }; + open.databases.opening.push(Opening { + database: database.clone(), + claim, + _stop: stop.clone().drop_guard(), + }); + } + let opening = { + let (database, origin) = (database.clone(), origin.clone()); + state + .services + .tasks + .spawn_task(async move { origin.open_in_database(&database, stop).await }) + }; + let window = self.downgrade(); + glib::spawn_future_local(async move { + let opened = opening.await; + match window.upgrade() { + Some(window) => window.on_database_opened(id, &origin, database, opened), + None => { + if let Ok(Ok(session)) = opened { + drop(session.begin_shutdown()); + } + } + } + }); + } + + /// The claim is still the connection's only while the session that + /// asked holds it: a teardown, or a reconnect that replaced it, + /// dropped the open with it. + fn on_database_opened( + &self, + id: Uuid, + origin: &Arc, + database: String, + opened: Result, ConnectError>, tablepro_session::runtime::TaskFailure>, + ) { + let claim = self + .imp() + .sessions + .borrow_mut() + .iter_mut() + .find(|open| open.claim.key() == &SessionKey::Saved(id) && Arc::ptr_eq(&open.session, origin)) + .and_then(|open| open.databases.take_opening(&database)); + match (claim, opened) { + (Some(claim), Ok(Ok(session))) => { + // A level picked once the open had read the connection's + // went only to the databases already open. + session.set_safe_mode(origin.safe_mode()); + self.join_database(id, claim, session); + } + (None, Ok(Ok(session))) => drop(session.begin_shutdown()), + (Some(_), Ok(Err(error))) if !matches!(error, ConnectError::Cancelled) => { + self.show_toast(&gettext_f( + "Could not open the database “{database}”: {error}", + &[("database", &database), ("error", &connect_error_message(&error))], + )); + } + (_, Ok(Err(_))) => {} + (_, Err(failure)) => tracing::warn!(%failure, "opening another database failed"), + } + } + + /// Build the database's workspace in the shell and give it to the + /// connection's open session. + fn join_database(&self, id: Uuid, claim: SessionClaim, session: Arc) { + let Some(shell) = self.shell() else { + drop(session.begin_shutdown()); + return; + }; + claim.attach(&session); + let Some(workspace) = self.new_workspace(claim.key().clone(), &session, &shell) else { + drop(session.begin_shutdown()); + return; + }; + { + let mut sessions = self.imp().sessions.borrow_mut(); + let Some(connection) = sessions + .iter_mut() + .find(|open| open.claim.key() == &SessionKey::Saved(id)) + else { + workspace.shutdown(true); + drop(session.begin_shutdown()); + return; + }; + connection + .databases + .open + .push(OpenSession::new(claim, session, workspace.clone())); + } + self.render(); + shell.add_session(&workspace); + workspace.start(); + self.sync_actions(); + } + + /// Open Database…: list the server's databases, then ask which to + /// open beside the connection under `key`. + pub(super) fn choose_database_in(&self, key: SessionKey) { + let SessionKey::Saved(id) = key else { + return; + }; + let (Some(session), Some(state)) = (self.held_session(&key), self.state()) else { + return; + }; + let listing = state.services.tasks.spawn_task(async move { + session + .run(OperationKind::Metadata, |lease, options| async move { + Ok(lease.list_databases(options).await?) + }) + .await + }); + let window = self.downgrade(); + glib::spawn_future_local(async move { + let listed = listing.await; + let Some(window) = window.upgrade() else { + return; + }; + match listed { + Ok(Ok(databases)) => window.ask_database(id, &databases), + Ok(Err(error)) => window.show_toast(&operation_error_message(&error)), + Err(failure) => tracing::warn!(%failure, "listing the server's databases failed"), + } + }); + } + + fn ask_database(&self, id: Uuid, databases: &[SchemaInfo]) { + let open = self + .find_open(|open| (open.claim.key() == &SessionKey::Saved(id)).then(|| open.databases.names())) + .unwrap_or_default(); + let names = offered(databases, &open); + if names.is_empty() { + self.show_toast(&gettext("No other database of this server to open")); + return; + } + let strings: Vec<&str> = names.iter().map(String::as_str).collect(); + let choice = gtk::DropDown::from_strings(&strings); + choice.update_property(&[gtk::accessible::Property::Label(&gettext("Database"))]); + let dialog = adw::AlertDialog::builder() + .heading(gettext("Open Database")) + .body(gettext( + "It opens beside the connection, with tables and tabs of its own.", + )) + .extra_child(&choice) + .default_response(OPEN) + .close_response("cancel") + .build(); + dialog.add_responses(&[("cancel", &gettext("Cancel")), (OPEN, &gettext("Open"))]); + dialog.set_response_appearance(OPEN, adw::ResponseAppearance::Suggested); + let window = self.downgrade(); + dialog.connect_response(None, move |_, response| { + let picked = usize::try_from(choice.selected()) + .ok() + .and_then(|at| names.get(at)) + .cloned(); + if let (OPEN, Some(window), Some(database)) = (response, window.upgrade(), picked) { + window.open_database(id, database); + } + }); + dialog.present(Some(self)); + } + + /// The session the window holds under `key`. + pub(super) fn held_session(&self, key: &SessionKey) -> Option> { + self.find_open(|open| (open.claim.key() == key).then(|| open.session.clone())) + } +} + +#[cfg(test)] +mod tests { + use tablepro_core::meta::SchemaInfo; + + use super::offered; + + fn database(name: &str, is_default: bool, is_system: bool) -> SchemaInfo { + SchemaInfo { + name: name.to_owned(), + is_default, + is_system, + } + } + + #[test] + fn the_chooser_offers_the_other_databases_the_users_first() { + let databases = [ + database("mysql", false, true), + database("shop", true, false), + database("analytics", false, false), + database("archive", false, false), + ]; + + assert_eq!( + offered(&databases, &["archive".to_owned()]), + ["analytics", "mysql"], + "neither the connection's own nor one open already" + ); + } +} diff --git a/linux/crates/app/src/ui/session_window/session_page.rs b/linux/crates/app/src/ui/session_window/session_page.rs index 9ac88abb35..3c9e9d3060 100644 --- a/linux/crates/app/src/ui/session_window/session_page.rs +++ b/linux/crates/app/src/ui/session_window/session_page.rs @@ -4,6 +4,7 @@ use tablepro_session::{ConnectSource, Session, SessionClaim, SessionKey}; use tablepro_storage::CredentialDemand; use tokio_util::sync::CancellationToken; +use super::other_databases::OtherDatabases; use super::session_window_command::ConnectFailure; use crate::ui::workspace::Workspace; @@ -13,6 +14,31 @@ pub(crate) struct OpenSession { pub(crate) claim: SessionClaim, pub(crate) session: Arc, pub(crate) workspace: Workspace, + /// A saved connection's other databases, which go with it (0030). + pub(crate) databases: OtherDatabases, +} + +impl OpenSession { + pub(crate) fn new(claim: SessionClaim, session: Arc, workspace: Workspace) -> Self { + Self { + claim, + session, + workspace, + databases: OtherDatabases::default(), + } + } + + /// This session, then the other databases open beside it. + pub(crate) fn with_databases(&self) -> impl Iterator { + std::iter::once(self).chain(self.databases.open.iter()) + } + + /// Write what this session and its other databases show. + pub(crate) fn flush(&self) { + for open in self.with_databases() { + open.workspace.flush(); + } + } } /// A connect the window runs, its first one included. Its result @@ -26,6 +52,9 @@ pub(crate) struct PendingConnect { /// What the connect asks beyond the stored secrets, kept for the /// connect that follows the question about its file. pub(crate) demand: CredentialDemand, + /// The connection's other databases a reconnect opens again once it + /// joins. + pub(crate) databases: Vec, } /// A connect that failed while the window had nothing else to show, diff --git a/linux/crates/app/src/ui/session_window/session_restore.rs b/linux/crates/app/src/ui/session_window/session_restore.rs index a762f94bc1..f977256ef2 100644 --- a/linux/crates/app/src/ui/session_window/session_restore.rs +++ b/linux/crates/app/src/ui/session_window/session_restore.rs @@ -22,6 +22,8 @@ impl SessionWindow { return; } SessionKey::Saved(id) => id, + // Never recorded: another database opens from its connection. + SessionKey::SavedIn { .. } => return, }; let (Some(state), Some(input)) = (self.state(), self.input()) else { return; @@ -76,6 +78,7 @@ impl SessionWindow { let restorable = match key { SessionKey::File(_) => true, SessionKey::Saved(_) => saved.is_some(), + SessionKey::SavedIn { .. } => false, }; if restorable && let Some(state) = self.state() { state.services.open_sessions().add(key.clone(), self.id()); diff --git a/linux/crates/app/src/ui/session_window/session_teardown.rs b/linux/crates/app/src/ui/session_window/session_teardown.rs index b6bb35396c..af4cf66278 100644 --- a/linux/crates/app/src/ui/session_window/session_teardown.rs +++ b/linux/crates/app/src/ui/session_window/session_teardown.rs @@ -39,7 +39,7 @@ impl SessionWindow { return; } for open in &sessions { - open.workspace.flush(); + open.flush(); } self.set_content(gtk::Widget::NONE); imp.page_toasts.replace(None); @@ -57,8 +57,9 @@ impl SessionWindow { } /// Take down the one session under `key` while the window keeps the - /// others and its connects. With the last one the shell goes too. - /// False when the window holds no such session. + /// others and its connects. A connection's other databases go with + /// it. With the last one the shell goes too. False when the window + /// holds no such session. pub(super) fn tear_down_session(&self, key: &SessionKey) -> bool { let imp = self.imp(); let open = { @@ -66,12 +67,15 @@ impl SessionWindow { tracing::warn!("a session was torn down while the sessions were read"); return false; }; - let Some(position) = sessions.iter().position(|open| open.claim.key() == key) else { - return false; - }; - sessions.remove(position) + match sessions.iter().position(|open| open.claim.key() == key) { + Some(position) => Some(sessions.remove(position)), + None => sessions.iter_mut().find_map(|open| open.databases.take(key)), + } }; - open.workspace.flush(); + let Some(open) = open else { + return false; + }; + open.flush(); let shell = self.shell(); self.close_open_session(shell.as_ref(), open, true); if imp.sessions.borrow().is_empty() { @@ -87,9 +91,10 @@ impl SessionWindow { true } - /// Take `open`'s workspace off the shell when one is given, shut it - /// down, drop its claim and record, and close its session. Its pages - /// leave the tab view when `close_pages`. + /// Close the other databases open beside `open` and stop those still + /// opening, then take `open`'s workspace off the shell when one is + /// given, shut it down, drop its claim and record, and close its + /// session. Its pages leave the tab view when `close_pages`. fn close_open_session(&self, shell: Option<&WorkspaceShell>, open: OpenSession, close_pages: bool) { let Some(state) = self.state() else { return; @@ -98,7 +103,11 @@ impl SessionWindow { claim, session, workspace, + databases, } = open; + for database in databases.close() { + self.close_open_session(shell, database, close_pages); + } let editor_sessions = workspace.take_editor_sessions(); let key = claim.key().clone(); if let Some(shell) = shell { diff --git a/linux/crates/app/src/ui/session_window/session_window_input.rs b/linux/crates/app/src/ui/session_window/session_window_input.rs index debea7282d..b592a9d193 100644 --- a/linux/crates/app/src/ui/session_window/session_window_input.rs +++ b/linux/crates/app/src/ui/session_window/session_window_input.rs @@ -69,6 +69,9 @@ pub(crate) enum SessionWindowInput { Disconnect, /// Close the session open under the key through its prompts. DisconnectSession(SessionKey), + /// Open Database… on a connection's row: pick another of its server's + /// databases to open beside it. + ChooseDatabaseIn(SessionKey), /// The prompts approved closing the session under the key. SessionTeardownApproved(SessionKey), /// Bring the session under the key to the front. diff --git a/linux/crates/app/src/ui/session_window/tests/mod.rs b/linux/crates/app/src/ui/session_window/tests/mod.rs index 5242097aef..c57c46380f 100644 --- a/linux/crates/app/src/ui/session_window/tests/mod.rs +++ b/linux/crates/app/src/ui/session_window/tests/mod.rs @@ -1,5 +1,6 @@ mod notification; mod open_file; +mod other_databases; mod sessions; use std::rc::Rc; diff --git a/linux/crates/app/src/ui/session_window/tests/other_databases.rs b/linux/crates/app/src/ui/session_window/tests/other_databases.rs new file mode 100644 index 0000000000..187f8e8ff2 --- /dev/null +++ b/linux/crates/app/src/ui/session_window/tests/other_databases.rs @@ -0,0 +1,184 @@ +//! A connection's other databases open beside it and live and go with +//! its session (0030). + +use gtk4::prelude::*; +use tablepro_session::SessionKey; +use tablepro_session::fakes::FakeEstablish; +use tablepro_test_support::{ConnectionScript, FakeReply}; + +use super::{Script, TestWindow, WAIT, pick_level}; +use crate::test_support::{drain_main_context, wait_until}; +use crate::ui::SessionWindowInput; + +const ANALYTICS: &str = "analytics"; + +/// A server connection whose password is stored, which another of its +/// databases opens with, unasked. `establish` is what each connect gets +/// before the test's connection from then on. +fn network_window(name: &str, establish: Vec) -> TestWindow { + let test = TestWindow::with_script( + name, + Script { + network: true, + establish, + ..Script::default() + }, + ); + let (vault, id) = (test.vault.clone(), test.saved.id); + crate::test_support::finish_on_main(WAIT, async move { + tablepro_core::credentials::SecretVault::store( + &*vault, + id, + tablepro_core::credentials::SecretKind::DatabasePassword, + &secrecy::SecretString::from("s3cret"), + "label", + ) + .await + }) + .expect("the store to end") + .expect("the password stored"); + test +} + +fn connect(test: &TestWindow) { + test.window + .send(SessionWindowInput::OpenSaved(Box::new(test.saved.clone()))); + test.wait_for_page("workspace"); +} + +fn analytics(test: &TestWindow) -> SessionKey { + SessionKey::SavedIn { + id: test.saved.id, + database: ANALYTICS.to_owned(), + } +} + +/// The connection connects; another of its databases never answers +/// until its open is stopped. +fn opening_never_ends() -> Vec { + let mut connection = ConnectionScript::default(); + connection.probe.always(FakeReply::ok(())); + connection.list_schemas.always(FakeReply::ok(Vec::new())); + connection.list_objects.always(FakeReply::ok(Vec::new())); + vec![FakeEstablish::Connect(connection), FakeEstablish::UntilCancelled] +} + +#[gtk4::test] +fn another_database_opens_beside_the_connection_and_goes_with_it() { + let test = network_window("OtherDatabase", Vec::new()); + connect(&test); + let sibling = analytics(&test); + + test.window.open_database(test.saved.id, ANALYTICS.to_owned()); + + wait_until(WAIT, || test.window.held_session(&sibling).is_some()).expect("the database to open"); + let session = test.window.held_session(&sibling).expect("the database's session"); + assert_eq!(session.database(), Some(ANALYTICS)); + let calls = test.connector.calls(); + assert!( + !calls.last().expect("a connect for the database").attended, + "opening the database asked for a secret" + ); + assert_eq!(test.window.workspaces().len(), 2, "the database has no workspace"); + + test.window.open_database(test.saved.id, ANALYTICS.to_owned()); + drain_main_context(); + assert_eq!( + test.connector.calls().len(), + calls.len(), + "the open database opened again" + ); + + test.window.send(SessionWindowInput::DisconnectSession(test.key())); + wait_until(WAIT, || test.window.held_session(&sibling).is_none()).expect("the database to go with it"); + assert_eq!(test.services.sessions.window_for(&sibling), None); +} + +/// A database still opening when its connection disconnects stops, and +/// its key is free for the next connect to open it again. +#[gtk4::test] +fn a_database_still_opening_goes_with_its_connection() { + let test = network_window("OtherDatabaseOpening", opening_never_ends()); + connect(&test); + let sibling = analytics(&test); + test.window.open_database(test.saved.id, ANALYTICS.to_owned()); + wait_until(WAIT, || test.connector.calls().len() == 2).expect("the database to start opening"); + assert_eq!( + test.services.sessions.window_for(&sibling), + Some(test.window.id()), + "the opening holds no claim" + ); + + test.window.send(SessionWindowInput::DisconnectSession(test.key())); + + wait_until(WAIT, || test.services.sessions.window_for(&sibling).is_none()).expect("the opening to let its key go"); + connect(&test); + test.window.open_database(test.saved.id, ANALYTICS.to_owned()); + wait_until(WAIT, || test.window.held_session(&sibling).is_some()).expect("the database to open again"); +} + +/// Closing the window stops a database still opening and lets its key +/// go. +#[gtk4::test] +fn a_database_still_opening_goes_with_the_window() { + let test = network_window("OtherDatabaseWindowClosed", opening_never_ends()); + connect(&test); + let sibling = analytics(&test); + test.window.open_database(test.saved.id, ANALYTICS.to_owned()); + wait_until(WAIT, || test.connector.calls().len() == 2).expect("the database to start opening"); + + test.window.close(); + + wait_until(WAIT, || test.services.sessions.window_for(&sibling).is_none()).expect("the opening to let its key go"); +} + +/// A level on the other side of Read-Only reconnects the connection, and +/// its other databases open again beside it at that level. +#[gtk4::test] +fn read_only_reopens_the_other_databases_at_the_new_level() { + let test = network_window("OtherDatabaseReadOnly", Vec::new()); + connect(&test); + let sibling = analytics(&test); + test.window.open_database(test.saved.id, ANALYTICS.to_owned()); + wait_until(WAIT, || test.window.held_session(&sibling).is_some()).expect("the database to open"); + let before = test.window.held_session(&sibling).expect("the database's session"); + + pick_level(&test, "read-only"); + + wait_until(WAIT, || { + test.window + .held_session(&sibling) + .is_some_and(|session| !std::sync::Arc::ptr_eq(&session, &before)) + }) + .expect("the database to open again"); + let reopened = test.window.held_session(&sibling).expect("the database's session"); + assert_eq!(reopened.safe_mode(), tablepro_core::SafeModeLevel::ReadOnly); + let request = test.connector.calls().pop().expect("the database's connect").request; + assert!(request.workspace_settings.read_only); +} + +/// A level picked while one of the connection's other databases is in +/// front is the connection's, and moves every one of its sessions. +#[gtk4::test] +fn a_level_picked_in_another_database_moves_the_connection() { + let test = network_window("OtherDatabaseLevel", Vec::new()); + connect(&test); + let sibling = analytics(&test); + test.window.open_database(test.saved.id, ANALYTICS.to_owned()); + wait_until(WAIT, || test.window.held_session(&sibling).is_some()).expect("the database to open"); + + test.window.send(SessionWindowInput::SetSafeMode { + key: sibling.clone(), + level: tablepro_core::SafeModeLevel::ConfirmWrites, + }); + + for key in [test.key(), sibling] { + wait_until(WAIT, || { + test.window + .held_session(&key) + .is_some_and(|session| session.safe_mode() == tablepro_core::SafeModeLevel::ConfirmWrites) + }) + .unwrap_or_else(|_| panic!("{key:?} kept its level")); + } + assert_eq!(test.connector.calls().len(), 2, "a level that writes reconnected"); +} diff --git a/linux/crates/app/src/ui/session_window/window_actions.rs b/linux/crates/app/src/ui/session_window/window_actions.rs index e8f47f3a36..e9c02cf709 100644 --- a/linux/crates/app/src/ui/session_window/window_actions.rs +++ b/linux/crates/app/src/ui/session_window/window_actions.rs @@ -44,12 +44,13 @@ const ACTIONS: [(&str, ActionInput); 11] = [ /// The actions a connection tree row or a toast fires for its own /// session, which take the session as their target (`session_target`). -const SESSION_ACTIONS: [(&str, SessionInput); 7] = [ +const SESSION_ACTIONS: [(&str, SessionInput); 8] = [ ("show-session", SessionWindowInput::ShowSession), ("disconnect-session", SessionWindowInput::DisconnectSession), ("stop-connecting", SessionWindowInput::StopConnecting), ("new-query-in", SessionWindowInput::NewQueryIn), ("refresh-in", SessionWindowInput::RefreshIn), + ("open-database-in", SessionWindowInput::ChooseDatabaseIn), ("show-connect-failure", SessionWindowInput::ShowConnectFailure), ("forget-connect-failure", SessionWindowInput::ForgetConnectFailure), ]; diff --git a/linux/crates/app/src/ui/workspace/assistant.rs b/linux/crates/app/src/ui/workspace/assistant.rs index 9bbcc91590..8e19ce75de 100644 --- a/linux/crates/app/src/ui/workspace/assistant.rs +++ b/linux/crates/app/src/ui/workspace/assistant.rs @@ -40,7 +40,9 @@ pub(super) fn access( let saved = services.saved_connections.clone(); let key = key.clone(); AssistantAccess::new(services.ai.clone(), session, engine, move || match &key { - SessionKey::Saved(id) => saved.get(*id).map_or_else(AiPolicy::default, |saved| saved.ai_policy), + SessionKey::Saved(id) | SessionKey::SavedIn { id, .. } => { + saved.get(*id).map_or_else(AiPolicy::default, |saved| saved.ai_policy) + } SessionKey::File(_) => AiPolicy::default(), }) } diff --git a/linux/crates/app/src/ui/workspace/mod.rs b/linux/crates/app/src/ui/workspace/mod.rs index b2fcf23602..5ecb86f536 100644 --- a/linux/crates/app/src/ui/workspace/mod.rs +++ b/linux/crates/app/src/ui/workspace/mod.rs @@ -1109,7 +1109,9 @@ impl WorkspaceModel { workspace.notify_connection_name(); } let icon = match &self.key { - SessionKey::Saved(id) => self.saved_connections.get(*id).and_then(|saved| saved.icon), + SessionKey::Saved(id) | SessionKey::SavedIn { id, .. } => { + self.saved_connections.get(*id).and_then(|saved| saved.icon) + } SessionKey::File(_) => None, } .map(|icon| crate::ui::icons::connection_icon(icon).to_owned()); diff --git a/linux/crates/app/src/ui/workspace/sidebar/connection_tree.rs b/linux/crates/app/src/ui/workspace/sidebar/connection_tree.rs index e761590ba1..2fcb87ddc3 100644 --- a/linux/crates/app/src/ui/workspace/sidebar/connection_tree.rs +++ b/linux/crates/app/src/ui/workspace/sidebar/connection_tree.rs @@ -425,10 +425,28 @@ impl TreeInner { fn saved_name(&self, key: &SessionKey) -> Option { match key { SessionKey::Saved(id) => self.saved.get(*id).map(|saved| saved.name), + SessionKey::SavedIn { id, database } => { + self.saved.get(*id).map(|saved| sibling_title(&saved.name, database)) + } SessionKey::File(_) => None, } } + /// The open databases of the connection `id` beside its own, by name. + fn siblings_of(&self, id: uuid::Uuid) -> Vec<(SessionKey, String)> { + let mut siblings: Vec<(SessionKey, String)> = self + .branches + .borrow() + .keys() + .filter_map(|key| match key { + SessionKey::SavedIn { id: of, database } if *of == id => Some((key.clone(), database.clone())), + _ => None, + }) + .collect(); + siblings.sort_by_cached_key(|(_, database)| (database.to_lowercase(), database.clone())); + siblings + } + /// The node of the connection under `key`, made under `title` for a /// key the tree has not seen. fn node_for(&self, key: &SessionKey, title: &str) -> TreeNode { @@ -485,11 +503,25 @@ impl TreeInner { saved.color.map_or("", crate::ui::connection_appearance::color_class), ); kept.insert(key); + // The connection's other open databases come right after it, + // in its group, by name ([0030]). + let mut rows = vec![node]; + for (sibling, database) in self.siblings_of(saved.id) { + let title = sibling_title(&saved.name, &database); + let node = self.node_for(&sibling, &title); + node.update( + &title, + crate::ui::icons::DRIVE_HARDDISK, + saved.color.map_or("", crate::ui::connection_appearance::color_class), + ); + kept.insert(sibling); + rows.push(node); + } match &saved.group { - None => ungrouped.push(node), + None => ungrouped.extend(rows), Some(group) => match grouped.last_mut() { - Some((name, members)) if *name == group.as_str() => members.push(node), - _ => grouped.push((group.as_str().to_owned(), vec![node])), + Some((name, members)) if *name == group.as_str() => members.extend(rows), + _ => grouped.push((group.as_str().to_owned(), rows)), }, } } @@ -787,7 +819,7 @@ impl TreeInner { Some(saved) => search.matches(&saved, &self.saved.tags()), None => search.matches_name(&node.title()), }, - SessionKey::File(_) => search.matches_name(&node.title()), + SessionKey::SavedIn { .. } | SessionKey::File(_) => search.matches_name(&node.title()), } } @@ -1076,7 +1108,8 @@ impl TreeInner { fn stored(&self, node: &TreeNode) -> Option { match node.key()? { SessionKey::Saved(id) => self.saved.get(id).map(|_| id), - SessionKey::File(_) => None, + // A database's row is not the connection's: it has no editing. + SessionKey::SavedIn { .. } | SessionKey::File(_) => None, } } @@ -1094,10 +1127,18 @@ impl TreeInner { stored: self.stored(node).is_some(), } }; - let lead = session_section(&key, state); + // A saved connection's own row, on an engine whose catalog holds + // one database (0030). + let opens_databases = matches!(key, SessionKey::Saved(_)) + && self + .branches + .borrow() + .get(&key) + .is_some_and(|branch| branch.view.affordances().opens_databases); + let lead = session_section(&key, state, opens_databases); let saved = match &key { SessionKey::Saved(id) => self.saved.get(*id), - SessionKey::File(_) => None, + SessionKey::SavedIn { .. } | SessionKey::File(_) => None, }; let Some(saved) = saved else { if lead.n_items() == 0 { @@ -1505,3 +1546,12 @@ fn index_in(model: &impl IsA, object: &impl IsA) - let object = object.upcast_ref::(); (0..model.n_items()).find(|&at| model.item(at).as_ref() == Some(object)) } + +/// A sibling database's row: its connection and the database, apart. +fn sibling_title(connection: &str, database: &str) -> String { + // Translators: a connection and another of its server's databases, open beside it. + crate::i18n::gettext_f( + "{connection} · {database}", + &[("connection", connection), ("database", database)], + ) +} diff --git a/linux/crates/app/src/ui/workspace/sidebar/sidebar_affordances.rs b/linux/crates/app/src/ui/workspace/sidebar/sidebar_affordances.rs index f0add5c9d1..c3b26f3d27 100644 --- a/linux/crates/app/src/ui/workspace/sidebar/sidebar_affordances.rs +++ b/linux/crates/app/src/ui/workspace/sidebar/sidebar_affordances.rs @@ -1,4 +1,4 @@ -use tablepro_core::dialect::SqlDialect; +use tablepro_core::dialect::{DatabaseLayout, SqlDialect}; use tablepro_session::Session; /// What the sidebar offers for the connection: the structure tab's @@ -12,6 +12,9 @@ pub(crate) struct SidebarAffordances { pub can_create_table: bool, pub can_edit_structure: bool, pub speaks_sql: bool, + /// Another of the server's databases opens beside the connection, + /// where a connection's catalog describes only its own (0030). + pub opens_databases: bool, } impl SidebarAffordances { @@ -22,6 +25,7 @@ impl SidebarAffordances { can_create_table: ddl.is_some_and(|ddl| ddl.capabilities().create_table), can_edit_structure: ddl.is_some(), speaks_sql: dialect.is_some_and(|dialect| dialect.grammar().is_sql()), + opens_databases: dialect.is_some_and(|dialect| dialect.database_layout() == DatabaseLayout::WithSchemas), } } @@ -57,6 +61,8 @@ mod tests { assert!(!none.can_edit_structure && !none.offers_new_table()); assert!(creates.speaks_sql); + assert!(creates.opens_databases, "PostgreSQL lists one database's schemas"); + assert!(!edits_only.opens_databases, "ClickHouse lists every database already"); let mongo = SidebarAffordances::of(false, Some(&tablepro_driver_mongodb::MongoDialect)); assert!( !mongo.can_edit_structure && !mongo.speaks_sql, diff --git a/linux/crates/app/src/ui/workspace/sidebar/sidebar_menu.rs b/linux/crates/app/src/ui/workspace/sidebar/sidebar_menu.rs index c6c9cf6479..048b0aabce 100644 --- a/linux/crates/app/src/ui/workspace/sidebar/sidebar_menu.rs +++ b/linux/crates/app/src/ui/workspace/sidebar/sidebar_menu.rs @@ -141,6 +141,7 @@ mod tests { can_create_table: true, can_edit_structure: true, speaks_sql: true, + opens_databases: true, } } @@ -210,6 +211,7 @@ mod tests { can_create_table: false, can_edit_structure: false, speaks_sql: false, + opens_databases: false, }; assert_eq!( diff --git a/linux/crates/app/src/ui/workspace/sidebar/tests.rs b/linux/crates/app/src/ui/workspace/sidebar/tests.rs index 6ce2dcabe0..e710518396 100644 --- a/linux/crates/app/src/ui/workspace/sidebar/tests.rs +++ b/linux/crates/app/src/ui/workspace/sidebar/tests.rs @@ -101,6 +101,7 @@ fn every_affordance() -> SidebarAffordances { can_create_table: true, can_edit_structure: true, speaks_sql: true, + opens_databases: true, } } @@ -1270,8 +1271,13 @@ fn the_connection_menu_holds_the_pickers_items() { ) ); assert_eq!( - open[..3], - ["win.new-query-in", "win.refresh-in", "win.disconnect-session"], + open[..4], + [ + "win.new-query-in", + "win.refresh-in", + "win.open-database-in", + "win.disconnect-session" + ], "{open:?}" ); assert!(!open.contains(&"picker.connect".to_owned())); @@ -1281,6 +1287,29 @@ fn the_connection_menu_holds_the_pickers_items() { assert!(state.is_ok(), "the menu's choices reach no action"); } +/// An engine whose connection already lists every database offers none +/// to open beside it. +#[gtk4::test] +fn a_connection_listing_every_database_offers_none_to_open() { + let alpha = connection("Alpha", None); + let shown = Shown::new(std::slice::from_ref(&alpha)); + let (view, _) = session( + &[object("public", "orders")], + SidebarAffordances { + opens_databases: false, + ..every_affordance() + }, + ); + shown.open(&alpha, &view); + let popover = shown.popover(); + + shown.menu("Alpha"); + + let open = menu_actions(&popover); + assert!(open.contains(&"win.new-query-in".to_owned()), "{open:?}"); + assert!(!open.contains(&"win.open-database-in".to_owned()), "{open:?}"); +} + #[gtk4::test] fn the_schema_menu_follows_the_dialect() { let alpha = connection("Alpha", None); diff --git a/linux/crates/app/src/ui/workspace/sidebar/tree_menu.rs b/linux/crates/app/src/ui/workspace/sidebar/tree_menu.rs index ee1cf92e37..34d4c8a2b0 100644 --- a/linux/crates/app/src/ui/workspace/sidebar/tree_menu.rs +++ b/linux/crates/app/src/ui/workspace/sidebar/tree_menu.rs @@ -6,6 +6,7 @@ use tablepro_session::SessionKey; pub(super) const SHOW_SESSION: &str = "win.show-session"; const NEW_QUERY_IN: &str = "win.new-query-in"; const REFRESH_IN: &str = "win.refresh-in"; +const OPEN_DATABASE_IN: &str = "win.open-database-in"; pub(super) const DISCONNECT_SESSION: &str = "win.disconnect-session"; const STOP_CONNECTING: &str = "win.stop-connecting"; pub(super) const SHOW_CONNECT_FAILURE: &str = "win.show-connect-failure"; @@ -27,7 +28,8 @@ pub(super) enum InWindow { /// The connection's menu section for the window: the session's own /// actions while it is open, Stop Connecting while it connects, else /// Connect for a saved connection, and the reason a connect failed. -pub(super) fn session_section(key: &SessionKey, state: InWindow) -> gio::Menu { +/// `opens_databases` offers Open Database… on an open one. +pub(super) fn session_section(key: &SessionKey, state: InWindow, opens_databases: bool) -> gio::Menu { let target = crate::ui::session_target::to_variant(key); let menu = gio::Menu::new(); let add = |label: String, action: &str| { @@ -39,6 +41,9 @@ pub(super) fn session_section(key: &SessionKey, state: InWindow) -> gio::Menu { InWindow::Open => { add(crate::i18n::gettext("New Query"), NEW_QUERY_IN); add(crate::i18n::gettext("Refresh"), REFRESH_IN); + if opens_databases { + add(crate::i18n::gettext("Open Database…"), OPEN_DATABASE_IN); + } add(crate::i18n::gettext("Disconnect"), DISCONNECT_SESSION); } // A connect can wait for a host that never answers. diff --git a/linux/crates/app/src/ui/workspace/tab_factory.rs b/linux/crates/app/src/ui/workspace/tab_factory.rs index 1fb9152af4..b6e2d8eefa 100644 --- a/linux/crates/app/src/ui/workspace/tab_factory.rs +++ b/linux/crates/app/src/ui/workspace/tab_factory.rs @@ -7,6 +7,7 @@ use gtk4::{gio, glib}; use libadwaita as adw; use libadwaita::prelude::*; use tablepro_core::meta::TableRef; +use tablepro_session::SessionKey; use tablepro_storage::{DraftId, DraftScope}; use uuid::Uuid; @@ -231,7 +232,14 @@ impl TabFactory { tracker: parts.change_trackers.register(id), schema: schema.clone(), table: name.clone(), - connection_id: parts.context.active.metadata().map(|metadata| metadata.id), + // Column widths and filters are kept per connection and table; + // another database's table of the same name is not that one. + connection_id: match parts.label.key() { + SessionKey::SavedIn { .. } => None, + SessionKey::Saved(_) | SessionKey::File(_) => { + parts.context.active.metadata().map(|metadata| metadata.id) + } + }, read_only: parts.read_only, page_size: page.page_size, initial_offset: page.offset, diff --git a/linux/crates/app/src/ui/workspace/tab_title.rs b/linux/crates/app/src/ui/workspace/tab_title.rs index 191d246902..370f859056 100644 --- a/linux/crates/app/src/ui/workspace/tab_title.rs +++ b/linux/crates/app/src/ui/workspace/tab_title.rs @@ -42,9 +42,20 @@ impl ConnectionLabel { } } + pub(crate) fn key(&self) -> &SessionKey { + &self.key + } + pub(crate) fn name(&self) -> String { let saved = match &self.key { SessionKey::Saved(id) => self.saved.get(*id).map(|saved| saved.name), + SessionKey::SavedIn { id, database } => self.saved.get(*id).map(|saved| { + // Translators: a connection and another of its server's databases, open beside it. + crate::i18n::gettext_f( + "{connection} · {database}", + &[("connection", &saved.name), ("database", database)], + ) + }), SessionKey::File(_) => None, }; saved diff --git a/linux/crates/app/src/workspace/close_guards.rs b/linux/crates/app/src/workspace/close_guards.rs index eae7124e5b..0addee6892 100644 --- a/linux/crates/app/src/workspace/close_guards.rs +++ b/linux/crates/app/src/workspace/close_guards.rs @@ -174,9 +174,13 @@ impl CloseGuards { self.teardown_acknowledged.borrow_mut().clear(); } - /// Whether every tab may go with the connection without asking. - pub(crate) fn may_tear_down(&self, handle: &WorkspaceHandle) -> bool { - handle.close_step(CloseScope::Window, &self.teardown_acknowledged.borrow()) == CloseStep::Approve + /// Whether every tab of `handles` may go with their connection + /// without asking. + pub(crate) fn may_tear_down(&self, handles: &[WorkspaceHandle]) -> bool { + let acknowledged = self.teardown_acknowledged.borrow(); + handles + .iter() + .all(|handle| handle.close_step(CloseScope::Window, &acknowledged) == CloseStep::Approve) } /// Ask whether every tab may go with the connection; `decided` hears @@ -184,19 +188,14 @@ impl CloseGuards { pub(crate) fn ask_tear_down( self: &Rc, parent: >k::Widget, - handle: &WorkspaceHandle, + handles: &[WorkspaceHandle], decided: impl FnOnce(CloseDecision) + 'static, ) { - let (guards, handle, parent) = (self.clone(), handle.clone(), parent.clone()); + let (guards, handles, parent) = (self.clone(), handles.to_vec(), parent.clone()); glib::spawn_future_local(async move { let mut acknowledged = guards.teardown_acknowledged.borrow().clone(); let decision = guards - .run( - &parent, - std::slice::from_ref(&handle), - CloseScope::Window, - &mut acknowledged, - ) + .run(&parent, &handles, CloseScope::Window, &mut acknowledged) .await; if decision == CloseDecision::Approved { guards.teardown_acknowledged.replace(acknowledged); @@ -465,21 +464,21 @@ mod tests { let guards = CloseGuards::new(); let window = window(); let parent = window.clone().upcast::(); - assert!(!guards.may_tear_down(&workspace.handle)); + assert!(!guards.may_tear_down(std::slice::from_ref(&workspace.handle))); let (kept, sink) = decision_sink(); - guards.ask_tear_down(&parent, &workspace.handle, sink); + guards.ask_tear_down(&parent, std::slice::from_ref(&workspace.handle), sink); let first = answer_next(&window, "Save Changes?", "cancel", None); wait_until(WAIT, || kept.get().is_some()).expect("a decision"); assert_eq!(kept.get(), Some(CloseDecision::Cancelled)); - assert!(!guards.may_tear_down(&workspace.handle)); + assert!(!guards.may_tear_down(std::slice::from_ref(&workspace.handle))); let (approved, sink) = decision_sink(); - guards.ask_tear_down(&parent, &workspace.handle, sink); + guards.ask_tear_down(&parent, std::slice::from_ref(&workspace.handle), sink); answer_next(&window, "Save Changes?", "discard", Some(&first)); wait_until(WAIT, || approved.get().is_some()).expect("a decision"); assert_eq!(approved.get(), Some(CloseDecision::Approved)); - assert!(guards.may_tear_down(&workspace.handle)); + assert!(guards.may_tear_down(std::slice::from_ref(&workspace.handle))); } /// A switch asks once before it connects and again before the new @@ -494,26 +493,26 @@ mod tests { guards.begin_teardown(); let (approved, sink) = decision_sink(); - guards.ask_tear_down(&parent, &workspace.handle, sink); + guards.ask_tear_down(&parent, std::slice::from_ref(&workspace.handle), sink); answer_next(&window, "Close With Open Transactions?", "close", None); let first = answer_next(&window, "Save Changes?", "discard", None); wait_until(WAIT, || approved.get().is_some()).expect("a decision"); - assert!(guards.may_tear_down(&workspace.handle)); + assert!(guards.may_tear_down(std::slice::from_ref(&workspace.handle))); edit_again(&workspace); assert!( - !guards.may_tear_down(&workspace.handle), + !guards.may_tear_down(std::slice::from_ref(&workspace.handle)), "an edit made while connecting went unasked" ); let (again, sink) = decision_sink(); - guards.ask_tear_down(&parent, &workspace.handle, sink); + guards.ask_tear_down(&parent, std::slice::from_ref(&workspace.handle), sink); answer_next(&window, "Save Changes?", "discard", Some(&first)); wait_until(WAIT, || again.get().is_some()).expect("a decision"); assert_eq!(again.get(), Some(CloseDecision::Approved)); guards.begin_teardown(); assert!( - !guards.may_tear_down(&workspace.handle), + !guards.may_tear_down(std::slice::from_ref(&workspace.handle)), "a new switch skipped the transaction" ); } diff --git a/linux/crates/app/src/workspace/session_key_record.rs b/linux/crates/app/src/workspace/session_key_record.rs index e9ccb5fcdb..6ed97082c1 100644 --- a/linux/crates/app/src/workspace/session_key_record.rs +++ b/linux/crates/app/src/workspace/session_key_record.rs @@ -7,6 +7,7 @@ use uuid::Uuid; #[serde(tag = "kind", rename_all = "snake_case")] pub enum SessionKeyRecord { Saved { id: Uuid }, + SavedIn { id: Uuid, database: String }, File { uri: String }, } @@ -14,6 +15,10 @@ impl From<&SessionKey> for SessionKeyRecord { fn from(key: &SessionKey) -> Self { match key { SessionKey::Saved(id) => Self::Saved { id: *id }, + SessionKey::SavedIn { id, database } => Self::SavedIn { + id: *id, + database: database.clone(), + }, SessionKey::File(uri) => Self::File { uri: uri.clone() }, } } @@ -23,6 +28,7 @@ impl SessionKeyRecord { pub fn into_key(self) -> SessionKey { match self { Self::Saved { id } => SessionKey::Saved(id), + Self::SavedIn { id, database } => SessionKey::SavedIn { id, database }, Self::File { uri } => SessionKey::File(uri), } } @@ -36,6 +42,10 @@ mod tests { fn each_key_comes_back_as_it_went_in() { for key in [ SessionKey::Saved(Uuid::new_v4()), + SessionKey::SavedIn { + id: Uuid::new_v4(), + database: "analytics".to_owned(), + }, SessionKey::File("file:///srv/app.db".to_owned()), ] { assert_eq!(SessionKeyRecord::from(&key).into_key(), key); diff --git a/linux/crates/app/src/workspace/workspace_file_name.rs b/linux/crates/app/src/workspace/workspace_file_name.rs index 79127d7327..c0d37a4ae4 100644 --- a/linux/crates/app/src/workspace/workspace_file_name.rs +++ b/linux/crates/app/src/workspace/workspace_file_name.rs @@ -15,6 +15,13 @@ impl WorkspaceFileName { pub fn for_key(key: &SessionKey) -> Self { let stem = match key { SessionKey::Saved(id) => id.to_string(), + // A database name can hold a slash, so it goes in as a digest. + SessionKey::SavedIn { id, database } => { + let digest = glib::compute_checksum_for_string(glib::ChecksumType::Sha256, database.as_str()) + .map(|digest| digest.to_string()) + .unwrap_or_default(); + format!("{}{digest}", Self::databases_prefix(*id)) + } SessionKey::File(uri) => { // GLib returns no digest only for a checksum type it // does not know, which SHA-256 is not. @@ -27,6 +34,11 @@ impl WorkspaceFileName { Self { stem } } + /// What the names of the connection `id`'s other databases start with. + pub fn databases_prefix(id: uuid::Uuid) -> String { + format!("{id}-database-") + } + pub fn file_name(&self) -> String { format!("{}.json", self.stem) } diff --git a/linux/crates/app/src/workspace/workspace_store.rs b/linux/crates/app/src/workspace/workspace_store.rs index 954e1e52ba..dd8d0c99c0 100644 --- a/linux/crates/app/src/workspace/workspace_store.rs +++ b/linux/crates/app/src/workspace/workspace_store.rs @@ -206,6 +206,33 @@ impl WorkspaceStore { async move { purging.await.unwrap_or(Err(WorkspaceSaveError::WriterStopped)) } } + /// Remove the saved tabs and drafts of every other database of the + /// connection `id` that was ever open beside it, as `purge` does for + /// one session. Their names hold a digest of the database, so the + /// writer finds them by the connection's part. + pub fn purge_databases_of(&self, id: uuid::Uuid) -> impl Future> + use<> { + let of_connection = |key: &SessionKey| matches!(key, SessionKey::SavedIn { id: of, .. } if *of == id); + self.inner.blocked.borrow_mut().retain(|key| !of_connection(key)); + self.inner.moved_aside.borrow_mut().retain(|key| !of_connection(key)); + let landings: Vec<_> = self + .inner + .outside + .keys_in_flight(of_connection) + .iter() + .filter_map(|key| self.inner.outside.landing(key)) + .collect(); + let (dir, writer) = (self.inner.dir.clone(), self.inner.writer.clone()); + let prefix = WorkspaceFileName::databases_prefix(id); + async move { + for landing in landings { + let _ = landing.await; + } + let (purged, receiver) = oneshot::channel(); + let _ = writer.send(WriteJob::PurgeNamed { dir, prefix, purged }); + receiver.await.unwrap_or(Err(WorkspaceSaveError::WriterStopped)) + } + } + /// Queue `record` as the sessions the windows show. A newer one /// replaces it if the writer has not reached it yet. pub fn schedule_open_sessions(&self, record: OpenSessionsRecord) -> Result<(), WorkspaceSaveError> { @@ -298,6 +325,7 @@ struct Round { /// A purge wins over a record queued for its file in the same /// round, whichever came first. purges: Vec<(PathBuf, DraftScope, Reply)>, + named_purges: Vec<(PathBuf, String, Reply)>, open_sessions: Option<(PathBuf, OpenSessionsRecord)>, waiting: Vec<(PathBuf, Reply)>, } @@ -311,8 +339,23 @@ impl Round { WriteJob::OpenSessions { path, record } => self.open_sessions = Some((path, record)), WriteJob::Flush { path, written } => self.waiting.push((path, written)), WriteJob::Purge { path, drafts, purged } => self.purges.push((path, drafts, purged)), + WriteJob::PurgeNamed { dir, prefix, purged } => self.named_purges.push((dir, prefix, purged)), } } + + fn purges_path(&self, path: &Path) -> bool { + self.purges.iter().any(|(purged, ..)| purged == path) + || self + .named_purges + .iter() + .any(|(_, prefix, _)| file_name_starts_with(path, prefix)) + } +} + +fn file_name_starts_with(path: &Path, prefix: &str) -> bool { + path.file_name() + .and_then(|name| name.to_str()) + .is_some_and(|name| name.starts_with(prefix)) } async fn run_writer( @@ -328,9 +371,9 @@ async fn run_writer( while let Ok(job) = jobs.try_recv() { round.take(job); } - let purged: HashSet = round.purges.iter().map(|(path, ..)| path.clone()).collect(); - for (path, record) in round.records { - if purged.contains(&path) { + let records = std::mem::take(&mut round.records); + for (path, record) in records { + if round.purges_path(&path) { continue; } let outcome = write_record(&tasks, &write, path.clone(), record).await; @@ -352,6 +395,18 @@ async fn run_writer( } let _ = reply.send(outcome); } + for (dir, prefix, reply) in round.named_purges { + last.retain(|path, _| !file_name_starts_with(path, &prefix)); + let store = drafts.clone(); + let outcome = tasks + .spawn_blocking_task(move || purge_named_blocking(&dir, &store, &prefix)) + .await + .unwrap_or_else(|failure| Err(WorkspaceSaveError::Purge(failure.to_string()))); + if let Err(error) = &outcome { + tracing::warn!(%error, "could not delete the saved tabs of a deleted connection's databases"); + } + let _ = reply.send(outcome); + } for (path, written) in round.waiting { let _ = written.send(last.get(&path).cloned().unwrap_or(Ok(()))); } @@ -372,6 +427,38 @@ fn purge_blocking(path: &Path, drafts: &DraftStore, scope: &DraftScope) -> Resul file.and(copies).and(drafts).map_err(WorkspaceSaveError::Purge) } +/// Every session named with `prefix` in `dir` or in the drafts, whether +/// it left a file, only a copy moved aside as `.json.corrupt-…`, +/// or only drafts. Each is purged as `purge_blocking` does; the first +/// failure is the one reported. +fn purge_named_blocking(dir: &Path, drafts: &DraftStore, prefix: &str) -> Result<(), WorkspaceSaveError> { + let mut stems = std::collections::BTreeSet::new(); + for root in [dir, drafts.root()] { + let entries = match std::fs::read_dir(root) { + Ok(entries) => entries, + Err(error) if error.kind() == ErrorKind::NotFound => continue, + Err(error) => return Err(WorkspaceSaveError::Purge(error.to_string())), + }; + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(name) = name.to_str().filter(|name| name.starts_with(prefix)) else { + continue; + }; + stems.insert(name.split_once(".json").map_or(name, |(stem, _)| stem).to_owned()); + } + } + let mut outcome = Ok(()); + for stem in stems { + let purged = DraftScope::new(&stem) + .map_err(|error| WorkspaceSaveError::Purge(error.to_string())) + .and_then(|scope| purge_blocking(&dir.join(format!("{stem}.json")), drafts, &scope)); + if outcome.is_ok() { + outcome = purged; + } + } + outcome +} + async fn write_record( tasks: &Tasks, write: &Arc, @@ -800,6 +887,61 @@ mod tests { assert_eq!(fixture.on_disk(&other), record(2)); } + /// A deleted connection's other databases leave nothing behind, + /// whether one left its saved tabs, only a copy Reset moved aside, or + /// only drafts. + #[tokio::test] + async fn a_connections_databases_go_whatever_each_left() { + let fixture = Fixture::new(); + let id = Uuid::new_v4(); + let in_database = |id: Uuid, database: &str| SessionKey::SavedIn { + id, + database: database.to_owned(), + }; + let (saved, moved_aside, drafts_only) = ( + in_database(id, "analytics"), + in_database(id, "archive"), + in_database(id, "staging"), + ); + let other = in_database(Uuid::new_v4(), "analytics"); + fixture.store.schedule(&saved, record(1)).expect("scheduled"); + fixture.store.flush(&saved).await.expect("written"); + let aside = fixture.seed(&moved_aside, b"{ not json"); + assert!(matches!( + fixture.store.load(&moved_aside).await, + WorkspaceLoad::Blocked(_) + )); + fixture.store.reset(&moved_aside).await.expect("reset"); + let store = drafts(fixture.root.path()); + let scope_of = |key: &SessionKey| WorkspaceFileName::for_key(key).draft_scope(); + for key in [&saved, &drafts_only, &other] { + store + .write_blocking(&scope_of(key), DraftId::new(), "SELECT 1") + .expect("a draft"); + } + fixture.store.schedule(&other, record(2)).expect("scheduled"); + fixture.store.flush(&other).await.expect("written"); + + fixture.store.purge_databases_of(id).await.expect("purged"); + + assert!(!fixture.path(&saved).exists(), "a database's saved tabs stayed"); + assert!( + !tablepro_storage::fs::moved_aside_exists_blocking(&aside).expect("list"), + "a copy moved aside with no file beside it stayed" + ); + for key in [&saved, &drafts_only] { + assert!( + !store.root().join(scope_of(key).as_str()).exists(), + "drafts with no file beside them stayed" + ); + } + assert!( + store.root().join(scope_of(&other).as_str()).exists(), + "another connection's drafts went" + ); + assert_eq!(fixture.on_disk(&other), record(2)); + } + #[tokio::test] async fn purge_drops_a_record_still_queued() { let root = tempfile::tempdir().expect("tempdir"); diff --git a/linux/crates/app/src/workspace/write_job.rs b/linux/crates/app/src/workspace/write_job.rs index 2e85c6a5a9..545da4e581 100644 --- a/linux/crates/app/src/workspace/write_job.rs +++ b/linux/crates/app/src/workspace/write_job.rs @@ -29,4 +29,12 @@ pub(super) enum WriteJob { drafts: DraftScope, purged: oneshot::Sender>, }, + /// Purge, as `Purge` does, every session whose name starts with + /// `prefix`: those found in `dir` as a file or a copy moved aside, + /// and those with drafts only. + PurgeNamed { + dir: PathBuf, + prefix: String, + purged: oneshot::Sender>, + }, } diff --git a/linux/crates/app/src/workspace/writes_in_flight.rs b/linux/crates/app/src/workspace/writes_in_flight.rs index 4984e59b35..29de1d7d01 100644 --- a/linux/crates/app/src/workspace/writes_in_flight.rs +++ b/linux/crates/app/src/workspace/writes_in_flight.rs @@ -27,6 +27,16 @@ impl OutsideWrites { } } + /// The sessions with writes still in flight that `keep` picks. + pub(super) fn keys_in_flight(&self, keep: impl Fn(&SessionKey) -> bool) -> Vec { + self.sessions + .borrow() + .iter() + .filter(|(key, landing)| landing.writes > 0 && keep(key)) + .map(|(key, _)| key.clone()) + .collect() + } + /// Resolves once every write for `key` has landed, or `None` when /// none is in flight. pub(super) fn landing(&self, key: &SessionKey) -> Option> { diff --git a/linux/crates/session/src/registry/session_key.rs b/linux/crates/session/src/registry/session_key.rs index b35b426ae0..0576d05b70 100644 --- a/linux/crates/session/src/registry/session_key.rs +++ b/linux/crates/session/src/registry/session_key.rs @@ -13,6 +13,15 @@ use super::FileIdentity; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum SessionKey { Saved(Uuid), + /// Another of a saved connection's databases, opened in the window + /// that holds the connection ([0030]). Its sibling sessions share the + /// window and never another's. + /// + /// [0030]: ../../../docs/decisions/0030-a-database-is-a-session-of-its-own.md + SavedIn { + id: Uuid, + database: String, + }, /// The canonical file URI when there is one, else the URI the file /// arrived as. File(String), diff --git a/linux/docs/decisions/0030-a-database-is-a-session-of-its-own.md b/linux/docs/decisions/0030-a-database-is-a-session-of-its-own.md index f2a1d5d0b0..c337953f2e 100644 --- a/linux/docs/decisions/0030-a-database-is-a-session-of-its-own.md +++ b/linux/docs/decisions/0030-a-database-is-a-session-of-its-own.md @@ -15,6 +15,8 @@ A query tab keeps its database, and its own editor session goes there. `EditorSe What the window shows of another database is a sibling session, `Session::open_in_database`: the same server and login, opened as a reconnect is. It joins the window with a workspace and a sidebar row of its own, from Open Database… on a connection's row, or when a tab moves to that database. Completion in a moved tab reads the sibling's catalog. +The connection's open session owns its siblings, open or still opening. They take its Safe Mode level, and a level that needs a reconnect opens them again beside it. Its disconnect and its window's close take them through the same teardown, and a sibling still opening holds its claim there, so none outlives the connection or keeps its key. + ## Rationale - **A tab keeps what it built.** A `USE` keeps the tab's temporary tables and settings. The database menu waits while the tab holds a transaction, so a move never rolls one back. @@ -25,7 +27,8 @@ What the window shows of another database is a sibling session, `Session::open_i ## Consequences - A sibling opens with the stored secret and never prompts; one that cannot open says so in a toast and the tab still runs in its database. -- The registry keys a sibling by its connection and database, so a database opens once per window. It goes when its connection disconnects. +- The registry keys a sibling by its connection and database, so a database opens once per window. +- Deleting a connection deletes its siblings' saved tabs and drafts, found by the connection's part of their names. - A tab names its database in its title when it is not the connection's. - The workspace record keeps a tab's database, and a restore reopens a connection's open databases beside it once it has joined. diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index 4720abdb1c..fbe9b45667 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -476,6 +476,7 @@ crates/app/src/ui/session_window/mod.rs crates/app/src/ui/session_window/notification_request.rs crates/app/src/ui/session_window/notification_sink.rs crates/app/src/ui/session_window/notifications.rs +crates/app/src/ui/session_window/other_databases.rs crates/app/src/ui/session_window/picker_page.rs crates/app/src/ui/session_window/resolve_file_identity.rs crates/app/src/ui/session_window/secret_retry.rs @@ -487,6 +488,7 @@ crates/app/src/ui/session_window/session_window_input.rs crates/app/src/ui/session_window/tests/mod.rs crates/app/src/ui/session_window/tests/notification.rs crates/app/src/ui/session_window/tests/open_file.rs +crates/app/src/ui/session_window/tests/other_databases.rs crates/app/src/ui/session_window/tests/sessions.rs crates/app/src/ui/session_window/window_actions.rs crates/app/src/ui/session_window/work_notifier.rs From 34b2363d8ced50195c02aa3da0ea27b9bbeec4a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sat, 31 Oct 2026 19:01:18 +0700 Subject: [PATCH 44/85] build(linux): give the package builds room for DuckDB 2.0 (#3937) --- linux/build-aux/debian/rules | 4 ++++ linux/crates/app/meson.build | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/linux/build-aux/debian/rules b/linux/build-aux/debian/rules index 2138013052..cc8a912ba9 100755 --- a/linux/build-aux/debian/rules +++ b/linux/build-aux/debian/rules @@ -33,6 +33,10 @@ export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto %: dh $@ --buildsystem=meson +# DuckDB 2.0's C++ debug info passes dwz's limit of 50 million DIEs, and +# dwz then fails the build instead of leaving the file as it is. +override_dh_dwz: + # dh_clean deletes *.orig files, and every vendored crate ships a # Cargo.toml.orig that its .cargo-checksum.json lists. override_dh_clean: diff --git a/linux/crates/app/meson.build b/linux/crates/app/meson.build index ee44c97e10..0ea3ecc0ca 100644 --- a/linux/crates/app/meson.build +++ b/linux/crates/app/meson.build @@ -53,7 +53,9 @@ if profile != 'development' endif # The workspace suite excludes the app, whose tests need a display and -# the GTK test accessibility backend. +# the GTK test accessibility backend. A package build compiles its tests in +# the release profile, DuckDB's C++ a second time among them, which took +# more than an hour on a 4-core builder with DuckDB 2.0. test( 'workspace', cargo, @@ -61,7 +63,7 @@ test( env: test_env, suite: 'unit', is_parallel: false, - timeout: 3600, + timeout: 7200, ) gtk_env = app_env From 973024b7c838a77843c17656e133f350454d6ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sat, 31 Oct 2026 21:00:38 +0700 Subject: [PATCH 45/85] fix(linux): give menu items access keys (#3938) --- linux/changelog.d/3938.fixed.md | 1 + .../app/src/test_support/access_keys.rs | 142 +++++++ linux/crates/app/src/test_support/mod.rs | 2 + linux/crates/app/src/ui/browse_tab/layout.rs | 12 +- .../app/src/ui/connection_appearance.rs | 15 + .../crates/app/src/ui/data_grid/grid_menu.rs | 95 ++--- .../app/src/ui/data_grid/references/mod.rs | 4 +- .../app/src/ui/data_grid/references/tests.rs | 49 ++- .../app/src/ui/data_grid/tests/menus.rs | 19 +- .../crates/app/src/ui/diagram/canvas_input.rs | 28 +- .../app/src/ui/editor/assistant_actions.rs | 26 +- linux/crates/app/src/ui/history_dialog/mod.rs | 12 +- .../crates/app/src/ui/history_dialog/tests.rs | 31 +- linux/crates/app/src/ui/menu_label.rs | 59 +++ .../app/src/ui/picker/connection_menu.rs | 69 ++-- linux/crates/app/src/ui/safe_mode.rs | 9 + .../crates/app/src/ui/saved_queries_dialog.rs | 40 +- .../src/ui/workspace/sidebar/sidebar_menu.rs | 25 +- .../app/src/ui/workspace/sidebar/tree_menu.rs | 54 ++- .../app/src/ui/workspace/workspace_shell.rs | 43 +- linux/data/resources/gtk/menus.ui | 26 +- linux/data/resources/ui/assistant-pane.ui | 2 +- linux/data/resources/ui/diagram-tab.ui | 6 +- linux/data/resources/ui/find-bar.ui | 6 +- linux/data/resources/ui/history-dialog.ui | 8 +- linux/data/resources/ui/row-detail-pane.ui | 8 +- linux/data/resources/ui/sql-editor.ui | 26 +- linux/po/POTFILES.in | 1 + linux/po/vi.po | 383 ++++++++++++------ 29 files changed, 900 insertions(+), 301 deletions(-) create mode 100644 linux/changelog.d/3938.fixed.md create mode 100644 linux/crates/app/src/test_support/access_keys.rs diff --git a/linux/changelog.d/3938.fixed.md b/linux/changelog.d/3938.fixed.md new file mode 100644 index 0000000000..6fd7086f41 --- /dev/null +++ b/linux/changelog.d/3938.fixed.md @@ -0,0 +1 @@ +Menu items had no access keys, so a menu could not be used by pressing an item's letter. diff --git a/linux/crates/app/src/test_support/access_keys.rs b/linux/crates/app/src/test_support/access_keys.rs new file mode 100644 index 0000000000..d821666f01 --- /dev/null +++ b/linux/crates/app/src/test_support/access_keys.rs @@ -0,0 +1,142 @@ +use gtk4::prelude::*; +use gtk4::{gio, glib}; + +/// The items one level of a menu shows at once. GtkPopoverMenu runs the +/// sections together and slides a submenu in as a page of its own, headed +/// by its item, whose label keeps the access key. +#[derive(Debug, Default)] +pub(crate) struct MenuPage { + pub labels: Vec, +} + +/// Every page of `menu`, its top level first. +pub(crate) fn menu_pages(menu: &impl IsA) -> Vec { + let mut pages = vec![MenuPage::default()]; + collect(menu.as_ref(), 0, &mut pages); + pages +} + +fn collect(menu: &gio::MenuModel, page: usize, pages: &mut Vec) { + for position in 0..menu.n_items() { + // A section's label is a heading, which GTK shows without a key. + if let Some(section) = menu.item_link(position, gio::MENU_LINK_SECTION) { + collect(§ion, page, pages); + continue; + } + let label = menu + .item_attribute_value(position, gio::MENU_ATTRIBUTE_LABEL, Some(glib::VariantTy::STRING)) + .and_then(|label| label.get::()); + if let Some(label) = &label { + pages[page].labels.push(label.clone()); + } + if let Some(submenu) = menu.item_link(position, gio::MENU_LINK_SUBMENU) { + pages.push(MenuPage { + labels: label.into_iter().collect(), + }); + let index = pages.len() - 1; + collect(&submenu, index, pages); + } + } +} + +/// The characters `label` marks as access keys. A doubled underscore is +/// a literal one. +pub(crate) fn access_keys(label: &str) -> Vec { + let mut keys = Vec::new(); + let mut chars = label.chars(); + while let Some(c) = chars.next() { + if c == '_' { + match chars.next() { + Some('_') | None => {} + Some(key) => keys.push(key), + } + } + } + keys +} + +/// Each item that has no access key, or more than one, or the key of +/// another item on its page, as the GNOME HIG asks of every menu. +pub(crate) fn access_key_problems(pages: &[MenuPage]) -> Vec { + let mut problems = Vec::new(); + for page in pages { + let mut taken: Vec<(String, &str)> = Vec::new(); + for label in &page.labels { + let keys = access_keys(label); + let [key] = keys.as_slice() else { + problems.push(format!("{label:?} has {} access keys", keys.len())); + continue; + }; + let key: String = key.to_lowercase().collect(); + if let Some((_, other)) = taken.iter().find(|(used, _)| *used == key) { + problems.push(format!("{label:?} has the access key of {other:?}")); + } + taken.push((key, label)); + } + } + problems +} + +pub(crate) fn assert_access_keys(menu: &impl IsA) { + let problems = access_key_problems(&menu_pages(menu)); + assert!(problems.is_empty(), "{problems:#?}"); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn menu(labels: &[&str]) -> gio::Menu { + let menu = gio::Menu::new(); + for label in labels { + menu.append(Some(*label), Some("app.any")); + } + menu + } + + #[test] + fn a_single_underscore_marks_the_key_and_a_doubled_one_is_text() { + assert_eq!(access_keys("Sort _Descending"), ['D']); + assert_eq!(access_keys("in__stock"), Vec::::new()); + assert_eq!(access_keys("_a__b"), ['a']); + assert_eq!(access_keys("Copy"), Vec::::new()); + assert_eq!(access_keys("_One _Two"), ['O', 'T']); + } + + #[test] + fn sections_share_a_page_and_a_submenu_is_its_own() { + let whole = menu(&["_Copy"]); + whole.append_section(Some("Heading"), &menu(&["C_lose"])); + whole.append_submenu(Some("Colu_mn"), &menu(&["_Copy Name"])); + + let pages = menu_pages(&whole); + + let labels: Vec> = pages + .iter() + .map(|page| page.labels.iter().map(String::as_str).collect()) + .collect(); + assert_eq!( + labels, + [vec!["_Copy", "C_lose", "Colu_mn"], vec!["Colu_mn", "_Copy Name"]] + ); + assert_eq!(access_key_problems(&pages), Vec::::new()); + } + + #[test] + fn a_missing_a_doubled_and_a_shared_key_are_each_reported() { + let whole = menu(&["_Copy", "Paste", "_Cut", "_One _Two"]); + whole.append_submenu(Some("_More"), &menu(&["_Mode"])); + + let problems = access_key_problems(&menu_pages(&whole)); + + assert_eq!( + problems, + [ + "\"Paste\" has 0 access keys", + "\"_Cut\" has the access key of \"_Copy\"", + "\"_One _Two\" has 2 access keys", + "\"_Mode\" has the access key of \"_More\"", + ] + ); + } +} diff --git a/linux/crates/app/src/test_support/mod.rs b/linux/crates/app/src/test_support/mod.rs index 17b7bab220..310a3faaae 100644 --- a/linux/crates/app/src/test_support/mod.rs +++ b/linux/crates/app/src/test_support/mod.rs @@ -1,3 +1,4 @@ +mod access_keys; mod accessible_label; mod alert_response; mod application_end; @@ -22,6 +23,7 @@ mod unlabelled_widget; mod wait_timed_out; mod widget_lookup; +pub(crate) use access_keys::{access_key_problems, assert_access_keys, menu_pages}; pub(crate) use accessible_label::{assert_labelled, test_backend_records_properties}; pub(crate) use alert_response::{click_response, respond}; pub(crate) use application_end::end_application; diff --git a/linux/crates/app/src/ui/browse_tab/layout.rs b/linux/crates/app/src/ui/browse_tab/layout.rs index 46fb075649..69865bb3d8 100644 --- a/linux/crates/app/src/ui/browse_tab/layout.rs +++ b/linux/crates/app/src/ui/browse_tab/layout.rs @@ -65,9 +65,9 @@ pub(super) fn build(tab: &BrowseTab, init: &LayoutInit<'_>) -> BrowseTabWidgets /// room for it. fn more_menu(page_sizes: &[u64]) -> gio::Menu { let actions = gio::Menu::new(); - actions.append(Some(&gettext("Filter Rows")), Some("browse.filter")); - actions.append(Some(&gettext("Export Rows…")), Some("browse.export")); - actions.append(Some(&gettext("Count Rows")), Some("browse.count-rows")); + actions.append(Some(&gettext("_Filter Rows")), Some("browse.filter")); + actions.append(Some(&gettext("_Export Rows…")), Some("browse.export")); + actions.append(Some(&gettext("_Count Rows")), Some("browse.count-rows")); let sizes = gio::Menu::new(); for size in page_sizes { let item = gio::MenuItem::new(Some(&format_thousands(*size)), None); @@ -284,6 +284,12 @@ fn collapse_top_bars_with(tab: &BrowseTab, filter: &FilterStrip) { mod tests { use super::*; + #[test] + fn every_action_in_the_more_menu_has_an_access_key_of_its_own() { + // A page size is a number, which takes no key. + crate::test_support::assert_access_keys(&more_menu(&[])); + } + #[gtk4::test] fn the_dropdown_shows_a_page_size_set_in_preferences() { let dropdown = gtk::DropDown::default(); diff --git a/linux/crates/app/src/ui/connection_appearance.rs b/linux/crates/app/src/ui/connection_appearance.rs index d397aedefc..5b805a226d 100644 --- a/linux/crates/app/src/ui/connection_appearance.rs +++ b/linux/crates/app/src/ui/connection_appearance.rs @@ -18,6 +18,21 @@ pub(crate) fn color_name(color: ConnectionColor) -> String { } } +// A tooltip or a drop-down shows `color_name`, which has no access key. +pub(crate) fn color_menu_label(color: ConnectionColor) -> String { + match color { + ConnectionColor::Blue => crate::i18n::gettext("_Blue"), + ConnectionColor::Teal => crate::i18n::gettext("_Teal"), + ConnectionColor::Green => crate::i18n::gettext("_Green"), + ConnectionColor::Yellow => crate::i18n::gettext("_Yellow"), + ConnectionColor::Orange => crate::i18n::gettext("_Orange"), + ConnectionColor::Red => crate::i18n::gettext("Re_d"), + ConnectionColor::Pink => crate::i18n::gettext("Pin_k"), + ConnectionColor::Purple => crate::i18n::gettext("_Purple"), + ConnectionColor::Slate => crate::i18n::gettext("_Slate"), + } +} + pub(crate) fn color_class(color: ConnectionColor) -> &'static str { match color { ConnectionColor::Blue => "tp-tag-blue", diff --git a/linux/crates/app/src/ui/data_grid/grid_menu.rs b/linux/crates/app/src/ui/data_grid/grid_menu.rs index 0247373849..48fe7e9d6a 100644 --- a/linux/crates/app/src/ui/data_grid/grid_menu.rs +++ b/linux/crates/app/src/ui/data_grid/grid_menu.rs @@ -37,9 +37,9 @@ fn item(label: &str, action: &str) -> gio::MenuItem { /// Set to Empty, NULL or DEFAULT, each shown where the cell takes it. pub(crate) fn presets_section() -> gio::Menu { section([ - hidden_while_disabled(&gettext("Set to Empty"), "grid.set-empty"), - hidden_while_disabled(&gettext("Set to NULL"), "grid.set-null"), - hidden_while_disabled(&gettext("Set to DEFAULT"), "grid.set-default"), + hidden_while_disabled(&gettext("Set _to Empty"), "grid.set-empty"), + hidden_while_disabled(&gettext("Set to _NULL"), "grid.set-null"), + hidden_while_disabled(&gettext("Set to DEFAU_LT"), "grid.set-default"), ]) } @@ -60,8 +60,8 @@ impl GridMenus { popover( grid, §ion([ - item(&gettext("Insert Row"), "grid.insert-row"), - item(&gettext("Paste"), "grid.paste"), + item(&gettext("_Insert Row"), "grid.insert-row"), + item(&gettext("_Paste"), "grid.paste"), ]), ) }); @@ -112,49 +112,52 @@ fn cell_menu(grid: &DataGrid, references: &gio::Menu) -> gio::Menu { let menu = gio::Menu::new(); if editable { let edits = presets_section(); - edits.prepend_item(&hidden_while_disabled(&gettext("Edit Cell"), "grid.edit")); + edits.prepend_item(&hidden_while_disabled(&gettext("_Edit Cell"), "grid.edit")); menu.append_section(None, &edits); } let copy_as = section([ - item(&gettext("Rows"), "grid.copy-rows"), - item(&gettext("With Headers"), "grid.copy-rows-headers"), - item(&gettext("CSV"), "grid.copy-csv"), - item(&gettext("CSV With Headers"), "grid.copy-csv-headers"), + item(&gettext("_Rows"), "grid.copy-rows"), + item(&gettext("_With Headers"), "grid.copy-rows-headers"), + item(&gettext("_CSV"), "grid.copy-csv"), + item(&gettext("CSV With _Headers"), "grid.copy-csv-headers"), ]); copy_as.append_section( None, §ion([ - item(&gettext("JSON"), "grid.copy-json"), - item(&gettext("JSON Lines"), "grid.copy-json-lines"), - item(&gettext("Markdown"), "grid.copy-markdown"), - item(&gettext("Text Table"), "grid.copy-text-table"), + item(&gettext("_JSON"), "grid.copy-json"), + item(&gettext("JSON _Lines"), "grid.copy-json-lines"), + item(&gettext("_Markdown"), "grid.copy-markdown"), + item(&gettext("_Text Table"), "grid.copy-text-table"), ]), ); copy_as.append_section( None, §ion([ - hidden_while_disabled(&gettext("IN Clause"), "grid.copy-in-clause"), - hidden_while_disabled(&gettext("One Row"), "grid.copy-one-row"), - hidden_while_disabled(&gettext("INSERT Statement"), "grid.copy-insert"), + hidden_while_disabled(&gettext("_IN Clause"), "grid.copy-in-clause"), + hidden_while_disabled(&gettext("_One Row"), "grid.copy-one-row"), + hidden_while_disabled(&gettext("INSERT _Statement"), "grid.copy-insert"), hidden_while_disabled( - &gettext("INSERT Without Generated Keys"), + &gettext("INSERT Without Generated _Keys"), "grid.copy-insert-without-generated-keys", ), ]), ); - copy_as.append_section(None, §ion([item(&gettext("Column Name"), "grid.copy-column-name")])); - let clipboard = section([item(&gettext("Copy"), "grid.copy")]); - clipboard.append_submenu(Some(&gettext("Copy As")), ©_as); + copy_as.append_section( + None, + §ion([item(&gettext("Column _Name"), "grid.copy-column-name")]), + ); + let clipboard = section([item(&gettext("_Copy"), "grid.copy")]); + clipboard.append_submenu(Some(&gettext("Copy _As")), ©_as); if editable { - clipboard.append_item(&item(&gettext("Paste"), "grid.paste")); + clipboard.append_item(&item(&gettext("_Paste"), "grid.paste")); } menu.append_section(None, &clipboard); if config.is_some_and(|config| config.filter_by_value) { menu.append_section( None, §ion([ - hidden_while_disabled(&gettext("Filter by Value"), "grid.filter-by-value"), - hidden_while_disabled(&gettext("Exclude Value"), "grid.exclude-value"), + hidden_while_disabled(&gettext("_Filter by Value"), "grid.filter-by-value"), + hidden_while_disabled(&gettext("Exclude _Value"), "grid.exclude-value"), ]), ); } @@ -162,16 +165,16 @@ fn cell_menu(grid: &DataGrid, references: &gio::Menu) -> gio::Menu { menu.append_section( None, §ion([ - item(&gettext("Row Details"), "grid.show-details"), - item(&gettext("Show Row as JSON"), "grid.show-row-json"), + item(&gettext("_Row Details"), "grid.show-details"), + item(&gettext("Show Row as _JSON"), "grid.show-row-json"), ]), ); menu.append_section(None, &column_section(sorts)); - let rows = section([item(&gettext("Export…"), "grid.export")]); + let rows = section([item(&gettext("E_xport…"), "grid.export")]); if editable { - rows.append_item(&item(&gettext("Insert Row"), "grid.insert-row")); - rows.append_item(&item(&gettext("Duplicate"), "grid.duplicate-rows")); - rows.append_item(&item(&gettext("Delete"), "grid.delete-rows")); + rows.append_item(&item(&gettext("_Insert Row"), "grid.insert-row")); + rows.append_item(&item(&gettext("D_uplicate"), "grid.duplicate-rows")); + rows.append_item(&item(&gettext("_Delete"), "grid.delete-rows")); } menu.append_section(None, &rows); menu @@ -183,25 +186,25 @@ fn column_section(sorts: bool) -> gio::Menu { let column = gio::Menu::new(); if sorts { column.append_item(&hidden_while_disabled( - &gettext("Sort Ascending"), + &gettext("Sort _Ascending"), "grid.sort-ascending", )); column.append_item(&hidden_while_disabled( - &gettext("Sort Descending"), + &gettext("Sort _Descending"), "grid.sort-descending", )); - column.append_item(&item(&gettext("Clear Sort"), "grid.clear-sort")); + column.append_item(&item(&gettext("_Clear Sort"), "grid.clear-sort")); } column.append_section( None, §ion([ - item(&gettext("Hide Column"), "grid.hide-column"), - item(&gettext("Size to Fit"), "grid.size-to-fit"), - item(&gettext("Columns…"), "grid.columns"), + item(&gettext("_Hide Column"), "grid.hide-column"), + item(&gettext("_Size to Fit"), "grid.size-to-fit"), + item(&gettext("Col_umns…"), "grid.columns"), ]), ); let menu = gio::Menu::new(); - menu.append_submenu(Some(&gettext("Column")), &column); + menu.append_submenu(Some(&gettext("Colu_mn")), &column); menu } @@ -219,21 +222,21 @@ pub fn header_menu(column: &GridColumn, sorting: GridSorting) -> gio::Menu { menu.append_section( None, §ion([ - at(gettext("Sort Ascending"), "grid.sort-column-ascending"), - at(gettext("Sort Descending"), "grid.sort-column-descending"), - item(&gettext("Clear Sort"), "grid.clear-sort"), + at(gettext("Sort _Ascending"), "grid.sort-column-ascending"), + at(gettext("Sort _Descending"), "grid.sort-column-descending"), + item(&gettext("_Clear Sort"), "grid.clear-sort"), ]), ); } menu.append_section( None, §ion([ - at(gettext("Hide Column"), "grid.hide-column-at"), - at(gettext("Size to Fit"), "grid.size-column-to-fit"), - at(gettext("Copy Column Name"), "grid.copy-column-name-at"), + at(gettext("_Hide Column"), "grid.hide-column-at"), + at(gettext("_Size to Fit"), "grid.size-column-to-fit"), + at(gettext("Copy Column _Name"), "grid.copy-column-name-at"), ]), ); - menu.append_section(None, §ion([item(&gettext("Columns…"), "grid.columns")])); + menu.append_section(None, §ion([item(&gettext("Col_umns…"), "grid.columns")])); menu } @@ -241,13 +244,13 @@ pub fn header_menu(column: &GridColumn, sorting: GridSorting) -> gio::Menu { /// sorting, so the header selects the rows from here. The key is /// GtkListBase's `list.select-all`, out of a header menu's reach. pub fn row_numbers_header_menu() -> gio::Menu { - let select_all = item(&gettext("Select All Rows"), "grid.select-all"); + let select_all = item(&gettext("Select _All Rows"), "grid.select-all"); if let Some(key) = crate::application::triggers_for("list.select-all").first() { select_all.set_attribute_value("accel", Some(&key.to_variant())); } let menu = gio::Menu::new(); menu.append_section(None, §ion([select_all])); - menu.append_section(None, §ion([item(&gettext("Columns…"), "grid.columns")])); + menu.append_section(None, §ion([item(&gettext("Col_umns…"), "grid.columns")])); menu } diff --git a/linux/crates/app/src/ui/data_grid/references/mod.rs b/linux/crates/app/src/ui/data_grid/references/mod.rs index 3d7f16b93f..a5203f1b90 100644 --- a/linux/crates/app/src/ui/data_grid/references/mod.rs +++ b/linux/crates/app/src/ui/data_grid/references/mod.rs @@ -128,7 +128,7 @@ impl DataGrid { for (index, label) in labelled(open.collect()) { choices.append_item(&choice(&label, OPEN_CHOICE, index)); } - let open_label = gettext("Open Referenced Row"); + let open_label = gettext("_Open Referenced Row"); match armed.offers.open.as_slice() { [] => {} // The item shows Ctrl+J but acts on the key it offered, as a key @@ -150,7 +150,7 @@ impl DataGrid { referencing.append_item(&choice(&label, REFERENCING, index)); } if referencing.n_items() > 0 { - section.append_submenu(Some(&gettext("Show Referencing Rows")), &referencing); + section.append_submenu(Some(&gettext("_Show Referencing Rows")), &referencing); } } diff --git a/linux/crates/app/src/ui/data_grid/references/tests.rs b/linux/crates/app/src/ui/data_grid/references/tests.rs index 397b7f1f17..be15694f0d 100644 --- a/linux/crates/app/src/ui/data_grid/references/tests.rs +++ b/linux/crates/app/src/ui/data_grid/references/tests.rs @@ -8,7 +8,7 @@ use tablepro_core::{ColumnInfo, ForeignKeyInfo, Value}; use crate::application::ShortcutOwner; use crate::test_support::shortcut_keys::{keystroke, owner_controller, shortcuts, trigger_keys}; -use crate::test_support::wait_until; +use crate::test_support::{access_key_problems, menu_pages, wait_until}; use crate::ui::data_grid::test_grid::{Grid, GridSpec, WAIT, named, persisted}; use crate::ui::data_grid::{GridRequest, KeysHandle, TableKeys}; @@ -168,7 +168,7 @@ fn a_key_cell_offers_open_referenced_row_and_sends_the_rows_it_names() { assert_eq!( offered, - [item("Open Referenced Row", Some("grid.open-reference"), Some(0))] + [item("_Open Referenced Row", Some("grid.open-reference"), Some(0))] ); assert_eq!(followed(&grid), [(public("customers"), filter(&[("id", "42")]))]); } @@ -244,7 +244,7 @@ fn a_column_in_two_keys_offers_each_and_follows_the_one_chosen() { assert_eq!( offered, [ - item("Open Referenced Row", None, None), + item("_Open Referenced Row", None, None), item("tenants (tenant__id)", Some("grid.open-reference"), Some(0)), item( "crm.customers (tenant__id, customer__id)", @@ -273,7 +273,7 @@ fn keys_from_the_same_columns_into_one_table_name_their_constraints() { assert_eq!( offered, [ - item("Open Referenced Row", None, None), + item("_Open Referenced Row", None, None), item( "customers (customer__id) through by__id", Some("grid.open-reference"), @@ -354,7 +354,7 @@ fn referencing_rows_are_offered_for_a_saved_row_and_not_for_a_draft() { assert_eq!( offered, [ - item("Show Referencing Rows", None, None), + item("_Show Referencing Rows", None, None), item( "sales.orders (customer__id)", Some("grid.show-referencing-rows"), @@ -366,6 +366,45 @@ fn referencing_rows_are_offered_for_a_saved_row_and_not_for_a_draft() { assert_eq!(on_draft, [], "a draft is offered rows that point at it"); } +/// The reference items join the cell menu's own page, so their keys +/// must be free there. +#[gtk4::test] +fn the_reference_items_keep_an_access_key_of_their_own() { + let grid = with_keys( + vec![named("id", "integer", true), named("customer_id", "integer", false)], + vec![vec![Value::Int(1), Value::Int(42)]], + TableKeys { + table: public("orders"), + outbound: vec![OutboundForeignKey { + key: key(&["customer_id"], public("customers"), &["id"]), + parent_compares_alike: true, + }], + inbound: vec![InboundForeignKey { + child_compares_alike: true, + name: "invoices_order_id_fkey".to_owned(), + table: public("invoices"), + columns: vec!["order_id".to_owned()], + referenced_columns: vec!["id".to_owned()], + }], + }, + ); + + open_menu(&grid, 1, 1); + let menu = grid + .grid + .menus() + .and_then(|menus| menus.cell.menu_model()) + .expect("the cell menu"); + let pages = menu_pages(&menu); + close_menus(&grid); + + let cell = &pages[0].labels; + assert!(cell.iter().any(|label| label == "_Open Referenced Row"), "{cell:?}"); + assert!(cell.iter().any(|label| label == "_Show Referencing Rows"), "{cell:?}"); + // The referencing tables' names fill the submenu, and a name takes no key. + assert_eq!(access_key_problems(&pages[..1]), Vec::::new()); +} + #[gtk4::test] fn a_grid_given_no_keys_offers_no_reference_items() { let grid = Grid::open( diff --git a/linux/crates/app/src/ui/data_grid/tests/menus.rs b/linux/crates/app/src/ui/data_grid/tests/menus.rs index c1e324efe1..65235213f1 100644 --- a/linux/crates/app/src/ui/data_grid/tests/menus.rs +++ b/linux/crates/app/src/ui/data_grid/tests/menus.rs @@ -8,7 +8,7 @@ use super::{ accel_of, action_names, cell_menu, close_menus, data_column, keyed, menu_actions, none, press, selected_ids, submenu, text, }; -use crate::test_support::{FocusThief, after_next_frame, drain_main_context, wait_until}; +use crate::test_support::{FocusThief, after_next_frame, assert_access_keys, drain_main_context, wait_until}; use crate::ui::data_grid::test_grid::{ Grid, GridSpec, WAIT, focus_in_grid, key, named, results, two_text_columns, two_text_values, }; @@ -61,7 +61,7 @@ fn every_header_action_is_in_the_cell_menus_column_submenu() { let grid = people(); let header = data_column(&grid, 1).header_menu().expect("the header's menu"); let menu = cell_menu(&grid); - let in_column = action_names(&submenu(&menu, "Column").expect("the cell menu's Column submenu")); + let in_column = action_names(&submenu(&menu, "Colu_mn").expect("the cell menu's Column submenu")); let in_cell = action_names(&menu); let header_actions = menu_actions(&header); @@ -81,6 +81,21 @@ fn every_header_action_is_in_the_cell_menus_column_submenu() { } } +#[gtk4::test] +fn every_grid_menu_gives_each_item_its_own_access_key() { + let grid = people(); + let empty = grid + .grid + .menus() + .and_then(|menus| menus.empty.as_ref()?.menu_model()) + .expect("the menu below the rows"); + + assert_access_keys(&cell_menu(&grid)); + assert_access_keys(&empty); + assert_access_keys(&data_column(&grid, 1).header_menu().expect("the header's menu")); + assert_access_keys(&crate::ui::data_grid::grid_menu::row_numbers_header_menu()); +} + #[gtk4::test] fn the_cell_menus_column_items_act_on_its_cell_not_the_focused_one() { let grid = people(); diff --git a/linux/crates/app/src/ui/diagram/canvas_input.rs b/linux/crates/app/src/ui/diagram/canvas_input.rs index 214fa0d9c6..b8626663ef 100644 --- a/linux/crates/app/src/ui/diagram/canvas_input.rs +++ b/linux/crates/app/src/ui/diagram/canvas_input.rs @@ -226,13 +226,7 @@ pub(super) fn popup_menu(canvas: &DiagramCanvas, at: gdk::Rectangle) { .popover .borrow_mut() .get_or_insert_with(|| { - let menu = gio::Menu::new(); - menu.append_item(&menu_item(&crate::i18n::gettext("Open Table"), "diagram.open-table")); - menu.append_item(&menu_item( - &crate::i18n::gettext("Show Structure"), - "diagram.show-structure", - )); - let popover = gtk::PopoverMenu::from_model(Some(&menu)); + let popover = gtk::PopoverMenu::from_model(Some(&table_menu())); popover.set_has_arrow(true); popover.set_parent(canvas); popover @@ -241,3 +235,23 @@ pub(super) fn popup_menu(canvas: &DiagramCanvas, at: gdk::Rectangle) { popover.set_pointing_to(Some(&at)); popover.popup(); } + +fn table_menu() -> gio::Menu { + let menu = gio::Menu::new(); + menu.append_item(&menu_item(&crate::i18n::gettext("_Open Table"), "diagram.open-table")); + menu.append_item(&menu_item( + &crate::i18n::gettext("Show _Structure"), + "diagram.show-structure", + )); + menu +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_item_has_an_access_key_of_its_own() { + crate::test_support::assert_access_keys(&table_menu()); + } +} diff --git a/linux/crates/app/src/ui/editor/assistant_actions.rs b/linux/crates/app/src/ui/editor/assistant_actions.rs index 2ef1794b84..e9999a5df8 100644 --- a/linux/crates/app/src/ui/editor/assistant_actions.rs +++ b/linux/crates/app/src/ui/editor/assistant_actions.rs @@ -263,7 +263,31 @@ pub(super) mod tests { let labels = menu_labels(&menu); assert!(labels.iter().any(|label| label == "C_hange Case"), "{labels:?}"); - assert!(labels.iter().any(|label| label == "Explain with AI"), "{labels:?}"); + assert!(labels.iter().any(|label| label == "Explain _with AI"), "{labels:?}"); + } + + /// GtkTextView 4.22 shows its own items on the context menu's first + /// page, before the extra menu. + const TEXT_VIEW_ITEMS: [&str; 8] = [ + "Cu_t", + "_Copy", + "_Paste", + "_Delete", + "_Undo", + "_Redo", + "Select _All", + "Insert _Emoji", + ]; + + #[gtk4::test] + fn the_context_menu_leaves_the_text_views_access_keys_alone() { + let harness = editor_with_assistant("SELECT 1;", AiPolicy::Ask); + let menu = harness.editor.imp().source_view.extra_menu().expect("the context menu"); + + let mut pages = crate::test_support::menu_pages(&menu); + pages[0].labels.extend(TEXT_VIEW_ITEMS.map(str::to_owned)); + + assert_eq!(crate::test_support::access_key_problems(&pages), Vec::::new()); } #[gtk4::test] diff --git a/linux/crates/app/src/ui/history_dialog/mod.rs b/linux/crates/app/src/ui/history_dialog/mod.rs index c625690fa0..b804278e5f 100644 --- a/linux/crates/app/src/ui/history_dialog/mod.rs +++ b/linux/crates/app/src/ui/history_dialog/mod.rs @@ -1090,18 +1090,18 @@ fn row_menu(entry: &HistoryEntryObject) -> gio::Menu { item }; let open = gio::Menu::new(); - open.append_item(&item(gettext("Open in New Tab"), "history.open")); - open.append_item(&item(gettext("Replace Current Tab"), "history.replace")); + open.append_item(&item(gettext("_Open in New Tab"), "history.open")); + open.append_item(&item(gettext("_Replace Current Tab"), "history.replace")); let keep = gio::Menu::new(); let pin = if entry.is_pinned() { - gettext("Unpin") + gettext("_Unpin") } else { - gettext("Pin") + gettext("_Pin") }; keep.append_item(&item(pin, "history.pin")); - keep.append_item(&item(gettext("Copy SQL"), "history.copy")); + keep.append_item(&item(gettext("_Copy SQL"), "history.copy")); let remove = gio::Menu::new(); - remove.append_item(&item(gettext("Delete"), "history.delete")); + remove.append_item(&item(gettext("_Delete"), "history.delete")); let menu = gio::Menu::new(); menu.append_section(None, &open); menu.append_section(None, &keep); diff --git a/linux/crates/app/src/ui/history_dialog/tests.rs b/linux/crates/app/src/ui/history_dialog/tests.rs index 1cf6dffb38..fa310d94ac 100644 --- a/linux/crates/app/src/ui/history_dialog/tests.rs +++ b/linux/crates/app/src/ui/history_dialog/tests.rs @@ -16,7 +16,7 @@ use uuid::Uuid; use super::*; use crate::runtime::AppRuntime; use crate::services::history_preferences::HistoryPreferences; -use crate::test_support::{descendants, drain_main_context, test_runtime, wait_until}; +use crate::test_support::{assert_access_keys, descendants, drain_main_context, test_runtime, wait_until}; use crate::ui::file_reveal::RecordingRevealer; /// Long enough that a loaded machine cannot decide the outcome; a @@ -359,7 +359,7 @@ fn row_context_menu_open_emits_output_for_clicked_id() { let menu = dialog.imp().parts.get().expect("parts").menu.clone(); assert!(menu.is_visible(), "the menu did not open"); let model = menu.menu_model().expect("a menu model"); - let (action, target) = menu_item(&model, "Open in New Tab").expect("an Open item"); + let (action, target) = menu_item(&model, "_Open in New Tab").expect("an Open item"); menu.popdown(); dialog.activate_action(&action, target.as_ref()).expect("the action"); @@ -601,6 +601,29 @@ fn the_open_search_bar_is_labelled() { ); } +#[test] +fn every_row_menu_item_has_an_access_key_of_its_own() { + let entry = |pinned: bool| { + HistoryEntryObject::new(tablepro_storage::query_history::Entry { + id: 1, + query: "SELECT 1".to_owned(), + driver_id: "sqlite".to_owned(), + connection_id: Uuid::new_v4(), + connection_name: "Local".to_owned(), + executed_at: SystemTime::UNIX_EPOCH, + duration_ms: None, + rows_affected: None, + success: true, + cancelled: false, + pinned, + error: None, + }) + }; + + assert_access_keys(&row_menu(&entry(false))); + assert_access_keys(&row_menu(&entry(true))); +} + #[gtk4::test] fn history_dialog_show_in_files_reveals_the_database_file() { let harness = Harness::new(); @@ -611,7 +634,7 @@ fn history_dialog_show_in_files_reveals_the_database_file() { .filter_map(|widget| widget.downcast::().ok()) .find_map(|button| button.menu_model()) .expect("the dialog's menu"); - let (action, target) = menu_item(&menu, "Show in Files").expect("a Show in Files item"); + let (action, target) = menu_item(&menu, "_Show in Files").expect("a Show in Files item"); dialog.activate_action(&action, target.as_ref()).expect("the action"); @@ -658,7 +681,7 @@ fn menu_key_opens_the_menu_of_the_focused_row() { let menu = dialog.imp().parts.get().expect("parts").menu.clone(); assert!(menu.is_visible(), "the menu did not open"); let model = menu.menu_model().expect("a menu model"); - let (_, target) = menu_item(&model, "Delete").expect("a Delete item"); + let (_, target) = menu_item(&model, "_Delete").expect("a Delete item"); assert_eq!(target.and_then(|target| target.get::()), Some(first)); menu.popdown(); } diff --git a/linux/crates/app/src/ui/menu_label.rs b/linux/crates/app/src/ui/menu_label.rs index f3862c1307..e31af1da0a 100644 --- a/linux/crates/app/src/ui/menu_label.rs +++ b/linux/crates/app/src/ui/menu_label.rs @@ -7,7 +7,11 @@ pub(crate) fn menu_label(text: &str) -> String { #[cfg(test)] mod tests { + use gtk4::{self as gtk, gio}; + use super::menu_label; + use crate::config::RESOURCE_BASE_PATH; + use crate::test_support::{access_key_problems, menu_pages}; #[test] fn underscores_are_doubled_and_nothing_else_changes() { @@ -15,4 +19,59 @@ mod tests { assert_eq!(menu_label("__x"), "____x"); assert_eq!(menu_label("Sales Q1"), "Sales Q1"); } + + fn ui_files(folder: &str) -> Vec { + let names = gio::resources_enumerate_children(folder, gio::ResourceLookupFlags::NONE).expect("the folder"); + names + .iter() + .flat_map(|name| match name.strip_suffix('/') { + Some(inner) => ui_files(&format!("{folder}/{inner}")), + None if name.ends_with(".ui") => vec![format!("{folder}/{name}")], + None => Vec::new(), + }) + .collect() + } + + // A template's class is not registered here, so only the menus are built. + fn menus(ui: &str) -> Vec<(&str, &str)> { + let mut found = Vec::new(); + let mut rest = ui; + while let Some(start) = rest.find("").expect("a closed menu") + "".len(); + found.push((&rest[id_start..id_end], &rest[start..end])); + rest = &rest[end..]; + } + found + } + + #[gtk4::test] + fn every_menu_a_ui_file_holds_gives_each_item_its_own_access_key() { + crate::register_test_resources(); + let mut checked = Vec::new(); + + for path in ui_files(RESOURCE_BASE_PATH) { + let data = gio::resources_lookup_data(&path, gio::ResourceLookupFlags::NONE).expect("the file"); + let found = menus(std::str::from_utf8(&data).expect("UTF-8")); + if found.is_empty() { + continue; + } + let elements: String = found.iter().map(|(_, element)| *element).collect(); + let builder = gtk::Builder::new(); + builder + .add_from_string(&format!("{elements}")) + .expect("the menus build"); + for (id, _) in found { + let menu = builder.object::(id).expect("the menu"); + let problems = access_key_problems(&menu_pages(&menu)); + assert!(problems.is_empty(), "{path} {id}: {problems:#?}"); + checked.push(id.to_owned()); + } + } + + for id in ["primary_menu", "tab_overview_menu", "editor_menu", "options_menu"] { + assert!(checked.iter().any(|read| read == id), "{id} was not read: {checked:?}"); + } + } } diff --git a/linux/crates/app/src/ui/picker/connection_menu.rs b/linux/crates/app/src/ui/picker/connection_menu.rs index fcca13cc7e..851da8acc4 100644 --- a/linux/crates/app/src/ui/picker/connection_menu.rs +++ b/linux/crates/app/src/ui/picker/connection_menu.rs @@ -3,7 +3,7 @@ use gtk4::{gio, glib}; use tablepro_storage::{ConnectionColor, ConnectionGroupName, ConnectionTag}; use uuid::Uuid; -use crate::ui::connection_appearance::color_name; +use crate::ui::connection_appearance::color_menu_label; use crate::ui::menu_label; pub(super) const PICKER: &str = "picker"; @@ -44,7 +44,7 @@ pub(crate) fn connect_action(id: Uuid) -> (String, glib::Variant) { /// The Connect item of the connection `id`'s menu. pub(crate) fn connect_item(id: Uuid) -> gio::MenuItem { item( - &crate::i18n::gettext("Connect"), + &crate::i18n::gettext("_Connect"), PICKER, CONNECT, &id.to_string().to_variant(), @@ -74,20 +74,20 @@ pub(crate) fn connection_menu_with( let menu = gio::Menu::new(); menu.append_section(None, lead); let change = gio::Menu::new(); - change.append_item(&item(&crate::i18n::gettext("Edit…"), PICKER, EDIT, &target)); - change.append_item(&item(&crate::i18n::gettext("Duplicate"), PICKER, DUPLICATE, &target)); + change.append_item(&item(&crate::i18n::gettext("_Edit…"), PICKER, EDIT, &target)); + change.append_item(&item(&crate::i18n::gettext("D_uplicate"), PICKER, DUPLICATE, &target)); menu.append_section(None, &change); let file = gio::Menu::new(); - file.append_submenu(Some(&crate::i18n::gettext("Colour")), &color_menu()); - file.append_submenu(Some(&crate::i18n::gettext("Tags")), &tag_menu(&target, tags)); + file.append_submenu(Some(&crate::i18n::gettext("Colou_r")), &color_menu()); + file.append_submenu(Some(&crate::i18n::gettext("Tag_s")), &tag_menu(&target, tags)); file.append_submenu( - Some(&crate::i18n::gettext("Move to Group")), + Some(&crate::i18n::gettext("_Move to Group")), &group_menu(&target, groups), ); menu.append_section(None, &file); let danger = gio::Menu::new(); // Named as the dialog it opens names it. - danger.append_item(&item(&crate::i18n::gettext("Delete…"), PICKER, DELETE, &target)); + danger.append_item(&item(&crate::i18n::gettext("Dele_te…"), PICKER, DELETE, &target)); menu.append_section(None, &danger); menu } @@ -103,14 +103,19 @@ fn item(label: &str, group: &str, action: &str, target: &glib::Variant) -> gio:: fn color_menu() -> gio::Menu { let menu = gio::Menu::new(); menu.append_item(&item( - &crate::i18n::gettext("No Colour"), + &crate::i18n::gettext("_No Colour"), ROW, ROW_COLOR, &NONE.to_variant(), )); let colors = gio::Menu::new(); for color in ConnectionColor::ALL { - colors.append_item(&item(&color_name(color), ROW, ROW_COLOR, &color.id().to_variant())); + colors.append_item(&item( + &color_menu_label(color), + ROW, + ROW_COLOR, + &color.id().to_variant(), + )); } menu.append_section(None, &colors); menu @@ -121,7 +126,7 @@ fn color_menu() -> gio::Menu { fn group_menu(target: &glib::Variant, groups: &[ConnectionGroupName]) -> gio::Menu { let menu = gio::Menu::new(); menu.append_item(&item( - &crate::i18n::gettext("No Group"), + &crate::i18n::gettext("_No Group"), ROW, ROW_GROUP, &NONE.to_variant(), @@ -139,7 +144,7 @@ fn group_menu(target: &glib::Variant, groups: &[ConnectionGroupName]) -> gio::Me menu.append_section(None, &existing); } let new = gio::Menu::new(); - new.append_item(&item(&crate::i18n::gettext("New Group…"), PICKER, NEW_GROUP, target)); + new.append_item(&item(&crate::i18n::gettext("New _Group…"), PICKER, NEW_GROUP, target)); menu.append_section(None, &new); menu } @@ -159,9 +164,9 @@ fn tag_menu(target: &glib::Variant, tags: &[ConnectionTag]) -> gio::Menu { menu.append_section(None, &existing); } let change = gio::Menu::new(); - change.append_item(&item(&crate::i18n::gettext("New Tag…"), PICKER, NEW_TAG, target)); + change.append_item(&item(&crate::i18n::gettext("_New Tag…"), PICKER, NEW_TAG, target)); change.append_item(&gio::MenuItem::new( - Some(&crate::i18n::gettext("Manage Tags…")), + Some(&crate::i18n::gettext("_Manage Tags…")), Some(MANAGE_TAGS), )); menu.append_section(None, &change); @@ -171,6 +176,7 @@ fn tag_menu(target: &glib::Variant, tags: &[ConnectionTag]) -> gio::Menu { #[cfg(test)] mod tests { use super::*; + use crate::test_support::assert_access_keys; /// Each item's label, action and target, in order, sections and /// submenus flattened. @@ -206,10 +212,10 @@ mod tests { let found = items(connection_menu(id, &[], &[]).upcast_ref()); let first = found.first().expect("an item"); - assert_eq!(first.0, "Connect"); + assert_eq!(first.0, "_Connect"); assert_eq!(first.1.as_deref(), Some("picker.connect")); let last = found.last().expect("an item"); - assert_eq!(last.0, "Delete…"); + assert_eq!(last.0, "Dele_te…"); assert_eq!(last.1.as_deref(), Some("picker.delete")); for (label, action, target) in &found { if action.as_deref().is_some_and(|action| action.starts_with("picker.")) { @@ -222,6 +228,11 @@ mod tests { } } + #[test] + fn every_item_has_an_access_key_of_its_own() { + assert_access_keys(&connection_menu(Uuid::new_v4(), &[], &[])); + } + fn group(name: &str) -> ConnectionGroupName { ConnectionGroupName::new(name).expect("a group name") } @@ -237,13 +248,13 @@ mod tests { }; let order = [ - shown("Connect"), - shown("Edit…"), - shown("Duplicate"), - shown("Colour"), - shown("Tags"), - shown("Move to Group"), - shown("Delete…"), + shown("_Connect"), + shown("_Edit…"), + shown("D_uplicate"), + shown("Colou_r"), + shown("Tag_s"), + shown("_Move to Group"), + shown("Dele_te…"), ]; assert!(order.windows(2).all(|pair| pair[0] < pair[1]), "{labels:?}"); @@ -258,8 +269,8 @@ mod tests { let labels = labels(&connection_menu_with(&lead, id, &[], &[])); assert_eq!(labels.first().map(String::as_str), Some("Disconnect")); - assert!(!labels.contains(&"Connect".to_owned()), "{labels:?}"); - assert_eq!(labels.last().map(String::as_str), Some("Delete…")); + assert!(!labels.contains(&"_Connect".to_owned()), "{labels:?}"); + assert_eq!(labels.last().map(String::as_str), Some("Dele_te…")); let (action, target) = connect_action(id); assert_eq!(action, "picker.connect"); assert_eq!(target.get::(), Some(id.to_string())); @@ -275,8 +286,8 @@ mod tests { assert!(labels.contains(&"Archive".to_owned()), "{labels:?}"); assert!(labels.contains(&"Work".to_owned()), "{labels:?}"); - assert!(labels.contains(&"No Group".to_owned()), "{labels:?}"); - assert!(labels.contains(&"New Group…".to_owned()), "{labels:?}"); + assert!(labels.contains(&"_No Group".to_owned()), "{labels:?}"); + assert!(labels.contains(&"New _Group…".to_owned()), "{labels:?}"); } #[test] @@ -308,11 +319,11 @@ mod tests { .expect("the tag's item"); assert_eq!(check.1.as_deref(), Some(format!("picker-row.tag-{}", tag.id).as_str())); assert_eq!(check.2, None, "a check item carries no target"); - let new = found.iter().find(|(label, ..)| label == "New Tag…").expect("New Tag…"); + let new = found.iter().find(|(label, ..)| label == "_New Tag…").expect("New Tag…"); assert_eq!(new.1.as_deref(), Some("picker.new-tag")); let manage = found .iter() - .find(|(label, ..)| label == "Manage Tags…") + .find(|(label, ..)| label == "_Manage Tags…") .expect("Manage Tags…"); assert_eq!(manage.1.as_deref(), Some(MANAGE_TAGS)); } diff --git a/linux/crates/app/src/ui/safe_mode.rs b/linux/crates/app/src/ui/safe_mode.rs index 8af5d5b742..7251d886e5 100644 --- a/linux/crates/app/src/ui/safe_mode.rs +++ b/linux/crates/app/src/ui/safe_mode.rs @@ -8,6 +8,15 @@ pub(crate) fn label(level: SafeModeLevel) -> String { } } +// The window title and the connect dialog show `label`, which has no access key. +pub(crate) fn menu_label(level: SafeModeLevel) -> String { + match level { + SafeModeLevel::Off => crate::i18n::gettext("_Off"), + SafeModeLevel::ConfirmWrites => crate::i18n::gettext("_Confirm Writes"), + SafeModeLevel::ReadOnly => crate::i18n::gettext("_Read-Only"), + } +} + /// What the level does on this engine. Read-Only's guarantee differs by /// engine, so the row says which one the user is getting, naming an /// engine that cannot hold a connection to reads, an engine of keys is diff --git a/linux/crates/app/src/ui/saved_queries_dialog.rs b/linux/crates/app/src/ui/saved_queries_dialog.rs index 77f055c7b6..bbf3e74ee6 100644 --- a/linux/crates/app/src/ui/saved_queries_dialog.rs +++ b/linux/crates/app/src/ui/saved_queries_dialog.rs @@ -329,21 +329,7 @@ impl SavedQueriesDialog { let sender_for_activate = sender.clone(); row.connect_activated(move |_| sender_for_activate.deliver(SavedQueriesDialogInput::Activate(id))); - let menu = gio::Menu::new(); - let open_section = gio::Menu::new(); - open_section.append(Some(&crate::i18n::gettext("Open in New Tab")), Some("saved.open")); - open_section.append( - Some(&crate::i18n::gettext("Replace Current Tab")), - Some("saved.replace"), - ); - open_section.append(Some(&crate::i18n::gettext("Copy SQL")), Some("saved.copy")); - menu.append_section(None, &open_section); - let edit_section = gio::Menu::new(); - edit_section.append(Some(&crate::i18n::gettext("Rename…")), Some("saved.rename")); - menu.append_section(None, &edit_section); - let danger_section = gio::Menu::new(); - danger_section.append(Some(&crate::i18n::gettext("Delete")), Some("saved.delete")); - menu.append_section(None, &danger_section); + let menu = row_menu(); let actions = gio::SimpleActionGroup::new(); let entry = |name: &str, message: fn(i64) -> SavedQueriesDialogInput, sender: ComponentSender| { @@ -480,6 +466,25 @@ fn matches(saved: &SavedQuery, needle: &str) -> bool { || saved.connection_name.to_lowercase().contains(needle) } +fn row_menu() -> gio::Menu { + let menu = gio::Menu::new(); + let open_section = gio::Menu::new(); + open_section.append(Some(&crate::i18n::gettext("_Open in New Tab")), Some("saved.open")); + open_section.append( + Some(&crate::i18n::gettext("_Replace Current Tab")), + Some("saved.replace"), + ); + open_section.append(Some(&crate::i18n::gettext("_Copy SQL")), Some("saved.copy")); + menu.append_section(None, &open_section); + let edit_section = gio::Menu::new(); + edit_section.append(Some(&crate::i18n::gettext("Re_name…")), Some("saved.rename")); + menu.append_section(None, &edit_section); + let danger_section = gio::Menu::new(); + danger_section.append(Some(&crate::i18n::gettext("_Delete")), Some("saved.delete")); + menu.append_section(None, &danger_section); + menu +} + #[cfg(test)] mod tests { use super::*; @@ -572,6 +577,11 @@ mod tests { assert!(!matches(&query, "invoices")); } + #[test] + fn every_row_menu_item_has_an_access_key_of_its_own() { + crate::test_support::assert_access_keys(&row_menu()); + } + #[test] fn a_search_ignores_case_and_surrounding_space() { assert!(matches(&saved("Daily", "SELECT 1", "local"), " daily ")); diff --git a/linux/crates/app/src/ui/workspace/sidebar/sidebar_menu.rs b/linux/crates/app/src/ui/workspace/sidebar/sidebar_menu.rs index 048b0aabce..2bae0311cd 100644 --- a/linux/crates/app/src/ui/workspace/sidebar/sidebar_menu.rs +++ b/linux/crates/app/src/ui/workspace/sidebar/sidebar_menu.rs @@ -25,24 +25,24 @@ pub(super) const GROUP: &str = "sidebar"; pub(crate) fn object_menu(key: &ObjectKey, kind: ObjectKind, affordances: SidebarAffordances) -> gio::Menu { let menu = gio::Menu::new(); let open = gio::Menu::new(); - open.append_item(&item(&crate::i18n::gettext("Open in New Tab"), OPEN_IN_NEW_TAB, key)); + open.append_item(&item(&crate::i18n::gettext("_Open in New Tab"), OPEN_IN_NEW_TAB, key)); // A key has no SQL to show, no structure to change and no diagram. if !kind.is_browsable() { menu.append_section(None, &open); return menu; } if affordances.speaks_sql { - open.append_item(&item(&crate::i18n::gettext("Show ER Diagram"), SHOW_DIAGRAM, key)); + open.append_item(&item(&crate::i18n::gettext("Show ER _Diagram"), SHOW_DIAGRAM, key)); } menu.append_section(None, &open); let table = kind.is_structure_editable(); let structure = gio::Menu::new(); if table && affordances.can_edit_structure { - structure.append_item(&item(&crate::i18n::gettext("Edit Structure"), EDIT_STRUCTURE, key)); + structure.append_item(&item(&crate::i18n::gettext("_Edit Structure"), EDIT_STRUCTURE, key)); } if affordances.speaks_sql { structure.append_item(&item( - &crate::i18n::gettext("Show CREATE TABLE"), + &crate::i18n::gettext("Show _CREATE TABLE"), SHOW_CREATE_TABLE, key, )); @@ -52,11 +52,11 @@ pub(crate) fn object_menu(key: &ObjectKey, kind: ObjectKind, affordances: Sideba return menu; } let data = gio::Menu::new(); - data.append_item(&item(&crate::i18n::gettext("Import CSV…"), IMPORT_CSV, key)); + data.append_item(&item(&crate::i18n::gettext("_Import CSV…"), IMPORT_CSV, key)); menu.append_section(None, &data); if affordances.can_edit_structure { let danger = gio::Menu::new(); - danger.append_item(&item(&crate::i18n::gettext("Drop Table…"), DROP_TABLE, key)); + danger.append_item(&item(&crate::i18n::gettext("Drop _Table…"), DROP_TABLE, key)); menu.append_section(None, &danger); } menu @@ -74,12 +74,12 @@ pub(crate) fn schema_menu(schema: Option<&str>, affordances: SidebarAffordances, let menu = gio::Menu::new(); let target = schema.map(str::to_owned).to_variant(); if affordances.offers_new_table() { - let item = gio::MenuItem::new(Some(&crate::i18n::gettext("New Table…")), None); + let item = gio::MenuItem::new(Some(&crate::i18n::gettext("_New Table…")), None); item.set_action_and_target_value(Some(&format!("{GROUP}.{NEW_TABLE}")), Some(&target)); menu.append_item(&item); } if affordances.speaks_sql { - let item = gio::MenuItem::new(Some(&crate::i18n::gettext("Show ER Diagram")), None); + let item = gio::MenuItem::new(Some(&crate::i18n::gettext("Show ER _Diagram")), None); item.set_action_and_target_value(Some(&format!("{GROUP}.{SHOW_SCHEMA_DIAGRAM}")), Some(&target)); menu.append_item(&item); } @@ -113,6 +113,7 @@ mod tests { use tablepro_core::meta::TableRef; use super::*; + use crate::test_support::assert_access_keys; /// Each item's action and target, in menu order, sections flattened. fn items(menu: &gio::MenuModel) -> Vec<(String, Option)> { @@ -159,6 +160,14 @@ mod tests { } } + #[test] + fn every_item_has_an_access_key_of_its_own() { + let key = ObjectKey(TableRef::new(Some("sales".to_owned()), "orders")); + + assert_access_keys(&object_menu(&key, ObjectKind::Table, all())); + assert_access_keys(&schema_menu(Some("sales"), all(), Holds::Tables).expect("a menu")); + } + #[test] fn a_read_only_connection_is_offered_nothing_that_writes() { let key = ObjectKey(TableRef::unqualified("orders")); diff --git a/linux/crates/app/src/ui/workspace/sidebar/tree_menu.rs b/linux/crates/app/src/ui/workspace/sidebar/tree_menu.rs index 34d4c8a2b0..9af84cd7dc 100644 --- a/linux/crates/app/src/ui/workspace/sidebar/tree_menu.rs +++ b/linux/crates/app/src/ui/workspace/sidebar/tree_menu.rs @@ -39,27 +39,67 @@ pub(super) fn session_section(key: &SessionKey, state: InWindow, opens_databases }; match state { InWindow::Open => { - add(crate::i18n::gettext("New Query"), NEW_QUERY_IN); - add(crate::i18n::gettext("Refresh"), REFRESH_IN); + add(crate::i18n::gettext("_New Query"), NEW_QUERY_IN); + add(crate::i18n::gettext("Re_fresh"), REFRESH_IN); if opens_databases { - add(crate::i18n::gettext("Open Database…"), OPEN_DATABASE_IN); + add(crate::i18n::gettext("_Open Database…"), OPEN_DATABASE_IN); } - add(crate::i18n::gettext("Disconnect"), DISCONNECT_SESSION); + add(crate::i18n::gettext("_Disconnect"), DISCONNECT_SESSION); } // A connect can wait for a host that never answers. - InWindow::Connecting => add(crate::i18n::gettext("Stop Connecting"), STOP_CONNECTING), + InWindow::Connecting => add(crate::i18n::gettext("Stop _Connecting"), STOP_CONNECTING), InWindow::Closed { failed, stored } => { if stored && let SessionKey::Saved(id) = key { menu.append_item(&crate::ui::picker::connect_item(*id)); } if failed { - add(crate::i18n::gettext("Show Error"), SHOW_CONNECT_FAILURE); + add(crate::i18n::gettext("S_how Error"), SHOW_CONNECT_FAILURE); } // Its row is only the failure's; nothing else removes it. if failed && !stored { - add(crate::i18n::gettext("Remove"), FORGET_CONNECT_FAILURE); + add(crate::i18n::gettext("_Remove"), FORGET_CONNECT_FAILURE); } } } menu } + +#[cfg(test)] +mod tests { + use uuid::Uuid; + + use super::*; + use crate::test_support::assert_access_keys; + + #[test] + fn every_state_keeps_the_access_keys_of_the_connections_menu_apart() { + let id = Uuid::new_v4(); + let key = SessionKey::Saved(id); + let stored = [ + InWindow::Open, + InWindow::Connecting, + InWindow::Closed { + failed: true, + stored: true, + }, + ]; + + for state in stored { + assert_access_keys(&crate::ui::picker::connection_menu_with( + &session_section(&key, state, true), + id, + &[], + &[], + )); + } + // No saved connection names this one, so the section is the menu. + assert_access_keys(&session_section( + &key, + InWindow::Closed { + failed: true, + stored: false, + }, + true, + )); + } +} diff --git a/linux/crates/app/src/ui/workspace/workspace_shell.rs b/linux/crates/app/src/ui/workspace/workspace_shell.rs index a44182b629..3e43714b71 100644 --- a/linux/crates/app/src/ui/workspace/workspace_shell.rs +++ b/linux/crates/app/src/ui/workspace/workspace_shell.rs @@ -202,15 +202,7 @@ impl WorkspaceShell { shell.dim_tab_menu(view, page); } }); - let others = gio::Menu::new(); - others.append(Some(&gettext("Close Other Tabs")), Some("tab.close-others")); - others.append(Some(&gettext("Close Tabs After This One")), Some("tab.close-after")); - let close = gio::Menu::new(); - close.append(Some(&gettext("Close")), Some("tab.close")); - let menu = gio::Menu::new(); - menu.append_section(None, &others); - menu.append_section(None, &close); - imp.tab_view.set_menu_model(Some(&menu)); + imp.tab_view.set_menu_model(Some(&tab_menu())); } /// Dim what would close nothing for `page`, whose menu opens. @@ -718,12 +710,24 @@ fn assistant_action(shell: &WorkspaceShell) -> gio::SimpleAction { action } +fn tab_menu() -> gio::Menu { + let others = gio::Menu::new(); + others.append(Some(&gettext("Close _Other Tabs")), Some("tab.close-others")); + others.append(Some(&gettext("Close Tabs _After This One")), Some("tab.close-after")); + let close = gio::Menu::new(); + close.append(Some(&gettext("_Close")), Some("tab.close")); + let menu = gio::Menu::new(); + menu.append_section(None, &others); + menu.append_section(None, &close); + menu +} + /// The main menu with the session's Safe Mode levels on top, as radio /// items of the window's stateful `safe-mode` action. fn with_safe_mode(menu: &gio::MenuModel) -> gio::Menu { let levels = gio::Menu::new(); for level in tablepro_core::SafeModeLevel::ALL { - let item = gio::MenuItem::new(Some(&crate::ui::safe_mode::label(level)), None); + let item = gio::MenuItem::new(Some(&crate::ui::safe_mode::menu_label(level)), None); item.set_action_and_target_value( Some("win.safe-mode"), Some(&crate::ui::safe_mode::action_target(level).to_variant()), @@ -731,7 +735,7 @@ fn with_safe_mode(menu: &gio::MenuModel) -> gio::Menu { levels.append_item(&item); } let section = gio::Menu::new(); - section.append_submenu(Some(&gettext("Safe Mode")), &levels); + section.append_submenu(Some(&gettext("_Safe Mode")), &levels); let whole = gio::Menu::new(); whole.append_section(None, §ion); whole.append_section(None, menu); @@ -758,3 +762,20 @@ fn follow_sidebar(split_view: &adw::OverlaySplitView, toggle: >k::ToggleButton ); visible.bind(toggle, "visible", Some(split_view)) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::config::RESOURCE_BASE_PATH; + use crate::test_support::assert_access_keys; + + #[gtk4::test] + fn the_tab_menu_and_the_main_menu_give_each_item_its_own_access_key() { + crate::register_test_resources(); + let builder = gtk::Builder::from_resource(&format!("{RESOURCE_BASE_PATH}/gtk/menus.ui")); + let main: gio::MenuModel = builder.object(PRIMARY_MENU).expect("the main menu"); + + assert_access_keys(&tab_menu()); + assert_access_keys(&with_safe_mode(&main)); + } +} diff --git a/linux/data/resources/gtk/menus.ui b/linux/data/resources/gtk/menus.ui index 9517677366..bb552aa9fb 100644 --- a/linux/data/resources/gtk/menus.ui +++ b/linux/data/resources/gtk/menus.ui @@ -5,11 +5,11 @@
- New Connection… + _New Connection… win.new-connection - New Window + New _Window app.new-window
@@ -17,48 +17,48 @@ - Query History + Query _History win.show-history action-missing - Saved Queries + Saved _Queries win.show-saved-queries action-missing - Disconnect + _Disconnect win.disconnect action-disabled
- Import Connections… + _Import Connections… win.import-connections - Export Connections… + _Export Connections… win.export-connections - Manage Tags… + _Manage Tags… win.manage-tags
- Preferences + _Preferences app.preferences - Keyboard Shortcuts + _Keyboard Shortcuts app.shortcuts - About TablePro + _About TablePro app.about
@@ -66,11 +66,11 @@
- Reopen Closed Tab + _Reopen Closed Tab win.reopen-closed-tab - Close All Tabs + Close _All Tabs win.close-all-tabs
diff --git a/linux/data/resources/ui/assistant-pane.ui b/linux/data/resources/ui/assistant-pane.ui index 794c4e87ec..2d7515d046 100644 --- a/linux/data/resources/ui/assistant-pane.ui +++ b/linux/data/resources/ui/assistant-pane.ui @@ -5,7 +5,7 @@
- New Conversation + _New Conversation assistant.new-conversation
diff --git a/linux/data/resources/ui/diagram-tab.ui b/linux/data/resources/ui/diagram-tab.ui index c9b0d8d12d..ca347d82b5 100644 --- a/linux/data/resources/ui/diagram-tab.ui +++ b/linux/data/resources/ui/diagram-tab.ui @@ -5,18 +5,18 @@
- Reset Layout + _Reset Layout diagram.reset-layout
- Export as PNG… + Export as _PNG… diagram.export png - Export as SVG… + Export as _SVG… diagram.export svg diff --git a/linux/data/resources/ui/find-bar.ui b/linux/data/resources/ui/find-bar.ui index 2372f4aaf3..6e4bd8ccf5 100644 --- a/linux/data/resources/ui/find-bar.ui +++ b/linux/data/resources/ui/find-bar.ui @@ -5,15 +5,15 @@
- Match Case + Match _Case find.case-sensitive - Match Whole Word + Match _Whole Word find.at-word-boundaries - Regular Expressions + _Regular Expressions find.regex-enabled
diff --git a/linux/data/resources/ui/history-dialog.ui b/linux/data/resources/ui/history-dialog.ui index 6ac5c71284..c5bc8a783d 100644 --- a/linux/data/resources/ui/history-dialog.ui +++ b/linux/data/resources/ui/history-dialog.ui @@ -156,13 +156,13 @@
- Show in Files + _Show in Files history.show-in-files
- Clear All History… + _Clear All History… history.clear-all
@@ -170,11 +170,11 @@
- Export as SQL… + Export as _SQL… history.export-sql - Export as CSV… + Export as _CSV… history.export-csv
diff --git a/linux/data/resources/ui/row-detail-pane.ui b/linux/data/resources/ui/row-detail-pane.ui index cc1c2f8c1d..a0465b692c 100644 --- a/linux/data/resources/ui/row-detail-pane.ui +++ b/linux/data/resources/ui/row-detail-pane.ui @@ -6,24 +6,24 @@
- Set to Empty + Set _to Empty grid.set-empty action-disabled - Set to NULL + Set to _NULL grid.set-null action-disabled - Set to DEFAULT + Set to DEFAU_LT grid.set-default action-disabled
- Copy Value + _Copy Value grid.copy
diff --git a/linux/data/resources/ui/sql-editor.ui b/linux/data/resources/ui/sql-editor.ui index 49da15b076..915c431572 100644 --- a/linux/data/resources/ui/sql-editor.ui +++ b/linux/data/resources/ui/sql-editor.ui @@ -6,57 +6,57 @@
- Open SQL File… + _Open SQL File… editor.open-file <Primary>o - Save + _Save editor.save <Primary>s - Save As… + Save _As… editor.save-as <Primary><Shift>s
- Save Query… + Save _Query… win.save-query - Format + Forma_t editor.format <Primary><Shift>f
- Explain with AI + Explain _with AI editor.ai-explain action-disabled - Review with AI + Re_view with AI editor.ai-review action-disabled - Optimize with AI + Opti_mize with AI editor.ai-optimize action-disabled
- Find… + _Find… editor.find <Primary>f - Find and Replace… + Find and _Replace… editor.find-replace <Primary>h @@ -66,17 +66,17 @@
- Explain with AI + Explain _with AI editor.ai-explain action-disabled - Review with AI + Re_view with AI editor.ai-review action-disabled - Optimize with AI + Opti_mize with AI editor.ai-optimize action-disabled diff --git a/linux/po/POTFILES.in b/linux/po/POTFILES.in index fbe9b45667..291befa8f1 100644 --- a/linux/po/POTFILES.in +++ b/linux/po/POTFILES.in @@ -126,6 +126,7 @@ crates/app/src/services/tracker_delta.rs crates/app/src/services/tracker_edit.rs crates/app/src/services/undo_op.rs crates/app/src/storage.rs +crates/app/src/test_support/access_keys.rs crates/app/src/test_support/accessible_label.rs crates/app/src/test_support/alert_response.rs crates/app/src/test_support/application_end.rs diff --git a/linux/po/vi.po b/linux/po/vi.po index bd48f3856e..608e7b216e 100644 --- a/linux/po/vi.po +++ b/linux/po/vi.po @@ -180,6 +180,10 @@ msgstr "Chùm khóa không lưu mật khẩu." msgid "Insert Row" msgstr "Thêm dòng" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_Insert Row" +msgstr "Thê_m dòng" + #: crates/app/src/ui/browse_tab/browse_actions.rs #: crates/app/src/ui/browse_tab/selection_handlers.rs msgid "Delete Selected Rows" @@ -190,6 +194,10 @@ msgstr "Xóa các dòng đã chọn" msgid "Export Rows…" msgstr "Xuất các dòng…" +#: crates/app/src/ui/browse_tab/layout.rs +msgid "_Export Rows…" +msgstr "_Xuất các dòng…" + #: crates/app/src/ui/browse_tab/clipboard_handlers.rs #: crates/app/src/ui/editor/mod.rs #: crates/app/src/ui/structure_tab/save_handlers.rs @@ -254,6 +262,12 @@ msgstr "Hủy" msgid "Delete" msgstr "Xóa" +#: crates/app/src/ui/data_grid/grid_menu.rs +#: crates/app/src/ui/history_dialog/mod.rs +#: crates/app/src/ui/saved_queries_dialog.rs +msgid "_Delete" +msgstr "_Xóa" + #: crates/app/src/ui/browse_tab/edit_handlers.rs msgid "" "The table's primary key changed, so no rows were marked for deletion. Select " @@ -268,11 +282,19 @@ msgstr "" msgid "Filter Rows" msgstr "Lọc dòng" +#: crates/app/src/ui/browse_tab/layout.rs +msgid "_Filter Rows" +msgstr "_Lọc dòng" + #: crates/app/src/ui/browse_tab/layout.rs #: crates/app/src/ui/browse_tab/pager_bar.rs data/resources/ui/pager-bar.ui msgid "Count Rows" msgstr "Đếm dòng" +#: crates/app/src/ui/browse_tab/layout.rs +msgid "_Count Rows" +msgstr "Đế_m dòng" + #: crates/app/src/ui/browse_tab/layout.rs msgid "Rows per Page" msgstr "Số dòng mỗi trang" @@ -667,6 +689,10 @@ msgstr "Nhập mật khẩu cơ sở dữ liệu cho {target}." msgid "Connect" msgstr "Kết nối" +#: crates/app/src/ui/picker/connection_menu.rs +msgid "_Connect" +msgstr "_Kết nối" + #: crates/app/src/ui/credential_prompt/prompt_text.rs #, rust-format msgid "Enter the SSH password for {target}." @@ -2547,14 +2573,26 @@ msgstr "Tùy chọn CSV" msgid "Rows" msgstr "Dòng" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_Rows" +msgstr "Dòn_g" + #: crates/app/src/ui/export/dialog.rs data/resources/ui/sql-editor.ui msgid "Format" msgstr "Định dạng" +#: data/resources/ui/sql-editor.ui +msgid "Forma_t" +msgstr "Định _dạng" + #: crates/app/src/ui/export/dialog.rs crates/app/src/ui/grid/grid_menus.rs msgid "Export…" msgstr "Xuất…" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "E_xport…" +msgstr "Xuấ_t…" + #: crates/app/src/ui/export/dialog.rs msgid "Reset to Defaults" msgstr "Khôi phục mặc định" @@ -2706,6 +2744,10 @@ msgstr "Không tạo được tệp." msgid "Show in Files" msgstr "Hiện trong Tệp" +#: data/resources/ui/history-dialog.ui +msgid "_Show in Files" +msgstr "_Hiện trong Tệp" + #: crates/app/src/ui/export/export_failure_text.rs #: crates/app/src/ui/history_dialog/history_export.rs msgid "Couldn't Export" @@ -2899,78 +2941,81 @@ msgstr[0] "… (thêm {count} byte)" msgid "Required" msgstr "Bắt buộc" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Edit Cell" -msgstr "Sửa ô" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_Edit Cell" +msgstr "Sử_a ô" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Set to Empty" -msgstr "Đặt thành rỗng" +#: crates/app/src/ui/data_grid/grid_menu.rs +#: data/resources/ui/row-detail-pane.ui +msgid "Set _to Empty" +msgstr "Đặt thành rỗn_g" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Set to NULL" -msgstr "Đặt thành NULL" +#: crates/app/src/ui/data_grid/grid_menu.rs +#: data/resources/ui/row-detail-pane.ui +msgid "Set to _NULL" +msgstr "Đặt thành _NULL" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Set to DEFAULT" -msgstr "Đặt thành DEFAULT" +#: crates/app/src/ui/data_grid/grid_menu.rs +#: data/resources/ui/row-detail-pane.ui +msgid "Set to DEFAU_LT" +msgstr "Đặt thành DEFAU_LT" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "With Headers" -msgstr "Kèm tiêu đề" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_With Headers" +msgstr "Kè_m tiêu đề" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "JSON" -msgstr "JSON" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_JSON" +msgstr "_JSON" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "CSV" -msgstr "CSV" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_CSV" +msgstr "_CSV" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "CSV With Headers" -msgstr "CSV kèm tiêu đề" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "CSV With _Headers" +msgstr "C_SV kèm tiêu đề" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Markdown" -msgstr "Markdown" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_Markdown" +msgstr "Ma_rkdown" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "IN Clause" -msgstr "Mệnh đề IN" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_IN Clause" +msgstr "Mệnh đề _IN" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "INSERT Statement" -msgstr "Câu lệnh INSERT" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "INSERT _Statement" +msgstr "Câu lện_h INSERT" #: crates/app/src/ui/grid/grid_menus.rs #: crates/app/src/ui/grid/row_json_dialog.rs msgid "Copy" msgstr "Sao chép" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Copy As" -msgstr "Sao chép dưới dạng" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_Copy" +msgstr "Sao c_hép" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Copy Column Name" -msgstr "Sao chép tên cột" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "Copy _As" +msgstr "Sao chép _dưới dạng" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Show Row as JSON" -msgstr "Hiện dòng dạng JSON" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "Copy Column _Name" +msgstr "Sao chép tên _cột" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Sort Ascending" -msgstr "Sắp xếp tăng dần" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "Show Row as _JSON" +msgstr "Hiện dòng dạng _JSON" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Sort Descending" -msgstr "Sắp xếp giảm dần" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "Sort _Ascending" +msgstr "Sắp xếp _tăng dần" -#: crates/app/src/ui/grid/grid_menus.rs -msgid "Clear Sort" -msgstr "Bỏ sắp xếp" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "_Clear Sort" +msgstr "_Bỏ sắp xếp" #: crates/app/src/ui/grid/grid_menus.rs data/resources/shortcuts-dialog.ui msgid "Widen Column" @@ -2984,10 +3029,10 @@ msgstr "Thu hẹp cột" msgid "Reset Column Width" msgstr "Đặt lại độ rộng cột" -#: crates/app/src/ui/grid/grid_menus.rs +#: crates/app/src/ui/data_grid/grid_menu.rs #: crates/app/src/ui/picker/connection_menu.rs -msgid "Duplicate" -msgstr "Nhân bản" +msgid "D_uplicate" +msgstr "Nhân _bản" #: crates/app/src/ui/grid/row_json_dialog.rs #, rust-format @@ -3116,11 +3161,19 @@ msgstr[0] "{n} ngày trước" msgid "Unpin" msgstr "Bỏ ghim" +#: crates/app/src/ui/history_dialog/mod.rs +msgid "_Unpin" +msgstr "_Bỏ ghim" + #: crates/app/src/ui/history_dialog/history_row.rs #: crates/app/src/ui/history_dialog/mod.rs msgid "Pin" msgstr "Ghim" +#: crates/app/src/ui/history_dialog/mod.rs +msgid "_Pin" +msgstr "_Ghim" + #: crates/app/src/ui/history_dialog/mod.rs #, rust-format msgid "{n} selected" @@ -3214,13 +3267,13 @@ msgstr "Mọi truy vấn" #: crates/app/src/ui/history_dialog/mod.rs #: crates/app/src/ui/saved_queries_dialog.rs #: crates/app/src/ui/workspace/sidebar/sidebar_menu.rs -msgid "Open in New Tab" -msgstr "Mở trong tab mới" +msgid "_Open in New Tab" +msgstr "_Mở trong tab mới" #: crates/app/src/ui/history_dialog/mod.rs #: crates/app/src/ui/saved_queries_dialog.rs -msgid "Replace Current Tab" -msgstr "Thay thế tab hiện tại" +msgid "_Replace Current Tab" +msgstr "_Thay thế tab hiện tại" #: crates/app/src/ui/history_dialog/mod.rs #: crates/app/src/ui/saved_queries_dialog.rs @@ -3228,6 +3281,11 @@ msgstr "Thay thế tab hiện tại" msgid "Copy SQL" msgstr "Sao chép SQL" +#: crates/app/src/ui/history_dialog/mod.rs +#: crates/app/src/ui/saved_queries_dialog.rs +msgid "_Copy SQL" +msgstr "Sao _chép SQL" + #: crates/app/src/ui/host_key_review.rs #: crates/app/src/ui/session_window/connections.rs #: crates/app/src/ui/tab_load/stale_banner.rs @@ -3490,69 +3548,109 @@ msgid "No values in the file" msgstr "Không có giá trị nào trong tệp" #: crates/app/src/ui/picker/connection_menu.rs -msgid "Edit…" -msgstr "Sửa…" +msgid "_Edit…" +msgstr "Sử_a…" #: crates/app/src/ui/picker/connection_menu.rs msgid "Colour" msgstr "Màu" #: crates/app/src/ui/picker/connection_menu.rs -msgid "Move to Group" -msgstr "Di chuyển vào nhóm" +msgid "Colou_r" +msgstr "_Màu" #: crates/app/src/ui/picker/connection_menu.rs -msgid "Delete…" -msgstr "Xóa…" +msgid "_Move to Group" +msgstr "_Di chuyển vào nhóm" + +#: crates/app/src/ui/picker/connection_menu.rs +msgid "Dele_te…" +msgstr "_Xóa…" #: crates/app/src/ui/picker/connection_menu.rs -msgid "No Colour" -msgstr "Không màu" +msgid "_No Colour" +msgstr "_Không màu" #: crates/app/src/ui/picker/connection_menu.rs -msgid "No Group" -msgstr "Không nhóm" +msgid "_No Group" +msgstr "_Không nhóm" #: crates/app/src/ui/picker/connection_menu.rs -msgid "New Group…" -msgstr "Nhóm mới…" +msgid "New _Group…" +msgstr "_Nhóm mới…" #: crates/app/src/ui/picker/connection_menu.rs msgid "Blue" msgstr "Xanh dương" +#: crates/app/src/ui/connection_appearance.rs +msgid "_Blue" +msgstr "Xanh _dương" + #: crates/app/src/ui/picker/connection_menu.rs msgid "Teal" msgstr "Xanh mòng két" +#: crates/app/src/ui/connection_appearance.rs +msgid "_Teal" +msgstr "Xanh mòn_g két" + #: crates/app/src/ui/picker/connection_menu.rs msgid "Green" msgstr "Xanh lá" +#: crates/app/src/ui/connection_appearance.rs +msgid "_Green" +msgstr "Xanh _lá" + #: crates/app/src/ui/picker/connection_menu.rs msgid "Yellow" msgstr "Vàng" +#: crates/app/src/ui/connection_appearance.rs +msgid "_Yellow" +msgstr "_Vàng" + #: crates/app/src/ui/picker/connection_menu.rs msgid "Orange" msgstr "Cam" +#: crates/app/src/ui/connection_appearance.rs +msgid "_Orange" +msgstr "_Cam" + #: crates/app/src/ui/picker/connection_menu.rs msgid "Red" msgstr "Đỏ" +#: crates/app/src/ui/connection_appearance.rs +msgid "Re_d" +msgstr "_Đỏ" + #: crates/app/src/ui/picker/connection_menu.rs msgid "Pink" msgstr "Hồng" +#: crates/app/src/ui/connection_appearance.rs +msgid "Pin_k" +msgstr "_Hồng" + #: crates/app/src/ui/picker/connection_menu.rs msgid "Purple" msgstr "Tím" +#: crates/app/src/ui/connection_appearance.rs +msgid "_Purple" +msgstr "_Tím" + #: crates/app/src/ui/picker/connection_menu.rs msgid "Slate" msgstr "Xám đá" +#: crates/app/src/ui/connection_appearance.rs +msgid "_Slate" +msgstr "_Xám đá" + #: crates/app/src/ui/picker/connection_row.rs msgid "Connection Options" msgstr "Tùy chọn kết nối" @@ -3648,14 +3746,26 @@ msgstr "Kết quả {n} ({rows})" msgid "Off" msgstr "Tắt" +#: crates/app/src/ui/safe_mode.rs +msgid "_Off" +msgstr "_Tắt" + #: crates/app/src/ui/safe_mode.rs msgid "Confirm Writes" msgstr "Xác nhận ghi" +#: crates/app/src/ui/safe_mode.rs +msgid "_Confirm Writes" +msgstr "_Xác nhận ghi" + #: crates/app/src/ui/safe_mode.rs msgid "Read-Only" msgstr "Chỉ đọc" +#: crates/app/src/ui/safe_mode.rs +msgid "_Read-Only" +msgstr "C_hỉ đọc" + #: crates/app/src/ui/safe_mode.rs msgid "Asks only before DROP, TRUNCATE, and DELETE or UPDATE without WHERE." msgstr "Chỉ hỏi trước DROP, TRUNCATE, và DELETE hoặc UPDATE không có WHERE." @@ -3688,6 +3798,10 @@ msgstr "" msgid "Saved Queries" msgstr "Truy vấn đã lưu" +#: data/resources/gtk/menus.ui +msgid "Saved _Queries" +msgstr "Truy _vấn đã lưu" + #: crates/app/src/ui/saved_queries_dialog.rs msgid "Search saved queries" msgstr "Tìm truy vấn đã lưu" @@ -3713,8 +3827,8 @@ msgid "Try a different search term." msgstr "Hãy thử từ khóa tìm kiếm khác." #: crates/app/src/ui/saved_queries_dialog.rs -msgid "Rename…" -msgstr "Đổi tên…" +msgid "Re_name…" +msgstr "Đổ_i tên…" #: crates/app/src/ui/saved_queries_dialog.rs msgid "Saved Query Options" @@ -4671,6 +4785,10 @@ msgstr "Hiện thanh bên" msgid "New Query" msgstr "Truy vấn mới" +#: crates/app/src/ui/workspace/sidebar/tree_menu.rs +msgid "_New Query" +msgstr "_Truy vấn mới" + #: crates/app/src/ui/workspace/shell.rs msgid "View Open Tabs" msgstr "Xem các tab đang mở" @@ -4679,6 +4797,10 @@ msgstr "Xem các tab đang mở" msgid "Safe Mode" msgstr "Chế độ an toàn" +#: crates/app/src/ui/workspace/workspace_shell.rs +msgid "_Safe Mode" +msgstr "_Chế độ an toàn" + #: crates/app/src/ui/workspace/sidebar/mod.rs #, rust-format msgid "Could not refresh the table list: {error}" @@ -4689,27 +4811,31 @@ msgstr "Không làm mới được danh sách bảng: {error}" msgid "New Table in {schema}…" msgstr "Bảng mới trong {schema}…" -#: crates/app/src/ui/workspace/sidebar/sidebar_header_factory.rs -msgid "New Table…" -msgstr "Bảng mới…" +#: crates/app/src/ui/workspace/sidebar/sidebar_menu.rs +msgid "_New Table…" +msgstr "_Bảng mới…" #: crates/app/src/ui/workspace/sidebar/sidebar_menu.rs -msgid "Edit Structure" -msgstr "Sửa cấu trúc" +msgid "_Edit Structure" +msgstr "_Sửa cấu trúc" #: crates/app/src/ui/workspace/sidebar/sidebar_menu.rs -msgid "Show CREATE TABLE" -msgstr "Hiện CREATE TABLE" +msgid "Show _CREATE TABLE" +msgstr "_Hiện CREATE TABLE" #: crates/app/src/ui/workspace/sidebar/sidebar_menu.rs -msgid "Import CSV…" -msgstr "Nhập CSV…" +msgid "_Import CSV…" +msgstr "_Nhập CSV…" #: crates/app/src/ui/workspace/sidebar/sidebar_menu.rs #: data/resources/ui/structure-tab.ui msgid "Drop Table…" msgstr "Xóa bảng…" +#: crates/app/src/ui/workspace/sidebar/sidebar_menu.rs +msgid "Drop _Table…" +msgstr "_Xóa bảng…" + #: crates/app/src/ui/workspace/sidebar/sidebar_object.rs msgctxt "object kind" msgid "View" @@ -4927,9 +5053,9 @@ msgstr "" msgid "Discard and Refresh" msgstr "Loại bỏ và làm mới" -#: crates/app/src/ui/workspace/workspace_tabs.rs -msgid "Close Other Tabs" -msgstr "Đóng các tab khác" +#: crates/app/src/ui/workspace/workspace_shell.rs +msgid "Close _Other Tabs" +msgstr "Đóng các tab _khác" #: crates/app/src/ui/workspace/workspace_tabs.rs msgid "Close Tabs to the Right" @@ -5209,6 +5335,10 @@ msgstr "" msgid "New Window" msgstr "Cửa sổ mới" +#: data/resources/gtk/menus.ui +msgid "New _Window" +msgstr "Cửa _sổ mới" + #: data/app.tablepro.TablePro.gschema.xml.in msgid "Window width" msgstr "Độ rộng cửa sổ" @@ -5517,34 +5647,51 @@ msgstr "Xuất kết quả và bảng sang CSV, JSON, SQL và các định dạn msgid "Query History" msgstr "Lịch sử truy vấn" +#: data/resources/gtk/menus.ui +msgid "Query _History" +msgstr "_Lịch sử truy vấn" + #: data/resources/gtk/menus.ui msgid "Disconnect" msgstr "Ngắt kết nối" +#: crates/app/src/ui/workspace/sidebar/tree_menu.rs +#: data/resources/gtk/menus.ui +msgid "_Disconnect" +msgstr "Ngắt _kết nối" + #: data/resources/gtk/menus.ui -msgid "Import Connections…" -msgstr "Nhập kết nối…" +msgid "_Import Connections…" +msgstr "N_hập kết nối…" #: data/resources/gtk/menus.ui -msgid "Export Connections…" -msgstr "Xuất kết nối…" +msgid "_Export Connections…" +msgstr "_Xuất kết nối…" #: data/resources/gtk/menus.ui data/resources/shortcuts-dialog.ui #: data/resources/ui/preferences-dialog.ui msgid "Preferences" msgstr "Tùy chỉnh" +#: data/resources/gtk/menus.ui +msgid "_Preferences" +msgstr "_Tùy chỉnh" + #: data/resources/gtk/menus.ui data/resources/shortcuts-dialog.ui msgid "Keyboard Shortcuts" msgstr "Phím tắt" #: data/resources/gtk/menus.ui -msgid "About TablePro" -msgstr "Giới thiệu TablePro" +msgid "_Keyboard Shortcuts" +msgstr "_Phím tắt" #: data/resources/gtk/menus.ui -msgid "Reopen Closed Tab" -msgstr "Mở lại tab đã đóng" +msgid "_About TablePro" +msgstr "_Giới thiệu TablePro" + +#: data/resources/gtk/menus.ui +msgid "_Reopen Closed Tab" +msgstr "_Mở lại tab đã đóng" #: data/resources/shortcuts-dialog.ui #: data/resources/ui/preferences-general-page.ui @@ -5619,6 +5766,10 @@ msgstr "Làm lại thay đổi gần nhất" msgid "Select All Rows" msgstr "Chọn tất cả các dòng" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "Select _All Rows" +msgstr "_Chọn tất cả các dòng" + #: data/resources/shortcuts-dialog.ui msgid "Clear the Selection" msgstr "Bỏ chọn" @@ -5828,6 +5979,10 @@ msgstr "Các bản xuất đang chạy" msgid "Column" msgstr "Cột" +#: crates/app/src/ui/data_grid/grid_menu.rs +msgid "Colu_mn" +msgstr "_Cột" + #: data/resources/ui/filter-rule-row.ui msgid "Operator" msgstr "Toán tử" @@ -5871,16 +6026,16 @@ msgid "Add Rule" msgstr "Thêm quy tắc" #: data/resources/ui/find-bar.ui -msgid "Match Case" -msgstr "Phân biệt hoa thường" +msgid "Match _Case" +msgstr "Phân biệt _hoa thường" #: data/resources/ui/find-bar.ui -msgid "Match Whole Word" -msgstr "Khớp nguyên từ" +msgid "Match _Whole Word" +msgstr "_Khớp nguyên từ" #: data/resources/ui/find-bar.ui -msgid "Regular Expressions" -msgstr "Biểu thức chính quy" +msgid "_Regular Expressions" +msgstr "_Biểu thức chính quy" #: data/resources/ui/find-bar.ui msgid "Replace with" @@ -5919,16 +6074,16 @@ msgid "Export" msgstr "Xuất" #: data/resources/ui/history-dialog.ui -msgid "Clear All History…" -msgstr "Xóa toàn bộ lịch sử…" +msgid "_Clear All History…" +msgstr "_Xóa toàn bộ lịch sử…" #: data/resources/ui/history-dialog.ui -msgid "Export as SQL…" -msgstr "Xuất dưới dạng SQL…" +msgid "Export as _SQL…" +msgstr "Xuất dưới dạng _SQL…" #: data/resources/ui/history-dialog.ui -msgid "Export as CSV…" -msgstr "Xuất dưới dạng CSV…" +msgid "Export as _CSV…" +msgstr "Xuất dưới dạng _CSV…" #: data/resources/ui/history-row.ui msgid "Select Query" @@ -6062,24 +6217,24 @@ msgid "Run a query to see what it returns." msgstr "Chạy một truy vấn để xem nó trả về gì." #: data/resources/ui/sql-editor.ui -msgid "Open SQL File…" -msgstr "Mở tệp SQL…" +msgid "_Open SQL File…" +msgstr "Mở tệp _SQL…" #: data/resources/ui/sql-editor.ui -msgid "Save As…" -msgstr "Lưu thành…" +msgid "Save _As…" +msgstr "Lư_u thành…" #: data/resources/ui/sql-editor.ui -msgid "Save Query…" -msgstr "Lưu truy vấn…" +msgid "Save _Query…" +msgstr "Lưu t_ruy vấn…" #: data/resources/ui/sql-editor.ui -msgid "Find…" -msgstr "Tìm…" +msgid "_Find…" +msgstr "_Tìm…" #: data/resources/ui/sql-editor.ui -msgid "Find and Replace…" -msgstr "Tìm và thay thế…" +msgid "Find and _Replace…" +msgstr "Tìm và t_hay thế…" #: data/resources/ui/sql-editor.ui msgid "Editor Options" From 998f3cb44d0bb7e02975822da21ca4cffe6b9351 Mon Sep 17 00:00:00 2001 From: Cristian Date: Sat, 31 Oct 2026 15:56:10 +0100 Subject: [PATCH 46/85] feat(linux-editor): complete a moved query tab from its database's own session (#3903) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3903.added.md | 1 + .../completion/sql_completion_provider.rs | 16 +++- .../completion/sql_completion_provider_imp.rs | 3 + linux/crates/app/src/ui/editor/tab_scope.rs | 35 +++++++ linux/crates/app/src/ui/editor/tests.rs | 51 ++++++++++ linux/crates/app/src/ui/mod.rs | 2 +- linux/crates/app/src/ui/session_window/mod.rs | 3 +- .../src/ui/session_window/other_databases.rs | 96 ++++++++++++++++++- .../ui/session_window/session_window_input.rs | 18 ++++ .../session_window/tests/other_databases.rs | 92 +++++++++++++++++- linux/crates/app/src/ui/workspace/mod.rs | 10 ++ .../app/src/ui/workspace/sidebar/mod.rs | 23 +++-- .../app/src/ui/workspace/tab_factory.rs | 6 ++ .../app/src/ui/workspace/workspace_shell.rs | 7 ++ .../app/src/ui/workspace/workspace_tabs.rs | 36 ++++++- 15 files changed, 383 insertions(+), 16 deletions(-) create mode 100644 linux/changelog.d/3903.added.md diff --git a/linux/changelog.d/3903.added.md b/linux/changelog.d/3903.added.md new file mode 100644 index 0000000000..1a3d1351da --- /dev/null +++ b/linux/changelog.d/3903.added.md @@ -0,0 +1 @@ +A query tab moved to another PostgreSQL or SQL Server database completes from that database's tables, from a session opened beside the connection. diff --git a/linux/crates/app/src/ui/editor/completion/sql_completion_provider.rs b/linux/crates/app/src/ui/editor/completion/sql_completion_provider.rs index bebf5a2564..3b755a6a03 100644 --- a/linux/crates/app/src/ui/editor/completion/sql_completion_provider.rs +++ b/linux/crates/app/src/ui/editor/completion/sql_completion_provider.rs @@ -49,6 +49,11 @@ impl SqlCompletionProvider { self.imp().home.replace(home); } + /// Read `session`'s catalog, the tab's database's, while it is open. + pub fn set_catalog_session(&self, session: Option>) { + self.imp().catalog_session.replace(session); + } + pub fn new(session: ActiveSession, tasks: Tasks, snippets: Rc) -> Self { let provider: Self = glib::Object::new(); let _ = provider.imp().deps.set(ProviderDeps { @@ -72,7 +77,16 @@ impl SqlCompletionProvider { let ranking = match (request.session.clone(), tasks.clone()) { (Some(session), Some(tasks)) if request.grammar.is_sql() => { let fallback = request.clone(); - let home = self.imp().home.borrow().clone(); + let followed = self + .imp() + .catalog_session + .borrow() + .clone() + .filter(|followed| !followed.is_shut_down()); + let (session, home) = match followed { + Some(followed) => (followed, CatalogHome::Connection), + None => (session, self.imp().home.borrow().clone()), + }; let ranked_for = needle.clone(); let blocking = tasks.clone(); tasks diff --git a/linux/crates/app/src/ui/editor/completion/sql_completion_provider_imp.rs b/linux/crates/app/src/ui/editor/completion/sql_completion_provider_imp.rs index 710220ea4c..f7363d0725 100644 --- a/linux/crates/app/src/ui/editor/completion/sql_completion_provider_imp.rs +++ b/linux/crates/app/src/ui/editor/completion/sql_completion_provider_imp.rs @@ -20,6 +20,9 @@ const PRIORITY: i32 = 100; pub struct SqlCompletionProvider { pub deps: OnceCell, pub home: std::cell::RefCell, + /// The session the window holds for the tab's database, whose catalog + /// is that database's own. + pub catalog_session: std::cell::RefCell>>, } #[glib::object_subclass] diff --git a/linux/crates/app/src/ui/editor/tab_scope.rs b/linux/crates/app/src/ui/editor/tab_scope.rs index 3a796c89bc..c31317fd51 100644 --- a/linux/crates/app/src/ui/editor/tab_scope.rs +++ b/linux/crates/app/src/ui/editor/tab_scope.rs @@ -1,6 +1,8 @@ //! The database the tab works on, named in the editor bar, and the menu //! that moves the tab to another of the server's databases. +use std::sync::Arc; + use gtk4::prelude::*; use gtk4::subclass::prelude::*; use gtk4::{gio, glib}; @@ -90,6 +92,9 @@ impl SqlEditor { self.follow_in_completion(catalog_home(layout, own.as_deref(), &database)); let moved = self.imp().named_database.replace(Some(database.clone())) != Some(database.clone()); if moved { + // The last database's session answers no more; the window + // hands over this one's. + self.follow_catalog(None); self.emit_by_name::<()>("database-changed", &[]); } if let Some(action) = super::imp_action(self, MOVE_ACTION) { @@ -231,6 +236,36 @@ impl SqlEditor { ) } + /// Completion reads `session`'s catalog: the session the window holds + /// for the tab's database, or `None` back on the connection's own. + pub(crate) fn follow_catalog(&self, session: Option>) { + // A session opened late for a database the tab has since left + // changes nothing; the one it is in may have answered first. + let current = self.imp().named_database.borrow().clone(); + if session + .as_ref() + .is_some_and(|session| session.database() != current.as_deref()) + { + return; + } + for provider in self.imp().completion_providers.borrow().iter() { + if let Some(provider) = provider.downcast_ref::() { + provider.set_catalog_session(session.clone()); + } + } + } + + /// The database of the session whose catalog completion reads, when + /// the window handed one over. + #[cfg(test)] + pub(crate) fn followed_catalog(&self) -> Option { + self.imp().completion_providers.borrow().iter().find_map(|provider| { + let provider = provider.downcast_ref::()?; + let session = provider.imp().catalog_session.borrow().clone()?; + session.database().map(str::to_owned) + }) + } + /// Where unqualified names in this tab resolve, for what the assistant /// says about it. pub(crate) fn catalog_home(&self) -> CatalogHome { diff --git a/linux/crates/app/src/ui/editor/tests.rs b/linux/crates/app/src/ui/editor/tests.rs index 7217fc22f4..da55fc009e 100644 --- a/linux/crates/app/src/ui/editor/tests.rs +++ b/linux/crates/app/src/ui/editor/tests.rs @@ -2964,3 +2964,54 @@ fn a_tab_holding_a_transaction_stays_where_it_is() { assert!(!action.is_enabled(), "the tab could leave its transaction"); } + +fn session_in(database: &str, tasks: Tasks) -> Arc { + let mut request = FakeConnector::network_request("db.example", 3306).expect("a request"); + if let tablepro_session::connect::RequestTarget::Network { database: opened, .. } = &mut request.target { + *opened = Some(database.to_owned()); + } + let (pending, _) = FakeConnector::pending_session(request, ConnectionScript::default()); + Session::start( + pending, + tablepro_session::SessionDeps { + connector: Arc::new(FakeConnector::new([])), + credentials: Arc::new(FakeCredentialSource::new()), + deadlines: Arc::new(DefaultDeadlines::new(tablepro_core::LivenessPolicy::DESKTOP)), + tasks, + prompter: None, + }, + ) +} + +fn followed_catalog(harness: &Harness) -> Option { + harness.editor.followed_catalog() +} + +/// Completion reads the session the window hands over only while the tab +/// is in that session's database, and lets it go when the tab moves. +#[gtk4::test] +fn completion_reads_only_the_session_of_the_tabs_database() { + let harness = databases_editor(answering(), false); + let analytics = session_in("analytics", harness.runtime.tasks()); + + harness.editor.follow_catalog(Some(analytics.clone())); + assert_eq!( + followed_catalog(&harness), + None, + "a session opened late for another database" + ); + + pick_database(&harness, "analytics"); + harness.editor.follow_catalog(Some(analytics)); + assert_eq!(followed_catalog(&harness).as_deref(), Some("analytics")); + let archive = session_in("archive", harness.runtime.tasks()); + harness.editor.follow_catalog(Some(archive)); + assert_eq!( + followed_catalog(&harness).as_deref(), + Some("analytics"), + "a late session for a database the tab left replaced the one it is in" + ); + + pick_database(&harness, "shop"); + assert_eq!(followed_catalog(&harness), None, "the last database's catalog stayed"); +} diff --git a/linux/crates/app/src/ui/mod.rs b/linux/crates/app/src/ui/mod.rs index 9f04d9deb5..561eee59dd 100644 --- a/linux/crates/app/src/ui/mod.rs +++ b/linux/crates/app/src/ui/mod.rs @@ -56,5 +56,5 @@ pub(crate) mod write_confirmation; pub(crate) use menu_label::menu_label; pub(crate) use plain_toast::plain_toast; pub(crate) use preferences_dialog::PreferencesDialog; -pub(crate) use session_window::{LaunchNotice, SessionWindow, SessionWindowInit, SessionWindowInput}; +pub(crate) use session_window::{LaunchNotice, MovedTab, SessionWindow, SessionWindowInit, SessionWindowInput}; pub(crate) use structure_tab::review_dialog; diff --git a/linux/crates/app/src/ui/session_window/mod.rs b/linux/crates/app/src/ui/session_window/mod.rs index dd8f494994..e8e56634ab 100644 --- a/linux/crates/app/src/ui/session_window/mod.rs +++ b/linux/crates/app/src/ui/session_window/mod.rs @@ -67,7 +67,7 @@ pub(super) use self::picker_page::PickerPage; use self::resolve_file_identity::{FileResolveError, FileTarget, ResolvedFile, resolve_file_blocking}; use self::session_page::{FailedConnect, OpenSession, PendingConnect, SessionPage, source_key}; use self::session_window_command::{AttemptError, ConnectFailure, Connected, SessionWindowCommand}; -pub(crate) use self::session_window_input::SessionWindowInput; +pub(crate) use self::session_window_input::{MovedTab, SessionWindowInput}; use crate::application::{AppServices, WindowHost, WindowLaunch}; use crate::runtime::{DeliverInput, InputSender}; use crate::services::session_context::SessionContext; @@ -469,6 +469,7 @@ impl SessionWindow { SessionWindowInput::Disconnect => self.on_disconnect_front(), SessionWindowInput::DisconnectSession(key) => self.on_disconnect(key), SessionWindowInput::ChooseDatabaseIn(key) => self.choose_database_in(key), + SessionWindowInput::MovedTab(asked) => self.on_moved_tab(asked), SessionWindowInput::SessionTeardownApproved(key) => self.on_session_teardown_approved(&key), SessionWindowInput::ShowSession(key) => self.show_session(&key), SessionWindowInput::NewQueryIn(key) => self.deliver_to(&key, WorkspaceInput::NewEditorTab), diff --git a/linux/crates/app/src/ui/session_window/other_databases.rs b/linux/crates/app/src/ui/session_window/other_databases.rs index 8d41ac01fc..a54f32caf6 100644 --- a/linux/crates/app/src/ui/session_window/other_databases.rs +++ b/linux/crates/app/src/ui/session_window/other_databases.rs @@ -12,16 +12,19 @@ use gtk4::subclass::prelude::*; use gtk4::{self as gtk, glib}; use libadwaita as adw; use libadwaita::prelude::*; +use tablepro_core::dialect::DatabaseLayout; use tablepro_core::meta::SchemaInfo; use tablepro_session::operation::OperationKind; use tablepro_session::{ConnectError, Session, SessionClaim, SessionKey}; use tokio_util::sync::{CancellationToken, DropGuard}; use uuid::Uuid; -use super::SessionWindow; use super::session_page::OpenSession; +use super::{MovedTab, SessionWindow}; use crate::i18n::{gettext, gettext_f}; +use crate::runtime::DeliverInput; use crate::ui::error_text::{connect_error_message, operation_error_message}; +use crate::ui::workspace::{Workspace, WorkspaceInput}; /// The response of the chooser that opens the database picked. const OPEN: &str = "open"; @@ -69,6 +72,15 @@ impl OtherDatabases { } } +/// The saved connection a session belongs to, its own or another of +/// its databases. +fn connection_of(key: &SessionKey) -> Option { + match key { + SessionKey::Saved(id) | SessionKey::SavedIn { id, .. } => Some(*id), + SessionKey::File(_) => None, + } +} + /// The databases to offer: neither the connection's own nor one open /// already, the user's first and the server's own after, as the editor /// bar's menu lists them. @@ -186,7 +198,8 @@ impl SessionWindow { drop(session.begin_shutdown()); return; }; - { + let joined = session.clone(); + let others = { let mut sessions = self.imp().sessions.borrow_mut(); let Some(connection) = sessions .iter_mut() @@ -196,15 +209,92 @@ impl SessionWindow { drop(session.begin_shutdown()); return; }; + let others: Vec = connection.with_databases().map(|open| open.workspace.clone()).collect(); connection .databases .open .push(OpenSession::new(claim, session, workspace.clone())); - } + others + }; self.render(); shell.add_session(&workspace); workspace.start(); self.sync_actions(); + // The connection's tabs in that database read its catalog now. + for other in others { + if let Some(input) = other.sender() { + input.deliver(WorkspaceInput::DatabaseOpened(joined.clone())); + } + } + } + + /// What an editor tab that moved to another database asks of the + /// window. + pub(super) fn on_moved_tab(&self, asked: MovedTab) { + match asked { + MovedTab::Follow { key, tab_id, database } => self.follow_database(&key, tab_id, database), + MovedTab::Refresh { key, database } => self.refresh_database(&key, database), + } + } + + /// Editor tab `tab_id` of the session under `key` moved to + /// `database`, `None` for the connection's own. Its completion reads + /// the session the window holds for that database, opened beside + /// the connection where a connection's catalog holds one database. + fn follow_database(&self, key: &SessionKey, tab_id: Uuid, database: Option) { + // A tab of another database's own workspace keeps that catalog: + // the connection's session may not know the name of its own. + let SessionKey::Saved(id) = *key else { + return; + }; + if !self.opens_databases(id) { + return; + } + let session = match self.holding(id, database.as_deref()) { + // The tab's own workspace reads that catalog already. + Some((held, _)) if &held == key => None, + Some((_, session)) => Some(session), + None => { + if let Some(database) = database { + self.open_database(id, database); + } + return; + } + }; + self.deliver_to(key, WorkspaceInput::FollowCatalog { tab_id, session }); + } + + /// A tab of the session under `key` changed the catalog of + /// `database`: the session holding it reads its catalog again. + fn refresh_database(&self, key: &SessionKey, database: String) { + let Some(id) = connection_of(key) else { + return; + }; + if let Some((held, _)) = self.holding(id, Some(&database)).filter(|(held, _)| held != key) { + self.deliver_to(&held, WorkspaceInput::RefreshCatalog); + } + } + + /// The key and session the window holds for the connection `id`'s + /// database `database`, `None` for its own. + fn holding(&self, id: Uuid, database: Option<&str>) -> Option<(SessionKey, Arc)> { + self.find_open(|open| { + let holds = match (open.claim.key(), database) { + (SessionKey::Saved(of), None) => *of == id, + (SessionKey::Saved(of), Some(name)) => *of == id && open.session.database() == Some(name), + (SessionKey::SavedIn { id: of, database: held }, Some(name)) => *of == id && held == name, + (SessionKey::SavedIn { .. } | SessionKey::File(_), _) => false, + }; + holds.then(|| (open.claim.key().clone(), open.session.clone())) + }) + } + + /// Whether the connection `id`'s catalog holds only its own database, + /// so another one needs a session of its own (0030). + fn opens_databases(&self, id: Uuid) -> bool { + self.held_session(&SessionKey::Saved(id)) + .and_then(|session| session.current_dialect()) + .is_some_and(|dialect| dialect.database_layout() == DatabaseLayout::WithSchemas) } /// Open Database…: list the server's databases, then ask which to diff --git a/linux/crates/app/src/ui/session_window/session_window_input.rs b/linux/crates/app/src/ui/session_window/session_window_input.rs index b592a9d193..d628ac0d80 100644 --- a/linux/crates/app/src/ui/session_window/session_window_input.rs +++ b/linux/crates/app/src/ui/session_window/session_window_input.rs @@ -72,6 +72,7 @@ pub(crate) enum SessionWindowInput { /// Open Database… on a connection's row: pick another of its server's /// databases to open beside it. ChooseDatabaseIn(SessionKey), + MovedTab(MovedTab), /// The prompts approved closing the session under the key. SessionTeardownApproved(SessionKey), /// Bring the session under the key to the front. @@ -169,3 +170,20 @@ pub(crate) enum SessionWindowInput { ImportPreviewed(Box), ShowToast(String), } + +/// What an editor tab that moved to another database asks of the window +/// (0030). +#[derive(Debug)] +pub(crate) enum MovedTab { + /// Tab `tab_id` of the session under `key` moved to `database`, + /// `None` for its connection's own: its completion reads that + /// database's session. + Follow { + key: SessionKey, + tab_id: uuid::Uuid, + database: Option, + }, + /// A tab of the session under `key` changed the catalog of + /// `database`, which another session of its connection may hold. + Refresh { key: SessionKey, database: String }, +} diff --git a/linux/crates/app/src/ui/session_window/tests/other_databases.rs b/linux/crates/app/src/ui/session_window/tests/other_databases.rs index 187f8e8ff2..b17ac249b7 100644 --- a/linux/crates/app/src/ui/session_window/tests/other_databases.rs +++ b/linux/crates/app/src/ui/session_window/tests/other_databases.rs @@ -6,7 +6,7 @@ use tablepro_session::SessionKey; use tablepro_session::fakes::FakeEstablish; use tablepro_test_support::{ConnectionScript, FakeReply}; -use super::{Script, TestWindow, WAIT, pick_level}; +use super::{Script, TestWindow, WAIT, editors, pick_level}; use crate::test_support::{drain_main_context, wait_until}; use crate::ui::SessionWindowInput; @@ -16,13 +16,22 @@ const ANALYTICS: &str = "analytics"; /// databases opens with, unasked. `establish` is what each connect gets /// before the test's connection from then on. fn network_window(name: &str, establish: Vec) -> TestWindow { - let test = TestWindow::with_script( + network_window_with( name, Script { - network: true, establish, ..Script::default() }, + ) +} + +fn network_window_with(name: &str, script: Script) -> TestWindow { + let test = TestWindow::with_script( + name, + Script { + network: true, + ..script + }, ); let (vault, id) = (test.vault.clone(), test.saved.id); crate::test_support::finish_on_main(WAIT, async move { @@ -182,3 +191,80 @@ fn a_level_picked_in_another_database_moves_the_connection() { } assert_eq!(test.connector.calls().len(), 2, "a level that writes reconnected"); } + +fn on_engine(name: &str, dialect: std::sync::Arc) -> TestWindow { + network_window_with( + name, + Script { + connection: ConnectionScript { + dialect, + ..ConnectionScript::default() + }, + ..Script::default() + }, + ) +} + +/// Each editor tab of the connection, `count` of them, moved to +/// `ANALYTICS` from the editor bar. +fn move_new_tabs(test: &TestWindow, count: usize) -> Vec { + let connection = crate::ui::session_target::to_variant(&test.key()); + for _ in 0..count { + WidgetExt::activate_action(&test.window, "win.new-query-in", Some(&connection)).expect("win.new-query-in"); + } + wait_until(WAIT, || editors(&test.window).len() == count).expect("the tabs"); + let tabs = editors(&test.window); + for editor in &tabs { + WidgetExt::activate_action(editor, "editor.database", Some(&ANALYTICS.to_variant())).expect("editor.database"); + } + tabs +} + +/// Tabs moved to a database whose engine's catalog holds only the +/// connection's own read that database's session, which opens once for +/// all of them. +#[gtk4::test] +fn tabs_moved_to_another_database_read_its_session() { + let test = on_engine( + "FollowDatabase", + std::sync::Arc::new(tablepro_driver_postgres::PgDialect::new( + &tablepro_core::ServerInfo::new("PostgreSQL", "18.0", 18, 0, 0), + )), + ); + connect(&test); + let sibling = analytics(&test); + + let tabs = move_new_tabs(&test, 2); + + wait_until(WAIT, || test.window.held_session(&sibling).is_some()).expect("the database to open"); + wait_until(WAIT, || { + tabs.iter() + .all(|editor| editor.followed_catalog().as_deref() == Some(ANALYTICS)) + }) + .expect("every moved tab to read the database's catalog"); + assert_eq!(test.connector.calls().len(), 2, "the database opened once per tab"); +} + +/// An engine whose connection lists every database already opens no +/// session for a moved tab. +#[gtk4::test] +fn a_tab_moved_where_the_catalog_lists_every_database_opens_nothing() { + let test = on_engine( + "FollowDatabaseListed", + std::sync::Arc::new(tablepro_driver_mysql::MySqlDialect::new( + tablepro_driver_mysql::MySqlFlavour::MySql, + &tablepro_core::ServerInfo::new("MySQL", "8.4.0", 8, 4, 0), + )), + ); + connect(&test); + + let tabs = move_new_tabs(&test, 1); + drain_main_context(); + + assert!( + test.window.held_session(&analytics(&test)).is_none(), + "the database opened" + ); + assert_eq!(test.connector.calls().len(), 1, "a session opened for the database"); + assert_eq!(tabs[0].followed_catalog(), None); +} diff --git a/linux/crates/app/src/ui/workspace/mod.rs b/linux/crates/app/src/ui/workspace/mod.rs index 5ecb86f536..f5443be7a1 100644 --- a/linux/crates/app/src/ui/workspace/mod.rs +++ b/linux/crates/app/src/ui/workspace/mod.rs @@ -518,6 +518,14 @@ pub(crate) enum WorkspaceInput { ResetWorkspaceLayout, /// The session moved to another Safe Mode level, which the title names. SafeModeChanged, + /// Completion in editor tab `tab_id` reads `session`'s catalog, its + /// database's, or its own connection's again with `None`. + FollowCatalog { + tab_id: Uuid, + session: Option>, + }, + /// Another database of the connection opened beside it. + DatabaseOpened(Arc), /// The saved connections changed, the title's icon perhaps with them. SavedConnectionsChanged, WorkspaceLayoutReset(Result, crate::workspace::WorkspaceSaveError>), @@ -1278,6 +1286,8 @@ impl WorkspaceModel { WorkspaceInput::EditorTabContentChanged(id) => self.on_editor_tab_content_changed(id), WorkspaceInput::EditorTabFileChanged(id) => self.on_editor_tab_file_changed(id), WorkspaceInput::EditorTabDatabaseChanged(id) => self.on_editor_tab_database_changed(id), + WorkspaceInput::FollowCatalog { tab_id, session } => self.follow_catalog(tab_id, session), + WorkspaceInput::DatabaseOpened(session) => self.on_database_opened(&session), WorkspaceInput::WorkspaceLoaded(loaded) => self.on_workspace_loaded(loaded), WorkspaceInput::ResetWorkspaceLayout => self.on_reset_workspace_layout(), WorkspaceInput::SafeModeChanged => self.refresh_title(), diff --git a/linux/crates/app/src/ui/workspace/sidebar/mod.rs b/linux/crates/app/src/ui/workspace/sidebar/mod.rs index 8b95752d96..4c1028b49e 100644 --- a/linux/crates/app/src/ui/workspace/sidebar/mod.rs +++ b/linux/crates/app/src/ui/workspace/sidebar/mod.rs @@ -273,12 +273,23 @@ impl WorkspaceModel { /// A run that sent DDL, or a commit that may publish it, changes what /// the sidebar and the completion list. pub(super) fn on_editor_run_finished(&mut self, id: uuid::Uuid) { - let changed = matches!( - self.workspace_tabs.borrow().get(&id), - Some(super::WorkspaceTab::Editor(slot)) if slot.editor.last_run_changes_catalog() - ); - if changed { - self.refresh_catalog(); + let moved_to = match self.workspace_tabs.borrow().get(&id) { + Some(super::WorkspaceTab::Editor(slot)) if slot.editor.last_run_changes_catalog() => { + Some(slot.editor.tab_database()) + } + _ => None, + }; + let Some(moved_to) = moved_to else { + return; + }; + self.refresh_catalog(); + // The statement ran in the tab's database, whose session the + // window may hold beside this one (0030). + if let (Some(database), Some(shell)) = (moved_to, self.shell.upgrade()) { + shell.tell_window(crate::ui::SessionWindowInput::MovedTab(crate::ui::MovedTab::Refresh { + key: self.key.clone(), + database, + })); } } diff --git a/linux/crates/app/src/ui/workspace/tab_factory.rs b/linux/crates/app/src/ui/workspace/tab_factory.rs index b6e2d8eefa..534da9791f 100644 --- a/linux/crates/app/src/ui/workspace/tab_factory.rs +++ b/linux/crates/app/src/ui/workspace/tab_factory.rs @@ -185,10 +185,16 @@ impl TabFactory { close_timeout: parts.context.close_timeout(), snippets: parts.snippets.clone(), }); + let moved = database.is_some(); if let Some(database) = database { editor.move_to_database(&database); } connect_editor(&editor, id, &parts.input, &parts.toasts); + // It moved before the workspace heard it, which follows it once + // the tab is in. + if moved { + parts.input.deliver(WorkspaceInput::EditorTabDatabaseChanged(id)); + } editor.attach_assistant(parts.assistant.clone()); // After the signals are connected, so the workspace marks a tab // restored with unsaved changes. diff --git a/linux/crates/app/src/ui/workspace/workspace_shell.rs b/linux/crates/app/src/ui/workspace/workspace_shell.rs index 3e43714b71..7ca4992ec1 100644 --- a/linux/crates/app/src/ui/workspace/workspace_shell.rs +++ b/linux/crates/app/src/ui/workspace/workspace_shell.rs @@ -66,6 +66,13 @@ pub(super) fn describe_buttons(shell: &WorkspaceShell) { } impl WorkspaceShell { + /// Hand `input` to the window that holds the shell. + pub(crate) fn tell_window(&self, input: SessionWindowInput) { + if let Some(sender) = self.imp().sender.get() { + sender.deliver(input); + } + } + /// A shell for `window`, with what it needs beyond the template: the /// connection tree and the picker's actions its rows name, the main /// menu, the window's actions, the tab menu and the overview's new diff --git a/linux/crates/app/src/ui/workspace/workspace_tabs.rs b/linux/crates/app/src/ui/workspace/workspace_tabs.rs index 2775d02249..cbabdaee1f 100644 --- a/linux/crates/app/src/ui/workspace/workspace_tabs.rs +++ b/linux/crates/app/src/ui/workspace/workspace_tabs.rs @@ -1,6 +1,7 @@ use std::cell::RefCell; use std::collections::{HashMap, HashSet}; use std::rc::Rc; +use std::sync::Arc; use gtk4 as gtk; use gtk4::glib; @@ -8,7 +9,7 @@ use libadwaita as adw; use libadwaita::prelude::*; use tablepro_core::meta::TableRef; -use tablepro_session::SessionKey; +use tablepro_session::{Session, SessionKey}; use tablepro_storage::{DraftId, DraftScope}; use uuid::Uuid; @@ -535,6 +536,39 @@ impl WorkspaceModel { pub(super) fn on_editor_tab_database_changed(&self, id: Uuid) { self.retitle_tab(id); self.persist_workspace_state(); + self.follow_editor_database(id); + } + + /// Ask the window for the session of the database tab `tab_id` moved + /// to, whose catalog its completion reads (0030). + fn follow_editor_database(&self, tab_id: Uuid) { + let database = match self.workspace_tabs.borrow().get(&tab_id) { + Some(WorkspaceTab::Editor(slot)) => slot.editor.tab_database(), + _ => return, + }; + if let Some(shell) = self.shell.upgrade() { + shell.tell_window(crate::ui::SessionWindowInput::MovedTab(crate::ui::MovedTab::Follow { + key: self.key.clone(), + tab_id, + database, + })); + } + } + + pub(super) fn follow_catalog(&self, tab_id: Uuid, session: Option>) { + if let Some(WorkspaceTab::Editor(slot)) = self.workspace_tabs.borrow().get(&tab_id) { + slot.editor.follow_catalog(session); + } + } + + /// Another database of the connection opened: each editor tab in it + /// reads its catalog. + pub(super) fn on_database_opened(&self, session: &Arc) { + for tab in self.workspace_tabs.borrow().values() { + if let WorkspaceTab::Editor(slot) = tab { + slot.editor.follow_catalog(Some(session.clone())); + } + } } pub(super) fn on_editor_tab_run_state_changed(&self, id: Uuid, running: bool) { From 5884849605be33c3f179dfd2d9eff8d2c1a07d2c Mon Sep 17 00:00:00 2001 From: Cristian Date: Sat, 31 Oct 2026 18:07:02 +0100 Subject: [PATCH 47/85] feat(linux-session): reopen a connection's other databases at launch and when a failed connect is retried (#3905) Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat --- linux/changelog.d/3905.added.md | 1 + .../app/src/ui/session_window/connections.rs | 26 +-- linux/crates/app/src/ui/session_window/imp.rs | 6 +- .../app/src/ui/session_window/locate_file.rs | 3 +- linux/crates/app/src/ui/session_window/mod.rs | 178 ++++++++++++++--- .../src/ui/session_window/other_databases.rs | 10 +- .../app/src/ui/session_window/secret_retry.rs | 4 +- .../app/src/ui/session_window/session_page.rs | 10 + .../src/ui/session_window/session_restore.rs | 186 +++++++++++++++++- .../session_window/tests/other_databases.rs | 33 +++- .../crates/app/src/workspace/open_sessions.rs | 37 +++- .../app/src/workspace/open_sessions_record.rs | 44 ++++- 12 files changed, 475 insertions(+), 63 deletions(-) create mode 100644 linux/changelog.d/3905.added.md diff --git a/linux/changelog.d/3905.added.md b/linux/changelog.d/3905.added.md new file mode 100644 index 0000000000..f1ca13a85c --- /dev/null +++ b/linux/changelog.d/3905.added.md @@ -0,0 +1 @@ +A database opened beside its connection reopens with it at the next launch, with its tabs. diff --git a/linux/crates/app/src/ui/session_window/connections.rs b/linux/crates/app/src/ui/session_window/connections.rs index 82c6346880..3fdbcbe2b0 100644 --- a/linux/crates/app/src/ui/session_window/connections.rs +++ b/linux/crates/app/src/ui/session_window/connections.rs @@ -619,19 +619,21 @@ impl SessionWindow { if !self.tear_down_session(&key) { return; } - let Some(saved) = stored else { - self.show_toast(&crate::i18n::gettext("The connection was deleted.")); - return; - }; - let source = ConnectSource::Saved(Box::new(saved)); - let Some(claim) = self.claim_for(&key) else { - return; - }; - if let Some(mut pending) = self.spawn_connect(claim, source, None, CredentialDemand::default()) { - pending.databases = databases; - self.imp().connects.borrow_mut().push(pending); + match stored { + Some(saved) => self.connect_saved(saved, databases), + None => self.show_toast(&crate::i18n::gettext("The connection was deleted.")), } - self.connects_changed(); + } + + /// Connect `saved` through the window's one connect path, and open + /// `databases` beside it once it joins. + pub(super) fn connect_saved(&self, saved: SavedConnection, databases: Vec) { + self.connect_reopening( + None, + ConnectSource::Saved(Box::new(saved)), + CredentialDemand::default(), + databases, + ); } /// Tick the level of the session in front in the main menu. diff --git a/linux/crates/app/src/ui/session_window/imp.rs b/linux/crates/app/src/ui/session_window/imp.rs index 42d4bd21d8..72dc6df305 100644 --- a/linux/crates/app/src/ui/session_window/imp.rs +++ b/linux/crates/app/src/ui/session_window/imp.rs @@ -27,6 +27,10 @@ use crate::ui::welcome_view::WelcomeView; use crate::ui::workspace::WorkspaceShell; use crate::workspace::{CloseGuards, WindowInhibitor}; +/// A connect that failed beside the window's sessions: where it came +/// from, why it failed, and the other databases its next connect opens. +pub(super) type FailureBeside = (ConnectSource, ConnectFailure, Vec); + #[derive(Default, gtk::CompositeTemplate, glib::Properties)] #[template(resource = "/app/tablepro/TablePro/ui/session-window.ui")] #[properties(wrapper_type = super::SessionWindow)] @@ -53,7 +57,7 @@ pub struct SessionWindow { pub(super) failed: RefCell>, /// The last failure of each connect that failed beside something /// else, until the key connects again, for its Details. - pub(super) failures: RefCell>, + pub(super) failures: RefCell>, /// The shell, from the first session the window adopts to the last /// one it tears down. pub(super) shell: RefCell>, diff --git a/linux/crates/app/src/ui/session_window/locate_file.rs b/linux/crates/app/src/ui/session_window/locate_file.rs index ed3f70434f..ea64ad404f 100644 --- a/linux/crates/app/src/ui/session_window/locate_file.rs +++ b/linux/crates/app/src/ui/session_window/locate_file.rs @@ -202,7 +202,8 @@ impl SessionWindow { if self.failed_saved(located.id).is_none() { return; } - if let Some((claim, _, _)) = self.take_failure(&SessionKey::Saved(located.id)) { + if let Some(super::session_page::TakenFailure { claim, .. }) = self.take_failure(&SessionKey::Saved(located.id)) + { self.connect_with( claim, ConnectSource::Saved(Box::new(located)), diff --git a/linux/crates/app/src/ui/session_window/mod.rs b/linux/crates/app/src/ui/session_window/mod.rs index e8e56634ab..c6b3816bc8 100644 --- a/linux/crates/app/src/ui/session_window/mod.rs +++ b/linux/crates/app/src/ui/session_window/mod.rs @@ -65,7 +65,7 @@ use self::file_location_dialog::FileCheck; pub(crate) use self::launch_notice::LaunchNotice; pub(super) use self::picker_page::PickerPage; use self::resolve_file_identity::{FileResolveError, FileTarget, ResolvedFile, resolve_file_blocking}; -use self::session_page::{FailedConnect, OpenSession, PendingConnect, SessionPage, source_key}; +use self::session_page::{FailedConnect, OpenSession, PendingConnect, SessionPage, TakenFailure, source_key}; use self::session_window_command::{AttemptError, ConnectFailure, Connected, SessionWindowCommand}; pub(crate) use self::session_window_input::{MovedTab, SessionWindowInput}; use crate::application::{AppServices, WindowHost, WindowLaunch}; @@ -406,7 +406,7 @@ impl SessionWindow { self.imp().shell.replace(Some(shell.clone())); self.sync_tree(); if let Some(tree) = shell.tree() { - for (key, (source, failure)) in self.imp().failures.borrow().iter() { + for (key, (source, failure, _)) in self.imp().failures.borrow().iter() { tree.set_failure(key, Some((&source_name(source), failure.message.as_str()))); } } @@ -683,7 +683,7 @@ impl SessionWindow { self.open_database(id, database); } } - Err(AttemptError::Failed(failure)) => self.on_connect_failed(claim, source, failure), + Err(AttemptError::Failed(failure)) => self.on_connect_failed(claim, source, failure, databases), // A location question was asked above, so this is a stop. Err(AttemptError::Cancelled | AttemptError::AskLocation(_)) => { let key = claim.key().clone(); @@ -699,7 +699,15 @@ impl SessionWindow { /// toast says so and its Details say why. A session restored at /// launch that no longer opens is not tried again at the next one; a /// Retry that works records it again. - fn on_connect_failed(&self, claim: SessionClaim, source: ConnectSource, failure: ConnectFailure) { + /// The connection's other databases wait with the failure for the + /// connect that brings it back. + fn on_connect_failed( + &self, + claim: SessionClaim, + source: ConnectSource, + failure: ConnectFailure, + databases: Vec, + ) { tracing::warn!(error = %failure.message, "connect failed"); let imp = self.imp(); let key = claim.key().clone(); @@ -710,7 +718,12 @@ impl SessionWindow { ConnectSource::Saved(saved) => failure.missing_file.clone().map(|missing| (saved.id, missing)), _ => None, }; - self.show_failure(FailedConnect { claim, source, failure }); + self.show_failure(FailedConnect { + claim, + source, + failure, + databases, + }); if let Some((id, missing)) = missing { self.offer_locate(id, missing); } @@ -721,7 +734,9 @@ impl SessionWindow { if let Some(tree) = self.shell().and_then(|shell| shell.tree()) { tree.set_failure(&key, Some((&name, failure.message.as_str()))); } - imp.failures.borrow_mut().insert(key.clone(), (source, failure)); + imp.failures + .borrow_mut() + .insert(key.clone(), (source, failure, databases)); // The toast goes to the page that shows from here on, which may // be another connect's progress. self.connects_changed(); @@ -892,7 +907,20 @@ impl SessionWindow { return None; } match self.imp().failed.take() { - Some(failed) if failed.claim.key() == key => Some(failed.claim), + Some(FailedConnect { + claim, + source, + failure, + databases, + }) if claim.key() == key => { + // The connect this claim starts takes them, as it takes + // a failure beside the window's sessions. + self.imp() + .failures + .borrow_mut() + .insert(key.clone(), (source, failure, databases)); + Some(claim) + } failed => { let left = failed.is_some(); drop(failed); @@ -915,6 +943,76 @@ impl SessionWindow { /// Connect with the failed page's claim when there is one, else as /// any connect does. + /// Connect `source` with `claim`, or a claim of its own, and open + /// `databases` beside it once it joins: a reconnect's or a restore's. + pub(super) fn connect_reopening( + &self, + claim: Option, + source: ConnectSource, + demand: CredentialDemand, + databases: Vec, + ) { + let key = source_key(&source); + let claim = match claim { + Some(claim) => claim, + None => match self.claim_for(&key) { + Some(claim) => claim, + None => { + self.open_beside(&key, databases); + return; + } + }, + }; + if let Some(mut pending) = self.spawn_connect(claim, source, None, demand) { + pending.databases.extend(databases); + self.imp().connects.borrow_mut().push(pending); + } + self.connects_changed(); + } + + /// Open `databases` beside the connection under `key` that is open or + /// connecting already, in this window or the one that holds it. + fn open_beside(&self, key: &SessionKey, databases: Vec) { + let SessionKey::Saved(id) = *key else { + return; + }; + if self.holds_session(key) { + for database in databases { + self.open_database(id, database); + } + return; + } + let joined = { + let mut connects = self.imp().connects.borrow_mut(); + match connects.iter_mut().find(|pending| pending.claim.key() == key) { + Some(pending) => { + pending.databases.extend(databases.iter().cloned()); + true + } + None => false, + } + }; + if joined { + return; + } + // Another window holds the connection, and its databases open + // there. + let owner = self + .state() + .and_then(|state| state.context.sessions.window_for(key)) + .filter(|owner| *owner != self.id()); + let owner = owner.and_then(|owner| { + self.application()? + .windows() + .into_iter() + .filter_map(|window| window.downcast::().ok()) + .find(|window| window.id() == owner) + }); + if let Some(owner) = owner { + owner.open_beside(key, databases); + } + } + fn connect_with(&self, claim: Option, source: ConnectSource, demand: CredentialDemand) { match claim { Some(claim) => self.start_connect(claim, source, None, demand), @@ -991,6 +1089,7 @@ impl SessionWindow { claim, source: ConnectSource::File(FileIdentity::new(None, uri)), failure: ConnectFailure::new(message), + databases: Vec::new(), }); } } @@ -1047,11 +1146,17 @@ impl SessionWindow { let id = imp.next_connect.get().wrapping_add(1); imp.next_connect.set(id); let key = claim.key(); - if imp.failures.borrow_mut().remove(key).is_some() - && let Some(tree) = self.shell().and_then(|shell| shell.tree()) - { - tree.set_failure(key, None); - } + // Whatever brings a failed connection back reopens the other + // databases its failure kept. + let databases = match imp.failures.borrow_mut().remove(key) { + Some((_, _, databases)) => { + if let Some(tree) = self.shell().and_then(|shell| shell.tree()) { + tree.set_failure(key, None); + } + databases + } + None => Vec::new(), + }; let cancel = CancellationToken::new(); let check = self.file_check(&source, answer); let work = connect_work(state, &source, check, demand, cancel.clone()); @@ -1062,7 +1167,7 @@ impl SessionWindow { source, cancel, demand, - databases: Vec::new(), + databases, }) } @@ -1186,25 +1291,36 @@ impl SessionWindow { imp.failures .borrow() .get(key) - .map(|(source, failure)| read(source, failure)) + .map(|(source, failure, _)| read(source, failure)) } /// Take the last failure for `key` to act on it, with the failed /// page's claim when it is the page's. - pub(super) fn take_failure( - &self, - key: &SessionKey, - ) -> Option<(Option, ConnectSource, ConnectFailure)> { + pub(super) fn take_failure(&self, key: &SessionKey) -> Option { let imp = self.imp(); let page = imp.failed.borrow_mut().take_if(|failed| failed.claim.key() == key); - if let Some(FailedConnect { claim, source, failure }) = page { - return Some((Some(claim), source, failure)); + if let Some(FailedConnect { + claim, + source, + databases, + .. + }) = page + { + return Some(TakenFailure { + claim: Some(claim), + source, + databases, + }); } - let (source, failure) = imp.failures.borrow_mut().remove(key)?; + let (source, _, databases) = imp.failures.borrow_mut().remove(key)?; if let Some(tree) = self.shell().and_then(|shell| shell.tree()) { tree.set_failure(key, None); } - Some((None, source, failure)) + Some(TakenFailure { + claim: None, + source, + databases, + }) } /// Drop the last failure for `key` beside the window's sessions, and @@ -1220,15 +1336,20 @@ impl SessionWindow { /// Connect again from where the failed connect came from, reading /// the keyring again. fn on_retry_connect(&self, key: &SessionKey) { - if let Some((claim, source, _)) = self.take_failure(key) { - self.reconnect(claim, source, CredentialDemand::default()); + if let Some(taken) = self.take_failure(key) { + self.reconnect(taken, CredentialDemand::default()); } } /// Connect `source` again as it is now: a file is resolved again, /// since it may have moved, and a saved connection read as saved now, /// since another window may have edited or deleted it. - fn reconnect(&self, claim: Option, source: ConnectSource, demand: CredentialDemand) { + fn reconnect(&self, taken: TakenFailure, demand: CredentialDemand) { + let TakenFailure { + claim, + source, + databases, + } = taken; match source { ConnectSource::File(identity) => { drop(claim); @@ -1236,14 +1357,17 @@ impl SessionWindow { self.on_open_file(gio::File::for_uri(&identity.uri)); } ConnectSource::Saved(saved) => match self.saved_as_stored(&saved) { - Some(current) => self.connect_with(claim, ConnectSource::Saved(Box::new(current)), demand), + Some(current) => { + self.connect_reopening(claim, ConnectSource::Saved(Box::new(current)), demand, databases) + } None => { drop(claim); self.render(); self.show_toast(&crate::i18n::gettext("The connection was deleted.")); } }, - source => self.connect_with(claim, source, demand), + // An edited connection's retry brings its databases back too. + source => self.connect_reopening(claim, source, demand, databases), } } diff --git a/linux/crates/app/src/ui/session_window/other_databases.rs b/linux/crates/app/src/ui/session_window/other_databases.rs index a54f32caf6..a55fb12d59 100644 --- a/linux/crates/app/src/ui/session_window/other_databases.rs +++ b/linux/crates/app/src/ui/session_window/other_databases.rs @@ -176,6 +176,12 @@ impl SessionWindow { } (None, Ok(Ok(session))) => drop(session.begin_shutdown()), (Some(_), Ok(Err(error))) if !matches!(error, ConnectError::Cancelled) => { + // A database restored at launch that no longer opens is + // not tried again at the next one. + self.forget_session(&SessionKey::SavedIn { + id, + database: database.clone(), + }); self.show_toast(&gettext_f( "Could not open the database “{database}”: {error}", &[("database", &database), ("error", &connect_error_message(&error))], @@ -194,7 +200,8 @@ impl SessionWindow { return; }; claim.attach(&session); - let Some(workspace) = self.new_workspace(claim.key().clone(), &session, &shell) else { + let key = claim.key().clone(); + let Some(workspace) = self.new_workspace(key.clone(), &session, &shell) else { drop(session.begin_shutdown()); return; }; @@ -216,6 +223,7 @@ impl SessionWindow { .push(OpenSession::new(claim, session, workspace.clone())); others }; + self.remember_session(&key, None); self.render(); shell.add_session(&workspace); workspace.start(); diff --git a/linux/crates/app/src/ui/session_window/secret_retry.rs b/linux/crates/app/src/ui/session_window/secret_retry.rs index 7c7bffbacc..8675bf5b6c 100644 --- a/linux/crates/app/src/ui/session_window/secret_retry.rs +++ b/linux/crates/app/src/ui/session_window/secret_retry.rs @@ -22,8 +22,8 @@ impl SessionWindow { if !offered { return; } - if let Some((claim, source, _)) = self.take_failure(key) { - self.reconnect(claim, source, CredentialDemand::reenter(kind)); + if let Some(taken) = self.take_failure(key) { + self.reconnect(taken, CredentialDemand::reenter(kind)); } } } diff --git a/linux/crates/app/src/ui/session_window/session_page.rs b/linux/crates/app/src/ui/session_window/session_page.rs index 3c9e9d3060..50f6282c22 100644 --- a/linux/crates/app/src/ui/session_window/session_page.rs +++ b/linux/crates/app/src/ui/session_window/session_page.rs @@ -63,6 +63,16 @@ pub(crate) struct FailedConnect { pub(crate) claim: SessionClaim, pub(crate) source: ConnectSource, pub(crate) failure: ConnectFailure, + /// The connection's other databases its next connect opens again. + pub(crate) databases: Vec, +} + +/// A failure taken to act on, with the failed page's claim when it was +/// the page's. +pub(crate) struct TakenFailure { + pub(crate) claim: Option, + pub(crate) source: ConnectSource, + pub(crate) databases: Vec, } /// The key a connect from `source` claims. diff --git a/linux/crates/app/src/ui/session_window/session_restore.rs b/linux/crates/app/src/ui/session_window/session_restore.rs index f977256ef2..c3fcf9db7d 100644 --- a/linux/crates/app/src/ui/session_window/session_restore.rs +++ b/linux/crates/app/src/ui/session_window/session_restore.rs @@ -22,8 +22,10 @@ impl SessionWindow { return; } SessionKey::Saved(id) => id, - // Never recorded: another database opens from its connection. - SessionKey::SavedIn { .. } => return, + SessionKey::SavedIn { id, database } => { + self.restore_database(id, database); + return; + } }; let (Some(state), Some(input)) = (self.state(), self.input()) else { return; @@ -60,25 +62,81 @@ impl SessionWindow { }); } + /// Another database of a connection reopens beside it (0030): with the + /// connection's restore, which takes it, or beside the connection open + /// or connecting already. One with no connection to open beside is + /// forgotten. + fn restore_database(&self, id: Uuid, database: String) { + let key = SessionKey::SavedIn { + id, + database: database.clone(), + }; + let connection = SessionKey::Saved(id); + if self.imp().restoring.borrow().contains(&connection) { + return; + } + self.end_restore(&key); + if self.held_session(&connection).is_some() { + self.open_database(id, database); + return; + } + let joined = { + let mut connects = self.imp().connects.borrow_mut(); + match connects.iter_mut().find(|pending| pending.claim.key() == &connection) { + Some(pending) => { + pending.databases.push(database); + true + } + None => false, + } + }; + if !joined { + self.forget_session(&key); + } + } + + /// The databases restored with the connection `id`, which its restore + /// takes. + fn take_restored_databases(&self, id: Uuid) -> Vec { + let Ok(mut restoring) = self.imp().restoring.try_borrow_mut() else { + return Vec::new(); + }; + let mut databases = Vec::new(); + restoring.retain(|key| match key { + SessionKey::SavedIn { id: of, database } if *of == id => { + databases.push(database.clone()); + false + } + _ => true, + }); + databases + } + pub(super) fn on_restore_saved(&self, id: Uuid, saved: Option) { self.end_restore(&SessionKey::Saved(id)); - match saved { - Some(saved) => self.on_open_saved(saved), + let databases = self.take_restored_databases(id); + // It may have been edited or deleted since the lookup read it. + match saved.and_then(|saved| self.saved_as_stored(&saved)) { + Some(saved) => self.connect_saved(saved, databases), None => { self.forget_session(&SessionKey::Saved(id)); + for database in databases { + self.forget_session(&SessionKey::SavedIn { id, database }); + } self.show_toast(&crate::i18n::gettext("The connection was deleted.")); } } } /// Record the session the window adopted when a later launch can - /// open it again: a file, or a connection the saved list holds. One - /// that was never saved has nothing to reopen it from. + /// open it again: a file, a connection the saved list holds, or + /// another database of one, which reopens beside it. One that was + /// never saved has nothing to reopen it from. pub(super) fn remember_session(&self, key: &SessionKey, saved: Option<&SavedConnection>) { let restorable = match key { SessionKey::File(_) => true, SessionKey::Saved(_) => saved.is_some(), - SessionKey::SavedIn { .. } => false, + SessionKey::SavedIn { .. } => true, }; if restorable && let Some(state) = self.state() { state.services.open_sessions().add(key.clone(), self.id()); @@ -356,6 +414,120 @@ mod tests { assert_eq!(test.connector.calls().len(), 1, "the missing file was connected to"); } + /// A database open beside its connection reopens beside it; one whose + /// connection does not reopen is forgotten (0030). + #[gtk4::test] + fn a_database_open_beside_its_connection_reopens_with_it() { + let test = TestWindow::with_script( + "RestoreDatabase", + Script { + network: true, + ..Script::default() + }, + ); + let (vault, id) = (test.vault.clone(), test.saved.id); + crate::test_support::finish_on_main(WAIT, async move { + tablepro_core::credentials::SecretVault::store( + &*vault, + id, + tablepro_core::credentials::SecretKind::DatabasePassword, + &secrecy::SecretString::from("s3cret"), + "label", + ) + .await + }) + .expect("the store to end") + .expect("the password stored"); + let sibling = SessionKey::SavedIn { + id, + database: "analytics".to_owned(), + }; + let orphan = SessionKey::SavedIn { + id: Uuid::new_v4(), + database: "analytics".to_owned(), + }; + + let window = test.restored_window_of(&[test.key(), sibling.clone(), orphan]); + wait_until(WAIT, || window.held_session(&sibling).is_some()).expect("the database to reopen"); + + assert_eq!(test.services.sessions.window_for(&sibling), Some(window.id())); + assert_eq!(test.recorded(), [test.key(), sibling]); + } + + /// A connection that fails at launch keeps the databases restored + /// with it, and Retry opens them beside it. + #[gtk4::test] + fn a_database_restored_with_a_failed_connection_reopens_on_retry() { + let test = TestWindow::with_script( + "RestoreDatabaseRetry", + Script { + network: true, + establish: vec![crate::ui::session_window::tests::refused()], + ..Script::default() + }, + ); + let (vault, id) = (test.vault.clone(), test.saved.id); + crate::test_support::finish_on_main(WAIT, async move { + tablepro_core::credentials::SecretVault::store( + &*vault, + id, + tablepro_core::credentials::SecretKind::DatabasePassword, + &secrecy::SecretString::from("s3cret"), + "label", + ) + .await + }) + .expect("the store to end") + .expect("the password stored"); + let sibling = SessionKey::SavedIn { + id, + database: "analytics".to_owned(), + }; + + let window = test.restored_window_of(&[test.key(), sibling.clone()]); + wait_until(WAIT, || window.page_name() == "connect-failed").expect("the failed page"); + window.send(SessionWindowInput::RetryConnect); + + wait_until(WAIT, || window.held_session(&sibling).is_some()).expect("the database to reopen"); + } + + /// Opening the failed connection again from the picker, not Retry, + /// reopens the databases restored with it too. + #[gtk4::test] + fn a_database_restored_with_a_failed_connection_reopens_when_it_is_opened_again() { + let test = TestWindow::with_script( + "RestoreDatabaseReopen", + Script { + network: true, + establish: vec![crate::ui::session_window::tests::refused()], + ..Script::default() + }, + ); + let (vault, id) = (test.vault.clone(), test.saved.id); + crate::test_support::finish_on_main(WAIT, async move { + tablepro_core::credentials::SecretVault::store( + &*vault, + id, + tablepro_core::credentials::SecretKind::DatabasePassword, + &secrecy::SecretString::from("s3cret"), + "label", + ) + .await + }) + .expect("the store to end") + .expect("the password stored"); + let sibling = SessionKey::SavedIn { + id, + database: "analytics".to_owned(), + }; + + let window = test.restored_window_of(&[test.key(), sibling.clone()]); + wait_until(WAIT, || window.page_name() == "connect-failed").expect("the failed page"); + window.send(SessionWindowInput::OpenSaved(Box::new(test.saved.clone()))); + + wait_until(WAIT, || window.held_session(&sibling).is_some()).expect("the database to reopen"); + } + #[gtk4::test] fn a_missing_file_is_forgotten_and_a_retry_records_it_again() { let test = TestWindow::picker("RestoreMissing"); diff --git a/linux/crates/app/src/ui/session_window/tests/other_databases.rs b/linux/crates/app/src/ui/session_window/tests/other_databases.rs index b17ac249b7..bb89c6a535 100644 --- a/linux/crates/app/src/ui/session_window/tests/other_databases.rs +++ b/linux/crates/app/src/ui/session_window/tests/other_databases.rs @@ -6,7 +6,7 @@ use tablepro_session::SessionKey; use tablepro_session::fakes::FakeEstablish; use tablepro_test_support::{ConnectionScript, FakeReply}; -use super::{Script, TestWindow, WAIT, editors, pick_level}; +use super::{Script, TestWindow, WAIT, editors, pick_level, refused}; use crate::test_support::{drain_main_context, wait_until}; use crate::ui::SessionWindowInput; @@ -62,14 +62,19 @@ fn analytics(test: &TestWindow) -> SessionKey { } } -/// The connection connects; another of its databases never answers -/// until its open is stopped. -fn opening_never_ends() -> Vec { +/// A connection that answers what a workspace asks as it opens. +fn answering() -> FakeEstablish { let mut connection = ConnectionScript::default(); connection.probe.always(FakeReply::ok(())); connection.list_schemas.always(FakeReply::ok(Vec::new())); connection.list_objects.always(FakeReply::ok(Vec::new())); - vec![FakeEstablish::Connect(connection), FakeEstablish::UntilCancelled] + FakeEstablish::Connect(connection) +} + +/// The connection connects; another of its databases never answers +/// until its open is stopped. +fn opening_never_ends() -> Vec { + vec![answering(), FakeEstablish::UntilCancelled] } #[gtk4::test] @@ -166,6 +171,24 @@ fn read_only_reopens_the_other_databases_at_the_new_level() { assert!(request.workspace_settings.read_only); } +/// A reconnect for Read-Only that fails keeps the connection's other +/// databases with its failure, and Retry opens them beside it again. +#[gtk4::test] +fn a_failed_reconnect_reopens_the_other_databases_on_retry() { + let test = network_window("OtherDatabaseRetry", vec![answering(), answering(), refused()]); + connect(&test); + let sibling = analytics(&test); + test.window.open_database(test.saved.id, ANALYTICS.to_owned()); + wait_until(WAIT, || test.window.held_session(&sibling).is_some()).expect("the database to open"); + + pick_level(&test, "read-only"); + test.wait_for_page("connect-failed"); + assert!(test.window.held_session(&sibling).is_none()); + test.window.send(SessionWindowInput::RetryConnect); + + wait_until(WAIT, || test.window.held_session(&sibling).is_some()).expect("the database to open again"); +} + /// A level picked while one of the connection's other databases is in /// front is the connection's, and moves every one of its sessions. #[gtk4::test] diff --git a/linux/crates/app/src/workspace/open_sessions.rs b/linux/crates/app/src/workspace/open_sessions.rs index fb480ba74c..a42fcf3c7e 100644 --- a/linux/crates/app/src/workspace/open_sessions.rs +++ b/linux/crates/app/src/workspace/open_sessions.rs @@ -114,7 +114,8 @@ impl OpenSessions { } /// Ignored while a quit runs: the windows it closes are the ones the - /// next launch reopens. + /// next launch reopens. A connection's other databases go with it, + /// since they reopen only beside it (0030). pub fn remove(&self, key: &SessionKey) { if self.frozen.get() { return; @@ -122,7 +123,7 @@ impl OpenSessions { { let mut keys = self.keys.borrow_mut(); let before = keys.len(); - keys.retain(|(kept, _)| kept != key); + keys.retain(|(kept, _)| kept != key && !opens_beside(kept, key)); if keys.len() == before { return; } @@ -210,6 +211,11 @@ impl std::fmt::Debug for OpenSessions { } } +/// Whether `kept` is another database of the connection `key`. +fn opens_beside(kept: &SessionKey, key: &SessionKey) -> bool { + matches!((kept, key), (SessionKey::SavedIn { id, .. }, SessionKey::Saved(of)) if id == of) +} + #[cfg(test)] mod tests { use std::path::{Path, PathBuf}; @@ -295,6 +301,33 @@ mod tests { assert_eq!(fixture.on_disk(), [first, second]); } + /// A connection's other databases reopen only beside it, so they go + /// from the record with it. + #[gtk4::test] + fn removing_a_connection_removes_its_other_databases() { + let fixture = Fixture::new(); + let connection = saved(); + let SessionKey::Saved(id) = connection else { + panic!("a saved key"); + }; + let database = SessionKey::SavedIn { + id, + database: "analytics".to_owned(), + }; + let other = SessionKey::SavedIn { + id: Uuid::new_v4(), + database: "analytics".to_owned(), + }; + for key in [&connection, &database, &other] { + fixture.sessions.add(key.clone(), 1); + } + + fixture.sessions.remove(&connection); + + assert_eq!(fixture.sessions.keys(), std::slice::from_ref(&other)); + assert_eq!(fixture.on_disk(), [other]); + } + #[gtk4::test] fn a_write_holds_the_process_until_it_lands() { let fixture = Fixture::new(); diff --git a/linux/crates/app/src/workspace/open_sessions_record.rs b/linux/crates/app/src/workspace/open_sessions_record.rs index b09ff738e7..2cf2deb105 100644 --- a/linux/crates/app/src/workspace/open_sessions_record.rs +++ b/linux/crates/app/src/workspace/open_sessions_record.rs @@ -21,8 +21,8 @@ pub struct OpenSessionsRecord { impl VersionedDocument for OpenSessionsRecord { const KIND: &'static str = "open-sessions"; - const VERSION: u32 = 2; - const UPGRADES: &'static [Upgrade] = &[window_per_session]; + const VERSION: u32 = 3; + const UPGRADES: &'static [Upgrade] = &[window_per_session, databases_beside_connections]; } /// Version 1 listed the sessions alone, and each opened in a window of @@ -40,6 +40,14 @@ fn window_per_session(mut document: Value) -> Result { Ok(document) } +/// Version 3 adds another database of a connection beside it. Every +/// version 2 key reads as it was, and a build that knows only version 2 +/// says a version 3 record is from a newer build rather than calling it +/// unreadable. +fn databases_beside_connections(document: Value) -> Result { + Ok(document) +} + impl OpenSessionsRecord { /// Beside the sessions' own files, under a name none of them can /// take. @@ -110,7 +118,13 @@ mod tests { fn record() -> OpenSessionsRecord { OpenSessionsRecord { windows: vec![ - vec![SessionKeyRecord::Saved { id: Uuid::new_v4() }], + vec![ + SessionKeyRecord::Saved { id: Uuid::new_v4() }, + SessionKeyRecord::SavedIn { + id: Uuid::new_v4(), + database: "analytics".to_owned(), + }, + ], vec![SessionKeyRecord::File { uri: "file:///srv/app.db".to_owned(), }], @@ -138,7 +152,7 @@ mod tests { assert_eq!(decoded, record); let text = String::from_utf8_lossy(&bytes); - assert!(text.contains("\"version\": 2"), "{text}"); + assert!(text.contains("\"version\": 3"), "{text}"); assert!(text.contains("\"kind\": \"saved\""), "{text}"); } @@ -146,7 +160,7 @@ mod tests { fn unsupported_version_moved_aside_and_loads_empty() { let dir = tempfile::tempdir().expect("tempdir"); let path = dir.path().join(OpenSessionsRecord::FILE_NAME); - let original = br#"{"version": 3, "windows": []}"#; + let original = br#"{"version": 4, "windows": []}"#; std::fs::write(&path, original).expect("seed"); let load = read_open_sessions_blocking(&path); @@ -205,6 +219,26 @@ mod tests { ); } + /// A version 2 record, the last without another database beside a + /// connection. Never edit the literal: it is what that build wrote. + #[test] + fn a_version_2_record_reads_as_it_was_written() { + let first = Uuid::new_v4(); + let bytes = format!( + r#"{{"version": 2, "windows": [[{{"kind": "saved", "id": "{first}"}}, {{"kind": "file", "uri": "file:///srv/app.db"}}]]}}"# + ); + + let record: OpenSessionsRecord = decode_document(Path::new("/tmp/o.json"), bytes.as_bytes()).expect("decode"); + + assert_eq!( + record.windows(), + [vec![ + SessionKey::Saved(first), + SessionKey::File("file:///srv/app.db".to_owned()), + ]] + ); + } + #[test] fn a_version_1_record_without_sessions_opens_nothing() { let record: OpenSessionsRecord = From e2891d8f5705e7545f86f767ed167b8fb44eb843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sun, 1 Nov 2026 00:44:20 +0700 Subject: [PATCH 48/85] Merge commit from fork * fix(security): pin PostgreSQL catalog reads to pg_catalog and SYS-qualify Oracle Explain Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CDER3MTT. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CRPAPF23. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): write Oracle Estimated Plan to SYS.PLAN_TABLE$ Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * test(security): expect the pg_catalog-qualified PostgreSQL cancel statement --- .github/workflows/repo-hygiene.yml | 4 + .../PostgreSQLCatalogSearchPath.swift | 28 ++ .../PostgreSQLCatalogTypeNames.swift | 5 +- .../PostgreSQLCatalogSearchPathTests.swift | 31 ++ .../PostgreSQLCatalogTypeNamesTests.swift | 2 +- .../TableProOracleCore/OracleDictionary.swift | 5 + .../OracleExplainPlan.swift | 14 +- .../OracleDictionaryQualificationTests.swift | 21 +- .../OracleExplainPlanTests.swift | 4 +- .../KingbaseESCompatibilityMode.swift | 2 +- .../KingbaseESPluginDriver.swift | 2 +- .../CockroachPluginDriver.swift | 32 +- .../LibPQBackedDriver+SchemaManagement.swift | 4 +- .../LibPQDriverCore.swift | 52 ++- ...bPQPluginConnection+CatalogTypeNames.swift | 5 +- .../LibPQPluginConnection.swift | 25 +- .../PostGISSpatialRewrite.swift | 16 +- .../PostgreSQLObjectQueries.swift | 42 +-- .../PostgreSQLPluginDriver+BulkMetadata.swift | 19 +- ...PostgreSQLPluginDriver+ColumnReorder.swift | 112 +++---- .../PostgreSQLPluginDriver+Columns.swift | 49 +-- .../PostgreSQLPluginDriver+Comments.swift | 2 +- .../PostgreSQLPluginDriver+EnumTypes.swift | 2 +- .../PostgreSQLPluginDriver+Principals.swift | 26 +- ...PostgreSQLPluginDriver+QualifiedRead.swift | 31 -- ...tgreSQLPluginDriver+RoutineDebugging.swift | 21 +- .../PostgreSQLPluginDriver+Routines.swift | 8 +- .../PostgreSQLPluginDriver+Sequences.swift | 10 +- ...PostgreSQLPluginDriver+ServerSupport.swift | 2 +- ...ostgreSQLPluginDriver+TriggerEditing.swift | 4 +- .../PostgreSQLPluginDriver+Types.swift | 8 +- .../PostgreSQLPluginDriver+Views.swift | 4 +- .../PostgreSQLPluginDriver.swift | 97 ++---- .../PostgreSQLPrincipalQueries.swift | 2 +- .../PostgreSQLRoutineDebugProtocol.swift | 28 +- .../PostgreSQLSchemaManagementQueries.swift | 3 +- .../PostgreSQLSchemaQueries.swift | 197 ++++++++---- .../PostgreSQLSessionScopeSQL.swift | 9 +- .../PostgreSQLTableRebuild.swift | 6 +- .../PostgreSQLVersionedStatements.swift | 2 +- .../PostgreSQLViewDefinition.swift | 12 +- .../RedshiftPluginDriver.swift | 73 ++--- .../PostGISSpatialRewriteTests.swift | 24 +- .../PostgreSQLCatalogQualificationTests.swift | 172 ++++++++++ .../PostgreSQLCatalogSQLPinTests.swift | 54 +--- .../PostgreSQLColumnQueryTests.swift | 19 +- .../PostgreSQLColumnTypeSpellingTests.swift | 14 +- ...PostgreSQLDefaultSchemaFallbackTests.swift | 4 +- .../PostgreSQLLegacyCatalogQueryTests.swift | 6 +- .../PostgreSQLObjectQueryTests.swift | 7 +- .../PostgreSQLSessionScopeSQLTests.swift | 2 +- .../PostgreSQLTypeQueryTests.swift | 2 +- .../PostgreSQLVersionedStatementsTests.swift | 4 +- .../PostgreSQLViewDefinitionTests.swift | 1 - .../Providers/PostgreSQLActivityCatalog.swift | 92 +++--- .../PostgreSQLDashboardProvider.swift | 25 +- .../Drivers/PostgreSQLDriver+Catalog.swift | 10 +- .../Drivers/PostgreSQLDriver.swift | 61 +++- .../PostgreSQLTransactionStatementTests.swift | 9 + .../MCP/MCPServerDashboardPayloadTests.swift | 2 +- .../PostgreSQLDashboardProviderTests.swift | 44 ++- .../+ghsa-hgr8-24vm-5m54.security.1.md | 1 + .../+ghsa-hgr8-24vm-5m54.security.2.md | 1 + .../+ghsa-hgr8-24vm-5m54.security.md | 1 + .../ci/check-oracle-dictionary-names.py | 163 ++++++++++ .../ci/test_check_oracle_dictionary_names.py | 111 +++++++ .../probes/check-oracle-explain-plans.sh | 187 ++++++++++- .../check-postgresql-catalog-shadowing.sh | 297 ++++++++++++++++++ .../probes/postgresql-shadowing-probe.swift | 208 ++++++++++++ 69 files changed, 1971 insertions(+), 571 deletions(-) create mode 100644 apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCatalogSearchPath.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/PostgreSQLCatalogSearchPathTests.swift delete mode 100644 apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+QualifiedRead.swift create mode 100644 apple/Plugins/PostgreSQLDriverTests/PostgreSQLCatalogQualificationTests.swift create mode 100644 apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.1.md create mode 100644 apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.2.md create mode 100644 apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.md create mode 100755 apple/scripts/ci/check-oracle-dictionary-names.py create mode 100644 apple/scripts/ci/test_check_oracle_dictionary_names.py create mode 100755 apple/scripts/probes/check-postgresql-catalog-shadowing.sh create mode 100644 apple/scripts/probes/postgresql-shadowing-probe.swift diff --git a/.github/workflows/repo-hygiene.yml b/.github/workflows/repo-hygiene.yml index 4b633d147c..3a8f59100f 100644 --- a/.github/workflows/repo-hygiene.yml +++ b/.github/workflows/repo-hygiene.yml @@ -117,6 +117,10 @@ jobs: - name: Check every test target is in its test plan run: python3 apple/scripts/ci/check-test-plans.py + # Oracle reads a bare dictionary name from the current schema before the SYS synonym. + - name: Check Oracle SQL names dictionary objects through OracleDictionary + run: python3 apple/scripts/ci/check-oracle-dictionary-names.py + # The corpora run by hand against servers (scripts/probes/check-column-type-corpus.sh), so a type a driver # starts listing is caught here. - name: Check every listed column type has a corpus column diff --git a/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCatalogSearchPath.swift b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCatalogSearchPath.swift new file mode 100644 index 0000000000..cda64d28c8 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCatalogSearchPath.swift @@ -0,0 +1,28 @@ +import Foundation + +/// The `search_path` a driver-authored catalog statement runs under, set with `SET LOCAL` in the same query string so it +/// lasts only as long as the read. +/// +/// PostgreSQL resolves a function or operator over every schema on the path, and a candidate that takes the call's +/// argument types exactly beats a `pg_catalog` one reached through a cast, wherever the two sit on the path. A +/// `pg_total_relation_size(oid)` or an `<>(oid, integer)` in a browsed schema would otherwise run with the reader's +/// privileges, the substitution of CVE-2018-1058. `pg_dump` runs with the same narrowed path for the same reason. +/// `pg_temp` is listed last so a temporary relation never shadows a catalog; functions and operators are never looked +/// up there. +/// +/// `SET LOCAL` in the read's own query string makes the pair one implicit transaction, so the path is back as soon as +/// the read returns; inside a transaction the caller holds, the driver scopes it with a savepoint. `pg_catalog` rather +/// than an empty path: PostgreSQL 9.1 rejects `''` ("schema "" does not exist"), and `pg_catalog` deparses every user +/// relation with its schema all the same (measured identical output on 9.2, 9.3, 9.6 and 17). +nonisolated public enum PostgreSQLCatalogSearchPath { + public static let pinned = "SET LOCAL search_path = pg_catalog, pg_temp; " + + /// For a read whose output the server deparses: `format_type`, `pg_get_expr` and the `pg_get_*def` functions leave + /// out the schema of a name the path resolves, so this writes the schema's own names bare. The schema is on the + /// path, so a statement run under it calls only `pg_catalog`-qualified functions and compares only operands a + /// `pg_catalog` operator takes exactly. + public static func relative(to schema: String) -> String { + let quoted = "\"\(schema.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" + return "SET LOCAL search_path = pg_catalog, \(quoted), pg_temp; " + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCatalogTypeNames.swift b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCatalogTypeNames.swift index 218ec3889d..e5bf1f8b15 100644 --- a/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCatalogTypeNames.swift +++ b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCatalogTypeNames.swift @@ -105,8 +105,11 @@ public enum PostgreSQLCatalogTypeNames { /// because Redshift's catalog predates `typcategory` and the predicate agrees with it on /// every real type: `point` and `name` carry a `typelem` at a fixed length, and the one /// disagreement is the pseudo-type `_record`, which no column has. + /// + /// Sent on the user's own session under its search path, so each oid is typed: a bare integer compares oid with + /// integer, which a `=(oid, integer)` in a schema on that path takes exactly. public static func lookupQuery(oids: [UInt32]) -> String? { - let list = Set(oids).sorted().map(String.init).joined(separator: ", ") + let list = Set(oids).sorted().map { "'\($0)'::pg_catalog.oid" }.joined(separator: ", ") guard !list.isEmpty else { return nil } return """ SELECT t.oid::text, t.typname, t.typtype::text, diff --git a/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/PostgreSQLCatalogSearchPathTests.swift b/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/PostgreSQLCatalogSearchPathTests.swift new file mode 100644 index 0000000000..7d938041b1 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/PostgreSQLCatalogSearchPathTests.swift @@ -0,0 +1,31 @@ +import Foundation +import Testing + +@testable import TableProPostgreSQLCore + +struct PostgreSQLCatalogSearchPathTests { + @Test("A catalog read resolves functions and operators in pg_catalog alone, with temporary relations last") + func pinnedPathIsPgCatalogThenPgTemp() { + #expect(PostgreSQLCatalogSearchPath.pinned == "SET LOCAL search_path = pg_catalog, pg_temp; ") + } + + @Test("A schema-relative read keeps pg_catalog first and pg_temp last, with the schema quoted") + func relativePathQuotesTheSchema() { + #expect( + PostgreSQLCatalogSearchPath.relative(to: "sales") + == "SET LOCAL search_path = pg_catalog, \"sales\", pg_temp; " + ) + #expect( + PostgreSQLCatalogSearchPath.relative(to: "Weird \"Schema\"") + == "SET LOCAL search_path = pg_catalog, \"Weird \"\"Schema\"\"\", pg_temp; " + ) + } + + @Test("A quote followed by a combining mark is still doubled") + func quoteBeforeCombiningMarkIsDoubled() { + #expect( + PostgreSQLCatalogSearchPath.relative(to: "a\"\u{0301}b") + == "SET LOCAL search_path = pg_catalog, \"a\"\"\u{0301}b\", pg_temp; " + ) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/PostgreSQLCatalogTypeNamesTests.swift b/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/PostgreSQLCatalogTypeNamesTests.swift index 5d6f03d2dc..739bbba76c 100644 --- a/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/PostgreSQLCatalogTypeNamesTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/PostgreSQLCatalogTypeNamesTests.swift @@ -58,7 +58,7 @@ struct PostgreSQLCatalogTypeNamesTests { @Test("The lookup lists each oid once, in order, and tells an array by its element and length") func lookupQueryShape() throws { let sql = try #require(PostgreSQLCatalogTypeNames.lookupQuery(oids: [16_390, 16_385, 16_390])) - #expect(sql.contains("WHERE t.oid IN (16385, 16390)")) + #expect(sql.contains("WHERE t.oid IN ('16385'::pg_catalog.oid, '16390'::pg_catalog.oid)")) #expect(sql.contains("LEFT JOIN pg_catalog.pg_type el ON el.oid = t.typelem AND t.typlen = -1")) #expect(sql.contains("pg_catalog.format_type(t.typbasetype, t.typtypmod)")) #expect(!sql.contains("typcategory")) diff --git a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleDictionary.swift b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleDictionary.swift index dd3c789050..4fe9a1e392 100644 --- a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleDictionary.swift +++ b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleDictionary.swift @@ -63,4 +63,9 @@ public enum OracleDictionary { public static let dbaSegments = view("DBA_SEGMENTS") public static let userSegments = view("USER_SEGMENTS") public static let versionView = performanceView("V$VERSION") + public static let parameterView = performanceView("V$PARAMETER") + public static let sessionView = performanceView("V$SESSION") + public static let sqlPlanStatisticsView = performanceView("V$SQL_PLAN_STATISTICS_ALL") + // The global temporary table behind the PLAN_TABLE public synonym, granted to PUBLIC by catplan.sql. + public static let planTable = view("PLAN_TABLE$") } diff --git a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleExplainPlan.swift b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleExplainPlan.swift index 38fa283d61..b09276415a 100644 --- a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleExplainPlan.swift +++ b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleExplainPlan.swift @@ -13,29 +13,29 @@ public enum OracleExplainPlan { } public static func explainStatement(_ statement: String, statementId: String) -> String { - "EXPLAIN PLAN SET STATEMENT_ID = '\(statementId)' FOR \(bare(statement))" + "EXPLAIN PLAN SET STATEMENT_ID = '\(statementId)' INTO \(OracleDictionary.planTable) FOR \(bare(statement))" } public static func planRowsQuery(statementId: String) -> String { """ SELECT \(planColumns.joined(separator: ", ")) - FROM PLAN_TABLE + FROM \(OracleDictionary.planTable) WHERE STATEMENT_ID = '\(statementId)' ORDER BY ID """ } public static func deleteStatement(statementId: String) -> String { - "DELETE FROM PLAN_TABLE WHERE STATEMENT_ID = '\(statementId)'" + "DELETE FROM \(OracleDictionary.planTable) WHERE STATEMENT_ID = '\(statementId)'" } // Fails at parse time when a view is out of reach, before the statement runs. V$PARAMETER holds session values. public static let actualPlanProbe = """ SELECT p.VALUE - FROM V$PARAMETER p + FROM \(OracleDictionary.parameterView) p WHERE p.NAME = 'statistics_level' - AND EXISTS (SELECT 1 FROM V$SESSION s WHERE s.SID = SYS_CONTEXT('USERENV', 'SID')) - AND NOT EXISTS (SELECT 1 FROM V$SQL_PLAN_STATISTICS_ALL WHERE 1 = 0) + AND EXISTS (SELECT 1 FROM \(OracleDictionary.sessionView) s WHERE s.SID = SYS_CONTEXT('USERENV', 'SID')) + AND NOT EXISTS (SELECT 1 FROM \(OracleDictionary.sqlPlanStatisticsView) WHERE 1 = 0) """ // Only Oracle's three levels, so a value read back is never spliced into a statement. @@ -51,7 +51,7 @@ public enum OracleExplainPlan { public static let lastCursorPlanQuery = """ SELECT \(planColumns.map { "p.\($0)" }.joined(separator: ", ")), \ \(actualColumns.map { "p.\($0)" }.joined(separator: ", ")) - FROM V$SQL_PLAN_STATISTICS_ALL p, V$SESSION s + FROM \(OracleDictionary.sqlPlanStatisticsView) p, \(OracleDictionary.sessionView) s WHERE s.SID = SYS_CONTEXT('USERENV', 'SID') AND p.SQL_ID = s.PREV_SQL_ID AND p.CHILD_NUMBER = s.PREV_CHILD_NUMBER diff --git a/apple/Packages/TableProOracle/Tests/TableProOracleCoreTests/OracleDictionaryQualificationTests.swift b/apple/Packages/TableProOracle/Tests/TableProOracleCoreTests/OracleDictionaryQualificationTests.swift index f4273c2b97..3ae756dc8a 100644 --- a/apple/Packages/TableProOracle/Tests/TableProOracleCoreTests/OracleDictionaryQualificationTests.swift +++ b/apple/Packages/TableProOracle/Tests/TableProOracleCoreTests/OracleDictionaryQualificationTests.swift @@ -10,7 +10,7 @@ struct OracleDictionaryQualificationTests { /// tables lowercase aliases, so a name after one (`c.USER_GENERATED`, a column of `ALL_TAB_COLS`) is a column and /// names no dictionary object, while an uppercase owner in front of a view is still caught. private static let bareDictionary = try! NSRegularExpression( - pattern: #"(? [PluginTableInfo] { - let result = try await execute(query: CockroachRelationSQL.tablesQuery(schema: schema ?? core.currentSchema)) + let result = try await catalogRead(CockroachRelationSQL.tablesQuery(schema: schema ?? core.currentSchema)) return result.rows.compactMap { CockroachRelationSQL.table(fromRow: $0.map(\.asText)) } } func fetchTables(inSchemas schemas: [String]) async throws -> [PluginTableInfo]? { guard !schemas.isEmpty else { return [] } - let result = try await execute(query: CockroachRelationSQL.tablesQuery(inSchemas: schemas)) + let result = try await catalogRead(CockroachRelationSQL.tablesQuery(inSchemas: schemas)) return result.rows.compactMap { CockroachRelationSQL.table(fromRow: $0.map(\.asText)) } } func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { let resolvedSchema = schema ?? core.currentSchema let query = CockroachRelationSQL.columnsQuery(schema: resolvedSchema, table: table) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { Self.mapColumnRow($0, includesTableName: false) } } func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] { let resolvedSchema = schema ?? core.currentSchema let query = CockroachRelationSQL.columnsQuery(schema: resolvedSchema, table: nil) - let result = try await execute(query: query) + let result = try await catalogRead(query) var allColumns: [String: [PluginColumnInfo]] = [:] for row in result.rows { guard let tableName = row.first?.asText, @@ -93,7 +93,7 @@ final class CockroachPluginDriver: LibPQBackedDriver, @unchecked Sendable { func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { let query = CockroachRelationSQL.showIndexes(table: table, schema: schema ?? core.currentSchema) - let result = try await execute(query: query) + let result = try await catalogRead(query) guard let columnIndex = result.columns.firstIndex(of: "column_name"), let nameIndex = result.columns.firstIndex(of: "index_name") else { @@ -147,13 +147,13 @@ final class CockroachPluginDriver: LibPQBackedDriver, @unchecked Sendable { serverVersionNumber: core.serverVersionNumber ) ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return PostgreSQLCatalogForeignKeys.foreignKeys(from: result.rows.map { $0.map(\.asText) }) } func fetchTableDDL(table: String, schema: String?) async throws -> String { - let result = try await execute( - query: CockroachRelationSQL.showCreateTable(table: table, schema: schema ?? core.currentSchema) + let result = try await catalogRead( + CockroachRelationSQL.showCreateTable(table: table, schema: schema ?? core.currentSchema) ) guard let ddl = Self.createStatement(from: result) else { throw LibPQPluginError(message: "Failed to fetch DDL for table '\(table)'", sqlState: nil, detail: nil) @@ -162,8 +162,8 @@ final class CockroachPluginDriver: LibPQBackedDriver, @unchecked Sendable { } func fetchViewDefinition(view: String, schema: String?) async throws -> String { - let result = try await execute( - query: CockroachRelationSQL.showCreateView(view: view, schema: schema ?? core.currentSchema) + let result = try await catalogRead( + CockroachRelationSQL.showCreateView(view: view, schema: schema ?? core.currentSchema) ) guard let ddl = Self.createStatement(from: result) else { throw LibPQPluginError(message: "Failed to fetch definition for view '\(view)'", sqlState: nil, detail: nil) @@ -192,14 +192,14 @@ final class CockroachPluginDriver: LibPQBackedDriver, @unchecked Sendable { } func fetchDatabases() async throws -> [String] { - let result = try await execute( - query: "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname" + let result = try await catalogRead( + "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname" ) return result.rows.compactMap { $0.first?.asText } } func fetchSchemas() async throws -> [String] { - let result = try await execute(query: PostgreSQLSchemaQueries.listSchemas) + let result = try await catalogRead(PostgreSQLSchemaQueries.listSchemas) return result.rows.compactMap { $0.first?.asText } } @@ -211,7 +211,7 @@ final class CockroachPluginDriver: LibPQBackedDriver, @unchecked Sendable { WHERE table_catalog = \(databaseLiteral) AND table_schema NOT IN ('pg_catalog', 'information_schema', 'crdb_internal', 'pg_extension') """ - let tableCount = (try? await execute(query: query)) + let tableCount = (try? await catalogRead(query)) .flatMap { $0.rows.first?.first?.asText } .flatMap { Int($0) } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQBackedDriver+SchemaManagement.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQBackedDriver+SchemaManagement.swift index 459b7d9f8f..b65ba59b3f 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQBackedDriver+SchemaManagement.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQBackedDriver+SchemaManagement.swift @@ -38,7 +38,7 @@ extension LibPQBackedDriver { } func fetchSchemaDetails(name: String) async throws -> PluginSchemaDetails? { - let header = try await execute(query: PostgreSQLSchemaManagementQueries.ownerAndComment(schema: name)) + let header = try await catalogRead(PostgreSQLSchemaManagementQueries.ownerAndComment(schema: name)) guard let row = header.rows.first else { return nil } return PluginSchemaDetails( name: name, @@ -54,7 +54,7 @@ extension LibPQBackedDriver { /// `REVOKE` only removes what the executing role granted. private func fetchSchemaACL(name: String) async throws -> [PluginSchemaGrant] { guard supportsSchemaACLIntrospection else { return [] } - let result = try await execute(query: PostgreSQLSchemaManagementQueries.grants(schema: name)) + let result = try await catalogRead(PostgreSQLSchemaManagementQueries.grants(schema: name)) return result.rows.compactMap { row -> PluginSchemaGrant? in guard let privilege = row[safe: 2]?.asText else { return nil } let granteeId = row[safe: 0]?.asText.flatMap(Int.init) diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift index 4ac2de19bd..2963ffe184 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift @@ -14,6 +14,7 @@ final class LibPQDriverCore: @unchecked Sendable { private let config: DriverConnectionConfig private let schemaFallbackQueries: [String] private let singleConnectionMode: Bool + private let pinsCatalogSearchPath: Bool private let connectionLock = NSLock() private var _libpqConnection: LibPQPluginConnection? private var _lostConnection = false @@ -58,14 +59,19 @@ final class LibPQDriverCore: @unchecked Sendable { ) } + /// Redshift and CockroachDB pass `pinsCatalogSearchPath: false`: neither has user-defined operators, so their + /// catalog statements are safe through `pg_catalog`-qualified calls alone, and neither was measured with + /// `SET LOCAL search_path` naming `pg_temp`. init( config: DriverConnectionConfig, schemaFallbackQueries: [String] = PostgreSQLSchemaQueries.schemaFallbackQueries, - singleConnectionMode: Bool = false + singleConnectionMode: Bool = false, + pinsCatalogSearchPath: Bool = true ) { self.config = config self.schemaFallbackQueries = schemaFallbackQueries self.singleConnectionMode = singleConnectionMode + self.pinsCatalogSearchPath = pinsCatalogSearchPath } // MARK: - Connection @@ -92,7 +98,8 @@ final class LibPQDriverCore: @unchecked Sendable { ), suppressServerSideCancel: singleConnectionMode, connectTimeout: LibPQConnectTimeout(milliseconds: timeout.milliseconds - elapsedMilliseconds), - kerberos: kerberos + kerberos: kerberos, + pinsCatalogSearchPath: pinsCatalogSearchPath ) do { @@ -141,15 +148,23 @@ final class LibPQDriverCore: @unchecked Sendable { private func firstFallbackSchema(_ pqConn: LibPQPluginConnection) async -> String? { for query in schemaFallbackQueries { - if case .schema(let schema) = await probeSchema(pqConn, query: query) { + // The search-path read answers from the session's own path; a schema listing reads the catalogs. + let readsCatalogs = query != PostgreSQLSchemaQueries.firstSearchPathSchema + if case .schema(let schema) = await probeSchema(pqConn, query: query, readsCatalogs: readsCatalogs) { return schema } } return nil } - private func probeSchema(_ pqConn: LibPQPluginConnection, query: String) async -> PostgreSQLSchemaProbe { - let result = try? await pqConn.executeQuery(query) + private func probeSchema( + _ pqConn: LibPQPluginConnection, + query: String, + readsCatalogs: Bool = false + ) async -> PostgreSQLSchemaProbe { + let result = readsCatalogs && pinsCatalogSearchPath + ? try? await pqConn.executeTransactionScopedRead(PostgreSQLCatalogSearchPath.pinned + query) + : try? await pqConn.executeQuery(query) return PostgreSQLSchemaQueries.probe(rows: result?.rows) } @@ -204,7 +219,18 @@ final class LibPQDriverCore: @unchecked Sendable { ) } - func executeTransactionScopedRead(_ statement: String) async throws -> PluginQueryResult { + /// The one way a statement the driver writes against the catalogs runs. Under `PostgreSQLCatalogSearchPath`, + /// whatever schema the session is pinned to, so a function or operator in that schema cannot stand in for a + /// `pg_catalog` one. A definition that is replayed (table DDL, routines, triggers, types) reads pinned and comes + /// back qualified, as `pg_dump` writes it. `namesRelativeTo` is for what a person reads or compares across + /// schemas: the column spellings, index definitions, check constraints and partition bounds. + func executeCatalogRead(_ statement: String, namesRelativeTo schema: String? = nil) async throws -> PluginQueryResult { + guard pinsCatalogSearchPath else { return try await execute(query: statement) } + let path = schema.map(PostgreSQLCatalogSearchPath.relative(to:)) ?? PostgreSQLCatalogSearchPath.pinned + return try await executeTransactionScopedRead(path + statement) + } + + private func executeTransactionScopedRead(_ statement: String) async throws -> PluginQueryResult { let pqConn = try connection() let startTime = Date() let result = try await pqConn.executeTransactionScopedRead(statement) @@ -353,12 +379,14 @@ extension LibPQBackedDriver { ) } + func catalogRead(_ statement: String, namesRelativeTo schema: String? = nil) async throws -> PluginQueryResult { + try await core.executeCatalogRead(statement, namesRelativeTo: schema) + } + func fetchCheckConstraints(table: String, schema: String?) async throws -> [PluginCheckConstraintInfo] { - let query = PostgreSQLSchemaQueries.checkConstraintsQuery( - schema: schema ?? core.currentSchema, - table: table - ) - let result = try await execute(query: query) + let resolvedSchema = schema ?? core.currentSchema + let query = PostgreSQLSchemaQueries.checkConstraintsQuery(schema: resolvedSchema, table: table) + let result = try await catalogRead(query, namesRelativeTo: resolvedSchema) return result.rows.compactMap { row in guard let name = row[safe: 0]?.asText, let definition = row[safe: 1]?.asText else { return nil } @@ -373,7 +401,7 @@ extension LibPQBackedDriver { func fetchRelationComment(table: String, schema: String) async throws -> String? { let query = PostgreSQLCommentStatements.relationCommentQuery(name: table, schema: schema) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.first?.first?.asText?.nilIfEmpty } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+CatalogTypeNames.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+CatalogTypeNames.swift index a76530c883..0d6d1e60b6 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+CatalogTypeNames.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+CatalogTypeNames.swift @@ -17,7 +17,7 @@ internal extension LibPQPluginConnection { func learnTypeNames(for oids: [UInt32], conn: OpaquePointer) { preconditionOnQueue() guard let query = PostgreSQLCatalogTypeNames.lookupQuery(oids: oids) else { return } - let result: OpaquePointer? = query.withCString { PQexec(conn, $0) } + let result = pinnedCatalogSideQueryOnQueue(query, conn: conn) guard let result else { return } defer { PQclear(result) } @@ -36,8 +36,7 @@ internal extension LibPQPluginConnection { func noteCommandTag(_ tag: String?, conn: OpaquePointer) { preconditionOnQueue() guard tag == Self.createTypeCommandTag else { return } - let query = PostgreSQLSchemaQueries.enumTypeOidQuery - let result: OpaquePointer? = query.withCString { PQexec(conn, $0) } + let result = pinnedCatalogSideQueryOnQueue(PostgreSQLSchemaQueries.enumTypeOidQuery, conn: conn) guard let result else { return } defer { PQclear(result) } guard PQresultStatus(result) == PGRES_TUPLES_OK else { return } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift index 42f3e0fa84..d2b1d9c012 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift @@ -61,6 +61,7 @@ final class LibPQPluginConnection: @unchecked Sendable { private let suppressServerSideCancel: Bool private let connectTimeout: LibPQConnectTimeout private let kerberos: LibPQKerberosSignIn? + private let pinsCatalogSearchPath: Bool private let stateLock = NSLock() let cancellationGate = PluginQueryCancellationGate() @@ -124,7 +125,8 @@ final class LibPQPluginConnection: @unchecked Sendable { connectTimeout: LibPQConnectTimeout = LibPQConnectTimeout( milliseconds: LibPQConnectTimeout.defaultMilliseconds ), - kerberos: LibPQKerberosSignIn? = nil + kerberos: LibPQKerberosSignIn? = nil, + pinsCatalogSearchPath: Bool = true ) { self.host = host self.port = port @@ -137,6 +139,7 @@ final class LibPQPluginConnection: @unchecked Sendable { self.suppressServerSideCancel = suppressServerSideCancel self.connectTimeout = connectTimeout self.kerberos = kerberos + self.pinsCatalogSearchPath = pinsCatalogSearchPath } deinit { @@ -665,6 +668,10 @@ final class LibPQPluginConnection: @unchecked Sendable { return try await pluginDispatchAsync(on: queue) { [self] in guard !isShuttingDown else { throw LibPQPluginError.notConnected } + // The post-connect probes read the catalogs too; no transaction can be open yet. + if let deadline = activeConnectDeadline { + return try executeConnectQuerySync(statementToRun, deadline: deadline) + } guard isInsideTransactionBlockOnQueue() else { return try executeQuerySync(statementToRun) } @@ -681,6 +688,22 @@ final class LibPQPluginConnection: @unchecked Sendable { } } + /// A catalog query the driver sends on its own while reading a result, on the connection's queue, scoped the way + /// `executeTransactionScopedRead` scopes one. The caller clears the result. Inside an aborted block it hands back + /// the savepoint's own `25P02` failure. + func pinnedCatalogSideQueryOnQueue(_ statement: String, conn: OpaquePointer) -> OpaquePointer? { + preconditionOnQueue() + guard pinsCatalogSearchPath else { return statement.withCString { PQexec(conn, $0) } } + let pinned = PostgreSQLCatalogSearchPath.pinned + statement + guard isInsideTransactionBlockOnQueue() else { return pinned.withCString { PQexec(conn, $0) } } + guard let savepoint = Self.scopedReadSavepoint.withCString({ PQexec(conn, $0) }) else { return nil } + guard PQresultStatus(savepoint) == PGRES_COMMAND_OK else { return savepoint } + PQclear(savepoint) + let result = pinned.withCString { PQexec(conn, $0) } + if let rollback = Self.scopedReadRollback.withCString({ PQexec(conn, $0) }) { PQclear(rollback) } + return result + } + private static let scopedReadSavepoint = "SAVEPOINT tablepro_scoped_read" private static let scopedReadRollback = "ROLLBACK TO SAVEPOINT tablepro_scoped_read; RELEASE SAVEPOINT tablepro_scoped_read" diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostGISSpatialRewrite.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostGISSpatialRewrite.swift index f17fb6cfbb..a020f6c6bc 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostGISSpatialRewrite.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostGISSpatialRewrite.swift @@ -20,11 +20,17 @@ struct PostGISType: Equatable, Sendable { } enum PostGISSpatialRewrite { + // The conversion calls ST_AsEWKT from the type's schema, so only the postgis extension's own base types count: + // another role's enum or domain named geometry would route the call to that role's function. static let probeQuery = """ SELECT t.oid, t.typname, n.nspname FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace - WHERE t.typname IN ('geometry', 'geography') + JOIN pg_catalog.pg_depend d + ON d.classid = 'pg_catalog.pg_type'::pg_catalog.regclass AND d.objid = t.oid AND d.deptype = 'e' + AND d.refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass + JOIN pg_catalog.pg_extension x ON x.oid = d.refobjid + WHERE t.typtype = 'b' AND t.typname IN ('geometry', 'geography') AND x.extname = 'postgis' """ /// A user savepoint of the same name is safe: PostgreSQL resolves a repeated savepoint name to @@ -37,16 +43,16 @@ enum PostGISSpatialRewrite { guard type.name == "geometry" || type.name == "geography" else { return nil } let schema = PostgreSQLObjectQueries.quoteIdentifier(type.schema) let qualifiedType = PostgreSQLObjectQueries.qualifiedName(schema: type.schema, name: type.name) - return "SELECT \(schema).ST_AsEWKT(($1::text[])[i]::\(qualifiedType)) " - + "FROM pg_catalog.generate_subscripts($1::text[], 1) AS i ORDER BY i" + return "SELECT \(schema).ST_AsEWKT(($1::pg_catalog.text[])[i]::\(qualifiedType)) " + + "FROM pg_catalog.generate_subscripts($1::pg_catalog.text[], 1) AS i ORDER BY i" } static func arrayLiteral(from values: [String?]) -> String { let elements = values.map { value -> String in guard let value else { return "NULL" } let escaped = value - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "\"", with: "\\\"") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "\"", with: "\\\"", options: .literal) return "\"\(escaped)\"" } return "{\(elements.joined(separator: ","))}" diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLObjectQueries.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLObjectQueries.swift index b18bf733b4..f22e51b8d9 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLObjectQueries.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLObjectQueries.swift @@ -87,7 +87,8 @@ nonisolated public enum PostgreSQLObjectQueries { AND \(kindFilter) AND NOT EXISTS ( SELECT 1 FROM pg_catalog.pg_depend d - WHERE d.classid = 'pg_catalog.pg_proc'::pg_catalog.regclass AND d.objid = p.oid AND d.deptype = 'e' + WHERE d.classid = 'pg_catalog.pg_proc'::pg_catalog.regclass::pg_catalog.oid + AND d.objid = p.oid AND d.deptype = 'e' ) ORDER BY p.proname, arguments """ @@ -97,7 +98,7 @@ nonisolated public enum PostgreSQLObjectQueries { /// instead of whichever row the planner happened to return first. public static func routineDefinition(identity: String) -> String { """ - SELECT pg_catalog.pg_get_functiondef(\(quoteLiteral(identity))::oid) + SELECT pg_catalog.pg_get_functiondef(\(quoteLiteral(identity))::pg_catalog.oid) """ } @@ -129,16 +130,16 @@ nonisolated public enum PostgreSQLObjectQueries { t.tgname AS name, c.relname AS table_name, n.nspname AS schema, - CASE WHEN (t.tgtype & 64) != 0 THEN 'INSTEAD OF' - WHEN (t.tgtype & 2) != 0 THEN 'BEFORE' + CASE WHEN (t.tgtype::pg_catalog.int4 & 64) <> 0 THEN 'INSTEAD OF' + WHEN (t.tgtype::pg_catalog.int4 & 2) <> 0 THEN 'BEFORE' ELSE 'AFTER' END AS timing, - concat_ws(' OR ', - CASE WHEN (t.tgtype & 4) != 0 THEN 'INSERT' END, - CASE WHEN (t.tgtype & 8) != 0 THEN 'DELETE' END, - CASE WHEN (t.tgtype & 16) != 0 THEN 'UPDATE' END, - CASE WHEN (t.tgtype & 32) != 0 THEN 'TRUNCATE' END + pg_catalog.concat_ws(' OR ', + CASE WHEN (t.tgtype::pg_catalog.int4 & 4) <> 0 THEN 'INSERT' END, + CASE WHEN (t.tgtype::pg_catalog.int4 & 8) <> 0 THEN 'DELETE' END, + CASE WHEN (t.tgtype::pg_catalog.int4 & 16) <> 0 THEN 'UPDATE' END, + CASE WHEN (t.tgtype::pg_catalog.int4 & 32) <> 0 THEN 'TRUNCATE' END ) AS event, - CASE WHEN (t.tgtype & 1) != 0 THEN 'ROW' ELSE 'STATEMENT' END AS orientation, + CASE WHEN (t.tgtype::pg_catalog.int4 & 1) <> 0 THEN 'ROW' ELSE 'STATEMENT' END AS orientation, t.tgenabled <> 'D' AS enabled, pg_catalog.pg_get_triggerdef(t.oid) AS definition, pg_catalog.pg_get_userbyid(c.relowner) AS owner @@ -166,7 +167,7 @@ nonisolated public enum PostgreSQLObjectQueries { static func userDefinedTypeList(schema: String?, identity: String?, capabilities: PostgreSQLCapabilities) -> String { let predicates = [ schema.map { "AND n.nspname = \(quoteLiteral($0))" }, - identity.flatMap { UInt32($0) }.map { "AND t.oid = \($0)::oid" } + identity.flatMap { UInt32($0) }.map { "AND t.oid = \($0)::pg_catalog.oid" } ].compactMap { $0 } return userDefinedTypeList(predicates: predicates, orderBy: "t.typname", capabilities: capabilities) } @@ -186,7 +187,7 @@ nonisolated public enum PostgreSQLObjectQueries { ) -> String { let kinds = capabilities.hasRangeTypes ? "('e', 'c', 'd', 'r')" : "('e', 'c', 'd')" let fieldCollation = """ - CASE WHEN a.attcollation <> 0 AND a.attcollation <> ty.typcollation + CASE WHEN a.attcollation <> 0::pg_catalog.oid AND a.attcollation <> ty.typcollation THEN \(collationName("a.attcollation")) END """ let rangeJoin = capabilities.hasRangeTypes ? "LEFT JOIN pg_catalog.pg_range r ON r.rngtypid = t.oid" : "" @@ -194,10 +195,10 @@ nonisolated public enum PostgreSQLObjectQueries { ? "CASE WHEN t.typtype = 'r' THEN pg_catalog.format_type(r.rngsubtype, NULL) END" : "NULL::text" let rangeCanonical = capabilities.hasRangeTypes - ? "CASE WHEN t.typtype = 'r' AND r.rngcanonical <> 0 THEN r.rngcanonical::regproc::text END" + ? "CASE WHEN t.typtype = 'r' AND r.rngcanonical::pg_catalog.oid <> 0::pg_catalog.oid THEN r.rngcanonical::pg_catalog.text END" : "NULL::text" let rangeSubtypeDiff = capabilities.hasRangeTypes - ? "CASE WHEN t.typtype = 'r' AND r.rngsubdiff <> 0 THEN r.rngsubdiff::regproc::text END" + ? "CASE WHEN t.typtype = 'r' AND r.rngsubdiff::pg_catalog.oid <> 0::pg_catalog.oid THEN r.rngsubdiff::pg_catalog.text END" : "NULL::text" let rangeOpclass = capabilities.hasRangeTypes ? """ CASE WHEN t.typtype = 'r' THEN @@ -208,7 +209,7 @@ nonisolated public enum PostgreSQLObjectQueries { END """ : "NULL::text" let rangeCollation = capabilities.hasRangeTypes ? """ - CASE WHEN t.typtype = 'r' AND r.rngcollation <> 0 + CASE WHEN t.typtype = 'r' AND r.rngcollation <> 0::pg_catalog.oid AND r.rngcollation <> (SELECT st.typcollation FROM pg_catalog.pg_type st WHERE st.oid = r.rngsubtype) THEN \(collationName("r.rngcollation")) END @@ -229,13 +230,13 @@ nonisolated public enum PostgreSQLObjectQueries { t.typtype::text AS kind, pg_catalog.pg_get_userbyid(t.typowner) AS owner, pg_catalog.obj_description(t.oid, 'pg_type') AS comment, - (SELECT array_agg(e.enumlabel ORDER BY e.enumsortorder) + (SELECT pg_catalog.array_agg(e.enumlabel ORDER BY e.enumsortorder) FROM pg_catalog.pg_enum e WHERE e.enumtypid = t.oid)::text AS enum_labels, \(compositeFieldArray("a.attname")) AS field_names, \(compositeFieldArray("pg_catalog.format_type(a.atttypid, a.atttypmod)")) AS field_types, \(compositeFieldArray(fieldCollation, joinsType: true)) AS field_collations, CASE WHEN t.typtype = 'd' THEN pg_catalog.format_type(t.typbasetype, t.typtypmod) END AS base_type, - CASE WHEN t.typtype = 'd' AND t.typcollation <> 0 + CASE WHEN t.typtype = 'd' AND t.typcollation <> 0::pg_catalog.oid AND t.typcollation <> (SELECT b.typcollation FROM pg_catalog.pg_type b WHERE b.oid = t.typbasetype) THEN \(collationName("t.typcollation")) END AS collation, @@ -259,7 +260,8 @@ nonisolated public enum PostgreSQLObjectQueries { AND (t.typtype <> 'c' OR c.relkind = 'c') AND NOT EXISTS ( SELECT 1 FROM pg_catalog.pg_depend d - WHERE d.classid = 'pg_catalog.pg_type'::regclass AND d.objid = t.oid AND d.deptype = 'e' + WHERE d.classid = 'pg_catalog.pg_type'::pg_catalog.regclass::pg_catalog.oid + AND d.objid = t.oid AND d.deptype = 'e' ) \(predicates.joined(separator: "\n ")) ORDER BY \(orderBy) @@ -271,7 +273,7 @@ nonisolated public enum PostgreSQLObjectQueries { private static func compositeFieldArray(_ expression: String, joinsType: Bool = false) -> String { let typeJoin = joinsType ? "\n JOIN pg_catalog.pg_type ty ON ty.oid = a.atttypid" : "" return """ - (SELECT array_agg(\(expression) ORDER BY a.attnum) + (SELECT pg_catalog.array_agg(\(expression) ORDER BY a.attnum) FROM pg_catalog.pg_attribute a\(typeJoin) WHERE a.attrelid = t.typrelid AND a.attnum > 0 AND NOT a.attisdropped)::text """ @@ -279,7 +281,7 @@ nonisolated public enum PostgreSQLObjectQueries { private static func domainCheckArray(_ expression: String) -> String { """ - (SELECT array_agg(\(expression) ORDER BY con.conname) + (SELECT pg_catalog.array_agg(\(expression) ORDER BY con.conname) FROM pg_catalog.pg_constraint con WHERE con.contypid = t.oid AND con.contype = 'c')::text """ diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+BulkMetadata.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+BulkMetadata.swift index 7b66e93149..dc60c02e2c 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+BulkMetadata.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+BulkMetadata.swift @@ -25,7 +25,7 @@ extension PostgreSQLPluginDriver { let query = PostgreSQLIndexQueries.indexList( schema: resolvedSchema, table: nil, capabilities: catalogCapabilities ) - let result = try await execute(query: query) + let result = try await catalogRead(query, namesRelativeTo: resolvedSchema) let ddl = try await fetchIndexSpellings(schema: resolvedSchema, table: nil) var indexes: [String: [PluginIndexInfo]] = [:] @@ -39,21 +39,8 @@ extension PostgreSQLPluginDriver { var providesBulkTableMetadataFetch: Bool { true } func fetchAllTableMetadata(schema: String?) async throws -> [String: PluginTableMetadata] { - let schemaLiteral = PostgreSQLObjectQueries.quoteLiteral(schema ?? core.currentSchema) - let query = """ - SELECT - c.relname AS table_name, - pg_total_relation_size(c.oid) AS total_size, - pg_table_size(c.oid) AS data_size, - pg_indexes_size(c.oid) AS index_size, - c.reltuples::bigint AS row_count, - obj_description(c.oid, 'pg_class') AS comment - FROM pg_class c - JOIN pg_namespace n ON n.oid = c.relnamespace - WHERE n.nspname = \(schemaLiteral) AND c.relkind IN ('r', 'p', 'm', 'f') - ORDER BY c.relname - """ - let result = try await execute(query: query) + let query = PostgreSQLSchemaQueries.allTableMetadata(schema: schema ?? core.currentSchema) + let result = try await catalogRead(query) var metadata: [String: PluginTableMetadata] = [:] for row in result.rows { diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+ColumnReorder.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+ColumnReorder.swift index 260d95fe3b..3d420e4cac 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+ColumnReorder.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+ColumnReorder.swift @@ -64,20 +64,20 @@ extension PostgreSQLPluginDriver { /// Named, and added after the staging table is gone. Declared inline instead, PostgreSQL /// finds the name already taken and quietly picks another. parts.tableConstraints = try await textRows(""" - SELECT 'CONSTRAINT ' || quote_ident(con.conname) || ' ' || pg_get_constraintdef(con.oid, true) - FROM pg_constraint con - JOIN pg_class c ON c.oid = con.conrelid - JOIN pg_namespace n ON n.oid = c.relnamespace + SELECT 'CONSTRAINT ' || pg_catalog.quote_ident(con.conname) || ' ' || pg_catalog.pg_get_constraintdef(con.oid, true) + FROM pg_catalog.pg_constraint con + JOIN pg_catalog.pg_class c ON c.oid = con.conrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND con.contype IN ('p', 'u', 'c', 'x') ORDER BY CASE con.contype WHEN 'p' THEN 0 WHEN 'u' THEN 1 ELSE 2 END, con.conname """) parts.outboundForeignKeys = try await textRows(""" - SELECT 'CONSTRAINT ' || quote_ident(con.conname) || ' ' || pg_get_constraintdef(con.oid, true) - FROM pg_constraint con - JOIN pg_class c ON c.oid = con.conrelid - JOIN pg_namespace n ON n.oid = c.relnamespace + SELECT 'CONSTRAINT ' || pg_catalog.quote_ident(con.conname) || ' ' || pg_catalog.pg_get_constraintdef(con.oid, true) + FROM pg_catalog.pg_constraint con + JOIN pg_catalog.pg_class c ON c.oid = con.conrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND con.contype = 'f' ORDER BY con.conname """) @@ -86,23 +86,23 @@ extension PostgreSQLPluginDriver { /// the only thing keeping it alive. Dropping every one is what lets the staging table go; /// re-adding them against the rebuilt table happens once its primary key is back. let inboundClause = """ - FROM pg_constraint con - JOIN pg_class c ON c.oid = con.confrelid - JOIN pg_namespace n ON n.oid = c.relnamespace - JOIN pg_class c2 ON c2.oid = con.conrelid - JOIN pg_namespace n2 ON n2.oid = c2.relnamespace + FROM pg_catalog.pg_constraint con + JOIN pg_catalog.pg_class c ON c.oid = con.confrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + JOIN pg_catalog.pg_class c2 ON c2.oid = con.conrelid + JOIN pg_catalog.pg_namespace n2 ON n2.oid = c2.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND con.contype = 'f' AND con.conrelid <> con.confrelid ORDER BY con.conname """ parts.inboundForeignKeyDrops = try await textRows(""" - SELECT 'ALTER TABLE ' || quote_ident(n2.nspname) || '.' || quote_ident(c2.relname) - || ' DROP CONSTRAINT ' || quote_ident(con.conname) + SELECT 'ALTER TABLE ' || pg_catalog.quote_ident(n2.nspname) || '.' || pg_catalog.quote_ident(c2.relname) + || ' DROP CONSTRAINT ' || pg_catalog.quote_ident(con.conname) \(inboundClause) """) parts.inboundForeignKeyAdds = try await textRows(""" - SELECT 'ALTER TABLE ' || quote_ident(n2.nspname) || '.' || quote_ident(c2.relname) - || ' ADD CONSTRAINT ' || quote_ident(con.conname) || ' ' || pg_get_constraintdef(con.oid, true) + SELECT 'ALTER TABLE ' || pg_catalog.quote_ident(n2.nspname) || '.' || pg_catalog.quote_ident(c2.relname) + || ' ADD CONSTRAINT ' || pg_catalog.quote_ident(con.conname) || ' ' || pg_catalog.pg_get_constraintdef(con.oid, true) \(inboundClause) """) @@ -113,10 +113,10 @@ extension PostgreSQLPluginDriver { parts.invalidIndexes = standaloneIndexes.invalidNames parts.triggers = try await textRows(""" - SELECT pg_get_triggerdef(t.oid, true) - FROM pg_trigger t - JOIN pg_class c ON c.oid = t.tgrelid - JOIN pg_namespace n ON n.oid = c.relnamespace + SELECT pg_catalog.pg_get_triggerdef(t.oid, true) + FROM pg_catalog.pg_trigger t + JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND NOT t.tgisinternal ORDER BY t.tgname """) @@ -126,16 +126,16 @@ extension PostgreSQLPluginDriver { /// the user disabled, or set to fire only on a replica or always, silently starts firing on /// writes it was excluded from. parts.triggerModes = try await textRows(""" - SELECT 'ALTER TABLE ' || quote_ident(n.nspname) || '.' || quote_ident(c.relname) + SELECT 'ALTER TABLE ' || pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname) || CASE t.tgenabled WHEN 'D' THEN ' DISABLE TRIGGER ' WHEN 'R' THEN ' ENABLE REPLICA TRIGGER ' WHEN 'A' THEN ' ENABLE ALWAYS TRIGGER ' END - || quote_ident(t.tgname) - FROM pg_trigger t - JOIN pg_class c ON c.oid = t.tgrelid - JOIN pg_namespace n ON n.oid = c.relnamespace + || pg_catalog.quote_ident(t.tgname) + FROM pg_catalog.pg_trigger t + JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND NOT t.tgisinternal AND t.tgenabled IN ('D', 'R', 'A') ORDER BY t.tgname @@ -145,50 +145,50 @@ extension PostgreSQLPluginDriver { /// still calls. The server writes the whole statement so no identifier has to be quoted or /// escaped here. parts.serialSequenceHandovers = try await textRows(""" - SELECT 'ALTER SEQUENCE ' || pg_get_serial_sequence( - quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname + SELECT 'ALTER SEQUENCE ' || pg_catalog.pg_get_serial_sequence( + pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname), a.attname ) - || ' OWNED BY ' || quote_ident(n.nspname) || '.' || quote_ident(c.relname) - || '.' || quote_ident(a.attname) - FROM pg_attribute a - JOIN pg_class c ON c.oid = a.attrelid - JOIN pg_namespace n ON n.oid = c.relnamespace + || ' OWNED BY ' || pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname) + || '.' || pg_catalog.quote_ident(a.attname) + FROM pg_catalog.pg_attribute a + JOIN pg_catalog.pg_class c ON c.oid = a.attrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND a.attnum > 0 AND NOT a.attisdropped AND \(caps.hasIdentityColumns ? "a.attidentity NOT IN ('a', 'd')" : "true") - AND pg_get_serial_sequence( - quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname + AND pg_catalog.pg_get_serial_sequence( + pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname), a.attname ) IS NOT NULL ORDER BY a.attnum """) parts.comments = try await textRows(""" - SELECT 'COMMENT ON TABLE ' || quote_ident(n.nspname) || '.' || quote_ident(c.relname) - || ' IS ' || quote_literal(obj_description(c.oid, 'pg_class')) - FROM pg_class c - JOIN pg_namespace n ON n.oid = c.relnamespace + SELECT 'COMMENT ON TABLE ' || pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname) + || ' IS ' || pg_catalog.quote_literal(pg_catalog.obj_description(c.oid, 'pg_class')) + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) - AND obj_description(c.oid, 'pg_class') IS NOT NULL + AND pg_catalog.obj_description(c.oid, 'pg_class') IS NOT NULL UNION ALL - SELECT 'COMMENT ON COLUMN ' || quote_ident(n.nspname) || '.' || quote_ident(c.relname) - || '.' || quote_ident(a.attname) - || ' IS ' || quote_literal(col_description(c.oid, a.attnum)) - FROM pg_attribute a - JOIN pg_class c ON c.oid = a.attrelid - JOIN pg_namespace n ON n.oid = c.relnamespace + SELECT 'COMMENT ON COLUMN ' || pg_catalog.quote_ident(n.nspname) || '.' || pg_catalog.quote_ident(c.relname) + || '.' || pg_catalog.quote_ident(a.attname) + || ' IS ' || pg_catalog.quote_literal(pg_catalog.col_description(c.oid, a.attnum)) + FROM pg_catalog.pg_attribute a + JOIN pg_catalog.pg_class c ON c.oid = a.attrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND a.attnum > 0 AND NOT a.attisdropped - AND col_description(c.oid, a.attnum) IS NOT NULL + AND pg_catalog.col_description(c.oid, a.attnum) IS NOT NULL """) parts.dependentViews = try await textRows(""" - SELECT DISTINCT quote_ident(dn.nspname) || '.' || quote_ident(dc.relname) - FROM pg_depend d - JOIN pg_rewrite r ON r.oid = d.objid - JOIN pg_class dc ON dc.oid = r.ev_class - JOIN pg_namespace dn ON dn.oid = dc.relnamespace - JOIN pg_class c ON c.oid = d.refobjid - JOIN pg_namespace n ON n.oid = c.relnamespace + SELECT DISTINCT pg_catalog.quote_ident(dn.nspname) || '.' || pg_catalog.quote_ident(dc.relname) + FROM pg_catalog.pg_depend d + JOIN pg_catalog.pg_rewrite r ON r.oid = d.objid + JOIN pg_catalog.pg_class dc ON dc.oid = r.ev_class + JOIN pg_catalog.pg_namespace dn ON dn.oid = dc.relnamespace + JOIN pg_catalog.pg_class c ON c.oid = d.refobjid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND dc.relkind IN ('v', 'm') AND dc.oid <> c.oid @@ -202,10 +202,10 @@ extension PostgreSQLPluginDriver { let query = PostgreSQLSchemaQueries.rebuildColumnsQuery( schema: schema, table: table, capabilities: versionedCapabilities ) - return try await execute(query: query).rows + return try await catalogRead(query).rows } private func textRows(_ query: String) async throws -> [String] { - try await execute(query: query).rows.compactMap { $0[safe: 0]?.asText } + try await catalogRead(query).rows.compactMap { $0[safe: 0]?.asText } } } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Columns.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Columns.swift index b6951e9d82..6fb587159d 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Columns.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Columns.swift @@ -16,10 +16,11 @@ extension PostgreSQLPluginDriver { capabilities: versionedCapabilities, includeMaterializedViews: includesMaterializedViews() ) - let result = try await executeSchemaRelativeRead(query, schema: resolvedSchema) + let result = try await catalogRead(query) + let spellings = try await fetchColumnSpellings(schema: resolvedSchema, table: table)[table] ?? [:] let columnDDL = try await fetchColumnDDL(schema: resolvedSchema, table: table)[table] ?? [:] return result.rows.compactMap { row in - mapPgColumnRow(row, tableNameOffset: 0, catalog: catalog, columnDDL: columnDDL) + mapPgColumnRow(row, tableNameOffset: 0, catalog: catalog, spellings: spellings, columnDDL: columnDDL) } } @@ -32,19 +33,34 @@ extension PostgreSQLPluginDriver { capabilities: versionedCapabilities, includeMaterializedViews: includesMaterializedViews() ) - let result = try await executeSchemaRelativeRead(query, schema: resolvedSchema) + let result = try await catalogRead(query) + let spellings = try await fetchColumnSpellings(schema: resolvedSchema, table: nil) let columnDDL = try await fetchColumnDDL(schema: resolvedSchema, table: nil) var allColumns: [String: [PluginColumnInfo]] = [:] for row in result.rows { guard row.count >= 5, let tableName = row[0].asText else { continue } let tableDDL = columnDDL[tableName] ?? [:] - if let column = mapPgColumnRow(row, tableNameOffset: 1, catalog: catalog, columnDDL: tableDDL) { + let tableSpellings = spellings[tableName] ?? [:] + if let column = mapPgColumnRow( + row, tableNameOffset: 1, catalog: catalog, spellings: tableSpellings, columnDDL: tableDDL + ) { allColumns[tableName, default: []].append(column) } } return allColumns } + fileprivate func fetchColumnSpellings( + schema: String, + table: String? + ) async throws -> [String: [String: PostgreSQLColumnSpelling]] { + let query = PostgreSQLSchemaQueries.columnSpellingQuery( + schema: schema, table: table, capabilities: versionedCapabilities + ) + let result = try await catalogRead(query, namesRelativeTo: schema) + return PostgreSQLSchemaQueries.columnSpellings(rows: result.rows) + } + fileprivate func fetchColumnDDL( schema: String, table: String? @@ -52,12 +68,12 @@ extension PostgreSQLPluginDriver { let query = PostgreSQLSchemaQueries.columnDDLQuery( schema: schema, table: table, capabilities: versionedCapabilities ) - let result = try await executeQualifiedRead(query) + let result = try await catalogRead(query) return PostgreSQLSchemaQueries.columnDDL(rows: result.rows) } fileprivate func fetchEnumLabelMap(schema: String, table: String?) async throws -> [String: [String]] { - let result = try await execute(query: PostgreSQLSchemaQueries.enumLabelQuery(schema: schema, table: table)) + let result = try await catalogRead(PostgreSQLSchemaQueries.enumLabelQuery(schema: schema, table: table)) var map: [String: [String]] = [:] for row in result.rows { guard let schemaName = row[safe: 0]?.asText, @@ -73,7 +89,7 @@ extension PostgreSQLPluginDriver { } fileprivate func fetchArrayTypeMap(schema: String, table: String?) async throws -> [String: PostgresArrayTypeInfo] { - let result = try await execute(query: PostgreSQLSchemaQueries.arrayTypeQuery(schema: schema, table: table)) + let result = try await catalogRead(PostgreSQLSchemaQueries.arrayTypeQuery(schema: schema, table: table)) var map: [String: PostgresArrayTypeInfo] = [:] for row in result.rows { guard let schemaName = row[safe: 0]?.asText, @@ -98,12 +114,12 @@ extension PostgreSQLPluginDriver { _ row: [PluginCellValue], tableNameOffset: Int, catalog: PostgresTypeCatalog, + spellings: [String: PostgreSQLColumnSpelling], columnDDL: [String: PostgreSQLCatalogColumnDDL] ) -> PluginColumnInfo? { let nameIdx = tableNameOffset let typeIdx = tableNameOffset + 1 let nullableIdx = tableNameOffset + 2 - let defaultIdx = tableNameOffset + 3 let collationIdx = tableNameOffset + 4 let commentIdx = tableNameOffset + 5 let udtIdx = tableNameOffset + 6 @@ -111,14 +127,13 @@ extension PostgreSQLPluginDriver { let identityIdx = tableNameOffset + 8 let generatedIdx = tableNameOffset + 9 let udtSchemaIdx = tableNameOffset + 10 - let generationExpressionIdx = tableNameOffset + 11 - let declaredTypeIdx = tableNameOffset + 12 let domainNameIdx = tableNameOffset + 13 guard row.count > typeIdx, let name = row[nameIdx].asText, let rawDataType = row[typeIdx].asText else { return nil } + let spelling = spellings[name] let udtName = row.count > udtIdx ? row[udtIdx].asText : nil let udtSchema = row.count > udtSchemaIdx ? row[udtSchemaIdx].asText : nil @@ -130,8 +145,8 @@ extension PostgreSQLPluginDriver { arrayTypesByQualifiedName: catalog.arrayTypes ) let allowedValues = resolution.allowedValues - let spelling = PostgreSQLColumnTypeSpelling.resolve( - declaredType: row.count > declaredTypeIdx ? row[declaredTypeIdx].asText : nil, + let typeSpelling = PostgreSQLColumnTypeSpelling.resolve( + declaredType: spelling?.declaredType, informationSchemaType: rawDataType, domainName: row.count > domainNameIdx ? row[domainNameIdx].asText : nil, udtSchema: udtSchema, @@ -139,7 +154,6 @@ extension PostgreSQLPluginDriver { ) let isNullable = row.count > nullableIdx && row[nullableIdx].asText == "YES" - let defaultValue = row.count > defaultIdx ? row[defaultIdx].asText : nil let collation = row.count > collationIdx ? row[collationIdx].asText : nil let comment = row.count > commentIdx ? row[commentIdx].asText : nil let isPk = row.count > pkIdx && row[pkIdx].asText == "YES" @@ -153,24 +167,23 @@ extension PostgreSQLPluginDriver { return PluginColumnInfo( name: name, - dataType: spelling.dataType, + dataType: typeSpelling.dataType, isNullable: isNullable, isPrimaryKey: isPk, - defaultValue: defaultValue, + defaultValue: spelling?.defaultValue, charset: charset, collation: collation, comment: comment?.isEmpty == false ? comment : nil, identityKind: pgIdentityKind(attidentity), isGenerated: attgenerated == "s" || attgenerated == "v", allowedValues: allowedValues, - generationExpression: row.count > generationExpressionIdx - ? row[generationExpressionIdx].asText.flatMap { $0.nilIfEmpty } : nil, + generationExpression: spelling?.generationExpression, generationKind: pgGenerationKind(attgenerated), ddlSpelling: columnDDL[name]?.typeSpelling, ddlDefault: columnDDL[name]?.defaultExpression, ddlGenerationExpression: columnDDL[name]?.generationExpression, ddlCollation: columnDDL[name]?.collation, - classificationTypeName: spelling.classificationTypeName + classificationTypeName: typeSpelling.classificationTypeName ) } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Comments.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Comments.swift index cab3e1bb5f..de75ea4787 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Comments.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Comments.swift @@ -15,7 +15,7 @@ extension PostgreSQLPluginDriver { func fetchCommentDDL(table: String, schema: String?) async throws -> [String] { let resolvedSchema = schema ?? core.currentSchema let query = PostgreSQLCommentStatements.catalogQuery(name: table, schema: resolvedSchema) - let result = try await execute(query: query) + let result = try await catalogRead(query) return PostgreSQLCommentStatements.statements( name: table, schema: resolvedSchema, diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+EnumTypes.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+EnumTypes.swift index 1e050e12e6..9eb8f64f03 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+EnumTypes.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+EnumTypes.swift @@ -17,7 +17,7 @@ private let enumProbeLogger = Logger( extension PostgreSQLPluginDriver { func probeEnumOids() async { do { - let result = try await core.execute(query: PostgreSQLSchemaQueries.enumTypeOidQuery) + let result = try await core.executeCatalogRead(PostgreSQLSchemaQueries.enumTypeOidQuery) let rows = result.rows.map { row in row.map(\.asText) } core.mergeCatalogTypeNames(PostgreSQLCatalogTypeNames.enumProbeNames(rows: rows)) } catch { diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Principals.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Principals.swift index 8fe1179131..a8d551357f 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Principals.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Principals.swift @@ -32,7 +32,7 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { pattern: query, limit: limit ) - let result = try await execute(query: sql) + let result = try await catalogRead(sql) return result.rows.compactMap { row in guard let schema = row[safe: 0]?.asText, @@ -47,7 +47,7 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { let query = PostgreSQLPrincipalQueries.principals( includeBypassRLS: versionedCapabilities.hasBypassRLS ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { row -> PluginPrincipalInfo? in guard let name = row[safe: 0]?.asText else { return nil } @@ -111,7 +111,7 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { } private func schemas(in database: String) async throws -> [PluginPrivilegeScope] { - let result = try await execute(query: PostgreSQLPrincipalQueries.schemas) + let result = try await catalogRead(PostgreSQLPrincipalQueries.schemas) return result.rows.compactMap { row in guard let schema = row[safe: 0]?.asText else { return nil } return .schema(database: database, schema: schema) @@ -120,7 +120,7 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { private func tables(in database: String, schema: String) async throws -> [PluginPrivilegeScope] { let query = PostgreSQLPrincipalQueries.tables(schema: schema) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { row in guard let table = row[safe: 0]?.asText else { return nil } return .table(database: database, schema: schema, table: table) @@ -136,7 +136,7 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { schema: schema, table: table ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { row in guard let column = row[safe: 0]?.asText else { return nil } return .column(database: database, schema: schema, table: table, column: column) @@ -145,7 +145,7 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { private func fetchColumnGrants(role: String, database: String) async throws -> [PluginGrantInfo] { let query = PostgreSQLPrincipalQueries.columnGrants(role: role) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { row -> PluginGrantInfo? in guard let schema = row[safe: 0]?.asText, let table = row[safe: 1]?.asText, @@ -160,7 +160,7 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { } func currentPrincipalRef() async throws -> PluginPrincipalRef? { - let result = try await execute(query: PostgreSQLPrincipalQueries.currentPrincipal) + let result = try await catalogRead(PostgreSQLPrincipalQueries.currentPrincipal) guard let name = result.rows.first?[safe: 0]?.asText else { return nil } return PluginPrincipalRef(name: name) } @@ -169,12 +169,12 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { let query = PostgreSQLPrincipalQueries.ownsObjects( role: principal.name ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return PostgreSQLCatalogBoolean.isTrue(result.rows.first?[safe: 0]?.asText) } private func fetchMemberships() async throws -> [String: [String]] { - let result = try await execute(query: PostgreSQLPrincipalQueries.memberships) + let result = try await catalogRead(PostgreSQLPrincipalQueries.memberships) var memberships: [String: [String]] = [:] for row in result.rows { guard let member = row[safe: 0]?.asText, @@ -185,13 +185,13 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { } private func currentDatabaseName() async throws -> String { - let result = try await execute(query: PostgreSQLPrincipalQueries.currentDatabase) + let result = try await catalogRead(PostgreSQLPrincipalQueries.currentDatabase) return result.rows.first?[safe: 0]?.asText ?? "" } private func fetchDatabaseGrants(role: String) async throws -> [PluginGrantInfo] { let query = PostgreSQLPrincipalQueries.databaseGrants(role: role) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { row -> PluginGrantInfo? in guard let database = row[safe: 0]?.asText, let privilege = row[safe: 1]?.asText else { return nil } @@ -205,7 +205,7 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { private func fetchSchemaGrants(role: String, database: String) async throws -> [PluginGrantInfo] { let query = PostgreSQLPrincipalQueries.schemaGrants(role: role) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { row -> PluginGrantInfo? in guard let schema = row[safe: 0]?.asText, let privilege = row[safe: 1]?.asText else { return nil } @@ -219,7 +219,7 @@ extension PostgreSQLPluginDriver: PluginPrincipalManagement { private func fetchTableGrants(role: String, database: String) async throws -> [PluginGrantInfo] { let query = PostgreSQLPrincipalQueries.tableGrants(role: role) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { row -> PluginGrantInfo? in guard let schema = row[safe: 0]?.asText, let table = row[safe: 1]?.asText, diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+QualifiedRead.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+QualifiedRead.swift deleted file mode 100644 index 961abb53be..0000000000 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+QualifiedRead.swift +++ /dev/null @@ -1,31 +0,0 @@ -// -// PostgreSQLPluginDriver+QualifiedRead.swift -// PostgreSQLDriverPlugin -// - -import Foundation -import TableProPluginKit - -extension PostgreSQLPluginDriver { - /// Reads with `search_path` narrowed to `pg_catalog`, so every name the server deparses comes - /// back qualified. A view body and a column's type, default and generation expression go through - /// here, because each is replayed as SQL on a connection whose path is some other schema. - /// - /// The narrowed path lasts only as long as the read, including on a session a query tab left - /// inside `BEGIN`; `LibPQPluginConnection.executeTransactionScopedRead` says how. - func executeQualifiedRead(_ query: String) async throws -> PluginQueryResult { - try await core.executeTransactionScopedRead(PostgreSQLViewDefinition.qualifiedReadPrefix + query) - } - - /// Reads with `search_path` narrowed to `pg_catalog` and one schema, so every name the server - /// deparses is relative to that schema: its own types bare, other schemas' qualified. The column - /// read goes through here, because that is the spelling a person reads in the Structure tab and - /// the one two schemas are compared in. - /// - /// Scoped to the read the same way `executeQualifiedRead` is. - func executeSchemaRelativeRead(_ query: String, schema: String) async throws -> PluginQueryResult { - try await core.executeTransactionScopedRead( - PostgreSQLSchemaQueries.schemaRelativeReadPrefix(schema: schema) + query - ) - } -} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+RoutineDebugging.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+RoutineDebugging.swift index f304638350..73280a66b8 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+RoutineDebugging.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+RoutineDebugging.swift @@ -17,11 +17,12 @@ extension PostgreSQLPluginDriver: PluginRoutineDebugging { func debugTargets(schema: String?) async throws -> [PluginDebugTarget] { guard canDebugRoutines else { return [] } + let resolvedSchema = schema ?? currentSchema ?? "public" let query = PostgreSQLRoutineDebugProtocol.targetListQuery( - schema: schema ?? currentSchema ?? "public", + schema: resolvedSchema, hasProcedureKind: catalogCapabilities.hasProcedureKind ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { PostgreSQLRoutineDebugProtocol.target(fromListRow: $0.map(\.asText)) } } @@ -55,10 +56,11 @@ extension PostgreSQLPluginDriver: PluginRoutineDebugging { try await target.prepareForDebugging(isTarget: true) let targetProcess = try? await target.execute(query: "SELECT pg_catalog.pg_backend_pid()") .rows.first?.first?.asText.flatMap { Int($0) } - let call = try PostgreSQLRoutineDebugProtocol.call(for: routine, arguments: request.arguments) guard let oid = UInt32(routine.identity) else { throw PostgreSQLRoutineDebugError.routineNotFound(routine.displayName) } + let callable = try await target.withDefaultsSpelledForThisSession(routine, oid: oid) + let call = try PostgreSQLRoutineDebugProtocol.call(for: callable, arguments: request.arguments) do { _ = try await target.execute( query: PostgreSQLRoutineDebugProtocol.oidDebugStatement(extensionSchema: extensionSchema, oid: oid) @@ -108,12 +110,23 @@ extension PostgreSQLPluginDriver: PluginRoutineDebugging { } } + private func withDefaultsSpelledForThisSession( + _ routine: PostgreSQLDebugRoutine, + oid: UInt32 + ) async throws -> PostgreSQLDebugRoutine { + guard let statement = PostgreSQLRoutineDebugProtocol.targetDefaultsStatement( + oid: oid, argumentCount: routine.defaultExpressions.count + ) else { return routine } + let spelled = try await execute(query: statement).rows.first?.first?.asText + return routine.withDefaultExpressions(PostgreSQLTextArray.elements(spelled)) + } + private func debugRoutine(identity: String, name: String) async throws -> PostgreSQLDebugRoutine { guard let oid = PostgreSQLRoutineDebugProtocol.oid(inIdentity: identity) else { throw PostgreSQLRoutineDebugError.routineNotFound(name) } let query = PostgreSQLRoutineDebugProtocol.routineQuery(oid: oid, capabilities: catalogCapabilities) - let result = try await execute(query: query) + let result = try await catalogRead(query) guard let row = result.rows.first else { throw PostgreSQLRoutineDebugError.routineNotFound(name) } let routine = PostgreSQLDebugRoutine(row: row.map(\.asText), identity: String(oid)) guard routine.exists else { throw PostgreSQLRoutineDebugError.routineNotFound(name) } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Routines.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Routines.swift index 75727409cc..929d9c36d9 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Routines.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Routines.swift @@ -14,7 +14,7 @@ extension PostgreSQLPluginDriver { schema: resolvedSchema, capabilities: catalogCapabilities ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { row -> PluginRoutineInfo? in guard let name = row[safe: 1]?.asText else { return nil } let kind: PluginRoutineKind = row[safe: 6]?.asText == "p" ? .procedure : .function @@ -43,7 +43,7 @@ extension PostgreSQLPluginDriver { arguments: routine.argumentSignature ) } - let result = try await execute(query: query) + let result = try await catalogRead(query) guard let ddl = result.rows.first?[safe: 0]?.asText, !ddl.isEmpty else { throw PluginObjectSourceError.notFound(routine.name) } @@ -55,7 +55,7 @@ extension PostgreSQLPluginDriver { func fetchAllTriggers(schema: String?) async throws -> [PluginTriggerInfo] { let resolvedSchema = schema ?? currentSchema ?? "public" let query = PostgreSQLObjectQueries.triggerList(schema: resolvedSchema, table: nil) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { Self.trigger(from: $0, fallbackSchema: resolvedSchema) } } @@ -63,7 +63,7 @@ extension PostgreSQLPluginDriver { if let definition = trigger.definition, !definition.isEmpty { return definition } let resolvedSchema = trigger.schema ?? currentSchema ?? "public" let query = PostgreSQLObjectQueries.triggerList(schema: resolvedSchema, table: trigger.table) - let result = try await execute(query: query) + let result = try await catalogRead(query) let match = result.rows .compactMap { Self.trigger(from: $0, fallbackSchema: resolvedSchema) } .first { $0.name == trigger.name } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Sequences.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Sequences.swift index 0b85fe0a06..7002f87995 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Sequences.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Sequences.swift @@ -33,7 +33,7 @@ extension PostgreSQLPluginDriver { private func position(schema: String, sequence: String) async -> PostgreSQLSequencePosition? { do { let query = PostgreSQLSequencePosition.query(schema: schema, sequence: sequence) - return PostgreSQLSequencePosition(row: try await execute(query: query).rows.first) + return PostgreSQLSequencePosition(row: try await catalogRead(query).rows.first) } catch { Self.sequenceLogger.debug( "Sequence position unavailable for \(sequence, privacy: .public): \(error.localizedDescription)" @@ -76,10 +76,8 @@ extension PostgreSQLPluginDriver { named sequence: String? = nil ) async throws -> [PostgreSQLSequenceDefinition] { let source = PostgreSQLSequenceQueries.source(hasSequencesCatalog: includesSequencesCatalog()) - let listing = try await execute( - query: PostgreSQLSequenceQueries.sequenceList( - schema: schema, dependentOnTable: table, source: source, named: sequence - ) + let listing = try await catalogRead( + PostgreSQLSequenceQueries.sequenceList(schema: schema, dependentOnTable: table, source: source, named: sequence) ) var definitions: [PostgreSQLSequenceDefinition] = [] for definition in PostgreSQLSequenceQueries.definitions(from: listing.rows) { @@ -95,7 +93,7 @@ extension PostgreSQLPluginDriver { private func lastValue(schema: String, sequence: String) async -> String? { do { let query = PostgreSQLSequenceQueries.lastValue(schema: schema, sequence: sequence) - return try await execute(query: query).rows.first?[safe: 0]?.asText + return try await catalogRead(query).rows.first?[safe: 0]?.asText } catch { Self.sequenceLogger.debug( "Sequence last value unavailable for \(sequence, privacy: .public): \(error.localizedDescription)" diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+ServerSupport.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+ServerSupport.swift index 3910755d61..b8f8db1e45 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+ServerSupport.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+ServerSupport.swift @@ -29,7 +29,7 @@ internal extension PostgreSQLPluginDriver { func probeSessionFacts() async { do { - let result = try await core.execute(query: PostgreSQLSessionFacts.probeQuery) + let result = try await core.executeCatalogRead(PostgreSQLSessionFacts.probeQuery) let row = result.rows.first?.map(\.asText) ?? [] let facts = PostgreSQLSessionFacts(probeRow: row) sessionFacts.withLock { $0 = facts } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+TriggerEditing.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+TriggerEditing.swift index 662466838d..dab2670b25 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+TriggerEditing.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+TriggerEditing.swift @@ -28,7 +28,7 @@ internal extension PostgreSQLPluginDriver { func fetchTriggerDefinition(name: String, table: String, schema: String?) async throws -> String? { let resolvedSchema = schema ?? core.currentSchema let query = """ - SELECT pg_get_functiondef(t.tgfoid), pg_get_triggerdef(t.oid) + SELECT pg_catalog.pg_get_functiondef(t.tgfoid), pg_catalog.pg_get_triggerdef(t.oid) FROM pg_catalog.pg_trigger t JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace @@ -38,7 +38,7 @@ internal extension PostgreSQLPluginDriver { AND NOT t.tgisinternal LIMIT 1 """ - let result = try await execute(query: query) + let result = try await catalogRead(query) guard let row = result.rows.first, row.count >= 2, let functionDef = row[0].asText, let triggerDef = row[1].asText else { return nil } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Types.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Types.swift index 96bbc8f6fa..30bcd2c9cc 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Types.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Types.swift @@ -15,7 +15,7 @@ extension PostgreSQLPluginDriver { identity: nil, capabilities: catalogCapabilities ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows .compactMap(PostgreSQLTypeDefinition.record(from:)) .map(PostgreSQLTypeDefinition.info(from:)) @@ -24,7 +24,7 @@ extension PostgreSQLPluginDriver { func fetchUserDefinedTypes(inSchemas schemas: [String]) async throws -> [PluginUserDefinedTypeInfo]? { guard !schemas.isEmpty else { return [] } let query = PostgreSQLObjectQueries.userDefinedTypeList(schemas: schemas, capabilities: catalogCapabilities) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows .compactMap(PostgreSQLTypeDefinition.record(from:)) .map(PostgreSQLTypeDefinition.info(from:)) @@ -42,7 +42,7 @@ extension PostgreSQLPluginDriver { identity: String(oid), capabilities: catalogCapabilities ) - let result = try await execute(query: query) + let result = try await catalogRead(query) let match = result.rows .compactMap(PostgreSQLTypeDefinition.record(from:)) .first { $0.identity == String(oid) } @@ -55,7 +55,7 @@ extension PostgreSQLPluginDriver { identity: nil, capabilities: catalogCapabilities ) - let result = try await execute(query: query) + let result = try await catalogRead(query) let match = result.rows .compactMap(PostgreSQLTypeDefinition.record(from:)) .first { $0.name == type.name } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Views.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Views.swift index f525e00af1..9d29295ac9 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Views.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Views.swift @@ -13,7 +13,7 @@ extension PostgreSQLPluginDriver { func fetchViewDefinition(view: String, schema: String?) async throws -> String { let resolvedSchema = schema ?? core.currentSchema let query = PostgreSQLViewDefinition.catalogQuery(name: view, schema: resolvedSchema) - let result = try await executeQualifiedRead(query) + let result = try await catalogRead(query) guard let row = result.rows.first, let catalogRow = PostgreSQLViewDefinition.parse(row: row.map(\.asText)) else { @@ -47,7 +47,7 @@ extension PostgreSQLPluginDriver { name: materializedView, schema: schema ?? core.currentSchema ) - let result = try await execute(query: query) + let result = try await catalogRead(query) guard let row = result.rows.first, row.count >= 2 else { throw LibPQPluginError( message: "Materialized view '\(materializedView)' was not found", diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift index 32f401b440..16732a7857 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift @@ -81,7 +81,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func probeCatalogPresence() async { do { - let result = try await core.execute(query: PostgreSQLCatalogPresence.probeQuery) + let result = try await core.executeCatalogRead(PostgreSQLCatalogPresence.probeQuery) let relationNames = result.rows.compactMap { $0.first?.asText } catalogPresence = PostgreSQLCatalogPresence(relationNames: relationNames) } catch { @@ -91,7 +91,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func probePostgisOids() async { do { - let result = try await core.execute(query: PostGISSpatialRewrite.probeQuery) + let result = try await core.executeCatalogRead(PostGISSpatialRewrite.probeQuery) var map: [UInt32: PostGISType] = [:] for row in result.rows { guard row.count >= 3, @@ -109,7 +109,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func probeVectorTypes() async { do { - let result = try await core.execute(query: PgvectorTypes.probeQuery) + let result = try await core.executeCatalogRead(PgvectorTypes.probeQuery) let rows = result.rows.map { row in row.map(\.asText) } core.mergeCatalogTypeNames(PgvectorTypes.names(fromProbeRows: rows)) } catch { @@ -207,7 +207,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { } func fetchTemporaryTables() async throws -> [PluginTableInfo] { - let result = try await execute(query: PostgreSQLTableListing.temporaryTables) + let result = try await catalogRead(PostgreSQLTableListing.temporaryTables) return result.rows.compactMap { PostgreSQLTableListing.table(fromRow: $0.map(\.asText)) } } @@ -227,14 +227,14 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { var answered = PostgreSQLTableListingLadder.leastCapableAttempt for attempt in PostgreSQLTableListingLadder.degradableAttempts where result == nil { do { - result = try await execute(query: query(attempt)) + result = try await catalogRead(query(attempt)) answered = attempt } catch let error as LibPQPluginError where PostgreSQLTableListingLadder.isDegradable(sqlState: error.sqlState) { reportListingDegrade(past: attempt, error: error) } } if result == nil { - result = try await execute(query: query(PostgreSQLTableListingLadder.leastCapableAttempt)) + result = try await catalogRead(query(PostgreSQLTableListingLadder.leastCapableAttempt)) } guard let result else { return [] } @@ -309,11 +309,10 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func partitionRows(table: String, schema: String?) async throws -> [PostgreSQLPartitionRow] { guard versionedCapabilities.hasDeclarativePartitioning else { return [] } - let result = try await execute( - query: PostgreSQLSchemaQueries.fetchPartitions( - schema: schema ?? core.currentSchema, - table: table - ) + let resolvedSchema = schema ?? core.currentSchema + let result = try await catalogRead( + PostgreSQLSchemaQueries.fetchPartitions(schema: resolvedSchema, table: table), + namesRelativeTo: resolvedSchema ) return result.rows.compactMap { row -> PostgreSQLPartitionRow? in guard let name = row[0].asText else { return nil } @@ -336,7 +335,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { let query = PostgreSQLIndexQueries.indexList( schema: resolvedSchema, table: table, capabilities: catalogCapabilities ) - let result = try await execute(query: query) + let result = try await catalogRead(query, namesRelativeTo: resolvedSchema) let ddl = try await fetchIndexSpellings(schema: resolvedSchema, table: table) return result.rows.compactMap { PostgreSQLIndexRow.index(from: $0, ddl: ddl)?.index } } @@ -346,7 +345,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { table: String? ) async throws -> [String: [String: PostgreSQLCatalogIndexDDL]] { let query = PostgreSQLIndexQueries.indexDDLQuery(schema: schema, table: table) - let result = try await executeQualifiedRead(query) + let result = try await catalogRead(query) return PostgreSQLIndexQueries.indexDDL(rows: result.rows) } @@ -355,7 +354,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { let query = PostgreSQLForeignKeyQueries.foreignKeyList( schema: resolvedSchema, table: table, capabilities: catalogCapabilities ) - let result = try await execute(query: query) + let result = try await catalogRead(query) let foreignKeys = result.rows.compactMap { PostgreSQLForeignKeyRow($0)?.foreignKey } Self.logger.info("[fk] postgres fetchForeignKeys schema=\(resolvedSchema, privacy: .public) table=\(table, privacy: .public) rows=\(result.rows.count) parsed=\(foreignKeys.count)") return foreignKeys @@ -366,7 +365,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { func fetchTriggers(table: String, schema: String?) async throws -> [PluginTriggerInfo] { let resolvedSchema = schema ?? core.currentSchema let query = PostgreSQLObjectQueries.triggerList(schema: resolvedSchema, table: table) - let result = try await execute(query: query) + let result = try await catalogRead(query) let triggers = result.rows.compactMap { Self.trigger(from: $0, fallbackSchema: resolvedSchema) } @@ -393,7 +392,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { let query = PostgreSQLForeignKeyQueries.foreignKeyList( schema: schema ?? core.currentSchema, table: nil, capabilities: catalogCapabilities ) - let result = try await execute(query: query) + let result = try await catalogRead(query) var grouped: [String: [PluginForeignKeyInfo]] = [:] for row in result.rows { guard let decoded = PostgreSQLForeignKeyRow(row) else { continue } @@ -406,7 +405,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { let query = PostgreSQLSchemaQueries.approximateRowCount( schema: schema ?? core.currentSchema, table: table ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return PostgreSQLSchemaQueries.rowEstimate(reltuples: result.rows.first?[safe: 0]?.asText).map { Int($0) } } @@ -421,8 +420,8 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { schema: resolvedSchema, table: table ) - async let columnsResult = execute(query: columnsQuery) - async let constraintsResult = execute(query: constraintsQuery) + async let columnsResult = catalogRead(columnsQuery) + async let constraintsResult = catalogRead(constraintsQuery) let (cols, cons) = try await (columnsResult, constraintsResult) @@ -464,7 +463,9 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { func fetchStandaloneIndexes(table: String, schema: String) async throws -> PostgreSQLStandaloneIndexes { let query = PostgreSQLIndexQueries.standaloneIndexQuery(schema: schema, table: table) - let indexes = PostgreSQLIndexQueries.standaloneIndexes(rows: try await execute(query: query).rows) + let indexes = PostgreSQLIndexQueries.standaloneIndexes( + rows: try await catalogRead(query).rows + ) if !indexes.invalidNames.isEmpty { Self.logger.info( "Left out \(indexes.invalidNames.count) invalid index(es) on \(table, privacy: .private(mask: .hash))" @@ -474,20 +475,8 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { } func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata { - let schemaLiteral = PostgreSQLObjectQueries.quoteLiteral(schema ?? core.currentSchema) - let query = """ - SELECT - pg_total_relation_size(c.oid) AS total_size, - pg_table_size(c.oid) AS data_size, - pg_indexes_size(c.oid) AS index_size, - c.reltuples::bigint AS row_count, - obj_description(c.oid, 'pg_class') AS comment - FROM pg_class c - JOIN pg_namespace n ON n.oid = c.relnamespace - WHERE c.relname = \(PostgreSQLObjectQueries.quoteLiteral(table)) - AND n.nspname = \(schemaLiteral) - """ - let result = try await execute(query: query) + let query = PostgreSQLSchemaQueries.tableMetadata(schema: schema ?? core.currentSchema, table: table) + let result = try await catalogRead(query) guard let row = result.rows.first else { return PluginTableMetadata(tableName: table) } @@ -510,17 +499,17 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { } func fetchDatabases() async throws -> [String] { - let result = try await execute(query: "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname") + let result = try await catalogRead(PostgreSQLSchemaQueries.databases) return result.rows.compactMap { row in row.first?.asText } } func fetchSchemas() async throws -> [String] { - let result = try await execute(query: PostgreSQLSchemaQueries.listSchemas) + let result = try await catalogRead(PostgreSQLSchemaQueries.listSchemas) return result.rows.compactMap { row in row.first?.asText } } func fetchDatabaseMetadata(_ database: String) async throws -> PluginDatabaseMetadata { - let result = try await execute(query: PostgreSQLSchemaQueries.databaseMetadata(database: database)) + let result = try await catalogRead(PostgreSQLSchemaQueries.databaseMetadata(database: database)) let row = result.rows.first return PluginDatabaseMetadata( name: database, @@ -531,7 +520,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { } func fetchAllDatabaseMetadata() async throws -> [PluginDatabaseMetadata] { - let result = try await execute(query: PostgreSQLSchemaQueries.allDatabaseMetadata) + let result = try await catalogRead(PostgreSQLSchemaQueries.allDatabaseMetadata) return result.rows.compactMap { row -> PluginDatabaseMetadata? in guard let dbName = row[safe: 0]?.asText else { return nil } return PluginDatabaseMetadata( @@ -542,31 +531,9 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { } } - /// The kinds `fetchTableDDL` writes a `CREATE TABLE` for. A view's DDL is its own statement, so - /// the enum types and sequences its columns happen to use are not a preamble to it: written in - /// front of a `CREATE VIEW` they recreated objects the view only reads. - private static let relkindsCreatedByTableDDL = "('r', 'p', 'f')" - func fetchDependentTypes(table: String, schema: String?) async throws -> [(name: String, labels: [String])] { - let tableLiteral = PostgreSQLObjectQueries.quoteLiteral(table) - let schemaLiteral = PostgreSQLObjectQueries.quoteLiteral(schema ?? core.currentSchema) - let query = """ - SELECT DISTINCT t.typname, - array_agg(e.enumlabel ORDER BY e.enumsortorder)::text - FROM pg_attribute a - JOIN pg_class c ON c.oid = a.attrelid - JOIN pg_namespace n ON n.oid = c.relnamespace - JOIN pg_type t ON t.oid = a.atttypid - JOIN pg_enum e ON e.enumtypid = t.oid - WHERE c.relname = \(tableLiteral) - AND n.nspname = \(schemaLiteral) - AND c.relkind IN \(Self.relkindsCreatedByTableDDL) - AND a.attnum > 0 - AND NOT a.attisdropped - GROUP BY t.typname - ORDER BY t.typname - """ - let result = try await execute(query: query) + let query = PostgreSQLSchemaQueries.dependentEnumTypes(schema: schema ?? core.currentSchema, table: table) + let result = try await catalogRead(query) return result.rows.compactMap { row -> (name: String, labels: [String])? in guard let typeName = row[0].asText, let labelsStr = row[1].asText else { return nil } return (name: typeName, labels: PostgreSQLTextArray.values(labelsStr)) @@ -771,8 +738,8 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { selectColumns = "datcollate, datctype, NULL, NULL" } do { - let result = try await execute( - query: "SELECT \(selectColumns) FROM pg_database WHERE datname = 'template1'" + let result = try await catalogRead( + "SELECT \(selectColumns) FROM pg_catalog.pg_database WHERE datname = 'template1'" ) guard let row = result.rows.first, row.count >= 4, @@ -796,7 +763,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func fetchCollations() async -> (libc: [String], icu: [String]) { do { - let result = try await execute(query: PostgreSQLSchemaQueries.collationList(capabilities: catalogCapabilities)) + let result = try await catalogRead(PostgreSQLSchemaQueries.collationList(capabilities: catalogCapabilities)) var libc: [String] = [] var icu: [String] = [] for row in result.rows { diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPrincipalQueries.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPrincipalQueries.swift index cd4023fe2d..e581f8f3f3 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPrincipalQueries.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPrincipalQueries.swift @@ -235,5 +235,5 @@ enum PostgreSQLPrincipalQueries { } static let currentPrincipal = "SELECT current_user" - static let currentDatabase = "SELECT current_database()" + static let currentDatabase = "SELECT pg_catalog.current_database()" } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLRoutineDebugProtocol.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLRoutineDebugProtocol.swift index 8cbc8e5b34..85ae223b8d 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLRoutineDebugProtocol.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLRoutineDebugProtocol.swift @@ -60,7 +60,7 @@ struct PostgreSQLDebugRoutine: Equatable, Sendable { // f(bit) being debugged (both measured). let argumentCastTypes: [String] // Each argument's default expression from pg_get_function_arg_default, nil where it has none. - let defaultExpressions: [String?] + private(set) var defaultExpressions: [String?] // The columns of `PostgreSQLRoutineDebugProtocol.routineQuery`, in order. init(row: [String?], identity: String) { @@ -92,6 +92,12 @@ struct PostgreSQLDebugRoutine: Equatable, Sendable { defaultExpressions = PostgreSQLTextArray.elements(text(20)) } + func withDefaultExpressions(_ expressions: [String?]) -> PostgreSQLDebugRoutine { + var routine = self + routine.defaultExpressions = expressions + return routine + } + var isProcedure: Bool { kind == "p" } var displayName: String { "\(name)(\(identityArguments))" } @@ -314,7 +320,8 @@ enum PostgreSQLRoutineDebugProtocol { AND NOT \(pseudoTypeArgumentTest) AND NOT EXISTS ( SELECT 1 FROM pg_catalog.pg_depend d - WHERE d.classid = 'pg_catalog.pg_proc'::pg_catalog.regclass AND d.objid = p.oid AND d.deptype = 'e' + WHERE d.classid = 'pg_catalog.pg_proc'::pg_catalog.regclass::pg_catalog.oid + AND d.objid = p.oid AND d.deptype = 'e' ) ORDER BY p.proname, 4 """ @@ -343,12 +350,25 @@ enum PostgreSQLRoutineDebugProtocol { "SELECT pg_catalog.pg_cancel_backend(\(process))" } + // The call replays each default as text on the target's own path, so it is read there: the deparser qualifies a name + // that path would resolve elsewhere. No operator, and every function names pg_catalog, so any path parses it alike. + static func targetDefaultsStatement(oid: UInt32, argumentCount: Int) -> String? { + guard argumentCount > 0 else { return nil } + return """ + SELECT ARRAY( + SELECT pg_catalog.pg_get_function_arg_default(\(oidLiteral(oid)), s.n) + FROM pg_catalog.generate_series(1, \(argumentCount)) AS s(n) + ORDER BY s.n + ) + """ + } + static func backendCountStatement(process: Int) -> String { - "SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_activity WHERE pid = \(process)" + "SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_activity WHERE pid OPERATOR(pg_catalog.=) \(process)" } static func routineSourceStatement(oid: UInt32) -> String { - "SELECT p.prosrc FROM pg_catalog.pg_proc p WHERE p.oid = \(oidLiteral(oid))" + "SELECT p.prosrc FROM pg_catalog.pg_proc p WHERE p.oid OPERATOR(pg_catalog.=) \(oidLiteral(oid))" } // MARK: - pldbgapi calls diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaManagementQueries.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaManagementQueries.swift index ed0db78a91..ea17f6307b 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaManagementQueries.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaManagementQueries.swift @@ -22,11 +22,12 @@ enum PostgreSQLSchemaManagementQueries { } } + // CURRENT_USER is a keyword: `pg_catalog.current_user` reads as a column of a table named pg_catalog and fails. static func ownerAndComment(schema: String) -> String { """ SELECT pg_catalog.pg_get_userbyid(n.nspowner), pg_catalog.obj_description(n.oid, 'pg_namespace'), - pg_catalog.current_user + CURRENT_USER FROM pg_catalog.pg_namespace n WHERE n.nspname = \(PostgreSQLObjectQueries.quoteLiteral(schema)) """ diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaQueries.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaQueries.swift index b38f26b455..b4d13e0707 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaQueries.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaQueries.swift @@ -19,12 +19,12 @@ enum PostgreSQLSchemaProbe: Equatable { enum PostgreSQLSchemaQueries { /// Returns the first schema on the effective search path, or SQL NULL /// when the path is empty (neither `$user` nor `public` exists). - static let currentSchema = "SELECT current_schema()" + static let currentSchema = "SELECT pg_catalog.current_schema()" /// Like `current_schema()`, but resolves via `current_schemas(false)`, /// which omits search path entries that do not correspond to existing, /// searchable schemas. - static let firstSearchPathSchema = "SELECT (current_schemas(false))[1]" + static let firstSearchPathSchema = "SELECT (pg_catalog.current_schemas(false))[1]" /// Queries tried in order when `current_schema()` resolves to NULL, so a /// database without a `public` schema still gets a usable default schema @@ -56,10 +56,10 @@ enum PostgreSQLSchemaQueries { /// Redshift variant: queries `pg_namespace` directly and additionally /// requires the connected role to hold `USAGE` on the schema. static let listSchemasRedshift = """ - SELECT nspname FROM pg_namespace + SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname NOT LIKE 'pg!_%' ESCAPE '!' AND nspname NOT IN ('information_schema', 'catalog_history') - AND has_schema_privilege(current_user, nspname, 'USAGE') + AND pg_catalog.has_schema_privilege(current_user, nspname, 'USAGE') ORDER BY nspname """ @@ -143,19 +143,66 @@ enum PostgreSQLSchemaQueries { """ } - static func setSearchPath(toSchema schema: String) -> String { - "SET search_path TO \(quotedSchemaIdentifier(schema))" + static func tableMetadata(schema: String, table: String) -> String { + """ + SELECT + pg_catalog.pg_total_relation_size(c.oid) AS total_size, + pg_catalog.pg_table_size(c.oid) AS data_size, + pg_catalog.pg_indexes_size(c.oid) AS index_size, + c.reltuples::bigint AS row_count, + pg_catalog.obj_description(c.oid, 'pg_class') AS comment + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + WHERE c.relname = \(PostgreSQLObjectQueries.quoteLiteral(table)) + AND n.nspname = \(PostgreSQLObjectQueries.quoteLiteral(schema)) + """ } - /// Narrows `search_path` to `pg_catalog` and one schema for the statement that follows, so every - /// name the server deparses is written the way that schema's own `CREATE TABLE` writes it: its - /// own types bare, every other schema's qualified. That is what `psql \d` shows, and it does not - /// depend on what the session's path happens to be. - /// - /// Run through `LibPQPluginConnection.executeTransactionScopedRead`, which is what confines the - /// setting to the read. - static func schemaRelativeReadPrefix(schema: String) -> String { - "SET LOCAL search_path = pg_catalog, \(quotedSchemaIdentifier(schema)); " + /// `tableMetadata(schema:table:)` for every table of one schema, the table's name first. + static func allTableMetadata(schema: String) -> String { + """ + SELECT + c.relname AS table_name, + pg_catalog.pg_total_relation_size(c.oid) AS total_size, + pg_catalog.pg_table_size(c.oid) AS data_size, + pg_catalog.pg_indexes_size(c.oid) AS index_size, + c.reltuples::bigint AS row_count, + pg_catalog.obj_description(c.oid, 'pg_class') AS comment + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + WHERE n.nspname = \(PostgreSQLObjectQueries.quoteLiteral(schema)) AND c.relkind IN ('r', 'p', 'm', 'f') + ORDER BY c.relname + """ + } + + static let databases = "SELECT datname FROM pg_catalog.pg_database WHERE NOT datistemplate ORDER BY datname" + + /// The kinds `fetchTableDDL` writes a `CREATE TABLE` for. A view's DDL is its own statement, so + /// the enum types and sequences its columns happen to use are not a preamble to it: written in + /// front of a `CREATE VIEW` they recreated objects the view only reads. + private static let relkindsCreatedByTableDDL = "('r', 'p', 'f')" + + static func dependentEnumTypes(schema: String, table: String) -> String { + """ + SELECT DISTINCT t.typname, + pg_catalog.array_agg(e.enumlabel ORDER BY e.enumsortorder)::text + FROM pg_catalog.pg_attribute a + JOIN pg_catalog.pg_class c ON c.oid = a.attrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + JOIN pg_catalog.pg_type t ON t.oid = a.atttypid + JOIN pg_catalog.pg_enum e ON e.enumtypid = t.oid + WHERE c.relname = \(PostgreSQLObjectQueries.quoteLiteral(table)) + AND n.nspname = \(PostgreSQLObjectQueries.quoteLiteral(schema)) + AND c.relkind IN \(relkindsCreatedByTableDDL) + AND a.attnum > 0 + AND NOT a.attisdropped + GROUP BY t.typname + ORDER BY t.typname + """ + } + + static func setSearchPath(toSchema schema: String) -> String { + "SET search_path TO \(quotedSchemaIdentifier(schema))" } private static func quotedSchemaIdentifier(_ schema: String) -> String { @@ -169,6 +216,8 @@ enum PostgreSQLSchemaQueries { return "SELECT collname, collprovider FROM pg_catalog.pg_collation WHERE collprovider IN ('b', 'c', 'i') ORDER BY collname" } + /// The app runs this itself, on the session's own path, which may name another schema before `pg_catalog`: every + /// call, relation and operator names `pg_catalog`. static func allTablesMetadata(schema: String) -> String { """ SELECT @@ -176,12 +225,12 @@ enum PostgreSQLSchemaQueries { relname as name, 'TABLE' as kind, n_live_tup as estimated_rows, - pg_size_pretty(pg_total_relation_size(relid)) as total_size, - pg_size_pretty(pg_relation_size(relid)) as data_size, - pg_size_pretty(pg_indexes_size(relid)) as index_size, - obj_description(relid, 'pg_class') as comment - FROM pg_stat_user_tables - WHERE schemaname = \(PostgreSQLObjectQueries.quoteLiteral(schema)) + pg_catalog.pg_size_pretty(pg_catalog.pg_total_relation_size(relid)) as total_size, + pg_catalog.pg_size_pretty(pg_catalog.pg_relation_size(relid)) as data_size, + pg_catalog.pg_size_pretty(pg_catalog.pg_indexes_size(relid)) as index_size, + pg_catalog.obj_description(relid, 'pg_class') as comment + FROM pg_catalog.pg_stat_user_tables + WHERE schemaname OPERATOR(pg_catalog.=) \(PostgreSQLObjectQueries.quoteLiteral(schema)) ORDER BY relname """ } @@ -249,7 +298,7 @@ enum PostgreSQLSchemaQueries { /// `public.geometry(Point,4326)` as `USER-DEFINED` and `geometry`, and `varchar(50)` as /// `character varying`. Both `format_type` and `pg_get_expr` leave out the schema of anything the /// reading session resolves without one, so the caller runs this with `search_path` narrowed - /// (see `PostgreSQLViewDefinition.qualifiedReadPrefix`). + /// (`PostgreSQLCatalogSearchPath.pinned`). /// /// A query of its own rather than columns of `columnsQuery`, because a narrowed path would /// change what that query reports for display and comparison. The relation kinds are the ones @@ -287,8 +336,49 @@ enum PostgreSQLSchemaQueries { """ } + /// What a person reads for each column in the Structure tab: its default, generation expression and declared type, + /// spelled relative to the table's schema the way `psql \d` shows them. Run under + /// `PostgreSQLCatalogSearchPath.relative(to:)`, so this statement calls `pg_catalog` functions by their qualified + /// name and compares only operands a `pg_catalog` operator takes exactly: the browsed schema is on the path. + /// + /// The default is `information_schema.columns.column_default`'s own expression: none for a generated column. + static func columnSpellingQuery(schema: String, table: String?, capabilities: PostgreSQLCapabilities) -> String { + let tableFilter = table.map { "\n AND c.relname = \(PostgreSQLObjectQueries.quoteLiteral($0))" } ?? "" + let isGenerated = capabilities.hasGeneratedColumns ? "a.attgenerated IN ('s', 'v')" : "false" + return """ + SELECT + c.relname, + a.attname, + CASE WHEN NOT (\(isGenerated)) THEN pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) END, + CASE WHEN \(isGenerated) THEN pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) END, + \(declaredType(attribute: "a")) + FROM pg_catalog.pg_attribute a + JOIN pg_catalog.pg_class c ON c.oid = a.attrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + LEFT JOIN pg_catalog.pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum + WHERE n.nspname = \(PostgreSQLObjectQueries.quoteLiteral(schema))\(tableFilter) + AND c.relkind IN ('r', 'v', 'f', 'p', 'm') + AND a.attnum > 0 + AND NOT a.attisdropped + """ + } + + /// Keyed by relation, then by column, with exact spellings, like `columnDDL(rows:)`. + static func columnSpellings(rows: [[PluginCellValue]]) -> [String: [String: PostgreSQLColumnSpelling]] { + var spellings: [String: [String: PostgreSQLColumnSpelling]] = [:] + for row in rows { + guard let table = row[safe: 0]?.asText, let column = row[safe: 1]?.asText else { continue } + spellings[table, default: [:]][column] = PostgreSQLColumnSpelling( + defaultValue: row[safe: 2]?.asText, + generationExpression: row[safe: 3]?.asText?.nilIfEmpty, + declaredType: row[safe: 4]?.asText + ) + } + return spellings + } + /// The column's type as the table's own schema declares it, read under - /// `schemaRelativeReadPrefix`. `format_type` is the only spelling that keeps the modifier, and + /// `PostgreSQLCatalogSearchPath.relative(to:)`. `format_type` is the only spelling that keeps the modifier, and /// under that path it writes the table's own types bare and every other schema's qualified. /// /// A type an extension owns is qualified even in its own schema. PostGIS and citext install into @@ -310,9 +400,9 @@ enum PostgreSQLSchemaQueries { AND EXISTS ( SELECT 1 FROM pg_catalog.pg_depend dd - WHERE dd.classid = 'pg_catalog.pg_type'::pg_catalog.regclass + WHERE dd.classid = 'pg_catalog.pg_type'::pg_catalog.regclass::pg_catalog.oid AND dd.deptype = 'e' - AND dd.objid = CASE WHEN dt.typlen = -1 AND dt.typelem <> 0 + AND dd.objid = CASE WHEN dt.typlen = -1 AND dt.typelem <> 0::pg_catalog.oid THEN dt.typelem ELSE dt.oid END)) THEN (SELECT pg_catalog.quote_ident(dtn.nspname) || '.' FROM pg_catalog.pg_type dt @@ -340,7 +430,7 @@ enum PostgreSQLSchemaQueries { private static func declaredCollation(_ value: String, otherwise fallback: String? = nil) -> String { """ - CASE WHEN a.attcollation <> 0 + CASE WHEN a.attcollation <> 0::pg_catalog.oid AND a.attcollation <> (SELECT ty.typcollation FROM pg_catalog.pg_type ty WHERE ty.oid = a.atttypid) THEN \(value)\(fallback.map { "\n ELSE \($0)" } ?? "") END @@ -394,10 +484,10 @@ enum PostgreSQLSchemaQueries { return """ SELECT con.conname, - pg_get_constraintdef(con.oid), + pg_catalog.pg_get_constraintdef(con.oid), con.convalidated, COALESCE(( - SELECT array_agg(att.attname ORDER BY att.attnum)::text + SELECT pg_catalog.array_agg(att.attname ORDER BY att.attnum)::text FROM pg_catalog.pg_attribute att WHERE att.attrelid = con.conrelid AND att.attnum = ANY (con.conkey) ), \'{}\') @@ -416,10 +506,10 @@ enum PostgreSQLSchemaQueries { let tableLiteral = PostgreSQLObjectQueries.quoteLiteral(table) return """ SELECT - pg_get_constraintdef(con.oid, true) - FROM pg_constraint con - JOIN pg_class c ON c.oid = con.conrelid - JOIN pg_namespace n ON n.oid = c.relnamespace + pg_catalog.pg_get_constraintdef(con.oid, true) + FROM pg_catalog.pg_constraint con + JOIN pg_catalog.pg_class c ON c.oid = con.conrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND con.contype IN ('p', 'u', 'c', 'x') @@ -460,8 +550,8 @@ enum PostgreSQLSchemaQueries { """ : "" let generationClause = capabilities.hasGeneratedColumns ? """ CASE - WHEN a.attgenerated = 's' THEN ' GENERATED ALWAYS AS (' || pg_get_expr(d.adbin, d.adrelid) || ') STORED' - WHEN a.attgenerated = 'v' THEN ' GENERATED ALWAYS AS (' || pg_get_expr(d.adbin, d.adrelid) || ') VIRTUAL' + WHEN a.attgenerated = 's' THEN ' GENERATED ALWAYS AS (' || pg_catalog.pg_get_expr(d.adbin, d.adrelid) || ') STORED' + WHEN a.attgenerated = 'v' THEN ' GENERATED ALWAYS AS (' || pg_catalog.pg_get_expr(d.adbin, d.adrelid) || ') VIRTUAL' ELSE '' END || """ : "" @@ -471,13 +561,13 @@ enum PostgreSQLSchemaQueries { capabilities.hasGeneratedColumns ? "AND a.attgenerated NOT IN ('s', 'v')" : "" ].filter { !$0.isEmpty }.joined(separator: " ") return """ - quote_ident(a.attname) || ' ' || format_type(a.atttypid, a.atttypmod) || \(columnCollateClause) || + pg_catalog.quote_ident(a.attname) || ' ' || pg_catalog.format_type(a.atttypid, a.atttypmod) || \(columnCollateClause) || \(identityClause) \(generationClause) CASE WHEN a.attnotnull THEN ' NOT NULL' ELSE '' END || CASE WHEN a.atthasdef \(defaultGuard) - THEN ' DEFAULT ' || pg_get_expr(d.adbin, d.adrelid) + THEN ' DEFAULT ' || pg_catalog.pg_get_expr(d.adbin, d.adrelid) ELSE '' END """ @@ -485,10 +575,10 @@ enum PostgreSQLSchemaQueries { private static func relationColumns(schema: String, table: String) -> String { """ - FROM pg_attribute a - JOIN pg_class c ON c.oid = a.attrelid - JOIN pg_namespace n ON n.oid = c.relnamespace - LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = a.attnum + FROM pg_catalog.pg_attribute a + JOIN pg_catalog.pg_class c ON c.oid = a.attrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid = c.oid AND d.adnum = a.attnum WHERE c.relname = \(PostgreSQLObjectQueries.quoteLiteral(table)) AND n.nspname = \(PostgreSQLObjectQueries.quoteLiteral(schema)) AND a.attnum > 0 @@ -497,7 +587,9 @@ enum PostgreSQLSchemaQueries { """ } - /// Column introspection for one schema, read under `schemaRelativeReadPrefix(schema:)`. + /// Column introspection for one schema, read under `PostgreSQLCatalogSearchPath.pinned`. The default, the + /// generation expression and the declared type depend on the path the server deparses them under, so they are + /// NULL here and come from `columnSpellingQuery`. /// /// `declared_type` is what the column shows and `data_type` is what the app classifies by, which /// are two different spellings: `information_schema` reports `character varying` for a @@ -532,15 +624,6 @@ enum PostgreSQLSchemaQueries { let identityProjection = capabilities.hasIdentityColumns ? "a.attidentity" : "NULL::text" let generatedProjection = capabilities.hasGeneratedColumns ? "a.attgenerated" : "NULL::text" // Not `c.generation_expression`: KingbaseES's Oracle-mode view compares `attgenerated <> NULL`, so it is always NULL. - let generationExpressionProjection = capabilities.hasGeneratedColumns - ? """ - CASE WHEN a.attgenerated IN ('s', 'v') - THEN (SELECT pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) - FROM pg_catalog.pg_attrdef ad - WHERE ad.adrelid = a.attrelid AND ad.adnum = a.attnum) - END - """ - : "NULL::text" let attributeJoin = """ LEFT JOIN pg_catalog.pg_attribute a @@ -552,7 +635,7 @@ enum PostgreSQLSchemaQueries { \(includesTableName ? "c.table_name AS table_name,\n " : "")c.column_name AS column_name, c.data_type AS data_type, c.is_nullable AS is_nullable, - c.column_default AS column_default, + NULL::text AS column_default, c.collation_name AS collation_name, pg_catalog.col_description(rel.oid, c.ordinal_position) AS column_comment, c.udt_name AS udt_name, @@ -560,8 +643,8 @@ enum PostgreSQLSchemaQueries { \(identityProjection) AS identity_kind, \(generatedProjection) AS generated_kind, c.udt_schema AS udt_schema, - \(generationExpressionProjection) AS generation_expression, - \(declaredType(attribute: "a")) AS declared_type, + NULL::text AS generation_expression, + NULL::text AS declared_type, c.domain_name AS domain_name, c.ordinal_position AS ordinal_position FROM information_schema.columns c @@ -651,7 +734,7 @@ enum PostgreSQLSchemaQueries { \(generatedProjection) AS generated_kind, COALESCE(mvbtn.nspname, mvtn.nspname) AS udt_schema, NULL::text AS generation_expression, - \(declaredType(attribute: "mva")) AS declared_type, + NULL::text AS declared_type, CASE WHEN mvt.typtype = 'd' THEN mvt.typname END AS domain_name, \(source.ordinalPosition) AS ordinal_position \(source.relation(schemaLiteral: schemaLiteral, table: table)) @@ -659,6 +742,12 @@ enum PostgreSQLSchemaQueries { } } +struct PostgreSQLColumnSpelling: Equatable { + let defaultValue: String? + let generationExpression: String? + let declaredType: String? +} + /// One column's clauses as the server spells them, read under a narrowed `search_path`. struct PostgreSQLCatalogColumnDDL: Equatable { let typeSpelling: String diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSessionScopeSQL.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSessionScopeSQL.swift index 65d30ab1f2..bdec181ea8 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSessionScopeSQL.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSessionScopeSQL.swift @@ -54,8 +54,15 @@ internal enum PostgreSQLSessionScopeSQL { } /// `pg_settings` spells `DateStyle`, `IntervalStyle` and `TimeZone` in mixed case, so the match ignores case. + /// + /// Sent on the user's own session, whose path may name another schema before `pg_catalog`, so the comparison + /// names `pg_catalog`'s operator. static func captureStatement(names: [String]) -> PostgreSQLBoundStatement { - let filter = PostgreSQLObjectQueries.membership("pg_catalog.lower(name)", in: names.map { $0.lowercased() }) + let filter = names.isEmpty + ? "false" + : "pg_catalog.lower(name) OPERATOR(pg_catalog.=) ANY (ARRAY[" + + names.map { PostgreSQLObjectQueries.quoteLiteral($0.lowercased()) }.joined(separator: ", ") + + "]::pg_catalog.text[])" return PostgreSQLBoundStatement( sql: "SELECT name, setting FROM pg_catalog.pg_settings WHERE \(filter)", parameters: [] diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLTableRebuild.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLTableRebuild.swift index e2841e146e..ad6c4710b6 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLTableRebuild.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLTableRebuild.swift @@ -125,9 +125,9 @@ struct PostgreSQLTableRebuild { return identityColumns.map { column in let columnLiteral = PostgreSQLObjectQueries.quoteLiteral(column) return """ - SELECT setval( - pg_get_serial_sequence(\(relationLiteral), \(columnLiteral)), - GREATEST(COALESCE((SELECT MAX(\(quote(column))) FROM \(qualified)), 0), 1), + SELECT pg_catalog.setval( + pg_catalog.pg_get_serial_sequence(\(relationLiteral), \(columnLiteral)), + GREATEST(COALESCE((SELECT pg_catalog.max(\(quote(column))) FROM \(qualified)), 0), 1), true ) """ diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLVersionedStatements.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLVersionedStatements.swift index 5bfb259c27..19ad34ba37 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLVersionedStatements.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLVersionedStatements.swift @@ -42,7 +42,7 @@ internal enum PostgreSQLVersionedStatements { } let nameLiteral = PostgreSQLObjectQueries.quoteLiteral(name) let createLiteral = PostgreSQLObjectQueries.quoteLiteral("CREATE SCHEMA \(identifier)") - let body = "BEGIN IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = \(nameLiteral)) " + let body = "BEGIN IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname OPERATOR(pg_catalog.=) \(nameLiteral)) " + "THEN EXECUTE \(createLiteral); END IF; END" let tag = dollarQuoteTag(avoiding: body) return "DO \(tag) \(body) \(tag)" diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLViewDefinition.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLViewDefinition.swift index f53ce9235e..a6fccf509a 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLViewDefinition.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLViewDefinition.swift @@ -33,7 +33,7 @@ public enum PostgreSQLViewDefinition { } } - /// Run with `search_path` narrowed first (see `qualifiedReadPrefix`). `pg_get_viewdef` writes a + /// Run under `PostgreSQLCatalogSearchPath.pinned`. `pg_get_viewdef` writes a /// table name bare whenever the reading session could resolve it without its schema, and the /// scoped connection this runs on has its path set to the view's own schema, so the body came /// back naming `orders` rather than `sales.orders`. Run anywhere else, that text silently bound @@ -56,16 +56,6 @@ public enum PostgreSQLViewDefinition { """ } - /// `SET LOCAL` lasts only as long as the transaction it runs in. Sent in the same query string - /// as the read, the pair is one implicit transaction, so the path is back to what it was as - /// soon as the read returns. Inside a transaction the caller already holds, it would last until - /// that transaction ends, which is why the driver wraps the read in a savepoint there. - /// - /// `pg_catalog` rather than an empty path: PostgreSQL 9.1 rejects `''` ("schema "" does not - /// exist"), and `pg_catalog` is searched implicitly anyway, so every user relation is still - /// written with its schema. Measured identical output on 9.2, 9.3, 9.6 and 17. - public static let qualifiedReadPrefix = "SET LOCAL search_path = pg_catalog; " - public static func parse(row: [String?]) -> CatalogRow? { guard row.count >= 5, let relkind = row[0], diff --git a/apple/Plugins/PostgreSQLDriverPlugin/RedshiftPluginDriver.swift b/apple/Plugins/PostgreSQLDriverPlugin/RedshiftPluginDriver.swift index 9d00a179ee..bf1a9ee399 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/RedshiftPluginDriver.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/RedshiftPluginDriver.swift @@ -42,7 +42,8 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { self.connectedDatabase = config.database self.core = LibPQDriverCore( config: config, - schemaFallbackQueries: PostgreSQLSchemaQueries.schemaFallbackQueriesRedshift + schemaFallbackQueries: PostgreSQLSchemaQueries.schemaFallbackQueriesRedshift, + pinsCatalogSearchPath: false ) core.onPostConnect = { [weak self] in await self?.probeExternalSchemas() @@ -57,7 +58,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { /// an empty one. A cluster with no external catalog answers in one cheap read. private func probeExternalSchemas() async { do { - let result = try await execute(query: RedshiftExternalSchemaQueries.listExternalSchemaNames) + let result = try await catalogRead(RedshiftExternalSchemaQueries.listExternalSchemaNames) let names = Set(result.rows.compactMap { $0.first?.asText }) externalSchemaLock.withLock { externalSchemaCache = names } } catch { @@ -78,7 +79,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { func fetchTables(schema: String?) async throws -> [PluginTableInfo] { let resolvedSchema = schema ?? core.currentSchema - let result = try await execute(query: RedshiftTableCatalog.listingQuery(schema: resolvedSchema)) + let result = try await catalogRead(RedshiftTableCatalog.listingQuery(schema: resolvedSchema)) let localTables = result.rows.compactMap { RedshiftTableCatalog.table(fromListingRow: $0.map(\.asText)) } guard isExternalSchema(resolvedSchema) else { return localTables } @@ -93,7 +94,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { func fetchTables(inSchemas schemas: [String]) async throws -> [PluginTableInfo]? { guard !schemas.isEmpty else { return [] } - let result = try await execute(query: RedshiftTableCatalog.listingQuery(inSchemas: schemas)) + let result = try await catalogRead(RedshiftTableCatalog.listingQuery(inSchemas: schemas)) var tables = result.rows.compactMap { RedshiftTableCatalog.table(fromListingRow: $0.map(\.asText)) } for schema in schemas where isExternalSchema(schema) { let localNames = Set(tables.lazy.filter { $0.schema == schema }.map(\.name)) @@ -104,8 +105,8 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func fetchExternalTables(schema: String) async -> [PluginTableInfo] { do { - let result = try await execute( - query: RedshiftExternalSchemaQueries.listExternalTables( + let result = try await catalogRead( + RedshiftExternalSchemaQueries.listExternalTables( schema: schema, database: connectedDatabase ) @@ -138,8 +139,8 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func fetchExternalColumns(schema: String, table: String) async -> [PluginColumnInfo] { do { - let result = try await execute( - query: RedshiftExternalSchemaQueries.listExternalColumns( + let result = try await catalogRead( + RedshiftExternalSchemaQueries.listExternalColumns( schema: schema, table: table, database: connectedDatabase @@ -181,7 +182,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func fetchLocalColumns(table: String, schema: String) async throws -> [PluginColumnInfo] { let query = RedshiftSchemaQueries.columnsQuery(schema: schema, table: table) - let result = try await execute(query: query) + let result = try await catalogRead(query) return result.rows.compactMap { row -> PluginColumnInfo? in guard row.count >= 4, let name = row[0].asText, @@ -234,8 +235,8 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func fetchExternalAllColumns(schema: String) async -> [String: [PluginColumnInfo]] { do { - let result = try await execute( - query: RedshiftExternalSchemaQueries.listExternalColumns( + let result = try await catalogRead( + RedshiftExternalSchemaQueries.listExternalColumns( schema: schema, table: nil, database: connectedDatabase @@ -262,7 +263,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { private func fetchLocalAllColumns(schema: String) async throws -> [String: [PluginColumnInfo]] { let query = RedshiftSchemaQueries.columnsQuery(schema: schema, table: nil) - let result = try await execute(query: query) + let result = try await catalogRead(query) var allColumns: [String: [PluginColumnInfo]] = [:] for row in result.rows { guard row.count >= 5, @@ -310,7 +311,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { let query = RedshiftTableCatalog.keysQuery(schema: schema ?? core.currentSchema, table: table) - let result = try await execute(query: query) + let result = try await catalogRead(query) return RedshiftTableCatalog.keys(fromRows: result.rows.map { $0.map(\.asText) }) } @@ -324,7 +325,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { serverVersionNumber: core.serverVersionNumber ) ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return PostgreSQLCatalogForeignKeys.foreignKeys(from: result.rows.map { $0.map(\.asText) }) } @@ -343,7 +344,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { WHERE "table" = \(tableLiteral) AND schema = \(schemaLiteral) """ - let result = try await execute(query: query) + let result = try await catalogRead(query) guard let firstRow = result.rows.first, let value = firstRow[0].asText, let count = Int(value) else { return nil } return count >= 0 ? count : nil } @@ -356,7 +357,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { let quotedSchema = quoteIdentifier(resolvedSchema) do { - let showResult = try await execute(query: "SHOW TABLE \(quotedSchema).\(quotedTable)") + let showResult = try await catalogRead("SHOW TABLE \(quotedSchema).\(quotedTable)") if let firstRow = showResult.rows.first, let ddl = firstRow[0].asText, !ddl.isEmpty { return ddl } @@ -366,27 +367,27 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { /// `pg_attribute` lists a view's columns as it lists a table's, so a view is looked up before /// the reconstruction below, which rebuilt a view as a `CREATE TABLE` of its columns. - let view = try await execute(query: Self.viewDefinitionQuery(view: table, schema: resolvedSchema)) + let view = try await catalogRead(Self.viewDefinitionQuery(view: table, schema: resolvedSchema)) if let ddl = view.rows.first?[safe: 0]?.asText { return ddl } let columnsQuery = """ SELECT - quote_ident(a.attname) || ' ' || format_type(a.atttypid, a.atttypmod) || + pg_catalog.quote_ident(a.attname) || ' ' || pg_catalog.format_type(a.atttypid, a.atttypmod) || CASE WHEN a.attnotnull THEN ' NOT NULL' ELSE '' END || - CASE WHEN a.atthasdef THEN ' DEFAULT ' || pg_get_expr(d.adbin, d.adrelid) ELSE '' END - FROM pg_attribute a - JOIN pg_class c ON c.oid = a.attrelid - JOIN pg_namespace n ON n.oid = c.relnamespace - LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = a.attnum + CASE WHEN a.atthasdef THEN ' DEFAULT ' || pg_catalog.pg_get_expr(d.adbin, d.adrelid) ELSE '' END + FROM pg_catalog.pg_attribute a + JOIN pg_catalog.pg_class c ON c.oid = a.attrelid + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid = c.oid AND d.adnum = a.attnum WHERE c.relname = \(tableLiteral) AND n.nspname = \(schemaLiteral) AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum """ - let columnsResult = try await execute(query: columnsQuery) + let columnsResult = try await catalogRead(columnsQuery) let columnDefs = columnsResult.rows.compactMap { $0[0].asText } guard !columnDefs.isEmpty else { throw LibPQPluginError(message: "Failed to fetch DDL for table '\(table)'", sqlState: nil, detail: nil) @@ -446,7 +447,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { } func fetchViewDefinition(view: String, schema: String?) async throws -> String { - let result = try await execute(query: Self.viewDefinitionQuery(view: view, schema: schema ?? core.currentSchema)) + let result = try await catalogRead(Self.viewDefinitionQuery(view: view, schema: schema ?? core.currentSchema)) guard let firstRow = result.rows.first, let ddl = firstRow[0].asText else { throw LibPQPluginError(message: "Failed to fetch definition for view '\(view)'", sqlState: nil, detail: nil) } @@ -455,8 +456,8 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { private static func viewDefinitionQuery(view: String, schema: String) -> String { """ - SELECT 'CREATE OR REPLACE VIEW ' || quote_ident(schemaname) || '.' || quote_ident(viewname) || ' AS ' || E'\\n' || definition AS ddl - FROM pg_views + SELECT 'CREATE OR REPLACE VIEW ' || pg_catalog.quote_ident(schemaname) || '.' || pg_catalog.quote_ident(viewname) || ' AS ' || E'\\n' || definition AS ddl + FROM pg_catalog.pg_views WHERE viewname = \(PostgreSQLObjectQueries.quoteLiteral(view)) AND schemaname = \(PostgreSQLObjectQueries.quoteLiteral(schema)) """ @@ -481,7 +482,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { WHERE "table" = \(tableLiteral) AND schema = \(schemaLiteral) """ - let result = try await execute(query: query) + let result = try await catalogRead(query) guard let row = result.rows.first else { return PluginTableMetadata(tableName: table, comment: comment) } @@ -523,14 +524,14 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { } func fetchDatabases() async throws -> [String] { - let result = try await execute( - query: "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname" + let result = try await catalogRead( + "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname" ) return result.rows.compactMap { row in row.first?.asText } } func fetchSchemas() async throws -> [String] { - let result = try await execute(query: PostgreSQLSchemaQueries.listSchemasRedshift) + let result = try await catalogRead(PostgreSQLSchemaQueries.listSchemasRedshift) await probeExternalSchemas() return result.rows.compactMap { row in row.first?.asText } } @@ -546,8 +547,8 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { let sizeQuery = """ SELECT SUM(size) FROM svv_table_info WHERE database = current_database() """ - async let countResult = execute(query: countQuery) - async let sizeResult = execute(query: sizeQuery) + async let countResult = catalogRead(countQuery) + async let sizeResult = catalogRead(sizeQuery) let (countRes, sizeRes) = try await (countResult, sizeResult) let tableCount = Int(countRes.rows.first?[0].asText ?? "0") ?? 0 @@ -563,8 +564,8 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { } func fetchAllDatabaseMetadata() async throws -> [PluginDatabaseMetadata] { - let dbResult = try await execute( - query: "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname" + let dbResult = try await catalogRead( + "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname" ) let dbNames = dbResult.rows.compactMap { $0.first?.asText } @@ -574,7 +575,7 @@ final class RedshiftPluginDriver: LibPQBackedDriver, @unchecked Sendable { WHERE schema NOT IN ('pg_internal', 'pg_catalog', 'information_schema') GROUP BY database """ - let infoResult = try await execute(query: infoQuery) + let infoResult = try await catalogRead(infoQuery) var metadataByName: [String: (tableCount: Int, sizeMb: Int64)] = [:] for row in infoResult.rows { guard let dbName = row[0].asText else { continue } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostGISSpatialRewriteTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostGISSpatialRewriteTests.swift index f951db5fb5..502a76c9fa 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostGISSpatialRewriteTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostGISSpatialRewriteTests.swift @@ -13,13 +13,13 @@ struct PostGISConversionQueryTests { @Test("geometry casts each element to the probed geometry type") func geometryQuery() throws { let query = try #require(PostGISSpatialRewrite.conversionQuery(for: geometry)) - #expect(query.contains("\"public\".ST_AsEWKT(($1::text[])[i]::\"public\".\"geometry\")")) + #expect(query.contains("\"public\".ST_AsEWKT(($1::pg_catalog.text[])[i]::\"public\".\"geometry\")")) } @Test("geography casts each element to the probed geography type") func geographyQuery() throws { let query = try #require(PostGISSpatialRewrite.conversionQuery(for: geography)) - #expect(query.contains("\"gis\".ST_AsEWKT(($1::text[])[i]::\"gis\".\"geography\")")) + #expect(query.contains("\"gis\".ST_AsEWKT(($1::pg_catalog.text[])[i]::\"gis\".\"geography\")")) } @Test("Unknown type name returns nil") @@ -32,7 +32,7 @@ struct PostGISConversionQueryTests { @Test("Elements are walked by generate_subscripts, which 9.1 has, in array order") func portableOrdering() throws { let query = try #require(PostGISSpatialRewrite.conversionQuery(for: geometry)) - #expect(query.contains("pg_catalog.generate_subscripts($1::text[], 1) AS i ORDER BY i")) + #expect(query.contains("pg_catalog.generate_subscripts($1::pg_catalog.text[], 1) AS i ORDER BY i")) #expect(!query.contains("WITH ORDINALITY")) #expect(!query.contains("unnest")) } @@ -64,6 +64,24 @@ struct PostGISConversionQueryTests { #expect(PostGISSpatialRewrite.probeQuery.contains("n.nspname")) #expect(PostGISSpatialRewrite.probeQuery.contains("JOIN pg_catalog.pg_namespace n")) } + + // Another role's `CREATE TYPE evil.geometry AS ENUM (...)` beside an `evil.st_asewkt(evil.geometry)` took the + // conversion to that role's function. + @Test("The probe takes only base types the postgis extension owns") + func probeTakesOnlyThePostgisExtensionsBaseTypes() { + let probe = PostGISSpatialRewrite.probeQuery + #expect(probe.contains("t.typtype = 'b'")) + #expect(probe.contains("d.deptype = 'e'")) + #expect(probe.contains("JOIN pg_catalog.pg_extension x ON x.oid = d.refobjid")) + #expect(probe.contains("x.extname = 'postgis'")) + } + + @Test("The conversion casts its parameter to pg_catalog's text array") + func conversionNamesPgCatalogText() throws { + let query = try #require(PostGISSpatialRewrite.conversionQuery(for: geometry)) + #expect(query.contains("$1::pg_catalog.text[]")) + #expect(!query.contains("$1::text[]")) + } } struct PostGISArrayLiteralTests { diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLCatalogQualificationTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLCatalogQualificationTests.swift new file mode 100644 index 0000000000..77c7e0879c --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLCatalogQualificationTests.swift @@ -0,0 +1,172 @@ +import Foundation +import TableProPluginKit +import TableProPostgreSQLCore +import Testing + +// Opening `evil.orders` must not reach a function or operator another role created in `evil`. The catalog entry pins +// the path for that; these statements also call pg_catalog by name, so they stay safe on a path the entry does not set: +// a schema-relative read, Redshift, CockroachDB, and the all-tables listing the app runs itself. +struct PostgreSQLCatalogQualificationTests { + private static let schema = "evil" + private static let table = "orders" + private static let capabilities = PostgreSQLCapabilities(serverVersion: 170_011) + + // Words that take parentheses without naming a function the path resolves. + private static let grammar: Set = [ + "select", "from", "where", "and", "or", "not", "in", "exists", "any", "all", "array", "case", "when", "then", + "else", "coalesce", "nullif", "greatest", "least", "cast", "values", "over", "filter", "as", "on", "join", + "using", "like", "ilike", "escape", "extract", "position", "substring", "trim", "row", "with", "union", "is", + "operator" + ] + + private static let call = try! NSRegularExpression(pattern: #"(? [String] { + let stripped = sql.replacingOccurrences(of: #"'(?:[^']|'')*'"#, with: "''", options: .regularExpression) + let range = NSRange(stripped.startIndex..., in: stripped) + return Self.call.matches(in: stripped, range: range).compactMap { match in + guard let nameRange = Range(match.range(at: 1), in: stripped) else { return nil } + let name = String(stripped[nameRange]) + // A column list after an alias, `AS k(n)`, names no function. + let before = stripped[.. 0` is oid against integer, which a `<>(oid, integer)` in `evil` takes exactly. + @Test("The schema-relative column read compares oids with oids") + func relativeColumnReadComparesExactTypes() { + let sql = PostgreSQLSchemaQueries.columnSpellingQuery(schema: Self.schema, table: Self.table, capabilities: Self.capabilities) + #expect(sql.contains("dt.typelem <> 0::pg_catalog.oid")) + #expect(sql.contains("dd.classid = 'pg_catalog.pg_type'::pg_catalog.regclass::pg_catalog.oid")) + #expect(!sql.contains("information_schema")) + } + + // Measured on 17.11: `SELECT pg_catalog.current_user` fails with "missing FROM-clause entry for table pg_catalog". + @Test("The schema details read names the current role with the keyword") + func schemaDetailsReadsCurrentUserAsAKeyword() { + let sql = PostgreSQLSchemaManagementQueries.ownerAndComment(schema: Self.schema) + #expect(sql.contains("CURRENT_USER")) + #expect(!sql.contains("pg_catalog.current_user")) + } + + // A database owner can give the session a default path that names its schema before pg_catalog, where an operator + // of identical signature wins. These run on that path, so each names pg_catalog's operator or uses none. + @Test("Statements sent on the session's own path name pg_catalog's operators") + func sessionPathStatementsNamePgCatalogOperators() throws { + let statements = [ + PostgreSQLSchemaQueries.allTablesMetadata(schema: Self.schema), + PostgreSQLSessionScopeSQL.captureStatement(names: ["DateStyle", "TimeZone"]).sql, + PostgreSQLRoutineDebugProtocol.backendCountStatement(process: 42), + PostgreSQLRoutineDebugProtocol.routineSourceStatement(oid: 16_385), + try #require(PostgreSQLRoutineDebugProtocol.targetDefaultsStatement(oid: 16_385, argumentCount: 2)), + try #require(PostGISSpatialRewrite.conversionQuery(for: PostGISType(name: "geometry", schema: Self.schema))) + ] + for sql in statements { + #expect(!sql.contains(" = ") && !sql.contains(" <> ") && !sql.contains(" IN ("), "\(sql)") + #expect(Self.unqualifiedCalls(in: sql).isEmpty, "\(sql)") + } + } + + @Test("The debugger reads no defaults for a routine without arguments") + func noDefaultsStatementWithoutArguments() { + #expect(PostgreSQLRoutineDebugProtocol.targetDefaultsStatement(oid: 16_385, argumentCount: 0) == nil) + } + + // Sent on the user's session after a result with a type created since connect, so under that session's path. + @Test("The type-name lookup compares oids with oids") + func typeNameLookupComparesOids() throws { + let sql = try #require(PostgreSQLCatalogTypeNames.lookupQuery(oids: [16_385])) + #expect(sql.contains("WHERE t.oid IN ('16385'::pg_catalog.oid)")) + } + + @Test("The trigger listing masks tgtype as an integer, which pg_catalog's & takes exactly") + func triggerMasksAreExact() { + let sql = PostgreSQLObjectQueries.triggerList(schema: Self.schema, table: Self.table) + #expect(!sql.contains("(t.tgtype &")) + #expect(sql.contains("(t.tgtype::pg_catalog.int4 & 64) <> 0")) + } + + @Test("The type listing compares collation and support-function oids with oids") + func typeListingComparesExactTypes() { + let sql = PostgreSQLObjectQueries.userDefinedTypeList( + schema: Self.schema, identity: nil, capabilities: Self.capabilities + ) + #expect(!sql.contains("collation <> 0 ")) + #expect(!sql.contains("rngcanonical <> 0")) + #expect(sql.contains("r.rngcanonical::pg_catalog.oid <> 0::pg_catalog.oid")) + } + + @Test("The column read leaves the path-dependent spellings to the schema-relative read") + func columnsQueryDeparsesNothing() { + let sql = PostgreSQLSchemaQueries.columnsQuery( + schema: Self.schema, table: Self.table, capabilities: Self.capabilities, includeMaterializedViews: true + ) + #expect(!sql.contains("pg_get_expr")) + #expect(!sql.contains("format_type(a.atttypid, a.atttypmod)")) + #expect(!sql.contains("c.column_default")) + } +} diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLCatalogSQLPinTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLCatalogSQLPinTests.swift index b3f682d6a6..f232e0994c 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLCatalogSQLPinTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLCatalogSQLPinTests.swift @@ -106,10 +106,10 @@ struct PostgreSQLCatalogSQLPinTests { } private static let storedGenerationArm = - "WHEN a.attgenerated = 's' THEN ' GENERATED ALWAYS AS (' || pg_get_expr(d.adbin, d.adrelid) || ') STORED'" + "WHEN a.attgenerated = 's' THEN ' GENERATED ALWAYS AS (' || pg_catalog.pg_get_expr(d.adbin, d.adrelid) || ') STORED'" private static let virtualGenerationArm = - "WHEN a.attgenerated = 'v' THEN ' GENERATED ALWAYS AS (' || pg_get_expr(d.adbin, d.adrelid) || ') VIRTUAL'" + "WHEN a.attgenerated = 'v' THEN ' GENERATED ALWAYS AS (' || pg_catalog.pg_get_expr(d.adbin, d.adrelid) || ') VIRTUAL'" private static let iOSListing = """ SELECT c.relname AS table_name, @@ -269,7 +269,7 @@ struct PostgreSQLCatalogSQLPinTests { c.column_name AS column_name, c.data_type AS data_type, c.is_nullable AS is_nullable, - c.column_default AS column_default, + NULL::text AS column_default, c.collation_name AS collation_name, pg_catalog.col_description(rel.oid, c.ordinal_position) AS column_comment, c.udt_name AS udt_name, @@ -277,31 +277,8 @@ struct PostgreSQLCatalogSQLPinTests { a.attidentity AS identity_kind, a.attgenerated AS generated_kind, c.udt_schema AS udt_schema, - CASE WHEN a.attgenerated IN ('s', 'v') - THEN (SELECT pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) - FROM pg_catalog.pg_attrdef ad - WHERE ad.adrelid = a.attrelid AND ad.adnum = a.attnum) - END AS generation_expression, - CASE WHEN EXISTS ( - SELECT 1 - FROM pg_catalog.pg_type dt - JOIN pg_catalog.pg_namespace dtn ON dtn.oid = dt.typnamespace - WHERE dt.oid = a.atttypid - AND dtn.nspname <> 'pg_catalog' - AND pg_catalog.pg_type_is_visible(dt.oid) - AND EXISTS ( - SELECT 1 - FROM pg_catalog.pg_depend dd - WHERE dd.classid = 'pg_catalog.pg_type'::pg_catalog.regclass - AND dd.deptype = 'e' - AND dd.objid = CASE WHEN dt.typlen = -1 AND dt.typelem <> 0 - THEN dt.typelem ELSE dt.oid END)) - THEN (SELECT pg_catalog.quote_ident(dtn.nspname) || '.' - FROM pg_catalog.pg_type dt - JOIN pg_catalog.pg_namespace dtn ON dtn.oid = dt.typnamespace - WHERE dt.oid = a.atttypid) - ELSE '' END - || pg_catalog.format_type(a.atttypid, a.atttypmod) AS declared_type, + NULL::text AS generation_expression, + NULL::text AS declared_type, c.domain_name AS domain_name, c.ordinal_position AS ordinal_position FROM information_schema.columns c @@ -347,26 +324,7 @@ struct PostgreSQLCatalogSQLPinTests { mva.attgenerated AS generated_kind, COALESCE(mvbtn.nspname, mvtn.nspname) AS udt_schema, NULL::text AS generation_expression, - CASE WHEN EXISTS ( - SELECT 1 - FROM pg_catalog.pg_type dt - JOIN pg_catalog.pg_namespace dtn ON dtn.oid = dt.typnamespace - WHERE dt.oid = mva.atttypid - AND dtn.nspname <> 'pg_catalog' - AND pg_catalog.pg_type_is_visible(dt.oid) - AND EXISTS ( - SELECT 1 - FROM pg_catalog.pg_depend dd - WHERE dd.classid = 'pg_catalog.pg_type'::pg_catalog.regclass - AND dd.deptype = 'e' - AND dd.objid = CASE WHEN dt.typlen = -1 AND dt.typelem <> 0 - THEN dt.typelem ELSE dt.oid END)) - THEN (SELECT pg_catalog.quote_ident(dtn.nspname) || '.' - FROM pg_catalog.pg_type dt - JOIN pg_catalog.pg_namespace dtn ON dtn.oid = dt.typnamespace - WHERE dt.oid = mva.atttypid) - ELSE '' END - || pg_catalog.format_type(mva.atttypid, mva.atttypmod) AS declared_type, + NULL::text AS declared_type, CASE WHEN mvt.typtype = 'd' THEN mvt.typname END AS domain_name, mva.attnum AS ordinal_position FROM pg_catalog.pg_class mvc diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLColumnQueryTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLColumnQueryTests.swift index 2d97d2f31c..409f84b31b 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLColumnQueryTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLColumnQueryTests.swift @@ -80,9 +80,13 @@ struct PostgreSQLColumnsQueryTests { let query = singleTable(schema: "s2", table: "orders") #expect(query.contains("a.attidentity")) #expect(query.contains("a.attgenerated")) - #expect(query.contains("CASE WHEN a.attgenerated IN ('s', 'v')")) - #expect(query.contains("THEN (SELECT pg_catalog.pg_get_expr(ad.adbin, ad.adrelid)")) + #expect(query.contains("NULL::text AS generation_expression")) #expect(!query.contains("c.generation_expression")) + let spelling = PostgreSQLSchemaQueries.columnSpellingQuery( + schema: "s2", table: "orders", capabilities: PostgreSQLCapabilities(serverVersion: 170_011) + ) + #expect(spelling.contains("CASE WHEN a.attgenerated IN ('s', 'v') THEN pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) END")) + #expect(spelling.contains("CASE WHEN NOT (a.attgenerated IN ('s', 'v')) THEN pg_catalog.pg_get_expr(ad.adbin, ad.adrelid) END")) #expect(query.contains("ON a.attrelid = rel.oid")) #expect(query.contains("AND a.attnum = c.ordinal_position")) } @@ -102,8 +106,7 @@ struct PostgreSQLColumnsQueryTests { @Test("both the modern and the legacy query read the declared type and the domain name") func everyServerReadsTheDeclaredType() { for query in [singleTable(schema: "s2", table: "orders"), allTables(schema: "s2")] { - #expect(query.contains("pg_catalog.format_type(a.atttypid, a.atttypmod)")) - #expect(query.contains("AS declared_type")) + #expect(query.contains("NULL::text AS declared_type")) #expect(query.contains("c.domain_name AS domain_name")) #expect(query.contains("ON a.attrelid = rel.oid")) #expect(query.contains("AND a.attnum = c.ordinal_position")) @@ -256,8 +259,12 @@ struct PostgreSQLMaterializedViewColumnsQueryTests { @Test("the declared type carries the modifier and the domain name comes from the type itself") func armReadsTheDeclaredType() { let rendered = query() - #expect(rendered.contains("pg_catalog.format_type(mva.atttypid, mva.atttypmod)")) - #expect(rendered.contains("AS declared_type")) + #expect(rendered.contains("NULL::text AS declared_type")) + let spelling = PostgreSQLSchemaQueries.columnSpellingQuery( + schema: "public", table: nil, capabilities: PostgreSQLCapabilities(serverVersion: 170_011) + ) + #expect(spelling.contains("c.relkind IN ('r', 'v', 'f', 'p', 'm')")) + #expect(spelling.contains("pg_catalog.format_type(a.atttypid, a.atttypmod)")) #expect(rendered.contains("CASE WHEN mvt.typtype = 'd' THEN mvt.typname END AS domain_name")) } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLColumnTypeSpellingTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLColumnTypeSpellingTests.swift index 3f393de0ef..2eaf394699 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLColumnTypeSpellingTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLColumnTypeSpellingTests.swift @@ -129,18 +129,6 @@ struct PostgreSQLColumnTypeSpellingTests { } struct PostgreSQLSchemaRelativeReadTests { - @Test("The prefix narrows the path to pg_catalog and the schema, with the identifier quoted") - func prefixQuotesTheSchema() { - #expect( - PostgreSQLSchemaQueries.schemaRelativeReadPrefix(schema: "sales") - == "SET LOCAL search_path = pg_catalog, \"sales\"; " - ) - #expect( - PostgreSQLSchemaQueries.schemaRelativeReadPrefix(schema: "Weird \"Schema\"") - == "SET LOCAL search_path = pg_catalog, \"Weird \"\"Schema\"\"\"; " - ) - } - @Test("A type an extension owns is qualified, taken from the element type for an array") func extensionTypesAreQualified() { let expression = PostgreSQLSchemaQueries.declaredType(attribute: "a") @@ -150,7 +138,7 @@ struct PostgreSQLSchemaRelativeReadTests { #expect(expression.contains("dtn.nspname <> 'pg_catalog'")) let collapsed = expression.split(whereSeparator: \.isWhitespace).joined(separator: " ") #expect(collapsed.contains( - "dd.objid = CASE WHEN dt.typlen = -1 AND dt.typelem <> 0 THEN dt.typelem ELSE dt.oid END" + "dd.objid = CASE WHEN dt.typlen = -1 AND dt.typelem <> 0::pg_catalog.oid THEN dt.typelem ELSE dt.oid END" )) #expect(expression.contains("pg_catalog.quote_ident(dtn.nspname) || '.'")) } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLDefaultSchemaFallbackTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLDefaultSchemaFallbackTests.swift index 4482aeb561..f43f034784 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLDefaultSchemaFallbackTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLDefaultSchemaFallbackTests.swift @@ -10,12 +10,12 @@ import Testing struct PostgreSQLDefaultSchemaFallbackTests { @Test("asks the server for the active schema first") func currentSchemaQuery() { - #expect(PostgreSQLSchemaQueries.currentSchema == "SELECT current_schema()") + #expect(PostgreSQLSchemaQueries.currentSchema == "SELECT pg_catalog.current_schema()") } @Test("resolves the first existing search path entry, omitting missing schemas") func firstSearchPathSchemaQuery() { - #expect(PostgreSQLSchemaQueries.firstSearchPathSchema == "SELECT (current_schemas(false))[1]") + #expect(PostgreSQLSchemaQueries.firstSearchPathSchema == "SELECT (pg_catalog.current_schemas(false))[1]") } @Test("falls back to the effective search path before the alphabetical schema list") diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLLegacyCatalogQueryTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLLegacyCatalogQueryTests.swift index 3490ca9fd2..22c06d466b 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLLegacyCatalogQueryTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLLegacyCatalogQueryTests.swift @@ -102,7 +102,7 @@ struct PostgreSQLLegacyCatalogQueryTests { @Test("The search-path fallback parenthesises the function before subscripting it") func searchPathFallbackParses() { - #expect(PostgreSQLSchemaQueries.firstSearchPathSchema == "SELECT (current_schemas(false))[1]") + #expect(PostgreSQLSchemaQueries.firstSearchPathSchema == "SELECT (pg_catalog.current_schemas(false))[1]") } } @@ -413,8 +413,8 @@ struct PostgreSQLCollationAndMetadataQueryTests { @Test("The schema is quoted as a literal") func allTablesMetadataQuotesSchema() { - #expect(PostgreSQLSchemaQueries.allTablesMetadata(schema: "o'brien").contains("schemaname = 'o''brien'")) - #expect(PostgreSQLSchemaQueries.allTablesMetadata(schema: "a\\b").contains("schemaname = E'a\\\\b'")) + #expect(PostgreSQLSchemaQueries.allTablesMetadata(schema: "o'brien").contains("schemaname OPERATOR(pg_catalog.=) 'o''brien'")) + #expect(PostgreSQLSchemaQueries.allTablesMetadata(schema: "a\\b").contains("schemaname OPERATOR(pg_catalog.=) E'a\\\\b'")) } } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLObjectQueryTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLObjectQueryTests.swift index 35b3090353..96e851fda7 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLObjectQueryTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLObjectQueryTests.swift @@ -29,9 +29,8 @@ struct PostgreSQLObjectQueryTests { @Test("The extension test names pg_proc, so a member of another catalog sharing the oid hides nothing") func routineExtensionTestNamesItsCatalog() { let sql = PostgreSQLObjectQueries.routineList(schema: "public", capabilities: .assumingModernWhenUnknown(160_000)) - #expect(sql.contains( - "WHERE d.classid = 'pg_catalog.pg_proc'::pg_catalog.regclass AND d.objid = p.oid AND d.deptype = 'e'" - )) + #expect(sql.contains("WHERE d.classid = 'pg_catalog.pg_proc'::pg_catalog.regclass::pg_catalog.oid")) + #expect(sql.contains("AND d.objid = p.oid AND d.deptype = 'e'")) } @Test("Aggregates and window functions are excluded because pg_get_functiondef raises on them") @@ -47,7 +46,7 @@ struct PostgreSQLObjectQueryTests { @Test("The DDL fetch addresses one oid, so an overload cannot resolve to a sibling") func routineDefinitionIsByOid() { let sql = PostgreSQLObjectQueries.routineDefinition(identity: "16401") - #expect(sql.contains("'16401'::oid")) + #expect(sql.contains("'16401'::pg_catalog.oid")) #expect(!sql.contains("LIMIT 1")) } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLSessionScopeSQLTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLSessionScopeSQLTests.swift index b39c229f05..632a24484e 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLSessionScopeSQLTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLSessionScopeSQLTests.swift @@ -46,7 +46,7 @@ struct PostgreSQLSessionScopeSQLTests { let statement = PostgreSQLSessionScopeSQL.captureStatement(names: ["DateStyle", "search_path", "TimeZone"]) #expect(statement.sql == """ SELECT name, setting FROM pg_catalog.pg_settings \ - WHERE pg_catalog.lower(name) IN ('datestyle', 'search_path', 'timezone') + WHERE pg_catalog.lower(name) OPERATOR(pg_catalog.=) ANY (ARRAY['datestyle', 'search_path', 'timezone']::pg_catalog.text[]) """) #expect(statement.parameters.isEmpty) } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLTypeQueryTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLTypeQueryTests.swift index 9fc45fc9af..15b79b0160 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLTypeQueryTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLTypeQueryTests.swift @@ -64,7 +64,7 @@ struct PostgreSQLTypeQueryTests { let sql = PostgreSQLObjectQueries.userDefinedTypeList( schema: nil, identity: "16387", capabilities: .assumingModernWhenUnknown(170_000) ) - #expect(sql.contains("AND t.oid = 16387::oid")) + #expect(sql.contains("AND t.oid = 16387::pg_catalog.oid")) #expect(!sql.contains("n.nspname =")) } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLVersionedStatementsTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLVersionedStatementsTests.swift index 4e285b8dbd..7ba7e8b292 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLVersionedStatementsTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLVersionedStatementsTests.swift @@ -92,7 +92,7 @@ struct PostgreSQLVersionedStatementsTests { let statement = PostgreSQLVersionedStatements.createSchema("sales", capabilities: Self.v91) #expect(statement.hasPrefix("DO $tablepro$ ")) #expect(statement.hasSuffix(" $tablepro$")) - #expect(statement.contains("WHERE nspname = 'sales'")) + #expect(statement.contains("WHERE nspname OPERATOR(pg_catalog.=) 'sales'")) #expect(statement.contains("EXECUTE 'CREATE SCHEMA \"sales\"'")) #expect(!statement.contains("IF NOT EXISTS \"")) } @@ -100,7 +100,7 @@ struct PostgreSQLVersionedStatementsTests { @Test("A legacy schema name with quotes and backslashes stays inside its literals") func createSchemaLegacyEscapesName() { let statement = PostgreSQLVersionedStatements.createSchema(#"o'b\"x"#, capabilities: Self.v91) - #expect(statement.contains(#"nspname = E'o''b\\"x'"#)) + #expect(statement.contains(#"nspname OPERATOR(pg_catalog.=) E'o''b\\"x'"#)) #expect(statement.contains(#"EXECUTE E'CREATE SCHEMA "o''b\\""x"'"#)) } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLViewDefinitionTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLViewDefinitionTests.swift index b289c36571..475f6b1d4a 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLViewDefinitionTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLViewDefinitionTests.swift @@ -172,7 +172,6 @@ struct PostgreSQLViewDefinitionTests { func catalogQueryIsQualifiedAndScoped() { let query = PostgreSQLViewDefinition.catalogQuery(name: "vw", schema: "sales") - #expect(PostgreSQLViewDefinition.qualifiedReadPrefix == "SET LOCAL search_path = pg_catalog; ") #expect(query.contains("pg_catalog.pg_get_viewdef(c.oid, true)")) #expect(query.contains("n.nspname = 'sales'")) #expect(query.contains("c.relname = 'vw'")) diff --git a/apple/TablePro/Core/ServerDashboard/Providers/PostgreSQLActivityCatalog.swift b/apple/TablePro/Core/ServerDashboard/Providers/PostgreSQLActivityCatalog.swift index 9371a17aa6..7a374d26b7 100644 --- a/apple/TablePro/Core/ServerDashboard/Providers/PostgreSQLActivityCatalog.swift +++ b/apple/TablePro/Core/ServerDashboard/Providers/PostgreSQLActivityCatalog.swift @@ -63,45 +63,56 @@ enum PostgreSQLActivityCatalog: Equatable, Sendable { values.map { "'\($0)'" }.joined(separator: ", ") } + // The dashboard reads on the user's session, whose path may name another schema before pg_catalog, so every + // function, relation, type and operator here names pg_catalog. A CASE with an operand or an IN list would look its + // equality operator up on that path. + private static func isAny(_ column: String, of values: [String]) -> String { + "\(column) OPERATOR(pg_catalog.=) ANY (ARRAY[\(literalList(values))]::pg_catalog.text[])" + } + + private static let duration = + "EXTRACT(EPOCH FROM (pg_catalog.now() OPERATOR(pg_catalog.-) query_start))::pg_catalog.int4 AS duration_secs" + var sessionsQuery: String { switch self { case .current: return """ SELECT pid, usename, datname, state, - EXTRACT(EPOCH FROM (now() - query_start))::int AS duration_secs, - left(query, 1000) AS query - FROM pg_stat_activity - WHERE pid <> pg_backend_pid() - AND backend_type = 'client backend' + \(Self.duration), + pg_catalog.left(query, 1000) AS query + FROM pg_catalog.pg_stat_activity + WHERE pid OPERATOR(pg_catalog.<>) pg_catalog.pg_backend_pid() + AND backend_type OPERATOR(pg_catalog.=) 'client backend' ORDER BY query_start NULLS LAST """ case .withoutBackendType: return """ SELECT pid, usename, datname, state, - EXTRACT(EPOCH FROM (now() - query_start))::int AS duration_secs, - left(query, 1000) AS query - FROM pg_stat_activity - WHERE pid <> pg_backend_pid() + \(Self.duration), + pg_catalog.left(query, 1000) AS query + FROM pg_catalog.pg_stat_activity + WHERE pid OPERATOR(pg_catalog.<>) pg_catalog.pg_backend_pid() ORDER BY query_start NULLS LAST """ case .procpid: return """ SELECT procpid AS pid, usename, datname, - CASE current_query - WHEN '' THEN 'idle' - WHEN ' in transaction' THEN 'idle in transaction' - WHEN ' in transaction (aborted)' THEN 'idle in transaction (aborted)' - WHEN '' THEN 'disabled' - WHEN '' THEN NULL - WHEN '' THEN NULL + CASE + WHEN current_query OPERATOR(pg_catalog.=) '' THEN 'idle' + WHEN current_query OPERATOR(pg_catalog.=) ' in transaction' THEN 'idle in transaction' + WHEN current_query OPERATOR(pg_catalog.=) ' in transaction (aborted)' + THEN 'idle in transaction (aborted)' + WHEN current_query OPERATOR(pg_catalog.=) '' THEN 'disabled' + WHEN current_query OPERATOR(pg_catalog.=) '' THEN NULL + WHEN current_query OPERATOR(pg_catalog.=) '' THEN NULL ELSE 'active' END AS state, - EXTRACT(EPOCH FROM (now() - query_start))::int AS duration_secs, - CASE WHEN current_query IN (\(Self.literalList(Self.idleSentinels))) THEN '' - ELSE left(current_query, 1000) + \(Self.duration), + CASE WHEN \(Self.isAny("current_query", of: Self.idleSentinels)) THEN '' + ELSE pg_catalog.left(current_query, 1000) END AS query - FROM pg_stat_activity - WHERE procpid <> pg_backend_pid() + FROM pg_catalog.pg_stat_activity + WHERE procpid OPERATOR(pg_catalog.<>) pg_catalog.pg_backend_pid() ORDER BY query_start NULLS LAST """ } @@ -110,9 +121,10 @@ enum PostgreSQLActivityCatalog: Equatable, Sendable { var connectionCountQuery: String { switch self { case .current: - return "SELECT count(*) FROM pg_stat_activity WHERE backend_type = 'client backend'" + return "SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_activity " + + "WHERE backend_type OPERATOR(pg_catalog.=) 'client backend'" case .withoutBackendType, .procpid: - return "SELECT count(*) FROM pg_stat_activity" + return "SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_activity" } } @@ -120,14 +132,14 @@ enum PostgreSQLActivityCatalog: Equatable, Sendable { switch self { case .current, .withoutBackendType: return """ - SELECT count(*) FROM pg_stat_activity - WHERE state = 'active' AND pid <> pg_backend_pid() + SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_activity + WHERE state OPERATOR(pg_catalog.=) 'active' AND pid OPERATOR(pg_catalog.<>) pg_catalog.pg_backend_pid() """ case .procpid: return """ - SELECT count(*) FROM pg_stat_activity - WHERE current_query NOT IN (\(Self.literalList(Self.hiddenSentinels))) - AND procpid <> pg_backend_pid() + SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_activity + WHERE NOT \(Self.isAny("current_query", of: Self.hiddenSentinels)) + AND procpid OPERATOR(pg_catalog.<>) pg_catalog.pg_backend_pid() """ } } @@ -137,23 +149,23 @@ enum PostgreSQLActivityCatalog: Equatable, Sendable { case .current, .withoutBackendType: return """ SELECT pid, usename, datname, - EXTRACT(EPOCH FROM (now() - query_start))::int AS duration_secs, - left(query, 1000) AS query - FROM pg_stat_activity - WHERE state = 'active' - AND now() - query_start > interval '1 second' - AND pid <> pg_backend_pid() + \(Self.duration), + pg_catalog.left(query, 1000) AS query + FROM pg_catalog.pg_stat_activity + WHERE state OPERATOR(pg_catalog.=) 'active' + AND (pg_catalog.now() OPERATOR(pg_catalog.-) query_start) OPERATOR(pg_catalog.>) INTERVAL '1 second' + AND pid OPERATOR(pg_catalog.<>) pg_catalog.pg_backend_pid() ORDER BY query_start """ case .procpid: return """ SELECT procpid AS pid, usename, datname, - EXTRACT(EPOCH FROM (now() - query_start))::int AS duration_secs, - left(current_query, 1000) AS query - FROM pg_stat_activity - WHERE current_query NOT IN (\(Self.literalList(Self.hiddenSentinels))) - AND now() - query_start > interval '1 second' - AND procpid <> pg_backend_pid() + \(Self.duration), + pg_catalog.left(current_query, 1000) AS query + FROM pg_catalog.pg_stat_activity + WHERE NOT \(Self.isAny("current_query", of: Self.hiddenSentinels)) + AND (pg_catalog.now() OPERATOR(pg_catalog.-) query_start) OPERATOR(pg_catalog.>) INTERVAL '1 second' + AND procpid OPERATOR(pg_catalog.<>) pg_catalog.pg_backend_pid() ORDER BY query_start """ } diff --git a/apple/TablePro/Core/ServerDashboard/Providers/PostgreSQLDashboardProvider.swift b/apple/TablePro/Core/ServerDashboard/Providers/PostgreSQLDashboardProvider.swift index 7a13e22f1a..92b7782f49 100644 --- a/apple/TablePro/Core/ServerDashboard/Providers/PostgreSQLDashboardProvider.swift +++ b/apple/TablePro/Core/ServerDashboard/Providers/PostgreSQLDashboardProvider.swift @@ -87,12 +87,12 @@ struct PostgreSQLDashboardProvider: ServerDashboardQueryProvider { func killSessionSQL(processId: String) -> String? { guard acceptsProcessId(processId), let pid = Int(processId) else { return nil } - return "SELECT pg_terminate_backend(\(pid))" + return "SELECT pg_catalog.pg_terminate_backend(\(pid))" } func cancelQuerySQL(processId: String) -> String? { guard acceptsProcessId(processId), let pid = Int(processId) else { return nil } - return "SELECT pg_cancel_backend(\(pid))" + return "SELECT pg_catalog.pg_cancel_backend(\(pid))" } func acceptsProcessId(_ processId: String) -> Bool { @@ -130,10 +130,16 @@ private extension PostgreSQLDashboardProvider { unit: "%", icon: "bolt", query: """ - SELECT CASE WHEN blks_hit + blks_read = 0 THEN '0' - ELSE round(blks_hit::numeric / (blks_hit + blks_read) * 100, 1)::text + SELECT CASE WHEN (blks_hit OPERATOR(pg_catalog.+) blks_read) OPERATOR(pg_catalog.=) 0 THEN '0' + ELSE pg_catalog.round( + blks_hit::pg_catalog.numeric + OPERATOR(pg_catalog./) (blks_hit OPERATOR(pg_catalog.+) blks_read)::pg_catalog.numeric + OPERATOR(pg_catalog.*) 100.0, + 1 + )::pg_catalog.text END - FROM pg_stat_database WHERE datname = current_database() + FROM pg_catalog.pg_stat_database + WHERE datname OPERATOR(pg_catalog.=) pg_catalog.current_database() """ ), MetricDefinition( @@ -141,7 +147,7 @@ private extension PostgreSQLDashboardProvider { label: String(localized: "Database Size"), unit: "", icon: "internaldrive", - query: "SELECT pg_size_pretty(pg_database_size(current_database()))" + query: "SELECT pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(pg_catalog.current_database()))" ), MetricDefinition( id: "uptime", @@ -149,7 +155,12 @@ private extension PostgreSQLDashboardProvider { unit: "", icon: "clock", // KingbaseES in Oracle mode subtracts timestamps into an Oracle interval, printed as +000000000 00:20:19. - query: "SELECT date_trunc('second', CAST(now() - pg_postmaster_start_time() AS pg_catalog.interval))::text" + query: """ + SELECT pg_catalog.date_trunc( + 'second', + CAST(pg_catalog.now() OPERATOR(pg_catalog.-) pg_catalog.pg_postmaster_start_time() AS pg_catalog.interval) + )::pg_catalog.text + """ ), MetricDefinition( id: "active_queries", diff --git a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver+Catalog.swift b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver+Catalog.swift index 07d1744849..baf2f324ed 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver+Catalog.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver+Catalog.swift @@ -12,7 +12,7 @@ nonisolated extension PostgreSQLDriver { let schemaName = schema ?? effectiveSchema let query = Self.tablesQuery(schema: schemaName, databaseType: databaseType, presence: catalogPresence) do { - let result = try await execute(query: query) + let result = try await catalogRead(query) return Self.tables(fromRows: result.rows, databaseType: databaseType) } catch is CancellationError { throw CancellationError() @@ -20,7 +20,7 @@ nonisolated extension PostgreSQLDriver { guard let fallback = Self.fallbackTablesQuery(schema: schemaName, databaseType: databaseType) else { throw error } Self.logger.debug("Table listing fell back to information_schema: \(error.localizedDescription, privacy: .private)") try Task.checkCancellation() - let result = try await execute(query: fallback) + let result = try await catalogRead(fallback) return Self.tables(fromRows: result.rows, databaseType: databaseType) } } @@ -47,7 +47,7 @@ nonisolated extension PostgreSQLDriver { } private func readColumns(table: String, schema: String, shape: PostgreSQLColumnReadShape) async throws -> [ColumnInfo] { - let result = try await execute(query: Self.columnsQuery(schema: schema, table: table, shape: shape)) + let result = try await catalogRead(Self.columnsQuery(schema: schema, table: table, shape: shape)) columnReadSupport = columnReadSupport.learning( from: shape, materializedViewsPresent: catalogPresence.hasMaterializedViews @@ -64,7 +64,7 @@ nonisolated extension PostgreSQLDriver { return columns } do { - let result = try await execute(query: Self.enumLabelsQuery(schema: schema, table: table)) + let result = try await catalogRead(Self.enumLabelsQuery(schema: schema, table: table)) let labels = Self.enumLabels(fromRows: result.rows) return columns.map { column in labels[column.name].map { column.withAllowedValues($0) } ?? column @@ -111,7 +111,7 @@ nonisolated extension PostgreSQLDriver { databaseType: databaseType, serverVersionNumber: serverVersionNumber ) - let result = try await execute(query: query) + let result = try await catalogRead(query) return Self.indexes(fromRows: result.rows, databaseType: databaseType) } diff --git a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift index b779a6ab28..93731fe39c 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift @@ -67,7 +67,7 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { private func probeCatalogPresence() async -> PostgreSQLCatalogPresence { guard databaseType != .redshift else { return Self.presence(probedRows: nil) } do { - let rows = try await actor.execute(PostgreSQLCatalogPresence.probeQuery).rows + let rows = try await catalogRead(PostgreSQLCatalogPresence.probeQuery).rows return Self.presence(probedRows: rows) } catch { Self.logger.warning( @@ -78,7 +78,7 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { } private func adoptServerSchema() async { - guard let schema = try? await actor.execute("SELECT current_schema()").rows.first?.first ?? nil, + guard let schema = try? await actor.execute("SELECT pg_catalog.current_schema()").rows.first?.first ?? nil, !schema.isEmpty else { return } currentSchema = schema } @@ -95,8 +95,18 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { // MARK: - Query Execution func execute(query: String) async throws -> QueryResult { - let raw = try await actor.execute(query) - return QueryResult( + Self.queryResult(from: try await actor.execute(query)) + } + + /// Every statement this driver writes against the catalogs runs here, under `PostgreSQLCatalogSearchPath`, so a + /// function or operator in the schema the session is switched to cannot stand in for a `pg_catalog` one. Redshift + /// has no user-defined operators and was never measured with that path, so its statements run as they are. + func catalogRead(_ statement: String) async throws -> QueryResult { + Self.queryResult(from: try await actor.executeCatalogRead(statement, pinsSearchPath: databaseType != .redshift)) + } + + private static func queryResult(from raw: RawPGResult) -> QueryResult { + QueryResult( columns: raw.columns.enumerated().map { i, name in ColumnInfo( name: name, @@ -182,7 +192,7 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { // MARK: - Schema func fetchForeignKeys(table: String, schema: String?) async throws -> [ForeignKeyInfo] { - let raw = try await actor.execute( + let result = try await catalogRead( Self.foreignKeysQuery( schema: schema ?? effectiveSchema, table: table, @@ -190,7 +200,7 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { ) ) - return PostgreSQLCatalogForeignKeys.foreignKeys(from: raw.rows).map { key in + return PostgreSQLCatalogForeignKeys.foreignKeys(from: result.rows).map { key in ForeignKeyInfo( name: key.name, column: key.column, @@ -214,10 +224,10 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { } func fetchDatabases() async throws -> [String] { - let raw = try await actor.execute( - "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname" + let result = try await catalogRead( + "SELECT datname FROM pg_catalog.pg_database WHERE datistemplate = false ORDER BY datname" ) - return raw.rows.compactMap { $0.first ?? nil } + return result.rows.compactMap { $0.first ?? nil } } func switchDatabase(to name: String) async throws { @@ -231,7 +241,7 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { } func fetchSchemas() async throws -> [String] { - let result = try await execute(query: "SELECT schema_name FROM information_schema.schemata ORDER BY schema_name") + let result = try await catalogRead("SELECT schema_name FROM information_schema.schemata ORDER BY schema_name") return result.rows.compactMap { $0.first ?? nil } } @@ -281,6 +291,11 @@ nonisolated enum PostgreSQLSessionTransaction { } } + // A SET LOCAL sent inside an open or aborted block would outlast the statement that needed it. + static func isInsideBlock(_ status: PGTransactionStatusType) -> Bool { + status == PQTRANS_INTRANS || status == PQTRANS_INERROR + } + // An aborted block refuses SELECT 1 on a live session, an open one takes its snapshot from it, and a stream would end. static func pingSendsStatement(in status: PGTransactionStatusType) -> Bool { status == PQTRANS_IDLE @@ -458,6 +473,32 @@ private actor PostgreSQLActor { return "\(major).\(minor).\(patch)" } + /// The search path lasts only as long as the read: outside a transaction block the pair is one implicit + /// transaction, and inside one a savepoint rolled back straight after scopes it. One actor call, so no other + /// statement on this session runs between the savepoint and its rollback. + func executeCatalogRead(_ statement: String, pinsSearchPath: Bool) throws -> RawPGResult { + guard pinsSearchPath else { return try execute(statement) } + endLeftoverStream() + let scoped = PostgreSQLCatalogSearchPath.pinned + statement + guard let conn, PostgreSQLSessionTransaction.isInsideBlock(PQtransactionStatus(conn)) else { + return try execute(scoped) + } + _ = try execute(Self.catalogReadSavepoint) + let result: RawPGResult + do { + result = try execute(scoped) + } catch { + _ = try? execute(Self.catalogReadRollback) + throw error + } + _ = try execute(Self.catalogReadRollback) + return result + } + + private static let catalogReadSavepoint = "SAVEPOINT tablepro_catalog_read" + private static let catalogReadRollback = + "ROLLBACK TO SAVEPOINT tablepro_catalog_read; RELEASE SAVEPOINT tablepro_catalog_read" + func execute(_ query: String) throws -> RawPGResult { endLeftoverStream() guard let conn else { throw PostgreSQLError.notConnected } diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLTransactionStatementTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLTransactionStatementTests.swift index a066876055..e9af55ecea 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLTransactionStatementTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLTransactionStatementTests.swift @@ -35,6 +35,15 @@ struct PostgreSQLSessionTransactionTests { #expect(PostgreSQLSessionTransaction.state(from: PQTRANS_INERROR) == .abortedTransaction) } + // A catalog read's SET LOCAL search_path would outlast the read inside a block, so it takes a savepoint there. + @Test("Only an open or aborted block scopes a catalog read with a savepoint") + func catalogReadScopesInsideABlockOnly() { + #expect(PostgreSQLSessionTransaction.isInsideBlock(PQTRANS_INTRANS)) + #expect(PostgreSQLSessionTransaction.isInsideBlock(PQTRANS_INERROR)) + #expect(!PostgreSQLSessionTransaction.isInsideBlock(PQTRANS_IDLE)) + #expect(!PostgreSQLSessionTransaction.isInsideBlock(PQTRANS_ACTIVE)) + } + @Test("A statement in flight reports the transaction it was sent in") func statementInFlightReportsStatusAtSend() { #expect(PostgreSQLSessionTransaction.state(from: PQTRANS_ACTIVE, statusAtSend: PQTRANS_IDLE) == .idle) diff --git a/apple/TableProTests/Core/MCP/MCPServerDashboardPayloadTests.swift b/apple/TableProTests/Core/MCP/MCPServerDashboardPayloadTests.swift index a96b24e5cc..6aa8a625d0 100644 --- a/apple/TableProTests/Core/MCP/MCPServerDashboardPayloadTests.swift +++ b/apple/TableProTests/Core/MCP/MCPServerDashboardPayloadTests.swift @@ -128,7 +128,7 @@ struct MCPSessionControlStatementTests { #expect( try MCPConnectionBridge.sessionControlStatement( provider: try provider(.postgresql), processId: "52", cancelOnly: true - ) == "SELECT pg_cancel_backend(52)" + ) == "SELECT pg_catalog.pg_cancel_backend(52)" ) #expect( try MCPConnectionBridge.sessionControlStatement( diff --git a/apple/TableProTests/Core/ServerDashboard/PostgreSQLDashboardProviderTests.swift b/apple/TableProTests/Core/ServerDashboard/PostgreSQLDashboardProviderTests.swift index b7afd70ee6..c2739d9441 100644 --- a/apple/TableProTests/Core/ServerDashboard/PostgreSQLDashboardProviderTests.swift +++ b/apple/TableProTests/Core/ServerDashboard/PostgreSQLDashboardProviderTests.swift @@ -102,7 +102,7 @@ struct PostgreSQLDashboardProviderTests { let catalog = PostgreSQLActivityCatalog.procpid #expect(catalog.activeQueryCountQuery.contains("''")) #expect(catalog.slowQueriesQuery.contains("''")) - #expect(catalog.sessionsQuery.contains("WHEN '' THEN NULL")) + #expect(catalog.sessionsQuery.contains("WHEN current_query OPERATOR(pg_catalog.=) '' THEN NULL")) } @Test("Servers before 10 never ask for backend_type") @@ -120,7 +120,9 @@ struct PostgreSQLDashboardProviderTests { func procpidCatalogUsesPreNinePointTwoColumns() { let catalog = PostgreSQLActivityCatalog.procpid let queries = [catalog.sessionsQuery, catalog.activeQueryCountQuery, catalog.slowQueriesQuery] - #expect(queries.allSatisfy { $0.contains("procpid <> pg_backend_pid()") && $0.contains("current_query") }) + #expect(queries.allSatisfy { + $0.contains("procpid OPERATOR(pg_catalog.<>) pg_catalog.pg_backend_pid()") && $0.contains("current_query") + }) #expect(queries.allSatisfy { !$0.contains("left(query") }) #expect(catalog.sessionsQuery.contains("SELECT procpid AS pid")) #expect(catalog.slowQueriesQuery.contains("SELECT procpid AS pid")) @@ -128,12 +130,40 @@ struct PostgreSQLDashboardProviderTests { #expect(!catalog.activeQueryCountQuery.contains("state =")) } - @Test("The current catalog is the SQL the dashboard has always sent") - func currentCatalogIsUnchanged() { + @Test("The current catalog filters client backends and active sessions") + func currentCatalogFilters() { let catalog = PostgreSQLActivityCatalog.current - #expect(catalog.connectionCountQuery == "SELECT count(*) FROM pg_stat_activity WHERE backend_type = 'client backend'") - #expect(catalog.sessionsQuery.contains("AND backend_type = 'client backend'")) - #expect(catalog.slowQueriesQuery.contains("WHERE state = 'active'")) + #expect(catalog.connectionCountQuery == """ + SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_activity \ + WHERE backend_type OPERATOR(pg_catalog.=) 'client backend' + """) + #expect(catalog.sessionsQuery.contains("AND backend_type OPERATOR(pg_catalog.=) 'client backend'")) + #expect(catalog.slowQueriesQuery.contains("WHERE state OPERATOR(pg_catalog.=) 'active'")) + } + + // A database owner can set a default search_path that puts its own schema before pg_catalog, where a function, + // operator or relation of the same name would be read instead. + @Test("Every dashboard statement names pg_catalog's functions, relations and operators") + func everyStatementNamesPgCatalog() async throws { + let provider = PostgreSQLDashboardProvider(activityCatalog: .current, metricSet: .full) + var asked: [String] = [] + _ = try await provider.fetchMetrics { sql in + asked.append(sql) + return .empty + } + for catalog in [PostgreSQLActivityCatalog.current, .withoutBackendType, .procpid] { + asked += [ + catalog.sessionsQuery, catalog.connectionCountQuery, + catalog.activeQueryCountQuery, catalog.slowQueriesQuery + ] + } + asked += [provider.killSessionSQL(processId: "42") ?? "", provider.cancelQuerySQL(processId: "42") ?? ""] + for sql in asked { + #expect(!sql.contains(" = ") && !sql.contains(" <> ") && !sql.contains(" IN ("), "\(sql)") + #expect(!sql.contains("FROM pg_stat"), "\(sql)") + #expect(!sql.contains("SELECT count(") && !sql.contains("(now()"), "\(sql)") + } + #expect(provider.killSessionSQL(processId: "42") == "SELECT pg_catalog.pg_terminate_backend(42)") } @Test("CockroachDB is asked only for the metrics it has") diff --git a/apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.1.md b/apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.1.md new file mode 100644 index 0000000000..a97fb7f1a7 --- /dev/null +++ b/apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.1.md @@ -0,0 +1 @@ +A PostgreSQL type named geometry or geography that PostGIS did not create could run its owner's code as you. diff --git a/apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.2.md b/apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.2.md new file mode 100644 index 0000000000..18510ddde8 --- /dev/null +++ b/apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.2.md @@ -0,0 +1 @@ +Oracle Explain reading a PLAN_TABLE or V$ views planted in the tab's schema instead of Oracle's own. diff --git a/apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.md b/apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.md new file mode 100644 index 0000000000..0fd9ed81cb --- /dev/null +++ b/apple/changelog.d/+ghsa-hgr8-24vm-5m54.security.md @@ -0,0 +1 @@ +Browsing a PostgreSQL schema could run a function or operator its owner planted there, with your privileges. diff --git a/apple/scripts/ci/check-oracle-dictionary-names.py b/apple/scripts/ci/check-oracle-dictionary-names.py new file mode 100755 index 0000000000..2d7f432796 --- /dev/null +++ b/apple/scripts/ci/check-oracle-dictionary-names.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python3 +"""Fail when Oracle or Dameng SQL names a data dictionary object without its SYS owner. + +Oracle resolves a bare `ALL_TABLES`, `DUAL`, `V$SESSION` or `PLAN_TABLE` in the session's current schema +before the public synonym, so a schema the reader switched into can plant a view that runs with the reader's +privileges. `OracleDictionary` is the one place that spells these names; every other string literal +reaches them through it. Skipped: localized messages, the name handed to an `OracleDictionary` call, +and a lone word in an array literal (a keyword list), none of which is SQL. + +Usage: + check-oracle-dictionary-names.py [path...] # default: the Oracle and Dameng sources +""" + +import re +import sys +from pathlib import Path + +APPLE = Path(__file__).resolve().parents[2] + +DEFAULT_PATHS = [ + APPLE / "Packages/TableProOracle/Sources", + APPLE / "Plugins/OracleDriverPlugin", + APPLE / "Plugins/DamengDriverPlugin", + APPLE / "TablePro/Core/ServerDashboard/Providers/OracleDashboardProvider.swift", + APPLE / "TableProMobile/TableProMobile/Drivers/OracleDriver.swift", +] + +OWNER_FILE = "OracleDictionary.swift" + +QUALIFIED = re.compile(r"\bSYS\.[A-Z_$0-9]+") +# A lowercase alias in front marks a column (`c.USER_GENERATED`), not an object. +BARE = re.compile(r"(? String { + "EXPLAIN PLAN SET STATEMENT_ID = '\\(id)' FOR \\(statement)" + } + static func delete(_ id: String) -> String { + "DELETE FROM PLAN_TABLE WHERE STATEMENT_ID = '\\(id)'" + } + static let qualified = "EXPLAIN PLAN SET STATEMENT_ID = 'X' INTO SYS.PLAN_TABLE$ FOR SELECT 1 FROM SYS.DUAL" +} +''' + +BARE_IN_INTERPOLATION = '''enum Queries { + static let rows = """ + SELECT \\(columns.joined(separator: ", ")) FROM ALL_TABLES + """ + static let nested = "SELECT \\(name("x")) FROM \\(source ?? "DUAL")" +} +''' + + +class CheckOracleDictionaryNamesTests(unittest.TestCase): + def violations(self, source): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "Queries.swift" + path.write_text(source, encoding="utf-8") + return check.violations(path) + + def test_reported_probe_fails(self): + self.assertEqual(self.violations(REPORTED), [(4, "V$PARAMETER"), (6, "V$SESSION")]) + + def test_reported_estimated_plan_fails(self): + self.assertEqual(self.violations(REPORTED_ESTIMATED), [(3, "PLAN_TABLE"), (6, "PLAN_TABLE")]) + + def test_owner_qualified_names_pass(self): + self.assertEqual(self.violations(QUALIFIED), []) + + def test_comments_keyword_lists_and_localized_messages_pass(self): + self.assertEqual(self.violations(NOT_SQL), []) + + def test_text_around_and_inside_an_interpolation_is_checked(self): + self.assertEqual(self.violations(BARE_IN_INTERPOLATION), [(3, "ALL_TABLES"), (5, "DUAL")]) + + def test_the_owner_file_is_skipped_and_others_fail_the_run(self): + with tempfile.TemporaryDirectory() as directory: + Path(directory, "OracleDictionary.swift").write_text('let dual = "SELECT 1 FROM DUAL"', encoding="utf-8") + Path(directory, "Explain.swift").write_text(REPORTED, encoding="utf-8") + output = io.StringIO() + with contextlib.redirect_stdout(output): + status = check.main([directory]) + self.assertEqual(status, 1) + self.assertNotIn("OracleDictionary.swift", output.getvalue()) + self.assertIn("Explain.swift:4: V$PARAMETER", output.getvalue()) + + def test_the_tree_passes(self): + output = io.StringIO() + with contextlib.redirect_stdout(output): + status = check.main([]) + self.assertEqual(status, 0, output.getvalue()) + + +if __name__ == "__main__": + unittest.main() diff --git a/apple/scripts/probes/check-oracle-explain-plans.sh b/apple/scripts/probes/check-oracle-explain-plans.sh index b2806ee65a..e15af5b067 100755 --- a/apple/scripts/probes/check-oracle-explain-plans.sh +++ b/apple/scripts/probes/check-oracle-explain-plans.sh @@ -9,16 +9,22 @@ # driver returns with the one V$SQL holds for a marker unique to the run, and checks the grants message an account # without V$ access gets, and that the grants it names are enough. # +# Oracle resolves a bare PLAN_TABLE or V$ name in the session's current schema before the public synonym, and a tab pins +# its schema with ALTER SESSION SET CURRENT_SCHEMA. So it also plants a PLAN_TABLE and the three V$ views as BEQUEATH +# CURRENT_USER views in one account's schema, each calling an AUTHID CURRENT_USER function that logs its calls, and runs +# both plans as a CREATE SESSION-only account switched into that schema: the log must stay empty. And it runs the +# estimated plan as an account that keeps its own PLAN_TABLE from utlxplan.sql: the plan must not be written there. +# # Usage: # scripts/probes/check-oracle-explain-plans.sh [host] [port] [service] [user] [password] [sys-password] # # Defaults suit a throwaway Oracle Free container: # docker run -d --name oracle-probe -p 1521:1521 -e ORACLE_PASSWORD=probe_pw gvenzl/oracle-free:23-slim-faststart # -# The user needs DBA rights (SYSTEM): it creates tables prefixed TP_XP_ in its own schema and a user TP_XP_PLAIN, and -# drops all of it again. Only SYS can grant the V$ views, so that check logs in AS SYSDBA with sys-password (default: -# the password) and is skipped when it cannot. Set TP_CHECK_SCRATCH to a directory to reuse the build between -# runs. Exits 1 when a check fails, 3 when it cannot run. +# The user needs DBA rights (SYSTEM): it creates tables prefixed TP_XP_ in its own schema and the users TP_XP_PLAIN, +# TP_XP_MALLORY, TP_XP_READER and TP_XP_KEEPER, and drops all of it again. Only SYS can grant the V$ views, so that +# check logs in AS SYSDBA with sys-password (default: the password) and is skipped when it cannot. Set TP_CHECK_SCRATCH +# to a directory to reuse the build between runs. Exits 1 when a check fails, 3 when it cannot run. set -uo pipefail @@ -103,6 +109,9 @@ enum Check { static let owner = (environment["TP_CHECK_USER"] ?? "system").uppercased() static let plainUser = "TP_XP_PLAIN" static let plainPassword = "TpXp" + UUID().uuidString.replacingOccurrences(of: "-", with: "").prefix(12) + static let mallory = "TP_XP_MALLORY" + static let reader = "TP_XP_READER" + static let keeper = "TP_XP_KEEPER" static func expect(_ condition: Bool, _ label: String, _ detail: @autoclosure () -> String = "") { if condition { @@ -236,6 +245,8 @@ enum Check { await estimated(admin) await actual(admin) await withoutGrants(admin) + await plantedSchema(admin) + await ownPlanTable(admin) await cleanUp(admin) admin.disconnect() @@ -244,7 +255,7 @@ enum Check { } static func estimated(_ admin: OraclePluginDriver) async { - let planRowsBefore = await scalar(admin, "SELECT TO_CHAR(COUNT(*)) FROM PLAN_TABLE") + let planRowsBefore = await scalar(admin, "SELECT TO_CHAR(COUNT(*)) FROM SYS.PLAN_TABLE$") do { let explained = try await explain(admin, join(region: "north", terminator: ";"), OracleExplainPlan.estimatedVariantId) expect(explained.plan != nil, "the estimated plan of a three-table join parses into a tree") @@ -260,8 +271,8 @@ enum Check { } catch { expect(false, "the estimated plan of a three-table join", message(error)) } - let planRowsAfter = await scalar(admin, "SELECT TO_CHAR(COUNT(*)) FROM PLAN_TABLE") - expect(planRowsBefore != nil && planRowsBefore == planRowsAfter, "the estimated plan leaves no rows in PLAN_TABLE", + let planRowsAfter = await scalar(admin, "SELECT TO_CHAR(COUNT(*)) FROM SYS.PLAN_TABLE$") + expect(planRowsBefore != nil && planRowsBefore == planRowsAfter, "the estimated plan leaves no rows in SYS.PLAN_TABLE$", "\(planRowsBefore ?? "nil") -> \(planRowsAfter ?? "nil")") _ = try? await admin.explainPlan(statement: "INSERT INTO TP_XP_LOG VALUES (1)", variantId: OracleExplainPlan.estimatedVariantId) @@ -405,11 +416,171 @@ enum Check { } } + static func createUser(_ admin: OraclePluginDriver, _ name: String, _ privileges: String, quota: Bool = false) async -> Bool { + for statement in [ + "CREATE USER \(name) IDENTIFIED BY \"\(plainPassword)\"\(quota ? " QUOTA UNLIMITED ON USERS" : "")", + "GRANT \(privileges) TO \(name)", + ] + joinedTables.sorted().map({ "GRANT SELECT ON \($0) TO \(name)" }) { + if let failure = await run(admin, statement) { + print("SKIP: cannot create \(name): \(message(failure))") + return false + } + } + return true + } + + // The attacker's part: views named as Explain names its tables, in TP_XP_MALLORY's own schema. Each calls an + // AUTHID CURRENT_USER function that logs who ran it, so a call is code running with the reader's privileges. + static let planted = [ + "CREATE TABLE CALLS (WHO VARCHAR2(128), WHAT VARCHAR2(30))", + "GRANT INSERT ON CALLS TO PUBLIC", + """ + CREATE OR REPLACE FUNCTION PAYLOAD(WHAT VARCHAR2) RETURN NUMBER AUTHID CURRENT_USER IS + PRAGMA AUTONOMOUS_TRANSACTION; + BEGIN + INSERT INTO \(mallory).CALLS VALUES (SYS_CONTEXT('USERENV', 'CURRENT_USER'), WHAT); + COMMIT; + RETURN 0; + END; + """, + "GRANT EXECUTE ON PAYLOAD TO PUBLIC", + """ + CREATE VIEW PLAN_TABLE BEQUEATH CURRENT_USER AS + SELECT * FROM SYS.PLAN_TABLE$ WHERE \(mallory).PAYLOAD('PLAN_TABLE') = 0 + """, + "GRANT SELECT, INSERT, DELETE ON PLAN_TABLE TO PUBLIC", + """ + CREATE VIEW V$PARAMETER BEQUEATH CURRENT_USER AS + SELECT 'statistics_level' AS NAME, 'TYPICAL' AS VALUE FROM SYS.DUAL WHERE \(mallory).PAYLOAD('V$PARAMETER') = 0 + """, + """ + CREATE VIEW V$SESSION BEQUEATH CURRENT_USER AS + SELECT TO_NUMBER(SYS_CONTEXT('USERENV', 'SID')) AS SID, CAST(NULL AS VARCHAR2(13)) AS PREV_SQL_ID, + 0 AS PREV_CHILD_NUMBER FROM SYS.DUAL WHERE \(mallory).PAYLOAD('V$SESSION') = 0 + """, + """ + CREATE VIEW V$SQL_PLAN_STATISTICS_ALL BEQUEATH CURRENT_USER AS + SELECT p.*, 1 AS LAST_STARTS, 1 AS LAST_OUTPUT_ROWS, 1 AS LAST_ELAPSED_TIME, 1 AS LAST_CR_BUFFER_GETS, + 1 AS LAST_DISK_READS, 1 AS LAST_MEMORY_USED, 1 AS LAST_TEMPSEG_SIZE, CAST(NULL AS VARCHAR2(13)) AS SQL_ID, + 0 AS CHILD_NUMBER FROM SYS.PLAN_TABLE$ p WHERE \(mallory).PAYLOAD('V$SQL_PLAN_STATISTICS_ALL') = 0 + """, + "GRANT SELECT ON V$PARAMETER TO PUBLIC", + "GRANT SELECT ON V$SESSION TO PUBLIC", + "GRANT SELECT ON V$SQL_PLAN_STATISTICS_ALL TO PUBLIC", + ] + + static func plantedSchema(_ admin: OraclePluginDriver) async { + guard await createUser(admin, mallory, "CREATE SESSION, CREATE TABLE, CREATE VIEW, CREATE PROCEDURE", quota: true), + await createUser(admin, reader, "CREATE SESSION") + else { return } + let attacker: OraclePluginDriver + let victim: OraclePluginDriver + do { + attacker = try await connect(user: mallory, password: plainPassword) + victim = try await connect(user: reader, password: plainPassword) + } catch { + expect(false, "a login as \(mallory) and \(reader)", message(error)) + return + } + defer { + attacker.disconnect() + victim.disconnect() + } + for statement in planted { + if let failure = await run(attacker, statement) { + // BEQUEATH CURRENT_USER needs 12.1. + print("SKIP: the planted schema check, \(mallory) cannot plant its views: \(message(failure))") + return + } + } + + do { + try await victim.switchSchema(to: mallory) + } catch { + expect(false, "\(reader) switches into \(mallory) as a tab does", message(error)) + return + } + do { + let explained = try await explain(victim, join(region: "north"), OracleExplainPlan.estimatedVariantId) + expect(relations(explained.plan) == joinedTables, "Estimated Plan in \(mallory)'s schema shows the statement's plan", + "\(relations(explained.plan))") + } catch { + expect(false, "Estimated Plan in \(mallory)'s schema", message(error)) + } + do { + _ = try await victim.explainPlan(statement: join(region: "north"), variantId: OracleExplainPlan.actualVariantId) + expect(false, "Actual Plan in \(mallory)'s schema, without V$ grants, gets the grants message", "the plan ran") + } catch { + expect(message(error) == OracleExplainPlan.missingGrantsMessage, + "Actual Plan in \(mallory)'s schema, without V$ grants, gets the grants message", message(error)) + } + + let calls = await values(admin, "SELECT WHAT, WHO, TO_CHAR(COUNT(*)) FROM \(mallory).CALLS GROUP BY WHAT, WHO ORDER BY WHAT") + let total = calls.compactMap { Int($0[2]) }.reduce(0, +) + print("INFO: \(mallory)'s functions ran \(total) time(s): \(calls.map { "\($0[0]) as \($0[1]) x\($0[2])" })") + expect(total == 0, "Explain in \(mallory)'s schema runs none of \(mallory)'s functions") + } + + // utlxplan.sql as Oracle 23ai ships it, without SHARING, which only an application container takes. + static let utlxplanColumns = """ + STATEMENT_ID VARCHAR2(30), PLAN_ID NUMBER, TIMESTAMP DATE, REMARKS VARCHAR2(4000), OPERATION VARCHAR2(30), + OPTIONS VARCHAR2(255), OBJECT_NODE VARCHAR2(128), OBJECT_OWNER VARCHAR2(128), OBJECT_NAME VARCHAR2(128), + OBJECT_ALIAS VARCHAR2(261), OBJECT_INSTANCE NUMERIC, OBJECT_TYPE VARCHAR2(30), OPTIMIZER VARCHAR2(255), + SEARCH_COLUMNS NUMBER, ID NUMERIC, PARENT_ID NUMERIC, DEPTH NUMERIC, POSITION NUMERIC, COST NUMERIC, + CARDINALITY NUMERIC, BYTES NUMERIC, OTHER_TAG VARCHAR2(255), PARTITION_START VARCHAR2(255), + PARTITION_STOP VARCHAR2(255), PARTITION_ID NUMERIC, OTHER LONG, DISTRIBUTION VARCHAR2(30), CPU_COST NUMERIC, + IO_COST NUMERIC, TEMP_SPACE NUMERIC, ACCESS_PREDICATES VARCHAR2(4000), FILTER_PREDICATES VARCHAR2(4000), + PROJECTION VARCHAR2(4000), TIME NUMERIC, QBLOCK_NAME VARCHAR2(128), OTHER_XML CLOB + """ + + static func ownPlanTable(_ admin: OraclePluginDriver) async { + guard await createUser(admin, keeper, "CREATE SESSION, CREATE TABLE", quota: true) else { return } + for statement in [ + "CREATE TABLE \(keeper).PLAN_TABLE (\(utlxplanColumns))", + "INSERT INTO \(keeper).PLAN_TABLE (STATEMENT_ID, OPERATION) VALUES ('KEEP', 'the account''s own row')", + "COMMIT", + "CREATE TABLE \(keeper).TP_XP_WRITES (N NUMBER)", + """ + CREATE TRIGGER \(keeper).TP_XP_WRITES_SEEN AFTER INSERT ON \(keeper).PLAN_TABLE FOR EACH ROW + DECLARE PRAGMA AUTONOMOUS_TRANSACTION; + BEGIN INSERT INTO \(keeper).TP_XP_WRITES VALUES (1); COMMIT; END; + """, + ] { + if let failure = await run(admin, statement) { + print("SKIP: the check with a private PLAN_TABLE: \(message(failure))") + return + } + } + let owner: OraclePluginDriver + do { + owner = try await connect(user: keeper, password: plainPassword) + } catch { + expect(false, "a login as \(keeper)", message(error)) + return + } + defer { owner.disconnect() } + + do { + let explained = try await explain(owner, join(region: "north"), OracleExplainPlan.estimatedVariantId) + expect(relations(explained.plan) == joinedTables, "Estimated Plan works for an account with its own PLAN_TABLE", + "\(relations(explained.plan))") + } catch { + expect(false, "Estimated Plan works for an account with its own PLAN_TABLE", message(error)) + } + let writes = await scalar(admin, "SELECT TO_CHAR(COUNT(*)) FROM \(keeper).TP_XP_WRITES") ?? "nil" + print("INFO: Estimated Plan wrote \(writes) row(s) to \(keeper).PLAN_TABLE") + expect(writes == "0", "Estimated Plan writes nothing to the account's own PLAN_TABLE") + let kept = await values(admin, "SELECT STATEMENT_ID FROM \(keeper).PLAN_TABLE").map { $0.first ?? "" } + expect(kept == ["KEEP"], "the account's own PLAN_TABLE keeps exactly its own rows", "\(kept)") + } + static func cleanUp(_ admin: OraclePluginDriver) async { for table in ["TP_XP_ITEMS", "TP_XP_ORDERS", "TP_XP_CUSTOMERS", "TP_XP_LOG"] { await run(admin, "BEGIN EXECUTE IMMEDIATE 'DROP TABLE \(table) PURGE'; EXCEPTION WHEN OTHERS THEN NULL; END;") } - await run(admin, "BEGIN EXECUTE IMMEDIATE 'DROP USER \(plainUser) CASCADE'; EXCEPTION WHEN OTHERS THEN NULL; END;") + for user in [plainUser, reader, keeper, mallory] { + await run(admin, "BEGIN EXECUTE IMMEDIATE 'DROP USER \(user) CASCADE'; EXCEPTION WHEN OTHERS THEN NULL; END;") + } } } SWIFT diff --git a/apple/scripts/probes/check-postgresql-catalog-shadowing.sh b/apple/scripts/probes/check-postgresql-catalog-shadowing.sh new file mode 100755 index 0000000000..a6cf0dc41d --- /dev/null +++ b/apple/scripts/probes/check-postgresql-catalog-shadowing.sh @@ -0,0 +1,297 @@ +#!/usr/bin/env bash +# +# Checks that the PostgreSQL driver's own catalog statements never run a function or operator that another role +# planted in the schema being browsed (CVE-2018-1058's substitution, aimed at the client). +# +# PostgreSQL picks a candidate that takes the call's argument types exactly over a pg_catalog one reached through a +# cast, wherever the two sit on search_path. So a role that owns a schema can create `pg_total_relation_size(oid)`, +# `<>(oid, integer)` or `=(sql_identifier, sql_identifier)` there and have a reader's session run it. +# +# Builds the PostgreSQL plugin with SwiftPM, loads a fixture into a schema owned by an unprivileged role, and reads +# every catalog surface of it through the driver twice: pinned to that schema as the metadata pool pins a connection, +# and with a path that names the schema before pg_catalog, as a database owner's default search_path can. Then +# that role plants lookalikes: one operator for each operator name and pair of catalog types pg_catalog has no exact +# match for, one function for each pg_catalog overload the driver calls with one or two argument types swapped, and +# array_agg and max over each type. Each lookalike counts its calls in a sequence, which survives a rollback. The +# planted reads must print exactly what the clean ones did and nothing may count. +# +# Also checks PostGIS when the server has it: an enum named geometry with its own st_asewkt in the schema must not +# take the spatial rendering. +# +# Usage: scripts/probes/check-postgresql-catalog-shadowing.sh [host] [port] (PostgreSQL 11 or later) +# Environment: PGUSER (default postgres, a superuser: the reader whose privileges are at stake), PGPASSWORD, PSQL, +# TP_SHADOW_APPLE_ROOT (the apple/ folder whose driver to build, default this checkout's). +# Prints OK, or BUG with the lookalikes that ran and the lines that changed. +set -euo pipefail + +ROOT="${TP_SHADOW_APPLE_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}" +PROBES="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +HOST="${1:-127.0.0.1}" +PORT="${2:-5432}" +READER="${PGUSER:-postgres}" +OWNER="tablepro_shadow_owner" +OWNER_PASSWORD="tablepro_shadow_owner" +DATABASE="tablepro_shadow_check" +SCHEMA="evil" +LOG_SCHEMA="shadow_log" +PSQL="${PSQL:-$(command -v psql || true)}" +[[ -n "$PSQL" ]] || { echo "FAIL: psql not found; set PSQL" >&2; exit 2; } + +WORK="$(mktemp -d)" +cleanup() { + "$PSQL" -X -q -h "$HOST" -p "$PORT" -U "$READER" -d postgres \ + -c "DROP DATABASE IF EXISTS $DATABASE" -c "DROP ROLE IF EXISTS $OWNER" > /dev/null 2>&1 || true + rm -rf "$WORK" +} +trap cleanup EXIT + +as_reader() { "$PSQL" -X -q -v ON_ERROR_STOP=1 -h "$HOST" -p "$PORT" -U "$READER" "$@"; } +as_owner() { PGPASSWORD="$OWNER_PASSWORD" "$PSQL" -X -q -v ON_ERROR_STOP=1 -h "$HOST" -p "$PORT" -U "$OWNER" -d "$DATABASE"; } + +as_reader -d postgres -Atc 'SELECT 1' > /dev/null || { echo "FAIL: cannot connect to $HOST:$PORT as $READER" >&2; exit 2; } +echo "PostgreSQL $(as_reader -d postgres -Atc 'SHOW server_version') at $HOST:$PORT, driver from $ROOT" + +mkdir -p "$WORK/Sources/Probe" +for source in "$ROOT"/Plugins/PostgreSQLDriverPlugin/*.swift; do + ln -s "$source" "$WORK/Sources/Probe/$(basename "$source")" +done +for shared in PluginConnectTimeout PluginQueryTimeout PluginURLSessionTaskBox; do + ln -s "$ROOT/Plugins/Shared/$shared.swift" "$WORK/Sources/Probe/$shared.swift" +done +ln -s "$PROBES/postgresql-shadowing-probe.swift" "$WORK/Sources/Probe/Probe.swift" +ln -s "$ROOT/Plugins/PostgreSQLDriverPlugin/CLibPQ" "$WORK/CModule" +cat > "$WORK/Package.swift" < "$WORK/build.log" 2>&1); then + grep -E "error:" "$WORK/build.log" | head -20 >&2 + exit 2 +fi + +as_reader -d postgres -c "DROP DATABASE IF EXISTS $DATABASE" -c "DROP ROLE IF EXISTS $OWNER" > /dev/null +as_reader -d postgres -c "CREATE ROLE $OWNER LOGIN PASSWORD '$OWNER_PASSWORD'" -c "CREATE DATABASE $DATABASE" > /dev/null +as_reader -d "$DATABASE" -c "CREATE SCHEMA $SCHEMA AUTHORIZATION $OWNER" \ + -c "CREATE SCHEMA $LOG_SCHEMA AUTHORIZATION $OWNER" > /dev/null +HAS_POSTGIS="$(as_reader -d "$DATABASE" -Atc "SELECT count(*) FROM pg_available_extensions WHERE name = 'postgis'")" +if [[ "$HAS_POSTGIS" == "1" ]]; then + as_reader -d "$DATABASE" -c "CREATE EXTENSION postgis" > /dev/null + REAL_GEOMETRY="public.geometry" +else + echo "PostGIS is not installed on this server; the spatial check reads a text column instead" + REAL_GEOMETRY="text" +fi + +# The call log exists before the first read, so both reads list the same relations. +as_owner > /dev/null < 0); +CREATE TYPE $SCHEMA.pair AS (a integer, b text); +CREATE TYPE $SCHEMA.floatrange AS RANGE (subtype = float8); +CREATE TABLE $SCHEMA.customers (id serial PRIMARY KEY, email text UNIQUE, tier $SCHEMA.mood); +CREATE TABLE $SCHEMA.orders ( + id bigserial PRIMARY KEY, + customer_id integer REFERENCES $SCHEMA.customers (id), + qty $SCHEMA.positive, + status $SCHEMA.mood DEFAULT 'happy', + tags text[], + note text COLLATE "C", + total integer GENERATED ALWAYS AS (qty * 2) STORED, + CONSTRAINT qty_small CHECK (qty < 1000 AND status <> 'sad'::$SCHEMA.mood) +); +COMMENT ON TABLE $SCHEMA.orders IS 'Orders'; +COMMENT ON COLUMN $SCHEMA.orders.qty IS 'Quantity'; +CREATE INDEX orders_lower_note ON $SCHEMA.orders (lower(note)) WHERE qty > 5; +CREATE FUNCTION $SCHEMA.touch() RETURNS trigger LANGUAGE plpgsql AS \$\$BEGIN RETURN NEW; END\$\$; +CREATE TRIGGER orders_touch BEFORE INSERT OR UPDATE ON $SCHEMA.orders FOR EACH ROW EXECUTE FUNCTION $SCHEMA.touch(); +CREATE FUNCTION $SCHEMA.label(m $SCHEMA.mood, n integer DEFAULT 1) RETURNS text LANGUAGE plpgsql + AS \$\$BEGIN RETURN m::text; END\$\$; +CREATE VIEW $SCHEMA.order_view AS SELECT id, status FROM $SCHEMA.orders; +CREATE MATERIALIZED VIEW $SCHEMA.order_totals AS SELECT status, count(*) AS n FROM $SCHEMA.orders GROUP BY status; +CREATE TABLE $SCHEMA.events (id integer, at date) PARTITION BY RANGE (at); +CREATE TABLE $SCHEMA.events_2026 PARTITION OF $SCHEMA.events FOR VALUES FROM ('2026-01-01') TO ('2027-01-01'); +CREATE SEQUENCE $SCHEMA.invoice_seq; +CREATE TYPE $SCHEMA.geometry AS ENUM ('POINT(0 0)'); +CREATE TABLE $SCHEMA.places (id integer, location $SCHEMA.geometry, real_location $REAL_GEOMETRY); +INSERT INTO $SCHEMA.customers (email, tier) VALUES ('a@example.com', 'happy'); +INSERT INTO $SCHEMA.orders (customer_id, qty, note, tags) VALUES (1, 7, 'Hello', '{a,b}'); +INSERT INTO $SCHEMA.places VALUES (1, 'POINT(0 0)', 'SRID=4326;POINT(1 2)'); +SQL + +read_schema() { + "$WORK/.build/debug/Probe" "$HOST" "$PORT" "$READER" "$DATABASE" "$SCHEMA" pinned > "$1-pinned.txt" + "$WORK/.build/debug/Probe" "$HOST" "$PORT" "$READER" "$DATABASE" "$SCHEMA" explicit > "$1-explicit.txt" +} + +echo "Reading the clean schema..." +read_schema "$WORK/clean" + +echo "Planting lookalikes..." +as_owner > "$WORK/plant.log" <', '<', '>', '<=', '>=', '&', '|', '~~', '~~*', '!~~', '!~~*', '~', '~*', '!~', + '||', '+', '-', '*', '/', '@>', '<@', '&&']; + types regtype[] := ARRAY['oid', 'name', 'text', 'smallint', 'integer', 'bigint', '"char"', 'boolean', 'regclass', + 'regtype', 'regproc', 'regprocedure', 'regnamespace', 'regrole', 'oidvector', 'int2vector', + 'oid[]', 'smallint[]', 'text[]', 'name[]', 'aclitem[]', '"char"[]', 'real', + 'double precision', 'numeric', 'character varying', 'character', 'xid', + 'timestamp with time zone', 'interval', 'information_schema.sql_identifier', + 'information_schema.cardinal_number', 'information_schema.character_data', + 'information_schema.yes_or_no']::regtype[]; + swaps regtype[] := ARRAY['oid', 'name', 'text', 'smallint', 'integer', 'bigint', '"char"', 'regclass', + 'character varying', 'information_schema.sql_identifier']::regtype[]; + functions text[] := ARRAY['pg_total_relation_size', 'pg_table_size', 'pg_indexes_size', 'pg_relation_size', + 'obj_description', 'col_description', 'quote_ident', 'quote_literal', 'format_type', + 'pg_get_expr', 'pg_get_constraintdef', 'pg_get_triggerdef', 'pg_get_indexdef', + 'pg_get_viewdef', 'pg_get_functiondef', 'pg_get_function_identity_arguments', + 'pg_get_function_result', 'pg_get_serial_sequence', 'pg_get_userbyid', 'concat_ws', + 'has_schema_privilege', 'has_table_privilege', 'has_sequence_privilege', + 'has_any_column_privilege', 'has_column_privilege', 'has_database_privilege', + 'pg_has_role', 'current_setting', 'pg_size_pretty', 'pg_database_size', 'setval', + 'pg_type_is_visible', 'pg_is_other_temp_schema', 'pg_sequence_parameters', + 'pg_get_function_arg_default', 'array_lower', 'replace', 'lower', 'left', 'length']; + op text; l regtype; r regtype; t regtype; u regtype; + f record; args regtype[]; trial regtype[]; i int; j int; n int := 0; + returns text; +BEGIN + FOREACH op IN ARRAY operators LOOP + FOREACH l IN ARRAY types LOOP + FOREACH r IN ARRAY types LOOP + CONTINUE WHEN EXISTS (SELECT 1 FROM pg_catalog.pg_operator o + WHERE o.oprname = op AND o.oprleft = l AND o.oprright = r); + n := n + 1; + EXECUTE format('CREATE FUNCTION $SCHEMA.%I(%s, %s) RETURNS boolean LANGUAGE plpgsql AS %L', + 'op_' || n, l, r, + format('BEGIN PERFORM $LOG_SCHEMA.hit(%L); RETURN NULL; END', op || '(' || l || ', ' || r || ')')); + EXECUTE format('CREATE OPERATOR $SCHEMA.%s (LEFTARG = %s, RIGHTARG = %s, FUNCTION = $SCHEMA.%I)', + op, l, r, 'op_' || n); + -- Thousands of objects in one transaction run out of lock slots. + IF n % 500 = 0 THEN COMMIT; END IF; + END LOOP; + END LOOP; + END LOOP; + + FOR f IN + SELECT p.proname, + ARRAY(SELECT a.t::pg_catalog.regtype + FROM pg_catalog.unnest(p.proargtypes::pg_catalog.oid[]) WITH ORDINALITY AS a(t, n) + ORDER BY a.n) AS args, + p.prorettype::pg_catalog.regtype AS rettype, + p.proretset, rt.typtype + FROM pg_catalog.pg_proc p + JOIN pg_catalog.pg_type rt ON rt.oid = p.prorettype + WHERE p.pronamespace = 'pg_catalog'::pg_catalog.regnamespace + AND p.proname = ANY (functions) + AND p.prokind = 'f' + AND p.pronargs BETWEEN 1 AND 3 + LOOP + returns := CASE WHEN f.typtype = 'p' THEN 'text' ELSE f.rettype::text END; + FOR i IN 1 .. pg_catalog.array_length(f.args, 1) LOOP + FOREACH t IN ARRAY swaps LOOP + FOR j IN i .. pg_catalog.array_length(f.args, 1) LOOP + FOREACH u IN ARRAY CASE WHEN j = i THEN ARRAY[t] ELSE swaps END LOOP + trial := f.args; + trial[i] := t; + trial[j] := u; + CONTINUE WHEN EXISTS ( + SELECT 1 FROM pg_catalog.pg_proc q + WHERE q.proname = f.proname + AND pg_catalog.array_to_string(q.proargtypes::pg_catalog.oid[], ' ') + = pg_catalog.array_to_string(trial::pg_catalog.oid[], ' ')); + BEGIN + EXECUTE format('CREATE FUNCTION $SCHEMA.%I(%s) RETURNS %s%s LANGUAGE plpgsql AS %L', + f.proname, pg_catalog.array_to_string(trial, ', '), + CASE WHEN f.proretset THEN 'SETOF ' ELSE '' END, returns, + format('BEGIN PERFORM $LOG_SCHEMA.hit(%L); RETURN%s; END', + f.proname || '(' || pg_catalog.array_to_string(trial, ', ') || ')', + CASE WHEN f.proretset THEN '' ELSE ' NULL' END)); + EXCEPTION WHEN OTHERS THEN + NULL; + END; + n := n + 1; + IF n % 500 = 0 THEN COMMIT; END IF; + END LOOP; + END LOOP; + END LOOP; + END LOOP; + END LOOP; + + FOREACH t IN ARRAY types LOOP + n := n + 1; + EXECUTE format('CREATE FUNCTION $SCHEMA.%I(text, %s) RETURNS text LANGUAGE plpgsql AS %L', + 'agg_' || n, t, format('BEGIN PERFORM $LOG_SCHEMA.hit(%L); RETURN NULL; END', 'aggregate(' || t || ')')); + IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_proc q + WHERE q.proname = 'array_agg' AND q.pronargs = 1 AND q.proargtypes[0] = t) THEN + EXECUTE format('CREATE AGGREGATE $SCHEMA.array_agg(%s) (SFUNC = $SCHEMA.%I, STYPE = text)', t, 'agg_' || n); + END IF; + IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_proc q + WHERE q.proname = 'max' AND q.pronargs = 1 AND q.proargtypes[0] = t) THEN + EXECUTE format('CREATE AGGREGATE $SCHEMA.max(%s) (SFUNC = $SCHEMA.%I, STYPE = text)', t, 'agg_' || n); + END IF; + END LOOP; +END +\$plant\$; +CALL $LOG_SCHEMA.plant(); +SELECT pg_catalog.count(*) || ' lookalike functions and operators' FROM pg_catalog.pg_proc +WHERE pronamespace = '$SCHEMA'::pg_catalog.regnamespace; +SQL +grep -i "lookalike" "$WORK/plant.log" | tr -d ' ' | sed 's/lookalike/ lookalike /' || true + +echo "Reading the planted schema..." +read_schema "$WORK/planted" + +HITS="$(as_reader -d "$DATABASE" -Atc "SELECT CASE WHEN is_called THEN last_value ELSE 0 END FROM $LOG_SCHEMA.hits")" +CALLS="$(as_reader -d "$DATABASE" -Atc "SELECT signature, pg_catalog.count(*) FROM $LOG_SCHEMA.calls GROUP BY 1 ORDER BY 1")" +CHANGED="$(diff "$WORK/clean-pinned.txt" "$WORK/planted-pinned.txt"; diff "$WORK/clean-explicit.txt" "$WORK/planted-explicit.txt")" || true + +grep -h '^error|' "$WORK/clean-pinned.txt" "$WORK/clean-explicit.txt" | sed 's/^/clean read failed: /' || true +echo "Lookalike calls: $HITS" +[[ -z "$CALLS" ]] || echo "$CALLS" | sed 's/^/ ran: /' +[[ -z "$CHANGED" ]] || { echo "Lines that changed:"; echo "$CHANGED" | head -60; } +if [[ "$HITS" == "0" && -z "$CHANGED" ]] && ! grep -q '^error|' "$WORK/clean-pinned.txt" "$WORK/clean-explicit.txt"; then + echo "OK" +else + echo "BUG" + exit 1 +fi diff --git a/apple/scripts/probes/postgresql-shadowing-probe.swift b/apple/scripts/probes/postgresql-shadowing-probe.swift new file mode 100644 index 0000000000..558c03afd6 --- /dev/null +++ b/apple/scripts/probes/postgresql-shadowing-probe.swift @@ -0,0 +1,208 @@ +import Foundation +import TableProPluginKit + +// Reads every catalog surface of one schema through the real driver and prints one canonical line per answer. +// `pinned` switches to the schema the way the metadata pool pins a connection; `explicit` sets a path that names the +// schema before pg_catalog, as a database owner's default can. check-postgresql-catalog-shadowing.sh diffs the lines a +// clean schema gives against those the same schema gives once its owner has planted lookalikes. +@main +enum ShadowingProbe { + static func main() async { + let arguments = CommandLine.arguments + guard arguments.count >= 7 else { + FileHandle.standardError.write(Data("usage: probe host port user database schema pinned|explicit\n".utf8)) + exit(2) + } + let schema = arguments[5] + let driver = PostgreSQLPluginDriver(config: DriverConnectionConfig( + host: arguments[1], + port: Int(arguments[2]) ?? 5_432, + username: arguments[3], + password: ProcessInfo.processInfo.environment["PGPASSWORD"] ?? "", + database: arguments[4] + )) + do { + try await driver.connect() + if arguments[6] == "explicit" { + _ = try await driver.execute(query: "SET search_path = \"\(schema)\", pg_catalog") + } else { + try await driver.switchSchema(to: schema) + } + } catch { + print("error|connect|\(error)") + exit(2) + } + await read(driver, schema: schema, database: arguments[4]) + await readTypeCreatedAfterConnect(driver, schema: schema) + driver.disconnect() + } + + // The lookalikes are routines in the same schema, so the listings keep to what the fixture created. + static let fixtureRoutines: Set = ["touch", "label"] + + // A type the connect-time probes never saw sends the type-name lookup, and CREATE TYPE the enum refresh, both on + // the session's own path. + static func readTypeCreatedAfterConnect(_ driver: PostgreSQLPluginDriver, schema: String) async { + let type = "\"\(schema)\".late_pair" + await attempt("typeCreatedAfterConnect") { + _ = try await driver.execute(query: "CREATE TYPE \(type) AS (a pg_catalog.int4)") + let result = try await driver.execute(query: "SELECT ROW(1)::\(type) AS pair") + line(["late-type", result.columnTypeNames.first, result.rows.first?.first?.asText]) + } + _ = try? await driver.execute(query: "DROP TYPE IF EXISTS \(type)") + } + + static func line(_ fields: [String?]) { + print(fields.map { ($0 ?? "-").replacingOccurrences(of: "\n", with: "\\n") }.joined(separator: "|")) + } + + static func attempt(_ label: String, _ body: () async throws -> Void) async { + do { + try await body() + } catch { + line(["error", label, "\(error)"]) + } + } + + static func column(_ table: String, _ column: PluginColumnInfo) { + line([ + "column", table, column.name, column.dataType, column.isNullable ? "null" : "not-null", + column.isPrimaryKey ? "pk" : "-", column.defaultValue, column.collation, column.comment, + column.isGenerated ? "generated" : "-", column.generationExpression, + column.allowedValues?.joined(separator: ","), column.ddlSpelling, column.ddlDefault + ]) + } + + static func read(_ driver: PostgreSQLPluginDriver, schema: String, database: String) async { + await attempt("fetchTables") { + for table in try await driver.fetchTables(schema: schema).sorted(by: { $0.name < $1.name }) { + line(["table", table.name, table.type, table.comment]) + } + } + await attempt("fetchTablesInSchemas") { + let tables = try await driver.fetchTables(inSchemas: [schema]) ?? [] + line(["tables-in-schemas", "\(tables.count)"]) + } + await attempt("fetchSchemas") { + line(["schemas", try await driver.fetchSchemas().contains(schema) ? "lists" : "misses"]) + } + await attempt("fetchDatabases") { + line(["databases", try await driver.fetchDatabases().contains(database) ? "lists" : "misses"]) + } + await attempt("fetchDatabaseMetadata") { + let metadata = try await driver.fetchDatabaseMetadata(database) + line(["database-metadata", metadata.tableCount.map(String.init), metadata.sizeBytes == nil ? "nil" : "sized"]) + } + await attempt("fetchColumns") { + for table in ["orders", "order_view", "order_totals", "places"] { + for item in try await driver.fetchColumns(table: table, schema: schema) { column(table, item) } + } + } + await attempt("fetchAllColumns") { + for (table, columns) in try await driver.fetchAllColumns(schema: schema).sorted(by: { $0.key < $1.key }) { + line(["all-columns", table, columns.map(\.name).joined(separator: ","), columns.map(\.dataType).joined(separator: ",")]) + } + } + await attempt("fetchIndexes") { + for index in try await driver.fetchIndexes(table: "orders", schema: schema) { + line(["index", index.name, index.columns.joined(separator: ","), index.type, index.whereClause]) + } + } + await attempt("fetchAllIndexes") { + for (table, indexes) in try await driver.fetchAllIndexes(schema: schema).sorted(by: { $0.key < $1.key }) { + line(["all-indexes", table, indexes.map(\.name).sorted().joined(separator: ",")]) + } + } + await attempt("fetchIndexDDL") { + for ddl in try await driver.fetchIndexDDL(table: "orders", schema: schema) { line(["index-ddl", ddl]) } + } + await attempt("fetchForeignKeys") { + for key in try await driver.fetchForeignKeys(table: "orders", schema: schema) { + line(["foreign-key", key.name, key.column, key.referencedTable, key.referencedColumn]) + } + line(["all-foreign-keys", "\(try await driver.fetchAllForeignKeys(schema: schema).count)"]) + } + await attempt("fetchCheckConstraints") { + for check in try await driver.fetchCheckConstraints(table: "orders", schema: schema) { + line(["check", check.name, check.expression, check.columns.joined(separator: ",")]) + } + } + await attempt("fetchTriggers") { + for trigger in try await driver.fetchTriggers(table: "orders", schema: schema) { + line(["trigger", trigger.name, trigger.timing, trigger.event, trigger.definition]) + line(["trigger-ddl", try await driver.fetchTriggerDDL(trigger)]) + } + line(["all-triggers", "\(try await driver.fetchAllTriggers(schema: schema).count)"]) + line(["trigger-definition", try await driver.fetchTriggerDefinition(name: "orders_touch", table: "orders", schema: schema)]) + } + await attempt("fetchTableDDL") { + for table in ["orders", "order_view"] { line(["ddl", table, try await driver.fetchTableDDL(table: table, schema: schema)]) } + } + await attempt("fetchViewDefinition") { + line(["view", try await driver.fetchViewDefinition(view: "order_totals", schema: schema)]) + } + await attempt("fetchTableMetadata") { + let metadata = try await driver.fetchTableMetadata(table: "orders", schema: schema) + line(["metadata", metadata.comment, metadata.totalSize == nil ? "nil" : "sized"]) + line(["all-metadata", "\(try await driver.fetchAllTableMetadata(schema: schema).count)"]) + line(["approximate-rows", try await driver.fetchApproximateRowCount(table: "orders", schema: schema).map(String.init)]) + line(["relation-comment", try await driver.fetchRelationComment(table: "orders", schema: schema)]) + } + await attempt("allTablesMetadataSQL") { + guard let sql = driver.allTablesMetadataSQL(schema: schema) else { return } + for row in try await driver.execute(query: sql).rows { line(["all-tables", row[safe: 1]?.asText, row[safe: 7]?.asText]) } + } + await attempt("fetchUserDefinedTypes") { + for type in try await driver.fetchUserDefinedTypes(schema: schema).sorted(by: { $0.name < $1.name }) { + line(["type", type.name, type.definition]) + line(["type-reload", try await driver.fetchUserDefinedType(type).definition]) + } + } + await attempt("fetchRoutines") { + let routines = try await driver.fetchRoutines(schema: schema).filter { fixtureRoutines.contains($0.name) } + for routine in routines.sorted(by: { $0.name < $1.name }) { + line(["routine", routine.name, routine.argumentSignature, routine.returnType]) + line(["routine-ddl", try await driver.fetchRoutineDDL(routine)]) + } + let targets = try await driver.debugTargets(schema: schema).filter { fixtureRoutines.contains($0.name) } + line(["debug-targets", "\(targets.count)"]) + } + await attempt("fetchSequences") { + for sequence in try await driver.fetchSequences(schema: schema) { line(["sequence", sequence.name, sequence.ddl]) } + for sequence in try await driver.fetchDependentSequences(table: "orders", schema: schema) { + line(["dependent-sequence", sequence.name, sequence.ddl]) + } + } + await attempt("fetchDependentTypes") { + for type in try await driver.fetchDependentTypes(table: "orders", schema: schema) { + line(["dependent-type", type.name, type.labels.joined(separator: ",")]) + } + } + await attempt("fetchPartitions") { + for partition in try await driver.fetchPartitionDetails(table: "events", schema: schema) { + line(["partition", partition.name, partition.bound]) + } + } + await attempt("fetchSchemaDetails") { + let details = try await driver.fetchSchemaDetails(name: schema) + line(["schema-details", details?.owner, "\(details?.grants.count ?? -1)"]) + } + await attempt("fetchPrincipals") { + line(["principals", try await driver.fetchPrincipals().isEmpty ? "none" : "listed"]) + } + await attempt("generateColumnReorderPlan") { + let columns = try await driver.fetchColumns(table: "customers", schema: schema) + let plan = try await driver.generateColumnReorderPlan( + table: "customers", + schema: schema, + columns: [], + desiredOrder: columns.map(\.name).reversed() + ) + for statement in plan?.statements ?? [] { line(["reorder", statement]) } + } + await attempt("spatialRendering") { + let result = try await driver.execute(query: "SELECT id, location, real_location FROM \"\(schema)\".places ORDER BY id") + for row in result.rows { line(["places", row.map { $0.asText ?? "-" }.joined(separator: ";")]) } + } + } +} From 9b80874ffbe60b97c5c1daef7055520eaf16a398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sun, 1 Nov 2026 00:44:45 +0700 Subject: [PATCH 49/85] Merge commit from fork * fix(security): harden the Git commands TablePro runs on its own Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-FRDSB33T. Advisory: GHSA-rhxc-5m3h-r5ch * fix(security): pin PostgreSQL catalog reads to pg_catalog and SYS-qualify Oracle Explain Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CDER3MTT. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CRPAPF23. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): write Oracle Estimated Plan to SYS.PLAN_TABLE$ Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * test(security): expect the pg_catalog-qualified PostgreSQL cancel statement --- apple/TablePro/Core/Git/GitClient.swift | 17 ++- apple/TablePro/Core/Git/GitCommand.swift | 44 ++++++- .../TablePro/Core/Git/GitOutputParsers.swift | 19 +++ .../TablePro/Core/Git/GitProcessRunner.swift | 8 +- .../TablePro/Resources/Localizable.xcstrings | 3 + .../Core/Git/GitIntegrationTests.swift | 116 +++++++++++++++++- .../Core/Git/GitOutputParserTests.swift | 63 ++++++++++ .../changelog.d/+ghsa-rhxc-5m3h-r5ch.fixed.md | 1 + .../+ghsa-rhxc-5m3h-r5ch.security.md | 1 + 9 files changed, 263 insertions(+), 9 deletions(-) create mode 100644 apple/changelog.d/+ghsa-rhxc-5m3h-r5ch.fixed.md create mode 100644 apple/changelog.d/+ghsa-rhxc-5m3h-r5ch.security.md diff --git a/apple/TablePro/Core/Git/GitClient.swift b/apple/TablePro/Core/Git/GitClient.swift index 652a6b0b0a..74bd99ead1 100644 --- a/apple/TablePro/Core/Git/GitClient.swift +++ b/apple/TablePro/Core/Git/GitClient.swift @@ -55,11 +55,26 @@ internal struct GitClient: Sendable { } func status(in directory: URL, pathspec: String = ".") async throws -> [GitStatusRecord] { - let result = try await runner.run(.status(in: directory, pathspec: pathspec)) + let filters = try await filterDrivers(in: directory) + let result = try await runner.run(.status(in: directory, pathspec: pathspec, disablingFilters: filters)) guard result.succeeded else { throw failure(result) } return GitStatusParser.parse(result.standardOutput) } + // Every scope: the user's own Git LFS driver runs commands the repository's configuration names. + private func filterDrivers(in directory: URL) async throws -> [String] { + let result = try await runner.run(.configuration(in: directory)) + guard result.succeeded else { throw failure(result) } + guard result.isComplete else { throw GitProcessError.timedOut } + guard let drivers = GitFilterDriverParser.parse(result.standardOutput) else { + throw GitCommandFailure( + exitCode: result.exitCode, + message: String(localized: "This repository defines a Git filter that TablePro cannot switch off.") + ) + } + return drivers + } + func trackedFiles(in directory: URL) async throws -> [String] { let result = try await runner.run(.trackedFiles(in: directory)) guard result.succeeded else { throw failure(result) } diff --git a/apple/TablePro/Core/Git/GitCommand.swift b/apple/TablePro/Core/Git/GitCommand.swift index d0ec0949b0..129dcecc90 100644 --- a/apple/TablePro/Core/Git/GitCommand.swift +++ b/apple/TablePro/Core/Git/GitCommand.swift @@ -16,6 +16,7 @@ internal struct GitCommand: Equatable, Sendable { let workingDirectory: URL var access: Access = .read var timeout: TimeInterval? + var disabledFilters: [String] = [] static let writeTimeout: TimeInterval = 300 @@ -35,9 +36,17 @@ internal struct GitCommand: Equatable, Sendable { "GIT_OPTIONAL_LOCKS": "0", "GIT_TERMINAL_PROMPT": "0", "GIT_PAGER": "cat", + ] + + // Git before 2.44 ignores GIT_NO_LAZY_FETCH; an empty allow list stops the transport a lazy fetch would use. + static let readOnlyEnvironment: [String: String] = [ "GIT_NO_LAZY_FETCH": "1", + "GIT_ALLOW_PROTOCOL": "", ] + static let emptyValueVariable = "TABLEPRO_GIT_EMPTY_VALUE" + static let disabledFilterVariables = ["clean", "process", "required"] + static let inheritedRepositoryVariables: Set = [ "GIT_DIR", "GIT_WORK_TREE", @@ -49,17 +58,33 @@ internal struct GitCommand: Equatable, Sendable { "GIT_CEILING_DIRECTORIES", "GIT_CONFIG_PARAMETERS", "GIT_CONFIG_COUNT", + // Points `git config` alone at another file, so the filter listing would miss the repository's drivers. + "GIT_CONFIG", "GIT_EXTERNAL_DIFF", ] static let historyFormat = "%x1e%H%x1f%an%x1f%aI%x1f%s" var processArguments: [String] { - Self.hardeningArguments + (access == .read ? Self.readOnlyArguments : []) + arguments + Self.hardeningArguments + (access == .read ? Self.readOnlyArguments : []) + filterArguments + arguments + } + + // `--config-env` splits at the last `=`, unlike `-c`, and git before 2.31 stops on it rather than run the filter. + private var filterArguments: [String] { + disabledFilters.flatMap { driver in + Self.disabledFilterVariables.map { "--config-env=filter.\(driver).\($0)=\(Self.emptyValueVariable)" } + } } func environment(base: [String: String]) -> [String: String] { - Self.environment(base: access == .write ? CLIToolEnvironment.augmented(base) : base) + var environment = Self.environment(base: access == .write ? CLIToolEnvironment.augmented(base) : base) + if access == .read { + environment.merge(Self.readOnlyEnvironment) { _, override in override } + } + if !disabledFilters.isEmpty { + environment[Self.emptyValueVariable] = "" + } + return environment } static func environment(base: [String: String]) -> [String: String] { @@ -79,10 +104,19 @@ internal struct GitCommand: Equatable, Sendable { GitCommand(arguments: ["rev-parse", "--verify", "--quiet", "HEAD"], workingDirectory: directory) } - static func status(in directory: URL, pathspec: String = ".") -> GitCommand { + static func configuration(in directory: URL) -> GitCommand { + GitCommand(arguments: ["config", "--list", "--name-only", "-z"], workingDirectory: directory) + } + + // Status pipes a stale file through its filter driver and runs its own status inside a nested repository. + static func status(in directory: URL, pathspec: String = ".", disablingFilters filters: [String] = []) -> GitCommand { GitCommand( - arguments: ["status", "--porcelain=v2", "-z", "--untracked-files=all", "--find-renames", "--", pathspec], - workingDirectory: directory + arguments: [ + "status", "--porcelain=v2", "-z", "--untracked-files=all", "--find-renames", + "--ignore-submodules=dirty", "--", pathspec, + ], + workingDirectory: directory, + disabledFilters: filters ) } diff --git a/apple/TablePro/Core/Git/GitOutputParsers.swift b/apple/TablePro/Core/Git/GitOutputParsers.swift index 4d512dcc28..ef006de542 100644 --- a/apple/TablePro/Core/Git/GitOutputParsers.swift +++ b/apple/TablePro/Core/Git/GitOutputParsers.swift @@ -74,6 +74,25 @@ internal enum GitStatusParser { } } +internal enum GitFilterDriverParser { + private static let section = Array("filter.".utf8) + + // Bytes, since a dot and a combining mark are one Character; nil for a name that may not reach git intact. + static func parse(_ data: Data) -> [String]? { + var drivers: [String] = [] + for key in data.split(separator: 0) where key.starts(with: section) { + let nameStart = key.startIndex + section.count + guard let variableDot = key.lastIndex(of: UInt8(ascii: ".")), variableDot >= nameStart else { continue } + let name = key[nameStart.. GitRepositoryInfo? { let lines = (String(bytes: data, encoding: .utf8) ?? "") diff --git a/apple/TablePro/Core/Git/GitProcessRunner.swift b/apple/TablePro/Core/Git/GitProcessRunner.swift index fb64a89f7a..86c545f959 100644 --- a/apple/TablePro/Core/Git/GitProcessRunner.swift +++ b/apple/TablePro/Core/Git/GitProcessRunner.swift @@ -10,6 +10,8 @@ internal struct GitProcessResult: Sendable { let exitCode: Int32 let standardOutput: Data let standardError: Data + // False when a child of git still held the output open after the grace period, so more may have followed. + var isComplete = true var succeeded: Bool { exitCode == 0 } @@ -98,7 +100,8 @@ internal struct GitProcessRunner: Sendable { throw GitProcessError.timedOut } // A hook's background job or an SSH ControlPersist master keeps the pipes open after git exits. - if readers.wait(timeout: .now() + Self.pipeDrainGrace) == .timedOut { + let drained = readers.wait(timeout: .now() + Self.pipeDrainGrace) == .success + if !drained { Self.logger.debug("git \(command.arguments.first ?? "", privacy: .public) left its output open in a child process") } @@ -108,7 +111,8 @@ internal struct GitProcessRunner: Sendable { return GitProcessResult( exitCode: process.terminationStatus, standardOutput: output.data, - standardError: errors.data + standardError: errors.data, + isComplete: drained ) } diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 61ada1d919..18e613f04c 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -198350,6 +198350,9 @@ } } } + }, + "This repository defines a Git filter that TablePro cannot switch off." : { + }, "This repository has no commits yet." : { "localizations" : { diff --git a/apple/TableProTests/Core/Git/GitIntegrationTests.swift b/apple/TableProTests/Core/Git/GitIntegrationTests.swift index d55b13410b..d395bccabc 100644 --- a/apple/TableProTests/Core/Git/GitIntegrationTests.swift +++ b/apple/TableProTests/Core/Git/GitIntegrationTests.swift @@ -46,13 +46,16 @@ private struct ScratchRepository { try String(contentsOf: url(relativePath), encoding: .utf8) } + // Setup is the test's own Terminal git, which clones and pushes; the app's reads reach no remote. @discardableResult func git(_ arguments: String...) async throws -> GitProcessResult { let identity = [ "-c", "user.name=Ann Author", "-c", "user.email=ann@example.com", "-c", "commit.gpgsign=false", "-c", "init.defaultBranch=main", ] - let result = try await client.runner.run(GitCommand(arguments: identity + arguments, workingDirectory: root)) + let result = try await client.runner.run( + GitCommand(arguments: identity + arguments, workingDirectory: root, access: .write) + ) guard result.succeeded else { throw GitCommandFailure(exitCode: result.exitCode, message: result.errorMessage) } return result } @@ -138,6 +141,93 @@ struct GitIntegrationTests { #expect(before[.modificationDate] as? Date == after[.modificationDate] as? Date) } + @Test("Status runs no filter command the repository's own configuration names") + func statusRunsNoRepositoryFilter() async throws { + let repo = try ScratchRepository(client: client) + defer { repo.remove() } + let marker = repo.url("filter-ran") + try await repo.git("init", "-q") + try repo.write("SELECT 1;\n", to: "a.sql") + try repo.write("*.sql filter=tidy\n", to: ".gitattributes") + try await repo.git("add", "-A") + try await repo.git("commit", "-qm", "first") + try await repo.git("config", "filter.tidy.clean", "touch '\(marker.path)'; cat") + try repo.write("SELECT 1;\n", to: "a.sql") + + _ = try await client.status(in: repo.root) + + #expect(!FileManager.default.fileExists(atPath: marker.path)) + } + + @Test( + "Status switches off a clean or process driver whatever it is called, and still sees the edit", + arguments: ["clean", "process"], ["tidy", "a=b.c", "Ti\"dy\\", ""] + ) + func statusRunsNoFilterOfAnyName(variable: String, driver: String) async throws { + let repo = try ScratchRepository(client: client) + defer { repo.remove() } + let marker = repo.url("filter-ran") + try await repo.git("init", "-q") + try repo.write("SELECT 1;\n", to: "a.sql") + try await repo.git("add", "-A") + try await repo.git("commit", "-qm", "first") + try repo.write("* filter=\(driver)\n", to: ".git/info/attributes") + try await repo.git("config", "filter.\(driver).\(variable)", "touch '\(marker.path)'; cat") + try await repo.git("config", "filter.\(driver).required", "true") + try repo.write("SELECT 2;\n", to: "a.sql") + + let records = try await client.status(in: repo.root) + + #expect(records.map(\.path) == ["a.sql"]) + #expect(!FileManager.default.fileExists(atPath: marker.path)) + } + + @Test("Status refuses a repository whose filter has a name it cannot pass back to git") + func statusRefusesUnreadableFilterName() async throws { + let repo = try ScratchRepository(client: client) + defer { repo.remove() } + let marker = repo.url("filter-ran") + try await repo.git("init", "-q") + try repo.write("SELECT 1;\n", to: "a.sql") + try await repo.git("add", "-A") + try await repo.git("commit", "-qm", "first") + let name = Data([0xFF, 0xFE]) + try (Data("* filter=".utf8) + name + Data("\n".utf8)).write(to: repo.url(".git/info/attributes")) + let section = Data("[filter \"".utf8) + name + Data("\"]\n\tclean = \"touch '\(marker.path)'; cat\"\n".utf8) + try (Data(contentsOf: repo.url(".git/config")) + section).write(to: repo.url(".git/config")) + try repo.write("SELECT 2;\n", to: "a.sql") + + await #expect(throws: GitCommandFailure.self) { try await client.status(in: repo.root) } + #expect(!FileManager.default.fileExists(atPath: marker.path)) + } + + @Test("Status runs no filter of a nested repository, and still reports it at another commit") + func statusRunsNoNestedRepositoryFilter() async throws { + let repo = try ScratchRepository(client: client) + defer { repo.remove() } + let marker = repo.url("filter-ran") + let nested = ScratchRepository(client: client, root: repo.url("vendor")) + try FileManager.default.createDirectory(at: nested.root, withIntermediateDirectories: true) + try await nested.git("init", "-q") + try nested.write("SELECT 1;\n", to: "b.sql") + try await nested.git("add", "-A") + try await nested.git("commit", "-qm", "first") + try await nested.git("commit", "-qm", "second", "--allow-empty") + try await repo.git("init", "-q") + try await repo.git("add", "-A") + try await repo.git("commit", "-qm", "first") + try nested.write("* filter=tidy\n", to: ".git/info/attributes") + try await nested.git("config", "filter.tidy.clean", "touch '\(marker.path)'; cat") + try nested.write("SELECT 2;\n", to: "b.sql") + + #expect(try await client.status(in: repo.root).isEmpty) + #expect(!FileManager.default.fileExists(atPath: marker.path)) + + try await nested.git("update-ref", "HEAD", "HEAD~1") + #expect(try await client.status(in: repo.root).map(\.path) == ["vendor"]) + #expect(!FileManager.default.fileExists(atPath: marker.path)) + } + @Test("A folder outside any repository is not one") func notARepository() async throws { let repo = try ScratchRepository(client: client) @@ -518,6 +608,30 @@ struct GitIntegrationTests { #expect(remoteLog == "My change") } + @Test("Pull fast-forwards a partial clone, fetching the files the new commit needs as Terminal git does") + func pullIntoPartialClone() async throws { + let workspace = try ScratchRepository(client: client) + defer { workspace.remove() } + try await workspace.git("init", "-q", "--bare", "remote.git") + try await workspace.git("--git-dir=remote.git", "config", "uploadpack.allowFilter", "true") + try await workspace.git("clone", "-q", "remote.git", "teammate") + let teammate = ScratchRepository(client: client, root: workspace.url("teammate")) + try await teammate.configureForWrites() + try teammate.write("SELECT 1;\n", to: "report.sql") + try await teammate.git("add", "-A") + try await teammate.git("commit", "-qm", "First") + try await teammate.git("push", "-q", "origin", "HEAD:main") + try await workspace.git("clone", "-q", "--filter=blob:none", workspace.url("remote.git").absoluteString, "mine") + let mine = ScratchRepository(client: client, root: workspace.url("mine")) + try teammate.write("SELECT 2;\n", to: "report.sql") + try await teammate.git("commit", "-qam", "Second") + try await teammate.git("push", "-q", "origin", "HEAD:main") + + try await client.pull(in: mine.root) + + #expect(try mine.read("report.sql") == "SELECT 2;\n") + } + @Test("A pull that cannot fast-forward stops, says so, and leaves the branch where it was") func divergedPullStops() async throws { let workspace = try ScratchRepository(client: client) diff --git a/apple/TableProTests/Core/Git/GitOutputParserTests.swift b/apple/TableProTests/Core/Git/GitOutputParserTests.swift index 85233d82cf..249901439b 100644 --- a/apple/TableProTests/Core/Git/GitOutputParserTests.swift +++ b/apple/TableProTests/Core/Git/GitOutputParserTests.swift @@ -233,6 +233,7 @@ struct GitCommandHardeningTests { let commands: [GitCommand] = [ .repositoryInfo(in: directory), .verifyHead(in: directory), + .configuration(in: directory), .status(in: directory), .fileHistory(of: file, limit: 10), .blob(revision: "HEAD", path: "a.sql", in: directory), @@ -254,6 +255,66 @@ struct GitCommandHardeningTests { #expect(GitCommand.blob(revision: "HEAD", path: "a.sql", in: directory).arguments.contains("--end-of-options")) } + @Test("Every filter driver the configuration lists counts, whatever its scope or name") + func filterDrivers() { + let keys = [ + "core.bare", "filter.lfs.process", "filter.lfs.required", "filter.tidy.clean", "filter.smudge", + "filter.a=b.c.process", "filter.a.b.clean", "filter..clean", "filter.Ti\"dy\\.clean", "filter.tidy.required", + ] + let listing = Data((keys.joined(separator: "\0") + "\0").utf8) + #expect(GitFilterDriverParser.parse(listing) == ["lfs", "tidy", "a=b.c", "a.b", "", "Ti\"dy\\"]) + #expect(GitFilterDriverParser.parse(Data()) == []) + #expect(GitFilterDriverParser.parse(Data("core.bare\0filter.\u{301}x.clean\0".utf8)) == nil) + #expect(GitFilterDriverParser.parse(Data("filter.".utf8) + Data([0xFF]) + Data(".clean\0".utf8)) == nil) + } + + @Test("Status switches off each listed driver for its own process and stays out of nested repositories") + func statusDisablesFilters() throws { + let directory = URL(fileURLWithPath: "/tmp/repo") + let plain = GitCommand.status(in: directory) + #expect(plain.arguments.contains("--ignore-submodules=dirty")) + #expect(!plain.processArguments.contains { $0.hasPrefix("--config-env") }) + #expect(plain.environment(base: [:])[GitCommand.emptyValueVariable] == nil) + + let command = GitCommand.status(in: directory, disablingFilters: ["a=b", ""]) + let overrides = command.processArguments.filter { $0.hasPrefix("--config-env") } + #expect(overrides == [ + "--config-env=filter.a=b.clean=TABLEPRO_GIT_EMPTY_VALUE", + "--config-env=filter.a=b.process=TABLEPRO_GIT_EMPTY_VALUE", + "--config-env=filter.a=b.required=TABLEPRO_GIT_EMPTY_VALUE", + "--config-env=filter..clean=TABLEPRO_GIT_EMPTY_VALUE", + "--config-env=filter..process=TABLEPRO_GIT_EMPTY_VALUE", + "--config-env=filter..required=TABLEPRO_GIT_EMPTY_VALUE", + ]) + let lastOverride = try #require(command.processArguments.lastIndex { $0.hasPrefix("--config-env") }) + let subcommand = try #require(command.processArguments.firstIndex(of: "status")) + #expect(subcommand > lastOverride) + #expect(command.environment(base: [GitCommand.emptyValueVariable: "x"])[GitCommand.emptyValueVariable] == "") + } + + @Test("A read reaches no remote, and a write fetches what it needs as Terminal git does") + func readsReachNoRemote() { + let directory = URL(fileURLWithPath: "/tmp/repo") + let base = ["PATH": "/usr/bin", "GIT_ALLOW_PROTOCOL": "file:ssh", "GIT_NO_LAZY_FETCH": "0"] + let reads: [GitCommand] = [ + .status(in: directory), + .configuration(in: directory), + .blob(revision: "HEAD", path: "a.sql", in: directory), + .fileHistory(of: directory.appendingPathComponent("a.sql"), limit: 10), + .changedPaths(from: "a", to: "b", in: directory), + ] + for command in reads { + let environment = command.environment(base: base) + #expect(environment["GIT_ALLOW_PROTOCOL"] == "") + #expect(environment["GIT_NO_LAZY_FETCH"] == "1") + } + for command in [GitCommand.pull(in: directory), .push(in: directory), .commit(["a.sql"], message: "Fix", in: directory)] { + let environment = command.environment(base: ["PATH": "/usr/bin"]) + #expect(environment["GIT_ALLOW_PROTOCOL"] == nil) + #expect(environment["GIT_NO_LAZY_FETCH"] == nil) + } + } + @Test("A write command runs the repository's hooks as Terminal git does and keeps the rest of the hardening") func writeCommandsRunHooks() { let directory = URL(fileURLWithPath: "/tmp/repo") @@ -322,11 +383,13 @@ struct GitCommandHardeningTests { let environment = GitCommand.environment(base: [ "GIT_DIR": "/elsewhere/.git", "GIT_WORK_TREE": "/elsewhere", + "GIT_CONFIG": "/dev/null", "GIT_OPTIONAL_LOCKS": "1", "PATH": "/usr/bin", ]) #expect(environment["GIT_DIR"] == nil) #expect(environment["GIT_WORK_TREE"] == nil) + #expect(environment["GIT_CONFIG"] == nil) #expect(environment["GIT_OPTIONAL_LOCKS"] == "0") #expect(environment["GIT_TERMINAL_PROMPT"] == "0") #expect(environment["PATH"] == "/usr/bin") diff --git a/apple/changelog.d/+ghsa-rhxc-5m3h-r5ch.fixed.md b/apple/changelog.d/+ghsa-rhxc-5m3h-r5ch.fixed.md new file mode 100644 index 0000000000..0a02c6c490 --- /dev/null +++ b/apple/changelog.d/+ghsa-rhxc-5m3h-r5ch.fixed.md @@ -0,0 +1 @@ +Pull in a linked folder failing when the repository is a partial clone. diff --git a/apple/changelog.d/+ghsa-rhxc-5m3h-r5ch.security.md b/apple/changelog.d/+ghsa-rhxc-5m3h-r5ch.security.md new file mode 100644 index 0000000000..820b478df2 --- /dev/null +++ b/apple/changelog.d/+ghsa-rhxc-5m3h-r5ch.security.md @@ -0,0 +1 @@ +Git status for a linked folder running filter and fetch commands set up in that folder's own Git configuration. From 8ef0f2ca8578ffc65372cc3fee6e840e65ec47b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sun, 1 Nov 2026 00:45:02 +0700 Subject: [PATCH 50/85] Merge commit from fork * fix(security): harden the Git commands TablePro runs on its own Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-FRDSB33T. Advisory: GHSA-rhxc-5m3h-r5ch * fix(security): pin PostgreSQL catalog reads to pg_catalog and SYS-qualify Oracle Explain Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CDER3MTT. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CRPAPF23. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): harden dump tools, shell quoting and SSH host names Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-1D30XTRS. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-2L44V7CH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-3NLPR9WG. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-AX94JCED. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-E9V7ZHHQ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TWLM1VYZ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-U19XTGQC. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CF3R4T4R. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BZJK0VNE. Advisory: GHSA-94m6-qx4g-cpwg * fix(security): write Oracle Estimated Plan to SYS.PLAN_TABLE$ Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): restore a DuckDB Parquet folder through a locked-down instance of its own * test(security): expect the pg_catalog-qualified PostgreSQL cancel statement --- apple/Packages/TableProCore/Package.swift | 2 +- .../Bundle/ConnectionBundle+Validation.swift | 6 + .../Bundle/ExportableConnection.swift | 10 + .../SSHDestinationName.swift | 58 ++++ .../ConnectionBundleValidationTests.swift | 48 +++ .../LoadableExtensionLoaderTests.swift | 7 + .../SSHDestinationNameTests.swift | 81 +++++ apple/Packages/TableProMacCore/Package.swift | 1 + .../TableProSSHConfig/ResolvedSSHTarget.swift | 7 +- .../TableProSSHConfig/SSHConfigResolver.swift | 74 +++- .../TableProSSHConfig/SSHConfigTokens.swift | 13 + .../SSHHostPatternMatcher.swift | 27 +- .../SSHHostnameCanonicalizer.swift | 70 +++- .../SSHConfigParserGrammarTests.swift | 6 +- .../SSHConfigResolverDestinationTests.swift | 196 +++++++++++ .../SSHConfigResolverTests.swift | 61 ++++ .../SSHHostPatternMatcherTests.swift | 11 + .../SSHHostnameCanonicalizerTests.swift | 130 +++++++ .../DuckDBIsolatedImport.swift | 80 +++++ .../DuckDBDriverPlugin/DuckDBPlugin.swift | 4 + .../LibPQConnectionString.swift | 4 +- .../LibPQConnectionStringTests.swift | 5 + .../LoadableExtensionError.swift | 2 +- .../PluginDatabaseDriver.swift | 4 + .../PluginDriverUnsupportedOperation.swift | 3 + .../PluginExportedDatabaseImport.swift | 12 + .../Core/Database/DatabaseDriver.swift | 6 + .../Core/Database/InProcessDumpRunner.swift | 58 +++- .../Core/Database/MySQLClientArguments.swift | 29 ++ .../Database/MySQLDumpToolIdentifier.swift | 23 +- .../Database/NativeDumpArgumentQuoting.swift | 33 +- .../Core/Database/NativeDumpDescriptor.swift | 6 +- .../Core/Database/NativeDumpRegistry.swift | 113 ++++--- .../Database/NativeDumpResolvedTool.swift | 20 ++ .../Core/Database/NativeDumpService.swift | 91 +++-- .../Database/RemoteDatabaseFileTransfer.swift | 2 +- .../Core/Plugins/PluginDriverAdapter.swift | 4 + apple/TablePro/Core/SSH/HostKeyStore.swift | 38 +-- apple/TablePro/Core/SSH/HostKeyVerifier.swift | 4 + .../Core/SSH/LibSSH2ExecChannel.swift | 2 +- .../Core/SSH/LibSSH2TunnelFactory.swift | 4 +- .../Infrastructure/CommandLineShim.swift | 2 +- .../Connection/PasswordSourceResolver.swift | 2 +- .../Connection/TunnelCommandBuilder.swift | 2 +- .../TeamLibrary/TeamLibraryModels.swift | 2 +- .../TablePro/Resources/Localizable.xcstrings | 80 +++++ .../TableProMobile/Localizable.xcstrings | 16 + .../TableProMobile/SSH/HostKeyStore.swift | 20 +- .../TableProMobile/SSH/HostKeyVerifier.swift | 4 + .../SSH/HostKeyStoreTests.swift | 28 ++ .../RemoteDatabaseFileCorrectnessTests.swift | 34 ++ .../Core/SSH/HostKeyStoreTests.swift | 28 ++ .../Infrastructure/CommandLineShimTests.swift | 1 + .../PasswordSourceResolverTests.swift | 5 + .../Database/MySQLClientArgumentsTests.swift | 21 ++ .../MySQLDumpToolIdentifierTests.swift | 12 + .../NativeDumpIsolatedImportTests.swift | 316 ++++++++++++++++++ .../Database/NativeDumpRegistryTests.swift | 105 +++++- .../Database/NativeDumpScopeTests.swift | 34 +- .../Database/NativeDumpServiceTests.swift | 4 +- .../TeamLibrary/TeamLibraryModelsTests.swift | 20 ++ .../+ghsa-94m6-qx4g-cpwg.changed.md | 1 + .../changelog.d/+ghsa-94m6-qx4g-cpwg.fixed.md | 1 + .../+ghsa-94m6-qx4g-cpwg.security.1.md | 1 + .../+ghsa-94m6-qx4g-cpwg.security.2.md | 1 + .../+ghsa-94m6-qx4g-cpwg.security.3.md | 1 + .../+ghsa-94m6-qx4g-cpwg.security.4.md | 1 + .../+ghsa-94m6-qx4g-cpwg.security.5.md | 1 + .../+ghsa-94m6-qx4g-cpwg.security.6.md | 1 + .../+ghsa-94m6-qx4g-cpwg.security.7.md | 1 + .../+ghsa-94m6-qx4g-cpwg.security.8.md | 1 + .../+ghsa-94m6-qx4g-cpwg.security.md | 1 + .../probes/check-duckdb-isolated-import.sh | 217 ++++++++++++ .../probes/check-mysql-dump-tool-flags.sh | 76 ++++- .../probes/check-restore-client-commands.sh | 92 +++++ 75 files changed, 2299 insertions(+), 188 deletions(-) create mode 100644 apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHDestinationName.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHDestinationNameTests.swift create mode 100644 apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigResolverDestinationTests.swift create mode 100644 apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHHostnameCanonicalizerTests.swift create mode 100644 apple/Plugins/DuckDBDriverPlugin/DuckDBIsolatedImport.swift create mode 100644 apple/Plugins/TableProPluginKit/PluginExportedDatabaseImport.swift create mode 100644 apple/TableProTests/Database/NativeDumpIsolatedImportTests.swift create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.changed.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.fixed.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.security.1.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.security.2.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.security.3.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.security.4.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.security.5.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.security.6.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.security.7.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.security.8.md create mode 100644 apple/changelog.d/+ghsa-94m6-qx4g-cpwg.security.md create mode 100755 apple/scripts/probes/check-duckdb-isolated-import.sh create mode 100755 apple/scripts/probes/check-restore-client-commands.sh diff --git a/apple/Packages/TableProCore/Package.swift b/apple/Packages/TableProCore/Package.swift index 95b0b558d8..0aaa335a4a 100644 --- a/apple/Packages/TableProCore/Package.swift +++ b/apple/Packages/TableProCore/Package.swift @@ -105,7 +105,7 @@ let package = Package( ), .target( name: "TableProImport", - dependencies: ["TableProConnectionLibrary"], + dependencies: ["TableProConnectionLibrary", "TableProSSHTransport"], path: "Sources/TableProImport" ), .target( diff --git a/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ConnectionBundle+Validation.swift b/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ConnectionBundle+Validation.swift index c7780f3ed9..c45cbd0787 100644 --- a/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ConnectionBundle+Validation.swift +++ b/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ConnectionBundle+Validation.swift @@ -7,6 +7,7 @@ enum BundleViolation: Equatable, Sendable { case unresolvedCredentials(BundleRef) case cycle(BundleRef) case tooDeep(BundleRef) + case invalidSSHDestination(connection: String) /// Deeper than any folder or group the app creates, and shallow enough that a crafted file cannot /// make every query row carry a path of thousands of names. @@ -30,6 +31,8 @@ enum BundleViolation: Equatable, Sendable { String(format: String(localized: "the reference “%@” is inside itself"), ref.rawValue) case .tooDeep(let ref): String(format: String(localized: "the reference “%@” is nested too deeply"), ref.rawValue) + case .invalidSSHDestination(let connection): + String(format: String(localized: "the SSH host or user of “%@” contains characters ssh does not allow"), connection) } } @@ -51,6 +54,9 @@ enum BundleViolation: Equatable, Sendable { for refs in refLists { if let violation = identityViolation(refs) { return violation } } + if let connection = connections.first(where: { $0.settings.refusedSSHDestination != nil }) { + return .invalidSSHDestination(connection: connection.settings.name) + } let connectionRefs = Set(connections.map(\.ref)) let groupRefs = Set(groups.map(\.ref)) diff --git a/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ExportableConnection.swift b/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ExportableConnection.swift index 1ad029607f..c6e2ede79d 100644 --- a/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ExportableConnection.swift +++ b/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ExportableConnection.swift @@ -1,5 +1,6 @@ import Foundation import TableProConnectionLibrary +import TableProSSHTransport import UniformTypeIdentifiers public extension UTType { @@ -243,6 +244,15 @@ public struct ExportableTunnelCommand: Codable, Sendable, Equatable { } } +public extension ExportableConnection { + // Match exec hands the host and user to /bin/sh, and the host key store is one entry per line. + var refusedSSHDestination: SSHDestinationName.Problem? { + guard let ssh = sshConfig, ssh.enabled else { return nil } + let hops = [(ssh.host, ssh.username)] + (ssh.jumpHosts ?? []).map { ($0.host, $0.username) } + return hops.lazy.compactMap { SSHDestinationName.problem(host: $0.0, user: $0.1) }.first + } +} + public struct ExportableSSHConfig: Codable, Sendable, Equatable { public var enabled: Bool public var host: String diff --git a/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHDestinationName.swift b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHDestinationName.swift new file mode 100644 index 0000000000..5c9d210946 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSSHTransport/SSHDestinationName.swift @@ -0,0 +1,58 @@ +import Foundation + +// The checks OpenSSH runs on a host and user before it reads any config (valid_hostname and +// valid_ruser in ssh.c). The user also refuses control characters, which no account name holds. +public enum SSHDestinationName { + public enum Problem: Error, Hashable, Sendable { + case host(String) + case user(String) + + public var explanation: String { + switch self { + case .host(let host): + return String( + format: String(localized: "The SSH host %@ contains characters ssh does not allow in a host name."), + host + ) + case .user(let user): + return String( + format: String(localized: "The SSH user %@ contains characters ssh does not allow in a user name."), + user + ) + } + } + } + + private static let hostShellCharacters = CharacterSet(charactersIn: "'`\"$;&<>|(){},") + private static let userShellCharacters = CharacterSet(charactersIn: "'`\";&<>|(){}") + + public static func problem(host: String, user: String) -> Problem? { + if !isValidHost(host) { return .host(host) } + if !isValidUser(user) { return .user(user) } + return nil + } + + public static func isValidHost(_ host: String) -> Bool { + guard host.unicodeScalars.first != "-" else { return false } + return host.unicodeScalars.allSatisfy { scalar in + !hostShellCharacters.contains(scalar) + && scalar != "\\" + && !CharacterSet.whitespacesAndNewlines.contains(scalar) + && !CharacterSet.controlCharacters.contains(scalar) + } + } + + public static func isValidUser(_ user: String) -> Bool { + let scalars = Array(user.unicodeScalars) + guard scalars.first != "-", scalars.last != "\\" else { return false } + for (index, scalar) in scalars.enumerated() { + if userShellCharacters.contains(scalar) || CharacterSet.controlCharacters.contains(scalar) { + return false + } + if CharacterSet.whitespacesAndNewlines.contains(scalar), index + 1 < scalars.count, scalars[index + 1] == "-" { + return false + } + } + return true + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProImportTests/ConnectionBundleValidationTests.swift b/apple/Packages/TableProCore/Tests/TableProImportTests/ConnectionBundleValidationTests.swift index 29a4f3c752..1f70400277 100644 --- a/apple/Packages/TableProCore/Tests/TableProImportTests/ConnectionBundleValidationTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProImportTests/ConnectionBundleValidationTests.swift @@ -192,6 +192,54 @@ struct ConnectionBundleValidationTests { #expect(bundle.replacingSettings(renamed, of: "c9") == bundle) } + @Test("A tunnel host or user ssh would refuse is refused", arguments: [ + ("replica.attacker.example\n[prod.corp.example", "deploy", nil), + ("db$(touch /tmp/pwned).example.com", "deploy", nil), + ("db.example.com", "x; touch /tmp/pwned", nil), + ("db.example.com", "deploy", "jump`id`.example.com"), + ] as [(String, String, String?)]) + func invalidSSHDestinationIsRefused(host: String, user: String, jumpHost: String?) { + let settings = Self.tunneled(host: host, user: user, jumpHost: jumpHost) + expectRefused(.invalidSSHDestination(connection: "Tunneled")) { + try Self.bundle(connections: [BundleConnection(ref: "c1", settings: settings)]) + } + } + + @Test("A tunnel host and user ssh accepts are kept, and a tunnel that is off is not checked") + func validOrDisabledSSHDestinationIsKept() throws { + let valid = Self.tunneled(host: "db.example.com", user: "DOMAIN\\deploy", jumpHost: "jump.example.com") + var disabled = Self.tunneled(host: "db .example.com", user: "deploy", jumpHost: nil) + disabled.sshConfig?.enabled = false + let bundle = try Self.bundle(connections: [ + BundleConnection(ref: "c1", settings: valid), + BundleConnection(ref: "c2", settings: disabled) + ]) + #expect(bundle.connections.count == 2) + } + + private static func tunneled(host: String, user: String, jumpHost: String?) -> ExportableConnection { + var settings = ExportableConnection( + name: "Tunneled", host: "127.0.0.1", port: 5_432, database: "d", username: "u", type: "PostgreSQL" + ) + settings.sshConfig = ExportableSSHConfig( + enabled: true, + host: host, + port: 22, + username: user, + authMethod: "password", + privateKeyPath: "", + agentSocketPath: "", + jumpHosts: jumpHost.map { + [ExportableJumpHost(host: $0, port: 22, username: "deploy", authMethod: "sshAgent", privateKeyPath: "")] + }, + totpMode: nil, + totpAlgorithm: nil, + totpDigits: nil, + totpPeriod: nil + ) + return settings + } + private static let settings = ExportableConnection( name: "X", host: "h", port: 5_432, database: "d", username: "u", type: "PostgreSQL" ) diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/LoadableExtensionLoaderTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/LoadableExtensionLoaderTests.swift index 338d15cb1e..d4365208ea 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/LoadableExtensionLoaderTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/LoadableExtensionLoaderTests.swift @@ -134,6 +134,13 @@ struct LoadableExtensionLoaderTests { #expect(quarantined.hasSuffix(" " + quoted)) } + @Test("A quote a combining mark follows is still escaped in a suggested command") + func recoveryCommandsQuoteAQuoteBeforeACombiningMark() { + let hostile = LoadableExtension(path: "/tmp/a'\u{301}$(touch pwned).dylib") + let suggestion = LoadableExtensionError.damagedSignature(hostile).recoverySuggestion ?? "" + #expect(suggestion.hasSuffix(" '/tmp/a'\\''\u{301}$(touch pwned).dylib'")) + } + @Test("A missing file fails before SQLite is asked, and loading is still closed") func missingFileClosesLoading() throws { let handle = FakeHandle() diff --git a/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHDestinationNameTests.swift b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHDestinationNameTests.swift new file mode 100644 index 0000000000..6ff5456766 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSSHTransportTests/SSHDestinationNameTests.swift @@ -0,0 +1,81 @@ +// +// SSHDestinationNameTests.swift +// TableProSSHTransportTests +// + +import Foundation +import Testing + +@testable import TableProSSHTransport + +struct SSHDestinationNameTests { + @Test("A host ssh refuses is refused", arguments: [ + "db$(touch /tmp/pwned).internal.example", + "db`touch /tmp/pwned`.internal.example", + "db;touch /tmp/pwned;.internal.example", + "db|sh.internal.example", + "db.internal.example\\", + "db\n.internal.example", + "replica.attacker.example\n[prod.corp.example", + "db .example.com", + "db\t.example.com", + "db\r.example.com", + "db\u{202E}.example.com", + "-oProxyCommand=touch /tmp/pwned", + "db'x.example.com", + "db\"x.example.com", + "db,evil.example", + ]) + func hostileHostIsRefused(host: String) { + #expect(!SSHDestinationName.isValidHost(host)) + #expect(SSHDestinationName.problem(host: host, user: "deploy") == .host(host)) + } + + @Test("A host ssh accepts is accepted", arguments: [ + "db.example.com", + "10.0.0.5", + "fe80::1%en0", + "[fd00::5]", + "bücher.example", + "db_1.example.com", + "", + ]) + func ordinaryHostIsAccepted(host: String) { + #expect(SSHDestinationName.isValidHost(host)) + } + + @Test("A user ssh would let reach a shell is refused", arguments: [ + "x; touch /tmp/pwned; true", + "x$(touch /tmp/pwned)", + "x\ntouch /tmp/pwned", + "-oProxyCommand=touch /tmp/pwned", + "x -oProxyCommand=touch", + "x\\", + "x\u{0}y", + "x\ty", + ]) + func hostileUserIsRefused(user: String) { + #expect(!SSHDestinationName.isValidUser(user)) + #expect(SSHDestinationName.problem(host: "db.example.com", user: user) == .user(user)) + } + + @Test("A user ssh accepts is accepted", arguments: [ + "deploy", + "DOMAIN\\deploy", + "first last", + "bob,evil.example.net", + "user@realm", + "backup$", + "", + ]) + func ordinaryUserIsAccepted(user: String) { + #expect(SSHDestinationName.isValidUser(user)) + #expect(SSHDestinationName.problem(host: "db.example.com", user: user) == nil) + } + + @Test("The explanation names the refused value") + func explanationNamesTheValue() { + #expect(SSHDestinationName.Problem.host("a;b").explanation.contains("a;b")) + #expect(SSHDestinationName.Problem.user("c|d").explanation.contains("c|d")) + } +} diff --git a/apple/Packages/TableProMacCore/Package.swift b/apple/Packages/TableProMacCore/Package.swift index a79189ebf6..f2e3cf9f4f 100644 --- a/apple/Packages/TableProMacCore/Package.swift +++ b/apple/Packages/TableProMacCore/Package.swift @@ -73,6 +73,7 @@ let package = Package( name: "TableProSSHConfigTests", dependencies: [ "TableProSSHConfig", + .product(name: "TableProSSHTransport", package: "TableProCore"), .product(name: "TableProTestSupport", package: "TableProCore") ], path: "Tests/TableProSSHConfigTests", diff --git a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/ResolvedSSHTarget.swift b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/ResolvedSSHTarget.swift index 5d12ec85f4..646d0b9b96 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/ResolvedSSHTarget.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/ResolvedSSHTarget.swift @@ -20,6 +20,9 @@ public enum AgentSocketOrigin: Sendable, Hashable, CaseIterable { public struct ResolvedSSHTarget: Sendable, Hashable { public let originalHost: String public let host: String + // The canonical name with a trailing dot, so the connection reaches the name canonicalization + // resolved rather than one the resolver's search list completes. `host` stays the identity. + public let dialHost: String public let port: Int public let username: String public let identityFiles: [String] @@ -50,10 +53,12 @@ public struct ResolvedSSHTarget: Sendable, Hashable { useKeychain: Bool, addKeysToAgent: Bool, proxyJump: [SSHJumpHost], - expansionFailure: SSHTokenExpansionError? = nil + expansionFailure: SSHTokenExpansionError? = nil, + dialHost: String? = nil ) { self.originalHost = originalHost self.host = host + self.dialHost = dialHost ?? host self.port = port self.username = username self.identityFiles = identityFiles diff --git a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHConfigResolver.swift b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHConfigResolver.swift index 8784d05329..a76b67cbdf 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHConfigResolver.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHConfigResolver.swift @@ -5,6 +5,7 @@ import Foundation import os +import TableProSSHTransport public struct ResolverEnvironment: Sendable { var runShell: @Sendable (String) -> Bool @@ -90,11 +91,18 @@ public enum SSHConfigResolver { env: ResolverEnvironment ) -> ResolvedSSHTarget { let localUser = env.currentLocalUser() - var failure: SSHTokenExpansionError? + // The host and user can come from an imported file or a link, and Match exec hands them to + // /bin/sh, so like ssh no block is evaluated for a name it would refuse. + var failure = SSHDestinationName.problem(host: originalHost, user: formUser) + .map(SSHTokenExpansionError.invalidDestination) + if let failure { + logger.warning("\(failure.explanation, privacy: .public)") + } + let blocks = failure == nil ? document.blocks : [] var firstPass = ResolutionState() applyMatchingBlocks( - blocks: document.blocks, + blocks: blocks, pass: PassInputs( originalHost: originalHost, currentHost: originalHost, @@ -130,15 +138,21 @@ public enum SSHConfigResolver { let isProxied = !formJumpHosts.isEmpty || firstPass.proxyJump.map { !SSHConfigParser.isNone($0) } == true let canonicalizes = canonicalOptions.mode == .always || (canonicalOptions.mode == .yes && !isProxied) let canonicalizedHost: String - if canonicalizes, let canonical = env.canonicalize(substitutedHost, canonicalOptions) { + if !canonicalizes { + canonicalizedHost = substitutedHost + } else if let canonical = env.canonicalize(substitutedHost, canonicalOptions) { canonicalizedHost = canonical } else { + // Dialing the short name would leave the server to the network's search list. + let error = SSHTokenExpansionError.hostNotInCanonicalDomains(host: substitutedHost) + logger.warning("\(error.explanation, privacy: .public)") + if failure == nil { failure = error } canonicalizedHost = substitutedHost } var secondPass = ResolutionState() applyMatchingBlocks( - blocks: document.blocks, + blocks: blocks, pass: PassInputs( originalHost: originalHost, currentHost: canonicalizedHost, @@ -246,7 +260,8 @@ public enum SSHConfigResolver { useKeychain: merged.useKeychain ?? true, addKeysToAgent: merged.addKeysToAgent ?? false, proxyJump: effectiveProxyJump, - expansionFailure: failure + expansionFailure: failure, + dialHost: canonicalizes && canonicalizedHost != substitutedHost ? canonicalizedHost + "." : effectiveHost ) } @@ -509,6 +524,12 @@ public enum SSHConfigResolver { localUser: inputs.localUser, localHostname: env.localHostname() ) + // A HostName substitution or a canonical name from DNS never passed the entry check. + if let problem = destinationProblem(in: context, substituting: tokens(in: command)) { + logger.warning("\(problem.explanation, privacy: .public)") + if failure == nil { failure = problem } + return nil + } guard let expanded = expand( command, scope: .matchExec, @@ -519,6 +540,49 @@ public enum SSHConfigResolver { return env.runShell(expanded) } } + + private static func destinationProblem( + in context: SSHTokenContext, + substituting tokens: Set + ) -> SSHTokenExpansionError? { + var hosts: [String?] = [] + if !tokens.isDisjoint(with: ["n", "k"]) { hosts.append(context.originalHost) } + if tokens.contains("h") { hosts.append(context.hostname) } + for host in hosts.compactMap({ $0 }) where !SSHDestinationName.isValidHost(host) { + return .invalidDestination(.host(host)) + } + // ssh lets `alice \-oProxyCommand=…` and `x$IFS-o…` through, and an unquoted %r turns + // either into an option of the probe's command. + if tokens.contains("r"), let user = context.remoteUser, + !SSHDestinationName.isValidUser(user) || user.contains("$") || startsAWordWithABackslash(user) { + return .invalidDestination(.user(user)) + } + return nil + } + + private static func startsAWordWithABackslash(_ value: String) -> Bool { + var previous: Unicode.Scalar = " " + for scalar in value.unicodeScalars { + if scalar == "\\", CharacterSet.whitespaces.contains(previous) { return true } + previous = scalar + } + return false + } + + private static func tokens(in command: String) -> Set { + var tokens: Set = [] + var index = command.startIndex + while index < command.endIndex { + let next = command.index(after: index) + guard command[index] == "%", next < command.endIndex else { + index = next + continue + } + if command[next] != "%" { tokens.insert(command[next]) } + index = command.index(after: next) + } + return tokens + } } // MARK: - Resolution state diff --git a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHConfigTokens.swift b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHConfigTokens.swift index f521891fe9..26ed946da5 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHConfigTokens.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHConfigTokens.swift @@ -6,6 +6,7 @@ import CryptoKit import Darwin import Foundation +import TableProSSHTransport import TableProUtilities /// Which `%X` tokens a keyword accepts, per the TOKENS section of ssh_config(5). OpenSSH treats a @@ -65,6 +66,9 @@ public enum SSHTokenExpansionError: Error, Hashable, Sendable { case undefinedEnvironmentVariable(keyword: String, name: String) /// A `${` with no closing brace, which ssh refuses to run on. case malformedEnvironmentReference(keyword: String) + case invalidDestination(SSHDestinationName.Problem) + // `CanonicalizeFallbackLocal no` and no `CanonicalDomains` suffix resolved, where ssh stops. + case hostNotInCanonicalDomains(host: String) public var explanation: String { switch self { @@ -90,6 +94,15 @@ public enum SSHTokenExpansionError: Error, Hashable, Sendable { format: String(localized: "The %@ line in ~/.ssh/config has a ${ with no closing brace."), keyword ) + case .invalidDestination(let problem): + return problem.explanation + case .hostNotInCanonicalDomains(let host): + return String( + format: String( + localized: "%@ was not found in any of the CanonicalDomains in ~/.ssh/config, and CanonicalizeFallbackLocal is set to no." + ), + host + ) } } } diff --git a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHHostPatternMatcher.swift b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHHostPatternMatcher.swift index 5e94977ea8..6fe382956c 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHHostPatternMatcher.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHHostPatternMatcher.swift @@ -7,8 +7,8 @@ import Darwin import Foundation /// Pattern list matching mirrors OpenSSH's `match_pattern_list`: a host matches iff at least one -/// positive pattern matches AND no negative pattern matches. Globs are evaluated via POSIX -/// `fnmatch(3)`, the same primitive OpenSSH uses. +/// positive pattern matches AND no negative pattern matches. `*` and `?` are the only wildcards, +/// as in `match_pattern`; `fnmatch(3)` also reads `[...]` and `\`, which ssh takes literally. /// /// The two constructs that take a pattern list do not agree on how it is written, measured against /// OpenSSH 10.3: a `Host` line is a whitespace-separated list matched case-sensitively, so @@ -23,7 +23,7 @@ enum SSHHostPatternMatcher { var hasPositiveMatch = false for pattern in patterns { let glob = caseSensitive ? pattern.glob : pattern.glob.lowercased() - guard fnmatch(glob, subject) else { continue } + guard matchPattern(Array(subject.utf8)[...], Array(glob.utf8)[...]) else { continue } if pattern.negated { return false @@ -55,11 +55,24 @@ enum SSHHostPatternMatcher { } } - private static func fnmatch(_ pattern: String, _ name: String) -> Bool { - pattern.withCString { patternPtr in - name.withCString { namePtr in - Darwin.fnmatch(patternPtr, namePtr, 0) == 0 + // match_pattern in OpenSSH's match.c, byte by byte. + private static func matchPattern(_ name: ArraySlice, _ pattern: ArraySlice) -> Bool { + var name = name + var pattern = pattern + while let head = pattern.first { + if head == UInt8(ascii: "*") { + pattern = pattern.drop { $0 == UInt8(ascii: "*") } + guard !pattern.isEmpty else { return true } + while !name.isEmpty { + if matchPattern(name, pattern) { return true } + name = name.dropFirst() + } + return false } + guard let character = name.first, head == UInt8(ascii: "?") || head == character else { return false } + name = name.dropFirst() + pattern = pattern.dropFirst() } + return name.isEmpty } } diff --git a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHHostnameCanonicalizer.swift b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHHostnameCanonicalizer.swift index 7097f57452..43c975aeec 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHHostnameCanonicalizer.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProSSHConfig/SSHHostnameCanonicalizer.swift @@ -26,9 +26,8 @@ struct SSHCanonicalizationOptions: Sendable, Hashable { enum SSHHostnameCanonicalizer { private static let logger = Logger(subsystem: "com.TablePro", category: "SSHCanonicalize") - /// Returns nil only when no `CanonicalDomains` candidate resolves AND - /// `CanonicalizeFallbackLocal` is set to `no`. The caller treats nil as - /// "abort canonicalization" per ssh_config(5). + // Nil only when no `CanonicalDomains` candidate resolves under `CanonicalizeFallbackLocal no`, + // which the caller reports instead of connecting. static func canonicalize( host: String, options: SSHCanonicalizationOptions, @@ -36,6 +35,14 @@ enum SSHHostnameCanonicalizer { ) -> String? { guard options.mode != .no else { return host } guard !host.isEmpty else { return host } + guard !isNumericHost(host) else { return host } + + // An anchored name is looked up as it is, before MaxDots and the suffixes, as ssh does. + if host.hasSuffix(".") { + let candidate = String(host.dropLast()) + guard let resolved = resolver(candidate) else { return options.fallbackLocal ? host : nil } + return followsCNAME(from: candidate, to: resolved, rules: options.permittedCNAMEs) ? resolved : candidate + } if dotCount(in: host) > options.maxDots { return host @@ -43,7 +50,10 @@ enum SSHHostnameCanonicalizer { for domain in options.domains { let candidate = host + "." + domain.trimmingCharacters(in: CharacterSet(charactersIn: ".")) - if let canonical = resolver(candidate) { + if let resolved = resolver(candidate) { + let canonical = followsCNAME(from: candidate, to: resolved, rules: options.permittedCNAMEs) + ? resolved + : candidate logger.debug("Canonicalized \(host, privacy: .public) -> \(canonical, privacy: .public)") return canonical } @@ -55,6 +65,52 @@ enum SSHHostnameCanonicalizer { return nil } + // check_follow_cname in ssh.c: the canonical name becomes the dialed host and the host key's + // name, so DNS picks it only where a `source_list:target_list` rule, or a bare `*`, covers both. + private static func followsCNAME(from candidate: String, to cname: String, rules: String?) -> Bool { + guard let rules, !SSHConfigParser.isNone(rules), cname != candidate, isValidDomain(cname) else { return false } + return rules.split(whereSeparator: { $0 == " " || $0 == "\t" }).contains { rule in + if rule == "*" { return true } + let lists = rule.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: false).map(String.init) + guard lists.count == 2 else { return false } + return matches(candidate, patternList: lists[0]) && matches(cname, patternList: lists[1]) + } + } + + private static func matches(_ name: String, patternList: String) -> Bool { + SSHHostPatternMatcher.matches( + host: name, + patterns: SSHHostPatternMatcher.parseMatchPatternList(patternList), + caseSensitive: false + ) + } + + // valid_domain in OpenSSH's misc.c, which ssh applies to a CNAME before using it. + private static func isValidDomain(_ name: String) -> Bool { + let scalars = Array(name.unicodeScalars) + guard let first = scalars.first, isASCIIAlphanumeric(first) || first == "_" else { return false } + for (index, scalar) in scalars.enumerated() { + guard isASCIIAlphanumeric(scalar) || scalar == "." || scalar == "-" || scalar == "_" else { return false } + if scalar == ".", index > 0, scalars[index - 1] == "." { return false } + } + return true + } + + private static func isASCIIAlphanumeric(_ scalar: Unicode.Scalar) -> Bool { + scalar.isASCII && CharacterSet.alphanumerics.contains(scalar) + } + + // getaddrinfo's numeric rules, as ssh's resolve_addr uses, so `127.1` and a scoped IPv6 count too. + private static func isNumericHost(_ host: String) -> Bool { + var hints = addrinfo() + hints.ai_family = AF_UNSPEC + hints.ai_flags = AI_NUMERICHOST + var result: UnsafeMutablePointer? + guard getaddrinfo(host, nil, &hints, &result) == 0 else { return false } + freeaddrinfo(result) + return true + } + private static func dotCount(in host: String) -> Int { host.reduce(0) { $1 == "." ? $0 + 1 : $0 } } @@ -66,14 +122,16 @@ enum SSHHostnameCanonicalizer { hints.ai_flags = AI_CANONNAME var result: UnsafeMutablePointer? - let rc = getaddrinfo(candidate, nil, &hints, &result) + // Anchored as ssh does, or the resolver's search list could complete a name that does not exist. + let rc = getaddrinfo(candidate + ".", nil, &hints, &result) guard rc == 0, let info = result else { return nil } defer { freeaddrinfo(info) } if let canonName = info.pointee.ai_canonname { - return String(cString: canonName) + let name = String(cString: canonName) + return name.hasSuffix(".") ? String(name.dropLast()) : name } return candidate } diff --git a/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigParserGrammarTests.swift b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigParserGrammarTests.swift index ac993ee96d..335041fb37 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigParserGrammarTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigParserGrammarTests.swift @@ -7,6 +7,7 @@ // import Foundation +import TableProSSHTransport import Testing @testable import TableProSSHConfig @@ -100,7 +101,7 @@ struct SSHConfigParserGrammarTests { // MARK: - Pattern lists /// `ssh -G a` gave port 22: a `Host` line is a whitespace-separated list, so the comma is an - /// ordinary character and `Host a,b` matches a host literally called `a,b`. + /// ordinary character and `Host a,b` matches only the literal `a,b`, a host ssh refuses to dial. @Test("A Host line separates patterns on whitespace, not commas") func hostPatternListUsesWhitespace() { let content = """ @@ -109,7 +110,8 @@ struct SSHConfigParserGrammarTests { """ #expect(resolve(content, host: "a").port == 22) #expect(resolve(content, host: "b").port == 22) - #expect(resolve(content, host: "a,b").port == 2_401) + #expect(resolve(content, host: "a,b").expansionFailure == .invalidDestination(.host("a,b"))) + #expect(SSHHostPatternMatcher.matches(host: "a,b", patterns: SSHHostPatternMatcher.parseHostPatternList("a,b"))) } @Test("A Host line with several patterns matches each of them") diff --git a/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigResolverDestinationTests.swift b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigResolverDestinationTests.swift new file mode 100644 index 0000000000..6112c62223 --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigResolverDestinationTests.swift @@ -0,0 +1,196 @@ +// +// SSHConfigResolverDestinationTests.swift +// TableProTests +// + +import Foundation +import TableProSSHTransport +import Testing + +@testable import TableProSSHConfig + +struct SSHConfigResolverDestinationTests { + private final class CommandRecorder: @unchecked Sendable { + private let lock = NSLock() + private var commands: [String] = [] + + func record(_ command: String) { + lock.lock() + defer { lock.unlock() } + commands.append(command) + } + + var recorded: [String] { + lock.lock() + defer { lock.unlock() } + return commands + } + } + + private static let document = SSHConfigParser.parseDocumentContent( + """ + Match host *.internal.example !exec "nc -z -w 1 %h %p" + ProxyJump bastion.example.com + Match exec "test %r = deploy" + IdentityFile ~/.ssh/deploy + """ + ) + + private func environment( + _ recorder: CommandRecorder, + canonicalize: @escaping @Sendable (String, SSHCanonicalizationOptions) -> String? = { host, _ in host } + ) -> ResolverEnvironment { + ResolverEnvironment( + runShell: { command in + recorder.record(command) + return true + }, + canonicalize: canonicalize, + currentLocalUser: { "tester" }, + localHostname: { "Mac" } + ) + } + + private func resolve( + host: String, + username: String = "", + document: SSHConfigDocument = Self.document + ) -> (target: ResolvedSSHTarget, commands: [String]) { + let recorder = CommandRecorder() + let target = SSHConfigResolver.resolve( + SSHConfiguration(enabled: true, host: host, username: username, authMethod: .privateKey), + document: document, + env: environment(recorder) + ) + return (target, recorder.recorded) + } + + @Test("A tunnel host with shell syntax never reaches the Match exec shell", arguments: [ + "db$(touch /tmp/pwned).internal.example", + "db`touch /tmp/pwned`.internal.example", + "db;touch /tmp/pwned;.internal.example", + "db|sh.internal.example", + "db.internal.example\\", + "db\n.internal.example", + "-oProxyCommand=touch /tmp/pwned", + ]) + func hostileHostIsNotExpandedIntoACommand(host: String) { + let (target, commands) = resolve(host: host, username: "deploy") + #expect(commands.isEmpty) + #expect(target.expansionFailure == .invalidDestination(.host(host))) + #expect(target.identityFiles.isEmpty) + } + + @Test("A tunnel user with shell syntax never reaches the Match exec shell", arguments: [ + "x; touch /tmp/pwned; true", + "x$(touch /tmp/pwned)", + "x\ntouch /tmp/pwned", + "-oProxyCommand=touch /tmp/pwned", + "x -oProxyCommand=touch", + "x\\", + "alice \\-oProxyCommand=printf\\ probe", + "\\-oProxyCommand=printf", + "x$IFS-oProxyCommand=printf", + "backup$", + ]) + func hostileUserIsNotExpandedIntoACommand(username: String) { + let (target, commands) = resolve(host: "db.example.com", username: username) + #expect(commands.isEmpty) + #expect(target.expansionFailure == .invalidDestination(.user(username))) + #expect(target.identityFiles.isEmpty) + } + + @Test("A user name ssh accepts still connects when no Match exec substitutes it", arguments: [ + "first last", "alice \\-oProxyCommand=printf", "backup$" + ]) + func userWithASpaceResolvesWhenNotSubstituted(username: String) { + let document = SSHConfigParser.parseDocumentContent(""" + Match exec "probe %h %%r" + Port 2222 + """) + let (target, commands) = resolve(host: "db.example.com", username: username, document: document) + #expect(commands == ["probe db.example.com %r"]) + #expect(target.expansionFailure == nil) + #expect(target.port == 2_222) + } + + @Test("A user with a space stays one word inside a quoted %r") + func userWithASpaceIsSubstituted() { + let document = SSHConfigParser.parseDocumentContent(""" + Match exec "test '%r' = 'first last'" + Port 2222 + """) + let (target, commands) = resolve(host: "db.example.com", username: "first last", document: document) + #expect(commands == ["test 'first last' = 'first last'"]) + #expect(target.expansionFailure == nil) + #expect(target.port == 2_222) + } + + @Test("An ordinary host and user are still probed") + func ordinaryNamesAreProbed() { + let (target, commands) = resolve(host: "db1.internal.example", username: "deploy") + #expect(commands == ["nc -z -w 1 db1.internal.example 22", "test deploy = deploy"]) + #expect(target.expansionFailure == nil) + #expect(target.identityFiles.map { $0.hasSuffix("/.ssh/deploy") } == [true]) + } + + @Test("Names ssh accepts are accepted", arguments: [ + ("fe80::1%en0", "deploy"), + ("db.example.com", "DOMAIN\\deploy"), + ("db.example.com", "bob,evil.example.net"), + ]) + func ordinaryNamesResolve(host: String, username: String) { + let (target, _) = resolve(host: host, username: username) + #expect(target.expansionFailure == nil) + #expect(target.host == host) + #expect(target.username == username) + } + + @Test("A jump host from the form is checked the same way") + func jumpHostIsChecked() { + let recorder = CommandRecorder() + let host = "bastion$(touch /tmp/pwned).internal.example" + let target = SSHConfigResolver.resolve( + SSHJumpHost(host: host, username: "deploy"), + document: Self.document, + env: environment(recorder) + ) + #expect(recorder.recorded.isEmpty) + #expect(target.expansionFailure == .invalidDestination(.host(host))) + } + + @Test("A HostName substitution with shell syntax never reaches the Match exec shell") + func substitutedHostIsChecked() { + let document = SSHConfigParser.parseDocumentContent( + """ + Host alias + HostName db;id + Match exec "probe %h" + User deploy + """ + ) + let (target, commands) = resolve(host: "alias", document: document) + #expect(commands.isEmpty) + #expect(target.expansionFailure == .invalidDestination(.host("db;id"))) + } + + @Test("A canonical name from DNS with shell syntax never reaches the second-pass Match exec") + func canonicalHostIsChecked() { + let document = SSHConfigParser.parseDocumentContent( + """ + CanonicalizeHostname yes + CanonicalDomains corp.example + Match canonical exec "probe %h" + User deploy + """ + ) + let recorder = CommandRecorder() + let target = SSHConfigResolver.resolve( + SSHConfiguration(enabled: true, host: "db", authMethod: .privateKey), + document: document, + env: environment(recorder) { _, _ in "db$(id).corp.example" } + ) + #expect(recorder.recorded.isEmpty) + #expect(target.expansionFailure == .invalidDestination(.host("db$(id).corp.example"))) + } +} diff --git a/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigResolverTests.swift b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigResolverTests.swift index 2eeba8b51a..4c1058b814 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigResolverTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHConfigResolverTests.swift @@ -312,6 +312,67 @@ struct SSHConfigResolverTests { #expect(resolvedHost(mode: "always") == "db.corp.example") } + private func canonicalized(host: String, fallbackLocal: String, known: Set = []) -> ResolvedSSHTarget { + let env = ResolverEnvironment( + runShell: { _ in true }, + canonicalize: { host, options in + SSHHostnameCanonicalizer.canonicalize(host: host, options: options) { known.contains($0) ? $0 : nil } + }, + currentLocalUser: { "tester" } + ) + let document = SSHConfigParser.parseDocumentContent(""" + CanonicalizeHostname yes + CanonicalDomains corp.example + CanonicalizeFallbackLocal \(fallbackLocal) + """) + return SSHConfigResolver.resolve(makeConfig(host: host), document: document, env: env) + } + + @Test("CanonicalizeFallbackLocal no stops the connection when no CanonicalDomains suffix resolves") + func canonicalizeFallbackLocalNoRefusesAnUnresolvedHost() { + #expect(canonicalized(host: "db", fallbackLocal: "yes").expansionFailure == nil) + #expect(canonicalized(host: "db", fallbackLocal: "yes").host == "db") + #expect(canonicalized(host: "db", fallbackLocal: "no").expansionFailure == .hostNotInCanonicalDomains(host: "db")) + + let found = canonicalized(host: "db", fallbackLocal: "no", known: ["db.corp.example"]) + #expect(found.expansionFailure == nil) + #expect(found.host == "db.corp.example") + #expect(found.dialHost == "db.corp.example.") + } + + @Test("An anchored host keeps ssh's undotted identity and is dialed anchored") + func anchoredHostIsMatchedUndottedAndDialedAnchored() { + let env = ResolverEnvironment( + runShell: { _ in true }, + canonicalize: { host, options in + SSHHostnameCanonicalizer.canonicalize(host: host, options: options) { $0 == "db.corp.example" ? $0 : nil } + }, + currentLocalUser: { "tester" } + ) + let document = SSHConfigParser.parseDocumentContent(""" + CanonicalizeHostname yes + CanonicalDomains corp.example + CanonicalizeFallbackLocal no + Match canonical host db.corp.example + Port 2200 + """) + let resolved = SSHConfigResolver.resolve(makeConfig(host: "db.corp.example."), document: document, env: env) + #expect(resolved.expansionFailure == nil) + #expect(resolved.host == "db.corp.example") + #expect(resolved.dialHost == "db.corp.example.") + #expect(resolved.port == 2_200) + #expect(canonicalized(host: "db.other.example", fallbackLocal: "no").dialHost == "db.other.example") + } + + @Test("CanonicalizeFallbackLocal no leaves an address and a qualified name alone", arguments: [ + "fd00::5", "fe80::1%en0", "10.0.0.5", "127.1", "db.other.example" + ]) + func canonicalizeFallbackLocalNoAcceptsWhatIsNotCanonicalized(host: String) { + let resolved = canonicalized(host: host, fallbackLocal: "no") + #expect(resolved.expansionFailure == nil) + #expect(resolved.host == host) + } + private func user(for content: String, addresses: [String] = []) -> String { let env = ResolverEnvironment( runShell: { _ in true }, diff --git a/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHHostPatternMatcherTests.swift b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHHostPatternMatcherTests.swift index dcc081658b..c7d2c210b0 100644 --- a/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHHostPatternMatcherTests.swift +++ b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHHostPatternMatcherTests.swift @@ -76,6 +76,17 @@ struct SSHHostPatternMatcherTests { #expect(SSHHostPatternMatcher.parseMatchPatternList("a,b").map(\.glob) == ["a", "b"]) } + @Test("Only * and ? are wildcards; brackets and backslashes match themselves, as in ssh") + func testBracketAndBackslashAreLiteral() { + let brackets = [HostPattern(glob: "db[12]", negated: false)] + #expect(!SSHHostPatternMatcher.matches(host: "db1", patterns: brackets)) + #expect(SSHHostPatternMatcher.matches(host: "db[12]", patterns: brackets)) + let backslash = [HostPattern(glob: "a\\*", negated: false)] + #expect(SSHHostPatternMatcher.matches(host: "a\\bc", patterns: backslash)) + #expect(!SSHHostPatternMatcher.matches(host: "a*", patterns: [HostPattern(glob: "a\\*x", negated: false)])) + #expect(SSHHostPatternMatcher.matches(host: "db-7.internal.example", patterns: [HostPattern(glob: "*-?.internal.*", negated: false)])) + } + @Test("Match host comparison ignores case") func testMatchHostIsCaseInsensitive() { let patterns = SSHHostPatternMatcher.parseMatchPatternList("real.EXAMPLE.com") diff --git a/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHHostnameCanonicalizerTests.swift b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHHostnameCanonicalizerTests.swift new file mode 100644 index 0000000000..9914a3dc8f --- /dev/null +++ b/apple/Packages/TableProMacCore/Tests/TableProSSHConfigTests/SSHHostnameCanonicalizerTests.swift @@ -0,0 +1,130 @@ +// +// SSHHostnameCanonicalizerTests.swift +// TableProTests +// + +import Foundation +import Testing + +@testable import TableProSSHConfig + +struct SSHHostnameCanonicalizerTests { + private func canonicalize( + _ host: String, + permittedCNAMEs: String?, + fallbackLocal: Bool = true, + resolver: (String) -> String? + ) -> String? { + let options = SSHCanonicalizationOptions( + mode: .yes, + domains: ["corp.example"], + fallbackLocal: fallbackLocal, + maxDots: 1, + permittedCNAMEs: permittedCNAMEs + ) + return SSHHostnameCanonicalizer.canonicalize(host: host, options: options, resolver: resolver) + } + + private func canonicalize(_ host: String, permittedCNAMEs: String?, cname: String) -> String? { + canonicalize(host, permittedCNAMEs: permittedCNAMEs) { _ in cname } + } + + @Test("A CNAME is not followed when no CanonicalizePermittedCNAMEs rule is set") + func cnameIsNotFollowedByDefault() { + #expect(canonicalize("db", permittedCNAMEs: nil, cname: "staging.vendor.example") == "db.corp.example") + } + + @Test("A CNAME no rule covers is not followed", arguments: [ + "none", + "*.corp.example:*.corp.example", + "*.other.example:*.vendor.example", + "*.corp.example:*,!*.vendor.example", + "*.corp.example", + "*.corp.example:", + ":*.vendor.example", + ]) + func cnameOutsideTheRulesIsNotFollowed(rules: String) { + #expect(canonicalize("db", permittedCNAMEs: rules, cname: "staging.vendor.example") == "db.corp.example") + } + + @Test("A bracket in a rule is a literal character, as in ssh") + func bracketInARuleIsLiteral() { + let rules = "*.corp.example:db[12].vendor.example" + #expect(canonicalize("db", permittedCNAMEs: rules, cname: "db1.vendor.example") == "db.corp.example") + #expect(canonicalize("db", permittedCNAMEs: rules, cname: "db[12].vendor.example") == "db.corp.example") + } + + @Test("A CNAME a rule covers is followed", arguments: [ + "*", + "*:*", + "*.corp.example:*.internal.example", + "*.CORP.example:*.Internal.Example", + "*.other.example:*.other.example *.corp.example:*.dmz.example,*.internal.example", + ]) + func permittedCNAMEIsFollowed(rules: String) { + #expect(canonicalize("db", permittedCNAMEs: rules, cname: "db-7.internal.example") == "db-7.internal.example") + } + + @Test("A CNAME may start with an underscore, as valid_domain allows") + func underscoreCNAMEIsFollowed() { + #expect(canonicalize("db", permittedCNAMEs: "*", cname: "_ssh.internal.example") == "_ssh.internal.example") + } + + @Test("A CNAME that is not a valid domain name is never followed", arguments: [ + "", "db\n.internal.example", "db$(id).internal.example", "db[12].internal.example", "-db.internal.example", + "db..internal.example", "bücher.internal.example" + ]) + func invalidCNAMEIsNotFollowed(cname: String) { + #expect(canonicalize("db", permittedCNAMEs: "*", cname: cname) == "db.corp.example") + } + + @Test("An address is never canonicalized", arguments: ["fd00::5", "fe80::1%en0", "10.0.0.5", "127.1", "2130706433"]) + func addressIsLeftAlone(host: String) { + #expect(canonicalize(host, permittedCNAMEs: "*", fallbackLocal: false) { _ in "elsewhere.example" } == host) + } + + @Test("An anchored name is looked up as it is, before MaxDots and the domains") + func anchoredNameIsLookedUpDirectly() { + var asked: [String] = [] + let found = canonicalize("db.corp.example.", permittedCNAMEs: nil, fallbackLocal: false) { name in + asked.append(name) + return name == "db.corp.example" ? name : nil + } + #expect(found == "db.corp.example") + #expect(asked == ["db.corp.example"]) + #expect(canonicalize("gone.example.", permittedCNAMEs: nil, fallbackLocal: false) { _ in nil } == nil) + #expect(canonicalize("gone.example.", permittedCNAMEs: nil, fallbackLocal: true) { _ in nil } == "gone.example.") + #expect(canonicalize("db.corp.example.", permittedCNAMEs: "*", cname: "db-7.internal.example") == "db-7.internal.example") + #expect(canonicalize("db.corp.example.", permittedCNAMEs: nil, cname: "db-7.internal.example") == "db.corp.example") + } + + @Test("CanonicalizeFallbackLocal no answers nil when no candidate resolves") + func fallbackLocalNoAnswersNil() { + #expect(canonicalize("db", permittedCNAMEs: nil, fallbackLocal: false) { _ in nil } == nil) + #expect(canonicalize("db", permittedCNAMEs: nil, fallbackLocal: true) { _ in nil } == "db") + } + + @Test("CanonicalizePermittedCNAMEs reaches the canonicalizer from the config") + func rulesComeFromTheConfig() { + func resolvedHost(rules: String) -> String { + let env = ResolverEnvironment( + runShell: { _ in true }, + canonicalize: { host, options in + SSHHostnameCanonicalizer.canonicalize(host: host, options: options) { _ in "db-7.internal.example" } + }, + currentLocalUser: { "tester" } + ) + let document = SSHConfigParser.parseDocumentContent(""" + CanonicalizeHostname yes + CanonicalDomains corp.example + \(rules) + """) + let config = SSHConfiguration(enabled: true, host: "db") + return SSHConfigResolver.resolve(config, document: document, env: env).host + } + + #expect(resolvedHost(rules: "") == "db.corp.example") + #expect(resolvedHost(rules: "CanonicalizePermittedCNAMEs *.corp.example:*.vendor.example") == "db.corp.example") + #expect(resolvedHost(rules: "CanonicalizePermittedCNAMEs *.corp.example:*.internal.example") == "db-7.internal.example") + } +} diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBIsolatedImport.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBIsolatedImport.swift new file mode 100644 index 0000000000..143ecacf7d --- /dev/null +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBIsolatedImport.swift @@ -0,0 +1,80 @@ +// +// DuckDBIsolatedImport.swift +// DuckDBDriverPlugin +// + +import CDuckDB +import Foundation + +// DuckDB's settings for running untrusted SQL ("Securing DuckDB" in its operations manual), on an instance of its own, +// since `enable_external_access` cannot be turned back on once a session turns it off. +enum DuckDBIsolatedImport { + static let openOptions: [(name: String, value: String)] = [ + ("autoinstall_known_extensions", "false"), + ("autoload_known_extensions", "false"), + ("allow_community_extensions", "false"), + ] + + // duckdb_set_config rejects allowed_directories, and DuckDB refuses to change it once external access is off. + static func lockdownStatements(allowing folder: String) -> [String] { + [ + "SET allowed_directories = [\(DuckDBSchemaQueries.quoteLiteral(folder))]", + "SET enable_external_access = false", + "SET lock_configuration = true", + ] + } + + static func importStatement(folder: String) -> String { + "IMPORT DATABASE \(DuckDBSchemaQueries.quoteLiteral(folder))" + } + + static func run(folder: String, into file: String) async throws { + let live = DuckDBLiveConnectionBox() + try await withTaskCancellationHandler { + try importFolder(folder, into: file, live: live) + } onCancel: { + live.interrupt() + } + } + + private static func importFolder(_ folder: String, into file: String, live: DuckDBLiveConnectionBox) throws { + var config: duckdb_config? + guard duckdb_create_config(&config) != DuckDBError else { + throw DuckDBPluginError.connectionFailed("DuckDB could not allocate a configuration") + } + defer { duckdb_destroy_config(&config) } + for option in openOptions where duckdb_set_config(config, option.name, option.value) == DuckDBError { + throw DuckDBPluginError.connectionFailed("DuckDB rejected \(option.name) = \(option.value)") + } + + var database: duckdb_database? + var openError: UnsafeMutablePointer? + guard duckdb_open_ext(file, &database, config, &openError) != DuckDBError else { + let detail = openError.map { String(cString: $0) } ?? "unknown error" + duckdb_free(openError) + throw DuckDBPluginError.connectionFailed("Failed to open DuckDB database at '\(file)': \(detail)") + } + defer { duckdb_close(&database) } + + var handle: duckdb_connection? + guard duckdb_connect(database, &handle) != DuckDBError, let connection = handle else { + throw DuckDBPluginError.connectionFailed("Failed to create DuckDB connection") + } + live.set(connection) + defer { + live.set(nil) + duckdb_disconnect(&handle) + } + if Task.isCancelled { throw CancellationError() } + + // IMPORT DATABASE expands into the folder's statements, which duckdb_prepare refuses. + for statement in lockdownStatements(allowing: folder) + [importStatement(folder: folder)] { + var result = duckdb_result() + defer { duckdb_destroy_result(&result) } + guard duckdb_query(connection, statement, &result) != DuckDBError else { + let message = duckdb_result_error(&result).map { String(cString: $0) } ?? "Unknown DuckDB error" + throw DuckDBPluginError.queryFailed(message) + } + } + } +} diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift index ce3cd3ebc4..72eb93e094 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift @@ -609,6 +609,10 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } } + func importExportedDatabase(from folder: URL, into file: URL) async throws { + try await DuckDBIsolatedImport.run(folder: folder.path, into: file.path) + } + func disconnect() { liveConnection.interrupt() resetCurrentPosition() diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQConnectionString.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQConnectionString.swift index 0d27a0d558..09bd9173d8 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQConnectionString.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQConnectionString.swift @@ -208,7 +208,7 @@ internal enum LibPQConnectionString { } static func escape(_ value: String) -> String { - value.replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "'", with: "\\'") + value.replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "'", with: "\\'", options: .literal) } } diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQConnectionStringTests.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQConnectionStringTests.swift index de506753a3..2996ad1003 100644 --- a/apple/Plugins/PostgreSQLDriverTests/LibPQConnectionStringTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQConnectionStringTests.swift @@ -185,6 +185,11 @@ struct LibPQConnectionStringTests { #expect(conninfo.contains("options='-c application_name=\\'x\\''")) } + @Test("A quote a combining mark follows is still escaped") + func escapesAQuoteBeforeACombiningMark() { + #expect(LibPQConnectionString.escape("a'\u{301} host=evil") == "a\\'\u{301} host=evil") + } + @Test("The application name reaches libpq as a fallback, ahead of the connection options") func sendsFallbackApplicationName() { let conninfo = build(options: "-c search_path=app", applicationName: "TablePro Metadata") diff --git a/apple/Plugins/TableProPluginKit/LoadableExtensionError.swift b/apple/Plugins/TableProPluginKit/LoadableExtensionError.swift index 24faf01470..b116836749 100644 --- a/apple/Plugins/TableProPluginKit/LoadableExtensionError.swift +++ b/apple/Plugins/TableProPluginKit/LoadableExtensionError.swift @@ -118,6 +118,6 @@ extension LoadableExtensionError: LocalizedError { /// One shell word, single-quoted, so a path that came from an import cannot add a command /// substitution or a second command to a line the user pastes into Terminal. static func shellQuoted(_ text: String) -> String { - "'" + text.replacingOccurrences(of: "'", with: "'\\''") + "'" + "'" + text.replacingOccurrences(of: "'", with: "'\\''", options: .literal) + "'" } } diff --git a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift index 916bde30e1..4af87436c1 100644 --- a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift +++ b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift @@ -278,6 +278,10 @@ public protocol PluginDatabaseDriver: AnyObject, Sendable { /// temporary objects, an open transaction, or settings the user changed are all reasons to keep /// it and say so. Throwing is reserved for a release that was attempted and failed. func releaseIdleResource() async throws -> PluginResourceRelease + + // Builds a new database file from a folder the engine exported, in an engine instance of its own that can reach no + // file outside `folder`, because the folder's SQL is untrusted. Nothing runs on this connection. + func importExportedDatabase(from folder: URL, into file: URL) async throws var serverVersion: String? { get } /// The warning the server sent with a login that succeeded, in the server's own words, such as an Oracle /// password in its grace period, or `nil` when there was none. The app reads it once `connect()` returns. diff --git a/apple/Plugins/TableProPluginKit/PluginDriverUnsupportedOperation.swift b/apple/Plugins/TableProPluginKit/PluginDriverUnsupportedOperation.swift index c3b27ffe5b..75d947a168 100644 --- a/apple/Plugins/TableProPluginKit/PluginDriverUnsupportedOperation.swift +++ b/apple/Plugins/TableProPluginKit/PluginDriverUnsupportedOperation.swift @@ -21,6 +21,7 @@ public enum PluginDriverUnsupportedOperation: Error, LocalizedError, Sendable { case renameSchema case writeDocument case explainPlan + case importExportedDatabase public var errorDescription: String? { switch self { @@ -34,6 +35,8 @@ public enum PluginDriverUnsupportedOperation: Error, LocalizedError, Sendable { return String(localized: "This database does not store whole documents") case .explainPlan: return String(localized: "This driver cannot explain a statement") + case .importExportedDatabase: + return String(localized: "This driver cannot restore an exported folder. Update it in Settings > Extensions.") } } } diff --git a/apple/Plugins/TableProPluginKit/PluginExportedDatabaseImport.swift b/apple/Plugins/TableProPluginKit/PluginExportedDatabaseImport.swift new file mode 100644 index 0000000000..3919dc2773 --- /dev/null +++ b/apple/Plugins/TableProPluginKit/PluginExportedDatabaseImport.swift @@ -0,0 +1,12 @@ +// +// PluginExportedDatabaseImport.swift +// TableProPluginKit +// + +import Foundation + +public extension PluginDatabaseDriver { + func importExportedDatabase(from folder: URL, into file: URL) async throws { + throw PluginDriverUnsupportedOperation.importExportedDatabase + } +} diff --git a/apple/TablePro/Core/Database/DatabaseDriver.swift b/apple/TablePro/Core/Database/DatabaseDriver.swift index 3d4de75985..d812cc62f7 100644 --- a/apple/TablePro/Core/Database/DatabaseDriver.swift +++ b/apple/TablePro/Core/Database/DatabaseDriver.swift @@ -69,6 +69,8 @@ protocol DatabaseDriver: AnyObject, Sendable { /// not restore what the session is currently holding. func releaseIdleResource() async throws -> PluginResourceRelease + func importExportedDatabase(from folder: URL, into file: URL) async throws + func resolveQueryCompletionProfile( databaseTypeId: String, base: QueryCompletionProfile @@ -685,6 +687,10 @@ extension DatabaseDriver { func releaseIdleResource() async throws -> PluginResourceRelease { .nothingToRelease } + func importExportedDatabase(from folder: URL, into file: URL) async throws { + throw PluginDriverUnsupportedOperation.importExportedDatabase + } + func testConnection() async throws -> Bool { try await connect() disconnect() diff --git a/apple/TablePro/Core/Database/InProcessDumpRunner.swift b/apple/TablePro/Core/Database/InProcessDumpRunner.swift index 84766c95ec..feafd44503 100644 --- a/apple/TablePro/Core/Database/InProcessDumpRunner.swift +++ b/apple/TablePro/Core/Database/InProcessDumpRunner.swift @@ -39,11 +39,13 @@ final class InProcessDumpRunner: NativeDumpRunner, @unchecked Sendable { } } + // The task's cancellation reaches the isolated import, which `cancelQuery` on the session driver cannot. func cancel() { stateLock.lock() wasCancelled = true let driver = runningDriver stateLock.unlock() + task?.cancel() try? driver?.cancelQuery() } @@ -63,8 +65,15 @@ final class InProcessDumpRunner: NativeDumpRunner, @unchecked Sendable { } private func execute() async { - let statements = job.statements - let cleanup = job.cleanupStatements + let result = await run() + if let isolated = job.isolatedImport { + try? FileManager.default.removeItem(at: isolated.directory) + } + finish(result) + } + + private func run() async -> NativeDumpRunResult { + let job = self.job let adopt: @Sendable (DatabaseDriver?) -> Void = { [weak self] driver in guard let self else { return } self.stateLock.lock() @@ -80,35 +89,48 @@ final class InProcessDumpRunner: NativeDumpRunner, @unchecked Sendable { ) { driver in adopt(driver) defer { adopt(nil) } + if let isolated = job.isolatedImport { + do { + try await driver.importExportedDatabase(from: isolated.archive, into: isolated.file) + } catch { + throw IsolatedImportFailure(underlying: error) + } + } do { - for statement in statements { + for statement in job.statements { + try Task.checkCancellation() _ = try await driver.execute(query: statement) } } catch { - /// Best effort and deliberately ignored. DuckDB aborts the whole transaction on - /// a failed `COPY FROM DATABASE`, so the `DETACH` that would tidy up fails too, - /// and the caller has the real error to report instead of this one. The alias is - /// unique per run, so a leaked attachment blocks nothing. - for statement in cleanup { + // Ignored: the DETACH can fail in the transaction the COPY aborted, and the alias is unique per run. + for statement in job.cleanupStatements { _ = try? await driver.execute(query: statement) } throw error } } - finish(NativeDumpRunResult(exitCode: 0, stderr: "", wasCancelled: readCancelled())) + return NativeDumpRunResult(exitCode: 0, stderr: "", wasCancelled: readCancelled()) } catch { let cancelled = readCancelled() - Self.logger.error("in-engine dump failed: \(error.publicLogShape, privacy: .public)") - finish( - NativeDumpRunResult( - exitCode: cancelled ? 130 : 1, - stderr: cancelled ? "" : error.localizedDescription, - wasCancelled: cancelled - ) + let isolated = error as? IsolatedImportFailure + Self.logger.error("in-engine dump failed: \((isolated?.underlying ?? error).publicLogShape, privacy: .public)") + return NativeDumpRunResult( + exitCode: cancelled ? 130 : 1, + stderr: cancelled ? "" : Self.message(for: error), + wasCancelled: cancelled, + targetUntouched: isolated != nil ) } } + private static func message(for error: any Error) -> String { + guard let isolated = error as? IsolatedImportFailure else { return error.localizedDescription } + return String( + format: String(localized: "The backup could not be imported, so this database was not changed. %@"), + isolated.underlying.localizedDescription + ) + } + private func readCancelled() -> Bool { stateLock.lock() defer { stateLock.unlock() } @@ -129,3 +151,7 @@ final class InProcessDumpRunner: NativeDumpRunner, @unchecked Sendable { pending?.resume(returning: result) } } + +private struct IsolatedImportFailure: Error { + let underlying: any Error +} diff --git a/apple/TablePro/Core/Database/MySQLClientArguments.swift b/apple/TablePro/Core/Database/MySQLClientArguments.swift index 185ca3aadd..ff26fd0624 100644 --- a/apple/TablePro/Core/Database/MySQLClientArguments.swift +++ b/apple/TablePro/Core/Database/MySQLClientArguments.swift @@ -76,6 +76,35 @@ enum MySQLClientArguments { MySQLLocalSocket.path(in: connection.additionalFields) == nil && !connection.tunnelSecuresTransport } + // A restore pipes the file into the client, which also reads its own commands from it: `\!` runs + // a shell command and `\.` reads a file. `--binary-mode` turns off every one of them but + // charset and delimiter for piped input in both families; the family's own switch is added + // where the client's version has it. + static func restoreSafety(tool: NativeDumpResolvedTool) -> [String] { + let flags = ["--binary-mode", "--local-infile=0"] + guard let release = MySQLDumpToolIdentifier.release(fromVersionText: tool.versionText) else { return flags } + switch tool.flavor { + case .mysql where skipSystemCommandReleases.contains(where: { $0.contains(release) }): + return flags + ["--skip-system-command"] + case .mariadb where sandboxReleases.contains(where: { $0.contains(release) }): + return flags + ["--sandbox"] + case .mysql, .mariadb, .unidentified: + return flags + } + } + + private static let skipSystemCommandReleases: [Range] = [ + NativeDumpToolRelease(8, 0, 40)..] = [ + NativeDumpToolRelease(10, 6, 18).. [String] { ["--plugin-dir=\(folder.path)"] } diff --git a/apple/TablePro/Core/Database/MySQLDumpToolIdentifier.swift b/apple/TablePro/Core/Database/MySQLDumpToolIdentifier.swift index 8a53755d4e..ac0b88c126 100644 --- a/apple/TablePro/Core/Database/MySQLDumpToolIdentifier.swift +++ b/apple/TablePro/Core/Database/MySQLDumpToolIdentifier.swift @@ -55,11 +55,28 @@ enum MySQLDumpToolIdentifier { /// alone makes a 5.7 client look newer than an 8.4 one, which is the wrong way round for every /// question worth asking of it. static func majorVersion(fromVersionText versionText: String?) -> Int? { + release(fromVersionText: versionText, markers: [#"\bDistrib\s+"#, #"\bVer\s+"#])?.major + } + + // MariaDB 11 and later print `mariadb from 11.4.2-MariaDB, client 15.2 for ...`, with no `Ver`. + static func release(fromVersionText versionText: String?) -> NativeDumpToolRelease? { + release(fromVersionText: versionText, markers: [#"\bDistrib\s+"#, #"\bfrom\s+"#, #"\bVer\s+"#]) + } + + private static func release(fromVersionText versionText: String?, markers: [String]) -> NativeDumpToolRelease? { guard let versionText else { return nil } - for pattern in [#"\bDistrib\s+"#, #"\bVer\s+"#] { + for pattern in markers { guard let marker = versionText.range(of: pattern, options: .regularExpression) else { continue } - let digits = versionText[marker.upperBound...].prefix { $0.isNumber } - if let major = Int(digits) { return major } + let numbers = versionText[marker.upperBound...] + .prefix { $0.isASCII && ($0.isNumber || $0 == ".") } + .split(separator: ".") + .compactMap { Int($0) } + guard let major = numbers.first else { continue } + return NativeDumpToolRelease( + major: major, + minor: numbers.count > 1 ? numbers[1] : 0, + patch: numbers.count > 2 ? numbers[2] : 0 + ) } return nil } diff --git a/apple/TablePro/Core/Database/NativeDumpArgumentQuoting.swift b/apple/TablePro/Core/Database/NativeDumpArgumentQuoting.swift index 67f85f0359..be22d5b9f3 100644 --- a/apple/TablePro/Core/Database/NativeDumpArgumentQuoting.swift +++ b/apple/TablePro/Core/Database/NativeDumpArgumentQuoting.swift @@ -27,7 +27,28 @@ enum NativeDumpArgumentQuoting { } private static func quotedPatternPart(_ value: String) -> String { - "\"\(value.replacingOccurrences(of: "\"", with: "\"\""))\"" + "\"\(value.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" + } + + // libpq reads a -d value holding `=` or a URI prefix as a whole conninfo, whose host, port and + // sslmode override -h, -p and PGSSLMODE. As the dbname of a conninfo it is only a name again. + static func postgresDatabase(_ name: String) throws -> String { + try refuseNUL(name) + let escaped = name + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "'", with: "\\'", options: .literal) + return "dbname='\(escaped)'" + } + + // SqlClient ends a bare value at the next `;` and takes the last of a repeated keyword, so a + // database name could move the sign-in, password included, to another server. + static func sqlServerConnectionStringValue(_ value: String) throws -> String { + try refuseNUL(value) + return "\"\(value.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" + } + + private static func refuseNUL(_ value: String) throws { + if value.unicodeScalars.contains("\0") { throw NativeDumpError.nulInArgument } } /// The whole `.dump` invocation for a narrowed SQLite backup, as ONE process argument. @@ -55,12 +76,12 @@ enum NativeDumpArgumentQuoting { static func sqliteDumpToken(_ name: String) -> String { let pattern = name - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "%", with: "\\%") - .replacingOccurrences(of: "_", with: "\\_") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "%", with: "\\%", options: .literal) + .replacingOccurrences(of: "_", with: "\\_", options: .literal) let tokenized = pattern - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "\"", with: "\\\"") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "\"", with: "\\\"", options: .literal) return "\"\(tokenized)\"" } diff --git a/apple/TablePro/Core/Database/NativeDumpDescriptor.swift b/apple/TablePro/Core/Database/NativeDumpDescriptor.swift index 8a9d301550..1e2e64a069 100644 --- a/apple/TablePro/Core/Database/NativeDumpDescriptor.swift +++ b/apple/TablePro/Core/Database/NativeDumpDescriptor.swift @@ -52,7 +52,7 @@ struct NativeDumpDescriptor: Sendable { struct Request: Sendable { let connection: DatabaseConnection let database: String - let fileURL: URL + var fileURL: URL let password: String? let scope: NativeDumpScope @@ -239,6 +239,10 @@ struct NativeDumpDescriptor: Sendable { /// the caller has the engine's real error to report rather than this one. let cleanupStatements: @Sendable (String) -> [String] + // Set when the archive's own SQL must not run on the session: the driver imports it into a new file of this + // format in an isolated engine instance, and `restoreStatements` restore that file. + var isolatedImportFormat: ArchiveFormat? + func statements(for kind: NativeDumpKind, request: Request, alias: String) -> [String] { kind == .backup ? backupStatements(request, alias) : restoreStatements(request, alias) } diff --git a/apple/TablePro/Core/Database/NativeDumpRegistry.swift b/apple/TablePro/Core/Database/NativeDumpRegistry.swift index d8b0d64cd2..a6ed78ff4d 100644 --- a/apple/TablePro/Core/Database/NativeDumpRegistry.swift +++ b/apple/TablePro/Core/Database/NativeDumpRegistry.swift @@ -93,13 +93,19 @@ enum NativeDumpRegistry { requiresUntranslatedMessages: true, toolForServer: toolForServer, backupArguments: { request, _ in - connectionFlags(request) - + ["-Fc", "-d", request.database] + try connectionFlags(request) + + ["-Fc", "-d", NativeDumpArgumentQuoting.postgresDatabase(request.database)] + postgresTableFlags(request) + ["-f", request.fileURL.path] }, restoreArguments: { request, _ in - connectionFlags(request) + ["--no-owner", "--no-acl", "-d", request.database, request.fileURL.path] + try connectionFlags(request) + [ + "--no-owner", + "--no-acl", + "-d", + NativeDumpArgumentQuoting.postgresDatabase(request.database), + request.fileURL.path + ] }, environment: { request in var environment: [String: String] = [:] @@ -212,6 +218,7 @@ enum NativeDumpRegistry { }, restoreArguments: { request, resolved in try mysqlConnectionFlags(request, resolved) + + MySQLClientArguments.restoreSafety(tool: resolved) + ["--default-character-set=utf8mb4", "--", request.database] }, environment: { request in @@ -436,15 +443,15 @@ enum NativeDumpRegistry { restoreDelivery: .toolWritesFile, exposesPasswordInArguments: true, backupArguments: { request, _ in - ["/Action:Export", - "/TargetFile:\(request.fileURL.path)", - "/SourceConnectionString:\(sqlServerConnectionString(request))"] + try ["/Action:Export", + "/TargetFile:\(request.fileURL.path)", + "/SourceConnectionString:\(sqlServerConnectionString(request))"] + request.scope.objects.map { "/p:TableData=\(sqlServerTableData($0))" } }, restoreArguments: { request, _ in - ["/Action:Import", - "/SourceFile:\(request.fileURL.path)", - "/TargetConnectionString:\(sqlServerConnectionString(request))"] + try ["/Action:Import", + "/SourceFile:\(request.fileURL.path)", + "/TargetConnectionString:\(sqlServerConnectionString(request))"] } ) ), @@ -472,16 +479,17 @@ enum NativeDumpRegistry { /// string is the only channel it has. That puts the password in `argv`, where `ps` can read it, /// so the string is built here and `NativeDumpService` keeps SQL Server out of the flows that /// promise otherwise. Anything better needs Entra or integrated auth, which have no password. - private static func sqlServerConnectionString(_ request: NativeDumpDescriptor.Request) -> String { - var parts = [ - "Server=\(request.host),\(request.connection.port)", - "Database=\(request.database)" + private static func sqlServerConnectionString(_ request: NativeDumpDescriptor.Request) throws -> String { + let quoted = NativeDumpArgumentQuoting.sqlServerConnectionStringValue + var parts = try [ + "Server=\(quoted("\(request.host),\(request.connection.port)"))", + "Database=\(quoted(request.database))" ] if request.connection.username.isEmpty { parts.append("Integrated Security=true") } else { - parts.append("User ID=\(request.connection.username)") - parts.append("Password=\(request.password ?? "")") + try parts.append("User ID=\(quoted(request.connection.username))") + try parts.append("Password=\(quoted(request.password ?? ""))") } parts.append(request.connection.sslConfig.isEnabled ? "Encrypt=true" : "Encrypt=false") if request.connection.sslConfig.mode == .required || request.connection.sslConfig.mode == .preferred { @@ -507,15 +515,18 @@ enum NativeDumpRegistry { installHint: String(localized: "Install it with “brew install sqlite” and link it."), backupDelivery: .standardOutput, restoreDelivery: .standardOutput, + // `.shell`, `.system` and writefile() run from a dump read on stdin unless `-safe` + // is given. Not `-readonly`: it cannot open a WAL database whose -wal and -shm are gone. backupArguments: { request, _ in [ + "-safe", sqlitePath(request), NativeDumpArgumentQuoting.sqliteDumpCommand( names: request.scope.objects.map(\.name) ) ] }, - restoreArguments: { request, _ in [sqlitePath(request)] } + restoreArguments: { request, _ in ["-safe", "-bail", sqlitePath(request)] } ) ), archiveFormat: NativeDumpDescriptor.ArchiveFormat( @@ -548,37 +559,9 @@ enum NativeDumpRegistry { producesDirectory: true ) - /// One `.duckdb` file, written by the engine already in front of the app. - /// - /// Measured against the vendored libduckdb v2.0.0: `ATTACH` plus `COPY FROM DATABASE` carries - /// tables, views, indexes, foreign keys and sequences, copying tables in foreign-key order, and - /// restores with the same statement reversed. A table whose foreign key references itself, with - /// rows that use it, still fails to copy. The source catalog comes from `current_database()`, because DuckDB derives a - /// catalog name from the file's basename: a file `my-weird.name.db` attaches as `my-weird`, and - /// `COPY FROM DATABASE main` fails with `Catalog "main" does not exist`. private static var duckDBFile: NativeDumpDescriptor { NativeDumpDescriptor( - mechanism: .engineStatements( - NativeDumpDescriptor.EngineStatements( - backupStatements: { request, alias in - guard let source = request.currentCatalog, !source.isEmpty else { return [] } - return [ - "ATTACH \(quotedLiteral(request.fileURL.path)) AS \(quotedIdentifier(alias))", - "COPY FROM DATABASE \(quotedIdentifier(source)) TO \(quotedIdentifier(alias))", - "DETACH \(quotedIdentifier(alias))" - ] - }, - restoreStatements: { request, alias in - guard let target = request.currentCatalog, !target.isEmpty else { return [] } - return [ - "ATTACH \(quotedLiteral(request.fileURL.path)) AS \(quotedIdentifier(alias)) (READ_ONLY)", - "COPY FROM DATABASE \(quotedIdentifier(alias)) TO \(quotedIdentifier(target))", - "DETACH \(quotedIdentifier(alias))" - ] - }, - cleanupStatements: { alias in ["DETACH \(quotedIdentifier(alias))"] } - ) - ), + mechanism: .engineStatements(duckDBFileStatements), archiveFormat: duckDBFileFormat, objectScope: .unsupported( reason: String(localized: "DuckDB copies the whole database. There is no table filter.") @@ -587,9 +570,32 @@ enum NativeDumpRegistry { ) } - /// A folder of Parquet next to `schema.sql` and `load.sql`, which tools other than DuckDB can - /// read. `EXPORT DATABASE` merges into a folder that already holds one, leaving the previous - /// run's data files behind, so the caller empties the destination first. + // The catalog is the session's, never `main`: DuckDB names one after the file, so `my-weird.name.db` attaches as `my-weird`. + private static let duckDBFileStatements = NativeDumpDescriptor.EngineStatements( + backupStatements: { request, alias in + guard let source = request.currentCatalog, !source.isEmpty else { return [] } + return ["ATTACH \(quotedLiteral(request.fileURL.path)) AS \(quotedIdentifier(alias))"] + + copyDatabase(from: source, to: alias) + + ["DETACH \(quotedIdentifier(alias))"] + }, + restoreStatements: { request, alias in + guard let target = request.currentCatalog, !target.isEmpty else { return [] } + return ["ATTACH \(quotedLiteral(request.fileURL.path)) AS \(quotedIdentifier(alias)) (READ_ONLY)"] + + copyDatabase(from: alias, to: target) + + ["DETACH \(quotedIdentifier(alias))"] + }, + cleanupStatements: { alias in ["DETACH \(quotedIdentifier(alias))"] } + ) + + // Without an option, COPY FROM DATABASE expands to these two statements, and a prepared statement holds only one. + private static func copyDatabase(from source: String, to target: String) -> [String] { + ["SCHEMA", "DATA"].map { part in + "COPY FROM DATABASE \(quotedIdentifier(source)) TO \(quotedIdentifier(target)) (\(part))" + } + } + + // EXPORT DATABASE merges into an earlier export, so the caller empties the folder first. On restore the folder's + // schema.sql and load.sql run only in the driver's isolated instance, and the session restores the file it builds. private static var duckDBParquet: NativeDumpDescriptor { NativeDumpDescriptor( mechanism: .engineStatements( @@ -597,10 +603,9 @@ enum NativeDumpRegistry { backupStatements: { request, _ in ["EXPORT DATABASE \(quotedLiteral(request.fileURL.path)) (FORMAT PARQUET)"] }, - restoreStatements: { request, _ in - ["IMPORT DATABASE \(quotedLiteral(request.fileURL.path))"] - }, - cleanupStatements: { _ in [] } + restoreStatements: duckDBFileStatements.restoreStatements, + cleanupStatements: duckDBFileStatements.cleanupStatements, + isolatedImportFormat: duckDBFileFormat ) ), archiveFormat: duckDBParquetFormat, @@ -612,10 +617,10 @@ enum NativeDumpRegistry { } private static func quotedLiteral(_ value: String) -> String { - "'\(value.replacingOccurrences(of: "'", with: "''"))'" + "'\(value.replacingOccurrences(of: "'", with: "''", options: .literal))'" } private static func quotedIdentifier(_ value: String) -> String { - "\"\(value.replacingOccurrences(of: "\"", with: "\"\""))\"" + "\"\(value.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } } diff --git a/apple/TablePro/Core/Database/NativeDumpResolvedTool.swift b/apple/TablePro/Core/Database/NativeDumpResolvedTool.swift index bd81e82d99..87cc3772e4 100644 --- a/apple/TablePro/Core/Database/NativeDumpResolvedTool.swift +++ b/apple/TablePro/Core/Database/NativeDumpResolvedTool.swift @@ -16,6 +16,26 @@ enum NativeDumpToolFlavor: String, Equatable, Sendable { case mariadb } +struct NativeDumpToolRelease: Comparable, Sendable { + let major: Int + let minor: Int + let patch: Int + + init(_ major: Int, _ minor: Int, _ patch: Int) { + self.init(major: major, minor: minor, patch: patch) + } + + init(major: Int, minor: Int, patch: Int) { + self.major = major + self.minor = minor + self.patch = patch + } + + static func < (lhs: Self, rhs: Self) -> Bool { + (lhs.major, lhs.minor, lhs.patch) < (rhs.major, rhs.minor, rhs.patch) + } +} + /// The binary a dump or restore is about to run, and what the app knows about it. /// /// It exists because an argument list is not a function of the connection alone. MariaDB renamed diff --git a/apple/TablePro/Core/Database/NativeDumpService.swift b/apple/TablePro/Core/Database/NativeDumpService.swift index c51a08ef14..f7f58ebbf9 100644 --- a/apple/TablePro/Core/Database/NativeDumpService.swift +++ b/apple/TablePro/Core/Database/NativeDumpService.swift @@ -43,6 +43,7 @@ enum NativeDumpError: LocalizedError, Equatable { case engineStatementUnavailable case incompatibleTool(message: String) case unreachableServer(message: String) + case nulInArgument var errorDescription: String? { switch self { @@ -64,6 +65,8 @@ enum NativeDumpError: LocalizedError, Equatable { return String(localized: "TablePro could not read the database's own name from the connection.") case .incompatibleTool(let message), .unreachableServer(let message): return message + case .nulInArgument: + return String(localized: "The database name or a sign-in value holds a NUL character, which a command line cannot carry.") } } } @@ -141,6 +144,15 @@ struct NativeDumpStatementJob: Sendable, Equatable { /// fails too, and the caller has the real error to report instead of this one. let cleanupStatements: [String] let scope: DatabaseScope + var isolatedImport: NativeDumpIsolatedImport? +} + +// The driver imports `archive` into `file` in an isolated engine instance before `statements` run. `directory` holds +// only that file and is removed when the run ends. +struct NativeDumpIsolatedImport: Sendable, Equatable { + let archive: URL + let directory: URL + let file: URL } /// What a run is, so the service can pick a runner without either runner accepting a shape it @@ -163,8 +175,14 @@ internal extension NativeDumpJob { // For a job that never reaches a runner, which is what removes these otherwise. func discardTemporaryFiles() { - guard case .process(let command) = self else { return } - for url in [command.temporaryCredentialsFileURL, command.clientPluginFolder?.url].compactMap(\.self) { + let urls: [URL?] + switch self { + case .process(let command): + urls = [command.temporaryCredentialsFileURL, command.clientPluginFolder?.url] + case .statements(let job): + urls = [job.isolatedImport?.directory] + } + for url in urls.compactMap(\.self) { try? FileManager.default.removeItem(at: url) } } @@ -175,6 +193,7 @@ struct NativeDumpRunResult: Equatable { let exitCode: Int32 let stderr: String let wasCancelled: Bool + var targetUntouched = false } /// Runs one backup or restore and reports how it ended. Abstracted so the state machine can be @@ -339,25 +358,18 @@ final class NativeDumpService: ObservableObject { try run(job: .process(command), database: database, fileURL: fileURL, totalBytesEstimate: totalBytesEstimate) case .engineStatements(let engine): - let alias = Self.backupAlias() - let statements = engine.statements(for: kind, request: request, alias: alias) - guard !statements.isEmpty else { throw NativeDumpError.engineStatementUnavailable } - toolName = connection.type.rawValue let scopeForRun = DatabaseManager.shared.resolvedScope( database: database, schema: nil, for: connection.id ) ?? DatabaseScope(connectionId: connection.id, database: database, schema: nil) - try run( - job: .statements( - NativeDumpStatementJob( - statements: statements, - cleanupStatements: engine.cleanupStatements(alias), - scope: scopeForRun - ) - ), - database: database, - fileURL: fileURL, - totalBytesEstimate: totalBytesEstimate + let job = try Self.statementJob( + kind: kind, + engine: engine, + request: request, + alias: Self.backupAlias(), + scope: scopeForRun ) + toolName = connection.type.rawValue + try run(job: .statements(job), database: database, fileURL: fileURL, totalBytesEstimate: totalBytesEstimate) } jobStarted = true @@ -555,6 +567,45 @@ final class NativeDumpService: ObservableObject { return picked.isEmpty ? nil : picked } + nonisolated static func statementJob( + kind: NativeDumpKind, + engine: NativeDumpDescriptor.EngineStatements, + request: NativeDumpDescriptor.Request, + alias: String, + scope: DatabaseScope + ) throws -> NativeDumpStatementJob { + guard kind == .restore, let format = engine.isolatedImportFormat else { + let statements = engine.statements(for: kind, request: request, alias: alias) + guard !statements.isEmpty else { throw NativeDumpError.engineStatementUnavailable } + return NativeDumpStatementJob( + statements: statements, + cleanupStatements: engine.cleanupStatements(alias), + scope: scope + ) + } + let directory = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-restore-\(UUID().uuidString)", isDirectory: true) + var imported = request + imported.fileURL = directory.appendingPathComponent("import").appendingPathExtension(format.fileExtension) + let statements = engine.restoreStatements(imported, alias) + guard !statements.isEmpty else { throw NativeDumpError.engineStatementUnavailable } + try FileManager.default.createDirectory( + at: directory, + withIntermediateDirectories: false, + attributes: [.posixPermissions: 0o700] + ) + return NativeDumpStatementJob( + statements: statements, + cleanupStatements: engine.cleanupStatements(alias), + scope: scope, + isolatedImport: NativeDumpIsolatedImport( + archive: request.fileURL, + directory: directory, + file: imported.fileURL + ) + ) + } + /// Unique per run, so a `DETACH` that could not execute leaves nothing behind that blocks the /// next attempt with "database with name already exists". static func backupAlias() -> String { @@ -633,8 +684,8 @@ final class NativeDumpService: ObservableObject { /// can hold without the value changing meaning. nonisolated static func mongoYAMLQuoted(_ value: String) -> String { let escaped = value - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "\"", with: "\\\"") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "\"", with: "\\\"", options: .literal) return "\"\(escaped)\"" } @@ -723,7 +774,7 @@ final class NativeDumpService: ObservableObject { ?? (result.stderr.isEmpty ? String(format: String(localized: "Process exited with code %d"), Int(result.exitCode)) : result.stderr) - setState(.failed(message: summary, targetMayBeModified: kind == .restore)) + setState(.failed(message: summary, targetMayBeModified: kind == .restore && !result.targetUntouched)) Self.logger.error("\(self.toolName, privacy: .public) failed code=\(result.exitCode) db=\(database, privacy: .private(mask: .hash)) stderr=\(result.stderr)") } diff --git a/apple/TablePro/Core/Database/RemoteDatabaseFileTransfer.swift b/apple/TablePro/Core/Database/RemoteDatabaseFileTransfer.swift index d5b5a50933..a8cd0fb9d4 100644 --- a/apple/TablePro/Core/Database/RemoteDatabaseFileTransfer.swift +++ b/apple/TablePro/Core/Database/RemoteDatabaseFileTransfer.swift @@ -400,7 +400,7 @@ enum RemoteDatabaseFileTransfer { /// Quotes a path for a SQL string literal, which is not the same job as quoting it for the /// shell: SQL escapes a single quote by doubling it, and the shell cannot. private static func sqlStringLiteral(_ value: String) -> String { - "'" + value.replacingOccurrences(of: "'", with: "''") + "'" + "'" + value.replacingOccurrences(of: "'", with: "''", options: .literal) + "'" } private static func requireLocalSpace(for bytes: UInt64, at directory: URL) throws { diff --git a/apple/TablePro/Core/Plugins/PluginDriverAdapter.swift b/apple/TablePro/Core/Plugins/PluginDriverAdapter.swift index c32a51f3aa..dd6b0e175a 100644 --- a/apple/TablePro/Core/Plugins/PluginDriverAdapter.swift +++ b/apple/TablePro/Core/Plugins/PluginDriverAdapter.swift @@ -394,6 +394,10 @@ final class PluginDriverAdapter: DatabaseDriver, SchemaSwitchable, DatabaseRepor try await pluginDriver.releaseIdleResource() } + func importExportedDatabase(from folder: URL, into file: URL) async throws { + try await pluginDriver.importExportedDatabase(from: folder, into: file) + } + func resolveQueryCompletionProfile( databaseTypeId: String, base: QueryCompletionProfile diff --git a/apple/TablePro/Core/SSH/HostKeyStore.swift b/apple/TablePro/Core/SSH/HostKeyStore.swift index 24c4892fca..1e2eaeca97 100644 --- a/apple/TablePro/Core/SSH/HostKeyStore.swift +++ b/apple/TablePro/Core/SSH/HostKeyStore.swift @@ -10,6 +10,7 @@ import CryptoKit import Foundation import os +import TableProSSHTransport /// Manages SSH host key verification for known hosts internal final class HostKeyStore: @unchecked Sendable { @@ -21,6 +22,9 @@ internal final class HostKeyStore: @unchecked Sendable { case trusted case unknown(fingerprint: String, keyType: String) case mismatch(expected: String, actual: String) + // The file is one entry per line split on spaces, so a host with a line break would plant + // an entry for another host. + case invalidHost(SSHDestinationName.Problem) } private let filePath: String @@ -39,18 +43,13 @@ internal final class HostKeyStore: @unchecked Sendable { // MARK: - Public API - /// Verify a host key against the known_hosts store - /// - Parameters: - /// - keyData: The raw host key bytes - /// - keyType: The key type string (e.g. "ssh-rsa", "ssh-ed25519") - /// - hostname: The remote hostname - /// - port: The remote port - /// - Returns: The verification result func verify(keyData: Data, keyType: String, hostname: String, port: Int) -> VerificationResult { lock.lock() defer { lock.unlock() } - let hostKey = hostIdentifier(hostname, port) + guard let hostKey = hostIdentifier(hostname, port) else { + return .invalidHost(.host(hostname)) + } let currentFingerprint = Self.fingerprint(of: keyData) let entries = loadEntries() @@ -72,17 +71,11 @@ internal final class HostKeyStore: @unchecked Sendable { return .mismatch(expected: storedFingerprint, actual: currentFingerprint) } - /// Add or update a trusted host key - /// - Parameters: - /// - hostname: The remote hostname - /// - port: The remote port - /// - key: The raw host key bytes - /// - keyType: The key type string func trust(hostname: String, port: Int, key: Data, keyType: String) { lock.lock() defer { lock.unlock() } - let hostKey = hostIdentifier(hostname, port) + guard let hostKey = hostIdentifier(hostname, port) else { return } var entries = loadEntries() entries.removeAll { $0.host == hostKey && $0.keyType == keyType } @@ -93,15 +86,11 @@ internal final class HostKeyStore: @unchecked Sendable { Self.logger.info("Trusted host key for \(hostKey) (\(keyType))") } - /// Remove a stored host key - /// - Parameters: - /// - hostname: The remote hostname - /// - port: The remote port func remove(hostname: String, port: Int) { lock.lock() defer { lock.unlock() } - let hostKey = hostIdentifier(hostname, port) + guard let hostKey = hostIdentifier(hostname, port) else { return } var entries = loadEntries() let countBefore = entries.count @@ -143,9 +132,12 @@ internal final class HostKeyStore: @unchecked Sendable { // MARK: - Private Helpers - /// Build the host identifier string: [hostname]:port - private func hostIdentifier(_ hostname: String, _ port: Int) -> String { - "[\(hostname)]:\(port)" + private func hostIdentifier(_ hostname: String, _ port: Int) -> String? { + guard SSHDestinationName.isValidHost(hostname) else { + Self.logger.warning("Refusing a host key entry for a host ssh would refuse") + return nil + } + return "[\(hostname)]:\(port)" } /// Load all entries from the known_hosts file diff --git a/apple/TablePro/Core/SSH/HostKeyVerifier.swift b/apple/TablePro/Core/SSH/HostKeyVerifier.swift index cf961319b7..52ad7f9004 100644 --- a/apple/TablePro/Core/SSH/HostKeyVerifier.swift +++ b/apple/TablePro/Core/SSH/HostKeyVerifier.swift @@ -9,6 +9,7 @@ import AppKit import Foundation import os +import TableProSSHTransport /// Handles host key verification with UI prompts internal enum HostKeyVerifier { @@ -42,6 +43,9 @@ internal enum HostKeyVerifier { logger.debug("Host key trusted for [\(hostname)]:\(port)") return + case .invalidHost(let problem): + throw SSHTunnelError.tunnelCreationFailed(problem.explanation) + case .unknown(let fingerprint, let keyType): logger.info("Unknown host key for [\(hostname)]:\(port), prompting user") let accepted = try await promptUnknownHost( diff --git a/apple/TablePro/Core/SSH/LibSSH2ExecChannel.swift b/apple/TablePro/Core/SSH/LibSSH2ExecChannel.swift index c0ef7bdbed..28ed4ec012 100644 --- a/apple/TablePro/Core/SSH/LibSSH2ExecChannel.swift +++ b/apple/TablePro/Core/SSH/LibSSH2ExecChannel.swift @@ -45,7 +45,7 @@ enum LibSSH2ExecChannel { /// and the only character they cannot carry is a single quote, which is closed, escaped and /// reopened. A remote path is the only thing the app ever passes through here. static func shellQuoted(_ value: String) -> String { - "'" + value.replacingOccurrences(of: "'", with: "'\\''") + "'" + "'" + value.replacingOccurrences(of: "'", with: "'\\''", options: .literal) + "'" } static func runSerialized( diff --git a/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift b/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift index d59a6bf610..21c64f1398 100644 --- a/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift +++ b/apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift @@ -224,7 +224,7 @@ internal enum LibSSH2TunnelFactory { let firstHop = resolved.firstHop let firstEndpoint = timeoutEndpoint(host: firstHop.host, port: firstHop.port) let socketFD = try await connectTCP( - host: firstHop.host, + host: firstHop.dialHost, port: firstHop.port, deadline: deadline, attempt: attempt @@ -264,7 +264,7 @@ internal enum LibSSH2TunnelFactory { try openChannel( session: currentSession, socketFD: currentSocketFD, - remoteHost: nextResolved.host, + remoteHost: nextResolved.dialHost, remotePort: nextResolved.port, endpoint: nextEndpoint, deadline: deadline, diff --git a/apple/TablePro/Core/Services/Infrastructure/CommandLineShim.swift b/apple/TablePro/Core/Services/Infrastructure/CommandLineShim.swift index 1c930cee14..62ec5e172c 100644 --- a/apple/TablePro/Core/Services/Infrastructure/CommandLineShim.swift +++ b/apple/TablePro/Core/Services/Infrastructure/CommandLineShim.swift @@ -52,6 +52,6 @@ internal struct CommandLineShim: Sendable { } internal static func quote(_ value: String) -> String { - "'" + value.replacingOccurrences(of: "'", with: "'\\''") + "'" + "'" + value.replacingOccurrences(of: "'", with: "'\\''", options: .literal) + "'" } } diff --git a/apple/TablePro/Core/Utilities/Connection/PasswordSourceResolver.swift b/apple/TablePro/Core/Utilities/Connection/PasswordSourceResolver.swift index 5c710e55a5..ad6806986e 100644 --- a/apple/TablePro/Core/Utilities/Connection/PasswordSourceResolver.swift +++ b/apple/TablePro/Core/Utilities/Connection/PasswordSourceResolver.swift @@ -147,7 +147,7 @@ enum PasswordSourceResolver { } static func shellQuote(_ value: String) -> String { - "'" + value.replacingOccurrences(of: "'", with: "'\\''") + "'" + "'" + value.replacingOccurrences(of: "'", with: "'\\''", options: .literal) + "'" } static func extractJsonField(_ key: String, from json: String) throws -> String { diff --git a/apple/TablePro/Models/Connection/TunnelCommandBuilder.swift b/apple/TablePro/Models/Connection/TunnelCommandBuilder.swift index fdd813fcd3..a2d800d09d 100644 --- a/apple/TablePro/Models/Connection/TunnelCommandBuilder.swift +++ b/apple/TablePro/Models/Connection/TunnelCommandBuilder.swift @@ -226,6 +226,6 @@ enum TunnelCommandBuilder { private static func displayQuoted(_ token: String) -> String { guard token.rangeOfCharacter(from: .whitespacesAndNewlines) != nil else { return token } - return "'" + token.replacingOccurrences(of: "'", with: "'\\''") + "'" + return "'" + token.replacingOccurrences(of: "'", with: "'\\''", options: .literal) + "'" } } diff --git a/apple/TablePro/Models/TeamLibrary/TeamLibraryModels.swift b/apple/TablePro/Models/TeamLibrary/TeamLibraryModels.swift index 7e91b0b2fe..5cef64a6c3 100644 --- a/apple/TablePro/Models/TeamLibrary/TeamLibraryModels.swift +++ b/apple/TablePro/Models/TeamLibrary/TeamLibraryModels.swift @@ -110,7 +110,7 @@ struct TeamLibraryPullResponse: Codable { /// confirmation step, so anything the app would act on by itself is dropped before caching. func sanitized() -> TeamLibraryPullResponse { TeamLibraryPullResponse( - connections: connections.map { + connections: connections.filter { $0.payload.refusedSSHDestination == nil }.map { Connection( id: $0.id, sourceConnectionId: $0.sourceConnectionId, diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 18e613f04c..9a79216826 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -5479,6 +5479,18 @@ } } }, + "%@ was not found in any of the CanonicalDomains in ~/.ssh/config, and CanonicalizeFallbackLocal is set to no." : { + "extractionState" : "manual", + "generatesSymbol" : false, + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "%@ est introuvable dans les CanonicalDomains de ~/.ssh/config, et CanonicalizeFallbackLocal vaut no." + } + } + } + }, "%@ was not found." : { "localizations" : { "fr" : { @@ -183318,6 +183330,16 @@ } } }, + "The backup could not be imported, so this database was not changed. %@" : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "La sauvegarde n’a pas pu être importée, cette base de données n’a donc pas été modifiée. %@" + } + } + } + }, "The batch before it stays applied." : { "localizations" : { "fr" : { @@ -185434,6 +185456,16 @@ } } }, + "The database name or a sign-in value holds a NUL character, which a command line cannot carry." : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Le nom de la base de données ou une valeur de connexion contient un caractère NUL, qu’une ligne de commande ne peut pas transmettre." + } + } + } + }, "The database server enforced this, not TablePro's Safe Mode. You are most likely connected to a read replica, or the server sets new transactions to read-only. Connect to the primary server to write.\n\nServer response: %@" : { "localizations" : { "fr" : { @@ -191121,6 +191153,30 @@ } } }, + "The SSH host %@ contains characters ssh does not allow in a host name." : { + "extractionState" : "manual", + "generatesSymbol" : false, + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "L’hôte SSH %@ contient des caractères que ssh n’accepte pas dans un nom d’hôte." + } + } + } + }, + "the SSH host or user of “%@” contains characters ssh does not allow" : { + "extractionState" : "manual", + "generatesSymbol" : false, + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "l’hôte ou l’utilisateur SSH de « %@ » contient des caractères que ssh n’accepte pas" + } + } + } + }, "The SSH server closed the connection during the handshake." : { }, @@ -191405,6 +191461,18 @@ } } }, + "The SSH user %@ contains characters ssh does not allow in a user name." : { + "extractionState" : "manual", + "generatesSymbol" : false, + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "L’utilisateur SSH %@ contient des caractères que ssh n’accepte pas dans un nom d’utilisateur." + } + } + } + }, "The ssh-agent macOS runs, from SSH_AUTH_SOCK. 1Password and Secretive listen elsewhere." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -195746,6 +195814,18 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "This driver cannot restore an exported folder. Update it in Settings > Extensions." : { + "extractionState" : "manual", + "generatesSymbol" : false, + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Ce pilote ne peut pas restaurer un dossier exporté. Mettez-le à jour dans Réglages > Extensions." + } + } + } + }, "This driver has no TLS fallback. Preferred forces TLS, same as Required." : { "localizations" : { "fr" : { diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index d20e9d9398..6cde5cd5f6 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -55,6 +55,18 @@ }, "The row's loaded values are missing, so it cannot be found again." : { + }, + "The SSH host %@ contains characters ssh does not allow in a host name." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "the SSH host or user of “%@” contains characters ssh does not allow" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "The SSH user %@ contains characters ssh does not allow in a user name." : { + "extractionState" : "manual", + "generatesSymbol" : false }, "“%@” has an open transaction. Deleting the connection rolls it back and discards its uncommitted changes." : { @@ -26896,6 +26908,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "This driver cannot restore an exported folder. Update it in Settings > Extensions." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "This file also holds saved queries, which import only on a Mac." : { "localizations" : { "ko" : { diff --git a/apple/TableProMobile/TableProMobile/SSH/HostKeyStore.swift b/apple/TableProMobile/TableProMobile/SSH/HostKeyStore.swift index 4e9b5c6186..e75ee733d7 100644 --- a/apple/TableProMobile/TableProMobile/SSH/HostKeyStore.swift +++ b/apple/TableProMobile/TableProMobile/SSH/HostKeyStore.swift @@ -9,6 +9,7 @@ import CryptoKit import Foundation import os +import TableProSSHTransport nonisolated final class HostKeyStore: @unchecked Sendable { static let shared = HostKeyStore() @@ -19,6 +20,9 @@ nonisolated final class HostKeyStore: @unchecked Sendable { case trusted case unknown(fingerprint: String, keyType: String) case mismatch(expected: String, actual: String) + // The file is one entry per line split on spaces, so a host with a line break would plant + // an entry for another host. + case invalidHost(SSHDestinationName.Problem) } private let filePath: String @@ -43,7 +47,9 @@ nonisolated final class HostKeyStore: @unchecked Sendable { lock.lock() defer { lock.unlock() } - let hostKey = hostIdentifier(hostname, port) + guard let hostKey = hostIdentifier(hostname, port) else { + return .invalidHost(.host(hostname)) + } let currentFingerprint = Self.fingerprint(of: keyData) let entries = loadEntries() @@ -68,7 +74,7 @@ nonisolated final class HostKeyStore: @unchecked Sendable { lock.lock() defer { lock.unlock() } - let hostKey = hostIdentifier(hostname, port) + guard let hostKey = hostIdentifier(hostname, port) else { return } var entries = loadEntries() entries.removeAll { $0.host == hostKey && $0.keyType == keyType } entries.append((host: hostKey, keyType: keyType, keyData: key)) @@ -79,7 +85,7 @@ nonisolated final class HostKeyStore: @unchecked Sendable { lock.lock() defer { lock.unlock() } - let hostKey = hostIdentifier(hostname, port) + guard let hostKey = hostIdentifier(hostname, port) else { return } var entries = loadEntries() entries.removeAll { $0.host == hostKey } saveEntries(entries) @@ -108,8 +114,12 @@ nonisolated final class HostKeyStore: @unchecked Sendable { return "SHA256:" + Data(digest).base64EncodedString().replacingOccurrences(of: "=", with: "") } - private func hostIdentifier(_ hostname: String, _ port: Int) -> String { - "[\(hostname)]:\(port)" + private func hostIdentifier(_ hostname: String, _ port: Int) -> String? { + guard SSHDestinationName.isValidHost(hostname) else { + Self.logger.warning("Refusing a host key entry for a host ssh would refuse") + return nil + } + return "[\(hostname)]:\(port)" } private func loadEntries() -> [(host: String, keyType: String, keyData: Data)] { diff --git a/apple/TableProMobile/TableProMobile/SSH/HostKeyVerifier.swift b/apple/TableProMobile/TableProMobile/SSH/HostKeyVerifier.swift index ba7e7cb9d0..408d61eb0d 100644 --- a/apple/TableProMobile/TableProMobile/SSH/HostKeyVerifier.swift +++ b/apple/TableProMobile/TableProMobile/SSH/HostKeyVerifier.swift @@ -8,6 +8,7 @@ import Foundation import TableProDatabase +import TableProSSHTransport enum HostKeyVerifier { static func verify( @@ -29,6 +30,9 @@ enum HostKeyVerifier { case .trusted: return + case let .invalidHost(problem): + throw SSHTunnelError.hostKeyRejected(problem.explanation) + case let .unknown(fingerprint, presentedType): try await decide( .unknownHostKey(host: hostname, port: port, keyType: presentedType, fingerprint: fingerprint), diff --git a/apple/TableProMobile/TableProMobileTests/SSH/HostKeyStoreTests.swift b/apple/TableProMobile/TableProMobileTests/SSH/HostKeyStoreTests.swift index 5093b222e0..53c751d900 100644 --- a/apple/TableProMobile/TableProMobileTests/SSH/HostKeyStoreTests.swift +++ b/apple/TableProMobile/TableProMobileTests/SSH/HostKeyStoreTests.swift @@ -4,6 +4,7 @@ // import Foundation +import TableProSSHTransport import Testing @testable import TableProMobile @@ -123,4 +124,31 @@ struct HostKeyStoreTests { #expect(fingerprint.hasPrefix("SHA256:")) #expect(!fingerprint.contains("=")) } + + @Test("A host with a line break plants no key for another host", arguments: [ + "replica.attacker.example\n[prod.corp.example", + "replica.attacker.example\r\n[prod.corp.example", + "replica.attacker.example [prod.corp.example", + ]) + func lineBreakInHostPlantsNoEntry(hostile: String) { + let path = makeTempFilePath() + defer { try? FileManager.default.removeItem(atPath: path) } + + let store = HostKeyStore(filePath: path) + let real = makeTestKey(0x01) + let forged = makeTestKey(0x02) + store.trust(hostname: "prod.corp.example", port: 22, key: real, keyType: "ssh-ed25519") + + #expect( + store.verify(keyData: forged, keyType: "ssh-ed25519", hostname: hostile, port: 22) + == .invalidHost(.host(hostile)) + ) + store.trust(hostname: hostile, port: 22, key: forged, keyType: "ssh-ed25519") + + #expect( + store.verify(keyData: forged, keyType: "ssh-ed25519", hostname: "prod.corp.example", port: 22) + == .mismatch(expected: HostKeyStore.fingerprint(of: real), actual: HostKeyStore.fingerprint(of: forged)) + ) + #expect(store.verify(keyData: real, keyType: "ssh-ed25519", hostname: "prod.corp.example", port: 22) == .trusted) + } } diff --git a/apple/TableProTests/Core/Database/RemoteDatabaseFileCorrectnessTests.swift b/apple/TableProTests/Core/Database/RemoteDatabaseFileCorrectnessTests.swift index 341b18923e..3f049654bb 100644 --- a/apple/TableProTests/Core/Database/RemoteDatabaseFileCorrectnessTests.swift +++ b/apple/TableProTests/Core/Database/RemoteDatabaseFileCorrectnessTests.swift @@ -613,6 +613,40 @@ struct RemoteDatabaseFileCorrectnessTests { #expect(session.effectiveImmediateRemovals == [snapshotPath]) } + // MARK: - Snapshot command quoting + + @Test("A quote that a combining mark follows is still escaped for the shell") + func shellQuotedEscapesAQuoteBeforeACombiningMark() { + #expect(LibSSH2ExecChannel.shellQuoted("a'\u{301}b") == "'a'\\''\u{301}b'") + } + + @Test("A remote path with quotes and combining marks adds no command to the snapshot command") + func snapshotCommandRunsNothingFromThePath() throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + let injectionMarker = directory.appendingPathComponent("INJECTED") + let remotePath = directory.appendingPathComponent("app.db'\u{301};: > INJECTED;: '\u{301}x").path + + let shell = Process() + shell.executableURL = URL(fileURLWithPath: "/bin/sh") + shell.arguments = [ + "-c", + RemoteDatabaseFileTransfer.remoteSnapshotCommand( + executable: "/usr/bin/true", + remotePath: remotePath, + snapshotPath: remotePath + ".tablepro-snapshot-test", + cleanupDelaySeconds: 0 + ) + ] + shell.currentDirectoryURL = directory + shell.standardOutput = FileHandle.nullDevice + shell.standardError = FileHandle.nullDevice + try shell.run() + shell.waitUntilExit() + + #expect(!FileManager.default.fileExists(atPath: injectionMarker.path)) + } + // MARK: - Killed remote command @Test("A signal-killed remote command does not report success") diff --git a/apple/TableProTests/Core/SSH/HostKeyStoreTests.swift b/apple/TableProTests/Core/SSH/HostKeyStoreTests.swift index 0826ef76b8..761c47f152 100644 --- a/apple/TableProTests/Core/SSH/HostKeyStoreTests.swift +++ b/apple/TableProTests/Core/SSH/HostKeyStoreTests.swift @@ -7,6 +7,7 @@ import Foundation import TableProPluginKit +import TableProSSHTransport import Testing @testable import TablePro @@ -232,4 +233,31 @@ struct HostKeyStoreTests { Issue.record("Expected .mismatch for old key after update, got \(result)") } } + + @Test("A host with a line break plants no key for another host", arguments: [ + "replica.attacker.example\n[prod.corp.example", + "replica.attacker.example\r\n[prod.corp.example", + "replica.attacker.example [prod.corp.example", + ]) + func lineBreakInHostPlantsNoEntry(hostile: String) { + let path = makeTempFilePath() + defer { try? FileManager.default.removeItem(atPath: path) } + + let store = HostKeyStore(filePath: path) + let real = makeTestKey(0x01) + let forged = makeTestKey(0x02) + store.trust(hostname: "prod.corp.example", port: 22, key: real, keyType: "ssh-ed25519") + + #expect( + store.verify(keyData: forged, keyType: "ssh-ed25519", hostname: hostile, port: 22) + == .invalidHost(.host(hostile)) + ) + store.trust(hostname: hostile, port: 22, key: forged, keyType: "ssh-ed25519") + + #expect( + store.verify(keyData: forged, keyType: "ssh-ed25519", hostname: "prod.corp.example", port: 22) + == .mismatch(expected: HostKeyStore.fingerprint(of: real), actual: HostKeyStore.fingerprint(of: forged)) + ) + #expect(store.verify(keyData: real, keyType: "ssh-ed25519", hostname: "prod.corp.example", port: 22) == .trusted) + } } diff --git a/apple/TableProTests/Core/Services/Infrastructure/CommandLineShimTests.swift b/apple/TableProTests/Core/Services/Infrastructure/CommandLineShimTests.swift index 98e65e0c25..73427c562e 100644 --- a/apple/TableProTests/Core/Services/Infrastructure/CommandLineShimTests.swift +++ b/apple/TableProTests/Core/Services/Infrastructure/CommandLineShimTests.swift @@ -126,5 +126,6 @@ struct CommandLineShimTests { @Test("A single quote in a path cannot break out of the quoting") func quotingEscapesSingleQuotes() { #expect(CommandLineShim.quote("/Apps/Dat's.app") == "'/Apps/Dat'\\''s.app'") + #expect(CommandLineShim.quote("/Apps/Dat'\u{301}s.app") == "'/Apps/Dat'\\''\u{301}s.app'") } } diff --git a/apple/TableProTests/Core/Utilities/PasswordSourceResolverTests.swift b/apple/TableProTests/Core/Utilities/PasswordSourceResolverTests.swift index 9cade99f64..b9f367f55f 100644 --- a/apple/TableProTests/Core/Utilities/PasswordSourceResolverTests.swift +++ b/apple/TableProTests/Core/Utilities/PasswordSourceResolverTests.swift @@ -337,6 +337,11 @@ struct PasswordSourceResolverTests { #expect(command == "op read --no-newline 'x'\\''; rm -rf ~ #'") } + @Test("Shell quoting escapes a quote that a combining mark follows") + func shellQuotingIsByCodeUnit() { + #expect(PasswordSourceResolver.shellQuote("x'\u{301}; rm -rf ~ #") == "'x'\\''\u{301}; rm -rf ~ #'") + } + @Test("Extracts a JSON field from a secret string") func extractsJsonField() throws { let value = try PasswordSourceResolver.extractJsonField( diff --git a/apple/TableProTests/Database/MySQLClientArgumentsTests.swift b/apple/TableProTests/Database/MySQLClientArgumentsTests.swift index 75db4b4f4a..f171600f1b 100644 --- a/apple/TableProTests/Database/MySQLClientArgumentsTests.swift +++ b/apple/TableProTests/Database/MySQLClientArgumentsTests.swift @@ -27,6 +27,27 @@ struct MySQLClientArgumentsTests { ) } + @Test("A restore client gets the family's command switch only from the release that has it", arguments: [ + (NativeDumpToolFlavor.mysql, "8.0.39", false), (.mysql, "8.0.40", true), (.mysql, "8.3.0", false), + (.mysql, "8.4.2", false), (.mysql, "8.4.3", true), (.mysql, "9.0.1", false), (.mysql, "9.1.0", true), + (.mysql, "9.5.0", true), (.mariadb, "10.5.25", false), (.mariadb, "10.6.17", false), + (.mariadb, "10.6.18", true), (.mariadb, "10.11.8", true), (.mariadb, "11.4.1", false), + (.mariadb, "11.4.2", true), (.mariadb, "13.0.2", true) + ] as [(NativeDumpToolFlavor, String, Bool)]) + func restoreSafetyFollowsTheRelease(flavor: NativeDumpToolFlavor, release: String, hasSwitch: Bool) { + let banner = flavor == .mariadb ? "mariadb from \(release)-MariaDB, client 15.2" : "mysql Ver \(release) for macos" + let tool = NativeDumpResolvedTool(name: "mysql", path: "/usr/bin/mysql", flavor: flavor, versionText: banner) + let switchFlag = flavor == .mariadb ? "--sandbox" : "--skip-system-command" + let flags = MySQLClientArguments.restoreSafety(tool: tool) + #expect(flags == ["--binary-mode", "--local-infile=0"] + (hasSwitch ? [switchFlag] : [])) + } + + @Test("A restore client that names no release still gets binary mode and no local files") + func restoreSafetyWithoutARelease() { + let unnamed = NativeDumpResolvedTool(name: "mysql", path: "/usr/bin/mysql", flavor: .unidentified) + #expect(MySQLClientArguments.restoreSafety(tool: unnamed) == ["--binary-mode", "--local-infile=0"]) + } + @Test("MySQL's tools take --ssl-mode for every mode") func mysqlSpelling() throws { let expected: [SSLMode: String] = [ diff --git a/apple/TableProTests/Database/MySQLDumpToolIdentifierTests.swift b/apple/TableProTests/Database/MySQLDumpToolIdentifierTests.swift index 4fc1967825..dae7fa19de 100644 --- a/apple/TableProTests/Database/MySQLDumpToolIdentifierTests.swift +++ b/apple/TableProTests/Database/MySQLDumpToolIdentifierTests.swift @@ -57,6 +57,18 @@ struct MySQLDumpToolIdentifierTests { #expect(MySQLDumpToolIdentifier.majorVersion(fromVersionText: nil) == nil) } + @Test("The full release is read from either family's banner") + func release() { + #expect(MySQLDumpToolIdentifier.release(fromVersionText: Self.mysql84) == NativeDumpToolRelease(8, 4, 11)) + #expect(MySQLDumpToolIdentifier.release(fromVersionText: Self.mysql57) == NativeDumpToolRelease(5, 7, 44)) + #expect(MySQLDumpToolIdentifier.release(fromVersionText: Self.mariaDB) == NativeDumpToolRelease(12, 3, 3)) + let mariaDB10 = "mysql Ver 15.1 Distrib 10.6.18-MariaDB, for osx10.19 (arm64) using readline 5.1" + #expect(MySQLDumpToolIdentifier.release(fromVersionText: mariaDB10) == NativeDumpToolRelease(10, 6, 18)) + #expect(MySQLDumpToolIdentifier.release(fromVersionText: "mysql Ver 9.1 for macos") == NativeDumpToolRelease(9, 1, 0)) + #expect(MySQLDumpToolIdentifier.release(fromVersionText: "no version here") == nil) + #expect(MySQLDumpToolIdentifier.release(fromVersionText: nil) == nil) + } + @Test("Identifying a tool keeps what it said, so nothing probes it twice") func identifyCarriesTheBanner() { let resolved = MySQLDumpToolIdentifier.identify( diff --git a/apple/TableProTests/Database/NativeDumpIsolatedImportTests.swift b/apple/TableProTests/Database/NativeDumpIsolatedImportTests.swift new file mode 100644 index 0000000000..6ed190d44a --- /dev/null +++ b/apple/TableProTests/Database/NativeDumpIsolatedImportTests.swift @@ -0,0 +1,316 @@ +// +// NativeDumpIsolatedImportTests.swift +// TableProTests +// +// A Parquet folder's schema.sql is untrusted: with DuckDB 1.5 a `COPY ... TO` in it writes any file the user can. +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private final class IsolatedRestoreDriver: PluginDriverStub, @unchecked Sendable { + enum Import { + case succeeds + case fails(String) + case waitsForCancel + } + + private let lock = NSLock() + private var log: [String] = [] + private let importBehavior: Import + private let failingStatement: String? + let importStarted = AsyncStream.makeStream() + private let cancelled = AsyncStream.makeStream() + + init(import importBehavior: Import, failingStatement: String? = nil) { + self.importBehavior = importBehavior + self.failingStatement = failingStatement + } + + var calls: [String] { lock.withLock { log } } + + func importExportedDatabase(from folder: URL, into file: URL) async throws { + lock.withLock { log.append("import \(folder.path) into \(file.path)") } + importStarted.continuation.yield() + switch importBehavior { + case .succeeds: + return + case .fails(let message): + throw DriverStubError(message: message) + case .waitsForCancel: + let signal = cancelled + try await withTaskCancellationHandler { + for await _ in signal.stream { break } + throw CancellationError() + } onCancel: { + signal.continuation.yield() + } + } + } + + func execute(query: String) async throws -> PluginQueryResult { + lock.withLock { log.append(query) } + if query == failingStatement { throw DriverStubError(message: "Constraint Error: Violates foreign key constraint") } + return .empty + } + + func switchDatabase(to database: String) async throws {} +} + +// A driver built before the requirement existed gets PluginKit's default. +private final class DriverWithoutIsolatedImport: PluginDriverStub, @unchecked Sendable { + private let lock = NSLock() + private var log: [String] = [] + + var statements: [String] { lock.withLock { log } } + + func execute(query: String) async throws -> PluginQueryResult { + lock.withLock { log.append(query) } + return .empty + } + + func switchDatabase(to database: String) async throws {} +} + +private struct DriverStubError: LocalizedError { + let message: String + var errorDescription: String? { message } +} + +@Suite("Native dump isolated import", .serialized) +@MainActor +struct NativeDumpIsolatedImportTests { + private let folder = URL(fileURLWithPath: "/tmp/sales-export's", isDirectory: true) + + private var parquet: NativeDumpDescriptor.EngineStatements { + get throws { + try #require(NativeDumpRegistry.descriptor(for: .duckdb, formatId: "parquet")?.engineStatements) + } + } + + private func request(_ connection: DatabaseConnection, catalog: String?) -> NativeDumpDescriptor.Request { + NativeDumpDescriptor.Request( + connection: connection, + database: "live", + fileURL: folder, + password: nil, + currentCatalog: catalog + ) + } + + private func job(for connection: DatabaseConnection) throws -> NativeDumpStatementJob { + try NativeDumpService.statementJob( + kind: .restore, + engine: parquet, + request: request(connection, catalog: "live"), + alias: "bak1", + scope: DatabaseScope(connectionId: connection.id, database: "live", schema: nil) + ) + } + + private func install(_ driver: some PluginDatabaseDriver) -> DatabaseConnection { + let connection = TestFixtures.makeConnection(database: "live", type: .duckdb) + var session = ConnectionSession( + connection: connection, + driver: PluginDriverAdapter(connection: connection, pluginDriver: driver) + ) + session.browseDatabase = "live" + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + return connection + } + + private func restore(_ job: NativeDumpStatementJob) async throws -> NativeDumpState { + let service = NativeDumpService(kind: .restore) + let states = service.stateUpdates() + try service.run(job: .statements(job), database: "live", fileURL: folder) + return await Self.terminal(states) + } + + private static func terminal(_ states: AsyncStream) async -> NativeDumpState { + for await state in states { + switch state { + case .finished, .failed, .cancelled: return state + case .idle, .running, .cancelling: continue + } + } + return .idle + } + + private static func stagingDirectories() throws -> Set { + let names = try FileManager.default.contentsOfDirectory(atPath: FileManager.default.temporaryDirectory.path) + return Set(names.filter { $0.hasPrefix("tablepro-restore-") }) + } + + @Test("A Parquet restore stages a private file and restores it, never the folder's SQL") + func parquetRestoreJob() throws { + let connection = TestFixtures.makeConnection(database: "live", type: .duckdb) + let job = try job(for: connection) + defer { NativeDumpJob.statements(job).discardTemporaryFiles() } + let isolated = try #require(job.isolatedImport) + let file = isolated.file.path + + #expect(isolated.archive == folder) + #expect(isolated.file.deletingLastPathComponent().path == isolated.directory.path) + #expect(isolated.file.pathExtension == "duckdb") + let permissions = try FileManager.default.attributesOfItem(atPath: isolated.directory.path)[.posixPermissions] + #expect((permissions as? NSNumber)?.intValue == 0o700) + #expect(job.statements == [ + "ATTACH '\(file)' AS \"bak1\" (READ_ONLY)", + "COPY FROM DATABASE \"bak1\" TO \"live\" (SCHEMA)", + "COPY FROM DATABASE \"bak1\" TO \"live\" (DATA)", + "DETACH \"bak1\"", + ]) + #expect(!job.statements.contains { $0.contains("IMPORT") || $0.contains("sales-export") }) + } + + @Test("A job that never reaches a runner leaves no staging directory") + func discardRemovesTheStagingDirectory() throws { + let job = try job(for: TestFixtures.makeConnection(database: "live", type: .duckdb)) + let directory = try #require(job.isolatedImport?.directory) + #expect(FileManager.default.fileExists(atPath: directory.path)) + + NativeDumpJob.statements(job).discardTemporaryFiles() + + #expect(!FileManager.default.fileExists(atPath: directory.path)) + } + + @Test("Without a catalog the job is refused before a staging directory exists") + func noCatalogCreatesNothing() throws { + let connection = TestFixtures.makeConnection(database: "live", type: .duckdb) + let before = try Self.stagingDirectories() + + #expect(throws: NativeDumpError.engineStatementUnavailable) { + try NativeDumpService.statementJob( + kind: .restore, + engine: parquet, + request: request(connection, catalog: nil), + alias: "bak1", + scope: DatabaseScope(connectionId: connection.id, database: "live", schema: nil) + ) + } + #expect(try Self.stagingDirectories() == before) + } + + @Test("A Parquet backup and a .duckdb restore run on the session with nothing staged") + func otherJobsStageNothing() throws { + let connection = TestFixtures.makeConnection(database: "live", type: .duckdb) + let scope = DatabaseScope(connectionId: connection.id, database: "live", schema: nil) + let file = try #require(NativeDumpRegistry.descriptor(for: .duckdb, formatId: "duckdb")?.engineStatements) + + let backup = try NativeDumpService.statementJob( + kind: .backup, engine: parquet, request: request(connection, catalog: "live"), alias: "bak1", scope: scope + ) + let fileRestore = try NativeDumpService.statementJob( + kind: .restore, engine: file, request: request(connection, catalog: "live"), alias: "bak1", scope: scope + ) + + #expect(backup.isolatedImport == nil) + #expect(backup.statements == ["EXPORT DATABASE '/tmp/sales-export''s' (FORMAT PARQUET)"]) + #expect(fileRestore.isolatedImport == nil) + } + + @Test("The import runs before the session restores its file, and the staging directory goes") + func importThenRestore() async throws { + let driver = IsolatedRestoreDriver(import: .succeeds) + let connection = install(driver) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let job = try job(for: connection) + let isolated = try #require(job.isolatedImport) + + let state = try await restore(job) + + guard case .finished = state else { + Issue.record("expected finished, got \(state)") + return + } + #expect(driver.calls == ["import \(folder.path) into \(isolated.file.path)"] + job.statements) + #expect(!FileManager.default.fileExists(atPath: isolated.directory.path)) + } + + @Test("A folder the isolated instance refuses fails the restore with the cause and an untouched target") + func refusedFolderLeavesTheTargetAlone() async throws { + let refusal = """ + Permission Error: Cannot access file "/Users/me/.zshrc" - file system operations are disabled by configuration + """ + let driver = IsolatedRestoreDriver(import: .fails(refusal)) + let connection = install(driver) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let job = try job(for: connection) + let isolated = try #require(job.isolatedImport) + + let state = try await restore(job) + + guard case .failed(let message, let targetMayBeModified) = state else { + Issue.record("expected failed, got \(state)") + return + } + #expect(message.contains("this database was not changed")) + #expect(message.contains(refusal)) + #expect(!targetMayBeModified) + #expect(driver.calls.count == 1) + #expect(!FileManager.default.fileExists(atPath: isolated.directory.path)) + } + + @Test("Cancel during the import stops it before the session runs anything") + func cancelDuringImport() async throws { + let driver = IsolatedRestoreDriver(import: .waitsForCancel) + let connection = install(driver) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let job = try job(for: connection) + let isolated = try #require(job.isolatedImport) + let service = NativeDumpService(kind: .restore) + let states = service.stateUpdates() + + try service.run(job: .statements(job), database: "live", fileURL: folder) + for await _ in driver.importStarted.stream { break } + service.cancel() + let state = await Self.terminal(states) + + #expect(state == .cancelled) + #expect(driver.calls.count == 1) + #expect(!FileManager.default.fileExists(atPath: isolated.directory.path)) + } + + @Test("A driver without the isolated import refuses the folder instead of running it on the session") + func driverWithoutTheRequirement() async throws { + let driver = DriverWithoutIsolatedImport() + let connection = install(driver) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let job = try job(for: connection) + + let state = try await restore(job) + + guard case .failed(let message, let targetMayBeModified) = state else { + Issue.record("expected failed, got \(state)") + return + } + #expect(message.contains(PluginDriverUnsupportedOperation.importExportedDatabase.localizedDescription)) + #expect(!targetMayBeModified) + #expect(driver.statements.isEmpty) + } + + @Test("A copy that fails after the import may have changed the target, and the alias is detached") + func copyFailureAfterImport() async throws { + let driver = IsolatedRestoreDriver( + import: .succeeds, + failingStatement: "COPY FROM DATABASE \"bak1\" TO \"live\" (DATA)" + ) + let connection = install(driver) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let job = try job(for: connection) + let isolated = try #require(job.isolatedImport) + + let state = try await restore(job) + + guard case .failed(_, let targetMayBeModified) = state else { + Issue.record("expected failed, got \(state)") + return + } + #expect(targetMayBeModified) + #expect(driver.calls.last == "DETACH \"bak1\"") + #expect(!FileManager.default.fileExists(atPath: isolated.directory.path)) + } +} diff --git a/apple/TableProTests/Database/NativeDumpRegistryTests.swift b/apple/TableProTests/Database/NativeDumpRegistryTests.swift index 5fc9c0634d..2544e836af 100644 --- a/apple/TableProTests/Database/NativeDumpRegistryTests.swift +++ b/apple/TableProTests/Database/NativeDumpRegistryTests.swift @@ -39,6 +39,7 @@ struct NativeDumpRegistryTests { _ type: DatabaseType, kind: NativeDumpKind = .backup, connection overrideConnection: DatabaseConnection? = nil, + database: String = "sales", password: String? = "s3cret", fileURL: URL = URL(fileURLWithPath: "/tmp/out.bin"), scope: NativeDumpScope = .wholeDatabase, @@ -60,7 +61,7 @@ struct NativeDumpRegistryTests { ), request: NativeDumpDescriptor.Request( connection: effective, - database: "sales", + database: database, fileURL: fileURL, password: password, scope: scope, @@ -337,13 +338,39 @@ struct NativeDumpRegistryTests { func sqliteUsesTheFilePath() throws { let file = connection(type: .sqlite, host: "", port: 0, database: "/tmp/app.sqlite", username: "") let backup = try command(.sqlite, connection: file) - #expect(backup.arguments == ["/tmp/app.sqlite", ".dump"]) + #expect(backup.arguments == ["-safe", "/tmp/app.sqlite", ".dump"]) let restore = try command(.sqlite, kind: .restore, connection: file) - #expect(restore.arguments == ["/tmp/app.sqlite"]) + #expect(restore.arguments == ["-safe", "-bail", "/tmp/app.sqlite"]) #expect(restore.isRestore) } + @Test("A SQLite restore refuses a dump's shell commands and stops at its first error") + func sqliteRestoreRunsNoShellCommand() throws { + let directory = FileManager.default.temporaryDirectory.appendingPathComponent("sqlite-safe-\(UUID().uuidString)") + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: directory) } + let marker = directory.appendingPathComponent("ran") + let dump = directory.appendingPathComponent("hostile.sql") + try "CREATE TABLE t(a);\n.shell touch '\(marker.path)'\nINSERT INTO t VALUES(1);\n" + .write(to: dump, atomically: true, encoding: .utf8) + let database = directory.appendingPathComponent("app.sqlite").path + let file = connection(type: .sqlite, host: "", port: 0, database: database, username: "") + let restore = try command(.sqlite, kind: .restore, connection: file) + + let process = Process() + process.executableURL = URL(fileURLWithPath: "/usr/bin/sqlite3") + process.arguments = restore.arguments + process.standardInput = try FileHandle(forReadingFrom: dump) + process.standardOutput = FileHandle.nullDevice + process.standardError = FileHandle.nullDevice + try process.run() + process.waitUntilExit() + + #expect(process.terminationStatus != 0) + #expect(!FileManager.default.fileExists(atPath: marker.path)) + } + /// libSQL claims the SQLite descriptor and keeps its path in a plugin-declared additional /// field, leaving `database` empty. Reading `database` handed `sqlite3` an empty path, which /// exits 0 after writing a 52-byte file the result sheet then reported as a backup. @@ -354,7 +381,7 @@ struct NativeDumpRegistryTests { let backup = try command( .libsql, connection: remote, localFilePath: "/tmp/turso.db" ) - #expect(backup.arguments == ["/tmp/turso.db", ".dump"]) + #expect(backup.arguments == ["-safe", "/tmp/turso.db", ".dump"]) } /// A libSQL connection to a Turso URL has no local file, so `sqlite3` cannot reach it at all. @@ -370,6 +397,76 @@ struct NativeDumpRegistryTests { #expect(NativeDumpRegistry.supports(local, localFilePath: "/tmp/turso.db")) } + @Test("Every value of the SQL Server connection string is quoted", arguments: [ + (NativeDumpKind.backup, "/SourceConnectionString:"), (.restore, "/TargetConnectionString:") + ]) + func sqlServerConnectionStringQuotesItsValues(kind: NativeDumpKind, flag: String) throws { + #expect(try NativeDumpArgumentQuoting.sqlServerConnectionStringValue(#"a;b"c"#) == #""a;b""c""#) + let built = try command( + .mssql, + kind: kind, + connection: connection(type: .mssql, port: 1_433, sslMode: .verifyIdentity), + database: "x;Server=elsewhere;TrustServerCertificate=true", + password: "p;w" + ) + let argument = try #require(built.arguments.first { $0.hasPrefix(flag) }) + #expect( + argument.dropFirst(flag.count) == """ + Server="db.example.com,1433";Database="x;Server=elsewhere;TrustServerCertificate=true";\ + User ID="alice";Password="p;w";Encrypt=true + """ + ) + } + + @Test("A NUL in a SQL Server or PostgreSQL value is refused before anything runs") + func nulInAConnectionValueIsRefused() { + #expect(throws: NativeDumpError.nulInArgument) { + try command(.mssql, connection: connection(type: .mssql, port: 1_433), password: "p\u{0}w") + } + #expect(throws: NativeDumpError.nulInArgument) { + try command(.postgresql, database: "sa\u{0}les") + } + } + + // libpq reads a -d value holding `=` as a conninfo whose host overrides -h. Measured with + // pg_dump 17.11: the raw name dialed 127.0.0.66, the quoted one dumped the named database. + @Test("A PostgreSQL database name reaches -d as a quoted dbname, in both directions", arguments: [ + NativeDumpKind.backup, .restore + ]) + func postgresDatabaseNameIsQuoted(kind: NativeDumpKind) throws { + let hostile = try command(.postgresql, kind: kind, database: "host=127.0.0.66 port=15433 sslmode=disable") + let value = try #require(hostile.arguments.firstIndex(of: "-d").map { hostile.arguments[$0 + 1] }) + #expect(value == "dbname='host=127.0.0.66 port=15433 sslmode=disable'") + #expect(hostile.arguments.starts(with: ["--no-password", "-h", "db.example.com", "-p", "5432"])) + + let quoted = try command(.postgresql, kind: kind, database: "a'b\\c'\u{301}") + let escaped = try #require(quoted.arguments.firstIndex(of: "-d").map { quoted.arguments[$0 + 1] }) + #expect(escaped == "dbname='a\\'b\\\\c\\'\u{301}'") + } + + // `\!` and `system` in piped input run a shell command, with MYSQL_PWD in its environment. + // Measured with MariaDB 13.0.2: --binary-mode refuses both and `\.`, and a mariadb-dump of + // routines, triggers, events and binary data restores the same under these flags. + @Test("A MySQL or MariaDB restore turns the client's own commands off", arguments: [ + (NativeDumpToolFlavor.mysql, "mysql Ver 8.4.6 for macos15 on arm64 (Homebrew)", "--skip-system-command"), + (.mysql, "mysql Ver 8.0.39 for macos14 on arm64", nil), + (.mariadb, "mysql from 13.0.2-MariaDB, client 15.2 for osx10.23 (arm64) using EditLine wrapper", "--sandbox"), + (.mariadb, "mysql Ver 15.1 Distrib 10.6.17-MariaDB, for osx10.19 (arm64)", nil), + (.unidentified, nil, nil), + ] as [(NativeDumpToolFlavor, String?, String?)]) + func mysqlRestoreTurnsClientCommandsOff(flavor: NativeDumpToolFlavor, versionText: String?, switchFlag: String?) throws { + let restore = try command(.mysql, kind: .restore, flavor: flavor, toolVersionText: versionText) + let terminator = try #require(restore.arguments.firstIndex(of: "--")) + let options = restore.arguments[..&2 + exit 1 +fi + +WORK="$(cd "$(mktemp -d /tmp/duckdb-isolated-import.XXXXXX)" && pwd -P)" +trap 'rm -rf "$WORK"' EXIT + +cat > "$WORK/harness.c" << 'HARNESS' +#include +#include +#include +#include "duckdb.h" + +// harness [-q] [-o name=value]... ...: one line per statement, OK or ERR, then rows. +int main(int argc, char **argv) { + duckdb_config config; + int prepared = 1, i = 1; + duckdb_create_config(&config); + for (; i < argc; i++) { + if (strcmp(argv[i], "-q") == 0) { prepared = 0; continue; } + if (strcmp(argv[i], "-o") != 0) break; + char *option = strdup(argv[++i]), *value = strchr(option, '='); + *value++ = 0; + if (duckdb_set_config(config, option, value) == DuckDBError) { printf("REJECTED %s\n", option); return 2; } + } + duckdb_database db; duckdb_connection con; char *error = NULL; + if (duckdb_open_ext(argv[i++], &db, config, &error) == DuckDBError) { printf("OPEN %s\n", error); return 2; } + duckdb_connect(db, &con); + for (; i < argc; i++) { + duckdb_result r; + duckdb_prepared_statement st = NULL; + duckdb_state state; + if (prepared) { + if (duckdb_prepare(con, argv[i], &st) == DuckDBError) { + printf("ERR %s\n", duckdb_prepare_error(st)); + duckdb_destroy_prepare(&st); + continue; + } + state = duckdb_execute_prepared(st, &r); + } else { + state = duckdb_query(con, argv[i], &r); + } + if (state == DuckDBError) { + char *message = strdup(duckdb_result_error(&r)); + for (char *c = message; *c; c++) if (*c == '\n') *c = ' '; + printf("ERR %s\n", message); + free(message); + } else { + printf("OK\n"); + for (idx_t row = 0; row < duckdb_row_count(&r); row++) { + for (idx_t col = 0; col < duckdb_column_count(&r); col++) { + char *v = duckdb_value_varchar(&r, col, row); + printf("%s%s", col ? " | " : " ", v ? v : "NULL"); + duckdb_free(v); + } + printf("\n"); + } + } + duckdb_destroy_result(&r); + duckdb_destroy_prepare(&st); + } + duckdb_disconnect(&con); + duckdb_close(&db); + return 0; +} +HARNESS +cc -O1 -w -I "$HEADERS" "$WORK/harness.c" "$LIB" -lc++ -o "$WORK/harness" + +OPEN_OPTIONS=() +while read -r option; do + OPEN_OPTIONS+=(-o "$option") +done < <(sed -nE 's/^ *\("([a-z_]+)", "([a-z]+)"\),$/\1=\2/p' "$SWIFT_FILE") +LOCKDOWN="$(sed -nE 's/^ *"(SET [^"]*)",$/\1/p' "$SWIFT_FILE")" +if [ "${#OPEN_OPTIONS[@]}" -ne 6 ] || [ "$(printf '%s\n' "$LOCKDOWN" | wc -l | tr -d ' ')" -ne 3 ]; then + echo "error: could not read three open options and three SET statements from $SWIFT_FILE" >&2 + exit 1 +fi + +export HOME="$WORK/home" +mkdir -p "$HOME" "$WORK/outside" "$WORK/h" "$WORK/run" + +# The sequence DuckDBIsolatedImport runs: open options, the SET statements, then IMPORT DATABASE. +isolated_import() { + local folder="$1" db="$2" + shift 2 + local quoted statements=() + quoted="'$(printf '%s' "$folder" | sed "s/'/''/g")'" + while IFS= read -r statement; do + statements+=("${statement//\\(DuckDBSchemaQueries.quoteLiteral(folder))/$quoted}") + done <<< "$LOCKDOWN" + "$WORK/harness" -q "${OPEN_OPTIONS[@]}" "$db" "${statements[@]}" "IMPORT DATABASE $quoted" "$@" +} + +secret="$WORK/secret.txt" +echo "TOP SECRET" > "$secret" +echo "original" > "$WORK/outside/victim.csv" +echo "not an extension" > "$WORK/outside/fake.duckdb_extension" +hostile() { + mkdir -p "$WORK/h/$1" + printf '%s\n' "$2" > "$WORK/h/$1/schema.sql" + : > "$WORK/h/$1/load.sql" +} +hostile copy_out "COPY (SELECT 'pwned' AS x) TO '$WORK/outside/copy_out.csv';" +hostile copy_dotdot "COPY (SELECT 'pwned' AS x) TO '$WORK/h/copy_dotdot/../../outside/dotdot.csv';" +hostile copy_relative "COPY (SELECT 'pwned' AS x) TO 'relative_out.csv';" +hostile symlink_dir "COPY (SELECT 'pwned' AS x) TO '$WORK/h/symlink_dir/escape/through_link.csv';" +ln -s "$WORK/outside" "$WORK/h/symlink_dir/escape" +hostile symlink_file_write "COPY (SELECT 'pwned' AS x) TO '$WORK/h/symlink_file_write/victim.csv' (HEADER false);" +ln -s "$WORK/outside/victim.csv" "$WORK/h/symlink_file_write/victim.csv" +hostile symlink_file_read "CREATE TABLE stolen AS SELECT content FROM read_text('$WORK/h/symlink_file_read/leak.txt');" +ln -s "$secret" "$WORK/h/symlink_file_read/leak.txt" +hostile attach_new "ATTACH '$WORK/outside/attached.duckdb' AS x; CREATE TABLE x.t AS SELECT 1 AS a;" +hostile install "INSTALL httpfs;" +hostile load_file "LOAD '$WORK/outside/fake.duckdb_extension';" +hostile load_known "LOAD json;" +hostile set_external "SET enable_external_access = true; COPY (SELECT 1 AS x) TO '$WORK/outside/reenabled.csv';" +hostile set_allowed "SET allowed_directories = ['/']; COPY (SELECT 1 AS x) TO '$WORK/outside/widened.csv';" +hostile set_extension_dir "SET extension_directory = '$WORK/outside/ext';" +hostile read_etc "CREATE TABLE stolen AS SELECT content FROM read_text('/etc/hosts');" +hostile read_csv_home "CREATE TABLE stolen AS SELECT * FROM read_csv('$secret');" +hostile glob_home "CREATE TABLE listing AS SELECT * FROM glob('$WORK/*');" +hostile view_reads "CREATE VIEW v AS SELECT content FROM read_text('/etc/hosts');" +hostile macro_reads "CREATE MACRO steal() AS TABLE SELECT content FROM read_text('$secret');" +hostile persistent_secret "CREATE PERSISTENT SECRET s (TYPE s3, KEY_ID 'a', SECRET 'b');" +hostile export_out "CREATE TABLE t AS SELECT 1 AS a; EXPORT DATABASE '$WORK/outside/exported';" +hostile profile_out "PRAGMA enable_profiling = 'json'; PRAGMA profiling_output = '$WORK/outside/profile.json'; SELECT 1;" + +failures=0 +cd "$WORK/run" +for folder in "$WORK"/h/*; do + name="$(basename "$folder")" + result="$(isolated_import "$folder" "$WORK/run/$name.duckdb" | tail -1)" + leaked="$(find "$WORK/outside" "$HOME" "$WORK/run" -mindepth 1 ! -name '*.duckdb' ! -name '*.wal' \ + ! -path "$WORK/outside/victim.csv" ! -path "$WORK/outside/fake.duckdb_extension" | tr '\n' ' ')" + if [[ "$result" != ERR* ]] || [ -n "$leaked" ] || [ "$(cat "$WORK/outside/victim.csv")" != original ]; then + echo "FAIL $name: ${result:0:160} ${leaked:+wrote $leaked}" + failures=$((failures + 1)) + else + echo "refused $name: ${result:4:110}" + fi +done + +genuine="$WORK/genuine" +"$WORK/harness" -q "$WORK/source.duckdb" \ + "CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy')" \ + "CREATE SEQUENCE order_seq START 100" \ + "CREATE SCHEMA app" \ + "CREATE TABLE app.customers (id INTEGER PRIMARY KEY, name VARCHAR NOT NULL UNIQUE, feeling mood, tags VARCHAR[], addr STRUCT(street VARCHAR, zip INTEGER), photo BLOB, note VARCHAR)" \ + "CREATE TABLE app.orders (id INTEGER PRIMARY KEY DEFAULT nextval('order_seq'), customer_id INTEGER, qty INTEGER CHECK (qty > 0), price DECIMAL(10,2), total DECIMAL(12,2) GENERATED ALWAYS AS (qty * price) VIRTUAL, created TIMESTAMP)" \ + "CREATE INDEX orders_created ON app.orders(created)" \ + "INSERT INTO app.customers VALUES (1, 'Ann', 'happy', ['a','b'], {'street': 'Main', 'zip': 1000}, '\\x00\\x01\\xFF'::BLOB, NULL), (2, 'Bob', NULL, NULL, NULL, NULL, 'two lines'), (3, 'Cé', 'sad', [], {'street': NULL, 'zip': NULL}, ''::BLOB, 'x')" \ + "INSERT INTO app.orders (customer_id, qty, price, created) VALUES (1, 2, 3.50, '2026-01-01 10:00:00'), (2, 1, 9.99, NULL)" \ + "CREATE VIEW app.big_orders AS SELECT o.id, c.name, o.total FROM app.orders o JOIN app.customers c ON c.id = o.customer_id WHERE o.qty > 1" \ + "CREATE MACRO app.double_it(x) AS x * 2" \ + "CREATE MACRO app.orders_of(cid) AS TABLE SELECT * FROM app.orders WHERE customer_id = cid" \ + "EXPORT DATABASE '$genuine' (FORMAT PARQUET)" > /dev/null + +describe=( + "SELECT schema_name, table_name, sql FROM duckdb_tables() WHERE database_name = current_database() ORDER BY 1, 2" + "SELECT schema_name, view_name, sql FROM duckdb_views() WHERE NOT internal AND database_name = current_database() ORDER BY 1, 2" + "SELECT schema_name, function_name, macro_definition FROM duckdb_functions() WHERE NOT internal AND database_name = current_database() ORDER BY 1, 2" + "SELECT schema_name, sequence_name, start_value, last_value FROM duckdb_sequences() WHERE database_name = current_database() ORDER BY 1, 2" + "SELECT schema_name, index_name, sql FROM duckdb_indexes() WHERE database_name = current_database() ORDER BY 1, 2" + "SELECT schema_name, table_name, constraint_text FROM duckdb_constraints() WHERE database_name = current_database() ORDER BY 1, 2, 3" + "SELECT type_name FROM duckdb_types() WHERE NOT internal AND database_name = current_database() ORDER BY 1" + "SELECT id, name, feeling::VARCHAR, tags::VARCHAR, addr::VARCHAR, hex(photo), note FROM app.customers ORDER BY id" + "SELECT * FROM app.orders ORDER BY id" + "SELECT * FROM app.big_orders ORDER BY id" + "SELECT app.double_it(21), (SELECT count(*) FROM app.orders_of(1))" +) + +"$WORK/harness" -q "$WORK/session_import.duckdb" "IMPORT DATABASE '$genuine'" > "$WORK/steps.txt" +isolated_import "$genuine" "$WORK/staged.duckdb" >> "$WORK/steps.txt" +"$WORK/harness" "$WORK/session_copy.duckdb" \ + "ATTACH '$WORK/staged.duckdb' AS bak (READ_ONLY)" \ + "COPY FROM DATABASE bak TO session_copy (SCHEMA)" \ + "COPY FROM DATABASE bak TO session_copy (DATA)" \ + "DETACH bak" >> "$WORK/steps.txt" +"$WORK/harness" "$WORK/session_import.duckdb" "${describe[@]}" > "$WORK/expected.txt" +"$WORK/harness" "$WORK/session_copy.duckdb" "${describe[@]}" > "$WORK/measured.txt" + +if grep -q '^ERR' "$WORK/steps.txt" "$WORK/expected.txt" || ! diff -u "$WORK/expected.txt" "$WORK/measured.txt"; then + echo "FAIL genuine export: the isolated restore differs from IMPORT DATABASE in the session" + failures=$((failures + 1)) +else + echo "restored genuine export: $(grep -c '^ ' "$WORK/measured.txt") catalog and data rows match IMPORT DATABASE" +fi + +if [ "$failures" -gt 0 ]; then + echo "$failures check(s) failed." + exit 1 +fi +echo "OK: every hostile folder failed in the locked instance, and the genuine export restored unchanged." diff --git a/apple/scripts/probes/check-mysql-dump-tool-flags.sh b/apple/scripts/probes/check-mysql-dump-tool-flags.sh index 17c42199c8..725c84febc 100755 --- a/apple/scripts/probes/check-mysql-dump-tool-flags.sh +++ b/apple/scripts/probes/check-mysql-dump-tool-flags.sh @@ -60,6 +60,19 @@ MARIADB_FLAGS=( "--ssl-key=/dev/null" "--plugin-dir=/dev/null" ) +# The restore client's own flags, checked against `mysql` and `mariadb` rather than the dump tool. +# MySQLClientArguments.restoreSafety adds the last of each list only from the release that has it, +# and release_has_switch below holds the same gates. +MYSQL_RESTORE_FLAGS=( + "--binary-mode" + "--local-infile=0" + "--skip-system-command" +) +MARIADB_RESTORE_FLAGS=( + "--binary-mode" + "--local-infile=0" + "--sandbox" +) failures=0 @@ -81,7 +94,8 @@ source_flags() { } checked_flags() { - printf '%s\n' "${MYSQL_FLAGS[@]}" "${MARIADB_FLAGS[@]}" | sed -E 's|=/dev/null|=|' | sort -u + printf '%s\n' "${MYSQL_FLAGS[@]}" "${MARIADB_FLAGS[@]}" "${MYSQL_RESTORE_FLAGS[@]}" "${MARIADB_RESTORE_FLAGS[@]}" \ + | sed -E 's|=/dev/null|=|' | sort -u } # Both directions, so neither a flag the code dropped nor one it gained goes unchecked. @@ -107,16 +121,45 @@ flavor_of() { esac } +# `Distrib` beats `Ver` on MySQL 5.7 and MariaDB 10, `from` is MariaDB 11 and later: the last +# marker on the line is the release. +release_of() { + "$1" --version 2> /dev/null | sed -nE 's/.*(Distrib|from|Ver)[[:space:]]+([0-9]+(\.[0-9]+)*).*/\2/p' | head -1 +} + +# 0 when the release is in [low, high). An empty high is open. +release_in() { + local release=$1 low=$2 high=$3 + [ "$(printf '%s\n%s\n' "$low" "$release" | sort -V | head -1)" = "$low" ] || return 1 + [ -z "$high" ] && return 0 + [ "$release" != "$high" ] && [ "$(printf '%s\n%s\n' "$release" "$high" | sort -V | head -1)" = "$release" ] +} + +release_has_switch() { + local flavor=$1 release=$2 + [ -n "$release" ] || return 1 + if [ "$flavor" = "mariadb" ]; then + release_in "$release" 10.6.18 10.7 || release_in "$release" 10.11.8 10.12 || release_in "$release" 11.4.2 "" + else + release_in "$release" 8.0.40 8.1 || release_in "$release" 8.4.3 8.5 || release_in "$release" 9.1.0 "" + fi +} + # A tool that rejects the flag says so before connecting, so the connection failure is the pass. check_tool() { - local tool=$1 flavor=$2 flag output + local tool=$1 flavor=$2 role=${3:-dump} flag output local -a flags - if [ "$flavor" = "mariadb" ]; then - flags=("${MARIADB_FLAGS[@]}") - else - flags=("${MYSQL_FLAGS[@]}") + case "$flavor/$role" in + mariadb/restore) flags=("${MARIADB_RESTORE_FLAGS[@]}") ;; + mysql/restore) flags=("${MYSQL_RESTORE_FLAGS[@]}") ;; + mariadb/dump) flags=("${MARIADB_FLAGS[@]}") ;; + *) flags=("${MYSQL_FLAGS[@]}") ;; + esac + echo "checking $flavor $role tool $tool ($("$tool" --version 2> /dev/null | head -1))" + if [ "$role" = "restore" ] && ! release_has_switch "$flavor" "$(release_of "$tool")"; then + echo " skip ${flags[${#flags[@]} - 1]}: the app adds it only from the release that has it" + unset 'flags[${#flags[@]}-1]' fi - echo "checking $flavor tool $tool" for flag in "${flags[@]}"; do output="$("$tool" --protocol=TCP -h 127.0.0.1 -P "$CLOSED_PORT" --socket="$MISSING_SOCKET" -u probe "$flag" nodb 2>&1)" case "$output" in @@ -138,6 +181,14 @@ candidates() { ls -1 /opt/homebrew/opt/*/bin/mysqldump /usr/local/opt/*/bin/mysqldump /usr/local/mysql/bin/mysqldump 2> /dev/null } +restore_candidates() { + local name + for name in mysql mariadb; do + command -v "$name" 2> /dev/null + done + ls -1 /opt/homebrew/opt/*/bin/mysql /usr/local/opt/*/bin/mysql /usr/local/mysql/bin/mysql 2> /dev/null +} + check_source_agrees declare -a seen_flavors=() @@ -151,6 +202,17 @@ while read -r tool; do check_tool "$tool" "$flavor" done < <(candidates | sort -u) +declare -a seen_restore_flavors=() +while read -r tool; do + [ -x "$tool" ] || continue + flavor="$(flavor_of "$tool")" || continue + case " ${seen_restore_flavors[*]:-} " in + *" $flavor "*) continue ;; + esac + seen_restore_flavors+=("$flavor") + check_tool "$tool" "$flavor" restore +done < <(restore_candidates | sort -u) + if [ ${#seen_flavors[@]} -eq 0 ]; then echo "no mysql-family client found; install mysql-client or mariadb to run this check" >&2 exit 3 diff --git a/apple/scripts/probes/check-restore-client-commands.sh b/apple/scripts/probes/check-restore-client-commands.sh new file mode 100755 index 0000000000..1d291433a4 --- /dev/null +++ b/apple/scripts/probes/check-restore-client-commands.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# +# Check that the restore clients refuse the commands a dump file can carry, with the flags +# NativeDumpRegistry passes them. +# +# A restore pipes the chosen file into the client, and both clients read their own commands from +# it: `.shell` and writefile() for sqlite3, `\!`, `system` and `\.` for mysql and mariadb. Each +# line below writes a marker file; the check fails when one appears. +# +# Usage: +# scripts/probes/check-restore-client-commands.sh +# MYSQL_PROBE_ARGS="-u root" scripts/probes/check-restore-client-commands.sh +# +# sqlite3 needs no server. The mysql-family check runs when the client on PATH can connect with +# MYSQL_PROBE_ARGS (default: none, the local socket) and create a scratch database, and skips +# otherwise. Exits non-zero on a command that ran. + +set -uo pipefail + +WORK="$(mktemp -d "${TMPDIR:-/tmp}/tablepro-restore-probe.XXXXXX")" +trap 'rm -rf "$WORK"' EXIT +failures=0 + +fail() { + echo "FAIL: $1" >&2 + failures=$((failures + 1)) +} + +check_marker() { + if [ -e "$WORK/$1" ]; then + fail "$2 ran a command from the dump" + rm -f "$WORK/$1" + else + echo " ok $2" + fi +} + +SQLITE="$(command -v sqlite3)" +if [ -n "$SQLITE" ]; then + echo "checking $SQLITE ($("$SQLITE" --version | cut -d' ' -f1))" + printf '.shell touch %s/control\n' "$WORK" | "$SQLITE" "$WORK/control.db" > /dev/null 2>&1 + [ -e "$WORK/control" ] && echo " control: without -safe the dump's .shell ran" || echo " note: .shell did not run even without -safe" + rm -f "$WORK/control" + printf 'CREATE TABLE t(a);\n.shell touch %s/shell\nINSERT INTO t VALUES(1);\n' "$WORK" \ + | "$SQLITE" -safe -bail "$WORK/a.db" > /dev/null 2>&1 + check_marker shell "sqlite3 -safe -bail .shell" + printf "SELECT writefile('%s/writefile', 'x');\n" "$WORK" \ + | "$SQLITE" -safe -bail "$WORK/b.db" > /dev/null 2>&1 + check_marker writefile "sqlite3 -safe -bail writefile()" + + # Why the backup does not also pass -readonly. + "$SQLITE" "$WORK/wal.db" "PRAGMA journal_mode=wal; CREATE TABLE t(a); PRAGMA wal_checkpoint(truncate);" > /dev/null + rm -f "$WORK/wal.db-wal" "$WORK/wal.db-shm" + if "$SQLITE" -safe -readonly "$WORK/wal.db" .dump > /dev/null 2>&1; then + echo " note: -readonly now opens a WAL database without its -wal and -shm files" + else + echo " ok -readonly still fails on a WAL database without its -wal and -shm files" + fi +else + echo "no sqlite3 on PATH; skipping" +fi + +MYSQL="$(command -v mysql || command -v mariadb || true)" +read -r -a PROBE_ARGS <<< "${MYSQL_PROBE_ARGS:-}" +DB="tablepro_restore_probe_$$" +if [ -n "$MYSQL" ] && "$MYSQL" ${PROBE_ARGS[@]+"${PROBE_ARGS[@]}"} -e "CREATE DATABASE $DB" > /dev/null 2>&1; then + banner="$("$MYSQL" --version)" + case "$banner" in + *MariaDB* | *mariadb*) switch="--sandbox" ;; + *) switch="--skip-system-command" ;; + esac + echo "checking $MYSQL ($banner)" + printf '\\! touch %s/control\n' "$WORK" | "$MYSQL" ${PROBE_ARGS[@]+"${PROBE_ARGS[@]}"} -- "$DB" > /dev/null 2>&1 + [ -e "$WORK/control" ] && echo " control: without flags the dump's \\! ran" || echo " note: \\! did not run even without flags" + rm -f "$WORK/control" + for flags in "--binary-mode --local-infile=0" "--binary-mode --local-infile=0 $switch"; do + read -r -a extra <<< "$flags" + printf 'SELECT 1;\n\\! touch %s/bang\nsystem touch %s/system\nSELECT 2;\n' "$WORK" "$WORK" \ + | "$MYSQL" ${PROBE_ARGS[@]+"${PROBE_ARGS[@]}"} "${extra[@]}" -- "$DB" > /dev/null 2>&1 + check_marker bang "mysql $flags \\!" + check_marker system "mysql $flags system" + done + "$MYSQL" ${PROBE_ARGS[@]+"${PROBE_ARGS[@]}"} -e "DROP DATABASE $DB" > /dev/null 2>&1 +else + echo "no mysql-family client that can create a scratch database; skipping" +fi + +if [ "$failures" -gt 0 ]; then + echo "$failures restore client(s) ran a command from the dump" >&2 + exit 1 +fi +echo "restore clients refuse the dump's commands" From 29ff06990e7ef8f2c992a47a1ca6d1a6d90bb33e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sun, 1 Nov 2026 00:45:29 +0700 Subject: [PATCH 51/85] Merge commit from fork * fix(security): harden the Git commands TablePro runs on its own Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-FRDSB33T. Advisory: GHSA-rhxc-5m3h-r5ch * fix(security): pin PostgreSQL catalog reads to pg_catalog and SYS-qualify Oracle Explain Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CDER3MTT. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CRPAPF23. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): harden dump tools, shell quoting and SSH host names Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-1D30XTRS. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-2L44V7CH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-3NLPR9WG. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-AX94JCED. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-E9V7ZHHQ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TWLM1VYZ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-U19XTGQC. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CF3R4T4R. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BZJK0VNE. Advisory: GHSA-94m6-qx4g-cpwg * fix(security): keep credentials on their own host and refuse TLS modes a driver cannot honour Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-EJ1059QD. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-L54ARVVX. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-R0FNK1XO. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KSESNBFN. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KGP1I8T0. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-11I9A28E. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-OP00LFZB. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-6ES8FYVM. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-60V4XYJM. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-YB76IHAI. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-9RQLLRZ0. Advisory: GHSA-j2xw-m3p7-3m64 * fix(security): write Oracle Estimated Plan to SYS.PLAN_TABLE$ Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): restore a DuckDB Parquet folder through a locked-down instance of its own * test(security): expect the pg_catalog-qualified PostgreSQL cancel statement --- apple/Packages/TableProCore/Package.swift | 5 + .../ConnectionSecretOptions.swift | 87 ++++++++++++++++++ .../Config/FirebaseConnectionSettings.swift | 12 ++- .../Bundle/ExportableConnection.swift | 22 ++++- .../TableProImport/Import/ImportPlanner.swift | 3 +- .../MSSQLConnectionOptions.swift | 11 +-- .../TableProSync/SyncRecordMapper.swift | 2 +- .../TeradataConnectionConfig.swift | 7 ++ .../TrinoRedirectPolicy.swift | 9 ++ .../TrinoStatementClient.swift | 19 +++- .../ConnectionSecretOptionsTests.swift | 65 +++++++++++++ .../FirebaseConnectionSettingsTests.swift | 73 +++++++++++++++ .../ConnectionBundleCodecTests.swift | 28 ++++++ .../ImportPlannerConnectionTests.swift | 4 +- .../ShareableAdditionalFieldsTests.swift | 38 ++++++++ ...SSQLConnectionOptionsAuthMethodTests.swift | 7 +- .../ConnectionSyncSchemaTests.swift | 15 +++ .../TeradataTunnelTLSTests.swift | 25 +++++ .../TrinoNextURITests.swift | 92 +++++++++++++++++++ .../CassandraConnection.swift | 6 ++ .../CassandraPlugin.swift | 1 + .../DamengDriverPlugin/DamengPlugin.swift | 10 ++ apple/Plugins/DamengDriverPlugin/README.md | 2 +- .../MSSQLDriverPlugin/MSSQLPlugin.swift | 2 +- .../MongoDBConnectionURI.swift | 18 ++++ .../MongoDBDriverPlugin/MongoDBPlugin.swift | 7 ++ .../MongoDBPluginDriver.swift | 7 ++ .../TeradataDriverPlugin/TeradataPlugin.swift | 5 +- .../TeradataSSLMapping.swift | 12 ++- apple/TablePro/AppDelegate.swift | 1 + .../ConnectionCredentialResolver.swift | 15 ++- .../Database/MongoToolsConnectionString.swift | 21 ++++- .../Core/Database/MongoTypedParameters.swift | 5 +- .../Core/Database/NativeDumpService.swift | 12 ++- ...ginMetadataRegistry+RegistryDefaults.swift | 8 ++ .../Services/ProjectImport/DotenvParser.swift | 2 +- .../ProjectImport/ProjectFolderScanner.swift | 12 ++- .../ProjectScanEnvironmentIndex.swift | 3 +- .../SpringPropertiesExtractor.swift | 2 +- .../ProjectImport/SpringYamlExtractor.swift | 2 +- .../Core/Storage/ConnectionStorage.swift | 61 +++++++++++- .../Storage/CredentialProfileStorage.swift | 4 +- .../TablePro/Core/Sync/SyncCoordinator.swift | 21 ++++- .../TablePro/Core/Sync/SyncRecordMapper.swift | 14 ++- .../TransientConnectionFactory.swift | 19 +++- .../Core/Utilities/UI/AlertHelper.swift | 9 ++ .../TablePro/Resources/Localizable.xcstrings | 14 +++ .../Connection/Import/ImportReviewStep.swift | 32 +++---- .../ConnectionFormCoordinator.swift | 30 +++++- .../CredentialProfileEditorSheet.swift | 2 +- .../TableProMobile/AppState.swift | 17 +++- .../Drivers/DriverSSLConfiguration.swift | 8 ++ .../TableProMobile/Drivers/MSSQLDriver.swift | 16 +++- .../Drivers/MongoDBDialPlan.swift | 20 ++++ .../TableProMobile/Localizable.xcstrings | 6 ++ .../Platform/ConnectionSecretKind.swift | 7 +- .../Platform/IOSDriverFactory.swift | 11 ++- .../Platform/MongoURISecretMigration.swift | 58 ++++++++++++ .../ViewModels/ConnectionFormViewModel.swift | 24 ++++- .../Views/ConnectionFormView.swift | 10 +- .../ConnectionFormViewModelIAMTests.swift | 23 +++++ .../ConnectionFormViewModelOracleTests.swift | 4 +- .../ConnectionFormViewModelSSLTests.swift | 8 +- .../ConnectionSecretsTests.swift | 2 + .../MongoURISecretKeychainMoveTests.swift | 58 ++++++++++++ .../Drivers/DriverSSLConfigurationTests.swift | 31 +++++++ .../Drivers/MongoDBOnIOSTests.swift | 18 ++++ .../MongoDBPluginConnectionTests.swift | 26 ++++++ ...nectionCredentialResolverPgpassTests.swift | 31 +++++++ .../ProjectFolderScannerTests.swift | 49 ++++++++++ .../CredentialProfileImportTests.swift | 4 +- .../MongoURISecretMigrationTests.swift | 89 ++++++++++++++++++ .../SyncRecordMapperConnectionTests.swift | 33 +++++++ .../ConnectionURLImportUsernameTests.swift | 17 ++++ .../Database/NativeDumpRegistryTests.swift | 18 ++++ .../ConnectionFormURLImportTests.swift | 21 +++++ .../Views/Connection/ImportReviewTests.swift | 22 +++++ .../+ghsa-j2xw-m3p7-3m64.security.1.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.10.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.2.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.3.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.4.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.5.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.6.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.7.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.8.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.9.md | 1 + .../+ghsa-j2xw-m3p7-3m64.security.md | 1 + .../scripts/probes/check-mssql-encryption.sh | 1 + 89 files changed, 1427 insertions(+), 99 deletions(-) create mode 100644 apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionSecretOptions.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProConnectionLibraryTests/ConnectionSecretOptionsTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProFirebaseCoreTests/FirebaseConnectionSettingsTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataTunnelTLSTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProTrinoCoreTests/TrinoNextURITests.swift create mode 100644 apple/TableProMobile/TableProMobile/Platform/MongoURISecretMigration.swift create mode 100644 apple/TableProMobile/TableProMobileTests/ConnectionLibrary/MongoURISecretKeychainMoveTests.swift create mode 100644 apple/TableProTests/Core/Database/ConnectionCredentialResolverPgpassTests.swift create mode 100644 apple/TableProTests/Core/Storage/MongoURISecretMigrationTests.swift create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.1.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.10.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.2.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.3.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.4.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.5.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.6.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.7.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.8.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.9.md create mode 100644 apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.md diff --git a/apple/Packages/TableProCore/Package.swift b/apple/Packages/TableProCore/Package.swift index 0aaa335a4a..9a57307662 100644 --- a/apple/Packages/TableProCore/Package.swift +++ b/apple/Packages/TableProCore/Package.swift @@ -273,6 +273,11 @@ let package = Package( dependencies: ["TableProBigtableCore", "TableProGoogleCloud"], path: "Tests/TableProBigtableCoreTests" ), + .testTarget( + name: "TableProFirebaseCoreTests", + dependencies: ["TableProFirebaseCore"], + path: "Tests/TableProFirebaseCoreTests" + ), .testTarget( name: "TableProSpannerCoreTests", dependencies: ["TableProSpannerCore"], diff --git a/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionSecretOptions.swift b/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionSecretOptions.swift new file mode 100644 index 0000000000..1247f32dad --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionSecretOptions.swift @@ -0,0 +1,87 @@ +import Foundation + +// MongoDB connection-string options that hold a credential. A pasted URI keeps unknown options as +// plain `mongoParam_` fields, so these are named here to keep them out of exports, links and sync. +public enum ConnectionSecretOptions { + public static let mongoOptionPrefix = "mongoParam_" + public static let mongoSessionTokenFieldId = "mongoAWSSessionToken" + + static let mongoSecretOptions: Set = [ + "tlscertificatekeyfilepassword", + "sslclientcertificatekeypassword", + "proxypassword", + ] + static let mongoKeyPassphraseOptions: Set = [ + "tlscertificatekeyfilepassword", + "sslclientcertificatekeypassword", + ] + static let mechanismPropertiesOption = "authmechanismproperties" + static let sessionTokenProperty = "AWS_SESSION_TOKEN" + + public static func isSecret(key: String, value: String) -> Bool { + if key.caseInsensitiveCompare(mongoSessionTokenFieldId) == .orderedSame { return true } + guard let option = mongoOption(in: key) else { return false } + if mongoSecretOptions.contains(option) { return true } + return option == mechanismPropertiesOption && splitSessionToken(from: value).sessionToken != nil + } + + // Mechanism properties keep everything but the token. + public static func removingSecrets(from fields: [String: String]) -> [String: String] { + var kept: [String: String] = [:] + for (key, value) in fields { + guard isSecret(key: key, value: value) else { + kept[key] = value + continue + } + if let option = mongoOption(in: key), isMongoMechanismProperties(option: option), + let remaining = splitSessionToken(from: value).remaining { + kept[key] = remaining + } + } + return kept + } + + // The lowercased option name of a `mongoParam_` field, nil for any other field. + public static func mongoOption(in key: String) -> String? { + guard key.count > mongoOptionPrefix.count, + key.lowercased().hasPrefix(mongoOptionPrefix.lowercased()) + else { return nil } + return String(key.dropFirst(mongoOptionPrefix.count)).lowercased() + } + + public static func isMongoKeyPassphrase(option: String) -> Bool { + mongoKeyPassphraseOptions.contains(option.lowercased()) + } + + public static func isMongoProxyPassword(option: String) -> Bool { + option.lowercased() == "proxypassword" + } + + public static func isMongoMechanismProperties(option: String) -> Bool { + option.lowercased() == mechanismPropertiesOption + } + + // `authMechanismProperties` is `KEY:value` pairs joined by commas; libmongoc reads keys without case. + public static func splitSessionToken(from properties: String) -> (remaining: String?, sessionToken: String?) { + var kept: [String] = [] + var token: String? + for pair in properties.split(separator: ",", omittingEmptySubsequences: true) { + let parts = pair.split(separator: ":", maxSplits: 1, omittingEmptySubsequences: false) + let name = parts[0].trimmingCharacters(in: .whitespaces) + if name.caseInsensitiveCompare(sessionTokenProperty) == .orderedSame { + let value = parts.count > 1 ? String(parts[1]) : "" + if !value.isEmpty { token = value } + } else { + kept.append(String(pair)) + } + } + return (kept.isEmpty ? nil : kept.joined(separator: ","), token) + } + + public static func joiningSessionToken(_ token: String, to properties: String?) -> String { + let remaining = properties.flatMap { splitSessionToken(from: $0).remaining } + let pair = "\(sessionTokenProperty):\(token)" + guard let remaining else { return pair } + return "\(remaining),\(pair)" + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProFirebaseCore/Config/FirebaseConnectionSettings.swift b/apple/Packages/TableProCore/Sources/TableProFirebaseCore/Config/FirebaseConnectionSettings.swift index 84175aef2d..b76179a1c6 100644 --- a/apple/Packages/TableProCore/Sources/TableProFirebaseCore/Config/FirebaseConnectionSettings.swift +++ b/apple/Packages/TableProCore/Sources/TableProFirebaseCore/Config/FirebaseConnectionSettings.swift @@ -113,8 +113,16 @@ public struct FirebaseConnectionSettings: Sendable, Equatable { return url } let trusted = host.hasSuffix(".firebaseio.com") || host.hasSuffix(".firebasedatabase.app") - guard scheme == "https", trusted else { throw invalidURL(text) } - return URL(string: "https://\(host)/") ?? url + guard scheme == "https", trusted, isDNSName(host), let instance = URL(string: "https://\(host)/") else { + throw invalidURL(text) + } + return instance + } + + // `URL.host` is percent-decoded, so an encoded `/`, `:`, `?`, `#` or `@` would pass the suffix check and then end + // the host early once the text is parsed again. + static func isDNSName(_ host: String) -> Bool { + host.range(of: #"^[a-z0-9-]+(\.[a-z0-9-]+)+$"#, options: .regularExpression) != nil } static func parseEmulatorHost(_ text: String?) throws -> String { diff --git a/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ExportableConnection.swift b/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ExportableConnection.swift index c6e2ede79d..178826dc3c 100644 --- a/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ExportableConnection.swift +++ b/apple/Packages/TableProCore/Sources/TableProImport/Bundle/ExportableConnection.swift @@ -144,17 +144,24 @@ public extension ExportableConnection { private static let queryTimeoutSecondsRange = 0 ... Int(Int32.max) / 1_000 static let importBlockedAdditionalFieldKeys: Set = [ + "beancountrunledgerplugins", "postdisconnectscript", "preconnectscript", "pretunnelhost", "pretunnelport", "promptforpassword", + "snowflakeconnectionname", "spendpoint", "sslclientkeypassphrase", "usepgpass", ] - static let importBlockedAdditionalFieldPrefixes: Set = ["aws"] + static let importBlockedAdditionalFieldPrefixes: Set = ["aws", "entra"] + + // Values that pick a credential kept on this Mac, under keys whose other values are plain settings. + static let importBlockedAdditionalFieldValues: [String: Set] = [ + "mongoauthmechanism": ["mongodb-aws"], + ] static func isImportBlockedAdditionalFieldKey(_ key: String) -> Bool { let normalized = key.lowercased() @@ -162,12 +169,19 @@ public extension ExportableConnection { return importBlockedAdditionalFieldPrefixes.contains { normalized.hasPrefix($0) } } + static func isImportBlockedAdditionalField(key: String, value: String) -> Bool { + if isImportBlockedAdditionalFieldKey(key) { return true } + if ConnectionSecretOptions.isSecret(key: key, value: value) { return true } + let blockedValues = importBlockedAdditionalFieldValues[key.lowercased()] ?? [] + return blockedValues.contains(value.trimmingCharacters(in: .whitespaces).lowercased()) + } + static func shareableAdditionalFields( _ fields: [String: String], excluding excludedKeys: Set = [] ) -> [String: String]? { - let shareable = fields.filter { key, _ in - !excludedKeys.contains(key) && !isImportBlockedAdditionalFieldKey(key) + let shareable = fields.filter { key, value in + !excludedKeys.contains(key) && !isImportBlockedAdditionalField(key: key, value: value) } return shareable.isEmpty ? nil : shareable } @@ -192,7 +206,7 @@ public extension ExportableConnection { /// An explicit timeout that is out of range is dropped, not replaced by a legacy additional field. func sanitizedForImport() -> ExportableConnection { - var allowed = (additionalFields ?? [:]).filter { !Self.isImportBlockedAdditionalFieldKey($0.key) } + var allowed = (additionalFields ?? [:]).filter { !Self.isImportBlockedAdditionalField(key: $0.key, value: $0.value) } let legacyConnectTimeout = allowed.removeValue(forKey: Self.connectTimeoutSecondsKey).flatMap(Int.init) let legacyQueryTimeout = allowed.removeValue(forKey: Self.queryTimeoutSecondsKey).flatMap(Int.init) var copy = self diff --git a/apple/Packages/TableProCore/Sources/TableProImport/Import/ImportPlanner.swift b/apple/Packages/TableProCore/Sources/TableProImport/Import/ImportPlanner.swift index 5cf8e769e7..aa939d542c 100644 --- a/apple/Packages/TableProCore/Sources/TableProImport/Import/ImportPlanner.swift +++ b/apple/Packages/TableProCore/Sources/TableProImport/Import/ImportPlanner.swift @@ -88,7 +88,8 @@ extension ImportPlanner { ref: profile.ref, name: profile.name, username: profile.username, - passwordMode: profile.passwordMode == .pgpass ? .pgpass : .prompt, + // A file never chooses how this Mac finds a password: pgpass would read ~/.pgpass for its host. + passwordMode: .prompt, secureFieldIds: profile.secureFieldIds )) } diff --git a/apple/Packages/TableProCore/Sources/TableProMSSQLCore/MSSQLConnectionOptions.swift b/apple/Packages/TableProCore/Sources/TableProMSSQLCore/MSSQLConnectionOptions.swift index ac0cf7b97b..aedd33873c 100644 --- a/apple/Packages/TableProCore/Sources/TableProMSSQLCore/MSSQLConnectionOptions.swift +++ b/apple/Packages/TableProCore/Sources/TableProMSSQLCore/MSSQLConnectionOptions.swift @@ -14,14 +14,11 @@ public struct MSSQLConnectionOptions: Sendable, Equatable { public var kerberosCachePath: String? public var kerberosServicePrincipal: String? - /// How far to check the server certificate. Set after init by the plugin from the - /// connection's SSL mode, so the existing initializer keeps its signature. - public var certificateVerification: MSSQLCertificateVerification = .none + // No default: a caller that forgets it would encrypt without checking who answers. + public var certificateVerification: MSSQLCertificateVerification public var caCertificatePath: String? - /// Microsoft Entra ID access token, sent in place of a user name and password. Set after - /// init for the same reason as `certificateVerification`. Only read when - /// `authMethod == .entra`. + // Sent in place of a user name and password, read only when `authMethod == .entra`. public var fedAuthToken: String? public static let defaultPort = 1_433 @@ -38,6 +35,7 @@ public struct MSSQLConnectionOptions: Sendable, Equatable { database: String, schema: String = MSSQLConnectionOptions.defaultSchema, encryptionLevel: MSSQLEncryptionLevel = MSSQLConnectionOptions.defaultEncryptionLevel, + certificateVerification: MSSQLCertificateVerification, applicationName: String = MSSQLConnectionOptions.defaultApplicationName, loginTimeoutSeconds: Int = MSSQLConnectionOptions.defaultLoginTimeoutSeconds, authMethod: MSSQLAuthMethod = .sqlServer, @@ -60,6 +58,7 @@ public struct MSSQLConnectionOptions: Sendable, Equatable { self.database = database self.schema = schema self.encryptionLevel = encryptionLevel + self.certificateVerification = certificateVerification self.applicationName = applicationName self.loginTimeoutSeconds = loginTimeoutSeconds } diff --git a/apple/Packages/TableProCore/Sources/TableProSync/SyncRecordMapper.swift b/apple/Packages/TableProCore/Sources/TableProSync/SyncRecordMapper.swift index 3faae1906d..7c02a1ec38 100644 --- a/apple/Packages/TableProCore/Sources/TableProSync/SyncRecordMapper.swift +++ b/apple/Packages/TableProCore/Sources/TableProSync/SyncRecordMapper.swift @@ -332,7 +332,7 @@ public enum SyncRecordMapper { } private static func syncedAdditionalFields(for connection: DatabaseConnection) -> [String: String] { - var fields = connection.additionalFields + var fields = ConnectionSecretOptions.removingSecrets(from: connection.additionalFields) fields.removeValue(forKey: DatabaseConnection.connectTimeoutSecondsKey) fields.removeValue(forKey: DatabaseConnection.queryTimeoutSecondsKey) if let connectTimeoutSeconds = validConnectTimeout(connection.connectTimeoutSeconds) { diff --git a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnectionConfig.swift b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnectionConfig.swift index a16581f0de..64a9431ff3 100644 --- a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnectionConfig.swift +++ b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnectionConfig.swift @@ -53,6 +53,13 @@ public struct TeradataTLSOptions: Sendable { } public static let disabled = TeradataTLSOptions() + + // TLS goes to the HTTPS port, which a tunnel never forwards, so it would reach whatever listens on this Mac's + // port 443. Nil when the mode demands TLS; Preferred goes through the tunnel without it. + public func throughTunnel() -> TeradataTLSOptions? { + guard enabled else { return self } + return allowPlaintextFallback ? .disabled : nil + } } public struct TeradataConnectionConfig: Sendable { diff --git a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoRedirectPolicy.swift b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoRedirectPolicy.swift index 39748972a0..4991dd3358 100644 --- a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoRedirectPolicy.swift +++ b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoRedirectPolicy.swift @@ -16,6 +16,15 @@ enum TrinoRedirectPolicy { return .tlsHandshakeFailed(kind: .serverRejectedPlaintext, serverMessage: "\(statusCode) redirect to \(shown)") } + static func isSameServer(_ target: URL, as requestURL: URL) -> Bool { + guard let host = target.host?.lowercased(), let requestHost = requestURL.host?.lowercased() else { + return false + } + return target.scheme?.lowercased() == requestURL.scheme?.lowercased() + && host == requestHost + && effectivePort(of: target) == effectivePort(of: requestURL) + } + private static func target(of response: TrinoHTTPResponse, relativeTo requestURL: URL) -> URL? { guard let location = response.headers.first("Location")?.trimmingCharacters(in: .whitespaces), !location.isEmpty, diff --git a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift index 9e502166ae..c13edc6ba4 100644 --- a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift +++ b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift @@ -344,11 +344,20 @@ public final class TrinoStatementClient: @unchecked Sendable { guard let url = URL(string: uri) else { throw TrinoError.invalidResponse("\(config.flavor.productName) returned an invalid nextUri") } - guard config.useTLS, url.scheme?.lowercased() != "https" else { return url } - throw TrinoError.invalidResponse( - "\(config.flavor.productName) answered an HTTPS request with a plain http:// address, so it was not followed. " - + "A coordinator behind a TLS proxy needs http-server.process-forwarded=true." - ) + if config.useTLS, url.scheme?.lowercased() != "https" { + throw TrinoError.invalidResponse( + "\(config.flavor.productName) answered an HTTPS request with a plain http:// address, so it was not followed. " + + "A coordinator behind a TLS proxy needs http-server.process-forwarded=true." + ) + } + // The request carries the connection's credential, so nextUri is a redirect in the body and stays on the server. + guard let statementURL = config.statementURL, TrinoRedirectPolicy.isSameServer(url, as: statementURL) else { + throw TrinoError.invalidResponse( + "\(config.flavor.productName) continued the statement on another host or port, so it was not followed. " + + "A coordinator behind a proxy needs http-server.process-forwarded=true." + ) + } + return url } private func failure(for response: TrinoHTTPResponse) -> TrinoError { diff --git a/apple/Packages/TableProCore/Tests/TableProConnectionLibraryTests/ConnectionSecretOptionsTests.swift b/apple/Packages/TableProCore/Tests/TableProConnectionLibraryTests/ConnectionSecretOptionsTests.swift new file mode 100644 index 0000000000..b39338d7f3 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProConnectionLibraryTests/ConnectionSecretOptionsTests.swift @@ -0,0 +1,65 @@ +import TableProConnectionLibrary +import Testing + +struct ConnectionSecretOptionsTests { + @Test("Credential options of a MongoDB connection string are secret in any case", arguments: [ + "mongoParam_tlsCertificateKeyFilePassword", + "mongoparam_TLSCERTIFICATEKEYFILEPASSWORD", + "mongoParam_sslClientCertificateKeyPassword", + "mongoParam_proxyPassword", + "mongoAWSSessionToken" + ]) + func credentialOptionsAreSecret(key: String) { + #expect(ConnectionSecretOptions.isSecret(key: key, value: "hunter2")) + } + + @Test("Ordinary options and other fields are not secret", arguments: [ + "mongoParam_proxyHost", + "mongoParam_retryWrites", + "mongoParam_", + "tlsCertificateKeyFilePassword", + "mongoAuthSource" + ]) + func ordinaryOptionsAreNotSecret(key: String) { + #expect(!ConnectionSecretOptions.isSecret(key: key, value: "x")) + } + + @Test("Mechanism properties are secret only while they carry a session token") + func mechanismPropertiesCarryingAToken() { + let key = "mongoParam_authMechanismProperties" + #expect(ConnectionSecretOptions.isSecret(key: key, value: "AWS_SESSION_TOKEN:FQoG")) + #expect(ConnectionSecretOptions.isSecret(key: key, value: "SERVICE_NAME:mongodb, aws_session_token:FQoG")) + #expect(!ConnectionSecretOptions.isSecret(key: key, value: "SERVICE_NAME:mongodb")) + #expect(!ConnectionSecretOptions.isSecret(key: key, value: "AWS_SESSION_TOKEN:")) + } + + @Test("The session token splits out of the properties and joins back") + func splitAndJoin() { + let split = ConnectionSecretOptions.splitSessionToken(from: "SERVICE_NAME:mongodb,AWS_SESSION_TOKEN:ab/c+d=") + #expect(split.remaining == "SERVICE_NAME:mongodb") + #expect(split.sessionToken == "ab/c+d=") + #expect(ConnectionSecretOptions.splitSessionToken(from: "AWS_SESSION_TOKEN:t").remaining == nil) + #expect(ConnectionSecretOptions.joiningSessionToken("t", to: nil) == "AWS_SESSION_TOKEN:t") + #expect( + ConnectionSecretOptions.joiningSessionToken("t", to: "SERVICE_NAME:mongodb,AWS_SESSION_TOKEN:old") + == "SERVICE_NAME:mongodb,AWS_SESSION_TOKEN:t" + ) + } + + @Test("Removing secrets keeps every other field and the properties beside the token") + func removingSecrets() { + let fields = [ + "mongoParam_tlsCertificateKeyFilePassword": "hunter2", + "mongoParam_proxyPassword": "proxy-secret", + "mongoParam_authMechanismProperties": "SERVICE_NAME:mongodb,AWS_SESSION_TOKEN:FQoG", + "mongoParam_retryWrites": "true", + "mongoAuthSource": "admin" + ] + + #expect(ConnectionSecretOptions.removingSecrets(from: fields) == [ + "mongoParam_authMechanismProperties": "SERVICE_NAME:mongodb", + "mongoParam_retryWrites": "true", + "mongoAuthSource": "admin" + ]) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProFirebaseCoreTests/FirebaseConnectionSettingsTests.swift b/apple/Packages/TableProCore/Tests/TableProFirebaseCoreTests/FirebaseConnectionSettingsTests.swift new file mode 100644 index 0000000000..4bb22a4b14 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProFirebaseCoreTests/FirebaseConnectionSettingsTests.swift @@ -0,0 +1,73 @@ +import Foundation +@testable import TableProFirebaseCore +import Testing + +struct FirebaseConnectionSettingsTests { + @Test("A Firebase host is kept and everything around it is dropped") + func keepsFirebaseHosts() throws { + let american = try FirebaseConnectionSettings.parseDatabaseURL( + "https://Shop-default-rtdb.firebaseio.com/users?x=1", emulator: false + ) + #expect(american.absoluteString == "https://shop-default-rtdb.firebaseio.com/") + let european = try FirebaseConnectionSettings.parseDatabaseURL( + "https://shop.europe-west1.firebasedatabase.app", emulator: false + ) + #expect(european.absoluteString == "https://shop.europe-west1.firebasedatabase.app/") + let spelled = try FirebaseConnectionSettings.parseDatabaseURL( + "https://user:pass@shop.firebaseio.com:8443/", emulator: false + ) + #expect(spelled.absoluteString == "https://shop.firebaseio.com/") + } + + @Test("A host that is not Firebase's is refused", arguments: [ + "https://collector.example.com/", + "https://firebaseio.com.example.com/", + "https://collector.example.com/.firebaseio.com", + "http://shop.firebaseio.com/" + ]) + func refusesOtherHosts(text: String) { + #expect(throws: FirebaseError.self) { + try FirebaseConnectionSettings.parseDatabaseURL(text, emulator: false) + } + } + + @Test("An encoded delimiter cannot end the host before the Firebase suffix", arguments: [ + "https://collector.example.com%2F.firebaseio.com", + "https://collector.example.com%2f.firebasedatabase.app", + "https://collector.example.com%3A8443%2F.firebaseio.com", + "https://collector.example.com%3F.firebaseio.com", + "https://collector.example.com%23.firebasedatabase.app", + "https://collector.example.com%5C.firebaseio.com", + "https://user%40collector.example.com%2F.firebaseio.com", + "https://collector.example.com%00.firebaseio.com", + "https://collector.example.com%09.firebaseio.com", + "https://collector.example.com\u{FF0F}.firebaseio.com" + ]) + func refusesEncodedDelimitersInTheHost(text: String) { + #expect(throws: FirebaseError.self) { + try FirebaseConnectionSettings.parseDatabaseURL(text, emulator: false) + } + } + + @Test("An imported Database URL cannot send Application Default Credentials to another host") + func importedFieldsStayOnFirebaseHosts() { + #expect(throws: FirebaseError.self) { + try FirebaseConnectionSettings.parse(fields: [ + FirebaseFieldID.driverVariant: FirebaseVariant.realtimeDatabase.rawValue, + FirebaseFieldID.authMethod: FirebaseAuthMethod.applicationDefault.rawValue, + FirebaseFieldID.databaseURL: "https://collector.example.com%2F.firebaseio.com" + ]) + } + } + + @Test("A server's correctUrl is adopted only when its host is Firebase's") + func correctURLStaysOnFirebaseHosts() { + let real = Data(#"{"correctUrl": "https://shop.europe-west1.firebasedatabase.app"}"#.utf8) + #expect( + RealtimeDatabaseClient.correctURL(in: real)?.absoluteString + == "https://shop.europe-west1.firebasedatabase.app/" + ) + let crafted = Data(#"{"correctUrl": "https://collector.example.com%2F.firebaseio.com"}"#.utf8) + #expect(RealtimeDatabaseClient.correctURL(in: crafted) == nil) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProImportTests/ConnectionBundleCodecTests.swift b/apple/Packages/TableProCore/Tests/TableProImportTests/ConnectionBundleCodecTests.swift index aa6148d605..c46a106687 100644 --- a/apple/Packages/TableProCore/Tests/TableProImportTests/ConnectionBundleCodecTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProImportTests/ConnectionBundleCodecTests.swift @@ -300,6 +300,34 @@ struct ConnectionBundleCodecTests { #expect(imported.queryTimeoutSeconds == 0) } + @Test("Decoding strips the fields that pick a credential kept on this device or let a ledger run code") + func decodeStripsLocalCredentialSelectors() throws { + let settings = Self.settings(additionalFields: [ + "mssqlAuthMethod": "entra", + "entraTenantId": "5b0f3c1e-0000-4000-8000-00000000c0de", + "entraClientId": "9a1d7e42-0000-4000-8000-0000000000aa", + "snowflakeAccount": "org-account", + "snowflakeConnectionName": "default", + "beancountRunLedgerPlugins": "true", + "mongoAuthMechanism": "MONGODB-AWS" + ]) + let data = try ConnectionBundleCodec.encode(Self.bundle(settings: [settings])) + + let imported = try #require(ConnectionBundleCodec.decode(data).connections.first?.settings) + + #expect(imported.additionalFields == ["mssqlAuthMethod": "entra", "snowflakeAccount": "org-account"]) + } + + @Test("Decoding keeps a MongoDB auth mechanism that signs in with what the file carries") + func decodeKeepsOrdinaryMongoMechanisms() throws { + let settings = Self.settings(additionalFields: ["mongoAuthMechanism": "SCRAM-SHA-256"]) + let data = try ConnectionBundleCodec.encode(Self.bundle(settings: [settings])) + + let imported = try #require(ConnectionBundleCodec.decode(data).connections.first?.settings) + + #expect(imported.additionalFields == ["mongoAuthMechanism": "SCRAM-SHA-256"]) + } + @Test("An older file without timeout fields decodes them as nil") func olderFileWithoutTimeouts() throws { let json = Data(""" diff --git a/apple/Packages/TableProCore/Tests/TableProImportTests/ImportPlannerConnectionTests.swift b/apple/Packages/TableProCore/Tests/TableProImportTests/ImportPlannerConnectionTests.swift index 1fb51b4df8..ec321769e1 100644 --- a/apple/Packages/TableProCore/Tests/TableProImportTests/ImportPlannerConnectionTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProImportTests/ImportPlannerConnectionTests.swift @@ -257,7 +257,7 @@ struct ImportPlannerConnectionTests { #expect(kept.settings.tunnelCommand?.command == "/usr/bin/forward --listen {port}") } - @Test("An imported profile asks for its password unless it reads pgpass") + @Test("An imported profile asks for its password, whatever mode the file names") func profilePasswordModes() throws { let bundle = try Fixtures.makeBundle( connections: [ @@ -275,7 +275,7 @@ struct ImportPlannerConnectionTests { let result = plan(preview) #expect(result.credentialProfiles.map(\.ref) == ["p1", "p2"]) - #expect(result.credentialProfiles.map(\.passwordMode) == [.pgpass, .prompt]) + #expect(result.credentialProfiles.map(\.passwordMode) == [.prompt, .prompt]) #expect(result.credentialProfiles[0].secureFieldIds == ["token"]) } diff --git a/apple/Packages/TableProCore/Tests/TableProImportTests/ShareableAdditionalFieldsTests.swift b/apple/Packages/TableProCore/Tests/TableProImportTests/ShareableAdditionalFieldsTests.swift index fdf7e9e06e..16a70ceae6 100644 --- a/apple/Packages/TableProCore/Tests/TableProImportTests/ShareableAdditionalFieldsTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProImportTests/ShareableAdditionalFieldsTests.swift @@ -19,6 +19,44 @@ struct ShareableAdditionalFieldsTests { #expect(shareable == ["connectionOptions": "-c search_path=app"]) } + @Test("Fields that pick a credential kept on this Mac are never shared") + func leavesOutLocalCredentialSelectors() { + let fields = [ + "entraTenantId": "tenant", + "EntraClientId": "client", + "snowflakeConnectionName": "default", + "beancountRunLedgerPlugins": "true", + "mongoAuthMechanism": "mongodb-aws", + "mongoAuthSource": "admin" + ] + + #expect(ExportableConnection.shareableAdditionalFields(fields) == ["mongoAuthSource": "admin"]) + } + + @Test("Secrets a MongoDB connection string carried as options are never shared") + func leavesOutMongoURISecrets() { + let fields = [ + "mongoParam_tlsCertificateKeyFilePassword": "hunter2", + "mongoParam_SSLCLIENTCERTIFICATEKEYPASSWORD": "hunter2", + "mongoParam_proxyPassword": "proxy-secret", + "mongoParam_proxyHost": "proxy.example.com", + "mongoParam_authMechanismProperties": "aws_session_token:FQoGZXIvYXdz", + "mongoParam_retryWrites": "true" + ] + + #expect(ExportableConnection.shareableAdditionalFields(fields) == [ + "mongoParam_proxyHost": "proxy.example.com", + "mongoParam_retryWrites": "true" + ]) + } + + @Test("Mechanism properties without a session token are shared") + func keepsMechanismPropertiesWithoutAToken() { + let fields = ["mongoParam_authMechanismProperties": "SERVICE_NAME:mongodb"] + + #expect(ExportableConnection.shareableAdditionalFields(fields) == fields) + } + @Test func testLeavesOutTheExcludedKeys() { let fields = ["mssqlKerberosPassword": "secret", "mssqlAuthMethod": "kerberos"] diff --git a/apple/Packages/TableProCore/Tests/TableProMSSQLCoreTests/MSSQLConnectionOptionsAuthMethodTests.swift b/apple/Packages/TableProCore/Tests/TableProMSSQLCoreTests/MSSQLConnectionOptionsAuthMethodTests.swift index 94173fc9e2..5dca4accd7 100644 --- a/apple/Packages/TableProCore/Tests/TableProMSSQLCoreTests/MSSQLConnectionOptionsAuthMethodTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProMSSQLCoreTests/MSSQLConnectionOptionsAuthMethodTests.swift @@ -10,6 +10,7 @@ struct MSSQLConnectionOptionsAuthMethodTests { user: "sa", password: "hunter2", database: "app", + certificateVerification: .none, authMethod: .sqlServer ) #expect(options.user == "sa") @@ -24,6 +25,7 @@ struct MSSQLConnectionOptionsAuthMethodTests { user: "sa", password: "hunter2", database: "app", + certificateVerification: .none, authMethod: .windows ) #expect(options.user == "") @@ -38,6 +40,7 @@ struct MSSQLConnectionOptionsAuthMethodTests { user: "sa", password: "hunter2", database: "app", + certificateVerification: .none, authMethod: .entra ) #expect(options.user == "") @@ -51,7 +54,9 @@ struct MSSQLConnectionOptionsAuthMethodTests { @Test("Default auth method is SQL Server") func defaultsToSqlServer() { - let options = MSSQLConnectionOptions(host: "h", user: "u", password: "p", database: "d") + let options = MSSQLConnectionOptions( + host: "h", user: "u", password: "p", database: "d", certificateVerification: .none + ) #expect(options.authMethod == .sqlServer) } diff --git a/apple/Packages/TableProCore/Tests/TableProSyncTests/ConnectionSyncSchemaTests.swift b/apple/Packages/TableProCore/Tests/TableProSyncTests/ConnectionSyncSchemaTests.swift index 2b70ebccfd..97058f580b 100644 --- a/apple/Packages/TableProCore/Tests/TableProSyncTests/ConnectionSyncSchemaTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSyncTests/ConnectionSyncSchemaTests.swift @@ -180,6 +180,21 @@ struct ConnectionSyncSchemaTests { #expect(SyncRecordMapper.toConnection(record)?.connectTimeoutSeconds == 12) } + @Test("a MongoDB connection-string secret never reaches the record") + func mongoURISecretsStayOffTheRecord() throws { + var connection = makeFullyPopulatedConnection() + connection.additionalFields = [ + "mongoParam_sslClientCertificateKeyPassword": "hunter2", + "mongoParam_authMechanismProperties": "SERVICE_NAME:mongodb,AWS_SESSION_TOKEN:FQoG" + ] + let record = SyncRecordMapper.toRecord(connection, zoneID: zoneID) + let data = try #require(record.fields(ConnectionSyncField.self)[.additionalFieldsJson] as? Data) + let additionalFields = try JSONDecoder().decode([String: String].self, from: data) + + #expect(additionalFields["mongoParam_sslClientCertificateKeyPassword"] == nil) + #expect(additionalFields["mongoParam_authMechanismProperties"] == "SERVICE_NAME:mongodb") + } + @Test("clearing additional fields removes their previous record value") func clearingAdditionalFieldsRemovesRecordValue() { var connection = makeFullyPopulatedConnection() diff --git a/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataTunnelTLSTests.swift b/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataTunnelTLSTests.swift new file mode 100644 index 0000000000..878e18417a --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataTunnelTLSTests.swift @@ -0,0 +1,25 @@ +import TableProTeradataCore +import Testing + +struct TeradataTunnelTLSTests { + @Test("Behind a tunnel a mode that demands TLS is refused, since TLS would dial this Mac's port 443") + func requiredTLSIsRefused() { + let required = TeradataTLSOptions(enabled: true, allowPlaintextFallback: false, modeLabel: "REQUIRE") + #expect(required.throughTunnel() == nil) + let verified = TeradataTLSOptions(enabled: true, verifiesCertificate: true, verifiesHostname: true) + #expect(verified.throughTunnel() == nil) + } + + @Test("Behind a tunnel Preferred goes through the forward without TLS") + func preferredSkipsTLS() throws { + let preferred = TeradataTLSOptions(enabled: true, allowPlaintextFallback: true, modeLabel: "PREFER") + let options = try #require(preferred.throughTunnel()) + #expect(!options.enabled) + } + + @Test("Behind a tunnel Disabled stays as it is") + func disabledIsKept() throws { + let options = try #require(TeradataTLSOptions.disabled.throughTunnel()) + #expect(!options.enabled) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProTrinoCoreTests/TrinoNextURITests.swift b/apple/Packages/TableProCore/Tests/TableProTrinoCoreTests/TrinoNextURITests.swift new file mode 100644 index 0000000000..2e15afcea7 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProTrinoCoreTests/TrinoNextURITests.swift @@ -0,0 +1,92 @@ +import Foundation +import TableProTrinoCore +import Testing + +private final class PagingTransport: TrinoTransport, @unchecked Sendable { + private let lock = NSLock() + private var recorded: [TrinoHTTPRequest] = [] + private let nextUri: String + + init(nextUri: String) { + self.nextUri = nextUri + } + + var requests: [TrinoHTTPRequest] { + lock.withLock { recorded } + } + + func send(_ request: TrinoHTTPRequest) async throws -> TrinoHTTPResponse { + lock.withLock { recorded.append(request) } + let body = request.method == .post ? #"{"id":"q1","nextUri":"\#(nextUri)"}"# : #"{"id":"q1"}"# + return TrinoHTTPResponse(statusCode: 200, headers: TrinoHeaderFields([:]), body: Data(body.utf8)) + } + + func cancelAll() {} +} + +struct TrinoNextURITests { + private func client(_ transport: PagingTransport, port: Int = 8_443, useTLS: Bool = true) -> TrinoStatementClient { + let config = TrinoClientConfig( + host: "trino.example", + port: port, + useTLS: useTLS, + user: "analyst", + auth: useTLS ? .basic(password: "secret") : .none + ) + return TrinoStatementClient(transport: transport, config: config, session: TrinoSessionState()) + } + + @Test("A nextUri on the coordinator is followed with the credential") + func nextUriOnTheCoordinatorIsFollowed() async throws { + let transport = PagingTransport(nextUri: "https://TRINO.example:8443/v1/statement/executing/q1/1") + _ = try await client(transport).execute("SELECT 1") + #expect(transport.requests.map(\.method) == [.post, .get]) + #expect(transport.requests.last?.headers["Authorization"] != nil) + } + + @Test("A nextUri that leaves out the default port is still the coordinator") + func nextUriWithoutTheDefaultPortIsFollowed() async throws { + let transport = PagingTransport(nextUri: "https://trino.example/v1/statement/executing/q1/1") + _ = try await client(transport, port: 443).execute("SELECT 1") + #expect(transport.requests.map(\.method) == [.post, .get]) + } + + @Test("A nextUri on another host or port gets no request", arguments: [ + "https://other.example:8443/v1/statement/executing/q1/1", + "https://trino.example:9443/v1/statement/executing/q1/1", + "https://trino.example/v1/statement/executing/q1/1", + "https://trino.example.other.example:8443/v1/statement/executing/q1/1", + "https://trino.example:8443@other.example/v1/statement/executing/q1/1", + "/v1/statement/executing/q1/1" + ]) + func nextUriElsewhereIsNotFollowed(nextUri: String) async { + let transport = PagingTransport(nextUri: nextUri) + await #expect(throws: TrinoError.self) { + _ = try await client(transport).execute("SELECT 1") + } + #expect(transport.requests.map(\.method) == [.post]) + } + + @Test("A connection without TLS stays on its server too", arguments: [ + "http://other.example:8080/v1/statement/executing/q1/1", + "https://trino.example:8080/v1/statement/executing/q1/1" + ]) + func plainNextUriElsewhereIsNotFollowed(nextUri: String) async { + let transport = PagingTransport(nextUri: nextUri) + await #expect(throws: TrinoError.self) { + _ = try await client(transport, port: 8_080, useTLS: false).execute("SELECT 1") + } + #expect(transport.requests.map(\.method) == [.post]) + } + + @Test("A plain http:// nextUri on a TLS connection keeps the message naming the proxy setting") + func plainNextUriOnTLSNamesTheProxySetting() async { + let transport = PagingTransport(nextUri: "http://trino.example:8443/v1/statement/executing/q1/1") + await #expect { + _ = try await client(transport).execute("SELECT 1") + } throws: { error in + error.localizedDescription.contains("plain http:// address") + } + #expect(transport.requests.map(\.method) == [.post]) + } +} diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift b/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift index 4c0e7042dd..9643edd19b 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift @@ -49,6 +49,7 @@ actor CassandraConnectionActor { sslClientCertPath: String?, sslClientKeyPath: String?, sslClientKeyPassphrase: String?, + pinnedToContactPoint: Bool, awsCredentials: AWSCredentials? = nil, awsRegion: String? = nil, connectTimeout: CassandraConnectTimeout = CassandraConnectTimeout( @@ -62,6 +63,11 @@ actor CassandraConnectionActor { cass_cluster_set_contact_points(cluster, host) cass_cluster_set_port(cluster, Int32(port)) + // Behind a tunnel the peers system.peers names sit outside it, and the tunnel already turned certificate + // checks off, so they would get the password unverified. + if pinnedToContactPoint { + cass_cluster_set_whitelist_filtering(cluster, host) + } if let awsCredentials, let awsRegion, !awsRegion.isEmpty { CassandraSigV4Authenticator.apply(to: cluster, credentials: awsCredentials, region: awsRegion) diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift index 0be08c02fb..933d4af78b 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift @@ -228,6 +228,7 @@ internal final class CassandraPluginDriver: PluginDatabaseDriver, @unchecked Sen sslClientCertPath: clientCertPath, sslClientKeyPath: clientKeyPath, sslClientKeyPassphrase: clientKeyPassphrase, + pinnedToContactPoint: config.additionalFields["preTunnelHost"]?.isEmpty == false, awsCredentials: awsCredentials, awsRegion: awsRegion, connectTimeout: CassandraConnectTimeout(milliseconds: Int(remainingConnectMilliseconds)) diff --git a/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift b/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift index f4a18bb052..166b2bc14f 100644 --- a/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift +++ b/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift @@ -173,6 +173,16 @@ final class DamengPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func connect() async throws { + // The bridge opens plain TCP only. The form offers no SSL mode, but a dm:// URL or an import can store one. + switch config.ssl.mode { + case .disabled, .preferred: + break + case .required, .verifyCa, .verifyIdentity: + throw DamengError(message: String(localized: """ + This connection asks for TLS, which the Dameng driver does not support. Add the connection again \ + without an SSL mode, and use an SSH, SOCKS or Cloudflare tunnel on a network you do not trust. + """)) + } try await connection.connect( host: config.host, port: config.port, diff --git a/apple/Plugins/DamengDriverPlugin/README.md b/apple/Plugins/DamengDriverPlugin/README.md index ba140a4b52..da281d1012 100644 --- a/apple/Plugins/DamengDriverPlugin/README.md +++ b/apple/Plugins/DamengDriverPlugin/README.md @@ -27,4 +27,4 @@ Use `scripts/build-dameng.sh arm64` or `x86_64` only for architecture-specific d ## Security and Limitations -The parameter binder recognizes placeholders only in SQL code, escapes text literals, and encodes bytes with `HEXTORAW`. Response bodies and LOB content are capped at 64 MiB. Native TLS is not available; use SSH, SOCKS, or Cloudflare tunneling for untrusted networks. Native binary and off-row LOB reads remain unsupported by the transport; use `RAWTOHEX` for binary values and cast CLOB values to `VARCHAR`. +The parameter binder recognizes placeholders only in SQL code, escapes text literals, and encodes bytes with `HEXTORAW`. Response bodies and LOB content are capped at 64 MiB. Native TLS is not available; use SSH, SOCKS, or Cloudflare tunneling for untrusted networks. A connection whose SSL mode is Required, Verify CA, or Verify Identity, which a `dm://` URL or an import can store, is refused before the sign-in is sent. Native binary and off-row LOB reads remain unsupported by the transport; use `RAWTOHEX` for binary values and cast CLOB values to `VARCHAR`. diff --git a/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift b/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift index 759a8528aa..ccfe6c1fbe 100644 --- a/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift +++ b/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift @@ -389,12 +389,12 @@ final class MSSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { database: config.database, schema: currentSchemaName, encryptionLevel: MSSQLSSLMapping.encryptionLevel(for: config.ssl.mode), + certificateVerification: MSSQLSSLMapping.certificateVerification(for: config.ssl.mode), loginTimeoutSeconds: max(1, (remainingMilliseconds + 999) / 1_000), authMethod: authMethod, kerberosCachePath: kerberosCachePath, kerberosServicePrincipal: kerberosServicePrincipal ) - options.certificateVerification = MSSQLSSLMapping.certificateVerification(for: config.ssl.mode) options.caCertificatePath = config.ssl.caCertificatePath options.fedAuthToken = fedAuthToken conn = FreeTDSConnection(options: options, connectTimeoutMilliseconds: remainingMilliseconds) diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnectionURI.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnectionURI.swift index 7b5fc7d60c..7aa6598bfb 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnectionURI.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnectionURI.swift @@ -115,6 +115,24 @@ nonisolated struct MongoDBConnectionURI: Sendable { return options } + // The Keychain holds these, so they replace any spelling a pasted connection string left among the options. + static func applySecrets(to params: inout [String: String], keyPassphrase: String?, sessionToken: String?) { + if let keyPassphrase, !keyPassphrase.isEmpty { + params = params.filter { canonicalKey($0.key) != "tlscertificatekeyfilepassword" } + params["tlsCertificateKeyFilePassword"] = keyPassphrase + } + if let sessionToken, !sessionToken.isEmpty { + let current = params.first { $0.key.lowercased() == "authmechanismproperties" } + params = params.filter { $0.key.lowercased() != "authmechanismproperties" } + let kept = (current?.value ?? "").split(separator: ",").filter { pair in + let name = pair.split(separator: ":", maxSplits: 1).first ?? "" + return name.trimmingCharacters(in: .whitespaces).caseInsensitiveCompare("AWS_SESSION_TOKEN") != .orderedSame + } + params["authMechanismProperties"] = (kept.map(String.init) + ["AWS_SESSION_TOKEN:\(sessionToken)"]) + .joined(separator: ",") + } + } + // libmongoc still reads the old `ssl` names as their `tls` options, so a typed one would turn off a check the SSL // mode asked for. static let deprecatedAliases = [ diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBPlugin.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBPlugin.swift index 18db273d8c..2a3231403b 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBPlugin.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBPlugin.swift @@ -71,6 +71,13 @@ nonisolated final class MongoDBPlugin: NSObject, TableProPlugin, DriverPlugin { ]), section: .authentication ), + ConnectionField( + id: "mongoAWSSessionToken", + label: "AWS Session Token", + fieldType: .secure, + section: .authentication, + visibleWhen: FieldVisibilityRule(fieldId: "mongoAuthMechanism", values: ["MONGODB-AWS"]) + ), ConnectionField( id: "mongoReplicaSet", label: "Replica Set", diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift index 2c605ed6e7..bec9ddbf0b 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift @@ -71,6 +71,13 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se extraParams[paramName] = value } } + MongoDBConnectionURI.applySecrets( + to: &extraParams, + keyPassphrase: config.ssl.isEnabled && !config.ssl.clientCertificatePath.isEmpty + ? config.additionalFields["sslClientKeyPassphrase"] : nil, + sessionToken: authMechanism?.uppercased() == "MONGODB-AWS" + ? config.additionalFields["mongoAWSSessionToken"] : nil + ) let effectiveHost = config.additionalFields["mongoHosts"].flatMap { hosts in hosts.isEmpty ? nil : hosts diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift b/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift index b7ee9b526c..f701ad9285 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift @@ -160,7 +160,10 @@ final class TeradataPluginDriver: PluginDatabaseDriver, @unchecked Sendable { database: config.database.isEmpty ? nil : config.database, logMech: logMech, transactionMode: transactionMode, - tls: try TeradataSSLMapping.tlsOptions(for: config.ssl), + tls: try TeradataSSLMapping.tlsOptions( + for: config.ssl, + tunneled: config.additionalFields["preTunnelHost"]?.isEmpty == false + ), connectTimeoutMilliseconds: TeradataConnectTimeout( additionalFields: config.additionalFields ).milliseconds) diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataSSLMapping.swift b/apple/Plugins/TeradataDriverPlugin/TeradataSSLMapping.swift index 7e657b8f77..4c75853210 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataSSLMapping.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataSSLMapping.swift @@ -39,28 +39,36 @@ struct TeradataConnectTimeout: Equatable, Sendable { internal enum TeradataSSLConfigurationError: Error, PluginDriverError { case verifyCaNeedsCertificate + case tlsBehindTunnel var pluginErrorMessage: String { switch self { case .verifyCaNeedsCertificate: return String(localized: "Verify CA needs a CA certificate. Choose the CA certificate that signed the server's certificate.") + case .tlsBehindTunnel: + return String(localized: "Teradata uses TLS on its HTTPS port, which the tunnel does not forward. Set SSL Mode to Disabled or Preferred to connect through the tunnel, or turn the tunnel off.") } } } enum TeradataSSLMapping { - static func tlsOptions(for ssl: SSLConfiguration) throws -> TeradataTLSOptions { + static func tlsOptions(for ssl: SSLConfiguration, tunneled: Bool) throws -> TeradataTLSOptions { guard ssl.isEnabled else { return .disabled } if ssl.mode == .verifyCa, ssl.caCertificatePath.trimmingCharacters(in: .whitespaces).isEmpty { throw TeradataSSLConfigurationError.verifyCaNeedsCertificate } - return TeradataTLSOptions( + let options = TeradataTLSOptions( enabled: true, allowPlaintextFallback: ssl.mode == .preferred, verifiesCertificate: ssl.verifiesCertificate, verifiesHostname: ssl.verifiesHostname, caCertificatePath: ssl.caCertificatePath, modeLabel: modeLabel(for: ssl.mode)) + guard tunneled else { return options } + guard let throughTunnel = options.throughTunnel() else { + throw TeradataSSLConfigurationError.tlsBehindTunnel + } + return throughTunnel } private static func modeLabel(for mode: SSLMode) -> String { diff --git a/apple/TablePro/AppDelegate.swift b/apple/TablePro/AppDelegate.swift index b44751e201..d87a339acd 100644 --- a/apple/TablePro/AppDelegate.swift +++ b/apple/TablePro/AppDelegate.swift @@ -139,6 +139,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { guard !AppStorageEnvironment.shared.isIsolated else { return } ConnectionStorage.shared.migratePluginSecureFieldsIfNeeded() + ConnectionStorage.shared.moveMongoURISecretsToKeychain() FavoriteTablesStorage.shared.migrateSyncIdentityIfNeeded() SoftwareUpdater.shared.start() AnalyticsService.shared.startPeriodicHeartbeat() diff --git a/apple/TablePro/Core/Database/ConnectionCredentialResolver.swift b/apple/TablePro/Core/Database/ConnectionCredentialResolver.swift index f0fd6f967f..74e46c8b23 100644 --- a/apple/TablePro/Core/Database/ConnectionCredentialResolver.swift +++ b/apple/TablePro/Core/Database/ConnectionCredentialResolver.swift @@ -72,7 +72,7 @@ enum ConnectionCredentialResolver { } return try await resolveSource(passwordSource, for: connection) } - if connection.usePgpass { + if connection.usePgpass, readsPgpass(connection) { return ResolvedSignIn(username: nil, password: pgpassPassword(for: connection, username: resolveUsername(for: connection))) } return ResolvedSignIn(username: nil, password: ConnectionStorage.shared.loadPassword(for: connection.id) ?? "") @@ -111,8 +111,16 @@ enum ConnectionCredentialResolver { guard case .profile(let profileId) = connection.credentialMode, let profile = CredentialProfileStorage.shared.profile(for: profileId) else { return connection.promptForPassword } - if case .prompt = profile.passwordMode { return true } - return false + switch profile.passwordMode { + case .prompt: return true + case .pgpass: return !readsPgpass(connection) + case .stored, .source: return false + } + } + + // ~/.pgpass is libpq's file. Another engine can dial a host list the lookup never saw, so it asks instead. + static func readsPgpass(_ connection: DatabaseConnection) -> Bool { + PluginManager.shared.additionalConnectionFields(for: connection.type).contains { $0.id == "usePgpass" } } private static func resolveProfileSignIn( @@ -126,6 +134,7 @@ enum ConnectionCredentialResolver { // The prompt's answer arrives as `override`; reaching here means it was declined or the connect is unattended. return ResolvedSignIn(username: nil, password: "") case .pgpass: + guard readsPgpass(connection) else { return ResolvedSignIn(username: nil, password: "") } return ResolvedSignIn(username: nil, password: pgpassPassword(for: connection, username: profile.username)) case .source(let source): // A source can name a shell command, or a Vault address to send a token to. diff --git a/apple/TablePro/Core/Database/MongoToolsConnectionString.swift b/apple/TablePro/Core/Database/MongoToolsConnectionString.swift index e30ddcac6b..aa2905bcbd 100644 --- a/apple/TablePro/Core/Database/MongoToolsConnectionString.swift +++ b/apple/TablePro/Core/Database/MongoToolsConnectionString.swift @@ -4,6 +4,7 @@ // import Foundation +import TableProConnectionLibrary import TableProPluginKit internal enum MongoToolsConnectionString { @@ -118,7 +119,25 @@ internal enum MongoToolsConnectionString { by: connection, besides: setKeys, tls: setKeys.contains("tls"), skipsVerification: setKeys.contains("tlsInsecure") ) - return parameters + typed.map { "\(encodedValue($0.key))=\(encodedValue($0.value))" } + var options = typed + MongoToolsConnectionString.applyKeychainSecrets(of: connection, to: &options) + return parameters + options.map { "\(encodedValue($0.key))=\(encodedValue($0.value))" } + } + + // The dump service puts the Keychain's passphrase and session token in the fields, as the driver gets them; the + // string goes only into the tools' private configuration file. + private static func applyKeychainSecrets(of connection: DatabaseConnection, to options: inout [(key: String, value: String)]) { + let ssl = connection.sslConfig + if ssl.isEnabled, !ssl.clientCertificatePath.isEmpty, + let passphrase = connection.additionalFields["sslClientKeyPassphrase"], !passphrase.isEmpty { + options.append((key: "tlsCertificateKeyFilePassword", value: passphrase)) + } + guard connection.mongoAuthMechanism?.uppercased() == "MONGODB-AWS", + let token = connection.additionalFields[ConnectionSecretOptions.mongoSessionTokenFieldId], !token.isEmpty + else { return } + let current = options.first { $0.key.lowercased() == "authmechanismproperties" }?.value + options.removeAll { $0.key.lowercased() == "authmechanismproperties" } + options.append((key: "authMechanismProperties", value: ConnectionSecretOptions.joiningSessionToken(token, to: current))) } private static func encodedHost(_ host: String) -> String { diff --git a/apple/TablePro/Core/Database/MongoTypedParameters.swift b/apple/TablePro/Core/Database/MongoTypedParameters.swift index 33b26f4221..f278ed10a3 100644 --- a/apple/TablePro/Core/Database/MongoTypedParameters.swift +++ b/apple/TablePro/Core/Database/MongoTypedParameters.swift @@ -4,6 +4,7 @@ // import Foundation +import TableProConnectionLibrary // The parameters typed into a MongoDB connection (its `mongoParam_` fields) that reach the driver. The connection's own // fields own their options, so a parameter named like one, in any case or under an old `ssl` name, never overrides it, @@ -47,7 +48,9 @@ internal enum MongoTypedParameters { } return connection.additionalFields .compactMap { key, value -> (key: String, value: String)? in - guard key.hasPrefix("mongoParam_"), !value.isEmpty else { return nil } + // A credential is never a typed option; the backup tools get the Keychain's copy separately. + guard key.hasPrefix("mongoParam_"), !value.isEmpty, + !ConnectionSecretOptions.isSecret(key: key, value: value) else { return nil } let name = String(key.dropFirst("mongoParam_".count)) return name.isEmpty ? nil : (name, value) } diff --git a/apple/TablePro/Core/Database/NativeDumpService.swift b/apple/TablePro/Core/Database/NativeDumpService.swift index f7f58ebbf9..5885fee28a 100644 --- a/apple/TablePro/Core/Database/NativeDumpService.swift +++ b/apple/TablePro/Core/Database/NativeDumpService.swift @@ -11,6 +11,7 @@ import Combine import Foundation import os +import TableProConnectionLibrary import TableProPluginKit // MARK: - Public Types @@ -302,13 +303,20 @@ final class NativeDumpService: ObservableObject { } var effective = session?.effectiveConnection ?? connection + let resolvedFields = DatabaseDriverFactory.resolvedAdditionalFields(for: effective) ?? effective.additionalFields // The connect's own resolution, errors included: a Keychain read misses every other source. let signIn = try await ConnectionCredentialResolver.resolveSignIn( for: effective, - fields: DatabaseDriverFactory.resolvedAdditionalFields(for: effective) - ?? effective.additionalFields, + fields: resolvedFields, override: session?.cachedPassword ) + // A tool signs in with the Keychain secrets the driver gets beside the password. + if let token = resolvedFields[ConnectionSecretOptions.mongoSessionTokenFieldId], !token.isEmpty { + effective.additionalFields[ConnectionSecretOptions.mongoSessionTokenFieldId] = token + } + if let passphrase = ConnectionStorage.shared.loadSSLClientKeyPassphrase(for: effective.id), !passphrase.isEmpty { + effective.additionalFields["sslClientKeyPassphrase"] = passphrase + } if let username = signIn.username { effective.username = username } diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift index 383f3e0822..1df518f949 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift @@ -71,6 +71,7 @@ extension PluginMetadataRegistry { supportsAddIndex: false, supportsDropIndex: false, supportsOpportunisticTLS: false, + supportsClientKeyPassphrase: true, authenticationIsDatabaseScoped: true, columnsAreSampled: true, patternFiltersReadAnyColumn: true, @@ -106,6 +107,13 @@ extension PluginMetadataRegistry { ConnectionField( id: "mongoAuthSource", label: "Auth Database", placeholder: "admin" ), + ConnectionField( + id: "mongoAWSSessionToken", + label: "AWS Session Token", + fieldType: .secure, + section: .authentication, + visibleWhen: FieldVisibilityRule(fieldId: "mongoAuthMechanism", values: ["MONGODB-AWS"]) + ), ConnectionField( id: "mongoReadPreference", label: "Read Preference", diff --git a/apple/TablePro/Core/Services/ProjectImport/DotenvParser.swift b/apple/TablePro/Core/Services/ProjectImport/DotenvParser.swift index 5d937ad76e..c2195004ef 100644 --- a/apple/TablePro/Core/Services/ProjectImport/DotenvParser.swift +++ b/apple/TablePro/Core/Services/ProjectImport/DotenvParser.swift @@ -62,7 +62,7 @@ enum DotenvParser { static func parse( _ contents: String, - processEnvironment: [String: String] = ProcessInfo.processInfo.environment + processEnvironment: [String: String] ) -> DotenvDocument { var document = DotenvDocument() let characters = Array(normalize(contents)) diff --git a/apple/TablePro/Core/Services/ProjectImport/ProjectFolderScanner.swift b/apple/TablePro/Core/Services/ProjectImport/ProjectFolderScanner.swift index 2b363a9dbf..854e21ec4d 100644 --- a/apple/TablePro/Core/Services/ProjectImport/ProjectFolderScanner.swift +++ b/apple/TablePro/Core/Services/ProjectImport/ProjectFolderScanner.swift @@ -91,12 +91,20 @@ enum ProjectFolderScanner { guard let contents = ProjectFileReader.text(at: file.url) else { return [] } - return SpringPropertiesExtractor.extract(contents: contents, relativePath: file.relativePath) + return SpringPropertiesExtractor.extract( + contents: contents, + relativePath: file.relativePath, + processEnvironment: [:] + ) case .springYaml: guard let contents = ProjectFileReader.text(at: file.url) else { return [] } - return SpringYamlExtractor.extract(contents: contents, relativePath: file.relativePath) + return SpringYamlExtractor.extract( + contents: contents, + relativePath: file.relativePath, + processEnvironment: [:] + ) case .appSettingsJson: guard let data = ProjectFileReader.data(at: file.url) else { return [] diff --git a/apple/TablePro/Core/Services/ProjectImport/ProjectScanEnvironmentIndex.swift b/apple/TablePro/Core/Services/ProjectImport/ProjectScanEnvironmentIndex.swift index b67bfe9801..ee42e7bd7f 100644 --- a/apple/TablePro/Core/Services/ProjectImport/ProjectScanEnvironmentIndex.swift +++ b/apple/TablePro/Core/Services/ProjectImport/ProjectScanEnvironmentIndex.swift @@ -29,7 +29,8 @@ struct ProjectScanEnvironmentIndex { guard let contents = ProjectFileReader.text(at: file.url) else { continue } - let document = DotenvParser.parse(contents) + // The folder may be someone else's, so a reference it does not define stays unresolved. + let document = DotenvParser.parse(contents, processEnvironment: [:]) let filePath = file.url.standardizedFileURL.path let directory = file.url.deletingLastPathComponent().standardizedFileURL.path documentsByFile[filePath] = document diff --git a/apple/TablePro/Core/Services/ProjectImport/SpringPropertiesExtractor.swift b/apple/TablePro/Core/Services/ProjectImport/SpringPropertiesExtractor.swift index 82d01e97d0..7d3af47503 100644 --- a/apple/TablePro/Core/Services/ProjectImport/SpringPropertiesExtractor.swift +++ b/apple/TablePro/Core/Services/ProjectImport/SpringPropertiesExtractor.swift @@ -9,7 +9,7 @@ enum SpringPropertiesExtractor { static func extract( contents: String, relativePath: String, - processEnvironment: [String: String] = ProcessInfo.processInfo.environment + processEnvironment: [String: String] ) -> [ScannedConnectionCandidate] { let properties = parse(contents) guard let rawURL = properties["spring.datasource.url"] else { diff --git a/apple/TablePro/Core/Services/ProjectImport/SpringYamlExtractor.swift b/apple/TablePro/Core/Services/ProjectImport/SpringYamlExtractor.swift index 1580833c08..13d385f1a1 100644 --- a/apple/TablePro/Core/Services/ProjectImport/SpringYamlExtractor.swift +++ b/apple/TablePro/Core/Services/ProjectImport/SpringYamlExtractor.swift @@ -9,7 +9,7 @@ enum SpringYamlExtractor { static func extract( contents: String, relativePath: String, - processEnvironment: [String: String] = ProcessInfo.processInfo.environment + processEnvironment: [String: String] ) -> [ScannedConnectionCandidate] { for document in YamlMappingSupport.loadDocuments(contents) { guard let rawURL = value(in: document, path: ["spring", "datasource", "url"]) else { diff --git a/apple/TablePro/Core/Storage/ConnectionStorage.swift b/apple/TablePro/Core/Storage/ConnectionStorage.swift index fb16b93a61..f7ebd2805b 100644 --- a/apple/TablePro/Core/Storage/ConnectionStorage.swift +++ b/apple/TablePro/Core/Storage/ConnectionStorage.swift @@ -663,9 +663,10 @@ final class ConnectionStorage { // MARK: - SSL Client Key Passphrase Storage - func saveSSLClientKeyPassphrase(_ passphrase: String, for connectionId: UUID) { + @discardableResult + func saveSSLClientKeyPassphrase(_ passphrase: String, for connectionId: UUID) -> Bool { let key = "com.TablePro.sslkeypassphrase.\(connectionId.uuidString)" - keychain.writeString(passphrase, forKey: key) + return keychain.writeString(passphrase, forKey: key) } func loadSSLClientKeyPassphrase(for connectionId: UUID) -> String? { @@ -864,4 +865,60 @@ final class ConnectionStorage { } } } + + // MARK: - MongoDB Connection-String Secrets + + // Import from URL once kept these as plain options, which exports, links, the team library and sync carry. Runs + // every launch, so a row an older build wrote is moved too. + func moveMongoURISecretsToKeychain() { + let connections = loadConnections() + guard !lastLoadFailed else { return } + var cleaned: [UUID: [String: String]] = [:] + for connection in connections { + let kept = keepingMongoURISecretsInKeychain(connection) + if kept.additionalFields != connection.additionalFields { + cleaned[connection.id] = kept.additionalFields + } + } + guard !cleaned.isEmpty else { return } + let moved = mutateConnections(ids: Set(cleaned.keys), byUser: false) { connection in + connection.additionalFields = cleaned[connection.id] ?? connection.additionalFields + } + if !moved { + Self.logger.error("Could not remove MongoDB connection-string secrets from connections.json; retrying next launch") + } + } + + // The Keychain write comes first: a field whose write fails stays in the record rather than being lost. A value + // already in the Keychain wins, since a legacy record an older build syncs can carry a stale copy. + func keepingMongoURISecretsInKeychain(_ connection: DatabaseConnection) -> DatabaseConnection { + var kept = connection + let id = connection.id + let tokenId = ConnectionSecretOptions.mongoSessionTokenFieldId + let holdsToken = { [self] (token: String) in + !(loadPluginSecureField(fieldId: tokenId, for: id) ?? "").isEmpty + || savePluginSecureField(token, fieldId: tokenId, for: id) + } + for (key, value) in connection.additionalFields where ConnectionSecretOptions.isSecret(key: key, value: value) { + guard let option = ConnectionSecretOptions.mongoOption(in: key) else { + if holdsToken(value) { + kept.additionalFields[key] = nil + } + continue + } + if ConnectionSecretOptions.isMongoKeyPassphrase(option: option) { + if !(loadSSLClientKeyPassphrase(for: id) ?? "").isEmpty || saveSSLClientKeyPassphrase(value, for: id) { + kept.additionalFields[key] = nil + } + } else if ConnectionSecretOptions.isMongoMechanismProperties(option: option) { + let split = ConnectionSecretOptions.splitSessionToken(from: value) + if let token = split.sessionToken, holdsToken(token) { + kept.additionalFields[key] = split.remaining + } + } else { + kept.additionalFields[key] = nil + } + } + return kept + } } diff --git a/apple/TablePro/Core/Storage/CredentialProfileStorage.swift b/apple/TablePro/Core/Storage/CredentialProfileStorage.swift index eb84862c22..024a385a14 100644 --- a/apple/TablePro/Core/Storage/CredentialProfileStorage.swift +++ b/apple/TablePro/Core/Storage/CredentialProfileStorage.swift @@ -134,8 +134,8 @@ final class CredentialProfileStorage { let added = CredentialProfile( name: profile.name, username: profile.username, - // A profile never arrives with a password, so `stored` would sign in with nothing. - passwordMode: profile.passwordMode == .pgpass ? .pgpass : .prompt, + // A file never chooses how this Mac finds a password: pgpass would read ~/.pgpass for its host. + passwordMode: .prompt, secureFieldIds: profile.secureFieldIds, sortOrder: nextSortOrder ) diff --git a/apple/TablePro/Core/Sync/SyncCoordinator.swift b/apple/TablePro/Core/Sync/SyncCoordinator.swift index cc99975f9c..db506285de 100644 --- a/apple/TablePro/Core/Sync/SyncCoordinator.swift +++ b/apple/TablePro/Core/Sync/SyncCoordinator.swift @@ -25,6 +25,7 @@ final class SyncCoordinator: ObservableObject { /// Created on the first run, so no CloudKit container exists while sync stays off. private lazy var transport: any SyncTransport = makeTransport() let changeTracker: SyncChangeTracker + private var connectionsCleanedOnPull: Set = [] let metadataStorage: SyncMetadataStorage let recordCache: SyncRecordCache let columnLayouts: () -> FileColumnLayoutPersister @@ -818,6 +819,9 @@ final class SyncCoordinator: ObservableObject { ) changeTracker.isSuppressed = false + // Pushed again without the secret it carried; the tracker ignores marks while suppressed. + changeTracker.markDirty(.connection, ids: Array(connectionsCleanedOnPull)) + connectionsCleanedOnPull.removeAll() changeTracker.markDeleted(.tableFavorite, idsByOwner: effects.tableFavoriteIdsToRetire) return !effects.persistenceFailed } @@ -977,9 +981,10 @@ final class SyncCoordinator: ObservableObject { } var connections = services.connectionStorage.loadConnections() - if let index = connections.firstIndex(where: { $0.id == remoteConnection.id }) { + var incoming = remoteConnection + let index = connections.firstIndex(where: { $0.id == remoteConnection.id }) + if let index { guard !connections[index].localOnly else { return .skipped } - var incoming = remoteConnection if changeTracker.dirtyRecords(for: .connection).contains(remoteConnection.id.uuidString) { guard let reconciled = mergeLocalEdits( into: record, @@ -989,14 +994,22 @@ final class SyncCoordinator: ObservableObject { } incoming = reconciled } - connections[index] = incoming.adoptingDeviceLocalState(from: connections[index]) + } + // A record an older build pushed can still carry a connection string's secret in plain text. + let kept = services.connectionStorage.keepingMongoURISecretsInKeychain(incoming) + let movedSecrets = kept.additionalFields != incoming.additionalFields + if let index { + connections[index] = kept.adoptingDeviceLocalState(from: connections[index]) } else { - connections.append(remoteConnection) + connections.append(kept) } guard services.connectionStorage.saveConnections(connections, byUser: false) else { Self.logger.error("Failed to apply remote connection update: persistence error for \(remoteConnection.id, privacy: .public)") return .failed } + if movedSecrets { + connectionsCleanedOnPull.insert(remoteConnection.id.uuidString) + } return .applied } diff --git a/apple/TablePro/Core/Sync/SyncRecordMapper.swift b/apple/TablePro/Core/Sync/SyncRecordMapper.swift index 4d26ed9ae3..ec07c3eec3 100644 --- a/apple/TablePro/Core/Sync/SyncRecordMapper.swift +++ b/apple/TablePro/Core/Sync/SyncRecordMapper.swift @@ -323,9 +323,21 @@ struct SyncRecordMapper { let baseAsWritten = base.flatMap { try? toConnection($0) }.map { toCKRecord($0, in: zone) } return SyncRecordEdit(ConnectionSyncField.self, record: record, base: base, baseAsWritten: baseAsWritten) { merged, server in settleRoute(in: merged, server: server, publisher: publisher) + removeSecretOptions(from: merged) } } + // A rebase keeps the server's additional fields when this edit left them alone, and an older build may have + // written a connection string's secret into them. + static func removeSecretOptions(from record: CKRecord) { + let fields = record.fields(ConnectionSyncField.self) + guard let data = fields[.additionalFieldsJson] as? Data, + let stored = try? decoder.decode([String: String].self, from: data) else { return } + let kept = ConnectionSecretOptions.removingSecrets(from: stored) + guard kept != stored else { return } + fields[.additionalFieldsJson] = kept.isEmpty ? nil : try? encoder.encode(kept) + } + // An edit judged its route against an older copy, so the route lands on the server's by the rules a dial uses. static func settleRoute(in merged: CKRecord, server: CKRecord, publisher: UUID) { let mergedFields = merged.fields(ConnectionSyncField.self) @@ -352,7 +364,7 @@ struct SyncRecordMapper { } private static func syncedAdditionalFields(for connection: DatabaseConnection) -> [String: String] { - var fields = connection.additionalFields + var fields = ConnectionSecretOptions.removingSecrets(from: connection.additionalFields) fields.removeValue(forKey: DatabaseConnection.connectTimeoutSecondsKey) fields.removeValue(forKey: DatabaseConnection.queryTimeoutSecondsKey) if let connectTimeoutSeconds = validConnectTimeout(connection.connectTimeoutSeconds) { diff --git a/apple/TablePro/Core/Utilities/Connection/TransientConnectionFactory.swift b/apple/TablePro/Core/Utilities/Connection/TransientConnectionFactory.swift index e9d4da2bfe..9f173c2657 100644 --- a/apple/TablePro/Core/Utilities/Connection/TransientConnectionFactory.swift +++ b/apple/TablePro/Core/Utilities/Connection/TransientConnectionFactory.swift @@ -4,6 +4,7 @@ // import Foundation +import TableProConnectionLibrary import TableProPluginKit import TableProSSHConfig @@ -64,13 +65,23 @@ internal enum TransientConnectionFactory { connection.additionalFields.merge(parsed.additionalFields) { _, imported in imported } - // The same field mapping as Import from URL, so an opened link reads legacy UUIDs the way its sender set. + // The same field mapping as Import from URL, so an opened link reads legacy UUIDs the way its sender set and + // its secrets sit where the driver and the backup tools read them. The connection is never saved. for (key, value) in parsed.mongoQueryParams where !value.isEmpty { - switch key { - case "authMechanism", "replicaSet": + switch key.lowercased() { + case "authmechanism", "replicaset", "proxypassword": continue - case "uuidRepresentation": + case "uuidrepresentation": connection.additionalFields["mongoUuidRepresentation"] = value + case "tlscertificatekeyfile", "sslclientcertificatekeyfile": + connection.sslConfig.clientCertificatePath = value + connection.sslConfig.clientKeyPath = value + case "tlscertificatekeyfilepassword", "sslclientcertificatekeypassword": + connection.additionalFields["sslClientKeyPassphrase"] = value + case "authmechanismproperties": + let split = ConnectionSecretOptions.splitSessionToken(from: value) + connection.additionalFields["mongoParam_\(key)"] = split.remaining + connection.additionalFields[ConnectionSecretOptions.mongoSessionTokenFieldId] = split.sessionToken default: connection.additionalFields["mongoParam_\(key)"] = value } diff --git a/apple/TablePro/Core/Utilities/UI/AlertHelper.swift b/apple/TablePro/Core/Utilities/UI/AlertHelper.swift index 9fe36ae79a..1c6f1c04de 100644 --- a/apple/TablePro/Core/Utilities/UI/AlertHelper.swift +++ b/apple/TablePro/Core/Utilities/UI/AlertHelper.swift @@ -383,9 +383,12 @@ final class AlertHelper { // MARK: - Three-Way Confirmations + // `detail` is the text the answer is given against, so it goes whole into a scrolling box that reveals invisible + // characters; `informativeText` does neither. An overlay scroller would hide that more is below. static func confirmThreeWay( title: String, message: String, + detail: String? = nil, first: String, second: String, third: String, @@ -394,6 +397,12 @@ final class AlertHelper { let alert = NSAlert() alert.messageText = title alert.informativeText = message + if let detail { + let box = scrollingTextAccessory(RevealedText(detail).plainText) + box.scrollerStyle = .legacy + box.autohidesScrollers = true + alert.accessoryView = box + } alert.alertStyle = .warning alert.addButton(withTitle: first) alert.addButton(withTitle: second) diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 9a79216826..2653c21b56 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -105243,6 +105243,9 @@ }, "Longest" : { + }, + "Looked up in ~/.pgpass by the host, port, database and username of the PostgreSQL-family connection that is opening. Other connections ask for the password." : { + }, "Looked up in ~/.pgpass by the host, port, database and username of whichever connection is opening." : { "localizations" : { @@ -181399,6 +181402,10 @@ }, "Temporary Table" : { + }, + "Teradata uses TLS on its HTTPS port, which the tunnel does not forward. Set SSL Mode to Disabled or Preferred to connect through the tunnel, or turn the tunnel off." : { + "extractionState" : "manual", + "generatesSymbol" : false }, "Teradata Vantage data warehouse" : { "localizations" : { @@ -193642,6 +193649,9 @@ } } } + }, + "These connections run commands every time they connect: a tunnel command on this Mac, SQL on the server, or both." : { + }, "These connections run commands every time they connect: a tunnel command on this Mac, SQL on the server, or both.\n\n%@" : { @@ -194245,6 +194255,10 @@ } } }, + "This connection asks for TLS, which the Dameng driver does not support. Add the connection again without an SSL mode, and use an SSH, SOCKS or Cloudflare tunnel on a network you do not trust." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "This connection called a stored routine, so TablePro cannot tell what the session is holding." : { "extractionState" : "manual", "generatesSymbol" : false, diff --git a/apple/TablePro/Views/Connection/Import/ImportReviewStep.swift b/apple/TablePro/Views/Connection/Import/ImportReviewStep.swift index 606074c7b7..29faf35cfd 100644 --- a/apple/TablePro/Views/Connection/Import/ImportReviewStep.swift +++ b/apple/TablePro/Views/Connection/Import/ImportReviewStep.swift @@ -139,7 +139,11 @@ internal struct ImportReviewStep: View { if !commanded.isEmpty { let choice = await AlertHelper.confirmThreeWay( title: String(localized: "Import Commands?"), - message: Self.commandConfirmation(for: commanded), + message: String(localized: """ + These connections run commands every time they connect: a tunnel command on this Mac, \ + SQL on the server, or both. + """), + detail: Self.commandConfirmation(for: commanded), first: String(localized: "Import Without Commands"), second: String(localized: "Import Commands"), third: String(localized: "Cancel"), @@ -177,10 +181,9 @@ internal struct ImportReviewStep: View { return banners } - /// Shows each command in full: it runs on this Mac every time the connection opens, so the - /// answer has to be given against the text, not against the fact that one exists. + // Every command whole: "Import Commands" keeps exactly this text, so the answer is given against all of it. static func commandConfirmation(for rows: [ConnectionRow]) -> String { - let entries = rows.map { row -> String in + rows.map { row -> String in let settings = row.settings var lines = [settings.name] if let tunnel = settings.tunnelCommand @@ -195,25 +198,14 @@ internal struct ImportReviewStep: View { lines.append(tunnel) } if row.carriesStartupCommands, let startup = settings.startupCommands { - lines.append(String(format: String(localized: "Startup SQL: %@"), Self.excerpt(startup))) + lines.append(String( + format: String(localized: "Startup SQL: %@"), + startup.trimmingCharacters(in: .whitespacesAndNewlines) + )) } return lines.joined(separator: "\n") } - return String( - format: String(localized: """ - These connections run commands every time they connect: a tunnel command on this Mac, \ - SQL on the server, or both. - - %@ - """), - entries.joined(separator: "\n\n") - ) - } - - private static func excerpt(_ sql: String) -> String { - let trimmed = sql.trimmingCharacters(in: .whitespacesAndNewlines) as NSString - let limit = 300 - return trimmed.length > limit ? trimmed.substring(to: limit) + "…" : trimmed as String + .joined(separator: "\n\n") } } diff --git a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift index 16db619b2e..0972a6182e 100644 --- a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift +++ b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift @@ -441,6 +441,11 @@ final class ConnectionFormCoordinator: ObservableObject { } edits.additionalFields.removeValue(forKey: field.id) } + // An installed plugin older than the field still must not leave the token in the record. + let sessionTokenId = ConnectionSecretOptions.mongoSessionTokenFieldId + if let token = edits.additionalFields.removeValue(forKey: sessionTokenId), !token.isEmpty { + storage.savePluginSecureField(token, fieldId: sessionTokenId, for: finalId) + } var connectionToSave = edits.applied(to: baseConnection(id: finalId)) @@ -995,13 +1000,30 @@ final class ConnectionFormCoordinator: ObservableObject { writeFieldByRegistry("mongoUseSrv", value: "true") } for (key, value) in parsed.mongoQueryParams where !value.isEmpty { - switch key { - case "authMechanism": + switch key.lowercased() { + case "authmechanism": writeFieldByRegistry("mongoAuthMechanism", value: value) - case "replicaSet": + case "replicaset": writeFieldByRegistry("mongoReplicaSet", value: value) - case "uuidRepresentation": + case "uuidrepresentation": writeFieldByRegistry("mongoUuidRepresentation", value: value) + // Secrets go to the Keychain through the SSL pane and a secure field, never into a plain option. + case "tlscertificatekeyfile", "sslclientcertificatekeyfile": + ssl.clientCertPath = value + ssl.clientKeyPath = value + case "tlscertificatekeyfilepassword", "sslclientcertificatekeypassword": + ssl.clientKeyPassphrase = value + case "authmechanismproperties": + let split = ConnectionSecretOptions.splitSessionToken(from: value) + if let remaining = split.remaining { + writeFieldByRegistry("mongoParam_\(key)", value: remaining) + } + if let token = split.sessionToken { + writeFieldByRegistry(ConnectionSecretOptions.mongoSessionTokenFieldId, value: token) + } + case "proxypassword": + // libmongoc has no proxy options, so the password would only sit in the record. + continue default: writeFieldByRegistry("mongoParam_\(key)", value: value) } diff --git a/apple/TablePro/Views/Settings/Profiles/CredentialProfileEditorSheet.swift b/apple/TablePro/Views/Settings/Profiles/CredentialProfileEditorSheet.swift index 4905e7185a..2dc3c51e75 100644 --- a/apple/TablePro/Views/Settings/Profiles/CredentialProfileEditorSheet.swift +++ b/apple/TablePro/Views/Settings/Profiles/CredentialProfileEditorSheet.swift @@ -141,7 +141,7 @@ struct CredentialProfileEditorSheet: View { .font(.caption) .foregroundStyle(.secondary) case .pgpass: - Text("Looked up in ~/.pgpass by the host, port, database and username of whichever connection is opening.") + Text("Looked up in ~/.pgpass by the host, port, database and username of the PostgreSQL-family connection that is opening. Other connections ask for the password.") .font(.caption) .foregroundStyle(.secondary) case .source: diff --git a/apple/TableProMobile/TableProMobile/AppState.swift b/apple/TableProMobile/TableProMobile/AppState.swift index 20885d1109..25ad474807 100644 --- a/apple/TableProMobile/TableProMobile/AppState.swift +++ b/apple/TableProMobile/TableProMobile/AppState.swift @@ -83,7 +83,7 @@ final class AppState { groupStorage = GroupPersistence(directory: libraryDirectory) tagStorage = TagPersistence(directory: libraryDirectory) let driverFactory: any DriverFactory = injectedDriverFactory - ?? IOSDriverFactory(bookmarkStore: bookmarkStore, localFiles: localDatabaseFiles) + ?? IOSDriverFactory(bookmarkStore: bookmarkStore, localFiles: localDatabaseFiles, secureStore: secureStore) self.secureStore = secureStore let sshProvider = IOSSSHProvider(secureStore: secureStore, container: localDatabaseFiles.container) self.sshProvider = sshProvider @@ -217,8 +217,9 @@ final class AppState { } @discardableResult - func applySyncedConnections(_ merged: [DatabaseConnection]) -> Bool { + func applySyncedConnections(_ pulled: [DatabaseConnection]) -> Bool { guard !refuseWriteIfNotReady() else { return false } + let merged = pulled.map { MongoURISecretMigration.keepingSecretsInKeychain($0, in: secureStore) } guard merged != connections || unsavedLibraryParts.contains(.connections) else { return true } let removedIds = Set(connections.map(\.id)).subtracting(merged.map(\.id)) guard persist(connections: merged, onlyIfSaved: true) else { return false } @@ -237,6 +238,7 @@ final class AppState { let stored = try storage.load() connectionsState = .loaded(stored.connections) movePastedSSHKeysToKeychain(from: stored) + moveMongoURISecretsToKeychain(from: stored.connections) } catch { connectionsState = .failed(error) Self.logger.error("Connections load failed: \(error.localizedDescription, privacy: .public)") @@ -257,6 +259,17 @@ final class AppState { } } + private func moveMongoURISecretsToKeychain(from loaded: [DatabaseConnection]) { + let cleaned = loaded.map { MongoURISecretMigration.keepingSecretsInKeychain($0, in: secureStore) } + guard cleaned != loaded else { return } + connectionsState = .loaded(cleaned) + do { + try storage.save(cleaned, keepingPastedSSHKeys: pastedSSHKeysAwaitingKeychain) + } catch { + Self.logger.error("Rewriting connections without MongoDB secrets failed: \(error.localizedDescription, privacy: .public)") + } + } + private func movePastedSSHKeysToKeychain(from stored: StoredConnections) { guard !stored.pastedSSHKeys.isEmpty else { return } let unstored = ConnectionSecrets(secureStore: secureStore).storeMissingPrivateKeys(stored.pastedSSHKeys) diff --git a/apple/TableProMobile/TableProMobile/Drivers/DriverSSLConfiguration.swift b/apple/TableProMobile/TableProMobile/Drivers/DriverSSLConfiguration.swift index 882e8357b7..d8a0bed09f 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/DriverSSLConfiguration.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/DriverSSLConfiguration.swift @@ -77,6 +77,14 @@ nonisolated struct DriverSSLConfiguration: Equatable, Sendable { } } + var mssqlCertificateVerification: MSSQLCertificateVerification { + switch mode { + case .disable, .require: return .none + case .verifyCa: return .chain + case .verifyFull: return .chainAndHostname + } + } + var mssqlEncryptionLevel: MSSQLEncryptionLevel { switch mode { case .disable: return .request diff --git a/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift index b9ee0b2238..b3322be4c0 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift @@ -33,8 +33,15 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { nonisolated(unsafe) private(set) var currentSchema: String? = "dbo" nonisolated(unsafe) private(set) var serverVersion: String? + // This form collects no CA for FreeTDS, so a verify mode from the Mac or an import cannot be honoured here. + static let verifyModeUnavailableMessage = String(localized: """ + SQL Server on this device cannot check the server's certificate. Set SSL Mode to Required in the \ + connection's settings, or connect from the Mac app. + """) + init(connection: DatabaseConnection, password: String?) { let authMethod = MSSQLConnectionOptions.authMethod(from: connection.additionalFields) + let ssl = DriverSSLConfiguration(sslEnabled: connection.sslEnabled, configuration: connection.sslConfiguration) let options = MSSQLConnectionOptions( host: connection.host, port: connection.port, @@ -42,10 +49,8 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { password: password ?? "", database: connection.database, schema: MSSQLConnectionOptions.schema(from: connection.additionalFields), - encryptionLevel: DriverSSLConfiguration( - sslEnabled: connection.sslEnabled, - configuration: connection.sslConfiguration - ).mssqlEncryptionLevel, + encryptionLevel: ssl.mssqlEncryptionLevel, + certificateVerification: ssl.mssqlCertificateVerification, loginTimeoutSeconds: Int(connection.additionalFields["mssqlLoginTimeout"] ?? "") ?? MSSQLConnectionOptions.defaultLoginTimeoutSeconds, authMethod: authMethod ) @@ -60,6 +65,9 @@ nonisolated final class MSSQLDriver: DatabaseDriver, @unchecked Sendable { // MARK: - Connection func connect() async throws { + guard !options.certificateVerification.needsAuthority else { + throw DatabaseError(message: Self.verifyModeUnavailableMessage) + } guard authMethod != .windows else { throw DatabaseError(message: String(localized: "Windows Authentication (Kerberos) isn't supported on iOS yet. Use SQL Server Authentication, or connect from the Mac app.")) } diff --git a/apple/TableProMobile/TableProMobile/Drivers/MongoDBDialPlan.swift b/apple/TableProMobile/TableProMobile/Drivers/MongoDBDialPlan.swift index 58c11a0458..f552caef12 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MongoDBDialPlan.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MongoDBDialPlan.swift @@ -49,6 +49,26 @@ nonisolated enum MongoDBDialPlan { return fields } + // The Mac keeps a connection string's session token and key passphrase in the Keychain, never in the record. + static let keychainFields: [(kind: ConnectionSecretKind, field: String)] = [ + (.mongoAWSSessionToken, ConnectionSecretOptions.mongoSessionTokenFieldId), + (.sslKeyPassphrase, "sslClientKeyPassphrase"), + ] + + static func addingKeychainSecrets( + to fields: [String: String], + of connection: DatabaseConnection, + in secureStore: any SecureStore + ) -> [String: String] { + var fields = fields + for (kind, field) in keychainFields { + guard let value = try? secureStore.retrieve(forKey: kind.account(for: connection.id)), !value.isEmpty + else { continue } + fields[field] = value + } + return fields + } + // OpenSSL on iOS has no trust store, so a verify mode needs the user's CA. Behind a tunnel the dialed name is the // loopback, so Verify Identity keeps the chain check and drops only the name check. static func ssl(_ ssl: DriverSSLConfiguration, tunneled: Bool) throws -> DriverSSLConfiguration { diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index 6cde5cd5f6..8ec2eb210d 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -21274,6 +21274,9 @@ } } } + }, + "SQL Server on this device cannot check the server's certificate. Set SSL Mode to Required in the connection's settings, or connect from the Mac app." : { + }, "SQL Server returned no plan for this statement." : { "extractionState" : "manual", @@ -22608,6 +22611,9 @@ }, "This cursor has already been closed." : { + }, + "This device cannot check a SQL Server certificate, so it does not connect in this mode. Choose Required to connect without the check." : { + }, "This document has no _id, so it cannot be edited here." : { diff --git a/apple/TableProMobile/TableProMobile/Platform/ConnectionSecretKind.swift b/apple/TableProMobile/TableProMobile/Platform/ConnectionSecretKind.swift index 81207c4204..2915088d8f 100644 --- a/apple/TableProMobile/TableProMobile/Platform/ConnectionSecretKind.swift +++ b/apple/TableProMobile/TableProMobile/Platform/ConnectionSecretKind.swift @@ -10,6 +10,8 @@ nonisolated enum ConnectionSecretKind: CaseIterable, Sendable { case awsSecretAccessKey case awsSessionToken case gcpServiceAccountKey + case mongoAWSSessionToken + case sslKeyPassphrase case deviceIAM var prefix: String { @@ -23,6 +25,8 @@ nonisolated enum ConnectionSecretKind: CaseIterable, Sendable { case .awsSecretAccessKey: "com.TablePro.plugin.awsSecretAccessKey." case .awsSessionToken: "com.TablePro.plugin.awsSessionToken." case .gcpServiceAccountKey: "com.TablePro.plugin.gcpServiceAccountKey." + case .mongoAWSSessionToken: "com.TablePro.plugin.mongoAWSSessionToken." + case .sslKeyPassphrase: "com.TablePro.sslkeypassphrase." case .deviceIAM: "com.TablePro.deviceiam." } } @@ -31,7 +35,8 @@ nonisolated enum ConnectionSecretKind: CaseIterable, Sendable { switch self { case .password, .sshPassword, .keyPassphrase, .awsSecretAccessKey, .awsSessionToken, .gcpServiceAccountKey, .deviceIAM: true - case .sshPrivateKey, .jumpHostKeys, .totpSecret: false + // The Mac owns these two, so this device never deletes them. + case .sshPrivateKey, .jumpHostKeys, .totpSecret, .mongoAWSSessionToken, .sslKeyPassphrase: false } } diff --git a/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift b/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift index 0b630f942e..203fff2994 100644 --- a/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift +++ b/apple/TableProMobile/TableProMobile/Platform/IOSDriverFactory.swift @@ -13,17 +13,20 @@ nonisolated final class IOSDriverFactory: DriverFactory { private let materializer: CertificateMaterializer private let localFiles: LocalDatabaseFileLocator private let mint: IAMPasswordMint + private let secureStore: any SecureStore init( bookmarkStore: FileBookmarkStore = FileBookmarkStore(), materializer: CertificateMaterializer = CertificateMaterializer(), localFiles: LocalDatabaseFileLocator = .live, - mint: IAMPasswordMint = IAMPasswordMint() + mint: IAMPasswordMint = IAMPasswordMint(), + secureStore: any SecureStore = KeychainSecureStore() ) { self.bookmarkStore = bookmarkStore self.materializer = materializer self.localFiles = localFiles self.mint = mint + self.secureStore = secureStore } private func sqliteSource(for connection: DatabaseConnection) throws -> LocalDatabaseFileSource { @@ -188,7 +191,11 @@ nonisolated final class IOSDriverFactory: DriverFactory { password: password, ssl: tls, preTunnelHost: preTunnelHost, - fields: try MongoDBDialPlan.additionalFields(of: connection, preTunnelHost: preTunnelHost) + fields: MongoDBDialPlan.addingKeychainSecrets( + to: try MongoDBDialPlan.additionalFields(of: connection, preTunnelHost: preTunnelHost), + of: connection, + in: secureStore + ) ) return MongoDBBackedDriver( mongo: MongoDBPluginDriver(config: config), diff --git a/apple/TableProMobile/TableProMobile/Platform/MongoURISecretMigration.swift b/apple/TableProMobile/TableProMobile/Platform/MongoURISecretMigration.swift new file mode 100644 index 0000000000..349b260f91 --- /dev/null +++ b/apple/TableProMobile/TableProMobile/Platform/MongoURISecretMigration.swift @@ -0,0 +1,58 @@ +import Foundation +import os +import TableProConnectionLibrary +import TableProDatabase +import TableProModels + +// A Mac build before the fix kept a pasted MongoDB connection string's secrets as plain options, and sync brought them +// here. They move to the Keychain under the Mac's names, so the driver still finds them and sync never sends them. +nonisolated enum MongoURISecretMigration { + private static let logger = Logger(subsystem: "com.TablePro", category: "MongoURISecretMigration") + + // A field whose Keychain write fails stays in the record rather than being lost. A value already in the Keychain + // wins, since a legacy record an older build syncs can carry a stale copy. + static func keepingSecretsInKeychain( + _ connection: DatabaseConnection, + in secureStore: any SecureStore + ) -> DatabaseConnection { + var kept = connection + for (key, value) in connection.additionalFields where ConnectionSecretOptions.isSecret(key: key, value: value) { + guard let option = ConnectionSecretOptions.mongoOption(in: key) else { + if store(value, as: .mongoAWSSessionToken, for: connection.id, in: secureStore) { + kept.additionalFields[key] = nil + } + continue + } + if ConnectionSecretOptions.isMongoKeyPassphrase(option: option) { + if store(value, as: .sslKeyPassphrase, for: connection.id, in: secureStore) { + kept.additionalFields[key] = nil + } + } else if ConnectionSecretOptions.isMongoMechanismProperties(option: option) { + let split = ConnectionSecretOptions.splitSessionToken(from: value) + if let token = split.sessionToken, store(token, as: .mongoAWSSessionToken, for: connection.id, in: secureStore) { + kept.additionalFields[key] = split.remaining + } + } else { + kept.additionalFields[key] = nil + } + } + return kept + } + + private static func store( + _ value: String, + as kind: ConnectionSecretKind, + for connectionId: UUID, + in secureStore: any SecureStore + ) -> Bool { + let account = kind.account(for: connectionId) + if let held = try? secureStore.retrieve(forKey: account), !held.isEmpty { return true } + do { + try secureStore.store(value, forKey: account) + return true + } catch { + logger.error("A MongoDB connection-string secret stayed in the record: \(error.localizedDescription, privacy: .private)") + return false + } + } +} diff --git a/apple/TableProMobile/TableProMobile/ViewModels/ConnectionFormViewModel.swift b/apple/TableProMobile/TableProMobile/ViewModels/ConnectionFormViewModel.swift index df2dda2613..aeebba9524 100644 --- a/apple/TableProMobile/TableProMobile/ViewModels/ConnectionFormViewModel.swift +++ b/apple/TableProMobile/TableProMobile/ViewModels/ConnectionFormViewModel.swift @@ -49,6 +49,7 @@ final class ConnectionFormViewModel { var sslEnabled = false var sslMode: SSLModeChoice = .disable var mssqlSSLMode: SSLModeChoice = .disable + @ObservationIgnored private(set) var storedMSSQLVerifyMode: SSLModeChoice? var oracleSSLMode: SSLModeChoice = .disable // Client certificates @@ -131,6 +132,7 @@ final class ConnectionFormViewModel { @ObservationIgnored private var addedNewConnection = false @ObservationIgnored private var loadedStoredCredentials = false @ObservationIgnored private var sshProfileSecrets: [String: String] = [:] + @ObservationIgnored private var driverSecrets: [ConnectionSecretKind: String] = [:] private(set) var baseline: ConnectionFormSnapshot? private(set) var storedSecrets = ConnectionFormSecrets() private let localFiles: LocalDatabaseFileLocator @@ -163,8 +165,9 @@ final class ConnectionFormViewModel { database = conn.database sslEnabled = conn.sslEnabled let storedMode = conn.sslConfiguration.map { SSLModeChoice($0) } ?? (conn.sslEnabled ? .require : .disable) - // The iOS SQL Server driver checks no certificate, so a verify mode runs as Required. - mssqlSSLMode = (storedMode == .verifyCa || storedMode == .verifyFull) ? .require : storedMode + // A verify mode stays as stored: the driver refuses it, where turning it into Required would skip the check. + mssqlSSLMode = storedMode + storedMSSQLVerifyMode = (storedMode == .verifyCa || storedMode == .verifyFull) ? storedMode : nil oracleSSLMode = storedMode sslMode = storedMode oracleConnectionType = OracleConnectionOptions.identifierMode(from: conn.additionalFields) @@ -311,6 +314,14 @@ final class ConnectionFormViewModel { localized: "OracleNIO has no TLS fallback. Preferred connects in plain TCP. Use Required for TCPS to Oracle Autonomous Database." ) + var mssqlSSLModeNote: String? { + guard type == .mssql, mssqlSSLMode == .verifyCa || mssqlSSLMode == .verifyFull else { return nil } + return String(localized: """ + This device cannot check a SQL Server certificate, so it does not connect in this mode. Choose Required \ + to connect without the check. + """) + } + var oracleSSLModeNote: String? { guard type == .oracle else { return nil } switch oracleSSLMode { @@ -437,6 +448,10 @@ final class ConnectionFormViewModel { sshProfileSecrets[account] = value } } + // The driver reads these itself, so a test connect needs them under its own id. + for (kind, _) in MongoDBDialPlan.keychainFields { + driverSecrets[kind] = Self.storedSecret(kind, for: conn.id, in: secureStore) + } let iamSecrets = ConnectionIAMSecrets.stored(for: conn.id, in: secureStore) iam.secrets = iamSecrets storedSecrets.iam = iamSecrets @@ -632,6 +647,9 @@ final class ConnectionFormViewModel { secrets[ConnectionSecretKind.password.account(for: connectionId)] = password } secrets.merge(iamTestSecrets(for: connectionId)) { _, iamSecret in iamSecret } + for (kind, value) in driverSecrets { + secrets[kind.account(for: connectionId)] = value + } guard sshEnabled else { return secrets } if !sshPassword.isEmpty { secrets[ConnectionSecretKind.sshPassword.account(for: connectionId)] = sshPassword @@ -673,7 +691,7 @@ final class ConnectionFormViewModel { let secrets = EphemeralSecureStore(testSecrets(for: tempId)) let manager = ConnectionManager( - driverFactory: IOSDriverFactory(bookmarkStore: bookmarkStore, localFiles: localFiles), + driverFactory: IOSDriverFactory(bookmarkStore: bookmarkStore, localFiles: localFiles, secureStore: secrets), secureStore: secrets, sshProvider: IOSSSHProvider(secureStore: secrets, container: localFiles.container) ) diff --git a/apple/TableProMobile/TableProMobile/Views/ConnectionFormView.swift b/apple/TableProMobile/TableProMobile/Views/ConnectionFormView.swift index 1f9d2ba8a5..9afcfe7240 100644 --- a/apple/TableProMobile/TableProMobile/Views/ConnectionFormView.swift +++ b/apple/TableProMobile/TableProMobile/Views/ConnectionFormView.swift @@ -123,15 +123,21 @@ struct ConnectionFormView: View { Text(String(localized: "Verify Identity")).tag(SSLModeChoice.verifyFull) } } else if viewModel.type == .mssql { - // No verify modes: this form collects no CA file for FreeTDS, so the iOS driver checks no certificate. + // No verify modes to choose: this form collects no CA file for FreeTDS. A synced one shows as it is. Picker(String(localized: "SSL Mode"), selection: $viewModel.mssqlSSLMode) { Text(String(localized: "Disabled")).tag(SSLModeChoice.disable) Text(String(localized: "Preferred")).tag(SSLModeChoice.preferred) Text(String(localized: "Required")).tag(SSLModeChoice.require) + if viewModel.storedMSSQLVerifyMode == .verifyCa { + Text(String(localized: "Verify CA")).tag(SSLModeChoice.verifyCa) + } + if viewModel.storedMSSQLVerifyMode == .verifyFull { + Text(String(localized: "Verify Identity")).tag(SSLModeChoice.verifyFull) + } } } } footer: { - if let note = viewModel.oracleSSLModeNote { + if let note = viewModel.oracleSSLModeNote ?? viewModel.mssqlSSLModeNote { Text(note) } } diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelIAMTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelIAMTests.swift index 185d6dd81b..6ade7b7ca3 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelIAMTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelIAMTests.swift @@ -43,6 +43,29 @@ struct ConnectionFormViewModelIAMTests { return state } + @Test("A MongoDB edit tests with the session token and key passphrase the Mac keeps, under the test's own id") + func mongoKeychainSecretsReachTheTest() async { + let connection = DatabaseConnection( + name: "Docs", + type: .mongodb, + host: "db.example.com", + port: 27_017, + username: "AKIAEXAMPLE", + database: "app", + additionalFields: ["mongoAuthMechanism": "MONGODB-AWS"] + ) + let store = MockSecureStore() + store.seed(ConnectionSecretKind.mongoAWSSessionToken.account(for: connection.id), "FQoGSESSION") + store.seed(ConnectionSecretKind.sslKeyPassphrase.account(for: connection.id), "hunter2") + let form = await loadedForm(connection, store: store) + + let tempId = UUID() + let secrets = form.testSecrets(for: tempId) + + #expect(secrets[ConnectionSecretKind.mongoAWSSessionToken.account(for: tempId)] == "FQoGSESSION") + #expect(secrets[ConnectionSecretKind.sslKeyPassphrase.account(for: tempId)] == "hunter2") + } + @Test("An edit with no change tests with the access key it loaded, under the test's own id") func unchangedEditTestsWithLoadedAccessKey() async throws { let connection = rds(.awsAccessKey, fields: ["awsAccessKeyId": "AKIAEXAMPLE"]) diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelOracleTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelOracleTests.swift index cc78be5ec4..66c20708df 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelOracleTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelOracleTests.swift @@ -83,12 +83,12 @@ struct ConnectionFormViewModelOracleTests { #expect(viewModel.oracleSID == "XE") } - @Test("Oracle SSL mode is kept verbatim rather than coerced like SQL Server") + @Test("A stored verify mode is kept verbatim in the Oracle and SQL Server pickers") func keepsVerifyModes() { let connection = makeOracleConnection(additionalFields: [:], sslMode: .verifyFull) let viewModel = ConnectionFormViewModel(editing: connection) #expect(viewModel.oracleSSLMode == .verifyFull) - #expect(viewModel.mssqlSSLMode == .require) + #expect(viewModel.mssqlSSLMode == .verifyFull) } @Test("Saving writes the same additionalFields keys the Mac app reads") diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelSSLTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelSSLTests.swift index 9e37b1b451..73c4c20761 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelSSLTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionFormViewModelSSLTests.swift @@ -76,13 +76,15 @@ struct ConnectionFormViewModelSSLTests { #expect(rebuilt.sslConfiguration?.clientCertificatePath == "/Users/mac/client.pem") } - @Test("an MSSQL connection still coerces verify modes but keeps its paths") + @Test("an MSSQL connection keeps a synced verify mode, so the driver refuses it rather than skipping the check") func mssqlKeepsPaths() { let viewModel = ConnectionFormViewModel(editing: existingConnection(type: .mssql)) - #expect(viewModel.mssqlSSLMode == .require) + #expect(viewModel.mssqlSSLMode == .verifyFull) + #expect(viewModel.storedMSSQLVerifyMode == .verifyFull) + #expect(viewModel.mssqlSSLModeNote != nil) let rebuilt = viewModel.buildConnection() - #expect(rebuilt.sslConfiguration?.mode == .require) + #expect(rebuilt.sslConfiguration?.mode == .verifyFull) #expect(rebuilt.sslConfiguration?.caCertificatePath == "/Users/mac/ca.pem") } diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/ConnectionSecretsTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/ConnectionSecretsTests.swift index fef4b5ee3d..fe3a3a6be8 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/ConnectionSecretsTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/ConnectionSecretsTests.swift @@ -73,6 +73,8 @@ struct ConnectionSecretsTests { "com.TablePro.plugin.awsSecretAccessKey.", "com.TablePro.plugin.awsSessionToken.", "com.TablePro.plugin.gcpServiceAccountKey.", + "com.TablePro.plugin.mongoAWSSessionToken.", + "com.TablePro.sslkeypassphrase.", "com.TablePro.deviceiam." ]) let id = UUID() diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/MongoURISecretKeychainMoveTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/MongoURISecretKeychainMoveTests.swift new file mode 100644 index 0000000000..1a755b5749 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/MongoURISecretKeychainMoveTests.swift @@ -0,0 +1,58 @@ +import Foundation +import TableProModels +import Testing + +@testable import TableProMobile + +struct MongoURISecretKeychainMoveTests { + @Test("A synced MongoDB row's connection-string secrets move to the Keychain under the Mac's names") + func secretsMoveToTheKeychain() throws { + let connection = DatabaseConnection( + name: "Docs", + type: .mongodb, + host: "db.example.com", + port: 27_017, + additionalFields: [ + "mongoParam_tlsCertificateKeyFilePassword": "hunter2", + "mongoParam_proxyPassword": "proxy-secret", + "mongoParam_authMechanismProperties": "SERVICE_NAME:mongodb,AWS_SESSION_TOKEN:FQoG", + "mongoParam_retryWrites": "true" + ] + ) + let store = EphemeralSecureStore() + + let kept = MongoURISecretMigration.keepingSecretsInKeychain(connection, in: store) + + #expect(kept.additionalFields == [ + "mongoParam_authMechanismProperties": "SERVICE_NAME:mongodb", + "mongoParam_retryWrites": "true" + ]) + #expect(try store.retrieve(forKey: ConnectionSecretKind.sslKeyPassphrase.account(for: connection.id)) == "hunter2") + #expect(try store.retrieve(forKey: ConnectionSecretKind.mongoAWSSessionToken.account(for: connection.id)) == "FQoG") + } + + @Test("A row with no connection-string secret is left as it is") + func ordinaryRowIsUntouched() { + let connection = DatabaseConnection( + name: "Docs", type: .mongodb, host: "db.example.com", port: 27_017, + additionalFields: ["mongoParam_retryWrites": "true"] + ) + + #expect(MongoURISecretMigration.keepingSecretsInKeychain(connection, in: EphemeralSecureStore()) == connection) + } + + @Test("A stale copy a legacy record carries never replaces the Keychain's own") + func keychainValueWins() throws { + let connection = DatabaseConnection( + name: "Docs", type: .mongodb, host: "db.example.com", port: 27_017, + additionalFields: ["mongoParam_sslClientCertificateKeyPassword": "stale"] + ) + let account = ConnectionSecretKind.sslKeyPassphrase.account(for: connection.id) + let store = EphemeralSecureStore([account: "current"]) + + let kept = MongoURISecretMigration.keepingSecretsInKeychain(connection, in: store) + + #expect(kept.additionalFields.isEmpty) + #expect(try store.retrieve(forKey: account) == "current") + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/DriverSSLConfigurationTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/DriverSSLConfigurationTests.swift index 7173505551..5fcf7b72a6 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/DriverSSLConfigurationTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/DriverSSLConfigurationTests.swift @@ -93,6 +93,37 @@ struct DriverSSLConfigurationTests { #expect(mssqlEncryptionLevel(SSLConfiguration(mode: .require)) == .require) } + @Test("The SQL Server driver refuses a synced verify mode it cannot honour before dialing", arguments: [ + SSLConfiguration.SSLMode.verifyCa, .verifyFull + ]) + func mssqlDriverRefusesVerifyModes(mode: SSLConfiguration.SSLMode) async { + let driver = MSSQLDriver( + connection: DatabaseConnection( + type: .mssql, + host: "sql.example.com", + port: 1_433, + sslEnabled: true, + sslConfiguration: SSLConfiguration(mode: mode, caCertificatePath: "/Users/alice/corp-ca.pem") + ), + password: "secret" + ) + + #expect(driver.options.certificateVerification.needsAuthority) + await #expect { + try await driver.connect() + } throws: { error in + (error as? DatabaseError)?.message == MSSQLDriver.verifyModeUnavailableMessage + } + } + + @Test("The SQL Server driver checks no certificate only where the mode asks for none") + func mssqlCertificateVerificationMapping() { + #expect(DriverSSLConfiguration(mode: .disable).mssqlCertificateVerification == .none) + #expect(DriverSSLConfiguration(mode: .require).mssqlCertificateVerification == .none) + #expect(DriverSSLConfiguration(mode: .verifyCa).mssqlCertificateVerification == .chain) + #expect(DriverSSLConfiguration(mode: .verifyFull).mssqlCertificateVerification == .chainAndHostname) + } + private func mssqlEncryptionLevel(_ configuration: SSLConfiguration) -> MSSQLEncryptionLevel { MSSQLDriver( connection: DatabaseConnection( diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MongoDBOnIOSTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MongoDBOnIOSTests.swift index 715cca4148..c26ca1a159 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/MongoDBOnIOSTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MongoDBOnIOSTests.swift @@ -252,6 +252,24 @@ struct MongoDBOnIOSTests { } } + @Test("A session token and key passphrase the Mac moved to the Keychain reach the driver") + func sessionTokenComesFromTheKeychain() { + let saved = connection(fields: ["mongoAuthMechanism": "MONGODB-AWS"]) + let store = EphemeralSecureStore([ + ConnectionSecretKind.mongoAWSSessionToken.account(for: saved.id): "FQoG", + ConnectionSecretKind.sslKeyPassphrase.account(for: saved.id): "hunter2" + ]) + + let fields = MongoDBDialPlan.addingKeychainSecrets(to: saved.additionalFields, of: saved, in: store) + + #expect(fields == [ + "mongoAuthMechanism": "MONGODB-AWS", + "mongoAWSSessionToken": "FQoG", + "sslClientKeyPassphrase": "hunter2" + ]) + #expect(MongoDBDialPlan.addingKeychainSecrets(to: [:], of: saved, in: EphemeralSecureStore()).isEmpty) + } + @Test("Without a tunnel the fields reach the plugin as stored") func directKeepsTheFields() throws { let stored = ["mongoHosts": "db1.internal:27017,db2.internal:27018"] diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MongoDBPluginConnectionTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MongoDBPluginConnectionTests.swift index 5fefd4cd4c..db073cba80 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/MongoDBPluginConnectionTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MongoDBPluginConnectionTests.swift @@ -209,6 +209,32 @@ struct MongoDBConnectionURITests { #expect(!mongoc_uri_get_option_as_bool(parsed, MONGOC_URI_TLSINSECURE, false)) } + @Test("The Keychain's passphrase and session token replace any spelling a pasted string left") + func keychainSecretsReplacePastedSpellings() { + var params = [ + "sslClientCertificateKeyPassword": "old", + "authmechanismproperties": "AWS_SESSION_TOKEN:old,SERVICE_NAME:mongodb", + "retryWrites": "true" + ] + + MongoDBConnectionURI.applySecrets(to: ¶ms, keyPassphrase: "new", sessionToken: "FQoG") + + #expect(params == [ + "tlsCertificateKeyFilePassword": "new", + "authMechanismProperties": "SERVICE_NAME:mongodb,AWS_SESSION_TOKEN:FQoG", + "retryWrites": "true" + ]) + } + + @Test("Without a Keychain secret the options are left as they are") + func noKeychainSecretLeavesOptions() { + var params = ["authMechanismProperties": "SERVICE_NAME:mongodb"] + + MongoDBConnectionURI.applySecrets(to: ¶ms, keyPassphrase: nil, sessionToken: "") + + #expect(params == ["authMechanismProperties": "SERVICE_NAME:mongodb"]) + } + @Test("A typed parameter named like a field's key in any case never overrides the field") func typedParameterKeysCompareWithoutCase() throws { let options = MongoDBConnectionURI.options( diff --git a/apple/TableProTests/Core/Database/ConnectionCredentialResolverPgpassTests.swift b/apple/TableProTests/Core/Database/ConnectionCredentialResolverPgpassTests.swift new file mode 100644 index 0000000000..178a0716e9 --- /dev/null +++ b/apple/TableProTests/Core/Database/ConnectionCredentialResolverPgpassTests.swift @@ -0,0 +1,31 @@ +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct ConnectionCredentialResolverPgpassTests { + @Test("Only engines that declare the pgpass field read ~/.pgpass") + func pgpassBelongsToThePostgreSQLFamily() { + #expect(ConnectionCredentialResolver.readsPgpass(TestFixtures.makeConnection(type: .postgresql))) + #expect(ConnectionCredentialResolver.readsPgpass(TestFixtures.makeConnection(type: .redshift))) + #expect(!ConnectionCredentialResolver.readsPgpass(TestFixtures.makeConnection(type: .redis))) + #expect(!ConnectionCredentialResolver.readsPgpass(TestFixtures.makeConnection(type: .mongodb))) + #expect(!ConnectionCredentialResolver.readsPgpass(TestFixtures.makeConnection(type: .mysql))) + } + + @Test("A pgpass profile asks for the password on an engine that can dial hosts ~/.pgpass never saw") + func pgpassProfileAsksOutsideThePostgreSQLFamily() throws { + let profile = CredentialProfile(name: "pgpass \(UUID().uuidString)", username: "app", passwordMode: .pgpass) + let profiles = CredentialProfileStorage.shared + try #require(profiles.addProfile(profile)) + defer { _ = profiles.deleteProfile(profile) } + + var redis = TestFixtures.makeConnection(type: .redis) + redis.credentialMode = .profile(id: profile.id) + var postgres = TestFixtures.makeConnection(type: .postgresql) + postgres.credentialMode = .profile(id: profile.id) + + #expect(ConnectionCredentialResolver.promptsForPassword(redis)) + #expect(!ConnectionCredentialResolver.promptsForPassword(postgres)) + } +} diff --git a/apple/TableProTests/Core/Services/ProjectImport/ProjectFolderScannerTests.swift b/apple/TableProTests/Core/Services/ProjectImport/ProjectFolderScannerTests.swift index 29e50c1e9a..9df04f968e 100644 --- a/apple/TableProTests/Core/Services/ProjectImport/ProjectFolderScannerTests.swift +++ b/apple/TableProTests/Core/Services/ProjectImport/ProjectFolderScannerTests.swift @@ -137,6 +137,55 @@ struct ProjectFolderScannerTests { } } + @Test("A dotenv reference the project does not define is not filled from TablePro's environment") + func dotenvReferenceIgnoresProcessEnvironment() throws { + let (name, secret) = setProbeVariable() + defer { unsetenv(name) } + try write("DB_CONNECTION=pgsql\nDB_HOST=db.example.com\nDB_USERNAME=app\nDB_PASSWORD=${\(name)}", to: ".env") + + let candidate = try #require(try scan().candidates.first) + + #expect(candidate.parsedURL.host == "db.example.com") + #expect(candidate.parsedURL.password != secret) + #expect(!candidate.hasPassword) + } + + @Test("A Spring placeholder the project does not define is not filled from TablePro's environment") + func springPlaceholderIgnoresProcessEnvironment() throws { + let (name, secret) = setProbeVariable() + defer { unsetenv(name) } + try write( + """ + spring.datasource.url=jdbc:postgresql://db.example.com:5432/appdb + spring.datasource.username=app + spring.datasource.password=${\(name)} + """, + to: "application.properties" + ) + try write( + """ + spring: + datasource: + url: jdbc:postgresql://db.example.com:5432/appdb + username: app + password: ${\(name)} + """, + to: "application.yml" + ) + + let result = try scan() + + #expect(!result.candidates.isEmpty) + #expect(!result.candidates.contains { $0.parsedURL.password == secret }) + } + + private func setProbeVariable() -> (name: String, secret: String) { + let name = "TABLEPRO_SCAN_PROBE_\(UUID().uuidString.replacingOccurrences(of: "-", with: ""))" + let secret = UUID().uuidString + setenv(name, secret, 1) + return (name, secret) + } + @Test("Results are ordered with the most trustworthy sources first") func testOrdering() throws { try write("DATABASE_URL=postgresql://u:p@localhost:5432/fromlocal", to: ".env.local") diff --git a/apple/TableProTests/Core/Storage/CredentialProfileImportTests.swift b/apple/TableProTests/Core/Storage/CredentialProfileImportTests.swift index 20c84f88d4..06b919705b 100644 --- a/apple/TableProTests/Core/Storage/CredentialProfileImportTests.swift +++ b/apple/TableProTests/Core/Storage/CredentialProfileImportTests.swift @@ -77,7 +77,7 @@ struct CredentialProfileImportTests { #expect(storage.loadProfiles().first { $0.name == "Prod Reader" }?.username == "local_user") } - @Test("A created profile asks for its password unless it reads pgpass") + @Test("A created profile asks for its password, whatever mode the file names") func createdProfileAsksForItsPassword() throws { let created = try #require(storage.addImportedProfiles([ planned("stored", name: "Stored", passwordMode: .stored, secureFieldIds: ["awsSecretAccessKey"]), @@ -89,7 +89,7 @@ struct CredentialProfileImportTests { #expect(stored.passwordMode == .prompt) #expect(stored.secureFieldIds == ["awsSecretAccessKey"]) let pgpassId = try #require(created["pgpass"]) - #expect(storage.profile(for: pgpassId)?.passwordMode == .pgpass) + #expect(storage.profile(for: pgpassId)?.passwordMode == .prompt) } @Test("Two rows with one name create one profile") diff --git a/apple/TableProTests/Core/Storage/MongoURISecretMigrationTests.swift b/apple/TableProTests/Core/Storage/MongoURISecretMigrationTests.swift new file mode 100644 index 0000000000..06899ed335 --- /dev/null +++ b/apple/TableProTests/Core/Storage/MongoURISecretMigrationTests.swift @@ -0,0 +1,89 @@ +import Foundation +import TableProSyncTransport +import Testing + +@testable import TablePro + +@MainActor +struct MongoURISecretMigrationTests { + private let pool = EphemeralDefaultsPool() + private let storage: ConnectionStorage + private let keychain: InMemoryKeychain + + init() throws { + let directory = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent(UUID().uuidString) + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + keychain = InMemoryKeychain() + storage = ConnectionStorage( + fileURL: directory.appendingPathComponent("connections.json"), + userDefaults: try pool.make().defaults, + syncTracker: SyncChangeTracker(metadataStorage: SyncMetadataStorage(userDefaults: try pool.make().defaults)), + keychain: keychain, + integrity: ConnectionStoreIntegrity(keySource: StoredIntegrityKeySource(store: keychain)) + ) + } + + @Test("Saved connection-string secrets move to the Keychain and leave the record") + func secretsMoveToTheKeychain() throws { + var connection = TestFixtures.makeConnection(type: .mongodb) + connection.additionalFields = [ + "mongoParam_tlsCertificateKeyFilePassword": "hunter2", + "mongoParam_proxyPassword": "proxy-secret", + "mongoParam_authMechanismProperties": "SERVICE_NAME:mongodb,AWS_SESSION_TOKEN:FQoG", + "mongoParam_retryWrites": "true" + ] + try #require(storage.addConnection(connection)) + + storage.moveMongoURISecretsToKeychain() + + let stored = try #require(storage.loadConnections().first { $0.id == connection.id }) + #expect(stored.additionalFields == [ + "mongoParam_authMechanismProperties": "SERVICE_NAME:mongodb", + "mongoParam_retryWrites": "true" + ]) + #expect(storage.loadSSLClientKeyPassphrase(for: connection.id) == "hunter2") + #expect(storage.loadPluginSecureField(fieldId: "mongoAWSSessionToken", for: connection.id) == "FQoG") + } + + @Test("A connection with no connection-string secret is left as it is") + func ordinaryConnectionIsUntouched() throws { + var connection = TestFixtures.makeConnection(type: .mongodb) + connection.additionalFields = ["mongoParam_retryWrites": "true"] + try #require(storage.addConnection(connection)) + + storage.moveMongoURISecretsToKeychain() + + #expect(storage.loadConnections().first { $0.id == connection.id }?.additionalFields == connection.additionalFields) + #expect(storage.loadSSLClientKeyPassphrase(for: connection.id) == nil) + } + + @Test("A connection a sync brings in keeps its secrets in the Keychain, not in the record") + func incomingConnectionKeepsSecretsInTheKeychain() { + var incoming = TestFixtures.makeConnection(type: .mongodb) + incoming.additionalFields = [ + "mongoParam_sslClientCertificateKeyPassword": "hunter2", + "mongoAWSSessionToken": "FQoG", + "mongoAuthSource": "admin" + ] + + let kept = storage.keepingMongoURISecretsInKeychain(incoming) + + #expect(kept.additionalFields == ["mongoAuthSource": "admin"]) + #expect(storage.loadSSLClientKeyPassphrase(for: incoming.id) == "hunter2") + #expect(storage.loadPluginSecureField(fieldId: "mongoAWSSessionToken", for: incoming.id) == "FQoG") + } + + @Test("A stale copy a legacy record carries never replaces the Keychain's own") + func keychainValueWins() { + var incoming = TestFixtures.makeConnection(type: .mongodb) + incoming.additionalFields = ["mongoParam_authMechanismProperties": "AWS_SESSION_TOKEN:stale"] + storage.savePluginSecureField("current", fieldId: "mongoAWSSessionToken", for: incoming.id) + + let kept = storage.keepingMongoURISecretsInKeychain(incoming) + + #expect(kept.additionalFields.isEmpty) + #expect(storage.loadPluginSecureField(fieldId: "mongoAWSSessionToken", for: incoming.id) == "current") + } +} diff --git a/apple/TableProTests/Core/Sync/SyncRecordMapperConnectionTests.swift b/apple/TableProTests/Core/Sync/SyncRecordMapperConnectionTests.swift index 254ccee63b..01deb5ee27 100644 --- a/apple/TableProTests/Core/Sync/SyncRecordMapperConnectionTests.swift +++ b/apple/TableProTests/Core/Sync/SyncRecordMapperConnectionTests.swift @@ -260,6 +260,39 @@ struct SyncRecordMapperConnectionTests { #expect(additionalFields["schema"] == "public") } + @Test("A MongoDB connection-string secret never reaches the record") + func mongoURISecretsStayOffTheRecord() throws { + var connection = TestFixtures.makeConnection(type: .mongodb) + connection.additionalFields = [ + "mongoParam_tlsCertificateKeyFilePassword": "hunter2", + "mongoParam_proxyPassword": "proxy-secret", + "mongoParam_authMechanismProperties": "AWS_SESSION_TOKEN:FQoG", + "mongoParam_retryWrites": "true" + ] + + let record = SyncRecordMapper.toCKRecord(connection, in: zoneID) + let data = try #require(record[ConnectionSyncField.additionalFieldsJson.key] as? Data) + let additionalFields = try JSONDecoder().decode([String: String].self, from: data) + + #expect(additionalFields == ["mongoParam_retryWrites": "true"]) + } + + @Test("A rebased record loses a secret an older build left in the server's additional fields") + func rebasedRecordLosesLegacySecret() throws { + var legacy = TestFixtures.makeConnection(type: .mongodb) + legacy.additionalFields = ["mongoParam_retryWrites": "true"] + let record = SyncRecordMapper.toCKRecord(legacy, in: zoneID) + record[ConnectionSyncField.additionalFieldsJson.key] = try JSONEncoder().encode([ + "mongoParam_tlsCertificateKeyFilePassword": "hunter2", + "mongoParam_retryWrites": "true" + ]) as CKRecordValue + + SyncRecordMapper.removeSecretOptions(from: record) + + let data = try #require(record[ConnectionSyncField.additionalFieldsJson.key] as? Data) + #expect(try JSONDecoder().decode([String: String].self, from: data) == ["mongoParam_retryWrites": "true"]) + } + @Test("Clearing timeouts and other additional fields clears the based record") func clearingTimeoutsClearsBasedRecord() { var connection = makeFullyPopulatedConnection() diff --git a/apple/TableProTests/Core/Utilities/ConnectionURLImportUsernameTests.swift b/apple/TableProTests/Core/Utilities/ConnectionURLImportUsernameTests.swift index d02f4c42a1..4305a000a8 100644 --- a/apple/TableProTests/Core/Utilities/ConnectionURLImportUsernameTests.swift +++ b/apple/TableProTests/Core/Utilities/ConnectionURLImportUsernameTests.swift @@ -39,6 +39,23 @@ struct ConnectionURLImportUsernameTests { #expect(connection.username == "admin") } + @Test("An opened MongoDB link keeps its secrets where the driver and backup tools read them, not as options") + func openedMongoLinkRoutesSecrets() throws { + let parsed = try parse( + "mongodb://AKIA@db.example.com:27017/app?tls=true&authMechanism=MONGODB-AWS" + + "&tlsCertificateKeyFile=%2Ftmp%2Fclient.pem&tlsCertificateKeyFilePassword=hunter2" + + "&authMechanismProperties=AWS_SESSION_TOKEN%3AFQoG&proxyPassword=proxy-secret" + ) + let connection = TransientConnectionFactory.build(from: parsed) + + #expect(connection.additionalFields["mongoAWSSessionToken"] == "FQoG") + #expect(connection.additionalFields["sslClientKeyPassphrase"] == "hunter2") + #expect(connection.sslConfig.clientCertificatePath == "/tmp/client.pem") + #expect(!connection.additionalFields.keys.contains { $0.hasPrefix("mongoParam_") }) + let url = ConnectionURLFormatter.format(connection, password: "", sshPassword: nil) + #expect(!url.contains("hunter2") && !url.contains("FQoG") && !url.contains("proxy-secret")) + } + @Test("An imported connection with no username exports a URL with no user info") func emptyUsernameRoundTripsThroughFormatter() throws { let parsed = try parse("mysql://localhost:3306/shop") diff --git a/apple/TableProTests/Database/NativeDumpRegistryTests.swift b/apple/TableProTests/Database/NativeDumpRegistryTests.swift index 2544e836af..cc5d72c1a6 100644 --- a/apple/TableProTests/Database/NativeDumpRegistryTests.swift +++ b/apple/TableProTests/Database/NativeDumpRegistryTests.swift @@ -225,6 +225,24 @@ struct NativeDumpRegistryTests { #expect(!built.arguments.contains { $0.hasPrefix("--host") || $0.hasPrefix("--port") }) } + @Test("The tools sign in with the Keychain's key passphrase and session token, never a plain copy") + func mongoToolsGetTheKeychainSecrets() throws { + var aws = connection(type: .mongodb, sslMode: .required, sslEnabled: true) + aws.sslConfig.clientCertificatePath = "/Users/alice/client.pem" + aws.mongoAuthMechanism = "MONGODB-AWS" + aws.additionalFields["sslClientKeyPassphrase"] = "hunter2" + aws.additionalFields["mongoAWSSessionToken"] = "FQoG" + aws.additionalFields["mongoParam_authMechanismProperties"] = "AWS_SESSION_TOKEN:stale" + aws.additionalFields["mongoParam_proxyPassword"] = "proxy-secret" + + let uri = try configurationValue("uri", in: configuration(of: command(.mongodb, connection: aws))) + + #expect(uri.contains("tlsCertificateKeyFilePassword=hunter2")) + #expect(uri.contains("authMechanismProperties=AWS_SESSION_TOKEN%3AFQoG")) + #expect(!uri.contains("stale")) + #expect(!uri.contains("proxy-secret")) + } + @Test("A host list and its replica set reach the tool whole, not as the first host") func mongoHostListUsesAConnectionString() throws { var replicaSet = connection(type: .mongodb, host: "a.example.com", port: 27_017) diff --git a/apple/TableProTests/ViewModels/ConnectionFormURLImportTests.swift b/apple/TableProTests/ViewModels/ConnectionFormURLImportTests.swift index 6059654d86..34fa795cec 100644 --- a/apple/TableProTests/ViewModels/ConnectionFormURLImportTests.swift +++ b/apple/TableProTests/ViewModels/ConnectionFormURLImportTests.swift @@ -20,6 +20,27 @@ struct ConnectionFormURLImportTests { return coordinator } + @Test("Secrets a MongoDB connection string carries reach Keychain-backed fields, never plain options") + func mongoURISecretsStayOutOfPlainOptions() throws { + let coordinator = try formImporting( + "mongodb://app:s3cret@db.example.com:27017/orders?tls=true" + + "&tlsCertificateKeyFile=%2FUsers%2Falice%2Fclient.pem&tlsCertificateKeyFilePassword=hunter2" + + "&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN%3AFQoGZXIvYXdz" + + "&proxyPassword=proxy-secret&retryWrites=true" + ) + let plain = coordinator.allAdditionalFieldValues.filter { $0.key != "mongoAWSSessionToken" } + + #expect(coordinator.ssl.clientCertPath == "/Users/alice/client.pem") + #expect(coordinator.ssl.clientKeyPassphrase == "hunter2") + #expect(coordinator.ssl.clientKeyPath == "/Users/alice/client.pem") + #expect(coordinator.auth.additionalFieldValues["mongoAWSSessionToken"] == "FQoGZXIvYXdz") + #expect(coordinator.secureFieldsOwnedByForm().contains { $0.id == "mongoAWSSessionToken" }) + #expect(!plain.values.contains("hunter2")) + #expect(!plain.values.contains { $0.contains("FQoGZXIvYXdz") }) + #expect(!plain.values.contains("proxy-secret")) + #expect(plain["mongoParam_retryWrites"] == "true") + } + @Test("sslmode=verify-full opens the form on Verify Identity") func verifyFullKeepsIdentityVerification() throws { let coordinator = try formImporting("postgresql://u@db.example.com/app?sslmode=verify-full") diff --git a/apple/TableProTests/Views/Connection/ImportReviewTests.swift b/apple/TableProTests/Views/Connection/ImportReviewTests.swift index 1e208fd189..5495d11592 100644 --- a/apple/TableProTests/Views/Connection/ImportReviewTests.swift +++ b/apple/TableProTests/Views/Connection/ImportReviewTests.swift @@ -104,6 +104,28 @@ struct ImportReviewTests { #expect(store.written.first?.planned.settings.startupCommands == nil) } + @Test("The command confirmation lists Startup SQL whole, so what is kept is what was shown") + func commandConfirmationShowsWholeStartupSQL() async throws { + var settings = Self.orders + settings.startupCommands = """ + SET search_path TO app; + -- session defaults \(String(repeating: "-", count: 300)) + DROP TABLE IF EXISTS audit_log; + """ + let store = RecordingLibraryStore() + let review = try makeReview(settings: settings, store: store) + + let confirmation = ImportReviewStep.commandConfirmation(for: review.rowsWithCommands) + _ = await review.commit(keepingCommands: true) + let kept = try #require(store.written.first?.planned.settings.startupCommands) + + #expect(confirmation.contains("SET search_path TO app;")) + #expect(!confirmation.contains("…")) + for line in kept.split(separator: "\n") { + #expect(confirmation.contains(line)) + } + } + @Test("Keep Existing writes no settings, so its tunnel command is not asked about") func keepExistingSkipsTunnelQuestion() throws { var settings = Self.orders diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.1.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.1.md new file mode 100644 index 0000000000..f34c040196 --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.1.md @@ -0,0 +1 @@ +Shared connections reusing your Entra ID token, Snowflake CLI profile, MongoDB AWS credentials or Run Ledger Plugins. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.10.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.10.md new file mode 100644 index 0000000000..eda1a215d4 --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.10.md @@ -0,0 +1 @@ +Open Project Folder filling a project's ${VAR} references from TablePro's own environment. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.2.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.2.md new file mode 100644 index 0000000000..f8b3dda809 --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.2.md @@ -0,0 +1 @@ +A Firebase Database URL with an encoded slash sending your Google access token to another host. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.3.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.3.md new file mode 100644 index 0000000000..f1a9bcc9f3 --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.3.md @@ -0,0 +1 @@ +Cassandra and ScyllaDB connections through a tunnel signing in to other cluster nodes outside the tunnel. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.4.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.4.md new file mode 100644 index 0000000000..a612b62191 --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.4.md @@ -0,0 +1 @@ +SQL Server Verify CA and Verify Identity on iPhone and iPad connecting without checking the certificate. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.5.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.5.md new file mode 100644 index 0000000000..2f6c25bb7b --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.5.md @@ -0,0 +1 @@ +Secrets in a pasted MongoDB connection string saved outside the Keychain and carried by exports, links and sync. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.6.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.6.md new file mode 100644 index 0000000000..48b7035998 --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.6.md @@ -0,0 +1 @@ +Trino and Presto sending the password or token to another host the server named in its reply. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.7.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.7.md new file mode 100644 index 0000000000..6388e71ae1 --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.7.md @@ -0,0 +1 @@ +Dameng connections set to Required or a Verify mode connecting without TLS. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.8.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.8.md new file mode 100644 index 0000000000..c8328274dc --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.8.md @@ -0,0 +1 @@ +Teradata with SSL through a tunnel sending the password to port 443 on this Mac. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.9.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.9.md new file mode 100644 index 0000000000..fa6351bcea --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.9.md @@ -0,0 +1 @@ +Import Commands showing the first 300 characters of Startup SQL while keeping all of it. diff --git a/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.md b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.md new file mode 100644 index 0000000000..7e898d4b4e --- /dev/null +++ b/apple/changelog.d/+ghsa-j2xw-m3p7-3m64.security.md @@ -0,0 +1 @@ +A credential profile in a shared connection file reading ~/.pgpass and sending the password to a host the file named. diff --git a/apple/scripts/probes/check-mssql-encryption.sh b/apple/scripts/probes/check-mssql-encryption.sh index 7fd17d83bf..be7d6395fb 100755 --- a/apple/scripts/probes/check-mssql-encryption.sh +++ b/apple/scripts/probes/check-mssql-encryption.sh @@ -451,6 +451,7 @@ enum Check { password: "", database: database, encryptionLevel: .require, + certificateVerification: .none, authMethod: .windows, kerberosCachePath: cachePath, kerberosServicePrincipal: servicePrincipal From c084acc39b7ee6b2ea8efba3daf6bf8eb774ce1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sun, 1 Nov 2026 00:47:47 +0700 Subject: [PATCH 52/85] Merge commit from fork * fix(security): harden the Git commands TablePro runs on its own Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-FRDSB33T. Advisory: GHSA-rhxc-5m3h-r5ch * fix(security): pin PostgreSQL catalog reads to pg_catalog and SYS-qualify Oracle Explain Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CDER3MTT. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CRPAPF23. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): harden dump tools, shell quoting and SSH host names Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-1D30XTRS. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-2L44V7CH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-3NLPR9WG. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-AX94JCED. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-E9V7ZHHQ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TWLM1VYZ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-U19XTGQC. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CF3R4T4R. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BZJK0VNE. Advisory: GHSA-94m6-qx4g-cpwg * fix(security): keep credentials on their own host and refuse TLS modes a driver cannot honour Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-EJ1059QD. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-L54ARVVX. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-R0FNK1XO. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KSESNBFN. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KGP1I8T0. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-11I9A28E. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-OP00LFZB. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-6ES8FYVM. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-60V4XYJM. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-YB76IHAI. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-9RQLLRZ0. Advisory: GHSA-j2xw-m3p7-3m64 * fix(security): escape quote characters by code unit in SQL quoters and escapers Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-MQSU9V64. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ANTKKVJ8. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TGUXS7JG. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-JXEUCZFY. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-RU2Q3CD4. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-LDFEMPI5. Advisory: GHSA-58fh-9ghw-9qwv * fix(security): write Oracle Estimated Plan to SYS.PLAN_TABLE$ Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): restore a DuckDB Parquet folder through a locked-down instance of its own * test(security): expect the pg_catalog-qualified PostgreSQL cancel statement --- .../SQLCompletionProvider.swift | 6 +- .../SQLIdentifierSpelling.swift | 4 +- .../TableProDatabase/SQLEscaping.swift | 16 ++-- .../Firestore/FirestoreQuery.swift | 2 +- .../MySQLLiteralSpelling.swift | 2 +- .../MySQLObjectQueries.swift | 8 +- .../LibPQStringConformance.swift | 6 +- .../TableProQuery/FilterSQLGenerator.swift | 6 +- .../R2SQLIntrospectionSQL.swift | 2 +- .../TableProRedisCore/RedisQueryBuilder.swift | 32 +++---- .../SQL/SpannerDialect.swift | 4 +- .../TableProTabularIO/DelimitedWriter.swift | 6 +- .../TeradataMessages.swift | 4 +- .../TableProTrinoCore/TrinoLiteral.swift | 2 +- .../WeaviateFilterBuilder.swift | 8 +- .../MySQLLiteralSpellingTests.swift | 6 ++ .../MySQLObjectQueryTests.swift | 6 ++ .../MySQLGrantEscapingTests.swift | 8 ++ .../PluginExportUtilitiesTests.swift | 15 +++ .../PluginParameterEscapingTests.swift | 41 +++++++++ .../LibPQStringConformanceTests.swift | 9 ++ .../FilterSQLGeneratorTests.swift | 13 +++ .../CommandLine/CLIRowWriter.swift | 5 +- .../OracleCellFormatting.swift | 4 +- .../BeancountPluginDriver.swift | 4 +- .../BigQueryQueryBuilder.swift | 2 +- .../CSVExportPlugin/CSVExportPlugin.swift | 8 +- .../CassandraBrowseStatement.swift | 2 +- .../CassandraConnection.swift | 2 +- .../CassandraPlugin.swift | 4 +- .../ClickHousePlugin.swift | 2 +- .../ClickHousePluginDriver+Routines.swift | 4 +- .../CloudflareD1DefaultValue.swift | 2 +- .../CloudflareD1PluginDriver.swift | 17 +--- .../DynamoDBPluginDriver.swift | 2 +- .../DynamoDBStatement.swift | 2 +- .../DynamoDBWriteStatements.swift | 2 +- .../ElasticsearchQueryBuilder.swift | 6 +- .../SQLExportCraftedNameTests.swift | 92 +++++++++++++++++++ .../KafkaDriverPlugin/KafkaPluginDriver.swift | 4 +- apple/Plugins/KafkaDriverPlugin/KafkaQL.swift | 4 +- .../MongoDBQueryBuilder.swift | 12 +-- .../MongoDBShellLiteral.swift | 2 +- .../MongoDBDriverPlugin/MongoScriptJson.swift | 2 +- .../MongoShellCommandLine.swift | 2 +- .../MySQLDriverPlugin/DatabendCatalog.swift | 6 +- .../MySQLColumnDefinitionSQL.swift | 2 +- .../MySQLPluginDriver+CreateDatabase.swift | 2 +- .../DatabendCatalogTests.swift | 6 ++ .../MySQLColumnDefinitionSQLTests.swift | 6 ++ .../OracleDriverPlugin/OracleBrowseSQL.swift | 6 +- .../OracleIndexStatements.swift | 4 +- .../OracleDriverPlugin/OraclePlugin.swift | 2 +- .../OracleDriverPlugin/OracleRowWriter.swift | 2 +- .../DuckDBStagingDatabase.swift | 4 +- .../PostgreSQLSchemaQueries.swift | 2 +- .../PostgreSQLVersionedStatements.swift | 2 +- .../PostgreSQLVersionedStatementsTests.swift | 7 ++ .../SQLExportPlugin/SQLExportPlugin.swift | 4 +- .../SnowflakeDriverPlugin/SnowflakeSQL.swift | 4 +- .../SurrealDBDriverPlugin/SurrealQL.swift | 12 +-- .../MongoShellValueTranslator.swift | 4 +- .../MySQLGrantPatternEscaping.swift | 30 +++--- .../PluginDatabaseDriver.swift | 10 +- .../PluginExportUtilities.swift | 20 ++-- .../TableProPluginKit/PluginRowWriters.swift | 6 +- .../PluginSQLRegexPattern.swift | 13 ++- .../TeradataRowEditSQL.swift | 2 +- .../TypesenseFilterBuilder.swift | 2 +- .../Core/Compare/CompareSQLLiteral.swift | 6 +- .../CrossEngine/CrossEngineValueCoercer.swift | 10 +- .../SQLTypeRenderer+BundledFamilies.swift | 2 +- .../SQLTypeRenderer+RegistryFamilies.swift | 2 +- .../DataWrite/Rewind/RewindExecutor.swift | 6 +- .../Core/Database/FilterSQLGenerator.swift | 4 +- .../TablePro/Core/Database/SQLEscaping.swift | 12 +-- .../Core/Database/TableDDLComposer.swift | 2 +- .../Core/MCP/MCPConnectionBridge+Data.swift | 3 +- .../MCP/Protocol/Tools/MCPExportWriters.swift | 11 ++- .../Services/Export/ImportErrorReport.swift | 12 +-- .../Services/Query/TableQueryBuilder.swift | 7 +- .../Query/UserDefinedTypeSuggestions.swift | 2 +- .../Core/Storage/QueryHistoryStorage.swift | 4 +- .../Core/Storage/SQLFavoriteStorage.swift | 2 +- .../Core/Utilities/SQL/CsvRowConverter.swift | 2 +- .../Utilities/SQL/DialectQuoteHelper.swift | 16 ++-- .../Utilities/SQL/InClauseConverter.swift | 2 +- .../Utilities/SQL/SQLParameterInliner.swift | 2 +- .../Views/Editor/QueryCompletionAdapter.swift | 2 +- .../Views/Main/MainContentCoordinator.swift | 2 +- .../QueryCompletionController.swift | 2 +- .../Drivers/DuckDBDriver+Schema.swift | 4 +- .../TableProMobile/Drivers/DuckDBDriver.swift | 2 +- .../TableProMobile/Drivers/MySQLDriver.swift | 4 +- .../Drivers/PostgreSQLConnectionString.swift | 4 +- .../Drivers/PostgreSQLDriver.swift | 2 +- .../TableProMobile/Drivers/SQLiteDriver.swift | 4 +- .../Helpers/ClipboardExporter.swift | 4 +- .../TableProMobile/Helpers/SQLBuilder.swift | 8 +- .../SQLBuilderIdentifierQuotingTests.swift | 26 ++++++ .../Core/Compare/CompareDataScopeTests.swift | 14 +++ .../Core/DataWrite/RewindPlannerTests.swift | 3 +- .../PluginMetadataRegistryVariantTests.swift | 3 +- .../TableQueryBuilderFilterTests.swift | 66 +++++++++++-- .../TableQueryBuilderSortScopeTests.swift | 4 +- .../Core/Utilities/CsvRowConverterTests.swift | 7 ++ .../Models/PaginationCapabilityTests.swift | 8 +- .../Models/Query/QueryTabBaseQueryTests.swift | 2 +- .../+ghsa-58fh-9ghw-9qwv.security.1.md | 1 + .../+ghsa-58fh-9ghw-9qwv.security.2.md | 1 + .../+ghsa-58fh-9ghw-9qwv.security.3.md | 1 + .../+ghsa-58fh-9ghw-9qwv.security.md | 1 + 112 files changed, 568 insertions(+), 264 deletions(-) create mode 100644 apple/Plugins/FormatPluginTests/SQLExportCraftedNameTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderIdentifierQuotingTests.swift create mode 100644 apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.1.md create mode 100644 apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.2.md create mode 100644 apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.3.md create mode 100644 apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.md diff --git a/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift b/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift index 4c7af0ac3c..ca5511a9f1 100644 --- a/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift +++ b/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift @@ -496,7 +496,7 @@ public final class SQLCompletionProvider { var item = SQLCompletionItem( label: "'\(value)'", kind: .keyword, - insertText: "'\(value.replacingOccurrences(of: "'", with: "''"))'", + insertText: "'\(value.replacingOccurrences(of: "'", with: "''", options: .literal))'", detail: column, filterText: value.lowercased() ) @@ -601,13 +601,13 @@ public final class SQLCompletionProvider { // `AS 'Total'` names the column on MySQL and SQLite, but ORDER BY 'Total' sorts by a constant. private func identifierSpelling(of spelling: String, name: String) -> String { - guard spelling.hasPrefix("'") else { return spelling } + guard spelling.unicodeScalars.first == "'" else { return spelling } let isBare = name.unicodeScalars.first.map { CharacterSet.letters.contains($0) || $0 == "_" } == true && name.unicodeScalars.allSatisfy { CharacterSet.alphanumerics.contains($0) || $0 == "_" } guard !isBare || SQLKeywords.isKeyword(name) else { return name } // The analyzer reads a backslash-escaped `"` as a string, which is where a backtick quotes a name instead. let quote = grammar.isSuperset(of: [.backslashEscapesInDoubleQuotes, .backtickQuotes]) ? "`" : "\"" - return quote + name.replacingOccurrences(of: quote, with: quote + quote) + quote + return quote + name.replacingOccurrences(of: quote, with: quote + quote, options: .literal) + quote } // CTEs were bound to their own scope by the parser, so what is left to expand is a table the catalog knows. diff --git a/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLIdentifierSpelling.swift b/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLIdentifierSpelling.swift index 5531a4ec99..8eadaa7789 100644 --- a/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLIdentifierSpelling.swift +++ b/apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLIdentifierSpelling.swift @@ -103,7 +103,7 @@ public struct SQLIdentifierSpelling: Sendable { public func quoted(_ name: String) -> String { let quote = String(rules.quote) - return quote + name.replacingOccurrences(of: quote, with: quote + quote) + quote + return quote + name.replacingOccurrences(of: quote, with: quote + quote, options: .literal) + quote } // A name written into a statement with nothing typed: as stored keeps the caller's own spelling. @@ -131,7 +131,7 @@ public struct SQLIdentifierSpelling: Sendable { if quoting != .none, identifier.parts.count == 1, case .catalog(let name) = identifier.parts[0] { // The user's own quote asks for the exact name: inside a closed pair it goes in raw. let quote = String(rules.quote) - let text = quoting == .enclosed ? name.replacingOccurrences(of: quote, with: quote + quote) : quoted(name) + let text = quoting == .enclosed ? name.replacingOccurrences(of: quote, with: quote + quote, options: .literal) : quoted(name) return item.recased(label: name, insertText: text) } // A qualified reference replaces the user's quotes, so it is spelled as if none were typed. diff --git a/apple/Packages/TableProCore/Sources/TableProDatabase/SQLEscaping.swift b/apple/Packages/TableProCore/Sources/TableProDatabase/SQLEscaping.swift index 4a31b07ddf..f992ca4a5e 100644 --- a/apple/Packages/TableProCore/Sources/TableProDatabase/SQLEscaping.swift +++ b/apple/Packages/TableProCore/Sources/TableProDatabase/SQLEscaping.swift @@ -3,17 +3,17 @@ import Foundation public enum SQLEscaping { public static func ansiStringLiteral(_ value: String) -> String { value - .replacingOccurrences(of: "\0", with: "") - .replacingOccurrences(of: "'", with: "''") + .replacingOccurrences(of: "\0", with: "", options: .literal) + .replacingOccurrences(of: "'", with: "''", options: .literal) } public static func backslashStringLiteral(_ value: String) -> String { value - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "\0", with: "\\0") - .replacingOccurrences(of: "\n", with: "\\n") - .replacingOccurrences(of: "\r", with: "\\r") - .replacingOccurrences(of: "\u{1a}", with: "\\Z") - .replacingOccurrences(of: "'", with: "''") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "\0", with: "\\0", options: .literal) + .replacingOccurrences(of: "\n", with: "\\n", options: .literal) + .replacingOccurrences(of: "\r", with: "\\r", options: .literal) + .replacingOccurrences(of: "\u{1a}", with: "\\Z", options: .literal) + .replacingOccurrences(of: "'", with: "''", options: .literal) } } diff --git a/apple/Packages/TableProCore/Sources/TableProFirebaseCore/Firestore/FirestoreQuery.swift b/apple/Packages/TableProCore/Sources/TableProFirebaseCore/Firestore/FirestoreQuery.swift index 8760f9b3f7..9fc630c138 100644 --- a/apple/Packages/TableProCore/Sources/TableProFirebaseCore/Firestore/FirestoreQuery.swift +++ b/apple/Packages/TableProCore/Sources/TableProFirebaseCore/Firestore/FirestoreQuery.swift @@ -268,7 +268,7 @@ public enum FirestoreQueryBuilder { if column == nameField { return column } let simple = column.range(of: "^[A-Za-z_][A-Za-z0-9_]*$", options: .regularExpression) != nil if simple { return column } - let escaped = column.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "`", with: "\\`") + let escaped = column.replacingOccurrences(of: "\\", with: "\\\\", options: .literal).replacingOccurrences(of: "`", with: "\\`", options: .literal) return "`" + escaped + "`" } diff --git a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLLiteralSpelling.swift b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLLiteralSpelling.swift index c9098218b2..722a4622ee 100644 --- a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLLiteralSpelling.swift +++ b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLLiteralSpelling.swift @@ -24,7 +24,7 @@ public enum MySQLLiteralSpelling: Equatable, Sendable { case .backslashEscapes: return mysqlEscapeStringLiteral(value) case .quoteDoubling: - return value.replacingOccurrences(of: "'", with: "''") + return value.replacingOccurrences(of: "'", with: "''", options: .literal) } } diff --git a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift index cfb77225bf..76d48cf2c5 100644 --- a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift +++ b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift @@ -14,7 +14,7 @@ public enum MySQLObjectQueries { } public static func quoteIdentifier(_ value: String) -> String { - "`\(value.replacingOccurrences(of: "`", with: "``"))`" + "`\(value.replacingOccurrences(of: "`", with: "``", options: .literal))`" } /// The database a catalog read means. @@ -129,9 +129,9 @@ public enum MySQLObjectQueries { public static func likePattern(matching name: String) -> String { name - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "%", with: "\\%") - .replacingOccurrences(of: "_", with: "\\_") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "%", with: "\\%", options: .literal) + .replacingOccurrences(of: "_", with: "\\_", options: .literal) } /// Whether `information_schema` describes this database at all, in one scalar. diff --git a/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/LibPQStringConformance.swift b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/LibPQStringConformance.swift index f22d445738..8310bc99c2 100644 --- a/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/LibPQStringConformance.swift +++ b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/LibPQStringConformance.swift @@ -22,9 +22,9 @@ public enum LibPQStringConformance { public static func escape(_ value: String, standardConformingStrings: Bool) -> String { let quoted = value - .replacingOccurrences(of: "\0", with: "") - .replacingOccurrences(of: "'", with: "''") + .replacingOccurrences(of: "\0", with: "", options: .literal) + .replacingOccurrences(of: "'", with: "''", options: .literal) guard !standardConformingStrings else { return quoted } - return quoted.replacingOccurrences(of: "\\", with: "\\\\") + return quoted.replacingOccurrences(of: "\\", with: "\\\\", options: .literal) } } diff --git a/apple/Packages/TableProCore/Sources/TableProQuery/FilterSQLGenerator.swift b/apple/Packages/TableProCore/Sources/TableProQuery/FilterSQLGenerator.swift index a3f252e54b..824aaf2522 100644 --- a/apple/Packages/TableProCore/Sources/TableProQuery/FilterSQLGenerator.swift +++ b/apple/Packages/TableProCore/Sources/TableProQuery/FilterSQLGenerator.swift @@ -117,7 +117,7 @@ public struct FilterSQLGenerator: Sendable { private func comparisonCondition( _ column: String, _ operand: String, _ operatorText: String, _ literal: String, folding: PluginSQLCaseFolding ) -> String { - guard folding.foldsComparisonOperands, literal.hasPrefix("'") else { + guard folding.foldsComparisonOperands, literal.unicodeScalars.first == "'" else { return "\(column) \(operatorText) \(literal)" } return "\(folding.fold(operand)) \(operatorText) \(folding.fold(literal))" @@ -172,7 +172,7 @@ public struct FilterSQLGenerator: Sendable { ) -> String { let keyword = negated ? "NOT IN" : "IN" let items = listItems(rawValue).map { escapeValue($0, kind: kind) } - let foldable = folding.foldsComparisonOperands && items.allSatisfy { $0.hasPrefix("'") } + let foldable = folding.foldsComparisonOperands && items.allSatisfy { $0.unicodeScalars.first == "'" } let rendered = foldable ? items.map { folding.fold($0) } : items let subject = foldable ? folding.fold(operand) : column return "\(subject) \(keyword) (\(rendered.joined(separator: ", ")))" @@ -202,7 +202,7 @@ public struct FilterSQLGenerator: Sendable { if let identifierQuoter { return identifierQuoter(name) } let open = dialect.identifierQuote let close = open == "[" ? "]" : open - let escaped = name.replacingOccurrences(of: close, with: "\(close)\(close)") + let escaped = name.replacingOccurrences(of: close, with: "\(close)\(close)", options: .literal) return "\(open)\(escaped)\(close)" } diff --git a/apple/Packages/TableProCore/Sources/TableProR2SQLCore/R2SQLIntrospectionSQL.swift b/apple/Packages/TableProCore/Sources/TableProR2SQLCore/R2SQLIntrospectionSQL.swift index b8cb4bb289..cd67a7309f 100644 --- a/apple/Packages/TableProCore/Sources/TableProR2SQLCore/R2SQLIntrospectionSQL.swift +++ b/apple/Packages/TableProCore/Sources/TableProR2SQLCore/R2SQLIntrospectionSQL.swift @@ -33,7 +33,7 @@ public enum R2SQLIntrospectionSQL { } public static func quoteIdentifier(_ identifier: String) -> String { - "\"" + identifier.replacingOccurrences(of: "\"", with: "\"\"") + "\"" + "\"" + identifier.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) + "\"" } static let namespaceColumns = ["namespace", "namespace_name", "database_name", "schema_name", "databaseName"] diff --git a/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisQueryBuilder.swift b/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisQueryBuilder.swift index b613314ba8..d2eb9468ba 100644 --- a/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisQueryBuilder.swift +++ b/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisQueryBuilder.swift @@ -119,15 +119,15 @@ public struct RedisQueryBuilder: Sendable { } private func quoteForCommand(_ str: String) -> String { - var result = "" - for char in str { - switch char { - case "\\": result += "\\\\" - case "\"": result += "\\\"" - default: result.append(char) + var result = String.UnicodeScalarView() + for scalar in str.unicodeScalars { + switch scalar { + case "\\", "\"": result.append("\\") + default: break } + result.append(scalar) } - return result + return String(result) } /// Try to extract a SCAN-compatible glob pattern from key-column filters @@ -158,18 +158,14 @@ public struct RedisQueryBuilder: Sendable { /// Escape Redis glob special characters in user input. /// Redis SCAN MATCH uses glob-style patterns where *, ?, and [ are special. private func escapeGlobChars(_ str: String) -> String { - var result = "" - for char in str { - switch char { - case "*", "?", "[", "]": - result.append("\\") - result.append(char) - case "\\": - result.append("\\\\") - default: - result.append(char) + var result = String.UnicodeScalarView() + for scalar in str.unicodeScalars { + switch scalar { + case "*", "?", "[", "]", "\\": result.append("\\") + default: break } + result.append(scalar) } - return result + return String(result) } } diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/SQL/SpannerDialect.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/SQL/SpannerDialect.swift index c82f15578a..b90fb6933d 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/SQL/SpannerDialect.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/SQL/SpannerDialect.swift @@ -43,7 +43,7 @@ public enum SpannerDialect: String, Sendable, Codable { case .googleSQL: GoogleSQLLiteral.quotedIdentifier(name) case .postgreSQL: - "\"" + name.replacingOccurrences(of: "\u{0}", with: "").replacingOccurrences(of: "\"", with: "\"\"") + "\"" + "\"" + name.replacingOccurrences(of: "\u{0}", with: "", options: .literal).replacingOccurrences(of: "\"", with: "\"\"", options: .literal) + "\"" } } @@ -56,7 +56,7 @@ public enum SpannerDialect: String, Sendable, Codable { case .googleSQL: GoogleSQLLiteral.escapedStringBody(value) case .postgreSQL: - value.replacingOccurrences(of: "\u{0}", with: "").replacingOccurrences(of: "'", with: "''") + value.replacingOccurrences(of: "\u{0}", with: "", options: .literal).replacingOccurrences(of: "'", with: "''", options: .literal) } } diff --git a/apple/Packages/TableProCore/Sources/TableProTabularIO/DelimitedWriter.swift b/apple/Packages/TableProCore/Sources/TableProTabularIO/DelimitedWriter.swift index bb8407bf00..8d21a5b59f 100644 --- a/apple/Packages/TableProCore/Sources/TableProTabularIO/DelimitedWriter.swift +++ b/apple/Packages/TableProCore/Sources/TableProTabularIO/DelimitedWriter.swift @@ -123,12 +123,12 @@ public struct DelimitedWriter { guard needsQuoting else { return field } let quoteText = String(quote) guard !dialect.escapesByDoubling else { - return quoteText + field.replacingOccurrences(of: quoteText, with: quoteText + quoteText) + quoteText + return quoteText + field.replacingOccurrences(of: quoteText, with: quoteText + quoteText, options: .literal) + quoteText } let escapeText = String(UnicodeScalar(dialect.escape)) let body = field - .replacingOccurrences(of: escapeText, with: escapeText + escapeText) - .replacingOccurrences(of: quoteText, with: escapeText + quoteText) + .replacingOccurrences(of: escapeText, with: escapeText + escapeText, options: .literal) + .replacingOccurrences(of: quoteText, with: escapeText + quoteText, options: .literal) return quoteText + body + quoteText } diff --git a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift index 6bd167d515..fe0fa3c5b7 100644 --- a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift +++ b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift @@ -130,10 +130,10 @@ enum TeradataMessages { } private static func quoteDouble(_ value: String) -> String { - "\"" + value.replacingOccurrences(of: "\"", with: "\"\"") + "\"" + "\"" + value.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) + "\"" } private static func quoteSingle(_ value: String) -> String { - "'" + value.replacingOccurrences(of: "'", with: "''") + "'" + "'" + value.replacingOccurrences(of: "'", with: "''", options: .literal) + "'" } } diff --git a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoLiteral.swift b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoLiteral.swift index 6e47cd85b3..44d6376df0 100644 --- a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoLiteral.swift +++ b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoLiteral.swift @@ -55,7 +55,7 @@ public enum TrinoLiteral { } static func quoted(_ text: String) -> String { - "'" + text.replacingOccurrences(of: "'", with: "''") + "'" + "'" + text.replacingOccurrences(of: "'", with: "''", options: .literal) + "'" } static func isNumeric(_ text: String) -> Bool { diff --git a/apple/Packages/TableProCore/Sources/TableProWeaviateCore/WeaviateFilterBuilder.swift b/apple/Packages/TableProCore/Sources/TableProWeaviateCore/WeaviateFilterBuilder.swift index 6a29940eba..43d7f01a2e 100644 --- a/apple/Packages/TableProCore/Sources/TableProWeaviateCore/WeaviateFilterBuilder.swift +++ b/apple/Packages/TableProCore/Sources/TableProWeaviateCore/WeaviateFilterBuilder.swift @@ -302,15 +302,15 @@ public enum WeaviateFilterBuilder { static func escape(_ value: String) -> String { var result = "" - result.reserveCapacity(value.count) - for character in value { - switch character { + result.reserveCapacity(value.utf8.count) + for scalar in value.unicodeScalars { + switch scalar { case "\\": result += "\\\\" case "\"": result += "\\\"" case "\n": result += "\\n" case "\r": result += "\\r" case "\t": result += "\\t" - default: result.append(character) + default: result.unicodeScalars.append(scalar) } } return result diff --git a/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLLiteralSpellingTests.swift b/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLLiteralSpellingTests.swift index 1c0ddc2b95..f085ed43bb 100644 --- a/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLLiteralSpellingTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLLiteralSpellingTests.swift @@ -14,6 +14,12 @@ import Testing struct MySQLLiteralSpellingTests { private let session = MySQLLiteralSpelling.quoteDoubling + @Test("A quote a combining mark follows is doubled for a session that reads backslashes literally") + func quoteBeforeMarkIsDoubled() { + let escaped = session.escaped("x'\u{301} OR 1 = 1 #") + #expect(Array(escaped.unicodeScalars) == Array("x''\u{301} OR 1 = 1 #".unicodeScalars)) + } + @Test("Only a session that reports NO_BACKSLASH_ESCAPES loses backslash escapes") func spellingFollowsTheStatusFlag() { #expect(MySQLLiteralSpelling(noBackslashEscapes: true) == .quoteDoubling) diff --git a/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift b/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift index 97eb4169fc..8ecaa42442 100644 --- a/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift @@ -4,6 +4,12 @@ import Testing @testable import TableProMySQLCore struct MySQLObjectQueryTests { + @Test("A backtick a combining mark follows is still doubled", arguments: ["\u{301}", "\u{200C}", "\u{FE0F}"]) + func backtickBeforeMarkIsDoubled(mark: String) { + let quoted = MySQLObjectQueries.quoteIdentifier("t`\(mark);DROP DATABASE prod;#") + #expect(Array(quoted.unicodeScalars) == Array("`t``\(mark);DROP DATABASE prod;#`".unicodeScalars)) + } + @Test("The DDL statement is schema-qualified") func routineDefinitionIsQualified() { let sql = MySQLObjectQueries.routineDefinition(kind: "PROCEDURE", schema: "analytics", name: "cleanup") diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/MySQLGrantEscapingTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/MySQLGrantEscapingTests.swift index d0efb2e8e8..839bc12314 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/MySQLGrantEscapingTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/MySQLGrantEscapingTests.swift @@ -18,6 +18,14 @@ struct MySQLGrantEscapingTests { #expect(MySQLGrantPatternEscaping.escapeDatabasePattern(#"back\slash"#) == #"back\\slash"#) } + @Test("A wildcard a combining mark follows is still escaped", arguments: ["%", "_", "\\"]) + func escapesWildcardBeforeCombiningMark(wildcard: String) { + let escaped = MySQLGrantPatternEscaping.escapeDatabasePattern("db\(wildcard)\u{301}") + #expect(Array(escaped.unicodeScalars) == Array("db\\\(wildcard)\u{301}".unicodeScalars)) + #expect(MySQLGrantPatternEscaping.unescapeDatabasePattern(escaped).unicodeScalars + .elementsEqual("db\(wildcard)\u{301}".unicodeScalars)) + } + @Test("Names without wildcards are left alone") func leavesPlainNames() { #expect(MySQLGrantPatternEscaping.escapeDatabasePattern("analytics") == "analytics") diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginExportUtilitiesTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginExportUtilitiesTests.swift index 1112d89d74..8c1dcb4b10 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginExportUtilitiesTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginExportUtilitiesTests.swift @@ -48,6 +48,21 @@ struct PluginExportUtilitiesTests { #expect(parsed?.toString().unicodeScalars.elementsEqual(name.unicodeScalars) == true) } + @Test("A line break a combining mark follows does not end a SQL comment", + arguments: ["\n", "\r", "\r\n", "\u{85}", "\u{2028}", "\u{2029}"]) + func commentKeepsNoLineBreakBeforeACombiningMark(lineBreak: String) { + let cleaned = PluginExportUtilities.sanitizeForSQLComment("x\(lineBreak)\u{301}; DELETE FROM accounts;") + let breaks: Set = ["\n", "\r", "\u{85}", "\u{2028}", "\u{2029}"] + #expect(!cleaned.unicodeScalars.contains { breaks.contains($0) }) + #expect(cleaned.unicodeScalars.last == ";") + } + + @Test("A comment keeps a plain name as it is and drops comment markers") + func commentKeepsPlainName() { + #expect(PluginExportUtilities.sanitizeForSQLComment("orders") == "orders") + #expect(PluginExportUtilities.sanitizeForSQLComment("a/*b*/c--d\ne") == "abcd e") + } + @Test("A collection reached through getCollection has its separators escaped") func accessorEscapesSeparators() { #expect(MongoCollectionAccessor.expression(for: "a\u{2028}b") == "db.getCollection(\"a\\u2028b\")") diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift index 9364e0ca04..401abdc770 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift @@ -194,3 +194,44 @@ struct SqlStandardEscapeTests { #expect(driver.escapedParameterValue("a\0b") == "'ab'") } } + +// MARK: - Default quoting + +struct DefaultQuotingTests { + private let driver = SqlStandardStubDriver() + + private static func scalars(_ text: String) -> [Unicode.Scalar] { + Array(text.unicodeScalars) + } + + @Test("The default identifier quoter doubles a quote a combining mark follows", + arguments: ["\u{301}", "\u{200C}", "\u{FE0F}", "\u{FF9E}", "\u{1F3FB}"]) + func identifierQuoteBeforeMarkIsDoubled(mark: String) { + let quoted = driver.quoteIdentifier("t\"\(mark); DROP TABLE users; --") + #expect(Self.scalars(quoted) == Self.scalars("\"t\"\"\(mark); DROP TABLE users; --\"")) + } + + @Test("A table name made to close the row count subquery stays one identifier") + func rowCountTableNameStaysOneIdentifier() { + let quoted = driver.quoteIdentifier("sqlite_master\"\u{301}); DROP TABLE users; --") + #expect(Self.scalars(quoted) == Self.scalars("\"sqlite_master\"\"\u{301}); DROP TABLE users; --\"")) + } + + @Test("The default literal escape doubles a quote a combining mark follows") + func literalQuoteBeforeMarkIsDoubled() { + let escaped = driver.escapeStringLiteral("x'\u{301}); DROP TABLE users; --") + #expect(Self.scalars(escaped) == Self.scalars("x''\u{301}); DROP TABLE users; --")) + } + + @Test("A LIKE escape doubles a quote and escapes a backslash a combining mark follows") + func likeEscapeBeforeMark() { + let escaped = PluginSQLFilter.escapeForLike("a'\u{301}b\\\u{301}c%\u{301}") + #expect(Self.scalars(escaped) == Self.scalars("a''\u{301}b\\\\\u{301}c\\%\u{301}")) + } + + @Test("A regex metacharacter a combining mark follows is still escaped") + func regexMetacharacterBeforeMarkIsEscaped() { + let pattern = PluginSQLRegexPattern.pattern(matchingLiteral: ".\u{301}", anchoring: .exact, ignoresCase: true) + #expect(Self.scalars(pattern) == Self.scalars("(?i)^\\.\u{301}$")) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/LibPQStringConformanceTests.swift b/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/LibPQStringConformanceTests.swift index d6ee97b65d..73d9bb4ca5 100644 --- a/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/LibPQStringConformanceTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPostgreSQLCoreTests/LibPQStringConformanceTests.swift @@ -9,6 +9,15 @@ import Testing @testable import TableProPostgreSQLCore struct LibPQStringConformanceTests { + @Test("A quote or backslash a combining mark follows is still escaped", arguments: [true, false]) + func combiningMarkCannotHideAnEscape(standardConformingStrings: Bool) { + let value = "thanks'\u{301}); DROP TABLE victims; --\\\u{301}" + let escaped = LibPQStringConformance.escape(value, standardConformingStrings: standardConformingStrings) + let backslash = standardConformingStrings ? "\\" : "\\\\" + let expected = "thanks''\u{301}); DROP TABLE victims; --\(backslash)\u{301}" + #expect(Array(escaped.unicodeScalars) == Array(expected.unicodeScalars)) + } + @Test("Every session turns standard_conforming_strings on") func sessionSetupForcesStandardConformingStrings() { #expect(LibPQStringConformance.enableStatement == "SET standard_conforming_strings TO on") diff --git a/apple/Packages/TableProCore/Tests/TableProQueryTests/FilterSQLGeneratorTests.swift b/apple/Packages/TableProCore/Tests/TableProQueryTests/FilterSQLGeneratorTests.swift index 1e693abbaa..dd71cf5f56 100644 --- a/apple/Packages/TableProCore/Tests/TableProQueryTests/FilterSQLGeneratorTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProQueryTests/FilterSQLGeneratorTests.swift @@ -198,6 +198,19 @@ struct FilterSQLGeneratorTests { #expect(clause("code", "a\\' OR 1=1 -- ", dialect: mysql) == "WHERE `code` = 'a\\\\'' OR 1=1 -- '") } + @Test("A quote a combining mark follows is doubled", arguments: ["\u{301}", "\u{200C}", "\u{FE0F}", "\u{FF9E}"]) + func quoteBeforeCombiningMarkIsDoubled(mark: String) { + let sql = clause("code", "x'\(mark): DROP TABLE t -- ") + #expect(Array(sql.unicodeScalars) == Array("WHERE \"code\" = 'x''\(mark): DROP TABLE t -- '".unicodeScalars)) + } + + @Test("A closing quote a combining mark follows is doubled in a column name") + func identifierQuoteBeforeCombiningMarkIsDoubled() { + let mssql = makeDialect(quote: "[") + let sql = clause("c]\u{FF9E}x", "1", dialect: mssql) + #expect(Array(sql.unicodeScalars).starts(with: Array("WHERE [c]]\u{FF9E}x]".unicodeScalars))) + } + // Snowflake takes an ESCAPE clause for LIKE and still reads `\'` inside a literal as a quote. @Test("A LIKE pattern doubles a backslash where the engine reads it as an escape, whatever the LIKE style") func likeDoublesBackslashForTheLiteral() { diff --git a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRowWriter.swift b/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRowWriter.swift index 0ea529431f..275ed881fc 100644 --- a/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRowWriter.swift +++ b/apple/Packages/TableProMacCore/Sources/TableProMCP/CommandLine/CLIRowWriter.swift @@ -78,9 +78,10 @@ public enum CLIRowWriter { // NULL is an empty field and an empty string is "", so the two stay apart. static func csvField(_ value: JsonValue) -> String { guard let text = text(of: value) else { return "" } - let needsQuotes = text.isEmpty || text.contains(where: { $0 == "," || $0 == "\"" || $0.isNewline }) + let needsQuotes = text.isEmpty + || text.unicodeScalars.contains { $0 == "," || $0 == "\"" || CharacterSet.newlines.contains($0) } guard needsQuotes else { return text } - return "\"" + text.replacingOccurrences(of: "\"", with: "\"\"") + "\"" + return "\"" + text.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) + "\"" } static func tsvField(_ value: JsonValue) -> String { diff --git a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCellFormatting.swift b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCellFormatting.swift index cf191b9ed9..c40818145b 100644 --- a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCellFormatting.swift +++ b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCellFormatting.swift @@ -126,8 +126,8 @@ public enum OracleCellFormatting { /// A BFILE spelled as the call that creates it, so the text names the file and can be written back. public static func bfileText(directory: String, fileName: String) -> String { - "BFILENAME('\(directory.replacingOccurrences(of: "'", with: "''"))', " - + "'\(fileName.replacingOccurrences(of: "'", with: "''"))')" + "BFILENAME('\(directory.replacingOccurrences(of: "'", with: "''", options: .literal))', " + + "'\(fileName.replacingOccurrences(of: "'", with: "''", options: .literal))')" } public static func hexEncode(_ bytes: [UInt8]) -> String { diff --git a/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift b/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift index 31c323c648..aaf528c840 100644 --- a/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift +++ b/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift @@ -300,11 +300,11 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func quoteIdentifier(_ name: String) -> String { - "\"\(name.replacingOccurrences(of: "\"", with: "\"\""))\"" + "\"\(name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } func escapeStringLiteral(_ value: String) -> String { - value.replacingOccurrences(of: "'", with: "''") + value.replacingOccurrences(of: "'", with: "''", options: .literal) } func execute(query: String) async throws -> PluginQueryResult { diff --git a/apple/Plugins/BigQueryDriverPlugin/BigQueryQueryBuilder.swift b/apple/Plugins/BigQueryDriverPlugin/BigQueryQueryBuilder.swift index 8375ae6632..53011eccc6 100644 --- a/apple/Plugins/BigQueryDriverPlugin/BigQueryQueryBuilder.swift +++ b/apple/Plugins/BigQueryDriverPlugin/BigQueryQueryBuilder.swift @@ -479,7 +479,7 @@ internal struct BigQueryQueryBuilder { } private static func foldedLiteral(_ literal: String, folding: PluginSQLCaseFolding) -> String { - guard folding.foldsComparisonOperands, literal.hasPrefix("'") else { return literal } + guard folding.foldsComparisonOperands, literal.unicodeScalars.first == "'" else { return literal } return folding.fold(literal) } diff --git a/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift b/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift index fcece7b015..6f11d384b2 100644 --- a/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift +++ b/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift @@ -169,9 +169,9 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi if options.convertLineBreakToSpace { processed = processed - .replacingOccurrences(of: "\r\n", with: " ") - .replacingOccurrences(of: "\r", with: " ") - .replacingOccurrences(of: "\n", with: " ") + .replacingOccurrences(of: "\r\n", with: " ", options: .literal) + .replacingOccurrences(of: "\r", with: " ", options: .literal) + .replacingOccurrences(of: "\n", with: " ", options: .literal) } if options.decimalFormat == .comma { @@ -195,7 +195,7 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi /// 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)) - guard originalHadLineBreaks, options.quoteHandling == .asNeeded, !escaped.hasPrefix("\"") else { + guard originalHadLineBreaks, options.quoteHandling == .asNeeded, escaped.unicodeScalars.first != "\"" else { return escaped } return "\"\(escaped.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraBrowseStatement.swift b/apple/Plugins/CassandraDriverPlugin/CassandraBrowseStatement.swift index a3636fc18d..90009c0d60 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraBrowseStatement.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraBrowseStatement.swift @@ -130,7 +130,7 @@ enum CassandraBrowseRenderer { } static func quote(_ identifier: String) -> String { - "\"" + identifier.replacingOccurrences(of: "\"", with: "\"\"") + "\"" + "\"" + identifier.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) + "\"" } static func qualifiedTable(keyspace: String?, table: String) -> String { diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift b/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift index 9643edd19b..d27d023c5f 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift @@ -1072,7 +1072,7 @@ actor CassandraConnectionActor { } private func escapeIdentifier(_ value: String) -> String { - value.replacingOccurrences(of: "\"", with: "\"\"") + value.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) } private static func apply(_ consistency: CassandraConsistency?, to statement: OpaquePointer) throws { diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift index 933d4af78b..534dd57e86 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift @@ -843,11 +843,11 @@ internal final class CassandraPluginDriver: PluginDatabaseDriver, @unchecked Sen } private func escapeIdentifier(_ value: String) -> String { - value.replacingOccurrences(of: "\"", with: "\"\"") + value.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) } private func escapeSingleQuote(_ value: String) -> String { - value.replacingOccurrences(of: "'", with: "''") + value.replacingOccurrences(of: "'", with: "''", options: .literal) } private func stripTrailingSemicolon(_ query: String) -> String { diff --git a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift index 3a26349f24..b1b8c66839 100644 --- a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift +++ b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift @@ -545,7 +545,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked ) do { - let escapedId = queryId.replacingOccurrences(of: "'", with: "''") + let escapedId = queryId.replacingOccurrences(of: "'", with: "''", options: .literal) let request = try buildRequest( query: "KILL QUERY WHERE query_id = '\(escapedId)'", database: "", diff --git a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Routines.swift b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Routines.swift index 4ecba38418..41b3c8816b 100644 --- a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Routines.swift +++ b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Routines.swift @@ -11,8 +11,8 @@ import TableProPluginKit nonisolated public enum ClickHouseObjectQueries { public static func escapeLiteral(_ value: String) -> String { value - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "'", with: "\\'") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "'", with: "\\'", options: .literal) } public static func quoteIdentifier(_ value: String) -> String { diff --git a/apple/Plugins/CloudflareD1DriverPlugin/CloudflareD1DefaultValue.swift b/apple/Plugins/CloudflareD1DriverPlugin/CloudflareD1DefaultValue.swift index e654315c85..141481b8af 100644 --- a/apple/Plugins/CloudflareD1DriverPlugin/CloudflareD1DefaultValue.swift +++ b/apple/Plugins/CloudflareD1DriverPlugin/CloudflareD1DefaultValue.swift @@ -58,5 +58,5 @@ internal func cloudflareD1LooksLikeExpression(_ value: String) -> Bool { } internal func cloudflareD1DefaultLiteralEscape(_ value: String) -> String { - value.replacingOccurrences(of: "\'", with: "\'\'") + value.replacingOccurrences(of: "\'", with: "\'\'", options: .literal) } diff --git a/apple/Plugins/CloudflareD1DriverPlugin/CloudflareD1PluginDriver.swift b/apple/Plugins/CloudflareD1DriverPlugin/CloudflareD1PluginDriver.swift index 1a6ba5844c..5c1212f9fb 100644 --- a/apple/Plugins/CloudflareD1DriverPlugin/CloudflareD1PluginDriver.swift +++ b/apple/Plugins/CloudflareD1DriverPlugin/CloudflareD1PluginDriver.swift @@ -409,8 +409,7 @@ final class CloudflareD1PluginDriver: PluginDatabaseDriver, @unchecked Sendable } func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata { - let safeTableName = table.replacingOccurrences(of: "\"", with: "\"\"") - let countQuery = "SELECT COUNT(*) FROM (SELECT 1 FROM \"\(safeTableName)\" LIMIT 100001)" + let countQuery = "SELECT COUNT(*) FROM (SELECT 1 FROM \(quoteIdentifier(table)) LIMIT 100001)" let countResult = try await execute(query: countQuery) let rowCount: Int64? = { guard let row = countResult.rows.first, let countStr = row.first else { return nil } @@ -509,20 +508,6 @@ final class CloudflareD1PluginDriver: PluginDatabaseDriver, @unchecked Sendable lock.withLock { httpClient?.databaseId = resolvedUuid } } - // MARK: - Identifier Quoting - - func quoteIdentifier(_ name: String) -> String { - let escaped = name.replacingOccurrences(of: "\"", with: "\"\"") - return "\"\(escaped)\"" - } - - func escapeStringLiteral(_ value: String) -> String { - var result = value - result = result.replacingOccurrences(of: "'", with: "''") - result = result.replacingOccurrences(of: "\0", with: "") - return result - } - func castColumnToText(_ column: String) -> String { "CAST(\(column) AS TEXT)" } diff --git a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver.swift b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver.swift index 75b963207a..48f0267048 100644 --- a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver.swift +++ b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver.swift @@ -46,7 +46,7 @@ final class DynamoDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func escapeStringLiteral(_ value: String) -> String { - value.replacingOccurrences(of: "'", with: "''") + value.replacingOccurrences(of: "'", with: "''", options: .literal) } // MARK: - Connection diff --git a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBStatement.swift b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBStatement.swift index 6689902c3b..80d329c704 100644 --- a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBStatement.swift +++ b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBStatement.swift @@ -141,7 +141,7 @@ enum DynamoDBStatement: Sendable, Equatable { } static func quote(_ identifier: String) -> String { - "\"\(identifier.replacingOccurrences(of: "\"", with: "\"\""))\"" + "\"\(identifier.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } // MARK: - Parsing helpers diff --git a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBWriteStatements.swift b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBWriteStatements.swift index e925e2f8c6..e77e3cd73a 100644 --- a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBWriteStatements.swift +++ b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBWriteStatements.swift @@ -142,7 +142,7 @@ struct DynamoDBWriteStatements { } static func literal(_ name: String) -> String { - "'\(name.replacingOccurrences(of: "'", with: "''"))'" + "'\(name.replacingOccurrences(of: "'", with: "''", options: .literal))'" } } diff --git a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchQueryBuilder.swift b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchQueryBuilder.swift index 705073ec5f..524b5c302b 100644 --- a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchQueryBuilder.swift +++ b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchQueryBuilder.swift @@ -609,8 +609,8 @@ struct ElasticsearchQueryBuilder { private static func escapeWildcard(_ value: String) -> String { value - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "*", with: "\\*") - .replacingOccurrences(of: "?", with: "\\?") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "*", with: "\\*", options: .literal) + .replacingOccurrences(of: "?", with: "\\?", options: .literal) } } diff --git a/apple/Plugins/FormatPluginTests/SQLExportCraftedNameTests.swift b/apple/Plugins/FormatPluginTests/SQLExportCraftedNameTests.swift new file mode 100644 index 0000000000..1766fe0089 --- /dev/null +++ b/apple/Plugins/FormatPluginTests/SQLExportCraftedNameTests.swift @@ -0,0 +1,92 @@ +// +// SQLExportCraftedNameTests.swift +// FormatPluginTests +// + +import Foundation +import TableProPluginKit +import Testing + +struct SQLExportCraftedNameTests { + private final class StubExportDataSource: PluginExportDataSource, @unchecked Sendable { + let databaseTypeId = "PostgreSQL" + let sequenceName: String + let typeName: String + + init(sequenceName: String = "orders_id_seq", typeName: String = "mood") { + self.sequenceName = sequenceName + self.typeName = typeName + } + + func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { + AsyncThrowingStream { continuation in + continuation.yield(.header(PluginStreamHeader(columns: ["id"], columnTypeNames: ["INTEGER"]))) + continuation.finish() + } + } + + func fetchTableDDL(table: String, databaseName: String) async throws -> String { + "CREATE TABLE \(quoteIdentifier(table)) (id INTEGER)" + } + + func fetchDependentSequences(table: String, databaseName: String) async throws -> [PluginSequenceInfo] { + [PluginSequenceInfo(name: sequenceName, ddl: "CREATE SEQUENCE \(quoteIdentifier(sequenceName))")] + } + + func fetchDependentTypes(table: String, databaseName: String) async throws -> [PluginEnumTypeInfo] { + [PluginEnumTypeInfo(name: typeName, labels: ["happy"])] + } + + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func quoteIdentifier(_ identifier: String) -> String { + "\"\(identifier.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" + } + + func escapeStringLiteral(_ value: String) -> String { + value.replacingOccurrences(of: "'", with: "''", options: .literal) + } + + func fetchApproximateRowCount(table: String, databaseName: String) async throws -> Int? { nil } + } + + private func table(_ name: String) -> PluginExportTable { + PluginExportTable( + name: name, + databaseName: "", + tableType: PluginExportObjectKind.table.rawValue, + optionValues: [true, true, true], + schema: nil, + kind: .table + ) + } + + private func lines(of dump: String) -> [[Unicode.Scalar]] { + dump.unicodeScalars.split(separator: "\n").map(Array.init) + } + + @Test("A sequence or enum type name with a quote a combining mark follows stays one identifier") + func dependentNamesStayOneIdentifier() async throws { + let source = StubExportDataSource( + sequenceName: "s\"\u{301}; DROP TABLE victims; --", + typeName: "e\"\u{301}; DROP TABLE victims; --" + ) + let dump = try await SQLExportHarness.shared.dump(tables: [table("orders")], dataSource: source).text + let sequenceDrop = Array("DROP SEQUENCE IF EXISTS \"s\"\"\u{301}; DROP TABLE victims; --\"".unicodeScalars) + let typeDrop = Array("DROP TYPE IF EXISTS \"e\"\"\u{301}; DROP TABLE victims; --\"".unicodeScalars) + + #expect(lines(of: dump).contains { $0.starts(with: sequenceDrop) }) + #expect(lines(of: dump).contains { $0.starts(with: typeDrop) }) + } + + @Test("A table name with a line break a combining mark follows stays inside its comment") + func tableCommentStaysOnOneLine() async throws { + let dump = try await SQLExportHarness.shared.dump( + tables: [table("x\n\u{301}; DELETE FROM accounts;")], dataSource: StubExportDataSource() + ).text + + #expect(lines(of: dump).contains(Array("-- Table: x \u{301}; DELETE FROM accounts;".unicodeScalars))) + } +} diff --git a/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver.swift b/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver.swift index a11664a951..85462fc501 100644 --- a/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver.swift +++ b/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver.swift @@ -226,8 +226,8 @@ final class KafkaPluginDriver: PluginDatabaseDriver, @unchecked Sendable { /// as further clauses. func escapeStringLiteral(_ value: String) -> String { value - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "\"", with: "\\\"") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "\"", with: "\\\"", options: .literal) } // MARK: - Browse diff --git a/apple/Plugins/KafkaDriverPlugin/KafkaQL.swift b/apple/Plugins/KafkaDriverPlugin/KafkaQL.swift index f911516488..e688d8e8db 100644 --- a/apple/Plugins/KafkaDriverPlugin/KafkaQL.swift +++ b/apple/Plugins/KafkaDriverPlugin/KafkaQL.swift @@ -398,8 +398,8 @@ enum KafkaQL { /// let the rest of the value parse as further clauses. static func quote(_ value: String) -> String { let escaped = value - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "\"", with: "\\\"") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "\"", with: "\\\"", options: .literal) return "\"\(escaped)\"" } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBQueryBuilder.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBQueryBuilder.swift index bc140658c4..2729efa23b 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBQueryBuilder.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBQueryBuilder.swift @@ -499,14 +499,14 @@ nonisolated struct MongoDBQueryBuilder { } private func escapeRegexChars(_ str: String) -> String { - let specialChars = "\\^$.|?*+()[]{}" - var result = "" - for char in str { - if specialChars.contains(char) { + let specialChars = "\\^$.|?*+()[]{}".unicodeScalars + var result = String.UnicodeScalarView() + for scalar in str.unicodeScalars { + if specialChars.contains(scalar) { result.append("\\") } - result.append(char) + result.append(scalar) } - return result + return String(result) } } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBShellLiteral.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBShellLiteral.swift index 6d05772e9a..2e0a065e5e 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBShellLiteral.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBShellLiteral.swift @@ -34,7 +34,7 @@ nonisolated enum MongoDBShellLiteral { /// `true`, `false` or `null` is written as the string it spells, so no text reaches the /// statement outside a literal. private static func scalar(_ token: String) -> String { - if token.hasPrefix("\"") { + if token.unicodeScalars.first == "\"" { return MongoScriptJson.jsonString(MongoScriptJson.decodedString(token) ?? token) } let isLiteral = ["true", "false", "null"].contains(token) diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoScriptJson.swift b/apple/Plugins/MongoDBDriverPlugin/MongoScriptJson.swift index 2ae3e0a044..3d83a74c5b 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoScriptJson.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoScriptJson.swift @@ -59,7 +59,7 @@ nonisolated enum MongoScriptJson { /// The string a member's value text spells, escapes decoded, or nil when the value is not a string. static func decodedString(_ valueJson: String) -> String? { let trimmed = valueJson.trimmingCharacters(in: .whitespacesAndNewlines) - guard trimmed.hasPrefix("\"") else { return nil } + guard trimmed.unicodeScalars.first == "\"" else { return nil } return try? JSONDecoder().decode(String.self, from: Data(trimmed.utf8)) } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoShellCommandLine.swift b/apple/Plugins/MongoDBDriverPlugin/MongoShellCommandLine.swift index 54b7293e0c..d170dcf15b 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoShellCommandLine.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoShellCommandLine.swift @@ -43,6 +43,6 @@ nonisolated enum MongoShellCommandLine { } private static func quoted(_ value: String) -> String { - "\"\(value.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\""))\"" + "\"\(value.replacingOccurrences(of: "\\", with: "\\\\", options: .literal).replacingOccurrences(of: "\"", with: "\\\"", options: .literal))\"" } } diff --git a/apple/Plugins/MySQLDriverPlugin/DatabendCatalog.swift b/apple/Plugins/MySQLDriverPlugin/DatabendCatalog.swift index 4f55e85d8e..c0cdf87602 100644 --- a/apple/Plugins/MySQLDriverPlugin/DatabendCatalog.swift +++ b/apple/Plugins/MySQLDriverPlugin/DatabendCatalog.swift @@ -9,10 +9,10 @@ import TableProPluginKit internal enum DatabendCatalog { static func quoteIdentifier(_ name: String) -> String { - guard name.contains("`") else { return "`\(name)`" } + guard name.unicodeScalars.contains("`") else { return "`\(name)`" } let escaped = name - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "\"", with: "\"\"") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "\"", with: "\"\"", options: .literal) return "\"\(escaped)\"" } diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLColumnDefinitionSQL.swift b/apple/Plugins/MySQLDriverPlugin/MySQLColumnDefinitionSQL.swift index 378ecb0af9..6ed12a388c 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLColumnDefinitionSQL.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLColumnDefinitionSQL.swift @@ -8,7 +8,7 @@ import TableProMySQLCore import TableProPluginKit internal func mysqlQuoteIdentifier(_ name: String) -> String { - let escaped = name.replacingOccurrences(of: "`", with: "``") + let escaped = name.replacingOccurrences(of: "`", with: "``", options: .literal) return "`\(escaped)`" } diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+CreateDatabase.swift b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+CreateDatabase.swift index 37d1147aee..9d0a0e4c44 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+CreateDatabase.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+CreateDatabase.swift @@ -119,7 +119,7 @@ extension MySQLPluginDriver { } } - let escapedName = request.name.replacingOccurrences(of: "`", with: "``") + let escapedName = request.name.replacingOccurrences(of: "`", with: "``", options: .literal) var query = "CREATE DATABASE `\(escapedName)` CHARACTER SET \(charset)" if let collation = collationValue { query += " COLLATE \(collation)" diff --git a/apple/Plugins/MySQLDriverTests/DatabendCatalogTests.swift b/apple/Plugins/MySQLDriverTests/DatabendCatalogTests.swift index 671690a1c1..56f8cb9976 100644 --- a/apple/Plugins/MySQLDriverTests/DatabendCatalogTests.swift +++ b/apple/Plugins/MySQLDriverTests/DatabendCatalogTests.swift @@ -17,6 +17,12 @@ struct DatabendCatalogTests { #expect(DatabendCatalog.quoteIdentifier("t`u\\") == "\"t`u\\\\\"") } + @Test("A backtick a combining mark follows still switches to double quotes") + func backtickBeforeCombiningMarkSwitchesQuotes() { + let quoted = DatabendCatalog.quoteIdentifier("t`\u{301} UNION SELECT 1 -- ") + #expect(Array(quoted.unicodeScalars) == Array("\"t`\u{301} UNION SELECT 1 -- \"".unicodeScalars)) + } + @Test("A system.columns row becomes the column, default and comment included") func parsesSystemColumnsRow() throws { let row: [PluginCellValue] = [.text("label"), .text("VARCHAR"), .text("DEFAULT"), .text("'x'"), .text("YES"), .text("cmt")] diff --git a/apple/Plugins/MySQLDriverTests/MySQLColumnDefinitionSQLTests.swift b/apple/Plugins/MySQLDriverTests/MySQLColumnDefinitionSQLTests.swift index b10038bb3a..8233f967ae 100644 --- a/apple/Plugins/MySQLDriverTests/MySQLColumnDefinitionSQLTests.swift +++ b/apple/Plugins/MySQLDriverTests/MySQLColumnDefinitionSQLTests.swift @@ -12,6 +12,12 @@ import TableProPluginKit import Testing struct MySQLColumnDefinitionSQLTests { + @Test("A backtick a combining mark follows is still doubled", arguments: ["\u{301}", "\u{200C}", "\u{FE0F}"]) + func backtickBeforeCombiningMark(mark: String) { + let quoted = mysqlQuoteIdentifier("a`\(mark) UNION SELECT attacker_function() -- x") + #expect(Array(quoted.unicodeScalars) == Array("`a``\(mark) UNION SELECT attacker_function() -- x`".unicodeScalars)) + } + private func timestampColumn( dataType: String = "TIMESTAMP", defaultValue: String? = nil, diff --git a/apple/Plugins/OracleDriverPlugin/OracleBrowseSQL.swift b/apple/Plugins/OracleDriverPlugin/OracleBrowseSQL.swift index 6119402485..635dd645b5 100644 --- a/apple/Plugins/OracleDriverPlugin/OracleBrowseSQL.swift +++ b/apple/Plugins/OracleDriverPlugin/OracleBrowseSQL.swift @@ -41,7 +41,7 @@ internal enum OracleBrowseSQL { quoteIdentifier: quote, escapeTypedValue: filterValue, regexCondition: { quoted, value, ignoresCase in - let pattern = value.replacingOccurrences(of: "'", with: "''") + let pattern = value.replacingOccurrences(of: "'", with: "''", options: .literal) guard ignoresCase else { return "REGEXP_LIKE(\(quoted), '\(pattern)')" } return "REGEXP_LIKE(\(quoted), '\(pattern)', 'i')" } @@ -66,7 +66,7 @@ internal enum OracleBrowseSQL { kind: kind, trueLiteral: nil, falseLiteral: nil, - quote: { "'\($0.replacingOccurrences(of: "'", with: "''"))'" } + quote: { "'\($0.replacingOccurrences(of: "'", with: "''", options: .literal))'" } ) } @@ -100,7 +100,7 @@ internal enum OracleBrowseSQL { } static func quote(_ identifier: String) -> String { - "\"\(identifier.replacingOccurrences(of: "\"", with: "\"\""))\"" + "\"\(identifier.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } static func qualifiedName(schema: String?, table: String) -> String { diff --git a/apple/Plugins/OracleDriverPlugin/OracleIndexStatements.swift b/apple/Plugins/OracleDriverPlugin/OracleIndexStatements.swift index 87ba154b4b..1c21dda35e 100644 --- a/apple/Plugins/OracleDriverPlugin/OracleIndexStatements.swift +++ b/apple/Plugins/OracleDriverPlugin/OracleIndexStatements.swift @@ -18,8 +18,8 @@ internal enum OracleIndexStatements { /// carrying its `SYS_NC` shadow column instead of its expression, so it is left out of the dump /// rather than written wrong. internal static func query(schema: String, table: String) -> String { - let owner = schema.replacingOccurrences(of: "'", with: "''") - let tableName = table.replacingOccurrences(of: "'", with: "''") + let owner = schema.replacingOccurrences(of: "'", with: "''", options: .literal) + let tableName = table.replacingOccurrences(of: "'", with: "''", options: .literal) return """ SELECT i.INDEX_NAME, i.UNIQUENESS, i.INDEX_TYPE, ic.COLUMN_NAME, ic.DESCEND FROM SYS.ALL_INDEXES i diff --git a/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift b/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift index 1a7b758383..39bd94c3e1 100644 --- a/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift +++ b/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift @@ -609,7 +609,7 @@ final class OraclePluginDriver: PluginDatabaseDriver, @unchecked Sendable { var replacesDefinitionsInPlace: Bool { true } func createTriggerTemplate(table: String, schema: String?) -> String? { - let quotedTable = "\"\(table.replacingOccurrences(of: "\"", with: "\"\""))\"" + let quotedTable = "\"\(table.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" return """ CREATE OR REPLACE TRIGGER \("\"TRIGGER_NAME\"") BEFORE INSERT ON \(quotedTable) diff --git a/apple/Plugins/OracleDriverPlugin/OracleRowWriter.swift b/apple/Plugins/OracleDriverPlugin/OracleRowWriter.swift index 1ba8facbb3..a60e5e9af3 100644 --- a/apple/Plugins/OracleDriverPlugin/OracleRowWriter.swift +++ b/apple/Plugins/OracleDriverPlugin/OracleRowWriter.swift @@ -289,7 +289,7 @@ internal struct OracleRowWriter { } static func quote(_ name: String) -> String { - "\"\(name.replacingOccurrences(of: "\"", with: "\"\""))\"" + "\"\(name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } private static func serverOwnedReason(_ column: String) -> String { diff --git a/apple/Plugins/ParquetExportPlugin/DuckDBStagingDatabase.swift b/apple/Plugins/ParquetExportPlugin/DuckDBStagingDatabase.swift index 0641811c70..1cd0f85ebd 100644 --- a/apple/Plugins/ParquetExportPlugin/DuckDBStagingDatabase.swift +++ b/apple/Plugins/ParquetExportPlugin/DuckDBStagingDatabase.swift @@ -136,11 +136,11 @@ final class DuckDBStagingDatabase { } private static func quote(_ identifier: String) -> String { - "\"\(identifier.replacingOccurrences(of: "\"", with: "\"\""))\"" + "\"\(identifier.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } private static func escapeLiteral(_ value: String) -> String { - value.replacingOccurrences(of: "'", with: "''") + value.replacingOccurrences(of: "'", with: "''", options: .literal) } } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaQueries.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaQueries.swift index b4d13e0707..317f79e677 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaQueries.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLSchemaQueries.swift @@ -206,7 +206,7 @@ enum PostgreSQLSchemaQueries { } private static func quotedSchemaIdentifier(_ schema: String) -> String { - "\"\(schema.replacingOccurrences(of: "\"", with: "\"\""))\"" + "\"\(schema.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } static func collationList(capabilities: PostgreSQLCapabilities) -> String { diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLVersionedStatements.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLVersionedStatements.swift index 19ad34ba37..e1c1f827da 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLVersionedStatements.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLVersionedStatements.swift @@ -195,7 +195,7 @@ internal enum PostgreSQLVersionedStatements { private static func dollarQuoteTag(avoiding body: String) -> String { var label = "tablepro" - while body.contains("$\(label)$") { + while body.range(of: "$\(label)$", options: .literal) != nil { label += "_" } return "$\(label)$" diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLVersionedStatementsTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLVersionedStatementsTests.swift index 7ba7e8b292..87918f313b 100644 --- a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLVersionedStatementsTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLVersionedStatementsTests.swift @@ -111,6 +111,13 @@ struct PostgreSQLVersionedStatementsTests { #expect(statement.hasSuffix(" $tablepro_$")) } + @Test("A tag a combining mark follows inside the name still counts as a collision") + func createSchemaLegacyAvoidsTagBeforeCombiningMark() { + let statement = PostgreSQLVersionedStatements.createSchema("s$tablepro$\u{301}", capabilities: Self.v91) + #expect(statement.hasPrefix("DO $tablepro_$ ")) + #expect(statement.hasSuffix(" $tablepro_$")) + } + @Test("REINDEX DATABASE names the current database where the server requires it") func reindexDatabase() { #expect(PostgreSQLVersionedStatements.reindexDatabase(currentDatabase: "app", verbose: false, capabilities: Self.v91) diff --git a/apple/Plugins/SQLExportPlugin/SQLExportPlugin.swift b/apple/Plugins/SQLExportPlugin/SQLExportPlugin.swift index a7bcae450a..a468e63c01 100644 --- a/apple/Plugins/SQLExportPlugin/SQLExportPlugin.swift +++ b/apple/Plugins/SQLExportPlugin/SQLExportPlugin.swift @@ -542,7 +542,7 @@ final class SQLExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi table: table.name, databaseName: table.databaseName) for seq in sequences where !emittedSequenceNames.contains(seq.name) { emittedSequenceNames.insert(seq.name) - let quotedName = "\"\(seq.name.replacingOccurrences(of: "\"", with: "\"\""))\"" + let quotedName = "\"\(seq.name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" if optionValue(table, at: 1) { try writer.write( "DROP SEQUENCE IF EXISTS \(quotedName)\(cascadeClause(dataSource));\(statementEnd)") @@ -562,7 +562,7 @@ final class SQLExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi table: table.name, databaseName: table.databaseName) for enumType in enumTypes where !emittedTypeNames.contains(enumType.name) { emittedTypeNames.insert(enumType.name) - let quotedName = "\"\(enumType.name.replacingOccurrences(of: "\"", with: "\"\""))\"" + let quotedName = "\"\(enumType.name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" if optionValue(table, at: 1) { try writer.write( "DROP TYPE IF EXISTS \(quotedName)\(cascadeClause(dataSource));\(statementEnd)") diff --git a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeSQL.swift b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeSQL.swift index 9d1828f99a..7b6eb1197c 100644 --- a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeSQL.swift +++ b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeSQL.swift @@ -28,7 +28,7 @@ public enum SnowflakeSQL { /// it matches objects the caller never named. public static func escapeLikePattern(_ value: String) -> String { escapeLiteral(value) - .replacingOccurrences(of: "_", with: "\\\\_") - .replacingOccurrences(of: "%", with: "\\\\%") + .replacingOccurrences(of: "_", with: "\\\\_", options: .literal) + .replacingOccurrences(of: "%", with: "\\\\%", options: .literal) } } diff --git a/apple/Plugins/SurrealDBDriverPlugin/SurrealQL.swift b/apple/Plugins/SurrealDBDriverPlugin/SurrealQL.swift index abdd65b18e..bdfd1a110a 100644 --- a/apple/Plugins/SurrealDBDriverPlugin/SurrealQL.swift +++ b/apple/Plugins/SurrealDBDriverPlugin/SurrealQL.swift @@ -13,9 +13,9 @@ public enum SurrealQL { public static func escapeStringLiteral(_ value: String) -> String { var out = "" - out.reserveCapacity(value.count) - for character in value { - switch character { + out.reserveCapacity(value.utf8.count) + for scalar in value.unicodeScalars { + switch scalar { case "\\": out += "\\\\" case "'": @@ -23,7 +23,7 @@ public enum SurrealQL { case "\u{00}": continue default: - out.append(character) + out.unicodeScalars.append(scalar) } } return out @@ -112,8 +112,8 @@ public enum SurrealQL { } private static func escapeBackticks(_ text: String) -> String { - text.replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "`", with: "\\`") + text.replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "`", with: "\\`", options: .literal) } private static func unescapeBackticks(_ text: String) -> String { diff --git a/apple/Plugins/TableProPluginKit/MongoShellValueTranslator.swift b/apple/Plugins/TableProPluginKit/MongoShellValueTranslator.swift index 116c6bda99..deb13d5614 100644 --- a/apple/Plugins/TableProPluginKit/MongoShellValueTranslator.swift +++ b/apple/Plugins/TableProPluginKit/MongoShellValueTranslator.swift @@ -329,7 +329,9 @@ public enum MongoShellValueTranslator { } private static func escaped(_ value: String) -> String { - value.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") + value + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "\"", with: "\\\"", options: .literal) } private static func hexData(_ value: String) -> Data? { diff --git a/apple/Plugins/TableProPluginKit/MySQLGrantPatternEscaping.swift b/apple/Plugins/TableProPluginKit/MySQLGrantPatternEscaping.swift index 8100a36988..3ec269b527 100644 --- a/apple/Plugins/TableProPluginKit/MySQLGrantPatternEscaping.swift +++ b/apple/Plugins/TableProPluginKit/MySQLGrantPatternEscaping.swift @@ -11,41 +11,37 @@ import Foundation public enum MySQLGrantPatternEscaping { public static func escapeDatabasePattern(_ value: String) -> String { - var result = "" - result.reserveCapacity(value.count) - for character in value { - switch character { - case "\\", "_", "%": - result.append("\\") - result.append(character) - default: - result.append(character) + var result = String.UnicodeScalarView() + for scalar in value.unicodeScalars { + switch scalar { + case "\\", "_", "%": result.append("\\") + default: break } + result.append(scalar) } - return result + return String(result) } public static func unescapeDatabasePattern(_ value: String) -> String { - var result = "" - result.reserveCapacity(value.count) + var result = String.UnicodeScalarView() var isEscaped = false - for character in value { + for scalar in value.unicodeScalars { if isEscaped { - result.append(character) + result.append(scalar) isEscaped = false continue } - if character == "\\" { + if scalar == "\\" { isEscaped = true continue } - result.append(character) + result.append(scalar) } if isEscaped { result.append("\\") } - return result + return String(result) } } diff --git a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift index 4af87436c1..ca8846ef83 100644 --- a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift +++ b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift @@ -1412,7 +1412,7 @@ public extension PluginDatabaseDriver { func defaultExportQuery(table: String, schema: String?) -> String? { defaultExportQuery(table: table) } func quoteIdentifier(_ name: String) -> String { - let escaped = name.replacingOccurrences(of: "\"", with: "\"\"") + let escaped = name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) return "\"\(escaped)\"" } @@ -1731,10 +1731,10 @@ public extension PluginDatabaseDriver { public enum PluginSQLFilter { public static func escapeForLike(_ text: String) -> String { text - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "%", with: "\\%") - .replacingOccurrences(of: "_", with: "\\_") - .replacingOccurrences(of: "'", with: "''") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "%", with: "\\%", options: .literal) + .replacingOccurrences(of: "_", with: "\\_", options: .literal) + .replacingOccurrences(of: "'", with: "''", options: .literal) } public static func buildOrderByClause( diff --git a/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift b/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift index a2ed3958f6..eced8b61d0 100644 --- a/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift +++ b/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift @@ -93,13 +93,19 @@ public enum PluginExportUtilities { } public static func sanitizeForSQLComment(_ name: String) -> String { - var result = name - result = result.replacingOccurrences(of: "\n", with: " ") - result = result.replacingOccurrences(of: "\r", with: " ") - result = result.replacingOccurrences(of: "/*", with: "") - result = result.replacingOccurrences(of: "*/", with: "") - result = result.replacingOccurrences(of: "--", with: "") - return result + var line = String.UnicodeScalarView() + for scalar in name.unicodeScalars { + switch scalar { + case "\n", "\r", "\u{85}", "\u{2028}", "\u{2029}": + line.append(" ") + default: + line.append(scalar) + } + } + return String(line) + .replacingOccurrences(of: "/*", with: "", options: .literal) + .replacingOccurrences(of: "*/", with: "", options: .literal) + .replacingOccurrences(of: "--", with: "", options: .literal) } public static func isNumericColumnType(_ typeName: String) -> Bool { diff --git a/apple/Plugins/TableProPluginKit/PluginRowWriters.swift b/apple/Plugins/TableProPluginKit/PluginRowWriters.swift index 969d1b0380..e92617afb2 100644 --- a/apple/Plugins/TableProPluginKit/PluginRowWriters.swift +++ b/apple/Plugins/TableProPluginKit/PluginRowWriters.swift @@ -68,9 +68,9 @@ public enum PluginRowWriters { var processed = value if options.flattensLineBreaks { processed = processed - .replacingOccurrences(of: "\r\n", with: " ") - .replacingOccurrences(of: "\n", with: " ") - .replacingOccurrences(of: "\r", with: " ") + .replacingOccurrences(of: "\r\n", with: " ", options: .literal) + .replacingOccurrences(of: "\n", with: " ", options: .literal) + .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. diff --git a/apple/Plugins/TableProPluginKit/PluginSQLRegexPattern.swift b/apple/Plugins/TableProPluginKit/PluginSQLRegexPattern.swift index 6217e4c550..5b4229f75c 100644 --- a/apple/Plugins/TableProPluginKit/PluginSQLRegexPattern.swift +++ b/apple/Plugins/TableProPluginKit/PluginSQLRegexPattern.swift @@ -9,13 +9,12 @@ public enum PluginSQLRegexPattern { } public static func escapedLiteral(_ value: String) -> String { - var escaped = "" - escaped.reserveCapacity(value.utf8.count * 2) - for character in value { - if metacharacters.contains(character) { escaped.append("\\") } - escaped.append(character) + var escaped = String.UnicodeScalarView() + for scalar in value.unicodeScalars { + if metacharacters.contains(scalar) { escaped.append("\\") } + escaped.append(scalar) } - return escaped + return String(escaped) } public static func pattern( @@ -38,7 +37,7 @@ public enum PluginSQLRegexPattern { return ignoresCase ? "(?i)\(body)" : body } - private static let metacharacters: Set = [ + private static let metacharacters: Set = [ "\\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", "|", "^", "$" ] } diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataRowEditSQL.swift b/apple/Plugins/TeradataDriverPlugin/TeradataRowEditSQL.swift index d2ad138869..896991da6a 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataRowEditSQL.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataRowEditSQL.swift @@ -18,7 +18,7 @@ enum TeradataRowEditSQL { case .null: return "NULL" case .text(let string): - return "'" + string.replacingOccurrences(of: "'", with: "''") + "'" + return "'" + string.replacingOccurrences(of: "'", with: "''", options: .literal) + "'" case .bytes(let data): return "'" + data.map { String(format: "%02X", $0) }.joined() + "'XB" } diff --git a/apple/Plugins/TypesenseDriverPlugin/TypesenseFilterBuilder.swift b/apple/Plugins/TypesenseDriverPlugin/TypesenseFilterBuilder.swift index 38d7596e8a..4bdb58cc72 100644 --- a/apple/Plugins/TypesenseDriverPlugin/TypesenseFilterBuilder.swift +++ b/apple/Plugins/TypesenseDriverPlugin/TypesenseFilterBuilder.swift @@ -209,7 +209,7 @@ enum TypesenseFilterBuilder { /// value: a backtick ends the literal, and everything after it is parsed as filter syntax. /// Measured on 29.0, ``tag:=`odd` || year:>0`` matched every document in the collection. static func textLiteral(_ value: String, column: String) throws -> String { - guard !value.contains("`") else { + guard !value.unicodeScalars.contains("`") else { throw TypesenseFilterError.backtickInValue(column: column) } return "`\(value)`" diff --git a/apple/TablePro/Core/Compare/CompareSQLLiteral.swift b/apple/TablePro/Core/Compare/CompareSQLLiteral.swift index 0217e55e14..a609186154 100644 --- a/apple/TablePro/Core/Compare/CompareSQLLiteral.swift +++ b/apple/TablePro/Core/Compare/CompareSQLLiteral.swift @@ -54,9 +54,9 @@ internal enum CompareSQLLiteral { /// A driver that passed the value through unquoted answered for a number, so the value holds /// no quote to escape. Escaping anyway is what keeps that true if a driver ever renders a /// quoted literal some other way. - let quoted = rendered.hasPrefix("'") + let quoted = rendered.unicodeScalars.first == "'" ? rendered - : "'\(text.replacingOccurrences(of: "'", with: "''"))'" + : "'\(text.replacingOccurrences(of: "'", with: "''", options: .literal))'" return prefixed(quoted, databaseType: databaseType) } @@ -64,7 +64,7 @@ internal enum CompareSQLLiteral { /// `NULL` for a null and passes a number through unquoted, and `N` in front of either is a /// syntax error, so the opening quote is what decides. internal static func prefixed(_ literal: String, databaseType: DatabaseType) -> String { - guard literal.hasPrefix("'") else { return literal } + guard literal.unicodeScalars.first == "'" else { return literal } return SQLStringLiteralPrefix.forDatabaseType(databaseType) + literal } diff --git a/apple/TablePro/Core/CrossEngine/CrossEngineValueCoercer.swift b/apple/TablePro/Core/CrossEngine/CrossEngineValueCoercer.swift index ffa6dde751..3e00035544 100644 --- a/apple/TablePro/Core/CrossEngine/CrossEngineValueCoercer.swift +++ b/apple/TablePro/Core/CrossEngine/CrossEngineValueCoercer.swift @@ -202,11 +202,11 @@ internal struct CrossEngineValueCoercer: Sendable { if raw == "true" || raw == "false" { return raw } if Int64(raw) != nil || Double(raw) != nil { return raw } let escaped = raw - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "\"", with: "\\\"") - .replacingOccurrences(of: "\n", with: "\\n") - .replacingOccurrences(of: "\r", with: "\\r") - .replacingOccurrences(of: "\t", with: "\\t") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "\"", with: "\\\"", options: .literal) + .replacingOccurrences(of: "\n", with: "\\n", options: .literal) + .replacingOccurrences(of: "\r", with: "\\r", options: .literal) + .replacingOccurrences(of: "\t", with: "\\t", options: .literal) return "\"\(escaped)\"" } } diff --git a/apple/TablePro/Core/CrossEngine/SQLTypeRenderer+BundledFamilies.swift b/apple/TablePro/Core/CrossEngine/SQLTypeRenderer+BundledFamilies.swift index a1debece23..ccb2099963 100644 --- a/apple/TablePro/Core/CrossEngine/SQLTypeRenderer+BundledFamilies.swift +++ b/apple/TablePro/Core/CrossEngine/SQLTypeRenderer+BundledFamilies.swift @@ -142,7 +142,7 @@ internal extension SQLTypeRenderer { return RenderedColumnType(spelling: "VARCHAR(255)", fidelity: .approximated, reason: enumBecomesText) } let labels = values - .map { "'\($0.replacingOccurrences(of: "'", with: "''"))'" } + .map { "'\($0.replacingOccurrences(of: "'", with: "''", options: .literal))'" } .joined(separator: ", ") return RenderedColumnType(spelling: "ENUM(\(labels))") } diff --git a/apple/TablePro/Core/CrossEngine/SQLTypeRenderer+RegistryFamilies.swift b/apple/TablePro/Core/CrossEngine/SQLTypeRenderer+RegistryFamilies.swift index cc6e03cbf6..12bb6b36df 100644 --- a/apple/TablePro/Core/CrossEngine/SQLTypeRenderer+RegistryFamilies.swift +++ b/apple/TablePro/Core/CrossEngine/SQLTypeRenderer+RegistryFamilies.swift @@ -262,7 +262,7 @@ internal extension SQLTypeRenderer { return RenderedColumnType(spelling: "String", fidelity: .approximated, reason: enumBecomesText) } let labels = values.enumerated() - .map { "'\($0.element.replacingOccurrences(of: "'", with: "\\'"))' = \($0.offset + 1)" } + .map { "'\($0.element.replacingOccurrences(of: "'", with: "\\'", options: .literal))' = \($0.offset + 1)" } .joined(separator: ", ") return RenderedColumnType(spelling: "Enum8(\(labels))") } diff --git a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift index 2c7a004f5f..65316509b3 100644 --- a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift +++ b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift @@ -32,14 +32,16 @@ struct RewindExecutor { factory: RowChangeStatementFactory, browseBuilder: (any PluginDatabaseDriver)? ) async throws -> RewindPlan { + let dialect = PluginManager.shared.sqlDialect(for: connection.type) let planner = RewindPlanner( record: record, factory: factory, queryBuilder: TableQueryBuilder( databaseType: connection.type, pluginDriver: browseBuilder, - dialect: PluginManager.shared.sqlDialect(for: connection.type), - pagination: PluginManager.shared.paginationCapability(for: connection.type) + dialect: dialect, + pagination: PluginManager.shared.paginationCapability(for: connection.type), + dialectQuote: dialect.map { quoteIdentifierFromDialect($0) } ?? { SQLEscaping.quoteIdentifier($0) } ) ) let queries = planner.readQueries() diff --git a/apple/TablePro/Core/Database/FilterSQLGenerator.swift b/apple/TablePro/Core/Database/FilterSQLGenerator.swift index 96b8a39e75..26818c7ebd 100644 --- a/apple/TablePro/Core/Database/FilterSQLGenerator.swift +++ b/apple/TablePro/Core/Database/FilterSQLGenerator.swift @@ -253,7 +253,7 @@ struct FilterSQLGenerator { private func foldedComparison(_ literal: String, folding: PluginSQLCaseFolding) -> String? { guard folding.foldsComparisonOperands else { return literal } - guard literal.hasPrefix("'") else { return nil } + guard literal.unicodeScalars.first == "'" else { return nil } return folding.fold(literal) } @@ -290,7 +290,7 @@ struct FilterSQLGenerator { } } - let foldsList = folding.foldsComparisonOperands && literals.allSatisfy { $0.hasPrefix("'") } + let foldsList = folding.foldsComparisonOperands && literals.allSatisfy { $0.unicodeScalars.first == "'" } let nonNullValues = foldsList ? literals.map(folding.fold) : literals let foldedColumn = foldsList ? folding.fold(patternOperand(column, columnType: columnType)) : column let inClause: String? = nonNullValues.isEmpty ? nil : { diff --git a/apple/TablePro/Core/Database/SQLEscaping.swift b/apple/TablePro/Core/Database/SQLEscaping.swift index b6e675696e..422d7b5451 100644 --- a/apple/TablePro/Core/Database/SQLEscaping.swift +++ b/apple/TablePro/Core/Database/SQLEscaping.swift @@ -20,15 +20,15 @@ enum SQLEscaping { /// - Returns: The escaped string safe for use in ANSI SQL string literals static func escapeStringLiteral(_ str: String) -> String { var result = str - result = result.replacingOccurrences(of: "'", with: "''") - result = result.replacingOccurrences(of: "\0", with: "") + result = result.replacingOccurrences(of: "'", with: "''", options: .literal) + result = result.replacingOccurrences(of: "\0", with: "", options: .literal) return result } /// Quote a SQL identifier using ANSI double-quote rules, doubling any embedded quote. static func quoteIdentifier(_ identifier: String) -> String { let quote = "\"" - let escaped = identifier.replacingOccurrences(of: quote, with: quote + quote) + let escaped = identifier.replacingOccurrences(of: quote, with: quote + quote, options: .literal) return "\(quote)\(escaped)\(quote)" } @@ -40,9 +40,9 @@ enum SQLEscaping { /// - Returns: The escaped value with %, _, and \ escaped static func escapeLikeWildcards(_ value: String) -> String { var result = value - result = result.replacingOccurrences(of: "\\", with: "\\\\") - result = result.replacingOccurrences(of: "%", with: "\\%") - result = result.replacingOccurrences(of: "_", with: "\\_") + result = result.replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + result = result.replacingOccurrences(of: "%", with: "\\%", options: .literal) + result = result.replacingOccurrences(of: "_", with: "\\_", options: .literal) return result } } diff --git a/apple/TablePro/Core/Database/TableDDLComposer.swift b/apple/TablePro/Core/Database/TableDDLComposer.swift index 527b6aff61..12bb98ba67 100644 --- a/apple/TablePro/Core/Database/TableDDLComposer.swift +++ b/apple/TablePro/Core/Database/TableDDLComposer.swift @@ -93,7 +93,7 @@ internal enum TableDDLComposer { preamble += sequence.ddl + "\n\n" } for enumType in enumTypes { - let quotedName = "\"\(enumType.name.replacingOccurrences(of: "\"", with: "\"\""))\"" + let quotedName = "\"\(enumType.name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" let quotedLabels = enumType.labels.map { "'\(SQLEscaping.escapeStringLiteral($0))'" } preamble += "CREATE TYPE \(quotedName) AS ENUM (\(quotedLabels.joined(separator: ", ")));\n" } diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift index a7974adcc1..5af1bb736b 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift @@ -98,7 +98,8 @@ extension MCPConnectionBridge { databaseType: databaseType, pluginDriver: driver.queryBuildingPluginDriver, dialect: dialect, - pagination: pagination + pagination: pagination, + dialectQuote: dialect.map { quoteIdentifierFromDialect($0) } ?? { SQLEscaping.quoteIdentifier($0) } ) let sortState = try MCPConnectionBridge.sortState(from: request.sort, columns: names) let requested = try MCPConnectionBridge.validatedSelection(request.columns, available: names) diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/MCPExportWriters.swift b/apple/TablePro/Core/MCP/Protocol/Tools/MCPExportWriters.swift index 2b48b96ce6..3816163f7a 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/MCPExportWriters.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/MCPExportWriters.swift @@ -112,22 +112,23 @@ struct MCPSqlExportDialect: Sendable { func quote(_ name: String) -> String { guard identifierQuote != "[" else { - return "[\(name.replacingOccurrences(of: "]", with: "]]"))]" + return "[\(name.replacingOccurrences(of: "]", with: "]]", options: .literal))]" } let escaped = name.replacingOccurrences( of: identifierQuote, - with: identifierQuote + identifierQuote + with: identifierQuote + identifierQuote, + options: .literal ) return "\(identifierQuote)\(escaped)\(identifierQuote)" } func literal(_ value: String) -> String { guard usesBackslashEscaping else { - return "'\(value.replacingOccurrences(of: "'", with: "''"))'" + return "'\(value.replacingOccurrences(of: "'", with: "''", options: .literal))'" } let escaped = value - .replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "'", with: "''") + .replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "'", with: "''", options: .literal) return "'\(escaped)'" } diff --git a/apple/TablePro/Core/Services/Export/ImportErrorReport.swift b/apple/TablePro/Core/Services/Export/ImportErrorReport.swift index c8b87b8649..57a4feabf9 100644 --- a/apple/TablePro/Core/Services/Export/ImportErrorReport.swift +++ b/apple/TablePro/Core/Services/Export/ImportErrorReport.swift @@ -60,14 +60,14 @@ enum ImportErrorReport { /// in Excel. The CSV export guards the same way, for the same reason. private static func csvField(_ value: String) -> String { var flattened = value - .replacingOccurrences(of: "\r\n", with: " ") - .replacingOccurrences(of: "\n", with: " ") - .replacingOccurrences(of: "\r", with: " ") - if let first = flattened.first, Self.formulaPrefixes.contains(first) { + .replacingOccurrences(of: "\r\n", with: " ", options: .literal) + .replacingOccurrences(of: "\n", with: " ", options: .literal) + .replacingOccurrences(of: "\r", with: " ", options: .literal) + if let first = flattened.unicodeScalars.first, Self.formulaPrefixes.contains(Character(first)) { flattened = "'" + flattened } - guard flattened.contains(",") || flattened.contains("\"") else { return flattened } - return "\"\(flattened.replacingOccurrences(of: "\"", with: "\"\""))\"" + guard flattened.unicodeScalars.contains(where: { $0 == "," || $0 == "\"" }) else { return flattened } + return "\"\(flattened.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } private static let formulaPrefixes: Set = ["=", "+", "-", "@"] diff --git a/apple/TablePro/Core/Services/Query/TableQueryBuilder.swift b/apple/TablePro/Core/Services/Query/TableQueryBuilder.swift index d27a99f9af..fd215baa7a 100644 --- a/apple/TablePro/Core/Services/Query/TableQueryBuilder.swift +++ b/apple/TablePro/Core/Services/Query/TableQueryBuilder.swift @@ -26,16 +26,13 @@ struct TableQueryBuilder { pluginDriver: (any PluginDatabaseDriver)? = nil, dialect: SQLDialectDescriptor? = nil, pagination: PaginationCapability, - dialectQuote: ((String) -> String)? = nil + dialectQuote: @escaping (String) -> String ) { self.databaseType = databaseType self.pluginDriver = pluginDriver self.dialect = dialect self.pagination = pagination - self.dialectQuote = dialectQuote ?? { name in - let escaped = name.replacingOccurrences(of: "\"", with: "\"\"") - return "\"\(escaped)\"" - } + self.dialectQuote = dialectQuote } mutating func setPluginDriver(_ driver: (any PluginDatabaseDriver)?) { diff --git a/apple/TablePro/Core/Services/Query/UserDefinedTypeSuggestions.swift b/apple/TablePro/Core/Services/Query/UserDefinedTypeSuggestions.swift index d5a4765dee..811d631304 100644 --- a/apple/TablePro/Core/Services/Query/UserDefinedTypeSuggestions.swift +++ b/apple/TablePro/Core/Services/Query/UserDefinedTypeSuggestions.swift @@ -68,7 +68,7 @@ enum UserDefinedTypeSuggestions { && !(scalars.first.map { CharacterSet.decimalDigits.contains($0) } ?? false) && scalars.allSatisfy { Self.plainIdentifierScalars.contains($0) } guard !isPlain else { return name } - return "\"\(name.replacingOccurrences(of: "\"", with: "\"\""))\"" + return "\"\(name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } private static let plainIdentifierScalars = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyz0123456789_") diff --git a/apple/TablePro/Core/Storage/QueryHistoryStorage.swift b/apple/TablePro/Core/Storage/QueryHistoryStorage.swift index d5a298414d..71f53a8b0b 100644 --- a/apple/TablePro/Core/Storage/QueryHistoryStorage.swift +++ b/apple/TablePro/Core/Storage/QueryHistoryStorage.swift @@ -653,10 +653,10 @@ actor QueryHistoryStorage { static func ftsQuery(for searchText: String) -> String { let terms = searchText .split(whereSeparator: { $0.isWhitespace }) - .map { $0.replacingOccurrences(of: "\"", with: "\"\"") } + .map { $0.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) } .filter { !$0.isEmpty } - guard !terms.isEmpty else { return "\"\(searchText.replacingOccurrences(of: "\"", with: "\"\""))\"*" } + guard !terms.isEmpty else { return "\"\(searchText.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"*" } return terms.map { "\"\($0)\"*" }.joined(separator: " AND ") } diff --git a/apple/TablePro/Core/Storage/SQLFavoriteStorage.swift b/apple/TablePro/Core/Storage/SQLFavoriteStorage.swift index 5633680ad0..9350f4e701 100644 --- a/apple/TablePro/Core/Storage/SQLFavoriteStorage.swift +++ b/apple/TablePro/Core/Storage/SQLFavoriteStorage.swift @@ -711,7 +711,7 @@ internal actor SQLFavoriteStorage { let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self) if let searchText = searchText, !searchText.isEmpty { - let sanitized = "\"\(searchText.replacingOccurrences(of: "\"", with: "\"\""))\"" + let sanitized = "\"\(searchText.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" sqlite3_bind_text(statement, bindIndex, sanitized, -1, SQLITE_TRANSIENT) bindIndex += 1 } diff --git a/apple/TablePro/Core/Utilities/SQL/CsvRowConverter.swift b/apple/TablePro/Core/Utilities/SQL/CsvRowConverter.swift index f358685542..e611a514d3 100644 --- a/apple/TablePro/Core/Utilities/SQL/CsvRowConverter.swift +++ b/apple/TablePro/Core/Utilities/SQL/CsvRowConverter.swift @@ -47,7 +47,7 @@ internal struct CsvRowConverter { // An unquoted field holds no quote, so wrapping it for its edge spaces needs no escaping. static func encode(_ value: String) -> String { let field = PluginRowWriters.csvField(value, options: fieldOptions) - guard !field.hasPrefix("\""), value.hasPrefix(" ") || value.hasSuffix(" ") else { return field } + guard field.unicodeScalars.first != "\"", value.hasPrefix(" ") || value.hasSuffix(" ") else { return field } return "\"\(field)\"" } diff --git a/apple/TablePro/Core/Utilities/SQL/DialectQuoteHelper.swift b/apple/TablePro/Core/Utilities/SQL/DialectQuoteHelper.swift index f379acd419..3c5ec428d3 100644 --- a/apple/TablePro/Core/Utilities/SQL/DialectQuoteHelper.swift +++ b/apple/TablePro/Core/Utilities/SQL/DialectQuoteHelper.swift @@ -53,14 +53,14 @@ func escapeStringLiteralFromDialect(_ dialect: SQLDialectDescriptor) -> @Sendabl guard dialect.requiresBackslashEscaping else { return SQLEscaping.escapeStringLiteral } return { value in var result = value - result = result.replacingOccurrences(of: "\\", with: "\\\\") - result = result.replacingOccurrences(of: "'", with: "''") - result = result.replacingOccurrences(of: "\n", with: "\\n") - result = result.replacingOccurrences(of: "\r", with: "\\r") - result = result.replacingOccurrences(of: "\t", with: "\\t") - result = result.replacingOccurrences(of: "\0", with: "\\0") - result = result.replacingOccurrences(of: "\u{08}", with: "\\b") - result = result.replacingOccurrences(of: "\u{1A}", with: "\\Z") + result = result.replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + result = result.replacingOccurrences(of: "'", with: "''", options: .literal) + result = result.replacingOccurrences(of: "\n", with: "\\n", options: .literal) + result = result.replacingOccurrences(of: "\r", with: "\\r", options: .literal) + result = result.replacingOccurrences(of: "\t", with: "\\t", options: .literal) + result = result.replacingOccurrences(of: "\0", with: "\\0", options: .literal) + result = result.replacingOccurrences(of: "\u{08}", with: "\\b", options: .literal) + result = result.replacingOccurrences(of: "\u{1A}", with: "\\Z", options: .literal) return result } } diff --git a/apple/TablePro/Core/Utilities/SQL/InClauseConverter.swift b/apple/TablePro/Core/Utilities/SQL/InClauseConverter.swift index 771d1d213b..42c455df7e 100644 --- a/apple/TablePro/Core/Utilities/SQL/InClauseConverter.swift +++ b/apple/TablePro/Core/Utilities/SQL/InClauseConverter.swift @@ -59,7 +59,7 @@ internal struct InClauseConverter { } private func quoted(_ value: String) -> String { - let escaped = escapeStringLiteral?(value) ?? value.replacingOccurrences(of: "'", with: "''") + let escaped = escapeStringLiteral?(value) ?? value.replacingOccurrences(of: "'", with: "''", options: .literal) return "\(stringLiteralPrefix)'\(escaped)'" } } diff --git a/apple/TablePro/Core/Utilities/SQL/SQLParameterInliner.swift b/apple/TablePro/Core/Utilities/SQL/SQLParameterInliner.swift index a52a9b7825..2842f57d78 100644 --- a/apple/TablePro/Core/Utilities/SQL/SQLParameterInliner.swift +++ b/apple/TablePro/Core/Utilities/SQL/SQLParameterInliner.swift @@ -212,6 +212,6 @@ struct SQLParameterInliner { /// Escapes single quotes by doubling them for SQL string literals. private static func escapeString(_ value: String) -> String { - value.replacingOccurrences(of: "'", with: "''") + value.replacingOccurrences(of: "'", with: "''", options: .literal) } } diff --git a/apple/TablePro/Views/Editor/QueryCompletionAdapter.swift b/apple/TablePro/Views/Editor/QueryCompletionAdapter.swift index ac96f70436..b96e7de915 100644 --- a/apple/TablePro/Views/Editor/QueryCompletionAdapter.swift +++ b/apple/TablePro/Views/Editor/QueryCompletionAdapter.swift @@ -259,7 +259,7 @@ final class QueryCompletionAdapter: CodeSuggestionDelegate { } let closeText = String(decoding: [closer], as: UTF16.self) - let name = item.label.replacingOccurrences(of: closeText, with: closeText + closeText) + let name = item.label.replacingOccurrences(of: closeText, with: closeText + closeText, options: .literal) let inner = NSRange(location: open + 1, length: (close ?? cursor) - open - 1) return Edit( range: inner, diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index 4677384b43..23debff28e 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -597,7 +597,7 @@ final class MainContentCoordinator: ObservableObject { databaseType: connection.type, dialect: dialect, pagination: services.pluginManager.paginationCapability(for: connection.type), - dialectQuote: dialect.map { quoteIdentifierFromDialect($0) } + dialectQuote: dialect.map { quoteIdentifierFromDialect($0) } ?? { SQLEscaping.quoteIdentifier($0) } ) self.persistence = TabPersistenceCoordinator.forConnection(connection.id) diff --git a/apple/TableProMobile/TableProMobile/Completion/QueryCompletionController.swift b/apple/TableProMobile/TableProMobile/Completion/QueryCompletionController.swift index e3183796f3..3892d5f982 100644 --- a/apple/TableProMobile/TableProMobile/Completion/QueryCompletionController.swift +++ b/apple/TableProMobile/TableProMobile/Completion/QueryCompletionController.swift @@ -387,7 +387,7 @@ final class QueryCompletionController { ) } let closeText = text.substring(with: NSRange(location: close, length: 1)) - let name = item.label.replacingOccurrences(of: closeText, with: closeText + closeText) + let name = item.label.replacingOccurrences(of: closeText, with: closeText + closeText, options: .literal) let inner = NSRange(location: quoted.open + 1, length: close - quoted.open - 1) return QueryCompletionEdit(range: inner, text: name, caret: inner.location + (name as NSString).length + 1) } diff --git a/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Schema.swift b/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Schema.swift index bd5644a208..4e321b95b8 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Schema.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver+Schema.swift @@ -150,11 +150,11 @@ nonisolated extension DuckDBDriver { } private func escapeLiteral(_ value: String) -> String { - value.replacingOccurrences(of: "'", with: "''") + value.replacingOccurrences(of: "'", with: "''", options: .literal) } private func escapeIdentifier(_ value: String) -> String { - value.replacingOccurrences(of: "\"", with: "\"\"") + value.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) } private static let indexColumnsRegex = try? NSRegularExpression( diff --git a/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver.swift index 4324e12c16..b3a0cd9fb6 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/DuckDBDriver.swift @@ -373,7 +373,7 @@ actor DuckDBActor { } static func quoteIdentifier(_ identifier: String) -> String { - "\"\(identifier.replacingOccurrences(of: "\"", with: "\"\""))\"" + "\"\(identifier.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } static func stripTrailingSemicolon(_ query: String) -> String { diff --git a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift index 2f8f9b9f90..023e7996f0 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift @@ -296,7 +296,7 @@ nonisolated final class MySQLDriver: DatabaseDriver, @unchecked Sendable { } func switchDatabase(to name: String) async throws { - let safe = name.replacingOccurrences(of: "`", with: "``") + let safe = name.replacingOccurrences(of: "`", with: "``", options: .literal) _ = try await actor.execute("USE `\(safe)`") } @@ -509,7 +509,7 @@ nonisolated extension MySQLDriver { } private static func quotedIdentifier(_ name: String) -> String { - "`\(name.replacingOccurrences(of: "`", with: "``"))`" + "`\(name.replacingOccurrences(of: "`", with: "``", options: .literal))`" } } diff --git a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLConnectionString.swift b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLConnectionString.swift index 958712522c..5f5e455d00 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLConnectionString.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLConnectionString.swift @@ -52,7 +52,7 @@ nonisolated enum PostgreSQLConnectionString { } static func escape(_ value: String) -> String { - value.replacingOccurrences(of: "\\", with: "\\\\") - .replacingOccurrences(of: "'", with: "\\'") + value.replacingOccurrences(of: "\\", with: "\\\\", options: .literal) + .replacingOccurrences(of: "'", with: "\\'", options: .literal) } } diff --git a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift index 93731fe39c..6b75131722 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift @@ -235,7 +235,7 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, @unchecked Sendable { } func switchSchema(to name: String) async throws { - let safe = name.replacingOccurrences(of: "\"", with: "\"\"") + let safe = name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) _ = try await actor.execute("SET search_path TO \"\(safe)\"") currentSchema = name } diff --git a/apple/TableProMobile/TableProMobile/Drivers/SQLiteDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/SQLiteDriver.swift index bca9aeaeed..30a3f20eda 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/SQLiteDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/SQLiteDriver.swift @@ -148,7 +148,7 @@ nonisolated final class SQLiteDriver: DatabaseDriver, @unchecked Sendable { } func fetchColumns(table: String, schema: String?) async throws -> [ColumnInfo] { - let safe = table.replacingOccurrences(of: "'", with: "''") + let safe = table.replacingOccurrences(of: "'", with: "''", options: .literal) let raw = try await actor.execute("PRAGMA table_info('\(safe)')") let createStatement = try await fetchCreateStatement(table: safe) @@ -186,7 +186,7 @@ nonisolated final class SQLiteDriver: DatabaseDriver, @unchecked Sendable { } func fetchIndexes(table: String, schema: String?) async throws -> [IndexInfo] { - let safe = table.replacingOccurrences(of: "'", with: "''") + let safe = table.replacingOccurrences(of: "'", with: "''", options: .literal) let raw = try await actor.execute(""" SELECT il.name, il."unique", il.origin, ii.name AS col_name FROM pragma_index_list('\(safe)') il diff --git a/apple/TableProMobile/TableProMobile/Helpers/ClipboardExporter.swift b/apple/TableProMobile/TableProMobile/Helpers/ClipboardExporter.swift index bfa197d930..fe9517b26a 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/ClipboardExporter.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/ClipboardExporter.swift @@ -281,8 +281,8 @@ nonisolated enum ClipboardExporter { } private static func escapeCsvField(_ field: String) -> String { - if field.contains(",") || field.contains("\"") || field.contains("\n") || field.contains("\r") { - return "\"" + field.replacingOccurrences(of: "\"", with: "\"\"") + "\"" + if field.unicodeScalars.contains(where: { $0 == "," || $0 == "\"" || $0 == "\n" || $0 == "\r" }) { + return "\"" + field.replacingOccurrences(of: "\"", with: "\"\"", options: .literal) + "\"" } return field } diff --git a/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift b/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift index cc0065f44b..99dfb3d9a2 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift @@ -11,17 +11,17 @@ nonisolated enum SQLBuilder { static func quoteIdentifier(_ name: String, for type: DatabaseType) -> String { if speaksMySQLDialect(type) { - return "`\(name.replacingOccurrences(of: "`", with: "``"))`" + return "`\(name.replacingOccurrences(of: "`", with: "``", options: .literal))`" } switch type { case .postgresql, .redshift: - return "\"\(name.replacingOccurrences(of: "\"", with: "\"\""))\"" + return "\"\(name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" case .mssql: - return "[\(name.replacingOccurrences(of: "]", with: "]]"))]" + return "[\(name.replacingOccurrences(of: "]", with: "]]", options: .literal))]" case .clickhouse: return clickHouseQuotedIdentifier(name) default: - return "\"\(name.replacingOccurrences(of: "\"", with: "\"\""))\"" + return "\"\(name.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } } diff --git a/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderIdentifierQuotingTests.swift b/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderIdentifierQuotingTests.swift new file mode 100644 index 0000000000..8caba3560b --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderIdentifierQuotingTests.swift @@ -0,0 +1,26 @@ +import Foundation +import TableProModels +import Testing +@testable import TableProMobile + +struct SQLBuilderIdentifierQuotingTests { + private static func scalars(_ text: String) -> [Unicode.Scalar] { + Array(text.unicodeScalars) + } + + @Test("A closing quote a combining mark follows is still doubled", arguments: ["\u{301}", "\u{FF9E}"]) + func closingQuoteBeforeCombiningMark(mark: String) { + #expect(Self.scalars(SQLBuilder.quoteIdentifier("t]\(mark)x", for: .mssql)) == Self.scalars("[t]]\(mark)x]")) + #expect(Self.scalars(SQLBuilder.quoteIdentifier("t\"\(mark)x", for: .postgresql)) == Self.scalars("\"t\"\"\(mark)x\"")) + #expect(Self.scalars(SQLBuilder.quoteIdentifier("t`\(mark)x", for: .mysql)) == Self.scalars("`t``\(mark)x`")) + #expect(Self.scalars(SQLBuilder.quoteIdentifier("t\"\(mark)x", for: .sqlite)) == Self.scalars("\"t\"\"\(mark)x\"")) + } + + @Test("Opening a crafted PostgreSQL table sends one statement") + func browseOfCraftedTableIsOneIdentifier() { + let sql = SQLBuilder.buildSelect( + table: "t\"\u{301}; ALTER ROLE mallory SUPERUSER; --", schema: "public", type: .postgresql, limit: 200, offset: 0 + ) + #expect(Self.scalars(sql).starts(with: Self.scalars("SELECT * FROM \"public\".\"t\"\"\u{301}; ALTER ROLE mallory SUPERUSER; --\""))) + } +} diff --git a/apple/TableProTests/Core/Compare/CompareDataScopeTests.swift b/apple/TableProTests/Core/Compare/CompareDataScopeTests.swift index a1ebedcac0..0e7e96ab5d 100644 --- a/apple/TableProTests/Core/Compare/CompareDataScopeTests.swift +++ b/apple/TableProTests/Core/Compare/CompareDataScopeTests.swift @@ -20,6 +20,10 @@ private final class ScopeQuotingDriver: PluginDriverStub, @unchecked Sendable { } } +private final class BackslashLiteralDriver: PluginDriverStub, @unchecked Sendable { + var requiresBackslashEscapingInLiterals: Bool { true } +} + private enum ScopeDialect { static func make( paginationStyle: SQLDialectDescriptor.PaginationStyle = .limit, @@ -615,6 +619,16 @@ struct KeyOrderedQueryScopeTests { struct CompareSQLLiteralTextTests { private let driver = ScopeQuotingDriver() + @Test + func aValueLedByACombiningMarkKeepsTheDriversBackslashEscape() { + let literal = CompareSQLLiteral.textLiteral( + "\u{301}\\' OR 1=1 -- ", columnType: .text(rawType: "TEXT"), databaseType: .mysql, + driver: BackslashLiteralDriver() + ) + + #expect(Array(literal.unicodeScalars) == Array("'\u{301}\\\\'' OR 1=1 -- '".unicodeScalars)) + } + @Test func numericLookingTextIsQuotedForATextColumn() { let literal = CompareSQLLiteral.textLiteral( diff --git a/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift b/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift index d79bc03d43..812c316353 100644 --- a/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift +++ b/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift @@ -15,7 +15,8 @@ struct RewindPlannerTests { private let sqliteQueryBuilder = TableQueryBuilder( databaseType: .sqlite, dialect: PluginManager.shared.sqlDialect(for: .sqlite), - pagination: .offset + pagination: .offset, + dialectQuote: SQLEscaping.quoteIdentifier ) private func operation( diff --git a/apple/TableProTests/Core/Plugins/PluginMetadataRegistryVariantTests.swift b/apple/TableProTests/Core/Plugins/PluginMetadataRegistryVariantTests.swift index 2ff8d5e58c..d73f3c5098 100644 --- a/apple/TableProTests/Core/Plugins/PluginMetadataRegistryVariantTests.swift +++ b/apple/TableProTests/Core/Plugins/PluginMetadataRegistryVariantTests.swift @@ -140,7 +140,8 @@ struct PluginMetadataRegistryVariantTests { let builder = TableQueryBuilder( databaseType: .presto, dialect: PluginManager.shared.sqlDialect(for: .presto), - pagination: PluginManager.shared.paginationCapability(for: .presto) + pagination: PluginManager.shared.paginationCapability(for: .presto), + dialectQuote: SQLEscaping.quoteIdentifier ) let query = builder.buildBaseQuery(tableName: "nation", schemaName: "tiny", limit: 1_000, offset: 1_000) #expect(query.hasSuffix("LIMIT 1000")) diff --git a/apple/TableProTests/Core/Services/TableQueryBuilderFilterTests.swift b/apple/TableProTests/Core/Services/TableQueryBuilderFilterTests.swift index aef380c502..0eab73970c 100644 --- a/apple/TableProTests/Core/Services/TableQueryBuilderFilterTests.swift +++ b/apple/TableProTests/Core/Services/TableQueryBuilderFilterTests.swift @@ -22,7 +22,7 @@ struct TableQueryBuilderFilteredQueryTests { likeEscapeStyle: .implicit, paginationStyle: .limit ) - private let builder = TableQueryBuilder(databaseType: .mysql, dialect: Self.mysqlDialect, pagination: .offset) + private let builder = TableQueryBuilder(databaseType: .mysql, dialect: Self.mysqlDialect, pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier) @Test("buildFilteredQuery with enabled filter produces WHERE clause") func filteredQueryWithEnabledFilter() throws { @@ -93,7 +93,7 @@ struct TableQueryBuilderFilteredCountTests { ) private var builder: TableQueryBuilder { - TableQueryBuilder(databaseType: .mysql, dialect: Self.mysqlDialect, pagination: .offset) + TableQueryBuilder(databaseType: .mysql, dialect: Self.mysqlDialect, pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier) } private func makeFilter(_ column: String, _ value: String, _ op: FilterOperator = .equal) -> TableFilter { @@ -136,9 +136,28 @@ struct TableQueryBuilderFilteredCountTests { #expect(dataQuery.contains(countWhere)) } + @Test("buildFilteredCountQuery doubles a quote a combining mark follows", arguments: ["\u{301}", "\u{FF9E}"]) + func filteredCountDoublesQuoteBeforeCombiningMark(mark: String) throws { + let dialect = SQLDialectDescriptor( + identifierQuote: "[", keywords: [], functions: [], dataTypes: [], + regexSyntax: .unsupported, booleanLiteralStyle: .numeric, + likeEscapeStyle: .explicit, paginationStyle: .offsetFetch + ) + let mssql = TableQueryBuilder( + databaseType: .mssql, dialect: dialect, pagination: .offset, + dialectQuote: quoteIdentifierFromDialect(dialect) + ) + let query = try #require(mssql.buildFilteredCountQuery( + tableName: "customers", filters: [makeFilter("code", "x'\(mark): DROP TABLE audit_log --")], + columns: ["code"], columnTypes: [.text(rawType: "nvarchar(64)")] + )) + let expected = "SELECT COUNT(*) FROM [customers] WHERE [code] = N'x''\(mark): DROP TABLE audit_log --'" + #expect(Array(query.unicodeScalars) == Array(expected.unicodeScalars)) + } + @Test("buildFilteredCountQuery returns nil without a dialect") func filteredCountNilWithoutDialect() { - let noDialect = TableQueryBuilder(databaseType: .mysql, pagination: .offset) + let noDialect = TableQueryBuilder(databaseType: .mysql, pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier) #expect(noDialect.buildFilteredCountQuery(tableName: "users", filters: [makeFilter("name", "Alice")]) == nil) } } @@ -163,7 +182,7 @@ struct TableQueryBuilderPaginationTests { ) private func builder(_ dialect: SQLDialectDescriptor) -> TableQueryBuilder { - TableQueryBuilder(databaseType: .postgresql, dialect: dialect, pagination: .offset) + TableQueryBuilder(databaseType: .postgresql, dialect: dialect, pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier) } private func enabledFilter(_ column: String, _ value: String) -> TableFilter { @@ -236,7 +255,9 @@ private final class UnfilterableDriver: PluginDriverStub, @unchecked Sendable { struct TableQueryBuilderDialectlessTests { @Test("An engine with no SQL dialect gets no SQL when its plugin cannot build the filter") func noSQLFallbackWithoutDialect() { - let builder = TableQueryBuilder(databaseType: .etcd, pluginDriver: UnfilterableDriver(), pagination: .offset) + let builder = TableQueryBuilder( + databaseType: .etcd, pluginDriver: UnfilterableDriver(), pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier + ) var filter = TableFilter() filter.columnName = "Value" filter.filterOperator = .contains @@ -248,7 +269,7 @@ struct TableQueryBuilderDialectlessTests { @Test("An engine with no SQL dialect and no plugin gets no SQL for a filter") func noSQLWithoutPlugin() { - let builder = TableQueryBuilder(databaseType: .mongodb, pagination: .offset) + let builder = TableQueryBuilder(databaseType: .mongodb, pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier) var filter = TableFilter() filter.columnName = "name" filter.filterOperator = .equal @@ -258,3 +279,36 @@ struct TableQueryBuilderDialectlessTests { #expect(builder.buildFilteredQuery(tableName: "collection", filters: [filter]) == nil) } } + +struct TableQueryBuilderQuotingTests { + private static let crafted = "t\"\u{301}; DROP TABLE users; --" + + @Test("The ANSI quoter doubles a quote a combining mark follows") + func ansiQuoterDoublesQuoteBeforeMark() { + let quoted = SQLEscaping.quoteIdentifier(Self.crafted) + #expect(Array(quoted.unicodeScalars) == Array("\"t\"\"\u{301}; DROP TABLE users; --\"".unicodeScalars)) + } + + @Test("A browse of a crafted table name keeps the name one identifier") + func browseKeepsCraftedNameOneIdentifier() { + let builder = TableQueryBuilder(databaseType: .postgresql, pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier) + let query = builder.buildBaseQuery(tableName: Self.crafted, schemaName: "app", limit: 100, offset: 0) + let expected = "SELECT * FROM \"app\".\"t\"\"\u{301}; DROP TABLE users; --\" LIMIT 100 OFFSET 0" + #expect(Array(query.unicodeScalars) == Array(expected.unicodeScalars)) + } + + @Test("A backtick dialect quotes with backticks, not double quotes") + func backtickDialectQuotesWithBackticks() { + let dialect = SQLDialectDescriptor( + identifierQuote: "`", keywords: [], functions: [], dataTypes: [], + regexSyntax: .regexp, booleanLiteralStyle: .numeric, + likeEscapeStyle: .implicit, paginationStyle: .limit + ) + let builder = TableQueryBuilder( + databaseType: .mysql, dialect: dialect, pagination: .offset, + dialectQuote: quoteIdentifierFromDialect(dialect) + ) + let query = builder.buildBaseQuery(tableName: "a`\u{301}b", limit: 10, offset: 0) + #expect(Array(query.unicodeScalars).starts(with: Array("SELECT * FROM `a``\u{301}b`".unicodeScalars))) + } +} diff --git a/apple/TableProTests/Core/Services/TableQueryBuilderSortScopeTests.swift b/apple/TableProTests/Core/Services/TableQueryBuilderSortScopeTests.swift index daeb5ab3a0..d0208344ab 100644 --- a/apple/TableProTests/Core/Services/TableQueryBuilderSortScopeTests.swift +++ b/apple/TableProTests/Core/Services/TableQueryBuilderSortScopeTests.swift @@ -65,7 +65,7 @@ struct TableQueryBuilderSortScopeTests { private let displayColumns = ["_id", "name", "email", "createdAt"] private func makeBuilder(_ driver: SortRecordingDriver) -> TableQueryBuilder { - TableQueryBuilder(databaseType: .mongodb, pluginDriver: driver, pagination: .offset) + TableQueryBuilder(databaseType: .mongodb, pluginDriver: driver, pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier) } /// Issue #2234: after hiding columns, the sort index landed outside the scoped list and the @@ -170,7 +170,7 @@ struct TableQueryBuilderSortScopeTests { let sortState = SortState(columns: [ SortColumn(columnIndex: 99, direction: .descending, columnName: "createdAt"), ]) - let query = TableQueryBuilder(databaseType: .mysql, pagination: .offset).buildBaseQuery( + let query = TableQueryBuilder(databaseType: .mysql, pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier).buildBaseQuery( tableName: "events", sortState: sortState, columns: displayColumns, diff --git a/apple/TableProTests/Core/Utilities/CsvRowConverterTests.swift b/apple/TableProTests/Core/Utilities/CsvRowConverterTests.swift index 0a23d2819a..2d63060221 100644 --- a/apple/TableProTests/Core/Utilities/CsvRowConverterTests.swift +++ b/apple/TableProTests/Core/Utilities/CsvRowConverterTests.swift @@ -97,6 +97,13 @@ struct CsvRowConverterTests { #expect(result == "\"line1\r\nline2\"\n\"a\rb\"\n") } + @Test("A quoted value led by a combining mark is not wrapped a second time") + func combiningMarkLedValueQuotedOnce() { + let converter = makeConverter(columns: ["note"], columnTypes: [.text(rawType: nil)]) + let result = converter.generateCsv(rows: [["\u{301}, x "]], includeHeaders: false) + #expect(Array(result.unicodeScalars) == Array("\"\u{301}, x \"\n".unicodeScalars)) + } + @Test("A tab forces quoting and a formula-like value is copied as it is") func tabQuotingWithoutNeutralising() { let converter = makeConverter(columns: ["note"], columnTypes: [.text(rawType: nil)]) diff --git a/apple/TableProTests/Models/PaginationCapabilityTests.swift b/apple/TableProTests/Models/PaginationCapabilityTests.swift index 569b2ce4a3..bc574065f8 100644 --- a/apple/TableProTests/Models/PaginationCapabilityTests.swift +++ b/apple/TableProTests/Models/PaginationCapabilityTests.swift @@ -58,7 +58,9 @@ struct PaginationCapabilityTests { @Test("Without its own browse, a Cassandra table query reads the leading rows with no OFFSET and no ceiling") func cassandraHostQueryReadsLeadingRows() { - let builder = TableQueryBuilder(databaseType: .cassandra, pagination: .leadingRowsOnly(maximumRows: nil)) + let builder = TableQueryBuilder( + databaseType: .cassandra, pagination: .leadingRowsOnly(maximumRows: nil), dialectQuote: SQLEscaping.quoteIdentifier + ) let query = builder.buildBaseQuery(tableName: "users", schemaName: "shop", limit: 500_000, offset: 3_000) #expect(query == #"SELECT * FROM "shop"."users" LIMIT 500000"#) @@ -72,7 +74,7 @@ struct PaginationCapabilityTests { @Test("A leading-rows table query states a clamped LIMIT and never an OFFSET") func builderNeverOffsets() { - let builder = TableQueryBuilder(databaseType: .cloudflareR2SQL, pagination: leadingRows) + let builder = TableQueryBuilder(databaseType: .cloudflareR2SQL, pagination: leadingRows, dialectQuote: SQLEscaping.quoteIdentifier) let query = builder.buildBaseQuery(tableName: "events", schemaName: "logs", limit: 50_000, offset: 0) #expect(query == #"SELECT * FROM "logs"."events" LIMIT 10000"#) @@ -81,7 +83,7 @@ struct PaginationCapabilityTests { @Test("An offset table query keeps LIMIT and OFFSET") func builderOffsets() { - let builder = TableQueryBuilder(databaseType: .postgresql, pagination: .offset) + let builder = TableQueryBuilder(databaseType: .postgresql, pagination: .offset, dialectQuote: SQLEscaping.quoteIdentifier) let query = builder.buildBaseQuery(tableName: "events", limit: 100, offset: 200) #expect(query.hasSuffix("LIMIT 100 OFFSET 200")) diff --git a/apple/TableProTests/Models/Query/QueryTabBaseQueryTests.swift b/apple/TableProTests/Models/Query/QueryTabBaseQueryTests.swift index 2e6464ecc1..3e05253cff 100644 --- a/apple/TableProTests/Models/Query/QueryTabBaseQueryTests.swift +++ b/apple/TableProTests/Models/Query/QueryTabBaseQueryTests.swift @@ -27,7 +27,7 @@ struct QueryTabBaseQueryTests { pluginDriver: PluginManager.shared.queryBuildingDriver(for: .mssql), dialect: dialect, pagination: .offset, - dialectQuote: quote + dialectQuote: quote ?? { SQLEscaping.quoteIdentifier($0) } ).buildBaseQuery(tableName: "users", schemaName: nil, limit: pageSize, offset: 0) #expect(editorQuery == executed) diff --git a/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.1.md b/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.1.md new file mode 100644 index 0000000000..233e69433c --- /dev/null +++ b/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.1.md @@ -0,0 +1 @@ +MCP clients with read access running writes through browse_table or get_table_statistics with a crafted table name. diff --git a/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.2.md b/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.2.md new file mode 100644 index 0000000000..f51fd0a6b4 --- /dev/null +++ b/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.2.md @@ -0,0 +1 @@ +A SQL export carrying a statement of its own in a cell value, a type name or a table comment when restored. diff --git a/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.3.md b/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.3.md new file mode 100644 index 0000000000..71d8ddf643 --- /dev/null +++ b/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.3.md @@ -0,0 +1 @@ +Opening a crafted table on iPhone or iPad running a second statement on PostgreSQL, Redshift or SQL Server. diff --git a/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.md b/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.md new file mode 100644 index 0000000000..2b2528cee5 --- /dev/null +++ b/apple/changelog.d/+ghsa-58fh-9ghw-9qwv.security.md @@ -0,0 +1 @@ +A name with a quote before an accent mark could run its own SQL when you browsed, searched or exported the table. From b709a4f78f0f3fc83b40a2079bf2eeb9da8ace1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sun, 1 Nov 2026 00:55:23 +0700 Subject: [PATCH 53/85] Merge commit from fork * fix(security): harden the Git commands TablePro runs on its own Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-FRDSB33T. Advisory: GHSA-rhxc-5m3h-r5ch * fix(security): pin PostgreSQL catalog reads to pg_catalog and SYS-qualify Oracle Explain Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CDER3MTT. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CRPAPF23. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): split external statements as the engine does and run them one at a time Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-0WQ6GN07. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-1KLNA54L. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-3CFSRVB9. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-5BPFIX62. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-5H2DRTGD. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-75OXA89M. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-76KHXTER. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-APRDPOPQ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BFGN15RH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ELQB6JC2. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-FIK4MMTE. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-J1NQZAA4. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-NPWEHMEH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-RWV5R6KH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-T03WFSGH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-T2IXRIQZ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TBK6W4KL. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TU0ACYJI. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-VKYFYDT1. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-WZ4W0W5V. Advisory: GHSA-gg4q-84m8-8jhv * docs(changelog): add the security fragments for GHSA-gg4q-84m8-8jhv * fix(security): harden dump tools, shell quoting and SSH host names Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-1D30XTRS. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-2L44V7CH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-3NLPR9WG. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-AX94JCED. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-E9V7ZHHQ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TWLM1VYZ. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-U19XTGQC. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CF3R4T4R. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BZJK0VNE. Advisory: GHSA-94m6-qx4g-cpwg * fix(security): keep credentials on their own host and refuse TLS modes a driver cannot honour Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-EJ1059QD. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-L54ARVVX. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-R0FNK1XO. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KSESNBFN. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KGP1I8T0. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-11I9A28E. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-OP00LFZB. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-6ES8FYVM. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-60V4XYJM. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-YB76IHAI. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-9RQLLRZ0. Advisory: GHSA-j2xw-m3p7-3m64 * fix(security): escape quote characters by code unit in SQL quoters and escapers Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-MQSU9V64. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ANTKKVJ8. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TGUXS7JG. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-JXEUCZFY. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-RU2Q3CD4. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-LDFEMPI5. Advisory: GHSA-58fh-9ghw-9qwv * chore(changelog): name the fragments after the advisory * fix(security): write Oracle Estimated Plan to SYS.PLAN_TABLE$ Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-PEGM4H4N. Advisory: GHSA-hgr8-24vm-5m54 * fix(security): restore a DuckDB Parquet folder through a locked-down instance of its own * test(security): expect the pg_catalog-qualified PostgreSQL cancel statement --- .../MySQLLiteralSpelling.swift | 3 +- .../MySQLStatementKeyword.swift | 3 +- .../TableProSQLGrammar/CQLBatchTracker.swift | 4 + .../TableProSQLGrammar/PLSQLUnitTracker.swift | 38 +++- .../TableProSQLGrammar/SQLCatalogEffect.swift | 43 ++-- .../SQLCodeProjection.swift | 6 +- .../SQLExecutableCommentReadings.swift | 21 +- .../SQLLexicalGrammar.swift | 33 ++- .../SQLLexicalProfile.swift | 63 ++++-- .../SQLLexicalReadings.swift | 13 +- .../TableProSQLGrammar/SQLNonCodeSpan.swift | 37 ++- .../SQLRoutineBodyTracker.swift | 186 +++++++++++++-- .../SQLStatementBoundaryTracking.swift | 8 + .../SQLStatementScanner.swift | 10 + .../SQLUnterminatedStatements.swift | 41 +++- .../SqlBlockStructure.swift | 109 +++++++-- .../TableProSQLGrammar/SqlDollarQuote.swift | 6 +- .../Sources/TableProSQLGrammar/SqlLexer.swift | 108 +++++++-- .../SQLiteStatementTail.swift | 79 +++++++ .../MySQLLiteralSpellingTests.swift | 2 + .../MySQLStatementKeywordTests.swift | 7 + .../SQLStatementSplittingTests.swift | 29 +++ .../SQLWriteClassifierTests.swift | 9 + .../PLSQLUnitTrackerTests.swift | 84 +++++++ .../SQLCaseExpressionEndTests.swift | 69 ++++++ .../SQLCatalogEffectClassifierTests.swift | 17 +- .../SQLExecutableCommentReadingsTests.swift | 15 ++ .../SQLLexicalCorpusTests.swift | 21 ++ .../SQLLexicalReadingsTests.swift | 59 +++++ .../SQLNonCodeSpanTests.swift | 103 ++++++++- .../SQLRoutineBodyNameTests.swift | 213 ++++++++++++++++++ .../SQLScriptBlockTests.swift | 2 +- .../SQLUnterminatedStatementsTests.swift | 27 +++ .../SQL/SQLTokenCursor.swift | 2 +- .../SQL/SQLiteExtensionCallScanner.swift | 27 ++- .../ColumnTypeCorpusRunner.swift | 4 +- .../SQLiteLocalBackend.swift | 36 +-- .../MySQLTemporaryTableNames.swift | 2 +- .../MySQLTemporaryTableNamesTests.swift | 1 + .../LibPQDriverCore.swift | 33 +++ .../LibPQPluginConnection.swift | 14 +- .../SQLiteExecutionBackend.swift | 28 ++- .../SQLiteSingleStatementTests.swift | 89 ++++++++ .../SnowflakeConnection.swift | 19 +- .../SnowflakePluginDriver.swift | 26 ++- .../TableProPluginKit/SQLFeatureLexer.swift | 26 ++- .../SQLStatementSplitting.swift | 8 +- .../QueryTableReferenceResolver.swift | 2 + .../QueryExecutionCoordinator+Batches.swift | 1 + .../Access/DatabaseAccessBridge.swift | 3 +- .../Core/MCP/MCPConnectionBridge+Data.swift | 2 +- .../Core/Plugins/ImportDataSinkAdapter.swift | 3 +- .../Execution/BatchResultMapping.swift | 3 +- .../Services/Query/ExplainResultRouter.swift | 7 +- .../Core/Services/Query/QueryExecutor.swift | 2 +- .../Core/Storage/QueryHistoryStorage.swift | 4 +- .../SQL/CatalogChangeClassifier.swift | 15 +- .../SQL/DynamoDBRequestStatement.swift | 7 +- .../SQL/QueryClassifier+DynamoDB.swift | 21 +- .../Utilities/SQL/QueryClassifier+PLSQL.swift | 8 +- .../Core/Utilities/SQL/QueryClassifier.swift | 59 ++--- .../Core/Utilities/SQL/SQLFileParser.swift | 38 +++- .../Utilities/SQL/SQLParameterExtractor.swift | 4 +- .../Models/Query/ExplainRequest.swift | 4 +- .../Models/Query/QueryHistoryEntry.swift | 2 +- .../Query/QueryHistoryStatementType.swift | 5 +- .../TablePro/Resources/Localizable.xcstrings | 2 + .../Views/Main/MainContentCoordinator.swift | 2 +- .../TransactionRollBackClassifier.swift | 3 +- .../TransactionRollBackClassifierTests.swift | 1 + .../AI/QueryTableReferenceResolverTests.swift | 3 +- .../ExternalStatementGateTests.swift | 40 +++- .../Execution/QueryBatchPlannerTests.swift | 12 +- .../SQL/QueryClassifierDynamoDBTests.swift | 3 +- .../SQL/QueryClassifierHardeningTests.swift | 18 ++ ...eryClassifierInvisibleCharacterTests.swift | 5 +- .../SQL/QueryClassifierLexicalTests.swift | 47 ++++ .../SQL/QueryClassifierPLSQLTests.swift | 1 + .../Utilities/SQL/QueryClassifierTests.swift | 57 ++--- .../SQL/SQLFileParserPLSQLTests.swift | 13 ++ .../Utilities/SQL/SQLFileParserTests.swift | 12 + .../Utilities/SQL/SQLTokenCursorTests.swift | 7 + .../SQL/SQLiteExtensionCallScannerTests.swift | 26 +++ .../Core/Utilities/SQL/SqlLexerTests.swift | 15 +- .../SQLParameterExtractorTests.swift | 5 + .../Models/QueryHistorySourceTests.swift | 21 +- .../SQLLexicalFeatureMappingTests.swift | 49 +++- .../+ghsa-gg4q-84m8-8jhv.security.md | 1 + .../check-mssql-unterminated-statements.sh | 14 +- .../probes/check-sql-lexical-grammar.sh | 2 + apple/scripts/probes/kingbasees-probe.swift | 4 +- .../+ghsa-gg4q-84m8-8jhv.security.md | 1 + .../core/src/sql_syntax/script/lexer.rs | 5 +- .../core/src/sql_syntax/script/script_plan.rs | 3 +- .../src/sql_syntax/script/server_reading.rs | 83 ++++++- .../core/src/sql_syntax/statement_access.rs | 41 ++++ linux/docs/drivers/mysql.md | 2 +- 97 files changed, 2154 insertions(+), 317 deletions(-) create mode 100644 apple/Packages/TableProCore/Sources/TableProSQLiteCore/SQLiteStatementTail.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/PLSQLUnitTrackerTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCaseExpressionEndTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLRoutineBodyNameTests.swift create mode 100644 apple/Plugins/SQLiteDriverTests/SQLiteSingleStatementTests.swift create mode 100644 apple/changelog.d/+ghsa-gg4q-84m8-8jhv.security.md create mode 100644 linux/changelog.d/+ghsa-gg4q-84m8-8jhv.security.md diff --git a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLLiteralSpelling.swift b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLLiteralSpelling.swift index 722a4622ee..3b0aaf5808 100644 --- a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLLiteralSpelling.swift +++ b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLLiteralSpelling.swift @@ -140,11 +140,12 @@ public enum MySQLLiteralSpelling: Equatable, Sendable { return scalars.count } + /// `--` opens a comment only when a space or a control character follows it, U+007F among them. private static func opensDashComment(_ scalars: [Unicode.Scalar], at index: Int) -> Bool { guard index + 1 < scalars.count, scalars[index + 1] == "-" else { return false } guard index + 2 < scalars.count else { return true } let next = scalars[index + 2] - return next == " " || next == "\t" || next == "\n" || next == "\r" + return next <= " " || next == "\u{7F}" } private static func endOfLine(in scalars: [Unicode.Scalar], from start: Int) -> Int { diff --git a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLStatementKeyword.swift b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLStatementKeyword.swift index 35bbf14782..1cb3bae3ff 100644 --- a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLStatementKeyword.swift +++ b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLStatementKeyword.swift @@ -20,7 +20,8 @@ nonisolated public func mysqlLeadingKeyword(_ query: String) -> String { while true { rest = rest.drop(while: { $0.isWhitespace }) if rest.hasPrefix("/*"), !rest.hasPrefix("/*!") { - guard let end = rest.range(of: "*/") else { return "" } + // Past the opener: its `*` closes nothing, so `/*/` is still open. + guard let end = rest.dropFirst(2).range(of: "*/") else { return "" } rest = rest[end.upperBound...] } else if rest.hasPrefix("#") || (rest.hasPrefix("--") && rest.dropFirst(2).first?.isWhitespace != false) { guard let end = rest.firstIndex(where: { $0.isNewline }) else { return "" } diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/CQLBatchTracker.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/CQLBatchTracker.swift index 743f8cd2cb..fa0640b9c1 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/CQLBatchTracker.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/CQLBatchTracker.swift @@ -34,6 +34,10 @@ public struct CQLBatchTracker: SQLStatementBoundaryTracking { true } + public var holdsOpenBlock: Bool { + phase == .insideBatch || phase == .afterCloser + } + public mutating func observeWord(_ word: String) { switch phase { case .start: diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/PLSQLUnitTracker.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/PLSQLUnitTracker.swift index f0c5161f62..74732eb67d 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/PLSQLUnitTracker.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/PLSQLUnitTracker.swift @@ -16,8 +16,11 @@ import Foundation /// Inside a unit the tracker counts the constructs that one bare `END` closes: a block, a subprogram or package body, /// a compound trigger and a `CASE`. A `BEGIN` continues the innermost construct when that construct is still in its /// declaration section, because `DECLARE ... BEGIN ... END` and `PROCEDURE p IS ... BEGIN ... END` each end once. -/// `END IF` and `END LOOP` close constructs that never opened one, and a word after `.` is a member name, never a -/// keyword. A `;` ends the unit only once every construct has closed. +/// `END IF` and `END LOOP` close constructs that never opened one, except between a loop's `FOR` or `WHILE` and its +/// `LOOP`: no statement ends there, so `CASE ... END LOOP` is a `CASE` expression and the `LOOP` that opens the loop. +/// A word after the `.` of `v.END` is a member name, never a keyword. The `..` of a range and the `.` that ends a +/// number name no member, so the word after `1..` or `2.` is a keyword. A `;` ends the unit only once every construct +/// has closed. /// /// The final `;` of a unit belongs to it, except after a trigger whose body is a `CALL`: measured on Oracle 23ai, /// `CREATE TRIGGER ... CALL p(:NEW.a);` is stored INVALID and the same text without the `;` compiles. @@ -50,6 +53,7 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { private static let lessThan = UInt16(UnicodeScalar("<").value) private static let greaterThan = UInt16(UnicodeScalar(">").value) private static let period = UInt16(UnicodeScalar(".").value) + private static let digits = UInt16(UnicodeScalar("0").value)...UInt16(UnicodeScalar("9").value) private static let createModifiers: Set = [ "OR", "REPLACE", "EDITIONABLE", "NONEDITIONABLE", "AND", "RESOLVE", "COMPILE", "NOFORCE", @@ -61,6 +65,10 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { /// One entry per construct a bare `END` will close, true while that construct still waits for its `BEGIN`. private var constructs: [Bool] = [] + + /// The open `CASE`s that have held no `;`, which only a `CASE` statement can. The next `END` closes one of them, + /// whatever word follows it. + private var openCaseExpressions = 0 private var parenDepth = 0 private var pendingEnd = false private var pendingBodyKeyword = false @@ -97,6 +105,18 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { } } + // An `END` still waiting for the word after it closes a construct when the text ends. + public var holdsOpenBlock: Bool { + switch kind { + case .anonymousBlock, .storedUnit: + return constructs.count > (pendingEnd ? 1 : 0) + case .inlineDeclarations: + return !inlineQueryStarted && constructs.count > (pendingEnd ? 1 : 0) + case .sql, .embeddedSourceHeader, .opaque: + return false + } + } + public mutating func observeWord(_ word: String) { previousSymbol = nil guard !inLabel else { return } @@ -153,6 +173,7 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { } public mutating func observeSymbol(_ symbol: UInt16) { + let precedingSymbol = previousSymbol if symbol == Self.lessThan, previousSymbol == Self.lessThan { inLabel = true previousSymbol = nil @@ -174,7 +195,7 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { default: break } - followsPeriod = symbol == Self.period + followsPeriod = symbol == Self.period && !Self.isPeriodOrDigit(precedingSymbol) } public mutating func observeOpaqueToken() { @@ -201,6 +222,7 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { public mutating func observeSemicolon() -> Bool { previousSymbol = nil followsPeriod = false + openCaseExpressions = 0 guard lead == .decided else { return true } switch kind { @@ -229,6 +251,12 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { // MARK: - Private + /// A name that ends in a digit arrives as a word, which leaves no preceding symbol, so `t1.END` is still a member. + private static func isPeriodOrDigit(_ symbol: UInt16?) -> Bool { + guard let symbol else { return false } + return symbol == period || digits.contains(symbol) + } + private var tracksConstructs: Bool { guard lead == .decided else { return false } switch kind { @@ -340,6 +368,7 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { constructs.append(true) case "CASE": constructs.append(false) + openCaseExpressions += 1 case "END": pendingEnd = true case "PROCEDURE", "FUNCTION": @@ -353,7 +382,7 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { /// Returns whether `word` was consumed by the `END` before it. private mutating func closeEnd(followedBy word: String) -> Bool { - switch SqlBlockStructure.endingFollowedBy(word) { + switch SqlBlockStructure.endingFollowedBy(word, closesCaseExpression: openCaseExpressions > 0) { case .closesControlFlow: return true case .closesCaseStatement: @@ -387,5 +416,6 @@ public struct PLSQLUnitTracker: SQLStatementBoundaryTracking { private mutating func closeConstruct() { guard !constructs.isEmpty else { return } constructs.removeLast() + openCaseExpressions = max(0, openCaseExpressions - 1) } } diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift index a899b93894..2e44afde28 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift @@ -65,7 +65,7 @@ public enum SQLCatalogEffectClassifier { } private static func textEffect(of sql: String, grammar: SQLLexicalGrammar) -> SQLCatalogEffect { - let trimmed = StatementBlank.trimming(SQLLeadingComments.stripping(sql)) + let trimmed = StatementBlank.trimming(SQLLeadingComments.stripping(sql, grammar: grammar)) guard !trimmed.isEmpty else { return .none } if SQLProceduralBlock.runs(trimmed, grammar: grammar) { return .opaque @@ -215,20 +215,24 @@ public enum SQLLeadingComments { public static let lineCommentTerminators: Set = ["\n", "\r", "\r\n"] - public static func stripping(_ sql: String) -> String { - var remaining = sql[...] - while true { - let trimmed = StatementBlank.trimmingLeading(remaining) - if trimmed.hasPrefix("--") { - guard let lineBreak = trimmed.firstIndex(where: endsLineComment) else { return "" } - remaining = trimmed[trimmed.index(after: lineBreak)...] - } else if trimmed.hasPrefix("/*"), !startsConditionalComment(trimmed) { - guard let close = trimmed.range(of: "*/") else { return "" } - remaining = trimmed[close.upperBound...] - } else { - return String(trimmed) + /// `sql` from its first token, past the blanks and the comments `grammar` reads, so a comment ends exactly where + /// the engine ends it. An executable comment is code and stays. + public static func stripping(_ sql: String, grammar: SQLLexicalGrammar) -> String { + let text = sql as NSString + let length = text.length + var index = 0 + while index < length { + let blank = StatementBlank.blankLength(in: text, at: index) + if blank > 0 { + index += blank + continue + } + guard let span = SQLNonCodeSpan.span(at: index, in: text, grammar: grammar), span.kind.isComment else { + break } + index = max(span.end, index + 1) } + return text.substring(from: min(index, length)) } public static func startsConditionalComment(_ text: Substring) -> Bool { @@ -261,20 +265,21 @@ public enum SQLProceduralBlock { } // A block may open after <
+ + + Move Column Left + <Alt>Left + + + + + Move Column Right + <Alt>Right + + Insert Row From 15998fa96a17f309e8ba1842e2adc0040f6f783b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Nam=20Long?= Date: Sun, 1 Nov 2026 15:10:50 +0700 Subject: [PATCH 76/85] feat(linux-structure): show and copy the CREATE INDEX for one index (#3964) --- linux/ROADMAP.md | 2 +- linux/changelog.d/3964.added.md | 1 + linux/crates/app/src/ui/diagram/model.rs | 2 + linux/crates/app/src/ui/structure_tab/imp.rs | 3 + .../app/src/ui/structure_tab/index_draft.rs | 1 + .../src/ui/structure_tab/index_handlers.rs | 19 ++- .../app/src/ui/structure_tab/index_object.rs | 10 ++ .../app/src/ui/structure_tab/indexes.rs | 45 +++++++ .../app/src/ui/structure_tab/indexes_input.rs | 2 + linux/crates/app/src/ui/structure_tab/mod.rs | 11 ++ .../crates/app/src/ui/structure_tab/tests.rs | 63 +++++++++ linux/crates/app/src/ui/workspace/mod.rs | 5 +- .../crates/app/src/ui/workspace/structure.rs | 2 +- .../app/src/ui/workspace/tab_factory.rs | 2 + linux/crates/core/src/ddl/tests.rs | 1 + linux/crates/core/src/meta/index_info.rs | 6 + linux/crates/drivers/cassandra/src/catalog.rs | 8 +- .../drivers/clickhouse/src/catalog/mod.rs | 18 ++- .../crates/drivers/clickhouse/src/dialect.rs | 14 +- .../clickhouse/tests/integration/metadata.rs | 59 +++++++++ .../crates/drivers/duckdb/src/catalog/mod.rs | 10 +- linux/crates/drivers/mongodb/src/catalog.rs | 1 + linux/crates/drivers/mssql/src/ddl.rs | 1 + linux/crates/drivers/mssql/src/metadata.rs | 121 ++++++++++++++++-- .../mssql/tests/integration/metadata.rs | 64 +++++++++ linux/crates/drivers/mysql/src/catalog/mod.rs | 2 +- linux/crates/drivers/mysql/src/ddl/mod.rs | 2 + linux/crates/drivers/mysql/src/metadata.rs | 112 ++++++++++++---- .../mysql/tests/integration/flavour_suite.rs | 66 ++++++++++ linux/crates/drivers/postgres/src/ddl.rs | 3 + .../drivers/postgres/src/metadata/indexes.rs | 7 +- .../postgres/tests/integration/metadata.rs | 4 + linux/crates/drivers/sqlite/src/ddl/tests.rs | 2 + linux/crates/drivers/sqlite/src/metadata.rs | 19 +++ .../sqlite/tests/integration/metadata.rs | 46 +++++++ .../test-support/src/conformance/cases/ddl.rs | 74 +++++++++++ .../src/conformance/ddl_suite_macro.rs | 6 + linux/docs/testing.md | 1 + linux/po/vi.po | 12 ++ 39 files changed, 770 insertions(+), 57 deletions(-) create mode 100644 linux/changelog.d/3964.added.md diff --git a/linux/ROADMAP.md b/linux/ROADMAP.md index a6e245c7f5..ffdb27ffe5 100644 --- a/linux/ROADMAP.md +++ b/linux/ROADMAP.md @@ -63,6 +63,7 @@ The app runs on GNOME 50 ([ADR 0015](docs/decisions/0015-gnome-50-platform-floor | Capability | Detail | |---|---| | Table definition | Columns, indexes, foreign keys and column comments | +| Index statements | Show CREATE INDEX and Copy CREATE INDEX on each index the table has: the server's own text on PostgreSQL, SQLite and DuckDB, and written from the catalog on MySQL, MariaDB, SQL Server, ClickHouse (as ALTER TABLE ADD INDEX), Cassandra and ScyllaDB. An index with no statement of its own, such as a MySQL primary key or a ClickHouse sorting key, offers none | | Changes | Create, rename and drop tables; add, drop and rename columns, and change their type, nullability, default, comment, primary key and auto-increment; add and drop indexes (column keys, unique, descending) and foreign keys. ClickHouse creates no tables, indexes or foreign keys and changes no primary key or auto-increment; SQL Server changes no auto-increment; SQLite keeps no column comments; Cassandra and ScyllaDB take no default or NOT NULL, and rename only key columns | | Review | The SQL is previewed, and a save that loses data or can stop halfway is reviewed before it runs | | SQLite rebuilds | Changes SQLite cannot ALTER rebuild the table in one transaction, keeping its rows, indexes, triggers, the views and triggers that name it and its AUTOINCREMENT counter, with foreign keys checked before commit. SQLite itself checks the views and triggers made again; a trigger that lists a dropped column or inserts by position is refused before Save | @@ -171,7 +172,6 @@ Not built yet, in no order. - Edit binary columns from and to a file. They are read-only today, as are array, spatial, composite and variant columns. - Time and timestamp pickers. Dates have a calendar; times and timestamps are typed. - Functions, procedures, sequences and triggers in the sidebar. The drivers already list the first three. -- The CREATE INDEX statement for one index. Show CREATE TABLE lists them after the table on PostgreSQL, SQL Server and SQLite (with the triggers on SQLite). - Reorder columns in the structure tab by dragging, with the ALTER previewed. ### Release and project diff --git a/linux/changelog.d/3964.added.md b/linux/changelog.d/3964.added.md new file mode 100644 index 0000000000..288f98135a --- /dev/null +++ b/linux/changelog.d/3964.added.md @@ -0,0 +1 @@ +Show or copy the CREATE INDEX statement for one index from the Indexes page of the structure tab. diff --git a/linux/crates/app/src/ui/diagram/model.rs b/linux/crates/app/src/ui/diagram/model.rs index a65c2d47c1..476ef61823 100644 --- a/linux/crates/app/src/ui/diagram/model.rs +++ b/linux/crates/app/src/ui/diagram/model.rs @@ -339,6 +339,7 @@ pub(crate) mod tests { unique: true, primary: false, predicate: None, + definition: None, }); let mut partial = badge.clone(); partial.table = TableRef::new(Some("public".to_owned()), "partial"); @@ -385,6 +386,7 @@ pub(crate) mod tests { unique: true, primary: false, predicate: None, + definition: None, }); let mut shared = seats.clone(); shared.table = TableRef::new(Some("public".to_owned()), "shared"); diff --git a/linux/crates/app/src/ui/structure_tab/imp.rs b/linux/crates/app/src/ui/structure_tab/imp.rs index 500d4547cc..5468ab9f7d 100644 --- a/linux/crates/app/src/ui/structure_tab/imp.rs +++ b/linux/crates/app/src/ui/structure_tab/imp.rs @@ -131,6 +131,9 @@ impl ObjectImpl for StructureTab { Signal::builder("alert") .param_types([String::static_type(), String::static_type()]) .build(), + Signal::builder("open-in-editor") + .param_types([String::static_type()]) + .build(), ] }) } diff --git a/linux/crates/app/src/ui/structure_tab/index_draft.rs b/linux/crates/app/src/ui/structure_tab/index_draft.rs index e24fe07eb9..9dc2c25373 100644 --- a/linux/crates/app/src/ui/structure_tab/index_draft.rs +++ b/linux/crates/app/src/ui/structure_tab/index_draft.rs @@ -65,6 +65,7 @@ impl IndexDraft { unique: self.unique, primary: false, predicate: None, + definition: None, }) } diff --git a/linux/crates/app/src/ui/structure_tab/index_handlers.rs b/linux/crates/app/src/ui/structure_tab/index_handlers.rs index 28d47be261..4309972ff3 100644 --- a/linux/crates/app/src/ui/structure_tab/index_handlers.rs +++ b/linux/crates/app/src/ui/structure_tab/index_handlers.rs @@ -1,5 +1,5 @@ use super::index_object::IndexObject; -use super::{IndexesInput, StructureEffects, StructureModel}; +use super::{IndexesInput, StructureEffects, StructureModel, StructureTabOutput}; impl StructureModel { pub(super) fn on_indexes(&mut self, input: IndexesInput, fx: &mut StructureEffects) { @@ -14,6 +14,23 @@ impl StructureModel { self.recompute_dirty_state(fx); } } + IndexesInput::CopyStatement(index) => { + let (Some(sql), Some(tab)) = (index.create_statement(), self.this.upgrade()) else { + return; + }; + let report = std::cell::RefCell::new(None); + crate::ui::clipboard::copy_text(&tab, sql, |message| { + report.replace(Some(message.to_owned())); + }); + if let Some(message) = report.into_inner() { + fx.output(StructureTabOutput::ShowToast(message)); + } + } + IndexesInput::ShowStatement(index) => { + if let Some(sql) = index.create_statement() { + fx.output(StructureTabOutput::OpenInEditor(sql)); + } + } } } } diff --git a/linux/crates/app/src/ui/structure_tab/index_object.rs b/linux/crates/app/src/ui/structure_tab/index_object.rs index 360a6f4946..25e0368bb9 100644 --- a/linux/crates/app/src/ui/structure_tab/index_object.rs +++ b/linux/crates/app/src/ui/structure_tab/index_object.rs @@ -36,6 +36,7 @@ static UNSET: IndexInfo = IndexInfo { unique: false, primary: false, predicate: None, + definition: None, }; impl IndexObject { @@ -48,4 +49,13 @@ impl IndexObject { pub(crate) fn info(&self) -> &IndexInfo { self.imp().info.get().unwrap_or(&UNSET) } + + /// The statement that creates the index as the server has it, ended + /// the way the editor runs it. An index added in this tab has none. + pub(crate) fn create_statement(&self) -> Option { + self.info() + .definition + .as_deref() + .map(|definition| format!("{definition};")) + } } diff --git a/linux/crates/app/src/ui/structure_tab/indexes.rs b/linux/crates/app/src/ui/structure_tab/indexes.rs index 5cd2dbbaa5..75f0a853c0 100644 --- a/linux/crates/app/src/ui/structure_tab/indexes.rs +++ b/linux/crates/app/src/ui/structure_tab/indexes.rs @@ -1,6 +1,7 @@ //! The index rows of the Structure tab's Indexes page. use gtk4 as gtk; +use gtk4::gio; use libadwaita as adw; use libadwaita::prelude::*; @@ -63,9 +64,52 @@ pub(super) fn build_index_row( }); row.add_suffix(&remove_button); + if idx.definition.is_some() { + row.add_suffix(&statement_menu(object, sender)); + } + row } +/// Show and copy the statement that creates the index, which only an +/// index the table already has carries. +fn statement_menu(object: &IndexObject, sender: &StructureTabSender) -> gtk::MenuButton { + let menu = gio::Menu::new(); + menu.append( + Some(&crate::i18n::gettext("Show _CREATE INDEX")), + Some("index.show-create"), + ); + menu.append( + Some(&crate::i18n::gettext("_Copy CREATE INDEX")), + Some("index.copy-create"), + ); + let actions = gio::SimpleActionGroup::new(); + let entry = |name: &str, message: fn(IndexObject) -> IndexesInput| { + let (index, sender) = (object.downgrade(), sender.clone()); + gio::ActionEntry::::builder(name) + .activate(move |_, _, _| { + if let Some(index) = index.upgrade() { + sender.input(message(index)); + } + }) + .build() + }; + actions.add_action_entries([ + entry("show-create", IndexesInput::ShowStatement), + entry("copy-create", IndexesInput::CopyStatement), + ]); + let button = gtk::MenuButton::builder() + .icon_name(crate::ui::icons::VIEW_MORE) + .valign(gtk::Align::Center) + .menu_model(&menu) + .build(); + button.add_css_class("flat"); + crate::ui::a11y::describe_icon_button(&button, &crate::i18n::gettext("Index Options"), None); + button.set_widget_name("index-statement"); + button.insert_action_group("index", Some(&actions)); + button +} + /// The keys an index covers, as the subtitle reads them: each column or /// expression in key order, `DESC` where it sorts descending, and the /// WHERE of a partial index after them. @@ -108,6 +152,7 @@ mod tests { unique: false, primary: false, predicate: predicate.map(str::to_owned), + definition: None, } } diff --git a/linux/crates/app/src/ui/structure_tab/indexes_input.rs b/linux/crates/app/src/ui/structure_tab/indexes_input.rs index fa8627c8d5..0fca6fd6d9 100644 --- a/linux/crates/app/src/ui/structure_tab/indexes_input.rs +++ b/linux/crates/app/src/ui/structure_tab/indexes_input.rs @@ -7,4 +7,6 @@ use super::index_object::IndexObject; pub(crate) enum IndexesInput { AddIndex(IndexInfo), RemoveIndex(IndexObject), + CopyStatement(IndexObject), + ShowStatement(IndexObject), } diff --git a/linux/crates/app/src/ui/structure_tab/mod.rs b/linux/crates/app/src/ui/structure_tab/mod.rs index a25c08511a..1013ac2b85 100644 --- a/linux/crates/app/src/ui/structure_tab/mod.rs +++ b/linux/crates/app/src/ui/structure_tab/mod.rs @@ -355,6 +355,14 @@ impl StructureTab { ) } + pub fn connect_open_in_editor(&self, f: F) -> glib::SignalHandlerId { + self.connect_closure( + "open-in-editor", + false, + glib::closure_local!(move |tab: &Self, sql: String| f(tab, sql)), + ) + } + /// A read of the table's definition started or ended. pub fn connect_loading_changed(&self, f: F) -> glib::SignalHandlerId { self.connect_closure( @@ -385,6 +393,7 @@ impl StructureTab { StructureTabOutput::ShowToast(text) => self.emit_by_name::<()>("toast", &[&text]), StructureTabOutput::ShowAlert { title, body } => self.emit_by_name::<()>("alert", &[&title, &body]), StructureTabOutput::LoadingChanged(loading) => self.emit_by_name::<()>("loading-changed", &[&loading]), + StructureTabOutput::OpenInEditor(sql) => self.emit_by_name::<()>("open-in-editor", &[&sql]), } } } @@ -472,6 +481,8 @@ pub(crate) enum StructureTabOutput { }, /// A read of the definition started or ended, for the tab bar. LoadingChanged(bool), + /// SQL the user asked to see in an editor tab. + OpenInEditor(String), } impl StructureModel { diff --git a/linux/crates/app/src/ui/structure_tab/tests.rs b/linux/crates/app/src/ui/structure_tab/tests.rs index 3c28fa2a04..5e33335edd 100644 --- a/linux/crates/app/src/ui/structure_tab/tests.rs +++ b/linux/crates/app/src/ui/structure_tab/tests.rs @@ -498,6 +498,7 @@ fn index_fetch_failure_keeps_loaded_indexes() { unique: true, primary: false, predicate: None, + definition: None, }], ..definition("id") }; @@ -1556,6 +1557,7 @@ fn icon_buttons_are_labelled_without_accelerator_hints() { unique: false, primary: false, predicate: None, + definition: Some("CREATE INDEX users_team_idx ON public.users USING btree (team_id)".to_owned()), }], foreign_keys: vec![tablepro_core::ForeignKeyInfo { name: "users_team_fk".to_owned(), @@ -1946,3 +1948,64 @@ fn a_failed_character_set_read_leaves_the_tab_editable() { assert!(imp.save_button.is_sensitive()); assert_eq!(imp.pending_label.label(), "2 unsaved changes"); } + +fn email_index(name: &str, definition: Option<&str>) -> tablepro_core::IndexInfo { + tablepro_core::IndexInfo { + name: name.to_owned(), + keys: vec![tablepro_core::meta::IndexKey::Column { + name: "email".to_owned(), + descending: false, + }], + unique: true, + primary: false, + predicate: None, + definition: definition.map(str::to_owned), + } +} + +/// An index the table has offers its statement to copy or open; one +/// only drafted in the tab has none to offer. +#[gtk4::test] +fn a_saved_index_shows_and_copies_its_create_statement() { + let statement = "CREATE UNIQUE INDEX users_email ON public.users USING btree (email)"; + let mut table = definition("email"); + table.indexes = vec![email_index("users_email", Some(statement))]; + let mut script = postgres_script(); + script.fetch_table_definition.always(FakeReply::ok(table)); + let harness = Harness::open(StructureMode::Edit, script, []); + wait_until(WAIT, || harness.page().as_deref() == Some("editor")).expect("the structure"); + let toasts = toasts(harness.tab()); + let opened = std::rc::Rc::new(std::cell::RefCell::new(Vec::new())); + let sink = opened.clone(); + harness + .tab() + .connect_open_in_editor(move |_, sql| sink.borrow_mut().push(sql)); + harness + .tab() + .send(IndexesInput::AddIndex(email_index("users_email_draft", None))); + drain_main_context(); + + let menus: Vec = descendants(harness.tab()) + .into_iter() + .filter(|widget| widget.widget_name() == "index-statement") + .collect(); + assert_eq!(menus.len(), 1, "only the saved index offers a statement"); + menus[0] + .activate_action("index.copy-create", None) + .expect("the copy action"); + menus[0] + .activate_action("index.show-create", None) + .expect("the show action"); + drain_main_context(); + + let copied = harness + .tab() + .clipboard() + .content() + .and_then(|content| content.value(String::static_type()).ok()) + .and_then(|value| value.get::().ok()); + let expected = format!("{statement};"); + assert_eq!(copied.as_deref(), Some(expected.as_str())); + assert_eq!(*toasts.borrow(), ["Copied to clipboard"]); + assert_eq!(*opened.borrow(), [expected]); +} diff --git a/linux/crates/app/src/ui/workspace/mod.rs b/linux/crates/app/src/ui/workspace/mod.rs index f5443be7a1..a2187c59a4 100644 --- a/linux/crates/app/src/ui/workspace/mod.rs +++ b/linux/crates/app/src/ui/workspace/mod.rs @@ -604,7 +604,8 @@ pub(crate) enum WorkspaceInput { schema: Option, table: String, }, - ShowCreateTableLoaded(String), + /// SQL the user asked to see, in a new editor tab of its own. + OpenInEditor(String), DropTablePrompt { schema: Option, table: String, @@ -1267,7 +1268,7 @@ impl WorkspaceModel { WorkspaceInput::ShowDiagram { schema } => self.show_diagram(schema, Vec::new()), WorkspaceInput::EditStructureTab { schema, table } => self.on_edit_structure_tab(schema, table), WorkspaceInput::ShowCreateTableForExisting { schema, table } => self.on_show_create_table(schema, table), - WorkspaceInput::ShowCreateTableLoaded(sql) => self.open_editor_tab(Some(sql)), + WorkspaceInput::OpenInEditor(sql) => self.open_editor_tab(Some(sql)), WorkspaceInput::DropTablePrompt { schema, table } => self.on_drop_table_prompt(schema, table), WorkspaceInput::DropTableChecked { table, referencing } => self.on_drop_table_checked(table, &referencing), WorkspaceInput::DropTableConfirmed { schema, table } => self.on_drop_table_confirmed(schema, table), diff --git a/linux/crates/app/src/ui/workspace/structure.rs b/linux/crates/app/src/ui/workspace/structure.rs index 2e7ccb5bb9..e5ab9a484f 100644 --- a/linux/crates/app/src/ui/workspace/structure.rs +++ b/linux/crates/app/src/ui/workspace/structure.rs @@ -323,7 +323,7 @@ impl WorkspaceModel { let message = match settled(result) { Ok((definition, dialect)) => match create_table_sql(&definition, &*dialect) { Ok(Some(sql)) => { - input.deliver(WorkspaceInput::ShowCreateTableLoaded(sql)); + input.deliver(WorkspaceInput::OpenInEditor(sql)); return; } Ok(None) => crate::i18n::gettext_f("Table {table} has no columns.", &[("table", &table)]), diff --git a/linux/crates/app/src/ui/workspace/tab_factory.rs b/linux/crates/app/src/ui/workspace/tab_factory.rs index 534da9791f..3c81cfa4df 100644 --- a/linux/crates/app/src/ui/workspace/tab_factory.rs +++ b/linux/crates/app/src/ui/workspace/tab_factory.rs @@ -420,6 +420,8 @@ fn connect_structure(structure: &StructureTab, tab_id: Uuid, input: &WorkspaceSe structure.connect_toast(move |_, text| toast.deliver(WorkspaceInput::ShowToast(text))); let alert = input.clone(); structure.connect_alert(move |_, title, body| alert.deliver(WorkspaceInput::ShowAlert { title, body })); + let open = input.clone(); + structure.connect_open_in_editor(move |_, sql| open.deliver(WorkspaceInput::OpenInEditor(sql))); } /// The diagram's signals, as the workspace's inputs: a table it names diff --git a/linux/crates/core/src/ddl/tests.rs b/linux/crates/core/src/ddl/tests.rs index 3a8873fbda..a3117ea605 100644 --- a/linux/crates/core/src/ddl/tests.rs +++ b/linux/crates/core/src/ddl/tests.rs @@ -70,6 +70,7 @@ fn index(name: &str, columns: &[&str]) -> IndexInfo { unique: false, primary: false, predicate: None, + definition: None, } } diff --git a/linux/crates/core/src/meta/index_info.rs b/linux/crates/core/src/meta/index_info.rs index e69bf0bc6e..9f66628a37 100644 --- a/linux/crates/core/src/meta/index_info.rs +++ b/linux/crates/core/src/meta/index_info.rs @@ -14,6 +14,11 @@ pub struct IndexInfo { /// A partial index's WHERE clause. An index with one cannot /// identify a row, because it does not cover every row. pub predicate: Option, + /// The statement that creates this index alone: the server's own + /// text where the engine keeps one, otherwise written by the driver + /// from its catalog, with no closing semicolon. `None` for an index + /// only a constraint makes. + pub definition: Option, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -69,6 +74,7 @@ mod tests { unique, primary: false, predicate: predicate.map(str::to_owned), + definition: None, } } diff --git a/linux/crates/drivers/cassandra/src/catalog.rs b/linux/crates/drivers/cassandra/src/catalog.rs index 7c69a6d527..f366eda4ac 100644 --- a/linux/crates/drivers/cassandra/src/catalog.rs +++ b/linux/crates/drivers/cassandra/src/catalog.rs @@ -320,16 +320,18 @@ fn primary_index(table: &CatalogTable, columns: &[ColumnRow]) -> Option IndexInfo { +fn secondary_index(table: &CatalogTable, index: &IndexRow) -> IndexInfo { IndexInfo { name: index.name.clone(), keys: vec![index_key(&index.target)], unique: false, primary: false, predicate: None, + definition: Some(create_index(table, index)), } } @@ -338,7 +340,7 @@ pub(crate) async fn fetch_indexes(link: &CqlLink, table: &CatalogTable) -> Resul let indexes = index_rows(link, table).await?; Ok(primary_index(table, &columns) .into_iter() - .chain(indexes.iter().map(secondary_index)) + .chain(indexes.iter().map(|index| secondary_index(table, index))) .collect()) } @@ -470,7 +472,7 @@ pub(crate) async fn fetch_table_definition( }), indexes: primary_index(table, &columns) .into_iter() - .chain(indexes.iter().map(secondary_index)) + .chain(indexes.iter().map(|index| secondary_index(table, index))) .collect(), columns: columns.iter().map(ColumnRow::definition).collect(), foreign_keys: Vec::new(), diff --git a/linux/crates/drivers/clickhouse/src/catalog/mod.rs b/linux/crates/drivers/clickhouse/src/catalog/mod.rs index 8368a02e73..52d9f372a6 100644 --- a/linux/crates/drivers/clickhouse/src/catalog/mod.rs +++ b/linux/crates/drivers/clickhouse/src/catalog/mod.rs @@ -11,6 +11,7 @@ pub(crate) use clickhouse_column_row::ClickHouseColumnRow; pub(crate) use table_definition::fetch_table_definition; use crate::call_client::CallClient; +use crate::dialect::quote; use crate::key_expression::{columns_in_key_order, index_keys}; use crate::result_reader::{CatalogRow, read_table}; use clickhouse_column_row::COLUMN_SELECT; @@ -76,19 +77,30 @@ pub(crate) async fn fetch_indexes(calls: &CallClient, table: &CatalogTable) -> R unique: false, primary: true, predicate: None, + definition: None, }); } - let sql = "SELECT name, expr FROM system.data_skipping_indices \ + let sql = "SELECT name, expr, type_full, granularity FROM system.data_skipping_indices \ WHERE database = {db:String} AND table = {tbl:String} ORDER BY name"; let skipping = read_table(table.bind(calls.raw(sql)?), None).await?; for row in &skipping.rows { let row = CatalogRow(row); + let (name, expression) = (row.text(0)?, row.text(1)?); + let definition = format!( + "ALTER TABLE {}.{} ADD INDEX {} {expression} TYPE {} GRANULARITY {}", + quote(&table.database), + quote(&table.name), + quote(&name), + row.text(2)?, + row.optional_u64(3)?.unwrap_or(1) + ); indexes.push(IndexInfo { - name: row.text(0)?, - keys: index_keys(&row.text(1)?), + keys: index_keys(&expression), + name, unique: false, primary: false, predicate: None, + definition: Some(definition), }); } Ok(indexes) diff --git a/linux/crates/drivers/clickhouse/src/dialect.rs b/linux/crates/drivers/clickhouse/src/dialect.rs index e3b22eaad0..1379f42e29 100644 --- a/linux/crates/drivers/clickhouse/src/dialect.rs +++ b/linux/crates/drivers/clickhouse/src/dialect.rs @@ -82,11 +82,7 @@ impl SqlDialect for ClickHouseDialect { } fn quote_identifier(&self, name: &str) -> String { - let mut quoted = String::with_capacity(name.len() + 2); - quoted.push('`'); - escape_into(name, &mut quoted); - quoted.push('`'); - quoted + quote(name) } fn placeholder(&self, ordinal: usize, value: Value, target: BindTarget<'_>) -> Result { @@ -224,6 +220,14 @@ impl SqlDialect for ClickHouseDialect { } } +pub(crate) fn quote(name: &str) -> String { + let mut quoted = String::with_capacity(name.len() + 2); + quoted.push('`'); + escape_into(name, &mut quoted); + quoted.push('`'); + quoted +} + fn escape_into(text: &str, out: &mut String) { for character in text.chars() { if ESCAPED.contains(&character) { diff --git a/linux/crates/drivers/clickhouse/tests/integration/metadata.rs b/linux/crates/drivers/clickhouse/tests/integration/metadata.rs index 1b9788ee3f..ccbe220aa5 100644 --- a/linux/crates/drivers/clickhouse/tests/integration/metadata.rs +++ b/linux/crates/drivers/clickhouse/tests/integration/metadata.rs @@ -297,3 +297,62 @@ async fn clickhouse_ilike_is_case_insensitive() { ); connection.close().await.expect("a close"); } + +/// A skipping index's statement, run after the index is dropped, makes +/// the same index again with its type and granularity. The sorting key +/// has no statement of its own. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn skipping_index_statements_recreate_their_indexes() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + let name = unique("skipping"); + setup( + connection.as_ref(), + &[&format!( + "CREATE TABLE {} (id UInt32, s String, \ + INDEX by_s lower(s) TYPE bloom_filter(0.01) GRANULARITY 4, INDEX by_id id TYPE minmax GRANULARITY 1) \ + ENGINE = MergeTree ORDER BY id", + quoted(&name) + )], + ) + .await; + let table = table(&name); + let indexes = connection + .fetch_indexes(&table, within(CALL)) + .await + .expect("the indexes"); + + assert!( + indexes + .iter() + .filter(|index| index.primary) + .all(|index| index.definition.is_none()), + "{indexes:#?}" + ); + let by_s = indexes.iter().find(|index| index.name == "by_s").expect("by_s"); + assert_eq!( + by_s.definition.as_deref(), + Some( + format!( + "ALTER TABLE `{DATABASE}`.`{name}` ADD INDEX `by_s` lower(s) TYPE bloom_filter(0.01) GRANULARITY 4" + ) + .as_str() + ) + ); + for index in indexes.iter().filter(|index| !index.primary) { + let statement = index.definition.clone().expect("a statement"); + let drop = format!("ALTER TABLE {} DROP INDEX `{}`", quoted(&name), index.name); + setup(connection.as_ref(), &[drop.as_str(), statement.as_str()]).await; + let remade = connection + .fetch_indexes(&table, within(CALL)) + .await + .expect("the indexes"); + assert_eq!( + remade.iter().find(|remade| remade.name == index.name), + Some(index), + "{statement}" + ); + } + connection.close().await.expect("a close"); +} diff --git a/linux/crates/drivers/duckdb/src/catalog/mod.rs b/linux/crates/drivers/duckdb/src/catalog/mod.rs index 06063f6b3f..b7f096d8d2 100644 --- a/linux/crates/drivers/duckdb/src/catalog/mod.rs +++ b/linux/crates/drivers/duckdb/src/catalog/mod.rs @@ -258,6 +258,7 @@ pub(crate) fn fetch_indexes(link: &Link, catalog: &str, table: &TableRef) -> Res unique: true, primary: kind == "PRIMARY KEY", predicate: None, + definition: None, }) .collect(); let columns: HashSet = fetch_columns(link, catalog, table)? @@ -268,7 +269,7 @@ pub(crate) fn fetch_indexes(link: &Link, catalog: &str, table: &TableRef) -> Res link, // DuckDB lists an index's keys as the text of a list. "SELECT index_name, CAST(is_unique AS VARCHAR), CAST(is_primary AS VARCHAR), \ - array_to_string(CAST(expressions AS VARCHAR[]), chr(31)) \ + array_to_string(CAST(expressions AS VARCHAR[]), chr(31)), sql \ FROM duckdb_indexes() WHERE database_name = ? AND schema_name = ? AND table_name = ? \ ORDER BY index_name", &[catalog, schema_of(table), &table.name], @@ -283,11 +284,18 @@ pub(crate) fn fetch_indexes(link: &Link, catalog: &str, table: &TableRef) -> Res unique: flag(row, 1), primary: flag(row, 2), predicate: None, + definition: statement(&text(row, 4)), } })); Ok(indexes) } +/// DuckDB keeps the text the index was created with, semicolon and all. +fn statement(sql: &str) -> Option { + let sql = sql.trim().trim_end_matches(';').trim_end(); + (!sql.is_empty()).then(|| sql.to_owned()) +} + /// One key of an index: a column where the text names one, an /// expression otherwise. fn index_key(text: &str, columns: &HashSet) -> IndexKey { diff --git a/linux/crates/drivers/mongodb/src/catalog.rs b/linux/crates/drivers/mongodb/src/catalog.rs index 74acb83b84..5c91865639 100644 --- a/linux/crates/drivers/mongodb/src/catalog.rs +++ b/linux/crates/drivers/mongodb/src/catalog.rs @@ -300,6 +300,7 @@ fn index_info(entry: &Document) -> Option { name, keys, predicate, + definition: None, }) } diff --git a/linux/crates/drivers/mssql/src/ddl.rs b/linux/crates/drivers/mssql/src/ddl.rs index 1badae38cd..066514b5a7 100644 --- a/linux/crates/drivers/mssql/src/ddl.rs +++ b/linux/crates/drivers/mssql/src/ddl.rs @@ -653,6 +653,7 @@ mod tests { unique: false, primary: false, predicate: Some("([sku] IS NOT NULL)".to_owned()), + definition: None, }]; draft.foreign_keys = vec![tablepro_core::ForeignKeyInfo { name: "lines_order_fk".to_owned(), diff --git a/linux/crates/drivers/mssql/src/metadata.rs b/linux/crates/drivers/mssql/src/metadata.rs index f098234f89..85faf4039c 100644 --- a/linux/crates/drivers/mssql/src/metadata.rs +++ b/linux/crates/drivers/mssql/src/metadata.rs @@ -14,6 +14,7 @@ use crate::catalog::{ COLUMNS_SQL, Cells, INBOUND_FOREIGN_KEYS_SQL, MssqlColumnRow, PRIMARY_KEY_SQL, column_definition, column_info, inbound_foreign_keys, primary_key, }; +use crate::dialect::quote; use crate::in_band_request::{Interrupt, Watch}; use crate::physical::MssqlClient; use crate::workspace_call::{catalogue_rows, text_param}; @@ -36,8 +37,15 @@ const OBJECTS_SQL: &str = "SELECT SCHEMA_NAME(o.schema_id), o.name, RTRIM(o.type FROM sys.objects o \ WHERE o.type IN ('U', 'V', 'SO', 'FN', 'IF', 'TF', 'FS', 'FT', 'P', 'PC') AND o.is_ms_shipped = 0"; +/// The INCLUDE columns come back as one quoted list, which `.value` +/// reads out of the XML without escaping `&` or `<` in a name. const INDEXES_SQL: &str = "SELECT i.name, CAST(i.is_unique AS int), CAST(i.is_primary_key AS int), c.name, \ - CAST(ic.is_descending_key AS int), i.filter_definition \ + CAST(ic.is_descending_key AS int), i.filter_definition, i.type_desc, \ + CAST(i.is_unique_constraint AS int), \ + STUFF((SELECT N', ' + QUOTENAME(inc.name) FROM sys.index_columns iic \ + JOIN sys.columns inc ON inc.object_id = iic.object_id AND inc.column_id = iic.column_id \ + WHERE iic.object_id = i.object_id AND iic.index_id = i.index_id AND iic.is_included_column = 1 \ + ORDER BY iic.index_column_id FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 2, N'') \ FROM sys.indexes i \ JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id \ JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id \ @@ -174,11 +182,11 @@ pub(crate) async fn fetch_indexes( relation: &str, ) -> Result, Interrupt> { let rows = catalogue_rows(client, watch, INDEXES_SQL, &[text_param(relation)]).await?; - Ok(indexes(&rows)?) + Ok(indexes(&rows, relation)?) } -fn indexes(rows: &[Vec]) -> Result, DriverError> { - let mut indexes: Vec = Vec::new(); +fn indexes(rows: &[Vec], relation: &str) -> Result, DriverError> { + let mut indexes: Vec<(IndexInfo, &[Value])> = Vec::new(); for row in rows { let cells = Cells::new(row, "sys.indexes"); let name = cells.required_text(0)?; @@ -187,17 +195,66 @@ fn indexes(rows: &[Vec]) -> Result, DriverError> { descending: cells.flag(4), }; match indexes.last_mut() { - Some(index) if index.name == name => index.keys.push(key), - _ => indexes.push(IndexInfo { - name, - keys: vec![key], - unique: cells.flag(1), - primary: cells.flag(2), - predicate: cells.text(5), - }), + Some((index, _)) if index.name == name => index.keys.push(key), + _ => indexes.push(( + IndexInfo { + name, + keys: vec![key], + unique: cells.flag(1), + primary: cells.flag(2), + predicate: cells.text(5), + definition: None, + }, + row, + )), } } - Ok(indexes) + Ok(indexes + .into_iter() + .map(|(mut index, row)| { + index.definition = create_index(relation, &index, &Cells::new(row, "sys.indexes")); + index + }) + .collect()) +} + +/// SQL Server keeps no text for an index, so this writes it from +/// sys.indexes: a UNIQUE constraint as the constraint it is, and only +/// rowstore indexes, whose keys and INCLUDE list are all there is. +fn create_index(relation: &str, index: &IndexInfo, cells: &Cells<'_>) -> Option { + let kind = cells + .text(6) + .filter(|kind| kind == "CLUSTERED" || kind == "NONCLUSTERED")?; + if index.primary { + return None; + } + let keys = index + .keys + .iter() + .filter_map(|key| { + let column = quote(key.column_name()?); + Some(match key.descending() { + true => format!("{column} DESC"), + false => column, + }) + }) + .collect::>() + .join(", "); + let name = quote(&index.name); + if cells.flag(7) { + return Some(format!( + "ALTER TABLE {relation} ADD CONSTRAINT {name} UNIQUE {kind} ({keys})" + )); + } + let unique = if index.unique { "UNIQUE " } else { "" }; + let mut sql = format!("CREATE {unique}{kind} INDEX {name} ON {relation} ({keys})"); + if let Some(included) = cells.text(8).filter(|included| !included.is_empty()) { + sql.push_str(&format!(" INCLUDE ({included})")); + } + if let Some(predicate) = &index.predicate { + sql.push_str(&format!(" WHERE {predicate}")); + } + Some(sql) } pub(crate) async fn fetch_foreign_keys( @@ -381,7 +438,7 @@ mod tests { ], ]; - let indexes = indexes(&rows).unwrap(); + let indexes = indexes(&rows, "[dbo].[t]").unwrap(); assert_eq!(indexes.len(), 2); assert_eq!(indexes[0].keys.len(), 2); @@ -391,6 +448,42 @@ mod tests { assert!(indexes[1].primary && indexes[1].can_identify_rows()); } + #[test] + fn an_index_is_written_as_sql_server_makes_it() { + let row = |name: &str, kind: &str, constraint: i64, included: Value, filter: Value| { + vec![ + text(name), + Value::Int(1), + Value::Int(0), + text("a"), + Value::Int(1), + filter, + text(kind), + Value::Int(constraint), + included, + ] + }; + let rows = vec![ + row("IX_a", "NONCLUSTERED", 0, text("[b], [c]"), text("([a]>(0))")), + row("UQ_a", "CLUSTERED", 1, Value::Null, Value::Null), + row("IX_x", "XML", 0, Value::Null, Value::Null), + ]; + + let indexes = indexes(&rows, "[dbo].[t]").unwrap(); + let statements: Vec> = indexes.iter().map(|index| index.definition.as_deref()).collect(); + + assert_eq!( + statements, + [ + Some( + "CREATE UNIQUE NONCLUSTERED INDEX [IX_a] ON [dbo].[t] ([a] DESC) INCLUDE ([b], [c]) WHERE ([a]>(0))" + ), + Some("ALTER TABLE [dbo].[t] ADD CONSTRAINT [UQ_a] UNIQUE CLUSTERED ([a] DESC)"), + None, + ] + ); + } + #[test] fn the_first_key_names_the_row() { let rows = vec![ diff --git a/linux/crates/drivers/mssql/tests/integration/metadata.rs b/linux/crates/drivers/mssql/tests/integration/metadata.rs index 9ccc4402d0..0e3c238115 100644 --- a/linux/crates/drivers/mssql/tests/integration/metadata.rs +++ b/linux/crates/drivers/mssql/tests/integration/metadata.rs @@ -338,3 +338,67 @@ async fn like_escape_matches_literal_underscore_and_percent() { Value::Text("x".to_owned()) ); } + +/// Each index's statement, run after the index is dropped, makes the +/// same index again: clustered, filtered with INCLUDE columns, and a +/// UNIQUE constraint. A columnstore index has no statement. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn index_statements_recreate_their_indexes() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + setup( + connection.as_ref(), + &[ + "CREATE TABLE dbo.ledger (id int NOT NULL CONSTRAINT ledger_pk PRIMARY KEY NONCLUSTERED, \ + code nvarchar(20) NOT NULL CONSTRAINT ledger_code UNIQUE, [a&b] int, amount int, note nvarchar(50))", + "CREATE CLUSTERED INDEX ledger_amount ON dbo.ledger (amount DESC)", + "CREATE NONCLUSTERED INDEX ledger_open ON dbo.ledger (code, id DESC) INCLUDE ([a&b], note) \ + WHERE amount > 0", + "CREATE NONCLUSTERED COLUMNSTORE INDEX ledger_columns ON dbo.ledger (amount)", + ], + ) + .await; + let table = dbo("ledger"); + let indexes = connection + .fetch_indexes(&table, within(CALL)) + .await + .expect("the indexes"); + + assert!( + indexes + .iter() + .filter(|index| index.primary || index.name == "ledger_columns") + .all(|index| index.definition.is_none()), + "{indexes:#?}" + ); + let open = indexes + .iter() + .find(|index| index.name == "ledger_open") + .expect("ledger_open"); + assert_eq!( + open.definition.as_deref(), + Some( + "CREATE NONCLUSTERED INDEX [ledger_open] ON [dbo].[ledger] ([code], [id] DESC) \ + INCLUDE ([a&b], [note]) WHERE ([amount]>(0))" + ) + ); + for index in indexes.iter().filter(|index| index.definition.is_some()) { + let statement = index.definition.clone().unwrap_or_default(); + let drop = match index.name.as_str() { + "ledger_code" => "ALTER TABLE dbo.ledger DROP CONSTRAINT ledger_code".to_owned(), + name => format!("DROP INDEX [{name}] ON dbo.ledger"), + }; + setup(connection.as_ref(), &[drop.as_str(), statement.as_str()]).await; + let remade = connection + .fetch_indexes(&table, within(CALL)) + .await + .expect("the indexes"); + assert_eq!( + remade.iter().find(|remade| remade.name == index.name), + Some(index), + "{statement}" + ); + } + connection.close().await.expect("a close"); +} diff --git a/linux/crates/drivers/mysql/src/catalog/mod.rs b/linux/crates/drivers/mysql/src/catalog/mod.rs index 1a02d6d9ce..9cc1373181 100644 --- a/linux/crates/drivers/mysql/src/catalog/mod.rs +++ b/linux/crates/drivers/mysql/src/catalog/mod.rs @@ -44,7 +44,7 @@ use tablepro_core::value::Value; use crate::flavour::MySqlFlavour; use crate::mysql_timestamp_mode::MySqlTimestampMode; -use escaped_expression::unescape_expression; +pub(crate) use escaped_expression::unescape_expression; use mysql_column_type::scan; /// The catalogue's facts that decide a kind, apart from the rest of the diff --git a/linux/crates/drivers/mysql/src/ddl/mod.rs b/linux/crates/drivers/mysql/src/ddl/mod.rs index 9f4e6c55d3..f3da090510 100644 --- a/linux/crates/drivers/mysql/src/ddl/mod.rs +++ b/linux/crates/drivers/mysql/src/ddl/mod.rs @@ -560,6 +560,7 @@ mod tests { unique: false, primary: false, predicate: None, + definition: None, }]; draft.foreign_keys = vec![ForeignKeyInfo { ref_schema: None, @@ -1128,6 +1129,7 @@ mod tests { unique: false, primary: false, predicate: None, + definition: None, }]; let plan = alter(&table, |draft| { diff --git a/linux/crates/drivers/mysql/src/metadata.rs b/linux/crates/drivers/mysql/src/metadata.rs index ca91863308..ca2f792cc0 100644 --- a/linux/crates/drivers/mysql/src/metadata.rs +++ b/linux/crates/drivers/mysql/src/metadata.rs @@ -8,6 +8,7 @@ use futures_util::TryStreamExt; use sqlx::mysql::MySqlConnection; use sqlx::{AssertSqlSafe, Executor as _, Row as _}; use tablepro_core::column::{ColumnDefault, ColumnInfo, ColumnKind, SqlExpression, TemporalZone}; +use tablepro_core::ddl::ddl_sql::parenthesized; use tablepro_core::ddl::{ CharacterSet, ColumnDefinition, InboundForeignKey, OutboundForeignKey, PrimaryKeyConstraint, ReferentialAction, TableDefinition, TableOptions, TableSource, UnmodeledAttribute, @@ -24,7 +25,7 @@ use crate::catalog::{ ColumnCheck, FULL_COLLATION_NAMES_SQL, FULL_COLLATIONS_SQL, INBOUND_FOREIGN_KEYS_SQL, ListedOptions, ListedSrid, MARIADB_COLUMN_CHECKS_SQL, MARIADB_GEOMETRY_SRIDS_SQL, MySqlColumnRow, TableComparisons, character_sets, column_checks, column_comparisons, column_definition, column_info, columns_sql, compares_alike, geometry_srids, - inbound_foreign_keys, place_column_checks, quoted_default, table_options, + inbound_foreign_keys, place_column_checks, quoted_default, table_options, unescape_expression, }; use crate::decode::decode_row; use crate::dialect::MySqlDialect; @@ -203,12 +204,12 @@ pub(crate) async fn fetch_columns( /// MySQL 8.0.13 and later name a functional key part in EXPRESSION; /// MariaDB has no such column, and no functional key parts. const MYSQL_INDEXES_SQL: &str = "\ -SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME, COLLATION, EXPRESSION +SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME, COLLATION, EXPRESSION, INDEX_TYPE, SUB_PART FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = COALESCE(?, DATABASE()) AND TABLE_NAME = ? ORDER BY INDEX_NAME = 'PRIMARY' DESC, INDEX_NAME, SEQ_IN_INDEX"; const MARIADB_INDEXES_SQL: &str = "\ -SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME, COLLATION, NULL AS EXPRESSION +SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME, COLLATION, NULL AS EXPRESSION, INDEX_TYPE, SUB_PART FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = COALESCE(?, DATABASE()) AND TABLE_NAME = ? ORDER BY INDEX_NAME = 'PRIMARY' DESC, INDEX_NAME, SEQ_IN_INDEX"; @@ -224,33 +225,97 @@ pub(crate) async fn fetch_indexes( false => MARIADB_INDEXES_SQL, }; let rows = catalog_rows(connection, flavour, sql, "STATISTICS", &table_binds(table)).await?; - let mut indexes: Vec = Vec::new(); + let mut indexes: Vec<(IndexInfo, String, Vec)> = Vec::new(); for row in rows { let name = row.required_text(0)?; let descending = row.text(3)?.as_deref() == Some("D"); - let key = match (row.text(2)?, row.text(4)?) { - (Some(column), _) => IndexKey::Column { - name: column, - descending, - }, - (None, Some(expression)) => IndexKey::Expression { - sql: expression, - descending, - }, + let (key, part) = match (row.text(2)?, row.text(4)?) { + (Some(column), _) => { + let prefix = row.number(6)?.map(|length| format!("({length})")).unwrap_or_default(); + let part = format!("{}{prefix}", dialect.quote_identifier(&column)); + ( + IndexKey::Column { + name: column, + descending, + }, + part, + ) + } + (None, Some(expression)) => { + let part = parenthesized(&unescape_expression(&expression)); + ( + IndexKey::Expression { + sql: expression, + descending, + }, + part, + ) + } (None, None) => continue, }; - match indexes.last_mut().filter(|index| index.name == name) { - Some(index) => index.keys.push(key), - None => indexes.push(IndexInfo { - unique: row.number(1)? == Some(0), - primary: name == "PRIMARY", - name, - keys: vec![key], - predicate: None, - }), + let part = match descending { + true => format!("{part} DESC"), + false => part, + }; + match indexes.last_mut().filter(|(index, ..)| index.name == name) { + Some((index, _, parts)) => { + index.keys.push(key); + parts.push(part); + } + None => indexes.push(( + IndexInfo { + unique: row.number(1)? == Some(0), + primary: name == "PRIMARY", + name, + keys: vec![key], + predicate: None, + definition: None, + }, + row.text(5)?.unwrap_or_default(), + vec![part], + )), } } - Ok(indexes) + Ok(indexes + .into_iter() + .map(|(mut index, index_type, parts)| { + index.definition = create_index(dialect, table, &index, &index_type, &parts); + index + }) + .collect()) +} + +/// The CREATE INDEX for an index STATISTICS lists, with the prefix +/// length a TEXT or BLOB key needs, the kind a full-text or spatial +/// index is made with, and the access method, since a MEMORY table +/// would otherwise make a B-tree index as a hash. The primary key is +/// only ever a constraint. +fn create_index( + dialect: &MySqlDialect, + table: &TableRef, + index: &IndexInfo, + index_type: &str, + parts: &[String], +) -> Option { + if index.primary { + return None; + } + let kind = match index_type { + "FULLTEXT" => "FULLTEXT ", + "SPATIAL" => "SPATIAL ", + _ if index.unique => "UNIQUE ", + _ => "", + }; + let method = match index_type { + "BTREE" | "HASH" => format!(" USING {index_type}"), + _ => String::new(), + }; + Some(format!( + "CREATE {kind}INDEX {} ON {} ({}){method}", + dialect.quote_identifier(&index.name), + dialect.qualify(table), + parts.join(", ") + )) } const FOREIGN_KEYS_SQL: &str = "\ @@ -747,6 +812,7 @@ mod tests { unique, primary: name == "PRIMARY", predicate: None, + definition: None, } } diff --git a/linux/crates/drivers/mysql/tests/integration/flavour_suite.rs b/linux/crates/drivers/mysql/tests/integration/flavour_suite.rs index e289adfaf2..78510b24f9 100644 --- a/linux/crates/drivers/mysql/tests/integration/flavour_suite.rs +++ b/linux/crates/drivers/mysql/tests/integration/flavour_suite.rs @@ -527,3 +527,69 @@ async fn caching_sha2_without_tls_uses_rsa() { connection.close().await.expect("a close"); root.close().await.expect("a close"); } + +/// Each index's statement, run after the index is dropped, makes the +/// same index again: a full-text index, a prefix on a TEXT column, and +/// on MySQL a functional key part with a quoted literal. A B-tree on a +/// MEMORY table, whose default is a hash, says USING BTREE. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn index_statements_recreate_their_indexes() { + for flavour in FLAVOURS { + let fixture = start(flavour).await; + let connection = connect(&fixture, false).await; + let mut statements = vec![ + "CREATE TABLE notes (id int PRIMARY KEY, title varchar(50), body text)", + "CREATE TABLE cache (id int PRIMARY KEY, k int, INDEX cache_k USING BTREE (k)) ENGINE=MEMORY", + "CREATE UNIQUE INDEX notes_title ON notes (title DESC, id)", + "CREATE INDEX notes_body ON notes (body(12))", + "CREATE FULLTEXT INDEX notes_search ON notes (title, body)", + ]; + if flavour == MysqlFlavour::Mysql { + statements.push("CREATE INDEX notes_tagged ON notes ((concat(title, 'it''s')))"); + } + setup(connection.as_ref(), &statements).await; + let cache = crate::support::table("cache"); + let cache_index = connection + .fetch_indexes(&cache, within(CALL)) + .await + .expect("the indexes") + .into_iter() + .find(|index| index.name == "cache_k") + .expect("cache_k"); + assert_eq!( + cache_index.definition.as_deref(), + Some("CREATE INDEX `cache_k` ON `tablepro`.`cache` (`k`) USING BTREE"), + "{flavour:?}: a MEMORY table keeps its B-tree" + ); + let table = crate::support::table("notes"); + let indexes = connection + .fetch_indexes(&table, within(CALL)) + .await + .expect("the indexes"); + + assert_eq!( + indexes + .iter() + .find(|index| index.primary) + .map(|index| &index.definition), + Some(&None), + "{flavour:?}: the primary key is a constraint" + ); + for index in indexes.iter().filter(|index| !index.primary) { + let statement = index.definition.clone().expect("a statement"); + let drop = format!("DROP INDEX `{}` ON `notes`", index.name); + setup(connection.as_ref(), &[drop.as_str(), statement.as_str()]).await; + let remade = connection + .fetch_indexes(&table, within(CALL)) + .await + .expect("the indexes"); + assert_eq!( + remade.iter().find(|remade| remade.name == index.name), + Some(index), + "{flavour:?}: {statement}" + ); + } + connection.close().await.expect("a close"); + } +} diff --git a/linux/crates/drivers/postgres/src/ddl.rs b/linux/crates/drivers/postgres/src/ddl.rs index 2f1e78645d..93235db3b0 100644 --- a/linux/crates/drivers/postgres/src/ddl.rs +++ b/linux/crates/drivers/postgres/src/ddl.rs @@ -756,6 +756,7 @@ mod tests { unique: true, primary: false, predicate: None, + definition: None, }]; draft.foreign_keys = vec![ForeignKeyInfo { name: "lines_order_fk".to_owned(), @@ -1005,6 +1006,7 @@ mod tests { unique: false, primary: false, predicate: None, + definition: None, }]; let plan = alter(&original, |draft| { @@ -1020,6 +1022,7 @@ mod tests { unique: false, primary: false, predicate: Some("nickname IS NOT NULL".to_owned()), + definition: None, }); }); diff --git a/linux/crates/drivers/postgres/src/metadata/indexes.rs b/linux/crates/drivers/postgres/src/metadata/indexes.rs index f22abce8d1..108ff1169f 100644 --- a/linux/crates/drivers/postgres/src/metadata/indexes.rs +++ b/linux/crates/drivers/postgres/src/metadata/indexes.rs @@ -6,7 +6,8 @@ use super::{decode_error, text_rows}; /// One row per key column, the INCLUDE columns past `indnkeyatts` left /// out. Attribute number 0 marks an expression, which only -/// `pg_get_indexdef` can print. +/// `pg_get_indexdef` can print, and with no column number it prints the +/// whole CREATE INDEX statement. pub(super) const INDEXES_SQL: &str = "\ SELECT ic.relname::text, i.indisunique, @@ -14,7 +15,8 @@ SELECT ic.relname::text, pg_catalog.pg_get_expr(i.indpred, i.indrelid)::text, a.attname::text, pg_catalog.pg_get_indexdef(i.indexrelid, k.ord::int4, true)::text, - COALESCE(pg_catalog.pg_index_column_has_property(i.indexrelid, k.ord::int4, 'desc'), false) + COALESCE(pg_catalog.pg_index_column_has_property(i.indexrelid, k.ord::int4, 'desc'), false), + pg_catalog.pg_get_indexdef(i.indexrelid)::text FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class ic ON ic.oid = i.indexrelid CROSS JOIN LATERAL pg_catalog.unnest(i.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord) @@ -45,6 +47,7 @@ pub(crate) async fn fetch_indexes(client: &Client, relation: &str) -> Result 0)") + ); assert!( indexes .iter() diff --git a/linux/crates/drivers/sqlite/src/ddl/tests.rs b/linux/crates/drivers/sqlite/src/ddl/tests.rs index d99ef4ecbb..ff04ab6226 100644 --- a/linux/crates/drivers/sqlite/src/ddl/tests.rs +++ b/linux/crates/drivers/sqlite/src/ddl/tests.rs @@ -93,6 +93,7 @@ fn index(name: &str, column: &str) -> IndexInfo { unique: false, primary: false, predicate: None, + definition: None, } } @@ -265,6 +266,7 @@ fn plan_create_table_composite_key_index_and_foreign_key() { unique: true, primary: false, predicate: Some("sku IS NOT NULL".to_owned()), + definition: None, }]; draft.foreign_keys = vec![ForeignKeyInfo { on_delete: Some(ReferentialAction::Cascade), diff --git a/linux/crates/drivers/sqlite/src/metadata.rs b/linux/crates/drivers/sqlite/src/metadata.rs index d2b90341df..238e05840c 100644 --- a/linux/crates/drivers/sqlite/src/metadata.rs +++ b/linux/crates/drivers/sqlite/src/metadata.rs @@ -614,6 +614,7 @@ async fn indexes( unique: unique != 0, primary, predicate, + definition: stored_sql.map(|sql| in_schema(&sql, schema)), }); } // An INTEGER PRIMARY KEY is the rowid itself and has no index of its @@ -632,6 +633,7 @@ async fn indexes( unique: true, primary: true, predicate: None, + definition: None, }); } Ok(indexes) @@ -706,6 +708,23 @@ fn qualifier(schema: &str) -> Option { (schema != MAIN_SCHEMA).then(|| schema.to_owned()) } +/// SQLite keeps an index's SQL with no schema and with its head +/// normalized to `CREATE [UNIQUE] INDEX`, so the index name follows +/// that head. Outside `main` the name takes its schema, or the +/// statement would make the index on a table of `main`. +fn in_schema(sql: &str, schema: &str) -> String { + let Some(schema) = qualifier(schema) else { + return sql.to_owned(); + }; + let head = ["CREATE UNIQUE INDEX ", "CREATE INDEX "] + .into_iter() + .find(|head| sql.starts_with(head)); + match head { + Some(head) => format!("{head}{}.{}", quote(&schema), &sql[head.len()..]), + None => sql.to_owned(), + } +} + fn quote(name: &str) -> String { format!("\"{}\"", name.replace('"', "\"\"")) } diff --git a/linux/crates/drivers/sqlite/tests/integration/metadata.rs b/linux/crates/drivers/sqlite/tests/integration/metadata.rs index fb0073b029..ef05381f71 100644 --- a/linux/crates/drivers/sqlite/tests/integration/metadata.rs +++ b/linux/crates/drivers/sqlite/tests/integration/metadata.rs @@ -57,6 +57,52 @@ async fn expression_desc_partial_index_keys() { connection.close().await.unwrap(); } +/// An attached database's index keeps its SQL without the schema, so +/// its statement names the schema, or it would index `main`'s table of +/// the same name. +#[tokio::test] +async fn an_attached_index_statement_names_its_schema() { + let file = TestFile::new().await; + let other = file.directory().join("other.sqlite3"); + create_database(&other).await.unwrap(); + let connection = file.open().await; + let attach = format!("ATTACH DATABASE '{}' AS archive", other.display()); + query(connection.as_ref(), &attach).await; + query( + connection.as_ref(), + "CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)", + ) + .await; + query( + connection.as_ref(), + "CREATE TABLE archive.people (id INTEGER PRIMARY KEY, name TEXT)", + ) + .await; + query(connection.as_ref(), "CREATE INDEX archive.by_name ON people (name)").await; + let archived = TableRef::new(Some("archive".to_owned()), "people"); + + let read = connection.fetch_indexes(&archived, call()).await.unwrap(); + let statement = read + .iter() + .find(|index| index.name == "by_name") + .and_then(|index| index.definition.clone()) + .expect("a statement"); + query(connection.as_ref(), "DROP INDEX archive.by_name").await; + query(connection.as_ref(), &statement).await; + + assert_eq!(statement, "CREATE INDEX \"archive\".by_name ON people (name)"); + assert_eq!(connection.fetch_indexes(&archived, call()).await.unwrap(), read); + assert!( + connection + .fetch_indexes(&table("people"), call()) + .await + .unwrap() + .iter() + .all(|index| index.name != "by_name") + ); + connection.close().await.unwrap(); +} + #[tokio::test] async fn views_listed_and_attached_schema_listed() { let file = TestFile::new().await; diff --git a/linux/crates/test-support/src/conformance/cases/ddl.rs b/linux/crates/test-support/src/conformance/cases/ddl.rs index 1de1b1fbf3..55d8c85051 100644 --- a/linux/crates/test-support/src/conformance/cases/ddl.rs +++ b/linux/crates/test-support/src/conformance/cases/ddl.rs @@ -24,6 +24,7 @@ const APPLY_CASE: &str = "ddl_plans_apply_and_read_back"; const FAILED_CASE: &str = "failed_ddl_batch_outcome_matches_ddl_atomicity"; const SCRIPT_CASE: &str = "create_table_script_recreates_the_table"; const ROWS_CASE: &str = "structure_changes_keep_rows_and_indexes"; +const INDEX_CASE: &str = "index_statement_recreates_the_index"; /// Case 1: each structure change the engine offers applies and reads /// back as drafted, and each one it refuses is refused by the plan @@ -181,6 +182,7 @@ pub async fn create_table_script_recreates_the_table( unique: false, primary: false, predicate: None, + definition: None, }; let step = Step { case, @@ -249,6 +251,7 @@ pub async fn structure_changes_keep_rows_and_indexes( unique: false, primary: false, predicate: None, + definition: None, }; let permission = table_permission(ddl.table_capabilities(&table).create_index); step.apply(&mut table, "create index", permission, |draft| { @@ -306,6 +309,76 @@ pub async fn structure_changes_keep_rows_and_indexes( Ok(()) } +/// Case 5: the statement an index reads back with, run after the index +/// is dropped, makes the same index again. +pub async fn index_statement_recreates_the_index(engine: E) -> Result<(), ConformanceFailure> { + let case = INDEX_CASE; + let connection = connect(case, &engine, writable()).await?; + let dialect = connection.dialect(); + let ddl = ddl_of(case, dialect.as_ref())?; + let parent = engine.scratch_table(&unique("ddl_index")); + create( + case, + &engine, + connection.as_ref(), + ddl, + &parent_draft(case, &engine, dialect.as_ref(), ddl, &parent)?, + ) + .await?; + let mut table = definition(connection.as_ref(), &parent).await?; + let capabilities = *ddl.capabilities(); + if capabilities.create_index && capabilities.drop_index { + let name = format!("{}_label", parent.name); + let step = Step { + case, + connection: connection.as_ref(), + ddl, + }; + step.apply(&mut table, "create index", Permission::Allowed, |draft| { + draft.indexes.push(IndexInfo { + name: name.clone(), + keys: vec![IndexKey::Column { + name: "label".to_owned(), + descending: capabilities.descending_index_keys, + }], + unique: false, + primary: false, + predicate: None, + definition: None, + }); + }) + .await?; + let made = named_index(case, &table, &name)?; + let statement = made + .definition + .clone() + .ok_or_else(|| unexpected(case, format!("index {name} reads with no statement: {made:?}")))?; + step.apply(&mut table, "drop index", Permission::Allowed, |draft| { + draft.indexes.retain(|index| index.name != name); + }) + .await?; + run(connection.as_ref(), &parent, DdlPlan::from_sql([statement.clone()])) + .await + .map_err(|error| unexpected(case, format!("{statement} failed: {error}")))?; + let remade = named_index(case, &definition(connection.as_ref(), &parent).await?, &name)?; + expect(case, remade == made, || { + format!("{statement} made {remade:?}, and the index was {made:?}") + })?; + } + drop_table(case, connection.as_ref(), ddl, &parent).await?; + connection.close().await?; + Ok(()) +} + +fn named_index(case: &'static str, table: &TableDefinition, name: &str) -> Result { + table + .indexes + .iter() + .find(|index| index.name == name) + .cloned() + .ok_or_else(|| unexpected(case, format!("no index {name} in {:?}", table.indexes))) +} + fn ddl_of<'a>(case: &'static str, dialect: &'a dyn SqlDialect) -> Result<&'a dyn DdlDialect, ConformanceFailure> { dialect .ddl() @@ -519,6 +592,7 @@ impl Step<'_> { unique: false, primary: false, predicate: None, + definition: None, }; let permission = table_permission(self.ddl.table_capabilities(table).create_index); self.apply(table, "create index", permission, |draft| draft.indexes.push(index)) diff --git a/linux/crates/test-support/src/conformance/ddl_suite_macro.rs b/linux/crates/test-support/src/conformance/ddl_suite_macro.rs index 63258dd3e3..409355041f 100644 --- a/linux/crates/test-support/src/conformance/ddl_suite_macro.rs +++ b/linux/crates/test-support/src/conformance/ddl_suite_macro.rs @@ -33,6 +33,12 @@ macro_rules! ddl_conformance_suite { structure_changes_keep_rows_and_indexes, $crate::conformance::cases::ddl::structure_changes_keep_rows_and_indexes::<$engine> } + $crate::__conformance_case! { + $docker, + $engine as ConformanceEngine, + index_statement_recreates_the_index, + $crate::conformance::cases::ddl::index_statement_recreates_the_index::<$engine> + } $crate::__conformance_case! { $docker, $engine as ConformanceEngine, diff --git a/linux/docs/testing.md b/linux/docs/testing.md index a0980d6b6d..ea2d7fe357 100644 --- a/linux/docs/testing.md +++ b/linux/docs/testing.md @@ -172,6 +172,7 @@ Every case but the last plans through `connection.dialect().ddl()` and runs the - `failed_ddl_batch_outcome_matches_ddl_atomicity` sends two plans whose second statement the server rejects, and checks the outcome against `ddl_writes`: `RolledBack` and nothing applied, or `PartiallyApplied` and the first plan applied. - `create_table_script_recreates_the_table` drops a table and runs its `create_table_script`, which has to bring back the same column definitions and index keys. - `structure_changes_keep_rows_and_indexes` fills an indexed table, then changes a default and a nullability. The rows and the index have to come through, which on SQLite means through the table rebuild. +- `index_statement_recreates_the_index` drops an index and runs the CREATE INDEX statement it was read with, which has to make the same index again. - `builtin_snippets_run_as_written` takes the SQL editor's built-in snippets for the connection's grammar, fills their table, index, column and value stops, with the row's `id` too on CQL, which finds a row only by its key, and keeps the rest as written (`tablepro_test_support::expand_snippet`), and runs each one through an editor session as the editor splits it: one statement each, all completing. The PostgreSQL, MySQL, SQL Server, ClickHouse, SQLite and DuckDB targets run it, and the MySQL target runs it on MariaDB as well. SQLite and DuckDB pass `docker: false`, so their cases run in the unit suite. diff --git a/linux/po/vi.po b/linux/po/vi.po index 608e7b216e..3e2a717c45 100644 --- a/linux/po/vi.po +++ b/linux/po/vi.po @@ -4823,6 +4823,18 @@ msgstr "_Sửa cấu trúc" msgid "Show _CREATE TABLE" msgstr "_Hiện CREATE TABLE" +#: crates/app/src/ui/structure_tab/indexes.rs +msgid "Show _CREATE INDEX" +msgstr "_Hiện CREATE INDEX" + +#: crates/app/src/ui/structure_tab/indexes.rs +msgid "_Copy CREATE INDEX" +msgstr "Sao _chép CREATE INDEX" + +#: crates/app/src/ui/structure_tab/indexes.rs +msgid "Index Options" +msgstr "Tùy chọn chỉ mục" + #: crates/app/src/ui/workspace/sidebar/sidebar_menu.rs msgid "_Import CSV…" msgstr "_Nhập CSV…" From 26fb9c8ce58d7a64f9117523a73bb83d0ba15c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Nam=20Long?= Date: Sun, 1 Nov 2026 15:10:55 +0700 Subject: [PATCH 77/85] feat(connections): shortcuts and strip menu for dashboard and insights (#3957) Co-authored-by: Ngo Quoc Dat --- .../Core/Menu/DatabaseMenuBuilder.swift | 25 ++-- .../ConnectionCloseAction.swift | 18 +-- .../Infrastructure/ConnectionWorkspace.swift | 11 +- ...nSplitViewController+EditMenuActions.swift | 27 +++++ ...nSplitViewController+FileMenuActions.swift | 45 ++----- ...inSplitViewController+MenuValidation.swift | 94 +++++++++------ .../Infrastructure/TabWindowController.swift | 7 +- .../ConnectionActionsMenuResolver.swift | 12 +- .../Toolbar/ToolbarContextResolver.swift | 14 +-- .../WorkspaceRailViewController.swift | 110 ++++++++++++------ .../Models/UI/KeyboardShortcutModels.swift | 13 ++- .../Views/AIChat/ChatComposerTextView.swift | 34 ++++++ .../MainContentCommandActions+BulkClose.swift | 39 ++++++- .../MainContentCommandActions+UndoState.swift | 5 +- .../Main/MainContentCommandActions.swift | 42 ++++--- .../Views/Main/MainContentCoordinator.swift | 8 +- .../Core/Menu/ChromeSnapshot.json | 4 +- .../Core/Menu/MainMenuBuilderTests.swift | 21 +++- .../Infrastructure/AgentModeWindowTests.swift | 60 ++++++++++ .../ConnectionActionsMenuResolverTests.swift | 34 ++++++ .../MenuActionSelectorCollisionTests.swift | 4 +- .../MenuContentModeParityTests.swift | 51 ++++++++ .../WorkspaceRailServerToolTests.swift | 101 ++++++++++++++++ .../Models/ShortcutUniquenessTests.swift | 1 + .../AIChat/ChatComposerScrollViewTests.swift | 30 +++++ .../Main/CommandActionsBulkCloseTests.swift | 31 +++++ apple/changelog.d/3957.added.md | 1 + apple/changelog.d/3957.fixed.1.md | 1 + apple/changelog.d/3957.fixed.2.md | 1 + apple/changelog.d/3957.fixed.3.md | 1 + apple/changelog.d/3957.fixed.md | 1 + 31 files changed, 655 insertions(+), 191 deletions(-) create mode 100644 apple/TableProTests/Core/Services/Infrastructure/WorkspaceRailServerToolTests.swift create mode 100644 apple/changelog.d/3957.added.md create mode 100644 apple/changelog.d/3957.fixed.1.md create mode 100644 apple/changelog.d/3957.fixed.2.md create mode 100644 apple/changelog.d/3957.fixed.3.md create mode 100644 apple/changelog.d/3957.fixed.md diff --git a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift index e165f2b8f7..fa759c6bfe 100644 --- a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift +++ b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift @@ -5,10 +5,7 @@ import AppKit -/// "Open Database" and "Close Tabs for Other Databases" read Database or Schema -/// depending on what the driver switches between. That is a closed two-string set -/// driven by `ContainerSwitchTarget`, not an interpolated driver name, because -/// System Settings binds an App Shortcut to a menu item's exact literal title. +// Database-or-Schema titles are a closed set, never interpolated: System Settings binds an App Shortcut to the exact title. @MainActor enum DatabaseMenuBuilder { static func build(keyboard: KeyboardSettings) -> NSMenuItem { @@ -64,10 +61,7 @@ enum DatabaseMenuBuilder { String(localized: "Search Database…"), action: #selector(MainSplitViewController.searchDatabase(_:)) ), - /// The sidebar's own Copy To and Duplicate Database, mirrored so both are reachable - /// from the keyboard. The menu acts on the database being browsed, which is what a - /// command with no clicked row can mean. Spelled exactly as the sidebar and the sheet - /// spell it: one command carrying two names reads as two commands. + // Mirrors the sidebar, acting on the browsed database; one command with two names reads as two. MenuItemFactory.item( String(localized: "Copy To…"), action: #selector(MainSplitViewController.copyObjectsToDatabase(_:)) @@ -85,8 +79,7 @@ enum DatabaseMenuBuilder { String(localized: "Edit View Definition…"), action: #selector(MainSplitViewController.editViewDefinition(_:)) ), - /// The sidebar's own per-object commands, mirrored so each is reachable from the menu - /// bar and the keyboard, and spelled exactly as the sidebar spells them. + // The sidebar's per-object commands, spelled as the sidebar spells them. MenuItemFactory.item( String(localized: "Show DDL"), action: #selector(MainSplitViewController.showObjectDDL(_:)) @@ -133,7 +126,9 @@ enum DatabaseMenuBuilder { ), MenuItemFactory.item( String(localized: "Server Dashboard"), - action: #selector(MainSplitViewController.showServerDashboard(_:)) + action: #selector(MainSplitViewController.showServerDashboard(_:)), + shortcut: .showServerDashboard, + keyboard: keyboard ), MenuItemFactory.item( String(localized: "Users & Roles"), @@ -141,16 +136,16 @@ enum DatabaseMenuBuilder { ), MenuItemFactory.item( String(localized: "Query Insights"), - action: #selector(MainSplitViewController.showQueryInsights(_:)) + action: #selector(MainSplitViewController.showQueryInsights(_:)), + shortcut: .showQueryInsights, + keyboard: keyboard ), MenuItemFactory.item( String(localized: "Table Health"), action: #selector(MainSplitViewController.showTableHealth(_:)) ), MenuItemFactory.separator, - /// No ellipsis: the HIG reserves one for an action that needs more information before - /// it can complete, and this needs none. It validates to disabled for every connection - /// whose driver holds no file, which is all of them but the embedded engines. + // No ellipsis: the HIG keeps one for a command that asks for more input, and this asks for none. MenuItemFactory.item( String(localized: "Release File Lock"), action: #selector(MainSplitViewController.releaseFileLock(_:)) diff --git a/apple/TablePro/Core/Services/Infrastructure/ConnectionCloseAction.swift b/apple/TablePro/Core/Services/Infrastructure/ConnectionCloseAction.swift index f7ebfbab3f..392900b057 100644 --- a/apple/TablePro/Core/Services/Infrastructure/ConnectionCloseAction.swift +++ b/apple/TablePro/Core/Services/Infrastructure/ConnectionCloseAction.swift @@ -25,22 +25,25 @@ internal enum ConnectionCloseAction { case confirmUnsavedWork } - /// Pure so the case that used to fail silently is pinned by a test: a connection with no session - /// has nothing to lose, and asking about it produced an alert nobody could answer. - /// Saves in every window hosting the connection, not only the one that answered first. Each - /// coordinator can save just its own selected tab's live work, so a Save that reached one of - /// them left the other window's grid edits behind and closed over them. + // Every window hosting the connection, every tab in each: saving only the selected tab closed over the rest. private static func saveEveryWindowsWork( across coordinators: [MainContentCoordinator], fallback: MainContentCoordinator? ) async -> Bool { let targets = coordinators.isEmpty ? [fallback].compactMap { $0 } : coordinators for coordinator in targets { - guard await coordinator.commandActions?.saveSelectedTabWork() == true else { return false } + guard await coordinator.commandActions?.saveEveryTabsWork() == true else { return false } } return !targets.isEmpty } + private static func unsavedWorkMessage(across coordinators: [MainContentCoordinator]) -> String { + let keepsTabsOpen = coordinators.contains { coordinator in + coordinator.commandActions?.holdsWorkOnlyItsTabCanSave(among: coordinator.tabManager.tabs) == true + } + return MainContentCommandActions.unsavedWorkMessage(keepsTabsOpen: keepsTabsOpen) + } + /// The transaction is asked about before unsaved work, whose Save writes at once: a Cancel on the /// transaction after it would keep the connection open with that write already made. internal static func decision(hasSession: Bool, holdsTransaction: Bool, hasUnsavedWork: Bool) -> Decision { @@ -106,8 +109,9 @@ internal enum ConnectionCloseAction { } let presentingWindow = reveal(connectionId: connectionId) + let targets = coordinators.isEmpty ? [coordinator].compactMap { $0 } : coordinators switch await AlertHelper.confirmSaveChanges( - message: String(localized: "Your changes will be lost if you don't save them."), + message: unsavedWorkMessage(across: targets), window: presentingWindow ) { case .save: diff --git a/apple/TablePro/Core/Services/Infrastructure/ConnectionWorkspace.swift b/apple/TablePro/Core/Services/Infrastructure/ConnectionWorkspace.swift index c0daa0a738..4f922750c2 100644 --- a/apple/TablePro/Core/Services/Infrastructure/ConnectionWorkspace.swift +++ b/apple/TablePro/Core/Services/Infrastructure/ConnectionWorkspace.swift @@ -56,10 +56,14 @@ internal final class ConnectionWorkspace { /// left the mode with the second window's layout. internal var browseCollapseState: (sidebar: Bool, inspector: Bool)? - /// Each workspace owns its undo stack. Routing through `NSWindow.undoManager` was correct - /// while a window meant one connection; sharing one window between several would let an - /// undo in one connection roll back an edit made in another. + // Per workspace: one window hosts several connections, and a shared stack undid one connection's edit from another. internal let undoManager: UndoManager + // Agent mode's own history: the browse one holds edits to a grid the mode hides. + internal let agentUndoManager = UndoManager() + + internal var activeUndoManager: UndoManager { + resolvedContentMode == .agent ? agentUndoManager : undoManager + } /// This connection's own view tree, built once and kept. The window shows one workspace's panes /// at a time by swapping which of these is the split items' child. @@ -401,6 +405,7 @@ internal final class ConnectionWorkspace { sessionState = nil session = nil undoManager.removeAllActions() + agentUndoManager.removeAllActions() /// Once nothing hosts this connection any more, its agent sessions stop: a stream, a tool /// loop or a card waiting for an answer would otherwise keep running with nothing on screen. /// Deferred so this workspace has already left the window's registry when the check runs. diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+EditMenuActions.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+EditMenuActions.swift index a44b67e21c..2ffdce2b29 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+EditMenuActions.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+EditMenuActions.swift @@ -10,14 +10,41 @@ extension MainSplitViewController: GridRowCopyCommandResponding {} // Fallback responders; paste: must be here, or AppKit disables Paste and the disabled item swallows Cmd+V window-wide. extension MainSplitViewController { + // Agent mode draws no tab or grid, so Undo runs the mode's own history, never the browse one behind it. + var agentUndoManager: UndoManager? { + contentMode == .agent ? workspaces.selected?.agentUndoManager : nil + } + @objc func undo(_ sender: Any?) { + guard contentMode == .browse else { + agentUndoManager?.undo() + return + } commandActions?.undoChange() } @objc func redo(_ sender: Any?) { + guard contentMode == .browse else { + agentUndoManager?.redo() + return + } commandActions?.redoChange() } + var undoTitle: String { + guard contentMode == .browse else { + return agentUndoManager?.undoMenuItemTitle ?? String(localized: "Undo") + } + return commandActions?.resolvedUndoTitle ?? String(localized: "Undo") + } + + var redoTitle: String { + guard contentMode == .browse else { + return agentUndoManager?.redoMenuItemTitle ?? String(localized: "Redo") + } + return commandActions?.resolvedRedoTitle ?? String(localized: "Redo") + } + @objc func copy(_ sender: Any?) { guard let actions = commandActions else { return } if actions.hasRowSelection { diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+FileMenuActions.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+FileMenuActions.swift index 879cd2c8c3..2d23bcea46 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+FileMenuActions.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+FileMenuActions.swift @@ -25,23 +25,9 @@ extension MainSplitViewController { commandActions?.newTab() } - /// The editor's meaning of Close, reached through `EditorWindow.performClose(_:)`. - /// - /// It is not an action itself, because the window already is one: `performClose:` resolves to - /// the nearest responder that claims it, so the connections strip still takes Close while it - /// holds the keyboard, and everything else in the window falls through to the window. - /// - /// A connecting or failed pane has no command surface, so Close ends the connection instead. - /// Leaving that to `commandActions` made the command inert on exactly the pane a user most - /// wants to dismiss. - /// Returns false when the window hosts nothing to close, so the window falls back to closing - /// itself rather than letting the command resolve to nothing, which is the failure this whole - /// route exists to make impossible. - /// What Close does here, in the words the connections strip already uses for the same command. - /// A window with no tab left closes the connection, so naming it "Close Tab" would describe an - /// action the command does not take. + // The strip's words for the same command. Agent mode shows no tab, so it names what a window with none would close. var closeCommandTitle: String? { - if commandActions?.hasOpenTab == true { return String(localized: "Close Tab") } + if contentMode == .browse, commandActions?.hasOpenTab == true { return String(localized: "Close Tab") } if let workspace = workspaces.selected, let entry = WorkspaceCloseAction.closableEntry( connectionId: workspace.connectionId, @@ -53,22 +39,18 @@ extension MainSplitViewController { return String(format: String(localized: "Close “%@”"), name) } + // Reached through `EditorWindow.performClose(_:)`; false lets the window close itself when it hosts nothing. func closeFrontmostTab() -> Bool { guard let actions = commandActions else { guard let connectionId = workspaces.selectedConnectionId else { return false } WindowManager.shared.closeWindow(for: connectionId) return true } - /// A window that exists only to hold a tab moved out of another one closes with that tab. - /// `closeTab` deliberately leaves a window standing when its last tab goes, because the - /// connection is still open in it and its object browser is still useful; that is the right - /// answer for the window a connection lives in and the wrong one here, where Close would - /// empty the window and pressing Close again would take the connection down in both. - /// - /// It still goes through `closeTabAwaiting`, which is the primitive Cmd+W is required to - /// keep. Skipping to `super.performClose` closed the window over a save prompt that was - /// never shown and never reached Recently Closed Tabs. The window closes only once the tab - /// actually went, so Cancel at the prompt leaves both standing. + guard contentMode == .browse else { + actions.closeWorkspace() + return true + } + // A window holding only a moved tab closes with it, after `closeTabAwaiting` so a cancelled save prompt keeps both. if isDetachedSingleTabWindow, let selected = workspaces.selected?.sessionState?.tabManager.selectedTab { Task { @MainActor [weak self] in await actions.closeTabAwaiting(id: selected.id) @@ -76,18 +58,11 @@ extension MainSplitViewController { self.workspaces.selected?.sessionState?.tabManager.tabs.isEmpty == true, let workspace = self.workspaces.selected else { return } - /// Asked again after the await. The save sheet can stand for as long as the user - /// likes, and the window this tab was moved out of can close underneath it: closing - /// then takes the connection's last window with it, which disconnects the session - /// and skips the whole-window confirmation that close would otherwise raise. + // Asked again: the source window can close during the prompt, and this one would then be the connection's last. guard WindowManager.shared.workspaces(for: workspace.connectionId).count > 1 else { return } - /// `closeWindowAwaiting`, not a raw `close()`. An inspector edit is connection - /// scoped, so `closeTabAwaiting` deliberately does not ask about it; closing the - /// window directly then tore down the per-window `RightPanelState` and dropped it - /// with no prompt. With the tab already gone this window has none left, which is - /// the branch that closes it. + // Not a raw `close()`, which dropped an unsaved inspector edit with no prompt. await actions.closeWindowAwaiting() } return true diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift index a6ee87fe59..40071b509b 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift @@ -147,7 +147,12 @@ struct MenuValidationContext: Equatable { extension MainSplitViewController: NSMenuItemValidation { // Nil: not this window's call. An unhandled selector stays lit (Clear Selection did); MenuValidationCoverageTests catches it. static func resolvedEnablement(_ selector: Selector, context: MenuValidationContext) -> Bool? { - if context.isAgentMode, browseContentSelectors.contains(selector) { return false } + guard let answer = ownEnablement(selector, context: context) else { return nil } + guard context.isAgentMode else { return answer } + return answer && agentModeSelectors.contains(selector) + } + + private static func ownEnablement(_ selector: Selector, context: MenuValidationContext) -> Bool? { if let find = isFindCommandEnabled(selector, context: context) { return find } if let query = isQueryCommandEnabled(selector, context: context) { return query } if let chooser = isContainerCommandEnabled(selector, context: context) { return chooser } @@ -385,6 +390,9 @@ extension MainSplitViewController: NSMenuItemValidation { return context.isConnected && !context.isReadOnly && context.canCreateSchema case #selector(createTableFolder(_:)): return context.isConnected && context.canCreateTableFolder + // The schema submenu adds this only for an editable schema; reaching here still needs a writable session. + case #selector(editCurrentSchema(_:)): + return context.isConnected && !context.isReadOnly default: return nil } @@ -446,39 +454,36 @@ extension MainSplitViewController: NSMenuItemValidation { } } - // Agent mode does not mount browse content. MenuContentModeParityTests derives this set from the toolbar. - private static let browseContentSelectors: Set = [ - #selector(refreshDatabase(_:)), - #selector(saveDocument(_:)), - #selector(discardChanges(_:)), - #selector(addRow(_:)), - #selector(restorePreviousValues(_:)), - #selector(editDocument(_:)), - #selector(insertDocument(_:)), - #selector(previewSQL(_:)), - #selector(toggleResults(_:)), - #selector(toggleQueryHistory(_:)), - #selector(newEditorTab(_:)), - #selector(openQuickSwitcher(_:)), - #selector(exportTables(_:)), - #selector(exportDataDictionary(_:)), - // Both spellings of one command; one without the other leaves the format list live over a dim leaf. - #selector(importData(_:)), - #selector(importDataFormat(_:)), - #selector(showServerDashboard(_:)), - #selector(showUsersAndRoles(_:)), - #selector(showQueryInsights(_:)), - #selector(showTableHealth(_:)), - #selector(navigateBack(_:)), - #selector(navigateForward(_:)), - #selector(moveTabToConnection(_:)), - #selector(searchDatabase(_:)), - #selector(buildJoinQuery(_:)), - #selector(pinResult(_:)), - #selector(compareResult(_:)), - #selector(showPreviousResult(_:)), - #selector(showNextResult(_:)), - #selector(closeResultTab(_:)), + // Agent mode mounts no browse content, so the window answers there only for the window, the connection and the conversation. + static let agentModeSelectors: Set = [ + #selector(switchConnection(_:)), + #selector(closeConnection(_:)), + #selector(setSafeModeLevel(_:)), + #selector(releaseFileLock(_:)), + #selector(openContainerSwitcher(_:)), + #selector(openSchemaSwitcher(_:)), + #selector(switchToSchema(_:)), + #selector(switchSessionContext(_:)), + #selector(setFavoriteDatabaseEnvironment(_:)), + #selector(removeFavoriteDatabase(_:)), + #selector(setContentModeFromMenu(_:)), + #selector(toggleContentModeFromMenu(_:)), + #selector(newAgentSession(_:)), + #selector(openAgentSession(_:)), + #selector(closeAgentSession(_:)), + #selector(deleteAgentSession(_:)), + #selector(newAIConversation(_:)), + #selector(switchAIConversation(_:)), + #selector(clearAIConversations(_:)), + #selector(focusAssistant(_:)), + #selector(focusInspector(_:)), + #selector(toggleWorkspaceRail(_:)), + #selector(showPreviousWorkspace(_:)), + #selector(showNextWorkspace(_:)), + #selector(switchToRecentTab(_:)), + #selector(switchToLeastRecentTab(_:)), + #selector(undo(_:)), + #selector(redo(_:)), ] // A command this window does not own stays enabled, which keeps `performClose:` and the system's own items working. @@ -564,6 +569,18 @@ extension MainSplitViewController: NSMenuItemValidation { } } + // Only the connection-level facts, read the way `menuValidationContext` reads them, so a connection off screen can be asked. + func menuValidationContext(forHosted connectionId: UUID) -> MenuValidationContext? { + guard let workspace = workspaces.workspace(for: connectionId) else { return nil } + var context = MenuValidationContext() + context.hasSelectedWorkspace = true + context.isConnected = Self.isConnected(phase: workspace.phase, pane: workspace.resolvedPane) + context.isAgentMode = workspace.resolvedContentMode == .agent + context.supportsServerDashboard = workspace.sessionState?.coordinator.commandActions? + .supportsServerDashboard ?? false + return context + } + // Rail facts and Focus Assistant come from the window: off the connection they dimmed with no coordinator or in Agent mode. var menuValidationContext: MenuValidationContext { let conversations = assistantConversationModel @@ -661,8 +678,8 @@ extension MainSplitViewController: NSMenuItemValidation { canSearchDatabase: actions.canSearchDatabase, canBuildJoinQuery: actions.canBuildJoinQuery, hasMaintenanceOperations: !actions.maintenanceOperations.isEmpty, - canUndo: actions.canUndo, - canRedo: actions.canRedo, + canUndo: contentMode == .browse ? actions.canUndo : agentUndoManager?.canUndo == true, + canRedo: contentMode == .browse ? actions.canRedo : agentUndoManager?.canRedo == true, hasEditorForFind: EditorEventRouter.shared.keyWindowHasEditor, hasSelectionForFind: EditorEventRouter.shared.keyWindowEditorHasSelectionForFind, hasActiveGridFind: actions.hasActiveGridFind, @@ -744,9 +761,9 @@ extension MainSplitViewController: NSMenuItemValidation { case #selector(toggleWorkspaceRail(_:)): setTitle(isWorkspaceRailEnabled ? "Hide Connections" : "Show Connections", on: menuItem) case #selector(undo(_:)): - setResolvedTitle(commandActions?.resolvedUndoTitle ?? String(localized: "Undo"), on: menuItem) + setResolvedTitle(undoTitle, on: menuItem) case #selector(redo(_:)): - setResolvedTitle(commandActions?.resolvedRedoTitle ?? String(localized: "Redo"), on: menuItem) + setResolvedTitle(redoTitle, on: menuItem) case #selector(toggleFilterBar(_:)): setTitle(commandActions?.isFilterBarVisible == true ? "Hide Filter Bar" : "Show Filter Bar", on: menuItem) case #selector(toggleQueryHistory(_:)): @@ -800,6 +817,7 @@ extension MainSplitViewController: NSMenuItemValidation { // The mode is in `representedObject`, so enablement needs the item, not the selector the shared table keys on. private func canShowResultView(_ menuItem: NSMenuItem) -> Bool { + guard contentMode == .browse else { return false } guard let raw = menuItem.representedObject as? String, let mode = ResultsViewMode(rawValue: raw) else { return false } return commandActions?.availableResultsViewModes.contains(mode) ?? false diff --git a/apple/TablePro/Core/Services/Infrastructure/TabWindowController.swift b/apple/TablePro/Core/Services/Infrastructure/TabWindowController.swift index c465b7fdcb..9a451ad053 100644 --- a/apple/TablePro/Core/Services/Infrastructure/TabWindowController.swift +++ b/apple/TablePro/Core/Services/Infrastructure/TabWindowController.swift @@ -237,13 +237,10 @@ internal final class TabWindowController: NSWindowController, NSWindowDelegate { Self.lifecycleLogger.debug("[switch] windowDidBecomeKey seq=\(seq) total ms=\(Int(Date().timeIntervalSince(t0) * 1_000))") } - /// `NSWindow.undoManager` resolves through here, so every undo domain that registers on the - /// window (the data grid's change manager, and the query editor's text view) lands in the - /// selected connection's own history. One window hosts every connection now, so sharing the - /// window's manager let Cmd+Z in one connection roll back an edit made in another. + // Every undo domain registering on the window lands in the selected connection's history for the mode on screen. internal func windowWillReturnUndoManager(_ window: NSWindow) -> UndoManager? { guard let host = window.contentViewController as? MainSplitViewController else { return nil } - return host.workspaces.selected?.undoManager + return host.workspaces.selected?.activeUndoManager } internal func windowDidResignKey(_ notification: Notification) { diff --git a/apple/TablePro/Core/Services/Infrastructure/Toolbar/ConnectionActionsMenuResolver.swift b/apple/TablePro/Core/Services/Infrastructure/Toolbar/ConnectionActionsMenuResolver.swift index d61ea01cf8..4d62d841bf 100644 --- a/apple/TablePro/Core/Services/Infrastructure/Toolbar/ConnectionActionsMenuResolver.swift +++ b/apple/TablePro/Core/Services/Infrastructure/Toolbar/ConnectionActionsMenuResolver.swift @@ -97,7 +97,8 @@ internal enum ConnectionActionsMenuResolver { entries.append( ActionsMenuEntry( title: String(localized: "Restore Previous Values…"), - selector: NSSelectorFromString("restorePreviousValues:") + selector: NSSelectorFromString("restorePreviousValues:"), + shortcut: .restorePreviousValues ) ) } @@ -200,8 +201,7 @@ internal enum ConnectionActionsMenuResolver { ]) } - /// The places in this connection the window can go. Each of these opens a tab or a drawer, so - /// they belong together and away from the commands that change data. + // Each opens a tab or a drawer, so they sit apart from the commands that change data. private static func windowSection(_ context: ToolbarContext) -> ActionsMenuSection? { var entries: [ActionsMenuEntry] = [ ActionsMenuEntry( @@ -215,14 +215,16 @@ internal enum ConnectionActionsMenuResolver { ), ActionsMenuEntry( title: String(localized: "Query Insights"), - selector: NSSelectorFromString("showQueryInsights:") + selector: NSSelectorFromString("showQueryInsights:"), + shortcut: .showQueryInsights ), ] if context.supportsServerDashboard { entries.append( ActionsMenuEntry( title: String(localized: "Server Dashboard"), - selector: NSSelectorFromString("showServerDashboard:") + selector: NSSelectorFromString("showServerDashboard:"), + shortcut: .showServerDashboard ) ) } diff --git a/apple/TablePro/Core/Services/Infrastructure/Toolbar/ToolbarContextResolver.swift b/apple/TablePro/Core/Services/Infrastructure/Toolbar/ToolbarContextResolver.swift index 24da7edd8b..8c30c37b00 100644 --- a/apple/TablePro/Core/Services/Infrastructure/Toolbar/ToolbarContextResolver.swift +++ b/apple/TablePro/Core/Services/Infrastructure/Toolbar/ToolbarContextResolver.swift @@ -121,19 +121,7 @@ internal enum ToolbarContextResolver { } } - /// The items that act on the browse content, which Agent mode does not mount. - /// - /// Two of them leave the titlebar there; the rest are opt-in from the customization palette, so a - /// user who put one back would otherwise have a live button over a surface that is not on screen: - /// Refresh with no grid to reload, Query History flipping a persisted flag for a drawer that is - /// not mounted and that then sprang open on the way back to browsing, New Tab opening a tab - /// behind the conversation, and the commit control over a gate frozen at the moment the mode - /// changed. - /// - /// Stated once rather than as a term in thirteen arms, because it is one rule. The menu bar's - /// `browseContentSelectors` is the same list in its own vocabulary, and - /// `MenuContentModeParityTests` derives this one back out of the toolbar and holds the two - /// together, so an item added here without a menu twin fails rather than ships. + // Agent mode mounts no browse content. `MenuContentModeParityTests` holds these to the menu bar's `agentModeSelectors`. private static let browseContentIdentifiers: Set = [ MainWindowToolbar.refresh, MainWindowToolbar.saveChanges, diff --git a/apple/TablePro/Core/Services/Infrastructure/WorkspaceRailViewController.swift b/apple/TablePro/Core/Services/Infrastructure/WorkspaceRailViewController.swift index 681ef0ce62..4d5d4a64ec 100644 --- a/apple/TablePro/Core/Services/Infrastructure/WorkspaceRailViewController.swift +++ b/apple/TablePro/Core/Services/Infrastructure/WorkspaceRailViewController.swift @@ -87,6 +87,12 @@ internal final class WorkspaceRailTableView: NSTableView { } } +internal struct WorkspaceRailServerTool: Equatable { + internal let title: String + internal let command: Selector + internal let railAction: Selector +} + @MainActor internal final class WorkspaceRailViewController: NSViewController { private var rowSizeObservation: AnyCancellable? @@ -509,42 +515,32 @@ internal final class WorkspaceRailViewController: NSViewController { activate(workspace) } - /// Every rail entry is backed by a live window, so the window is raised directly rather - /// than routed through `TabRouter.openConnection`, which resolves the id against - /// `ConnectionStorage` and would fail for a connection opened from a URL that was never - /// saved. Moving between two containers of the same connection stays in one window and - /// only moves that window's browse cursor. - /// - /// One path, whichever window owns the entry. Selecting the connection in its own window comes - /// first and always: the cross-window case used to raise the target window and move the target - /// connection's browse cursor without ever telling that window to show it, so it came forward - /// still showing the connection it had, its tabs and toolbar named that one, and the work the - /// user clicked moved silently behind it. - /// - /// It ends at `applySelection`, which reads this window's own selection back: a workspace this - /// window hosts leaves the highlight on the row the user picked; one belonging to another window - /// leaves it where it was, because this window did not move. + // `applySelection` reads the window back, so a row another window hosts leaves this rail's highlight where it was. private func activate(_ workspace: WorkspaceID) { + guard let shown = show(workspace) else { return } + moveBrowseCursor(of: shown.window, to: workspace) + applySelection() + } + + // Raised directly: `TabRouter.openConnection` resolves through `ConnectionStorage` and fails for an unsaved URL connection. + private func show(_ workspace: WorkspaceID) -> (window: NSWindow, host: MainSplitViewController)? { guard let window = WindowManager.shared.window(for: workspace.connectionId), - let target = window.contentViewController as? MainSplitViewController else { + let host = window.contentViewController as? MainSplitViewController else { Self.logger.error( - "activate has no window target=\(Self.describe(workspace), privacy: .public)" + "show has no window target=\(Self.describe(workspace), privacy: .public)" ) - return + return nil } - target.selectHostedConnection(workspace.connectionId) + // Before the raise, or the window comes forward still showing the connection it had. + host.selectHostedConnection(workspace.connectionId) - guard window !== view.window else { - moveBrowseCursor(of: window, to: workspace) - applySelection() - return - } + guard window !== view.window else { return (window, host) } let group = window.tabGroup Self.logger.info( """ - activate rail=\(self.railName, privacy: .public) \ + show rail=\(self.railName, privacy: .public) \ target=\(Self.describe(workspace), privacy: .public) \ window=\(window.windowNumber, privacy: .public) visible=\(window.isVisible, privacy: .public) \ tabs=\(group?.windows.count ?? 0, privacy: .public) \ @@ -552,16 +548,13 @@ internal final class WorkspaceRailViewController: NSViewController { """ ) - /// A background tab is a window AppKit will happily make key without bringing to the - /// front of its group, which reads as the click doing nothing. Selecting it in the - /// group first is the documented way to raise a specific tab. + // AppKit makes a background tab key without bringing it to the front of its group. if let group, group.selectedWindow !== window { group.selectedWindow = window } window.makeKeyAndOrderFront(nil) AppActivationPolicyController.shared.activate() - moveBrowseCursor(of: window, to: workspace) - applySelection() + return (window, host) } /// Going to an entry of a connection: its tabs come forward, and the object tree follows to its @@ -618,6 +611,48 @@ internal final class WorkspaceRailViewController: NSViewController { WindowManager.shared.moveToNewWindow(connectionId: workspace.connectionId) } + // Not `showServerDashboard(_:)`: the rail sits ahead of the window in the responder chain and would take the menu bar's command. + @objc + private func showWorkspaceServerDashboard(_ sender: NSMenuItem) { + runServerTool(#selector(MainSplitViewController.showServerDashboard(_:)), from: sender) + } + + @objc + private func showWorkspaceQueryInsights(_ sender: NSMenuItem) { + runServerTool(#selector(MainSplitViewController.showQueryInsights(_:)), from: sender) + } + + // Both tools are server-wide, so the browse cursor stays: going to the row's container would switch databases for nothing. + private func runServerTool(_ command: Selector, from sender: NSMenuItem) { + guard let workspace = sender.representedObject as? WorkspaceID, + let shown = show(workspace) else { return } + applySelection() + guard MainSplitViewController.isEnabled(command, context: shown.host.menuValidationContext) else { return } + NSApp.sendAction(command, to: shown.host, from: sender) + } + + // The Database menu's titles, order and commands, offered on the menu bar's own answer. + internal static func serverTools(offeredWith context: MenuValidationContext) -> [WorkspaceRailServerTool] { + [ + WorkspaceRailServerTool( + title: String(localized: "Server Dashboard"), + command: #selector(MainSplitViewController.showServerDashboard(_:)), + railAction: #selector(showWorkspaceServerDashboard(_:)) + ), + WorkspaceRailServerTool( + title: String(localized: "Query Insights"), + command: #selector(MainSplitViewController.showQueryInsights(_:)), + railAction: #selector(showWorkspaceQueryInsights(_:)) + ), + ].filter { MainSplitViewController.isEnabled($0.command, context: context) } + } + + private static func serverTools(for connectionId: UUID) -> [WorkspaceRailServerTool] { + let host = WindowManager.shared.window(for: connectionId)?.contentViewController as? MainSplitViewController + guard let context = host?.menuValidationContext(forHosted: connectionId) else { return [] } + return serverTools(offeredWith: context) + } + @objc private func closeWorkspace(_ sender: NSMenuItem) { guard let workspace = sender.representedObject as? WorkspaceID else { return } @@ -727,15 +762,16 @@ extension WorkspaceRailViewController: CloseCommandNaming { // MARK: - NSMenuDelegate extension WorkspaceRailViewController: NSMenuDelegate { - /// Lighter action first, the one that ends the connection last, which is the order Finder uses - /// on a Locations row and Mail on an account. Close carries the connection's own name because - /// a row can be one of several a connection has open, and the command takes all of them. internal func menuNeedsUpdate(_ menu: NSMenu) { menu.removeAllItems() let row = tableView.clickedRow guard entries.indices.contains(row) else { return } let entry = entries[row] + populate(menu, for: entry, serverTools: Self.serverTools(for: entry.workspace.connectionId)) + } + // Lighter actions first and the one that ends the connection last, the order Finder and Mail keep. + internal func populate(_ menu: NSMenu, for entry: WorkspaceRailEntry, serverTools: [WorkspaceRailServerTool]) { if WindowManager.shared.canMoveToNewWindow(connectionId: entry.workspace.connectionId) { addItem( to: menu, @@ -746,6 +782,14 @@ extension WorkspaceRailViewController: NSMenuDelegate { menu.addItem(.separator()) } + // No key equivalent: a context item's chord would run on the window's connection, not the row's. + for tool in serverTools { + addItem(to: menu, title: tool.title, action: tool.railAction, workspace: entry.workspace) + } + if !serverTools.isEmpty { + menu.addItem(.separator()) + } + if let releaseTitle = ConnectionFileLockAction.commandTitle( connectionId: entry.workspace.connectionId ) { diff --git a/apple/TablePro/Models/UI/KeyboardShortcutModels.swift b/apple/TablePro/Models/UI/KeyboardShortcutModels.swift index 79b0e75513..675283e65d 100644 --- a/apple/TablePro/Models/UI/KeyboardShortcutModels.swift +++ b/apple/TablePro/Models/UI/KeyboardShortcutModels.swift @@ -59,7 +59,6 @@ enum ShortcutCategory: String, Codable, CaseIterable, Identifiable { // MARK: - Shortcut Action -/// All customizable keyboard shortcut actions enum ShortcutAction: String, Codable, CaseIterable, Identifiable { // Connections case manageConnections @@ -67,6 +66,8 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable { case openDatabase case switchConnection case moveTabToConnection + case showServerDashboard + case showQueryInsights // Editor & Query case openFile @@ -194,7 +195,8 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable { var category: ShortcutCategory { switch self { - case .manageConnections, .newConnection, .openDatabase, .switchConnection, .moveTabToConnection: + case .manageConnections, .newConnection, .openDatabase, .switchConnection, .moveTabToConnection, + .showServerDashboard, .showQueryInsights: return .connections case .openFile, .saveChanges, .saveAs, .copyPath, .copyRelativePath, .executeQuery, .executeAllStatements, .executeQueryWithoutLimit, .cancelQuery, .explainQuery, .formatQuery, @@ -302,6 +304,8 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable { case .openFile: return String(localized: "Open File") case .switchConnection: return String(localized: "Switch Connection") case .moveTabToConnection: return String(localized: "Move Tab to Connection") + case .showServerDashboard: return String(localized: "Server Dashboard") + case .showQueryInsights: return String(localized: "Query Insights") case .saveChanges: return String(localized: "Save Changes") case .saveAs: return String(localized: "Save As") case .copyPath: return String(localized: "Copy Path") @@ -654,14 +658,15 @@ struct KeyboardSettings: Codable, Equatable { // MARK: - Default Shortcuts - /// Default shortcuts, applied when the user has no override. An action absent - /// from this map has no default and shows as unassigned until the user binds it. + // An action absent here shows as unassigned until the user binds it. static let defaultShortcuts: [ShortcutAction: BoundKey] = [ // Connections .newConnection: .character("n", command: true), .openDatabase: .character("k", command: true), .switchConnection: .character("c", command: true, control: true), .moveTabToConnection: .character("c", command: true, option: true, control: true), + .showServerDashboard: .character("d", command: true, shift: true, option: true), + .showQueryInsights: .character("i", command: true, shift: true, option: true), // Editor & Query .openFile: .character("o", command: true), diff --git a/apple/TablePro/Views/AIChat/ChatComposerTextView.swift b/apple/TablePro/Views/AIChat/ChatComposerTextView.swift index abbb44c972..9be8004b76 100644 --- a/apple/TablePro/Views/AIChat/ChatComposerTextView.swift +++ b/apple/TablePro/Views/AIChat/ChatComposerTextView.swift @@ -236,6 +236,40 @@ final class ChatComposerNSTextView: NSTextView { return textView } + // Its own typing history: the window's is the connection's, and the window's Undo routes into tab histories. + private let typingUndoManager = UndoManager() + + override var undoManager: UndoManager? { + typingUndoManager + } + + @objc func undo(_ sender: Any?) { + typingUndoManager.undo() + } + + @objc func redo(_ sender: Any?) { + typingUndoManager.redo() + } + + override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { + switch menuItem.action { + case #selector(undo(_:)): + Self.setTitle(typingUndoManager.undoMenuItemTitle, on: menuItem) + return typingUndoManager.canUndo + case #selector(redo(_:)): + Self.setTitle(typingUndoManager.redoMenuItemTitle, on: menuItem) + return typingUndoManager.canRedo + default: + return super.validateMenuItem(menuItem) + } + } + + // A title write re-lays out an open menu and cancels its tracking. + private static func setTitle(_ title: String, on menuItem: NSMenuItem) { + guard menuItem.title != title else { return } + menuItem.title = title + } + override func becomeFirstResponder() -> Bool { let became = super.becomeFirstResponder() if became { onFocusChange?(true) } diff --git a/apple/TablePro/Views/Main/MainContentCommandActions+BulkClose.swift b/apple/TablePro/Views/Main/MainContentCommandActions+BulkClose.swift index daf9947399..30d27f936c 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions+BulkClose.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions+BulkClose.swift @@ -61,6 +61,22 @@ extension MainContentCommandActions { ) } + // What Close does with no tab on screen: the container, else the connection. + func closeWorkspace() { + guard let coordinator else { + Task { await closeWindowAwaiting() } + return + } + if let entry = WorkspaceCloseAction.closableEntry( + connectionId: connectionId, + container: coordinator.tabManager.visibleContainer + ) { + Task { await WorkspaceCloseAction.close(entry.workspace) } + return + } + Task { await ConnectionCloseAction.close(connectionId: connectionId) } + } + // A list edit, not a window close: the window hosts every connection, so closing all tabs leaves this one empty. private func runBatchClose(kind: BatchCloseKind) async { guard let coordinator else { return } @@ -96,9 +112,16 @@ extension MainContentCommandActions { // Save cannot reach off-screen grid edits, staged principals or table drafts; those tabs stay open and the alert says so. private func unsavedWorkMessage(for victims: [QueryTab]) -> String { - guard let coordinator, - victims.contains(where: { coordinator.savability(of: $0) == .mountedOnly }) - else { + Self.unsavedWorkMessage(keepsTabsOpen: holdsWorkOnlyItsTabCanSave(among: victims)) + } + + func holdsWorkOnlyItsTabCanSave(among victims: [QueryTab]) -> Bool { + guard let coordinator else { return false } + return victims.contains { coordinator.savability(of: $0) == .mountedOnly } + } + + static func unsavedWorkMessage(keepsTabsOpen: Bool) -> String { + guard keepsTabsOpen else { return String(localized: "Your changes will be lost if you don't save them.") } return String( @@ -109,6 +132,16 @@ extension MainContentCommandActions { ) } + // Closing the connection takes every tab, so Save has to reach each one; false keeps the connection open. + func saveEveryTabsWork() async -> Bool { + guard let coordinator else { return true } + coordinator.commitActiveGridEdit() + let tabs = coordinator.tabManager.tabs + guard !tabs.isEmpty else { return await saveSelectedTabWork() } + let saved = await saveVictims(tabs) + return tabs.allSatisfy { saved.contains($0.id) } + } + // A victim it cannot save keeps its tab, a file conflict too: the conflict sheet is one window-level slot with no queue. private func saveVictims(_ victims: [QueryTab]) async -> Set { guard let coordinator else { return Set(victims.map(\.id)) } diff --git a/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift b/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift index f35e03c609..b3f291f407 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift @@ -20,11 +20,10 @@ extension StructureEditingSession: StructureUndoHistory {} extension CreateTableDraft: StructureUndoHistory {} -// Users & Roles, the diagram and the structure editor keep their own histories; everything else registers on the -// window's undo manager. Availability, title and command resolve through the same branch so they cannot disagree. +// Users & Roles, the diagram and the structure editor keep their own histories; everything else uses the browse one. extension MainContentCommandActions { private var windowUndoManager: UndoManager? { - coordinator?.contentWindow?.undoManager + coordinator?.browseUndoManager } // A Create Table tab keeps its view mode at Data, so it is asked for by tab type. diff --git a/apple/TablePro/Views/Main/MainContentCommandActions.swift b/apple/TablePro/Views/Main/MainContentCommandActions.swift index 9db904f8a0..ff81dffedd 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions.swift @@ -139,18 +139,25 @@ final class MainContentCommandActions: ObservableObject { // MARK: - Save Action private func saveInspectorEdits() { - let editState = trailingPaneState.inspector.editState Task { [weak self] in - guard let self else { return } - do { - try await self.coordinator?.saveSidebarEdits(editState: editState) - } catch { - AlertHelper.showErrorSheet( - title: String(localized: "Failed to Save Changes"), - message: error.localizedDescription, - window: self.window - ) - } + await self?.saveInspectorEditsAwaiting() + } + } + + // A close waits on this: started and left, the write could land after the window it reports to closed. + @discardableResult + private func saveInspectorEditsAwaiting() async -> Bool { + let editState = trailingPaneState.inspector.editState + do { + try await coordinator?.saveSidebarEdits(editState: editState) + return true + } catch { + AlertHelper.showErrorSheet( + title: String(localized: "Failed to Save Changes"), + message: error.localizedDescription, + window: window + ) + return false } } @@ -677,14 +684,7 @@ final class MainContentCommandActions: ObservableObject { closeTab(id: selected.id) return } - if let entry = WorkspaceCloseAction.closableEntry( - connectionId: connectionId, - container: coordinator.tabManager.visibleContainer - ) { - Task { await WorkspaceCloseAction.close(entry.workspace) } - return - } - Task { await ConnectionCloseAction.close(connectionId: connectionId) } + closeWorkspace() } // asBatchSurvivor: nil for a lone close (the window decides), true for the window a batch keeps blank, false otherwise. @@ -804,10 +804,8 @@ final class MainContentCommandActions: ObservableObject { } } - // Sidebar-only edits (made directly in the inspector panel) if trailingPaneState.inspector.editState.hasEdits { - saveInspectorEdits() - return true + return await saveInspectorEditsAwaiting() } // File save (query editor with source file) diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index ed69bb516c..5c95425d6b 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -240,6 +240,12 @@ final class MainContentCoordinator: ObservableObject { // For alerts instead of NSApp.keyWindow, which may return a sheet window and leave dialogs stuck. weak var contentWindow: NSWindow? + // Its own workspace's browse history: the window's answer follows the connection and mode on screen. + var browseUndoManager: UndoManager? { + let host = contentWindow?.contentViewController as? MainSplitViewController + return host?.workspaces.workspace(for: connectionId)?.undoManager ?? contentWindow?.undoManager + } + // No SwiftUI `Scene` in the AppKit lifecycle, so a focused value can't resolve; this reaches AppKit and SwiftUI callers. weak var commandActions: MainContentCommandActions? @@ -602,7 +608,7 @@ final class MainContentCoordinator: ObservableObject { self.persistence = TabPersistenceCoordinator.forConnection(connection.id) ConnectionDataCache.shared(for: connection.id).ensureLoaded() - changeManager.undoManagerProvider = { [weak self] in self?.contentWindow?.undoManager } + changeManager.undoManagerProvider = { [weak self] in self?.browseUndoManager } changeManager.onUndoApplied = { [weak self] result in self?.handleUndoResult(result) } tabManager.onVisibleContainerReconciled = { [weak self] in self?.browseCursorCoordinator.followVisibleContainer() diff --git a/apple/TableProTests/Core/Menu/ChromeSnapshot.json b/apple/TableProTests/Core/Menu/ChromeSnapshot.json index 538e3869b2..23e6ab856e 100644 --- a/apple/TableProTests/Core/Menu/ChromeSnapshot.json +++ b/apple/TableProTests/Core/Menu/ChromeSnapshot.json @@ -220,9 +220,9 @@ {"path": "Database", "separator": true}, {"path": "Database > View ER Diagram", "action": "showERDiagram:"}, {"path": "Database > Build Join Query…", "action": "buildJoinQuery:"}, - {"path": "Database > Server Dashboard", "action": "showServerDashboard:"}, + {"path": "Database > Server Dashboard", "identifier": "shortcut.showServerDashboard", "action": "showServerDashboard:", "key": "d", "modifiers": ["option", "shift", "command"]}, {"path": "Database > Users & Roles", "action": "showUsersAndRoles:"}, - {"path": "Database > Query Insights", "action": "showQueryInsights:"}, + {"path": "Database > Query Insights", "identifier": "shortcut.showQueryInsights", "action": "showQueryInsights:", "key": "i", "modifiers": ["option", "shift", "command"]}, {"path": "Database", "separator": true}, {"path": "Database > Release File Lock", "action": "releaseFileLock:"}, {"path": "Database > Disconnect", "action": "requestDisconnect"}, diff --git a/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift b/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift index a05e9c6725..cf97f14665 100644 --- a/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift +++ b/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift @@ -786,7 +786,6 @@ struct MainMenuValidationTests { context.canInsertRows = true context.canDeleteRows = true context.supportsTableHealth = true - context.isCurrentTabEditable = true context.isCurrentTabSchemaResolved = true context.hasTableSelection = true context.hasRowSelection = true @@ -911,6 +910,26 @@ struct MainMenuValidationTests { } } + // Each opens a tab, and Agent mode does not mount the tab strip, so the tab would wait unseen behind the conversation. + @Test("Agent mode dims the commands that open a database tool's tab") + func databaseToolsDimInAgentMode() { + var context = capableContext() + context.isConnected = true + let toolCommands = [ + #selector(MainSplitViewController.showServerDashboard(_:)), + #selector(MainSplitViewController.showQueryInsights(_:)), + #selector(MainSplitViewController.showUsersAndRoles(_:)), + #selector(MainSplitViewController.showERDiagram(_:)) + ] + for selector in toolCommands { + #expect(enabled(selector, context), "\(selector) is dim while browsing") + } + context.isAgentMode = true + for selector in toolCommands { + #expect(!enabled(selector, context), "\(selector) stayed lit in Agent mode") + } + } + @Test("A stale selection does not keep content commands enabled without a connection") func contentCommandsNeedTheConnection() { var context = capableContext() diff --git a/apple/TableProTests/Core/Services/Infrastructure/AgentModeWindowTests.swift b/apple/TableProTests/Core/Services/Infrastructure/AgentModeWindowTests.swift index 0aebd89ab2..0828d5799f 100644 --- a/apple/TableProTests/Core/Services/Infrastructure/AgentModeWindowTests.swift +++ b/apple/TableProTests/Core/Services/Infrastructure/AgentModeWindowTests.swift @@ -217,6 +217,66 @@ struct AgentModeWindowTests { #expect(harness.detailItem.minimumThickness == UsersRolesLayoutMetrics.tabMinimumWidth) } + // The browse tab and grid stay behind the conversation, so Close and Undo must not reach them. + @Test("In Agent mode Close names the connection and Undo runs the mode's own history") + func agentModeCloseAndUndoSkipTheHiddenTab() throws { + let harness = try Harness() + defer { harness.tearDown() } + try harness.requireContent() + let tabManager = try #require(harness.selected.sessionState?.tabManager) + tabManager.adoptTab(QueryTab(id: UUID(), title: "Users & Roles", query: "", tabType: .usersRoles)) + var undoneBrowse = false + var undoneAgent = false + Self.register(on: harness.selected.undoManager) { undoneBrowse = true } + + #expect(harness.controller.closeCommandTitle == String(localized: "Close Tab")) + #expect(harness.selected.activeUndoManager === harness.selected.undoManager) + + harness.controller.setContentMode(.agent) + let title = try #require(harness.controller.closeCommandTitle) + #expect(title != String(localized: "Close Tab")) + #expect(harness.selected.activeUndoManager === harness.selected.agentUndoManager) + #expect(!harness.controller.menuValidationContext.canUndo, "A hidden browse edit lit Undo") + + Self.register(on: harness.selected.agentUndoManager) { undoneAgent = true } + #expect(harness.controller.menuValidationContext.canUndo) + #expect(harness.controller.undoTitle == harness.selected.agentUndoManager.undoMenuItemTitle) + harness.controller.undo(nil) + #expect(undoneAgent) + #expect(!undoneBrowse) + } + + private static func register(on manager: UndoManager, _ action: @escaping @MainActor () -> Void) { + manager.groupsByEvent = false + manager.beginUndoGrouping() + manager.registerUndo(withTarget: manager) { _ in action() } + manager.endUndoGrouping() + } + + // The strip asks this of a row's connection before offering a command, so it has to read what the window reads. + @Test("A hosted connection's command facts match the window's own, and Agent mode offers no server tool") + func hostedContextMatchesTheWindow() throws { + let harness = try Harness(type: .postgresql) + defer { harness.tearDown() } + try harness.requireContent() + + for mode in ConnectionWorkspaceContentMode.allCases { + harness.controller.setContentMode(mode) + let own = harness.controller.menuValidationContext + let hosted = try #require(harness.controller.menuValidationContext(forHosted: harness.connection.id)) + #expect(hosted.isConnected == own.isConnected, "\(mode.rawValue)") + #expect(hosted.isAgentMode == own.isAgentMode, "\(mode.rawValue)") + #expect(hosted.supportsServerDashboard == own.supportsServerDashboard, "\(mode.rawValue)") + + let offered = WorkspaceRailViewController.serverTools(offeredWith: hosted).map(\.title) + let expected = mode == .browse + ? [String(localized: "Server Dashboard"), String(localized: "Query Insights")] + : [] + #expect(offered == expected, "\(mode.rawValue)") + } + #expect(harness.controller.menuValidationContext(forHosted: UUID()) == nil) + } + /// The error, Retry and Manage Connections live on the browse side's unavailable screen, and a /// composer with none of them is a dead end. A reconnect hands the column back. @Test("A connection that drops in Agent mode shows the unavailable screen, and a reconnect the conversation") diff --git a/apple/TableProTests/Core/Services/Infrastructure/ConnectionActionsMenuResolverTests.swift b/apple/TableProTests/Core/Services/Infrastructure/ConnectionActionsMenuResolverTests.swift index d760189a91..0c90fd6701 100644 --- a/apple/TableProTests/Core/Services/Infrastructure/ConnectionActionsMenuResolverTests.swift +++ b/apple/TableProTests/Core/Services/Infrastructure/ConnectionActionsMenuResolverTests.swift @@ -108,6 +108,40 @@ struct ConnectionActionsMenuResolverTests { } } + // The delegate draws a row's chord only when the entry declares one. + @Test("Every command entry declares the shortcut its menu-bar twin carries") + @MainActor + func entriesCarryTheirTwinsShortcut() { + var twinShortcuts: [String: ShortcutAction] = [:] + collectShortcuts(from: MainMenuBuilder.build(keyboard: KeyboardSettings()).bar, into: &twinShortcuts) + + #expect(twinShortcuts.count > 20, "Only \(twinShortcuts.count) shortcuts collected; the walk missed the menus") + for tabKind in Self.tabKinds + [nil] { + for contentMode in ConnectionWorkspaceContentMode.allCases { + for isConnected in [true, false] { + let context = Self.context(tabKind: tabKind, contentMode: contentMode, isConnected: isConnected) + for entry in Self.entries(context) { + guard case let .command(_, shortcut) = entry.role else { continue } + #expect(shortcut == twinShortcuts[entry.title], "\(entry.title)") + } + } + } + } + } + + @MainActor + private func collectShortcuts(from menu: NSMenu, into shortcuts: inout [String: ShortcutAction]) { + let actions = Dictionary( + uniqueKeysWithValues: ShortcutAction.allCases.map { (MenuItemFactory.identifier(for: $0), $0) } + ) + for item in menu.items where !item.isSeparatorItem { + if let identifier = item.identifier, let action = actions[identifier] { + shortcuts[item.title] = action + } + if let submenu = item.submenu { collectShortcuts(from: submenu, into: &shortcuts) } + } + } + @MainActor private func collectTitles(from menu: NSMenu, into titles: inout Set) { for item in menu.items where !item.isSeparatorItem { diff --git a/apple/TableProTests/Core/Services/Infrastructure/MenuActionSelectorCollisionTests.swift b/apple/TableProTests/Core/Services/Infrastructure/MenuActionSelectorCollisionTests.swift index 72ea8d9a74..621aa50020 100644 --- a/apple/TableProTests/Core/Services/Infrastructure/MenuActionSelectorCollisionTests.swift +++ b/apple/TableProTests/Core/Services/Infrastructure/MenuActionSelectorCollisionTests.swift @@ -28,7 +28,9 @@ struct MenuActionSelectorCollisionTests { #selector(MainSplitViewController.exportTables(_:)), #selector(MainSplitViewController.exportQueryResults(_:)), #selector(MainSplitViewController.backupDatabase(_:)), - #selector(MainSplitViewController.restoreDatabase(_:)) + #selector(MainSplitViewController.restoreDatabase(_:)), + #selector(MainSplitViewController.showServerDashboard(_:)), + #selector(MainSplitViewController.showQueryInsights(_:)) ] ) func railDoesNotShadowMenuBarCommands(command: Selector) { diff --git a/apple/TableProTests/Core/Services/Infrastructure/MenuContentModeParityTests.swift b/apple/TableProTests/Core/Services/Infrastructure/MenuContentModeParityTests.swift index 95af34205e..0c9871d298 100644 --- a/apple/TableProTests/Core/Services/Infrastructure/MenuContentModeParityTests.swift +++ b/apple/TableProTests/Core/Services/Infrastructure/MenuContentModeParityTests.swift @@ -222,6 +222,57 @@ struct MenuContentModeParityTests { } } + // Each was lit in Agent mode and acted on the browse tab, grid, sidebar selection or sheet the mode hides. + @Test("Agent mode dims the commands that act on hidden browse content", arguments: [ + #selector(MainSplitViewController.createNewTable(_:)), + #selector(MainSplitViewController.showTableStructure(_:)), + #selector(MainSplitViewController.executeQuery(_:)), + #selector(MainSplitViewController.duplicateRow(_:)), + #selector(MainSplitViewController.copy(_:)), + #selector(MainSplitViewController.paste(_:)), + #selector(MainSplitViewController.delete(_:)), + #selector(MainSplitViewController.closeOtherTabs(_:)), + #selector(MainSplitViewController.closeAllTabs(_:)), + #selector(MainSplitViewController.goToNextPage(_:)), + #selector(MainSplitViewController.toggleFilterBar(_:)), + #selector(MainSplitViewController.focusResults(_:)), + #selector(MainSplitViewController.exportQueryResults(_:)), + #selector(MainSplitViewController.backupDatabase(_:)), + #selector(MainSplitViewController.createNewDatabase(_:)), + #selector(MainSplitViewController.editCurrentSchema(_:)), + #selector(MainSplitViewController.showQueryInsights(_:)), + ]) + func agentModeDimsHiddenContentCommands(command: Selector) { + var browse = Self.capableMenuContext(.browse) + var agent = Self.capableMenuContext(.agent) + browse.isAgentMode = false + agent.isAgentMode = true + #expect(MainSplitViewController.isEnabled(command, context: browse), "\(NSStringFromSelector(command)) is dim while browsing") + #expect(!MainSplitViewController.isEnabled(command, context: agent), "\(NSStringFromSelector(command)) stayed lit in Agent mode") + } + + // Everything a browse command could ask for, so only the mode can dim it. + private static func capableMenuContext(_ contentMode: ConnectionWorkspaceContentMode) -> MenuValidationContext { + var context = menuContext(contentMode) + context.canCreateTable = true + context.canCreateDatabase = true + context.canShowTableStructure = true + context.hasQueryText = true + context.hasRowSelection = true + context.hasTableSelection = true + context.hasResultRows = true + context.canPasteRows = true + context.canDropSelectedTables = true + context.canCloseOtherTabs = true + context.canCloseAllTabs = true + context.canNavigatePages = true + context.canUseTableResultCommands = true + context.canFocusResults = true + context.supportsBackup = true + context.canCreateSchema = true + return context + } + /// Named rather than left to the equality above, which two disabled answers would also satisfy. @Test("The window's own commands still answer in Agent mode") func theWindowsCommandsAnswerInAgentMode() { diff --git a/apple/TableProTests/Core/Services/Infrastructure/WorkspaceRailServerToolTests.swift b/apple/TableProTests/Core/Services/Infrastructure/WorkspaceRailServerToolTests.swift new file mode 100644 index 0000000000..b689db04d5 --- /dev/null +++ b/apple/TableProTests/Core/Services/Infrastructure/WorkspaceRailServerToolTests.swift @@ -0,0 +1,101 @@ +// +// WorkspaceRailServerToolTests.swift +// TableProTests +// + +import AppKit +@testable import TablePro +import Testing + +@MainActor +struct WorkspaceRailServerToolTests { + private static let dashboard = String(localized: "Server Dashboard") + private static let insights = String(localized: "Query Insights") + + private static func context( + isConnected: Bool = true, + isAgentMode: Bool = false, + supportsServerDashboard: Bool = true + ) -> MenuValidationContext { + var context = MenuValidationContext() + context.hasSelectedWorkspace = true + context.isConnected = isConnected + context.isAgentMode = isAgentMode + context.supportsServerDashboard = supportsServerDashboard + return context + } + + private static func offered(_ context: MenuValidationContext) -> [String] { + WorkspaceRailViewController.serverTools(offeredWith: context).map(\.title) + } + + @Test("A browsing connection gets both tools in the Database menu's order") + func browsingOffersBoth() { + #expect(Self.offered(Self.context()) == [Self.dashboard, Self.insights]) + } + + @Test("An engine without a dashboard gets Query Insights only") + func dashboardFollowsTheEngine() { + #expect(Self.offered(Self.context(supportsServerDashboard: false)) == [Self.insights]) + } + + @Test("Agent mode and a connection that is not live get neither") + func neitherWithoutBrowseContent() { + #expect(Self.offered(Self.context(isAgentMode: true)).isEmpty) + #expect(Self.offered(Self.context(isConnected: false)).isEmpty) + } + + @Test("Each tool runs the menu bar's own command") + func toolsRunTheMenuBarCommands() { + let commands = WorkspaceRailViewController.serverTools(offeredWith: Self.context()).map(\.command) + #expect(commands == [ + #selector(MainSplitViewController.showServerDashboard(_:)), + #selector(MainSplitViewController.showQueryInsights(_:)), + ]) + } + + private static func entry() -> WorkspaceRailEntry { + WorkspaceRailEntry( + workspace: WorkspaceID(connectionId: UUID(), container: ""), + connection: TestFixtures.makeConnection(name: "Primary", type: .postgresql), + status: .connected + ) + } + + private static func layout(of menu: NSMenu) -> [String] { + menu.items.map { $0.isSeparatorItem ? "---" : $0.title } + } + + @Test("The tools sit in their own group, targeted at the rail and naming the row, with no chord") + func toolItemsAreGroupedAndNameTheRow() { + let rail = WorkspaceRailViewController() + let entry = Self.entry() + let tools = WorkspaceRailViewController.serverTools(offeredWith: Self.context()) + let menu = NSMenu() + + rail.populate(menu, for: entry, serverTools: tools) + + let close = String(format: String(localized: "Close Connection “%@”"), entry.connection.name) + #expect(Self.layout(of: menu) == [ + Self.dashboard, Self.insights, "---", String(localized: "Disconnect"), "---", close, + ]) + for tool in tools { + let item = menu.items.first { $0.title == tool.title } + #expect(item?.action == tool.railAction) + #expect(item?.target === rail) + #expect(item?.representedObject as? WorkspaceID == entry.workspace) + #expect(item?.keyEquivalent.isEmpty == true, "\(tool.title) draws a chord the row cannot run") + } + } + + @Test("No tools leaves no empty group behind") + func noToolsNoSeparator() { + let entry = Self.entry() + let menu = NSMenu() + + WorkspaceRailViewController().populate(menu, for: entry, serverTools: []) + + let close = String(format: String(localized: "Close Connection “%@”"), entry.connection.name) + #expect(Self.layout(of: menu) == [String(localized: "Disconnect"), "---", close]) + } +} diff --git a/apple/TableProTests/Models/ShortcutUniquenessTests.swift b/apple/TableProTests/Models/ShortcutUniquenessTests.swift index 2b49c46e85..3bc64bbb25 100644 --- a/apple/TableProTests/Models/ShortcutUniquenessTests.swift +++ b/apple/TableProTests/Models/ShortcutUniquenessTests.swift @@ -76,6 +76,7 @@ struct ShortcutUniquenessTests { ShortcutAction.showTablesList, .showFavoritesList, .restorePreviousValues, .newAgentSession, .openAgentSession, .closeAgentSession, .deleteAgentSession, .newAIConversation, .copyPath, .copyRelativePath, .setCellsToValue, .pinTab, + .showServerDashboard, .showQueryInsights, ]) func newlyRebindableCommandsAreListable(action: ShortcutAction) { #expect(!action.displayName.isEmpty) diff --git a/apple/TableProTests/Views/AIChat/ChatComposerScrollViewTests.swift b/apple/TableProTests/Views/AIChat/ChatComposerScrollViewTests.swift index 1373f53339..d52199c44c 100644 --- a/apple/TableProTests/Views/AIChat/ChatComposerScrollViewTests.swift +++ b/apple/TableProTests/Views/AIChat/ChatComposerScrollViewTests.swift @@ -17,6 +17,36 @@ struct ChatComposerScrollViewTests { return scrollView } + // The window's Undo routes into tab histories, so the composer answers Undo with its own typing. + @Test("Typing undoes in the composer's own history, never the window's") + func typingUndoStaysInTheComposer() { + let textView = ChatComposerNSTextView.make() + let window = NSWindow( + contentRect: NSRect(x: 0, y: 0, width: 300, height: 100), + styleMask: [.titled], + backing: .buffered, + defer: false + ) + window.isReleasedWhenClosed = false + window.contentView = textView + defer { window.orderOut(nil) } + + #expect(textView.undoManager !== window.undoManager) + textView.insertText("select 1", replacementRange: NSRange(location: 0, length: 0)) + textView.breakUndoCoalescing() + #expect(window.undoManager?.canUndo != true, "Typing landed in the window's history") + + let undo = NSMenuItem(title: "Undo", action: #selector(ChatComposerNSTextView.undo(_:)), keyEquivalent: "z") + #expect(textView.validateMenuItem(undo)) + textView.undo(nil) + #expect(textView.string.isEmpty) + + let redo = NSMenuItem(title: "Redo", action: #selector(ChatComposerNSTextView.redo(_:)), keyEquivalent: "Z") + #expect(textView.validateMenuItem(redo)) + textView.redo(nil) + #expect(textView.string == "select 1") + } + @Test("The text view width follows the scroll view width") func documentWidthFollowsScrollView() throws { let scrollView = makeComposer(width: 420) diff --git a/apple/TableProTests/Views/Main/CommandActionsBulkCloseTests.swift b/apple/TableProTests/Views/Main/CommandActionsBulkCloseTests.swift index 313f4300ca..762910ee39 100644 --- a/apple/TableProTests/Views/Main/CommandActionsBulkCloseTests.swift +++ b/apple/TableProTests/Views/Main/CommandActionsBulkCloseTests.swift @@ -289,6 +289,37 @@ struct CommandActionsBulkCloseTests { #expect(current.coordinator.savability(of: selected) == .nothingAtRisk) } + // Closing the connection used to save only the selected tab, then close over a background tab's edits. + @Test("closing the connection keeps it open when a background tab holds work only that tab can save") + func connectionSaveRefusesUnreachableBackgroundWork() async throws { + let connection = TestFixtures.makeConnection(database: "db_a") + let current = makeWindow(connection: connection) + defer { current.coordinator.teardown() } + + current.coordinator.tabManager.addTab(initialQuery: "SELECT 1", databaseName: "db_a") + current.coordinator.tabManager.addTab(initialQuery: "SELECT 2", databaseName: "db_a") + let background = try #require(current.coordinator.tabManager.tabs.first) + current.coordinator.tabManager.mutate(tabId: background.id) { tab in + tab.pendingChanges.deletedRowIDs = [.existing(0)] + } + + #expect(current.actions.holdsWorkOnlyItsTabCanSave(among: current.coordinator.tabManager.tabs)) + #expect(await current.actions.saveEveryTabsWork() == false) + } + + @Test("closing a clean connection saves nothing and lets it close") + func connectionSaveOfCleanTabsCloses() async { + let connection = TestFixtures.makeConnection(database: "db_a") + let current = makeWindow(connection: connection) + defer { current.coordinator.teardown() } + + current.coordinator.tabManager.addTab(initialQuery: "SELECT 1", databaseName: "db_a") + current.coordinator.tabManager.addTab(initialQuery: "SELECT 2", databaseName: "db_a") + + #expect(!current.actions.holdsWorkOnlyItsTabCanSave(among: current.coordinator.tabManager.tabs)) + #expect(await current.actions.saveEveryTabsWork()) + } + @Test("a clean batch closes without asking") func cleanBatchNeedsNoPrompt() async { let connection = TestFixtures.makeConnection(database: "db_a") diff --git a/apple/changelog.d/3957.added.md b/apple/changelog.d/3957.added.md new file mode 100644 index 0000000000..d7147478dc --- /dev/null +++ b/apple/changelog.d/3957.added.md @@ -0,0 +1 @@ +Server Dashboard and Query Insights open with ⌥⇧⌘D and ⌥⇧⌘I, or from a connection's menu on the connections strip. diff --git a/apple/changelog.d/3957.fixed.1.md b/apple/changelog.d/3957.fixed.1.md new file mode 100644 index 0000000000..cfa4997869 --- /dev/null +++ b/apple/changelog.d/3957.fixed.1.md @@ -0,0 +1 @@ +Undo in the AI chat composer undid a Structure, Users & Roles or ER diagram edit instead of the typing. diff --git a/apple/changelog.d/3957.fixed.2.md b/apple/changelog.d/3957.fixed.2.md new file mode 100644 index 0000000000..cc1c86e51b --- /dev/null +++ b/apple/changelog.d/3957.fixed.2.md @@ -0,0 +1 @@ +Close Connection with Save closed over unsaved edits in tabs other than the one in front. diff --git a/apple/changelog.d/3957.fixed.3.md b/apple/changelog.d/3957.fixed.3.md new file mode 100644 index 0000000000..ef45f88f84 --- /dev/null +++ b/apple/changelog.d/3957.fixed.3.md @@ -0,0 +1 @@ +Save at the close prompt closed before inspector edits were written, so a failed write went unreported. diff --git a/apple/changelog.d/3957.fixed.md b/apple/changelog.d/3957.fixed.md new file mode 100644 index 0000000000..235620511c --- /dev/null +++ b/apple/changelog.d/3957.fixed.md @@ -0,0 +1 @@ +In Agent mode, menu commands and shortcuts acted on the hidden browse tabs, grid and sidebar. From 4617d7aa29bb30a8b896209188c7c76144c713b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Nam=20Long?= Date: Sun, 1 Nov 2026 15:14:51 +0700 Subject: [PATCH 78/85] feat(linux-connections): set a query timeout per connection (#3977) --- linux/ROADMAP.md | 2 +- linux/changelog.d/3977.added.md | 1 + linux/crates/app/src/application/tests.rs | 1 + .../app/src/services/connection_deletion.rs | 1 + .../app/src/services/connection_search.rs | 1 + .../app/src/services/file_resolution.rs | 1 + .../src/services/saved_connection_editor.rs | 1 + .../app/src/services/saved_connection_list.rs | 1 + .../src/services/saved_connection_object.rs | 1 + .../src/services/saved_connection_order.rs | 1 + .../crates/app/src/services/secret_changes.rs | 1 + .../crates/app/src/services/secret_labels.rs | 1 + .../app/src/ui/connect_dialog/connect_form.rs | 5 ++ linux/crates/app/src/ui/connect_dialog/imp.rs | 4 ++ linux/crates/app/src/ui/connect_dialog/mod.rs | 14 ++++- .../crates/app/src/ui/connect_dialog/tests.rs | 59 ++++++++++++++++++ linux/crates/app/src/ui/editor/mod.rs | 5 +- linux/crates/app/src/ui/editor/tests.rs | 36 ++++++++++- .../ui/history_dialog/connection_filter.rs | 1 + .../crates/app/src/ui/history_dialog/tests.rs | 1 + .../app/src/ui/picker/connection_row.rs | 1 + linux/crates/app/src/ui/picker/tests.rs | 1 + .../ui/session_window/connect_failure_view.rs | 1 + .../app/src/ui/session_window/connect_flow.rs | 1 + .../app/src/ui/session_window/secret_retry.rs | 1 + .../app/src/ui/session_window/tests/mod.rs | 2 + .../app/src/ui/tags/manage_tags_dialog.rs | 1 + .../app/src/ui/workspace/sidebar/tests.rs | 1 + linux/crates/app/tests/accessibility/world.rs | 1 + .../crates/app/tests/application_lifecycle.rs | 1 + linux/crates/app/tests/secret_save_flow.rs | 1 + linux/crates/core/src/lib.rs | 2 + linux/crates/core/src/query_timeout.rs | 60 +++++++++++++++++++ linux/crates/core/src/workspace_settings.rs | 11 ++++ linux/crates/mcp/tests/mcp_server.rs | 1 + .../src/connect/persist_pending_save.rs | 1 + .../session/src/connect/resolve_saved.rs | 44 +++++++++++++- .../session/src/registry/session_key.rs | 1 + linux/crates/storage/src/connections.rs | 7 +++ .../src/credentials/keyring_credentials.rs | 1 + linux/crates/storage/tests/isolation.rs | 1 + linux/crates/storage/tests/stored_formats.rs | 49 +++++++++++++++ linux/data/resources/ui/connect-dialog.ui | 24 ++++++++ linux/docs/storage.md | 1 + 44 files changed, 344 insertions(+), 9 deletions(-) create mode 100644 linux/changelog.d/3977.added.md create mode 100644 linux/crates/core/src/query_timeout.rs diff --git a/linux/ROADMAP.md b/linux/ROADMAP.md index ffdb27ffe5..7e927c5058 100644 --- a/linux/ROADMAP.md +++ b/linux/ROADMAP.md @@ -18,6 +18,7 @@ The app runs on GNOME 50 ([ADR 0015](docs/decisions/0015-gnome-50-platform-floor | Test before saving | Test connects once and reports, then disconnects | | Passwords | Kept in the Secret Service; a prompt asks when one is missing or refused, with Remember Password | | Safe Mode | A level per connection, set in the connect dialog or the main menu while connected and saved back to the connection. Off asks before DROP, TRUNCATE, and DELETE or UPDATE without WHERE. Confirm Writes also asks before every other write from the editor, a grid save, a structure save, a CSV import or Drop Table, showing the SQL and the connection, with Cancel the default. Read-Only refuses every editor statement that does not read, and opens the connection read-only: SQLite, DuckDB and ClickHouse refuse writes at the engine, PostgreSQL and MySQL run in read-only transactions, SQL Server relies on the login, with the app's own reads in a rolled-back transaction, and Cassandra and ScyllaDB rely on the login. A statement is a read only when its engine's grammar shows it reads, each statement of a script on its own. Moving to or from Read-Only reconnects the window | +| Query timeout per connection | Custom Query Timeout in the connect dialog sets the editor's timeout for that connection in place of the one in Preferences, from the next connect; 0 turns it off. The app's own calls keep their fixed limits: 15 seconds for metadata, 60 for a page or a count, 120 for a row save or a CSV import, none for a schema change or an export, and on the server 60 for a table-tab statement and 10 for a lock wait | | Database files | SQLite and DuckDB: choose a file, create a new one, open one from Files or a terminal, where the file's header picks the engine, find a moved one with Locate, and choose read-only or read-write for one on a network or FUSE filesystem | | Kerberos for SQL Server | From the ticket cache, in the Flatpak through the KCM socket | | Health and reconnect | A banner while the connection is lost, reconnect with a backoff from 5 to 60 seconds, and metadata, page and count reads replayed once after it; writes are never replayed | @@ -164,7 +165,6 @@ Not built yet, in no order. ### Connections -- A statement timeout per connection. Today one preference covers editor runs. Other calls have fixed limits: 15 seconds for metadata, 60 for a page or a count, 120 for a row save or a CSV import, and none for a schema change or an export; the server also stops any table-tab statement at 60 seconds and a lock wait at 10. - Kerberos against a service outside the client's realm, which needs an SPN override in tiberius. ### Browse, edit and structure diff --git a/linux/changelog.d/3977.added.md b/linux/changelog.d/3977.added.md new file mode 100644 index 0000000000..f9b9db8548 --- /dev/null +++ b/linux/changelog.d/3977.added.md @@ -0,0 +1 @@ +A connection can set its own query timeout for the SQL editor in the connect dialog, in place of the one in Preferences. diff --git a/linux/crates/app/src/application/tests.rs b/linux/crates/app/src/application/tests.rs index 8a4067ff99..beff4cf3dd 100644 --- a/linux/crates/app/src/application/tests.rs +++ b/linux/crates/app/src/application/tests.rs @@ -995,6 +995,7 @@ mod mcp { safe_mode: SafeModeLevel::Off, external_access: access, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/services/connection_deletion.rs b/linux/crates/app/src/services/connection_deletion.rs index 723ca09658..02dbd65a43 100644 --- a/linux/crates/app/src/services/connection_deletion.rs +++ b/linux/crates/app/src/services/connection_deletion.rs @@ -179,6 +179,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/services/connection_search.rs b/linux/crates/app/src/services/connection_search.rs index 2f27dc1e9f..3344601943 100644 --- a/linux/crates/app/src/services/connection_search.rs +++ b/linux/crates/app/src/services/connection_search.rs @@ -81,6 +81,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/services/file_resolution.rs b/linux/crates/app/src/services/file_resolution.rs index 4a9120ff42..c39b68caba 100644 --- a/linux/crates/app/src/services/file_resolution.rs +++ b/linux/crates/app/src/services/file_resolution.rs @@ -247,6 +247,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/services/saved_connection_editor.rs b/linux/crates/app/src/services/saved_connection_editor.rs index 7ad36e28b4..ea5feaa0da 100644 --- a/linux/crates/app/src/services/saved_connection_editor.rs +++ b/linux/crates/app/src/services/saved_connection_editor.rs @@ -365,6 +365,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: Some(chrono::Utc::now()), diff --git a/linux/crates/app/src/services/saved_connection_list.rs b/linux/crates/app/src/services/saved_connection_list.rs index c9392987f7..3b4f028b5d 100644 --- a/linux/crates/app/src/services/saved_connection_list.rs +++ b/linux/crates/app/src/services/saved_connection_list.rs @@ -228,6 +228,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/services/saved_connection_object.rs b/linux/crates/app/src/services/saved_connection_object.rs index a09bc50afe..c74c579f3c 100644 --- a/linux/crates/app/src/services/saved_connection_object.rs +++ b/linux/crates/app/src/services/saved_connection_object.rs @@ -70,6 +70,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/services/saved_connection_order.rs b/linux/crates/app/src/services/saved_connection_order.rs index d909008777..09400ebd85 100644 --- a/linux/crates/app/src/services/saved_connection_order.rs +++ b/linux/crates/app/src/services/saved_connection_order.rs @@ -94,6 +94,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: opened_days_ago.map(|days| { diff --git a/linux/crates/app/src/services/secret_changes.rs b/linux/crates/app/src/services/secret_changes.rs index f453e5ef35..d9859d94d9 100644 --- a/linux/crates/app/src/services/secret_changes.rs +++ b/linux/crates/app/src/services/secret_changes.rs @@ -60,6 +60,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: Some(SavedSshConfig { host: "bastion".to_owned(), diff --git a/linux/crates/app/src/services/secret_labels.rs b/linux/crates/app/src/services/secret_labels.rs index f40549851c..c92976e513 100644 --- a/linux/crates/app/src/services/secret_labels.rs +++ b/linux/crates/app/src/services/secret_labels.rs @@ -45,6 +45,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/ui/connect_dialog/connect_form.rs b/linux/crates/app/src/ui/connect_dialog/connect_form.rs index 2d42a72826..7e9e4b1069 100644 --- a/linux/crates/app/src/ui/connect_dialog/connect_form.rs +++ b/linux/crates/app/src/ui/connect_dialog/connect_form.rs @@ -131,6 +131,8 @@ pub struct ConnectForm { pub safe_mode: SafeModeLevel, pub external_access: ExternalAccess, pub ai_policy: AiPolicy, + /// `None` follows the timeout in Preferences. + pub query_timeout_secs: Option, pub ssh: Option, pub file: Option, /// The ids of the tags ticked in the Appearance group. @@ -165,6 +167,7 @@ impl ConnectForm { safe_mode: SafeModeLevel::Off, external_access: ExternalAccess::default(), ai_policy: AiPolicy::default(), + query_timeout_secs: None, ssh: None, file: None, tags: Vec::new(), @@ -182,6 +185,7 @@ impl ConnectForm { form.safe_mode = saved.safe_mode; form.external_access = saved.external_access; form.ai_policy = saved.ai_policy; + form.query_timeout_secs = saved.query_timeout_secs; form.tags.clone_from(&saved.tags); form.icon = saved.icon; if caps.is_file_based() { @@ -424,6 +428,7 @@ impl ConnectForm { safe_mode: self.safe_mode, external_access: self.external_access, ai_policy: self.ai_policy, + query_timeout_secs: self.query_timeout_secs, auth_mode, ssh: valid.ssh.as_ref().map(|(saved, _)| saved.clone()), last_opened_at: existing.and_then(|saved| saved.last_opened_at), diff --git a/linux/crates/app/src/ui/connect_dialog/imp.rs b/linux/crates/app/src/ui/connect_dialog/imp.rs index a39a82c509..1d1eecf594 100644 --- a/linux/crates/app/src/ui/connect_dialog/imp.rs +++ b/linux/crates/app/src/ui/connect_dialog/imp.rs @@ -74,6 +74,10 @@ pub struct ConnectDialog { #[template_child] pub ai_policy_names: TemplateChild, #[template_child] + pub query_timeout_row: TemplateChild, + #[template_child] + pub query_timeout_seconds_row: TemplateChild, + #[template_child] pub icon_row: TemplateChild, #[template_child] pub tags_row: TemplateChild, diff --git a/linux/crates/app/src/ui/connect_dialog/mod.rs b/linux/crates/app/src/ui/connect_dialog/mod.rs index 9bc051acc5..4c91921fcb 100644 --- a/linux/crates/app/src/ui/connect_dialog/mod.rs +++ b/linux/crates/app/src/ui/connect_dialog/mod.rs @@ -28,7 +28,7 @@ use secrecy::SecretString; use tablepro_core::credentials::CredentialInteraction; use tablepro_core::{ AiPolicy, AuthMode, ConfigError, DatabaseDriver, DriverCapabilities, DriverRegistry, ExternalAccess, NetworkOption, - NetworkOptions, SafeModeLevel, TlsConfig, TransportClass, WorkspaceSettings, + NetworkOptions, QueryTimeout, SafeModeLevel, TlsConfig, TransportClass, WorkspaceSettings, }; use tablepro_session::runtime::{ProcessHold, Tasks}; use tablepro_session::{ConnectError, ConnectRequest, ConnectionMetadata, Connector, CredentialSource, PendingSave}; @@ -436,6 +436,10 @@ impl ConnectDialog { .get(imp.ai_policy_row.selected() as usize) .copied() .unwrap_or_default(), + query_timeout_secs: imp + .query_timeout_row + .enables_expansion() + .then(|| imp.query_timeout_seconds_row.value() as u32), ssh: (!caps.is_file_based()).then(|| imp.ssh_section.form_state()).flatten(), file: imp.file_row.endpoint(), tags: self.picked_tags(), @@ -512,6 +516,11 @@ impl ConnectDialog { .position(|policy| *policy == form.ai_policy) .unwrap_or_default(); imp.ai_policy_row.set_selected(policy as u32); + imp.query_timeout_row + .set_enable_expansion(form.query_timeout_secs.is_some()); + if let Some(seconds) = form.query_timeout_secs { + imp.query_timeout_seconds_row.set_value(f64::from(seconds)); + } if form.file.is_some() || imp.file_row.endpoint().is_some() { imp.file_row.set_endpoint(form.file.clone()); } @@ -979,7 +988,8 @@ fn connect_request( }, driver: entry.driver.clone(), target: valid.target.clone(), - workspace_settings: WorkspaceSettings::workspace(form.safe_mode.blocks_writes()), + workspace_settings: WorkspaceSettings::workspace(form.safe_mode.blocks_writes()) + .with_editor_timeout(QueryTimeout::from_seconds(form.query_timeout_secs)), safe_mode: form.safe_mode, } } diff --git a/linux/crates/app/src/ui/connect_dialog/tests.rs b/linux/crates/app/src/ui/connect_dialog/tests.rs index c717ed27cc..0429e1ad09 100644 --- a/linux/crates/app/src/ui/connect_dialog/tests.rs +++ b/linux/crates/app/src/ui/connect_dialog/tests.rs @@ -603,6 +603,63 @@ fn an_edit_opens_on_the_saved_ai_policy_and_keeps_it_across_a_driver_change() { assert_eq!(harness.dialog.form().ai_policy, AiPolicy::Allow); } +#[gtk4::test] +fn a_new_connection_follows_the_preference_query_timeout() { + let harness = dialog(); + let imp = harness.dialog.imp(); + + assert!(!imp.query_timeout_row.enables_expansion()); + assert_eq!(harness.dialog.form().query_timeout_secs, None); + + imp.query_timeout_row.set_enable_expansion(true); + imp.query_timeout_seconds_row.set_value(0.0); + + assert_eq!(harness.dialog.form().query_timeout_secs, Some(0)); +} + +#[gtk4::test] +fn an_edit_opens_on_the_saved_query_timeout_and_keeps_it_across_a_driver_change() { + let saved = SavedConnection { + query_timeout_secs: Some(300), + ..shop() + }; + let harness = open(Options { + saved: vec![saved.clone()], + mode: ConnectDialogMode::Edit(Box::new(saved)), + ..Options::default() + }); + let imp = harness.dialog.imp(); + + assert!(imp.query_timeout_row.enables_expansion()); + assert_eq!(imp.query_timeout_seconds_row.value() as u32, 300); + select(&harness.dialog, "mysql"); + assert_eq!(harness.dialog.form().query_timeout_secs, Some(300)); + + imp.query_timeout_row.set_enable_expansion(false); + + assert_eq!(harness.dialog.form().query_timeout_secs, None); +} + +#[gtk4::test] +fn a_test_connect_runs_under_the_connection_query_timeout() { + let harness = open(Options { + connector: FakeConnector::new([FakeEstablish::Connect(ConnectionScript::default())]), + ..Options::default() + }); + fill_network(&harness.dialog); + let imp = harness.dialog.imp(); + imp.query_timeout_row.set_enable_expansion(true); + imp.query_timeout_seconds_row.set_value(90.0); + + imp.test_button.emit_clicked(); + wait_until(WAIT, || harness.connector.calls().len() == 1).expect("the test connect"); + + assert_eq!( + harness.connector.calls()[0].request.workspace_settings.editor_timeout, + tablepro_core::QueryTimeout::After(Duration::from_secs(90)) + ); +} + #[gtk4::test] fn connect_insensitive_while_pem_check_pending() { let harness = dialog(); @@ -940,6 +997,8 @@ fn every_row_has_an_accessible_name() { imp.safe_mode_row.get().upcast(), imp.external_access_row.get().upcast(), imp.ai_policy_row.get().upcast(), + imp.query_timeout_row.get().upcast(), + imp.query_timeout_seconds_row.get().upcast(), tls.encryption_row.get().upcast(), tls.certificates_row.get().upcast(), tls.ca_row.get().upcast(), diff --git a/linux/crates/app/src/ui/editor/mod.rs b/linux/crates/app/src/ui/editor/mod.rs index 457426700f..a84a81826f 100644 --- a/linux/crates/app/src/ui/editor/mod.rs +++ b/linux/crates/app/src/ui/editor/mod.rs @@ -495,7 +495,10 @@ impl SqlEditor { let run = ScriptRun { batches, policy, - statement_timeout: deps.settings.query_timeout(), + statement_timeout: session + .workspace_settings() + .editor_timeout + .resolve(deps.settings.query_timeout()), row_limit: RowLimit::EDITOR_DEFAULT, run_row_limit: RUN_ROW_LIMIT, }; diff --git a/linux/crates/app/src/ui/editor/tests.rs b/linux/crates/app/src/ui/editor/tests.rs index da55fc009e..199fa66742 100644 --- a/linux/crates/app/src/ui/editor/tests.rs +++ b/linux/crates/app/src/ui/editor/tests.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use std::time::Duration; use tablepro_core::column::{ColumnType, ResultColumn}; -use tablepro_core::{DriverError, EditorSession, ResultEvent, TransactionState, Truncation, Value}; +use tablepro_core::{DriverError, EditorSession, QueryTimeout, ResultEvent, TransactionState, Truncation, Value}; use tablepro_session::Session; use tablepro_session::fakes::{FakeConnector, FakeCredentialSource}; use tablepro_session::operation::DefaultDeadlines; @@ -28,10 +28,20 @@ fn session_with(fake: Arc, tasks: Tasks) -> Arc { session_on(fake, tasks, ConnectionScript::default()) } -fn session_on(fake: Arc, tasks: Tasks, mut script: ConnectionScript) -> Arc { +fn session_on(fake: Arc, tasks: Tasks, script: ConnectionScript) -> Arc { + session_timing_out(fake, tasks, script, QueryTimeout::Preference) +} + +fn session_timing_out( + fake: Arc, + tasks: Tasks, + mut script: ConnectionScript, + editor_timeout: QueryTimeout, +) -> Arc { let editor: Arc = fake; script.open_editor_session.always(FakeReply::ok(editor)); - let request = FakeConnector::request().expect("a request"); + let mut request = FakeConnector::request().expect("a request"); + request.workspace_settings = request.workspace_settings.with_editor_timeout(editor_timeout); let (pending, _) = FakeConnector::pending_session(request, script); Session::start( pending, @@ -513,6 +523,26 @@ fn stop_cancels_the_running_statement() { ); } +#[gtk4::test] +fn the_connection_query_timeout_wins_over_the_preference() { + let mut runs = Replies::default(); + runs.always(FakeRun::until_stopped()); + let settings = MemorySettings::new(); + settings.get().set_query_timeout_secs(3600).expect("the preference"); + let harness = build_editor_on(settings, runs, "SELECT pg_sleep(60)", |_| None); + harness.active.replace(session_timing_out( + harness.fake.clone(), + harness.runtime.tasks(), + ConnectionScript::default(), + QueryTimeout::After(Duration::from_millis(200)), + )); + + harness.editor.run(); + + wait_until(WAIT, || !harness.editor.is_running()).expect("the connection's timeout to end the run"); + assert_eq!(harness.editor.imp().results.n_entries(), 1); +} + #[gtk4::test] fn replace_query_can_be_undone() { let harness = editor_with(answering(), "SELECT 1"); diff --git a/linux/crates/app/src/ui/history_dialog/connection_filter.rs b/linux/crates/app/src/ui/history_dialog/connection_filter.rs index 6988b375e3..d45b52d1b7 100644 --- a/linux/crates/app/src/ui/history_dialog/connection_filter.rs +++ b/linux/crates/app/src/ui/history_dialog/connection_filter.rs @@ -95,6 +95,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/ui/history_dialog/tests.rs b/linux/crates/app/src/ui/history_dialog/tests.rs index fa310d94ac..1b3b6ddbfe 100644 --- a/linux/crates/app/src/ui/history_dialog/tests.rs +++ b/linux/crates/app/src/ui/history_dialog/tests.rs @@ -105,6 +105,7 @@ impl Harness { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/ui/picker/connection_row.rs b/linux/crates/app/src/ui/picker/connection_row.rs index b765de4bdd..1c3ffae01e 100644 --- a/linux/crates/app/src/ui/picker/connection_row.rs +++ b/linux/crates/app/src/ui/picker/connection_row.rs @@ -208,6 +208,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/ui/picker/tests.rs b/linux/crates/app/src/ui/picker/tests.rs index d3548466e2..8b94936c62 100644 --- a/linux/crates/app/src/ui/picker/tests.rs +++ b/linux/crates/app/src/ui/picker/tests.rs @@ -32,6 +32,7 @@ fn connection(name: &str, group: Option<&str>) -> SavedConnection { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/ui/session_window/connect_failure_view.rs b/linux/crates/app/src/ui/session_window/connect_failure_view.rs index d73512bd8e..f0a9482afd 100644 --- a/linux/crates/app/src/ui/session_window/connect_failure_view.rs +++ b/linux/crates/app/src/ui/session_window/connect_failure_view.rs @@ -59,6 +59,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/ui/session_window/connect_flow.rs b/linux/crates/app/src/ui/session_window/connect_flow.rs index f2c7e1ed63..05746c6a99 100644 --- a/linux/crates/app/src/ui/session_window/connect_flow.rs +++ b/linux/crates/app/src/ui/session_window/connect_flow.rs @@ -182,6 +182,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/ui/session_window/secret_retry.rs b/linux/crates/app/src/ui/session_window/secret_retry.rs index 8675bf5b6c..ad1e72aeba 100644 --- a/linux/crates/app/src/ui/session_window/secret_retry.rs +++ b/linux/crates/app/src/ui/session_window/secret_retry.rs @@ -91,6 +91,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode, ssh: ssh.map(|auth| SavedSshConfig { host: "bastion.example.com".to_owned(), diff --git a/linux/crates/app/src/ui/session_window/tests/mod.rs b/linux/crates/app/src/ui/session_window/tests/mod.rs index c57c46380f..062db1ffbc 100644 --- a/linux/crates/app/src/ui/session_window/tests/mod.rs +++ b/linux/crates/app/src/ui/session_window/tests/mod.rs @@ -342,6 +342,7 @@ pub(crate) fn saved_connection(database: &std::path::Path) -> SavedConnection { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, @@ -367,6 +368,7 @@ fn network_connection() -> SavedConnection { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/ui/tags/manage_tags_dialog.rs b/linux/crates/app/src/ui/tags/manage_tags_dialog.rs index d61fb7ae4d..dcbb2f8420 100644 --- a/linux/crates/app/src/ui/tags/manage_tags_dialog.rs +++ b/linux/crates/app/src/ui/tags/manage_tags_dialog.rs @@ -274,6 +274,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/src/ui/workspace/sidebar/tests.rs b/linux/crates/app/src/ui/workspace/sidebar/tests.rs index e710518396..3d9e3c5abc 100644 --- a/linux/crates/app/src/ui/workspace/sidebar/tests.rs +++ b/linux/crates/app/src/ui/workspace/sidebar/tests.rs @@ -122,6 +122,7 @@ fn connection(name: &str, group: Option<&str>) -> SavedConnection { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/tests/accessibility/world.rs b/linux/crates/app/tests/accessibility/world.rs index 3fe81dc81e..1c5e936fe2 100644 --- a/linux/crates/app/tests/accessibility/world.rs +++ b/linux/crates/app/tests/accessibility/world.rs @@ -137,6 +137,7 @@ fn saved_connection(database: &Path) -> SavedConnection { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/tests/application_lifecycle.rs b/linux/crates/app/tests/application_lifecycle.rs index c0f5e47ee3..b4e714fbb5 100644 --- a/linux/crates/app/tests/application_lifecycle.rs +++ b/linux/crates/app/tests/application_lifecycle.rs @@ -472,6 +472,7 @@ fn saved_connection(database: &Path) -> SavedConnection { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/app/tests/secret_save_flow.rs b/linux/crates/app/tests/secret_save_flow.rs index c1665acb16..0d391dcd33 100644 --- a/linux/crates/app/tests/secret_save_flow.rs +++ b/linux/crates/app/tests/secret_save_flow.rs @@ -48,6 +48,7 @@ fn connection(id: Uuid, ssh: Option) -> SavedConnection { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh, last_opened_at: None, diff --git a/linux/crates/core/src/lib.rs b/linux/crates/core/src/lib.rs index fe29020dec..60a9759eb5 100644 --- a/linux/crates/core/src/lib.rs +++ b/linux/crates/core/src/lib.rs @@ -47,6 +47,7 @@ mod network_options; mod network_target; mod owned_calls; pub mod query_result; +mod query_timeout; mod read_only_refusal; mod read_statement; mod registry; @@ -138,6 +139,7 @@ pub use network_options::{NetworkOption, NetworkOptions}; pub use network_target::NetworkTarget; pub use owned_calls::OwnedCalls; pub use query_result::QueryResult; +pub use query_timeout::QueryTimeout; pub use read_only_refusal::ReadOnlyRefusal; pub use read_statement::ReadStatement; pub use registry::DriverRegistry; diff --git a/linux/crates/core/src/query_timeout.rs b/linux/crates/core/src/query_timeout.rs new file mode 100644 index 0000000000..fe6e3a4c2e --- /dev/null +++ b/linux/crates/core/src/query_timeout.rs @@ -0,0 +1,60 @@ +use std::time::Duration; + +/// How long the SQL editor lets a statement run on one connection. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub enum QueryTimeout { + /// The query timeout in Preferences, which every connection starts on. + #[default] + Preference, + Off, + After(Duration), +} + +impl QueryTimeout { + /// The seconds a connection saves: none follows Preferences, and + /// zero turns the limit off, as zero does there. + pub fn from_seconds(seconds: Option) -> Self { + match seconds { + None => Self::Preference, + Some(0) => Self::Off, + Some(seconds) => Self::After(Duration::from_secs(u64::from(seconds))), + } + } + + /// The limit a run gets, given the one Preferences sets. + pub fn resolve(self, preference: Option) -> Option { + match self { + Self::Preference => preference, + Self::Off => None, + Self::After(limit) => Some(limit), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const PREFERENCE: Option = Some(Duration::from_secs(60)); + + #[test] + fn a_connection_without_its_own_timeout_follows_preferences() { + assert_eq!(QueryTimeout::from_seconds(None), QueryTimeout::Preference); + assert_eq!(QueryTimeout::Preference.resolve(PREFERENCE), PREFERENCE); + assert_eq!(QueryTimeout::Preference.resolve(None), None); + } + + #[test] + fn a_connection_timeout_wins_over_preferences_and_zero_turns_it_off() { + assert_eq!( + QueryTimeout::from_seconds(Some(300)).resolve(PREFERENCE), + Some(Duration::from_secs(300)) + ); + assert_eq!( + QueryTimeout::from_seconds(Some(5)).resolve(None), + Some(Duration::from_secs(5)) + ); + assert_eq!(QueryTimeout::from_seconds(Some(0)), QueryTimeout::Off); + assert_eq!(QueryTimeout::Off.resolve(PREFERENCE), None); + } +} diff --git a/linux/crates/core/src/workspace_settings.rs b/linux/crates/core/src/workspace_settings.rs index efc5693f76..671068da97 100644 --- a/linux/crates/core/src/workspace_settings.rs +++ b/linux/crates/core/src/workspace_settings.rs @@ -1,5 +1,7 @@ use std::time::Duration; +use crate::QueryTimeout; + /// What every workspace call on a connection runs under. /// /// The limits are the server's, set on the session or per call, so a @@ -13,6 +15,9 @@ pub struct WorkspaceSettings { /// shorter than the statement timeout so a blocked edit reports the /// lock rather than a generic timeout. pub lock_timeout: Duration, + /// The limit on the connection's editor runs, which the server + /// enforces too where the engine can. + pub editor_timeout: QueryTimeout, } impl WorkspaceSettings { @@ -24,8 +29,13 @@ impl WorkspaceSettings { read_only, statement_timeout: Self::STATEMENT_TIMEOUT, lock_timeout: Self::LOCK_TIMEOUT, + editor_timeout: QueryTimeout::Preference, } } + + pub fn with_editor_timeout(self, editor_timeout: QueryTimeout) -> Self { + Self { editor_timeout, ..self } + } } #[cfg(test)] @@ -39,6 +49,7 @@ mod tests { assert!(settings.read_only); assert_eq!(settings.statement_timeout, Duration::from_secs(60)); assert_eq!(settings.lock_timeout, Duration::from_secs(10)); + assert_eq!(settings.editor_timeout, QueryTimeout::Preference); assert!(!WorkspaceSettings::workspace(false).read_only); } } diff --git a/linux/crates/mcp/tests/mcp_server.rs b/linux/crates/mcp/tests/mcp_server.rs index 8f367c7068..ab5f18c13b 100644 --- a/linux/crates/mcp/tests/mcp_server.rs +++ b/linux/crates/mcp/tests/mcp_server.rs @@ -111,6 +111,7 @@ fn saved(id: Uuid, name: &str, file: &Path, access: ExternalAccess) -> SavedConn safe_mode: SafeModeLevel::Off, external_access: access, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/session/src/connect/persist_pending_save.rs b/linux/crates/session/src/connect/persist_pending_save.rs index 5bcda0dc69..a432347faa 100644 --- a/linux/crates/session/src/connect/persist_pending_save.rs +++ b/linux/crates/session/src/connect/persist_pending_save.rs @@ -142,6 +142,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: Some(SavedSshConfig { host: "bastion.example.com".into(), diff --git a/linux/crates/session/src/connect/resolve_saved.rs b/linux/crates/session/src/connect/resolve_saved.rs index f06a8ec49f..2f04aac1df 100644 --- a/linux/crates/session/src/connect/resolve_saved.rs +++ b/linux/crates/session/src/connect/resolve_saved.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use secrecy::SecretString; use tablepro_core::credentials::CredentialInteraction; use tablepro_core::{ - AuthMode, Credentials, DriverError, DriverRegistry, FileEndpoint, NetworkEndpoint, WorkspaceSettings, + AuthMode, Credentials, DriverError, DriverRegistry, FileEndpoint, NetworkEndpoint, QueryTimeout, WorkspaceSettings, }; use tablepro_ssh::{SshAuth, SshConfig, SshDestination}; use tablepro_storage::{CredentialDemand, SavedConnection, SavedSshAuth, SavedSshConfig}; @@ -66,7 +66,8 @@ fn saved_request(saved: &SavedConnection, drivers: &DriverRegistry) -> Result| async move { + let mut saved = saved(None); + saved.query_timeout_secs = seconds; + let vault = Arc::new(FakeSecretVault::new()); + vault + .store( + saved.id, + SecretKind::DatabasePassword, + &SecretString::from("db"), + "label", + ) + .await + .unwrap(); + let credentials = KeyringCredentials::for_saved(vault, &saved); + resolve_saved( + &saved, + &drivers(), + &credentials, + CredentialInteraction::Unattended, + CredentialDemand::default(), + ) + .await + .unwrap() + .request + .workspace_settings + .editor_timeout + }; + + assert_eq!(resolve(None).await, QueryTimeout::Preference); + assert_eq!(resolve(Some(0)).await, QueryTimeout::Off); + assert_eq!( + resolve(Some(300)).await, + QueryTimeout::After(std::time::Duration::from_secs(300)) + ); + } + #[tokio::test] async fn an_unknown_driver_and_a_bad_host_are_reported() { let mut unknown = saved(None); diff --git a/linux/crates/session/src/registry/session_key.rs b/linux/crates/session/src/registry/session_key.rs index 0576d05b70..613d312ee2 100644 --- a/linux/crates/session/src/registry/session_key.rs +++ b/linux/crates/session/src/registry/session_key.rs @@ -80,6 +80,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/storage/src/connections.rs b/linux/crates/storage/src/connections.rs index f24cc70911..47c0617583 100644 --- a/linux/crates/storage/src/connections.rs +++ b/linux/crates/storage/src/connections.rs @@ -42,6 +42,11 @@ pub struct SavedConnection { /// starts at. #[serde(default)] pub ai_policy: AiPolicy, + /// The editor's query timeout here, in seconds, zero for none. A file + /// written before it existed, like a connection that never set one, + /// leaves it out and follows the one in Preferences. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub query_timeout_secs: Option, #[serde(default)] pub auth_mode: AuthMode, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -876,6 +881,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: Some(Utc::now()), @@ -904,6 +910,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/storage/src/credentials/keyring_credentials.rs b/linux/crates/storage/src/credentials/keyring_credentials.rs index c0d64609c4..7b0d355a60 100644 --- a/linux/crates/storage/src/credentials/keyring_credentials.rs +++ b/linux/crates/storage/src/credentials/keyring_credentials.rs @@ -317,6 +317,7 @@ mod tests { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: ssh.map(|auth| SavedSshConfig { host: "bastion.example.com".to_owned(), diff --git a/linux/crates/storage/tests/isolation.rs b/linux/crates/storage/tests/isolation.rs index 5d5d347298..e125e96c50 100644 --- a/linux/crates/storage/tests/isolation.rs +++ b/linux/crates/storage/tests/isolation.rs @@ -48,6 +48,7 @@ fn connection(name: &str) -> SavedConnection { safe_mode: tablepro_core::SafeModeLevel::Off, external_access: tablepro_core::ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: tablepro_core::AuthMode::Password, ssh: None, last_opened_at: None, diff --git a/linux/crates/storage/tests/stored_formats.rs b/linux/crates/storage/tests/stored_formats.rs index b1fa22cb75..70d6ea87f3 100644 --- a/linux/crates/storage/tests/stored_formats.rs +++ b/linux/crates/storage/tests/stored_formats.rs @@ -59,6 +59,7 @@ fn saved(id: &str, name: &str, driver_id: &str, host: &str, port: u16) -> Result safe_mode: SafeModeLevel::Off, external_access: ExternalAccess::ReadOnly, ai_policy: tablepro_core::AiPolicy::Ask, + query_timeout_secs: None, auth_mode: AuthMode::Password, ssh: None, last_opened_at: None, @@ -367,6 +368,54 @@ fn an_ai_policy_writes_back_as_it_was_set() -> TestResult { Ok(()) } +#[test] +fn a_list_written_before_the_query_timeout_follows_preferences_and_writes_back_without_it() -> TestResult { + let root = tempfile::tempdir()?; + let paths = StoragePaths::under(root.path(), "tablepro", "app.tablepro.TablePro"); + let store = ConnectionStore::new(&paths); + store.import_blocking(Path::new("/tmp/connections-v2.json"), CONNECTIONS_V2)?; + + let exported = store.export_blocking()?; + + assert!(!String::from_utf8_lossy(CONNECTIONS_V2).contains("query_timeout_secs")); + assert!( + store + .load_blocking()? + .iter() + .all(|connection| connection.query_timeout_secs.is_none()) + ); + assert!(!String::from_utf8_lossy(&exported).contains("query_timeout_secs")); + Ok(()) +} + +#[test] +fn a_query_timeout_writes_back_as_it_was_set() -> TestResult { + let root = tempfile::tempdir()?; + let paths = StoragePaths::under(root.path(), "tablepro", "app.tablepro.TablePro"); + let store = ConnectionStore::new(&paths); + store.import_blocking(Path::new("/tmp/connections-v2.json"), CONNECTIONS_V2)?; + let ids: Vec<_> = store.load_blocking()?.iter().map(|connection| connection.id).collect(); + let [first, second, ..] = ids.as_slice() else { + return Err("the fixture has fewer than two connections".into()); + }; + store.update_blocking(*first, |connection| connection.query_timeout_secs = Some(300))?; + store.update_blocking(*second, |connection| connection.query_timeout_secs = Some(0))?; + + let exported = store.export_blocking()?; + let again = tempfile::tempdir()?; + let read = connections_from(again.path(), &exported)?; + let seconds = |id| { + read.iter() + .find(|connection| connection.id == id) + .and_then(|connection| connection.query_timeout_secs) + }; + + assert!(String::from_utf8_lossy(&exported).contains("\"query_timeout_secs\": 300")); + assert_eq!(seconds(*first), Some(300)); + assert_eq!(seconds(*second), Some(0)); + Ok(()) +} + #[test] fn a_version_1_connection_list_imports_as_it_was_written() -> TestResult { let root = tempfile::tempdir()?; diff --git a/linux/data/resources/ui/connect-dialog.ui b/linux/data/resources/ui/connect-dialog.ui index 3b28dfbe20..f0e2599039 100644 --- a/linux/data/resources/ui/connect-dialog.ui +++ b/linux/data/resources/ui/connect-dialog.ui @@ -160,6 +160,30 @@ + + + + Custom Query Timeout + Use a timeout for editor queries on this connection instead of the one in Preferences + True + False + + + Query Timeout (seconds) + Set to 0 to disable + + + 0 + 3600 + 60 + 5 + 60 + + + + + +
diff --git a/linux/docs/storage.md b/linux/docs/storage.md index d83da66845..4937045c0f 100644 --- a/linux/docs/storage.md +++ b/linux/docs/storage.md @@ -25,6 +25,7 @@ The app's data goes to the store its shape calls for. The `storage` crate owns t - `safe_mode` is the connection's Safe Mode level: `off`, `confirm_writes` or `read_only`. It has no default, since guessing Off would drop a guard. Version 1 of the file had a `read_only` switch instead, which reads as `read_only` when on and `off` when off. - `external_access` is what MCP clients may do on the connection: `blocked`, `read_only` or `read_write` ([ADR 0022](decisions/0022-ai-assistant-and-mcp-server.md)). A file written before it existed reads as `read_only`, the level a new connection starts at. - `ai_policy` is what the AI assistant may send from the connection: `ask`, `allow` or `never`. A file written before it existed reads as `ask`, the policy a new connection starts at. +- `query_timeout_secs` is the editor's query timeout on the connection, in seconds, with 0 for none. Left out, as in a file written before it existed, the connection follows the timeout in Preferences, and it is written back without one. - `group` is a `ConnectionGroupName`: trimmed, 1 to 64 characters, no control characters. Earlier builds stored any name, so a stored one is read with its control characters turned into spaces and cut to 64 characters, and a blank one reads as no group. - The file's `tags` list holds every `ConnectionTag`: an `id`, a `name` (`ConnectionTagName`: trimmed, 1 to 32 characters, no control characters, cleaned on read as a group's is) and a `color` from the same palette as `color`. No two tags share a name in any case: `create_tag_blocking` and `update_tag_blocking` refuse one with `TagNameTaken`. - A connection's `tags` are ids from that list. Every write drops an id the list lacks and a repeated one, so `delete_tag_blocking` takes the tag off every connection in the same write, and a connection saved with a tag deleted meanwhile loses it. From 8de8cb98434812b19a626f904882007873719f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sun, 1 Nov 2026 16:49:45 +0700 Subject: [PATCH 79/85] fix(backup): say what each engine's restore keeps in its confirmation (#3984) --- .../Core/Database/NativeDumpDescriptor.swift | 1 + .../Core/Database/NativeDumpRegistry.swift | 2 +- .../TablePro/Resources/Localizable.xcstrings | 32 ++++ .../Backup/RestoreConfirmationText.swift | 18 ++- .../Backup/RestoreConfirmationTextTests.swift | 30 +++- apple/changelog.d/3984.fixed.md | 1 + .../probes/check-restore-error-semantics.sh | 146 ++++++++++++++++++ 7 files changed, 220 insertions(+), 10 deletions(-) create mode 100644 apple/changelog.d/3984.fixed.md create mode 100755 apple/scripts/probes/check-restore-error-semantics.sh diff --git a/apple/TablePro/Core/Database/NativeDumpDescriptor.swift b/apple/TablePro/Core/Database/NativeDumpDescriptor.swift index 1e2e64a069..5225cbd7ec 100644 --- a/apple/TablePro/Core/Database/NativeDumpDescriptor.swift +++ b/apple/TablePro/Core/Database/NativeDumpDescriptor.swift @@ -256,6 +256,7 @@ struct NativeDumpDescriptor: Sendable { internal enum RestoreSemantics: Sendable, Equatable { case replacesObjects case addsToExistingObjects + case stopsAtFirstError case stopsAtExistingObjects case requiresEmptyDatabase } diff --git a/apple/TablePro/Core/Database/NativeDumpRegistry.swift b/apple/TablePro/Core/Database/NativeDumpRegistry.swift index a6ed78ff4d..b21d734037 100644 --- a/apple/TablePro/Core/Database/NativeDumpRegistry.swift +++ b/apple/TablePro/Core/Database/NativeDumpRegistry.swift @@ -534,7 +534,7 @@ enum NativeDumpRegistry { contentDescription: String(localized: "SQL statements") ), objectScope: .tables(caveat: nil), - restoreSemantics: .addsToExistingObjects, + restoreSemantics: .stopsAtFirstError, requiresLocalFile: true ) } diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index b51e6f29db..608d5cb7fb 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -186267,7 +186267,38 @@ } } }, + "The dump is replayed into this database, and the change cannot be undone. A backup TablePro wrote overwrites each object it names. The restore stops at the first error and keeps what ran before it." : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Le dump est rejoué dans cette base de données, et la modification est irréversible. Une sauvegarde écrite par TablePro écrase chaque objet qu’elle nomme. La restauration s’arrête à la première erreur et conserve ce qui s’est exécuté avant." + } + } + } + }, + "The dump is replayed into this database, and the change cannot be undone. Objects that already exist are kept, not replaced, and the dump's data is added to them. The restore carries on past errors, such as a key that already exists." : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Le dump est rejoué dans cette base de données, et la modification est irréversible. Les objets existants sont conservés, et non remplacés, et les données du dump y sont ajoutées. La restauration se poursuit malgré les erreurs, comme une clé qui existe déjà." + } + } + } + }, + "The dump is replayed into this database, and the change cannot be undone. The restore stops at the first error, such as a key or object that already exists. If the dump is one transaction, as every backup TablePro writes is, none of it is kept. Otherwise what ran before the error stays." : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Le dump est rejoué dans cette base de données, et la modification est irréversible. La restauration s’arrête à la première erreur, comme une clé ou un objet qui existe déjà. Si le dump tient en une seule transaction, comme toute sauvegarde écrite par TablePro, rien n’en est conservé. Sinon, ce qui s’est exécuté avant l’erreur reste en place." + } + } + } + }, "The dump is replayed into this database. Objects it names are overwritten and the change cannot be undone." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { @@ -186278,6 +186309,7 @@ } }, "The dump is replayed into this database. Objects that already exist are kept, not replaced, and the dump's data is added to them. The change cannot be undone." : { + "extractionState" : "stale", "localizations" : { "fr" : { "stringUnit" : { diff --git a/apple/TablePro/Views/Backup/RestoreConfirmationText.swift b/apple/TablePro/Views/Backup/RestoreConfirmationText.swift index 34798d4436..1c6b1668e5 100644 --- a/apple/TablePro/Views/Backup/RestoreConfirmationText.swift +++ b/apple/TablePro/Views/Backup/RestoreConfirmationText.swift @@ -16,14 +16,24 @@ internal enum RestoreConfirmationText { case .replacesObjects: return String( localized: """ - The dump is replayed into this database. Objects it names are overwritten and \ - the change cannot be undone. + The dump is replayed into this database, and the change cannot be undone. A backup \ + TablePro wrote overwrites each object it names. The restore stops at the first \ + error and keeps what ran before it. """) case .addsToExistingObjects: return String( localized: """ - The dump is replayed into this database. Objects that already exist are kept, \ - not replaced, and the dump's data is added to them. The change cannot be undone. + The dump is replayed into this database, and the change cannot be undone. Objects \ + that already exist are kept, not replaced, and the dump's data is added to them. \ + The restore carries on past errors, such as a key that already exists. + """) + case .stopsAtFirstError: + return String( + localized: """ + The dump is replayed into this database, and the change cannot be undone. The \ + restore stops at the first error, such as a key or object that already exists. If \ + the dump is one transaction, as every backup TablePro writes is, none of it is kept. \ + Otherwise what ran before the error stays. """) case .stopsAtExistingObjects: return String( diff --git a/apple/TableProTests/Views/Backup/RestoreConfirmationTextTests.swift b/apple/TableProTests/Views/Backup/RestoreConfirmationTextTests.swift index 71b577ef44..5c4c72056d 100644 --- a/apple/TableProTests/Views/Backup/RestoreConfirmationTextTests.swift +++ b/apple/TableProTests/Views/Backup/RestoreConfirmationTextTests.swift @@ -14,19 +14,39 @@ struct RestoreConfirmationTextTests { func onlyMySQLOverwrites() throws { for type in [DatabaseType.mysql, .mariadb] { let message = try #require(RestoreConfirmationText.message(for: type)) - #expect(message.contains("overwritten"), "\(type.rawValue)") + #expect(message.contains("overwrites each object"), "\(type.rawValue)") } for type in [DatabaseType.postgresql, .redshift, .mongodb, .sqlite, .libsql, .mssql, .duckdb] { let message = try #require(RestoreConfirmationText.message(for: type)) - #expect(!message.contains("overwritten"), "\(type.rawValue)") + #expect(!message.contains("overwrit"), "\(type.rawValue)") } } - @Test("pg_restore, mongorestore and sqlite3 say existing objects are kept and added to") + @Test("mysql runs without --force, so MySQL and MariaDB say the restore stops at the first error and keeps what ran") + func mySQLStopsAtTheFirstError() throws { + for type in [DatabaseType.mysql, .mariadb] { + let message = try #require(RestoreConfirmationText.message(for: type)) + #expect(message.contains("stops at the first error and keeps what ran before it"), "\(type.rawValue)") + } + } + + @Test("pg_restore and mongorestore say existing objects are kept, added to, and errors do not stop the run") func appendingEnginesKeepExistingObjects() throws { - for type in [DatabaseType.postgresql, .redshift, .mongodb, .sqlite, .libsql] { + for type in [DatabaseType.postgresql, .redshift, .mongodb] { + let message = try #require(RestoreConfirmationText.message(for: type)) + #expect(message.contains("kept, not replaced"), "\(type.rawValue)") + #expect(message.contains("carries on past errors"), "\(type.rawValue)") + } + } + + @Test("sqlite3 runs with -bail, so SQLite and libSQL say the restore stops at the first error and keeps no transaction") + func sqliteStopsAtTheFirstError() throws { + for type in [DatabaseType.sqlite, .libsql] { let message = try #require(RestoreConfirmationText.message(for: type)) - #expect(message.contains("kept"), "\(type.rawValue)") + #expect(message.contains("stops at the first error"), "\(type.rawValue)") + #expect(message.contains("none of it is kept"), "\(type.rawValue)") + #expect(message.contains("what ran before the error stays"), "\(type.rawValue)") + #expect(!message.contains("added to them"), "\(type.rawValue)") } } diff --git a/apple/changelog.d/3984.fixed.md b/apple/changelog.d/3984.fixed.md new file mode 100644 index 0000000000..d08fd763f9 --- /dev/null +++ b/apple/changelog.d/3984.fixed.md @@ -0,0 +1 @@ +The SQLite restore confirmation said existing objects get the dump's data, though the restore stops at the first error. diff --git a/apple/scripts/probes/check-restore-error-semantics.sh b/apple/scripts/probes/check-restore-error-semantics.sh new file mode 100755 index 0000000000..2676ea8745 --- /dev/null +++ b/apple/scripts/probes/check-restore-error-semantics.sh @@ -0,0 +1,146 @@ +#!/usr/bin/env bash +# +# Check what a restore keeps when the dump meets objects that already exist or an error, with the +# flags NativeDumpRegistry passes. RestoreConfirmationText states these facts for each engine. +# +# - sqlite3 -safe -bail: stops at the first error. A `.dump` is one transaction, so nothing of it is +# kept; a dump without a transaction keeps what ran before the error. `.dump` writes +# CREATE TABLE IF NOT EXISTS for a table whose name is quoted, so such a table that already +# exists gets the dump's rows added when no key collides. +# - mysql or mariadb without --force: a dump from the matching dump tool drops and recreates each +# table it names; the client stops at the first error and keeps what ran before it. +# - pg_restore without --exit-on-error: existing tables are kept and get the dump's rows unless a +# key collides, and the run carries on past errors and exits 1. +# +# Usage: +# apple/scripts/probes/check-restore-error-semantics.sh +# MYSQL_PROBE_ARGS="--protocol=TCP -h 127.0.0.1 -P 3306 -u root" PGHOST=127.0.0.1 PGPORT=5432 PGUSER=postgres \ +# apple/scripts/probes/check-restore-error-semantics.sh +# +# sqlite3 needs no server. The mysql family runs when the client on PATH can connect with +# MYSQL_PROBE_ARGS (default: none, the local socket) and create a scratch database, PostgreSQL when +# psql can create one with the libpq environment. Each skips otherwise. Exits 1 when a fact differs. + +set -uo pipefail + +WORK="$(mktemp -d "${TMPDIR:-/tmp}/tablepro-restore-errors.XXXXXX")" +trap 'rm -rf "$WORK"' EXIT +failures=0 + +expect() { + if [ "$2" = "$3" ]; then + echo " ok $1" + else + echo "FAIL: $1: expected '$3', got '$2'" >&2 + failures=$((failures + 1)) + fi +} + +SQLITE="$(command -v sqlite3 || true)" +if [ -n "$SQLITE" ]; then + echo "checking $SQLITE ($("$SQLITE" --version | cut -d' ' -f1))" + sq() { "$SQLITE" "$@" 2>/dev/null; } + restore() { "$SQLITE" -safe -bail "$1" < "$2" > /dev/null 2>&1; echo $?; } + tables() { sq "$1" "SELECT group_concat(name, ',') FROM (SELECT name FROM sqlite_schema WHERE type = 'table' ORDER BY name)"; } + + sq "$WORK/src.db" "CREATE TABLE plain(id INTEGER PRIMARY KEY); INSERT INTO plain VALUES (1); + CREATE TABLE \"quoted\"(id INTEGER PRIMARY KEY); INSERT INTO quoted VALUES (1); + CREATE TABLE \"keyless\"(v TEXT); INSERT INTO keyless VALUES ('dump'); + CREATE TABLE later(v INT); INSERT INTO later VALUES (1);" + "$SQLITE" -safe "$WORK/src.db" .dump > "$WORK/dump.sql" + expect ".dump opens one transaction" "$(grep -c '^BEGIN TRANSACTION;$' "$WORK/dump.sql")" "1" + + sq "$WORK/existing.db" "CREATE TABLE plain(id INTEGER PRIMARY KEY); INSERT INTO plain VALUES (9);" + expect "an existing table stops the restore" "$(restore "$WORK/existing.db" "$WORK/dump.sql")" "1" + expect "and keeps none of the dump" "$(tables "$WORK/existing.db"),$(sq "$WORK/existing.db" 'SELECT id FROM plain')" "plain,9" + + sq "$WORK/collision.db" "CREATE TABLE \"quoted\"(id INTEGER PRIMARY KEY); INSERT INTO quoted VALUES (1);" + expect "a key collision stops the restore" "$(restore "$WORK/collision.db" "$WORK/dump.sql")" "1" + expect "and keeps none of the dump" "$(tables "$WORK/collision.db")" "quoted" + + sq "$WORK/keyless.db" "CREATE TABLE \"keyless\"(v TEXT); INSERT INTO keyless VALUES ('own');" + expect "an existing quoted table without a key takes the dump" "$(restore "$WORK/keyless.db" "$WORK/dump.sql")" "0" + expect "and keeps its own rows beside the dump's" "$(sq "$WORK/keyless.db" "SELECT group_concat(v) FROM keyless")" "own,dump" + + printf 'CREATE TABLE first(v INT);\nINSERT INTO first VALUES (1);\nCREATE TABLE plain(id INT);\nCREATE TABLE second(v INT);\n' \ + > "$WORK/foreign.sql" + sq "$WORK/foreign.db" "CREATE TABLE plain(id INTEGER PRIMARY KEY);" + expect "a dump without a transaction stops at the error" "$(restore "$WORK/foreign.db" "$WORK/foreign.sql")" "1" + expect "and keeps what ran before it" "$(tables "$WORK/foreign.db")" "first,plain" +else + echo "no sqlite3 on PATH; skipping" +fi + +MYSQL="$(command -v mysql || command -v mariadb || true)" +DUMP="$(command -v mysqldump || command -v mariadb-dump || true)" +read -r -a PROBE_ARGS <<< "${MYSQL_PROBE_ARGS:-}" +DB="tablepro_restore_errors_$$" +my() { "$MYSQL" ${PROBE_ARGS[@]+"${PROBE_ARGS[@]}"} "$@" 2>/dev/null; } +if [ -n "$MYSQL" ] && [ -n "$DUMP" ] && my -e "CREATE DATABASE ${DB}_src; CREATE DATABASE $DB" > /dev/null; then + banner="$("$MYSQL" --version)" + case "$banner" in + *MariaDB* | *mariadb*) switch="--sandbox" ;; + *) switch="--skip-system-command" ;; + esac + echo "checking $MYSQL and $DUMP ($banner)" + restore() { + "$MYSQL" ${PROBE_ARGS[@]+"${PROBE_ARGS[@]}"} --binary-mode --local-infile=0 "$switch" \ + --default-character-set=utf8mb4 -- "$DB" < "$1" > /dev/null 2>&1 + echo $? + } + rows() { my -N "$DB" -e "SELECT IFNULL(group_concat(id ORDER BY id), '') FROM $1" || echo "missing"; } + reset() { my -e "DROP DATABASE $DB; CREATE DATABASE $DB" > /dev/null; my "$DB" -e "$1" > /dev/null; } + + my "${DB}_src" -e "CREATE TABLE a(id INT PRIMARY KEY); INSERT INTO a VALUES (1), (2); + CREATE TABLE b(id INT PRIMARY KEY); INSERT INTO b VALUES (1);" > /dev/null + "$DUMP" ${PROBE_ARGS[@]+"${PROBE_ARGS[@]}"} --single-transaction --routines --triggers --events \ + --default-character-set=utf8mb4 -- "${DB}_src" > "$WORK/my.sql" 2>/dev/null + + reset "CREATE TABLE a(id INT PRIMARY KEY, extra INT); INSERT INTO a VALUES (9, 9); CREATE TABLE c(id INT);" + expect "a dump over existing tables restores" "$(restore "$WORK/my.sql")" "0" + expect "and overwrites the table it names" "$(rows a)" "1,2" + expect "and leaves a table it does not name" "$(rows c)" "" + + # A two-value row in b's INSERT fails with 1136 after a was replaced; MariaDB breaks the line after VALUES. + sed 's/^INSERT INTO `b` VALUES/INSERT INTO `b` VALUES (1, 2),/' "$WORK/my.sql" > "$WORK/my-broken.sql" + reset "CREATE TABLE a(id INT PRIMARY KEY); INSERT INTO a VALUES (9);" + expect "an error stops the restore" "$(restore "$WORK/my-broken.sql")" "1" + expect "and keeps the table replaced before it" "$(rows a)" "1,2" + + printf 'CREATE TABLE first(id INT);\nINSERT INTO first VALUES (1);\nCREATE TABLE b(id INT);\nCREATE TABLE second(id INT);\n' \ + > "$WORK/my-foreign.sql" + reset "CREATE TABLE b(id INT PRIMARY KEY);" + expect "a dump without DROP stops at an existing table" "$(restore "$WORK/my-foreign.sql")" "1" + expect "and keeps what ran before it" "$(rows first),$(rows second)" "1,missing" + + my -e "DROP DATABASE $DB; DROP DATABASE ${DB}_src" > /dev/null +else + echo "no mysql-family client and dump tool that can create a scratch database; skipping" +fi + +PSQL="$(command -v psql || true)" +if [ -n "$PSQL" ] && command -v pg_dump > /dev/null && command -v pg_restore > /dev/null \ + && "$PSQL" -qX -d postgres -c "CREATE DATABASE ${DB}_src" -c "CREATE DATABASE $DB" > /dev/null 2>&1; then + echo "checking $(command -v pg_restore) ($(pg_restore --version))" + pq() { "$PSQL" -qXAt -d "$@" 2>/dev/null; } + pq "${DB}_src" -c "CREATE TABLE keyed(id int PRIMARY KEY); INSERT INTO keyed VALUES (1); + CREATE TABLE keyless(v int); INSERT INTO keyless VALUES (1); + CREATE TABLE fresh(v int); INSERT INTO fresh VALUES (1);" + pq "$DB" -c "CREATE TABLE keyed(id int PRIMARY KEY, extra int); INSERT INTO keyed VALUES (1, 9); + CREATE TABLE keyless(v int); INSERT INTO keyless VALUES (7);" + pg_dump -Fc -d "${DB}_src" -f "$WORK/pg.dump" + pg_restore --no-owner --no-acl -d "$DB" "$WORK/pg.dump" > /dev/null 2>&1 + expect "pg_restore carries on past errors and exits 1" "$?" "1" + expect "and keeps an existing table as it was" "$(pq "$DB" -c "SELECT string_agg(row(t.*)::text, ' ') FROM keyed t")" "(1,9)" + expect "and adds the dump's rows to a table without a key" "$(pq "$DB" -c "SELECT string_agg(v::text, ',' ORDER BY v) FROM keyless")" "1,7" + expect "and restores the rest" "$(pq "$DB" -c "SELECT count(*) FROM fresh")" "1" + "$PSQL" -qX -d postgres -c "DROP DATABASE $DB" -c "DROP DATABASE ${DB}_src" > /dev/null 2>&1 +else + echo "no psql, pg_dump and pg_restore that can create a scratch database; skipping" +fi + +if [ "$failures" -gt 0 ]; then + echo "$failures restore fact(s) differ from what RestoreConfirmationText says" >&2 + exit 1 +fi +echo "restore clients keep what RestoreConfirmationText says" From 04a8bd18d3626d67cdb0d54437ba190a3914c725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Nam=20Long?= Date: Sun, 1 Nov 2026 17:04:02 +0700 Subject: [PATCH 80/85] feat(ios): show table rows and query results in a grid (#3963) Co-authored-by: Ngo Quoc Dat --- .../TableProMobile/Localizable.xcstrings | 6 + .../Platform/AppPreferences.swift | 1 + .../Views/Components/RowCard.swift | 15 +- .../Views/Components/RowGrid.swift | 294 ++++++++++++++++++ .../Views/DataBrowserView.swift | 96 +++++- .../Views/QueryEditorView.swift | 40 ++- .../TableProMobile/Views/RowLayout.swift | 45 +++ .../Views/RowGridTests.swift | 153 +++++++++ .../Views/RowLayoutTests.swift | 65 ++++ apple/changelog.d/3963.added.md | 1 + 10 files changed, 694 insertions(+), 22 deletions(-) create mode 100644 apple/TableProMobile/TableProMobile/Views/Components/RowGrid.swift create mode 100644 apple/TableProMobile/TableProMobile/Views/RowLayout.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Views/RowGridTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Views/RowLayoutTests.swift create mode 100644 apple/changelog.d/3963.added.md diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index 8ec2eb210d..7d6a1d957a 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -4329,6 +4329,9 @@ "Car" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Cards" : { + }, "Cart" : { "extractionState" : "manual", @@ -12942,6 +12945,9 @@ "Layers" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Layout" : { + }, "Leaf" : { "extractionState" : "manual", diff --git a/apple/TableProMobile/TableProMobile/Platform/AppPreferences.swift b/apple/TableProMobile/TableProMobile/Platform/AppPreferences.swift index b7dac65eb5..109d9da615 100644 --- a/apple/TableProMobile/TableProMobile/Platform/AppPreferences.swift +++ b/apple/TableProMobile/TableProMobile/Platform/AppPreferences.swift @@ -8,6 +8,7 @@ nonisolated enum AppPreferences { static let defaultPageSizeKey = "com.TablePro.settings.defaultPageSize" static let defaultSafeModeKey = "com.TablePro.settings.defaultSafeMode" static let hideQueryPreviewInActivityKey = "com.TablePro.settings.hideQueryPreviewInActivity" + static let rowLayoutKey = "com.TablePro.settings.rowLayout" static let pageSizeOptions: [Int] = [50, 100, 200, 500] diff --git a/apple/TableProMobile/TableProMobile/Views/Components/RowCard.swift b/apple/TableProMobile/TableProMobile/Views/Components/RowCard.swift index 16d9a71b7f..ec6f1e6854 100644 --- a/apple/TableProMobile/TableProMobile/Views/Components/RowCard.swift +++ b/apple/TableProMobile/TableProMobile/Views/Components/RowCard.swift @@ -1,6 +1,12 @@ import SwiftUI import TableProModels +nonisolated enum RowKey { + static func columnIndex(columns: [ColumnInfo], primaryKeys: Set) -> Int? { + columns.firstIndex { primaryKeys.contains($0.name) } ?? (columns.isEmpty ? nil : 0) + } +} + struct RowCard: View { let columns: [ColumnInfo] let columnDetails: [ColumnInfo] @@ -12,12 +18,9 @@ struct RowCard: View { } private var titlePair: (name: String, value: String)? { - let pks = pkNames - for (col, val) in zip(columns, row) where pks.contains(col.name) { - return (col.name, val ?? "NULL") - } - guard let first = columns.first else { return nil } - return (first.name, row.first.flatMap { $0 } ?? "NULL") + guard let index = RowKey.columnIndex(columns: columns, primaryKeys: pkNames) else { return nil } + let value = row.indices.contains(index) ? row[index] : nil + return (columns[index].name, value ?? "NULL") } private var detailPairs: [(name: String, value: String)] { diff --git a/apple/TableProMobile/TableProMobile/Views/Components/RowGrid.swift b/apple/TableProMobile/TableProMobile/Views/Components/RowGrid.swift new file mode 100644 index 0000000000..90f1c2adea --- /dev/null +++ b/apple/TableProMobile/TableProMobile/Views/Components/RowGrid.swift @@ -0,0 +1,294 @@ +import SwiftUI +import TableProModels +import UIKit + +nonisolated struct RowGridMetrics: Equatable, Sendable { + var minimumWidth: CGFloat + var maximumWidth: CGFloat + var horizontalPadding: CGFloat +} + +nonisolated struct RowGridLayout: Equatable, Sendable { + static let sampledRowCount = 50 + // Past this many characters even the narrowest glyphs overflow the widest column. + static let maximumMeasuredCharacters = 128 + + let order: [Int] + let widths: [CGFloat] + + var totalWidth: CGFloat { widths.reduce(0, +) } + + static func make( + columnNames: [String], + rows: some Collection<[String?]>, + keyColumn: Int?, + metrics: RowGridMetrics, + measure: (_ text: String, _ isHeader: Bool) -> CGFloat + ) -> RowGridLayout { + let order = displayOrder(columnCount: columnNames.count, keyColumn: keyColumn) + let sample = rows.prefix(sampledRowCount) + let widestText = metrics.maximumWidth - metrics.horizontalPadding * 2 + let widths = order.map { column in + var widest = measure(measuredText(columnNames[column]), true) + for row in sample where widest < widestText { + let value = row.indices.contains(column) ? row[column] : nil + widest = max(widest, measure(measuredText(value ?? "NULL"), false)) + } + let width = widest.rounded(.up) + metrics.horizontalPadding * 2 + return min(max(width, metrics.minimumWidth), metrics.maximumWidth) + } + return RowGridLayout(order: order, widths: widths) + } + + // A key column wider than this share of the screen would leave no room to scroll the others past it. + static let keyColumnViewportShare: CGFloat = 0.4 + + func limitingKeyColumn(toViewportWidth viewportWidth: CGFloat) -> RowGridLayout { + let limit = (viewportWidth * Self.keyColumnViewportShare).rounded(.down) + guard let keyWidth = widths.first, limit > 0, keyWidth > limit else { return self } + return RowGridLayout(order: order, widths: [limit] + widths.dropFirst()) + } + + static func displayOrder(columnCount: Int, keyColumn: Int?) -> [Int] { + let columns = Array(0.. String { + var end = text.startIndex + var count = 0 + while end < text.endIndex, count < maximumMeasuredCharacters, !text[end].isNewline { + text.formIndex(after: &end) + count += 1 + } + let measured = String(text[.. CGFloat { + (text as NSString).size(withAttributes: [.font: isHeader ? headerFont : cellFont]).width + } +} + +nonisolated struct RowGridViewport: Equatable, Sendable { + let width: CGFloat + let leadingInset: CGFloat +} + +struct RowGrid: View { + let columns: [ColumnInfo] + let rows: [[String?]] + let keyColumn: Int? + @ViewBuilder let row: (_ index: Int, _ line: RowGridLine) -> Row + + @Environment(\.dynamicTypeSize) private var dynamicTypeSize + @Environment(\.displayScale) private var displayScale + @ScaledMetric(relativeTo: .subheadline) private var minimumWidth: CGFloat = 72 + @ScaledMetric(relativeTo: .subheadline) private var maximumWidth: CGFloat = 320 + @ScaledMetric(relativeTo: .subheadline) private var horizontalPadding: CGFloat = 12 + @ScaledMetric(relativeTo: .subheadline) private var verticalPadding: CGFloat = 10 + @State private var computedLayout: (key: LayoutKey, layout: RowGridLayout)? + @State private var viewport = RowGridViewport(width: 0, leadingInset: 0) + + private var layoutKey: LayoutKey { + LayoutKey( + columnNames: columns.map(\.name), + sample: rows.prefix(RowGridLayout.sampledRowCount), + keyColumn: keyColumn, + dynamicTypeSize: dynamicTypeSize, + metrics: RowGridMetrics( + minimumWidth: minimumWidth, maximumWidth: maximumWidth, horizontalPadding: horizontalPadding + ) + ) + } + + private var style: RowGridLine.Style { + RowGridLine.Style( + horizontalPadding: horizontalPadding, + verticalPadding: verticalPadding, + hairline: HairlineThickness.points(forDisplayScale: displayScale), + pinnedInset: viewport.leadingInset + ) + } + + var body: some View { + let layout = currentLayout.limitingKeyColumn(toViewportWidth: viewport.width) + ScrollView([.horizontal, .vertical]) { + grid(layout, width: max(layout.totalWidth, viewport.width)) + } + .scrollBounceBehavior(.basedOnSize, axes: .horizontal) + .defaultScrollAnchor(.topLeading, for: .alignment) + .buttonStyle(.plain) + // Changes only on a resize or an inset change, so scrolling runs no body pass. The container is the + // visible width; the insets are where the scroll view reaches under a sidebar or into the safe area. + .onScrollGeometryChange(for: RowGridViewport.self) { geometry in + RowGridViewport(width: geometry.containerSize.width, leadingInset: geometry.contentInsets.leading) + } action: { _, newViewport in + viewport = newViewport + } + // Reads only the key: the closure's other captures belong to the previous body. + .onChange(of: layoutKey, initial: true) { _, key in + computedLayout = (key, Self.makeLayout(key)) + } + } + + // Until onChange stores the layout for these inputs, it is measured here, so no pass draws an empty grid. + private var currentLayout: RowGridLayout { + let key = layoutKey + if let computedLayout, computedLayout.key == key { return computedLayout.layout } + return Self.makeLayout(key) + } + + private static func makeLayout(_ key: LayoutKey) -> RowGridLayout { + let measure = RowGridTextMeasure(dynamicTypeSize: key.dynamicTypeSize) + return RowGridLayout.make( + columnNames: key.columnNames, + rows: key.sample, + keyColumn: key.keyColumn, + metrics: key.metrics, + measure: measure.width(of:isHeader:) + ) + } + + private func grid(_ layout: RowGridLayout, width: CGFloat) -> some View { + let style = style + return LazyVStack(alignment: .leading, spacing: 0, pinnedViews: [.sectionHeaders]) { + Section { + ForEach(IndexedRow.wrap(rows)) { item in + row(item.id, RowGridLine( + columns: columns, values: item.values, layout: layout, width: width, style: style + )) + } + } header: { + header(layout, width: width, style: style) + } + } + .frame(width: width, alignment: .leading) + } + + private func header(_ layout: RowGridLayout, width: CGFloat, style: RowGridLine.Style) -> some View { + HStack(spacing: 0) { + ForEach(layout.order.indices, id: \.self) { position in + Text(verbatim: columns[layout.order[position]].name) + .font(.subheadline) + .fontWeight(.semibold) + .foregroundStyle(.secondary) + .lineLimit(1) + .padding(.horizontal, style.horizontalPadding) + .padding(.vertical, style.verticalPadding) + .frame(width: layout.widths[position], alignment: .leading) + .modifier(RowGridCell(isPinned: position == 0, style: style)) + } + } + .frame(width: width, alignment: .leading) + .background(Color(.systemBackground)) + .overlay(alignment: .bottom) { + Rectangle().fill(Color(.separator)).frame(height: style.hairline) + } + .accessibilityElement(children: .combine) + .accessibilityAddTraits(.isHeader) + } + + private struct LayoutKey: Equatable { + let columnNames: [String] + let sample: ArraySlice<[String?]> + let keyColumn: Int? + let dynamicTypeSize: DynamicTypeSize + let metrics: RowGridMetrics + } +} + +struct RowGridLine: View { + struct Style { + let horizontalPadding: CGFloat + let verticalPadding: CGFloat + let hairline: CGFloat + let pinnedInset: CGFloat + } + + let columns: [ColumnInfo] + let values: [String?] + let layout: RowGridLayout + let width: CGFloat + let style: Style + + var body: some View { + HStack(spacing: 0) { + ForEach(layout.order.indices, id: \.self) { position in + cell(value(at: layout.order[position])) + .frame(width: layout.widths[position], alignment: .leading) + .modifier(RowGridCell(isPinned: position == 0, style: style)) + } + } + .frame(width: width, alignment: .leading) + .overlay(alignment: .bottom) { + Rectangle().fill(Color(.separator)).frame(height: style.hairline) + } + .contentShape(Rectangle()) + .accessibilityElement(children: .ignore) + .accessibilityLabel(Text(verbatim: accessibilityText)) + } + + private func value(at column: Int) -> String? { + values.indices.contains(column) ? values[column] : nil + } + + private var accessibilityText: String { + layout.order + .filter { columns.indices.contains($0) } + .map { "\(columns[$0].name), \(value(at: $0) ?? "NULL")" } + .joined(separator: ", ") + } + + private func cell(_ value: String?) -> some View { + Group { + if let value { + Text(verbatim: value) + } else { + Text(verbatim: "NULL") + .foregroundStyle(.tertiary) + } + } + .font(.subheadline) + .lineLimit(1) + .padding(.horizontal, style.horizontalPadding) + .padding(.vertical, style.verticalPadding) + } +} + +private struct RowGridCell: ViewModifier { + let isPinned: Bool + let style: RowGridLine.Style + + func body(content: Content) -> some View { + if isPinned { + let inset = style.pinnedInset + // Offset in the render pass, so a sideways scroll moves no state and rebuilds no row. + content + // Reaches over the leading inset, so cells scrolled past the key never show beside it. + .background(Color(.systemBackground).padding(.leading, -inset)) + .overlay(alignment: .trailing) { + Rectangle().fill(Color(.separator)).frame(width: style.hairline) + } + .visualEffect { content, proxy in + content.offset(x: max(0, -proxy.frame(in: .scrollView).minX)) + } + .zIndex(1) + } else { + content + } + } +} diff --git a/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift b/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift index b70ab4aa42..f9e2336d32 100644 --- a/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift +++ b/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift @@ -37,6 +37,7 @@ struct DataBrowserView: View { @State private var isCompletingCells = false @State private var hapticSuccess = false @State private var hapticError = false + @AppStorage(AppPreferences.rowLayoutKey) private var preferredLayout: RowLayout? private var activeSchema: String? { coordinator.supportsSchemas ? coordinator.activeSchema : nil @@ -299,7 +300,21 @@ struct DataBrowserView: View { } } } else { - rowList + rowsView + } + } + + private var rowLayout: RowLayout { + RowLayout.resolved(preferred: preferredLayout, idiom: UIDevice.current.userInterfaceIdiom) + } + + @ViewBuilder + private var rowsView: some View { + switch rowLayout { + case .cards: + pageContainer(rowList) + case .grid: + pageContainer(rowGrid) } } @@ -311,12 +326,50 @@ struct DataBrowserView: View { } } .listStyle(.plain) - .id(viewModel.pagination.currentPage) - .opacity(viewModel.isPageLoading ? 0.5 : 1) - .allowsHitTesting(!viewModel.isPageLoading) - .overlay { if viewModel.isPageLoading { ProgressView() } } - .animation(.default, value: viewModel.isPageLoading) - .refreshable { await viewModel.load() } + } + + private var rowGrid: some View { + let primaryKeys = primaryKeyNames + return RowGrid( + columns: columns, + rows: rows, + keyColumn: RowKey.columnIndex(columns: columns, primaryKeys: primaryKeys) + ) { index, line in + NavigationLink { + rowDetail(index: index) + } label: { + line + } + .hoverEffect() + .contextMenu { + rowContextMenu(index: index) + } preview: { + // The row itself can be thousands of points wide; the lift preview shows its card. + RowCard( + columns: columns, + columnDetails: viewModel.columnDetails, + row: rows.indices.contains(index) ? rows[index] : [], + previewFieldCount: DuoLayoutResolver.regularPreviewFieldCount + ) + .padding() + .frame(maxWidth: 360, alignment: .leading) + } + .accessibilityActions { deleteAccessibilityAction(index: index) } + } + } + + private var primaryKeyNames: Set { + Set(viewModel.columnDetails.filter(\.isPrimaryKey).map(\.name)) + } + + private func pageContainer(_ rows: some View) -> some View { + rows + .id(viewModel.pagination.currentPage) + .opacity(viewModel.isPageLoading ? 0.5 : 1) + .allowsHitTesting(!viewModel.isPageLoading) + .overlay { if viewModel.isPageLoading { ProgressView() } } + .animation(.default, value: viewModel.isPageLoading) + .refreshable { await viewModel.load() } } private func rowDetail(index: Int) -> some View { @@ -351,8 +404,7 @@ struct DataBrowserView: View { } private func rowLink(index: Int, row: [String?]) -> some View { - let cells = viewModel.cells(at: index) - let deleteKeys = canDeleteRows ? viewModel.rowLocator(forRowAt: index) : nil + let deleteKeys = deletableRow(at: index) return NavigationLink { rowDetail(index: index) } label: { @@ -364,7 +416,7 @@ struct DataBrowserView: View { ) } .hoverEffect() - .contextMenu { rowContextMenu(cells: cells) } + .contextMenu { rowContextMenu(index: index) } .swipeActions(edge: .trailing, allowsFullSwipe: false) { if let deleteKeys { Button { @@ -375,10 +427,17 @@ struct DataBrowserView: View { .tint(.red) } } - .accessibilityActions { - if let deleteKeys { - Button("Delete row") { confirmDelete(deleteKeys) } - } + .accessibilityActions { deleteAccessibilityAction(index: index) } + } + + private func deletableRow(at index: Int) -> RowLocator? { + canDeleteRows ? viewModel.rowLocator(forRowAt: index) : nil + } + + @ViewBuilder + private func deleteAccessibilityAction(index: Int) -> some View { + if let locator = deletableRow(at: index) { + Button("Delete row") { confirmDelete(locator) } } } @@ -388,7 +447,8 @@ struct DataBrowserView: View { } @ViewBuilder - private func rowContextMenu(cells: [Cell]) -> some View { + private func rowContextMenu(index: Int) -> some View { + let cells = viewModel.cells(at: index) if DataBrowserViewModel.canComplete(cells) { Menu("Share Row") { ForEach(ExportFormat.offered(for: connection.type)) { format in @@ -435,6 +495,10 @@ struct DataBrowserView: View { } } } + if let locator = deletableRow(at: index) { + Divider() + Button("Delete Row", systemImage: "trash", role: .destructive) { confirmDelete(locator) } + } } @ToolbarContentBuilder @@ -450,6 +514,8 @@ struct DataBrowserView: View { } Divider() } + RowLayoutPicker() + Divider() Section("Export") { ForEach(ExportFormat.offered(for: connection.type)) { format in Button { diff --git a/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift b/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift index c63ee96275..c3c8841ab8 100644 --- a/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift +++ b/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift @@ -50,6 +50,7 @@ struct QueryEditorView: View { @State private var containerSize: CGSize = .zero @State private var savedQueriesRoute: SavedQueriesSheetRoute? @State private var savedQueryFolderExpansion: Set = [] + @AppStorage(AppPreferences.rowLayoutKey) private var preferredLayout: RowLayout? private var session: ConnectionSession? { coordinator.session } private var tables: [TableInfo] { coordinator.tables } @@ -427,7 +428,7 @@ struct QueryEditorView: View { } else { VStack(spacing: 0) { resultNotices(includesTruncation: true) - resultList + resultRows } } } @@ -541,6 +542,42 @@ struct QueryEditorView: View { .background(.thinMaterial) } + private var rowLayout: RowLayout { + RowLayout.resolved(preferred: preferredLayout, idiom: UIDevice.current.userInterfaceIdiom) + } + + @ViewBuilder + private var resultRows: some View { + switch rowLayout { + case .cards: resultList + case .grid: resultGrid + } + } + + private var resultGrid: some View { + let columns = viewModel.columns + let rows = viewModel.legacyRows + return RowGrid( + columns: columns, + rows: rows, + keyColumn: RowKey.columnIndex(columns: columns, primaryKeys: []) + ) { index, line in + NavigationLink { + RowDetailView(columns: columns, rows: viewModel.window.rows, initialIndex: index) + } label: { + line + } + .hoverEffect() + .contextMenu { + resultRowContextMenu(columns: columns, rowIndex: index) + } preview: { + resultRowCard(columns: columns, row: rows.indices.contains(index) ? rows[index] : []) + .padding() + .frame(maxWidth: 360, alignment: .leading) + } + } + } + private var resultList: some View { let indexed = IndexedRow.wrap(viewModel.legacyRows) return List { @@ -665,6 +702,7 @@ struct QueryEditorView: View { } if !viewModel.legacyRows.isEmpty { + RowLayoutPicker() Section("Share Results") { ForEach(ExportFormat.offered(for: databaseType)) { format in Button { diff --git a/apple/TableProMobile/TableProMobile/Views/RowLayout.swift b/apple/TableProMobile/TableProMobile/Views/RowLayout.swift new file mode 100644 index 0000000000..a6df08d533 --- /dev/null +++ b/apple/TableProMobile/TableProMobile/Views/RowLayout.swift @@ -0,0 +1,45 @@ +import SwiftUI +import UIKit + +nonisolated enum RowLayout: String, CaseIterable, Identifiable, Sendable { + case cards + case grid + + var id: String { rawValue } + + // The idiom, not the size class: a resize or rotation must not swap the layout under a pushed row. + static func resolved(preferred: RowLayout?, idiom: UIUserInterfaceIdiom) -> RowLayout { + if let preferred { return preferred } + return idiom == .phone ? .cards : .grid + } + + var title: String { + switch self { + case .cards: return String(localized: "Cards") + case .grid: return String(localized: "Grid") + } + } + + var systemImage: String { + switch self { + case .cards: return "rectangle.grid.1x2" + case .grid: return "tablecells" + } + } +} + +struct RowLayoutPicker: View { + @AppStorage(AppPreferences.rowLayoutKey) private var preferredLayout: RowLayout? + + var body: some View { + let current = RowLayout.resolved(preferred: preferredLayout, idiom: UIDevice.current.userInterfaceIdiom) + Picker(selection: Binding(get: { current }, set: { preferredLayout = $0 })) { + ForEach(RowLayout.allCases) { layout in + Label(layout.title, systemImage: layout.systemImage).tag(layout) + } + } label: { + Label("Layout", systemImage: current.systemImage) + } + .pickerStyle(.inline) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Views/RowGridTests.swift b/apple/TableProMobile/TableProMobileTests/Views/RowGridTests.swift new file mode 100644 index 0000000000..7d514acbea --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Views/RowGridTests.swift @@ -0,0 +1,153 @@ +import CoreGraphics +import SwiftUI +@testable import TableProMobile +import Testing +import UIKit + +struct RowGridLayoutTests { + private static let metrics = RowGridMetrics(minimumWidth: 72, maximumWidth: 320, horizontalPadding: 12) + + private static func layout( + _ names: [String], + _ rows: [[String?]], + keyColumn: Int? = 0, + measure: (String, Bool) -> CGFloat = { text, _ in CGFloat(text.count) * 8 } + ) -> RowGridLayout { + RowGridLayout.make(columnNames: names, rows: rows, keyColumn: keyColumn, metrics: metrics, measure: measure) + } + + @Test("The key column leads and the rest keep table order") + func keyColumnLeads() { + #expect(RowGridLayout.displayOrder(columnCount: 4, keyColumn: 2) == [2, 0, 1, 3]) + #expect(RowGridLayout.displayOrder(columnCount: 4, keyColumn: 0) == [0, 1, 2, 3]) + } + + @Test("A missing or out of range key column keeps table order") + func noKeyKeepsOrder() { + #expect(RowGridLayout.displayOrder(columnCount: 3, keyColumn: nil) == [0, 1, 2]) + #expect(RowGridLayout.displayOrder(columnCount: 3, keyColumn: 5) == [0, 1, 2]) + #expect(RowGridLayout.displayOrder(columnCount: 0, keyColumn: 0).isEmpty) + } + + @Test("Widths follow the display order, one per column, and a short row counts as NULL") + func widthPerDisplayedColumn() { + let layout = Self.layout(["a", "bbbbbbbbbbbbbbbbbbbb"], [["1"]], keyColumn: 1) + #expect(layout.order == [1, 0]) + #expect(layout.widths == [20 * 8 + 24, 72]) + #expect(layout.totalWidth == CGFloat(20 * 8 + 24 + 72)) + } + + @Test("Widths stay between the minimum and the maximum") + func widthsAreClamped() { + let layout = Self.layout(["id", "body"], [["1", String(repeating: "x", count: 500)]]) + #expect(layout.widths == [72, 320]) + } + + @Test("The header is measured as a header and values as cells") + func headerMeasuredAsHeader() { + var measured: [String: Bool] = [:] + _ = Self.layout(["name"], [["value"]]) { text, isHeader in + measured[text] = isHeader + return 0 + } + #expect(measured == ["name": true, "value": false]) + } + + @Test("A wider measurement widens the column even with fewer characters") + func widthFollowsMeasurement() { + let layout = Self.layout(["c"], [["abcdefghij"], ["日本語"]]) { text, _ in + text == "日本語" ? 200 : CGFloat(text.count) * 8 + } + #expect(layout.widths == [200 + 24]) + } + + @Test("Only the first rows are sampled") + func onlyFirstRowsSampled() { + var rows: [[String?]] = Array(repeating: ["x"], count: RowGridLayout.sampledRowCount) + rows.append([String(repeating: "y", count: 30)]) + #expect(Self.layout(["c"], rows).widths == [72]) + } + + @Test("A column already at the maximum stops measuring") + func stopsAtMaximum() { + var calls = 0 + let rows: [[String?]] = Array(repeating: [String(repeating: "z", count: 100)], count: 10) + _ = Self.layout(["c"], rows) { text, _ in + calls += 1 + return CGFloat(text.count) * 8 + } + #expect(calls == 2) + } + + @Test("The key column takes at most its share of the viewport, and only the key column shrinks") + func keyColumnLimitedByViewport() { + let layout = RowGridLayout(order: [0, 1], widths: [300, 200]) + #expect(layout.limitingKeyColumn(toViewportWidth: 400).widths == [160, 200]) + #expect(layout.limitingKeyColumn(toViewportWidth: 1_000).widths == [300, 200]) + #expect(layout.limitingKeyColumn(toViewportWidth: 0).widths == [300, 200]) + #expect(RowGridLayout(order: [], widths: []).limitingKeyColumn(toViewportWidth: 400).widths.isEmpty) + } + + @Test("Only the first line counts, with room for the ellipsis that marks the rest") + func firstLineWithEllipsis() { + #expect(RowGridLayout.measuredText("first line\nsecond") == "first line…") + #expect(RowGridLayout.measuredText("one line") == "one line") + #expect(RowGridLayout.measuredText("") == "") + } + + @Test("A long value is measured up to the cap, not to its end") + func longValueCapped() { + let text = String(repeating: "a", count: 4_096) + let measured = RowGridLayout.measuredText(text) + #expect(measured.count == RowGridLayout.maximumMeasuredCharacters + 1) + #expect(measured.hasSuffix("…")) + } +} + +@MainActor +struct RowGridTextMeasureTests { + private static let samples: [(text: String, isHeader: Bool)] = [ + ("11170334", false), + ("2026-11-01 09:41:00", false), + ("WWWWWWWWWW", false), + ("For Those About To Rock (We Salute You)", false), + ("日本語のテキストと中文", false), + ("Ünïcødé ẞtraße", false), + ("NULL", false), + ("TrackId", true), + ("PlaylistId", true), + ("InvoiceLineId", true), + ("AlbumId", true), + ("UnitPrice", true), + ("MediaTypeId", true), + ] + + private static func renderedWidth(_ text: String, isHeader: Bool, size: DynamicTypeSize) -> CGFloat { + let view = Text(verbatim: text) + .font(.subheadline) + .fontWeight(isHeader ? .semibold : nil) + .lineLimit(1) + .fixedSize() + .environment(\.dynamicTypeSize, size) + return UIHostingController(rootView: view) + .sizeThatFits(in: CGSize(width: CGFloat.greatestFiniteMagnitude, height: .greatestFiniteMagnitude)) + .width + } + + @Test("A measured width holds the text as SwiftUI renders it", arguments: [DynamicTypeSize.large, .xxxLarge, .accessibility3]) + func measuredWidthHoldsRenderedText(size: DynamicTypeSize) { + let measure = RowGridTextMeasure(dynamicTypeSize: size) + for sample in Self.samples { + let measured = measure.width(of: sample.text, isHeader: sample.isHeader).rounded(.up) + let rendered = Self.renderedWidth(sample.text, isHeader: sample.isHeader, size: size) + #expect(measured >= rendered, "\(sample.text) at \(size): measured \(measured), rendered \(rendered)") + } + } + + @Test("Larger text measures wider") + func scalesWithDynamicType() { + let regular = RowGridTextMeasure(dynamicTypeSize: .large).width(of: "AlbumId", isHeader: false) + let large = RowGridTextMeasure(dynamicTypeSize: .accessibility3).width(of: "AlbumId", isHeader: false) + #expect(large > regular) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Views/RowLayoutTests.swift b/apple/TableProMobile/TableProMobileTests/Views/RowLayoutTests.swift new file mode 100644 index 0000000000..d855ff4d13 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Views/RowLayoutTests.swift @@ -0,0 +1,65 @@ +import Foundation +import SwiftUI +import TableProModels +@testable import TableProMobile +import Testing +import UIKit + +struct RowLayoutTests { + @Test("With no choice made, iPhone keeps the cards and every other device opens the grid") + func defaultFollowsIdiom() { + #expect(RowLayout.resolved(preferred: nil, idiom: .phone) == .cards) + #expect(RowLayout.resolved(preferred: nil, idiom: .pad) == .grid) + #expect(RowLayout.resolved(preferred: nil, idiom: .mac) == .grid) + } + + @Test("A choice the user made wins on every device", arguments: RowLayout.allCases) + func choiceWinsOnEveryDevice(layout: RowLayout) { + for idiom in [UIUserInterfaceIdiom.phone, .pad, .mac] { + #expect(RowLayout.resolved(preferred: layout, idiom: idiom) == layout) + } + } + + @Test("A stored choice is read back by a later browser") + func storedChoiceIsRemembered() throws { + let suite = "RowLayoutTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + defer { defaults.removePersistentDomain(forName: suite) } + + let writer = AppStorage(AppPreferences.rowLayoutKey, store: defaults) + #expect(writer.wrappedValue == nil) + writer.wrappedValue = .grid + + let reader = AppStorage(AppPreferences.rowLayoutKey, store: defaults) + #expect(reader.wrappedValue == .grid) + #expect(defaults.string(forKey: AppPreferences.rowLayoutKey) == "grid") + } + + @Test("An unknown stored value falls back to the device default") + func unknownStoredValueIsIgnored() throws { + let suite = "RowLayoutTests.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: suite)) + defer { defaults.removePersistentDomain(forName: suite) } + defaults.set("table", forKey: AppPreferences.rowLayoutKey) + + let stored = AppStorage(AppPreferences.rowLayoutKey, store: defaults).wrappedValue + #expect(stored == nil) + #expect(RowLayout.resolved(preferred: stored, idiom: .phone) == .cards) + } +} + +struct RowKeyTests { + private static let columns = ["created_at", "name", "id", "code"].map { ColumnInfo(name: $0, typeName: "text") } + + @Test("The first primary key column in table order names the row") + func primaryKeyNamesTheRow() { + #expect(RowKey.columnIndex(columns: Self.columns, primaryKeys: ["id"]) == 2) + #expect(RowKey.columnIndex(columns: Self.columns, primaryKeys: ["code", "id"]) == 2) + } + + @Test("Without a primary key the first column names the row, and no columns name nothing") + func firstColumnWithoutKey() { + #expect(RowKey.columnIndex(columns: Self.columns, primaryKeys: []) == 0) + #expect(RowKey.columnIndex(columns: [], primaryKeys: ["id"]) == nil) + } +} diff --git a/apple/changelog.d/3963.added.md b/apple/changelog.d/3963.added.md new file mode 100644 index 0000000000..552d0db539 --- /dev/null +++ b/apple/changelog.d/3963.added.md @@ -0,0 +1 @@ +iPhone and iPad show table rows and query results in a grid with a pinned header and key column, beside the cards. From 4cd4f06f37f13f3398c6fcb9ff249dbe346976e2 Mon Sep 17 00:00:00 2001 From: Victor Babenko Date: Sun, 1 Nov 2026 02:31:59 -0800 Subject: [PATCH 81/85] fix(inspector): field resize handle tracks the drag and keeps a height per column (#3975) Co-authored-by: Cursor Co-authored-by: Ngo Quoc Dat --- .claude/rules/data-sync-storage.md | 1 + apple/TablePro/AppDelegate.swift | 2 +- .../Core/Storage/AppSettingsManager.swift | 1 + .../Core/Storage/AppSettingsStorage.swift | 27 ++- .../Storage/InspectorFieldHeightStore.swift | 83 +++++++ .../Storage/Preferences/PreferenceKeys.swift | 10 +- .../Models/UI/MultiRowEditState.swift | 8 + .../ResizeCursorSplitViewController.swift | 13 +- .../FieldEditors/FieldResizeHandle.swift | 138 ++++++++++++ .../FieldEditors/GeometryFieldView.swift | 56 ++--- .../FieldEditors/JsonEditorView.swift | 15 +- .../FieldEditors/MultiLineEditorView.swift | 6 +- .../ResizableEditorContainer.swift | 86 ++++--- .../FieldEditors/ResizableFieldMetrics.swift | 18 +- .../RowInspector/InspectorFieldListView.swift | 60 ++--- .../AppSettingsStorageMigrationTests.swift | 47 ++-- .../AppSettingsStorageResetTests.swift | 10 +- .../InspectorFieldHeightStoreTests.swift | 80 +++++++ .../Storage/PreferenceKeysGuardTests.swift | 4 +- .../UI/MultiRowEditStateExpansionTests.swift | 80 +++++++ .../RowInspector/FieldResizeHandleTests.swift | 211 ++++++++++++++++++ .../ResizableFieldMetricsTests.swift | 13 ++ apple/changelog.d/3975.changed.md | 1 + apple/changelog.d/3975.fixed.1.md | 1 + apple/changelog.d/3975.fixed.md | 1 + 25 files changed, 768 insertions(+), 204 deletions(-) create mode 100644 apple/TablePro/Core/Storage/InspectorFieldHeightStore.swift create mode 100644 apple/TablePro/Views/RowInspector/FieldEditors/FieldResizeHandle.swift create mode 100644 apple/TableProTests/Core/Storage/InspectorFieldHeightStoreTests.swift create mode 100644 apple/TableProTests/Models/UI/MultiRowEditStateExpansionTests.swift create mode 100644 apple/TableProTests/Views/RowInspector/FieldResizeHandleTests.swift create mode 100644 apple/changelog.d/3975.changed.md create mode 100644 apple/changelog.d/3975.fixed.1.md create mode 100644 apple/changelog.d/3975.fixed.md diff --git a/.claude/rules/data-sync-storage.md b/.claude/rules/data-sync-storage.md index daafa2f767..042a2bf20a 100644 --- a/.claude/rules/data-sync-storage.md +++ b/.claude/rules/data-sync-storage.md @@ -35,6 +35,7 @@ This path holds user data. A silent wrong answer here looks exactly like an empt | Filter defaults and presets | UserDefaults | `FilterSettingsStorage`, `FilterPresetStorage` | | Per-table filters | JSON, one file per table | `FilterSettingsStorage` | | Generate Rows settings per table | UserDefaults, device-local | `RowGenerationSettingsStore` | +| Row inspector field heights, per column name | UserDefaults, device-local | `InspectorFieldHeightStore` | | Favorite tables (iCloud-synced) | UserDefaults | `FavoriteTablesStorage` | | Sidebar database and schema filters, recent tables | UserDefaults, device-local | `DatabaseTreeFilterStorage`, `RecentTablesStore` | | History drawer state | UserDefaults, device-local | `HistoryPanelPreferencesStorage` | diff --git a/apple/TablePro/AppDelegate.swift b/apple/TablePro/AppDelegate.swift index d87a339acd..c50ee76efa 100644 --- a/apple/TablePro/AppDelegate.swift +++ b/apple/TablePro/AppDelegate.swift @@ -21,7 +21,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationWillFinishLaunching(_ notification: Notification) { LaunchTracer.shared.mark(.willFinishLaunchingBegan) AppSettingsStorage.shared.migrateStartupBehaviorToReopenLastIfNeeded() - AppSettingsStorage.shared.migrateJsonFieldHeightKeyIfNeeded() + AppSettingsStorage.shared.removeRetiredFieldHeightKeys() AIProviderRegistration.registerAll() /// Installed before any window exists, so the bar is correct from the first frame. diff --git a/apple/TablePro/Core/Storage/AppSettingsManager.swift b/apple/TablePro/Core/Storage/AppSettingsManager.swift index 253cc4f055..47b3a24e27 100644 --- a/apple/TablePro/Core/Storage/AppSettingsManager.swift +++ b/apple/TablePro/Core/Storage/AppSettingsManager.swift @@ -277,6 +277,7 @@ final class AppSettingsManager: ObservableObject { mcp = .default typography = .default storage.resetToDefaults() + InspectorFieldHeightStore.shared.reload() connectionListPreferences.resetShowsRecent() SoftwareUpdater.shared.resetUpdatePreferences() AppModuleActivation.resetToDefaults() diff --git a/apple/TablePro/Core/Storage/AppSettingsStorage.swift b/apple/TablePro/Core/Storage/AppSettingsStorage.swift index 6e789f10e3..42809b4038 100644 --- a/apple/TablePro/Core/Storage/AppSettingsStorage.swift +++ b/apple/TablePro/Core/Storage/AppSettingsStorage.swift @@ -37,8 +37,14 @@ final class AppSettingsStorage: Sendable { static let typography = "com.TablePro.settings.typography" static let hasSeenWelcomeSheet = "com.TablePro.settings.hasCompletedOnboarding" static let startupReopenMigration = "com.TablePro.settings.didMigrateStartupToReopenLast" - static let jsonFieldHeightMigration = "com.TablePro.settings.didMigrateJsonFieldHeightKey" - static let legacyJsonFieldHeight = "rightSidebar.jsonFieldHeight" + // One height per editor kind, before a height belonged to its column. + static let retiredFieldHeights = [ + "rightSidebar.jsonFieldHeight", + "com.TablePro.rightSidebar.jsonFieldHeight", + "com.TablePro.rightSidebar.textFieldHeight", + "com.TablePro.rightSidebar.geometryFieldHeight", + "com.TablePro.settings.didMigrateJsonFieldHeightKey", + ] } /// The storage environment's defaults, not `.standard`. A UI test runs against a per-sandbox @@ -71,15 +77,10 @@ final class AppSettingsStorage: Sendable { saveGeneral(general) } - func migrateJsonFieldHeightKeyIfNeeded() { - guard !defaults.bool(forKey: Keys.jsonFieldHeightMigration) else { return } - defaults.set(true, forKey: Keys.jsonFieldHeightMigration) - - let newKey = PreferenceKeys.rowInspectorJsonFieldHeight.name - guard defaults.object(forKey: Keys.legacyJsonFieldHeight) != nil, - defaults.object(forKey: newKey) == nil else { return } - defaults.set(defaults.double(forKey: Keys.legacyJsonFieldHeight), forKey: newKey) - defaults.removeObject(forKey: Keys.legacyJsonFieldHeight) + func removeRetiredFieldHeightKeys() { + for key in Keys.retiredFieldHeights where defaults.object(forKey: key) != nil { + defaults.removeObject(forKey: key) + } } // MARK: - Appearance Settings @@ -310,9 +311,7 @@ final class AppSettingsStorage: Sendable { saveMCP(.default) saveTypography(.default) defaults.removeObject(forKey: PreferenceKeys.selectedSettingsPane.name) - defaults.removeObject(forKey: PreferenceKeys.rowInspectorJsonFieldHeight.name) - defaults.removeObject(forKey: PreferenceKeys.rowInspectorTextFieldHeight.name) - defaults.removeObject(forKey: PreferenceKeys.rowInspectorGeometryFieldHeight.name) + defaults.removeObject(forKey: PreferenceKeys.rowInspectorFieldHeights.name) defaults.removeObject(forKey: PreferenceKeys.rowInspectorGeometryFieldMode.name) defaults.removeObject(forKey: SidebarPersistenceKey.defaultLayout) RoutineDebugOptionStore(defaults: { [defaults] in defaults }).removeAll() diff --git a/apple/TablePro/Core/Storage/InspectorFieldHeightStore.swift b/apple/TablePro/Core/Storage/InspectorFieldHeightStore.swift new file mode 100644 index 0000000000..aef607926e --- /dev/null +++ b/apple/TablePro/Core/Storage/InspectorFieldHeightStore.swift @@ -0,0 +1,83 @@ +// +// InspectorFieldHeightStore.swift +// TablePro +// + +import Combine +import Foundation +import os + +// Device-local. Keyed by column name alone, so a size holds on every row and in every result showing that column. +@MainActor +internal final class InspectorFieldHeightStore: ObservableObject { + static let shared = InspectorFieldHeightStore() + static let capacity = 200 + + nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "InspectorFieldHeightStore") + private static let key = PreferenceKeys.rowInspectorFieldHeights.name + + @Published private(set) var heights: [String: Double] = [:] + // Least recently set first, so the oldest is dropped past `capacity`. + private var order: [String] = [] + private let store: KeyValueStore + + init(defaults: KeyValueStore = AppStorageEnvironment.shared.defaults) { + store = defaults + reload() + } + + func height(for column: String) -> Double? { + heights[column] + } + + func setHeight(_ height: Double, for column: String) { + order.removeAll { $0 == column } + order.append(column) + var updated = heights + updated[column] = height + if order.count > Self.capacity { + for evicted in order.prefix(order.count - Self.capacity) { + updated[evicted] = nil + } + order.removeFirst(order.count - Self.capacity) + } + heights = updated + persist() + } + + func reload() { + var loaded: [String: Double] = [:] + var loadedOrder: [String] = [] + for entry in store.dataValue(forKey: Self.key).flatMap(Self.decode) ?? [] { + if loaded.updateValue(entry.height, forKey: entry.column) != nil { + loadedOrder.removeAll { $0 == entry.column } + } + loadedOrder.append(entry.column) + } + order = loadedOrder + heights = loaded + } + + private func persist() { + let entries = order.compactMap { column in heights[column].map { Entry(column: column, height: $0) } } + do { + store.setDataValue(try JSONEncoder().encode(entries), forKey: Self.key) + } catch { + Self.logger.error("Could not save field heights: \(error.publicLogShape, privacy: .public)") + } + } + + private static func decode(_ data: Data) -> [Entry]? { + do { + return try JSONDecoder().decode([Entry].self, from: data) + } catch { + logger.error("Discarding unreadable field heights: \(error.publicLogShape, privacy: .public)") + return nil + } + } + + private struct Entry: Codable { + let column: String + let height: Double + } +} diff --git a/apple/TablePro/Core/Storage/Preferences/PreferenceKeys.swift b/apple/TablePro/Core/Storage/Preferences/PreferenceKeys.swift index a6514424f8..e9cbca1d32 100644 --- a/apple/TablePro/Core/Storage/Preferences/PreferenceKeys.swift +++ b/apple/TablePro/Core/Storage/Preferences/PreferenceKeys.swift @@ -4,10 +4,8 @@ enum PreferenceKeys { static let linkedFolders = DefaultsKey<[LinkedFolder]>("com.TablePro.linkedFolders") static let linkedSQLFolders = DefaultsKey<[LinkedSQLFolder]>("com.TablePro.linkedSQLFolders") static let selectedSettingsPane = DefaultsKey("com.TablePro.settings.selectedPane") - static let rowInspectorJsonFieldHeight = DefaultsKey("com.TablePro.rightSidebar.jsonFieldHeight") - static let rowInspectorTextFieldHeight = DefaultsKey("com.TablePro.rightSidebar.textFieldHeight") - static let rowInspectorGeometryFieldHeight = DefaultsKey("com.TablePro.rightSidebar.geometryFieldHeight") - /// Text or Map, as the field's picker last left it. Device-local, like the heights beside it. + static let rowInspectorFieldHeights = DefaultsKey("com.TablePro.rightSidebar.fieldHeights") + // Text or Map, as the field's picker last left it. Device-local, like the heights beside it. static let rowInspectorGeometryFieldMode = DefaultsKey("com.TablePro.rightSidebar.geometryFieldMode") static let workspaceRailOrder = DefaultsKey<[WorkspaceID]>("com.TablePro.workspaceRail.order") static let queryPlanRawFontSize = DefaultsKey("com.TablePro.queryPlan.rawFontSize") @@ -33,9 +31,7 @@ enum PreferenceKeys { linkedFolders.name, linkedSQLFolders.name, selectedSettingsPane.name, - rowInspectorJsonFieldHeight.name, - rowInspectorTextFieldHeight.name, - rowInspectorGeometryFieldHeight.name, + rowInspectorFieldHeights.name, rowInspectorGeometryFieldMode.name, workspaceRailOrder.name, queryPlanRawFontSize.name, diff --git a/apple/TablePro/Models/UI/MultiRowEditState.swift b/apple/TablePro/Models/UI/MultiRowEditState.swift index b4c2c95517..26c98ce2f2 100644 --- a/apple/TablePro/Models/UI/MultiRowEditState.swift +++ b/apple/TablePro/Models/UI/MultiRowEditState.swift @@ -127,6 +127,8 @@ final class MultiRowEditState: ObservableObject { @Published private(set) var expressionCells: [Set] = [] @Published private(set) var columns: [String] = [] @Published private(set) var columnTypes: [ColumnType] = [] + // By column, not field id: every selection change reissues the ids. Cleared when the columns change. + @Published private(set) var expandedColumn: Int? private var nonNullableColumns: Set = [] // Nil and .none offer no Set Value command and promote nothing. @@ -180,6 +182,7 @@ final class MultiRowEditState: ObservableObject { self.columnTypes = columnTypes self.nonNullableColumns = nonNullableColumns self.databaseType = databaseType + if columnsChanged { expandedColumn = nil } self.cellWriter = cellWriter rowsAreInserted = !rowIDs.isEmpty && rowIDs.allSatisfy { $0.isInserted } @@ -576,6 +579,10 @@ final class MultiRowEditState: ObservableObject { } } + func toggleExpansion(ofColumn index: Int) { + expandedColumn = expandedColumn == index ? nil : index + } + func clearEdits() { for index in fields.indices { resetPending(at: index) @@ -598,6 +605,7 @@ final class MultiRowEditState: ObservableObject { columns = [] columnTypes = [] nonNullableColumns = [] + expandedColumn = nil databaseType = nil cellWriter = .none rowsAreInserted = false diff --git a/apple/TablePro/Views/Components/ResizeCursorSplitViewController.swift b/apple/TablePro/Views/Components/ResizeCursorSplitViewController.swift index 1916057b08..89529747ff 100644 --- a/apple/TablePro/Views/Components/ResizeCursorSplitViewController.swift +++ b/apple/TablePro/Views/Components/ResizeCursorSplitViewController.swift @@ -109,11 +109,16 @@ internal class ResizeCursorSplitViewController: NSSplitViewController { } private var resizeCursor: NSCursor { + .dividerResize(isVertical: splitView.isVertical) + } +} + +internal extension NSCursor { + // `isVertical` as NSSplitView means it: a vertical divider resizes left and right. + static func dividerResize(isVertical: Bool) -> NSCursor { if #available(macOS 15.0, *) { - return splitView.isVertical - ? .columnResize(directions: [.left, .right]) - : .rowResize(directions: [.up, .down]) + return isVertical ? .columnResize(directions: [.left, .right]) : .rowResize(directions: [.up, .down]) } - return splitView.isVertical ? .resizeLeftRight : .resizeUpDown + return isVertical ? .resizeLeftRight : .resizeUpDown } } diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/FieldResizeHandle.swift b/apple/TablePro/Views/RowInspector/FieldEditors/FieldResizeHandle.swift new file mode 100644 index 0000000000..b23f87e060 --- /dev/null +++ b/apple/TablePro/Views/RowInspector/FieldEditors/FieldResizeHandle.swift @@ -0,0 +1,138 @@ +// +// FieldResizeHandle.swift +// TablePro +// + +import AppKit +import SwiftUI + +// AppKit on purpose: in the inspector's List, an NSTableView, a SwiftUI DragGesture lost the mouse to the table a few points into the drag. +internal struct FieldResizeHandle: NSViewRepresentable { + let onDrag: (Double) -> Void + let onEnd: (Double) -> Void + + func makeNSView(context: Context) -> FieldResizeHandleView { + FieldResizeHandleView() + } + + func updateNSView(_ view: FieldResizeHandleView, context: Context) { + view.onDrag = onDrag + view.onEnd = onEnd + view.isEnabled = context.environment.isEnabled + } +} + +internal final class FieldResizeHandleView: NSView { + static let gripSize = NSSize(width: 26, height: 4) + + var onDrag: ((Double) -> Void)? + var onEnd: ((Double) -> Void)? + + // The inspector keeps its field list mounted, disabled, behind the JSON rendering. + var isEnabled = true { + didSet { + guard isEnabled != oldValue, !isEnabled else { return } + drag = nil + isHovered = false + } + } + + private(set) var isHovered = false { + didSet { if isHovered != oldValue { needsDisplay = true } } + } + + // Window coordinates, which stay put while the field grows and moves this view down. + private var drag: (startY: CGFloat, hasMoved: Bool)? { + didSet { if (drag == nil) != (oldValue == nil) { needsDisplay = true } } + } + + var isHighlighted: Bool { isHovered || drag != nil } + + override var mouseDownCanMoveWindow: Bool { false } + + override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true } + + override func hitTest(_ point: NSPoint) -> NSView? { + isEnabled ? super.hitTest(point) : nil + } + + override func updateTrackingAreas() { + super.updateTrackingAreas() + for area in trackingAreas where area.owner === self { + removeTrackingArea(area) + } + addTrackingArea(NSTrackingArea( + rect: .zero, + options: [.mouseEnteredAndExited, .cursorUpdate, .activeInKeyWindow, .inVisibleRect], + owner: self + )) + } + + override func cursorUpdate(with event: NSEvent) { + guard isEnabled else { + super.cursorUpdate(with: event) + return + } + NSCursor.dividerResize(isVertical: false).set() + } + + override func mouseEntered(with event: NSEvent) { + isHovered = isEnabled + } + + override func mouseExited(with event: NSEvent) { + isHovered = false + } + + // A handle removed mid-drag, as Expand does, gets no mouseUp it could act on. + override func viewWillMove(toWindow newWindow: NSWindow?) { + super.viewWillMove(toWindow: newWindow) + guard newWindow == nil else { return } + drag = nil + isHovered = false + } + + override func mouseDown(with event: NSEvent) { + guard isEnabled else { return } + drag = (event.locationInWindow.y, false) + } + + override func mouseDragged(with event: NSEvent) { + guard let delta = downwardDelta(to: event) else { return } + drag?.hasMoved = true + NSCursor.dividerResize(isVertical: false).set() + onDrag?(delta) + } + + override func mouseUp(with event: NSEvent) { + guard let delta = downwardDelta(to: event), let drag else { return } + self.drag = nil + if drag.hasMoved { onEnd?(delta) } + } + + override func draw(_ dirtyRect: NSRect) { + guard let context = NSGraphicsContext.current?.cgContext else { return } + let size = Self.gripSize + let grip = NSRect( + x: bounds.midX - size.width / 2, + y: bounds.midY - size.height / 2, + width: size.width, + height: size.height + ) + context.saveGState() + context.setAlpha(isHighlighted ? 1 : 0.5) + NSColor.tertiaryLabelColor.setFill() + NSBezierPath(roundedRect: grip, xRadius: size.height / 2, yRadius: size.height / 2).fill() + context.restoreGState() + } + + override func viewDidChangeEffectiveAppearance() { + super.viewDidChangeEffectiveAppearance() + needsDisplay = true + } + + private func downwardDelta(to event: NSEvent) -> Double? { + guard let drag else { return nil } + return Double(drag.startY - event.locationInWindow.y) + } +} diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/GeometryFieldView.swift b/apple/TablePro/Views/RowInspector/FieldEditors/GeometryFieldView.swift index ec283a95de..0efecf9236 100644 --- a/apple/TablePro/Views/RowInspector/FieldEditors/GeometryFieldView.swift +++ b/apple/TablePro/Views/RowInspector/FieldEditors/GeometryFieldView.swift @@ -7,22 +7,19 @@ import Combine import SwiftUI import TableProTheme -/// A field whose value is a geometry: a map of that value, with the editor the field would -/// otherwise get one segment away. +// A geometry value drawn as a map, with the field's own editor one segment away. internal struct GeometryFieldView: View { @ObservedObject private var themeEngine = ThemeEngine.shared let context: FieldEditorContext let descriptor: GeometryFieldDescriptor var onPopOut: ((String) -> Void)? var isExpanded = false - /// Told whether Tab can stop here without Full Keyboard Access. A map takes no focus. + // Told whether Tab can stop here without Full Keyboard Access; a map takes no focus. var onTabStopChange: ((Bool) -> Void)? @Environment(\.isEnabled) private var isEnabled @AppStorage(PreferenceKeys.rowInspectorGeometryFieldMode.name, store: AppStorageEnvironment.shared.defaults) private var storedMode = Mode.map.rawValue - @AppStorage(PreferenceKeys.rowInspectorGeometryFieldHeight.name, store: AppStorageEnvironment.shared.defaults) - private var mapHeight = ResizableFieldMetrics.defaultGeometryHeight @StateObject private var previews = GeometryFieldPreviewStore() @State private var latch = ModeLatch() @@ -44,15 +41,12 @@ internal struct GeometryFieldView: View { mapPane(resolution.preview) } } - /// The row puts the field's text on its editor as an accessibility value. On a bare - /// container that lands on the picker and the caption, in place of the selected segment. + // On a bare container the row's accessibility value lands on the picker and the caption. .accessibilityElement(children: .contain) .onAppear { onTabStopChange?(mode == .text) } - /// No reset on disappear: a lazy list unmounts a row scrolled out of view while the field is - /// still in the list, and Tab would then stop on a map with nothing to focus. + // No reset on disappear: the lazy list unmounts off-screen rows, and Tab would then stop on a map. .onChange(of: mode) { onTabStopChange?($0 == .text) } - /// Runs again when a read starts or ends, so the mode is latched by whichever preview - /// comes first: the one read during this pass, or the large one read here. + // Reruns when a read starts or ends, so whichever preview comes first latches the mode. .task(id: resolution.pending) { guard let pending = resolution.pending else { latch.offer(preferred: preferredMode, preview: resolution.preview) @@ -63,7 +57,7 @@ internal struct GeometryFieldView: View { } } - /// Text mode reads nothing: the value is only parsed while a map is, or may be, on screen. + // Text mode reads nothing: the value is parsed only while a map is, or may be, on screen. private func resolvePreview() -> GeometryFieldPreviewStore.Resolution { guard latch.needsPreview(preferred: preferredMode) else { return GeometryFieldPreviewStore.Resolution(preview: nil, pending: nil) @@ -113,8 +107,7 @@ internal struct GeometryFieldView: View { } } - /// Only the picker latches a mode and only the picker writes the preference, so a value that - /// becomes drawable while it is typed never moves the field off Text. + // Only the picker latches and stores the mode, so a value turning drawable as it is typed stays on Text. private func modeSelection(shown mode: Mode) -> Binding { Binding( get: { mode }, @@ -125,7 +118,7 @@ internal struct GeometryFieldView: View { ) } - /// No window edits bytes: the text window would write the bytes back as text. + // No window edits bytes: the text window would write them back as text. private func offersWindow(in mode: Mode) -> Bool { switch mode { case .map: return true @@ -152,8 +145,7 @@ internal struct GeometryFieldView: View { return false } - /// The map window is handed the text alone, so it would call a NULL field or a selection of - /// several values unreadable. It opens for a value that draws and for nothing else. + // The map window gets the text alone and would call NULL or mixed values unreadable, so only a drawable value opens it. static func canOpenWindow(in mode: Mode, preview: GeometryFieldPreview?) -> Bool { switch mode { case .map: return isDrawable(preview) @@ -163,8 +155,7 @@ internal struct GeometryFieldView: View { // MARK: - Panes - /// The embedded editors carry no pop-out button of their own: the toolbar's one button serves - /// both segments. This kind opts out of the row's value font, so the pane names it. + // The toolbar's pop-out serves both segments; this kind opts out of the row's value font, so the pane sets it. private var textPane: some View { Group { switch descriptor.textEditor { @@ -182,7 +173,8 @@ internal struct GeometryFieldView: View { private func mapPane(_ preview: GeometryFieldPreview?) -> some View { VStack(alignment: .leading, spacing: 2) { ResizableEditorContainer( - height: $mapHeight, + column: context.columnName, + defaultHeight: ResizableFieldMetrics.defaultGeometryHeight, range: ResizableFieldMetrics.geometryHeightRange, expandedHeight: isExpanded ? ResizableFieldMetrics.expandedHeight : nil ) { @@ -199,8 +191,7 @@ internal struct GeometryFieldView: View { } } - /// The canvas is dropped while the list is disabled, which is how the inspector hides it behind - /// its JSON rendering: a mounted map view holds about 45 MB whether or not it is on screen. + // Dropped while disabled, which is how the inspector hides it behind the JSON rendering: a mounted map holds about 45 MB. @ViewBuilder private func mapContent(_ preview: GeometryFieldPreview?) -> some View { switch preview { @@ -235,7 +226,7 @@ internal extension GeometryFieldView { case text case map - /// Nil while the first read of a large value is still running: nothing has decided yet. + // Nil while the first read of a large value is still running. static func opening(preferred: Mode, preview: GeometryFieldPreview?) -> Mode? { guard preferred == .map else { return .text } switch preview { @@ -246,8 +237,7 @@ internal extension GeometryFieldView { } } - /// The mode a field opens on is decided once per view. Following the text instead would swap - /// the editor out from under the caret the moment a typed value became drawable. + // Decided once per view: following the text would swap the editor from under the caret once a typed value draws. struct ModeLatch: Equatable { private(set) var mode: Mode? @@ -272,8 +262,7 @@ internal extension GeometryFieldView { // MARK: - Preview store -/// Reads a value once per text rather than once per body pass: the list redraws every field on -/// each keystroke in any of them. Not published, so filling it during a pass redraws nothing. +// Once per text, not per body pass, since every keystroke redraws every field. Unpublished, so filling it redraws nothing. @MainActor internal final class GeometryFieldPreviewStore: ObservableObject { internal struct Request: Equatable, Sendable { @@ -282,8 +271,7 @@ internal final class GeometryFieldPreviewStore: ObservableObject { } internal struct Resolution { - /// The newest preview there is. While `pending` is set it is the previous value's, so the - /// map stays up during a re-read. + // While `pending` is set this is the previous value's preview, so the map stays up during a re-read. let preview: GeometryFieldPreview? let pending: Request? } @@ -291,12 +279,11 @@ internal final class GeometryFieldPreviewStore: ObservableObject { internal typealias Reader = @Sendable (Request, GeometryFieldSource) -> GeometryFieldPreview internal private(set) var preview: GeometryFieldPreview? - /// Moves with the shapes, so the canvas can tell new ones from the ones it already drew. + // Moves with the shapes, so the canvas can tell new ones from the ones it already drew. internal private(set) var token = 0 private var loaded: Request? private var pending: Request? - /// A parse runs to the end once started, so a pane re-added mid-read joins it instead of - /// starting a second copy of the same work. + // A parse runs to the end, so a pane re-added mid-read joins it instead of starting a second one. private var inFlight: (request: Request, task: Task)? private let reader: Reader @@ -317,8 +304,7 @@ internal final class GeometryFieldPreviewStore: ObservableObject { return Resolution(preview: preview, pending: nil) } - /// Nil when the value moved on while this one was being read. A finished read is committed even - /// when its caller was cancelled, so a pane that comes back finds it done. + // Nil when the value moved on during the read. A finished read commits even when its caller was cancelled. internal func load(_ request: Request, source: GeometryFieldSource) async -> GeometryFieldPreview? { if request == loaded { return preview } let task: Task @@ -347,7 +333,7 @@ internal final class GeometryFieldPreviewStore: ObservableObject { } internal extension View { - /// The map pane's well, which shows while the map builds or for a value with nothing to draw. + // Shows while the map builds, or for a value with nothing to draw. func geometryMapWell() -> some View { themeBackground(.editorBackground, native: Color(nsColor: .textBackgroundColor)) .clipShape(RoundedRectangle(cornerRadius: 5)) diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/JsonEditorView.swift b/apple/TablePro/Views/RowInspector/FieldEditors/JsonEditorView.swift index a53c05b691..231f0c5d11 100644 --- a/apple/TablePro/Views/RowInspector/FieldEditors/JsonEditorView.swift +++ b/apple/TablePro/Views/RowInspector/FieldEditors/JsonEditorView.swift @@ -11,8 +11,6 @@ internal struct JsonEditorView: View { var isExpanded = false @State private var model: JsonFieldEditingModel - @AppStorage(PreferenceKeys.rowInspectorJsonFieldHeight.name, store: AppStorageEnvironment.shared.defaults) private var fieldHeight = ResizableFieldMetrics - .defaultJsonHeight init(context: FieldEditorContext, onPopOut: ((String) -> Void)? = nil, isExpanded: Bool = false) { self.context = context @@ -21,9 +19,7 @@ internal struct JsonEditorView: View { self._model = State(wrappedValue: JsonFieldEditingModel(storedValue: context.value.wrappedValue)) } - /// The editor writes through the model, which decides whether the text is worth publishing. - /// Binding `$model.displayText` straight to the editor would publish the model's own - /// corrections back into the store. + // Through the model: binding `$model.displayText` directly would publish the model's own corrections. private var editorText: Binding { Binding( get: { model.displayText }, @@ -36,7 +32,8 @@ internal struct JsonEditorView: View { var body: some View { ResizableEditorContainer( - height: $fieldHeight, + column: context.columnName, + defaultHeight: ResizableFieldMetrics.defaultJsonHeight, range: ResizableFieldMetrics.jsonHeightRange, expandedHeight: isExpanded ? ResizableFieldMetrics.expandedHeight : nil ) { @@ -48,11 +45,7 @@ internal struct JsonEditorView: View { .valueWell(clipsContent: true) .overlay(alignment: .bottomTrailing) { actionButtons } } - /// `newValue`, never a re-read of `context.value.wrappedValue`. An `onChange` action - /// closure belongs to the render that registered it, so its captured context is a render - /// behind and answers with the value this edit has just replaced. Re-reading it made the - /// editor undo every keystroke and push the undone text back, at 75 rounds a second and - /// without ever settling (#3051). + // `newValue`, never `context.value`: the action's context is a render behind and undid every keystroke (#3051). .onChange(of: context.value.wrappedValue) { newValue in _ = model.received(newValue) } diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/MultiLineEditorView.swift b/apple/TablePro/Views/RowInspector/FieldEditors/MultiLineEditorView.swift index ab3058311c..b324f11faf 100644 --- a/apple/TablePro/Views/RowInspector/FieldEditors/MultiLineEditorView.swift +++ b/apple/TablePro/Views/RowInspector/FieldEditors/MultiLineEditorView.swift @@ -12,12 +12,10 @@ internal struct MultiLineEditorView: View { var onPopOut: ((String) -> Void)? var isExpanded = false - @AppStorage(PreferenceKeys.rowInspectorTextFieldHeight.name, store: AppStorageEnvironment.shared.defaults) - private var fieldHeight = ResizableFieldMetrics.defaultTextHeight - var body: some View { ResizableEditorContainer( - height: $fieldHeight, + column: context.columnName, + defaultHeight: ResizableFieldMetrics.defaultTextHeight, range: ResizableFieldMetrics.textHeightRange, expandedHeight: isExpanded ? ResizableFieldMetrics.expandedHeight : nil ) { diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/ResizableEditorContainer.swift b/apple/TablePro/Views/RowInspector/FieldEditors/ResizableEditorContainer.swift index 6438601394..df98ea8847 100644 --- a/apple/TablePro/Views/RowInspector/FieldEditors/ResizableEditorContainer.swift +++ b/apple/TablePro/Views/RowInspector/FieldEditors/ResizableEditorContainer.swift @@ -3,26 +3,43 @@ // TablePro // -import AppKit import SwiftUI internal struct ResizableEditorContainer: View { - @Binding var height: Double + let column: String + let defaultHeight: Double let range: ClosedRange - - /// Set while the field is expanded in place. It wins over the dragged height for as long as it - /// lasts and never overwrites it, so collapsing returns the editor to the size the user chose. + // Wins over the dragged height without overwriting it, so collapsing returns to the user's size. var expandedHeight: Double? - + @ObservedObject var heights: InspectorFieldHeightStore @ViewBuilder let content: () -> Content - @GestureState private var liveDelta: Double = 0 - @State private var isHandleHovered = false + @Environment(\.isEnabled) private var isEnabled + @State private var liveDelta: Double = 0 + + init( + column: String, + defaultHeight: Double, + range: ClosedRange, + expandedHeight: Double? = nil, + heights: InspectorFieldHeightStore = .shared, + @ViewBuilder content: @escaping () -> Content + ) { + self.column = column + self.defaultHeight = defaultHeight + self.range = range + self.expandedHeight = expandedHeight + self.heights = heights + self.content = content + } + + private var baseHeight: Double { + heights.height(for: column) ?? defaultHeight + } private var resolvedHeight: Double { - let dragged = ResizableFieldMetrics.resolve(base: height, delta: liveDelta, range: range) - /// Never below what the user already dragged. The allowed ranges reach 600, so a field - /// enlarged past the expanded height would otherwise shrink when asked to expand. + let dragged = ResizableFieldMetrics.resolve(base: baseHeight, delta: liveDelta, range: range) + // The ranges reach 600, so a field dragged past the expanded height must not shrink on Expand. if let expandedHeight { return max(expandedHeight, dragged) } return dragged } @@ -35,43 +52,24 @@ internal struct ResizableEditorContainer: View { resizeHandle } } + .onChange(of: isEnabled) { enabled in + if !enabled { liveDelta = 0 } + } } private var resizeHandle: some View { - Capsule() - .fill(Color(nsColor: .tertiaryLabelColor)) - .frame(width: 26, height: 4) - .opacity(isHandleHovered ? 1 : 0.5) - .frame(maxWidth: .infinity, minHeight: 11) - .contentShape(Rectangle()) - .gesture( - DragGesture(minimumDistance: 1, coordinateSpace: .global) - .updating($liveDelta) { value, state, _ in - state = value.translation.height - } - .onEnded { value in - height = ResizableFieldMetrics.resolve( - base: height, - delta: value.translation.height, - range: range - ) - } - ) - .onHover { hovering in - guard hovering != isHandleHovered else { return } - isHandleHovered = hovering - if hovering { - NSCursor.resizeUpDown.push() - } else { - NSCursor.pop() - } - } - .onDisappear { - if isHandleHovered { - NSCursor.pop() - isHandleHovered = false + FieldResizeHandle( + onDrag: { liveDelta = $0 }, + onEnd: { delta in + if let height = ResizableFieldMetrics.committedHeight(base: baseHeight, delta: delta, range: range) { + heights.setHeight(height, for: column) } + liveDelta = 0 } - .accessibilityHidden(true) + ) + .frame(maxWidth: .infinity) + .frame(height: 11) + .onDisappear { liveDelta = 0 } + .accessibilityHidden(true) } } diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/ResizableFieldMetrics.swift b/apple/TablePro/Views/RowInspector/FieldEditors/ResizableFieldMetrics.swift index d3da9331b1..00be373360 100644 --- a/apple/TablePro/Views/RowInspector/FieldEditors/ResizableFieldMetrics.swift +++ b/apple/TablePro/Views/RowInspector/FieldEditors/ResizableFieldMetrics.swift @@ -12,16 +12,24 @@ internal enum ResizableFieldMetrics { static let textHeightRange: ClosedRange = 60...600 static let defaultTextHeight: Double = 110 - /// MapKit hides its logo and legal link on a map under 100pt tall, and the HIG asks for both. + // MapKit hides its logo and legal link on a map under 100pt tall, and the HIG asks for both. static let geometryHeightRange: ClosedRange = 100...600 static let defaultGeometryHeight: Double = 160 - /// What a field grows to when it is expanded in place. Tall enough to be worth the gesture and - /// short enough that the fields around it are still on screen, which is the whole difference - /// between this and the full-pane takeover it replaces. + // Short enough that the fields around an expanded one stay on screen, unlike a full-pane takeover. static let expandedHeight: Double = 320 + // The base is clamped first: a geometry map and its text pane share one height under different floors. static func resolve(base: Double, delta: Double, range: ClosedRange) -> Double { - min(max(base + delta, range.lowerBound), range.upperBound) + clamp(clamp(base, to: range) + delta, to: range) + } + + // A drag that ends where it began stores nothing, so the field keeps following its default. + static func committedHeight(base: Double, delta: Double, range: ClosedRange) -> Double? { + delta == 0 ? nil : resolve(base: base, delta: delta, range: range) + } + + private static func clamp(_ value: Double, to range: ClosedRange) -> Double { + min(max(value, range.lowerBound), range.upperBound) } } diff --git a/apple/TablePro/Views/RowInspector/InspectorFieldListView.swift b/apple/TablePro/Views/RowInspector/InspectorFieldListView.swift index 8328ab0a09..c3ec4ce0c4 100644 --- a/apple/TablePro/Views/RowInspector/InspectorFieldListView.swift +++ b/apple/TablePro/Views/RowInspector/InspectorFieldListView.swift @@ -7,12 +7,7 @@ import AppKit import Combine import SwiftUI -/// The inspected row's fields. -/// -/// A `List` and not a `Form`. Measured at the pane's width: `List` is lazy and stays lazy with rows -/// of differing heights, holding 336 subviews and about 135ms to first display whether the row has -/// 100 columns or 2000. `Form(.formStyle(.grouped))` builds every row eagerly, four subviews each, -/// and costs 525ms at 2000. A wide table would have paid that on every selection change. +// A List, not a Form: measured lazy at 336 subviews and ~135ms for 100 or 2000 columns; a grouped Form costs 525ms at 2000. internal struct InspectorFieldListView: View { @ObservedObject internal var editState: MultiRowEditState internal let isEditable: Bool @@ -26,14 +21,11 @@ internal struct InspectorFieldListView: View { @State private var searchText = "" @State private var showsModifiedOnly = false - @State private var expandedFieldID: UUID? @StateObject private var tabStops = InspectorTabStopRegistry() @FocusState private var focusedField: UUID? var body: some View { - /// Resolved once per pass. `editState.fields` is observed, so the body runs on every - /// keystroke in every field, and filtering inside `List` as well as inside the keyboard - /// handlers walked the whole column list several times over for each one. + // Once per pass: the body runs on every keystroke, and filtering per use walked every column several times. let fields = visibleFields return VStack(spacing: 0) { filterBar @@ -45,16 +37,13 @@ internal struct InspectorFieldListView: View { } } .onChange(of: editState.fields.map(\.columnName)) { _ in - expandedFieldID = nil focusedField = nil } } // MARK: - Filtering - /// Always visible rather than bolted above the list on some modes and not others. A row of two - /// hundred columns is unreadable without it, and a control that is the only way to reach a - /// field cannot be conditional. + // Always visible: on a row of two hundred columns it is the only way to reach a field. private var filterBar: some View { HStack(spacing: 6) { NativeSearchField( @@ -76,12 +65,7 @@ internal struct InspectorFieldListView: View { .padding(.vertical, 5) } - /// Whether this field accepts a change at all. - /// - /// One answer, read by the editor, by the value menu and by the keyboard shortcuts alike. A - /// PHP-serialized value cannot be round-tripped without PHP, so the field is presented but never - /// mutated: every Set Value command would overwrite the serialized payload with something the app - /// cannot rebuild. Bytes decoded under a binary format have no writer back either. + // One answer for the editor, the value menu and the shortcuts. PHP-serialized and decoded binary values have no writer back. internal static func isFieldEditable( _ field: FieldEditState, kind: FieldEditorKind, @@ -135,9 +119,7 @@ internal struct InspectorFieldListView: View { )) } } - /// `.plain`, not `.inset`: the inset style spends a hard 16pt per side before any - /// `listRowInsets` is consulted, so the pane's content sat at 24pt while its own header sat - /// at 10. `.plain` lands at half of `intercellSpacing` and can be corrected onto the edge. + // `.plain`, not `.inset`: the inset style spends 16pt per side before `listRowInsets`, which no inset can undo. .listStyle(.plain) .splitItemGlassBackground() .modifier(InspectorFieldKeyShortcuts( @@ -157,13 +139,13 @@ internal struct InspectorFieldListView: View { isModified: field.hasEdit || field.hasCommittedEdit, isPrimaryKey: field.isPrimaryKey, isForeignKey: field.isForeignKey, - isExpanded: expandedFieldID == field.id, + isExpanded: editState.expandedColumn == field.columnIndex, commands: commands, isPendingExpression: field.isPendingExpression, onSetValue: { editState.setValue($0, at: field.columnIndex) }, onRemoveField: offersFieldRemoval ? { editState.removeField(at: field.columnIndex) } : nil, onToggleExpand: FieldEditorContent.canExpand(kind: kind, state: FieldValueState.resolve(field)) - ? { expandedFieldID = expandedFieldID == field.id ? nil : field.id } + ? { editState.toggleExpansion(ofColumn: field.columnIndex) } : nil, onPopOut: { onPopOut?(field, $0, kind) }, onTabStopChange: { [tabStops] in tabStops.record($0, for: field.id) }, @@ -183,11 +165,7 @@ internal struct InspectorFieldListView: View { columnName: field.columnName, columnType: field.columnTypeEnum, isLongText: field.isLongText, - /// The getter reads the store, not the `state` resolved above. That value is a copy - /// taken when this context was built, so a getter closing over it answers whatever the - /// field held during that render, and an `onChange` action, which belongs to the - /// render that registered it, is a render behind again. An editor comparing its text - /// against that answer sees the value it had just replaced and puts it back (#3051). + // Reads the store, not `state`: a copy from this render made editors put back the value just replaced (#3051). value: isEditable ? Binding( get: { editState.currentText(at: columnIndex) }, @@ -207,21 +185,19 @@ internal struct InspectorFieldListView: View { // MARK: - Keyboard - /// Tab is intercepted because the key view loop inside a SwiftUI `List` has one stop, measured. - /// Ignored at either end so the key falls through and focus can leave the list, not wrap. + // A List's key view loop has one stop, measured. Ignored at either end so focus leaves the list instead of wrapping. private func moveFocus(within fields: [FieldEditState], forward: Bool) -> KeyPressResultCompat { guard !fields.isEmpty else { return .ignored } let order = fields.map(\.id) tabStops.forget(allBut: order) - /// With Full Keyboard Access on, the controls of a field showing a map are key views. + // With Full Keyboard Access on, the controls of a field showing a map are key views. let skipped = NSApplication.shared.isFullKeyboardAccessEnabled ? [] : tabStops.fieldsWithoutTabStop let destination = Self.focusDestination(from: focusedField, in: order, forward: forward, skipping: skipped) focusedField = destination return destination == nil ? .ignored : .handled } - /// The field Tab lands on, or nil to let the key leave the list. A field with nothing to focus - /// is passed over, so focus is never sent where no view can take it. + // Nil lets the key leave the list. A field with nothing to focus is passed over. internal static func focusDestination( from current: UUID?, in order: [UUID], @@ -251,8 +227,7 @@ internal struct InspectorFieldListView: View { return .handled } - /// Control-Option-N and Control-Option-D do only what the field's menu offers. Unhandled, the key goes on - /// to the next responder rather than staging a NULL the column refuses. + // Only what the field's menu offers; otherwise the key goes on rather than staging a NULL the column refuses. internal static func stateShortcutCommand(for key: Character, offered: [SetValueCommand]) -> SetValueCommand? { let command: SetValueCommand switch key { @@ -264,8 +239,7 @@ internal struct InspectorFieldListView: View { } } -/// Fields whose editor holds nothing Tab can land on. A reference that publishes nothing, so a -/// field reporting in does not redraw the list. +// Fields with nothing Tab can land on. Publishes nothing, so a field reporting in does not redraw the list. @MainActor internal final class InspectorTabStopRegistry: ObservableObject { internal private(set) var fieldsWithoutTabStop: Set = [] @@ -278,15 +252,13 @@ internal final class InspectorTabStopRegistry: ObservableObject { } } - /// Field ids are reissued on every selection change, so the old ones are dropped. + // Field ids are reissued on every selection change. internal func forget(allBut fieldIDs: [UUID]) { fieldsWithoutTabStop.formIntersection(fieldIDs) } } -/// `onKeyPress` is macOS 14, and `KeyPress` cannot appear outside the check, so the handlers -/// are gated as a pair. Tab still moves focus on macOS 13 through the responder chain; what is -/// lost there is the Control-Option state shortcut, which the field's own menu also offers. +// `onKeyPress` is macOS 14. On 13 Tab still moves focus through the responder chain; the field's menu covers the shortcuts. private struct InspectorFieldKeyShortcuts: ViewModifier { let moveFocus: (Bool) -> KeyPressResultCompat let applyStateShortcut: (Character) -> KeyPressResultCompat @@ -310,7 +282,7 @@ private struct InspectorFieldKeyShortcuts: ViewModifier { } } -/// Mirrors `KeyPress.Result`, which is macOS 14, so the handlers can be declared outside the check. +// Mirrors macOS 14's `KeyPress.Result`, so the handlers can be declared outside the check. internal enum KeyPressResultCompat { case handled case ignored diff --git a/apple/TableProTests/Core/Storage/AppSettingsStorageMigrationTests.swift b/apple/TableProTests/Core/Storage/AppSettingsStorageMigrationTests.swift index f35a5bd28d..2b3771f3ac 100644 --- a/apple/TableProTests/Core/Storage/AppSettingsStorageMigrationTests.swift +++ b/apple/TableProTests/Core/Storage/AppSettingsStorageMigrationTests.swift @@ -2,7 +2,7 @@ // AppSettingsStorageMigrationTests.swift // TableProTests // -// Tests the one-time startup-behavior migration to "Reopen Last Session". +// Tests the startup-behavior migration and the retired inspector field heights. // import Foundation @@ -67,39 +67,38 @@ struct AppSettingsStorageMigrationTests { #expect(storage.loadGeneral().startupBehavior == .showWelcome) } - private let legacyJsonHeightKey = "rightSidebar.jsonFieldHeight" + private let retiredFieldHeightKeys = [ + "rightSidebar.jsonFieldHeight", + "com.TablePro.rightSidebar.jsonFieldHeight", + "com.TablePro.rightSidebar.textFieldHeight", + "com.TablePro.rightSidebar.geometryFieldHeight", + "com.TablePro.settings.didMigrateJsonFieldHeightKey", + ] - @Test("A saved legacy JSON field height moves to the namespaced key") - func migratesLegacyJsonFieldHeight() { + @Test("The per-kind field heights an older build saved are removed") + func removesRetiredFieldHeights() { let (storage, defaults, suite) = makeStorage() defer { defaults.removePersistentDomain(forName: suite) } - defaults.set(320.0, forKey: legacyJsonHeightKey) - storage.migrateJsonFieldHeightKeyIfNeeded() + for key in retiredFieldHeightKeys { + defaults.set(320.0, forKey: key) + } + storage.removeRetiredFieldHeightKeys() - #expect(defaults.double(forKey: PreferenceKeys.rowInspectorJsonFieldHeight.name) == 320.0) - #expect(defaults.object(forKey: legacyJsonHeightKey) == nil) + for key in retiredFieldHeightKeys { + #expect(defaults.object(forKey: key) == nil, "\(key) survived") + } } - @Test("A fresh install writes no JSON field height key") - func skipsJsonFieldHeightOnFreshInstall() { + @Test("Removing the per-kind heights leaves the per-column ones") + func keepsPerColumnFieldHeights() { let (storage, defaults, suite) = makeStorage() defer { defaults.removePersistentDomain(forName: suite) } - storage.migrateJsonFieldHeightKeyIfNeeded() + let saved = Data("[]".utf8) + defaults.set(saved, forKey: PreferenceKeys.rowInspectorFieldHeights.name) + storage.removeRetiredFieldHeightKeys() - #expect(defaults.object(forKey: PreferenceKeys.rowInspectorJsonFieldHeight.name) == nil) - } - - @Test("An existing namespaced JSON field height is not overwritten") - func leavesNamespacedJsonFieldHeight() { - let (storage, defaults, suite) = makeStorage() - defer { defaults.removePersistentDomain(forName: suite) } - - defaults.set(200.0, forKey: PreferenceKeys.rowInspectorJsonFieldHeight.name) - defaults.set(320.0, forKey: legacyJsonHeightKey) - storage.migrateJsonFieldHeightKeyIfNeeded() - - #expect(defaults.double(forKey: PreferenceKeys.rowInspectorJsonFieldHeight.name) == 200.0) + #expect(defaults.data(forKey: PreferenceKeys.rowInspectorFieldHeights.name) == saved) } } diff --git a/apple/TableProTests/Core/Storage/AppSettingsStorageResetTests.swift b/apple/TableProTests/Core/Storage/AppSettingsStorageResetTests.swift index 433da457a9..3a986cf84a 100644 --- a/apple/TableProTests/Core/Storage/AppSettingsStorageResetTests.swift +++ b/apple/TableProTests/Core/Storage/AppSettingsStorageResetTests.swift @@ -16,29 +16,23 @@ struct AppSettingsStorageResetTests { let storage = AppSettingsStorage(userDefaults: defaults) defaults.set("account", forKey: PreferenceKeys.selectedSettingsPane.name) defaults.set("tree", forKey: SidebarPersistenceKey.defaultLayout) - defaults.set(320.0, forKey: PreferenceKeys.rowInspectorJsonFieldHeight.name) storage.resetToDefaults() #expect(defaults.string(forKey: PreferenceKeys.selectedSettingsPane.name) == nil) #expect(defaults.string(forKey: SidebarPersistenceKey.defaultLayout) == nil) - #expect(defaults.object(forKey: PreferenceKeys.rowInspectorJsonFieldHeight.name) == nil) } @Test("Reset clears every remembered inspector field height and the geometry field's mode") func resetClearsInspectorFieldPreferences() throws { let defaults = try pool.make().defaults let storage = AppSettingsStorage(userDefaults: defaults) - defaults.set(320.0, forKey: PreferenceKeys.rowInspectorJsonFieldHeight.name) - defaults.set(240.0, forKey: PreferenceKeys.rowInspectorTextFieldHeight.name) - defaults.set(400.0, forKey: PreferenceKeys.rowInspectorGeometryFieldHeight.name) + defaults.set(Data("[]".utf8), forKey: PreferenceKeys.rowInspectorFieldHeights.name) defaults.set("text", forKey: PreferenceKeys.rowInspectorGeometryFieldMode.name) storage.resetToDefaults() - #expect(defaults.object(forKey: PreferenceKeys.rowInspectorJsonFieldHeight.name) == nil) - #expect(defaults.object(forKey: PreferenceKeys.rowInspectorTextFieldHeight.name) == nil) - #expect(defaults.object(forKey: PreferenceKeys.rowInspectorGeometryFieldHeight.name) == nil) + #expect(defaults.object(forKey: PreferenceKeys.rowInspectorFieldHeights.name) == nil) #expect(defaults.object(forKey: PreferenceKeys.rowInspectorGeometryFieldMode.name) == nil) } } diff --git a/apple/TableProTests/Core/Storage/InspectorFieldHeightStoreTests.swift b/apple/TableProTests/Core/Storage/InspectorFieldHeightStoreTests.swift new file mode 100644 index 0000000000..0bd2edb500 --- /dev/null +++ b/apple/TableProTests/Core/Storage/InspectorFieldHeightStoreTests.swift @@ -0,0 +1,80 @@ +// +// InspectorFieldHeightStoreTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct InspectorFieldHeightStoreTests { + private let scratch: EphemeralDefaults + + init() throws { + scratch = try EphemeralDefaults() + } + + @Test("A height belongs to its column, and other columns keep their default") + func heightIsPerColumn() { + let store = InspectorFieldHeightStore(defaults: scratch.defaults) + + store.setHeight(300, for: "payload") + + #expect(store.height(for: "payload") == 300) + #expect(store.height(for: "metadata") == nil) + } + + @Test("A height survives a relaunch") + func heightSurvivesRelaunch() { + InspectorFieldHeightStore(defaults: scratch.defaults).setHeight(300, for: "payload") + + #expect(InspectorFieldHeightStore(defaults: scratch.defaults).height(for: "payload") == 300) + } + + @Test("Setting a column again replaces its height") + func heightIsReplaced() { + let store = InspectorFieldHeightStore(defaults: scratch.defaults) + + store.setHeight(300, for: "payload") + store.setHeight(180, for: "payload") + + #expect(store.height(for: "payload") == 180) + #expect(InspectorFieldHeightStore(defaults: scratch.defaults).heights == ["payload": 180]) + } + + @Test("Past capacity the least recently set column is dropped") + func evictsLeastRecentlySet() { + let store = InspectorFieldHeightStore(defaults: scratch.defaults) + for index in 0.. NSEvent { + NSEvent.mouseEvent( + with: type, + location: NSPoint(x: 50, y: y), + modifierFlags: [], + timestamp: 0, + windowNumber: window.windowNumber, + context: nil, + eventNumber: 0, + clickCount: 1, + pressure: 1 + )! + } + + private func recordingHandle() -> (FieldResizeHandleView, Recorder) { + let view = FieldResizeHandleView() + let recorder = Recorder() + view.onDrag = { recorder.drags.append($0) } + view.onEnd = { recorder.ends.append($0) } + return (view, recorder) + } + + @Test("Dragging down reports a growing delta, and the release reports the final one") + func reportsDownwardDrag() { + let (view, recorder) = recordingHandle() + + view.mouseDown(with: event(.leftMouseDown, y: 300)) + view.mouseDragged(with: event(.leftMouseDragged, y: 290)) + view.mouseDragged(with: event(.leftMouseDragged, y: 260)) + view.mouseUp(with: event(.leftMouseUp, y: 250)) + + #expect(recorder.drags == [10, 40]) + #expect(recorder.ends == [50]) + } + + @Test("Dragging up reports a negative delta") + func reportsUpwardDrag() { + let (view, recorder) = recordingHandle() + + view.mouseDown(with: event(.leftMouseDown, y: 300)) + view.mouseDragged(with: event(.leftMouseDragged, y: 320)) + view.mouseUp(with: event(.leftMouseUp, y: 330)) + + #expect(recorder.ends == [-30]) + } + + @Test("A click on the grip resizes nothing") + func clickReportsNothing() { + let (view, recorder) = recordingHandle() + + view.mouseDown(with: event(.leftMouseDown, y: 300)) + view.mouseUp(with: event(.leftMouseUp, y: 300)) + + #expect(recorder.drags.isEmpty) + #expect(recorder.ends.isEmpty) + } + + @Test("A drag that did not start on the handle reports nothing") + func ignoresStrayEvents() { + let (view, recorder) = recordingHandle() + + view.mouseDragged(with: event(.leftMouseDragged, y: 200)) + view.mouseUp(with: event(.leftMouseUp, y: 200)) + + #expect(recorder.drags.isEmpty) + #expect(recorder.ends.isEmpty) + } + + @Test("A disabled handle takes no press, no hover and no hit") + func disabledHandleIsInert() { + let (view, recorder) = recordingHandle() + view.frame = NSRect(x: 0, y: 0, width: 100, height: 11) + view.isEnabled = false + + view.mouseEntered(with: event(.mouseMoved, y: 5)) + view.mouseDown(with: event(.leftMouseDown, y: 300)) + view.mouseDragged(with: event(.leftMouseDragged, y: 250)) + view.mouseUp(with: event(.leftMouseUp, y: 250)) + + #expect(view.isHighlighted == false) + #expect(view.hitTest(NSPoint(x: 50, y: 5)) == nil) + #expect(recorder.ends.isEmpty) + } + + @Test("Disabling the handle mid-drag drops the drag") + func disablingCancelsDrag() { + let (view, recorder) = recordingHandle() + + view.mouseDown(with: event(.leftMouseDown, y: 300)) + view.mouseDragged(with: event(.leftMouseDragged, y: 280)) + view.isEnabled = false + view.mouseUp(with: event(.leftMouseUp, y: 250)) + + #expect(recorder.ends.isEmpty) + #expect(view.isHighlighted == false) + } + + @Test("A handle removed mid-drag ends neither the drag nor the hover later") + func removalDropsDragAndHover() { + let (view, recorder) = recordingHandle() + window.contentView?.addSubview(view) + + view.mouseEntered(with: event(.mouseMoved, y: 10)) + view.mouseDown(with: event(.leftMouseDown, y: 300)) + view.mouseDragged(with: event(.leftMouseDragged, y: 280)) + view.removeFromSuperview() + view.mouseUp(with: event(.leftMouseUp, y: 250)) + + #expect(recorder.ends.isEmpty) + #expect(view.isHighlighted == false) + } + + private func mountInList(_ heights: InspectorFieldHeightStore) async throws -> FieldResizeHandleView { + let list = List { + ResizableEditorContainer(column: "payload", defaultHeight: 120, range: 80...600, heights: heights) { + Color.clear + } + } + .listStyle(.plain) + let hosting = NSHostingView(rootView: list) + hosting.frame = NSRect(x: 0, y: 0, width: 300, height: 400) + window.contentView = hosting + try await waitUntil { + hosting.layoutSubtreeIfNeeded() + hosting.displayIfNeeded() + return Self.firstSubview(of: FieldResizeHandleView.self, in: hosting) != nil + } + return try #require(Self.firstSubview(of: FieldResizeHandleView.self, in: hosting)) + } + + private func windowEvent(_ type: NSEvent.EventType, at point: NSPoint) -> NSEvent { + NSEvent.mouseEvent( + with: type, + location: point, + modifierFlags: [], + timestamp: ProcessInfo.processInfo.systemUptime, + windowNumber: window.windowNumber, + context: nil, + eventNumber: 0, + clickCount: 1, + pressure: 1 + )! + } + + // The table took the mouse from a SwiftUI gesture in its row; the press must land on the AppKit view instead. + @Test("Inside a List, a press on the grip lands on the handle view") + func pressInListLandsOnHandle() async throws { + let scratch = try EphemeralDefaults() + let handle = try await mountInList(InspectorFieldHeightStore(defaults: scratch.defaults)) + defer { window.contentView = nil } + + let center = handle.convert(NSPoint(x: handle.bounds.midX, y: handle.bounds.midY), to: nil) + let root = try #require(handle.window?.contentView?.superview) + #expect(root.hitTest(root.convert(center, from: nil)) === handle) + } + + // Driven on the view: an offscreen test window is never key, so `sendEvent` delivers no mouse event. + @Test("Inside a List, a drag released off the handle stores the column's height") + func dragInListStoresHeight() async throws { + let scratch = try EphemeralDefaults() + let heights = InspectorFieldHeightStore(defaults: scratch.defaults) + let handle = try await mountInList(heights) + defer { window.contentView = nil } + + let start = handle.convert(NSPoint(x: handle.bounds.midX, y: handle.bounds.midY), to: nil) + let end = NSPoint(x: start.x + 120, y: start.y - 40) + handle.mouseDown(with: windowEvent(.leftMouseDown, at: start)) + handle.mouseDragged(with: windowEvent(.leftMouseDragged, at: NSPoint(x: start.x, y: start.y - 20))) + handle.mouseDragged(with: windowEvent(.leftMouseDragged, at: end)) + handle.mouseUp(with: windowEvent(.leftMouseUp, at: end)) + + #expect(heights.height(for: "payload") == 160) + #expect(InspectorFieldHeightStore(defaults: scratch.defaults).height(for: "payload") == 160) + } + + private static func firstSubview(of type: View.Type, in view: NSView) -> View? { + for subview in view.subviews { + if let match = subview as? View ?? firstSubview(of: type, in: subview) { return match } + } + return nil + } +} + +private final class Recorder { + var drags: [Double] = [] + var ends: [Double] = [] +} diff --git a/apple/TableProTests/Views/RowInspector/ResizableFieldMetricsTests.swift b/apple/TableProTests/Views/RowInspector/ResizableFieldMetricsTests.swift index 7ed3e341bf..eddb12388a 100644 --- a/apple/TableProTests/Views/RowInspector/ResizableFieldMetricsTests.swift +++ b/apple/TableProTests/Views/RowInspector/ResizableFieldMetricsTests.swift @@ -45,6 +45,19 @@ struct ResizableFieldMetricsTests { #expect(ResizableFieldMetrics.resolve(base: 160, delta: 5_000, range: mapRange) == 600) } + // A text pane dragged to 60 and then shown as a 100pt map: the drag moves it from 100 at once. + @Test func testABaseOutsideTheRangeMovesWithTheFirstPointOfDrag() { + let mapRange = ResizableFieldMetrics.geometryHeightRange + #expect(ResizableFieldMetrics.resolve(base: 60, delta: 30, range: mapRange) == 130) + #expect(ResizableFieldMetrics.resolve(base: 700, delta: -50, range: mapRange) == 550) + } + + @Test func testADragThatEndsWhereItBeganStoresNothing() { + #expect(ResizableFieldMetrics.committedHeight(base: 120, delta: 0, range: range) == nil) + #expect(ResizableFieldMetrics.committedHeight(base: 120, delta: 40, range: range) == 160) + #expect(ResizableFieldMetrics.committedHeight(base: 600, delta: 40, range: range) == 600) + } + @Test func testTextHeightResolvesWithinItsOwnRange() { let textRange = ResizableFieldMetrics.textHeightRange #expect(ResizableFieldMetrics.resolve(base: 110, delta: -500, range: textRange) == textRange.lowerBound) diff --git a/apple/changelog.d/3975.changed.md b/apple/changelog.d/3975.changed.md new file mode 100644 index 0000000000..8637f18f2a --- /dev/null +++ b/apple/changelog.d/3975.changed.md @@ -0,0 +1 @@ +A dragged field in the row inspector keeps its height per column, not for every field of its kind. diff --git a/apple/changelog.d/3975.fixed.1.md b/apple/changelog.d/3975.fixed.1.md new file mode 100644 index 0000000000..b4b852148d --- /dev/null +++ b/apple/changelog.d/3975.fixed.1.md @@ -0,0 +1 @@ +An expanded field in the row inspector collapsed when another row was selected. diff --git a/apple/changelog.d/3975.fixed.md b/apple/changelog.d/3975.fixed.md new file mode 100644 index 0000000000..f0c149de81 --- /dev/null +++ b/apple/changelog.d/3975.fixed.md @@ -0,0 +1 @@ +Dragging a field's resize handle in the row inspector snapped back a few points into the drag. From c1959db8ae93ebbcfac0b3d9808c1ff2963c7088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Nam=20Long?= Date: Sun, 1 Nov 2026 17:32:47 +0700 Subject: [PATCH 82/85] feat(masking): mask column values in the grid, exports, AI and MCP (#3978) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ngô Quốc Đạt --- .../QueryExecutionCoordinator+Batches.swift | 12 +- .../QueryExecutionCoordinator+Helpers.swift | 8 +- ...yExecutionCoordinator+MultiStatement.swift | 6 +- .../Access/DatabaseAccessBridge+Scripts.swift | 8 +- .../Access/DatabaseAccessBridge.swift | 21 ++- .../Database/DatabaseManager+Sessions.swift | 2 + .../Core/MCP/MCPConnectionBridge+Data.swift | 3 +- .../Core/MCP/MCPConnectionBridge.swift | 12 +- .../Plugins/QueryResultExportDataSource.swift | 12 +- .../RoutineDebugController.swift | 3 +- .../Core/Services/Export/ExportService.swift | 13 +- .../Core/Services/Masking/MaskedValue.swift | 49 +++++ .../Masking/MaskingExportDataSource.swift | 167 +++++++++++++++++ .../Core/Services/Masking/MaskingPolicy.swift | 131 ++++++++++++++ .../Core/Services/Masking/MaskingRule.swift | 83 +++++++++ .../Masking/QueryResult+Masking.swift | 51 ++++++ .../Services/Query/ForeignKeyRowFetcher.swift | 4 +- .../DatabaseConnection+Masking.swift | 40 +++++ apple/TablePro/Models/Query/QueryTab.swift | 2 + .../TablePro/Models/Query/ResultOrigin.swift | 3 + apple/TablePro/Models/Query/TableRows.swift | 36 +++- .../TablePro/Resources/Localizable.xcstrings | 36 ++++ .../ViewModels/ServerDashboardViewModel.swift | 7 +- .../ConnectionFormCoordinator.swift | 8 +- .../Panes/OptionsPaneView.swift | 77 ++++++++ .../Support/ConnectionFormEdits.swift | 3 + .../MaskingRulesPaneViewModel.swift | 35 ++++ .../DataGridViewController+Header.swift | 48 +++-- .../Views/Main/Child/DataTabGridAdapter.swift | 10 ++ .../MainContentCoordinator+Masking.swift | 67 +++++++ ...MainContentCoordinator+ResultEditing.swift | 2 + .../Views/Main/MainContentCoordinator.swift | 2 + .../ConnectionBundleExporterTests.swift | 15 ++ .../Services/Masking/MaskingBridgeTests.swift | 111 ++++++++++++ .../Services/Masking/MaskingExportTests.swift | 130 ++++++++++++++ .../Services/Masking/MaskingGridTests.swift | 169 ++++++++++++++++++ .../Services/Masking/MaskingPolicyTests.swift | 143 +++++++++++++++ .../Helpers/ScriptAnsweringDriver.swift | 9 +- .../ViewModels/ConnectionFormEditsTests.swift | 28 ++- apple/changelog.d/3978.added.md | 1 + 40 files changed, 1522 insertions(+), 45 deletions(-) create mode 100644 apple/TablePro/Core/Services/Masking/MaskedValue.swift create mode 100644 apple/TablePro/Core/Services/Masking/MaskingExportDataSource.swift create mode 100644 apple/TablePro/Core/Services/Masking/MaskingPolicy.swift create mode 100644 apple/TablePro/Core/Services/Masking/MaskingRule.swift create mode 100644 apple/TablePro/Core/Services/Masking/QueryResult+Masking.swift create mode 100644 apple/TablePro/Models/Connection/DatabaseConnection+Masking.swift create mode 100644 apple/TablePro/Views/ConnectionForm/ViewModels/MaskingRulesPaneViewModel.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Masking.swift create mode 100644 apple/TableProTests/Core/Services/Masking/MaskingBridgeTests.swift create mode 100644 apple/TableProTests/Core/Services/Masking/MaskingExportTests.swift create mode 100644 apple/TableProTests/Core/Services/Masking/MaskingGridTests.swift create mode 100644 apple/TableProTests/Core/Services/Masking/MaskingPolicyTests.swift create mode 100644 apple/changelog.d/3978.added.md diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift index 923231fcc9..83dbe3eaf0 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift @@ -526,7 +526,9 @@ extension QueryExecutionCoordinator { : [] } else { produced = output.resultSets.enumerated().map { offset, result in - numberedResultSet(result, anchor: entry.batch.anchor, index: firstNumber + resultSets.count + offset) + numberedResultSet( + result, anchor: entry.batch.anchor, index: firstNumber + resultSets.count + offset, tabId: tabId + ) } } produced.last?.serverOutput = output.serverOutput @@ -550,14 +552,16 @@ extension QueryExecutionCoordinator { ) } - private func numberedResultSet(_ result: QueryResult, anchor: StatementAnchor?, index: Int) -> ResultSet { + private func numberedResultSet(_ result: QueryResult, anchor: StatementAnchor?, index: Int, tabId: UUID) -> ResultSet { + let columns = result.columns.map { String($0) } let resultSet = ResultSet( label: ResultSet.label(tableName: nil, anchor: nil, index: index), tableRows: TableRows.from( queryRows: result.rows, - columns: result.columns.map { String($0) }, + columns: columns, columnTypes: result.columnTypes, - absentCells: result.absentCells + absentCells: result.absentCells, + masks: parent.columnMasks(forTabId: tabId, table: nil, columns: columns, columnTypes: result.columnTypes) ) ) resultSet.statementAnchor = anchor diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift index b984726674..c9997c1d6f 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift @@ -234,6 +234,11 @@ extension QueryExecutionCoordinator { tableName: tableName ) let generatedColumns = resolved.generatedColumns + // A Reveal covers the table it was asked on, not the next one this tab opens. + if parent.tabManager.tabs[idx].revealsMaskedValues, parent.tabManager.tabs[idx].tableContext.tableName != tableName { + parent.tabManager.mutate(at: idx) { $0.revealsMaskedValues = false } + } + let masks = parent.columnMasks(forTabId: tabId, table: tableName, columns: columns, columnTypes: columnTypes) let newTableRows = TableRows.from( queryRows: rows, @@ -252,7 +257,8 @@ extension QueryExecutionCoordinator { foreignKeysFetched: resolved.foreignKeysFetched, rowLocators: rowLocators, absentCells: absentCells, - unloadedCells: unloadedCells + unloadedCells: unloadedCells, + masks: masks ) let previousTableName = parent.tabManager.tabs[idx].tableContext.tableName let definitionChanged = parent.tabSessionRegistry.needsDefinition(existingTabId) diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+MultiStatement.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+MultiStatement.swift index 8a47a37c8f..98d014e3f0 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+MultiStatement.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+MultiStatement.swift @@ -45,11 +45,13 @@ extension QueryExecutionCoordinator { anchor: StatementAnchor? = nil ) -> ResultSet { let tableName = parent.extractTableName(from: sql) + let columns = result.columns.map { String($0) } let rows = TableRows.from( queryRows: result.rows, - columns: result.columns.map { String($0) }, + columns: columns, columnTypes: result.columnTypes, - absentCells: result.absentCells + absentCells: result.absentCells, + masks: parent.columnMasks(forTabId: tabId, table: tableName, columns: columns, columnTypes: result.columnTypes) ) let resultSet = ResultSet( label: ResultSet.label(tableName: tableName, anchor: anchor, index: index), diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift index 383ee0663f..727d37b426 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift @@ -66,7 +66,7 @@ extension DatabaseAccessBridge { ) return .statement(outcome.result, executionTimeMs: outcome.executionTimeMs) case .batches(let batches): - return try await runBatches( + let outcome = try await runBatches( batches, of: query, scope: scope, @@ -75,6 +75,12 @@ extension DatabaseAccessBridge { timeoutSeconds: timeoutSeconds, cancellation: cancellation ) + let policy = await maskingPolicy(for: scope.connectionId) + return ScriptOutcome( + resultSets: outcome.resultSets.map { $0.masked(by: policy) }, + primary: outcome.primary.masked(by: policy), + executionTimeMs: outcome.executionTimeMs + ) case .statements: throw DatabaseAccessError.invalidArgument( String(localized: "Update the database driver in Settings > Extensions to run several statements in one call.") diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift index 1420e142da..dc94f2b897 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift @@ -123,6 +123,18 @@ internal actor DatabaseAccessBridge { } } + /// Every row an automation client, MCP or the in-app assistant reads leaves through this bridge, so this is + /// where those rows take the connection's masking rules. + internal func maskingPolicy(for connectionId: UUID) async -> MaskingPolicy { + let manager = await databaseManager + return await MainActor.run { + if let session = manager.session(for: connectionId) { + return session.connection.maskingPolicy + } + return manager.connectionStorage.loadConnection(id: connectionId)?.maskingPolicy ?? .none + } + } + internal func resolveDriver(_ connectionId: UUID) async throws -> (DatabaseDriver, DatabaseType) { let manager = await databaseManager let pending: DatabaseConnection? = await MainActor.run { @@ -204,12 +216,15 @@ internal actor DatabaseAccessBridge { internal let executionTimeMs: Double } + /// `maskingTable` names the table the rows come from when the caller built the query for one, so a + /// `table.column` rule matches by table; without it a rule matches by column name alone. internal func runStatement( scope: DatabaseScope, query: String, maxRows: Int, timeoutSeconds: Int, - cancellation: (any StatementCancellationSignal)? + cancellation: (any StatementCancellationSignal)?, + maskingTable: String? = nil ) async throws -> StatementOutcome { guard !Self.statementText(query, grammar: .ansi).isEmpty else { throw DatabaseAccessError.invalidArgument(String(localized: "The query is empty.")) @@ -293,7 +308,9 @@ internal actor DatabaseAccessBridge { if let tableEdits { CatalogChangeService.post(.statementsSucceeded(tableEdits)) } - return StatementOutcome(result: result, executionTimeMs: (CFAbsoluteTimeGetCurrent() - startTime) * 1_000) + let executionTimeMs = (CFAbsoluteTimeGetCurrent() - startTime) * 1_000 + let masking = await maskingPolicy(for: connectionId) + return StatementOutcome(result: result.masked(by: masking, table: maskingTable), executionTimeMs: executionTimeMs) } internal func runExplainPlan( diff --git a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift index e845a66cf1..7ac777740d 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift @@ -912,6 +912,8 @@ extension DatabaseManager { adopted.color = source.color adopted.iconName = source.iconName adopted.tagIds = source.tagIds + // Masking changes no route, and a rule saved while the window is open must hide values at once. + adopted.maskingRules = source.maskingRules return adopted } diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift index 4f75131f16..f94d9fc86d 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge+Data.swift @@ -147,7 +147,8 @@ extension MCPConnectionBridge { query: sql, maxRows: limit, timeoutSeconds: timeoutSeconds, - cancellation: cancellation + cancellation: cancellation, + maskingTable: request.table ) if case .object(var fields) = payload { fields["table"] = .string(request.table) diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge.swift index 39e4fe410b..dd54dc492c 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge.swift @@ -113,14 +113,16 @@ public actor MCPConnectionBridge { query: String, maxRows: Int, timeoutSeconds: Int, - cancellation: MCPCancellationToken? + cancellation: MCPCancellationToken?, + maskingTable: String? = nil ) async throws -> JsonValue { let outcome = try await runStatement( scope: scope, query: query, maxRows: maxRows, timeoutSeconds: timeoutSeconds, - cancellation: cancellation + cancellation: cancellation, + maskingTable: maskingTable ) return Self.encode( result: outcome.result, @@ -151,14 +153,16 @@ public actor MCPConnectionBridge { query: String, maxRows: Int, timeoutSeconds: Int, - cancellation: MCPCancellationToken? + cancellation: MCPCancellationToken?, + maskingTable: String? = nil ) async throws -> (result: QueryResult, executionTimeMs: Double) { let outcome = try await access.runStatement( scope: scope, query: query, maxRows: maxRows, timeoutSeconds: timeoutSeconds, - cancellation: cancellation + cancellation: cancellation, + maskingTable: maskingTable ) return (outcome.result, outcome.executionTimeMs) } diff --git a/apple/TablePro/Core/Plugins/QueryResultExportDataSource.swift b/apple/TablePro/Core/Plugins/QueryResultExportDataSource.swift index a760427d17..8ae6fafd0c 100644 --- a/apple/TablePro/Core/Plugins/QueryResultExportDataSource.swift +++ b/apple/TablePro/Core/Plugins/QueryResultExportDataSource.swift @@ -64,7 +64,7 @@ final class QueryResultExportDataSource: PluginExportDataSource, @unchecked Send columnTypeNames: Self.columnTypeNames(of: tableRows), rows: tableRows.rows.map { Array($0.values) }, unloadedRows: unloadedRows, - fillUnloaded: fillUnloaded, + fillUnloaded: fillUnloaded.map { Self.masking($0, by: tableRows.masks) }, databaseTypeId: databaseType.rawValue, quoteIdentifier: quoting.quoteIdentifier, escapeStringLiteral: quoting.escapeStringLiteral @@ -85,6 +85,16 @@ final class QueryResultExportDataSource: PluginExportDataSource, @unchecked Send ) } + // A value the page left on the server arrives raw, so it takes the same masks as the rows already held. + private static func masking(_ fill: @escaping UnloadedValueFill, by masks: ColumnMasks) -> UnloadedValueFill { + guard !masks.isEmpty else { return fill } + return { rows in + try await fill(rows).mapValues { cells in + Dictionary(uniqueKeysWithValues: cells.map { ($0.key, masks.value($0.value, column: $0.key)) }) + } + } + } + private static func columnTypeNames(of tableRows: TableRows) -> [String] { tableRows.columnTypes.map { $0.rawType ?? "" } } diff --git a/apple/TablePro/Core/RoutineDebugging/RoutineDebugController.swift b/apple/TablePro/Core/RoutineDebugging/RoutineDebugController.swift index 9393c0b321..41f98644ee 100644 --- a/apple/TablePro/Core/RoutineDebugging/RoutineDebugController.swift +++ b/apple/TablePro/Core/RoutineDebugging/RoutineDebugController.swift @@ -722,7 +722,8 @@ internal final class RoutineDebugController: ObservableObject { break } if case .finished(let result) = final { - resultGrid = result.flatMap { Self.makeResultGrid($0, databaseType: databaseType) } + let policy = DatabaseManager.shared.session(for: connectionId)?.connection.maskingPolicy ?? .none + resultGrid = result.flatMap { Self.makeResultGrid($0.masked(by: policy), databaseType: databaseType) } } hasEnded = true setState(final) diff --git a/apple/TablePro/Core/Services/Export/ExportService.swift b/apple/TablePro/Core/Services/Export/ExportService.swift index 408f425972..fbcf13983b 100644 --- a/apple/TablePro/Core/Services/Export/ExportService.swift +++ b/apple/TablePro/Core/Services/Export/ExportService.swift @@ -250,7 +250,7 @@ final class ExportService: ObservableObject { do { result = try await plugin.export( tables: pluginTables, - dataSource: dataSource, + dataSource: MaskingExportDataSource.wrapping(dataSource, policy: maskingPolicy(for: driver)), destination: url, progress: progress ) @@ -267,6 +267,13 @@ final class ExportService: ObservableObject { state.notes = result.notes } + // MARK: - Masking + + // The live session's copy, so a rule saved after the connection opened applies to this export. + private func maskingPolicy(for driver: DatabaseDriver) -> MaskingPolicy { + (DatabaseManager.shared.session(for: driver.connection.id)?.connection ?? driver.connection).maskingPolicy + } + // MARK: - Statement Timeout func suppressStatementTimeout(on driver: DatabaseDriver) async { @@ -433,7 +440,9 @@ final class ExportService: ObservableObject { do { result = try await plugin.export( tables: [exportTable], - dataSource: dataSource, + dataSource: MaskingExportDataSource.wrapping( + dataSource, policy: maskingPolicy(for: driver), streamsTables: false + ), destination: url, progress: progress ) diff --git a/apple/TablePro/Core/Services/Masking/MaskedValue.swift b/apple/TablePro/Core/Services/Masking/MaskedValue.swift new file mode 100644 index 0000000000..c66f293c2a --- /dev/null +++ b/apple/TablePro/Core/Services/Masking/MaskedValue.swift @@ -0,0 +1,49 @@ +// +// MaskedValue.swift +// TablePro +// + +import CryptoKit +import Foundation +import TableProPluginKit + +enum MaskedValue { + static let bullets = "••••" + + // Keyed per launch, so equal values hash alike everywhere in this run but a hash cannot be looked up in a table. + private static let hashKey = SymmetricKey(size: .bits256) + + static func mask(_ value: PluginCellValue, style: MaskingStyle) -> PluginCellValue { + if style == .null { return .null } + switch value { + case .null: + return .null + case .text(let text): + return .text(mask(text: text, style: style)) + case .bytes(let data): + return .text(style == .hash ? hash(data) : bullets) + } + } + + private static func mask(text: String, style: MaskingStyle) -> String { + switch style { + case .full, .null: + return bullets + case .lastFour: + guard text.count > 4 else { return bullets } + return bullets + String(text.suffix(4)) + case .email: + guard let at = text.lastIndex(of: "@"), at != text.startIndex else { return bullets } + let domain = text[text.index(after: at)...] + guard !domain.isEmpty else { return bullets } + return String(text[text.startIndex]) + "•••@" + domain + case .hash: + return hash(Data(text.utf8)) + } + } + + private static func hash(_ data: Data) -> String { + let code = HMAC.authenticationCode(for: data, using: hashKey) + return code.prefix(6).map { String(format: "%02x", $0) }.joined() + } +} diff --git a/apple/TablePro/Core/Services/Masking/MaskingExportDataSource.swift b/apple/TablePro/Core/Services/Masking/MaskingExportDataSource.swift new file mode 100644 index 0000000000..ee711d05e0 --- /dev/null +++ b/apple/TablePro/Core/Services/Masking/MaskingExportDataSource.swift @@ -0,0 +1,167 @@ +// +// MaskingExportDataSource.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +/// Stands between an export format and a data source that reads the database, so every row a format +/// receives, by stream or by its own query, has the connection's masking rules applied. +final class MaskingExportDataSource: PluginExportDataSource, @unchecked Sendable { + private let base: any PluginExportDataSource + private let policy: MaskingPolicy + private let streamsTables: Bool + + /// `streamsTables` is false when the export table is a query result named after its output file, so a + /// `table.column` rule matches by column name instead of by that name. + static func wrapping( + _ base: any PluginExportDataSource, + policy: MaskingPolicy, + streamsTables: Bool = true + ) -> any PluginExportDataSource { + policy.isEmpty ? base : MaskingExportDataSource(base: base, policy: policy, streamsTables: streamsTables) + } + + init(base: any PluginExportDataSource, policy: MaskingPolicy, streamsTables: Bool = true) { + self.base = base + self.policy = policy + self.streamsTables = streamsTables + } + + var databaseTypeId: String { base.databaseTypeId } + var tableDDLIncludesForeignKeys: Bool { base.tableDDLIncludesForeignKeys } + var lexicalFeatures: SQLLexicalFeatures { base.lexicalFeatures } + var supportsCascadeDrop: Bool { base.supportsCascadeDrop } + + func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { + Self.masking(base.streamRows(table: table, databaseName: databaseName), table: sourceTable(table), policy: policy) + } + + func streamRows(for object: PluginExportTable) -> AsyncThrowingStream { + Self.masking(base.streamRows(for: object), table: sourceTable(object.name), policy: policy) + } + + private func sourceTable(_ name: String) -> String? { + streamsTables ? name : nil + } + + func execute(query: String) async throws -> PluginQueryResult { + try await base.execute(query: query).masked(by: policy) + } + + static func masking( + _ stream: AsyncThrowingStream, + table: String?, + policy: MaskingPolicy + ) -> AsyncThrowingStream { + AsyncThrowingStream { continuation in + let task = Task { + var masks: ColumnMasks? + do { + for try await element in stream { + switch element { + case .header(let header): + masks = policy.columnMasks(table: table, columns: header.columns, typeNames: header.columnTypeNames) + continuation.yield(element) + case .rows(let rows): + // Without a header there are no column names to match, so no row goes out unmatched. + guard let masks else { + throw PluginExportError.exportFailed("Rows arrived before their columns") + } + continuation.yield(.rows(masks.rows(rows))) + } + } + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + continuation.onTermination = { _ in task.cancel() } + } + } + + func fetchTableDDL(table: String, databaseName: String) async throws -> String { + try await base.fetchTableDDL(table: table, databaseName: databaseName) + } + + func quoteIdentifier(_ identifier: String) -> String { + base.quoteIdentifier(identifier) + } + + func escapeStringLiteral(_ value: String) -> String { + base.escapeStringLiteral(value) + } + + func fetchApproximateRowCount(table: String, databaseName: String) async throws -> Int? { + try await base.fetchApproximateRowCount(table: table, databaseName: databaseName) + } + + func fetchDependentSequences(table: String, databaseName: String) async throws -> [PluginSequenceInfo] { + try await base.fetchDependentSequences(table: table, databaseName: databaseName) + } + + func fetchDependentTypes(table: String, databaseName: String) async throws -> [PluginEnumTypeInfo] { + try await base.fetchDependentTypes(table: table, databaseName: databaseName) + } + + func fetchColumns(table: String, databaseName: String) async throws -> [PluginColumnInfo] { + try await base.fetchColumns(table: table, databaseName: databaseName) + } + + func fetchAllColumns(databaseName: String) async throws -> [String: [PluginColumnInfo]] { + try await base.fetchAllColumns(databaseName: databaseName) + } + + func fetchForeignKeys(table: String, databaseName: String) async throws -> [PluginForeignKeyInfo] { + try await base.fetchForeignKeys(table: table, databaseName: databaseName) + } + + func fetchAllForeignKeys(databaseName: String) async throws -> [String: [PluginForeignKeyInfo]] { + try await base.fetchAllForeignKeys(databaseName: databaseName) + } + + func fetchIndexDDL(table: String, databaseName: String) async throws -> [String] { + try await base.fetchIndexDDL(table: table, databaseName: databaseName) + } + + func fetchCommentDDL(table: String, databaseName: String) async throws -> [String] { + try await base.fetchCommentDDL(table: table, databaseName: databaseName) + } + + func fetchObjectDDL(_ object: PluginExportTable) async throws -> String { + try await base.fetchObjectDDL(object) + } + + func scriptText(for ddl: String) -> String { + base.scriptText(for: ddl) + } + + func fetchGrantStatements(principal: String, host: String?) async throws -> [String] { + try await base.fetchGrantStatements(principal: principal, host: host) + } + + func dropStatement(for object: PluginExportTable) -> String? { + base.dropStatement(for: object) + } + + func openSessionScope(for purpose: PluginSessionPurpose) async throws -> PluginSessionScope? { + try await base.openSessionScope(for: purpose) + } + + func closeSessionScope(_ scope: PluginSessionScope) async throws { + try await base.closeSessionScope(scope) + } + + func fetchCreationScope(_ object: PluginExportTable) async throws -> PluginStatementScope? { + try await base.fetchCreationScope(object) + } + + func fetchIndexes(table: String, databaseName: String) async throws -> [PluginIndexInfo] { + try await base.fetchIndexes(table: table, databaseName: databaseName) + } + + func fetchObjectComment(_ object: PluginExportTable) async throws -> String? { + try await base.fetchObjectComment(object) + } +} diff --git a/apple/TablePro/Core/Services/Masking/MaskingPolicy.swift b/apple/TablePro/Core/Services/Masking/MaskingPolicy.swift new file mode 100644 index 0000000000..629a6432bc --- /dev/null +++ b/apple/TablePro/Core/Services/Masking/MaskingPolicy.swift @@ -0,0 +1,131 @@ +// +// MaskingPolicy.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +/// The rules one connection masks its values by, and the only place they are matched against columns. +struct MaskingPolicy: Equatable, Sendable { + let rules: [MaskingRule] + + static let none = MaskingPolicy(rules: []) + + init(rules: [MaskingRule]) { + self.rules = rules.filter { !$0.trimmedPattern.isEmpty } + } + + var isEmpty: Bool { rules.isEmpty } + + /// The first rule that matches wins. Without a table, as for most query results, a `table.column` + /// pattern matches by its column part alone, so an unknown source masks more rather than less. + func style(table: String?, column: String, typeName: String?) -> MaskingStyle? { + for rule in rules where Self.matches(rule, table: table, column: column, typeName: typeName) { + return rule.style + } + return nil + } + + func columnMasks(table: String?, columns: [String], typeNames: [String?]) -> ColumnMasks { + guard !isEmpty else { return .none } + var styles: [Int: MaskingStyle] = [:] + for (index, column) in columns.enumerated() { + let typeName = typeNames.indices.contains(index) ? typeNames[index] : nil + if let style = style(table: table, column: column, typeName: typeName) { + styles[index] = style + } + } + return ColumnMasks(styles: styles) + } + + func columnMasks(table: String?, columns: [String], columnTypes: [ColumnType]) -> ColumnMasks { + columnMasks(table: table, columns: columns, typeNames: columnTypes.map(\.rawType)) + } + + private static func matches(_ rule: MaskingRule, table: String?, column: String, typeName: String?) -> Bool { + let pattern = rule.trimmedPattern + switch rule.target { + case .type: + guard let typeName else { return false } + return MaskingGlob.matches(pattern, typeName) + case .column: + guard let dot = pattern.lastIndex(of: ".") else { + return MaskingGlob.matches(pattern, column) + } + let tablePattern = String(pattern[.. Bool { + let pattern = Array(pattern.lowercased()) + let text = Array(text.lowercased()) + var patternIndex = 0 + var textIndex = 0 + var starIndex: Int? + var resumeIndex = 0 + while textIndex < text.count { + if patternIndex < pattern.count, pattern[patternIndex] == "?" || pattern[patternIndex] == text[textIndex] { + patternIndex += 1 + textIndex += 1 + } else if patternIndex < pattern.count, pattern[patternIndex] == "*" { + starIndex = patternIndex + resumeIndex = textIndex + patternIndex += 1 + } else if let star = starIndex { + patternIndex = star + 1 + resumeIndex += 1 + textIndex = resumeIndex + } else { + return false + } + } + while patternIndex < pattern.count, pattern[patternIndex] == "*" { + patternIndex += 1 + } + return patternIndex == pattern.count + } +} + +/// Which columns of one result are masked, and how. Every surface masks rows through this. +struct ColumnMasks: Equatable, Sendable { + let styles: [Int: MaskingStyle] + + static let none = ColumnMasks(styles: [:]) + + var isEmpty: Bool { styles.isEmpty } + + var maskedColumns: Set { Set(styles.keys) } + + func isMasked(_ column: Int) -> Bool { + styles[column] != nil + } + + func value(_ value: PluginCellValue, column: Int) -> PluginCellValue { + guard let style = styles[column] else { return value } + return MaskedValue.mask(value, style: style) + } + + func row(_ values: [PluginCellValue]) -> [PluginCellValue] { + guard !isEmpty else { return values } + var masked = values + for (column, style) in styles where column < masked.count { + masked[column] = MaskedValue.mask(masked[column], style: style) + } + return masked + } + + func rows(_ rows: [[PluginCellValue]]) -> [[PluginCellValue]] { + guard !isEmpty else { return rows } + return rows.map(row) + } +} diff --git a/apple/TablePro/Core/Services/Masking/MaskingRule.swift b/apple/TablePro/Core/Services/Masking/MaskingRule.swift new file mode 100644 index 0000000000..53008bbf4a --- /dev/null +++ b/apple/TablePro/Core/Services/Masking/MaskingRule.swift @@ -0,0 +1,83 @@ +// +// MaskingRule.swift +// TablePro +// + +import Foundation + +enum MaskingStyle: String, Codable, CaseIterable, Identifiable, Sendable { + case full + case lastFour + case email + case hash + case null + + var id: String { rawValue } + + var displayName: String { + switch self { + case .full: return String(localized: "Full") + case .lastFour: return String(localized: "Last 4 Characters") + case .email: return String(localized: "Email") + case .hash: return String(localized: "Stable Hash") + case .null: return "NULL" + } + } +} + +enum MaskingTarget: String, Codable, CaseIterable, Identifiable, Sendable { + case column + case type + + var id: String { rawValue } + + var displayName: String { + switch self { + case .column: return String(localized: "Column") + case .type: return String(localized: "Type") + } + } +} + +struct MaskingRule: Codable, Hashable, Identifiable, Sendable { + var id: UUID + var pattern: String + var target: MaskingTarget + var style: MaskingStyle + + init(id: UUID = UUID(), pattern: String, target: MaskingTarget = .column, style: MaskingStyle = .full) { + self.id = id + self.pattern = pattern + self.target = target + self.style = style + } + + private enum CodingKeys: String, CodingKey { + case id, pattern, target, style + } + + /// What a rule this release cannot read in full becomes, so a style, target or field a newer release added + /// never shows a value it meant to hide. + static let maskEverything = MaskingRule(pattern: "*") + + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + id = try container.decodeIfPresent(UUID.self, forKey: .id) ?? UUID() + let pattern = try container.decodeIfPresent(String.self, forKey: .pattern) + let target = try container.decodeIfPresent(String.self, forKey: .target).map { MaskingTarget(rawValue: $0) } + let style = try container.decodeIfPresent(String.self, forKey: .style).map { MaskingStyle(rawValue: $0) } + guard let pattern, target != .some(nil) else { + self.pattern = Self.maskEverything.pattern + self.target = .column + self.style = .full + return + } + self.pattern = pattern + self.target = target.flatMap { $0 } ?? .column + self.style = style.flatMap { $0 } ?? .full + } + + var trimmedPattern: String { + pattern.trimmingCharacters(in: .whitespacesAndNewlines) + } +} diff --git a/apple/TablePro/Core/Services/Masking/QueryResult+Masking.swift b/apple/TablePro/Core/Services/Masking/QueryResult+Masking.swift new file mode 100644 index 0000000000..b65b52f9b2 --- /dev/null +++ b/apple/TablePro/Core/Services/Masking/QueryResult+Masking.swift @@ -0,0 +1,51 @@ +// +// QueryResult+Masking.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +extension QueryResult { + func masked(by policy: MaskingPolicy, table: String? = nil) -> QueryResult { + let masks = policy.columnMasks(table: table, columns: columns, columnTypes: columnTypes) + guard !masks.isEmpty else { return self } + var masked = QueryResult( + columns: columns, + columnTypes: columnTypes, + rows: masks.rows(rows), + rowsAffected: rowsAffected, + executionTime: executionTime, + error: error + ) + masked.timing = timing + masked.isTruncated = isTruncated + masked.statusMessage = statusMessage + masked.columnMeta = columnMeta + masked.rowLocators = rowLocators + masked.serverOutput = serverOutput + masked.absentCells = absentCells + masked.unloadedCells = unloadedCells + return masked + } +} + +extension PluginQueryResult { + func masked(by policy: MaskingPolicy, table: String? = nil) -> PluginQueryResult { + let masks = policy.columnMasks(table: table, columns: columns, typeNames: columnTypeNames) + guard !masks.isEmpty else { return self } + var masked = PluginQueryResult( + columns: columns, + columnTypeNames: columnTypeNames, + rows: masks.rows(rows), + rowsAffected: rowsAffected, + timing: timing, + isTruncated: isTruncated, + statusMessage: statusMessage, + columnMeta: columnMeta + ) + masked.rowLocators = rowLocators + masked.absentCells = absentCells + return masked + } +} diff --git a/apple/TablePro/Core/Services/Query/ForeignKeyRowFetcher.swift b/apple/TablePro/Core/Services/Query/ForeignKeyRowFetcher.swift index d50ab3f9b1..000eb266a3 100644 --- a/apple/TablePro/Core/Services/Query/ForeignKeyRowFetcher.swift +++ b/apple/TablePro/Core/Services/Query/ForeignKeyRowFetcher.swift @@ -68,7 +68,9 @@ enum ForeignKeyRowFetcher { ) return try await driver.execute(query: query) } - guard let firstRow = result.rows.first else { return nil } + let policy = DatabaseManager.shared.session(for: origin.connectionId)?.connection.maskingPolicy ?? .none + let masked = result.masked(by: policy, table: reference.referencedTable) + guard let firstRow = masked.rows.first else { return nil } let foreignKeys = includeForeignKeys ? await referencedTableForeignKeys(target: target, reference: reference) diff --git a/apple/TablePro/Models/Connection/DatabaseConnection+Masking.swift b/apple/TablePro/Models/Connection/DatabaseConnection+Masking.swift new file mode 100644 index 0000000000..1170c04b6d --- /dev/null +++ b/apple/TablePro/Models/Connection/DatabaseConnection+Masking.swift @@ -0,0 +1,40 @@ +// +// DatabaseConnection+Masking.swift +// TablePro +// + +import Foundation +import os + +extension DatabaseConnection { + // Kept in the additional fields, which iCloud sync and a published Team Catalog entry already carry. + static let maskingRulesKey = "maskingRules" + + private static let maskingLogger = Logger(subsystem: "com.TablePro", category: "Masking") + + var maskingRules: [MaskingRule] { + get { + guard let json = additionalFields[Self.maskingRulesKey], !json.isEmpty else { return [] } + do { + return try JSONDecoder().decode([MaskingRule].self, from: Data(json.utf8)) + } catch { + Self.maskingLogger.error("Masking rules could not be read: \(error.publicLogShape, privacy: .public)") + return [.maskEverything] + } + } + set { + let kept = newValue.filter { !$0.trimmedPattern.isEmpty } + guard !kept.isEmpty, + let data = try? JSONEncoder().encode(kept), + let json = String(data: data, encoding: .utf8) else { + additionalFields.removeValue(forKey: Self.maskingRulesKey) + return + } + additionalFields[Self.maskingRulesKey] = json + } + } + + var maskingPolicy: MaskingPolicy { + MaskingPolicy(rules: maskingRules) + } +} diff --git a/apple/TablePro/Models/Query/QueryTab.swift b/apple/TablePro/Models/Query/QueryTab.swift index 072966f0b8..c4fe61aa97 100644 --- a/apple/TablePro/Models/Query/QueryTab.swift +++ b/apple/TablePro/Models/Query/QueryTab.swift @@ -64,6 +64,8 @@ struct QueryTab: Identifiable, Equatable { // On the tab, not the grid: JSON mode, the inspector and the Edit menu's row commands read the order unmounted (#2251). var valueFilter: GridValueFilterState var sessionHighlightRules: [HighlightRule] = [] + // Set only by Reveal, after the user authenticated; never saved, so a reopened tab masks again. + var revealsMaskedValues = false var pagination: PaginationState var chartConfiguration: ResultChartConfiguration var mapConfiguration: ResultMapConfiguration diff --git a/apple/TablePro/Models/Query/ResultOrigin.swift b/apple/TablePro/Models/Query/ResultOrigin.swift index d7891faccd..ef3dfd03cb 100644 --- a/apple/TablePro/Models/Query/ResultOrigin.swift +++ b/apple/TablePro/Models/Query/ResultOrigin.swift @@ -63,6 +63,7 @@ enum ResultEditRefusal: Equatable { case view(PluginViewReadOnlyReason?) case databaseMoved case keysUnresolved + case masked var message: String { switch self { @@ -74,6 +75,8 @@ enum ResultEditRefusal: Equatable { return String(localized: "These rows came from another database than the one this tab is now using.") case .keysUnresolved: return String(localized: "TablePro has not looked up this table's key columns, so it cannot tell which row an edit would change.") + case .masked: + return String(localized: "These rows show masked values, which cannot be written back. Reveal them to edit.") } } diff --git a/apple/TablePro/Models/Query/TableRows.swift b/apple/TablePro/Models/Query/TableRows.swift index b09235c46c..5ffb2f607e 100644 --- a/apple/TablePro/Models/Query/TableRows.swift +++ b/apple/TablePro/Models/Query/TableRows.swift @@ -34,6 +34,13 @@ struct TableRows: Sendable { /// filter or a removal cannot pair a row with another row's document. Only a driver that edits /// whole documents fills it, and a row with none cannot be edited that way. private(set) var rowLocators: [RowID: String] = [:] + /// The masking rules matched to these columns. Every fetched value is written through it, and a + /// masked column never takes an edit, so no reader can see the stored value. + private(set) var masks: ColumnMasks = .none + + var maskedColumnNames: Set { + Set(masks.maskedColumns.compactMap { columns.indices.contains($0) ? columns[$0] : nil }) + } init( rows: ContiguousArray = [], @@ -145,6 +152,22 @@ struct TableRows: Sendable { rows.contains { $0.hasUnloadedValues } } + /// Masks the held values of every column `incoming` adds. A column already masked keeps its mask, since the + /// value under it is gone until the next fetch. + @discardableResult + mutating func addMasks(_ incoming: ColumnMasks) -> Delta { + let added = incoming.styles.filter { column, _ in column < columns.count && !masks.isMasked(column) } + guard !added.isEmpty else { return .none } + let addedMasks = ColumnMasks(styles: added) + for index in rows.indices { + for column in added.keys where column < rows[index].values.count { + rows[index].values[column] = addedMasks.value(rows[index].values[column], column: column) + } + } + masks = ColumnMasks(styles: masks.styles.merging(added) { current, _ in current }) + return .fullReplace + } + // A cell an edit or an earlier load already filled keeps what it holds. @discardableResult mutating func fillUnloaded(_ values: [RowID: [Int: PluginCellValue]]) -> Delta { @@ -153,7 +176,7 @@ struct TableRows: Sendable { guard let row = indexByID[id] else { continue } for (column, value) in columns where rows[row].unloadedColumns[column] != nil { guard column < rows[row].values.count else { continue } - rows[row].values[column] = value + rows[row].values[column] = masks.value(value, column: column) rows[row].unloadedColumns.removeValue(forKey: column) changed.insert(CellPosition(row: row, column: column)) } @@ -208,6 +231,7 @@ struct TableRows: Sendable { } private mutating func write(_ value: PluginCellValue, isAbsent: Bool, row: Int, column: Int) -> Bool { + guard !masks.isMasked(column) else { return false } let wasAbsent = rows[row].isAbsent(column) let wasUnloaded = rows[row].unloadedColumns.removeValue(forKey: column) != nil guard rows[row].values[column] != value || wasAbsent != isAbsent || wasUnloaded else { return false } @@ -264,7 +288,7 @@ struct TableRows: Sendable { rows.reserveCapacity(rows.count + pageRows.count) indexByID.reserveCapacity(indexByID.count + pageRows.count) for (idx, values) in pageRows.enumerated() { - let normalized = Self.normalize(values: values, toCount: columns.count) + let normalized = Self.normalize(values: masks.row(values), toCount: columns.count) let row = Row( id: .existing(offset + idx), values: normalized, @@ -319,7 +343,7 @@ struct TableRows: Sendable { var rebuiltIndex = [RowID: Int]() rebuiltIndex.reserveCapacity(replacementRows.count) for (idx, values) in replacementRows.enumerated() { - let normalized = Self.normalize(values: values, toCount: columns.count) + let normalized = Self.normalize(values: masks.row(values), toCount: columns.count) let row = Row( id: .existing(offset + idx), values: normalized, @@ -417,12 +441,13 @@ struct TableRows: Sendable { foreignKeysFetched: Bool = false, rowLocators: [String?]? = nil, absentCells: [Int: Set] = [:], - unloadedCells: [Int: [Int: Int]] = [:] + unloadedCells: [Int: [Int: Int]] = [:], + masks: ColumnMasks = .none ) -> TableRows { var rows = ContiguousArray() rows.reserveCapacity(queryRows.count) for (index, values) in queryRows.enumerated() { - let normalized = normalize(values: values, toCount: columns.count) + let normalized = normalize(values: masks.row(values), toCount: columns.count) rows.append(Row( id: .existing(index), values: normalized, @@ -447,6 +472,7 @@ struct TableRows: Sendable { foreignKeysFetched: foreignKeysFetched ) tableRows.rowLocators = locators(rowLocators, rowCount: queryRows.count, offset: 0) + tableRows.masks = masks return tableRows } diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 608d5cb7fb..892f55b355 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -85020,6 +85020,9 @@ } } } + }, + "Full" : { + }, "FULL (rewrites the whole object, blocks access)" : { "extractionState" : "manual", @@ -88699,6 +88702,9 @@ } } } + }, + "Hides matching values in the grid, copies, exports, the AI assistant and MCP. A pattern is table.column or column, with * as a wildcard, such as *.email. Filters and sorts still run on the stored values, so masking does not replace database permissions." : { + }, "High" : { "localizations" : { @@ -99587,6 +99593,9 @@ } } } + }, + "Last 4 Characters" : { + }, "Last 4 Weeks" : { "localizations" : { @@ -106551,6 +106560,21 @@ }, "Marked for deletion. Undo or Undo Delete brings it back." : { + }, + "Mask" : { + + }, + "masked" : { + + }, + "Masked" : { + + }, + "Masked by a masking rule" : { + + }, + "Masking" : { + }, "Match" : { "localizations" : { @@ -150867,6 +150891,9 @@ }, "Reuse Result For" : { + }, + "Reveal Masked Values…" : { + }, "Reveal token" : { "localizations" : { @@ -164682,6 +164709,9 @@ } } } + }, + "show masked values" : { + }, "SHOW needs TOPICS, BROKERS, GROUPS or CLUSTER." : { "extractionState" : "manual", @@ -171900,6 +171930,9 @@ } } } + }, + "Stable Hash" : { + }, "Stack" : { "extractionState" : "manual", @@ -194159,6 +194192,9 @@ } } } + }, + "These rows show masked values, which cannot be written back. Reveal them to edit." : { + }, "These two engines cannot share a script." : { "localizations" : { diff --git a/apple/TablePro/ViewModels/ServerDashboardViewModel.swift b/apple/TablePro/ViewModels/ServerDashboardViewModel.swift index 417728c431..32d2b89271 100644 --- a/apple/TablePro/ViewModels/ServerDashboardViewModel.swift +++ b/apple/TablePro/ViewModels/ServerDashboardViewModel.swift @@ -185,10 +185,13 @@ final class ServerDashboardViewModel: ObservableObject { private static func tileExecutor(connectionId: UUID, services: AppServices) -> DashboardTileBoard.Execute { { [connectionId, services] query in let driver = try liveDriver(connectionId: connectionId, services: services) + let policy = await MainActor.run { + services.databaseManager.session(for: connectionId)?.connection.maskingPolicy ?? .none + } if let bounded = try await driver.executeBoundedQuery(query: query, rowCap: DashboardTileTable.rowCap) { - return bounded + return bounded.masked(by: policy) } - return try await driver.executeRead(query: query) + return try await driver.executeRead(query: query).masked(by: policy) } } diff --git a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift index 0972a6182e..4658ab5212 100644 --- a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift +++ b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift @@ -42,6 +42,7 @@ final class ConnectionFormCoordinator: ObservableObject { @Published var customization: CustomizationPaneViewModel @Published var advanced: AdvancedPaneViewModel @Published var aiRules: AIRulesPaneViewModel + @Published var maskingRules: MaskingRulesPaneViewModel @Published var selectedTab: ConnectionFormTab = .general @Published var hasLoadedData: Bool = false @@ -129,6 +130,7 @@ final class ConnectionFormCoordinator: ObservableObject { self.customization = CustomizationPaneViewModel() self.advanced = AdvancedPaneViewModel() self.aiRules = AIRulesPaneViewModel() + self.maskingRules = MaskingRulesPaneViewModel() let ref = WeakCoordinatorRef(self) network.coordinator = ref @@ -143,6 +145,7 @@ final class ConnectionFormCoordinator: ObservableObject { customization.coordinator = ref advanced.coordinator = ref aiRules.coordinator = ref + maskingRules.coordinator = ref childChangeForwarding = forwardChildChanges() } @@ -167,6 +170,7 @@ final class ConnectionFormCoordinator: ObservableObject { Self.changes(of: $customization), Self.changes(of: $advanced), Self.changes(of: $aiRules), + Self.changes(of: $maskingRules), ]) .sink { [weak self] in self?.objectWillChange.send() } } @@ -231,6 +235,7 @@ final class ConnectionFormCoordinator: ObservableObject { customization.load(from: existing) advanced.load(from: existing) aiRules.load(from: existing) + maskingRules.load(from: existing) normalizeTransport() } hasLoadedData = true @@ -371,7 +376,8 @@ final class ConnectionFormCoordinator: ObservableObject { ? nil : advanced.startupCommands, localOnly: advanced.localOnly, additionalFields: fields, - ownedAdditionalFieldIDs: ownedAdditionalFieldIDs() + ownedAdditionalFieldIDs: ownedAdditionalFieldIDs(), + maskingRules: maskingRules.savedRules ) } diff --git a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift index 61336bfd6d..a148c976f0 100644 --- a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift +++ b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift @@ -32,6 +32,7 @@ struct OptionsPaneView: View { preConnectSection postDisconnectSection safetySection + MaskingRulesSection(model: coordinator.maskingRules) if aiIsEnabled { aiRulesSection } @@ -434,3 +435,79 @@ struct OptionsPaneView: View { } } } + +/// Columns whose values TablePro hides, for this connection, in the grid, exports, the assistant and MCP. +private struct MaskingRulesSection: View { + @ObservedObject var model: MaskingRulesPaneViewModel + + var body: some View { + Section { + ForEach(model.rules) { rule in + MaskingRuleRow(rule: rule, onChange: model.update, onRemove: { model.removeRule(id: rule.id) }) + } + Button(String(localized: "Add Rule")) { + model.addRule() + } + .accessibilityIdentifier("connection-form-add-masking-rule") + } header: { + Text(String(localized: "Masking")) + } footer: { + Text(String(localized: """ + Hides matching values in the grid, copies, exports, the AI assistant and MCP. \ + A pattern is table.column or column, with * as a wildcard, such as *.email. \ + Filters and sorts still run on the stored values, so masking does not replace database permissions. + """)) + .font(.caption) + .foregroundStyle(.secondary) + } + } +} + +private struct MaskingRuleRow: View { + let rule: MaskingRule + let onChange: (MaskingRule) -> Void + let onRemove: () -> Void + + var body: some View { + HStack(spacing: 8) { + Picker(String(localized: "Match"), selection: binding(\.target)) { + ForEach(MaskingTarget.allCases) { target in + Text(target.displayName).tag(target) + } + } + .labelsHidden() + .fixedSize() + TextField( + String(localized: "Pattern"), + text: binding(\.pattern), + prompt: Text(verbatim: rule.target == .column ? "*.email" : "uuid") + ) + .labelsHidden() + .accessibilityIdentifier("connection-form-masking-pattern") + Picker(String(localized: "Mask"), selection: binding(\.style)) { + ForEach(MaskingStyle.allCases) { style in + Text(style.displayName).tag(style) + } + } + .labelsHidden() + .fixedSize() + Button(action: onRemove) { + Image(systemName: "minus.circle") + } + .buttonStyle(.borderless) + .help(String(localized: "Remove Rule")) + .accessibilityLabel(String(localized: "Remove Rule")) + } + } + + private func binding(_ keyPath: WritableKeyPath) -> Binding { + Binding( + get: { rule[keyPath: keyPath] }, + set: { newValue in + var updated = rule + updated[keyPath: keyPath] = newValue + onChange(updated) + } + ) + } +} diff --git a/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift b/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift index 6a31fc0e64..21fcb4ee5f 100644 --- a/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift +++ b/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift @@ -40,6 +40,7 @@ struct ConnectionFormEdits: Equatable { var localOnly: Bool var additionalFields: [String: String] var ownedAdditionalFieldIDs: Set + var maskingRules: [MaskingRule] = [] static let appManagedAdditionalFieldIDs: Set = [ "preConnectScript", @@ -50,6 +51,7 @@ struct ConnectionFormEdits: Equatable { DatabaseConnection.queryTimeoutSecondsKey, DatabaseConnection.binaryInlineLimitKilobytesKey, DatabaseConnection.completionIdentifierCaseKey, + DatabaseConnection.maskingRulesKey, DatabaseConnection.sshForwardUnixSocketPathKey, MySQLLocalSocket.fieldKey ] @@ -87,6 +89,7 @@ struct ConnectionFormEdits: Equatable { result.queryTimeoutSeconds = queryTimeoutSeconds result.binaryInlineLimitKilobytes = binaryInlineLimitKilobytes result.completionIdentifierCase = completionIdentifierCase + result.maskingRules = maskingRules return result } diff --git a/apple/TablePro/Views/ConnectionForm/ViewModels/MaskingRulesPaneViewModel.swift b/apple/TablePro/Views/ConnectionForm/ViewModels/MaskingRulesPaneViewModel.swift new file mode 100644 index 0000000000..f09d004a4d --- /dev/null +++ b/apple/TablePro/Views/ConnectionForm/ViewModels/MaskingRulesPaneViewModel.swift @@ -0,0 +1,35 @@ +// +// MaskingRulesPaneViewModel.swift +// TablePro +// + +import Combine +import Foundation + +@MainActor +final class MaskingRulesPaneViewModel: ObservableObject { + @Published var rules: [MaskingRule] = [] + + @Published var coordinator: WeakCoordinatorRef? + + func load(from connection: DatabaseConnection) { + rules = connection.maskingRules + } + + func addRule() { + rules.append(MaskingRule(pattern: "")) + } + + func removeRule(id: UUID) { + rules.removeAll { $0.id == id } + } + + func update(_ rule: MaskingRule) { + guard let index = rules.firstIndex(where: { $0.id == rule.id }) else { return } + rules[index] = rule + } + + var savedRules: [MaskingRule] { + rules.filter { !$0.trimmedPattern.isEmpty } + } +} diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+Header.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+Header.swift index ea37295a25..f97f1afd0d 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+Header.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+Header.swift @@ -127,6 +127,7 @@ extension DataGridViewController { func refreshHeaders() { let columns = model.columns let filter = model.store.valueFilter + let masks = model.rows.masks let sortEntries = presentedSortEntries() let showsComments = AppSettingsManager.shared.general.showObjectComments var sortIndex: [DataGridColumnID: (direction: SortDirection, priority: Int)] = [:] @@ -137,14 +138,17 @@ extension DataGridViewController { for tableColumn in pane.tableView.tableColumns { guard let id = pane.columnID(of: tableColumn), let column = columns.column(id: id) else { continue } let sort = sortIndex[id] + let isMasked = masks.isMasked(column.dataIndex) applyHeaderTitle( to: tableColumn, column: column, - isFiltered: model.configuration.supportsValueFilter && filter.isActive(column: column.dataIndex) + isFiltered: model.configuration.supportsValueFilter && filter.isActive(column: column.dataIndex), + isMasked: isMasked ) let tooltip = headerToolTip( for: column, showsComment: showsComments, + isMasked: isMasked, sort: sort, sortKeyCount: sortEntries.count ) @@ -152,7 +156,7 @@ extension DataGridViewController { tableColumn.headerToolTip = tooltip } let cell = tableColumn.headerCell - let label = headerAccessibilityLabel(for: column, showsComment: showsComments) + let label = headerAccessibilityLabel(for: column, showsComment: showsComments, isMasked: isMasked) if cell.accessibilityLabel() != label { cell.setAccessibilityLabel(label) } @@ -163,9 +167,9 @@ extension DataGridViewController { } } - private func applyHeaderTitle(to tableColumn: NSTableColumn, column: DataGridColumn, isFiltered: Bool) { + private func applyHeaderTitle(to tableColumn: NSTableColumn, column: DataGridColumn, isFiltered: Bool, isMasked: Bool) { let cell = tableColumn.headerCell - guard isFiltered else { + guard isFiltered || isMasked else { if cell.stringValue != column.name { tableColumn.title = column.name } @@ -179,10 +183,21 @@ extension DataGridViewController { .font: font, .paragraphStyle: paragraph, ] - let title = NSMutableAttributedString(string: column.name + " ", attributes: attributes) - if let funnel = Self.filterFunnel(for: font, color: model.themeScope.resolved[.gridHeaderText]) { - let attachment = NSAttributedString(attachment: funnel) - let marked = NSMutableAttributedString(attributedString: attachment) + let title = NSMutableAttributedString(string: column.name, attributes: attributes) + let color = model.themeScope.resolved[.gridHeaderText] + var symbols: [(name: String, description: String)] = [] + if isMasked { + symbols.append(("eye.slash", String(localized: "Masked"))) + } + if isFiltered { + symbols.append(("line.3.horizontal.decrease.circle.fill", String(localized: "Filtered"))) + } + for symbol in symbols { + guard let image = Self.headerSymbol(symbol.name, description: symbol.description, font: font, color: color) else { + continue + } + title.append(NSAttributedString(string: " ", attributes: attributes)) + let marked = NSMutableAttributedString(attributedString: NSAttributedString(attachment: image)) marked.addAttributes(attributes, range: NSRange(location: 0, length: marked.length)) title.append(marked) } @@ -190,12 +205,12 @@ extension DataGridViewController { } // A symbol image keeps the color it was built with, so it takes the header text color here. - private static func filterFunnel(for font: NSFont, color: NSColor) -> NSTextAttachment? { + private static func headerSymbol(_ name: String, description: String, font: NSFont, color: NSColor) -> NSTextAttachment? { let configuration = NSImage.SymbolConfiguration(pointSize: font.pointSize, weight: .regular) .applying(.init(hierarchicalColor: color)) guard let image = NSImage( - systemSymbolName: "line.3.horizontal.decrease.circle.fill", - accessibilityDescription: String(localized: "Filtered") + systemSymbolName: name, + accessibilityDescription: description )?.withSymbolConfiguration(configuration) else { return nil } let attachment = NSTextAttachment() attachment.image = image @@ -207,6 +222,7 @@ extension DataGridViewController { private func headerToolTip( for column: DataGridColumn, showsComment: Bool, + isMasked: Bool, sort: (direction: SortDirection, priority: Int)?, sortKeyCount: Int ) -> String { @@ -219,6 +235,9 @@ extension DataGridViewController { if showsComment, let comment = Self.displayableComment(column.comment) { tooltip += "\n" + comment } + if isMasked { + tooltip += "\n" + String(localized: "Masked by a masking rule") + } if let sort, sortKeyCount > 1 { tooltip += "\n" + String( format: String(localized: "Sort key %d of %d, %@"), @@ -230,8 +249,11 @@ extension DataGridViewController { return tooltip } - private func headerAccessibilityLabel(for column: DataGridColumn, showsComment: Bool) -> String { - let label = String(format: String(localized: "Column: %@"), column.name) + private func headerAccessibilityLabel(for column: DataGridColumn, showsComment: Bool, isMasked: Bool) -> String { + var label = String(format: String(localized: "Column: %@"), column.name) + if isMasked { + label += ", " + String(localized: "masked") + } guard showsComment, let comment = Self.displayableComment(column.comment) else { return label } return label + ", " + comment } diff --git a/apple/TablePro/Views/Main/Child/DataTabGridAdapter.swift b/apple/TablePro/Views/Main/Child/DataTabGridAdapter.swift index fc724a2854..e6da4abdb8 100644 --- a/apple/TablePro/Views/Main/Child/DataTabGridAdapter.swift +++ b/apple/TablePro/Views/Main/Child/DataTabGridAdapter.swift @@ -198,6 +198,16 @@ final class DataTabGridAdapter: DataGridDataSource, DataGridActions { // MARK: - Menus + func gridColumnMenuItems(for column: DataGridColumn) -> [NSMenuItem] { + guard gridRows.masks.isMasked(column.dataIndex) else { return [] } + let tabId = tabId + return [ClosureMenuTarget.item(title: String(localized: "Reveal Masked Values…")) { [weak coordinator] in + Task { @MainActor [weak coordinator] in + await coordinator?.revealMaskedValues(tabId: tabId) + } + }] + } + func gridRowMenuItems(for rowIDs: [RowID], clickedRow: RowID) -> [NSMenuItem] { guard let coordinator, ownsWindowState, coordinator.canInsertDocument else { return [] } var items: [NSMenuItem] = [] diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Masking.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Masking.swift new file mode 100644 index 0000000000..902472bf92 --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Masking.swift @@ -0,0 +1,67 @@ +// +// MainContentCoordinator+Masking.swift +// TablePro +// + +import Combine +import Foundation + +extension MainContentCoordinator { + var maskingPolicy: MaskingPolicy { + (services.databaseManager.session(for: connectionId)?.connection ?? connection).maskingPolicy + } + + func columnMasks(forTabId tabId: UUID, table: String?, columns: [String], columnTypes: [ColumnType]) -> ColumnMasks { + if tabManager.tabs.first(where: { $0.id == tabId })?.revealsMaskedValues == true { return .none } + return maskingPolicy.columnMasks(table: table, columns: columns, columnTypes: columnTypes) + } + + func observeMaskingRuleChanges() -> AnyCancellable { + services.appEvents.connectionUpdated + .filter { [connectionId] changed in changed == nil || changed == connectionId } + .receive(on: RunLoop.main) + .sink { [weak self] _ in self?.maskLoadedResults() } + } + + /// Masks the rows already on screen when a rule is saved, rather than waiting for the next run. Read from storage, + /// since the live session may adopt the saved record after this runs. + func maskLoadedResults() { + guard let stored = services.databaseManager.connectionStorage.loadConnection(id: connectionId) else { return } + let policy = stored.maskingPolicy + guard !policy.isEmpty else { return } + for tab in tabManager.tabs where !tab.revealsMaskedValues { + let table = tab.tabType == .table ? tab.tableContext.tableName : nil + mutateActiveTableRows(for: tab.id) { rows in + rows.addMasks(policy.columnMasks(table: table, columns: rows.columns, columnTypes: rows.columnTypes)) + } + for resultSet in tab.display.resultSets { + let rows = resultSet.tableRows + resultSet.tableRows.addMasks(policy.columnMasks(table: nil, columns: rows.columns, columnTypes: rows.columnTypes)) + } + } + } + + // LAContext shows this as "TablePro is trying to .", so it stays a lowercase phrase with no period. + static var revealMaskedValuesReason: String { + String(localized: "show masked values") + } + + func revealMaskedValues( + tabId: UUID, + authenticator: any OperationAuthenticating = BiometricOperationAuthenticating() + ) async { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), !tab.revealsMaskedValues else { return } + guard await authenticator.authenticate(reason: Self.revealMaskedValuesReason) else { return } + tabManager.mutate(tabId: tabId) { $0.revealsMaskedValues = true } + guard tabManager.selectedTabId == tabId else { return } + switch tab.tabType { + case .table: + runQuery(viewport: .keepPlace) + default: + // Running a query tab again could repeat a write, so only a read reruns; anything else shows on its next run. + let classification = QueryClassifier.classify(tab.content.query, databaseType: connection.type) + guard classification.tier == .safe else { return } + runQuery(viewport: .keepPlace) + } + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultEditing.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultEditing.swift index 309f70044e..42a1a7eec2 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultEditing.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultEditing.swift @@ -11,6 +11,8 @@ extension MainContentCoordinator { // origin, fail-closed (#2243). A table tab has one result and only a view there explains a refusal. var activeResultEditRefusal: ResultEditRefusal? { guard let tab = tabManager.selectedTab else { return nil } + // A masked value can neither be written back nor find its row, and a copied row would insert the mask. + if !tabSessionRegistry.tableRows(for: tab.id).masks.isEmpty { return .masked } guard tab.tabType == .query else { return tab.tableContext.viewEditRefusal } return ResultEditability.refusal( for: tab.display.activeResultSet?.origin, diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index 5c95425d6b..941e288852 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -338,6 +338,7 @@ final class MainContentCoordinator: ObservableObject { private var schemaSwitchCancellable: AnyCancellable? private var clearedEditsCancellable: AnyCancellable? private var gridRepaintCancellable: AnyCancellable? + var maskingRulesCancellable: AnyCancellable? private var browseContainerCancellable: AnyCancellable? @Published var fileConflictRequest: FileConflictRequest? @@ -670,6 +671,7 @@ final class MainContentCoordinator: ObservableObject { self.rowEditingCoordinator = RowEditingCoordinator(parent: self) clearedEditsCancellable = resumeWhenEditsClear() gridRepaintCancellable = observeChangeManagerForGridRepaint() + maskingRulesCancellable = observeMaskingRuleChanges() Self.lifecycleLogger.info( "[open] MainContentCoordinator.init done connId=\(connection.id, privacy: .public) elapsedMs=\(Int(Date().timeIntervalSince(initStart) * 1_000))" diff --git a/apple/TableProTests/Core/Services/Export/ConnectionBundleExporterTests.swift b/apple/TableProTests/Core/Services/Export/ConnectionBundleExporterTests.swift index 8229063982..8b2e0be54a 100644 --- a/apple/TableProTests/Core/Services/Export/ConnectionBundleExporterTests.swift +++ b/apple/TableProTests/Core/Services/Export/ConnectionBundleExporterTests.swift @@ -180,6 +180,21 @@ struct ConnectionBundleExporterTests { #expect(try ConnectionBundleCodec.decode(plain).connections.count == 1) } + @Test("A connection published to the Team Catalog carries its masking rules") + func connectionsOnlyBundleCarriesMaskingRules() throws { + let library = try ImportLibraryFixture() + defer { library.cleanUp() } + var exported = makeConnection(name: "Orders") + exported.maskingRules = [MaskingRule(pattern: "*.email", style: .email)] + + let bundle = try library.exporter.connectionsOnlyBundle(for: [exported]) + + let fields = try #require(bundle.connections.first?.settings.additionalFields) + var published = DatabaseConnection(name: "Published") + published.additionalFields = fields + #expect(published.maskingRules == exported.maskingRules) + } + @Test("The Team Catalog bundle never carries saved queries or credentials") func connectionsOnlyBundleCarriesNoQueries() async throws { let library = try ImportLibraryFixture() diff --git a/apple/TableProTests/Core/Services/Masking/MaskingBridgeTests.swift b/apple/TableProTests/Core/Services/Masking/MaskingBridgeTests.swift new file mode 100644 index 0000000000..deb7d89f5b --- /dev/null +++ b/apple/TableProTests/Core/Services/Masking/MaskingBridgeTests.swift @@ -0,0 +1,111 @@ +// +// MaskingBridgeTests.swift +// TableProTests +// +// MCP tools, the AI chat tools, AppleScript and Shortcuts all read rows through DatabaseAccessBridge. +// + +import Foundation +@testable import TablePro +import TableProMCP +import TableProPluginKit +import Testing + +@Suite(.serialized) +@MainActor +struct MaskingBridgeTests { + nonisolated private static func customers() -> QueryResult { + ScriptAnsweringDriver.resultSet( + columns: ["id", "email"], + rows: [["1", "jane@example.com"], ["2", "john@example.com"]] + ) + } + + private func install(type: DatabaseType = .mysql, sendsBatchesWhole: Bool = true) -> (ScriptAnsweringDriver, DatabaseScope) { + var connection = TestFixtures.makeConnection(database: "shop", type: type) + connection.maskingRules = [MaskingRule(pattern: "*.email")] + let driver = ScriptAnsweringDriver( + connection: connection, + sendsBatchesWhole: sendsBatchesWhole, + answer: { _ in ScriptAnsweringDriver.batch([Self.customers(), Self.customers()]) }, + statementAnswer: { _ in Self.customers() } + ) + var session = ConnectionSession(connection: connection) + session.driver = driver + DatabaseManager.shared.injectSession(session, for: connection.id) + return (driver, DatabaseScope(connectionId: connection.id, database: "shop", schema: nil)) + } + + private static func leaks(_ result: QueryResult) -> Bool { + result.rows.flatMap { $0 }.contains { $0.asText?.contains("@example.com") == true } + } + + @Test("A statement read for MCP, the assistant or a script comes back masked") + func statementIsMasked() async throws { + let (driver, scope) = install() + defer { DatabaseManager.shared.removeSession(for: driver.connection.id) } + + let outcome = try await DatabaseAccessBridge().runStatement( + scope: scope, query: "SELECT id, email FROM customers", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + #expect(!Self.leaks(outcome.result)) + #expect(outcome.result.rows.map { $0[0] } == [.text("1"), .text("2")]) + } + + @Test("Every result set of a script comes back masked") + func scriptIsMasked() async throws { + let (driver, scope) = install(type: .mssql) + defer { DatabaseManager.shared.removeSession(for: driver.connection.id) } + + let outcome = try await DatabaseAccessBridge().runScript( + scope: scope, + query: "SELECT * FROM customers\nGO\nSELECT * FROM customers", + maxRows: 10, + timeoutSeconds: 30, + cancellation: nil + ) + + #expect(!outcome.resultSets.isEmpty) + #expect(!outcome.resultSets.contains(where: Self.leaks)) + #expect(!Self.leaks(outcome.primary)) + } + + @Test("What an MCP client receives as JSON holds no masked value") + func mcpPayloadIsMasked() async throws { + let (driver, scope) = install() + defer { DatabaseManager.shared.removeSession(for: driver.connection.id) } + + let payload = try await MCPConnectionBridge().executeQuery( + scope: scope, query: "SELECT id, email FROM customers", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + let json = payload.jsonString(prettyPrinted: false) + #expect(!json.contains("@example.com")) + #expect(json.contains("••••")) + } + + @Test("A table.column rule matches by table when the caller names the table") + func maskingTableNarrowsTheMatch() async throws { + var connection = TestFixtures.makeConnection(database: "shop") + connection.maskingRules = [MaskingRule(pattern: "users.email")] + let driver = ScriptAnsweringDriver(connection: connection, statementAnswer: { _ in Self.customers() }) + var session = ConnectionSession(connection: connection) + session.driver = driver + DatabaseManager.shared.injectSession(session, for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: nil) + let bridge = DatabaseAccessBridge() + + let other = try await bridge.runStatement( + scope: scope, query: "SELECT * FROM customers", maxRows: 10, timeoutSeconds: 30, cancellation: nil, + maskingTable: "customers" + ) + let unnamed = try await bridge.runStatement( + scope: scope, query: "SELECT * FROM customers", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + #expect(Self.leaks(other.result)) + #expect(!Self.leaks(unnamed.result)) + } +} diff --git a/apple/TableProTests/Core/Services/Masking/MaskingExportTests.swift b/apple/TableProTests/Core/Services/Masking/MaskingExportTests.swift new file mode 100644 index 0000000000..6d2ff85d69 --- /dev/null +++ b/apple/TableProTests/Core/Services/Masking/MaskingExportTests.swift @@ -0,0 +1,130 @@ +// +// MaskingExportTests.swift +// TableProTests +// +// File > Export and Export Results hand rows to a format plugin. A masked column must reach no format raw. +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private final class FixedRowsDataSource: PluginExportDataSource, @unchecked Sendable { + let databaseTypeId = "mysql" + private let elements: [PluginStreamElement] + + init(_ elements: [PluginStreamElement]) { + self.elements = elements + } + + static let customers = FixedRowsDataSource([ + .header(PluginStreamHeader(columns: ["id", "email"], columnTypeNames: ["int", "varchar"])), + .rows([[.text("1"), .text("jane@example.com")], [.text("2"), .text("john@example.com")]]), + ]) + + func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { + let elements = elements + return AsyncThrowingStream { continuation in + for element in elements { continuation.yield(element) } + continuation.finish() + } + } + + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult( + columns: ["email"], + columnTypeNames: ["varchar"], + rows: [[.text("jane@example.com")]], + rowsAffected: 0, + executionTime: 0 + ) + } + + func fetchTableDDL(table: String, databaseName: String) async throws -> String { "" } + func quoteIdentifier(_ identifier: String) -> String { identifier } + func escapeStringLiteral(_ value: String) -> String { value } + func fetchApproximateRowCount(table: String, databaseName: String) async throws -> Int? { 2 } +} + +struct MaskingExportTests { + private let policy = MaskingPolicy(rules: [MaskingRule(pattern: "*.email")]) + + private static func collect(_ stream: AsyncThrowingStream) async throws -> [PluginRow] { + var rows: [PluginRow] = [] + for try await element in stream { + if case .rows(let chunk) = element { rows += chunk } + } + return rows + } + + @Test("File > Export streams a masked column masked, by table and by object") + func streamedRowsAreMasked() async throws { + let source = MaskingExportDataSource.wrapping(FixedRowsDataSource.customers, policy: policy) + + let byTable = try await Self.collect(source.streamRows(table: "customers", databaseName: "shop")) + let object = PluginExportTable(name: "customers", databaseName: "shop", tableType: "table") + let byObject = try await Self.collect(source.streamRows(for: object)) + + for rows in [byTable, byObject] { + #expect(rows == [[.text("1"), .text("••••")], [.text("2"), .text("••••")]]) + } + } + + @Test("A query export named after its file matches a table.column rule by column, not by the file name") + func queryExportIgnoresTheFileName() async throws { + let usersOnly = MaskingPolicy(rules: [MaskingRule(pattern: "users.email")]) + let source = MaskingExportDataSource.wrapping(FixedRowsDataSource.customers, policy: usersOnly, streamsTables: false) + + let rows = try await Self.collect(source.streamRows(table: "results", databaseName: "")) + + #expect(rows.allSatisfy { $0[1] == .text("••••") }) + } + + @Test("A format's own query comes back masked") + func executedRowsAreMasked() async throws { + let source = MaskingExportDataSource.wrapping(FixedRowsDataSource.customers, policy: policy) + let result = try await source.execute(query: "SELECT email FROM customers") + #expect(result.rows == [[.text("••••")]]) + } + + @Test("No rule leaves the data source unwrapped") + func noRuleIsUnwrapped() { + let base = FixedRowsDataSource.customers + #expect(MaskingExportDataSource.wrapping(base, policy: .none) === base) + } + + @Test("Rows that arrive before their header are refused rather than let through") + func rowsBeforeHeaderAreRefused() async { + let headless = FixedRowsDataSource([.rows([[.text("jane@example.com")]])]) + let source = MaskingExportDataSource.wrapping(headless, policy: policy) + await #expect(throws: (any Error).self) { + _ = try await Self.collect(source.streamRows(table: "customers", databaseName: "shop")) + } + } + + @Test("Export Results writes the grid's masked rows and masks a value it loads from the server") + @MainActor + func exportResultsMasksLoadedValues() async throws { + let masks = policy.columnMasks(table: "customers", columns: ["id", "email"], typeNames: [nil, nil]) + let tableRows = TableRows.from( + queryRows: [[.text("1"), .text("jane@example.com")], [.text("2"), .null]], + columns: ["id", "email"], + columnTypes: [.integer(rawType: nil), .text(rawType: nil)], + unloadedCells: [1: [1: 4_000]], + masks: masks + ) + let source = QueryResultExportDataSource( + tableRows: tableRows, + databaseType: .mysql, + driver: nil, + fillUnloaded: { rows in + Dictionary(uniqueKeysWithValues: rows.map { ($0.id, [1: PluginCellValue.text("john@example.com")]) }) + } + ) + + let rows = try await Self.collect(source.streamRows(table: "customers", databaseName: "")) + + #expect(rows == [[.text("1"), .text("••••")], [.text("2"), .text("••••")]]) + } +} diff --git a/apple/TableProTests/Core/Services/Masking/MaskingGridTests.swift b/apple/TableProTests/Core/Services/Masking/MaskingGridTests.swift new file mode 100644 index 0000000000..d01f5b3132 --- /dev/null +++ b/apple/TableProTests/Core/Services/Masking/MaskingGridTests.swift @@ -0,0 +1,169 @@ +// +// MaskingGridTests.swift +// TableProTests +// +// The grid, Copy and Copy As, the row inspector, charts and the assistant's result summary all read the tab's +// TableRows, so a masked value never written there cannot reach any of them. +// + +import AppKit +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private let rawEmails: Set = ["jane@example.com", "john@example.com", "ann@example.com", "bob@example.com"] + +private func leaks(_ rows: TableRows) -> Bool { + rows.rows.contains { row in row.values.contains { $0.asText.map(rawEmails.contains) == true } } +} + +struct MaskingTableRowsTests { + private let masks = MaskingPolicy(rules: [MaskingRule(pattern: "*.email")]) + .columnMasks(table: "users", columns: ["id", "email"], typeNames: [nil, nil]) + + @Test("Every write of fetched rows masks: the first page, a reload, a page appended and a value loaded later") + func everyFetchedWriteIsMasked() { + var rows = TableRows.from( + queryRows: [[.text("1"), .text("jane@example.com")], [.text("2"), .null]], + columns: ["id", "email"], + columnTypes: [.integer(rawType: nil), .text(rawType: nil)], + unloadedCells: [1: [1: 4_000]], + masks: masks + ) + #expect(!leaks(rows)) + + rows.fillUnloaded([.existing(1): [1: .text("john@example.com")]]) + #expect(!leaks(rows)) + #expect(rows.value(at: 1, column: 1) == .text("••••")) + + rows.appendPage([[.text("3"), .text("ann@example.com")]], startingAt: 2) + #expect(!leaks(rows)) + + rows.replace(rows: [[.text("4"), .text("bob@example.com")]]) + #expect(!leaks(rows)) + #expect(rows.value(at: 0, column: 0) == .text("4")) + } + + @Test("A rule saved while rows are loaded masks them in place and keeps an existing mask") + func addedMasksApplyToLoadedRows() { + var rows = TableRows.from( + queryRows: [[.text("1"), .text("jane@example.com")]], + columns: ["id", "email"], + columnTypes: [.integer(rawType: nil), .text(rawType: nil)] + ) + #expect(rows.addMasks(masks) == .fullReplace) + #expect(!leaks(rows)) + #expect(rows.masks.isMasked(1)) + + let restyled = ColumnMasks(styles: [1: .hash]) + #expect(rows.addMasks(restyled) == .none) + #expect(rows.value(at: 0, column: 1) == .text("••••")) + } + + @Test("A masked cell takes no edit, so a mask is never staged as a new value") + func maskedCellRefusesEdits() { + var rows = TableRows.from( + queryRows: [[.text("1"), .text("jane@example.com")]], + columns: ["id", "email"], + columnTypes: [.integer(rawType: nil), .text(rawType: nil)], + masks: masks + ) + #expect(rows.edit(row: 0, column: 1, value: .text("new@example.com")) == .none) + #expect(rows.value(at: 0, column: 1) == .text("••••")) + #expect(rows.edit(row: 0, column: 0, value: .text("9")) != .none) + #expect(rows.maskedColumnNames == ["email"]) + } +} + +extension GlobalStateSuites { + @Suite(.recordingClipboard) + @MainActor + struct MaskingGridTests { + private let fixture: CoordinatorFixture = { + var connection = TestFixtures.makeConnection(database: "shop") + connection.maskingRules = [MaskingRule(pattern: "*.email", style: .email)] + return CoordinatorFixture(connection: connection) + }() + + private func loadUsers() throws -> QueryTab { + let tab = fixture.addTab(QueryTab(title: "users", query: "SELECT * FROM users", tabType: .table, tableName: "users")) + fixture.coordinator.applyPhase1Result( + tabId: tab.id, + columns: ["id", "email"], + columnTypes: [.integer(rawType: "int"), .text(rawType: "varchar")], + rows: [[.text("1"), .text("jane@example.com")], [.text("2"), .text("john@example.com")]], + executionTime: 0, + rowsAffected: 0, + statusMessage: nil, + tableName: "users", + isEditable: true, + metadata: nil, + hasSchema: false, + read: TableFreshness.Read(startedAt: .now, includesDefinition: false), + sql: "SELECT * FROM users", + connection: fixture.connection + ) + return try #require(fixture.tabManager.tabs.first { $0.id == tab.id }) + } + + @Test("A fetched result lands in the grid masked") + func fetchedRowsAreMasked() throws { + let tab = try loadUsers() + let rows = fixture.coordinator.tabSessionRegistry.tableRows(for: tab.id) + #expect(!leaks(rows)) + #expect(rows.value(at: 0, column: 1) == .text("j•••@example.com")) + #expect(fixture.coordinator.gridModel(forTab: tab.id).rows.masks.isMasked(1)) + } + + @Test("Copy and Copy as JSON copy the mask") + func copiesAreMasked() throws { + let clipboard = try #require(RecordingClipboard.current) + _ = try loadUsers() + + fixture.coordinator.copyRowsAsJson([.existing(0), .existing(1)]) + + let copied = try #require(clipboard.text) + #expect(!copied.contains("jane@example.com")) + #expect(copied.contains("j•••@example.com")) + } + + @Test("A grid holding masked values is read-only and says why") + func maskedGridIsReadOnly() throws { + _ = try loadUsers() + #expect(fixture.coordinator.activeResultEditRefusal == .masked) + #expect(!fixture.coordinator.canEditActiveResult) + } + + @Test("Reveal is refused without authentication and shows raw values for that tab once granted") + func revealNeedsAuthentication() async throws { + let tab = try loadUsers() + // Unselected, so a granted reveal records the choice without starting a query on a tab with no driver. + fixture.tabManager.selectedTabId = nil + + await fixture.coordinator.revealMaskedValues(tabId: tab.id, authenticator: StubAuthenticating(answer: false)) + #expect(fixture.tabManager.tabs.first { $0.id == tab.id }?.revealsMaskedValues == false) + + await fixture.coordinator.revealMaskedValues(tabId: tab.id, authenticator: StubAuthenticating(answer: true)) + #expect(fixture.tabManager.tabs.first { $0.id == tab.id }?.revealsMaskedValues == true) + let masks = fixture.coordinator.columnMasks( + forTabId: tab.id, table: "users", columns: ["email"], columnTypes: [.text(rawType: nil)] + ) + #expect(masks.isEmpty) + } + + @Test("Another tab stays masked after one tab reveals") + func revealIsPerTab() async throws { + let revealed = try loadUsers() + let other = fixture.addTab(QueryTab(title: "Q", query: "SELECT email FROM users", tabType: .query), select: false) + fixture.tabManager.selectedTabId = nil + + await fixture.coordinator.revealMaskedValues(tabId: revealed.id, authenticator: StubAuthenticating(answer: true)) + + let masks = fixture.coordinator.columnMasks( + forTabId: other.id, table: nil, columns: ["email"], columnTypes: [.text(rawType: nil)] + ) + #expect(masks.isMasked(0)) + } + } +} diff --git a/apple/TableProTests/Core/Services/Masking/MaskingPolicyTests.swift b/apple/TableProTests/Core/Services/Masking/MaskingPolicyTests.swift new file mode 100644 index 0000000000..6ba8ee668b --- /dev/null +++ b/apple/TableProTests/Core/Services/Masking/MaskingPolicyTests.swift @@ -0,0 +1,143 @@ +// +// MaskingPolicyTests.swift +// TableProTests +// + +import CloudKit +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +struct MaskingPolicyTests { + private let emailRule = MaskingPolicy(rules: [MaskingRule(pattern: "*.email", style: .email)]) + + @Test("A table.column pattern matches its table and column, ignoring case") + func tableColumnPatternMatches() { + let policy = MaskingPolicy(rules: [MaskingRule(pattern: "users.ssn")]) + #expect(policy.style(table: "Users", column: "SSN", typeName: nil) == .full) + #expect(policy.style(table: "public.users", column: "ssn", typeName: nil) == .full) + #expect(policy.style(table: "orders", column: "ssn", typeName: nil) == nil) + #expect(policy.style(table: "users", column: "name", typeName: nil) == nil) + } + + @Test("Without a source table a table.column pattern matches by its column alone") + func unknownTableMatchesByColumn() { + let policy = MaskingPolicy(rules: [MaskingRule(pattern: "users.ssn")]) + #expect(policy.style(table: nil, column: "ssn", typeName: nil) == .full) + } + + @Test("A bare pattern with wildcards matches any table's column") + func wildcardColumnPattern() { + let policy = MaskingPolicy(rules: [MaskingRule(pattern: "*password*", style: .null)]) + #expect(policy.style(table: "users", column: "password_hash", typeName: nil) == .null) + #expect(policy.style(table: nil, column: "old_password", typeName: nil) == .null) + #expect(policy.style(table: "users", column: "name", typeName: nil) == nil) + } + + @Test("A type rule matches the column's declared type") + func typeRuleMatches() { + let policy = MaskingPolicy(rules: [MaskingRule(pattern: "inet*", target: .type)]) + #expect(policy.style(table: "hosts", column: "address", typeName: "inet") == .full) + #expect(policy.style(table: "hosts", column: "address", typeName: "text") == nil) + #expect(policy.style(table: "hosts", column: "address", typeName: nil) == nil) + } + + @Test("The first matching rule wins, and an empty pattern is ignored") + func firstRuleWins() { + let policy = MaskingPolicy(rules: [ + MaskingRule(pattern: " "), + MaskingRule(pattern: "*.email", style: .hash), + MaskingRule(pattern: "email", style: .full), + ]) + #expect(policy.rules.count == 2) + #expect(policy.style(table: "users", column: "email", typeName: nil) == .hash) + } + + @Test("Each style hides the value as the issue describes") + func styles() { + let email: PluginCellValue = .text("jane.doe@example.com") + #expect(MaskedValue.mask(email, style: .full) == .text("••••")) + #expect(MaskedValue.mask(email, style: .email) == .text("j•••@example.com")) + #expect(MaskedValue.mask(.text("4111111111111234"), style: .lastFour) == .text("••••1234")) + #expect(MaskedValue.mask(.text("1234"), style: .lastFour) == .text("••••")) + #expect(MaskedValue.mask(.text("not-an-email"), style: .email) == .text("••••")) + #expect(MaskedValue.mask(email, style: .null) == .null) + #expect(MaskedValue.mask(.null, style: .full) == .null) + #expect(MaskedValue.mask(.bytes(Data([1, 2, 3])), style: .full) == .text("••••")) + } + + @Test("A stable hash keeps equal values equal without showing them") + func stableHash() throws { + let first = try #require(MaskedValue.mask(.text("jane@example.com"), style: .hash).asText) + let again = MaskedValue.mask(.text("jane@example.com"), style: .hash).asText + let other = MaskedValue.mask(.text("john@example.com"), style: .hash).asText + #expect(first == again) + #expect(first != other) + #expect(!first.contains("jane")) + #expect(first.count == 12) + } + + @Test("Column masks hide only the matched columns of a row") + func columnMasksRow() { + let masks = emailRule.columnMasks(table: "users", columns: ["id", "email"], typeNames: [nil, nil]) + #expect(masks.maskedColumns == [1]) + #expect(masks.row([.text("7"), .text("jane@example.com")]) == [.text("7"), .text("j•••@example.com")]) + } + + @Test("Rules round-trip through the connection's synced fields, and clearing them removes the key") + func connectionRoundTrip() { + var connection = TestFixtures.makeConnection() + connection.maskingRules = [MaskingRule(pattern: "*.email", style: .email), MaskingRule(pattern: "")] + #expect(connection.additionalFields[DatabaseConnection.maskingRulesKey] != nil) + #expect(connection.maskingRules.map(\.pattern) == ["*.email"]) + #expect(connection.maskingPolicy.style(table: "users", column: "email", typeName: nil) == .email) + + connection.maskingRules = [] + #expect(connection.additionalFields[DatabaseConnection.maskingRulesKey] == nil) + } + + @Test("Rules travel with the connection through iCloud sync") + func rulesSync() throws { + var connection = TestFixtures.makeConnection() + connection.maskingRules = [MaskingRule(pattern: "users.ssn", style: .lastFour)] + let zoneID = CKRecordZone.ID(zoneName: "TestZone", ownerName: CKCurrentUserDefaultName) + + let synced = try SyncRecordMapper.toConnection(SyncRecordMapper.toCKRecord(connection, in: zoneID)) + + #expect(synced.maskingRules == connection.maskingRules) + } + + @Test("An unknown style masks fully, and an unknown target or a missing pattern masks every column") + func unreadableRulesFailClosed() throws { + let json = #"[{"pattern":"*.email","style":"later"},{"pattern":"^ssn$","target":"regex"},{"style":"full"}]"# + let rules = try JSONDecoder().decode([MaskingRule].self, from: Data(json.utf8)) + #expect(rules.map { $0.style } == [.full, .full, .full]) + #expect(rules.map { $0.pattern } == ["*.email", "*", "*"]) + #expect(MaskingPolicy(rules: rules).style(table: "users", column: "name", typeName: nil) == .full) + } + + @Test("Stored rules that cannot be read at all mask every column") + func corruptRulesFailClosed() { + var connection = TestFixtures.makeConnection() + connection.additionalFields[DatabaseConnection.maskingRulesKey] = "{not json" + #expect(connection.maskingPolicy.style(table: "users", column: "name", typeName: nil) == .full) + } + + @Test("QueryResult masking keeps everything but the masked values") + func queryResultMasking() { + var result = QueryResult( + columns: ["id", "email"], + columnTypes: [.integer(rawType: "int"), .text(rawType: "varchar")], + rows: [[.text("1"), .text("jane@example.com")]], + rowsAffected: 0, + executionTime: 0.5, + error: nil + ) + result.isTruncated = true + let masked = result.masked(by: emailRule) + #expect(masked.rows == [[.text("1"), .text("j•••@example.com")]]) + #expect(masked.isTruncated) + #expect(masked.columns == result.columns) + } +} diff --git a/apple/TableProTests/Helpers/ScriptAnsweringDriver.swift b/apple/TableProTests/Helpers/ScriptAnsweringDriver.swift index 3c5783a2dd..7015f6246f 100644 --- a/apple/TableProTests/Helpers/ScriptAnsweringDriver.swift +++ b/apple/TableProTests/Helpers/ScriptAnsweringDriver.swift @@ -24,6 +24,7 @@ final class ScriptAnsweringDriver: DatabaseDriver, @unchecked Sendable { private let transactionState: PluginSessionTransactionState private let transactionStateAfterBatches: PluginSessionTransactionState? private let answer: @Sendable (String) -> QueryBatchResult + private let statementAnswer: @Sendable (String) -> QueryResult private let lock = NSLock() private var batches: [SentBatch] = [] private var statements: [String] = [] @@ -40,7 +41,10 @@ final class ScriptAnsweringDriver: DatabaseDriver, @unchecked Sendable { transactionState: PluginSessionTransactionState = .idle, transactionStateAfterBatches: PluginSessionTransactionState? = nil, sessionLexicalState: PluginSessionLexicalState? = nil, - answer: @escaping @Sendable (String) -> QueryBatchResult = { _ in .empty } + answer: @escaping @Sendable (String) -> QueryBatchResult = { _ in .empty }, + statementAnswer: @escaping @Sendable (String) -> QueryResult = { _ in + ScriptAnsweringDriver.resultSet(columns: ["n"], rows: [["1"]]) + } ) { self.connection = connection self.sessionLexicalState = sessionLexicalState @@ -48,6 +52,7 @@ final class ScriptAnsweringDriver: DatabaseDriver, @unchecked Sendable { self.transactionState = transactionState self.transactionStateAfterBatches = transactionStateAfterBatches self.answer = answer + self.statementAnswer = statementAnswer } var sentBatches: [SentBatch] { @@ -79,7 +84,7 @@ final class ScriptAnsweringDriver: DatabaseDriver, @unchecked Sendable { private func record(_ query: String) -> QueryResult { lock.withLock { statements.append(query) } - return Self.resultSet(columns: ["n"], rows: [["1"]]) + return statementAnswer(query) } func explainPlan(statement: String, variantId: String) async throws -> QueryResult { diff --git a/apple/TableProTests/ViewModels/ConnectionFormEditsTests.swift b/apple/TableProTests/ViewModels/ConnectionFormEditsTests.swift index 74f9d02bfe..aae879f871 100644 --- a/apple/TableProTests/ViewModels/ConnectionFormEditsTests.swift +++ b/apple/TableProTests/ViewModels/ConnectionFormEditsTests.swift @@ -18,7 +18,8 @@ struct ConnectionFormEditsTests { queryTimeoutSeconds: Int? = nil, completionIdentifierCase: SQLIdentifierCase = .default, additionalFields: [String: String] = [:], - ownedAdditionalFieldIDs: Set = [] + ownedAdditionalFieldIDs: Set = [], + maskingRules: [MaskingRule] = [] ) -> ConnectionFormEdits { ConnectionFormEdits( name: "Edited", @@ -51,10 +52,33 @@ struct ConnectionFormEditsTests { startupCommands: nil, localOnly: false, additionalFields: additionalFields, - ownedAdditionalFieldIDs: ownedAdditionalFieldIDs + ownedAdditionalFieldIDs: ownedAdditionalFieldIDs, + maskingRules: maskingRules ) } + @Test("The form saves masking rules and removing every rule clears them, beside the driver's own fields") + func maskingRulesAreSaved() { + var original = populatedConnection() + original.additionalFields["sslmode"] = "require" + let rules = [MaskingRule(pattern: "*.email", style: .email)] + + let saved = edits( + additionalFields: ["sslmode": "require"], + ownedAdditionalFieldIDs: ConnectionFormEdits.appManagedAdditionalFieldIDs, + maskingRules: rules + ).applied(to: original) + #expect(saved.maskingRules == rules) + #expect(saved.additionalFields["sslmode"] == "require") + + let cleared = edits( + additionalFields: ["sslmode": "require"], + ownedAdditionalFieldIDs: ConnectionFormEdits.appManagedAdditionalFieldIDs + ).applied(to: saved) + #expect(cleared.maskingRules.isEmpty) + #expect(cleared.additionalFields[DatabaseConnection.maskingRulesKey] == nil) + } + private func populatedConnection() -> DatabaseConnection { var connection = DatabaseConnection( id: UUID(), diff --git a/apple/changelog.d/3978.added.md b/apple/changelog.d/3978.added.md new file mode 100644 index 0000000000..83f0fbedad --- /dev/null +++ b/apple/changelog.d/3978.added.md @@ -0,0 +1 @@ +Masking rules hide matching column values in the grid, copies, exports, the AI assistant and MCP. From be149c81683132b8683f620b818280a052c85992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Nam=20Long?= Date: Sun, 1 Nov 2026 17:34:31 +0700 Subject: [PATCH 83/85] feat(plugin-memcached): add a Memcached driver over the text protocol (#3980) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ngô Quốc Đạt --- .github/plugin-registry.json | 3 + apple/Packages/TableProCore/Package.swift | 13 + .../SQLDDLFallbackPolicy.swift | 1 + .../TableProCoreTypes/DatabaseType.swift | 4 +- .../MemcachedBrowseSpec.swift | 165 +++++++ .../MemcachedCommand.swift | 286 +++++++++++ .../MemcachedKeyListing.swift | 95 ++++ .../MemcachedReplyReader.swift | 152 ++++++ .../MemcachedStatements.swift | 35 ++ .../SQLLexicalProfile.swift | 1 + .../MemcachedCommandParserTests.swift | 135 ++++++ .../MemcachedKeyListingTests.swift | 121 +++++ .../MemcachedReplyReaderTests.swift | 79 ++++ .../DatabaseTypeTests.swift | 10 +- .../Plugins/MemcachedDriverPlugin/Info.plist | 12 + .../MemcachedConnection.swift | 304 ++++++++++++ .../MemcachedError.swift | 91 ++++ .../MemcachedPlugin.swift | 70 +++ .../MemcachedPluginDriver.swift | 445 ++++++++++++++++++ .../MemcachedStatementGenerator.swift | 162 +++++++ ...inMetadataRegistry+MemcachedDefaults.swift | 64 +++ ...ginMetadataRegistry+RegistryDefaults.swift | 2 +- .../MemcachedDashboardProvider.swift | 85 ++++ .../ServerDashboardQueryProviderFactory.swift | 5 + .../Execution/ExternalStatementGate.swift | 1 + .../SQL/QueryClassifier+Memcached.swift | 36 ++ .../Core/Utilities/SQL/QueryClassifier.swift | 2 + .../Models/Connection/DatabaseType.swift | 1 + .../TablePro/Resources/Localizable.xcstrings | 111 +++++ ...rnalStatementGatePrivateRequestTests.swift | 2 + ...PluginMetadataRegistryTypeCountTests.swift | 2 +- .../MemcachedDashboardProviderTests.swift | 51 ++ ...erDashboardQueryProviderFactoryTests.swift | 2 +- .../Query/AllTablesListingTests.swift | 2 +- .../SQL/QueryClassifierHardeningTests.swift | 22 + .../Models/Query/QueryTabBaseQueryTests.swift | 2 +- .../Main/QueryParameterDetectionTests.swift | 2 +- apple/changelog.d/3980.added.md | 1 + apple/project.yml | 16 + apple/scripts/probes/check-memcached.sh | 56 +++ apple/scripts/probes/memcached-probe.swift | 195 ++++++++ 41 files changed, 2836 insertions(+), 8 deletions(-) create mode 100644 apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedBrowseSpec.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedCommand.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedKeyListing.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedReplyReader.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedStatements.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedCommandParserTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedKeyListingTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedReplyReaderTests.swift create mode 100644 apple/Plugins/MemcachedDriverPlugin/Info.plist create mode 100644 apple/Plugins/MemcachedDriverPlugin/MemcachedConnection.swift create mode 100644 apple/Plugins/MemcachedDriverPlugin/MemcachedError.swift create mode 100644 apple/Plugins/MemcachedDriverPlugin/MemcachedPlugin.swift create mode 100644 apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift create mode 100644 apple/Plugins/MemcachedDriverPlugin/MemcachedStatementGenerator.swift create mode 100644 apple/TablePro/Core/Plugins/PluginMetadataRegistry+MemcachedDefaults.swift create mode 100644 apple/TablePro/Core/ServerDashboard/Providers/MemcachedDashboardProvider.swift create mode 100644 apple/TablePro/Core/Utilities/SQL/QueryClassifier+Memcached.swift create mode 100644 apple/TableProTests/Core/ServerDashboard/MemcachedDashboardProviderTests.swift create mode 100644 apple/changelog.d/3980.added.md create mode 100755 apple/scripts/probes/check-memcached.sh create mode 100644 apple/scripts/probes/memcached-probe.swift diff --git a/.github/plugin-registry.json b/.github/plugin-registry.json index f6f39ddbb2..778efe9f7e 100644 --- a/.github/plugin-registry.json +++ b/.github/plugin-registry.json @@ -49,6 +49,9 @@ "LibSQLDriverPlugin": { "name": "libSQL / Turso Driver" }, + "MemcachedDriverPlugin": { + "name": "Memcached Driver" + }, "MSSQLDriver": { "name": "MSSQL Driver" }, diff --git a/apple/Packages/TableProCore/Package.swift b/apple/Packages/TableProCore/Package.swift index 61247fd6ed..e0b658ff61 100644 --- a/apple/Packages/TableProCore/Package.swift +++ b/apple/Packages/TableProCore/Package.swift @@ -57,6 +57,7 @@ let package = Package( .library(name: "TableProMySQLCore", targets: ["TableProMySQLCore"]), .library(name: "TableProPostgreSQLCore", targets: ["TableProPostgreSQLCore"]), .library(name: "TableProRedisCore", targets: ["TableProRedisCore"]), + .library(name: "TableProMemcachedCore", targets: ["TableProMemcachedCore"]), .library(name: "TableProTestSupport", targets: ["TableProTestSupport"]) ], targets: [ @@ -275,6 +276,12 @@ let package = Package( path: "Sources/TableProPostgreSQLCore", swiftSettings: pluginCoreSettings ), + .target( + name: "TableProMemcachedCore", + dependencies: [], + path: "Sources/TableProMemcachedCore", + swiftSettings: pluginCoreSettings + ), .target( name: "TableProRedisCore", dependencies: [], @@ -451,6 +458,12 @@ let package = Package( path: "Tests/TableProPostgreSQLCoreTests", swiftSettings: pluginCoreSettings ), + .testTarget( + name: "TableProMemcachedCoreTests", + dependencies: ["TableProMemcachedCore"], + path: "Tests/TableProMemcachedCoreTests", + swiftSettings: pluginCoreSettings + ), .testTarget( name: "TableProRedisCoreTests", dependencies: ["TableProRedisCore"], diff --git a/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/SQLDDLFallbackPolicy.swift b/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/SQLDDLFallbackPolicy.swift index 09f552b188..d5bc57829a 100644 --- a/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/SQLDDLFallbackPolicy.swift +++ b/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/SQLDDLFallbackPolicy.swift @@ -36,6 +36,7 @@ public enum SQLDDLFallbackPolicy { "Elasticsearch", "Typesense", "Weaviate", + "Memcached", "Kafka", "etcd", "Redis", diff --git a/apple/Packages/TableProCore/Sources/TableProCoreTypes/DatabaseType.swift b/apple/Packages/TableProCore/Sources/TableProCoreTypes/DatabaseType.swift index cfa5a5fb91..b9b9da2621 100644 --- a/apple/Packages/TableProCore/Sources/TableProCoreTypes/DatabaseType.swift +++ b/apple/Packages/TableProCore/Sources/TableProCoreTypes/DatabaseType.swift @@ -43,6 +43,7 @@ public struct DatabaseType: Hashable, Codable, Sendable, RawRepresentable { public static let kafka = DatabaseType(rawValue: "Kafka") public static let cloudflareR2SQL = DatabaseType(rawValue: "Cloudflare R2 SQL") public static let weaviate = DatabaseType(rawValue: "Weaviate") + public static let memcached = DatabaseType(rawValue: "Memcached") public static let firestore = DatabaseType(rawValue: "Firestore") public static let firebaseRealtimeDatabase = DatabaseType(rawValue: "Firebase Realtime Database") public static let bigtable = DatabaseType(rawValue: "Bigtable") @@ -51,7 +52,7 @@ public struct DatabaseType: Hashable, Codable, Sendable, RawRepresentable { .mysql, .mariadb, .tidb, .databend, .oceanbase, .starrocks, .postgresql, .sqlite, .redis, .mongodb, .clickhouse, .mssql, .oracle, .dameng, .duckdb, .cassandra, .redshift, .etcd, .cloudflareD1, .dynamodb, .bigquery, .spanner, .snowflake, .libsql, .beancount, - .surrealdb, .teradata, .trino, .kafka, .cloudflareR2SQL, .weaviate, .firestore, .firebaseRealtimeDatabase, .bigtable + .surrealdb, .teradata, .trino, .kafka, .cloudflareR2SQL, .weaviate, .memcached, .firestore, .firebaseRealtimeDatabase, .bigtable ] /// Icon name for this database type — asset catalog name (e.g. "mysql-icon") or SF Symbol fallback @@ -89,6 +90,7 @@ public struct DatabaseType: Hashable, Codable, Sendable, RawRepresentable { case .kafka: return "kafka-icon" case .cloudflareR2SQL: return "cloudflare-r2-sql-icon" case .weaviate: return "weaviate-icon" + case .memcached: return "memorychip" case .firestore, .firebaseRealtimeDatabase: return "firebase-icon" case .bigtable: return "bigtable-icon" default: return "externaldrive" diff --git a/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedBrowseSpec.swift b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedBrowseSpec.swift new file mode 100644 index 0000000000..bb65220078 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedBrowseSpec.swift @@ -0,0 +1,165 @@ +import Foundation + +public enum MemcachedColumn: String, CaseIterable, Sendable { + case key = "Key" + case value = "Value" + case ttl = "TTL" + case flags = "Flags" + + public var typeName: String { + switch self { + case .key, .value: return "String" + case .ttl, .flags: return "Int64" + } + } +} + +public enum MemcachedKeyComparison: String, Codable, CaseIterable, Sendable { + case equal = "=" + case notEqual = "!=" + case contains = "CONTAINS" + case notContains = "NOT CONTAINS" + case startsWith = "STARTS WITH" + case endsWith = "ENDS WITH" + case inList = "IN" + case notInList = "NOT IN" + case regex = "REGEX" +} + +public enum MemcachedFilterRefusal: Error, Equatable, Sendable { + case notKeyColumn(String) + case unsupportedOperator(String) + case emptyList(String) + case invalidPattern(String) +} + +/// A page of the key grid. The grid hands the driver this spec as a tagged statement, so a page, its sort and its +/// filter reach the driver the way every other statement does. +public struct MemcachedBrowseSpec: Codable, Equatable, Sendable { + public struct KeyCondition: Codable, Equatable, Sendable { + public let comparison: MemcachedKeyComparison + public let value: String + public let isCaseSensitive: Bool + + public init(comparison: MemcachedKeyComparison, value: String, isCaseSensitive: Bool) { + self.comparison = comparison + self.value = value + self.isCaseSensitive = isCaseSensitive + } + } + + public static let tag = "MEMCACHED_BROWSE:" + public static let refusalTag = "MEMCACHED_REFUSAL:" + + public let limit: Int + public let offset: Int + public let ascending: Bool + public let conditions: [KeyCondition] + public let matchesAll: Bool + + public init(limit: Int, offset: Int, ascending: Bool, conditions: [KeyCondition] = [], matchesAll: Bool = true) { + self.limit = limit + self.offset = offset + self.ascending = ascending + self.conditions = conditions + self.matchesAll = matchesAll + } + + public var statement: String { + let encoder = JSONEncoder() + encoder.outputFormatting = .sortedKeys + return Self.tag + ((try? encoder.encode(self)) ?? Data()).base64EncodedString() + } + + public static func parse(_ statement: String) -> MemcachedBrowseSpec? { + guard statement.hasPrefix(tag), let data = Data(base64Encoded: String(statement.dropFirst(tag.count))) else { + return nil + } + guard let spec = try? JSONDecoder().decode(MemcachedBrowseSpec.self, from: data), + spec.limit >= 0, spec.offset >= 0 else { return nil } + return spec + } + + public static func refusalStatement(_ message: String) -> String { + refusalTag + Data(message.utf8).base64EncodedString() + } + + public static func refusalMessage(_ statement: String) -> String? { + guard statement.hasPrefix(refusalTag), + let data = Data(base64Encoded: String(statement.dropFirst(refusalTag.count))) else { return nil } + return String(bytes: data, encoding: .utf8) + } + + public static func condition( + column: String, op: String, value: String, isCaseSensitive: Bool + ) throws(MemcachedFilterRefusal) -> KeyCondition { + guard column == MemcachedColumn.key.rawValue else { throw .notKeyColumn(column) } + let name = op.trimmingCharacters(in: .whitespaces).uppercased() + guard let comparison = MemcachedKeyComparison(rawValue: name) else { throw .unsupportedOperator(op) } + let condition = KeyCondition(comparison: comparison, value: value, isCaseSensitive: isCaseSensitive) + _ = try KeyTest(condition) + return condition + } + + /// The keys this page shows, out of every key listed: filtered, sorted by key, then cut to the page. + public func page(of keys: [MemcachedKeyEntry]) throws(MemcachedFilterRefusal) -> [MemcachedKeyEntry] { + let matching = try filter(keys) + let sorted = matching.sorted { ascending ? $0.key < $1.key : $0.key > $1.key } + guard offset < sorted.count else { return [] } + return Array(sorted[offset ..< offset + min(limit, sorted.count - offset)]) + } + + public func filter(_ keys: [MemcachedKeyEntry]) throws(MemcachedFilterRefusal) -> [MemcachedKeyEntry] { + guard !conditions.isEmpty else { return keys } + var tests: [KeyTest] = [] + for condition in conditions { tests.append(try KeyTest(condition)) } + return keys.filter { entry in + matchesAll ? tests.allSatisfy { $0.matches(entry.key) } : tests.contains { $0.matches(entry.key) } + } + } +} + +private struct KeyTest { + let condition: MemcachedBrowseSpec.KeyCondition + let items: [String] + let regex: NSRegularExpression? + + init(_ condition: MemcachedBrowseSpec.KeyCondition) throws(MemcachedFilterRefusal) { + self.condition = condition + switch condition.comparison { + case .inList, .notInList: + items = condition.value.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) } + .filter { !$0.isEmpty } + guard !items.isEmpty else { throw .emptyList(condition.comparison.rawValue) } + regex = nil + case .regex: + items = [] + let options: NSRegularExpression.Options = condition.isCaseSensitive ? [] : [.caseInsensitive] + guard let compiled = try? NSRegularExpression(pattern: condition.value, options: options) else { + throw .invalidPattern(condition.value) + } + regex = compiled + default: + items = [] + regex = nil + } + } + + func matches(_ key: String) -> Bool { + let options: String.CompareOptions = condition.isCaseSensitive ? [.literal] : [.caseInsensitive] + let value = condition.value + switch condition.comparison { + case .equal: return key.compare(value, options: options) == .orderedSame + case .notEqual: return key.compare(value, options: options) != .orderedSame + case .contains: return key.range(of: value, options: options) != nil + case .notContains: return key.range(of: value, options: options) == nil + case .startsWith: return key.range(of: value, options: options.union(.anchored)) != nil + case .endsWith: return key.range(of: value, options: options.union([.anchored, .backwards])) != nil + case .inList: return items.contains { key.compare($0, options: options) == .orderedSame } + case .notInList: return !items.contains { key.compare($0, options: options) == .orderedSame } + case .regex: + let range = NSRange(key.startIndex..., in: key) + return regex?.firstMatch(in: key, range: range) != nil + } + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedCommand.swift b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedCommand.swift new file mode 100644 index 0000000000..78e4a26736 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedCommand.swift @@ -0,0 +1,286 @@ +import Foundation + +public enum MemcachedStorageVerb: String, CaseIterable, Sendable { + case set, add, replace, append, prepend +} + +/// One statement of the console, in the order the text protocol takes its arguments. A value is the last argument +/// of the line instead of a data block, so a statement stays one line the review sheet can show. +public enum MemcachedCommand: Equatable, Sendable { + case get(keys: [String], withCAS: Bool) + case getAndTouch(exptime: Int64, keys: [String], withCAS: Bool) + case store(MemcachedStorageVerb, key: String, flags: UInt32, exptime: Int64, value: Data) + case compareAndSwap(key: String, flags: UInt32, exptime: Int64, unique: UInt64, value: Data) + case touch(key: String, exptime: Int64) + case delete(key: String) + case increment(key: String, delta: UInt64) + case decrement(key: String, delta: UInt64) + case flushAll(delay: Int64?) + case stats(arguments: [String]) + case version + case metadump(selector: String) + + public var replyShape: MemcachedReplyShape { + switch self { + case .get, .getAndTouch: return .values + case .stats(let arguments): return Self.statsChangingServer(arguments) ? .status : .lines + case .metadump: return .lines + default: return .status + } + } + + /// `stats reset`, `stats detail on|off` and `stats sizes_enable|sizes_disable` change the server and answer one + /// status line with no `END`. Every other `stats` form reads. + public static func statsChangingServer(_ arguments: [String]) -> Bool { + let words = arguments.map { $0.lowercased() } + switch words.first { + case "reset", "sizes_enable", "sizes_disable": return true + case "detail": return words.count > 1 && words[1] != "dump" + default: return false + } + } + + /// The bytes the text protocol takes for this command, data block included. + public var wireBytes: [UInt8] { + func line(_ words: [String]) -> [UInt8] { Array((words.joined(separator: " ") + "\r\n").utf8) } + func block(_ head: [String], _ value: Data) -> [UInt8] { + line(head + [String(value.count)]) + Array(value) + Array("\r\n".utf8) + } + switch self { + case .get(let keys, let withCAS): + return line([withCAS ? "gets" : "get"] + keys) + case .getAndTouch(let exptime, let keys, let withCAS): + return line([withCAS ? "gats" : "gat", String(exptime)] + keys) + case .store(let verb, let key, let flags, let exptime, let value): + return block([verb.rawValue, key, String(flags), String(exptime)], value) + case .compareAndSwap(let key, let flags, let exptime, let unique, let value): + return line(["cas", key, String(flags), String(exptime), String(value.count), String(unique)]) + + Array(value) + Array("\r\n".utf8) + case .touch(let key, let exptime): + return line(["touch", key, String(exptime)]) + case .delete(let key): + return line(["delete", key]) + case .increment(let key, let delta): + return line(["incr", key, String(delta)]) + case .decrement(let key, let delta): + return line(["decr", key, String(delta)]) + case .flushAll(let delay): + return line(["flush_all"] + (delay.map { [String($0)] } ?? [])) + case .stats(let arguments): + return line(["stats"] + arguments) + case .version: + return line(["version"]) + case .metadump(let selector): + return line(["lru_crawler", "metadump", selector]) + } + } +} + +public enum MemcachedCommandError: Error, Equatable, Sendable { + case empty + case unknownCommand(String) + case wrongArguments(command: String, usage: String) + case invalidKey(String) + case invalidNumber(String) + case unterminatedQuote +} + +public enum MemcachedCommandParser { + /// The longest key memcached accepts, in bytes. + public static let maximumKeyLength = 250 + + public static func parse(_ statement: String) throws(MemcachedCommandError) -> MemcachedCommand { + let words = try tokenize(statement) + guard let first = words.first else { throw .empty } + let name = Self.text(first).lowercased() + let arguments = Array(words.dropFirst()) + func text(_ index: Int) -> String { Self.text(arguments[index]) } + func usage(_ form: String) -> MemcachedCommandError { .wrongArguments(command: name, usage: form) } + + switch name { + case "get", "gets": + guard !arguments.isEmpty else { throw usage("\(name) [...]") } + return .get(keys: try keys(arguments[...]), withCAS: name == "gets") + case "gat", "gats": + guard arguments.count >= 2 else { throw usage("\(name) [...]") } + return .getAndTouch( + exptime: try integer(text(0)), keys: try keys(arguments.dropFirst()), withCAS: name == "gats" + ) + case "set", "add", "replace", "append", "prepend": + guard arguments.count == 4, let verb = MemcachedStorageVerb(rawValue: name) else { + throw usage("\(name) ") + } + return .store( + verb, key: try key(arguments[0]), flags: try flags(text(1)), exptime: try integer(text(2)), + value: Data(arguments[3]) + ) + case "cas": + guard arguments.count == 5 else { throw usage("cas ") } + return .compareAndSwap( + key: try key(arguments[0]), flags: try flags(text(1)), exptime: try integer(text(2)), + unique: try unsigned(text(3)), value: Data(arguments[4]) + ) + case "touch": + guard arguments.count == 2 else { throw usage("touch ") } + return .touch(key: try key(arguments[0]), exptime: try integer(text(1))) + case "delete": + guard arguments.count == 1 else { throw usage("delete ") } + return .delete(key: try key(arguments[0])) + case "incr", "decr": + guard arguments.count == 2 else { throw usage("\(name) ") } + let key = try key(arguments[0]) + let delta = try unsigned(text(1)) + return name == "incr" ? .increment(key: key, delta: delta) : .decrement(key: key, delta: delta) + case "flush_all": + guard arguments.count <= 1 else { throw usage("flush_all []") } + return .flushAll(delay: try arguments.isEmpty ? nil : integer(text(0))) + case "stats": + let words = arguments.indices.map(text) + guard words.allSatisfy(isPlainWord) else { throw usage("stats [ []]") } + return .stats(arguments: words) + case "version": + guard arguments.isEmpty else { throw usage("version") } + return .version + case "lru_crawler": + guard arguments.count == 2, text(0).lowercased() == "metadump", isPlainWord(text(1)) else { + throw usage("lru_crawler metadump ") + } + return .metadump(selector: text(1)) + default: + throw .unknownCommand(name) + } + } + + /// Writes a value so `parse` reads it back byte for byte. + public static func quoted(_ value: String) -> String { + escaped(Array(value.utf8), keepingUTF8: true) + } + + /// A key as one argument: as written unless it holds a quote or backslash, which the tokenizer would read. + public static func argument(_ key: String) -> String { + key.contains(where: { $0 == "\"" || $0 == "'" || $0 == "\\" }) ? quoted(key) : key + } + + /// Writes bytes that may not be UTF-8, every byte past ASCII as `\xHH`. + public static func quoted(bytes: Data) -> String { + escaped(Array(bytes), keepingUTF8: false) + } + + private static func escaped(_ bytes: [UInt8], keepingUTF8: Bool) -> String { + var out: [UInt8] = [0x22] + for byte in bytes { + switch byte { + case 0x22: out += Array(#"\""#.utf8) + case 0x5C: out += Array(#"\\"#.utf8) + case 0x0A: out += Array(#"\n"#.utf8) + case 0x0D: out += Array(#"\r"#.utf8) + case 0x09: out += Array(#"\t"#.utf8) + case 0x20 ..< 0x7F: out.append(byte) + case 0x80...: out += keepingUTF8 ? [byte] : Array(String(format: #"\x%02X"#, byte).utf8) + default: out += Array(String(format: #"\x%02X"#, byte).utf8) + } + } + out.append(0x22) + return text(out) + } + + /// Splits a line on whitespace. Double quotes take `\n`, `\r`, `\t`, `\xHH`, `\"` and `\\`; single quotes + /// take everything as written. + static func tokenize(_ text: String) throws(MemcachedCommandError) -> [[UInt8]] { + var words: [[UInt8]] = [] + var current: [UInt8] = [] + var inWord = false + var scalars = Array(text.unicodeScalars)[...] + while let scalar = scalars.popFirst() { + switch scalar { + case " ", "\t", "\n", "\r": + if inWord { words.append(current); current = []; inWord = false } + case "'": + inWord = true + guard let end = scalars.firstIndex(of: "'") else { throw .unterminatedQuote } + current += Array(String(String.UnicodeScalarView(scalars[.., + into current: inout [UInt8] + ) throws(MemcachedCommandError) { + while let scalar = scalars.popFirst() { + switch scalar { + case "\"": + return + case "\\": + guard let escaped = scalars.popFirst() else { throw .unterminatedQuote } + switch escaped { + case "n": current.append(0x0A) + case "r": current.append(0x0D) + case "t": current.append(0x09) + case "x": + let digits = String(String.UnicodeScalarView(scalars.prefix(2))) + guard digits.count == 2, let byte = UInt8(digits, radix: 16) else { + current += Array("\\x".utf8) + continue + } + current.append(byte) + scalars = scalars.dropFirst(2) + default: current += Array(String(escaped).utf8) + } + default: + current += Array(String(scalar).utf8) + } + } + throw .unterminatedQuote + } + + /// A key is 1 to 250 bytes with no space or control character, the limits memcached's text protocol sets. + public static func isValidKey(_ bytes: [UInt8]) -> Bool { + !bytes.isEmpty && bytes.count <= maximumKeyLength && !bytes.contains { $0 <= 0x20 || $0 == 0x7F } + } + + private static func key(_ bytes: [UInt8]) throws(MemcachedCommandError) -> String { + let text = Self.text(bytes) + guard isValidKey(bytes) else { throw .invalidKey(text) } + return text + } + + private static func keys(_ words: ArraySlice<[UInt8]>) throws(MemcachedCommandError) -> [String] { + var keys: [String] = [] + for word in words { keys.append(try key(word)) } + return keys + } + + /// Bytes as text for a message or a number, never for a value: a byte that is not UTF-8 reads as Latin-1. + static func text(_ bytes: [UInt8]) -> String { + String(bytes: bytes, encoding: .utf8) ?? String(bytes: bytes, encoding: .isoLatin1) ?? "" + } + + private static func isPlainWord(_ word: String) -> Bool { + isValidKey(Array(word.utf8)) + } + + private static func integer(_ text: String) throws(MemcachedCommandError) -> Int64 { + guard let value = Int64(text) else { throw .invalidNumber(text) } + return value + } + + private static func unsigned(_ text: String) throws(MemcachedCommandError) -> UInt64 { + guard let value = UInt64(text) else { throw .invalidNumber(text) } + return value + } + + private static func flags(_ text: String) throws(MemcachedCommandError) -> UInt32 { + guard let value = UInt32(text) else { throw .invalidNumber(text) } + return value + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedKeyListing.swift b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedKeyListing.swift new file mode 100644 index 0000000000..cd44dc0707 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedKeyListing.swift @@ -0,0 +1,95 @@ +import Foundation + +public struct MemcachedKeyEntry: Equatable, Sendable { + public let key: String + /// Unix time the item expires at, or nil for an item that never expires. + public let expiresAt: Int64? + public let flags: UInt32? + + public init(key: String, expiresAt: Int64?, flags: UInt32?) { + self.key = key + self.expiresAt = expiresAt + self.flags = flags + } + + /// Seconds left at `serverTime`, nil for no expiry. An item past its time reads as gone. + public func secondsLeft(at serverTime: Int64) -> Int64? { + expiresAt.map { $0 - serverTime } + } + + public func isExpired(at serverTime: Int64) -> Bool { + (secondsLeft(at: serverTime) ?? 1) <= 0 + } +} + +/// How the key list was read. A cachedump is a per-slab sample capped by the server, never the whole cache. +public enum MemcachedKeySource: String, Sendable, Equatable { + case metadump + case cachedump +} + +public enum MemcachedKeyListing { + /// One line of `lru_crawler metadump`: `key=a%3A1 exp=-1 la=1793499867 cas=2 fetch=yes cls=1 size=71 flags=5`. + /// The key is URL-encoded, and `exp` is a Unix time or -1 for never. + public static func metadumpEntry(_ line: String) -> MemcachedKeyEntry? { + var fields: [Substring: Substring] = [:] + for part in line.split(separator: " ") { + guard let equals = part.firstIndex(of: "=") else { continue } + fields[part[.. 0 ? $0 : nil }, + flags: fields["flags"].flatMap { UInt32($0) } + ) + } + + /// One line of `stats cachedump`: `ITEM user:42 [5 b; 1793500167 s]`. An expiry of 0 means never. + public static func cachedumpEntry(_ line: String) -> MemcachedKeyEntry? { + guard line.hasPrefix("ITEM "), let open = line.lastIndex(of: "[") else { return nil } + let key = line[line.index(line.startIndex, offsetBy: 5) ..< open].trimmingCharacters(in: .whitespaces) + guard !key.isEmpty else { return nil } + let inside = line[line.index(after: open)...].split(whereSeparator: { $0 == ";" || $0 == " " || $0 == "]" }) + let expiry = inside.count >= 3 ? Int64(inside[2]) : nil + return MemcachedKeyEntry(key: key, expiresAt: expiry.flatMap { $0 > 0 ? $0 : nil }, flags: nil) + } + + /// The slab classes holding items, read from `stats items` lines such as `STAT items:1:number 3`. + public static func slabClasses(fromItemStats lines: [String]) -> [Int] { + var classes: [Int] = [] + for (name, value) in statPairs(lines) where name.hasSuffix(":number") && (Int(value) ?? 0) > 0 { + let parts = name.split(separator: ":") + if parts.count == 3, parts[0] == "items", let id = Int(parts[1]), !classes.contains(id) { + classes.append(id) + } + } + return classes.sorted() + } + + /// `STAT ` lines as pairs, in the server's order. + public static func statPairs(_ lines: [String]) -> [(name: String, value: String)] { + lines.compactMap { line in + let parts = line.split(separator: " ", maxSplits: 2, omittingEmptySubsequences: true) + guard parts.count >= 2, parts[0] == "STAT" else { return nil } + return (String(parts[1]), parts.count == 3 ? String(parts[2]) : "") + } + } + + private static func percentDecoded(_ text: Substring) -> String? { + var bytes: [UInt8] = [] + var iterator = Array(text.utf8)[...] + while let byte = iterator.popFirst() { + guard byte == UInt8(ascii: "%") else { + bytes.append(byte) + continue + } + guard iterator.count >= 2, + let decoded = UInt8(String(bytes: iterator.prefix(2), encoding: .ascii) ?? "", radix: 16) else { return nil } + bytes.append(decoded) + iterator = iterator.dropFirst(2) + } + return String(bytes: bytes, encoding: .utf8) + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedReplyReader.swift b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedReplyReader.swift new file mode 100644 index 0000000000..e90c657a49 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedReplyReader.swift @@ -0,0 +1,152 @@ +import Foundation + +public enum MemcachedReplyShape: Sendable, Equatable { + /// `VALUE` blocks closed by `END`, the reply to `get` and its kin. + case values + /// Lines closed by `END`, the reply to `stats` and `lru_crawler metadump`. + case lines + /// One status line such as `STORED`, `DELETED`, `VERSION 1.6.45` or a counter's new value. + case status +} + +public struct MemcachedValue: Equatable, Sendable { + public let key: String + public let flags: UInt32 + public let data: Data + public let unique: UInt64? + + public init(key: String, flags: UInt32, data: Data, unique: UInt64? = nil) { + self.key = key + self.flags = flags + self.data = data + self.unique = unique + } +} + +public enum MemcachedReply: Equatable, Sendable { + case values([MemcachedValue]) + /// `truncated` means the reader stopped at its line limit before `END`; the rest is still on the wire. + case lines([String], truncated: Bool) + case status(String) +} + +public struct MemcachedServerError: Error, Equatable, Sendable { + public enum Kind: String, Sendable { + case error = "ERROR" + case clientError = "CLIENT_ERROR" + case serverError = "SERVER_ERROR" + case busy = "BUSY" + } + + public let kind: Kind + public let message: String + + public init(kind: Kind, message: String) { + self.kind = kind + self.message = message + } + + /// `CLIENT_ERROR unauthenticated` is what a server started with an auth file says to any command before login. + public var needsAuthentication: Bool { + kind == .clientError && message.lowercased().contains("unauthenticated") + } +} + +public enum MemcachedProtocolError: Error, Equatable, Sendable { + case malformedLine(String) + case lineTooLong +} + +/// Reads one reply from a byte stream fed in pieces. A reply can be large, so lines already read are kept rather +/// than scanned again when more bytes arrive. +public struct MemcachedReplyReader: Sendable { + public let shape: MemcachedReplyShape + private let lineLimit: Int + private var buffer: [UInt8] = [] + private var cursor = 0 + private var values: [MemcachedValue] = [] + private var lines: [String] = [] + private var pendingValue: (key: String, flags: UInt32, length: Int, unique: UInt64?)? + + private static let maximumLineLength = 64 * 1_024 + /// Memcached's largest item size setting, `-I 1024m`. A longer data block is not a Memcached reply. + public static let maximumValueLength = 1 << 30 + + public init(shape: MemcachedReplyShape, lineLimit: Int = .max) { + self.shape = shape + self.lineLimit = lineLimit + } + + public mutating func append(_ bytes: Bytes) where Bytes.Element == UInt8 { + if cursor > 1 << 16 { + buffer.removeFirst(cursor) + cursor = 0 + } + buffer.append(contentsOf: bytes) + } + + /// The reply once it is whole, or nil while more bytes are needed. + public mutating func reply() throws -> MemcachedReply? { + while true { + if let pending = pendingValue { + guard buffer.count - cursor >= pending.length + 2 else { return nil } + let data = Data(buffer[cursor ..< cursor + pending.length]) + guard buffer[cursor + pending.length] == 0x0D, buffer[cursor + pending.length + 1] == 0x0A else { + throw MemcachedProtocolError.malformedLine("VALUE \(pending.key)") + } + cursor += pending.length + 2 + values.append(MemcachedValue(key: pending.key, flags: pending.flags, data: data, unique: pending.unique)) + pendingValue = nil + continue + } + guard let line = try nextLine() else { return nil } + if let error = Self.serverError(line) { throw error } + switch shape { + case .status: + return .status(line) + case .lines: + if line == "END" { return .lines(lines, truncated: false) } + if line == "OK", lines.isEmpty { continue } + lines.append(line) + if lines.count >= lineLimit { return .lines(lines, truncated: true) } + case .values: + if line == "END" { return .values(values) } + pendingValue = try Self.valueHeader(line) + } + } + } + + /// `lru_crawler metadump` ends its key lines with a bare LF and only `END` with CRLF, so a line ends at LF and + /// a CR before it is dropped. Data blocks are read by length, never as lines. + private mutating func nextLine() throws -> String? { + guard let newline = buffer[cursor...].firstIndex(of: 0x0A) else { + if buffer.count - cursor > Self.maximumLineLength { throw MemcachedProtocolError.lineTooLong } + return nil + } + let end = newline > cursor && buffer[newline - 1] == 0x0D ? newline - 1 : newline + let line = MemcachedCommandParser.text(Array(buffer[cursor ..< end])) + cursor = newline + 1 + return line + } + + static func serverError(_ line: String) -> MemcachedServerError? { + for kind in [MemcachedServerError.Kind.clientError, .serverError, .busy, .error] { + let name = kind.rawValue + if line == name { return MemcachedServerError(kind: kind, message: "") } + if line.hasPrefix(name + " ") { + return MemcachedServerError(kind: kind, message: String(line.dropFirst(name.count + 1))) + } + } + return nil + } + + private static func valueHeader(_ line: String) throws -> (key: String, flags: UInt32, length: Int, unique: UInt64?) { + let parts = line.split(separator: " ", omittingEmptySubsequences: true) + guard parts.count == 4 || parts.count == 5, parts[0] == "VALUE", + let flags = UInt32(parts[2]), let length = Int(parts[3]), (0 ... maximumValueLength).contains(length) else { + throw MemcachedProtocolError.malformedLine(line) + } + let unique = parts.count == 5 ? UInt64(parts[4]) : nil + return (String(parts[1]), flags, length, unique) + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedStatements.swift b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedStatements.swift new file mode 100644 index 0000000000..dd91bb1722 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProMemcachedCore/MemcachedStatements.swift @@ -0,0 +1,35 @@ +import Foundation + +/// The console lines a grid edit becomes. Each one parses back into the command it stands for. +public enum MemcachedStatements { + /// Memcached reads an exptime above 30 days as a Unix time rather than seconds from now. + public static let relativeExptimeLimit: Int64 = 60 * 60 * 24 * 30 + + /// The text protocol reads an exptime as a 32-bit number. + public static let maximumExptime = Int64(Int32.max) + + /// No TTL, or 0, keeps the item until it is evicted. A TTL past 30 days is sent as the time it ends. Nil when + /// the end falls past what the protocol can carry. + public static func exptime(forTTL ttl: Int64?, now: Int64) -> Int64? { + guard let ttl, ttl != 0 else { return 0 } + guard ttl > relativeExptimeLimit else { return ttl >= -maximumExptime ? ttl : nil } + let (end, overflow) = now.addingReportingOverflow(ttl) + return !overflow && end <= maximumExptime ? end : nil + } + + public static func set(key: String, flags: UInt32, exptime: Int64, value: String) -> String { + "set \(MemcachedCommandParser.argument(key)) \(flags) \(exptime) \(MemcachedCommandParser.quoted(value))" + } + + public static func set(key: String, flags: UInt32, exptime: Int64, bytes: Data) -> String { + "set \(MemcachedCommandParser.argument(key)) \(flags) \(exptime) \(MemcachedCommandParser.quoted(bytes: bytes))" + } + + public static func touch(key: String, exptime: Int64) -> String { + "touch \(MemcachedCommandParser.argument(key)) \(exptime)" + } + + public static func delete(key: String) -> String { + "delete \(MemcachedCommandParser.argument(key))" + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift index d749954aae..e919e4c3ba 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift @@ -328,6 +328,7 @@ public struct SQLLexicalProfile: Sendable, Hashable { "Elasticsearch": commandLineFamily, "Typesense": commandLineFamily, "Weaviate": commandLineFamily, + "Memcached": commandLineFamily, "Kafka": commandLineFamily, "Firestore": commandLineFamily, "Firebase Realtime Database": commandLineFamily, diff --git a/apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedCommandParserTests.swift b/apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedCommandParserTests.swift new file mode 100644 index 0000000000..610ddcea26 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedCommandParserTests.swift @@ -0,0 +1,135 @@ +import Foundation +import TableProMemcachedCore +import Testing + +struct MemcachedCommandParserTests { + @Test("A storage command takes the value as its last argument and sends it as a data block") + func storeIsDataBlock() throws { + let command = try MemcachedCommandParser.parse(#"set user:1 5 300 "hello world""#) + #expect(command == .store(.set, key: "user:1", flags: 5, exptime: 300, value: Data("hello world".utf8))) + #expect(String(bytes: command.wireBytes, encoding: .utf8) == "set user:1 5 300 11\r\nhello world\r\n") + } + + @Test("A quoted value round-trips byte for byte", arguments: [ + "plain", "two words", "line\nbreak\r\n", "tab\there", "quote \" and \\ slash", "bell\u{07}", "日本語", + ]) + func quotedRoundTrips(value: String) throws { + let command = try MemcachedCommandParser.parse("set k 0 0 \(MemcachedCommandParser.quoted(value))") + #expect(command == .store(.set, key: "k", flags: 0, exptime: 0, value: Data(value.utf8))) + } + + @Test("Bytes that are not UTF-8 round-trip through \\x escapes") + func bytesRoundTrip() throws { + let bytes = Data([0x00, 0xFF, 0xC3, 0x28, 0x41, 0x22]) + let command = try MemcachedCommandParser.parse("set k 0 0 \(MemcachedCommandParser.quoted(bytes: bytes))") + #expect(command == .store(.set, key: "k", flags: 0, exptime: 0, value: bytes)) + } + + @Test("Single quotes keep backslashes as written") + func singleQuotesAreLiteral() throws { + let command = try MemcachedCommandParser.parse(#"append k 0 0 'a\nb'"#) + #expect(command == .store(.append, key: "k", flags: 0, exptime: 0, value: Data(#"a\nb"#.utf8))) + } + + @Test("Reads, counters and admin commands parse", arguments: [ + ("get a b", MemcachedCommand.get(keys: ["a", "b"], withCAS: false)), + ("GETS a", .get(keys: ["a"], withCAS: true)), + ("gat 60 a b", .getAndTouch(exptime: 60, keys: ["a", "b"], withCAS: false)), + ("touch a 0", .touch(key: "a", exptime: 0)), + ("delete a", .delete(key: "a")), + ("incr hits 3", .increment(key: "hits", delta: 3)), + ("decr hits 1", .decrement(key: "hits", delta: 1)), + ("flush_all", .flushAll(delay: nil)), + ("flush_all 10", .flushAll(delay: 10)), + ("stats", .stats(arguments: [])), + ("stats cachedump 1 0", .stats(arguments: ["cachedump", "1", "0"])), + ("version", .version), + ("lru_crawler metadump all", .metadump(selector: "all")), + ]) + func parses(text: String, expected: MemcachedCommand) throws { + #expect(try MemcachedCommandParser.parse(text) == expected) + } + + @Test("cas puts the unique after the byte count") + func casWire() throws { + let command = try MemcachedCommandParser.parse("cas k 0 0 42 v") + #expect(String(bytes: command.wireBytes, encoding: .utf8) == "cas k 0 0 1 42\r\nv\r\n") + } + + @Test("A key with a space or over 250 bytes is refused before it reaches the server") + func invalidKeys() { + #expect(throws: MemcachedCommandError.invalidKey("a b")) { try MemcachedCommandParser.parse(#"get "a b""#) } + let long = String(repeating: "k", count: 251) + #expect(throws: MemcachedCommandError.invalidKey(long)) { try MemcachedCommandParser.parse("delete \(long)") } + } + + @Test("A wrong argument count names the usage") + func wrongArguments() { + #expect(throws: MemcachedCommandError.wrongArguments( + command: "set", usage: "set " + )) { try MemcachedCommandParser.parse("set k 0 0") } + #expect(throws: MemcachedCommandError.unknownCommand("shutdown")) { try MemcachedCommandParser.parse("shutdown") } + #expect(throws: MemcachedCommandError.unterminatedQuote) { try MemcachedCommandParser.parse(#"set k 0 0 "x"#) } + #expect(throws: MemcachedCommandError.invalidNumber("-1")) { try MemcachedCommandParser.parse("set k -1 0 v") } + } + + @Test("A stats argument cannot smuggle a second command") + func statsArgumentsArePlain() { + #expect(throws: MemcachedCommandError.self) { try MemcachedCommandParser.parse("stats \"items\r\nflush_all\"") } + } +} + +struct MemcachedStatementsTests { + @Test("A TTL past 30 days is sent as the Unix time it ends, since memcached reads it that way") + func longTTLIsAbsolute() { + #expect(MemcachedStatements.exptime(forTTL: nil, now: 1_000) == 0) + #expect(MemcachedStatements.exptime(forTTL: 0, now: 1_000) == 0) + #expect(MemcachedStatements.exptime(forTTL: 300, now: 1_000) == 300) + #expect(MemcachedStatements.exptime(forTTL: MemcachedStatements.relativeExptimeLimit, now: 1_000) + == MemcachedStatements.relativeExptimeLimit) + #expect(MemcachedStatements.exptime(forTTL: MemcachedStatements.relativeExptimeLimit + 1, now: 1_000) + == 1_000 + MemcachedStatements.relativeExptimeLimit + 1) + } + + @Test("A TTL whose end the protocol cannot carry is refused rather than overflowing", arguments: [ + Int64.max, Int64(Int32.max), Int64.min, + ]) + func hugeTTL(ttl: Int64) { + #expect(MemcachedStatements.exptime(forTTL: ttl, now: 1_793_500_000) == nil) + } + + @Test("Each write statement parses back into its command") + func statementsParse() throws { + #expect(try MemcachedCommandParser.parse(MemcachedStatements.set(key: "k", flags: 3, exptime: 60, value: "a \"b\"\n")) + == .store(.set, key: "k", flags: 3, exptime: 60, value: Data("a \"b\"\n".utf8))) + #expect(try MemcachedCommandParser.parse(MemcachedStatements.set(key: "k", flags: 0, exptime: 0, bytes: Data([0xFF]))) + == .store(.set, key: "k", flags: 0, exptime: 0, value: Data([0xFF]))) + #expect(try MemcachedCommandParser.parse(MemcachedStatements.touch(key: "k", exptime: 10)) == .touch(key: "k", exptime: 10)) + #expect(try MemcachedCommandParser.parse(MemcachedStatements.delete(key: "k")) == .delete(key: "k")) + } +} + +struct MemcachedKeyQuotingTests { + @Test("A key holding a quote or backslash is quoted, so the statement addresses that key", arguments: [ + "user'1", "a\"b\"", "back\\slash", "plain:key", + ]) + func keysRoundTrip(key: String) throws { + #expect(try MemcachedCommandParser.parse(MemcachedStatements.delete(key: key)) == .delete(key: key)) + #expect(try MemcachedCommandParser.parse(MemcachedStatements.touch(key: key, exptime: 5)) == .touch(key: key, exptime: 5)) + #expect(try MemcachedCommandParser.parse(MemcachedStatements.set(key: key, flags: 0, exptime: 0, value: "v")) + == .store(.set, key: key, flags: 0, exptime: 0, value: Data("v".utf8))) + } + + @Test("A plain key is written as it is") + func plainKey() { + #expect(MemcachedStatements.delete(key: "user:1") == "delete user:1") + } + + @Test("stats forms that change the server answer one status line", arguments: [ + ("stats reset", MemcachedReplyShape.status), ("stats detail on", .status), ("stats detail off", .status), + ("stats sizes_enable", .status), ("stats detail dump", .lines), ("stats items", .lines), ("stats", .lines), + ]) + func statsShapes(statement: String, shape: MemcachedReplyShape) throws { + #expect(try MemcachedCommandParser.parse(statement).replyShape == shape) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedKeyListingTests.swift b/apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedKeyListingTests.swift new file mode 100644 index 0000000000..ff68599232 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedKeyListingTests.swift @@ -0,0 +1,121 @@ +import Foundation +import TableProMemcachedCore +import Testing + +struct MemcachedKeyListingTests { + @Test("A metadump line gives the decoded key, its expiry and flags") + func metadump() { + let entry = MemcachedKeyListing.metadumpEntry( + "key=a%3A1 exp=1793500167 la=1793499867 cas=2 fetch=yes cls=1 size=71 flags=5 " + ) + #expect(entry == MemcachedKeyEntry(key: "a:1", expiresAt: 1_793_500_167, flags: 5)) + #expect(entry?.secondsLeft(at: 1_793_499_867) == 300) + } + + @Test("exp=-1 means the item never expires") + func metadumpNeverExpires() { + let entry = MemcachedKeyListing.metadumpEntry("key=%22sp%22 exp=-1 la=1 cas=4 fetch=no cls=1 size=64 flags=0") + #expect(entry == MemcachedKeyEntry(key: "\"sp\"", expiresAt: nil, flags: 0)) + #expect(entry?.isExpired(at: .max) == false) + } + + @Test("A cachedump line gives the key and expiry; 0 means never") + func cachedump() { + #expect(MemcachedKeyListing.cachedumpEntry("ITEM k1 [2 b; 0 s]") == MemcachedKeyEntry( + key: "k1", expiresAt: nil, flags: nil + )) + #expect(MemcachedKeyListing.cachedumpEntry("ITEM user:42 [5 b; 1793500167 s]") == MemcachedKeyEntry( + key: "user:42", expiresAt: 1_793_500_167, flags: nil + )) + #expect(MemcachedKeyListing.cachedumpEntry("STAT pid 1") == nil) + } + + @Test("Slab classes come from the item counts in stats items") + func slabClasses() { + let lines = [ + "STAT items:1:number 3", "STAT items:1:age 10", "STAT items:12:number 1", "STAT items:5:number 0", + ] + #expect(MemcachedKeyListing.slabClasses(fromItemStats: lines) == [1, 12]) + } + + @Test("Stat pairs keep a value with spaces whole") + func statPairs() { + let pairs = MemcachedKeyListing.statPairs(["STAT version 1.6.45", "STAT slab_reassign_last_busy_status none x"]) + #expect(pairs.map(\.name) == ["version", "slab_reassign_last_busy_status"]) + #expect(pairs.last?.value == "none x") + } +} + +struct MemcachedBrowseSpecTests { + private let keys = ["user:2", "user:10", "session:a", "User:3"].map { + MemcachedKeyEntry(key: $0, expiresAt: nil, flags: nil) + } + + @Test("A spec survives its tagged statement") + func roundTrip() throws { + let condition = try MemcachedBrowseSpec.condition(column: "Key", op: "starts with", value: "user:", isCaseSensitive: true) + let spec = MemcachedBrowseSpec(limit: 50, offset: 100, ascending: false, conditions: [condition]) + #expect(MemcachedBrowseSpec.parse(spec.statement) == spec) + #expect(MemcachedBrowseSpec.parse("get a") == nil) + } + + @Test("A spec with a negative bound is not a page, and a huge limit stops at the last key") + func bounds() throws { + #expect(MemcachedBrowseSpec.parse(MemcachedBrowseSpec(limit: -1, offset: 0, ascending: true).statement) == nil) + #expect(MemcachedBrowseSpec.parse(MemcachedBrowseSpec(limit: 10, offset: -5, ascending: true).statement) == nil) + #expect(try MemcachedBrowseSpec(limit: .max, offset: 1, ascending: true).page(of: keys).count == 3) + } + + @Test("A page is filtered, sorted by key, then cut") + func page() throws { + let condition = try MemcachedBrowseSpec.condition(column: "Key", op: "STARTS WITH", value: "user:", isCaseSensitive: false) + let spec = MemcachedBrowseSpec(limit: 2, offset: 1, ascending: true, conditions: [condition]) + #expect(try spec.page(of: keys).map(\.key) == ["user:10", "user:2"]) + #expect(try MemcachedBrowseSpec(limit: 10, offset: 9, ascending: true).page(of: keys).isEmpty) + } + + @Test("Key comparisons", arguments: [ + ("=", "user:2", ["user:2"]), + ("!=", "user:2", ["User:3", "session:a", "user:10"]), + ("CONTAINS", "sion", ["session:a"]), + ("ENDS WITH", ":a", ["session:a"]), + ("IN", "user:2, session:a", ["session:a", "user:2"]), + ("NOT IN", "user:2,session:a", ["User:3", "user:10"]), + ("REGEX", "^user:[0-9]$", ["user:2"]), + ]) + func comparisons(op: String, value: String, expected: [String]) throws { + let condition = try MemcachedBrowseSpec.condition(column: "Key", op: op, value: value, isCaseSensitive: true) + let spec = MemcachedBrowseSpec(limit: 100, offset: 0, ascending: true, conditions: [condition]) + #expect(try spec.page(of: keys).map(\.key) == expected) + } + + @Test("OR keeps a key any condition matches") + func orLogic() throws { + let first = try MemcachedBrowseSpec.condition(column: "Key", op: "=", value: "user:2", isCaseSensitive: true) + let second = try MemcachedBrowseSpec.condition(column: "Key", op: "=", value: "User:3", isCaseSensitive: true) + let spec = MemcachedBrowseSpec(limit: 100, offset: 0, ascending: true, conditions: [first, second], matchesAll: false) + #expect(try spec.page(of: keys).map(\.key) == ["User:3", "user:2"]) + } + + @Test("Only Key filters, and a bad operator or pattern is refused") + func refusals() { + #expect(throws: MemcachedFilterRefusal.notKeyColumn("Value")) { + try MemcachedBrowseSpec.condition(column: "Value", op: "=", value: "x", isCaseSensitive: true) + } + #expect(throws: MemcachedFilterRefusal.unsupportedOperator(">")) { + try MemcachedBrowseSpec.condition(column: "Key", op: ">", value: "x", isCaseSensitive: true) + } + #expect(throws: MemcachedFilterRefusal.invalidPattern("[")) { + try MemcachedBrowseSpec.condition(column: "Key", op: "REGEX", value: "[", isCaseSensitive: true) + } + #expect(throws: MemcachedFilterRefusal.emptyList("IN")) { + try MemcachedBrowseSpec.condition(column: "Key", op: "IN", value: " , ", isCaseSensitive: true) + } + } + + @Test("A refusal statement carries its message") + func refusalStatement() { + let statement = MemcachedBrowseSpec.refusalStatement("Memcached can only filter by Key.") + #expect(MemcachedBrowseSpec.refusalMessage(statement) == "Memcached can only filter by Key.") + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedReplyReaderTests.swift b/apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedReplyReaderTests.swift new file mode 100644 index 0000000000..fde57f9fb4 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProMemcachedCoreTests/MemcachedReplyReaderTests.swift @@ -0,0 +1,79 @@ +import Foundation +import TableProMemcachedCore +import Testing + +struct MemcachedReplyReaderTests { + private func read(_ shape: MemcachedReplyShape, _ chunks: [String], lineLimit: Int = .max) throws -> MemcachedReply? { + var reader = MemcachedReplyReader(shape: shape, lineLimit: lineLimit) + var reply: MemcachedReply? + for chunk in chunks { + reader.append(Array(chunk.utf8)) + reply = try reader.reply() + } + return reply + } + + @Test("Values split across reads, with a CRLF inside the data, come back whole") + func valuesAcrossChunks() throws { + let reply = try read(.values, ["VALUE a 5 4\r\nx\r", "\ny\r\nVALUE b 0 0 7\r\n\r\nEN", "D\r\n"]) + #expect(reply == .values([ + MemcachedValue(key: "a", flags: 5, data: Data("x\r\ny".utf8)), + MemcachedValue(key: "b", flags: 0, data: Data(), unique: 7), + ])) + } + + @Test("A reply missing its END is not whole yet") + func waitsForEnd() throws { + #expect(try read(.values, ["VALUE a 0 1\r\nx\r\n"]) == nil) + #expect(try read(.lines, ["STAT pid 1\r\n"]) == nil) + } + + @Test("Stats lines, and the OK some servers send before a metadump, are read up to END") + func lines() throws { + #expect(try read(.lines, ["STAT pid 1\r\nSTAT uptime 20\r\nEND\r\n"]) + == .lines(["STAT pid 1", "STAT uptime 20"], truncated: false)) + #expect(try read(.lines, ["OK\r\nkey=a exp=-1\r\nEND\r\n"]) == .lines(["key=a exp=-1"], truncated: false)) + } + + @Test("A metadump ends its key lines with a bare LF and only END with CRLF") + func metadumpLineEndings() throws { + #expect(try read(.lines, ["OK\r\nkey=a exp=-1 \nkey=b", " exp=-1 \nEND\r\n"]) + == .lines(["key=a exp=-1 ", "key=b exp=-1 "], truncated: false)) + } + + @Test("The line limit stops a long listing early and says so") + func lineLimit() throws { + #expect(try read(.lines, ["key=a\r\nkey=b\r\nkey=c\r\n"], lineLimit: 2) == .lines(["key=a", "key=b"], truncated: true)) + } + + @Test("Error lines throw with the server's message", arguments: [ + ("ERROR\r\n", MemcachedServerError(kind: .error, message: "")), + ("CLIENT_ERROR unauthenticated\r\n", MemcachedServerError(kind: .clientError, message: "unauthenticated")), + ("SERVER_ERROR out of memory storing object\r\n", MemcachedServerError( + kind: .serverError, message: "out of memory storing object" + )), + ("CLIENT_ERROR lru crawler disabled\r\n", MemcachedServerError(kind: .clientError, message: "lru crawler disabled")), + ]) + func errors(line: String, expected: MemcachedServerError) { + #expect(throws: expected) { try read(.lines, [line]) } + } + + @Test("An unauthenticated refusal is recognized") + func needsAuthentication() { + #expect(MemcachedServerError(kind: .clientError, message: "unauthenticated").needsAuthentication) + #expect(!MemcachedServerError(kind: .clientError, message: "bad data chunk").needsAuthentication) + } + + @Test("A status reply is its one line") + func status() throws { + #expect(try read(.status, ["STO", "RED\r\n"]) == .status("STORED")) + #expect(try read(.status, ["VERSION 1.6.45\r\n"]) == .status("VERSION 1.6.45")) + } + + @Test("A malformed VALUE header, or one announcing more than memcached can store, is a protocol error", arguments: [ + "VALUE a x 1\r\n", "VALUE a 0 9223372036854775807\r\n", "VALUE a 0 -1\r\n", + ]) + func malformedHeader(header: String) { + #expect(throws: MemcachedProtocolError.self) { try read(.values, [header]) } + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProModelsTests/DatabaseTypeTests.swift b/apple/Packages/TableProCore/Tests/TableProModelsTests/DatabaseTypeTests.swift index c125adbab9..2953d84f21 100644 --- a/apple/Packages/TableProCore/Tests/TableProModelsTests/DatabaseTypeTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProModelsTests/DatabaseTypeTests.swift @@ -63,7 +63,7 @@ struct DatabaseTypeTests { @Test("allKnownTypes contains all expected types") func allKnownTypesComplete() { - #expect(DatabaseType.allKnownTypes.count == 34) + #expect(DatabaseType.allKnownTypes.count == 35) #expect(DatabaseType.allKnownTypes.contains(.mysql)) #expect(DatabaseType.allKnownTypes.contains(.tidb)) #expect(DatabaseType.allKnownTypes.contains(.databend)) @@ -81,6 +81,7 @@ struct DatabaseTypeTests { #expect(DatabaseType.allKnownTypes.contains(.kafka)) #expect(DatabaseType.allKnownTypes.contains(.cloudflareR2SQL)) #expect(DatabaseType.allKnownTypes.contains(.weaviate)) + #expect(DatabaseType.allKnownTypes.contains(.memcached)) #expect(DatabaseType.allKnownTypes.contains(.firestore)) #expect(DatabaseType.allKnownTypes.contains(.firebaseRealtimeDatabase)) #expect(DatabaseType.allKnownTypes.contains(.bigtable)) @@ -108,6 +109,13 @@ struct DatabaseTypeTests { #expect(DatabaseType.weaviate.pluginTypeId == "Weaviate") } + @Test("Memcached resolves its icon and plugin type id") + func memcachedIdentity() { + #expect(DatabaseType.memcached.rawValue == "Memcached") + #expect(DatabaseType.memcached.iconName == "memorychip") + #expect(DatabaseType.memcached.pluginTypeId == "Memcached") + } + @Test("Both Firebase types resolve the Firebase icon and keep their own plugin type id") func firebaseIdentity() { #expect(DatabaseType.firestore.rawValue == "Firestore") diff --git a/apple/Plugins/MemcachedDriverPlugin/Info.plist b/apple/Plugins/MemcachedDriverPlugin/Info.plist new file mode 100644 index 0000000000..a9240bbf3f --- /dev/null +++ b/apple/Plugins/MemcachedDriverPlugin/Info.plist @@ -0,0 +1,12 @@ + + + + + TableProPluginKitVersion + TABLEPRO_PLUGINKIT_VERSION + TableProProvidesDatabaseTypeIds + + Memcached + + + diff --git a/apple/Plugins/MemcachedDriverPlugin/MemcachedConnection.swift b/apple/Plugins/MemcachedDriverPlugin/MemcachedConnection.swift new file mode 100644 index 0000000000..583a7e8213 --- /dev/null +++ b/apple/Plugins/MemcachedDriverPlugin/MemcachedConnection.swift @@ -0,0 +1,304 @@ +import Foundation +import NIOConcurrencyHelpers +import NIOCore +import NIOPosix +import NIOSSL +import TableProMemcachedCore +import TableProPluginKit + +/// Feeds the bytes of the one request in flight into its reader and resumes the caller once the reply is whole. +/// Every path that resumes goes through the lock and clears the continuation first, so it resumes once. +private final class MemcachedReplyHandler: ChannelInboundHandler, @unchecked Sendable { + typealias InboundIn = ByteBuffer + + private var reader: MemcachedReplyReader? + private var continuation: CheckedContinuation? + private var failure: Error? + private let lock = NIOLock() + + func expect( + _ shape: MemcachedReplyShape, + lineLimit: Int, + continuation: CheckedContinuation + ) -> Bool { + let refusal: Error? = lock.withLock { + if let failure { return failure } + reader = MemcachedReplyReader(shape: shape, lineLimit: lineLimit) + self.continuation = continuation + return nil + } + guard let refusal else { return true } + continuation.resume(throwing: refusal) + return false + } + + func channelRead(context: ChannelHandlerContext, data: NIOAny) { + let buffer = unwrapInboundIn(data) + let outcome: (CheckedContinuation, Result)? = lock.withLock { + guard continuation != nil else { return nil } + reader?.append(buffer.readableBytesView) + let result: Result + do { + guard let reply = try reader?.reply() else { return nil } + result = .success(reply) + } catch { + result = .failure(error) + } + defer { continuation = nil; reader = nil } + return continuation.map { ($0, result) } + } + guard let (waiting, result) = outcome else { return } + waiting.resume(with: result) + } + + func errorCaught(context: ChannelHandlerContext, error: Error) { + finish(with: error) + context.close(promise: nil) + } + + func channelInactive(context: ChannelHandlerContext) { + finish(with: MemcachedError.connectionClosed) + } + + func finish(with error: Error) { + let waiting = lock.withLock { () -> CheckedContinuation? in + if failure == nil { failure = error } + defer { continuation = nil; reader = nil } + return continuation + } + waiting?.resume(throwing: error) + } +} + +/// Resumes a connect's caller once, whichever of the connect and the cancellation arrives first. +private final class ConnectGate: @unchecked Sendable { + private var continuation: CheckedContinuation? + private var settled = false + private let lock = NIOLock() + + func arm(_ continuation: CheckedContinuation) { + let alreadySettled: Bool = lock.withLock { + if settled { return true } + self.continuation = continuation + return false + } + if alreadySettled { continuation.resume(throwing: CancellationError()) } + } + + func deliver(_ channel: Channel) -> Bool { + guard let waiting = take() else { return false } + waiting.resume(returning: channel) + return true + } + + func fail(_ error: Error) { take()?.resume(throwing: error) } + func cancel() { take()?.resume(throwing: CancellationError()) } + + private func take() -> CheckedContinuation? { + lock.withLock { + guard !settled else { return nil } + settled = true + defer { continuation = nil } + return continuation + } + } +} + +struct MemcachedEndpoint: Sendable { + let host: String + let port: Int + let ssl: SSLConfiguration + let username: String + let password: String + let connectTimeout: TimeAmount +} + +/// One TCP connection speaking the text protocol, one request at a time. The text protocol has no request ids, +/// so replies are matched to requests by order alone, and a queue keeps two callers from interleaving. +actor MemcachedConnection { + private let endpoint: MemcachedEndpoint + private var channel: Channel? + private var handler: MemcachedReplyHandler? + private var wireSlotIsTaken = false + private var waitingForWireSlot: [CheckedContinuation] = [] + private var requestTimeout: TimeAmount? + + /// A reply that has not finished by then closes the connection, since the text protocol cannot skip it. + func setRequestTimeout(seconds: Int) { + requestTimeout = seconds > 0 ? .seconds(Int64(seconds)) : nil + } + + init(endpoint: MemcachedEndpoint) { + self.endpoint = endpoint + } + + func open() async throws { + guard channel == nil else { return } + let sslContext = try Self.makeSSLContext(endpoint.ssl) + let serverHostname = endpoint.ssl.verifiesHostname ? endpoint.host : nil + let replyHandler = MemcachedReplyHandler() + let bootstrap = ClientBootstrap(group: NIOSingletons.posixEventLoopGroup) + .connectTimeout(endpoint.connectTimeout) + .channelOption(ChannelOptions.socketOption(.so_keepalive), value: 1) + .channelInitializer { channel in + do { + if let sslContext { + let tls = try NIOSSLClientHandler(context: sslContext, serverHostname: serverHostname) + try channel.pipeline.syncOperations.addHandler(tls) + } + try channel.pipeline.syncOperations.addHandler(replyHandler) + return channel.eventLoop.makeSucceededVoidFuture() + } catch { + return channel.eventLoop.makeFailedFuture(error) + } + } + + do { + channel = try await Self.connect(bootstrap, host: endpoint.host, port: endpoint.port) + } catch is CancellationError { + throw CancellationError() + } catch { + throw MemcachedError.connectFailed(Self.describeConnect(error)) + } + handler = replyHandler + + let requestTimeoutAfterOpen = requestTimeout + requestTimeout = endpoint.connectTimeout + defer { requestTimeout = requestTimeoutAfterOpen } + do { + try await authenticate() + } catch { + await close() + throw error + } + } + + /// A server started with `-Y ` refuses every command until a `set` carries the user and password. + /// Any other server would store them as an ordinary item, so they are sent only after a refusal. + private func authenticate() async throws { + do { + _ = try await send(.version) + return + } catch let error as MemcachedServerError where error.needsAuthentication { + guard !endpoint.username.isEmpty else { throw MemcachedError.authenticationRequired } + } + let login = MemcachedCommand.store( + .set, key: "tablepro", flags: 0, exptime: 0, value: Data("\(endpoint.username) \(endpoint.password)".utf8) + ) + do { + guard case .status("STORED") = try await send(login) else { throw MemcachedError.authenticationFailed } + } catch let error as MemcachedServerError where error.kind == .clientError { + throw MemcachedError.authenticationFailed + } + } + + private static func connect(_ bootstrap: ClientBootstrap, host: String, port: Int) async throws -> Channel { + let gate = ConnectGate() + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { continuation in + gate.arm(continuation) + bootstrap.connect(host: host, port: port).whenComplete { result in + switch result { + case .success(let channel): + if !gate.deliver(channel) { channel.close(promise: nil) } + case .failure(let error): + gate.fail(error) + } + } + } + } onCancel: { + gate.cancel() + } + } + + func close() async { + let existing = channel + handler?.finish(with: MemcachedError.notConnected) + channel = nil + handler = nil + try? await existing?.close() + } + + var isOpen: Bool { channel?.isActive == true } + + /// Sends one command and reads its reply. Cancelling closes the channel: once a request is written the text + /// protocol has no way to skip its reply, so the connection cannot be reused for the next one. + func send(_ command: MemcachedCommand, lineLimit: Int = 1_000_000) async throws -> MemcachedReply { + await claimWireSlot() + defer { releaseWireSlot() } + + guard let channel, let handler, channel.isActive else { throw MemcachedError.notConnected } + try Task.checkCancellation() + + let bytes = command.wireBytes + var buffer = channel.allocator.buffer(capacity: bytes.count) + buffer.writeBytes(bytes) + let deadline = requestTimeout.map { timeout in + channel.eventLoop.scheduleTask(in: timeout) { + handler.finish(with: MemcachedError.timedOut) + channel.close(promise: nil) + } + } + defer { deadline?.cancel() } + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { continuation in + guard handler.expect(command.replyShape, lineLimit: lineLimit, continuation: continuation) else { return } + channel.writeAndFlush(buffer).whenFailure { error in + handler.finish(with: MemcachedError.connectFailed(error.localizedDescription)) + } + } + } onCancel: { + channel.close(promise: nil) + } + } + + private func claimWireSlot() async { + guard wireSlotIsTaken else { + wireSlotIsTaken = true + return + } + await withCheckedContinuation { waitingForWireSlot.append($0) } + } + + private func releaseWireSlot() { + guard !waitingForWireSlot.isEmpty else { + wireSlotIsTaken = false + return + } + waitingForWireSlot.removeFirst().resume() + } + + private static func makeSSLContext(_ ssl: SSLConfiguration) throws -> NIOSSLContext? { + guard ssl.isEnabled else { return nil } + var configuration = TLSConfiguration.makeClientConfiguration() + switch ssl.mode { + case .verifyIdentity: configuration.certificateVerification = .fullVerification + case .verifyCa: configuration.certificateVerification = .noHostnameVerification + default: configuration.certificateVerification = .none + } + if ssl.verifiesCertificate, !ssl.caCertificatePath.isEmpty { + configuration.trustRoots = .certificates(try NIOSSLCertificate.fromPEMFile(ssl.caCertificatePath)) + } + if !ssl.clientCertificatePath.isEmpty { + let chain = try NIOSSLCertificate.fromPEMFile(ssl.clientCertificatePath) + configuration.certificateChain = chain.map { .certificate($0) } + } + if !ssl.clientKeyPath.isEmpty { + configuration.privateKey = .privateKey(try NIOSSLPrivateKey(file: ssl.clientKeyPath, format: .pem)) + } + return try NIOSSLContext(configuration: configuration) + } + + private static func describeConnect(_ error: Error) -> String { + if let channelError = error as? ChannelError, case .connectTimeout = channelError { + return String(localized: "the connection timed out") + } + if let ioError = error as? IOError, ioError.errnoCode == ECONNREFUSED { + return String(localized: "the connection was refused") + } + if error is NIOSSLError || error is NIOSSLExtraError { + return String(localized: "the TLS handshake failed") + } + return error.localizedDescription + } +} diff --git a/apple/Plugins/MemcachedDriverPlugin/MemcachedError.swift b/apple/Plugins/MemcachedDriverPlugin/MemcachedError.swift new file mode 100644 index 0000000000..813c3d6258 --- /dev/null +++ b/apple/Plugins/MemcachedDriverPlugin/MemcachedError.swift @@ -0,0 +1,91 @@ +import Foundation +import TableProMemcachedCore +import TableProPluginKit + +enum MemcachedError: Error, PluginDriverError { + case notConnected + case connectionClosed + case timedOut + case connectFailed(String) + case authenticationRequired + case authenticationFailed + case server(MemcachedServerError) + case command(MemcachedCommandError) + case filter(MemcachedFilterRefusal) + case refused(String) + case unexpectedReply(String) + + var pluginErrorMessage: String { + switch self { + case .notConnected: + return String(localized: "Not connected to Memcached") + case .connectionClosed: + return String(localized: "Memcached closed the connection") + case .timedOut: + return String(localized: "Memcached did not answer in time") + case .connectFailed(let reason): + return String(format: String(localized: "Could not connect to Memcached: %@"), reason) + case .authenticationRequired: + return String(localized: "This Memcached server requires a username and password") + case .authenticationFailed: + return String(localized: "Memcached rejected the username or password") + case .server(let error): + return error.message.isEmpty ? error.kind.rawValue : "\(error.kind.rawValue): \(error.message)" + case .command(let error): + return Self.describe(error) + case .filter(let refusal): + return Self.describe(refusal) + case .refused(let message): + return message + case .unexpectedReply(let line): + return String(format: String(localized: "Unexpected reply from Memcached: %@"), line) + } + } + + var errorDescription: String? { pluginErrorMessage } + + static func wrap(_ error: Error) -> Error { + switch error { + case let error as MemcachedServerError: + return error.needsAuthentication ? MemcachedError.authenticationRequired : MemcachedError.server(error) + case let error as MemcachedCommandError: + return MemcachedError.command(error) + case let error as MemcachedFilterRefusal: + return MemcachedError.filter(error) + case is MemcachedProtocolError: + return MemcachedError.unexpectedReply(String(describing: error)) + default: + return error + } + } + + private static func describe(_ error: MemcachedCommandError) -> String { + switch error { + case .empty: + return String(localized: "Enter a Memcached command") + case .unknownCommand(let name): + return String(format: String(localized: "Unsupported Memcached command: %@"), name) + case .wrongArguments(_, let usage): + return String(format: String(localized: "Usage: %@"), usage) + case .invalidKey(let key): + return String(format: String(localized: "'%@' is not a valid key: a key is 1 to 250 bytes with no spaces or control characters"), key) + case .invalidNumber(let text): + return String(format: String(localized: "'%@' is not a valid number here"), text) + case .unterminatedQuote: + return String(localized: "A quoted value is missing its closing quote") + } + } + + private static func describe(_ refusal: MemcachedFilterRefusal) -> String { + switch refusal { + case .notKeyColumn: + return String(localized: "Memcached can only filter by Key") + case .unsupportedOperator(let op): + return String(format: String(localized: "Memcached cannot filter keys with %@"), op) + case .emptyList(let op): + return String(format: String(localized: "Enter at least one value to filter with %@"), op) + case .invalidPattern(let pattern): + return String(format: String(localized: "'%@' is not a valid regular expression"), pattern) + } + } +} diff --git a/apple/Plugins/MemcachedDriverPlugin/MemcachedPlugin.swift b/apple/Plugins/MemcachedDriverPlugin/MemcachedPlugin.swift new file mode 100644 index 0000000000..16344b92ff --- /dev/null +++ b/apple/Plugins/MemcachedDriverPlugin/MemcachedPlugin.swift @@ -0,0 +1,70 @@ +import Foundation +import TableProPluginKit + +final class MemcachedPlugin: NSObject, TableProPlugin, DriverPlugin { + static let pluginName = "Memcached Driver" + static let pluginVersion = "1.0.0" + static let pluginDescription = "Memcached support over the text protocol" + static let capabilities: [PluginCapability] = [.databaseDriver] + + static let databaseTypeId = "Memcached" + static let databaseDisplayName = "Memcached" + static let iconName = "memorychip" + static let defaultPort = 11_211 + static let isDownloadable = true + + static let navigationModel: NavigationModel = .standard + static let pathFieldRole: PathFieldRole = .database + static let requiresAuthentication = false + static let urlSchemes: [String] = ["memcached"] + static let brandColorHex = "#2A7AB0" + static let queryLanguageName = "Memcached" + static let editorLanguage: EditorLanguage = .bash + static let supportsForeignKeys = false + static let supportsSchemaEditing = false + static let supportsDatabaseSwitching = false + static let supportsImport = false + static let supportsSSL = true + static let tableEntityName = "Keys" + static let supportsForeignKeyDisable = false + static let supportsReadOnlyMode = true + static let databaseGroupingStrategy: GroupingStrategy = .flat + static let defaultGroupName = "default" + static let defaultPrimaryKeyColumn: String? = "Key" + static let structureColumnFields: [StructureColumnField] = [.name, .type, .nullable] + static let sqlDialect: SQLDialectDescriptor? = nil + static let caseSensitivityStyle: SQLDialectDescriptor.CaseSensitivityStyle = .driverManaged + static let columnTypesByCategory: [String: [String]] = ["String": ["String"], "Integer": ["Int64"]] + + /// The one table the sidebar shows. Memcached has no namespaces, so every key lives in it. + static let keysTable = "keys" + + static var statementCompletions: [CompletionEntry] { + [ + CompletionEntry(label: "get", insertText: "get "), + CompletionEntry(label: "gets", insertText: "gets "), + CompletionEntry(label: "gat", insertText: "gat "), + CompletionEntry(label: "set", insertText: "set "), + CompletionEntry(label: "add", insertText: "add "), + CompletionEntry(label: "replace", insertText: "replace "), + CompletionEntry(label: "append", insertText: "append "), + CompletionEntry(label: "prepend", insertText: "prepend "), + CompletionEntry(label: "cas", insertText: "cas "), + CompletionEntry(label: "touch", insertText: "touch "), + CompletionEntry(label: "delete", insertText: "delete "), + CompletionEntry(label: "incr", insertText: "incr "), + CompletionEntry(label: "decr", insertText: "decr "), + CompletionEntry(label: "flush_all", insertText: "flush_all"), + CompletionEntry(label: "stats", insertText: "stats"), + CompletionEntry(label: "stats items", insertText: "stats items"), + CompletionEntry(label: "stats slabs", insertText: "stats slabs"), + CompletionEntry(label: "stats settings", insertText: "stats settings"), + CompletionEntry(label: "version", insertText: "version"), + CompletionEntry(label: "lru_crawler metadump all", insertText: "lru_crawler metadump all"), + ] + } + + func createDriver(config: DriverConnectionConfig) -> any PluginDatabaseDriver { + MemcachedPluginDriver(config: config) + } +} diff --git a/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift b/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift new file mode 100644 index 0000000000..fb5fa15397 --- /dev/null +++ b/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift @@ -0,0 +1,445 @@ +import Foundation +import NIOCore +import os +import TableProMemcachedCore +import TableProPluginKit + +/// The text protocol has no command that lists keys. `lru_crawler metadump` (1.4.31 and later) walks every item, +/// so it lists them all; an older server, or one started with the crawler off, only offers `stats cachedump`, +/// which the server caps per slab class, so that list is a sample and the table says so. +final class MemcachedPluginDriver: PluginDatabaseDriver, @unchecked Sendable { + private static let logger = Logger(subsystem: "com.TablePro", category: "MemcachedPluginDriver") + + /// Keys one listing reads before it stops, so a cache holding millions of items cannot exhaust memory. + static let listingLimit = 1_000_000 + private static let valuesPerRequest = 100 + + private let endpoint: MemcachedEndpoint + private let lock = NSLock() + private var connection: MemcachedConnection? + private var _serverVersion: String? + private var keySource: MemcachedKeySource = .metadump + private var queryTimeout = 0 + private var shownExpiries: [String: MemcachedKnownExpiry] = [:] + private var serverClockOffset: Int64 = 0 + private static let shownExpiryLimit = 100_000 + + init(config: DriverConnectionConfig) { + endpoint = MemcachedEndpoint( + host: config.host, + port: config.port, + ssl: config.ssl, + username: config.username, + password: config.password, + connectTimeout: .seconds(10) + ) + } + + var serverVersion: String? { lock.withLock { _serverVersion } } + var supportsTransactions: Bool { false } + var capabilities: PluginCapabilities { [] } + + func beginTransaction() async throws {} + func commitTransaction() async throws {} + func rollbackTransaction() async throws {} + + func quoteIdentifier(_ name: String) -> String { name } + func escapeStringLiteral(_ value: String) -> String { value } + + // MARK: - Connection + + func connect() async throws { + let opened = MemcachedConnection(endpoint: endpoint) + do { + try await opened.open() + await opened.setRequestTimeout(seconds: lock.withLock { queryTimeout }) + let version = try await Self.version(on: opened) + let source = try await Self.keySource(on: opened) + lock.withLock { + connection = opened + _serverVersion = version + keySource = source + } + } catch { + await opened.close() + throw MemcachedError.wrap(error) + } + } + + func disconnect() { + let existing = lock.withLock { () -> MemcachedConnection? in + defer { connection = nil } + return connection + } + guard let existing else { return } + Task { await existing.close() } + } + + func ping() async throws { + _ = try await Self.version(on: try liveConnection()) + } + + func applyQueryTimeout(_ seconds: Int) async throws { + lock.withLock { queryTimeout = seconds } + await lock.withLock({ connection })?.setRequestTimeout(seconds: seconds) + } + + private func liveConnection() throws -> MemcachedConnection { + guard let connection = lock.withLock({ connection }) else { throw MemcachedError.notConnected } + return connection + } + + private static func version(on connection: MemcachedConnection) async throws -> String { + guard case .status(let line) = try await connection.send(.version), line.hasPrefix("VERSION ") else { + throw MemcachedError.unexpectedReply("version") + } + return String(line.dropFirst("VERSION ".count)) + } + + /// `stats settings` reports `lru_crawler yes` when the crawler runs. A server too old to report it has none. + private static func keySource(on connection: MemcachedConnection) async throws -> MemcachedKeySource { + guard case .lines(let lines, _) = try await connection.send(.stats(arguments: ["settings"])) else { + return .cachedump + } + let crawler = MemcachedKeyListing.statPairs(lines).first { $0.name == "lru_crawler" }?.value + return crawler == "yes" ? .metadump : .cachedump + } + + // MARK: - Execution + + func execute(query: String) async throws -> PluginQueryResult { + let started = Date() + let statement = query.trimmingCharacters(in: .whitespacesAndNewlines) + do { + if statement.lowercased() == "select 1" { + try await ping() + return result(columns: ["ok"], types: ["Int32"], rows: [[.text("1")]], started: started) + } + if let message = MemcachedBrowseSpec.refusalMessage(statement) { + throw MemcachedError.refused(message) + } + if let spec = MemcachedBrowseSpec.parse(statement) { + return try await browse(spec, started: started) + } + return try await run(try MemcachedCommandParser.parse(statement), started: started) + } catch { + throw MemcachedError.wrap(error) + } + } + + func executeParameterized(query: String, parameters: [PluginCellValue]) async throws -> PluginQueryResult { + try await execute(query: query) + } + + private func run(_ command: MemcachedCommand, started: Date) async throws -> PluginQueryResult { + let connection = try liveConnection() + if case .metadump = command { + return try await metadumpResult(command, started: started) + } + switch try await connection.send(command) { + case .values(let values): + let withCAS: Bool + switch command { + case .get(_, let cas), .getAndTouch(_, _, let cas): withCAS = cas + default: withCAS = false + } + let rows: [PluginRow] = values.map { value in + var row: PluginRow = [.text(value.key), Self.cell(value.data), .text(String(value.flags))] + if withCAS { row.append(PluginCellValue.fromOptional(value.unique.map(String.init))) } + return row + } + return result( + columns: ["Key", "Value", "Flags"] + (withCAS ? ["CAS"] : []), + types: ["String", "String", "Int64"] + (withCAS ? ["Int64"] : []), + rows: rows, started: started + ) + case .lines(let lines, _): + let pairs = MemcachedKeyListing.statPairs(lines) + guard pairs.count == lines.count else { + let rows: [PluginRow] = lines.map { [.text($0)] } + return result(columns: ["Line"], types: ["String"], rows: rows, started: started) + } + let rows: [PluginRow] = pairs.map { [.text($0.name), .text($0.value)] } + return result(columns: ["Name", "Value"], types: ["String", "String"], rows: rows, started: started) + case .status(let line): + let affected = ["STORED", "DELETED", "TOUCHED", "OK"].contains(line) || UInt64(line) != nil ? 1 : 0 + return result( + columns: ["Result"], types: ["String"], rows: [[.text(line)]], affected: affected, started: started + ) + } + } + + /// A metadump cannot share a connection with other commands, so it runs on its own. + private func metadumpResult(_ command: MemcachedCommand, started: Date) async throws -> PluginQueryResult { + let lines = try await withListingConnection { connection in + guard case .lines(let lines, _) = try await connection.send(command, lineLimit: Self.listingLimit) else { + return [String]() + } + return lines + } + let rows: [PluginRow] = lines.compactMap(MemcachedKeyListing.metadumpEntry).map { entry in + [ + .text(entry.key), + PluginCellValue.fromOptional(entry.expiresAt.map(String.init)), + PluginCellValue.fromOptional(entry.flags.map(String.init)), + ] + } + return result(columns: ["Key", "Expires", "Flags"], types: ["String", "Int64", "Int64"], rows: rows, started: started) + } + + // MARK: - Browsing + + private func browse(_ spec: MemcachedBrowseSpec, started: Date) async throws -> PluginQueryResult { + let connection = try liveConnection() + let serverTime = try await Self.serverTime(on: connection) + let listed = try await listKeys().filter { !$0.isExpired(at: serverTime) } + let page = try spec.page(of: listed) + + var values: [String: MemcachedValue] = [:] + var start = page.startIndex + while start < page.endIndex { + try Task.checkCancellation() + let end = min(start + Self.valuesPerRequest, page.endIndex) + let keys = page[start ..< end].map(\.key) + if case .values(let read) = try await connection.send(.get(keys: keys, withCAS: false)) { + for value in read { values[value.key] = value } + } + start = end + } + + lock.withLock { + serverClockOffset = serverTime - Int64(Date().timeIntervalSince1970) + if shownExpiries.count + page.count > Self.shownExpiryLimit { shownExpiries.removeAll() } + for entry in page { shownExpiries[entry.key] = MemcachedKnownExpiry(expiresAt: entry.expiresAt) } + } + + let rows: [PluginRow] = page.compactMap { entry in + guard let value = values[entry.key] else { return nil } + return [ + .text(entry.key), + Self.cell(value.data), + PluginCellValue.fromOptional(entry.secondsLeft(at: serverTime).map(String.init)), + .text(String(value.flags)), + ] + } + return result( + columns: MemcachedColumn.allCases.map(\.rawValue), + types: MemcachedColumn.allCases.map(\.typeName), + rows: rows, started: started + ) + } + + private func listKeys() async throws -> [MemcachedKeyEntry] { + if lock.withLock({ keySource }) == .metadump { + do { + return try await withListingConnection { connection in + let reply = try await connection.send(.metadump(selector: "all"), lineLimit: Self.listingLimit) + guard case .lines(let lines, _) = reply else { return [] } + return lines.compactMap(MemcachedKeyListing.metadumpEntry) + } + } catch let error as MemcachedServerError where error.kind != .busy && !error.needsAuthentication { + Self.logger.info("metadump refused, listing with cachedump") + lock.withLock { keySource = .cachedump } + } + } + return try await cachedumpKeys() + } + + private func cachedumpKeys() async throws -> [MemcachedKeyEntry] { + let connection = try liveConnection() + guard case .lines(let itemStats, _) = try await connection.send(.stats(arguments: ["items"])) else { return [] } + var entries: [MemcachedKeyEntry] = [] + for slab in MemcachedKeyListing.slabClasses(fromItemStats: itemStats) { + try Task.checkCancellation() + let reply = try await connection.send(.stats(arguments: ["cachedump", String(slab), "0"])) + guard case .lines(let lines, _) = reply else { continue } + entries += lines.compactMap(MemcachedKeyListing.cachedumpEntry) + if entries.count >= Self.listingLimit { break } + } + return entries + } + + /// Opens a connection for one listing and closes it after, even when the listing stopped early with the rest + /// of the reply still on the wire. + private func withListingConnection( + _ body: (MemcachedConnection) async throws -> T + ) async throws -> T { + let listing = MemcachedConnection(endpoint: endpoint) + do { + try await listing.open() + await listing.setRequestTimeout(seconds: lock.withLock { queryTimeout }) + let value = try await body(listing) + await listing.close() + return value + } catch { + await listing.close() + throw error + } + } + + private static func serverTime(on connection: MemcachedConnection) async throws -> Int64 { + guard case .lines(let lines, _) = try await connection.send(.stats(arguments: [])), + let time = MemcachedKeyListing.statPairs(lines).first(where: { $0.name == "time" }).flatMap({ Int64($0.value) }) + else { return Int64(Date().timeIntervalSince1970) } + return time + } + + private func stat(_ name: String) async throws -> String? { + guard case .lines(let lines, _) = try await liveConnection().send(.stats(arguments: [])) else { return nil } + return MemcachedKeyListing.statPairs(lines).first { $0.name == name }?.value + } + + // MARK: - Schema + + func fetchDatabases() async throws -> [String] { [MemcachedPlugin.defaultGroupName] } + + func fetchDatabaseMetadata(_ database: String) async throws -> PluginDatabaseMetadata { + let bytes = try await stat("bytes").flatMap { Int64($0) } + return PluginDatabaseMetadata(name: database, tableCount: 1, sizeBytes: bytes) + } + + func fetchTables(schema: String?) async throws -> [PluginTableInfo] { + let count = try await stat("curr_items").flatMap { Int($0) } + return [PluginTableInfo( + name: MemcachedPlugin.keysTable, type: "TABLE", rowCount: count, comment: listingComment + )] + } + + private var listingComment: String { + switch lock.withLock({ keySource }) { + case .metadump: + return String(localized: "Keys listed with lru_crawler metadump") + case .cachedump: + return String(localized: "Keys listed with stats cachedump, which returns a capped sample per slab class") + } + } + + func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { + [ + PluginColumnInfo(name: "Key", dataType: "String", isNullable: false, isPrimaryKey: true), + PluginColumnInfo(name: "Value", dataType: "String", isNullable: true), + PluginColumnInfo(name: "TTL", dataType: "Int64", isNullable: true), + PluginColumnInfo(name: "Flags", dataType: "Int64", isNullable: false), + ] + } + + func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] { + [MemcachedPlugin.keysTable: try await fetchColumns(table: MemcachedPlugin.keysTable, schema: schema)] + } + + func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { [] } + func fetchForeignKeys(table: String, schema: String?) async throws -> [PluginForeignKeyInfo] { [] } + + func fetchApproximateRowCount(table: String, schema: String?) async throws -> Int? { + try await stat("curr_items").flatMap { Int($0) } + } + + func fetchTableDDL(table: String, schema: String?) async throws -> String { + "// Memcached keys\n// \(listingComment)\n// Use 'get ' to read a value" + } + + func fetchViewDefinition(view: String, schema: String?) async throws -> String { + throw MemcachedError.refused(String(localized: "Memcached has no views")) + } + + func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata { + let count = try await stat("curr_items").flatMap { Int64($0) } + return PluginTableMetadata(tableName: table, rowCount: count, comment: listingComment, engine: "Memcached") + } + + // MARK: - Grid + + func buildBrowseQuery( + table: String, + sortColumns: [(columnIndex: Int, ascending: Bool)], + columns: [String], + limit: Int, + offset: Int + ) -> String? { + MemcachedBrowseSpec(limit: limit, offset: offset, ascending: Self.ascending(sortColumns)).statement + } + + func buildFilteredQuery( + table: String, + schema: String?, + queryFilters filters: [PluginQueryFilter], + logicMode: String, + sortColumns: [(columnIndex: Int, ascending: Bool)], + columns: [String], + limit: Int, + offset: Int, + columnKinds: [String: PluginColumnKind] + ) -> String? { + do { + var conditions: [MemcachedBrowseSpec.KeyCondition] = [] + for filter in filters { + conditions.append(try MemcachedBrowseSpec.condition( + column: filter.column, op: filter.op, value: filter.value, isCaseSensitive: filter.isCaseSensitive + )) + } + return MemcachedBrowseSpec( + limit: limit, offset: offset, ascending: Self.ascending(sortColumns), + conditions: conditions, matchesAll: logicMode.lowercased() != "or" + ).statement + } catch { + return MemcachedBrowseSpec.refusalStatement(MemcachedError.filter(error).pluginErrorMessage) + } + } + + private static func ascending(_ sortColumns: [(columnIndex: Int, ascending: Bool)]) -> Bool { + sortColumns.first { $0.columnIndex == 0 }?.ascending ?? true + } + + func defaultExportQuery(table: String) -> String? { + MemcachedBrowseSpec(limit: Self.listingLimit, offset: 0, ascending: true).statement + } + + func truncateTableStatements(table: String, schema: String?, cascade: Bool) -> [String]? { + ["flush_all"] + } + + func dropObjectStatement(name: String, objectType: String, schema: String?, cascade: Bool) -> String? { nil } + + func generateRowWrites( + table: String, + schema: String?, + columns: [String], + primaryKeyColumns: [String], + changes: [PluginRowChange], + insertedRowData: [Int: [PluginCellValue]], + deletedRowIndices: Set, + insertedRowIndices: Set + ) throws -> [PluginRowWrite]? { + let offset = lock.withLock { serverClockOffset } + let expiries = lock.withLock { shownExpiries } + let generator = MemcachedStatementGenerator( + columns: columns, + now: { Int64(Date().timeIntervalSince1970) + offset }, + knownExpiry: { expiries[$0] } + ) + return try generator.generateRowWrites( + from: changes, + insertedRowData: insertedRowData, + deletedRowIndices: deletedRowIndices, + insertedRowIndices: insertedRowIndices + ) + } + + // MARK: - Helpers + + private static func cell(_ data: Data) -> PluginCellValue { + String(data: data, encoding: .utf8).map(PluginCellValue.text) ?? .bytes(data) + } + + private func result( + columns: [String], types: [String], rows: [PluginRow], affected: Int = 0, started: Date + ) -> PluginQueryResult { + PluginQueryResult( + columns: columns, + columnTypeNames: types, + rows: rows, + rowsAffected: affected, + executionTime: Date().timeIntervalSince(started) + ) + } +} diff --git a/apple/Plugins/MemcachedDriverPlugin/MemcachedStatementGenerator.swift b/apple/Plugins/MemcachedDriverPlugin/MemcachedStatementGenerator.swift new file mode 100644 index 0000000000..a0e40fe779 --- /dev/null +++ b/apple/Plugins/MemcachedDriverPlugin/MemcachedStatementGenerator.swift @@ -0,0 +1,162 @@ +import Foundation +import TableProMemcachedCore +import TableProPluginKit + +/// When a key the grid showed expires, as the server put it. `expiresAt` nil means it never does. +struct MemcachedKnownExpiry: Sendable { + let expiresAt: Int64? +} + +/// Turns grid edits into console commands: `set` for a new row or a new value or flags, `touch` for a TTL alone, +/// `delete` for a removed row. The key names the item, so it is never rewritten in place. +struct MemcachedStatementGenerator { + let columns: [String] + /// The server's clock, so an absolute expiry is compared with the time the server keeps. + var now: () -> Int64 = { Int64(Date().timeIntervalSince1970) } + var knownExpiry: (String) -> MemcachedKnownExpiry? = { _ in nil } + + func generateRowWrites( + from changes: [PluginRowChange], + insertedRowData: [Int: [PluginCellValue]], + deletedRowIndices: Set, + insertedRowIndices: Set + ) throws -> [PluginRowWrite] { + var writes: [PluginRowWrite] = [] + for change in changes { + let statement: String? + switch change.type { + case .insert: + guard insertedRowIndices.contains(change.rowIndex) else { continue } + statement = try insertStatement(for: change, row: insertedRowData[change.rowIndex]) + case .update: + statement = try updateStatement(for: change) + case .delete: + guard deletedRowIndices.contains(change.rowIndex) else { continue } + statement = MemcachedStatements.delete(key: try originalKey(of: change)) + } + if let statement { writes.append(PluginRowWrite(statement: statement, rowIndices: [change.rowIndex])) } + } + return writes + } + + private func insertStatement(for change: PluginRowChange, row: [PluginCellValue]?) throws -> String { + var cells: [MemcachedColumn: PluginCellValue] = [:] + if let row { + for column in MemcachedColumn.allCases { + if let index = columns.firstIndex(of: column.rawValue), index < row.count { cells[column] = row[index] } + } + } else { + for cell in change.cellChanges { + if let column = MemcachedColumn(rawValue: cell.columnName) { cells[column] = cell.newValue } + } + } + guard let key = cells[.key]?.asText, !key.isEmpty else { + throw PluginRowWriteRefusal(rowIndex: change.rowIndex, reason: String(localized: "A new key needs a name.")) + } + guard MemcachedCommandParser.isValidKey(Array(key.utf8)) else { + throw PluginRowWriteRefusal( + rowIndex: change.rowIndex, + reason: String(localized: "A key is 1 to 250 bytes with no spaces or control characters.") + ) + } + let ttl = try number(cells[.ttl], of: .ttl, rowIndex: change.rowIndex) + return setStatement( + key: key, + flags: try number(cells[.flags], of: .flags, rowIndex: change.rowIndex) ?? 0, + exptime: try exptime(forTTL: ttl, rowIndex: change.rowIndex), + value: cells[.value] ?? .null + ) + } + + private func updateStatement(for change: PluginRowChange) throws -> String? { + guard !change.cellChanges.isEmpty else { return nil } + let key = try originalKey(of: change) + var edited: [MemcachedColumn: PluginCellValue] = [:] + for cell in change.cellChanges { + guard let column = MemcachedColumn(rawValue: cell.columnName) else { continue } + guard column != .key else { + throw PluginRowWriteRefusal( + rowIndex: change.rowIndex, + reason: String(localized: "A key cannot be renamed. Add a row with the new key instead.") + ) + } + edited[column] = cell.newValue + } + if let ttl = edited[.ttl], edited[.value] == nil, edited[.flags] == nil { + let seconds = try number(ttl, of: .ttl, rowIndex: change.rowIndex) + return MemcachedStatements.touch(key: key, exptime: try exptime(forTTL: seconds, rowIndex: change.rowIndex)) + } + guard edited[.value] != nil || edited[.flags] != nil else { return nil } + let exptime: Int64 + if let ttl = edited[.ttl] { + exptime = try self.exptime(forTTL: try number(ttl, of: .ttl, rowIndex: change.rowIndex), rowIndex: change.rowIndex) + } else { + exptime = try currentExptime(of: key, change: change) + } + return setStatement( + key: key, + flags: try number(edited[.flags] ?? original(.flags, of: change), of: .flags, rowIndex: change.rowIndex) ?? 0, + exptime: exptime, + value: edited[.value] ?? original(.value, of: change) ?? .null + ) + } + + /// A `set` replaces the expiry too, so one that leaves TTL alone sends the time the item already ends at, + /// rather than the TTL the grid read, which has been counting down since. + private func currentExptime(of key: String, change: PluginRowChange) throws -> Int64 { + guard let known = knownExpiry(key) else { + return try exptime(forTTL: try number(original(.ttl, of: change), of: .ttl, rowIndex: change.rowIndex), rowIndex: change.rowIndex) + } + guard let expiresAt = known.expiresAt else { return 0 } + guard expiresAt > now() else { + throw PluginRowWriteRefusal( + rowIndex: change.rowIndex, + reason: String(localized: "This item expired while the grid was open. Refresh to see whether it still exists.") + ) + } + return expiresAt + } + + private func setStatement(key: String, flags: Int64, exptime: Int64, value: PluginCellValue) -> String { + let flags = UInt32(flags) + if case .bytes(let data) = value { + return MemcachedStatements.set(key: key, flags: flags, exptime: exptime, bytes: data) + } + return MemcachedStatements.set(key: key, flags: flags, exptime: exptime, value: value.asText ?? "") + } + + private func exptime(forTTL ttl: Int64?, rowIndex: Int) throws -> Int64 { + guard let exptime = MemcachedStatements.exptime(forTTL: ttl, now: now()) else { + throw PluginRowWriteRefusal(rowIndex: rowIndex, reason: String(localized: "This TTL is too large for Memcached.")) + } + return exptime + } + + private func number(_ cell: PluginCellValue?, of column: MemcachedColumn, rowIndex: Int) throws -> Int64? { + guard let text = cell?.asText?.trimmingCharacters(in: .whitespaces), !text.isEmpty else { return nil } + let limit: ClosedRange = column == .flags ? 0 ... Int64(UInt32.max) : Int64.min ... Int64.max + guard let value = Int64(text), limit.contains(value) else { + throw PluginRowWriteRefusal( + rowIndex: rowIndex, + reason: String(format: String(localized: "%@ must be a whole number."), column.rawValue) + ) + } + return value + } + + private func originalKey(of change: PluginRowChange) throws -> String { + guard let key = original(.key, of: change)?.asText, MemcachedCommandParser.isValidKey(Array(key.utf8)) else { + throw PluginRowWriteRefusal( + rowIndex: change.rowIndex, + reason: String(localized: "This row's key cannot be addressed from the grid.") + ) + } + return key + } + + private func original(_ column: MemcachedColumn, of change: PluginRowChange) -> PluginCellValue? { + guard let index = columns.firstIndex(of: column.rawValue), + let row = change.originalRow, index < row.count else { return nil } + return row[index] + } +} diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MemcachedDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MemcachedDefaults.swift new file mode 100644 index 0000000000..c77d8f12a1 --- /dev/null +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+MemcachedDefaults.swift @@ -0,0 +1,64 @@ +import Foundation +import TableProPluginKit + +extension PluginMetadataRegistry { + func memcachedPluginDefaults() -> [(typeId: String, snapshot: PluginMetadataSnapshot)] { + [ + ("Memcached", PluginMetadataSnapshot( + displayName: "Memcached", iconName: "memorychip", defaultPort: 11_211, + requiresAuthentication: false, supportsForeignKeys: false, supportsSchemaEditing: false, + isDownloadable: true, primaryUrlScheme: "memcached", parameterStyle: .questionMark, + navigationModel: .standard, explainVariants: [], pathFieldRole: .database, + supportsHealthMonitor: true, urlSchemes: ["memcached"], postConnectActions: [], + brandColorHex: "#2A7AB0", + queryLanguageName: "Memcached", editorLanguage: .bash, + connectionMode: .network, supportsDatabaseSwitching: false, + capabilities: PluginMetadataSnapshot.CapabilityFlags( + supportsSchemaSwitching: false, + supportsImport: false, + supportsExport: true, + supportsSSH: true, + supportsSSL: true, + supportsCascadeDrop: false, + supportsForeignKeyDisable: false, + supportsReadOnlyMode: true, + supportsQueryProgress: false, + requiresReconnectForDatabaseSwitch: false, + supportsDropDatabase: false, + supportsOpportunisticTLS: false, + supportsColumnSort: false + ), + schema: PluginMetadataSnapshot.SchemaInfo( + defaultSchemaName: "", + defaultGroupName: "default", + tableEntityName: "Keys", + containerEntityName: "Server", + defaultPrimaryKeyColumn: "Key", + immutableColumns: ["Key"], + systemDatabaseNames: [], + systemSchemaNames: [], + fileExtensions: [], + databaseGroupingStrategy: .flat, + structureColumnFields: [.name, .type, .nullable] + ), + editor: PluginMetadataSnapshot.EditorConfig( + sqlDialect: nil, + statementCompletions: memcachedCompletions, + columnTypesByCategory: ["String": ["String"], "Integer": ["Int64"]] + ), + connection: PluginMetadataSnapshot.ConnectionConfig( + additionalConnectionFields: [], + category: .keyValue, + tagline: String(localized: "In-memory key-value cache"), + hidesBuiltInDatabase: true + ) + )) + ] + } +} + +private let memcachedCompletions: [CompletionEntry] = [ + "get", "gets", "gat", "set", "add", "replace", "append", "prepend", "cas", "touch", "delete", "incr", "decr", +].map { CompletionEntry(label: $0, insertText: "\($0) ") } + [ + "flush_all", "stats", "stats items", "stats slabs", "stats settings", "version", "lru_crawler metadump all", +].map { CompletionEntry(label: $0, insertText: $0) } diff --git a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift index 791d9304e4..5097398613 100644 --- a/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift +++ b/apple/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift @@ -1047,7 +1047,7 @@ extension PluginMetadataRegistry { + kafkaPluginDefaults() + typesensePluginDefaults() + r2SQLPluginDefaults() + weaviatePluginDefaults() + hanaPluginDefaults() + firebasePluginDefaults() + bigtablePluginDefaults() + neo4jPluginDefaults() - + trinoPluginDefaults() + + trinoPluginDefaults() + memcachedPluginDefaults() } // swiftlint:enable function_body_length } diff --git a/apple/TablePro/Core/ServerDashboard/Providers/MemcachedDashboardProvider.swift b/apple/TablePro/Core/ServerDashboard/Providers/MemcachedDashboardProvider.swift new file mode 100644 index 0000000000..80cd0c9a99 --- /dev/null +++ b/apple/TablePro/Core/ServerDashboard/Providers/MemcachedDashboardProvider.swift @@ -0,0 +1,85 @@ +// +// MemcachedDashboardProvider.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +/// Server metrics for Memcached, read from `stats`, which the driver returns as a Name and Value grid. +struct MemcachedDashboardProvider: ServerDashboardQueryProvider { + let supportedPanels: Set = [.serverMetrics] + + func fetchMetrics(execute: (String) async throws -> QueryResult) async throws -> [DashboardMetric] { + let stats = Self.stats(from: try await execute("stats")) + func number(_ name: String) -> Int64? { stats[name].flatMap { Int64($0) } } + + var metrics: [DashboardMetric] = [] + if let used = number("bytes") { + let limit = number("limit_maxbytes") ?? 0 + metrics.append(DashboardMetric( + id: "memory", + label: String(localized: "Used Memory"), + value: ByteSizeFormatting.string(bytes: used), + unit: limit > 0 ? Self.share(used, of: limit) : "", + icon: "memorychip" + )) + } + if let items = stats["curr_items"] { + metrics.append(DashboardMetric( + id: "items", label: String(localized: "Items"), value: items, unit: "", icon: "key" + )) + } + if let hits = number("get_hits"), let misses = number("get_misses") { + let lookups = Double(hits + misses) + metrics.append(DashboardMetric( + id: "hit_ratio", + label: String(localized: "Hit Ratio"), + value: lookups > 0 ? String(format: "%.1f", Double(hits) / lookups * 100) : "0", + unit: "%", + icon: "bolt" + )) + } + if let evictions = stats["evictions"] { + metrics.append(DashboardMetric( + id: "evictions", label: String(localized: "Evictions"), value: evictions, unit: "", icon: "arrow.uturn.left" + )) + } + if let connections = stats["curr_connections"] { + metrics.append(DashboardMetric( + id: "connections", + label: String(localized: "Connections"), + value: connections, + unit: "", + icon: "person.2" + )) + } + if let uptime = number("uptime") { + metrics.append(DashboardMetric( + id: "uptime", + label: String(localized: "Uptime"), + value: DurationFormatting.string(seconds: Int(uptime)), + unit: "", + icon: "clock" + )) + } + return metrics + } + + func acceptsProcessId(_ processId: String) -> Bool { + false + } + + private static func stats(from result: QueryResult) -> [String: String] { + var stats: [String: String] = [:] + for row in result.rows where row.count >= 2 { + if let name = row[0].asText { stats[name] = row[1].asText } + } + return stats + } + + private static func share(_ used: Int64, of limit: Int64) -> String { + let share = (Double(used) / Double(limit)).formatted(.percent.precision(.fractionLength(0))) + return String(format: String(localized: "%@ used"), share) + } +} diff --git a/apple/TablePro/Core/ServerDashboard/ServerDashboardQueryProviderFactory.swift b/apple/TablePro/Core/ServerDashboard/ServerDashboardQueryProviderFactory.swift index 03c6fe62f4..2c34f93633 100644 --- a/apple/TablePro/Core/ServerDashboard/ServerDashboardQueryProviderFactory.swift +++ b/apple/TablePro/Core/ServerDashboard/ServerDashboardQueryProviderFactory.swift @@ -48,6 +48,8 @@ enum ServerDashboardQueryProviderFactory { return MongoDBDashboardProvider() case .redis: return RedisDashboardProvider(serverVersion: serverVersion) + case .memcached: + return MemcachedDashboardProvider() } } } @@ -64,6 +66,7 @@ private enum DashboardEngine { case oracle case mongodb case redis + case memcached init?(_ databaseType: DatabaseType) { switch databaseType { @@ -87,6 +90,8 @@ private enum DashboardEngine { self = .mongodb case .redis: self = .redis + case .memcached: + self = .memcached default: return nil } diff --git a/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift b/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift index dbc89e62f4..e75a0d2393 100644 --- a/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift +++ b/apple/TablePro/Core/Services/Execution/ExternalStatementGate.swift @@ -176,6 +176,7 @@ internal enum ExternalStatementGate { "ELASTICSEARCH_EXPORT:", "ELASTICSEARCH_SEARCH:", "ELASTICSEARCH_WRITE:", "ETCD_COUNT:", "ETCD_RANGE:", "ETCD_REFUSAL:", "FIREBASE_BROWSE:", + "MEMCACHED_BROWSE:", "MEMCACHED_REFUSAL:", "SPANNER_BROWSE:", "TYPESENSE_EXPORT:", "TYPESENSE_SEARCH:", "TYPESENSE_WRITE:", "WEAVIATE_EXPORT:", "WEAVIATE_SEARCH:", "WEAVIATE_WRITE:", diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier+Memcached.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+Memcached.swift new file mode 100644 index 0000000000..6792887c72 --- /dev/null +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier+Memcached.swift @@ -0,0 +1,36 @@ +// +// QueryClassifier+Memcached.swift +// TablePro +// + +import Foundation + +extension QueryClassifier { + private static let memcachedReadCommands: Set = ["get", "gets", "version"] + private static let memcachedDestructiveCommands: Set = ["delete", "flush_all"] + private static let memcachedReadingStats: Set = ["items", "slabs", "settings", "conns", "sizes", "cachedump"] + + /// Only the `stats` forms that read are safe: `stats reset` clears the counters and `stats detail on` changes + /// what the server records. `gat` reads a value and moves its expiry, so it writes. The driver strips quotes + /// before it sends a word, so they are stripped here too. + static func memcachedClassification(_ trimmed: String) -> QueryClassification { + if trimmed.hasPrefix("MEMCACHED_BROWSE:") || trimmed.hasPrefix("MEMCACHED_REFUSAL:") { return .safe } + let words = trimmed.split(whereSeparator: \.isWhitespace).map { + $0.lowercased().filter { $0 != "\"" && $0 != "'" } + } + guard let command = words.first else { return .safe } + let subcommand = words.dropFirst().first + switch command { + case "stats": + guard let subcommand, !memcachedReadingStats.contains(subcommand) else { return .safe } + if subcommand == "detail", words.dropFirst(2).first == "dump" { return .safe } + return QueryClassification(tier: .write, reachesFilesystemOrExecutesCode: false) + case "lru_crawler" where subcommand == "metadump": + return .safe + default: + if memcachedReadCommands.contains(command) { return .safe } + let tier: QueryTier = memcachedDestructiveCommands.contains(command) ? .destructive : .write + return QueryClassification(tier: tier, reachesFilesystemOrExecutesCode: false) + } + } +} diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift index e2a38b52df..07843be136 100644 --- a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift @@ -1014,6 +1014,8 @@ private extension QueryClassifier { return worstRequest(typesenseClassification) case .weaviate: return worstRequest(weaviateClassification) + case .memcached: + return worstRequest(memcachedClassification) case .firestore, .firebaseRealtimeDatabase: return worstRequest { firebaseClassification($0, databaseType: databaseType) } case .bigtable: diff --git a/apple/TablePro/Models/Connection/DatabaseType.swift b/apple/TablePro/Models/Connection/DatabaseType.swift index 644bf840e1..ef72bbc5e8 100644 --- a/apple/TablePro/Models/Connection/DatabaseType.swift +++ b/apple/TablePro/Models/Connection/DatabaseType.swift @@ -54,6 +54,7 @@ extension DatabaseType { static let trino = DatabaseType(rawValue: "Trino") static let presto = DatabaseType(rawValue: "Presto") static let weaviate = DatabaseType(rawValue: "Weaviate") + static let memcached = DatabaseType(rawValue: "Memcached") static let bigtable = DatabaseType(rawValue: "Bigtable") static let firestore = DatabaseType(rawValue: "Firestore") static let firebaseRealtimeDatabase = DatabaseType(rawValue: "Firebase Realtime Database") diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 892f55b355..956a6be91a 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -656,6 +656,18 @@ } } }, + "'%@' is not a valid key: a key is 1 to 250 bytes with no spaces or control characters" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "'%@' is not a valid number here" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "'%@' is not a valid regular expression" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "'%@' is not a valid regular expression." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -4313,6 +4325,8 @@ } }, "%@ must be a whole number." : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -15850,6 +15864,14 @@ } } }, + "A key cannot be renamed. Add a row with the new key instead." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "A key is 1 to 250 bytes with no spaces or control characters." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "A key needs a name." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -16329,6 +16351,10 @@ } } }, + "A quoted value is missing its closing quote" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "A quoted value is missing its closing quote." : { "extractionState" : "manual", "generatesSymbol" : false @@ -52499,6 +52525,10 @@ } } }, + "Could not connect to Memcached: %@" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Could not connect to Neo4j: %@" : { "extractionState" : "manual", "generatesSymbol" : false @@ -71777,6 +71807,10 @@ } } }, + "Enter a Memcached command" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Enter a name for the new table." : { "localizations" : { "fr" : { @@ -71962,6 +71996,10 @@ }, "Enter at least one value for %@." : { + }, + "Enter at least one value to filter with %@" : { + "extractionState" : "manual", + "generatesSymbol" : false }, "Enter at least one value to filter with %@." : { "extractionState" : "manual", @@ -74451,6 +74489,9 @@ }, "Everything selected was already in your library." : { + }, + "Evictions" : { + }, "Exact value" : { "localizations" : { @@ -89146,6 +89187,9 @@ } } } + }, + "Hit Ratio" : { + }, "Holds a bidirectional control, which can change the order the text is shown in" : { @@ -92392,6 +92436,9 @@ } } } + }, + "In-memory key-value cache" : { + }, "Inactive" : { "localizations" : { @@ -98922,6 +98969,14 @@ } } }, + "Keys listed with lru_crawler metadump" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Keys listed with stats cachedump, which returns a capped sample per slab class" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Keys only" : { "extractionState" : "manual", "generatesSymbol" : false, @@ -108640,6 +108695,30 @@ } } }, + "Memcached can only filter by Key" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Memcached cannot filter keys with %@" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Memcached closed the connection" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Memcached did not answer in time" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Memcached has no views" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, + "Memcached rejected the username or password" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Memory" : { "extractionState" : "manual", "generatesSymbol" : false @@ -121660,6 +121739,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Not connected to Memcached" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Not connected to MongoDB" : { "extractionState" : "manual", "generatesSymbol" : false, @@ -197421,6 +197504,10 @@ } } }, + "This item expired while the grid was open. Refresh to see whether it still exists." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "This item no longer exists." : { "localizations" : { "fr" : { @@ -198276,6 +198363,10 @@ } } }, + "This Memcached server requires a username and password" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "This operation is not supported" : { "localizations" : { "fr" : { @@ -198925,6 +199016,10 @@ } } }, + "This row's key cannot be addressed from the grid." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "This row's key cannot be matched, so it cannot be checked before writing." : { "localizations" : { "fr" : { @@ -199754,6 +199849,10 @@ } } }, + "This TTL is too large for Memcached." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "This user can grant this privilege to others." : { "localizations" : { "fr" : { @@ -204988,6 +205087,10 @@ } } }, + "Unexpected reply from Memcached: %@" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Unexpected response from the SSO portal for profile \"%@\"." : { "extractionState" : "manual", "generatesSymbol" : false, @@ -206839,6 +206942,10 @@ } } }, + "Unsupported Memcached command: %@" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Unsupported MongoDB method: %@" : { "extractionState" : "manual", "generatesSymbol" : false, @@ -207965,6 +208072,10 @@ } } }, + "Usage: %@" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Use" : { "extractionState" : "stale", "localizations" : { diff --git a/apple/TableProTests/Core/Execution/ExternalStatementGatePrivateRequestTests.swift b/apple/TableProTests/Core/Execution/ExternalStatementGatePrivateRequestTests.swift index 1e7e0924a5..b841a3bb27 100644 --- a/apple/TableProTests/Core/Execution/ExternalStatementGatePrivateRequestTests.swift +++ b/apple/TableProTests/Core/Execution/ExternalStatementGatePrivateRequestTests.swift @@ -75,6 +75,8 @@ struct ExternalStatementGatePrivateRequestTests { (.weaviate, "WEAVIATE_SEARCH:"), (.weaviate, "WEAVIATE_WRITE:"), (.weaviate, "WEAVIATE_EXPORT:"), + (.memcached, "MEMCACHED_BROWSE:"), + (.memcached, "MEMCACHED_REFUSAL:"), (.etcd, "ETCD_RANGE:"), (.etcd, "ETCD_COUNT:"), (.etcd, "ETCD_REFUSAL:"), diff --git a/apple/TableProTests/Core/Plugins/PluginMetadataRegistryTypeCountTests.swift b/apple/TableProTests/Core/Plugins/PluginMetadataRegistryTypeCountTests.swift index 5f20ffb7ca..9aea24bb71 100644 --- a/apple/TableProTests/Core/Plugins/PluginMetadataRegistryTypeCountTests.swift +++ b/apple/TableProTests/Core/Plugins/PluginMetadataRegistryTypeCountTests.swift @@ -11,7 +11,7 @@ struct PluginMetadataRegistryTypeCountTests { "libSQL", "MariaDB", "MongoDB", "MySQL", "Oracle", "PGlite", "PostgreSQL", "Redis", "Redshift", "SAP HANA", "ScyllaDB", "Snowflake", "Spanner", "SQL Server", "SQLite", "SurrealDB", "Teradata", "TiDB", "Trino", "Presto", "Turso", "Typesense", "Weaviate", "OceanBase", "StarRocks", "Firestore", "Firebase Realtime Database", "Bigtable", - "Neo4j" + "Neo4j", "Memcached" ] private static func builtInTypeIds() -> Set { diff --git a/apple/TableProTests/Core/ServerDashboard/MemcachedDashboardProviderTests.swift b/apple/TableProTests/Core/ServerDashboard/MemcachedDashboardProviderTests.swift new file mode 100644 index 0000000000..ce576feba2 --- /dev/null +++ b/apple/TableProTests/Core/ServerDashboard/MemcachedDashboardProviderTests.swift @@ -0,0 +1,51 @@ +// +// MemcachedDashboardProviderTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +struct MemcachedDashboardProviderTests { + private static func stats(_ pairs: [(String, String)]) -> QueryResult { + QueryResult( + columns: ["Name", "Value"], + columnTypes: [], + rows: pairs.map { [.text($0.0), .text($0.1)] }, + rowsAffected: 0, + executionTime: 0, + error: nil + ) + } + + @Test("Metrics come from one stats call") + func metricsFromStats() async throws { + var commands: [String] = [] + let metrics = try await MemcachedDashboardProvider().fetchMetrics { command in + commands.append(command) + return Self.stats([ + ("uptime", "3600"), ("curr_connections", "4"), ("curr_items", "12"), ("bytes", "1048576"), + ("limit_maxbytes", "67108864"), ("get_hits", "3"), ("get_misses", "1"), ("evictions", "0"), + ]) + } + #expect(commands == ["stats"]) + let byId = Dictionary(uniqueKeysWithValues: metrics.map { ($0.id, $0) }) + #expect(byId["items"]?.value == "12") + #expect(byId["hit_ratio"]?.value == "75.0") + #expect(byId["connections"]?.value == "4") + #expect(byId["evictions"]?.value == "0") + #expect(byId["memory"] != nil) + #expect(byId["uptime"] != nil) + } + + @Test("No lookups yet reads as a 0 hit ratio, and missing stats are left out") + func sparseStats() async throws { + let metrics = try await MemcachedDashboardProvider().fetchMetrics { _ in + Self.stats([("get_hits", "0"), ("get_misses", "0")]) + } + #expect(metrics.map(\.id) == ["hit_ratio"]) + #expect(metrics.first?.value == "0") + } +} diff --git a/apple/TableProTests/Core/ServerDashboard/ServerDashboardQueryProviderFactoryTests.swift b/apple/TableProTests/Core/ServerDashboard/ServerDashboardQueryProviderFactoryTests.swift index 7cfbc7f656..268e5f3732 100644 --- a/apple/TableProTests/Core/ServerDashboard/ServerDashboardQueryProviderFactoryTests.swift +++ b/apple/TableProTests/Core/ServerDashboard/ServerDashboardQueryProviderFactoryTests.swift @@ -34,7 +34,7 @@ struct ServerDashboardQueryProviderFactoryTests { func serverEnginesHaveOne() { let types: [DatabaseType] = [ .postgresql, .kingbasees, .mysql, .mariadb, .redshift, .cockroachdb, .mssql, .clickhouse, .oracle, .mongodb, - .redis, + .redis, .memcached, ] for type in types { #expect(ServerDashboardQueryProviderFactory.supportsDashboard(for: type), "\(type.rawValue)") diff --git a/apple/TableProTests/Core/Services/Query/AllTablesListingTests.swift b/apple/TableProTests/Core/Services/Query/AllTablesListingTests.swift index d5dabb8050..3e6dfbd3e4 100644 --- a/apple/TableProTests/Core/Services/Query/AllTablesListingTests.swift +++ b/apple/TableProTests/Core/Services/Query/AllTablesListingTests.swift @@ -28,7 +28,7 @@ struct AllTablesListingTests { } @Test("An engine that only highlights like MongoDB or Redis gets no shell command", arguments: [ - DatabaseType.elasticsearch, .typesense, .weaviate, .etcd, .cassandra + DatabaseType.elasticsearch, .typesense, .weaviate, .etcd, .memcached, .cassandra ]) func noListingForOtherEngines(databaseType: DatabaseType) { #expect(AllTablesListing.statement(databaseType: databaseType, pluginListing: nil) == nil) diff --git a/apple/TableProTests/Core/Utilities/SQL/QueryClassifierHardeningTests.swift b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierHardeningTests.swift index 6b36262694..c7f4837e41 100644 --- a/apple/TableProTests/Core/Utilities/SQL/QueryClassifierHardeningTests.swift +++ b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierHardeningTests.swift @@ -899,6 +899,28 @@ struct QueryClassifierNonSqlTests { #expect(!QueryClassifier.isWriteQuery(search, databaseType: .weaviate)) } + @Test("Memcached reads are safe, stores write, and delete and flush_all are destructive") + func memcachedTiers() { + #expect(!QueryClassifier.isWriteQuery("MEMCACHED_BROWSE:e30=", databaseType: .memcached)) + #expect(!QueryClassifier.isWriteQuery("get user:1 user:2", databaseType: .memcached)) + #expect(!QueryClassifier.isWriteQuery("stats items", databaseType: .memcached)) + #expect(!QueryClassifier.isWriteQuery("lru_crawler metadump all", databaseType: .memcached)) + #expect(QueryClassifier.classifyTier("stats reset", databaseType: .memcached) == .write) + #expect(QueryClassifier.classifyTier("stats \"reset\"", databaseType: .memcached) == .write) + #expect(QueryClassifier.classifyTier("stats 'detail' on", databaseType: .memcached) == .write) + #expect(QueryClassifier.classifyTier("stats sizes_enable", databaseType: .memcached) == .write) + #expect(!QueryClassifier.isWriteQuery("stats detail dump", databaseType: .memcached)) + #expect(!QueryClassifier.isWriteQuery("stats cachedump 1 0", databaseType: .memcached)) + #expect(!QueryClassifier.isWriteQuery("stats", databaseType: .memcached)) + #expect(QueryClassifier.classifyTier("lru_crawler disable", databaseType: .memcached) == .write) + #expect(QueryClassifier.classifyTier("set k 0 0 \"v\"", databaseType: .memcached) == .write) + #expect(QueryClassifier.classifyTier("gat 60 k", databaseType: .memcached) == .write) + #expect(QueryClassifier.classifyTier("touch k 0", databaseType: .memcached) == .write) + #expect(QueryClassifier.classifyTier("delete k", databaseType: .memcached) == .destructive) + #expect(QueryClassifier.classifyTier("FLUSH_ALL", databaseType: .memcached) == .destructive) + #expect(QueryClassifier.classifyTier("get k; flush_all", databaseType: .memcached) == .destructive) + } + @Test("Bigtable request lines are tiered by their verb, and a PUT that nulls a cell is destructive") func bigtableTiers() { #expect(!QueryClassifier.isWriteQuery("BIGTABLE_BROWSE:e30=", databaseType: .bigtable)) diff --git a/apple/TableProTests/Models/Query/QueryTabBaseQueryTests.swift b/apple/TableProTests/Models/Query/QueryTabBaseQueryTests.swift index 3e05253cff..d3dff2c4db 100644 --- a/apple/TableProTests/Models/Query/QueryTabBaseQueryTests.swift +++ b/apple/TableProTests/Models/Query/QueryTabBaseQueryTests.swift @@ -51,7 +51,7 @@ struct QueryTabBaseQueryTests { /// Their plugins build the browse query. Without one the tab must not be handed another engine's /// command: either the plugin builds it or the build throws. @Test("An engine that highlights like MongoDB or Redis is not browsed with their commands", arguments: [ - DatabaseType.elasticsearch, .typesense, .weaviate, .etcd + DatabaseType.elasticsearch, .typesense, .weaviate, .etcd, .memcached ]) func otherEnginesNeverGetShellBrowseQueries(databaseType: DatabaseType) { let query = try? QueryTab.buildBaseTableQuery(tableName: "products", databaseType: databaseType) diff --git a/apple/TableProTests/Views/Main/QueryParameterDetectionTests.swift b/apple/TableProTests/Views/Main/QueryParameterDetectionTests.swift index 1cd4d7e40c..e5b239c795 100644 --- a/apple/TableProTests/Views/Main/QueryParameterDetectionTests.swift +++ b/apple/TableProTests/Views/Main/QueryParameterDetectionTests.swift @@ -14,7 +14,7 @@ extension GlobalStateSuites { arguments: [ (DatabaseType.mysql, true), (.postgresql, true), (.sqlite, true), (.elasticsearch, false), (.typesense, false), (.weaviate, false), (.mongodb, false), - (.redis, false), (.etcd, false), (.surrealdb, false), (.kafka, false) + (.redis, false), (.etcd, false), (.memcached, false), (.surrealdb, false), (.kafka, false) ] ) func onlySQLBindsNamedParameters(type: DatabaseType, binds: Bool) { diff --git a/apple/changelog.d/3980.added.md b/apple/changelog.d/3980.added.md new file mode 100644 index 0000000000..981626415e --- /dev/null +++ b/apple/changelog.d/3980.added.md @@ -0,0 +1 @@ +Memcached support: browse keys with values and TTLs, edit them in the grid, and run text protocol commands. diff --git a/apple/project.yml b/apple/project.yml index 009af4d549..ba993e528f 100644 --- a/apple/project.yml +++ b/apple/project.yml @@ -1071,6 +1071,21 @@ targets: - package: swift-nio-ssl product: NIOSSL + MemcachedDriverPlugin: + templates: [DriverPlugin] + templateAttributes: + folder: MemcachedDriverPlugin + principalClass: MemcachedPlugin + dependencies: + - package: TableProCore + product: TableProMemcachedCore + - package: swift-nio + product: NIOCore + - package: swift-nio + product: NIOPosix + - package: swift-nio-ssl + product: NIOSSL + KingbaseESDriverPlugin: templates: [DriverPlugin, PostgreSQLLinkage] templateAttributes: @@ -1332,6 +1347,7 @@ aggregateTargets: - KafkaDriverPlugin - KingbaseESDriverPlugin - LibSQLDriverPlugin + - MemcachedDriverPlugin - MSSQLDriver - MongoDBDriver - Neo4jDriverPlugin diff --git a/apple/scripts/probes/check-memcached.sh b/apple/scripts/probes/check-memcached.sh new file mode 100755 index 0000000000..ad430b9403 --- /dev/null +++ b/apple/scripts/probes/check-memcached.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# +# Run the Memcached driver against a live server: connect, the auth file login, key listing through +# lru_crawler metadump and through the stats cachedump fallback, browse pages with values and TTLs, +# key filters, grid writes (set, touch, delete), console commands and stats. The probe writes keys +# under tablepro_probe: and deletes them after. +# +# Usage: +# scripts/probes/check-memcached.sh [host] [port] +# +# MEMCACHED_USER and MEMCACHED_PASSWORD log in to a server started with -Y . Start one with: +# docker run -d --name tablepro-memcached-probe -p 11211:11211 memcached:1.6 +# +# Exits 3 when no server answers. + +set -euo pipefail + +HOST="${1:-127.0.0.1}" +PORT="${2:-11211}" +HERE="$(cd "$(dirname "$0")" && pwd)" +APPLE="$(cd "$HERE/../.." && pwd)" + +if ! nc -z -w 3 "$HOST" "$PORT" 2>/dev/null; then + echo "No Memcached server answers at $HOST:$PORT." >&2 + exit 3 +fi + +WORK="${TMPDIR:-/tmp}/tablepro-memcached-probe" +rm -rf "$WORK/Sources" +mkdir -p "$WORK/Sources/probe" +cp "$HERE/memcached-probe.swift" "$WORK/Sources/probe/main.swift" +for file in "$APPLE"/Plugins/MemcachedDriverPlugin/*.swift; do + ln -s "$file" "$WORK/Sources/probe/$(basename "$file")" +done +cat > "$WORK/Package.swift" < [[String?]] { + result.rows.map { $0.map(\.asText) } +} + +let env = ProcessInfo.processInfo.environment +func refusal(user: String, password: String) async -> String? { + let attempt = MemcachedPluginDriver(config: DriverConnectionConfig( + host: env["MEMCACHED_HOST"] ?? "127.0.0.1", port: Int(env["MEMCACHED_PORT"] ?? "") ?? 11_211, + username: user, password: password, database: "" + )) + do { + try await attempt.connect() + attempt.disconnect() + return nil + } catch { + return error.localizedDescription + } +} +if env["MEMCACHED_USER"]?.isEmpty ?? true { + _ = await refusal(user: "probe-user", password: "probe-password") +} +if let user = env["MEMCACHED_USER"], !user.isEmpty { + let missing = await refusal(user: "", password: "") + check(missing?.contains("requires a username") == true, "no credentials: \(missing ?? "connected")") + let wrong = await refusal(user: user, password: "wrong") + check(wrong?.contains("rejected") == true, "wrong password: \(wrong ?? "connected")") +} +let driver = MemcachedPluginDriver(config: DriverConnectionConfig( + host: env["MEMCACHED_HOST"] ?? "127.0.0.1", + port: Int(env["MEMCACHED_PORT"] ?? "") ?? 11_211, + username: env["MEMCACHED_USER"] ?? "", + password: env["MEMCACHED_PASSWORD"] ?? "", + database: "" +)) +try await driver.connect() +print("version", driver.serverVersion ?? "nil") +check(driver.serverVersion != nil, "connects and reads the version") + +let prefix = "tablepro_probe:" +for index in 0 ..< 30 { + _ = try await driver.execute(query: "set \(prefix)\(String(format: "%02d", index)) \(index) 0 \"value \(index)\"") +} +_ = try await driver.execute(query: "set \(prefix)ttl 0 600 \"expires\"") +_ = try await driver.execute(query: "set \(prefix)multi 0 0 \"line one\\nline two \\\"quoted\\\"\"") + +let tables = try await driver.fetchTables(schema: nil) +print("listing", tables.first?.comment ?? "") +check(tables.map(\.name) == ["keys"], "one keys table") + +let condition = try MemcachedBrowseSpec.condition(column: "Key", op: "STARTS WITH", value: prefix, isCaseSensitive: true) +let firstPage = try await driver.execute(query: MemcachedBrowseSpec( + limit: 10, offset: 0, ascending: true, conditions: [condition] +).statement) +check(firstPage.columns == ["Key", "Value", "TTL", "Flags"], "browse columns") +check(texts(firstPage).first == ["\(prefix)00", "value 0", nil, "0"], "first row sorted by key") +check(firstPage.rows.count == 10, "page of 10") + +let thirdPage = try await driver.execute(query: MemcachedBrowseSpec( + limit: 10, offset: 20, ascending: true, conditions: [condition] +).statement) +check(texts(thirdPage).first?.first == "\(prefix)20", "offset 20 starts at key 20") +check(texts(thirdPage).first?[3] == "20", "flags read back") + +let ttlRow = try await driver.execute(query: MemcachedBrowseSpec(limit: 10, offset: 0, ascending: true, conditions: [ + try MemcachedBrowseSpec.condition(column: "Key", op: "=", value: "\(prefix)ttl", isCaseSensitive: true), +]).statement) +let ttl = texts(ttlRow).first?[2].flatMap { Int($0) } ?? 0 +check(ttl > 590 && ttl <= 600, "TTL counts down from 600 (\(ttl))") + +let keepExpiry = try driver.generateRowWrites( + table: "keys", schema: nil, columns: ["Key", "Value", "TTL", "Flags"], primaryKeyColumns: ["Key"], + changes: [PluginRowChange(rowIndex: 0, type: .update, cellChanges: [(1, "Value", .text("expires"), .text("changed"))], + originalRow: [.text("\(prefix)ttl"), .text("expires"), .text("600"), .text("0")])], + insertedRowData: [:], deletedRowIndices: [], insertedRowIndices: [] +) ?? [] +let keepStatement = keepExpiry.first?.statement ?? "" +let keepExptime = Int64(keepStatement.split(separator: " ").dropFirst(3).first ?? "") ?? 0 +check(keepExptime > MemcachedStatements.relativeExptimeLimit, "a value edit sends the absolute expiry it read (\(keepStatement))") +for write in keepExpiry { _ = try await driver.execute(query: write.statement) } +let keptRow = try await driver.execute(query: MemcachedBrowseSpec(limit: 10, offset: 0, ascending: true, conditions: [ + try MemcachedBrowseSpec.condition(column: "Key", op: "=", value: "\(prefix)ttl", isCaseSensitive: true), +]).statement) +let keptTTL = texts(keptRow).first?[2].flatMap { Int($0) } ?? 0 +check(texts(keptRow).first?[1] == "changed" && keptTTL > 0 && keptTTL <= ttl, "the value changed and the TTL kept counting down (\(keptTTL))") + +let multi = try await driver.execute(query: "get \(prefix)multi") +check(texts(multi).first?[1] == "line one\nline two \"quoted\"", "escaped value round-trips") + +let refusal = driver.buildFilteredQuery( + table: "keys", schema: nil, queryFilters: [PluginQueryFilter(column: "Value", op: "=", value: "x")], + logicMode: "and", sortColumns: [], columns: [], limit: 10, offset: 0, columnKinds: [:] +) ?? "" +do { + _ = try await driver.execute(query: refusal) + check(false, "a Value filter is refused") +} catch { + check("\(error.localizedDescription)".contains("Key"), "a Value filter is refused: \(error.localizedDescription)") +} + +let columns = ["Key", "Value", "TTL", "Flags"] +let original: [PluginCellValue] = [.text("\(prefix)05"), .text("value 5"), .null, .text("5")] +let writes = try driver.generateRowWrites( + table: "keys", schema: nil, columns: columns, primaryKeyColumns: ["Key"], + changes: [ + PluginRowChange(rowIndex: 0, type: .update, cellChanges: [(1, "Value", .text("value 5"), .text("edited"))], originalRow: original), + PluginRowChange(rowIndex: 1, type: .update, cellChanges: [(2, "TTL", .null, .text("120"))], originalRow: [ + .text("\(prefix)06"), .text("value 6"), .null, .text("6"), + ]), + PluginRowChange(rowIndex: 2, type: .delete, cellChanges: [], originalRow: [.text("\(prefix)07"), .text("value 7"), .null, .text("7")]), + PluginRowChange(rowIndex: 3, type: .insert, cellChanges: [], originalRow: nil), + ], + insertedRowData: [3: [.text("\(prefix)new"), .text("inserted"), .text("60"), .text("9")]], + deletedRowIndices: [2], insertedRowIndices: [3] +) ?? [] +for write in writes { _ = try await driver.execute(query: write.statement) } +let afterWrites = try await driver.execute(query: "get \(prefix)05 \(prefix)06 \(prefix)07 \(prefix)new") +let byKey = Dictionary(uniqueKeysWithValues: texts(afterWrites).map { ($0[0] ?? "", $0) }) +check(byKey["\(prefix)05"]?[1] == "edited" && byKey["\(prefix)05"]?[2] == "5", "set keeps flags") +check(byKey["\(prefix)06"]?[1] == "value 6", "touch keeps the value") +check(byKey["\(prefix)07"] == nil, "delete removes the key") +check(byKey["\(prefix)new"]?[1] == "inserted" && byKey["\(prefix)new"]?[2] == "9", "insert sets value and flags") +// A metadump walks the LRU while the server moves items between its segments, so an item bumped by the touch can +// be missed by one walk and listed by the next. +var ttls: [Int] = [] +for _ in 0 ..< 5 where ttls.count < 2 { + let touched = try await driver.execute(query: MemcachedBrowseSpec(limit: 10, offset: 0, ascending: true, conditions: [ + try MemcachedBrowseSpec.condition(column: "Key", op: "IN", value: "\(prefix)06,\(prefix)new", isCaseSensitive: true), + ]).statement) + ttls = texts(touched).map { Int($0[2] ?? "") ?? -1 } +} +check(ttls.count == 2 && ttls[0] > 110 && ttls[0] <= 120 && ttls[1] > 50 && ttls[1] <= 60, "TTLs from touch and insert \(ttls)") + +let incr = try await driver.execute(query: "set \(prefix)counter 0 0 \"41\"") +check(texts(incr).first?.first == "STORED", "set replies STORED") +let counted = try await driver.execute(query: "incr \(prefix)counter 1") +check(texts(counted).first?.first == "42", "incr replies the new value") +let stats = try await driver.execute(query: "stats") +check(stats.columns == ["Name", "Value"] && texts(stats).contains { $0.first == "curr_items" }, "stats as a grid") +do { + let metadump = try await driver.execute(query: "lru_crawler metadump all") + check(metadump.rows.count >= 30, "metadump lists the probe's keys (\(metadump.rows.count) rows)") +} catch { + check(tables.first?.comment?.contains("cachedump") == true, "metadump refused where the listing uses cachedump: \(error.localizedDescription)") +} +do { + _ = try await driver.execute(query: "set bad key 0 0 v") + check(false, "a bad statement is refused") +} catch { + check(true, "a bad statement is refused: \(error.localizedDescription)") +} +let leaked = try await driver.execute(query: "get tablepro") +check(leaked.rows.isEmpty, "credentials given to an open server are not stored as an item") +let slabs = try await driver.execute(query: "stats items") +if let slab = texts(slabs).compactMap({ $0[0] }).first(where: { $0.hasSuffix(":number") })?.split(separator: ":").dropFirst().first { + let dump = try await driver.execute(query: "stats cachedump \(slab) 5") + check(dump.columns == ["Line"] && texts(dump).first?.first??.hasPrefix("ITEM ") == true, "stats cachedump shows its ITEM lines") +} +let reset = try await driver.execute(query: "stats reset") +check(texts(reset).first?.first == "RESET", "stats reset answers one status line") +let quotedKey = "\(prefix)it's \"quoted\"" +let quotedWrites = try driver.generateRowWrites( + table: "keys", schema: nil, columns: columns, primaryKeyColumns: ["Key"], + changes: [PluginRowChange(rowIndex: 0, type: .insert, cellChanges: [], originalRow: nil)], + insertedRowData: [0: [.text(quotedKey.replacingOccurrences(of: " ", with: "_")), .text("q"), .null, .null]], + deletedRowIndices: [], insertedRowIndices: [0] +) ?? [] +for write in quotedWrites { _ = try await driver.execute(query: write.statement) } +let quotedRead = try await driver.execute(query: "get \(MemcachedCommandParser.argument(quotedKey.replacingOccurrences(of: " ", with: "_")))") +check(texts(quotedRead).first?[1] == "q", "a key with quotes is written and read back") +let notStored = try await driver.execute(query: "add \(prefix)00 0 0 \"x\"") +check(texts(notStored).first?.first == "NOT_STORED", "add on an existing key replies NOT_STORED") + +let keys = try await driver.execute(query: MemcachedBrowseSpec( + limit: 100, offset: 0, ascending: true, conditions: [condition] +).statement) +for row in texts(keys) { _ = try await driver.execute(query: MemcachedStatements.delete(key: row[0] ?? "")) } +try await driver.ping() +driver.disconnect() + +print(failures == 0 ? "ALL PASS" : "\(failures) FAILED") +exit(failures == 0 ? 0 : 1) From db3abe6615ebef24fdaf47c8019df0293787b886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Sun, 1 Nov 2026 18:01:37 +0700 Subject: [PATCH 84/85] fix(linux-structure): make each index statement recreate the index the server has (#3983) --- linux/ROADMAP.md | 2 +- linux/changelog.d/3983.fixed.md | 1 + .../src/ui/structure_tab/index_handlers.rs | 19 +- .../app/src/ui/structure_tab/index_object.rs | 9 - .../app/src/ui/structure_tab/indexes.rs | 33 +- .../app/src/ui/structure_tab/indexes_input.rs | 2 - linux/crates/app/src/ui/structure_tab/mod.rs | 5 +- .../crates/app/src/ui/structure_tab/tests.rs | 15 +- linux/crates/app/src/ui/workspace/mod.rs | 1 - linux/crates/core/src/ddl/ddl_dialect.rs | 10 +- linux/crates/core/src/ddl/tests.rs | 15 + linux/crates/core/src/meta/index_info.rs | 5 +- .../drivers/clickhouse/src/catalog/mod.rs | 7 +- .../clickhouse/tests/integration/metadata.rs | 12 +- .../crates/drivers/duckdb/src/catalog/mod.rs | 62 ++- .../drivers/duckdb/tests/integration/ddl.rs | 40 ++ linux/crates/drivers/mssql/src/connection.rs | 5 +- linux/crates/drivers/mssql/src/ddl.rs | 135 ++++- .../drivers/mssql/src/index_statements.rs | 480 ++++++++++++++++++ linux/crates/drivers/mssql/src/lib.rs | 1 + linux/crates/drivers/mssql/src/metadata.rs | 127 +---- .../mssql/tests/integration/metadata.rs | 96 ++-- linux/crates/drivers/mysql/src/catalog/mod.rs | 6 +- .../mysql/src/catalog/table_definition.rs | 229 ++++++++- linux/crates/drivers/mysql/src/metadata.rs | 155 +++--- .../mysql/tests/integration/flavour_suite.rs | 102 ++-- linux/crates/drivers/postgres/src/ddl.rs | 32 ++ .../drivers/postgres/src/metadata/indexes.rs | 122 ++++- .../drivers/postgres/src/metadata/mod.rs | 7 +- .../postgres/tests/integration/metadata.rs | 81 ++- linux/crates/drivers/sqlite/src/metadata.rs | 10 +- .../sqlite/tests/integration/metadata.rs | 32 +- .../test-support/src/conformance/cases/ddl.rs | 3 +- linux/po/vi.po | 2 +- 34 files changed, 1462 insertions(+), 401 deletions(-) create mode 100644 linux/changelog.d/3983.fixed.md create mode 100644 linux/crates/drivers/mssql/src/index_statements.rs diff --git a/linux/ROADMAP.md b/linux/ROADMAP.md index 7e927c5058..55a988c3b8 100644 --- a/linux/ROADMAP.md +++ b/linux/ROADMAP.md @@ -64,7 +64,7 @@ The app runs on GNOME 50 ([ADR 0015](docs/decisions/0015-gnome-50-platform-floor | Capability | Detail | |---|---| | Table definition | Columns, indexes, foreign keys and column comments | -| Index statements | Show CREATE INDEX and Copy CREATE INDEX on each index the table has: the server's own text on PostgreSQL, SQLite and DuckDB, and written from the catalog on MySQL, MariaDB, SQL Server, ClickHouse (as ALTER TABLE ADD INDEX), Cassandra and ScyllaDB. An index with no statement of its own, such as a MySQL primary key or a ClickHouse sorting key, offers none | +| Index statements | Show CREATE INDEX and Copy CREATE INDEX on each index the table has, as the statement that makes it again: the server's own text on PostgreSQL, MySQL, MariaDB, SQLite and DuckDB, and written from the catalog, storage and options included, on SQL Server, ClickHouse, Cassandra and ScyllaDB. An index a constraint owns, such as a primary key or a UNIQUE constraint, a partition's index attached to its parent's and a ClickHouse sorting key offer none. Show CREATE TABLE uses the same statements | | Changes | Create, rename and drop tables; add, drop and rename columns, and change their type, nullability, default, comment, primary key and auto-increment; add and drop indexes (column keys, unique, descending) and foreign keys. ClickHouse creates no tables, indexes or foreign keys and changes no primary key or auto-increment; SQL Server changes no auto-increment; SQLite keeps no column comments; Cassandra and ScyllaDB take no default or NOT NULL, and rename only key columns | | Review | The SQL is previewed, and a save that loses data or can stop halfway is reviewed before it runs | | SQLite rebuilds | Changes SQLite cannot ALTER rebuild the table in one transaction, keeping its rows, indexes, triggers, the views and triggers that name it and its AUTOINCREMENT counter, with foreign keys checked before commit. SQLite itself checks the views and triggers made again; a trigger that lists a dropped column or inserts by position is refused before Save | diff --git a/linux/changelog.d/3983.fixed.md b/linux/changelog.d/3983.fixed.md new file mode 100644 index 0000000000..6e3345152d --- /dev/null +++ b/linux/changelog.d/3983.fixed.md @@ -0,0 +1 @@ +Show CREATE TABLE on PostgreSQL and SQL Server dropped each index's method, options and storage. diff --git a/linux/crates/app/src/ui/structure_tab/index_handlers.rs b/linux/crates/app/src/ui/structure_tab/index_handlers.rs index 4309972ff3..28d47be261 100644 --- a/linux/crates/app/src/ui/structure_tab/index_handlers.rs +++ b/linux/crates/app/src/ui/structure_tab/index_handlers.rs @@ -1,5 +1,5 @@ use super::index_object::IndexObject; -use super::{IndexesInput, StructureEffects, StructureModel, StructureTabOutput}; +use super::{IndexesInput, StructureEffects, StructureModel}; impl StructureModel { pub(super) fn on_indexes(&mut self, input: IndexesInput, fx: &mut StructureEffects) { @@ -14,23 +14,6 @@ impl StructureModel { self.recompute_dirty_state(fx); } } - IndexesInput::CopyStatement(index) => { - let (Some(sql), Some(tab)) = (index.create_statement(), self.this.upgrade()) else { - return; - }; - let report = std::cell::RefCell::new(None); - crate::ui::clipboard::copy_text(&tab, sql, |message| { - report.replace(Some(message.to_owned())); - }); - if let Some(message) = report.into_inner() { - fx.output(StructureTabOutput::ShowToast(message)); - } - } - IndexesInput::ShowStatement(index) => { - if let Some(sql) = index.create_statement() { - fx.output(StructureTabOutput::OpenInEditor(sql)); - } - } } } } diff --git a/linux/crates/app/src/ui/structure_tab/index_object.rs b/linux/crates/app/src/ui/structure_tab/index_object.rs index 25e0368bb9..534b9b0d6b 100644 --- a/linux/crates/app/src/ui/structure_tab/index_object.rs +++ b/linux/crates/app/src/ui/structure_tab/index_object.rs @@ -49,13 +49,4 @@ impl IndexObject { pub(crate) fn info(&self) -> &IndexInfo { self.imp().info.get().unwrap_or(&UNSET) } - - /// The statement that creates the index as the server has it, ended - /// the way the editor runs it. An index added in this tab has none. - pub(crate) fn create_statement(&self) -> Option { - self.info() - .definition - .as_deref() - .map(|definition| format!("{definition};")) - } } diff --git a/linux/crates/app/src/ui/structure_tab/indexes.rs b/linux/crates/app/src/ui/structure_tab/indexes.rs index 75f0a853c0..baec3ebbfc 100644 --- a/linux/crates/app/src/ui/structure_tab/indexes.rs +++ b/linux/crates/app/src/ui/structure_tab/indexes.rs @@ -11,7 +11,7 @@ use tablepro_core::meta::IndexKey; use super::columns::gate; use super::index_object::IndexObject; -use super::{IndexesInput, StructureTabSender}; +use super::{IndexesInput, StructureTab, StructureTabOutput, StructureTabSender}; fn index_badge(label: &str, accent_class: &str) -> gtk::Label { let badge = gtk::Label::builder().label(label).valign(gtk::Align::Center).build(); @@ -64,39 +64,44 @@ pub(super) fn build_index_row( }); row.add_suffix(&remove_button); - if idx.definition.is_some() { - row.add_suffix(&statement_menu(object, sender)); + if let Some(definition) = &idx.definition { + row.add_suffix(&statement_menu(format!("{definition};"), sender)); } row } -/// Show and copy the statement that creates the index, which only an -/// index the table already has carries. -fn statement_menu(object: &IndexObject, sender: &StructureTabSender) -> gtk::MenuButton { +// Changes nothing, so it skips the edit lock; the page still dims while the table is read again. +fn statement_menu(statement: String, sender: &StructureTabSender) -> gtk::MenuButton { let menu = gio::Menu::new(); menu.append( - Some(&crate::i18n::gettext("Show _CREATE INDEX")), + Some(&crate::i18n::gettext("_Show CREATE INDEX")), Some("index.show-create"), ); menu.append( Some(&crate::i18n::gettext("_Copy CREATE INDEX")), Some("index.copy-create"), ); - let actions = gio::SimpleActionGroup::new(); - let entry = |name: &str, message: fn(IndexObject) -> IndexesInput| { - let (index, sender) = (object.downgrade(), sender.clone()); + let entry = |name: &str, act: fn(&StructureTab, String)| { + let (statement, sender) = (statement.clone(), sender.clone()); gio::ActionEntry::::builder(name) .activate(move |_, _, _| { - if let Some(index) = index.upgrade() { - sender.input(message(index)); + if let Some(tab) = sender.tab() { + act(&tab, statement.clone()); } }) .build() }; + let actions = gio::SimpleActionGroup::new(); actions.add_action_entries([ - entry("show-create", IndexesInput::ShowStatement), - entry("copy-create", IndexesInput::CopyStatement), + entry("show-create", |tab, sql| { + tab.emit_output(StructureTabOutput::OpenInEditor(sql)); + }), + entry("copy-create", |tab, sql| { + crate::ui::clipboard::copy_text(tab, sql, |message| { + tab.emit_output(StructureTabOutput::ShowToast(message.to_owned())); + }); + }), ]); let button = gtk::MenuButton::builder() .icon_name(crate::ui::icons::VIEW_MORE) diff --git a/linux/crates/app/src/ui/structure_tab/indexes_input.rs b/linux/crates/app/src/ui/structure_tab/indexes_input.rs index 0fca6fd6d9..fa8627c8d5 100644 --- a/linux/crates/app/src/ui/structure_tab/indexes_input.rs +++ b/linux/crates/app/src/ui/structure_tab/indexes_input.rs @@ -7,6 +7,4 @@ use super::index_object::IndexObject; pub(crate) enum IndexesInput { AddIndex(IndexInfo), RemoveIndex(IndexObject), - CopyStatement(IndexObject), - ShowStatement(IndexObject), } diff --git a/linux/crates/app/src/ui/structure_tab/mod.rs b/linux/crates/app/src/ui/structure_tab/mod.rs index 1013ac2b85..84b9eb6eab 100644 --- a/linux/crates/app/src/ui/structure_tab/mod.rs +++ b/linux/crates/app/src/ui/structure_tab/mod.rs @@ -163,6 +163,10 @@ impl StructureTabSender { tab.send(msg); } } + + fn tab(&self) -> Option { + self.tab.upgrade() + } } impl StructureTab { @@ -481,7 +485,6 @@ pub(crate) enum StructureTabOutput { }, /// A read of the definition started or ended, for the tab bar. LoadingChanged(bool), - /// SQL the user asked to see in an editor tab. OpenInEditor(String), } diff --git a/linux/crates/app/src/ui/structure_tab/tests.rs b/linux/crates/app/src/ui/structure_tab/tests.rs index 5e33335edd..99d8d7f89f 100644 --- a/linux/crates/app/src/ui/structure_tab/tests.rs +++ b/linux/crates/app/src/ui/structure_tab/tests.rs @@ -1963,13 +1963,15 @@ fn email_index(name: &str, definition: Option<&str>) -> tablepro_core::IndexInfo } } -/// An index the table has offers its statement to copy or open; one -/// only drafted in the tab has none to offer. +// The copy runs while a save from the window holds the draft, which refuses edits. #[gtk4::test] fn a_saved_index_shows_and_copies_its_create_statement() { let statement = "CREATE UNIQUE INDEX users_email ON public.users USING btree (email)"; - let mut table = definition("email"); - table.indexes = vec![email_index("users_email", Some(statement))]; + let table = TableDefinition { + columns: vec![integer_column("id", true), integer_column("email", false)], + indexes: vec![email_index("users_email", Some(statement))], + ..definition("id") + }; let mut script = postgres_script(); script.fetch_table_definition.always(FakeReply::ok(table)); let harness = Harness::open(StructureMode::Edit, script, []); @@ -1984,6 +1986,11 @@ fn a_saved_index_shows_and_copies_its_create_statement() { .tab() .send(IndexesInput::AddIndex(email_index("users_email_draft", None))); drain_main_context(); + let _saving = harness + .tracker() + .begin_save() + .expect("a save") + .expect("the drafted index is pending"); let menus: Vec = descendants(harness.tab()) .into_iter() diff --git a/linux/crates/app/src/ui/workspace/mod.rs b/linux/crates/app/src/ui/workspace/mod.rs index a2187c59a4..9978c4dbac 100644 --- a/linux/crates/app/src/ui/workspace/mod.rs +++ b/linux/crates/app/src/ui/workspace/mod.rs @@ -604,7 +604,6 @@ pub(crate) enum WorkspaceInput { schema: Option, table: String, }, - /// SQL the user asked to see, in a new editor tab of its own. OpenInEditor(String), DropTablePrompt { schema: Option, diff --git a/linux/crates/core/src/ddl/ddl_dialect.rs b/linux/crates/core/src/ddl/ddl_dialect.rs index 79d4c74c34..4fe8d919e9 100644 --- a/linux/crates/core/src/ddl/ddl_dialect.rs +++ b/linux/crates/core/src/ddl/ddl_dialect.rs @@ -79,6 +79,14 @@ pub trait DdlDialect: Send + Sync + std::fmt::Debug { /// server's own text where the engine keeps one, otherwise written /// from the definition. fn create_table_script(&self, definition: &TableDefinition) -> Result { - self.plan_create_table(&TableDraft::from_definition(definition)) + let mut draft = TableDraft::from_definition(definition); + // Keys alone lose an index's method, operator classes and storage. + let (stated, written): (Vec, Vec) = + draft.indexes.into_iter().partition(|index| index.definition.is_some()); + draft.indexes = written; + let mut plan = self.plan_create_table(&draft)?; + let stated = DdlPlan::from_sql(stated.into_iter().filter_map(|index| index.definition)); + plan.statements.extend(stated.statements); + Ok(plan) } } diff --git a/linux/crates/core/src/ddl/tests.rs b/linux/crates/core/src/ddl/tests.rs index a3117ea605..3d8d3eea55 100644 --- a/linux/crates/core/src/ddl/tests.rs +++ b/linux/crates/core/src/ddl/tests.rs @@ -194,6 +194,21 @@ fn diff_table_detects_each_change_kind() { ); } +#[test] +fn an_index_dropped_and_drafted_again_on_its_keys_is_replaced() { + let mut original = users(); + original.indexes[1].definition = Some("CREATE INDEX by_email ON app.users USING gin (email)".to_owned()); + let mut draft = TableDraft::from_definition(&original); + draft.indexes.retain(|index| index.name != "by_email"); + draft.indexes.push(index("by_email", &["email"])); + + let changes = diff_table(&original, &draft); + + let names = |indexes: &[IndexInfo]| indexes.iter().map(|index| index.name.clone()).collect::>(); + assert_eq!(names(&changes.dropped_indexes), ["by_email"]); + assert_eq!(names(&changes.added_indexes), ["by_email"]); +} + #[test] fn an_unedited_draft_changes_nothing() { let original = users(); diff --git a/linux/crates/core/src/meta/index_info.rs b/linux/crates/core/src/meta/index_info.rs index 9f66628a37..7bbe5ca51e 100644 --- a/linux/crates/core/src/meta/index_info.rs +++ b/linux/crates/core/src/meta/index_info.rs @@ -14,10 +14,7 @@ pub struct IndexInfo { /// A partial index's WHERE clause. An index with one cannot /// identify a row, because it does not cover every row. pub predicate: Option, - /// The statement that creates this index alone: the server's own - /// text where the engine keeps one, otherwise written by the driver - /// from its catalog, with no closing semicolon. `None` for an index - /// only a constraint makes. + // Guaranteed only from fetch_table_definition; None for an index a constraint owns. pub definition: Option, } diff --git a/linux/crates/drivers/clickhouse/src/catalog/mod.rs b/linux/crates/drivers/clickhouse/src/catalog/mod.rs index 52d9f372a6..a88669b2e0 100644 --- a/linux/crates/drivers/clickhouse/src/catalog/mod.rs +++ b/linux/crates/drivers/clickhouse/src/catalog/mod.rs @@ -5,6 +5,7 @@ mod table_definition; use clickhouse::query::Query; use tablepro_core::DriverError; +use tablepro_core::ddl::ddl_sql::parenthesized; use tablepro_core::meta::{IndexInfo, RowIdentity, TableEstimate}; pub(crate) use clickhouse_column_row::ClickHouseColumnRow; @@ -86,11 +87,13 @@ pub(crate) async fn fetch_indexes(calls: &CallClient, table: &CatalogTable) -> R for row in &skipping.rows { let row = CatalogRow(row); let (name, expression) = (row.text(0)?, row.text(1)?); + // A multi-column index lists its expressions bare: `n, lower(s)`. let definition = format!( - "ALTER TABLE {}.{} ADD INDEX {} {expression} TYPE {} GRANULARITY {}", + "CREATE INDEX {} ON {}.{} {} TYPE {} GRANULARITY {}", + quote(&name), quote(&table.database), quote(&table.name), - quote(&name), + parenthesized(&expression), row.text(2)?, row.optional_u64(3)?.unwrap_or(1) ); diff --git a/linux/crates/drivers/clickhouse/tests/integration/metadata.rs b/linux/crates/drivers/clickhouse/tests/integration/metadata.rs index ccbe220aa5..0c359ab323 100644 --- a/linux/crates/drivers/clickhouse/tests/integration/metadata.rs +++ b/linux/crates/drivers/clickhouse/tests/integration/metadata.rs @@ -298,9 +298,6 @@ async fn clickhouse_ilike_is_case_insensitive() { connection.close().await.expect("a close"); } -/// A skipping index's statement, run after the index is dropped, makes -/// the same index again with its type and granularity. The sorting key -/// has no statement of its own. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] async fn skipping_index_statements_recreate_their_indexes() { @@ -311,7 +308,8 @@ async fn skipping_index_statements_recreate_their_indexes() { connection.as_ref(), &[&format!( "CREATE TABLE {} (id UInt32, s String, \ - INDEX by_s lower(s) TYPE bloom_filter(0.01) GRANULARITY 4, INDEX by_id id TYPE minmax GRANULARITY 1) \ + INDEX by_s lower(s) TYPE bloom_filter(0.01) GRANULARITY 4, INDEX by_id id TYPE minmax GRANULARITY 1, \ + INDEX by_pair (id, s) TYPE set(100) GRANULARITY 2) \ ENGINE = MergeTree ORDER BY id", quoted(&name) )], @@ -334,10 +332,8 @@ async fn skipping_index_statements_recreate_their_indexes() { assert_eq!( by_s.definition.as_deref(), Some( - format!( - "ALTER TABLE `{DATABASE}`.`{name}` ADD INDEX `by_s` lower(s) TYPE bloom_filter(0.01) GRANULARITY 4" - ) - .as_str() + format!("CREATE INDEX `by_s` ON `{DATABASE}`.`{name}` (lower(s)) TYPE bloom_filter(0.01) GRANULARITY 4") + .as_str() ) ); for index in indexes.iter().filter(|index| !index.primary) { diff --git a/linux/crates/drivers/duckdb/src/catalog/mod.rs b/linux/crates/drivers/duckdb/src/catalog/mod.rs index b7f096d8d2..f21c78db32 100644 --- a/linux/crates/drivers/duckdb/src/catalog/mod.rs +++ b/linux/crates/drivers/duckdb/src/catalog/mod.rs @@ -274,6 +274,8 @@ pub(crate) fn fetch_indexes(link: &Link, catalog: &str, table: &TableRef) -> Res ORDER BY index_name", &[catalog, schema_of(table), &table.name], )?; + let quote = |name: &str| format!("\"{}\"", name.replace('"', "\"\"")); + let target = format!("{}.{}.{}", quote(catalog), quote(schema_of(table)), quote(&table.name)); indexes.extend(rows.iter().map(|row| { IndexInfo { name: text(row, 0), @@ -284,18 +286,55 @@ pub(crate) fn fetch_indexes(link: &Link, catalog: &str, table: &TableRef) -> Res unique: flag(row, 1), primary: flag(row, 2), predicate: None, - definition: statement(&text(row, 4)), + definition: statement(&text(row, 4)).and_then(|sql| on_own_table(&sql, &target)), } })); Ok(indexes) } -/// DuckDB keeps the text the index was created with, semicolon and all. +// DuckDB ends its index text with a semicolon, which the app adds itself. fn statement(sql: &str) -> Option { let sql = sql.trim().trim_end_matches(';').trim_end(); (!sql.is_empty()).then(|| sql.to_owned()) } +// DuckDB writes a table of schema main bare, and a temp table of the same name catches that and +// `main.t` alike: only the catalog's name reaches the database's own table. +fn on_own_table(sql: &str, target: &str) -> Option { + let rest = sql + .strip_prefix("CREATE UNIQUE INDEX ") + .or_else(|| sql.strip_prefix("CREATE INDEX "))?; + let rest = rest.get(identifier_length(rest)?..)?.strip_prefix(" ON ")?; + let start = sql.len() - rest.len(); + let end = start + reference_length(rest)?; + Some(format!("{}{target}{}", &sql[..start], &sql[end..])) +} + +fn reference_length(text: &str) -> Option { + let mut length = identifier_length(text)?; + while text.get(length..)?.starts_with('.') { + length += 1 + identifier_length(text.get(length + 1..)?)?; + } + Some(length) +} + +fn identifier_length(text: &str) -> Option { + let Some(quoted) = text.strip_prefix('"') else { + let length = text + .find(|character: char| !(character.is_alphanumeric() || character == '_')) + .unwrap_or(text.len()); + return (length > 0).then_some(length); + }; + let mut position = 0; + loop { + let close = position + quoted.get(position..)?.find('"')?; + match quoted.get(close + 1..)?.starts_with('"') { + true => position = close + 2, + false => return Some(close + 2), + } + } +} + /// One key of an index: a column where the text names one, an /// expression otherwise. fn index_key(text: &str, columns: &HashSet) -> IndexKey { @@ -639,6 +678,25 @@ mod tests { use super::*; + #[test] + fn an_index_statement_names_its_catalog() { + let target = "\"dd\".\"main\".\"t\""; + + assert_eq!( + on_own_table("CREATE INDEX i0 ON t(a)", target).as_deref(), + Some("CREATE INDEX i0 ON \"dd\".\"main\".\"t\"(a)") + ); + assert_eq!( + on_own_table( + "CREATE UNIQUE INDEX \"we\"\"ird\" ON s.\"T t\"((lower(b)))", + "\"dd\".\"s\".\"T t\"" + ) + .as_deref(), + Some("CREATE UNIQUE INDEX \"we\"\"ird\" ON \"dd\".\"s\".\"T t\"((lower(b)))") + ); + assert_eq!(on_own_table("CREATE INDEX \"open ON t(a)", target), None); + } + #[test] fn a_literal_default_reads_as_its_value() { let integer = column_type_from_text("INTEGER"); diff --git a/linux/crates/drivers/duckdb/tests/integration/ddl.rs b/linux/crates/drivers/duckdb/tests/integration/ddl.rs index 65883bc6bc..9731f5d1e2 100644 --- a/linux/crates/drivers/duckdb/tests/integration/ddl.rs +++ b/linux/crates/drivers/duckdb/tests/integration/ddl.rs @@ -282,3 +282,43 @@ async fn a_new_type_keeps_the_columns_collation() { ); connection.close().await.unwrap(); } + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn an_index_statement_makes_the_index_again_on_its_own_table() { + let file = TestFile::new(); + let connection = file.open().await; + apply( + connection.as_ref(), + &[ + "CREATE TABLE t (a INTEGER, b VARCHAR)", + "CREATE UNIQUE INDEX t_b ON t ((lower(b)))", + "CREATE SCHEMA s", + "CREATE TABLE s.t (a INTEGER)", + "CREATE INDEX \"s a\" ON s.t (a)", + ], + ) + .await + .unwrap(); + let nested = TableRef::new(Some("s".to_owned()), "t"); + let read = |table: TableRef| { + let connection = connection.as_ref(); + async move { connection.fetch_table_definition(&table, call()).await.unwrap().indexes } + }; + let main = read(TableRef::unqualified("t")).await; + let catalog = file.path.file_stem().unwrap().to_string_lossy().into_owned(); + + assert_eq!( + main[0].definition, + Some(format!( + "CREATE UNIQUE INDEX t_b ON \"{catalog}\".\"main\".\"t\"((lower(b)))" + )) + ); + for (table, drop, indexes) in [ + (TableRef::unqualified("t"), "DROP INDEX t_b", main), + (nested.clone(), "DROP INDEX s.\"s a\"", read(nested).await), + ] { + let statement = indexes[0].definition.clone().expect("a statement"); + apply(connection.as_ref(), &[drop, statement.as_str()]).await.unwrap(); + assert_eq!(read(table).await, indexes, "{statement}"); + } +} diff --git a/linux/crates/drivers/mssql/src/connection.rs b/linux/crates/drivers/mssql/src/connection.rs index d88cd13e86..b50b23dea4 100644 --- a/linux/crates/drivers/mssql/src/connection.rs +++ b/linux/crates/drivers/mssql/src/connection.rs @@ -348,10 +348,13 @@ impl Connection for MssqlConnection { ) -> Result { let relation = self.relation(table); let table = table.clone(); + let major_version = self.dialect.major_version(); self.read( options, read_op(move |client, watch| { - Box::pin(async move { metadata::fetch_table_definition(client, watch, &table, &relation).await }) + Box::pin(async move { + metadata::fetch_table_definition(client, watch, &table, &relation, major_version).await + }) }), ) .await diff --git a/linux/crates/drivers/mssql/src/ddl.rs b/linux/crates/drivers/mssql/src/ddl.rs index 066514b5a7..9d7f485e22 100644 --- a/linux/crates/drivers/mssql/src/ddl.rs +++ b/linux/crates/drivers/mssql/src/ddl.rs @@ -6,10 +6,11 @@ use tablepro_core::ddl::{ ReferentialAction, RefusalReason, ResolvedType, TableChanges, TableDefinition, TableDraft, }; use tablepro_core::dialect::SqlDialect; -use tablepro_core::meta::TableRef; +use tablepro_core::meta::{IndexInfo, TableRef}; use crate::catalog::SqlServerType; use crate::dialect::{MssqlDialect, national_literal}; +use crate::index_statements::disabling; static CAPABILITIES: DdlCapabilities = DdlCapabilities { create_table: true, @@ -123,36 +124,39 @@ impl DdlDialect for MssqlDialect { } fn plan_create_table(&self, draft: &TableDraft) -> Result { - draft.check_create(self)?; - let table = TableRef::new(draft.table.schema.clone(), draft.table.name.trim()); - let mut definitions = draft - .columns + self.create_table(draft, "") + } + + fn create_table_script(&self, definition: &TableDefinition) -> Result { + let mut draft = TableDraft::from_definition(definition); + let (stated, written): (Vec, Vec) = + draft.indexes.into_iter().partition(|index| index.definition.is_some()); + draft.indexes = written; + let stated: Vec<(&str, &str)> = stated .iter() - .map(|column| self.column_definition(column)) - .collect::, _>>()?; - let key: Vec = draft.primary_key().into_iter().map(str::to_owned).collect(); - if !key.is_empty() { - definitions.push(format!("PRIMARY KEY ({})", column_list(self, &key))); - } - definitions.extend(draft.foreign_keys.iter().map(|key| foreign_key_sql(self, &table, key))); - let mut statements = vec![format!( - "CREATE TABLE {} (\n {}\n)", - self.qualify(&table), - definitions.join(",\n ") - )]; - statements.extend( - draft - .indexes - .iter() - .filter(|index| !index.primary) - .map(|index| create_index_sql(self, &table, index)), - ); - for column in &draft.columns { - if let Some(comment) = column.comment_text() { - statements.push(column_comment(&table, &column.name, false, Some(comment))); + .filter_map(|index| Some((index.name.as_str(), index.definition.as_deref()?))) + .collect(); + // A table has one clustered index, so a stated one leaves the key nonclustered. + let clustered = stated + .iter() + .any(|(_, sql)| sql.starts_with("CREATE CLUSTERED ") || sql.starts_with("CREATE UNIQUE CLUSTERED ")); + let mut plan = self.create_table(&draft, if clustered { " NONCLUSTERED" } else { "" })?; + let relation = self.qualify(&definition.table); + let (mut created, mut disabled) = (Vec::new(), Vec::new()); + // A disabled clustered index refuses every index made after it. + for (name, sql) in stated { + let disable = disabling(name, &relation); + match sql.strip_suffix(&format!(";\n\n{disable}")) { + Some(create) => { + created.push(create.to_owned()); + disabled.push(disable); + } + None => created.push(sql.to_owned()), } } - Ok(DdlPlan::from_sql(statements)) + plan.statements + .extend(DdlPlan::from_sql(created.into_iter().chain(disabled)).statements); + Ok(plan) } fn plan_alter_table(&self, original: &TableDefinition, changes: &TableChanges) -> Result { @@ -228,6 +232,40 @@ impl DdlDialect for MssqlDialect { } impl MssqlDialect { + // `key_kind` is " NONCLUSTERED" where another index of the table is the clustered one. + fn create_table(&self, draft: &TableDraft, key_kind: &str) -> Result { + draft.check_create(self)?; + let table = TableRef::new(draft.table.schema.clone(), draft.table.name.trim()); + let mut definitions = draft + .columns + .iter() + .map(|column| self.column_definition(column)) + .collect::, _>>()?; + let key: Vec = draft.primary_key().into_iter().map(str::to_owned).collect(); + if !key.is_empty() { + definitions.push(format!("PRIMARY KEY{key_kind} ({})", column_list(self, &key))); + } + definitions.extend(draft.foreign_keys.iter().map(|key| foreign_key_sql(self, &table, key))); + let mut statements = vec![format!( + "CREATE TABLE {} (\n {}\n)", + self.qualify(&table), + definitions.join(",\n ") + )]; + statements.extend( + draft + .indexes + .iter() + .filter(|index| !index.primary) + .map(|index| create_index_sql(self, &table, index)), + ); + for column in &draft.columns { + if let Some(comment) = column.comment_text() { + statements.push(column_comment(&table, &column.name, false, Some(comment))); + } + } + Ok(DdlPlan::from_sql(statements)) + } + /// A column bound to a default cannot change type, so its default /// goes into `statements` first, and the statements returned add it /// back under the same name once the column and the columns added @@ -590,6 +628,47 @@ mod tests { DraftColumn::new(name, DraftType::Typed(declaration.to_owned())) } + #[test] + fn the_script_keeps_a_stated_clustered_index_and_disables_last() { + let mut table = orders(); + let index = |name: &str, column: &str, definition: &str| tablepro_core::IndexInfo { + name: name.to_owned(), + keys: vec![tablepro_core::meta::IndexKey::Column { + name: column.to_owned(), + descending: false, + }], + unique: false, + primary: false, + predicate: None, + definition: Some(definition.to_owned()), + }; + table.indexes = vec![ + index( + "cx", + "status", + "CREATE CLUSTERED INDEX [cx] ON [dbo].[orders] ([status]) ON [PRIMARY];\n\n\ + ALTER INDEX [cx] ON [dbo].[orders] DISABLE", + ), + index( + "ix", + "note", + "CREATE NONCLUSTERED INDEX [ix] ON [dbo].[orders] ([note]) ON [PRIMARY]", + ), + ]; + + let script = sql(dialect().create_table_script(&table)); + + assert!(script[0].contains("PRIMARY KEY NONCLUSTERED ([id])"), "{}", script[0]); + assert_eq!( + script[script.len() - 3..], + [ + "CREATE CLUSTERED INDEX [cx] ON [dbo].[orders] ([status]) ON [PRIMARY]", + "CREATE NONCLUSTERED INDEX [ix] ON [dbo].[orders] ([note]) ON [PRIMARY]", + "ALTER INDEX [cx] ON [dbo].[orders] DISABLE", + ] + ); + } + #[test] fn plan_create_table_simple() { let mut draft = TableDraft::new(TableRef::new(Some("dbo".to_owned()), "notes")); diff --git a/linux/crates/drivers/mssql/src/index_statements.rs b/linux/crates/drivers/mssql/src/index_statements.rs new file mode 100644 index 0000000000..3f44f4a9e4 --- /dev/null +++ b/linux/crates/drivers/mssql/src/index_statements.rs @@ -0,0 +1,480 @@ +use std::collections::HashMap; + +use tablepro_core::ddl::ddl_sql::index_keys_sql; +use tablepro_core::meta::IndexInfo; +use tablepro_core::{DriverError, Value}; + +use crate::catalog::Cells; +use crate::dialect::{MssqlDialect, quote}; +use crate::in_band_request::{Interrupt, Watch}; +use crate::physical::MssqlClient; +use crate::workspace_call::{catalogue_rows, text_param}; + +// A constraint's index has no CREATE INDEX of its own; XML, spatial and columnstore indexes take +// syntax this does not write. +fn index_options_sql(major_version: u32) -> String { + // OPTIMIZE_FOR_SEQUENTIAL_KEY came with SQL Server 2019. + let sequential = match major_version >= 15 { + true => "CAST(i.optimize_for_sequential_key AS int)", + false => "0", + }; + format!( + "SELECT i.name, i.type_desc, CAST(i.is_unique AS int), i.filter_definition, i.fill_factor, \ + CAST(i.is_padded AS int), CAST(i.ignore_dup_key AS int), CAST(s.no_recompute AS int), \ + CAST(s.is_incremental AS int), CAST(i.allow_row_locks AS int), CAST(i.allow_page_locks AS int), \ + {sequential}, CAST(i.is_disabled AS int), ds.name, RTRIM(ds.type), CAST(i.has_filter AS int) \ + FROM sys.indexes i \ + JOIN sys.stats s ON s.object_id = i.object_id AND s.stats_id = i.index_id \ + JOIN sys.data_spaces ds ON ds.data_space_id = i.data_space_id \ + WHERE i.object_id = OBJECT_ID(@P1) AND i.type IN (1, 2) AND i.is_primary_key = 0 \ + AND i.is_unique_constraint = 0 AND i.is_hypothetical = 0" + ) +} + +// An aligned index lists its partition column as neither key nor included. +const INDEX_COLUMNS_SQL: &str = "SELECT i.name, c.name, CAST(ic.is_included_column AS int), ic.partition_ordinal \ + FROM sys.indexes i \ + JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id \ + JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id \ + WHERE i.object_id = OBJECT_ID(@P1) AND i.type IN (1, 2) \ + AND (ic.is_included_column = 1 OR ic.partition_ordinal = 1) \ + ORDER BY i.index_id, ic.index_column_id"; + +// A disabled index has no partitions, so its compression is not known. +fn index_partitions_sql(major_version: u32) -> String { + // XML_COMPRESSION came with SQL Server 2022. + let xml = match major_version >= 16 { + true => "p.xml_compression_desc", + false => "'OFF'", + }; + format!( + "SELECT i.name, p.partition_number, p.data_compression_desc, {xml} \ + FROM sys.indexes i \ + JOIN sys.partitions p ON p.object_id = i.object_id AND p.index_id = i.index_id \ + WHERE i.object_id = OBJECT_ID(@P1) AND i.type IN (1, 2) \ + ORDER BY i.index_id, p.partition_number" + ) +} + +pub(crate) async fn fetch_index_statements( + client: &mut MssqlClient, + watch: &Watch, + relation: &str, + major_version: u32, + indexes: &mut [IndexInfo], +) -> Result<(), Interrupt> { + let params = [text_param(relation)]; + let options = catalogue_rows(client, watch, &index_options_sql(major_version), ¶ms).await?; + let columns = catalogue_rows(client, watch, INDEX_COLUMNS_SQL, ¶ms).await?; + let partitions = catalogue_rows(client, watch, &index_partitions_sql(major_version), ¶ms).await?; + let rows = CatalogRows { + options: &options, + columns: &columns, + partitions: &partitions, + }; + let mut statements = statements(&MssqlDialect::new(major_version), relation, indexes, &rows)?; + for index in indexes { + index.definition = statements.remove(&index.name); + } + Ok(()) +} + +// CREATE INDEX has no way to say disabled, so a disabled index's statement ends with this one. +pub(crate) fn disabling(name: &str, relation: &str) -> String { + format!("ALTER INDEX {} ON {relation} DISABLE", quote(name)) +} + +struct CatalogRows<'a> { + options: &'a [Vec], + columns: &'a [Vec], + partitions: &'a [Vec], +} + +#[derive(Default)] +struct IndexParts { + included: Vec, + partitioned_by: Option, + data_compression: Vec<(i64, String)>, + xml_compression: Vec<(i64, String)>, +} + +fn statements( + dialect: &MssqlDialect, + relation: &str, + indexes: &[IndexInfo], + rows: &CatalogRows<'_>, +) -> Result, DriverError> { + let mut parts: HashMap = HashMap::new(); + for row in rows.columns { + let cells = Cells::new(row, "sys.index_columns"); + let entry = parts.entry(cells.required_text(0)?).or_default(); + let column = cells.required_text(1)?; + if cells.int(3) == Some(1) { + entry.partitioned_by = Some(column.clone()); + } + if cells.flag(2) { + entry.included.push(column); + } + } + for row in rows.partitions { + let cells = Cells::new(row, "sys.partitions"); + let entry = parts.entry(cells.required_text(0)?).or_default(); + let number = cells.int(1).unwrap_or_default(); + entry.data_compression.push((number, cells.required_text(2)?)); + entry.xml_compression.push((number, cells.required_text(3)?)); + } + let mut statements = HashMap::new(); + for row in rows.options { + let cells = Cells::new(row, "sys.indexes"); + let name = cells.required_text(0)?; + let Some(index) = indexes.iter().find(|index| index.name == name) else { + continue; + }; + // Without VIEW DEFINITION the filter reads NULL, and the index would come back unfiltered. + if cells.flag(15) && cells.text(3).is_none() { + continue; + } + let parts = parts.remove(&name).unwrap_or_default(); + statements.insert(name, statement(dialect, relation, index, &cells, &parts)?); + } + Ok(statements) +} + +// SQL Server keeps no text for an index. Storage counts: a clustered index made again without its +// partition scheme moves the whole table off it. +fn statement( + dialect: &MssqlDialect, + relation: &str, + index: &IndexInfo, + cells: &Cells<'_>, + parts: &IndexParts, +) -> Result { + let unique = if cells.flag(2) { "UNIQUE " } else { "" }; + let mut sql = format!( + "CREATE {unique}{} INDEX {} ON {relation} ({})", + cells.required_text(1)?, + quote(&index.name), + index_keys_sql(dialect, index) + ); + if !parts.included.is_empty() { + let included: Vec = parts.included.iter().map(|column| quote(column)).collect(); + sql.push_str(&format!(" INCLUDE ({})", included.join(", "))); + } + if let Some(filter) = cells.text(3) { + sql.push_str(&format!(" WHERE {filter}")); + } + let mut options = with_options(cells); + options.extend(compression("DATA_COMPRESSION", "NONE", &parts.data_compression)); + options.extend(compression("XML_COMPRESSION", "OFF", &parts.xml_compression)); + if !options.is_empty() { + sql.push_str(&format!(" WITH ({})", options.join(", "))); + } + let space = quote(&cells.required_text(13)?); + match (cells.text(14).as_deref(), &parts.partitioned_by) { + (Some("PS"), Some(column)) => sql.push_str(&format!(" ON {space}({})", quote(column))), + _ => sql.push_str(&format!(" ON {space}")), + } + if cells.flag(12) { + sql.push_str(&format!(";\n\n{}", disabling(&index.name, relation))); + } + Ok(sql) +} + +// Only what differs from CREATE INDEX's defaults, in the order the documentation lists them. +fn with_options(cells: &Cells<'_>) -> Vec { + let mut options = Vec::new(); + if cells.flag(5) { + options.push("PAD_INDEX = ON".to_owned()); + } + if let Some(fill) = cells.int(4).filter(|fill| *fill > 0) { + options.push(format!("FILLFACTOR = {fill}")); + } + for (column, on, option) in [ + (6, true, "IGNORE_DUP_KEY"), + (7, true, "STATISTICS_NORECOMPUTE"), + (8, true, "STATISTICS_INCREMENTAL"), + (9, false, "ALLOW_ROW_LOCKS"), + (10, false, "ALLOW_PAGE_LOCKS"), + (11, true, "OPTIMIZE_FOR_SEQUENTIAL_KEY"), + ] { + if cells.flag(column) == on { + options.push(format!("{option} = {}", if on { "ON" } else { "OFF" })); + } + } + options +} + +// One setting covers an index whose partitions agree; ON PARTITIONS names the rest. +fn compression(option: &str, default: &str, partitions: &[(i64, String)]) -> Vec { + let mut kinds: Vec<&str> = Vec::new(); + for (_, kind) in partitions { + if !kinds.contains(&kind.as_str()) { + kinds.push(kind); + } + } + match kinds.as_slice() { + [] => Vec::new(), + [kind] if *kind == default => Vec::new(), + [kind] => vec![format!("{option} = {kind}")], + _ => kinds + .iter() + .filter(|kind| **kind != default) + .map(|kind| { + let numbers: Vec = partitions + .iter() + .filter(|(_, other)| other == kind) + .map(|(number, _)| number.to_string()) + .collect(); + format!("{option} = {kind} ON PARTITIONS ({})", numbers.join(", ")) + }) + .collect(), + } +} + +#[cfg(test)] +mod tests { + use tablepro_core::meta::IndexKey; + + use super::*; + + fn text(value: &str) -> Value { + Value::Text(value.to_owned()) + } + + fn index(name: &str, keys: &[(&str, bool)]) -> IndexInfo { + IndexInfo { + name: name.to_owned(), + keys: keys + .iter() + .map(|(column, descending)| IndexKey::Column { + name: (*column).to_owned(), + descending: *descending, + }) + .collect(), + unique: false, + primary: false, + predicate: None, + definition: None, + } + } + + struct Facts { + kind: &'static str, + unique: bool, + filter: Option<&'static str>, + flags: [i64; 9], + space: (&'static str, &'static str), + filtered: bool, + } + + const PLAIN: Facts = Facts { + kind: "NONCLUSTERED", + unique: false, + filter: None, + // fill, pad, ignore_dup_key, no_recompute, incremental, row locks, page locks, sequential, disabled + flags: [0, 0, 0, 0, 0, 1, 1, 0, 0], + space: ("PRIMARY", "FG"), + filtered: false, + }; + + fn row(name: &str, facts: Facts) -> Vec { + let mut row = vec![ + text(name), + text(facts.kind), + Value::Int(i64::from(facts.unique)), + facts.filter.map_or(Value::Null, text), + ]; + row.extend(facts.flags.iter().map(|flag| Value::Int(*flag))); + row.extend([ + text(facts.space.0), + text(facts.space.1), + Value::Int(i64::from(facts.filtered)), + ]); + row + } + + fn partition_rows(index: &str, kinds: &[(&str, &str)]) -> Vec> { + (1..) + .zip(kinds) + .map(|(number, (data, xml))| vec![text(index), Value::Int(number), text(data), text(xml)]) + .collect() + } + + fn column(index: &str, name: &str, included: bool, partition: i64) -> Vec { + vec![ + text(index), + text(name), + Value::Int(i64::from(included)), + Value::Int(partition), + ] + } + + fn written() -> HashMap { + let indexes = [ + index("cx_pt", &[("k", false)]), + index("ix_filt", &[("a", true), ("c", false)]), + index("ux_opts", &[("c", false), ("d", false)]), + index("ix_ppc", &[("a", false)]), + index("ix_dis", &[("e", false)]), + index("we]ird", &[("f", false)]), + ]; + let options = [ + row( + "cx_pt", + Facts { + kind: "CLUSTERED", + space: ("ps", "PS"), + ..PLAIN + }, + ), + row( + "ix_filt", + Facts { + filter: Some("([a]>(0) AND [b]<>N'x''y')"), + filtered: true, + ..PLAIN + }, + ), + row( + "ux_opts", + Facts { + unique: true, + flags: [80, 1, 1, 1, 0, 1, 0, 1, 0], + ..PLAIN + }, + ), + row( + "ix_ppc", + Facts { + flags: [0, 0, 0, 0, 1, 1, 1, 0, 0], + space: ("ps", "PS"), + ..PLAIN + }, + ), + row( + "ix_dis", + Facts { + flags: [0, 0, 0, 0, 0, 1, 1, 0, 1], + ..PLAIN + }, + ), + row( + "we]ird", + Facts { + flags: [0, 0, 0, 0, 0, 0, 1, 0, 0], + space: ("fg2", "FG"), + ..PLAIN + }, + ), + ]; + let columns = [ + column("cx_pt", "k", false, 1), + column("ix_filt", "b", true, 0), + column("ix_ppc", "k", false, 1), + ]; + + let partitions = [ + partition_rows("cx_pt", &[("NONE", "OFF"), ("NONE", "OFF"), ("NONE", "OFF")]), + partition_rows("ix_filt", &[("NONE", "OFF")]), + partition_rows("ux_opts", &[("PAGE", "ON")]), + partition_rows("ix_ppc", &[("PAGE", "OFF"), ("NONE", "OFF"), ("ROW", "ON")]), + partition_rows("we]ird", &[("NONE", "OFF")]), + ] + .concat(); + let rows = CatalogRows { + options: &options, + columns: &columns, + partitions: &partitions, + }; + + statements(&MssqlDialect::new(16), "[dbo].[t]", &indexes, &rows).expect("the statements") + } + + #[test] + fn an_index_is_written_with_its_storage_and_options() { + let mut statements = written(); + let mut statement = |name: &str| statements.remove(name).expect(name); + + assert_eq!( + statement("cx_pt"), + "CREATE CLUSTERED INDEX [cx_pt] ON [dbo].[t] ([k]) ON [ps]([k])" + ); + assert_eq!( + statement("ix_filt"), + "CREATE NONCLUSTERED INDEX [ix_filt] ON [dbo].[t] ([a] DESC, [c]) INCLUDE ([b]) \ + WHERE ([a]>(0) AND [b]<>N'x''y') ON [PRIMARY]" + ); + assert_eq!( + statement("ux_opts"), + "CREATE UNIQUE NONCLUSTERED INDEX [ux_opts] ON [dbo].[t] ([c], [d]) WITH (PAD_INDEX = ON, \ + FILLFACTOR = 80, IGNORE_DUP_KEY = ON, STATISTICS_NORECOMPUTE = ON, ALLOW_PAGE_LOCKS = OFF, \ + OPTIMIZE_FOR_SEQUENTIAL_KEY = ON, DATA_COMPRESSION = PAGE, XML_COMPRESSION = ON) ON [PRIMARY]" + ); + } + + #[test] + fn partitions_a_disabled_index_and_a_bracket_are_written() { + let mut statements = written(); + let mut statement = |name: &str| statements.remove(name).expect(name); + + assert_eq!( + statement("ix_ppc"), + "CREATE NONCLUSTERED INDEX [ix_ppc] ON [dbo].[t] ([a]) WITH (STATISTICS_INCREMENTAL = ON, \ + DATA_COMPRESSION = PAGE ON PARTITIONS (1), DATA_COMPRESSION = ROW ON PARTITIONS (3), \ + XML_COMPRESSION = ON ON PARTITIONS (3)) ON [ps]([k])" + ); + assert_eq!( + statement("ix_dis"), + "CREATE NONCLUSTERED INDEX [ix_dis] ON [dbo].[t] ([e]) ON [PRIMARY];\n\n\ + ALTER INDEX [ix_dis] ON [dbo].[t] DISABLE" + ); + assert_eq!( + statement("we]ird"), + "CREATE NONCLUSTERED INDEX [we]]ird] ON [dbo].[t] ([f]) WITH (ALLOW_ROW_LOCKS = OFF) ON [fg2]" + ); + } + + #[test] + fn a_filter_the_user_may_not_read_withholds_the_statement() { + let indexes = [index("ix_hidden", &[("a", false)])]; + let options = [row( + "ix_hidden", + Facts { + filtered: true, + ..PLAIN + }, + )]; + let rows = CatalogRows { + options: &options, + columns: &[], + partitions: &[], + }; + + let statements = statements(&MssqlDialect::new(16), "[dbo].[t]", &indexes, &rows).expect("the statements"); + + assert!(statements.is_empty()); + } + + #[test] + fn an_index_the_options_do_not_list_has_no_statement() { + let indexes = [index("pk", &[("id", false)])]; + + let rows = CatalogRows { + options: &[], + columns: &[], + partitions: &[], + }; + + let statements = statements(&MssqlDialect::new(14), "[dbo].[t]", &indexes, &rows).expect("the statements"); + + assert!(statements.is_empty()); + } + + #[test] + fn older_servers_read_no_newer_catalog_columns() { + assert!(!index_options_sql(14).contains("optimize_for_sequential_key")); + assert!(index_options_sql(15).contains("i.optimize_for_sequential_key")); + assert!(!index_partitions_sql(15).contains("xml_compression")); + assert!(index_partitions_sql(16).contains("p.xml_compression_desc")); + } +} diff --git a/linux/crates/drivers/mssql/src/lib.rs b/linux/crates/drivers/mssql/src/lib.rs index 8c59c0497a..cc78bf7acc 100644 --- a/linux/crates/drivers/mssql/src/lib.rs +++ b/linux/crates/drivers/mssql/src/lib.rs @@ -22,6 +22,7 @@ mod encode; mod endpoint; mod errors; mod in_band_request; +mod index_statements; mod login; mod metadata; mod mssql_manager; diff --git a/linux/crates/drivers/mssql/src/metadata.rs b/linux/crates/drivers/mssql/src/metadata.rs index 85faf4039c..63837f404a 100644 --- a/linux/crates/drivers/mssql/src/metadata.rs +++ b/linux/crates/drivers/mssql/src/metadata.rs @@ -14,8 +14,8 @@ use crate::catalog::{ COLUMNS_SQL, Cells, INBOUND_FOREIGN_KEYS_SQL, MssqlColumnRow, PRIMARY_KEY_SQL, column_definition, column_info, inbound_foreign_keys, primary_key, }; -use crate::dialect::quote; use crate::in_band_request::{Interrupt, Watch}; +use crate::index_statements::fetch_index_statements; use crate::physical::MssqlClient; use crate::workspace_call::{catalogue_rows, text_param}; @@ -37,15 +37,8 @@ const OBJECTS_SQL: &str = "SELECT SCHEMA_NAME(o.schema_id), o.name, RTRIM(o.type FROM sys.objects o \ WHERE o.type IN ('U', 'V', 'SO', 'FN', 'IF', 'TF', 'FS', 'FT', 'P', 'PC') AND o.is_ms_shipped = 0"; -/// The INCLUDE columns come back as one quoted list, which `.value` -/// reads out of the XML without escaping `&` or `<` in a name. const INDEXES_SQL: &str = "SELECT i.name, CAST(i.is_unique AS int), CAST(i.is_primary_key AS int), c.name, \ - CAST(ic.is_descending_key AS int), i.filter_definition, i.type_desc, \ - CAST(i.is_unique_constraint AS int), \ - STUFF((SELECT N', ' + QUOTENAME(inc.name) FROM sys.index_columns iic \ - JOIN sys.columns inc ON inc.object_id = iic.object_id AND inc.column_id = iic.column_id \ - WHERE iic.object_id = i.object_id AND iic.index_id = i.index_id AND iic.is_included_column = 1 \ - ORDER BY iic.index_column_id FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 2, N'') \ + CAST(ic.is_descending_key AS int), i.filter_definition \ FROM sys.indexes i \ JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id \ JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id \ @@ -182,11 +175,11 @@ pub(crate) async fn fetch_indexes( relation: &str, ) -> Result, Interrupt> { let rows = catalogue_rows(client, watch, INDEXES_SQL, &[text_param(relation)]).await?; - Ok(indexes(&rows, relation)?) + Ok(indexes(&rows)?) } -fn indexes(rows: &[Vec], relation: &str) -> Result, DriverError> { - let mut indexes: Vec<(IndexInfo, &[Value])> = Vec::new(); +fn indexes(rows: &[Vec]) -> Result, DriverError> { + let mut indexes: Vec = Vec::new(); for row in rows { let cells = Cells::new(row, "sys.indexes"); let name = cells.required_text(0)?; @@ -195,66 +188,18 @@ fn indexes(rows: &[Vec], relation: &str) -> Result, Driver descending: cells.flag(4), }; match indexes.last_mut() { - Some((index, _)) if index.name == name => index.keys.push(key), - _ => indexes.push(( - IndexInfo { - name, - keys: vec![key], - unique: cells.flag(1), - primary: cells.flag(2), - predicate: cells.text(5), - definition: None, - }, - row, - )), + Some(index) if index.name == name => index.keys.push(key), + _ => indexes.push(IndexInfo { + name, + keys: vec![key], + unique: cells.flag(1), + primary: cells.flag(2), + predicate: cells.text(5), + definition: None, + }), } } - Ok(indexes - .into_iter() - .map(|(mut index, row)| { - index.definition = create_index(relation, &index, &Cells::new(row, "sys.indexes")); - index - }) - .collect()) -} - -/// SQL Server keeps no text for an index, so this writes it from -/// sys.indexes: a UNIQUE constraint as the constraint it is, and only -/// rowstore indexes, whose keys and INCLUDE list are all there is. -fn create_index(relation: &str, index: &IndexInfo, cells: &Cells<'_>) -> Option { - let kind = cells - .text(6) - .filter(|kind| kind == "CLUSTERED" || kind == "NONCLUSTERED")?; - if index.primary { - return None; - } - let keys = index - .keys - .iter() - .filter_map(|key| { - let column = quote(key.column_name()?); - Some(match key.descending() { - true => format!("{column} DESC"), - false => column, - }) - }) - .collect::>() - .join(", "); - let name = quote(&index.name); - if cells.flag(7) { - return Some(format!( - "ALTER TABLE {relation} ADD CONSTRAINT {name} UNIQUE {kind} ({keys})" - )); - } - let unique = if index.unique { "UNIQUE " } else { "" }; - let mut sql = format!("CREATE {unique}{kind} INDEX {name} ON {relation} ({keys})"); - if let Some(included) = cells.text(8).filter(|included| !included.is_empty()) { - sql.push_str(&format!(" INCLUDE ({included})")); - } - if let Some(predicate) = &index.predicate { - sql.push_str(&format!(" WHERE {predicate}")); - } - Some(sql) + Ok(indexes) } pub(crate) async fn fetch_foreign_keys( @@ -356,10 +301,12 @@ pub(crate) async fn fetch_table_definition( watch: &Watch, table: &TableRef, relation: &str, + major_version: u32, ) -> Result { let columns = column_rows(client, watch, relation).await?; let primary = catalogue_rows(client, watch, PRIMARY_KEY_SQL, &[text_param(relation)]).await?; - let indexes = fetch_indexes(client, watch, relation).await?; + let mut indexes = fetch_indexes(client, watch, relation).await?; + fetch_index_statements(client, watch, relation, major_version, &mut indexes).await?; let foreign_keys = fetch_foreign_keys(client, watch, relation).await?; let inbound_foreign_keys = fetch_inbound_foreign_keys(client, watch, relation).await?; Ok(TableDefinition { @@ -438,7 +385,7 @@ mod tests { ], ]; - let indexes = indexes(&rows, "[dbo].[t]").unwrap(); + let indexes = indexes(&rows).unwrap(); assert_eq!(indexes.len(), 2); assert_eq!(indexes[0].keys.len(), 2); @@ -448,42 +395,6 @@ mod tests { assert!(indexes[1].primary && indexes[1].can_identify_rows()); } - #[test] - fn an_index_is_written_as_sql_server_makes_it() { - let row = |name: &str, kind: &str, constraint: i64, included: Value, filter: Value| { - vec![ - text(name), - Value::Int(1), - Value::Int(0), - text("a"), - Value::Int(1), - filter, - text(kind), - Value::Int(constraint), - included, - ] - }; - let rows = vec![ - row("IX_a", "NONCLUSTERED", 0, text("[b], [c]"), text("([a]>(0))")), - row("UQ_a", "CLUSTERED", 1, Value::Null, Value::Null), - row("IX_x", "XML", 0, Value::Null, Value::Null), - ]; - - let indexes = indexes(&rows, "[dbo].[t]").unwrap(); - let statements: Vec> = indexes.iter().map(|index| index.definition.as_deref()).collect(); - - assert_eq!( - statements, - [ - Some( - "CREATE UNIQUE NONCLUSTERED INDEX [IX_a] ON [dbo].[t] ([a] DESC) INCLUDE ([b], [c]) WHERE ([a]>(0))" - ), - Some("ALTER TABLE [dbo].[t] ADD CONSTRAINT [UQ_a] UNIQUE CLUSTERED ([a] DESC)"), - None, - ] - ); - } - #[test] fn the_first_key_names_the_row() { let rows = vec![ diff --git a/linux/crates/drivers/mssql/tests/integration/metadata.rs b/linux/crates/drivers/mssql/tests/integration/metadata.rs index 0e3c238115..a5dffc067f 100644 --- a/linux/crates/drivers/mssql/tests/integration/metadata.rs +++ b/linux/crates/drivers/mssql/tests/integration/metadata.rs @@ -339,9 +339,6 @@ async fn like_escape_matches_literal_underscore_and_percent() { ); } -/// Each index's statement, run after the index is dropped, makes the -/// same index again: clustered, filtered with INCLUDE columns, and a -/// UNIQUE constraint. A columnstore index has no statement. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] async fn index_statements_recreate_their_indexes() { @@ -355,49 +352,84 @@ async fn index_statements_recreate_their_indexes() { "CREATE CLUSTERED INDEX ledger_amount ON dbo.ledger (amount DESC)", "CREATE NONCLUSTERED INDEX ledger_open ON dbo.ledger (code, id DESC) INCLUDE ([a&b], note) \ WHERE amount > 0", - "CREATE NONCLUSTERED COLUMNSTORE INDEX ledger_columns ON dbo.ledger (amount)", + "CREATE UNIQUE INDEX ledger_note ON dbo.ledger (note) WITH (FILLFACTOR = 80, IGNORE_DUP_KEY = ON, \ + ALLOW_PAGE_LOCKS = OFF, DATA_COMPRESSION = PAGE)", + "CREATE INDEX ledger_idle ON dbo.ledger ([a&b])", + "ALTER INDEX ledger_idle ON dbo.ledger DISABLE", + "CREATE PARTITION FUNCTION ledger_pf (int) AS RANGE LEFT FOR VALUES (10, 20)", + "CREATE PARTITION SCHEME ledger_ps AS PARTITION ledger_pf ALL TO ([PRIMARY])", + "CREATE TABLE dbo.parted (k int NOT NULL, v int)", + "CREATE CLUSTERED INDEX parted_k ON dbo.parted (k) ON ledger_ps (k)", + "CREATE INDEX parted_v ON dbo.parted (v) WITH (DATA_COMPRESSION = PAGE ON PARTITIONS (1))", ], ) .await; - let table = dbo("ledger"); - let indexes = connection - .fetch_indexes(&table, within(CALL)) - .await - .expect("the indexes"); - assert!( + let read = |name: &'static str| { + let connection = connection.as_ref(); + async move { + connection + .fetch_table_definition(&dbo(name), within(CALL)) + .await + .expect("the table") + .indexes + } + }; + let ledger = read("ledger").await; + let statement = |indexes: &[tablepro_core::IndexInfo], name: &str| { indexes .iter() - .filter(|index| index.primary || index.name == "ledger_columns") - .all(|index| index.definition.is_none()), - "{indexes:#?}" - ); - let open = indexes - .iter() - .find(|index| index.name == "ledger_open") - .expect("ledger_open"); + .find(|index| index.name == name) + .and_then(|index| index.definition.clone()) + }; + assert_eq!(statement(&ledger, "ledger_pk"), None); + assert_eq!(statement(&ledger, "ledger_code"), None); assert_eq!( - open.definition.as_deref(), + statement(&ledger, "ledger_open").as_deref(), Some( "CREATE NONCLUSTERED INDEX [ledger_open] ON [dbo].[ledger] ([code], [id] DESC) \ - INCLUDE ([a&b], [note]) WHERE ([amount]>(0))" + INCLUDE ([a&b], [note]) WHERE ([amount]>(0)) ON [PRIMARY]" ) ); - for index in indexes.iter().filter(|index| index.definition.is_some()) { - let statement = index.definition.clone().unwrap_or_default(); - let drop = match index.name.as_str() { - "ledger_code" => "ALTER TABLE dbo.ledger DROP CONSTRAINT ledger_code".to_owned(), - name => format!("DROP INDEX [{name}] ON dbo.ledger"), - }; - setup(connection.as_ref(), &[drop.as_str(), statement.as_str()]).await; - let remade = connection - .fetch_indexes(&table, within(CALL)) - .await - .expect("the indexes"); + let parted = read("parted").await; + assert_eq!( + statement(&parted, "parted_k").as_deref(), + Some("CREATE CLUSTERED INDEX [parted_k] ON [dbo].[parted] ([k]) ON [ledger_ps]([k])") + ); + + for (table, indexes) in [("ledger", &ledger), ("parted", &parted)] { + for index in indexes.iter().filter(|index| index.definition.is_some()) { + let statement = index.definition.clone().unwrap_or_default(); + let drop = format!("DROP INDEX [{}] ON dbo.{table}", index.name); + setup(connection.as_ref(), &[drop.as_str(), statement.as_str()]).await; + let remade = read(table).await; + assert_eq!( + remade.iter().find(|remade| remade.name == index.name), + Some(index), + "{statement}" + ); + } + } + + let definition = connection + .fetch_table_definition(&dbo("ledger"), within(CALL)) + .await + .expect("the table"); + let dialect = connection.dialect(); + let script = dialect + .ddl() + .expect("SQL Server plans DDL") + .create_table_script(&definition) + .expect("the script"); + let mut statements = vec!["DROP TABLE dbo.ledger"]; + statements.extend(script.sql()); + setup(connection.as_ref(), &statements).await; + let remade = read("ledger").await; + for index in definition.indexes.iter().filter(|index| index.definition.is_some()) { assert_eq!( remade.iter().find(|remade| remade.name == index.name), Some(index), - "{statement}" + "{script:?}" ); } connection.close().await.expect("a close"); diff --git a/linux/crates/drivers/mysql/src/catalog/mod.rs b/linux/crates/drivers/mysql/src/catalog/mod.rs index 9cc1373181..f4717a9b09 100644 --- a/linux/crates/drivers/mysql/src/catalog/mod.rs +++ b/linux/crates/drivers/mysql/src/catalog/mod.rs @@ -21,8 +21,8 @@ pub(crate) use mysql_column_row::{MySqlColumnRow, columns_sql}; pub(crate) use mysql_extra::{ExtraGenerated, MySqlExtra}; pub(crate) use table_definition::{ COLUMN_ENGINE_ATTRIBUTES_SQL, ColumnCheck, INBOUND_FOREIGN_KEYS_SQL, ListedSrid, MARIADB_COLUMN_CHECKS_SQL, - MARIADB_GEOMETRY_SRIDS_SQL, column_checks, geometry_srids, inbound_foreign_keys, place_column_checks, - printed_names, quoted_default, + MARIADB_GEOMETRY_SRIDS_SQL, column_checks, geometry_srids, has_mode, hides_index_options, inbound_foreign_keys, + index_statement, place_column_checks, printed_names, quoted_default, without_backslash_escapes, }; pub(crate) use table_options::{ListedOptions, table_options}; @@ -44,7 +44,7 @@ use tablepro_core::value::Value; use crate::flavour::MySqlFlavour; use crate::mysql_timestamp_mode::MySqlTimestampMode; -pub(crate) use escaped_expression::unescape_expression; +use escaped_expression::unescape_expression; use mysql_column_type::scan; /// The catalogue's facts that decide a kind, apart from the rest of the diff --git a/linux/crates/drivers/mysql/src/catalog/table_definition.rs b/linux/crates/drivers/mysql/src/catalog/table_definition.rs index cc4148f28c..52c2ea647a 100644 --- a/linux/crates/drivers/mysql/src/catalog/table_definition.rs +++ b/linux/crates/drivers/mysql/src/catalog/table_definition.rs @@ -116,14 +116,148 @@ pub(crate) fn printed_names(name: &str) -> [String; 2] { /// in, and escapes a line break inside a string or comment. fn column_line<'a>(create_statement: &'a str, column: &str) -> Option<&'a str> { printed_names(column).iter().find_map(|printed| { - let head = format!("\n {printed} "); - let start = create_statement.find(&head)? + 3; - let rest = create_statement.get(start..)?; - let line = rest.split('\n').next()?; - Some(line.strip_suffix(',').unwrap_or(line)) + let head = format!("{printed} "); + elements(create_statement).find_map(|element| { + let line = element.strip_prefix(" ")?; + line.starts_with(&head).then(|| line.strip_suffix(',').unwrap_or(line)) + }) + }) +} + +// The body's elements, each two spaces in. Matching a whole element keeps a name that spells +// another element's head, in a mode that quotes names differently, from being taken for it. +fn elements(create_statement: &str) -> impl Iterator { + let mut rest = create_statement + .get(entry(create_statement).len() + 1..) + .unwrap_or_default(); + std::iter::from_fn(move || { + if !rest.starts_with(" ") { + return None; + } + let element = entry(rest); + rest = rest.get(element.len() + 1..).unwrap_or_default(); + Some(element) + }) +} + +// The server escapes a line break in a string but prints one in a name as it is. +fn entry(text: &str) -> &str { + let mut quote: Option = None; + let mut escaped = false; + for (position, character) in text.char_indices() { + match quote { + Some('\'') if escaped => escaped = false, + Some('\'') if character == '\\' => escaped = true, + Some(open) if character == open => quote = None, + Some(_) => {} + None if character == '\n' => return &text[..position], + None if matches!(character, '`' | '"' | '\'') => quote = Some(character), + None => {} + } + } + text +} + +// An index's own line keeps options the catalogue tables do not all list: INVISIBLE, COMMENT, +// WITH PARSER, IGNORED, a vector index's M and DISTANCE. CREATE INDEX takes them after the keys. +pub(crate) fn index_statement(create_statement: &str, table: &str, index: &str) -> Option { + const KINDS: [(&str, &str); 5] = [ + ("KEY", "INDEX"), + ("UNIQUE KEY", "UNIQUE INDEX"), + ("FULLTEXT KEY", "FULLTEXT INDEX"), + ("SPATIAL KEY", "SPATIAL INDEX"), + ("VECTOR KEY", "VECTOR INDEX"), + ]; + printed_names(index).iter().find_map(|printed| { + KINDS.iter().find_map(|(key, kind)| { + let head = format!(" {key} {printed} "); + elements(create_statement).find_map(|element| { + let line = element.strip_prefix(&head)?.trim_end(); + let rest = line.strip_suffix(',').unwrap_or(line).trim_end(); + Some(format!("CREATE {kind} {printed} ON {table} {rest}")) + }) + }) }) } +// SHOW CREATE TABLE leaves USING, COMMENT and visibility out under these modes, so an index's +// element no longer says all of it. +pub(crate) fn hides_index_options(sql_mode: &str) -> bool { + const MODES: [&str; 7] = [ + "ANSI", + "NO_KEY_OPTIONS", + "ORACLE", + "MSSQL", + "DB2", + "POSTGRESQL", + "MAXDB", + ]; + sql_mode.split(',').any(|mode| MODES.contains(&mode.trim())) +} + +pub(crate) fn has_mode(sql_mode: &str, mode: &str) -> bool { + sql_mode.split(',').any(|set| set.trim() == mode) +} + +// SHOW CREATE TABLE escapes with backslashes, which an editor under NO_BACKSLASH_ESCAPES reads as +// themselves. Each string is written the way that mode reads it; a NUL has no such spelling. +pub(crate) fn without_backslash_escapes(sql: &str) -> Option { + let mut out = String::with_capacity(sql.len()); + let mut characters = sql.chars().peekable(); + let mut quote: Option = None; + while let Some(character) = characters.next() { + match quote { + Some('\'') => match character { + '\\' => { + let decoded = match characters.next()? { + '0' => return None, + 'b' => '\u{8}', + 'n' => '\n', + 'r' => '\r', + 't' => '\t', + 'Z' => '\u{1a}', + '%' => { + out.push('\\'); + '%' + } + '_' => { + out.push('\\'); + '_' + } + other => other, + }; + match decoded { + '\'' => out.push_str("''"), + other => out.push(other), + } + } + '\'' if characters.peek() == Some(&'\'') => { + characters.next(); + out.push_str("''"); + } + '\'' => { + quote = None; + out.push(character); + } + other => out.push(other), + }, + Some(open) => { + if character == open { + quote = None; + } + out.push(character); + } + None => { + if matches!(character, '\'' | '`' | '"') { + quote = Some(character); + } + out.push(character); + } + } + } + Some(out) +} + /// The quoted DEFAULT on the column's line of SHOW CREATE TABLE, without /// its quotes. The server prints a TIMESTAMP default there as wall time /// in the session's zone, where MySQL's `COLUMN_DEFAULT` keeps the text @@ -185,6 +319,91 @@ mod tests { ) } + #[test] + fn an_index_line_becomes_its_create_index() { + let create = "CREATE TABLE `t` (\n `id` int NOT NULL,\n `KEY x` text,\n PRIMARY KEY (`id`),\n \ + UNIQUE KEY `u` (`id` DESC,`b`(10)) USING HASH,\n KEY `a``b c` ((lower(`b`))) COMMENT 'x\\ny' \ + /*!80000 INVISIBLE */,\n FULLTEXT KEY `f` (`b`) /*!50100 WITH PARSER `ngram` */ ,\n \ + SPATIAL KEY `g` (`p`)\n) ENGINE=InnoDB"; + let statement = |index: &str| index_statement(create, "`d`.`t`", index); + + assert_eq!( + statement("u").as_deref(), + Some("CREATE UNIQUE INDEX `u` ON `d`.`t` (`id` DESC,`b`(10)) USING HASH") + ); + assert_eq!( + statement("a`b c").as_deref(), + Some("CREATE INDEX `a``b c` ON `d`.`t` ((lower(`b`))) COMMENT 'x\\ny' /*!80000 INVISIBLE */") + ); + assert_eq!( + statement("f").as_deref(), + Some("CREATE FULLTEXT INDEX `f` ON `d`.`t` (`b`) /*!50100 WITH PARSER `ngram` */") + ); + assert_eq!( + statement("g").as_deref(), + Some("CREATE SPATIAL INDEX `g` ON `d`.`t` (`p`)") + ); + assert_eq!(statement("x"), None); + assert_eq!(statement("PRIMARY"), None); + } + + #[test] + fn a_line_break_in_a_name_stays_in_its_entry() { + let create = "CREATE TABLE `t` (\n `a\nb` int DEFAULT '7',\n KEY `k\n2` (`a\nb`),\n KEY `next` (`id`)\n)"; + + assert_eq!( + index_statement(create, "`d`.`t`", "k\n2").as_deref(), + Some("CREATE INDEX `k\n2` ON `d`.`t` (`a\nb`)") + ); + assert_eq!(quoted_default(create, "a\nb").as_deref(), Some("7")); + } + + #[test] + fn a_name_that_spells_an_index_line_is_not_taken_for_it() { + let create = "CREATE TABLE \"t\" (\n \"id\" int,\n \"x\n KEY `k` (`id`); DROP TABLE users\nz\" int,\n \ + KEY \"k\" (\"id\")\n)"; + + assert_eq!( + index_statement(create, "`d`.`t`", "k").as_deref(), + Some("CREATE INDEX \"k\" ON `d`.`t` (\"id\")") + ); + } + + #[test] + fn a_statement_reads_the_same_without_backslash_escapes() { + let statement = "CREATE INDEX `k\\` ON `d`.`t` ((concat(`b`,_utf8mb4'it\\'s \\\\ x\\ny'))) \ + COMMENT 'a''b \\\\ c\\%'"; + + assert_eq!( + without_backslash_escapes(statement).as_deref(), + Some("CREATE INDEX `k\\` ON `d`.`t` ((concat(`b`,_utf8mb4'it''s \\ x\ny'))) COMMENT 'a''b \\ c\\%'") + ); + assert_eq!(without_backslash_escapes("COMMENT 'a\\0b'"), None); + } + + #[test] + fn modes_that_trim_show_create_hide_index_options() { + assert!(hides_index_options( + "REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ANSI" + )); + assert!(hides_index_options("NO_KEY_OPTIONS")); + assert!(!hides_index_options("ANSI_QUOTES,STRICT_ALL_TABLES")); + assert!(has_mode( + "STRICT_ALL_TABLES,NO_BACKSLASH_ESCAPES", + "NO_BACKSLASH_ESCAPES" + )); + } + + #[test] + fn an_ansi_quoted_index_line_keeps_its_quotes() { + let create = "CREATE TABLE \"t\" (\n \"id\" int,\n KEY \"k\" (\"id\")\n)"; + + assert_eq!( + index_statement(create, "`d`.`t`", "k").as_deref(), + Some("CREATE INDEX \"k\" ON `d`.`t` (\"id\")") + ); + } + #[test] fn a_quoted_default_is_read_from_its_own_columns_line() { let create = "CREATE TABLE `t` (\n `id` int NOT NULL,\n `at` timestamp NULL DEFAULT '1999-12-31 17:00:00' \ diff --git a/linux/crates/drivers/mysql/src/metadata.rs b/linux/crates/drivers/mysql/src/metadata.rs index ca2f792cc0..8fe21aae1f 100644 --- a/linux/crates/drivers/mysql/src/metadata.rs +++ b/linux/crates/drivers/mysql/src/metadata.rs @@ -8,7 +8,6 @@ use futures_util::TryStreamExt; use sqlx::mysql::MySqlConnection; use sqlx::{AssertSqlSafe, Executor as _, Row as _}; use tablepro_core::column::{ColumnDefault, ColumnInfo, ColumnKind, SqlExpression, TemporalZone}; -use tablepro_core::ddl::ddl_sql::parenthesized; use tablepro_core::ddl::{ CharacterSet, ColumnDefinition, InboundForeignKey, OutboundForeignKey, PrimaryKeyConstraint, ReferentialAction, TableDefinition, TableOptions, TableSource, UnmodeledAttribute, @@ -25,7 +24,8 @@ use crate::catalog::{ ColumnCheck, FULL_COLLATION_NAMES_SQL, FULL_COLLATIONS_SQL, INBOUND_FOREIGN_KEYS_SQL, ListedOptions, ListedSrid, MARIADB_COLUMN_CHECKS_SQL, MARIADB_GEOMETRY_SRIDS_SQL, MySqlColumnRow, TableComparisons, character_sets, column_checks, column_comparisons, column_definition, column_info, columns_sql, compares_alike, geometry_srids, - inbound_foreign_keys, place_column_checks, quoted_default, table_options, unescape_expression, + has_mode, hides_index_options, inbound_foreign_keys, index_statement, place_column_checks, quoted_default, + table_options, without_backslash_escapes, }; use crate::decode::decode_row; use crate::dialect::MySqlDialect; @@ -204,12 +204,12 @@ pub(crate) async fn fetch_columns( /// MySQL 8.0.13 and later name a functional key part in EXPRESSION; /// MariaDB has no such column, and no functional key parts. const MYSQL_INDEXES_SQL: &str = "\ -SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME, COLLATION, EXPRESSION, INDEX_TYPE, SUB_PART +SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME, COLLATION, EXPRESSION FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = COALESCE(?, DATABASE()) AND TABLE_NAME = ? ORDER BY INDEX_NAME = 'PRIMARY' DESC, INDEX_NAME, SEQ_IN_INDEX"; const MARIADB_INDEXES_SQL: &str = "\ -SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME, COLLATION, NULL AS EXPRESSION, INDEX_TYPE, SUB_PART +SELECT INDEX_NAME, NON_UNIQUE, COLUMN_NAME, COLLATION, NULL AS EXPRESSION FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = COALESCE(?, DATABASE()) AND TABLE_NAME = ? ORDER BY INDEX_NAME = 'PRIMARY' DESC, INDEX_NAME, SEQ_IN_INDEX"; @@ -225,97 +225,34 @@ pub(crate) async fn fetch_indexes( false => MARIADB_INDEXES_SQL, }; let rows = catalog_rows(connection, flavour, sql, "STATISTICS", &table_binds(table)).await?; - let mut indexes: Vec<(IndexInfo, String, Vec)> = Vec::new(); + let mut indexes: Vec = Vec::new(); for row in rows { let name = row.required_text(0)?; let descending = row.text(3)?.as_deref() == Some("D"); - let (key, part) = match (row.text(2)?, row.text(4)?) { - (Some(column), _) => { - let prefix = row.number(6)?.map(|length| format!("({length})")).unwrap_or_default(); - let part = format!("{}{prefix}", dialect.quote_identifier(&column)); - ( - IndexKey::Column { - name: column, - descending, - }, - part, - ) - } - (None, Some(expression)) => { - let part = parenthesized(&unescape_expression(&expression)); - ( - IndexKey::Expression { - sql: expression, - descending, - }, - part, - ) - } + let key = match (row.text(2)?, row.text(4)?) { + (Some(column), _) => IndexKey::Column { + name: column, + descending, + }, + (None, Some(expression)) => IndexKey::Expression { + sql: expression, + descending, + }, (None, None) => continue, }; - let part = match descending { - true => format!("{part} DESC"), - false => part, - }; - match indexes.last_mut().filter(|(index, ..)| index.name == name) { - Some((index, _, parts)) => { - index.keys.push(key); - parts.push(part); - } - None => indexes.push(( - IndexInfo { - unique: row.number(1)? == Some(0), - primary: name == "PRIMARY", - name, - keys: vec![key], - predicate: None, - definition: None, - }, - row.text(5)?.unwrap_or_default(), - vec![part], - )), + match indexes.last_mut().filter(|index| index.name == name) { + Some(index) => index.keys.push(key), + None => indexes.push(IndexInfo { + unique: row.number(1)? == Some(0), + primary: name == "PRIMARY", + name, + keys: vec![key], + predicate: None, + definition: None, + }), } } - Ok(indexes - .into_iter() - .map(|(mut index, index_type, parts)| { - index.definition = create_index(dialect, table, &index, &index_type, &parts); - index - }) - .collect()) -} - -/// The CREATE INDEX for an index STATISTICS lists, with the prefix -/// length a TEXT or BLOB key needs, the kind a full-text or spatial -/// index is made with, and the access method, since a MEMORY table -/// would otherwise make a B-tree index as a hash. The primary key is -/// only ever a constraint. -fn create_index( - dialect: &MySqlDialect, - table: &TableRef, - index: &IndexInfo, - index_type: &str, - parts: &[String], -) -> Option { - if index.primary { - return None; - } - let kind = match index_type { - "FULLTEXT" => "FULLTEXT ", - "SPATIAL" => "SPATIAL ", - _ if index.unique => "UNIQUE ", - _ => "", - }; - let method = match index_type { - "BTREE" | "HASH" => format!(" USING {index_type}"), - _ => String::new(), - }; - Some(format!( - "CREATE {kind}INDEX {} ON {} ({}){method}", - dialect.quote_identifier(&index.name), - dialect.qualify(table), - parts.join(", ") - )) + Ok(indexes) } const FOREIGN_KEYS_SQL: &str = "\ @@ -574,7 +511,8 @@ pub(crate) async fn fetch_table_definition( definition.info.default = default; } } - let indexes = fetch_indexes(connection, dialect, table).await?; + let mut indexes = fetch_indexes(connection, dialect, table).await?; + state_indexes(connection, dialect, table, &create_statement, &mut indexes).await?; let primary_key = indexes .iter() .find(|index| index.primary) @@ -746,6 +684,45 @@ async fn engine_attributes( /// The table's own DDL as the server prints it. SHOW cannot take a /// parameter, so the name goes in quoted by the dialect. +async fn state_indexes( + connection: &mut MySqlConnection, + dialect: &MySqlDialect, + table: &TableRef, + create_statement: &str, + indexes: &mut [IndexInfo], +) -> Result<(), DriverError> { + let (printed_in, editors_read_in) = sql_modes(connection, dialect).await?; + if hides_index_options(&printed_in) { + return Ok(()); + } + let qualified = dialect.qualify(table); + let escapes = !has_mode(&editors_read_in, "NO_BACKSLASH_ESCAPES"); + for index in indexes.iter_mut().filter(|index| !index.primary) { + index.definition = + index_statement(create_statement, &qualified, &index.name).and_then(|statement| match escapes { + true => Some(statement), + false => without_backslash_escapes(&statement), + }); + } + Ok(()) +} + +// The mode this session printed SHOW CREATE TABLE in, and the one an editor session starts in. +async fn sql_modes(connection: &mut MySqlConnection, dialect: &MySqlDialect) -> Result<(String, String), DriverError> { + let row = connection + .fetch_one(sqlx::raw_sql("SELECT @@SESSION.sql_mode, @@GLOBAL.sql_mode")) + .await + .map_err(|error| map_error(&error, dialect.flavour()))?; + let text = |index: usize| -> Result { + let bytes: &[u8] = row.try_get_unchecked(index).map_err(|error| DriverError::Decode { + column: "sql_mode".to_owned(), + detail: error.to_string(), + })?; + Ok(String::from_utf8_lossy(bytes).into_owned()) + }; + Ok((text(0)?, text(1)?)) +} + async fn show_create( connection: &mut MySqlConnection, dialect: &MySqlDialect, diff --git a/linux/crates/drivers/mysql/tests/integration/flavour_suite.rs b/linux/crates/drivers/mysql/tests/integration/flavour_suite.rs index 78510b24f9..9f3918a717 100644 --- a/linux/crates/drivers/mysql/tests/integration/flavour_suite.rs +++ b/linux/crates/drivers/mysql/tests/integration/flavour_suite.rs @@ -528,10 +528,6 @@ async fn caching_sha2_without_tls_uses_rsa() { root.close().await.expect("a close"); } -/// Each index's statement, run after the index is dropped, makes the -/// same index again: a full-text index, a prefix on a TEXT column, and -/// on MySQL a functional key part with a quoted literal. A B-tree on a -/// MEMORY table, whose default is a hash, says USING BTREE. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] async fn index_statements_recreate_their_indexes() { @@ -539,56 +535,74 @@ async fn index_statements_recreate_their_indexes() { let fixture = start(flavour).await; let connection = connect(&fixture, false).await; let mut statements = vec![ - "CREATE TABLE notes (id int PRIMARY KEY, title varchar(50), body text)", - "CREATE TABLE cache (id int PRIMARY KEY, k int, INDEX cache_k USING BTREE (k)) ENGINE=MEMORY", + "CREATE TABLE notes (id int PRIMARY KEY, title varchar(50), body text, `a\nb` int)", + "CREATE TABLE cache (id int PRIMARY KEY, k int, INDEX cache_tree USING BTREE (k), \ + INDEX cache_hash (k)) ENGINE=MEMORY", "CREATE UNIQUE INDEX notes_title ON notes (title DESC, id)", "CREATE INDEX notes_body ON notes (body(12))", "CREATE FULLTEXT INDEX notes_search ON notes (title, body)", + "CREATE INDEX `notes\nbroken` ON notes (`a\nb`) COMMENT 'it''s a \\\\ back\\nslash'", ]; - if flavour == MysqlFlavour::Mysql { - statements.push("CREATE INDEX notes_tagged ON notes ((concat(title, 'it''s')))"); - } + let hidden = match flavour { + MysqlFlavour::Mysql => { + statements.extend([ + "CREATE TABLE shapes (id int PRIMARY KEY, p POINT NOT NULL SRID 0, SPATIAL INDEX shapes_p (p))", + "CREATE INDEX notes_tagged ON notes ((concat(title, 'it''s')))", + "CREATE FULLTEXT INDEX notes_words ON notes (body) WITH PARSER ngram", + "CREATE INDEX notes_hidden ON notes (id) INVISIBLE", + ]); + "CREATE INDEX `notes_hidden` ON `tablepro`.`notes` (`id`) /*!80000 INVISIBLE */" + } + MysqlFlavour::Mariadb => { + statements.extend([ + "CREATE TABLE shapes (id int PRIMARY KEY, p POINT NOT NULL, SPATIAL INDEX shapes_p (p))", + "CREATE INDEX notes_hidden ON notes (id) IGNORED", + ]); + "CREATE INDEX `notes_hidden` ON `tablepro`.`notes` (`id`) IGNORED" + } + }; setup(connection.as_ref(), &statements).await; - let cache = crate::support::table("cache"); - let cache_index = connection - .fetch_indexes(&cache, within(CALL)) - .await - .expect("the indexes") - .into_iter() - .find(|index| index.name == "cache_k") - .expect("cache_k"); - assert_eq!( - cache_index.definition.as_deref(), - Some("CREATE INDEX `cache_k` ON `tablepro`.`cache` (`k`) USING BTREE"), - "{flavour:?}: a MEMORY table keeps its B-tree" - ); - let table = crate::support::table("notes"); - let indexes = connection - .fetch_indexes(&table, within(CALL)) - .await - .expect("the indexes"); - + let read = |name: &'static str| { + let connection = connection.as_ref(); + async move { + connection + .fetch_table_definition(&crate::support::table(name), within(CALL)) + .await + .expect("the table") + .indexes + } + }; + let notes = read("notes").await; assert_eq!( - indexes + notes .iter() - .find(|index| index.primary) - .map(|index| &index.definition), - Some(&None), - "{flavour:?}: the primary key is a constraint" + .find(|index| index.name == "notes_hidden") + .and_then(|index| index.definition.as_deref()), + Some(hidden), + "{flavour:?}" ); - for index in indexes.iter().filter(|index| !index.primary) { - let statement = index.definition.clone().expect("a statement"); - let drop = format!("DROP INDEX `{}` ON `notes`", index.name); - setup(connection.as_ref(), &[drop.as_str(), statement.as_str()]).await; - let remade = connection - .fetch_indexes(&table, within(CALL)) - .await - .expect("the indexes"); + + for table in ["notes", "cache", "shapes"] { + let indexes = read(table).await; assert_eq!( - remade.iter().find(|remade| remade.name == index.name), - Some(index), - "{flavour:?}: {statement}" + indexes + .iter() + .find(|index| index.primary) + .map(|index| &index.definition), + Some(&None), + "{flavour:?}: the primary key is a constraint" ); + for index in indexes.iter().filter(|index| !index.primary) { + let statement = index.definition.clone().expect("a statement"); + let drop = format!("DROP INDEX `{}` ON `{table}`", index.name); + setup(connection.as_ref(), &[drop.as_str(), statement.as_str()]).await; + let remade = read(table).await; + assert_eq!( + remade.iter().find(|remade| remade.name == index.name), + Some(index), + "{flavour:?}: {statement}" + ); + } } connection.close().await.expect("a close"); } diff --git a/linux/crates/drivers/postgres/src/ddl.rs b/linux/crates/drivers/postgres/src/ddl.rs index 93235db3b0..ac37814718 100644 --- a/linux/crates/drivers/postgres/src/ddl.rs +++ b/linux/crates/drivers/postgres/src/ddl.rs @@ -737,6 +737,38 @@ mod tests { ); } + #[test] + fn the_script_states_an_index_as_the_server_printed_it() { + let mut table = users(); + let keys = |name: &str| IndexKey::Column { + name: name.to_owned(), + descending: false, + }; + let index = |name: &str, definition: Option<&str>| IndexInfo { + name: name.to_owned(), + keys: vec![keys("email")], + unique: false, + primary: false, + predicate: None, + definition: definition.map(str::to_owned), + }; + table.indexes = vec![ + index( + "by_email", + Some("CREATE INDEX by_email ON app.users USING gin (email gin_trgm_ops)"), + ), + index("users_email_key", None), + ]; + + assert_eq!( + sql(dialect().create_table_script(&table))[1..], + [ + "CREATE INDEX \"users_email_key\" ON \"app\".\"users\" (\"email\")", + "CREATE INDEX by_email ON app.users USING gin (email gin_trgm_ops)", + ] + ); + } + #[test] fn plan_create_table_composite_key_index_and_foreign_key() { let mut draft = TableDraft::new(TableRef::unqualified("lines")); diff --git a/linux/crates/drivers/postgres/src/metadata/indexes.rs b/linux/crates/drivers/postgres/src/metadata/indexes.rs index 108ff1169f..7bb7cbe618 100644 --- a/linux/crates/drivers/postgres/src/metadata/indexes.rs +++ b/linux/crates/drivers/postgres/src/metadata/indexes.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use tablepro_core::DriverError; use tablepro_core::meta::{IndexInfo, IndexKey}; use tokio_postgres::Client; @@ -6,8 +8,7 @@ use super::{decode_error, text_rows}; /// One row per key column, the INCLUDE columns past `indnkeyatts` left /// out. Attribute number 0 marks an expression, which only -/// `pg_get_indexdef` can print, and with no column number it prints the -/// whole CREATE INDEX statement. +/// `pg_get_indexdef` can print. pub(super) const INDEXES_SQL: &str = "\ SELECT ic.relname::text, i.indisunique, @@ -15,8 +16,7 @@ SELECT ic.relname::text, pg_catalog.pg_get_expr(i.indpred, i.indrelid)::text, a.attname::text, pg_catalog.pg_get_indexdef(i.indexrelid, k.ord::int4, true)::text, - COALESCE(pg_catalog.pg_index_column_has_property(i.indexrelid, k.ord::int4, 'desc'), false), - pg_catalog.pg_get_indexdef(i.indexrelid)::text + COALESCE(pg_catalog.pg_index_column_has_property(i.indexrelid, k.ord::int4, 'desc'), false) FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class ic ON ic.oid = i.indexrelid CROSS JOIN LATERAL pg_catalog.unnest(i.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord) @@ -47,9 +47,121 @@ pub(crate) async fn fetch_indexes(client: &Client, relation: &str) -> Result Result, DriverError> { + let field = decode_error("pg_index"); + text_rows(client, INDEX_STATEMENTS_SQL, &[relation]) + .await? + .iter() + .map(|row| { + let mut statement: String = row.try_get(1).map_err(&field)?; + if row.try_get(2).map_err(&field)? { + let quoted_name: String = row.try_get(4).map_err(&field)?; + statement = without_only(statement, row.try_get(3).map_err(&field)?, "ed_name); + } + if let Some(tablespace) = row.try_get::<_, Option>(5).map_err(&field)? { + let predicate: Option = row.try_get(6).map_err(&field)?; + statement = in_tablespace(statement, &tablespace, predicate.as_deref()); + } + Ok((row.try_get(0).map_err(&field)?, statement)) + }) + .collect() +} + +// pg_get_indexdef prints ON ONLY for a partitioned table's index and has no form without it; +// run as is, it builds the parent's index alone, invalid and on no partition. +fn without_only(statement: String, unique: bool, quoted_name: &str) -> String { + let head = format!("CREATE {}INDEX {quoted_name} ON ", if unique { "UNIQUE " } else { "" }); + match statement + .strip_prefix(&head) + .and_then(|rest| rest.strip_prefix("ONLY ")) + { + Some(rest) => format!("{head}{rest}"), + None => statement, + } +} + +// pg_get_indexdef leaves the tablespace out. It goes before the predicate, which ends the text. +fn in_tablespace(statement: String, tablespace: &str, predicate: Option<&str>) -> String { + let clause = format!(" TABLESPACE {tablespace}"); + let head = predicate.and_then(|predicate| statement.strip_suffix(&format!(" WHERE {predicate}"))); + match (head, predicate) { + (Some(head), Some(predicate)) => format!("{head}{clause} WHERE {predicate}"), + _ => format!("{statement}{clause}"), + } +} + +#[cfg(test)] +mod tests { + use super::{in_tablespace, without_only}; + + #[test] + fn a_partitioned_index_is_built_on_every_partition() { + assert_eq!( + without_only( + "CREATE UNIQUE INDEX \"Only\" ON ONLY public.p USING btree (k)".to_owned(), + true, + "\"Only\"" + ), + "CREATE UNIQUE INDEX \"Only\" ON public.p USING btree (k)" + ); + assert_eq!( + without_only( + "CREATE INDEX p_k ON ONLY public.p USING btree (k)".to_owned(), + false, + "p_k" + ), + "CREATE INDEX p_k ON public.p USING btree (k)" + ); + } + + #[test] + fn a_table_named_only_keeps_its_name() { + let statement = "CREATE INDEX t_k ON public.only USING btree (k)".to_owned(); + + assert_eq!(without_only(statement.clone(), false, "t_k"), statement); + } + + #[test] + fn the_tablespace_goes_before_the_predicate() { + let partial = "CREATE INDEX t_k ON public.t USING btree (k) WITH (fillfactor='70') WHERE (k > 0)".to_owned(); + + assert_eq!( + in_tablespace(partial, "\"Fast\"", Some("(k > 0)")), + "CREATE INDEX t_k ON public.t USING btree (k) WITH (fillfactor='70') TABLESPACE \"Fast\" WHERE (k > 0)" + ); + assert_eq!( + in_tablespace("CREATE INDEX t_k ON public.t USING btree (k)".to_owned(), "fast", None), + "CREATE INDEX t_k ON public.t USING btree (k) TABLESPACE fast" + ); + } +} diff --git a/linux/crates/drivers/postgres/src/metadata/mod.rs b/linux/crates/drivers/postgres/src/metadata/mod.rs index b7ad20caac..fda75016bb 100644 --- a/linux/crates/drivers/postgres/src/metadata/mod.rs +++ b/linux/crates/drivers/postgres/src/metadata/mod.rs @@ -89,13 +89,17 @@ pub(crate) async fn fetch_table_definition( // Every query is sent before the first answer is awaited, so all of // them run inside the flight's transaction, ahead of its COMMIT. let params = [relation]; - let (columns, primary_key, indexes, foreign_keys, inbound_foreign_keys) = futures_util::try_join!( + let (columns, primary_key, mut indexes, mut statements, foreign_keys, inbound_foreign_keys) = futures_util::try_join!( column_rows(client, columns_sql, relation), text_rows(client, PRIMARY_KEY_NAME_SQL, ¶ms), fetch_indexes(client, relation), + indexes::fetch_index_statements(client, relation), fetch_foreign_keys(client, relation), fetch_inbound_foreign_keys(client, relation), )?; + for index in &mut indexes { + index.definition = statements.remove(&index.name); + } let primary_key = primary_key .first() .map(|row| -> Result { @@ -175,6 +179,7 @@ mod tests { ("objects", objects::OBJECTS_SQL.to_owned()), ("identity", identity::IDENTITY_SQL.to_owned()), ("indexes", indexes::INDEXES_SQL.to_owned()), + ("index statements", indexes::INDEX_STATEMENTS_SQL.to_owned()), ("estimate", estimate::ESTIMATE_SQL.to_owned()), ("foreign keys", foreign_keys::foreign_keys_sql()), ("inbound foreign keys", inbound_foreign_keys_sql()), diff --git a/linux/crates/drivers/postgres/tests/integration/metadata.rs b/linux/crates/drivers/postgres/tests/integration/metadata.rs index 5be3d3b082..df16ee7edd 100644 --- a/linux/crates/drivers/postgres/tests/integration/metadata.rs +++ b/linux/crates/drivers/postgres/tests/integration/metadata.rs @@ -129,10 +129,7 @@ async fn expression_desc_partial_index_keys() { ] ); assert!(!partial.can_identify_rows()); - assert_eq!( - partial.definition.as_deref(), - Some("CREATE UNIQUE INDEX t_label ON public.t USING btree (lower(label) DESC, id) WHERE (id > 0)") - ); + assert!(indexes.iter().all(|index| index.definition.is_none())); assert!( indexes .iter() @@ -140,6 +137,82 @@ async fn expression_desc_partial_index_keys() { ); } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +#[ignore = "requires docker"] +async fn index_statements_are_the_servers_and_skip_constraint_indexes() { + let fixture = start().await; + let connection = connect(&fixture, false).await; + setup( + connection.as_ref(), + &[ + "CREATE TABLE q (id int PRIMARY KEY, u int UNIQUE, r int4range, doc jsonb, \ + EXCLUDE USING gist (r WITH &&))", + "CREATE INDEX q_doc ON q USING gin (doc jsonb_path_ops)", + "CREATE TABLE q_child (q_id int REFERENCES q (id))", + "CREATE TABLE p (k int) PARTITION BY RANGE (k)", + "CREATE TABLE p_low PARTITION OF p FOR VALUES FROM (0) TO (10)", + "CREATE TABLE p_high PARTITION OF p FOR VALUES FROM (10) TO (20)", + "CREATE INDEX p_k ON p (k)", + ], + ) + .await; + + let q = connection + .fetch_table_definition(&table("q"), within(CALL)) + .await + .expect("q"); + let statements: Vec<(&str, Option<&str>)> = q + .indexes + .iter() + .map(|index| (index.name.as_str(), index.definition.as_deref())) + .collect(); + assert_eq!( + statements, + [ + ( + "q_doc", + Some("CREATE INDEX q_doc ON public.q USING gin (doc jsonb_path_ops)") + ), + ("q_pkey", None), + ("q_r_excl", None), + ("q_u_key", None), + ] + ); + + let read = |name: &'static str| { + let connection = connection.as_ref(); + async move { + connection + .fetch_table_definition(&table(name), within(CALL)) + .await + .expect("the table") + .indexes + } + }; + let attached = read("p_low").await; + assert_eq!(attached.len(), 1); + assert_eq!(attached[0].definition, None, "only the parent's statement makes it"); + let parent = read("p").await; + let statement = parent[0].definition.clone().expect("the parent's statement"); + assert_eq!(statement, "CREATE INDEX p_k ON public.p USING btree (k)"); + setup(connection.as_ref(), &["DROP INDEX p_k"]).await; + setup(connection.as_ref(), &[statement.as_str()]).await; + + assert_eq!(read("p").await, parent); + assert_eq!(read("p_low").await.len(), 1); + assert_eq!(read("p_high").await.len(), 1); + assert_eq!( + text( + &scalar( + connection.as_ref(), + "SELECT bool_and(indisvalid)::text FROM pg_index WHERE indrelid = 'p'::regclass" + ) + .await + ), + "true" + ); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[ignore = "requires docker"] async fn identity_pk_unique_notnull_ctid_partitioned() { diff --git a/linux/crates/drivers/sqlite/src/metadata.rs b/linux/crates/drivers/sqlite/src/metadata.rs index 238e05840c..5abf11e4f4 100644 --- a/linux/crates/drivers/sqlite/src/metadata.rs +++ b/linux/crates/drivers/sqlite/src/metadata.rs @@ -708,19 +708,13 @@ fn qualifier(schema: &str) -> Option { (schema != MAIN_SCHEMA).then(|| schema.to_owned()) } -/// SQLite keeps an index's SQL with no schema and with its head -/// normalized to `CREATE [UNIQUE] INDEX`, so the index name follows -/// that head. Outside `main` the name takes its schema, or the -/// statement would make the index on a table of `main`. +// SQLite stores the name bare, and a TEMP table of the same name would catch an unqualified statement. fn in_schema(sql: &str, schema: &str) -> String { - let Some(schema) = qualifier(schema) else { - return sql.to_owned(); - }; let head = ["CREATE UNIQUE INDEX ", "CREATE INDEX "] .into_iter() .find(|head| sql.starts_with(head)); match head { - Some(head) => format!("{head}{}.{}", quote(&schema), &sql[head.len()..]), + Some(head) => format!("{head}{}.{}", quote(schema), &sql[head.len()..]), None => sql.to_owned(), } } diff --git a/linux/crates/drivers/sqlite/tests/integration/metadata.rs b/linux/crates/drivers/sqlite/tests/integration/metadata.rs index ef05381f71..71080d041f 100644 --- a/linux/crates/drivers/sqlite/tests/integration/metadata.rs +++ b/linux/crates/drivers/sqlite/tests/integration/metadata.rs @@ -57,9 +57,6 @@ async fn expression_desc_partial_index_keys() { connection.close().await.unwrap(); } -/// An attached database's index keeps its SQL without the schema, so -/// its statement names the schema, or it would index `main`'s table of -/// the same name. #[tokio::test] async fn an_attached_index_statement_names_its_schema() { let file = TestFile::new().await; @@ -103,6 +100,35 @@ async fn an_attached_index_statement_names_its_schema() { connection.close().await.unwrap(); } +#[tokio::test] +async fn a_main_index_statement_is_not_caught_by_a_temp_table() { + let file = TestFile::new().await; + let connection = file.open().await; + query( + connection.as_ref(), + "CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)", + ) + .await; + query(connection.as_ref(), "CREATE INDEX by_name ON people (name)").await; + query(connection.as_ref(), "CREATE TEMP TABLE people (id INTEGER, name TEXT)").await; + let main = TableRef::new(Some("main".to_owned()), "people"); + let read = connection.fetch_indexes(&main, call()).await.unwrap(); + let statement = read + .iter() + .find(|index| index.name == "by_name") + .and_then(|index| index.definition.clone()) + .expect("a statement"); + + query(connection.as_ref(), "DROP INDEX main.by_name").await; + query(connection.as_ref(), &statement).await; + + assert_eq!(statement, "CREATE INDEX \"main\".by_name ON people (name)"); + assert_eq!(connection.fetch_indexes(&main, call()).await.unwrap(), read); + let temp = TableRef::new(Some("temp".to_owned()), "people"); + assert!(connection.fetch_indexes(&temp, call()).await.unwrap().is_empty()); + connection.close().await.unwrap(); +} + #[tokio::test] async fn views_listed_and_attached_schema_listed() { let file = TestFile::new().await; diff --git a/linux/crates/test-support/src/conformance/cases/ddl.rs b/linux/crates/test-support/src/conformance/cases/ddl.rs index 55d8c85051..b5e94c2fd7 100644 --- a/linux/crates/test-support/src/conformance/cases/ddl.rs +++ b/linux/crates/test-support/src/conformance/cases/ddl.rs @@ -309,8 +309,7 @@ pub async fn structure_changes_keep_rows_and_indexes( Ok(()) } -/// Case 5: the statement an index reads back with, run after the index -/// is dropped, makes the same index again. +// Case 5: an index's statement, run after the index is dropped, makes the same index again. pub async fn index_statement_recreates_the_index(engine: E) -> Result<(), ConformanceFailure> { let case = INDEX_CASE; let connection = connect(case, &engine, writable()).await?; diff --git a/linux/po/vi.po b/linux/po/vi.po index 3e2a717c45..d2f3410e83 100644 --- a/linux/po/vi.po +++ b/linux/po/vi.po @@ -4824,7 +4824,7 @@ msgid "Show _CREATE TABLE" msgstr "_Hiện CREATE TABLE" #: crates/app/src/ui/structure_tab/indexes.rs -msgid "Show _CREATE INDEX" +msgid "_Show CREATE INDEX" msgstr "_Hiện CREATE INDEX" #: crates/app/src/ui/structure_tab/indexes.rs From 3c7c801b01df29b26b9944213dfed67afcb56639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Nam=20Long?= Date: Sun, 1 Nov 2026 18:30:41 +0700 Subject: [PATCH 85/85] feat(live-messages): follow PostgreSQL, Redis and Kafka messages live (#3981) Co-authored-by: Ngo Quoc Dat --- .../KafkaPluginDriver+LiveMessages.swift | 140 +++++++++ .../KafkaDriverPlugin/KafkaPluginDriver.swift | 2 + .../LibPQDriverCore.swift | 5 + .../LibPQPluginConnection.swift | 33 +++ .../PGlitePluginDriver.swift | 2 + .../PostgreSQLPluginDriver+LiveMessages.swift | 96 +++++++ .../PostgreSQLPluginDriver.swift | 1 + .../PostgreSQLListenStatementsTests.swift | 18 ++ .../RedisClusterChannel.swift | 5 + .../RedisCommandChannel.swift | 5 + .../RedisPluginConnection.swift | 74 +++++ .../RedisPluginDriver+LiveMessages.swift | 127 +++++++++ .../RedisDriverPlugin/RedisPluginDriver.swift | 2 + .../RedisSentinelChannel.swift | 2 + .../RedisDriverTests/RedisLivePushTests.swift | 45 +++ .../PluginLiveMessaging.swift | 75 +++++ .../Database/DatabaseManager+Sessions.swift | 1 + .../Core/LiveMessages/LiveMessageBuffer.swift | 84 ++++++ .../LiveMessages/LiveMessageController.swift | 215 ++++++++++++++ .../Core/LiveMessages/LiveMessageExport.swift | 87 ++++++ .../LiveMessageSessionSource.swift | 96 +++++++ .../LiveMessages/LiveMessageSessions.swift | 57 ++++ .../MainContentCoordinator+LiveMessages.swift | 62 ++++ .../Tools/MCPTabSnapshotProvider.swift | 2 +- .../Core/Menu/DatabaseMenuBuilder.swift | 31 +- .../TablePro/Core/Menu/MainMenuBuilder.swift | 5 +- apple/TablePro/Core/Modules/AppModule.swift | 5 +- .../Core/Modules/AppModuleActivation.swift | 2 + .../Core/Modules/AppModuleTermination.swift | 1 + .../Core/Scripting/ScriptEnumerations.swift | 2 +- .../EditorTabDetachPolicy.swift | 5 +- .../Infrastructure/EditorTabOpener.swift | 4 +- ...litViewController+LiveMessageActions.swift | 18 ++ ...inSplitViewController+MenuValidation.swift | 6 + .../MainSplitViewController+RecentTabs.swift | 2 + .../MainSplitViewController.swift | 2 +- .../Toolbar/ToolbarContextResolver.swift | 6 +- .../Infrastructure/WindowTitleResolver.swift | 5 + .../Models/Query/QueryTabManager.swift | 12 +- .../TablePro/Models/Query/QueryTabState.swift | 5 +- .../Models/UI/GridSelectionOwner.swift | 2 +- .../Models/UI/PendingChangeKind.swift | 2 +- .../TablePro/Resources/Localizable.xcstrings | 96 +++++++ .../LiveMessages/LiveMessageStartSheet.swift | 120 ++++++++ .../LiveMessages/LiveMessageTabView.swift | 265 ++++++++++++++++++ .../Main/Child/MainEditorContentView.swift | 3 + .../MainContentCoordinator+Refresh.swift | 2 +- .../MainContentCoordinator+TabClosing.swift | 1 + .../Views/Main/MainContentCoordinator.swift | 1 + .../LiveMessages/LiveMessageBufferTests.swift | 58 ++++ .../LiveMessageControllerTests.swift | 174 ++++++++++++ .../LiveMessages/LiveMessageExportTests.swift | 41 +++ .../LiveMessagesModuleResidueTests.swift | 40 +++ .../EditorTabConnectionMovePolicyTests.swift | 2 +- apple/changelog.d/3981.added.md | 1 + 55 files changed, 2134 insertions(+), 21 deletions(-) create mode 100644 apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver+LiveMessages.swift create mode 100644 apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+LiveMessages.swift create mode 100644 apple/Plugins/PostgreSQLDriverTests/PostgreSQLListenStatementsTests.swift create mode 100644 apple/Plugins/RedisDriverPlugin/RedisPluginDriver+LiveMessages.swift create mode 100644 apple/Plugins/RedisDriverTests/RedisLivePushTests.swift create mode 100644 apple/Plugins/TableProPluginKit/PluginLiveMessaging.swift create mode 100644 apple/TablePro/Core/LiveMessages/LiveMessageBuffer.swift create mode 100644 apple/TablePro/Core/LiveMessages/LiveMessageController.swift create mode 100644 apple/TablePro/Core/LiveMessages/LiveMessageExport.swift create mode 100644 apple/TablePro/Core/LiveMessages/LiveMessageSessionSource.swift create mode 100644 apple/TablePro/Core/LiveMessages/LiveMessageSessions.swift create mode 100644 apple/TablePro/Core/LiveMessages/MainContentCoordinator+LiveMessages.swift create mode 100644 apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+LiveMessageActions.swift create mode 100644 apple/TablePro/Views/LiveMessages/LiveMessageStartSheet.swift create mode 100644 apple/TablePro/Views/LiveMessages/LiveMessageTabView.swift create mode 100644 apple/TableProTests/Core/LiveMessages/LiveMessageBufferTests.swift create mode 100644 apple/TableProTests/Core/LiveMessages/LiveMessageControllerTests.swift create mode 100644 apple/TableProTests/Core/LiveMessages/LiveMessageExportTests.swift create mode 100644 apple/TableProTests/Core/LiveMessages/LiveMessagesModuleResidueTests.swift create mode 100644 apple/changelog.d/3981.added.md diff --git a/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver+LiveMessages.swift b/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver+LiveMessages.swift new file mode 100644 index 0000000000..c71a3f8b62 --- /dev/null +++ b/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver+LiveMessages.swift @@ -0,0 +1,140 @@ +import Foundation +import os +import TableProPluginKit + +// Follows topics from each partition's end offset with plain Fetch requests and no consumer group, so it never +// joins a group or moves a group's committed offsets. +extension KafkaPluginDriver: PluginLiveMessaging { + var liveMessageKind: PluginLiveMessageKind? { .topics } + + func subscribe( + _ request: PluginLiveSubscriptionRequest, + sessions: any PluginLiveSessionSource + ) async throws -> any PluginLiveSubscription { + guard let follower = try await sessions.openSession() as? KafkaPluginDriver else { + throw KafkaError.notConnected + } + let cluster = follower.liveCluster + var cursors: [KafkaFollowCursor] = [] + for topicName in request.channels { + let metadata = try await cluster.metadata(topics: [topicName], refresh: true) + let topic = try metadata.requireTopic(named: topicName) + let partitions = topic.partitions.map(\.index).sorted() + let ends = try await KafkaOffsetsRequest.listOffsets( + topic: topicName, + partitions: partitions, + timestamp: KafkaOffsetsRequest.latestTimestamp, + cluster: cluster + ) + cursors += KafkaFollowCursor.cursors(topic: topicName, ends: ends) + } + return KafkaLiveSubscription(cluster: cluster, cursors: cursors) + } +} + +struct KafkaFollowCursor: Sendable, Equatable { + let topic: String + let partition: Int32 + let nextOffset: Int64 + + static func cursors(topic: String, ends: [Int32: Int64]) -> [KafkaFollowCursor] { + ends.keys.sorted().compactMap { partition in + ends[partition].map { KafkaFollowCursor(topic: topic, partition: partition, nextOffset: $0) } + } + } + + static func message(from record: KafkaRecord, topic: String) -> PluginLiveMessage { + PluginLiveMessage( + timestamp: Date(timeIntervalSince1970: TimeInterval(record.timestamp) / 1_000), + channel: topic, + payload: text(record.value) ?? "", + key: text(record.key), + partition: Int(record.partition), + offset: record.offset + ) + } + + // A payload that is not UTF-8 reads as hex, the way the topic grid shows it. + static func text(_ data: Data?) -> String? { + guard let data else { return nil } + return String(data: data, encoding: .utf8) ?? data.map { String(format: "%02X", $0) }.joined() + } +} + +final class KafkaLiveSubscription: PluginLiveSubscription, @unchecked Sendable { + private static let recordsPerFetch = 500 + private static let idleWait: Duration = .milliseconds(500) + private static let logger = Logger(subsystem: "com.TablePro.KafkaDriver", category: "LiveMessages") + + let messages: AsyncThrowingStream + private let cluster: KafkaCluster + private let reader: Task + + init(cluster: KafkaCluster, cursors: [KafkaFollowCursor]) { + self.cluster = cluster + let (stream, continuation) = AsyncThrowingStream.makeStream() + messages = stream + reader = Task { + do { + // Each Fetch waits up to 500 ms on the broker when the partition has nothing new, so no loop spins. + try await withThrowingTaskGroup(of: Void.self) { group in + for cursor in cursors { + group.addTask { + var offset = cursor.nextOffset + while !Task.isCancelled { + let started = ContinuousClock.now + let page = try await KafkaFetchRequest.fetch( + topic: cursor.topic, + partition: cursor.partition, + startOffset: offset, + maximumRecords: Self.recordsPerFetch, + cluster: cluster + ) + for record in page.records { + continuation.yield(KafkaFollowCursor.message(from: record, topic: cursor.topic)) + } + if let last = page.records.last { + offset = last.offset + 1 + } else if ContinuousClock.now - started < Self.idleWait / 2 { + // A trailing transaction marker answers at once and reads as no record, + // where an idle partition makes the broker wait out the long poll. + try await Task.sleep(for: Self.idleWait) + } + } + } + } + // The first partition that fails ends the follow, and leaving the group cancels the others. + while try await group.next() != nil {} + } + continuation.finish() + } catch { + guard !Task.isCancelled else { + continuation.finish() + return + } + Self.logger.info("Topic follow ended: \(String(describing: type(of: error)), privacy: .public) \(error.localizedDescription, privacy: .private)") + continuation.finish(throwing: error) + } + } + } + + func publish(_ payload: String, to channel: String) async throws { + let metadata = try await cluster.metadata(topics: [channel]) + let topic = try metadata.requireTopic(named: channel) + guard let partition = topic.partitions.map(\.index).min() else { throw KafkaError.unknownTopic(channel) } + _ = try await KafkaProduceRequest.produce( + topic: channel, + partition: partition, + key: nil, + value: Data(payload.utf8), + headers: [], + timestamp: Int64(Date().timeIntervalSince1970 * 1_000), + cluster: cluster + ) + } + + func close() async { + reader.cancel() + _ = await reader.value + } +} diff --git a/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver.swift b/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver.swift index 85462fc501..d532868e48 100644 --- a/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver.swift +++ b/apple/Plugins/KafkaDriverPlugin/KafkaPluginDriver.swift @@ -13,6 +13,8 @@ final class KafkaPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private let state = KafkaDriverState() private let anchors = KafkaAnchorStore() + var liveCluster: KafkaCluster { cluster } + var capabilities: PluginCapabilities { [] } /// The config is read here and not retained. It carries the password, and nothing after diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift index 6355c2c140..6f02976256 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift @@ -304,6 +304,11 @@ final class LibPQDriverCore: @unchecked Sendable { libpqConnection?.observeServerNotices(observer) } + func nextNotifications(waitMicroseconds: Int64) async throws -> [LibPQNotification] { + guard let connection = libpqConnection else { throw LibPQPluginError.notConnected } + return try await connection.nextNotifications(waitMicroseconds: waitMicroseconds) + } + func cancelQueryKeepingReply() { libpqConnection?.requestServerCancelKeepingReply() } diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift index abc295d78d..0c2d48583d 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift @@ -628,6 +628,39 @@ final class LibPQPluginConnection: @unchecked Sendable { typeNames.merge(names) } + // MARK: - Notifications + + // Waits at most `waitMicroseconds` on the queue, so a NOTIFY sent on this session gets its turn between waits. + func nextNotifications(waitMicroseconds: Int64) async throws -> [LibPQNotification] { + try await pluginDispatchAsync(on: queue) { [self] in + guard !isShuttingDown, let conn = connectionHandle else { throw LibPQPluginError.notConnected } + var received = Self.drainNotifications(conn) + guard received.isEmpty else { return received } + let socket = PQsocket(conn) + guard socket >= 0 else { throw connectionError(from: conn) } + let deadline = pg_usec_time_t(Int64(PQgetCurrentTimeUSec()) + waitMicroseconds) + guard PQsocketPoll(socket, 1, 0, deadline) >= 0 else { throw connectionError(from: conn) } + guard PQconsumeInput(conn) != 0, PQstatus(conn) == CONNECTION_OK else { + stateLock.withLock { _hasLostConnection = true } + throw connectionError(from: conn) + } + received = Self.drainNotifications(conn) + return received + } + } + + private static func drainNotifications(_ conn: OpaquePointer) -> [LibPQNotification] { + var received: [LibPQNotification] = [] + while let notify = PQnotifies(conn) { + received.append(LibPQNotification( + channel: String(cString: notify.pointee.relname), + payload: notify.pointee.extra.map { String(cString: $0) } ?? "" + )) + PQfreemem(notify) + } + return received + } + // MARK: - Query Cancellation func cancelCurrentQuery() { diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PGlitePluginDriver.swift b/apple/Plugins/PostgreSQLDriverPlugin/PGlitePluginDriver.swift index 9c29dd68d6..39684d302e 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PGlitePluginDriver.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PGlitePluginDriver.swift @@ -28,6 +28,8 @@ final class PGlitePluginDriver: PostgreSQLPluginDriver { // pldebugger is a server C extension and needs a second session; PGlite has neither. override var canDebugRoutines: Bool { false } + // A second session would open a different, empty database, so a NOTIFY from the person's session never arrives. + override var canFollowNotifications: Bool { false } override func connect() async throws { do { diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+LiveMessages.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+LiveMessages.swift new file mode 100644 index 0000000000..36d7f6ed9b --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+LiveMessages.swift @@ -0,0 +1,96 @@ +// +// PostgreSQLPluginDriver+LiveMessages.swift +// PostgreSQLDriverPlugin +// + +import Foundation +import os +import TableProLogRedaction +import TableProPluginKit + +struct LibPQNotification: Sendable, Equatable { + let channel: String + let payload: String +} + +// Redshift and CockroachDB are siblings of this class and never conform; PGlite turns it off through +// `canFollowNotifications`. +extension PostgreSQLPluginDriver: PluginLiveMessaging { + var liveMessageKind: PluginLiveMessageKind? { + canFollowNotifications ? .notifications : nil + } + + func subscribe( + _ request: PluginLiveSubscriptionRequest, + sessions: any PluginLiveSessionSource + ) async throws -> any PluginLiveSubscription { + guard canFollowNotifications, + let listener = try await sessions.openSession() as? PostgreSQLPluginDriver + else { throw LibPQPluginError.notConnected } + listener.core.interruptBlockedCallOnDisconnect() + for statement in PostgreSQLListenStatements.listen(request.channels) { + _ = try await listener.execute(query: statement) + } + return PostgreSQLLiveSubscription(listener: listener) + } +} + +enum PostgreSQLListenStatements { + static let notifyStatement = "SELECT pg_catalog.pg_notify($1, $2)" + + // LISTEN takes an identifier, never a parameter, so each channel is quoted as one. + static func listen(_ channels: [String]) -> [String] { + channels.map { "LISTEN \(PostgreSQLObjectQueries.quoteIdentifier($0))" } + } +} + +final class PostgreSQLLiveSubscription: PluginLiveSubscription, @unchecked Sendable { + // Short, so a NOTIFY sent on this session runs between two waits instead of behind a long one. + private static let waitMicroseconds: Int64 = 250_000 + private static let logger = Logger(subsystem: "com.TablePro.PostgreSQLDriver", category: "LiveMessages") + + let messages: AsyncThrowingStream + private let listener: PostgreSQLPluginDriver + private let reader: Task + + init(listener: PostgreSQLPluginDriver) { + self.listener = listener + let (stream, continuation) = AsyncThrowingStream.makeStream() + messages = stream + reader = Task { + do { + while !Task.isCancelled { + let batch = try await listener.core.nextNotifications(waitMicroseconds: Self.waitMicroseconds) + let arrived = Date() + for notification in batch { + continuation.yield(PluginLiveMessage( + timestamp: arrived, + channel: notification.channel, + payload: notification.payload + )) + } + } + continuation.finish() + } catch { + guard !Task.isCancelled else { + continuation.finish() + return + } + Self.logger.info("Notification read ended: \(LogRedaction.publicDescription(of: error), privacy: .public) \(error.localizedDescription, privacy: .private)") + continuation.finish(throwing: error) + } + } + } + + func publish(_ payload: String, to channel: String) async throws { + _ = try await listener.executeParameterized( + query: PostgreSQLListenStatements.notifyStatement, + parameters: [.text(channel), .text(payload)] + ) + } + + func close() async { + reader.cancel() + _ = await reader.value + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift index 16732a7857..52e28d5670 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift @@ -56,6 +56,7 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { // In the class body so PGlite, which inherits the debugger conformance, can turn it off. var canDebugRoutines: Bool { true } + var canFollowNotifications: Bool { true } init(config: DriverConnectionConfig, singleConnectionMode: Bool = false) { self.core = LibPQDriverCore(config: config, singleConnectionMode: singleConnectionMode) diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLListenStatementsTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLListenStatementsTests.swift new file mode 100644 index 0000000000..f1bd371a1e --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLListenStatementsTests.swift @@ -0,0 +1,18 @@ +import Foundation +import TableProPluginKit +import Testing + +struct PostgreSQLListenStatementsTests { + @Test("Each channel is LISTENed as a quoted identifier, so case and quotes survive") + func listenQuotesIdentifiers() { + #expect(PostgreSQLListenStatements.listen(["Orders", "a\"b"]) == [ + "LISTEN \"Orders\"", + "LISTEN \"a\"\"b\"", + ]) + } + + @Test("NOTIFY goes through pg_notify with bound parameters, never a spliced payload") + func notifyIsParameterized() { + #expect(PostgreSQLListenStatements.notifyStatement == "SELECT pg_catalog.pg_notify($1, $2)") + } +} diff --git a/apple/Plugins/RedisDriverPlugin/RedisClusterChannel.swift b/apple/Plugins/RedisDriverPlugin/RedisClusterChannel.swift index d8411d804c..9564c102b2 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisClusterChannel.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisClusterChannel.swift @@ -76,6 +76,11 @@ final class RedisClusterChannel: RedisCommandChannel, @unchecked Sendable { var supportsDatabaseSelection: Bool { servedDatabaseCount > 1 } + /// Any node: the cluster bus delivers every classic PUBLISH to every node's subscribers. + func pubSubConnection() -> RedisPluginConnection? { + lock.withLock { connections.values.lazy.compactMap { $0 as? RedisPluginConnection }.first } + } + private var servedDatabaseCount: Int { lock.lock() defer { lock.unlock() } diff --git a/apple/Plugins/RedisDriverPlugin/RedisCommandChannel.swift b/apple/Plugins/RedisDriverPlugin/RedisCommandChannel.swift index 240d88868e..db79b92cfa 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisCommandChannel.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisCommandChannel.swift @@ -85,9 +85,14 @@ protocol RedisCommandChannel: AnyObject, Sendable { /// Sentinel needs this because a demoted primary keeps answering `role:master` and keeps /// accepting writes for several seconds after the quorum has moved on. func verifyStillPrimary() async throws + + /// The one node connection a dedicated driver subscribes on; nil when the channel has none. + func pubSubConnection() -> RedisPluginConnection? } extension RedisCommandChannel { + func pubSubConnection() -> RedisPluginConnection? { nil } + var supportsDatabaseSelection: Bool { true } var supportsTransactions: Bool { true } var partitionsKeyspace: Bool { false } diff --git a/apple/Plugins/RedisDriverPlugin/RedisPluginConnection.swift b/apple/Plugins/RedisDriverPlugin/RedisPluginConnection.swift index ff0e1fe1e5..c63ca1f85b 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisPluginConnection.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisPluginConnection.swift @@ -412,6 +412,80 @@ final class RedisPluginConnection: RedisCommandChannel, @unchecked Sendable { extension RedisPluginConnection: RedisClusterNodeConnection {} +// MARK: - Pub/Sub + +extension RedisPluginConnection { + func pubSubConnection() -> RedisPluginConnection? { self } + + /// Sends without reading: a subscribed connection answers with one confirmation per channel, + /// which `nextPushReplies` reads with the messages. + func sendSubscription(_ args: [Data]) async throws { + try await pluginDispatchAsync(on: queue) { [self] in + #if canImport(CRedis) + let ctx = try subscribedContext() + let argc = Int32(args.count) + try RedisCommandArgv.withPointers(args) { argv, argvlen in + guard redisAppendCommandArgv(ctx, argc, argv, argvlen) == REDIS_OK else { + throw transportFailure(ctx, delivered: false) + } + } + var done: Int32 = 0 + while done == 0 { + guard redisBufferWrite(ctx, &done) == REDIS_OK else { + throw transportFailure(ctx, delivered: false) + } + } + #else + throw RedisPluginError.notConnected + #endif + } + } + + /// Waits at most `waitMilliseconds` for the socket, never in hiredis, whose read timeout would + /// leave the context unusable. + func nextPushReplies(waitMilliseconds: Int32) async throws -> [RedisReply] { + try await pluginDispatchAsync(on: queue) { [self] in + #if canImport(CRedis) + let ctx = try subscribedContext() + var replies = try bufferedReplies(ctx) + guard replies.isEmpty else { return replies } + var descriptor = pollfd(fd: ctx.pointee.fd, events: Int16(POLLIN), revents: 0) + let ready = poll(&descriptor, 1, waitMilliseconds) + guard ready >= 0 || errno == EINTR else { throw transportFailure(ctx, delivered: true) } + guard ready > 0 else { return [] } + guard redisBufferRead(ctx) == REDIS_OK else { throw transportFailure(ctx, delivered: true) } + replies = try bufferedReplies(ctx) + return replies + #else + throw RedisPluginError.notConnected + #endif + } + } + + #if canImport(CRedis) + private func subscribedContext() throws -> UnsafeMutablePointer { + guard !isShuttingDown else { throw RedisPluginError.notConnected } + stateLock.lock() + defer { stateLock.unlock() } + guard let ctx = context else { throw RedisPluginError.notConnected } + return ctx + } + + private func bufferedReplies(_ ctx: UnsafeMutablePointer) throws -> [RedisReply] { + var replies: [RedisReply] = [] + while true { + var rawReply: UnsafeMutableRawPointer? + guard redisGetReplyFromReader(ctx, &rawReply) == REDIS_OK else { + throw transportFailure(ctx, delivered: true) + } + guard let reply = rawReply else { return replies } + replies.append(parseReply(reply.assumingMemoryBound(to: redisReply.self))) + freeReplyObject(reply) + } + } + #endif +} + // MARK: - Synchronous Helpers (must be called on the serial queue) #if canImport(CRedis) diff --git a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+LiveMessages.swift b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+LiveMessages.swift new file mode 100644 index 0000000000..43a610d083 --- /dev/null +++ b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+LiveMessages.swift @@ -0,0 +1,127 @@ +// +// RedisPluginDriver+LiveMessages.swift +// RedisDriverPlugin +// + +import Foundation +import os +import TableProPluginKit + +extension RedisPluginDriver: PluginLiveMessaging { + var liveMessageKind: PluginLiveMessageKind? { .pubSub } + + func subscribe( + _ request: PluginLiveSubscriptionRequest, + sessions: any PluginLiveSessionSource + ) async throws -> any PluginLiveSubscription { + guard let subscriber = try await sessions.openSession() as? RedisPluginDriver, + let connection = subscriber.pubSubConnection + else { throw RedisPluginError.notConnected } + for command in RedisLivePush.subscribeCommands(for: request) { + try await connection.sendSubscription(command) + } + return RedisLiveSubscription(subscriber: connection, sessions: sessions) + } +} + +enum RedisLivePush { + static func subscribeCommands(for request: PluginLiveSubscriptionRequest) -> [[Data]] { + var commands: [[Data]] = [] + if !request.channels.isEmpty { + commands.append([Data("SUBSCRIBE".utf8)] + request.channels.map { Data($0.utf8) }) + } + if !request.patterns.isEmpty { + commands.append([Data("PSUBSCRIBE".utf8)] + request.patterns.map { Data($0.utf8) }) + } + return commands + } + + // nil for a confirmation or anything else a subscribed connection answers that is not a message. + static func message(from reply: RedisReply, receivedAt: Date) throws -> PluginLiveMessage? { + if let error = reply.errorMessage { + throw RedisPluginError(code: 0, message: error, refusedByServer: true) + } + guard let items = reply.arrayValue, let kind = items.first?.stringValue?.lowercased() else { return nil } + switch (kind, items.count) { + case ("message", 3): + return PluginLiveMessage( + timestamp: receivedAt, + channel: items[1].displayText, + payload: items[2].displayText + ) + case ("pmessage", 4): + return PluginLiveMessage( + timestamp: receivedAt, + channel: items[2].displayText, + pattern: items[1].displayText, + payload: items[3].displayText + ) + default: + return nil + } + } +} + +final class RedisLiveSubscription: PluginLiveSubscription, @unchecked Sendable { + private static let waitMilliseconds: Int32 = 250 + private static let logger = Logger(subsystem: "com.TablePro.RedisDriver", category: "LiveMessages") + + let messages: AsyncThrowingStream + private let reader: Task + private let sessions: any PluginLiveSessionSource + private let publisherLock = NSLock() + // A subscribed connection refuses PUBLISH, so a test message goes out on a second session, opened on first use. + private var publisher: RedisPluginConnection? + + init(subscriber: RedisPluginConnection, sessions: any PluginLiveSessionSource) { + self.sessions = sessions + let (stream, continuation) = AsyncThrowingStream.makeStream() + messages = stream + reader = Task { + do { + while !Task.isCancelled { + let replies = try await subscriber.nextPushReplies(waitMilliseconds: Self.waitMilliseconds) + let arrived = Date() + for reply in replies { + if let message = try RedisLivePush.message(from: reply, receivedAt: arrived) { + continuation.yield(message) + } + } + } + continuation.finish() + } catch { + guard !Task.isCancelled else { + continuation.finish() + return + } + Self.logger.info("Subscription read ended: \(String(describing: type(of: error)), privacy: .public) \(error.localizedDescription, privacy: .private)") + continuation.finish(throwing: error) + } + } + } + + func publish(_ payload: String, to channel: String) async throws { + let connection = try await publishingConnection() + let reply = try await connection.executeCommand( + [Data("PUBLISH".utf8), Data(channel.utf8), Data(payload.utf8)], + scope: .outsideBlock + ) + if let error = reply.errorMessage { + throw RedisPluginError(code: 0, message: error, refusedByServer: true) + } + } + + func close() async { + reader.cancel() + _ = await reader.value + } + + private func publishingConnection() async throws -> RedisPluginConnection { + if let publisher = publisherLock.withLock({ publisher }) { return publisher } + guard let driver = try await sessions.openSession() as? RedisPluginDriver, + let connection = driver.pubSubConnection + else { throw RedisPluginError.notConnected } + publisherLock.withLock { publisher = connection } + return connection + } +} diff --git a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift index eb64f5e42a..4aabdce4d6 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift @@ -33,6 +33,8 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private let config: DriverConnectionConfig private var redisConnection: (any RedisCommandChannel)? + var pubSubConnection: RedisPluginConnection? { redisConnection?.pubSubConnection() } + private static let logger = Logger(subsystem: "com.TablePro.RedisDriver", category: "RedisPluginDriver") static let maxKeyBrowseScan = 10_000 diff --git a/apple/Plugins/RedisDriverPlugin/RedisSentinelChannel.swift b/apple/Plugins/RedisDriverPlugin/RedisSentinelChannel.swift index 222e7a196e..a7bea86187 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisSentinelChannel.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisSentinelChannel.swift @@ -56,6 +56,8 @@ final class RedisSentinelChannel: RedisCommandChannel, @unchecked Sendable { var isConnected: Bool { current?.isConnected ?? false } + func pubSubConnection() -> RedisPluginConnection? { current } + private var current: RedisPluginConnection? { lock.lock() defer { lock.unlock() } diff --git a/apple/Plugins/RedisDriverTests/RedisLivePushTests.swift b/apple/Plugins/RedisDriverTests/RedisLivePushTests.swift new file mode 100644 index 0000000000..9e82b31e5a --- /dev/null +++ b/apple/Plugins/RedisDriverTests/RedisLivePushTests.swift @@ -0,0 +1,45 @@ +import Foundation +import TableProPluginKit +import Testing + +struct RedisLivePushTests { + private static let now = Date(timeIntervalSince1970: 0) + + @Test("Channels and patterns go out as SUBSCRIBE and PSUBSCRIBE") + func subscribeCommands() { + let commands = RedisLivePush.subscribeCommands( + for: PluginLiveSubscriptionRequest(channels: ["a", "b"], patterns: ["news.*"]) + ) + let text = commands.map { $0.map { String(decoding: $0, as: UTF8.self) } } + #expect(text == [["SUBSCRIBE", "a", "b"], ["PSUBSCRIBE", "news.*"]]) + } + + @Test("A message and a pattern message become live messages; a confirmation does not") + func parsesPushReplies() throws { + let message = try RedisLivePush.message( + from: .array([.data(Data("message".utf8)), .data(Data("a".utf8)), .data(Data("hi".utf8))]), + receivedAt: Self.now + ) + #expect(message == PluginLiveMessage(timestamp: Self.now, channel: "a", payload: "hi")) + + let patterned = try RedisLivePush.message( + from: .array([.string("pmessage"), .string("news.*"), .string("news.eu"), .string("x")]), + receivedAt: Self.now + ) + #expect(patterned?.channel == "news.eu") + #expect(patterned?.pattern == "news.*") + + let confirmation = try RedisLivePush.message( + from: .array([.string("subscribe"), .string("a"), .integer(1)]), + receivedAt: Self.now + ) + #expect(confirmation == nil) + } + + @Test("An error reply ends the read with the server's message") + func errorReplyThrows() { + #expect(throws: RedisPluginError.self) { + _ = try RedisLivePush.message(from: .error("NOPERM no access"), receivedAt: Self.now) + } + } +} diff --git a/apple/Plugins/TableProPluginKit/PluginLiveMessaging.swift b/apple/Plugins/TableProPluginKit/PluginLiveMessaging.swift new file mode 100644 index 0000000000..137d4c369c --- /dev/null +++ b/apple/Plugins/TableProPluginKit/PluginLiveMessaging.swift @@ -0,0 +1,75 @@ +import Foundation + +// Messages that flow past a connection rather than being stored: notifications, pub/sub channels, topic tails. +public protocol PluginLiveMessaging: AnyObject, Sendable { + // Synchronous so a menu can ask without I/O; nil means this driver cannot follow live messages. + var liveMessageKind: PluginLiveMessageKind? { get } + // Every driver the plugin takes from `sessions` is closed by the host once the subscription ends, so the person's + // own session is never subscribed. Throws when a channel or topic cannot be followed. + func subscribe( + _ request: PluginLiveSubscriptionRequest, + sessions: any PluginLiveSessionSource + ) async throws -> any PluginLiveSubscription +} + +public protocol PluginLiveSessionSource: Sendable { + func openSession() async throws -> any PluginDatabaseDriver +} + +// The host never overlaps two `publish` calls; `close()` may arrive at any time and ends `messages`. +public protocol PluginLiveSubscription: AnyObject, Sendable { + // Finishes after `close()`; throws when the connection drops, and the host then subscribes again. + var messages: AsyncThrowingStream { get } + func publish(_ payload: String, to channel: String) async throws + func close() async +} + +public enum PluginLiveMessageKind: String, Sendable { + // PostgreSQL LISTEN and NOTIFY. + case notifications + // Redis SUBSCRIBE, PSUBSCRIBE and PUBLISH. + case pubSub + // Kafka topics read from their end, without a consumer group. + case topics +} + +public struct PluginLiveSubscriptionRequest: Sendable, Equatable { + public let channels: [String] + // Glob patterns, on a kind that matches channels by pattern. + public let patterns: [String] + + public init(channels: [String], patterns: [String] = []) { + self.channels = channels + self.patterns = patterns + } +} + +public struct PluginLiveMessage: Sendable, Equatable { + // When the engine stamped it, or when it arrived when the engine stamps nothing. + public let timestamp: Date + public let channel: String + // The pattern that matched, for a message a pattern subscription received. + public let pattern: String? + public let payload: String + public let key: String? + public let partition: Int? + public let offset: Int64? + + public init( + timestamp: Date, + channel: String, + pattern: String? = nil, + payload: String, + key: String? = nil, + partition: Int? = nil, + offset: Int64? = nil + ) { + self.timestamp = timestamp + self.channel = channel + self.pattern = pattern + self.payload = payload + self.key = key + self.partition = partition + self.offset = offset + } +} diff --git a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift index 7ac777740d..772ab3966d 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift @@ -751,6 +751,7 @@ extension DatabaseManager { // Before the tunnel goes: a debug target whose client vanishes keeps its locks until someone aborts it. await RoutineDebugSessions.shared.stopAll(connectionId: sessionId, tabsCloseAfter: true) defer { RoutineDebugSessions.shared.reopenAdmission(for: sessionId) } + LiveMessageSessions.shared.end(connectionId: sessionId) tabStatePersister?.persistTabState(for: sessionId) if origin == .userRequested { diff --git a/apple/TablePro/Core/LiveMessages/LiveMessageBuffer.swift b/apple/TablePro/Core/LiveMessages/LiveMessageBuffer.swift new file mode 100644 index 0000000000..9092b1678a --- /dev/null +++ b/apple/TablePro/Core/LiveMessages/LiveMessageBuffer.swift @@ -0,0 +1,84 @@ +// +// LiveMessageBuffer.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +internal struct LiveMessageEntry: Identifiable, Equatable, Sendable { + enum Kind: Equatable, Sendable { + case message(PluginLiveMessage) + // The connection dropped: messages sent until it came back were not received. + case gap(reason: String) + } + + let id: Int + let kind: Kind + + var message: PluginLiveMessage? { + if case .message(let message) = kind { return message } + return nil + } + + func matches(_ filter: String) -> Bool { + guard !filter.isEmpty else { return true } + guard let message else { return true } + return message.channel.localizedCaseInsensitiveContains(filter) + || message.payload.localizedCaseInsensitiveContains(filter) + || (message.key?.localizedCaseInsensitiveContains(filter) ?? false) + } +} + +// The newest `capacity` entries. A pause freezes what is shown, never what is kept, so resuming loses nothing up to the cap. +internal struct LiveMessageBuffer: Sendable { + static let defaultCapacity = 10_000 + + let capacity: Int + private(set) var entries: [LiveMessageEntry] = [] + private(set) var droppedCount = 0 + private var nextId = 0 + + init(capacity: Int = defaultCapacity) { + self.capacity = max(1, capacity) + } + + mutating func append(_ messages: [PluginLiveMessage]) { + for message in messages { + push(.message(message)) + } + trim() + } + + mutating func appendGap(reason: String) { + push(.gap(reason: reason)) + trim() + } + + mutating func clear() { + entries.removeAll(keepingCapacity: true) + droppedCount = 0 + } + + func visible(filter: String) -> [LiveMessageEntry] { + Self.filter(entries, by: filter) + } + + static func filter(_ entries: [LiveMessageEntry], by filter: String) -> [LiveMessageEntry] { + let trimmed = filter.trimmingCharacters(in: .whitespaces) + guard !trimmed.isEmpty else { return entries } + return entries.filter { $0.matches(trimmed) } + } + + private mutating func push(_ kind: LiveMessageEntry.Kind) { + entries.append(LiveMessageEntry(id: nextId, kind: kind)) + nextId += 1 + } + + private mutating func trim() { + let excess = entries.count - capacity + guard excess > 0 else { return } + entries.removeFirst(excess) + droppedCount += excess + } +} diff --git a/apple/TablePro/Core/LiveMessages/LiveMessageController.swift b/apple/TablePro/Core/LiveMessages/LiveMessageController.swift new file mode 100644 index 0000000000..ce80621f05 --- /dev/null +++ b/apple/TablePro/Core/LiveMessages/LiveMessageController.swift @@ -0,0 +1,215 @@ +// +// LiveMessageController.swift +// TablePro +// + +import Combine +import Foundation +import os +import TableProPluginKit + +// One live tab: a subscription on connections of its own, re-opened after a drop, and the messages it kept. +@MainActor +internal final class LiveMessageController: ObservableObject { + enum Phase: Equatable { + case connecting + case live + case reconnecting(String) + case ended(String?) + } + + typealias Subscribe = @Sendable (LiveMessageSessionSource) async throws -> any PluginLiveSubscription + + static let refreshInterval: Duration = .milliseconds(100) + static let maximumBackoff: Duration = .seconds(30) + private static let logger = Logger(subsystem: "com.TablePro", category: "LiveMessages") + + let kind: PluginLiveMessageKind + let request: PluginLiveSubscriptionRequest + + @Published private(set) var phase: Phase = .connecting + @Published private(set) var shown: [LiveMessageEntry] = [] + @Published private(set) var isPaused = false + // Kept while paused and not yet shown. + @Published private(set) var unseenCount = 0 + @Published var filter = "" { + didSet { refreshShown() } + } + + private(set) var buffer: LiveMessageBuffer + // What the table showed when it was paused; the buffer keeps growing underneath. + private var frozen: [LiveMessageEntry] = [] + private let subscribe: Subscribe + private let makeSource: @MainActor () -> LiveMessageSessionSource + private let clock: any Clock + private var runTask: Task? + private var refreshTask: Task? + private var subscription: (any PluginLiveSubscription)? + private var source: LiveMessageSessionSource? + + init( + kind: PluginLiveMessageKind, + request: PluginLiveSubscriptionRequest, + capacity: Int = LiveMessageBuffer.defaultCapacity, + clock: any Clock = ContinuousClock(), + makeSource: @escaping @MainActor () -> LiveMessageSessionSource, + subscribe: @escaping Subscribe + ) { + self.kind = kind + self.request = request + self.buffer = LiveMessageBuffer(capacity: capacity) + self.clock = clock + self.makeSource = makeSource + self.subscribe = subscribe + } + + static func title(for request: PluginLiveSubscriptionRequest) -> String { + let names = (request.channels + request.patterns).joined(separator: ", ") + return String(format: String(localized: "Live: %@"), names) + } + + var hasEnded: Bool { + if case .ended = phase { return true } + return false + } + + var canPublish: Bool { phase == .live } + + // MARK: - Lifecycle + + func start() { + guard runTask == nil, !hasEnded else { return } + runTask = Task { [weak self] in + await self?.run() + } + } + + // Ends the subscription and closes every connection it opened; the messages stay on screen. + func stop() { + guard !hasEnded else { return } + phase = .ended(nil) + runTask?.cancel() + runTask = nil + refreshTask?.cancel() + refreshTask = nil + refreshShown() + let subscription = subscription + let source = source + self.subscription = nil + self.source = nil + Task { + await subscription?.close() + await source?.closeAll() + } + } + + private func run() async { + var hasSubscribed = false + var backoff: Duration = .seconds(1) + while !Task.isCancelled { + let source = makeSource() + self.source = source + do { + let opened = try await subscribe(source) + guard !Task.isCancelled else { + await opened.close() + await source.closeAll() + return + } + subscription = opened + phase = .live + hasSubscribed = true + backoff = .seconds(1) + for try await message in opened.messages { + receive(message) + } + guard !Task.isCancelled else { return } + await dropped(subscription: opened, source: source, reason: String(localized: "The connection closed.")) + } catch { + guard !Task.isCancelled else { return } + // A first subscribe that fails is a wrong channel, topic or permission: retrying would only repeat it. + guard hasSubscribed else { + await source.closeAll() + self.source = nil + phase = .ended(error.localizedDescription) + return + } + await dropped(subscription: subscription, source: source, reason: error.localizedDescription) + } + do { + try await clock.sleep(for: backoff) + } catch { + return + } + backoff = min(backoff * 2, Self.maximumBackoff) + } + } + + private func dropped(subscription dropped: (any PluginLiveSubscription)?, source: LiveMessageSessionSource, reason: String) async { + Self.logger.info("Live subscription dropped: \(reason, privacy: .private)") + if case .reconnecting = phase {} else { + buffer.appendGap(reason: reason) + scheduleRefresh() + } + phase = .reconnecting(reason) + subscription = nil + self.source = nil + await dropped?.close() + await source.closeAll() + } + + // MARK: - Messages + + func receive(_ message: PluginLiveMessage) { + buffer.append([message]) + if isPaused { + unseenCount += 1 + } else { + scheduleRefresh() + } + } + + func pause() { + guard !isPaused else { return } + isPaused = true + refreshTask?.cancel() + refreshTask = nil + frozen = buffer.entries + } + + func resume() { + guard isPaused else { return } + isPaused = false + unseenCount = 0 + frozen = [] + refreshShown() + } + + func clear() { + buffer.clear() + frozen = [] + unseenCount = 0 + refreshShown() + } + + func publish(_ payload: String, to channel: String) async throws { + guard let subscription, phase == .live else { throw LiveMessageError.ended } + try await subscription.publish(payload, to: channel) + } + + // Coalesced, so a burst of messages redraws the table once per interval rather than once per message. + private func scheduleRefresh() { + guard refreshTask == nil, !isPaused else { return } + let clock = clock + refreshTask = Task { [weak self] in + try? await clock.sleep(for: Self.refreshInterval) + guard let self, !Task.isCancelled else { return } + self.refreshTask = nil + self.refreshShown() + } + } + + private func refreshShown() { + shown = LiveMessageBuffer.filter(isPaused ? frozen : buffer.entries, by: filter) + } +} diff --git a/apple/TablePro/Core/LiveMessages/LiveMessageExport.swift b/apple/TablePro/Core/LiveMessages/LiveMessageExport.swift new file mode 100644 index 0000000000..96e5f5bbb0 --- /dev/null +++ b/apple/TablePro/Core/LiveMessages/LiveMessageExport.swift @@ -0,0 +1,87 @@ +// +// LiveMessageExport.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// The columns a kind shows, and the same columns as text for Copy and Export. +internal enum LiveMessageExport { + enum Column: CaseIterable { + case time, channel, pattern, partition, offset, key, payload + + var title: String { + switch self { + case .time: String(localized: "Time") + case .channel: String(localized: "Channel") + case .pattern: String(localized: "Pattern") + case .partition: String(localized: "Partition") + case .offset: String(localized: "Offset") + case .key: String(localized: "Key") + case .payload: String(localized: "Payload") + } + } + } + + static func columns(for kind: PluginLiveMessageKind) -> [Column] { + switch kind { + case .pubSub: [.time, .channel, .pattern, .payload] + case .topics: [.time, .channel, .partition, .offset, .key, .payload] + case .notifications: [.time, .channel, .payload] + @unknown default: [.time, .channel, .payload] + } + } + + static func timeText(_ date: Date) -> String { + date.formatted(.iso8601.year().month().day().time(includingFractionalSeconds: true)) + } + + static func value(of column: Column, in entry: LiveMessageEntry) -> String { + guard let message = entry.message else { + guard case .gap(let reason) = entry.kind else { return "" } + switch column { + case .channel: return String(localized: "Connection lost") + case .payload: return reason + default: return "" + } + } + switch column { + case .time: return timeText(message.timestamp) + case .channel: return message.channel + case .pattern: return message.pattern ?? "" + case .partition: return message.partition.map(String.init) ?? "" + case .offset: return message.offset.map(String.init) ?? "" + case .key: return message.key ?? "" + case .payload: return message.payload + } + } + + // Tab-separated with a header row, the way a grid copies, so it pastes into a spreadsheet. + static func tsv(_ entries: [LiveMessageEntry], kind: PluginLiveMessageKind) -> String { + let columns = columns(for: kind) + let header = columns.map(\.title).joined(separator: "\t") + let rows = entries.map { entry in + columns.map { column in + value(of: column, in: entry) + .replacingOccurrences(of: "\t", with: " ") + .replacingOccurrences(of: "\n", with: " ") + } + .joined(separator: "\t") + } + return ([header] + rows).joined(separator: "\n") + } + + static func csv(_ entries: [LiveMessageEntry], kind: PluginLiveMessageKind) -> String { + let columns = columns(for: kind) + let lines = [columns.map { quoted($0.title) }.joined(separator: ",")] + entries.map { entry in + columns.map { quoted(value(of: $0, in: entry)) }.joined(separator: ",") + } + return lines.joined(separator: "\r\n") + "\r\n" + } + + private static func quoted(_ field: String) -> String { + guard field.contains(where: { $0 == "," || $0 == "\"" || $0.isNewline }) else { return field } + return "\"" + field.replacingOccurrences(of: "\"", with: "\"\"") + "\"" + } +} diff --git a/apple/TablePro/Core/LiveMessages/LiveMessageSessionSource.swift b/apple/TablePro/Core/LiveMessages/LiveMessageSessionSource.swift new file mode 100644 index 0000000000..095c7c72c9 --- /dev/null +++ b/apple/TablePro/Core/LiveMessages/LiveMessageSessionSource.swift @@ -0,0 +1,96 @@ +// +// LiveMessageSessionSource.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +internal struct LiveMessageDriverHandle: Sendable { + let driver: any PluginDatabaseDriver + let close: @Sendable () -> Void +} + +internal enum LiveMessageError: LocalizedError, Equatable { + case ended + case needsSeparateConnection + case unsupported + + var errorDescription: String? { + switch self { + case .ended: + return String(localized: "The live message view has closed.") + case .needsSeparateConnection: + return String(localized: "This connection can't open the separate session live messages need.") + case .unsupported: + return String(localized: "This connection's driver can't follow live messages.") + } + } +} + +// The connections one subscription attempt opened: never pooled, never health-pinged, no query timeout, all closed together. +internal actor LiveMessageSessionSource: PluginLiveSessionSource { + typealias Opener = @Sendable () async throws -> LiveMessageDriverHandle + + // A subscription waits on messages, not on a statement, so any timeout would end it between two messages. + static let queryTimeoutSeconds = 0 + + private let opener: Opener + private var handles: [LiveMessageDriverHandle] = [] + private var isClosed = false + + init(opener: @escaping Opener) { + self.opener = opener + } + + func openSession() async throws -> any PluginDatabaseDriver { + guard !isClosed else { throw LiveMessageError.ended } + let handle = try await opener() + // Closed while the driver was being dialed. + guard !isClosed else { + handle.close() + throw LiveMessageError.ended + } + handles.append(handle) + return handle.driver + } + + func closeAll() { + isClosed = true + let closing = handles + handles.removeAll() + for handle in closing { + handle.close() + } + } +} + +extension LiveMessageSessionSource { + // A second connection to an embedded engine opens a different, empty database, so those engines are refused. + @MainActor + static func live(scope: DatabaseScope) -> LiveMessageSessionSource { + LiveMessageSessionSource { + let driver = try await openDedicatedDriver(scope: scope) + guard let adapter = driver as? PluginDriverAdapter else { + driver.disconnect() + throw LiveMessageError.unsupported + } + return LiveMessageDriverHandle(driver: adapter.schemaPluginDriver) { adapter.disconnect() } + } + } + + @MainActor + private static func openDedicatedDriver(scope: DatabaseScope) async throws -> DatabaseDriver { + guard let session = DatabaseManager.shared.session(for: scope.connectionId) else { + throw DatabaseError.notConnected + } + guard session.connection.type.supportsConnectionPooling else { + throw LiveMessageError.needsSeparateConnection + } + return try await MetadataConnectionPool.openServerDriver( + for: scope, + purpose: .session, + queryTimeoutSeconds: queryTimeoutSeconds + ) + } +} diff --git a/apple/TablePro/Core/LiveMessages/LiveMessageSessions.swift b/apple/TablePro/Core/LiveMessages/LiveMessageSessions.swift new file mode 100644 index 0000000000..e24dde794f --- /dev/null +++ b/apple/TablePro/Core/LiveMessages/LiveMessageSessions.swift @@ -0,0 +1,57 @@ +// +// LiveMessageSessions.swift +// TablePro +// + +import Foundation + +// Every live tab's subscription, by the tab that shows it. Closing the tab is what ends it. +@MainActor +internal final class LiveMessageSessions { + static let shared = LiveMessageSessions() + + private struct Entry { + let controller: LiveMessageController + let connectionId: UUID + } + + private var entries: [UUID: Entry] = [:] + + func register(_ controller: LiveMessageController, tabId: UUID, connectionId: UUID) { + entries[tabId] = Entry(controller: controller, connectionId: connectionId) + } + + func controller(forTab tabId: UUID) -> LiveMessageController? { + entries[tabId]?.controller + } + + func end(tabId: UUID) { + entries.removeValue(forKey: tabId)?.controller.stop() + } + + // Before the connection's transport goes, so no subscription keeps redialing through a closed tunnel. + func end(connectionId: UUID) { + let tabIds = entries.filter { $0.value.connectionId == connectionId }.map(\.key) + end(tabIds: tabIds) + } + + func end(tabIds: [UUID]) { + for tabId in tabIds { + end(tabId: tabId) + } + } + + // The module went off or the app is quitting. With `closingTabs`, every window drops its live tabs too. + func endAll(closingTabs: Bool = false) { + if closingTabs { + for coordinator in MainContentCoordinator.activeCoordinators.values { + coordinator.closeLiveMessageTabs() + } + } + let ending = entries.values.map(\.controller) + entries.removeAll() + for controller in ending { + controller.stop() + } + } +} diff --git a/apple/TablePro/Core/LiveMessages/MainContentCoordinator+LiveMessages.swift b/apple/TablePro/Core/LiveMessages/MainContentCoordinator+LiveMessages.swift new file mode 100644 index 0000000000..627c150b49 --- /dev/null +++ b/apple/TablePro/Core/LiveMessages/MainContentCoordinator+LiveMessages.swift @@ -0,0 +1,62 @@ +// +// MainContentCoordinator+LiveMessages.swift +// TablePro +// + +import AppKit +import Foundation +import SwiftUI +import TableProPluginKit + +extension MainContentCoordinator { + // Answers without I/O, so a menu validation can ask. The subscription itself runs on connections of its own. + private var liveMessaging: (any PluginDatabaseDriver & PluginLiveMessaging)? { + guard AppModuleSettings.shared.isEnabled(.liveMessages), + connection.type.supportsConnectionPooling, + let adapter = services.databaseManager.driver(for: connectionId) as? PluginDriverAdapter, + let messaging = adapter.schemaPluginDriver as? any PluginDatabaseDriver & PluginLiveMessaging, + messaging.liveMessageKind != nil + else { return nil } + return messaging + } + + func canOpenLiveMessages() -> Bool { + liveMessaging != nil && services.databaseManager.session(for: connectionId)?.isConnected == true + } + + func openLiveMessages() { + guard let kind = liveMessaging?.liveMessageKind, let window = contentWindow else { return } + let suggested = kind == .topics ? tabManager.selectedTab?.tableContext.tableName : nil + let setup = RoutineDebugSetup() + Task { + let request = await RoutineDebugSheet.present(on: window, setup: setup) { finish in + LiveMessageStartSheet(kind: kind, suggestedChannel: suggested ?? "", onFinish: finish) + } + guard let request else { return } + openLiveMessageTab(kind: kind, request: request) + } + } + + // Not a user close: nothing goes to Reopen Closed Tab and the saved tab set is left alone. + func closeLiveMessageTabs() { + let ids = tabManager.tabs.filter { $0.tabType == .liveMessages }.map(\.id) + for id in ids where tabManager.tabs.contains(where: { $0.id == id }) { + tabManager.closeTab(id: id) + } + } + + private func openLiveMessageTab(kind: PluginLiveMessageKind, request: PluginLiveSubscriptionRequest) { + // Read again: the module may have gone off, or the connection dropped, while the sheet was up. + guard let messaging = liveMessaging, let scope = browseScope else { return } + let controller = LiveMessageController( + kind: kind, + request: request, + makeSource: { LiveMessageSessionSource.live(scope: scope) }, + subscribe: { source in try await messaging.subscribe(request, sessions: source) } + ) + let tabId = UUID() + LiveMessageSessions.shared.register(controller, tabId: tabId, connectionId: connectionId) + tabManager.addLiveMessagesTab(id: tabId, title: LiveMessageController.title(for: request)) + controller.start() + } +} diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/MCPTabSnapshotProvider.swift b/apple/TablePro/Core/MCP/Protocol/Tools/MCPTabSnapshotProvider.swift index 20886d5950..4415afdd91 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/MCPTabSnapshotProvider.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/MCPTabSnapshotProvider.swift @@ -171,7 +171,7 @@ private extension TabType { case .objectSource: "objectSource" case .versionHistory: "versionHistory" case .tableListing: "tableListing" - case .routineDebug: nil + case .routineDebug, .liveMessages: nil } } } diff --git a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift index fa759c6bfe..f1e617b865 100644 --- a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift +++ b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift @@ -8,8 +8,11 @@ import AppKit // Database-or-Schema titles are a closed set, never interpolated: System Settings binds an App Shortcut to the exact title. @MainActor enum DatabaseMenuBuilder { - static func build(keyboard: KeyboardSettings) -> NSMenuItem { - MenuItemFactory.menu(String(localized: "Database"), items: [ + static let identifier = NSUserInterfaceItemIdentifier("menu.database") + static let liveMessagesIdentifier = NSUserInterfaceItemIdentifier("menu.database.liveMessages") + + static func build(keyboard: KeyboardSettings, modules: Set = []) -> NSMenuItem { + let item = MenuItemFactory.menu(String(localized: "Database"), items: [ MenuItemFactory.item( String(localized: "Switch Connection…"), action: #selector(MainSplitViewController.switchConnection(_:)), @@ -161,6 +164,30 @@ enum DatabaseMenuBuilder { MenuItemFactory.separator, compareSubmenu() ]) + item.identifier = identifier + if let menu = item.submenu { + applyModules(modules, to: menu) + } + return item + } + + // Live Messages… sits with the other connection-wide tabs, and exists only while its module is on. + static func applyModules(_ modules: Set, to menu: NSMenu) { + let existing = menu.items.first { $0.identifier == liveMessagesIdentifier } + switch (modules.contains(.liveMessages), existing) { + case (true, nil): + let anchor = menu.items.firstIndex { $0.action == #selector(MainSplitViewController.showTableHealth(_:)) } + let item = MenuItemFactory.item( + String(localized: "Live Messages…"), + action: #selector(MainSplitViewController.openLiveMessages(_:)) + ) + item.identifier = liveMessagesIdentifier + menu.insertItem(item, at: anchor.map { $0 + 1 } ?? menu.items.count) + case (false, let item?): + menu.removeItem(item) + default: + return + } } /// The HIG asks that every toolbar item also be a menu-bar command. These are the Compare & diff --git a/apple/TablePro/Core/Menu/MainMenuBuilder.swift b/apple/TablePro/Core/Menu/MainMenuBuilder.swift index 2fbc402a49..20c161903c 100644 --- a/apple/TablePro/Core/Menu/MainMenuBuilder.swift +++ b/apple/TablePro/Core/Menu/MainMenuBuilder.swift @@ -32,7 +32,7 @@ enum MainMenuBuilder { bar.addItem(FileMenuBuilder.build(keyboard: keyboard)) bar.addItem(EditMenuBuilder.build(keyboard: keyboard)) bar.addItem(ViewMenuBuilder.build(keyboard: keyboard)) - bar.addItem(DatabaseMenuBuilder.build(keyboard: keyboard)) + bar.addItem(DatabaseMenuBuilder.build(keyboard: keyboard, modules: modules)) bar.addItem(QueryMenuBuilder.build(keyboard: keyboard)) if modules.contains(.routineDebugger) { bar.addItem(DebugMenuBuilder.build(keyboard: keyboard)) @@ -63,6 +63,9 @@ enum MainMenuBuilder { to bar: NSMenu, windowsMenu: NSMenu? ) { + if let database = bar.items.first(where: { $0.identifier == DatabaseMenuBuilder.identifier })?.submenu { + DatabaseMenuBuilder.applyModules(modules, to: database) + } let existing = bar.items.first { $0.identifier == DebugMenuBuilder.identifier } switch (modules.contains(.routineDebugger), existing) { case (true, nil): diff --git a/apple/TablePro/Core/Modules/AppModule.swift b/apple/TablePro/Core/Modules/AppModule.swift index e65d22848f..a4dad1f91d 100644 --- a/apple/TablePro/Core/Modules/AppModule.swift +++ b/apple/TablePro/Core/Modules/AppModule.swift @@ -2,22 +2,25 @@ import Foundation enum AppModule: String, CaseIterable, Sendable { case routineDebugger + case liveMessages var title: String { switch self { case .routineDebugger: String(localized: "Routine Debugger") + case .liveMessages: String(localized: "Live Messages") } } var summary: String { switch self { case .routineDebugger: String(localized: "Step through PL/pgSQL and PL/SQL routines with breakpoints.") + case .liveMessages: String(localized: "Watch PostgreSQL notifications, Redis channels and Kafka topics as messages arrive.") } } var defaultEnabled: Bool { switch self { - case .routineDebugger: false + case .routineDebugger, .liveMessages: false } } diff --git a/apple/TablePro/Core/Modules/AppModuleActivation.swift b/apple/TablePro/Core/Modules/AppModuleActivation.swift index 14fc7db9e5..01ab20851d 100644 --- a/apple/TablePro/Core/Modules/AppModuleActivation.swift +++ b/apple/TablePro/Core/Modules/AppModuleActivation.swift @@ -31,6 +31,8 @@ enum AppModuleActivation { switch module { case .routineDebugger: Task { await RoutineDebugSessions.shared.stopAll() } + case .liveMessages: + LiveMessageSessions.shared.endAll(closingTabs: true) } } } diff --git a/apple/TablePro/Core/Modules/AppModuleTermination.swift b/apple/TablePro/Core/Modules/AppModuleTermination.swift index 9974d64f5b..3f3bcc8a30 100644 --- a/apple/TablePro/Core/Modules/AppModuleTermination.swift +++ b/apple/TablePro/Core/Modules/AppModuleTermination.swift @@ -10,6 +10,7 @@ enum AppModuleTermination { // the same one the runs close their connections at. static func stopModuleWork() async { hasBegun = true + LiveMessageSessions.shared.endAll() guard RoutineDebugSessions.shared.hasActiveSessions else { return } await RoutineDebugSessions.shared.stopAll(timeout: stopLimit) } diff --git a/apple/TablePro/Core/Scripting/ScriptEnumerations.swift b/apple/TablePro/Core/Scripting/ScriptEnumerations.swift index 70707a1a68..18a54c2178 100644 --- a/apple/TablePro/Core/Scripting/ScriptEnumerations.swift +++ b/apple/TablePro/Core/Scripting/ScriptEnumerations.swift @@ -46,7 +46,7 @@ internal enum ScriptEnumerations { case .objectSource: fourCharCode("TPk8") case .versionHistory: fourCharCode("TPk9") case .tableListing: fourCharCode("TPkl") - case .routineDebug: nil + case .routineDebug, .liveMessages: nil } } diff --git a/apple/TablePro/Core/Services/Infrastructure/EditorTabDetachPolicy.swift b/apple/TablePro/Core/Services/Infrastructure/EditorTabDetachPolicy.swift index 850d679e24..d83582b9ad 100644 --- a/apple/TablePro/Core/Services/Infrastructure/EditorTabDetachPolicy.swift +++ b/apple/TablePro/Core/Services/Infrastructure/EditorTabDetachPolicy.swift @@ -15,8 +15,9 @@ internal enum EditorTabDetachPolicy { isBusy: Bool, isConnected: Bool ) -> Bool { - // A debug run's prompts and its close on a declined start act on the coordinator that opened it. - guard tabType != .routineDebug else { return false } + // A debug run's prompts and its close on a declined start act on the coordinator that opened it; a live + // subscription is registered with that coordinator's tab. + guard tabType != .routineDebug, tabType != .liveMessages else { return false } // Moving the last tab is Move Connection to New Window. guard tabCount > 1 else { return false } guard !hasUnsavedWork else { return false } diff --git a/apple/TablePro/Core/Services/Infrastructure/EditorTabOpener.swift b/apple/TablePro/Core/Services/Infrastructure/EditorTabOpener.swift index bb74abcead..fc2d84e0af 100644 --- a/apple/TablePro/Core/Services/Infrastructure/EditorTabOpener.swift +++ b/apple/TablePro/Core/Services/Infrastructure/EditorTabOpener.swift @@ -104,8 +104,8 @@ internal enum EditorTabOpener { databaseName: payload.databaseName ?? openingDatabaseName, schemaName: payload.schemaName ) - case .routineDebug: - // A payload cannot carry a live session; the coordinator opens debug tabs itself. + case .routineDebug, .liveMessages: + // A payload cannot carry a live session; the coordinator opens these tabs itself. return } } diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+LiveMessageActions.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+LiveMessageActions.swift new file mode 100644 index 0000000000..06ad4f4c72 --- /dev/null +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+LiveMessageActions.swift @@ -0,0 +1,18 @@ +import AppKit + +extension MainSplitViewController { + @objc func openLiveMessages(_ sender: Any?) { + guard let coordinator = liveMessagesCoordinator, coordinator.canOpenLiveMessages() else { return } + coordinator.openLiveMessages() + } + + var canOpenLiveMessages: Bool { + liveMessagesCoordinator?.canOpenLiveMessages() == true + } + + // Nil while the module is off, so no command reaches the subscription code. + private var liveMessagesCoordinator: MainContentCoordinator? { + guard AppModuleSettings.shared.isEnabled(.liveMessages) else { return nil } + return commandActions?.coordinator + } +} diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift index 40071b509b..ce7ef8a184 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift @@ -137,6 +137,7 @@ struct MenuValidationContext: Equatable { var hasSchemaFilter = false // Filled only when a Debug menu item is validated; the menu exists only while the module is on. var routineDebug = RoutineDebugMenuState() + var canOpenLiveMessages = false // The validation and the window's handler both read it, so a lit Use Selection for Find never lands on an editor with no selection. var usesGridSelectionForFind: Bool { @@ -296,6 +297,8 @@ extension MainSplitViewController: NSMenuItemValidation { return context.isConnected case #selector(showTableHealth(_:)): return context.isConnected && context.supportsTableHealth + case #selector(openLiveMessages(_:)): + return context.isConnected && !context.isAgentMode && context.canOpenLiveMessages case #selector(toggleFilterBar(_:)): return context.isConnected && context.canUseTableResultCommands @@ -729,6 +732,9 @@ extension MainSplitViewController: NSMenuItemValidation { if Self.routineDebugSelectors.contains(action) { context.routineDebug = routineDebugMenuState } + if action == #selector(openLiveMessages(_:)) { + context.canOpenLiveMessages = canOpenLiveMessages + } guard Self.agentSessionSelectors.contains(action) else { return context } context.agentSessionTarget = agentSessionTarget(for: menuItem)?.status return context diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+RecentTabs.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+RecentTabs.swift index ac82152aa8..e9068e4b76 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+RecentTabs.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+RecentTabs.swift @@ -217,6 +217,8 @@ internal extension MainSplitViewController { return "list.bullet.rectangle" case .routineDebug: return "ladybug" + case .liveMessages: + return "dot.radiowaves.left.and.right" } } } diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift index 46aa54f0fb..8550bdab41 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift @@ -1386,7 +1386,7 @@ internal final class MainSplitViewController: NSSplitViewController { case .usersRoles: return UsersRolesLayoutMetrics.tabMinimumWidth case .query, .table, .createTable, .erDiagram, .serverDashboard, .insights, .tableHealth, .objectSource, - .versionHistory, .tableListing, .routineDebug: + .versionHistory, .tableListing, .routineDebug, .liveMessages: return defaultDetailMinThickness } } diff --git a/apple/TablePro/Core/Services/Infrastructure/Toolbar/ToolbarContextResolver.swift b/apple/TablePro/Core/Services/Infrastructure/Toolbar/ToolbarContextResolver.swift index 8c30c37b00..500b7b548f 100644 --- a/apple/TablePro/Core/Services/Infrastructure/Toolbar/ToolbarContextResolver.swift +++ b/apple/TablePro/Core/Services/Infrastructure/Toolbar/ToolbarContextResolver.swift @@ -70,7 +70,7 @@ internal enum ToolbarContextResolver { case .usersRoles: String(localized: "Apply Changes") case .query, .table, .erDiagram, .serverDashboard, .insights, .tableHealth, .objectSource, .versionHistory, - .tableListing, .routineDebug, nil: + .tableListing, .routineDebug, .liveMessages, nil: String(localized: "Save Changes") } } @@ -113,8 +113,8 @@ internal enum ToolbarContextResolver { case .serverDashboard, .insights, .tableHealth, .objectSource, .versionHistory, .tableListing: /// None of these can stage a change, so the commit control could only ever be dim. return [MainWindowToolbar.saveChanges] - case .routineDebug: - // A debug run has nothing to reload or commit; its own controls take that place. + case .routineDebug, .liveMessages: + // A debug run or a live subscription has nothing to reload or commit; its own controls take that place. return [MainWindowToolbar.refresh, MainWindowToolbar.saveChanges] case .query, .table, .usersRoles, .erDiagram: return [] diff --git a/apple/TablePro/Core/Services/Infrastructure/WindowTitleResolver.swift b/apple/TablePro/Core/Services/Infrastructure/WindowTitleResolver.swift index 0b57548852..26df4f0481 100644 --- a/apple/TablePro/Core/Services/Infrastructure/WindowTitleResolver.swift +++ b/apple/TablePro/Core/Services/Infrastructure/WindowTitleResolver.swift @@ -184,6 +184,11 @@ enum WindowTitleResolver { return explicitTitle } return String(localized: "Debug") + case .liveMessages: + if let explicitTitle, !explicitTitle.isBlank { + return explicitTitle + } + return String(localized: "Live Messages") default: break } diff --git a/apple/TablePro/Models/Query/QueryTabManager.swift b/apple/TablePro/Models/Query/QueryTabManager.swift index 27fbb60c23..7f1d233ecf 100644 --- a/apple/TablePro/Models/Query/QueryTabManager.swift +++ b/apple/TablePro/Models/Query/QueryTabManager.swift @@ -586,6 +586,16 @@ final class QueryTabManager: ObservableObject { String(format: String(localized: "Debug %@"), name) } + @discardableResult + func addLiveMessagesTab(id: UUID = UUID(), title: String) -> UUID { + var newTab = QueryTab(id: id, title: title, tabType: .liveMessages) + newTab.tableContext.isEditable = false + newTab.hasUserInteraction = true + tabs.append(newTab) + selectedTabId = newTab.id + return newTab.id + } + // Qualified by schema where tables are listed per schema, or two listings in one database would share a label. static func tableListingTitle(kindTitle: String, schemaName: String?) -> String { guard let schemaName, !schemaName.isEmpty else { return kindTitle } @@ -807,7 +817,7 @@ extension QueryTabManager { case .query, .table, .createTable, .erDiagram, .objectSource, .tableListing: return true // A paused debug target holds locks, so its tab stays in sight whichever entry the sidebar shows. - case .serverDashboard, .usersRoles, .insights, .tableHealth, .versionHistory, .routineDebug: + case .serverDashboard, .usersRoles, .insights, .tableHealth, .versionHistory, .routineDebug, .liveMessages: return false } } diff --git a/apple/TablePro/Models/Query/QueryTabState.swift b/apple/TablePro/Models/Query/QueryTabState.swift index fa48f0b88c..57639beb68 100644 --- a/apple/TablePro/Models/Query/QueryTabState.swift +++ b/apple/TablePro/Models/Query/QueryTabState.swift @@ -22,12 +22,13 @@ enum TabType: Equatable, Codable, Hashable { // Read-only grid of one database's or schema's tables, rebuilt from the catalog on each load, never from saved SQL. case tableListing case routineDebug + case liveMessages } extension TabType { - // A live debug session cannot be restored, and the debugger is a module, so its tab never reaches disk or an outside client. + // A live session cannot be restored, and both tools are modules, so their tabs never reach disk or an outside client. var isTransient: Bool { - self == .routineDebug + self == .routineDebug || self == .liveMessages } } diff --git a/apple/TablePro/Models/UI/GridSelectionOwner.swift b/apple/TablePro/Models/UI/GridSelectionOwner.swift index 4364959955..7b1c633c4d 100644 --- a/apple/TablePro/Models/UI/GridSelectionOwner.swift +++ b/apple/TablePro/Models/UI/GridSelectionOwner.swift @@ -33,7 +33,7 @@ internal enum GridSelectionOwner: Equatable { // Map selects through the same model: a clicked shape selects its row. return .dataGrid case .createTable, .erDiagram, .serverDashboard, .usersRoles, .insights, .tableHealth, .objectSource, - .versionHistory, .tableListing, .routineDebug: + .versionHistory, .tableListing, .routineDebug, .liveMessages: return .none } } diff --git a/apple/TablePro/Models/UI/PendingChangeKind.swift b/apple/TablePro/Models/UI/PendingChangeKind.swift index 74c21d62be..e0bbe94d95 100644 --- a/apple/TablePro/Models/UI/PendingChangeKind.swift +++ b/apple/TablePro/Models/UI/PendingChangeKind.swift @@ -37,7 +37,7 @@ internal enum PendingChangeKind: Equatable, Hashable, Sendable { case .usersRoles: return hasPrincipalChanges ? .principals : nil case .query, .table, .erDiagram, .serverDashboard, .insights, .tableHealth, .objectSource, .versionHistory, - .tableListing, .routineDebug: + .tableListing, .routineDebug, .liveMessages: return contentKind( hasDataChanges: hasDataChanges, hasStructureChanges: hasStructureChanges, diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 956a6be91a..2fa693df9d 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -9298,6 +9298,9 @@ } } } + }, + "%d messages" : { + }, "%d more" : { "localizations" : { @@ -9390,6 +9393,9 @@ } } } + }, + "%d new" : { + }, "%d of %d" : { "localizations" : { @@ -33143,6 +33149,9 @@ }, "Can't Debug %@" : { + }, + "Can't Export Messages" : { + }, "Can't List Routines" : { @@ -36194,6 +36203,12 @@ } } } + }, + "Channel" : { + + }, + "Channels" : { + }, "Character Set" : { "extractionState" : "manual", @@ -83925,6 +83940,12 @@ } } } + }, + "Follow" : { + + }, + "Follow Topics" : { + }, "Follow-up on step %d \"%@\" (%@): %@" : { "localizations" : { @@ -85003,6 +85024,9 @@ } } } + }, + "Freeze the list; messages keep arriving underneath" : { + }, "Freeze Up to This Column" : { @@ -104210,6 +104234,12 @@ } } } + }, + "Listen" : { + + }, + "Listen for Notifications" : { + }, "Listens on all interfaces (0.0.0.0), reachable from your local network." : { "localizations" : { @@ -104360,6 +104390,18 @@ } } } + }, + "Live Messages" : { + + }, + "Live Messages Ended" : { + + }, + "Live Messages…" : { + + }, + "Live: %@" : { + }, "Load" : { "localizations" : { @@ -132399,6 +132441,9 @@ }, "Pattern" : { + }, + "Patterns" : { + }, "Pause" : { "extractionState" : "manual", @@ -132539,6 +132584,9 @@ "Paw Print" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Payload" : { + }, "Pending approval for" : { "localizations" : { @@ -137710,6 +137758,9 @@ "Processor" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Produce" : { + }, "PRODUCE needs a topic name." : { "extractionState" : "manual", @@ -143722,6 +143773,9 @@ } } } + }, + "Reconnecting…" : { + }, "Record %@ has no fields list." : { @@ -161093,6 +161147,9 @@ } } } + }, + "Send" : { + }, "Send Message" : { "localizations" : { @@ -161486,6 +161543,18 @@ } } } + }, + "Separate names with commas." : { + + }, + "Separate names with commas. A separate session runs LISTEN and closes with the tab." : { + + }, + "Separate names with commas. Patterns use glob syntax, such as news.*. A separate connection subscribes and closes with the tab." : { + + }, + "Separate names with commas. Reading starts at the end of each partition, without a consumer group, so no committed offset moves." : { + }, "Separator" : { "localizations" : { @@ -165627,6 +165696,9 @@ } } } + }, + "Show the messages that arrived while paused" : { + }, "Show Toolbar" : { "localizations" : { @@ -175049,6 +175121,12 @@ } } } + }, + "Subscribe" : { + + }, + "Subscribe to Channels" : { + }, "Subtype" : { @@ -184906,6 +184984,9 @@ } } } + }, + "The connection closed." : { + }, "The connection failed while Spanner was committing, so the changes may have been saved. Check the data before running it again." : { "extractionState" : "manual", @@ -187853,6 +187934,9 @@ } } } + }, + "The live message view has closed." : { + }, "The map cannot draw %@." : { @@ -194741,6 +194825,9 @@ } } } + }, + "This connection can't open the separate session live messages need." : { + }, "This connection can't open the separate sessions the debugger needs." : { @@ -195324,6 +195411,9 @@ }, "This connection's driver can't debug routines." : { + }, + "This connection's driver can't follow live messages." : { + }, "This connection's driver does not list its tables." : { "localizations" : { @@ -202413,6 +202503,9 @@ } } } + }, + "Topics" : { + }, "Tortoise" : { "extractionState" : "manual", @@ -212123,6 +212216,9 @@ }, "Was %@" : { + }, + "Watch PostgreSQL notifications, Redis channels and Kafka topics as messages arrive." : { + }, "Watch shared folders for connection files." : { "localizations" : { diff --git a/apple/TablePro/Views/LiveMessages/LiveMessageStartSheet.swift b/apple/TablePro/Views/LiveMessages/LiveMessageStartSheet.swift new file mode 100644 index 0000000000..b847b95c9e --- /dev/null +++ b/apple/TablePro/Views/LiveMessages/LiveMessageStartSheet.swift @@ -0,0 +1,120 @@ +// +// LiveMessageStartSheet.swift +// TablePro +// + +import SwiftUI +import TableProPluginKit + +internal struct LiveMessageStartSheet: View { + let kind: PluginLiveMessageKind + let onFinish: @MainActor (PluginLiveSubscriptionRequest?) -> Void + + @State private var channels: String + @State private var patterns = "" + + init( + kind: PluginLiveMessageKind, + suggestedChannel: String, + onFinish: @escaping @MainActor (PluginLiveSubscriptionRequest?) -> Void + ) { + self.kind = kind + self.onFinish = onFinish + _channels = State(initialValue: suggestedChannel) + } + + // Commas or new lines separate names; a name itself may hold spaces. + nonisolated static func names(in text: String) -> [String] { + var seen = Set() + return text + .split(whereSeparator: { $0 == "," || $0.isNewline }) + .map { $0.trimmingCharacters(in: .whitespaces) } + .filter { !$0.isEmpty && seen.insert($0).inserted } + } + + private var request: PluginLiveSubscriptionRequest { + PluginLiveSubscriptionRequest( + channels: Self.names(in: channels), + patterns: kind == .pubSub ? Self.names(in: patterns) : [] + ) + } + + private var canStart: Bool { + !request.channels.isEmpty || !request.patterns.isEmpty + } + + var body: some View { + VStack(alignment: .leading, spacing: 16) { + Text(LiveMessageKindText.sheetTitle(kind)) + .font(.headline) + Form { + TextField(LiveMessageKindText.channelsLabel(kind), text: $channels) + .accessibilityIdentifier("live-messages-channels") + if kind == .pubSub { + TextField(String(localized: "Patterns"), text: $patterns) + .accessibilityIdentifier("live-messages-patterns") + } + } + Text(LiveMessageKindText.sheetNote(kind)) + .font(.callout) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + HStack { + Spacer() + Button(String(localized: "Cancel"), role: .cancel) { onFinish(nil) } + .keyboardShortcut(.cancelAction) + Button(LiveMessageKindText.startTitle(kind)) { onFinish(request) } + .keyboardShortcut(.defaultAction) + .disabled(!canStart) + } + } + .padding(20) + .frame(width: 440) + } +} + +internal enum LiveMessageKindText { + static func sheetTitle(_ kind: PluginLiveMessageKind) -> String { + switch kind { + case .pubSub: String(localized: "Subscribe to Channels") + case .topics: String(localized: "Follow Topics") + case .notifications: String(localized: "Listen for Notifications") + @unknown default: String(localized: "Listen for Notifications") + } + } + + static func channelsLabel(_ kind: PluginLiveMessageKind) -> String { + kind == .topics ? String(localized: "Topics") : String(localized: "Channels") + } + + static func startTitle(_ kind: PluginLiveMessageKind) -> String { + switch kind { + case .pubSub: String(localized: "Subscribe") + case .topics: String(localized: "Follow") + case .notifications: String(localized: "Listen") + @unknown default: String(localized: "Listen") + } + } + + static func sheetNote(_ kind: PluginLiveMessageKind) -> String { + switch kind { + case .pubSub: + String(localized: "Separate names with commas. Patterns use glob syntax, such as news.*. A separate connection subscribes and closes with the tab.") + case .topics: + String(localized: "Separate names with commas. Reading starts at the end of each partition, without a consumer group, so no committed offset moves.") + case .notifications: + String(localized: "Separate names with commas. A separate session runs LISTEN and closes with the tab.") + @unknown default: + String(localized: "Separate names with commas.") + } + } + + static func publishTitle(_ kind: PluginLiveMessageKind) -> String { + switch kind { + case .pubSub: "PUBLISH" + case .topics: String(localized: "Produce") + case .notifications: "NOTIFY" + @unknown default: String(localized: "Send") + } + } +} diff --git a/apple/TablePro/Views/LiveMessages/LiveMessageTabView.swift b/apple/TablePro/Views/LiveMessages/LiveMessageTabView.swift new file mode 100644 index 0000000000..4c33e9c04c --- /dev/null +++ b/apple/TablePro/Views/LiveMessages/LiveMessageTabView.swift @@ -0,0 +1,265 @@ +// +// LiveMessageTabView.swift +// TablePro +// + +import AppKit +import SwiftUI +import TableProPluginKit +import UniformTypeIdentifiers + +// A live tab exists only with a live controller, so a tab that has none says so rather than drawing an empty table. +internal struct LiveMessageTabContent: View { + let tabId: UUID + + var body: some View { + if let controller = LiveMessageSessions.shared.controller(forTab: tabId) { + LiveMessageTabView(controller: controller) + } else { + UnavailableStateView(String(localized: "Live Messages Ended"), systemImage: "dot.radiowaves.left.and.right") + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + } +} + +internal struct LiveMessageTabView: View { + @ObservedObject var controller: LiveMessageController + @State private var selection = Set() + @State private var publishChannel = "" + @State private var publishPayload = "" + @State private var isPublishing = false + @State private var publishError: String? + + // Newest first, so an arriving message is in sight without scrolling. + private var rows: [LiveMessageEntry] { + controller.shown.reversed() + } + + var body: some View { + VStack(spacing: 0) { + controlBar + .paneBarBackground() + Divider() + table + .frame(maxWidth: .infinity, maxHeight: .infinity) + Divider() + publishBar + .paneBarBackground() + } + .onAppear { + if publishChannel.isEmpty { + publishChannel = controller.request.channels.first ?? "" + } + } + } + + // MARK: - Control bar + + private var controlBar: some View { + HStack(spacing: 10) { + status + Spacer(minLength: 8) + if controller.isPaused, controller.unseenCount > 0 { + Text(String(format: String(localized: "%d new"), controller.unseenCount)) + .font(.callout) + .foregroundStyle(.secondary) + .monospacedDigit() + } + Text(String(format: String(localized: "%d messages"), controller.shown.count)) + .font(.callout) + .foregroundStyle(.secondary) + .monospacedDigit() + NativeSearchField( + text: $controller.filter, + placeholder: String(localized: "Filter"), + controlSize: .small, + accessibilityIdentifier: "live-messages-filter" + ) + .frame(width: 180) + Button { + controller.isPaused ? controller.resume() : controller.pause() + } label: { + Label( + controller.isPaused ? String(localized: "Resume") : String(localized: "Pause"), + systemImage: controller.isPaused ? "play.fill" : "pause.fill" + ) + } + .help(controller.isPaused + ? String(localized: "Show the messages that arrived while paused") + : String(localized: "Freeze the list; messages keep arriving underneath")) + Button(String(localized: "Clear")) { + selection.removeAll() + controller.clear() + } + Button(String(localized: "Copy")) { copy() } + .disabled(controller.shown.isEmpty) + Button(String(localized: "Export…")) { export() } + .disabled(controller.shown.isEmpty) + } + .controlSize(.small) + .padding(.horizontal, 12) + .padding(.vertical, 6) + } + + @ViewBuilder + private var status: some View { + switch controller.phase { + case .connecting: + Label(String(localized: "Connecting…"), systemImage: "circle.dotted") + .foregroundStyle(.secondary) + case .live: + Label(LiveMessageController.title(for: controller.request), systemImage: "dot.radiowaves.left.and.right") + .lineLimit(1) + .truncationMode(.tail) + case .reconnecting(let reason): + Label(String(localized: "Reconnecting…"), systemImage: "arrow.triangle.2.circlepath") + .foregroundStyle(.secondary) + .help(reason) + case .ended(let error): + if let error { + Label(error, systemImage: "exclamationmark.triangle") + .foregroundStyle(.secondary) + .lineLimit(2) + .help(error) + } else { + Label(String(localized: "Stopped"), systemImage: "stop.circle") + .foregroundStyle(.secondary) + } + } + } + + // MARK: - Table + + @ViewBuilder + private var table: some View { + switch controller.kind { + case .pubSub: + Table(rows, selection: $selection) { + timeColumn + textColumn(.channel) + textColumn(.pattern) + textColumn(.payload) + } + .onCopyCommand { copyProviders() } + case .topics: + Table(rows, selection: $selection) { + timeColumn + textColumn(.channel) + textColumn(.partition) + textColumn(.offset) + textColumn(.key) + textColumn(.payload) + } + .onCopyCommand { copyProviders() } + case .notifications: + channelTable + @unknown default: + channelTable + } + } + + private var channelTable: some View { + Table(rows, selection: $selection) { + timeColumn + textColumn(.channel) + textColumn(.payload) + } + .onCopyCommand { copyProviders() } + } + + private var timeColumn: TableColumn { + TableColumn(LiveMessageExport.Column.time.title) { entry in + Text(entry.message.map { $0.timestamp.formatted(.dateTime.hour().minute().second().secondFraction(.fractional(3))) } ?? "") + .monospacedDigit() + } + } + + private func textColumn(_ column: LiveMessageExport.Column) -> TableColumn { + TableColumn(column.title) { entry in + let text = Text(LiveMessageExport.value(of: column, in: entry)) + if entry.message == nil { + return text.foregroundColor(.secondary).italic() + } + return text + } + } + + // MARK: - Publish + + private var publishBar: some View { + HStack(spacing: 8) { + Text(LiveMessageKindText.publishTitle(controller.kind)) + .font(.callout.weight(.medium)) + .foregroundStyle(.secondary) + TextField(LiveMessageKindText.channelsLabel(controller.kind), text: $publishChannel) + .frame(width: 160) + .accessibilityIdentifier("live-messages-publish-channel") + TextField(String(localized: "Payload"), text: $publishPayload) + .onSubmit { send() } + .accessibilityIdentifier("live-messages-publish-payload") + Button(String(localized: "Send")) { send() } + .disabled(!controller.canPublish || isPublishing + || publishChannel.trimmingCharacters(in: .whitespaces).isEmpty) + if let publishError { + Image(systemName: "exclamationmark.triangle") + .foregroundStyle(.secondary) + .help(publishError) + } + } + .controlSize(.small) + .padding(.horizontal, 12) + .padding(.vertical, 6) + } + + private func send() { + let channel = publishChannel.trimmingCharacters(in: .whitespaces) + guard controller.canPublish, !isPublishing, !channel.isEmpty else { return } + let payload = publishPayload + isPublishing = true + publishError = nil + Task { + do { + try await controller.publish(payload, to: channel) + publishPayload = "" + } catch { + publishError = error.localizedDescription + } + isPublishing = false + } + } + + // MARK: - Copy and export + + private var selectedOrAll: [LiveMessageEntry] { + let chosen = controller.shown.filter { selection.contains($0.id) } + return chosen.isEmpty ? controller.shown : chosen + } + + private func copy() { + ClipboardService.shared.writeText(LiveMessageExport.tsv(selectedOrAll, kind: controller.kind)) + } + + private func copyProviders() -> [NSItemProvider] { + [NSItemProvider(object: LiveMessageExport.tsv(selectedOrAll, kind: controller.kind) as NSString)] + } + + private func export() { + let text = LiveMessageExport.csv(controller.shown, kind: controller.kind) + let panel = NSSavePanel() + panel.allowedContentTypes = [.commaSeparatedText] + panel.nameFieldStringValue = "live-messages.csv" + guard let window = NSApp.keyWindow else { return } + panel.beginSheetModal(for: window) { response in + guard response == .OK, let url = panel.url else { return } + do { + try Data(text.utf8).write(to: url, options: .atomic) + } catch { + AlertHelper.showErrorSheet( + title: String(localized: "Can't Export Messages"), + message: error.localizedDescription, + window: window + ) + } + } + } +} diff --git a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift index 458ab4de9a..a3de00ddeb 100644 --- a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -217,6 +217,9 @@ struct MainEditorContentView: View { case .routineDebug: RoutineDebugTabContent(tabId: tab.id, databaseType: connection.type) .id(tab.id) + case .liveMessages: + LiveMessageTabContent(tabId: tab.id) + .id(tab.id) } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift index e95d3fb3c2..b08726e990 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift @@ -55,7 +55,7 @@ extension MainContentCoordinator { case .usersRoles: usersRolesActions?.refresh() return - case .query, .table, .createTable, .routineDebug: + case .query, .table, .createTable, .routineDebug, .liveMessages: break } if tab.display.resultsViewMode == .structure { diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift index b2bb6f89cc..3a7f159b30 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift @@ -15,6 +15,7 @@ extension MainContentCoordinator { saveLastFilters(of: tab) if tab.tabType.isTransient { RoutineDebugSessions.shared.endSession(forTab: id, window: contentWindow) + LiveMessageSessions.shared.end(tabId: id) } else { RecentlyClosedTabStore.shared.push(tab: tab, connection: connection) } diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index 941e288852..d2699ff5ef 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -881,6 +881,7 @@ final class MainContentCoordinator: ObservableObject { let debugTabIds = tabManager.tabs.filter { $0.tabType.isTransient }.map(\.id) if !debugTabIds.isEmpty { RoutineDebugSessions.shared.endSessions(forTabs: debugTabIds, window: contentWindow) + LiveMessageSessions.shared.end(tabIds: debugTabIds) } tabManager.tabs.removeAll() tabManager.selectedTabId = nil diff --git a/apple/TableProTests/Core/LiveMessages/LiveMessageBufferTests.swift b/apple/TableProTests/Core/LiveMessages/LiveMessageBufferTests.swift new file mode 100644 index 0000000000..c86488a1da --- /dev/null +++ b/apple/TableProTests/Core/LiveMessages/LiveMessageBufferTests.swift @@ -0,0 +1,58 @@ +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +struct LiveMessageBufferTests { + private static func message(_ payload: String, channel: String = "orders") -> PluginLiveMessage { + PluginLiveMessage(timestamp: Date(timeIntervalSince1970: 0), channel: channel, payload: payload) + } + + @Test("Keeps only the newest messages up to the cap and counts the ones it dropped") + func keepsNewestUpToCap() { + var buffer = LiveMessageBuffer(capacity: 3) + buffer.append((1...5).map { Self.message("m\($0)") }) + + #expect(buffer.entries.compactMap { $0.message?.payload } == ["m3", "m4", "m5"]) + #expect(buffer.droppedCount == 2) + } + + @Test("Entry ids keep growing after a trim and a clear, so a table never reuses one") + func idsNeverRepeat() { + var buffer = LiveMessageBuffer(capacity: 2) + buffer.append([Self.message("a"), Self.message("b"), Self.message("c")]) + let before = buffer.entries.map(\.id) + buffer.clear() + buffer.append([Self.message("d")]) + + #expect(buffer.entries.isEmpty == false) + #expect(!before.contains(buffer.entries[0].id)) + } + + @Test("A gap marker is kept in order and always passes the filter") + func gapKeptAndUnfiltered() { + var buffer = LiveMessageBuffer() + buffer.append([Self.message("before")]) + buffer.appendGap(reason: "server closed the connection") + buffer.append([Self.message("after", channel: "audit")]) + + let visible = buffer.visible(filter: "audit") + #expect(visible.count == 2) + #expect(visible.first?.message == nil) + #expect(visible.last?.message?.payload == "after") + } + + @Test("The filter matches channel, payload and key, ignoring case") + func filterMatchesFields() { + var buffer = LiveMessageBuffer() + buffer.append([ + Self.message("hello", channel: "orders"), + PluginLiveMessage(timestamp: Date(), channel: "t", payload: "x", key: "Customer-7"), + Self.message("other", channel: "misc"), + ]) + + #expect(buffer.visible(filter: "ORDERS").count == 1) + #expect(buffer.visible(filter: "customer").count == 1) + #expect(buffer.visible(filter: " ").count == 3) + } +} diff --git a/apple/TableProTests/Core/LiveMessages/LiveMessageControllerTests.swift b/apple/TableProTests/Core/LiveMessages/LiveMessageControllerTests.swift new file mode 100644 index 0000000000..5d26de889a --- /dev/null +++ b/apple/TableProTests/Core/LiveMessages/LiveMessageControllerTests.swift @@ -0,0 +1,174 @@ +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +private final class FakeSubscription: PluginLiveSubscription, @unchecked Sendable { + let messages: AsyncThrowingStream + let continuation: AsyncThrowingStream.Continuation + private let lock = NSLock() + private var _published: [String] = [] + private var _isClosed = false + + init() { + (messages, continuation) = AsyncThrowingStream.makeStream() + } + + var published: [String] { lock.withLock { _published } } + var isClosed: Bool { lock.withLock { _isClosed } } + + func publish(_ payload: String, to channel: String) async throws { + lock.withLock { _published.append("\(channel):\(payload)") } + } + + func close() async { + lock.withLock { _isClosed = true } + continuation.finish() + } +} + +private struct Dropped: Error, LocalizedError { + var errorDescription: String? { "server closed the connection" } +} + +@MainActor +private final class Harness { + var subscriptions: [FakeSubscription] = [] + var failFirst = false + var opened = 0 + var closedSources = 0 + let clock = ManualClock() + private(set) var controller: LiveMessageController! + + init(capacity: Int = LiveMessageBuffer.defaultCapacity) { + controller = LiveMessageController( + kind: .notifications, + request: PluginLiveSubscriptionRequest(channels: ["orders"]), + capacity: capacity, + clock: clock, + makeSource: { LiveMessageSessionSource { throw LiveMessageError.unsupported } }, + subscribe: { [weak self] _ in try await self!.next() } + ) + } + + func next() async throws -> any PluginLiveSubscription { + opened += 1 + if failFirst { throw Dropped() } + let subscription = FakeSubscription() + subscriptions.append(subscription) + return subscription + } + + static func message(_ payload: String) -> PluginLiveMessage { + PluginLiveMessage(timestamp: Date(), channel: "orders", payload: payload) + } +} + +@MainActor +struct LiveMessageControllerTests { + @Test("Messages that arrive while paused are kept and shown on resume") + func pauseLosesNothing() async throws { + let harness = Harness() + let controller = harness.controller! + controller.start() + try await waitUntil { controller.phase == .live } + + controller.pause() + harness.subscriptions[0].continuation.yield(Harness.message("one")) + harness.subscriptions[0].continuation.yield(Harness.message("two")) + try await waitUntil { controller.unseenCount == 2 } + #expect(controller.shown.isEmpty) + + controller.resume() + #expect(controller.shown.compactMap { $0.message?.payload } == ["one", "two"]) + #expect(controller.unseenCount == 0) + controller.stop() + } + + @Test("A dropped connection leaves one gap row and subscribes again after the backoff") + func dropReconnects() async throws { + let harness = Harness() + let controller = harness.controller! + controller.start() + try await waitUntil { controller.phase == .live } + + harness.subscriptions[0].continuation.finish(throwing: Dropped()) + try await waitUntil { controller.phase == .reconnecting("server closed the connection") } + #expect(harness.subscriptions[0].isClosed) + #expect(controller.buffer.entries.count == 1) + #expect(controller.buffer.entries.first?.message == nil) + + // The table refresh and the backoff both sleep on the clock. + try await waitUntil { harness.clock.pendingSleeperCount == 2 } + harness.clock.advance(by: .seconds(1)) + try await waitUntil { harness.opened == 2 && controller.phase == .live } + controller.stop() + } + + @Test("A first subscribe that fails ends the tab with the error instead of retrying") + func firstFailureEnds() async throws { + let harness = Harness() + harness.failFirst = true + let controller = harness.controller! + controller.start() + + try await waitUntil { controller.hasEnded } + #expect(controller.phase == .ended("server closed the connection")) + #expect(harness.opened == 1) + } + + @Test("Stopping closes the subscription and refuses a publish") + func stopCloses() async throws { + let harness = Harness() + let controller = harness.controller! + controller.start() + try await waitUntil { controller.phase == .live } + + try await controller.publish("hi", to: "orders") + #expect(harness.subscriptions[0].published == ["orders:hi"]) + + controller.stop() + try await waitUntil { harness.subscriptions[0].isClosed } + await #expect(throws: LiveMessageError.ended) { + try await controller.publish("late", to: "orders") + } + } + + @Test("Closing a live tab's tab id stops its controller") + func sessionsEndStopsController() async throws { + let harness = Harness() + let controller = harness.controller! + let sessions = LiveMessageSessions() + let tabId = UUID() + sessions.register(controller, tabId: tabId, connectionId: UUID()) + controller.start() + try await waitUntil { controller.phase == .live } + + sessions.end(tabId: tabId) + #expect(controller.hasEnded) + #expect(sessions.controller(forTab: tabId) == nil) + try await waitUntil { harness.subscriptions[0].isClosed } + } +} + +@MainActor +struct LiveMessageSessionsTests { + @Test("Disconnecting a connection stops only that connection's live tabs") + func endByConnection() async throws { + let first = Harness() + let second = Harness() + let sessions = LiveMessageSessions() + let connection = UUID() + sessions.register(first.controller, tabId: UUID(), connectionId: connection) + sessions.register(second.controller, tabId: UUID(), connectionId: UUID()) + first.controller.start() + second.controller.start() + try await waitUntil { first.controller.phase == .live && second.controller.phase == .live } + + sessions.end(connectionId: connection) + #expect(first.controller.hasEnded) + #expect(!second.controller.hasEnded) + second.controller.stop() + } +} diff --git a/apple/TableProTests/Core/LiveMessages/LiveMessageExportTests.swift b/apple/TableProTests/Core/LiveMessages/LiveMessageExportTests.swift new file mode 100644 index 0000000000..24c3e60418 --- /dev/null +++ b/apple/TableProTests/Core/LiveMessages/LiveMessageExportTests.swift @@ -0,0 +1,41 @@ +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +struct LiveMessageExportTests { + private static let kafka = LiveMessageEntry(id: 0, kind: .message(PluginLiveMessage( + timestamp: Date(timeIntervalSince1970: 0), + channel: "orders", + payload: "a,\"b\"", + key: "k1", + partition: 2, + offset: 41 + ))) + + @Test("Each kind shows its own columns") + func columnsPerKind() { + #expect(LiveMessageExport.columns(for: .notifications) == [.time, .channel, .payload]) + #expect(LiveMessageExport.columns(for: .pubSub).contains(.pattern)) + #expect(LiveMessageExport.columns(for: .topics) == [.time, .channel, .partition, .offset, .key, .payload]) + } + + @Test("CSV quotes a field holding a comma or a quote") + func csvQuotes() { + let csv = LiveMessageExport.csv([Self.kafka], kind: .topics) + let row = csv.components(separatedBy: "\r\n")[1] + #expect(row.hasSuffix(",orders,2,41,k1,\"a,\"\"b\"\"\"")) + } + + @Test("A gap row copies as Connection lost with its reason") + func gapCopies() { + let gap = LiveMessageEntry(id: 1, kind: .gap(reason: "timeout")) + let tsv = LiveMessageExport.tsv([gap], kind: .notifications) + #expect(tsv.components(separatedBy: "\n")[1] == "\t\(String(localized: "Connection lost"))\ttimeout") + } + + @Test("Names split on commas and new lines, trimmed and without repeats") + func startSheetNames() { + #expect(LiveMessageStartSheet.names(in: " orders, audit log\norders,, ") == ["orders", "audit log"]) + } +} diff --git a/apple/TableProTests/Core/LiveMessages/LiveMessagesModuleResidueTests.swift b/apple/TableProTests/Core/LiveMessages/LiveMessagesModuleResidueTests.swift new file mode 100644 index 0000000000..68cd0d6a71 --- /dev/null +++ b/apple/TableProTests/Core/LiveMessages/LiveMessagesModuleResidueTests.swift @@ -0,0 +1,40 @@ +import AppKit +import Foundation +@testable import TablePro +import Testing + +// An off module leaves nothing behind (apple/AGENTS.md, Scope): no Live Messages… command anywhere in the menu bar. +@MainActor +struct LiveMessagesModuleResidueTests { + private func liveItems(in menu: NSMenu) -> [NSMenuItem] { + menu.items.flatMap { item -> [NSMenuItem] in + let own = item.action == #selector(MainSplitViewController.openLiveMessages(_:)) + || item.identifier == DatabaseMenuBuilder.liveMessagesIdentifier ? [item] : [] + return own + (item.submenu.map(liveItems(in:)) ?? []) + } + } + + @Test("Off: the menu bar has no Live Messages… command") + func offLeavesNoMenuItem() { + let bar = MainMenuBuilder.build(keyboard: KeyboardSettings(), modules: []).bar + #expect(liveItems(in: bar).isEmpty) + } + + @Test("On: Live Messages… sits in the Database menu, and turning the module off removes it") + func togglesWithModule() throws { + let built = MainMenuBuilder.build(keyboard: KeyboardSettings(), modules: [.liveMessages]) + let database = try #require(built.bar.items.first { $0.identifier == DatabaseMenuBuilder.identifier }?.submenu) + #expect(liveItems(in: database).count == 1) + + MainMenuBuilder.applyModules([], keyboard: KeyboardSettings(), to: built.bar, windowsMenu: built.windowsMenu) + #expect(liveItems(in: built.bar).isEmpty) + + MainMenuBuilder.applyModules([.liveMessages], keyboard: KeyboardSettings(), to: built.bar, windowsMenu: built.windowsMenu) + #expect(liveItems(in: built.bar).count == 1) + } + + @Test("The module is off by default") + func offByDefault() { + #expect(!AppModule.liveMessages.defaultEnabled) + } +} diff --git a/apple/TableProTests/Core/Services/EditorTabConnectionMovePolicyTests.swift b/apple/TableProTests/Core/Services/EditorTabConnectionMovePolicyTests.swift index 401701638c..5346e27113 100644 --- a/apple/TableProTests/Core/Services/EditorTabConnectionMovePolicyTests.swift +++ b/apple/TableProTests/Core/Services/EditorTabConnectionMovePolicyTests.swift @@ -17,7 +17,7 @@ struct EditorTabConnectionMovePolicyTests { @Test("Every other tab type names an object of its own connection and stays") func nonQueryTabsStay() { for tabType in [TabType.table, .createTable, .erDiagram, .serverDashboard, .usersRoles, .insights, - .tableHealth, .objectSource, .versionHistory, .tableListing, .routineDebug] { + .tableHealth, .objectSource, .versionHistory, .tableListing, .routineDebug, .liveMessages] { #expect(!EditorTabConnectionMovePolicy.canMove(tabType: tabType, isBusy: false, hasPendingGridEdits: false)) } } diff --git a/apple/changelog.d/3981.added.md b/apple/changelog.d/3981.added.md new file mode 100644 index 0000000000..fa0761e735 --- /dev/null +++ b/apple/changelog.d/3981.added.md @@ -0,0 +1 @@ +Live Messages module: watch PostgreSQL notifications, Redis channels and Kafka topics as messages arrive.