-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
69 lines (55 loc) · 1.54 KB
/
update.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package pgkebab
import (
"context"
"errors"
"fmt"
"log"
"strings"
"time"
)
// Update executes "update" sql queries against database
// At least one whereParameters is mandatory.
// Important: WHERE only accepts "AND" where criteria.
// If you need to send more complex operations/statements, use Execute()
func (l *DBLink) Update(table string, updatePairs map[string]interface{}, wherePairs map[string]interface{}) (int64, error) {
if !l.supposedReady {
return 0, fmt.Errorf("connection not properly initialized")
}
if len(updatePairs) < 1 {
return 0, errors.New("there are no update-pairs to operate")
}
if len(wherePairs) < 1 {
return 0, errors.New("there are no where-pairs to operate")
}
var (
fields []string
whereFields []string
i uint
values []interface{}
)
for l, v := range updatePairs {
i++
fields = append(fields, fmt.Sprintf("%s=$%d", l, i))
values = append(values, v)
}
for l, v := range wherePairs {
i++
whereFields = append(whereFields, fmt.Sprintf("%s=$%d", l, i))
values = append(values, v)
}
sqlQuery := fmt.Sprintf("UPDATE %s SET %s WHERE %s",
table,
strings.Join(fields, ","),
strings.Join(whereFields, " AND "),
)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(l.executionTimeoutSeconds)*time.Second)
defer cancel()
rs, err := l.db.ExecContext(ctx, sqlQuery, values...)
if err != nil {
if l.debugPrint {
log.Printf(`pgkebab.Update %s db.ExecContext has failed with "%v"\n`, table, err)
}
return 0, err
}
return rs.RowsAffected()
}