use of com.teradata.jaqy.resultset.InMemoryResultSet 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;
}
}
use of com.teradata.jaqy.resultset.InMemoryResultSet in project jaqy by Teradata.
the class TeradataHelper method getResultSet.
@Override
public JaqyResultSet getResultSet(ResultSet rs, JaqyInterpreter interpreter) throws SQLException {
if (rs == null)
return null;
ResultSetMetaData meta = rs.getMetaData();
// guess if the output is from SHOW statement.
if (meta != null && meta.getColumnCount() == 1) {
int type = meta.getColumnType(1);
if (TypesUtils.isString(type)) {
getGlobals().log(Level.INFO, "Potential SHOW ResultSet.");
//
// Teradata SHOW statements due to legacy, use '\r' instead of '\n'
// characters. That can be problematic in the output.
//
// Per discussion with a Teradata JDBC architect, no internals of
// Teradata JDBC should be exposed in OSS code. So the only thing
// we can do is to guess the output could be from, and replace
// the EOL character that way.
//
// All SHOW statements are single column. Typically, the output
// is a row of VARCHAR. However, I am not sure if there is ever
// a case of having CLOB output, or just multiple rows.
//
// So we do generic handling here.
//
InMemoryResultSet newRS = ResultSetUtils.copyResultSet(rs, 0, this, interpreter);
rs.close();
for (Object[] row : newRS.getRows()) {
for (int i = 0; i < row.length; ++i) {
if (row[i] != null) {
Object o = row[i];
if (o instanceof String) {
row[i] = ((String) o).replace('\r', '\n');
} else if (o instanceof CachedClob) {
CachedClob clob = (CachedClob) o;
String s = clob.getSubString(1, (int) clob.length());
clob = new CachedClob(s);
}
}
}
}
rs = newRS;
}
}
return new JaqyDefaultResultSet(rs, this);
}
use of com.teradata.jaqy.resultset.InMemoryResultSet in project jaqy by Teradata.
the class DefaultHelper method getTableColumns.
@Override
public JaqyResultSet getTableColumns(String tableName, JaqyInterpreter interpreter) throws Exception {
if (m_tableColumnFormat != null) {
JaqyResultSet rs = interpreter.getResultSet(m_tableColumnFormat.format(new Object[] { tableName }));
if (rs == null)
throw ExceptionUtils.getTableNotFound();
InMemoryResultSet inmemrs = (InMemoryResultSet) rs.getResultSet();
if (inmemrs.getRows().size() == 0)
throw ExceptionUtils.getTableNotFound();
return rs;
}
String query = "SELECT * FROM " + tableName + " WHERE 1 = 0";
JaqyStatement stmt = null;
try {
stmt = createStatement(true);
stmt.execute(query);
JaqyResultSet rs = stmt.getResultSet(interpreter);
if (rs == null)
throw ExceptionUtils.getTableNotFound();
JaqyResultSetMetaData meta = rs.getMetaData();
SchemaInfo schemaInfo = ResultSetMetaDataUtils.getColumnInfo(meta.getMetaData(), null);
int count = schemaInfo.columns.length;
PropertyTable pt = new PropertyTable(new String[] { "Column", "Type", "Nullable" });
for (int i = 0; i < count; ++i) {
String columnName = schemaInfo.columns[i].name;
String columnType = getTypeName(schemaInfo.columns[i]);
String nullable = (schemaInfo.columns[i].nullable == ResultSetMetaData.columnNoNulls) ? "No" : (schemaInfo.columns[i].nullable == ResultSetMetaData.columnNullable ? "Yes" : "Unknown");
pt.addRow(new String[] { columnName, columnType, nullable });
}
rs.close();
return ResultSetUtils.getResultSet(pt);
} finally {
try {
stmt.close();
} catch (Exception ex) {
}
}
}
Aggregations