Skip to content

Commit 443b1b3

Browse files
committed
Update go sql to as a driver
1 parent 743b18c commit 443b1b3

7 files changed

Lines changed: 353 additions & 233 deletions

File tree

go/database/sql/connection.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package sql
2+
3+
import (
4+
"context"
5+
"database/sql/driver"
6+
"fmt"
7+
"runtime/debug"
8+
"strings"
9+
10+
"github.com/google/sqlcommenter/go/core"
11+
)
12+
13+
var attemptedToAutosetApplication = false
14+
15+
type sqlCommenterConn struct {
16+
driver.Conn
17+
options core.CommenterOptions
18+
}
19+
20+
func newSQLCommenterConn(conn driver.Conn, options core.CommenterOptions) *sqlCommenterConn {
21+
return &sqlCommenterConn{
22+
Conn: conn,
23+
options: options,
24+
}
25+
}
26+
27+
func (s *sqlCommenterConn) Query(query string, args []driver.Value) (driver.Rows, error) {
28+
queryer, ok := s.Conn.(driver.Queryer)
29+
if !ok {
30+
return nil, driver.ErrSkip
31+
}
32+
ctx := context.Background()
33+
extendedQuery := s.withComment(ctx, query)
34+
return queryer.Query(extendedQuery, args)
35+
}
36+
37+
func (s *sqlCommenterConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
38+
queryer, ok := s.Conn.(driver.QueryerContext)
39+
if !ok {
40+
return nil, driver.ErrSkip
41+
}
42+
extendedQuery := s.withComment(ctx, query)
43+
return queryer.QueryContext(ctx, extendedQuery, args)
44+
}
45+
46+
func (s *sqlCommenterConn) Exec(query string, args []driver.Value) (driver.Result, error) {
47+
execor, ok := s.Conn.(driver.Execer)
48+
if !ok {
49+
return nil, driver.ErrSkip
50+
}
51+
ctx := context.Background()
52+
extendedQuery := s.withComment(ctx, query)
53+
return execor.Exec(extendedQuery, args)
54+
}
55+
56+
func (s *sqlCommenterConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
57+
execor, ok := s.Conn.(driver.ExecerContext)
58+
if !ok {
59+
return nil, driver.ErrSkip
60+
}
61+
extendedQuery := s.withComment(ctx, query)
62+
return execor.ExecContext(ctx, extendedQuery, args)
63+
}
64+
65+
func (s *sqlCommenterConn) Raw() driver.Conn {
66+
return s.Conn
67+
}
68+
69+
// ***** Commenter Functions *****
70+
71+
func (conn *sqlCommenterConn) withComment(ctx context.Context, query string) string {
72+
var commentsMap = map[string]string{}
73+
query = strings.TrimSpace(query)
74+
config := conn.options.Config
75+
76+
// Sorted alphabetically
77+
if config.EnableAction && (ctx.Value(core.Action) != nil) {
78+
commentsMap[core.Action] = ctx.Value(core.Action).(string)
79+
}
80+
81+
// `driver` information should not be coming from framework.
82+
// So, explicitly adding that here.
83+
if config.EnableDBDriver {
84+
commentsMap[core.Driver] = fmt.Sprintf("database/sql:%s", conn.options.Tags.DriverName)
85+
}
86+
87+
if config.EnableFramework && (ctx.Value(core.Framework) != nil) {
88+
commentsMap[core.Framework] = ctx.Value(core.Framework).(string)
89+
}
90+
91+
if config.EnableRoute && (ctx.Value(core.Route) != nil) {
92+
commentsMap[core.Route] = ctx.Value(core.Route).(string)
93+
}
94+
95+
if config.EnableTraceparent {
96+
carrier := core.ExtractTraceparent(ctx)
97+
if val, ok := carrier["traceparent"]; ok {
98+
commentsMap[core.Traceparent] = val
99+
}
100+
}
101+
102+
if config.EnableApplication {
103+
if !attemptedToAutosetApplication && conn.options.Tags.Application == "" {
104+
attemptedToAutosetApplication = true
105+
bi, ok := debug.ReadBuildInfo()
106+
if ok {
107+
conn.options.Tags.Application = bi.Path
108+
}
109+
}
110+
commentsMap[core.Application] = conn.options.Tags.Application
111+
}
112+
113+
var commentsString string = ""
114+
if len(commentsMap) > 0 { // Converts comments map to string and appends it to query
115+
commentsString = fmt.Sprintf("/*%s*/", core.ConvertMapToComment(commentsMap))
116+
}
117+
118+
// A semicolon at the end of the SQL statement means the query ends there.
119+
// We need to insert the comment before that to be considered as part of the SQL statemtent.
120+
if query[len(query)-1:] == ";" {
121+
return fmt.Sprintf("%s%s;", strings.TrimSuffix(query, ";"), commentsString)
122+
}
123+
return fmt.Sprintf("%s%s", query, commentsString)
124+
}
125+
126+
// ***** Commenter Functions *****

