Skip to content

[FORMATTING] psycopg2 params broken up by formatter #733

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

Closed
ibrokemypie opened this issue Apr 22, 2024 · 2 comments
Closed

[FORMATTING] psycopg2 params broken up by formatter #733

ibrokemypie opened this issue Apr 22, 2024 · 2 comments
Labels

Comments

@ibrokemypie
Copy link

Input data

Which SQL and options did you provide as input?

SELECT
%(param)s param
FROM 
table;

Expected Output

SELECT
  %(param)s param
FROM
  table;

Actual Output

SELECT
  % (param) s param
FROM
  table;

Usage

Using sql-formatter version 15.3.0 through nvim lsp-config with the language set to postgresql. Usage is described by the psycopg2 docs here https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries

Seems like this is the pyformat style from the PEP 249 standard https://peps.python.org/pep-0249/#paramstyle. Found #545 (comment) but as I am using a higher version already I believe this is a separate issue.

@nene
Copy link
Collaborator

nene commented Apr 22, 2024

I think you misread the issue you're referencing. What was implemented in the context of it wasn't support for PEP 245 (or whatever other parameter syntax). What was actually implemented was a way to define custom SQL parameters syntax.

@nene nene closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2024
@ibrokemypie
Copy link
Author

To anyone who ends up here in the future, I got it working with this monstrosity (config for conform.nvim)

formatters = {
sql_formatter = {
	args = {
		"-l",
		"postgresql",
		"--config",
		"{ "
		.. '"keywordCase": "upper", '
		.. '"paramTypes": { '
		.. '"custom": ['
		.. "{ "
		.. [["regex": "%\\(.+\\)s"]]
		-- .. '"key": "(text) => text.slice(2, -2)"'
                .. " },"                                    
                .. "{ "                                     
                .. [["regex": "%s"]]                        
                .. " }"                                     
                .. "] "                                     
		.. "} "
		.. "}",
	},
    },
},

I wasn't able to get key working as it seems to require a function which I don't know if it is possible to encode in json, but as I am not using sql-formatter's param substitution (using psycopg2's) I think thats okay.

Both of these now format correctly

SELECT
  %s foo
  %(bar)s
FROM
  table;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants