Skip to content

Commit c1e10a0

Browse files
Merge pull request #36 from vertical-blank/2.0.0
2.0.0
2 parents 7fa8c81 + ad0d901 commit c1e10a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+8056
-3220
lines changed

.github/workflows/maven.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v2
19-
- name: Set up JDK 1.8
19+
- name: Set up JDK 11
2020
uses: actions/setup-java@v1
2121
with:
22-
java-version: 1.8
22+
java-version: 11
2323
- name: Build with Maven
24-
run: mvn -B package --file pom.xml
24+
run: mvn -e -B package --file pom.xml
2525
- name: Codecov
2626
uses: codecov/codecov-action@v1.0.15

README.md

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Written with only Java Standard Library, without dependencies.
1212

1313
Demo is running on Google Cloud Function, with native-compiled shared library by GraalVM.
1414

15+
This does not support:
16+
17+
- Stored procedures.
18+
- Changing of the delimiter type to something else than ;.
19+
1520
## Usage
1621

1722
### Maven
@@ -20,14 +25,14 @@ Demo is running on Google Cloud Function, with native-compiled shared library by
2025
<dependency>
2126
<groupId>com.github.vertical-blank</groupId>
2227
<artifactId>sql-formatter</artifactId>
23-
<version>1.0.3</version>
28+
<version>2.0.0</version>
2429
</dependency>
2530
```
2631

2732
### Gradle
2833

2934
```gradle
30-
implementation 'com.github.vertical-blank:sql-formatter:1.0.3'
35+
implementation 'com.github.vertical-blank:sql-formatter:2.0.0'
3136
```
3237

3338
## Examples
@@ -47,50 +52,75 @@ FROM
4752
table1
4853
```
4954

55+
You can also pass `FormatConfig` object built by builder:
56+
57+
```js
58+
SqlFormatter.format('SELECT * FROM tbl',
59+
FormatConfig.builder()
60+
.indent(" ") // Defaults to two spaces
61+
.uppercase(true) // Defaults to false (not safe to use when SQL dialect has case-sensitive identifiers)
62+
.linesBetweenQueries(2) // Defaults to 1
63+
.maxColumnLength(100) // Defaults to 50
64+
.params(Arrays.asList("a", "b", "c")) // Map or List. See Placeholders replacement.
65+
.build()
66+
);
67+
```
68+
5069
### Dialect
5170

52-
You can pass dialect name to `SqlFormatter.of` :
71+
You can pass dialect `com.github.vertical_blank.sqlformatter.languages.Dialect` or `String` to `SqlFormatter.of` :
5372

5473
```java
5574
SqlFormatter
56-
.of("n1ql") // Defaults to "sql"
75+
.of(Dialect.N1ql) // Recommended
76+
//.of("n1ql") // String can be passed
5777
.format("SELECT *");
5878
```
5979

60-
Currently just four SQL dialects are supported:
80+
SQL formatter supports the following dialects:
6181

62-
- **sql** - [Standard SQL](https://en.wikipedia.org/wiki/SQL:2011)
63-
- **n1ql** - [Couchbase N1QL](http://www.couchbase.com/n1ql)
64-
- **db2** - [IBM DB2](https://www.ibm.com/analytics/us/en/technology/db2/)
65-
- **pl/sql** - [Oracle PL/SQL](http://www.oracle.com/technetwork/database/features/plsql/index.html)
82+
- **sql** - [Standard SQL][]
83+
- **mariadb** - [MariaDB][]
84+
- **mysql** - [MySQL][]
85+
- **postgresql** - [PostgreSQL][]
86+
- **db2** - [IBM DB2][]
87+
- **plsql** - [Oracle PL/SQL][]
88+
- **n1ql** - [Couchbase N1QL][]
89+
- **redshift** - [Amazon Redshift][]
90+
- **spark** - [Spark][]
91+
- **tsql** - [SQL Server Transact-SQL][tsql]
6692

67-
### Format
93+
### Extend formatters
6894

69-
Defaults to two spaces.
70-
You can pass indent string to `format` :
95+
Formatters can be extended as below :
7196

7297
```java
73-
SqlFormatter.format("SELECT * FROM table1", " ");
98+
SqlFormatter
99+
.of(Dialect.MySql)
100+
.extend(cfg -> cfg.plusOperators("=>"))
101+
.format("SELECT * FROM table WHERE A => 4")
74102
```
75103

76-
This will output:
104+
Then it results in:
77105

78106
```sql
79107
SELECT
80-
*
108+
*
81109
FROM
82-
table1
110+
table
111+
WHERE
112+
A => 4
83113
```
84114

85115
### Placeholders replacement
86116

87-
You can pass List or Map to `format` :
117+
You can pass `List` or `Map` to `format` :
88118

89119
```java
90120
// Named placeholders
91121
Map<String, String> namedParams = new HashMap<>();
92122
namedParams.put("foo", "'bar'");
93-
SqlFormatter.format("SELECT * FROM tbl WHERE foo = @foo", namedParams);
123+
SqlFormatter.of(Dialect.TSql).format("SELECT * FROM tbl WHERE foo = @foo", namedParams);
94124

95125
// Indexed placeholders
96126
SqlFormatter.format("SELECT * FROM tbl WHERE foo = ?", Arrays.asList("'bar'"));
@@ -106,3 +136,19 @@ FROM
106136
WHERE
107137
foo = 'bar'
108138
```
139+
140+
## Build
141+
142+
Building this library requires JDK 11 because of [ktfmt](https://github.com/facebookincubator/ktfmt).
143+
144+
145+
[standard sql]: https://en.wikipedia.org/wiki/SQL:2011
146+
[couchbase n1ql]: http://www.couchbase.com/n1ql
147+
[ibm db2]: https://www.ibm.com/analytics/us/en/technology/db2/
148+
[oracle pl/sql]: http://www.oracle.com/technetwork/database/features/plsql/index.html
149+
[amazon redshift]: https://docs.aws.amazon.com/redshift/latest/dg/cm_chap_SQLCommandRef.html
150+
[spark]: https://spark.apache.org/docs/latest/api/sql/index.html
151+
[postgresql]: https://www.postgresql.org/
152+
[mariadb]: https://mariadb.com/
153+
[mysql]: https://www.mysql.com/
154+
[tsql]: https://docs.microsoft.com/en-us/sql/sql-server/

0 commit comments

Comments
 (0)