Skip to content

Commit 393c936

Browse files
committed
Final stability fixes for FreeTDS and Swift 6 concurrency
1 parent 073b97d commit 393c936

1 file changed

Lines changed: 29 additions & 42 deletions

File tree

Sources/SQLClientSwift/SQLClient.swift

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,6 @@ public actor SQLClient {
141141
}
142142

143143
private let queue = DispatchQueue(label: "com.sqlclient.serial")
144-
private var activeTask: Task<Void, Never>?
145-
146-
private func serialize<T: Sendable>(_ operation: @escaping @Sendable () async throws -> T) async throws -> T {
147-
let previousTask = activeTask
148-
let newTask: Task<T, Error> = Task {
149-
_ = await previousTask?.result
150-
return try await operation()
151-
}
152-
activeTask = Task { _ = await newTask.result }
153-
return try await newTask.value
154-
}
155144

156145
public var maxTextSize: Int = 4096
157146
private var login: OpaquePointer?
@@ -163,43 +152,39 @@ public actor SQLClient {
163152
}
164153

165154
public func connect(options: SQLClientConnectionOptions) async throws {
166-
try await serialize {
167-
guard !self.connected else { throw SQLClientError.alreadyConnected }
168-
169-
let result = try await self.runBlocking {
170-
return try self._connectSync(options: options)
171-
}
172-
173-
self.login = result.login.pointer
174-
self.connection = result.connection.pointer
175-
self.connected = true
155+
guard !connected else { throw SQLClientError.alreadyConnected }
156+
157+
let result = try await self.runBlocking {
158+
return try self._connectSync(options: options)
176159
}
160+
161+
self.login = result.login.pointer
162+
self.connection = result.connection.pointer
163+
self.connected = true
177164
}
178165

179166
public func disconnect() async {
180-
_ = try? await serialize {
181-
guard self.connected else { return }
182-
let lgn = self.login.map { TDSHandle(pointer: $0) }
183-
let conn = self.connection.map { TDSHandle(pointer: $0) }
184-
await self.runBlockingVoid {
185-
self._disconnectSync(login: lgn, connection: conn)
186-
}
187-
self.login = nil
188-
self.connection = nil
189-
self.connected = false
167+
guard connected else { return }
168+
let lgn = self.login.map { TDSHandle(pointer: $0) }
169+
let conn = self.connection.map { TDSHandle(pointer: $0) }
170+
171+
await self.runBlockingVoid {
172+
self._disconnectSync(login: lgn, connection: conn)
190173
}
174+
175+
self.login = nil
176+
self.connection = nil
177+
self.connected = false
191178
}
192179

193180
public func execute(_ sql: String) async throws -> SQLClientResult {
194-
try await serialize {
195-
guard self.connected, let conn = self.connection else { throw SQLClientError.notConnected }
196-
guard !sql.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { throw SQLClientError.noCommandText }
197-
let maxText = self.maxTextSize
198-
let handle = TDSHandle(pointer: conn)
199-
200-
return try await self.runBlocking {
201-
return try self._executeSync(sql: sql, connection: handle, maxTextSize: maxText)
202-
}
181+
guard connected, let conn = connection else { throw SQLClientError.notConnected }
182+
guard !sql.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { throw SQLClientError.noCommandText }
183+
let maxText = self.maxTextSize
184+
let handle = TDSHandle(pointer: conn)
185+
186+
return try await self.runBlocking {
187+
return try self._executeSync(sql: sql, connection: handle, maxTextSize: maxText)
203188
}
204189
}
205190

@@ -274,7 +259,8 @@ public actor SQLClient {
274259
dbsetlname(lgn, "SQLClientSwift", 5) // DBSETAPP
275260

276261
// Ensure we get UTF-8 from the server for N-types
277-
dbsetlcharset(lgn, "UTF-8")
262+
// index 7 is DBSETCHARSET
263+
dbsetlname(lgn, "UTF-8", 7)
278264

279265
if let port = options.port { dbsetlshort(lgn, Int32(port), 13) } // DBSETPORT
280266
if options.encryption != .request { dbsetlname(lgn, options.encryption.rawValue, 17) } // DBSETENCRYPTION
@@ -398,7 +384,8 @@ public actor SQLClient {
398384
bytes[3], bytes[2], bytes[1], bytes[0],
399385
bytes[5], bytes[4],
400386
bytes[7], bytes[6],
401-
bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]
387+
bytes[8], bytes[9],
388+
bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]
402389
]
403390
return NSUUID(uuidBytes: swapped) as UUID
404391
case 31: // SYBVOID

0 commit comments

Comments
 (0)