Search in sources :

Example 51 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class AbstractMapStore method validateValueForWriting.

/**
 * Utility to validate a value is ok for writing (present in the datastore).
 * @param ownerOP ObjectProvider for the owner of the map
 * @param value The value to check.
 */
protected void validateValueForWriting(ObjectProvider ownerOP, Object value) {
    // TODO Pass in cascade flag and if value not present then throw exception
    ExecutionContext ec = ownerOP.getExecutionContext();
    validateValueType(ec.getClassLoaderResolver(), value);
    if (!valuesAreEmbedded && !valuesAreSerialised) {
        SCOUtils.validateObjectForWriting(ec, value, null);
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext)

Example 52 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class AbstractMapStore method validateKeyForWriting.

/**
 * Utility to validate a key is ok for writing (present in the datastore).
 * @param ownerOP ObjectProvider for the owner of the map
 * @param key The key to check.
 */
protected void validateKeyForWriting(ObjectProvider ownerOP, Object key) {
    // TODO Pass in cascade flag and if key not present then throw exception
    ExecutionContext ec = ownerOP.getExecutionContext();
    validateKeyType(ec.getClassLoaderResolver(), key);
    if (!keysAreEmbedded && !keysAreSerialised) {
        SCOUtils.validateObjectForWriting(ec, key, null);
    }
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext)

Example 53 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class AbstractSetStore method remove.

/**
 * Removes the association to one element
 * @param op ObjectProvider for the container
 * @param element Element to remove
 * @param size Current size
 * @param allowDependentField Whether to allow any cascade deletes caused by this removal
 * @return Whether it was successful
 */
public boolean remove(ObjectProvider op, Object element, int size, boolean allowDependentField) {
    if (!validateElementForReading(op, element)) {
        NucleusLogger.DATASTORE.debug("Attempt to remove element=" + StringUtils.toJVMIDString(element) + " but doesn't exist in this Set.");
        return false;
    }
    Object elementToRemove = element;
    ExecutionContext ec = op.getExecutionContext();
    if (ec.getApiAdapter().isDetached(element)) {
        // Element passed in is detached so find attached version (DON'T attach this object)
        elementToRemove = ec.findObject(ec.getApiAdapter().getIdForObject(element), true, false, element.getClass().getName());
    }
    // Remove the element
    boolean modified = false;
    String removeStmt = getRemoveStmt(elementToRemove);
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeStmt, false);
            try {
                int jdbcPosition = 1;
                jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, elementToRemove, jdbcPosition, elementMapping);
                if (relationDiscriminatorMapping != null) {
                    jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                }
                int[] rowsDeleted = sqlControl.executeStatementUpdate(ec, mconn, removeStmt, ps, true);
                modified = (rowsDeleted[0] == 1);
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        String msg = Localiser.msg("056012", removeStmt);
        NucleusLogger.DATASTORE.error(msg, e);
        throw new NucleusDataStoreException(msg, e);
    }
    CollectionMetaData collmd = ownerMemberMetaData.getCollection();
    boolean dependent = collmd.isDependentElement();
    if (ownerMemberMetaData.isCascadeRemoveOrphans()) {
        dependent = true;
    }
    if (allowDependentField && dependent && !collmd.isEmbeddedElement()) {
        // Delete the element if it is dependent
        op.getExecutionContext().deleteObjectInternal(elementToRemove);
    }
    return modified;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) SQLException(java.sql.SQLException) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) PreparedStatement(java.sql.PreparedStatement) CollectionMetaData(org.datanucleus.metadata.CollectionMetaData) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 54 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class BaseContainerStore method getObjectProviderForEmbeddedPCObject.

/**
 * Method to return the ObjectProvider for an embedded PC object (element, key, value).
 * It creates one if the element is not currently managed.
 * @param op ObjectProvider of the owner
 * @param obj The embedded PC object
 * @param ownerMmd The meta data for the owner field
 * @param pcType Object type for the embedded object (see ObjectProvider EMBEDDED_PC etc)
 * @return The ObjectProvider
 */
public ObjectProvider getObjectProviderForEmbeddedPCObject(ObjectProvider op, Object obj, AbstractMemberMetaData ownerMmd, short pcType) {
    ExecutionContext ec = op.getExecutionContext();
    ObjectProvider objOP = ec.findObjectProvider(obj);
    if (objOP == null) {
        objOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, obj, false, op, ownerMmd.getAbsoluteFieldNumber());
    }
    objOP.setPcObjectType(pcType);
    return objOP;
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 55 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class ElementContainerStore method getSize.

public int getSize(ObjectProvider ownerOP) {
    int numRows;
    String sizeStmt = getSizeStmt();
    try {
        ExecutionContext ec = ownerOP.getExecutionContext();
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, sizeStmt);
            try {
                int jdbcPosition = 1;
                if (elementInfo == null) {
                    jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
                } else {
                    if (usingJoinTable()) {
                        jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
                        if (elementInfo[0].getDiscriminatorMapping() != null) {
                            jdbcPosition = BackingStoreHelper.populateElementDiscriminatorInStatement(ec, ps, jdbcPosition, true, elementInfo[0], clr);
                        }
                        if (relationDiscriminatorMapping != null) {
                            jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                        }
                    } else {
                        for (int i = 0; i < elementInfo.length; i++) {
                            jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
                            if (elementInfo[i].getDiscriminatorMapping() != null) {
                                jdbcPosition = BackingStoreHelper.populateElementDiscriminatorInStatement(ec, ps, jdbcPosition, true, elementInfo[i], clr);
                            }
                            if (relationDiscriminatorMapping != null) {
                                jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                            }
                        }
                    }
                }
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, sizeStmt, ps);
                try {
                    if (!rs.next()) {
                        throw new NucleusDataStoreException(Localiser.msg("056007", sizeStmt));
                    }
                    numRows = rs.getInt(1);
                    if (elementInfo != null && elementInfo.length > 1) {
                        while (rs.next()) {
                            numRows = numRows + rs.getInt(1);
                        }
                    }
                    JDBCUtils.logWarnings(rs);
                } finally {
                    rs.close();
                }
            } catch (SQLException sqle) {
                NucleusLogger.GENERAL.error("Exception in size", sqle);
                throw sqle;
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056007", sizeStmt), e);
    }
    return numRows;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Aggregations

ExecutionContext (org.datanucleus.ExecutionContext)178 ObjectProvider (org.datanucleus.state.ObjectProvider)85 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)73 SQLException (java.sql.SQLException)66 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)64 SQLController (org.datanucleus.store.rdbms.SQLController)63 PreparedStatement (java.sql.PreparedStatement)62 Iterator (java.util.Iterator)56 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)27 ResultSet (java.sql.ResultSet)26 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)26 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)25 Map (java.util.Map)23 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)20 Collection (java.util.Collection)18 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)18 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)17 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)16 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)16 ArrayList (java.util.ArrayList)15