本篇文章給大家分享的是有關(guān)gorm中OnConflict的作用是什么,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供大城網(wǎng)站建設、大城做網(wǎng)站、大城網(wǎng)站設計、大城網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、大城企業(yè)網(wǎng)站模板建站服務,十余年大城做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
gorm.io/gorm@v1.20.11/clause/on_conflict.go
type OnConflict struct { Columns []Column Where Where OnConstraint string DoNothing bool DoUpdates Set UpdateAll bool } func (OnConflict) Name() string { return "ON CONFLICT" } // Build build onConflict clause func (onConflict OnConflict) Build(builder Builder) { if len(onConflict.Columns) > 0 { builder.WriteByte('(') for idx, column := range onConflict.Columns { if idx > 0 { builder.WriteByte(',') } builder.WriteQuoted(column) } builder.WriteString(`) `) } if len(onConflict.Where.Exprs) > 0 { builder.WriteString("WHERE ") onConflict.Where.Build(builder) builder.WriteByte(' ') } if onConflict.OnConstraint != "" { builder.WriteString("ON CONSTRAINT ") builder.WriteString(onConflict.OnConstraint) builder.WriteByte(' ') } if onConflict.DoNothing { builder.WriteString("DO NOTHING") } else { builder.WriteString("DO UPDATE SET ") onConflict.DoUpdates.Build(builder) } } // MergeClause merge onConflict clauses func (onConflict OnConflict) MergeClause(clause *Clause) { clause.Expression = onConflict }
OnConflict定義了Columns、Where、OnConstraint、DoNothing、DoUpdates、UpdateAll屬性;Build方法會根據(jù)這些屬性拼裝sql,如果是DoNothing則追加
DO NOTHING
,否則追加DO UPDATE SET
gorm.io/gorm@v1.20.11/clause/set.go
type Set []Assignment type Assignment struct { Column Column Value interface{} } func (set Set) Name() string { return "SET" } func (set Set) Build(builder Builder) { if len(set) > 0 { for idx, assignment := range set { if idx > 0 { builder.WriteByte(',') } builder.WriteQuoted(assignment.Column) builder.WriteByte('=') builder.AddVar(builder, assignment.Value) } } else { builder.WriteQuoted(PrimaryColumn) builder.WriteByte('=') builder.WriteQuoted(PrimaryColumn) } } // MergeClause merge assignments clauses func (set Set) MergeClause(clause *Clause) { copiedAssignments := make([]Assignment, len(set)) copy(copiedAssignments, set) clause.Expression = Set(copiedAssignments) } func Assignments(values map[string]interface{}) Set { keys := make([]string, 0, len(values)) for key := range values { keys = append(keys, key) } sort.Strings(keys) assignments := make([]Assignment, len(keys)) for idx, key := range keys { assignments[idx] = Assignment{Column: Column{Name: key}, Value: values[key]} } return assignments } func AssignmentColumns(values []string) Set { assignments := make([]Assignment, len(values)) for idx, value := range values { assignments[idx] = Assignment{Column: Column{Name: value}, Value: Column{Table: "excluded", Name: value}} } return assignments }
的DoUpdates屬性是Set類型,Set類型實際是Assignment數(shù)組;其Build方法會組裝assignment的sql
func onConflictDemo(db *gorm.DB) { entities := []DemoEntity{ { Model: gorm.Model{ID: 1}, Name: "coco", }, { Model: gorm.Model{ID: 2}, Name: "bear", }, } result := db.Debug().Create(&entities) b, _ := json.Marshal(entities) log.Println("data:", string(b)) log.Println("result.RowsAffected:", result.RowsAffected, "result.Error:", result.Error) if err := db.Debug().Clauses(clause.OnConflict{DoNothing: true}).Create(&entities).Error; err != nil { panic(err) } }
輸出
2021/01/17 20:03:31 /demo.go:53 UNIQUE constraint failed: demo_entities.id [0.487ms] [rows:0] INSERT INTO `demo_entities` (`created_at`,`updated_at`,`deleted_at`,`name`,`id`) VALUES ("2021-01-17 20:03:31.711","2021-01-17 20:03:31.711",NULL,"coco",1),("2021-01-17 20:03:31.711","2021-01-17 20:03:31.711",NULL,"bear",2) 2021/01/17 20:03:31 data: [{"ID":1,"CreatedAt":"2021-01-17T20:03:31.71143+08:00","UpdatedAt":"2021-01-17T20:03:31.71143+08:00","DeletedAt":null,"Name":"coco"},{"ID":2,"CreatedAt":"2021-01-17T20:03:31.71143+08:00","UpdatedAt":"2021-01-17T20:03:31.71143+08:00","DeletedAt":null,"Name":"bear"}] 2021/01/17 20:03:31 result.RowsAffected: 0 result.Error: UNIQUE constraint failed: demo_entities.id 2021/01/17 20:03:31 /demo.go:58 [0.123ms] [rows:0] INSERT INTO `demo_entities` (`created_at`,`updated_at`,`deleted_at`,`name`,`id`) VALUES ("2021-01-17 20:03:31.711","2021-01-17 20:03:31.711",NULL,"coco",1),("2021-01-17 20:03:31.711","2021-01-17 20:03:31.711",NULL,"bear",2) ON CONFLICT DO NOTHING
gorm的OnConflict定義了Columns、Where、OnConstraint、DoNothing、DoUpdates、UpdateAll屬性;Build方法會根據(jù)這些屬性拼裝sql,如果是DoNothing則追加DO NOTHING
,否則追加DO UPDATE SET
。
以上就是gorm中OnConflict的作用是什么,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。