-
Notifications
You must be signed in to change notification settings - Fork 421
Feature Request: SQL in SQL prettification #680
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
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 |
While this issue mainly focuses on sql-in-sql, I want to note that dollar quoted strings can also be used e.g. for comments: comment on schema "public" is $$
Here is my comment.
$$; There are a lot of tools that rely on special comment syntax (e.g. Supabase and Postgraphile) that may break if the comment formatting changes! That said, I really do want formatting of dollar quoted sql - that's why I'm here to begin with. While not perfect, perhaps an acceptable MVP to get the feature started would be a magical opt-in comment (similarly to other tools such as eslint), e.g. create or replace function "foo" () returns text as $$
-- sql-formatter-optin dollar-quoted-sql
select 'bar'::text;
$$ language sql stable; |
FYI, this feature is unlikely to get implemented as I'm no more doing any real feature development on this library and instead concentrating on prettier-plugin-sql-cst. If you really want this feature in SQL Formatter, you should consider writing a pull request. But I admit that it's no small feat to pull this off - SQL Formatter is pretty limited in it's architecture. It would be simpler to instead contribute that feature to prettier-plugin-sql-cst, which already implements very similar feature for BigQuery. Or just raise a feature request there and I'm more likely to implement it. |
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: