Skip to content

Support for building dynamic queries? #147

@railsmechanic

Description

@railsmechanic

Is there any builtin support to build e.g. dynamic UPDATE queries?
I found the sqlx.Named function which is able to take a map and assign the values according to the keys in the query, but is it possible with sqlx to build a (whole) query based on a given map?

Currently I'm building the query this way:

...

params := map[string]interface{}{
    "name": "John",
    "age":32,
}

named := make([]string, 0, len(params))
for key := range params {
  named = append(named, fmt.Sprintf("%s=:%s", key, key))
}
params["id"] = 232

result, err := db.NamedExec(fmt.Sprintf("UPDATE users SET %s WHERE id=:id;", strings.Join(named, ", ")), params)

...

Many thanks for your help...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions