From 21acc67578aa20559a92dcf340075585c05ff872 Mon Sep 17 00:00:00 2001 From: greycodee Date: Fri, 3 Feb 2023 15:14:45 +0800 Subject: [PATCH 1/2] add gorm framework --- .vscode/launch.json | 3 ++- db/message.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ db/message_test.go | 26 +++++++++++++++++++ go.mod | 7 +++-- go.sum | 16 +++++++++--- 5 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 db/message.go create mode 100644 db/message_test.go diff --git a/.vscode/launch.json b/.vscode/launch.json index 5fbad76..957295a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,5 +12,6 @@ "program": "${fileDirname}" } - ] + ], + "go.testFlags":["-v"] } \ No newline at end of file diff --git a/db/message.go b/db/message.go new file mode 100644 index 0000000..0686087 --- /dev/null +++ b/db/message.go @@ -0,0 +1,62 @@ +package db + +type Message struct { + MsgId int `gorm:"column:msgId;primary_key" json:"msgId"` + MsgSvrId int `gorm:"column:msgSvrId" json:"msgSvrId"` + Type int `gorm:"column:type" json:"type"` + Status int `gorm:"column:status" json:"status"` + IsSend int `gorm:"column:isSend" json:"isSend"` + IsShowTimer int `gorm:"column:isShowTimer" json:"isShowTimer"` + CreateTime int `gorm:"column:createTime" json:"createTime"` + Talker string `gorm:"column:talker" json:"talker"` + Content string `gorm:"column:content" json:"content"` + ImgPath string `gorm:"column:imgPath" json:"imgPath"` + Reserved string `gorm:"column:reserved" json:"reserved"` + Lvbuffer string `gorm:"column:lvbuffer" json:"lvbuffer"` + TransContent string `gorm:"column:transContent" json:"transContent"` + TransBrandWording string `gorm:"column:transBrandWording" json:"transBrandWording"` + TalkerId int `gorm:"column:talkerId" json:"talkerId"` + BizClientMsgId string `gorm:"column:bizClientMsgId" json:"bizClientMsgId"` + BizChatId int `gorm:"column:bizChatId" json:"bizChatId"` + BizChatUserId string `gorm:"column:bizChatUserId" json:"bizChatUserId"` + MsgSeq int `gorm:"column:msgSeq" json:"msgSeq"` + Flag int `gorm:"column:flag" json:"flag"` + SolitaireFoldInfo string `gorm:"column:solitaireFoldInfo" json:"solitaireFoldInfo"` + HistoryId string `gorm:"column:historyId" json:"historyId"` +} + +func (Message) TableName() string { + return "message" +} + +type Rcontact struct { + Username string `gorm:"column:username;primary_key" json:"username"` + Alias string `gorm:"column:alias" json:"alias"` + ConRemark string `gorm:"column:conRemark" json:"conRemark"` + DomainList string `gorm:"column:domainList" json:"domainList"` + Nickname string `gorm:"column:nickname" json:"nickname"` + PyInitial string `gorm:"column:pyInitial" json:"pyInitial"` + QuanPin string `gorm:"column:quanPin" json:"quanPin"` + ShowHead int `gorm:"column:showHead;default:0" json:"showHead"` + Type int `gorm:"column:type;default:0" json:"type"` + UiType int `gorm:"column:uiType;default:0" json:"uiType"` + WeiboFlag int `gorm:"column:weiboFlag;default:0" json:"weiboFlag"` + WeiboNickname string `gorm:"column:weiboNickname" json:"weiboNickname"` + ConRemarkPYFull string `gorm:"column:conRemarkPYFull" json:"conRemarkPYFull"` + ConRemarkPYShort string `gorm:"column:conRemarkPYShort" json:"conRemarkPYShort"` + Lvbuff string `gorm:"column:lvbuff" json:"lvbuff"` + VerifyFlag int `gorm:"column:verifyFlag;default:0" json:"verifyFlag"` + EncryptUsername string `gorm:"column:encryptUsername" json:"encryptUsername"` + ChatroomFlag int `gorm:"column:chatroomFlag" json:"chatroomFlag"` + DeleteFlag int `gorm:"column:deleteFlag;default:0" json:"deleteFlag"` + ContactLabelIds string `gorm:"column:contactLabelIds" json:"contactLabelIds"` + DescWordingId string `gorm:"column:descWordingId" json:"descWordingId"` + OpenImAppid string `gorm:"column:openImAppid" json:"openImAppid"` + SourceExtInfo string `gorm:"column:sourceExtInfo" json:"sourceExtInfo"` + Ticket string `gorm:"column:ticket" json:"ticket"` + UsernameFlag int `gorm:"column:usernameFlag;default:0" json:"usernameFlag"` +} + +func (Rcontact) TableName() string { + return "rcontact" +} diff --git a/db/message_test.go b/db/message_test.go new file mode 100644 index 0000000..7b5ca17 --- /dev/null +++ b/db/message_test.go @@ -0,0 +1,26 @@ +package db + +import ( + "testing" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +func TestQuery(t *testing.T) { + db, err := gorm.Open(sqlite.Open("/home/zheng/coding/wechatbak/dest/79b23ef49a3016d8c52a787fc4ab59e4/EnMicroMsg_plain.db"), &gorm.Config{}) + if err != nil { + t.Fatal(err) + } + // db.AutoMigrate(&Message{}) + + var message Message + db.Take(&message) + t.Log(message) + +} + +func TestOut(t *testing.T) { + t.Log("hello") + // t.Error("sad") +} diff --git a/go.mod b/go.mod index 03f273f..953056a 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,9 @@ go 1.17 require ( github.com/gin-gonic/gin v1.8.2 - github.com/mattn/go-sqlite3 v1.14.13 - golang.org/x/sync v0.1.0 + github.com/mattn/go-sqlite3 v1.14.15 + gorm.io/driver/sqlite v1.4.4 + gorm.io/gorm v1.24.4 ) require ( @@ -14,6 +15,8 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect github.com/goccy/go-json v0.10.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/kr/text v0.2.0 // indirect github.com/leodido/go-urn v1.2.1 // indirect diff --git a/go.sum b/go.sum index fdf59ae..dd69412 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,11 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -40,8 +45,8 @@ github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ic github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-sqlite3 v1.14.13 h1:1tj15ngiFfcZzii7yd82foL+ks+ouQcj8j/TPq3fk1I= -github.com/mattn/go-sqlite3 v1.14.13/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -86,8 +91,6 @@ golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -131,3 +134,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.4.4 h1:gIufGoR0dQzjkyqDyYSCvsYR6fba1Gw5YKDqKeChxFc= +gorm.io/driver/sqlite v1.4.4/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI= +gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= +gorm.io/gorm v1.24.4 h1:Z6E28cuEnBw4/lcglUTM3XF6fkKQEbNyccdI00yiwGw= +gorm.io/gorm v1.24.4/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= From 0cea808ff91ed3ac1ec016a2e2843afc226c3a3e Mon Sep 17 00:00:00 2001 From: greycodee Date: Sat, 4 Feb 2023 15:25:27 +0800 Subject: [PATCH 2/2] add test --- db/message.go | 33 +++++++++++++++++++++++++++++++++ db/message_test.go | 21 ++++----------------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/db/message.go b/db/message.go index 0686087..e66a9cd 100644 --- a/db/message.go +++ b/db/message.go @@ -1,5 +1,10 @@ package db +import ( + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + type Message struct { MsgId int `gorm:"column:msgId;primary_key" json:"msgId"` MsgSvrId int `gorm:"column:msgSvrId" json:"msgSvrId"` @@ -60,3 +65,31 @@ type Rcontact struct { func (Rcontact) TableName() string { return "rcontact" } + +type ChatListResult struct { + MsgCount int64 `gorm:"column:msgCount"` + Alias string `gorm:"column:alias"` + Talker string `gorm:"column:talker"` + NickName string `gorm:"column:nickname"` + ConRemark string `gorm:"column:conRemark"` + Reserved1 string `gorm:"column:reserved1"` + Reserved2 string `gorm:"column:reserved2"` + CreateTime int64 `gorm:"column:createTime"` +} + +func GetChatList() (list []ChatListResult, err error) { + db, err := gorm.Open(sqlite.Open("/home/zheng/coding/wechatbak/dest/79b23ef49a3016d8c52a787fc4ab59e4/EnMicroMsg_plain.db"), &gorm.Config{}) + if err != nil { + return nil, err + } + + db.Table("message msg"). + Select("count(*) as msgCount,rc.alias,msg.talker,rc.nickname,rc.conRemark,imf.reserved1,imf.reserved2,msg.createTime"). + Joins("left join rcontact rc on msg.talker=rc.username"). + Joins("left join img_flag imf on msg.talker=imf.username"). + Group("msg.talker"). + Order("msg.createTime desc"). + Limit(5).Scan(&list) + + return +} diff --git a/db/message_test.go b/db/message_test.go index 7b5ca17..2fb3f71 100644 --- a/db/message_test.go +++ b/db/message_test.go @@ -1,26 +1,13 @@ package db import ( + "encoding/json" "testing" - - "gorm.io/driver/sqlite" - "gorm.io/gorm" ) func TestQuery(t *testing.T) { - db, err := gorm.Open(sqlite.Open("/home/zheng/coding/wechatbak/dest/79b23ef49a3016d8c52a787fc4ab59e4/EnMicroMsg_plain.db"), &gorm.Config{}) - if err != nil { - t.Fatal(err) - } - // db.AutoMigrate(&Message{}) - - var message Message - db.Take(&message) - t.Log(message) - -} + res, _ := GetChatList() -func TestOut(t *testing.T) { - t.Log("hello") - // t.Error("sad") + str, _ := json.Marshal(res) + t.Log(string(str)) }