forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLInsert.swift
49 lines (40 loc) · 1.53 KB
/
PostgreSQLInsert.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
public struct PostgreSQLInsert: SQLInsert {
/// See `SQLInsert`.
public static func insert(_ table: PostgreSQLTableIdentifier) -> PostgreSQLInsert {
return .init(insert: .insert(table), returning: [])
}
/// See `SQLInsert`.
public typealias TableIdentifier = PostgreSQLTableIdentifier
/// See `SQLInsert`.
public typealias ColumnIdentifier = PostgreSQLColumnIdentifier
/// See `SQLInsert`.
public typealias Expression = PostgreSQLExpression
/// Root insert statement.
private var insert: GenericSQLInsert<TableIdentifier, ColumnIdentifier, Expression>
/// `RETURNING *`
public var returning: [PostgreSQLSelectExpression]
/// See `SQLInsert`.
public var columns: [PostgreSQLColumnIdentifier] {
get { return insert.columns }
set { insert.columns = newValue }
}
/// See `SQLInsert`.
public var values: [[PostgreSQLExpression]] {
get { return insert.values }
set { insert.values = newValue}
}
/// See `SQLSerializable`.
public func serialize(_ binds: inout [Encodable]) -> String {
if returning.isEmpty {
return insert.serialize(&binds)
} else {
return insert.serialize(&binds) + " RETURNING (" + returning.serialize(&binds) + ")"
}
}
}
extension SQLInsertBuilder where Connection.Query.Insert == PostgreSQLInsert {
public func returning(_ exprs: PostgreSQLSelectExpression...) -> Self {
insert.returning += exprs
return self
}
}