use of org.datanucleus.FetchPlan 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.getDatastoreMapping(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.getDatastoreMappings().length && !found; l++) {
Column df = mapping.getDatastoreMapping(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(ObjectProvider op) {
rsGetter.setObjectProvider(op);
op.replaceFields(resultFieldNumbers, rsGetter, false);
}
public void fetchNonLoadedFields(ObjectProvider op) {
rsGetter.setObjectProvider(op);
op.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(ObjectProvider op) {
rsGetter.setObjectProvider(op);
op.replaceFields(resultFieldNumbers, rsGetter, false);
}
public void fetchNonLoadedFields(ObjectProvider op) {
rsGetter.setObjectProvider(op);
op.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()]);
}
}
use of org.datanucleus.FetchPlan in project datanucleus-rdbms by datanucleus.
the class FKMapStore method put.
/**
* Method to put an item in the Map.
* @param op ObjectProvider for the map.
* @param newKey The key to store the value against
* @param newValue The value to store.
* @return The value stored.
*/
public V put(final ObjectProvider op, final K newKey, V newValue) {
ExecutionContext ec = op.getExecutionContext();
if (keyFieldNumber >= 0) {
validateKeyForWriting(op, newKey);
validateValueType(ec.getClassLoaderResolver(), newValue);
} else {
validateKeyType(ec.getClassLoaderResolver(), newKey);
validateValueForWriting(op, newValue);
}
// Check if there is an existing value for this key
V oldValue = get(op, newKey);
if (oldValue != newValue) {
if (valueCmd != null) {
if (oldValue != null && !oldValue.equals(newValue)) {
// Key is stored in the value and the value has changed so remove the old value
removeValue(op, newKey, oldValue);
}
final Object newOwner = op.getObject();
if (ec.getApiAdapter().isPersistent(newValue)) {
/*
* The new value is already persistent.
*
* "Put" the new value in the map by updating its owner and key
* fields to the appropriate values. This is done with the same
* methods the PC itself would use if the application code
* modified the fields. It should result in no actual database
* activity if the fields were already set to the right values.
*/
if (ec != ec.getApiAdapter().getExecutionContext(newValue)) {
throw new NucleusUserException(Localiser.msg("RDBMS.SCO.Map.WriteValueInvalidWithDifferentPM"), ec.getApiAdapter().getIdForObject(newValue));
}
ObjectProvider vsm = ec.findObjectProvider(newValue);
// Ensure the current owner field is loaded, and replace with new value
if (ownerFieldNumber >= 0) {
vsm.isLoaded(ownerFieldNumber);
Object oldOwner = vsm.provideField(ownerFieldNumber);
vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
if (ec.getManageRelations()) {
ec.getRelationshipManager(vsm).relationChange(ownerFieldNumber, oldOwner, newOwner);
}
} else {
updateValueFk(op, newValue, newOwner);
}
// Ensure the current key field is loaded, and replace with new value
vsm.isLoaded(keyFieldNumber);
Object oldKey = vsm.provideField(keyFieldNumber);
vsm.replaceFieldMakeDirty(keyFieldNumber, newKey);
if (ec.getManageRelations()) {
ec.getRelationshipManager(vsm).relationChange(keyFieldNumber, oldKey, newKey);
}
} else {
/*
* The new value is not yet persistent.
*
* Update its owner and key fields to the appropriate values and
* *then* make it persistent. Making the changes before DB
* insertion avoids an unnecessary UPDATE allows the owner
* and/or key fields to be non-nullable.
*/
ec.persistObjectInternal(newValue, new FieldValues() {
public void fetchFields(ObjectProvider vsm) {
if (ownerFieldNumber >= 0) {
vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
}
vsm.replaceFieldMakeDirty(keyFieldNumber, newKey);
JavaTypeMapping externalFKMapping = valueTable.getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_FK);
if (externalFKMapping != null) {
// Set the owner in the value object where appropriate
vsm.setAssociatedValue(externalFKMapping, op.getObject());
}
}
public void fetchNonLoadedFields(ObjectProvider op) {
}
public FetchPlan getFetchPlanForLoading() {
return null;
}
}, ObjectProvider.PC);
}
} else {
// Value is stored in the key
final Object newOwner = op.getObject();
if (ec.getApiAdapter().isPersistent(newKey)) {
/*
* The new key is already persistent.
*
* "Put" the new key in the map by updating its owner and value
* fields to the appropriate values. This is done with the same
* methods the PC itself would use if the application code
* modified the fields. It should result in no actual database
* activity if the fields were already set to the right values.
*/
if (ec != ec.getApiAdapter().getExecutionContext(newKey)) {
throw new NucleusUserException(Localiser.msg("056060"), ec.getApiAdapter().getIdForObject(newKey));
}
ObjectProvider valOP = ec.findObjectProvider(newKey);
// Ensure the current owner field is loaded, and replace with new key
if (ownerFieldNumber >= 0) {
valOP.isLoaded(ownerFieldNumber);
Object oldOwner = valOP.provideField(ownerFieldNumber);
valOP.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
if (ec.getManageRelations()) {
ec.getRelationshipManager(valOP).relationChange(ownerFieldNumber, oldOwner, newOwner);
}
} else {
updateKeyFk(op, newKey, newOwner);
}
// Ensure the current value field is loaded, and replace with new value
valOP.isLoaded(valueFieldNumber);
// TODO Should we update the local variable ?
oldValue = (V) valOP.provideField(valueFieldNumber);
valOP.replaceFieldMakeDirty(valueFieldNumber, newValue);
if (ec.getManageRelations()) {
ec.getRelationshipManager(valOP).relationChange(valueFieldNumber, oldValue, newValue);
}
} else {
/*
* The new key is not yet persistent.
*
* Update its owner and key fields to the appropriate values and
* *then* make it persistent. Making the changes before DB
* insertion avoids an unnecessary UPDATE allows the owner
* and/or key fields to be non-nullable.
*/
final Object newValueObj = newValue;
ec.persistObjectInternal(newKey, new FieldValues() {
public void fetchFields(ObjectProvider vsm) {
if (ownerFieldNumber >= 0) {
vsm.replaceFieldMakeDirty(ownerFieldNumber, newOwner);
}
vsm.replaceFieldMakeDirty(valueFieldNumber, newValueObj);
JavaTypeMapping externalFKMapping = valueTable.getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_FK);
if (externalFKMapping != null) {
// Set the owner in the value object where appropriate
vsm.setAssociatedValue(externalFKMapping, op.getObject());
}
}
public void fetchNonLoadedFields(ObjectProvider op) {
}
public FetchPlan getFetchPlanForLoading() {
return null;
}
}, ObjectProvider.PC);
/*if (ownerFieldNumber < 0)
{
// TODO Think about removing this since we set the associated owner here
updateKeyFk(sm, newKey, newOwner);
}*/
}
}
}
// TODO Cater for key being PC and having delete-dependent
if (ownerMemberMetaData.getMap().isDependentValue() && oldValue != null) {
// Delete the old value if it is no longer contained and is dependent
if (!containsValue(op, oldValue)) {
ec.deleteObjectInternal(oldValue);
}
}
return oldValue;
}
use of org.datanucleus.FetchPlan in project datanucleus-rdbms by datanucleus.
the class FKSetStore method add.
/**
* Method to add an object to the relationship at the collection end.
* @param ownerOP ObjectProvider for the owner.
* @param element Element to be added
* @return Success indicator
*/
public boolean add(final ObjectProvider ownerOP, E element, int size) {
if (element == null) {
// Sets allow no duplicates
throw new NucleusUserException(Localiser.msg("056039"));
}
// Make sure that the element is persisted in the datastore (reachability)
final Object newOwner = ownerOP.getObject();
final ExecutionContext ec = ownerOP.getExecutionContext();
// Find the (element) table storing the FK back to the owner
boolean isPersistentInterface = storeMgr.getNucleusContext().getMetaDataManager().isPersistentInterface(elementType);
DatastoreClass elementTable = null;
if (isPersistentInterface) {
elementTable = storeMgr.getDatastoreClass(storeMgr.getNucleusContext().getMetaDataManager().getImplementationNameForPersistentInterface(elementType), clr);
} else {
Class elementTypeCls = clr.classForName(elementType);
if (elementTypeCls.isInterface()) {
// Set<interface> so use type of element passed in and get its table
elementTable = storeMgr.getDatastoreClass(element.getClass().getName(), clr);
} else {
// Use table for element type
elementTable = storeMgr.getDatastoreClass(elementType, clr);
}
}
if (elementTable == null) {
// "subclass-table", persisted into table of other class
AbstractClassMetaData[] managingCmds = storeMgr.getClassesManagingTableForClass(elementCmd, clr);
if (managingCmds != null && managingCmds.length > 0) {
// Find which of these subclasses is appropriate for this element
for (int i = 0; i < managingCmds.length; i++) {
Class tblCls = clr.classForName(managingCmds[i].getFullClassName());
if (tblCls.isAssignableFrom(element.getClass())) {
elementTable = storeMgr.getDatastoreClass(managingCmds[i].getFullClassName(), clr);
break;
}
}
}
}
final DatastoreClass elementTbl = elementTable;
boolean inserted = validateElementForWriting(ec, element, new FieldValues() {
public void fetchFields(ObjectProvider elementOP) {
if (elementTbl != null) {
JavaTypeMapping externalFKMapping = elementTbl.getExternalMapping(ownerMemberMetaData, MappingType.EXTERNAL_FK);
if (externalFKMapping != null) {
// The element has an external FK mapping so set the value it needs to use in the INSERT
elementOP.setAssociatedValue(externalFKMapping, ownerOP.getObject());
}
if (relationDiscriminatorMapping != null) {
// Element type has a shared FK so set the discriminator value for this relation
elementOP.setAssociatedValue(relationDiscriminatorMapping, relationDiscriminatorValue);
}
}
int fieldNumInElement = getFieldNumberInElementForBidirectional(elementOP);
if (fieldNumInElement >= 0) {
// TODO Move this into RelationshipManager
// Managed Relations : 1-N bidir, so make sure owner is correct at persist
Object currentOwner = elementOP.provideField(fieldNumInElement);
if (currentOwner == null) {
// No owner, so correct it
NucleusLogger.PERSISTENCE.info(Localiser.msg("056037", ownerOP.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject())));
elementOP.replaceFieldMakeDirty(fieldNumInElement, newOwner);
} else if (currentOwner != newOwner) {
// Check for owner change
Object ownerId1 = ec.getApiAdapter().getIdForObject(currentOwner);
Object ownerId2 = ec.getApiAdapter().getIdForObject(newOwner);
if (ownerId1 != null && ownerId2 != null && ownerId1.equals(ownerId2)) {
// Must be attaching
if (!ec.getApiAdapter().isDetached(newOwner)) {
// Attaching, so make sure we set to the attached owner
elementOP.replaceField(fieldNumInElement, newOwner);
}
} else if (ownerOP.getReferencedPC() == null) {
// Not being attached so must be inconsistent owner, so throw exception
throw new NucleusUserException(Localiser.msg("056038", ownerOP.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject()), StringUtils.toJVMIDString(currentOwner)));
}
}
}
}
public void fetchNonLoadedFields(ObjectProvider op) {
}
public FetchPlan getFetchPlanForLoading() {
return null;
}
});
if (inserted) {
// Element has just been persisted so the FK will be set
return true;
}
// Element was already persistent so make sure the FK is in place
// TODO This is really "ManagedRelationships" so needs to go in RelationshipManager
ObjectProvider elementOP = ec.findObjectProvider(element);
if (elementOP == null) {
// Element is likely being attached and this is the detached element; lookup the attached element via the id
Object elementId = ec.getApiAdapter().getIdForObject(element);
if (elementId != null) {
element = (E) ec.findObject(elementId, false, false, element.getClass().getName());
if (element != null) {
elementOP = ec.findObjectProvider(element);
}
}
}
int fieldNumInElement = getFieldNumberInElementForBidirectional(elementOP);
if (fieldNumInElement >= 0 && elementOP != null) {
// Managed Relations : 1-N bidir, so update the owner of the element
// Ensure is loaded
elementOP.isLoaded(fieldNumInElement);
Object oldOwner = elementOP.provideField(fieldNumInElement);
if (oldOwner != newOwner) {
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("055009", ownerOP.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(element)));
}
elementOP.replaceFieldMakeDirty(fieldNumInElement, newOwner);
if (ec.getManageRelations()) {
// Managed Relationships - add the change we've made here to be analysed at flush
RelationshipManager relationshipManager = ec.getRelationshipManager(elementOP);
relationshipManager.relationChange(fieldNumInElement, oldOwner, newOwner);
if (ec.isFlushing()) {
// When already flushing process the changes right away to make them effective during the current flush
relationshipManager.process();
}
}
if (ec.isFlushing()) {
elementOP.flush();
}
}
return oldOwner != newOwner;
}
// 1-N unidir so update the FK if not set to be contained in the set
boolean contained = contains(ownerOP, element);
return (contained ? false : updateElementFk(ownerOP, element, newOwner));
}
use of org.datanucleus.FetchPlan in project datanucleus-rdbms by datanucleus.
the class MapKeySetStore method getSQLStatementForIterator.
/**
* Method to generate an SQLStatement for iterating through keys of the map.
* Populates the iteratorMappingDef and iteratorMappingParams.
* Creates a statement that selects the key table(s), and adds any necessary join to the containerTable
* if that is not the key table. If the key is embedded then selects the table it is embedded in.
* Adds a restriction on the ownerMapping of the containerTable so we can restrict to the owner object.
* @param ownerOP ObjectProvider for the owner object
* @return The SQLStatement
*/
protected SelectStatement getSQLStatementForIterator(ObjectProvider ownerOP) {
SelectStatement sqlStmt = null;
ExecutionContext ec = ownerOP.getExecutionContext();
final ClassLoaderResolver clr = ec.getClassLoaderResolver();
final Class keyCls = clr.classForName(elementType);
SQLTable containerSqlTbl = null;
MapType mapType = getOwnerMemberMetaData().getMap().getMapType();
FetchPlan fp = ec.getFetchPlan();
if (elementCmd != null && elementCmd.getDiscriminatorStrategyForTable() != null && elementCmd.getDiscriminatorStrategyForTable() != DiscriminatorStrategy.NONE) {
// Map<PC, ?> where key has discriminator
if (ClassUtils.isReferenceType(keyCls)) {
// Take the metadata for the first implementation of the reference type
String[] clsNames = storeMgr.getNucleusContext().getMetaDataManager().getClassesImplementingInterface(elementType, clr);
Class[] cls = new Class[clsNames.length];
for (int j = 0; j < clsNames.length; j++) {
cls[j] = clr.classForName(clsNames[j]);
}
sqlStmt = new DiscriminatorStatementGenerator(storeMgr, clr, cls, true, null, null).getStatement(ec);
} else {
sqlStmt = new DiscriminatorStatementGenerator(storeMgr, clr, clr.classForName(elementInfo[0].getClassName()), true, null, null).getStatement(ec);
}
containerSqlTbl = sqlStmt.getPrimaryTable();
iterateUsingDiscriminator = true;
if (mapType == MapType.MAP_TYPE_VALUE_IN_KEY) {
// Select key fields
containerSqlTbl = sqlStmt.getPrimaryTable();
iteratorMappingDef = new StatementClassMapping();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
} else {
// MAP_TYPE_KEY_IN_VALUE, MAP_TYPE_JOIN
// Join to join table and select key fields
JavaTypeMapping keyIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
containerSqlTbl = sqlStmt.join(JoinType.INNER_JOIN, sqlStmt.getPrimaryTable(), keyIdMapping, containerTable, null, elementMapping, null, null, true);
iteratorMappingDef = new StatementClassMapping();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
}
} else {
if (mapType == MapType.MAP_TYPE_VALUE_IN_KEY) {
// Select of key in key table (allow union of possible key types)
iteratorMappingDef = new StatementClassMapping();
UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, keyCls, true, null, null);
stmtGen.setOption(SelectStatementGenerator.OPTION_SELECT_DN_TYPE);
iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.DN_TYPE_COLUMN);
sqlStmt = stmtGen.getStatement(ec);
containerSqlTbl = sqlStmt.getPrimaryTable();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
} else {
// MAP_TYPE_KEY_IN_VALUE, MAP_TYPE_JOIN
if (elementCmd != null) {
// Select of key table, joining to join table
iteratorMappingDef = new StatementClassMapping();
UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, keyCls, true, null, null);
stmtGen.setOption(SelectStatementGenerator.OPTION_SELECT_DN_TYPE);
iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.DN_TYPE_COLUMN);
sqlStmt = stmtGen.getStatement(ec);
JavaTypeMapping keyIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
containerSqlTbl = sqlStmt.join(JoinType.INNER_JOIN, sqlStmt.getPrimaryTable(), keyIdMapping, containerTable, null, elementMapping, null, null, true);
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
} else {
// Select of key in join table
sqlStmt = new SelectStatement(storeMgr, containerTable, null, null);
sqlStmt.setClassLoaderResolver(clr);
containerSqlTbl = sqlStmt.getPrimaryTable();
SQLTable elemSqlTblForKey = containerSqlTbl;
if (elementMapping.getTable() != containerSqlTbl.getTable()) {
elemSqlTblForKey = sqlStmt.getTableForDatastoreContainer(elementMapping.getTable());
if (elemSqlTblForKey == null) {
// Add join to element table
elemSqlTblForKey = sqlStmt.join(JoinType.INNER_JOIN, sqlStmt.getPrimaryTable(), sqlStmt.getPrimaryTable().getTable().getIdMapping(), elementMapping.getTable(), null, elementMapping.getTable().getIdMapping(), null, null, true);
}
}
sqlStmt.select(elemSqlTblForKey, elementMapping, null);
}
}
}
// Apply condition on owner field to filter by owner
SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
SQLTable ownerSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, containerSqlTbl, ownerMapping);
SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);
// Input parameter(s) - the owner
int inputParamNum = 1;
StatementMappingIndex ownerIdx = new StatementMappingIndex(ownerMapping);
if (sqlStmt.getNumberOfUnions() > 0) {
// Add parameter occurrence for each union of statement
for (int j = 0; j < sqlStmt.getNumberOfUnions() + 1; j++) {
int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k = 0; k < ownerMapping.getNumberOfDatastoreMappings(); k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
} else {
int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k = 0; k < ownerMapping.getNumberOfDatastoreMappings(); k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
iteratorMappingParams = new StatementParameterMapping();
iteratorMappingParams.addMappingForParameter("owner", ownerIdx);
return sqlStmt;
}
use of org.datanucleus.FetchPlan in project datanucleus-rdbms by datanucleus.
the class MapValueCollectionStore method getSQLStatementForIterator.
/**
* Method to generate an SQLStatement for iterating through values of the map.
* Populates the iteratorMappingDef and iteratorMappingParams.
* Creates a statement that selects the value table(s), and adds any necessary join to the containerTable
* if that is not the value table. If the value is embedded then selects the table it is embedded in.
* Adds a restriction on the ownerMapping of the containerTable so we can restrict to the owner object.
* @param ownerOP ObjectProvider for the owner object
* @return The SQLStatement
*/
protected SelectStatement getSQLStatementForIterator(ObjectProvider ownerOP) {
SelectStatement sqlStmt = null;
ExecutionContext ec = ownerOP.getExecutionContext();
final ClassLoaderResolver clr = ec.getClassLoaderResolver();
final Class valueCls = clr.classForName(elementType);
SQLTable containerSqlTbl = null;
MapType mapType = getOwnerMemberMetaData().getMap().getMapType();
FetchPlan fp = ec.getFetchPlan();
if (elementCmd != null && elementCmd.getDiscriminatorStrategyForTable() != null && elementCmd.getDiscriminatorStrategyForTable() != DiscriminatorStrategy.NONE) {
// Map<?, PC> where value has discriminator
if (ClassUtils.isReferenceType(valueCls)) {
// Take the metadata for the first implementation of the reference type
String[] clsNames = storeMgr.getNucleusContext().getMetaDataManager().getClassesImplementingInterface(elementType, clr);
Class[] cls = new Class[clsNames.length];
for (int j = 0; j < clsNames.length; j++) {
cls[j] = clr.classForName(clsNames[j]);
}
SelectStatementGenerator stmtGen = new DiscriminatorStatementGenerator(storeMgr, clr, cls, true, null, null);
sqlStmt = stmtGen.getStatement(ec);
} else {
SelectStatementGenerator stmtGen = new DiscriminatorStatementGenerator(storeMgr, clr, valueCls, true, null, null);
sqlStmt = stmtGen.getStatement(ec);
}
iterateUsingDiscriminator = true;
if (mapType == MapType.MAP_TYPE_VALUE_IN_KEY) {
// Join to key table and select value fields
JavaTypeMapping valueIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
containerSqlTbl = sqlStmt.join(JoinType.INNER_JOIN, sqlStmt.getPrimaryTable(), valueIdMapping, containerTable, null, elementMapping, null, null, true);
iteratorMappingDef = new StatementClassMapping();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
} else if (mapType == MapType.MAP_TYPE_KEY_IN_VALUE) {
// Select value fields
containerSqlTbl = sqlStmt.getPrimaryTable();
iteratorMappingDef = new StatementClassMapping();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
} else {
// Join to join table and select value fields
JavaTypeMapping valueIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
containerSqlTbl = sqlStmt.join(JoinType.INNER_JOIN, sqlStmt.getPrimaryTable(), valueIdMapping, containerTable, null, elementMapping, null, null, true);
iteratorMappingDef = new StatementClassMapping();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
}
} else {
if (mapType == MapType.MAP_TYPE_VALUE_IN_KEY) {
if (elementCmd != null) {
// TODO Allow for null value [change to select the key table and left outer join to the key]
// Select of value table, joining to key table
iteratorMappingDef = new StatementClassMapping();
UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, valueCls, true, null, null);
stmtGen.setOption(SelectStatementGenerator.OPTION_SELECT_DN_TYPE);
iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.DN_TYPE_COLUMN);
sqlStmt = stmtGen.getStatement(ec);
JavaTypeMapping valueIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
containerSqlTbl = sqlStmt.join(JoinType.INNER_JOIN, sqlStmt.getPrimaryTable(), valueIdMapping, containerTable, null, elementMapping, null, null, true);
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
} else {
// Select of value in key table
sqlStmt = new SelectStatement(storeMgr, containerTable, null, null);
sqlStmt.setClassLoaderResolver(clr);
containerSqlTbl = sqlStmt.getPrimaryTable();
SQLTable elemSqlTblForValue = containerSqlTbl;
if (elementMapping.getTable() != containerSqlTbl.getTable()) {
elemSqlTblForValue = sqlStmt.getTableForDatastoreContainer(elementMapping.getTable());
if (elemSqlTblForValue == null) {
// Add join to key table holding value
elemSqlTblForValue = sqlStmt.join(JoinType.INNER_JOIN, sqlStmt.getPrimaryTable(), sqlStmt.getPrimaryTable().getTable().getIdMapping(), elementMapping.getTable(), null, elementMapping.getTable().getIdMapping(), null, null, true);
}
}
sqlStmt.select(elemSqlTblForValue, elementMapping, null);
}
} else if (mapType == MapType.MAP_TYPE_KEY_IN_VALUE) {
// Select of value in value table (allow union of possible value types)
iteratorMappingDef = new StatementClassMapping();
UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, valueCls, true, null, null);
stmtGen.setOption(SelectStatementGenerator.OPTION_SELECT_DN_TYPE);
iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.DN_TYPE_COLUMN);
sqlStmt = stmtGen.getStatement(ec);
containerSqlTbl = sqlStmt.getPrimaryTable();
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
} else {
if (elementCmd != null) {
// TODO Allow for null value [change to select the join table and left outer join to the key]
// Select of value table, joining to key table
iteratorMappingDef = new StatementClassMapping();
UnionStatementGenerator stmtGen = new UnionStatementGenerator(storeMgr, clr, valueCls, true, null, null);
stmtGen.setOption(SelectStatementGenerator.OPTION_SELECT_DN_TYPE);
iteratorMappingDef.setNucleusTypeColumnName(UnionStatementGenerator.DN_TYPE_COLUMN);
sqlStmt = stmtGen.getStatement(ec);
JavaTypeMapping valueIdMapping = sqlStmt.getPrimaryTable().getTable().getIdMapping();
containerSqlTbl = sqlStmt.join(JoinType.INNER_JOIN, sqlStmt.getPrimaryTable(), valueIdMapping, containerTable, null, elementMapping, null, null, true);
SQLStatementHelper.selectFetchPlanOfSourceClassInStatement(sqlStmt, iteratorMappingDef, fp, sqlStmt.getPrimaryTable(), elementCmd, fp.getMaxFetchDepth());
} else {
// Select of value in join table
sqlStmt = new SelectStatement(storeMgr, containerTable, null, null);
containerSqlTbl = sqlStmt.getPrimaryTable();
sqlStmt.select(sqlStmt.getPrimaryTable(), elementMapping, null);
}
}
}
// Apply condition on owner field to filter by owner
SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
SQLTable ownerSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, containerSqlTbl, ownerMapping);
SQLExpression ownerExpr = exprFactory.newExpression(sqlStmt, ownerSqlTbl, ownerMapping);
SQLExpression ownerVal = exprFactory.newLiteralParameter(sqlStmt, ownerMapping, null, "OWNER");
sqlStmt.whereAnd(ownerExpr.eq(ownerVal), true);
// Input parameter(s) - the owner
int inputParamNum = 1;
StatementMappingIndex ownerIdx = new StatementMappingIndex(ownerMapping);
if (sqlStmt.getNumberOfUnions() > 0) {
// Add parameter occurrence for each union of statement
for (int j = 0; j < sqlStmt.getNumberOfUnions() + 1; j++) {
int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k = 0; k < ownerMapping.getNumberOfDatastoreMappings(); k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
} else {
int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k = 0; k < ownerMapping.getNumberOfDatastoreMappings(); k++) {
paramPositions[k] = inputParamNum++;
}
ownerIdx.addParameterOccurrence(paramPositions);
}
iteratorMappingParams = new StatementParameterMapping();
iteratorMappingParams.addMappingForParameter("owner", ownerIdx);
return sqlStmt;
}
Aggregations