Search in sources :

Example 1 with ResultSetTypedIterator

use of annis.sqlgen.ResultSetTypedIterator in project ANNIS by korpling.

the class QueryDaoImpl method find.

@Transactional(readOnly = true)
@Override
public boolean find(final QueryData queryData, final OutputStream out) {
    prepareTransaction(queryData);
    Boolean finished = getJdbcTemplate().execute(new ConnectionCallback<Boolean>() {

        @Override
        public Boolean doInConnection(Connection con) throws SQLException, DataAccessException {
            try (Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
                String sql = findSqlGenerator.toSql(queryData);
                PrintWriter w;
                try (ResultSet rs = stmt.executeQuery(sql)) {
                    w = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
                    ResultSetTypedIterator<Match> itMatches = new ResultSetTypedIterator<>(rs, findSqlGenerator);
                    int i = 1;
                    while (itMatches.hasNext()) {
                        // write single match to output stream
                        Match m = itMatches.next();
                        w.print(m.toString());
                        w.print("\n");
                        // flush after every 10th item
                        if (i % 10 == 0) {
                            w.flush();
                        }
                        i++;
                    }
                // end for each match
                }
                w.flush();
                return true;
            } catch (UnsupportedEncodingException ex) {
                log.error("Your system is not able to handle UTF-8 but ANNIS really needs this charset", ex);
            }
            return false;
        }
    });
    return finished;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Match(annis.service.objects.Match) ResultSet(java.sql.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) ResultSetTypedIterator(annis.sqlgen.ResultSetTypedIterator) DataAccessException(org.springframework.dao.DataAccessException) PrintWriter(java.io.PrintWriter) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Match (annis.service.objects.Match)1 ResultSetTypedIterator (annis.sqlgen.ResultSetTypedIterator)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 DataAccessException (org.springframework.dao.DataAccessException)1 Transactional (org.springframework.transaction.annotation.Transactional)1