-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhey_track.go
More file actions
103 lines (83 loc) · 1.94 KB
/
hey_track.go
File metadata and controls
103 lines (83 loc) · 1.94 KB
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
package hey
import (
"context"
"time"
)
type MyTrackType string
const (
TrackDebug MyTrackType = "DEBUG"
TrackSQL MyTrackType = "SQL"
TrackTransaction MyTrackType = "TRANSACTION"
)
// Track Trace debug output, transaction status, and executed SQL statements.
type Track interface {
Track(ctx context.Context, track any)
}
// MyTrack Implement Track interface.
type MyTrack struct {
// Context Value of context.
Context context.Context
// Type Track type.
Type MyTrackType
// TimeStart Start time.
TimeStart time.Time
// TimeEnd End time.
TimeEnd time.Time
// Err Error value.
Err error
// TxId Transaction id.
TxId string
// TxMsg Transaction message.
TxMsg string
// TxState Transaction state.
TxState string
// Prepare Original SQL statement.
Prepare string
// Args The parameter list corresponding to the original SQL statement.
Args []any
// Script Visual SQL statement.
Script string
}
func (s *MyTrack) write(track Track, way *Way) {
if track == nil || way == nil {
return
}
if s.TimeEnd.IsZero() {
s.TimeEnd = time.Now()
}
s.Script = SQLToString(NewSQL(s.Prepare, s.Args...))
if way.IsInTransaction() {
way.transaction.addScript(s)
} else {
track.Track(s.Context, s)
}
}
func newTrack(typeValue MyTrackType) *MyTrack {
return &MyTrack{
Type: typeValue,
}
}
func trackDebug(ctx context.Context, maker Maker) *MyTrack {
script := maker.ToSQL()
tmp := newTrack(TrackDebug)
tmp.Context = ctx
tmp.Prepare = script.Prepare
tmp.Args = script.Args
tmp.Script = SQLToString(script)
tmp.TimeStart = time.Now()
return tmp
}
func trackSQL(ctx context.Context, prepare string, args []any) *MyTrack {
tmp := newTrack(TrackSQL)
tmp.Context = ctx
tmp.Prepare = prepare
tmp.Args = args
tmp.TimeStart = time.Now()
return tmp
}
func trackTransaction(ctx context.Context, startTime time.Time) *MyTrack {
tmp := newTrack(TrackTransaction)
tmp.Context = ctx
tmp.TimeStart = startTime
return tmp
}