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