|
@@ -146,6 +146,44 @@ func (m *Mysql) insertOrReplaceBatchByTx(tx *sql.Tx, tp string, afterInsert, tab
|
|
|
return v1, v2
|
|
|
}
|
|
|
|
|
|
+func (m *Mysql) InsertBulk(tableName string, fields []string, doc ...map[string]interface{}) (int64, int64) {
|
|
|
+ placeholders := []string{}
|
|
|
+ for range fields {
|
|
|
+ placeholders = append(placeholders, "?")
|
|
|
+ }
|
|
|
+ placeholder := strings.Join(placeholders, ",")
|
|
|
+ array := []string{}
|
|
|
+ for i := 0; i < len(doc); i++ {
|
|
|
+ array = append(array, fmt.Sprintf("(%s)", placeholder))
|
|
|
+ }
|
|
|
+ var values []interface{}
|
|
|
+ for _, d := range doc {
|
|
|
+ for _, f := range fields {
|
|
|
+ if d[f] != nil {
|
|
|
+ values = append(values, d[f])
|
|
|
+ } else {
|
|
|
+ values = append(values, nil)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ q := fmt.Sprintf("%s %s INTO %s (%s) VALUES %s", "REPLACE", "", tableName, strings.Join(fields, ","), strings.Join(array, ","))
|
|
|
+ result, _ := m.ExecBySqlByTx(nil, q, values...)
|
|
|
+ if result == nil {
|
|
|
+ return -1, -1
|
|
|
+ }
|
|
|
+ v1, e1 := result.RowsAffected()
|
|
|
+ if e1 != nil {
|
|
|
+ log.Println(e1)
|
|
|
+ return -1, -1
|
|
|
+ }
|
|
|
+ v2, e2 := result.LastInsertId()
|
|
|
+ if e2 != nil {
|
|
|
+ log.Println(e2)
|
|
|
+ return -1, -1
|
|
|
+ }
|
|
|
+ return v1, v2
|
|
|
+}
|
|
|
+
|
|
|
//sql语句执行
|
|
|
func (m *Mysql) ExecBySql(q string, args ...interface{}) (sql.Result, error) {
|
|
|
return m.ExecBySqlByTx(nil, q, args...)
|