1
1
public struct PostgreSQLInsert : SQLInsert {
2
2
/// See `SQLInsert`.
3
3
public static func insert( _ table: PostgreSQLTableIdentifier ) -> PostgreSQLInsert {
4
- return . init( insert : . insert ( table) , returning: [ ] )
4
+ return self . init ( table : table, columns : [ ] , values : [ ] , upsert : nil , returning: [ ] )
5
5
}
6
6
7
7
/// See `SQLInsert`.
@@ -16,37 +16,36 @@ public struct PostgreSQLInsert: SQLInsert {
16
16
/// See `SQLInsert`.
17
17
public typealias Upsert = PostgreSQLUpsert
18
18
19
- /// Root insert statement.
20
- private var insert : GenericSQLInsert < TableIdentifier , ColumnIdentifier , Expression , Upsert >
21
-
22
- /// `RETURNING *`
23
- public var returning : [ PostgreSQLSelectExpression ]
24
-
19
+ /// Table to insert into.
20
+ public var table : TableIdentifier
25
21
/// See `SQLInsert`.
26
- public var columns : [ PostgreSQLColumnIdentifier ] {
27
- get { return insert. columns }
28
- set { insert. columns = newValue }
29
- }
22
+ public var columns : [ PostgreSQLColumnIdentifier ]
30
23
31
24
/// See `SQLInsert`.
32
- public var values : [ [ PostgreSQLExpression ] ] {
33
- get { return insert. values }
34
- set { insert. values = newValue}
35
- }
25
+ public var values : [ [ PostgreSQLExpression ] ]
36
26
37
- /// See `SQLInsert` .
38
- public var upsert : PostgreSQLUpsert ? {
39
- get { return insert . upsert }
40
- set { insert . upsert = newValue }
41
- }
27
+ /// Optional "upsert" condition .
28
+ public var upsert : PostgreSQLUpsert ?
29
+
30
+ /// `RETURNING *`
31
+ public var returning : [ PostgreSQLSelectExpression ]
42
32
43
33
/// See `SQLSerializable`.
44
34
public func serialize( _ binds: inout [ Encodable ] ) -> String {
45
- if returning. isEmpty {
46
- return insert. serialize ( & binds)
47
- } else {
48
- return insert. serialize ( & binds) + " RETURNING " + returning. serialize ( & binds)
35
+ var sql : [ String ] = [ ]
36
+ sql. append ( " INSERT INTO " )
37
+ sql. append ( table. serialize ( & binds) )
38
+ sql. append ( " ( " + columns. serialize ( & binds) + " ) " )
39
+ sql. append ( " VALUES " )
40
+ sql. append ( values. map { " ( " + $0. serialize ( & binds) + " ) " } . joined ( separator: " , " ) )
41
+ if let upsert = upsert {
42
+ sql. append ( upsert. serialize ( & binds) )
43
+ }
44
+ if !returning. isEmpty {
45
+ sql. append ( " RETURNING " )
46
+ sql. append ( returning. serialize ( & binds) )
49
47
}
48
+ return sql. joined ( separator: " " )
50
49
}
51
50
}
52
51
0 commit comments