mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Add availability checks for FTS3 unicode61 tokenizer, FTS4 unindexed columns, and partial indexes.
This commit is contained in:
1 parent
ae0928e686
commit
7a90b9ce23
9 files changed
+224
-3
No files matched your search
@@ -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<Character> = [], tokenCharacters: Set<Character> = []) -> 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<Character> = [], tokenCharacters: Set<Character> = []) -> 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<Character> = [], tokenCharacters: Set<Character> = []) -> FTS3TokenizerDescriptor {
|
||||
var arguments: [String] = []
|
||||
if !removeDiacritics {
|
||||
arguments.append("remove_diacritics=0")
|
||||
|
||||
@@ -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.
|
||||
///
|
||||
|
||||
@@ -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.
|
||||
///
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user