Skip to content

Commit

Permalink
Adds ALTER _ RENAME TO _ support (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeedleInAJayStack authored Apr 10, 2022
1 parent 960a86a commit afc74d8
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
91 changes: 91 additions & 0 deletions Sources/SQLKit/Builders/SQLAlterTableBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Rename the table.
/// - Parameter newName: The new name to apply to the table
/// - Returns: `self` for chaining.
public func rename(
to newName: String
) -> Self {
return self.rename(to: SQLIdentifier(newName))
}

@discardableResult
/// Rename the table.
/// - Parameter newName: The new name to apply to the table
/// - Returns: `self` for chaining.
public func rename(
to newName: SQLExpression
) -> Self {
self.alterTable.renameTo = newName
return self
}

@discardableResult
/// Add a column to the table.
/// - Parameters:
/// - column: The name of the new column
/// - dataType: The new datatype of the new column
/// - constraints: Constraints for the new column
/// - Returns: `self` for chaining.
public func column(
_ column: String,
type dataType: SQLDataType,
Expand All @@ -33,6 +60,12 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Add a column to the table.
/// - Parameters:
/// - column: The name of the new column
/// - dataType: The new datatype of the new column
/// - constraints: Constraints for the new column
/// - Returns: `self` for chaining.
public func column(
_ column: String,
type dataType: SQLDataType,
Expand All @@ -46,6 +79,12 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Add a column to the table.
/// - Parameters:
/// - column: The name of the new column
/// - dataType: The new datatype of the new column
/// - constraints: Constraints for the new column
/// - Returns: `self` for chaining.
public func column(
_ column: SQLExpression,
type dataType: SQLExpression,
Expand All @@ -59,6 +98,12 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Add a column to the table.
/// - Parameters:
/// - column: The name of the new column
/// - dataType: The new datatype of the new column
/// - constraints: Constraints for the new column
/// - Returns: `self` for chaining.
public func column(
_ column: SQLExpression,
type dataType: SQLExpression,
Expand All @@ -72,12 +117,21 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Add a column to the table.
/// - Parameter columnDefinition: Expression defining the column
/// - Returns: `self` for chaining.
public func addColumn(_ columnDefinition: SQLExpression) -> Self {
self.alterTable.addColumns.append(columnDefinition)
return self
}

@discardableResult
/// Alter a column in the table.
/// - Parameters:
/// - column: The name of the column to alter
/// - dataType: The datatype to change the column to
/// - constraints: Constraints to apply to the altered column
/// - Returns: `self` for chaining.
public func modifyColumn(
_ column: String,
type dataType: SQLDataType,
Expand All @@ -91,6 +145,12 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Alter a column in the table.
/// - Parameters:
/// - column: The name of the column to alter
/// - dataType: The datatype to change the column to
/// - constraints: Constraints to apply to the altered column
/// - Returns: `self` for chaining.
public func modifyColumn(
_ column: String,
type dataType: SQLDataType,
Expand All @@ -104,6 +164,12 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Alter a column in the table.
/// - Parameters:
/// - column: The name of the column to alter
/// - dataType: The datatype to change the column to
/// - constraints: Constraints to apply to the altered column
/// - Returns: `self` for chaining.
public func modifyColumn(
_ column: SQLExpression,
type dataType: SQLExpression,
Expand All @@ -117,6 +183,12 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Alter a column in the table.
/// - Parameters:
/// - column: The name of the column to alter
/// - dataType: The datatype to change the column to
/// - constraints: Constraints to apply to the altered column
/// - Returns: `self` for chaining.
public func modifyColumn(
_ column: SQLExpression,
type dataType: SQLExpression,
Expand All @@ -130,6 +202,11 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Alter a column in the table.
/// - Parameters:
/// - column: The name of the column to alter
/// - dataType: The datatype to change the column to
/// - Returns: `self` for chaining.
public func update(
column: String,
type dataType: SQLDataType
Expand All @@ -141,6 +218,11 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Alter a column in the table.
/// - Parameters:
/// - column: The name of the column to alter
/// - dataType: The datatype to change the column to
/// - Returns: `self` for chaining.
public func update(
column: SQLExpression,
type dataType: SQLExpression
Expand All @@ -152,19 +234,28 @@ public final class SQLAlterTableBuilder: SQLQueryBuilder {
}

@discardableResult
/// Alter a column in the table.
/// - Parameter columnDefinition: Expression defining the column changes
/// - Returns: `self` for chaining.
public func modifyColumn(_ columnDefinition: SQLExpression) -> Self {
self.alterTable.modifyColumns.append(columnDefinition)
return self
}

@discardableResult
/// Drop the column from the table
/// - Parameter column: The name of the column to drop
/// - Returns: `self` for chaining.
public func dropColumn(
_ column: String
) -> Self {
return self.dropColumn(SQLIdentifier(column))
}

@discardableResult
/// Drop the column from the table
/// - Parameter column: The name of the column to drop
/// - Returns: `self` for chaining.
public func dropColumn(
_ column: SQLExpression
) -> Self {
Expand Down
10 changes: 9 additions & 1 deletion Sources/SQLKit/Query/SQLAlterTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
/// See `SQLAlterTableBuilder` for more information.
public struct SQLAlterTable: SQLExpression {
public var name: SQLExpression

/// New name
public var renameTo: SQLExpression?
/// Columns to add.
public var addColumns: [SQLExpression]
/// Columns to update.
Expand All @@ -21,6 +24,7 @@ public struct SQLAlterTable: SQLExpression {
/// Creates a new `SQLAlterTable`. See `SQLAlterTableBuilder`.
public init(name: SQLExpression) {
self.name = name
self.renameTo = nil
self.addColumns = []
self.modifyColumns = []
self.dropColumns = []
Expand All @@ -30,7 +34,7 @@ public struct SQLAlterTable: SQLExpression {

public func serialize(to serializer: inout SQLSerializer) {
let syntax = serializer.dialect.alterTableSyntax

if !syntax.allowsBatch && self.addColumns.count + self.modifyColumns.count + self.dropColumns.count > 1 {
serializer.database.logger.warning("Database does not support batch table alterations. You will need to rewrite as individual alter statements.")
}
Expand All @@ -57,6 +61,10 @@ public struct SQLAlterTable: SQLExpression {
serializer.statement {
$0.append("ALTER TABLE")
$0.append(self.name)
if let renameTo = renameTo {
$0.append("RENAME TO")
$0.append(renameTo)
}
for (idx, alteration) in alterations.enumerated() {
if idx > 0 {
$0.append(",")
Expand Down
6 changes: 6 additions & 0 deletions Tests/SQLKitTests/SQLKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ final class SQLKitTests: XCTestCase {
.update(column: "again", type: .text)
.run().wait()
XCTAssertEqual(db.results[6], "ALTER TABLE `alterable` ADD `hello` TEXT , DROP `there` , MODIFY `again` TEXT")

// Table renaming
try db.alter(table: "alterable")
.rename(to: "new_alterable")
.run().wait()
XCTAssertEqual(db.results[7], "ALTER TABLE `alterable` RENAME TO `new_alterable`")
}

// MARK: Distinct
Expand Down

0 comments on commit afc74d8

Please sign in to comment.