use of org.datanucleus.store.rdbms.table.DatastoreClass in project datanucleus-rdbms by datanucleus.
the class ExpressionUtils method populatePrimaryKeyMappingsValuesForPCMapping.
/**
* Convenience method to populate PK mappings/values allowing for recursion where a PK field is itself
* a PCMapping, that itself has PK mappings, which in turn may include PCMappings.
* The pkMappings/pkFieldValues arrays are already created and we populate from "position".
* @param pkMappings The array of pk mappings to be populated
* @param pkFieldValues The array of pk field values to be populated
* @param position The current position needing populating
* @param pcMapping The PC mapping we are processing
* @param cmd ClassMetaData for the owning class with this PCMapping field
* @param mmd Field metadata for the field that this PCMapping represents
* @param fieldValue The value for the PCMapping field in the owning object
* @param storeMgr Store Manager
* @param clr ClassLoader resolver
* @return The current position (after our processing)
*/
public static int populatePrimaryKeyMappingsValuesForPCMapping(JavaTypeMapping[] pkMappings, Object[] pkFieldValues, int position, PersistableMapping pcMapping, AbstractClassMetaData cmd, AbstractMemberMetaData mmd, Object fieldValue, RDBMSStoreManager storeMgr, ClassLoaderResolver clr) {
ExecutionContext ec = storeMgr.getApiAdapter().getExecutionContext(fieldValue);
JavaTypeMapping[] subMappings = pcMapping.getJavaTypeMapping();
if (subMappings.length == 0) {
// Embedded PC has no PK so must be embedded-only so use mapping from owner table
DatastoreClass table = storeMgr.getDatastoreClass(cmd.getFullClassName(), clr);
JavaTypeMapping ownerMapping = table.getMemberMapping(mmd);
EmbeddedMapping embMapping = (EmbeddedMapping) ownerMapping;
for (int k = 0; k < embMapping.getNumberOfJavaTypeMappings(); k++) {
JavaTypeMapping subMapping = embMapping.getJavaTypeMapping(k);
pkMappings[position] = subMapping;
pkFieldValues[position] = getValueForMemberOfObject(ec, subMapping.getMemberMetaData(), fieldValue);
position++;
}
} else {
AbstractClassMetaData pcCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(pcMapping.getType(), clr);
int[] pcPkPositions = pcCmd.getPKMemberPositions();
for (int k = 0; k < subMappings.length; k++) {
AbstractMemberMetaData pcMmd = pcCmd.getMetaDataForManagedMemberAtAbsolutePosition(pcPkPositions[k]);
if (subMappings[k] instanceof PersistableMapping) {
Object val = getValueForMemberOfObject(ec, pcMmd, fieldValue);
position = populatePrimaryKeyMappingsValuesForPCMapping(pkMappings, pkFieldValues, position, (PersistableMapping) subMappings[k], pcCmd, pcMmd, val, storeMgr, clr);
} else {
Object val = getValueForMemberOfObject(ec, pcMmd, fieldValue);
pkMappings[position] = subMappings[k];
pkFieldValues[position] = val;
position++;
}
}
}
return position;
}
use of org.datanucleus.store.rdbms.table.DatastoreClass in project tests by datanucleus.
the class SchemaHandlerTest method testIndexRetrieval.
/**
* Test of the retrieval of indices.
*/
public void testIndexRetrieval() {
addClassesToSchema(new Class[] { SchemaClass1.class, SchemaClass2.class });
PersistenceManager pm = pmf.getPersistenceManager();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
// Retrieve the table for SchemaClass1
ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
DatastoreClass table1 = databaseMgr.getDatastoreClass(SchemaClass1.class.getName(), clr);
DatastoreClass table2 = databaseMgr.getDatastoreClass(SchemaClass2.class.getName(), clr);
// Check for the indices using the schema handler
StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
RDBMSTableIndexInfo indexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(con, "indices", new Object[] { table1 });
int numIndices = 3;
if (vendorID.equals("hsql")) {
// HSQL will create an index for the FK without asking, and we can't replace it with our own so end up with two
numIndices = 4;
}
assertEquals("Number of Indices for table " + table1 + " is wrong", numIndices, indexInfo.getNumberOfChildren());
Iterator indexIter = indexInfo.getChildren().iterator();
while (indexIter.hasNext()) {
IndexInfo index = (IndexInfo) indexIter.next();
String columnName = ((String) index.getProperty("column_name")).toUpperCase();
boolean unique = !((Boolean) index.getProperty("non_unique")).booleanValue();
if (columnName.equals("OTHER_ID")) {
assertFalse("Index for column " + columnName + " is unique!", unique);
} else if (columnName.equals("TABLE1_ID1")) {
assertTrue("Index for column " + columnName + " is not unique!", unique);
} else if (columnName.equals("TABLE1_ID2")) {
assertTrue("Index for column " + columnName + " is not unique!", unique);
} else {
fail("Unexpected index " + columnName + " for table " + table1);
}
}
indexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(con, "indices", new Object[] { table2 });
assertEquals("Number of Indices for table " + table2 + " is wrong", 2, indexInfo.getNumberOfChildren());
indexIter = indexInfo.getChildren().iterator();
while (indexIter.hasNext()) {
IndexInfo index = (IndexInfo) indexIter.next();
String columnName = ((String) index.getProperty("column_name")).toUpperCase();
String indexName = ((String) index.getProperty("index_name")).toUpperCase();
boolean unique = !((Boolean) index.getProperty("non_unique")).booleanValue();
if (columnName.equals("VALUE")) {
assertFalse("Index for column " + columnName + " is unique!", unique);
assertEquals("Index name for column " + columnName + " is wrong!", "VALUE_IDX", indexName);
} else if (columnName.equals("TABLE2_ID")) {
assertTrue("Index for column " + columnName + " is not unique!", unique);
} else {
fail("Unexpected index " + columnName + " for table " + table1);
}
}
}
Aggregations