SQLTableBuilder: column.references(deferred: true)

This commit is contained in:
Gwendal Roué committed 2016-08-05 08:24:00 +02:00
1 parent 8246f1c990
commit 39e3cba80e
2 files changed
+9 -6

No files matched your search

+7 -4
View File
@@ -46,7 +46,7 @@ public class SQLColumnBuilder {
var checkExpression: _SQLExpression?
var defaultExpression: _SQLExpression?
var collationName: String?
var reference: (table: String, column: String?, deleteAction: SQLForeignKeyAction?, updateAction: SQLForeignKeyAction?)?
var reference: (table: String, column: String?, deleteAction: SQLForeignKeyAction?, updateAction: SQLForeignKeyAction?, deferred: Bool)?
init(name: String, type: SQLColumnType) {
self.name = name
@@ -81,8 +81,8 @@ public class SQLColumnBuilder {
collationName = collation.name
}
public func references(table: String, column: String? = nil, onDelete deleteAction: SQLForeignKeyAction? = nil, onUpdate updateAction: SQLForeignKeyAction? = nil) {
reference = (table: table, column: column, deleteAction: deleteAction, updateAction: updateAction)
public func references(table: String, column: String? = nil, onDelete deleteAction: SQLForeignKeyAction? = nil, onUpdate updateAction: SQLForeignKeyAction? = nil, deferred: Bool = false) {
reference = (table: table, column: column, deleteAction: deleteAction, updateAction: updateAction, deferred: deferred)
}
func sql(db: Database) throws -> String {
@@ -141,7 +141,7 @@ public class SQLColumnBuilder {
chunks.append(collationName)
}
if let (table, column, deleteAction, updateAction) = reference {
if let (table, column, deleteAction, updateAction, deferred) = reference {
chunks.append("REFERENCES")
if let column = column {
chunks.append("\(table.quotedDatabaseIdentifier)(\(column.quotedDatabaseIdentifier))")
@@ -158,6 +158,9 @@ public class SQLColumnBuilder {
chunks.append("ON UPDATE")
chunks.append(updateAction.rawValue)
}
if deferred {
chunks.append("DEFERRABLE INITIALLY DEFERRED")
}
}
return chunks.joinWithSeparator(" ")
@@ -199,12 +199,12 @@ class SQLTableBuilderTests: GRDBTestCase {
}
try db.create(table: "child") { t in
t.column("parentName", .Text).references("parent", onDelete: .Cascade, onUpdate: .Cascade)
t.column("parentEmail", .Text).references("parent", column: "email", onDelete: .Restrict)
t.column("parentEmail", .Text).references("parent", column: "email", onDelete: .Restrict, deferred: true)
}
XCTAssertEqual(self.lastSQLQuery,
"CREATE TABLE \"child\" (" +
"\"parentName\" TEXT REFERENCES \"parent\"(\"name\") ON DELETE CASCADE ON UPDATE CASCADE, " +
"\"parentEmail\" TEXT REFERENCES \"parent\"(\"email\") ON DELETE RESTRICT" +
"\"parentEmail\" TEXT REFERENCES \"parent\"(\"email\") ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED" +
")")
}
}