Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: JdbcCursorItemReader and 'IN' parameter lists

  1. #1
    Join Date
    Aug 2008
    Posts
    7

    Question JdbcCursorItemReader and 'IN' parameter lists

    Hi

    Spring's JdbcTemplate supports providing an array of arguments to be populated into a sql statement containing an "in"

    eg "SELECT column FROM tble WHERE id IN ( :values )"

    I've made use of this via the JdbcTemplate.queryForList().

    I would like to do something similar using the JdbcCursorItemReader?

    Looking at the exposed PreparedStatementSetter I am not sure that it is possible/supported.

    Is it possible to do so with the JCIR?
    thanks

  2. #2
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    It's not possible since there is no support for named parameters. What is your use case?
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Aug 2008
    Posts
    7

    Default

    The use case is to select entities using criteria that is based upon a configurable list of values.

    basically where column equals an item in the configured list.

    I guess I could code the number of currently configured values, resulting in a fairly brittle solution. Or dynamically generating the placeholders based upon the configuration.

    Are there other options?

    thanks
    Last edited by cousinsj; Aug 29th, 2008 at 04:46 AM.

  4. #4
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    The only thing that doesn't work is named parameters, can't you use normal '?' substitution based on an index? That's what the current implementation of StepExecutionPreparedStatementSetter uses.

  5. #5
    Join Date
    Aug 2008
    Posts
    7

    Default

    I can use the normal ? substitution but will require me to dynamically generate placeholders based upon the application configuration.
    I was trying to avoid that if it was possible / supported.

    thanks

  6. #6
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    This shouldn't be String manipulation. It should be setting it on the prepared statement, I don't understand how that's any more or less dynamic than the named parameter approach? Have you looked at the implementation of StepExecutionPreparedStatementSetter.
    Last edited by lucasward; Aug 29th, 2008 at 09:46 AM. Reason: clarification

  7. #7
    Join Date
    Aug 2008
    Posts
    7

    Default

    I hope I am not misunderstanding something here.

    I have looked at the StepExecutionPreparedStatementSetter impl, and understand that the parameters are being set upon the "ps" passed to the PreparedStatementSetter impl. A value for each parameter key set via the list set on the object.

    I understand this to require that a "?" placeholder must exist for each paramater key in the sql given to the JCIR, which then creates the PreparedStatement.

    Correct?

    If the number of parameters is to be dynamic then the sql supplied to the JCIR must have a ? for each parameter - correct?

  8. #8
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    The use case is to select entities using criteria that is based upon a configurable list of values.

    basically where column equals an item in the configured list.
    If you have a large number of values then including them in an IN clause is not a good solution - the JDBC driver doesn't have to support more than a hundred values and batch jobs typically exceed this. A better solution is to put the list of values in a table (value_list) and use that in your query -
    Code:
    select * from my_table
    where id in (select id from value_list)
    This way there is no limitation for the number of values and no need to use a variable parameter list for your SQL.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  9. #9

    Default

    how about

    Code:
    SELECT * FROM tble t
    INNER JOIN valuelist val ON t.id=val.id

    That will beat a subselect every time.

  10. #10
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    That will beat a subselect every time.
    What do you base that on?

    Your query would work as well, but Oracle's optimizer is pretty smart and would handle both queries in a similar if not identical manner.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •