Skip to content

Commit

Permalink
DOPE-228: added Between and NotBetween comparison and extension funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
martinagallati-ergon committed Jul 4, 2024
1 parent 43715c4 commit 60e228b
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.relational

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.BooleanType
import ch.ergon.dope.validtype.ComparableType

class BetweenExpression<T : ComparableType>(
private val expression: TypeExpression<T>,
private val start: TypeExpression<T>,
private val end: TypeExpression<T>,
) : TypeExpression<BooleanType> {
override fun toDopeQuery(): DopeQuery {
val expressionDopeQuery = expression.toDopeQuery()
val startDopeQuery = start.toDopeQuery()
val endDopeQuery = end.toDopeQuery()
return DopeQuery(
queryString = "${expressionDopeQuery.queryString} BETWEEN ${startDopeQuery.queryString} AND ${endDopeQuery.queryString}",
parameters = expressionDopeQuery.parameters + startDopeQuery.parameters + endDopeQuery.parameters,
)
}
}

fun <T : ComparableType> TypeExpression<T>.between(start: TypeExpression<T>, end: TypeExpression<T>) =
BetweenExpression(this, start, end)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.relational

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.BooleanType
import ch.ergon.dope.validtype.ComparableType

class NotBetweenExpression<T : ComparableType>(
private val expression: TypeExpression<T>,
private val start: TypeExpression<T>,
private val end: TypeExpression<T>,
) : TypeExpression<BooleanType> {
override fun toDopeQuery(): DopeQuery {
val expressionDopeQuery = expression.toDopeQuery()
val startDopeQuery = start.toDopeQuery()
val endDopeQuery = end.toDopeQuery()
return DopeQuery(
queryString = "${expressionDopeQuery.queryString} NOT BETWEEN ${startDopeQuery.queryString} AND ${endDopeQuery.queryString}",
parameters = expressionDopeQuery.parameters + startDopeQuery.parameters + endDopeQuery.parameters,
)
}
}

fun <T : ComparableType> TypeExpression<T>.notBetween(start: TypeExpression<T>, end: TypeExpression<T>) =
NotBetweenExpression(this, start, end)
19 changes: 19 additions & 0 deletions core/src/test/kotlin/ch/ergon/dope/BooleanComparatorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isNotNull
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isNotValued
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isNull
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isValued
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.notBetween
import ch.ergon.dope.resolvable.expression.unaliased.type.toDopeType
import org.junit.jupiter.api.Assertions.assertEquals
import kotlin.test.BeforeTest
Expand Down Expand Up @@ -918,4 +919,22 @@ class BooleanComparatorTest {

assertEquals(expected, actual)
}

@Test
fun `should support BETWEEN comparison`() {
val expected = "`numberField` BETWEEN 1 AND 10"

val actual = someNumberField().notBetween(1.toDopeType(), 10.toDopeType()).toDopeQuery().queryString

assertEquals(expected, actual)
}

