/sql-formatter

SQL formatter written with only Java Standard Library, without dependencies.

Primary LanguageJavaMIT LicenseMIT

sql-formatter

Maven Central travis codecov

Java port of great SQL formatter https://github.com/zeroturnaround/sql-formatter.

Written with only Java Standard Library, without dependencies.

Demo

Demo is running on Google Cloud Function, with native-compiled shared library by GraalVM.

Usage

Maven

<dependency>
  <groupId>com.github.vertical-blank</groupId>
  <artifactId>sql-formatter</artifactId>
  <version>1.0</version>
</dependency>

Gradle

implementation 'com.github.vertical-blank:sql-formatter:1.0'

Examples

You can easily use com.github.vertical_blank.sqlformatter.SqlFormatter :

SqlFormatter.format("SELECT * FROM table1")

This will output:

SELECT
  *
FROM
  table1

Dialect

You can pass dialect name to SqlFormatter.of :

SqlFormatter
    .of("n1ql")  // Defaults to "sql"
    .format("SELECT *");

Currently just four SQL dialects are supported:

Format

Defaults to two spaces. You can pass indent string to format :

SqlFormatter.format("SELECT * FROM table1", "    ");

This will output:

SELECT
    *
FROM
    table1

Placeholders replacement

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'