// cursor not yet created
switch(fetchType.fetchType){
case ITER:
default:
{
PreparedQuery pq = prepare(query);
if(pag.isPaginating()){
// in case of pagination, we need to allow asynchronous calls such as:
// QueryAsync<MyClass> query = pm.createQuery(MyClass).paginate(5).stateful().order("name");
// SienaFuture<Iterable<MyClass>> future1 = query.iter();
// SienaFuture<Iterable<MyClass>> future2 = query.nextPage().iter();
// Iterable<MyClass> it = future1.get().iterator();
// while(it.hasNext()) { // do it }
// it = future2.get().iterator();
// while(it.hasNext()) { // do it }
// so we can't use the asQueryResultIterable as the cursor is not moved to the end of the current page
// but moved at each call of iterable.iterator().next()
// thus we use the List in this case to be able to move directly to the next page with cursors
QueryResultList<Entity> entities = pq.asQueryResultList(fetchOptions);
// activates the GaeCtx now that it is initialised
gaeCtx.activate();
// sets the current cursor (in stateful mode, cursor is always kept for further use)
//if(gaeCtx.useCursor){
Cursor cursor = entities.getCursor();
if(cursor!=null){
gaeCtx.addCursor(cursor.toWebSafeString());
}
//}
return new GaeSienaIterable<T>(this, entities, query);
}else {
// if not paginating, we simply use the queryresultiterable and moves the current cursor
// while iterating
QueryResultIterable<Entity> entities = pq.asQueryResultIterable(fetchOptions);
// activates the GaeCtx now that it is initialised
gaeCtx.activate();
return new GaeSienaIterableWithCursor<T>(this, entities, query);
}
}
}
}else {
switch(fetchType.fetchType){
case ITER:
default:
{
PreparedQuery pq = prepare(query);
if(pag.isPaginating()){
// in case of pagination, we need to allow asynchronous calls such as:
// QueryAsync<MyClass> query = pm.createQuery(MyClass).paginate(5).stateful().order("name");
// SienaFuture<Iterable<MyClass>> future1 = query.iter();
// SienaFuture<Iterable<MyClass>> future2 = query.nextPage().iter();
// Iterable<MyClass> it = future1.get().iterator();
// while(it.hasNext()) { // do it }
// it = future2.get().iterator();
// while(it.hasNext()) { // do it }
// so we can't use the asQueryResultIterable as the cursor is not moved to the end of the current page
// but moved at each call of iterable.iterator().next()
// thus we use the List in this case to be able to move directly to the next page with cursors
QueryResultList<Entity> entities;
if(!gaeCtx.useCursor){
// then uses offset (in case of IN or != operators)
//if(offset.isActive()){
// fetchOptions.offset(gaeCtx.realOffset);
//}
fetchOptions.offset(gaeCtx.realOffset);
entities = pq.asQueryResultList(fetchOptions);
}else {
String cursor = gaeCtx.currentCursor();
if(cursor!=null){
entities = pq.asQueryResultList(
fetchOptions.startCursor(Cursor.fromWebSafeString(cursor)));
}else {
entities = pq.asQueryResultList(fetchOptions);
}
// sets the current cursor (in stateful mode, cursor is always kept for further use)
//if(gaeCtx.useCursor){
gaeCtx.addCursor(entities.getCursor().toWebSafeString());
//}
}
return new GaeSienaIterable<T>(this, entities, query);
}else {
// if not paginating, we simply use the queryresultiterable and moves the current cursor
// while iterating
QueryResultIterable<Entity> entities;
if(!gaeCtx.useCursor){
// then uses offset (in case of IN or != operators)
//if(offset.isActive()){
// fetchOptions.offset(gaeCtx.realOffset);
//}
fetchOptions.offset(gaeCtx.realOffset);
entities = pq.asQueryResultIterable(fetchOptions);
}else {
String cursor = gaeCtx.currentCursor();
if(cursor!=null){
entities = pq.asQueryResultIterable(
fetchOptions.startCursor(Cursor.fromWebSafeString(gaeCtx.currentCursor())));
}else {
entities = pq.asQueryResultIterable(fetchOptions);
}
}
return new GaeSienaIterableWithCursor<T>(this, entities, query);
}
}