【解析SQL模板-1】Mybatis的SQL模板组合成可运行的SQL

背景

实现平台化的mybatis能力,即在页面上输入mybatis的SQL模板,并传入参数,最终解析成可运行的SQL。

实现原理

引入依赖:

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.7</version>
</dependency>

mybatis的SQL生成器:

  1. 解析mybatis模板,生成预编译的SQL;
  2. 解析预编译SQL,参数替换?;
@Slf4j
public class MybatisGenerator {

    private static final String HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis"
            + ".org/dtd/mybatis-3-mapper.dtd\">"
            + "<mapper namespace=\"customGenerator\">"
            + "<select id=\"selectData\" parameterType=\"map\" resultType=\"map\">\n";

    private static final String FOOT = "\n</select></mapper>";

    private static final LoadingCache<String, MappedStatement> mappedStatementCache = CacheBuilder.newBuilder()
            .refreshAfterWrite(1, TimeUnit.DAYS)
            .build(new CacheLoader<String, MappedStatement>() {
                @Override
                public MappedStatement load(@NotNull String key) {
                    Configuration configuration = new Configuration();
                    configuration.setShrinkWhitespacesInSql(true);
                    String sourceSQL = HEAD + key + FOOT;
                    XMLMapperBuilder xmlMapperBuilder =
                            new XMLMapperBuilder(IOUtils.toInputStream(sourceSQL, Charset.forName("UTF-8")),
                                    configuration, null,
                                    null);
                    xmlMapperBuilder.parse();
                    return xmlMapperBuilder.getConfiguration().getMappedStatement("selectData");
                }
            });

    //生成完整SQL
    public static String generateDsl(SQLConfig apiConfig, Map<String, Object> conditions) {
        String sql = apiConfig.getSqlTemplate();
        try {
            MappedStatement mappedStatement = mappedStatementCache.getUnchecked(sql);
            BoundSql boundSql = mappedStatement.getBoundSql(conditions);
            if (!boundSql.getParameterMappings().isEmpty()) {
                List<PreparedStatementParameter> parameters = boundSql.getParameterMappings()
                        .stream().map(ParameterMapping::getProperty)
                        .map(param -> Optional.ofNullable(boundSql.getAdditionalParameter(param))
                                .orElseGet(() -> conditions.get(param)))
                        .map(PreparedStatementParameter::fromObject)
                        .collect(Collectors.toList());
                //解析占位符,获取到完整SQL
                return PreparedStatementParser.parse(boundSql.getSql()).buildSql(parameters);
            } else {
                return boundSql.getSql();
            }
        } catch (UncheckedExecutionException e) {
            throw e;
        }
    }

    @Data
    public static class SQLConfig {
        //SQL模板
        private String sqlTemplate;
    }
}

因为需要处理?(占位符),所以需要判断是否进行转义处理。


public final class ValueFormatter {
    private static final Escaper ESCAPER = Escapers.builder()
            .addEscape('\\', "\\\\")
            .addEscape('\n', "\\n")
            .addEscape('\t', "\\t")
            .addEscape('\b', "\\b")
            .addEscape('\f', "\\f")
            .addEscape('\r', "\\r")
            .addEscape('\u0000', "\\0")
            .addEscape('\'', "\\'")
            .addEscape('`', "\\`")
            .build();

    public static final String NULL_MARKER = "\\N";
    private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
    private static final ThreadLocal<SimpleDateFormat> DATE_TIME_FORMAT =
            ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    public static String formatBytes(byte[] bytes) {
        if (bytes == null) {
            return null;
        } else {
            char[] hexArray =
                    new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
            char[] hexChars = new char[bytes.length * 4];

            for (int j = 0; j < bytes.length; ++j) {
                int v = bytes[j] & 255;
                hexChars[j * 4] = '\\';
                hexChars[j * 4 + 1] = 'x';
                hexChars[j * 4 + 2] = hexArray[v / 16];
                hexChars[j * 4 + 3] = hexArray[v % 16];
            }

            return new String(hexChars);
        }
    }

