use of com.teradata.jaqy.connection.JaqyFilterResultSet in project jaqy by Teradata.
the class JaqyInterpreter method print.
/**
* Print the ResultSet.
* <p>
* It should be noted that this function does not close the ResultSet.
* This behavior is to allow saved ResultSet to be re-used.
* <p>
* Depending on the client filtering, projection and sorting, either a
* copy or another JaqyResult could be created to deal with these.
*
* @param rs
* ResultSet to be printed.
* @return the ResultSet passed in, or a copy of the ResultSet after
* filtering, projection and sorting.
*/
public JaqyResultSet print(JaqyResultSet rs) {
Display display = m_display;
try {
m_globals.log(Level.INFO, "ResultSet Type: " + ResultSetUtils.getResultSetType(rs.getType()));
// before printing, we clear the current activity count in case we
// had an error.
m_activityCount = -1;
boolean rsCanProject = false;
// First, filter the ResultSet
if (m_predicate != null) {
@SuppressWarnings("resource") JaqyFilterResultSet newRS = new JaqyFilterResultSet(rs, rs.getHelper(), this);
newRS.setStatement(rs.getStatement());
newRS.setPredicate(m_predicate);
rs = newRS;
m_predicate = null;
rsCanProject = true;
}
// Second, sort the ResultSet
if (m_sortInfos != null) {
if (!rs.isSortable()) {
JaqyResultSet newRS = ResultSetUtils.copyResultSet(rs, m_limit, this);
rs.close();
rs = newRS;
}
rs.sort(m_sortInfos);
m_sortInfos = null;
rsCanProject = false;
}
// Lastly, do projection
if (m_projectList != null) {
if (rsCanProject) {
// Re-use the current JaqyFilterResultSet if possible.
((JaqyFilterResultSet) rs).setProjection(m_projectList);
} else {
@SuppressWarnings("resource") JaqyFilterResultSet newRS = new JaqyFilterResultSet(rs, rs.getHelper(), this);
newRS.setStatement(rs.getStatement());
newRS.setPredicate(m_predicate);
newRS.setProjection(m_projectList);
rs = newRS;
}
m_projectList = null;
}
// Now check if we need to save the ResultSet. If so, we need
// to make this ResultSet in-memory and without filtering and
// projections.
boolean rewind = false;
if (m_saveResultSet) {
// check if we can avoid the copy the ResultSet
if (!(rs instanceof JaqyDefaultResultSet) || !(rs.getResultSet() instanceof InMemoryResultSet)) {
JaqyResultSet newRS = ResultSetUtils.copyResultSet(rs, 0, this);
rs.close();
rs = newRS;
}
getVariableManager().put("save", rs);
rewind = true;
m_saveResultSet = false;
}
JaqyExporter exporter = getExporter();
if (exporter != null) {
setExporter(null);
m_activityCount = exporter.export(rs, this);
return rs;
}
if (m_quiet) {
m_activityCount = QuietPrinter.getInstance().print(rs, display.getPrintWriter(), m_limit, this);
} else {
m_activityCount = m_printer.print(rs, display.getPrintWriter(), m_limit, this);
}
if (rewind) {
rs.beforeFirst();
// prevent the ResultSet from closed.
rs = null;
}
return rs;
} catch (Exception ex) {
display.error(this, ex);
return null;
}
}
Aggregations