Skip to content

Commit

Permalink
DOPE-179 added JoinType enum, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
martinagallati-ergon committed Jun 24, 2024
1 parent a72aaa2 commit e8878ae
Showing 1 changed file with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,57 @@ package ch.ergon.dope.resolvable.clause.model
import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.clause.ISelectFromClause
import ch.ergon.dope.resolvable.clause.ISelectJoinClause
import ch.ergon.dope.resolvable.clause.model.JoinType.INNER_JOIN
import ch.ergon.dope.resolvable.clause.model.JoinType.JOIN
import ch.ergon.dope.resolvable.clause.model.JoinType.LEFT_JOIN
import ch.ergon.dope.resolvable.clause.model.JoinType.RIGHT_JOIN
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.resolvable.fromable.Bucket
import ch.ergon.dope.validtype.BooleanType
import ch.ergon.dope.validtype.ValidType

private const val JOIN = "JOIN"
private const val LEFT_JOIN = "LEFT JOIN"
private const val INNER_JOIN = "INNER JOIN"
private enum class JoinType(val type: String) {
JOIN("JOIN"),
LEFT_JOIN("LEFT JOIN"),
INNER_JOIN("INNER JOIN"),
RIGHT_JOIN("RIGHT JOIN"),
}

sealed class SelectJoinClause : ISelectJoinClause {
private val dopeQuery: DopeQuery

constructor(joinType: String, bucket: Bucket, onCondition: TypeExpression<BooleanType>, parentClause: ISelectFromClause) {
constructor(joinType: JoinType, bucket: Bucket, onCondition: TypeExpression<BooleanType>, parentClause: ISelectFromClause) {
val parentDopeQuery = parentClause.toDopeQuery()
val bucketDopeQuery = bucket.toDopeQuery()
val onConditionDopeQuery = onCondition.toDopeQuery()
dopeQuery = DopeQuery(
queryString = "${parentDopeQuery.queryString} $joinType ${bucketDopeQuery.queryString} ON ${onConditionDopeQuery.queryString}",
queryString = "${parentDopeQuery.queryString} ${joinType.type} ${bucketDopeQuery.queryString} " +
"ON ${onConditionDopeQuery.queryString}",
parameters = parentDopeQuery.parameters + bucketDopeQuery.parameters + onConditionDopeQuery.parameters,
)
}

constructor(joinType: String, bucket: Bucket, key: Field<out ValidType>, parentClause: ISelectFromClause) {
constructor(joinType: JoinType, bucket: Bucket, onKeys: Field<out ValidType>, parentClause: ISelectFromClause) {
val parentDopeQuery = parentClause.toDopeQuery()
val bucketDopeQuery = bucket.toDopeQuery()
val keyDopeQuery = key.toDopeQuery()
val keyDopeQuery = onKeys.toDopeQuery()
dopeQuery = DopeQuery(
queryString = "${parentDopeQuery.queryString} $joinType ${bucketDopeQuery.queryString} ON KEYS ${keyDopeQuery.queryString}",
queryString = "${parentDopeQuery.queryString} ${joinType.type} ${bucketDopeQuery.queryString} " +
"ON KEYS ${keyDopeQuery.queryString}",
parameters = parentDopeQuery.parameters + bucketDopeQuery.parameters + keyDopeQuery.parameters,
)
}

constructor(joinType: String, bucket: Bucket, key: Field<out ValidType>, forBucket: Bucket, parentClause: ISelectFromClause) {
constructor(joinType: JoinType, bucket: Bucket, onKey: Field<out ValidType>, forBucket: Bucket, parentClause: ISelectFromClause) {
val parentDopeQuery = parentClause.toDopeQuery()
val bucketDopeQuery = bucket.toDopeQuery()
val keyDopeQuery = key.toDopeQuery()
val keyDopeQuery = onKey.toDopeQuery()
val forBucketDopeQuery = forBucket.toDopeQuery()
dopeQuery = DopeQuery(
queryString = "${parentDopeQuery.queryString} $joinType ${bucketDopeQuery.queryString} " +
queryString = "${parentDopeQuery.queryString} ${joinType.type} ${bucketDopeQuery.queryString} " +
"ON KEY ${keyDopeQuery.queryString} FOR ${forBucketDopeQuery.queryString}",
parameters = parentDopeQuery.parameters + bucketDopeQuery.parameters + keyDopeQuery.parameters,
parameters = parentDopeQuery.parameters + bucketDopeQuery.parameters + keyDopeQuery.parameters + forBucketDopeQuery.parameters,
)
}

Expand Down Expand Up @@ -85,4 +94,4 @@ class InnerJoinClause : SelectJoinClause {
}

class RightJoinClause(bucket: Bucket, onCondition: TypeExpression<BooleanType>, parentClause: ISelectFromClause) :
SelectJoinClause("RIGHT JOIN", bucket, onCondition, parentClause)
SelectJoinClause(RIGHT_JOIN, bucket, onCondition, parentClause)

0 comments on commit e8878ae

Please sign in to comment.