Describe the feature
Support for the SQLCommenter tagging system, used by Query Insights on Google Cloud SQL.
Motivation
We are in the process of analyzing and optimizing all requests on our PostgreSQL, having the ability to tags each request to a specific app (or one controller/endpoint) would be really helpful.
Right now, GORM provide an easy way to add a comment after a specific clause, but not at the end of the entire statement.
Related Issues
Comment From: a631807682
Right now, GORM provide an easy way to add a comment after a specific clause, but not at the end of the entire statement.
You can handle it by registering specified clauses at the end. https://github.com/go-gorm/mysql/blob/master/mysql.go#L151 https://github.com/go-gorm/hints/blob/master/hints.go#L23 This is a similar implementation
This feature should be supported by a plugin, would you like to create a plugin for SQLCommenter ?
Comment From: gouyelliot
Thanks for the example, I did not realise it was doable using the Plugin interface. I'll keep in touch if we decide to make one we can share.
Comment From: insomnius
I tried with hints plugin, but it's just not doable and not covering all of the cases because of this gorm "callbacks". For example, we can create a plugin to add comment for query callback but not when the query is only doing "select * from x", the comment would not generate because of "FROM" clause is formed inside a query and there is no way to modify that.
What I want to suggest is maybe adding a hook before query execution statement, currently this is what we have:
https://github.com/insomnius/gorm/blob/835d7bde59a24ac769a1c5ded206b58f7cedfba3/callbacks/callbacks.go#L40-L74
My suggestion is adding new callback like "gorm:before_query_execution" to modify the complete SQL statement inside a plugin
Comment From: HoangViet144
For those who still gets stuck when integrating SQLCommenter with gorm, we can init gorm like this:
dbConnPool, err := gosql.Open("mysql", "connection string", sqlcommentercore.CommenterOptions{
Config: sqlcommentercore.CommenterConfig{
EnableDBDriver: true,
EnableRoute: true,
EnableFramework: true,
EnableAction: true,
EnableTraceparent: true,
EnableApplication: true,
},
Tags: sqlcommentercore.StaticTags{
Application: "service name",
},
})
if err != nil {
panic(err)
}
db, err = gorm.Open(mysql.New(mysql.Config{
Conn: dbConnPool,
}), &gorm.Config{})
Comment From: Liudon
I tried with hints plugin, but it's just not doable and not covering all of the cases because of this gorm "callbacks". For example, we can create a plugin to add comment for query callback but not when the query is only doing "select * from x", the comment would not generate because of "FROM" clause is formed inside a query and there is no way to modify that.
What I want to suggest is maybe adding a hook before query execution statement, currently this is what we have:
https://github.com/insomnius/gorm/blob/835d7bde59a24ac769a1c5ded206b58f7cedfba3/callbacks/callbacks.go#L40-L74
My suggestion is adding new callback like "gorm:before_query_execution" to modify the complete SQL statement inside a plugin
https://liudon.com/posts/gorm-supports-sqlcommenter/
use custom callback to support sqlcommenter