[#3233] Add DataTypeDefinition.getConverter()

This commit is contained in:
Lukas Eder 2014-05-05 15:14:26 +02:00
parent 90202fbb90
commit 591aa0f322
4 changed files with 40 additions and 20 deletions

View File

@ -95,7 +95,6 @@ import org.jooq.tools.StringUtils;
import org.jooq.tools.reflect.Reflect;
import org.jooq.tools.reflect.ReflectException;
import org.jooq.util.GeneratorStrategy.Mode;
import org.jooq.util.jaxb.CustomType;
import org.jooq.util.postgres.PostgresDatabase;
@ -1749,18 +1748,16 @@ public class JavaGenerator extends AbstractGenerator {
final String columnId = getStrategy().getJavaIdentifier(column);
final String columnName = column.getName();
final String columnComment = StringUtils.defaultString(column.getComment());
final CustomType columnCustomType = database.getConfiguredCustomType(column.getType().getUserType());
final String columnConverterType = column.getType().getConverter();
String isStatic = generateInstanceFields() ? "" : "static ";
String tableRef = generateInstanceFields() ? "this" : getStrategy().getJavaIdentifier(table);
out.tab(1).javadoc("The column <code>%s</code>.%s", column.getQualifiedOutputName(), defaultIfBlank(" " + columnComment, ""));
if (columnCustomType != null) {
String converter = columnCustomType.getConverter();
if (columnConverterType != null) {
out.tab(1).println("public %sfinal %s<%s, %s> %s = createField(\"%s\", %s, %s, \"%s\", new %s());",
isStatic, TableField.class, recordType, columnType, columnId, columnName, columnTypeRef, tableRef, escapeString(columnComment), converter);
isStatic, TableField.class, recordType, columnType, columnId, columnName, columnTypeRef, tableRef, escapeString(columnComment), columnConverterType);
}
else {
out.tab(1).println("public %sfinal %s<%s, %s> %s = createField(\"%s\", %s, %s, \"%s\");",

View File

@ -130,9 +130,20 @@ abstract class AbstractTypedElementDefinition<T extends Definition>
// [#677] Forced types for matching regular expressions
ForcedType forcedType = db.getConfiguredForcedType(child, definedType);
if (forcedType != null) {
String type = getCustomType(db, forcedType.getName());
String type = forcedType.getName();
String converter = null;
log.info("Forcing type", child + " into " + type);
CustomType customType = customType(db, forcedType.getName());
if (customType != null) {
type = (!StringUtils.isBlank(customType.getType()))
? customType.getType()
: customType.getName();
converter = customType.getConverter();
}
log.info("Forcing type", child + " into " + type + (converter != null ? " using converter " + converter : ""));
DataType<?> forcedDataType = null;
String t = result.getType();
@ -148,30 +159,25 @@ abstract class AbstractTypedElementDefinition<T extends Definition>
// [#677] SQLDataType matches are actual type-rewrites
if (forcedDataType != null) {
result = new DefaultDataTypeDefinition(db, child.getSchema(), type, l, p, s, n, d);
result = new DefaultDataTypeDefinition(db, child.getSchema(), type, l, p, s, n, d, null, converter);
}
// Other forced types are UDT's, enums, etc.
else {
result = new DefaultDataTypeDefinition(db, child.getSchema(), t, l, p, s, n, d, type);
result = new DefaultDataTypeDefinition(db, child.getSchema(), t, l, p, s, n, d, type, converter);
}
}
return result;
}
private static String getCustomType(Database db, String name) {
static CustomType customType(Database db, String name) {
for (CustomType type : db.getConfiguredCustomTypes()) {
if (name.equals(type.getName())) {
if (!StringUtils.isBlank(type.getType())) {
return type.getType();
}
else {
return type.getName();
}
return type;
}
}
return name;
return null;
}
}

View File

@ -53,6 +53,12 @@ public interface DataTypeDefinition {
*/
String getType();
/**
* The converter type that is applied to this data type, or
* <code>null</code>, if no such converter type is configured.
*/
String getConverter();
/**
* The type's length.
*/

View File

@ -56,6 +56,7 @@ public class DefaultDataTypeDefinition implements DataTypeDefinition {
private final SchemaDefinition schema;
private final String typeName;
private final String udtName;
private final String converter;
private final boolean nullable;
private final boolean defaulted;
private final int length;
@ -63,18 +64,23 @@ public class DefaultDataTypeDefinition implements DataTypeDefinition {
private final int scale;
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName) {
this(database, schema, typeName, null, null, null, null, null);
this(database, schema, typeName, null, null, null, null, null, null);
}
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, Boolean defaultable) {
this(database, schema, typeName, length, precision, scale, nullable, defaultable, typeName);
this(database, schema, typeName, length, precision, scale, nullable, defaultable, typeName, null);
}
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, Boolean defaultable, String udtName) {
this(database, schema, typeName, length, precision, scale, nullable, defaultable, udtName, null);
}
public DefaultDataTypeDefinition(Database database, SchemaDefinition schema, String typeName, Number length, Number precision, Number scale, Boolean nullable, Boolean defaultable, String udtName, String converter) {
this.database = database;
this.schema = schema;
this.typeName = typeName;
this.udtName = udtName;
this.converter = converter;
// Some dialects do not distinguish between length and precision...
if (length != null && precision != null && length.intValue() != 0 && precision.intValue() != 0) {
@ -131,6 +137,11 @@ public class DefaultDataTypeDefinition implements DataTypeDefinition {
return typeName;
}
@Override
public final String getConverter() {
return converter;
}
@Override
public final int getLength() {
return length;