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
Expand Up @@ -160,7 +160,8 @@ public enum SQLCatalogEffectClassifier {
sql.range(of: #"\bINTO\b"#, options: [.regularExpression, .caseInsensitive]) != nil
}

private static let transactionEndKeywords: Set<String> = ["COMMIT", "END", "ROLLBACK", "ABORT"]
// ET is Teradata's END TRANSACTION.
private static let transactionEndKeywords: Set<String> = ["COMMIT", "END", "ROLLBACK", "ABORT", "ET"]

// PostgreSQL's `DISCARD TEMP` and `DISCARD ALL` drop the session's temporary tables.
private static let discardedTableTargets: Set<String> = ["TEMP", "TEMPORARY", "ALL"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
private var _sessionMode: TeradataTransactionMode
private var _sessionModeSource: TeradataSessionModeSource
private var _transaction: TeradataTransactionTracker
// Nil until a switch the connection made itself, and again once a request of the user's may have moved it.
private var _defaultDatabase: String?

// 3110: "The transaction was aborted by the user."
public static let userAbortCode = 3_110
Expand Down Expand Up @@ -170,6 +172,7 @@
_sessionMode = mode
_sessionModeSource = source
_transaction = TeradataTransactionTracker(mode: mode)
_defaultDatabase = config.database.flatMap { $0.isEmpty ? nil : $0 }
}
}

Expand Down Expand Up @@ -223,10 +226,18 @@

// After DATABASE inside a transaction only the closing statement is legal (ANSI counts it as DDL; Teradata mode
// is not documented either way), so a switch never joins an open one, and in ANSI it is committed at once.
// The host re-sends the switch before every statement, so one to where the session already is sends nothing.
public func switchDatabase(to database: String) throws {
let (mode, state) = socketLock.withLock { (_sessionMode, _transaction.state) }
guard state != .open else { throw TeradataWireError.openTransactionBlocksDatabaseSwitch }
let (mode, state, current) = socketLock.withLock { (_sessionMode, _transaction.state, _defaultDatabase) }
guard state != .open else {
// Teradata compares database names without regard to case.
guard let current, current.caseInsensitiveCompare(database) == .orderedSame else {
throw TeradataWireError.openTransactionBlocksDatabaseSwitch
}
return
}
_ = try execute(TeradataSchemaQueries.setDatabase(database))
socketLock.withLock { _defaultDatabase = database }
if mode == .ansi {
_ = try execute(TeradataTransactionSQL.commit(.ansi))
}
Expand Down Expand Up @@ -280,6 +291,7 @@
) throws -> Int {
guard socket != nil else { throw TeradataWireError.connectionFailed("not connected") }
let kinds = TeradataStatementKind.kinds(in: sql)
let movesDatabase = TeradataStatementKind.mayMoveDefaultDatabase(sql)
let requestNumber = nextRequest()
let body = TeradataMessages.optionsParcel(returnStatementInfo: false).encoded()
+ TeradataMessages.requestParcel(sql: sql).encoded()
Expand All @@ -303,7 +315,7 @@
case .refused(let error):
guard case .server(let code, _)? = error as? TeradataWireError else {
// Refused by its reader rather than the server, so the request ran.
socketLock.withLock { _transaction.ran(kinds) }
recordRan(kinds, movesDatabase: movesDatabase)
throw error
}
let abortRolledBack = socketLock.withLock { () -> Bool in
Expand All @@ -316,15 +328,22 @@
}
throw error
case .outputEnded where stopped:
socketLock.withLock { _transaction.ran(kinds) }
recordRan(kinds, movesDatabase: movesDatabase)
throw TeradataWireError.cancelled
case .answered, .outputEnded:
socketLock.withLock { _transaction.ran(kinds) }
recordRan(kinds, movesDatabase: movesDatabase)
try reader.finish(onColumns: onColumns)
return reader.activityCount
}
}

private func recordRan(_ kinds: [TeradataStatementKind], movesDatabase: Bool) {
socketLock.withLock {
_transaction.ran(kinds)
if movesDatabase { _defaultDatabase = nil }
}
}

private func exchange(
_ body: [UInt8],
requestNumber: UInt32,
Expand Down Expand Up @@ -690,7 +709,7 @@
let sockaddrIn = addr.withMemoryRebound(to: sockaddr_in.self, capacity: 1) { $0.pointee }
var inAddr = sockaddrIn.sin_addr
guard inet_ntop(AF_INET, &inAddr, &buffer, socklen_t(INET_ADDRSTRLEN)) != nil else { return nil }
return String(cString: buffer)

Check warning on line 712 in apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift

View workflow job for this annotation

GitHub Actions / macos / macOS Tests

'init(cString:)' is deprecated: Use String(decoding: array, as: UTF8.self) instead, after truncating the null termination. [#DeprecatedDeclaration]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ enum TeradataStatementKind: Equatable {
TeradataRequestTokens.statements(in: request).map { kind(of: $0[...]) }
}

// `DATABASE x`, `SET SESSION DATABASE x` and its `SS` abbreviation move the session's default database, and a
// macro or procedure can hold one where the request text does not show it.
static func mayMoveDefaultDatabase(_ request: String) -> Bool {
TeradataRequestTokens.statements(in: request).contains { statement in
let words = statement.prefix(3).map(\.word)
switch words.first {
case "DATABASE"?, "EXEC"?, "EXECUTE"?, "CALL"?:
return true
case "SET"?:
return words.count > 2 && words[1] == "SESSION" && words[2] == "DATABASE"
case "SS"?:
return words.count > 1 && words[1] == "DATABASE"
default:
return false
}
}
}

private static let readKeywords: Set<String> = ["SELECT", "SEL", "HELP", "SHOW", "EXPLAIN"]
private static let writeKeywords: Set<String> = ["INSERT", "INS", "UPDATE", "UPD", "DELETE", "DEL", "MERGE"]
private static let lockTypes: Set<String> = ["ACCESS", "READ", "WRITE", "EXCLUSIVE", "SHARE", "CHECKSUM", "LOAD"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct SQLCatalogEffectClassifierTests {
#expect(SQLCatalogEffectClassifier.effect(of: "rollback", grammar: grammar("MySQL")).endsTransaction)
#expect(SQLCatalogEffectClassifier.effect(of: "ROLLBACK TO SAVEPOINT a", grammar: grammar("PostgreSQL")) == .none)
#expect(SQLCatalogEffectClassifier.effect(of: "BEGIN", grammar: grammar("PostgreSQL")) == .none)
#expect(SQLCatalogEffectClassifier.effect(of: "ET", grammar: grammar("Teradata")).endsTransaction)
}

@Test("Code the text cannot show refreshes every kind")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,65 @@ struct TeradataTransactionStatementTests {
#expect(throws: TeradataWireError.self) { try tera.switchDatabase(to: "hr") }
#expect(try TeradataWire.sentSQL(teraTransport) == [TeradataSchemaQueries.setDatabase("sales"), "BEGIN TRANSACTION"])
}

@Test("Inside an open transaction a switch to the database the session is on sends nothing")
func switchToTheCurrentDatabaseInsideATransaction() throws {
let (ansi, ansiTransport) = TeradataWire.connection(
[TeradataWire.finished, TeradataWire.finished, TeradataWire.finished], mode: .ansi
)
try ansi.switchDatabase(to: "sales")
_ = try ansi.execute("UPDATE sales.t SET a = 1")

try ansi.switchDatabase(to: "SALES")

#expect(try TeradataWire.sentSQL(ansiTransport) == [
TeradataSchemaQueries.setDatabase("sales"), "COMMIT WORK", "UPDATE sales.t SET a = 1",
])
#expect(ansi.transactionState == .open)

let (tera, teraTransport) = TeradataWire.connection([TeradataWire.finished, TeradataWire.finished], mode: .tera)
try tera.switchDatabase(to: "sales")
try tera.beginTransaction()
try tera.switchDatabase(to: "sales")
#expect(try TeradataWire.sentSQL(teraTransport) == [TeradataSchemaQueries.setDatabase("sales"), "BEGIN TRANSACTION"])
}

@Test(
"A request that moves the default database leaves the next switch inside a transaction refused",
arguments: ["DATABASE hr", "SET SESSION DATABASE hr", "SS DATABASE hr", "EXEC sales.refresh", "CALL sales.load()"]
)
func userMovedDatabaseIsNotTrusted(_ statement: String) throws {
let (ansi, ansiTransport) = TeradataWire.connection(
[TeradataWire.finished, TeradataWire.finished, TeradataWire.finished], mode: .ansi
)
try ansi.switchDatabase(to: "sales")
_ = try ansi.execute(statement)

#expect(throws: TeradataWireError.self) { try ansi.switchDatabase(to: "sales") }
#expect(try TeradataWire.sentSQL(ansiTransport).count == 3)
}
}

struct TeradataDefaultDatabaseStatementTests {
@Test(
"Statements that can move the default database",
arguments: [
("DATABASE hr", true),
("database \"hr\";", true),
("SET SESSION DATABASE hr", true),
("ss database hr", true),
("EXEC sales.refresh", true),
("CALL sales.load()", true),
("SELECT 1; DATABASE hr", true),
("SET SESSION DATEFORM = ANSIDATE", false),
("SELECT DATABASE", false),
("CREATE DATABASE hr FROM dbc AS PERM = 0", false),
("COMMENT ON DATABASE hr AS 'x'", false),
]
)
func mayMoveDefaultDatabase(_ request: String, _ moves: Bool) {
#expect(TeradataStatementKind.mayMoveDefaultDatabase(request) == moves)
}
}

struct TeradataUnreadableColumnTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ extension TeradataPluginDriver {
)
}

// Teradata has no TRUNCATE; DELETE ... ALL is its documented way to empty a table, and it has no CASCADE.
func truncateTableStatements(table: String, schema: String?, cascade: Bool) -> [String]? {
["DELETE FROM \(TeradataSchemaQueries.qualifiedName(database: effectiveDatabase(schema), table: table)) ALL"]
}

