1 回答

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
純?nèi)??
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 關(guān)注
- 219 瀏覽
添加回答
舉報(bào)