Skip to content

Commit

Permalink
Removed redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Jan 7, 2025
1 parent ebb2f39 commit dec9d71
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 347 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,34 +243,6 @@ 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()
}

return queryEqualWithAttachedRowId(
list,
selectionColumn = EntitiesTable.COLUMN_LABEL,
selectionArg = label
).foldAndClose {
mapCursorRowToEntity(it, it.getInt(ROW_ID))
}
}

override fun getAllByProperty(
list: String,
property: String,
Expand Down Expand Up @@ -366,36 +338,6 @@ 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 @@ -515,148 +515,6 @@ abstract class EntitiesRepositoryTest {
assertThat(queriedCanet, equalTo(otherFavouriteWines.first { it.id == "2" }))
}

@Test
fun `#getByIdNot returns entities with not matching id`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")
val canet = Entity.New("2", "Pontet-Canet 2014")
val ardbeg = Entity.New("3", "Ardbeg 10")
repository.save("wines", leoville, canet, ardbeg)

val wines = repository.getEntities("wines")

val queriedLeoville = repository.getByIdNot("wines", "2")
assertThat(queriedLeoville, containsInAnyOrder(wines.first { it.id == "1" }, wines.first { it.id == "3" }))
}

@Test
fun `#getByIdNot returns empty list when there are no matches`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")
repository.save("wines", leoville)

assertThat(repository.getByIdNot("wines", "1"), equalTo(emptyList()))
}

@Test
fun `#getByIdNot returns empty list when there is a match in a different list`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")
val ardbeg = Entity.New("2", "Ardbeg 10")
repository.save("wines", leoville)
repository.save("whisky", ardbeg)

assertThat(repository.getByIdNot("whisky", "2"), equalTo(emptyList()))
}

@Test
fun `#getByIdNot returns empty list where there are no entities in the list`() {
val repository = buildSubject()
assertThat(repository.getByIdNot("wines", "3"), equalTo(emptyList()))
}

@Test
fun `#getByIdNot supports list names with dots and dashes`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")
val canet = Entity.New("2", "Pontet-Canet 2014")
repository.save("favourite-wines", leoville)
repository.save("other.favourite.wines", canet)

val favouriteWines = repository.getEntities("favourite-wines")
val otherFavouriteWines = repository.getEntities("other.favourite.wines")

val queriedLeoville = repository.getByIdNot("favourite-wines", "2")
assertThat(queriedLeoville, contains(favouriteWines.first { it.id == "1" }))

val queriedCanet = repository.getByIdNot("other.favourite.wines", "1")
assertThat(queriedCanet, contains(otherFavouriteWines.first { it.id == "2" }))
}

@Test
fun `#getByLabel returns entities with matching label`() {
val repository = buildSubject()

val leoville1 = Entity.New("1", "Léoville Barton 2008")
val canet = Entity.New("2", "Pontet-Canet 2014")
val leoville2 = Entity.New("3", "Léoville Barton 2008")

repository.save("wines", leoville1, canet, leoville2)

val wines = repository.getEntities("wines")

val queriedLeoville = repository.getByLabel("wines", "Léoville Barton 2008")
assertThat(queriedLeoville, containsInAnyOrder(wines.first { it.id == "1" }, wines.first { it.id == "3" }))

val queriedCanet = repository.getByLabel("wines", "Pontet-Canet 2014")
assertThat(queriedCanet, contains(wines.first { it.id == "2" }))
}

@Test
fun `#getByLabel returns entities with matching label when label is null`() {
val repository = buildSubject()

val leoville = Entity.New("1", null)
val canet = Entity.New("2", "Pontet-Canet 2014")
repository.save("wines", leoville, canet)

val queriedNull = repository.getByLabel("wines", null)
val wines = repository.getEntities("wines")
assertThat(queriedNull, contains(wines.first { it.id == "1" }))
}

@Test
fun `#getByLabel returns empty list when there are no matches`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")
val canet = Entity.New("2", "Pontet-Canet 2014")
repository.save("wines", leoville, canet)

assertThat(repository.getByLabel("wines", "Ardbeg 10"), equalTo(emptyList()))
}

@Test
fun `#getByLabel returns empty list when there is a match in a different list`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")
val ardbeg = Entity.New("2", "Ardbeg 10")
repository.save("wines", leoville)
repository.save("whisky", ardbeg)

assertThat(repository.getByLabel("whisky", "Léoville Barton 2008"), equalTo(emptyList()))
}

@Test
fun `#getByLabel returns empty list where there are no entities in the list`() {
val repository = buildSubject()
assertThat(repository.getByLabel("wines", "Léoville Barton 2008"), equalTo(emptyList()))
}

@Test
fun `#getByLabel supports list names with dots and dashes`() {
val repository = buildSubject()

val leoville = Entity.New("1", "Léoville Barton 2008")
val canet = Entity.New("2", "Pontet-Canet 2014")
repository.save("favourite-wines", leoville)
repository.save("other.favourite.wines", canet)

val favouriteWines = repository.getEntities("favourite-wines")
val otherFavouriteWines = repository.getEntities("other.favourite.wines")

val queriedLeoville = repository.getByLabel("favourite-wines", "Léoville Barton 2008")
assertThat(queriedLeoville, contains(favouriteWines.first { it.id == "1" }))

val queriedCanet = repository.getByLabel("other.favourite.wines", "Pontet-Canet 2014")
assertThat(queriedCanet, contains(otherFavouriteWines.first { it.id == "2" }))
}

@Test
fun `#getByAllByProperty returns entities with matching property value`() {
val repository = buildSubject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,73 +45,6 @@ class LocalEntitiesInstanceAdapter(private val entitiesRepository: EntitiesRepos
.map { convertToElement(it) }
}

fun queryEq(instanceId: String, child: String, value: String): List<TreeElement> {
return when (child) {
EntityItemElement.ID -> {
val entity = entitiesRepository.getById(
instanceId,
value
)

if (entity != null) {
listOf(convertToElement(entity))
} else {
emptyList()
}
}

EntityItemElement.LABEL -> {
entitiesRepository.getByLabel(
instanceId,
value
).map { convertToElement(it) }
}

EntityItemElement.VERSION -> {
filterAndConvertEntities(instanceId) { it.version == value.toInt() }
}

EntityItemElement.TRUNK_VERSION -> {
filterAndConvertEntities(instanceId) { it.trunkVersion == value.toInt() }
}

EntityItemElement.BRANCH_ID -> {
filterAndConvertEntities(instanceId) { it.branchId == value }
}

else -> {
val entities = entitiesRepository.getAllByProperty(
instanceId,
child,
value
)

entities.map { convertToElement(it) }
}
}
}

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
): List<TreeElement> {
val entities = entitiesRepository.getEntities(list)
return entities.filter(filter).map { convertToElement(it) }
}

private fun convertToElement(entity: Entity.Saved): TreeElement {
val name = TreeElement(EntityItemElement.ID)
val label = TreeElement(EntityItemElement.LABEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ interface EntitiesRepository {
fun delete(id: String)
fun query(list: String, query: Query): List<Entity.Saved>
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?
fun updateListHash(list: String, hash: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ 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 }
}

override fun getAllByProperty(
list: String,
property: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,6 @@ 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)
}

override fun getAllByProperty(
list: String,
property: String,
Expand Down

This file was deleted.

0 comments on commit dec9d71

Please sign in to comment.