Search in sources :

Example 21 with PersistentClassROF

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

the class FKMapStore method getValue.

/**
 * Method to retrieve a value from the Map given the key.
 * @param ownerSM StateManager for the owner of the map.
 * @param key The key to retrieve the value for.
 * @return The value for this key
 * @throws NoSuchElementException if the key was not found
 */
protected V getValue(DNStateManager ownerSM, Object key) throws NoSuchElementException {
    if (!validateKeyForReading(ownerSM, key)) {
        return null;
    }
    ExecutionContext ec = ownerSM.getExecutionContext();
    if (getStmtLocked == 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 = getSQLStatementForGet(ownerSM);
            getStmtUnlocked = sqlStmt.getSQLText().toSQL();
            sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
            getStmtLocked = sqlStmt.getSQLText().toSQL();
        }
    }
    Boolean serializeRead = ec.getTransaction().getSerializeRead();
    String stmt = (serializeRead != null && serializeRead ? getStmtLocked : getStmtUnlocked);
    Object value = null;
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            // Create the statement and supply owner/key params
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
            StatementMappingIndex ownerIdx = getMappingParams.getMappingForParameter("owner");
            int numParams = ownerIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), ownerSM.getObject());
            }
            StatementMappingIndex keyIdx = getMappingParams.getMappingForParameter("key");
            numParams = keyIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                keyIdx.getMapping().setObject(ec, ps, keyIdx.getParameterPositionsForOccurrence(paramInstance), key);
            }
            try {
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
                try {
                    boolean found = rs.next();
                    if (!found) {
                        throw new NoSuchElementException();
                    }
                    if (valuesAreEmbedded || valuesAreSerialised) {
                        // Embedded/serialised into table of key
                        int[] param = new int[valueMapping.getNumberOfColumnMappings()];
                        for (int i = 0; i < param.length; ++i) {
                            param[i] = i + 1;
                        }
                        if (valueMapping instanceof SerialisedPCMapping || valueMapping instanceof SerialisedReferenceMapping || valueMapping instanceof EmbeddedKeyPCMapping) {
                            // Value = Serialised
                            String msg = "You appear to have a map (persistable) value serialised/embedded into the key of the map at " + getOwnerMemberMetaData().getFullFieldName() + " Not supported";
                            NucleusLogger.PERSISTENCE.error(msg);
                            throw new NucleusDataStoreException(msg);
                        }
                        // Value = Non-PC
                        value = valueMapping.getObject(ec, rs, param);
                    } else if (valueMapping instanceof ReferenceMapping) {
                        // Value = Reference (Interface/Object)
                        int[] param = new int[valueMapping.getNumberOfColumnMappings()];
                        for (int i = 0; i < param.length; ++i) {
                            param[i] = i + 1;
                        }
                        value = valueMapping.getObject(ec, rs, param);
                    } else {
                        // Value = PC
                        value = new PersistentClassROF(ec, rs, false, ec.getFetchPlan(), getMappingDef, valueCmd, clr.classForName(valueType)).getObject();
                    }
                    JDBCUtils.logWarnings(rs);
                } finally {
                    rs.close();
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056014", stmt), e);
    }
    return (V) value;
}
Also used : SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) SQLException(java.sql.SQLException) 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) SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) PersistentClassROF(org.datanucleus.store.rdbms.query.PersistentClassROF) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) SerialisedPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping) NoSuchElementException(java.util.NoSuchElementException) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Aggregations

PersistentClassROF (org.datanucleus.store.rdbms.query.PersistentClassROF)21 PreparedStatement (java.sql.PreparedStatement)20 ResultSet (java.sql.ResultSet)20 SQLException (java.sql.SQLException)20 ExecutionContext (org.datanucleus.ExecutionContext)20 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)20 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)20 SQLController (org.datanucleus.store.rdbms.SQLController)20 ResultObjectFactory (org.datanucleus.store.rdbms.query.ResultObjectFactory)20 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)20 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)12 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)12 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)11 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)8 SQLStatement (org.datanucleus.store.rdbms.sql.SQLStatement)8 Transaction (org.datanucleus.Transaction)6 NucleusException (org.datanucleus.exceptions.NucleusException)6 DNStateManager (org.datanucleus.state.DNStateManager)6 ObjectProvider (org.datanucleus.state.ObjectProvider)6 SerialisedReferenceMapping (org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping)5