/
orm.go
205 lines (171 loc) · 4.86 KB
/
orm.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package orm
import (
"fmt"
"sync"
cd "github.com/muidea/magicCommon/def"
"github.com/muidea/magicCommon/foundation/log"
"github.com/muidea/magicOrm/executor"
"github.com/muidea/magicOrm/model"
"github.com/muidea/magicOrm/provider"
)
const maxDeepLevel = 3
// Orm orm interface
type Orm interface {
Create(entity model.Model) *cd.Result
Drop(entity model.Model) *cd.Result
Insert(entity model.Model) (model.Model, *cd.Result)
Update(entity model.Model) (model.Model, *cd.Result)
Delete(entity model.Model) (model.Model, *cd.Result)
Query(entity model.Model) (model.Model, *cd.Result)
Count(filter model.Filter) (int64, *cd.Result)
BatchQuery(filter model.Filter) ([]model.Model, *cd.Result)
BeginTransaction() *cd.Result
CommitTransaction() *cd.Result
RollbackTransaction() *cd.Result
Release()
}
var name2Pool sync.Map
var name2PoolInitializeOnce sync.Once
var name2PoolUninitializedOnce sync.Once
// NewPool new executor pool
func NewPool() executor.Pool {
return executor.NewPool()
}
// NewExecutor NewExecutor
func NewExecutor(config executor.Config) (executor.Executor, *cd.Result) {
return executor.NewExecutor(config)
}
func NewConfig(dbServer, dbName, username, password, charSet string) executor.Config {
return executor.NewConfig(dbServer, dbName, username, password, charSet)
}
// Initialize InitOrm
func Initialize() {
name2PoolInitializeOnce.Do(func() {
name2Pool = sync.Map{}
})
}
// Uninitialized orm
func Uninitialized() {
name2PoolUninitializedOnce.Do(func() {
name2Pool.Range(func(_, val any) bool {
pool := val.(executor.Pool)
pool.Uninitialized()
return true
})
name2Pool = sync.Map{}
})
}
func AddDatabase(dbServer, dbName, username, password, charSet string, maxConnNum int, owner string) (err *cd.Result) {
config := NewConfig(dbServer, dbName, username, password, charSet)
val, ok := name2Pool.Load(owner)
if ok {
pool := val.(executor.Pool)
pool.IncReference()
err = pool.CheckConfig(config)
return
}
pool := NewPool()
err = pool.Initialize(maxConnNum, config)
if err != nil {
log.Errorf("AddDatabase failed, pool.Initialize error:%s", err.Error())
return
}
pool.IncReference()
name2Pool.Store(owner, pool)
return
}
func DelDatabase(owner string) {
val, ok := name2Pool.Load(owner)
if !ok {
return
}
pool := val.(executor.Pool)
if pool.DecReference() == 0 {
pool.Uninitialized()
name2Pool.Delete(owner)
}
}
// NewOrm create new Orm
func NewOrm(provider provider.Provider, cfg executor.Config, prefix string) (Orm, *cd.Result) {
executorVal, executorErr := NewExecutor(cfg)
if executorErr != nil {
log.Errorf("NewOrm failed, NewExecutor error:%s", executorErr.Error())
return nil, cd.NewError(cd.UnExpected, executorErr.Error())
}
orm := &impl{executor: executorVal, modelProvider: provider, specialPrefix: prefix}
return orm, nil
}
// GetOrm get orm from pool
func GetOrm(provider provider.Provider, prefix string) (ret Orm, err *cd.Result) {
val, ok := name2Pool.Load(provider.Owner())
if !ok {
err = cd.NewError(cd.UnExpected, fmt.Sprintf("can't find orm,name:%s", provider.Owner()))
log.Errorf("GetOrm failed, error:%s", err.Error())
return
}
pool := val.(executor.Pool)
executorVal, executorErr := pool.GetExecutor()
if executorErr != nil {
err = executorErr
log.Errorf("GetOrm failed, pool.GetExecutor error:%s", err.Error())
return
}
ret = &impl{executor: executorVal, modelProvider: provider, specialPrefix: prefix}
return
}
// impl orm
type impl struct {
executor executor.Executor
modelProvider provider.Provider
specialPrefix string
}
// BeginTransaction begin transaction
func (s *impl) BeginTransaction() (err *cd.Result) {
if s.executor != nil {
err = s.executor.BeginTransaction()
if err != nil {
log.Errorf("BeginTransaction failed, s.executor.BeginTransaction error:%s", err.Error())
}
}
return
}
// CommitTransaction commit transaction
func (s *impl) CommitTransaction() (err *cd.Result) {
if s.executor != nil {
err = s.executor.CommitTransaction()
if err != nil {
log.Errorf("CommitTransaction failed, s.executor.CommitTransaction error:%s", err.Error())
}
}
return
}
// RollbackTransaction rollback transaction
func (s *impl) RollbackTransaction() (err *cd.Result) {
if s.executor != nil {
err = s.executor.RollbackTransaction()
if err != nil {
log.Errorf("RollbackTransaction failed, s.executor.RollbackTransaction error:%s", err.Error())
}
}
return
}
func (s *impl) finalTransaction(err *cd.Result) {
if err == nil {
err = s.executor.CommitTransaction()
if err != nil {
log.Errorf("finalTransaction failed, s.executor.CommitTransaction error:%s", err.Error())
}
return
}
err = s.executor.RollbackTransaction()
if err != nil {
log.Errorf("finalTransaction failed, s.executor.RollbackTransaction error:%s", err.Error())
}
return
}
func (s *impl) Release() {
if s.executor != nil {
s.executor.Release()
s.executor = nil
}
}