@@ -9,26 +9,29 @@ import (
99)
1010
1111type (
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.
2932func 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.
4463func (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.
83119func (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.
88135func (d * Tx ) Commit () error {
89136 return d .Tx .Commit ()
0 commit comments