Search in sources :

Example 16 with ColumnMetaData

use of org.datanucleus.metadata.ColumnMetaData 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 17 with ColumnMetaData

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

the class TypeConversionHelper method getStoredValueFromEnum.

/**
 * Convenience method to return the "value" of an Enum, for a field and role.
 * Firstly checks for a defined method on the Enum that returns the "value", otherwise falls back to use the ordinal.
 * @param mmd Metadata for the member
 * @param role Role of the Enum in this member
 * @param myEnum The enum
 * @return The "value" (String or Integer)
 */
public static Object getStoredValueFromEnum(AbstractMemberMetaData mmd, FieldRole role, Enum myEnum) {
    String methodName = null;
    // When nothing is specified we align to the JDO default (since JPA will always have jdbcType)
    boolean numeric = false;
    if (mmd != null) {
        ColumnMetaData[] colmds = null;
        if (role == FieldRole.ROLE_FIELD) {
            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) {
                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) {
                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) {
                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) {
                if (MetaDataUtils.isJdbcTypeNumeric(colmds[0].getJdbcType())) {
                    numeric = true;
                } else if (MetaDataUtils.isJdbcTypeString(colmds[0].getJdbcType())) {
                    numeric = false;
                }
            }
        }
    }
    if (methodName != null) {
        try {
            Method getterMethod = ClassUtils.getMethodForClass(myEnum.getClass(), methodName, null);
            return getterMethod.invoke(myEnum);
        } 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);
        }
    }
    // Fallback to standard Enum handling via ordinal() or name()
    return numeric ? myEnum.ordinal() : myEnum.name();
}
Also used : Method(java.lang.reflect.Method) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Example 18 with ColumnMetaData

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

the class ValidationNotNullAnnotationHandler method processMemberAnnotation.

/* (non-Javadoc)
     * @see org.datanucleus.metadata.annotations.MemberAnnotationHandler#processMemberAnnotation(org.datanucleus.metadata.annotations.AnnotationObject, org.datanucleus.metadata.AbstractMemberMetaData, org.datanucleus.ClassLoaderResolver)
     */
@Override
public void processMemberAnnotation(AnnotationObject annotation, AbstractMemberMetaData mmd, ClassLoaderResolver clr) {
    ColumnMetaData[] colmds = mmd.getColumnMetaData();
    if (colmds == null || colmds.length == 0) {
        ColumnMetaData colmd = new ColumnMetaData();
        colmd.setAllowsNull(false);
        mmd.addColumn(colmd);
        return;
    }
    if (colmds[0].getAllowsNull() == null) {
        // No length set, so
        colmds[0].setAllowsNull(false);
    }
}
Also used : ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Example 19 with ColumnMetaData

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

the class ValidationSizeAnnotationHandler method processMemberAnnotation.

/* (non-Javadoc)
     * @see org.datanucleus.metadata.annotations.MemberAnnotationHandler#processMemberAnnotation(org.datanucleus.metadata.annotations.AnnotationObject, org.datanucleus.metadata.AbstractMemberMetaData, org.datanucleus.ClassLoaderResolver)
     */
@Override
public void processMemberAnnotation(AnnotationObject annotation, AbstractMemberMetaData mmd, ClassLoaderResolver clr) {
    Map<String, Object> annotationValues = annotation.getNameValueMap();
    int max = (Integer) annotationValues.get("max");
    ColumnMetaData[] colmds = mmd.getColumnMetaData();
    // TODO This should only be processed when the member is STRING. Currently dont have access to that info
    if (colmds == null || colmds.length == 0) {
        ColumnMetaData colmd = new ColumnMetaData();
        colmd.setLength(max);
        mmd.addColumn(colmd);
        return;
    }
    if (colmds[0].getLength() == null) {
        // No length set, so
        colmds[0].setLength(max);
    }
}
Also used : ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Example 20 with ColumnMetaData

use of org.datanucleus.metadata.ColumnMetaData in project datanucleus-api-jdo by datanucleus.

the class IndexMetadataImpl method getColumns.

/* (non-Javadoc)
     * @see javax.jdo.metadata.IndexMetadata#getColumns()
     */
public ColumnMetadata[] getColumns() {
    String[] internalColumnNames = getInternal().getColumnNames();
    if (internalColumnNames == null) {
        return null;
    }
    ColumnMetadataImpl[] colmds = new ColumnMetadataImpl[internalColumnNames.length];
    for (int i = 0; i < colmds.length; i++) {
        ColumnMetaData internalColmd = new ColumnMetaData();
        internalColmd.setName(internalColumnNames[i]);
        colmds[i] = new ColumnMetadataImpl(internalColmd);
        colmds[i].parent = this;
    }
    return colmds;
}
Also used : ColumnMetaData(org.datanucleus.metadata.ColumnMetaData)

Aggregations

ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)85 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)28 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)16 ClassMetaData (org.datanucleus.metadata.ClassMetaData)14 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)13 ElementMetaData (org.datanucleus.metadata.ElementMetaData)13 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)11 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)10 JoinMetaData (org.datanucleus.metadata.JoinMetaData)9 MetaDataManager (org.datanucleus.metadata.MetaDataManager)9 MappingManager (org.datanucleus.store.rdbms.mapping.MappingManager)8 Column (org.datanucleus.store.rdbms.table.Column)8 NucleusContext (org.datanucleus.NucleusContext)7 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)7 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)7 PrimaryKeyMetaData (org.datanucleus.metadata.PrimaryKeyMetaData)7 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)7 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)7 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)6 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)6