Skip to content
SQL formatter written with only Java Standard Library, without dependencies.
Java Groovy
Branch: master
Clone or download
Latest commit 56eafdb Oct 4, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
demo add demo sources May 2, 2019
src gradle to maven Apr 30, 2019
.gitignore gradle to maven Apr 30, 2019
.travis.yml gradle to maven Apr 30, 2019
LICENSE Update LICENSE Oct 5, 2019
README.md fix link May 8, 2019
pom.xml

README.md

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'
You can’t perform that action at this time.