diff --git a/database/sql.go b/database/sql.go index 72cc8bd8f4fa5291f17e4410cfa13f4fa7f118bc_ZGF0YWJhc2Uvc3FsLmdv..412ebc99bd7586418b7f09796079ebd3ee440f76_ZGF0YWJhc2Uvc3FsLmdv 100644 --- a/database/sql.go +++ b/database/sql.go @@ -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. diff --git a/database/sql_helper.go b/database/sql_helper.go index 72cc8bd8f4fa5291f17e4410cfa13f4fa7f118bc_ZGF0YWJhc2Uvc3FsX2hlbHBlci5nbw==..412ebc99bd7586418b7f09796079ebd3ee440f76_ZGF0YWJhc2Uvc3FsX2hlbHBlci5nbw== 100644 --- a/database/sql_helper.go +++ b/database/sql_helper.go @@ -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 {