cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrus Adamchik <and...@objectstyle.org>
Subject Re: Using Datacontext.performIteratedQuery() and ResultIterator
Date Tue, 22 Mar 2011 10:02:33 GMT
Hi Dave,

By "object context" you don't mean ObjectContext, but rather the type of the result, right?

If you need to determine result type dynamically, you can look at query metadata:

QueryMetadata md = query.getMetaData(context.getEntityResolver());
List<Object> rsMapping = md.getResultSetMapping();

The returned list contains instances of EntityResultSegment or ScalarResultSegment, so you
ca make sense of the result format. Not sure if this was your question here? 

Andrus


On Mar 22, 2011, at 6:43 AM, Dave Mansfield wrote:

> Hello -
> 
> I'm having a tough time understanding how to use the ResultItator properly.  Reading
the 3.0.1 Javadocs, it reports:
> 
> Result "rows", depending on the query, may be represented as scalar values, DataRows,
or Object[] arrays containing a mix of scalars and DataRows.
> 
> In the performance tuning example, the guide shows:
> 
> // ResultIterator operations all throw checked CayenneException
> try {
>  // special "performIteratedQuery" method is used
>  it = context.performIteratedQuery(q);
> 
>  while(it.hasNextRow()) {
>     // ResultIterator always returns data rows      Map row = (Map) it.nextRow();
>          // do something with the row...
>     ...
>  }
> }
> 
> 
> How can one pre-determine the object context being returned from a ResultIterator.nextRow()
call?  My code depends on a base class applying object-specific behavior to serialize queries
to another context.. One pass a scalar is returned; yet another pass I have a Map returned..
> 
> Tks!
> 
> Code snippet:
> 
>     ResultIterator resultIter = null;
>     query.setPageSize(mPageSize);
>     query.setFetchingDataRows(true);
>     long rowCnt = 0;
>     try {
>        resultIter = mDataCtx.performIteratedQuery(query);
>     } catch (CayenneException e) {
>        mLogger.log(Level.FATAL, "Query failed!", e);
>        return;
>     }
>     boolean needHeader = true;
>     try {
>        while ( resultIter.hasNextRow() ) {
>           DataRow row = (DataRow)resultIter.nextRow();
>           rowCnt++;
>           if ( needHeader ) {
>              this.writeHeader(row, errors);
>           }
>           this.writeLine(row, errors);
>        }
>        resultIter.close();
>     } catch (CayenneException e) {
>        mLogger.log(Level.FATAL, "Iterating through query failed!", e);
>        return;
>     }
>     finally {
>        try { if ( resultIter != null ) { resultIter.close(); } } catch (CayenneException
e) { /* nothing */ }
>     }
>  }
> 
> 


Mime
View raw message