基于GORM實現(xiàn)CreateOrUpdate方法詳解
正文
CreateOrUpdate 是業(yè)務(wù)開發(fā)中很常見的場景,我們支持用戶對某個業(yè)務(wù)實體進行創(chuàng)建/配置。希望實現(xiàn)的 repository 接口要達(dá)到以下兩個要求:
- 如果此前不存在該實體,創(chuàng)建一個新的;
- 如果此前該實體已經(jīng)存在,更新相關(guān)屬性。
根據(jù)筆者的團隊合作經(jīng)驗看,很多 Golang 開發(fā)同學(xué)不是很確定對于這種場景到底怎么實現(xiàn),寫出來的代碼五花八門,還可能有并發(fā)問題。今天我們就來看看基于 GORM 怎么來實現(xiàn) CreateOrUpdate。
GORM 寫接口原理
我們先來看下 GORM 提供了那些方法來支持我們往數(shù)據(jù)庫插入數(shù)據(jù),對 GORM 比較熟悉的同學(xué)可以忽略這部分:
Create
插入一條記錄到數(shù)據(jù)庫,注意需要通過數(shù)據(jù)的指針來創(chuàng)建,回填主鍵;
// Create insert the value into database
func (db *DB) Create(value interface{}) (tx *DB) {
if db.CreateBatchSize > 0 {
return db.CreateInBatches(value, db.CreateBatchSize)
}
tx = db.getInstance()
tx.Statement.Dest = value
return tx.callbacks.Create().Execute(tx)
}
賦值 Dest 后直接進入 Create 的 callback 流程。
Save
保存所有的字段,即使字段是零值。如果我們傳入的結(jié)構(gòu)主鍵為零值,則會插入記錄。
// Save update value in database, if the value doesn't have primary key, will insert it
func (db *DB) Save(value interface{}) (tx *DB) {
tx = db.getInstance()
tx.Statement.Dest = value
reflectValue := reflect.Indirect(reflect.ValueOf(value))
for reflectValue.Kind() == reflect.Ptr || reflectValue.Kind() == reflect.Interface {
reflectValue = reflect.Indirect(reflectValue)
}
switch reflectValue.Kind() {
case reflect.Slice, reflect.Array:
if _, ok := tx.Statement.Clauses["ON CONFLICT"]; !ok {
tx = tx.Clauses(clause.OnConflict{UpdateAll: true})
}
tx = tx.callbacks.Create().Execute(tx.Set("gorm:update_track_time", true))
case reflect.Struct:
if err := tx.Statement.Parse(value); err == nil && tx.Statement.Schema != nil {
for _, pf := range tx.Statement.Schema.PrimaryFields {
if _, isZero := pf.ValueOf(tx.Statement.Context, reflectValue); isZero {
return tx.callbacks.Create().Execute(tx)
}
}
}
fallthrough
default:
selectedUpdate := len(tx.Statement.Selects) != 0
// when updating, use all fields including those zero-value fields
if !selectedUpdate {
tx.Statement.Selects = append(tx.Statement.Selects, "*")
}
tx = tx.callbacks.Update().Execute(tx)
if tx.Error == nil && tx.RowsAffected == 0 && !tx.DryRun && !selectedUpdate {
result := reflect.New(tx.Statement.Schema.ModelType).Interface()
if result := tx.Session(&Session{}).Limit(1).Find(result); result.RowsAffected == 0 {
return tx.Create(value)
}
}
}
return
}
關(guān)注點:
- 在 reflect.Struct 的分支,判斷 PrimaryFields 也就是主鍵列是否為零值,如果是,直接開始調(diào)用 Create 的 callback,這也和 Save 的說明匹配;
- switch 里面用到了 fallthrough 關(guān)鍵字,說明 switch 命中后繼續(xù)往下命中 default;
- 如果我們沒有用 Select() 方法指定需要更新的字段,則默認(rèn)是全部更新,包含所有零值字段,這里用的通配符 *
- 如果主鍵不為零值,說明記錄已經(jīng)存在,這個時候就會去更新。
事實上有一些業(yè)務(wù)場景下,我們可以用 Save 來實現(xiàn) CreateOrUpdate 的語義:
- 首次調(diào)用時主鍵ID為空,這時 Save 會走到 Create 分支去插入數(shù)據(jù)。
- 隨后調(diào)用時存在主鍵ID,觸發(fā)更新邏輯。
但 Save 本身語義其實比較混亂,不太建議使用,把這部分留給業(yè)務(wù)自己實現(xiàn),用Updates,Create用起來更明確些。
Update & Updates
Update 前者更新單個列。
Updates 更新多列,且當(dāng)使用 struct 更新時,默認(rèn)情況下,GORM 只會更新非零值的字段(可以用 Select 指定來解這個問題)。使用 map 更新時則會全部更新。
// Update update attributes with callbacks, refer: https://gorm.io/docs/update.html#Update-Changed-Fields
func (db *DB) Update(column string, value interface{}) (tx *DB) {
tx = db.getInstance()
tx.Statement.Dest = map[string]interface{}{column: value}
return tx.callbacks.Update().Execute(tx)
}
// Updates update attributes with callbacks, refer: https://gorm.io/docs/update.html#Update-Changed-Fields
func (db *DB) Updates(values interface{}) (tx *DB) {
tx = db.getInstance()
tx.Statement.Dest = values
return tx.callbacks.Update().Execute(tx)
}
這里也能從實現(xiàn)中看出來一些端倪。Update 接口內(nèi)部是封裝了一個 map[string]interface{},而 Updates 則是可以接受 map 也可以走 struct,最終寫入 Dest。
FirstOrInit
獲取第一條匹配的記錄,或者根據(jù)給定的條件初始化一個實例(僅支持 struct 和 map)
// FirstOrInit gets the first matched record or initialize a new instance with given conditions (only works with struct or map conditions)
func (db *DB) FirstOrInit(dest interface{}, conds ...interface{}) (tx *DB) {
queryTx := db.Limit(1).Order(clause.OrderByColumn{
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
})
if tx = queryTx.Find(dest, conds...); tx.RowsAffected == 0 {
if c, ok := tx.Statement.Clauses["WHERE"]; ok {
if where, ok := c.Expression.(clause.Where); ok {
tx.assignInterfacesToValue(where.Exprs)
}
}
// initialize with attrs, conds
if len(tx.Statement.attrs) > 0 {
tx.assignInterfacesToValue(tx.Statement.attrs...)
}
}
// initialize with attrs, conds
if len(tx.Statement.assigns) > 0 {
tx.assignInterfacesToValue(tx.Statement.assigns...)
}
return
}
注意,Init 和 Create 的區(qū)別,如果沒有找到,這里會把實例給初始化,不會存入 DB,可以看到 RowsAffected == 0 分支的處理,這里并不會走 Create 的 callback 函數(shù)。這里的定位是一個純粹的讀接口。
FirstOrCreate
獲取第一條匹配的記錄,或者根據(jù)給定的條件創(chuàng)建一條新紀(jì)錄(僅支持 struct 和 map 條件)。FirstOrCreate可能會執(zhí)行兩條sql,他們是一個事務(wù)中的。
// FirstOrCreate gets the first matched record or create a new one with given conditions (only works with struct, map conditions)
func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) {
tx = db.getInstance()
queryTx := db.Session(&Session{}).Limit(1).Order(clause.OrderByColumn{
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
})
if result := queryTx.Find(dest, conds...); result.Error == nil {
if result.RowsAffected == 0 {
if c, ok := result.Statement.Clauses["WHERE"]; ok {
if where, ok := c.Expression.(clause.Where); ok {
result.assignInterfacesToValue(where.Exprs)
}
}
// initialize with attrs, conds
if len(db.Statement.attrs) > 0 {
result.assignInterfacesToValue(db.Statement.attrs...)
}
// initialize with attrs, conds
if len(db.Statement.assigns) > 0 {
result.assignInterfacesToValue(db.Statement.assigns...)
}
return tx.Create(dest)
} else if len(db.Statement.assigns) > 0 {
exprs := tx.Statement.BuildCondition(db.Statement.assigns[0], db.Statement.assigns[1:]...)
assigns := map[string]interface{}{}
for _, expr := range exprs {
if eq, ok := expr.(clause.Eq); ok {
switch column := eq.Column.(type) {
case string:
assigns[column] = eq.Value
case clause.Column:
assigns[column.Name] = eq.Value
default:
}
}
}
return tx.Model(dest).Updates(assigns)
}
} else {
tx.Error = result.Error
}
return tx
}
注意區(qū)別,同樣是構(gòu)造 queryTx 去調(diào)用 Find 方法查詢,后續(xù)的處理很關(guān)鍵:
- 若沒有查到結(jié)果,將 where 條件,Attrs() 以及 Assign() 方法賦值的屬性寫入對象,從源碼可以看到是通過三次 assignInterfacesToValue 實現(xiàn)的。屬性更新后,調(diào)用 Create 方法往數(shù)據(jù)庫中插入;
- 若查到了結(jié)果,但 Assign() 此前已經(jīng)寫入了一些屬性,就將其寫入對象,進行 Updates 調(diào)用。
第一個分支好理解,需要插入新數(shù)據(jù)。重點在于 else if len(db.Statement.assigns) > 0 分支。
我們調(diào)用 FirstOrCreate 時,需要傳入一個對象,再傳入一批條件,這批條件會作為 Where 語句的部分在一開始進行查詢。而這個函數(shù)同時可以配合 Assign() 使用,這一點就賦予了生命力。
不管是否找到記錄,Assign 都會將屬性賦值給 struct,并將結(jié)果寫回數(shù)據(jù)庫。
方案一:FirstOrCreate + Assign
func (db *DB) Attrs(attrs ...interface{}) (tx *DB) {
tx = db.getInstance()
tx.Statement.attrs = attrs
return
}
func (db *DB) Assign(attrs ...interface{}) (tx *DB) {
tx = db.getInstance()
tx.Statement.assigns = attrs
return
}
這種方式充分利用了 Assign 的能力。我們在上面 FirstOrCreate 的分析中可以看出,這里是會將 Assign 進來的屬性應(yīng)用到 struct 上,寫入數(shù)據(jù)庫的。區(qū)別只在于是插入(Insert)還是更新(Update)。
// 未找到 user,根據(jù)條件和 Assign 屬性創(chuàng)建記錄
db.Where(User{Name: "non_existing"}).Assign(User{Age: 20}).FirstOrCreate(&user)
// SELECT * FROM users WHERE name = 'non_existing' ORDER BY id LIMIT 1;
// INSERT INTO "users" (name, age) VALUES ("non_existing", 20);
// user -> User{ID: 112, Name: "non_existing", Age: 20}
// 找到了 `name` = `jinzhu` 的 user,依然會根據(jù) Assign 更新記錄
db.Where(User{Name: "jinzhu"}).Assign(User{Age: 20}).FirstOrCreate(&user)
// SELECT * FROM users WHERE name = 'jinzhu' ORDER BY id LIMIT 1;
// UPDATE users SET age=20 WHERE id = 111;
// user -> User{ID: 111, Name: "jinzhu", Age: 20}
所以,要實現(xiàn) CreateOrUpdate,我們可以將需要 Update 的屬性通過 Assign 函數(shù)放進來,隨后如果通過 Where 找到了記錄,也會將 Assign 屬性應(yīng)用上,隨后 Update。
這樣的思路一定是可以跑通的,但使用之前要看場景。
為什么?
因為參看上面源碼我們就知道,F(xiàn)irstOrCreate 本質(zhì)是 Select + Insert 或者 Select + Update。
無論怎樣,都是兩條 SQL,可能有并發(fā)安全問題。如果你的業(yè)務(wù)場景不存在并發(fā),可以放心用 FirstOrCreate + Assign,功能更多,適配更多場景。
而如果可能有并發(fā)安全的坑,我們就要考慮方案二:Upsert。
方案二:Upsert
鑒于 MySQL 提供了 ON DUPLICATE KEY UPDATE 的能力,我們可以充分利用唯一鍵的約束,來搞定并發(fā)場景下的 CreateOrUpdate。
import "gorm.io/gorm/clause"
// 不處理沖突
DB.Clauses(clause.OnConflict{DoNothing: true}).Create(&user)
// `id` 沖突時,將字段值更新為默認(rèn)值
DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.Assignments(map[string]interface{}{"role": "user"}),
}).Create(&users)
// MERGE INTO "users" USING *** WHEN NOT MATCHED THEN INSERT *** WHEN MATCHED THEN UPDATE SET ***; SQL Server
// INSERT INTO `users` *** ON DUPLICATE KEY UPDATE ***; MySQL
// Update columns to new value on `id` conflict
DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.AssignmentColumns([]string{"name", "age"}),
}).Create(&users)
// MERGE INTO "users" USING *** WHEN NOT MATCHED THEN INSERT *** WHEN MATCHED THEN UPDATE SET "name"="excluded"."name"; SQL Server
// INSERT INTO "users" *** ON CONFLICT ("id") DO UPDATE SET "name"="excluded"."name", "age"="excluded"."age"; PostgreSQL
// INSERT INTO `users` *** ON DUPLICATE KEY UPDATE `name`=VALUES(name),`age=VALUES(age); MySQL
這里依賴了 GORM 的 Clauses 方法,我們來看一下:
type Interface interface {
Name() string
Build(Builder)
MergeClause(*Clause)
}
// AddClause add clause
func (stmt *Statement) AddClause(v clause.Interface) {
if optimizer, ok := v.(StatementModifier); ok {
optimizer.ModifyStatement(stmt)
} else {
name := v.Name()
c := stmt.Clauses[name]
c.Name = name
v.MergeClause(&c)
stmt.Clauses[name] = c
}
}
這里添加進來一個 Clause 之后,會調(diào)用 MergeClause 將語句進行合并,而 OnConflict 的適配是這樣:
package clause
type OnConflict struct {
Columns []Column
Where Where
TargetWhere 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.TargetWhere.Exprs) > 0 {
builder.WriteString(" WHERE ")
onConflict.TargetWhere.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)
}
if len(onConflict.Where.Exprs) > 0 {
builder.WriteString(" WHERE ")
onConflict.Where.Build(builder)
builder.WriteByte(' ')
}
}
// MergeClause merge onConflict clauses
func (onConflict OnConflict) MergeClause(clause *Clause) {
clause.Expression = onConflict
}
初階的用法中,我們只需要關(guān)注三個屬性:
- DoNothing:沖突后不處理,參照上面的 Build 實現(xiàn)可以看到,這里只會加入 DO NOTHING;
- DoUpdates: 配置一批需要賦值的 KV,如果沒有指定 DoNothing,會根據(jù)這一批 Assignment 來寫入要更新的列和值;
type Set []Assignment
type Assignment struct {
Column Column
Value interface{}
}
- UpdateAll: 沖突后更新所有的值(非 default tag字段)。
需要注意的是,所謂 OnConflict,并不一定是主鍵沖突,唯一鍵也包含在內(nèi)。所以,使用 OnConflict 這套 Upsert 的先決條件是【唯一索引】或【主鍵】都可以。生成一條SQL語句,并發(fā)安全。
如果沒有唯一索引的限制,我們就無法復(fù)用這個能力,需要考慮別的解法。如果
總結(jié)
- 若你的 CreateOrUpdate 能用到【唯一索引】或【主鍵】,建議使用方案二,這也是作者金柱大佬最推薦的方案,并發(fā)安全;
- 若無法用【唯一索引】來限制,需要用其他列來判斷,且不關(guān)注并發(fā)安全,可以采用方案一;
- 若只需要按照【主鍵】是否為零值來實現(xiàn) CreateOrUpdate,可以使用 Save(接口語義不是特別明確,用的時候小心,如果可以,盡量用 Create/Update)。
以上就是基于GORM實現(xiàn)CreateOrUpdate方法詳解的詳細(xì)內(nèi)容,更多關(guān)于GORM CreateOrUpdate方法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang中的Slice與數(shù)組及區(qū)別詳解
數(shù)組是一種具有固定長度的基本數(shù)據(jù)結(jié)構(gòu),在golang中與C語言一樣數(shù)組一旦創(chuàng)建了它的長度就不允許改變,數(shù)組的空余位置用0填補,不允許數(shù)組越界。今天小編通過實例代碼操作給大家詳細(xì)介紹lang中的Slice與數(shù)組的相關(guān)知識,一起看看吧2020-02-02
go?time.Sleep睡眠指定時間實例詳解(小時級到納秒級)
golang的休眠可以使用time包中的sleep,下面這篇文章主要給大家介紹了關(guān)于go?time.Sleep睡眠指定時間(小時級到納秒級)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11

