senkenn / sqlsurge

Visual Studio Code extension for SQL language server
https://marketplace.visualstudio.com/items?itemName=senken.sqlsurge
MIT License
18 stars 0 forks source link

Customize formatting #105

Closed musjj closed 3 weeks ago

musjj commented 1 month ago

It would be nice if you can customize sql-formatter. For example to automatically have uppercase keywords:

.sql-formatter.json

{
  "dialect": "postgresql",
  "keywordCase": "upper",
  "dataTypeCase": "upper"
}

It seems that the config file is ignored currently.

senkenn commented 1 month ago

ok! i’ll try.

senkenn commented 1 month ago

Hmm, I also think it might be best to use the format feature of LSP. Let me be discreet, please.

senkenn commented 1 month ago

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.

musjj commented 1 month ago

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.

senkenn commented 3 weeks ago

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.

senkenn commented 3 weeks ago

@musjj try v0.6.0!

musjj commented 3 weeks ago

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.

musjj commented 2 weeks ago

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?;
senkenn commented 2 weeks ago

@musjj Use paramTypes !


{
  "dialect": "postgresql",
  "keywordCase": "upper",
  "dataTypeCase": "upper",
+ "paramTypes": {
+   "numbered": ["$"]
+ }
}
musjj commented 2 weeks ago

Thank you, it works perfectly now!

musjj commented 2 weeks ago

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": ["$"]
- }