Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Are there any (Linux based) SQL Query Formatting programs/plugins/extensions?

I use PostgreSQL and MySQL but other DB's are welcome as well.

I can use a VM to test with but would prefer a Linux (Ubuntu) based solution.

I have seen a online version but nothing as a installable.

Eclipse Based IDE's are a plus as well

Example:

select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc

to something like

SELECT f1, f2, fname, lName
FROM tblName
WHERE f1 = true
AND fname IS NOT NULL
ORDER BY lName ASC

Here is a online example:

But I would rather this be in a local environment

Related:

UPDATE:

Looking at this:

FINAL UPDATE:

While this might be an overkill Jet Brains has a new Database IDE which has some great re-formatting options

share|improve this question
    
Did you already have a look to the SQLinForm (sqlinform.com) formatter. It is written in Java and thus runs on Linux and has a command line version and available API –  Guido Aug 26 at 20:19

5 Answers 5

up vote 5 down vote accepted

pgAdmin comes with a built in query formatter. You would create a view to see the query formatted like this:

enter image description here

Of course, unquoted mixed case identifiers will be cast to lower case. (But I would consider this a blessing.)

And noise words like ASC in ORDER BY will be trimmed.

And last but not least, the view would have to be valid within the database to begin with. So not exactly what you asked for. Queries are actually reverse engineered from the view definition.

share|improve this answer
    
Sorry i think you misunderstood the question, I can format this myself. What I'm looking for is something to do this in a command or IDE. Example: I would highlight the query and press ctrl + shift + F, this would auto indent, all keyword to UPPER, each command/keyword starts on a new line, etc... So if someone type or scripted this long query I just would like a quick way to format it instead of doing this by hand. Here is a online example: dpriver.com/pp/sqlformat.htm –  Phill Pafford Jan 23 '12 at 18:54
    
@PhillPafford: Yeah, not exactly what you are looking for. The closest thing to a PostgreSQL query formatter I know. Doesn't mean there isn't anything out there. –  Erwin Brandstetter Jan 23 '12 at 19:15
    
Have you tried jetbrains.com/dbe –  Phill Pafford Aug 27 at 13:22

While more generic (SQL instead of pgSQL), fsqlf (http://sourceforge.net/projects/fsqlf/) is a command line or GUI program, open source, to format SQL. It supports having a formatting.conf file which allows you a lot of flexibility in how the final product looks.

Examples:

☺  [wwalker@speedy:~] 
$ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf 

SELECT
  f1
, f2
, fname
, lName
FROM tblName
WHERE f1=true
AND fname is not null
ORDER BY lName asc
☺  [wwalker@speedy:~] 
$ vim formatting.conf # 2 character change
☺  [wwalker@speedy:~] 
$ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf 

SELECT
 f1 ,
 f2 ,
 fname ,
 lName
FROM tblName
WHERE f1=true
AND fname is not null
ORDER BY lName asc
☺  [wwalker@speedy:~] 
$ vim formatting.conf # 1 character change
☺  [wwalker@speedy:~] 
$ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf 

SELECT
 f1 , f2 , fname , lName
FROM tblName
WHERE f1=true
AND fname is not null
ORDER BY lName asc
☺  [wwalker@speedy:~] 
$ 
share|improve this answer

BlancoSqlEditorPlugin is an Eclipse Plugin based SQL formatter. It is written in Java and is OSS.

share|improve this answer
    
Is there an English version of this page? –  dezso Oct 24 '12 at 7:32

MySQL Workbench's query editor natively provides a SQL Query Formatter: you just need to click on the "beautify" icon as shown below (red circle). Note that it doesn't change the case of the SQL operators. (I submitted a bug report.)

enter image description here

share|improve this answer

SQLinForm (http://www.sqlinform.com) is based upon Java and thus can run in Linux environment. They have an API and command line version which you could easily use in your scripts.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.