Search in sources :

Example 1 with EmbeddedElementPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.

the class BackingStoreHelper method populateEmbeddedElementFieldsInStatement.

/**
 * Convenience method to populate the passed PreparedStatement with the field values from
 * the embedded element starting at the specified jdbc position.
 * @param op ObjectProvider of the owning container
 * @param element The embedded element
 * @param ps The PreparedStatement
 * @param jdbcPosition JDBC position in the statement to start at
 * @param ownerFieldMetaData The meta data for the owner field
 * @param elementMapping mapping for the element
 * @param emd Metadata for the element class
 * @param bcs Container store
 * @return The next JDBC position
 */
public static int populateEmbeddedElementFieldsInStatement(ObjectProvider op, Object element, PreparedStatement ps, int jdbcPosition, AbstractMemberMetaData ownerFieldMetaData, JavaTypeMapping elementMapping, AbstractClassMetaData emd, BaseContainerStore bcs) {
    EmbeddedElementPCMapping embeddedMapping = (EmbeddedElementPCMapping) elementMapping;
    StatementClassMapping mappingDefinition = new StatementClassMapping();
    int[] elementFieldNumbers = new int[embeddedMapping.getNumberOfJavaTypeMappings()];
    for (int i = 0; i < embeddedMapping.getNumberOfJavaTypeMappings(); i++) {
        JavaTypeMapping fieldMapping = embeddedMapping.getJavaTypeMapping(i);
        int absFieldNum = emd.getAbsolutePositionOfMember(fieldMapping.getMemberMetaData().getName());
        elementFieldNumbers[i] = absFieldNum;
        StatementMappingIndex stmtMapping = new StatementMappingIndex(fieldMapping);
        int[] jdbcParamPositions = new int[fieldMapping.getNumberOfDatastoreMappings()];
        for (int j = 0; j < fieldMapping.getNumberOfDatastoreMappings(); j++) {
            jdbcParamPositions[j] = jdbcPosition++;
        }
        stmtMapping.addParameterOccurrence(jdbcParamPositions);
        mappingDefinition.addMappingForMember(absFieldNum, stmtMapping);
    }
    ObjectProvider elementOP = bcs.getObjectProviderForEmbeddedPCObject(op, element, ownerFieldMetaData, ObjectProvider.EMBEDDED_COLLECTION_ELEMENT_PC);
    elementOP.provideFields(elementFieldNumbers, new ParameterSetter(elementOP, ps, mappingDefinition));
    return jdbcPosition;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ObjectProvider(org.datanucleus.state.ObjectProvider) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) ParameterSetter(org.datanucleus.store.rdbms.fieldmanager.ParameterSetter) EmbeddedElementPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping) StatementClassMapping(org.datanucleus.store.rdbms.query.StatementClassMapping)

Example 2 with EmbeddedElementPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.

the class AbstractRDBMSQueryResult method registerMemberBulkResultSet.

