title: Very dynamic JDBC query in Mule
body: <p>I'm trying to create a flow in Mule that allow queries in a table with a dynamic where-statement.
  The idea is to send a map of parameters with zero or more entries and then build a query string from that.</p>
  
  <p>I use hsqldb for testing and get some strange errors about unexpected token.</p>
  
  <p>Here is the flow:</p>
  
  <pre><code>&lt;script:transformer name="extractParameters"&gt;
    &lt;script:script file="extractParameters.groovy" engine="groovy"/&gt;
  &lt;/script:transformer&gt;
  
  &lt;flow name="datafetch.flow"&gt;
    &lt;vm:inbound-endpoint address="vm://datafetch" exchange-pattern="request-response"/&gt;
    &lt;enricher target="#[variable:where-statement]"&gt;
      &lt;transformer ref="extractParameters"/&gt;
    &lt;/enricher&gt;
    &lt;jdbc:outbound-endpoint connector-ref="datasource" 
                            queryKey="parameterized-select" 
                            exchange-pattern="request-response" 
                            mimeType="text/plain"&gt;
      &lt;jdbc:query key="parameterized-select" 
         value="select * from ${datasource.table} #[variable:where-statement]"/&gt;
    &lt;/jdbc:outbound-endpoint&gt;
    &lt;json:object-to-json-transformer name="CaseInsensitiveHashMapToJson"/&gt;
  &lt;/flow&gt;
  </code></pre>
  
  <p>The enricher is a groovy script that converts a json-structure to the where-statement string:</p>
  
  <pre><code>import groovy.json.JsonSlurper
  
  def input = new JsonSlurper().parseText(payload)
  def parameters = input?.get("parameters")
  def result = ""
  if(parameters==null) return result
  
  def where = parameters.inject([]) { list, entry -&gt; list &lt;&lt; "${entry.key}=${entry.value}"}.join(" AND ")
  if (where.isEmpty()) return result
  
  result = "where " + where
  return result
  </code></pre>
  
  <p>Sending empty parameters results in the enricher producing an empty string, and the errors are:</p>
  
  <pre><code>1. unexpected token: ? (org.hsqldb.HsqlException)
    org.hsqldb.error.Error:-1 (null)
  2. unexpected token: ?(SQL Code: -5581, SQL State: + 42581) (java.sql.SQLSyntaxErrorException)
    org.hsqldb.jdbc.Util:-1 (null)
  3. unexpected token: ? Query: select * from TABLE1 ? Parameters: [](SQL Code: -5581, SQL State: + 42581) (java.sql.SQLException)
    org.apache.commons.dbutils.QueryRunner:540 (null)
  </code></pre>
  
  <p>Sending one parameter (C1=0) results in the enricher producing the string "where C1=0", and the errors are:</p>
  
  <pre><code>1. unexpected token: ? (org.hsqldb.HsqlException)
    org.hsqldb.error.Error:-1 (null)
  2. unexpected token: ?(SQL Code: -5581, SQL State: + 42581) (java.sql.SQLSyntaxErrorException)
    org.hsqldb.jdbc.Util:-1 (null)
  3. unexpected token: ? Query: select * from TABLE1 ? Parameters: [where C1=0](SQL Code: -5581, SQL State: + 42581) (java.sql.SQLException)
    org.apache.commons.dbutils.QueryRunner:540 (null)
  </code></pre>
  
  <p>So, it looks like the query expects something else than a string where I have written <code>#[variable:where-statement]</code></p>
  
  <p>So, what do I need to feed into the <code>where-statement</code> variable to make this work?
  Or, is there some other way to specify this?</p>