Revise fetchArticlesMatching function to use FTS.
This commit is contained in:
parent
af2d66d467
commit
b957753f5c
@ -501,9 +501,37 @@ private extension ArticlesTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func fetchArticlesMatching(_ searchString: String, _ database: FMDatabase) -> Set<Article> {
|
func fetchArticlesMatching(_ searchString: String, _ database: FMDatabase) -> Set<Article> {
|
||||||
let whereClause = "(starred=1 or dateArrived>?) and userDeleted=0 and (textMatchesSearchString(title,?) or textMatchesSearchString(contentHTML,?) or textMatchesSearchString(contentText,?) or textMatchesSearchString(summary,?))"
|
let sql = "select rowid from search where search match ?;"
|
||||||
let parameters: [AnyObject] = [articleCutoffDate as AnyObject, searchString as AnyObject, searchString as AnyObject, searchString as AnyObject, searchString as AnyObject]
|
let sqlSearchString = sqliteSearchString(with: searchString)
|
||||||
return self.fetchArticlesWithWhereClause(database, whereClause: whereClause, parameters: parameters, withLimits: false)
|
let searchStringParameters = [sqlSearchString]
|
||||||
|
guard let resultSet = database.executeQuery(sql, withArgumentsIn: searchStringParameters) else {
|
||||||
|
return Set<Article>()
|
||||||
|
}
|
||||||
|
let searchRowIDs = resultSet.mapToSet { $0.longLongInt(forColumn: DatabaseKey.rowID) }
|
||||||
|
resultSet.close()
|
||||||
|
if searchRowIDs.isEmpty {
|
||||||
|
return Set<Article>()
|
||||||
|
}
|
||||||
|
|
||||||
|
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(searchRowIDs.count))!
|
||||||
|
let whereClause = "searchRowID in \(placeholders);"
|
||||||
|
let parameters: [AnyObject] = Array(searchRowIDs) as [AnyObject]
|
||||||
|
return self.fetchArticlesWithWhereClause(database, whereClause: whereClause, parameters: parameters, withLimits: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sqliteSearchString(with searchString: String) -> String {
|
||||||
|
var s = ""
|
||||||
|
searchString.enumerateSubstrings(in: searchString.startIndex..<searchString.endIndex, options: .byWords) { (word, range, enclosingRange, stop) in
|
||||||
|
guard let word = word else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s += word
|
||||||
|
if s != "AND" && s != "OR" {
|
||||||
|
s += "*"
|
||||||
|
}
|
||||||
|
s += " "
|
||||||
|
}
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func articlesWithSQL(_ sql: String, _ parameters: [AnyObject], _ database: FMDatabase) -> Set<Article> {
|
func articlesWithSQL(_ sql: String, _ parameters: [AnyObject], _ database: FMDatabase) -> Set<Article> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user