cn.sylinx.hbatis.ext.parse.GenericTokenParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hbatis-core Show documentation
Show all versions of hbatis-core Show documentation
hbatis is a simple orm framework
package cn.sylinx.hbatis.ext.parse;
import java.util.ArrayList;
import java.util.List;
import cn.sylinx.hbatis.exception.HbatisException;
import cn.sylinx.hbatis.ext.function.FunctionParser;
import cn.sylinx.hbatis.ext.ifblock.IfEndBlockParser;
import cn.sylinx.hbatis.ext.ifblock.IfEndBlockParserImpl;
import cn.sylinx.hbatis.ext.ifblock.IfEndMatcher;
import cn.sylinx.hbatis.ext.ifblock.IfEndMatcherImpl;
import cn.sylinx.hbatis.kit.Tuple;
import cn.sylinx.hbatis.log.GLog;
/**
* 参数占位符
*
* @author han
*
*/
public class GenericTokenParser {
// 静态参数解析
private final static String TOKEN_BEGIN = "#{";
private final static String TOKEN_END = "}";
// 外部sql解析
private final static String TOKEN_DY_BEGIN = "${";
private final static String TOKEN_DY_END = "}";
// 判断符号
private final static String TOKEN_IF_BEGIN = "#IF";
private TokenHandler handler;
public GenericTokenParser(TokenHandler handler) {
this.handler = handler;
}
/**
* format
*
* @param preSt
* @return
*/
protected String formatPreStatement(String preSt) {
return preSt.replaceAll("\r", " ").replaceAll("\n", " ").replaceAll("\t", " ");
}
public Tuple parse(String preSt, boolean format) {
// 1、sql排版
String preSt1 = format ? formatPreStatement(preSt) : preSt;
// 2、解析出动态sql
String preSt2 = parseIfDynamicSql(preSt1);
// 3、解析外部加入动态sql
String preSt3 = parseDynamicPartSql(preSt2);
// 4、解析函数
String st = parseFunctions(preSt3);
// 5、以下解析参数
return parseSqlAndParameters(st);
}
private Tuple parseSqlAndParameters(String st) {
List