diff --git a/CHANGELOG.md b/CHANGELOG.md index fa3fc2d96..f4173d01e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - **New**: [#1075](https://github.com/groue/GRDB.swift/pull/1075) by [@mallman](https://github.com/mallman): Progress-reporting database backups - **New**: [#1129](https://github.com/groue/GRDB.swift/pull/1129) by [@groue](https://github.com/groue): Detached columns - **New**: [#1132](https://github.com/groue/GRDB.swift/pull/1132) by [@groue](https://github.com/groue): Bump custom SQLite builds v3.72.2 +- **New**: [#1133](https://github.com/groue/GRDB.swift/pull/1133) by [@groue](https://github.com/groue): Support for STRICT tables - **Fixed**: [#1120](https://github.com/groue/GRDB.swift/pull/1120) by [@jnross](https://github.com/jnross): Fix typos - **Fixed**: [#1121](https://github.com/groue/GRDB.swift/pull/1121) by [@byohay](https://github.com/byohay): Quote PROJECT_DIR and SRCROOT environment variables - **Fixed**: [#1128](https://github.com/groue/GRDB.swift/pull/1128) by [@groue](https://github.com/groue): Deal with authorization callbacks from cursors of FetchableRecord diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 0b25b4f5e..21ab900c4 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -1537,6 +1537,9 @@ extension Database { /// The `DOUBLE` SQL column type public static let double = ColumnType(rawValue: "DOUBLE") + /// The `REAL` SQL column type + public static let real = ColumnType(rawValue: "REAL") + /// The `NUMERIC` SQL column type public static let numeric = ColumnType(rawValue: "NUMERIC") @@ -1551,6 +1554,9 @@ extension Database { /// The `DATETIME` SQL column type public static let datetime = ColumnType(rawValue: "DATETIME") + + /// The `ANY` SQL column type + public static let any = ColumnType(rawValue: "ANY") } /// An SQLite conflict resolution. diff --git a/GRDB/Core/DatabaseError.swift b/GRDB/Core/DatabaseError.swift index d027d26cd..35eb51ea7 100644 --- a/GRDB/Core/DatabaseError.swift +++ b/GRDB/Core/DatabaseError.swift @@ -116,17 +116,22 @@ public struct ResultCode: RawRepresentable, Equatable, CustomStringConvertible { public static let SQLITE_IOERR_BEGIN_ATOMIC = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (29<<8))) public static let SQLITE_IOERR_COMMIT_ATOMIC = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (30<<8))) public static let SQLITE_IOERR_ROLLBACK_ATOMIC = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (31<<8))) + public static let SQLITE_IOERR_DATA = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (32<<8))) + public static let SQLITE_IOERR_CORRUPTFS = ResultCode(rawValue: (SQLITE_IOERR.rawValue | (33<<8))) public static let SQLITE_LOCKED_SHAREDCACHE = ResultCode(rawValue: (SQLITE_LOCKED.rawValue | (1<<8))) public static let SQLITE_LOCKED_VTAB = ResultCode(rawValue: (SQLITE_LOCKED.rawValue | (2<<8))) public static let SQLITE_BUSY_RECOVERY = ResultCode(rawValue: (SQLITE_BUSY.rawValue | (1<<8))) public static let SQLITE_BUSY_SNAPSHOT = ResultCode(rawValue: (SQLITE_BUSY.rawValue | (2<<8))) + public static let SQLITE_BUSY_TIMEOUT = ResultCode(rawValue: (SQLITE_BUSY.rawValue | (3<<8))) public static let SQLITE_CANTOPEN_NOTEMPDIR = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (1<<8))) public static let SQLITE_CANTOPEN_ISDIR = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (2<<8))) public static let SQLITE_CANTOPEN_FULLPATH = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (3<<8))) public static let SQLITE_CANTOPEN_CONVPATH = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (4<<8))) public static let SQLITE_CANTOPEN_DIRTYWAL = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (5<<8))) /* Not Used */ + public static let SQLITE_CANTOPEN_SYMLINK = ResultCode(rawValue: (SQLITE_CANTOPEN.rawValue | (6<<8))) public static let SQLITE_CORRUPT_VTAB = ResultCode(rawValue: (SQLITE_CORRUPT.rawValue | (1<<8))) public static let SQLITE_CORRUPT_SEQUENCE = ResultCode(rawValue: (SQLITE_CORRUPT.rawValue | (2<<8))) + public static let SQLITE_CORRUPT_INDEX = ResultCode(rawValue: (SQLITE_CORRUPT.rawValue | (3<<8))) public static let SQLITE_READONLY_RECOVERY = ResultCode(rawValue: (SQLITE_READONLY.rawValue | (1<<8))) public static let SQLITE_READONLY_CANTLOCK = ResultCode(rawValue: (SQLITE_READONLY.rawValue | (2<<8))) public static let SQLITE_READONLY_ROLLBACK = ResultCode(rawValue: (SQLITE_READONLY.rawValue | (3<<8))) @@ -144,11 +149,14 @@ public struct ResultCode: RawRepresentable, Equatable, CustomStringConvertible { public static let SQLITE_CONSTRAINT_UNIQUE = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (8<<8))) public static let SQLITE_CONSTRAINT_VTAB = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (9<<8))) public static let SQLITE_CONSTRAINT_ROWID = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (10<<8))) + public static let SQLITE_CONSTRAINT_PINNED = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (11<<8))) + public static let SQLITE_CONSTRAINT_DATATYPE = ResultCode(rawValue: (SQLITE_CONSTRAINT.rawValue | (12<<8))) public static let SQLITE_NOTICE_RECOVER_WAL = ResultCode(rawValue: (SQLITE_NOTICE.rawValue | (1<<8))) public static let SQLITE_NOTICE_RECOVER_ROLLBACK = ResultCode(rawValue: (SQLITE_NOTICE.rawValue | (2<<8))) public static let SQLITE_WARNING_AUTOINDEX = ResultCode(rawValue: (SQLITE_WARNING.rawValue | (1<<8))) public static let SQLITE_AUTH_USER = ResultCode(rawValue: (SQLITE_AUTH.rawValue | (1<<8))) public static let SQLITE_OK_LOAD_PERMANENTLY = ResultCode(rawValue: (SQLITE_OK.rawValue | (1<<8))) + public static let SQLITE_OK_SYMLINK = ResultCode(rawValue: (SQLITE_OK.rawValue | (2<<8))) // swiftlint:enable operator_usage_whitespace line_length } @@ -333,17 +341,22 @@ extension DatabaseError { public static let SQLITE_IOERR_BEGIN_ATOMIC = ResultCode.SQLITE_IOERR_BEGIN_ATOMIC public static let SQLITE_IOERR_COMMIT_ATOMIC = ResultCode.SQLITE_IOERR_COMMIT_ATOMIC public static let SQLITE_IOERR_ROLLBACK_ATOMIC = ResultCode.SQLITE_IOERR_ROLLBACK_ATOMIC + public static let SQLITE_IOERR_DATA = ResultCode.SQLITE_IOERR_DATA + public static let SQLITE_IOERR_CORRUPTFS = ResultCode.SQLITE_IOERR_CORRUPTFS public static let SQLITE_LOCKED_SHAREDCACHE = ResultCode.SQLITE_LOCKED_SHAREDCACHE public static let SQLITE_LOCKED_VTAB = ResultCode.SQLITE_LOCKED_VTAB public static let SQLITE_BUSY_RECOVERY = ResultCode.SQLITE_BUSY_RECOVERY public static let SQLITE_BUSY_SNAPSHOT = ResultCode.SQLITE_BUSY_SNAPSHOT + public static let SQLITE_BUSY_TIMEOUT = ResultCode.SQLITE_BUSY_TIMEOUT public static let SQLITE_CANTOPEN_NOTEMPDIR = ResultCode.SQLITE_CANTOPEN_NOTEMPDIR public static let SQLITE_CANTOPEN_ISDIR = ResultCode.SQLITE_CANTOPEN_ISDIR public static let SQLITE_CANTOPEN_FULLPATH = ResultCode.SQLITE_CANTOPEN_FULLPATH public static let SQLITE_CANTOPEN_CONVPATH = ResultCode.SQLITE_CANTOPEN_CONVPATH public static let SQLITE_CANTOPEN_DIRTYWAL = ResultCode.SQLITE_CANTOPEN_DIRTYWAL + public static let SQLITE_CANTOPEN_SYMLINK = ResultCode.SQLITE_CANTOPEN_SYMLINK public static let SQLITE_CORRUPT_VTAB = ResultCode.SQLITE_CORRUPT_VTAB public static let SQLITE_CORRUPT_SEQUENCE = ResultCode.SQLITE_CORRUPT_SEQUENCE + public static let SQLITE_CORRUPT_INDEX = ResultCode.SQLITE_CORRUPT_INDEX public static let SQLITE_READONLY_RECOVERY = ResultCode.SQLITE_READONLY_RECOVERY public static let SQLITE_READONLY_CANTLOCK = ResultCode.SQLITE_READONLY_CANTLOCK public static let SQLITE_READONLY_ROLLBACK = ResultCode.SQLITE_READONLY_ROLLBACK @@ -361,11 +374,14 @@ extension DatabaseError { public static let SQLITE_CONSTRAINT_UNIQUE = ResultCode.SQLITE_CONSTRAINT_UNIQUE public static let SQLITE_CONSTRAINT_VTAB = ResultCode.SQLITE_CONSTRAINT_VTAB public static let SQLITE_CONSTRAINT_ROWID = ResultCode.SQLITE_CONSTRAINT_ROWID + public static let SQLITE_CONSTRAINT_PINNED = ResultCode.SQLITE_CONSTRAINT_PINNED + public static let SQLITE_CONSTRAINT_DATATYPE = ResultCode.SQLITE_CONSTRAINT_DATATYPE public static let SQLITE_NOTICE_RECOVER_WAL = ResultCode.SQLITE_NOTICE_RECOVER_WAL public static let SQLITE_NOTICE_RECOVER_ROLLBACK = ResultCode.SQLITE_NOTICE_RECOVER_ROLLBACK public static let SQLITE_WARNING_AUTOINDEX = ResultCode.SQLITE_WARNING_AUTOINDEX public static let SQLITE_AUTH_USER = ResultCode.SQLITE_AUTH_USER public static let SQLITE_OK_LOAD_PERMANENTLY = ResultCode.SQLITE_OK_LOAD_PERMANENTLY + public static let SQLITE_OK_SYMLINK = ResultCode.SQLITE_OK_SYMLINK } extension DatabaseError { diff --git a/GRDB/QueryInterface/Schema/TableDefinition.swift b/GRDB/QueryInterface/Schema/TableDefinition.swift index ad0918e2b..ae9533480 100644 --- a/GRDB/QueryInterface/Schema/TableDefinition.swift +++ b/GRDB/QueryInterface/Schema/TableDefinition.swift @@ -2,6 +2,48 @@ extension Database { // MARK: - Database Schema + // TODO: deprecate just before GRDB 6 + /// Creates a database table. + /// + /// try db.create(table: "place") { t in + /// t.autoIncrementedPrimaryKey("id") + /// t.column("title", .text) + /// t.column("favorite", .boolean).notNull().default(false) + /// t.column("longitude", .double).notNull() + /// t.column("latitude", .double).notNull() + /// } + /// + /// See and + /// + /// + /// - warning: This method is sunsetted. You should prefer + /// `create(table:options:body:)` instead. + /// + /// - parameters: + /// - name: The table name. + /// - temporary: If true, creates a temporary table. + /// - ifNotExists: If false (the default), an error is thrown if the + /// table already exists. Otherwise, the table is created unless it + /// already exists. + /// - withoutRowID: If true, uses WITHOUT ROWID optimization. + /// - body: A closure that defines table columns and constraints. + /// - throws: A DatabaseError whenever an SQLite error occurs. + @_disfavoredOverload + public func create( + table name: String, + temporary: Bool = false, + ifNotExists: Bool = false, + withoutRowID: Bool = false, + body: (TableDefinition) throws -> Void) + throws + { + var options: TableOptions = [] + if temporary { options.insert(.temporary) } + if ifNotExists { options.insert(.ifNotExists) } + if withoutRowID { options.insert(.withoutRowID) } + try create(table: name, options: options, body: body) + } + /// Creates a database table. /// /// try db.create(table: "place") { t in @@ -17,26 +59,18 @@ extension Database { /// /// - parameters: /// - name: The table name. - /// - temporary: If true, creates a temporary table. - /// - ifNotExists: If false (the default), an error is thrown if the - /// table already exists. Otherwise, the table is created unless it - /// already exists. - /// - withoutRowID: If true, uses WITHOUT ROWID optimization. + /// - options: Table creation options. /// - body: A closure that defines table columns and constraints. /// - throws: A DatabaseError whenever an SQLite error occurs. public func create( table name: String, - temporary: Bool = false, - ifNotExists: Bool = false, - withoutRowID: Bool = false, + options: TableOptions = [], body: (TableDefinition) throws -> Void) throws { let definition = TableDefinition( name: name, - temporary: temporary, - ifNotExists: ifNotExists, - withoutRowID: withoutRowID) + options: options) try body(definition) let sql = try definition.sql(self) try execute(sql: sql) @@ -79,6 +113,46 @@ extension Database { try execute(sql: "DROP TABLE \(name.quotedDatabaseIdentifier)") } + // TODO: deprecate just before GRDB 6 + /// Creates an index. + /// + /// try db.create(index: "playerByEmail", on: "player", columns: ["email"]) + /// + /// SQLite can also index expressions (https://www.sqlite.org/expridx.html) + /// and use specific collations. To create such an index, use a raw SQL + /// query. + /// + /// try db.execute(sql: "CREATE INDEX ...") + /// + /// See + /// + /// - warning: This method is sunsetted. You should prefer + /// `create(index:on:columns:options:condition:)` instead. + /// + /// - parameters: + /// - name: The index name. + /// - table: The name of the indexed table. + /// - columns: The indexed columns. + /// - unique: If true, creates a unique index. + /// - ifNotExists: If true, no error is thrown if index already exists. + /// - condition: If not nil, creates a partial index + /// (see ). + @_disfavoredOverload + public func create( + index name: String, + on table: String, + columns: [String], + unique: Bool = false, + ifNotExists: Bool = false, + condition: SQLExpressible? = nil) + throws + { + var options: IndexOptions = [] + if ifNotExists { options.insert(.ifNotExists) } + if unique { options.insert(.unique) } + try create(index: name, on: table, columns: columns, options: options, condition: condition) + } + /// Creates an index. /// /// try db.create(index: "playerByEmail", on: "player", columns: ["email"]) @@ -95,16 +169,14 @@ extension Database { /// - name: The index name. /// - table: The name of the indexed table. /// - columns: The indexed columns. - /// - unique: If true, creates a unique index. - /// - ifNotExists: If true, no error is thrown if index already exists. + /// - options: Index creation options. /// - condition: If not nil, creates a partial index /// (see ). public func create( index name: String, on table: String, columns: [String], - unique: Bool = false, - ifNotExists: Bool = false, + options: IndexOptions = [], condition: SQLExpressible? = nil) throws { @@ -112,8 +184,7 @@ extension Database { name: name, table: table, columns: columns, - unique: unique, - ifNotExists: ifNotExists, + options: options, condition: condition?.sqlExpression) let sql = try definition.sql(self) try execute(sql: sql) @@ -153,6 +224,29 @@ extension Database { } } +/// Table creation options +public struct TableOptions: OptionSet { + /// :nodoc: + public let rawValue: Int + + /// :nodoc: + public init(rawValue: Int) { self.rawValue = rawValue } + + /// Only creates the table if it does not already exist. + public static let ifNotExists = TableOptions(rawValue: 1 << 0) + + /// Creates a temporary table. + public static let temporary = TableOptions(rawValue: 1 << 1) + + /// Creates a without rowid table. See + public static let withoutRowID = TableOptions(rawValue: 1 << 2) + +#if GRDBCUSTOMSQLITE + /// Creates a strict table. See + public static let strict = TableOptions(rawValue: 1 << 3) +#endif +} + /// The TableDefinition class lets you define table columns and constraints. /// /// You don't create instances of this class. Instead, you use the Database @@ -200,9 +294,7 @@ public final class TableDefinition { } private let name: String - private let temporary: Bool - private let ifNotExists: Bool - private let withoutRowID: Bool + private let options: TableOptions private var columns: [ColumnItem] = [] private var primaryKeyConstraint: KeyConstraint? private var uniqueKeyConstraints: [KeyConstraint] = [] @@ -210,11 +302,9 @@ public final class TableDefinition { private var checkConstraints: [SQLExpression] = [] private var literalConstraints: [SQL] = [] - init(name: String, temporary: Bool, ifNotExists: Bool, withoutRowID: Bool) { + init(name: String, options: TableOptions) { self.name = name - self.temporary = temporary - self.ifNotExists = ifNotExists - self.withoutRowID = withoutRowID + self.options = options } /// Defines the auto-incremented primary key. @@ -444,11 +534,11 @@ public final class TableDefinition { do { var chunks: [String] = [] chunks.append("CREATE") - if temporary { + if options.contains(.temporary) { chunks.append("TEMPORARY") } chunks.append("TABLE") - if ifNotExists { + if options.contains(.ifNotExists) { chunks.append("IF NOT EXISTS") } chunks.append(name.quotedDatabaseIdentifier) @@ -549,14 +639,29 @@ public final class TableDefinition { chunks.append("(\(items.joined(separator: ", ")))") } - if withoutRowID { - chunks.append("WITHOUT ROWID") + var tableOptions: [String] = [] + +#if GRDBCUSTOMSQLITE + if options.contains(.strict) { + tableOptions.append("STRICT") } +#endif + + if options.contains(.withoutRowID) { + tableOptions.append("WITHOUT ROWID") + } + + if !tableOptions.isEmpty { + chunks.append(tableOptions.joined(separator: ", ")) + } + statements.append(chunks.joined(separator: " ")) } + var indexOptions: IndexOptions = [] + if options.contains(.ifNotExists) { indexOptions.insert(.ifNotExists) } let indexStatements = try columns - .compactMap { $0.columnDefinition?.indexDefinition(in: name, ifNotExists: ifNotExists) } + .compactMap { $0.columnDefinition?.indexDefinition(in: name, options: indexOptions) } .map { try $0.sql(db) } statements.append(contentsOf: indexStatements) return statements.joined(separator: "; ") @@ -680,7 +785,7 @@ public final class TableAlteration { let statement = chunks.joined(separator: " ") statements.append(statement) - if let indexDefinition = column.indexDefinition(in: name, ifNotExists: false) { + if let indexDefinition = column.indexDefinition(in: name) { try statements.append(indexDefinition.sql(db)) } @@ -1143,7 +1248,7 @@ public final class ColumnDefinition { return chunks.joined(separator: " ") } - fileprivate func indexDefinition(in table: String, ifNotExists: Bool) -> IndexDefinition? { + fileprivate func indexDefinition(in table: String, options: IndexOptions = []) -> IndexDefinition? { switch index { case .none: return nil case .unique: return nil @@ -1152,29 +1257,42 @@ public final class ColumnDefinition { name: "\(table)_on_\(name)", table: table, columns: [name], - unique: false, - ifNotExists: ifNotExists, + options: options, condition: nil) } } } +/// Table creation options +public struct IndexOptions: OptionSet { + /// :nodoc: + public let rawValue: Int + + /// :nodoc: + public init(rawValue: Int) { self.rawValue = rawValue } + + /// Only creates the index if it does not already exist. + public static let ifNotExists = IndexOptions(rawValue: 1 << 0) + + /// Creates a unique index. + public static let unique = IndexOptions(rawValue: 1 << 1) +} + private struct IndexDefinition { let name: String let table: String let columns: [String] - let unique: Bool - let ifNotExists: Bool + let options: IndexOptions let condition: SQLExpression? func sql(_ db: Database) throws -> String { var chunks: [String] = [] chunks.append("CREATE") - if unique { + if options.contains(.unique) { chunks.append("UNIQUE") } chunks.append("INDEX") - if ifNotExists { + if options.contains(.ifNotExists) { chunks.append("IF NOT EXISTS") } chunks.append(name.quotedDatabaseIdentifier) diff --git a/README.md b/README.md index ca5ccbeaf..1d85eeda2 100644 --- a/README.md +++ b/README.md @@ -4187,7 +4187,7 @@ SQLite itself has many reference documents about table creation: [CREATE TABLE]( try db.create(table: "example") { t in ... } // CREATE TEMPORARY TABLE example IF NOT EXISTS ( -try db.create(table: "example", temporary: true, ifNotExists: true) { t in ... } +try db.create(table: "example", options: [.temporary, .ifNotExists]) { t in ... } ``` > :bulb: **Tip**: database table names should be singular, and camelCased. Make them look like Swift identifiers: `place`, `country`, `postalAddress`, `httpRequest`. @@ -4196,7 +4196,7 @@ try db.create(table: "example", temporary: true, ifNotExists: true) { t in ... } > > :point_up: **Note**: [`WITHOUT ROWID`](https://www.sqlite.org/withoutrowid.html) tables can not be tracked with [Database Observation] tools. -**Add regular columns** with their name and eventual type (text, integer, double, numeric, boolean, blob, date and datetime) - see [SQLite data types](https://www.sqlite.org/datatype3.html): +**Add regular columns** with their name and eventual type (`text`, `integer`, `double`, `real`, `numeric`, `boolean`, `blob`, `date`, `datetime` and `any`) - see [SQLite data types](https://www.sqlite.org/datatype3.html): ```swift // CREATE TABLE example ( @@ -4335,8 +4335,11 @@ try db.drop(table: "obsolete") Create indexes with the `create(index:)` method: ```swift -// CREATE UNIQUE INDEX byEmail ON users(email) -try db.create(index: "byEmail", on: "users", columns: ["email"], unique: true) +// CREATE INDEX byName ON users(lastName, firstName) +try db.create(index: "byName", on: "users", columns: ["lastName, "firstName"]) + +// CREATE UNIQUE INDEX byEmail IF NOT EXISTS ON users(email) +try db.create(index: "byEmail", on: "users", columns: ["email"], options: [.unique, .ifNotExists]) ``` Relevant SQLite documentation: diff --git a/Tests/GRDBTests/AssociationBelongsToSQLTests.swift b/Tests/GRDBTests/AssociationBelongsToSQLTests.swift index 152c6aa68..03aceea2e 100644 --- a/Tests/GRDBTests/AssociationBelongsToSQLTests.swift +++ b/Tests/GRDBTests/AssociationBelongsToSQLTests.swift @@ -2057,7 +2057,7 @@ class AssociationBelongsToSQLTests: GRDBTestCase { // Existence check is performed on the primary key. let dbQueue = try makeDatabaseQueue() try dbQueue.write { db in - try db.create(table: "author", withoutRowID: true) { t in + try db.create(table: "author", options: [.withoutRowID]) { t in t.column("a", .text).notNull() t.column("b", .text).notNull() t.primaryKey(["a", "b"]) diff --git a/Tests/GRDBTests/TableDefinitionTests.swift b/Tests/GRDBTests/TableDefinitionTests.swift index 9b8d146ce..f7b59facf 100644 --- a/Tests/GRDBTests/TableDefinitionTests.swift +++ b/Tests/GRDBTests/TableDefinitionTests.swift @@ -33,6 +33,46 @@ class TableDefinitionTests: GRDBTestCase { ) WITHOUT ROWID """) } + + try dbQueue.inDatabase { db in + try db.create(table: "test2", options: [.temporary, .ifNotExists, .withoutRowID]) { t in + t.column("id", .integer).primaryKey() + } + assertEqualSQL(lastSQLQuery!, """ + CREATE TEMPORARY TABLE IF NOT EXISTS "test2" (\ + "id" INTEGER PRIMARY KEY\ + ) WITHOUT ROWID + """) + } + +#if GRDBCUSTOMSQLITE + try dbQueue.inDatabase { db in + try db.create(table: "test3", options: [.strict, .withoutRowID]) { 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, WITHOUT ROWID + """) + + 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 } func testColumnLiteral() throws { @@ -160,7 +200,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in sqlQueries.removeAll() - try db.create(table: "test", ifNotExists: true) { t in + try db.create(table: "test", options: [.ifNotExists]) { t in t.column("a", .integer).indexed() t.column("b", .integer).indexed() } @@ -663,8 +703,11 @@ class TableDefinitionTests: GRDBTestCase { try db.create(index: "test_on_a_b", on: "test", columns: ["a", "b"], unique: true, ifNotExists: true) assertEqualSQL(lastSQLQuery!, "CREATE UNIQUE INDEX IF NOT EXISTS \"test_on_a_b\" ON \"test\"(\"a\", \"b\")") + try db.create(index: "test_on_a_b_2", on: "test", columns: ["a", "b"], options: [.unique, .ifNotExists]) + assertEqualSQL(lastSQLQuery!, "CREATE UNIQUE INDEX IF NOT EXISTS \"test_on_a_b_2\" ON \"test\"(\"a\", \"b\")") + // Sanity check - XCTAssertEqual(try Set(db.indexes(on: "test").map(\.name)), ["test_on_a", "test_on_a_b"]) + XCTAssertEqual(try Set(db.indexes(on: "test").map(\.name)), ["test_on_a", "test_on_a_b", "test_on_a_b_2"]) } } @@ -677,7 +720,7 @@ class TableDefinitionTests: GRDBTestCase { t.column("b", .text) } - try db.create(index: "test_on_a_b", on: "test", columns: ["a", "b"], unique: true, ifNotExists: true, condition: Column("a") == 1) + try db.create(index: "test_on_a_b", on: "test", columns: ["a", "b"], options: [.unique, .ifNotExists], condition: Column("a") == 1) assertEqualSQL(lastSQLQuery!, "CREATE UNIQUE INDEX IF NOT EXISTS \"test_on_a_b\" ON \"test\"(\"a\", \"b\") WHERE \"a\" = 1") // Sanity check