示例#1
0
  /*
   * @see org.eclipse.birt.data.engine.odi.IPreparedDSQuery#execute()
   */
  public IResultIterator execute(IEventHandler eventHandler) throws DataException {
    assert odaStatement != null;

    IResultIterator ri = null;

    this.setInputParameterBinding();

    IOdaDataSetDesign design = null;
    if (session.getDataSetCacheManager().getCurrentDataSetDesign() instanceof IOdaDataSetDesign)
      design = (IOdaDataSetDesign) session.getDataSetCacheManager().getCurrentDataSetDesign();

    if (session.getDataSetCacheManager().doesSaveToCache()) {
      int fetchRowLimit = 0;
      if (design != null) {
        fetchRowLimit =
            session.getDataSetCacheManager().getCurrentDataSetDesign().getRowFetchLimit();
      }

      int cacheCountConfig = 0;
      if (design.getFilters().isEmpty()) {
        cacheCountConfig = session.getDataSetCacheManager().getCacheCountConfig();
      }

      if (cacheCountConfig > 0) {
        if (fetchRowLimit != 0 && fetchRowLimit < cacheCountConfig) {

          odaStatement.setMaxRows(fetchRowLimit);
        } else {
          odaStatement.setMaxRows(cacheCountConfig);
        }
      } else {
        if (fetchRowLimit != 0) {
          odaStatement.setMaxRows(fetchRowLimit);
        }
      }
    }

    ICancellable queryCanceller = new OdaQueryCanceller(odaStatement, session.getStopSign());
    this.session.getCancelManager().register(queryCanceller);

    if (!session.getStopSign().isStopped()) {
      long startTime = System.currentTimeMillis();
      odaStatement.execute();
      long endTime = System.currentTimeMillis();
      if (logger.isLoggable(Level.FINE))
        logger.log(
            Level.FINE,
            "ODA query execution time: "
                + (endTime - startTime)
                + " ms;\n   Executed query: "
                + odaStatement.getEffectiveQueryText());
    }

    QueryContextVisitorUtil.populateEffectiveQueryText(qcv, odaStatement.getEffectiveQueryText());

    if (queryCanceller.collectException() != null) {
      if (!(queryCanceller.collectException().getCause() instanceof UnsupportedOperationException))
        throw queryCanceller.collectException();
    }

    this.session.getCancelManager().deregister(queryCanceller);

    ResultSet rs = null;

    if (design != null) {
      if (canAccessResultSetByName(design)) {
        try {

          rs = odaStatement.getResultSet(design.getPrimaryResultSetName());
        } catch (DataException e) {
          throw new DataException(
              ResourceConstants.ERROR_HAPPEN_WHEN_RETRIEVE_RESULTSET,
              design.getPrimaryResultSetName());
        }
      } else if (canAccessResultSetByNumber(design)) {
        try {
          rs = odaStatement.getResultSet(design.getPrimaryResultSetNumber());
        } catch (DataException e) {
          throw new DataException(
              ResourceConstants.ERROR_HAPPEN_WHEN_RETRIEVE_RESULTSET,
              design.getPrimaryResultSetNumber());
        }
      }
    }
    if (rs == null && !session.getStopSign().isStopped()) {
      rs = odaStatement.getResultSet();
    }

    // If we did not get a result set metadata at prepare() time, get it now
    if (resultMetadata == null) {
      List modelResultHints = design.getResultSetHints();
      resultMetadata = rs.getMetaData();
      if (resultMetadata == null) throw new DataException(ResourceConstants.METADATA_NOT_AVAILABLE);
      resultMetadata = mergeResultHint(modelResultHints, resultMetadata);
    }

    // Initialize CachedResultSet using the ODA result set
    if (session.getDataSetCacheManager().doesSaveToCache() == false) {
      if (((session.getEngineContext().getMode() == DataEngineContext.DIRECT_PRESENTATION
              || session.getEngineContext().getMode() == DataEngineContext.MODE_GENERATION))
          && this.getQueryDefinition() instanceof IQueryDefinition) {
        IQueryDefinition queryDefn = (IQueryDefinition) this.getQueryDefinition();

        Strategy strategy =
            QueryExecutionStrategyUtil.getQueryExecutionStrategy(
                this.session,
                queryDefn,
                queryDefn.getDataSetName() == null
                    ? null
                    : ((DataEngineImpl) this.session.getEngine())
                        .getDataSetDesign(queryDefn.getDataSetName()));
        if (strategy != Strategy.Complex) {
          SimpleResultSet simpleResult =
              new SimpleResultSet(
                  this,
                  rs,
                  resultMetadata,
                  eventHandler,
                  this.getGrouping(),
                  this.session,
                  strategy == Strategy.SimpleLookingFoward);

          return simpleResult.getResultSetIterator();
        }
      }

      ri = new CachedResultSet(this, resultMetadata, rs, eventHandler, session);
    } else
      ri =
          new CachedResultSet(
              this,
              resultMetadata,
              new DataSetToCache(rs, resultMetadata, session),
              eventHandler,
              session);

    if (ri != null) ((CachedResultSet) ri).setOdaResultSet(rs);

    return ri;
  }
