Search in sources :

Example 96 with AbstractMemberMetaData

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

the class LoadFieldManager method internalFetchObjectField.

/**
 * Method to fetch an object field whether it is SCO collection, PC, or whatever for the fetchplan
 * process.
 * @param fieldNumber Number of the field
 * @return The object
 */
protected Object internalFetchObjectField(int fieldNumber) {
    SingleValueFieldManager sfv = new SingleValueFieldManager();
    op.provideFields(new int[] { fieldNumber }, sfv);
    Object value = sfv.fetchObjectField(fieldNumber);
    if (value != null) {
        AbstractMemberMetaData mmd = op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
        RelationType relationType = mmd.getRelationType(op.getExecutionContext().getClassLoaderResolver());
        if (relationType != RelationType.NONE) {
            if (mmd.hasContainer()) {
                value = processContainer(fieldNumber, value, mmd);
            } else {
                // Process PC fields
                processPersistable(value);
            }
        }
    }
    return value;
}
Also used : RelationType(org.datanucleus.metadata.RelationType) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 97 with AbstractMemberMetaData

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

the class MakeTransientFieldManager method internalFetchObjectField.

/**
 * Method to fetch an object field whether it is SCO collection, PC, or whatever for the fetchplan process.
 * @param fieldNumber Number of the field
 * @return The object
 */
protected Object internalFetchObjectField(int fieldNumber) {
    SingleValueFieldManager sfv = new SingleValueFieldManager();
    op.provideFields(new int[] { fieldNumber }, sfv);
    Object value = sfv.fetchObjectField(fieldNumber);
    if (value != null) {
        AbstractMemberMetaData mmd = op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
        RelationType relType = mmd.getRelationType(op.getExecutionContext().getClassLoaderResolver());
        if (relType != RelationType.NONE) {
            if (mmd.hasContainer()) {
                // Replace with SCO, when possible
                value = SCOUtils.wrapSCOField(op, fieldNumber, value, true);
                TypeManager typeManager = op.getExecutionContext().getTypeManager();
                ContainerAdapter containerAdapter = typeManager.getContainerAdapter(value);
                ApiAdapter api = op.getExecutionContext().getApiAdapter();
                // Process all elements of the Container that are PC
                for (Object object : containerAdapter) {
                    if (api.isPersistable(object)) {
                        processPersistable(object);
                    }
                }
            } else {
                processPersistable(value);
            }
        }
        if (value instanceof SCO) {
            ((SCO) value).unsetOwner();
        }
    }
    return value;
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) RelationType(org.datanucleus.metadata.RelationType) TypeManager(org.datanucleus.store.types.TypeManager) ContainerAdapter(org.datanucleus.store.types.ContainerAdapter) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) SCO(org.datanucleus.store.types.SCO)

Example 98 with AbstractMemberMetaData

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

the class NullifyRelationFieldManager method fetchObjectField.

/**
 * Accessor for object field.
 * @param fieldNumber Number of field
 * @return Object value
 */
public Object fetchObjectField(int fieldNumber) {
    Object value = op.provideField(fieldNumber);
    if (value != null) {
        AbstractMemberMetaData mmd = op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
        RelationType relType = mmd.getRelationType(op.getExecutionContext().getClassLoaderResolver());
        // do not need to nullify fields that are not references and resides embedded in this object
        if (relType != RelationType.NONE) {
            if (mmd.hasContainer()) {
                TypeManager typeManager = op.getExecutionContext().getTypeManager();
                ContainerAdapter containerAdapter = typeManager.getContainerAdapter(value);
                containerAdapter.clear();
                return containerAdapter.getContainer();
            }
            // Process PC fields
            op.makeDirty(fieldNumber);
            return null;
        }
    }
    return value;
}
Also used : RelationType(org.datanucleus.metadata.RelationType) TypeManager(org.datanucleus.store.types.TypeManager) ContainerAdapter(org.datanucleus.store.types.ContainerAdapter) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 99 with AbstractMemberMetaData

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

the class AbstractStoreManager method getValueGenerationStrategyForNative.

/**
 * Method defining which value-strategy to use when the user specifies "native". This will return as follows
 * <ul>
 * <li>If your field is Numeric-based (or datastore-id with numeric or no jdbc-type) then chooses the
 * first one that is supported of "identity", "sequence", "increment", otherwise exception.</li>
 * <li>Otherwise your field is String-based then chooses "uuid-hex".</li>
 * </ul>
 * If your store plugin requires something else then override this
 * @param cmd Class requiring the strategy
 * @param absFieldNumber Field of the class
 * @return The strategy used when "native" is specified
 */
public String getValueGenerationStrategyForNative(AbstractClassMetaData cmd, int absFieldNumber) {
    if (absFieldNumber >= 0) {
        AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(absFieldNumber);
        Class type = mmd.getType();
        if (String.class.isAssignableFrom(type)) {
            // TODO Do we really want this when we have "uuid"?
            return ValueGenerationStrategy.UUIDHEX.toString();
        } else if (type == Long.class || type == Integer.class || type == Short.class || type == long.class || type == int.class || type == short.class || type == BigInteger.class) {
            if (supportsValueGenerationStrategy(ValueGenerationStrategy.IDENTITY.toString())) {
                return ValueGenerationStrategy.IDENTITY.toString();
            } else if (supportsValueGenerationStrategy(ValueGenerationStrategy.SEQUENCE.toString()) && mmd.getSequence() != null) {
                return ValueGenerationStrategy.SEQUENCE.toString();
            } else if (supportsValueGenerationStrategy(ValueGenerationStrategy.INCREMENT.toString())) {
                return ValueGenerationStrategy.INCREMENT.toString();
            }
            throw new NucleusUserException("This datastore provider doesn't support numeric native strategy for member " + mmd.getFullFieldName());
        } else {
            throw new NucleusUserException("This datastore provider doesn't support native strategy for field of type " + type.getName());
        }
    }
    IdentityMetaData idmd = cmd.getBaseIdentityMetaData();
    if (idmd != null && idmd.getColumnMetaData() != null) {
        if (MetaDataUtils.isJdbcTypeString(idmd.getColumnMetaData().getJdbcType())) {
            return ValueGenerationStrategy.UUIDHEX.toString();
        }
    }
    // Numeric datastore-identity
    if (supportsValueGenerationStrategy(ValueGenerationStrategy.IDENTITY.toString())) {
        return ValueGenerationStrategy.IDENTITY.toString();
    } else if (supportsValueGenerationStrategy(ValueGenerationStrategy.SEQUENCE.toString()) && idmd != null && idmd.getSequence() != null) {
        return ValueGenerationStrategy.SEQUENCE.toString();
    } else if (supportsValueGenerationStrategy(ValueGenerationStrategy.INCREMENT.toString())) {
        return ValueGenerationStrategy.INCREMENT.toString();
    }
    throw new NucleusUserException("This datastore provider doesn't support numeric native strategy for class " + cmd.getFullClassName());
}
Also used : BigInteger(java.math.BigInteger) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) IdentityMetaData(org.datanucleus.metadata.IdentityMetaData)

Example 100 with AbstractMemberMetaData

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

the class AbstractStoreManager method getValueGeneratorForMember.

