android|android room 动态化sql查询实践
实践就是自己一个一个的拼凑....
fun getSiteListByDB(
event: CloseSiteFilterEvent,
mWarmBean: BookCaseWarmBean?
) {
val mDataList = arrayListOf()
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 = https://www.it610.com/article/mDataList
}
【android|android room 动态化sql查询实践】dao中的代码:
@RawQuery
abstract fun find(sql:SupportSQLiteQuery): List?
推荐阅读
- android第三方框架(五)ButterKnife
- Android中的AES加密-下
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)
- android防止连续点击的简单实现(kotlin)
- Android|Android install 多个设备时指定设备