-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
131 lines (113 loc) · 3.43 KB
/
client.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
package dbx
import (
"context"
"database/sql"
"errors"
"github.com/jmoiron/sqlx"
)
//Client represent db client
type Client struct {
*sqlx.DB
transaction *Transaction
IsUserDefinedTransaction bool
}
//ExecStatement create, update or update statement
func (me *Client) ExecStatement(statement *Statement) (sql.Result, error) {
if me.transaction != nil {
return me.transaction.ExecStatement(statement)
}
if me.DB == nil {
return nil, errors.New("DB instance of type (*sql.DB) is nil")
}
return me.DB.NamedExec(statement.SQL, statement.Parameters)
}
//ExecStatementContext create, update or update statement
func (me *Client) ExecStatementContext(ctx context.Context, statement *Statement) (sql.Result, error) {
if me.transaction != nil {
return me.transaction.ExecStatementContext(ctx, statement)
}
if me.DB == nil {
return nil, errors.New("DB instance of type (*sql.DB) is nil")
}
return me.DB.NamedExecContext(ctx, statement.SQL, statement.Parameters)
}
//QueryStatement records on database and return it as sqlx.Rows
func (me *Client) QueryStatement(statement *Statement) (*sqlx.Rows, error) {
if me.transaction != nil && !me.transaction.IsComplete() {
return me.transaction.QueryStatement(statement)
}
return me.DB.NamedQuery(statement.SQL, statement.Parameters)
}
//QueryStatementContext records on database and return it as sqlx.Rows
func (me *Client) QueryStatementContext(ctx context.Context, statement *Statement) (*sqlx.Rows, error) {
if me.transaction != nil && !me.transaction.IsComplete() {
return me.transaction.QueryStatementContext(ctx, statement)
}
return me.DB.NamedQueryContext(ctx, statement.SQL, statement.Parameters)
}
//BeginTransaction begin a new transaction
func (me *Client) BeginTransaction() (*Transaction, error) {
sqlxTx, err := me.DB.Beginx()
if err != nil {
return nil, err
}
newTransaction := &Transaction{
Tx: sqlxTx,
}
me.SetTransaction(newTransaction)
return newTransaction, nil
}
//CompleteTransaction commit and reset current transaction
func (me *Client) CompleteTransaction() error {
if me.transaction != nil && !me.transaction.isComplete {
err := me.transaction.Commit()
if err != nil {
if rollbackError := me.transaction.Rollback(); rollbackError != nil {
return rollbackError
}
me.ResetTransaction()
return err
}
}
me.ResetTransaction()
return nil
}
//GetTransaction return current transaction
func (me *Client) GetTransaction() *Transaction {
return me.transaction
}
//SetTransaction set the dbclient to which transaction it's belong to
func (me *Client) SetTransaction(transaction *Transaction) {
me.transaction = transaction
if transaction != nil {
me.IsUserDefinedTransaction = true
}
}
//SetTransactionScope set transaction to the specified context
func (me *Context) SetTransactionScope(ctx context.Context) error {
newTransaction, err := NewTransaction(me.DB)
if err != nil {
return err
}
ctx = context.WithValue(ctx, contextKey, newTransaction)
return nil
}
//GetTransactionScope get transaction from context
func (me *Context) GetTransactionScope(ctx context.Context) *Transaction {
value := ctx.Value(contextKey)
if value != nil {
return value.(*Transaction)
}
return nil
}
//ResetTransaction set current transaction to nil
func (me *Client) ResetTransaction() {
me.IsUserDefinedTransaction = false
me.SetTransaction(nil)
}
//NewClient create new DB client instance
func NewClient(db *sqlx.DB) *Client {
return &Client{
DB: db,
}
}