From 6a478ca315febbea846cb442059b2e4c0e8ef5a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 5 Oct 2016 06:17:42 +0200 Subject: [PATCH] Introduce FTS tests --- GRDB.xcodeproj/project.pbxproj | 36 ++++ GRDB/FTS/FTS3.swift | 1 + GRDB/FTS/FTS4.swift | 137 +++++++++---- Tests/Public/FTS/FTS3TableBuilderTests.swift | 47 +++-- Tests/Public/FTS/FTS4RecordTests.swift | 86 ++++++++ Tests/Public/FTS/FTS4TableBuilderTests.swift | 194 +++++++++++++++++++ 6 files changed, 447 insertions(+), 54 deletions(-) create mode 100644 Tests/Public/FTS/FTS4RecordTests.swift create mode 100644 Tests/Public/FTS/FTS4TableBuilderTests.swift diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 0ef39cc13..4744f933b 100755 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -626,6 +626,22 @@ 5698AC8E1DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC881DA389380056AF8C /* FTS3TableBuilderTests.swift */; }; 5698AC8F1DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC881DA389380056AF8C /* FTS3TableBuilderTests.swift */; }; 5698AC901DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC881DA389380056AF8C /* FTS3TableBuilderTests.swift */; }; + 5698AC961DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */; }; + 5698AC971DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */; }; + 5698AC981DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */; }; + 5698AC991DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */; }; + 5698AC9A1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */; }; + 5698AC9B1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */; }; + 5698AC9C1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */; }; + 5698AC9D1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */; }; + 5698AC9E1DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */; }; + 5698AC9F1DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */; }; + 5698ACA01DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */; }; + 5698ACA11DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */; }; + 5698ACA21DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */; }; + 5698ACA31DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */; }; + 5698ACA41DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */; }; + 5698ACA51DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */; }; 569C1EB31CF07DDD0042627B /* DatabaseSchedulerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569C1EB11CF07DDD0042627B /* DatabaseSchedulerTests.swift */; }; 569C1EB41CF07DDD0042627B /* DatabaseSchedulerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569C1EB11CF07DDD0042627B /* DatabaseSchedulerTests.swift */; }; 569C1EB51CF07DDD0042627B /* DatabaseSchedulerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569C1EB11CF07DDD0042627B /* DatabaseSchedulerTests.swift */; }; @@ -1646,6 +1662,8 @@ 5698AC771DA37DCB0056AF8C /* VirtualTableDefinition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VirtualTableDefinition.swift; sourceTree = ""; }; 5698AC7F1DA380A20056AF8C /* VirtualTableBuilderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VirtualTableBuilderTests.swift; sourceTree = ""; }; 5698AC881DA389380056AF8C /* FTS3TableBuilderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS3TableBuilderTests.swift; sourceTree = ""; }; + 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS4RecordTests.swift; sourceTree = ""; }; + 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS4TableBuilderTests.swift; sourceTree = ""; }; 569C1EB11CF07DDD0042627B /* DatabaseSchedulerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseSchedulerTests.swift; sourceTree = ""; }; 56A238131B9C74A90082EB20 /* DatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseTests.swift; sourceTree = ""; }; 56A238141B9C74A90082EB20 /* InMemoryDatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InMemoryDatabaseTests.swift; sourceTree = ""; }; @@ -2174,6 +2192,8 @@ 5698AC3F1DA2BED90056AF8C /* FTS3PatternTests.swift */, 5698AC481DA2D48A0056AF8C /* FTS3RecordTests.swift */, 5698AC881DA389380056AF8C /* FTS3TableBuilderTests.swift */, + 5698AC941DA4B0430056AF8C /* FTS4RecordTests.swift */, + 5698AC951DA4B0430056AF8C /* FTS4TableBuilderTests.swift */, ); path = FTS; sourceTree = ""; @@ -3527,6 +3547,7 @@ 560FC5781CB00B880014AA8E /* MetalRowTests.swift in Sources */, 560FC5791CB00B880014AA8E /* PrimaryKeyMultipleTests.swift in Sources */, 560FC57A1CB00B880014AA8E /* DatabasePoolFileAttributesTests.swift in Sources */, + 5698AC971DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 5657AB4F1D108BA9006283EF /* NSNumberTests.swift in Sources */, 560FC57C1CB00B880014AA8E /* StatementArgumentsTests.swift in Sources */, 5634B1081CF9B970005360B9 /* TransactionObserverSavepointsTests.swift in Sources */, @@ -3565,6 +3586,7 @@ 560FC5961CB00B880014AA8E /* DatabaseValueConvertibleFetchTests.swift in Sources */, 560FC5971CB00B880014AA8E /* DatabaseErrorTests.swift in Sources */, 560FC5991CB00B880014AA8E /* MinimalPrimaryKeySingleTests.swift in Sources */, + 5698AC9F1DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */, 5698AC4A1DA2D48A0056AF8C /* FTS3RecordTests.swift in Sources */, 560FC59B1CB00B880014AA8E /* DatabaseTimestampTests.swift in Sources */, 5698AC411DA2BED90056AF8C /* FTS3PatternTests.swift in Sources */, @@ -3717,6 +3739,7 @@ 567156361CB16729007DC145 /* MetalRowTests.swift in Sources */, 567156371CB16729007DC145 /* PrimaryKeyMultipleTests.swift in Sources */, 5657AB401D108BA9006283EF /* NSDataTests.swift in Sources */, + 5698AC981DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 567156381CB16729007DC145 /* DatabasePoolFileAttributesTests.swift in Sources */, 5671563A1CB16729007DC145 /* StatementArgumentsTests.swift in Sources */, 5657AB601D108BA9006283EF /* NSURLTests.swift in Sources */, @@ -3755,6 +3778,7 @@ 565EFAF01D0436CE00A8FA9D /* NumericOverflowTests.swift in Sources */, 567156551CB16729007DC145 /* DatabaseValueConvertibleFetchTests.swift in Sources */, 567156561CB16729007DC145 /* DatabaseErrorTests.swift in Sources */, + 5698ACA01DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */, 5698AC4B1DA2D48A0056AF8C /* FTS3RecordTests.swift in Sources */, 567156571CB16729007DC145 /* MinimalPrimaryKeySingleTests.swift in Sources */, 5698AC421DA2BED90056AF8C /* FTS3PatternTests.swift in Sources */, @@ -3903,6 +3927,7 @@ 56AFCA531CB1AA9900F48B96 /* DatabaseValueConversionTests.swift in Sources */, 56AFCA541CB1AA9900F48B96 /* MappingTests.swift in Sources */, 5657AB531D108BA9006283EF /* NSNumberTests.swift in Sources */, + 5698AC9B1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 56AFCA551CB1AA9900F48B96 /* RawRepresentableTests.swift in Sources */, 56AFCA561CB1AA9900F48B96 /* DatabasePoolConcurrencyTests.swift in Sources */, 56AFCA571CB1AA9900F48B96 /* TransactionObserverTests.swift in Sources */, @@ -3953,6 +3978,7 @@ 567A80581D41350C00C7DCEC /* IndexInfoTests.swift in Sources */, 56AFCA751CB1AA9900F48B96 /* CGFloatTests.swift in Sources */, 56AFCA761CB1AA9900F48B96 /* StatementInformationTests.swift in Sources */, + 5698ACA31DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */, 5691784A1CED9B6000E179EA /* DatabaseQueueTests.swift in Sources */, 56AFCA771CB1AA9900F48B96 /* GRDBTestCase.swift in Sources */, ); @@ -4013,6 +4039,7 @@ 56A8C24B1D1918F10096E9D4 /* NSUUIDTests.swift in Sources */, 56AFCAAB1CB1ABC800F48B96 /* PersistableTests.swift in Sources */, 56FF45461D2C23BA00F21EF9 /* DeleteByKeyTests.swift in Sources */, + 5698AC9C1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 56AFCAAC1CB1ABC800F48B96 /* DatabaseValueConversionTests.swift in Sources */, 56AFCAAD1CB1ABC800F48B96 /* MappingTests.swift in Sources */, 5657AB441D108BA9006283EF /* NSDataTests.swift in Sources */, @@ -4063,6 +4090,7 @@ 567A80591D41350C00C7DCEC /* IndexInfoTests.swift in Sources */, 56AFCACF1CB1ABC800F48B96 /* StatementInformationTests.swift in Sources */, 56AFCAD01CB1ABC800F48B96 /* GRDBTestCase.swift in Sources */, + 5698ACA41DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */, 565EFAF41D0436CE00A8FA9D /* NumericOverflowTests.swift in Sources */, 5634B0781CF22BEF005360B9 /* FetchRequestTests.swift in Sources */, ); @@ -4207,11 +4235,13 @@ 56A8C2471D1918F00096E9D4 /* NSUUIDTests.swift in Sources */, 5698AC441DA2BED90056AF8C /* FTS3PatternTests.swift in Sources */, 565B0FF01BBC7D980098DE03 /* RowConvertibleTests.swift in Sources */, + 5698AC9A1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 56A4CDB41D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, 56E8CE0E1BB4FA5600828BEC /* DatabaseValueConvertibleFetchTests.swift in Sources */, 56A238581B9C74A90082EB20 /* PrimaryKeyRowIDTests.swift in Sources */, 56A238621B9C74A90082EB20 /* RecordEditedTests.swift in Sources */, 5695312A1C908A5B00CF1A2B /* DatabasePoolSchemaCacheTests.swift in Sources */, + 5698ACA21DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */, 56A2385C1B9C74A90082EB20 /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */, 56A238401B9C74A90082EB20 /* DatabaseValueConvertibleSubclassTests.swift in Sources */, 56A8C2481D1918F00096E9D4 /* UUIDTests.swift in Sources */, @@ -4254,6 +4284,7 @@ 56D496591D81304E008276D7 /* NSDateTests.swift in Sources */, 56D4968B1D81316E008276D7 /* NumericOverflowTests.swift in Sources */, 56D496B31D8133E3008276D7 /* IndexInfoTests.swift in Sources */, + 5698AC961DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 56D4966D1D81309E008276D7 /* MinimalPrimaryKeySingleTests.swift in Sources */, 56D496811D813131008276D7 /* TransactionObserverSavepointsTests.swift in Sources */, 56D496AB1D8132CA008276D7 /* DatabasePoolFunctionTests.swift in Sources */, @@ -4305,6 +4336,7 @@ 56D496B81D813465008276D7 /* DataMemoryTests.swift in Sources */, 56D496541D812F5B008276D7 /* SQLExpressionLiteralTests.swift in Sources */, 56D496961D81317B008276D7 /* PersistableTests.swift in Sources */, + 5698AC9E1DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */, 56D496641D81304E008276D7 /* UUIDTests.swift in Sources */, 56D496921D81316E008276D7 /* RowAsDictionaryLiteralConvertibleTests.swift in Sources */, 56D496751D81309E008276D7 /* RecordEditedTests.swift in Sources */, @@ -4553,11 +4585,13 @@ 56A8C24D1D1918F30096E9D4 /* NSUUIDTests.swift in Sources */, 5698AC471DA2BED90056AF8C /* FTS3PatternTests.swift in Sources */, F3BA80D61CFB2FFD003DC1BA /* DatabaseReaderTests.swift in Sources */, + 5698AC9D1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 56A4CDB71D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */, F3BA80F91CFB3021003DC1BA /* UpdateStatementTests.swift in Sources */, F3BA80DA1CFB300E003DC1BA /* DatabaseTimestampTests.swift in Sources */, F3BA81201CFB3063003DC1BA /* PrimaryKeyMultipleTests.swift in Sources */, F3BA812B1CFB3063003DC1BA /* RecordWithColumnNameManglingTests.swift in Sources */, + 5698ACA51DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */, F3BA80B11CFB2FC4003DC1BA /* DatabaseTests.swift in Sources */, 56A8C24E1D1918F30096E9D4 /* UUIDTests.swift in Sources */, 5657AB3D1D108BA9006283EF /* DataTests.swift in Sources */, @@ -4668,6 +4702,7 @@ F3BA81341CFB3064003DC1BA /* RecordCopyTests.swift in Sources */, F3BA81101CFB3057003DC1BA /* Row+FoundationTests.swift in Sources */, F3BA812C1CFB3064003DC1BA /* MinimalPrimaryKeyRowIDTests.swift in Sources */, + 5698AC991DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */, 56A8C2451D1918EF0096E9D4 /* NSUUIDTests.swift in Sources */, 5657AB591D108BA9006283EF /* NSStringTests.swift in Sources */, 56B14E821D4DAE54000BF4A3 /* RowAsDictionaryLiteralConvertibleTests.swift in Sources */, @@ -4719,6 +4754,7 @@ 5690C33A1D23E7D200E59934 /* DateTests.swift in Sources */, F3BA81001CFB3025003DC1BA /* TransactionObserverTests.swift in Sources */, F3BA80C01CFB2FD2003DC1BA /* DatabasePoolCollationTests.swift in Sources */, + 5698ACA11DA4B0430056AF8C /* FTS4TableBuilderTests.swift in Sources */, F3BA80C31CFB2FD2003DC1BA /* DatabasePoolFunctionTests.swift in Sources */, F3BA80D51CFB2FFB003DC1BA /* DatabaseReaderTests.swift in Sources */, F3BA81321CFB3064003DC1BA /* PrimaryKeySingleWithReplaceConflictResolutionTests.swift in Sources */, diff --git a/GRDB/FTS/FTS3.swift b/GRDB/FTS/FTS3.swift index b0aa6837b..501c4e299 100644 --- a/GRDB/FTS/FTS3.swift +++ b/GRDB/FTS/FTS3.swift @@ -55,6 +55,7 @@ public final class FTS3TableDefinition : VirtualTableDefinition { /// try db.create(virtualTable: "documents", using: FTS3()) { t in /// t.tokenizer = .porter /// } + /// See https://www.sqlite.org/fts3.html#creating_and_destroying_fts_tables public var tokenizer: FTS3Tokenizer? /// Appends a table column. diff --git a/GRDB/FTS/FTS4.swift b/GRDB/FTS/FTS4.swift index 36bf2593d..00a7396b3 100644 --- a/GRDB/FTS/FTS4.swift +++ b/GRDB/FTS/FTS4.swift @@ -4,17 +4,10 @@ /// try db.create(virtualTable: "documents", using: FTS4()) { t in /// t.column("content") /// } +/// +/// See https://www.sqlite.org/fts3.html public struct FTS4 : VirtualTableModule { - /// TODO - public enum Storage { - case regular - case contentless - case externalContent(String) - } - - let storage: Storage - /// Creates a FTS4 module suitable for the Database /// `create(virtualTable:using:)` method. /// @@ -23,24 +16,8 @@ public struct FTS4 : VirtualTableModule { /// t.column("content") /// } /// - /// For contentless FTS4 table, and external content FTS4 table, provide a - /// storage argument: - /// - /// // CREATE VIRTUAL TABLE documents USING fts4(content="", title, body) - /// let fts4 = FTS4(storage: .contentless) - /// try db.create(virtualTable: "documents", using: fts4) { t in - /// t.column("title") - /// t.column("body") - /// } - /// - /// // CREATE VIRTUAL TABLE documents USING fts4(content="source", title, body) - /// // let fts4 = FTS4(storage: .externalContent("source")) - /// try db.create(virtualTable: "documents", using: fts4) { t in - /// t.column("title") - /// t.column("body") - /// } - public init(storage: Storage = .regular) { - self.storage = storage + /// See https://www.sqlite.org/fts3.html + public init() { } // MARK: - VirtualTableModule Adoption @@ -55,7 +32,15 @@ public struct FTS4 : VirtualTableModule { /// Don't use this method. public func moduleArguments(_ definition: FTS4TableDefinition) -> [String] { - var arguments = definition.columns + var arguments: [String] = [] + + for column in definition.columns { + arguments.append(column.name) + if !column.isIndexed { + arguments.append("notindexed=\(column.name)") + } + } + if let tokenizer = definition.tokenizer { if tokenizer.options.isEmpty { arguments.append("tokenize=\(tokenizer.name)") @@ -63,6 +48,27 @@ public struct FTS4 : VirtualTableModule { arguments.append("tokenize=\(tokenizer.name) " + tokenizer.options.map { "\"\($0)\"" as String }.joined(separator: " ")) } } + + if let content = definition.content { + arguments.append("content=\"\(content)\"") + } + + if let compress = definition.compress { + arguments.append("compress=\"\(compress)\"") + } + + if let uncompress = definition.uncompress { + arguments.append("uncompress=\"\(uncompress)\"") + } + + if let languageid = definition.languageid { + arguments.append("languageid=\"\(languageid)\"") + } + + if let prefix = definition.prefix { + arguments.append("prefix=\"\(prefix)\"") + } + return arguments } } @@ -75,30 +81,44 @@ public struct FTS4 : VirtualTableModule { /// try db.create(virtualTable: "documents", using: FTS4()) { t in // t is FTS4TableDefinition /// t.column("content") /// } +/// +/// See https://www.sqlite.org/fts3.html public final class FTS4TableDefinition : VirtualTableDefinition { - fileprivate var columns: [String] = [] + fileprivate var columns: [FTS4ColumnDefinition] = [] /// The virtual table tokenizer /// /// try db.create(virtualTable: "documents", using: FTS4()) { t in /// t.tokenizer = .porter /// } + /// + /// See https://www.sqlite.org/fts3.html#creating_and_destroying_fts_tables public var tokenizer: FTS3Tokenizer? - /// TODO + /// The FTS4 `content` option + /// + /// See https://www.sqlite.org/fts3.html#the_content_option_ + public var content: String? + + /// The FTS4 `compress` option + /// + /// See https://www.sqlite.org/fts3.html#the_compress_and_uncompress_options public var compress: String? - /// TODO + /// The FTS4 `uncompress` option + /// + /// See https://www.sqlite.org/fts3.html#the_compress_and_uncompress_options public var uncompress: String? - /// TODO - public var languageid + /// The FTS4 `languageid` option + /// + /// See https://www.sqlite.org/fts3.html#the_languageid_option + public var languageid: String? - /// TODO (as a column property) - public var notindexed - - /// TODO - public var prefix + /// The FTS4 `prefix` option + /// + /// See https://www.sqlite.org/fts3.html#the_prefix_option + public var prefix: String? /// Appends a table column. /// @@ -107,7 +127,44 @@ public final class FTS4TableDefinition : VirtualTableDefinition { /// } /// /// - parameter name: the column name. - public func column(_ name: String) { - columns.append(name) + @discardableResult public func column(_ name: String) -> FTS4ColumnDefinition { + let column = FTS4ColumnDefinition(name: name) + columns.append(column) + return column + } +} + +/// The FTS4ColumnDefinition class lets you refine a column of an FTS4 +/// virtual table. +/// +/// You get instances of this class when you create an FTS4 table: +/// +/// try db.create(virtualTable: "persons", using: FTS4()) { t in +/// t.column("content") // ColumnDefinition +/// } +/// +/// See https://www.sqlite.org/fts3.html +public final class FTS4ColumnDefinition { + fileprivate let name: String + fileprivate var isIndexed: Bool + + init(name: String) { + self.name = name + self.isIndexed = true + } + + /// 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. + @discardableResult public func notIndexed() -> Self { + self.isIndexed = false + return self } } diff --git a/Tests/Public/FTS/FTS3TableBuilderTests.swift b/Tests/Public/FTS/FTS3TableBuilderTests.swift index cd12ca2e6..44a78662e 100644 --- a/Tests/Public/FTS/FTS3TableBuilderTests.swift +++ b/Tests/Public/FTS/FTS3TableBuilderTests.swift @@ -9,26 +9,15 @@ import XCTest class FTS3TableBuilderTests: GRDBTestCase { - func testColumns() { - assertNoError { - let dbQueue = try makeDatabaseQueue() - try dbQueue.inDatabase { db in - try db.create(virtualTable: "books", using: FTS3()) { t in - t.column("author") - t.column("title") - t.column("body") - } - XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"books\" USING fts3(author, title, body)")) - } - } - } - func testWithoutBody() { assertNoError { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", using: FTS3()) XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts3")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["abc"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["abc"])!, 1) } } } @@ -39,6 +28,9 @@ class FTS3TableBuilderTests: GRDBTestCase { try dbQueue.inDatabase { db in try db.create(virtualTable: "documents", ifNotExists: true, using: FTS3()) XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE IF NOT EXISTS \"documents\" USING fts3")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["abc"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["abc"])!, 1) } } } @@ -51,6 +43,9 @@ class FTS3TableBuilderTests: GRDBTestCase { t.tokenizer = .simple } XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts3(tokenize=simple)")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["abc"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["abc"])!, 1) } } } @@ -63,6 +58,9 @@ class FTS3TableBuilderTests: GRDBTestCase { t.tokenizer = .porter } XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts3(tokenize=porter)")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["frustrated"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["frustration"])!, 1) } } } @@ -130,4 +128,25 @@ class FTS3TableBuilderTests: GRDBTestCase { } } } + + func testColumns() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "books", using: FTS3()) { t in + t.column("author") + t.column("title") + t.column("body") + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"books\" USING fts3(author, title, body)")) + + try db.execute("INSERT INTO books VALUES (?, ?, ?)", arguments: ["Melville", "Moby Dick", "Call me Ishmael."]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: ["Melville"])!, 1) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: ["title:Melville"])!, 0) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE title MATCH ?", arguments: ["Melville"])!, 0) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: ["author:Melville"])!, 1) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE author MATCH ?", arguments: ["Melville"])!, 1) + } + } + } } diff --git a/Tests/Public/FTS/FTS4RecordTests.swift b/Tests/Public/FTS/FTS4RecordTests.swift new file mode 100644 index 000000000..5a95b56ab --- /dev/null +++ b/Tests/Public/FTS/FTS4RecordTests.swift @@ -0,0 +1,86 @@ +import XCTest +#if USING_SQLCIPHER + import GRDBCipher +#elseif USING_CUSTOMSQLITE + import GRDBCustomSQLite +#else + import GRDB +#endif + +private struct Book { + var id: Int64? + let title: String + let author: String + let body: String +} + +extension Book : RowConvertible { + init(row: Row) { + id = row.value(named: "docid") + title = row.value(named: "title") + author = row.value(named: "author") + body = row.value(named: "body") + } +} + +extension Book : MutablePersistable { + static let databaseTableName = "books" + + var persistentDictionary: [String: DatabaseValueConvertible?] { + return [ + "docid": id, + "title": title, + "author": author, + "body": body, + ] + } + + mutating func didInsert(with rowID: Int64, for column: String?) { + id = rowID + } +} + +class FTS4RecordTests: GRDBTestCase { + + override func setup(_ dbWriter: DatabaseWriter) throws { + try dbWriter.write { db in + try db.create(virtualTable: "books", using: FTS4()) { t in + t.column("title") + t.column("author") + t.column("body") + } + } + } + + func testInsertionNotifiesRowId() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + var book = Book(id: nil, title: "Moby Dick", author: "Herman Melville", body: "Call me Ishmael.") + try book.insert(db) + XCTAssertEqual(book.id, 1) + } + } + } + + func testDocIdIsNotSelectedByDefault() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + do { + var book = Book(id: nil, title: "Moby Dick", author: "Herman Melville", body: "Call me Ishmael.") + try book.insert(db) + } + + let request = Book.all() + let row = Row.fetchOne(db, request)! + XCTAssertEqual(row.count, 3) + XCTAssertTrue(row.hasColumn("title")) + XCTAssertTrue(row.hasColumn("author")) + XCTAssertTrue(row.hasColumn("body")) + XCTAssertTrue(!row.hasColumn("docid")) + } + } + } + +} diff --git a/Tests/Public/FTS/FTS4TableBuilderTests.swift b/Tests/Public/FTS/FTS4TableBuilderTests.swift new file mode 100644 index 000000000..599b1ceed --- /dev/null +++ b/Tests/Public/FTS/FTS4TableBuilderTests.swift @@ -0,0 +1,194 @@ +import XCTest +#if USING_SQLCIPHER + import GRDBCipher +#elseif USING_CUSTOMSQLITE + import GRDBCustomSQLite +#else + import GRDB +#endif + +class FTS4TableBuilderTests: GRDBTestCase { + + func testWithoutBody() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "documents", using: FTS4()) + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["abc"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["abc"])!, 1) + } + } + } + + func testOptions() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "documents", ifNotExists: true, using: FTS4()) + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE IF NOT EXISTS \"documents\" USING fts4")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["abc"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["abc"])!, 1) + } + } + } + + func testSimpleTokenizer() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "documents", using: FTS4()) { t in + t.tokenizer = .simple + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=simple)")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["abc"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["abc"])!, 1) + } + } + } + + func testPorterTokenizer() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "documents", using: FTS4()) { t in + t.tokenizer = .porter + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=porter)")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["frustrated"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["frustration"])!, 1) + } + } + } + + func testUnicode61Tokenizer() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "documents", using: FTS4()) { t in + t.tokenizer = .unicode61() + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=unicode61)")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["eéèÉÈ"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["eeeee"])!, 1) + } + } + } + + func testUnicode61TokenizerRemoveDiacritics() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "documents", using: FTS4()) { t in + t.tokenizer = .unicode61(removeDiacritics: false) + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=unicode61 \"remove_diacritics=0\")")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["eéèÉÈ"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["eeeee"])!, 0) + } + } + } + + func testUnicode61TokenizerSeparators() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "documents", using: FTS4()) { t in + t.tokenizer = .unicode61(separators: ["X"]) + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=unicode61 \"separators=X\")")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["abcXdef"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["abcXdef"])!, 1) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["abc"])!, 1) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["def"])!, 1) + } + } + } + + func testUnicode61TokenizerTokenCharacters() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "documents", using: FTS4()) { t in + t.tokenizer = .unicode61(tokenCharacters: Set(".-".characters)) + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=unicode61 \"tokenchars=.-\")") || sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=unicode61 \"tokenchars=-.\")")) + + try db.execute("INSERT INTO documents VALUES (?)", arguments: ["2016-10-04.txt"]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["2016-10-04.txt"])!, 1) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["2016"])!, 0) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["txt"])!, 0) + } + } + } + + func testColumns() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "books", using: FTS4()) { t in + t.column("author") + t.column("title") + t.column("body") + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"books\" USING fts4(author, title, body)")) + + try db.execute("INSERT INTO books VALUES (?, ?, ?)", arguments: ["Melville", "Moby Dick", "Call me Ishmael."]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: ["Melville"])!, 1) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: ["title:Melville"])!, 0) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE title MATCH ?", arguments: ["Melville"])!, 0) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: ["author:Melville"])!, 1) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE author MATCH ?", arguments: ["Melville"])!, 1) + } + } + } + + func testNotIndexedColumns() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "books", using: FTS4()) { t in + t.column("author").notIndexed() + t.column("title") + t.column("body").notIndexed() + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"books\" USING fts4(author, notindexed=author, title, body, notindexed=body)")) + + try db.execute("INSERT INTO books VALUES (?, ?, ?)", arguments: ["Melville", "Moby Dick", "Call me Ishmael."]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: ["Melville"])!, 0) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: ["title:Melville"])!, 0) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE title MATCH ?", arguments: ["Melville"])!, 0) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE books MATCH ?", arguments: ["author:Melville"])!, 0) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM books WHERE author MATCH ?", arguments: ["Melville"])!, 0) + } + } + } + + func testFTS4Options() { + assertNoError { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(virtualTable: "documents", using: FTS4()) { t in + t.content = "" + t.compress = "zip" + t.uncompress = "unzip" + t.languageid = "lid" + t.prefix = "2,4" + t.column("content") + } + XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(content, content=\"\", compress=\"zip\", uncompress=\"unzip\", languageid=\"lid\", prefix=\"2,4\")")) + + try db.execute("INSERT INTO documents (docid, content, lid) VALUES (?, ?, ?)", arguments: [1, "abc", 0]) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ? AND lid=0", arguments: ["abc"])!, 1) + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ? AND lid=1", arguments: ["abc"])!, 0) + } + } + } +}