Merge pull request #1202 from querydsl/i1157

Make all options of Configuration available in the Maven plugin
This commit is contained in:
Ruben Dijkstra 2015-02-17 08:55:38 +01:00
commit c3fa9a0eea
8 changed files with 267 additions and 11 deletions

View File

@ -211,6 +211,11 @@ public class AbstractMetaDataExportMojo extends AbstractMojo{
*/
private NumericMapping[] numericMappings;
/**
* @parameter
*/
private RenameMapping[] renameMappings;
/**
* @parameter default-value=false
*/
@ -391,21 +396,20 @@ public class AbstractMetaDataExportMojo extends AbstractMojo{
}
if (typeMappings != null) {
for (TypeMapping mapping : typeMappings) {
Class<?> typeClass = Class.forName(mapping.type);
if (Type.class.isAssignableFrom(typeClass)) {
configuration.register(mapping.table, mapping.column, (Type<?>)typeClass.newInstance());
} else {
configuration.register(mapping.table, mapping.column, typeClass);
}
mapping.apply(configuration);
}
}
if (numericMappings != null) {
for (NumericMapping mapping : numericMappings) {
int total = mapping.total;
int decimal = mapping.decimal;
configuration.registerNumeric(total, decimal, Class.forName(mapping.javaType));
mapping.apply(configuration);
}
}
if (renameMappings != null) {
for (RenameMapping mapping : renameMappings) {
mapping.apply(configuration);
}
}
if (columnComparatorClass != null) {
try {
exporter.setColumnComparatorClass( (Class) Class.forName(this.columnComparatorClass).asSubclass(Comparator.class));
@ -554,6 +558,10 @@ public class AbstractMetaDataExportMojo extends AbstractMojo{
this.numericMappings = numericMappings;
}
public void setRenameMappings(RenameMapping[] renameMappings) {
this.renameMappings = renameMappings;
}
public void setImports(String[] imports) {
this.imports = imports;
}

View File

@ -0,0 +1,19 @@
package com.querydsl.maven;
import com.querydsl.sql.Configuration;
/**
* Specifies mapping customization options.
*
* @author tiwe
*/
public interface Mapping {
/**
* Apply the customization to the passed in configuration.
*
* @param configuration the configuration to apply the customization to
*/
void apply(Configuration configuration);
}

View File

@ -13,14 +13,26 @@
*/
package com.querydsl.maven;
import com.querydsl.sql.Configuration;
/**
* {@linkplain NumericMapping} is used to customize mappings of various numeric precisions to data types.
*
* @author tiwe
*
*/
public class NumericMapping {
public class NumericMapping implements Mapping {
public int total, decimal;
public String javaType;
@Override
public void apply(Configuration configuration) {
try {
configuration.registerNumeric(total, decimal, Class.forName(javaType));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,64 @@
/*
* Copyright 2015, Timo Westkämper
*
* 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.querydsl.maven;
import com.google.common.collect.ImmutableList;
import com.querydsl.sql.Configuration;
/**
* {@linkplain RenameMapping} is used to override schemas, tables, columns and combinations of the three.
*
* @author tiwe
*/
public class RenameMapping implements Mapping {
String fromSchema, fromTable, fromColumn;
String toSchema, toTable, toColumn;
@Override
public void apply(Configuration configuration) {
if (fromSchema != null) {
if (fromTable != null && fromColumn != null && toColumn != null) {
configuration.registerColumnOverride(fromSchema, fromTable, fromColumn, toColumn);
} else if (fromTable != null && toTable != null) {
if (toSchema != null) {
configuration.registerTableOverride(fromSchema, fromTable, toSchema, toTable);
} else {
configuration.registerTableOverride(fromSchema, fromTable, toTable);
}
} else if (toSchema != null) {
configuration.registerSchemaOverride(fromSchema, toSchema);
} else {
insufficientArgs();
}
} else if (fromTable != null) {
if (fromColumn != null && toColumn != null) {
configuration.registerColumnOverride(fromTable, fromColumn, toColumn);
} else if (toTable != null) {
configuration.registerTableOverride(fromTable, toTable);
} else {
insufficientArgs();
}
} else {
insufficientArgs();
}
}
private void insufficientArgs() {
throw new IllegalArgumentException("Insufficient args " +
ImmutableList.of(fromSchema, fromTable, fromColumn) + " to " +
ImmutableList.of(toSchema, toTable, toColumn));
}
}

View File

@ -13,11 +13,16 @@
*/
package com.querydsl.maven;
import com.querydsl.sql.Configuration;
import com.querydsl.sql.types.Type;
/**
* {@linkplain TypeMapping} is used to customize the mapping from table + column to a type.
*
* @author tiwe
*
*/
public class TypeMapping {
public class TypeMapping implements Mapping {
public String table;
@ -25,4 +30,22 @@ public class TypeMapping {
public String type;
@Override
public void apply(Configuration configuration) {
try {
Class<?> typeClass = Class.forName(type);
if (Type.class.isAssignableFrom(typeClass)) {
configuration.register(table, column, (Type<?>)typeClass.newInstance());
} else {
configuration.register(table, column, typeClass);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -176,4 +176,18 @@ public class MetadataExportMojoTest {
assertTrue(new File("target/export12").exists());
}
@Test
public void ExecuteWithRenames() throws Exception {
RenameMapping mapping = new RenameMapping();
mapping.fromSchema = "ABC";
mapping.toSchema = "DEF";
mojo.setTargetFolder("target/export13");
mojo.setRenameMappings(new RenameMapping[]{mapping});
mojo.execute();
assertEquals(Collections.singletonList("target/export13"), project.getCompileSourceRoots());
assertTrue(new File("target/export13").exists());
}
}

View File

@ -0,0 +1,112 @@
package com.querydsl.maven;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.querydsl.sql.Configuration;
import com.querydsl.sql.SQLTemplates;
import com.querydsl.sql.SchemaAndTable;
public class RenameMappingTest {
private RenameMapping mapping = new RenameMapping();
private Configuration configuration = new Configuration(SQLTemplates.DEFAULT);
// to schema
@Test
public void SchemaToSchema() {
mapping.fromSchema = "ABC";
mapping.toSchema = "DEF";
mapping.apply(configuration);
assertEquals(
new SchemaAndTable("DEF", "TABLE"),
configuration.getOverride(new SchemaAndTable("ABC", "TABLE")));
assertEquals(
new SchemaAndTable("ABCD", "TABLE"),
configuration.getOverride(new SchemaAndTable("ABCD", "TABLE")));
}
// to table
@Test
public void TableToTable() {
mapping.fromTable = "TABLE1";
mapping.toTable = "TABLE2";
mapping.apply(configuration);
assertEquals(
new SchemaAndTable("DEF", "TABLE2"),
configuration.getOverride(new SchemaAndTable("DEF", "TABLE1")));
assertEquals(
new SchemaAndTable("DEF", "TABLE3"),
configuration.getOverride(new SchemaAndTable("DEF", "TABLE3")));
}
@Test
public void SchemaTableToTable() {
mapping.fromSchema = "ABC";
mapping.fromTable = "TABLE1";
mapping.toTable = "TABLE2";
mapping.apply(configuration);
assertEquals(
new SchemaAndTable("ABC", "TABLE2"),
configuration.getOverride(new SchemaAndTable("ABC", "TABLE1")));
assertEquals(
new SchemaAndTable("DEF", "TABLE1"),
configuration.getOverride(new SchemaAndTable("DEF", "TABLE1")));
}
@Test
public void SchemaTableToSchemaTable() {
mapping.fromSchema = "ABC";
mapping.fromTable = "TABLE1";
mapping.toSchema = "ABC";
mapping.toTable = "TABLE2";
mapping.apply(configuration);
assertEquals(
new SchemaAndTable("ABC", "TABLE2"),
configuration.getOverride(new SchemaAndTable("ABC", "TABLE1")));
assertEquals(
new SchemaAndTable("DEF", "TABLE1"),
configuration.getOverride(new SchemaAndTable("DEF", "TABLE1")));
}
// to column
@Test
public void SchemaTableColumnToColumn() {
mapping.fromSchema = "ABC";
mapping.fromTable = "TABLE1";
mapping.fromColumn = "COLUMN1";
mapping.toColumn = "COLUMN2";
mapping.apply(configuration);
assertEquals(
"COLUMN2",
configuration.getColumnOverride(new SchemaAndTable("ABC", "TABLE1"), "COLUMN1"));
assertEquals(
"COLUMN1",
configuration.getColumnOverride(new SchemaAndTable("DEF", "TABLE1"), "COLUMN1"));
}
@Test
public void TableColumnToColumn() {
mapping.fromTable = "TABLE1";
mapping.fromColumn = "COLUMN1";
mapping.toColumn = "COLUMN2";
mapping.apply(configuration);
assertEquals(
"COLUMN2",
configuration.getColumnOverride(new SchemaAndTable("ABC", "TABLE1"), "COLUMN1"));
assertEquals(
"COLUMN1",
configuration.getColumnOverride(new SchemaAndTable("ABC", "TABLE2"), "COLUMN1"));
}
}

View File

@ -54,4 +54,8 @@ public class SchemaAndTable implements Serializable {
return (schema != null ? 31 * schema.hashCode() : 0) + table.hashCode();
}
@Override
public String toString() {
return "(" + schema + " " + table + ")";
}
}