mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
SQLTableBuilder: table.check()
This commit is contained in:
1 parent
9b9b4d4bdf
commit
b88d2fe111
2 files changed
+48
No files matched your search
@@ -23,6 +23,7 @@ public final class SQLTableBuilder {
|
||||
var primaryKeyConstraint: (columns: [String], conflictResolution: SQLConflictResolution?)?
|
||||
var uniqueKeyConstraints: [(columns: [String], conflictResolution: SQLConflictResolution?)] = []
|
||||
var foreignKeyConstraints: [(columns: [String], table: String, destinationColumns: [String]?, deleteAction: SQLForeignKeyAction?, updateAction: SQLForeignKeyAction?, deferred: Bool)] = []
|
||||
var checkConstraints: [_SQLExpression] = []
|
||||
|
||||
init(name: String, temporary: Bool, ifNotExists: Bool, withoutRowID: Bool) {
|
||||
self.name = name
|
||||
@@ -55,6 +56,16 @@ public final class SQLTableBuilder {
|
||||
public func foreignKey(columns: [String], to table: String, columns destinationColumns: [String]? = nil, onDelete deleteAction: SQLForeignKeyAction? = nil, onUpdate updateAction: SQLForeignKeyAction? = nil, deferred: Bool = false) {
|
||||
foreignKeyConstraints.append((columns: columns, table: table, destinationColumns: destinationColumns, deleteAction: deleteAction, updateAction: updateAction, deferred: deferred))
|
||||
}
|
||||
|
||||
// TODO: doc
|
||||
public func check(condition: _SQLExpressible) {
|
||||
checkConstraints.append(condition.sqlExpression)
|
||||
}
|
||||
|
||||
// TODO: doc
|
||||
public func check(sql sql: String) {
|
||||
checkConstraints.append(_SQLExpression.Literal(sql, nil))
|
||||
}
|
||||
}
|
||||
|
||||
public final class SQLColumnBuilder {
|
||||
@@ -234,6 +245,14 @@ extension SQLTableBuilder {
|
||||
items.append(chunks.joinWithSeparator(" "))
|
||||
}
|
||||
|
||||
for checkExpression in checkConstraints {
|
||||
var chunks: [String] = []
|
||||
var arguments: StatementArguments? = nil // nil so that checkExpression.sql(&arguments) embeds literals
|
||||
chunks.append("CHECK")
|
||||
chunks.append("(" + checkExpression.sql(&arguments) + ")")
|
||||
items.append(chunks.joinWithSeparator(" "))
|
||||
}
|
||||
|
||||
chunks.append("(\(items.joinWithSeparator(", ")))")
|
||||
}
|
||||
|
||||
|
||||
@@ -305,6 +305,35 @@ class SQLTableBuilderTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testTableCheck() {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.create(table: "test") { t in
|
||||
t.column("a", .Integer)
|
||||
t.column("b", .Integer)
|
||||
t.check(SQLColumn("a") > SQLColumn("b"))
|
||||
t.check(sql: "a + b < 10")
|
||||
}
|
||||
XCTAssertEqual(self.lastSQLQuery,
|
||||
"CREATE TABLE \"test\" (" +
|
||||
"\"a\" INTEGER, " +
|
||||
"\"b\" INTEGER, " +
|
||||
"CHECK ((\"a\" > \"b\")), " +
|
||||
"CHECK (a + b < 10)" +
|
||||
")")
|
||||
|
||||
// Sanity check
|
||||
try db.execute("INSERT INTO test (a, b) VALUES (1, 0)")
|
||||
do {
|
||||
try db.execute("INSERT INTO test (a, b) VALUES (0, 1)")
|
||||
XCTFail()
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDropTable() {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
Reference in new issue
Block a user