Search in sources :

Example 26 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class JoinListStore method removeAll.

/**
 * Remove all elements from a collection from the association owner vs
 * elements. Performs the removal in 3 steps. The first gets the indices
 * that will be removed (and the highest index present). The second step
 * removes these elements from the list. The third step updates the indices
 * of the remaining indices to fill the holes created.
 * @param op ObjectProvider
 * @param elements Collection of elements to remove
 * @return Whether the database was updated
 */
public boolean removeAll(ObjectProvider op, Collection elements, int size) {
    if (elements == null || elements.size() == 0) {
        return false;
    }
    // Get the current size of the list (and hence maximum index size)
    int currentListSize = size(op);
    // Get the indices of the elements we are going to remove (highest first)
    int[] indices = getIndicesOf(op, elements);
    if (indices == null) {
        return false;
    }
    boolean modified = false;
    SQLController sqlControl = storeMgr.getSQLController();
    ExecutionContext ec = op.getExecutionContext();
    // Remove the specified elements from the join table
    String removeAllStmt = getRemoveAllStmt(elements);
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        try {
            PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeAllStmt, false);
            try {
                int jdbcPosition = 1;
                Iterator iter = elements.iterator();
                while (iter.hasNext()) {
                    Object element = iter.next();
                    jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                    jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, element, jdbcPosition, elementMapping);
                    if (relationDiscriminatorMapping != null) {
                        jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                    }
                }
                int[] number = sqlControl.executeStatementUpdate(ec, mconn, removeAllStmt, ps, true);
                if (number[0] > 0) {
                    modified = true;
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        NucleusLogger.DATASTORE.error(e);
        throw new NucleusDataStoreException(Localiser.msg("056012", removeAllStmt), e);
    }
    // Shift the remaining indices to remove the holes in ordering
    try {
        boolean batched = storeMgr.allowsBatching();
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        try {
            for (int i = 0; i < currentListSize; i++) {
                // Find the number of deleted indexes above this index
                int shift = 0;
                boolean removed = false;
                for (int j = 0; j < indices.length; j++) {
                    if (indices[j] == i) {
                        removed = true;
                        break;
                    }
                    if (indices[j] < i) {
                        shift++;
                    }
                }
                if (!removed && shift > 0) {
                    internalShift(op, mconn, batched, i, -1 * shift, (i == currentListSize - 1));
                }
            }
        } finally {
            mconn.release();
        }
    } catch (MappedDatastoreException e) {
        NucleusLogger.DATASTORE.error(e);
        throw new NucleusDataStoreException(Localiser.msg("056012", removeAllStmt), e);
    }
    // Dependent field
    boolean dependent = getOwnerMemberMetaData().getCollection().isDependentElement();
    if (getOwnerMemberMetaData().isCascadeRemoveOrphans()) {
        dependent = true;
    }
    if (dependent) {
        // "delete-dependent" : delete elements if the collection is marked as dependent
        // TODO What if the collection contains elements that are not in the List ? should not delete them
        op.getExecutionContext().deleteObjects(elements.toArray());
    }
    return modified;
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 27 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class JoinListStore method internalRemove.

/**
 * Convenience method to remove the specified element from the List.
 * @param element The element
 * @param ownerOP ObjectProvider of the owner
 * @param size Current size of list if known. -1 if not known
 * @return Whether the List was modified
 */
protected boolean internalRemove(ObjectProvider ownerOP, Object element, int size) {
    boolean modified = false;
    if (indexedList) {
        // Indexed List, so retrieve the index of the element and remove the object
        // Get the indices of the elements to remove in reverse order (highest first)
        // This is done because the element could be duplicated in the list.
        Collection elements = new ArrayList();
        elements.add(element);
        int[] indices = getIndicesOf(ownerOP, elements);
        if (indices == null) {
            return false;
        }
        // TODO : Change this to remove all in one go and then shift once
        for (int i = 0; i < indices.length; i++) {
            internalRemoveAt(ownerOP, indices[i], size);
            modified = true;
        }
    } else {
        // Ordered List - just remove the list item since no indexing present
        ExecutionContext ec = ownerOP.getExecutionContext();
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        try {
            int[] rcs = internalRemove(ownerOP, mconn, false, element, true);
            if (rcs != null) {
                if (rcs[0] > 0) {
                    modified = true;
                }
            }
        } catch (MappedDatastoreException sqe) {
            String msg = Localiser.msg("056012", sqe.getMessage());
            NucleusLogger.DATASTORE.error(msg, sqe.getCause());
            throw new NucleusDataStoreException(msg, sqe, ownerOP.getObject());
        } finally {
            mconn.release();
        }
    }
    return modified;
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 28 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class JoinListStore method listIterator.

/**
 * Accessor for an iterator through the list elements.
 * @param ownerOP ObjectProvider for the owner
 * @param startIdx The start point in the list (only for indexed lists).
 * @param endIdx End index in the list (only for indexed lists).
 * @return The List Iterator
 */
protected ListIterator<E> listIterator(ObjectProvider ownerOP, int startIdx, int endIdx) {
    ExecutionContext ec = ownerOP.getExecutionContext();
    Transaction tx = ec.getTransaction();
    // Generate the statement. Note that this is not cached since depends on the current FetchPlan and other things
    IteratorStatement iterStmt = getIteratorStatement(ownerOP.getExecutionContext(), ec.getFetchPlan(), true, startIdx, endIdx);
    SelectStatement sqlStmt = iterStmt.getSelectStatement();
    StatementClassMapping resultMapping = iterStmt.getStatementClassMapping();
    // Input parameter(s) - the owner
    int inputParamNum = 1;
    StatementMappingIndex ownerIdx = new StatementMappingIndex(ownerMapping);
    if (sqlStmt.getNumberOfUnions() > 0) {
        // Add parameter occurrence for each union of statement
        for (int j = 0; j < sqlStmt.getNumberOfUnions() + 1; j++) {
            int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
            for (int k = 0; k < paramPositions.length; k++) {
                paramPositions[k] = inputParamNum++;
            }
            ownerIdx.addParameterOccurrence(paramPositions);
        }
    } else {
        int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
        for (int k = 0; k < paramPositions.length; k++) {
            paramPositions[k] = inputParamNum++;
        }
        ownerIdx.addParameterOccurrence(paramPositions);
    }
    if (tx.getSerializeRead() != null && tx.getSerializeRead()) {
        sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
    }
    String stmt = sqlStmt.getSQLText().toSQL();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            // Create the statement
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
            // Set the owner
            ObjectProvider stmtOwnerOP = BackingStoreHelper.getOwnerObjectProviderForBackingStore(ownerOP);
            int numParams = ownerIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), stmtOwnerOP.getObject());
            }
            try {
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
                try {
                    if (elementsAreEmbedded || elementsAreSerialised) {
                        // No ResultObjectFactory needed - handled by SetStoreIterator
                        return new ListStoreIterator(ownerOP, rs, null, this);
                    } else if (elementMapping instanceof ReferenceMapping) {
                        // No ResultObjectFactory needed - handled by SetStoreIterator
                        return new ListStoreIterator(ownerOP, rs, null, this);
                    } else {
                        ResultObjectFactory rof = new PersistentClassROF(ec, rs, false, resultMapping, elementCmd, clr.classForName(elementType));
                        return new ListStoreIterator(ownerOP, rs, rof, this);
                    }
                } finally {
                    rs.close();
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException | MappedDatastoreException e) {
        throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) SQLException(java.sql.SQLException) ResultObjectFactory(org.datanucleus.store.rdbms.query.ResultObjectFactory) PreparedStatement(java.sql.PreparedStatement) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) StatementClassMapping(org.datanucleus.store.rdbms.query.StatementClassMapping) SQLController(org.datanucleus.store.rdbms.SQLController) SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) Transaction(org.datanucleus.Transaction) PersistentClassROF(org.datanucleus.store.rdbms.query.PersistentClassROF) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 29 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class JoinListStore method set.

