/**
  * INTERNAL: Return the value to be stored in the object's attribute. This value is determined by
  * the batchQuery. In this case, extract the result from the query.
  */
 public Object valueFromBatchQuery(
     ReadQuery batchQuery,
     AbstractRecord row,
     ObjectLevelReadQuery originalQuery,
     CacheKey parentCacheKey) {
   return getForeignReferenceMapping()
       .extractResultFromBatchQuery(
           batchQuery, parentCacheKey, row, originalQuery.getSession(), originalQuery);
 }
 /**
  * Initialize the query-based value holder.
  *
  * @param query The query that returns the object when executed.
  * @param row The row representation of the object.
  * @param mapping The mapping that is uses batch reading.
  */
 public BatchValueHolder(
     ReadQuery query,
     AbstractRecord row,
     ForeignReferenceMapping mapping,
     ObjectLevelReadQuery originalQuery,
     CacheKey parentCacheKey) {
   super(query, row, originalQuery.getSession());
   this.mapping = mapping;
   this.originalQuery = originalQuery;
   this.parentCacheKey = parentCacheKey;
 }
 /**
  * INTERNAL: Prepare the query from the prepared query. This allows a dynamic query to prepare
  * itself directly from a prepared query instance. This is used in the JPQL parse cache to allow
  * preparsed queries to be used to prepare dynamic queries. This only copies over properties that
  * are configured through JPQL.
  */
 public void prepareFromQuery(DatabaseQuery query) {
   super.prepareFromQuery(query);
   if (query.isReadAllQuery()) {
     ReadAllQuery readQuery = (ReadAllQuery) query;
     this.containerPolicy = readQuery.containerPolicy;
     if (readQuery.hasHierarchicalExpressions()) {
       this.orderSiblingsByExpressions = readQuery.getOrderSiblingsByExpressions();
       this.connectByExpression = readQuery.getConnectByExpression();
       this.startWithExpression = readQuery.getStartWithExpression();
     }
     if (readQuery.hasBatchReadAttributes()) {
       this.batchReadAttributeExpressions = readQuery.batchReadAttributeExpressions;
       this.batchReadMappingQueries = readQuery.batchReadMappingQueries;
       this.batchReadAttributes = readQuery.batchReadAttributes;
     }
   }
 }
  /** INTERNAL: Prepare the receiver for execution in a session. */
  protected void prepare() throws QueryException {
    if ((!isReportQuery()) && prepareFromCachedQuery()) {
      return;
    }
    super.prepare();

    getContainerPolicy().prepare(this, getSession());

    if (getContainerPolicy().overridesRead()) {
      return;
    }

    if (getDescriptor().isDescriptorForInterface()) {
      return;
    }

    prepareSelectAllRows();
    computeBatchReadMappingQueries();
  }
  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);
  }
  /**
   * INTERNAL: Return the value of the reference attribute or a value holder. Check whether the
   * mapping's attribute should be optimized through batch and joining.
   */
  public Object valueFromRow(
      AbstractRecord row,
      JoinedAttributeManager joinManager,
      ObjectBuildingQuery sourceQuery,
      AbstractSession executionSession)
      throws DatabaseException {
    if (((EISDescriptor) this.getDescriptor()).getDataFormat() == EISDescriptor.XML) {
      ((XMLRecord) row).setSession(executionSession);
    }

    ReadQuery targetQuery = getSelectionQuery();
    if (!isForeignKeyRelationship) {
      // if the source query is cascading then the target query must use the same settings
      if (targetQuery.isObjectLevelReadQuery()
          && (sourceQuery.shouldCascadeAllParts()
              || (sourceQuery.shouldCascadePrivateParts() && isPrivateOwned())
              || (sourceQuery.shouldCascadeByMapping() && this.cascadeRefresh))) {
        targetQuery = (ObjectLevelReadQuery) targetQuery.clone();
        ((ObjectLevelReadQuery) targetQuery)
            .setShouldRefreshIdentityMapResult(sourceQuery.shouldRefreshIdentityMapResult());
        targetQuery.setCascadePolicy(sourceQuery.getCascadePolicy());
        // CR #4365
        targetQuery.setQueryId(sourceQuery.getQueryId());
        // For queries that have turned caching off, such as aggregate collection, leave it off.
        if (targetQuery.shouldMaintainCache()) {
          targetQuery.setShouldMaintainCache(sourceQuery.shouldMaintainCache());
        }
      }

      return getIndirectionPolicy().valueFromQuery(targetQuery, row, sourceQuery.getSession());
    } else {
      if (getIndirectionPolicy().usesIndirection()) {
        EISOneToManyQueryBasedValueHolder valueholder =
            new EISOneToManyQueryBasedValueHolder(this, targetQuery, row, sourceQuery.getSession());
        return valueholder;
      } else {
        Vector subRows = getForeignKeyRows(row);

        if (subRows == null) {
          return null;
        }

        ContainerPolicy cp = this.getContainerPolicy();
        Object results = cp.containerInstance(subRows.size());

        for (int i = 0; i < subRows.size(); i++) {
          XMLRecord subRow = (XMLRecord) subRows.elementAt(i);
          subRow.setSession(executionSession);
          Object object =
              getIndirectionPolicy().valueFromQuery(targetQuery, subRow, sourceQuery.getSession());
          if (object instanceof Collection) {
            java.util.Iterator iter = ((Collection) object).iterator();
            while (iter.hasNext()) {
              cp.addInto(iter.next(), results, executionSession);
            }
          } else if (object instanceof java.util.Map) {
            java.util.Iterator iter = ((java.util.Map) object).values().iterator();
            while (iter.hasNext()) {
              cp.addInto(iter.next(), results, executionSession);
            }
          } else {
            cp.addInto(object, results, executionSession);
          }
        }
        if (cp.sizeFor(results) == 0) {
          return null;
        }
        return results;
      }
    }
  }
  /** INTERNAL: Prepare the receiver for execution in a session. */
  public void prepareForExecution() throws QueryException {
    super.prepareForExecution();

    getContainerPolicy().prepareForExecution();
  }
 public JPQLTestCase(ObjectLevelReadQuery theQuery) {
   this();
   this.theQuery = theQuery;
   this.ejbql =
       (((JPQLCallQueryMechanism) theQuery.getQueryMechanism()).getJPQLCall()).getEjbqlString();
 }