-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSQLiteRow+Extensions.swift
More file actions
65 lines (60 loc) · 2.45 KB
/
Copy pathSQLiteRow+Extensions.swift
File metadata and controls
65 lines (60 loc) · 2.45 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
import Foundation
public extension Dictionary where Dictionary.Key == String, Dictionary.Value == SQLiteValue {
func optionalValue<V>(for key: CodingKey) -> V? {
try? value(for: key)
}
func optionalValue<V>(for key: String) -> V? {
try? value(for: key)
}
func value<V>(for key: CodingKey) throws -> V {
try value(for: key.stringValue)
}
func value<V>(for key: String) throws -> V {
if String.self == V.self {
guard let value = self[key]?.stringValue
else { throw SQLiteError.SQLITE_MISMATCH }
return value as! V
} else if Int.self == V.self {
guard let value = self[key]?.intValue else { throw SQLiteError.SQLITE_MISMATCH }
return value as! V
} else if Bool.self == V.self {
guard let value = self[key]?.boolValue else { throw SQLiteError.SQLITE_MISMATCH }
return value as! V
} else if Double.self == V.self {
guard let value = self[key]?.doubleValue
else { throw SQLiteError.SQLITE_MISMATCH }
return value as! V
} else if Data.self == V.self {
guard let value = self[key]?.dataValue else { throw SQLiteError.SQLITE_MISMATCH }
return value as! V
} else if Date.self == V.self {
guard let date = date(from: self[key]?.stringValue)
else { throw SQLiteError.SQLITE_MISMATCH }
return date as! V
} else if Int64.self == V.self {
guard let value = self[key]?.int64Value
else { throw SQLiteError.SQLITE_MISMATCH }
return value as! V
} else if String?.self == V.self {
return self[key]?.stringValue as! V
} else if Int?.self == V.self {
return self[key]?.intValue as! V
} else if Bool?.self == V.self {
return self[key]?.boolValue as! V
} else if Double?.self == V.self {
return self[key]?.doubleValue as! V
} else if Data?.self == V.self {
return self[key]?.dataValue as! V
} else if Date?.self == V.self {
return date(from: self[key]?.stringValue) as! V
} else if Int64?.self == V.self {
return self[key]?.int64Value as! V
} else {
throw SQLiteError.SQLITE_MISMATCH
}
}
}
private func date(from string: String?) -> Date? {
guard let string else { return nil }
return PreciseDateFormatter.date(from: string)
}