31

I am looking for a Java open source beautifier or reformatter for SQL that I can use to clean up DDL statements that I am generating with openArchitectureWare.

Nothing in the answer to "Online Code Beautifier And Formatter" is of use to me and I have not been able to get Simple SQL Formatter to work for me.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Brian Matthews
  • 8,506
  • 7
  • 46
  • 68

8 Answers8

55

UPDATE 2:

org.hibernate.jdbc.util.BasicFormatterImpl got moved in release 4.0. It is now located at: org.hibernate.engine.jdbc.internal.BasicFormatterImpl.

UPDATE 1:

Technology marches on. As noted by Alex, org.hibernate.pretty.Formatter no longer exists as of version 3.3.2.GA. The replacement is org.hibernate.jdbc.util.BasicFormatterImpl:

String formattedSQL = new BasicFormatterImpl().format(sql);

ORIGINAL ANSWER:

If you're using Hibernate, they've got one built-in: org.hibernate.pretty.Formatter

String formattedSQL = new Formatter(sql).format();
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Robert J. Walker
  • 10,027
  • 5
  • 46
  • 65
  • 1
    Or DDLFormatter, which will a) Insert newline after each comma; b) Indent three spaces after each inserted newline; – Stephen Denne Feb 17 '09 at 23:44
  • This would appear to be exactly what I am looking for. Hibernate is already a core component of my solution so its not introducing a new dependency. – Brian Matthews Feb 18 '09 at 18:17
  • 1
    FYI, BasicFormatterImpl does not handle comments '--' and DDLFormatter is even more primitive see http://www.dil.univ-mrs.fr/~massat/docs/hibernate-3.1/api/org/hibernate/pretty/DDLFormatter.html#format() – Karl Dec 06 '13 at 19:26
  • SQLinForm at www.sqlinform.com is handling comments too and has many formatting options. – Guido Aug 24 '14 at 18:24
  • 2
    I created a standalone SQL beautifulier tool which uses `BasicFormatterImpl` and `DDLFormatterImpl`. https://github.com/lbtc-xxx/sql-beautifulier – Kohei Nozaki Jan 24 '16 at 08:29
  • Check out SQL Formatter by Yohei Yyamana. It's Java, open source, zero-dependencies, and already in maven central! https://github.com/vertical-blank/sql-formatter – Choppy The Lumberjack Sep 25 '19 at 11:44
6

With Hibernate v3.3.2.GA, org.hibernate.pretty.Formatter doesn't exist anymore. You can use its replacement : org.hibernate.jdbc.util.BasicFormatterImpl

Use org.hibernate.engine.jdbc.internal.BasicFormatterImpl for Hibernate 4.0+.

Formatter f = new BasicFormatterImpl();
String formatted_sql_code = f.format(ugly_sql_code);
Stephan
  • 41,764
  • 65
  • 238
  • 329
5

Part of the eclipse Data Tools Platform is the SQL Development Tools Project.

The page describing how to use the SQL Query Parser has an extremely brief use of SQLQuerySourceFormat which provides these options:

  • preserveSourceFormat = the option to preserve the input source formating when SQL source text is generated
  • statementTerminator = the character separating multiple SQL statements
  • hostVariablePrefix = the character that preceedes a host language variable
  • parameterMarker = the character that identifies a host language parameter
  • delimitedIdentifierQuote* = the character that encloses delimited identifiers whose writing in case will be preserved
  • omitSchema = the current schema (omitted in SQL source, implicit to unqualified table references)
  • qualifyIdentifiers = the flag describing how identifiers in the SQL source will be qualified
  • preserveComments = the option to preserve comments in the parsed SQL source or/and the generated SQL source
  • generateCommentsForStatementOnly = the option to generate comments for the SQL source only in the context of the complete statement, or if set to false, for single SQL Query objects outside the context of a statement as well
Stephen Denne
  • 36,219
  • 10
  • 45
  • 60
  • This solution would definitely work for me but I prefer the simplicity of the Hibernate solution. A pity I could only give you a +1 – Brian Matthews Feb 18 '09 at 18:18
2

Have you considered:

http://www.sqlinform.com

They provide both an API version and a command line version (as well as an online version).

No knowledge of costs though.

Community
  • 1
  • 1
Guy
  • 9,720
  • 7
  • 38
  • 42
1

Perhaps jsqlparser will work for you.

Not as easy to find as you might think, as there are a fair few defunct projects out there. In fact i failed to find it so ended up doing my own thing (based on the h2 parser - you can contact me if all else fails). As a consequence i do not know if it has a beautifier, but writing one on top should be straight forward enough.

Its based on a grammar and JavaCC, so probably a better option than reinventing this wheel with antlr in any event. You may find if you need to support various dialects of sql in complex statements that any approach based on a grammar will fail you.

mike g
  • 1,791
  • 1
  • 18
  • 25
1

Would this work - SQL Formatter.

Ferdeen
  • 21,332
  • 6
  • 29
  • 31
  • 1
    I need to be able to do this off-line as after code generation step in my build process. So SQL Formatter is not an option for me. – Brian Matthews Feb 18 '09 at 18:07
0

You could just use a SQL grammar and build the AST with antlr. Then you can output the tree in any format you like.

tcurdt
  • 14,518
  • 10
  • 57
  • 72
  • Thanks. That will be my plan of last resort. Fortunately I am too busy with a new job to look at this now and hopefully I'll get an answer before I have to roll up my sleeves. – Brian Matthews Feb 11 '09 at 18:53
0

So this is definitely what you are looking for: A SQL formatter library that support Oracle, SQL Server, DB2, MySQL, Teradata and PostgreSQL.

James Wang
  • 453
  • 4
  • 5