|
| 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 ***** |
0 commit comments