1 回答
TA贡献1874条经验 获得超12个赞
纯去😉
func prepareInsert(table string, params map[string]interface{}) string {
columns := len(params)
fieldSlice := make([]string, 0, columns)
for field, _ := range params {
fieldSlice = append(fieldSlice, field)
}
fields := strings.Join(fieldSlice, ",")
placeholders := prepareQueryPlaceholders(1, columns)
return fmt.Sprintf(`INSERT INTO %s (%s) VALUES (%s) RETURNING %s`, table, fields, placeholders, fields)
}
func prepareQueryPlaceholders(start, quantity int) string {
placeholders := make([]string, 0, quantity)
end := start + quantity
for i := start; i < end; i++ {
placeholders = append(placeholders, strings.Join([]string{"$", strconv.Itoa(i)}, ""))
}
return strings.Join(placeholders, ",")
}
- 1 回答
- 0 关注
- 180 浏览
添加回答
举报