在Mule 4的流程中,我试图查询数据库中的特定数据。例如,我想运行这样的查询:
SELECT * FROM mulesoft WHERE plant = CCCNNB;问题是plant和CCNNB都需要是动态的。它们将通过API请求。我可以处理要动态的值,但是每当我试图使字段动态时,我就会得到空的结果。我首先创建一个变量,该变量存储来自请求的json:
set-variable value="#[payload]" doc:name="Set Variable" doc:id="8ed26865-d722-4fdb-9407-1f629b45d318" variableName="SORT_KEY"/>请求如下:
{
"FILTER_KEY": "plant",
"FILTER_VALS": "CCNNB"
}之后,在db连接器中配置以下内容:
<db:select doc:name="Select" doc:id="13a66f51-2a4e-4949-b383-86c43056f7a3" config-ref="Database_Config">
<db:sql><![CDATA[SELECT * FROM mulesoft WHERE :filter_key = :filter_val;]]></db:sql>
<db:input-parameters ><![CDATA[#[{
"filter_val": vars.SORT_KEY.FILTER_VALS,
"filter_key": vars.SORT_KEY.FILTER_KEY
}]]]></db:input-parameters>用:filter_key替换plant是有效的,但是一旦我试图使它具有动态性,我就不会在响应中得到任何信息。虽然它没有失败,响应代码是200,但是我在里面什么也没有。我怎么才能把这事做好?
发布于 2022-03-08 06:43:33
您可以直接使用查询本身中存储的变量。
查询应该是DataWeave中的表达式。
#["SELECT * FROM $(vars.table) WHERE $(vars.SORT_KEY.FILTER_KEY) = :filter_val"]<db:select config-ref="Database_Config">
<db:sql><![CDATA[#["SELECT * FROM $(vars.table) WHERE $(vars.SORT_KEY.FILTER_KEY) = :filter_val"]]]></db:sql>
<db:input-parameters ><![CDATA[#[{
"filter_val": vars.SORT_KEY.FILTER_VALS
}]]]>
</db:input-parameters>
</db:select>发布于 2022-03-08 10:11:55
还有另一种从有效负载读取值的方法,以构建动态查询,如下所示
#["SELECT * FROM mulesoft
WHERE " ++ vars.SORT_KEY.FILTER_KEY ++ " = '" ++ vars.SORT_KEY.FILTER_VALS ++ "'"]下面是作为POC为此创建的XML
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:os="http://www.mulesoft.org/schema/mule/os"
xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:xml-module="http://www.mulesoft.org/schema/mule/xml-module"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/os http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd">
<http:listener-config name="HTTP_Listener_config1"
doc:name="HTTP Listener config"
doc:id="6d5de64b-1355-4967-9352-4b324f02c7ad">
<http:listener-connection host="0.0.0.0"
port="8081" />
</http:listener-config>
<db:config name="Database_Config" doc:name="Database Config"
doc:id="d5c4d49c-aef3-4d4a-a7b5-470da3354127">
<db:my-sql-connection host="localhost"
port="3306" user="root" password="admin123" database="Mysql" />
</db:config>
<flow name="testFlow"
doc:id="8cfea1b0-d244-40d9-989c-e136af0d9f80" initialState="started">
<http:listener doc:name="Listener"
doc:id="265e671b-7d2f-4f3a-908c-8065a5f36a07"
config-ref="HTTP_Listener_config1" path="test" />
<set-variable value="#[payload]" doc:name="Set Variable"
doc:id="265a16c5-68d4-4217-8626-c4ab0a3e38e5" variableName="SORT_KEY" />
<db:select doc:name="Select"
doc:id="bdf4a59c-0bcc-46ac-8258-f1f1762c4e7f"
config-ref="Database_Config">
<db:sql><![CDATA[#["SELECT * FROM mulesoft.mulesoft WHERE " ++ vars.SORT_KEY.FILTER_KEY ++ " = '" ++ vars.SORT_KEY.FILTER_VALS ++ "'"]]]></db:sql>
</db:select>
<ee:transform doc:name="Transform Message"
doc:id="72cbe69f-c52e-4df9-ba5b-dd751990bc08">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
</mule>对流的解释
我正在使用Question
发布于 2022-04-04 14:55:22
所以这里有几个问题。第一,创建sql语句。如果需要,可以在DB:SELECT组件中执行DW操作,如前面的答案所示。要么是
#["SELECT * FROM myTable" ++ vars.myWhere]或
#["SELECT * FROM myTable $(vars.myWhere)"]工作。
您会遇到的问题是,DataSense不喜欢这样。如果没有列名的文字文本,DataSense将找不出要检索的列,因此它会引发一个“无法解析prameter: sql值”的错误。这在您的代码中留下了一个错误,这总是让我感到不安。我希望Mulesoft能解决这个问题。
顺便说一句,如果您执行动态SQL,您仍然应该为每个值使用输入参数,以避免SQL注入。
我在这里发布了一个“想法”来修复伪造的错误:https://help.mulesoft.com/s/ideas#0872T000000XbjkQAC
https://stackoverflow.com/questions/71390304
复制相似问题