Skip to content

Commit 1b27cf5

Browse files
authored
sqlcomment: expose the underlying driver QueryContext/ExecContext methods (#5)
1 parent 6bb67a6 commit 1b27cf5

3 files changed

Lines changed: 112 additions & 16 deletions

File tree

driver.go

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,29 @@ import (
99
)
1010

1111
type (
12-
commenter struct {
13-
options
14-
}
15-
// Driver is a driver that adds an SQL comment (see https://google.github.io/sqlcommenter/).
12+
// Driver is a driver that adds an SQL comment.
13+
// See: https://google.github.io/sqlcommenter.
1614
Driver struct {
1715
dialect.Driver // underlying driver.
1816
commenter
1917
}
18+
2019
// Tx is a transaction implementation that adds an SQL comment.
2120
Tx struct {
2221
dialect.Tx // underlying transaction.
2322
ctx context.Context // underlying transaction context.
2423
commenter
2524
}
25+
26+
commenter struct {
27+
options
28+
}
2629
)
2730

2831
// NewDriver decorates the given driver and adds an SQL comment to every query.
2932
func NewDriver(drv dialect.Driver, options ...Option) dialect.Driver {
30-
defaultCommenters := []Tagger{contextTagger{}}
31-
opts := buildOptions(append(options, WithTagger(defaultCommenters...)))
33+
taggers := []Tagger{contextTagger{}}
34+
opts := buildOptions(append(options, WithTagger(taggers...)))
3235
return &Driver{drv, commenter{opts}}
3336
}
3437

@@ -40,14 +43,36 @@ func (c commenter) withComment(ctx context.Context, query string) string {
4043
return fmt.Sprintf("%s /*%s*/", query, tags.Marshal())
4144
}
4245

46+
// Query adds an SQL comment to the original query and calls the underlying driver Query method.
47+
func (d *Driver) Query(ctx context.Context, query string, args, v interface{}) error {
48+
return d.Driver.Query(ctx, d.withComment(ctx, query), args, v)
49+
}
50+
51+
// QueryContext calls QueryContext of the underlying driver, or fails if it is not supported.
52+
func (d *Driver) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
53+
drv, ok := d.Driver.(interface {
54+
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
55+
})
56+
if !ok {
57+
return nil, fmt.Errorf("Driver.QueryContext is not supported")
58+
}
59+
return drv.QueryContext(ctx, d.withComment(ctx, query), args...)
60+
}
61+
4362
// Exec adds an SQL comment to the original query and calls the underlying driver Exec method.
4463
func (d *Driver) Exec(ctx context.Context, query string, args, v interface{}) error {
4564
return d.Driver.Exec(ctx, d.withComment(ctx, query), args, v)
4665
}
4766

48-
// Query adds an SQL comment to the original query and calls the underlying driver Query method.
49-
func (d *Driver) Query(ctx context.Context, query string, args, v interface{}) error {
50-
return d.Driver.Query(ctx, d.withComment(ctx, query), args, v)
67+
// ExecContext calls ExecContext of the underlying driver, or fails if it is not supported.
68+
func (d *Driver) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
69+
drv, ok := d.Driver.(interface {
70+
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
71+
})
72+
if !ok {
73+
return nil, fmt.Errorf("Driver.ExecContext is not supported")
74+
}
75+
return drv.ExecContext(ctx, d.withComment(ctx, query), args...)
5176
}
5277

5378
// Tx wraps the underlying Tx command with a commenter.
@@ -79,11 +104,33 @@ func (d *Tx) Exec(ctx context.Context, query string, args, v interface{}) error
79104
return d.Tx.Exec(ctx, d.withComment(ctx, query), args, v)
80105
}
81106

107+
// ExecContext logs its params and calls the underlying transaction ExecContext method if it is supported.
108+
func (d *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
109+
tx, ok := d.Tx.(interface {
110+
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
111+
})
112+
if !ok {
113+
return nil, fmt.Errorf("Tx.ExecContext is not supported")
114+
}
115+
return tx.ExecContext(ctx, d.withComment(ctx, query), args...)
116+
}
117+
82118
// Query adds an SQL comment and calls the underlying transaction Query method.
83119
func (d *Tx) Query(ctx context.Context, query string, args, v interface{}) error {
84120
return d.Tx.Query(ctx, d.withComment(ctx, query), args, v)
85121
}
86122

123+
// QueryContext logs its params and calls the underlying transaction QueryContext method if it is supported.
124+
func (d *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
125+
tx, ok := d.Tx.(interface {
126+
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
127+
})
128+
if !ok {
129+
return nil, fmt.Errorf("Tx.QueryContext is not supported")
130+
}
131+
return tx.QueryContext(ctx, d.withComment(ctx, query), args...)
132+
}
133+
87134
// Commit commits the underlying Tx.
88135
func (d *Tx) Commit() error {
89136
return d.Tx.Commit()

go.mod

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module ariga.io/sqlcomment
22

3-
go 1.17
3+
go 1.19
44

55
require (
6-
entgo.io/ent v0.9.1
7-
github.com/mattn/go-sqlite3 v1.14.8
8-
github.com/stretchr/testify v1.7.0
6+
entgo.io/ent v0.11.3-0.20220816070906-2b54aadcce3a
7+
github.com/mattn/go-sqlite3 v1.14.14
8+
github.com/stretchr/testify v1.8.0
99
go.opencensus.io v0.23.0
1010
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.25.0
1111
go.opentelemetry.io/otel v1.0.1
@@ -14,14 +14,24 @@ require (
1414
)
1515

1616
require (
17+
ariga.io/atlas v0.6.0 // indirect
18+
github.com/agext/levenshtein v1.2.1 // indirect
19+
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
1720
github.com/davecgh/go-spew v1.1.1 // indirect
1821
github.com/felixge/httpsnoop v1.0.2 // indirect
19-
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
22+
github.com/go-openapi/inflect v0.19.0 // indirect
23+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
24+
github.com/google/go-cmp v0.5.6 // indirect
2025
github.com/google/uuid v1.3.0 // indirect
26+
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
27+
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
2128
github.com/pmezard/go-difflib v1.0.0 // indirect
29+
github.com/zclconf/go-cty v1.8.0 // indirect
2230
go.opentelemetry.io/otel/internal/metric v0.24.0 // indirect
2331
go.opentelemetry.io/otel/metric v0.24.0 // indirect
2432
go.opentelemetry.io/otel/trace v1.0.1 // indirect
25-
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
26-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
33+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
34+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
35+
golang.org/x/text v0.3.7 // indirect
36+
gopkg.in/yaml.v3 v3.0.1 // indirect
2737
)

0 commit comments

Comments
 (0)