Skip to content

Commit

Permalink
Review issues: eliminate AliasOnlyExpression interface
Browse files Browse the repository at this point in the history
  • Loading branch information
obabichevjb committed Nov 4, 2024
1 parent 12b9bd2 commit 3d8cd9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
22 changes: 11 additions & 11 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Alias.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,19 @@ class ExpressionAlias<T>(val delegate: Expression<T>, val alias: String) : Expre
/** Returns an [Expression] containing only the string representation of this [alias]. */
fun aliasOnlyExpression(): Expression<T> {
return if (delegate is ExpressionWithColumnType<T>) {
object : AliasOnlyExpression<T>, Function<T>(delegate.columnType) {
override val origin: ExpressionAlias<T> = this@ExpressionAlias

object : Function<T>(delegate.columnType) {
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { append(alias) }
}
} else {
object : AliasOnlyExpression<T>, Expression<T>() {
override val origin: ExpressionAlias<T> = this@ExpressionAlias

object : Expression<T>() {
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { append(alias) }
}
}
}
}

internal interface AliasOnlyExpression<T> {
val origin: ExpressionAlias<T>
}

/** Represents a temporary SQL identifier, [alias], for a [query]. */
class QueryAlias(val query: AbstractQuery<*>, val alias: String) : ColumnSet() {

override fun describe(s: Transaction, queryBuilder: QueryBuilder) = queryBuilder {
append("(")
query.prepareSQL(queryBuilder)
Expand All @@ -170,6 +161,15 @@ class QueryAlias(val query: AbstractQuery<*>, val alias: String) : ColumnSet() {
?: error("Field not found in original table fields")
}

internal val aliasedFields: List<Expression<*>>
get() = query.set.fields.map { expression ->
when (expression) {
is Column<*> -> expression.clone()
is ExpressionAlias<*> -> expression.delegate.alias("$alias.${expression.alias}").aliasOnlyExpression()
else -> expression
}
}

override fun join(
otherTable: ColumnSet,
joinType: JoinType,
Expand Down
15 changes: 3 additions & 12 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,9 @@ class Join(
) { it.joinPart.columns }

override val fields: List<Expression<*>>
get() = joinParts.flatMapTo(
table.fields.toMutableList()
) {
when {
it.joinPart is QueryAlias ->
it.joinPart.fields.map { field ->
when (field) {
is AliasOnlyExpression<*> ->
field.alias("${it.joinPart.alias}.${field.origin.alias}").aliasOnlyExpression()
else -> field
}
}
get() = joinParts.flatMapTo(table.fields.toMutableList()) {
when (it.joinPart) {
is QueryAlias -> it.joinPart.aliasedFields
else -> it.joinPart.fields
}
}
Expand Down

0 comments on commit 3d8cd9b

Please sign in to comment.