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

added an UpdateColumns helper

parent 938b5b90d386
No related branches found
No related tags found
No related merge requests found
Pipeline #28252 passed
...@@ -123,6 +123,9 @@ ...@@ -123,6 +123,9 @@
// SQLUpdate generates a squirrel "update" statement // SQLUpdate generates a squirrel "update" statement
// for the given mapped instance (auto-selecting by its pkey) // 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. q := squirrel.
Update(m.Table()). Update(m.Table()).
...@@ -127,6 +130,6 @@ ...@@ -127,6 +130,6 @@
q := squirrel. q := squirrel.
Update(m.Table()). Update(m.Table()).
SetMap(ValuesMap(m, m.Columns(false)...)).Where( SetMap(ValuesMap(m, columns...)).Where(
squirrel.Eq{m.PKeyColumn(): m.Values(m.PKeyColumn())}) squirrel.Eq{m.PKeyColumn(): m.Values(m.PKeyColumn())})
return q return q
} }
......
...@@ -128,6 +128,15 @@ ...@@ -128,6 +128,15 @@
return nil 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 // UpsertNoPKey upserts a Mapped into the db
func (h *SQLHelper) UpsertNoPKey(keyCols []string, instances ...Mapped) error { func (h *SQLHelper) UpsertNoPKey(keyCols []string, instances ...Mapped) error {
for _, instance := range instances { 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