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

Function for parsing both sql and plpgsql? #148

Open
rex-remind101 opened this issue Jul 19, 2022 · 3 comments
Open

Function for parsing both sql and plpgsql? #148

rex-remind101 opened this issue Jul 19, 2022 · 3 comments
Labels

Comments

@rex-remind101
Copy link

I tried using pg_query::parse (rust) but the body of plpgsql functions comes up as a string "begin <SQL stuff> end". Moreover to use parse_plpgsql I have to use a full function definition, not just what is in between begin and end. Is there a way to parse both of these, a full fledged sql file that will have queries, triggers, and functions and fully parse the function bodies?

Thanks you

@lfittl
Copy link
Member

lfittl commented Jul 19, 2022

@rex-remind101 There isn't any function like that today - technically it should be possible to add, but the challenge is with the resulting parse tree (the regular SQL AST is independent of the PL/pgSQL AST).

The easiest approach today, which is a bit of a hack, would be to parse the SQL first, then identify the relevant CreateFunctionStmts and then either (1) Use the location field of the top-level node to identify where the statement starts/end in the source string, (2) use the deparser (which is pretty complete) to turn the function definition back into SQL, and then pass that to the PL/pgSQL parser.

The easiest way to improve this (different than your suggestion), would be to have a function that takes a CreateFunctionStmt parse node and does the PL/pgSQL parsing based on that. PRs welcome :)

@rex-remind101
Copy link
Author

I will give the deparser method a shot for now, thank you.

@aight8
Copy link

aight8 commented Sep 10, 2022

ah this issue already exists here.
I created a duplicate in the bad repo: pganalyze/pg_query#254

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

3 participants