Build manually the item selection query

This commit is contained in:
Shinokuni 2021-05-13 21:21:14 +02:00
parent b9d6e6d135
commit d9b31d4cfa
3 changed files with 40 additions and 8 deletions

View File

@ -14,6 +14,7 @@ import com.readrops.db.Database;
import com.readrops.db.entities.Item;
import com.readrops.db.entities.account.Account;
import com.readrops.db.pojo.ItemWithFeed;
import com.readrops.db.queries.ItemSelectionQueryBuilder;
import org.koin.core.parameter.DefinitionParametersKt;
import org.koin.java.KoinJavaComponent;
@ -39,7 +40,8 @@ public class ItemViewModel extends ViewModel {
}
public LiveData<ItemWithFeed> getItemById(int id) {
return database.itemDao().getItemById(id);
return database.itemDao().getItemById(ItemSelectionQueryBuilder.buildQuery(id,
account.getConfig().useSeparateState()));
}
public Completable setStarState(Item item) {

View File

@ -6,7 +6,6 @@ import androidx.paging.DataSource;
import androidx.room.Dao;
import androidx.room.Query;
import androidx.room.RawQuery;
import androidx.room.RoomWarnings;
import androidx.sqlite.db.SupportSQLiteQuery;
import com.readrops.db.entities.Feed;
@ -53,11 +52,8 @@ public interface ItemDao extends BaseDao<Item> {
@Query("Select count(*) From Item Where feed_id = :feedId And read = 0")
int getUnreadCount(int feedId);
@SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
@Query("Select Item.id, Item.remoteId, title, Item.description, content, link, pub_date, image_link, author, read, text_color, " +
"background_color, read_time, starred, Feed.name, Feed.id as feedId, siteUrl, Folder.id as folder_id, " +
"Folder.name as folder_name from Item Inner Join Feed On Item.feed_id = Feed.id Left Join Folder on Folder.id = Feed.folder_id Where Item.id = :id")
LiveData<ItemWithFeed> getItemById(int id);
@RawQuery(observedEntities = {Item.class, ItemState.class})
LiveData<ItemWithFeed> getItemById(SupportSQLiteQuery query);
@Query("Select Item.remoteId From Item Inner Join Feed On Item.feed_id = Feed.id Where read = 1 And account_id = :accountId")
List<String> getReadChanges(int accountId);

View File

@ -0,0 +1,34 @@
package com.readrops.db.queries
import androidx.sqlite.db.SupportSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQueryBuilder
object ItemSelectionQueryBuilder {
private val COLUMNS = arrayOf("Item.id", "Item.remoteId", "title", "Item.description", "content",
"link", "pub_date", "image_link", "author", "Item.read", "text_color",
"background_color", "read_time", "Feed.name", "Feed.id as feedId", "siteUrl",
"Folder.id as folder_id", "Folder.name as folder_name")
private val SEPARATE_STATE_COLUMNS = arrayOf("case When ItemState.starred = 1 Then 1 else 0 End starred")
private const val JOIN = "Item Inner Join Feed On Item.feed_id = Feed.id Left Join Folder on Folder.id = Feed.folder_id"
private const val SEPARATE_STATE_JOIN = " Left Join ItemState On ItemState.remote_id = Item.remoteId"
/**
* @param separateState Indicates if item state must be retrieved from ItemState table
*/
@JvmStatic
fun buildQuery(itemId: Int, separateState: Boolean): SupportSQLiteQuery {
val tables = if (separateState) JOIN + SEPARATE_STATE_JOIN else JOIN
val columns = if (separateState) COLUMNS.plus(SEPARATE_STATE_COLUMNS) else COLUMNS.plus("starred")
return SupportSQLiteQueryBuilder.builder(tables).run {
columns(columns)
selection("Item.id = $itemId", null)
create()
}
}
}