# HG changeset patch # User Florent Aide <florent.aide@gmail.com> # Date 1641310754 -3600 # Tue Jan 04 16:39:14 2022 +0100 # Node ID f2bf45643d8001bf2449901daf0d0e6f67b63af0 # Parent 938b5b90d3868910456337685c20280da081b724 added an UpdateColumns helper diff --git a/database/sql.go b/database/sql.go --- a/database/sql.go +++ b/database/sql.go @@ -123,10 +123,13 @@ // SQLUpdate generates a squirrel "update" statement // for the given mapped instance (auto-selecting by its pkey) -func SQLUpdate(m Mapped) squirrel.UpdateBuilder { +func SQLUpdate(m Mapped, columns ...string) squirrel.UpdateBuilder { + if len(columns) == 0 { + columns = m.Columns(false) + } q := squirrel. Update(m.Table()). - SetMap(ValuesMap(m, m.Columns(false)...)).Where( + SetMap(ValuesMap(m, columns...)).Where( squirrel.Eq{m.PKeyColumn(): m.Values(m.PKeyColumn())}) return q } diff --git a/database/sql_helper.go b/database/sql_helper.go --- a/database/sql_helper.go +++ b/database/sql_helper.go @@ -128,6 +128,15 @@ return nil } +// UpdateColumns update a mapped but only for the given columns +func (h *SQLHelper) UpdateColumns(instance Mapped, columns ...string) error { + query := SQLUpdate(instance, columns...) + 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 {