Giter Site home page Giter Site logo

willam2004 / sql-formatter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vertical-blank/sql-formatter

0.0 1.0 0.0 410 KB

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

License: MIT License

Java 69.85% Kotlin 30.15%

sql-formatter's Introduction

sql-formatter

Maven Central Java CI with Maven 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.

This does not support:

  • Stored procedures.
  • Changing of the delimiter type to something else than ;.

Usage

Maven

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

Gradle

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

Examples

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

SqlFormatter.format("SELECT * FROM table1")

This will output:

SELECT
  *
FROM
  table1

You can also pass FormatConfig object built by builder:

SqlFormatter.format('SELECT * FROM tbl',
  FormatConfig.builder()
    .indent("    ") // Defaults to two spaces
    .uppercase(true) // Defaults to false (not safe to use when SQL dialect has case-sensitive identifiers)
    .linesBetweenQueries(2) // Defaults to 1
    .maxColumnLength(100) // Defaults to 50
    .params(Arrays.asList("a", "b", "c")) // Map or List. See Placeholders replacement.
    .build()
);

Dialect

You can pass dialect com.github.vertical_blank.sqlformatter.languages.Dialect or String to SqlFormatter.of :

SqlFormatter
    .of(Dialect.N1ql)  // Recommended
     //.of("n1ql")      // String can be passed
    .format("SELECT *");

SQL formatter supports the following dialects:

Extend formatters

Formatters can be extended as below :

SqlFormatter
    .of(Dialect.MySql)
    .extend(cfg -> cfg.plusOperators("=>"))
    .format("SELECT * FROM table WHERE A => 4")

Then it results in:

SELECT
  *
FROM
  table
WHERE
  A => 4

Placeholders replacement

You can pass List or Map to format :

// Named placeholders
Map<String, String> namedParams = new HashMap<>();
namedParams.put("foo", "'bar'");
SqlFormatter.of(Dialect.TSql).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'

Build

Building this library requires JDK 11 because of ktfmt.

sql-formatter's People

Contributors

vertical-blank avatar niveditha-phdata avatar dependabot[bot] avatar vatsalmevada avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.