github.com/kunlun-qilian/sqlx/v3@v3.0.0/builder/addition_on_conflict.go (about) 1 package builder 2 3 import ( 4 "context" 5 ) 6 7 type OnConflictAddition struct { 8 } 9 10 func (OnConflictAddition) AdditionType() AdditionType { 11 return AdditionOnConflict 12 } 13 14 func OnConflict(columns *Columns) *onConflict { 15 return &onConflict{ 16 columns: columns, 17 } 18 } 19 20 type onConflict struct { 21 OnConflictAddition 22 23 columns *Columns 24 doNothing bool 25 assignments []*Assignment 26 } 27 28 func (o onConflict) DoNothing() *onConflict { 29 o.doNothing = true 30 return &o 31 } 32 33 func (o onConflict) DoUpdateSet(assignments ...*Assignment) *onConflict { 34 o.assignments = assignments 35 return &o 36 } 37 38 func (o *onConflict) IsNil() bool { 39 return o == nil || IsNilExpr(o.columns) || (!o.doNothing && len(o.assignments) == 0) 40 } 41 42 func (o *onConflict) Ex(ctx context.Context) *Ex { 43 e := Expr("ON CONFLICT ") 44 45 e.WriteGroup(func(e *Ex) { 46 e.WriteExpr(o.columns) 47 }) 48 49 e.WriteQuery(" DO ") 50 51 if o.doNothing { 52 e.WriteQuery("NOTHING") 53 } else { 54 e.WriteQuery("UPDATE SET ") 55 for i := range o.assignments { 56 if i > 0 { 57 e.WriteQuery(", ") 58 } 59 e.WriteExpr(o.assignments[i]) 60 } 61 } 62 63 return e.Ex(ctx) 64 }