Potentially off-by-one when setting SQL prepared statement parameters

Bug #705409 reported by Jesus Zazueta M on 2011-01-20
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Querydsl
Fix Released
Undecided
Unassigned

Bug Description

Hello.

Since using the SQL flavor of QueryDSL, I've noted that sometimes, when setting named parameters, queryDSL would successfully generate the SQL query for my entities, however, it would throw an excception similar to the following:

java.lang.IllegalArgumentException: Expected 9 paths, but got 8
 at com.mysema.query.sql.AbstractSQLQuery.setParameters(AbstractSQLQuery.java:483)
 at com.mysema.query.sql.AbstractSQLQuery.iterateMultiple(AbstractSQLQuery.java:332)
 at com.mysema.query.sql.AbstractSQLQuery.iterate(AbstractSQLQuery.java:299)
 at com.mysema.query.sql.AbstractSQLQuery.list(AbstractSQLQuery.java:438)

I believe the issue might still be present in release 2.0.9.

Example generated query:

8178 [main] DEBUG com.mysema.query.sql.AbstractSQLQuery - query :

select count(t.ID) TCount, to_char(t.CREATION_DATE,'DD-MON-YYYY') TransTime from TRANSACTIONS t
where t.CREATION_DATE >= ?
and t.CREATION_DATE <= ?
and t.MERCHANT_ID != ?
and t.RP_CONFIRMATION_NUM is not null
and t.RP_CONFIRMATION_NUM != ?
and cast(substr(t.RP_CONFIRMATION_NUM,?+1,1) as char) in (?, ?, ?, ?)
group by to_char(t.CREATION_DATE,'DD-MON-YYYY')
order by to_date(to_char(t.CREATION_DATE,'DD-MON-YYYY'),'DD-MON-YYYY') asc

I traced this error down to the method.

com.mysema.query.SQLSerializer::public Void visit(Constant<?> expr, Void context)

And the following code segment:

    int size = ((Collection)expr.getConstant()).size() - 1;
    for (int i = 0; i < size; i++){
        constantPaths.add(constantPaths.get(constantPaths.size()-1));
    }

Incrementing 'size' by one while debugging avoided the exception for me, and the query generated and ran successfully.

Is there any special reason for which the first line should not be instead:

    int size = ((Collection)expr.getConstant()).size();

Please let me know if there's anything I may be missing, or if there's anything else I could do or specify to help.

Thanks for your time!

Jesus Zazueta M (jjzazuet) wrote :

Addendum 1: My apologies, meant 'JDBC parameters' instead of 'named parameters' since we're dealing with positional jdbc parameters :P. Thanks!

Could you provide an example query where this fails?

 size -1 is used to add additional duplicates of the last added constant paths to make sure that for example with

  stringPath in ("1", ",2", "3")

you get the following path / constant pairs

  stringPath "1"
  stringPath "2"
  stringPath "3"

the first stringPath instance is added to the list of constantPaths before the constants are visited

Changed in querydsl:
status: New → In Progress
Jesus Zazueta M (jjzazuet) wrote :

Sure. The two classes should represent the issue I have in a compact way. Let me know if more info is needed.

Thanks again!

I commited a fix to SVN trunk. Could you verify that your queries work now?

The problem was with path.charAt(?) in chars style patterns. They were not treated properly.

Changed in querydsl:
status: In Progress → Fix Committed
Jesus Zazueta M (jjzazuet) wrote :

Ok, I'll give the source a try and let you know later today.

Released in 2.1.0-beta3

Changed in querydsl:
status: Fix Committed → Fix Released
Jesus Zazueta M (jjzazuet) wrote :

My apologies for the late response Timo. Jesus here.

The fix works in my case. Thanks again for your time and help!

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers

Bug attachments