dbmeta/dbmeta.go

26 lines
601 B
Go

package dbmeta
import (
"context"
"fmt"
"strings"
)
func createInsertSQLForFields(ctx context.Context, table string, fields []string, rowsAmount int) string {
fieldsStr := strings.Join(fields, "`,`")
valuesStr := "(?" + strings.Repeat(", ?", len(fields)-1) + "),"
sql := fmt.Sprintf("INSERT INTO `%s` (`%s`) VALUES ", table, fieldsStr)
sql += strings.TrimRight(valuesStr+strings.Repeat(valuesStr, rowsAmount-1), ",")
sql += " ON DUPLICATE KEY UPDATE "
for i, field := range fields {
if i > 0 {
sql += ","
}
sql += fmt.Sprintf("`%s`=VALUES(`%s`)", field, field)
}
return sql
}