Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Get query from UnformattedQuery() or FormattedQuery() #1971

Unanswered
abhi9560 asked this question in Q&A
Discussion options

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 ??

You must be logged in to vote

Replies: 6 comments 1 reply

Comment options

Can you share some example code to help us better understand? Youre looking to see if a specific field is part of a query?

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

How are you binding the query hooks? I can also post a more thorough example using the code you included this evening.

You must be logged in to vote
1 reply
Comment options

Sorry I have not forgotten about this I just have not had time yet.

Comment options

Thanks for given solution. Now I get querys.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /