Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please give some example!!! #10

Open
weiboyiyou opened this issue Nov 14, 2017 · 7 comments
Open

Please give some example!!! #10

weiboyiyou opened this issue Nov 14, 2017 · 7 comments
Labels

Comments

@weiboyiyou
Copy link

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 ?

@lfittl
Copy link
Member

lfittl commented Nov 14, 2017

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

@weiboyiyou
Copy link
Author

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

@elliotcourant
Copy link

@weiboyiyou are you still looking for examples?

@pattarp
Copy link

pattarp commented Mar 27, 2019

@elliotcourant a few examples would be pretty helpful

@camilovietnam
Copy link

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

@lfittl
Copy link
Member

lfittl commented Aug 3, 2022

@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 lfittl added the question label Jan 3, 2024
@arikkfir
Copy link

For the authors of this library - this is such a useful library, thank you!

I do agree with the need for some useful examples, despite the fact that this library simply exposes some PG internals, for the reason that this library would serve as the API for potential users. Thus, this is where people expect to find such examples.

I also think that some examples here would go a long long way towards more widespread adoption of the library, assuming lack of examples coupled with relative complexity of the API serves as the biggest potential barrier for adoption here (few people are familiar with internal PG ASTs...)

Some ideas for examples:

  • Modify a query's WHERE clause to add conditions (e.g. to be used in a REST service with filters for a SELECT query...)
  • Modify a query's ORDER BY clause
  • Change a SELECT a, b, c clause to a SELECT COUNT(*) clause

These are use-cases I see every-day and can be useful in a variety of contexts.

Regardless, thanks again for writing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants