Skip to content

Commit b1bdd09

Browse files
committed
Handle diffrences in column naming
1 parent f69f4f7 commit b1bdd09

File tree

4 files changed

+67
-24
lines changed

4 files changed

+67
-24
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private object ListsTable {
2828
const val COLUMN_HASH = "hash"
2929
}
3030

31-
private object EntitiesTable {
31+
object EntitiesTable {
3232
const val COLUMN_ID = "id"
3333
const val COLUMN_LABEL = "label"
3434
const val COLUMN_VERSION = "version"
@@ -220,15 +220,17 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
220220
}
221221
} else {
222222
databaseConnection.withConnection {
223+
val querySelectionAndArgs = query.toSqlite()
224+
223225
readableDatabase
224226
.rawQuery(
225227
"""
226228
SELECT *, i.$ROW_ID
227229
FROM "$list" e, "${getRowIdTableName(list)}" i
228-
WHERE e._id = i._id AND ${query.selection}
230+
WHERE e._id = i._id AND ${querySelectionAndArgs.selection}
229231
ORDER BY i.$ROW_ID
230232
""".trimIndent(),
231-
query.selectionArgs
233+
querySelectionAndArgs.selectionArgs
232234
)
233235
}
234236
}.foldAndClose {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.odk.collect.android.database.entities
2+
3+
import org.odk.collect.android.database.entities.EntitiesTable.getPropertyColumn
4+
import org.odk.collect.db.sqlite.Query
5+
import org.odk.collect.db.sqlite.QuerySelectionAndArgs
6+
import org.odk.collect.entities.javarosa.parse.EntityItemElement
7+
8+
fun Query.toSqlite(): QuerySelectionAndArgs {
9+
return when (this) {
10+
is Query.Eq -> {
11+
val columnName = getColumnName(column)
12+
13+
QuerySelectionAndArgs("$columnName = ?", arrayOf(value))
14+
}
15+
16+
is Query.NotEq -> {
17+
val columnName = getColumnName(column)
18+
19+
QuerySelectionAndArgs("$columnName != ?", arrayOf(value))
20+
}
21+
22+
is Query.And -> {
23+
val queryAResult = queryA.toSqlite()
24+
val queryBResult = queryB.toSqlite()
25+
26+
QuerySelectionAndArgs(
27+
"(${queryAResult.selection} AND ${queryBResult.selection})",
28+
queryAResult.selectionArgs + queryBResult.selectionArgs
29+
)
30+
}
31+
32+
is Query.Or -> {
33+
val queryAResult = queryA.toSqlite()
34+
val queryBResult = queryB.toSqlite()
35+
36+
QuerySelectionAndArgs(
37+
"(${queryAResult.selection} OR ${queryBResult.selection})",
38+
queryAResult.selectionArgs + queryBResult.selectionArgs
39+
)
40+
}
41+
}
42+
}
43+
44+
private fun getColumnName(column: String): String {
45+
return when (column) {
46+
EntityItemElement.ID -> EntitiesTable.COLUMN_ID
47+
EntityItemElement.LABEL -> EntitiesTable.COLUMN_LABEL
48+
EntityItemElement.VERSION -> EntitiesTable.COLUMN_VERSION
49+
else -> getPropertyColumn(column)
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
package org.odk.collect.db.sqlite
22

3-
sealed class Query(
4-
val selection: String,
5-
val selectionArgs: Array<String>
6-
) {
7-
class Eq(val column: String, val value: String) : Query(
8-
"$column = ?",
9-
arrayOf(value)
10-
)
3+
sealed interface Query {
4+
class Eq(val column: String, val value: String) : Query
115

12-
class NotEq(val column: String, val value: String) : Query(
13-
"$column != ?",
14-
arrayOf(value)
15-
)
6+
class NotEq(val column: String, val value: String) : Query
167

17-
class And(val queryA: Query, val queryB: Query) : Query(
18-
"(${queryA.selection} AND ${queryB.selection})",
19-
queryA.selectionArgs + queryB.selectionArgs
20-
)
8+
class And(val queryA: Query, val queryB: Query) : Query
219

22-
class Or(val queryA: Query, val queryB: Query) : Query(
23-
"(${queryA.selection} OR ${queryB.selection})",
24-
queryA.selectionArgs + queryB.selectionArgs
25-
)
10+
class Or(val queryA: Query, val queryB: Query) : Query
2611
}
12+
13+
data class QuerySelectionAndArgs(
14+
val selection: String,
15+
val selectionArgs: Array<String>
16+
)

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)