generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryer.go
66 lines (54 loc) · 1.85 KB
/
queryer.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package sqlex
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
)
var (
// ensure sqlx.DB and sqlx.Tx implement Queryer interface.
_ Queryer = &sqlx.DB{}
_ Queryer = &sqlx.Tx{}
)
// Queryer is an interface that wraps sqlx.Queryer and sqlx.QueryerContext.
type Queryer interface {
sqlx.Queryer
sqlx.QueryerContext
}
// Query equals to sqlx.Queryer.Query and sqlx.QueryerContext.QueryContext.
func Query(ctx context.Context, q Queryer, query string, args ...interface{}) (*sql.Rows, error) {
if ctx == nil {
return q.Query(query, args...)
}
return q.QueryContext(ctx, query, args...)
}
// Queryx equals to sqlx.Queryer.Queryx and sqlx.QueryerContext.QueryxContext.
func Queryx(ctx context.Context, q Queryer, query string, args ...interface{}) (*sqlx.Rows, error) {
if ctx == nil {
return q.Queryx(query, args...)
}
return q.QueryxContext(ctx, query, args...)
}
// QueryRowx equals to sqlx.Queryer.QueryRowx and sqlx.QueryerContext.QueryRowxContext.
func QueryRowx(ctx context.Context, q Queryer, query string, args ...interface{}) *sqlx.Row {
if ctx == nil {
return q.QueryRowx(query, args...)
}
return q.QueryRowxContext(ctx, query, args...)
}
// Get equals to sqlx.Get and sqlx.GetContext.
func Get(ctx context.Context, q Queryer, dest interface{}, query string, args ...interface{}) error {
if ctx == nil {
return sqlx.Get(q, dest, query, args...)
}
return sqlx.GetContext(ctx, q, dest, query, args...)
}
// Select equals to sqlx.Select and sqlx.SelectContext.
func Select(ctx context.Context, q Queryer, dest interface{}, query string, args ...interface{}) error {
if ctx == nil {
return sqlx.Select(q, dest, query, args...)
}
return sqlx.SelectContext(ctx, q, dest, query, args...)
}