-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathSQLiteQueryDecoder.swift
More file actions
83 lines (70 loc) · 2.28 KB
/
SQLiteQueryDecoder.swift
File metadata and controls
83 lines (70 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import Foundation
import GRDBSQLite
import StructuredQueriesCore
@usableFromInline
struct SQLiteQueryDecoder: QueryDecoder {
@usableFromInline
let statement: OpaquePointer
@usableFromInline
var currentIndex: Int32 = 0
@usableFromInline
init(statement: OpaquePointer) {
self.statement = statement
}
@inlinable
mutating func next() {
currentIndex = 0
}
@inlinable
mutating func decode(_ columnType: [UInt8].Type) throws -> [UInt8]? {
defer { currentIndex += 1 }
guard sqlite3_column_type(statement, currentIndex) != SQLITE_NULL else { return nil }
return [UInt8](
UnsafeRawBufferPointer(
start: sqlite3_column_blob(statement, currentIndex),
count: Int(sqlite3_column_bytes(statement, currentIndex))
)
)
}
@inlinable
mutating func decode(_ columnType: Bool.Type) throws -> Bool? {
try decode(Int64.self).map { $0 != 0 }
}
@inlinable
mutating func decode(_ columnType: Date.Type) throws -> Date? {
try decode(String.self).map { try Date(iso8601String: $0) }
}
@inlinable
mutating func decode(_ columnType: Double.Type) throws -> Double? {
defer { currentIndex += 1 }
guard sqlite3_column_type(statement, currentIndex) != SQLITE_NULL else { return nil }
return sqlite3_column_double(statement, currentIndex)
}
@inlinable
mutating func decode(_ columnType: Int.Type) throws -> Int? {
try decode(Int64.self).map(Int.init)
}
@inlinable
mutating func decode(_ columnType: Int64.Type) throws -> Int64? {
defer { currentIndex += 1 }
guard sqlite3_column_type(statement, currentIndex) != SQLITE_NULL else { return nil }
return sqlite3_column_int64(statement, currentIndex)
}
@inlinable
mutating func decode(_ columnType: String.Type) throws -> String? {
defer { currentIndex += 1 }
guard sqlite3_column_type(statement, currentIndex) != SQLITE_NULL else { return nil }
return String(cString: sqlite3_column_text(statement, currentIndex))
}
@inlinable
mutating func decode(_ columnType: UUID.Type) throws -> UUID? {
guard let uuidString = try decode(String.self) else { return nil }
guard let uuid = UUID(uuidString: uuidString) else { throw InvalidUUID() }
return uuid
}
}
@usableFromInline
struct InvalidUUID: Error {
@usableFromInline
init() {}
}