niki/pkg/query_builder/update.go

23 lines
608 B
Go
Raw Normal View History

package querybuilder
import (
"fmt"
"strings"
)
// BuildUpdateQuery constructs a dynamic SQL update query based on the provided fields
func BuildUpdateQuery(table string, fields map[string]interface{}, idField string, idValue interface{}) (string, []interface{}) {
var setClauses []string
var args []interface{}
for field, value := range fields {
setClauses = append(setClauses, fmt.Sprintf("%s = ?", field))
args = append(args, value)
}
query := fmt.Sprintf("UPDATE %s SET %s WHERE %s = ?", table, strings.Join(setClauses, ", "), idField)
args = append(args, idValue)
return query, args
}