public void registerMemberBulkResultSet(IteratorStatement iterStmt, ResultSet rs) {
    if (bulkLoadedValueByMemberNumber == null) {
        bulkLoadedValueByMemberNumber = new HashMap<>();
    }
    try {
        ExecutionContext ec = query.getExecutionContext();
        AbstractMemberMetaData mmd = iterStmt.getBackingStore().getOwnerMemberMetaData();
        if (mmd.hasCollection() || mmd.hasArray()) {
            ElementIteratorStatement elemIterStmt = (ElementIteratorStatement) iterStmt;
            ElementContainerStore backingStore = (ElementContainerStore) iterStmt.getBackingStore();
            if (backingStore.isElementsAreEmbedded() || backingStore.isElementsAreSerialised()) {
                int[] param = new int[backingStore.getElementMapping().getNumberOfColumnMappings()];
                for (int i = 0; i < param.length; ++i) {
                    param[i] = i + 1;
                }
                if (backingStore.getElementMapping() instanceof SerialisedPCMapping || backingStore.getElementMapping() instanceof SerialisedReferenceMapping || backingStore.getElementMapping() instanceof EmbeddedElementPCMapping) {
                    // Element = Serialised
                    while (rs.next()) {
                        Object owner = iterStmt.getOwnerMapIndex().getMapping().getObject(ec, rs, iterStmt.getOwnerMapIndex().getColumnPositions());
                        Object element = backingStore.getElementMapping().getObject(ec, rs, param, ec.findStateManager(owner), backingStore.getOwnerMemberMetaData().getAbsoluteFieldNumber());
                        addOwnerMemberCollectionElement(mmd, owner, element);
                    }
                } else {
                    // Element = Non-PC
                    while (rs.next()) {
                        Object owner = iterStmt.getOwnerMapIndex().getMapping().getObject(ec, rs, iterStmt.getOwnerMapIndex().getColumnPositions());
                        Object element = backingStore.getElementMapping().getObject(ec, rs, param);
                        addOwnerMemberCollectionElement(mmd, owner, element);
                    }
                }
            } else if (backingStore.getElementMapping() instanceof ReferenceMapping) {
                // Element is Reference (interface/Object) so just use elementMapping
                int[] param = new int[backingStore.getElementMapping().getNumberOfColumnMappings()];
                for (int i = 0; i < param.length; ++i) {
                    param[i] = i + 1;
                }
                while (rs.next()) {
                    Object owner = iterStmt.getOwnerMapIndex().getMapping().getObject(ec, rs, iterStmt.getOwnerMapIndex().getColumnPositions());
                    Object element = backingStore.getElementMapping().getObject(ec, rs, param);
                    addOwnerMemberCollectionElement(mmd, owner, element);
                }
            } else {
                String elementType = mmd.hasCollection() ? backingStore.getOwnerMemberMetaData().getCollection().getElementType() : backingStore.getOwnerMemberMetaData().getArray().getElementType();
                ResultObjectFactory<E> scoROF = new PersistentClassROF(ec, rs, query.getIgnoreCache(), fp, elemIterStmt.getElementClassMapping(), backingStore.getElementClassMetaData(), ec.getClassLoaderResolver().classForName(elementType));
                while (rs.next()) {
                    Object owner = iterStmt.getOwnerMapIndex().getMapping().getObject(ec, rs, iterStmt.getOwnerMapIndex().getColumnPositions());
                    Object element = scoROF.getObject();
                    addOwnerMemberCollectionElement(mmd, owner, element);
                }
            }
        } else if (mmd.hasMap()) {
        // TODO Cater for maps
        }
    } catch (SQLException sqle) {
        NucleusLogger.DATASTORE.error("Exception thrown processing bulk loaded field " + iterStmt.getBackingStore().getOwnerMemberMetaData().getFullFieldName(), sqle);
    } finally {
        // Close the ResultSet (and its Statement)
        try {
            Statement stmt = null;
            try {
                stmt = rs.getStatement();
                // Close the result set
                rs.close();
            } catch (SQLException e) {
                NucleusLogger.DATASTORE.error(Localiser.msg("052605", e));
            } finally {
                try {
                    if (stmt != null) {
                        // Close the original statement
                        stmt.close();
                    }
                } catch (SQLException e) {
                // Do nothing
                }
            }
        } finally {
            rs = null;
        }
    }
}
Also used : SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) SQLException(java.sql.SQLException) ElementIteratorStatement(org.datanucleus.store.rdbms.scostore.ElementIteratorStatement) Statement(java.sql.Statement) IteratorStatement(org.datanucleus.store.rdbms.scostore.IteratorStatement) ElementIteratorStatement(org.datanucleus.store.rdbms.scostore.ElementIteratorStatement) ElementContainerStore(org.datanucleus.store.rdbms.scostore.ElementContainerStore) EmbeddedElementPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping) ExecutionContext(org.datanucleus.ExecutionContext) SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) SerialisedPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 3 with EmbeddedElementPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.

the class BackingStoreHelper method populateEmbeddedElementFieldsInStatement.

/**
 * Convenience method to populate the passed PreparedStatement with the field values from
 * the embedded element starting at the specified jdbc position.
 * @param sm StateManager of the owning container
 * @param element The embedded element
 * @param ps The PreparedStatement
 * @param jdbcPosition JDBC position in the statement to start at
 * @param ownerFieldMetaData The meta data for the owner field
 * @param elementMapping mapping for the element
 * @param emd Metadata for the element class
 * @param bcs Container store
 * @return The next JDBC position
 */
public static int populateEmbeddedElementFieldsInStatement(DNStateManager sm, Object element, PreparedStatement ps, int jdbcPosition, AbstractMemberMetaData ownerFieldMetaData, JavaTypeMapping elementMapping, AbstractClassMetaData emd, BaseContainerStore bcs) {
    EmbeddedElementPCMapping embeddedMapping = (EmbeddedElementPCMapping) elementMapping;
    StatementClassMapping mappingDefinition = new StatementClassMapping();
    int[] elementFieldNumbers = new int[embeddedMapping.getNumberOfJavaTypeMappings()];
    for (int i = 0; i < embeddedMapping.getNumberOfJavaTypeMappings(); i++) {
        JavaTypeMapping fieldMapping = embeddedMapping.getJavaTypeMapping(i);
        int absFieldNum = emd.getAbsolutePositionOfMember(fieldMapping.getMemberMetaData().getName());
        elementFieldNumbers[i] = absFieldNum;
        StatementMappingIndex stmtMapping = new StatementMappingIndex(fieldMapping);
        int[] jdbcParamPositions = new int[fieldMapping.getNumberOfColumnMappings()];
        for (int j = 0; j < fieldMapping.getNumberOfColumnMappings(); j++) {
            jdbcParamPositions[j] = jdbcPosition++;
        }
        stmtMapping.addParameterOccurrence(jdbcParamPositions);
        mappingDefinition.addMappingForMember(absFieldNum, stmtMapping);
    }
    DNStateManager elementSM = bcs.getStateManagerForEmbeddedPCObject(sm, element, ownerFieldMetaData, PersistableObjectType.EMBEDDED_COLLECTION_ELEMENT_PC);
    elementSM.provideFields(elementFieldNumbers, new ParameterSetter(elementSM, ps, mappingDefinition));
    return jdbcPosition;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) DNStateManager(org.datanucleus.state.DNStateManager) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) ParameterSetter(org.datanucleus.store.rdbms.fieldmanager.ParameterSetter) EmbeddedElementPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping) StatementClassMapping(org.datanucleus.store.rdbms.query.StatementClassMapping)

Example 4 with EmbeddedElementPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.

the class ElementContainerTable method getExpectedForeignKeys.

/**
 * Accessor for the expected foreign keys for this table.
 * @param clr The ClassLoaderResolver
 * @return The expected foreign keys.
 */
@Override
public List<ForeignKey> getExpectedForeignKeys(ClassLoaderResolver clr) {
    assertIsInitialized();
    // Find the mode that we're operating in for FK addition
    boolean autoMode = false;
    if (storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_RDBMS_CONSTRAINT_CREATE_MODE).equals("DataNucleus")) {
        autoMode = true;
    }
    List<ForeignKey> foreignKeys = new ArrayList();
    try {
        // FK from join table to owner table
        DatastoreClass referencedTable = storeMgr.getDatastoreClass(ownerType, clr);
        if (referencedTable != null) {
            // Single owner table, so add a single FK to the owner as appropriate
            ForeignKey fk = getForeignKeyToOwner(referencedTable, autoMode);
            if (fk != null) {
                foreignKeys.add(fk);
            }
        } else {
        // No single owner so we don't bother with the FK since referential integrity by FK cannot work
        // if we don't have a single owner at the other end of the FK(s).
        }
        // FK from join table to element table(s)
        if (elementMapping instanceof SerialisedPCMapping) {
        // Do nothing since no element table
        } else if (elementMapping instanceof EmbeddedElementPCMapping) {
            // Add any FKs for the fields of the (embedded) element
            EmbeddedElementPCMapping embMapping = (EmbeddedElementPCMapping) elementMapping;
            for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
                JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
                if (ClassUtils.isReferenceType(embFmd.getType()) && embFieldMapping instanceof ReferenceMapping) {
                    // Field is a reference type, so add a FK to the table of the PC for each PC implementation
                    Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                    foreignKeys.addAll(fks);
                } else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null && embFieldMapping.getNumberOfColumnMappings() > 0 && embFieldMapping instanceof PersistableMapping) {
                    // Field is for a PC class with the FK at this side, so add a FK to the table of this PC
                    ForeignKey fk = TableUtils.getForeignKeyForPCField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                    if (fk != null) {
                        foreignKeys.add(fk);
                    }
                }
            }
        } else if (elementMapping instanceof ReferenceMapping) {
            JavaTypeMapping[] implJavaTypeMappings = ((ReferenceMapping) elementMapping).getJavaTypeMapping();
            for (int i = 0; i < implJavaTypeMappings.length; i++) {
                JavaTypeMapping implMapping = implJavaTypeMappings[i];
                if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(implMapping.getType(), clr) != null && implMapping.getNumberOfColumnMappings() > 0) {
                    referencedTable = storeMgr.getDatastoreClass(implMapping.getType(), clr);
                    if (referencedTable != null) {
                        ForeignKey fk = getForeignKeyToElement(referencedTable, autoMode, implMapping);
                        if (fk != null) {
                            foreignKeys.add(fk);
                        }
                    }
                }
            }
        } else {
            referencedTable = storeMgr.getDatastoreClass(getElementType(), clr);
            if (referencedTable != null) {
                ForeignKey fk = getForeignKeyToElement(referencedTable, autoMode, elementMapping);
                if (fk != null) {
                    foreignKeys.add(fk);
                }
            } else {
            // Either no element table or multiple (where the user has element with "subclass-table" strategy, or using "complete-table")
            // so do nothing since referential integrity will not allow multiple FKs.
            }
        }
    } catch (NoTableManagedException e) {
    // expected when no table exists
    }
    return foreignKeys;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey) EmbeddedElementPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) Collection(java.util.Collection) SerialisedPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException)

Example 5 with EmbeddedElementPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping in project datanucleus-rdbms by datanucleus.

the class AbstractCollectionStore method updateEmbeddedElement.

/**
 * Method to update a field of an embedded element.
 * @param op ObjectProvider of the owner
 * @param element The element to update
 * @param fieldNumber The number of the field to update
 * @param value The value
 * @return true if the datastore was updated
 */
public boolean updateEmbeddedElement(ObjectProvider op, E element, int fieldNumber, Object value) {
    boolean modified = false;
    if (elementMapping != null && elementMapping instanceof EmbeddedElementPCMapping) {
        String fieldName = elementCmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
        if (fieldName == null) {
            // We have no mapping for this field so presumably is the owner field or a PK field
            return false;
        }
        JavaTypeMapping fieldMapping = ((EmbeddedElementPCMapping) elementMapping).getJavaTypeMapping(fieldName);
        if (fieldMapping == null) {
            // We have no mapping for this field so presumably is the owner field or a PK field
            return false;
        }
        modified = updateEmbeddedElement(op, element, fieldNumber, value, fieldMapping);
    }
    return modified;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EmbeddedElementPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping)

Aggregations

EmbeddedElementPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping)9 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)8 SQLException (java.sql.SQLException)2 ExecutionContext (org.datanucleus.ExecutionContext)2 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)2 ParameterSetter (org.datanucleus.store.rdbms.fieldmanager.ParameterSetter)2 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)2 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)2 SerialisedPCMapping (org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping)2 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)2 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)2 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 ElementMetaData (org.datanucleus.metadata.ElementMetaData)1 IndexMetaData (org.datanucleus.metadata.IndexMetaData)1