added support for ORDINAL_POSITION metadata

This commit is contained in:
Tuomas Kiviaho 2014-04-08 18:02:58 +03:00
parent 3812288e86
commit 4ec6da5e45
9 changed files with 112 additions and 22 deletions

View File

@ -17,6 +17,7 @@ import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Comparator;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
@ -248,6 +249,13 @@ public class AbstractMetaDataExportMojo extends AbstractMojo{
*/
private boolean exportForeignKeys;
/**
* override default column order (default: alphabetical)
*
* @parameter
*/
private String columnComparatorClass;
/**
* java import added to generated query classes:
* com.bar for package (without .* notation)
@ -370,6 +378,14 @@ public class AbstractMetaDataExportMojo extends AbstractMojo{
configuration.registerNumeric(mapping.size, mapping.digits, Class.forName(mapping.javaType));
}
}
if (columnComparatorClass != null) {
try {
exporter.setColumnComparatorClass( (Class) Class.forName(this.columnComparatorClass).asSubclass(Comparator.class));
} catch (ClassNotFoundException e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage(), e);
}
}
exporter.setConfiguration(configuration);

View File

@ -17,6 +17,7 @@ import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Comparator;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
@ -182,6 +183,11 @@ public class AntMetaDataExporter extends Task {
*
*/
private boolean beanPrintSupertype;
/**
* override default column order (default: alphabetical)
*/
private String columnComparatorClass;
/**
* java import added to generated query classes:
@ -192,7 +198,8 @@ public class AntMetaDataExporter extends Task {
private String[] imports;
@Override
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void execute() {
Connection dbConn = null;
File targetPackagePath = new File(targetSourceFolder);
@ -256,6 +263,9 @@ public class AntMetaDataExporter extends Task {
if (sourceEncoding != null) {
exporter.setSourceEncoding(sourceEncoding);
}
if (columnComparatorClass != null) {
exporter.setColumnComparatorClass((Class) Class.forName(this.columnComparatorClass).asSubclass(Comparator.class));
}
exporter.export(dbConn.getMetaData());

View File

@ -23,6 +23,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -251,6 +252,7 @@ public class MetaDataExporter {
int columnType = columns.getInt("DATA_TYPE");
Number columnSize = (Number) columns.getObject("COLUMN_SIZE");
Number columnDigits = (Number) columns.getObject("DECIMAL_DIGITS");
int columnIndex = columns.getInt("ORDINAL_POSITION");
int nullable = columns.getInt("NULLABLE");
String propertyName = namingStrategy.getPropertyName(normalizedColumnName, classModel);
@ -269,7 +271,7 @@ public class MetaDataExporter {
}
Type typeModel = new ClassType(fieldType, clazz);
Property property = createProperty(classModel, normalizedColumnName, propertyName, typeModel);
ColumnMetadata column = ColumnMetadata.named(normalizedColumnName).ofType(columnType);
ColumnMetadata column = ColumnMetadata.named(normalizedColumnName).ofType(columnType).withIndex(columnIndex);
if (nullable == DatabaseMetaData.columnNoNulls) {
column = column.notNull();
}
@ -539,6 +541,13 @@ public class MetaDataExporter {
module.bind(SQLCodegenModule.INNER_CLASSES_FOR_KEYS, innerClassesForKeys);
}
/**
* @param columnComparator
*/
public void setColumnComparatorClass(Class<? extends Comparator<Property>> columnComparatorClass) {
module.bind(SQLCodegenModule.COLUMN_COMPARATOR, columnComparatorClass);
}
/**
* @param serializerClass
*/

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -28,6 +29,7 @@ import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
import com.google.common.collect.Lists;
import com.mysema.codegen.CodeWriter;
import com.mysema.codegen.model.ClassType;
import com.mysema.codegen.model.Parameter;
@ -63,6 +65,8 @@ public class MetaDataSerializer extends EntitySerializer {
private final boolean innerClassesForKeys;
private final Set<String> imports;
private final Comparator<Property> columnComparator;
/**
* Create a new MetaDataSerializer instance
@ -76,11 +80,13 @@ public class MetaDataSerializer extends EntitySerializer {
TypeMappings typeMappings,
NamingStrategy namingStrategy,
@Named(SQLCodegenModule.INNER_CLASSES_FOR_KEYS) boolean innerClassesForKeys,
@Named(SQLCodegenModule.IMPORTS) Set<String> imports) {
@Named(SQLCodegenModule.IMPORTS) Set<String> imports,
@Named(SQLCodegenModule.COLUMN_COMPARATOR) Comparator<Property> columnComparator) {
super(typeMappings,Collections.<String>emptyList());
this.namingStrategy = namingStrategy;
this.innerClassesForKeys = innerClassesForKeys;
this.imports = new HashSet<String>(imports);
this.columnComparator = columnComparator;
}
@Override
@ -202,12 +208,17 @@ public class MetaDataSerializer extends EntitySerializer {
@Override
protected void outro(EntityType model, CodeWriter writer) throws IOException {
writer.beginPublicMethod(Types.VOID,"addMetadata");
for (Property property : model.getProperties()) {
List<Property> properties = Lists.newArrayList(model.getProperties());
if (columnComparator != null) {
Collections.sort(properties, columnComparator);
}
for (Property property : properties) {
String name = property.getEscapedName();
ColumnMetadata metadata = (ColumnMetadata) property.getData().get("COLUMN");
StringBuilder columnMeta = new StringBuilder();
columnMeta.append("ColumnMetadata");
columnMeta.append(".named(\"" + metadata.getName() + "\")");
columnMeta.append(".withIndex(" + metadata.getIndex() + ")");
columnMeta.append(".ofType(" + metadata.getJdbcType() + ")");
if (metadata.hasSize()) {
columnMeta.append(".withSize(" + metadata.getSize() + ")");

View File

@ -0,0 +1,28 @@
package com.mysema.query.sql.codegen;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import com.mysema.query.codegen.Property;
import com.mysema.query.sql.ColumnMetadata;
public class OrdinalPositionComparator implements Comparator<Property> {
public OrdinalPositionComparator() {
super();
}
@Override
public int compare(Property property1, Property property2) {
Integer comparison = null;
for (Property property : Arrays.asList(property1, property2)) {
Map<Object, Object> data = property.getData();
ColumnMetadata columnMetadata = (ColumnMetadata) data.get("COLUMN");
int index = columnMetadata.getIndex();
comparison = comparison == null ? index : comparison - index;
}
return comparison;
}
}

View File

@ -42,6 +42,8 @@ public class SQLCodegenModule extends CodegenModule{
public static final String SCHEMA_TO_PACKAGE = "schemaToPackage";
public static final String COLUMN_COMPARATOR = "columnComparator";
@Override
protected void configure() {
super.configure();
@ -57,6 +59,7 @@ public class SQLCodegenModule extends CodegenModule{
bind(PACKAGE_NAME, "com.example");
bind(BEAN_SERIALIZER, (Class<?>)null);
bind(SCHEMA_TO_PACKAGE, false);
bind(COLUMN_COMPARATOR, null);
}
public String getPrefix() {

View File

@ -152,14 +152,14 @@ public class MetaDataExporterTest {
@Test
public void NormalSettings_Repetition() throws SQLException {
test("Q", "", "", "", defaultNaming, "target/1", false, false);
test("Q", "", "", "", defaultNaming, "target/1", false, false, false);
File file = new File("target/1/test/QEmployee.java");
long lastModified = file.lastModified();
assertTrue(file.exists());
clean = false;
test("Q", "", "", "", defaultNaming, "target/1", false, false);
test("Q", "", "", "", defaultNaming, "target/1", false, false, false);
assertEquals(lastModified, file.lastModified());
}
@ -179,14 +179,15 @@ public class MetaDataExporterTest {
for (boolean exportColumns : trueAndFalse) {
for (String beanPackage : Arrays.asList("test2", null)) {
for (Serializer beanSerializer : BEAN_SERIALIZERS) {
for (boolean withOriginalPositioning : trueAndFalse) {
counter++;
this.beanPackageName = beanPackage;
this.schemaToPackage = schemaToPackage;
this.exportColumns = exportColumns;
this.beanSerializer = beanSerializer;
test(namePrefix, nameSuffix, beanPrefix, beanSuffix,
ns, "target/multiple_"+counter, withBeans, withInnerClasses);
}}}}}}}}}}}
ns, "target/multiple_"+counter, withBeans, withInnerClasses, withOriginalPositioning);
}}}}}}}}}}}}
}
@Test
@ -290,7 +291,7 @@ public class MetaDataExporterTest {
private void test(String namePrefix, String nameSuffix, String beanPrefix, String beanSuffix,
NamingStrategy namingStrategy, String target, boolean withBeans,
boolean withInnerClasses) throws SQLException{
boolean withInnerClasses, boolean withOrdinalPositioning) throws SQLException{
File targetDir = new File(target);
if (clean) {
try {
@ -318,6 +319,9 @@ public class MetaDataExporterTest {
if (withBeans) {
exporter.setBeanSerializer(beanSerializer);
}
if (withOrdinalPositioning) {
exporter.setColumnComparatorClass(OrdinalPositionComparator.class);
}
exporter.export(connection.getMetaData());
JavaCompiler compiler = new SimpleCompiler();

View File

@ -61,12 +61,14 @@ public final class ColumnMetadata implements Serializable {
* if the name is null
*/
public static ColumnMetadata named(String name) {
return new ColumnMetadata(name, null, true, UNDEFINED, UNDEFINED);
return new ColumnMetadata(null, name, null, true, UNDEFINED, UNDEFINED);
}
private static final int UNDEFINED = -1;
private final String name;
private final Integer index;
private final Integer jdbcType;
@ -76,8 +78,9 @@ public final class ColumnMetadata implements Serializable {
private final int decimalDigits;
private ColumnMetadata(String name, Integer jdbcType, boolean nullable, int size,
private ColumnMetadata(Integer index, String name, Integer jdbcType, boolean nullable, int size,
int decimalDigits) {
this.index = index;
this.name = name;
this.jdbcType = jdbcType;
this.nullable = nullable;
@ -88,6 +91,14 @@ public final class ColumnMetadata implements Serializable {
public String getName() {
return name;
}
public int getIndex() {
return index;
}
public ColumnMetadata withIndex(int index) {
return new ColumnMetadata(index, name, jdbcType, nullable, size, decimalDigits);
}
public int getJdbcType() {
return jdbcType;
@ -98,7 +109,7 @@ public final class ColumnMetadata implements Serializable {
}
public ColumnMetadata ofType(int jdbcType) {
return new ColumnMetadata(name, jdbcType, nullable, size, decimalDigits);
return new ColumnMetadata(index, name, jdbcType, nullable, size, decimalDigits);
}
public boolean isNullable() {
@ -106,7 +117,7 @@ public final class ColumnMetadata implements Serializable {
}
public ColumnMetadata notNull() {
return new ColumnMetadata(name, jdbcType, false, size, decimalDigits);
return new ColumnMetadata(index, name, jdbcType, false, size, decimalDigits);
}
/**
@ -123,7 +134,7 @@ public final class ColumnMetadata implements Serializable {
}
public ColumnMetadata withSize(int size) {
return new ColumnMetadata(name, jdbcType, nullable, size, decimalDigits);
return new ColumnMetadata(index, name, jdbcType, nullable, size, decimalDigits);
}
/**
@ -140,7 +151,7 @@ public final class ColumnMetadata implements Serializable {
}
public ColumnMetadata withDigits(int decimalDigits) {
return new ColumnMetadata(name, jdbcType, nullable, size, decimalDigits);
return new ColumnMetadata(index, name, jdbcType, nullable, size, decimalDigits);
}
@Override

View File

@ -15,6 +15,7 @@ package com.mysema.query.sql;
import static com.google.common.collect.ImmutableList.copyOf;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -47,9 +48,7 @@ public class RelationalPathBase<T> extends BeanPath<T> implements RelationalPath
@Nullable
private PrimaryKey<T> primaryKey;
private final List<Path<?>> columns = Lists.newArrayList();
private final Map<Path<?>, ColumnMetadata> columnMetadata = Maps.newHashMap();
private final Map<Path<?>, ColumnMetadata> columnMetadata = Maps.newLinkedHashMap();
private final List<ForeignKey<?>> foreignKeys = Lists.newArrayList();
@ -143,20 +142,19 @@ public class RelationalPathBase<T> extends BeanPath<T> implements RelationalPath
}
public Path<?>[] all() {
Path<?>[] all = new Path[columns.size()];
columns.toArray(all);
Path<?>[] all = new Path[columnMetadata.size()];
columnMetadata.keySet().toArray(all);
return all;
}
@Override
protected <P extends Path<?>> P add(P path) {
columns.add(path);
return path;
}
@Override
public List<Path<?>> getColumns() {
return columns;
return Arrays.asList(all());
}
@Override