Commit fcc5778a authored by yanzg's avatar yanzg

SQL层级处理的支持

parent ca4e45ce
package com.yanzuoguang.util.printer;
package com.yanzuoguang.util.helper;
public interface PrinterFormatHandle {
/**
* 字符串格式化参数
*
* @author 颜佐光
*/
public interface StringFormatHandle {
/**
* 对模板进行格式化处理
......
......@@ -1059,19 +1059,56 @@ public class StringHelper {
* @return 处理后的值对象
*/
public static String getCodeString(String from, Object target) {
String regex = "\\{(.*?)\\}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(from);
// 寻找到的代码片段 不包含分括号
while (m.find()) {
String name = m.group();
String key = m.group(1);
String value = StringHelper.getFirst(ObjectHelper.getString(target, key), EMPTY);
from = from.replace(name, value);
return getFormat(from, StringHelper.EMPTY, new StringFormatHandle() {
/**
* 对模板进行格式化处理
*
* @param sb 返回得结果模板
* @param fieldFull 全格式
* @param field 字段
* @param command 字段格式化
*/
@Override
public void addPos(StringBuilder sb, String fieldFull, String field, String command) {
String value = StringHelper.getFirst(ObjectHelper.getString(target, field), EMPTY);
sb.append(value);
}
});
}
return from;
/**
* 进行字段格式化处理
*
* @param format 需要处理得格式字符串
* @param defaultField 默认字段
* @param fieldFormatHandle 对字段进行格式化处理
* @return 处理之后得值
*/
public static String getFormat(String format, String defaultField, StringFormatHandle fieldFormatHandle) {
StringBuilder sb = new StringBuilder();
Pattern reg = Pattern.compile("\\{(.+?)\\}");
Matcher matches = reg.matcher(format);
int start = 0;
while (matches.find()) {
int len = matches.start() - start;
if (len > 0) {
sb.append(format.substring(start, matches.start()));
}
String fieldFull = matches.group(1);
if (fieldFull.startsWith("0") && !StringHelper.isEmpty(defaultField)) {
fieldFull = defaultField + fieldFull.substring(1);
}
String[] fields = fieldFull.split(":");
String field = fields[0];
String fieldFormat = StringHelper.EMPTY;
if (fields.length > 1) {
fieldFormat = fieldFull.substring(field.length() + 1);
}
fieldFormatHandle.addPos(sb, fieldFull, field, fieldFormat);
start = matches.end();
}
sb.append(format.substring(start));
return sb.toString();
}
}
......@@ -9,6 +9,7 @@ import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.ArrayHelper;
import com.yanzuoguang.util.helper.StringFormatHandle;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.*;
......@@ -27,7 +28,7 @@ public abstract class BaseDaoSql {
/**
* SQL语句支持的最大长度
*/
public static int MAX_SQL_LENGTH = 1024 * 20;
public static int MAX_LEVEL = 10;
/**
* 定义列表记录缓存对象
......@@ -577,24 +578,33 @@ public abstract class BaseDaoSql {
* @return Sql语句值
*/
protected String handleCodeReplace(SqlData sqlData, String sql, Map<String, List<String>> codeMap) {
// 正则表达式匹配代码片段,并根据代码片段获取数组进行增加
String regex = "\\{.*?\\}";
Pattern p = Pattern.compile(regex);
// 最大处理层数量,默认最小1层
int level = Math.max(1, MAX_LEVEL);
for (int i = 0; i < level; i++) {
// 判断是否寻找到标签
final boolean[] find = {false};
sql = StringHelper.getFormat(sql, StringHelper.EMPTY, new StringFormatHandle() {
@Override
public void addPos(StringBuilder sb, String fieldFull, String field, String command) {
// 设置寻找到标签
find[0] = true;
// 寻找到的代码片段 不包含分括号
String name = fieldFull;
List<String> codes = codeMap.containsKey(name) ? codeMap.get(name) : new ArrayList<>();
for (String code : codes) {
sb.append(" ");
sb.append(code);
}
sb.append(" ");
}
});
do {
Matcher m = p.matcher(sql);
if (!m.find()) {
// 当没有寻找到时,则进行其他处理
if (!find[0]) {
break;
}
if (sql.length() > MAX_SQL_LENGTH) {
throw new CodeException("SQL语句" + this.getClass().getSimpleName() + "." + sqlData.getName() + "过长,不能再次处理代码片段");
}
// 寻找到的代码片段 不包含分括号
String name = m.group();
List<String> codes = codeMap.containsKey(name) ? codeMap.get(name) : new ArrayList<>();
String code = String.format(" %s ", String.join(" ", codes));
sql = sql.replace(name, code);
} while (true);
sql = sql.replaceAll("\\s+", " ");
......
......@@ -4,9 +4,9 @@ package com.yanzuoguang.util;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringFormatHandle;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.printer.ConvertPlan;
import com.yanzuoguang.util.printer.PrinterFormatHandle;
import com.yanzuoguang.util.printer.format.FormatCenter;
import com.yanzuoguang.util.printer.vo.PrinterPagerData;
import com.yanzuoguang.util.printer.vo.PrinterPagerItemData;
......@@ -15,8 +15,6 @@ import com.yanzuoguang.util.vo.MapRow;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 构造函数
......@@ -25,41 +23,6 @@ import java.util.regex.Pattern;
*/
public class PrinterHelper {
/**
* 进行字段格式化处理
*
* @param format 需要处理得格式字符串
* @param defaultField 默认字段
* @param fieldFormatHandle 对字段进行格式化处理
* @return 处理之后得值
*/
public static String getFormat(String format, String defaultField, PrinterFormatHandle fieldFormatHandle) {
StringBuilder sb = new StringBuilder();
Pattern reg = Pattern.compile("\\{(\\S+?)\\}");
Matcher matches = reg.matcher(format);
int start = 0;
while (matches.find()) {
int len = matches.start() - start;
if (len > 0) {
sb.append(format.substring(start, matches.start()));
}
String fieldFull = matches.group(1);
if (fieldFull.startsWith("0")) {
fieldFull = defaultField + fieldFull.substring(1);
}
String[] fields = fieldFull.split(":");
String field = fields[0];
String fieldFormat = StringHelper.EMPTY;
if (fields.length > 1) {
fieldFormat = fieldFull.substring(field.length() + 1);
}
fieldFormatHandle.addPos(sb, fieldFull, field, fieldFormat);
start = matches.end();
}
sb.append(format.substring(start));
return sb.toString();
}
/**
* 将来源模板与数据转换为目标模板和数据
*
......@@ -123,7 +86,7 @@ public class PrinterHelper {
item.setFormat("{0}");
}
// 生成目标格式化字符串
String format = getFormat(item.getFormat(), item.getName(), new PrinterFormatHandle() {
String format = StringHelper.getFormat(item.getFormat(), item.getName(), new StringFormatHandle() {
@Override
public void addPos(StringBuilder sb, String fieldFull, String field, String command) {
// 对现有的字段进行加密
......@@ -166,7 +129,7 @@ public class PrinterHelper {
*/
public static String getFormatTo(Map<String, Object> resultTo, Object resultFrom,
String format, String defaultField) {
return getFormat(format, defaultField, new PrinterFormatHandle() {
return StringHelper.getFormat(format, defaultField, new StringFormatHandle() {
@Override
public void addPos(StringBuilder sb, String fieldFull, String field, String command) {
sb.append("{");
......@@ -190,7 +153,7 @@ public class PrinterHelper {
* @return
*/
public static String getResult(Object resultFrom, String format) {
return getFormat(format, StringHelper.EMPTY, new PrinterFormatHandle() {
return StringHelper.getFormat(format, StringHelper.EMPTY, new StringFormatHandle() {
@Override
public void addPos(StringBuilder sb, String fieldFull, String field, String command) {
Object fieldValue = ObjectHelper.get(resultFrom, field);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment