Search in sources :

Example 6 with EmbeddedKeyPCMapping

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

the class MapTable method getExpectedCandidateKeys.

/**
 * Accessor for the candidate keys for this table.
 * @return The indices
 */
protected List getExpectedCandidateKeys() {
    // The indices required by foreign keys (BaseTable)
    List candidateKeys = super.getExpectedCandidateKeys();
    if (keyMapping instanceof EmbeddedKeyPCMapping) {
        // Add all candidate keys required by fields of the embedded key
        EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
        for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
            JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
            UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
            if (umd != null) {
                CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                if (ck != null) {
                    candidateKeys.add(ck);
                }
            }
        }
    } else if (mmd.getKeyMetaData() != null) {
        UniqueMetaData unimd = mmd.getKeyMetaData().getUniqueMetaData();
        if (unimd != null) {
            CandidateKey ck = TableUtils.getCandidateKeyForField(this, unimd, keyMapping);
            if (ck != null) {
                candidateKeys.add(ck);
            }
        }
    }
    if (valueMapping instanceof EmbeddedValuePCMapping) {
        // Add all candidate keys required by fields of the embedded value
        EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
        for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
            JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
            UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
            if (umd != null) {
                CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
                if (ck != null) {
                    candidateKeys.add(ck);
                }
            }
        }
    } else if (mmd.getValueMetaData() != null) {
        UniqueMetaData unimd = mmd.getValueMetaData().getUniqueMetaData();
        if (unimd != null) {
            CandidateKey ck = TableUtils.getCandidateKeyForField(this, unimd, valueMapping);
            if (ck != null) {
                candidateKeys.add(ck);
            }
        }
    }
    return candidateKeys;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) List(java.util.List) UniqueMetaData(org.datanucleus.metadata.UniqueMetaData) EmbeddedValuePCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Example 7 with EmbeddedKeyPCMapping

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

the class MapTable method getExpectedForeignKeys.

/**
 * Accessor for the expected foreign keys for this table.
 * @param clr The ClassLoaderResolver
 * @return The expected foreign keys.
 */
public List getExpectedForeignKeys(ClassLoaderResolver clr) {
    assertIsInitialized();
    boolean autoMode = false;
    if (storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_RDBMS_CONSTRAINT_CREATE_MODE).equals("DataNucleus")) {
        autoMode = true;
    }
    ArrayList foreignKeys = new ArrayList();
    try {
        // FK from join table to owner table
        DatastoreClass referencedTable = storeMgr.getDatastoreClass(ownerType, clr);
        if (referencedTable != null) {
            // Take <foreign-key> from <join>
            ForeignKeyMetaData fkmd = null;
            if (mmd.getJoinMetaData() != null) {
                fkmd = mmd.getJoinMetaData().getForeignKeyMetaData();
            }
            if (fkmd != null || autoMode) {
                ForeignKey fk = new ForeignKey(ownerMapping, dba, referencedTable, true);
                fk.setForMetaData(fkmd);
                foreignKeys.add(fk);
            }
        }
        if (!isSerialisedValuePC()) {
            if (isEmbeddedValuePC()) {
                // Add any FKs for the fields of the (embedded) value
                EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
                for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
                    JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                    AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
                    if (ClassUtils.isReferenceType(embFmd.getType()) && embFieldMapping instanceof ReferenceMapping) {
                        // Field is a reference type, so add a FK to the table of the PC for each PC implementation
                        Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                        foreignKeys.addAll(fks);
                    } else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null && embFieldMapping.getNumberOfDatastoreMappings() > 0 && embFieldMapping instanceof PersistableMapping) {
                        // Field is for a PC class with the FK at this side, so add a FK to the table of this PC
                        ForeignKey fk = TableUtils.getForeignKeyForPCField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                        if (fk != null) {
                            foreignKeys.add(fk);
                        }
                    }
                }
            } else if (mmd.getMap().valueIsPersistent()) {
                // FK from join table to value table
                referencedTable = storeMgr.getDatastoreClass(mmd.getMap().getValueType(), clr);
                if (referencedTable != null) {
                    // Take <foreign-key> from <value>
                    ForeignKeyMetaData fkmd = null;
                    if (mmd.getValueMetaData() != null) {
                        fkmd = mmd.getValueMetaData().getForeignKeyMetaData();
                    }
                    if (fkmd != null || autoMode) {
                        ForeignKey fk = new ForeignKey(valueMapping, dba, referencedTable, true);
                        fk.setForMetaData(fkmd);
                        foreignKeys.add(fk);
                    }
                }
            }
        }
        if (!isSerialisedKeyPC()) {
            if (isEmbeddedKeyPC()) {
                // Add any FKs for the fields of the (embedded) key
                EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
                for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
                    JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
                    AbstractMemberMetaData embFmd = embFieldMapping.getMemberMetaData();
                    if (ClassUtils.isReferenceType(embFmd.getType()) && embFieldMapping instanceof ReferenceMapping) {
                        // Field is a reference type, so add a FK to the table of the PC for each PC implementation
                        Collection fks = TableUtils.getForeignKeysForReferenceField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                        foreignKeys.addAll(fks);
                    } else if (storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(embFmd.getType(), clr) != null && embFieldMapping.getNumberOfDatastoreMappings() > 0 && embFieldMapping instanceof PersistableMapping) {
                        // Field is for a PC class with the FK at this side, so add a FK to the table of this PC
                        ForeignKey fk = TableUtils.getForeignKeyForPCField(embFieldMapping, embFmd, autoMode, storeMgr, clr);
                        if (fk != null) {
                            foreignKeys.add(fk);
                        }
                    }
                }
            } else if (mmd.getMap().keyIsPersistent()) {
                // FK from join table to key table
                referencedTable = storeMgr.getDatastoreClass(mmd.getMap().getKeyType(), clr);
                if (referencedTable != null) {
                    // Take <foreign-key> from <key>
                    ForeignKeyMetaData fkmd = null;
                    if (mmd.getKeyMetaData() != null) {
                        fkmd = mmd.getKeyMetaData().getForeignKeyMetaData();
                    }
                    if (fkmd != null || autoMode) {
                        ForeignKey fk = new ForeignKey(keyMapping, dba, referencedTable, true);
                        fk.setForMetaData(fkmd);
                        foreignKeys.add(fk);
                    }
                }
            }
        }
    } catch (NoTableManagedException e) {
    // expected when no table exists
    }
    return foreignKeys;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ArrayList(java.util.ArrayList) EmbeddedValuePCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) Collection(java.util.Collection) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) NoTableManagedException(org.datanucleus.store.rdbms.exceptions.NoTableManagedException) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Example 8 with EmbeddedKeyPCMapping

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

