Search in sources :

Example 1 with FieldValues

use of org.datanucleus.store.FieldValues in project datanucleus-rdbms by datanucleus.

the class MappingHelper method getObjectForApplicationIdentity.

/**
 * Get the object instance for a class using application identity
 * @param ec ExecutionContext
 * @param mapping The mapping in which this is returned
 * @param rs the ResultSet
 * @param resultIndexes indexes in the result set to retrieve
 * @param cmd the AbstractClassMetaData
 * @return the id
 */
public static Object getObjectForApplicationIdentity(final ExecutionContext ec, JavaTypeMapping mapping, final ResultSet rs, int[] resultIndexes, AbstractClassMetaData cmd) {
    ClassLoaderResolver clr = ec.getClassLoaderResolver();
    // Abstract class
    if (((ClassMetaData) cmd).isAbstract() && cmd.getObjectidClass() != null) {
        return getObjectForAbstractClass(ec, mapping, rs, resultIndexes, cmd);
    }
    int totalFieldCount = cmd.getNoOfManagedMembers() + cmd.getNoOfInheritedManagedMembers();
    final StatementMappingIndex[] statementExpressionIndex = new StatementMappingIndex[totalFieldCount];
    int paramIndex = 0;
    DatastoreClass datastoreClass = mapping.getStoreManager().getDatastoreClass(cmd.getFullClassName(), clr);
    final int[] pkFieldNumbers = cmd.getPKMemberPositions();
    for (int i = 0; i < pkFieldNumbers.length; ++i) {
        AbstractMemberMetaData fmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNumbers[i]);
        JavaTypeMapping m = datastoreClass.getMemberMapping(fmd);
        statementExpressionIndex[fmd.getAbsoluteFieldNumber()] = new StatementMappingIndex(m);
        int[] expressionsIndex = new int[m.getNumberOfDatastoreMappings()];
        for (int j = 0; j < expressionsIndex.length; j++) {
            expressionsIndex[j] = resultIndexes[paramIndex++];
        }
        statementExpressionIndex[fmd.getAbsoluteFieldNumber()].setColumnPositions(expressionsIndex);
    }
    final StatementClassMapping resultMappings = new StatementClassMapping();
    for (int i = 0; i < pkFieldNumbers.length; i++) {
        resultMappings.addMappingForMember(pkFieldNumbers[i], statementExpressionIndex[pkFieldNumbers[i]]);
    }
    // TODO Use any other (non-PK) param values
    final FieldManager resultsFM = new ResultSetGetter(ec, rs, resultMappings, cmd);
    Object id = IdentityUtils.getApplicationIdentityForResultSetRow(ec, cmd, null, false, resultsFM);
    Class type = ec.getClassLoaderResolver().classForName(cmd.getFullClassName());
    return ec.findObject(id, new FieldValues() {

        public void fetchFields(ObjectProvider sm) {
            sm.replaceFields(pkFieldNumbers, resultsFM);
        }

        public void fetchNonLoadedFields(ObjectProvider sm) {
            sm.replaceNonLoadedFields(pkFieldNumbers, resultsFM);
        }

        public FetchPlan getFetchPlanForLoading() {
            return ec.getFetchPlan();
        }
    }, type, false, true);
}
Also used : FieldManager(org.datanucleus.store.fieldmanager.FieldManager) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) FetchPlan(org.datanucleus.FetchPlan) StatementClassMapping(org.datanucleus.store.rdbms.query.StatementClassMapping) ResultSetGetter(org.datanucleus.store.rdbms.fieldmanager.ResultSetGetter) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) ObjectProvider(org.datanucleus.state.ObjectProvider) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) FieldValues(org.datanucleus.store.FieldValues) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 2 with FieldValues

use of org.datanucleus.store.FieldValues in project datanucleus-rdbms by datanucleus.

the class FKListStore method validateElementForWriting.

/**
 * Method to validate that an element is valid for writing to the datastore.
 * TODO Minimise differences to super.validateElementForWriting()
 * @param op ObjectProvider for the List
 * @param element The element to validate
 * @param index The position that the element is being stored at in the list
 * @return Whether the element was inserted
 */
protected boolean validateElementForWriting(final ObjectProvider op, final Object element, final int index) {
    final Object newOwner = op.getObject();
    ComponentInfo info = getComponentInfoForElement(element);
    final DatastoreClass elementTable;
    if (storeMgr.getNucleusContext().getMetaDataManager().isPersistentInterface(elementType)) {
        elementTable = storeMgr.getDatastoreClass(storeMgr.getNucleusContext().getMetaDataManager().getImplementationNameForPersistentInterface(elementType), clr);
    } else {
        elementTable = storeMgr.getDatastoreClass(element.getClass().getName(), clr);
    }
    final JavaTypeMapping orderMapping;
    if (info != null) {
        orderMapping = info.getDatastoreClass().getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_INDEX);
    } else {
        orderMapping = this.orderMapping;
    }
    // Check if element is ok for use in the datastore, specifying any external mappings that may be required
    boolean inserted = super.validateElementForWriting(op.getExecutionContext(), element, new FieldValues() {

        public void fetchFields(ObjectProvider elemOP) {
            // Find the (element) table storing the FK back to the owner
            if (elementTable != null) {
                JavaTypeMapping externalFKMapping = elementTable.getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_FK);
                if (externalFKMapping != null) {
                    // The element has an external FK mapping so set the value it needs to use in the INSERT
                    elemOP.setAssociatedValue(externalFKMapping, op.getObject());
                }
                if (relationDiscriminatorMapping != null) {
                    elemOP.setAssociatedValue(relationDiscriminatorMapping, relationDiscriminatorValue);
                }
                if (orderMapping != null && index >= 0) {
                    if (ownerMemberMetaData.getOrderMetaData() != null && ownerMemberMetaData.getOrderMetaData().getMappedBy() != null) {
                        // Order is stored in a field in the element so update it
                        // We support mapped-by fields of types int/long/Integer/Long currently
                        Object indexValue = null;
                        if (orderMapping.getMemberMetaData().getTypeName().equals(ClassNameConstants.JAVA_LANG_LONG) || orderMapping.getMemberMetaData().getTypeName().equals(ClassNameConstants.LONG)) {
                            indexValue = Long.valueOf(index);
                        } else {
                            indexValue = Integer.valueOf(index);
                        }
                        elemOP.replaceFieldMakeDirty(orderMapping.getMemberMetaData().getAbsoluteFieldNumber(), indexValue);
                    } else {
                        // Order is stored in a surrogate column so save its vaue for the element to use later
                        elemOP.setAssociatedValue(orderMapping, Integer.valueOf(index));
                    }
                }
            }
            if (ownerMemberMetaData.getMappedBy() != null) {
                // TODO This is ManagedRelations - move into RelationshipManager
                // Managed Relations : 1-N bidir, so make sure owner is correct at persist
                // TODO Support DOT notation in mappedBy
                ObjectProvider ownerHolderOP = elemOP;
                int ownerFieldNumberInHolder = -1;
                if (ownerMemberMetaData.getMappedBy().indexOf('.') > 0) {
                    AbstractMemberMetaData otherMmd = null;
                    AbstractClassMetaData otherCmd = info.getAbstractClassMetaData();
                    String remainingMappedBy = ownerMemberMetaData.getMappedBy();
                    while (remainingMappedBy.indexOf('.') > 0) {
                        int dotPosition = remainingMappedBy.indexOf('.');
                        String thisMappedBy = remainingMappedBy.substring(0, dotPosition);
                        otherMmd = otherCmd.getMetaDataForMember(thisMappedBy);
                        Object holderValueAtField = ownerHolderOP.provideField(otherMmd.getAbsoluteFieldNumber());
                        ownerHolderOP = op.getExecutionContext().findObjectProviderForEmbedded(holderValueAtField, ownerHolderOP, otherMmd);
                        remainingMappedBy = remainingMappedBy.substring(dotPosition + 1);
                        otherCmd = storeMgr.getMetaDataManager().getMetaDataForClass(otherMmd.getTypeName(), clr);
                        if (remainingMappedBy.indexOf('.') < 0) {
                            otherMmd = otherCmd.getMetaDataForMember(remainingMappedBy);
                            ownerFieldNumberInHolder = otherMmd.getAbsoluteFieldNumber();
                        }
                    }
                } else {
                    ownerFieldNumberInHolder = info.getAbstractClassMetaData().getAbsolutePositionOfMember(ownerMemberMetaData.getMappedBy());
                }
                Object currentOwner = ownerHolderOP.provideField(ownerFieldNumberInHolder);
                if (currentOwner == null) {
                    // No owner, so correct it
                    NucleusLogger.PERSISTENCE.info(Localiser.msg("056037", op.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(ownerHolderOP.getObject())));
                    ownerHolderOP.replaceFieldMakeDirty(ownerFieldNumberInHolder, newOwner);
                } else if (currentOwner != newOwner && op.getReferencedPC() == null) {
                    // Inconsistent owner, so throw exception
                    throw new NucleusUserException(Localiser.msg("056038", op.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(ownerHolderOP.getObject()), StringUtils.toJVMIDString(currentOwner)));
                }
            }
        }

        public void fetchNonLoadedFields(ObjectProvider op) {
        }

        public FetchPlan getFetchPlanForLoading() {
            return null;
        }
    });
    return inserted;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ObjectProvider(org.datanucleus.state.ObjectProvider) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) FieldValues(org.datanucleus.store.FieldValues) FetchPlan(org.datanucleus.FetchPlan) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 3 with FieldValues

use of org.datanucleus.store.FieldValues in project datanucleus-rdbms by datanucleus.

the class ResultMetaDataROF method getObject.

/**
 * Accessor for the object(s) from the current row of the ResultSet.
 * @return The object(s) for this row of the ResultSet.
 */
public Object getObject() {
    List returnObjects = new ArrayList();
    // A). Process persistent types
    PersistentTypeMapping[] persistentTypes = queryResultMetaData.getPersistentTypeMappings();
    if (persistentTypes != null) {
        if (persistentTypeResultSetGetters == null) {
            persistentTypeResultSetGetters = new ResultSetGetter[persistentTypes.length];
        }
        int startColumnIndex = 0;
        for (int i = 0; i < persistentTypes.length; i++) {
            Set<String> columnsInThisType = new HashSet<>();
            AbstractMemberMetaData[] mmds = new AbstractMemberMetaData[columnNames.length];
            Map<String, AbstractMemberMetaData> fieldColumns = new HashMap<>();
            DatastoreClass dc = ((RDBMSStoreManager) ec.getStoreManager()).getDatastoreClass(persistentTypes[i].getClassName(), ec.getClassLoaderResolver());
            AbstractClassMetaData acmd = ec.getMetaDataManager().getMetaDataForClass(persistentTypes[i].getClassName(), ec.getClassLoaderResolver());
            Object id = null;
            // and two columns with similar names e.g "Col1" and "col1". Until that situation comes up we ignore it :-)
            for (int j = startColumnIndex; j < columnNames.length; j++) {
                if (columnsInThisType.contains(columnNames[j])) {
                    // already added this column, so must be another persistent type
                    startColumnIndex = j;
                    break;
                }
                boolean found = false;
                if (acmd.getIdentityType() == IdentityType.DATASTORE) {
                    JavaTypeMapping datastoreIdMapping = dc.getSurrogateMapping(SurrogateColumnType.DATASTORE_ID, false);
                    Column df = datastoreIdMapping.getDatastoreMapping(0).getColumn();
                    if (df.getIdentifier().getName().equalsIgnoreCase(columnNames[j])) {
                        // add +1 because result sets in jdbc starts with 1
                        int datastoreIdentityExpressionIndex = j + 1;
                        id = datastoreIdMapping.getObject(ec, rs, new int[] { datastoreIdentityExpressionIndex });
                        found = true;
                    }
                }
                for (int k = 0; k < acmd.getNoOfManagedMembers() + acmd.getNoOfInheritedManagedMembers() && !found; k++) {
                    AbstractMemberMetaData apmd = acmd.getMetaDataForManagedMemberAtAbsolutePosition(k);
                    if (persistentTypes[i].getColumnForField(apmd.getName()) != null) {
                        if (persistentTypes[i].getColumnForField(apmd.getName()).equalsIgnoreCase(columnNames[j])) {
                            fieldColumns.put(columnNames[j], apmd);
                            columnsInThisType.add(columnNames[j]);
                            mmds[j] = apmd;
                            found = true;
                        }
                    } else {
                        JavaTypeMapping mapping = dc.getMemberMapping(apmd);
                        for (int l = 0; l < mapping.getDatastoreMappings().length && !found; l++) {
                            Column df = mapping.getDatastoreMapping(l).getColumn();
                            if (df.getIdentifier().getName().equalsIgnoreCase(columnNames[j])) {
                                fieldColumns.put(columnNames[j], apmd);
                                columnsInThisType.add(columnNames[j]);
                                mmds[j] = apmd;
                                found = true;
                            }
                        }
                    }
                }
                if (!columnsInThisType.contains(columnNames[j])) {
                    // column not found in this type, so must be another persistent type
                    startColumnIndex = j;
                    break;
                }
            }
            // Build fields and mappings in the results
            StatementMappingIndex[] stmtMappings = new StatementMappingIndex[acmd.getNoOfManagedMembers() + acmd.getNoOfInheritedManagedMembers()];
            Set<AbstractMemberMetaData> resultMmds = new HashSet<>();
            resultMmds.addAll(fieldColumns.values());
            int[] resultFieldNumbers = new int[resultMmds.size()];
            int j = 0;
            for (AbstractMemberMetaData apmd : resultMmds) {
                StatementMappingIndex stmtMapping = new StatementMappingIndex(dc.getMemberMapping(apmd));
                resultFieldNumbers[j] = apmd.getAbsoluteFieldNumber();
                List indexes = new ArrayList();
                for (int k = 0; k < mmds.length; k++) {
                    if (mmds[k] == apmd) {
                        indexes.add(Integer.valueOf(k));
                    }
                }
                int[] indxs = new int[indexes.size()];
                for (int k = 0; k < indxs.length; k++) {
                    // add +1 because result sets in JDBC starts with 1
                    indxs[k] = ((Integer) indexes.get(k)).intValue() + 1;
                }
                stmtMapping.setColumnPositions(indxs);
                stmtMappings[resultFieldNumbers[j]] = stmtMapping;
                j++;
            }
            Object obj = null;
            Class type = ec.getClassLoaderResolver().classForName(persistentTypes[i].getClassName());
            if (acmd.getIdentityType() == IdentityType.APPLICATION) {
                if (persistentTypeResultSetGetters[i] == null) {
                    final StatementClassMapping resultMappings = new StatementClassMapping();
                    for (int k = 0; k < resultFieldNumbers.length; k++) {
                        resultMappings.addMappingForMember(resultFieldNumbers[k], stmtMappings[resultFieldNumbers[k]]);
                    }
                    persistentTypeResultSetGetters[i] = new ResultSetGetter(ec, rs, resultMappings, acmd);
                }
                ResultSetGetter rsGetter = persistentTypeResultSetGetters[i];
                // TODO Make use of discriminator like in PersistentClassROF and set the pcClass in this?
                id = IdentityUtils.getApplicationIdentityForResultSetRow(ec, acmd, type, false, rsGetter);
                obj = ec.findObject(id, new FieldValues() {

                    public void fetchFields(ObjectProvider op) {
                        rsGetter.setObjectProvider(op);
                        op.replaceFields(resultFieldNumbers, rsGetter, false);
                    }

                    public void fetchNonLoadedFields(ObjectProvider op) {
                        rsGetter.setObjectProvider(op);
                        op.replaceNonLoadedFields(resultFieldNumbers, rsGetter);
                    }

                    public FetchPlan getFetchPlanForLoading() {
                        return null;
                    }
                }, type, ignoreCache, false);
            } else if (acmd.getIdentityType() == IdentityType.DATASTORE) {
                if (persistentTypeResultSetGetters[i] == null) {
                    final StatementClassMapping resultMappings = new StatementClassMapping();
                    for (int k = 0; k < resultFieldNumbers.length; k++) {
                        resultMappings.addMappingForMember(resultFieldNumbers[k], stmtMappings[resultFieldNumbers[k]]);
                    }
                    persistentTypeResultSetGetters[i] = new ResultSetGetter(ec, rs, resultMappings, acmd);
                }
                ResultSetGetter rsGetter = persistentTypeResultSetGetters[i];
                obj = ec.findObject(id, new FieldValues() {

                    public void fetchFields(ObjectProvider op) {
                        rsGetter.setObjectProvider(op);
                        op.replaceFields(resultFieldNumbers, rsGetter, false);
                    }

                    public void fetchNonLoadedFields(ObjectProvider op) {
                        rsGetter.setObjectProvider(op);
                        op.replaceNonLoadedFields(resultFieldNumbers, rsGetter);
                    }

                    public FetchPlan getFetchPlanForLoading() {
                        return null;
                    }
                }, type, ignoreCache, false);
            } else {
                // TODO Handle non-durable
                NucleusLogger.QUERY.warn("We do not currently support non-durable objects in the results of this type of query.");
            }
            returnObjects.add(obj);
        }
    }
    // B). Process simple columns
    String[] columns = queryResultMetaData.getScalarColumns();
    if (columns != null) {
        for (int i = 0; i < columns.length; i++) {
            try {
                Object obj = rs.getObject(columns[i]);
                returnObjects.add(obj);
            } catch (SQLException sqe) {
                String msg = Localiser.msg("059027", sqe.getMessage());
                NucleusLogger.QUERY.error(msg);
                throw new NucleusUserException(msg, sqe);
            }
        }
    }
    // C). Process constructor type mappings
    ConstructorTypeMapping[] ctrTypeMappings = queryResultMetaData.getConstructorTypeMappings();
    if (ctrTypeMappings != null) {
        for (int i = 0; i < ctrTypeMappings.length; i++) {
            String ctrClassName = ctrTypeMappings[i].getClassName();
            Class ctrCls = ec.getClassLoaderResolver().classForName(ctrClassName);
            List<ConstructorTypeColumn> ctrColumns = ctrTypeMappings[i].getColumnsForConstructor();
            Class[] ctrArgTypes = null;
            Object[] ctrArgVals = null;
            if (ctrColumns != null && ctrColumns.size() > 0) {
                int j = 0;
                ctrArgTypes = new Class[ctrColumns.size()];
                ctrArgVals = new Object[ctrColumns.size()];
                Iterator<ConstructorTypeColumn> colIter = ctrColumns.iterator();
                while (colIter.hasNext()) {
                    ConstructorTypeColumn ctrCol = colIter.next();
                    try {
                        Object colVal = rs.getObject(ctrCol.getColumnName());
                        ctrArgTypes[j] = colVal.getClass();
                        if (ctrCol.getJavaType() != null) {
                            // Attempt to convert to the type requested
                            ctrArgTypes[j] = ctrCol.getJavaType();
                            ctrArgVals[j] = TypeConversionHelper.convertTo(colVal, ctrArgTypes[j]);
                        } else {
                            ctrArgTypes[j] = colVal.getClass();
                            ctrArgVals[j] = colVal;
                        }
                    } catch (SQLException sqle) {
                    // TODO Handle this
                    }
                    j++;
                }
            }
            returnObjects.add(ClassUtils.newInstance(ctrCls, ctrArgTypes, ctrArgVals));
        }
    }
    if (returnObjects.size() == 0) {
        // No objects so user must have supplied incorrect MetaData
        return null;
    } else if (returnObjects.size() == 1) {
        // Return Object
        return returnObjects.get(0);
    } else {
        // Return Object[]
        return returnObjects.toArray(new Object[returnObjects.size()]);
    }
}
Also used : HashMap(java.util.HashMap) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) FetchPlan(org.datanucleus.FetchPlan) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ResultSetGetter(org.datanucleus.store.rdbms.fieldmanager.ResultSetGetter) ConstructorTypeColumn(org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeColumn) Column(org.datanucleus.store.rdbms.table.Column) ArrayList(java.util.ArrayList) List(java.util.List) PersistentTypeMapping(org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping) HashSet(java.util.HashSet) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) ConstructorTypeColumn(org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeColumn) ConstructorTypeMapping(org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) ObjectProvider(org.datanucleus.state.ObjectProvider) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) FieldValues(org.datanucleus.store.FieldValues) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 4 with FieldValues

use of org.datanucleus.store.FieldValues in project datanucleus-rdbms by datanucleus.

the class FKMapStore method put.

/**
 * Method to put an item in the Map.
 * @param op ObjectProvider for the map.
 * @param newKey The key to store the value against
 * @param newValue The value to store.
 * @return The value stored.
 */
public V put(final ObjectProvider op, final K newKey, V newValue) {
    ExecutionContext ec = op.getExecutionContext();
    if (keyFieldNumber >= 0) {
        validateKeyForWriting(op, newKey);
        validateValueType(ec.getClassLoaderResolver(), newValue);
    } else {
        validateKeyType(ec.getClassLoaderResolver(), newKey);
        validateValueForWriting(op, newValue);
    }
    // Check if there is an existing value for this key
    V oldValue = get(op, newKey);
    if (oldValue != newValue) {
        if (valueCmd != null) {
            if (oldValue != null && !oldValue.equals(newValue)) {
                // Key is stored in the value and the value has changed so remove the old value
                removeValue(op, newKey, oldValue);
            }
            final Object newOwner = op.getObject();
            if (ec.getApiAdapter().isPersistent(newValue)) {
                /*
                     * The new value is already persistent.
                     *
                     * "Put" the new value in the map by updating its owner and key
                     * fields to the appropriate values.  This is done with the same
                     * methods the PC itself would use if the application code
                     * modified the fields.  It should result in no actual database
                     * activity if the fields were already set to the right values.
                     */
                if (ec != ec.getApiAdapter().getExecutionContext(newValue)) {
                    throw new NucleusUserException(Localiser.msg("RDBMS.SCO.Map.WriteValueInvalidWithDifferentPM"), ec.getApiAdapter().getIdForObject(newValue));
                }
                ObjectProvider vsm = ec.findObjectProvider(newValue);
                // Ensure the current owner field is loaded, and replace with new value
                if (ownerFieldNumber >= 0) {
                    vsm.isLoaded(ownerFieldNumber);
                    Object oldOwner = vsm.provideField(ownerFieldNumber);
                    vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
                    if (ec.getManageRelations()) {
                        ec.getRelationshipManager(vsm).relationChange(ownerFieldNumber, oldOwner, newOwner);
                    }
                } else {
                    updateValueFk(op, newValue, newOwner);
                }
                // Ensure the current key field is loaded, and replace with new value
                vsm.isLoaded(keyFieldNumber);
                Object oldKey = vsm.provideField(keyFieldNumber);
                vsm.replaceFieldMakeDirty(keyFieldNumber, newKey);
                if (ec.getManageRelations()) {
                    ec.getRelationshipManager(vsm).relationChange(keyFieldNumber, oldKey, newKey);
                }
            } else {
                /*
                     * The new value is not yet persistent.
                     *
                     * Update its owner and key fields to the appropriate values and
                     * *then* make it persistent.  Making the changes before DB
                     * insertion avoids an unnecessary UPDATE allows the owner
                     * and/or key fields to be non-nullable.
                     */
                ec.persistObjectInternal(newValue, new FieldValues() {

                    public void fetchFields(ObjectProvider vsm) {
                        if (ownerFieldNumber >= 0) {
                            vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
                        }
                        vsm.replaceFieldMakeDirty(keyFieldNumber, newKey);
                        JavaTypeMapping externalFKMapping = valueTable.getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_FK);
                        if (externalFKMapping != null) {
                            // Set the owner in the value object where appropriate
                            vsm.setAssociatedValue(externalFKMapping, op.getObject());
                        }
                    }

                    public void fetchNonLoadedFields(ObjectProvider op) {
                    }

                    public FetchPlan getFetchPlanForLoading() {
                        return null;
                    }
                }, ObjectProvider.PC);
            }
        } else {
            // Value is stored in the key
            final Object newOwner = op.getObject();
            if (ec.getApiAdapter().isPersistent(newKey)) {
                /*
                     * The new key is already persistent.
                     *
                     * "Put" the new key in the map by updating its owner and value
                     * fields to the appropriate values. This is done with the same
                     * methods the PC itself would use if the application code
                     * modified the fields. It should result in no actual database
                     * activity if the fields were already set to the right values.
                     */
                if (ec != ec.getApiAdapter().getExecutionContext(newKey)) {
                    throw new NucleusUserException(Localiser.msg("056060"), ec.getApiAdapter().getIdForObject(newKey));
                }
                ObjectProvider valOP = ec.findObjectProvider(newKey);
                // Ensure the current owner field is loaded, and replace with new key
                if (ownerFieldNumber >= 0) {
                    valOP.isLoaded(ownerFieldNumber);
                    Object oldOwner = valOP.provideField(ownerFieldNumber);
                    valOP.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
                    if (ec.getManageRelations()) {
                        ec.getRelationshipManager(valOP).relationChange(ownerFieldNumber, oldOwner, newOwner);
                    }
                } else {
                    updateKeyFk(op, newKey, newOwner);
                }
                // Ensure the current value field is loaded, and replace with new value
                valOP.isLoaded(valueFieldNumber);
                // TODO Should we update the local variable ?
                oldValue = (V) valOP.provideField(valueFieldNumber);
                valOP.replaceFieldMakeDirty(valueFieldNumber, newValue);
                if (ec.getManageRelations()) {
                    ec.getRelationshipManager(valOP).relationChange(valueFieldNumber, oldValue, newValue);
                }
            } else {
                /*
                     * The new key is not yet persistent.
                     *
                     * Update its owner and key fields to the appropriate values and
                     * *then* make it persistent.  Making the changes before DB
                     * insertion avoids an unnecessary UPDATE allows the owner
                     * and/or key fields to be non-nullable.
                     */
                final Object newValueObj = newValue;
                ec.persistObjectInternal(newKey, new FieldValues() {

                    public void fetchFields(ObjectProvider vsm) {
                        if (ownerFieldNumber >= 0) {
                            vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
                        }
                        vsm.replaceFieldMakeDirty(valueFieldNumber, newValueObj);
                        JavaTypeMapping externalFKMapping = valueTable.getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_FK);
                        if (externalFKMapping != null) {
                            // Set the owner in the value object where appropriate
                            vsm.setAssociatedValue(externalFKMapping, op.getObject());
                        }
                    }

                    public void fetchNonLoadedFields(ObjectProvider op) {
                    }

                    public FetchPlan getFetchPlanForLoading() {
                        return null;
                    }
                }, ObjectProvider.PC);
            /*if (ownerFieldNumber < 0)
                    {
                        // TODO Think about removing this since we set the associated owner here
                        updateKeyFk(sm, newKey, newOwner);
                    }*/
            }
        }
    }
    // TODO Cater for key being PC and having delete-dependent
    if (ownerMemberMetaData.getMap().isDependentValue() && oldValue != null) {
        // Delete the old value if it is no longer contained and is dependent
        if (!containsValue(op, oldValue)) {
            ec.deleteObjectInternal(oldValue);
        }
    }
    return oldValue;
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ObjectProvider(org.datanucleus.state.ObjectProvider) FieldValues(org.datanucleus.store.FieldValues) FetchPlan(org.datanucleus.FetchPlan)

Example 5 with FieldValues

use of org.datanucleus.store.FieldValues in project datanucleus-rdbms by datanucleus.

the class FKSetStore method add.

/**
 * Method to add an object to the relationship at the collection end.
 * @param ownerOP ObjectProvider for the owner.
 * @param element Element to be added
 * @return Success indicator
 */
public boolean add(final ObjectProvider ownerOP, E element, int size) {
    if (element == null) {
        // Sets allow no duplicates
        throw new NucleusUserException(Localiser.msg("056039"));
    }
    // Make sure that the element is persisted in the datastore (reachability)
    final Object newOwner = ownerOP.getObject();
    final ExecutionContext ec = ownerOP.getExecutionContext();
    // Find the (element) table storing the FK back to the owner
    boolean isPersistentInterface = storeMgr.getNucleusContext().getMetaDataManager().isPersistentInterface(elementType);
    DatastoreClass elementTable = null;
    if (isPersistentInterface) {
        elementTable = storeMgr.getDatastoreClass(storeMgr.getNucleusContext().getMetaDataManager().getImplementationNameForPersistentInterface(elementType), clr);
    } else {
        Class elementTypeCls = clr.classForName(elementType);
        if (elementTypeCls.isInterface()) {
            // Set<interface> so use type of element passed in and get its table
            elementTable = storeMgr.getDatastoreClass(element.getClass().getName(), clr);
        } else {
            // Use table for element type
            elementTable = storeMgr.getDatastoreClass(elementType, clr);
        }
    }
    if (elementTable == null) {
        // "subclass-table", persisted into table of other class
        AbstractClassMetaData[] managingCmds = storeMgr.getClassesManagingTableForClass(elementCmd, clr);
        if (managingCmds != null && managingCmds.length > 0) {
            // Find which of these subclasses is appropriate for this element
            for (int i = 0; i < managingCmds.length; i++) {
                Class tblCls = clr.classForName(managingCmds[i].getFullClassName());
                if (tblCls.isAssignableFrom(element.getClass())) {
                    elementTable = storeMgr.getDatastoreClass(managingCmds[i].getFullClassName(), clr);
                    break;
                }
            }
        }
    }
    final DatastoreClass elementTbl = elementTable;
    boolean inserted = validateElementForWriting(ec, element, new FieldValues() {

        public void fetchFields(ObjectProvider elementOP) {
            if (elementTbl != null) {
                JavaTypeMapping externalFKMapping = elementTbl.getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_FK);
                if (externalFKMapping != null) {
                    // The element has an external FK mapping so set the value it needs to use in the INSERT
                    elementOP.setAssociatedValue(externalFKMapping, ownerOP.getObject());
                }
                if (relationDiscriminatorMapping != null) {
                    // Element type has a shared FK so set the discriminator value for this relation
                    elementOP.setAssociatedValue(relationDiscriminatorMapping, relationDiscriminatorValue);
                }
            }
            int fieldNumInElement = getFieldNumberInElementForBidirectional(elementOP);
            if (fieldNumInElement >= 0) {
                // TODO Move this into RelationshipManager
                // Managed Relations : 1-N bidir, so make sure owner is correct at persist
                Object currentOwner = elementOP.provideField(fieldNumInElement);
                if (currentOwner == null) {
                    // No owner, so correct it
                    NucleusLogger.PERSISTENCE.info(Localiser.msg("056037", ownerOP.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject())));
                    elementOP.replaceFieldMakeDirty(fieldNumInElement, newOwner);
                } else if (currentOwner != newOwner) {
                    // Check for owner change
                    Object ownerId1 = ec.getApiAdapter().getIdForObject(currentOwner);
                    Object ownerId2 = ec.getApiAdapter().getIdForObject(newOwner);
                    if (ownerId1 != null && ownerId2 != null && ownerId1.equals(ownerId2)) {
                        // Must be attaching
                        if (!ec.getApiAdapter().isDetached(newOwner)) {
                            // Attaching, so make sure we set to the attached owner
                            elementOP.replaceField(fieldNumInElement, newOwner);
                        }
                    } else if (ownerOP.getReferencedPC() == null) {
                        // Not being attached so must be inconsistent owner, so throw exception
                        throw new NucleusUserException(Localiser.msg("056038", ownerOP.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject()), StringUtils.toJVMIDString(currentOwner)));
                    }
                }
            }
        }

        public void fetchNonLoadedFields(ObjectProvider op) {
        }

        public FetchPlan getFetchPlanForLoading() {
            return null;
        }
    });
    if (inserted) {
        // Element has just been persisted so the FK will be set
        return true;
    }
    // Element was already persistent so make sure the FK is in place
    // TODO This is really "ManagedRelationships" so needs to go in RelationshipManager
    ObjectProvider elementOP = ec.findObjectProvider(element);
    if (elementOP == null) {
        // Element is likely being attached and this is the detached element; lookup the attached element via the id
        Object elementId = ec.getApiAdapter().getIdForObject(element);
        if (elementId != null) {
            element = (E) ec.findObject(elementId, false, false, element.getClass().getName());
            if (element != null) {
                elementOP = ec.findObjectProvider(element);
            }
        }
    }
    int fieldNumInElement = getFieldNumberInElementForBidirectional(elementOP);
    if (fieldNumInElement >= 0 && elementOP != null) {
        // Managed Relations : 1-N bidir, so update the owner of the element
        // Ensure is loaded
        elementOP.isLoaded(fieldNumInElement);
        Object oldOwner = elementOP.provideField(fieldNumInElement);
        if (oldOwner != newOwner) {
            if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
                NucleusLogger.PERSISTENCE.debug(Localiser.msg("055009", ownerOP.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(element)));
            }
            elementOP.replaceFieldMakeDirty(fieldNumInElement, newOwner);
            if (ec.getManageRelations()) {
                // Managed Relationships - add the change we've made here to be analysed at flush
                RelationshipManager relationshipManager = ec.getRelationshipManager(elementOP);
                relationshipManager.relationChange(fieldNumInElement, oldOwner, newOwner);
                if (ec.isFlushing()) {
                    // When already flushing process the changes right away to make them effective during the current flush
                    relationshipManager.process();
                }
            }
            if (ec.isFlushing()) {
                elementOP.flush();
            }
        }
        return oldOwner != newOwner;
    }
    // 1-N unidir so update the FK if not set to be contained in the set
    boolean contained = contains(ownerOP, element);
    return (contained ? false : updateElementFk(ownerOP, element, newOwner));
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) FetchPlan(org.datanucleus.FetchPlan) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ExecutionContext(org.datanucleus.ExecutionContext) RelationshipManager(org.datanucleus.state.RelationshipManager) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) ObjectProvider(org.datanucleus.state.ObjectProvider) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) FieldValues(org.datanucleus.store.FieldValues)

Aggregations

FetchPlan (org.datanucleus.FetchPlan)5 ObjectProvider (org.datanucleus.state.ObjectProvider)5 FieldValues (org.datanucleus.store.FieldValues)5 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)5 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)4 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)4 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)3 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 ExecutionContext (org.datanucleus.ExecutionContext)2 ResultSetGetter (org.datanucleus.store.rdbms.fieldmanager.ResultSetGetter)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 ConstructorTypeColumn (org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeColumn)1 ConstructorTypeMapping (org.datanucleus.metadata.QueryResultMetaData.ConstructorTypeMapping)1 PersistentTypeMapping (org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping)1 RelationshipManager (org.datanucleus.state.RelationshipManager)1