wangchuanjin 5 年之前
父节点
当前提交
88c5548167
共有 1 个文件被更改,包括 14 次插入4 次删除
  1. 14 4
      common/src/qfw/util/mysql/mysql.go

+ 14 - 4
common/src/qfw/util/mysql/mysql.go

@@ -95,7 +95,7 @@ func (m *Mysql) InsertIgnoreBatch(tableName string, fields []string, values []in
 
 //带事务的批量新增
 func (m *Mysql) InsertIgnoreBatchByTx(tx *sql.Tx, tableName string, fields []string, values []interface{}) (int64, int64) {
-	return m.insertBatchByTx(tx, "IGNORE", tableName, fields, values)
+	return m.insertOrReplaceBatchByTx(tx, "INSERT", "IGNORE", tableName, fields, values)
 }
 
 //批量新增
@@ -105,10 +105,20 @@ func (m *Mysql) InsertBatch(tableName string, fields []string, values []interfac
 
 //带事务的批量新增
 func (m *Mysql) InsertBatchByTx(tx *sql.Tx, tableName string, fields []string, values []interface{}) (int64, int64) {
-	return m.insertBatchByTx(tx, "", tableName, fields, values)
+	return m.insertOrReplaceBatchByTx(tx, "INSERT", "", tableName, fields, values)
 }
 
-func (m *Mysql) insertBatchByTx(tx *sql.Tx, afterInsert, tableName string, fields []string, values []interface{}) (int64, int64) {
+//批量更新
+func (m *Mysql) ReplaceBatch(tableName string, fields []string, values []interface{}) (int64, int64) {
+	return m.InsertBatchByTx(nil, tableName, fields, values)
+}
+
+//带事务的批量更新
+func (m *Mysql) ReplaceBatchByTx(tx *sql.Tx, tableName string, fields []string, values []interface{}) (int64, int64) {
+	return m.insertOrReplaceBatchByTx(tx, "REPLACE", "", tableName, fields, values)
+}
+
+func (m *Mysql) insertOrReplaceBatchByTx(tx *sql.Tx, tp string, afterInsert, tableName string, fields []string, values []interface{}) (int64, int64) {
 	placeholders := []string{}
 	for range fields {
 		placeholders = append(placeholders, "?")
@@ -118,7 +128,7 @@ func (m *Mysql) insertBatchByTx(tx *sql.Tx, afterInsert, tableName string, field
 	for i := 0; i < len(values)/len(fields); i++ {
 		array = append(array, fmt.Sprintf("(%s)", placeholder))
 	}
-	q := fmt.Sprintf("INSERT %s INTO %s (%s) VALUES %s", afterInsert, tableName, strings.Join(fields, ","), strings.Join(array, ","))
+	q := fmt.Sprintf("%s %s INTO %s (%s) VALUES %s", tp, afterInsert, tableName, strings.Join(fields, ","), strings.Join(array, ","))
 	result, _ := m.ExecBySqlByTx(tx, q, values...)
 	if result == nil {
 		return -1, -1