Skip to content

Commit

Permalink
Fix query issue with large ids
Browse files Browse the repository at this point in the history
  • Loading branch information
ismartcoding committed Aug 25, 2023
1 parent 889926f commit 36824e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ abstract class BaseContentHelper {
}

abstract fun getWhere(query: String): ContentWhere

open fun getWheres(query: String): List<ContentWhere> {
return listOf(getWhere(query))
}

abstract fun getProjection(): Array<String>

open fun count(context: Context, query: String): Int {
val where = getWhere(query)
return getWheres(query).sumOf { count(context, it) }
}

private fun count(context: Context, where: ContentWhere): Int {
var result = 0
if (isQPlus()) {
context.contentResolver.query(uriExternal, null, Bundle().apply {
Expand Down Expand Up @@ -107,7 +115,7 @@ abstract class BaseContentHelper {
}

open fun deleteByIds(context: Context, ids: Set<String>) {
ids.chunked(30).forEach { chunk ->
ids.chunked(500).forEach { chunk ->
val selection = "${BaseColumns._ID} IN (${StringHelper.getQuestionMarks(chunk.size)})"
val selectionArgs = chunk.map { it }.toTypedArray()
context.contentResolver.delete(uriExternal, selection, selectionArgs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.provider.MediaStore
import com.ismartcoding.lib.content.ContentWhere
import com.ismartcoding.lib.extensions.getLongValue
import com.ismartcoding.lib.extensions.getStringValue
import com.ismartcoding.lib.helpers.FilterField
import com.ismartcoding.lib.helpers.SearchHelper
import com.ismartcoding.plain.data.DMediaBucket
import com.ismartcoding.plain.features.BaseContentHelper
Expand All @@ -24,24 +25,59 @@ object ImageHelper : BaseContentHelper() {
}

override fun getWhere(query: String): ContentWhere {
if (query.isNotEmpty()) {
val queryGroups = SearchHelper.parse(query)
val where = getBaseWhere(queryGroups)
val idsGroup = queryGroups.firstOrNull { it.name == "ids" }
if (idsGroup != null) {
val ids = idsGroup.value.split(",")
if (ids.isNotEmpty()) {
where.addIn(MediaStore.Images.Media._ID, ids)
}
}
return where
}

return ContentWhere()
}

private fun getBaseWhere(groups: List<FilterField>): ContentWhere {
val where = ContentWhere()
groups.forEach {
if (it.name == "text") {
where.add("${MediaStore.Images.Media.TITLE} LIKE ?", "%${it.value}%")
} else if (it.name == "bucket_id") {
where.add("${MediaStore.Images.Media.BUCKET_ID} = ?", it.value)
}
}
return where
}

override fun getWheres(query: String): List<ContentWhere> {
val wheres = mutableListOf<ContentWhere>()
if (query.isNotEmpty()) {
val queryGroups = SearchHelper.parse(query)
queryGroups.forEach {
if (it.name == "text") {
where.add("${MediaStore.Images.Media.TITLE} LIKE ?", "%${it.value}%")
} else if (it.name == "bucket_id") {
where.add("${MediaStore.Images.Media.BUCKET_ID} = ?", it.value)
} else if (it.name == "ids") {
val ids = it.value.split(",")
if (ids.isNotEmpty()) {
where.addIn(MediaStore.Images.Media._ID, ids)
val where = getBaseWhere(queryGroups)
val idsGroup = queryGroups.firstOrNull { it.name == "ids" }
if (idsGroup != null) {
val ids = idsGroup.value.split(",")
if (ids.isNotEmpty()) {
ids.chunked(2000).forEach {
val w = where.copy()
w.addIn(MediaStore.Images.Media._ID, it)
wheres.add(w)
}
} else {
wheres.add(where)
}
} else {
wheres.add(where)
}
} else {
wheres.add(ContentWhere())
}

return where
return wheres
}

fun search(context: Context, query: String, limit: Int, offset: Int, sortBy: FileSortBy): List<DImage> {
Expand Down

0 comments on commit 36824e7

Please sign in to comment.