房间特权与查询的关系

时间:2018-07-25 09:10:41

标签: android android-room

我有两个桌子。

餐桌甲板:

Decks:
-id
-name

桌卡:

Cards:
-id
-name
-deckId

我为此查询创建了一个数据类:

data class DeckWithDueDatedCards(

        @Embedded
        var deckEntity: DeckEntity,

        var list: List<CardEntity>

)

我删除了@Relation列表的顶部,因为它查询了所有卡。 我想添加一个条件。因此,我不需要甲板上的所有卡。 让我们看看:

我创建了下一个查询:

@Query("SELECT *, (SELECT  * FROM cards WHERE deckId = D.id AND dueDate < date('now')) as list  FROM decks D WHERE D.id = :deckId")
fun getDeckWithDueDatedCards(deckId: Long): Flowable<DeckWithDueDatedCards>

但是我得到一个错误,因为内部选择错误。它不能使列表膨胀。

There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (sub-select returns 8 columns - expected 1)
public abstract io.reactivex.Flowable<....database.entity.DeckWithDueDatedCards> getDeckWithDueDatedCards(long deckId);

我看到问题是内部选择有8个项目,但只有1个除外。但是我在那里有一个列表。如何给它充气?

任何人都知道如何解决此问题?可以查询吗?

1 个答案:

答案 0 :(得分:1)

Room Dao的允许非抽象方法。

如果要getDeckWithDueDatedCards,可以编写一种方法,该方法将首先加载所有CardEntity,然后加载链接的DeckEntity以构建DeckWithDueDatedCards

Java代码:

@Query("SELECT *  FROM decks WHERE id = :deckId")
public abstract DeckEntity loadDeck(int deckId);

@Query("SELECT  * FROM cards WHERE deckId = D.id AND dueDate < :date")
public abstract Flowable<List<CardEntity>> loadCardEntity(String date);

@Transaction
public Flowable<DeckWithDueDatedCards> getDeckWithDueDatedCards(int deckId, String date) {
    return loadCardEntity(date)
            .map(cardEntityList -> {
                List<DeckWithDueDatedCards> res = new ArrayList<>();
                for(CardEntity cardEntity : cardEntityList) {
                    res.add(new DeckWithDueDatedCards(cardEntity, loadDeck(deckId)));
                }
                return res;
            });
}

注意:请注意,对CardEntity的修改将触发您onNext上的subscriber,但是对DeckEntity的修改不会...

编辑:如果您需要在DeckEntity更改更新loadCardEntity查询中得到通知

@Query("SELECT  cards.* FROM cards INNER JOIN decks ON cards.deckId = decks.id WHERE deckId = D.id AND dueDate < :date")
public abstract Flowable<List<CardEntity>> loadCardEntity(String date);