Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginning of SQL. #163

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/sql/HibernateStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2016 DiffPlug
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...but we should probably change this to "2017". :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018 is coming soon enough, I was gonna change our copyright notice then :) Also want to change the notice to point to https://github.com/diffplug/spotless/graphs/contributors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds very sensible to me. :)

*
* 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.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Objects;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;

/** Wraps up [BasicFormatterImpl](https://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.html) as a FormatterStep. */
public class HibernateStep {
// prevent direct instantiation
private HibernateStep() {}

private static final String DEFAULT_VERSION = "5.2.12.Final";
static final String NAME = "hibernateSql";
static final String MAVEN_COORDINATE = "org.hibernate:hibernate-core:";

public enum Kind {
BASIC("org.hibernate.engine.jdbc.internal.BasicFormatterImpl"), DDL("org.hibernate.engine.jdbc.internal.DDLFormatterImpl");

private final String className;

Kind(String className) {
this.className = className;
}
}

public static FormatterStep create(Provisioner provisioner, Kind kind) {
return create(defaultVersion(), provisioner, kind);
}

public static FormatterStep create(String version, Provisioner provisioner, Kind kind) {
Objects.requireNonNull(version, "version");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new State(version, provisioner, kind.className),
State::createFormat);
}

public static String defaultVersion() {
return DEFAULT_VERSION;
}

static final class State implements Serializable {
private static final long serialVersionUID = 1L;

final String formatterClassname;
final JarState jarState;

State(String version, Provisioner provisioner, String formatterClassname) throws IOException {
this.jarState = JarState.from(MAVEN_COORDINATE + version, provisioner);
this.formatterClassname = formatterClassname;
}

FormatterFunc createFormat() throws Exception {
ClassLoader classLoader = jarState.getClassLoader();

// this is how we actually do a format
Class<?> formatterClazz = classLoader.loadClass(formatterClassname);
Object formatter = formatterClazz.newInstance();
Method formatMethod = formatterClazz.getMethod("format", String.class);
return input -> {
return (String) formatMethod.invoke(formatter, input);
};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this will work with Java 9.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JLLeitschuh Are you referring to the use of reflection here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the use of the classloader in this way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably missing something, but I'm not yet convinced it would be a problem on Java 9, as I have a real-world project using Java 9 and Spotless with google-java-format, and I've had no problems with Spotless or google-java-format specifically yet. (But it's worth noting that the project's using the classpath rather than the module-path, so I dunno how the JPMS would factor into things.)

}
}
7 changes: 7 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/sql/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package com.diffplug.spotless.sql;

import javax.annotation.ParametersAreNonnullByDefault;

import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public void groovyGradle(Action<GroovyGradleExtension> closure) {
configure(GroovyGradleExtension.NAME, GroovyGradleExtension.class, closure);
}

/** Configures the special sql-specific extension for SQL files. */
public void sql(Action<SqlExtension> closure) {
configure(SqlExtension.NAME, SqlExtension.class, closure);
}

/** Configures a custom extension. */
public void format(String name, Action<FormatExtension> closure) {
requireNonNull(name, "name");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.gradle.spotless;

import com.diffplug.spotless.sql.HibernateStep;

public class SqlExtension extends FormatExtension {
static final String NAME = "sql";

public SqlExtension(SpotlessExtension rootExtension) {
super(rootExtension);
}

public void hibernateSql() {
hibernateSql(HibernateStep.defaultVersion());
}

public void hibernateSql(String version) {
hibernate(version, HibernateStep.Kind.BASIC);
}

public void hibernateDdl() {
hibernateDdl(HibernateStep.defaultVersion());
}

public void hibernateDdl(String version) {
hibernate(version, HibernateStep.Kind.DDL);
}

private void hibernate(String version, HibernateStep.Kind kind) {
this.addStep(HibernateStep.create(
version,
GradleProvisioner.fromProject(getProject()),
kind));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.IOException;

import org.junit.Test;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.SerializableEqualityTester;
import com.diffplug.spotless.SerializableEqualityTester.API;
import com.diffplug.spotless.StepHarness;
import com.diffplug.spotless.TestProvisioner;
import com.diffplug.spotless.sql.HibernateStep.Kind;

public class HibernateStepTest extends ResourceHarness {
@Test
public void behaviorBasic() throws Exception {
FormatterStep step = HibernateStep.create(TestProvisioner.mavenCentral(), HibernateStep.Kind.BASIC);
StepHarness.forStep(step)
.testResource("sql/hibernate/basic.dirty", "sql/hibernate/basic.clean");
}

@Test
public void behaviorDdl() throws Exception {
FormatterStep step = HibernateStep.create(TestProvisioner.mavenCentral(), HibernateStep.Kind.DDL);
StepHarness.forStep(step)
.testResource("sql/hibernate/ddl.dirty", "sql/hibernate/ddl.clean");
}

@Test
public void equality() throws Exception {
new SerializableEqualityTester() {
String version = HibernateStep.defaultVersion();
Kind kind = Kind.BASIC;

@Override
protected void setupTest(API api) throws IOException {
// same version == same
api.areDifferentThan();
// change the version, and it's different
version = "5.2.11.Final";
api.areDifferentThan();
// change the kind, and it's different
kind = Kind.DDL;
api.areDifferentThan();
}

@Override
protected FormatterStep create() {
return HibernateStep.create(version, TestProvisioner.mavenCentral(), kind);
}
}.testEquals();
}
}
7 changes: 7 additions & 0 deletions testlib/src/test/resources/sql/hibernate/basic.clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE userinfo ALTER COLUMN id
drop DEFAULT;
ALTER TABLE userinfo
drop constraint userinfo_pkey cascade;
alter table userinfo
add primary key (user_id);
ALTER TABLE userinfo DROP COLUMN id
4 changes: 4 additions & 0 deletions testlib/src/test/resources/sql/hibernate/basic.dirty
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE userinfo ALTER COLUMN id drop DEFAULT;
ALTER TABLE userinfo drop constraint userinfo_pkey cascade;
alter table userinfo add primary key (user_id);
ALTER TABLE userinfo DROP COLUMN id;
Empty file.
4 changes: 4 additions & 0 deletions testlib/src/test/resources/sql/hibernate/ddl.dirty
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE userinfo ALTER COLUMN id drop DEFAULT;
ALTER TABLE userinfo drop constraint userinfo_pkey cascade;
alter table userinfo add primary key (user_id);
ALTER TABLE userinfo DROP COLUMN id;