Search in sources :

Example 36 with StatementMappingIndex

use of org.datanucleus.store.rdbms.query.StatementMappingIndex in project datanucleus-rdbms by datanucleus.

the class ParameterSetter method storeObjectField.

public void storeObjectField(int fieldNumber, Object value) {
    StatementMappingIndex mapIdx = stmtMappings.getMappingForMemberPosition(fieldNumber);
    if (value == null && mapIdx.getMapping().getMemberMetaData().getNullValue() == NullValue.EXCEPTION) {
        throw new NucleusUserException(Localiser.msg("052400", mapIdx.getMapping().getMemberMetaData().getFullFieldName()));
    }
    try {
        JavaTypeMapping mapping = mapIdx.getMapping();
        boolean provideOwner = false;
        if (mapping instanceof EmbeddedPCMapping || mapping instanceof SerialisedPCMapping || mapping instanceof SerialisedReferenceMapping || mapping instanceof PersistableMapping || mapping instanceof InterfaceMapping) {
            // Pass in the owner ObjectProvider/field for any mappings that have relations
            provideOwner = true;
        }
        if (mapIdx.getNumberOfParameterOccurrences() > 0) {
            for (int i = 0; i < mapIdx.getNumberOfParameterOccurrences(); i++) {
                // Set this value for all occurrences of this parameter
                if (provideOwner) {
                    mapping.setObject(ec, statement, mapIdx.getParameterPositionsForOccurrence(i), value, op, fieldNumber);
                } else {
                    mapping.setObject(ec, statement, mapIdx.getParameterPositionsForOccurrence(i), value);
                }
            }
        } else {
            // Important : call setObject even if the paramIndices is null (reachability)
            if (provideOwner) {
                mapping.setObject(ec, statement, null, value, op, fieldNumber);
            } else {
                mapping.setObject(ec, statement, null, value);
            }
        }
        AbstractMemberMetaData mmd = op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
        RelationType relationType = mmd.getRelationType(ec.getClassLoaderResolver());
        if (op.getClassMetaData().getSCOMutableMemberFlags()[fieldNumber]) {
            SCOUtils.wrapSCOField(op, fieldNumber, value, true);
        } else if (RelationType.isRelationSingleValued(relationType) && (mmd.getEmbeddedMetaData() != null && mmd.getEmbeddedMetaData().getOwnerMember() != null)) {
            // Embedded PC, so make sure the field is wrapped where appropriate TODO This should be part of ManagedRelationships
            op.updateOwnerFieldInEmbeddedField(fieldNumber, value);
        }
    } catch (NotYetFlushedException e) {
        if (op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getNullValue() == NullValue.EXCEPTION) {
            throw e;
        }
        op.updateFieldAfterInsert(e.getPersistable(), fieldNumber);
    }
}
Also used : InterfaceMapping(org.datanucleus.store.rdbms.mapping.java.InterfaceMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) EmbeddedPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) NotYetFlushedException(org.datanucleus.exceptions.NotYetFlushedException) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) RelationType(org.datanucleus.metadata.RelationType) SerialisedPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 37 with StatementMappingIndex

use of org.datanucleus.store.rdbms.query.StatementMappingIndex in project datanucleus-rdbms by datanucleus.

the class ResultSetGetter method fetchObjectField.

public Object fetchObjectField(int fieldNumber) {
    StatementMappingIndex mapIdx = resultMappings.getMappingForMemberPosition(fieldNumber);
    JavaTypeMapping mapping = mapIdx.getMapping();
    AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
    RelationType relationType = mmd.getRelationType(ec.getClassLoaderResolver());
    Object value;
    if (mapping instanceof EmbeddedPCMapping || mapping instanceof SerialisedPCMapping || mapping instanceof SerialisedReferenceMapping) {
        value = mapping.getObject(ec, rs, mapIdx.getColumnPositions(), op, fieldNumber);
    } else {
        if (mmd.isSingleCollection()) {
            StatementClassMapping relationMappings = resultMappings.getMappingDefinitionForMemberPosition(fieldNumber);
            if (relationMappings != null) {
                Class type = ec.getClassLoaderResolver().classForName(mmd.getCollection().getElementType());
                value = processSubObjectFields(mapping, type, relationMappings);
                ElementContainerHandler containerHandler = ec.getTypeManager().getContainerHandler(mmd.getType());
                value = containerHandler.newContainer(mmd, value);
            } else {
                value = mapping.getObject(ec, rs, mapIdx.getColumnPositions());
            }
        } else if (RelationType.isRelationSingleValued(relationType)) {
            // Process fields of sub-object if available in this result set
            StatementClassMapping relationMappings = resultMappings.getMappingDefinitionForMemberPosition(fieldNumber);
            if (relationMappings != null) {
                value = processSubObjectFields(mapping, mmd.getType(), relationMappings);
            } else {
                value = mapping.getObject(ec, rs, mapIdx.getColumnPositions());
            }
        } else {
            value = mapping.getObject(ec, rs, mapIdx.getColumnPositions());
        }
    }
    if (op != null) {
        if (cmd.getSCOMutableMemberFlags()[fieldNumber]) {
            // Wrap any SCO mutable fields
            return SCOUtils.wrapSCOField(op, fieldNumber, value, false);
        } else if (RelationType.isRelationSingleValued(relationType) && (mmd.getEmbeddedMetaData() != null && mmd.getEmbeddedMetaData().getOwnerMember() != null)) {
            // Embedded PC, so make sure the field is wrapped where appropriate TODO This should be part of ManagedRelationships
            op.updateOwnerFieldInEmbeddedField(fieldNumber, value);
            return value;
        }
    }
    return value;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) EmbeddedPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping) RelationType(org.datanucleus.metadata.RelationType) SerialisedPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ElementContainerHandler(org.datanucleus.store.types.ElementContainerHandler) StatementClassMapping(org.datanucleus.store.rdbms.query.StatementClassMapping)

Aggregations

StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)37 ExecutionContext (org.datanucleus.ExecutionContext)25 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)24 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)21 PreparedStatement (java.sql.PreparedStatement)19 SQLException (java.sql.SQLException)19 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)19 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)19 SQLController (org.datanucleus.store.rdbms.SQLController)19 ResultSet (java.sql.ResultSet)16 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)14 ObjectProvider (org.datanucleus.state.ObjectProvider)14 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)14 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)11 ParameterSetter (org.datanucleus.store.rdbms.fieldmanager.ParameterSetter)11 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)11 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)10 PersistentClassROF (org.datanucleus.store.rdbms.query.PersistentClassROF)10 ResultObjectFactory (org.datanucleus.store.rdbms.query.ResultObjectFactory)10 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)9