go/database/sql/go-sql.go

Lines changed: 0 additions & 130 deletions
This file was deleted.

go/database/sql/go-sql_test.go

Lines changed: 0 additions & 101 deletions
This file was deleted.

go/database/sql/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/google/sqlcommenter/go/database/sql
33
go 1.19
44

55
require (
6-
github.com/google/sqlcommenter/go/core v0.0.2-beta
6+
github.com/google/sqlcommenter/go/core v0.0.5-beta
77
go.opentelemetry.io/otel/sdk v1.10.0
88
)
99

@@ -16,7 +16,7 @@ require (
1616

1717
require (
1818
github.com/DATA-DOG/go-sqlmock v1.5.0
19-
github.com/google/sqlcommenter/go/net/http v0.0.2-beta
19+
github.com/google/sqlcommenter/go/net/http v0.0.3-beta
2020
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.10.0
2121
go.opentelemetry.io/otel/trace v1.11.1 // indirect
2222
)

go/database/sql/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ github.com/google/sqlcommenter/go/core v0.0.1-beta h1:IVszEHanWVeS7UcmP8C3SHa57C
1111
github.com/google/sqlcommenter/go/core v0.0.1-beta/go.mod h1:CZfcqmbIxngExnZ7Se6AsKNVubZhKyi54aeDJZiqTMQ=
1212
github.com/google/sqlcommenter/go/core v0.0.2-beta h1:VnX58Jvf1mkI5KveBddZhCm4YtzG9IQErCNdmfXBU1I=
1313
github.com/google/sqlcommenter/go/core v0.0.2-beta/go.mod h1:CZfcqmbIxngExnZ7Se6AsKNVubZhKyi54aeDJZiqTMQ=
14+
github.com/google/sqlcommenter/go/core v0.0.5-beta h1:axqYR1zQCCdRBLnwr/j+ckllBSBJ7uaVdsnANuGzCUI=
15+
github.com/google/sqlcommenter/go/core v0.0.5-beta/go.mod h1:GORu2htXRC4xtejBzOa4ct1L20pohP81DFNYKdCJI70=
1416
github.com/google/sqlcommenter/go/net/http v0.0.1-beta h1:7XQ6poZv+ZJwwHWQHlesq9IMsRus3G6Z9n10qAkrGqE=
1517
github.com/google/sqlcommenter/go/net/http v0.0.1-beta/go.mod h1:tVUqM1YZ/K3eRTdGzeav1GSbw+BXNdTGzSAbLW9CxAc=
1618
github.com/google/sqlcommenter/go/net/http v0.0.2-beta h1:hL/nLxgWeM+2A7yKPoqhyJeaqQZI12kbruQ6/IEiErM=
1719
github.com/google/sqlcommenter/go/net/http v0.0.2-beta/go.mod h1:1sd6t92iCHaNQc/v5qxTHp+td7KNoD8IIeG4BRetFZo=
20+
github.com/google/sqlcommenter/go/net/http v0.0.3-beta h1:IE/vO3xKddn/2Bq3k+hSy4CxcEuvE1lUiIDYTXjApzA=
21+
github.com/google/sqlcommenter/go/net/http v0.0.3-beta/go.mod h1:duXQQvXZYCX8eQ+XOrlojWF512ltEp1eSKXc/KiS9lg=
1822
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1923
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
2024
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=

0 commit comments

Comments
 (0)