From 0f26eff7d099b105f864f231f5e0efb646536191 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 08:05:56 +0700 Subject: [PATCH 1/3] fix(ios): name the SQL Server database in Tables tab statements and run Read-Only in the read-only transaction --- .../TableProDatabase/DatabaseDriver.swift | 30 +++ .../TableProModels/StreamingResult.swift | 7 +- .../MySQLLexicalFeatures.swift | 2 +- .../MySQLReadOnlyTransaction+Statement.swift | 71 ++++++ .../MySQLReadOnlyTransaction.swift | 84 +------ .../MySQLServerVersion.swift | 13 + .../MySQLSessionFootprint.swift | 2 +- .../MySQLTemporaryTableNames.swift | 6 +- .../MySQLServerVersionTests.swift | 16 ++ .../LibPQReadOnlyTransaction+Statement.swift | 68 ++++++ .../LibPQReadOnlyTransaction.swift | 85 ++----- .../Coordinators/ConnectionCoordinator.swift | 42 +--- .../TableProMobile/Drivers/MSSQLDriver.swift | 55 ++++- .../TableProMobile/Drivers/MySQLDriver.swift | 155 ++++++++++-- .../Drivers/PostgreSQLDriver.swift | 138 +++++++++-- .../Drivers/ReadOnlyModeRefusal.swift | 18 ++ .../Helpers/ReadOnlyFunctionGuard.swift | 23 +- .../TableProMobile/Helpers/RowInserter.swift | 9 +- .../Helpers/RowWriteBatch.swift | 15 +- .../TableProMobile/Helpers/SQLBuilder.swift | 67 ++++-- .../TableProMobile/Localizable.xcstrings | 42 ++++ .../Models/ForeignKeyReference.swift | 31 ++- .../ViewModels/DataBrowserViewModel.swift | 71 ++++-- .../ViewModels/ForeignKeyPreviewModel.swift | 20 +- .../ViewModels/QueryEditorViewModel.swift | 10 +- .../ViewModels/RowDetailViewModel.swift | 4 + .../Views/DataBrowserView.swift | 37 +-- .../TableProMobile/Views/InsertRowView.swift | 12 +- .../Views/QueryEditorView.swift | 18 +- .../TableProMobile/Views/RowDetailView.swift | 14 +- .../TableProMobile/Views/StructureView.swift | 9 +- .../TableProMobile/Views/TableListView.swift | 11 +- .../ConnectionCoordinatorCatalogTests.swift | 67 ++++-- .../DataBrowserViewModelTests.swift | 82 ++++++- .../Drivers/FakeMySQLServer.swift | 97 +++++++- .../Drivers/FakePostgreSQLServer.swift | 152 ++++++++++-- .../Drivers/MSSQLCatalogDatabaseTests.swift | 47 ++++ .../Drivers/MySQLDriverTransactionTests.swift | 14 ++ .../Drivers/MySQLReadOnlyRunTests.swift | 224 ++++++++++++++++++ .../Drivers/PostgreSQLReadOnlyRunTests.swift | 179 ++++++++++++++ .../Drivers/PostgreSQLStreamTests.swift | 31 +++ .../ForeignKeyPreviewModelTests.swift | 19 ++ .../ForeignKeyReferenceTests.swift | 16 ++ .../Helpers/ReadOnlyFunctionGuardTests.swift | 100 +++++++- .../Helpers/RowInsertPlannerTests.swift | 13 +- .../Helpers/RowWriteRoutingTests.swift | 39 +++ .../SQLBuilderSchemaQualificationTests.swift | 92 ++++++- .../Mocks/MockDatabaseDriver.swift | 15 ++ .../QueryEditorViewModelTests.swift | 48 ++++ apple/TableProMobile/project.yml | 7 + 50 files changed, 2006 insertions(+), 421 deletions(-) create mode 100644 apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction+Statement.swift create mode 100644 apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction+Statement.swift create mode 100644 apple/TableProMobile/TableProMobile/Drivers/ReadOnlyModeRefusal.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/MSSQLCatalogDatabaseTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/MySQLReadOnlyRunTests.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLReadOnlyRunTests.swift diff --git a/apple/Packages/TableProCore/Sources/TableProDatabase/DatabaseDriver.swift b/apple/Packages/TableProCore/Sources/TableProDatabase/DatabaseDriver.swift index f1d92e1622..5fe26dfd00 100644 --- a/apple/Packages/TableProCore/Sources/TableProDatabase/DatabaseDriver.swift +++ b/apple/Packages/TableProCore/Sources/TableProDatabase/DatabaseDriver.swift @@ -27,6 +27,17 @@ public protocol DatabaseDriver: AnyObject, Sendable { func fetchForeignKeys(table: String, schema: String?) async throws -> [ForeignKeyInfo] func fetchDatabases() async throws -> [String] + // The table in `database`, for an engine that names the database beside the schema (SQL Server's + // `[db].[schema].[table]`), so the read never follows a `USE` the session ran. Any other engine reads as above. + func fetchColumns(table: String, schema: String?, database: String?) async throws -> [ColumnInfo] + func fetchIndexes(table: String, schema: String?, database: String?) async throws -> [IndexInfo] + func fetchForeignKeys(table: String, schema: String?, database: String?) async throws -> [ForeignKeyInfo] + + // Runs a statement sent with `StreamOptions.enforcesReadOnly` in the engine's read-only mode, or refuses it. + var enforcesReadOnlyMode: Bool { get } + // Whether the mode can run now: its own transaction would end a transaction or a table lock the session holds. + func entersReadOnlyMode() async -> Bool + func switchDatabase(to name: String) async throws var supportsSchemas: Bool { get } func switchSchema(to name: String) async throws @@ -58,6 +69,25 @@ public extension DatabaseDriver { func sessionLexicalFacts() async -> SQLSessionLexicalFacts? { nil } + func fetchColumns(table: String, schema: String?, database: String?) async throws -> [ColumnInfo] { + try await fetchColumns(table: table, schema: schema) + } + + func fetchIndexes(table: String, schema: String?, database: String?) async throws -> [IndexInfo] { + try await fetchIndexes(table: table, schema: schema) + } + + func fetchForeignKeys(table: String, schema: String?, database: String?) async throws -> [ForeignKeyInfo] { + try await fetchForeignKeys(table: table, schema: schema) + } + + var enforcesReadOnlyMode: Bool { false } + + func entersReadOnlyMode() async -> Bool { + guard enforcesReadOnlyMode else { return false } + return await sessionTransactionState() == .idle + } + func beginTransaction(mode: PluginTransactionAccessMode) async throws { try await beginTransaction() } diff --git a/apple/Packages/TableProCore/Sources/TableProModels/StreamingResult.swift b/apple/Packages/TableProCore/Sources/TableProModels/StreamingResult.swift index 46043693fe..1eab5e3482 100644 --- a/apple/Packages/TableProCore/Sources/TableProModels/StreamingResult.swift +++ b/apple/Packages/TableProCore/Sources/TableProModels/StreamingResult.swift @@ -23,19 +23,24 @@ public struct StreamOptions: Sendable { public let lazyContext: LazyContext? // Long text with no ref stays whole while this admits it; nil cuts every long value. public var textBudget: TextBudget? + // Safe Mode is Read-Only. A driver whose `enforcesReadOnlyMode` is true runs the statement in the engine's + // read-only mode or refuses it; any other driver runs it as it is. + public var enforcesReadOnly: Bool public init( textTruncationBytes: Int = Cell.previewByteLimit, inlineBinary: Bool = false, maxRows: Int = 100_000, lazyContext: LazyContext? = nil, - textBudget: TextBudget? = nil + textBudget: TextBudget? = nil, + enforcesReadOnly: Bool = false ) { self.textTruncationBytes = textTruncationBytes self.inlineBinary = inlineBinary self.maxRows = maxRows self.lazyContext = lazyContext self.textBudget = textBudget + self.enforcesReadOnly = enforcesReadOnly } public static let `default` = StreamOptions() diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLLexicalFeatures.swift b/apple/Plugins/MySQLDriverPlugin/MySQLLexicalFeatures.swift index 9fc43e6dae..9075378c0b 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLLexicalFeatures.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLLexicalFeatures.swift @@ -10,7 +10,7 @@ import TableProPluginKit /// /// The app's curated table is the authority for its gates; these are the same facts, held here because a plugin /// cannot link the app's package, and `SQLLexicalFeatureMappingTests` holds the two equal. -enum MySQLLexicalFeatures { +nonisolated enum MySQLLexicalFeatures { /// MySQL 8.4.11 and MariaDB 11.8.9, measured. static let mySQL: SQLLexicalFeatures = [ .backslashEscapesInSingleQuotes, .backslashEscapesInDoubleQuotes, .backtickQuotes, .hashLineComments, diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction+Statement.swift b/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction+Statement.swift new file mode 100644 index 0000000000..7e08ce8487 --- /dev/null +++ b/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction+Statement.swift @@ -0,0 +1,71 @@ +// +// MySQLReadOnlyTransaction+Statement.swift +// MySQLDriverPlugin +// + +import Foundation +import TableProPluginKit + +extension MySQLReadOnlyTransaction { + // The session state is read on the statement's own queue, so nothing queued behind it can open a transaction first. + struct Scope: Sendable { + let reason: Reason + let sessionState: @Sendable (Bool) -> PluginSessionTransactionState + } + + static var sessionNotIdle: MariaDBPluginError { + MariaDBPluginError(code: 0, message: notIdleMessage(.externalClients), sqlState: nil) + } + + static var safeModeSessionNotIdle: MariaDBPluginError { + MariaDBPluginError(code: 0, message: notIdleMessage(.safeMode), sqlState: nil) + } + + static func reason(for context: PluginStatementContext, flavor: MySQLServerFlavor) -> Reason? { + guard flavor.enforcesReadOnlyTransactions else { return nil } + if context.readOnly, context.origin == .external { return .externalClients } + if context.enforcesReadOnly { return .safeMode } + return nil + } + + static func applies(to context: PluginStatementContext, flavor: MySQLServerFlavor) -> Bool { + reason(for: context, flavor: flavor) != nil + } + + // `START TRANSACTION` would commit the user's open transaction and release their `LOCK TABLES`. + static func run( + state: PluginSessionTransactionState, + reason: Reason, + send: (String) throws -> Void, + statement: () throws -> Result + ) throws -> Result { + guard state == .idle else { throw notIdle(reason) } + do { + try send(begin) + } catch { + try? send(end) + throw error + } + let result: Result + do { + result = try statement() + } catch { + try? send(end) + throw refusal(from: error, reason: reason) + } + try send(end) + return result + } + + static func notIdle(_ reason: Reason) -> MariaDBPluginError { + switch reason { + case .externalClients: sessionNotIdle + case .safeMode: safeModeSessionNotIdle + } + } + + static func refusal(from error: Error, reason: Reason) -> Error { + guard let failure = error as? MariaDBPluginError, failure.code == refusalCode else { return error } + return MariaDBPluginError(code: failure.code, message: refusalMessage(reason), sqlState: failure.sqlState) + } +} diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction.swift b/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction.swift index 2abe7697c8..65196a14c7 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction.swift @@ -4,21 +4,16 @@ // import Foundation -import TableProPluginKit -internal enum MySQLReadOnlyTransaction { +// The iOS MySQL driver compiles this file too; the plugin's statement handling is in +// MySQLReadOnlyTransaction+Statement.swift. +nonisolated internal enum MySQLReadOnlyTransaction { // Who asked for the read-only mode, which is what a refusal names. enum Reason: Sendable, Equatable { case externalClients case safeMode } - // The session state is read on the statement's own queue, so nothing queued behind it can open a transaction first. - struct Scope: Sendable { - let reason: Reason - let sessionState: @Sendable (Bool) -> PluginSessionTransactionState - } - // Not the `/*!50605 ... */` spelling: a server older than 5.6.5 would run that as a plain read-write transaction. static let begin = "START TRANSACTION READ ONLY" static let end = "ROLLBACK" @@ -26,75 +21,18 @@ internal enum MySQLReadOnlyTransaction { // ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION. static let refusalCode: UInt32 = 1_792 - static var sessionNotIdle: MariaDBPluginError { - MariaDBPluginError( - code: 0, - message: String( - localized: "This connection has a transaction open. Commit or roll it back in TablePro, then try again." - ), - sqlState: nil - ) - } - - // Read-Only offers no Commit, so the way out is a rollback. - static var safeModeSessionNotIdle: MariaDBPluginError { - MariaDBPluginError( - code: 0, - message: String( - localized: "Safe Mode is set to Read-Only for this connection, and it has a transaction open. Roll it back, then try again." - ), - sqlState: nil - ) - } - - static func reason(for context: PluginStatementContext, flavor: MySQLServerFlavor) -> Reason? { - guard flavor.enforcesReadOnlyTransactions else { return nil } - if context.readOnly, context.origin == .external { return .externalClients } - if context.enforcesReadOnly { return .safeMode } - return nil - } - - static func applies(to context: PluginStatementContext, flavor: MySQLServerFlavor) -> Bool { - reason(for: context, flavor: flavor) != nil - } - - // `START TRANSACTION` would commit the user's open transaction and release their `LOCK TABLES`. - static func run( - state: PluginSessionTransactionState, - reason: Reason, - send: (String) throws -> Void, - statement: () throws -> Result - ) throws -> Result { - guard state == .idle else { throw notIdle(reason) } - do { - try send(begin) - } catch { - try? send(end) - throw error - } - let result: Result - do { - result = try statement() - } catch { - try? send(end) - throw refusal(from: error, reason: reason) - } - try send(end) - return result - } - - static func notIdle(_ reason: Reason) -> MariaDBPluginError { + static func notIdleMessage(_ reason: Reason) -> String { switch reason { - case .externalClients: sessionNotIdle - case .safeMode: safeModeSessionNotIdle + case .externalClients: + String(localized: "This connection has a transaction open. Commit or roll it back in TablePro, then try again.") + // Read-Only offers no Commit, so the way out is a rollback. + case .safeMode: + String( + localized: "Safe Mode is set to Read-Only for this connection, and it has a transaction open. Roll it back, then try again." + ) } } - static func refusal(from error: Error, reason: Reason) -> Error { - guard let failure = error as? MariaDBPluginError, failure.code == refusalCode else { return error } - return MariaDBPluginError(code: failure.code, message: refusalMessage(reason), sqlState: failure.sqlState) - } - static func refusalMessage(_ reason: Reason) -> String { switch reason { case .externalClients: diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLServerVersion.swift b/apple/Plugins/MySQLDriverPlugin/MySQLServerVersion.swift index 06b49ebf21..0908a55b5e 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLServerVersion.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLServerVersion.swift @@ -101,6 +101,19 @@ nonisolated internal enum MySQLServerVersion { flavor.isMariaDB && isAtLeast((10, 2, 7), banner: banner) } + // MySQL 5.6.5 (WL #5968) and MariaDB 10.0.0 added `START TRANSACTION READ ONLY`. No other flavor is held to it, as + // `MySQLServerFlavor.enforcesReadOnlyTransactions` says. + static func hasReadOnlyTransactions(banner: String?, flavor: MySQLServerFlavor) -> Bool { + switch flavor { + case .mysql: + return !isLegacy(below: (5, 6, 5), banner: banner, flavor: flavor) + case .mariadb: + return !isLegacy(below: (10, 0, 0), banner: banner, flavor: flavor) + case .tidb, .oceanbase, .databend, .starrocks: + return false + } + } + // A binary default arrives as its bytes from MySQL 5.7.44, MariaDB and OceanBase, and as `0x` hex from MySQL 8.0.11 to 8.4.13. static func reportsBinaryDefaultAsHex(banner: String?, flavor: MySQLServerFlavor) -> Bool { switch flavor { diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLSessionFootprint.swift b/apple/Plugins/MySQLDriverPlugin/MySQLSessionFootprint.swift index 8f55556a51..9e386e5dc9 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLSessionFootprint.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLSessionFootprint.swift @@ -29,7 +29,7 @@ import TableProPluginKit /// Every ambiguity resolves toward "the session is holding something". The cost of that is only /// that a connection keeps its slot, which is what happens today; the cost of the opposite is /// destroying a user's transaction or temporary table without telling them. -struct MySQLSessionFootprint: Equatable { +nonisolated struct MySQLSessionFootprint: Equatable { private(set) var hasOpenTransaction = false private(set) var hasTemporaryTables = false private(set) var hasUserVariables = false diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLTemporaryTableNames.swift b/apple/Plugins/MySQLDriverPlugin/MySQLTemporaryTableNames.swift index 8dc474070a..73b7298922 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLTemporaryTableNames.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLTemporaryTableNames.swift @@ -8,7 +8,7 @@ import Foundation // MySQL 8.4.11 lists a session's temporary tables nowhere an ordinary user can read (`INNODB_TEMP_TABLE_INFO` needs // PROCESS), so the names come from the statement text. Only the table part is kept: `SHOW CREATE TABLE name` on the // session resolves it in the browsed database and answers `CREATE TEMPORARY TABLE` for one. -enum MySQLTemporaryTableNames { +nonisolated enum MySQLTemporaryTableNames { static func created(by statement: String) -> String? { var reader = Reader(statement) guard reader.accept("CREATE") else { return nil } @@ -53,7 +53,7 @@ enum MySQLTemporaryTableNames { } } -private enum NameToken: Equatable { +nonisolated private enum NameToken: Equatable { case word(String) case quoted(String) case dot @@ -62,7 +62,7 @@ private enum NameToken: Equatable { } // A string literal is one `other` token, so a keyword inside one is never read. -private struct Reader { +nonisolated private struct Reader { private let scalars: [Unicode.Scalar] private var index = 0 private var executableCommentDepth = 0 diff --git a/apple/Plugins/MySQLDriverTests/MySQLServerVersionTests.swift b/apple/Plugins/MySQLDriverTests/MySQLServerVersionTests.swift index db606c0d9b..fb79fabb9f 100644 --- a/apple/Plugins/MySQLDriverTests/MySQLServerVersionTests.swift +++ b/apple/Plugins/MySQLDriverTests/MySQLServerVersionTests.swift @@ -15,6 +15,22 @@ struct MySQLServerVersionTests { #expect(!MySQLServerVersion.hasGenerationExpression(banner: "unknown", flavor: .mysql)) } + @Test("READ ONLY transactions start at MySQL 5.6.5 and MariaDB 10.0, an unreadable banner is not an old server, and no other flavor has them") + func readOnlyTransactionFloor() { + #expect(!MySQLServerVersion.hasReadOnlyTransactions(banner: "5.6.4-log", flavor: .mysql)) + #expect(MySQLServerVersion.hasReadOnlyTransactions(banner: "5.6.5", flavor: .mysql)) + #expect(MySQLServerVersion.hasReadOnlyTransactions(banner: "8.4.13", flavor: .mysql)) + #expect(!MySQLServerVersion.hasReadOnlyTransactions(banner: "5.5.64-MariaDB", flavor: .mariadb)) + #expect(MySQLServerVersion.hasReadOnlyTransactions(banner: "10.0.0-MariaDB", flavor: .mariadb)) + #expect(MySQLServerVersion.hasReadOnlyTransactions(banner: "11.4.13-MariaDB", flavor: .mariadb)) + #expect(MySQLServerVersion.hasReadOnlyTransactions(banner: nil, flavor: .mysql)) + #expect(MySQLServerVersion.hasReadOnlyTransactions(banner: "unknown", flavor: .mariadb)) + #expect(!MySQLServerVersion.hasReadOnlyTransactions(banner: "8.0.11-TiDB-v8.5.1", flavor: .tidb(version: nil))) + #expect(!MySQLServerVersion.hasReadOnlyTransactions(banner: "5.7.25", flavor: .oceanbase(version: nil))) + #expect(!MySQLServerVersion.hasReadOnlyTransactions(banner: "8.0.0", flavor: .databend)) + #expect(!MySQLServerVersion.hasReadOnlyTransactions(banner: "8.0.33", flavor: .starrocks)) + } + /// The other direction: a gate that picks legacy syntax has to hear a version before it does, /// because the legacy statements are a 1064 on MySQL 8. @Test("isKnownBelow answers false for a banner it cannot read") diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction+Statement.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction+Statement.swift new file mode 100644 index 0000000000..a32d2af586 --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction+Statement.swift @@ -0,0 +1,68 @@ +// +// LibPQReadOnlyTransaction+Statement.swift +// PostgreSQLDriverPlugin +// + +import Foundation +import TableProPluginKit + +extension LibPQReadOnlyTransaction { + static var sessionNotIdle: LibPQPluginError { + LibPQPluginError(message: notIdleMessage(.externalClients), sqlState: nil, detail: nil) + } + + static var safeModeSessionNotIdle: LibPQPluginError { + LibPQPluginError(message: notIdleMessage(.safeMode), sqlState: nil, detail: nil) + } + + static func reason(for context: PluginStatementContext, capabilities: PluginCapabilities) -> Reason? { + if context.readOnly, context.origin == .external, capabilities.contains(.readOnlyStatements) { + return .externalClients + } + if context.enforcesReadOnly, capabilities.contains(.enforcesReadOnlyMode) { + return .safeMode + } + return nil + } + + static func applies(to context: PluginStatementContext, capabilities: PluginCapabilities) -> Bool { + reason(for: context, capabilities: capabilities) != nil + } + + // Inside an open transaction the wrap would make the user's own transaction read-only. + static func run( + state: PluginSessionTransactionState, + reason: Reason, + send: (String) throws -> Void, + statement: () throws -> Result + ) throws -> Result { + guard state == .idle else { throw notIdle(reason) } + do { + try send(begin) + } catch { + try? send(end) + throw error + } + let result: Result + do { + result = try statement() + } catch { + try? send(end) + throw refusal(from: error, reason: reason) + } + try send(end) + return result + } + + static func notIdle(_ reason: Reason) -> LibPQPluginError { + switch reason { + case .externalClients: sessionNotIdle + case .safeMode: safeModeSessionNotIdle + } + } + + static func refusal(from error: Error, reason: Reason) -> Error { + guard let failure = error as? LibPQPluginError, isRefusal(sqlState: failure.sqlState) else { return error } + return LibPQPluginError(message: refusalMessage(reason), sqlState: failure.sqlState, detail: failure.message) + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction.swift index 81204295f1..9d497da505 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction.swift @@ -4,9 +4,10 @@ // import Foundation -import TableProPluginKit -internal enum LibPQReadOnlyTransaction { +// The iOS PostgreSQL driver compiles this file too; the plugin's statement handling is in +// LibPQReadOnlyTransaction+Statement.swift. +nonisolated internal enum LibPQReadOnlyTransaction { // Who asked for the read-only mode, which is what a refusal names. enum Reason: Sendable, Equatable { case externalClients @@ -21,81 +22,23 @@ internal enum LibPQReadOnlyTransaction { // `cannot execute ... in a read-only transaction`, and `cannot run inside a transaction block` for VACUUM. static let refusalStates: Set = ["25006", "25001"] - static var sessionNotIdle: LibPQPluginError { - LibPQPluginError( - message: String( - localized: "This connection has a transaction open. Commit or roll it back in TablePro, then try again." - ), - sqlState: nil, - detail: nil - ) + static func isRefusal(sqlState: String?) -> Bool { + guard let sqlState else { return false } + return refusalStates.contains(sqlState) } - // Read-Only offers no Commit, so the way out is a rollback. - static var safeModeSessionNotIdle: LibPQPluginError { - LibPQPluginError( - message: String( - localized: "Safe Mode is set to Read-Only for this connection, and it has a transaction open. Roll it back, then try again." - ), - sqlState: nil, - detail: nil - ) - } - - static func reason(for context: PluginStatementContext, capabilities: PluginCapabilities) -> Reason? { - if context.readOnly, context.origin == .external, capabilities.contains(.readOnlyStatements) { - return .externalClients - } - if context.enforcesReadOnly, capabilities.contains(.enforcesReadOnlyMode) { - return .safeMode - } - return nil - } - - static func applies(to context: PluginStatementContext, capabilities: PluginCapabilities) -> Bool { - reason(for: context, capabilities: capabilities) != nil - } - - // Inside an open transaction the wrap would make the user's own transaction read-only. - static func run( - state: PluginSessionTransactionState, - reason: Reason, - send: (String) throws -> Void, - statement: () throws -> Result - ) throws -> Result { - guard state == .idle else { throw notIdle(reason) } - do { - try send(begin) - } catch { - try? send(end) - throw error - } - let result: Result - do { - result = try statement() - } catch { - try? send(end) - throw refusal(from: error, reason: reason) - } - try send(end) - return result - } - - static func notIdle(_ reason: Reason) -> LibPQPluginError { + static func notIdleMessage(_ reason: Reason) -> String { switch reason { - case .externalClients: sessionNotIdle - case .safeMode: safeModeSessionNotIdle + case .externalClients: + String(localized: "This connection has a transaction open. Commit or roll it back in TablePro, then try again.") + // Read-Only offers no Commit, so the way out is a rollback. + case .safeMode: + String( + localized: "Safe Mode is set to Read-Only for this connection, and it has a transaction open. Roll it back, then try again." + ) } } - static func refusal(from error: Error, reason: Reason) -> Error { - guard let failure = error as? LibPQPluginError, - let sqlState = failure.sqlState, - refusalStates.contains(sqlState) - else { return error } - return LibPQPluginError(message: refusalMessage(reason), sqlState: sqlState, detail: failure.message) - } - static func refusalMessage(_ reason: Reason) -> String { switch reason { case .externalClients: diff --git a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift index d25e056259..7e72b5f3a9 100644 --- a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift +++ b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionCoordinator.swift @@ -272,6 +272,11 @@ final class ConnectionCoordinator { return activeDatabase } + // SQL Server names it in front of the schema; other engines ignore it. + var tableDatabase: String? { + activeDatabase.isEmpty ? nil : activeDatabase + } + static var editorHeldElsewhereMessage: String { String(localized: "Another window has unsaved changes on this connection. Save or discard them before switching database.") } @@ -462,16 +467,13 @@ final class ConnectionCoordinator { model.endSwitch() await model.loadSchemasAfterSwitch(to: used, from: self) await model.runHeldBackWork(from: self) - // MySQL names the database in every Tables tab write, and a plugin driver runs the app's requests in the - // menu's database; SQL Server saves only once the session is back. + // Every Tables tab write names its database, and a plugin driver runs the app's requests in the menu's. presentFailure( title: Self.switchDatabaseFailedTitle, - message: model.qualifiesTablesWithDatabase || driver.reportsDatabaseWithoutStatement - ? String(format: String(localized: "The query moved the session to “%@” and it could not go back to “%@”. Unsaved changes still save to “%@”."), used, back, back) - : String( - format: String(localized: "The query moved the session to “%@” and it could not go back to “%@”. To save the unsaved changes, run USE %@ in the Query tab of the window that holds them."), - used, back, SQLBuilder.quoteIdentifier(back, for: connection.type) - ) + message: String( + format: String(localized: "The query moved the session to “%@” and it could not go back to “%@”. Unsaved changes still save to “%@”."), + used, back, back + ) ) return } @@ -489,8 +491,8 @@ final class ConnectionCoordinator { } // From the start of a run with a `USE` until the picker follows it, another run's statements could go out around the - // move, so no window runs one, and on SQL Server, whose Tables tab names no database, every window's writes are - // refused. A driver that reports its database without a statement keeps the app's requests where the picker says. + // move, so no window runs one. The Tables tab names its database, so its writes go on. A driver that reports its + // database without a statement keeps the app's requests where the picker says. func beginDatabaseMove(ifRunning text: String) -> Bool { guard supportsDatabaseSwitching, let driver = session?.driver as? any SessionDatabaseReporting, !driver.reportsDatabaseWithoutStatement, @@ -515,26 +517,6 @@ final class ConnectionCoordinator { ) } - var databaseMoveRefusal: AppError? { - guard supportsDatabaseSwitching, !model.qualifiesTablesWithDatabase, - let driver = session?.driver as? any SessionDatabaseReporting, - !driver.reportsDatabaseWithoutStatement else { return nil } - if model.isDatabaseMoving { return Self.databaseMovingRefusal } - // A session that could not go back to where an unsaved edit was made. SQL Server compares names by its collation, - // which ignores case unless set otherwise. - if let current = driver.currentDatabase, !current.isEmpty, !activeDatabase.isEmpty, - current.caseInsensitiveCompare(activeDatabase) != .orderedSame { - return AppError( - category: .query, - title: String(localized: "Session in Another Database"), - message: String(format: String(localized: "The session is in “%@”, not “%@”, so this change cannot be saved now."), current, activeDatabase), - recovery: nil, - underlying: nil - ) - } - return nil - } - func switchDatabase(to name: String) async { await switchDatabase(to: name, inside: nil) } diff --git a/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift index 6182d6bd72..e00048133e 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MSSQLDriver.swift @@ -38,6 +38,9 @@ nonisolated final class MSSQLDriver: DatabaseDriver, SessionDatabaseReporting, @ nonisolated(unsafe) private(set) var currentSchema: String? = "dbo" // A connection that names no database opens in the login's default one, which only the server knows. private let sessionDatabaseName = OSAllocatedUnfairLock(initialState: nil) + // Where the app last put the session (the open, a switch), which a `USE` the user runs never moves. Catalog reads + // run there, so a list read while such a run is in flight still lists the picker's database. + private let appDatabaseName = OSAllocatedUnfairLock(initialState: nil) var currentDatabase: String? { sessionDatabaseName.withLock { $0 } @@ -113,7 +116,8 @@ nonisolated final class MSSQLDriver: DatabaseDriver, SessionDatabaseReporting, @ } } - _ = try? await sessionDatabase() + let opened = try? await sessionDatabase() + appDatabaseName.withLock { $0 = opened } if let serverSchema = try? await runQuery(MSSQLSchemaQueries.currentSchema).rows.first?.first ?? nil, !serverSchema.isEmpty { @@ -291,10 +295,11 @@ nonisolated final class MSSQLDriver: DatabaseDriver, SessionDatabaseReporting, @ throw mapToConnectionError(error) } sessionDatabaseName.withLock { $0 = name } + appDatabaseName.withLock { $0 = name } } func fetchSchemas() async throws -> [String] { - let result = try await runQuery(MSSQLSchemaQueries.schemas) + let result = try await runQuery(catalogRead(MSSQLSchemaQueries.schemas, in: nil)) return result.rows.compactMap { $0.first ?? nil } } @@ -312,8 +317,28 @@ nonisolated final class MSSQLDriver: DatabaseDriver, SessionDatabaseReporting, @ private var effectiveSchema: String { currentSchema ?? "dbo" } + // A system procedure called through a database name runs in that database and leaves the session where it is. + static func catalogStatement(_ statement: String, in database: String?) -> String { + guard let database, !database.isEmpty else { return statement } + return MSSQLPrincipalStatements.inDatabase(database, statement) + } + + // The database a window names wins; a list, or a read that names none, goes where the app last put the session. + static func catalogTarget(named database: String?, appDatabase: String?) -> String? { + database.flatMap { $0.isEmpty ? nil : $0 } ?? appDatabase + } + + private func catalogRead(_ statement: String, in database: String?) -> String { + Self.catalogStatement( + statement, + in: Self.catalogTarget(named: database, appDatabase: appDatabaseName.withLock { $0 }) + ) + } + func fetchTables(schema: String?) async throws -> [TableInfo] { - let result = try await runQuery(MSSQLSchemaQueries.tables(schema: schema ?? effectiveSchema)) + let result = try await runQuery( + catalogRead(MSSQLSchemaQueries.tables(schema: schema ?? effectiveSchema), in: nil) + ) return result.rows.compactMap { row in MSSQLSchemaQueries.parseTableRow(row).map { TableInfo(name: $0.name, type: $0.isView ? .view : .table) @@ -322,7 +347,13 @@ nonisolated final class MSSQLDriver: DatabaseDriver, SessionDatabaseReporting, @ } func fetchColumns(table: String, schema: String?) async throws -> [ColumnInfo] { - let result = try await runQuery(MSSQLSchemaQueries.columns(schema: schema ?? effectiveSchema, table: table)) + try await fetchColumns(table: table, schema: schema, database: nil) + } + + func fetchColumns(table: String, schema: String?, database: String?) async throws -> [ColumnInfo] { + let result = try await runQuery( + catalogRead(MSSQLSchemaQueries.columns(schema: schema ?? effectiveSchema, table: table), in: database) + ) return result.rows.enumerated().compactMap { idx, row in MSSQLSchemaQueries.parseColumnRow(row).map { parsed in ColumnInfo( @@ -342,7 +373,13 @@ nonisolated final class MSSQLDriver: DatabaseDriver, SessionDatabaseReporting, @ } func fetchIndexes(table: String, schema: String?) async throws -> [IndexInfo] { - let result = try await runQuery(MSSQLSchemaQueries.indexes(schema: schema ?? effectiveSchema, table: table)) + try await fetchIndexes(table: table, schema: schema, database: nil) + } + + func fetchIndexes(table: String, schema: String?, database: String?) async throws -> [IndexInfo] { + let result = try await runQuery( + catalogRead(MSSQLSchemaQueries.indexes(schema: schema ?? effectiveSchema, table: table), in: database) + ) var byName: [String: (unique: Bool, primary: Bool, cols: [String])] = [:] for row in result.rows { guard let parsed = MSSQLSchemaQueries.parseIndexRow(row) else { continue } @@ -357,7 +394,13 @@ nonisolated final class MSSQLDriver: DatabaseDriver, SessionDatabaseReporting, @ } func fetchForeignKeys(table: String, schema: String?) async throws -> [ForeignKeyInfo] { - let result = try await runQuery(MSSQLSchemaQueries.foreignKeys(schema: schema ?? effectiveSchema, table: table)) + try await fetchForeignKeys(table: table, schema: schema, database: nil) + } + + func fetchForeignKeys(table: String, schema: String?, database: String?) async throws -> [ForeignKeyInfo] { + let result = try await runQuery( + catalogRead(MSSQLSchemaQueries.foreignKeys(schema: schema ?? effectiveSchema, table: table), in: database) + ) return result.rows.compactMap { row in MSSQLSchemaQueries.parseForeignKeyRow(row).map { ForeignKeyInfo(name: $0.constraintName, column: $0.columnName, diff --git a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift index 47bb123785..475fffe139 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/MySQLDriver.swift @@ -33,6 +33,12 @@ nonisolated final class MySQLDriver: DatabaseDriver, SessionDatabaseReporting, @ var currentSchema: String? { nil } var supportsTransactions: Bool { true } var reportsDatabaseWithoutStatement: Bool { false } + var enforcesReadOnlyMode: Bool { Self.runsReadOnlyTransactions(for: databaseType, banner: serverVersion) } + + func entersReadOnlyMode() async -> Bool { + guard enforcesReadOnlyMode else { return false } + return await actor.takesReadOnlyTransaction() + } private let settledFacts = OSAllocatedUnfairLock(initialState: nil) @@ -133,6 +139,11 @@ nonisolated final class MySQLDriver: DatabaseDriver, SessionDatabaseReporting, @ ) } + static func runsReadOnlyTransactions(for databaseType: DatabaseType, banner: String?) -> Bool { + let flavor = serverFlavor(for: databaseType, banner: banner) + return flavor.enforcesReadOnlyTransactions && MySQLServerVersion.hasReadOnlyTransactions(banner: banner, flavor: flavor) + } + func connect() async throws { try await LocalNetworkPermission.shared.ensureAccess(for: host) try await actor.connect( @@ -208,11 +219,14 @@ nonisolated final class MySQLDriver: DatabaseDriver, SessionDatabaseReporting, @ let statement = killer.reserve() let databaseType = self.databaseType let session = settledLexicalFacts + let inReadOnlyTransaction = options.enforcesReadOnly && enforcesReadOnlyMode return AsyncThrowingStream { continuation in let task = Task { var openStream: MySQLStreamID? do { - switch try await actor.beginStream(query: query, statement: statement) { + switch try await actor.beginStream( + query: query, statement: statement, inReadOnlyTransaction: inReadOnlyTransaction + ) { case .noResult(let affectedRows): if affectedRows != 0 { continuation.yield(.rowsAffected(affectedRows)) @@ -658,6 +672,11 @@ nonisolated enum MySQLSessionTransaction { guard serverStatus & UInt32(SERVER_STATUS_IN_TRANS) != 0 else { return .idle } return serverStatus & UInt32(SERVER_STATUS_AUTOCOMMIT) != 0 ? .explicitTransaction : .implicitTransaction } + + // A Read-Only statement's own transaction opened on an idle session and ends with its stream. + static func state(infoResult: my_bool, serverStatus: UInt32, holdsReadOnlyTransaction: Bool) -> DriverTransactionState { + holdsReadOnlyTransaction ? .idle : state(infoResult: infoResult, serverStatus: serverStatus) + } } // MARK: - MySQL Actor (thread-safe C API access) @@ -865,8 +884,11 @@ private actor MySQLActor { showTextIsBinary && mysqlStatementListsServerMetadata(query) } + // Closing the session ends a read-only transaction on the server. func close() { killer.detach() + holdsReadOnlyTransaction = false + footprint.reset() guard let mysql else { return } if let result = streamingResult { // An open result points at the handle `mysql_close` frees, so it goes first; shutting the socket @@ -923,7 +945,9 @@ private actor MySQLActor { guard let mysql else { return .unknown } var serverStatus: UInt32 = 0 let infoResult = mariadb_get_info(mysql, MARIADB_CONNECTION_SERVER_STATUS, &serverStatus) - return MySQLSessionTransaction.state(infoResult: infoResult, serverStatus: serverStatus) + return MySQLSessionTransaction.state( + infoResult: infoResult, serverStatus: serverStatus, holdsReadOnlyTransaction: holdsReadOnlyTransaction + ) } /// `NO_BACKSLASH_ESCAPES` from the status flags of the last reply turns the backslash off in both string quotes. @@ -954,7 +978,10 @@ private actor MySQLActor { let start = Date() + let features = lexicalFeatures(of: mysql) + footprint.observe(query, lexicalFeatures: features) guard mysql_real_query(mysql, query, UInt(query.utf8.count)) == 0 else { + footprint.observeFailure(of: query, lexicalFeatures: features) throw MySQLError.queryFailed(message(from: mysql)) } @@ -1020,10 +1047,22 @@ private actor MySQLActor { private var streamingColumns: [ColumnInfo] = [] private var streamingDecoding = MySQLResultColumns() private var streamingStatement: SentStatement? - - func beginStream(query: String, statement: UInt64) throws -> MySQLBeginStreamResult { + // Set from START TRANSACTION READ ONLY until the ROLLBACK after the statement's last result. + private var holdsReadOnlyTransaction = false + // The status flags never show a LOCK TABLES, which a START TRANSACTION releases, so the statements say it. + private var footprint = MySQLSessionFootprint() + + // With `inReadOnlyTransaction` the state check, the transaction and the statement go out in this one actor call, + // so nothing else on the session runs between them. The session is opened without CLIENT_MULTI_STATEMENTS, so no + // `COMMIT; DELETE ...` can follow the read. + func beginStream( + query: String, + statement: UInt64, + inReadOnlyTransaction: Bool = false + ) throws -> MySQLBeginStreamResult { guard let mysql else { throw MySQLError.notConnected } endLeftoverStream(of: mysql) + if inReadOnlyTransaction { try beginReadOnlyTransaction() } streamGeneration &+= 1 let stream = MySQLStreamID(generation: streamGeneration) let sent = send(statement, on: mysql) @@ -1031,23 +1070,30 @@ private actor MySQLActor { // flight and kills it. guard !Task.isCancelled else { finish(sent, on: mysql) + try? endReadOnlyTransaction() throw CancellationError() } + let features = lexicalFeatures(of: mysql) + footprint.observe(query, lexicalFeatures: features) guard mysql_real_query(mysql, query, UInt(query.utf8.count)) == 0 else { + footprint.observeFailure(of: query, lexicalFeatures: features) + let failure = queryFailure(of: mysql) finish(sent, on: mysql) - throw MySQLError.queryFailed(message(from: mysql)) + try? endReadOnlyTransaction() + throw failure } guard let result = mysql_use_result(mysql) else { - defer { finish(sent, on: mysql) } - if mysql_field_count(mysql) != 0 { - throw MySQLError.queryFailed(message(from: mysql)) + return try settle(sent, on: mysql) { + if mysql_field_count(mysql) != 0 { + throw queryFailure(of: mysql) + } + let raw = mysql_affected_rows(mysql) + let affected = raw == .max ? 0 : Int(clamping: raw) + try consumeTrailingResults(of: mysql) + return .noResult(affectedRows: affected) } - let raw = mysql_affected_rows(mysql) - let affected = raw == .max ? 0 : Int(clamping: raw) - try consumeTrailingResults(of: mysql) - return .noResult(affectedRows: affected) } streamingResult = result @@ -1119,11 +1165,7 @@ private actor MySQLActor { if let sent = streamingStatement { killer.rowCapReached(sent.id) } - do { - try releaseStream(of: mysql) - } catch { - guard flavor.isInterruptedByKill(errno: mysql_errno(mysql), message: message(from: mysql)) else { throw error } - } + try releaseStream(of: mysql, atRowCap: true) } /// The producer of a superseded stream only learns that its rows were cut off, so an error in the rest of @@ -1138,13 +1180,76 @@ private actor MySQLActor { } } - private func releaseStream(of mysql: UnsafeMutablePointer) throws { + // At the row cap the kill's 1317 is the cut the reader was already told of. + private func releaseStream(of mysql: UnsafeMutablePointer, atRowCap: Bool = false) throws { guard let result = streamingResult else { return } let sent = streamingStatement clearStream() - defer { if let sent { finish(sent, on: mysql) } } - try drain(result, of: mysql) - try consumeTrailingResults(of: mysql) + try settle(sent, on: mysql) { + do { + try drain(result, of: mysql) + try consumeTrailingResults(of: mysql) + } catch where atRowCap && flavor.isInterruptedByKill(errno: mysql_errno(mysql), message: message(from: mysql)) {} + } + } + + // The kill bookkeeping reads the statement's error code, so the ROLLBACK, which resets it, comes after. + private func settle( + _ sent: SentStatement?, + on mysql: UnsafeMutablePointer, + _ statement: () throws -> Value + ) throws -> Value { + let outcome = Result(catching: statement) + if let sent { finish(sent, on: mysql) } + switch outcome { + case .success(let value): + try endReadOnlyTransaction() + return value + case .failure(let error): + try? endReadOnlyTransaction() + throw error + } + } + + // A START TRANSACTION would commit the session's open transaction and release its LOCK TABLES. A `SET autocommit + // = 0` session is always in one, so the app does not ask for the read-only one there. + private func beginReadOnlyTransaction() throws { + guard takesReadOnlyTransaction() else { + throw ReadOnlyModeRefusal.sessionNotIdle(message: MySQLReadOnlyTransaction.notIdleMessage(.safeMode)) + } + do { + _ = try execute(MySQLReadOnlyTransaction.begin) + } catch { + _ = try? execute(MySQLReadOnlyTransaction.end) + throw error + } + holdsReadOnlyTransaction = true + } + + func takesReadOnlyTransaction() -> Bool { + mysql != nil && transactionState() == .idle && !footprint.hasLockedTables + } + + private func lexicalFeatures(of mysql: UnsafeMutablePointer) -> SQLLexicalFeatures { + var serverStatus: UInt32 = 0 + let known = mariadb_get_info(mysql, MARIADB_CONNECTION_SERVER_STATUS, &serverStatus) == 0 + let noBackslashEscapes = known ? serverStatus & UInt32(SERVER_STATUS_NO_BACKSLASH_ESCAPES) != 0 : nil + return MySQLLexicalFeatures.features(for: flavor, noBackslashEscapes: noBackslashEscapes) + } + + private func endReadOnlyTransaction() throws { + guard holdsReadOnlyTransaction else { return } + holdsReadOnlyTransaction = false + _ = try execute(MySQLReadOnlyTransaction.end) + } + + // Taken before the ROLLBACK, which resets the code. + private func queryFailure(of mysql: UnsafeMutablePointer) -> any Error { + let text = message(from: mysql) + guard holdsReadOnlyTransaction, mysql_errno(mysql) == MySQLReadOnlyTransaction.refusalCode else { + return MySQLError.queryFailed(text) + } + return ReadOnlyModeRefusal.refused(message: MySQLReadOnlyTransaction.refusalMessage(.safeMode), serverMessage: text) } private func clearStream() { @@ -1196,12 +1301,12 @@ private actor MySQLActor { private func consumeTrailingResults(of mysql: UnsafeMutablePointer) throws { while mysql_more_results(mysql) != 0 { let status = mysql_next_result(mysql) - if status > 0 { throw MySQLError.queryFailed(message(from: mysql)) } + if status > 0 { throw queryFailure(of: mysql) } if status < 0 { return } if let result = mysql_use_result(mysql) { try drain(result, of: mysql) } else if mysql_field_count(mysql) != 0 { - throw MySQLError.queryFailed(message(from: mysql)) + throw queryFailure(of: mysql) } } } @@ -1216,10 +1321,10 @@ private actor MySQLActor { /// `mysql_fetch_row` returns nil both at the end and on an error, and a command refused while the result /// was open leaves its own code on the handle, so only a code the fetch set means the rows ended in an error. - private func fetchFailure(of mysql: UnsafeMutablePointer, errorBefore: UInt32) -> MySQLError? { + private func fetchFailure(of mysql: UnsafeMutablePointer, errorBefore: UInt32) -> (any Error)? { let code = mysql_errno(mysql) guard code != 0, code != errorBefore else { return nil } - return .queryFailed(message(from: mysql)) + return queryFailure(of: mysql) } private func makeCellRef(column: String, row: MYSQL_ROW, options: StreamOptions, columns: [ColumnInfo]) -> CellRef? { diff --git a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift index bc1112cdaf..8408576e5d 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift @@ -24,6 +24,8 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, SessionSchemaReporting var supportsSchemas: Bool { true } var supportsTransactions: Bool { true } + // As on the Mac, where the PostgreSQL plugin driver declares the mode and Redshift's does not. + var enforcesReadOnlyMode: Bool { databaseType == .postgresql } private let settledFacts = OSAllocatedUnfairLock(initialState: nil) @@ -186,10 +188,13 @@ nonisolated final class PostgreSQLDriver: DatabaseDriver, SessionSchemaReporting let cancelHandle = actor.cancelHandle let id = cancelHandle.reserveQuery() let readsOnly = !SQLWriteClassifier.isWriteQuery(query, databaseType: databaseType) + let inReadOnlyTransaction = options.enforcesReadOnly && enforcesReadOnlyMode return AsyncThrowingStream { continuation in let task = Task { do { - switch try await actor.beginStream(query: query, id: id, readsOnly: readsOnly) { + switch try await actor.beginStream( + query: query, id: id, readsOnly: readsOnly, inReadOnlyTransaction: inReadOnlyTransaction + ) { case .commandOk(let affectedRows, let statusMessage): if affectedRows != 0 { continuation.yield(.rowsAffected(affectedRows)) @@ -643,18 +648,33 @@ private actor PostgreSQLActor { /// of a stream that a later query replaced can neither read nor end the later one. private var stream: PostgreSQLStreamState? - func beginStream(query: String, id: UInt64, readsOnly: Bool) throws -> PGBeginStreamResult { + // With `inReadOnlyTransaction` the state check, the transaction and the statement go out in this one actor call, so + // nothing else on the session runs between them. + func beginStream( + query: String, + id: UInt64, + readsOnly: Bool, + inReadOnlyTransaction: Bool = false + ) throws -> PGBeginStreamResult { endLeftoverStream() guard let conn else { throw PostgreSQLError.notConnected } let cancelsOutput = cancelsAbandonedOutput(conn) + let statusAtSend = PQtransactionStatus(conn) + if inReadOnlyTransaction { try beginReadOnlyTransaction(from: statusAtSend) } let state = PostgreSQLStreamState( id: id, cancelsAbandonedOutput: cancelsOutput, cancelsUnreadRows: cancelsOutput && readsOnly, - transactionStatusAtSend: PQtransactionStatus(conn) + transactionStatusAtSend: statusAtSend, + runsReadOnly: inReadOnlyTransaction ) - try send(query, id: id, on: conn) + do { + try send(query, id: id, on: conn, singleStatement: inReadOnlyTransaction) + } catch { + _ = endReadOnlyTransaction(of: state) + throw error + } guard PQsetSingleRowMode(conn) == 1 else { _ = drain(state) throw PostgreSQLError.queryFailed("Failed to enter single-row streaming mode") @@ -672,14 +692,14 @@ private actor PostgreSQLActor { case PGRES_TUPLES_OK: let columns = parseColumns(firstResult) PQclear(firstResult) - if let error = drain(state).error { throw error } + if let error = drainFailure(state) { throw error } stream = state return .tuples(columns) case PGRES_COMMAND_OK: let affected = Int(String(cString: PQcmdTuples(firstResult))) ?? 0 let commandTag = String(cString: PQcmdStatus(firstResult)) PQclear(firstResult) - if let error = drain(state).error { throw error } + if let error = drainFailure(state) { throw error } return .commandOk( affectedRows: affected, statusMessage: PostgreSQLSessionTransaction.statusMessage( @@ -690,18 +710,55 @@ private actor PostgreSQLActor { ) default: state.pendingResult = firstResult - if let error = drain(state).error { throw error } + if let error = drainFailure(state) { throw error } return .commandOk(affectedRows: 0, statusMessage: nil) } } + // Inside an open transaction the wrap would make the user's own transaction read-only. + private func beginReadOnlyTransaction(from status: PGTransactionStatusType) throws { + guard status == PQTRANS_IDLE else { + throw ReadOnlyModeRefusal.sessionNotIdle(message: LibPQReadOnlyTransaction.notIdleMessage(.safeMode)) + } + do { + _ = try execute(LibPQReadOnlyTransaction.begin) + } catch { + _ = rollBackReadOnlyTransaction() + throw error + } + } + + // Once per stream, after its last result. Nil when the ROLLBACK went through or there was none to send. + private func endReadOnlyTransaction(of state: PostgreSQLStreamState) -> String? { + guard state.holdsReadOnlyTransaction else { return nil } + state.holdsReadOnlyTransaction = false + return rollBackReadOnlyTransaction() + } + + // Registered with no query id, so Stop cannot cancel it and leave the read-only transaction open. A connection the + // drain dropped has nothing to roll back. + private func rollBackReadOnlyTransaction() -> String? { + guard let conn else { return nil } + cancelHandle.waitForPendingCancels() + guard let result = PQexec(conn, LibPQReadOnlyTransaction.end) else { return Self.message(from: conn) } + defer { PQclear(result) } + guard PQresultStatus(result) == PGRES_COMMAND_OK else { return String(cString: PQresultErrorMessage(result)) } + return nil + } + + private func drainFailure(_ state: PostgreSQLStreamState) -> (any Error)? { + drain(state).error(inReadOnlyTransaction: state.runsReadOnly) + } + /// A cancel still on its way could reach the server after this query and stop it instead, so the /// query waits for it. A consumer that leaves as the query goes out finds nothing in flight to /// cancel, so its cancel is sent here once the query is registered. - private func send(_ query: String, id: UInt64, on conn: OpaquePointer) throws { + private func send(_ query: String, id: UInt64, on conn: OpaquePointer, singleStatement: Bool) throws { cancelHandle.waitForPendingCancels() try Task.checkCancellation() - guard PQsendQuery(conn, query) == 1 else { + // The extended protocol takes one statement, so no `COMMIT; DELETE ...` can follow a read-only read. + let sent = singleStatement ? PQsendQueryParams(conn, query, 0, nil, nil, nil, nil, 0) : PQsendQuery(conn, query) + guard sent == 1 else { throw PostgreSQLError.queryFailed(String(cString: PQerrorMessage(conn))) } cancelHandle.querySent(id) @@ -726,7 +783,7 @@ private actor PostgreSQLActor { } guard PQresultStatus(result) == PGRES_SINGLE_TUPLE else { state.pendingResult = result - if let error = drain(state).error { throw error } + if let error = drainFailure(state) { throw error } return nil } defer { PQclear(result) } @@ -738,7 +795,7 @@ private actor PostgreSQLActor { guard let state = stream, state.id == id else { return } stream = nil let end = drain(state) - guard end != .cancelled, let error = end.error else { return } + guard end != .cancelled, let error = end.error(inReadOnlyTransaction: state.runsReadOnly) else { return } throw error } @@ -786,22 +843,36 @@ private actor PostgreSQLActor { _ = drain(leftover) } + // The connection closes next, which ends a read-only transaction on the server. private func discardStream() { guard let state = stream else { return } stream = nil if let readAhead = state.pendingResult { PQclear(readAhead) } state.pendingResult = nil + state.holdsReadOnlyTransaction = false markDrained(state) } private func markDrained(_ state: PostgreSQLStreamState) { state.isDrained = true cancelHandle.queryFinished(state.id) + if let failure = endReadOnlyTransaction(of: state) { + Self.logger.warning("The read-only transaction did not roll back: \(failure, privacy: .private)") + } } - /// Reads every result the stream's query still has in flight, ending any COPY among them. + // The ROLLBACK goes out once the results are read and the query is no longer in flight. Its failure is the stream's + // when the statement itself ended cleanly. private func drain(_ state: PostgreSQLStreamState) -> PostgreSQLStreamEnd { guard !state.isDrained else { return .completed } + let end = drainResults(state) + let rollbackFailure = endReadOnlyTransaction(of: state) + guard end == .completed, let rollbackFailure else { return end } + return .failed(rollbackFailure) + } + + // Reads every result the stream's query still has in flight, ending any COPY among them. + private func drainResults(_ state: PostgreSQLStreamState) -> PostgreSQLStreamEnd { state.isDrained = true defer { cancelHandle.queryFinished(state.id) } var readAhead = state.pendingResult @@ -828,9 +899,15 @@ private actor PostgreSQLActor { private static func pendingResult(of result: OpaquePointer) -> PostgreSQLPendingResult { if let copy = LibPQCopyState.copy(of: result) { return .copy(copy) } guard PostgreSQLStreamDrain.isFailure(PQresultStatus(result)) else { return .completed } - return .failed(String(cString: PQresultErrorMessage(result))) + return .failed( + String(cString: PQresultErrorMessage(result)), + sqlState: PQresultErrorField(result, sqlStateField).map { String(cString: $0) } + ) } + // PG_DIAG_SQLSTATE, a character macro Swift does not import. + private static let sqlStateField = Int32(UInt8(ascii: "C")) + private func cells(of result: OpaquePointer, options: StreamOptions, columns: [ColumnInfo]) -> [Cell] { var cells: [Cell] = [] cells.reserveCapacity(columns.count) @@ -924,20 +1001,27 @@ nonisolated private final class PostgreSQLStreamState { /// `UPDATE ... RETURNING` past the row cap has to finish, or its write is undone. let cancelsUnreadRows: Bool let transactionStatusAtSend: PGTransactionStatusType + // The statement ran in Read-Only's read-only transaction, so a refusal state is Read-Only's refusal. + let runsReadOnly: Bool /// A result read before its turn: the first row, or what the row-cap check took off the wire. var pendingResult: OpaquePointer? var isDrained = false + // Until the ROLLBACK goes out. + var holdsReadOnlyTransaction: Bool init( id: UInt64, cancelsAbandonedOutput: Bool, cancelsUnreadRows: Bool, - transactionStatusAtSend: PGTransactionStatusType + transactionStatusAtSend: PGTransactionStatusType, + runsReadOnly: Bool = false ) { self.id = id self.cancelsAbandonedOutput = cancelsAbandonedOutput self.cancelsUnreadRows = cancelsUnreadRows self.transactionStatusAtSend = transactionStatusAtSend + self.runsReadOnly = runsReadOnly + holdsReadOnlyTransaction = runsReadOnly } } @@ -1085,7 +1169,7 @@ nonisolated private final class PostgreSQLCancelHandle: @unchecked Sendable { /// A result read while a stream ends, reduced to what decides how the stream ends. nonisolated enum PostgreSQLPendingResult: Equatable, Sendable { case copy(LibPQCopy) - case failed(String) + case failed(String, sqlState: String? = nil) case completed } @@ -1093,9 +1177,14 @@ nonisolated enum PostgreSQLStreamEnd: Equatable, Sendable { case completed case cancelled case abandonedCopy(LibPQCopyDirection) - case failed(String) + case failed(String, sqlState: String? = nil) var error: (any Error)? { + error(inReadOnlyTransaction: false) + } + + // In Read-Only's read-only transaction a refusal state is the engine holding Read-Only, not a failed query. + func error(inReadOnlyTransaction: Bool) -> (any Error)? { switch self { case .completed: return nil @@ -1103,8 +1192,13 @@ nonisolated enum PostgreSQLStreamEnd: Equatable, Sendable { return CancellationError() case .abandonedCopy(let direction): return PostgreSQLError.unsupported(direction.mobileUnsupportedMessage) - case .failed(let message): - return PostgreSQLError.queryFailed(message) + case .failed(let message, let sqlState): + guard inReadOnlyTransaction, LibPQReadOnlyTransaction.isRefusal(sqlState: sqlState) else { + return PostgreSQLError.queryFailed(message) + } + return ReadOnlyModeRefusal.refused( + message: LibPQReadOnlyTransaction.refusalMessage(.safeMode), serverMessage: message + ) } } } @@ -1124,14 +1218,14 @@ nonisolated enum PostgreSQLStreamDrain { endCopy: (LibPQCopy) -> Void, afterCancel: @autoclosure () -> Bool ) -> (end: PostgreSQLStreamEnd, outcome: LibPQDrainOutcome) { - var firstFailure: String? + var firstFailure: (message: String, sqlState: String?)? let outcome = LibPQPendingResultDrain.drain( nextResult: { switch nextResult() { case .copy(let copy): return .copy(copy) - case .failed(let message): - if firstFailure == nil { firstFailure = message } + case .failed(let message, let sqlState): + if firstFailure == nil { firstFailure = (message, sqlState) } return .completed case .completed: return .completed @@ -1145,7 +1239,7 @@ nonisolated enum PostgreSQLStreamDrain { if let copy = outcome.abandonedCopy { end = .abandonedCopy(copy.direction) } else if let firstFailure { - end = afterCancel() ? .cancelled : .failed(firstFailure) + end = afterCancel() ? .cancelled : .failed(firstFailure.message, sqlState: firstFailure.sqlState) } else { end = .completed } diff --git a/apple/TableProMobile/TableProMobile/Drivers/ReadOnlyModeRefusal.swift b/apple/TableProMobile/TableProMobile/Drivers/ReadOnlyModeRefusal.swift new file mode 100644 index 0000000000..78047408b4 --- /dev/null +++ b/apple/TableProMobile/TableProMobile/Drivers/ReadOnlyModeRefusal.swift @@ -0,0 +1,18 @@ +import Foundation + +// Safe Mode Read-Only's refusal from a driver that runs the statement in the engine's read-only transaction. The message +// is the one the engine's Mac plugin shows, from the file both apps compile. +nonisolated enum ReadOnlyModeRefusal: LocalizedError, Equatable { + case sessionNotIdle(message: String) + case refused(message: String, serverMessage: String) + + var errorDescription: String? { + switch self { + case .sessionNotIdle(let message): + return message + case .refused(let message, let serverMessage): + let server = serverMessage.trimmingCharacters(in: .whitespacesAndNewlines) + return server.isEmpty ? message : "\(message)\n\(server)" + } + } +} diff --git a/apple/TableProMobile/TableProMobile/Helpers/ReadOnlyFunctionGuard.swift b/apple/TableProMobile/TableProMobile/Helpers/ReadOnlyFunctionGuard.swift index 9693630ddb..6cae5c072a 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/ReadOnlyFunctionGuard.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/ReadOnlyFunctionGuard.swift @@ -1,14 +1,22 @@ import Foundation +import TableProDatabase import TableProModels import TableProSQLGrammar import TableProSQLVocabulary -// No iOS driver runs a read in the engine's read-only mode, so at Read-Only a call off the built-in list, on an engine -// whose functions can write, cannot be told from a write and is refused like one. +// In the engine's read-only mode the server refuses what a function writes inside the transaction. Without it, at +// Read-Only a call off the built-in list, on an engine whose functions can write, cannot be told from a write and is +// refused like one. nonisolated enum ReadOnlyFunctionGuard { - static func refuses(_ sql: String, databaseType: DatabaseType, level: SafeModeLevel) -> Bool { + static func refuses( + _ sql: String, + databaseType: DatabaseType, + level: SafeModeLevel, + enforcesReadOnlyMode: Bool + ) -> Bool { let typeId = databaseType.rawValue - guard level.blocksWrites, SQLReadOnlyFunctionCalls.queryFunctionsCanWrite.contains(typeId) else { return false } + guard level.blocksWrites, !enforcesReadOnlyMode, + SQLReadOnlyFunctionCalls.queryFunctionsCanWrite.contains(typeId) else { return false } return SQLReadOnlyFunctionCalls.callsUnlistedFunction( sql, databaseTypeId: typeId, @@ -16,4 +24,11 @@ nonisolated enum ReadOnlyFunctionGuard { typeNames: SQLEngineVocabulary.curated(forDatabaseTypeId: typeId)?.dataTypes ?? [] ) } + + // A write never reaches the driver at Read-Only, so only a read asks the session. One holding a transaction or a + // table lock runs as it is, and MySQL with autocommit off always holds a transaction. + static func runsInReadOnlyMode(writes: Bool, level: SafeModeLevel, driver: any DatabaseDriver) async -> Bool { + guard !writes, level.blocksWrites else { return false } + return await driver.entersReadOnlyMode() + } } diff --git a/apple/TableProMobile/TableProMobile/Helpers/RowInserter.swift b/apple/TableProMobile/TableProMobile/Helpers/RowInserter.swift index 51e30f4422..2ec25c6f66 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/RowInserter.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/RowInserter.swift @@ -7,6 +7,7 @@ nonisolated enum RowInsertPlanner { static func statements( table: String, schema: String?, + database: String? = nil, type: DatabaseType, driver: any DatabaseDriver, columns: [ColumnInfo], @@ -21,6 +22,7 @@ nonisolated enum RowInsertPlanner { SQLBuilder.buildInsert( table: table, schema: schema, + database: database, type: type, driver: driver, columns: values.map(\.column), @@ -32,6 +34,7 @@ nonisolated enum RowInsertPlanner { static func batch( table: String, schema: String?, + database: String? = nil, type: DatabaseType, driver: any DatabaseDriver, columns: [ColumnInfo], @@ -41,7 +44,7 @@ nonisolated enum RowInsertPlanner { ) throws -> RowWriteBatch? { guard let writer = driver as? any DriverRowWriting else { let sql = try statements( - table: table, schema: schema, type: type, driver: driver, columns: columns, rows: rows, + table: table, schema: schema, database: database, type: type, driver: driver, columns: columns, rows: rows, allowAllDefaults: allowAllDefaults, dropsEmptyPrimaryKey: dropsEmptyPrimaryKey ) return sql.isEmpty ? nil : .sql(sql, driver) @@ -80,13 +83,15 @@ nonisolated enum RowInserter { table: String, type: DatabaseType, schema: String?, + database: String? = nil, qualifier: String?, rows: [PayloadRow] ) async throws -> Int { - let columns = try await driver.fetchColumns(table: table, schema: schema) + let columns = try await driver.fetchColumns(table: table, schema: schema, database: database) guard let batch = try RowInsertPlanner.batch( table: table, schema: qualifier, + database: database, type: type, driver: driver, columns: columns, diff --git a/apple/TableProMobile/TableProMobile/Helpers/RowWriteBatch.swift b/apple/TableProMobile/TableProMobile/Helpers/RowWriteBatch.swift index 18174895e4..493980c306 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/RowWriteBatch.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/RowWriteBatch.swift @@ -32,6 +32,7 @@ nonisolated enum RowWriteBatch: Sendable { static func update( table: String, schema: String?, + database: String? = nil, type: DatabaseType, driver: any DatabaseDriver, columns: [String], @@ -49,8 +50,8 @@ nonisolated enum RowWriteBatch: Sendable { } guard let keys = locator.keyValues else { return nil } let sql = SQLBuilder.buildUpdate( - table: table, schema: schema, type: type, driver: driver, changes: changes, primaryKeys: keys, - columns: columnDetails + table: table, schema: schema, database: database, type: type, driver: driver, changes: changes, + primaryKeys: keys, columns: columnDetails ) return .sql([sql], driver) } @@ -58,6 +59,7 @@ nonisolated enum RowWriteBatch: Sendable { static func delete( table: String, schema: String?, + database: String? = nil, type: DatabaseType, driver: any DatabaseDriver, columns: [String], @@ -73,7 +75,8 @@ nonisolated enum RowWriteBatch: Sendable { } guard let keys = locator.keyValues, !keys.isEmpty else { return nil } let sql = SQLBuilder.buildDelete( - table: table, schema: schema, type: type, driver: driver, primaryKeys: keys, columns: columnDetails + table: table, schema: schema, database: database, type: type, driver: driver, primaryKeys: keys, + columns: columnDetails ) return .sql([sql], driver) } @@ -95,6 +98,7 @@ nonisolated struct PlannedRowWrite: Sendable { static func update( table: String, schema: String?, + database: String? = nil, type: DatabaseType, columns: [String], columnDetails: [ColumnInfo], @@ -103,7 +107,7 @@ nonisolated struct PlannedRowWrite: Sendable { ) -> PlannedRowWrite { PlannedRowWrite { driver in try RowWriteBatch.update( - table: table, schema: schema, type: type, driver: driver, columns: columns, + table: table, schema: schema, database: database, type: type, driver: driver, columns: columns, columnDetails: columnDetails, locator: locator, changes: changes ) } @@ -112,13 +116,14 @@ nonisolated struct PlannedRowWrite: Sendable { static func insert( table: String, schema: String?, + database: String? = nil, type: DatabaseType, columns: [ColumnInfo], values: [String: PayloadValue] ) -> PlannedRowWrite { PlannedRowWrite { driver in try RowInsertPlanner.batch( - table: table, schema: schema, type: type, driver: driver, columns: columns, + table: table, schema: schema, database: database, type: type, driver: driver, columns: columns, rows: [PayloadRow(values: values)], allowAllDefaults: true, dropsEmptyPrimaryKey: false ) } diff --git a/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift b/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift index e9f7dca291..4fbe342211 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/SQLBuilder.swift @@ -46,13 +46,15 @@ nonisolated enum SQLBuilder { } } - static func buildCount(table: String, schema: String?, type: DatabaseType) -> String { - let quoted = qualifiedIdentifier(table: table, schema: schema, for: type) + static func buildCount(table: String, schema: String?, database: String? = nil, type: DatabaseType) -> String { + let quoted = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) return "SELECT COUNT(*) FROM \(quoted)" } - static func buildSelect(table: String, schema: String?, type: DatabaseType, limit: Int, offset: Int) -> String { - let quoted = qualifiedIdentifier(table: table, schema: schema, for: type) + static func buildSelect( + table: String, schema: String?, database: String? = nil, type: DatabaseType, limit: Int, offset: Int + ) -> String { + let quoted = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) let pagination = paginationClause(orderBy: "", limit: limit, offset: offset, for: type) return "SELECT * FROM \(quoted) \(pagination)" } @@ -62,12 +64,13 @@ nonisolated enum SQLBuilder { static func buildSingleRowLookup( table: String, schema: String?, + database: String? = nil, key: [(column: String, value: String)], columns: [ColumnInfo], type: DatabaseType, driver: any DatabaseDriver ) -> String { - let quotedTable = qualifiedIdentifier(table: table, schema: schema, for: type) + let quotedTable = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) let predicate = key.isEmpty ? "1 = 0" : keyPredicate(key, columns: columns, type: type, driver: driver, exactText: false) let pagination = paginationClause(orderBy: "", limit: 1, offset: 0, for: type) @@ -135,24 +138,26 @@ nonisolated enum SQLBuilder { static func buildKeyMatchCount( table: String, schema: String?, + database: String? = nil, key: [(column: String, value: String)], columns: [ColumnInfo], type: DatabaseType, driver: any DatabaseDriver ) -> String { - let quotedTable = qualifiedIdentifier(table: table, schema: schema, for: type) + let quotedTable = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) return "SELECT COUNT(*) FROM \(quotedTable) WHERE \(keyPredicate(key, columns: columns, type: type, driver: driver))" } static func buildDelete( table: String, schema: String?, + database: String? = nil, type: DatabaseType, driver: any DatabaseDriver, primaryKeys: [(column: String, value: String)], columns: [ColumnInfo] ) -> String { - let quotedTable = qualifiedIdentifier(table: table, schema: schema, for: type) + let quotedTable = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) let predicate = keyPredicate(primaryKeys, columns: columns, type: type, driver: driver, uniqueIn: quotedTable) return "DELETE FROM \(quotedTable) WHERE \(predicate)" } @@ -160,13 +165,14 @@ nonisolated enum SQLBuilder { static func buildUpdate( table: String, schema: String?, + database: String? = nil, type: DatabaseType, driver: any DatabaseDriver, changes: [(column: String, value: String?)], primaryKeys: [(column: String, value: String)], columns: [ColumnInfo] ) -> String { - let quotedTable = qualifiedIdentifier(table: table, schema: schema, for: type) + let quotedTable = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) let assignments = changes.map { col, val in let qcol = quoteIdentifier(col, for: type) if let val { return "\(qcol) = \(stringLiteral(val, for: type, driver: driver))" } @@ -179,12 +185,13 @@ nonisolated enum SQLBuilder { static func buildInsert( table: String, schema: String?, + database: String? = nil, type: DatabaseType, driver: any DatabaseDriver, columns: [String], values: [String?] ) -> String? { - let qualifiedTable = qualifiedIdentifier(table: table, schema: schema, for: type) + let qualifiedTable = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) guard !columns.isEmpty else { return buildAllDefaultsInsert(qualifiedTable: qualifiedTable, for: type) } @@ -206,8 +213,8 @@ nonisolated enum SQLBuilder { } // SQLite has no TRUNCATE statement; a DELETE with no WHERE is its truncate. - static func truncateStatement(table: String, schema: String?, type: DatabaseType) -> String { - let quoted = qualifiedIdentifier(table: table, schema: schema, for: type) + static func truncateStatement(table: String, schema: String?, database: String? = nil, type: DatabaseType) -> String { + let quoted = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) return type.isSQLiteFamily ? "DELETE FROM \(quoted)" : "TRUNCATE TABLE \(quoted)" } @@ -215,25 +222,33 @@ nonisolated enum SQLBuilder { buildAllDefaultsInsert(qualifiedTable: "t", for: type) != nil } - static func qualifiedIdentifier(table: String, schema: String?, for type: DatabaseType) -> String { + // SQL Server resolves `[schema].[table]` in the session's database, which a `USE` run in the Query tab moves, so + // it names the database too. MySQL's database already sits in `schema`; other engines have no third part. + static func qualifiedIdentifier( + table: String, schema: String?, database: String? = nil, for type: DatabaseType + ) -> String { let quotedTable = quoteIdentifier(table, for: type) - guard let schema, !schema.isEmpty else { return quotedTable } - return "\(quoteIdentifier(schema, for: type)).\(quotedTable)" + let quotedSchema = schema.flatMap { $0.isEmpty ? nil : quoteIdentifier($0, for: type) } + if type == .mssql, let database, !database.isEmpty { + return "\(quoteIdentifier(database, for: type)).\(quotedSchema ?? "").\(quotedTable)" + } + guard let quotedSchema else { return quotedTable } + return "\(quotedSchema).\(quotedTable)" } static func buildSelect( - table: String, schema: String?, type: DatabaseType, + table: String, schema: String?, database: String? = nil, type: DatabaseType, sortState: SortState, limit: Int, offset: Int ) -> String { - let quoted = qualifiedIdentifier(table: table, schema: schema, for: type) + let quoted = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) let orderBy = buildOrderByClause(sortState, for: type) let pagination = paginationClause(orderBy: orderBy, limit: limit, offset: offset, for: type) return "SELECT * FROM \(quoted) \(pagination)" } static func buildFilteredSelect( - table: String, schema: String?, type: DatabaseType, + table: String, schema: String?, database: String? = nil, type: DatabaseType, filters: [TableFilter], logicMode: FilterLogicMode, columns: [ColumnInfo] = [], session: SQLSessionLexicalFacts? = nil, @@ -242,7 +257,7 @@ nonisolated enum SQLBuilder { let dialect = dialectDescriptor(for: type) let generator = filterGenerator(dialect: dialect, type: type, session: session) let whereClause = generator.generateWhereClause(from: filters, logicMode: logicMode, columns: columns) - let quoted = qualifiedIdentifier(table: table, schema: schema, for: type) + let quoted = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) let pagination = paginationClause(orderBy: "", limit: limit, offset: offset, for: type) var sql = "SELECT * FROM \(quoted)" if !whereClause.isEmpty { sql += " \(whereClause)" } @@ -251,7 +266,7 @@ nonisolated enum SQLBuilder { } static func buildFilteredSelect( - table: String, schema: String?, type: DatabaseType, + table: String, schema: String?, database: String? = nil, type: DatabaseType, filters: [TableFilter], logicMode: FilterLogicMode, columns: [ColumnInfo] = [], session: SQLSessionLexicalFacts? = nil, @@ -262,7 +277,7 @@ nonisolated enum SQLBuilder { let generator = filterGenerator(dialect: dialect, type: type, session: session) let whereClause = generator.generateWhereClause(from: filters, logicMode: logicMode, columns: columns) let orderBy = buildOrderByClause(sortState, for: type) - let quoted = qualifiedIdentifier(table: table, schema: schema, for: type) + let quoted = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) let pagination = paginationClause(orderBy: orderBy, limit: limit, offset: offset, for: type) var sql = "SELECT * FROM \(quoted)" if !whereClause.isEmpty { sql += " \(whereClause)" } @@ -271,7 +286,7 @@ nonisolated enum SQLBuilder { } static func buildFilteredCount( - table: String, schema: String?, type: DatabaseType, + table: String, schema: String?, database: String? = nil, type: DatabaseType, filters: [TableFilter], logicMode: FilterLogicMode, columns: [ColumnInfo] = [], session: SQLSessionLexicalFacts? = nil @@ -279,7 +294,7 @@ nonisolated enum SQLBuilder { let dialect = dialectDescriptor(for: type) let generator = filterGenerator(dialect: dialect, type: type, session: session) let whereClause = generator.generateWhereClause(from: filters, logicMode: logicMode, columns: columns) - let quoted = qualifiedIdentifier(table: table, schema: schema, for: type) + let quoted = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) if whereClause.isEmpty { return "SELECT COUNT(*) FROM \(quoted)" } @@ -289,7 +304,7 @@ nonisolated enum SQLBuilder { // MARK: - Search static func buildSearchSelect( - table: String, schema: String?, type: DatabaseType, + table: String, schema: String?, database: String? = nil, type: DatabaseType, searchText: String, searchColumns: [ColumnInfo], filters: [TableFilter] = [], logicMode: FilterLogicMode = .and, columns: [ColumnInfo] = [], @@ -297,7 +312,7 @@ nonisolated enum SQLBuilder { sortState: SortState = SortState(), limit: Int, offset: Int ) -> String { - let quoted = qualifiedIdentifier(table: table, schema: schema, for: type) + let quoted = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) let whereClause = buildSearchWhereClause( searchText: searchText, searchColumns: searchColumns, filters: filters, logicMode: logicMode, columns: columns, type: type, session: session @@ -311,13 +326,13 @@ nonisolated enum SQLBuilder { } static func buildSearchCount( - table: String, schema: String?, type: DatabaseType, + table: String, schema: String?, database: String? = nil, type: DatabaseType, searchText: String, searchColumns: [ColumnInfo], filters: [TableFilter] = [], logicMode: FilterLogicMode = .and, columns: [ColumnInfo] = [], session: SQLSessionLexicalFacts? = nil ) -> String { - let quoted = qualifiedIdentifier(table: table, schema: schema, for: type) + let quoted = qualifiedIdentifier(table: table, schema: schema, database: database, for: type) let whereClause = buildSearchWhereClause( searchText: searchText, searchColumns: searchColumns, filters: filters, logicMode: logicMode, columns: columns, type: type, session: session diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index 18769fa956..088283e25d 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -19998,6 +19998,9 @@ } } } + }, + "Safe Mode is set to Read-Only for this connection, and it has a transaction open. Roll it back, then try again." : { + }, "Safe Mode is set to Read-Only for this connection, and the database refused the statement." : { @@ -23520,6 +23523,9 @@ "TablePro on iPhone and iPad can't connect to %1$@ with the SSL mode %2$@." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "TablePro is reading or writing rows on this connection with settings of its own." : { + }, "TablePro on iPhone and iPad can't open %@ connections." : { "extractionState" : "manual", @@ -26845,6 +26851,9 @@ } } } + }, + "This connection called a stored routine, so TablePro cannot tell what the session is holding." : { + }, "This connection goes through an SSH jump host, which TablePro on iPhone and iPad cannot dial. Open it in TablePro on the Mac." : { "localizations" : { @@ -26929,6 +26938,33 @@ } } } + }, + "This connection has a transaction open. Commit or roll it back in TablePro, then try again." : { + + }, + "This connection has an open transaction. Commit or roll it back first." : { + + }, + "This connection has open HANDLER cursors, which reconnecting would close." : { + + }, + "This connection has prepared statements, which reconnecting would discard." : { + + }, + "This connection has session settings changed, which reconnecting would reset." : { + + }, + "This connection has session variables set, which reconnecting would clear." : { + + }, + "This connection has temporary tables, which reconnecting would delete." : { + + }, + "This connection holds advisory locks, which reconnecting would release." : { + + }, + "This connection holds table locks, which reconnecting would release." : { + }, "This connection is in read-only mode. Write queries are not allowed." : { "localizations" : { @@ -26957,6 +26993,9 @@ } } } + }, + "This connection is read only for external clients, and the database refused the statement." : { + }, "This connection no longer exists. It may have been removed from another device." : { "localizations" : { @@ -27007,6 +27046,9 @@ }, "This connection signs in with the gcloud credentials on the Mac." : { + }, + "This connection switched database with USE, which reconnecting would undo." : { + }, "This connection uses a socket on a Mac and cannot be opened on iPhone or iPad." : { "extractionState" : "manual", diff --git a/apple/TableProMobile/TableProMobile/Models/ForeignKeyReference.swift b/apple/TableProMobile/TableProMobile/Models/ForeignKeyReference.swift index b65c0acfb5..ce9e1ce7a9 100644 --- a/apple/TableProMobile/TableProMobile/Models/ForeignKeyReference.swift +++ b/apple/TableProMobile/TableProMobile/Models/ForeignKeyReference.swift @@ -9,20 +9,32 @@ nonisolated struct ForeignKeyReference: Hashable, Identifiable, Sendable { let table: String let schema: String? + // Where the key was read. A SQL Server foreign key never crosses databases. + let database: String? let parts: [Part] var id: Self { self } + init(table: String, schema: String?, database: String? = nil, parts: [Part]) { + self.table = table + self.schema = schema + self.database = database + self.parts = parts + } + static func make( forColumn column: String, foreignKeys: [ForeignKeyInfo], columns: [ColumnInfo], cells: [Cell], - sourceSchema: String? + sourceSchema: String?, + sourceDatabase: String? = nil ) -> Self? { guard let target = columnIndex(named: column, in: columns) else { return nil } for key in keys(in: foreignKeys) where key.contains(where: { columnIndex(named: $0.column, in: columns) == target }) { - if let reference = resolve(key, columns: columns, cells: cells, sourceSchema: sourceSchema) { + if let reference = resolve( + key, columns: columns, cells: cells, sourceSchema: sourceSchema, sourceDatabase: sourceDatabase + ) { return reference } } @@ -33,11 +45,14 @@ nonisolated struct ForeignKeyReference: Hashable, Identifiable, Sendable { foreignKeys: [ForeignKeyInfo], columns: [ColumnInfo], cells: [Cell], - sourceSchema: String? + sourceSchema: String?, + sourceDatabase: String? = nil ) -> [(name: String, column: String, reference: Self)] { keys(in: foreignKeys).compactMap { key in guard let first = key.first, - let reference = resolve(key, columns: columns, cells: cells, sourceSchema: sourceSchema) + let reference = resolve( + key, columns: columns, cells: cells, sourceSchema: sourceSchema, sourceDatabase: sourceDatabase + ) else { return nil } return (name: first.name, column: key.map(\.column).joined(separator: ", "), reference: reference) } @@ -85,7 +100,8 @@ nonisolated struct ForeignKeyReference: Hashable, Identifiable, Sendable { _ key: [ForeignKeyInfo], columns: [ColumnInfo], cells: [Cell], - sourceSchema: String? + sourceSchema: String?, + sourceDatabase: String? ) -> Self? { guard let first = key.first else { return nil } var parts: [Part] = [] @@ -95,7 +111,10 @@ nonisolated struct ForeignKeyReference: Hashable, Identifiable, Sendable { else { return nil } parts.append(Part(column: pair.referencedColumn, value: value)) } - return Self(table: first.referencedTable, schema: first.referencedSchema ?? sourceSchema, parts: parts) + return Self( + table: first.referencedTable, schema: first.referencedSchema ?? sourceSchema, database: sourceDatabase, + parts: parts + ) } private static func columnIndex(named name: String, in columns: [ColumnInfo]) -> Int? { diff --git a/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift b/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift index 4385b10da9..cc770621ae 100644 --- a/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift +++ b/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift @@ -44,8 +44,10 @@ final class DataBrowserViewModel { @ObservationIgnored private var session: ConnectionSession? @ObservationIgnored private var table: TableInfo? @ObservationIgnored private var databaseType: DatabaseType = .mysql - // The schema the rows on screen came from. Every statement reads it, so an edit cannot target another schema. + // The schema and database the rows on screen came from. Every statement reads them, so an edit cannot target + // another schema or database. private(set) var schema: String? + private(set) var database: String? @ObservationIgnored private var host: String = "" @ObservationIgnored private var trackRead: SessionReadTracker? @ObservationIgnored private var pageEndedOnItsOwn = false @@ -123,15 +125,18 @@ final class DataBrowserViewModel { databaseType: DatabaseType, host: String, schema: String? = nil, + database: String? = nil, trackRead: SessionReadTracker? = nil ) { - // Another session or schema holds other rows, so neither the total nor a count still running applies. - let movesSource = session?.driver !== self.session?.driver || schema != self.schema || table.name != self.table?.name + // Another session, schema or database holds other rows, so neither the total nor a count still running applies. + let movesSource = session?.driver !== self.session?.driver || schema != self.schema || database != self.database + || table.name != self.table?.name self.session = session self.table = table self.databaseType = databaseType self.host = host self.schema = schema + self.database = database self.trackRead = trackRead if movesSource { resetTotal() @@ -216,7 +221,7 @@ final class DataBrowserViewModel { private func loadRows(session: ConnectionSession, table: TableInfo, isInitial: Bool, generation: Int) async { do { if columnDetails.isEmpty || isInitial { - let fetched = try await session.driver.fetchColumns(table: table.name, schema: schema) + let fetched = try await session.driver.fetchColumns(table: table.name, schema: schema, database: database) guard isCurrent(generation) else { return } columnDetails = fetched } @@ -247,7 +252,9 @@ final class DataBrowserViewModel { if !foreignKeysRead || isInitial { do { - let fetched = try await session.driver.fetchForeignKeys(table: table.name, schema: schema) + let fetched = try await session.driver.fetchForeignKeys( + table: table.name, schema: schema, database: database + ) guard isCurrent(generation) else { return } foreignKeys = fetched foreignKeysRead = true @@ -329,7 +336,7 @@ final class DataBrowserViewModel { let activeSort = effectiveSortState() if hasActiveSearch { return SQLBuilder.buildSearchSelect( - table: table.name, schema: schema, type: databaseType, + table: table.name, schema: schema, database: database, type: databaseType, searchText: activeSearchText, searchColumns: searchableColumns(), filters: filters, logicMode: filterLogicMode, columns: columnDetails, session: session?.driver.settledLexicalFacts, @@ -339,7 +346,7 @@ final class DataBrowserViewModel { } if hasActiveFilters { return SQLBuilder.buildFilteredSelect( - table: table.name, schema: schema, type: databaseType, + table: table.name, schema: schema, database: database, type: databaseType, filters: filters, logicMode: filterLogicMode, columns: columnDetails, session: session?.driver.settledLexicalFacts, sortState: activeSort, @@ -348,13 +355,13 @@ final class DataBrowserViewModel { } if activeSort.isSorting { return SQLBuilder.buildSelect( - table: table.name, schema: schema, type: databaseType, + table: table.name, schema: schema, database: database, type: databaseType, sortState: activeSort, limit: pagination.pageSize, offset: pagination.currentOffset ) } return SQLBuilder.buildSelect( - table: table.name, schema: schema, type: databaseType, + table: table.name, schema: schema, database: database, type: databaseType, limit: pagination.pageSize, offset: pagination.currentOffset ) } @@ -410,19 +417,21 @@ final class DataBrowserViewModel { let countQuery: String if hasActiveSearch { countQuery = SQLBuilder.buildSearchCount( - table: table.name, schema: schema, type: databaseType, + table: table.name, schema: schema, database: database, type: databaseType, searchText: activeSearchText, searchColumns: searchableColumns(), filters: filters, logicMode: filterLogicMode, columns: columnDetails, session: session.driver.settledLexicalFacts ) } else if hasActiveFilters { countQuery = SQLBuilder.buildFilteredCount( - table: table.name, schema: schema, type: databaseType, + table: table.name, schema: schema, database: database, type: databaseType, filters: filters, logicMode: filterLogicMode, columns: columnDetails, session: session.driver.settledLexicalFacts ) } else { - countQuery = SQLBuilder.buildCount(table: table.name, schema: schema, type: databaseType) + countQuery = SQLBuilder.buildCount( + table: table.name, schema: schema, database: database, type: databaseType + ) } let countResult = try await session.driver.execute(query: countQuery) guard epoch == countEpoch else { return } @@ -535,15 +544,16 @@ final class DataBrowserViewModel { ) } - // Where the rows on screen were read. Another window's switch re-attaches the browser to another schema, where the - // same key names another row. + // Where the rows on screen were read. Another window's switch re-attaches the browser to another schema or + // database, where the same key names another row. struct RowSource: Equatable { let driver: ObjectIdentifier? let schema: String? + let database: String? } var rowSource: RowSource { - RowSource(driver: session.map { ObjectIdentifier($0.driver) }, schema: schema) + RowSource(driver: session.map { ObjectIdentifier($0.driver) }, schema: schema, database: database) } func refusesDelete(raisedOn source: RowSource) -> Bool { @@ -561,6 +571,7 @@ final class DataBrowserViewModel { func deleteRow(_ locator: RowLocator) async -> Bool { guard let session, let table else { return false } let schema = schema + let database = database if let keys = locator.keyValues, let shared = await SharedRowKey.error( table: table.name, schema: schema, key: keys, columns: columnDetails, type: databaseType, driver: session.driver @@ -580,6 +591,7 @@ final class DataBrowserViewModel { guard let batch = try RowWriteBatch.delete( table: table.name, schema: schema, + database: database, type: databaseType, driver: session.driver, columns: locator.columnNames(of: columns), @@ -592,8 +604,8 @@ final class DataBrowserViewModel { var isGone = deleted > 0 if !isGone, let keys = locator.keyValues, !keys.isEmpty { isGone = try await !rowExists( - table: table.name, schema: schema, primaryKey: keys, locator: locator.documentLocator, - driver: session.driver + table: table.name, schema: schema, database: database, primaryKey: keys, + locator: locator.documentLocator, driver: session.driver ) } else if !isGone, let writer { isGone = try await writer.matchingRowCount( @@ -644,6 +656,7 @@ final class DataBrowserViewModel { private func rowExists( table: String, schema: String?, + database: String?, primaryKey: [(column: String, value: String)], locator: String?, driver: any DatabaseDriver @@ -651,21 +664,28 @@ final class DataBrowserViewModel { let components = primaryKey.map { PrimaryKeyComponent(column: $0.column, value: $0.value) } let ref = CellRef(table: table, column: components[0].column, primaryKey: components, locator: locator) do { - _ = try await readValue(driver: driver, ref: ref, databaseType: databaseType, schema: schema, ofOneRow: false) + _ = try await readValue( + driver: driver, ref: ref, databaseType: databaseType, schema: schema, database: database, ofOneRow: false + ) return true } catch CellLookupError.rowNotFound { return false } } - // The caller passes the schema its rows came from: a schema switch while a read is in flight must not move it. - func loadFullValue(driver: DatabaseDriver, ref: CellRef, databaseType: DatabaseType, schema: String?) async throws -> String? { - try await readValue(driver: driver, ref: ref, databaseType: databaseType, schema: schema, ofOneRow: true) + // The caller passes the schema and database its rows came from: a switch while a read is in flight must not move it. + func loadFullValue( + driver: DatabaseDriver, ref: CellRef, databaseType: DatabaseType, schema: String?, database: String? = nil + ) async throws -> String? { + try await readValue( + driver: driver, ref: ref, databaseType: databaseType, schema: schema, database: database, ofOneRow: true + ) } // A full value is read only from a key that names one row; whether a deleted row is still there is not. private func readValue( - driver: DatabaseDriver, ref: CellRef, databaseType: DatabaseType, schema: String?, ofOneRow: Bool + driver: DatabaseDriver, ref: CellRef, databaseType: DatabaseType, schema: String?, database: String?, + ofOneRow: Bool ) async throws -> String? { if let builder = driver as? any DriverQueryBuilding { return try await builder.storedValue( @@ -676,7 +696,7 @@ final class DataBrowserViewModel { locator: ref.locator ) } - let table = SQLBuilder.qualifiedIdentifier(table: ref.table, schema: schema, for: databaseType) + let table = SQLBuilder.qualifiedIdentifier(table: ref.table, schema: schema, database: database, for: databaseType) let predicate = SQLBuilder.keyPredicate( ref.primaryKey.map { (column: $0.column, value: $0.value) }, columns: columnDetails, @@ -722,6 +742,7 @@ final class DataBrowserViewModel { throw ClipboardExporter.TooLarge() } let schema = schema + let database = database var complete: [[Cell]] = [] complete.reserveCapacity(rows.count) for cells in rows { @@ -729,7 +750,9 @@ final class DataBrowserViewModel { for (index, cell) in cells.enumerated() { guard case .truncatedText(_, _, let ref?) = cell else { continue } try Task.checkCancellation() - let value = try await loadFullValue(driver: driver, ref: ref, databaseType: databaseType, schema: schema) + let value = try await loadFullValue( + driver: driver, ref: ref, databaseType: databaseType, schema: schema, database: database + ) row[index] = value.map(Cell.text) ?? .null } complete.append(row) diff --git a/apple/TableProMobile/TableProMobile/ViewModels/ForeignKeyPreviewModel.swift b/apple/TableProMobile/TableProMobile/ViewModels/ForeignKeyPreviewModel.swift index 9d4ee99662..461c4b6539 100644 --- a/apple/TableProMobile/TableProMobile/ViewModels/ForeignKeyPreviewModel.swift +++ b/apple/TableProMobile/TableProMobile/ViewModels/ForeignKeyPreviewModel.swift @@ -81,7 +81,9 @@ final class ForeignKeyPreviewModel { // The key columns' types pick each literal, as they do for the filter Open Table runs. var keyColumns: [ColumnInfo] = [] do { - keyColumns = try await driver.fetchColumns(table: reference.table, schema: reference.schema) + keyColumns = try await driver.fetchColumns( + table: reference.table, schema: reference.schema, database: reference.database + ) } catch is CancellationError { return } catch { @@ -98,6 +100,7 @@ final class ForeignKeyPreviewModel { let query = SQLBuilder.buildSingleRowLookup( table: reference.table, schema: reference.schema, + database: reference.database, key: key, columns: keyColumns, type: databaseType, @@ -126,12 +129,17 @@ final class ForeignKeyPreviewModel { var foreignKeys: [ForeignKeyInfo] = [] do { - foreignKeys = try await driver.fetchForeignKeys(table: reference.table, schema: reference.schema) + foreignKeys = try await driver.fetchForeignKeys( + table: reference.table, schema: reference.schema, database: reference.database + ) } catch { Self.logger.warning("Foreign key preview could not read keys: \(error.localizedDescription, privacy: .private)") } - fields = Self.fields(columns: result.columns, row: row, foreignKeys: foreignKeys, schema: reference.schema) + fields = Self.fields( + columns: result.columns, row: row, foreignKeys: foreignKeys, schema: reference.schema, + database: reference.database + ) phase = .loaded } @@ -140,7 +148,8 @@ final class ForeignKeyPreviewModel { columns: [ColumnInfo], row: [String?], foreignKeys: [ForeignKeyInfo], - schema: String? + schema: String?, + database: String? ) -> [Field] { let options = StreamOptions(textTruncationBytes: .max) let cells = row.indices.map { index in @@ -160,7 +169,8 @@ final class ForeignKeyPreviewModel { foreignKeys: foreignKeys, columns: columns, cells: cells, - sourceSchema: schema + sourceSchema: schema, + sourceDatabase: database ) ) } diff --git a/apple/TableProMobile/TableProMobile/ViewModels/QueryEditorViewModel.swift b/apple/TableProMobile/TableProMobile/ViewModels/QueryEditorViewModel.swift index 5b0c1b451d..c72841ce51 100644 --- a/apple/TableProMobile/TableProMobile/ViewModels/QueryEditorViewModel.swift +++ b/apple/TableProMobile/TableProMobile/ViewModels/QueryEditorViewModel.swift @@ -267,13 +267,19 @@ final class QueryEditorViewModel { return error.message } - func run(driver: DatabaseDriver, script: QueryScript, maxRows: Int = QueryEditorViewModel.maxBufferedRows) async { + func run( + driver: DatabaseDriver, + script: QueryScript, + maxRows: Int = QueryEditorViewModel.maxBufferedRows, + enforcesReadOnly: Bool = false + ) async { fetchTask?.cancel() let options = StreamOptions( textTruncationBytes: 4_096, inlineBinary: false, maxRows: maxRows, - lazyContext: nil + lazyContext: nil, + enforcesReadOnly: enforcesReadOnly ) runID &+= 1 let run = runID diff --git a/apple/TableProMobile/TableProMobile/ViewModels/RowDetailViewModel.swift b/apple/TableProMobile/TableProMobile/ViewModels/RowDetailViewModel.swift index 80af225dc2..bdf4e73f61 100644 --- a/apple/TableProMobile/TableProMobile/ViewModels/RowDetailViewModel.swift +++ b/apple/TableProMobile/TableProMobile/ViewModels/RowDetailViewModel.swift @@ -20,6 +20,7 @@ final class RowDetailViewModel { @ObservationIgnored private let liveSession: (() -> Result)? let databaseType: DatabaseType let schema: String? + let database: String? @ObservationIgnored private let readSafeModeLevel: () -> SafeModeLevel private(set) var rows: [Row] @@ -48,6 +49,7 @@ final class RowDetailViewModel { columnDetails: [ColumnInfo] = [], databaseType: DatabaseType = .sqlite, schema: String? = nil, + database: String? = nil, safeModeLevel: @escaping () -> SafeModeLevel = { .off }, foreignKeys: @escaping @autoclosure () -> [ForeignKeyInfo] = [], onSaved: (() -> Void)? = nil, @@ -63,6 +65,7 @@ final class RowDetailViewModel { self.columnDetails = columnDetails self.databaseType = databaseType self.schema = schema + self.database = database self.readSafeModeLevel = safeModeLevel self.readForeignKeys = foreignKeys self.onSaved = onSaved @@ -432,6 +435,7 @@ final class RowDetailViewModel { let write = PlannedRowWrite.update( table: table.name, schema: schema, + database: database, type: databaseType, columns: locator.columnNames(of: columns), columnDetails: columnDetails, diff --git a/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift b/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift index faa7f6a31c..a73e67880b 100644 --- a/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift +++ b/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift @@ -48,6 +48,12 @@ struct DataBrowserView: View { return activeSchema } + // A SQL Server foreign key never leaves its database, so a referenced table is read where the key was. + private var browserDatabase: String? { + if let reference { return reference.database } + return coordinator.tableDatabase + } + private var displaySchema: String? { reference?.displaySchema(current: activeSchema) } @@ -124,7 +130,7 @@ struct DataBrowserView: View { .task { viewModel.attach( session: session, table: table, databaseType: connection.type, - host: connection.host, schema: browserSchema, trackRead: pageReads + host: connection.host, schema: browserSchema, database: browserDatabase, trackRead: pageReads ) if let filters = reference?.filters() { viewModel.seedFilters(filters) @@ -136,15 +142,15 @@ struct DataBrowserView: View { guard found, reference != nil, viewModel.takeSingleRowMatch() else { return } showMatchedRow = true } - // A redial or another window's database switch hands this table a new session; a referenced table keeps its schema. - .onChange(of: BrowseSource(session: session, schema: browserSchema)) { _, source in + // A redial or another window's switch hands this table a new session; a referenced table keeps its schema and database. + .onChange(of: BrowseSource(session: session, schema: browserSchema, database: browserDatabase)) { _, source in // The confirmation names a row of the table this browser showed before. showDeleteConfirmation = false deleteTarget = nil guard source.driver != nil else { return } viewModel.attach( session: session, table: table, databaseType: connection.type, - host: connection.host, schema: source.schema, trackRead: pageReads + host: connection.host, schema: source.schema, database: source.database, trackRead: pageReads ) Task { await viewModel.load(isInitial: true) } } @@ -218,7 +224,10 @@ struct DataBrowserView: View { .sensoryFeedback(.success, trigger: hapticSuccess) .sensoryFeedback(.error, trigger: hapticError) .navigationDestination(isPresented: $showStructure) { - StructureView(table: table, session: session, databaseType: connection.type, schema: viewModel.schema) + StructureView( + table: table, session: session, databaseType: connection.type, schema: viewModel.schema, + database: viewModel.database + ) .transactionBar(for: coordinator) } .navigationDestination(isPresented: $showMatchedRow) { @@ -392,15 +401,17 @@ struct DataBrowserView: View { columnDetails: viewModel.columnDetails, databaseType: connection.type, schema: viewModel.schema, + database: viewModel.database, safeModeLevel: { [coordinator] in coordinator.connection.safeModeLevel }, foreignKeys: viewModel.foreignKeys, onSaved: { Task { await viewModel.load() } }, - loadFullValue: { [schema = viewModel.schema] ref in + loadFullValue: { [schema = viewModel.schema, database = viewModel.database] ref in guard let session else { return nil } // With autocommit off, a MySQL read opens a transaction; a failed read refreshes too, and never redials. do { let value = try await viewModel.loadFullValue( - driver: session.driver, ref: ref, databaseType: connection.type, schema: schema + driver: session.driver, ref: ref, databaseType: connection.type, schema: schema, + database: database ) await coordinator.refreshTransaction() return value @@ -484,7 +495,7 @@ struct DataBrowserView: View { } let references = ForeignKeyReference.all( foreignKeys: viewModel.foreignKeys, columns: columns, - cells: cells, sourceSchema: viewModel.schema + cells: cells, sourceSchema: viewModel.schema, sourceDatabase: viewModel.database ) if !references.isEmpty { Divider() @@ -635,6 +646,7 @@ struct DataBrowserView: View { session: session, databaseType: connection.type, schema: viewModel.schema, + database: viewModel.database, safeModeLevel: { [coordinator] in coordinator.connection.safeModeLevel }, onInserted: { Task { await viewModel.load() } } ) @@ -708,11 +720,6 @@ struct DataBrowserView: View { } private func performDelete(_ pending: PendingRowDelete) async { - if let refusal = coordinator.databaseMoveRefusal { - viewModel.operationError = refusal - hapticError.toggle() - return - } guard !viewModel.refusesDelete(raisedOn: pending.source) else { hapticError.toggle() return @@ -735,9 +742,11 @@ private struct PendingRowDelete { private struct BrowseSource: Equatable { let driver: ObjectIdentifier? let schema: String? + let database: String? - init(session: ConnectionSession?, schema: String?) { + init(session: ConnectionSession?, schema: String?, database: String?) { driver = session.map { ObjectIdentifier($0.driver) } self.schema = schema + self.database = database } } diff --git a/apple/TableProMobile/TableProMobile/Views/InsertRowView.swift b/apple/TableProMobile/TableProMobile/Views/InsertRowView.swift index 65c79ebe96..bd92195011 100644 --- a/apple/TableProMobile/TableProMobile/Views/InsertRowView.swift +++ b/apple/TableProMobile/TableProMobile/Views/InsertRowView.swift @@ -8,6 +8,7 @@ struct InsertRowView: View { let session: ConnectionSession? let databaseType: DatabaseType let schema: String? + let database: String? let safeModeLevel: () -> SafeModeLevel var onInserted: (() -> Void)? @@ -36,7 +37,10 @@ struct InsertRowView: View { } private var insertWrite: PlannedRowWrite { - PlannedRowWrite.insert(table: table.name, schema: schema, type: databaseType, columns: columnDetails, values: fields) + PlannedRowWrite.insert( + table: table.name, schema: schema, database: database, type: databaseType, columns: columnDetails, + values: fields + ) } var body: some View { @@ -305,12 +309,6 @@ struct InsertRowView: View { } private func executeInsert(_ write: PlannedRowWrite) async { - if let refusal = coordinator.databaseMoveRefusal { - operationError = refusal - showOperationError = true - hapticError.toggle() - return - } isSaving = true defer { isSaving = false } diff --git a/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift b/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift index 83688e6ce3..52e2600dec 100644 --- a/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift +++ b/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift @@ -239,7 +239,7 @@ struct QueryEditorView: View { } .confirmationDialog(String(localized: "Execute Write Query?"), isPresented: $showWriteConfirmation, titleVisibility: .visible) { Button(String(localized: "Execute"), role: .destructive) { - executeTask = Task { await executeQueryDirect(pendingWriteQuery) } + executeTask = Task { await executeQueryDirect(pendingWriteQuery, enforcesReadOnly: false) } } } message: { Text("This query will modify data. Are you sure you want to continue?") @@ -848,7 +848,11 @@ struct QueryEditorView: View { return } - if SQLWriteClassifier.isWriteQuery(trimmed, databaseType: databaseType) { + let writes = SQLWriteClassifier.isWriteQuery(trimmed, databaseType: databaseType) + let readOnlyMode = await ReadOnlyFunctionGuard.runsInReadOnlyMode( + writes: writes, level: safeModeLevel, driver: session.driver + ) + if writes { switch safeModeLevel.writePermission { case .blocked: showWriteBlockedAlert = true @@ -860,16 +864,18 @@ struct QueryEditorView: View { case .proceed: break } - } else if ReadOnlyFunctionGuard.refuses(trimmed, databaseType: databaseType, level: safeModeLevel) { + } else if ReadOnlyFunctionGuard.refuses( + trimmed, databaseType: databaseType, level: safeModeLevel, enforcesReadOnlyMode: readOnlyMode + ) { showFunctionBlockedAlert = true return } - await executeQueryDirect(trimmed) + await executeQueryDirect(trimmed, enforcesReadOnly: readOnlyMode) } // Claimed before the first await, so a second Run while the session reports how it reads literals starts nothing. - private func executeQueryDirect(_ trimmed: String) async { + private func executeQueryDirect(_ trimmed: String, enforcesReadOnly: Bool) async { guard let session, !isExecuting else { return } // A run with a `USE` in any window holds the session until the picker follows it. guard !coordinator.isDatabaseMoving else { @@ -937,7 +943,7 @@ struct QueryEditorView: View { failed: { _ in QueryExecutionOutcome(phase: viewModel.phase) == .failed }, rolledBack: { _ in viewModel.ranTransactionRollBack } ) { - await viewModel.run(driver: session.driver, script: script) + await viewModel.run(driver: session.driver, script: script, enforcesReadOnly: enforcesReadOnly) } progressUpdater.cancel() diff --git a/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift b/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift index 999f0b7a16..335954bee4 100644 --- a/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift +++ b/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift @@ -27,6 +27,7 @@ struct RowDetailView: View { columnDetails: [ColumnInfo] = [], databaseType: DatabaseType = .sqlite, schema: String? = nil, + database: String? = nil, safeModeLevel: @escaping () -> SafeModeLevel = { .off }, foreignKeys: @escaping @autoclosure () -> [ForeignKeyInfo] = [], onSaved: (() -> Void)? = nil, @@ -43,6 +44,7 @@ struct RowDetailView: View { columnDetails: columnDetails, databaseType: databaseType, schema: schema, + database: database, safeModeLevel: safeModeLevel, foreignKeys: foreignKeys(), onSaved: onSaved, @@ -289,7 +291,8 @@ struct RowDetailView: View { if let fk = viewModel.foreignKeys.first(where: { $0.column == column.name }), let reference = ForeignKeyReference.make( forColumn: column.name, foreignKeys: viewModel.foreignKeys, - columns: viewModel.columns, cells: cells, sourceSchema: viewModel.schema + columns: viewModel.columns, cells: cells, sourceSchema: viewModel.schema, + sourceDatabase: viewModel.database ) { Button { fkPreviewItem = FKPreviewItem(reference: reference) @@ -422,7 +425,6 @@ struct RowDetailView: View { } private func handleSave() async { - guard !refusesWhileDatabaseMoves() else { return } let success = await connectionCoordinator.track(failed: { _ in viewModel.operationError != nil }) { await viewModel.saveChanges() } @@ -438,7 +440,6 @@ struct RowDetailView: View { } private func executePendingSave() async { - guard !refusesWhileDatabaseMoves() else { return } let success = await connectionCoordinator.track(failed: { _ in viewModel.operationError != nil }) { await viewModel.executePendingSave() } @@ -449,13 +450,6 @@ struct RowDetailView: View { } } - private func refusesWhileDatabaseMoves() -> Bool { - guard let refusal = connectionCoordinator.databaseMoveRefusal else { return false } - viewModel.operationError = refusal - hapticError.toggle() - return true - } - // Names the cell rather than carrying its text, so the route stays cheap to compare. private struct FullValueRoute: Hashable { let row: Int diff --git a/apple/TableProMobile/TableProMobile/Views/StructureView.swift b/apple/TableProMobile/TableProMobile/Views/StructureView.swift index d1ff0db75d..7deac729de 100644 --- a/apple/TableProMobile/TableProMobile/Views/StructureView.swift +++ b/apple/TableProMobile/TableProMobile/Views/StructureView.swift @@ -9,6 +9,7 @@ struct StructureView: View { let session: ConnectionSession? let databaseType: DatabaseType let schema: String? + let database: String? private static let logger = Logger(subsystem: "com.TablePro", category: "StructureView") @@ -242,9 +243,11 @@ struct StructureView: View { appError = nil do { - async let fetchedColumns = session.driver.fetchColumns(table: table.name, schema: schema) - async let fetchedIndexes = session.driver.fetchIndexes(table: table.name, schema: schema) - async let fetchedForeignKeys = session.driver.fetchForeignKeys(table: table.name, schema: schema) + async let fetchedColumns = session.driver.fetchColumns(table: table.name, schema: schema, database: database) + async let fetchedIndexes = session.driver.fetchIndexes(table: table.name, schema: schema, database: database) + async let fetchedForeignKeys = session.driver.fetchForeignKeys( + table: table.name, schema: schema, database: database + ) self.columns = try await fetchedColumns self.indexes = try await fetchedIndexes diff --git a/apple/TableProMobile/TableProMobile/Views/TableListView.swift b/apple/TableProMobile/TableProMobile/Views/TableListView.swift index d22b0116c2..bd66c57ec8 100644 --- a/apple/TableProMobile/TableProMobile/Views/TableListView.swift +++ b/apple/TableProMobile/TableProMobile/Views/TableListView.swift @@ -162,7 +162,8 @@ struct TableListView: View { guard let driver = driver(for: action) else { return } do { let statement = SQLBuilder.truncateStatement( - table: action.table.name, schema: action.placement.schema, type: connection.type + table: action.table.name, schema: action.placement.schema, + database: action.placement.database, type: connection.type ) _ = try await coordinator.track { try await driver.executeWrite([statement]) } await coordinator.refreshTables() @@ -192,7 +193,8 @@ struct TableListView: View { guard let driver = driver(for: action) else { return } do { let quoted = SQLBuilder.qualifiedIdentifier( - table: action.table.name, schema: action.placement.schema, for: connection.type + table: action.table.name, schema: action.placement.schema, + database: action.placement.database, for: connection.type ) _ = try await coordinator.track { try await driver.executeWrite(["DROP TABLE \(quoted)"]) } await coordinator.refreshTables() @@ -226,11 +228,6 @@ struct TableListView: View { // Another window can switch the shared session between the tap and the confirmation; the table named in the dialog // may then sit in another database or schema, so nothing runs. private func driver(for action: PendingTableAction) -> (any DatabaseDriver)? { - if let refusal = coordinator.databaseMoveRefusal { - errorMessage = refusal.message - showError = true - return nil - } guard action.placement == coordinator.tablePlacement, let driver = session?.driver else { errorMessage = String(localized: "The connection moved to another database or schema. Choose the table again.") showError = true diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorCatalogTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorCatalogTests.swift index 5c5355b794..1ba67a44a7 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorCatalogTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorCatalogTests.swift @@ -386,53 +386,72 @@ struct ConnectionCoordinatorCatalogTests { editing.isHeldByEditor = false } - @Test("While a run with a USE is in flight, SQL Server writes in every window are refused and no window runs") - func useRunRefusesSQLServerWrites() async { - var mssql = connection - mssql.type = .mssql - factory.queue(driver(tables: ["orders"])) - let coordinator = ConnectionCoordinator(connection: mssql, appState: appState) + @Test("A run with a USE holds Run in every window, and the Tables tab keeps naming the database it shows", arguments: [ + DatabaseType.mssql, .mysql + ]) + func useRunHoldsOnlyRun(type: DatabaseType) async { + var record = connection + record.type = type + let session = driver(tables: ["orders"]) + factory.queue(session) + let coordinator = ConnectionCoordinator(connection: record, appState: appState) await coordinator.connect() - let otherWindow = ConnectionCoordinator(connection: mssql, appState: appState) + let otherWindow = ConnectionCoordinator(connection: record, appState: appState) await otherWindow.connect() #expect(!coordinator.beginDatabaseMove(ifRunning: "SELECT * FROM orders")) - #expect(otherWindow.databaseMoveRefusal == nil) + #expect(!otherWindow.isDatabaseMoving) #expect(coordinator.beginDatabaseMove(ifRunning: "USE archive\nDELETE FROM orders")) + session.scriptedCurrentDatabase = "archive" #expect(otherWindow.isDatabaseMoving) - #expect(otherWindow.databaseMoveRefusal != nil) + #expect(otherWindow.tableDatabase == "app_prod") + #expect(otherWindow.tablePlacement.database == "app_prod") coordinator.endDatabaseMove() #expect(!otherWindow.isDatabaseMoving) - #expect(otherWindow.databaseMoveRefusal == nil) } - @Test("MySQL writes name their database, so a run with a USE holds only the Run button") - func useRunLeavesMySQLWritesAlone() async { - let coordinator = await connected(driver(tables: ["orders"])) + @Test("A SQL Server window names the database it shows while the session sits in another, a pinned window too") + func sqlServerWindowNamesTheDatabaseItShows() async { + var mssql = connection + mssql.type = .mssql + let session = driver(tables: ["orders"]) + factory.queue(session) + let coordinator = ConnectionCoordinator(connection: mssql, appState: appState) + await coordinator.connect() + let editing = ConnectionCoordinator(connection: mssql, appState: appState) + editing.isHeldByEditor = true + session.scriptedSessionDatabase = "app_staging" + session.refusedDatabases = ["app_prod"] - #expect(coordinator.beginDatabaseMove(ifRunning: "USE app_staging")) + await coordinator.followSessionPlacement(after: .init(database: true)) - #expect(coordinator.isDatabaseMoving) - #expect(coordinator.databaseMoveRefusal == nil) - coordinator.endDatabaseMove() + #expect(coordinator.tableDatabase == "app_staging") + #expect(editing.tableDatabase == "app_prod") + #expect(editing.tablePlacement.database == "app_prod") + #expect(coordinator.failureAlertMessage == String( + format: String(localized: "The query moved the session to “%@” and it could not go back to “%@”. Unsaved changes still save to “%@”."), + "app_staging", "app_prod", "app_prod" + )) + editing.isHeldByEditor = false } - @Test("A SQL Server session left in another database refuses writes made for the one the window shows") - func sqlServerSessionElsewhereRefusesWrites() async { + @Test("A SQL Server connection naming no database names the one it opened in, even when the database list fails") + func failedDatabaseListNamesTheOpenedDatabase() async { var mssql = connection mssql.type = .mssql + mssql.database = "" let session = driver(tables: ["orders"]) + session.scriptedCurrentDatabase = "app_default" + session.fetchDatabasesError = MockDatabaseDriver.MockError.scripted factory.queue(session) let coordinator = ConnectionCoordinator(connection: mssql, appState: appState) await coordinator.connect() - session.scriptedCurrentDatabase = "APP_PROD" - #expect(coordinator.databaseMoveRefusal == nil) - - session.scriptedCurrentDatabase = "archive" - #expect(coordinator.databaseMoveRefusal != nil) + #expect(session.fetchDatabasesCalls > 0) + #expect(coordinator.activeDatabase == "app_default") + #expect(coordinator.tableDatabase == "app_default") } @Test("A COMMIT that ends a SET LOCAL search_path moves the schema picker back") diff --git a/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift b/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift index 45ef4d764d..0cd439b7fc 100644 --- a/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift +++ b/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift @@ -7,7 +7,6 @@ import TableProQuery import Testing @MainActor -@Suite("DataBrowserViewModel") struct DataBrowserViewModelTests { private func makeSession(driver: MockDatabaseDriver) -> ConnectionSession { ConnectionSession( @@ -1036,6 +1035,87 @@ struct DataBrowserViewModelTests { #expect(keyless == "SELECT * FROM [events] ORDER BY [note] ASC OFFSET 0 ROWS FETCH NEXT {limit} ROWS ONLY") } + @Test("A SQL Server page, its count and its column and key reads name the browser's database") + func sqlServerPageNamesTheDatabase() async { + let driver = MockDatabaseDriver() + driver.scriptedColumns = makeColumns() + let vm = DataBrowserViewModel() + // A full page leaves the total to the count. + driver.scriptedExecuteResults = [ + .success(numberedPage(from: 1, count: vm.pagination.pageSize)), + .success(QueryResult(columns: [], rows: [["500"]], rowsAffected: 0, executionTime: 0)) + ] + vm.attach( + session: makeSession(driver: driver), table: TableInfo(name: "orders"), databaseType: .mssql, + host: "localhost", schema: "dbo", database: "shop" + ) + await vm.load(isInitial: true) + + #expect(driver.executedQueries.first?.hasPrefix("SELECT * FROM [shop].[dbo].[orders] ORDER BY [id] ASC") == true) + #expect(driver.executedQueries.contains("SELECT COUNT(*) FROM [shop].[dbo].[orders]")) + #expect(driver.fetchColumnsDatabases == ["shop"]) + #expect(driver.fetchForeignKeysDatabases == ["shop"]) + #expect(vm.pagination.totalRows == 500) + } + + @Test("A SQL Server delete, and the read that finds its row gone, name the browser's database") + func sqlServerDeleteNamesTheDatabase() async { + let driver = MockDatabaseDriver() + driver.scriptedColumns = makeColumns() + driver.scriptedExecuteResults = [ + .success(QueryResult(columns: makeColumns(), rows: [["1", "Alice"]], rowsAffected: 0, executionTime: 0)) + ] + let vm = DataBrowserViewModel() + vm.attach( + session: makeSession(driver: driver), table: TableInfo(name: "orders"), databaseType: .mssql, + host: "localhost", schema: "dbo", database: "shop" + ) + await vm.load(isInitial: true) + // SET NOCOUNT ON reports 0, so the delete looks for the row afterwards. + driver.scriptedExecuteResults = [ + .success(QueryResult(columns: [], rows: [], rowsAffected: 0, executionTime: 0)), + .success(QueryResult(columns: [], rows: [], rowsAffected: 0, executionTime: 0)) + ] + + let deleted = await vm.deleteRow(RowLocator(row: ["1", "Alice"], keyValues: [(column: "id", value: "1")])) + + #expect(deleted) + #expect(driver.executedQueries.contains("DELETE FROM [shop].[dbo].[orders] WHERE [id] = 1")) + #expect(driver.executedQueries.contains("SELECT TOP 1 [id] FROM [shop].[dbo].[orders] WHERE [id] = 1")) + } + + @Test("A delete confirmed after the browser moved to another database is refused") + func deleteRaisedInAnotherDatabaseIsRefused() { + let session = makeSession(driver: MockDatabaseDriver()) + let vm = DataBrowserViewModel() + vm.attach( + session: session, table: TableInfo(name: "orders"), databaseType: .mssql, host: "localhost", + schema: "dbo", database: "shop" + ) + let raised = vm.rowSource + + vm.attach( + session: session, table: TableInfo(name: "orders"), databaseType: .mssql, host: "localhost", + schema: "dbo", database: "staging" + ) + + #expect(vm.refusesDelete(raisedOn: raised)) + } + + @Test("A SQL Server full value is read from the database its row came from") + func sqlServerFullValueNamesTheDatabase() async throws { + let driver = MockDatabaseDriver() + let vm = DataBrowserViewModel() + driver.scriptedExecuteResults = [.success(QueryResult(columns: [], rows: [["full"]], rowsAffected: 0, executionTime: 0))] + + let value = try await vm.loadFullValue( + driver: driver, ref: bodyRef("bio"), databaseType: .mssql, schema: "dbo", database: "shop" + ) + + #expect(value == "full") + #expect(driver.executedQueries.first?.hasPrefix("SELECT TOP 1 [bio] FROM [shop].[dbo].[users] WHERE") == true) + } + @Test("MySQL and Redshift pages keep their scan order") func scanOrderedEnginesAddNoOrder() async { #expect(await firstPageQuery(.mysql, columns: compositeKey) == "SELECT * FROM `events` LIMIT {limit} OFFSET 0") diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift b/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift index cc15f357aa..e4e1102876 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/FakeMySQLServer.swift @@ -13,10 +13,26 @@ import TableProTLSTestFixtures // statement does, or with `killedQueriesComplete` succeeds as one that finished before the kill landed. Unkilled, // or after `release`, it succeeds. A query starting with `streamsRowsStarting` returns `streamedRows` one-column rows, // cut by a kill for its session with 1317. A cleartext password passed to `refuse` is answered with 1045, as an expired -// token is. +// token is. A query in `failures` is answered with its error, after its rows when it has any. Replies carry +// SERVER_STATUS_IN_TRANS from a BEGIN or START TRANSACTION to the COMMIT or ROLLBACK that ends it. final class FakeMySQLServer: @unchecked Sendable { struct SetupFailure: Error {} + struct Failure: Sendable { + let code: UInt16 + let sqlState: String + let message: String + var rowsFirst: Int? + + // ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION. + static func readOnlyRefusal(rowsFirst: Int? = nil) -> Failure { + Failure( + code: 1_792, sqlState: "25006", message: "Cannot execute statement in a READ ONLY transaction.", + rowsFirst: rowsFirst + ) + } + } + struct Identity { let certificate: String let key: String @@ -66,7 +82,6 @@ final class FakeMySQLServer: @unchecked Sendable { private static let comQuit: UInt8 = 0x01 private static let comQuery: UInt8 = 0x03 - private static let okPacket: [UInt8] = [0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00] private static let sslRequestLength = 32 private static let switchToCleartext: [UInt8] = [0xFE] + Array("mysql_clear_password".utf8) + [0] private static let switchToSHA2: [UInt8] = [0xFE] + Array("caching_sha2_password".utf8) + [0] @@ -78,8 +93,6 @@ final class FakeMySQLServer: @unchecked Sendable { // ER_ACCESS_DENIED_ERROR (1045), SQLSTATE 28000. private static let accessDeniedPacket: [UInt8] = [0xFF, 0x15, 0x04] + Array("#28000".utf8) + Array("Access denied for user (using password: YES)".utf8) - // No warnings, SERVER_STATUS_AUTOCOMMIT. - private static let eofPacket: [UInt8] = [0xFE, 0x00, 0x00, 0x02, 0x00] // A utf8mb4 VARCHAR(255) named `n`: catalog, schema, table, org_table, name and org_name, then charset 45, length // 1020, type 0xFD, no flags or decimals. private static let textColumnDefinition: [UInt8] = ["def", "", "", "", "n", ""] @@ -96,6 +109,8 @@ final class FakeMySQLServer: @unchecked Sendable { private let killedQueriesComplete: Bool private let streamedPrefix: String? private let streamedRows: Int + private let serverVersion: String + private let failures: [String: Failure] private let fixtures: URL? private let kills = KillBoard() private let refusedPasswords = OSAllocatedUnfairLock>(initialState: []) @@ -122,7 +137,9 @@ final class FakeMySQLServer: @unchecked Sendable { holdSeconds: TimeInterval = 10, killedQueriesComplete: Bool = false, streamsRowsStarting streamedPrefix: String? = nil, - streamedRows: Int = 0 + streamedRows: Int = 0, + serverVersion: String = "8.0.36", + failures: [String: Failure] = [:] ) throws { self.asksForCleartext = asksForCleartext self.asksForFullSHA2Auth = asksForFullSHA2Auth @@ -132,6 +149,8 @@ final class FakeMySQLServer: @unchecked Sendable { self.killedQueriesComplete = killedQueriesComplete self.streamedPrefix = streamedPrefix self.streamedRows = streamedRows + self.serverVersion = serverVersion + self.failures = failures if let identity { let directory = FileManager.default.temporaryDirectory .appendingPathComponent("FakeMySQLServer-\(UUID().uuidString)", isDirectory: true) @@ -276,20 +295,28 @@ final class FakeMySQLServer: @unchecked Sendable { return } } - guard channel.send(Self.okPacket, sequence: sequence &+ 1) else { return } + guard channel.send(Self.okPacket(inTransaction: false), sequence: sequence &+ 1) else { return } session.loggedIn = true + var inTransaction = false while let command = channel.readPacket() { if command.payload.first == Self.comQuit { session.quit = true return } - var reply = Self.okPacket + var reply = Self.okPacket(inTransaction: inTransaction) if command.payload.first == Self.comQuery { let query = String(decoding: command.payload.dropFirst(), as: UTF8.self) session.queries.append(query) + inTransaction = Self.inTransaction(after: query, was: inTransaction) + reply = Self.okPacket(inTransaction: inTransaction) + if let failure = failures[query] { + guard fail(with: failure, on: channel, after: command.sequence, inTransaction: inTransaction) else { return } + continue + } if let streamedPrefix, query.hasPrefix(streamedPrefix) { - guard streamRows(on: channel, after: command.sequence, into: &session) else { return } + guard streamRows(on: channel, after: command.sequence, inTransaction: inTransaction, into: &session) + else { return } continue } if let killed = Self.killedSession(in: query) { @@ -305,13 +332,19 @@ final class FakeMySQLServer: @unchecked Sendable { // A text result set without CLIENT_DEPRECATE_EOF, which the handshake does not offer: the column count, the column, // an EOF, the rows and a closing EOF, or 1317 in place of the rest once a kill for the session arrives. - private func streamRows(on channel: ServerChannel, after sequence: UInt8, into session: inout Session) -> Bool { + private func streamRows( + on channel: ServerChannel, + after sequence: UInt8, + inTransaction: Bool, + into session: inout Session + ) -> Bool { var next = sequence func send(_ payload: [UInt8]) -> Bool { next &+= 1 return channel.send(payload, sequence: next) } - guard send([1]), send(Self.textColumnDefinition), send(Self.eofPacket) else { return false } + let eof = Self.eofPacket(inTransaction: inTransaction) + guard send([1]), send(Self.textColumnDefinition), send(eof) else { return false } for index in 0.. Bool { + var next = sequence + func send(_ payload: [UInt8]) -> Bool { + next &+= 1 + return channel.send(payload, sequence: next) + } + if let rows = failure.rowsFirst { + guard send([1]), send(Self.textColumnDefinition), send(Self.eofPacket(inTransaction: inTransaction)) else { + return false + } + for index in 0..> 8)] + Array("#\(failure.sqlState)".utf8) + return send(error + Array(failure.message.utf8)) + } + + private static func inTransaction(after query: String, was open: Bool) -> Bool { + let head = query.trimmingCharacters(in: .whitespaces).uppercased() + if head.hasPrefix("BEGIN") || head.hasPrefix("START TRANSACTION") { return true } + if head.hasPrefix("COMMIT") || head.hasPrefix("ROLLBACK") { return false } + return open + } + + // SERVER_STATUS_AUTOCOMMIT, with SERVER_STATUS_IN_TRANS inside a transaction. + private static func status(inTransaction: Bool) -> [UInt8] { + [inTransaction ? 0x03 : 0x02, 0x00] + } + + private static func okPacket(inTransaction: Bool) -> [UInt8] { + [0x00, 0x00, 0x00] + status(inTransaction: inTransaction) + [0x00, 0x00] + } + + // No warnings. + private static func eofPacket(inTransaction: Bool) -> [UInt8] { + [0xFE, 0x00, 0x00] + status(inTransaction: inTransaction) } private static func lengthEncoded(_ text: String) -> [UInt8] { @@ -341,7 +414,7 @@ final class FakeMySQLServer: @unchecked Sendable { capabilities |= Capability.ssl } var payload: [UInt8] = [10] - payload += Array("8.0.36".utf8) + [0] + payload += Array(serverVersion.utf8) + [0] payload += (0..<4).map { UInt8((connectionId >> ($0 * 8)) & 0xFF) } payload += Array("abcdefgh".utf8) + [0] payload += [UInt8(capabilities & 0xFF), UInt8((capabilities >> 8) & 0xFF)] diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/FakePostgreSQLServer.swift b/apple/TableProMobile/TableProMobileTests/Drivers/FakePostgreSQLServer.swift index 4f1d047357..f17ab72448 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/FakePostgreSQLServer.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/FakePostgreSQLServer.swift @@ -1,21 +1,36 @@ import Darwin import Foundation +import os // A loopback PostgreSQL server for one plaintext session: it refuses SSL and GSS encryption, accepts any startup without -// a password, and answers every simple query with an empty completion. +// a password, and answers a statement through the simple or the extended protocol with its scripted reply, or with an +// empty completion. It keeps the transaction status a BEGIN, START TRANSACTION, COMMIT or ROLLBACK leaves, and answers a +// cancel request on a second connection without acting on it. final class FakePostgreSQLServer: @unchecked Sendable { struct SetupFailure: Error {} + enum Reply: Sendable { + // One text column `n`, its values `row 0`, `row 1` and on. + case rows(Int) + // An ErrorResponse after `rowsFirst` rows. + case failure(sqlState: String, message: String, rowsFirst: Int = 0) + } + struct Session: Sendable { var startedUp = false + // Every statement in the order it arrived, as a simple Query or a Parse. var queries: [String] = [] + var extendedQueries: [String] = [] var terminated = false } private static let protocolVersion: UInt32 = 196_608 private static let sslRequest: UInt32 = 80_877_103 private static let gssEncryptionRequest: UInt32 = 80_877_104 + private static let cancelRequest: UInt32 = 80_877_102 private static let pollMilliseconds: Int32 = 10_000 + private static let cancelPollMilliseconds: Int32 = 100 + private static let textOid: UInt32 = 25 private static let parameters: [(String, String)] = [ ("server_version", "16.4"), ("server_encoding", "UTF8"), @@ -28,8 +43,11 @@ final class FakePostgreSQLServer: @unchecked Sendable { let port: Int private let listener: Int32 + private let replies: [String: Reply] + private let sessionEnded = OSAllocatedUnfairLock(initialState: false) - init() throws { + init(replies: [String: Reply] = [:]) throws { + self.replies = replies let socketFD = socket(AF_INET, SOCK_STREAM, 0) guard socketFD >= 0 else { throw SetupFailure() } var option: Int32 = 1 @@ -76,27 +94,106 @@ final class FakePostgreSQLServer: @unchecked Sendable { private func runSession() -> Session { var session = Session() - guard let client = acceptClient() else { return session } + guard let client = acceptClient(within: Self.pollMilliseconds) else { return session } defer { Darwin.close(client) } + // Stopped and waited for before the session returns, so it never polls a listener the test has closed. + let cancels = DispatchGroup() + cancels.enter() + DispatchQueue(label: "com.TablePro.tests.fake-postgresql.cancel").async { + self.answerCancels() + cancels.leave() + } + defer { + sessionEnded.withLock { $0 = true } + cancels.wait() + } guard startUp(client) else { return session } session.startedUp = true + var conversation = Conversation() while let type = receive(client, 1)?.first, let body = receiveBody(client) { - switch type { - case UInt8(ascii: "Q"): - session.queries.append(String(decoding: body.prefix { $0 != 0 }, as: UTF8.self)) - guard send(client, Self.message("C", Array("SELECT 0".utf8) + [0])), - send(client, Self.readyForQuery) else { return session } - case UInt8(ascii: "X"): + guard type != UInt8(ascii: "X") else { session.terminated = true return session - default: - return session } + guard let reply = conversation.answer(type, body, server: self, session: &session), + reply.isEmpty || send(client, reply) else { return session } } return session } + // libpq's PQcancel opens a connection of its own, sends the request and waits for the server to close it. + private func answerCancels() { + while !sessionEnded.withLock({ $0 }) { + var pollFD = pollfd(fd: listener, events: Int16(POLLIN), revents: 0) + let ready = poll(&pollFD, 1, Self.cancelPollMilliseconds) + guard ready >= 0, pollFD.revents & Int16(POLLNVAL) == 0 else { return } + guard ready > 0 else { continue } + let connection = accept(listener, nil, nil) + guard connection >= 0 else { continue } + _ = receiveBody(connection) + Darwin.close(connection) + } + } + + // What one session has said so far: the transaction status and, in the extended protocol, the statement parsed and + // whether an error has the rest skipped up to the Sync. + private struct Conversation { + var status = UInt8(ascii: "I") + var parsed = "" + var skippingToSync = false + + mutating func answer(_ type: UInt8, _ body: [UInt8], server: FakePostgreSQLServer, session: inout Session) -> [UInt8]? { + switch type { + case UInt8(ascii: "Q"): + let query = String(decoding: body.prefix { $0 != 0 }, as: UTF8.self) + session.queries.append(query) + return run(query, server: server, describes: true) + FakePostgreSQLServer.message("Z", [status]) + case UInt8(ascii: "P"): + let fields = body.split(separator: 0, maxSplits: 2, omittingEmptySubsequences: false) + parsed = fields.count > 1 ? String(decoding: fields[1], as: UTF8.self) : "" + session.queries.append(parsed) + session.extendedQueries.append(parsed) + return FakePostgreSQLServer.message("1", []) + case UInt8(ascii: "B"): + return skippingToSync ? [] : FakePostgreSQLServer.message("2", []) + case UInt8(ascii: "D"): + return skippingToSync ? [] : FakePostgreSQLServer.rowDescription + case UInt8(ascii: "E"): + guard !skippingToSync else { return [] } + if case .failure? = server.replies[parsed] { skippingToSync = true } + return run(parsed, server: server, describes: false) + case UInt8(ascii: "S"): + skippingToSync = false + return FakePostgreSQLServer.message("Z", [status]) + case UInt8(ascii: "H"): + return [] + default: + return nil + } + } + + private mutating func run(_ query: String, server: FakePostgreSQLServer, describes: Bool) -> [UInt8] { + let head = query.trimmingCharacters(in: .whitespaces).uppercased() + switch server.replies[query] { + case .rows(let count)?: + return (describes ? FakePostgreSQLServer.rowDescription : []) + FakePostgreSQLServer.dataRows(count) + + FakePostgreSQLServer.message("C", Array("SELECT \(count)".utf8) + [0]) + case .failure(let sqlState, let message, let rowsFirst)?: + if status == UInt8(ascii: "T") { status = UInt8(ascii: "E") } + let header = rowsFirst > 0 && describes ? FakePostgreSQLServer.rowDescription : [] + return header + FakePostgreSQLServer.dataRows(rowsFirst) + FakePostgreSQLServer.errorResponse(sqlState, message) + case nil: + if head.hasPrefix("BEGIN") || head.hasPrefix("START TRANSACTION") { + status = UInt8(ascii: "T") + } else if ["COMMIT", "ROLLBACK", "END", "ABORT"].contains(where: { head.hasPrefix($0) }) { + status = UInt8(ascii: "I") + } + return FakePostgreSQLServer.message("C", Array("SELECT 0".utf8) + [0]) + } + } + } + // The client may ask for SSL or GSS encryption first; each gets "N" and the real startup follows. private func startUp(_ client: Int32) -> Bool { while let packet = receiveBody(client), packet.count >= 4 { @@ -111,14 +208,33 @@ final class FakePostgreSQLServer: @unchecked Sendable { reply += Self.message("S", Array(name.utf8) + [0] + Array(value.utf8) + [0]) } reply += Self.message("K", Self.bytes(UInt32(4_242)) + Self.bytes(UInt32(7_777))) - reply += Self.readyForQuery + reply += Self.message("Z", [UInt8(ascii: "I")]) return send(client, reply) } return false } - private static var readyForQuery: [UInt8] { - message("Z", [UInt8(ascii: "I")]) + // A text column `n`, from no table. + private static var rowDescription: [UInt8] { + message( + "T", + bytes16(1) + Array("n".utf8) + [0] + bytes(UInt32(0)) + bytes16(0) + bytes(textOid) + bytes16(0xFFFF) + + bytes(UInt32.max) + bytes16(0) + ) + } + + private static func dataRows(_ count: Int) -> [UInt8] { + var rows: [UInt8] = [] + for index in 0.. [UInt8] { + let fields: [(Character, String)] = [("S", "ERROR"), ("V", "ERROR"), ("C", sqlState), ("M", text)] + return message("E", fields.flatMap { [$0.0.asciiValue ?? 0] + Array($0.1.utf8) + [0] } + [0]) } private static func message(_ type: Character, _ body: [UInt8]) -> [UInt8] { @@ -129,6 +245,10 @@ final class FakePostgreSQLServer: @unchecked Sendable { [UInt8((value >> 24) & 0xFF), UInt8((value >> 16) & 0xFF), UInt8((value >> 8) & 0xFF), UInt8(value & 0xFF)] } + private static func bytes16(_ value: UInt16) -> [UInt8] { + [UInt8(value >> 8), UInt8(value & 0xFF)] + } + private static func uint32(_ bytes: C) -> UInt32 where C.Element == UInt8 { bytes.reduce(0) { $0 << 8 | UInt32($1) } } @@ -141,9 +261,9 @@ final class FakePostgreSQLServer: @unchecked Sendable { return length == 4 ? [] : receive(client, length - 4) } - private func acceptClient() -> Int32? { + private func acceptClient(within milliseconds: Int32) -> Int32? { var pollFD = pollfd(fd: listener, events: Int16(POLLIN), revents: 0) - guard poll(&pollFD, 1, Self.pollMilliseconds) > 0 else { return nil } + guard poll(&pollFD, 1, milliseconds) > 0 else { return nil } let client = accept(listener, nil, nil) guard client >= 0 else { return nil } var noSignal: Int32 = 1 diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MSSQLCatalogDatabaseTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MSSQLCatalogDatabaseTests.swift new file mode 100644 index 0000000000..02996a82ff --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MSSQLCatalogDatabaseTests.swift @@ -0,0 +1,47 @@ +@testable import TableProMobile +import TableProMSSQLCore +import Testing + +struct MSSQLCatalogDatabaseTests { + @Test("A catalog read for a database runs through that database's sp_executesql, its quotes doubled") + func runsInTheDatabase() { + let statement = "SELECT name FROM sys.tables WHERE name = N'o''brien'" + + #expect(MSSQLDriver.catalogStatement(statement, in: "shop") + == "EXEC [shop].sys.sp_executesql N'SELECT name FROM sys.tables WHERE name = N''o''''brien'''") + } + + @Test("A closing bracket in the database name is doubled") + func quotesTheDatabase() { + #expect(MSSQLDriver.catalogStatement("SELECT 1", in: "shop]; DROP TABLE x --") + == "EXEC [shop]]; DROP TABLE x --].sys.sp_executesql N'SELECT 1'") + } + + @Test("No database, or an empty one, leaves the statement as it is") + func noDatabaseLeavesTheStatement() { + #expect(MSSQLDriver.catalogStatement("SELECT 1", in: nil) == "SELECT 1") + #expect(MSSQLDriver.catalogStatement("SELECT 1", in: "") == "SELECT 1") + } + + @Test("The database a window names wins, then the one the app last put the session in, then none") + func catalogTargetOrder() { + #expect(MSSQLDriver.catalogTarget(named: "shop", appDatabase: "staging") == "shop") + #expect(MSSQLDriver.catalogTarget(named: nil, appDatabase: "staging") == "staging") + #expect(MSSQLDriver.catalogTarget(named: "", appDatabase: "staging") == "staging") + #expect(MSSQLDriver.catalogTarget(named: nil, appDatabase: nil) == nil) + } + + @Test("Column, index and foreign key reads run whole inside the database, so OBJECT_ID resolves there") + func tableReadsRunInTheDatabase() { + let reads = [ + MSSQLSchemaQueries.columns(schema: "dbo", table: "orders"), + MSSQLSchemaQueries.indexes(schema: "dbo", table: "orders"), + MSSQLSchemaQueries.foreignKeys(schema: "dbo", table: "orders") + ] + + for read in reads { + #expect(MSSQLDriver.catalogStatement(read, in: "shop") + == "EXEC [shop].sys.sp_executesql \(MSSQLStringLiteral.quoted(read))") + } + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLDriverTransactionTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLDriverTransactionTests.swift index 3299ed8c72..a9f57b7381 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLDriverTransactionTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLDriverTransactionTests.swift @@ -87,6 +87,20 @@ struct MySQLSessionTransactionTests { ) } + @Test("Read-Only's own transaction reads as idle while it runs, and the flags decide once it ends") + func readOnlyTransactionReadsAsIdle() { + #expect( + MySQLSessionTransaction.state( + infoResult: 0, serverStatus: inTransaction | autocommit, holdsReadOnlyTransaction: true + ) == .idle + ) + #expect( + MySQLSessionTransaction.state( + infoResult: 0, serverStatus: inTransaction | autocommit, holdsReadOnlyTransaction: false + ) == .explicitTransaction + ) + } + @Test("A transaction the server opened because autocommit is off is implicit") func autocommitOffTransactionIsImplicit() { #expect(MySQLSessionTransaction.state(infoResult: 0, serverStatus: inTransaction) == .implicitTransaction) diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLReadOnlyRunTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLReadOnlyRunTests.swift new file mode 100644 index 0000000000..15fed7c1c7 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLReadOnlyRunTests.swift @@ -0,0 +1,224 @@ +import Foundation +import TableProDatabase +@testable import TableProMobile +import TableProModels +import Testing + +// The fake server gives the first session thread id 1; a kill arrives on a second session. +@Suite(.timeLimit(.minutes(1))) +struct MySQLReadOnlyRunTests { + private static let scan = "SELECT body FROM events" + private static let purge = "SELECT purge_events()" + private static let readOnly = StreamOptions(maxRows: 100, enforcesReadOnly: true) + + private static func wrapped(_ statement: String) -> [String] { + [MySQLReadOnlyTransaction.begin, statement, MySQLReadOnlyTransaction.end, "SELECT 2"] + } + + @Test("A Read-Only statement runs inside START TRANSACTION READ ONLY, and ROLLBACK follows its last row") + func statementRunsInReadOnlyTransaction() async throws { + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: 3) + let run = try await Self.run(on: server) { driver in + #expect(driver.enforcesReadOnlyMode) + return try await Self.read(driver.executeStreaming(query: Self.scan, options: Self.readOnly)) + } + + #expect(run.read.rows == 3) + #expect(Array(run.served.queries.suffix(4)) == Self.wrapped(Self.scan)) + } + + @Test("A session holding LOCK TABLES takes no read-only transaction, which would release the lock") + func tableLockKeepsTheReadOnlyTransactionOut() async throws { + let server = try FakeMySQLServer(identity: nil) + let run = try await Self.run(on: server) { driver in + let before = await driver.entersReadOnlyMode() + _ = try await driver.execute(query: "LOCK TABLES events READ") + let locked = await driver.entersReadOnlyMode() + _ = try await driver.execute(query: "UNLOCK TABLES") + return (before: before, locked: locked, unlocked: await driver.entersReadOnlyMode()) + } + + #expect(run.read.before) + #expect(!run.read.locked) + #expect(run.read.unlocked) + } + + @Test("A statement with no result set ends its read-only transaction too") + func noResultEndsTheTransaction() async throws { + let server = try FakeMySQLServer(identity: nil) + let run = try await Self.run(on: server) { driver in + try await Self.read(driver.executeStreaming(query: "DO 1", options: Self.readOnly)) + } + + #expect(Array(run.served.queries.suffix(4)) == Self.wrapped("DO 1")) + } + + @Test("Without Read-Only, on TiDB and on a server older than READ ONLY, the statement runs with no transaction around it", arguments: [ + (DatabaseType.mysql, "8.0.36", false), (.tidb, "8.0.11-TiDB-v8.5.1", true), (.mysql, "5.6.4-log", true), + (.mariadb, "5.5.64-MariaDB", true), + ]) + func otherRunsAreNotWrapped(type: DatabaseType, banner: String, enforcesReadOnly: Bool) async throws { + let server = try FakeMySQLServer( + identity: nil, streamsRowsStarting: Self.scan, streamedRows: 3, serverVersion: banner + ) + let options = StreamOptions(maxRows: 100, enforcesReadOnly: enforcesReadOnly) + let run = try await Self.run(on: server, databaseType: type) { driver in + try await Self.read(driver.executeStreaming(query: Self.scan, options: options)) + } + + #expect(run.read.rows == 3) + #expect(Array(run.served.queries.suffix(2)) == [Self.scan, "SELECT 2"]) + #expect(!run.served.queries.contains(MySQLReadOnlyTransaction.begin)) + } + + @Test("A statement that fails stays a query error, and its read-only transaction still ends") + func failedStatementEndsTheTransaction() async throws { + let missing = FakeMySQLServer.Failure(code: 1_146, sqlState: "42S02", message: "Table 'app.events' doesn't exist") + let server = try FakeMySQLServer(identity: nil, failures: [Self.scan: missing]) + let run = try await Self.run(on: server) { driver in + await #expect(throws: MySQLError.self) { + _ = try await Self.read(driver.executeStreaming(query: Self.scan, options: Self.readOnly)) + } + } + + #expect(Array(run.served.queries.suffix(4)) == Self.wrapped(Self.scan)) + } + + @Test("1792 is Read-Only's refusal, whether it ends the statement or its rows", arguments: [nil, 2] as [Int?]) + func refusalCodeIsReadOnlysRefusal(rowsFirst: Int?) async throws { + let server = try FakeMySQLServer( + identity: nil, failures: [Self.purge: .readOnlyRefusal(rowsFirst: rowsFirst)] + ) + let run = try await Self.run(on: server) { driver in + await #expect(throws: ReadOnlyModeRefusal.self) { + _ = try await Self.read(driver.executeStreaming(query: Self.purge, options: Self.readOnly)) + } + } + + guard case .refused(let message, let serverMessage)? = run.read else { + Issue.record("expected Read-Only's refusal, got \(String(describing: run.read))") + return + } + #expect(message == MySQLReadOnlyTransaction.refusalMessage(.safeMode)) + #expect(serverMessage.contains("Cannot execute statement in a READ ONLY transaction.")) + #expect(Array(run.served.queries.suffix(4)) == Self.wrapped(Self.purge)) + } + + @Test("A session with a transaction open is refused before anything is sent") + func openTransactionIsRefused() async throws { + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: 3) + let run = try await Self.run(on: server, closingWith: "ROLLBACK") { driver in + _ = try await driver.execute(query: "BEGIN") + return await #expect(throws: ReadOnlyModeRefusal.self) { + _ = try await Self.read(driver.executeStreaming(query: Self.scan, options: Self.readOnly)) + } + } + + #expect(run.read == .sessionNotIdle(message: MySQLReadOnlyTransaction.notIdleMessage(.safeMode))) + #expect(Array(run.served.queries.suffix(2)) == ["BEGIN", "ROLLBACK"]) + } + + @Test("The session reads as idle during and after a Read-Only read") + func streamReadsAsIdle() async throws { + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: 10_000) + let run = try await Self.run(on: server) { driver in + var midStream: DriverTransactionState? + let options = StreamOptions(maxRows: 100_000, enforcesReadOnly: true) + for try await element in driver.executeStreaming(query: Self.scan, options: options) { + guard case .row = element, midStream == nil else { continue } + midStream = await driver.sessionTransactionState() + } + return (midStream: midStream, after: await driver.sessionTransactionState()) + } + + #expect(run.read.midStream == .idle) + #expect(run.read.after == .idle) + } + + @Test("At the row cap the statement is killed and its read-only transaction ends") + func rowCapEndsTheTransaction() async throws { + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: 1_000_000) + defer { server.close() } + async let sessions = server.serveConcurrently(sessions: 2) + let driver = Self.driver(port: server.port) + try await driver.connect() + + let read = try await Self.read(driver.executeStreaming(query: Self.scan, options: Self.readOnly)) + _ = try await driver.execute(query: "SELECT 2") + try await driver.disconnect() + + let served = await sessions + #expect(read.rows == 100) + #expect(read.cappedAt == 100) + #expect(served[1].queries == ["KILL QUERY 1"]) + #expect(served[0].streamWasKilled) + #expect(Array(served[0].queries.suffix(4)) == Self.wrapped(Self.scan)) + } + + @Test("A query sent while a Read-Only read streams reaches the server after its ROLLBACK") + func replacedStreamEndsTheTransaction() async throws { + let rows = 50_000 + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: rows) + let run = try await Self.run(on: server, closingWith: nil) { driver in + var read = Read() + let options = StreamOptions(maxRows: 100_000, enforcesReadOnly: true) + for try await element in driver.executeStreaming(query: Self.scan, options: options) { + guard case .row = element else { continue } + read.rows += 1 + if read.rows == 1 { _ = try await driver.execute(query: "SELECT 2") } + } + return read + } + + #expect(Array(run.served.queries.suffix(4)) == Self.wrapped(Self.scan)) + } + + private struct Read { + var rows = 0 + var cappedAt: Int? + } + + private struct Run { + let read: Value + let served: FakeMySQLServer.Session + } + + // Connects, runs `body`, sends `closingWith` so the server shows what came after it, and disconnects. + private static func run( + on server: FakeMySQLServer, + databaseType: DatabaseType = .mysql, + closingWith closing: String? = "SELECT 2", + _ body: (MySQLDriver) async throws -> Value + ) async throws -> Run { + defer { server.close() } + async let session = server.serveOneSession() + let driver = Self.driver(port: server.port, databaseType: databaseType) + try await driver.connect() + let read = try await body(driver) + if let closing { _ = try await driver.execute(query: closing) } + try await driver.disconnect() + return Run(read: read, served: await session) + } + + private static func read(_ stream: AsyncThrowingStream) async throws -> Read { + var read = Read() + for try await element in stream { + switch element { + case .row: + read.rows += 1 + case .truncated(let reason): + if case .rowCap(let cap) = reason { read.cappedAt = cap } + default: + break + } + } + return read + } + + private static func driver(port: Int, databaseType: DatabaseType = .mysql) -> MySQLDriver { + MySQLDriver( + host: "127.0.0.1", port: port, user: "app", password: "app-password", database: "", + databaseType: databaseType + ) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLReadOnlyRunTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLReadOnlyRunTests.swift new file mode 100644 index 0000000000..02bb3d1594 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLReadOnlyRunTests.swift @@ -0,0 +1,179 @@ +import Foundation +@testable import TableProMobile +import TableProModels +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct PostgreSQLReadOnlyRunTests { + private static let report = "SELECT n FROM orders" + private static let purge = "SELECT purge_orders()" + private static let readOnly = StreamOptions(maxRows: 100, enforcesReadOnly: true) + private static let wrapped = [LibPQReadOnlyTransaction.begin, report, LibPQReadOnlyTransaction.end, "SELECT 2"] + + @Test("A Read-Only statement runs alone through the extended protocol inside a read-only transaction, then ROLLBACK") + func statementRunsInReadOnlyTransaction() async throws { + let server = try FakePostgreSQLServer(replies: [Self.report: .rows(3)]) + let run = try await Self.run(on: server) { driver in + try await Self.read(driver.executeStreaming(query: Self.report, options: Self.readOnly)) + } + + #expect(run.read.rows == 3) + #expect(Array(run.served.queries.suffix(4)) == Self.wrapped) + #expect(run.served.extendedQueries == [Self.report]) + } + + @Test("Without Read-Only, and on Redshift, the statement goes out as a simple query with no transaction around it", arguments: [ + (DatabaseType.postgresql, false), (.redshift, true), + ]) + func otherRunsAreNotWrapped(type: DatabaseType, enforcesReadOnly: Bool) async throws { + let server = try FakePostgreSQLServer(replies: [Self.report: .rows(3)]) + let options = StreamOptions(maxRows: 100, enforcesReadOnly: enforcesReadOnly) + let run = try await Self.run(on: server, databaseType: type) { driver in + try await Self.read(driver.executeStreaming(query: Self.report, options: options)) + } + + #expect(run.read.rows == 3) + #expect(Array(run.served.queries.suffix(2)) == [Self.report, "SELECT 2"]) + #expect(run.served.extendedQueries.isEmpty) + } + + @Test("A statement that fails stays a query error, and its read-only transaction still ends") + func failedStatementEndsTheTransaction() async throws { + let server = try FakePostgreSQLServer(replies: [ + Self.report: .failure(sqlState: "42P01", message: "relation \"orders\" does not exist"), + ]) + let run = try await Self.run(on: server) { driver in + await #expect(throws: PostgreSQLError.self) { + _ = try await Self.read(driver.executeStreaming(query: Self.report, options: Self.readOnly)) + } + } + + #expect(Array(run.served.queries.suffix(4)) == Self.wrapped) + } + + @Test("The server refusing a write in the read-only transaction is Read-Only's refusal, before or after rows", arguments: [ + 0, 2, + ]) + func refusalStateIsReadOnlysRefusal(rowsFirst: Int) async throws { + let server = try FakePostgreSQLServer(replies: [ + Self.purge: .failure( + sqlState: "25006", message: "cannot execute DELETE in a read-only transaction", rowsFirst: rowsFirst + ), + ]) + let run = try await Self.run(on: server) { driver in + await #expect(throws: ReadOnlyModeRefusal.self) { + _ = try await Self.read(driver.executeStreaming(query: Self.purge, options: Self.readOnly)) + } + } + + guard case .refused(let message, let serverMessage)? = run.read else { + Issue.record("expected Read-Only's refusal, got \(String(describing: run.read))") + return + } + #expect(message == LibPQReadOnlyTransaction.refusalMessage(.safeMode)) + #expect(serverMessage.contains("cannot execute DELETE in a read-only transaction")) + #expect(Array(run.served.queries.suffix(4)) == [ + LibPQReadOnlyTransaction.begin, Self.purge, LibPQReadOnlyTransaction.end, "SELECT 2", + ]) + } + + @Test("A session with a transaction open is refused before anything is sent") + func openTransactionIsRefused() async throws { + let server = try FakePostgreSQLServer(replies: [Self.report: .rows(3)]) + defer { server.close() } + async let session = server.serveOneSession() + let driver = Self.driver(port: server.port) + try await driver.connect() + + _ = try await driver.execute(query: "BEGIN") + let error = await #expect(throws: ReadOnlyModeRefusal.self) { + _ = try await Self.read(driver.executeStreaming(query: Self.report, options: Self.readOnly)) + } + _ = try await driver.execute(query: "ROLLBACK") + try await driver.disconnect() + + let served = await session + #expect(error == .sessionNotIdle(message: LibPQReadOnlyTransaction.notIdleMessage(.safeMode))) + #expect(Array(served.queries.suffix(2)) == ["BEGIN", "ROLLBACK"]) + #expect(served.extendedQueries.isEmpty) + } + + @Test("At the row cap the statement is cancelled and its read-only transaction ends") + func rowCapEndsTheTransaction() async throws { + let server = try FakePostgreSQLServer(replies: [Self.report: .rows(1_000)]) + let run = try await Self.run(on: server) { driver in + try await Self.read(driver.executeStreaming(query: Self.report, options: Self.readOnly)) + } + + #expect(run.read.rows == 100) + #expect(run.read.cappedAt == 100) + #expect(Array(run.served.queries.suffix(4)) == Self.wrapped) + } + + @Test("A query sent while a Read-Only read streams reaches the server after its ROLLBACK") + func replacedStreamEndsTheTransaction() async throws { + let rows = 50_000 + let server = try FakePostgreSQLServer(replies: [Self.report: .rows(rows)]) + let run = try await Self.run(on: server, closingWith: nil) { driver in + var read = Read() + let options = StreamOptions(maxRows: 100_000, enforcesReadOnly: true) + for try await element in driver.executeStreaming(query: Self.report, options: options) { + guard case .row = element else { continue } + read.rows += 1 + if read.rows == 1 { _ = try await driver.execute(query: "SELECT 2") } + } + return read + } + + #expect(Array(run.served.queries.suffix(4)) == Self.wrapped) + } + + private struct Read { + var rows = 0 + var cappedAt: Int? + } + + private struct Run { + let read: Value + let served: FakePostgreSQLServer.Session + } + + // Connects, runs `body`, sends `closingWith` so the server shows what came after it, and disconnects. + private static func run( + on server: FakePostgreSQLServer, + databaseType: DatabaseType = .postgresql, + closingWith closing: String? = "SELECT 2", + _ body: (PostgreSQLDriver) async throws -> Value + ) async throws -> Run { + defer { server.close() } + async let session = server.serveOneSession() + let driver = Self.driver(port: server.port, databaseType: databaseType) + try await driver.connect() + let read = try await body(driver) + if let closing { _ = try await driver.execute(query: closing) } + try await driver.disconnect() + return Run(read: read, served: await session) + } + + private static func read(_ stream: AsyncThrowingStream) async throws -> Read { + var read = Read() + for try await element in stream { + switch element { + case .row: + read.rows += 1 + case .truncated(let reason): + if case .rowCap(let cap) = reason { read.cappedAt = cap } + default: + break + } + } + return read + } + + private static func driver(port: Int, databaseType: DatabaseType = .postgresql) -> PostgreSQLDriver { + PostgreSQLDriver( + host: "127.0.0.1", port: port, user: "app", password: "app-password", database: "app", + databaseType: databaseType + ) + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLStreamTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLStreamTests.swift index 349d3758e5..405043ed68 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLStreamTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/PostgreSQLStreamTests.swift @@ -85,6 +85,37 @@ struct PostgreSQLStreamTests { #expect(drained.end == .cancelled) } + @Test("The first failure keeps its SQLSTATE") + func firstFailureKeepsItsState() { + let drained = drain([.failed("ERROR: cannot execute DELETE\n", sqlState: "25006"), .failed("ERROR: later\n", sqlState: "XX000")]) + #expect(drained.end == .failed("ERROR: cannot execute DELETE\n", sqlState: "25006")) + } + + @Test("In the read-only transaction a refusal state is Read-Only's refusal; anywhere else it is a query error", arguments: [ + "25006", "25001", + ]) + func refusalStateInReadOnlyTransaction(sqlState: String) { + let end = PostgreSQLStreamEnd.failed("ERROR: cannot execute DELETE in a read-only transaction\n", sqlState: sqlState) + let refusal = end.error(inReadOnlyTransaction: true) as? ReadOnlyModeRefusal + #expect(refusal == .refused( + message: LibPQReadOnlyTransaction.refusalMessage(.safeMode), + serverMessage: "ERROR: cannot execute DELETE in a read-only transaction\n" + )) + #expect(refusal?.errorDescription == """ + \(LibPQReadOnlyTransaction.refusalMessage(.safeMode)) + ERROR: cannot execute DELETE in a read-only transaction + """) + #expect(end.error(inReadOnlyTransaction: false) is PostgreSQLError) + #expect(end.error is PostgreSQLError) + } + + @Test("In the read-only transaction any other failure stays a query error") + func otherStateInReadOnlyTransaction() { + let end = PostgreSQLStreamEnd.failed("ERROR: relation \"orders\" does not exist\n", sqlState: "42P01") + #expect(end.error(inReadOnlyTransaction: true) is PostgreSQLError) + #expect(PostgreSQLStreamEnd.failed("ERROR: lost\n").error(inReadOnlyTransaction: true) is PostgreSQLError) + } + @Test("A failed end carries the server's message, and a cancelled end is a cancellation") func endErrors() { let failure = PostgreSQLStreamEnd.failed("ERROR: division by zero\n").error as? PostgreSQLError diff --git a/apple/TableProMobile/TableProMobileTests/ForeignKeyPreviewModelTests.swift b/apple/TableProMobile/TableProMobileTests/ForeignKeyPreviewModelTests.swift index 64201edb24..0b9aa94273 100644 --- a/apple/TableProMobile/TableProMobileTests/ForeignKeyPreviewModelTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ForeignKeyPreviewModelTests.swift @@ -79,6 +79,25 @@ struct ForeignKeyPreviewModelTests { #expect(query.contains("\"id\" = '5'")) } + @Test("On SQL Server the lookup and the referenced table's reads name the database the key was read in") + func sqlServerLookupNamesTheDatabase() async throws { + let driver = Self.driver(rows: [Self.userRow]) + let reference = ForeignKeyReference(table: "tenant_users", schema: "dbo", database: "shop", parts: [ + ForeignKeyReference.Part(column: "tenant_id", value: "2"), + ForeignKeyReference.Part(column: "id", value: "5") + ]) + let model = ForeignKeyPreviewModel(reference: reference, session: Self.session(driver), databaseType: .mssql) + await model.load() + + let query = try #require(driver.executedQueries.first) + #expect(query.hasPrefix("SELECT * FROM [shop].[dbo].[tenant_users]")) + #expect(driver.fetchColumnsDatabases == ["shop"]) + #expect(driver.fetchForeignKeysDatabases == ["shop"]) + let linked = model.fields.compactMap { $0.reference } + #expect(!linked.isEmpty) + #expect(linked.allSatisfy { $0.database == "shop" }) + } + // MySQL finds no row for `bit_col = '5'`, and the browser's filter compares the same column bare. @Test("The lookup reads the referenced table's columns first and takes each literal from their types") func lookupTakesLiteralsFromTheColumns() async throws { diff --git a/apple/TableProMobile/TableProMobileTests/ForeignKeyReferenceTests.swift b/apple/TableProMobile/TableProMobileTests/ForeignKeyReferenceTests.swift index 5071833516..2bff67f4aa 100644 --- a/apple/TableProMobile/TableProMobileTests/ForeignKeyReferenceTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ForeignKeyReferenceTests.swift @@ -35,6 +35,22 @@ struct ForeignKeyReferenceTests { #expect(reference.parts == [ForeignKeyReference.Part(column: "id", value: "5")]) } + @Test("A reference keeps the database its key was read in, from one key or from all of them") + func referenceKeepsTheSourceDatabase() throws { + let cells = Self.cells(.text("1"), .text("2"), .text("5"), .null) + let reference = try #require(ForeignKeyReference.make( + forColumn: "user_id", foreignKeys: [Self.userKey], columns: Self.orderColumns, cells: cells, + sourceSchema: "dbo", sourceDatabase: "shop" + )) + let all = ForeignKeyReference.all( + foreignKeys: [Self.userKey], columns: Self.orderColumns, cells: cells, + sourceSchema: "dbo", sourceDatabase: "shop" + ) + + #expect(reference.database == "shop") + #expect(all.map { $0.reference.database } == ["shop"]) + } + @Test("The key's own schema wins over the source schema") func referencedSchemaWins() throws { let key = ForeignKeyInfo( diff --git a/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyFunctionGuardTests.swift b/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyFunctionGuardTests.swift index e22591e53c..3e35c52a3f 100644 --- a/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyFunctionGuardTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Helpers/ReadOnlyFunctionGuardTests.swift @@ -1,15 +1,60 @@ import Foundation @testable import TableProMobile +import TableProDatabase import TableProModels import Testing struct ReadOnlyFunctionGuardTests { - @Test("Read-Only refuses a call off the built-in list on an engine whose functions can write", arguments: [ + @Test("Without the engine's read-only mode, Read-Only refuses a call off the built-in list where functions can write", arguments: [ DatabaseType.postgresql, .mysql, .mariadb, .oceanbase, .oracle, ]) - func refusesAnUnlistedCall(type: DatabaseType) { - #expect(ReadOnlyFunctionGuard.refuses("SELECT purge_orders()", databaseType: type, level: .readOnly)) - #expect(ReadOnlyFunctionGuard.refuses("SELECT app.count(id) FROM orders", databaseType: type, level: .readOnly)) + func refusesAnUnlistedCallWithoutReadOnlyMode(type: DatabaseType) { + #expect(ReadOnlyFunctionGuard.refuses( + "SELECT purge_orders()", databaseType: type, level: .readOnly, enforcesReadOnlyMode: false + )) + #expect(ReadOnlyFunctionGuard.refuses( + "SELECT app.count(id) FROM orders", databaseType: type, level: .readOnly, enforcesReadOnlyMode: false + )) + } + + @Test("In the engine's read-only mode the call runs, since the server refuses what it writes", arguments: [ + DatabaseType.postgresql, .mysql, .mariadb, + ]) + func runsAnUnlistedCallInReadOnlyMode(type: DatabaseType) { + #expect(!ReadOnlyFunctionGuard.refuses( + "SELECT purge_orders()", databaseType: type, level: .readOnly, enforcesReadOnlyMode: true + )) + } + + @Test("PostgreSQL, MySQL and MariaDB drivers hold Read-Only in the engine; Oracle, OceanBase, TiDB, Redshift and old servers do not") + func driversSayWhichRunInReadOnlyMode() throws { + let factory = IOSDriverFactory() + func mode(_ type: DatabaseType) throws -> Bool { + let connection = DatabaseConnection(name: "Shop", type: type, host: "db.example.com") + return try factory.createDriver(for: connection, password: nil).enforcesReadOnlyMode + } + #expect(try mode(.postgresql)) + #expect(try mode(.mysql)) + #expect(try mode(.mariadb)) + #expect(try !mode(.redshift)) + #expect(try !mode(.oracle)) + #expect(try !mode(.oceanbase)) + #expect(try !mode(.tidb)) + + #expect(MySQLDriver.runsReadOnlyTransactions(for: .mysql, banner: "8.4.13")) + #expect(MySQLDriver.runsReadOnlyTransactions(for: .mysql, banner: "10.11.6-MariaDB")) + #expect(!MySQLDriver.runsReadOnlyTransactions(for: .mysql, banner: "5.6.4-log")) + #expect(!MySQLDriver.runsReadOnlyTransactions(for: .mariadb, banner: "5.5.64-MariaDB")) + #expect(!MySQLDriver.runsReadOnlyTransactions(for: .mysql, banner: "8.0.11-TiDB-v8.5.1")) + #expect(!MySQLDriver.runsReadOnlyTransactions(for: .oceanbase, banner: "5.7.25-OceanBase_CE-v4.2.1.0")) + } + + @Test("A server too old for READ ONLY keeps the refusal") + func oldServerKeepsTheRefusal() { + let mode = MySQLDriver.runsReadOnlyTransactions(for: .mysql, banner: "5.6.4-log") + #expect(ReadOnlyFunctionGuard.refuses( + "SELECT purge_orders()", databaseType: .mysql, level: .readOnly, enforcesReadOnlyMode: mode + )) } @Test("Built-ins, a cast to the engine's own type and a plain EXPLAIN run", arguments: [ @@ -19,14 +64,51 @@ struct ReadOnlyFunctionGuardTests { (.oracle, "SELECT NVL(note, 'x'), TO_CHAR(created, 'YYYY') FROM orders"), ]) func runsBuiltins(type: DatabaseType, sql: String) { - #expect(!ReadOnlyFunctionGuard.refuses(sql, databaseType: type, level: .readOnly)) + #expect(!ReadOnlyFunctionGuard.refuses(sql, databaseType: type, level: .readOnly, enforcesReadOnlyMode: false)) } @Test("Only Read-Only refuses, and only where a function a query calls can write") func refusesOnlyAtReadOnlyWhereFunctionsWrite() { - #expect(!ReadOnlyFunctionGuard.refuses("SELECT purge_orders()", databaseType: .postgresql, level: .confirmWrites)) - #expect(!ReadOnlyFunctionGuard.refuses("SELECT purge_orders()", databaseType: .postgresql, level: .off)) - #expect(!ReadOnlyFunctionGuard.refuses("SELECT purge_orders()", databaseType: .mssql, level: .readOnly)) - #expect(!ReadOnlyFunctionGuard.refuses("SELECT purge_orders()", databaseType: .sqlite, level: .readOnly)) + let sql = "SELECT purge_orders()" + #expect(!ReadOnlyFunctionGuard.refuses(sql, databaseType: .postgresql, level: .confirmWrites, enforcesReadOnlyMode: false)) + #expect(!ReadOnlyFunctionGuard.refuses(sql, databaseType: .postgresql, level: .off, enforcesReadOnlyMode: false)) + #expect(!ReadOnlyFunctionGuard.refuses(sql, databaseType: .mssql, level: .readOnly, enforcesReadOnlyMode: false)) + #expect(!ReadOnlyFunctionGuard.refuses(sql, databaseType: .sqlite, level: .readOnly, enforcesReadOnlyMode: false)) + } + + @Test("A read at Read-Only on an idle session runs in the read-only mode") + func idleReadRunsInReadOnlyMode() async { + let driver = MockDatabaseDriver() + driver.enforcesReadOnlyMode = true + driver.scriptedTransactionState = .idle + + #expect(await ReadOnlyFunctionGuard.runsInReadOnlyMode(writes: false, level: .readOnly, driver: driver)) + #expect(driver.transactionStateReads == 1) + } + + @Test("A session holding a transaction, or one it cannot name, runs the read as it is", arguments: [ + DriverTransactionState.explicitTransaction, .implicitTransaction, .abortedTransaction, .unknown, + ]) + func busySessionRunsAsItIs(state: DriverTransactionState) async { + let driver = MockDatabaseDriver() + driver.enforcesReadOnlyMode = true + driver.scriptedTransactionState = state + + #expect(await !ReadOnlyFunctionGuard.runsInReadOnlyMode(writes: false, level: .readOnly, driver: driver)) + } + + @Test("A write, a level below Read-Only and a driver without the mode never ask the session") + func otherRunsNeverAskTheSession() async { + let withMode = MockDatabaseDriver() + withMode.enforcesReadOnlyMode = true + withMode.scriptedTransactionState = .idle + let withoutMode = MockDatabaseDriver() + withoutMode.scriptedTransactionState = .idle + + #expect(await !ReadOnlyFunctionGuard.runsInReadOnlyMode(writes: true, level: .readOnly, driver: withMode)) + #expect(await !ReadOnlyFunctionGuard.runsInReadOnlyMode(writes: false, level: .confirmWrites, driver: withMode)) + #expect(await !ReadOnlyFunctionGuard.runsInReadOnlyMode(writes: false, level: .readOnly, driver: withoutMode)) + #expect(withMode.transactionStateReads == 0) + #expect(withoutMode.transactionStateReads == 0) } } diff --git a/apple/TableProMobile/TableProMobileTests/Helpers/RowInsertPlannerTests.swift b/apple/TableProMobile/TableProMobileTests/Helpers/RowInsertPlannerTests.swift index 49c9af8d46..855e0d68f9 100644 --- a/apple/TableProMobile/TableProMobileTests/Helpers/RowInsertPlannerTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Helpers/RowInsertPlannerTests.swift @@ -3,7 +3,6 @@ import Testing import TableProModels @testable import TableProMobile -@Suite("RowInsertPlanner") struct RowInsertPlannerTests { private let columns = [ ColumnInfo(name: "id", typeName: "integer", isPrimaryKey: true, isNullable: false, ordinalPosition: 0), @@ -69,6 +68,18 @@ struct RowInsertPlannerTests { #expect((mysql.first ?? "").contains("1890")) } + @Test("A SQL Server insert names the window's database in front of the schema") + func sqlServerInsertNamesTheDatabase() throws { + let write = PlannedRowWrite.insert( + table: "orders", schema: "dbo", database: "shop", type: .mssql, columns: columns, + values: ["name": .text("Ada")] + ) + + let batch = try #require(try write.batch(on: ansiDriver())) + + #expect(batch.statements == ["INSERT INTO [shop].[dbo].[orders] ([name]) VALUES (N'Ada')"]) + } + @Test("writes NULL for null values") func nullValue() throws { let row = PayloadRow(values: ["name": .text("Ada"), "note": .null]) diff --git a/apple/TableProMobile/TableProMobileTests/Helpers/RowWriteRoutingTests.swift b/apple/TableProMobile/TableProMobileTests/Helpers/RowWriteRoutingTests.swift index c429dc64c7..86b2433e47 100644 --- a/apple/TableProMobile/TableProMobileTests/Helpers/RowWriteRoutingTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Helpers/RowWriteRoutingTests.swift @@ -417,6 +417,45 @@ struct RowWriteRoutingTests { #expect(plugin.generatedInserts[0] == [.text(PluginRowWritingDriver.defaultSentinel), "x"]) } + @Test("A SQL Server row save names the database the row was read in, in front of its schema") + func sqlServerSaveNamesTheDatabase() async { + let driver = MockDatabaseDriver() + driver.scriptedExecuteResults = [.success(QueryResult(columns: [], rows: [], rowsAffected: 1, executionTime: 0))] + let details = [ + ColumnInfo(name: "id", typeName: "int", isPrimaryKey: true, isNullable: false, ordinalPosition: 0), + ColumnInfo(name: "note", typeName: "nvarchar", ordinalPosition: 1) + ] + let vm = RowDetailViewModel( + columns: details, + rows: [Row(cells: [.text("1"), .text("old")])], + initialIndex: 0, + table: TableInfo(name: "orders"), + session: ConnectionSession(connectionId: UUID(), driver: driver, activeDatabase: "staging"), + columnDetails: details, + databaseType: .mssql, + schema: "dbo", + database: "shop" + ) + vm.startEditing() + vm.setEditedValue("new", at: 1) + + let saved = await vm.saveChanges() + + #expect(saved) + #expect(driver.executedQueries == ["UPDATE [shop].[dbo].[orders] SET [note] = N'new' WHERE [id] = 1"]) + } + + @Test("A SQL Server delete batch names the database in front of the schema") + func sqlServerDeleteBatchNamesTheDatabase() throws { + let details = [ColumnInfo(name: "id", typeName: "int", isPrimaryKey: true, isNullable: false, ordinalPosition: 0)] + let batch = try RowWriteBatch.delete( + table: "orders", schema: "dbo", database: "shop", type: .mssql, driver: MockDatabaseDriver(), + columns: ["id"], columnDetails: details, locator: RowLocator(row: ["1"], keyValues: [(column: "id", value: "1")]) + ) + + #expect(batch?.statements == ["DELETE FROM [shop].[dbo].[orders] WHERE [id] = 1"]) + } + // D1's plugin generates no row writes; behind a row writer every edit failed with "cannot write this change". @Test("A D1 edit, delete and insert become SQLite statements keyed on the primary key") func cloudflareD1WritesAreSQL() throws { diff --git a/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderSchemaQualificationTests.swift b/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderSchemaQualificationTests.swift index 9e57a5b478..384f0b18b9 100644 --- a/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderSchemaQualificationTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Helpers/SQLBuilderSchemaQualificationTests.swift @@ -4,10 +4,8 @@ import TableProQuery import Testing @testable import TableProMobile -/// SQL Server has no session-level schema to set, so every data statement has to name the schema -/// the toolbar shows. An unqualified one resolves against the login's default schema, which is a -/// different table of the same name. -@Suite("SQLBuilder schema qualification") +// SQL Server has no session schema to set and resolves `[schema].[table]` in the session's database, which a `USE` +// moves, so every statement names the schema and the database the window shows. struct SQLBuilderSchemaQualificationTests { private func driver() -> MockDatabaseDriver { MockDatabaseDriver() } @@ -70,6 +68,92 @@ struct SQLBuilderSchemaQualificationTests { == "SELECT COUNT(*) FROM `orders`") } + @Test("SQL Server's row writes and truncate name the window's database in front of the schema") + func sqlServerWritesNameTheDatabase() { + let key = [(column: "id", value: "1")] + #expect(SQLBuilder.buildUpdate( + table: "orders", schema: "dbo", database: "shop", type: .mssql, driver: driver(), + changes: [(column: "note", value: "x")], primaryKeys: key, columns: [] + ).hasPrefix("UPDATE [shop].[dbo].[orders] SET")) + #expect(SQLBuilder.buildDelete( + table: "orders", schema: "dbo", database: "shop", type: .mssql, driver: driver(), + primaryKeys: key, columns: [] + ).hasPrefix("DELETE FROM [shop].[dbo].[orders] WHERE")) + #expect(SQLBuilder.buildInsert( + table: "orders", schema: "dbo", database: "shop", type: .mssql, driver: driver(), + columns: ["note"], values: ["x"] + ) == "INSERT INTO [shop].[dbo].[orders] ([note]) VALUES (N'x')") + #expect(SQLBuilder.buildInsert( + table: "orders", schema: "dbo", database: "shop", type: .mssql, driver: driver(), columns: [], values: [] + ) == "INSERT INTO [shop].[dbo].[orders] DEFAULT VALUES") + #expect(SQLBuilder.truncateStatement(table: "orders", schema: "dbo", database: "shop", type: .mssql) + == "TRUNCATE TABLE [shop].[dbo].[orders]") + } + + @Test("SQL Server's browse, count, filter, search and lookup reads name the window's database") + func sqlServerReadsNameTheDatabase() { + let columns = [ColumnInfo(name: "note", typeName: "nvarchar", ordinalPosition: 0)] + let sorted = SortState(columns: [SortColumn(name: "note", ascending: true)]) + let table = "SELECT * FROM [shop].[dbo].[orders] " + #expect(SQLBuilder.buildSelect(table: "orders", schema: "dbo", database: "shop", type: .mssql, limit: 10, offset: 0) + .hasPrefix(table)) + #expect(SQLBuilder.buildSelect( + table: "orders", schema: "dbo", database: "shop", type: .mssql, sortState: sorted, limit: 10, offset: 0 + ).hasPrefix(table)) + #expect(SQLBuilder.buildCount(table: "orders", schema: "dbo", database: "shop", type: .mssql) + == "SELECT COUNT(*) FROM [shop].[dbo].[orders]") + #expect(SQLBuilder.buildFilteredSelect( + table: "orders", schema: "dbo", database: "shop", type: .mssql, + filters: [], logicMode: .and, limit: 10, offset: 0 + ).hasPrefix(table)) + #expect(SQLBuilder.buildFilteredCount( + table: "orders", schema: "dbo", database: "shop", type: .mssql, filters: [], logicMode: .and + ) == "SELECT COUNT(*) FROM [shop].[dbo].[orders]") + #expect(SQLBuilder.buildSearchSelect( + table: "orders", schema: "dbo", database: "shop", type: .mssql, + searchText: "a", searchColumns: columns, limit: 10, offset: 0 + ).hasPrefix(table)) + #expect(SQLBuilder.buildSearchCount( + table: "orders", schema: "dbo", database: "shop", type: .mssql, searchText: "a", searchColumns: columns + ).hasPrefix("SELECT COUNT(*) FROM [shop].[dbo].[orders] WHERE")) + #expect(SQLBuilder.buildSingleRowLookup( + table: "orders", schema: "dbo", database: "shop", key: [(column: "id", value: "1")], columns: [], + type: .mssql, driver: driver() + ).hasPrefix("SELECT * FROM [shop].[dbo].[orders] WHERE")) + } + + @Test("SQL Server with a database and no schema reads the database's default schema") + func sqlServerDatabaseWithoutSchema() { + #expect(SQLBuilder.qualifiedIdentifier(table: "orders", schema: nil, database: "shop", for: .mssql) + == "[shop]..[orders]") + #expect(SQLBuilder.qualifiedIdentifier(table: "orders", schema: "", database: "shop", for: .mssql) + == "[shop]..[orders]") + #expect(SQLBuilder.qualifiedIdentifier(table: "orders", schema: "dbo", database: "", for: .mssql) + == "[dbo].[orders]") + #expect(SQLBuilder.qualifiedIdentifier(table: "orders", schema: "dbo", database: nil, for: .mssql) + == "[dbo].[orders]") + } + + @Test("A closing bracket in a SQL Server database name is doubled") + func sqlServerQuotesTheDatabase() { + #expect(SQLBuilder.qualifiedIdentifier(table: "orders", schema: "dbo", database: "shop]; DROP TABLE x --", for: .mssql) + == "[shop]]; DROP TABLE x --].[dbo].[orders]") + } + + @Test("MySQL and PostgreSQL ignore the database: MySQL's sits in the schema slot, PostgreSQL has no third part") + func otherEnginesIgnoreTheDatabase() { + #expect(SQLBuilder.qualifiedIdentifier(table: "orders", schema: "shop", database: "shop", for: .mysql) + == "`shop`.`orders`") + #expect(SQLBuilder.qualifiedIdentifier(table: "orders", schema: "public", database: "shop", for: .postgresql) + == "\"public\".\"orders\"") + #expect(SQLBuilder.buildDelete( + table: "orders", schema: "shop", database: "staging", type: .mysql, driver: driver(), + primaryKeys: [(column: "id", value: "1")], columns: [] + ).hasPrefix("DELETE FROM `shop`.`orders` WHERE")) + #expect(SQLBuilder.truncateStatement(table: "orders", schema: "public", database: "shop", type: .postgresql) + == "TRUNCATE TABLE \"public\".\"orders\"") + } + @Test("the qualifier follows each dialect's own quoting") func dialectQuoting() { #expect(SQLBuilder.qualifiedIdentifier(table: "orders", schema: "sales", for: .postgresql) diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/MockDatabaseDriver.swift b/apple/TableProMobile/TableProMobileTests/Mocks/MockDatabaseDriver.swift index b0113e8490..4f6120180f 100644 --- a/apple/TableProMobile/TableProMobileTests/Mocks/MockDatabaseDriver.swift +++ b/apple/TableProMobile/TableProMobileTests/Mocks/MockDatabaseDriver.swift @@ -31,6 +31,7 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { var connectError: Error? var switchDatabaseError: Error? var fetchTablesError: Error? + var fetchDatabasesError: Error? var commitError: Error? var rollbackError: Error? @@ -39,6 +40,8 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { private(set) var fetchForeignKeysCalls: Int = 0 private(set) var fetchColumnsSchemas: [String?] = [] private(set) var fetchForeignKeysSchemas: [String?] = [] + private(set) var fetchColumnsDatabases: [String?] = [] + private(set) var fetchForeignKeysDatabases: [String?] = [] private(set) var beganTransactionModes: [PluginTransactionAccessMode] = [] private(set) var didBeginTransaction = false private(set) var didCommitTransaction = false @@ -58,6 +61,7 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { var holdsSuspensionBlockingResource: Bool = false var sendsBatchesWhole: Bool = false var usesBackslashEscaping: Bool = false + var enforcesReadOnlyMode: Bool = false func escapeStringLiteral(_ value: String) -> String { usesBackslashEscaping @@ -107,8 +111,13 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { } func fetchColumns(table: String, schema: String?) async throws -> [ColumnInfo] { + try await fetchColumns(table: table, schema: schema, database: nil) + } + + func fetchColumns(table: String, schema: String?, database: String?) async throws -> [ColumnInfo] { fetchColumnsCalls += 1 fetchColumnsSchemas.append(schema) + fetchColumnsDatabases.append(database) if let fetchColumnsError { throw fetchColumnsError } return scriptedColumns } @@ -116,14 +125,20 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { func fetchIndexes(table: String, schema: String?) async throws -> [IndexInfo] { [] } func fetchForeignKeys(table: String, schema: String?) async throws -> [ForeignKeyInfo] { + try await fetchForeignKeys(table: table, schema: schema, database: nil) + } + + func fetchForeignKeys(table: String, schema: String?, database: String?) async throws -> [ForeignKeyInfo] { fetchForeignKeysCalls += 1 fetchForeignKeysSchemas.append(schema) + fetchForeignKeysDatabases.append(database) if let fetchForeignKeysError { throw fetchForeignKeysError } return scriptedForeignKeys } func fetchDatabases() async throws -> [String] { fetchDatabasesCalls += 1 + if let fetchDatabasesError { throw fetchDatabasesError } return scriptedDatabases } diff --git a/apple/TableProMobile/TableProMobileTests/QueryEditorViewModelTests.swift b/apple/TableProMobile/TableProMobileTests/QueryEditorViewModelTests.swift index fa118350e7..49c61a02c0 100644 --- a/apple/TableProMobile/TableProMobileTests/QueryEditorViewModelTests.swift +++ b/apple/TableProMobile/TableProMobileTests/QueryEditorViewModelTests.swift @@ -495,6 +495,20 @@ struct QueryEditorViewModelTests { #expect(!QueryEditorViewModel.resultsFitExport(rows: rows, availableBytes: margin + 40_000)) } + @Test("a Read-Only run asks every request for the engine's read-only mode, and any other run asks none") + func readOnlyReachesEveryRequest() async { + let driver = StreamOptionsRecorder() + let vm = QueryEditorViewModel(windowCapacity: 100) + + await vm.run(driver: driver, script: .statements("SELECT 1", "SELECT 2"), enforcesReadOnly: true) + let readOnlyRun = driver.options.map { $0.enforcesReadOnly } + await vm.run(driver: driver, script: .statements("SELECT 3")) + let plainRun = driver.options.dropFirst(2).map { $0.enforcesReadOnly } + + #expect(readOnlyRun == [true, true]) + #expect(plainRun == [false]) + } + @Test("the budget is a quarter of the headroom past 64 MB, at most 64 MB, and 64 MB when the headroom is unknown") func textBudgetFollowsTheHeadroom() { let megabyte = 1_024 * 1_024 @@ -515,6 +529,40 @@ private extension QueryScript { } } +// Keeps the options each request streams with and answers every one with nothing. +private final class StreamOptionsRecorder: DatabaseDriver, @unchecked Sendable { + private(set) var options: [StreamOptions] = [] + + var supportsSchemas: Bool { false } + var currentSchema: String? { nil } + var supportsTransactions: Bool { true } + var serverVersion: String? { nil } + + func executeStreaming(query: String, options: StreamOptions) -> AsyncThrowingStream { + self.options.append(options) + return AsyncThrowingStream { $0.finish() } + } + + func connect() async throws {} + func disconnect() async throws {} + func ping() async throws -> Bool { true } + func execute(query: String) async throws -> QueryResult { + QueryResult(columns: [], rows: [], rowsAffected: 0, executionTime: 0) + } + func cancelCurrentQuery() async throws {} + func fetchTables(schema: String?) async throws -> [TableInfo] { [] } + func fetchColumns(table: String, schema: String?) async throws -> [ColumnInfo] { [] } + func fetchIndexes(table: String, schema: String?) async throws -> [IndexInfo] { [] } + func fetchForeignKeys(table: String, schema: String?) async throws -> [ForeignKeyInfo] { [] } + func fetchDatabases() async throws -> [String] { [] } + func switchDatabase(to name: String) async throws {} + func switchSchema(to name: String) async throws {} + func fetchSchemas() async throws -> [String] { [] } + func beginTransaction() async throws {} + func commitTransaction() async throws {} + func rollbackTransaction() async throws {} +} + private actor QueryGate { private var continuation: CheckedContinuation? private var isOpen = false diff --git a/apple/TableProMobile/project.yml b/apple/TableProMobile/project.yml index 27996ea3a8..d1671a8228 100644 --- a/apple/TableProMobile/project.yml +++ b/apple/TableProMobile/project.yml @@ -67,6 +67,13 @@ targets: - ../Plugins/MySQLDriverPlugin/MySQLErrorText.swift - ../Plugins/MySQLDriverPlugin/MySQLForeignKeyClause.swift - ../Plugins/MySQLDriverPlugin/MySQLServerFlavor.swift + # The read-only transaction statements, refusal codes and messages both apps run a Read-Only statement with. + - ../Plugins/MySQLDriverPlugin/MySQLReadOnlyTransaction.swift + - ../Plugins/PostgreSQLDriverPlugin/LibPQReadOnlyTransaction.swift + # What a MySQL session holds that the read-only transaction would end, its table locks among them. + - ../Plugins/MySQLDriverPlugin/MySQLSessionFootprint.swift + - ../Plugins/MySQLDriverPlugin/MySQLTemporaryTableNames.swift + - ../Plugins/MySQLDriverPlugin/MySQLLexicalFeatures.swift # The socket key a Mac connection stores, so iOS refuses exactly what the Mac driver dials # through a socket. - ../Plugins/MySQLDriverPlugin/MySQLLocalSocket.swift From fad62aa297c8aad3c209d4338ead370ba3adbfbc Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 08:07:12 +0700 Subject: [PATCH 2/3] docs(changelog): add D-8 and D-11 fragments --- apple/changelog.d/4257.changed.md | 1 + apple/changelog.d/4257.fixed.md | 1 + 2 files changed, 2 insertions(+) create mode 100644 apple/changelog.d/4257.changed.md create mode 100644 apple/changelog.d/4257.fixed.md diff --git a/apple/changelog.d/4257.changed.md b/apple/changelog.d/4257.changed.md new file mode 100644 index 0000000000..e118becfd8 --- /dev/null +++ b/apple/changelog.d/4257.changed.md @@ -0,0 +1 @@ +On iOS, Read-Only runs PostgreSQL, MySQL and MariaDB queries in a read-only transaction, so function calls run. diff --git a/apple/changelog.d/4257.fixed.md b/apple/changelog.d/4257.fixed.md new file mode 100644 index 0000000000..341f841dcb --- /dev/null +++ b/apple/changelog.d/4257.fixed.md @@ -0,0 +1 @@ +On iOS, SQL Server saves, deletes, truncates and drops stopped while a USE run had moved the session elsewhere. From 132d55cf269c3fc5ed4d7fe361d71e705259a4fd Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 08:12:17 +0700 Subject: [PATCH 3/3] test(ios): refuse a Read-Only read under LOCK TABLES in the driver, and keep one changelog entry --- .../Drivers/MySQLReadOnlyRunTests.swift | 14 ++++++++++++++ apple/changelog.d/4257.changed.md | 2 +- apple/changelog.d/4257.fixed.md | 1 - 3 files changed, 15 insertions(+), 2 deletions(-) delete mode 100644 apple/changelog.d/4257.fixed.md diff --git a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLReadOnlyRunTests.swift b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLReadOnlyRunTests.swift index 15fed7c1c7..d86d63178a 100644 --- a/apple/TableProMobile/TableProMobileTests/Drivers/MySQLReadOnlyRunTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Drivers/MySQLReadOnlyRunTests.swift @@ -118,6 +118,20 @@ struct MySQLReadOnlyRunTests { #expect(Array(run.served.queries.suffix(2)) == ["BEGIN", "ROLLBACK"]) } + @Test("A Read-Only read on a session holding LOCK TABLES is refused before the read-only transaction goes out") + func tableLockIsRefusedByTheDriver() async throws { + let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: 3) + let run = try await Self.run(on: server, closingWith: "UNLOCK TABLES") { driver in + _ = try await driver.execute(query: "LOCK TABLES events READ") + return await #expect(throws: ReadOnlyModeRefusal.self) { + _ = try await Self.read(driver.executeStreaming(query: Self.scan, options: Self.readOnly)) + } + } + + #expect(run.read == .sessionNotIdle(message: MySQLReadOnlyTransaction.notIdleMessage(.safeMode))) + #expect(Array(run.served.queries.suffix(2)) == ["LOCK TABLES events READ", "UNLOCK TABLES"]) + } + @Test("The session reads as idle during and after a Read-Only read") func streamReadsAsIdle() async throws { let server = try FakeMySQLServer(identity: nil, streamsRowsStarting: Self.scan, streamedRows: 10_000) diff --git a/apple/changelog.d/4257.changed.md b/apple/changelog.d/4257.changed.md index e118becfd8..4fb4eef962 100644 --- a/apple/changelog.d/4257.changed.md +++ b/apple/changelog.d/4257.changed.md @@ -1 +1 @@ -On iOS, Read-Only runs PostgreSQL, MySQL and MariaDB queries in a read-only transaction, so function calls run. +iOS Read-Only runs PostgreSQL, MySQL and MariaDB queries in a read-only transaction, so the server refuses their writes. diff --git a/apple/changelog.d/4257.fixed.md b/apple/changelog.d/4257.fixed.md deleted file mode 100644 index 341f841dcb..0000000000 --- a/apple/changelog.d/4257.fixed.md +++ /dev/null @@ -1 +0,0 @@ -On iOS, SQL Server saves, deletes, truncates and drops stopped while a USE run had moved the session elsewhere.