Closed dineshtessell closed 1 year ago
@dineshtessell Thanks for reaching out! Have you looked at the Normalize function? (https://pkg.go.dev/github.com/pganalyze/pg_query_go/v4#Normalize)
That replaces literal values with parameter references ($1
, etc). The motivation for using parameter references like $1
and not ?
is for the query to remain parseable by the parser (?
is an operator in Postgres, and thus introduces ambiguity into the query).
(side note: I just realized that the code comment for that function is out of date - we used to use ?
a long time ago, but no longer do)
Thank you so much @lfittl. This truly helps
For others reference,
package main
import ( "fmt"
pg_query "github.com/pganalyze/pg_query_go/v4"
)
func main() {
tree, err := pg_query.Normalize(SELECT name, email FROM "users", xyz WHERE name = 'John Doe' AND age > 30;
)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", tree)
}
Output SELECT name, email FROM "users", xyz WHERE name = $1 AND age > $2;
I am looking for a way, how we can achieve all the SQLValues to be replaced with the placeholders like "?". Is there any example for it.
For example, SELECT name, email FROM users, xyz WHERE name = 'John Doe' AND age > 30
Should be changed to SELECT name, email FROM users, xyz WHERE name = ? AND age > ?
We are able to acehive this MySQL package as below, and for PostgreSQL we are looking.
MySQL
ubuntu@foo:~/sql_parser$ go run main.go Found literal: John Doe Found literal: 30 select name, email from users, xyz where name = ? and age > ?