cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Mansfield <dmansfi...@sbcglobal.net>
Subject Re: Using Datacontext.performIteratedQuery() and ResultIterator
Date Sat, 26 Mar 2011 17:47:14 GMT
Hi Andrus:

Andrus Adamchik wrote:
> Hi Dave,
>
> By "object context" you don't mean ObjectContext, but rather the type of the result,
right?
>   
Yes, this is correct.
> 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? 
>   
I believe so: My problem arises from a base class for "serializing" 
database entities, and the kids being responsible (via the writeheader() 
and writeline() methods - which are abstract at the base class level) 
for handling the specifics of dealing with particular DB entities. As 
long as the queries are small, no problem. When they get huge, I have to 
fallback to retrieving datarows, then converting to per-DB-entity types 
of objects.

I delegated the initial, "convert from datarow to dataobject" at the 
base class level - that's where I hit the snag of some query results 
returning scalars, DataRows or Object[]. I will factor your info into my 
design and see how it works out.

thank you for the feedback!

-- Dave
> 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