-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This first version of the sql formatter is taken from dbeaver project. It is configurable using closure ``` sql { dbeaver().configFile 'sqlFormatter.properties } ``` 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 ```
- Loading branch information
1 parent
d349656
commit 4fa35e8
Showing
32 changed files
with
2,462 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.sql; | ||
|
||
import java.util.Properties; | ||
|
||
import com.diffplug.spotless.sql.dbeaver.SQLFormatterConfiguration; | ||
import com.diffplug.spotless.sql.dbeaver.SQLTokenizedFormatter; | ||
|
||
/** | ||
* @author Baptiste Mesta. | ||
*/ | ||
public class DBeaverSQLFormatter { | ||
|
||
private final SQLTokenizedFormatter sqlTokenizedFormatter; | ||
|
||
DBeaverSQLFormatter(Properties properties) { | ||
SQLFormatterConfiguration configuration = new SQLFormatterConfiguration(properties); | ||
sqlTokenizedFormatter = new SQLTokenizedFormatter(configuration); | ||
} | ||
|
||
public String format(String input) { | ||
return sqlTokenizedFormatter.format(input); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.sql; | ||
|
||
import java.io.File; | ||
import java.io.Serializable; | ||
|
||
import com.diffplug.spotless.FileSignature; | ||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterProperties; | ||
import com.diffplug.spotless.FormatterStep; | ||
|
||
/** Wraps up [BasicFormatterImpl](https://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.html) as a FormatterStep. */ | ||
public class DBeaverSQLFormatterStep { | ||
|
||
static final String NAME = "dbeaverSql"; | ||
|
||
// prevent direct instantiation | ||
private DBeaverSQLFormatterStep() {} | ||
|
||
public static FormatterStep create(Iterable<File> files) { | ||
return FormatterStep.createLazy(NAME, | ||
() -> new State(files), | ||
State::createFormat); | ||
} | ||
|
||
static final class State implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** The signature of the settings file. */ | ||
final FileSignature settings; | ||
|
||
State(final Iterable<File> settingsFiles) throws Exception { | ||
this.settings = FileSignature.signAsList(settingsFiles); | ||
} | ||
|
||
FormatterFunc createFormat() throws Exception { | ||
FormatterProperties preferences = FormatterProperties.from(settings.files()); | ||
DBeaverSQLFormatter DBeaverSqlFormatter = new DBeaverSQLFormatter(preferences.getProperties()); | ||
return DBeaverSqlFormatter::format; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
lib/src/main/java/com/diffplug/spotless/sql/dbeaver/DBPKeywordType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.sql.dbeaver; | ||
|
||
/** | ||
* Forked from | ||
* DBeaver - Universal Database Manager | ||
* Copyright (C) 2010-2017 Serge Rider ([email protected]) | ||
* | ||
* Database keyword type | ||
*/ | ||
public enum DBPKeywordType { | ||
KEYWORD, FUNCTION, TYPE, OTHER | ||
} |
67 changes: 67 additions & 0 deletions
67
lib/src/main/java/com/diffplug/spotless/sql/dbeaver/FormatterToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.sql.dbeaver; | ||
|
||
class FormatterToken { | ||
|
||
private TokenType fType; | ||
private String fString; | ||
private int fPos = -1; | ||
|
||
public FormatterToken(final TokenType argType, final String argString, final int argPos) { | ||
fType = argType; | ||
fString = argString; | ||
fPos = argPos; | ||
} | ||
|
||
public FormatterToken(final TokenType argType, final String argString) { | ||
this(argType, argString, -1); | ||
} | ||
|
||
public void setType(final TokenType argType) { | ||
fType = argType; | ||
} | ||
|
||
public TokenType getType() { | ||
return fType; | ||
} | ||
|
||
public void setString(final String argString) { | ||
fString = argString; | ||
} | ||
|
||
public String getString() { | ||
return fString; | ||
} | ||
|
||
public void setPos(final int argPos) { | ||
fPos = argPos; | ||
} | ||
|
||
public int getPos() { | ||
return fPos; | ||
} | ||
|
||
public String toString() { | ||
final StringBuilder buf = new StringBuilder(); | ||
buf.append(getClass().getName()); | ||
buf.append("type=").append(fType); | ||
buf.append(",string=").append(fString); | ||
buf.append(",pos=").append(fPos); | ||
buf.append("]"); | ||
return buf.toString(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
lib/src/main/java/com/diffplug/spotless/sql/dbeaver/KeywordCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.sql.dbeaver; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* @author Baptiste Mesta. | ||
*/ | ||
public enum KeywordCase { | ||
UPPER { | ||
public String transform(String value) { | ||
return value.toUpperCase(Locale.ENGLISH); | ||
} | ||
}, | ||
LOWER { | ||
public String transform(String value) { | ||
return value.toLowerCase(Locale.ENGLISH); | ||
} | ||
}, | ||
ORIGINAL { | ||
public String transform(String value) { | ||
return value; | ||
} | ||
}; | ||
|
||
public abstract String transform(String value); | ||
} |
50 changes: 50 additions & 0 deletions
50
lib/src/main/java/com/diffplug/spotless/sql/dbeaver/Pair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.sql.dbeaver; | ||
|
||
/** | ||
* Pair | ||
*/ | ||
public class Pair<T1, T2> { | ||
private T1 first; | ||
private T2 second; | ||
|
||
public Pair(T1 first, T2 second) { | ||
this.first = first; | ||
this.second = second; | ||
} | ||
|
||
public T1 getFirst() { | ||
return first; | ||
} | ||
|
||
public void setFirst(T1 first) { | ||
this.first = first; | ||
} | ||
|
||
public T2 getSecond() { | ||
return second; | ||
} | ||
|
||
public void setSecond(T2 second) { | ||
this.second = second; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return first + "=" + second; | ||
} | ||
} |
Oops, something went wrong.