Search in sources :

Example 51 with SQLController

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

the class SequenceTable method getFetchAllSequences.

/**
 * Accessor for the sequences
 * @param conn Connection for this datastore.
 * @return The HashSet of Sequence names
 * @throws SQLException Thrown when an error occurs in the process.
 */
public HashSet getFetchAllSequences(ManagedConnection conn) throws SQLException {
    HashSet sequenceNames = new HashSet();
    PreparedStatement ps = null;
    SQLController sqlControl = storeMgr.getSQLController();
    try {
        ps = sqlControl.getStatementForQuery(conn, fetchAllStmt);
        ResultSet rs = sqlControl.executeStatementQuery(null, conn, fetchAllStmt, ps);
        try {
            while (rs.next()) {
                sequenceNames.add(rs.getString(2));
            }
        } finally {
            rs.close();
        }
    } finally {
        if (ps != null) {
            sqlControl.closeStatement(conn, ps);
        }
    }
    return sequenceNames;
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashSet(java.util.HashSet) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 52 with SQLController

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

the class SequenceTable method deleteAllSequences.

/**
 * Method to delete all sequences
 *
 * @param conn Connection to the datastore
 * @throws SQLException Thrown when an error occurs deleting.
 */
public void deleteAllSequences(ManagedConnection conn) throws SQLException {
    PreparedStatement ps = null;
    SQLController sqlControl = storeMgr.getSQLController();
    try {
        ps = sqlControl.getStatementForUpdate(conn, deleteAllStmt, false);
        sqlControl.executeStatementUpdate(null, conn, deleteAllStmt, ps, true);
    // TODO : handle any warning messages
    } finally {
        if (ps != null) {
            sqlControl.closeStatement(conn, ps);
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 53 with SQLController

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

the class FKArrayStore method updateElementFk.

/**
 * Update a FK and element position in the element.
 * @param ownerOP ObjectProvider for the owner
 * @param element The element to update
 * @param owner The owner object to set in the FK
 * @param index The index position (or -1 if not known)
 * @return Whether it was performed successfully
 */
private boolean updateElementFk(ObjectProvider ownerOP, E element, Object owner, int index) {
    if (element == null) {
        return false;
    }
    boolean retval;
    String updateFkStmt = getUpdateFkStmt();
    ExecutionContext ec = ownerOP.getExecutionContext();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, updateFkStmt, false);
            try {
                int jdbcPosition = 1;
                if (elementInfo.length > 1) {
                    DatastoreClass table = storeMgr.getDatastoreClass(element.getClass().getName(), clr);
                    if (table != null) {
                        ps.setString(jdbcPosition++, table.toString());
                    } else {
                        NucleusLogger.PERSISTENCE.info(">> FKArrayStore.updateElementFK : need to set table in statement but dont know table where to store " + element);
                    }
                }
                if (owner == null) {
                    ownerMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, ownerMapping), null);
                    jdbcPosition += ownerMapping.getNumberOfDatastoreMappings();
                } else {
                    jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
                }
                jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, index, jdbcPosition, orderMapping);
                if (relationDiscriminatorMapping != null) {
                    jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                }
                jdbcPosition = BackingStoreHelper.populateElementInStatement(ec, ps, element, jdbcPosition, elementMapping);
                sqlControl.executeStatementUpdate(ec, mconn, updateFkStmt, ps, true);
                retval = true;
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056027", updateFkStmt), e);
    }
    return retval;
}
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) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 54 with SQLController

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

the class FKListStore method clear.

/**
 * Method to clear the List.
 * This is called by the List.clear() method, or when the container object is being deleted
 * and the elements are to be removed (maybe for dependent field), or also when updating a Collection
 * and removing all existing prior to adding all new.
 * @param ownerOP ObjectProvider for the owner
 */
