-
Notifications
You must be signed in to change notification settings - Fork 28
/
database.go
283 lines (251 loc) · 7.5 KB
/
database.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package sqlingo
import (
"context"
"database/sql"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
const (
// for colorful terminal print
green = "\033[32m"
red = "\033[31m"
blue = "\033[34m"
reset = "\033[0m"
)
// Database is the interface of a database with underlying sql.DB object.
type Database interface {
// GetDB returns the underlying sql.DB object of the database
GetDB() *sql.DB
// BeginTx starts a transaction and executes the function f.
BeginTx(ctx context.Context, opts *sql.TxOptions, f func(tx Transaction) error) error
// Query executes a query and returns the cursor
Query(sql string) (Cursor, error)
// QueryContext executes a query with context and returns the cursor
QueryContext(ctx context.Context, sqlString string) (Cursor, error)
// Execute executes a statement
Execute(sql string) (sql.Result, error)
// ExecuteContext executes a statement with context
ExecuteContext(ctx context.Context, sql string) (sql.Result, error)
// SetLogger sets the logger function.
// Deprecated: use SetInterceptor instead
SetLogger(logger LoggerFunc)
// SetRetryPolicy sets the retry policy function.
// Deprecated: use SetInterceptor instead
SetRetryPolicy(retryPolicy func(err error) bool)
// EnableCallerInfo enable or disable the caller info in the log.
// Deprecated: use SetInterceptor instead
EnableCallerInfo(enableCallerInfo bool)
// SetInterceptor sets an interceptor function
SetInterceptor(interceptor InterceptorFunc)
// Select initiates a SELECT statement
Select(fields ...interface{}) selectWithFields
// SelectDistinct initiates a SELECT DISTINCT statement
SelectDistinct(fields ...interface{}) selectWithFields
// SelectFrom initiates a SELECT * FROM statement
SelectFrom(tables ...Table) selectWithTables
// InsertInto initiates a INSERT INTO statement
InsertInto(table Table) insertWithTable
// ReplaceInto initiates a REPLACE INTO statement
ReplaceInto(table Table) insertWithTable
// Update initiates a UPDATE statement
Update(table Table) updateWithSet
// DeleteFrom initiates a DELETE FROM statement
DeleteFrom(table Table) deleteWithTable
}
type txOrDB interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}
var (
once sync.Once
srcPrefix string
)
type database struct {
db *sql.DB
tx *sql.Tx
logger LoggerFunc
dialect dialect
retryPolicy func(error) bool
enableCallerInfo bool
interceptor InterceptorFunc
}
type LoggerFunc func(sql string, duration time.Duration, isTx bool, retry bool)
func (d *database) SetLogger(loggerFunc LoggerFunc) {
d.logger = loggerFunc
}
// DefaultLogger is sqlingo default logger,
// which print log to stderr and regard executing time gt 100ms as slow sql.
func DefaultLogger(sql string, duration time.Duration, isTx bool, retry bool) {
// for finding code position, try once is enough
once.Do(func() {
// $GOPATH/pkg/mod/github.com/lqs/[email protected]/database.go
_, file, _, _ := runtime.Caller(0)
// $GOPATH/pkg/mod/github.com/lqs/[email protected]
srcPrefix = filepath.Dir(file)
})
var file string
var line int
var ok bool
for i := 0; i < 16; i++ {
_, file, line, ok = runtime.Caller(i)
// `!strings.HasPrefix(file, srcPrefix)` jump out when using sqlingo as dependent package
// `strings.HasSuffix(file, "_test.go")` jump out when executing unit test cases
// `!ok` this is so terrible for something unexpected happened
if !ok || !strings.HasPrefix(file, srcPrefix) || strings.HasSuffix(file, "_test.go") {
break
}
}
// todo shouldn't append ';' here
if !strings.HasSuffix(sql, ";") {
sql += ";"
}
sb := strings.Builder{}
sb.Grow(32)
sb.WriteString("|")
sb.WriteString(duration.String())
if isTx {
sb.WriteString("|transaction") // todo using something traceable
}
if retry {
sb.WriteString("|retry")
}
sb.WriteString("|")
line1 := strings.Join(
[]string{
"[sqlingo]",
time.Now().Format("2006-01-02 15:04:05"),
sb.String(),
file + ":" + fmt.Sprint(line),
},
" ")
// print to stderr
fmt.Fprintln(os.Stderr, blue+line1+reset)
if duration < 100*time.Millisecond {
fmt.Fprintf(os.Stderr, "%s%s%s\n", green, sql, reset)
} else {
fmt.Fprintf(os.Stderr, "%s%s%s\n", red, sql, reset)
}
fmt.Fprintln(os.Stderr)
}
func (d *database) SetRetryPolicy(retryPolicy func(err error) bool) {
d.retryPolicy = retryPolicy
}
func (d *database) EnableCallerInfo(enableCallerInfo bool) {
d.enableCallerInfo = enableCallerInfo
}
func (d *database) SetInterceptor(interceptor InterceptorFunc) {
d.interceptor = interceptor
}
// Open a database, similar to sql.Open.
// `db` using a default logger, which print log to stderr and regard executing time gt 100ms as slow sql.
// To disable the default logger, use `db.SetLogger(nil)`.
func Open(driverName string, dataSourceName string) (db Database, err error) {
var sqlDB *sql.DB
if dataSourceName != "" {
sqlDB, err = sql.Open(driverName, dataSourceName)
if err != nil {
return
}
}
db = Use(driverName, sqlDB)
return
}
// Use an existing *sql.DB handle
func Use(driverName string, sqlDB *sql.DB) Database {
return &database{
dialect: getDialectFromDriverName(driverName),
db: sqlDB,
}
}
func (d database) GetDB() *sql.DB {
return d.db
}
func (d database) getTxOrDB() txOrDB {
if d.tx != nil {
return d.tx
}
return d.db
}
func (d database) Query(sqlString string) (Cursor, error) {
return d.QueryContext(context.Background(), sqlString)
}
func (d database) QueryContext(ctx context.Context, sqlString string) (Cursor, error) {
isRetry := false
for {
sqlStringWithCallerInfo := getCallerInfo(d, isRetry) + sqlString
rows, err := d.queryContextOnce(ctx, sqlStringWithCallerInfo, isRetry)
if err != nil {
isRetry = d.tx == nil && d.retryPolicy != nil && d.retryPolicy(err)
if isRetry {
continue
}
return nil, err
}
return cursor{rows: rows}, nil
}
}
func (d database) queryContextOnce(ctx context.Context, sqlString string, retry bool) (*sql.Rows, error) {
if ctx == nil {
ctx = context.Background()
}
startTime := time.Now()
defer func() {
endTime := time.Now()
if d.logger != nil {
d.logger(sqlString, endTime.Sub(startTime), false, retry)
}
}()
interceptor := d.interceptor
var rows *sql.Rows
invoker := func(ctx context.Context, sql string) (err error) {
rows, err = d.getTxOrDB().QueryContext(ctx, sql)
return
}
var err error
if interceptor == nil {
err = invoker(ctx, sqlString)
} else {
err = interceptor(ctx, sqlString, invoker)
}
if err != nil {
return nil, err
}
return rows, nil
}
func (d database) Execute(sqlString string) (sql.Result, error) {
return d.ExecuteContext(context.Background(), sqlString)
}
// ExecuteContext todo Is there need retry?
func (d database) ExecuteContext(ctx context.Context, sqlString string) (sql.Result, error) {
if ctx == nil {
ctx = context.Background()
}
sqlStringWithCallerInfo := getCallerInfo(d, false) + sqlString
startTime := time.Now()
defer func() {
endTime := time.Now()
if d.logger != nil {
d.logger(sqlStringWithCallerInfo, endTime.Sub(startTime), false, false)
}
}()
var result sql.Result
invoker := func(ctx context.Context, sql string) (err error) {
result, err = d.getTxOrDB().ExecContext(ctx, sql)
return
}
var err error
if d.interceptor == nil {
err = invoker(ctx, sqlStringWithCallerInfo)
} else {
err = d.interceptor(ctx, sqlStringWithCallerInfo, invoker)
}
if err != nil {
return nil, err
}
return result, err
}