Closed musjj closed 3 weeks ago
ok! i’ll try.
Hmm, I also think it might be best to use the format feature of LSP. Let me be discreet, please.
Using the LSP formatter feels more consistent with the SQL in the SQL file and the raw SQL in the other files, and feels like the right way to go.
LSP formatters are great, but the nice thing about external formatters is that you can trigger them in a CI. Though I guess you currently can't use the formatter for embedded sql strings.
you currently can't use the formatter for embedded sql strings.
This is true, and I looked into LSP formatters again, but no one offered a format with CLI... So I decided to continue using sql-formatter and support customizing it.
@musjj try v0.6.0!
Thank you! I can't test this right now due to an issue on Nix, but I'll test it ASAP once it gets fixed.
Hmmm I tried 0.7.0
, but I'm getting this error:
[commandFormatSqlProvider] Error: Parse error: Unexpected "$1, $2) RE" at line
3 column 70. This likely happens because you're using the default "sql"
dialect. If possible, please select a more specific dialect (like sqlite,
postgresql, etc).
I already have a .sql-formatter.json
in my directory:
{
"dialect": "postgresql",
"keywordCase": "upper",
"dataTypeCase": "upper"
}
This is my query:
INSERT INTO users (name, username) VALUES ($1, $2) RETURNING *
It's embedded inside rust code:
sqlx::query!(
r#"
INSERT INTO users (name, username) VALUES ($1, $2) RETURNING *
"#,
name,
username
)
.fetch_one(&pool)
.await?;
@musjj Use paramTypes !
{
"dialect": "postgresql",
"keywordCase": "upper",
"dataTypeCase": "upper",
+ "paramTypes": {
+ "numbered": ["$"]
+ }
}
Thank you, it works perfectly now!
Sorry for another question, but I'm getting another error with this query:
INSERT INTO
users (name)
SELECT
*
FROM
UNNEST ($1::text[])
[commandFormatSqlProvider] Error: Parse error: Unexpected "::text[]) " at line
7 column 21. This likely happens because you're using the default "sql"
dialect. If possible, please select a more specific dialect (like sqlite,
postgresql, etc).
It's a valid postgresql syntax though. I"m guessing that it's ignoring the dialect setting?
EDIT: Ah, I think I figured it out, you need to add language
to the config:
{
"language": "postgresql"
}
With this, you can also remove this:
- "paramTypes": {
- "numbered": ["$"]
- }
It would be nice if you can customize sql-formatter. For example to automatically have uppercase keywords:
.sql-formatter.json
It seems that the config file is ignored currently.