Skip to content

Commit

Permalink
Implemented #getByIdNot
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Jan 7, 2025
1 parent 65e89c5 commit 15fb0d5
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}
}

override fun getByIdNot(list: String, id: String): List<Entity.Saved> {
if (!listExists(list)) {
return emptyList()
}

return queryNotEqualWithAttachedRowId(
list,
selectionColumn = EntitiesTable.COLUMN_ID,
selectionArg = id
).foldAndClose {
mapCursorRowToEntity(it, it.getInt(ROW_ID))
}
}

override fun getByLabel(list: String, label: String?): List<Entity.Saved> {
if (!listExists(list)) {
return emptyList()
Expand Down Expand Up @@ -331,6 +345,36 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}
}

private fun queryNotEqualWithAttachedRowId(
list: String,
selectionColumn: String,
selectionArg: String?
): Cursor {
return databaseConnection.withConnection {
if (selectionArg == null) {
readableDatabase.rawQuery(
"""
SELECT *, i.$ROW_ID
FROM "$list" e, "${getRowIdTableName(list)}" i
WHERE e._id = i._id AND $selectionColumn IS NOT NULL
ORDER BY i.$ROW_ID
""".trimIndent(),
null
)
} else {
readableDatabase.rawQuery(
"""
SELECT *, i.$ROW_ID
FROM "$list" e, "${getRowIdTableName(list)}" i
WHERE e._id = i._id AND $selectionColumn != ?
ORDER BY i.$ROW_ID
""".trimIndent(),
arrayOf(selectionArg)
)
}
}
}

/**
* Dropping and recreating this table on every change allows to maintain a sequential
* "positions" for each entity that can be used as [Entity.Saved.index]. This method appears
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,31 @@ class LocalEntitiesFilterStrategy(entitiesRepository: EntitiesRepository) :
val candidate = CompareToNodeExpression.parse(predicate)
return when (val original = candidate?.original) {
is XPathEqExpr -> {
if (original.isEqual) {
val child = candidate.nodeSide.steps[0].name.name
val value = candidate.evalContextSide(sourceInstance, evaluationContext)
val child = candidate.nodeSide.steps[0].name.name
val value = candidate.evalContextSide(sourceInstance, evaluationContext)

val results = instanceAdapter.queryEq(
val results = if (original.isEqual) {
instanceAdapter.queryEq(
sourceInstance.instanceId,
child,
value as String
)
} else {
instanceAdapter.queryNotEq(
sourceInstance.instanceId,
child,
value as String
)
}

if (results == null) {
next.get()
} else {
sourceInstance.replacePartialElements(results)
results.map {
it.parent = sourceInstance.root
it.ref
}
} else {
next.get()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos
}
}

fun queryNotEq(instanceId: String, child: String, value: String): List<TreeElement>? {
return when (child) {
EntityItemElement.ID -> {
entitiesRepository.getByIdNot(
instanceId,
value
).map { convertToElement(it) }
}

else -> null
}
}

private fun filterAndConvertEntities(
list: String,
filter: (Entity.Saved) -> Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface EntitiesRepository {
fun addList(list: String)
fun delete(id: String)
fun getById(list: String, id: String): Entity.Saved?
fun getByIdNot(list: String, id: String): List<Entity.Saved>
fun getByLabel(list: String, label: String?): List<Entity.Saved>
fun getAllByProperty(list: String, property: String, value: String): List<Entity.Saved>
fun getByIndex(list: String, index: Int): Entity.Saved?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class InMemEntitiesRepository : EntitiesRepository {
return getEntities(list).firstOrNull { it.id == id }
}

override fun getByIdNot(list: String, id: String): List<Entity.Saved> {
return getEntities(list).filter { it.id != id }
}

override fun getByLabel(list: String, label: String?): List<Entity.Saved> {
return getEntities(list).filter { it.label == label }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,11 @@ private class MeasurableEntitiesRepository(private val wrapped: EntitiesReposito
return wrapped.getById(list, id)
}

override fun getByIdNot(list: String, id: String): List<Entity.Saved> {
accesses += 1
return wrapped.getByIdNot(list, id)
}

override fun getByLabel(list: String, label: String?): List<Entity.Saved> {
accesses += 1
return wrapped.getByLabel(list, label)
Expand Down

0 comments on commit 15fb0d5

Please sign in to comment.