【问题标题】:Place Single quotes inside the String using Mule 4 Dataweave使用 Mule 4 Dataweave 在字符串中放置单引号
【发布时间】: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 括起来

任何帮助将不胜感激。

【问题讨论】:

    标签: mule dataweave mule4


    【解决方案1】:

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

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

    或者,您可以只转义单引号,但它看起来更令人困惑:

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

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 2018-05-06
      • 2017-05-20
      • 1970-01-01
      • 2014-08-08
      相关资源
      最近更新 更多