Example usage for org.apache.commons.csv CSVStrategy CSVStrategy

List of usage examples for org.apache.commons.csv CSVStrategy CSVStrategy

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVStrategy CSVStrategy.

Prototype

public CSVStrategy(char delimiter, char encapsulator, char commentStart, boolean ignoreLeadingWhitespace,
        boolean interpretUnicodeEscapes, boolean ignoreEmptyLines) 

Source Link

Usage

From source file:com.appspot.relaxe.tools.CSVInsertTask.java

public void run(Connection connection, Reader input, Table table) throws IOException, SQLException {

    if (connection == null) {
        throw new NullPointerException("'connection' must not be null");
    }/* w  ww .ja va 2 s. com*/

    if (input == null) {
        throw new NullPointerException("'input' must not be null");
    }

    if (table == null) {
        throw new NullPointerException("'table' must not be null");
    }

    boolean committed = false;

    try {
        connection.setAutoCommit(false);
        CSVStrategy cs = new CSVStrategy('\t', '"', CSVStrategy.COMMENTS_DISABLED, false, false, false);
        CSVParser p = new CSVParser(input, cs);

        // get header line
        String[] line = p.getLine();

        // configure by using the column headers:        
        ColumnMap cm = table.getColumnMap();
        List<Identifier> names = new ArrayList<Identifier>();
        List<Column> columnList = new ArrayList<Column>();

        for (String n : line) {
            Column column = cm.get(n);

            if (column == null) {
                throw new IllegalArgumentException("column not found " + n);
            }

            columnList.add(column);
            names.add(column.getColumnName());
        }

        if (names.isEmpty()) {
            throw new IllegalStateException("no column names available");
        }

        ElementList<Identifier> nel = ElementList.newElementList(names);

        final int expectedColumnCount = line.length;
        //            int recno = 0;        

        PreparedStatement ps = null;
        InsertStatement ins = null;

        VarcharParameter[] params = new VarcharParameter[expectedColumnCount];
        ValueAssignerFactory vaf = getImplementation().getValueAssignerFactory();

        AssignmentVisitor pa = null;

        while ((line = p.getLine()) != null) {
            //                recno++;
            final int cols = line.length;
            int lineno = p.getLineNumber();

            if (cols != expectedColumnCount) {
                throw new IllegalStateException("unexpected column count: " + cols + " at line " + lineno);
            }

            if (ps == null) {

                List<RowValueConstructorElement> vl = new ArrayList<RowValueConstructorElement>(params.length);

                for (int i = 0; i < params.length; i++) {
                    Column column = columnList.get(i);
                    VarcharHolder h = parse(column, line[i]);

                    VarcharParameter param = new VarcharParameter(column, h);
                    params[i] = param;
                    vl.add(param);
                }

                RowValueConstructor rvc = AbstractRowValueConstructor.of(vl);
                ins = new InsertStatement(table, nel, rvc);

                String q = ins.generate();
                ps = connection.prepareStatement(q);
                pa = new AssignmentVisitor(vaf, ps);

                //                System.err.println("lineno: " + lineno);
                //                System.err.println("record: " + recno);
                //                System.err.println("query: " + q);
            } else {
                pa.reset();

                for (int i = 0; i < line.length; i++) {
                    Column column = columnList.get(i);
                    VarcharHolder h = parse(column, line[i]);
                    VarcharParameter param = params[i];
                    param.setValue(h);
                }
            }

            ins.traverse(null, pa);
            ps.addBatch();
        }

        int[] updateCounts = ps.executeBatch();
        updated(updateCounts);
        connection.commit();
        committed = true;
    } finally {
        if (!(committed)) {
            QueryHelper.doRollback(connection);
        }
    }
}