func createViewTemplate() -> String? {
"CREATE VIEW view_name AS\nSELECT column1, column2\nFROM table_name\nWHERE condition;"
}
Expand Down
11 changes: 9 additions & 2 deletions apple/TablePro/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
guard QuitApproval.approve() else {
guard let plan = QuitApproval.approve() else {
AppRelauncher.shared.terminationWasCancelled()
return .terminateCancel
}

Task {
// First, while the drivers are open: a debug stop rolls back the routine's transaction.
// Before anything the app could not take back if it stays open.
guard await QuitApproval.commitBeforeQuitting(plan) else {
NSApp.reply(toApplicationShouldTerminate: false)
AppRelauncher.shared.terminationWasCancelled()
return
}
RunningWorkRegistry.shared.cancel(plan.work)
// While the drivers are open: a debug stop rolls back the routine's transaction.
await AppModuleTermination.stopModuleWork()
await MCPServerManager.shared.stop()
await DatabaseManager.shared.runPostDisconnectScriptsBeforeQuit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ private struct PreparedStatement: @unchecked Sendable {
let anchor: StatementAnchor?
/// Where the script was cut after this statement, which a replan of the rest starts from.
let statement: SQLStatementScanner.ExecutableStatement?
/// Whether this is the script's own `COMMIT`, read from the text before the lease is taken so
/// the run never lexes inside it.
// Read from the text before the lease is taken, so the run never lexes inside it.
let isCommitPoint: Bool
let endsItsTransaction: Bool
let closesTransaction: Bool
let movesDatabase: Bool
}

// What a run sends: the rest is cut again from the script once a statement changes how the session reads literals.
Expand Down Expand Up @@ -441,7 +443,8 @@ extension QueryExecutionCoordinator {
)
let outcome = run.outcome
let sent = running.prepared
let sessionNotice = run.plan.readsSessionEndState ? run.sessionState.openTransactionNotice : nil
let readsEndState = run.plan.readsSessionEndState || run.plan == .autocommit
let sessionNotice = readsEndState ? run.sessionState.openTransactionNotice : nil

let ranStatements: [String]
switch outcome {
Expand Down Expand Up @@ -544,6 +547,8 @@ extension QueryExecutionCoordinator {
: SQLParameterExtractor.convertToNativeStyle(sql: sql, parameters: parameters, style: style, grammar: grammar)
let executableSQL = conversion?.sql ?? sql
let bounded = resolveStatement(sql: executableSQL, tabType: tabType, bypassLimit: bypassRowLimit)
let family = TransactionEngineFamily.of(parent.connection.type)
let definitionEnds = family.definitionEndsTransaction
return PreparedStatement(
originalSQL: sql,
executableSQL: executableSQL,
Expand All @@ -552,7 +557,10 @@ extension QueryExecutionCoordinator {
rowCap: bounded.rowCap,
anchor: StatementAnchor(statement),
statement: statement,
isCommitPoint: BatchCommitStatement.matches(sql, grammar: grammar)
isCommitPoint: BatchCommitStatement.matches(sql, grammar: grammar),
endsItsTransaction: definitionEnds && AutocommitOnlyStatement.matches(sql, family: family, grammar: grammar),
closesTransaction: definitionEnds && TransactionEndStatement.matches(sql, grammar: grammar),
movesDatabase: AutocommitOnlyStatement.movesDatabase(sql, family: family, grammar: grammar)
)
}

Expand All @@ -571,6 +579,7 @@ extension QueryExecutionCoordinator {
let failureOutput = ServerOutputBox()
let commitOutput = ServerOutputBox()
let databaseType = parent.connection.type
let definitionEnds = TransactionEngineFamily.of(databaseType).definitionEndsTransaction
let safeModeIsReadOnly = parent.runsAtReadOnly
let grammar = parent.lexicalGrammar
let statementTexts = prepared.map(\.sentSQL)
Expand Down Expand Up @@ -601,6 +610,9 @@ extension QueryExecutionCoordinator {
gate: self.claimGate(for: claim),
failureSQL: \.executableSQL,
isCommitPoint: \.isCommitPoint,
endsItsTransaction: \.endsItsTransaction,
closesTransaction: \.closesTransaction,
movesDatabase: \.movesDatabase,
replanned: { ran in
running.replanned(
after: ran,
Expand Down Expand Up @@ -629,14 +641,15 @@ extension QueryExecutionCoordinator {
let endState: PluginSessionTransactionState = asksEndState
? await driver.heldSessionTransactionState()
: .unknown
// An unwrapped run can still leave a transaction open: a Teradata ANSI session holds its DDL in one.
let failedState: PluginSessionTransactionState = sessionPlan == .autocommit && !outcome.isCompleted
// An unwrapped run can still leave a transaction open: a Teradata ANSI session holds its DDL in one, completed or not.
let readsHeldWork = sessionPlan == .autocommit && (!outcome.isCompleted || definitionEnds)
let heldState: PluginSessionTransactionState = readsHeldWork
? (asksEndState ? endState : await driver.heldSessionTransactionState())
: .idle
return MultiStatementRun(
outcome: outcome,
plan: sessionPlan,
sessionState: failedState,
sessionState: heldState,
startState: startState,
endState: endState
)
Expand Down Expand Up @@ -931,7 +944,10 @@ extension QueryExecutionCoordinator {
.flatMap { anchors.indices.contains($0) ? anchors[$0] : nil }

let failedStatementSQL = report.failedSQL
reportOperation(kind: .queryBatch, claim: claim, outcome: .failed(reason: errorDescription))
// A run stopped to wait for a commit has no server error, so the notification carries the report.
reportOperation(
kind: .queryBatch, claim: claim, outcome: .failed(reason: errorDescription.isEmpty ? report.message : errorDescription)
)
recordExecutedStatements(
history,
tabId: tabId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,19 @@ extension RowEditingCoordinator {
}

if clearTableOps {
/// On an engine whose DDL commits as it runs, a TRUNCATE before the failure has
/// already emptied its table, and nothing reports which ones ran. Queuing them
/// again would empty the table a second time on the next Save, rows added since
/// included, so they are left for the user to stage again.
// The executor throws every refusal before its first statement.
let ranNothing = error is any ManualCommitRefusing
// Where DDL commits as it runs a failed save may have emptied a table, and a requeued truncate empties it again.
let ddlRolledBack = DatabaseManager.shared.driver(for: conn.id)?.supportsTransactionalDDL ?? false
restorePendingTableOperations(
connectionId: conn.id,
truncates: ddlRolledBack ? truncatedTables : [],
truncates: ranNothing || ddlRolledBack ? truncatedTables : [],
deletes: deletedTables,
options: capturedOptions
)
reconcileDropsAfterFailedSave(deletedTables, scope: scope)
if !ranNothing {
reconcileDropsAfterFailedSave(deletedTables, scope: scope)
}
}

failSave(
Expand Down
Loading
Loading