-
-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add mechanism for prefixing queries with a comment string #1002
base: master
Are you sure you want to change the base?
Conversation
query_base.go
Outdated
q.comment = c | ||
} | ||
|
||
func (q *baseQuery) appendComment(b []byte) []byte { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please rework this to accept ctx context.Context
and then extract the comment from the context in this function?
Then we can remove commentFromContext
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @vmihailenco! Updated as you requested and rebased on top of master.
@kylemcc This looks useful, but I have a minor refactoring request. Please take a look when you have time. |
Signed-off-by: Kyle McCullough <[email protected]>
if c, _ := ctx.Value(queryCommentCtxKey{}).(string); c == "" { | ||
return b | ||
} else { | ||
q.comment = c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please change this so it does not modify the baseQuery
? So the following is possible:
q := NewSelectQuery()
q.AppendQuery(ctxWithComment1) // one comment
q.AppendQuery(ctxWithComment2) // another comment
Also, one might think that both comments must be appended: one from the query and one from the context. I will leave it to you to decide what to do in such case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If multiple comments are allowed, then we can also consider supporting this: NewSelect().Comment("comment1").Comment("comment2")
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please change this so it does not modify the
baseQuery
? So the following is possible:q := NewSelectQuery() q.AppendQuery(ctxWithComment1) // one comment q.AppendQuery(ctxWithComment2) // another comment
Also, one might think that both comments must be appended: one from the query and one from the context. I will leave it to you to decide what to do in such case.
Ah, yes, good catch. Didn't think about the AppendQuery
use case.
If multiple comments are allowed, then we can also consider supporting this: NewSelect().Comment("comment1").Comment("comment2").
This seems reasonable, too. Let me experiment with the formatting a bit to see what looks best.
@kylemcc any chance you can finish this? |
This change proposes a new
Comment(string)
method andContextWithComment(context.Context, string)
function for prepending a comment to a SQL query.Here's simple example:
This would produce a query that looks like:
This may warrant a bit of discussion first, but I wanted to include the code with the proposal. I have had this code running in production since April with no problems. It appears that others may be interested in this as well: #998
My immediate use case for this is to add a comment to a query with tracing/debug information that will show up in the process list/
pg_stat_activity
, but there are certainly other uses for this functionality.