@@ -18,8 +18,12 @@ import org.odk.collect.db.sqlite.SQLiteDatabaseExt.doesColumnExist
18
18
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.getColumnNames
19
19
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.query
20
20
import org.odk.collect.db.sqlite.SynchronizedDatabaseConnection
21
+ import org.odk.collect.db.sqlite.toSql
22
+ import org.odk.collect.entities.javarosa.parse.EntitySchema
21
23
import org.odk.collect.entities.storage.EntitiesRepository
22
24
import org.odk.collect.entities.storage.Entity
25
+ import org.odk.collect.shared.Query
26
+ import org.odk.collect.shared.mapColumns
23
27
24
28
private object ListsTable {
25
29
const val TABLE_NAME = " lists"
@@ -155,12 +159,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
155
159
return emptyList()
156
160
}
157
161
158
- return queryWithAttachedRowId(list).foldAndClose {
159
- mapCursorRowToEntity(
160
- it,
161
- it.getInt(ROW_ID )
162
- )
163
- }
162
+ return queryWithAttachedRowId(list, null )
164
163
}
165
164
166
165
override fun getCount (list : String ): Int {
@@ -208,18 +207,27 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
208
207
updateRowIdTables()
209
208
}
210
209
210
+ override fun query (list : String , query : Query ): List <Entity .Saved > {
211
+ if (! listExists(list)) {
212
+ return emptyList()
213
+ }
214
+
215
+ return queryWithAttachedRowId(list, query.mapColumns { columnName ->
216
+ when (columnName) {
217
+ EntitySchema .ID -> EntitiesTable .COLUMN_ID
218
+ EntitySchema .LABEL -> EntitiesTable .COLUMN_LABEL
219
+ EntitySchema .VERSION -> EntitiesTable .COLUMN_VERSION
220
+ else -> EntitiesTable .getPropertyColumn(columnName)
221
+ }
222
+ })
223
+ }
224
+
211
225
override fun getById (list : String , id : String ): Entity .Saved ? {
212
226
if (! listExists(list)) {
213
227
return null
214
228
}
215
229
216
- return queryWithAttachedRowId(
217
- list,
218
- selectionColumn = EntitiesTable .COLUMN_ID ,
219
- selectionArg = id
220
- ).first {
221
- mapCursorRowToEntity(it, it.getInt(ROW_ID ))
222
- }
230
+ return queryWithAttachedRowId(list, Query .Eq (EntitiesTable .COLUMN_ID , id)).firstOrNull()
223
231
}
224
232
225
233
override fun getAllByProperty (
@@ -238,15 +246,10 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
238
246
return if (propertyExists) {
239
247
queryWithAttachedRowId(
240
248
list,
241
- selectionColumn = EntitiesTable .getPropertyColumn(property),
242
- selectionArg = value
243
- ).foldAndClose {
244
- mapCursorRowToEntity(it, it.getInt(ROW_ID ))
245
- }
249
+ Query .Eq (EntitiesTable .getPropertyColumn(property), value)
250
+ )
246
251
} else if (value == " " ) {
247
- queryWithAttachedRowId(list).foldAndClose {
248
- mapCursorRowToEntity(it, it.getInt(ROW_ID ))
249
- }
252
+ queryWithAttachedRowId(list, null )
250
253
} else {
251
254
emptyList()
252
255
}
@@ -257,51 +260,39 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
257
260
return null
258
261
}
259
262
260
- return databaseConnection.withConnection {
261
- readableDatabase
262
- .rawQuery(
263
- """
264
- SELECT *, i.$ROW_ID
265
- FROM "$list " e, "${getRowIdTableName(list)} " i
266
- WHERE e._id = i._id AND i.$ROW_ID = ?
267
- """ .trimIndent(),
268
- arrayOf((index + 1 ).toString())
269
- ).first {
270
- mapCursorRowToEntity(it, it.getInt(ROW_ID ))
271
- }
272
- }
263
+ return queryWithAttachedRowId(list, Query .Eq (" i.$ROW_ID " , (index + 1 ).toString())).firstOrNull()
273
264
}
274
265
275
- private fun queryWithAttachedRowId (list : String ): Cursor {
276
- return databaseConnection.withConnection {
277
- readableDatabase
278
- .rawQuery(
279
- """
280
- SELECT *, i. $ROW_ID
281
- FROM " $list " e, " ${getRowIdTableName(list)} " i
282
- WHERE e._id = i._id
283
- ORDER BY i. $ROW_ID
284
- """ .trimIndent(),
285
- null
286
- )
287
- }
288
- }
289
-
290
- private fun queryWithAttachedRowId (
291
- list : String ,
292
- selectionColumn : String ,
293
- selectionArg : String
294
- ): Cursor {
295
- return databaseConnection.withConnection {
296
- readableDatabase.rawQuery(
297
- """
298
- SELECT *, i.$ROW_ID
299
- FROM " $list " e, " ${getRowIdTableName(list)} " i
300
- WHERE e._id = i._id AND $selectionColumn = ?
301
- ORDER BY i. $ROW_ID
302
- """ .trimIndent(),
303
- arrayOf(selectionArg)
304
- )
266
+ private fun queryWithAttachedRowId (list : String , query : Query ? ): List < Entity . Saved > {
267
+ return if (query == null ) {
268
+ databaseConnection.withConnection {
269
+ readableDatabase
270
+ .rawQuery(
271
+ """
272
+ SELECT *, i. $ROW_ID
273
+ FROM " $list " e, " ${getRowIdTableName(list)} " i
274
+ WHERE e._id = i._id
275
+ ORDER BY i. $ROW_ID
276
+ """ .trimIndent(),
277
+ null
278
+ )
279
+ }
280
+ } else {
281
+ databaseConnection.withConnection {
282
+ val sqlQuery = query.toSql()
283
+ readableDatabase
284
+ .rawQuery(
285
+ """
286
+ SELECT *, i. $ROW_ID
287
+ FROM " $list " e, " ${getRowIdTableName(list)} " i
288
+ WHERE e._id = i._id AND ${sqlQuery.selection}
289
+ ORDER BY i.$ROW_ID
290
+ """ .trimIndent(),
291
+ sqlQuery.selectionArgs
292
+ )
293
+ }
294
+ }.foldAndClose {
295
+ mapCursorRowToEntity(it, it.getInt( ROW_ID ) )
305
296
}
306
297
}
307
298
0 commit comments