Search in sources :

Example 1 with CustomType

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

the class AbstractDatabase method getConfiguredCustomType.

@Override
@Deprecated
public final CustomType getConfiguredCustomType(String typeName) {
    // The user type name that is passed here can be null.
    if (typeName == null)
        return null;
    Iterator<CustomType> it1 = getConfiguredCustomTypes().iterator();
    while (it1.hasNext()) {
        CustomType type = it1.next();
        if (type == null || (type.getName() == null && type.getType() == null)) {
            log.warn("Invalid custom type encountered: " + type);
            it1.remove();
            continue;
        }
        if (StringUtils.equals(type.getType() != null ? type.getType() : type.getName(), typeName)) {
            return type;
        }
    }
    Iterator<ForcedType> it2 = configuredForcedTypes.iterator();
    while (it2.hasNext()) {
        ForcedType type = it2.next();
        if (type.getExpressions() != null) {
            type.setIncludeExpression(type.getExpressions());
            type.setExpressions(null);
            log.warn("DEPRECATED", "The <expressions/> element in <forcedType/> is deprecated. Use <includeExpression/> instead: " + type);
        }
        if (type.getExpression() != null) {
            type.setIncludeExpression(type.getExpression());
            type.setExpression(null);
            log.warn("DEPRECATED", "The <expression/> element in <forcedType/> is deprecated. Use <includeExpression/> instead: " + type);
        }
        if (type.getTypes() != null) {
            type.setIncludeTypes(type.getTypes());
            type.setTypes(null);
            log.warn("DEPRECATED", "The <types/> element in <forcedType/> is deprecated. Use <includeTypes/> instead: " + type);
        }
        if (StringUtils.isBlank(type.getName())) {
            if (StringUtils.isBlank(type.getUserType())) {
                log.warn("Bad configuration for <forcedType/>. Either <name/> or <userType/> is required: " + type);
                it2.remove();
                continue;
            }
            if (StringUtils.isBlank(type.getBinding()) && StringUtils.isBlank(type.getConverter()) && !Boolean.TRUE.equals(type.isEnumConverter()) && type.getLambdaConverter() == null) {
                log.warn("Bad configuration for <forcedType/>. Either <binding/> or <converter/> or <enumConverter/> or <lambdaConverter/> is required: " + type);
                it2.remove();
                continue;
            }
        } else {
            if (!StringUtils.isBlank(type.getUserType())) {
                log.warn("Bad configuration for <forcedType/>. <userType/> is not allowed when <name/> is provided: " + type);
                type.setUserType(null);
            }
            if (!StringUtils.isBlank(type.getBinding())) {
                log.warn("Bad configuration for <forcedType/>. <binding/> is not allowed when <name/> is provided: " + type);
                type.setBinding(null);
            }
            if (!StringUtils.isBlank(type.getConverter())) {
                log.warn("Bad configuration for <forcedType/>. <converter/> is not allowed when <name/> is provided: " + type);
                type.setConverter(null);
            }
            if (Boolean.TRUE.equals(type.isEnumConverter())) {
                log.warn("Bad configuration for <forcedType/>. <enumConverter/> is not allowed when <name/> is provided: " + type);
                type.setEnumConverter(null);
            }
            if (type.getLambdaConverter() != null) {
                log.warn("Bad configuration for <forcedType/>. <lambdaConverter/> is not allowed when <name/> is provided: " + type);
                type.setLambdaConverter(null);
            }
        }
        if (type.getUserType() != null && StringUtils.equals(type.getUserType(), typeName)) {
            return customType(this, type);
        }
    }
    return null;
}
Also used : CustomType(org.jooq.meta.jaxb.CustomType) ForcedType(org.jooq.meta.jaxb.ForcedType)

Example 2 with CustomType

use of org.jooq.meta.jaxb.CustomType 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

CustomType (org.jooq.meta.jaxb.CustomType)2 ForcedType (org.jooq.meta.jaxb.ForcedType)2 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 LambdaConverter (org.jooq.meta.jaxb.LambdaConverter)1