Search in sources :

Example 1 with MappingCallbacks

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

the class FetchRequest method processMembersOfClass.

/**
 * Method to process the supplied members of the class, adding to the SQLStatement as required.
 * Can recurse if some of the requested fields are persistent objects in their own right, so we
 * take the opportunity to retrieve some of their fields.
 * @param sqlStatement Statement being built
 * @param mmds Meta-data for the required fields/properties
 * @param table The table to look for member mappings
 * @param sqlTbl The table in the SQL statement to use for selects
 * @param mappingDef Mapping definition for the result
 * @param fetchCallbacks Any additional required callbacks are added here
 * @param clr ClassLoader resolver
 * @return Number of fields being fetched
 */
protected int processMembersOfClass(SelectStatement sqlStatement, AbstractMemberMetaData[] mmds, DatastoreClass table, SQLTable sqlTbl, StatementClassMapping mappingDef, Collection fetchCallbacks, ClassLoaderResolver clr) {
    int number = 0;
    if (mmds != null) {
        for (int i = 0; i < mmds.length; i++) {
            // Get the mapping (in this table, or super-table)
            AbstractMemberMetaData mmd = mmds[i];
            JavaTypeMapping mapping = table.getMemberMapping(mmd);
            if (mapping != null) {
                if (!mmd.isPrimaryKey() && mapping.includeInFetchStatement()) {
                    // The depth is the number of levels down to load in this statement.
                    // 0 is to load just this objects fields (as with JPOX, and DataNucleus up to 1.1.3)
                    int depth = 0;
                    AbstractMemberMetaData mmdToUse = mmd;
                    JavaTypeMapping mappingToUse = mapping;
                    if (mapping instanceof SingleCollectionMapping) {
                        // Check the wrapped type
                        mappingToUse = ((SingleCollectionMapping) mapping).getWrappedMapping();
                        mmdToUse = ((SingleCollectionMapping) mapping).getWrappedMapping().getMemberMetaData();
                    }
                    if (mappingToUse instanceof PersistableMapping) {
                        // Special case of 1-1/N-1 where we know the other side type so know what to join to, hence can load the related object
                        depth = 1;
                        if (Modifier.isAbstract(mmdToUse.getType().getModifiers())) {
                            String typeName = mmdToUse.getTypeName();
                            DatastoreClass relTable = table.getStoreManager().getDatastoreClass(typeName, clr);
                            if (relTable != null && relTable.getSurrogateMapping(SurrogateColumnType.DISCRIMINATOR, false) == null) {
                                // 1-1 relation to base class with no discriminator and has subclasses
                                // hence no way of determining the exact type, hence no point in fetching it
                                String[] subclasses = table.getStoreManager().getMetaDataManager().getSubclassesForClass(typeName, false);
                                if (subclasses != null && subclasses.length > 0) {
                                    depth = 0;
                                }
                            }
                        }
                    } else if (mappingToUse instanceof ReferenceMapping) {
                        ReferenceMapping refMapping = (ReferenceMapping) mappingToUse;
                        if (refMapping.getMappingStrategy() == ReferenceMapping.PER_IMPLEMENTATION_MAPPING) {
                            JavaTypeMapping[] subMappings = refMapping.getJavaTypeMapping();
                            if (subMappings != null && subMappings.length == 1) {
                                // Support special case of reference mapping with single implementation possible
                                depth = 1;
                            }
                        }
                    }
                    // TODO We should use the actual FetchPlan, and the max fetch depth, so then it can pull in all related objects within reach.
                    // But this will mean we cannot cache the statement, since it is for a specific ExecutionContext
                    // TODO If this field is a 1-1 and the other side has a discriminator or version then we really ought to fetch it
                    SQLStatementHelper.selectMemberOfSourceInStatement(sqlStatement, mappingDef, null, sqlTbl, mmd, clr, depth, null);
                    number++;
                }
                if (mapping instanceof MappingCallbacks) {
                    // TODO Need to add that this mapping is for base object or base.field1, etc
                    fetchCallbacks.add(mapping);
                }
            }
        }
    }
    JavaTypeMapping versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true);
    if (versionMapping != null) {
        // Select version
        StatementMappingIndex verMapIdx = new StatementMappingIndex(versionMapping);
        SQLTable verSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(sqlStatement, sqlTbl, versionMapping);
        int[] cols = sqlStatement.select(verSqlTbl, versionMapping, null);
        verMapIdx.setColumnPositions(cols);
        mappingDef.addMappingForMember(SurrogateColumnType.VERSION.getFieldNumber(), verMapIdx);
    }
    return number;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) MappingCallbacks(org.datanucleus.store.rdbms.mapping.MappingCallbacks) SingleCollectionMapping(org.datanucleus.store.rdbms.mapping.java.SingleCollectionMapping) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) SQLTable(org.datanucleus.store.rdbms.sql.SQLTable) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 MappingCallbacks (org.datanucleus.store.rdbms.mapping.MappingCallbacks)1 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)1 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)1 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)1 SingleCollectionMapping (org.datanucleus.store.rdbms.mapping.java.SingleCollectionMapping)1 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)1 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)1 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)1