Example usage for org.springframework.batch.item.database.support HsqlPagingQueryProvider setSelectClause

List of usage examples for org.springframework.batch.item.database.support HsqlPagingQueryProvider setSelectClause

Introduction

In this page you can find the example usage for org.springframework.batch.item.database.support HsqlPagingQueryProvider setSelectClause.

Prototype

public void setSelectClause(String selectClause) 

Source Link

Usage

From source file:de.langmi.spring.batch.examples.readers.jdbc.JdbcPagingItemReaderTests.java

@Test
public void testWithoutFactory() throws Exception {
    // setup queryProvider
    HsqlPagingQueryProvider queryProvider = new HsqlPagingQueryProvider();
    queryProvider.setSelectClause("select ID, NAME");
    queryProvider.setFromClause("from TEST");
    queryProvider.setWhereClause("where NAME <> 'foo'");
    queryProvider.setSortKey("ID");
    // call init to imitate spring context startup behaviour
    queryProvider.init(dataSource);/*from   w w w. j av  a  2s  .  c om*/

    // setup reader
    JdbcPagingItemReader<String> reader = new JdbcPagingItemReader<String>();
    reader.setDataSource(dataSource);
    reader.setQueryProvider(queryProvider);
    reader.setRowMapper(new ParameterizedRowMapper<String>() {

        @Override
        public String mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getString("NAME");
        }
    });
    reader.setPageSize(2);

    // needed call, normally done at spring application context startup
    reader.afterPropertiesSet();

    reader.open(MetaDataInstanceFactory.createStepExecution().getExecutionContext());
    // read
    try {
        int count = 0;
        String line;
        while ((line = reader.read()) != null) {
            assertEquals(String.valueOf(count), line);
            count++;
        }
        assertEquals(EXPECTED_COUNT, count);
    } catch (Exception e) {
        throw e;
    } finally {
        reader.close();
    }
}