dal/pkg/builder/convert_insert.go
Anton Nesterov fb13fcbece
[fix] always use sorted map
Signed-off-by: Anton Nesterov <anton@demiurg.io>
2024-08-09 17:06:15 +02:00

39 lines
741 B
Go

package builder
import (
"fmt"
"strings"
)
type InsertData struct {
Statement string
Values []interface{}
}
func ConvertInsert(ctx Context, inserts []Map) (InsertData, error) {
keys := AggregateSortedKeys(inserts)
placeholder := make([]string, 0)
for range keys {
placeholder = append(placeholder, "?")
}
values := make([]interface{}, 0)
for _, insert := range inserts {
vals := make([]interface{}, 0)
for _, key := range keys {
vals = append(vals, insert[key])
}
values = append(values, vals)
}
sfmt := fmt.Sprintf(
"INSERT INTO %s (%s) VALUES (%s)", ctx.GetTableName(),
strings.Join(keys, ","),
strings.Join(placeholder, ","),
)
return InsertData{
Statement: sfmt,
Values: values,
}, nil
}