-
Notifications
You must be signed in to change notification settings - Fork 391
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
Feature Request: SQL in SQL prettification #680
Comments
It's a feature that would be definitely nice to have. However, there are several obstacles to implementing it.
See readme for information about another formatting library, which does support that, though not yet for Postgres. It implements a similar feature for BigQuery, and makes use of Prettier API to e.g. format Python code inside strings. |
When it comes to the issue of "what is in this quoted string", I think that can be addressed in a well-defined way, since a function definition is going to have a In my particular case, I would be happy to change every You are right that if you implemented this, my next request would be formatting So I think I will continue ignoring this problem for now, because I think that what needs to happen is for Thanks for explaining the lay of the land! |
The default language case actually complicates this problem even more, as then we'd need to detect whether the string occurs inside a CREATE FUNCTION statement, which is not a trivial thing, because the formatter doesn't really understand the context. The good news is that Prettier 3 support in |
I guess I assumed that the formatter has access to enough of the AST to be able to tell when it's inside a But even so, I'd be happy to change my |
Asking users to modify their SQL to suit the formatter should really be the last resort. I think there are better approaches to make it work for most cases. Like detecting whether a string is followed by |
Describe the Feature
I want formatting of SQL inside function bodies written in quoted stings
Why do you want this feature?
If you are writing SQL functions, it's pretty common to have SQL embedded inside SQL; for example (straight out of Postgres documentation):
In this case,
$$
is a string quoting delimiter, andSELECT $1, CAST($1 AS text) || ' is text'
is a quoted string.The issue is that most SQL formatters do not recurse into these quoted string function bodies, so the function declaration is prettified but the function implementation remains ugly. Here's what I get if I run the above through
prettier
withprettier-plugin-sql
:What I wish I got would be more like
(Which is what I get if I manually run the function body through
prettier-plugin-sql
and then paste it inside$$
delimiters.)The text was updated successfully, but these errors were encountered: