-
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.
- Loading branch information
1 parent
d349656
commit 7140a56
Showing
25 changed files
with
2,634 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
lib/src/main/java/com/diffplug/spotless/sql/ArrayUtils.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,123 @@ | ||
/* | ||
* 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.lang.reflect.Array; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Common utils | ||
*/ | ||
public class ArrayUtils { | ||
|
||
public static boolean isEmpty(Object[] arr) { | ||
return arr == null || arr.length == 0; | ||
} | ||
|
||
public static boolean isEmpty(short[] array) { | ||
return array == null || array.length == 0; | ||
} | ||
|
||
public static boolean contains(short[] array, short value) { | ||
if (array == null) | ||
return false; | ||
for (int i = 0, arrayLength = array.length; i < arrayLength; i++) { | ||
if (array[i] == value) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean contains(char[] array, char value) { | ||
if (array == null || array.length == 0) | ||
return false; | ||
for (int i = 0, arrayLength = array.length; i < arrayLength; i++) { | ||
if (array[i] == value) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean isEmpty(int[] array) { | ||
return array == null || array.length == 0; | ||
} | ||
|
||
public static boolean contains(int[] array, int value) { | ||
if (array == null) | ||
return false; | ||
for (int v : array) { | ||
if (v == value) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean isEmpty(long[] array) { | ||
return array == null || array.length == 0; | ||
} | ||
|
||
public static boolean contains(long[] array, long value) { | ||
if (array == null) | ||
return false; | ||
for (long v : array) { | ||
if (v == value) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static <OBJECT_TYPE> boolean contains(OBJECT_TYPE[] array, OBJECT_TYPE value) { | ||
if (isEmpty(array)) | ||
return false; | ||
for (int i = 0; i < array.length; i++) { | ||
if (CommonUtils.equalObjects(value, array[i])) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@SafeVarargs | ||
public static <OBJECT_TYPE> boolean contains(OBJECT_TYPE[] array, OBJECT_TYPE... values) { | ||
if (isEmpty(array)) | ||
return false; | ||
for (int i = 0; i < array.length; i++) { | ||
for (int k = 0; k < values.length; k++) { | ||
if (CommonUtils.equalObjects(array[i], values[k])) | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static <T> List<T> safeArray(T[] array) { | ||
if (array == null) { | ||
return Collections.emptyList(); | ||
} else { | ||
return Arrays.asList(array); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> T[] add(Class<T> type, T[] elements, T add) { | ||
T[] newArray = (T[]) Array.newInstance(type, elements.length + 1); | ||
System.arraycopy(elements, 0, newArray, 0, elements.length); | ||
newArray[elements.length] = add; | ||
return newArray; | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
lib/src/main/java/com/diffplug/spotless/sql/CommonUtils.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,75 @@ | ||
/* | ||
* 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.Collection; | ||
import java.util.Map; | ||
|
||
/** | ||
* Common utils | ||
*/ | ||
public class CommonUtils { | ||
|
||
public static boolean isEmpty(CharSequence value) { | ||
return value == null || value.length() == 0; | ||
} | ||
|
||
public static boolean isEmpty(String value) { | ||
return value == null || value.length() == 0; | ||
} | ||
|
||
public static boolean isEmpty(Collection<?> value) { | ||
return value == null || value.isEmpty(); | ||
} | ||
|
||
public static boolean isEmpty(Map<?, ?> value) { | ||
return value == null || value.isEmpty(); | ||
} | ||
|
||
public static boolean equalObjects(Object o1, Object o2) { | ||
if (o1 == o2) { | ||
return true; | ||
} | ||
if (o1 == null || o2 == null) { | ||
return false; | ||
} | ||
// if (o1.getClass() != o2.getClass()) { | ||
// return false; | ||
// } | ||
return o1.equals(o2); | ||
} | ||
|
||
public static String toString(Object object) { | ||
if (object == null) { | ||
return ""; | ||
} else if (object instanceof String) { | ||
return (String) object; | ||
} else { | ||
return object.toString(); | ||
} | ||
} | ||
|
||
public static String toString(Object object, String def) { | ||
if (object == null) { | ||
return def; | ||
} else if (object instanceof String) { | ||
return (String) object; | ||
} else { | ||
return object.toString(); | ||
} | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
lib/src/main/java/com/diffplug/spotless/sql/DBPIdentifierCase.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,42 @@ | ||
/* | ||
* 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.Locale; | ||
|
||
/** | ||
* Database keyword type | ||
*/ | ||
public enum DBPIdentifierCase { | ||
UPPER { | ||
public String transform(String value) { | ||
return value.toUpperCase(Locale.ENGLISH); | ||
} | ||
}, | ||
LOWER { | ||
public String transform(String value) { | ||
return value.toLowerCase(Locale.ENGLISH); | ||
} | ||
}, | ||
MIXED { | ||
public String transform(String value) { | ||
return value; | ||
} | ||
}; | ||
|
||
public abstract String transform(String value); | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
lib/src/main/java/com/diffplug/spotless/sql/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,23 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* 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/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; | ||
|
||
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(); | ||
} | ||
} |
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; | ||
|
||
/** | ||
* 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.