use of org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping in project tests by datanucleus.
the class AnnotationTest method testSqlResultSetMapping.
/**
* Test of JPA @SqlResultSetMapping
*/
public void testSqlResultSetMapping() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
metaDataMgr.loadPersistenceUnit(pumd, null);
ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
QueryResultMetaData[] queryResultMappings = cmd.getQueryResultMetaData();
assertNotNull("LoginAccount has no QueryResultMetaData!", queryResultMappings);
assertEquals("LoginAccount has incorrect number of query result mappings", 4, queryResultMappings.length);
// Example 1 : Returning 2 entities
QueryResultMetaData qrmd = null;
for (int i = 0; i < queryResultMappings.length; i++) {
QueryResultMetaData md = queryResultMappings[i];
if (md.getName().equals("AN_LOGIN_PLUS_ACCOUNT")) {
qrmd = md;
break;
}
}
if (qrmd == null) {
fail("SQL ResultSet mapping AN_LOGIN_PLUS_ACCOUNT is not present!");
}
String[] scalarCols = qrmd.getScalarColumns();
assertNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
PersistentTypeMapping[] sqlMappingEntities = qrmd.getPersistentTypeMappings();
assertNotNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
assertEquals("LoginAccount sql mapping has incorrect number of entities", 2, sqlMappingEntities.length);
// LoginAccount
assertEquals("LoginAccount sql mapping entity 0 has incorrect class", LoginAccount.class.getName(), sqlMappingEntities[0].getClassName());
assertNull("LoginAccount sql mapping entity 0 has incorrect discriminator", sqlMappingEntities[0].getDiscriminatorColumn());
// Login
assertEquals("LoginAccount sql mapping entity 1 has incorrect class", Login.class.getName(), sqlMappingEntities[1].getClassName());
assertNull("LoginAccount sql mapping entity 1 has incorrect discriminator", sqlMappingEntities[1].getDiscriminatorColumn());
// Example 2 : Returning 2 scalars
qrmd = null;
for (int i = 0; i < queryResultMappings.length; i++) {
QueryResultMetaData md = queryResultMappings[i];
if (md.getName().equals("AN_ACCOUNT_NAMES")) {
qrmd = md;
break;
}
}
if (qrmd == null) {
fail("SQL ResultSet mapping AN_ACCOUNT_NAMES is not present!");
}
scalarCols = qrmd.getScalarColumns();
assertNotNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
assertEquals("LoginAccount sql mapping has incorrect column name", "FIRSTNAME", scalarCols[0]);
assertEquals("LoginAccount sql mapping has incorrect column name", "LASTNAME", scalarCols[1]);
sqlMappingEntities = qrmd.getPersistentTypeMappings();
assertNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
}
use of org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping in project tests by datanucleus.
the class XMLTest method testSqlResultSetMapping.
/**
* Test of JPA <sql-result-set-mapping>
*/
public void testSqlResultSetMapping() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
metaDataMgr.loadPersistenceUnit(pumd, null);
ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
QueryResultMetaData[] queryResultMappings = cmd.getQueryResultMetaData();
assertNotNull("LoginAccount has no QueryResultMetaData!", queryResultMappings);
assertEquals("LoginAccount has incorrect number of query result mappings", 2, queryResultMappings.length);
// Example 1 : Returning 2 entities
QueryResultMetaData qrmd = null;
for (int i = 0; i < queryResultMappings.length; i++) {
QueryResultMetaData md = queryResultMappings[i];
if (md.getName().equals("MD_LOGIN_PLUS_ACCOUNT")) {
qrmd = md;
break;
}
}
if (qrmd == null) {
fail("SQL ResultSet mapping MD_LOGIN_PLUS_ACCOUNT is not present!");
}
String[] scalarCols = qrmd.getScalarColumns();
assertNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
PersistentTypeMapping[] sqlMappingEntities = qrmd.getPersistentTypeMappings();
assertNotNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
assertEquals("LoginAccount sql mapping has incorrect number of entities", 2, sqlMappingEntities.length);
// LoginAccount
assertEquals("LoginAccount sql mapping entity 0 has incorrect class", LoginAccount.class.getName(), sqlMappingEntities[0].getClassName());
assertNull("LoginAccount sql mapping entity 0 has incorrect discriminator", sqlMappingEntities[0].getDiscriminatorColumn());
// Login
assertEquals("LoginAccount sql mapping entity 1 has incorrect class", Login.class.getName(), sqlMappingEntities[1].getClassName());
assertNull("LoginAccount sql mapping entity 1 has incorrect discriminator", sqlMappingEntities[1].getDiscriminatorColumn());
// Example 2 : Returning 2 scalars
qrmd = null;
for (int i = 0; i < queryResultMappings.length; i++) {
QueryResultMetaData md = queryResultMappings[i];
if (md.getName().equals("MD_ACCOUNT_NAMES")) {
qrmd = md;
break;
}
}
if (qrmd == null) {
fail("SQL ResultSet mapping MD_ACCOUNT_NAMES is not present!");
}
scalarCols = qrmd.getScalarColumns();
assertNotNull("LoginAccount sql mapping has incorrect scalar cols", scalarCols);
assertEquals("LoginAccount sql mapping has incorrect column name", "FIRSTNAME", scalarCols[0]);
assertEquals("LoginAccount sql mapping has incorrect column name", "LASTNAME", scalarCols[1]);
sqlMappingEntities = qrmd.getPersistentTypeMappings();
assertNull("LoginAccount sql mapping has incorrect entities", sqlMappingEntities);
}
use of org.datanucleus.metadata.QueryResultMetaData.PersistentTypeMapping in project datanucleus-rdbms by datanucleus.
the class ResultMetaDataROF method getObject.
/**
* Accessor for the object(s) from the current row of the ResultSet.
* @return The object(s) for this row of the ResultSet.
*/
public Object getObject() {
List returnObjects = new ArrayList();
// A). Process persistent types
PersistentTypeMapping[] persistentTypes = queryResultMetaData.getPersistentTypeMappings();
if (persistentTypes != null) {
if (persistentTypeResultSetGetters == null) {
persistentTypeResultSetGetters = new ResultSetGetter[persistentTypes.length];
}
int startColumnIndex = 0;
for (int i = 0; i < persistentTypes.length; i++) {
Set<String> columnsInThisType = new HashSet<>();
AbstractMemberMetaData[] mmds = new AbstractMemberMetaData[columnNames.length];
Map<String, AbstractMemberMetaData> fieldColumns = new HashMap<>();
DatastoreClass dc = ((RDBMSStoreManager) ec.getStoreManager()).getDatastoreClass(persistentTypes[i].getClassName(), ec.getClassLoaderResolver());
AbstractClassMetaData acmd = ec.getMetaDataManager().getMetaDataForClass(persistentTypes[i].getClassName(), ec.getClassLoaderResolver());
Object id = null;
// and two columns with similar names e.g "Col1" and "col1". Until that situation comes up we ignore it :-)
for (int j = startColumnIndex; j < columnNames.length; j++) {
if (columnsInThisType.contains(columnNames[j])) {
// already added this column, so must be another persistent type
startColumnIndex = j;
break;
}
boolean found = false;
if (acmd.getIdentityType() == IdentityType.DATASTORE) {
JavaTypeMapping datastoreIdMapping = dc.getSurrogateMapping(SurrogateColumnType.DATASTORE_ID, false);
Column df = datastoreIdMapping.getColumnMapping(0).getColumn();
if (df.getIdentifier().getName().equalsIgnoreCase(columnNames[j])) {
// add +1 because result sets in jdbc starts with 1
int datastoreIdentityExpressionIndex = j + 1;
id = datastoreIdMapping.getObject(ec, rs, new int[] { datastoreIdentityExpressionIndex });
found = true;
}
}
for (int k = 0; k < acmd.getNoOfManagedMembers() + acmd.getNoOfInheritedManagedMembers() && !found; k++) {
AbstractMemberMetaData apmd = acmd.getMetaDataForManagedMemberAtAbsolutePosition(k);
if (persistentTypes[i].getColumnForField(apmd.getName()) != null) {
if (persistentTypes[i].getColumnForField(apmd.getName()).equalsIgnoreCase(columnNames[j])) {
fieldColumns.put(columnNames[j], apmd);
columnsInThisType.add(columnNames[j]);
mmds[j] = apmd;
found = true;
}
} else {
JavaTypeMapping mapping = dc.getMemberMapping(apmd);
for (int l = 0; l < mapping.getColumnMappings().length && !found; l++) {
Column df = mapping.getColumnMapping(l).getColumn();
if (df.getIdentifier().getName().equalsIgnoreCase(columnNames[j])) {
fieldColumns.put(columnNames[j], apmd);
columnsInThisType.add(columnNames[j]);
mmds[j] = apmd;
found = true;
}
}
}
}
if (!columnsInThisType.contains(columnNames[j])) {
// column not found in this type, so must be another persistent type
startColumnIndex = j;
break;
}
}
// Build fields and mappings in the results
StatementMappingIndex[] stmtMappings = new StatementMappingIndex[acmd.getNoOfManagedMembers() + acmd.getNoOfInheritedManagedMembers()];
Set<AbstractMemberMetaData> resultMmds = new HashSet<>();
resultMmds.addAll(fieldColumns.values());
int[] resultFieldNumbers = new int[resultMmds.size()];
int j = 0;
for (AbstractMemberMetaData apmd : resultMmds) {
StatementMappingIndex stmtMapping = new StatementMappingIndex(dc.getMemberMapping(apmd));
resultFieldNumbers[j] = apmd.getAbsoluteFieldNumber();
List indexes = new ArrayList();
for (int k = 0; k < mmds.length; k++) {
if (mmds[k] == apmd) {
indexes.add(Integer.valueOf(k));
}
}
int[] indxs = new int[indexes.size()];
for (int k = 0; k < indxs.length; k++) {
// add +1 because result sets in JDBC starts with 1
indxs[k] = ((Integer) indexes.get(k)).intValue() + 1;
}
stmtMapping.setColumnPositions(indxs);
stmtMappings[resultFieldNumbers[j]] = stmtMapping;
j++;
}
Object obj = null;
Class type = ec.getClassLoaderResolver().classForName(persistentTypes[i].getClassName());
if (acmd.getIdentityType() == IdentityType.APPLICATION) {
if (persistentTypeResultSetGetters[i] == null) {
final StatementClassMapping resultMappings = new StatementClassMapping();
for (int k = 0; k < resultFieldNumbers.length; k++) {
resultMappings.addMappingForMember(resultFieldNumbers[k], stmtMappings[resultFieldNumbers[k]]);
}
persistentTypeResultSetGetters[i] = new ResultSetGetter(ec, rs, resultMappings, acmd);
}
ResultSetGetter rsGetter = persistentTypeResultSetGetters[i];
// TODO Make use of discriminator like in PersistentClassROF and set the pcClass in this?
id = IdentityUtils.getApplicationIdentityForResultSetRow(ec, acmd, type, false, rsGetter);
obj = ec.findObject(id, new FieldValues() {
public void fetchFields(DNStateManager sm) {
rsGetter.setStateManager(sm);
sm.replaceFields(resultFieldNumbers, rsGetter, false);
}
public void fetchNonLoadedFields(DNStateManager sm) {
rsGetter.setStateManager(sm);
sm.replaceNonLoadedFields(resultFieldNumbers, rsGetter);
}
public FetchPlan getFetchPlanForLoading() {
return null;
}
}, type, ignoreCache, false);
} else if (acmd.getIdentityType() == IdentityType.DATASTORE) {
if (persistentTypeResultSetGetters[i] == null) {
final StatementClassMapping resultMappings = new StatementClassMapping();
for (int k = 0; k < resultFieldNumbers.length; k++) {
resultMappings.addMappingForMember(resultFieldNumbers[k], stmtMappings[resultFieldNumbers[k]]);
}
persistentTypeResultSetGetters[i] = new ResultSetGetter(ec, rs, resultMappings, acmd);
}
ResultSetGetter rsGetter = persistentTypeResultSetGetters[i];
obj = ec.findObject(id, new FieldValues() {
public void fetchFields(DNStateManager sm) {
rsGetter.setStateManager(sm);
sm.replaceFields(resultFieldNumbers, rsGetter, false);
}
public void fetchNonLoadedFields(DNStateManager sm) {
rsGetter.setStateManager(sm);
sm.replaceNonLoadedFields(resultFieldNumbers, rsGetter);
}
public FetchPlan getFetchPlanForLoading() {
return null;
}
}, type, ignoreCache, false);
} else {
// TODO Handle non-durable
NucleusLogger.QUERY.warn("We do not currently support non-durable objects in the results of this type of query.");
}
returnObjects.add(obj);
}
}
// B). Process simple columns
String[] columns = queryResultMetaData.getScalarColumns();
if (columns != null) {
for (int i = 0; i < columns.length; i++) {
try {
Object obj = rs.getObject(columns[i]);
returnObjects.add(obj);
} catch (SQLException sqe) {
String msg = Localiser.msg("059027", sqe.getMessage());
NucleusLogger.QUERY.error(msg);
throw new NucleusUserException(msg, sqe);
}
}
}
// C). Process constructor type mappings
ConstructorTypeMapping[] ctrTypeMappings = queryResultMetaData.getConstructorTypeMappings();
if (ctrTypeMappings != null) {
for (int i = 0; i < ctrTypeMappings.length; i++) {
String ctrClassName = ctrTypeMappings[i].getClassName();
Class ctrCls = ec.getClassLoaderResolver().classForName(ctrClassName);
List<ConstructorTypeColumn> ctrColumns = ctrTypeMappings[i].getColumnsForConstructor();
Class[] ctrArgTypes = null;
Object[] ctrArgVals = null;
if (ctrColumns != null && ctrColumns.size() > 0) {
int j = 0;
ctrArgTypes = new Class[ctrColumns.size()];
ctrArgVals = new Object[ctrColumns.size()];
Iterator<ConstructorTypeColumn> colIter = ctrColumns.iterator();
while (colIter.hasNext()) {
ConstructorTypeColumn ctrCol = colIter.next();
try {
Object colVal = rs.getObject(ctrCol.getColumnName());
ctrArgTypes[j] = colVal.getClass();
if (ctrCol.getJavaType() != null) {
// Attempt to convert to the type requested
ctrArgTypes[j] = ctrCol.getJavaType();
ctrArgVals[j] = TypeConversionHelper.convertTo(colVal, ctrArgTypes[j]);
} else {
ctrArgTypes[j] = colVal.getClass();
ctrArgVals[j] = colVal;
}
} catch (SQLException sqle) {
// TODO Handle this
}
j++;
}
}
returnObjects.add(ClassUtils.newInstance(ctrCls, ctrArgTypes, ctrArgVals));
}
}
if (returnObjects.size() == 0) {
// No objects so user must have supplied incorrect MetaData
return null;
} else if (returnObjects.size() == 1) {
// Return Object
return returnObjects.get(0);
} else {
// Return Object[]
return returnObjects.toArray(new Object[returnObjects.size()]);
}
}
Aggregations