Implement TopicsFts

pull/1323/head
lihenggui 2 years ago
parent ef2fe0252d
commit 5d1988a65a

@ -16,24 +16,45 @@
package com.google.samples.apps.nowinandroid.core.database.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.coroutines.mapToOne
import com.google.samples.apps.nowinandroid.core.database.NiaDatabase
import com.google.samples.apps.nowinandroid.core.database.model.TopicFtsEntity
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
/**
* DAO for [TopicFtsEntity] access.
*/
@Dao
interface TopicFtsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(topics: List<TopicFtsEntity>)
class TopicFtsDao(db: NiaDatabase, private val dispatcher: CoroutineDispatcher) {
@Query("SELECT topicId FROM topicsFts WHERE topicsFts MATCH :query")
fun searchAllTopics(query: String): Flow<List<String>>
private val dbQuery = db.topicFtsQueries
@Query("SELECT count(*) FROM topicsFts")
fun getCount(): Flow<Int>
suspend fun insertAll(topics: List<TopicFtsEntity>) {
topics.forEach {
dbQuery.insert(
topic_id = it.topicId,
name = it.name,
short_description = it.shortDescription,
long_description = it.longDescription,
)
}
}
fun searchAllTopics(query: String): Flow<List<String>> {
return dbQuery.searchAllTopics(query) {
it.orEmpty()
}
.asFlow()
.mapToList(dispatcher)
}
fun getCount(): Flow<Int> {
return dbQuery.getCount()
.asFlow()
.mapToOne(dispatcher)
.map { it.toInt() }
}
}

@ -26,7 +26,7 @@ BEGIN
WHERE topic_id = new.id;
END;
insertAll:
insert:
INSERT INTO topic_fts (topic_id, name, short_description, long_description)
VALUES (?, ?, ?, ?)
ON CONFLICT(topic_id) DO UPDATE SET
Loading…
Cancel
Save