Skip to content

Commit c228b3c

Browse files
committed
Removed redundant code
1 parent a5eba56 commit c228b3c

File tree

7 files changed

+20
-426
lines changed

7 files changed

+20
-426
lines changed

collect_app/src/main/java/org/odk/collect/android/database/entities/DatabaseEntitiesRepository.kt

+12-129
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
155155
return emptyList()
156156
}
157157

158-
return queryEqualWithAttachedRowId(list).foldAndClose {
159-
mapCursorRowToEntity(
160-
it,
161-
it.getInt(ROW_ID)
162-
)
163-
}
158+
return queryWithAttachedRowId(list, "", null)
164159
}
165160

166161
override fun getCount(list: String): Int {
@@ -208,7 +203,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
208203
updateRowIdTables()
209204
}
210205

211-
override fun query(list: String, selection: String, selectionArgs: Array<String>): List<Entity.Saved> {
206+
override fun queryWithAttachedRowId(list: String, selection: String, selectionArgs: Array<String>?): List<Entity.Saved> {
212207
return databaseConnection.withConnection {
213208
readableDatabase
214209
.rawQuery(
@@ -221,10 +216,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
221216
selectionArgs
222217
)
223218
}.foldAndClose {
224-
mapCursorRowToEntity(
225-
it,
226-
it.getInt(ROW_ID)
227-
)
219+
mapCursorRowToEntity(it, it.getInt(ROW_ID))
228220
}
229221
}
230222

@@ -233,41 +225,11 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
233225
return null
234226
}
235227

236-
return queryEqualWithAttachedRowId(
237-
list,
238-
selectionColumn = EntitiesTable.COLUMN_ID,
239-
selectionArg = id
240-
).first {
241-
mapCursorRowToEntity(it, it.getInt(ROW_ID))
242-
}
243-
}
244-
245-
override fun getByIdNot(list: String, id: String): List<Entity.Saved> {
246-
if (!listExists(list)) {
247-
return emptyList()
248-
}
249-
250-
return queryNotEqualWithAttachedRowId(
228+
return queryWithAttachedRowId(
251229
list,
252-
selectionColumn = EntitiesTable.COLUMN_ID,
253-
selectionArg = id
254-
).foldAndClose {
255-
mapCursorRowToEntity(it, it.getInt(ROW_ID))
256-
}
257-
}
258-
259-
override fun getByLabel(list: String, label: String?): List<Entity.Saved> {
260-
if (!listExists(list)) {
261-
return emptyList()
262-
}
263-
264-
return queryEqualWithAttachedRowId(
265-
list,
266-
selectionColumn = EntitiesTable.COLUMN_LABEL,
267-
selectionArg = label
268-
).foldAndClose {
269-
mapCursorRowToEntity(it, it.getInt(ROW_ID))
270-
}
230+
EntitiesTable.COLUMN_ID,
231+
arrayOf(id)
232+
).first()
271233
}
272234

273235
override fun getAllByProperty(
@@ -284,17 +246,13 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
284246
}
285247

286248
return if (propertyExists) {
287-
queryEqualWithAttachedRowId(
249+
queryWithAttachedRowId(
288250
list,
289-
selectionColumn = EntitiesTable.getPropertyColumn(property),
290-
selectionArg = value
291-
).foldAndClose {
292-
mapCursorRowToEntity(it, it.getInt(ROW_ID))
293-
}
251+
EntitiesTable.getPropertyColumn(property),
252+
arrayOf(value)
253+
)
294254
} else if (value == "") {
295-
queryEqualWithAttachedRowId(list).foldAndClose {
296-
mapCursorRowToEntity(it, it.getInt(ROW_ID))
297-
}
255+
queryWithAttachedRowId(list, "", null)
298256
} else {
299257
emptyList()
300258
}
@@ -320,81 +278,6 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
320278
}
321279
}
322280

323-
private fun queryEqualWithAttachedRowId(list: String): Cursor {
324-
return databaseConnection.withConnection {
325-
readableDatabase
326-
.rawQuery(
327-
"""
328-
SELECT *, i.$ROW_ID
329-
FROM "$list" e, "${getRowIdTableName(list)}" i
330-
WHERE e._id = i._id
331-
ORDER BY i.$ROW_ID
332-
""".trimIndent(),
333-
null
334-
)
335-
}
336-
}
337-
338-
private fun queryEqualWithAttachedRowId(
339-
list: String,
340-
selectionColumn: String,
341-
selectionArg: String?
342-
): Cursor {
343-
return databaseConnection.withConnection {
344-
if (selectionArg == null) {
345-
readableDatabase.rawQuery(
346-
"""
347-
SELECT *, i.$ROW_ID
348-
FROM "$list" e, "${getRowIdTableName(list)}" i
349-
WHERE e._id = i._id AND $selectionColumn IS NULL
350-
ORDER BY i.$ROW_ID
351-
""".trimIndent(),
352-
null
353-
)
354-
} else {
355-
readableDatabase.rawQuery(
356-
"""
357-
SELECT *, i.$ROW_ID
358-
FROM "$list" e, "${getRowIdTableName(list)}" i
359-
WHERE e._id = i._id AND $selectionColumn = ?
360-
ORDER BY i.$ROW_ID
361-
""".trimIndent(),
362-
arrayOf(selectionArg)
363-
)
364-
}
365-
}
366-
}
367-
368-
private fun queryNotEqualWithAttachedRowId(
369-
list: String,
370-
selectionColumn: String,
371-
selectionArg: String?
372-
): Cursor {
373-
return databaseConnection.withConnection {
374-
if (selectionArg == null) {
375-
readableDatabase.rawQuery(
376-
"""
377-
SELECT *, i.$ROW_ID
378-
FROM "$list" e, "${getRowIdTableName(list)}" i
379-
WHERE e._id = i._id AND $selectionColumn IS NOT NULL
380-
ORDER BY i.$ROW_ID
381-
""".trimIndent(),
382-
null
383-
)
384-
} else {
385-
readableDatabase.rawQuery(
386-
"""
387-
SELECT *, i.$ROW_ID
388-
FROM "$list" e, "${getRowIdTableName(list)}" i
389-
WHERE e._id = i._id AND $selectionColumn != ?
390-
ORDER BY i.$ROW_ID
391-
""".trimIndent(),
392-
arrayOf(selectionArg)
393-
)
394-
}
395-
}
396-
}
397-
398281
/**
399282
* Dropping and recreating this table on every change allows to maintain a sequential
400283
* "positions" for each entity that can be used as [Entity.Saved.index]. This method appears

collect_app/src/test/java/org/odk/collect/android/entities/EntitiesRepositoryTest.kt

-142
Original file line numberDiff line numberDiff line change
@@ -515,148 +515,6 @@ abstract class EntitiesRepositoryTest {
515515
assertThat(queriedCanet, equalTo(otherFavouriteWines.first { it.id == "2" }))
516516
}
517517

518-
@Test
519-
fun `#getByIdNot returns entities with not matching id`() {
520-
val repository = buildSubject()
521-
522-
val leoville = Entity.New("1", "Léoville Barton 2008")
523-
val canet = Entity.New("2", "Pontet-Canet 2014")
524-
val ardbeg = Entity.New("3", "Ardbeg 10")
525-
repository.save("wines", leoville, canet, ardbeg)
526-
527-
val wines = repository.getEntities("wines")
528-
529-
val queriedLeoville = repository.getByIdNot("wines", "2")
530-
assertThat(queriedLeoville, containsInAnyOrder(wines.first { it.id == "1" }, wines.first { it.id == "3" }))
531-
}
532-
533-
@Test
534-
fun `#getByIdNot returns empty list when there are no matches`() {
535-
val repository = buildSubject()
536-
537-
val leoville = Entity.New("1", "Léoville Barton 2008")
538-
repository.save("wines", leoville)
539-
540-
assertThat(repository.getByIdNot("wines", "1"), equalTo(emptyList()))
541-
}
542-
543-
@Test
544-
fun `#getByIdNot returns empty list when there is a match in a different list`() {
545-
val repository = buildSubject()
546-
547-
val leoville = Entity.New("1", "Léoville Barton 2008")
548-
val ardbeg = Entity.New("2", "Ardbeg 10")
549-
repository.save("wines", leoville)
550-
repository.save("whisky", ardbeg)
551-
552-
assertThat(repository.getByIdNot("whisky", "2"), equalTo(emptyList()))
553-
}
554-
555-
@Test
556-
fun `#getByIdNot returns empty list where there are no entities in the list`() {
557-
val repository = buildSubject()
558-
assertThat(repository.getByIdNot("wines", "3"), equalTo(emptyList()))
559-
}
560-
561-
@Test
562-
fun `#getByIdNot supports list names with dots and dashes`() {
563-
val repository = buildSubject()
564-
565-
val leoville = Entity.New("1", "Léoville Barton 2008")
566-
val canet = Entity.New("2", "Pontet-Canet 2014")
567-
repository.save("favourite-wines", leoville)
568-
repository.save("other.favourite.wines", canet)
569-
570-
val favouriteWines = repository.getEntities("favourite-wines")
571-
val otherFavouriteWines = repository.getEntities("other.favourite.wines")
572-
573-
val queriedLeoville = repository.getByIdNot("favourite-wines", "2")
574-
assertThat(queriedLeoville, contains(favouriteWines.first { it.id == "1" }))
575-
576-
val queriedCanet = repository.getByIdNot("other.favourite.wines", "1")
577-
assertThat(queriedCanet, contains(otherFavouriteWines.first { it.id == "2" }))
578-
}
579-
580-
@Test
581-
fun `#getByLabel returns entities with matching label`() {
582-
val repository = buildSubject()
583-
584-
val leoville1 = Entity.New("1", "Léoville Barton 2008")
585-
val canet = Entity.New("2", "Pontet-Canet 2014")
586-
val leoville2 = Entity.New("3", "Léoville Barton 2008")
587-
588-
repository.save("wines", leoville1, canet, leoville2)
589-
590-
val wines = repository.getEntities("wines")
591-
592-
val queriedLeoville = repository.getByLabel("wines", "Léoville Barton 2008")
593-
assertThat(queriedLeoville, containsInAnyOrder(wines.first { it.id == "1" }, wines.first { it.id == "3" }))
594-
595-
val queriedCanet = repository.getByLabel("wines", "Pontet-Canet 2014")
596-
assertThat(queriedCanet, contains(wines.first { it.id == "2" }))
597-
}
598-
599-
@Test
600-
fun `#getByLabel returns entities with matching label when label is null`() {
601-
val repository = buildSubject()
602-
603-
val leoville = Entity.New("1", null)
604-
val canet = Entity.New("2", "Pontet-Canet 2014")
605-
repository.save("wines", leoville, canet)
606-
607-
val queriedNull = repository.getByLabel("wines", null)
608-
val wines = repository.getEntities("wines")
609-
assertThat(queriedNull, contains(wines.first { it.id == "1" }))
610-
}
611-
612-
@Test
613-
fun `#getByLabel returns empty list when there are no matches`() {
614-
val repository = buildSubject()
615-
616-
val leoville = Entity.New("1", "Léoville Barton 2008")
617-
val canet = Entity.New("2", "Pontet-Canet 2014")
618-
repository.save("wines", leoville, canet)
619-
620-
assertThat(repository.getByLabel("wines", "Ardbeg 10"), equalTo(emptyList()))
621-
}
622-
623-
@Test
624-
fun `#getByLabel returns empty list when there is a match in a different list`() {
625-
val repository = buildSubject()
626-
627-
val leoville = Entity.New("1", "Léoville Barton 2008")
628-
val ardbeg = Entity.New("2", "Ardbeg 10")
629-
repository.save("wines", leoville)
630-
repository.save("whisky", ardbeg)
631-
632-
assertThat(repository.getByLabel("whisky", "Léoville Barton 2008"), equalTo(emptyList()))
633-
}
634-
635-
@Test
636-
fun `#getByLabel returns empty list where there are no entities in the list`() {
637-
val repository = buildSubject()
638-
assertThat(repository.getByLabel("wines", "Léoville Barton 2008"), equalTo(emptyList()))
639-
}
640-
641-
@Test
642-
fun `#getByLabel supports list names with dots and dashes`() {
643-
val repository = buildSubject()
644-
645-
val leoville = Entity.New("1", "Léoville Barton 2008")
646-
val canet = Entity.New("2", "Pontet-Canet 2014")
647-
repository.save("favourite-wines", leoville)
648-
repository.save("other.favourite.wines", canet)
649-
650-
val favouriteWines = repository.getEntities("favourite-wines")
651-
val otherFavouriteWines = repository.getEntities("other.favourite.wines")
652-
653-
val queriedLeoville = repository.getByLabel("favourite-wines", "Léoville Barton 2008")
654-
assertThat(queriedLeoville, contains(favouriteWines.first { it.id == "1" }))
655-
656-
val queriedCanet = repository.getByLabel("other.favourite.wines", "Pontet-Canet 2014")
657-
assertThat(queriedCanet, contains(otherFavouriteWines.first { it.id == "2" }))
658-
}
659-
660518
@Test
661519
fun `#getByAllByProperty returns entities with matching property value`() {
662520
val repository = buildSubject()

0 commit comments

Comments
 (0)