public void clear(ObjectProvider ownerOP) {
    boolean deleteElements = false;
    ExecutionContext ec = ownerOP.getExecutionContext();
    boolean dependent = ownerMemberMetaData.getCollection().isDependentElement();
    if (ownerMemberMetaData.isCascadeRemoveOrphans()) {
        dependent = true;
    }
    if (dependent) {
        // Elements are dependent and can't exist on their own, so delete them all
        NucleusLogger.DATASTORE.debug(Localiser.msg("056034"));
        deleteElements = true;
    } else {
        if (ownerMapping.isNullable() && orderMapping == null) {
            // Field is not dependent, and nullable so we null the FK
            NucleusLogger.DATASTORE.debug(Localiser.msg("056036"));
            deleteElements = false;
        } else if (ownerMapping.isNullable() && orderMapping != null && orderMapping.isNullable()) {
            // Field is not dependent, and nullable so we null the FK
            NucleusLogger.DATASTORE.debug(Localiser.msg("056036"));
            deleteElements = false;
        } else {
            // Field is not dependent, and not nullable so we just delete the elements
            NucleusLogger.DATASTORE.debug(Localiser.msg("056035"));
            deleteElements = true;
        }
    }
    if (deleteElements) {
        // Find elements present in the datastore and delete them one-by-one
        Iterator elementsIter = iterator(ownerOP);
        if (elementsIter != null) {
            while (elementsIter.hasNext()) {
                Object element = elementsIter.next();
                if (ec.getApiAdapter().isPersistable(element) && ec.getApiAdapter().isDeleted(element)) {
                    // Element is waiting to be deleted so flush it (it has the FK)
                    ObjectProvider objSM = ec.findObjectProvider(element);
                    objSM.flush();
                } else {
                    // Element not yet marked for deletion so go through the normal process
                    ec.deleteObjectInternal(element);
                }
            }
        }
    } else {
        boolean ownerSoftDelete = ownerOP.getClassMetaData().hasExtension(MetaData.EXTENSION_CLASS_SOFTDELETE);
        if (!ownerSoftDelete) {
            // Clear without delete
            // TODO If the relation is bidirectional we need to clear the owner in the element
            String clearNullifyStmt = getClearNullifyStmt();
            try {
                ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
                SQLController sqlControl = storeMgr.getSQLController();
                try {
                    PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, clearNullifyStmt, 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, clearNullifyStmt, ps, true);
                    } finally {
                        sqlControl.closeStatement(mconn, ps);
                    }
                } finally {
                    mconn.release();
                }
            } catch (SQLException e) {
                throw new NucleusDataStoreException(Localiser.msg("056013", clearNullifyStmt), e);
            }
        }
    }
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) SQLException(java.sql.SQLException) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ObjectProvider(org.datanucleus.state.ObjectProvider) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 55 with SQLController

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

the class FKSetStore method updateElementFk.

/**
 * Utility to update a foreign-key (and distinguisher) in the element in the case of a unidirectional 1-N relationship.
 * @param ownerOP ObjectProvider for the owner
 * @param element The element to update
 * @param owner The owner object to set in the FK
 * @return Whether it was performed successfully
 */
private boolean updateElementFk(ObjectProvider ownerOP, Object element, Object owner) {
    if (element == null) {
        return false;
    }
    validateElementForWriting(ownerOP.getExecutionContext(), element, null);
    boolean retval;
    ExecutionContext ec = ownerOP.getExecutionContext();
    String stmt = getUpdateFkStmt(element);
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
            try {
                ComponentInfo elemInfo = getComponentInfoForElement(element);
                JavaTypeMapping ownerMapping = elemInfo.getOwnerMapping();
                JavaTypeMapping elemMapping = elemInfo.getDatastoreClass().getIdMapping();
                int jdbcPosition = 1;
                if (owner == null) {
                    ownerMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, ownerMapping), null, ownerOP, ownerMemberMetaData.getAbsoluteFieldNumber());
                } else {
                    ownerMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, ownerMapping), ownerOP.getObject(), ownerOP, ownerMemberMetaData.getAbsoluteFieldNumber());
                }
                jdbcPosition += ownerMapping.getNumberOfDatastoreMappings();
                if (relationDiscriminatorMapping != null) {
                    jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                }
                jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, element, jdbcPosition, elemMapping);
                sqlControl.executeStatementUpdate(ec, mconn, stmt, ps, true);
                retval = true;
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056027", stmt), e);
    }
    return retval;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SQLException(java.sql.SQLException) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) 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