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;
}
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();
}
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);
}
}
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);
}
}
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;
}
Aggregations