forked from pointfreeco/sqlite-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteQueryDecoder.swift
More file actions
164 lines (145 loc) · 4.35 KB
/
Copy pathSQLiteQueryDecoder.swift
File metadata and controls
164 lines (145 loc) · 4.35 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
public import Foundation
public import GRDBSQLite
public import StructuredQueriesCore
#if !StrictDecoding
import ConcurrencyExtras
import IssueReporting
#endif
@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(QueryDecodingError) -> [UInt8]? {
switch sqlite3_column_type(statement, currentIndex) {
case SQLITE_NULL:
currentIndex += 1
return nil
case SQLITE_BLOB:
break
default:
try reportTypeMismatch([UInt8].self)
}
defer { currentIndex += 1 }
return [UInt8](
UnsafeRawBufferPointer(
start: sqlite3_column_blob(statement, currentIndex),
count: Int(sqlite3_column_bytes(statement, currentIndex))
)
)
}
@inlinable
mutating func decode(_ columnType: Bool.Type) throws(QueryDecodingError) -> Bool? {
try decode(Int64.self).map { $0 != 0 }
}
@inlinable
mutating func decode(_ columnType: Date.Type) throws(QueryDecodingError) -> Date? {
guard let iso8601String = try decode(String.self) else { return nil }
do {
return try Date(iso8601String: iso8601String) }
} catch {
throw .other(error)
}
}
@inlinable
mutating func decode(_ columnType: Double.Type) throws(QueryDecodingError) -> Double? {
switch sqlite3_column_type(statement, currentIndex) {
case SQLITE_NULL:
currentIndex += 1
return nil
case SQLITE_FLOAT:
break
default:
try reportTypeMismatch(Double.self)
}
defer { currentIndex += 1 }
return sqlite3_column_double(statement, currentIndex)
}
@inlinable
mutating func decode(_ columnType: Int.Type) throws(QueryDecodingError) -> Int? {
try decode(Int64.self).map(Int.init)
}
@inlinable
mutating func decode(_ columnType: Int64.Type) throws(QueryDecodingError) -> Int64? {
switch sqlite3_column_type(statement, currentIndex) {
case SQLITE_NULL:
currentIndex += 1
return nil
case SQLITE_INTEGER:
break
default:
try reportTypeMismatch(Int64.self)
}
defer { currentIndex += 1 }
return sqlite3_column_int64(statement, currentIndex)
}
@inlinable
mutating func decode(_ columnType: String.Type) throws(QueryDecodingError) -> String? {
switch sqlite3_column_type(statement, currentIndex) {
case SQLITE_NULL:
currentIndex += 1
return nil
case SQLITE_TEXT:
break
default:
try reportTypeMismatch(String.self)
}
defer { currentIndex += 1 }
return String(cString: sqlite3_column_text(statement, currentIndex))
}
@inlinable
mutating func decode(_ columnType: UInt64.Type) throws(QueryDecodingError) -> UInt64? {
guard let n = try decode(Int64.self) else { return nil }
guard n >= 0 else { throw .other(UInt64OverflowError(signedInteger: n)) }
return UInt64(n)
}
@inlinable
mutating func decode(_ columnType: UUID.Type) throws(QueryDecodingError) -> UUID? {
guard let uuidString = try decode(String.self) else { return nil }
guard let uuid = UUID(uuidString: uuidString) else { throw .other(InvalidUUID()) }
return uuid
}
@usableFromInline
func reportTypeMismatch(_ columnType: Any.Type) throws(QueryDecodingError) {
#if StrictDecoding
throw QueryDecodingError.typeMismatch(columnType)
#else
let sql = sqlite3_sql(statement).map { String(cString: $0) } ?? ""
let key = "\(currentIndex)|\(sql)"
guard reportedTypeMismatches.withValue({ $0.insert(key).inserted })
else { return }
let columnName = sqlite3_column_name(statement, currentIndex).map { String(cString: $0) }
reportIssue(
"""
Expected column \(currentIndex) (\((columnName ?? "").debugDescription)) to decode \
\(columnType), but found \
\(storageClassName(sqlite3_column_type(statement, currentIndex))): ...
\(sql)
"""
)
#endif
}
}
@usableFromInline
struct InvalidUUID: Error {
@usableFromInline
init() {}
}
@usableFromInline
struct UInt64OverflowError: Error {
let signedInteger: Int64
@usableFromInline
init(signedInteger: Int64) {
self.signedInteger = signedInteger
}
}