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

where in subquery #610

Merged
merged 1 commit into from
Jul 27, 2019
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
6 changes: 6 additions & 0 deletions exposed/src/main/kotlin/org/jetbrains/exposed/sql/Op.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class InListOrNotInListOp<T>(val expr: ExpressionWithColumnType<T>, val list: It
}
}

class InSubQueryOp<T>(val expr: Expression<T>, val query: Query): Op<Boolean>() {
override fun toSQL(queryBuilder: QueryBuilder): String = buildString {
append("${expr.toSQL(queryBuilder)} IN (${query.prepareSQL(queryBuilder)})")
}
}

class QueryParameter<T>(val value: T, val sqlType: IColumnType) : Expression<T>() {
override fun toSQL(queryBuilder: QueryBuilder): String = queryBuilder.registerArgument(sqlType, value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ object SqlExpressionBuilder {

infix fun<T> ExpressionWithColumnType<T>.notInList(list: Iterable<T>): Op<Boolean> = InListOrNotInListOp(this, list, isInList = false)

infix fun<T> ExpressionWithColumnType<T>.inSubQuery(query: Query): Op<Boolean> = InSubQueryOp(this, query)

@Suppress("UNCHECKED_CAST")
fun<T, S: T?> ExpressionWithColumnType<S>.asLiteral(value: T) = when (value) {
is Boolean -> booleanLiteral(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,14 @@ class DMLTests : DatabaseTestsBase() {
}
}

@Test
fun testInSubQuery01() {
withCitiesAndUsers { cities, users, userData ->
val r = cities.select { cities.id inSubQuery cities.slice(cities.id).select { cities.id eq 2 } }
assertEquals(1, r.count())
}
}

@Test
fun testInsertSelect01() {
withCitiesAndUsers(exclude = listOf(TestDB.ORACLE)) { cities, users, userData ->
Expand Down