Skip to content

Commit 910eaa1

Browse files
committed
Fixed mapping collumn names
1 parent f69f4f7 commit 910eaa1

File tree

3 files changed

+63
-34
lines changed

3 files changed

+63
-34
lines changed

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

+47-33
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.odk.collect.db.sqlite.SQLiteDatabaseExt.doesColumnExist
1919
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.getColumnNames
2020
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.query
2121
import org.odk.collect.db.sqlite.SynchronizedDatabaseConnection
22+
import org.odk.collect.entities.javarosa.parse.EntityItemElement
2223
import org.odk.collect.entities.storage.EntitiesRepository
2324
import org.odk.collect.entities.storage.Entity
2425

@@ -156,7 +157,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
156157
return emptyList()
157158
}
158159

159-
return query(list, null)
160+
return queryWithAttachedRowId(list, null)
160161
}
161162

162163
override fun getCount(list: String): Int {
@@ -205,43 +206,24 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
205206
}
206207

207208
override fun query(list: String, query: Query?): List<Entity.Saved> {
208-
return if (query == null) {
209-
databaseConnection.withConnection {
210-
readableDatabase
211-
.rawQuery(
212-
"""
213-
SELECT *, i.$ROW_ID
214-
FROM "$list" e, "${getRowIdTableName(list)}" i
215-
WHERE e._id = i._id
216-
ORDER BY i.$ROW_ID
217-
""".trimIndent(),
218-
null
219-
)
220-
}
221-
} else {
222-
databaseConnection.withConnection {
223-
readableDatabase
224-
.rawQuery(
225-
"""
226-
SELECT *, i.$ROW_ID
227-
FROM "$list" e, "${getRowIdTableName(list)}" i
228-
WHERE e._id = i._id AND ${query.selection}
229-
ORDER BY i.$ROW_ID
230-
""".trimIndent(),
231-
query.selectionArgs
232-
)
209+
return queryWithAttachedRowId(list, query?.copyWithAdjustedColumns { columnName ->
210+
when (columnName) {
211+
EntityItemElement.ID -> EntitiesTable.COLUMN_ID
212+
EntityItemElement.LABEL -> EntitiesTable.COLUMN_LABEL
213+
EntityItemElement.VERSION -> EntitiesTable.COLUMN_VERSION
214+
else -> {
215+
EntitiesTable.getPropertyColumn(columnName)
216+
}
233217
}
234-
}.foldAndClose {
235-
mapCursorRowToEntity(it, it.getInt(ROW_ID))
236-
}
218+
})
237219
}
238220

239221
override fun getById(list: String, id: String): Entity.Saved? {
240222
if (!listExists(list)) {
241223
return null
242224
}
243225

244-
return query(list, Query.Eq(EntitiesTable.COLUMN_ID, id)).firstOrNull()
226+
return queryWithAttachedRowId(list, Query.Eq(EntitiesTable.COLUMN_ID, id)).firstOrNull()
245227
}
246228

247229
override fun getAllByProperty(
@@ -258,12 +240,12 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
258240
}
259241

260242
return if (propertyExists) {
261-
query(
243+
queryWithAttachedRowId(
262244
list,
263245
Query.Eq(EntitiesTable.getPropertyColumn(property), value)
264246
)
265247
} else if (value == "") {
266-
query(list, null)
248+
queryWithAttachedRowId(list, null)
267249
} else {
268250
emptyList()
269251
}
@@ -274,7 +256,39 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
274256
return null
275257
}
276258

277-
return query(list, Query.Eq("i.$ROW_ID", (index + 1).toString())).firstOrNull()
259+
return queryWithAttachedRowId(list, Query.Eq("i.$ROW_ID", (index + 1).toString())).firstOrNull()
260+
}
261+
262+
private fun queryWithAttachedRowId(list: String, query: Query?): List<Entity.Saved> {
263+
return if (query == null) {
264+
databaseConnection.withConnection {
265+
readableDatabase
266+
.rawQuery(
267+
"""
268+
SELECT *, i.$ROW_ID
269+
FROM "$list" e, "${getRowIdTableName(list)}" i
270+
WHERE e._id = i._id
271+
ORDER BY i.$ROW_ID
272+
""".trimIndent(),
273+
null
274+
)
275+
}
276+
} else {
277+
databaseConnection.withConnection {
278+
readableDatabase
279+
.rawQuery(
280+
"""
281+
SELECT *, i.$ROW_ID
282+
FROM "$list" e, "${getRowIdTableName(list)}" i
283+
WHERE e._id = i._id AND ${query.selection}
284+
ORDER BY i.$ROW_ID
285+
""".trimIndent(),
286+
query.selectionArgs
287+
)
288+
}
289+
}.foldAndClose {
290+
mapCursorRowToEntity(it, it.getInt(ROW_ID))
291+
}
278292
}
279293

280294
/**

db/src/main/java/org/odk/collect/db/sqlite/Query.kt

+15
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,19 @@ sealed class Query(
2323
"(${queryA.selection} OR ${queryB.selection})",
2424
queryA.selectionArgs + queryB.selectionArgs
2525
)
26+
27+
fun copyWithAdjustedColumns(columnMapper: (String) -> String): Query {
28+
return when (this) {
29+
is Eq -> Eq(columnMapper(column), value)
30+
is NotEq -> NotEq(columnMapper(column), value)
31+
is And -> And(
32+
queryA.copyWithAdjustedColumns(columnMapper),
33+
queryB.copyWithAdjustedColumns(columnMapper)
34+
)
35+
is Or -> Or(
36+
queryA.copyWithAdjustedColumns(columnMapper),
37+
queryB.copyWithAdjustedColumns(columnMapper)
38+
)
39+
}
40+
}
2641
}

entities/src/main/java/org/odk/collect/entities/javarosa/parse/EntityItemElement.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.odk.collect.entities.javarosa.parse
22

3-
internal object EntityItemElement {
3+
object EntityItemElement {
44
const val ID = "name"
55
const val LABEL = "label"
66
const val VERSION = "__version"

0 commit comments

Comments
 (0)