Search in sources :

Example 1 with JaqyDefaultResultSet

use of com.teradata.jaqy.connection.JaqyDefaultResultSet 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;
    }
}
Also used : JaqyFilterResultSet(com.teradata.jaqy.connection.JaqyFilterResultSet) InMemoryResultSet(com.teradata.jaqy.resultset.InMemoryResultSet) JaqyDefaultResultSet(com.teradata.jaqy.connection.JaqyDefaultResultSet) IOException(java.io.IOException) SQLException(java.sql.SQLException) ScriptException(javax.script.ScriptException)

Example 2 with JaqyDefaultResultSet

use of com.teradata.jaqy.connection.JaqyDefaultResultSet in project jaqy by Teradata.

the class ResultSetUtils method copyResultSet.

public static JaqyResultSet copyResultSet(JaqyResultSet rs, long limit, JaqyInterpreter interpreter) throws SQLException {
    InMemoryResultSetMetaData rsmd = ResultSetMetaDataUtils.copyResultSetMetaData(rs.getMetaData().getMetaData(), rs.getHelper());
    ArrayList<Object[]> rows = new ArrayList<Object[]>();
    JaqyStatement stmt = rs.getStatement();
    int columnCount = rsmd.getColumnCount();
    if (limit == 0)
        limit = Long.MAX_VALUE;
    boolean hasLob = false;
    while (rs.next() && limit > 0) {
        Object[] row = new Object[columnCount];
        for (int i = 0; i < columnCount; ++i) {
            Object o = rs.getObject(i + 1);
            row[i] = ResultSetUtils.copyIfNecessary(o, interpreter);
            if (row[i] instanceof CachedClob || row[i] instanceof CachedBlob || row[i] instanceof CachedSQLXML)
                hasLob = true;
        }
        rows.add(row);
        --limit;
    }
    InMemoryResultSet newRS = new InMemoryResultSet(rows, rsmd, stmt == null ? null : stmt.getStatement());
    newRS.setHasLob(hasLob);
    JaqyDefaultResultSet jqrs = new JaqyDefaultResultSet(newRS, DummyHelper.getInstance());
    jqrs.setStatement(stmt);
    return jqrs;
}
Also used : ArrayList(java.util.ArrayList) JaqyDefaultResultSet(com.teradata.jaqy.connection.JaqyDefaultResultSet) JaqyStatement(com.teradata.jaqy.connection.JaqyStatement)

Example 3 with JaqyDefaultResultSet

use of com.teradata.jaqy.connection.JaqyDefaultResultSet 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);
}
Also used : CachedClob(com.teradata.jaqy.resultset.CachedClob) InMemoryResultSet(com.teradata.jaqy.resultset.InMemoryResultSet) JaqyDefaultResultSet(com.teradata.jaqy.connection.JaqyDefaultResultSet)

Aggregations

JaqyDefaultResultSet (com.teradata.jaqy.connection.JaqyDefaultResultSet)3 InMemoryResultSet (com.teradata.jaqy.resultset.InMemoryResultSet)2 JaqyFilterResultSet (com.teradata.jaqy.connection.JaqyFilterResultSet)1 JaqyStatement (com.teradata.jaqy.connection.JaqyStatement)1 CachedClob (com.teradata.jaqy.resultset.CachedClob)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 ScriptException (javax.script.ScriptException)1