question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Format SQL strings

See original GitHub issue

With Black and Prettier in place SQL strings are pretty much the only major holdouts in the project I’m working on. Manually enforcing a SQL style is time consuming and boring, format-sql is broken and unmaintained, and there doesn’t seem to be any other options at the moment.

Of course, detecting plain SQL strings is not going to work on its own; there would be too many false positives. What we could do is detect string literals being used in certain specific method calls, such as psycopg2.SQL("""SELECT …"""), and format only those. More advanced versions could support psycopg2.SQL(query) or even psycopg2.SQL(query + optional_where_clause).

Ideally the solution would apply similar rules as in Python to common structures. So SELECT foo, bar would be unchanged, while SELECT foo, [lots of columns], bar would be formatted as something like

SELECT foo,
       first_intermediate_column,
       …,
       bar

or

SELECT
    foo,
    first_intermediate_column,
    …,
    bar

I suspect this is not appropriate as a Black core module because of the dependency on third party packages to determine which strings are actually SQL, but maybe as a plugin?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
zsolcommented, Dec 3, 2019

This is not something that’s likely going to make it into Black itself. Having said that, at my company we use Black’s APIs to parse out all strings and then determine if they’re SQL or not. If they are well-formed SQL statements, we format those using an external binary, shove them back into the AST and then let Black run on the result. So it’s basically the approach you describe. It’s a bit slow because of the need to identify if a string is SQL or not, but in practice it works well enough.

0reactions
louisabrahamcommented, Jan 26, 2022

That’s awesome!!! Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is a Format String in SQL Server? - Database.Guide
A format string is a string that contains one or more predefined format specifiers , which are single characters or groups of characters...
Read more >
SQL Server FORMAT() Function - W3Schools
The FORMAT() function formats a value with the specified format (and an optional culture in SQL Server 2017). Use the FORMAT() function to...
Read more >
FORMAT (Transact-SQL) - SQL Server - Microsoft Learn
Use the FORMAT function for locale-aware formatting of date/time and number values as strings. For general data type conversions, ...
Read more >
String.Format like functionality in T-SQL? - Stack Overflow
I'm looking for a built-in function/extended function in T-SQL for string manipulation similar to the String.Format method in .NET.
Read more >
FORMAT() Function in SQL Server - GeeksforGeeks
The FORMAT() function is one of the String Functions, which is used to format the specified value in the given format. ... Parameter:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found