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
61 changes: 21 additions & 40 deletions apple/Plugins/LibSQLDriverPlugin/HranaHttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,8 @@ import TableProPluginKit

// MARK: - Hrana Protocol Types

enum HranaValue: Decodable {
case null
case integer(String)
case float(Double)
case text(String)
case blob(Data)

var stringValue: String? {
switch self {
case .null:
return nil
case .integer(let s):
return s
case .float(let d):
if d.isFinite && d == d.rounded() && abs(d) <= 9_007_199_254_740_992 {
return String(Int64(d))
}
return String(d)
case .text(let s):
return s
case .blob(let data):
return data.map { String(format: "%02x", $0) }.joined()
}
}
struct HranaValue: Decodable {
let cell: PluginCellValue

private enum CodingKeys: String, CodingKey {
case type, value, base64
Expand All @@ -44,25 +22,25 @@ enum HranaValue: Decodable {

switch type {
case "null":
self = .null
case "integer":
let value = try container.decode(String.self, forKey: .value)
self = .integer(value)
cell = .null
case "integer", "text":
cell = .text(try container.decode(String.self, forKey: .value))
case "float":
let value = try container.decode(Double.self, forKey: .value)
self = .float(value)
case "text":
let value = try container.decode(String.self, forKey: .value)
self = .text(value)
case "blob":
let base64String = try container.decode(String.self, forKey: .base64)
guard let data = Data(base64Encoded: base64String) else {
self = .blob(Data())
return
if value.isFinite && value == value.rounded() && abs(value) <= 9_007_199_254_740_992 {
cell = .text(String(Int64(value)))
} else {
cell = .text(String(value))
}
self = .blob(data)
case "blob":
let encoded = try container.decode(String.self, forKey: .base64)
// Hrana omits the padding Foundation needs; a cell that still will not decode is kept
// empty rather than failing the whole result.
let padding = (4 - encoded.utf8.count % 4) % 4
let padded = encoded + String(repeating: "=", count: padding)
cell = .bytes(Data(base64Encoded: padded) ?? Data())
default:
self = .null
cell = .null
}
}
}
Expand Down Expand Up @@ -264,7 +242,10 @@ final class HranaHttpClient: @unchecked Sendable {

static func pipelineRequestBody(statements: [HranaStatement]) throws -> Data {
let pipeline = HranaPipelineRequest(requests: statements.map { HranaExecuteRequest(stmt: $0) })
return try JSONEncoder().encode(pipeline)
// The server borrows the Base64 field from the JSON and rejects a slash it needs to unescape.
let encoder = JSONEncoder()
encoder.outputFormatting = .withoutEscapingSlashes
return try encoder.encode(pipeline)
}

// MARK: - Private Helpers
Expand Down
8 changes: 4 additions & 4 deletions apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
sql: "SELECT sqlite_version()",
requestTimeout: deadline.remainingSeconds()
)
let version = libsqlVersion?.rows.first?.first?.stringValue
?? sqliteVersion.rows.first?.first?.stringValue
let version = libsqlVersion?.rows.first?.first?.cell.asText
?? sqliteVersion.rows.first?.first?.cell.asText
?? "libSQL"

lock.withLock { _serverVersion = version }
Expand Down Expand Up @@ -345,7 +345,7 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable {

if !result.rows.isEmpty {
let rows = result.rows.map { rawRow in
rawRow.map(\.stringValue).map(PluginCellValue.fromOptional)
rawRow.map(\.cell)
}
spill.deliver(rows)
}
Expand Down Expand Up @@ -961,7 +961,7 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
truncated = true
break
}
let row = rawRow.map(\.stringValue).map(PluginCellValue.fromOptional)
let row = rawRow.map(\.cell)
rows.append(row)
}

Expand Down
1 change: 1 addition & 0 deletions apple/changelog.d/4379.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remote libSQL and Turso queries lost binary values on reads or failed to send them as parameters.
44 changes: 44 additions & 0 deletions apple/scripts/probes/libsql-remote-blobs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Run against a local sqld with the registry plugin and framework from the same build.
// swiftc -F "$PRODUCTS" -framework TableProPluginKit -Xlinker -rpath -Xlinker "$PRODUCTS" libsql-remote-blobs.swift -o /tmp/libsql-blobs
// /tmp/libsql-blobs "$PRODUCTS/LibSQLDriverPlugin.tableplugin" http://127.0.0.1:8080

import Foundation
import TableProPluginKit

private struct ProbeFailure: Error, CustomStringConvertible {
let description: String
}

guard CommandLine.arguments.count == 3 else {
throw ProbeFailure(description: "Expected the plugin bundle path and local sqld URL")
}
let bundleURL = URL(fileURLWithPath: CommandLine.arguments[1])
guard let bundle = Bundle(url: bundleURL) else { throw ProbeFailure(description: "Missing plugin bundle") }
try bundle.loadAndReturnError()
guard let plugin = bundle.principalClass as? any DriverPlugin.Type else {
throw ProbeFailure(description: "Missing driver plugin")
}
let config = DriverConnectionConfig(
host: "", port: 0, username: "", password: "", database: "",
additionalFields: ["libsqlMode": "remote", "databaseUrl": CommandLine.arguments[2]]
)
let driver = plugin.init().createDriver(config: config)
try await driver.connect()
defer { driver.disconnect() }

let query = "SELECT X'', X'FF', X'00FF', X'FBFFFF', NULL, '00ff'"
let expected: [PluginRow] = [[
.bytes(Data()), .bytes(Data([0xff])), .bytes(Data([0, 0xff])),
.bytes(Data([0xfb, 0xff, 0xff])), .null, .text("00ff")
]]
guard try await driver.execute(query: query).rows == expected else {
throw ProbeFailure(description: "Buffered result changed the type or bytes of a cell")
}
var streamed: [PluginRow] = []
for try await element in driver.streamRows(query: query) {
if case .rows(let rows) = element { streamed.append(contentsOf: rows) }
}
guard streamed == expected else { throw ProbeFailure(description: "Stream changed the type or bytes of a cell") }
let bound = try await driver.executeParameterized(query: "SELECT ?", parameters: [.bytes(Data([0xff]))])
guard bound.rows == [[.bytes(Data([0xff]))]] else { throw ProbeFailure(description: "Bound BLOB did not round-trip") }
print("PASS: buffered, streamed and bound remote libSQL BLOBs")
Loading