pg_query_go icon indicating copy to clipboard operation
pg_query_go copied to clipboard

Please give some example!!!

Open weiboyiyou opened this issue 7 years ago • 7 comments

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 ?

weiboyiyou avatar Nov 14 '17 06:11 weiboyiyou

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

lfittl avatar Nov 14 '17 06:11 lfittl

Hi,@lfittl, Waiting for your example !!!Thanks a lot.

weiboyiyou avatar Nov 14 '17 07:11 weiboyiyou

@weiboyiyou are you still looking for examples?

elliotcourant avatar Dec 09 '18 00:12 elliotcourant

@elliotcourant a few examples would be pretty helpful

pattarp avatar Mar 27 '19 15:03 pattarp

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 avatar Aug 03 '22 05:08 camilovietnam

@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

lfittl avatar Aug 03 '22 07:08 lfittl