吹静静

欢迎QQ交流:592590682

public static void main(String[] args) {
        String SQL = "SELECT * FROM table";

        Statement statement = SQLParserUtil.getStatement(SQL);
        SelectBody selectBody = ((Select) statement).getSelectBody();
        Set<String> columns = new HashSet<>();
        getSQLAllColumns(selectBody, columns);

        for(String s : columns){
            System.out.println(s);
        }
    }




    /**
     * 获取SQL中的所有字段
     * @param selectBody
     * @param columns
     */
    public static void getSQLAllColumns(SelectBody selectBody, Set<String> columns){
        if(null != selectBody){
            // 1.提取Select中的字段
            List<SelectItem> selectItems = ((PlainSelect) selectBody).getSelectItems();
            if(null != selectItems && selectItems.size() > 0){
                getSelectColumns(selectItems, columns);
            }
            // 2.提取Join中的字段
            List<Join> joinsList = ((PlainSelect) selectBody).getJoins();
            if(null != joinsList && 0 != joinsList.size()){
                getJoinColumns(joinsList, columns);
            }
            // 3.提取Where中的字段
            Expression where = ((PlainSelect) selectBody).getWhere();
            if(null != where){
                getWhereColumns(where, columns);
            }
        }
    }

    /**
     * 获取Select下所有字段
     * @param selectItems
     * @param columns
     */
    public static void getSelectColumns(List<SelectItem> selectItems, Set<String> columns){
        for (SelectItem selectItem : selectItems){
            Expression expression = ((SelectExpressionItem) selectItem).getExpression();
            getExpressionColumns(expression, columns);
        }
    }

    /**
     * 获取JOIN下所有字段
     * @param joinsList
     * @param columns
     */
    public static void getJoinColumns(List<Join> joinsList, Set<String> columns){
        if(0 != joinsList.size()){
            for(Join join : joinsList){
                Collection<Expression> onExpressions = join.getOnExpressions();
                for (Expression onExpression : onExpressions){
                    getEqualsExpressionColumns(onExpression, columns);
                }
                FromItem rightItem = join.getRightItem(); // join 里面是子查询
                if(rightItem instanceof SubSelect){
                    List<SelectBody> joinSubSelects = SQLParserUtil.getJoinSubSelects(join);
                    for (SelectBody subSelectBody : joinSubSelects) {
                        getSQLAllColumns(subSelectBody, columns);
                    }
                }
            }
        }
    }

    /**
     * 获取Where下所有字段
     * @param expression
     * @param columns
     */
    public static void getWhereColumns(Expression expression, Set<String> columns){
        if(expression instanceof AndExpression){ // 判断where后面的条件是否包含And
            Expression leftExpression = ((AndExpression) expression).getLeftExpression();   // And左侧表达式,一大堆
            Expression rightExpression = ((AndExpression) expression).getRightExpression(); // And右侧表达式,只有一个
            getExpressionColumns(rightExpression, columns);  // 判断右侧表达
            if(leftExpression instanceof AndExpression){ // 遍历左侧一大堆表达式
                getWhereColumns(leftExpression, columns);
            }
        } else { // 判断where后面的条件是否只有一个条件
            getExpressionColumns(expression, columns);
        }
    }

    /**
     * 解析SQL中的表达式
     * @param expression
     * @param columns
     */
    public static void getExpressionColumns(Expression expression, Set<String> columns){
        if(expression instanceof EqualsTo){ // 判断表达式是否为 =
            getEqualsExpressionColumns(expression, columns);
        } else if(expression instanceof GreaterThanEquals){ // 判断表达式是否为 >=
            getGreaterThanEqualsExpressionColumns(expression, columns);
        } else if(expression instanceof MinorThan){ // 判断表达式是否为 <
            getMinorThanExpressionColumns(expression, columns);
        }  else if(expression instanceof NotEqualsTo){ // 判断表达式是否为 <>
            getNotEqualsToExpressionColumns(expression, columns);
        } else if(expression instanceof GeometryDistance){ // 判断表达式是否为 <->
            getGeometryDistanceExpressionColumns(expression, columns);
        } else if(expression instanceof GreaterThan){ // 判断表达式是否为 >
            getGreaterThanExpressionColumns(expression, columns);
        } else if(expression instanceof MinorThanEquals){ // 判断表达式是否为 <=
            getMinorThanEqualsExpressionColumns(expression, columns);
        } else if(expression instanceof ExistsExpression){  // 判断表达式是否为 Exists
            getExsitsExpressionColumns(expression, columns);
        } else if(expression instanceof InExpression){  // 判断表达式是否为 In
            getInExpressionColumns(expression, columns);
        } else if(expression instanceof Subtraction){  // 判断表达式是否为 -
            getSubtractionExpressionColumns(expression, columns);
        } else if(expression instanceof Addition){  // 判断表达式是否为 +
            getAdditionExpressionColumns(expression, columns);
        } else if(expression instanceof Function){  // 判断表达式是否为 函数
            getFunctionExpressionColumns(expression, columns);
        } else if(expression instanceof BitwiseAnd){  // 判断表达式是否为 +
            getBitwiseAndExpressionColumns(expression, columns);
        } else if(expression instanceof BitwiseLeftShift){  // 判断表达式是否为 <<
            getBitwiseLeftShiftExpressionColumns(expression, columns);
        } else if(expression instanceof BitwiseOr){  // 判断表达式是否为 |
            getBitwiseOrExpressionColumns(expression, columns);
        } else if(expression instanceof BitwiseRightShift){  // 判断表达式是否为 >>
            getBitwiseRightShiftExpressionColumns(expression, columns);
        } else if(expression instanceof BitwiseXor){  // 判断表达式是否为 ^
            getBitwiseRightShiftExpressionColumns(expression, columns);
        } else if(expression instanceof Concat){  // 判断表达式是否为 ||
            getConcatExpressionColumns(expression, columns);
        } else if(expression instanceof Division){  // 判断表达式是否为 /
            getDivisionExpressionColumns(expression, columns);
        } else if(expression instanceof Modulo){  // 判断表达式是否为 %
            getModuloExpressionColumns(expression, columns);
        } else if(expression instanceof Multiplication){  // 判断表达式是否为 *
            getMultiplicationExpressionColumns(expression, columns);
        } else if(expression instanceof Column){  // 判断表达式是否为 字段
            getColumns(expression, columns);
        }
    }

    /**
     * 解析 * 左右两侧
     * @param expression
     * @param columns
     */
    private static void getMultiplicationExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof Multiplication){
            Expression leftExpression = ((Multiplication) expression).getLeftExpression();
            Expression rightExpression = ((Multiplication) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析 % 左右两侧
     * @param expression
     * @param columns
     */
    private static void getModuloExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof Modulo){
            Expression leftExpression = ((Modulo) expression).getLeftExpression();
            Expression rightExpression = ((Modulo) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析 / 左右两侧
     * @param expression
     * @param columns
     */
    private static void getDivisionExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof Division){
            Expression leftExpression = ((Division) expression).getLeftExpression();
            Expression rightExpression = ((Division) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析 || 左右两侧
     * @param expression
     * @param columns
     */
    private static void getConcatExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof Concat){
            Expression leftExpression = ((Concat) expression).getLeftExpression();
            Expression rightExpression = ((Concat) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }


    /**
     * 解析 ^ 左右两侧
     * @param expression
     * @param columns
     */
    private static void getBitwiseXorExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof BitwiseXor){
            Expression leftExpression = ((BitwiseXor) expression).getLeftExpression();
            Expression rightExpression = ((BitwiseXor) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析 >> 左右两侧
     * @param expression
     * @param columns
     */
    private static void getBitwiseRightShiftExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof BitwiseRightShift){
            Expression leftExpression = ((BitwiseRightShift) expression).getLeftExpression();
            Expression rightExpression = ((BitwiseRightShift) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析 | 左右两侧
     * @param expression
     * @param columns
     */
    private static void getBitwiseOrExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof BitwiseOr){
            Expression leftExpression = ((BitwiseOr) expression).getLeftExpression();
            Expression rightExpression = ((BitwiseOr) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析 << 左右两侧
     * @param expression
     * @param columns
     */
    private static void getBitwiseLeftShiftExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof BitwiseLeftShift){
            Expression leftExpression = ((BitwiseLeftShift) expression).getLeftExpression();
            Expression rightExpression = ((BitwiseLeftShift) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析 & 左右两侧
     * @param expression
     * @param columns
     */
    private static void getBitwiseAndExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof BitwiseAnd){
            Expression leftExpression = ((BitwiseAnd) expression).getLeftExpression();
            Expression rightExpression = ((BitwiseAnd) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析 - 左右两侧
     * @param expression
     * @param columns
     */
    private static void getAdditionExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof Addition){
            Expression leftExpression = ((Addition) expression).getLeftExpression();
            Expression rightExpression = ((Addition) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析函数内字段
     * @param expression
     * @param columns
     */
    private static void getFunctionExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof Function){
            ExpressionList expressionList = ((Function) expression).getParameters();
            List<Expression> expressions = expressionList.getExpressions();
            for(Expression colExpression : expressions){
                getExpressionColumns(colExpression, columns);
            }
        }
    }

    /**
     * 解析 - 左右两侧
     * @param expression
     * @param columns
     */
    private static void getSubtractionExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof Subtraction){
            Expression leftExpression = ((Subtraction) expression).getLeftExpression();
            Expression rightExpression = ((Subtraction) expression).getRightExpression();
            getExpressionColumns(rightExpression, columns);
            getExpressionColumns(leftExpression, columns);
        }
    }

    /**
     * 解析 <= 左右两侧
     * @param expression
     * @param columns
     */
    private static void getMinorThanEqualsExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof MinorThanEquals){
            Expression leftExpression = ((MinorThanEquals) expression).getLeftExpression();
            Expression rightExpression = ((MinorThanEquals) expression).getRightExpression();
            getExpressionColumns(leftExpression, columns);
            getExpressionColumns(rightExpression, columns);
        }
    }

    /**
     * 解析 <-> 左右两侧
     * @param expression
     * @param columns
     */
    private static void getGeometryDistanceExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof GeometryDistance){
            Expression leftExpression = ((GeometryDistance) expression).getLeftExpression();
            Expression rightExpression = ((GeometryDistance) expression).getRightExpression();
            getExpressionColumns(leftExpression, columns);
            getExpressionColumns(rightExpression, columns);
        }
    }

    /**
     * 解析 > 左右两侧
     * @param expression
     * @param columns
     */
    private static void getGreaterThanExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof GreaterThan){
            Expression leftExpression = ((GreaterThan) expression).getLeftExpression();
            Expression rightExpression = ((GreaterThan) expression).getRightExpression();
            getExpressionColumns(leftExpression, columns);
            getExpressionColumns(rightExpression, columns);
        }
    }

    /**
     * 解析 <> 左右两侧
     * @param expression
     * @param columns
     */
    private static void getNotEqualsToExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof NotEqualsTo){
            Expression leftExpression = ((NotEqualsTo) expression).getLeftExpression();
            Expression rightExpression = ((NotEqualsTo) expression).getRightExpression();
            getExpressionColumns(leftExpression, columns);
            getExpressionColumns(rightExpression, columns);
        }
    }

    /**
     * 解析 < 左右两侧
     * @param expression
     * @param columns
     */
    private static void getMinorThanExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof MinorThan){
            Expression leftExpression = ((MinorThan) expression).getLeftExpression();
            Expression rightExpression = ((MinorThan) expression).getRightExpression();
            getExpressionColumns(leftExpression, columns);
            getExpressionColumns(rightExpression, columns);
        }
    }

    /**
     * 解析 >= 左右两侧
     * @param expression
     * @param columns
     */
    private static void getGreaterThanEqualsExpressionColumns(Expression expression, Set<String> columns) {
        if(expression instanceof GreaterThanEquals){
            Expression leftExpression = ((GreaterThanEquals) expression).getLeftExpression();
            Expression rightExpression = ((GreaterThanEquals) expression).getRightExpression();
            getExpressionColumns(leftExpression, columns);
            getExpressionColumns(rightExpression, columns);
        }
    }

    /**
     * 解析 In 左右两侧
     * @param expression
     * @param columns
     */
    public static void getInExpressionColumns(Expression expression, Set<String> columns){
        if(expression instanceof InExpression) {
            Expression InLeftExpression = ((InExpression) expression).getLeftExpression();
            Expression InRightExpression = ((InExpression) expression).getRightExpression();
            if (InLeftExpression instanceof Column) {  // 解析In左侧的字段
                String columnName = ((Column) InLeftExpression).getColumnName();
                columns.add(columnName);
            }
            if (InRightExpression instanceof SubSelect) {  // 判断In 中是否为子查询
                SelectBody selectBody = ((SubSelect) InRightExpression).getSelectBody();
                getSQLAllColumns(selectBody, columns);
            }
        }
    }

    /**
     * 解析 Exsits 左右两侧
     * @param expression
     * @param columns
     */
    public static void getExsitsExpressionColumns(Expression expression, Set<String> columns){
        if(expression instanceof ExistsExpression) {
            Expression ExsitsRightExpression = ((ExistsExpression) expression).getRightExpression();
            if (ExsitsRightExpression instanceof SubSelect) {
                SelectBody selectBody = ((SubSelect) ExsitsRightExpression).getSelectBody();
                getSQLAllColumns(selectBody, columns);
            }
        }
    }

    /**
     * 解析 = 左右两侧
     * @param expression
     * @param columns
     */
    public static void getEqualsExpressionColumns(Expression expression, Set<String> columns){
        if(expression instanceof EqualsTo){
            Expression leftExpression = ((EqualsTo) expression).getLeftExpression();
            Expression rightExpression = ((EqualsTo) expression).getRightExpression();
            getExpressionColumns(leftExpression, columns);
            getExpressionColumns(rightExpression, columns);
        }
    }

    /**
     * 解析字段
     * @param expression
     * @param columns
     */
    public static void getColumns(Expression expression, Set<String> columns){
        if(expression instanceof Column){ // 将字段取出
            String columnName = ((Column) expression).getColumnName();
            columns.add(columnName);
        }
    }

     * 获取Join中的子查询
     * @return
     */
    public static List<SelectBody> getJoinSubSelects(Join join){
        FromItem rightItem = join.getRightItem();
        List<SelectBody> subSelects = null;
        if(rightItem instanceof SubSelect){
            SelectBody subSelectBody = ((SubSelect) rightItem).getSelectBody();
            if(subSelectBody instanceof SetOperationList){
                subSelects = ((SetOperationList) subSelectBody).getSelects();
            } else {
                subSelects = new ArrayList<>();
                subSelects.add(subSelectBody);
            }
        }
        return subSelects;
    }
 

 

posted on 2022-11-07 18:42  吹静静  阅读(487)  评论(0编辑  收藏  举报