From 7a90b9ce23e5e557e609cc2cfb19a5efd77fd4b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 26 Mar 2017 14:41:15 +0200 Subject: [PATCH] Add availability checks for FTS3 unicode61 tokenizer, FTS4 unindexed columns, and partial indexes. --- GRDB/FTS/FTS3TokenizerDescriptor.swift | 28 ++++++++++ GRDB/FTS/FTS4.swift | 23 ++++++++ GRDB/QueryInterface/TableDefinition.swift | 55 ++++++++++++++++++- .../DatabasePoolConcurrencyTests.swift | 2 +- Tests/Public/FTS/FTS3TableBuilderTests.swift | 24 ++++++++ Tests/Public/FTS/FTS3TokenizerTests.swift | 24 ++++++++ Tests/Public/FTS/FTS4TableBuilderTests.swift | 30 ++++++++++ .../SQLTableBuilderTests.swift | 25 ++++++++- .../VirtualTableModuleTests.swift | 16 ++++++ 9 files changed, 224 insertions(+), 3 deletions(-) diff --git a/GRDB/FTS/FTS3TokenizerDescriptor.swift b/GRDB/FTS/FTS3TokenizerDescriptor.swift index d48bf746d..850384d1c 100644 --- a/GRDB/FTS/FTS3TokenizerDescriptor.swift +++ b/GRDB/FTS/FTS3TokenizerDescriptor.swift @@ -32,6 +32,7 @@ public struct FTS3TokenizerDescriptor { /// See https://www.sqlite.org/fts3.html#tokenizer public static let porter = FTS3TokenizerDescriptor("porter") + #if USING_CUSTOMSQLITE || USING_SQLCIPHER /// The "unicode61" tokenizer. /// /// db.create(virtualTable: "books", using: FTS4()) { t in @@ -48,6 +49,33 @@ public struct FTS3TokenizerDescriptor { /// /// See https://www.sqlite.org/fts3.html#tokenizer public static func unicode61(removeDiacritics: Bool = true, separators: Set = [], tokenCharacters: Set = []) -> FTS3TokenizerDescriptor { + return _unicode61(removeDiacritics: removeDiacritics, separators: separators, tokenCharacters: tokenCharacters) + } + #else + /// The "unicode61" tokenizer. + /// + /// db.create(virtualTable: "books", using: FTS4()) { t in + /// t.tokenizer = .unicode61() + /// } + /// + /// - parameters: + /// - removeDiacritics: If true (the default), then SQLite will strip + /// diacritics from latin characters. + /// - separators: Unless empty (the default), SQLite will consider these + /// characters as token separators. + /// - tokenCharacters: Unless empty (the default), SQLite will consider + /// these characters as token characters. + /// + /// See https://www.sqlite.org/fts3.html#tokenizer + @available(iOS 8.2, OSX 10.10, *) + public static func unicode61(removeDiacritics: Bool = true, separators: Set = [], tokenCharacters: Set = []) -> FTS3TokenizerDescriptor { + // query_only pragma was added in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) + return _unicode61(removeDiacritics: removeDiacritics, separators: separators, tokenCharacters: tokenCharacters) + } + #endif + + private static func _unicode61(removeDiacritics: Bool = true, separators: Set = [], tokenCharacters: Set = []) -> FTS3TokenizerDescriptor { var arguments: [String] = [] if !removeDiacritics { arguments.append("remove_diacritics=0") diff --git a/GRDB/FTS/FTS4.swift b/GRDB/FTS/FTS4.swift index c6cc80304..0087dcfec 100644 --- a/GRDB/FTS/FTS4.swift +++ b/GRDB/FTS/FTS4.swift @@ -263,6 +263,7 @@ public final class FTS4ColumnDefinition { self.isLanguageId = false } + #if USING_CUSTOMSQLITE || USING_SQLCIPHER /// Excludes the column from the full-text index. /// /// try db.create(virtualTable: "persons", using: FTS4()) { t in @@ -275,9 +276,31 @@ public final class FTS4ColumnDefinition { /// - returns: Self so that you can further refine the column definition. @discardableResult public func notIndexed() -> Self { + // notindexed FTS4 option was added in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) self.isIndexed = false return self } + #else + /// Excludes the column from the full-text index. + /// + /// try db.create(virtualTable: "persons", using: FTS4()) { t in + /// t.column("a") + /// t.column("b").notIndexed() + /// } + /// + /// See https://www.sqlite.org/fts3.html#the_notindexed_option + /// + /// - returns: Self so that you can further refine the column definition. + @available(iOS 8.2, OSX 10.10, *) + @discardableResult + public func notIndexed() -> Self { + // notindexed FTS4 option was added in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) + self.isIndexed = false + return self + } + #endif /// Uses the column as the Int32 language id hidden column. /// diff --git a/GRDB/QueryInterface/TableDefinition.swift b/GRDB/QueryInterface/TableDefinition.swift index ec30dfe6d..a1f0cffac 100644 --- a/GRDB/QueryInterface/TableDefinition.swift +++ b/GRDB/QueryInterface/TableDefinition.swift @@ -123,7 +123,8 @@ extension Database { try execute("DROP TABLE \(name.quotedDatabaseIdentifier)") } - /// Creates a database index. + #if USING_CUSTOMSQLITE || USING_SQLCIPHER + /// Creates an index. /// /// try db.create(index: "personByEmail", on: "person", columns: ["email"]) /// @@ -144,10 +145,62 @@ extension Database { /// - condition: If not nil, creates a partial index /// (see https://www.sqlite.org/partialindex.html). public func create(index name: String, on table: String, columns: [String], unique: Bool = false, ifNotExists: Bool = false, condition: SQLExpressible? = nil) throws { + // Partial indexes were introduced in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) let definition = IndexDefinition(name: name, table: table, columns: columns, unique: unique, ifNotExists: ifNotExists, condition: condition?.sqlExpression) let sql = definition.sql() try execute(sql) } + #else + /// Creates an index. + /// + /// try db.create(index: "personByEmail", on: "person", 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("CREATE INDEX ...") + /// + /// See https://www.sqlite.org/lang_createindex.html + /// + /// - 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 false, no error is thrown if index already exists. + public func create(index name: String, on table: String, columns: [String], unique: Bool = false, ifNotExists: Bool = false) throws { + // Partial indexes were introduced in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) + let definition = IndexDefinition(name: name, table: table, columns: columns, unique: unique, ifNotExists: ifNotExists, condition: nil) + let sql = definition.sql() + try execute(sql) + } + + /// Creates a partial index. + /// + /// try db.create(index: "personByEmail", on: "person", columns: ["email"], condition: Column("email") != nil) + /// + /// See https://www.sqlite.org/lang_createindex.html, and + /// https://www.sqlite.org/partialindex.html + /// + /// - 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 false, no error is thrown if index already exists. + /// - condition: The condition that indexed rows must verify. + @available(iOS 8.2, OSX 10.10, *) + public func create(index name: String, on table: String, columns: [String], unique: Bool = false, ifNotExists: Bool = false, condition: SQLExpressible) throws { + // Partial indexes were introduced in SQLite 3.8.0 http://www.sqlite.org/changes.html#version_3_8_0 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) + let definition = IndexDefinition(name: name, table: table, columns: columns, unique: unique, ifNotExists: ifNotExists, condition: condition.sqlExpression) + let sql = definition.sql() + try execute(sql) + } + #endif /// Deletes a database index. /// diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift index b53daeab6..98c8fdb08 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift @@ -880,7 +880,7 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { let dbPool = try makeDatabasePool() try dbPool.write { db in - try db.execute("CREATE VIRTUAL TABLE search USING fts3(title, tokenize = unicode61)") + try db.execute("CREATE VIRTUAL TABLE search USING fts3(title)") } try dbPool.read { db in _ = try Row.fetchAll(db, "SELECT * FROM search") diff --git a/Tests/Public/FTS/FTS3TableBuilderTests.swift b/Tests/Public/FTS/FTS3TableBuilderTests.swift index 9cb978828..65cf580b3 100644 --- a/Tests/Public/FTS/FTS3TableBuilderTests.swift +++ b/Tests/Public/FTS/FTS3TableBuilderTests.swift @@ -63,6 +63,12 @@ class FTS3TableBuilderTests: GRDBTestCase { } func testUnicode61Tokenizer() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS3()) { t in @@ -73,6 +79,12 @@ class FTS3TableBuilderTests: GRDBTestCase { } func testUnicode61TokenizerRemoveDiacritics() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS3()) { t in @@ -83,6 +95,12 @@ class FTS3TableBuilderTests: GRDBTestCase { } func testUnicode61TokenizerSeparators() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS3()) { t in @@ -93,6 +111,12 @@ class FTS3TableBuilderTests: GRDBTestCase { } func testUnicode61TokenizerTokenCharacters() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS3()) { t in diff --git a/Tests/Public/FTS/FTS3TokenizerTests.swift b/Tests/Public/FTS/FTS3TokenizerTests.swift index 41eaf1a6f..11e9372db 100644 --- a/Tests/Public/FTS/FTS3TokenizerTests.swift +++ b/Tests/Public/FTS/FTS3TokenizerTests.swift @@ -60,6 +60,12 @@ class FTS3TokenizerTests: GRDBTestCase { } func testUnicode61Tokenizer() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS3()) { t in @@ -81,6 +87,12 @@ class FTS3TokenizerTests: GRDBTestCase { } func testUnicode61TokenizerRemoveDiacritics() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS3()) { t in @@ -102,6 +114,12 @@ class FTS3TokenizerTests: GRDBTestCase { } func testUnicode61TokenizerSeparators() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS3()) { t in @@ -116,6 +134,12 @@ class FTS3TokenizerTests: GRDBTestCase { } func testUnicode61TokenizerTokenCharacters() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS3()) { t in diff --git a/Tests/Public/FTS/FTS4TableBuilderTests.swift b/Tests/Public/FTS/FTS4TableBuilderTests.swift index 7df14e886..e5d1feb2e 100644 --- a/Tests/Public/FTS/FTS4TableBuilderTests.swift +++ b/Tests/Public/FTS/FTS4TableBuilderTests.swift @@ -63,6 +63,12 @@ class FTS4TableBuilderTests: GRDBTestCase { } func testUnicode61Tokenizer() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS4()) { t in @@ -73,6 +79,12 @@ class FTS4TableBuilderTests: GRDBTestCase { } func testUnicode61TokenizerRemoveDiacritics() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS4()) { t in @@ -83,6 +95,12 @@ class FTS4TableBuilderTests: GRDBTestCase { } func testUnicode61TokenizerSeparators() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS4()) { t in @@ -93,6 +111,12 @@ class FTS4TableBuilderTests: GRDBTestCase { } func testUnicode61TokenizerTokenCharacters() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS4()) { t in @@ -122,6 +146,12 @@ class FTS4TableBuilderTests: GRDBTestCase { } func testNotIndexedColumns() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "books", using: FTS4()) { t in diff --git a/Tests/Public/SQLTableBuilder/SQLTableBuilderTests.swift b/Tests/Public/SQLTableBuilder/SQLTableBuilderTests.swift index 101af1906..15bfcb24a 100644 --- a/Tests/Public/SQLTableBuilder/SQLTableBuilderTests.swift +++ b/Tests/Public/SQLTableBuilder/SQLTableBuilderTests.swift @@ -451,11 +451,34 @@ class SQLTableBuilderTests: GRDBTestCase { try db.create(index: "test_on_a", on: "test", columns: ["a"]) XCTAssertEqual(self.lastSQLQuery, "CREATE INDEX \"test_on_a\" ON \"test\"(\"a\")") + try db.create(index: "test_on_a_b", on: "test", columns: ["a", "b"], unique: true, ifNotExists: true) + XCTAssertEqual(self.lastSQLQuery, "CREATE UNIQUE INDEX IF NOT EXISTS \"test_on_a_b\" ON \"test\"(\"a\", \"b\")") + + // Sanity check + XCTAssertEqual(try Set(db.indexes(on: "test").map { $0.name }), ["test_on_a", "test_on_a_b"]) + } + } + + func testCreatePartialIndex() throws { + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "test") { t in + t.column("id", .integer).primaryKey() + t.column("a", .text) + 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) XCTAssertEqual(self.lastSQLQuery, "CREATE UNIQUE INDEX IF NOT EXISTS \"test_on_a_b\" ON \"test\"(\"a\", \"b\") WHERE (\"a\" = 1)") // Sanity check - XCTAssertEqual(try Set(db.indexes(on: "test").map { $0.name }), ["test_on_a", "test_on_a_b"]) + XCTAssertEqual(try Set(db.indexes(on: "test").map { $0.name }), ["test_on_a_b"]) } } diff --git a/Tests/Public/SQLTableBuilder/VirtualTableModuleTests.swift b/Tests/Public/SQLTableBuilder/VirtualTableModuleTests.swift index a70201ba3..e37179431 100644 --- a/Tests/Public/SQLTableBuilder/VirtualTableModuleTests.swift +++ b/Tests/Public/SQLTableBuilder/VirtualTableModuleTests.swift @@ -51,6 +51,14 @@ private final class FTS3TokenizeTableDefinition { class VirtualTableModuleTests: GRDBTestCase { func testCustomVirtualTableModule() throws { + // fts3tokenize was introduced in SQLite 3.7.17 https://www.sqlite.org/changes.html#version_3_7_17 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "test", using: FTS3TokenizeModule()) { t in @@ -62,6 +70,14 @@ class VirtualTableModuleTests: GRDBTestCase { } func testThrowingCustomVirtualTableModule() throws { + // fts3tokenize was introduced in SQLite 3.7.17 https://www.sqlite.org/changes.html#version_3_7_17 + // It is available from iOS 8.2 and OS X 10.10 https://github.com/yapstudios/YapDatabase/wiki/SQLite-version-(bundled-with-OS) + #if !USING_CUSTOMSQLITE && !USING_SQLCIPHER + guard #available(iOS 8.2, OSX 10.10, *) else { + return + } + #endif + let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in do {