diff --git a/database/sql_helper.go b/database/sql_helper.go index fe82db40aa33ddeec5923dd8a2cf69d5e4d31d54_ZGF0YWJhc2Uvc3FsX2hlbHBlci5nbw==..299043c82fc0a4842c11608c96967918939a17cc_ZGF0YWJhc2Uvc3FsX2hlbHBlci5nbw== 100644 --- a/database/sql_helper.go +++ b/database/sql_helper.go @@ -71,3 +71,30 @@ } return nil } + +// SyncRelationStrings ... +func (h *SQLHelper) SyncRelationStrings( + table string, colFrom string, colTo string, + colFromValue string, colToValues []string, +) error { + // Upsert ts les couples + q := sq.Insert(table). + Columns(colFrom, colTo) + for _, value := range colToValues { + q = q.Values(colFromValue, value) + } + q = q.Suffix("ON CONFLICT DO NOTHING") + if _, err := h.Exec(q); err != nil { + return err + } + + filter := sq.And{sq.Eq{colFrom: colFromValue}} + for _, value := range colToValues { + filter = append(filter, sq.NotEq{colTo: value}) + } + // Delete les valeurs non voulues + if _, err := h.Exec(sq.Delete(table).Where(filter)); err != nil { + return err + } + return nil +}