From cfe9ad7c018e4d2ccd267a271fd1f0fb950f0398 Mon Sep 17 00:00:00 2001 From: kostyay Date: Mon, 12 Dec 2022 19:22:21 +0200 Subject: [PATCH 1/2] Add support for drivers with ConnBeginTx interface --- go/core/core.go | 8 +++++++- go/database/sql/connection.go | 22 ++++++++++++++++++++++ go/database/sql/gosql.go | 4 ++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/go/core/core.go b/go/core/core.go index 6fca682a..711ff049 100644 --- a/go/core/core.go +++ b/go/core/core.go @@ -52,10 +52,16 @@ type StaticTags struct { } type CommenterOptions struct { + Driver DriverOptions Config CommenterConfig Tags StaticTags } +type DriverOptions struct { + // Setting this to true means your underlying driver supports the ConnBeginTx interface + WithBeginTX bool +} + func encodeURL(k string) string { return url.QueryEscape(string(k)) } @@ -71,7 +77,7 @@ func ConvertMapToComment(tags map[string]string) string { var sb strings.Builder i, sz := 0, len(tags) - //sort by keys + // sort by keys sortedKeys := make([]string, 0, len(tags)) for k := range tags { sortedKeys = append(sortedKeys, k) diff --git a/go/database/sql/connection.go b/go/database/sql/connection.go index 2dbb5f1d..faac4201 100644 --- a/go/database/sql/connection.go +++ b/go/database/sql/connection.go @@ -31,6 +31,19 @@ type sqlCommenterConn struct { options core.CommenterOptions } +type sqlCommenterConnWithTx struct { + sqlCommenterConn +} + +func newConn(conn driver.Conn, options core.CommenterOptions) driver.Conn { + commenterConn := newSQLCommenterConn(conn, options) + if options.Driver.WithBeginTX { + return &sqlCommenterConnWithTx{*commenterConn} + } + + return commenterConn +} + func newSQLCommenterConn(conn driver.Conn, options core.CommenterOptions) *sqlCommenterConn { return &sqlCommenterConn{ Conn: conn, @@ -89,6 +102,15 @@ func (s *sqlCommenterConn) Raw() driver.Conn { return s.Conn } +func (s *sqlCommenterConnWithTx) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { + begintxer, ok := s.Conn.(driver.ConnBeginTx) + if !ok { + return nil, driver.ErrSkip + } + + return begintxer.BeginTx(ctx, opts) +} + // ***** Commenter Functions ***** func (conn *sqlCommenterConn) withComment(ctx context.Context, query string) string { diff --git a/go/database/sql/gosql.go b/go/database/sql/gosql.go index 525ddd37..24f68396 100644 --- a/go/database/sql/gosql.go +++ b/go/database/sql/gosql.go @@ -43,7 +43,7 @@ func (d *sqlCommenterDriver) Open(name string) (driver.Conn, error) { if err != nil { return nil, err } - return newSQLCommenterConn(rawConn, d.options), nil + return newConn(rawConn, d.options), nil } func (d *sqlCommenterDriver) OpenConnector(name string) (driver.Connector, error) { @@ -73,7 +73,7 @@ func (c *sqlCommenterConnector) Connect(ctx context.Context) (connection driver. if err != nil { return nil, err } - return newSQLCommenterConn(connection, c.options), nil + return newConn(connection, c.options), nil } func (c *sqlCommenterConnector) Driver() driver.Driver { From 0ef8142a48b43c2d4d44f235df87d5b17ad0cc63 Mon Sep 17 00:00:00 2001 From: kostyay Date: Mon, 12 Dec 2022 19:28:50 +0200 Subject: [PATCH 2/2] Add go workspace --- go/go.work | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 go/go.work diff --git a/go/go.work b/go/go.work new file mode 100644 index 00000000..086e1927 --- /dev/null +++ b/go/go.work @@ -0,0 +1,9 @@ +go 1.19 + +use ( + ./core + ./database/sql + ./gorrila/mux + ./net/http + ./samples/http +)