Search in sources :

Example 1 with TypeHandler

use of com.teradata.jaqy.typehandler.TypeHandler 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)

Example 2 with TypeHandler

use of com.teradata.jaqy.typehandler.TypeHandler in project jaqy by Teradata.

the class CSVExporter method export.

@Override
public long export(JaqyResultSet rs, JaqyInterpreter interpreter) throws Exception {
    PrintWriter pw = new PrintWriter(new BufferedWriter(m_out));
    JaqyResultSetMetaData metaData = rs.getMetaData();
    int columns = metaData.getColumnCount();
    TypeHandler[] handlers = new TypeHandler[columns];
    JaqyHelper helper = rs.getHelper();
    CSVPrinter printer = new CSVPrinter(pw, m_format);
    for (int i = 0; i < columns; ++i) {
        // print the header row
        printer.print(metaData.getColumnLabel(i + 1));
        CSVExportInfo fileInfo = m_fileInfoMap.get(i + 1);
        if (fileInfo == null)
            handlers[i] = helper.getTypeHandler(rs, i + 1);
        else
            handlers[i] = new FileHandler(m_file, fileInfo);
    }
    printer.println();
    long count = 0;
    while (rs.next()) {
        ++count;
        for (int i = 0; i < columns; ++i) {
            printer.print(handlers[i].getString(rs, i + 1, interpreter));
        }
        printer.println();
    }
    printer.close();
    return count;
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) JaqyResultSetMetaData(com.teradata.jaqy.connection.JaqyResultSetMetaData) JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) CSVExportInfo(com.teradata.jaqy.utils.CSVExportInfo) TypeHandler(com.teradata.jaqy.typehandler.TypeHandler)

Example 3 with TypeHandler

use of com.teradata.jaqy.typehandler.TypeHandler in project jaqy by Teradata.

the class CSVPrinter method print.

@Override
public long print(JaqyResultSet rs, PrintWriter pw, long limit, JaqyInterpreter interpreter) throws Exception {
    JaqyHelper helper = rs.getHelper();
    JaqyResultSetMetaData metaData = rs.getMetaData();
    int columns = metaData.getColumnCount();
    TypeHandler[] handlers = new TypeHandler[columns];
    @SuppressWarnings("resource") org.apache.commons.csv.CSVPrinter printer = new org.apache.commons.csv.CSVPrinter(pw, m_format);
    for (int i = 0; i < columns; ++i) {
        // print the header row
        printer.print(metaData.getColumnLabel(i + 1));
        handlers[i] = helper.getTypeHandler(rs, i + 1);
    }
    printer.println();
    long count = 0;
    if (limit == 0)
        limit = Long.MAX_VALUE;
    while (rs.next() && count < limit) {
        ++count;
        for (int i = 0; i < columns; ++i) {
            printer.print(handlers[i].getString(rs, i + 1, interpreter));
        }
        printer.println();
    }
    printer.flush();
    return count;
}
Also used : JaqyResultSetMetaData(com.teradata.jaqy.connection.JaqyResultSetMetaData) JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) TypeHandler(com.teradata.jaqy.typehandler.TypeHandler)

Aggregations

JaqyResultSetMetaData (com.teradata.jaqy.connection.JaqyResultSetMetaData)3 JaqyHelper (com.teradata.jaqy.interfaces.JaqyHelper)3 TypeHandler (com.teradata.jaqy.typehandler.TypeHandler)3 JaqyResultSet (com.teradata.jaqy.interfaces.JaqyResultSet)1 CSVExportInfo (com.teradata.jaqy.utils.CSVExportInfo)1 CSVPrinter (org.apache.commons.csv.CSVPrinter)1