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.
- Host: GitHub
- URL: https://github.com/takayahilton/sql-formatter
- Owner: takayahilton
- License: mit
- Created: 2020-03-25T02:47:48.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-06-24T19:18:10.000Z (over 2 years ago)
- Last Synced: 2023-07-01T00:05:36.016Z (over 2 years ago)
- Topics: scala, scala-js, scala-native, scalajs, sql, sql-formatter
- Language: Scala
- Homepage:
- Size: 763 KB
- Stars: 6
- Watchers: 1
- Forks: 2
- Open Issues: 16
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# sql-formatter
[](https://travis-ci.org/takayahilton/sql-formatter)
[](https://search.maven.org/search?q=g:%22com.github.takayahilton%22%20AND%20a:%22sql-formatter_2.12%22)
[](https://codecov.io/gh/takayahilton/sql-formatter/branch/master)
[](#scalajs)
[](#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'
```