|
| 1 | +import Foundation |
| 2 | +import PowerSync |
| 3 | +import StructuredQueries |
| 4 | + |
| 5 | +@usableFromInline |
| 6 | +struct PowerSyncQueryDecoder: QueryDecoder { |
| 7 | + /// The structured queries library will decode the typed struct's columns in |
| 8 | + /// the same order which it compiled the SELECT statement's column list. |
| 9 | + /// The library will call the correct typed `decode` method, we just need to keep |
| 10 | + /// track of which column index we're on. |
| 11 | + @usableFromInline |
| 12 | + var currentIndex: Int = 0 |
| 13 | + |
| 14 | + /// The PowerSync `SqlCursor` provided in the `mapper` closure. |
| 15 | + @usableFromInline |
| 16 | + var cursor: SqlCursor |
| 17 | + |
| 18 | + @usableFromInline |
| 19 | + init(cursor: SqlCursor) { |
| 20 | + self.cursor = cursor |
| 21 | + } |
| 22 | + |
| 23 | + /// Decodes a single tuple of the given type starting from the current column. |
| 24 | + /// |
| 25 | + /// - Parameter columnTypes: The types to decode as. |
| 26 | + /// - Returns: A tuple of the requested types. |
| 27 | + @inlinable |
| 28 | + @inline(__always) |
| 29 | + public mutating func decodeColumns<each T: QueryRepresentable>( |
| 30 | + _: (repeat each T).Type |
| 31 | + ) throws -> (repeat (each T).QueryOutput) { |
| 32 | + try (repeat (each T)(decoder: &self).queryOutput) |
| 33 | + } |
| 34 | + |
| 35 | + @inlinable |
| 36 | + mutating func decode(_: [UInt8].Type) throws -> [UInt8]? { |
| 37 | + defer { currentIndex += 1 } |
| 38 | + // TODO: blob support |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + @inlinable |
| 43 | + mutating func decode(_: Bool.Type) throws -> Bool? { |
| 44 | + try decode(Int64.self).map { $0 != 0 } |
| 45 | + } |
| 46 | + |
| 47 | + @inlinable |
| 48 | + mutating func decode(_: Date.Type) throws -> Date? { |
| 49 | + try decode(String.self).map { |
| 50 | + let formatter = ISO8601DateFormatter() |
| 51 | + guard let date = formatter.date(from: $0) else { |
| 52 | + throw InvalidDate() |
| 53 | + } |
| 54 | + return date |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @inlinable |
| 59 | + mutating func decode(_: Double.Type) throws -> Double? { |
| 60 | + defer { currentIndex += 1 } |
| 61 | + return cursor.getDoubleOptional(index: currentIndex) |
| 62 | + } |
| 63 | + |
| 64 | + @inlinable |
| 65 | + mutating func decode(_: Int.Type) throws -> Int? { |
| 66 | + try decode(Int64.self).map(Int.init) |
| 67 | + } |
| 68 | + |
| 69 | + @inlinable |
| 70 | + mutating func decode(_: Int64.Type) throws -> Int64? { |
| 71 | + defer { currentIndex += 1 } |
| 72 | + return cursor.getInt64Optional(index: currentIndex) |
| 73 | + } |
| 74 | + |
| 75 | + @inlinable |
| 76 | + mutating func decode(_: String.Type) throws -> String? { |
| 77 | + defer { currentIndex += 1 } |
| 78 | + return cursor.getStringOptional(index: currentIndex) |
| 79 | + } |
| 80 | + |
| 81 | + @inlinable |
| 82 | + mutating func decode(_: UUID.Type) throws -> UUID? { |
| 83 | + guard let uuidString = try decode(String.self) else { return nil } |
| 84 | + guard let uuid = UUID(uuidString: uuidString) else { throw InvalidUUID() } |
| 85 | + return uuid |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +@usableFromInline |
| 90 | +struct InvalidUUID: Error { |
| 91 | + @usableFromInline |
| 92 | + init() {} |
| 93 | +} |
| 94 | + |
| 95 | +@usableFromInline |
| 96 | +struct InvalidDate: Error { |
| 97 | + @usableFromInline |
| 98 | + init() {} |
| 99 | +} |
0 commit comments