    public static String formatInt(int myInt) {
        return Integer.toString(myInt);
    }

    public static String formatDouble(double myDouble) {
        return Double.toString(myDouble);
    }

    public static String formatChar(char myChar) {
        return Character.toString(myChar);
    }

    public static String formatLong(long myLong) {
        return Long.toString(myLong);
    }

    public static String formatFloat(float myFloat) {
        return Float.toString(myFloat);
    }

    public static String formatBigDecimal(BigDecimal myBigDecimal) {
        return myBigDecimal != null ? myBigDecimal.toPlainString() : "\\N";
    }

    public static String formatShort(short myShort) {
        return Short.toString(myShort);
    }

    public static String formatString(String myString) {
        return escape(myString);
    }

    public static String formatNull() {
        return "\\N";
    }

    public static String formatByte(byte myByte) {
        return Byte.toString(myByte);
    }

    public static String formatBoolean(boolean myBoolean) {
        return myBoolean ? "1" : "0";
    }

    public static String formatUUID(UUID x) {
        return x.toString();
    }

    public static String formatBigInteger(BigInteger x) {
        return x.toString();
    }

    public static String formatObject(Object x) {
        if (x == null) {
            return null;
        } else if (x instanceof Byte) {
            return formatInt(((Byte) x).intValue());
        } else if (x instanceof String) {
            return formatString((String) x);
        } else if (x instanceof BigDecimal) {
            return formatBigDecimal((BigDecimal) x);
        } else if (x instanceof Short) {
            return formatShort((Short) x);
        } else if (x instanceof Integer) {
            return formatInt((Integer) x);
        } else if (x instanceof Long) {
            return formatLong((Long) x);
        } else if (x instanceof Float) {
            return formatFloat((Float) x);
        } else if (x instanceof Double) {
            return formatDouble((Double) x);
        } else if (x instanceof byte[]) {
            return formatBytes((byte[]) x);
        } else if (x instanceof Boolean) {
            return formatBoolean((Boolean) x);
        } else if (x instanceof UUID) {
            return formatUUID((UUID) x);
        } else if (x instanceof BigInteger) {
            return formatBigInteger((BigInteger) x);
        } else {
            return String.valueOf(x);
        }
    }

    public static boolean needsQuoting(Object o) {
        if (o == null) {
            return false;
        } else if (o instanceof Number) {
            return false;
        } else if (o instanceof Boolean) {
            return false;
        } else if (o.getClass().isArray()) {
            return false;
        } else {
            return !(o instanceof Collection);
        }
    }

    private static SimpleDateFormat getDateFormat() {
        return DATE_FORMAT.get();
    }

    private static SimpleDateFormat getDateTimeFormat() {
        return DATE_TIME_FORMAT.get();
    }

    public static String escape(String s) {
        return s == null ? "\\N" : ESCAPER.escape(s);
    }

    public static String quoteIdentifier(String s) {
        if (s == null) {
            throw new IllegalArgumentException("Can't quote null as identifier");
        } else {
            StringBuilder sb = new StringBuilder(s.length() + 2);
            sb.append('`');
            sb.append(ESCAPER.escape(s));
            sb.append('`');
            return sb.toString();
        }
    }
}

定义预编译的参数:

public final class PreparedStatementParameter {
    private static final PreparedStatementParameter
            NULL_PARAM = new PreparedStatementParameter((String) null, false);
    private static final PreparedStatementParameter
            TRUE_PARAM = new PreparedStatementParameter("1", false);
    private static final PreparedStatementParameter
            FALSE_PARAM = new PreparedStatementParameter("0", false);
    private final String stringValue;
    private final boolean quoteNeeded;

    //判断是否转义
    public static PreparedStatementParameter fromObject(Object x) {
        return x == null ? NULL_PARAM : new PreparedStatementParameter(
                ValueFormatter.formatObject(x),
                ValueFormatter.needsQuoting(x));
    }

    public static PreparedStatementParameter nullParameter() {
        return NULL_PARAM;
    }

