|
@@ -88,6 +88,16 @@ func (m *Mysql) InsertBySqlByTx(tx *sql.Tx, sql string, args ...interface{}) int
|
|
|
return id
|
|
|
}
|
|
|
|
|
|
+//批量新增
|
|
|
+func (m *Mysql) InsertIgnoreBatch(tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
+ return m.InsertIgnoreBatchByTx(nil, tableName, fields, values)
|
|
|
+}
|
|
|
+
|
|
|
+//带事务的批量新增
|
|
|
+func (m *Mysql) InsertIgnoreBatchByTx(tx *sql.Tx, tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
+ return m.insertBatchByTx(tx, "IGNORE", tableName, fields, values)
|
|
|
+}
|
|
|
+
|
|
|
//批量新增
|
|
|
func (m *Mysql) InsertBatch(tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
return m.InsertBatchByTx(nil, tableName, fields, values)
|
|
@@ -95,6 +105,10 @@ 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)
|
|
|
+}
|
|
|
+
|
|
|
+func (m *Mysql) insertBatchByTx(tx *sql.Tx, afterInsert, tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
placeholders := []string{}
|
|
|
for range fields {
|
|
|
placeholders = append(placeholders, "?")
|
|
@@ -104,7 +118,7 @@ func (m *Mysql) InsertBatchByTx(tx *sql.Tx, tableName string, fields []string, v
|
|
|
for i := 0; i < len(values)/len(fields); i++ {
|
|
|
array = append(array, fmt.Sprintf("(%s)", placeholder))
|
|
|
}
|
|
|
- q := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", tableName, strings.Join(fields, ","), strings.Join(array, ","))
|
|
|
+ q := fmt.Sprintf("INSERT %s INTO %s (%s) VALUES %s", afterInsert, tableName, strings.Join(fields, ","), strings.Join(array, ","))
|
|
|
result, _ := m.ExecBySqlByTx(tx, q, values...)
|
|
|
if result == nil {
|
|
|
return -1, -1
|