Search in sources :

Example 6 with CandidateKey

use of org.datanucleus.store.rdbms.key.CandidateKey in project datanucleus-rdbms by datanucleus.

the class TableUtils method getCandidateKeyForField.

/**
 * Convenience method to return the candidate key (if any) for a field.
 * @param table The table
 * @param umd The Unique MetaData
 * @param fieldMapping Mapping for the field
 * @return The Candidate Key
 */
public static CandidateKey getCandidateKeyForField(Table table, UniqueMetaData umd, JavaTypeMapping fieldMapping) {
    CandidateKey ck = new CandidateKey(table, umd != null ? umd.getExtensions() : null);
    // Set the key name if required
    if (umd.getName() != null) {
        IdentifierFactory idFactory = table.getStoreManager().getIdentifierFactory();
        DatastoreIdentifier ckId = idFactory.newIdentifier(IdentifierType.CANDIDATE_KEY, umd.getName());
        ck.setName(ckId.toString());
    }
    // Field-level index so use all columns for the field
    int countFields = fieldMapping.getNumberOfDatastoreMappings();
    for (int j = 0; j < countFields; j++) {
        ck.addColumn(fieldMapping.getDatastoreMapping(j).getColumn());
    }
    return ck;
}
Also used : DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey)

Example 7 with CandidateKey

use of org.datanucleus.store.rdbms.key.CandidateKey in project datanucleus-rdbms by datanucleus.

the class ClassTable method getCandidateKeyForUniqueMetaData.

/**
 * Convenience method to convert a UniqueMetaData into a CandidateKey.
 * @param umd The Unique MetaData
 * @return The Candidate Key
 */
private CandidateKey getCandidateKeyForUniqueMetaData(UniqueMetaData umd) {
    CandidateKey ck = new CandidateKey(this, umd.getExtensions());
    // Set the key name if required
    if (umd.getName() != null) {
        ck.setName(umd.getName());
    }
    // a). Columns specified directly
    if (umd.getNumberOfColumns() > 0) {
        String[] columnNames = umd.getColumnNames();
        for (String columnName : columnNames) {
            DatastoreIdentifier colName = storeMgr.getIdentifierFactory().newColumnIdentifier(columnName);
            Column col = columnsByIdentifier.get(colName);
            if (col == null) {
                NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("058202", toString(), ck.getName(), columnName));
                break;
            }
            ck.addColumn(col);
        }
    } else // b). Columns specified using fields
    if (umd.getNumberOfMembers() > 0) {
        String[] memberNames = umd.getMemberNames();
        for (String memberName : memberNames) {
            // Find the metadata for the actual field with the same name as this "unique" field
            AbstractMemberMetaData realMmd = getMetaDataForMember(memberName);
            if (realMmd == null) {
                throw new NucleusUserException("Table " + this + " has unique key specified on member " + memberName + " but that member does not exist in the class that this table represents");
            }
            JavaTypeMapping memberMapping = memberMappingsMap.get(realMmd);
            int countFields = memberMapping.getNumberOfDatastoreMappings();
            for (int j = 0; j < countFields; j++) {
                ck.addColumn(memberMapping.getDatastoreMapping(j).getColumn());
            }
        }
    } else {
        // We can't have an index of no columns
        NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("058203", toString(), ck.getName()));
        return null;
    }
    return ck;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) MacroString(org.datanucleus.util.MacroString) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey)

Example 8 with CandidateKey

use of org.datanucleus.store.rdbms.key.CandidateKey in project datanucleus-rdbms by datanucleus.

the class ElementContainerTable 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 (elementMapping instanceof EmbeddedElementPCMapping) {
        // Add all candidate keys required by fields of the embedded element
        EmbeddedElementPCMapping embMapping = (EmbeddedElementPCMapping) elementMapping;
        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);
                }
            }
        }
    }
    if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getUniqueMetaData() != null) {
        // User has defined a unique key on the join table
        UniqueMetaData unimd = mmd.getJoinMetaData().getUniqueMetaData();
        if (unimd.getNumberOfColumns() > 0) {
            String[] columnNames = unimd.getColumnNames();
            CandidateKey uniKey = new CandidateKey(this, null);
            String unimdName = unimd.getName();
            if (!StringUtils.isWhitespace(unimdName)) {
                uniKey.setName(unimd.getName());
            }
            IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
            for (String columnName : columnNames) {
                Column col = getColumn(idFactory.newColumnIdentifier(columnName));
                if (col != null) {
                    uniKey.addColumn(col);
                } else {
                    throw new NucleusUserException("Unique key on join-table " + this + " has column " + columnName + " that is not found");
                }
            }
            candidateKeys.add(uniKey);
        }
    }
    return candidateKeys;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ArrayList(java.util.ArrayList) List(java.util.List) UniqueMetaData(org.datanucleus.metadata.UniqueMetaData) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) EmbeddedElementPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey)

Example 9 with CandidateKey

use of org.datanucleus.store.rdbms.key.CandidateKey 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)

Aggregations

CandidateKey (org.datanucleus.store.rdbms.key.CandidateKey)9 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)5 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)5 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)4 UniqueMetaData (org.datanucleus.metadata.UniqueMetaData)4 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)2 MacroString (org.datanucleus.util.MacroString)2 ClassDefinitionException (org.datanucleus.store.rdbms.exceptions.ClassDefinitionException)1 PrimaryKey (org.datanucleus.store.rdbms.key.PrimaryKey)1 EmbeddedElementPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedElementPCMapping)1 EmbeddedKeyPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)1 EmbeddedPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping)1