Skip to content

Commit

Permalink
Merge pull request #1319 from atzedus/upsert-more-convenion-naming
Browse files Browse the repository at this point in the history
UpsertConflictObject -> UpsertConflictTarget+docs
  • Loading branch information
stephenafamo authored Oct 18, 2023
2 parents 849adb0 + cf047cd commit 6210ed1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,18 @@ p1.Name = "Hogan"
// INSERT INTO pilots ("id", "name") VALUES ($1, $2)
// ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name"
err := p1.Upsert(ctx, db, true, []string{"id"}, boil.Whitelist("name"), boil.Whitelist("id", "name"))

// Custom conflict_target expression:
// INSERT INTO pilots ("id", "name") VALUES (9, 'Antwerp Design')
// ON CONFLICT ON CONSTRAINT pilots_pkey DO NOTHING;
conflictTarget := models.UpsertConflictTarget
err := p1.Upsert(ctx, db, false, nil, boil.Whitelist("id", "name"), boil.None(), conflictTarget("ON CONSTRAINT pilots_pkey"))

// Custom UPDATE SET expression:
// INSERT INTO pilots ("id", "name") VALUES (9, 'Antwerp Design')
// ON CONFLICT ("id") DO UPDATE SET (id, name) = (sub-SELECT)
updateSet := models.UpsertUpdateSet
err := p1.Upsert(ctx, db, true, []string{"id"}, boil.Whitelist("id", "name"), boil.None(), updateSet("(id, name) = (sub-SELECT)"))
```

* **Postgres**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
type UpsertOptions struct {
conflictObject string
conflictTarget string
updateSet string
}

type UpsertOptionFunc func(o *UpsertOptions)

func UpsertConflictObject(conflictObject string) UpsertOptionFunc {
func UpsertConflictTarget(conflictTarget string) UpsertOptionFunc {
return func(o *UpsertOptions) {
o.conflictObject = conflictObject
o.conflictTarget = conflictTarget
}
}

Expand Down Expand Up @@ -45,8 +45,8 @@ func buildUpsertQueryPostgres(dia drivers.Dialect, tableName string, updateOnCon
columns,
)

if upsertOpts.conflictObject != "" {
buf.WriteString(upsertOpts.conflictObject)
if upsertOpts.conflictTarget != "" {
buf.WriteString(upsertOpts.conflictTarget)
} else if len(conflict) != 0 {
buf.WriteByte('(')
buf.WriteString(strings.Join(conflict, ", "))
Expand Down

0 comments on commit 6210ed1

Please sign in to comment.