Skip to content

Commit

Permalink
Merge pull request #166 from baptistemesta/feat/sql_dbeaver_formatter
Browse files Browse the repository at this point in the history
feat(sql): add sql formatter
  • Loading branch information
nedtwigg authored Dec 2, 2017
2 parents 34edc24 + e1c955b commit 0b54310
Show file tree
Hide file tree
Showing 36 changed files with 2,487 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ You might be looking for:
* Updated default eclipse-jdt version to `4.7.1` from `4.6.3`.
* Updated jgit from `4.5.0.201609210915-r` to `4.9.0.201710071750-r`.
* Updated concurrent-trees from `2.6.0` to `2.6.1` (performance improvement).
* Added `dbeaverSql` formatter step, for formatting sql scripts. ([#166](https://github.com/diffplug/spotless/pull/166))
+ Many thanks to [Baptiste Mesta](https://github.com/baptistemesta) for porting to Spotless.
+ Many thanks to [DBeaver](https://dbeaver.jkiss.org/) and the [DBeaver contributors](https://github.com/serge-rider/dbeaver/graphs/contributors) for building the implementation.

### Version 1.6.0 - September 29th 2017 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.6.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.6.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ lib('scala.ScalaFmtStep') +'{{yes}} | {{no}}

## Acknowledgements

* Huge thanks to [Jonathan Bluett-Duncan](https://github.com/jbduncan) for
* Thanks to [Baptiste Mesta](https://github.com/baptistemesta) for porting the DBeaver formatter to Spotless, and thanks to [DBeaver](https://dbeaver.jkiss.org/) and [its authors](https://github.com/serge-rider/dbeaver/graphs/contributors) for their excellent SQL formatter.
* Thanks to [Jonathan Bluett-Duncan](https://github.com/jbduncan) for
+ implementing up-to-date checking [#31](https://github.com/diffplug/spotless/issues/31)
+ breaking spotless into libraries [#56](https://github.com/diffplug/spotless/issues/56)
+ lots of other things, but especially the diff support in `spotlessCheck`
* Huge thanks to [Frank Vennemeyer](https://github.com/fvgh) for [Groovy support via greclipse](https://github.com/diffplug/spotless/issues/13).
* Huge thanks to [Stefan Oehme](https://github.com/oehme) for tons of help on the internal mechanics of Gradle.
* Thanks to [Frank Vennemeyer](https://github.com/fvgh) for [Groovy support via greclipse](https://github.com/diffplug/spotless/issues/13).
* Thanks to [Stefan Oehme](https://github.com/oehme) for tons of help on the internal mechanics of Gradle.
* Formatting by Eclipse
+ Special thanks to [Mateusz Matela](https://waynebeaton.wordpress.com/2015/03/15/great-fixes-for-mars-winners-part-i/) for huge improvements to the eclipse code formatter!
* Thanks to [Stanley Shyiko](https://github.com/shyiko) for his help integrating [ktlint](https://github.com/shyiko/ktlint).
Expand Down
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);
}
}
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;
}
}
}
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
}
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();
}
}
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 lib/src/main/java/com/diffplug/spotless/sql/dbeaver/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.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;
}
}
Loading

0 comments on commit 0b54310

Please sign in to comment.