main
lizifeng 2024-03-11 12:09:59 +08:00
parent 6233bdfde6
commit 8f16a44e73
2 changed files with 32 additions and 27 deletions

View File

@ -2,8 +2,6 @@ package mysqlserver
import (
"bytes"
"github.com/huandu/go-sqlbuilder"
jsoniter "github.com/json-iterator/go"
"io"
"proxymysql/app/conf"
"proxymysql/app/db"
@ -11,6 +9,9 @@ import (
"regexp"
"strings"
"time"
"github.com/huandu/go-sqlbuilder"
jsoniter "github.com/json-iterator/go"
)
var _ io.Writer = (*RecordQuery)(nil)
@ -62,7 +63,7 @@ func (r *RecordQuery) readQuery() {
zlog.Debugf("query: %s\n", query)
r.saveToDb(query)
r.saveToDb(query, false)
case ComPrepare:
query := string(packet.Payload[1:])
@ -71,7 +72,7 @@ func (r *RecordQuery) readQuery() {
r.stmtId++
r.stmtMap[r.stmtId] = query
r.saveToDb(query)
r.saveToDb(query, true)
case ComStmtExecute:
query := r.stmtMap[r.stmtId]
@ -87,14 +88,13 @@ func (r *RecordQuery) readQuery() {
zlog.Debugf("stmt: %s\n", fullSqlQuery)
}
r.saveToDb(fullSqlQuery)
r.saveToDb(fullSqlQuery, false)
case ComStmtClose:
delete(r.stmtMap, r.stmtId)
}
}
}
type BindArg struct {
@ -225,6 +225,7 @@ type SqlComment struct {
Query string
CallInfo string
CreateTime string
IsPrepare int32
}
var adminCommentReg = regexp.MustCompile(`/\*\s+TzAdmin-([\s\S]+)-TzAdmin\s+\*/`)
@ -259,12 +260,16 @@ func (r *RecordQuery) parseSqlComment(query string) (sc *SqlComment) {
return
}
func (r *RecordQuery) saveToDb(query string) {
func (r *RecordQuery) saveToDb(query string, isPrepare bool) {
if conf.App.SaveLog == false {
return
}
sc := r.parseSqlComment(query)
if isPrepare {
sc.IsPrepare = 1
}
// sc.Query = base64.StdEncoding.EncodeToString([]byte(sc.Query))
err := db.GetDB().Table("sql_query_log").Create(sc).Error

View File

@ -1,13 +1,14 @@
package webserver
import (
"github.com/gin-gonic/gin"
"net/http"
"proxymysql/app/conf"
"proxymysql/app/db"
"proxymysql/app/zlog"
"strings"
"time"
"github.com/gin-gonic/gin"
)
type ApiRes struct {
@ -47,6 +48,7 @@ func Start() {
query := db.GetDB().Table("sql_query_log a")
query.Select("a.*")
query.Where("a.admin_name != ''")
query.Where("a.is_prepare = ?", 0)
//if req.GameId >= 0 {
// query.Where("header_game_id", req.GameId)
@ -164,7 +166,6 @@ func Start() {
"TotalCount": GetSimplePageCount(req.Page, req.Limit, len(list)),
},
})
})
g.POST("/SelectInfo", func(ctx *gin.Context) {
@ -195,7 +196,6 @@ func Start() {
zlog.Infof("web server listen on %s", conf.App.WebAddr)
g.Run(conf.App.WebAddr)
}
func GetSimplePageCount(pageNum int, limit int, rowsNum int) int {