forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLBind.swift
41 lines (36 loc) · 1.21 KB
/
PostgreSQLBind.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
/// Representable as a `PostgreSQLExpression`.
public protocol PostgreSQLExpressionRepresentable {
/// Self converted to a `PostgreSQLExpression`.
var postgreSQLExpression: PostgreSQLExpression { get }
}
/// PostgreSQL specific `SQLBind`.
public struct PostgreSQLBind: SQLBind {
/// See `SQLBind`.
public static func encodable<E>(_ value: E) -> PostgreSQLBind
where E: Encodable
{
if let expr = value as? PostgreSQLExpressionRepresentable {
return self.init(value: .expression(expr.postgreSQLExpression))
} else {
return self.init(value: .encodable(value))
}
}
/// Specific type of bind.
public enum Value {
/// A `PostgreSQLExpression`.
case expression(PostgreSQLExpression)
/// A bound `Encodable` type.
case encodable(Encodable)
}
/// Bind value.
public var value: Value
/// See `SQLSerializable`.
public func serialize(_ binds: inout [Encodable]) -> String {
switch value {
case .expression(let expr): return expr.serialize(&binds)
case .encodable(let value):
binds.append(value)
return "$\(binds.count)"
}
}
}