Search in sources :

Example 1 with JaqyResultSet

use of com.teradata.jaqy.interfaces.JaqyResultSet in project jaqy by Teradata.

the class DescribeCommand method execute.

@Override
public void execute(String[] args, boolean silent, JaqyInterpreter interpreter) throws Exception {
    SessionUtils.checkOpen(interpreter);
    CommandLine cmdLine = getCommandLine(args);
    args = cmdLine.getArgs();
    if (args.length == 0) {
        interpreter.error("Missing table name.");
    }
    boolean displaySQL = false;
    for (Option option : cmdLine.getOptions()) {
        switch(option.getOpt().charAt(0)) {
            case 's':
                {
                    displaySQL = true;
                    break;
                }
        }
    }
    String tableName = args[0];
    for (int i = 1; i < args.length; ++i) tableName += args[i];
    if (displaySQL) {
        String schema = interpreter.getSession().getConnection().getHelper().getTableSchema(tableName, interpreter);
        interpreter.println(schema);
    } else {
        JaqyResultSet rs = interpreter.getSession().getConnection().getHelper().getTableColumns(tableName, interpreter);
        interpreter.print(rs);
        rs.close();
    }
}
Also used : JaqyResultSet(com.teradata.jaqy.interfaces.JaqyResultSet) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option)

Example 2 with JaqyResultSet

use of com.teradata.jaqy.interfaces.JaqyResultSet in project jaqy by Teradata.

the class DebugManager method dumpPreparedStatement.

public void dumpPreparedStatement(Display display, Session session, JaqyPreparedStatement stmt, JaqyInterpreter interpreter) {
    if (m_dumpPreparedStatement) {
        try {
            JaqyParameterMetaData metaData = stmt.getParameterMetaData();
            ParameterMetaDataUtils.dump(display, session, metaData);
        } catch (SQLException ex) {
            display.getGlobals().log(Level.INFO, ex);
        }
        try {
            JaqyResultSet rs = stmt.getResultSet(interpreter);
            if (rs != null)
                ResultSetMetaDataUtils.dump(display, session, rs.getMetaData());
        } catch (SQLException ex) {
            display.getGlobals().log(Level.INFO, ex);
        }
    }
}
Also used : JaqyResultSet(com.teradata.jaqy.interfaces.JaqyResultSet) JaqyParameterMetaData(com.teradata.jaqy.connection.JaqyParameterMetaData) SQLException(java.sql.SQLException)

Example 3 with JaqyResultSet

use of com.teradata.jaqy.interfaces.JaqyResultSet in project jaqy by Teradata.

the class DefaultHelper method getTableSchema.

/**
 * Guess the schema for a table.
 */
@Override
public String getTableSchema(String tableName, JaqyInterpreter interpreter) throws Exception {
    if (m_tableSchemaQuery != null) {
        String value = interpreter.getQueryString(m_tableSchemaFormat.format(new Object[] { tableName }), m_tableSchemaQuery.columnIndex);
        if (value == null || value.length() == 0)
            throw ExceptionUtils.getTableNotFound();
        return value;
    }
    /*
		 * Creates a dummy query that gets no actual results.  We just need
		 * the resulting ResultSetMetaData to infer the table schema.
		 */
    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);
        String schema = SchemaUtils.getTableSchema(this, schemaInfo, tableName, true);
        rs.close();
        return schema;
    } finally {
        try {
            stmt.close();
        } catch (Exception ex) {
        }
    }
}
Also used : JaqyResultSet(com.teradata.jaqy.interfaces.JaqyResultSet)

Example 4 with JaqyResultSet

use of com.teradata.jaqy.interfaces.JaqyResultSet 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) {
        }
    }
}
Also used : JaqyResultSet(com.teradata.jaqy.interfaces.JaqyResultSet) PropertyTable(com.teradata.jaqy.PropertyTable) InMemoryResultSet(com.teradata.jaqy.resultset.InMemoryResultSet)

Example 5 with JaqyResultSet

use of com.teradata.jaqy.interfaces.JaqyResultSet in project jaqy by Teradata.

the class TablePrinter method print.

