forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLAlterTable.swift
43 lines (35 loc) · 1.39 KB
/
PostgreSQLAlterTable.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
/// Represents an `ALTER TABLE ...` query.
public struct PostgreSQLAlterTable: SQLAlterTable {
/// See `SQLAlterTable`.
public typealias ColumnDefinition = PostgreSQLColumnDefinition
/// See `SQLAlterTable`.
public typealias TableIdentifier = PostgreSQLTableIdentifier
/// See `SQLAlterTable`.
public static func alterTable(_ table: PostgreSQLTableIdentifier) -> PostgreSQLAlterTable {
return .init(table: table)
}
/// Name of table to alter.
public var table: PostgreSQLTableIdentifier
/// See `SQLAlterTable`.
public var columns: [PostgreSQLColumnDefinition]
/// See `SQLAlterTable`.
public var constraints: [PostgreSQLTableConstraint]
/// Creates a new `AlterTable`.
///
/// - parameters:
/// - table: Name of table to alter.
public init(table: PostgreSQLTableIdentifier) {
self.table = table
self.columns = []
self.constraints = []
}
/// See `SQLSerializable`.
public func serialize(_ binds: inout [Encodable]) -> String {
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) }
sql.append(actions.joined(separator: ", "))
return sql.joined(separator: " ")
}
}