-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
ALL
and ANY
operators accepting array, subquery, or tab…
…le parameters (#1886) * Initially implement `allOp` and `anyOp` for `Iterable<T>` and add some simple tests which are not run yet * Pass arrays instead of lists/iterables, getting `testEqAny` (for an array of strings) to work on PostgreSQL (`postgres` and `postgresNG`) Cherry-picked from: 7dd846b * Add an alternative custom function implementation of `ALL` and `ANY` and refactor to simplify the `AllAnyOp` implementation As tested, it works for H2 as well in addition to PostgreSQL. * Improve code, add comments, and skip tests on unsupported dialects * Add a KDoc line * Add some more KDocs, add an `eqAny` shortcut, and fix a typo * Describe return types explicitly to conform to the contributing guidelines * Run `apiDump` again * Refactor and improve as requested by @bog-walk Major changes: 1. support the `ALL` and `ANY` operators taking subquery arguments and table arguments, and the `IN` operator taking table arguments 1. remove some no longer needed code, simplify code, and rename some functions 1. add related tests 1. run `apiDump` The operators (`IN`, `ANY`, and `ALL`) with table arguments are only supported by PostgreSQL and H2. MySQL claims supporting this but it does not work with the JDBC driver as tested. Those with subquery arguments are not supported by SQLite. * Complete the remaining tests * Fix code issues as requested by @bog-walk * AutoCorrect by detekt rules
- Loading branch information
Showing
11 changed files
with
342 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ops/AllAnyOps.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.jetbrains.exposed.sql.ops | ||
|
||
import org.jetbrains.exposed.sql.AbstractQuery | ||
import org.jetbrains.exposed.sql.Op | ||
import org.jetbrains.exposed.sql.QueryBuilder | ||
import org.jetbrains.exposed.sql.Table | ||
import org.jetbrains.exposed.sql.UntypedAndUnsizedArrayColumnType | ||
|
||
abstract class AllAnyFromBaseOp<T, SubSearch>(val isAny: Boolean, val subSearch: SubSearch) : Op<T>() { | ||
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { | ||
+(if (isAny) "ANY" else "ALL") | ||
+" (" | ||
registerSubSearchArgument(subSearch) | ||
+')' | ||
} | ||
|
||
abstract fun QueryBuilder.registerSubSearchArgument(subSearch: SubSearch) | ||
} | ||
|
||
class AllAnyFromSubQueryOp<T>(isAny: Boolean, subQuery: AbstractQuery<*>) : AllAnyFromBaseOp<T, AbstractQuery<*>>(isAny, subQuery) { | ||
override fun QueryBuilder.registerSubSearchArgument(subSearch: AbstractQuery<*>) { | ||
subSearch.prepareSQL(this) | ||
} | ||
} | ||
|
||
/** This function is only supported by PostgreSQL and H2 dialects. */ | ||
class AllAnyFromArrayOp<T>(isAny: Boolean, array: Array<T>) : AllAnyFromBaseOp<T, Array<T>>(isAny, array) { | ||
override fun QueryBuilder.registerSubSearchArgument(subSearch: Array<T>) = | ||
registerArgument(UntypedAndUnsizedArrayColumnType, subSearch) | ||
} | ||
|
||
/** This function is only supported by PostgreSQL and H2 dialects. */ | ||
class AllAnyFromTableOp<T>(isAny: Boolean, table: Table) : AllAnyFromBaseOp<T, Table>(isAny, table) { | ||
override fun QueryBuilder.registerSubSearchArgument(subSearch: Table) { | ||
+"TABLE " | ||
+subSearch.tableName | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ops/InTableOp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.jetbrains.exposed.sql.ops | ||
|
||
import org.jetbrains.exposed.sql.ComplexExpression | ||
import org.jetbrains.exposed.sql.Expression | ||
import org.jetbrains.exposed.sql.Op | ||
import org.jetbrains.exposed.sql.QueryBuilder | ||
import org.jetbrains.exposed.sql.Table | ||
|
||
/** This function is only supported by PostgreSQL and H2 dialects. */ | ||
class InTableOp( | ||
val expr: Expression<*>, | ||
/** the table to check against. */ | ||
val table: Table, | ||
/** Returns `true` if the check is inverted, `false` otherwise. */ | ||
val isInTable: Boolean = true | ||
) : Op<Boolean>(), ComplexExpression { | ||
override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder { | ||
+expr | ||
+" " | ||
+if (isInTable) "" else "NOT " | ||
+"IN (" | ||
+"TABLE " | ||
+table.tableName | ||
+')' | ||
} | ||
} |
Oops, something went wrong.