-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
135 lines (112 loc) · 3.55 KB
/
context.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
132
133
134
135
package dbx
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
)
type txContextKey string
const contextKey txContextKey = "transaction"
//Transactioner TODO: must be added some more signatures
//Transactioner interface for dbclient
type Transactioner interface {
ExecStatement(statement *Statement) (sql.Result, error)
QueryStatement(statement *Statement) (*sqlx.Rows, error)
ExecStatementContext(ctx context.Context, statement *Statement) (sql.Result, error)
QueryStatementContext(ctx context.Context, statement *Statement) (*sqlx.Rows, error)
Rollback() error
Commit() error
}
//Context is dbclient too but wrap on or more statements.
type Context struct {
Client
Statements []*Statement
}
//AddStatement add new statement to context
func (me *Context) AddStatement(statement *Statement) {
me.Statements = append(me.Statements, statement)
}
//AddStatements add statements to context
func (me *Context) AddStatements(statements ...*Statement) {
for _, statement := range statements {
me.Statements = append(me.Statements, statement)
}
}
//ClearStatements clear current statements
func (me *Context) ClearStatements() {
me.Statements = nil
}
//MustUseTransaction check if the context should use transaction or not
func (me *Context) MustUseTransaction() bool {
return len(me.Statements) > 1 || me.transaction != nil
}
//execUseTransaction execute all deferred statements by using transaction
func (me *Context) execUseTransaction(ctx context.Context, transactioner Transactioner, statements []*Statement) ([]sql.Result, error) {
var saveResults []sql.Result
for _, statement := range statements {
result, err := transactioner.ExecStatementContext(ctx, statement)
if err != nil {
if rollbackError := transactioner.Rollback(); rollbackError != nil {
return nil, rollbackError
}
return nil, err
}
saveResults = append(saveResults, result)
}
if !me.IsUserDefinedTransaction {
me.CompleteTransaction()
}
return saveResults, nil
}
//execWithoutTransaction execute statement without transaction
func (me *Context) execWithoutTransaction(ctx context.Context, statements []*Statement) ([]sql.Result, error) {
var saveResults []sql.Result
for _, statement := range statements {
result, err := me.ExecStatementContext(ctx, statement)
if err != nil {
return nil, err
}
saveResults = append(saveResults, result)
}
return saveResults, nil
}
//SaveChanges execute all defered statements to database
func (me *Context) SaveChanges(ctx context.Context) ([]sql.Result, error) {
if ctx == nil {
ctx = context.Background()
}
if me.MustUseTransaction() {
txFromContext := me.GetTransactionScope(ctx)
if txFromContext != nil {
me.SetTransaction(txFromContext)
}
if me.GetTransaction() == nil {
me.IsUserDefinedTransaction = false //this flag is used in execUseTransaction(). if false, execUseTransaction will complete the transaction
newTransaction, err := NewTransaction(me.DB)
if err != nil {
return nil, err
}
results, err := me.execUseTransaction(ctx, newTransaction, me.Statements)
me.ClearStatements()
if err != nil {
return nil, err
}
err = newTransaction.Commit()
if err != nil {
return nil, err
}
return results, nil
}
results, err := me.execUseTransaction(ctx, me.GetTransaction(), me.Statements)
me.ClearStatements()
return results, err
}
results, err := me.execWithoutTransaction(ctx, me.Statements)
me.ClearStatements()
return results, err
}
//NewContext create new dbContext instance
func NewContext(dbClient *Client) *Context {
return &Context{
Client: *dbClient,
}
}