-
-
Notifications
You must be signed in to change notification settings - Fork 744
/
Copy pathOptional.swift
106 lines (96 loc) · 2.96 KB
/
Optional.swift
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
// Import C SQLite functions
#if GRDBCIPHER
import SQLCipher
#elseif SWIFT_PACKAGE
import GRDBSQLite
#elseif !GRDBCUSTOMSQLITE && !GRDBCIPHER
import SQLite3
#endif
extension Optional: StatementBinding where Wrapped: StatementBinding {
public func bind(to sqliteStatement: SQLiteStatement, at index: CInt) -> CInt {
switch self {
case .none:
return sqlite3_bind_null(sqliteStatement, index)
case let .some(value):
return value.bind(to: sqliteStatement, at: index)
}
}
}
extension Optional: SQLExpressible where Wrapped: SQLExpressible {
public var sqlExpression: SQLExpression {
switch self {
case .none:
return .null
case let .some(value):
return value.sqlExpression
}
}
}
extension Optional: SQLOrderingTerm where Wrapped: SQLOrderingTerm {
public var sqlOrdering: SQLOrdering {
switch self {
case .none:
return .expression(.null)
case let .some(value):
return value.sqlOrdering
}
}
}
extension Optional: SQLSelectable where Wrapped: SQLSelectable {
public var sqlSelection: SQLSelection {
switch self {
case .none:
return .expression(.null)
case let .some(value):
return value.sqlSelection
}
}
}
extension Optional: SQLSpecificExpressible where Wrapped: SQLSpecificExpressible { }
extension Optional: DatabaseValueConvertible where Wrapped: DatabaseValueConvertible {
public var databaseValue: DatabaseValue {
switch self {
case .none:
return .null
case let .some(value):
return value.databaseValue
}
}
public static func fromMissingColumn() -> Self? {
.some(.none) // success
}
public static func fromDatabaseValue(_ dbValue: DatabaseValue) -> Self? {
if let value = Wrapped.fromDatabaseValue(dbValue) {
// Valid value
return value
} else if dbValue.isNull {
// NULL
return .some(.none)
} else {
// Invalid value
return .none
}
}
}
extension Optional: StatementColumnConvertible where Wrapped: StatementColumnConvertible {
@inline(__always)
@inlinable
public static func fromStatement(_ sqliteStatement: SQLiteStatement, atUncheckedIndex index: CInt) -> Self? {
if let value = Wrapped.fromStatement(sqliteStatement, atUncheckedIndex: index) {
// Valid value
return value
} else if sqlite3_column_type(sqliteStatement, index) == SQLITE_NULL {
// NULL
return .some(.none)
} else {
// Invalid value
return .none
}
}
public init?(sqliteStatement: SQLiteStatement, index: CInt) {
guard let value = Wrapped(sqliteStatement: sqliteStatement, index: index) else {
return nil
}
self = .some(value)
}
}