Mule 4 - 将字符串数组转换为单个字符串
Mule 4 - Convert Array of strings to single string
我有一个 SQL 更新语句需要的主键数组。我的目标是不循环遍历每个主键并多次执行更新语句。我想做的是创建一个字符串,只执行语句 1 次。
这是我的主键数组
["1234-114600000034-000093-ABCDE","1234-034-114600000378-FGH","1234-
114600000034-1146000-JKLMN","1234-1146034-00380-OPQRS","1234-
114600000034-1181-TUVWX"]
我想要的输出是这样的字符串
('1234-114600000034-000093-ABCDE','1234-034-114600000378-FGH','1234-
114600000034-1146000-JKLMN','1234-1146034-00380-OPQRS','1234-
114600000034-1181-TUVWX')
我的 SQL 语句看起来像这样
UPDATE [TABLE_NAME] SET [COLUMN_NAME] = 1 WHERE ID IN ('1234-114600000034-000093-ABCDE','1234-034-114600000378-FGH','1234-
114600000034-1146000-JKLMN','1234-1146034-00380-OPQRS','1234-
114600000034-1181-TUVWX')
如有任何帮助,我们将不胜感激。
其中一种方法是使用 reduce。
脚本
%dw 2.0
output application/java
var inp = ["1234-114600000034-000093-ABCDE","1234-034-114600000378-FGH","1234-114600000034-1146000-JKLMN","1234-1146034-00380-OPQRS","1234-114600000034-1181-TUVWX"]
---
('(' ++ (inp reduce ((item, acc = '') -> acc ++ "'" ++ item ++ "'," )) ++ ')') replace ',)' with ')'
输出
('1234-114600000034-000093-ABCDE','1234-034-114600000378-FGH','1234-114600000034-1146000-JKLMN','1234-1146034-00380-OPQRS','1234-114600000034-1181-TUVWX')
您可以使用 joinBy
函数,然后在前面和后面附加引号和括号。
%dw 2.0
output application/json
var ids = ["1234-114600000034-000093-ABCDE","1234-034-114600000378-FGH",
"1234-114600000034-1146000-JKLMN","1234-1146034-00380-OPQRS",
"1234-114600000034-1181-TUVWX"]
---
"UPDATE [TABLE_NAME] SET [COLUMN_NAME] = 1 WHERE ID IN ('"
++ (ids joinBy "','") ++ "')"
还有另一种方法可以获得所需的输出
在下面的代码中,我将主键数组作为变量并对其执行逻辑
%dw 2.0
output application/java
var inputPayload = ["1234-114600000034-000093-ABCDE","1234-034-114600000378-FGH","1234-114600000034-1146000-JKLMN","1234-1146034-00380-OPQRS","1234- 114600000034-1181-TUVWX"]
---
"UPDATE [TABLE_NAME] SET [COLUMN_NAME] = 1 WHERE ID IN " ++ ((write(inputPayload, "application/json")) replace "[" with "(") replace "]" with ")"
以上逻辑的输出为
UPDATE [TABLE_NAME) SET [COLUMN_NAME) = 1 WHERE ID IN (
"1234-114600000034-000093-ABCDE",
"1234-034-114600000378-FGH",
"1234-114600000034-1146000-JKLMN",
"1234-1146034-00380-OPQRS",
"1234- 114600000034-1181-TUVWX"
)
我有一个 SQL 更新语句需要的主键数组。我的目标是不循环遍历每个主键并多次执行更新语句。我想做的是创建一个字符串,只执行语句 1 次。
这是我的主键数组
["1234-114600000034-000093-ABCDE","1234-034-114600000378-FGH","1234-
114600000034-1146000-JKLMN","1234-1146034-00380-OPQRS","1234-
114600000034-1181-TUVWX"]
我想要的输出是这样的字符串
('1234-114600000034-000093-ABCDE','1234-034-114600000378-FGH','1234-
114600000034-1146000-JKLMN','1234-1146034-00380-OPQRS','1234-
114600000034-1181-TUVWX')
我的 SQL 语句看起来像这样
UPDATE [TABLE_NAME] SET [COLUMN_NAME] = 1 WHERE ID IN ('1234-114600000034-000093-ABCDE','1234-034-114600000378-FGH','1234-
114600000034-1146000-JKLMN','1234-1146034-00380-OPQRS','1234-
114600000034-1181-TUVWX')
如有任何帮助,我们将不胜感激。
其中一种方法是使用 reduce。
脚本
%dw 2.0
output application/java
var inp = ["1234-114600000034-000093-ABCDE","1234-034-114600000378-FGH","1234-114600000034-1146000-JKLMN","1234-1146034-00380-OPQRS","1234-114600000034-1181-TUVWX"]
---
('(' ++ (inp reduce ((item, acc = '') -> acc ++ "'" ++ item ++ "'," )) ++ ')') replace ',)' with ')'
输出
('1234-114600000034-000093-ABCDE','1234-034-114600000378-FGH','1234-114600000034-1146000-JKLMN','1234-1146034-00380-OPQRS','1234-114600000034-1181-TUVWX')
您可以使用 joinBy
函数,然后在前面和后面附加引号和括号。
%dw 2.0
output application/json
var ids = ["1234-114600000034-000093-ABCDE","1234-034-114600000378-FGH",
"1234-114600000034-1146000-JKLMN","1234-1146034-00380-OPQRS",
"1234-114600000034-1181-TUVWX"]
---
"UPDATE [TABLE_NAME] SET [COLUMN_NAME] = 1 WHERE ID IN ('"
++ (ids joinBy "','") ++ "')"
还有另一种方法可以获得所需的输出
在下面的代码中,我将主键数组作为变量并对其执行逻辑
%dw 2.0
output application/java
var inputPayload = ["1234-114600000034-000093-ABCDE","1234-034-114600000378-FGH","1234-114600000034-1146000-JKLMN","1234-1146034-00380-OPQRS","1234- 114600000034-1181-TUVWX"]
---
"UPDATE [TABLE_NAME] SET [COLUMN_NAME] = 1 WHERE ID IN " ++ ((write(inputPayload, "application/json")) replace "[" with "(") replace "]" with ")"
以上逻辑的输出为
UPDATE [TABLE_NAME) SET [COLUMN_NAME) = 1 WHERE ID IN (
"1234-114600000034-000093-ABCDE",
"1234-034-114600000378-FGH",
"1234-114600000034-1146000-JKLMN",
"1234-1146034-00380-OPQRS",
"1234- 114600000034-1181-TUVWX"
)