Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOPE-202: extended Bucket with Expression and added functionality for… #46

Merged
merged 11 commits into from
Sep 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package ch.ergon.dope.resolvable.clause.model
import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.clause.ISelectClause
import ch.ergon.dope.resolvable.expression.Expression
import ch.ergon.dope.resolvable.expression.SingleExpression
import ch.ergon.dope.resolvable.formatToQueryString
import ch.ergon.dope.resolvable.fromable.AliasedBucket

class SelectClause(private val expression: Expression, private vararg val expressions: Expression) : ISelectClause {

override fun toDopeQuery(): DopeQuery {
val expressionDopeQuery = expression.toDopeQuery()
val expressionsDopeQuery = expressions.map { it.toDopeQuery() }
val expressionDopeQuery = getExpressionDopeQuery(expression)
val expressionsDopeQuery = expressions.map { getExpressionDopeQuery(it) }
return DopeQuery(
queryString = formatToQueryString(
"SELECT",
Expand All @@ -23,7 +25,7 @@ class SelectClause(private val expression: Expression, private vararg val expres
}
}

class SelectRawClause(private val expression: Expression) : ISelectClause {
class SelectRawClause(private val expression: SingleExpression) : ISelectClause {

override fun toDopeQuery(): DopeQuery {
val expressionDopeQuery = expression.toDopeQuery()
Expand All @@ -36,8 +38,8 @@ class SelectRawClause(private val expression: Expression) : ISelectClause {

class SelectDistinctClause(private val expression: Expression, private vararg val expressions: Expression) : ISelectClause {
override fun toDopeQuery(): DopeQuery {
val expressionsDopeQuery = expressions.map { it.toDopeQuery() }
val expressionDopeQuery = expression.toDopeQuery()
val expressionDopeQuery = getExpressionDopeQuery(expression)
val expressionsDopeQuery = expressions.map { getExpressionDopeQuery(it) }
return DopeQuery(
queryString = formatToQueryString(
"SELECT DISTINCT",
Expand All @@ -50,3 +52,6 @@ class SelectDistinctClause(private val expression: Expression, private vararg va
)
}
}

private fun getExpressionDopeQuery(expression: Expression) =
jansigi marked this conversation as resolved.
Show resolved Hide resolved
if (expression is AliasedBucket) DopeQuery("`${expression.alias}`", emptyMap()) else expression.toDopeQuery()
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ch.ergon.dope.validtype.ValidType
class AliasedExpression<T : ValidType>(
private val unaliasedExpression: UnaliasedExpression<T>,
private val alias: String,
) : Expression {
) : SingleExpression {
override fun toDopeQuery(): DopeQuery {
val unaliasedExpressionDopeQuery = unaliasedExpression.toDopeQuery()
return DopeQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package ch.ergon.dope.resolvable.fromable

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.expression.AsteriskExpression
import ch.ergon.dope.resolvable.expression.Expression

sealed class Bucket(open val name: String) : Fromable {
sealed class Bucket(open val name: String) : Fromable, Expression {
override fun toDopeQuery() = DopeQuery("`$name`", emptyMap())
}

Expand Down
14 changes: 14 additions & 0 deletions core/src/test/kotlin/ch/ergon/dope/buildTest/QueryBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -787,4 +787,18 @@ class QueryBuilderTest {

assertEquals(expected, actual)
}

@Test
fun `should support select with buckets`() {
val expected = "SELECT `bucket1`, `alias` FROM `bucket2` AS `alias`"
val bucket1 = someBucket("bucket1")
val bucket2 = someBucket("bucket2").alias("alias")

val actual = create
.select(bucket1, bucket2)
.from(bucket2)
.build().queryString

assertEquals(expected, actual)
}
}
jansigi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.ergon.dope.resolvable.clause

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.helper.ParameterDependentTest
import ch.ergon.dope.helper.someBucket
import ch.ergon.dope.helper.someNumberField
import ch.ergon.dope.helper.someStringArrayField
import ch.ergon.dope.helper.someStringField
Expand All @@ -27,6 +28,32 @@ class SelectClauseTest : ParameterDependentTest {
assertEquals(expected, actual)
}

@Test
fun `should support select with bucket`() {
val expected = DopeQuery(
"SELECT `someBucket`",
emptyMap(),
)
val underTest = SelectClause(someBucket())

val actual = underTest.toDopeQuery()

assertEquals(expected, actual)
}

@Test
fun `should support select with aliased bucket`() {
val expected = DopeQuery(
"SELECT `alias`",
emptyMap(),
)
val underTest = SelectClause(someBucket().alias("alias"))

val actual = underTest.toDopeQuery()

assertEquals(expected, actual)
}

@Test
fun `should support select with positional parameter`() {
val parameterValue = "value"
Expand Down Expand Up @@ -67,6 +94,32 @@ class SelectClauseTest : ParameterDependentTest {
assertEquals(expected, actual)
}

@Test
fun `should support select distinct with bucket`() {
val expected = DopeQuery(
"SELECT DISTINCT `someBucket`",
emptyMap(),
)
val underTest = SelectDistinctClause(someBucket())

val actual = underTest.toDopeQuery()

assertEquals(expected, actual)
}

@Test
fun `should support select distinct with aliased bucket`() {
val expected = DopeQuery(
"SELECT DISTINCT `alias`",
emptyMap(),
)
val underTest = SelectDistinctClause(someBucket().alias("alias"))

val actual = underTest.toDopeQuery()

assertEquals(expected, actual)
}

@Test
fun `should support select distinct with positional parameter`() {
val parameterValue = "value"
Expand Down
Loading