Skip to content

Commit

Permalink
reset
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Sep 10, 2020
1 parent 0f67a6b commit b0e81a3
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions gorm/sqlx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gorm

import (
"strings"

"github.com/jinzhu/gorm"
)

// @desc 批量保存 如果唯一索引重复则更新,唯一索引不重复或者不存在唯一索引则插入
// @auth liuguoqiang 2020-04-21
// @param
// @return
func BulkSave(db *gorm.DB, table string, fields []string, params []map[string]interface{}) error {
sql := "INSERT INTO `" + table + "` (" + strings.Join(fields, ",") + ") VALUES "
updateArr := make([]string, 0)
args := make([]interface{}, 0)
valueArr := make([]string, 0)
varArr := make([]string, 0)
for _, value := range fields {
updateArr = append(updateArr, value+"=VALUES("+value+")")
}
for _, obj := range params {
varArr = varArr[:0]
varStr := "("
for _, value := range fields {
varArr = append(varArr, "?")
args = append(args, obj[value])
}
varStr += strings.Join(varArr, ",") + ")"
valueArr = append(valueArr, varStr)
}
sql += strings.Join(valueArr, ",")
sql += " ON DUPLICATE KEY UPDATE " + strings.Join(updateArr, ",")
err := db.Exec(sql, args...).Error
return err
}

0 comments on commit b0e81a3

Please sign in to comment.