use of org.datanucleus.store.rdbms.mapping.java.OrderIndexMapping in project datanucleus-rdbms by datanucleus.
the class ClassTable method addOrderColumn.
/**
* Adds an ordering column to the element table (this) in FK list relationships.
* Used to store the position of the element in the List.
* If the <order> provides a mapped-by, this will return the existing column mapping.
* @param mmd The MetaData of the field/property with the list for the column to map to
* @return The Mapping for the order column
*/
private JavaTypeMapping addOrderColumn(AbstractMemberMetaData mmd, ClassLoaderResolver clr) {
Class indexType = Integer.class;
JavaTypeMapping orderIndexMapping = new OrderIndexMapping();
orderIndexMapping.initialize(storeMgr, indexType.getName());
orderIndexMapping.setMemberMetaData(mmd);
orderIndexMapping.setTable(this);
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
DatastoreIdentifier indexColumnName = null;
ColumnMetaData colmd = null;
// Allow for any user definition in OrderMetaData
OrderMetaData omd = mmd.getOrderMetaData();
if (omd != null) {
colmd = (omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0 ? omd.getColumnMetaData()[0] : null);
if (omd.getMappedBy() != null) {
// User has defined ordering using the column(s) of an existing field.
// Not adding anything so just set table back to "initialised"
state = TABLE_STATE_INITIALIZED;
JavaTypeMapping orderMapping = getMemberMapping(omd.getMappedBy());
if (!(orderMapping instanceof IntegerMapping) && !(orderMapping instanceof LongMapping)) {
throw new NucleusUserException(Localiser.msg("057022", mmd.getFullFieldName(), omd.getMappedBy()));
}
return orderMapping;
}
String colName = null;
if (omd.getColumnMetaData() != null && omd.getColumnMetaData().length > 0 && omd.getColumnMetaData()[0].getName() != null) {
// User-defined name so create an identifier using it
colName = omd.getColumnMetaData()[0].getName();
indexColumnName = idFactory.newColumnIdentifier(colName);
}
}
if (indexColumnName == null) {
// No name defined so generate one
indexColumnName = idFactory.newForeignKeyFieldIdentifier(mmd, null, null, storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(indexType), FieldRole.ROLE_INDEX);
}
Column column = addColumn(indexType.getName(), indexColumnName, orderIndexMapping, colmd);
if (colmd == null || (colmd.getAllowsNull() == null) || (colmd.getAllowsNull() != null && colmd.isAllowsNull())) {
// User either wants it nullable, or havent specified anything, so make it nullable
column.setNullable(true);
}
storeMgr.getMappingManager().createDatastoreMapping(orderIndexMapping, column, indexType.getName());
return orderIndexMapping;
}
Aggregations