|
15 | 15 | */
|
16 | 16 | package org.springframework.sbm.mule.actions.javadsl.translators.db;
|
17 | 17 |
|
| 18 | + |
| 19 | +import lombok.AllArgsConstructor; |
| 20 | +import lombok.Data; |
| 21 | +import lombok.NoArgsConstructor; |
| 22 | +import org.mulesoft.schema.mule.db.AdvancedDbMessageProcessorType; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.regex.Matcher; |
| 27 | +import java.util.regex.Pattern; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +@Data |
| 31 | +@AllArgsConstructor |
| 32 | +@NoArgsConstructor |
| 33 | +class QueryWithParameters { |
| 34 | + private String query = ""; |
| 35 | + private List<String> muleExpressions = new ArrayList<>(); |
| 36 | +} |
| 37 | + |
| 38 | +@Data |
| 39 | +@AllArgsConstructor |
| 40 | +@NoArgsConstructor |
| 41 | +class QueryFunctionParameter { |
| 42 | + private String query = ""; |
| 43 | + private String arguments = ""; |
| 44 | +} |
| 45 | + |
18 | 46 | public class DBCommons {
|
19 | 47 | public static String escapeDoubleQuotes(String str) {
|
20 | 48 | return str.replace("\"", "\\\"");
|
21 | 49 | }
|
| 50 | + |
| 51 | + private static final String regexPattern = "#\\[(.+?)]"; |
| 52 | + private static final Pattern pattern = Pattern.compile(regexPattern); |
| 53 | + |
| 54 | + public static QueryWithParameters parseQueryParameter(String input) { |
| 55 | + |
| 56 | + if (input == null) { |
| 57 | + return new QueryWithParameters(); |
| 58 | + } |
| 59 | + |
| 60 | + Matcher m = pattern.matcher(input); |
| 61 | + |
| 62 | + List<String> muleExpressions = new ArrayList<>(); |
| 63 | + |
| 64 | + while (m.find()) { |
| 65 | + muleExpressions.add(m.group(1)); |
| 66 | + } |
| 67 | + |
| 68 | + return new QueryWithParameters(input |
| 69 | + .replaceAll(regexPattern, "?") |
| 70 | + .replace("'?'", "?") |
| 71 | + , muleExpressions); |
| 72 | + } |
| 73 | + |
| 74 | + public static QueryFunctionParameter extractQueryAndParameters(AdvancedDbMessageProcessorType component) { |
| 75 | + |
| 76 | + String query = component.getDynamicQuery() == null ? component.getParameterizedQuery() |
| 77 | + : component.getDynamicQuery(); |
| 78 | + QueryWithParameters queryWithParameters = DBCommons.parseQueryParameter(query); |
| 79 | + String argumentTemplate = " p.getFirst(\"%s\") /* TODO: Translate #[%s] to java expression*/"; |
| 80 | + String arguments = queryWithParameters |
| 81 | + .getMuleExpressions() |
| 82 | + .stream() |
| 83 | + .map(muleExpression -> String.format(argumentTemplate, muleExpression, muleExpression)) |
| 84 | + .collect(Collectors.joining(",\n")); |
| 85 | + |
| 86 | + if (!arguments.isEmpty()) { |
| 87 | + arguments = ",\n" + arguments + "\n"; |
| 88 | + } |
| 89 | + |
| 90 | + return new QueryFunctionParameter(queryWithParameters.getQuery(), arguments); |
| 91 | + } |
22 | 92 | }
|
0 commit comments