Search in sources :

Example 11 with TypeConverter

use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-core by datanucleus.

the class TypeManagerTest method checkStringConverterType.

private void checkStringConverterType(Class type, Class expectedStringConverterType) {
    TypeConverter conv = typeMgr.getTypeConverterForType(type, String.class);
    assertEquals(expectedStringConverterType, conv.getClass());
}
Also used : TypeConverter(org.datanucleus.store.types.converters.TypeConverter)

Example 12 with TypeConverter

use of org.datanucleus.store.types.converters.TypeConverter in project datanucleus-core by datanucleus.

the class TypeManagerTest method testGetDatastoreTypeForTypeConverter.

public void testGetDatastoreTypeForTypeConverter() {
    TypeConverter conv1 = typeMgr.getTypeConverterForType(URL.class, String.class);
    Class cls1 = typeMgr.getDatastoreTypeForTypeConverter(conv1, URL.class);
    assertEquals(String.class, cls1);
    TypeConverter conv2 = typeMgr.getTypeConverterForName("dn.date-long");
    Class cls2 = typeMgr.getDatastoreTypeForTypeConverter(conv2, Date.class);
    assertEquals(Long.class, cls2);
}
Also used : TypeConverter(org.datanucleus.store.types.converters.TypeConverter)

Example 13 with TypeConverter

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

TypeConverter (org.datanucleus.store.types.converters.TypeConverter)13 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)8 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)7 NucleusException (org.datanucleus.exceptions.NucleusException)6 NoTableManagedException (org.datanucleus.store.rdbms.exceptions.NoTableManagedException)5 TypeConverterMapping (org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping)5 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)4 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)4 TypeConverterMultiMapping (org.datanucleus.store.rdbms.mapping.java.TypeConverterMultiMapping)4 TypeManager (org.datanucleus.store.types.TypeManager)4 MultiColumnConverter (org.datanucleus.store.types.converters.MultiColumnConverter)4 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 BigDecimal (java.math.BigDecimal)2 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 JdbcType (org.datanucleus.metadata.JdbcType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1