-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathgo-sql.go
More file actions
113 lines (89 loc) · 3.5 KB
/
go-sql.go
File metadata and controls
113 lines (89 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gosql
import (
"context"
"database/sql"
"fmt"
"strings"
"google.com/sqlcommenter/core"
)
type DB struct {
*sql.DB
options core.CommenterOptions
}
func Open(driverName string, dataSourceName string, options core.CommenterOptions) (*DB, error) {
db, err := sql.Open(driverName, dataSourceName)
return &DB{DB: db, options: options}, err
}
// ***** Query Functions *****
func (db *DB) Query(query string, args ...any) (*sql.Rows, error) {
return db.DB.Query(db.withComment(context.Background(), query), args...)
}
func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row {
return db.DB.QueryRow(db.withComment(context.Background(), query), args...)
}
func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
return db.DB.QueryContext(ctx, db.withComment(ctx, query), args...)
}
func (db *DB) Exec(query string, args ...any) (sql.Result, error) {
return db.DB.Exec(db.withComment(context.Background(), query), args...)
}
func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
return db.DB.ExecContext(ctx, db.withComment(ctx, query), args...)
}
func (db *DB) Prepare(query string) (*sql.Stmt, error) {
return db.DB.Prepare(db.withComment(context.Background(), query))
}
func (db *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
return db.DB.PrepareContext(ctx, db.withComment(ctx, query))
}
// ***** Query Functions *****
// ***** Commenter Functions *****
func (db *DB) withComment(ctx context.Context, query string) string {
var commentsMap = map[string]string{}
query = strings.TrimSpace(query)
// Sorted alphabetically
if db.options.EnableAction && (ctx.Value(core.Action) != nil) {
commentsMap[core.Action] = ctx.Value(core.Action).(string)
}
// `driver` information should not be coming from framework.
// So, explicitly adding that here.
if db.options.EnableDBDriver {
commentsMap[core.Driver] = "database/sql"
}
if db.options.EnableFramework && (ctx.Value(core.Framework) != nil) {
commentsMap[core.Framework] = ctx.Value(core.Framework).(string)
}
if db.options.EnableRoute && (ctx.Value(core.Route) != nil) {
commentsMap[core.Route] = ctx.Value(core.Route).(string)
}
if db.options.EnableTraceparent {
carrier := core.ExtractTraceparent(ctx)
if val, ok := carrier["traceparent"]; ok {
commentsMap[core.Traceparent] = val
}
}
var commentsString string = ""
if len(commentsMap) > 0 { // Converts comments map to string and appends it to query
commentsString = fmt.Sprintf("/*%s*/", core.ConvertMapToComment(commentsMap))
}
// A semicolon at the end of the SQL statement means the query ends there.
// We need to insert the comment before that to be considered as part of the SQL statemtent.
if query[len(query)-1:] == ";" {
return fmt.Sprintf("%s%s;", strings.TrimSuffix(query, ";"), commentsString)
}
return fmt.Sprintf("%s%s", query, commentsString)
}
// ***** Commenter Functions *****