sql-formatter

Build Status Maven Central codecov.io Scala.js Scala.js

Scala port of great SQL formatter https://github.com/zeroturnaround/sql-formatter, https://github.com/vertical-blank/sql-formatter.

Written with only Scala Standard Library, without dependencies.

Demo

Usage

Scala (on JVM)

libraryDependencies += "com.github.takayahilton" %% "sql-formatter" % "1.2.1"

Scala.js

libraryDependencies += "com.github.takayahilton" %%% "sql-formatter" % "1.2.1"

Scala Native

libraryDependencies += "com.github.takayahilton" %%% "sql-formatter" % "1.2.1"

Examples

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

import com.github.takayahilton.sqlformatter._

SqlFormatter.format("SELECT * FROM table1")

This will output:

SELECT
  *
FROM
  table1

Dialect

You can pass dialect name to SqlFormatter.of :

import com.github.takayahilton.sqlformatter._

SqlFormatter.of(SqlDialect.CouchbaseN1QL).format("SELECT *")

Currently just four SQL dialects are supported:

Format

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

import com.github.takayahilton.sqlformatter._

SqlFormatter.format(
  "SELECT * FROM table1",
  indent = "    ")

This will output:

SELECT
    *
FROM
    table1

Placeholders replacement

You can pass Seq or Map to format :

import com.github.takayahilton.sqlformatter._

// Named placeholders
SqlFormatter.format("SELECT * FROM tbl WHERE foo = @foo", params = Map("foo" -> "'bar'"))

// Indexed placeholders
SqlFormatter.format("SELECT * FROM tbl WHERE foo = ?", params = Seq("'bar'"))

Both result in:

SELECT
  *
FROM
  tbl
WHERE
  foo = 'bar'