You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Camel Guy (JIRA)" <ji...@apache.org> on 2014/11/10 22:26:34 UTC

[jira] [Issue Comment Deleted] (CAMEL-6473) NULL values are not supported in named parameters

     [ https://issues.apache.org/jira/browse/CAMEL-6473?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Camel Guy updated CAMEL-6473:
-----------------------------
    Comment: was deleted

(was: I appreciate that this is fixed. I spent the past 48 hours trying to figure this out. Eventually I found this JIRA and switched to 2.14.1-SNAPSHOT. When will 2.14.1 be released? I'm OK but I thought I would add my two cents. This was a killer as the error message implies that it's user error.)

> NULL values are not supported in named parameters
> -------------------------------------------------
>
>                 Key: CAMEL-6473
>                 URL: https://issues.apache.org/jira/browse/CAMEL-6473
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-sql
>    Affects Versions: 2.11.0
>            Reporter: Sergey Galkin
>             Fix For: 2.11.1, 2.12.0
>
>
> Due to the bug in the DefaultSqlPrepareStatementStrategy there no ability to use NULL values.
> Following query will be failed, if we try to use NULL  as a parameter value:
> bq. select a, b from foo where (:#param1 IS NOT NULL AND a > 12 ) OR (:#param2 IS NOT NULL AND b > 12)
> We'll get an error: 
> {quote}
> Caused by: java.sql.SQLException: Number of parameters mismatch. Expected: 2, was:1
> at org.apache.camel.component.sql.DefaultSqlPrepareStatementStrategy.populateStatement(DefaultSqlPrepareStatementStrategy.java:132) ~[camel-sql-2.11.0.jar:2.11.0]
> 	at org.apache.camel.component.sql.SqlProducer$1.doInPreparedStatement(SqlProducer.java:74) ~[camel-sql-2.11.0.jar:2.11.0]
> 	at org.apache.camel.component.sql.SqlProducer$1.doInPreparedStatement(SqlProducer.java:57) ~[camel-sql-2.11.0.jar:2.11.0]
> 	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:587) ~[spring-jdbc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
> {quote}
> Fix is quite simple: iterator implementation (returned by DefaultSqlPrepareStatementStrategy.createPopulateIterator()) should be changed as follows:
> {code:java}
>                 return new Iterator<Object>() {
>                     private NamedQueryParser parser = new NamedQueryParser(query);
>                     private String nextParam;
>                     private boolean done;
>                     @Override
>                     public boolean hasNext() {
>                         if (done) {
>                             return false;
>                         }
>                         
>                         if (nextParam == null) {
>                             nextParam = parser.next();
>                             if (nextParam == null) {
>                                 done = true;
>                             }
>                         }
>                         return nextParam != null;
>                     }
>                     @Override
>                     public Object next() {
>                         if(!hasNext()){
>                             throw new NoSuchElementException();
>                         }
>                         
>                         boolean contains = bodyMap != null ? bodyMap.containsKey(nextParam) : false;
>                         contains |= headerMap != null ? headerMap.containsKey(nextParam) : false;
>                         if (!contains) {
>                             throw new RuntimeExchangeException("Cannot find key [" + nextParam + "] in message body or headers to use when setting named parameter in query [" + query + "]", exchange);
>                         }
>                             
>                         // get from body before header
>                         Object next = bodyMap != null ? bodyMap.get(nextParam) : null;
>                         if (next == null) {
>                             next = headerMap != null ? headerMap.get(nextParam) : null;
>                         }
>                         nextParam = null;
>                         return next;
>                     }
>                     @Override
>                     public void remove() {
>                         // noop
>                     }
>                 };
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)