Skip to content

Commit

Permalink
[#3233] Add DataTypeDefinition.getConverter()
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed May 5, 2014
1 parent 90202fb commit 591aa0f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
9 changes: 3 additions & 6 deletions jOOQ-codegen/src/main/java/org/jooq/util/JavaGenerator.java
Expand Up @@ -95,7 +95,6 @@
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;


Expand Down Expand Up @@ -1749,18 +1748,16 @@ protected void generateTable(SchemaDefinition schema, TableDefinition table) {
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\");",
Expand Down
Expand Up @@ -130,9 +130,20 @@ static DataTypeDefinition mapDefinedType(Definition container, Definition child,
// [#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();
Expand All @@ -148,30 +159,25 @@ static DataTypeDefinition mapDefinedType(Definition container, Definition child,

// [#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;
}
}
6 changes: 6 additions & 0 deletions jOOQ-meta/src/main/java/org/jooq/util/DataTypeDefinition.java
Expand Up @@ -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.
*/
Expand Down
Expand Up @@ -56,25 +56,31 @@ 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;
private final int precision;
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) {
Expand Down Expand Up @@ -131,6 +137,11 @@ public final String getType() {
return typeName;
}

@Override
public final String getConverter() {
return converter;
}

@Override
public final int getLength() {
return length;
Expand Down

0 comments on commit 591aa0f

Please sign in to comment.