-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathSQLiteFunctionDecoder.swift
More file actions
101 lines (87 loc) · 2.92 KB
/
SQLiteFunctionDecoder.swift
File metadata and controls
101 lines (87 loc) · 2.92 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
import Foundation
#if GRDBCIPHER
import SQLCipher
#else
import GRDBSQLite
#endif
import StructuredQueriesCore
@usableFromInline
struct SQLiteFunctionDecoder: QueryDecoder {
@usableFromInline
let argumentCount: Int32
@usableFromInline
let arguments: UnsafeMutablePointer<OpaquePointer?>?
@usableFromInline
var currentIndex: Int32 = 0
@usableFromInline
init(argumentCount: Int32, arguments: UnsafeMutablePointer<OpaquePointer?>?) {
self.argumentCount = argumentCount
self.arguments = arguments
}
@inlinable
mutating func next() {
currentIndex = 0
}
@inlinable
mutating func decode(_ columnType: [UInt8].Type) throws -> [UInt8]? {
defer { currentIndex += 1 }
precondition(argumentCount > currentIndex)
let value = arguments?[Int(currentIndex)]
guard sqlite3_value_type(value) != SQLITE_NULL else { return nil }
if let blob = sqlite3_value_blob(value) {
let count = Int(sqlite3_value_bytes(value))
let buffer = UnsafeRawBufferPointer(start: blob, count: count)
return [UInt8](buffer)
} else {
return []
}
}
@inlinable
mutating func decode(_ columnType: Bool.Type) throws -> Bool? {
try decode(Int64.self).map { $0 != 0 }
}
@usableFromInline
mutating func decode(_ columnType: Date.Type) throws -> Date? {
guard let iso8601String = try decode(String.self) else { return nil }
return try Date(iso8601String: iso8601String)
}
@inlinable
mutating func decode(_ columnType: Double.Type) throws -> Double? {
defer { currentIndex += 1 }
precondition(argumentCount > currentIndex)
let value = arguments?[Int(currentIndex)]
guard sqlite3_value_type(value) != SQLITE_NULL else { return nil }
return sqlite3_value_double(value)
}
@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 }
precondition(argumentCount > currentIndex)
let value = arguments?[Int(currentIndex)]
guard sqlite3_value_type(value) != SQLITE_NULL else { return nil }
return sqlite3_value_int64(value)
}
@inlinable
mutating func decode(_ columnType: String.Type) throws -> String? {
defer { currentIndex += 1 }
precondition(argumentCount > currentIndex)
let value = arguments?[Int(currentIndex)]
guard sqlite3_value_type(value) != SQLITE_NULL else { return nil }
return String(cString: sqlite3_value_text(value))
}
@inlinable
mutating func decode(_ columnType: UInt64.Type) throws -> UInt64? {
guard let n = try decode(Int64.self) else { return nil }
guard n >= 0 else { throw UInt64OverflowError(signedInteger: n) }
return UInt64(n)
}
@usableFromInline
mutating func decode(_ columnType: UUID.Type) throws -> UUID? {
guard let uuidString = try decode(String.self) else { return nil }
return UUID(uuidString: uuidString)
}
}