use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class MapTable method initialize.
/**
* Method to initialise the table definition.
* @param clr The ClassLoaderResolver
*/
@Override
public void initialize(ClassLoaderResolver clr) {
assertIsUninitialized();
MapMetaData mapmd = mmd.getMap();
if (mapmd == null) {
throw new NucleusUserException(Localiser.msg("057017", mmd));
}
PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
boolean pkColsSpecified = (pkmd != null && pkmd.getColumnMetaData() != null);
boolean pkRequired = requiresPrimaryKey();
// Add owner mapping
ColumnMetaData[] ownerColmd = null;
if (mmd.getJoinMetaData() != null && mmd.getJoinMetaData().getColumnMetaData() != null && mmd.getJoinMetaData().getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
// When specified at this side they use the <join> tag
ownerColmd = mmd.getJoinMetaData().getColumnMetaData();
}
ownerMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(ownerType), mmd, ownerColmd, storeMgr, this, pkRequired, false, FieldRole.ROLE_OWNER, clr, null);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[OWNER]", ownerMapping);
}
String keyValueFieldName = (mmd.getKeyMetaData() != null ? mmd.getKeyMetaData().getMappedBy() : null);
String valueKeyFieldName = (mmd.getValueMetaData() != null ? mmd.getValueMetaData().getMappedBy() : null);
// Add key mapping
boolean keyPC = (mmd.hasMap() && mmd.getMap().keyIsPersistent());
Class keyCls = clr.classForName(mapmd.getKeyType());
if (keyValueFieldName != null && isEmbeddedValuePC()) {
// Added in value code
} else if (isSerialisedKey() || isEmbeddedKeyPC() || (isEmbeddedKey() && !keyPC) || ClassUtils.isReferenceType(keyCls)) {
// Key = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
keyMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_MAP_KEY);
if (Boolean.TRUE.equals(mmd.getContainer().allowNulls())) {
// Make all key col(s) nullable so we can store null elements
for (int i = 0; i < keyMapping.getNumberOfColumnMappings(); i++) {
Column elementCol = keyMapping.getColumnMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[KEY]", keyMapping);
}
if (valueKeyFieldName != null && isEmbeddedKeyPC()) {
// Key (PC) is embedded and value is a field of the key
EmbeddedKeyPCMapping embMapping = (EmbeddedKeyPCMapping) keyMapping;
valueMapping = embMapping.getJavaTypeMapping(valueKeyFieldName);
}
} else {
// Key = PC
ColumnMetaData[] keyColmd = null;
KeyMetaData keymd = mmd.getKeyMetaData();
if (keymd != null && keymd.getColumnMetaData() != null && keymd.getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
keyColmd = keymd.getColumnMetaData();
}
keyMapping = ColumnCreator.createColumnsForJoinTables(keyCls, mmd, keyColmd, storeMgr, this, false, false, FieldRole.ROLE_MAP_KEY, clr, null);
if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
// Make all key col(s) nullable so we can store null elements
for (int i = 0; i < keyMapping.getNumberOfColumnMappings(); i++) {
Column elementCol = keyMapping.getColumnMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[KEY]", keyMapping);
}
}
// Add value mapping
boolean valuePC = (mmd.hasMap() && mmd.getMap().valueIsPersistent());
Class valueCls = clr.classForName(mapmd.getValueType());
if (valueKeyFieldName != null && isEmbeddedKeyPC()) {
// Added in key code
} else if (isSerialisedValue() || isEmbeddedValuePC() || (isEmbeddedValue() && !valuePC) || ClassUtils.isReferenceType(valueCls)) {
// Value = PC(embedded), PC(serialised), Non-PC(serialised), Non-PC(embedded), Reference
valueMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_MAP_VALUE);
if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
// Make all value col(s) nullable so we can store null elements
for (int i = 0; i < valueMapping.getNumberOfColumnMappings(); i++) {
Column elementCol = valueMapping.getColumnMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[VALUE]", valueMapping);
}
if (keyValueFieldName != null && isEmbeddedValuePC()) {
// Value (PC) is embedded and key is a field of the value
EmbeddedValuePCMapping embMapping = (EmbeddedValuePCMapping) valueMapping;
keyMapping = embMapping.getJavaTypeMapping(keyValueFieldName);
}
} else {
// Value = PC
ColumnMetaData[] valueColmd = null;
ValueMetaData valuemd = mmd.getValueMetaData();
if (valuemd != null && valuemd.getColumnMetaData() != null && valuemd.getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
valueColmd = valuemd.getColumnMetaData();
}
valueMapping = ColumnCreator.createColumnsForJoinTables(clr.classForName(mapmd.getValueType()), mmd, valueColmd, storeMgr, this, false, true, FieldRole.ROLE_MAP_VALUE, clr, null);
if (mmd.getContainer().allowNulls() == Boolean.TRUE) {
// Make all value col(s) nullable so we can store null elements
for (int i = 0; i < valueMapping.getNumberOfColumnMappings(); i++) {
Column elementCol = valueMapping.getColumnMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[VALUE]", valueMapping);
}
}
// Add order mapping if required
boolean orderRequired = false;
if (mmd.getOrderMetaData() != null) {
// User requested order column so add one
orderRequired = true;
} else if (requiresPrimaryKey() && !pkColsSpecified) {
// PK is required so maybe need to add an index to form the PK
if (isEmbeddedKeyPC()) {
if (mmd.hasExtension("surrogate-pk-column") && mmd.getValueForExtension("surrogate-pk-column").equalsIgnoreCase("true")) {
// Allow user to request surrogate pk column be added (for use with JPA)
orderRequired = true;
} else if (storeMgr.getApiAdapter().getName().equalsIgnoreCase("JDO") && mmd.getMap().getKeyClassMetaData(clr).getIdentityType() != IdentityType.APPLICATION) {
// Embedded key PC with datastore id so we need an index to form the PK TODO It is arguable that we can just use all embedded key fields as part of PK here always
orderRequired = true;
}
} else if (isSerialisedKey()) {
// Serialised key, so need an index to form the PK
orderRequired = true;
} else if (keyMapping instanceof ReferenceMapping) {
// ReferenceMapping, so have order if more than 1 implementation
ReferenceMapping refMapping = (ReferenceMapping) keyMapping;
if (refMapping.getJavaTypeMapping().length > 1) {
orderRequired = true;
}
} else if (!(keyMapping instanceof PersistableMapping)) {
// Non-PC, so depends if the key column can be used as part of a PK
// TODO This assumes the keyMapping has a single column but what if it is Color with 4 cols?
Column elementCol = keyMapping.getColumnMapping(0).getColumn();
if (!storeMgr.getDatastoreAdapter().isValidPrimaryKeyType(elementCol.getJdbcType())) {
// Not possible to use this Non-PC type as part of the PK
orderRequired = true;
}
}
}
if (orderRequired) {
// Order/Adapter (index) column is required (integer based)
ColumnMetaData orderColmd = null;
if (mmd.getOrderMetaData() != null && mmd.getOrderMetaData().getColumnMetaData() != null && mmd.getOrderMetaData().getColumnMetaData().length > 0) {
// Specified "order" column info
orderColmd = mmd.getOrderMetaData().getColumnMetaData()[0];
if (orderColmd.getName() == null) {
// No column name so use default
orderColmd = new ColumnMetaData(orderColmd);
DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
orderColmd.setName(id.getName());
}
} else {
// No column name so use default
DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
orderColmd = new ColumnMetaData();
orderColmd.setName(id.getName());
}
// JDO2 spec [18.5] order column is assumed to be "int"
orderMapping = storeMgr.getMappingManager().getMapping(int.class);
ColumnCreator.createIndexColumn(orderMapping, storeMgr, clr, this, orderColmd, pkRequired && !pkColsSpecified);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ORDER]", orderMapping);
}
}
// Define primary key of the join table (if any)
if (pkRequired) {
if (pkColsSpecified) {
// Apply the users PK specification
applyUserPrimaryKeySpecification(pkmd);
} else {
// Define PK using internal rules
if (orderRequired) {
// Order column specified so owner+order are the PK
orderMapping.getColumnMapping(0).getColumn().setPrimaryKey();
} else {
// No order column specified so owner+key are the PK
for (int i = 0; i < keyMapping.getNumberOfColumnMappings(); i++) {
keyMapping.getColumnMapping(i).getColumn().setPrimaryKey();
}
}
}
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
}
storeMgr.registerTableInitialized(this);
state = TABLE_STATE_INITIALIZED;
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class ColumnCreator method createIndexColumn.
/**
* Convenience method to add the column for an index mapping.
* @param mapping The mapping
* @param storeMgr Manager for the store
* @param clr ClassLoaderResolver
* @param table Table where we create the column
* @param colmd The column MetaData
* @param pk Whether this column is (part of) the PK.
* @return The added column
*/
public static Column createIndexColumn(JavaTypeMapping mapping, RDBMSStoreManager storeMgr, ClassLoaderResolver clr, Table table, ColumnMetaData colmd, boolean pk) {
DatastoreIdentifier identifier = null;
if (colmd != null && colmd.getName() != null) {
// User defined name
identifier = storeMgr.getIdentifierFactory().newColumnIdentifier(colmd.getName());
} else {
// No name so generate one
identifier = storeMgr.getIdentifierFactory().newAdapterIndexFieldIdentifier();
}
Column column = table.addColumn(mapping.getType(), identifier, mapping, colmd);
storeMgr.getMappingManager().createColumnMapping(mapping, column, mapping.getJavaType().getName());
if (pk) {
column.setPrimaryKey();
}
return column;
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class AbstractTable method addColumnInternal.
// -------------------------------- Internal Implementation ---------------------------
/**
* Utility method to add a column to the internal representation
* @param col The column
*/
protected synchronized void addColumnInternal(Column col) {
DatastoreIdentifier colName = col.getIdentifier();
columns.add(col);
columnsByIdentifier.put(colName, col);
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project datanucleus-rdbms by datanucleus.
the class ArrayTable method initialize.
/**
* Method to initialise the table definition.
* @param clr The ClassLoaderResolver
*/
@Override
public void initialize(ClassLoaderResolver clr) {
super.initialize(clr);
// Add field(s) for element
boolean elementPC = (mmd.hasArray() && mmd.getArray().elementIsPersistent());
if (isSerialisedElementPC() || isEmbeddedElementPC() || (isEmbeddedElement() && !elementPC) || ClassUtils.isReferenceType(mmd.getType().getComponentType())) {
// Element = PC(embedded), PC(serialised), Non-PC(embedded), Reference
elementMapping = storeMgr.getMappingManager().getMapping(this, mmd, clr, FieldRole.ROLE_ARRAY_ELEMENT);
if (Boolean.TRUE.equals(mmd.getContainer().allowNulls())) {
// Make all element col(s) nullable so we can store null elements
for (int i = 0; i < elementMapping.getNumberOfColumnMappings(); i++) {
Column elementCol = elementMapping.getColumnMapping(i).getColumn();
elementCol.setNullable(true);
}
}
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
}
} else {
// Element = PC
ColumnMetaData[] elemColmd = null;
ElementMetaData elemmd = mmd.getElementMetaData();
if (elemmd != null && elemmd.getColumnMetaData() != null && elemmd.getColumnMetaData().length > 0) {
// Column mappings defined at this side (1-N, M-N)
elemColmd = elemmd.getColumnMetaData();
}
elementMapping = ColumnCreator.createColumnsForJoinTables(mmd.getType().getComponentType(), mmd, elemColmd, storeMgr, this, false, true, FieldRole.ROLE_ARRAY_ELEMENT, clr, null);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ELEMENT]", elementMapping);
}
}
boolean pkRequired = requiresPrimaryKey();
PrimaryKeyMetaData pkmd = (mmd.getJoinMetaData() != null ? mmd.getJoinMetaData().getPrimaryKeyMetaData() : null);
boolean pkColsSpecified = (pkmd != null ? pkmd.getColumnMetaData() != null : false);
// Add order mapping
ColumnMetaData colmd = null;
if (mmd.getOrderMetaData() != null && mmd.getOrderMetaData().getColumnMetaData() != null && mmd.getOrderMetaData().getColumnMetaData().length > 0) {
// Specified "order" column info
colmd = mmd.getOrderMetaData().getColumnMetaData()[0];
} else {
// No column name so use default
DatastoreIdentifier id = storeMgr.getIdentifierFactory().newIndexFieldIdentifier(mmd);
colmd = new ColumnMetaData();
colmd.setName(id.getName());
}
// JDO2 spec [18.5] order column is assumed to be "int"
orderMapping = storeMgr.getMappingManager().getMapping(int.class);
ColumnCreator.createIndexColumn(orderMapping, storeMgr, clr, this, colmd, pkRequired && !pkColsSpecified);
if (NucleusLogger.DATASTORE.isDebugEnabled()) {
logMapping(mmd.getFullFieldName() + ".[ORDER]", orderMapping);
}
// Define primary key of the join table (if any)
if (pkRequired) {
if (pkColsSpecified) {
// Apply the users PK specification
applyUserPrimaryKeySpecification(pkmd);
} else {
// Define PK
for (int i = 0; i < ownerMapping.getNumberOfColumnMappings(); i++) {
ownerMapping.getColumnMapping(i).getColumn().setPrimaryKey();
}
}
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
}
storeMgr.registerTableInitialized(this);
state = TABLE_STATE_INITIALIZED;
}
use of org.datanucleus.store.rdbms.identifier.DatastoreIdentifier in project tests by datanucleus.
the class IdentifierFactoryTest method testDataNucleus2.
/**
* Tests for DNIdentifierFactory
*/
public void testDataNucleus2() {
RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
IdentifierFactory idFactory = srm.getIdentifierFactory();
ClassLoaderResolver clr = new ClassLoaderResolverImpl();
// Table identifiers
// a). generated name shorter than max length
DatastoreIdentifier id = idFactory.newIdentifier(IdentifierType.TABLE, "MyClass");
assertTrue("newIdentifier(TABLE, String) has generated an incorrect name : " + id.getName(), "MYCLASS".equalsIgnoreCase(id.getName()));
// b). specified name shorter than max length
id = idFactory.newTableIdentifier("MY_TABLE_NAME");
assertTrue("newDatastoreContainerIdentifier(String) has returned an incorrect name when should have used the supplied name " + id.getName(), "MY_TABLE_NAME".equalsIgnoreCase(id.getName()));
// c). name specified via ClassMetaData
AbstractClassMetaData managerCMD = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass("org.jpox.samples.models.company.Manager", clr);
id = idFactory.newTableIdentifier(managerCMD);
assertTrue("newDatastoreContainerIdentifier(clr, ClassMetaData) has returned an incorrect generated name " + id.getName(), "MANAGER".equalsIgnoreCase(id.getName()));
// d). name specified via ClassMetaData
AbstractMemberMetaData fmd = managerCMD.getMetaDataForMember("subordinates");
id = idFactory.newTableIdentifier(fmd);
assertTrue("newDatastoreContainerIdentifier(clr, AbstractMemberMetaData) has returned an incorrect generated name " + id.getName(), "MANAGER_SUBORDINATES".equalsIgnoreCase(id.getName()));
// Column identifiers
// a). generated name shorter than max length
id = idFactory.newIdentifier(IdentifierType.COLUMN, "myField");
assertTrue("newIdentifier(COLUMN, String) has generated an incorrect name : " + id.getName(), "MYFIELD".equalsIgnoreCase(id.getName()));
// b). specified name shorter than max length
id = idFactory.newColumnIdentifier("MYCOLUMNNAME");
assertTrue("newColumnIdentifier(String) has returned an incorrect name when should have used the supplied name " + id.getName(), "MYCOLUMNNAME".equalsIgnoreCase(id.getName()));
// c). Discriminator column identifier
id = idFactory.newDiscriminatorFieldIdentifier();
assertTrue("newDiscriminatorFieldIdentifier() has returned an incorrect name : " + id.getName(), "DISCRIMINATOR".equalsIgnoreCase(id.getName()));
// d). Version column identifier
id = idFactory.newVersionFieldIdentifier();
assertTrue("newVersionFieldIdentifier() has returned an incorrect name : " + id.getName(), "VERSION".equalsIgnoreCase(id.getName()));
// e). Index (ordering) column identifier
id = idFactory.newIndexFieldIdentifier(fmd);
assertTrue("newIndexFieldIdentifier() has returned an incorrect name : " + id.getName(), "IDX".equalsIgnoreCase(id.getName()));
// f). Adapter Index column identifier
id = idFactory.newAdapterIndexFieldIdentifier();
assertTrue("newAdapterIndexFieldIdentifier() has returned an incorrect name : " + id.getName(), "IDX".equalsIgnoreCase(id.getName()));
AbstractMemberMetaData[] relatedMmds = fmd.getRelatedMemberMetaData(clr);
// g). join table owner column identifier (1-N bi JoinTable)
DatastoreIdentifier destId = idFactory.newColumnIdentifier("MANAGER_ID");
id = idFactory.newJoinTableFieldIdentifier(fmd, relatedMmds != null ? relatedMmds[0] : null, destId, false, FieldRole.ROLE_OWNER);
assertTrue("newJoinTableFieldIdentifier(OWNER) has returned an incorrect generated name " + id.getName(), "MANAGER_ID_OID".equalsIgnoreCase(id.getName()));
// h). join table element column identifier (1-N bi JoinTable)
destId = idFactory.newColumnIdentifier("EMPLOYEE_ID");
id = idFactory.newJoinTableFieldIdentifier(fmd, relatedMmds != null ? relatedMmds[0] : null, destId, false, FieldRole.ROLE_COLLECTION_ELEMENT);
assertTrue("newJoinTableFieldIdentifier(ELEMENT) has returned an incorrect generated name " + id.getName(), "EMPLOYEE_ID_EID".equalsIgnoreCase(id.getName()));
// i). FK owner column identifier (1-N bi FK)
AbstractMemberMetaData deptsFMD = managerCMD.getMetaDataForMember("departments");
AbstractMemberMetaData[] deptsRelatedMmds = deptsFMD.getRelatedMemberMetaData(clr);
destId = idFactory.newColumnIdentifier("MANAGER_ID");
id = idFactory.newForeignKeyFieldIdentifier(deptsFMD, deptsRelatedMmds != null ? deptsRelatedMmds[0] : null, destId, false, FieldRole.ROLE_OWNER);
assertTrue("newForeignKeyFieldIdentifier(OWNER) has returned an incorrect generated name " + id.getName(), "MANAGER_MANAGER_ID_OID".equalsIgnoreCase(id.getName()));
// Primary key identifiers
// Index identifiers
// Foreign key identifiers
// Candidate key identifiers
// Sequence identifiers
}
Aggregations