mirror of https://github.com/tasks/tasks
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.6 KiB
Kotlin
44 lines
1.6 KiB
Kotlin
package com.todoroo.andlib.sql
|
|
|
|
open class UnaryCriterion private constructor(private val expression: Field, operator: Operator, private val value: Any?) : Criterion(operator) {
|
|
override fun populate() = "$expression${populateOperator()}${afterPopulateOperator()}"
|
|
|
|
open fun populateOperator() = "$operator"
|
|
|
|
private fun afterPopulateOperator(): Any = if (value is String) {
|
|
"'${sanitize(value)}'"
|
|
} else {
|
|
value ?: ""
|
|
}
|
|
|
|
companion object {
|
|
fun eq(expression: Field, value: Any?): Criterion = UnaryCriterion(expression, Operator.eq, value)
|
|
|
|
/** Sanitize the given input for SQL */
|
|
fun sanitize(input: String): String = input.replace("'", "''")
|
|
|
|
fun gt(field: Field, value: Any?): Criterion = UnaryCriterion(field, Operator.gt, value)
|
|
|
|
fun lt(field: Field, value: Any?): Criterion = UnaryCriterion(field, Operator.lt, value)
|
|
|
|
fun lte(field: Field, value: Any?): Criterion = UnaryCriterion(field, Operator.lte, value)
|
|
|
|
fun isNotNull(field: Field): Criterion {
|
|
return object : UnaryCriterion(field, Operator.isNotNull, null) {
|
|
override fun populateOperator() = " $operator"
|
|
}
|
|
}
|
|
|
|
fun isNull(field: Field): Criterion {
|
|
return object : UnaryCriterion(field, Operator.isNull, null) {
|
|
override fun populateOperator() = " $operator"
|
|
}
|
|
}
|
|
|
|
fun like(field: Field, value: String?): Criterion {
|
|
return object : UnaryCriterion(field, Operator.like, value) {
|
|
override fun populateOperator() = " $operator "
|
|
}
|
|
}
|
|
}
|
|
} |