pg_query_go
pg_query_go copied to clipboard
Please give some example!!!
Hi, please give me some how to use pg_query_go,like pg_query.Parse ,when I get the ParsetreeList , how can i get where the sql is select or update ?
Hi @weiboyiyou,
You would need to use a type assertion to check for the type of node thats contained in the list.
I don't have a good example handy right now, though I can try writing one later this week, let me know if helpful.
If you are looking for a more approachable API, the Ruby library is better documented and easier to use for simple use cases: https://github.com/lfittl/pg_query
Best, Lukas
Hi,@lfittl, Waiting for your example !!!Thanks a lot.
@weiboyiyou are you still looking for examples?
@elliotcourant a few examples would be pretty helpful
The lack of examples really makes it complicated to understand how to work with the parsed struct objects. I've been just trying all the methods in the result.Stmts
objects trying to figure out how to read whether a column statement has a DEFAULT value or not. I feel like 90% of that time has been wasted
@camilovietnam You may find it helpful to look at the Postgres source code directly - all that that pg_query does is package the Postgres internal AST structs in a way that makes them accessible outside the server itself.
For example, regarding the default value: https://github.com/postgres/postgres/blob/master/src/include/nodes/parsenodes.h#L695
All the Go code does is transform that into actual structs - if you search for "ColumnDef" in the protobuf generated code you'll find the relevant parts: https://raw.githubusercontent.com/pganalyze/pg_query_go/main/pg_query.pb.go
There is also auto-generated documentation that may be a bit easier to read: https://pkg.go.dev/github.com/pganalyze/pg_query_go/v2#ColumnDef