| | |
| | | import com.ycl.scheme.entity.BlockCode; |
| | | import com.ycl.scheme.entity.Keyword; |
| | | import com.ycl.scheme.entity.KeywordType; |
| | | import com.ycl.scheme.service.SchemeService; |
| | | import com.ycl.scheme.entity.TriggerType; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | |
| | | public class SqlQueryBuilder extends AbstractBlockCodeBuilder { |
| | | |
| | | |
| | | private static final String[] splits = {" ", "/", "\\", "(", ")", System.lineSeparator(), ":", ";", ",", "."}; |
| | | private BlockText blockText; |
| | |
| | | |
| | | private Integer idx ; |
| | | |
| | | |
| | | private List<Parameter> pars=new ArrayList<>(); |
| | | private TriggerType triggerType; |
| | | |
| | | |
| | | public SqlQueryBuilder(BlockText blockText, List<Keyword> keywords, Integer idx) { |
| | | private Map<String,Parameter> parameters = new HashMap<>(); |
| | | |
| | | |
| | | public SqlQueryBuilder(BlockText blockText, List<Keyword> keywords, Integer idx, TriggerType triggerType) { |
| | | this.blockText = blockText; |
| | | |
| | | this.keywords = keywords; |
| | | |
| | | this.idx= idx; |
| | | |
| | | this.triggerType = triggerType; |
| | | |
| | | } |
| | | |
| | |
| | | throw new RuntimeException("没有找到数据源:" + this.blockText.getSource().trim()); |
| | | } |
| | | |
| | | this.scanExpression(); |
| | | this.scanWhere(); |
| | | if(this.triggerType== TriggerType.CANAL){ |
| | | this.addWhereRowSql(); |
| | | } |
| | | this.scanResult(); |
| | | this.buildSql(); |
| | | BlockCode bc = this.buildCode(); |
| | | return bc; |
| | | } |
| | | |
| | | private void scanExpression() { |
| | | private void scanWhere() { |
| | | String inputExpression = this.blockText.getExpression(); |
| | | int idx = 0; |
| | | |
| | |
| | | |
| | | } |
| | | |
| | | private void addWhereRowSql(){ |
| | | this.where += this.tableKeyword.getTableKey() + "= :rowId"; |
| | | } |
| | | |
| | | private boolean sqlTest(){ |
| | | //测试sql执行是否正确 |
| | | return true; |
| | |
| | | if(ss.length < 2){ |
| | | throw new RuntimeException("结果表达式不正确"); |
| | | } |
| | | |
| | | |
| | | |
| | | String fields = ss[0]; |
| | | String compareStr = resultText.substring(fields.length()); |
| | |
| | | res +=c; |
| | | } |
| | | idx++; |
| | | if(kw!=null && !kw.getKeywordType().equals(KeywordType.FUNCTION) && kw.getCode().equals("count") && !c.equals("(")){ |
| | | res+=c; |
| | | if(kw==null || !kw.getKeywordType().equals(KeywordType.FUNCTION) || kw.getFunctionParameterNullable()==null || !kw.getFunctionParameterNullable()){ |
| | | res +=c; |
| | | } |
| | | |
| | | skw=""; |
| | | }else{ |
| | | skw += c; |
| | |
| | | |
| | | private void buildSql(){ |
| | | this.sql = "select " + this.select + " from " + this.tableKeyword.getTable() + " where " + this.where; |
| | | |
| | | } |
| | | |
| | | private BlockCode buildCode(){ |
| | |
| | | StringBuilder csb = new StringBuilder(); |
| | | String sql = "\"" +this.sql + "\""; |
| | | csb.append(" def " + funcName + "(){\n"); |
| | | csb.append(" List<Object> pars = new ArrayList<>();\n"); |
| | | csb.append(" Map<String,Object> pars = super.getSqlParameters();\n"); |
| | | csb.append(" def sql= " +sql + ";\n"); |
| | | for(int i=0;i<this.pars.size();i++){ |
| | | String pname= "p_" + i; |
| | | |
| | | Parameter p = this.pars.get(i); |
| | | csb.append(" def " + pname + " = " + p.getScript()+";\n"); |
| | | csb.append(" pars.add("+pname + ");\n"); |
| | | this.parameters.forEach((k,v)->{ |
| | | String pname= "p_" + k; |
| | | csb.append(" def " + pname + " = " + v.getScript()+";\n"); |
| | | csb.append(" pars.put('" +v.getName() +"'," + pname +");\n"); |
| | | |
| | | } |
| | | }); |
| | | csb.append(" def rs = service.execQuery(sql,pars);\n"); |
| | | csb.append(" if(rs " + this.compareScript + "){\n"); |
| | | csb.append(" return true;\n"); |
| | |
| | | } |
| | | |
| | | throw new RuntimeException("读取字段名称时发生错误"); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | private MatchResult matchLike(String skw,Keyword lkw,int idx){ |
| | | String sub = blockText.getExpression().substring(idx); |
| | |
| | | |
| | | private MatchResult matchTimeRange(String skw, Keyword tkw,int idx){ |
| | | |
| | | |
| | | String sub = this.blockText.getExpression().substring(idx); |
| | | |
| | | String regex = tkw.getRegex(); |
| | |
| | | String d = vm.group(0); |
| | | day = Integer.valueOf(d); |
| | | |
| | | rs.setOutput(tableKeyword.getTableTimeField() + " between ? and ?"); |
| | | rs.setOutput(tableKeyword.getTableTimeField() + " between :beginTime and :endTime"); |
| | | rs.setInput(skw); |
| | | rs.setIdx(idx + e); |
| | | |
| | | //有几个问号,就要有几个对应的参数 |
| | | Parameter p1 = new Parameter(); |
| | | p1.setName("endTime"); |
| | | p1.setScript("LocalDateTime.now()"); |
| | | p1.setTypeName("LocalDateTime"); |
| | | Parameter p2 = new Parameter(); |
| | | p2.setName("beginTime"); |
| | | p2.setScript("LocalDateTime.now().minusDays("+day+ ")"); |
| | | |
| | | pars.add(p1); |
| | | pars.add(p2); |
| | | p2.setTypeName("LocalDateTime"); |
| | | parameters.put(p1.getName(),p1); |
| | | parameters.put(p2.getName(),p2); |
| | | |
| | | }else{ |
| | | throw new RuntimeException("没有在日期范围中找到指定天数"); |