Search in sources :

Example 71 with SQLController

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

the class AbstractMapStore method updateEmbeddedValue.

public boolean updateEmbeddedValue(ObjectProvider op, Object value, int fieldNumber, Object newValue, JavaTypeMapping fieldMapping) {
    boolean modified;
    String stmt = getUpdateEmbeddedValueStmt(fieldMapping, getOwnerMapping(), getValueMapping(), getMapTable());
    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), newValue);
                jdbcPosition += fieldMapping.getNumberOfDatastoreMappings();
                jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                jdbcPosition = BackingStoreHelper.populateEmbeddedValueFieldsInStatement(op, value, ps, jdbcPosition, (JoinTable) getMapTable(), this);
                sqlControl.executeStatementUpdate(ec, mconn, stmt, ps, true);
                modified = true;
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        NucleusLogger.DATASTORE_PERSIST.warn("Exception in backing store update", e);
        throw new NucleusDataStoreException(Localiser.msg("056011", 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 72 with SQLController

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

the class AbstractMapStore method updatedEmbeddedKey.

public boolean updatedEmbeddedKey(ObjectProvider op, Object key, int fieldNumber, Object newValue, JavaTypeMapping fieldMapping) {
    boolean modified;
    String stmt = getUpdateEmbeddedKeyStmt(fieldMapping, getOwnerMapping(), getKeyMapping(), getMapTable());
    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), key);
                jdbcPosition += fieldMapping.getNumberOfDatastoreMappings();
                jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                jdbcPosition = BackingStoreHelper.populateEmbeddedKeyFieldsInStatement(op, key, ps, jdbcPosition, (JoinTable) getMapTable(), this);
                sqlControl.executeStatementUpdate(ec, mconn, stmt, ps, true);
                modified = true;
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        NucleusLogger.DATASTORE_PERSIST.warn("Exception during backing store update", e);
        throw new NucleusDataStoreException(Localiser.msg("056010", 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 73 with SQLController

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

the class AbstractSetStore method removeAll.

/**
 * Remove all elements from a collection from the association owner vs elements.
 * This implementation iterates around the remove() method doing each element 1 at a time.
 * Please refer to the JoinSetStore and FKSetStore for the variations used there.
 * This is used for Map key and value stores.
 * @param op ObjectProvider for the container
 * @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;
    }
    boolean modified = false;
    List exceptions = new ArrayList();
    boolean batched = (elements.size() > 1);
    // Validate all elements exist
    Iterator iter = elements.iterator();
    while (iter.hasNext()) {
        Object element = iter.next();
        if (!validateElementForReading(op, element)) {
            NucleusLogger.DATASTORE.debug("AbstractSetStore::removeAll element=" + element + " doesn't exist in this Set.");
            return false;
        }
    }
    try {
        ExecutionContext ec = op.getExecutionContext();
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        try {
            // Process all waiting batched statements before we start our work
            SQLController sqlControl = storeMgr.getSQLController();
            try {
                sqlControl.processStatementsForConnection(mconn);
            } catch (SQLException e) {
                throw new MappedDatastoreException("SQLException", e);
            }
            iter = elements.iterator();
            while (iter.hasNext()) {
                Object element = iter.next();
                try {
                    // Process the remove
                    int[] rc = null;
                    String removeStmt = getRemoveStmt(element);
                    try {
                        PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeStmt, batched);
                        try {
                            int jdbcPosition = 1;
                            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);
                            }
                            // Execute the statement
                            rc = sqlControl.executeStatementUpdate(ec, mconn, removeStmt, ps, !batched || (batched && !iter.hasNext()));
                            if (rc != null) {
                                for (int i = 0; i < rc.length; i++) {
                                    if (rc[i] > 0) {
                                        // At least one record was inserted
                                        modified = true;
                                    }
                                }
                            }
                        } finally {
                            sqlControl.closeStatement(mconn, ps);
                        }
                    } catch (SQLException e) {
                        throw new MappedDatastoreException("SQLException", e);
                    }
                } catch (MappedDatastoreException mde) {
                    exceptions.add(mde);
                    NucleusLogger.DATASTORE.error("Exception in remove", mde);
                }
            }
        } finally {
            mconn.release();
        }
    } catch (MappedDatastoreException e) {
        exceptions.add(e);
        NucleusLogger.DATASTORE.error("Exception performing removeAll on set backing store", e);
    }
    if (!exceptions.isEmpty()) {
        // Throw all exceptions received as the cause of a NucleusDataStoreException so the user can see which record(s) didn't remove
        String msg = Localiser.msg("056012", ((Exception) exceptions.get(0)).getMessage());
        NucleusLogger.DATASTORE.error(msg);
        throw new NucleusDataStoreException(msg, (Throwable[]) exceptions.toArray(new Throwable[exceptions.size()]), op.getObject());
    }
    return modified;
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) 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) ArrayList(java.util.ArrayList) List(java.util.List) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 74 with SQLController

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

the class ElementContainerStore method clear.

/**
 * Clear the association from owner to all elements.
 * Provides cascade-delete when the elements being deleted are PC types.
 * @param ownerOP ObjectProvider for the container.
 */
public void clear(ObjectProvider ownerOP) {
    Collection dependentElements = null;
    CollectionMetaData collmd = ownerMemberMetaData.getCollection();
    boolean dependent = collmd.isDependentElement();
    if (ownerMemberMetaData.isCascadeRemoveOrphans()) {
        dependent = true;
    }
    if (dependent && !collmd.isEmbeddedElement() && !collmd.isSerializedElement()) {
        // Retain the dependent elements that need deleting after clearing
        dependentElements = new HashSet();
        Iterator iter = iterator(ownerOP);
        while (iter.hasNext()) {
            dependentElements.add(iter.next());
        }
    }
    boolean ownerSoftDelete = ownerOP.getClassMetaData().hasExtension(MetaData.EXTENSION_CLASS_SOFTDELETE);
    boolean elementSoftDelete = (elementInfo != null ? elementInfo[0].cmd.hasExtension(MetaData.EXTENSION_CLASS_SOFTDELETE) : false);
    if (ownerSoftDelete && elementSoftDelete) {
    // Owner and elements are being soft deleted, so no need to touch join table entries
    } else if (!dependent && ownerSoftDelete) {
    // Not deleting the elements, but owner is being soft deleted, so leave join table entries
    } else {
        String clearStmt = getClearStmt();
        try {
            ExecutionContext ec = ownerOP.getExecutionContext();
            ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
            SQLController sqlControl = storeMgr.getSQLController();
            try {
                PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, clearStmt, false);
                try {
                    int jdbcPosition = 1;
                    jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
                    if (relationDiscriminatorMapping != null) {
                        BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                    }
                    sqlControl.executeStatementUpdate(ec, mconn, clearStmt, ps, true);
                } finally {
                    sqlControl.closeStatement(mconn, ps);
                }
            } finally {
                mconn.release();
            }
        } catch (SQLException e) {
            throw new NucleusDataStoreException(Localiser.msg("056013", clearStmt), e);
        }
    }
    // Cascade-delete
    if (dependentElements != null && dependentElements.size() > 0) {
        Iterator iter = dependentElements.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (ownerOP.getExecutionContext().getApiAdapter().isDeleted(obj)) {
            // Element is tagged for deletion so will be deleted at flush(), and we dont need it immediately
            } else {
                ownerOP.getExecutionContext().deleteObjectInternal(obj);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) CollectionMetaData(org.datanucleus.metadata.CollectionMetaData) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) Iterator(java.util.Iterator) Collection(java.util.Collection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) HashSet(java.util.HashSet)

Example 75 with SQLController

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

the class SQLStatementHelper method getPreparedStatementForSQLStatement.

/**
 * Convenience method to return a PreparedStatement for an SQLStatement.
 * @param sqlStmt The query expression
 * @param ec execution context
 * @param mconn The connection to use
 * @param resultSetType Type of result set (if any)
 * @param resultSetConcurrency result-set concurrency (if any)
 * @return The PreparedStatement
 * @throws SQLException If an error occurs in creation
 */
public static PreparedStatement getPreparedStatementForSQLStatement(SQLStatement sqlStmt, ExecutionContext ec, ManagedConnection mconn, String resultSetType, String resultSetConcurrency) throws SQLException {
    SQLText sqlText = sqlStmt.getSQLText();
    SQLController sqlControl = sqlStmt.getRDBMSManager().getSQLController();
    // Generate the statement using the statement text
    PreparedStatement ps = sqlControl.getStatementForQuery(mconn, sqlText.toString(), resultSetType, resultSetConcurrency);
    boolean done = false;
    try {
        // Apply any parameter values for the statement
        sqlText.applyParametersToStatement(ec, ps);
        done = true;
    } finally {
        if (!done) {
            sqlControl.closeStatement(mconn, ps);
        }
    }
    return ps;
}
Also used : PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Aggregations

SQLController (org.datanucleus.store.rdbms.SQLController)83 PreparedStatement (java.sql.PreparedStatement)82 SQLException (java.sql.SQLException)72 ExecutionContext (org.datanucleus.ExecutionContext)63 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)63 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)60 ResultSet (java.sql.ResultSet)36 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)20 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)19 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)17 ArrayList (java.util.ArrayList)15 List (java.util.List)13 ObjectProvider (org.datanucleus.state.ObjectProvider)12 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)12 Iterator (java.util.Iterator)11 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)10 PersistentClassROF (org.datanucleus.store.rdbms.query.PersistentClassROF)10 ResultObjectFactory (org.datanucleus.store.rdbms.query.ResultObjectFactory)10 NucleusException (org.datanucleus.exceptions.NucleusException)9 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)9