-
Notifications
You must be signed in to change notification settings - Fork 455
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
Beginning of SQL. #163
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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 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); | ||
}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if this will work with Java 9. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JLLeitschuh Are you referring to the use of reflection here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the use of the classloader in this way. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
} |
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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
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 |
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; |
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; |
There was a problem hiding this comment.
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". :)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. :)