-
Notifications
You must be signed in to change notification settings - Fork 0
/
exists.go
38 lines (28 loc) · 808 Bytes
/
exists.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
package mysqlkebab
import (
"context"
"database/sql"
"fmt"
"log"
"time"
)
// Exists returns true if the query returns non null resultset
func (l *DBLink) Exists(sqlQuery string, args ...interface{}) (bool, error) {
if !l.supposedReady {
return false, fmt.Errorf("connection not properly initialized")
}
var b sql.NullBool
sqlQuery = fmt.Sprintf("SELECT EXISTS ( %s )", sqlQuery)
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 {
if l.IsEmptyErr(err) {
return false, nil
}
if l.debugPrint {
log.Printf("mysqlkebab.Exists.QueryRowContext().Scan(%s) %v\n", sqlQuery, err)
}
return false, err
}
return b.Bool, nil
}