Skip to content
Snippets Groups Projects
Commit 412ebc99bd75 authored by Florent Aide's avatar Florent Aide
Browse files

Add a sql update helper

parent 72cc8bd8f4fa
No related branches found
No related tags found
No related merge requests found
Pipeline #7741 passed
......@@ -121,6 +121,16 @@
return values
}
// SQLUpdate generates a squirrel "update" statement
// for the given mapped instance (auto-selecting by its pkey)
func SQLUpdate(m Mapped) squirrel.UpdateBuilder {
q := squirrel.
Update(m.Table()).
SetMap(ValuesMap(m, m.Columns(false)...)).Where(
squirrel.Eq{m.PKeyColumn(): m.Values(m.PKeyColumn())})
return q
}
// SQLUpsert generates a squirrel "upsert" statement
func SQLUpsert(m Mapped) squirrel.InsertBuilder {
updateSQL, updateArgs, err := squirrel.
......
......@@ -84,6 +84,18 @@
return nil
}
// Update updates the given Mapped into the db using their
// pkey as predicate for the where clause
func (h *SQLHelper) Update(instances ...Mapped) error {
for _, instance := range instances {
query := SQLUpdate(instance)
if _, err := h.Exec(query); err != nil {
return err
}
}
return nil
}
// UpsertNoPKey upserts a Mapped into the db
func (h *SQLHelper) UpsertNoPKey(keyCols []string, instances ...Mapped) error {
for _, instance := range instances {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment