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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
))
}
}
Expand Down
23 changes: 17 additions & 6 deletions apple/Plugins/MSSQLDriverPlugin/MSSQLStatementGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MYSQL>,
Expand All @@ -48,14 +52,14 @@ enum MySQLTransport: Sendable, Equatable {
) -> UnsafeMutablePointer<MYSQL>? {
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)
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions apple/Plugins/MySQLDriverTests/MySQLFoundRowsTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
11 changes: 7 additions & 4 deletions apple/Plugins/OracleDriverPlugin/OracleRowWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -134,7 +135,7 @@ internal struct OracleRowWriter {
let match = try rowMatch(for: change, parameters: &parameters)
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] {
Expand All @@ -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
)
}
}
Expand All @@ -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
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand Down Expand Up @@ -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 }
Expand All @@ -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
Expand Down
17 changes: 16 additions & 1 deletion apple/Plugins/TableProPluginKit/PluginRowWrite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions apple/TablePro/Core/DataWrite/DataWriteExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down
14 changes: 10 additions & 4 deletions apple/TablePro/Core/DataWrite/DataWritePlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -245,6 +248,7 @@ struct DataWriteStep: Sendable {
self.refusesSharedKey = refusesSharedKey
self.rowKind = rowKind
self.lookup = lookup
self.countsMatchedRows = countsMatchedRows
}
}

Expand Down Expand Up @@ -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
)
}
}
Expand Down
Loading
Loading