-
Notifications
You must be signed in to change notification settings - Fork 8
/
juice.go
223 lines (192 loc) · 6.12 KB
/
juice.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
Copyright 2023 eatmoreapple
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package juice
import (
"context"
"database/sql"
"github.com/eatmoreapple/juice/cache"
"github.com/eatmoreapple/juice/driver"
"sync"
)
// Engine is the implementation of Manager interface and the core of juice.
type Engine struct {
// configuration is the configuration of the engine
// It is used to initialize the engine and to one the mapper statements
configuration IConfiguration
// driver is the driver used by the engine
// It is used to initialize the database connection and translate the mapper statements
driver driver.Driver
// db is the database connection
db *sql.DB
// rw is the read write lock
// default use sync.RWMutex
// if you have many multiple processes to access the same database,
// you can use the distributed lock, such as redis, etc.
// call SetLocker to set your own business lock.
rw RWLocker
// middlewares is the middlewares of the engine
// It is used to intercept the execution of the statements
// like logging, tracing, etc.
middlewares MiddlewareGroup
// executorAdapter is the wrapper of the executor
// which is used to wrap the executor
executorAdapter ExecutorAdapterGroup
}
// executor represents a mapper executor with the given parameters
func (e *Engine) executor(v any) (*executor, error) {
stat, err := e.GetConfiguration().GetStatement(v)
if err != nil {
return nil, err
}
return &executor{
statement: stat,
session: e.DB(),
driver: e.driver,
middlewares: e.middlewares,
}, nil
}
// warpExecutor wraps the executor, ensure enable the middlewares
func (e *Engine) warpExecutor(executor Executor) Executor {
return e.executorAdapter.AdapterExecutor(executor)
}
// Object implements the Manager interface
func (e *Engine) Object(v any) Executor {
exe, err := e.executor(v)
if err != nil {
return inValidExecutor(err)
}
return e.warpExecutor(exe)
}
// Tx returns a TxManager
func (e *Engine) Tx() TxManager {
return e.ContextTx(context.Background(), nil)
}
// ContextTx returns a TxManager with the given context
func (e *Engine) ContextTx(ctx context.Context, opt *sql.TxOptions) TxManager {
tx, err := e.DB().BeginTx(ctx, opt)
if err != nil {
return invalidTxManager{err: err}
}
return &txManager{engine: e, tx: tx}
}
// CacheTx returns a TxCacheManager.
func (e *Engine) CacheTx() TxCacheManager {
return e.ContextCacheTx(context.Background(), nil)
}
// ContextCacheTx returns a TxCacheManager with the given context.
func (e *Engine) ContextCacheTx(ctx context.Context, opt *sql.TxOptions) TxCacheManager {
tx := e.ContextTx(ctx, opt)
return NewTxCacheManager(tx, cache.InMemoryScopeCache())
}
// GetConfiguration returns the configuration of the engine
func (e *Engine) GetConfiguration() IConfiguration {
e.rw.RLock()
defer e.rw.RUnlock()
return e.configuration
}
// SetConfiguration sets the configuration of the engine
func (e *Engine) SetConfiguration(cfg IConfiguration) {
e.rw.Lock()
defer e.rw.Unlock()
e.configuration = cfg
}
// Use adds a middleware to the engine
func (e *Engine) Use(middleware Middleware) {
e.middlewares = append(e.middlewares, middleware)
}
// DB returns the database connection of the engine
func (e *Engine) DB() *sql.DB {
return e.db
}
// Driver returns the driver of the engine
func (e *Engine) Driver() driver.Driver {
return e.driver
}
// Close closes the database connection if it is not nil.
func (e *Engine) Close() error {
if e.db != nil {
return e.db.Close()
}
return nil
}
// SetLocker sets the locker of the engine
// it is not goroutine safe, so it should be called before the engine is used
func (e *Engine) SetLocker(locker RWLocker) {
if locker == nil {
panic("locker is nil")
}
e.rw = locker
}
// AddExecutorAdapter adds the executor adapter to the engine to wrap the executor.
func (e *Engine) AddExecutorAdapter(executorAdapter ExecutorAdapter) {
if executorAdapter == nil {
panic("executorAdapter is nil")
}
e.executorAdapter = append(e.executorAdapter, executorAdapter)
}
// init initializes the engine
func (e *Engine) init() error {
// one the default environment from the configuration
envs := e.configuration.Environments()
defaultEnvName := envs.Attribute("default")
env, err := envs.Use(defaultEnvName)
if err != nil {
return err
}
// try to one the driver from the configuration
drv, err := driver.Get(env.Driver)
if err != nil {
return err
}
e.driver = drv
// open the database connection
e.db, err = ConnectFromEnv(env)
return err
}
// NewEngine creates a new Engine
// Deprecated: use New instead
func NewEngine(configuration IConfiguration) (*Engine, error) {
return New(configuration)
}
// New is the alias of NewEngine
func New(configuration IConfiguration) (*Engine, error) {
engine := &Engine{}
engine.SetLocker(&sync.RWMutex{})
engine.SetConfiguration(configuration)
if err := engine.init(); err != nil {
return nil, err
}
// add the default middlewares
engine.Use(&useGeneratedKeysMiddleware{})
// set default executor wrapper
// those are necessary for the engine to work properly
engine.AddExecutorAdapter(NewParamCtxExecutorAdapter())
engine.AddExecutorAdapter(NewSessionCtxInjectorExecutorAdapter())
return engine, nil
}
// DefaultEngine is the alias of Default
// Deprecated: use Default instead
func DefaultEngine(configuration IConfiguration) (*Engine, error) {
return Default(configuration)
}
// Default creates a new Engine with the default middlewares
// It adds an interceptor to log the statements
func Default(configuration IConfiguration) (*Engine, error) {
engine, err := New(configuration)
if err != nil {
return nil, err
}
engine.Use(&TimeoutMiddleware{})
engine.Use(&DebugMiddleware{})
return engine, nil
}