【解析SQL模板-1】Mybatis的SQL模板組合成可運行的SQL

背景

實現(xiàn)平臺化的mybatis能力蹄胰,即在頁面上輸入mybatis的SQL模板岳遥,并傳入?yún)?shù),最終解析成可運行的SQL裕寨。

實現(xiàn)原理

引入依賴:

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

mybatis的SQL生成器:

  1. 解析mybatis模板浩蓉,生成預編譯的SQL;
  2. 解析預編譯SQL宾袜,參數(shù)替換捻艳?;
@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();
        }
    }
}

定義預編譯的參數(shù):

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;
    }
}

預編譯解析器:將參數(shù)替換到占位符

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最終執(zhí)行的sql

【Mybatis】單獨使用mybatis的SQL模板解析

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末嘁字,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子杉畜,更是在濱河造成了極大的恐慌纪蜒,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件此叠,死亡現(xiàn)場離奇詭異纯续,居然都是意外死亡,警方通過查閱死者的電腦和手機拌蜘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進店門杆烁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人简卧,你說我怎么就攤上這事兔魂。” “怎么了举娩?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵析校,是天一觀的道長。 經(jīng)常有香客問我铜涉,道長智玻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任芙代,我火速辦了婚禮吊奢,結果婚禮上,老公的妹妹穿的比我還像新娘纹烹。我一直安慰自己页滚,他們只是感情好召边,可當我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著裹驰,像睡著了一般隧熙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上幻林,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天贞盯,我揣著相機與錄音,去河邊找鬼沪饺。 笑死躏敢,一個胖子當著我的面吹牛,可吹牛的內容都是我干的随闽。 我是一名探鬼主播父丰,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼掘宪!你這毒婦竟也來了蛾扇?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤魏滚,失蹤者是張志新(化名)和其女友劉穎镀首,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鼠次,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡更哄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了腥寇。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片成翩。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖赦役,靈堂內的尸體忽然破棺而出麻敌,到底是詐尸還是另有隱情,我是刑警寧澤掂摔,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布术羔,位于F島的核電站,受9級特大地震影響乙漓,放射性物質發(fā)生泄漏级历。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一叭披、第九天 我趴在偏房一處隱蔽的房頂上張望寥殖。 院中可真熱鬧,春花似錦、人聲如沸扛禽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽编曼。三九已至,卻和暖如春剩辟,著一層夾襖步出監(jiān)牢的瞬間掐场,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工贩猎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留熊户,地道東北人。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓吭服,卻偏偏與公主長得像嚷堡,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子艇棕,可洞房花燭夜當晚...
    茶點故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內容