Search in sources :

Example 11 with PersistentClassROF

use of org.datanucleus.store.rdbms.query.PersistentClassROF in project datanucleus-rdbms by datanucleus.

the class MapValueCollectionStore method iterator.

/**
 * Accessor for an iterator for the set.
 * @param ownerOP ObjectProvider for the set.
 * @return Iterator for the set.
 */
public Iterator<V> iterator(ObjectProvider ownerOP) {
    ExecutionContext ec = ownerOP.getExecutionContext();
    if (iteratorStmtLocked == null) {
        synchronized (// Make sure this completes in case another thread needs the same info
        this) {
            // Generate the statement, and statement mapping/parameter information
            SQLStatement sqlStmt = getSQLStatementForIterator(ownerOP);
            iteratorStmtUnlocked = sqlStmt.getSQLText().toSQL();
            sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
            iteratorStmtLocked = sqlStmt.getSQLText().toSQL();
        }
    }
    Transaction tx = ec.getTransaction();
    String stmt = (tx.getSerializeRead() != null && tx.getSerializeRead() ? iteratorStmtLocked : iteratorStmtUnlocked);
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            // Create the statement and set the owner
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
            StatementMappingIndex ownerIdx = iteratorMappingParams.getMappingForParameter("owner");
            int numParams = ownerIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), ownerOP.getObject());
            }
            try {
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
                try {
                    ResultObjectFactory rof = null;
                    if (elementsAreEmbedded || elementsAreSerialised) {
                        // No ResultObjectFactory needed - handled by SetStoreIterator
                        return new CollectionStoreIterator(ownerOP, rs, null, this);
                    }
                    rof = new PersistentClassROF(ec, rs, false, iteratorMappingDef, elementCmd, clr.classForName(elementType));
                    return new CollectionStoreIterator(ownerOP, rs, rof, this);
                } finally {
                    rs.close();
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
    } catch (MappedDatastoreException e) {
        throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) SQLException(java.sql.SQLException) ResultObjectFactory(org.datanucleus.store.rdbms.query.ResultObjectFactory) PreparedStatement(java.sql.PreparedStatement) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) SQLStatement(org.datanucleus.store.rdbms.sql.SQLStatement) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) Transaction(org.datanucleus.Transaction) PersistentClassROF(org.datanucleus.store.rdbms.query.PersistentClassROF) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Aggregations

PersistentClassROF (org.datanucleus.store.rdbms.query.PersistentClassROF)11 ResultObjectFactory (org.datanucleus.store.rdbms.query.ResultObjectFactory)11 PreparedStatement (java.sql.PreparedStatement)10 ResultSet (java.sql.ResultSet)10 SQLException (java.sql.SQLException)10 ExecutionContext (org.datanucleus.ExecutionContext)10 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)10 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)10 SQLController (org.datanucleus.store.rdbms.SQLController)10 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)10 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)8 Transaction (org.datanucleus.Transaction)6 ObjectProvider (org.datanucleus.state.ObjectProvider)6 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)6 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)6 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)6 SQLStatement (org.datanucleus.store.rdbms.sql.SQLStatement)4 NucleusException (org.datanucleus.exceptions.NucleusException)3 SerialisedReferenceMapping (org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping)3 NoSuchElementException (java.util.NoSuchElementException)2