    public static PreparedStatementParameter boolParameter(boolean value) {
        return value ? TRUE_PARAM : FALSE_PARAM;
    }

    public PreparedStatementParameter(String stringValue, boolean quoteNeeded) {
        this.stringValue = stringValue == null ? "\\N" : stringValue;
        this.quoteNeeded = quoteNeeded;
    }

    //判断是否需要转义
    String getRegularValue() {
        return !"\\N".equals(this.stringValue) ? (this.quoteNeeded ? "'" + this.stringValue + "'" : this.stringValue)
                                               : "null";
    }

    String getBatchValue() {
        return this.stringValue;
    }

    public String toString() {
        return this.stringValue;
    }
}

预编译解析器:将参数替换到占位符

public class PreparedStatementParser {

    static final String PARAM_MARKER = "?";
    static final String NULL_MARKER = "\\N";

    private static final Pattern VALUES = Pattern.compile(
            "(?i)INSERT\\s+INTO\\s+.+VALUES\\s*\\(",
            Pattern.MULTILINE | Pattern.DOTALL);

    private List<List<String>> parameters;
    private List<String> parts;
    private boolean valuesMode;

    private PreparedStatementParser() {
        parameters = new ArrayList<>();
        parts = new ArrayList<>();
        valuesMode = false;
    }

    public static PreparedStatementParser parse(String sql) {
        return parse(sql, -1);
    }

    public static PreparedStatementParser parse(String sql, int valuesEndPosition) {
        if (StringUtils.isBlank(sql)) {
            throw new IllegalArgumentException("SQL may not be blank");
        }
        PreparedStatementParser parser = new PreparedStatementParser();
        parser.parseSQL(sql, valuesEndPosition);
        return parser;
    }

    List<List<String>> getParameters() {
        return Collections.unmodifiableList(parameters);
    }

    List<String> getParts() {
        return Collections.unmodifiableList(parts);
    }

    boolean isValuesMode() {
        return valuesMode;
    }

    private void reset() {
        parameters.clear();
        parts.clear();
        valuesMode = false;
    }

