Skip to content

Commit c57e76b

Browse files
committed
Add support for .strict table option in 3.37+
1 parent 9531a7f commit c57e76b

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

GRDB/QueryInterface/Schema/TableDefinition.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,16 @@ public struct TableOptions: OptionSet {
241241
/// Creates a without rowid table. See <https://www.sqlite.org/withoutrowid.html>
242242
public static let withoutRowID = TableOptions(rawValue: 1 << 2)
243243

244-
#if GRDBCUSTOMSQLITE
245-
/// Creates a strict table. See <https://www.sqlite.org/stricttables.html>
244+
#if GRDBCUSTOMSQLITE || GRDBCIPHER
245+
/// Creates a STRICT table
246+
///
247+
/// See <https://www.sqlite.org/stricttables.html>
248+
public static let strict = TableOptions(rawValue: 1 << 3)
249+
#else
250+
@available(iOS 15.4, macOS 12.4, tvOS 15.4, watchOS 8.5, *) // SQLite 3.37+
251+
/// Creates a STRICT table
252+
///
253+
/// See <https://www.sqlite.org/stricttables.html>
246254
public static let strict = TableOptions(rawValue: 1 << 3)
247255
#endif
248256
}
@@ -641,12 +649,17 @@ public final class TableDefinition {
641649

642650
var tableOptions: [String] = []
643651

644-
#if GRDBCUSTOMSQLITE
652+
#if GRDBCUSTOMSQLITE || GRDBCIPHER
645653
if options.contains(.strict) {
646654
tableOptions.append("STRICT")
647655
}
656+
#else
657+
if #available(iOS 15.4, macOS 12.4, tvOS 15.4, watchOS 8.5, *) {
658+
if options.contains(.strict) {
659+
tableOptions.append("STRICT")
660+
}
661+
}
648662
#endif
649-
650663
if options.contains(.withoutRowID) {
651664
tableOptions.append("WITHOUT ROWID")
652665
}

Tests/GRDBTests/TableDefinitionTests.swift

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ class TableDefinitionTests: GRDBTestCase {
4444
) WITHOUT ROWID
4545
""")
4646
}
47-
48-
#if GRDBCUSTOMSQLITE
47+
}
48+
49+
#if GRDBCUSTOMSQLITE || GRDBCIPHER
50+
func testStrictTableCreationOptionCustomAndCipher() throws {
51+
let dbQueue = try makeDatabaseQueue()
4952
try dbQueue.inDatabase { db in
50-
try db.create(table: "test3", options: [.strict, .withoutRowID]) { t in
53+
try db.create(table: "test3", options: [.strict]) { t in
5154
t.column("id", .integer).primaryKey()
5255
t.column("a", .integer)
5356
t.column("b", .real)
@@ -63,18 +66,37 @@ class TableDefinitionTests: GRDBTestCase {
6366
"c" TEXT, \
6467
"d" BLOB, \
6568
"e" ANY\
66-
) STRICT, WITHOUT ROWID
69+
) STRICT
6770
""")
68-
69-
do {
70-
try db.execute(sql: "INSERT INTO test3 (id, a) VALUES (1, 'foo')")
71-
XCTFail("Expected DatabaseError.SQLITE_CONSTRAINT_DATATYPE")
72-
} catch DatabaseError.SQLITE_CONSTRAINT_DATATYPE {
73-
}
7471
}
72+
}
7573
#endif
74+
75+
@available(iOS 15.4, macOS 12.4, tvOS 15.4, watchOS 8.5, *)
76+
func testStrictTableCreationOption() throws {
77+
let dbQueue = try makeDatabaseQueue()
78+
try dbQueue.inDatabase { db in
79+
try db.create(table: "test3", options: [.strict]) { t in
80+
t.column("id", .integer).primaryKey()
81+
t.column("a", .integer)
82+
t.column("b", .real)
83+
t.column("c", .text)
84+
t.column("d", .blob)
85+
t.column("e", .any)
86+
}
87+
assertEqualSQL(lastSQLQuery!, """
88+
CREATE TABLE "test3" (\
89+
"id" INTEGER PRIMARY KEY, \
90+
"a" INTEGER, \
91+
"b" REAL, \
92+
"c" TEXT, \
93+
"d" BLOB, \
94+
"e" ANY\
95+
) STRICT
96+
""")
97+
}
7698
}
77-
99+
78100
func testColumnLiteral() throws {
79101
let dbQueue = try makeDatabaseQueue()
80102
try dbQueue.inDatabase { db in

0 commit comments

Comments
 (0)