/** * INTERNAL: * Ensure that the descriptor has been set. * @param session Current session. */ @Override public void checkDescriptor(AbstractSession session) throws QueryException { if (this.descriptor == null) { if (getReferenceClass() == null) { throw QueryException.referenceClassMissing(this); } ClassDescriptor referenceDescriptor = session.getDescriptor(getReferenceClass()); if (referenceDescriptor == null) { throw QueryException.descriptorIsMissing(getReferenceClass(), this); } setDescriptor(referenceDescriptor); } }
/** * INTERNAL: * Ensure that the descriptor has been set. */ @Override public void checkDescriptor(AbstractSession session) throws QueryException { if (this.descriptor == null) { if (getReferenceClass() == null) { throw QueryException.referenceClassMissing(this); } ClassDescriptor referenceDescriptor = session.getDescriptor(getReferenceClass()); if (referenceDescriptor == null) { throw QueryException.descriptorIsMissing(getReferenceClass(), this); } setDescriptor(referenceDescriptor); } }
/** * INTERNAL: * Ensure that the descriptor has been set. */ public void checkDescriptor(AbstractSession session) throws QueryException { if (this.descriptor == null) { if (getReferenceClass() == null) { throw QueryException.referenceClassMissing(this); } ClassDescriptor referenceDescriptor = session.getDescriptor(getReferenceClass()); if (referenceDescriptor == null) { throw QueryException.descriptorIsMissing(getReferenceClass(), this); } setDescriptor(referenceDescriptor); } }
/** * INTERNAL: * Compute a consistent hash-code for the expression. * This is used to allow dynamic expression's SQL to be cached. */ public int hashCode() { if (!isExpressionQuery()) { return super.hashCode(); } int hashCode = 32; if (getReferenceClass() != null) { hashCode = hashCode + getReferenceClass().hashCode(); } if (getSelectionCriteria() != null) { hashCode = hashCode + getSelectionCriteria().hashCode(); } return hashCode; }
/** * Read a single row from the database. * This is used from query mechanism during an abstract-multiple table read. */ public AbstractRecord selectOneRowFromConcreteTable() throws DatabaseException { ObjectLevelReadQuery query = (ObjectLevelReadQuery)this.query; // PERF: First check the subclass calls cache for the prepared call. DatabaseCall call = query.getConcreteSubclassCalls().get(query.getReferenceClass()); if (call == null) { setSQLStatement(buildConcreteSelectStatement()); // Must also build the call. super.prepareSelectOneRow(); query.getConcreteSubclassCalls().put(query.getReferenceClass(), (DatabaseCall)this.call); } else { setCall(call); } return super.selectOneRow(); }
/** * INTERNAL: * Iterate through a list of joined expressions and add the fields they represent to a list * of fields. */ protected void addSelectionFieldsForJoinedExpressions(List fields, List joinedExpressions) { for (int index = 0; index < joinedExpressions.size(); index++) { ObjectExpression objectExpression = (ObjectExpression)joinedExpressions.get(index); // Expression may not have been initialized. ExpressionBuilder builder = objectExpression.getBuilder(); builder.setSession(getSession().getRootSession(null)); builder.setQueryClass(getReferenceClass()); ClassDescriptor descriptor = objectExpression.getMapping().getReferenceDescriptor(); fields.addAll(descriptor.getAllFields()); } }
/** * INTERNAL: * Compute the cache batched attributes. * Used to recompute batched attributes for nested aggregate queries. */ public void computeBatchReadAttributes() { List<Expression> batchReadAttributeExpressions = getBatchReadAttributeExpressions(); this.batchFetchPolicy.setAttributes(new ArrayList(batchReadAttributeExpressions.size())); int size = batchReadAttributeExpressions.size(); for (int index = 0; index < size; index++) { ObjectExpression objectExpression = (ObjectExpression)batchReadAttributeExpressions.get(index); // Expression may not have been initialized. ExpressionBuilder builder = objectExpression.getBuilder(); if (builder.getSession() == null) { builder.setSession(getSession().getRootSession(null)); } if (builder.getQueryClass() == null) { builder.setQueryClass(getReferenceClass()); } // PERF: Cache join attribute names. ObjectExpression baseExpression = objectExpression; while (!baseExpression.getBaseExpression().isExpressionBuilder()) { baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); } this.batchFetchPolicy.getAttributes().add(baseExpression.getName()); } }
/** * INTERNAL: * Compute the cache batched attributes. * Used to recompute batched attributes for nested aggregate queries. */ public void computeBatchReadAttributes() { List<Expression> batchReadAttributeExpressions = getBatchReadAttributeExpressions(); this.batchFetchPolicy.setAttributes(new ArrayList(batchReadAttributeExpressions.size())); int size = batchReadAttributeExpressions.size(); for (int index = 0; index < size; index++) { ObjectExpression objectExpression = (ObjectExpression)batchReadAttributeExpressions.get(index); // Expression may not have been initialized. ExpressionBuilder builder = objectExpression.getBuilder(); if (builder.getSession() == null) { builder.setSession(getSession().getRootSession(null)); } if (builder.getQueryClass() == null) { builder.setQueryClass(getReferenceClass()); } // PERF: Cache join attribute names. ObjectExpression baseExpression = objectExpression; while (!baseExpression.getBaseExpression().isExpressionBuilder()) { baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); } this.batchFetchPolicy.getAttributes().add(baseExpression.getName()); } }
/** * INTERNAL: * In the case of EJBQL or query by example, an expression needs to be * generated. Build the required expression. */ public void buildSelectionCriteria(AbstractSession session) { if (isParsed() || (getExampleObject() == null)) { return; } ObjectLevelReadQuery query = (ObjectLevelReadQuery)getQuery(); query.checkDescriptor(session); QueryByExamplePolicy policy = getQueryByExamplePolicy(); if (policy == null) { policy = new QueryByExamplePolicy(); } if (query.getReferenceClass().isInstance(getExampleObject())) { Expression exampleExpression = query.getDescriptor().getObjectBuilder().buildExpressionFromExample(getExampleObject(), policy, query.getExpressionBuilder(), new IdentityHashMap(), session); if (getSelectionCriteria() != null) { setSelectionCriteria(getSelectionCriteria().and(exampleExpression)); } else { setSelectionCriteria(exampleExpression); } } else { throw QueryException.exampleAndReferenceObjectClassMismatch(getExampleObject().getClass(), query.getReferenceClass(), query); } setIsParsed(true); }
/** * INTERNAL: * In the case of EJBQL or query by example, an expression needs to be * generated. Build the required expression. */ public void buildSelectionCriteria(AbstractSession session) { if (isParsed() || (getExampleObject() == null)) { return; } ObjectLevelReadQuery query = (ObjectLevelReadQuery)getQuery(); query.checkDescriptor(session); QueryByExamplePolicy policy = getQueryByExamplePolicy(); if (policy == null) { policy = new QueryByExamplePolicy(); } if (query.getReferenceClass().isInstance(getExampleObject())) { Expression exampleExpression = query.getDescriptor().getObjectBuilder().buildExpressionFromExample(getExampleObject(), policy, query.getExpressionBuilder(), new IdentityHashMap(), session); if (getSelectionCriteria() != null) { setSelectionCriteria(getSelectionCriteria().and(exampleExpression)); } else { setSelectionCriteria(exampleExpression); } } else { throw QueryException.exampleAndReferenceObjectClassMismatch(getExampleObject().getClass(), query.getReferenceClass(), query); } setIsParsed(true); }
/** * INTERNAL: * In the case of EJBQL or query by example, an expression needs to be * generated. Build the required expression. */ public void buildSelectionCriteria(AbstractSession session) { if (isParsed() || (getExampleObject() == null)) { return; } ObjectLevelReadQuery query = (ObjectLevelReadQuery)getQuery(); query.checkDescriptor(session); QueryByExamplePolicy policy = getQueryByExamplePolicy(); if (policy == null) { policy = new QueryByExamplePolicy(); } if (query.getReferenceClass().isInstance(getExampleObject())) { Expression exampleExpression = query.getDescriptor().getObjectBuilder().buildExpressionFromExample(getExampleObject(), policy, query.getExpressionBuilder(), new IdentityHashMap(), session); if (getSelectionCriteria() != null) { setSelectionCriteria(getSelectionCriteria().and(exampleExpression)); } else { setSelectionCriteria(exampleExpression); } } else { throw QueryException.exampleAndReferenceObjectClassMismatch(getExampleObject().getClass(), query.getReferenceClass(), query); } setIsParsed(true); }
/** * A subclass should implement this method if it wants non default behavior. */ protected void initializeSelectionQuery(AbstractSession session) throws DescriptorException { if (((ObjectLevelReadQuery)getSelectionQuery()).getReferenceClass() == null) { throw DescriptorException.referenceClassNotSpecified(this); } getSelectionQuery().setName(getAttributeName()); getSelectionQuery().setDescriptor(getReferenceDescriptor()); getSelectionQuery().setSourceMapping(this); }
/** * A subclass should implement this method if it wants non default behavior. */ protected void initializeSelectionQuery(AbstractSession session) throws DescriptorException { if (((ObjectLevelReadQuery)getSelectionQuery()).getReferenceClass() == null) { throw DescriptorException.referenceClassNotSpecified(this); } getSelectionQuery().setName(getAttributeName()); getSelectionQuery().setDescriptor(getReferenceDescriptor()); getSelectionQuery().setSourceMapping(this); if (getSelectionQuery().getPartitioningPolicy() == null) { getSelectionQuery().setPartitioningPolicy(getPartitioningPolicy()); } }
private void populateReadQueryInternal(ObjectLevelReadQuery objectQuery, GenerationContext generationContext) { // Get the reference class if it does not exist. This is done // for dynamic queries in EJBQL 3.0 if (objectQuery.getReferenceClass() == null) { // Adjust the reference class if necessary adjustReferenceClassForQuery(objectQuery, generationContext); } // Initialize the base expression in the generation context initBaseExpression(objectQuery, generationContext); // Validate parse tree validate(generationContext.getSession(), getClassLoader()); // Apply the query node to the query (this will be a SelectNode for a read query) applyQueryNodeToQuery(objectQuery, generationContext); // Verify the SELECT is valid (valid alias, etc) verifySelect(objectQuery, generationContext); // This is what it's all about... setSelectionCriteriaForQuery(objectQuery, generationContext); // Add any ordering addOrderingToQuery(objectQuery, generationContext); // Add any grouping addGroupingToQuery(objectQuery, generationContext); // Add having addHavingToQuery(objectQuery, generationContext); // Add non fetch joined variables addNonFetchJoinAttributes(objectQuery, generationContext); }
if (objectQuery.getReferenceClass() == null) {
/** * A subclass should implement this method if it wants non default behavior. */ protected void initializeSelectionQuery(AbstractSession session) throws DescriptorException { if (((ObjectLevelReadQuery)getSelectionQuery()).getReferenceClass() == null) { throw DescriptorException.referenceClassNotSpecified(this); } getSelectionQuery().setName(getAttributeName()); getSelectionQuery().setDescriptor(getReferenceDescriptor()); getSelectionQuery().setSourceMapping(this); if (getSelectionQuery().getPartitioningPolicy() == null) { getSelectionQuery().setPartitioningPolicy(getPartitioningPolicy()); } }
void visitAbstractFromClause(AbstractFromClause expression) { // Set the ExpressionBuilder Expression baseExpression = queryContext.getBaseExpression(); ExpressionBuilder expressionBuilder = baseExpression.getBuilder(); query.setExpressionBuilder(expressionBuilder); // Set the reference class if it's not set if (query.getReferenceClass() == null) { query.setReferenceClass(expressionBuilder.getQueryClass()); query.changeDescriptor(queryContext.getSession()); } // Add join expressions to the query (but not the join fetch expressions) JoinVisitor visitor = new JoinVisitor(); expression.accept(visitor); // Visit the AS OF clause if (expression.hasAsOfClause()) { expression.getAsOfClause().accept(this); } // Visit the hierarchical clause if (expression.hasHierarchicalQueryClause()) { expression.getHierarchicalQueryClause().accept(this); } }
void visitAbstractFromClause(AbstractFromClause expression) { // Set the ExpressionBuilder Expression baseExpression = queryContext.getBaseExpression(); ExpressionBuilder expressionBuilder = baseExpression.getBuilder(); query.setExpressionBuilder(expressionBuilder); // Set the reference class if it's not set if (query.getReferenceClass() == null) { query.setReferenceClass(expressionBuilder.getQueryClass()); query.changeDescriptor(queryContext.getSession()); } // Add join expressions to the query (but not the join fetch expressions) JoinVisitor visitor = new JoinVisitor(); expression.accept(visitor); // Visit the AS OF clause if (expression.hasAsOfClause()) { expression.getAsOfClause().accept(this); } // Visit the hierarchical clause if (expression.hasHierarchicalQueryClause()) { expression.getHierarchicalQueryClause().accept(this); } }
ParseTreeContext context = generationContext.getParseTreeContext(); if (context.isRangeVariable(variable)) { Class referenceClass = theQuery.getReferenceClass();
ParseTreeContext context = generationContext.getParseTreeContext(); if (context.isRangeVariable(variable)) { Class referenceClass = theQuery.getReferenceClass();