Skip to content

Commit

Permalink
Use term "clause" instead of terms "when" and "whenClause"
Browse files Browse the repository at this point in the history
  • Loading branch information
obabichevjb committed May 22, 2024
1 parent 3fadd84 commit 10194a6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 63 deletions.
28 changes: 14 additions & 14 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -3018,8 +3018,8 @@ public abstract class org/jetbrains/exposed/sql/statements/MergeStatement : org/
public fun arguments ()Ljava/lang/Iterable;
public fun executeInternal (Lorg/jetbrains/exposed/sql/statements/api/PreparedStatementApi;Lorg/jetbrains/exposed/sql/Transaction;)Ljava/lang/Integer;
public synthetic fun executeInternal (Lorg/jetbrains/exposed/sql/statements/api/PreparedStatementApi;Lorg/jetbrains/exposed/sql/Transaction;)Ljava/lang/Object;
protected final fun getClauses ()Ljava/util/List;
public final fun getTable ()Lorg/jetbrains/exposed/sql/Table;
protected final fun getWhenClauses ()Ljava/util/List;
public final fun whenMatchedDelete (Lorg/jetbrains/exposed/sql/Op;)V
public static synthetic fun whenMatchedDelete$default (Lorg/jetbrains/exposed/sql/statements/MergeStatement;Lorg/jetbrains/exposed/sql/Op;ILjava/lang/Object;)V
public final fun whenMatchedUpdate (Lorg/jetbrains/exposed/sql/Op;Lorg/jetbrains/exposed/sql/Op;Lkotlin/jvm/functions/Function1;)V
Expand All @@ -3028,31 +3028,31 @@ public abstract class org/jetbrains/exposed/sql/statements/MergeStatement : org/
public static synthetic fun whenNotMatchedInsert$default (Lorg/jetbrains/exposed/sql/statements/MergeStatement;Lorg/jetbrains/exposed/sql/Op;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
}

public final class org/jetbrains/exposed/sql/statements/MergeStatement$When {
public fun <init> (Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;Ljava/util/List;Lorg/jetbrains/exposed/sql/Op;Lorg/jetbrains/exposed/sql/Op;)V
public synthetic fun <init> (Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;Ljava/util/List;Lorg/jetbrains/exposed/sql/Op;Lorg/jetbrains/exposed/sql/Op;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;
public final class org/jetbrains/exposed/sql/statements/MergeStatement$Clause {
public fun <init> (Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;Ljava/util/List;Lorg/jetbrains/exposed/sql/Op;Lorg/jetbrains/exposed/sql/Op;)V
public synthetic fun <init> (Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;Ljava/util/List;Lorg/jetbrains/exposed/sql/Op;Lorg/jetbrains/exposed/sql/Op;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;
public final fun component2 ()Ljava/util/List;
public final fun component3 ()Lorg/jetbrains/exposed/sql/Op;
public final fun component4 ()Lorg/jetbrains/exposed/sql/Op;
public final fun copy (Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;Ljava/util/List;Lorg/jetbrains/exposed/sql/Op;Lorg/jetbrains/exposed/sql/Op;)Lorg/jetbrains/exposed/sql/statements/MergeStatement$When;
public static synthetic fun copy$default (Lorg/jetbrains/exposed/sql/statements/MergeStatement$When;Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;Ljava/util/List;Lorg/jetbrains/exposed/sql/Op;Lorg/jetbrains/exposed/sql/Op;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/statements/MergeStatement$When;
public final fun copy (Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;Ljava/util/List;Lorg/jetbrains/exposed/sql/Op;Lorg/jetbrains/exposed/sql/Op;)Lorg/jetbrains/exposed/sql/statements/MergeStatement$Clause;
public static synthetic fun copy$default (Lorg/jetbrains/exposed/sql/statements/MergeStatement$Clause;Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;Ljava/util/List;Lorg/jetbrains/exposed/sql/Op;Lorg/jetbrains/exposed/sql/Op;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/statements/MergeStatement$Clause;
public fun equals (Ljava/lang/Object;)Z
public final fun getAction ()Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;
public final fun getAction ()Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;
public final fun getAnd ()Lorg/jetbrains/exposed/sql/Op;
public final fun getArguments ()Ljava/util/List;
public final fun getDeleteWhere ()Lorg/jetbrains/exposed/sql/Op;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class org/jetbrains/exposed/sql/statements/MergeStatement$WhenAction : java/lang/Enum {
public static final field DELETE Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;
public static final field INSERT Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;
public static final field UPDATE Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;
public final class org/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction : java/lang/Enum {
public static final field DELETE Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;
public static final field INSERT Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;
public static final field UPDATE Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;
public static fun values ()[Lorg/jetbrains/exposed/sql/statements/MergeStatement$WhenAction;
public static fun valueOf (Ljava/lang/String;)Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;
public static fun values ()[Lorg/jetbrains/exposed/sql/statements/MergeStatement$ClauseAction;
}

public class org/jetbrains/exposed/sql/statements/MergeTableStatement : org/jetbrains/exposed/sql/statements/MergeStatement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class MergeSelectStatement(
val on: Op<Boolean>
) : MergeStatement(dest) {
override fun prepareSQL(transaction: Transaction, prepared: Boolean): String {
return transaction.db.dialect.functionProvider.mergeSelect(table, selectQuery, transaction, whenClauses, on, prepared)
return transaction.db.dialect.functionProvider.mergeSelect(table, selectQuery, transaction, clauses, on, prepared)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
abstract class MergeStatement(val table: Table) : Statement<Int>(
StatementType.MERGE, listOf(table)
) {
protected val whenClauses = mutableListOf<When>()
protected val clauses = mutableListOf<Clause>()

override fun PreparedStatementApi.executeInternal(transaction: Transaction): Int? {
return executeUpdate()
}

override fun arguments(): Iterable<Iterable<Pair<IColumnType<*>, Any?>>> {
val result = QueryBuilder(true).run {
whenClauses.flatMap { it.arguments }.forEach { (column, value) ->
clauses.flatMap { it.arguments }.forEach { (column, value) ->
if (value !is Column<*> && value !is Expression<*>) {
registerArgument(column, value)
}
Expand All @@ -38,7 +38,7 @@ abstract class MergeStatement(val table: Table) : Statement<Int>(
*/
fun whenNotMatchedInsert(and: Op<Boolean>? = null, body: (InsertStatement<Int>) -> Unit) {
val arguments = InsertStatement<Int>(table).apply(body).arguments!!.first()
whenClauses.add(When(WhenAction.INSERT, arguments, and))
clauses.add(Clause(ClauseAction.INSERT, arguments, and))
}

/**
Expand All @@ -52,7 +52,7 @@ abstract class MergeStatement(val table: Table) : Statement<Int>(
*/
fun whenMatchedUpdate(and: Op<Boolean>? = null, deleteWhere: Op<Boolean>? = null, body: (UpdateStatement) -> Unit) {
val arguments = UpdateStatement(table, limit = 1).apply(body).firstDataSet
whenClauses.add(When(WhenAction.UPDATE, arguments, and, deleteWhere))
clauses.add(Clause(ClauseAction.UPDATE, arguments, and, deleteWhere))
}

/**
Expand All @@ -62,11 +62,11 @@ abstract class MergeStatement(val table: Table) : Statement<Int>(
* should be performed.
*/
fun whenMatchedDelete(and: Op<Boolean>? = null) {
whenClauses.add(When(WhenAction.DELETE, emptyList(), and))
clauses.add(Clause(ClauseAction.DELETE, emptyList(), and))
}

data class When(
val action: WhenAction,
data class Clause(
val action: ClauseAction,
val arguments: List<Pair<Column<*>, Any?>>,
val and: Op<Boolean>?,
/**
Expand All @@ -75,7 +75,7 @@ abstract class MergeStatement(val table: Table) : Statement<Int>(
val deleteWhere: Op<Boolean>? = null
)

enum class WhenAction {
enum class ClauseAction {
INSERT, UPDATE, DELETE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ open class MergeTableStatement(
private val on: Op<Boolean>?
) : MergeStatement(dest) {
override fun prepareSQL(transaction: Transaction, prepared: Boolean): String {
val result = transaction.db.dialect.functionProvider.merge(table, source, transaction, whenClauses, on)
return result
return transaction.db.dialect.functionProvider.merge(table, source, transaction, clauses, on)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -446,17 +446,17 @@ abstract class FunctionProvider {
* @param dest The table that will be modified.
* @param source The table providing the data for modification.
* @param transaction The transaction in which the operation will be executed.
* @param whenClauses A list of `MergeStatement.When` instances describing the `when` clauses of the SQL command.
* @param clauses A list of `MergeStatement.When` instances describing the `when` clauses of the SQL command.
* @param on The condition that determines whether to apply insertions or updates/deletions.
*/
open fun merge(
dest: Table,
source: Table,
transaction: Transaction,
whenClauses: List<MergeStatement.When>,
clauses: List<MergeStatement.Clause>,
on: Op<Boolean>?
): String {
if (whenClauses.any { it.deleteWhere != null } && currentDialect !is OracleDialect) {
if (clauses.any { it.deleteWhere != null } && currentDialect !is OracleDialect) {
transaction.throwUnsupportedException("'deleteWhere' parameter can be used only as a part of Oracle SQL update clause statement.")
}

Expand All @@ -477,7 +477,7 @@ abstract class FunctionProvider {
+"MERGE INTO ${transaction.identity(dest)} "
+"USING ${transaction.identity(source)} "
+"ON $onCondition "
addWhenClausesToMergeStatement(transaction, dest, whenClauses)
addClausesToMergeStatement(transaction, dest, clauses)
toString()
}
}
Expand All @@ -489,14 +489,14 @@ abstract class FunctionProvider {
* @param dest The table that will be modified.
* @param source The query providing the data for modification.
* @param transaction The transaction in which the operation will be executed.
* @param whenClauses A list of `MergeStatement.When` instances describing the `when` clauses of the SQL command.
* @param clauses A list of `MergeStatement.When` instances describing the `when` clauses of the SQL command.
* @param on The condition that determines whether to apply insertions or updates/deletions.
*/
open fun mergeSelect(
dest: Table,
source: QueryAlias,
transaction: Transaction,
whenClauses: List<MergeStatement.When>,
clauses: List<MergeStatement.Clause>,
on: Op<Boolean>,
prepared: Boolean
): String {
Expand All @@ -508,7 +508,7 @@ abstract class FunctionProvider {
+"MERGE INTO ${transaction.identity(dest)} "
+"USING ( $using ) ${if (currentDialect is OracleDialect) "" else "as"} ${source.alias} "
+"ON $onRaw "
addWhenClausesToMergeStatement(transaction, dest, whenClauses)
addClausesToMergeStatement(transaction, dest, clauses)
toString()
}
}
Expand Down Expand Up @@ -840,7 +840,7 @@ abstract class FunctionProvider {
}

@Suppress("NestedBlockDepth")
private fun QueryBuilder.addWhenClausesToMergeStatement(transaction: Transaction, table: Table, whenClauses: List<MergeStatement.When>) {
private fun QueryBuilder.addClausesToMergeStatement(transaction: Transaction, table: Table, clauses: List<MergeStatement.Clause>) {
fun QueryBuilder.appendValueAlias(column: Column<*>, value: Any?) {
when (value) {
is Column<*> -> {
Expand All @@ -859,9 +859,9 @@ private fun QueryBuilder.addWhenClausesToMergeStatement(transaction: Transaction

val autoIncColumn = table.autoIncColumn

whenClauses.forEach { (action, arguments, condition, deleteWhere) ->
clauses.forEach { (action, arguments, condition, deleteWhere) ->
when (action) {
MergeStatement.WhenAction.INSERT -> {
MergeStatement.ClauseAction.INSERT -> {
val nextValExpression = autoIncColumn?.autoIncColumnType?.nextValExpression?.takeIf { autoIncColumn !in arguments.map { it.first } }

val extraArg = if (nextValExpression != null) listOf(autoIncColumn to nextValExpression) else emptyList()
Expand All @@ -883,7 +883,7 @@ private fun QueryBuilder.addWhenClausesToMergeStatement(transaction: Transaction
}
}

MergeStatement.WhenAction.UPDATE -> {
MergeStatement.ClauseAction.UPDATE -> {
+"WHEN MATCHED "
if (currentDialect !is OracleDialect) {
condition?.let { append("AND ($condition) ") }
Expand All @@ -901,7 +901,7 @@ private fun QueryBuilder.addWhenClausesToMergeStatement(transaction: Transaction
}
}

MergeStatement.WhenAction.DELETE -> {
MergeStatement.ClauseAction.DELETE -> {
+"WHEN MATCHED "
condition?.let { append("AND ($condition) ") }
+"THEN DELETE "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package org.jetbrains.exposed.sql.vendors
import org.jetbrains.exposed.exceptions.throwUnsupportedException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.MergeStatement
import org.jetbrains.exposed.sql.statements.MergeStatement.WhenAction.DELETE
import org.jetbrains.exposed.sql.statements.MergeStatement.WhenAction.INSERT
import org.jetbrains.exposed.sql.statements.MergeStatement.WhenAction.UPDATE
import org.jetbrains.exposed.sql.statements.MergeStatement.ClauseAction.DELETE
import org.jetbrains.exposed.sql.statements.MergeStatement.ClauseAction.INSERT
import org.jetbrains.exposed.sql.statements.MergeStatement.ClauseAction.UPDATE
import org.jetbrains.exposed.sql.transactions.TransactionManager
import java.sql.DatabaseMetaData
import java.util.*
Expand Down Expand Up @@ -311,31 +311,31 @@ internal object OracleFunctionProvider : FunctionProvider() {
)
}

override fun merge(dest: Table, source: Table, transaction: Transaction, whenClauses: List<MergeStatement.When>, on: Op<Boolean>?): String {
validateMergeCommandWhenClauses(transaction, whenClauses)
return super.merge(dest, source, transaction, whenClauses, on)
override fun merge(dest: Table, source: Table, transaction: Transaction, clauses: List<MergeStatement.Clause>, on: Op<Boolean>?): String {
validateMergeCommandClauses(transaction, clauses)
return super.merge(dest, source, transaction, clauses, on)
}

override fun mergeSelect(
dest: Table,
source: QueryAlias,
transaction: Transaction,
whenClauses: List<MergeStatement.When>,
clauses: List<MergeStatement.Clause>,
on: Op<Boolean>,
prepared: Boolean
): String {
validateMergeCommandWhenClauses(transaction, whenClauses)
return super.mergeSelect(dest, source, transaction, whenClauses, on, prepared)
validateMergeCommandClauses(transaction, clauses)
return super.mergeSelect(dest, source, transaction, clauses, on, prepared)
}
}

private fun validateMergeCommandWhenClauses(transaction: Transaction, whenClauses: List<MergeStatement.When>) {
private fun validateMergeCommandClauses(transaction: Transaction, clauses: List<MergeStatement.Clause>) {
when {
whenClauses.count { it.action == INSERT } > 1 ->
clauses.count { it.action == INSERT } > 1 ->
transaction.throwUnsupportedException("Multiple insert clauses are not supported by DB.")
whenClauses.count { it.action == UPDATE } > 1 ->
clauses.count { it.action == UPDATE } > 1 ->
transaction.throwUnsupportedException("Multiple update clauses are not supported by DB.")
whenClauses.count { it.action == DELETE } > 0 ->
clauses.count { it.action == DELETE } > 0 ->
transaction.throwUnsupportedException("Delete clauses are not supported by DB. You must use 'delete where' inside 'update' clause")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package org.jetbrains.exposed.sql.vendors
import org.jetbrains.exposed.exceptions.throwUnsupportedException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.MergeStatement
import org.jetbrains.exposed.sql.statements.MergeStatement.WhenAction.DELETE
import org.jetbrains.exposed.sql.statements.MergeStatement.WhenAction.INSERT
import org.jetbrains.exposed.sql.statements.MergeStatement.WhenAction.UPDATE
import org.jetbrains.exposed.sql.statements.MergeStatement.ClauseAction.DELETE
import org.jetbrains.exposed.sql.statements.MergeStatement.ClauseAction.INSERT
import org.jetbrains.exposed.sql.statements.MergeStatement.ClauseAction.UPDATE
import org.jetbrains.exposed.sql.transactions.TransactionManager
import java.util.*

Expand Down Expand Up @@ -251,31 +251,31 @@ internal object SQLServerFunctionProvider : FunctionProvider() {
)
}

override fun merge(dest: Table, source: Table, transaction: Transaction, whenClauses: List<MergeStatement.When>, on: Op<Boolean>?): String {
validateMergeCommandWhenClauses(transaction, whenClauses)
return super.merge(dest, source, transaction, whenClauses, on) + ";"
override fun merge(dest: Table, source: Table, transaction: Transaction, clauses: List<MergeStatement.Clause>, on: Op<Boolean>?): String {
validateMergeCommandClauses(transaction, clauses)
return super.merge(dest, source, transaction, clauses, on) + ";"
}

override fun mergeSelect(
dest: Table,
source: QueryAlias,
transaction: Transaction,
whenClauses: List<MergeStatement.When>,
clauses: List<MergeStatement.Clause>,
on: Op<Boolean>,
prepared: Boolean
): String {
validateMergeCommandWhenClauses(transaction, whenClauses)
return super.mergeSelect(dest, source, transaction, whenClauses, on, prepared) + ";"
validateMergeCommandClauses(transaction, clauses)
return super.mergeSelect(dest, source, transaction, clauses, on, prepared) + ";"
}
}

private fun validateMergeCommandWhenClauses(transaction: Transaction, whenClauses: List<MergeStatement.When>) {
private fun validateMergeCommandClauses(transaction: Transaction, clauses: List<MergeStatement.Clause>) {
when {
whenClauses.count { it.action == INSERT } > 1 ->
clauses.count { it.action == INSERT } > 1 ->
transaction.throwUnsupportedException("Multiple insert clauses are not supported by DB")
whenClauses.count { it.action == UPDATE } > 1 ->
clauses.count { it.action == UPDATE } > 1 ->
transaction.throwUnsupportedException("Multiple update clauses are not supported by DB")
whenClauses.count { it.action == DELETE } > 1 ->
clauses.count { it.action == DELETE } > 1 ->
transaction.throwUnsupportedException("Multiple delete clauses are not supported by DB")
}
}
Expand Down

0 comments on commit 10194a6

Please sign in to comment.