Skip to content

Commit

Permalink
Removed duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Jan 7, 2025
1 parent 1f78139 commit d12a741
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
return emptyList()
}

return queryWithAttachedRowId(list).foldAndClose {
mapCursorRowToEntity(
it,
it.getInt(ROW_ID)
)
}
return query(list, null)
}

override fun getCount(list: String): Int {
Expand Down Expand Up @@ -209,23 +204,35 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
updateRowIdTables()
}

override fun query(list: String, query: Query): List<Entity.Saved> {
return databaseConnection.withConnection {
readableDatabase
.rawQuery(
"""
SELECT *, i.$ROW_ID
FROM "$list" e, "${getRowIdTableName(list)}" i
WHERE e._id = i._id AND ${query.selection}
ORDER BY i.$ROW_ID
""".trimIndent(),
query.selectionArgs
)
override fun query(list: String, query: Query?): List<Entity.Saved> {
return if (query == null) {
databaseConnection.withConnection {
readableDatabase
.rawQuery(
"""
SELECT *, i.$ROW_ID
FROM "$list" e, "${getRowIdTableName(list)}" i
WHERE e._id = i._id
ORDER BY i.$ROW_ID
""".trimIndent(),
null
)
}
} else {
databaseConnection.withConnection {
readableDatabase
.rawQuery(
"""
SELECT *, i.$ROW_ID
FROM "$list" e, "${getRowIdTableName(list)}" i
WHERE e._id = i._id AND ${query.selection}
ORDER BY i.$ROW_ID
""".trimIndent(),
query.selectionArgs
)
}
}.foldAndClose {
mapCursorRowToEntity(
it,
it.getInt(ROW_ID)
)
mapCursorRowToEntity(it, it.getInt(ROW_ID))
}
}

Expand All @@ -234,13 +241,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
return null
}

return queryWithAttachedRowId(
list,
selectionColumn = EntitiesTable.COLUMN_ID,
selectionArg = id
).first {
mapCursorRowToEntity(it, it.getInt(ROW_ID))
}
return query(list, Query.Eq(EntitiesTable.COLUMN_ID, id)).firstOrNull()
}

override fun getAllByProperty(
Expand All @@ -257,17 +258,12 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}

return if (propertyExists) {
queryWithAttachedRowId(
query(
list,
selectionColumn = EntitiesTable.getPropertyColumn(property),
selectionArg = value
).foldAndClose {
mapCursorRowToEntity(it, it.getInt(ROW_ID))
}
Query.Eq(EntitiesTable.getPropertyColumn(property), value)
)
} else if (value == "") {
queryWithAttachedRowId(list).foldAndClose {
mapCursorRowToEntity(it, it.getInt(ROW_ID))
}
query(list, null)
} else {
emptyList()
}
Expand All @@ -278,64 +274,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
return null
}

return databaseConnection.withConnection {
readableDatabase
.rawQuery(
"""
SELECT *, i.$ROW_ID
FROM "$list" e, "${getRowIdTableName(list)}" i
WHERE e._id = i._id AND i.$ROW_ID = ?
""".trimIndent(),
arrayOf((index + 1).toString())
).first {
mapCursorRowToEntity(it, it.getInt(ROW_ID))
}
}
}

private fun queryWithAttachedRowId(list: String): Cursor {
return databaseConnection.withConnection {
readableDatabase
.rawQuery(
"""
SELECT *, i.$ROW_ID
FROM "$list" e, "${getRowIdTableName(list)}" i
WHERE e._id = i._id
ORDER BY i.$ROW_ID
""".trimIndent(),
null
)
}
}

private fun queryWithAttachedRowId(
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 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)
)
}
}
return query(list, Query.Eq("i.$ROW_ID", (index + 1).toString())).firstOrNull()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface EntitiesRepository {
fun clear()
fun addList(list: String)
fun delete(id: String)
fun query(list: String, query: Query): List<Entity.Saved>
fun query(list: String, query: Query?): List<Entity.Saved>
fun getById(list: String, id: String): 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 @@ -48,9 +48,13 @@ class InMemEntitiesRepository : EntitiesRepository {
}
}

override fun query(list: String, query: Query): List<Entity.Saved> {
override fun query(list: String, query: Query?): List<Entity.Saved> {
val entities = getEntities(list)

if (query == null) {
return entities
}

return when (query) {
is Query.Eq -> {
entities.filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ private class MeasurableEntitiesRepository(private val wrapped: EntitiesReposito
wrapped.delete(id)
}

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

0 comments on commit d12a741

Please sign in to comment.