public long print(JaqyResultSet rs, PrintWriter pw, long limit, JaqyInterpreter interpreter) throws Exception {
    JaqyHelper helper = rs.getHelper();
    // rewind operation.
    if (m_autoShrink && rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
        JaqyResultSet newRS = ResultSetUtils.copyResultSet(rs, limit, interpreter);
        rs.close();
        rs = newRS;
    }
    JaqyResultSetMetaData metaData = rs.getMetaData();
    int columns = metaData.getColumnCount();
    boolean[] leftAligns = new boolean[columns];
    int[] widths = new int[columns];
    TypeHandler[] handlers = new TypeHandler[columns];
    for (int i = 0; i < columns; ++i) {
        int columnIndex = i + 1;
        int type = metaData.getColumnType(columnIndex);
        leftAligns[i] = isLeftAlign(type);
        handlers[i] = helper.getTypeHandler(rs, i + 1);
    }
    if (m_autoShrink) {
        // 
        for (int i = 0; i < columns; ++i) {
            widths[i] = 0;
            int dispSize = ResultSetUtils.getDisplayWidth(rs, i + 1);
            if (dispSize <= m_columnThreshold)
                widths[i] = dispSize;
        }
        shrink(columns, widths, rs, handlers, limit, interpreter);
    } else {
        // 
        for (int i = 0; i < columns; ++i) {
            widths[i] = ResultSetUtils.getDisplayWidth(rs, i + 1);
            if (widths[i] > m_maxColumnSize)
                widths[i] = m_maxColumnSize;
        }
    }
    // Make sure the title length is considered as well.
    for (int i = 0; i < columns; ++i) {
        String title = metaData.getColumnLabel(i + 1);
        if (widths[i] < title.length())
            widths[i] = title.length();
    }
    if (m_border)
        printBorder(pw, widths);
    // Print the title
    if (m_border)
        pw.print("| ");
    for (int i = 0; i < columns; ++i) {
        String title = metaData.getColumnLabel(i + 1);
        if (i > 0) {
            if (m_border)
                pw.print(" | ");
            else
                pw.print(' ');
        }
        StringUtils.print(pw, title, widths[i], leftAligns[i], (i < (columns - 1)) || m_border);
    }
    if (m_border)
        pw.print(" |");
    pw.println();
    // print the dashes
    if (m_border)
        printBorder(pw, widths);
    else
        printDash(pw, widths);
    long count = 0;
    if (limit == 0)
        limit = Long.MAX_VALUE;
    while (rs.next() && count < limit) {
        ++count;
        if (m_border)
            pw.print("| ");
        for (int i = 0; i < columns; ++i) {
            if (i > 0) {
                if (m_border)
                    pw.print(" | ");
                else
                    pw.print(' ');
            }
            StringUtils.print(pw, handlers[i].getString(rs, i + 1, interpreter), widths[i], leftAligns[i], (i < (columns - 1)) || m_border);
        }
        if (m_border)
            pw.print(" |");
        pw.println();
    }
    if (m_border)
        printBorder(pw, widths);
    return count;
}
Also used : JaqyResultSet(com.teradata.jaqy.interfaces.JaqyResultSet) JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) JaqyResultSetMetaData(com.teradata.jaqy.connection.JaqyResultSetMetaData) TypeHandler(com.teradata.jaqy.typehandler.TypeHandler)

Aggregations

JaqyResultSet (com.teradata.jaqy.interfaces.JaqyResultSet)9 SQLException (java.sql.SQLException)3 JaqyStatement (com.teradata.jaqy.connection.JaqyStatement)2 JaqyHelper (com.teradata.jaqy.interfaces.JaqyHelper)2 CommandLine (org.apache.commons.cli.CommandLine)2 Option (org.apache.commons.cli.Option)2 PropertyTable (com.teradata.jaqy.PropertyTable)1 Session (com.teradata.jaqy.Session)1 JaqyParameterMetaData (com.teradata.jaqy.connection.JaqyParameterMetaData)1 JaqyResultSetMetaData (com.teradata.jaqy.connection.JaqyResultSetMetaData)1 PipeExporter (com.teradata.jaqy.exporter.PipeExporter)1 JaqyExporter (com.teradata.jaqy.interfaces.JaqyExporter)1 InMemoryResultSet (com.teradata.jaqy.resultset.InMemoryResultSet)1 SchemaInfo (com.teradata.jaqy.schema.SchemaInfo)1 TypeHandler (com.teradata.jaqy.typehandler.TypeHandler)1