飞道的博客

查询引擎: SQL反解析(json2sql)(附源码)

566人阅读  评论(0)

目录

1.前言

2.主要设计

1.请求体

2.构建Table

3.使用

4.自定义过滤条件

5.自定义聚合指标


1.前言

1.业务人员不懂SQL,通过UI自助查询关系型数据库;支持单表过滤、分组、排序、聚合指标、limit;

2.过滤条件包含:between、like、=、!=、>、>=、<、<=等

3.聚合指标包含:count、max、min、sum等

 

2.主要设计

1.请求体

1.QueryRequest


  
  1. private List<RequestElementMeasure> measures;
  2. private String table;
  3. private RequestElementFilter filter;
  4. private List<String> groupByList;
  5. private List<Integer> orderByList;
  6. private Long limit;

2.RequestElementMeasure


  
  1. private String field;
  2. private String aggregator;
  3. private List<Object> params;

3.RequestElementFilter


  
  1. private String relation;
  2. private List<RequestElementCondition> conditions;
  3. private List<RequestElementFilter> filters;

 

2.构建Table


  
  1. protected String tableName;
  2. protected List<AbstractColumn> columns = new ArrayList<>();
  3. private AbstractFilter filter;
  4. private List<String> groupByList;
  5. private List<Integer> orderByList;
  6. protected Limit limit;

 

3.使用


  
  1. String requestJson = "{\"limit\":10,\"orderByList\":[1],\"groupByList\":[\"name\"],\"filter\":{\"and\":true,\"conditions\":[{\"field\":\"age\",\"function\":\"equal\",\"params\":[20]}]},\"measures\":[{\"aggregator\":\"count\",\"field\":\"age\"}],\"table\":\"person\"}";
  2. QueryRequest request = JSONObject.parseObject(requestJson, QueryRequest.class);
  3. AliasGenerator aliasGenerator = new AliasGenerator();
  4. Table table = new Table(request.getTable());
  5. for (String groupBy : request.getGroupByList()) {
  6. table.addSelect( new AtomColumn(groupBy, aliasGenerator.nextAlias()));
  7. }
  8. request.getMeasures().forEach(measure -> {
  9. ExpressionColumn expressionColumn = TableUtil.constructAggregation(measure, aliasGenerator.nextAlias());
  10. table.addSelect(expressionColumn);
  11. });
  12. RequestElementFilter filter = request.getFilter();
  13. CompoundFilter compoundFilter = new CompoundFilter();
  14. compoundFilter.setIsAnd(filter.isAnd());
  15. for (RequestElementCondition condition : filter.getConditions()) {
  16. AbstractColumn column = new AtomColumn(condition.getField(), aliasGenerator.nextAlias());
  17. compoundFilter.addSubFilter(FilterUtil.getFunctionCondition(condition.getField(), condition.getFunction(), condition.getParams())
  18. .createFilter(column));
  19. }
  20. table.setFilter(compoundFilter);
  21. table.setGroupByList(request.getGroupByList());
  22. table.setOrderByList(request.getOrderByList());
  23. table.setLimit( new Limit(request.getLimit()));
  24. System.out.println( ">>>" + table.constructSql());

 

4.自定义过滤条件


  
  1. 1.定义FunctionCondition(参考GreaterCondition)
  2. 2.定义SimpleFilter(参考Greater)

5.自定义聚合指标


  
  1. 1.定义ExecutableExpression(参考Max)
  2. 2.添加AggregatorType
  3. 3.添加TableUtil

6.源码地址

https://github.com/ostarsier/queryengine

 

 

 


转载:https://blog.csdn.net/laughing_yang/article/details/111274660
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场