Search in sources :

Example 1 with LambdaConverter

use of org.jooq.meta.jaxb.LambdaConverter in project jOOQ by jOOQ.

the class AbstractTypedElementDefinition method mapDefinedType.

public static final DataTypeDefinition mapDefinedType(Definition container, Definition child, DataTypeDefinition definedType, JavaTypeResolver resolver) {
    DataTypeDefinition result = definedType;
    Database db = container.getDatabase();
    log.debug("Type mapping", child + " with type " + definedType.getType());
    // [#976] Mapping DATE as TIMESTAMP
    if (db.dateAsTimestamp()) {
        DataType<?> dataType = null;
        try {
            dataType = getDataType(db, result.getType(), 0, 0);
        } catch (SQLDialectNotSupportedException ignore) {
        }
        if (dataType != null) {
            // [#5239] [#5762] [#6453] Don't rely on getSQLType()
            if (SQLDataType.DATE.equals(dataType.getSQLDataType())) {
                DataType<?> forcedDataType = getDataType(db, SQLDataType.TIMESTAMP.getTypeName(), 0, 0);
                String binding = DateAsTimestampBinding.class.getName();
                if (db.javaTimeTypes())
                    binding = org.jooq.impl.LocalDateAsLocalDateTimeBinding.class.getName();
                result = new DefaultDataTypeDefinition(db, child.getSchema(), forcedDataType.getTypeName(), 0, 0, 0, result.isNullable(), result.getDefaultValue(), result.isIdentity(), (Name) null, null, binding, null);
            }
        }
    }
    // [#677] Forced types for matching regular expressions
    ForcedType forcedType = db.getConfiguredForcedType(child, definedType);
    if (forcedType != null) {
        String uType = forcedType.getName();
        String converter = null;
        String binding = result.getBinding();
        CustomType customType = customType(db, forcedType);
        if (customType != null) {
            uType = (!StringUtils.isBlank(customType.getType())) ? customType.getType() : customType.getName();
            // [#5877] [#6567] EnumConverters profit from simplified configuration
            if (Boolean.TRUE.equals(customType.isEnumConverter()) || EnumConverter.class.getName().equals(customType.getConverter())) {
                String tType = tType(db, resolver, definedType);
                converter = resolver.constructorCall(EnumConverter.class.getName() + "<" + resolver.ref(tType) + ", " + resolver.ref(uType) + ">") + "(" + resolver.classLiteral(tType) + ", " + resolver.classLiteral(uType) + ")";
            } else if (customType.getLambdaConverter() != null) {
                LambdaConverter c = customType.getLambdaConverter();
                String tType = tType(db, resolver, definedType);
                converter = resolver.ref(Converter.class) + ".of" + (!FALSE.equals(c.isNullable()) ? "Nullable" : "") + "(" + resolver.classLiteral(tType) + ", " + resolver.classLiteral(uType) + ", " + c.getFrom() + ", " + c.getTo() + ")";
            } else if (!StringUtils.isBlank(customType.getConverter())) {
                converter = customType.getConverter();
            }
            if (!StringUtils.isBlank(customType.getBinding()))
                binding = customType.getBinding();
        }
        if (uType != null) {
            db.markUsed(forcedType);
            log.info("Forcing type", child + " to " + forcedType);
            DataType<?> forcedDataType = null;
            boolean n = result.isNullable();
            String d = result.getDefaultValue();
            boolean i = result.isIdentity();
            boolean r = result.isReadonly();
            String g = result.getGeneratedAlwaysAs();
            int l = 0;
            int p = 0;
            int s = 0;
            // [#2486] Allow users to override length, precision, and scale
            Matcher matcher = LENGTH_PRECISION_SCALE_PATTERN.matcher(uType);
            if (matcher.find()) {
                if (!isEmpty(matcher.group(1))) {
                    l = p = convert(matcher.group(1), int.class);
                } else {
                    p = convert(matcher.group(2), int.class);
                    s = convert(matcher.group(3), int.class);
                }
            }
            try {
                forcedDataType = getDataType(db, uType, p, s);
            } catch (SQLDialectNotSupportedException ignore) {
            }
            // [#677] SQLDataType matches are actual type-rewrites
            if (forcedDataType != null) {
                // [#3704] When <forcedType/> matches a custom type AND a data type rewrite, the rewrite was usually accidental.
                if (customType != null)
                    log.warn("Custom type conflict", child + " has custom type " + customType + " forced by " + forcedType + " but a data type rewrite applies");
                result = new DefaultDataTypeDefinition(db, child.getSchema(), uType, l, p, s, n, r, g, d, i, (Name) null, converter, binding, null);
            } else // Other forced types are UDT's, enums, etc.
            if (customType != null) {
                l = result.getLength();
                p = result.getPrecision();
                s = result.getScale();
                String t = result.getType();
                Name u = result.getQualifiedUserType();
                result = new DefaultDataTypeDefinition(db, definedType.getSchema(), t, l, p, s, n, r, g, d, i, u, converter, binding, uType);
            } else // [#4597] If we don't have a type-rewrite (forcedDataType) or a
            // matching customType, the user probably malconfigured
            // their <forcedTypes/> or <customTypes/>
            {
                // [#7373] [#10944] Refer to <customType/> only if someone is still using the feature
                if (db.getConfiguredCustomTypes().isEmpty())
                    log.warn("Bad configuration for <forcedType/> " + forcedType.getName() + ". No matching SQLDataType found: " + forcedType);
                else
                    log.warn("Bad configuration for <forcedType/> " + forcedType.getName() + ". No matching <customType/> found, and no matching SQLDataType found: " + forcedType);
            }
        }
    }
    return result;
}
Also used : CustomType(org.jooq.meta.jaxb.CustomType) ForcedType(org.jooq.meta.jaxb.ForcedType) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) LambdaConverter(org.jooq.meta.jaxb.LambdaConverter) Matcher(java.util.regex.Matcher) Name(org.jooq.Name) Converter(org.jooq.Converter) LambdaConverter(org.jooq.meta.jaxb.LambdaConverter) EnumConverter(org.jooq.impl.EnumConverter)

Aggregations

Matcher (java.util.regex.Matcher)1 Converter (org.jooq.Converter)1 Name (org.jooq.Name)1 SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)1 EnumConverter (org.jooq.impl.EnumConverter)1 CustomType (org.jooq.meta.jaxb.CustomType)1 ForcedType (org.jooq.meta.jaxb.ForcedType)1 LambdaConverter (org.jooq.meta.jaxb.LambdaConverter)1