Skip to content

Commit

Permalink
added: where in subquery (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
otkmnb2783 authored and Tapac committed Jul 27, 2019
1 parent 9267c57 commit 9b82661
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
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

0 comments on commit 9b82661

Please sign in to comment.