use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method createColumn.
/**
* Method to create a column for a Java type mapping.
* This is NOT used for persistable mappings - see method below.
* @param mapping Java type mapping for the field
* @param javaType The type of field being stored in this column
* @param datastoreFieldIndex Index of the datastore field to use
* @return The datastore field
*/
public Column createColumn(JavaTypeMapping mapping, String javaType, int datastoreFieldIndex) {
AbstractMemberMetaData mmd = mapping.getMemberMetaData();
FieldRole roleForField = mapping.getRoleForMember();
Table tbl = mapping.getTable();
// Take the column MetaData from the component that this mappings role relates to
ColumnMetaData colmd = null;
ColumnMetaDataContainer columnContainer = mmd;
if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT || roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
columnContainer = mmd.getElementMetaData();
} else if (roleForField == FieldRole.ROLE_MAP_KEY) {
columnContainer = mmd.getKeyMetaData();
} else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
columnContainer = mmd.getValueMetaData();
}
Column col;
ColumnMetaData[] colmds;
if (columnContainer != null && columnContainer.getColumnMetaData().length > datastoreFieldIndex) {
colmd = columnContainer.getColumnMetaData()[datastoreFieldIndex];
colmds = columnContainer.getColumnMetaData();
} else {
// If column specified add one (use any column name specified on field element)
colmd = new ColumnMetaData();
if (mmd.getColumnMetaData() != null && mmd.getColumnMetaData().length > datastoreFieldIndex) {
colmd.setName(mmd.getColumnMetaData()[datastoreFieldIndex].getName());
}
if (columnContainer != null) {
columnContainer.addColumn(colmd);
colmds = columnContainer.getColumnMetaData();
} else {
colmds = new ColumnMetaData[1];
colmds[0] = colmd;
}
}
// Generate the column identifier
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
DatastoreIdentifier identifier = null;
if (colmd.getName() == null) {
// No name specified, so generate the identifier from the field name
if (roleForField == FieldRole.ROLE_COLLECTION_ELEMENT) {
// Join table collection element
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_COLLECTION_ELEMENT);
} else if (roleForField == FieldRole.ROLE_ARRAY_ELEMENT) {
// Join table array element
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_ARRAY_ELEMENT);
} else if (roleForField == FieldRole.ROLE_MAP_KEY) {
// Join table map key
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_KEY);
} else if (roleForField == FieldRole.ROLE_MAP_VALUE) {
// Join table map value
identifier = idFactory.newJoinTableFieldIdentifier(mmd, null, null, true, FieldRole.ROLE_MAP_VALUE);
} else {
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName());
int i = 0;
while (tbl.hasColumn(identifier)) {
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName() + "_" + i);
i++;
}
}
colmd.setName(identifier.getName());
} else {
// User has specified a name, so try to keep this unmodified
identifier = idFactory.newColumnIdentifier(colmds[datastoreFieldIndex].getName(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), null, true);
}
// Create the column
col = tbl.addColumn(javaType, identifier, mapping, colmd);
if (mmd.isPrimaryKey()) {
col.setPrimaryKey();
}
if (!(mmd.getParent() instanceof AbstractClassMetaData)) {
// Embedded so can't be datastore-attributed
} else {
/*if (!mmd.getClassName(true).equals(mmd.getAbstractClassMetaData().getFullClassName()))
{
if (storeMgr.isStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass)
{
if ((mmd.isPrimaryKey() && ((DatastoreClass)tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey())
{
NucleusLogger.GENERAL.info(">> Column addition " + mmd.getFullFieldName() + " IGNORING use of IDENTITY since override of base metadata! See RDBMSMappingManager");
}
}
// Overriding member, so ignore TODO This can be incorrect in many cases
}
else
{*/
if (storeMgr.isValueGenerationStrategyDatastoreAttributed(mmd.getAbstractClassMetaData(), mmd.getAbsoluteFieldNumber()) && tbl instanceof DatastoreClass) {
if ((mmd.isPrimaryKey() && ((DatastoreClass) tbl).isBaseDatastoreClass()) || !mmd.isPrimaryKey()) {
// Increment any PK field if we are in base class, and increment any other field
col.setIdentity(true);
}
}
/*}*/
}
if (mmd.getValueForExtension("select-function") != null) {
col.setWrapperFunction(mmd.getValueForExtension("select-function"), Column.WRAPPER_FUNCTION_SELECT);
}
if (mmd.getValueForExtension("insert-function") != null) {
col.setWrapperFunction(mmd.getValueForExtension("insert-function"), Column.WRAPPER_FUNCTION_INSERT);
}
if (mmd.getValueForExtension("update-function") != null) {
col.setWrapperFunction(mmd.getValueForExtension("update-function"), Column.WRAPPER_FUNCTION_UPDATE);
}
setColumnNullability(mmd, colmd, col);
if (mmd.getNullValue() == NullValue.DEFAULT) {
// Users default should be applied if a null is to be inserted
col.setDefaultable(colmd.getDefaultValue());
}
return col;
}
use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project tests by datanucleus.
the class IdentifierFactoryTest method testJPOX.
/**
* Verify that putting "datanucleus.identifierFactory=jpox" results in JPOXIdentifierFactory being used
*/
public void testJPOX() {
Properties props = new Properties();
props.put("datanucleus.identifierFactory", "jpox");
PersistenceManagerFactory pmf2 = getPMF(1, props);
PersistenceManager pm = pmf2.getPersistenceManager();
ExecutionContext ec = ((JDOPersistenceManager) pm).getExecutionContext();
IdentifierFactory identifierFactory = ((RDBMSStoreManager) ec.getStoreManager()).getIdentifierFactory();
assertTrue(identifierFactory instanceof JPOXIdentifierFactory);
}
use of org.datanucleus.store.rdbms.identifier.IdentifierFactory in project tests by datanucleus.
the class IdentifierFactoryTest method testJPA.
/**
* Tests for JPAIdentifierFactory.
*/
public void testJPA() {
RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
IdentifierFactory idFactory = null;
ClassLoaderResolver clr = new ClassLoaderResolverImpl();
try {
JDOPersistenceManagerFactory thePMF = (JDOPersistenceManagerFactory) pmf;
Map props = new HashMap();
Configuration conf = getConfigurationForPMF(thePMF);
if (conf.getStringProperty("datanucleus.mapping.Catalog") != null) {
props.put("DefaultCatalog", conf.getStringProperty("datanucleus.mapping.Catalog"));
}
if (conf.getStringProperty("datanucleus.mapping.Schema") != null) {
props.put("DefaultSchema", conf.getStringProperty("datanucleus.mapping.Schema"));
}
if (conf.getStringProperty("datanucleus.identifier.case") != null) {
props.put("RequiredCase", conf.getStringProperty("datanucleus.identifier.case"));
} else {
props.put("RequiredCase", srm.getDefaultIdentifierCase());
}
if (conf.getStringProperty("datanucleus.identifier.wordSeparator") != null) {
props.put("WordSeparator", conf.getStringProperty("datanucleus.identifier.wordSeparator"));
}
if (conf.getStringProperty("datanucleus.identifier.tablePrefix") != null) {
props.put("TablePrefix", conf.getStringProperty("datanucleus.identifier.tablePrefix"));
}
if (conf.getStringProperty("datanucleus.identifier.tableSuffix") != null) {
props.put("TableSuffix", conf.getStringProperty("datanucleus.identifier.tableSuffix"));
}
Class cls = Class.forName("org.datanucleus.store.rdbms.identifier.JPAIdentifierFactory");
Class[] argTypes = new Class[] { DatastoreAdapter.class, ClassLoaderResolver.class, Map.class };
Object[] args = new Object[] { srm.getDatastoreAdapter(), srm.getNucleusContext().getClassLoaderResolver(null), props };
idFactory = (IdentifierFactory) ClassUtils.newInstance(cls, argTypes, args);
} catch (Exception e) {
fail("Error creating JPAIdentifierFactory : " + e.getMessage());
}
// 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_EMPLOYEE".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("MY_COLUMN_NAME");
assertTrue("newColumnIdentifier(String) has returned an incorrect name when should have used the supplied name " + id.getName(), "MY_COLUMN_NAME".equalsIgnoreCase(id.getName()));
// c). Discriminator column identifier
id = idFactory.newDiscriminatorFieldIdentifier();
assertTrue("newDiscriminatorFieldIdentifier() has returned an incorrect name : " + id.getName(), "DTYPE".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(), "SUBORDINATES_ORDER".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_MANAGER_ID".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(), "SUBORDINATES_EMPLOYEE_ID".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, true, FieldRole.ROLE_OWNER);
assertTrue("newForeignKeyFieldIdentifier(OWNER) has returned an incorrect generated name " + id.getName(), "MANAGER_MANAGER_ID".equalsIgnoreCase(id.getName()));
// Primary key identifiers
// Index identifiers
// Foreign key identifiers
// Candidate key identifiers
// Sequence identifiers
}
Aggregations