Search in sources :

Example 11 with EmbeddedKeyPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping in project datanucleus-rdbms by datanucleus.

the class JoinMapStore method updateEmbeddedKey.

@Override
public boolean updateEmbeddedKey(DNStateManager sm, Object key, int fieldNumber, Object newValue) {
    boolean modified = false;
    if (keyMapping != null && keyMapping instanceof EmbeddedKeyPCMapping) {
        String fieldName = valueCmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
        if (fieldName == null) {
            // We have no mapping for this field so presumably is the owner field or a PK field
            return false;
        }
        JavaTypeMapping fieldMapping = ((EmbeddedKeyPCMapping) keyMapping).getJavaTypeMapping(fieldName);
        if (fieldMapping == null) {
            // We have no mapping for this field so presumably is the owner field or a PK field
            return false;
        }
        // Update the embedded key
        String stmt = getUpdateEmbeddedKeyStmt(fieldMapping, getOwnerMapping(), getKeyMapping(), mapTable);
        try {
            ExecutionContext ec = sm.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.getNumberOfColumnMappings();
                    jdbcPosition = BackingStoreHelper.populateOwnerInStatement(sm, ec, ps, jdbcPosition, this);
                    jdbcPosition = BackingStoreHelper.populateEmbeddedKeyFieldsInStatement(sm, key, ps, jdbcPosition, mapTable, this);
                    sqlControl.executeStatementUpdate(ec, mconn, stmt, ps, true);
                    modified = true;
                } finally {
                    sqlControl.closeStatement(mconn, ps);
                }
            } finally {
                mconn.release();
            }
        } catch (SQLException e) {
            String msg = Localiser.msg("056010", stmt);
            NucleusLogger.DATASTORE_PERSIST.warn(msg, e);
            throw new NucleusDataStoreException(msg, e);
        }
    }
    return modified;
}
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) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 12 with EmbeddedKeyPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping in project datanucleus-rdbms by datanucleus.

the class JoinMapStore method getUpdateEmbeddedKeyStmt.

/**
 * Generate statement for update the field of an embedded key.
 * <PRE>
 * UPDATE MAPTABLE
 * SET EMBEDDEDKEYCOL1 = ?
 * WHERE OWNERCOL=?
 * AND EMBEDDEDKEYCOL1 = ?
 * AND EMBEDDEDKEYCOL2 = ? ...
 * </PRE>
 * @param fieldMapping The mapping for the field (of the key) to be updated
 * @param ownerMapping The owner mapping
 * @param keyMapping The key mapping
 * @param mapTable The map table
 * @return Statement for updating an embedded key in the Set
 */
protected String getUpdateEmbeddedKeyStmt(JavaTypeMapping fieldMapping, JavaTypeMapping ownerMapping, JavaTypeMapping keyMapping, Table mapTable) {
    StringBuilder stmt = new StringBuilder("UPDATE ");
    stmt.append(mapTable.toString());
    stmt.append(" SET ");
    for (int i = 0; i < fieldMapping.getNumberOfColumnMappings(); i++) {
        if (i > 0) {
            stmt.append(",");
        }
        stmt.append(fieldMapping.getColumnMapping(i).getColumn().getIdentifier().toString());
        stmt.append(" = ");
        stmt.append(fieldMapping.getColumnMapping(i).getUpdateInputParameter());
    }
    stmt.append(" WHERE ");
    BackingStoreHelper.appendWhereClauseForMapping(stmt, ownerMapping, null, true);
    EmbeddedKeyPCMapping embeddedMapping = (EmbeddedKeyPCMapping) keyMapping;
    for (int i = 0; i < embeddedMapping.getNumberOfJavaTypeMappings(); i++) {
        JavaTypeMapping m = embeddedMapping.getJavaTypeMapping(i);
        if (m != null) {
            for (int j = 0; j < m.getNumberOfColumnMappings(); j++) {
                stmt.append(" AND ");
                stmt.append(m.getColumnMapping(j).getColumn().getIdentifier().toString());
                stmt.append(" = ");
                stmt.append(m.getColumnMapping(j).getUpdateInputParameter());
            }
        }
    }
    return stmt.toString();
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Example 13 with EmbeddedKeyPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping in project datanucleus-rdbms by datanucleus.

the class BackingStoreHelper method populateEmbeddedKeyFieldsInStatement.

/**
 * Convenience method to populate the passed PreparedStatement with the field values from
 * the embedded map key starting at the specified jdbc position.
 * @param sm StateManager of the owning container
 * @param key The embedded key
 * @param ps The PreparedStatement
 * @param jdbcPosition JDBC position in the statement to start at
 * @param joinTable The Join table where the values are embedded
 * @param mapStore the map store
 * @return The next JDBC position
 */
public static int populateEmbeddedKeyFieldsInStatement(DNStateManager sm, Object key, PreparedStatement ps, int jdbcPosition, JoinTable joinTable, AbstractMapStore mapStore) {
    AbstractClassMetaData kmd = mapStore.getKeyClassMetaData();
    EmbeddedKeyPCMapping embeddedMapping = (EmbeddedKeyPCMapping) mapStore.getKeyMapping();
    StatementClassMapping mappingDefinition = new StatementClassMapping();
    int[] elementFieldNumbers = new int[embeddedMapping.getNumberOfJavaTypeMappings()];
    for (int i = 0; i < embeddedMapping.getNumberOfJavaTypeMappings(); i++) {
        JavaTypeMapping fieldMapping = embeddedMapping.getJavaTypeMapping(i);
        int absFieldNum = kmd.getAbsolutePositionOfMember(fieldMapping.getMemberMetaData().getName());
        elementFieldNumbers[i] = absFieldNum;
        StatementMappingIndex stmtMapping = new StatementMappingIndex(fieldMapping);
        int[] jdbcParamPositions = new int[fieldMapping.getNumberOfColumnMappings()];
        for (int j = 0; j < fieldMapping.getNumberOfColumnMappings(); j++) {
            jdbcParamPositions[j] = jdbcPosition++;
        }
        stmtMapping.addParameterOccurrence(jdbcParamPositions);
        mappingDefinition.addMappingForMember(absFieldNum, stmtMapping);
    }
    DNStateManager elementSM = mapStore.getStateManagerForEmbeddedPCObject(sm, key, joinTable.getOwnerMemberMetaData(), PersistableObjectType.EMBEDDED_MAP_KEY_PC);
    elementSM.provideFields(elementFieldNumbers, new ParameterSetter(elementSM, ps, mappingDefinition));
    return jdbcPosition;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) DNStateManager(org.datanucleus.state.DNStateManager) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) ParameterSetter(org.datanucleus.store.rdbms.fieldmanager.ParameterSetter) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping) StatementClassMapping(org.datanucleus.store.rdbms.query.StatementClassMapping)

Example 14 with EmbeddedKeyPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping 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

EmbeddedKeyPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)14 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)9 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)6 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)6 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 ExecutionContext (org.datanucleus.ExecutionContext)5 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)5 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)5 SQLController (org.datanucleus.store.rdbms.SQLController)5 ResultSet (java.sql.ResultSet)4 NoSuchElementException (java.util.NoSuchElementException)4 EmbeddedValuePCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping)4 SerialisedPCMapping (org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping)4 SerialisedReferenceMapping (org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping)4 PersistentClassROF (org.datanucleus.store.rdbms.query.PersistentClassROF)4 SQLStatement (org.datanucleus.store.rdbms.sql.SQLStatement)4 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)3 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)3 ResultObjectFactory (org.datanucleus.store.rdbms.query.ResultObjectFactory)3