【发布时间】:2021-10-12 09:58:17
【问题描述】:
我们正在使用 Mule 4(运行时 4.3.0)和 Dataweave 2.0
数据库期望查询类似于
Select id from table where firstName = 'test-from-text';
名字的值来自查询参数 firstName。我们正在像下面这样在 mule 中构建 SQL 查询:-
<ee:transform doc:name="firstName" doc:id="1e151bed-d1c4-4af2-b2f6-91f9cbf1f89d">
<ee:message>
</ee:message>
<ee:variables>
<ee:set-variable variableName="firstName"><![CDATA[%dw 2.0
output application/java
---
attributes.queryParams.firstName]]></ee:set-variable></ee:variables>
</ee:transform>
<ee:transform doc:name="Sql Query" doc:id="765c361e-e400-45ad-810a-c504169eb4ad" >
<ee:message >
</ee:message>
<ee:variables >
<ee:set-variable variableName="sqlQuery" ><![CDATA[%dw 2.0
output application/java
---
'SELECT id
FROM table WHERE firstName =' ++vars.firstName
</ee:set-variable>
</ee:variables>
</ee:transform>
我们将 vars.sqlQuery 传递给数据库连接器。上面的代码会生成类似“select id from the table where firstName = test-from-text”的查询。数据库期望查询类似于
Select id from table where firstName = 'test-from-text';
注意:
我们需要在像这里这样的变量中构造查询(原因:它周围有很多条件)。我们确实有一个类似日期的列,我们需要在其中提到引号。
在上面的名字中包含“from”,这是一个数据库保留关键字。当我们在带有引号的 sql 编辑器中查询时,它可以工作。
问题: 如何在 vars.SqlQuery 字符串中用单引号将 vars.firstName 括起来
任何帮助将不胜感激。
【问题讨论】: