forked from forbole/callisto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
38 lines (32 loc) · 836 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package database
import (
"fmt"
)
// InsertEnableModules allows to save enabled module into the database
func (db *Db) InsertEnableModules(modules []string) error {
if len(modules) == 0 {
return nil
}
// Remove existing modules
stmt := "DELETE FROM modules WHERE TRUE"
_, err := db.Sql.Exec(stmt)
if err != nil {
return fmt.Errorf("error while deleting modules: %s", err)
}
if len(modules) == 0 {
return nil
}
var values []interface{}
stmt = `INSERT INTO modules (module_name) VALUES`
for key, value := range modules {
stmt += fmt.Sprintf("($%d),", key+1)
values = append(values, value)
}
stmt = stmt[:len(stmt)-1] // remove tailing ","
stmt += " ON CONFLICT DO NOTHING"
_, err = db.Sql.Exec(stmt, values...)
if err != nil {
return fmt.Errorf("error while storing modules: %s", err)
}
return nil
}