@@ -12,6 +12,11 @@ Written with only Java Standard Library, without dependencies.
12
12
13
13
Demo is running on Google Cloud Function, with native-compiled shared library by GraalVM.
14
14
15
+ This does not support:
16
+
17
+ - Stored procedures.
18
+ - Changing of the delimiter type to something else than ;.
19
+
15
20
## Usage
16
21
17
22
### Maven
@@ -20,14 +25,14 @@ Demo is running on Google Cloud Function, with native-compiled shared library by
20
25
<dependency >
21
26
<groupId >com.github.vertical-blank</groupId >
22
27
<artifactId >sql-formatter</artifactId >
23
- <version >1 .0.3 </version >
28
+ <version >2 .0.0 </version >
24
29
</dependency >
25
30
```
26
31
27
32
### Gradle
28
33
29
34
``` gradle
30
- implementation 'com.github.vertical-blank:sql-formatter:1 .0.3 '
35
+ implementation 'com.github.vertical-blank:sql-formatter:2 .0.0 '
31
36
```
32
37
33
38
## Examples
47
52
table1
48
53
```
49
54
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
+
50
69
### Dialect
51
70
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 ` :
53
72
54
73
``` java
55
74
SqlFormatter
56
- .of(" n1ql" ) // Defaults to "sql"
75
+ .of(Dialect . N1ql ) // Recommended
76
+ // .of("n1ql") // String can be passed
57
77
.format(" SELECT *" );
58
78
```
59
79
60
- Currently just four SQL dialects are supported :
80
+ SQL formatter supports the following dialects :
61
81
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 ]
66
92
67
- ### Format
93
+ ### Extend formatters
68
94
69
- Defaults to two spaces.
70
- You can pass indent string to ` format ` :
95
+ Formatters can be extended as below :
71
96
72
97
``` 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" )
74
102
```
75
103
76
- This will output :
104
+ Then it results in :
77
105
78
106
``` sql
79
107
SELECT
80
- *
108
+ *
81
109
FROM
82
- table1
110
+ table
111
+ WHERE
112
+ A => 4
83
113
```
84
114
85
115
### Placeholders replacement
86
116
87
- You can pass List or Map to ` format ` :
117
+ You can pass ` List ` or ` Map ` to ` format ` :
88
118
89
119
``` java
90
120
// Named placeholders
91
121
Map<String , String > namedParams = new HashMap<> ();
92
122
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);
94
124
95
125
// Indexed placeholders
96
126
SqlFormatter . format(" SELECT * FROM tbl WHERE foo = ?" , Arrays . asList(" 'bar'" ));
@@ -106,3 +136,19 @@ FROM
106
136
WHERE
107
137
foo = ' bar'
108
138
```
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