use of org.datanucleus.FetchPlanForClass in project datanucleus-rdbms by datanucleus.
the class JDOQLQuery method compileQueryFull.
/**
* Method to set the (native) query statement for the compiled query as a whole.
* @param parameters Input parameters (if known)
* @param candidateCmd Metadata for the candidate class
*/
private void compileQueryFull(Map parameters, AbstractClassMetaData candidateCmd) {
if (type != QueryType.SELECT) {
return;
}
long startTime = 0;
if (NucleusLogger.QUERY.isDebugEnabled()) {
startTime = System.currentTimeMillis();
NucleusLogger.QUERY.debug(Localiser.msg("021083", getLanguage(), toString()));
}
if (result != null) {
datastoreCompilation.setResultDefinition(new StatementResultMapping());
} else {
datastoreCompilation.setResultDefinitionForClass(new StatementClassMapping());
}
// Generate statement for candidate(s)
SelectStatement stmt = null;
try {
boolean includeSoftDeletes = getBooleanExtensionProperty("include-soft-deletes", false);
Set<String> options = null;
if (includeSoftDeletes) {
options = new HashSet<>();
options.add(SelectStatementGenerator.OPTION_INCLUDE_SOFT_DELETES);
}
stmt = RDBMSQueryUtils.getStatementForCandidates((RDBMSStoreManager) getStoreManager(), null, candidateCmd, datastoreCompilation.getResultDefinitionForClass(), ec, candidateClass, subclasses, result, null, null, options);
} catch (NucleusException ne) {
// Statement would result in no results, so just catch it and avoid generating the statement
NucleusLogger.QUERY.warn("Query for candidates of " + candidateClass.getName() + (subclasses ? " and subclasses" : "") + " resulted in no possible candidates : " + StringUtils.getMessageFromRootCauseOfThrowable(ne));
statementReturnsEmpty = true;
return;
}
// Update the SQLStatement with filter, ordering, result etc
Set<String> options = new HashSet<>();
options.add(QueryToSQLMapper.OPTION_BULK_UPDATE_VERSION);
if (getBooleanExtensionProperty(EXTENSION_USE_IS_NULL_WHEN_EQUALS_NULL_PARAM, true)) {
options.add(QueryToSQLMapper.OPTION_NULL_PARAM_USE_IS_NULL);
}
if (getBooleanExtensionProperty(EXTENSION_NON_DISTINCT_IMPLICIT_JOIN, false)) {
options.add(QueryToSQLMapper.OPTION_NON_DISTINCT_IMPLICIT_JOINS);
}
QueryToSQLMapper sqlMapper = new QueryToSQLMapper(stmt, compilation, parameters, datastoreCompilation.getResultDefinitionForClass(), datastoreCompilation.getResultDefinition(), candidateCmd, subclasses, getFetchPlan(), ec, getParsedImports(), options, extensions);
setMapperJoinTypes(sqlMapper);
sqlMapper.compile();
datastoreCompilation.setParameterNameByPosition(sqlMapper.getParameterNameByPosition());
datastoreCompilation.setPrecompilable(sqlMapper.isPrecompilable());
if (!getResultDistinct() && stmt.isDistinct()) {
setResultDistinct(true);
compilation.setResultDistinct();
}
if (candidateCollection != null) {
// Restrict to the supplied candidate ids
BooleanExpression candidateExpr = null;
Iterator iter = candidateCollection.iterator();
JavaTypeMapping idMapping = stmt.getPrimaryTable().getTable().getIdMapping();
while (iter.hasNext()) {
Object candidate = iter.next();
SQLExpression idExpr = stmt.getSQLExpressionFactory().newExpression(stmt, stmt.getPrimaryTable(), idMapping);
SQLExpression idVal = stmt.getSQLExpressionFactory().newLiteral(stmt, idMapping, candidate);
if (candidateExpr == null) {
candidateExpr = idExpr.eq(idVal);
} else {
candidateExpr = candidateExpr.ior(idExpr.eq(idVal));
}
}
stmt.whereAnd(candidateExpr, true);
}
// Apply any range
if (range != null) {
long lower = fromInclNo;
long upper = toExclNo;
if (fromInclParam != null) {
if (parameters.containsKey(fromInclParam)) {
lower = ((Number) parameters.get(fromInclParam)).longValue();
} else {
// Must be numbered input so take penultimate
lower = ((Number) parameters.get(Integer.valueOf(parameters.size() - 2))).longValue();
}
}
if (toExclParam != null) {
if (parameters.containsKey(toExclParam)) {
upper = ((Number) parameters.get(toExclParam)).longValue();
} else {
// Must be numbered input so take ultimate
upper = ((Number) parameters.get(Integer.valueOf(parameters.size() - 1))).longValue();
}
}
stmt.setRange(lower, upper - lower);
}
// Set any extensions
boolean useUpdateLock = RDBMSQueryUtils.useUpdateLockForQuery(this);
stmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, Boolean.valueOf(useUpdateLock));
if (getBooleanExtensionProperty(EXTENSION_FOR_UPDATE_NOWAIT, false)) {
stmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE_NOWAIT, Boolean.TRUE);
}
datastoreCompilation.addStatement(stmt, stmt.getSQLText().toSQL(), false);
datastoreCompilation.setStatementParameters(stmt.getSQLText().getParametersForStatement());
if (result == null && !(resultClass != null && resultClass != candidateClass)) {
// Select of candidates, so check for any immediate multi-valued fields that are marked for fetching
// TODO If the query joins to a 1-1/N-1 and then we have a multi-valued field, we should allow that too
FetchPlanForClass fpc = getFetchPlan().getFetchPlanForClass(candidateCmd);
int[] fpMembers = fpc.getMemberNumbers();
for (int i = 0; i < fpMembers.length; i++) {
AbstractMemberMetaData fpMmd = candidateCmd.getMetaDataForManagedMemberAtAbsolutePosition(fpMembers[i]);
RelationType fpRelType = fpMmd.getRelationType(clr);
if (RelationType.isRelationMultiValued(fpRelType)) {
if (fpMmd.hasCollection() && SCOUtils.collectionHasSerialisedElements(fpMmd)) {
// Ignore collections serialised into the owner (retrieved in main query)
} else if (fpMmd.hasMap() && SCOUtils.mapHasSerialisedKeysAndValues(fpMmd)) {
// Ignore maps serialised into the owner (retrieved in main query)
} else if (fpMmd.hasMap()) {
// Ignore maps for now until we support them
} else {
String multifetchType = getStringExtensionProperty(RDBMSPropertyNames.PROPERTY_RDBMS_QUERY_MULTIVALUED_FETCH, null);
if (multifetchType == null) {
// Default to bulk-fetch EXISTS, so advise the user of why this is happening and how to turn it off
NucleusLogger.QUERY.debug("You have selected field " + fpMmd.getFullFieldName() + " for fetching by this query. We will fetch it using 'EXISTS'." + " To disable this set the query extension/hint '" + RDBMSPropertyNames.PROPERTY_RDBMS_QUERY_MULTIVALUED_FETCH + "' as 'none' or remove the field" + " from the query FetchPlan. If this bulk-fetch generates an invalid or unoptimised query, please report it with a way of reproducing it");
multifetchType = "exists";
}
if (multifetchType.equalsIgnoreCase("exists")) {
// Fetch container contents for all candidate owners
BulkFetchExistsHandler helper = new BulkFetchExistsHandler();
IteratorStatement iterStmt = helper.getStatementToBulkFetchField(candidateCmd, fpMmd, this, parameters, datastoreCompilation, options);
if (iterStmt != null) {
datastoreCompilation.setSCOIteratorStatement(fpMmd.getFullFieldName(), iterStmt);
} else {
NucleusLogger.GENERAL.debug("Note that query has field " + fpMmd.getFullFieldName() + " marked in the FetchPlan, yet this is currently not fetched by this query");
}
} else if (multifetchType.equalsIgnoreCase("join")) {
// Fetch container contents for all candidate owners
BulkFetchJoinHandler helper = new BulkFetchJoinHandler();
IteratorStatement iterStmt = helper.getStatementToBulkFetchField(candidateCmd, fpMmd, this, parameters, datastoreCompilation, options);
if (iterStmt != null) {
datastoreCompilation.setSCOIteratorStatement(fpMmd.getFullFieldName(), iterStmt);
} else {
NucleusLogger.GENERAL.debug("Note that query has field " + fpMmd.getFullFieldName() + " marked in the FetchPlan, yet this is currently not fetched by this query");
}
} else {
NucleusLogger.GENERAL.debug("Note that query has field " + fpMmd.getFullFieldName() + " marked in the FetchPlan, yet this is not fetched by this query.");
}
// TODO Continue this bulk fetch process to multivalued fields of this field
}
} else if (RelationType.isRelationSingleValued(fpRelType)) {
// TODO Check for multivalued fields of this 1-1/N-1 field
}
}
}
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021084", getLanguage(), System.currentTimeMillis() - startTime));
}
}
use of org.datanucleus.FetchPlanForClass in project datanucleus-rdbms by datanucleus.
the class JPQLQuery method compileQueryFull.
/**
* Method to set the (native) query statement for the compiled query as a whole.
* The "table groups" in the resultant SQLStatement will be named as per the candidate alias,
* and thereafter "{alias}.{fieldName}".
* @param parameters Input parameters (if known)
* @param candidateCmd Metadata for the candidate class
*/
private void compileQueryFull(Map parameters, AbstractClassMetaData candidateCmd) {
if (type != QueryType.SELECT) {
return;
}
if (candidateCollection != null) {
return;
}
long startTime = 0;
if (NucleusLogger.QUERY.isDebugEnabled()) {
startTime = System.currentTimeMillis();
NucleusLogger.QUERY.debug(Localiser.msg("021083", getLanguage(), toString()));
}
if (result != null) {
datastoreCompilation.setResultDefinition(new StatementResultMapping());
} else {
datastoreCompilation.setResultDefinitionForClass(new StatementClassMapping());
}
// Generate statement for candidate(s)
SelectStatement stmt = RDBMSQueryUtils.getStatementForCandidates((RDBMSStoreManager) getStoreManager(), null, candidateCmd, datastoreCompilation.getResultDefinitionForClass(), ec, candidateClass, subclasses, result, compilation.getCandidateAlias(), compilation.getCandidateAlias(), null);
// Update the SQLStatement with filter, ordering, result etc
Set<String> options = new HashSet<>();
options.add(QueryToSQLMapper.OPTION_CASE_INSENSITIVE);
options.add(QueryToSQLMapper.OPTION_EXPLICIT_JOINS);
if (// Default to false for "IS NULL" with null param
getBooleanExtensionProperty(EXTENSION_USE_IS_NULL_WHEN_EQUALS_NULL_PARAM, false)) {
options.add(QueryToSQLMapper.OPTION_NULL_PARAM_USE_IS_NULL);
}
QueryToSQLMapper sqlMapper = new QueryToSQLMapper(stmt, compilation, parameters, datastoreCompilation.getResultDefinitionForClass(), datastoreCompilation.getResultDefinition(), candidateCmd, subclasses, getFetchPlan(), ec, null, options, extensions);
setMapperJoinTypes(sqlMapper);
sqlMapper.compile();
datastoreCompilation.setParameterNameByPosition(sqlMapper.getParameterNameByPosition());
datastoreCompilation.setPrecompilable(sqlMapper.isPrecompilable());
// Apply any range
if (range != null) {
long lower = fromInclNo;
long upper = toExclNo;
if (fromInclParam != null) {
lower = ((Number) parameters.get(fromInclParam)).longValue();
}
if (toExclParam != null) {
upper = ((Number) parameters.get(toExclParam)).longValue();
}
long count = upper - lower;
if (upper == Long.MAX_VALUE) {
count = -1;
}
stmt.setRange(lower, count);
}
// Set any extensions
boolean useUpdateLock = RDBMSQueryUtils.useUpdateLockForQuery(this);
stmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, Boolean.valueOf(useUpdateLock));
if (getBooleanExtensionProperty(EXTENSION_FOR_UPDATE_NOWAIT, false)) {
stmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE_NOWAIT, Boolean.TRUE);
}
datastoreCompilation.addStatement(stmt, stmt.getSQLText().toSQL(), false);
datastoreCompilation.setStatementParameters(stmt.getSQLText().getParametersForStatement());
if (result == null && !(resultClass != null && resultClass != candidateClass)) {
// Select of candidates, so check for any immediate multi-valued fields that are marked for fetching
// TODO If the query joins to a 1-1/N-1 and then we have a multi-valued field, we should allow that too
FetchPlanForClass fpc = getFetchPlan().getFetchPlanForClass(candidateCmd);
int[] fpMembers = fpc.getMemberNumbers();
for (int i = 0; i < fpMembers.length; i++) {
AbstractMemberMetaData fpMmd = candidateCmd.getMetaDataForManagedMemberAtAbsolutePosition(fpMembers[i]);
RelationType fpRelType = fpMmd.getRelationType(clr);
if (RelationType.isRelationMultiValued(fpRelType)) {
if (fpMmd.hasCollection() && SCOUtils.collectionHasSerialisedElements(fpMmd)) {
// Ignore collections serialised into the owner (retrieved in main query)
} else if (fpMmd.hasMap() && SCOUtils.mapHasSerialisedKeysAndValues(fpMmd)) {
// Ignore maps serialised into the owner (retrieved in main query)
} else if (fpMmd.hasMap()) {
// Ignore maps for now, until we support bulk-fetch
} else {
String multifetchType = getStringExtensionProperty(RDBMSPropertyNames.PROPERTY_RDBMS_QUERY_MULTIVALUED_FETCH, null);
if (multifetchType == null) {
// Default to bulk-fetch, so advise the user of why this is happening and how to turn it off
NucleusLogger.QUERY.debug("You have selected field " + fpMmd.getFullFieldName() + " for fetching by this query. We will fetch it using 'EXISTS'." + " To disable this set the query extension/hint '" + RDBMSPropertyNames.PROPERTY_RDBMS_QUERY_MULTIVALUED_FETCH + "' as 'none' or remove the field" + " from the query FetchPlan. If this bulk-fetch generates an invalid or unoptimised query, please report it with a way of reproducing it");
multifetchType = "exists";
}
if (multifetchType.equalsIgnoreCase("exists")) {
// Fetch container contents for all candidate owners
BulkFetchExistsHandler helper = new BulkFetchExistsHandler();
IteratorStatement iterStmt = helper.getStatementToBulkFetchField(candidateCmd, fpMmd, this, parameters, datastoreCompilation, options);
if (iterStmt != null) {
datastoreCompilation.setSCOIteratorStatement(fpMmd.getFullFieldName(), iterStmt);
} else {
NucleusLogger.GENERAL.debug("Note that query has field " + fpMmd.getFullFieldName() + " marked in the FetchPlan, yet this is currently not fetched by this query");
}
} else if (multifetchType.equalsIgnoreCase("join")) {
// Fetch container contents for all candidate owners
BulkFetchJoinHandler helper = new BulkFetchJoinHandler();
IteratorStatement iterStmt = helper.getStatementToBulkFetchField(candidateCmd, fpMmd, this, parameters, datastoreCompilation, options);
if (iterStmt != null) {
datastoreCompilation.setSCOIteratorStatement(fpMmd.getFullFieldName(), iterStmt);
} else {
NucleusLogger.GENERAL.debug("Note that query has field " + fpMmd.getFullFieldName() + " marked in the FetchPlan, yet this is currently not fetched by this query");
}
} else {
NucleusLogger.GENERAL.debug("Note that query has field " + fpMmd.getFullFieldName() + " marked in the FetchPlan, yet this is not fetched by this query.");
}
// TODO Continue this bulk fetch process to fields of fields that are fetched
}
} else if (RelationType.isRelationSingleValued(fpRelType)) {
// TODO Check for multivalued fields of this 1-1/N-1 field
}
}
}
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021084", getLanguage(), System.currentTimeMillis() - startTime));
}
}
use of org.datanucleus.FetchPlanForClass in project datanucleus-core by datanucleus.
the class StateManagerImpl method loadFieldValues.
// --------------------------- Load Field Methods --------------------------
/**
* Convenience method to load the passed field values.
* Loads the fields using any required fetch plan and calls dnPostLoad() as appropriate.
* @param fv Field Values to load (including any fetch plan to use when loading)
*/
public void loadFieldValues(FieldValues fv) {
// Fetch the required fields using any defined fetch plan
FetchPlanForClass origFetchPlan = myFP;
FetchPlan loadFetchPlan = fv.getFetchPlanForLoading();
if (loadFetchPlan != null) {
myFP = loadFetchPlan.getFetchPlanForClass(cmd);
}
boolean callPostLoad = myFP.isToCallPostLoadFetchPlan(this.loadedFields);
if (loadedFields.length == 0) {
// Class has no fields so since we are loading from scratch just call postLoad
callPostLoad = true;
}
fv.fetchFields(this);
if (callPostLoad && areFieldsLoaded(myFP.getMemberNumbers())) {
postLoad();
}
// Reinstate the original (PM) fetch plan
myFP = origFetchPlan;
}
use of org.datanucleus.FetchPlanForClass in project datanucleus-core by datanucleus.
the class StateManagerImpl method loadUnloadedFieldsOfClassInFetchPlan.
/**
* Fetchs from the database all currently unloaded fields in the actual fetch plan.
* Called by life-cycle transitions.
*/
public void loadUnloadedFieldsOfClassInFetchPlan(FetchPlan fetchPlan) {
FetchPlanForClass fpc = fetchPlan.getFetchPlanForClass(this.cmd);
int[] fieldNumbers = ClassUtils.getFlagsSetTo(loadedFields, fpc.getMemberNumbers(), false);
if (fieldNumbers != null && fieldNumbers.length > 0) {
boolean callPostLoad = fpc.isToCallPostLoadFetchPlan(this.loadedFields);
int[] unloadedFieldNumbers = loadFieldsFromLevel2Cache(fieldNumbers);
if (unloadedFieldNumbers != null) {
loadFieldsFromDatastore(unloadedFieldNumbers);
updateLevel2CacheForFields(unloadedFieldNumbers);
}
if (callPostLoad) {
postLoad();
}
}
}
use of org.datanucleus.FetchPlanForClass in project tests by datanucleus.
the class FetchPlanTest method testNestedFetchGroupsAgain.
/**
* Test nested fetch groups.
* TODO Merge this with the test above
*/
public void testNestedFetchGroupsAgain() {
FetchPlan fp = getFetchPlan();
PersistenceNucleusContextImpl nucleusCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataManager metaMgr = new JDOMetaDataManager(nucleusCtx);
// test parent with fetch-group = group1
AbstractClassMetaData cmd = metaMgr.getMetaDataForClass(FP1Sub.class, new ClassLoaderResolverImpl());
fp.addGroup("1");
fp.removeGroup(FetchPlan.DEFAULT);
FetchPlanForClass fpc = fp.getFetchPlanForClass(cmd);
int[] fieldsInFP = fpc.getMemberNumbers();
assertEquals("should have 2 fields in fetchplan", 2, fieldsInFP.length);
// check if fields are in the FP
BitSet fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
assertTrue("name should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("name")));
assertTrue("room should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("room")));
}
Aggregations