diff --git a/Sources/PostgreSQL/SQL/PostgreSQLAlterTable.swift b/Sources/PostgreSQL/SQL/PostgreSQLAlterTable.swift index 1711ba84..49301f6c 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLAlterTable.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLAlterTable.swift @@ -20,6 +20,62 @@ public struct PostgreSQLAlterTable: SQLAlterTable { /// See `SQLAlterTable`. public var constraints: [PostgreSQLTableConstraint] + /// DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ] + /// DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ] + public struct DropAction: SQLSerializable { + public enum Method { + case restrict + case cascade + } + + public enum Kind { + case column + case constraint + } + + public var kind: Kind + + public var ifExists: Bool + + public var column: PostgreSQLIdentifier + + public var method: Method? + + public init( + _ kind: Kind, + ifExists: Bool = false, + _ column: PostgreSQLIdentifier, + _ method: Method? = nil + ) { + self.kind = kind + self.ifExists = ifExists + self.column = column + self.method = method + } + + /// See `SQLSerializable`. + public func serialize(_ binds: inout [Encodable]) -> String { + var sql: [String] = [] + sql.append("DROP") + switch kind { + case .column: sql.append("COLUMN") + case .constraint: sql.append("CONSTRAINT") + } + if ifExists { + sql.append("IF EXISTS") + } + sql.append(column.serialize(&binds)) + if let method = method { + switch method { + case .cascade: sql.append("CASCADE") + case .restrict: sql.append("RESTRICT") + } + } + return sql.joined(separator: " ") + } + } + + public var dropActions: [DropAction] /// Creates a new `AlterTable`. /// @@ -29,6 +85,7 @@ public struct PostgreSQLAlterTable: SQLAlterTable { self.table = table self.columns = [] self.constraints = [] + self.dropActions = [] } /// See `SQLSerializable`. @@ -36,7 +93,9 @@ public struct PostgreSQLAlterTable: SQLAlterTable { var sql: [String] = [] sql.append("ALTER TABLE") sql.append(table.serialize(&binds)) - let actions = columns.map { "ADD COLUMN " + $0.serialize(&binds) } + constraints.map { "ADD " + $0.serialize(&binds) } + let actions = columns.map { "ADD COLUMN " + $0.serialize(&binds) } + + constraints.map { "ADD " + $0.serialize(&binds) } + + dropActions.map { $0.serialize(&binds) } sql.append(actions.joined(separator: ", ")) return sql.joined(separator: " ") }