-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.go
More file actions
executable file
·118 lines (101 loc) · 3.11 KB
/
db.go
File metadata and controls
executable file
·118 lines (101 loc) · 3.11 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package sqlxplus
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
)
type sqlxer interface {
sqlx.QueryerContext
sqlx.ExecerContext
sqlx.PreparerContext
sqlx.Queryer
sqlx.Execer
sqlx.Preparer
}
type DB struct {
SqlxDB sqlxer
Log Logger
}
func (that *DB) GetSqlxDB() *sqlx.DB {
return that.SqlxDB.(*sqlx.DB)
}
func (that *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
that.Log.Printf(ctx, query, args...)
rows, err = that.SqlxDB.QueryContext(ctx, query, args...)
if err != nil {
that.Log.Errorln(ctx, err)
}
return
}
func (that *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (rows *sqlx.Rows, err error) {
that.Log.Printf(ctx, query, args...)
rows, err = that.SqlxDB.QueryxContext(ctx, query, args...)
if err != nil {
that.Log.Errorln(ctx, err)
}
return
}
func (that *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
that.Log.Printf(ctx, query, args...)
return that.SqlxDB.QueryRowxContext(ctx, query, args...)
}
func (that *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error) {
that.Log.Printf(ctx, query, args...)
result, err = that.SqlxDB.ExecContext(ctx, query, args...)
if err != nil {
that.Log.Errorln(ctx, err)
}
return
}
func (that *DB) PrepareContext(ctx context.Context, query string) (stmt *sql.Stmt, err error) {
that.Log.Println(ctx, query)
stmt, err = that.SqlxDB.PrepareContext(ctx, query)
if err != nil {
that.Log.Errorln(ctx, err)
}
return
}
func (that *DB) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
that.Log.Printf(context.Background(), query, args...)
rows, err = that.SqlxDB.Query(query, args...)
if err != nil {
that.Log.Errorln(context.Background(), err)
}
return
}
func (that *DB) Queryx(query string, args ...interface{}) (rows *sqlx.Rows, err error) {
that.Log.Printf(context.Background(), query, args...)
rows, err = that.SqlxDB.Queryx(query, args...)
if err != nil {
that.Log.Errorln(context.Background(), err)
}
return
}
func (that *DB) QueryRowx(query string, args ...interface{}) *sqlx.Row {
that.Log.Printf(context.Background(), query, args...)
return that.SqlxDB.QueryRowx(query, args...)
}
func (that *DB) Exec(query string, args ...interface{}) (result sql.Result, err error) {
that.Log.Printf(context.Background(), query, args...)
result, err = that.SqlxDB.Exec(query, args...)
if err != nil {
that.Log.Errorln(context.Background(), err)
}
return
}
func (that *DB) Prepare(query string) (stmt *sql.Stmt, err error) {
that.Log.Println(context.Background(), query)
stmt, err = that.SqlxDB.Prepare(query)
if err != nil {
that.Log.Errorln(context.Background(), err)
}
return
}
// ExecContext error handling of sqlx.MustExecContext.
func ExecContext(ctx context.Context, e sqlx.ExecerContext, query string, args ...interface{}) (sql.Result, error) {
return e.ExecContext(ctx, query, args...)
}
// Exec error handling of sqlx.MustExec.
func Exec(e sqlx.Execer, query string, args ...interface{}) (sql.Result, error) {
return e.Exec(query, args...)
}