Skip to content

Commit

Permalink
Merge pull request #21 from ergon/feature/dope-211-array-index-access
Browse files Browse the repository at this point in the history
DOPE-211 added array index accessing
  • Loading branch information
martinagallati-ergon authored Jun 6, 2024
2 parents d25b94a + 6e30b4e commit 99475c1
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.access

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.ValidType

class ArrayAccess<T : ValidType>(
private val array: TypeExpression<ArrayType<T>>,
private val index: TypeExpression<NumberType>,
) : TypeExpression<T> {
override fun toDopeQuery(): DopeQuery {
val arrayDopeQuery = array.toDopeQuery()
val indexDopeQuery = index.toDopeQuery()
return DopeQuery(
queryString = "${arrayDopeQuery.queryString}[${indexDopeQuery.queryString}]",
parameters = arrayDopeQuery.parameters + indexDopeQuery.parameters,
)
}
}

fun <T : ValidType> TypeExpression<ArrayType<T>>.get(index: TypeExpression<NumberType>) = ArrayAccess(this, index)
103 changes: 103 additions & 0 deletions core/src/test/kotlin/ch/ergon/dope/ArrayTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package ch.ergon.dope

import ch.ergon.dope.helper.someBooleanField
import ch.ergon.dope.helper.someBucket
import ch.ergon.dope.helper.someNumberArrayField
import ch.ergon.dope.helper.someNumberField
import ch.ergon.dope.helper.someStringArrayField
import ch.ergon.dope.helper.someStringField
import ch.ergon.dope.resolvable.expression.alias
import ch.ergon.dope.resolvable.expression.unaliased.type.Primitive.Companion.FALSE
import ch.ergon.dope.resolvable.expression.unaliased.type.Primitive.Companion.TRUE
import ch.ergon.dope.resolvable.expression.unaliased.type.access.get
import ch.ergon.dope.resolvable.expression.unaliased.type.arithmetic.add
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.inArray
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isEqualTo
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.toBooleanType
Expand Down Expand Up @@ -301,4 +306,102 @@ class ArrayTest {

assertEquals(expected, actual)
}

@Test
fun `should support index based array accessing`() {
val expected = "SELECT `numberArrayField`[0] FROM `someBucket`"

val actual: String = create
.select(
someNumberArrayField().get(0.toNumberType()),
).from(
someBucket(),
).build().queryString

assertEquals(expected, actual)
}

@Test
fun `should support index based array accessing with addition`() {
val expected = "SELECT `numberArrayField`[(1 + 1)] FROM `someBucket`"

val actual: String = create
.select(
someNumberArrayField().get(1.toNumberType().add(1)),
).from(
someBucket(),
).build().queryString

assertEquals(expected, actual)
}

@Test
fun `should support index based array accessing with negative numbers`() {
val expected = "SELECT `numberArrayField`[-1] FROM `someBucket`"

val actual: String = create
.select(
someNumberArrayField().get((-1).toNumberType()),
).from(
someBucket(),
).build().queryString

assertEquals(expected, actual)
}

@Test
fun `should support index based array accessing with nested arrays`() {
val expected = "SELECT `stringArrayField`[`numberArrayField`[0]] FROM `someBucket`"

val actual: String = create
.select(
someStringArrayField().get(someNumberArrayField().get(0.toNumberType())),
).from(
someBucket(),
).build().queryString

assertEquals(expected, actual)
}

@Test
fun `should support index based array accessing in where clause`() {
val expected = "SELECT * FROM `someBucket` WHERE `numberArrayField`[0] = 1"

val actual: String = create
.selectAsterisk().from(
someBucket(),
).where(
someNumberArrayField().get(0.toNumberType()).isEqualTo(1),
).build().queryString

assertEquals(expected, actual)
}

@Test
fun `should support index based array accessing in offset clause`() {
val expected = "SELECT * FROM `someBucket` OFFSET `numberArrayField`[0]"

val actual: String = create
.selectAsterisk().from(
someBucket(),
).offset(
someNumberArrayField().get(0.toNumberType()),
).build().queryString

assertEquals(expected, actual)
}

@Test
fun `should support index based array accessing in limit clause`() {
val expected = "SELECT * FROM `someBucket` LIMIT `numberArrayField`[0]"

val actual: String = create
.selectAsterisk().from(
someBucket(),
).limit(
someNumberArrayField().get(0.toNumberType()),
).build().queryString

assertEquals(expected, actual)
}
}
2 changes: 0 additions & 2 deletions core/src/test/kotlin/ch/ergon/dope/NumberFunctionsTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.ergon.dope

import ch.ergon.dope.helper.someBucket
import ch.ergon.dope.resolvable.expression.alias
import ch.ergon.dope.resolvable.expression.unaliased.type.toNumberType
import junit.framework.TestCase.assertEquals
Expand All @@ -10,7 +9,6 @@ import kotlin.test.Test
class NumberFunctionsTest {
private lateinit var builder: StringBuilder
private lateinit var create: QueryBuilder
private val person = someBucket("person")

@BeforeTest
fun setup() {
Expand Down

0 comments on commit 99475c1

Please sign in to comment.