-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
32 lines (29 loc) · 832 Bytes
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package edb
import (
"fmt"
"github.com/jinzhu/gorm"
"strings"
)
func BatchUpdate(db *gorm.DB, batch []DbMessage, tName string, values ...string) error {
if len(batch) == 0 {
return nil
}
var vStr []string
for _, v := range values {
vStr = append(vStr, fmt.Sprintf("%s=VALUES(%s)", v, v))
}
sql, vs := batchInsertSql(batch, tName, false)
sql += "ON DUPLICATE KEY UPDATE " + strings.Join(vStr, ",")
if err := db.Exec(sql, vs...).Error; err != nil {
return err
}
return nil
}
func Update(db *gorm.DB, msg DbMessage, values ...string) error {
var vStr []string
for _, v := range values {
vStr = append(vStr, fmt.Sprintf("%s=VALUES(%s)", v, v))
}
sql := InsertPrefix + msg.TableName() + msg.Column() + OrmValue + "ON DUPLICATE KEY UPDATE " + strings.Join(vStr, ",")
return db.Exec(sql, msg.Values()).Error
}