Skip to content

Commit 6850bb9

Browse files
committed
Add Sql formatter
This first version of the sql formatter is taken from dbeaver project. it is configurable using closure ``` sql { sqlFormatter().configFile // the configuration file } ``` configuration file support following properties (with default values) ``` sql.formatter.keyword.case=UPPER sql.formatter.statement.delimiter=; sql.formatter.indent.type=space sql.formatter.indent.size=4 ```
1 parent d349656 commit 6850bb9

27 files changed

+2291
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.sql;
17+
18+
/**
19+
* Forked from
20+
* DBeaver - Universal Database Manager
21+
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
22+
*
23+
* Database keyword type
24+
*/
25+
public enum DBPKeywordType {
26+
KEYWORD, FUNCTION, TYPE, OTHER
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.sql;
17+
18+
class FormatterToken {
19+
20+
private TokenType fType;
21+
private String fString;
22+
private int fPos = -1;
23+
24+
public FormatterToken(final TokenType argType, final String argString, final int argPos) {
25+
fType = argType;
26+
fString = argString;
27+
fPos = argPos;
28+
}
29+
30+
public FormatterToken(final TokenType argType, final String argString) {
31+
this(argType, argString, -1);
32+
}
33+
34+
public void setType(final TokenType argType) {
35+
fType = argType;
36+
}
37+
38+
public TokenType getType() {
39+
return fType;
40+
}
41+
42+
public void setString(final String argString) {
43+
fString = argString;
44+
}
45+
46+
public String getString() {
47+
return fString;
48+
}
49+
50+
public void setPos(final int argPos) {
51+
fPos = argPos;
52+
}
53+
54+
public int getPos() {
55+
return fPos;
56+
}
57+
58+
public String toString() {
59+
final StringBuilder buf = new StringBuilder();
60+
buf.append(getClass().getName());
61+
buf.append("type=").append(fType);
62+
buf.append(",string=").append(fString);
63+
buf.append(",pos=").append(fPos);
64+
buf.append("]");
65+
return buf.toString();
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.diffplug.spotless.sql;
2+
3+
import java.util.Locale;
4+
5+
/**
6+
* @author Baptiste Mesta.
7+
*/
8+
public enum KeywordCase {
9+
UPPER {
10+
public String transform(String value) {
11+
return value.toUpperCase(Locale.ENGLISH);
12+
}
13+
},
14+
LOWER {
15+
public String transform(String value) {
16+
return value.toLowerCase(Locale.ENGLISH);
17+
}
18+
},
19+
ORIGINAL {
20+
public String transform(String value) {
21+
return value;
22+
}
23+
};
24+
25+
public abstract String transform(String value);
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.sql;
17+
18+
/**
19+
* Pair
20+
*/
21+
public class Pair<T1, T2> {
22+
private T1 first;
23+
private T2 second;
24+
25+
public Pair(T1 first, T2 second) {
26+
this.first = first;
27+
this.second = second;
28+
}
29+
30+
public T1 getFirst() {
31+
return first;
32+
}
33+
34+
public void setFirst(T1 first) {
35+
this.first = first;
36+
}
37+
38+
public T2 getSecond() {
39+
return second;
40+
}
41+
42+
public void setSecond(T2 second) {
43+
this.second = second;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return first + "=" + second;
49+
}
50+
}

0 commit comments

Comments
 (0)