Skip to content

Commit

Permalink
Merge pull request #36 from vertical-blank/2.0.0
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
vertical-blank authored Mar 13, 2021
2 parents 7fa8c81 + ad0d901 commit c1e10a0
Show file tree
Hide file tree
Showing 64 changed files with 8,056 additions and 3,220 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 1.8
java-version: 11
- name: Build with Maven
run: mvn -B package --file pom.xml
run: mvn -e -B package --file pom.xml
- name: Codecov
uses: codecov/codecov-action@v1.0.15
82 changes: 64 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Written with only Java Standard Library, without dependencies.

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
Expand All @@ -20,14 +25,14 @@ Demo is running on Google Cloud Function, with native-compiled shared library by
<dependency>
<groupId>com.github.vertical-blank</groupId>
<artifactId>sql-formatter</artifactId>
<version>1.0.3</version>
<version>2.0.0</version>
</dependency>
```

### Gradle

```gradle
implementation 'com.github.vertical-blank:sql-formatter:1.0.3'
implementation 'com.github.vertical-blank:sql-formatter:2.0.0'
```

## Examples
Expand All @@ -47,50 +52,75 @@ FROM
table1
```

You can also pass `FormatConfig` object built by builder:

```js
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 name to `SqlFormatter.of` :
You can pass dialect `com.github.vertical_blank.sqlformatter.languages.Dialect` or `String` to `SqlFormatter.of` :

```java
SqlFormatter
.of("n1ql") // Defaults to "sql"
.of(Dialect.N1ql) // Recommended
//.of("n1ql") // String can be passed
.format("SELECT *");
```

Currently just four SQL dialects are supported:
SQL formatter supports the following dialects:

- **sql** - [Standard SQL](https://en.wikipedia.org/wiki/SQL:2011)
- **n1ql** - [Couchbase N1QL](http://www.couchbase.com/n1ql)
- **db2** - [IBM DB2](https://www.ibm.com/analytics/us/en/technology/db2/)
- **pl/sql** - [Oracle PL/SQL](http://www.oracle.com/technetwork/database/features/plsql/index.html)
- **sql** - [Standard SQL][]
- **mariadb** - [MariaDB][]
- **mysql** - [MySQL][]
- **postgresql** - [PostgreSQL][]
- **db2** - [IBM DB2][]
- **plsql** - [Oracle PL/SQL][]
- **n1ql** - [Couchbase N1QL][]
- **redshift** - [Amazon Redshift][]
- **spark** - [Spark][]
- **tsql** - [SQL Server Transact-SQL][tsql]

### Format
### Extend formatters

Defaults to two spaces.
You can pass indent string to `format` :
Formatters can be extended as below :

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

This will output:
Then it results in:

```sql
SELECT
*
*
FROM
table1
table
WHERE
A => 4
```

### Placeholders replacement

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

```java
// Named placeholders
Map<String, String> namedParams = new HashMap<>();
namedParams.put("foo", "'bar'");
SqlFormatter.format("SELECT * FROM tbl WHERE foo = @foo", namedParams);
SqlFormatter.of(Dialect.TSql).format("SELECT * FROM tbl WHERE foo = @foo", namedParams);

// Indexed placeholders
SqlFormatter.format("SELECT * FROM tbl WHERE foo = ?", Arrays.asList("'bar'"));
Expand All @@ -106,3 +136,19 @@ FROM
WHERE
foo = 'bar'
```

## Build

Building this library requires JDK 11 because of [ktfmt](https://github.com/facebookincubator/ktfmt).


[standard sql]: https://en.wikipedia.org/wiki/SQL:2011
[couchbase n1ql]: http://www.couchbase.com/n1ql
[ibm db2]: https://www.ibm.com/analytics/us/en/technology/db2/
[oracle pl/sql]: http://www.oracle.com/technetwork/database/features/plsql/index.html
[amazon redshift]: https://docs.aws.amazon.com/redshift/latest/dg/cm_chap_SQLCommandRef.html
[spark]: https://spark.apache.org/docs/latest/api/sql/index.html
[postgresql]: https://www.postgresql.org/
[mariadb]: https://mariadb.com/
[mysql]: https://www.mysql.com/
[tsql]: https://docs.microsoft.com/en-us/sql/sql-server/
Loading

0 comments on commit c1e10a0

Please sign in to comment.