@Test
fun `should support NOT BETWEEN comparison`() {
val expected = "`numberField` NOT BETWEEN 1 AND 10"

val actual = someNumberField().notBetween(1.toDopeType(), 10.toDopeType()).toDopeQuery().queryString

assertEquals(expected, actual)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ch.ergon.dope.extension.type.relational

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.BetweenExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.between
import ch.ergon.dope.toDopeType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.StringType
import com.schwarz.crystalapi.schema.CMField

@JvmName("betweenNumber")
fun CMField<out Number>.between(start: CMField<out Number>, end: CMField<out Number>): BetweenExpression<NumberType> =
toDopeType().between(start.toDopeType(), end.toDopeType())

@JvmName("betweenNumber")
fun CMField<out Number>.between(start: CMField<out Number>, end: TypeExpression<NumberType>): BetweenExpression<NumberType> =
toDopeType().between(start.toDopeType(), end)

@JvmName("betweenNumber")
fun CMField<out Number>.between(start: TypeExpression<NumberType>, end: CMField<out Number>): BetweenExpression<NumberType> =
toDopeType().between(start, end.toDopeType())

@JvmName("betweenNumber")
fun CMField<out Number>.between(start: TypeExpression<NumberType>, end: TypeExpression<NumberType>): BetweenExpression<NumberType> =
toDopeType().between(start, end)

@JvmName("betweenNumber")
fun TypeExpression<NumberType>.between(start: CMField<out Number>, end: CMField<out Number>): BetweenExpression<NumberType> =
this.between(start.toDopeType(), end.toDopeType())

@JvmName("betweenNumber")
fun TypeExpression<NumberType>.between(start: CMField<out Number>, end: TypeExpression<NumberType>): BetweenExpression<NumberType> =
this.between(start.toDopeType(), end)

@JvmName("betweenNumber")
fun TypeExpression<NumberType>.between(start: TypeExpression<NumberType>, end: CMField<out Number>): BetweenExpression<NumberType> =
this.between(start, end.toDopeType())

@JvmName("betweenString")
fun CMField<String>.between(start: CMField<String>, end: CMField<String>): BetweenExpression<StringType> =
toDopeType().between(start.toDopeType(), end.toDopeType())

@JvmName("betweenString")
fun CMField<String>.between(start: CMField<String>, end: TypeExpression<StringType>): BetweenExpression<StringType> =
toDopeType().between(start.toDopeType(), end)

@JvmName("betweenString")
fun CMField<String>.between(start: TypeExpression<StringType>, end: CMField<String>): BetweenExpression<StringType> =
toDopeType().between(start, end.toDopeType())

@JvmName("betweenString")
fun CMField<String>.between(start: TypeExpression<StringType>, end: TypeExpression<StringType>): BetweenExpression<StringType> =
toDopeType().between(start, end)

@JvmName("betweenString")
fun TypeExpression<StringType>.between(start: CMField<String>, end: CMField<String>): BetweenExpression<StringType> =
this.between(start.toDopeType(), end.toDopeType())

@JvmName("betweenString")
fun TypeExpression<StringType>.between(start: CMField<String>, end: TypeExpression<StringType>): BetweenExpression<StringType> =
this.between(start.toDopeType(), end)

@JvmName("betweenString")
fun TypeExpression<StringType>.between(start: TypeExpression<StringType>, end: CMField<String>): BetweenExpression<StringType> =
this.between(start, end.toDopeType())
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ch.ergon.dope.extension.type.relational

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.NotBetweenExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.notBetween
import ch.ergon.dope.toDopeType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.StringType
import com.schwarz.crystalapi.schema.CMField

@JvmName("betweenNumber")
fun CMField<out Number>.notBetween(start: CMField<out Number>, end: CMField<out Number>): NotBetweenExpression<NumberType> =
toDopeType().notBetween(start.toDopeType(), end.toDopeType())

@JvmName("betweenNumber")
fun CMField<out Number>.notBetween(start: CMField<out Number>, end: TypeExpression<NumberType>): NotBetweenExpression<NumberType> =
toDopeType().notBetween(start.toDopeType(), end)

@JvmName("betweenNumber")
fun CMField<out Number>.notBetween(start: TypeExpression<NumberType>, end: CMField<out Number>): NotBetweenExpression<NumberType> =
toDopeType().notBetween(start, end.toDopeType())

@JvmName("betweenNumber")
fun CMField<out Number>.notBetween(start: TypeExpression<NumberType>, end: TypeExpression<NumberType>): NotBetweenExpression<NumberType> =
toDopeType().notBetween(start, end)

@JvmName("betweenNumber")
fun TypeExpression<NumberType>.notBetween(start: CMField<out Number>, end: CMField<out Number>): NotBetweenExpression<NumberType> =
this.notBetween(start.toDopeType(), end.toDopeType())

@JvmName("betweenNumber")
fun TypeExpression<NumberType>.notBetween(start: CMField<out Number>, end: TypeExpression<NumberType>): NotBetweenExpression<NumberType> =
this.notBetween(start.toDopeType(), end)

@JvmName("betweenNumber")
fun TypeExpression<NumberType>.notBetween(start: TypeExpression<NumberType>, end: CMField<out Number>): NotBetweenExpression<NumberType> =
this.notBetween(start, end.toDopeType())

@JvmName("betweenString")
fun CMField<String>.notBetween(start: CMField<String>, end: CMField<String>): NotBetweenExpression<StringType> =
toDopeType().notBetween(start.toDopeType(), end.toDopeType())

@JvmName("betweenString")
fun CMField<String>.notBetween(start: CMField<String>, end: TypeExpression<StringType>): NotBetweenExpression<StringType> =
toDopeType().notBetween(start.toDopeType(), end)

@JvmName("betweenString")
fun CMField<String>.notBetween(start: TypeExpression<StringType>, end: CMField<String>): NotBetweenExpression<StringType> =
toDopeType().notBetween(start, end.toDopeType())

@JvmName("betweenString")
fun CMField<String>.notBetween(start: TypeExpression<StringType>, end: TypeExpression<StringType>): NotBetweenExpression<StringType> =
toDopeType().notBetween(start, end)

@JvmName("betweenString")
fun TypeExpression<StringType>.notBetween(start: CMField<String>, end: CMField<String>): NotBetweenExpression<StringType> =
this.notBetween(start.toDopeType(), end.toDopeType())

@JvmName("betweenString")
fun TypeExpression<StringType>.notBetween(start: CMField<String>, end: TypeExpression<StringType>): NotBetweenExpression<StringType> =
this.notBetween(start.toDopeType(), end)

@JvmName("betweenString")
fun TypeExpression<StringType>.notBetween(start: TypeExpression<StringType>, end: CMField<String>): NotBetweenExpression<StringType> =
this.notBetween(start, end.toDopeType())

0 comments on commit 60e228b

Please sign in to comment.