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,14 +72,14 @@ 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]
_, args := r.parseStmtArgs(strings.Count(query, "?"), packet.Payload)
//fmt.Printf("ComStmtExecute: %s %+v\n", query, args)
// fmt.Printf("ComStmtExecute: %s %+v\n", query, args)
fullSqlQuery, err := sqlbuilder.MySQL.Interpolate(query, args)
if err != nil {
@ -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 {
@ -116,18 +116,18 @@ func (r *RecordQuery) parseStmtArgs(argNum int, data []byte) ([]*BindArg, []any)
buf := bytes.NewBuffer(data)
//fmt.Printf("%+v\n", buf.Bytes())
// fmt.Printf("%+v\n", buf.Bytes())
buf.Next(skipPos)
nullBitMapLen := (argNum + 7) / 8
//fmt.Println(nullBitMapLen)
// fmt.Println(nullBitMapLen)
nullBitMap := buf.Next(nullBitMapLen)
//fmt.Println("nullBitMap", nullBitMap)
// fmt.Println("nullBitMap", nullBitMap)
newParamsBindFlag := ReadByte(buf.Next(1))
//fmt.Println("newParamsBindFlag", ReadByte(newParamsBindFlag))
// fmt.Println("newParamsBindFlag", ReadByte(newParamsBindFlag))
if newParamsBindFlag != 0x01 {
return nil, nil
@ -138,10 +138,10 @@ func (r *RecordQuery) parseStmtArgs(argNum int, data []byte) ([]*BindArg, []any)
for i := 0; i < argNum; i++ {
filedType := ReadByte(buf.Next(1))
//fmt.Printf("filedType: %+v\n", filedType)
// fmt.Printf("filedType: %+v\n", filedType)
unsigned := ReadByte(buf.Next(1))
//fmt.Printf("unsigned: %+v\n", unsigned)
// fmt.Printf("unsigned: %+v\n", unsigned)
bindArgs[i] = &BindArg{
ArgType: filedType,
@ -150,23 +150,23 @@ func (r *RecordQuery) parseStmtArgs(argNum int, data []byte) ([]*BindArg, []any)
}
}
//fmt.Printf("val: %+v\n", buf.Bytes())
// fmt.Printf("val: %+v\n", buf.Bytes())
//fmt.Printf("%+v\n", nullBitMap)
// fmt.Printf("%+v\n", nullBitMap)
for i := 0; i < argNum; i++ {
nullBytePos := i / 8
nullBitPos := i % 8
//fmt.Printf("nullBytePos: %08b\n", nullBitMap[nullBytePos])
//fmt.Printf("nullBitPos: %08b\n", 1<<nullBitPos)
// fmt.Printf("nullBytePos: %08b\n", nullBitMap[nullBytePos])
// fmt.Printf("nullBitPos: %08b\n", 1<<nullBitPos)
if (nullBitMap[nullBytePos] & (1 << nullBitPos)) > 0 {
//buf.Next(1)
// buf.Next(1)
bindArgs[i].ArgValue = nil
args[i] = nil
//fmt.Printf("%+v\n", bindArgs[i])
// fmt.Printf("%+v\n", bindArgs[i])
continue
}
@ -204,7 +204,7 @@ func (r *RecordQuery) parseStmtArgs(argNum int, data []byte) ([]*BindArg, []any)
bindArgs[i].ArgValue = val
args[i] = val
//fmt.Printf("str: %s\n", val)
// fmt.Printf("str: %s\n", val)
}
}
@ -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+\*/`)
@ -253,19 +254,23 @@ func (r *RecordQuery) parseSqlComment(query string) (sc *SqlComment) {
_sql := strings.TrimSpace(adminCommentReg.ReplaceAllString(sc.Query, ""))
sc.Query = _sql
//zlog.Infof("%+v\n", sc)
// zlog.Infof("%+v\n", sc)
}
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)
//sc.Query = base64.StdEncoding.EncodeToString([]byte(sc.Query))
if isPrepare {
sc.IsPrepare = 1
}
// sc.Query = base64.StdEncoding.EncodeToString([]byte(sc.Query))
err := db.GetDB().Table("sql_query_log").Create(sc).Error
if err != nil {

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 {
@ -28,7 +29,7 @@ func Start() {
})
g.POST("/ListSqlQueryLog", func(ctx *gin.Context) {
//time.Sleep(time.Millisecond * 200)
// time.Sleep(time.Millisecond * 200)
type Req struct {
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
@ -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 {