-
-
Notifications
You must be signed in to change notification settings - Fork 397
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
Convert an orm.Query to a String #1366
Comments
It might be possible in the future to do something like this, but I believe it was an intentional design decision. But I do not know the exact reason. If you would like to see the queries though as well as other useful information about what go-pg is doing with the database; you can use Wireshark. Wireshark will let you see the queries being sent to the database as well as the responses from the database in real time independent of your code. |
I want to build queries that go-pg isn't capable of building. For example, in a test I want to add |
You can a query hook as soon as you connect to your DB and you can automatically print the formatted query.
and add |
That's very awkward. It would be nice if there was a more straightforward way to do this. |
The correct code is: type dbLogger struct{}
func (d dbLogger) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
return c, nil
}
func (d dbLogger) AfterQuery(c context.Context, q *pg.QueryEvent) error {
fq, _ := q.FormattedQuery()
fmt.Println(string(fq))
return nil
}
// later in your code
db := pg.Connect(opt)
db.AddQueryHook(dbLogger{}) It's awkward but it works like a charm. |
My specific use case: I want to write a test that checks whether or not a database index is used by a query. To do this, I get the query as a string, add |
Effectively, it does prevent perfect concurrency. In pseudo code:
In fact, if you want to test if you must declares indexes, you don't requires that sort of things, you should be able to understand what You should make indexes where you put The best way to test if you need indexes is :
Faster don't means better in all cases. The size of your database is also important. I'm working with Big Data constraints. |
I'm not testing if I need indexes. I'm testing whether or not indexes were used by a query. This is to prevent queries from drifting away from the indexes that were created for them. If a big query ceases to use its indexes, it can cause a huge performance problem. I'm sure there are other reasons people would want to get the query as a string in code in a more sensible way. Please consider implementing this feature. |
query := db.Model(...)
sel := orm.NewSelectQuery(query)
fmt.Println(sel.String()) The API above is not stable and may be changed at any time. Use at your own risk. |
It would be helpful if there were a way to convert an orm.Query object to a string so I can inspect it in tests or alter it before sending it to the database. I'm surprised that I can't find such a feature in this library since it would be useful when generating documentation.
The text was updated successfully, but these errors were encountered: