Skip to content

Commit

Permalink
Add Sql formatter
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
baptistemesta committed Nov 21, 2017
1 parent d349656 commit 6850bb9
Show file tree
Hide file tree
Showing 27 changed files with 2,291 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/sql/DBPKeywordType.java
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;

/**
* 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 lib/src/main/java/com/diffplug/spotless/sql/FormatterToken.java
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;

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();
}
}
26 changes: 26 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/sql/KeywordCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.diffplug.spotless.sql;

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 lib/src/main/java/com/diffplug/spotless/sql/Pair.java
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;

/**
* 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;
}
}
Loading

0 comments on commit 6850bb9

Please sign in to comment.