# HG changeset patch
# User Florent Aide <florent.aide@gmail.com>
# Date 1604995863 -3600
#      Tue Nov 10 09:11:03 2020 +0100
# Node ID 412ebc99bd7586418b7f09796079ebd3ee440f76
# Parent  72cc8bd8f4fa5291f17e4410cfa13f4fa7f118bc
Add a sql update helper

diff --git a/database/sql.go b/database/sql.go
--- 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
--- 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 {