]> 127.0.0.1 Git - part/.git/commitdiff
1
authorqydysky <qydysky@foxmail.com>
Mon, 22 Apr 2024 17:57:42 +0000 (17:57 +0000)
committerqydysky <qydysky@foxmail.com>
Mon, 22 Apr 2024 17:57:42 +0000 (17:57 +0000)
ctx/Ctx.go
go.mod
go.sum
log/Log.go
log/Log_test.go
sql/Sql_test.go

index db0174ef422dd0bba3e5c54ae2cf65ca17556cec..c47703fc18c451a7afb1c6dd34ee3d4fd1464bbe 100644 (file)
@@ -151,3 +151,11 @@ func CallCancel(ctx context.Context) error {
        }
        return nil
 }
+
+func GenTOCtx(t time.Duration) context.Context {
+       return CarryCancel(context.WithTimeout(context.Background(), t))
+}
+
+func GenDLCtx(t time.Time) context.Context {
+       return CarryCancel(context.WithDeadline(context.Background(), t))
+}
diff --git a/go.mod b/go.mod
index 416a8cd95b65b6b77fe2a49300c95f831e17893f..4155288066c30f691e6fba911b54ba49a5b49dc6 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -15,7 +15,12 @@ require (
 
 require (
        github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
+       github.com/jackc/pgpassfile v1.0.0 // indirect
+       github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
+       github.com/jackc/puddle/v2 v2.2.1 // indirect
        github.com/ncruces/go-strftime v0.1.9 // indirect
+       golang.org/x/crypto v0.21.0 // indirect
+       golang.org/x/sync v0.6.0 // indirect
        modernc.org/gc/v3 v3.0.0-20240304020402-f0dba7c97c2b // indirect
 )
 
@@ -38,7 +43,7 @@ require (
 require (
        github.com/dustin/go-humanize v1.0.1
        github.com/go-ole/go-ole v1.3.0 // indirect
-       github.com/lib/pq v1.10.9
+       github.com/jackc/pgx/v5 v5.5.5
        github.com/stretchr/testify v1.9.0 // indirect
        golang.org/x/net v0.22.0 // indirect
        golang.org/x/sys v0.18.0 // indirect
diff --git a/go.sum b/go.sum
index 537021b307f68ade625c2a0372589c82c063c471..a654eb97c0e3e111d150be1d568a2a556a22a36b 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,6 @@
 github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
 github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
@@ -15,10 +16,16 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
 github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
 github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
 github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
+github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
+github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
+github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
+github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
+github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
+github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
+github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
+github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
 github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
 github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
-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.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
 github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
 github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
@@ -35,6 +42,9 @@ github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKl
 github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
 github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
 github.com/thedevsaddam/gojsonq/v2 v2.5.2 h1:CoMVaYyKFsVj6TjU6APqAhAvC07hTI6IQen8PHzHYY0=
@@ -45,6 +55,8 @@ github.com/tklauser/numcpus v0.7.0 h1:yjuerZP127QG9m5Zh/mSO4wqurYil27tHrqwRoRjpr
 github.com/tklauser/numcpus v0.7.0/go.mod h1:bb6dMVcj8A42tSE7i32fsIUCbQNllK5iDguyOZRUzAY=
 github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
 github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
+golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
+golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
 golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
 golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
 golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
@@ -60,6 +72,8 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
 golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
 golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
 golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/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=
 modernc.org/cc/v4 v4.19.3 h1:vE9kmJqUcyvNOf8F2Hn8od14SOMq34BiqcZ2tMzLk5c=
index 0e2ec6ddb46f8e291dd7846910a7606a0f9aea8a..3ee54376238ea54c6eae8f1148db9fcb79242e5e 100644 (file)
@@ -1,7 +1,6 @@
 package part
 
 import (
-       "context"
        "database/sql"
        "fmt"
        "io"
@@ -10,6 +9,7 @@ import (
        "strings"
        "time"
 
+       pctx "github.com/qydysky/part/ctx"
        f "github.com/qydysky/part/file"
        m "github.com/qydysky/part/msgq"
        psql "github.com/qydysky/part/sql"
@@ -73,7 +73,7 @@ func New(c Config) (o *Log_interface) {
                        }
                }
                if msg.DBConn != nil && msg.DBInsert != `` {
-                       sqlTx := psql.BeginTx[any](msg.DBConn, context.Background())
+                       sqlTx := psql.BeginTx[any](msg.DBConn, pctx.GenTOCtx(o.To))
                        sqlTx.SimpleDo(
                                msg.DBInsert,
                                strings.TrimSpace(msg.Prefix),
index 58af2dea34319ddff4c2b867f4c6b3cbbc5b24a7..49aecc474221a31115d23f1b7b50e975e4e444df 100644 (file)
@@ -3,15 +3,17 @@ package part
 import (
        // "fmt"
 
-       "context"
        "database/sql"
        "errors"
+       "log/slog"
        "testing"
+       "time"
 
        _ "net/http/pprof"
 
        _ "modernc.org/sqlite"
 
+       pctx "github.com/qydysky/part/ctx"
        psql "github.com/qydysky/part/sql"
 )
 
@@ -47,7 +49,7 @@ func Test_2(t *testing.T) {
        defer db.Close()
 
        {
-               tx := psql.BeginTx[any](db, context.Background(), &sql.TxOptions{})
+               tx := psql.BeginTx[any](db, pctx.GenTOCtx(time.Second), &sql.TxOptions{})
                tx = tx.Do(psql.SqlFunc[any]{
                        Query:      "create table log (p test,base text,msg text)",
                        SkipSqlErr: true,
@@ -74,7 +76,7 @@ func Test_2(t *testing.T) {
                        Base string
                        Msg  string `sql:"s"`
                }
-               tx := psql.BeginTx[any](db, context.Background(), &sql.TxOptions{})
+               tx := psql.BeginTx[any](db, pctx.GenTOCtx(time.Second), &sql.TxOptions{})
                tx = tx.SimpleDo("select p,base,msg as s from log")
                tx.AfterQF(func(_ *any, rows *sql.Rows, e *error) {
                        if ls, err := psql.DealRows[logg](rows, func() logg { return logg{} }); err == nil {
@@ -93,3 +95,9 @@ func Test_2(t *testing.T) {
                }
        }
 }
+
+func Test_3(t *testing.T) {
+       logger := slog.Default()
+       logger = logger.WithGroup("122")
+       logger.Info("sss", slog.String("1", "3"))
+}
index 1880c0e9ca1e79e53daae9a4dbcbd670576eb56b..6b9ad280fc58e28a83f747835925d8576224cf23 100644 (file)
@@ -9,7 +9,8 @@ import (
        "testing"
        "time"
 
-       _ "github.com/lib/pq"
+       _ "github.com/jackc/pgx/v5/stdlib"
+       pctx "github.com/qydysky/part/ctx"
        file "github.com/qydysky/part/file"
        _ "modernc.org/sqlite"
 )
@@ -260,17 +261,22 @@ func TestMain4(t *testing.T) {
 
 func Local_TestPostgresql(t *testing.T) {
        // connect
-       db, err := sql.Open("postgres", "postgres://postgres:qydysky@192.168.31.103:5432/postgres?sslmode=disable")
+       db, err := sql.Open("pgx", "postgres://postgres:qydysky@192.168.31.103:5432/postgres?sslmode=disable")
        if err != nil {
                t.Fatal(err)
        }
        defer db.Close()
 
+       // c := pctx.CarryCancel(context.WithTimeout(context.Background(), time.Second))
+       // if e := db.PingContext(c); e != nil {
+       //      t.Fatal(e)
+       // }
+
        type test1 struct {
                Created string `sql:"sss"`
        }
 
-       if _, e := BeginTx[any](db, context.Background(), &sql.TxOptions{}).Do(SqlFunc[any]{
+       if _, e := BeginTx[any](db, pctx.GenTOCtx(time.Second), &sql.TxOptions{}).Do(SqlFunc[any]{
                Query:      "create table test (created varchar(20))",
                SkipSqlErr: true,
        }).Fin(); e != nil {