使用 Mule 4 Dataweave 在字符串中放置单引号

Place Single quotes inside the String using Mule 4 Dataweave

我们正在使用 Mule 4(运行时 4.3.0)和 Dataweave 2.0

数据库希望查询类似于

Select id from table where firstName = 'test-from-text';

名字的值来自查询参数 firstName。我们正在构建 SQL 类似 mule 的查询:-

<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 String

中用单引号括起 vars.firstName

如有任何帮助,我们将不胜感激。

对字符串文字使用双引号,因此单引号只是另一个字符:

%dw 2.0
output application/java
---
"SELECT id
FROM table WHERE firstName = '" ++ vars.firstName ++ "'"

或者你可以只转义单引号,但它看起来更混乱:

'SELECT id
FROM table WHERE firstName = \'' ++ vars.firstName ++ '\''