Search in sources :

Example 1 with JdbcType

use of org.datanucleus.metadata.JdbcType in project datanucleus-rdbms by datanucleus.

the class MappingManagerImpl method getDefaultJavaTypeMapping.

/**
 * Method to return the default java type mapping class for a specified java type.
 * @param javaType java type
 * @param colmds Metadata for the column(s) (optional)
 * @return The mapping class to use (by default)
 */
protected MappingConverterDetails getDefaultJavaTypeMapping(Class javaType, ColumnMetaData[] colmds) {
    // Check for an explicit mapping
    Class<? extends JavaTypeMapping> cls = storeMgr.getMappingManager().getMappingType(javaType.getName());
    if (cls == null) {
        // No explicit mapping for this java type, so fall back to TypeConverter if available
        TypeManager typeMgr = storeMgr.getNucleusContext().getTypeManager();
        if (colmds != null && colmds.length > 0) {
            if (colmds.length > 1) {
                // Find TypeConverter with right number of columns
                Collection<TypeConverter> converters = typeMgr.getTypeConvertersForType(javaType);
                if (converters != null && !converters.isEmpty()) {
                    for (TypeConverter conv : converters) {
                        if (conv instanceof MultiColumnConverter) {
                            if (((MultiColumnConverter) conv).getDatastoreColumnTypes().length == colmds.length) {
                                return new MappingConverterDetails(TypeConverterMultiMapping.class, conv);
                            }
                        }
                    }
                }
            } else {
                JdbcType jdbcType = colmds[0].getJdbcType();
                if (jdbcType != null) {
                    // JDBC type specified so don't just take the default
                    TypeConverter conv = null;
                    if (MetaDataUtils.isJdbcTypeString(jdbcType)) {
                        conv = typeMgr.getTypeConverterForType(javaType, String.class);
                    } else if (MetaDataUtils.isJdbcTypeNumeric(jdbcType)) {
                        conv = typeMgr.getTypeConverterForType(javaType, Long.class);
                    } else if (jdbcType == JdbcType.TIMESTAMP) {
                        conv = typeMgr.getTypeConverterForType(javaType, Timestamp.class);
                    } else if (jdbcType == JdbcType.TIME) {
                        conv = typeMgr.getTypeConverterForType(javaType, Time.class);
                    } else if (jdbcType == JdbcType.DATE) {
                        conv = typeMgr.getTypeConverterForType(javaType, Date.class);
                    }
                    if (conv != null) {
                        return new MappingConverterDetails(TypeConverterMapping.class, conv);
                    }
                }
            }
        }
        TypeConverter conv = typeMgr.getDefaultTypeConverterForType(javaType);
        if (conv != null) {
            if (conv instanceof MultiColumnConverter) {
                return new MappingConverterDetails(TypeConverterMultiMapping.class, conv);
            }
            return new MappingConverterDetails(TypeConverterMapping.class, conv);
        }
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("041000", javaType.getName()));
        return null;
    }
    return new MappingConverterDetails(cls);
}
Also used : TypeConverter(org.datanucleus.store.types.converters.TypeConverter) TypeManager(org.datanucleus.store.types.TypeManager) JdbcType(org.datanucleus.metadata.JdbcType) MultiColumnConverter(org.datanucleus.store.types.converters.MultiColumnConverter) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Example 2 with JdbcType

use of org.datanucleus.metadata.JdbcType in project datanucleus-core by datanucleus.

the class TypeConversionHelper method getJdbcTypeForEnum.

public static JdbcType getJdbcTypeForEnum(AbstractMemberMetaData mmd, FieldRole role, ClassLoaderResolver clr) {
    JdbcType jdbcType = JdbcType.VARCHAR;
    if (mmd != null) {
        String methodName = null;
        Class enumType = null;
        ColumnMetaData[] colmds = null;
        if (role == FieldRole.ROLE_FIELD) {
            enumType = mmd.getType();
            if (mmd.hasExtension(MetaData.EXTENSION_MEMBER_ENUM_VALUE_GETTER)) {
                methodName = mmd.getValueForExtension(MetaData.EXTENSION_MEMBER_ENUM_VALUE_GETTER);
            }
            colmds = mmd.getColumnMetaData();
        } else if (role == FieldRole.ROLE_COLLECTION_ELEMENT || role == FieldRole.ROLE_ARRAY_ELEMENT) {
            if (mmd.getElementMetaData() != null) {
                enumType = clr.classForName(mmd.hasCollection() ? mmd.getCollection().getElementType() : mmd.getArray().getElementType());
                if (mmd.getElementMetaData().hasExtension(MetaData.EXTENSION_MEMBER_ENUM_VALUE_GETTER)) {
                    methodName = mmd.getElementMetaData().getValueForExtension(MetaData.EXTENSION_MEMBER_ENUM_VALUE_GETTER);
                }
                colmds = mmd.getElementMetaData().getColumnMetaData();
            }
        } else if (role == FieldRole.ROLE_MAP_KEY) {
            if (mmd.getKeyMetaData() != null) {
                enumType = clr.classForName(mmd.getMap().getKeyType());
                if (mmd.getKeyMetaData().hasExtension(MetaData.EXTENSION_MEMBER_ENUM_VALUE_GETTER)) {
                    methodName = mmd.getKeyMetaData().getValueForExtension(MetaData.EXTENSION_MEMBER_ENUM_VALUE_GETTER);
                }
                colmds = mmd.getKeyMetaData().getColumnMetaData();
            }
        } else if (role == FieldRole.ROLE_MAP_VALUE) {
            if (mmd.getValueMetaData() != null) {
                enumType = clr.classForName(mmd.getMap().getValueType());
                if (mmd.getValueMetaData().hasExtension(MetaData.EXTENSION_MEMBER_ENUM_VALUE_GETTER)) {
                    methodName = mmd.getValueMetaData().getValueForExtension(MetaData.EXTENSION_MEMBER_ENUM_VALUE_GETTER);
                }
                colmds = mmd.getValueMetaData().getColumnMetaData();
            }
        }
        if (methodName == null) {
            if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                jdbcType = colmds[0].getJdbcType();
            }
        } else {
            try {
                Method getterMethod = ClassUtils.getMethodForClass(enumType, methodName, null);
                Class returnType = getterMethod.getReturnType();
                if (returnType == short.class || returnType == int.class || returnType == long.class || Number.class.isAssignableFrom(returnType)) {
                    return JdbcType.INTEGER;
                }
                return JdbcType.VARCHAR;
            } catch (Exception e) {
                NucleusLogger.PERSISTENCE.warn("Specified enum value-getter for method " + methodName + " on field " + mmd.getFullFieldName() + " gave an error on extracting the value", e);
            }
        }
    }
    return jdbcType;
}
Also used : JdbcType(org.datanucleus.metadata.JdbcType) Method(java.lang.reflect.Method) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Example 3 with JdbcType

