Android 房间一对多并计算值

Android room one to many and compute values

我在房间里有一个一对多的关系。 我需要获取数据列表并在每个对象上计算一个 属性 作为金额总和。 我可以为此写一个查询吗?或者我必须迭代代码中的值?

更新


@Entity(tableName = "account_table")
class AccountModel(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,
    var name: String,
    var bankNumber: String,
    var amount: Double
)


@Entity(tableName = "spent_table")
data class SpentModel(
    var amount: Double,
    var title: String,
    var description: String,
    var date: String?,
    var accountId: Int,
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,
)

data class AccountWithSpent(
    @Embedded val account: AccountModel,
    @Relation(
        parentColumn = "id",
        entityColumn = "accountId"
    )
    val spent: List<SpentModel>
)

AccountWithSpent 是我的数据的一对多关系,我想从支出中获取所有金额并将其存储到帐户对象金额 属性。

Can i write a query for that?

or i must iterate over the values in the code?

不,但你可以。

注意 虽然内容可能有用,但您可能希望直接到底部并阅读 Additional/Correction 部分。

假设您想在检索一个或多个 AccountWithSpent 对象时获得花费的金额总和,那么您可以为此在 AccountWithSpent 例如:-

data class AccountWithSpent(
    @Embedded val account: AccountModel,
    @Relation(
        parentColumn = "id",
        entityColumn = "accountId"
    )
    val spent: List<SpentModel>,
    val sumOfSpent: Double /*<<<<< ADDED for SUM*/
)

然后您可以进行如下查询:-

@Transaction
@Query("SELECT *, (SELECT sum(spent_table.amount)  FROM spent_table WHERE spent_table.accountId = a.id) AS sumOfSpent   FROM account_table AS a")
    fun getAccountWithSpent(): List<AccountWithSpent>
  • AS sumOfSpent 很重要,它命名要从中获取数据的列与 POJO 中的字段名称相同,它们必须匹配。
  • AS a 在子查询的 WHERE 子句中使用时消除 id 列的歧义 ((SELECT sum(spent_table.amount) FROM spent_table WHERE spent_table.accountId = a.id))
    • 即account_table 和 spent_table 都有一个 id 列。所以使用 a.id 强制使用 account_table id.

您也可以有这样的查询:-

@Transaction
@Query("SELECT account_table.*, sum(spent_table.amount) AS sumOfSpent FROM account_table JOIN spent_table ON account_table.id = spent_table.accountId GROUP BY account_table.id")
fun getAccountWithSpentV2(): List<AccountWithSpent>
  • 这不使用子查询,而是 JOIN 的 tables 并使用 GROUP BY 将行分组到帐户中。
  • 为了抑制未使用的列(来自 spent_table),Room 发出警告,account_table.* 用于定义输出列(即仅来自帐户 [=133= 的列) ])
    • Room 再次从账户构建底层 Spent 对象

备选

如果您只想要带有 Sum 的帐户而不是基础 Spent 对象的列表,那么您可以使用以下内容:-

data class AccountWithSumOfSpent(
    @Embedded
    val account: AccountModel,
    val sumOfSpent: Double
)

与 :-

@Query("SELECT *, sum(spent_table.amount) AS sumOfSpent FROM account_table JOIN spent_table ON account_table.id = spent_table.accountId GROUP BY account_table.id")
fun getAccountWithSumofSpent(): List<AccountWithSumOfSpent>
  • 注意 @Transaction 不是必需的,因为所有数据都是通过单个查询提取的

    • 子查询嵌入到主查询中。但是,当使用 @Relation Room 时,除了主查询之外还会构建和执行查询,因此它会警告使用 @Transaction

使用这两种方法的工作示例

    /* ADD 1 Account with 3 Spends (totalling 0.00) */
    val account1Id = dao.insert(AccountModel(0,"BANKX","000000",0.00)).toInt()
    dao.insert(SpentModel(30.00,"SPEND1","BUYA","2021-06-25",account1Id))
    dao.insert(SpentModel(30.00,"SPEND2","BUYB","2021-06-24",account1Id))
    dao.insert(SpentModel(40.00,"SPEND3","BUYC","2021-06-23",account1Id))

    /* Extract and Log using first method (Account, with Sum and with all Spends) */
    for(a: AccountWithSpent in dao.getAccountWithSpent()) {
        Log.d("ACCNTINFO","Account Name is ${a.account.name} Bank Number is ${a.account.bankNumber} SUM of Spend is ${a.sumOfSpent}")
        //Log.d("ACCNTINFO","Account Name is ${a.account.name} Bank Number is ${a.account.bankNumber}")
        for(s: SpentModel in a.spent) {
            Log.d("ACCNTINFO","Title is \t${s.title} Description is ${s.description} Date is ${s.date} Amount is ${s.amount}")
        }
    }
    /* Extract and Log using second method (no Spends) */
    for(a: AccountWithSumOfSpent in dao.getAccountWithSumofSpent()) {
        Log.d("ACCNTINFO", "Account Name is ${a.account.name} Bank Number is ${a.account.bankNumber} SUM of Spend is ${a.sumOfSpent}")
    }

结果

日志包括:-

2021-06-26 07:58:55.265 D/ACCNTINFO: Account Name is BANKX Bank Number is 000000 SUM of Spend is 100.0
2021-06-26 07:58:55.265 D/ACCNTINFO: Title is   SPEND1 Description is BUYA Date is 2021-06-25 Amount is 30.0
2021-06-26 07:58:55.265 D/ACCNTINFO: Title is   SPEND2 Description is BUYB Date is 2021-06-24 Amount is 30.0
2021-06-26 07:58:55.265 D/ACCNTINFO: Title is   SPEND3 Description is BUYC Date is 2021-06-23 Amount is 40.0


2021-06-26 07:58:55.268 D/ACCNTINFO: Account Name is BANKX Bank Number is 000000 SUM of Spend is 100.0

Additional/Correction

I need to fetch list of data and calculate one property on every object as sum of amounts. AccountWithSpent is one to many relation of my data and i want get all amounts from spent and store it to account object amount property.

我没有正确阅读以上内容,因此根据您的原始 AccountWithSpent,您可以使用:-

@Transaction
@Query("SELECT account_table.id,account_table.name, account_table.bankNumber, sum(spent_table.amount) AS amount FROM account_table JOIN spent_table ON account_table.id = spent_table.accountId GROUP BY account_table.id")
fun getAccountWithSpent(): List<AccountWithSpent>

AccountModel 的金额将相应地填充。

但是,需要注意的是数据库中的金额还是会保持原来的样子,不会改变。一个反问,你需要存入数据库的金额,因为它总是可以计算的吗?

也许 AccountModel 有:-

.....
@Ignore 
var amount: Double

如果您想要数据库中的金额,那么也许您应该始终维护数据库中的金额。如果是这样,那么您可能希望考虑在支出为 added/deleted 或更新时使用 TRIGGER 以自动调整金额 (如果是这样,那将是另一个问题).