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;
}
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;
}
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;
}
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;
}
Aggregations