An open API service indexing awesome lists of open source software.

https://github.com/takayahilton/sql-formatter

SQL Formatter for Scala.
https://github.com/takayahilton/sql-formatter

scala scala-js scala-native scalajs sql sql-formatter

Last synced: 28 days ago
JSON representation

SQL Formatter for Scala.

Awesome Lists containing this project

README

          

# sql-formatter

[![Build Status](https://travis-ci.org/takayahilton/sql-formatter.png?branch=master)](https://travis-ci.org/takayahilton/sql-formatter)
[![Maven Central](https://img.shields.io/maven-central/v/com.github.takayahilton/sql-formatter_2.12.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.takayahilton%22%20AND%20a:%22sql-formatter_2.12%22)
[![codecov.io](http://codecov.io/github/takayahilton/sql-formatter/coverage.svg?branch=master)](https://codecov.io/gh/takayahilton/sql-formatter/branch/master)
[![Scala.js](https://www.scala-js.org/assets/badges/scalajs-0.6.29.svg)](#scalajs)
[![Scala.js](https://www.scala-js.org/assets/badges/scalajs-1.2.0.svg)](#scalajs)

Scala port of great SQL formatter , .

Written with only Scala Standard Library, without dependencies.

[Demo](https://takayahilton.github.io/sql-formatter/)

## Usage

### Scala (on JVM)

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

### Scala.js

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

### Scala Native

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

### Examples

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

```scala
import com.github.takayahilton.sqlformatter._

SqlFormatter.format("SELECT * FROM table1")
```

This will output:

```sql
SELECT
*
FROM
table1
```

### Dialect

You can pass dialect name to SqlFormatter.of :

```scala
import com.github.takayahilton.sqlformatter._

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

Currently just four SQL dialects are supported:

- StandardSQL - [Standard SQL](https://en.wikipedia.org/wiki/SQL:2011)
- CouchbaseN1QL - [Couchbase N1QL](http://www.couchbase.com/n1ql)
- DB2 - [IBM DB2](https://www.ibm.com/analytics/us/en/technology/db2/)
- PLSQL - [Oracle PL/SQL](http://www.oracle.com/technetwork/database/features/plsql/index.html)

### Format

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

```scala
import com.github.takayahilton.sqlformatter._

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

This will output:

```sql
SELECT
*
FROM
table1
```

### Placeholders replacement

You can pass `Seq` or `Map` to `format` :

```scala
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:

```sql
SELECT
*
FROM
tbl
WHERE
foo = 'bar'
```