Skip to content

Niveditha-phData/sql-formatter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.1</version>
</dependency>

Gradle

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

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'

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 53.9%
  • Groovy 46.1%