-
Notifications
You must be signed in to change notification settings - Fork 0
/
gettimef.go
42 lines (31 loc) · 1.14 KB
/
gettimef.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 mysqlkebab
import (
"context"
"database/sql"
"fmt"
"time"
)
// MustGetTimef returns the query result's single value as a formatted time.Time, considering the given format string
// If sql.ErrNoRows occurs, it's returned.
// The other routines ( without "Must" preffix ) ignores sql.ErrNoRows
func (l *DBLink) MustGetTimef(format, sqlQuery string, args ...interface{}) (string, error) {
if !l.supposedReady {
return "", fmt.Errorf("connection not properly initialized")
}
var t sql.NullTime
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(l.executionTimeoutSeconds)*time.Second)
defer cancel()
if err := l.db.QueryRowContext(ctx, sqlQuery, args...).Scan(&t); err != nil {
l.log("mysqlkebab.GetTime.QueryRowContext().Scan(%s) %v", sqlQuery, err)
return "", err
}
return t.Time.Format(format), nil
}
// GetTimef returns the query result's single value as a formatted time.Time, considering the given format string
func (l *DBLink) GetTimef(format, sqlQuery string, args ...interface{}) (string, error) {
s, err := l.MustGetTimef(format, sqlQuery, args...)
if l.IsEmptyErr(err) {
err = nil
}
return s, err
}