简体   繁体   English

Mule 4 - 将字符串数组转换为单个字符串

[英]Mule 4 - Convert Array of strings to single string

I have an array of primary keys that i need for a SQL update statement.我有一个 SQL 更新语句所需的主键数组。 My goal here is to NOT loop through each primary key and execute an update statement many times.我的目标是不要循环遍历每个主键并多次执行更新语句。 What i want to do is create a string and only execute the statement 1 time.我想要做的是创建一个字符串并且只执行语句 1 次。

Here is my array of primary keys这是我的主键数组

["1234-114600000034-000093-ABCDE","1234-034-114600000378-FGH","1234- 
 114600000034-1146000-JKLMN","1234-1146034-00380-OPQRS","1234- 
 114600000034-1181-TUVWX"]

My desired output would be a string like this我想要的 output 将是这样的字符串

('1234-114600000034-000093-ABCDE','1234-034-114600000378-FGH','1234- 
 114600000034-1146000-JKLMN','1234-1146034-00380-OPQRS','1234- 
 114600000034-1181-TUVWX')

My SQL statement would look something like this我的 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')

Any help would be greatly appreciated.任何帮助将不胜感激。

You can use the joinBy function and then prepend and append the quote and parenthesis.您可以使用joinBy function 然后在 append 前面加上引号和括号。

%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  "','") ++ "')"

One of the ways to do so could be using reduce.其中一种方法是使用reduce。

Script脚本

%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 ')'

Output Output

('1234-114600000034-000093-ABCDE','1234-034-114600000378-FGH','1234-114600000034-1146000-JKLMN','1234-1146034-00380-OPQRS','1234-114600000034-1181-TUVWX')

There is another way to get desired Output还有另一种方法可以获得所需的 Output

in the below code, I am taking an array of primary keys as a variable and performing the logic on it在下面的代码中,我将主键数组作为变量并对其执行逻辑

%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 ")"

The output of above logic would be上述逻辑的 output 将是

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"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM