android room 动态化sql查询实践

实践就是自己一个一个的拼凑....

fun getSiteListByDB(
        event: CloseSiteFilterEvent,
        mWarmBean: BookCaseWarmBean?
    ) {
        val mDataList = arrayListOf<SiteBean>()
        val stringBuilder = StringBuilder(" SELECT * FROM site ")
        if (event.siteName.isNotEmpty() || event.bookState.any { it != 0 } || event.siteState.any { it != 0 }) {
            stringBuilder.append(" WHERE ")
            if (event.siteName.isNotEmpty()) {
                stringBuilder.append(" deviceName like '%${event.siteName}%' ")
            }
            if (event.siteName.isNotEmpty() && event.bookState.any { it != 0 }) {
                stringBuilder.append(" and ")
            }
            event.bookState.forEachIndexed { index, bookState ->
                if (index > 0) {
                    stringBuilder.append(" or ")
                }
                when (bookState) {
                    1 -> stringBuilder.append(
                        " inventoryExistCount >= (${mWarmBean?.minBookCount
                            ?: 40} * doorCount) and inventoryExistCount <= (${mWarmBean?.maxBookCount
                            ?: 70} * doorCount) "
                    )
                    2 -> stringBuilder.append(
                        " inventoryExistCount < (${mWarmBean?.minBookCount
                            ?: 40} * doorCount) "
                    )
                    3 -> stringBuilder.append(
                        " inventoryExistCount > (${mWarmBean?.maxBookCount ?: 70} * doorCount) "
                    )
                }
            }

            if ((event.siteName.isNotEmpty() || event.bookState.any { it != 0 }) && event.siteState.any { it != 0 }) {
                stringBuilder.append(" and ")
            }
            event.siteState.forEachIndexed { index, siteState ->
                if (index > 0) {
                    stringBuilder.append(" or ")
                }
                when (siteState) {
                    1 -> stringBuilder.append(" deviceState = 'CONNECT_ONLINE' ")
                    2 -> stringBuilder.append(" deviceState = 'CONNECT_OFFLINE' ")
                }
            }
        }
        when (event.sort) {
            1 -> stringBuilder.append(" order by deviceState")
            2 -> stringBuilder.append(" order by inventoryExistCount")
            3 -> stringBuilder.append(" order by inventoryExistCount desc")
        }
        d(stringBuilder)
        DBHelper.instance.siteDao().find(SimpleSQLiteQuery(stringBuilder.toString()))?.also { mDataList.addAll(it) }
        d(mDataList)
        mDBSiteList.value = mDataList
    }

dao中的代码:

@RawQuery
    abstract fun find(sql:SupportSQLiteQuery): List<SiteBean>?
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 虽然你走的路不一定对,也不是一条好走的路。但你走的是你自己选的路,你在做你自己,让自己越来越好。所以,非常...
    万物向上阅读 596评论 0 26
  • 前节概要:狠下决心,索引主动联系了许敏。孙虎重色轻友。 六 照例是桌餐。十几天下来,用餐的桌签已遁行。索引望了望,...
    简丰阅读 3,027评论 102 83
  • 此时,我与我后背的疼痛在一起。 生命在痛的地方,不知道是否是这样子的。 今晚危娜老师简单提到的那个性,这触动了我自...
    CherryHuang阅读 388评论 8 10
  • 还记得去年我们闹得不可开交的时候,这棵树被我差点删除,可是最后我们挺过来了,我又重新把你拉了进来。 我对你说,我想...
    初见月夜阅读 126评论 0 1
  • 谋也阅读 186评论 0 0