From 3d8cd9b064b6131220531914edc93a6c8f29ac7e Mon Sep 17 00:00:00 2001 From: Oleg Babichev Date: Mon, 4 Nov 2024 14:39:13 +0100 Subject: [PATCH] Review issues: eliminate AliasOnlyExpression interface --- .../kotlin/org/jetbrains/exposed/sql/Alias.kt | 22 +++++++++---------- .../kotlin/org/jetbrains/exposed/sql/Table.kt | 15 +++---------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Alias.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Alias.kt index ef9cf763d6..0ad2e0fa8f 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Alias.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Alias.kt @@ -123,28 +123,19 @@ class ExpressionAlias(val delegate: Expression, val alias: String) : Expre /** Returns an [Expression] containing only the string representation of this [alias]. */ fun aliasOnlyExpression(): Expression { return if (delegate is ExpressionWithColumnType) { - object : AliasOnlyExpression, Function(delegate.columnType) { - override val origin: ExpressionAlias = this@ExpressionAlias - + object : Function(delegate.columnType) { override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { append(alias) } } } else { - object : AliasOnlyExpression, Expression() { - override val origin: ExpressionAlias = this@ExpressionAlias - + object : Expression() { override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { append(alias) } } } } } -internal interface AliasOnlyExpression { - val origin: ExpressionAlias -} - /** 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) @@ -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> + 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, diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt index 14b48628ea..11171dac14 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt @@ -247,18 +247,9 @@ class Join( ) { it.joinPart.columns } override val fields: List> - 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 } }