protected synchronized ValueGenerator getValueGeneratorForMember(ClassLoaderResolver clr, AbstractClassMetaData cmd, int absoluteFieldNumber) {
    String memberKey = valueGenerationMgr.getMemberKey(cmd, absoluteFieldNumber);
    // Check if we have a ValueGenerator already created for this member
    ValueGenerator generator = valueGenerationMgr.getValueGeneratorForMemberKey(memberKey);
    if (generator != null) {
        // Return the ValueGenerator already registered against this member "key"
        return generator;
    }
    // No ValueGenerator registered for this memberKey, so need to determine which to use and create it as required.
    String fieldName = null;
    ValueGenerationStrategy strategy = null;
    String sequence = null;
    String valueGeneratorName = null;
    if (absoluteFieldNumber >= 0) {
        // real field
        AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(absoluteFieldNumber);
        fieldName = mmd.getFullFieldName();
        strategy = mmd.getValueStrategy();
        sequence = mmd.getSequence();
        valueGeneratorName = mmd.getValueGeneratorName();
    } else {
        // datastore-identity surrogate field
        fieldName = cmd.getFullClassName() + " (datastore id)";
        strategy = cmd.getIdentityMetaData().getValueStrategy();
        sequence = cmd.getIdentityMetaData().getSequence();
        valueGeneratorName = cmd.getIdentityMetaData().getValueGeneratorName();
    }
    String strategyName = strategy.toString();
    if (strategy.equals(ValueGenerationStrategy.CUSTOM)) {
        // Using a "custom" generator
        strategyName = strategy.getCustomName();
    } else if (strategy.equals(ValueGenerationStrategy.NATIVE)) {
        strategyName = getValueGenerationStrategyForNative(cmd, absoluteFieldNumber);
        strategy = ValueGenerationStrategy.getIdentityStrategy(strategyName);
    }
    // Check for this strategy being a "unique" ValueGenerator, and created if not yet present
    generator = valueGenerationMgr.getUniqueValueGeneratorByName(strategyName);
    if (generator != null) {
        // "unique" ValueGenerator already defined for this strategy, so register it against the member
        valueGenerationMgr.registerValueGeneratorForMemberKey(memberKey, generator);
        return generator;
    }
    // Must be "datastore" specific generator so use plugin mechanism to create one and register against this member "key"
    // Set up the default properties available for all value generators
    // Extract any metadata-based generation information keyed by the "valueGeneratorName"
    TableGeneratorMetaData tableGeneratorMetaData = null;
    SequenceMetaData sequenceMetaData = null;
    if (valueGeneratorName != null) {
        if (strategy == ValueGenerationStrategy.INCREMENT) {
            tableGeneratorMetaData = getMetaDataManager().getMetaDataForTableGenerator(clr, valueGeneratorName);
            if (tableGeneratorMetaData == null) {
                throw new NucleusUserException(Localiser.msg("038005", fieldName, valueGeneratorName));
            }
        } else if (strategy == ValueGenerationStrategy.SEQUENCE) {
            sequenceMetaData = getMetaDataManager().getMetaDataForSequence(clr, valueGeneratorName);
            if (sequenceMetaData == null) {
                throw new NucleusUserException(Localiser.msg("038006", fieldName, valueGeneratorName));
            }
        }
    } else if (strategy == ValueGenerationStrategy.SEQUENCE && sequence != null) {
        // TODO Allow for package name of this class prefix for the sequence name
        sequenceMetaData = getMetaDataManager().getMetaDataForSequence(clr, sequence);
        if (sequenceMetaData == null) {
            // No <sequence> defining the datastore sequence name, so fallback to this name directly in the datastore
            NucleusLogger.VALUEGENERATION.info("Member " + fieldName + " has been specified to use sequence '" + sequence + "' but there is no <sequence> specified in the MetaData. Falling back to use a sequence in the datastore with this name directly.");
        }
    }
    Properties props = getPropertiesForValueGenerator(cmd, absoluteFieldNumber, clr, sequenceMetaData, tableGeneratorMetaData);
    return valueGenerationMgr.createAndRegisterValueGenerator(memberKey, strategyName, props);
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ValueGenerator(org.datanucleus.store.valuegenerator.ValueGenerator) ValueGenerationStrategy(org.datanucleus.metadata.ValueGenerationStrategy) TableGeneratorMetaData(org.datanucleus.metadata.TableGeneratorMetaData) Properties(java.util.Properties) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) SequenceMetaData(org.datanucleus.metadata.SequenceMetaData)

Aggregations

AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)267 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)89 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)84 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)82 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)59 ClassMetaData (org.datanucleus.metadata.ClassMetaData)55 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)50 NucleusException (org.datanucleus.exceptions.NucleusException)41 MetaDataManager (org.datanucleus.metadata.MetaDataManager)40 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)38 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)35 RelationType (org.datanucleus.metadata.RelationType)33 NucleusContext (org.datanucleus.NucleusContext)32 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)32 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)29 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)28 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)27 ExecutionContext (org.datanucleus.ExecutionContext)26 ObjectProvider (org.datanucleus.state.ObjectProvider)25 ArrayList (java.util.ArrayList)24