diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SQLExpressionBuilder.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SQLExpressionBuilder.kt index 720843eb4d..a231eff4d1 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SQLExpressionBuilder.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SQLExpressionBuilder.kt @@ -393,7 +393,7 @@ interface ISqlExpressionBuilder { /** Checks if this [EntityID] expression is less than some [t] value. */ @JvmName("lessEntityID") infix fun > Column>.less(t: T): LessOp = - LessOp(this, wrap(EntityID(t, (this.foreignKey?.targetTable ?: this.table) as IdTable))) + LessOp(this, wrap(EntityID(t, this.idTable()))) /** Checks if this [EntityID] expression is less than some [other] expression. */ infix fun , E : EntityID?, V : T?> ExpressionWithColumnType.less( @@ -417,7 +417,7 @@ interface ISqlExpressionBuilder { /** Checks if this [EntityID] expression is less than or equal to some [t] value */ @JvmName("lessEqEntityID") infix fun > Column>.lessEq(t: T): LessEqOp = - LessEqOp(this, wrap(EntityID(t, (this.foreignKey?.targetTable ?: this.table) as IdTable))) + LessEqOp(this, wrap(EntityID(t, this.idTable()))) /** Checks if this [EntityID] expression is less than or equal to some [other] expression */ infix fun , E : EntityID?, V : T?> ExpressionWithColumnType.lessEq( @@ -441,7 +441,7 @@ interface ISqlExpressionBuilder { /** Checks if this [EntityID] expression is greater than some [t] value. */ @JvmName("greaterEntityID") infix fun > Column>.greater(t: T): GreaterOp = - GreaterOp(this, wrap(EntityID(t, (this.foreignKey?.targetTable ?: this.table) as IdTable))) + GreaterOp(this, wrap(EntityID(t, this.idTable()))) /** Checks if this [EntityID] expression is greater than some [other] expression. */ infix fun , E : EntityID?, V : T?> ExpressionWithColumnType.greater( @@ -465,7 +465,7 @@ interface ISqlExpressionBuilder { /** Checks if this [EntityID] expression is greater than or equal to some [t] value */ @JvmName("greaterEqEntityID") infix fun > Column>.greaterEq(t: T): GreaterEqOp = - GreaterEqOp(this, wrap(EntityID(t, (this.foreignKey?.targetTable ?: this.table) as IdTable))) + GreaterEqOp(this, wrap(EntityID(t, this.idTable()))) /** Checks if this [EntityID] expression is greater than or equal to some [other] expression */ infix fun , E : EntityID?, V : T?> ExpressionWithColumnType.greaterEq( @@ -484,11 +484,7 @@ interface ISqlExpressionBuilder { /** Returns `true` if this [EntityID] expression is between the values [from] and [to], `false` otherwise. */ fun , E : EntityID?> Column.between(from: T, to: T): Between = - Between( - this, - wrap(EntityID(from, (this.foreignKey?.targetTable ?: this.table) as IdTable)), - wrap(EntityID(to, (this.foreignKey?.targetTable ?: this.table) as IdTable)) - ) + Between(this, wrap(EntityID(from, this.idTable())), wrap(EntityID(to, this.idTable()))) /** Returns `true` if this expression is null, `false` otherwise. */ fun Expression.isNull(): IsNullOp = IsNullOp(this) @@ -506,7 +502,7 @@ interface ISqlExpressionBuilder { /** Checks if this expression is equal to some [t] value, with `null` treated as a comparable value */ @JvmName("isNotDistinctFromEntityID") infix fun > Column>.isNotDistinctFrom(t: T): IsNotDistinctFromOp = - IsNotDistinctFromOp(this, wrap(EntityID(t, (this.foreignKey?.targetTable ?: this.table) as IdTable))) + IsNotDistinctFromOp(this, wrap(EntityID(t, this.idTable()))) /** Checks if this [EntityID] expression is equal to some [other] expression */ infix fun , E : EntityID?, V : T?> ExpressionWithColumnType.isNotDistinctFrom( @@ -528,7 +524,7 @@ interface ISqlExpressionBuilder { /** Checks if this expression is not equal to some [t] value, with `null` treated as a comparable value */ @JvmName("isDistinctFromEntityID") infix fun > Column>.isDistinctFrom(t: T): IsDistinctFromOp = - IsDistinctFromOp(this, wrap(EntityID(t, (this.foreignKey?.targetTable ?: this.table) as IdTable))) + IsDistinctFromOp(this, wrap(EntityID(t, this.idTable()))) /** Checks if this [EntityID] expression is not equal to some [other] expression */ infix fun , E : EntityID?, V : T?> ExpressionWithColumnType.isDistinctFrom( @@ -933,6 +929,12 @@ interface ISqlExpressionBuilder { fun ExpressionWithColumnType.intToDecimal(): NoOpConversion = NoOpConversion(this, DecimalColumnType(precision = 15, scale = 0)) + + private fun , E : EntityID> Column.idTable(): IdTable = + when (val table = this.foreignKey?.targetTable ?: this.table) { + is Alias<*> -> table.delegate + else -> table + } as IdTable } /** diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/FetchBatchedResultsTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/FetchBatchedResultsTests.kt index 5937e6bd97..71f1516be9 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/FetchBatchedResultsTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/FetchBatchedResultsTests.kt @@ -1,7 +1,12 @@ package org.jetbrains.exposed.sql.tests.shared.dml import org.jetbrains.exposed.dao.id.IntIdTable -import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.ReferenceOption +import org.jetbrains.exposed.sql.SortOrder +import org.jetbrains.exposed.sql.alias +import org.jetbrains.exposed.sql.batchInsert +import org.jetbrains.exposed.sql.insert +import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.tests.DatabaseTestsBase import org.jetbrains.exposed.sql.tests.shared.assertEqualLists import org.junit.Test @@ -117,4 +122,16 @@ class FetchBatchedResultsTests : DatabaseTestsBase() { (tester2 innerJoin tester1).selectAll().fetchBatchedResults(10_000).flatten() } } + + @Test + fun testFetchBatchedResultsWithAlias() { + val tester = object : IntIdTable("tester") { + val name = varchar("name", 1) + } + withTables(tester) { + tester.insert { it[name] = "a" } + tester.insert { it[name] = "b" } + tester.alias("tester_alias").selectAll().fetchBatchedResults(1).flatten() + } + } }