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 @@ -26,6 +26,7 @@ private struct BatchOutput: Sendable {
let rowsAffected: Int
let errors: [PluginBatchError]
let discardedResultSetCount: Int
let supersededResultSetCount: Int
let executionTime: TimeInterval
let errorDescription: String?
var serverOutput: PluginServerOutput = .none
Expand Down Expand Up @@ -308,7 +309,8 @@ extension QueryExecutionCoordinator {
driver: DatabaseDriver,
failureOutput: ServerOutputBox
) async throws -> BatchOutput {
var combined = QueryBatchResult.empty
var earliest = QueryBatchResult.empty
var latest = QueryBatchResult.empty
var printed = BatchPrintedOutput()
for _ in 0..<max(prepared.batch.repeatCount, 1) {
try Task.checkCancellation()
Expand All @@ -331,14 +333,18 @@ extension QueryExecutionCoordinator {
}
printed.append(printedAnswer.serverOutput)
let answer = printedAnswer.answer
combined = combined.followed(by: answer)
earliest = earliest.followed(by: answer)
latest = latest.followedKeepingLatest(by: answer)
guard answer.errors.isEmpty else { break }
}
// Once the driver read past result sets, the newest ones kept are not the batch's last.
let combined = latest.discardedResultSetCount == 0 ? latest : earliest
var output = BatchOutput(
resultSets: combined.resultSets,
rowsAffected: combined.rowsAffected,
errors: combined.errors,
discardedResultSetCount: combined.discardedResultSetCount,
supersededResultSetCount: combined.supersededResultSetCount,
executionTime: combined.executionTime,
errorDescription: BatchErrorText.describe(combined.errors, batchStartLine: prepared.startLine)
)
Expand Down Expand Up @@ -558,6 +564,7 @@ extension QueryExecutionCoordinator {
}
}
for ((entry, output), queries) in zip(zip(prepared, outputs), plan.queries) {
candidate += output.supersededResultSetCount
let firstCandidate = candidate
if let queries {
for (statement, result) in zip(queries, output.resultSets) {
Expand Down Expand Up @@ -627,6 +634,8 @@ extension QueryExecutionCoordinator {
}
continue
}
// Result sets the batch let go still count toward the run's results, and are never shown.
shapes += repeatElement(true, count: output.supersededResultSetCount)
let pairs: [(sql: String?, result: QueryResult)] = mapped.map { mapped in
zip(mapped, output.resultSets).map { (sql: $0.sql as String?, result: $1) }
} ?? output.resultSets.map { (sql: nil, result: $0) }
Expand All @@ -653,7 +662,7 @@ extension QueryExecutionCoordinator {
let isPlainQuery = { (sql: String) in
QueryExecutor.qualifiesForRowCap(sql: sql, tabType: .query, databaseType: databaseType)
}
guard BatchResultMapping.mapsToStatements(
guard output.supersededResultSetCount == 0, BatchResultMapping.mapsToStatements(
entry.batch,
resultSetCount: output.resultSets.count,
hasErrors: !output.errors.isEmpty,
Expand Down
26 changes: 26 additions & 0 deletions apple/TablePro/Models/Query/QueryBatchResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct QueryBatchResult {
let errors: [PluginBatchError]
let discardedResultSetCount: Int
let executionTime: TimeInterval
// Earlier result sets `followedKeepingLatest(by:)` let go to keep later ones.
var supersededResultSetCount = 0

static let empty = QueryBatchResult(
resultSets: [],
Expand Down Expand Up @@ -49,4 +51,28 @@ struct QueryBatchResult {
executionTime: executionTime + next.executionTime
)
}

// The editor shows the last results of a run, so past the same ceiling it keeps the newest result sets.
func followedKeepingLatest(by next: QueryBatchResult) -> QueryBatchResult {
let combined = resultSets + next.resultSets
let superseded = max(combined.count - Self.keptResultSetLimit, 0)
let shifted = { (error: PluginBatchError, preceding: Int) in
PluginBatchError(
message: error.message,
code: error.code,
line: error.line,
procedure: error.procedure,
precedingResultSetCount: max(preceding - superseded, 0)
)
}
return QueryBatchResult(
resultSets: Array(combined.suffix(Self.keptResultSetLimit)),
rowsAffected: rowsAffected + next.rowsAffected,
errors: errors.map { shifted($0, $0.precedingResultSetCount) }
+ next.errors.map { shifted($0, resultSets.count + $0.precedingResultSetCount) },
discardedResultSetCount: discardedResultSetCount + next.discardedResultSetCount,
executionTime: executionTime + next.executionTime,
supersededResultSetCount: supersededResultSetCount + next.supersededResultSetCount + superseded
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// QueryBatchResultTests.swift
// TableProTests
//
// `GO n` sends a batch n times, and the editor and every external caller fold the answers into one with
// `followed(by:)`, so the ceiling, the counts and the place of each error are decided in one place.
// `GO n` sends a batch n times, and every caller folds the answers into one here, so the ceiling, the counts and
// the place of each error are decided in one place. The editor keeps the newest result sets past the ceiling.
//

import Foundation
Expand Down Expand Up @@ -51,4 +51,17 @@ struct QueryBatchResultTests {

#expect(combined.errors.map(\.precedingResultSetCount) == [3])
}

@Test("Keeping the latest, result sets past the ceiling push out the oldest and are counted as superseded")
func latestPastTheCeiling() {
let limit = QueryBatchResult.keptResultSetLimit
let combined = answer(resultSets: limit - 1).followedKeepingLatest(by: answer(resultSets: 3, errorAfter: 3))

#expect(combined.resultSets.count == limit)
#expect(combined.resultSets.last?.columns == ["c2"])
#expect(combined.resultSets.dropLast(3).last?.columns == ["c\(limit - 2)"])
#expect(combined.supersededResultSetCount == 2)
#expect(combined.discardedResultSetCount == 0)
#expect(combined.errors.map(\.precedingResultSetCount) == [limit])
}
}
3 changes: 3 additions & 0 deletions apple/TableProTests/Helpers/FakeMSSQLPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ final class FakeMSSQLPluginDriver: PluginDriverStub, @unchecked Sendable {
}
func disconnect() { disconnectCallCount += 1 }

// SQL Server moves a live session with USE, so a statement pinned to a database after a reconnect lands.
func switchDatabase(to database: String) async throws {}

func applyQueryTimeout(_ seconds: Int) async throws {
queryTimeoutValues.withLock { $0.append(seconds) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ struct ConnectionFormPortTLSTests {
#expect(coordinator.ssl.validationIssues.isEmpty)
}

@Test("PostgreSQL Verify Identity still asks for a CA certificate")
func postgresStillRequiresCA() {
@Test("PostgreSQL Verify Identity saves without a CA certificate, and Verify CA still asks for one")
func postgresNeedsACAOnlyForVerifyCA() {
let coordinator = newConnection(.postgresql)
coordinator.ssl.select(.verifyIdentity)
#expect(coordinator.ssl.validationIssues.isEmpty)

coordinator.ssl.select(.verifyCa)
#expect(!coordinator.ssl.validationIssues.isEmpty)
}
}
Loading