+
Skip to content

allow pg upsert specify conflict target column #181

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

Merged
merged 1 commit into from
Jul 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import me.liuwj.ktorm.expression.ColumnExpression
import me.liuwj.ktorm.expression.SqlExpression
import me.liuwj.ktorm.expression.TableExpression
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column

/**
* Insert or update expression, represents an insert statement with an
Expand Down Expand Up @@ -87,7 +88,8 @@ fun <T : BaseTable<*>> Database.insertOrUpdate(table: T, block: InsertOrUpdateSt
val expression = InsertOrUpdateExpression(
table = table.asExpression(),
assignments = assignments,
conflictTarget = primaryKeys.map { it.asExpression() },
conflictTarget = builder.conflictColumns.map { it.asExpression() }.takeIf { it.isNotEmpty() }
?: primaryKeys.map { it.asExpression() },
updateAssignments = builder.updateAssignments
)

Expand All @@ -103,11 +105,13 @@ class InsertOrUpdateStatementBuilder(
) : AssignmentsBuilder(assignments) {

internal val updateAssignments = ArrayList<ColumnAssignmentExpression<*>>()
internal val conflictColumns = ArrayList<Column<*>>()

/**
* Specify the update assignments while any key conflict exists.
*/
fun onDuplicateKey(block: AssignmentsBuilder.() -> Unit) {
fun onDuplicateKey(vararg columns: Column<*>, block: AssignmentsBuilder.() -> Unit) {
conflictColumns += columns
val assignments = ArrayList<ColumnAssignmentExpression<*>>()
AssignmentsBuilder(assignments).apply(block)
updateAssignments += assignments
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载