@@ -20,6 +20,62 @@ public struct PostgreSQLAlterTable: SQLAlterTable {
20
20
/// See `SQLAlterTable`.
21
21
public var constraints : [ PostgreSQLTableConstraint ]
22
22
23
+ /// DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]
24
+ /// DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]
25
+ public struct DropAction : SQLSerializable {
26
+ public enum Method {
27
+ case restrict
28
+ case cascade
29
+ }
30
+
31
+ public enum Kind {
32
+ case column
33
+ case constraint
34
+ }
35
+
36
+ public var kind : Kind
37
+
38
+ public var ifExists : Bool
39
+
40
+ public var column : PostgreSQLIdentifier
41
+
42
+ public var method : Method ?
43
+
44
+ public init (
45
+ _ kind: Kind ,
46
+ ifExists: Bool = false ,
47
+ _ column: PostgreSQLIdentifier ,
48
+ _ method: Method ? = nil
49
+ ) {
50
+ self . kind = kind
51
+ self . ifExists = ifExists
52
+ self . column = column
53
+ self . method = method
54
+ }
55
+
56
+ /// See `SQLSerializable`.
57
+ public func serialize( _ binds: inout [ Encodable ] ) -> String {
58
+ var sql : [ String ] = [ ]
59
+ sql. append ( " DROP " )
60
+ switch kind {
61
+ case . column: sql. append ( " COLUMN " )
62
+ case . constraint: sql. append ( " CONSTRAINT " )
63
+ }
64
+ if ifExists {
65
+ sql. append ( " IF EXISTS " )
66
+ }
67
+ sql. append ( column. serialize ( & binds) )
68
+ if let method = method {
69
+ switch method {
70
+ case . cascade: sql. append ( " CASCADE " )
71
+ case . restrict: sql. append ( " RESTRICT " )
72
+ }
73
+ }
74
+ return sql. joined ( separator: " " )
75
+ }
76
+ }
77
+
78
+ public var dropActions : [ DropAction ]
23
79
24
80
/// Creates a new `AlterTable`.
25
81
///
@@ -29,14 +85,17 @@ public struct PostgreSQLAlterTable: SQLAlterTable {
29
85
self . table = table
30
86
self . columns = [ ]
31
87
self . constraints = [ ]
88
+ self . dropActions = [ ]
32
89
}
33
90
34
91
/// See `SQLSerializable`.
35
92
public func serialize( _ binds: inout [ Encodable ] ) -> String {
36
93
var sql : [ String ] = [ ]
37
94
sql. append ( " ALTER TABLE " )
38
95
sql. append ( table. serialize ( & binds) )
39
- let actions = columns. map { " ADD COLUMN " + $0. serialize ( & binds) } + constraints. map { " ADD " + $0. serialize ( & binds) }
96
+ let actions = columns. map { " ADD COLUMN " + $0. serialize ( & binds) }
97
+ + constraints. map { " ADD " + $0. serialize ( & binds) }
98
+ + dropActions. map { $0. serialize ( & binds) }
40
99
sql. append ( actions. joined ( separator: " , " ) )
41
100
return sql. joined ( separator: " " )
42
101
}
0 commit comments