Search in sources :

Example 6 with JaqyHelper

use of com.teradata.jaqy.interfaces.JaqyHelper 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 7 with JaqyHelper

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

the class ImportSchemaCommand method execute.

@Override
public void execute(String[] args, boolean silent, JaqyInterpreter interpreter) throws Exception {
    JaqyImporter<?> importer = interpreter.getImporter();
    if (importer == null) {
        interpreter.error("There is no current import.");
    }
    SchemaInfo schemaInfo = importer.getSchema();
    if (schemaInfo == null) {
        interpreter.error("Current import schema is not available.");
    }
    boolean displaySQL = false;
    CommandLine cmdLine = getCommandLine(args);
    for (Option option : cmdLine.getOptions()) {
        switch(option.getOpt().charAt(0)) {
            case 's':
                {
                    displaySQL = true;
                    break;
                }
        }
    }
    SessionUtils.checkOpen(interpreter);
    Session session = interpreter.getSession();
    JaqyHelper helper = session.getConnection().getHelper();
    if (displaySQL) {
        String sql = SchemaUtils.getTableSchema(helper, schemaInfo, "TABLENAME", false);
        interpreter.println(sql);
    } else {
        JaqyResultSet rs = SchemaUtils.getSchemaResultSet(helper, schemaInfo, false, interpreter);
        interpreter.print(rs);
        rs.close();
    }
}
Also used : JaqyResultSet(com.teradata.jaqy.interfaces.JaqyResultSet) CommandLine(org.apache.commons.cli.CommandLine) JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) Option(org.apache.commons.cli.Option) SchemaInfo(com.teradata.jaqy.schema.SchemaInfo) Session(com.teradata.jaqy.Session)

Example 8 with JaqyHelper

use of com.teradata.jaqy.interfaces.JaqyHelper 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 9 with JaqyHelper

use of com.teradata.jaqy.interfaces.JaqyHelper 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)

Example 10 with JaqyHelper

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

the class ProjectColumnList method bind.

public void bind(JaqyResultSet rs, JaqyInterpreter interpreter) throws SQLException {
    int numCols = m_columnList.size();
    ExpNode[] exps = new ExpNode[numCols];
    FullColumnInfo[] columnInfos = new FullColumnInfo[numCols];
    ResultSetMetaData rsmd = rs.getMetaData().getMetaData();
    JaqyHelper helper = rs.getHelper();
    for (int i = 0; i < numCols; ++i) {
        ProjectColumn column = m_columnList.get(i);
        int index = rs.findColumn(column.name);
        columnInfos[i] = ResultSetMetaDataUtils.getColumnInfo(rsmd, index, helper);
        columnInfos[i].name = column.asName;
        columnInfos[i].label = column.asName;
        ColumnNode exp = new ColumnNode(column.name);
        exp.bind(rs, null, interpreter);
        exps[i] = exp;
    }
    m_project = new ExpNodeProject(exps);
    m_meta = new JaqyResultSetMetaData(new InMemoryResultSetMetaData(columnInfos), DummyHelper.getInstance());
}
Also used : InMemoryResultSetMetaData(com.teradata.jaqy.resultset.InMemoryResultSetMetaData) ResultSetMetaData(java.sql.ResultSetMetaData) JaqyResultSetMetaData(com.teradata.jaqy.connection.JaqyResultSetMetaData) FullColumnInfo(com.teradata.jaqy.schema.FullColumnInfo) JaqyHelper(com.teradata.jaqy.interfaces.JaqyHelper) JaqyResultSetMetaData(com.teradata.jaqy.connection.JaqyResultSetMetaData) ColumnNode(com.teradata.jaqy.utils.exp.ColumnNode) ExpNode(com.teradata.jaqy.utils.exp.ExpNode) InMemoryResultSetMetaData(com.teradata.jaqy.resultset.InMemoryResultSetMetaData)

Aggregations

JaqyHelper (com.teradata.jaqy.interfaces.JaqyHelper)10 Session (com.teradata.jaqy.Session)5 JaqyConnection (com.teradata.jaqy.connection.JaqyConnection)4 JaqyResultSetMetaData (com.teradata.jaqy.connection.JaqyResultSetMetaData)4 SchemaInfo (com.teradata.jaqy.schema.SchemaInfo)3 TypeHandler (com.teradata.jaqy.typehandler.TypeHandler)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 JaqyResultSet (com.teradata.jaqy.interfaces.JaqyResultSet)2 InMemoryResultSetMetaData (com.teradata.jaqy.resultset.InMemoryResultSetMetaData)1 FullColumnInfo (com.teradata.jaqy.schema.FullColumnInfo)1 CSVExportInfo (com.teradata.jaqy.utils.CSVExportInfo)1 ColumnNode (com.teradata.jaqy.utils.exp.ColumnNode)1 ExpNode (com.teradata.jaqy.utils.exp.ExpNode)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Schema (org.apache.avro.Schema)1 DataFileWriter (org.apache.avro.file.DataFileWriter)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 GenericRecord (org.apache.avro.generic.GenericRecord)1