Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for strict tables #1288

Merged
merged 5 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions GRDB/QueryInterface/Schema/TableDefinition.swift
Expand Up @@ -241,8 +241,16 @@ public struct TableOptions: OptionSet {
/// Creates a without rowid table. See <https://www.sqlite.org/withoutrowid.html>
public static let withoutRowID = TableOptions(rawValue: 1 << 2)

#if GRDBCUSTOMSQLITE
/// Creates a strict table. See <https://www.sqlite.org/stricttables.html>
#if GRDBCUSTOMSQLITE || GRDBCIPHER
/// Creates a STRICT table
///
/// See <https://www.sqlite.org/stricttables.html>
public static let strict = TableOptions(rawValue: 1 << 3)
#else
@available(iOS 15.4, macOS 12.4, tvOS 15.4, watchOS 8.5, *) // SQLite 3.37+
/// Creates a STRICT table
///
/// See <https://www.sqlite.org/stricttables.html>
public static let strict = TableOptions(rawValue: 1 << 3)
#endif
}
Expand Down Expand Up @@ -641,12 +649,17 @@ public final class TableDefinition {

var tableOptions: [String] = []

#if GRDBCUSTOMSQLITE
#if GRDBCUSTOMSQLITE || GRDBCIPHER
if options.contains(.strict) {
tableOptions.append("STRICT")
}
#else
if #available(iOS 15.4, macOS 12.4, tvOS 15.4, watchOS 8.5, *) {
if options.contains(.strict) {
tableOptions.append("STRICT")
}
}
#endif

if options.contains(.withoutRowID) {
tableOptions.append("WITHOUT ROWID")
}
Expand Down
44 changes: 33 additions & 11 deletions Tests/GRDBTests/TableDefinitionTests.swift
Expand Up @@ -44,10 +44,13 @@ class TableDefinitionTests: GRDBTestCase {
) WITHOUT ROWID
""")
}

#if GRDBCUSTOMSQLITE
}

#if GRDBCUSTOMSQLITE || GRDBCIPHER
func testStrictTableCreationOptionCustomAndCipher() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(table: "test3", options: [.strict, .withoutRowID]) { t in
try db.create(table: "test3", options: [.strict]) { t in
t.column("id", .integer).primaryKey()
t.column("a", .integer)
t.column("b", .real)
Expand All @@ -63,18 +66,37 @@ class TableDefinitionTests: GRDBTestCase {
"c" TEXT, \
"d" BLOB, \
"e" ANY\
) STRICT, WITHOUT ROWID
) STRICT
""")

do {
try db.execute(sql: "INSERT INTO test3 (id, a) VALUES (1, 'foo')")
XCTFail("Expected DatabaseError.SQLITE_CONSTRAINT_DATATYPE")
} catch DatabaseError.SQLITE_CONSTRAINT_DATATYPE {
}
groue marked this conversation as resolved.
Show resolved Hide resolved
}
}
#endif

@available(iOS 15.4, macOS 12.4, tvOS 15.4, watchOS 8.5, *)
func testStrictTableCreationOption() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(table: "test3", options: [.strict]) { t in
t.column("id", .integer).primaryKey()
t.column("a", .integer)
t.column("b", .real)
t.column("c", .text)
t.column("d", .blob)
t.column("e", .any)
}
assertEqualSQL(lastSQLQuery!, """
CREATE TABLE "test3" (\
"id" INTEGER PRIMARY KEY, \
"a" INTEGER, \
"b" REAL, \
"c" TEXT, \
"d" BLOB, \
"e" ANY\
) STRICT
""")
}
}

func testColumnLiteral() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
Expand Down