/**
 * Method to set an object in the List.
 * @param op ObjectProvider for the owner
 * @param index The item index
 * @param element What to set it to.
 * @param allowDependentField Whether to allow dependent field deletes
 * @return The value before setting.
 */
public E set(ObjectProvider op, int index, Object element, boolean allowDependentField) {
    ExecutionContext ec = op.getExecutionContext();
    validateElementForWriting(ec, element, null);
    // Find the original element at this position
    E oldElement = null;
    List fieldVal = (List) op.provideField(ownerMemberMetaData.getAbsoluteFieldNumber());
    if (fieldVal != null && fieldVal instanceof BackedSCO && ((BackedSCO) fieldVal).isLoaded()) {
        // Already loaded in the wrapper
        oldElement = (E) fieldVal.get(index);
    } else {
        oldElement = get(op, index);
    }
    // Check for dynamic schema updates prior to update
    if (storeMgr.getBooleanObjectProperty(RDBMSPropertyNames.PROPERTY_RDBMS_DYNAMIC_SCHEMA_UPDATES).booleanValue()) {
        DynamicSchemaFieldManager dynamicSchemaFM = new DynamicSchemaFieldManager(storeMgr, op);
        Collection coll = new ArrayList();
        coll.add(element);
        dynamicSchemaFM.storeObjectField(getOwnerMemberMetaData().getAbsoluteFieldNumber(), coll);
        if (dynamicSchemaFM.hasPerformedSchemaUpdates()) {
            setStmt = null;
        }
    }
    String theSetStmt = getSetStmt();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, theSetStmt, false);
            try {
                int jdbcPosition = 1;
                jdbcPosition = BackingStoreHelper.populateElementInStatement(ec, ps, element, jdbcPosition, elementMapping);
                jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                if (getOwnerMemberMetaData().getOrderMetaData() != null && !getOwnerMemberMetaData().getOrderMetaData().isIndexedList()) {
                    // Ordered list, so can't easily do a set!!!
                    NucleusLogger.PERSISTENCE.warn("Calling List.addElement at a position for an ordered list is a stupid thing to do; the ordering is set my the ordering specification. Use an indexed list to do this correctly");
                } else {
                    jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, index, jdbcPosition, orderMapping);
                }
                if (relationDiscriminatorMapping != null) {
                    jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                }
                sqlControl.executeStatementUpdate(ec, mconn, theSetStmt, ps, true);
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056015", theSetStmt), e);
    }
    // Dependent field
    CollectionMetaData collmd = ownerMemberMetaData.getCollection();
    boolean dependent = collmd.isDependentElement();
    if (ownerMemberMetaData.isCascadeRemoveOrphans()) {
        dependent = true;
    }
    if (dependent && !collmd.isEmbeddedElement() && allowDependentField) {
        if (oldElement != null && !contains(op, oldElement)) {
            // Delete the element if it is dependent and doesn't have a duplicate entry in the list
            ec.deleteObjectInternal(oldElement);
        }
    }
    return oldElement;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) CollectionMetaData(org.datanucleus.metadata.CollectionMetaData) BackedSCO(org.datanucleus.store.types.wrappers.backed.BackedSCO) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) DynamicSchemaFieldManager(org.datanucleus.store.rdbms.fieldmanager.DynamicSchemaFieldManager) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 30 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class JoinListStore method internalAdd.

/**
 * Internal method to add element(s) to the List.
 * Performs the add in 2 steps.
 * <ol>
 * <li>Shift all existing elements into their new positions so we can insert.</li>
 * <li>Insert all new elements directly at their desired positions</li>
 * </ol>
 * Both steps can be batched (separately).
 * @param op The ObjectProvider
 * @param start The start location (if required)
 * @param atEnd Whether to add the element at the end
 * @param c The collection of objects to add.
 * @param size Current size of list if known. -1 if not known
 * @return Whether it was successful
 */
protected boolean internalAdd(ObjectProvider op, int start, boolean atEnd, Collection<E> c, int size) {
    if (c == null || c.size() == 0) {
        return true;
    }
    if (relationType == RelationType.MANY_TO_MANY_BI && ownerMemberMetaData.getMappedBy() != null) {
        // M-N non-owner : don't add from this side to avoid duplicates
        return true;
    }
    // Calculate the amount we need to shift any existing elements by
    // This is used where inserting between existing elements and have to shift down all elements after the start point
    int shift = c.size();
    // check all elements are valid for persisting and exist (persistence-by-reachability)
    ExecutionContext ec = op.getExecutionContext();
    Iterator iter = c.iterator();
    while (iter.hasNext()) {
        Object element = iter.next();
        validateElementForWriting(ec, element, null);
        if (relationType == RelationType.ONE_TO_MANY_BI) {
            // TODO This is ManagedRelations - move into RelationshipManager
            ObjectProvider elementOP = ec.findObjectProvider(element);
            if (elementOP != null) {
                AbstractMemberMetaData[] relatedMmds = ownerMemberMetaData.getRelatedMemberMetaData(clr);
                // TODO Cater for more than 1 related field
                Object elementOwner = elementOP.provideField(relatedMmds[0].getAbsoluteFieldNumber());
                if (elementOwner == null) {
                    // No owner, so correct it
                    NucleusLogger.PERSISTENCE.info(Localiser.msg("056037", op.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject())));
                    elementOP.replaceField(relatedMmds[0].getAbsoluteFieldNumber(), op.getObject());
                } else if (elementOwner != op.getObject() && op.getReferencedPC() == null) {
                    // Inconsistent owner, so throw exception
                    throw new NucleusUserException(Localiser.msg("056038", op.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject()), StringUtils.toJVMIDString(elementOwner)));
                }
            }
        }
    }
    // Check what we have persistent already
    int currentListSize = 0;
    if (size < 0) {
        // Get the current size from the datastore
        currentListSize = size(op);
    } else {
        currentListSize = size;
    }
    // Check for dynamic schema updates prior to addition
    if (storeMgr.getBooleanObjectProperty(RDBMSPropertyNames.PROPERTY_RDBMS_DYNAMIC_SCHEMA_UPDATES).booleanValue()) {
        DynamicSchemaFieldManager dynamicSchemaFM = new DynamicSchemaFieldManager(storeMgr, op);
        dynamicSchemaFM.storeObjectField(getOwnerMemberMetaData().getAbsoluteFieldNumber(), c);
        if (dynamicSchemaFM.hasPerformedSchemaUpdates()) {
            invalidateAddStmt();
        }
    }
    String addStmt = getAddStmtForJoinTable();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            // Shift any existing elements so that we can insert the new element(s) at their position
            if (!atEnd && start != currentListSize) {
                boolean batched = currentListSize - start > 0;
                for (int i = currentListSize - 1; i >= start; i--) {
                    // Shift the index for this row by "shift"
                    internalShift(op, mconn, batched, i, shift, (i == start));
                }
            } else {
                start = currentListSize;
            }
            // Insert the elements at their required location
            int jdbcPosition = 1;
            boolean batched = (c.size() > 1);
            Iterator elemIter = c.iterator();
            while (elemIter.hasNext()) {
                Object element = elemIter.next();
                PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, addStmt, batched);
                try {
                    jdbcPosition = 1;
                    jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                    jdbcPosition = BackingStoreHelper.populateElementInStatement(ec, ps, element, jdbcPosition, elementMapping);
                    if (orderMapping != null) {
                        jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, start, jdbcPosition, orderMapping);
                    }
                    if (relationDiscriminatorMapping != null) {
                        jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                    }
                    start++;
                    // Execute the statement
                    sqlControl.executeStatementUpdate(ec, mconn, addStmt, ps, !iter.hasNext());
                } finally {
                    sqlControl.closeStatement(mconn, ps);
                }
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException | MappedDatastoreException e) {
        throw new NucleusDataStoreException(Localiser.msg("056009", addStmt), e);
    }
    return true;
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) SQLException(java.sql.SQLException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) DynamicSchemaFieldManager(org.datanucleus.store.rdbms.fieldmanager.DynamicSchemaFieldManager) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ObjectProvider(org.datanucleus.state.ObjectProvider) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Aggregations

NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)288 SQLException (java.sql.SQLException)185 ExecutionContext (org.datanucleus.ExecutionContext)139 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)131 PreparedStatement (java.sql.PreparedStatement)128 SQLController (org.datanucleus.store.rdbms.SQLController)126 ResultSet (java.sql.ResultSet)73 Iterator (java.util.Iterator)43 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)38 CollectionAddOperation (org.datanucleus.flush.CollectionAddOperation)34 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)33 ArrayList (java.util.ArrayList)32 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)30 Collection (java.util.Collection)27 CollectionRemoveOperation (org.datanucleus.flush.CollectionRemoveOperation)26 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)24 List (java.util.List)23 ObjectProvider (org.datanucleus.state.ObjectProvider)22 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)22 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)21