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