Search in sources :

Example 1 with ForcedType

use of org.jooq.util.jaxb.ForcedType in project jOOQ by jOOQ.

the class AbstractTypedElementDefinition method mapDefinedType.

static DataTypeDefinition mapDefinedType(Definition container, Definition child, DataTypeDefinition definedType) {
    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 = DefaultDataType.getDataType(db.getDialect(), result.getType(), 0, 0);
        } catch (SQLDialectNotSupportedException ignore) {
        }
        if (dataType != null) {
            if (dataType.getSQLType() == Types.DATE) {
                DataType<?> forcedDataType = DefaultDataType.getDataType(db.getDialect(), SQLDataType.TIMESTAMP.getTypeName(), 0, 0);
                result = new DefaultDataTypeDefinition(db, child.getSchema(), forcedDataType.getTypeName(), 0, 0, 0, result.isNullable(), result.getDefaultValue(), (Name) null, null, DateAsTimestampBinding.class.getName());
            }
        }
    }
    // [#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();
            if (Boolean.TRUE.equals(customType.isEnumConverter())) {
                String tType = DefaultDataType.getDataType(db.getDialect(), definedType.getType(), definedType.getPrecision(), definedType.getScale()).getType().getName();
                converter = "new " + EnumConverter.class.getName() + "<" + tType + ", " + uType + ">(" + tType + ".class, " + uType + ".class)";
            } else if (!StringUtils.isBlank(customType.getConverter())) {
                converter = customType.getConverter();
            }
            if (!StringUtils.isBlank(customType.getBinding()))
                binding = customType.getBinding();
        }
        if (uType != null) {
            log.info("Forcing type", child + " with type " + definedType.getType() + " into " + uType + (converter != null ? " using converter " + converter : "") + (binding != null ? " using binding " + binding : ""));
            DataType<?> forcedDataType = null;
            boolean n = result.isNullable();
            String d = result.getDefaultValue();
            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 = DefaultDataType.getDataType(db.getDialect(), uType, p, s);
            } catch (SQLDialectNotSupportedException ignore) {
            }
            // [#677] SQLDataType matches are actual type-rewrites
            if (forcedDataType != null) {
                result = new DefaultDataTypeDefinition(db, child.getSchema(), uType, l, p, s, n, d, (Name) null, converter, binding);
            } 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, child.getSchema(), t, l, p, s, n, d, 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/>
            {
                StringWriter writer = new StringWriter();
                JAXB.marshal(forcedType, writer);
                log.warn("Bad configuration for <forcedType/> " + forcedType.getName() + ". No matching <customType/> found, and no matching SQLDataType found: " + writer);
            }
        }
    }
    return result;
}
Also used : CustomType(org.jooq.util.jaxb.CustomType) ForcedType(org.jooq.util.jaxb.ForcedType) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) Matcher(java.util.regex.Matcher) Name(org.jooq.Name) StringWriter(java.io.StringWriter) EnumConverter(org.jooq.impl.EnumConverter)

Example 2 with ForcedType

use of org.jooq.util.jaxb.ForcedType 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 = configuredCustomTypes.iterator();
    while (it1.hasNext()) {
        CustomType type = it1.next();
        if (type == null || (type.getName() == null && type.getType() == null)) {
            try {
                StringWriter writer = new StringWriter();
                JAXB.marshal(type, writer);
                log.warn("Invalid custom type encountered: " + writer.toString());
            } catch (Exception e) {
                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.setExpression(type.getExpressions());
            type.setExpressions(null);
            log.warn("DEPRECATED", "The <expressions/> element in <forcedType/> is deprecated. Use <expression/> instead: " + toString(type));
        }
        if (StringUtils.isBlank(type.getName())) {
            if (StringUtils.isBlank(type.getUserType())) {
                log.warn("Bad configuration for <forcedType/>. Either <name/> or <userType/> is required: " + toString(type));
                it2.remove();
                continue;
            }
            if (StringUtils.isBlank(type.getBinding()) && StringUtils.isBlank(type.getConverter()) && !Boolean.TRUE.equals(type.isEnumConverter())) {
                log.warn("Bad configuration for <forcedType/>. Either <binding/> or <converter/> or <enumConverter/> is required: " + toString(type));
                it2.remove();
                continue;
            }
        } else {
            if (!StringUtils.isBlank(type.getUserType())) {
                log.warn("Bad configuration for <forcedType/>. <userType/> is not allowed when <name/> is provided: " + toString(type));
                type.setUserType(null);
            }
            if (!StringUtils.isBlank(type.getBinding())) {
                log.warn("Bad configuration for <forcedType/>. <binding/> is not allowed when <name/> is provided: " + toString(type));
                type.setBinding(null);
            }
            if (!StringUtils.isBlank(type.getConverter())) {
                log.warn("Bad configuration for <forcedType/>. <converter/> is not allowed when <name/> is provided: " + toString(type));
                type.setConverter(null);
            }
            if (Boolean.TRUE.equals(type.isEnumConverter())) {
                log.warn("Bad configuration for <forcedType/>. <enumConverter/> is not allowed when <name/> is provided: " + toString(type));
                type.setEnumConverter(null);
            }
        }
        if (type.getUserType() != null && StringUtils.equals(type.getUserType(), typeName)) {
            return customType(this, type);
        }
    }
    return null;
}
Also used : CustomType(org.jooq.util.jaxb.CustomType) ForcedType(org.jooq.util.jaxb.ForcedType) StringWriter(java.io.StringWriter) SQLException(java.sql.SQLException) DataAccessException(org.jooq.exception.DataAccessException) IOException(java.io.IOException)

Aggregations

StringWriter (java.io.StringWriter)2 CustomType (org.jooq.util.jaxb.CustomType)2 ForcedType (org.jooq.util.jaxb.ForcedType)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Matcher (java.util.regex.Matcher)1 Name (org.jooq.Name)1 DataAccessException (org.jooq.exception.DataAccessException)1 SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)1 EnumConverter (org.jooq.impl.EnumConverter)1