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

Enhancement: SQL in SQL prettification #44

Closed
innermatrix opened this issue Dec 1, 2023 · 5 comments
Closed

Enhancement: SQL in SQL prettification #44

innermatrix opened this issue Dec 1, 2023 · 5 comments

Comments

@innermatrix
Copy link

I am guessing that you will probably find this to be out of scope of your plugin, but maybe not, so I figure I'd ask:

If you are writing SQL functions, it's pretty common to have SQL embedded inside SQL; for example (straight out of Postgres documentation):

CREATE FUNCTION dup(int) RETURNS dup_result
    AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$
    LANGUAGE SQL;

In this case, $$ is a string quoting delimiter, and SELECT $1, CAST($1 AS text) || ' is text' is a quoted string.

The issue is that most SQL formatters (and in particular, the one used by prettier-plugin-embed 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 with prettier-plugin-sql (regardless of whether prettier-plugin-embed is on):

create function dup (int) returns dup_result as $$ SELECT $1, CAST($1 AS text) || ' is text' $$ language sql;

What I wish I got would be more like

create function dup (int) returns dup_result as $$ 
    select
        $1,
        cast($1 as text)||' is text'
$$ language sql;

(That's what I get if I manually run the function body through prettier-plugin-sql and then paste it inside $$ delimiters.

Now, on the one hand, I realize that the description of prettier-plugin-embed specifically says that its only intent is to handle tagged strings in TS/JS, so if you just want to turn this idea away, I get that. But if you're looking for another cool thing to do in the realm of embedded string prettification, well, then I think this one is a good candidate 🙂

@Sec-ant
Copy link
Owner

Sec-ant commented Dec 1, 2023

If language A can embed language B (A and B can be the same language), then the formatting support of the embedded B should be added in the plugin of A, because the AST/CST information of A is needed to recognize an embedded node. Prettier has a dedicated API embed for plugins to do this.

So I think this should be a feature request for prettier-plugin-sql. cc @JounQin

@innermatrix
Copy link
Author

Gotcha, ty for explaining

@JounQin
Copy link

JounQin commented Dec 1, 2023

@innermatrix
Copy link
Author

@Sec-ant
Copy link
Owner

Sec-ant commented Dec 25, 2023

There's nothing we can do in this repo currently, so let's track this feature in the following links:

@Sec-ant Sec-ant closed this as completed Dec 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants