int nrows = 0;
DirectObjectCursor cursor = null;
PostgresOutputter<List<?>> outputter = null;
PostgresDirectObjectCopier copier = null;
bindings = PostgresLoadablePlan.setParameters(bindings, invocation);
ServerCallContextStack stack = ServerCallContextStack.get();
boolean suspended = false, success = false;
stack.push(context, invocation);
try {
context.initStore(schema);
cursor = context.startCursor(this, bindings);
switch (outputMode) {
case TABLE:
outputter = new PostgresDirectObjectOutputter(context, this);
break;
case COPY:
case COPY_WITH_NEWLINE:
outputter = copier = new PostgresDirectObjectCopier(context, this, (outputMode == DirectObjectPlan.OutputMode.COPY_WITH_NEWLINE));
copier.respond();
break;
}
if (cursor != null) {
List<?> row;
while ((row = cursor.next()) != null) {
if (row.isEmpty()) {
messenger.flush();
}
else {
outputter.output(row);
nrows++;
}
if ((maxrows > 0) && (nrows >= maxrows)) {
suspended = true;
break;
}
}
}
if (copier != null) {
copier.done();
}
success = true;
}
finally {
suspended = context.finishCursor(this, cursor, nrows, suspended);
stack.pop(context, invocation, success);
}
if (suspended) {
messenger.beginMessage(PostgresMessages.PORTAL_SUSPENDED_TYPE.code());
messenger.sendMessage();
}