/
db_log.go
71 lines (55 loc) · 1.45 KB
/
db_log.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
package uses
import (
"github.com/jetiny/sgin/common"
"github.com/gin-gonic/gin"
)
const logKey = "dbLogKey"
type LogTargetTable int
func (s LogTargetTable) Get(c *gin.Context) *dbLog {
return getDbLog(c).Table(s)
}
const (
LogTableNone LogTargetTable = 0 // 没有表
)
type dbLog struct {
c *gin.Context
tableId LogTargetTable
}
var gLogHandle common.LogHandler
func (l *dbLog) Table(tableId LogTargetTable) *dbLog {
return &dbLog{
c: l.c,
tableId: tableId,
}
}
func (l *dbLog) LogInsert(primaryId int64, data any) *dbLog {
gLogHandle(l.c, int(l.tableId), common.LogOpInsert, primaryId, data)
return l
}
func (l *dbLog) LogUpdate(primaryId int64, data any) *dbLog {
gLogHandle(l.c, int(l.tableId), common.LogOpUpdate, primaryId, data)
return l
}
func (l *dbLog) Log(primaryId int64, data any) *dbLog {
gLogHandle(l.c, int(l.tableId), common.LogOpNormal, primaryId, data)
return l
}
func (l *dbLog) LogDelete(primaryId int64, data any) *dbLog {
gLogHandle(l.c, int(l.tableId), common.LogOpDelete, primaryId, data)
return l
}
func (l *dbLog) LogDrop(primaryId int64, data any) *dbLog {
gLogHandle(l.c, int(l.tableId), common.LogOpDrop, primaryId, data)
return l
}
func withDbLog(handler common.LogHandler) gin.HandlerFunc {
gLogHandle = handler
return func(c *gin.Context) {
c.Set(logKey, &dbLog{c: c, tableId: LogTableNone})
c.Next()
}
}
func getDbLog(c *gin.Context) *dbLog {
value := c.MustGet(logKey)
return value.(*dbLog)
}