com.jquicker.persistent.rdb.model.DynamicSQL Maven / Gradle / Ivy
package com.jquicker.persistent.rdb.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.jquicker.commons.util.JsonUtils;
import com.jquicker.persistent.rdb.sqlfile.SQLParser;
public class DynamicSQL {
public static final String INSERT = "INSERT";
public static final String DELETE = "DELETE";
public static final String UPDATE = "UPDATE";
public static final String SELECT = "SELECT";
private String sqlId;
private String sqlStatement;
private String action; // insert、update、delete、select
private String paramType; // Map、Entity、String、int、long、、、
private Class> returnType; // Map、Entity
private List expressions;
private List relations = new ArrayList(); // 关联查询
/**
* 获取最终的sql语句
*
* @return
* @author OL
*/
public String getFinalSql(Map params) {
return null;
}
public void addRelation(Relation relation){
relations.add(relation);
}
public DynamicSQL(String sqlId) {
this.sqlId = sqlId;
}
public String getSqlId() {
return sqlId;
}
public void setSqlId(String sqlId) {
this.sqlId = sqlId;
}
public String getSqlStatement() {
return sqlStatement;
}
public void setSqlStatement(String sqlStatement) {
this.sqlStatement = sqlStatement;
}
public String getAction() {
if (action == null) {
action = sqlStatement.substring(0, 6).toUpperCase();
}
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getParamType() {
return paramType;
}
public void setParamType(String paramType) {
this.paramType = paramType;
}
public Class> getReturnType() {
return returnType;
}
public void setReturnType(Class> returnType) {
this.returnType = returnType;
}
public List getExpressions() {
if (expressions == null) {
expressions = SQLParser.findExpressions(sqlStatement);
for (int i = 0; i < expressions.size(); i++) {
SQLExpression expression = expressions.get(i);
sqlStatement = sqlStatement.replace(expression.getContent(), "expression_" + i);
}
}
return expressions;
}
public void setExpressions(List expressions) {
this.expressions = expressions;
}
public List getRelations() {
return relations;
}
@Override
public String toString() {
return JsonUtils.toJsonString(this);
}
public static void main(String[] args) {
/*String sql = "UPDATE test SET name = #{name} WHERE id = #{id} AND code = ${code}";
String regex = "([#|$]\\{[\\S\\s]+?\\})";
List params = RegexUtils.matcher(sql, regex);
System.out.println(params);
System.out.println();
String update = "UPDATE test SET id = id #if(name) , name = #{name} ## #if(remark != null) , remark = #{remark} ## WHERE id = #{id}";
String regex1 = "#([if|case][\\S\\s]+?)##";
List expressions = RegexUtils.matcher(update, regex1);
System.out.println(expressions);*/
String select = "SELECT * FROM test WHERE 1 = 1 #if(name){ AND name = #(name) } #if(code != null){ AND code = #(code) } "
+ "#if(state == 1){ AND 1 = 1 } #else if(state == 2){ AND 2 = 2 } #else if(state == 3){ AND 3 = 3 } #else { AND 1 = 2 }";
String regex2 = "#if\\s*\\(([\\S\\s]+?)\\)\\s*\\{([\\S\\s]+?)\\}\\s*"
+ "(else\\s*if\\s*\\(([\\S\\s]+?)\\)\\s*\\{([\\S\\s]+?)\\}\\s*)*"
+ "(else\\s*\\{([\\S\\s]+?)\\}\\s*)?"; // (?:pattern)匹配pattern但不获取匹配结果
// System.out.println(RegexUtils.matcher(select, regex2));
regex2 = "#(if|else\\s*if|else|for)\\s*(?:\\(([\\S\\s]+?)\\))?\\s*\\{([\\S\\s]+?)\\}";
Pattern pattern = Pattern.compile(regex2);
Matcher matcher = pattern.matcher(select);
while (matcher.find()) {
int count = matcher.groupCount();
for (int i = 0; i <= count; i++) {
System.out.print(matcher.group(i) + ",\t");
}
System.out.println();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy