I have a simple query using the IN clause. However, trying to get the JdbcCursorItemReader to work is a lot harder than it should be. I have tried using the example PreparedStatement option and the queryArguments and neither works. Considering how straight forward it would be to subsitute this with other implements it’s just frustrating to use especially with no concrete examples. I’d appreciate if someone can help me figure this out. The error is p.qualityCodes in (:qualityCodes)) ]: given 1 parameters but expected 0. I also tried using queryParameters(new Object[]{qualityCodes}) with the ? placeholder but that doesn’t work either.
Here is the reader configuration
@Configuration
@Slf4j
@RequiredArgsConstructor
public class UpsertProductStepsConfig {
@Qualifier("productDataSource")
final DataSource productDataSource;
final MongoOperations mongoOperations;
final JobRepository jobRepository;
final PlatformTransactionManager transactionManager;
final String schemaQuery = "select distinct p.code, p.category " +
" from product_schemas p " +
" where trunc(p.date_range) >= trunc(CURRENT_DATE)n" +
" and p.quality_code in (:qualityCodes)) "; // using ? instead of :qualityCodes does not work either
@Bean
public ItemReader<ClaritySchedule> codesReader() {
List<Long> codes = Arrays.asList(new Long[]{12344L, 342323L});
var parameters = new MapSqlParameterSource();
parameters.addValue("qualityCode", codes);
var parsedSql = NamedParameterUtils.parseSqlStatement(schemaQuery);
var sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, parameters);
log.info("SQL: {}", sqlToUse);
var declaredParams = NamedParameterUtils.buildSqlParameterList(parsedSql, parameters);
var params = NamedParameterUtils.buildValueArray(parsedSql, parameters, null);
var pscf = new PreparedStatementCreatorFactory(schemaQuery, declaredParams);
var pss = pscf.newPreparedStatementSetter(params);
var args = new Object[1];
args[0] = codes;
return new JdbcCursorItemReaderBuilder<ClaritySchedule>()
.name("cursorItemProductReader")
.dataSource(productDataSource)
.sql(schemaQuery)
//.preparedStatementSetter(pss) // does not work with ()
.queryArguments(args)
.rowMapper(new SchemaRowMapper())
.build();
}
}
The query works when placed in database console and parameters substituted.