-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: EXPOSED-45 Support single statement UPSERT (#1743)
* Add functionality for insert or update (upsert) command in all dialects. MySQL, PostgreSQL, and SQLite use their own insert syntax, while the other dialects use the standard merge syntax. The implementation accepts optional user-defined conflict key columns, as well as a single update where clause. It also allows update expressions to be provided if they need to be different from the provided insert values. Column expressions, defaults, and subqueries are also enabled if the individual dialect allows them. * Extract excluding H2 v1 condition check function * Add KDocs to UpsertStatement class
- Loading branch information
Showing
10 changed files
with
696 additions
and
0 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
33 changes: 33 additions & 0 deletions
33
exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/UpsertStatement.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,33 @@ | ||
package org.jetbrains.exposed.sql.statements | ||
|
||
import org.jetbrains.exposed.sql.* | ||
import org.jetbrains.exposed.sql.vendors.* | ||
|
||
/** | ||
* Represents the SQL command that either inserts a new row into a table, or updates the existing row if insertion would violate a unique constraint. | ||
* | ||
* @param table Table to either insert values into or update values from. | ||
* @param keys (optional) Columns to include in the condition that determines a unique constraint match. If no columns are provided, | ||
* primary keys will be used. If the table does not have any primary keys, the first unique index will be attempted. | ||
* @param onUpdate List of pairs of specific columns to update and the expressions to update them with. | ||
* If left null, all columns will be updated with the values provided for the insert. | ||
* @param where Condition that determines which rows to update, if a unique violation is found. This clause may not be supported by all vendors. | ||
*/ | ||
open class UpsertStatement<Key : Any>( | ||
table: Table, | ||
vararg val keys: Column<*>, | ||
val onUpdate: List<Pair<Column<*>, Expression<*>>>?, | ||
val where: Op<Boolean>? | ||
) : InsertStatement<Key>(table) { | ||
|
||
override fun prepareSQL(transaction: Transaction): String { | ||
val functionProvider = when (val dialect = transaction.db.dialect) { | ||
is H2Dialect -> when (dialect.h2Mode) { | ||
H2Dialect.H2CompatibilityMode.MariaDB, H2Dialect.H2CompatibilityMode.MySQL -> MysqlFunctionProvider() | ||
else -> H2FunctionProvider | ||
} | ||
else -> dialect.functionProvider | ||
} | ||
return functionProvider.upsert(table, arguments!!.first(), onUpdate, where, transaction, *keys) | ||
} | ||
} |
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
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
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
Oops, something went wrong.