    private void parseSQL(String sql, int valuesEndPosition) {
        reset();
        List<String> currentParamList = new ArrayList<String>();
        boolean afterBackSlash = false;
        boolean inQuotes = false;
        boolean inBackQuotes = false;
        boolean inSingleLineComment = false;
        boolean inMultiLineComment = false;
        boolean whiteSpace = false;
        int endPosition = 0;
        if (valuesEndPosition > 0) {
            valuesMode = true;
            endPosition = valuesEndPosition;
        } else {
            Matcher matcher = VALUES.matcher(sql);
            if (matcher.find()) {
                valuesMode = true;
                endPosition = matcher.end() - 1;
            }
        }

        int currentParensLevel = 0;
        int quotedStart = 0;
        int partStart = 0;
        int sqlLength = sql.length();
        for (int i = valuesMode ? endPosition : 0, idxStart = i, idxEnd = i; i < sqlLength; i++) {
            char c = sql.charAt(i);
            if (inSingleLineComment) {
                if (c == '\n') {
                    inSingleLineComment = false;
                }
            } else if (inMultiLineComment) {
                if (c == '*' && sqlLength > i + 1 && sql.charAt(i + 1) == '/') {
                    inMultiLineComment = false;
                    i++;
                }
            } else if (afterBackSlash) {
                afterBackSlash = false;
            } else if (c == '\\') {
                afterBackSlash = true;
            } else if (c == '\'' && !inBackQuotes) {
                inQuotes = !inQuotes;
                if (inQuotes) {
                    quotedStart = i;
                } else if (!afterBackSlash) {
                    idxStart = quotedStart;
                    idxEnd = i + 1;
                }
            } else if (c == '`' && !inQuotes) {
                inBackQuotes = !inBackQuotes;
            } else if (!inQuotes && !inBackQuotes) {
                if (c == '?') {
                    if (currentParensLevel > 0) {
                        idxStart = i;
                        idxEnd = i + 1;
                    }
                    if (!valuesMode) {
                        parts.add(sql.substring(partStart, i));
                        partStart = i + 1;
                        currentParamList.add(PARAM_MARKER);
                    }
                } else if (c == '-' && sqlLength > i + 1 && sql.charAt(i + 1) == '-') {
                    inSingleLineComment = true;
                    i++;
                } else if (c == '/' && sqlLength > i + 1 && sql.charAt(i + 1) == '*') {
                    inMultiLineComment = true;
                    i++;
                } else if (c == ',') {
                    if (valuesMode && idxEnd > idxStart) {
                        currentParamList.add(typeTransformParameterValue(sql.substring(idxStart, idxEnd)));
                        parts.add(sql.substring(partStart, idxStart));
                        partStart = idxEnd;
                        idxEnd = i;
                        idxStart = idxEnd;
                    }
                    idxStart++;
                    idxEnd++;
                } else if (c == '(') {
                    currentParensLevel++;
                    idxStart++;
                    idxEnd++;
                } else if (c == ')') {
                    currentParensLevel--;
                    if (valuesMode && currentParensLevel == 0) {
                        if (idxEnd > idxStart) {
                            currentParamList.add(typeTransformParameterValue(sql.substring(idxStart, idxEnd)));
                            parts.add(sql.substring(partStart, idxStart));
                            partStart = idxEnd;
                            idxEnd = i;
                            idxStart = idxEnd;
                        }
                        if (!currentParamList.isEmpty()) {
                            parameters.add(currentParamList);
                            currentParamList = new ArrayList<>(currentParamList.size());
                        }
                    }
                } else if (Character.isWhitespace(c)) {
                    whiteSpace = true;
                } else if (currentParensLevel > 0) {
                    if (whiteSpace) {
                        idxStart = i;
                        idxEnd = i + 1;
                    } else {
                        idxEnd++;
                    }
                    whiteSpace = false;
                }
            }
        }
        if (!valuesMode && !currentParamList.isEmpty()) {
            parameters.add(currentParamList);
        }
        String lastPart = sql.substring(partStart, sqlLength);
        parts.add(lastPart);
    }

    private static String typeTransformParameterValue(String paramValue) {
        if (paramValue == null) {
            return null;
        }
        if (Boolean.TRUE.toString().equalsIgnoreCase(paramValue)) {
            return "1";
        }
        if (Boolean.FALSE.toString().equalsIgnoreCase(paramValue)) {
            return "0";
        }
        if ("NULL".equalsIgnoreCase(paramValue)) {
            return NULL_MARKER;
        }
        return paramValue;
    }

    public String buildSql(List<PreparedStatementParameter> binds) {
        if (this.parts.size() == 1) {
            return this.parts.get(0);
        } else {
            StringBuilder sb = new StringBuilder(this.parts.get(0));
            int i = 1;

            for (int t = 0; i < this.parts.size(); ++i) {
                String pValue = this.getParameter(i - 1);
                //占位符-#{}会进行转义
                if ("?".equals(pValue)) {
                    sb.append(binds.get(t++).getRegularValue());
                } else {
                    sb.append(pValue);
                }
                sb.append(this.parts.get(i));
            }
            return sb.toString();
        }
    }

    private String getParameter(int paramIndex) {
        int i = 0;
        for (int count = paramIndex; i < this.parameters.size(); ++i) {
            List<String> pList = this.parameters.get(i);
            count = count - pList.size();
            if (count < 0) {
                return pList.get(pList.size() + count);
            }
        }
        return null;
    }
}

文章参考

Mybatis interceptor 获取clickhouse最终执行的sql

【Mybatis】单独使用mybatis的SQL模板解析

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 193,812评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,626评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,144评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,052评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,925评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,035评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,461评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,150评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,413评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,501评论 2 307
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,277评论 1 325
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,159评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,528评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,868评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,143评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,407评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,615评论 2 335

推荐阅读更多精彩内容