Skip to content

Commit

Permalink
feat: allow setting a query comment through a context value
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle McCullough <[email protected]>
  • Loading branch information
kylemcc committed Feb 11, 2025
1 parent 2dc8553 commit 9870802
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions query_column_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (q *AddColumnQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Res
return nil, feature.NewNotSupportError(feature.AlterColumnExists)
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions query_column_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func (q *DropColumnQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byt
//------------------------------------------------------------------------------

func (q *DropColumnQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions query_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ func (q *DeleteQuery) scanOrExec(
return nil, err
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

// Generate the query before checking hasReturning.
queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions query_index_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ func (q *CreateIndexQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []by
//------------------------------------------------------------------------------

func (q *CreateIndexQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions query_index_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func (q *DropIndexQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte
//------------------------------------------------------------------------------

func (q *DropIndexQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions query_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ func (q *InsertQuery) scanOrExec(
return nil, err
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

// Generate the query before checking hasReturning.
queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions query_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ func (q *MergeQuery) scanOrExec(
return nil, err
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

// Generate the query before checking hasReturning.
queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions query_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func (q *RawQuery) scanOrExec(
}
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

query := q.db.format(q.query, q.args)
var res sql.Result

Expand Down
18 changes: 18 additions & 0 deletions query_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,9 @@ func (q *SelectQuery) Rows(ctx context.Context) (*sql.Rows, error) {
return nil, err
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand All @@ -812,6 +815,9 @@ func (q *SelectQuery) Exec(ctx context.Context, dest ...interface{}) (res sql.Re
return nil, err
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand Down Expand Up @@ -872,6 +878,9 @@ func (q *SelectQuery) scanResult(ctx context.Context, dest ...interface{}) (sql.
return nil, err
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand Down Expand Up @@ -924,6 +933,9 @@ func (q *SelectQuery) Count(ctx context.Context) (int, error) {
return 0, q.err
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

qq := countQuery{q}

queryBytes, err := qq.AppendQuery(q.db.fmter, nil)
Expand Down Expand Up @@ -1028,6 +1040,9 @@ func (q *SelectQuery) Exists(ctx context.Context) (bool, error) {
}

func (q *SelectQuery) selectExists(ctx context.Context) (bool, error) {
// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

qq := selectExistsQuery{q}

queryBytes, err := qq.AppendQuery(q.db.fmter, nil)
Expand All @@ -1047,6 +1062,9 @@ func (q *SelectQuery) selectExists(ctx context.Context) (bool, error) {
}

func (q *SelectQuery) whereExists(ctx context.Context) (bool, error) {
// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

qq := whereExistsQuery{q}

queryBytes, err := qq.AppendQuery(q.db.fmter, nil)
Expand Down
3 changes: 3 additions & 0 deletions query_table_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ func (q *CreateTableQuery) Exec(ctx context.Context, dest ...interface{}) (sql.R
return nil, err
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions query_table_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func (q *DropTableQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Res
}
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions query_table_truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ func (q *TruncateTableQuery) AppendQuery(
//------------------------------------------------------------------------------

func (q *TruncateTableQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions query_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ func (q *UpdateQuery) scanOrExec(
return nil, err
}

// if a comment is propagated via the context, use it
setCommentFromContext(ctx, q)

// Generate the query before checking hasReturning.
queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bun

import (
"context"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -86,3 +87,26 @@ func appendComment(b []byte, name string) []byte {
name = strings.ReplaceAll(name, `*/`, `*\/`)
return append(b, fmt.Sprintf("/* %s */ ", name)...)
}

// queryCommentCtxKey is a context key for setting a query comment on a context instead of calling the Comment("...") API directly
type queryCommentCtxKey struct{}

// ContextWithComment returns a context that includes a comment that may be included in a query for debugging
//
// If a context with an attached query is used, a comment set by the Comment("...") API will be overwritten.
func ContextWithComment(ctx context.Context, comment string) context.Context {
return context.WithValue(ctx, queryCommentCtxKey{}, comment)
}

// commenter describes the Comment interface implemented by all of the query types
type commenter[T any] interface {
Comment(string) T
}

// setCommentFromContext sets the comment on the given query from the supplied context if one is set using the Comment(...) method.
func setCommentFromContext[T any](ctx context.Context, q commenter[T]) {
s, _ := ctx.Value(queryCommentCtxKey{}).(string)
if s != "" {
q.Comment(s)
}
}

0 comments on commit 9870802

Please sign in to comment.