-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
57 lines (43 loc) · 1.23 KB
/
delete.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
package mysqlkebab
import (
"context"
"errors"
"fmt"
"log"
"strings"
"time"
)
// Delete executes "delete" statements against specific table, considering the mandatory "where" criteria
// The routine tries to return affected rows count.
func (l *DBLink) Delete(table string, wherePairs map[string]interface{}) (int64, error) {
if !l.supposedReady {
return 0, fmt.Errorf("connection not properly initialized")
}
if len(wherePairs) == 0 {
return 0, errors.New(`mysqlkebab.Delete("where" criteria not given`)
}
var (
whereList []string
parameters []interface{}
)
for k, v := range wherePairs {
s := fmt.Sprintf("%s=?", k)
whereList = append(whereList, s)
parameters = append(parameters, v)
}
whereExpression := strings.Join(whereList, " AND ")
sqlQuery := fmt.Sprintf("DELETE FROM %s WHERE %s", table, whereExpression)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(l.executionTimeoutSeconds)*time.Second)
defer cancel()
rs, err := l.db.ExecContext(ctx, sqlQuery, parameters...)
if err != nil {
if l.IsEmptyErr(err) {
return 0, nil
}
if l.debugPrint {
log.Printf(`mysqlkebab.Delete "%s" db.Exec has failed: "%v"\n`, table, err)
}
return 0, err
}
return rs.RowsAffected()
}