Java port of great SQL formatter https://github.com/zeroturnaround/sql-formatter.
Written with only Java Standard Library, without dependencies.
Demo is running on Google Cloud Function, with native-compiled shared library by GraalVM.
<dependency>
<groupId>com.github.vertical-blank</groupId>
<artifactId>sql-formatter</artifactId>
<version>1.0</version>
</dependency>
implementation 'com.github.vertical-blank:sql-formatter:1.0'
You can easily use com.github.vertical_blank.sqlformatter.SqlFormatter
:
SqlFormatter.format("SELECT * FROM table1")
This will output:
SELECT
*
FROM
table1
You can pass dialect name to SqlFormatter.of
:
SqlFormatter
.of("n1ql") // Defaults to "sql"
.format("SELECT *");
Currently just four SQL dialects are supported:
- sql - Standard SQL
- n1ql - Couchbase N1QL
- db2 - IBM DB2
- pl/sql - Oracle PL/SQL
Defaults to two spaces.
You can pass indent string to format
:
SqlFormatter.format("SELECT * FROM table1", " ");
This will output:
SELECT
*
FROM
table1
You can pass List or Map to format
:
// Named placeholders
Map<String, String> namedParams = new HashMap<>();
namedParams.put("foo", "'bar'");
SqlFormatter.format("SELECT * FROM tbl WHERE foo = @foo", namedParams);
// Indexed placeholders
SqlFormatter.format("SELECT * FROM tbl WHERE foo = ?", Arrays.asList("'bar'"));
Both result in:
SELECT
*
FROM
tbl
WHERE
foo = 'bar'