Room - 如何从 DAO 查询中获取 COUNT 值?

Room - How to get COUNT value from DAO query?

我在我的 Room DAO 中有一个查询(对于提供的 countryId)查询我的 share_groups table 和 returns 行,其中包括 num_twitter_users 数字(通过计算相关 share_group_individuals table 中填充的行数计算得出)。

这是查询代码:

@Query("SELECT `share_groups`.`_id`, `share_group_name`, `share_group_description`, `share_group_instructions`, `share_group_order`, `share_group_is_visible`, `share_groups`.`last_modified`" +

    // `num_twitter_users` will contain the number of share_group_individuals within this share_group that have a Twitter username.
    ", COUNT(DISTINCT(CASE WHEN (`share_group_individuals`.`share_group_individual_twitter` IS NOT NULL AND `share_group_individuals`.`share_group_individual_twitter` != '') THEN `share_group_individuals`.`_id` ELSE NULL END)) AS `num_twitter_users`" +

    " FROM `share_groups`" +

    // Join with `share_group_individuals` - on countryId
    " LEFT JOIN `share_group_individuals` ON (`share_group_individuals`.`share_group_id` = `share_groups`.`_id`" +
    " AND `share_group_individuals`.`share_group_individual_country_id` = :countryId)" +

    // Search on countryId
    " WHERE `share_group_individuals`.`share_group_individual_country_id` = :countryId" +

    " GROUP BY `share_groups`.`share_group_name`, `share_group_individuals`.`share_group_id`" +

    // Only return share_groups that contain at least 1 share_group_individual with a Twitter account
    " HAVING `num_twitter_users` > 0" +

    " ORDER BY `share_group_order` ASC")
LiveData<List<ShareGroup>> getAllShareGroups(long countryId);

但是,我无法从 returned LivaData<List<ShareGroup>> 访问 num_twitter_users 值。

我想知道部分解决方案是否可能是创建一个 ShareGroupWithData POJO(因此 return 从我的查询中创建一个 LivaData<List<ShareGroupWithData>>)但是,如果这是正确的方法,我将如何在 ShareGroupWithData class?

中填充 numTwitterUsers 变量

我无法在网上或 official documentation 中找到任何相关信息,因此请指出正确的方向 and/or 一些示例代码将不胜感激。

为了完整起见,我的 share_groups table 由这个实体表示:

@Entity(tableName = "share_groups")
public class ShareGroup {

    @PrimaryKey
    @ColumnInfo(name = "_id")
    private long id;

    @NonNull
    @ColumnInfo(name = "share_group_name")
    private String shareGroupName;

    @ColumnInfo(name = "share_group_description")
    private String shareGroupDescription;

    @ColumnInfo(name = "share_group_instructions")
    private String shareGroupInstructions;

    @ColumnInfo(name = "share_group_order", defaultValue = "10")
    private int shareGroupOrder;

    @ColumnInfo(name = "share_group_is_visible", defaultValue = "1")
    private boolean shareGroupIsVisible;

    @ColumnInfo(name = "last_modified")
    private long lastModified;

...
}

而我的 share_groups_individual table 由这个实体表示:

@Entity(tableName = "share_group_individuals",
        indices = {@Index(name = "index_share_group_id", value = "share_group_id")},
        foreignKeys = @ForeignKey(entity = ShareGroup.class, parentColumns = "_id", childColumns = "share_group_id", onDelete = ForeignKey.CASCADE)
)
public class ShareGroupIndividual {

    @PrimaryKey
    @ColumnInfo(name = "_id")
    private long id;

    @ColumnInfo(name = "share_group_id")
    private long shareGroupId;

    @ColumnInfo(name = "share_group_individual_country_id", defaultValue = "-1")
    private long shareGroupIndividualCountryId;

    @ColumnInfo(name = "share_group_individual_name")
    private String shareGroupIndividualName;

    @ColumnInfo(name = "share_group_individual_twitter")
    private String shareGroupIndividualTwitter;

...
}

最后,我通过创建一个 POJO 解决了这个问题:

public class ShareGroupWithData {

    @Embedded
    public ShareGroup shareGroup;

    @ColumnInfo(name = "num_twitter_users", defaultValue = "0")
    public int numTwitterUsers;
...
}

我的 DAO 代码更改为:

LiveData<List<ShareGroupWithData>> getAllShareGroupsWithData(long countryId);