Search in sources :

Example 46 with ExecutionContext

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

the class AbstractArrayStore method internalAdd.

/**
 * Internal method to add a row to the join table.
 * Used by add() and set() to add a row to the join table.
 * @param op ObjectProvider for the owner of the collection
 * @param element The element to add the relation to
 * @param conn The connection
 * @param batched Whether we are batching
 * @param orderId The order id to use for this element relation
 * @param executeNow Whether to execute the statement now (and not wait for any batch)
 * @return Whether a row was inserted
 * @throws MappedDatastoreException Thrown if an error occurs
 */
public int[] internalAdd(ObjectProvider op, E element, ManagedConnection conn, boolean batched, int orderId, boolean executeNow) throws MappedDatastoreException {
    ExecutionContext ec = op.getExecutionContext();
    SQLController sqlControl = storeMgr.getSQLController();
    String addStmt = getAddStmtForJoinTable();
    try {
        PreparedStatement ps = sqlControl.getStatementForUpdate(conn, addStmt, false);
        boolean notYetFlushedError = false;
        try {
            // Insert the join table row
            int jdbcPosition = 1;
            jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
            jdbcPosition = BackingStoreHelper.populateElementInStatement(ec, ps, element, jdbcPosition, elementMapping);
            jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, orderId, jdbcPosition, orderMapping);
            if (relationDiscriminatorMapping != null) {
                jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
            }
            // Execute the statement
            return sqlControl.executeStatementUpdate(ec, conn, addStmt, ps, executeNow);
        } catch (NotYetFlushedException nfe) {
            notYetFlushedError = true;
            throw nfe;
        } finally {
            if (notYetFlushedError) {
                sqlControl.abortStatementForConnection(conn, ps);
            } else {
                sqlControl.closeStatement(conn, ps);
            }
        }
    } catch (SQLException e) {
        throw new MappedDatastoreException(addStmt, e);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) ExecutionContext(org.datanucleus.ExecutionContext) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) NotYetFlushedException(org.datanucleus.exceptions.NotYetFlushedException) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 47 with ExecutionContext

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

the class AbstractCollectionStore method updateEmbeddedElement.

public boolean updateEmbeddedElement(ObjectProvider op, E element, int fieldNumber, Object value, JavaTypeMapping fieldMapping) {
    boolean modified = false;
    String stmt = getUpdateEmbeddedElementStmt(fieldMapping);
    try {
        ExecutionContext ec = op.getExecutionContext();
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
            try {
                int jdbcPosition = 1;
                fieldMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, fieldMapping), value);
                jdbcPosition += fieldMapping.getNumberOfDatastoreMappings();
                jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                jdbcPosition = BackingStoreHelper.populateEmbeddedElementFieldsInStatement(op, element, ps, jdbcPosition, ((JoinTable) containerTable).getOwnerMemberMetaData(), elementMapping, elementCmd, this);
                sqlControl.executeStatementUpdate(ec, mconn, stmt, ps, true);
                modified = true;
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        NucleusLogger.DATASTORE_PERSIST.error("Exception updating embedded element in collection", e);
        // TODO Update this localised message to reflect that it is the update of an embedded element
        throw new NucleusDataStoreException(Localiser.msg("056009", stmt), e);
    }
    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) SQLController(org.datanucleus.store.rdbms.SQLController) JoinTable(org.datanucleus.store.rdbms.table.JoinTable)

Example 48 with ExecutionContext

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

the class AbstractListStore method internalShift.

/**
 * Method to process a "shift" statement, updating the index in the list of the specified index.
 * @param op ObjectProvider
 * @param conn The connection
 * @param batched Whether the statement is batched
 * @param oldIndex The old index
 * @param amount Amount to shift by (negative means shift down)
 * @param executeNow Whether to execute the statement now (or wait for batching)
 * @return Return code(s) from any executed statements
 * @throws MappedDatastoreException Thrown if an error occurs
 */
protected int[] internalShift(ObjectProvider op, ManagedConnection conn, boolean batched, int oldIndex, int amount, boolean executeNow) throws MappedDatastoreException {
    ExecutionContext ec = op.getExecutionContext();
    SQLController sqlControl = storeMgr.getSQLController();
    String shiftStmt = getShiftStmt();
    try {
        PreparedStatement ps = sqlControl.getStatementForUpdate(conn, shiftStmt, batched);
        try {
            int jdbcPosition = 1;
            jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, amount, jdbcPosition, orderMapping);
            jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
            jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, oldIndex, jdbcPosition, orderMapping);
            if (relationDiscriminatorMapping != null) {
                jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
            }
            // Execute the statement
            return sqlControl.executeStatementUpdate(ec, conn, shiftStmt, ps, executeNow);
        } finally {
            sqlControl.closeStatement(conn, ps);
        }
    } catch (SQLException sqle) {
        throw new MappedDatastoreException(shiftStmt, sqle);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) ExecutionContext(org.datanucleus.ExecutionContext) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 49 with ExecutionContext

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

the class AbstractListStore method getIndicesOf.

/**
 * Utility to find the indices of a collection of elements.
 * The returned list are in reverse order (highest index first).
 * @param op ObjectProvider
 * @param elements The elements
 * @return The indices of the elements in the List.
 */
protected int[] getIndicesOf(ObjectProvider op, Collection elements) {
    if (elements == null || elements.isEmpty()) {
        return null;
    }
    Iterator iter = elements.iterator();
    while (iter.hasNext()) {
        validateElementForReading(op, iter.next());
    }
    String stmt = getIndicesOfStmt(elements);
    try {
        ExecutionContext ec = op.getExecutionContext();
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
            try {
                Iterator elemIter = elements.iterator();
                int jdbcPosition = 1;
                while (elemIter.hasNext()) {
                    Object element = elemIter.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);
                    }
                }
                List<Integer> indexes = new ArrayList();
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
                try {
                    while (rs.next()) {
                        indexes.add(rs.getInt(1));
                    }
                    JDBCUtils.logWarnings(rs);
                } finally {
                    rs.close();
                }
                if (indexes.isEmpty()) {
                    return null;
                }
                int i = 0;
                int[] indicesReturn = new int[indexes.size()];
                for (Integer idx : indexes) {
                    indicesReturn[i++] = idx;
                }
                return indicesReturn;
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056017", stmt), e);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 50 with ExecutionContext

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

the class AbstractMapStore method containsValue.

/**
 * Method to check if a value exists in the Map.
 * @param op ObjectProvider for the map
 * @param value The value to check for.
 * @return Whether the value exists in the Map.
 */
public boolean containsValue(ObjectProvider op, Object value) {
    if (value == null) {
        // nulls not allowed
        return false;
    }
    if (!validateValueForReading(op, value)) {
        return false;
    }
    boolean exists = false;
    try {
        ExecutionContext ec = op.getExecutionContext();
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, containsValueStmt);
            try {
                int jdbcPosition = 1;
                jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                BackingStoreHelper.populateValueInStatement(ec, ps, value, jdbcPosition, getValueMapping());
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, containsValueStmt, ps);
                try {
                    if (rs.next()) {
                        exists = true;
                    }
                    JDBCUtils.logWarnings(rs);
                } finally {
                    rs.close();
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        NucleusLogger.DATASTORE_RETRIEVE.warn("Exception during backing store select", e);
        throw new NucleusDataStoreException(Localiser.msg("056019", containsValueStmt), e);
    }
    return exists;
}
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