generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execer.go
41 lines (33 loc) · 1023 Bytes
/
execer.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
// 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 Execer interface.
_ Execer = &sqlx.DB{}
_ Execer = &sqlx.Tx{}
)
// Execer is an interface that wraps sqlx.Execer and sqlx.ExecerContext.
type Execer interface {
sqlx.Execer
sqlx.ExecerContext
}
// Exec equals to sqlx.Execer.Exec and sqlx.ExecerContext.ExecContext.
func Exec(ctx context.Context, e Execer, query string, args ...interface{}) (sql.Result, error) {
if ctx == nil {
return e.Exec(query, args...)
}
return e.ExecContext(ctx, query, args...)
}
// MustExec equals to sqlx.MustExec and sqlx.MustExecContext.
func MustExec(ctx context.Context, e Execer, query string, args ...interface{}) sql.Result {
if ctx == nil {
return sqlx.MustExec(e, query, args...)
}
return sqlx.MustExecContext(ctx, e, query, args...)
}