166 lines
6.6 KiB
Java
166 lines
6.6 KiB
Java
package com.example.processor;
|
||
|
||
import org.apache.camel.Exchange;
|
||
import org.apache.camel.Processor;
|
||
import org.apache.camel.Configuration;
|
||
import org.apache.camel.BindToRegistry;
|
||
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
|
||
import java.util.Map;
|
||
import java.util.regex.Matcher;
|
||
import java.util.regex.Pattern;
|
||
|
||
@Configuration
|
||
@BindToRegistry("sqlTemplateProcessor")
|
||
public class SqlTemplateProcessor implements Processor {
|
||
|
||
private static final Logger LOG = LoggerFactory.getLogger(UserProcessor.class);
|
||
// private static final Pattern IF_PATTERN = Pattern.compile("#\\{if\\}\\((.+?)\\)(.+?)#\\{end\\}", Pattern.DOTALL);
|
||
// private static final Pattern IF_ELSE_PATTERN = Pattern.compile("#\\{if\\}\\((.+?)\\)(.+?)#\\{else\\}(.+?)#\\{end\\}", Pattern.DOTALL);
|
||
// 简化的正则表达式,可能更容易匹配
|
||
private static final Pattern IF_PATTERN = Pattern.compile("#\\{if\\}\\(([^)]+)\\)([^#]+)#\\{end\\}", Pattern.DOTALL);
|
||
private static final Pattern IF_ELSE_PATTERN = Pattern.compile("#\\{if\\}\\(([^)]+)\\)([^#]+)#\\{else\\}([^#]+)#\\{end\\}", Pattern.DOTALL);
|
||
|
||
@Override
|
||
public void process(Exchange exchange) throws Exception {
|
||
// 获取原始SQL模板
|
||
String sqlTemplate = exchange.getIn().getHeader("currentSqlString",String.class);
|
||
LOG.info("IF_PATTERN: {}", IF_PATTERN);
|
||
LOG.info("IF_ELSE_PATTERN: {}", IF_ELSE_PATTERN);
|
||
LOG.info("处理前的sql: {}", sqlTemplate);
|
||
// 获取参数
|
||
Map<String, Object> params = exchange.getIn().getBody(Map.class);
|
||
LOG.info("获取到的body里的参数: {}", params);
|
||
if (params == null) {
|
||
params = exchange.getIn().getHeaders();
|
||
}
|
||
|
||
LOG.info("模板处理的参数: {}", params);
|
||
// 处理模板
|
||
String processedSql = processSqlTemplate(sqlTemplate, params);
|
||
|
||
LOG.info("处理后的sql: {}", processedSql);
|
||
// 设置处理后的SQL
|
||
exchange.getIn().setBody(processedSql);
|
||
}
|
||
|
||
/**
|
||
* 处理SQL模板,替换条件部分
|
||
*/
|
||
private String processSqlTemplate(String template, Map<String, Object> params) {
|
||
// String result = template;
|
||
|
||
// // 处理if-else条件
|
||
// Matcher ifElseMatcher = IF_ELSE_PATTERN.matcher(result);
|
||
// while (ifElseMatcher.find()) {
|
||
|
||
// String condition = ifElseMatcher.group(1);
|
||
// String ifBlock = ifElseMatcher.group(2);
|
||
// String elseBlock = ifElseMatcher.group(3);
|
||
|
||
// boolean conditionResult = evaluateCondition(condition, params);
|
||
// String replacement = conditionResult ? ifBlock : elseBlock;
|
||
|
||
// LOG.info("ifElseMatcher.group(0): {}", ifElseMatcher.group(0));
|
||
// result = result.replace(ifElseMatcher.group(0), replacement);
|
||
// }
|
||
// LOG.info("ifelse处理结果: {}", result);
|
||
|
||
// // 处理if条件
|
||
// Matcher ifMatcher = IF_PATTERN.matcher(result);
|
||
// while (ifMatcher.find()) {
|
||
// String condition = ifMatcher.group(1);
|
||
// String ifBlock = ifMatcher.group(2);
|
||
|
||
// boolean conditionResult = evaluateCondition(condition, params);
|
||
// String replacement = conditionResult ? ifBlock : "";
|
||
|
||
// result = result.replace(ifMatcher.group(0), replacement);
|
||
// }
|
||
// LOG.info("if处理结果: {}", result);
|
||
|
||
// return result.trim().replaceAll("\\s+", " ");
|
||
|
||
String result = template;
|
||
|
||
// 处理if-else条件
|
||
Matcher ifElseMatcher = IF_ELSE_PATTERN.matcher(result);
|
||
StringBuffer sb = new StringBuffer();
|
||
|
||
while (ifElseMatcher.find()) {
|
||
String condition = ifElseMatcher.group(1);
|
||
String ifBlock = ifElseMatcher.group(2);
|
||
String elseBlock = ifElseMatcher.group(3);
|
||
|
||
boolean conditionResult = evaluateCondition(condition, params);
|
||
String replacement = conditionResult ? ifBlock : elseBlock;
|
||
|
||
// 使用quoteReplacement处理替换字符串中的特殊字符
|
||
ifElseMatcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
|
||
}
|
||
ifElseMatcher.appendTail(sb);
|
||
result = sb.toString();
|
||
|
||
// 处理if条件
|
||
Matcher ifMatcher = IF_PATTERN.matcher(result);
|
||
sb = new StringBuffer();
|
||
|
||
while (ifMatcher.find()) {
|
||
String condition = ifMatcher.group(1);
|
||
String ifBlock = ifMatcher.group(2);
|
||
|
||
boolean conditionResult = evaluateCondition(condition, params);
|
||
String replacement = conditionResult ? ifBlock : "";
|
||
|
||
// 使用quoteReplacement处理替换字符串中的特殊字符
|
||
ifMatcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
|
||
}
|
||
ifMatcher.appendTail(sb);
|
||
|
||
return sb.toString().trim().replaceAll("\\s+", " ");
|
||
}
|
||
|
||
/**
|
||
* 评估条件表达式
|
||
*/
|
||
private boolean evaluateCondition(String condition, Map<String, Object> params) {
|
||
// 简单解析条件表达式
|
||
// 这里只是一个示例实现,可能需要更复杂的表达式解析逻辑
|
||
condition = condition.trim();
|
||
|
||
if (condition.contains("!=")) {
|
||
String[] parts = condition.split("!=");
|
||
String paramName = parts[0].trim().replace(":?", "");
|
||
String value = parts[1].trim().replace("'", "");
|
||
|
||
Object paramValue = params.get(paramName);
|
||
if (paramValue == null) return false;
|
||
|
||
if (value.equals("null")) {
|
||
return paramValue != null;
|
||
} else if (value.equals("''")) {
|
||
return paramValue != null && !paramValue.toString().isEmpty();
|
||
} else {
|
||
return !paramValue.toString().equals(value);
|
||
}
|
||
} else if (condition.contains("=")) {
|
||
String[] parts = condition.split("=");
|
||
String paramName = parts[0].trim().replace(":?", "");
|
||
String value = parts[1].trim().replace("'", "");
|
||
|
||
Object paramValue = params.get(paramName);
|
||
|
||
if (value.equals("null")) {
|
||
return paramValue == null;
|
||
} else if (value.equals("''")) {
|
||
return paramValue == null || paramValue.toString().isEmpty();
|
||
} else {
|
||
return paramValue != null && paramValue.toString().equals(value);
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
} |