private void addColumnHints(int rsetNumber, PreparedStatement stmt) throws DataException { assert stmt != null; if (resultHints == null || resultHints.size() == 0) return; Iterator it = resultHints.iterator(); while (it.hasNext()) { ColumnHint colHint = prepareOdiHint((IDataSourceQuery.ResultFieldHint) it.next()); stmt.addColumnHint(rsetNumber, colHint); } }
/** * @param design * @param odaStatement * @return * @throws DataException */ private IResultClass getMetaData(IOdaDataSetDesign design, PreparedStatement odaStatement) throws DataException { IResultClass result = null; if (design != null) { if (canAccessResultSetByName(design)) { try { result = odaStatement.getMetaData(design.getPrimaryResultSetName()); } catch (DataException e) { throw new DataException( ResourceConstants.ERROR_HAPPEN_WHEN_RETRIEVE_RESULTSET, design.getPrimaryResultSetName()); } } else if (canAccessResultSetByNumber(design)) { try { result = odaStatement.getMetaData(design.getPrimaryResultSetNumber()); } catch (DataException e) { throw new DataException( ResourceConstants.ERROR_HAPPEN_WHEN_RETRIEVE_RESULTSET, design.getPrimaryResultSetNumber()); } } } if (result == null) result = odaStatement.getMetaData(); if (design != null) { List hintList = design.getResultSetHints(); for (int i = 0; i < hintList.size(); i++) { IColumnDefinition columnDefinition = (IColumnDefinition) hintList.get(i); for (int j = 1; j <= result.getFieldCount(); j++) { ResultFieldMetadata resultFieldMetadata = result.getFieldMetaData(j); if (columnDefinition.getColumnName().equals(resultFieldMetadata.getName())) { resultFieldMetadata.setAlias(columnDefinition.getAlias()); resultFieldMetadata.setAnalysisType(columnDefinition.getAnalysisType()); resultFieldMetadata.setAnalysisColumn(columnDefinition.getAnalysisColumn()); resultFieldMetadata.setIndexColumn(columnDefinition.isIndexColumn()); resultFieldMetadata.setCompressedColumn(columnDefinition.isCompressedColumn()); break; } } } } return result; }
private void addCustomFields(int rsetNumber, PreparedStatement stmt) throws DataException { if (this.customFields != null) { Iterator it = this.customFields.iterator(); while (it.hasNext()) { CustomField customField = (CustomField) it.next(); stmt.declareCustomColumn( rsetNumber, customField.getName(), DataType.getClass(customField.getDataType())); } } }
/** set input parameter bindings */ private void setInputParameterBinding() throws DataException { assert odaStatement != null; // set input parameter bindings Iterator inputParamValueslist = getInputParamValues().iterator(); while (inputParamValueslist.hasNext()) { ParameterBinding paramBind = (ParameterBinding) inputParamValueslist.next(); if (paramBind.getPosition() <= 0 || odaStatement.supportsNamedParameter()) { try { odaStatement.setParameterValue(paramBind.getName(), paramBind.getValue()); } catch (DataException e) { if (paramBind.getPosition() <= 0) { throw e; } else { odaStatement.setParameterValue(paramBind.getPosition(), paramBind.getValue()); } } } else { odaStatement.setParameterValue(paramBind.getPosition(), paramBind.getValue()); } } }
/** * Adds custom properties to prepared oda statement; use the same properties already set in * querySpec before prepare */ @SuppressWarnings("restriction") private void addPropertiesToPreparedStatement() throws DataException { if (this.querySpecificaton == null || this.querySpecificaton.getProperties().isEmpty()) return; // no properties to add assert odaStatement != null; Map<String, Object> propertyMap = this.querySpecificaton.getProperties(); Iterator<Entry<String, Object>> iter = propertyMap.entrySet().iterator(); while (iter.hasNext()) { Entry<String, Object> property = iter.next(); String value = (property.getValue() == null) ? null : property.getValue().toString(); odaStatement.setProperty(property.getKey(), value); } }
/** Adds input and output parameter hints to odaStatement */ private void addParameterDefns() throws DataException { assert odaStatement != null; if (this.parameterHints == null) return; // nothing to add // iterate thru the collection to add parameter hints Iterator it = this.parameterHints.iterator(); while (it.hasNext()) { ParameterHint parameterHint = (ParameterHint) it.next(); odaStatement.addParameterHint(parameterHint); // If the parameter is input parameter then add it to input value list. if (parameterHint.isInputMode()) { Object inputValue = getParameterInputValue(parameterHint); if (parameterHint.getPosition() <= 0 || odaStatement.supportsNamedParameter()) { this.setInputParamValue(parameterHint.getName(), parameterHint.getPosition(), inputValue); } else { this.setInputParamValue(parameterHint.getPosition(), inputValue); } } } this.setInputParameterBinding(); }
/* * @see org.eclipse.birt.data.engine.odi.IPreparedDSQuery#getParameterMetaData() */ public Collection getParameterMetaData() throws DataException { if (odaStatement == null) throw new DataException(ResourceConstants.QUERY_HAS_NOT_PREPARED); Collection odaParamsInfo = odaStatement.getParameterMetaData(); if (odaParamsInfo == null || odaParamsInfo.isEmpty()) return null; // iterates thru the most up-to-date collection, and // wraps each of the odaconsumer parameter metadata object ArrayList paramMetaDataList = new ArrayList(odaParamsInfo.size()); Iterator odaParamMDIter = odaParamsInfo.iterator(); while (odaParamMDIter.hasNext()) { org.eclipse.birt.data.engine.odaconsumer.ParameterMetaData odaMetaData = (org.eclipse.birt.data.engine.odaconsumer.ParameterMetaData) odaParamMDIter.next(); paramMetaDataList.add(new ParameterMetaData(odaMetaData)); } return paramMetaDataList; }
/* * @see org.eclipse.birt.data.engine.odi.IPreparedDSQuery#getParameterValue(java.lang.String) */ public Object getOutputParameterValue(String name) throws DataException { assert odaStatement != null; checkOutputParamNameValid(name); return odaStatement.getParameterValue(name); }
/* * @see org.eclipse.birt.data.engine.odi.IPreparedDSQuery#getParameterValue(int) */ public Object getOutputParameterValue(int index) throws DataException { assert odaStatement != null; int newIndex = getCorrectParamIndex(index); return odaStatement.getParameterValue(newIndex); }
/* * @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; }
private void prepareColumns() throws DataException { addCustomFields(odaStatement); addColumnHints(odaStatement); if (this.projectedFields != null) odaStatement.setColumnsProjection(this.projectedFields); }
private boolean canAccessResultSetByNumber(IOdaDataSetDesign design) throws DataException { return design.getPrimaryResultSetNumber() > 0 && odaStatement.supportsMultipleResultSets(); }
private boolean canAccessResultSetByName(IOdaDataSetDesign design) throws DataException { return design.getPrimaryResultSetName() != null && odaStatement.supportsNamedResults(); }
/* (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; }