在Mule中管理和操作大型sql查询的最佳方法

问题描述 投票:0回答:1

我需要在m子中运行大型sql查询,但是查询会根据有效负载而变化。我必须根据有效负载来修改列名称,where和条件分组等。

当前,我将模板查询放在资源文件夹中,例如test.sql。在查询中,我放置了一些要替换的关键字,例如“ replaceColumn”。然后,我使用set变量组件将关键字替换为所需的关键字,例如“ Field1Column”

变量:formQuery

%dw 2.0
output application/java
var query = readUrl('classpath://test.sql','text/plain')
---
query replace "replaceColumn" with payload.Field1Column

在数据库选择组件中,我简单地输入#[vars.formQuery]

此解决方案对我有效,但是通过嵌套replace运算符很难替换查询的许多部分。

什么是这样做的好方法?

mule mule-studio dataweave mulesoft mule-esb
1个回答
0
投票

您可以基于具有键/值的映射进行此递归替换

%dw 2.0
var x="The quick brown fox jumps over the lazy dog"
var myMap={fox:'chicken', dog:"me"}
var keys=myMap pluck ($$)
fun changeMe ( value, index ) = 
  if ( index < sizeOf(keys)  ) 
  changeMe( (value replace ( keys[index] ) with ( myMap[keys[index]] ) ) , index+1 ) 
  else value
output application/json
---
changeMe(x,0)

输出

"The quick brown chicken jumps over the lazy me"
© www.soinside.com 2019 - 2024. All rights reserved.