diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowWriteTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowWriteTests.swift new file mode 100644 index 0000000000..0954525b55 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowWriteTests.swift @@ -0,0 +1,29 @@ +import Foundation +import TableProPluginKit +import Testing + +struct PluginRowWriteTests { + @Test("A write built without a count leaves it to the host") + func countDefaultsToNil() { + let lookup = PluginRowLookup(query: "SELECT 1", parameters: [], rowCount: 1, pinsRows: false) + + #expect(PluginRowWrite(statement: "DELETE", rowIndices: [0]).expectedRowCount == nil) + #expect(PluginRowWrite(statement: "DELETE", rowIndices: [0], lookup: lookup).expectedRowCount == nil) + } + + @Test("A write keeps the count and the lookup its driver gave it") + func keepsTheDriversCount() { + let lookup = PluginRowLookup(query: "SELECT 1", parameters: [], rowCount: 1, pinsRows: false) + let write = PluginRowWrite( + statement: "UPDATE t SET a = ? WHERE id = ?", + parameters: [.text("1"), .text("2")], + rowIndices: [3], + lookup: lookup, + expectedRowCount: 1 + ) + + #expect(write.expectedRowCount == 1) + #expect(write.lookup == lookup) + #expect(write.rowIndices == [3]) + } +} diff --git a/apple/Plugins/DamengDriverPlugin/DamengPluginDriver+Editing.swift b/apple/Plugins/DamengDriverPlugin/DamengPluginDriver+Editing.swift index f06963b60f..f0c495b41e 100644 --- a/apple/Plugins/DamengDriverPlugin/DamengPluginDriver+Editing.swift +++ b/apple/Plugins/DamengDriverPlugin/DamengPluginDriver+Editing.swift @@ -36,11 +36,13 @@ extension DamengPluginDriver { parameters.append(cell.newValue) return "\(column) = ?" } + // Measured on DM8: the count takes in a row already holding the value and leaves out rows a trigger writes. writes.append(PluginRowWrite( statement: "UPDATE \(qualifiedTable) SET \(assignments.joined(separator: ", ")) WHERE \(match.predicate) AND ROWNUM = 1", parameters: parameters + match.parameters, rowIndices: [change.rowIndex], - lookup: match.lookup + lookup: match.lookup, + expectedRowCount: 1 )) case .delete: guard deletedRowIndices.contains(change.rowIndex) else { continue } @@ -49,7 +51,8 @@ extension DamengPluginDriver { statement: "DELETE FROM \(qualifiedTable) WHERE \(match.predicate) AND ROWNUM = 1", parameters: match.parameters, rowIndices: [change.rowIndex], - lookup: match.lookup + lookup: match.lookup, + expectedRowCount: 1 )) } } diff --git a/apple/Plugins/MSSQLDriverPlugin/MSSQLStatementGenerator.swift b/apple/Plugins/MSSQLDriverPlugin/MSSQLStatementGenerator.swift index 590bb87789..e6a7e184fc 100644 --- a/apple/Plugins/MSSQLDriverPlugin/MSSQLStatementGenerator.swift +++ b/apple/Plugins/MSSQLDriverPlugin/MSSQLStatementGenerator.swift @@ -41,11 +41,21 @@ struct MSSQLStatementGenerator { writes.append(PluginRowWrite(statement: statement.statement, parameters: statement.parameters, rowIndices: [change.rowIndex])) case .update: guard let statement = try updateStatement(for: change) else { continue } - writes.append(PluginRowWrite(statement: statement.statement, parameters: statement.parameters, rowIndices: [change.rowIndex])) + writes.append(PluginRowWrite( + statement: statement.statement, + parameters: statement.parameters, + rowIndices: [change.rowIndex], + expectedRowCount: 1 + )) case .delete: guard deletedRowIndices.contains(change.rowIndex), let statement = try deleteStatement(for: change) else { continue } - writes.append(PluginRowWrite(statement: statement.statement, parameters: statement.parameters, rowIndices: [change.rowIndex])) + writes.append(PluginRowWrite( + statement: statement.statement, + parameters: statement.parameters, + rowIndices: [change.rowIndex], + expectedRowCount: 1 + )) } } return writes @@ -133,11 +143,12 @@ struct MSSQLStatementGenerator { primaryKeyColumns.isEmpty ? "TOP (1) " : "" } - /// The app holds a keyless write to the row it matched, and under `SET NOCOUNT ON` the server reports no count. - /// A SET inside `sp_executesql` reverts when the call returns. A bound statement already runs in one, so only an - /// unbound one, a row whose every column is NULL, is given its own. + /// The app holds every update and delete to the row it matched, and under `SET NOCOUNT ON` the server reports no + /// count. A SET inside `sp_executesql` reverts when the call returns. A bound statement already runs in one, so + /// only an unbound one, such as a keyless row whose every column is NULL, is given its own. Measured on SQL Server + /// 2022: the count is the rows matched, a row already holding the value included, and leaves out rows a trigger + /// writes (`scripts/probes/check-mssql-keyed-row-counts.sh`). private func countReported(_ sql: String, parameters: [PluginCellValue]) -> String { - guard primaryKeyColumns.isEmpty else { return sql } let counted = "SET NOCOUNT OFF; \(sql)" guard parameters.isEmpty else { return counted } return "EXEC sp_executesql \(MSSQLStringLiteral.quoted(counted))" diff --git a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection+Transport.swift b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection+Transport.swift index dc1c4b5612..847f4bf2b9 100644 --- a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection+Transport.swift +++ b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection+Transport.swift @@ -39,6 +39,10 @@ enum MySQLTransport: Sendable, Equatable { mysql_options(handle, MYSQL_OPT_PROTOCOL, &value) } + // An UPDATE then reports the rows it matched, not only those whose value changed, so a save can tell a row that + // already held the value from one that is gone. + static let clientFlags = UInt(CLIENT_FOUND_ROWS) + /// Connector/C takes the socket path only when the host is NULL or exactly `localhost`. func realConnect( _ handle: UnsafeMutablePointer, @@ -48,14 +52,14 @@ enum MySQLTransport: Sendable, Equatable { ) -> UnsafeMutablePointer? { switch self { case .tcp(let host, let port): - let connected = mysql_real_connect(handle, host, user, password, database, port, nil, 0) + let connected = mysql_real_connect(handle, host, user, password, database, port, nil, Self.clientFlags) // Every session and kill connection dials through here, and a session's socket outlives the connect. if connected != nil, !MySQLSocketKeepalive.enable(on: mysql_get_socket(handle)) { Self.logger.warning("TCP keepalive could not be set: errno \(errno, privacy: .public)") } return connected case .unixSocket(let path): - return mysql_real_connect(handle, "localhost", user, password, database, 0, path, 0) + return mysql_real_connect(handle, "localhost", user, password, database, 0, path, Self.clientFlags) } } } diff --git a/apple/Plugins/MySQLDriverTests/MySQLFoundRowsTests.swift b/apple/Plugins/MySQLDriverTests/MySQLFoundRowsTests.swift new file mode 100644 index 0000000000..ccd89fa473 --- /dev/null +++ b/apple/Plugins/MySQLDriverTests/MySQLFoundRowsTests.swift @@ -0,0 +1,33 @@ +import CMariaDB +import Foundation +import TableProPluginKit +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct MySQLFoundRowsTests { + // A save holds a keyed UPDATE to the one row it matched, which only reads right when the server counts a row + // that already held the value: scripts/probes/check-mysql-found-rows.sh measures that against real servers. + @Test("A session signs in asking for CLIENT_FOUND_ROWS, so an UPDATE counts the rows it matched") + func signInAsksForFoundRows() async throws { + let server = try PlaintextMySQLServer(asksForCleartext: false) + defer { server.close() } + async let session = server.serveOneSession() + let connection = MariaDBPluginConnection( + transport: .tcp(host: "127.0.0.1", port: UInt32(server.port)), + user: "app", + password: "app-password", + database: "", + sslConfig: SSLConfiguration(mode: .disabled), + enableCleartextPlugin: false + ) + + try await connection.connect() + connection.disconnect() + let received = await session.received + + // The first packet is the sign-in: a 4-byte header, then the client's capability flags, little-endian. + try #require(received.count >= 8) + let flags = received[4..<8].enumerated().reduce(UInt32(0)) { $0 | UInt32($1.element) << (8 * $1.offset) } + #expect(flags & UInt32(CLIENT_FOUND_ROWS) != 0) + } +} diff --git a/apple/Plugins/OracleDriverPlugin/OracleRowWriter.swift b/apple/Plugins/OracleDriverPlugin/OracleRowWriter.swift index 6b82456d53..14ee2761f0 100644 --- a/apple/Plugins/OracleDriverPlugin/OracleRowWriter.swift +++ b/apple/Plugins/OracleDriverPlugin/OracleRowWriter.swift @@ -11,7 +11,8 @@ import TableProSQLQuoting /// /// A keyed row is found by its primary key alone. A keyless row is found by every column it has, bounded with /// `ROWNUM = 1` so a save never touches a second row that looks the same, and a change whose match would need a -/// column that cannot be compared is refused rather than sent without it. +/// column that cannot be compared is refused rather than sent without it. An update or delete reports the rows it +/// matches: measured on 23ai, the count takes in a row already holding the value and leaves out rows a trigger writes. /// /// Every value is written for the type the host reports for its column, so nothing depends on the session's NLS /// settings. Measured on 23ai: a DATE or TIMESTAMP value is converted with an explicit mask, because the same text @@ -134,7 +135,7 @@ internal struct OracleRowWriter { let match = try rowMatch(for: change, parameters: ¶meters) let bound = primaryKeyColumns.isEmpty ? " AND ROWNUM = 1" : "" let statement = "UPDATE \(qualifiedTable) SET \(assignments.joined(separator: ", ")) WHERE \(match)\(bound)" - return PluginRowWrite(statement: statement, parameters: parameters, rowIndices: [change.rowIndex]) + return PluginRowWrite(statement: statement, parameters: parameters, rowIndices: [change.rowIndex], expectedRowCount: 1) } private func deleteWrites(for changes: [PluginRowChange]) throws -> [PluginRowWrite] { @@ -145,7 +146,8 @@ internal struct OracleRowWriter { return PluginRowWrite( statement: "DELETE FROM \(qualifiedTable) WHERE \(match) AND ROWNUM = 1", parameters: parameters, - rowIndices: [change.rowIndex] + rowIndices: [change.rowIndex], + expectedRowCount: 1 ) } } @@ -160,7 +162,8 @@ internal struct OracleRowWriter { return PluginRowWrite( statement: "DELETE FROM \(qualifiedTable) WHERE \(matches.joined(separator: " OR "))", parameters: parameters, - rowIndices: chunk.map(\.rowIndex) + rowIndices: chunk.map(\.rowIndex), + expectedRowCount: chunk.count ) } } diff --git a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeStatementGenerator.swift b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeStatementGenerator.swift index 1d72ede5f9..8f1c5a4ea7 100644 --- a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeStatementGenerator.swift +++ b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeStatementGenerator.swift @@ -18,7 +18,7 @@ struct SnowflakeStatementGenerator { let primaryKeyColumns: [String] // Snowflake does not enforce a primary key, so a key match can reach several rows too: every update and delete - // first has to find exactly one. + // first has to find exactly one, and is then held to it by the rows-updated or rows-deleted count it reports. func rowWrites( from changes: [PluginRowChange], insertedRowData: [Int: [PluginCellValue]], @@ -51,7 +51,8 @@ struct SnowflakeStatementGenerator { statement: "UPDATE \(qualifiedTable) SET \(setClauses.joined(separator: ", ")) WHERE \(condition.sql)", parameters: parameters + condition.parameters, rowIndices: [change.rowIndex], - lookup: lookup(condition) + lookup: lookup(condition), + expectedRowCount: 1 )) case .delete: guard deletedRowIndices.contains(change.rowIndex) else { continue } @@ -60,7 +61,8 @@ struct SnowflakeStatementGenerator { statement: "DELETE FROM \(qualifiedTable) WHERE \(condition.sql)", parameters: condition.parameters, rowIndices: [change.rowIndex], - lookup: lookup(condition) + lookup: lookup(condition), + expectedRowCount: 1 )) @unknown default: continue diff --git a/apple/Plugins/TableProPluginKit/PluginRowWrite.swift b/apple/Plugins/TableProPluginKit/PluginRowWrite.swift index 7e85f6a811..e31c7b5250 100644 --- a/apple/Plugins/TableProPluginKit/PluginRowWrite.swift +++ b/apple/Plugins/TableProPluginKit/PluginRowWrite.swift @@ -19,17 +19,32 @@ public struct PluginRowWrite: Sendable { public let parameters: [PluginCellValue] public let rowIndices: [Int] public let lookup: PluginRowLookup? + // Set only when the engine's count for this statement is the rows it matched, a row that already held the value + // included. The host then refuses the save when the count is higher, or lower for an UPDATE, and rolls it back. + public let expectedRowCount: Int? @_disfavoredOverload public init(statement: String, parameters: [PluginCellValue] = [], rowIndices: [Int]) { - self.init(statement: statement, parameters: parameters, rowIndices: rowIndices, lookup: nil) + self.init(statement: statement, parameters: parameters, rowIndices: rowIndices, lookup: nil, expectedRowCount: nil) } + @_disfavoredOverload public init(statement: String, parameters: [PluginCellValue] = [], rowIndices: [Int], lookup: PluginRowLookup? = nil) { + self.init(statement: statement, parameters: parameters, rowIndices: rowIndices, lookup: lookup, expectedRowCount: nil) + } + + public init( + statement: String, + parameters: [PluginCellValue] = [], + rowIndices: [Int], + lookup: PluginRowLookup? = nil, + expectedRowCount: Int? = nil + ) { self.statement = statement self.parameters = parameters self.rowIndices = rowIndices self.lookup = lookup + self.expectedRowCount = expectedRowCount } } diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift index fca7c1b6b6..408ed53bfa 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift @@ -12,7 +12,7 @@ extension RowEditingCoordinator { // MARK: - Sidebar Transaction // Scope is read before the authorization prompt (sheet and Touch ID), which gives the selection time to move. - func executeSidebarChanges(steps: [DataWriteStep]) async throws { + func executeSidebarChanges(steps: [DataWriteStep], viewTarget: PluginViewWriteTarget?) async throws { guard let scope = parent.selectedTabScope else { throw DatabaseError.notConnected } @@ -35,7 +35,9 @@ extension RowEditingCoordinator { } let mode: PluginTransactionAccessMode = kind.declaresWrite ? .readWrite : .serverDefault - let plan = DataWritePlan(scope: scope, databaseType: parent.connection.type, steps: steps) + var built = DataWritePlan(scope: scope, databaseType: parent.connection.type, steps: steps) + built.viewTarget = viewTarget + let plan = built let commitMode = DatabaseManager.shared.commitMode(for: parent.connectionId) _ = try await DatabaseManager.shared.withScopedDriver( scope: scope, diff --git a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift index 1bc2d5077c..8e581d55e3 100644 --- a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift +++ b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift @@ -11,16 +11,16 @@ // affected count against the count the plan expected is what catches it, and inside a transaction // it is caught before it lands. // -// For a keyed write the comparison is `actual > expected`. MySQL reports zero affected rows for an -// UPDATE that writes a value a row already holds, which is a normal save, not a fault. Where the -// count is the rows the UPDATE matched, a short one means the row was not written: deleted or +// For a keyed write the comparison is `actual > expected`, and for a keyed UPDATE also `actual < +// expected` where the count is the rows it matched (`DataWriteRowCounts`, or a driver's own +// `PluginRowWrite.expectedRowCount`): a short one means the row was not written, deleted or // rekeyed since it was read, kept out by row-level security, or skipped by a trigger. // // A keyless write is held to both ends. It finds its row by matching every column against the text // the grid read, and that match can simply find nothing: measured on MySQL 8.4.11 and OceanBase // 4.4.2.1, a row holding a FLOAT or a JSON column matched zero rows, the statement wrote nothing, // and the save reported success while the edit disappeared on the next reload. Only the engines -// whose drivers report a real count can be held to it, which `reportsRowsAffected` decides. +// whose drivers report a real count can be held to it, which `DataWriteRowCounts` decides. // import Foundation @@ -170,8 +170,7 @@ enum DataWriteExecutor { commitMode: commitMode ) var results: [DataWriteStepResult] = [] - // Through a view the count is whatever its trigger or rule reports. - let countsKeyedUpdates = DataWriteRowCounts.countsMatchedRows(for: plan.databaseType) && plan.viewTarget == nil + let engineCountsMatchedRows = DataWriteRowCounts.countsMatchedRows(for: plan.databaseType) // A delete earlier in the save can cascade to a row a later update edits, which then matches nothing. var ranDelete = false @@ -212,12 +211,14 @@ enum DataWriteExecutor { ) } + let countsMatchedRows = engineCountsMatchedRows || step.countsMatchedRows try verify( step, rowsAffected: result.rowsAffected, owner: owner, - countsAreMeaningful: DataWriteRowCounts.areMeaningful(for: plan.databaseType), - holdsKeyedUpdates: countsKeyedUpdates && !ranDelete + countsAreMeaningful: countsMatchedRows, + // Through a view the count is whatever its trigger or rule reports. + holdsKeyedUpdates: countsMatchedRows && plan.viewTarget == nil && !ranDelete ) ranDelete = ranDelete || step.rowKind == .delete || step.kind == .tableOperation results.append( diff --git a/apple/TablePro/Core/DataWrite/DataWritePlan.swift b/apple/TablePro/Core/DataWrite/DataWritePlan.swift index 2cc5f615a3..3fa90222df 100644 --- a/apple/TablePro/Core/DataWrite/DataWritePlan.swift +++ b/apple/TablePro/Core/DataWrite/DataWritePlan.swift @@ -214,7 +214,7 @@ struct DataWriteStep: Sendable { let statement: ParameterizedStatement /// The most rows this statement may legitimately touch, or nil when the engine's count for it /// carries no meaning: DDL, a session setting, or a statement a plugin wrote itself for a table - /// with a primary key, where nothing tells the host which rows went into it. + /// with a primary key without saying how many rows it matches. let expectedRowCount: Int? let tableName: String? /// Whether this statement finds its rows by matching every column, because the table declares no @@ -226,6 +226,8 @@ struct DataWriteStep: Sendable { let rowKind: RowWriteKind? // Finds the statement's rows right before it runs, for a driver whose engine has no key that names one row. let lookup: RowLookup? + // The driver vouched for `expectedRowCount` as the rows the statement matches, whatever engine it runs on. + let countsMatchedRows: Bool init( kind: Kind, @@ -235,7 +237,8 @@ struct DataWriteStep: Sendable { matchesRowsWithoutKey: Bool = false, refusesSharedKey: Bool = false, rowKind: RowWriteKind? = nil, - lookup: RowLookup? = nil + lookup: RowLookup? = nil, + countsMatchedRows: Bool = false ) { self.kind = kind self.statement = statement @@ -245,6 +248,7 @@ struct DataWriteStep: Sendable { self.refusesSharedKey = refusesSharedKey self.rowKind = rowKind self.lookup = lookup + self.countsMatchedRows = countsMatchedRows } } @@ -272,11 +276,13 @@ extension DataWriteStep { DataWriteStep( kind: .rowWrite, statement: $0.statement, - expectedRowCount: $0.keylessRowCount ?? $0.keyedRowCount, + // The lowest: a driver's count never loosens the one row the host holds a statement to. + expectedRowCount: [$0.matchedRowCount, $0.keylessRowCount, $0.keyedRowCount].compactMap { $0 }.min(), tableName: tableName, matchesRowsWithoutKey: $0.keylessRowCount != nil, rowKind: $0.kind, - lookup: $0.lookup + lookup: $0.lookup, + countsMatchedRows: $0.matchedRowCount != nil ) } } diff --git a/apple/TablePro/Core/DataWrite/DataWriteRowCounts.swift b/apple/TablePro/Core/DataWrite/DataWriteRowCounts.swift index 02c0ca1b97..13bd4787fc 100644 --- a/apple/TablePro/Core/DataWrite/DataWriteRowCounts.swift +++ b/apple/TablePro/Core/DataWrite/DataWriteRowCounts.swift @@ -5,33 +5,23 @@ import Foundation -/// Whether a statement's reported row count means what it says on a given engine. +/// Whether a statement's reported row count is the rows it matched on a given engine, a row that +/// already held the value included. /// -/// A keyless save is only held to the count it expected where the count is real. Most drivers read -/// one from the server, but a driver that reports a constant would fail every such save, so an -/// engine has to earn its way onto this list by a driver that actually asks: MySQL's -/// `mysql_affected_rows`, libpq's `PQcmdTuples`, `sqlite3_changes`, db-lib's `dbcount`, and the -/// count `OracleNIO` carries on a finished stream. +/// A save is only held to the count it expected where the count is real. Most drivers read one +/// from the server, but a driver that reports a constant would fail every such save, so an engine +/// has to earn its way onto this list by a driver that actually asks: MySQL's `mysql_affected_rows` +/// on a session opened with `CLIENT_FOUND_ROWS`, libpq's `PQcmdTuples`, `sqlite3_changes`, db-lib's +/// `dbcount`, and the count `OracleNIO` carries on a finished stream. A driver can also vouch for one +/// statement's count through `PluginRowWrite.expectedRowCount`. enum DataWriteRowCounts { - private static let enginesReportingRealCounts: Set = [ - .mysql, .mariadb, .tidb, .oceanbase, - .postgresql, .redshift, .cockroachdb, .pglite, .kingbasees, - .sqlite, .libsql, .turso, .duckdb, - .mssql, .oracle, - ] - - // An UPDATE's count there is the rows it matched, including rows already holding the value; the MySQL family - // reports the rows it changed, so 0 there is also an UPDATE that wrote what the row held. private static let enginesCountingMatchedRows: Set = [ + .mysql, .mariadb, .tidb, .oceanbase, .postgresql, .redshift, .cockroachdb, .pglite, .kingbasees, .sqlite, .libsql, .turso, .duckdb, .mssql, .oracle, ] - static func areMeaningful(for databaseType: DatabaseType) -> Bool { - enginesReportingRealCounts.contains(databaseType) - } - static func countsMatchedRows(for databaseType: DatabaseType) -> Bool { enginesCountingMatchedRows.contains(databaseType) } diff --git a/apple/TablePro/Core/DataWrite/KeyedWriteVerification.swift b/apple/TablePro/Core/DataWrite/KeyedWriteVerification.swift index 65822b5157..0b7ef0d62c 100644 --- a/apple/TablePro/Core/DataWrite/KeyedWriteVerification.swift +++ b/apple/TablePro/Core/DataWrite/KeyedWriteVerification.swift @@ -3,8 +3,8 @@ // TablePro // // The rule every keyed write is held to: more rows than expected is a fault. Fewer is one only -// for an UPDATE on an engine whose count is the rows it matched (`DataWriteRowCounts`): MySQL -// reports zero affected rows for an UPDATE that writes a value a row already holds. +// for an UPDATE whose count is the rows it matched (`DataWriteRowCounts`): a DELETE that found its +// row already gone left the table as the user wanted it. // import Foundation diff --git a/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift b/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift index 978770a650..4b936fe665 100644 --- a/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift +++ b/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift @@ -53,6 +53,8 @@ struct DriverWrittenStatement { var lookup: RowLookup? // A keyed write through a view, held to its one row: the key is the view's answer, not the table's own. var keyedRowCount: Int? + // The driver's own count of the rows the statement matches. + var matchedRowCount: Int? } @MainActor @@ -597,7 +599,8 @@ struct RowChangeStatementFactory { lookup: write.lookup.map(RowLookup.init), keyedRowCount: write.lookup == nil && holdsKeyedWritesToOneRow ? oneRowCount(of: write, keyed: keyed) - : nil + : nil, + matchedRowCount: write.expectedRowCount ) } } @@ -666,7 +669,7 @@ struct RowChangeStatementFactory { } private func oneRowCount(of write: PluginRowWrite, keyed: PluginKeyedChanges) -> Int? { - guard DataWriteRowCounts.areMeaningful(for: databaseType), + guard DataWriteRowCounts.countsMatchedRows(for: databaseType), write.rowIndices.count == 1, let row = write.rowIndices.first else { return nil } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Discard.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Discard.swift index 9289878f49..d8eb9361dd 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Discard.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Discard.swift @@ -4,10 +4,11 @@ // import Foundation +import TableProPluginKit extension MainContentCoordinator { - func executeSidebarChanges(steps: [DataWriteStep]) async throws { - try await rowEditingCoordinator.executeSidebarChanges(steps: steps) + func executeSidebarChanges(steps: [DataWriteStep], viewTarget: PluginViewWriteTarget?) async throws { + try await rowEditingCoordinator.executeSidebarChanges(steps: steps, viewTarget: viewTarget) } func handleDiscard( diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SidebarSave.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SidebarSave.swift index 6222977189..61f0a3abf2 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SidebarSave.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SidebarSave.swift @@ -13,9 +13,12 @@ extension MainContentCoordinator { let changes = sidebarEditChanges(editedFields: editState.getEditedFields()) guard !changes.isEmpty else { return } // Held to its row counts as a grid save is, so a write that found no row fails instead of saving nothing. - let steps = try changeManager.writeSteps(for: changes, factory: sidebarStatementFactory()) + let viewTarget = activeViewWriteTarget + var factory = try sidebarStatementFactory() + factory.holdsKeyedWritesToOneRow = viewTarget != nil + let steps = try changeManager.writeSteps(for: changes, factory: factory) guard !steps.isEmpty else { return } - try await executeSidebarChanges(steps: steps) + try await executeSidebarChanges(steps: steps, viewTarget: viewTarget) reloadActiveResult() } diff --git a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift index b979266ec6..3295752a56 100644 --- a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift +++ b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift @@ -88,7 +88,8 @@ struct DataWriteExecutorTests { matchesRowsWithoutKey: Bool = false, refusesSharedKey: Bool = false, rowKind: RowWriteKind? = nil, - databaseType: DatabaseType = .sqlite + databaseType: DatabaseType = .sqlite, + countsMatchedRows: Bool = false ) -> DataWritePlan { DataWritePlan( scope: DatabaseScope(connectionId: UUID(), database: "shop", schema: nil), @@ -101,7 +102,8 @@ struct DataWriteExecutorTests { tableName: "t", matchesRowsWithoutKey: matchesRowsWithoutKey, refusesSharedKey: refusesSharedKey, - rowKind: rowKind + rowKind: rowKind, + countsMatchedRows: countsMatchedRows ) }, prologue: prologue, @@ -143,11 +145,11 @@ struct DataWriteExecutorTests { #expect(counting.didRollBack == false) } - @Test("A keyed UPDATE short of its rows is an ordinary save on MySQL, which reports zero for an unchanged value") + @Test("A keyed UPDATE short of its rows is an ordinary save where nothing says the count is the rows matched") func fewerRowsIsNotAFailure() async throws { let counting = CountingDriver(affectedRows: 0) let results = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, rowKind: .update, databaseType: .mysql), + plan(expectedRowCount: 1, rowKind: .update, databaseType: .snowflake), commitMode: .automatic, on: driver(counting) ).results @@ -158,7 +160,8 @@ struct DataWriteExecutorTests { @Test("A keyed UPDATE that matched no row fails where the count is the rows matched") func keyedUpdateThatMatchedNothingFails() async throws { - for databaseType in [DatabaseType.postgresql, .sqlite, .duckdb] { + let engines: [DatabaseType] = [.postgresql, .sqlite, .duckdb, .mysql, .mariadb, .tidb, .oceanbase] + for databaseType in engines { let counting = CountingDriver(affectedRows: 0) await #expect(throws: DataWriteError.self) { try await DataWriteExecutor.run( @@ -171,6 +174,41 @@ struct DataWriteExecutorTests { } } + @Test("A driver's own matched-row count holds a keyed UPDATE on an engine the host keeps no count for") + func driverCountHoldsAKeyedUpdate() async throws { + let counting = CountingDriver(affectedRows: 0) + await #expect(throws: DataWriteError.self) { + try await DataWriteExecutor.run( + plan(expectedRowCount: 1, rowKind: .update, databaseType: .snowflake, countsMatchedRows: true), + commitMode: .automatic, on: driver(counting) + ) + } + #expect(counting.didRollBack) + #expect(counting.didCommit == false) + } + + @Test("A driver's own count leaves a keyed DELETE of a row already gone an ordinary save") + func driverCountLeavesAGoneRowsDeleteAlone() async throws { + let counting = CountingDriver(affectedRows: 0) + _ = try await DataWriteExecutor.run( + plan(expectedRowCount: 1, rowKind: .delete, databaseType: .snowflake, countsMatchedRows: true), + commitMode: .automatic, on: driver(counting) + ) + + #expect(counting.didCommit) + } + + @Test("A driver's own count through a view keeps the lenient rule") + func driverCountThroughAViewStaysLenient() async throws { + let counting = CountingDriver(affectedRows: 0) + var viewPlan = plan(expectedRowCount: 1, rowKind: .update, databaseType: .mssql, countsMatchedRows: true) + viewPlan.viewTarget = TestFixtures.viewTarget(keys: ["id"]) + + _ = try await DataWriteExecutor.run(viewPlan, commitMode: .automatic, on: driver(counting)) + + #expect(counting.didCommit) + } + @Test("A keyed UPDATE through a view keeps the lenient rule: its count comes from the view's trigger") func keyedUpdateThroughAViewStaysLenient() async throws { let counting = CountingDriver(affectedRows: 0) diff --git a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift index c6c74fa0fc..f4df13db8a 100644 --- a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift +++ b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift @@ -319,7 +319,42 @@ struct RowChangeStatementFactoryCoverageTests { let build = try manager.buildRowWrites(database: "d", schema: nil, containsTableOperation: false) #expect(build.steps.map(\.statement.sql) == ["updateOne(0)"]) - #expect(build.steps.allSatisfy { $0.expectedRowCount == nil }) + #expect(build.steps.allSatisfy { $0.expectedRowCount == nil && !$0.countsMatchedRows }) + } + + @Test("A driver's own matched-row count reaches the save plan as the count the step is held to") + func buildRowWritesTakesTheDriversMatchedRowCount() throws { + let manager = DataChangeManager() + manager.configureForTable( + tableName: "items", columns: columns, primaryKeyColumns: ["_id"], + databaseType: .snowflake, generatedColumns: [] + ) + manager.pluginDriver = RowWriteStubDriver { changes, _, _, _ in + changes.map { PluginRowWrite(statement: "updateOne(\($0.rowIndex))", rowIndices: [$0.rowIndex], expectedRowCount: 1) } + } + manager.recordCellChange( + rowID: .existing(0), columnIndex: 1, columnName: "name", + oldValue: "a", newValue: "z", originalRow: ["0", "a"] + ) + + let build = try manager.buildRowWrites(database: "d", schema: nil, containsTableOperation: false) + + #expect(build.steps.map(\.expectedRowCount) == [1]) + #expect(build.steps.allSatisfy { $0.countsMatchedRows && !$0.matchesRowsWithoutKey }) + } + + @Test("A driver's count never raises the one row the host holds a keyless statement to") + func driverCountNeverLoosensTheKeylessHold() throws { + let driver = RowWriteStubDriver { changes, _, _, _ in + changes.map { PluginRowWrite(statement: "updateOne(\($0.rowIndex))", rowIndices: [$0.rowIndex], expectedRowCount: 3) } + } + let written = try factory(primaryKeyColumns: [], databaseType: .mssql, driver: driver) + .rowWriteStatements(for: [nameEdit()]) + + let steps = DataWriteStep.rowWrites(written, tableName: "items", primaryKeyColumns: []) + + #expect(steps.map(\.expectedRowCount) == [1]) + #expect(steps.allSatisfy { $0.matchesRowsWithoutKey && $0.countsMatchedRows }) } @Test("A driver's keyed statement through a view is held to its one row") diff --git a/apple/TableProTests/Core/Plugins/MySQLProtocolVariantParityTests.swift b/apple/TableProTests/Core/Plugins/MySQLProtocolVariantParityTests.swift index 4ccdd7da4d..6e7313f9f0 100644 --- a/apple/TableProTests/Core/Plugins/MySQLProtocolVariantParityTests.swift +++ b/apple/TableProTests/Core/Plugins/MySQLProtocolVariantParityTests.swift @@ -47,7 +47,7 @@ struct MySQLProtocolVariantParityTests { @Test("A variant whose driver reads mysql_affected_rows holds a keyless save to its row count", arguments: variants) func rowCountsAgree(type: DatabaseType) { - #expect(DataWriteRowCounts.areMeaningful(for: type) == !Self.outsideMySQLDialect.contains(type)) + #expect(DataWriteRowCounts.countsMatchedRows(for: type) == !Self.outsideMySQLDialect.contains(type)) } /// The app classifies a database list by the connection type's registry entry and the driver flags metadata by diff --git a/apple/TableProTests/Core/Plugins/PluginMetadataRegistryStarRocksTests.swift b/apple/TableProTests/Core/Plugins/PluginMetadataRegistryStarRocksTests.swift index c82b0b9db1..79d0c2de92 100644 --- a/apple/TableProTests/Core/Plugins/PluginMetadataRegistryStarRocksTests.swift +++ b/apple/TableProTests/Core/Plugins/PluginMetadataRegistryStarRocksTests.swift @@ -39,7 +39,7 @@ struct PluginMetadataRegistryStarRocksTests { @Test("A DELETE on a duplicate key table reports 0 rows, so a keyless save is not held to its count") func rowCountsAreNotMeaningful() { - #expect(!DataWriteRowCounts.areMeaningful(for: .starrocks)) + #expect(!DataWriteRowCounts.countsMatchedRows(for: .starrocks)) } @Test("A StarRocks URL keeps the catalog.database path and routes to StarRocks") diff --git a/apple/changelog.d/4242.changed.md b/apple/changelog.d/4242.changed.md new file mode 100644 index 0000000000..256009722f --- /dev/null +++ b/apple/changelog.d/4242.changed.md @@ -0,0 +1 @@ +MySQL, MariaDB, TiDB and OceanBase count an UPDATE's matched rows as affected, unchanged rows included. diff --git a/apple/changelog.d/4242.fixed.md b/apple/changelog.d/4242.fixed.md new file mode 100644 index 0000000000..52614570f2 --- /dev/null +++ b/apple/changelog.d/4242.fixed.md @@ -0,0 +1 @@ +Saving an edit that matched no row reported success on MySQL, MariaDB, SQL Server, Oracle, Snowflake and Dameng. diff --git a/apple/scripts/probes/check-mssql-keyed-row-counts.sh b/apple/scripts/probes/check-mssql-keyed-row-counts.sh new file mode 100755 index 0000000000..09e1a9fa3b --- /dev/null +++ b/apple/scripts/probes/check-mssql-keyed-row-counts.sh @@ -0,0 +1,266 @@ +#!/usr/bin/env bash +# +# Check that the count SQL Server reports for a grid save's UPDATE and DELETE is the rows the statement matched. +# +# MSSQLStatementGenerator tells the host each update and delete matches one row (PluginRowWrite.expectedRowCount), and +# the host then fails a save whose count is higher, or lower for an UPDATE. That is only right while these hold, and +# no unit test can see them because each depends on what the server sends and how db-lib reads it: +# +# - An UPDATE that writes the value a row already holds still counts the row. +# - An UPDATE or DELETE that matches nothing counts 0. +# - A session left at SET NOCOUNT ON still reports the count, through the SET NOCOUNT OFF the statement carries. +# - Rows an AFTER trigger writes are not added, with or without SET NOCOUNT ON inside the trigger, and an +# INSTEAD OF trigger leaves the statement's own count. +# +# So this builds a harness from the real Plugins/MSSQLDriverPlugin sources, the TableProCore package and the shipped +# Libs/libsybdb.a, writes each statement with MSSQLStatementGenerator, runs it through MSSQLPluginDriver, and fails +# when a count differs. Verified against SQL Server 2022 CU14 with FreeTDS 1.4.22. +# +# Usage: +# MSSQL_SA_PASSWORD=secret scripts/probes/check-mssql-keyed-row-counts.sh [host] [port] [user] [database] +# +# The database defaults to tablepro_row_count_check and is created when missing. Exits 1 when a check fails, 3 when it +# cannot run. + +set -uo pipefail + +HOST="${1:-127.0.0.1}" +PORT="${2:-1433}" +USER_NAME="${3:-sa}" +DATABASE="${4:-tablepro_row_count_check}" +PASSWORD="${MSSQL_SA_PASSWORD:-}" + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +[ -f "$ROOT/Libs/libsybdb.a" ] || { + echo "not found: Libs/libsybdb.a (run scripts/download-libs.sh)" >&2 + exit 3 +} +[ -n "$PASSWORD" ] || { + echo "no password: set MSSQL_SA_PASSWORD for the server at $HOST:$PORT" >&2 + exit 3 +} +nc -z "$HOST" "$PORT" > /dev/null 2>&1 || { + echo "no SQL Server listening at $HOST:$PORT" >&2 + exit 3 +} + +mkdir -p "$WORK/Sources/Check" +ln -s "$ROOT/Plugins/MSSQLDriverPlugin/CFreeTDS" "$WORK/CFreeTDS" +# The plugin target's sources: its folder plus the shared files the DriverPlugin template in project.yml adds. +for source in "$ROOT"/Plugins/MSSQLDriverPlugin/*.swift "$ROOT"/Plugins/Shared/PluginConnectTimeout.swift \ + "$ROOT"/Plugins/Shared/PluginQueryTimeout.swift "$ROOT"/Plugins/Shared/PluginURLSessionTaskBox.swift \ + "$ROOT"/Plugins/Shared/PluginRedirectRefusingDelegate.swift; do + ln -s "$source" "$WORK/Sources/Check/$(basename "$source")" +done + +cat > "$WORK/Package.swift" << MANIFEST +// swift-tools-version: 6.0 +import PackageDescription + +let package = Package( + name: "MSSQLRowCountCheck", + platforms: [.macOS(.v14)], + dependencies: [.package(path: "$ROOT/Packages/TableProCore")], + targets: [ + .systemLibrary(name: "CFreeTDS", path: "CFreeTDS"), + .executableTarget( + name: "Check", + dependencies: [ + "CFreeTDS", + .product(name: "TableProPluginKit", package: "TableProCore"), + .product(name: "TableProCoreTypes", package: "TableProCore"), + .product(name: "TableProMSSQLCore", package: "TableProCore"), + .product(name: "TableProLogRedaction", package: "TableProCore"), + .product(name: "TableProKerberos", package: "TableProCore"), + .product(name: "TableProSQLQuoting", package: "TableProCore"), + .product(name: "TableProSQLVocabulary", package: "TableProCore"), + ], + path: "Sources/Check", + swiftSettings: [.swiftLanguageMode(.v6)], + linkerSettings: [.unsafeFlags([ + "-L$ROOT/Libs", "-L$ROOT/Libs/dylibs", "-lsybdb", "-lssl.3", "-lcrypto.3", "-liconv", + "-framework", "GSS", "-lcom_err", "-Xlinker", "-rpath", "-Xlinker", "$ROOT/Libs/dylibs", + ])] + ), + ] +) +MANIFEST + +cat > "$WORK/Sources/Check/Check.swift" << 'SWIFT' +import Foundation +import TableProPluginKit + +@main +enum Check { + nonisolated(unsafe) static var failures = 0 + + static let database = ProcessInfo.processInfo.environment["TP_CHECK_DATABASE"] ?? "tablepro_row_count_check" + static let columns = ["id", "a"] + + static func expect(_ condition: Bool, _ label: String, _ detail: @autoclosure () -> String = "") { + if condition { + print("PASS: \(label)") + } else { + failures += 1 + print("FAIL: \(label) \(detail())") + } + } + + static func connect(_ database: String) async throws -> MSSQLPluginDriver { + let environment = ProcessInfo.processInfo.environment + let config = DriverConnectionConfig( + host: environment["TP_CHECK_HOST"] ?? "127.0.0.1", + port: Int(environment["TP_CHECK_PORT"] ?? "") ?? 1433, + username: environment["TP_CHECK_USER"] ?? "sa", + password: environment["TP_CHECK_PASSWORD"] ?? "", + database: database + ) + let driver = MSSQLPluginDriver(config: config) + try await driver.connect() + return driver + } + + static func main() async { + setvbuf(stdout, nil, _IOLBF, 0) + let driver: MSSQLPluginDriver + do { + let admin = try await connect("master") + _ = try await admin.execute(query: "IF DB_ID(N'\(database)') IS NULL CREATE DATABASE [\(database)]") + admin.disconnect() + driver = try await connect(database) + } catch { + print("cannot connect: \(error)") + exit(3) + } + do { + try await run(driver) + } catch { + failures += 1 + print("FAIL: unexpected error \(error)") + } + print(failures == 0 ? "OK: every check passed" : "\(failures) check(s) failed") + exit(failures == 0 ? 0 : 1) + } + + static func update(id: String, to value: String) -> PluginRowChange { + PluginRowChange( + rowIndex: 0, type: .update, + cellChanges: [(columnIndex: 1, columnName: "a", oldValue: .text(value), newValue: .text(value))], + originalRow: [.text(id), .text(value)] + ) + } + + static func delete(id: String) -> PluginRowChange { + PluginRowChange(rowIndex: 0, type: .delete, cellChanges: [], originalRow: [.text(id), .text("1")]) + } + + static func write(_ change: PluginRowChange, keyed: Bool) throws -> PluginRowWrite { + let generator = MSSQLStatementGenerator( + qualifiedTable: "[dbo].[rc_t]", columns: columns, primaryKeyColumns: keyed ? ["id"] : [] + ) + let writes = try generator.rowWrites( + for: [change], insertedRowData: [:], + deletedRowIndices: change.type == .delete ? [0] : [], insertedRowIndices: [] + ) + guard let write = writes.first else { throw CocoaError(.featureUnsupported) } + return write + } + + static func count(_ write: PluginRowWrite, on driver: MSSQLPluginDriver) async throws -> Int { + try await driver.executeParameterized(query: write.statement, parameters: write.parameters).rowsAffected + } + + static func reset(_ driver: MSSQLPluginDriver, trigger: String?) async throws { + _ = try await driver.execute(query: "IF OBJECT_ID(N'dbo.rc_t') IS NOT NULL DROP TABLE dbo.rc_t") + _ = try await driver.execute(query: "IF OBJECT_ID(N'dbo.rc_audit') IS NOT NULL DROP TABLE dbo.rc_audit") + _ = try await driver.execute(query: "CREATE TABLE dbo.rc_t (id INT PRIMARY KEY, a INT)") + _ = try await driver.execute(query: "CREATE TABLE dbo.rc_audit (n INT)") + _ = try await driver.execute(query: "INSERT INTO dbo.rc_t VALUES (1, 1), (2, 1), (3, 1)") + if let trigger { _ = try await driver.execute(query: trigger) } + } + + static func checkCounts(_ driver: MSSQLPluginDriver, _ label: String) async throws { + let same = try write(update(id: "1", to: "1"), keyed: true) + expect(same.expectedRowCount == 1, "\(label): a keyed update says it matches one row") + let sameCount = try await count(same, on: driver) + expect(sameCount == 1, "\(label): a keyed update writing the value the row holds counts 1", "got \(sameCount)") + let missCount = try await count(try write(update(id: "99", to: "1"), keyed: true), on: driver) + expect(missCount == 0, "\(label): a keyed update that matches nothing counts 0", "got \(missCount)") + let deleteCount = try await count(try write(delete(id: "2"), keyed: true), on: driver) + expect(deleteCount == 1, "\(label): a keyed delete counts 1", "got \(deleteCount)") + let goneCount = try await count(try write(delete(id: "2"), keyed: true), on: driver) + expect(goneCount == 0, "\(label): a keyed delete of a row already gone counts 0", "got \(goneCount)") + let keylessCount = try await count(try write(update(id: "3", to: "1"), keyed: false), on: driver) + expect(keylessCount == 1, "\(label): a keyless update writing the value the row holds counts 1", "got \(keylessCount)") + } + + static func run(_ driver: MSSQLPluginDriver) async throws { + try await reset(driver, trigger: nil) + try await checkCounts(driver, "no trigger") + + try await reset(driver, trigger: nil) + _ = try await driver.execute(query: "SET NOCOUNT ON") + try await checkCounts(driver, "session at SET NOCOUNT ON") + let options = try await driver.execute(query: "SELECT @@OPTIONS & 512") + expect(options.rows.first?.first?.asText == "512", "the session is still at SET NOCOUNT ON afterwards") + _ = try await driver.execute(query: "SET NOCOUNT OFF") + + // A keyless row of NULLs set to DEFAULT binds nothing, so the statement runs in its own sp_executesql. + _ = try await driver.execute(query: "IF OBJECT_ID(N'dbo.rc_k') IS NOT NULL DROP TABLE dbo.rc_k") + _ = try await driver.execute(query: "CREATE TABLE dbo.rc_k (id INT NULL, a INT NULL)") + _ = try await driver.execute(query: "INSERT INTO dbo.rc_k VALUES (NULL, NULL)") + var toDefault = PluginRowChange( + rowIndex: 0, type: .update, + cellChanges: [(columnIndex: 1, columnName: "a", oldValue: .null, newValue: .null)], + originalRow: [.null, .null] + ) + toDefault.defaultColumns = [1] + let unbound = try MSSQLStatementGenerator(qualifiedTable: "[dbo].[rc_k]", columns: columns, primaryKeyColumns: []) + .rowWrites(for: [toDefault], insertedRowData: [:], deletedRowIndices: [], insertedRowIndices: []) + guard let unboundWrite = unbound.first else { throw CocoaError(.featureUnsupported) } + expect(unboundWrite.parameters.isEmpty && unboundWrite.statement.hasPrefix("EXEC sp_executesql"), + "a statement binding nothing runs in its own sp_executesql", unboundWrite.statement) + _ = try await driver.execute(query: "SET NOCOUNT ON") + let unboundCount = try await count(unboundWrite, on: driver) + expect(unboundCount == 1, "a statement binding nothing counts 1 under SET NOCOUNT ON", "got \(unboundCount)") + _ = try await driver.execute(query: "SET NOCOUNT OFF") + _ = try await driver.execute(query: "DROP TABLE dbo.rc_k") + + let audit = "INSERT INTO dbo.rc_audit SELECT id FROM inserted UNION ALL SELECT id FROM deleted" + try await reset(driver, trigger: "CREATE TRIGGER dbo.rc_trg ON dbo.rc_t AFTER UPDATE, DELETE AS \(audit)") + try await checkCounts(driver, "AFTER trigger") + let audited = try await driver.execute(query: "SELECT COUNT(*) FROM dbo.rc_audit") + expect(audited.rows.first?.first?.asText != "0", "AFTER trigger: the trigger wrote its rows") + + try await reset( + driver, + trigger: "CREATE TRIGGER dbo.rc_trg ON dbo.rc_t AFTER UPDATE, DELETE AS BEGIN SET NOCOUNT ON; \(audit) END" + ) + try await checkCounts(driver, "AFTER trigger at SET NOCOUNT ON") + + try await reset(driver, trigger: """ + CREATE TRIGGER dbo.rc_trg ON dbo.rc_t INSTEAD OF UPDATE AS \ + UPDATE t SET a = i.a FROM dbo.rc_t t JOIN inserted i ON i.id = t.id + """) + let insteadCount = try await count(try write(update(id: "1", to: "1"), keyed: true), on: driver) + expect(insteadCount == 1, "INSTEAD OF trigger: a keyed update counts 1", "got \(insteadCount)") + + _ = try await driver.execute(query: "DROP TABLE dbo.rc_t") + _ = try await driver.execute(query: "DROP TABLE dbo.rc_audit") + driver.disconnect() + } +} +SWIFT + +export DEVELOPER_DIR="${DEVELOPER_DIR:-$(xcode-select -p)}" +swift build --package-path "$WORK" --scratch-path "$WORK/.build" > "$WORK/build.log" 2>&1 || { + echo "the check failed to build" >&2 + grep -E "error:" "$WORK/build.log" >&2 + exit 3 +} + +TP_CHECK_HOST="$HOST" TP_CHECK_PORT="$PORT" TP_CHECK_USER="$USER_NAME" TP_CHECK_PASSWORD="$PASSWORD" \ + TP_CHECK_DATABASE="$DATABASE" "$WORK/.build/debug/Check" diff --git a/apple/scripts/probes/check-mysql-found-rows.sh b/apple/scripts/probes/check-mysql-found-rows.sh new file mode 100755 index 0000000000..30f477d1bb --- /dev/null +++ b/apple/scripts/probes/check-mysql-found-rows.sh @@ -0,0 +1,179 @@ +#!/usr/bin/env bash +# +# Check that a MySQL-family session opened with CLIENT_FOUND_ROWS counts the rows an UPDATE matched. +# +# The MySQL driver connects with CLIENT_FOUND_ROWS (MySQLTransport.clientFlags), and DataWriteRowCounts then holds a +# keyed UPDATE to the one row it was written for: a count of 0 fails the save. That is only right while the server +# counts a row that already held the value, which a session without the flag does not. No unit test can see it, so +# this builds a client from the shipped Libs/libmariadb.a and measures, with and without the flag: +# +# - an UPDATE writing the value the row holds counts 1 (0 without the flag), as text and as a prepared statement, +# and ROW_COUNT() agrees; +# - an UPDATE that matches nothing counts 0; +# - INSERT ... ON DUPLICATE KEY UPDATE onto a row holding those values counts 1; +# - rows an AFTER UPDATE trigger writes are not added (skipped on a server with no triggers, as TiDB). +# +# Verified against MySQL 8.4.13, MariaDB 11.4.13, TiDB 8.5.1 and OceanBase 4.4.2.1 with libmariadb 3.4.4. +# +# Usage: +# MYSQL_PWD=secret scripts/probes/check-mysql-found-rows.sh [host] [port] [user] [database] +# +# The password is read from MYSQL_PWD only, never from an argument. TLS is required unless the host is a loopback +# address. It creates and drops the tables fr_check and fr_check_audit in the database, test by default. Exits 1 when +# a count differs, 3 when it cannot run. + +set -uo pipefail + +HOST="${1:-127.0.0.1}" +PORT="${2:-3306}" +USER_NAME="${3:-root}" +DATABASE="${4:-test}" + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +[ -f "$ROOT/Libs/libmariadb.a" ] || { + echo "not found: Libs/libmariadb.a (run scripts/download-libs.sh)" >&2 + exit 3 +} + +LOOPBACK=0 +case "$HOST" in + 127.*|localhost|::1) LOOPBACK=1 ;; +esac + +cat > "$WORK/check.c" << 'C' +#include +#include +#include +#include + +static int failures = 0; + +static void expect(long long actual, long long expected, const char *label) { + if (actual == expected) { + printf("PASS: %s\n", label); + } else { + failures++; + printf("FAIL: %s (expected %lld, got %lld)\n", label, expected, actual); + } +} + +static long long affected(MYSQL *m, const char *sql) { + if (mysql_query(m, sql) != 0) { + printf("FAIL: %s: %s\n", sql, mysql_error(m)); + failures++; + return -1; + } + return (long long)mysql_affected_rows(m); +} + +static long long scalar(MYSQL *m, const char *sql) { + long long value = -1; + if (mysql_query(m, sql) != 0) { + printf("FAIL: %s: %s\n", sql, mysql_error(m)); + failures++; + return value; + } + MYSQL_RES *result = mysql_store_result(m); + MYSQL_ROW row = result ? mysql_fetch_row(result) : NULL; + if (row && row[0]) value = atoll(row[0]); + if (result) mysql_free_result(result); + return value; +} + +static long long prepared(MYSQL *m, int value, int id) { + const char *sql = "UPDATE fr_check SET a = ? WHERE id = ?"; + MYSQL_STMT *s = mysql_stmt_init(m); + MYSQL_BIND b[2]; + memset(b, 0, sizeof b); + b[0].buffer_type = MYSQL_TYPE_LONG; + b[0].buffer = &value; + b[1].buffer_type = MYSQL_TYPE_LONG; + b[1].buffer = &id; + long long count = -1; + if (mysql_stmt_prepare(s, sql, strlen(sql)) == 0 && mysql_stmt_bind_param(s, b) == 0 && mysql_stmt_execute(s) == 0) { + count = (long long)mysql_stmt_affected_rows(s); + } else { + printf("FAIL: prepared UPDATE: %s\n", mysql_stmt_error(s)); + failures++; + } + mysql_stmt_close(s); + return count; +} + +static MYSQL *open_session(char **argv, int loopback, unsigned long flags) { + MYSQL *m = mysql_init(NULL); + if (loopback) { + my_bool off = 0; + mysql_options(m, MYSQL_OPT_SSL_ENFORCE, &off); + mysql_options(m, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &off); + } + if (!mysql_real_connect(m, argv[1], argv[3], getenv("MYSQL_PWD"), argv[4], (unsigned)atoi(argv[2]), NULL, flags)) { + fprintf(stderr, "cannot connect: %s\n", mysql_error(m)); + exit(3); + } + return m; +} + +static void reset(MYSQL *m) { + affected(m, "DROP TABLE IF EXISTS fr_check_audit"); + affected(m, "DROP TABLE IF EXISTS fr_check"); + affected(m, "CREATE TABLE fr_check (id INT PRIMARY KEY, a INT)"); + affected(m, "INSERT INTO fr_check VALUES (1, 1), (2, 1)"); +} + +int main(int argc, char **argv) { + (void)argc; + int loopback = atoi(argv[5]); + + MYSQL *plain = open_session(argv, loopback, 0); + printf("server %s, libmariadb %s\n", mysql_get_server_info(plain), mysql_get_client_info()); + reset(plain); + expect(affected(plain, "UPDATE fr_check SET a = 1 WHERE id = 1"), 0, + "without the flag an UPDATE writing the value the row holds counts 0"); + mysql_close(plain); + + MYSQL *m = open_session(argv, loopback, CLIENT_FOUND_ROWS); + unsigned long server = 0; + mariadb_get_infov(m, MARIADB_CONNECTION_SERVER_CAPABILITIES, &server); + expect((server & CLIENT_FOUND_ROWS) != 0, 1, "the server offers CLIENT_FOUND_ROWS"); + reset(m); + expect(affected(m, "UPDATE fr_check SET a = 1 WHERE id = 1"), 1, + "an UPDATE writing the value the row holds counts 1"); + expect(scalar(m, "SELECT ROW_COUNT()"), 1, "ROW_COUNT() after it is 1"); + expect(prepared(m, 1, 1), 1, "a prepared UPDATE writing the value the row holds counts 1"); + expect(affected(m, "UPDATE fr_check SET a = 1 WHERE id = 99"), 0, "an UPDATE that matches nothing counts 0"); + expect(prepared(m, 1, 99), 0, "a prepared UPDATE that matches nothing counts 0"); + expect(affected(m, "UPDATE fr_check SET a = 1"), 2, "an UPDATE over two rows holding the value counts 2"); + expect(affected(m, "INSERT INTO fr_check VALUES (2, 1) ON DUPLICATE KEY UPDATE a = VALUES(a)"), 1, + "ON DUPLICATE KEY UPDATE onto a row holding the values counts 1"); + + affected(m, "CREATE TABLE fr_check_audit (n INT)"); + if (mysql_query(m, "CREATE TRIGGER fr_check_trg AFTER UPDATE ON fr_check FOR EACH ROW " + "INSERT INTO fr_check_audit VALUES (NEW.id), (NEW.id)") == 0) { + expect(affected(m, "UPDATE fr_check SET a = 1 WHERE id = 1"), 1, + "rows an AFTER UPDATE trigger writes are not counted"); + expect(scalar(m, "SELECT COUNT(*) FROM fr_check_audit"), 2, "the trigger wrote its two rows"); + } else { + printf("SKIP: no triggers here: %s\n", mysql_error(m)); + } + affected(m, "DROP TABLE fr_check_audit"); + affected(m, "DROP TABLE fr_check"); + mysql_close(m); + + printf(failures == 0 ? "OK: every check passed\n" : "%d check(s) failed\n", failures); + return failures == 0 ? 0 : 1; +} +C + +clang -O0 -o "$WORK/check" "$WORK/check.c" -I"$ROOT/Plugins/MySQLDriverPlugin/CMariaDB/include" "$ROOT/Libs/libmariadb.a" \ + -L"$ROOT/Libs/dylibs" -lssl.3 -lcrypto.3 -liconv -lz -framework GSS -Wl,-rpath,"$ROOT/Libs/dylibs" \ + > "$WORK/build.log" 2>&1 || { + echo "the check failed to build" >&2 + cat "$WORK/build.log" >&2 + exit 3 +} + +"$WORK/check" "$HOST" "$PORT" "$USER_NAME" "$DATABASE" "$LOOPBACK"