Skip to content

Commit 70e8dd2

Browse files
committed
DOPE-281: refactor Bucket and related classes to introduce Definable interface for improved bucket definition handling
1 parent 1047a1b commit 70e8dd2

23 files changed

Lines changed: 271 additions & 230 deletions

File tree

core/src/main/kotlin/ch/ergon/dope/resolvable/bucket/Bucket.kt

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ import ch.ergon.dope.resolvable.Updatable
99
import ch.ergon.dope.resolvable.expression.SingleExpression
1010
import ch.ergon.dope.validtype.ObjectType
1111

12-
sealed interface Bucket : Fromable, Joinable, Nestable, Deletable, Updatable, SingleExpression<ObjectType> {
12+
interface Bucket : Fromable, Joinable, Nestable, Deletable, Updatable, SingleExpression<ObjectType> {
1313
val name: String
1414
val scope: BucketScope?
1515
}
1616

17+
interface Definable : Bucket {
18+
val alias: String
19+
20+
fun asBucketDefinition(): AliasedBucketDefinition = AliasedBucketDefinition(name, alias, scope)
21+
}
22+
1723
data class BucketScope(val name: String, val collection: ScopeCollection? = null) {
18-
fun withCollection(collectionName: String) = copy(collection = ScopeCollection(collectionName))
1924

25+
fun withCollection(collectionName: String) = copy(collection = ScopeCollection(collectionName))
2026
fun withCollection(collection: ScopeCollection) = copy(collection = collection)
2127
}
2228

@@ -38,21 +44,12 @@ data class UnaliasedBucket(
3844

3945
data class AliasedBucket(
4046
override val name: String,
41-
val alias: String,
47+
override val alias: String,
4248
override val scope: BucketScope? = null,
43-
) : Bucket {
44-
fun asBucketDefinition(): AliasedBucketDefinition =
45-
AliasedBucketDefinition(
46-
bucketName = name,
47-
scopeName = scope?.name,
48-
collectionName = scope?.collection?.name,
49-
alias = alias,
50-
)
51-
}
49+
) : Definable
5250

5351
data class AliasedBucketDefinition(
5452
val bucketName: String,
55-
val scopeName: String? = null,
56-
val collectionName: String? = null,
5753
val alias: String,
54+
val scope: BucketScope? = null,
5855
) : Resolvable

core/src/main/kotlin/ch/ergon/dope/resolvable/keyspace/Keyspace.kt

Whitespace-only changes.

couchbase/src/main/kotlin/ch/ergon/dope/couchbase/resolvable/keyspace/SystemKeyspaces.kt

Lines changed: 162 additions & 157 deletions
Large diffs are not rendered by default.

couchbase/src/main/kotlin/ch/ergon/dope/couchbase/resolver/KeySpaceResolver.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@ import ch.ergon.dope.couchbase.resolver.expression.queryString
55
import ch.ergon.dope.couchbase.util.formatBucket
66
import ch.ergon.dope.couchbase.util.formatListToQueryStringWithBrackets
77
import ch.ergon.dope.couchbase.util.formatToQueryStringWithSymbol
8-
import ch.ergon.dope.resolvable.bucket.AliasedBucket
98
import ch.ergon.dope.resolvable.bucket.AliasedBucketDefinition
9+
import ch.ergon.dope.resolvable.bucket.Definable
1010
import ch.ergon.dope.resolvable.bucket.IndexReference
11+
import ch.ergon.dope.resolvable.bucket.UnaliasedBucket
1112
import ch.ergon.dope.resolvable.bucket.UseIndex
1213
import ch.ergon.dope.resolvable.bucket.UseKeysClass
1314

1415
interface KeySpaceResolver : AbstractCouchbaseResolver {
1516
fun resolve(aliasedBucket: AliasedBucketDefinition): CouchbaseDopeQuery =
1617
CouchbaseDopeQuery(
17-
formatBucket(
18-
aliasedBucket.bucketName,
19-
aliasedBucket.scopeName,
20-
aliasedBucket.collectionName,
21-
) + " AS `${aliasedBucket.alias}`",
18+
formatBucket(UnaliasedBucket(aliasedBucket.bucketName, aliasedBucket.scope)) + " AS `${aliasedBucket.alias}`",
2219
)
2320

2421
fun resolve(useKeysClass: UseKeysClass): CouchbaseDopeQuery {
2522
val bucket = when (val bucket = useKeysClass.bucket) {
26-
is AliasedBucket -> bucket.asBucketDefinition().toDopeQuery(this)
23+
is Definable -> bucket.asBucketDefinition().toDopeQuery(this)
2724
else -> useKeysClass.bucket.toDopeQuery(this)
2825
}
2926
val keys = useKeysClass.useKeys.toDopeQuery(this)
@@ -42,7 +39,7 @@ interface KeySpaceResolver : AbstractCouchbaseResolver {
4239
fun resolve(useIndex: UseIndex): CouchbaseDopeQuery {
4340
val bucket = useIndex.bucket
4441
val bucketDopeQuery = when (bucket) {
45-
is AliasedBucket -> bucket.asBucketDefinition().toDopeQuery(this)
42+
is Definable -> bucket.asBucketDefinition().toDopeQuery(this)
4643
else -> bucket.toDopeQuery(this)
4744
}
4845
val refs = useIndex.indexReferences.map { it.toDopeQuery(this) }

couchbase/src/main/kotlin/ch/ergon/dope/couchbase/resolver/clause/ClauseResolver.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ch.ergon.dope.couchbase.util.formatToQueryString
77
import ch.ergon.dope.couchbase.util.formatToQueryStringWithSymbol
88
import ch.ergon.dope.merge
99
import ch.ergon.dope.resolvable.AliasedSelectClause
10-
import ch.ergon.dope.resolvable.bucket.AliasedBucket
10+
import ch.ergon.dope.resolvable.bucket.Definable
1111
import ch.ergon.dope.resolvable.clause.Clause
1212
import ch.ergon.dope.resolvable.clause.ISelectOffsetClause
1313
import ch.ergon.dope.resolvable.clause.joinHint.HashOrNestedLoopHint
@@ -76,7 +76,7 @@ interface ClauseResolver : SelectClauseResolver {
7676

7777
is UpdateClause -> {
7878
val updatable = when (val updatable = clause.updatable) {
79-
is AliasedBucket -> updatable.asBucketDefinition().toDopeQuery(this)
79+
is Definable -> updatable.asBucketDefinition().toDopeQuery(this)
8080
else -> clause.updatable.toDopeQuery(this)
8181
}
8282
CouchbaseDopeQuery(
@@ -87,7 +87,7 @@ interface ClauseResolver : SelectClauseResolver {
8787

8888
is DeleteClause -> {
8989
val bucket = when (val deletable = clause.deletable) {
90-
is AliasedBucket -> deletable.asBucketDefinition().toDopeQuery(this)
90+
is Definable -> deletable.asBucketDefinition().toDopeQuery(this)
9191
else -> clause.deletable.toDopeQuery(this)
9292
}
9393
CouchbaseDopeQuery(

couchbase/src/main/kotlin/ch/ergon/dope/couchbase/resolver/clause/MergeableClauseResolver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ch.ergon.dope.couchbase.resolver.AbstractCouchbaseResolver
55
import ch.ergon.dope.couchbase.util.formatPartsToQueryStringWithSpace
66
import ch.ergon.dope.orEmpty
77
import ch.ergon.dope.resolvable.AliasedSelectClause
8-
import ch.ergon.dope.resolvable.bucket.AliasedBucket
8+
import ch.ergon.dope.resolvable.bucket.Definable
99
import ch.ergon.dope.resolvable.clause.model.mergeable.JoinType
1010
import ch.ergon.dope.resolvable.clause.model.mergeable.MergeableClause
1111
import ch.ergon.dope.resolvable.clause.model.mergeable.NestType
@@ -21,7 +21,7 @@ interface MergeableClauseResolver : AbstractCouchbaseResolver {
2121
}
2222
val parent = selectClause.parentClause.toDopeQuery(this)
2323
val mergeable = when (val mergeable = selectClause.mergeable) {
24-
is AliasedBucket -> mergeable.asBucketDefinition().toDopeQuery(this)
24+
is Definable -> mergeable.asBucketDefinition().toDopeQuery(this)
2525
is AliasedSelectClause<*> -> mergeable.asAliasedSelectClauseDefinition().toDopeQuery(this)
2626
else -> selectClause.mergeable.toDopeQuery(this)
2727
}

couchbase/src/main/kotlin/ch/ergon/dope/couchbase/resolver/clause/SelectClauseResolver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ch.ergon.dope.couchbase.util.formatQueryStringWithNullableFirst
55
import ch.ergon.dope.couchbase.util.formatToQueryStringWithSymbol
66
import ch.ergon.dope.orEmpty
77
import ch.ergon.dope.resolvable.AliasedSelectClause
8-
import ch.ergon.dope.resolvable.bucket.AliasedBucket
8+
import ch.ergon.dope.resolvable.bucket.Definable
99
import ch.ergon.dope.resolvable.clause.ISelectOffsetClause
1010
import ch.ergon.dope.resolvable.clause.SetOperator
1111
import ch.ergon.dope.resolvable.clause.model.AliasedUnnestClause
@@ -95,7 +95,7 @@ interface SelectClauseResolver : MergeableClauseResolver {
9595
is FromClause<*> -> {
9696
val parentDopeQuery = selectClause.parentClause.toDopeQuery(this)
9797
val fromableDopeQuery = when (val fromable = selectClause.fromable) {
98-
is AliasedBucket -> fromable.asBucketDefinition().toDopeQuery(this)
98+
is Definable -> fromable.asBucketDefinition().toDopeQuery(this)
9999
is AliasedSelectClause<*> -> fromable.asAliasedSelectClauseDefinition().toDopeQuery(this)
100100
else -> selectClause.fromable.toDopeQuery(this)
101101
}

couchbase/src/main/kotlin/ch/ergon/dope/couchbase/resolver/expression/ExpressionResolver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import ch.ergon.dope.merge
88
import ch.ergon.dope.resolvable.AliasedSelectClause
99
import ch.ergon.dope.resolvable.AliasedSelectClauseDefinition
1010
import ch.ergon.dope.resolvable.Asterisk
11-
import ch.ergon.dope.resolvable.bucket.AliasedBucket
1211
import ch.ergon.dope.resolvable.bucket.Bucket
12+
import ch.ergon.dope.resolvable.bucket.Definable
1313
import ch.ergon.dope.resolvable.expression.Expression
1414
import ch.ergon.dope.resolvable.expression.rowscope.AliasedRowScopeExpression
1515
import ch.ergon.dope.resolvable.expression.rowscope.RowScopeExpression
@@ -20,7 +20,7 @@ interface ExpressionResolver : TypeExpressionResolver {
2020
fun resolve(expression: Expression<*>): CouchbaseDopeQuery = when (expression) {
2121
is TypeExpression<*> -> resolve(expression)
2222

23-
is AliasedBucket -> CouchbaseDopeQuery("`${expression.alias}`")
23+
is Definable -> CouchbaseDopeQuery("`${expression.alias}`")
2424

2525
is Bucket -> CouchbaseDopeQuery(formatBucket(expression))
2626

couchbase/src/main/kotlin/ch/ergon/dope/couchbase/resolver/expression/TypeExpressionResolver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ch.ergon.dope.couchbase.util.formatToQueryString
88
import ch.ergon.dope.couchbase.util.formatToQueryStringWithSeparator
99
import ch.ergon.dope.merge
1010
import ch.ergon.dope.orEmpty
11-
import ch.ergon.dope.resolvable.bucket.AliasedBucket
11+
import ch.ergon.dope.resolvable.bucket.Definable
1212
import ch.ergon.dope.resolvable.bucket.UnaliasedBucket
1313
import ch.ergon.dope.resolvable.expression.operator.FunctionOperator
1414
import ch.ergon.dope.resolvable.expression.operator.InfixOperator
@@ -241,7 +241,7 @@ interface TypeExpressionResolver : InfixOperatorResolver, FunctionOperatorResolv
241241
is IField<*> -> {
242242
val bucket = typeExpression.bucket
243243
val fieldQuery = when (bucket) {
244-
is AliasedBucket -> "`${bucket.alias}`.`${typeExpression.name}`"
244+
is Definable -> "`${bucket.alias}`.`${typeExpression.name}`"
245245

246246
is UnaliasedBucket -> {
247247
val path = when {

couchbase/src/main/kotlin/ch/ergon/dope/couchbase/util/QueryStringBuilder.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal fun formatPathToQueryString(name: String, path: String) =
2525
}
2626

2727
internal fun formatBucket(bucket: Bucket): String =
28-
bucket.name.split('.').plus(listOfNotNull(bucket.scope?.name, bucket.scope?.collection?.name))
28+
bucket.name.split(".").plus(listOfNotNull(bucket.scope?.name, bucket.scope?.collection?.name))
2929
.filter { it.isNotBlank() }
3030
.joinToString(".") { dotPart ->
3131
dotPart
@@ -34,11 +34,6 @@ internal fun formatBucket(bucket: Bucket): String =
3434
.joinToString(":") { "`$it`" }
3535
}
3636

37-
internal fun formatBucket(bucket: String, scope: String? = null, collection: String? = null): String =
38-
listOfNotNull(bucket, scope, collection)
39-
.filter { it.isNotBlank() }
40-
.joinToString(".") { "`$it`" }
41-
4237
internal fun formatPartsToQueryStringWithSpace(vararg string: String?) =
4338
listOfNotNull(*string).joinToString(separator = " ")
4439

0 commit comments

Comments
 (0)