-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple query structure from SQL translation logic
- Loading branch information
1 parent
8e9c25f
commit 3766a57
Showing
13 changed files
with
80 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.odk.collect.db.sqlite | ||
|
||
import org.odk.collect.shared.Query | ||
|
||
data class SqlQuery( | ||
val selection: String, | ||
val selectionArgs: Array<String> | ||
) | ||
|
||
fun Query.toSql(): SqlQuery { | ||
return when (this) { | ||
is Query.Eq -> SqlQuery("$column = ?", arrayOf(value)) | ||
is Query.NotEq -> SqlQuery("$column != ?", arrayOf(value)) | ||
is Query.And -> { | ||
val sqlA = queryA.toSql() | ||
val sqlB = queryB.toSql() | ||
SqlQuery( | ||
"(${sqlA.selection} AND ${sqlB.selection})", | ||
sqlA.selectionArgs + sqlB.selectionArgs | ||
) | ||
} | ||
is Query.Or -> { | ||
val sqlA = queryA.toSql() | ||
val sqlB = queryB.toSql() | ||
SqlQuery( | ||
"(${sqlA.selection} OR ${sqlB.selection})", | ||
sqlA.selectionArgs + sqlB.selectionArgs | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
entities/src/main/java/org/odk/collect/entities/storage/EntitiesRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
entities/src/main/java/org/odk/collect/entities/storage/InMemEntitiesRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.odk.collect.shared | ||
|
||
import org.odk.collect.shared.Query.And | ||
import org.odk.collect.shared.Query.Eq | ||
import org.odk.collect.shared.Query.NotEq | ||
import org.odk.collect.shared.Query.Or | ||
|
||
sealed class Query { | ||
class Eq(val column: String, val value: String) : Query() | ||
class NotEq(val column: String, val value: String) : Query() | ||
class And(val queryA: Query, val queryB: Query) : Query() | ||
class Or(val queryA: Query, val queryB: Query) : Query() | ||
} | ||
|
||
fun Query.mapColumns(columnMapper: (String) -> String): Query { | ||
return when (this) { | ||
is Eq -> Eq(columnMapper(column), value) | ||
is NotEq -> NotEq(columnMapper(column), value) | ||
is And -> And( | ||
queryA.mapColumns(columnMapper), | ||
queryB.mapColumns(columnMapper) | ||
) | ||
is Or -> Or( | ||
queryA.mapColumns(columnMapper), | ||
queryB.mapColumns(columnMapper) | ||
) | ||
} | ||
} |