]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add 保存弹幕至db支持postgreSql
authorqydysky <qydysky@foxmail.com>
Sun, 18 Jun 2023 13:24:15 +0000 (13:24 +0000)
committerqydysky <qydysky@foxmail.com>
Sun, 18 Jun 2023 13:24:15 +0000 (13:24 +0000)
README.md
Reply/F.go
go.mod
go.sum

index f2f46035eaf9ddad83b5f9cd25495ed9a76de13e..bea45ad39d2823c224edc1c143c129e61555f43e 100644 (file)
--- a/README.md
+++ b/README.md
 #### 保存弹幕至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
 {
index 7d02b996209b929d6607b7ed5f20093f4dabe312..91a7c879ef33fd5b496acca22e08aac2f2459725 100644 (file)
@@ -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 a179fa8b90f52e8c9c45d1b68128893c82f99517..dad0d4d33b8c6703ec0996d5b46c939a392934c6 100644 (file)
--- 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 7db034807497eed322a4e70b5d90255c83ac5f23..4917b47fec3ba41710dabafe82439293804f9119 100644 (file)
--- 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=