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-196: added all function on bucket and tests #8

Merged
merged 11 commits into from
Apr 29, 2024
5 changes: 3 additions & 2 deletions core/src/main/kotlin/ch/ergon/dope/DSLContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import ch.ergon.dope.resolvable.clause.select.factory.From
import ch.ergon.dope.resolvable.clause.select.factory.Select
import ch.ergon.dope.resolvable.clause.select.factory.Where
import ch.ergon.dope.resolvable.expression.Expression
import ch.ergon.dope.resolvable.expression.SingleExpression

class DSLContext {
private val select = Select()

fun select(expression: Expression, vararg expressions: Expression): From = select.select(expression, *expressions)

fun selectAll(): From = select.selectAll()
fun selectAsterisk(): From = select.selectAsterisk()

fun selectDistinct(expression: Expression, vararg expressions: Expression): From = select.selectDistinct(expression, *expressions)

fun selectRaw(expression: Expression): From = select.selectRaw(expression)
fun selectRaw(expression: SingleExpression): From = select.selectRaw(expression)

fun selectFrom(fromable: Fromable): Where = select.selectFrom(fromable)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import ch.ergon.dope.resolvable.clause.select.factory.Offset
import ch.ergon.dope.resolvable.clause.select.factory.OrderBy
import ch.ergon.dope.resolvable.clause.select.factory.OrderByType
import ch.ergon.dope.resolvable.clause.select.factory.Where
import ch.ergon.dope.resolvable.expression.ASTERISK
import ch.ergon.dope.resolvable.expression.Expression
import ch.ergon.dope.resolvable.expression.SingleExpression
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.validtype.BooleanType
Expand All @@ -30,14 +32,14 @@ import ch.ergon.dope.validtype.ValidType
class ClauseBuilder(private val clauses: List<Clause>) {
fun select(expression: Expression, vararg expressions: Expression): From = From(addClause(SelectClause(expression, *expressions)))

fun selectAll(): From = From(addClause(SelectClause(AsterisksExpression())))
fun selectAsterisk(): From = From(addClause(SelectClause(ASTERISK)))

fun selectDistinct(expression: Expression, vararg expressions: Expression): From =
From(addClause(SelectDistinctClause(expression, *expressions)))

fun selectRaw(expression: Expression): From = From(addClause(SelectRawClause(expression)))
fun selectRaw(expression: SingleExpression): From = From(addClause(SelectRawClause(expression)))

fun selectFrom(fromable: Fromable): Where = ClauseBuilder(addClause(SelectClause(AsterisksExpression()))).from(fromable)
fun selectFrom(fromable: Fromable): Where = ClauseBuilder(addClause(SelectClause(ASTERISK))).from(fromable)

fun from(fromable: Fromable): Where = Where(addClause(FromClause(fromable)))

Expand All @@ -61,7 +63,3 @@ class ClauseBuilder(private val clauses: List<Clause>) {
return clauses + clause
}
}

private class AsterisksExpression : Expression {
override fun toQueryString(): String = "*"
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ch.ergon.dope.resolvable.clause.select

import ch.ergon.dope.resolvable.clause.Clause
import ch.ergon.dope.resolvable.expression.Expression
import ch.ergon.dope.resolvable.expression.SingleExpression
import ch.ergon.dope.resolvable.formatToQueryString

class SelectRawClause(private val expression: Expression) : Clause {
class SelectRawClause(private val expression: SingleExpression) : Clause {
override fun toQueryString(): String = formatToQueryString("SELECT RAW", expression)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import ch.ergon.dope.resolvable.clause.Clause
import ch.ergon.dope.resolvable.clause.ClauseBuilder
import ch.ergon.dope.resolvable.clause.select.Fromable
import ch.ergon.dope.resolvable.expression.Expression
import ch.ergon.dope.resolvable.expression.SingleExpression

class Select(clauses: List<Clause> = emptyList()) : From(clauses) {
private val clauseBuilder: ClauseBuilder =
ClauseBuilder(clauses)

fun select(expression: Expression, vararg expressions: Expression): From = clauseBuilder.select(expression, *expressions)

fun selectAll(): From = clauseBuilder.selectAll()
fun selectAsterisk(): From = clauseBuilder.selectAsterisk()

fun selectDistinct(expression: Expression, vararg expressions: Expression): From = clauseBuilder.selectDistinct(expression, *expressions)

fun selectRaw(expression: Expression): From = clauseBuilder.selectRaw(expression)
fun selectRaw(expression: SingleExpression): From = clauseBuilder.selectRaw(expression)

fun selectFrom(fromable: Fromable): Where = clauseBuilder.selectFrom(fromable)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ch.ergon.dope.resolvable.expression

import ch.ergon.dope.resolvable.fromable.AliasedBucket
import ch.ergon.dope.resolvable.fromable.Bucket
import ch.ergon.dope.resolvable.fromable.UnaliasedBucket

val ASTERISK = AsteriskExpression()
jansigi marked this conversation as resolved.
Show resolved Hide resolved

class AsteriskExpression : Expression {
private val asterisk = "*"
private val querySting: String
jansigi marked this conversation as resolved.
Show resolved Hide resolved

constructor(bucket: Bucket) {
querySting = when (bucket) {
is AliasedBucket -> bucket.alias
is UnaliasedBucket -> bucket.name
} + ".$asterisk"
}

constructor() {
querySting = asterisk
}

override fun toQueryString(): String = querySting
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ch.ergon.dope.validtype.ValidType

interface Expression : Resolvable

interface UnaliasedExpression<T : ValidType> : Expression
interface SingleExpression : Expression

interface UnaliasedExpression<T : ValidType> : SingleExpression

interface TypeExpression<T : ValidType> : UnaliasedExpression<T>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.ergon.dope.resolvable.expression.unaliased.aggregator

import ch.ergon.dope.resolvable.expression.ASTERISK
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.resolvable.formatToQueryStringWithBrackets
import ch.ergon.dope.validtype.StringType
Expand All @@ -13,4 +14,4 @@ class CountExpression(

fun count(field: Field<out ValidType>) = CountExpression(field)

fun countAll() = CountExpression(Field<StringType>("*", ""))
fun countAsterisk() = CountExpression(Field<StringType>(ASTERISK.toQueryString(), ""))
jansigi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ch.ergon.dope.resolvable.fromable

import ch.ergon.dope.resolvable.clause.select.Fromable
import ch.ergon.dope.resolvable.expression.AsteriskExpression

abstract class Bucket(open val name: String) : Fromable {
sealed class Bucket(open val name: String) : Fromable {
jansigi marked this conversation as resolved.
Show resolved Hide resolved
override fun toQueryString(): String = name
}

Expand All @@ -13,3 +14,5 @@ open class UnaliasedBucket(name: String) : Bucket(name) {
class AliasedBucket(name: String, val alias: String) : Bucket(name) {
override fun toQueryString(): String = "$name AS $alias"
}

fun Bucket.asterisk() = AsteriskExpression(this)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.validtype.BooleanType
import ch.ergon.dope.validtype.ValidType

abstract class JoinOperator : Fromable {
sealed class JoinOperator : Fromable {
private val queryString: String

constructor(fromBucket: Bucket, joinType: String, goalBucket: Bucket, onCondition: TypeExpression<BooleanType>) {
Expand Down
Loading