【jsqlpaser使用008】 过滤算子
2021/12/3 2:09:47
本文主要是介绍【jsqlpaser使用008】 过滤算子,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
过滤算子
public class FiltersNodeHandler extends BaseOperatorNodeHandler { public FiltersNodeHandler(DdlMutableGraphContext mutableGraph, DdlNode node) { super(mutableGraph, node); } @Override public PlainSelect sql() { //获取前置节点的sql PlainSelect preSql = prePlainSelect(); DdlNode.FilterNode filterNode = currentNode().getOperators().getFilterNode(); //最外层的条件 Boolean logic = filterNode.getLogic(); //先拼接内层的条件 List<DdlNode.Children> childrens = filterNode.getChildren(); if (!CollectionUtils.isEmpty(childrens)) { //拼接内层条件子节点 Expression expressionOut; for (DdlNode.Children children : childrens) { Expression where = preSql.getWhere(); //子节点的操作字段 Boolean logicChild = children.getLogic(); //遍历第一个子节点 List<DdlNode.Operand> operands = children.getOperand(); if (CollectionUtil.isNotEmpty(operands)) { //operand共用同一个子logicChild //不等于1并且为奇数的时候创建 int size = operands.size(); Expression innerExpression = null; for (int i = 0; i < size; i++) { //为偶数才创建 if ((i & 1) == 0) { if (Objects.nonNull(logicChild)) { if (logicChild) { AndExpressionExtend andExpression = new AndExpressionExtend(); if (innerExpression != null) { andExpression.setLeftExpression(innerExpression); } innerExpression = andExpression; } else { OrExpressionExtend orExpression = new OrExpressionExtend(); if (innerExpression != null) { orExpression.setLeftExpression(innerExpression); } innerExpression = orExpression; } } } String operator = operands.get(i).getOperator(); String name = operands.get(i).getName(); String value = operands.get(i).getValue(); String type = operands.get(i).getType(); Expression temp; Expression expressionRight; IsNullExpression isNullExpression; InExpression inExpression; Between between; ExistsExpression existsExpression; LikeExpression likeExpression; Expression startExpression; Expression endExpression; String[] split; switch (operator) { case "=": EqualsTo equalsTo = new EqualsTo(); equalsTo.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); equalsTo.setRightExpression(expressionRight); temp = equalsTo; break; case "!=": NotEqualsTo notEqualsTo = new NotEqualsTo(); notEqualsTo.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); notEqualsTo.setRightExpression(expressionRight); temp = notEqualsTo; break; case "in": inExpression = new InExpression(); inExpression.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); inExpression.setRightExpression(expressionRight); temp = inExpression; break; case "not in": inExpression = new InExpression(); inExpression.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); inExpression.setRightExpression(expressionRight); inExpression.setNot(true); temp = inExpression; break; case "is null": isNullExpression = new IsNullExpression(); isNullExpression.setLeftExpression(new Column(name)); temp = isNullExpression; break; case "is not null": isNullExpression = new IsNullExpression(); isNullExpression.setLeftExpression(new Column(name)); isNullExpression.setNot(true); temp = isNullExpression; break; case "like": likeExpression = new LikeExpression(); likeExpression.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); likeExpression.setRightExpression(expressionRight); temp = likeExpression; break; case "not like": likeExpression = new LikeExpression(); likeExpression.setNot(true); likeExpression.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); likeExpression.setRightExpression(expressionRight); temp = likeExpression; break; case ">": GreaterThan greaterThan = new GreaterThan(); greaterThan.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); greaterThan.setRightExpression(expressionRight); temp = greaterThan; break; case ">=": GreaterThanEquals greaterThanEquals = new GreaterThanEquals(); greaterThanEquals.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); greaterThanEquals.setRightExpression(expressionRight); temp = greaterThanEquals; break; case "<": MinorThan minorThan = new MinorThan(); minorThan.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); minorThan.setRightExpression(expressionRight); temp = minorThan; break; case "<=": MinorThanEquals minorThanEquals = new MinorThanEquals(); minorThanEquals.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); minorThanEquals.setRightExpression(expressionRight); temp = minorThanEquals; break; case "between": between = new Between(); between.setLeftExpression(new Column(name)); split = value.split(","); startExpression = this.getRightExpression(split[0], type); endExpression = this.getRightExpression(split[1], type); between.setBetweenExpressionStart(startExpression); between.setBetweenExpressionEnd(endExpression); temp = between; break; case "not between": between = new Between(); between.setLeftExpression(new Column(name)); split = value.split(","); startExpression = this.getRightExpression(split[0], type); endExpression = this.getRightExpression(split[1], type); between.setBetweenExpressionStart(startExpression); between.setBetweenExpressionEnd(endExpression); between.setNot(true); temp = between; break; case "exists": existsExpression = new ExistsExpression(); expressionRight = this.getRightExpression(value, type); existsExpression.setRightExpression(expressionRight); temp = existsExpression; break; case "not exists": existsExpression = new ExistsExpression(); expressionRight = this.getRightExpression(value, type); existsExpression.setRightExpression(expressionRight); existsExpression.setNot(true); temp = existsExpression; break; case "contains": Matches matches = new Matches(); matches.setLeftExpression(new Column(name)); expressionRight = this.getRightExpression(value, type); matches.setRightExpression(expressionRight); temp = matches; break; default: throw new DdlException("操作符不存在"); } if (Objects.nonNull(logicChild)) { if (logicChild) { AndExpressionExtend andExpression = (AndExpressionExtend) innerExpression; if (andExpression.getLeftExpression() == null) { andExpression.setLeftExpression(temp); } else { andExpression.setRightExpression(temp); } innerExpression = andExpression; } else { OrExpressionExtend orExpression = (OrExpressionExtend) innerExpression; if (orExpression.getLeftExpression() == null) { orExpression.setLeftExpression(temp); } else { orExpression.setRightExpression(temp); } orExpression.setLeftExpression(temp); innerExpression = orExpression; } } else { innerExpression = temp; } if (i == size - 1) { //拼接外层的条件 expressionOut = innerExpression; if (Objects.nonNull(where)) { if (!logic) { expressionOut = new OrExpressionExtend(where, expressionOut); } else { expressionOut = new AndExpressionExtend(where, expressionOut); } } preSql.setWhere(expressionOut); } } } } } log(preSql.toString()); return preSql; } /** * 获取右端表达式 */ private Expression getRightExpression(String value, String type) { //默认是String类型 Expression expression = new StringValue(value); if (DataTypeEnum.STRING.toString().equals(type)) { expression = new StringValue(value); } if (DataTypeEnum.UINT_8.toString().equals(type) || DataTypeEnum.UINT32.toString().equals(type)) { expression = new LongValue(value); } if (DataTypeEnum.DATE.toString().equals(type)) { expression = new DateValue(value); } if (DataTypeEnum.TIME.toString().equals(type)) { expression = new TimeValue(value); } if (DataTypeEnum.DOUBLE.toString().equals(type)) { expression = new DoubleValue(value); } if (DataTypeEnum.HEX.toString().equals(type)) { expression = new HexValue(value); } if (DataTypeEnum.NULL.toString().equals(type)) { expression = new NullValue(); } return expression; } @Override public List<DdlColumn> columns() { super.checkNode(); return super.preColumns(); } }
这篇关于【jsqlpaser使用008】 过滤算子的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16Vue3资料:新手入门必读教程
- 2024-11-16Vue3资料:新手入门全面指南
- 2024-11-16Vue资料:新手入门完全指南
- 2024-11-16Vue项目实战:新手入门指南
- 2024-11-16React Hooks之useEffect案例详解
- 2024-11-16useRef案例详解:React中的useRef使用教程
- 2024-11-16React Hooks之useState案例详解
- 2024-11-16Vue入门指南:从零开始搭建第一个Vue项目
- 2024-11-16Vue3学习:新手入门教程与实践指南
- 2024-11-16Vue3学习:从入门到初级实战教程