Skip to content

Commit

Permalink
Merge pull request #15 from vapor/fk-action
Browse files Browse the repository at this point in the history
replace conflict resolution with SQLForeignKeyAction
  • Loading branch information
tanner0101 authored Jun 21, 2018
2 parents 27fafe9 + 424b31a commit 5127aaf
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 69 deletions.
12 changes: 6 additions & 6 deletions Sources/SQL/SQLColumnConstraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ extension SQLColumnConstraint {

public static func references<T, V>(
_ keyPath: KeyPath<T, V>,
onDelete: Algorithm.ForeignKey.ConflictResolution? = nil,
onUpdate: Algorithm.ForeignKey.ConflictResolution? = nil,
onDelete: Algorithm.ForeignKey.Action? = nil,
onUpdate: Algorithm.ForeignKey.Action? = nil,
identifier: Identifier? = nil
) -> Self
) -> Self
where T: SQLTable
{
return references(.keyPath(keyPath), [.keyPath(keyPath)], onDelete: onDelete, onUpdate: onUpdate, identifier: identifier)
Expand All @@ -69,10 +69,10 @@ extension SQLColumnConstraint {
public static func references(
_ foreignTable: Algorithm.ForeignKey.TableIdentifier,
_ foreignColumns: [Algorithm.ForeignKey.Identifier],
onDelete: Algorithm.ForeignKey.ConflictResolution? = nil,
onUpdate: Algorithm.ForeignKey.ConflictResolution? = nil,
onDelete: Algorithm.ForeignKey.Action? = nil,
onUpdate: Algorithm.ForeignKey.Action? = nil,
identifier: Identifier? = nil
) -> Self {
) -> Self {
return .constraint(.foreignKey(.foreignKey(foreignTable, foreignColumns, onDelete: onDelete, onUpdate: onUpdate)), identifier)
}
}
Expand Down
51 changes: 0 additions & 51 deletions Sources/SQL/SQLConflictResolution.swift

This file was deleted.

20 changes: 10 additions & 10 deletions Sources/SQL/SQLForeignKey.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
public protocol SQLForeignKey: SQLSerializable {
associatedtype TableIdentifier: SQLTableIdentifier
associatedtype Identifier: SQLIdentifier
associatedtype ConflictResolution: SQLConflictResolution
associatedtype Action: SQLForeignKeyAction

static func foreignKey(
_ foreignTable: TableIdentifier,
_ foreignColumns: [Identifier],
onDelete: ConflictResolution?,
onUpdate: ConflictResolution?
onDelete: Action?,
onUpdate: Action?
) -> Self
}

// MARK: Generic

public struct GenericSQLForeignKey<TableIdentifier, Identifier, ConflictResolution>: SQLForeignKey
where TableIdentifier: SQLTableIdentifier, Identifier: SQLIdentifier, ConflictResolution: SQLConflictResolution
public struct GenericSQLForeignKey<TableIdentifier, Identifier, Action>: SQLForeignKey
where TableIdentifier: SQLTableIdentifier, Identifier: SQLIdentifier, Action: SQLForeignKeyAction
{
public typealias `Self` = GenericSQLForeignKey<TableIdentifier, Identifier, ConflictResolution>
public typealias `Self` = GenericSQLForeignKey<TableIdentifier, Identifier, Action>

/// See `SQLForeignKey`.
public static func foreignKey(
_ foreignTable: TableIdentifier,
_ foreignColumns: [Identifier],
onDelete: ConflictResolution?,
onUpdate: ConflictResolution?
onDelete: Action?,
onUpdate: Action?
) -> Self {
return .init(foreignTable: foreignTable, foreignColumns: foreignColumns, onDelete: onDelete, onUpdate: onUpdate)
}
Expand All @@ -32,9 +32,9 @@ public struct GenericSQLForeignKey<TableIdentifier, Identifier, ConflictResoluti

public var foreignColumns: [Identifier]

public var onDelete: ConflictResolution?
public var onDelete: Action?

public var onUpdate: ConflictResolution?
public var onUpdate: Action?

/// See `SQLSerializable`.
public func serialize(_ binds: inout [Encodable]) -> String {
Expand Down
58 changes: 58 additions & 0 deletions Sources/SQL/SQLForeignKeyAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/// RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
public protocol SQLForeignKeyAction: SQLSerializable {
/// Produce an error indicating that the deletion or update would create a foreign key constraint violation.
/// If the constraint is deferred, this error will be produced at constraint check time if there still exist any referencing rows.
/// This is the default action.
static var noAction: Self { get }

/// Produce an error indicating that the deletion or update would create a foreign key constraint violation.
static var restrict: Self { get }

/// Delete any rows referencing the deleted row, or update the values of the referencing column(s) to the new values of the referenced columns, respectively.
static var cascade: Self { get }

/// Set the referencing column(s) to null.
static var setNull: Self { get }

/// Set the referencing column(s) to their default values.
/// (There must be a row in the referenced table matching the default values, if they are not null, or the operation will fail.)
static var setDefault: Self { get }
}

/// Generic implementation of `SQLForeignKeyAction` suitable as a common-denominator for all SQL flavors.
public enum GenericSQLForeignKeyAction: SQLForeignKeyAction {
/// See `SQLForeignKeyAction`.
public typealias `Self` = GenericSQLForeignKeyAction

/// See `SQLForeignKeyAction`.
public static var noAction: Self { return ._noAction }

/// See `SQLForeignKeyAction`.
public static var restrict: Self { return ._restrict }

/// See `SQLForeignKeyAction`.
public static var cascade: Self { return ._cascade }

/// See `SQLForeignKeyAction`.
public static var setNull: Self { return ._setNull }

/// See `SQLForeignKeyAction`.
public static var setDefault: Self { return ._setDefault }

case _noAction
case _restrict
case _cascade
case _setNull
case _setDefault

/// See `SQLSerializable`.
public func serialize(_ binds: inout [Encodable]) -> String {
switch self {
case ._noAction: return "NO ACTION"
case ._restrict: return "RESTRICT"
case ._cascade: return "CASCADE"
case ._setNull: return "SET NULL"
case ._setDefault: return "SET DEFAULT"
}
}
}
4 changes: 2 additions & 2 deletions Sources/SQL/SQLTableConstraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ extension SQLTableConstraint {
_ columns: [Algorithm.Identifier],
references foreignTable: Algorithm.ForeignKey.TableIdentifier,
_ foreignColumns: [Algorithm.ForeignKey.Identifier],
onDelete: Algorithm.ForeignKey.ConflictResolution? = nil,
onUpdate: Algorithm.ForeignKey.ConflictResolution? = nil,
onDelete: Algorithm.ForeignKey.Action? = nil,
onUpdate: Algorithm.ForeignKey.Action? = nil,
identifier: Identifier? = nil
) -> Self {
return .constraint(.foreignKey(columns, .foreignKey(foreignTable, foreignColumns, onDelete: onDelete, onUpdate: onUpdate)), identifier)
Expand Down

0 comments on commit 5127aaf

Please sign in to comment.