/** * PUBLIC: * When using Query By Example, an instance of QueryByExamplePolicy is used to customize the query. * The policy is useful when special operations are to be used for comparisons (notEqual, lessThan, * greaterThan, like etc.), when a certain value is to be ignored, or when dealing with nulls. */ public QueryByExamplePolicy getQueryByExamplePolicy() { if (getQueryMechanism().isQueryByExampleMechanism()) { return ((QueryByExampleMechanism)getQueryMechanism()).getQueryByExamplePolicy(); } else { return null; } }
/** * PUBLIC: * When using Query By Example, an instance of QueryByExamplePolicy is used to customize the query. * The policy is useful when special operations are to be used for comparisons (notEqual, lessThan, * greaterThan, like etc.), when a certain value is to be ignored, or when dealing with nulls. */ public QueryByExamplePolicy getQueryByExamplePolicy() { if (getQueryMechanism().isQueryByExampleMechanism()) { return ((QueryByExampleMechanism)getQueryMechanism()).getQueryByExamplePolicy(); } else { return null; } }
/** * PUBLIC: * This method returns the current example object. The "example" object is an actual domain object, provided * by the client, from which an expression is generated. * This expression is used for a query of all objects from the same class, that match the attribute values of * the "example" object. */ public Object getExampleObject() { if (getQueryMechanism().isQueryByExampleMechanism()) { return ((QueryByExampleMechanism)getQueryMechanism()).getExampleObject(); } else { return null; } }
/** * PUBLIC: * This method returns the current example object. The "example" object is an actual domain object, provided * by the client, from which an expression is generated. * This expression is used for a query of all objects from the same class, that match the attribute values of * the "example" object. */ public Object getExampleObject() { if (getQueryMechanism().isQueryByExampleMechanism()) { return ((QueryByExampleMechanism)getQueryMechanism()).getExampleObject(); } else { return null; } }
/** * PUBLIC: * When using Query By Example, an instance of QueryByExamplePolicy is used to customize the query. * The policy is useful when special operations are to be used for comparisons (notEqual, lessThan, * greaterThan, like etc.), when a certain value is to be ignored, or when dealing with nulls. */ public QueryByExamplePolicy getQueryByExamplePolicy() { if (getQueryMechanism().isQueryByExampleMechanism()) { return ((QueryByExampleMechanism)getQueryMechanism()).getQueryByExamplePolicy(); } else { return null; } }
/** * PUBLIC: * This method returns the current example object. The "example" object is an actual domain object, provided * by the client, from which an expression is generated. * This expression is used for a query of all objects from the same class, that match the attribute values of * the "example" object. */ public Object getExampleObject() { if (getQueryMechanism().isQueryByExampleMechanism()) { return ((QueryByExampleMechanism)getQueryMechanism()).getExampleObject(); } else { return null; } }
/** * PUBLIC: * The QueryByExamplePolicy, is a useful to customize the query when Query By Example is used. * The policy will control what attributes should, or should not be included in the query. * When dealing with nulls, using special operations (notEqual, lessThan, like, etc.) * for comparison, or choosing to include certain attributes at all times, it is useful to modify * the policy accordingly. * <p>Once a query is executed you must make an explicit call to setQueryByExamplePolicy * when changing the policy, so the query will know to prepare itself again. * <p>There is a caution to setting both a selection criteria and an example object: * If you set the policy after execution you must also reset the selection criteria. * (This is because after execution the original criteria and Query By Example criteria are fused together, * and the former cannot be easily recovered). */ public void setQueryByExamplePolicy(QueryByExamplePolicy queryByExamplePolicy) { if (!getQueryMechanism().isQueryByExampleMechanism()) { setQueryMechanism(new QueryByExampleMechanism(this, getSelectionCriteria())); ((QueryByExampleMechanism)getQueryMechanism()).setQueryByExamplePolicy(queryByExamplePolicy); setIsPrepared(false); } else { ((QueryByExampleMechanism)getQueryMechanism()).setQueryByExamplePolicy(queryByExamplePolicy); // Must allow the query to be prepared again. if (isPrepared() || (!shouldPrepare() && ((QueryByExampleMechanism)getQueryMechanism()).isParsed())) { ((QueryByExampleMechanism)getQueryMechanism()).setIsParsed(false); setSelectionCriteria(null); // setIsPrepared(false) triggered by previous. } } setIsPrePrepared(false); }
/** * PUBLIC: * Specify the foreign-reference mapped attribute to be optimized in this query. * The query will execute normally, however when any of the batched parts is accessed, * the parts will all be read in a single query, * this allows all of the data required for the parts to be read in a single query instead of (n) queries. * This should be used when the application knows that it requires the part for all of the objects being read. * This can be used for one-to-one, one-to-many, many-to-many and direct collection mappings. * * The use of the expression allows for nested batch reading to be expressed. * <p>Example: query.addBatchReadAttribute(query.getExpressionBuilder().get("policies").get("claims")) * * @see #setBatchFetchType(BatchFetchType) * @see ObjectLevelReadQuery#addJoinedAttribute(String) */ public void addBatchReadAttribute(Expression attributeExpression) { if (! getQueryMechanism().isExpressionQueryMechanism()){ throw QueryException.batchReadingNotSupported(this); } getBatchReadAttributeExpressions().add(attributeExpression); setIsPrepared(false); }
/** * PUBLIC: * Specify the foreign-reference mapped attribute to be optimized in this query. * The query will execute normally, however when any of the batched parts is accessed, * the parts will all be read in a single query, * this allows all of the data required for the parts to be read in a single query instead of (n) queries. * This should be used when the application knows that it requires the part for all of the objects being read. * This can be used for one-to-one, one-to-many, many-to-many and direct collection mappings. * * The use of the expression allows for nested batch reading to be expressed. * <p>Example: query.addBatchReadAttribute(query.getExpressionBuilder().get("policies").get("claims")) * * @see #setBatchFetchType(BatchFetchType) * @see ObjectLevelReadQuery#addJoinedAttribute(String) */ public void addBatchReadAttribute(Expression attributeExpression) { if (! getQueryMechanism().isExpressionQueryMechanism()){ throw QueryException.batchReadingNotSupported(this); } getBatchReadAttributeExpressions().add(attributeExpression); setIsPrepared(false); }
/** * Initialize the expression builder which should be used for this query. If * there is a where clause, use its expression builder, otherwise * generate one and cache it. This helps avoid unnecessary rebuilds. */ protected void initializeDefaultBuilder() { DatabaseQueryMechanism mech = getQueryMechanism(); if (mech.isExpressionQueryMechanism() && ((ExpressionQueryMechanism)mech).getExpressionBuilder() != null) { this.defaultBuilder = ((ExpressionQueryMechanism)mech).getExpressionBuilder(); if (this.defaultBuilder.getQueryClass() != null && !this.defaultBuilder.getQueryClass().equals(this.referenceClass)){ this.defaultBuilder = new ExpressionBuilder(); } return; } this.defaultBuilder = new ExpressionBuilder(); }
/** * Initialize the expression builder which should be used for this query. If * there is a where clause, use its expression builder, otherwise * generate one and cache it. This helps avoid unnecessary rebuilds. */ protected void initializeDefaultBuilder() { DatabaseQueryMechanism mech = getQueryMechanism(); if (mech.isExpressionQueryMechanism() && ((ExpressionQueryMechanism)mech).getExpressionBuilder() != null) { this.defaultBuilder = ((ExpressionQueryMechanism)mech).getExpressionBuilder(); if (this.defaultBuilder.getQueryClass() != null && !this.defaultBuilder.getQueryClass().equals(this.referenceClass)){ this.defaultBuilder = new ExpressionBuilder(); } return; } this.defaultBuilder = new ExpressionBuilder(); }
/** * Initialize the expression builder which should be used for this query. If * there is a where clause, use its expression builder, otherwise * generate one and cache it. This helps avoid unnecessary rebuilds. */ protected void initializeDefaultBuilder() { DatabaseQueryMechanism mech = getQueryMechanism(); if (mech.isExpressionQueryMechanism() && ((ExpressionQueryMechanism)mech).getExpressionBuilder() != null) { this.defaultBuilder = ((ExpressionQueryMechanism)mech).getExpressionBuilder(); if (this.defaultBuilder.getQueryClass() != null && !this.defaultBuilder.getQueryClass().equals(this.referenceClass)){ this.defaultBuilder = new ExpressionBuilder(); } return; } this.defaultBuilder = new ExpressionBuilder(); }
/** * {@inheritDoc} */ @Override public void visit(SelectStatement expression) { ObjectLevelReadQuery query = queryContext.getDatabaseQuery(); // Create and prepare the query if (query == null) { query = buildReadAllQuery(expression); queryContext.setDatabasQuery(query); query.setJPQLString(jpqlQuery); ((JPQLCallQueryMechanism) query.getQueryMechanism()).getJPQLCall().setIsParsed(true); } // Now populate it expression.accept(buildVisitor(query)); }
/** * {@inheritDoc} */ @Override public void visit(SelectStatement expression) { ObjectLevelReadQuery query = queryContext.getDatabaseQuery(); // Create and prepare the query if (query == null) { query = buildReadAllQuery(expression); queryContext.setDatabasQuery(query); query.setJPQLString(jpqlQuery); ((JPQLCallQueryMechanism) query.getQueryMechanism()).getJPQLCall().setIsParsed(true); } // Now populate it expression.accept(buildVisitor(query)); }
/** * INTERNAL: * Select all rows from a abstract table descriptor. * This is accomplished by selecting for all of the concrete classes and then merging the rows. * This does not optimize using type select, as the type information is not known. * @return vector containing database rows. * @exception DatabaseException - an error has occurred on the database. */ protected Vector selectAllRowUsingCustomMultipleTableSubclassRead(ObjectLevelReadQuery query) throws DatabaseException { Vector rows = new Vector(); // CR#3701077, it must either have a filter only instances expression, or not have subclasses. // This method recurses, so even though this is only called when shouldReadSubclasses is true, it may be false for subclasses. if ((getOnlyInstancesExpression() != null) || (! shouldReadSubclasses())) { ObjectLevelReadQuery concreteQuery = (ObjectLevelReadQuery)query.clone(); concreteQuery.setReferenceClass(getDescriptor().getJavaClass()); concreteQuery.setDescriptor(getDescriptor()); Vector concreteRows = ((ExpressionQueryMechanism)concreteQuery.getQueryMechanism()).selectAllRowsFromConcreteTable(); rows = Helper.concatenateVectors(rows, concreteRows); } // Recursively collect all rows from all concrete children and their children. // If this descriptor did not have a child with its own table, then the concrete select // would have selected them all. if (hasMultipleTableChild() || !shouldReadSubclasses()) { for (ClassDescriptor concreteDescriptor : getChildDescriptors()) { Vector concreteRows = concreteDescriptor.getInheritancePolicy().selectAllRowUsingCustomMultipleTableSubclassRead(query); rows = Helper.concatenateVectors(rows, concreteRows); } } return rows; }
/** * INTERNAL: * Check if the query is cached and prepare from it. * Return true if the query was cached. */ protected boolean prepareFromCachedQuery() { // PERF: Check if the equivalent expression query has already been prepared. // Only allow queries with default properties to be cached. boolean isCacheable = isExpressionQuery() && (!getQueryMechanism().isJPQLCallQueryMechanism()) && isDefaultPropertiesQuery() && (!getSession().isHistoricalSession()); DatabaseQuery cachedQuery = null; if (isCacheable) { cachedQuery = this.descriptor.getQueryManager().getCachedExpressionQuery(this); } else { return false; } if ((cachedQuery != null) && cachedQuery.isPrepared()) { prepareFromQuery(cachedQuery); setIsPrepared(true); return true; } this.descriptor.getQueryManager().putCachedExpressionQuery(this); this.isCachedExpressionQuery = true; this.isExecutionClone = false; return false; }
/** * INTERNAL: * Check if the query is cached and prepare from it. * Return true if the query was cached. */ protected boolean prepareFromCachedQuery() { // PERF: Check if the equivalent expression query has already been prepared. // Only allow queries with default properties to be cached. boolean isCacheable = isExpressionQuery() && (!getQueryMechanism().isJPQLCallQueryMechanism()) && isDefaultPropertiesQuery() && (!getSession().isHistoricalSession()); DatabaseQuery cachedQuery = null; if (isCacheable) { cachedQuery = this.descriptor.getQueryManager().getCachedExpressionQuery(this); } else { return false; } if ((cachedQuery != null) && cachedQuery.isPrepared()) { prepareFromQuery(cachedQuery); setIsPrepared(true); return true; } this.descriptor.getQueryManager().putCachedExpressionQuery(this); this.isExecutionClone = false; return false; }
/** * INTERNAL: * Return a select statement that will be used to query the class indicators required to query. * This is used in the abstract-multiple read. */ public SQLSelectStatement buildClassIndicatorSelectStatement(ObjectLevelReadQuery query) { SQLSelectStatement selectStatement; selectStatement = new SQLSelectStatement(); selectStatement.useDistinct(); selectStatement.addTable(classIndicatorField.getTable()); selectStatement.addField(getClassIndicatorField()); // 2612538 - the default size of Map (32) is appropriate Map clonedExpressions = new IdentityHashMap(); selectStatement.setWhereClause(((ExpressionQueryMechanism)query.getQueryMechanism()).buildBaseSelectionCriteria(false, clonedExpressions)); appendWithAllSubclassesExpression(selectStatement); selectStatement.setTranslationRow(query.getTranslationRow()); if (query.isReadAllQuery() && ((ReadAllQuery)query).hasHierarchicalExpressions()) { ReadAllQuery readAllQuery = (ReadAllQuery)query; selectStatement.setHierarchicalQueryExpressions(readAllQuery.getStartWithExpression(), readAllQuery.getConnectByExpression(), readAllQuery.getOrderSiblingsByExpressions()); } selectStatement.setHintString(query.getHintString()); selectStatement.normalize(query.getSession(), getDescriptor(), clonedExpressions); return selectStatement; }
/** * INTERNAL: * Return a select statement that will be used to query the class indicators required to query. * This is used in the abstract-multiple read. */ public SQLSelectStatement buildClassIndicatorSelectStatement(ObjectLevelReadQuery query) { SQLSelectStatement selectStatement; selectStatement = new SQLSelectStatement(); selectStatement.useDistinct(); selectStatement.addTable(classIndicatorField.getTable()); selectStatement.addField(getClassIndicatorField()); // 2612538 - the default size of Map (32) is appropriate Map clonedExpressions = new IdentityHashMap(); selectStatement.setWhereClause(((ExpressionQueryMechanism)query.getQueryMechanism()).buildBaseSelectionCriteria(false, clonedExpressions)); appendWithAllSubclassesExpression(selectStatement); selectStatement.setTranslationRow(query.getTranslationRow()); if (query.isReadAllQuery() && ((ReadAllQuery)query).hasHierarchicalExpressions()) { ReadAllQuery readAllQuery = (ReadAllQuery)query; selectStatement.setHierarchicalQueryExpressions(readAllQuery.getStartWithExpression(), readAllQuery.getConnectByExpression(), readAllQuery.getOrderSiblingsByExpressions(), readAllQuery.getDirection()); } selectStatement.setHintString(query.getHintString()); selectStatement.normalize(query.getSession(), getDescriptor(), clonedExpressions); return selectStatement; }
/** * INTERNAL: * Return a select statement that will be used to query the class indicators required to query. * This is used in the abstract-multiple read. */ public SQLSelectStatement buildClassIndicatorSelectStatement(ObjectLevelReadQuery query) { SQLSelectStatement selectStatement; selectStatement = new SQLSelectStatement(); selectStatement.useDistinct(); selectStatement.addTable(classIndicatorField.getTable()); selectStatement.addField(getClassIndicatorField()); // 2612538 - the default size of Map (32) is appropriate Map clonedExpressions = new IdentityHashMap(); selectStatement.setWhereClause(((ExpressionQueryMechanism)query.getQueryMechanism()).buildBaseSelectionCriteria(false, clonedExpressions)); appendWithAllSubclassesExpression(selectStatement); selectStatement.setTranslationRow(query.getTranslationRow()); if (query.isReadAllQuery() && ((ReadAllQuery)query).hasHierarchicalExpressions()) { ReadAllQuery readAllQuery = (ReadAllQuery)query; selectStatement.setHierarchicalQueryExpressions(readAllQuery.getStartWithExpression(), readAllQuery.getConnectByExpression(), readAllQuery.getOrderSiblingsByExpressions(), readAllQuery.getDirection()); } selectStatement.setHintString(query.getHintString()); selectStatement.normalize(query.getSession(), getDescriptor(), clonedExpressions); return selectStatement; }