Skip to content

Commit

Permalink
Merge pull request #22 from ergon/bugfix/dope-217-use-parameters-insi…
Browse files Browse the repository at this point in the history
…de-arraytype

DOPE-217: added usage for the ignored parameters inside the Primitive type ArrayType
  • Loading branch information
jansigi authored Jun 7, 2024
2 parents 99475c1 + 7535a3a commit a0ba5b8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,47 @@ import ch.ergon.dope.validtype.StringType
import ch.ergon.dope.validtype.ValidType

class Primitive<T : ValidType> : TypeExpression<T> {
private val queryString: String
override fun toDopeQuery(): DopeQuery = DopeQuery(queryString = queryString, parameters = emptyMap())
private val dopeQuery: DopeQuery

override fun toDopeQuery(): DopeQuery = dopeQuery

constructor(value: Number) {
this.queryString = "$value"
this.dopeQuery = DopeQuery(queryString = "$value", parameters = emptyMap())
}

constructor(value: String) {
this.queryString = "\"$value\""
this.dopeQuery = DopeQuery(queryString = "\"$value\"", parameters = emptyMap())
}

constructor(value: Boolean) {
this.queryString = when (value) {
true -> TRUE.queryString
false -> FALSE.queryString
}
this.dopeQuery = DopeQuery(
queryString = when (value) {
true -> TRUE.toDopeQuery().queryString
false -> FALSE.toDopeQuery().queryString
},
parameters = emptyMap(),
)
}

constructor(collection: Collection<TypeExpression<out ValidType>>) {
this.queryString = collection.joinToString(separator = ", ", prefix = "[", postfix = "]") { it.toDopeQuery().queryString }
val dopeQueries = collection.map { it.toDopeQuery() }
this.dopeQuery = DopeQuery(
queryString = dopeQueries.joinToString(separator = ", ", prefix = "[", postfix = "]") { it.queryString },
parameters = dopeQueries.fold(emptyMap()) { parameters, dopeQueryElement ->
parameters + dopeQueryElement.parameters
},
)
}

private constructor(primitiveType: PrimitiveType) {
this.queryString = when (primitiveType) {
val queryString = when (primitiveType) {
PrimitiveType.NULL -> "NULL"
PrimitiveType.MISSING -> "MISSING"
PrimitiveType.TRUE -> "TRUE"
PrimitiveType.FALSE -> "FALSE"
}

this.dopeQuery = DopeQuery(queryString, emptyMap())
}

companion object {
Expand Down
16 changes: 14 additions & 2 deletions core/src/test/kotlin/ch/ergon/dope/ParameterizedTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import ch.ergon.dope.resolvable.expression.unaliased.type.logical.or
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isEqualTo
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isNotEqualTo
import ch.ergon.dope.resolvable.expression.unaliased.type.stringfunction.concat
import ch.ergon.dope.resolvable.expression.unaliased.type.toArrayType
import ch.ergon.dope.resolvable.expression.unaliased.type.toNumberType
import junit.framework.TestCase.assertEquals
import kotlin.test.BeforeTest
import org.junit.jupiter.api.BeforeEach
import kotlin.test.Test

class ParameterizedTest {
private lateinit var create: QueryBuilder

@BeforeTest
@BeforeEach
fun setup() {
create = QueryBuilder()
}
Expand Down Expand Up @@ -138,4 +139,15 @@ class ParameterizedTest {
assertEquals(value6, parameters["$3"])
assertEquals(6, parameters.size)
}

@Test
fun `should get correct value inside array parameters`() {
val parameterValue = 3
val parameter = parameterValue.asParameter()

val parameters = create.select(listOf(parameter).toArrayType()).build().parameters

assertEquals(parameterValue, parameters["$1"])
assertEquals(1, parameters.size)
}
}

0 comments on commit a0ba5b8

Please sign in to comment.