the class AbstractMapStore 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 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.getNumberOfDatastoreMappings(); i++) {
        if (i > 0) {
            stmt.append(",");
        }
        stmt.append(fieldMapping.getDatastoreMapping(i).getColumn().getIdentifier().toString());
        stmt.append(" = ");
        stmt.append(fieldMapping.getDatastoreMapping(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.getNumberOfDatastoreMappings(); j++) {
                stmt.append(" AND ");
                stmt.append(m.getDatastoreMapping(j).getColumn().getIdentifier().toString());
                stmt.append(" = ");
                stmt.append(m.getDatastoreMapping(j).getUpdateInputParameter());
            }
        }
    }
    return stmt.toString();
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Example 9 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 op ObjectProvider 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(ObjectProvider op, 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.getNumberOfDatastoreMappings()];
        for (int j = 0; j < fieldMapping.getNumberOfDatastoreMappings(); j++) {
            jdbcParamPositions[j] = jdbcPosition++;
        }
        stmtMapping.addParameterOccurrence(jdbcParamPositions);
        mappingDefinition.addMappingForMember(absFieldNum, stmtMapping);
    }
    ObjectProvider elementOP = mapStore.getObjectProviderForEmbeddedPCObject(op, key, joinTable.getOwnerMemberMetaData(), ObjectProvider.EMBEDDED_MAP_KEY_PC);
    elementOP.provideFields(elementFieldNumbers, new ParameterSetter(elementOP, ps, mappingDefinition));
    return jdbcPosition;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) ObjectProvider(org.datanucleus.state.ObjectProvider) 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)

Aggregations

EmbeddedKeyPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)9 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)6 EmbeddedValuePCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedValuePCMapping)4 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)4 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)3 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)3 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 NoSuchElementException (java.util.NoSuchElementException)2 ExecutionContext (org.datanucleus.ExecutionContext)2 Transaction (org.datanucleus.Transaction)2 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)2 KeyMetaData (org.datanucleus.metadata.KeyMetaData)2 PrimaryKeyMetaData (org.datanucleus.metadata.PrimaryKeyMetaData)2 ValueMetaData (org.datanucleus.metadata.ValueMetaData)2 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)2 SQLController (org.datanucleus.store.rdbms.SQLController)2