-
-
Notifications
You must be signed in to change notification settings - Fork 415
-
I tried to get query field from struct QueryEvent using of method UnformattedQuery() or FormattedQuery(), but I didnot get any solution for this. Any solution ??
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 6 comments 1 reply
-
Can you share some example code to help us better understand? Youre looking to see if a specific field is part of a query?
Beta Was this translation helpful? Give feedback.
All reactions
-
How to find Query from QueryEvent
I have following object
evt *pg.QueryEvent
to get query in string format I used following methods
sqlQuery, _ := evt.FormattedQuery()
println("post query ",string(sqlQuery))
sqlQuery, _ := evt.UnformattedQuery()
println("post query ",string(sqlQuery))
In both the case string(sqlQuery) return empty string
Beta Was this translation helpful? Give feedback.
All reactions
-
How are you building your query perhaps? If you are passing the query as a raw string; like:
var count int _, err := db.Model((*Book)(nil)).QueryOne(pg.Scan(&count), ` SELECT count(*) FROM ?TableName AS ?TableAlias `)
Where the query is not built by pg; then formatter/unformatted query I don't think will return anything.
Instead the query string is stored like this:
switch query := event.Query.(type) { case string: query = strings.TrimSpace(query) query = strings.ReplaceAll(query, "\n", " ") ...
Where the query itself is actually on event.Query
. But only if the type of event.Query
is a string.
You can seen an example of this here: https://github.com/monetr/monetr/blob/d121b42b21631c47ad6d03067a241e68667777f9/pkg/logging/pg.go#L44-L130
Where I'm using the after hook to log the query and some other metrics for Sentry.
Beta Was this translation helpful? Give feedback.
All reactions
-
My code is as follows:
DB_USER, DB_PASSWORD := os.Getenv("POSTGRES_USER"), os.Getenv("POSTGRES_PASS") if DB_USER == "" && DB_PASSWORD == "" { DB_USER = "postgres" DB_PASSWORD = "12345678" } db := pg.Connect(&pg.Options{ User: DB_USER, Password: DB_PASSWORD, }) defer db.Close() var student []Student _, err := db.Query(&student, `SELECT * FROM student`) _, err = db.Query(&stu, `SELECT * FROM student WHERE Rollno = ?`, 1)
I also tried the above link but queryType is coming UNKNOWN.
Beta Was this translation helpful? Give feedback.
All reactions
-
How are you binding the query hooks? I can also post a more thorough example using the code you included this evening.
Beta Was this translation helpful? Give feedback.
All reactions
-
Sorry I have not forgotten about this I just have not had time yet.
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks for given solution. Now I get querys.
Beta Was this translation helpful? Give feedback.