Search in sources :

Example 6 with MultiColumnConverter

use of org.datanucleus.store.types.converters.MultiColumnConverter 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

MultiColumnConverter (org.datanucleus.store.types.converters.MultiColumnConverter)6 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)4 TypeConverter (org.datanucleus.store.types.converters.TypeConverter)4 TypeManager (org.datanucleus.store.types.TypeManager)3 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)2 JdbcType (org.datanucleus.metadata.JdbcType)2 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 EmbeddedMetaData (org.datanucleus.metadata.EmbeddedMetaData)1 MetaDataManager (org.datanucleus.metadata.MetaDataManager)1 RelationType (org.datanucleus.metadata.RelationType)1 NoTableManagedException (org.datanucleus.store.rdbms.exceptions.NoTableManagedException)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1