jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • groovy
  • GROOVY-5111

Named parameter support breaks with postgres casting syntax

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.8.2, 1.8.6, 2.0-beta-3
  • Fix Version/s: 2.0-rc-1, 1.8.7
  • Component/s: SQL processing
  • Labels:
    None

Description

Postgres supports a casting syntax like "SELECT '2011-01-01'::date". I recently wrote a query like: "SELECT * FROM table WHERE my_date BETWEEN ?-'1 week'::interval AND ?" and was surprised to receive strange errors about java.sql.Date not supporting the "interval" property. It would appear that the regex parsing that identifies named parameters is a bit broken in this instance.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
David Sutherland added a comment - 09/May/12 7:59 AM

In groovy.sql.Sql changing:

private static final Pattern NAMED_QUERY_PATTERN = Pattern.compile("(?::|\\?(\\d?)\\.?)(\\w*)");

to:

private static final Pattern NAMED_QUERY_PATTERN = Pattern.compile("(?<!:)(:|\\?(\\d*)\\.)(\\w+)");

should fix this problem. It matches:

:foo 
?.bar 
?1.baz 
?12.baz

but not:

:
::asdf

It does not cover the case when something that looks like a named parameter is contained in a string e.g:

select * from some_table where description = ':notANamedParameter'

This case is probably hard to cover in a regex as it must check that it is not a named parameter surrounded by strings e.g.:

':notANamedParameter' :aNamedParameter ':alsoNotANamedParameter'

So while this is not a perfect solution it adds functionality without removing any (as far as I can tell).

Show
David Sutherland added a comment - 09/May/12 7:59 AM In groovy.sql.Sql changing:
private static final Pattern NAMED_QUERY_PATTERN = Pattern.compile("(?::|\\?(\\d?)\\.?)(\\w*)");
to:
private static final Pattern NAMED_QUERY_PATTERN = Pattern.compile("(?<!:)(:|\\?(\\d*)\\.)(\\w+)");
should fix this problem. It matches:
:foo 
?.bar 
?1.baz 
?12.baz
but not:
:
::asdf
It does not cover the case when something that looks like a named parameter is contained in a string e.g:
select * from some_table where description = ':notANamedParameter'
This case is probably hard to cover in a regex as it must check that it is not a named parameter surrounded by strings e.g.:
':notANamedParameter' :aNamedParameter ':alsoNotANamedParameter'
So while this is not a perfect solution it adds functionality without removing any (as far as I can tell).
Hide
Permalink
Paul King added a comment - 10/May/12 6:35 AM

@Lucian, try setting enableNamedQueries=false.

@David, that looks close. We need to also support ordinal (non-named) params and we currently cover both cases with a single regex, so we need to support ?1 for instance. I have adapted your proposed solution and all tests pass - I will commit once I have a good test case.

Show
Paul King added a comment - 10/May/12 6:35 AM @Lucian, try setting enableNamedQueries=false. @David, that looks close. We need to also support ordinal (non-named) params and we currently cover both cases with a single regex, so we need to support ?1 for instance. I have adapted your proposed solution and all tests pass - I will commit once I have a good test case.
Hide
Permalink
Lucian Holland added a comment - 10/May/12 6:49 AM

@Paul, Thanks for the suggestion. Unfortunately it's been a while since I was working on this project and I'm rather snowed under with another project in a completely different environment... I can't remember off the top of my head whether we needed named parameter support or not, but I suspect we might have been able to work around it like that...

Show
Lucian Holland added a comment - 10/May/12 6:49 AM @Paul, Thanks for the suggestion. Unfortunately it's been a while since I was working on this project and I'm rather snowed under with another project in a completely different environment... I can't remember off the top of my head whether we needed named parameter support or not, but I suspect we might have been able to work around it like that...
Hide
Permalink
Paul King added a comment - 12/May/12 6:10 AM

changed as per the suggestion (thanks) with some minor changes

Show
Paul King added a comment - 12/May/12 6:10 AM changed as per the suggestion (thanks) with some minor changes

People

  • Assignee:
    Paul King
    Reporter:
    Lucian Holland
Vote (0)
Watch (0)

Dates

  • Created:
    01/Nov/11 11:58 AM
    Updated:
    12/May/12 6:10 AM
    Resolved:
    12/May/12 6:10 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.