-
Notifications
You must be signed in to change notification settings - Fork 695
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
feat: EXPOSED-52 Support batch UPSERT #1749
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 42 additions & 0 deletions
42
exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/BatchUpsertStatement.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,42 @@ | ||
package org.jetbrains.exposed.sql.statements | ||
|
||
import org.jetbrains.exposed.sql.Column | ||
import org.jetbrains.exposed.sql.Expression | ||
import org.jetbrains.exposed.sql.Table | ||
import org.jetbrains.exposed.sql.Transaction | ||
import org.jetbrains.exposed.sql.vendors.H2Dialect | ||
import org.jetbrains.exposed.sql.vendors.H2FunctionProvider | ||
import org.jetbrains.exposed.sql.vendors.MysqlFunctionProvider | ||
|
||
/** | ||
* Represents the SQL command that either batch inserts new rows into a table, or updates the existing rows if insertions violate unique constraints. | ||
* | ||
* **Note**: Unlike `UpsertStatement`, `BatchUpsertStatement` does not include a `where` parameter. Please log a feature request | ||
* on [YouTrack](https://youtrack.jetbrains.com/newIssue?project=EXPOSED&c=Type%20Feature&draftId=25-4449790) if a use-case requires inclusion of a `where` clause. | ||
* | ||
* @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 shouldReturnGeneratedValues Specifies whether newly generated values (for example, auto-incremented IDs) should be returned. | ||
* See [Batch Insert](https://github.com/JetBrains/Exposed/wiki/DSL#batch-insert) for more details. | ||
*/ | ||
open class BatchUpsertStatement( | ||
table: Table, | ||
vararg val keys: Column<*>, | ||
val onUpdate: List<Pair<Column<*>, Expression<*>>>?, | ||
shouldReturnGeneratedValues: Boolean = true | ||
) : BaseBatchInsertStatement(table, ignore = false, shouldReturnGeneratedValues) { | ||
|
||
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, null, 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Single statement
upsert()
takes a WHERE clause as an argument (to be used in update part), but this option hasn't been included inbatchUpsert()
.The rationale is that I couldn't find a use case for a batch upsert including a where that applies to all batch data (it seems to defeat the purpose?). All the PostgreSQL examples I came across used an UPDATE without WHERE (and MySQL doesn't allow WHERE for even single upsert).
I can implement its inclusion if anybody believes it should definitely still be an option. Or we can wait to see if users request it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can note this in KDoc with link to youtrack if there is a use-case