Search in sources :

Example 1 with ISelectStreamHandler

use of org.eclipse.scout.rt.server.jdbc.ISelectStreamHandler in project scout.rt by eclipse.

the class CsvSqlAdapter method exportData.

/**
 * Export sql data into a file
 *
 * @param params
 */
public void exportData(CsvSettings params) {
    final CsvHelper h = new CsvHelper(params.getContentLocale(), params.getColSeparator(), params.getTextDelimiter(), "\n");
    if (params.getCsvColumnTypes() != null) {
        h.setColumnTypes(params.getCsvColumnTypes());
    }
    if (params.getCsvColumnNames() != null) {
        h.setColumnNames(params.getCsvColumnNames());
    }
    Collection<String> cols = new ArrayList<String>();
    cols.addAll(params.getCsvColumnNames());
    // prepare select statement
    String sqlText;
    Object[] base = null;
    if (params.getSqlSelect() != null) {
        sqlText = params.getSqlSelect();
        base = params.getBindBase();
    } else {
        StringBuilder buf = new StringBuilder();
        buf.append("SELECT ");
        for (Iterator<String> it = cols.iterator(); it.hasNext(); ) {
            String colName = it.next();
            buf.append(colName);
            if (it.hasNext()) {
                buf.append(",");
            }
        }
        buf.append(" FROM ");
        buf.append(params.getTableName());
        if (params.getGroupKeyValue() != null) {
            buf.append(" WHERE ");
            buf.append(params.getGroupKeyColumnName());
            buf.append("=:groupKeyColumnValue");
        }
        if (params.getLineNumberColumnName() != null) {
            buf.append(" ORDER BY ");
            buf.append(params.getLineNumberColumnName());
        }
        sqlText = buf.toString();
        if (params.getGroupKeyValue() != null) {
            base = new Object[1];
            base[0] = new NVPair("groupKeyColumnValue", params.getGroupKeyValue());
        }
    }
    try (FileOutputStream out = new FileOutputStream(params.getFile());
        Writer w = new OutputStreamWriter(out, params.getEncoding())) {
        h.exportHeaderRows(w, params.getWriteColumnNames(), params.getWriteColumnTypes());
        ISelectStreamHandler handler = new ISelectStreamHandler() {

            @Override
            public void handleRow(Connection con, PreparedStatement stm, ResultSet rs, int rowIndex, List<SqlBind> values) {
                Object[] row = new Object[values.size()];
                for (int i = 0; i < row.length; i++) {
                    row[i] = values.get(i).getValue();
                }
                h.exportDataRow(row, w, false);
            }

            @Override
            public void finished(Connection con, PreparedStatement stm, ResultSet rs, int rowCount) {
            // do nothing
            }
        };
        m_sqlService.selectStreaming(sqlText, handler, base);
    } catch (IOException e) {
        throw new ProcessingException(e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) CsvHelper(org.eclipse.scout.rt.shared.csv.CsvHelper) OutputStreamWriter(java.io.OutputStreamWriter) ISelectStreamHandler(org.eclipse.scout.rt.server.jdbc.ISelectStreamHandler) ArrayList(java.util.ArrayList) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Aggregations

FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)1 NVPair (org.eclipse.scout.rt.platform.holders.NVPair)1 ISelectStreamHandler (org.eclipse.scout.rt.server.jdbc.ISelectStreamHandler)1 CsvHelper (org.eclipse.scout.rt.shared.csv.CsvHelper)1