简体   繁体   English

Android Room在运行时确定查询中的列名称

[英]Android Room determine column name in query at runtime

This is my Value table structure 这是我的Value表结构

varName   value    timestamp    oneHour    oneDay    oneWeek
============================================================
varA      12.7     129387230    true       false     false
varB      32.0     129387230    true       true      false
varA      32.2     129387234    true       true      true
...

As you can see, each entry has one or more flags that determine whether this entry is used in a graph with the corresponding timescale. 如您所见,每个条目都有一个或多个标志,这些标志确定是否在具有相应时标的图形中使用该条目。

In my Dao I have a query to get all values of a timescale for a variable. 在我的Dao我有一个查询以获取变量的时间标度的所有值。 Timescale is just an enum with oneHour, oneDay, oneWeek Timescale oneHour, oneDay, oneWeek只是一个oneHour, oneDay, oneWeek一个oneHour, oneDay, oneWeek一个oneHour, oneDay, oneWeekenum

@Query("SELECT * FROM Value WHERE varName = :varName AND [       ] ORDER BY timestamp ASC")
fun getByTimescale(varName: String, timescale: Timescale): Flowable<List<Value>>

In the bracket, I want to do something like timescale.name = true , but that would throw an error. 在括号中,我想做类似timescale.name = true ,但这会引发错误。

Changing the query at runtime would not be possible as Room generates code during build time based on the SQL query to fetch the result. 在运行时更改查询是不可能的,因为Room在构建期间会根据SQL查询来获取结果,从而生成代码。 Changing query means changing the method implementation. 更改查询意味着更改方法的实现。

You can achieve this by creating 3 queries, one for each enum value. 您可以通过创建3个查询来实现此目的,每个枚举值一个。 Then at runtime, decide which of the 3 methods you need to call. 然后在运行时,确定需要调用3种方法中的哪一种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM