Search in sources :

Example 1 with FieldRole

use of org.datanucleus.metadata.FieldRole in project datanucleus-rdbms by datanucleus.

the class MappingManagerImpl method createColumn.

/**
 * Method to create a column for a Java type mapping.
 * This is NOT used for persistable mappings - see method below.
 * @param mapping Java type mapping for the field
 * @param javaType The type of field being stored in this column
 * @param datastoreFieldIndex Index of the datastore field to use
 * @return The datastore field
 */
public Column createColumn(JavaTypeMapping mapping, String javaType, int datastoreFieldIndex) {
    AbstractMemberMetaData mmd = mapping.getMemberMetaData();
    FieldRole roleForField = mapping.getRoleForMember();
    Table tbl = mapping.getTable();
    // Take the column MetaData from the component that this mappings role relates to
    ColumnMetaData colmd = null;
    ColumnMetaDataContainer columnContainer = mmd;
    if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT || roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
        columnContainer = mmd.getElementMetaData();
    } else if (roleForField == FieldRole.ROLE_MAP_KEY) {
        columnContainer = mmd.getKeyMetaData();
    } else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
        columnContainer = mmd.getValueMetaData();
    }
    Column col;
    ColumnMetaData[] colmds;
    if (columnContainer != null && columnContainer.getColumnMetaData().length > datastoreFieldIndex) {
        colmd = columnContainer.getColumnMetaData()[datastoreFieldIndex];
        colmds = columnContainer.getColumnMetaData();
    } else {
        // If column specified add one (use any column name specified on field element)
        colmd = new ColumnMetaData();
        if (mmd.getColumnMetaData() != null && mmd.getColumnMetaData().length > datastoreFieldIndex) {
            colmd.setName(mmd.getColumnMetaData()[datastoreFieldIndex].getName());
        }
        if (columnContainer != null) {
            columnContainer.addColumn(colmd);
            colmds = columnContainer.getColumnMetaData();
        } else {
            colmds = new ColumnMetaData[1];
            colmds[0] = colmd;
        }
    }
    // Generate the column identifier
    IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
    DatastoreIdentifier identifier = null;
    if (colmd.getName() == null) {
        // No name specified, so generate the identifier from the field name
        if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT) {
            // Join table collection element
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_COLLECTION_ELEMENT);
        } else if (roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
            // Join table array element
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_ARRAY_ELEMENT);
        } else if (roleForField == FieldRole.ROLE_MAP_KEY) {
            // Join table map key
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_KEY);
        } else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
            // Join table map value
            identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_VALUE);
        } else {
            identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName());
            int i = 0;
            while (tbl.hasColumn(identifier)) {
                identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName() + "_" + i);
                i++;
            }
        }
        colmd.setName(identifier.getName());
    } else {
        // User has specified a name, so try to keep this unmodified
        identifier = idFactory.newColumnIdentifier(colmds[datastoreFieldIndex].getName(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), null, true);
    }
    // Create the column
    col = tbl.addColumn(javaType, identifier, mapping, colmd);
    if (mmd.isPrimaryKey()) {
        col.setPrimaryKey();
    }
    if (!(mmd.getParent() instanceof AbstractClassMetaData)) {
    // Embedded so can't be datastore-attributed
    } else {
        /*if (!mmd.getClassName(true).equals(mmd.getAbstractClassMetaData().getFullClassName()))
            {
                if (storeMgr.isStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass)
                {
                    if ((mmd.isPrimaryKey() && ((DatastoreClass)tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey())
                    {
                        NucleusLogger.GENERAL.info(">> Column addition " + mmd.getFullFieldName() + " IGNORING use of IDENTITY since override of base metadata! See RDBMSMappingManager");
                    }
                }
                // Overriding member, so ignore TODO This can be incorrect in many cases
            }
            else
            {*/
        if (storeMgr.isValueGenerationStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass) {
            if ((mmd.isPrimaryKey() && ((DatastoreClass) tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey()) {
                // Increment any PK field if we are in base class, and increment any other field
                col.setIdentity(true);
            }
        }
    /*}*/
    }
    if (mmd.getValueForExtension("select-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("select-function"), Column.WRAPPER_FUNCTION_SELECT);
    }
    if (mmd.getValueForExtension("insert-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("insert-function"), Column.WRAPPER_FUNCTION_INSERT);
    }
    if (mmd.getValueForExtension("update-function") != null) {
        col.setWrapperFunction(mmd.getValueForExtension("update-function"), Column.WRAPPER_FUNCTION_UPDATE);
    }
    setColumnNullability(mmd, colmd, col);
    if (mmd.getNullValue() == NullValue.DEFAULT) {
        // Users default should be applied if a null is to be inserted
        col.setDefaultable(colmd.getDefaultValue());
    }
    return col;
}
Also used : Table(org.datanucleus.store.rdbms.table.Table) ColumnMetaDataContainer(org.datanucleus.metadata.ColumnMetaDataContainer) Column(org.datanucleus.store.rdbms.table.Column) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) FieldRole(org.datanucleus.metadata.FieldRole) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Aggregations

AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 ColumnMetaDataContainer (org.datanucleus.metadata.ColumnMetaDataContainer)1 FieldRole (org.datanucleus.metadata.FieldRole)1 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)1 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)1 Column (org.datanucleus.store.rdbms.table.Column)1 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)1 Table (org.datanucleus.store.rdbms.table.Table)1