[英]Very dynamic JDBC query in Mule
I'm trying to create a flow in Mule that allow queries in a table with a dynamic where-statement. 我正在尝试在Mule中创建一个流,允许在具有动态where语句的表中进行查询。 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. 我使用hsqldb进行测试,并获得有关意外令牌的一些奇怪错误。
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: richher是一个groovy脚本,它将json结构转换为where-statement字符串:
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: 发送空参数会导致richher生成一个空字符串,错误是:
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: 发送一个参数(C1 = 0)会导致更丰富的产生字符串“where C1 = 0”,错误是:
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]
所以,看起来查询需要别的东西而不是我写的字符串#[variable:where-statement]
So, what do I need to feed into the where-statement
variable to make this work? 那么,我需要提供什么来进入where-statement
变量才能使其工作? Or, is there some other way to specify this? 或者,还有其他方法来指定这个吗?
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]" />
... ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.