diff --git a/GRDB/QueryInterface/Schema/TableDefinition.swift b/GRDB/QueryInterface/Schema/TableDefinition.swift index d09e3183b..2cef7d4a3 100644 --- a/GRDB/QueryInterface/Schema/TableDefinition.swift +++ b/GRDB/QueryInterface/Schema/TableDefinition.swift @@ -241,8 +241,16 @@ public struct TableOptions: OptionSet { /// Creates a without rowid table. See public static let withoutRowID = TableOptions(rawValue: 1 << 2) -#if GRDBCUSTOMSQLITE - /// Creates a strict table. See +#if GRDBCUSTOMSQLITE || GRDBCIPHER + /// Creates a STRICT table + /// + /// See + 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 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") } diff --git a/Tests/GRDBTests/TableDefinitionTests.swift b/Tests/GRDBTests/TableDefinitionTests.swift index f9ee53083..ed7ac3db6 100644 --- a/Tests/GRDBTests/TableDefinitionTests.swift +++ b/Tests/GRDBTests/TableDefinitionTests.swift @@ -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