From 820772fc6dc3970bed5390ba557d369f51d3103d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 7 Dec 2019 12:01:51 +0100 Subject: [PATCH 1/8] SQlite 3.30.1 --- SQLiteCustom/src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SQLiteCustom/src b/SQLiteCustom/src index d3829a886..7de55d32c 160000 --- a/SQLiteCustom/src +++ b/SQLiteCustom/src @@ -1 +1 @@ -Subproject commit d3829a886bdd69f9e6a34d3c0a8df4a1117eb4f5 +Subproject commit 7de55d32c9e2c83f454e4d17c02b30ef0904b986 From 2c9bef6440a16e2bc57edbeb08ff792834a0f8a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 7 Dec 2019 12:07:06 +0100 Subject: [PATCH 2/8] "*" is no longer a valid FTS5 pattern --- Tests/GRDBTests/FTS5PatternTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/GRDBTests/FTS5PatternTests.swift b/Tests/GRDBTests/FTS5PatternTests.swift index 9d51de329..72105ba93 100644 --- a/Tests/GRDBTests/FTS5PatternTests.swift +++ b/Tests/GRDBTests/FTS5PatternTests.swift @@ -32,7 +32,7 @@ class FTS5PatternTests: GRDBTestCase { ("écarlates", 1), ("fooéı👨👨🏿🇫🇷🇨🇮", 0), // Prefix queries - ("*", 1), // weird + // ("*", 1), // No longer valid on SQLite 3.30.1 ("Robin*", 1), // Phrase queries ("\"foulent muscles\"", 1), From 7208f5317670eaf07a84fd1547d96fa4636d77d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 7 Dec 2019 12:31:58 +0100 Subject: [PATCH 3/8] Support for NULLS FIRST and NULLS LAST --- .../SQL/SQLCollatedExpression.swift | 26 ++++++++ GRDB/QueryInterface/SQL/SQLOrdering.swift | 22 +++++++ ...QLSpecificExpressible+QueryInterface.swift | 16 +++++ README.md | 7 +++ .../QueryInterfaceRequestTests.swift | 62 ++++++++++++++----- ...bleRecord+QueryInterfaceRequestTests.swift | 8 +++ 6 files changed, 126 insertions(+), 15 deletions(-) diff --git a/GRDB/QueryInterface/SQL/SQLCollatedExpression.swift b/GRDB/QueryInterface/SQL/SQLCollatedExpression.swift index f80d4de53..1cdc5cc2b 100644 --- a/GRDB/QueryInterface/SQL/SQLCollatedExpression.swift +++ b/GRDB/QueryInterface/SQL/SQLCollatedExpression.swift @@ -40,6 +40,32 @@ public struct SQLCollatedExpression { return SQLOrdering.desc(sqlExpression) } + #if GRDBCUSTOMSQLITE + /// Returns an ordering suitable for QueryInterfaceRequest.order() + /// + /// let email: SQLCollatedExpression = Column("email").collating(.nocase) + /// + /// // SELECT * FROM player ORDER BY email COLLATE NOCASE ASC NULLS LAST + /// Player.order(email.ascNullsLast) + /// + /// See https://github.com/groue/GRDB.swift/#the-query-interface + public var ascNullsLast: SQLOrderingTerm { + return SQLOrdering.ascNullsLast(sqlExpression) + } + + /// Returns an ordering suitable for QueryInterfaceRequest.order() + /// + /// let email: SQLCollatedExpression = Column("email").collating(.nocase) + /// + /// // SELECT * FROM player ORDER BY email COLLATE NOCASE DESC NULLS FIRST + /// Player.order(email.descNullsFirst) + /// + /// See https://github.com/groue/GRDB.swift/#the-query-interface + public var descNullsFirst: SQLOrderingTerm { + return SQLOrdering.descNullsFirst(sqlExpression) + } + #endif + init(_ expression: SQLExpression, collationName: Database.CollationName) { self.expression = expression self.collationName = collationName diff --git a/GRDB/QueryInterface/SQL/SQLOrdering.swift b/GRDB/QueryInterface/SQL/SQLOrdering.swift index 25be3e43e..2908cbc8e 100644 --- a/GRDB/QueryInterface/SQL/SQLOrdering.swift +++ b/GRDB/QueryInterface/SQL/SQLOrdering.swift @@ -26,6 +26,10 @@ public protocol SQLOrderingTerm { enum SQLOrdering: SQLOrderingTerm { case asc(SQLExpression) case desc(SQLExpression) + #if GRDBCUSTOMSQLITE + case ascNullsLast(SQLExpression) + case descNullsFirst(SQLExpression) + #endif var reversed: SQLOrderingTerm { switch self { @@ -33,6 +37,12 @@ enum SQLOrdering: SQLOrderingTerm { return SQLOrdering.desc(expression) case .desc(let expression): return SQLOrdering.asc(expression) + #if GRDBCUSTOMSQLITE + case .ascNullsLast(let expression): + return SQLOrdering.descNullsFirst(expression) + case .descNullsFirst(let expression): + return SQLOrdering.ascNullsLast(expression) + #endif } } @@ -42,6 +52,12 @@ enum SQLOrdering: SQLOrderingTerm { return expression.expressionSQL(&context, wrappedInParenthesis: false) + " ASC" case .desc(let expression): return expression.expressionSQL(&context, wrappedInParenthesis: false) + " DESC" + #if GRDBCUSTOMSQLITE + case .ascNullsLast(let expression): + return expression.expressionSQL(&context, wrappedInParenthesis: false) + " ASC NULLS LAST" + case .descNullsFirst(let expression): + return expression.expressionSQL(&context, wrappedInParenthesis: false) + " DESC NULLS FIRST" + #endif } } @@ -51,6 +67,12 @@ enum SQLOrdering: SQLOrderingTerm { return SQLOrdering.asc(expression.qualifiedExpression(with: alias)) case .desc(let expression): return SQLOrdering.desc(expression.qualifiedExpression(with: alias)) + #if GRDBCUSTOMSQLITE + case .ascNullsLast(let expression): + return SQLOrdering.ascNullsLast(expression.qualifiedExpression(with: alias)) + case .descNullsFirst(let expression): + return SQLOrdering.descNullsFirst(expression.qualifiedExpression(with: alias)) + #endif } } } diff --git a/GRDB/QueryInterface/SQL/SQLSpecificExpressible+QueryInterface.swift b/GRDB/QueryInterface/SQL/SQLSpecificExpressible+QueryInterface.swift index 25e8bfceb..8d6ce9af8 100644 --- a/GRDB/QueryInterface/SQL/SQLSpecificExpressible+QueryInterface.swift +++ b/GRDB/QueryInterface/SQL/SQLSpecificExpressible+QueryInterface.swift @@ -16,6 +16,22 @@ extension SQLSpecificExpressible { public var desc: SQLOrderingTerm { return SQLOrdering.desc(sqlExpression) } + + #if GRDBCUSTOMSQLITE + /// Returns a value that can be used as an argument to QueryInterfaceRequest.order() + /// + /// See https://github.com/groue/GRDB.swift/#the-query-interface + public var ascNullsLast: SQLOrderingTerm { + return SQLOrdering.ascNullsLast(sqlExpression) + } + + /// Returns a value that can be used as an argument to QueryInterfaceRequest.order() + /// + /// See https://github.com/groue/GRDB.swift/#the-query-interface + public var descNullsFirst: SQLOrderingTerm { + return SQLOrdering.descNullsFirst(sqlExpression) + } + #endif } diff --git a/README.md b/README.md index e6007c61d..970c71ca8 100644 --- a/README.md +++ b/README.md @@ -4083,6 +4083,13 @@ You can now build requests with the following methods: `all`, `none`, `select`, Player.order(scoreColumn.desc, nameColumn) ``` + SQLite considers NULL values to be smaller than any other values for sorting purposes. Hence, NULLs naturally appear at the beginning of an ascending ordering and at the end of a descending ordering. With a [custom SQLite build], this can be changed using `.ascNullsLast` and `.descNullsFirst`: + + ```swift + // SELECT * FROM player ORDER BY score ASC NULLS LAST + Player.order(nameColumn.ascNullsLast) + ``` + Each `order` call clears any previous ordering: ```swift diff --git a/Tests/GRDBTests/QueryInterfaceRequestTests.swift b/Tests/GRDBTests/QueryInterfaceRequestTests.swift index 203c01efc..51cc6649d 100644 --- a/Tests/GRDBTests/QueryInterfaceRequestTests.swift +++ b/Tests/GRDBTests/QueryInterfaceRequestTests.swift @@ -1,8 +1,8 @@ import XCTest #if GRDBCUSTOMSQLITE - import GRDBCustomSQLite +import GRDBCustomSQLite #else - import GRDB +import GRDB #endif private struct Col { @@ -18,7 +18,7 @@ private struct Reader : TableRecord { private let tableRequest = Reader.all() class QueryInterfaceRequestTests: GRDBTestCase { - + var collation: DatabaseCollation! override func setup(_ dbWriter: DatabaseWriter) throws { @@ -88,10 +88,10 @@ class QueryInterfaceRequestTests: GRDBTestCase { } } } - - + + // MARK: - Count - + func testFetchCount() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in @@ -135,10 +135,10 @@ class QueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual(lastSQLQuery, "SELECT COUNT(*) FROM (SELECT MAX(\"age\") FROM \"readers\" GROUP BY \"name\")") } } - - + + // MARK: - Select - + func testSelectLiteral() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in @@ -155,7 +155,7 @@ class QueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual(rows[1][1] as Int64, 1) } } - + func testSelectLiteralWithPositionalArguments() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in @@ -172,7 +172,7 @@ class QueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual(rows[1][1] as Int64, 1) } } - + func testSelectLiteralWithNamedArguments() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in @@ -228,7 +228,7 @@ class QueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual(rows[1][1] as Int64, 1) } } - + func testSelectionCustomKey() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in @@ -526,14 +526,14 @@ class QueryInterfaceRequestTests: GRDBTestCase { .filter(sql: "name = :name", arguments: ["name": "arthur"])), "SELECT * FROM \"readers\" WHERE (age > 20) AND (name = 'arthur')") } - + func testFilter() throws { let dbQueue = try makeDatabaseQueue() XCTAssertEqual( sql(dbQueue, tableRequest.filter(true)), "SELECT * FROM \"readers\" WHERE 1") } - + func testMultipleFilter() throws { let dbQueue = try makeDatabaseQueue() XCTAssertEqual( @@ -617,7 +617,7 @@ class QueryInterfaceRequestTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() XCTAssertEqual( sql(dbQueue, tableRequest.group(Col.name).having(min(Col.age) > 18).having(max(Col.age) < 50)), - "SELECT * FROM \"readers\" GROUP BY \"name\" HAVING (MIN(\"age\") > 18) AND (MAX(\"age\") < 50)") + "SELECT * FROM \"readers\" GROUP BY \"name\" HAVING (MIN(\"age\") > 18) AND (MAX(\"age\") < 50)") } @@ -661,6 +661,14 @@ class QueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual( sql(dbQueue, tableRequest.order(abs(Col.age))), "SELECT * FROM \"readers\" ORDER BY ABS(\"age\")") + #if GRDBCUSTOMSQLITE + XCTAssertEqual( + sql(dbQueue, tableRequest.order(Col.age.ascNullsLast)), + "SELECT * FROM \"readers\" ORDER BY \"age\" ASC NULLS LAST") + XCTAssertEqual( + sql(dbQueue, tableRequest.order(Col.age.descNullsFirst)), + "SELECT * FROM \"readers\" ORDER BY \"age\" DESC NULLS FIRST") + #endif } func testSortWithCollation() throws { @@ -674,6 +682,14 @@ class QueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual( sql(dbQueue, tableRequest.order(Col.name.collating(collation))), "SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE localized_case_insensitive") + #if GRDBCUSTOMSQLITE + XCTAssertEqual( + sql(dbQueue, tableRequest.order(Col.name.collating(.nocase).ascNullsLast)), + "SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE NOCASE ASC NULLS LAST") + XCTAssertEqual( + sql(dbQueue, tableRequest.order(Col.name.collating(.nocase).descNullsFirst)), + "SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE NOCASE DESC NULLS FIRST") + #endif } func testMultipleSort() throws { @@ -706,6 +722,14 @@ class QueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual( sql(dbQueue, tableRequest.order(abs(Col.age)).reversed()), "SELECT * FROM \"readers\" ORDER BY ABS(\"age\") DESC") + #if GRDBCUSTOMSQLITE + XCTAssertEqual( + sql(dbQueue, tableRequest.order(Col.age.descNullsFirst).reversed()), + "SELECT * FROM \"readers\" ORDER BY \"age\" ASC NULLS LAST") + XCTAssertEqual( + sql(dbQueue, tableRequest.order(Col.age.ascNullsLast).reversed()), + "SELECT * FROM \"readers\" ORDER BY \"age\" DESC NULLS FIRST") + #endif } func testReverseWithCollation() throws { @@ -719,6 +743,14 @@ class QueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual( sql(dbQueue, tableRequest.order(Col.name.collating(collation)).reversed()), "SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE localized_case_insensitive DESC") + #if GRDBCUSTOMSQLITE + XCTAssertEqual( + sql(dbQueue, tableRequest.order(Col.name.collating(.nocase).ascNullsLast).reversed()), + "SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE NOCASE DESC NULLS FIRST") + XCTAssertEqual( + sql(dbQueue, tableRequest.order(Col.name.collating(.nocase).descNullsFirst).reversed()), + "SELECT * FROM \"readers\" ORDER BY \"name\" COLLATE NOCASE ASC NULLS LAST") + #endif } func testMultipleReverse() throws { diff --git a/Tests/GRDBTests/TableRecord+QueryInterfaceRequestTests.swift b/Tests/GRDBTests/TableRecord+QueryInterfaceRequestTests.swift index b7791daf7..4a211a52a 100644 --- a/Tests/GRDBTests/TableRecord+QueryInterfaceRequestTests.swift +++ b/Tests/GRDBTests/TableRecord+QueryInterfaceRequestTests.swift @@ -258,6 +258,14 @@ class TableRecordQueryInterfaceRequestTests: GRDBTestCase { XCTAssertEqual( sql(dbQueue, Reader.order(abs(Col.age))), "SELECT * FROM \"readers\" ORDER BY ABS(\"age\")") + #if GRDBCUSTOMSQLITE + XCTAssertEqual( + sql(dbQueue, Reader.order(Col.age.ascNullsLast)), + "SELECT * FROM \"readers\" ORDER BY \"age\" ASC NULLS LAST") + XCTAssertEqual( + sql(dbQueue, Reader.order(Col.age.descNullsFirst)), + "SELECT * FROM \"readers\" ORDER BY \"age\" DESC NULLS FIRST") + #endif } func testMultipleSort() throws { From 7b4dc144851c3ab5bd2d459af29d16e882767495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 7 Dec 2019 13:32:33 +0100 Subject: [PATCH 4/8] [breaking] disable double-quoted string literals by default --- CHANGELOG.md | 12 +++ GRDB/Core/Configuration.swift | 13 ++++ GRDB/Core/Database.swift | 9 +++ Sources/sqlite3/shim.h | 23 +++++- Support/grdb_config.h | 23 +++++- TODO.md | 1 + .../DatabaseConfigurationTests.swift | 74 ++++++++++++++++++- 7 files changed, 149 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aeea08257..0dfda9d86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,18 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - [#659](https://github.com/groue/GRDB.swift/pull/659): Database interruption - [#660](https://github.com/groue/GRDB.swift/pull/660): Database Lock Prevention +### Breaking Changes + +[Custom SQLite builds](Documentation/CustomSQLiteBuilds.md) now disable by default the support for the [Double-quoted String Literals misfeature](https://sqlite.org/quirks.html#dblquote). You can restore the previous behavior if your application relies on it: + +```swift +// Enable support for Double-quoted String Literals +var configuration = Configuration() +configuration.acceptsDoubleQuotedStringLiterals = true +let dbQueue = try DatabaseQueue(path: ..., configuration: configuration) +``` + + ### Documentation Diff A new [Interrupt a Database](README.md#interrupt-a-database) chapter documents the new `interrupt()` method. diff --git a/GRDB/Core/Configuration.swift b/GRDB/Core/Configuration.swift index bd72ee6e8..faf3b60cc 100644 --- a/GRDB/Core/Configuration.swift +++ b/GRDB/Core/Configuration.swift @@ -75,6 +75,19 @@ public struct Configuration { /// Default: nil public var trace: TraceFunction? + /// If false, SQLite from version 3.29.0 will not interpret a double-quoted + /// string as a string literal if it does not match any valid identifier: + /// + /// // no such column: naame + /// let name = try String.fetchOne(db, sql: """ + /// SELECT "naame" FROM "player" + /// """) + /// + /// See https://sqlite.org/quirks.html#dblquote for more information. + /// + /// Default: false + public var acceptsDoubleQuotedStringLiterals = false + // MARK: - Encryption #if SQLITE_HAS_CODEC diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 54f2aebdb..c8ea618e5 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -209,6 +209,7 @@ public final class Database { func setup() throws { // Setup trace first, so that setup queries are traced. setupTrace() + setupDoubleQuotedStringLiterals() try setupForeignKeys() setupBusyMode() setupDefaultFunctions() @@ -294,6 +295,14 @@ public final class Database { return SQLITE_OK } + private func setupDoubleQuotedStringLiterals() { + if configuration.acceptsDoubleQuotedStringLiterals { + enableDoubleQuotedStringLiterals(sqliteConnection) + } else { + disableDoubleQuotedStringLiterals(sqliteConnection) + } + } + private func setupForeignKeys() throws { // Foreign keys are disabled by default with SQLite3 if configuration.foreignKeysEnabled { diff --git a/Sources/sqlite3/shim.h b/Sources/sqlite3/shim.h index d018d2fce..be4cd9cf9 100644 --- a/Sources/sqlite3/shim.h +++ b/Sources/sqlite3/shim.h @@ -2,8 +2,27 @@ typedef void(*errorLogCallback)(void *pArg, int iErrCode, const char *zMsg); -// Wrapper around sqlite3_config(SQLITE_CONFIG_LOG, ...) which is a variadic -// function that can't be used from Swift. +/// Wrapper around sqlite3_config(SQLITE_CONFIG_LOG, ...) which is a variadic +/// function that can't be used from Swift. static inline void registerErrorLogCallback(errorLogCallback callback) { sqlite3_config(SQLITE_CONFIG_LOG, callback, 0); } + +#if SQLITE_VERSION_NUMBER >= 3029000 +/// Wrapper around sqlite3_db_config() which is a variadic function that can't +/// be used from Swift. +static inline void disableDoubleQuotedStringLiterals(sqlite3 *db) { + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DDL, 0, (void *)0); + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DML, 0, (void *)0); +} + +/// Wrapper around sqlite3_db_config() which is a variadic function that can't +/// be used from Swift. +static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DDL, 1, (void *)0); + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DML, 1, (void *)0); +} +#else +static inline void disableDoubleQuotedStringLiterals(sqlite3 *db) { } +static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { } +#endif diff --git a/Support/grdb_config.h b/Support/grdb_config.h index 90e83eb5e..5038365c8 100644 --- a/Support/grdb_config.h +++ b/Support/grdb_config.h @@ -15,12 +15,31 @@ typedef void(*errorLogCallback)(void *pArg, int iErrCode, const char *zMsg); -// Wrapper around sqlite3_config(SQLITE_CONFIG_LOG, ...) which is a variadic -// function that can't be used from Swift. +/// Wrapper around sqlite3_config(SQLITE_CONFIG_LOG, ...) which is a variadic +/// function that can't be used from Swift. static inline void registerErrorLogCallback(errorLogCallback callback) { sqlite3_config(SQLITE_CONFIG_LOG, callback, 0); } +#if SQLITE_VERSION_NUMBER >= 3029000 +/// Wrapper around sqlite3_db_config() which is a variadic function that can't +/// be used from Swift. +static inline void disableDoubleQuotedStringLiterals(sqlite3 *db) { + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DDL, 0, (void *)0); + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DML, 0, (void *)0); +} + +/// Wrapper around sqlite3_db_config() which is a variadic function that can't +/// be used from Swift. +static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DDL, 1, (void *)0); + sqlite3_db_config(db, SQLITE_DBCONFIG_DQS_DML, 1, (void *)0); +} +#else +static inline void disableDoubleQuotedStringLiterals(sqlite3 *db) { } +static inline void enableDoubleQuotedStringLiterals(sqlite3 *db) { } +#endif + // Expose APIs that are missing from system #ifdef GRDB_SQLITE_ENABLE_PREUPDATE_HOOK SQLITE_API void *sqlite3_preupdate_hook( diff --git a/TODO.md b/TODO.md index 8c18ba1de..a345e5bd8 100644 --- a/TODO.md +++ b/TODO.md @@ -41,6 +41,7 @@ ## Unsure if necessary +- [ ] https://sqlite.org/pragma.html#pragma_index_xinfo - [ ] Deprecate DatabaseQueue/Pool.addFunction, collation, tokenizer: those should be done in Configuration.prepareDatabase - [ ] filter(rowid:), filter(rowids:) - [ ] https://github.com/apple/swift-evolution/blob/master/proposals/0075-import-test.md diff --git a/Tests/GRDBTests/DatabaseConfigurationTests.swift b/Tests/GRDBTests/DatabaseConfigurationTests.swift index d9ab79d88..1374fb3c3 100644 --- a/Tests/GRDBTests/DatabaseConfigurationTests.swift +++ b/Tests/GRDBTests/DatabaseConfigurationTests.swift @@ -1,8 +1,8 @@ import XCTest #if GRDBCUSTOMSQLITE - import GRDBCustomSQLite +import GRDBCustomSQLite #else - import GRDB +import GRDB #endif class DatabaseConfigurationTests: GRDBTestCase { @@ -72,4 +72,74 @@ class DatabaseConfigurationTests: GRDBTestCase { } catch is TestError { } } } + + func testAcceptsDoubleQuotedStringLiteralsDefault() throws { + let configuration = Configuration() + XCTAssertFalse(configuration.acceptsDoubleQuotedStringLiterals) + } + + func testAcceptsDoubleQuotedStringLiteralsTrue() throws { + var configuration = Configuration() + configuration.acceptsDoubleQuotedStringLiterals = true + let dbQueue = try makeDatabaseQueue(configuration: configuration) + try dbQueue.inDatabase { db in + try db.execute(sql: """ + CREATE TABLE player(name TEXT); + INSERT INTO player DEFAULT VALUES; + """) + } + + // Test SQLITE_DBCONFIG_DQS_DML + let foo = try dbQueue.inDatabase { db in + try String.fetchOne(db, sql: "SELECT \"foo\" FROM player") + } + XCTAssertEqual(foo, "foo") + + // Test SQLITE_DBCONFIG_DQS_DDL + try dbQueue.inDatabase { db in + try db.execute(sql: "CREATE INDEX i ON player(\"foo\")") + } + } + + func testAcceptsDoubleQuotedStringLiteralsFalse() throws { + var configuration = Configuration() + configuration.acceptsDoubleQuotedStringLiterals = false + let dbQueue = try makeDatabaseQueue(configuration: configuration) + try dbQueue.inDatabase { db in + try db.execute(sql: """ + CREATE TABLE player(name TEXT); + INSERT INTO player DEFAULT VALUES; + """) + } + + // Test SQLITE_DBCONFIG_DQS_DML + do { + let foo = try dbQueue.inDatabase { db in + try String.fetchOne(db, sql: "SELECT \"foo\" FROM player") + } + if sqlite3_libversion_number() >= 3029000 { + XCTFail("Expected error") + } else { + XCTAssertEqual(foo, "foo") + } + } catch let error as DatabaseError { + XCTAssertEqual(error.resultCode, .SQLITE_ERROR) + XCTAssertEqual(error.message, "no such column: foo") + XCTAssertEqual(error.sql, "SELECT \"foo\" FROM player") + } + + // Test SQLITE_DBCONFIG_DQS_DDL + do { + try dbQueue.inDatabase { db in + try db.execute(sql: "CREATE INDEX i ON player(\"foo\")") + } + if sqlite3_libversion_number() >= 3029000 { + XCTFail("Expected error") + } + } catch let error as DatabaseError { + XCTAssertEqual(error.resultCode, .SQLITE_ERROR) + XCTAssertEqual(error.message, "no such column: foo") + XCTAssertEqual(error.sql, "CREATE INDEX i ON player(\"foo\")") + } + } } From d6a459ad789bfa56e72def1ad869a8e42f96722b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 7 Dec 2019 13:42:52 +0100 Subject: [PATCH 5/8] CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dfda9d86..bb1db856b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,13 +63,14 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - [#659](https://github.com/groue/GRDB.swift/pull/659): Database interruption - [#660](https://github.com/groue/GRDB.swift/pull/660): Database Lock Prevention +- [#662](https://github.com/groue/GRDB.swift/pull/662): Upgrade custom SQLite builds to version 3.30.1 (thanks to [@swiftlyfalling](https://github.com/swiftlyfalling/SQLiteLib)) ### Breaking Changes [Custom SQLite builds](Documentation/CustomSQLiteBuilds.md) now disable by default the support for the [Double-quoted String Literals misfeature](https://sqlite.org/quirks.html#dblquote). You can restore the previous behavior if your application relies on it: ```swift -// Enable support for Double-quoted String Literals +// Enable support for the Double-quoted String Literals misfeature var configuration = Configuration() configuration.acceptsDoubleQuotedStringLiterals = true let dbQueue = try DatabaseQueue(path: ..., configuration: configuration) From a0fddfe7ec14113280a80c44aec30a2bcc8463ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 7 Dec 2019 13:47:00 +0100 Subject: [PATCH 6/8] Documentation --- GRDB/Core/Configuration.swift | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/GRDB/Core/Configuration.swift b/GRDB/Core/Configuration.swift index faf3b60cc..e9d3c985c 100644 --- a/GRDB/Core/Configuration.swift +++ b/GRDB/Core/Configuration.swift @@ -76,16 +76,26 @@ public struct Configuration { public var trace: TraceFunction? /// If false, SQLite from version 3.29.0 will not interpret a double-quoted - /// string as a string literal if it does not match any valid identifier: + /// string as a string literal if it does not match any valid identifier. /// - /// // no such column: naame + /// For example: + /// + /// // Error: no such column: foo /// let name = try String.fetchOne(db, sql: """ - /// SELECT "naame" FROM "player" + /// SELECT "foo" FROM "player" /// """) /// - /// See https://sqlite.org/quirks.html#dblquote for more information. + /// When true, or before version 3.29.0, such strings are interpreted as + /// string literals, as in the example below. This is a well known SQLite + /// [misfeature](https://sqlite.org/quirks.html#dblquote). /// - /// Default: false + /// // Success: "foo" + /// let name = try String.fetchOne(db, sql: """ + /// SELECT "foo" FROM "player" + /// """) + /// + /// - Recommended value: false + /// - Default value: false public var acceptsDoubleQuotedStringLiterals = false // MARK: - Encryption From d1e1dcd097b41f3468e74ce0a1d170f52ceaf11d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 8 Dec 2019 08:55:05 +0100 Subject: [PATCH 7/8] Fix test for SPM --- Tests/GRDBTests/DatabaseConfigurationTests.swift | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Tests/GRDBTests/DatabaseConfigurationTests.swift b/Tests/GRDBTests/DatabaseConfigurationTests.swift index 1374fb3c3..4fd964ae3 100644 --- a/Tests/GRDBTests/DatabaseConfigurationTests.swift +++ b/Tests/GRDBTests/DatabaseConfigurationTests.swift @@ -1,8 +1,13 @@ import XCTest #if GRDBCUSTOMSQLITE -import GRDBCustomSQLite + import GRDBCustomSQLite #else -import GRDB + #if SWIFT_PACKAGE + import CSQLite + #else + import SQLite3 + #endif + import GRDB #endif class DatabaseConfigurationTests: GRDBTestCase { From e90ef7aa6ea9668bace11ca41fa2309b99b2dac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 8 Dec 2019 18:07:41 +0100 Subject: [PATCH 8/8] Fix acceptsDoubleQuotedStringLiterals tests --- Tests/GRDBTests/DatabaseConfigurationTests.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Tests/GRDBTests/DatabaseConfigurationTests.swift b/Tests/GRDBTests/DatabaseConfigurationTests.swift index 4fd964ae3..3441ac76c 100644 --- a/Tests/GRDBTests/DatabaseConfigurationTests.swift +++ b/Tests/GRDBTests/DatabaseConfigurationTests.swift @@ -101,8 +101,10 @@ class DatabaseConfigurationTests: GRDBTestCase { XCTAssertEqual(foo, "foo") // Test SQLITE_DBCONFIG_DQS_DDL - try dbQueue.inDatabase { db in - try db.execute(sql: "CREATE INDEX i ON player(\"foo\")") + if sqlite3_libversion_number() > 3008010 { + try dbQueue.inDatabase { db in + try db.execute(sql: "CREATE INDEX i ON player(\"foo\")") + } } } @@ -143,7 +145,10 @@ class DatabaseConfigurationTests: GRDBTestCase { } } catch let error as DatabaseError { XCTAssertEqual(error.resultCode, .SQLITE_ERROR) - XCTAssertEqual(error.message, "no such column: foo") + XCTAssert([ + "no such column: foo", + "table player has no column named foo"] + .contains(error.message)) XCTAssertEqual(error.sql, "CREATE INDEX i ON player(\"foo\")") } }