I'm trying to query an FTS table using SQLite.swift. Previously I have done it in Android. The essence of what I am trying to do is this:
SELECT *
FROM t2
WHERE id IN (SELECT docid
FROM fts_table
WHERE col_text MATCH 'something')
From the SQLite.swift documentation I see the IN
condition can be written like this:
users.filter([1, 2, 3, 4, 5].contains(id))
// SELECT * FROM "users" WHERE ("id" IN (1, 2, 3, 4, 5))
And virtual tables can be queried like this:
let wonderfulEmails = emails.match("wonder*")
// SELECT * FROM "emails" WHERE "emails" MATCH 'wonder*'
let replies = emails.filter(subject.match("Re:*"))
// SELECT * FROM "emails" WHERE "subject" MATCH 'Re:*'
However, I can't figure out how to combine these. I don't really want to have to execute arbitrary SQL (although this is now working thanks to help from this answer).
Update
Here is my most recent try that is not working:
let tableText = Table("t2")
let id = Expression<Int64>("id")
let someColumn = Expression<String>("someColumn")
let tableFts = VirtualTable("fts_table")
let columnDocId = Expression<Int64>("docId")
let ftsQuery = tableFts
.select(columnDocId)
.filter(tableFts.match("something"))
let tableQuery = tableText
.filter((try db.prepare(ftsQuery))
.contains(id)) // <-- error
for row in try db.prepare(tableQuery) {
print(row[someColumn])
}
where the line with contains(id)
throws the error:
Cannot convert value of type 'Expression' to expected argument type '@noescape (Row) throws -> Bool'