示例#2
0
  /* (non-Javadoc)
   * @see org.eclipse.birt.data.engine.odi.IDataSourceQuery#prepare()
   */
  @SuppressWarnings("restriction")
  public IPreparedDSQuery prepare() throws DataException {
    if (odaStatement != null) throw new DataException(ResourceConstants.QUERY_HAS_PREPARED);

    // create and populate a query specification for preparing a statement
    populateQuerySpecification();

    if (this.querySpecificaton != null
        && this.querySpecificaton.getBaseQuery() instanceof CombinedQuery) {
      odaStatement = dataSource.prepareStatement(null, queryType, this.querySpecificaton);
    } else {
      odaStatement = dataSource.prepareStatement(queryText, queryType, this.querySpecificaton);
    }

    // Add custom properties to odaStatement
    addPropertiesToPreparedStatement();

    // Adds input and output parameter hints to odaStatement.
    // This step must be done before odaStatement.setColumnsProjection()
    // for some jdbc driver need to carry out a query execution before the metadata can be achieved
    // and only when the Parameters are successfully set the query execution can succeed.
    addParameterDefns();

    // Here the "max rows" means the max number of rows that can fetch from data source.
    odaStatement.setMaxRows(this.getRowFetchLimit());

    IOdaDataSetDesign design = null;
    if (session.getDataSetCacheManager().getCurrentDataSetDesign() instanceof IOdaDataSetDesign)
      design = (IOdaDataSetDesign) session.getDataSetCacheManager().getCurrentDataSetDesign();

    ICancellable queryCanceller = new OdaQueryCanceller(odaStatement, session.getStopSign());

    if (design != null) {
      if (canAccessResultSetByName(design)) {
        // Ordering is important for the following operations. Column hints
        // should be defined
        // after custom fields are declared (since hints may be given to
        // those custom fields).
        // Column projection comes last because it needs hints and
        // custom
        // column information
        addCustomFields(design.getPrimaryResultSetName(), odaStatement);
        addColumnHints(design.getPrimaryResultSetName(), odaStatement);

        if (this.projectedFields != null)
          odaStatement.setColumnsProjection(design.getPrimaryResultSetName(), this.projectedFields);
      } else if (canAccessResultSetByNumber(design)) {
        addCustomFields(design.getPrimaryResultSetNumber(), odaStatement);
        addColumnHints(design.getPrimaryResultSetNumber(), odaStatement);

        if (this.projectedFields != null)
          odaStatement.setColumnsProjection(
              design.getPrimaryResultSetNumber(), this.projectedFields);
      } else {
        this.session.getCancelManager().register(queryCanceller);
        if (!session.getStopSign().isStopped()) {
          prepareColumns();
        }
        this.session.getCancelManager().deregister(queryCanceller);
      }
    } else {
      this.session.getCancelManager().register(queryCanceller);
      if (!session.getStopSign().isStopped()) {
        prepareColumns();
      }
      this.session.getCancelManager().deregister(queryCanceller);
    }

    // If ODA can provide result metadata, get it now
    try {

      this.session.getCancelManager().register(queryCanceller);

      if (!session.getStopSign().isStopped())
        resultMetadata =
            getMetaData(
                (IOdaDataSetDesign) session.getDataSetCacheManager().getCurrentDataSetDesign(),
                odaStatement);
      if (design != null) {
        List modelResultHints = design.getResultSetHints();
        resultMetadata = mergeResultHint(modelResultHints, resultMetadata);
      }

      if (queryCanceller.collectException() != null) {
        if (!(queryCanceller.collectException().getCause()
            instanceof UnsupportedOperationException)) throw queryCanceller.collectException();
      }

      this.session.getCancelManager().deregister(queryCanceller);

    } catch (DataException e) {
      // Assume metadata not available at prepare time; ignore the exception
      resultMetadata = null;
    }

    return this;
  }