Search in sources :

Example 1 with CSVExportInfo

use of com.teradata.jaqy.utils.CSVExportInfo 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 2 with CSVExportInfo

use of com.teradata.jaqy.utils.CSVExportInfo in project jaqy by Teradata.

the class CSVExporterFactory method getHandler.

@Override
public JaqyExporter getHandler(CommandLine cmdLine, JaqyInterpreter interpreter) throws Exception {
    Charset charset = DEFAULT_CHARSET;
    CSVFormat format = CSVFormat.DEFAULT;
    HashMap<Integer, CSVExportInfo> exportInfoMap = new HashMap<Integer, CSVExportInfo>();
    CSVNameGen nameGen = new CSVNameGen(DEFAULT_NAME_PATTERN);
    Charset encoding = DEFAULT_CHARSET;
    for (Option option : cmdLine.getOptions()) {
        switch(option.getOpt().charAt(0)) {
            case 'c':
                {
                    charset = Charset.forName(option.getValue());
                    break;
                }
            case 'd':
                {
                    char delimiter = CSVUtils.getChar(option.getValue());
                    if (delimiter == 0)
                        throw new IllegalArgumentException("invalid delimiter: " + option.getValue());
                    format = format.withDelimiter(delimiter);
                    break;
                }
            case 't':
                {
                    format = CSVUtils.getFormat(option.getValue());
                    break;
                }
            case 'n':
                {
                    String fmt = option.getValue();
                    nameGen = new CSVNameGen(fmt);
                    // now check if the name is a valid format.
                    if (fmt.equals(nameGen.getName(1)))
                        interpreter.error("Invalid name pattern: " + fmt);
                    break;
                }
            case 'e':
                {
                    encoding = Charset.forName(option.getValue());
                    break;
                }
            case 'f':
                {
                    int column = Integer.parseInt(option.getValue());
                    if (column < 1) {
                        interpreter.error("Column index cannot be smaller than 1.");
                    }
                    CSVExportInfo info = new CSVExportInfo(nameGen, encoding);
                    exportInfoMap.put(column, info);
                    break;
                }
        }
    }
    String[] args = cmdLine.getArgs();
    if (args.length == 0)
        throw new IllegalArgumentException("missing file name.");
    Path file = interpreter.getPath(args[0]);
    return new CSVExporter(file, charset, format, exportInfoMap);
}
Also used : Path(com.teradata.jaqy.interfaces.Path) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) CSVExportInfo(com.teradata.jaqy.utils.CSVExportInfo) CSVNameGen(com.teradata.jaqy.utils.CSVNameGen) CSVFormat(org.apache.commons.csv.CSVFormat) Option(org.apache.commons.cli.Option)

Aggregations

CSVExportInfo (com.teradata.jaqy.utils.CSVExportInfo)2 JaqyResultSetMetaData (com.teradata.jaqy.connection.JaqyResultSetMetaData)1 JaqyHelper (com.teradata.jaqy.interfaces.JaqyHelper)1 Path (com.teradata.jaqy.interfaces.Path)1 TypeHandler (com.teradata.jaqy.typehandler.TypeHandler)1 CSVNameGen (com.teradata.jaqy.utils.CSVNameGen)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 Option (org.apache.commons.cli.Option)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVPrinter (org.apache.commons.csv.CSVPrinter)1