From a7bcbe996098127d62c56333cec1717ca598bb16 Mon Sep 17 00:00:00 2001 From: qydysky Date: Sun, 18 Jun 2023 13:24:15 +0000 Subject: [PATCH] =?utf8?q?Add=20=E4=BF=9D=E5=AD=98=E5=BC=B9=E5=B9=95?= =?utf8?q?=E8=87=B3db=E6=94=AF=E6=8C=81postgreSql?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- README.md | 13 +++++++++++++ Reply/F.go | 13 ++++++++++++- go.mod | 3 ++- go.sum | 7 +++++-- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f2f4603..bea45ad 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,19 @@ #### 保存弹幕至DB 配置文件中添加配置项`保存弹幕至db`。参考以下实例: +postgreSql: +```json +{ + "保存弹幕至db": { + "dbname": "postgres", + "url":"postgres://postgres:qydysky@192.168.31.103:5432/postgres?sslmode=disable", + "字段help":"time.Now().Format(time.DateTime), time.Now().Unix(), item.msg, item.color, item.auth, item.uid, item.roomid", + "create":"create table danmu (created varchar(20), createdunix varchar(20), msg varchar(100), color varchar(20), auth varchar(50), uid varchar(30), roomid varchar(30))", + "insert":"insert into danmu (created, createdunix, msg, color, auth, uid, roomid) values ({Date},{Unix},{Msg},{Color},{Auth},{Uid},{Roomid})" + } +} +``` + mysql: ```json { diff --git a/Reply/F.go b/Reply/F.go index 7d02b99..91a7c87 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -21,6 +21,7 @@ import ( "database/sql" _ "github.com/go-sql-driver/mysql" + _ "github.com/lib/pq" psql "github.com/qydysky/part/sql" "golang.org/x/text/encoding/simplifiedchinese" _ "modernc.org/sqlite" @@ -1660,6 +1661,7 @@ func (t *DanmuReLiveTriger) Check(uid, msg string) { var saveDanmuToDB SaveDanmuToDB type SaveDanmuToDB struct { + dbname string db *sql.DB insert string sync.Once @@ -1682,6 +1684,8 @@ func (t *SaveDanmuToDB) init(c *c.Common) { return } + t.dbname = dbname + if db, e := sql.Open(dbname, url); e != nil { c.Log.Base_add("保存弹幕至db").L(`E: `, e) } else { @@ -1716,6 +1720,13 @@ func (t *SaveDanmuToDB) danmu(item Danmu_item) { Roomid int64 } + var replaceF []func(index int, holder string) (replaceTo string) + if t.dbname == "postgres" { + replaceF = append(replaceF, func(index int, holder string) (replaceTo string) { + return fmt.Sprintf("$%d", index+1) + }) + } + tx := psql.BeginTx[any](t.db, context.Background()) tx.DoPlaceHolder(psql.SqlFunc[any]{Query: t.insert}, &DanmuI{ Date: time.Now().Format(time.DateTime), @@ -1725,7 +1736,7 @@ func (t *SaveDanmuToDB) danmu(item Danmu_item) { Auth: item.auth, Uid: item.uid, Roomid: int64(item.roomid), - }) + }, replaceF...) tx.AfterEF(func(_ *any, result sql.Result, txE error) (_ *any, stopErr error) { if v, e := result.RowsAffected(); e != nil { return nil, e diff --git a/go.mod b/go.mod index a179fa8..dad0d4d 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/gotk3/gotk3 v0.6.2 github.com/mdp/qrterminal/v3 v3.1.1 - github.com/qydysky/part v0.28.1-0.20230606105248-58d1b579625e + github.com/qydysky/part v0.28.1-0.20230618130156-8e4660c16bc9 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 golang.org/x/text v0.9.0 @@ -39,6 +39,7 @@ require ( github.com/go-sql-driver/mysql v1.7.1 github.com/gorilla/websocket v1.5.0 // indirect github.com/klauspost/compress v1.16.5 // indirect + github.com/lib/pq v1.10.9 github.com/miekg/dns v1.1.54 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/thedevsaddam/gojsonq/v2 v2.5.2 // indirect diff --git a/go.sum b/go.sum index 7db0348..4917b47 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,8 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNU github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI= github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= @@ -28,8 +30,8 @@ github.com/mdp/qrterminal/v3 v3.1.1/go.mod h1:5lJlXe7Jdr8wlPDdcsJttv1/knsRgzXASy github.com/miekg/dns v1.1.54 h1:5jon9mWcb0sFJGpnI99tOMhCPyJ+RPVz5b63MQG0VWI= github.com/miekg/dns v1.1.54/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/qydysky/part v0.28.1-0.20230606105248-58d1b579625e h1:uMWF6xwTeyT8PPmdbQ4yo60IqeJ+afZ+B4oK8Gt/P6g= -github.com/qydysky/part v0.28.1-0.20230606105248-58d1b579625e/go.mod h1:xSAOQyg7Wyd+QtXxmHBodYzx3y5HJnfr4R29MXxNKF4= +github.com/qydysky/part v0.28.1-0.20230618130156-8e4660c16bc9 h1:g5+GYyikM6n7e0enC9E6EnmfKkOTajYUmpMudyF2aA8= +github.com/qydysky/part v0.28.1-0.20230618130156-8e4660c16bc9/go.mod h1:CdkAHZ+OxieG1sI4M6UowP9j0QQDnhtDtN4tWsylCPU= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= @@ -65,6 +67,7 @@ golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM= golang.org/x/tools v0.9.3/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -- 2.39.2