-
Notifications
You must be signed in to change notification settings - Fork 1
/
getint.go
43 lines (32 loc) · 1.02 KB
/
getint.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
package pgkebab
import (
"context"
"database/sql"
"fmt"
"strconv"
"time"
)
// MustGetInt returns the query result's single value as an int
// If sql.ErrNoRows occurs, it's returned.
// The other routines ( without "Must" preffix ) ignores sql.ErrNoRows
func (l *DBLink) MustGetInt(sqlQuery string, args ...interface{}) (int, error) {
if !l.supposedReady {
return 0, fmt.Errorf("connection not properly initialized")
}
var i sql.NullInt64
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(l.executionTimeoutSeconds)*time.Second)
defer cancel()
if err := l.db.QueryRowContext(ctx, sqlQuery, args...).Scan(&i); err != nil {
l.log("pgkebab.GetInt.QueryRowContext().Scan(%s) %v", sqlQuery, err)
return 0, err
}
return strconv.Atoi(fmt.Sprintf("%d", i.Int64))
}
// GetInt returns the query result's single value as an int64
func (l *DBLink) GetInt(sqlQuery string, args ...interface{}) (int, error) {
i, err := l.MustGetInt(sqlQuery, args...)
if l.IsEmptyErr(err) {
err = nil
}
return i, err
}