use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class ClassTable method runCallBacks.
/**
* Execute the callbacks for the classes that this table maps to.
* @param clr ClassLoader resolver
*/
private void runCallBacks(ClassLoaderResolver clr) {
// Run callbacks for all classes managed by this table
Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
while (cmdIter.hasNext()) {
AbstractClassMetaData managedCmd = cmdIter.next();
if (managingClassCurrent != null && managingClassCurrent.equals(managedCmd.getFullClassName())) {
// We can't run callbacks for this class since it is still being initialised. Mark callbacks to run after it completes
runCallbacksAfterManageClass = true;
break;
}
Collection processedCallbacks = callbacksAppliedForManagedClass.get(managedCmd.getFullClassName());
Collection c = (Collection) storeMgr.getSchemaCallbacks().get(managedCmd.getFullClassName());
if (c != null) {
if (processedCallbacks == null) {
processedCallbacks = new HashSet();
callbacksAppliedForManagedClass.put(managedCmd.getFullClassName(), processedCallbacks);
}
for (Iterator it = c.iterator(); it.hasNext(); ) {
AbstractMemberMetaData callbackMmd = (AbstractMemberMetaData) it.next();
if (processedCallbacks.contains(callbackMmd)) {
continue;
}
processedCallbacks.add(callbackMmd);
if (callbackMmd.getJoinMetaData() == null) {
// 1-N FK relationship
AbstractMemberMetaData ownerFmd = callbackMmd;
if (ownerFmd.getMappedBy() != null) {
// Bidirectional (element has a PC mapping to the owner)
// Check that the "mapped-by" field in the other class actually exists
AbstractMemberMetaData fmd = null;
if (ownerFmd.getMappedBy().indexOf('.') > 0) {
// TODO Can we just use getRelatedMemberMetaData always?
AbstractMemberMetaData[] relMmds = ownerFmd.getRelatedMemberMetaData(clr);
fmd = (relMmds != null && relMmds.length > 0) ? relMmds[0] : null;
} else {
fmd = managedCmd.getMetaDataForMember(ownerFmd.getMappedBy());
}
if (fmd == null) {
throw new NucleusUserException(Localiser.msg("057036", ownerFmd.getMappedBy(), managedCmd.getFullClassName(), ownerFmd.getFullFieldName()));
}
if (ownerFmd.getMap() != null && storeMgr.getBooleanProperty(RDBMSPropertyNames.PROPERTY_RDBMS_UNIQUE_CONSTRAINTS_MAP_INVERSE)) {
initializeFKMapUniqueConstraints(ownerFmd);
}
boolean duplicate = false;
JavaTypeMapping fkDiscrimMapping = null;
JavaTypeMapping orderMapping = null;
if (ownerFmd.hasExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_COLUMN)) {
// Collection has a relation discriminator so we need to share the FK. Check for the required discriminator
String colName = ownerFmd.getValueForExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_COLUMN);
if (colName == null) {
// No column defined so use a fallback name
colName = "RELATION_DISCRIM";
}
Set fkDiscrimEntries = getExternalFkDiscriminatorMappings().entrySet();
Iterator discrimMappingIter = fkDiscrimEntries.iterator();
while (discrimMappingIter.hasNext()) {
Map.Entry entry = (Map.Entry) discrimMappingIter.next();
JavaTypeMapping discrimMapping = (JavaTypeMapping) entry.getValue();
String discrimColName = (discrimMapping.getColumnMapping(0).getColumn().getColumnMetaData()).getName();
if (discrimColName.equalsIgnoreCase(colName)) {
duplicate = true;
fkDiscrimMapping = discrimMapping;
orderMapping = getExternalOrderMappings().get(entry.getKey());
break;
}
}
if (!duplicate) {
// Create the relation discriminator column since we dont have this discriminator
ColumnMetaData colmd = new ColumnMetaData();
colmd.setName(colName);
// Allow for elements not in any discriminated collection
colmd.setAllowsNull(Boolean.TRUE);
// Only support String discriminators currently
fkDiscrimMapping = storeMgr.getMappingManager().getMapping(String.class);
fkDiscrimMapping.setTable(this);
ColumnCreator.createIndexColumn(fkDiscrimMapping, storeMgr, clr, this, colmd, false);
}
if (fkDiscrimMapping != null) {
getExternalFkDiscriminatorMappings().put(ownerFmd, fkDiscrimMapping);
}
}
// Add the order mapping as necessary
addOrderMapping(ownerFmd, orderMapping, clr);
} else {
// Unidirectional (element knows nothing about the owner)
String ownerClassName = ownerFmd.getAbstractClassMetaData().getFullClassName();
JavaTypeMapping fkMapping = new PersistableMapping();
fkMapping.setTable(this);
fkMapping.initialize(storeMgr, ownerClassName);
JavaTypeMapping fkDiscrimMapping = null;
JavaTypeMapping orderMapping = null;
boolean duplicate = false;
try {
// Get the owner id mapping of the "1" end
DatastoreClass ownerTbl = storeMgr.getDatastoreClass(ownerClassName, clr);
if (ownerTbl == null) {
// Class doesn't have its own table (subclass-table) so find where it persists
AbstractClassMetaData[] ownerParentCmds = storeMgr.getClassesManagingTableForClass(ownerFmd.getAbstractClassMetaData(), clr);
if (ownerParentCmds.length > 1) {
throw new NucleusUserException("Relation (" + ownerFmd.getFullFieldName() + ") with multiple related tables (using subclass-table). Not supported");
}
ownerClassName = ownerParentCmds[0].getFullClassName();
ownerTbl = storeMgr.getDatastoreClass(ownerClassName, clr);
if (ownerTbl == null) {
throw new NucleusException("Failed to get owner table at other end of relation for field=" + ownerFmd.getFullFieldName());
}
}
JavaTypeMapping ownerIdMapping = ownerTbl.getIdMapping();
ColumnMetaDataContainer colmdContainer = null;
if (ownerFmd.hasCollection() || ownerFmd.hasArray()) {
// 1-N Collection/array
colmdContainer = ownerFmd.getElementMetaData();
} else if (ownerFmd.hasMap() && ownerFmd.getKeyMetaData() != null && ownerFmd.getKeyMetaData().getMappedBy() != null) {
// 1-N Map with key stored in the value
colmdContainer = ownerFmd.getValueMetaData();
} else if (ownerFmd.hasMap() && ownerFmd.getValueMetaData() != null && ownerFmd.getValueMetaData().getMappedBy() != null) {
// 1-N Map with value stored in the key
colmdContainer = ownerFmd.getKeyMetaData();
}
CorrespondentColumnsMapper correspondentColumnsMapping = new CorrespondentColumnsMapper(colmdContainer, this, ownerIdMapping, true);
int countIdFields = ownerIdMapping.getNumberOfColumnMappings();
for (int i = 0; i < countIdFields; i++) {
ColumnMapping refColumnMapping = ownerIdMapping.getColumnMapping(i);
JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(refColumnMapping.getJavaTypeMapping().getJavaType());
ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(refColumnMapping.getColumn().getIdentifier());
if (colmd == null) {
throw new NucleusUserException(Localiser.msg("057035", refColumnMapping.getColumn().getIdentifier(), toString())).setFatal();
}
DatastoreIdentifier identifier = null;
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
if (colmd.getName() == null || colmd.getName().length() < 1) {
// No user provided name so generate one
identifier = idFactory.newForeignKeyFieldIdentifier(ownerFmd, null, refColumnMapping.getColumn().getIdentifier(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mapping.getJavaType()), FieldRole.ROLE_OWNER);
} else {
// User-defined name
identifier = idFactory.newColumnIdentifier(colmd.getName());
}
Column refColumn = addColumn(mapping.getJavaType().getName(), identifier, mapping, colmd);
refColumnMapping.getColumn().copyConfigurationTo(refColumn);
if ((colmd.getAllowsNull() == null) || (colmd.getAllowsNull() != null && colmd.isAllowsNull())) {
// User either wants it nullable, or haven't specified anything, so make it nullable
refColumn.setNullable(true);
}
fkMapping.addColumnMapping(getStoreManager().getMappingManager().createColumnMapping(mapping, refColumn, refColumnMapping.getJavaTypeMapping().getJavaType().getName()));
((PersistableMapping) fkMapping).addJavaTypeMapping(mapping);
}
} catch (DuplicateColumnException dce) {
// If the user hasnt specified "relation-discriminator-column" here we dont allow the sharing of columns
if (!ownerFmd.hasExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_COLUMN)) {
throw dce;
}
// Find the FK using this column and use it instead of creating a new one since we're sharing
Iterator fkIter = getExternalFkMappings().entrySet().iterator();
fkMapping = null;
while (fkIter.hasNext()) {
Map.Entry entry = (Map.Entry) fkIter.next();
JavaTypeMapping existingFkMapping = (JavaTypeMapping) entry.getValue();
for (int j = 0; j < existingFkMapping.getNumberOfColumnMappings(); j++) {
if (existingFkMapping.getColumnMapping(j).getColumn().getIdentifier().toString().equals(dce.getConflictingColumn().getIdentifier().toString())) {
// The FK is shared (and so if it is a List we also share the index)
fkMapping = existingFkMapping;
fkDiscrimMapping = externalFkDiscriminatorMappings.get(entry.getKey());
orderMapping = getExternalOrderMappings().get(entry.getKey());
break;
}
}
}
if (fkMapping == null) {
// Should never happen since we know there is a col duplicating ours
throw dce;
}
duplicate = true;
}
if (!duplicate && ownerFmd.hasExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_COLUMN)) {
// Create the relation discriminator column
String colName = ownerFmd.getValueForExtension(MetaData.EXTENSION_MEMBER_RELATION_DISCRIM_COLUMN);
if (colName == null) {
// No column defined so use a fallback name
colName = "RELATION_DISCRIM";
}
ColumnMetaData colmd = new ColumnMetaData();
colmd.setName(colName);
// Allow for elements not in any discriminated collection
colmd.setAllowsNull(Boolean.TRUE);
// Only support String discriminators currently
fkDiscrimMapping = storeMgr.getMappingManager().getMapping(String.class);
fkDiscrimMapping.setTable(this);
ColumnCreator.createIndexColumn(fkDiscrimMapping, storeMgr, clr, this, colmd, false);
}
// Save the external FK
getExternalFkMappings().put(ownerFmd, fkMapping);
if (fkDiscrimMapping != null) {
getExternalFkDiscriminatorMappings().put(ownerFmd, fkDiscrimMapping);
}
// Add the order mapping as necessary
addOrderMapping(ownerFmd, orderMapping, clr);
}
}
}
}
}
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class ClassTable method getIndexForIndexMetaData.
/**
* Convenience method to convert an IndexMetaData into an Index.
* @param imd The Index MetaData
* @return The Index
*/
private Index getIndexForIndexMetaData(IndexMetaData imd) {
// Verify if a unique index is needed
boolean unique = imd.isUnique();
Index index = new Index(this, unique, imd.getExtensions());
// Set the index name if required
if (imd.getName() != null) {
index.setName(imd.getName());
}
// Class-level index so use its column definition
if (imd.getNumberOfColumns() > 0) {
// a). Columns specified directly
String[] columnNames = imd.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("058001", toString(), index.getName(), columnName));
break;
}
index.addColumn(col);
}
// Apply any user-provided ordering of the columns
String idxOrdering = imd.getValueForExtension(MetaData.EXTENSION_INDEX_COLUMN_ORDERING);
if (!StringUtils.isWhitespace(idxOrdering)) {
index.setColumnOrdering(idxOrdering);
}
} else if (imd.getNumberOfMembers() > 0) {
// b). Columns specified using members
String[] memberNames = imd.getMemberNames();
for (int i = 0; i < memberNames.length; i++) {
// Find the metadata for the actual field with the same name as this "index" field
AbstractMemberMetaData realMmd = getMetaDataForMember(memberNames[i]);
if (realMmd == null) {
throw new NucleusUserException("Table " + this + " has index specified on member " + memberNames[i] + " but that member does not exist in the class that this table represents");
}
JavaTypeMapping fieldMapping = memberMappingsMap.get(realMmd);
int countFields = fieldMapping.getNumberOfColumnMappings();
for (int j = 0; j < countFields; j++) {
index.addColumn(fieldMapping.getColumnMapping(j).getColumn());
}
// Apply any user-provided ordering of the columns
String idxOrdering = imd.getValueForExtension(MetaData.EXTENSION_INDEX_COLUMN_ORDERING);
if (!StringUtils.isWhitespace(idxOrdering)) {
index.setColumnOrdering(idxOrdering);
}
}
} else {
// We can't have an index of no columns
NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("058002", toString(), index.getName()));
return null;
}
return index;
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class ClassTable method getForeignKeyForForeignKeyMetaData.
/**
* Convenience method to create a FK for the specified ForeignKeyMetaData.
* Used for foreign-keys specified at <class> level.
* @param fkmd ForeignKey MetaData
* @return The ForeignKey
*/
private ForeignKey getForeignKeyForForeignKeyMetaData(ForeignKeyMetaData fkmd) {
if (fkmd == null) {
return null;
}
// Create the ForeignKey base details
ForeignKey fk = new ForeignKey(dba, fkmd.isDeferred());
fk.setForMetaData(fkmd);
if (fkmd.getFkDefinitionApplies()) {
// User-defined FK definition should be used
return fk;
}
// Find the target of the foreign-key
AbstractClassMetaData acmd = cmd;
if (fkmd.getTable() == null) {
// Can't create a FK if we don't know where it goes to
NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("058105", acmd.getFullClassName()));
return null;
}
DatastoreIdentifier tableId = storeMgr.getIdentifierFactory().newTableIdentifier(fkmd.getTable());
ClassTable refTable = (ClassTable) storeMgr.getDatastoreClass(tableId);
if (refTable == null) {
// TODO Go to the datastore and query for this table to get the columns of the PK
NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("058106", acmd.getFullClassName(), fkmd.getTable()));
return null;
}
PrimaryKey pk = refTable.getPrimaryKey();
List targetCols = pk.getColumns();
// Generate the columns for the source of the foreign-key
List<Column> sourceCols = new ArrayList<>();
ColumnMetaData[] colmds = fkmd.getColumnMetaData();
String[] memberNames = fkmd.getMemberNames();
if (colmds != null && colmds.length > 0) {
// FK specified via <column>
for (int i = 0; i < colmds.length; i++) {
// Find the column and add to the source columns for the FK
DatastoreIdentifier colId = storeMgr.getIdentifierFactory().newColumnIdentifier(colmds[i].getName());
Column sourceCol = columnsByIdentifier.get(colId);
if (sourceCol == null) {
NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("058107", acmd.getFullClassName(), fkmd.getTable(), colmds[i].getName(), toString()));
return null;
}
sourceCols.add(sourceCol);
}
} else if (memberNames != null && memberNames.length > 0) {
// FK specified via <field>
for (int i = 0; i < memberNames.length; i++) {
// Find the metadata for the actual field with the same name as this "foreign-key" field
// and add all columns to the source columns for the FK
AbstractMemberMetaData realMmd = getMetaDataForMember(memberNames[i]);
if (realMmd == null) {
throw new NucleusUserException("Table " + this + " has foreign-key specified on member " + memberNames[i] + " but that member does not exist in the class that this table represents");
}
JavaTypeMapping fieldMapping = memberMappingsMap.get(realMmd);
int countCols = fieldMapping.getNumberOfColumnMappings();
for (int j = 0; j < countCols; j++) {
// Add each column of this field to the FK definition
sourceCols.add(fieldMapping.getColumnMapping(j).getColumn());
}
}
}
if (sourceCols.size() != targetCols.size()) {
// Different number of cols in this table and target table
NucleusLogger.DATASTORE_SCHEMA.warn(Localiser.msg("058108", acmd.getFullClassName(), fkmd.getTable(), "" + sourceCols.size(), "" + targetCols.size()));
}
// Add all column mappings to the ForeignKey
if (sourceCols.size() > 0) {
for (int i = 0; i < sourceCols.size(); i++) {
Column source = sourceCols.get(i);
String targetColName = (colmds != null && colmds[i] != null) ? colmds[i].getTarget() : null;
// Default to matching via the natural order
Column target = (Column) targetCols.get(i);
if (targetColName != null) {
// User has specified the target column for this col so try it in our target list
for (int j = 0; j < targetCols.size(); j++) {
Column targetCol = (Column) targetCols.get(j);
if (targetCol.getIdentifier().getName().equalsIgnoreCase(targetColName)) {
// Found the required column
target = targetCol;
break;
}
}
}
fk.addColumn(source, target);
}
}
return fk;
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class ClassTable method manageUnmappedColumns.
/**
* Adds on management of the columns in the defined MetaData that are "unmapped" (have no field associated).
* @param theCmd ClassMetaData for the class to be managed
* @param clr The ClassLoaderResolver
*/
private void manageUnmappedColumns(AbstractClassMetaData theCmd, ClassLoaderResolver clr) {
List cols = theCmd.getUnmappedColumns();
if (cols != null && cols.size() > 0) {
Iterator colsIter = cols.iterator();
while (colsIter.hasNext()) {
ColumnMetaData colmd = (ColumnMetaData) colsIter.next();
// Create a column with the specified name and jdbc-type
if (colmd.getJdbcType() == JdbcType.VARCHAR && colmd.getLength() == null) {
colmd.setLength(storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_RDBMS_STRING_DEFAULT_LENGTH));
}
IdentifierFactory idFactory = getStoreManager().getIdentifierFactory();
DatastoreIdentifier colIdentifier = idFactory.newIdentifier(IdentifierType.COLUMN, colmd.getName());
Column col = addColumn(null, colIdentifier, null, colmd);
SQLTypeInfo sqlTypeInfo = storeMgr.getSQLTypeInfoForJDBCType(dba.getJDBCTypeForName(colmd.getJdbcTypeName()));
col.setTypeInfo(sqlTypeInfo);
if (unmappedColumns == null) {
unmappedColumns = new HashSet();
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057011", col.toString(), colmd.getJdbcType()));
}
unmappedColumns.add(col);
}
}
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier 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.getNumberOfColumnMappings();
for (int j = 0; j < countFields; j++) {
ck.addColumn(memberMapping.getColumnMapping(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;
}
Aggregations