use of org.datanucleus.metadata.JdbcType in project datanucleus-core by datanucleus.

the class CompleteClassTable method getTypeConverterForMember.

protected TypeConverter getTypeConverterForMember(AbstractMemberMetaData mmd, ColumnMetaData[] colmds, TypeManager typeMgr) {
    TypeConverter typeConv = null;
    String typeConvName = mmd.getTypeConverterName();
    if (typeConvName != null) {
        // User has specified the TypeConverter
        typeConv = typeMgr.getTypeConverterForName(typeConvName);
        if (typeConv == null) {
            throw new NucleusUserException(Localiser.msg("044062", mmd.getFullFieldName(), typeConvName));
        }
    } else {
        // No explicit TypeConverter so maybe there is an auto-apply converter for this member type
        typeConv = typeMgr.getAutoApplyTypeConverterForType(mmd.getType());
    }
    if (typeConv == null) {
        // Try to find a TypeConverter matching any column JDBC type definition
        if (colmds != null && colmds.length > 1) {
            // Multiple columns, so try to find a converter with the right number of columns (note we could, in future, check the types of columns also)
            Collection<TypeConverter> converters = typeMgr.getTypeConvertersForType(mmd.getType());
            if (converters != null && !converters.isEmpty()) {
                for (TypeConverter conv : converters) {
                    if (conv instanceof MultiColumnConverter && ((MultiColumnConverter) conv).getDatastoreColumnTypes().length == colmds.length) {
                        typeConv = conv;
                        break;
                    }
                }
            }
            if (typeConv == null) {
            // TODO Throw exception since user column specification leaves no possible converter
            }
        } else {
            // Single column, so try to match the JDBC type if provided
            JdbcType jdbcType = colmds != null && colmds.length > 0 ? colmds[0].getJdbcType() : null;
            if (jdbcType != null) {
                // JDBC type specified so don't just take the default
                if (MetaDataUtils.isJdbcTypeString(jdbcType)) {
                    typeConv = typeMgr.getTypeConverterForType(mmd.getType(), String.class);
                } else if (MetaDataUtils.isJdbcTypeNumeric(jdbcType)) {
                    typeConv = typeMgr.getTypeConverterForType(mmd.getType(), Long.class);
                } else if (jdbcType == JdbcType.TIMESTAMP) {
                    typeConv = typeMgr.getTypeConverterForType(mmd.getType(), Timestamp.class);
                } else if (jdbcType == JdbcType.TIME) {
                    typeConv = typeMgr.getTypeConverterForType(mmd.getType(), Time.class);
                } else if (jdbcType == JdbcType.DATE) {
                    typeConv = typeMgr.getTypeConverterForType(mmd.getType(), Date.class);
                }
            // TODO Support other JDBC types
            } else {
                // Fallback to default type converter for this member type (if any)
                typeConv = typeMgr.getDefaultTypeConverterForType(mmd.getType());
            }
        }
    }
    if (schemaVerifier != null) {
        // Make sure that the schema verifier supports this conversion
        typeConv = schemaVerifier.verifyTypeConverterForMember(mmd, typeConv);
    }
    return typeConv;
}
Also used : TypeConverter(org.datanucleus.store.types.converters.TypeConverter) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) JdbcType(org.datanucleus.metadata.JdbcType) MultiColumnConverter(org.datanucleus.store.types.converters.MultiColumnConverter) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Aggregations

JdbcType (org.datanucleus.metadata.JdbcType)3 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 MultiColumnConverter (org.datanucleus.store.types.converters.MultiColumnConverter)2 TypeConverter (org.datanucleus.store.types.converters.TypeConverter)2 Method (java.lang.reflect.Method)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 TypeManager (org.datanucleus.store.types.TypeManager)1