Very dynamic JDBC query in Mule

2019-09-09 22:31发布

问题:

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.

I use hsqldb for testing and get some strange errors about unexpected token.

Here is the flow:

<script:transformer name="extractParameters">
  <script:script file="extractParameters.groovy" engine="groovy"/>
</script:transformer>

<flow name="datafetch.flow">
  <vm:inbound-endpoint address="vm://datafetch" exchange-pattern="request-response"/>
  <enricher target="#[variable:where-statement]">
    <transformer ref="extractParameters"/>
  </enricher>
  <jdbc:outbound-endpoint connector-ref="datasource" 
                          queryKey="parameterized-select" 
                          exchange-pattern="request-response" 
                          mimeType="text/plain">
    <jdbc:query key="parameterized-select" 
       value="select * from ${datasource.table} #[variable:where-statement]"/>
  </jdbc:outbound-endpoint>
  <json:object-to-json-transformer name="CaseInsensitiveHashMapToJson"/>
</flow>

The enricher is a groovy script that converts a json-structure to the where-statement string:

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 -> list << "${entry.key}=${entry.value}"}.join(" AND ")
if (where.isEmpty()) return result

result = "where " + where
return result

Sending empty parameters results in the enricher producing an empty string, and the errors are:

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)

Sending one parameter (C1=0) results in the enricher producing the string "where C1=0", and the errors are:

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)

So, it looks like the query expects something else than a string where I have written #[variable:where-statement]

So, what do I need to feed into the where-statement variable to make this work? Or, is there some other way to specify this?

回答1:

I tryed this way and it works:

<scripting:transformer doc:name="Groovy">
        <scripting:script engine="Groovy"><![CDATA[def q ="select * from myTable where 1=1 "; 
if(payload.containsKey('param1')) q = q + " And mycolumn = " + payload.param1;

 payload.put('dynamicWhere', q);
 return payload;]]></scripting:script>
   </scripting:transformer>
 <outbound-endpoint exchange-pattern="request-response"  connector-ref="ConnectorDb" doc:name="Database" address="jdbc://#[payload.dynamicWhere]" />

...



标签: jdbc mule