Search in sources :

Example 46 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException 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 47 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException 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)

Example 48 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException 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 49 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException 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)

Example 50 with NucleusDataStoreException

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

the class RDBMSSchemaHandler method getRDBMSSchemasInfo.

/**
 * Convenience method to read the schemas information for this datastore.
 * @param conn Connection to the datastore
 * @param schemaName Name of the schema to check for
 * @param catalogName Name of the catalog to check for
 * @return The RDBMSTypesInfo
 */
protected RDBMSSchemaInfo getRDBMSSchemasInfo(Connection conn, String schemaName, String catalogName) {
    try {
        if (conn == null) {
            // No connection provided so nothing to return
            return null;
        }
        DatabaseMetaData dmd = conn.getMetaData();
        ResultSet rs = dmd.getSchemas();
        try {
            String inputSchema = getNameWithoutQuotes(schemaName);
            String inputCatalog = getNameWithoutQuotes(catalogName);
            while (rs.next()) {
                String schema = rs.getString("TABLE_SCHEM");
                String foundSchema = getNameWithoutQuotes(schema);
                boolean schemaCorrect = false;
                if (StringUtils.isWhitespace(inputSchema) && StringUtils.isWhitespace(foundSchema)) {
                    schemaCorrect = true;
                } else {
                    if (inputSchema != null && inputSchema.equals(foundSchema)) {
                        schemaCorrect = true;
                    } else if (foundSchema != null && StringUtils.isWhitespace(inputSchema) && foundSchema.equals(((RDBMSStoreManager) storeMgr).getSchemaName())) {
                        schemaCorrect = true;
                    }
                }
                boolean catalogCorrect = false;
                String catalog = inputCatalog;
                try {
                    catalog = rs.getString("TABLE_CATALOG");
                    String foundCatalog = getNameWithoutQuotes(catalog);
                    if (StringUtils.isWhitespace(inputCatalog) && StringUtils.isWhitespace(foundCatalog)) {
                        catalogCorrect = true;
                    } else if (inputCatalog != null && inputCatalog.equals(foundCatalog)) {
                        catalogCorrect = true;
                    } else if (foundCatalog != null && StringUtils.isWhitespace(inputCatalog) && foundCatalog.equals(((RDBMSStoreManager) storeMgr).getCatalogName())) {
                        catalogCorrect = true;
                    }
                } catch (SQLException sqle) {
                    // This datastore doesn't return catalog (e.g Oracle)
                    if (catalogName == null) {
                        catalogCorrect = true;
                    }
                }
                if (schemaCorrect && catalogCorrect) {
                    return new RDBMSSchemaInfo(catalog, schema);
                }
            }
        } finally {
            rs.close();
        }
    } catch (SQLException sqle) {
        throw new NucleusDataStoreException("Exception thrown retrieving schema information from datastore", sqle);
    }
    return null;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)200 SQLException (java.sql.SQLException)98 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)74 ExecutionContext (org.datanucleus.ExecutionContext)73 PreparedStatement (java.sql.PreparedStatement)63 SQLController (org.datanucleus.store.rdbms.SQLController)60 ResultSet (java.sql.ResultSet)45 Iterator (java.util.Iterator)34 CollectionAddOperation (org.datanucleus.flush.CollectionAddOperation)34 CollectionRemoveOperation (org.datanucleus.flush.CollectionRemoveOperation)26 ObjectProvider (org.datanucleus.state.ObjectProvider)22 ArrayList (java.util.ArrayList)21 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)21 Collection (java.util.Collection)20 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)20 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)19 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)18 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)15 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)14 List (java.util.List)13