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-218: localized parameter and iterator manager creation #52

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions core/src/main/kotlin/ch/ergon/dope/Manager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ch.ergon.dope

class DopeQueryManager {
val parameterManager = ParameterManager()
val iteratorManager = IteratorManager()
}

class ParameterManager {
var count: Int = 1
get() = field++
private set
}

class IteratorManager {
private var count: Int = 1
get() = field++

fun getIteratorName() = "iterator$count"
}
3 changes: 2 additions & 1 deletion core/src/main/kotlin/ch/ergon/dope/resolvable/Resolvable.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package ch.ergon.dope.resolvable

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager

interface Resolvable {
fun toDopeQuery(): DopeQuery
fun toDopeQuery(manager: DopeQueryManager): DopeQuery
}
10 changes: 2 additions & 8 deletions core/src/main/kotlin/ch/ergon/dope/resolvable/clause/Clause.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package ch.ergon.dope.resolvable.clause

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.Resolvable
import ch.ergon.dope.resolvable.expression.unaliased.type.ParameterManager
import ch.ergon.dope.resolvable.expression.unaliased.type.collection.IteratorManager

interface Clause : Resolvable {
fun build(): DopeQuery {
ParameterManager.resetCounter()
IteratorManager.resetCounter()
return toDopeQuery()
}
fun build() = toDopeQuery(manager = DopeQueryManager())
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.IDeleteClause
import ch.ergon.dope.resolvable.fromable.Bucket

class DeleteClause(private val bucket: Bucket) : IDeleteClause {
override fun toDopeQuery(): DopeQuery {
val bucketDopeQuery = bucket.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val bucketDopeQuery = bucket.toDopeQuery(manager)
return DopeQuery(
queryString = "DELETE FROM ${bucketDopeQuery.queryString}",
parameters = bucketDopeQuery.parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.ISelectClause
import ch.ergon.dope.resolvable.clause.ISelectUnnestClause
import ch.ergon.dope.resolvable.formatToQueryStringWithSymbol
import ch.ergon.dope.resolvable.fromable.Fromable

class FromClause(private val fromable: Fromable, private val parentClause: ISelectClause) : ISelectUnnestClause {
override fun toDopeQuery(): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery()
val fromableDopeQuery = fromable.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery(manager)
val fromableDopeQuery = fromable.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryStringWithSymbol(parentDopeQuery.queryString, "FROM", fromableDopeQuery.queryString),
parameters = fromableDopeQuery.parameters + parentDopeQuery.parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.ISelectGroupByClause
import ch.ergon.dope.resolvable.clause.ISelectWhereClause
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
Expand All @@ -12,10 +13,10 @@ class GroupByClause(
private vararg val fields: Field<out ValidType>,
private val parentClause: ISelectWhereClause,
) : ISelectGroupByClause {
override fun toDopeQuery(): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery()
val fieldDopeQuery = field.toDopeQuery()
val fieldsDopeQuery = fields.map { it.toDopeQuery() }
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery(manager)
val fieldDopeQuery = field.toDopeQuery(manager)
val fieldsDopeQuery = fields.map { it.toDopeQuery(manager) }
return DopeQuery(
queryString = formatToQueryStringWithSymbol(
parentDopeQuery.queryString,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.ISelectFromClause
import ch.ergon.dope.resolvable.clause.ISelectJoinClause
import ch.ergon.dope.resolvable.clause.model.JoinType.INNER_JOIN
Expand Down Expand Up @@ -72,32 +73,32 @@ sealed class SelectJoinClause : ISelectJoinClause {
this.onKeys = null
}

override fun toDopeQuery(): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery()
val bucketDopeQuery = bucket.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery(manager)
val bucketDopeQuery = bucket.toDopeQuery(manager)
val joinQueryString = "${parentDopeQuery.queryString} ${joinType.type} ${bucketDopeQuery.queryString}"
val joinParameters = parentDopeQuery.parameters + bucketDopeQuery.parameters

return when (onType) {
ON -> {
val onConditionDopeQuery = onCondition?.toDopeQuery()
val onConditionDopeQuery = onCondition?.toDopeQuery(manager)
DopeQuery(
queryString = "$joinQueryString ON ${onConditionDopeQuery?.queryString}",
parameters = joinParameters + onConditionDopeQuery?.parameters.orEmpty(),
)
}

ON_KEYS -> {
val keyDopeQuery = onKeys?.toDopeQuery()
val keyDopeQuery = onKeys?.toDopeQuery(manager)
DopeQuery(
queryString = "$joinQueryString ON KEYS ${keyDopeQuery?.queryString}",
parameters = joinParameters + keyDopeQuery?.parameters.orEmpty(),
)
}

ON_KEY_FOR -> {
val keyDopeQuery = onKey?.toDopeQuery()
val forBucketDopeQuery = forBucket?.toDopeQuery()
val keyDopeQuery = onKey?.toDopeQuery(manager)
val forBucketDopeQuery = forBucket?.toDopeQuery(manager)
DopeQuery(
queryString = "$joinQueryString ON KEY ${keyDopeQuery?.queryString} FOR ${forBucketDopeQuery?.queryString}",
parameters = joinParameters + keyDopeQuery?.parameters.orEmpty() +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.Clause
import ch.ergon.dope.resolvable.clause.IDeleteLimitClause
import ch.ergon.dope.resolvable.clause.IDeleteWhereClause
Expand All @@ -16,9 +17,9 @@ sealed class LimitClause(
private val numberExpression: TypeExpression<NumberType>,
private val parentClause: Clause,
) {
fun toDopeQuery(): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery()
val numberDopeQuery = numberExpression.toDopeQuery()
fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery(manager)
val numberDopeQuery = numberExpression.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryStringWithSymbol(parentDopeQuery.queryString, "LIMIT", numberDopeQuery.queryString),
parameters = numberDopeQuery.parameters + parentDopeQuery.parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.Clause
import ch.ergon.dope.resolvable.clause.IDeleteLimitClause
import ch.ergon.dope.resolvable.clause.IDeleteOffsetClause
Expand All @@ -14,9 +15,9 @@ sealed class OffsetClause(
private val numberExpression: TypeExpression<NumberType>,
private val parentClause: Clause,
) {
fun toDopeQuery(): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery()
val numberDopeQuery = numberExpression.toDopeQuery()
fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery(manager)
val numberDopeQuery = numberExpression.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryStringWithSymbol(parentDopeQuery.queryString, "OFFSET", numberDopeQuery.queryString),
parameters = numberDopeQuery.parameters + parentDopeQuery.parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.ISelectGroupByClause
import ch.ergon.dope.resolvable.clause.ISelectOrderByClause
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
Expand All @@ -17,9 +18,9 @@ private const val ORDER_BY = "ORDER BY"
open class SelectOrderByClause(private val stringField: Field<StringType>, private val parentClause: ISelectGroupByClause) :
ISelectOrderByClause {

override fun toDopeQuery(): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery()
val stringDopeQuery = stringField.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery(manager)
val stringDopeQuery = stringField.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryStringWithSymbol(parentDopeQuery.queryString, ORDER_BY, stringDopeQuery.queryString),
parameters = stringDopeQuery.parameters + parentDopeQuery.parameters,
Expand All @@ -33,9 +34,9 @@ class SelectOrderByTypeClause(
private val parentClause: ISelectGroupByClause,
) : SelectOrderByClause(stringField, parentClause) {

override fun toDopeQuery(): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery()
val stringDopeQuery = stringField.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery(manager)
val stringDopeQuery = stringField.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryStringWithSymbol(parentDopeQuery.queryString, ORDER_BY, stringDopeQuery.queryString + " $orderByType"),
parameters = stringDopeQuery.parameters + parentDopeQuery.parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.Clause
import ch.ergon.dope.resolvable.clause.IDeleteOffsetClause
import ch.ergon.dope.resolvable.clause.IDeleteReturningClause
Expand All @@ -15,10 +16,10 @@ sealed class ReturningClause(
private vararg val fields: Field<out ValidType>,
private val parentClause: Clause,
) {
fun toDopeQuery(): DopeQuery {
val fieldsDopeQuery = fields.map { it.toDopeQuery() }
val fieldDopeQuery = field.toDopeQuery()
val parentDopeQuery = parentClause.toDopeQuery()
fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val fieldsDopeQuery = fields.map { it.toDopeQuery(manager) }
val fieldDopeQuery = field.toDopeQuery(manager)
val parentDopeQuery = parentClause.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryStringWithSymbol(
parentDopeQuery.queryString,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.ISelectClause
import ch.ergon.dope.resolvable.expression.Expression
import ch.ergon.dope.resolvable.formatToQueryString

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() }
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val expressionDopeQuery = expression.toDopeQuery(manager)
val expressionsDopeQuery = expressions.map { it.toDopeQuery(manager) }
return DopeQuery(
queryString = formatToQueryString(
"SELECT",
Expand All @@ -25,8 +26,8 @@ class SelectClause(private val expression: Expression, private vararg val expres

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

override fun toDopeQuery(): DopeQuery {
val expressionDopeQuery = expression.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val expressionDopeQuery = expression.toDopeQuery(manager)
return DopeQuery(
formatToQueryString("SELECT RAW", expressionDopeQuery.queryString),
expressionDopeQuery.parameters,
Expand All @@ -35,9 +36,9 @@ 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()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val expressionsDopeQuery = expressions.map { it.toDopeQuery(manager) }
val expressionDopeQuery = expression.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryString(
"SELECT DISTINCT",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.Resolvable
import ch.ergon.dope.resolvable.clause.IUpdateSetClause
import ch.ergon.dope.resolvable.expression.TypeExpression
Expand All @@ -17,10 +18,10 @@ class SetClause(
private vararg val fieldAssignments: SetAssignment<out ValidType>,
private val parentClause: IUpdateSetClause,
) : IUpdateSetClause {
override fun toDopeQuery(): DopeQuery {
val fieldAssignmentDopeQuery = fieldAssignment.toDopeQuery()
val fieldAssignmentsDopeQuery = fieldAssignments.map { it.toDopeQuery() }
val parentClauseDopeQuery = parentClause.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val fieldAssignmentDopeQuery = fieldAssignment.toDopeQuery(manager)
val fieldAssignmentsDopeQuery = fieldAssignments.map { it.toDopeQuery(manager) }
val parentClauseDopeQuery = parentClause.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryString(
"${parentClauseDopeQuery.queryString} SET",
Expand Down Expand Up @@ -66,9 +67,9 @@ class SetAssignment<T : ValidType>(
private val field: Field<T>,
private val value: TypeExpression<T>,
) : Resolvable {
override fun toDopeQuery(): DopeQuery {
val fieldDopeQuery = field.toDopeQuery()
val valueDopeQuery = value.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val fieldDopeQuery = field.toDopeQuery(manager)
val valueDopeQuery = value.toDopeQuery(manager)
return DopeQuery(
queryString = "${fieldDopeQuery.queryString} = ${valueDopeQuery.queryString}",
parameters = fieldDopeQuery.parameters + valueDopeQuery.parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.ISelectUnnestClause
import ch.ergon.dope.resolvable.expression.AliasedExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
Expand All @@ -12,9 +13,9 @@ private const val UNNEST = "UNNEST"

class UnnestClause<T : ValidType>(private val arrayTypeField: Field<ArrayType<T>>, private val parentClause: ISelectUnnestClause) :
ISelectUnnestClause {
override fun toDopeQuery(): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery()
val arrayTypeDopeQuery = arrayTypeField.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery(manager)
val arrayTypeDopeQuery = arrayTypeField.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryStringWithSymbol(parentDopeQuery.queryString, UNNEST, arrayTypeDopeQuery.queryString),
parameters = parentDopeQuery.parameters + arrayTypeDopeQuery.parameters,
Expand All @@ -26,9 +27,9 @@ class AliasedUnnestClause<T : ValidType>(
private val aliasedExpression: AliasedExpression<ArrayType<T>>,
private val parentClause: ISelectUnnestClause,
) : ISelectUnnestClause {
override fun toDopeQuery(): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery()
val aliasedExpressionDopeQuery = aliasedExpression.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val parentDopeQuery = parentClause.toDopeQuery(manager)
val aliasedExpressionDopeQuery = aliasedExpression.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryStringWithSymbol(parentDopeQuery.queryString, UNNEST, aliasedExpressionDopeQuery.queryString),
parameters = parentDopeQuery.parameters + aliasedExpressionDopeQuery.parameters,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ergon.dope.resolvable.clause.model

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.resolvable.clause.IUpdateUnsetClause
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.resolvable.formatToQueryString
Expand All @@ -11,10 +12,10 @@ class UnsetClause(
private vararg val fields: Field<out ValidType>,
private val parentClause: IUpdateUnsetClause,
) : IUpdateUnsetClause {
override fun toDopeQuery(): DopeQuery {
val fieldDopeQuery = field.toDopeQuery()
val fieldsDopeQuery = fields.map { it.toDopeQuery() }
val parentClauseDopeQuery = parentClause.toDopeQuery()
override fun toDopeQuery(manager: DopeQueryManager): DopeQuery {
val fieldDopeQuery = field.toDopeQuery(manager)
val fieldsDopeQuery = fields.map { it.toDopeQuery(manager) }
val parentClauseDopeQuery = parentClause.toDopeQuery(manager)
return DopeQuery(
queryString = formatToQueryString(
"${parentClauseDopeQuery.queryString} UNSET",
Expand Down
Loading
Loading