-
Notifications
You must be signed in to change notification settings - Fork 0
/
getbool.go
50 lines (37 loc) · 1.23 KB
/
getbool.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
package mysqlkebab
import (
"context"
"database/sql"
"fmt"
"time"
)
// MustGetBool returns the query result's single value as a boolean
// If sql.ErrNoRows occurs, it's returned.
// The other routines ( without "Must" preffix ) ignores sql.ErrNoRows
func (l *DBLink) MustGetBool(sqlQuery string, args ...interface{}) (bool, error) {
if !l.supposedReady {
return false, fmt.Errorf("connection not properly initialized")
}
var b sql.NullBool
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(l.executionTimeoutSeconds)*time.Second)
defer cancel()
if err := l.db.QueryRowContext(ctx, sqlQuery, args...).Scan(&b); err != nil {
l.log("mysqlkebab.GetBool.QueryRowContext().Scan(%s) %v", sqlQuery, err)
return false, err
}
return b.Bool, nil
}
// GetBool returns the query result's single value as a boolean
func (l *DBLink) GetBool(sqlQuery string, args ...interface{}) (bool, error) {
b, err := l.MustGetBool(sqlQuery, args...)
if l.IsEmptyErr(err) {
err = nil
}
return b, err
}
// Bool returns the query result's single value as a boolean.
// in case of any error, it returns false
func (l *DBLink) Bool(sqlQuery string, args ...interface{}) bool {
b, _ := l.MustGetBool(sqlQuery, args...)
return b
}