Add support for .strict table option in 3.37+

This commit is contained in:
Josip Cavar committed 2022-10-19 15:55:46 +02:00
1 parent 9531a7f8fc
commit c57e76be52
2 files changed
+51 -16

No files matched your search

@@ -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
}
@@ -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")
}
+34 -12
View File
@@ -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)
@@ -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 {
}
}
#endif
}
#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