Search in sources :

Example 1 with MySqlExtractExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr in project druid by alibaba.

the class MySqlExprParser method primaryRest.

public final SQLExpr primaryRest(SQLExpr expr) {
    if (expr == null) {
        throw new IllegalArgumentException("expr");
    }
    if (lexer.token() == Token.LITERAL_CHARS) {
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String ident = identExpr.getName();
            if (ident.equalsIgnoreCase("x")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                expr = new SQLHexExpr(charValue);
                return primaryRest(expr);
            } else if (ident.equalsIgnoreCase("b")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                expr = new SQLBinaryExpr(charValue);
                return primaryRest(expr);
            } else if (ident.startsWith("_")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                MySqlCharExpr mysqlCharExpr = new MySqlCharExpr(charValue);
                mysqlCharExpr.setCharset(identExpr.getName());
                if (identifierEquals("COLLATE")) {
                    lexer.nextToken();
                    String collate = lexer.stringVal();
                    mysqlCharExpr.setCollate(collate);
                    accept(Token.IDENTIFIER);
                }
                expr = mysqlCharExpr;
                return primaryRest(expr);
            }
        } else if (expr instanceof SQLCharExpr) {
            SQLMethodInvokeExpr concat = new SQLMethodInvokeExpr("CONCAT");
            concat.addParameter(expr);
            do {
                String chars = lexer.stringVal();
                concat.addParameter(new SQLCharExpr(chars));
                lexer.nextToken();
            } while (lexer.token() == Token.LITERAL_CHARS || lexer.token() == Token.LITERAL_ALIAS);
            expr = concat;
        }
    } else if (lexer.token() == Token.IDENTIFIER) {
        if (expr instanceof SQLHexExpr) {
            if ("USING".equalsIgnoreCase(lexer.stringVal())) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error, illegal hex");
                }
                String charSet = lexer.stringVal();
                lexer.nextToken();
                expr.getAttributes().put("USING", charSet);
                return primaryRest(expr);
            }
        } else if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("syntax error");
            }
            String collate = lexer.stringVal();
            lexer.nextToken();
            SQLBinaryOpExpr binaryExpr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.COLLATE, new SQLIdentifierExpr(collate), JdbcConstants.MYSQL);
            expr = binaryExpr;
            return primaryRest(expr);
        } else if (expr instanceof SQLVariantRefExpr) {
            if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error");
                }
                String collate = lexer.stringVal();
                lexer.nextToken();
                expr.putAttribute("COLLATE", collate);
                return primaryRest(expr);
            }
        } else if (expr instanceof SQLIntegerExpr) {
            SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
            String binaryString = lexer.stringVal();
            if (intExpr.getNumber().intValue() == 0 && binaryString.startsWith("b")) {
                lexer.nextToken();
                expr = new SQLBinaryExpr(binaryString.substring(1));
                return primaryRest(expr);
            }
        }
    }
    if (lexer.token() == Token.LPAREN && expr instanceof SQLIdentifierExpr) {
        SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
        String ident = identExpr.getName();
        if ("EXTRACT".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("syntax error");
            }
            String unitVal = lexer.stringVal();
            MySqlIntervalUnit unit = MySqlIntervalUnit.valueOf(unitVal.toUpperCase());
            lexer.nextToken();
            accept(Token.FROM);
            SQLExpr value = expr();
            MySqlExtractExpr extract = new MySqlExtractExpr();
            extract.setValue(value);
            extract.setUnit(unit);
            accept(Token.RPAREN);
            expr = extract;
            return primaryRest(expr);
        } else if ("SUBSTRING".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            for (; ; ) {
                SQLExpr param = expr();
                methodInvokeExpr.addParameter(param);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else if (lexer.token() == Token.FROM) {
                    lexer.nextToken();
                    SQLExpr from = expr();
                    methodInvokeExpr.addParameter(from);
                    if (lexer.token() == Token.FOR) {
                        lexer.nextToken();
                        SQLExpr forExpr = expr();
                        methodInvokeExpr.addParameter(forExpr);
                    }
                    break;
                } else if (lexer.token() == Token.RPAREN) {
                    break;
                } else {
                    throw new ParserException("syntax error");
                }
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("TRIM".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            if (lexer.token() == Token.IDENTIFIER) {
                String flagVal = lexer.stringVal();
                if ("LEADING".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.getAttributes().put("TRIM_TYPE", "LEADING");
                } else if ("BOTH".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.getAttributes().put("TRIM_TYPE", "BOTH");
                } else if ("TRAILING".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.putAttribute("TRIM_TYPE", "TRAILING");
                }
            }
            SQLExpr param = expr();
            methodInvokeExpr.addParameter(param);
            if (lexer.token() == Token.FROM) {
                lexer.nextToken();
                SQLExpr from = expr();
                methodInvokeExpr.putAttribute("FROM", from);
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("MATCH".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();
            if (lexer.token() == Token.RPAREN) {
                lexer.nextToken();
            } else {
                exprList(matchAgainstExpr.getColumns(), matchAgainstExpr);
                accept(Token.RPAREN);
            }
            acceptIdentifier("AGAINST");
            accept(Token.LPAREN);
            SQLExpr against = primary();
            matchAgainstExpr.setAgainst(against);
            if (lexer.token() == Token.IN) {
                lexer.nextToken();
                if (identifierEquals("NATURAL")) {
                    lexer.nextToken();
                    acceptIdentifier("LANGUAGE");
                    acceptIdentifier("MODE");
                    if (lexer.token() == Token.WITH) {
                        lexer.nextToken();
                        acceptIdentifier("QUERY");
                        acceptIdentifier("EXPANSION");
                        matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
                    } else {
                        matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE);
                    }
                } else if (identifierEquals("BOOLEAN")) {
                    lexer.nextToken();
                    acceptIdentifier("MODE");
                    matchAgainstExpr.setSearchModifier(SearchModifier.IN_BOOLEAN_MODE);
                } else {
                    throw new ParserException("TODO");
                }
            } else if (lexer.token() == Token.WITH) {
                throw new ParserException("TODO");
            }
            accept(Token.RPAREN);
            expr = matchAgainstExpr;
            return primaryRest(expr);
        } else if (("CONVERT".equalsIgnoreCase(ident)) || ("CHAR".equalsIgnoreCase(ident))) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            if (lexer.token() != Token.RPAREN) {
                exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
            }
            if (identifierEquals("USING")) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error");
                }
                String charset = lexer.stringVal();
                lexer.nextToken();
                methodInvokeExpr.putAttribute("USING", charset);
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("POSITION".equalsIgnoreCase(ident)) {
            accept(Token.LPAREN);
            SQLExpr subStr = this.primary();
            accept(Token.IN);
            SQLExpr str = this.expr();
            accept(Token.RPAREN);
            SQLMethodInvokeExpr locate = new SQLMethodInvokeExpr("LOCATE");
            locate.addParameter(subStr);
            locate.addParameter(str);
            expr = locate;
            return primaryRest(expr);
        }
    }
    if (lexer.token() == Token.VARIANT && "@".equals(lexer.stringVal())) {
        lexer.nextToken();
        MySqlUserName userName = new MySqlUserName();
        if (expr instanceof SQLCharExpr) {
            userName.setUserName(((SQLCharExpr) expr).toString());
        } else {
            userName.setUserName(((SQLIdentifierExpr) expr).getName());
        }
        if (lexer.token() == Token.LITERAL_CHARS) {
            userName.setHost("'" + lexer.stringVal() + "'");
        } else {
            userName.setHost(lexer.stringVal());
        }
        lexer.nextToken();
        return userName;
    }
    if (lexer.token() == Token.ERROR) {
        throw new ParserException("syntax error, token: " + lexer.token() + " " + lexer.stringVal() + ", pos : " + lexer.pos());
    }
    return super.primaryRest(expr);
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLBinaryExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryExpr) SQLHexExpr(com.alibaba.druid.sql.ast.expr.SQLHexExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) MySqlMatchAgainstExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MySqlIntervalUnit(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalUnit) MySqlCharExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) MySqlExtractExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr) MySqlUserName(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName)

Example 2 with MySqlExtractExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr in project dble by actiontech.

the class ItemExtract method toExpression.

@Override
public SQLExpr toExpression() {
    MySqlExtractExpr extract = new MySqlExtractExpr();
    extract.setValue(args.get(0).toExpression());
    extract.setUnit(intType);
    return extract;
}
Also used : MySqlExtractExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr)

Example 3 with MySqlExtractExpr

use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr in project dble by actiontech.

the class Testparser method test.

public void test(String sql) {
    System.out.println("-----------------------------------------------------------");
    System.out.println("-----------------------------------------------------------");
    System.out.println(sql);
    SQLStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement statement = parser.parseStatement();
    if (statement instanceof MySqlExplainStatement) {
        System.out.println("MySqlExplainStatement" + statement.toString());
    } else if (statement instanceof MySqlCreateTableStatement) {
        MySqlCreateTableStatement createStment = (MySqlCreateTableStatement) statement;
        SQLExpr expr = createStment.getTableSource().getExpr();
        if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
            System.out.println((propertyExpr.getOwner().toString()));
            System.out.println(propertyExpr.getName());
        } else if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) expr;
            System.out.println(identifierExpr.getName());
        } else {
            System.out.println(expr.getClass() + "\n");
        }
    } else if (statement instanceof SQLAlterTableStatement) {
        SQLAlterTableStatement alterStatement = (SQLAlterTableStatement) statement;
        SQLExprTableSource tableSource = alterStatement.getTableSource();
        for (SQLAlterTableItem alterItem : alterStatement.getItems()) {
            if (alterItem instanceof SQLAlterTableAddColumn) {
                SQLAlterTableAddColumn addColumn = (SQLAlterTableAddColumn) alterItem;
                boolean isFirst = addColumn.isFirst();
                SQLName afterColumn = addColumn.getAfterColumn();
                if (afterColumn != null) {
                    System.out.println(sql + ": afterColumns:\n" + afterColumn.getClass().toString() + "\n");
                }
                for (SQLColumnDefinition columnDef : addColumn.getColumns()) {
                }
            } else if (alterItem instanceof SQLAlterTableAddIndex) {
                SQLAlterTableAddIndex addIndex = (SQLAlterTableAddIndex) alterItem;
                SQLName name = addIndex.getName();
                System.out.println(sql + ":indexname:\n" + name.getClass().toString() + "\n");
                String type = addIndex.getType();
                // ??
                addIndex.isUnique();
                for (SQLSelectOrderByItem item : addIndex.getItems()) {
                    System.out.println(sql + ": item.getExpr():\n" + item.getExpr().getClass().toString() + "\n");
                }
            } else if (alterItem instanceof SQLAlterTableAddConstraint) {
                SQLAlterTableAddConstraint addConstraint = (SQLAlterTableAddConstraint) alterItem;
                SQLConstraint constraint = addConstraint.getConstraint();
                if (constraint instanceof MySqlUnique) {
                    MySqlUnique unique = (MySqlUnique) constraint;
                    unique.getName();
                } else if (constraint instanceof MySqlPrimaryKey) {
                } else if (constraint instanceof MysqlForeignKey) {
                    System.out.println("NOT SUPPORT\n");
                }
                System.out.println(sql + ": constraint:\n" + constraint.getClass().toString() + "\n");
            } else if (alterItem instanceof SQLAlterTableDropIndex) {
                SQLAlterTableDropIndex dropIndex = (SQLAlterTableDropIndex) alterItem;
                SQLIdentifierExpr indexName = (SQLIdentifierExpr) dropIndex.getIndexName();
                String strIndexName = indexName.getName();
            } else if (alterItem instanceof SQLAlterTableDropKey) {
                SQLAlterTableDropKey dropIndex = (SQLAlterTableDropKey) alterItem;
                SQLIdentifierExpr indexName = (SQLIdentifierExpr) dropIndex.getKeyName();
                String strIndexName = indexName.getName();
            } else if (alterItem instanceof MySqlAlterTableChangeColumn) {
                MySqlAlterTableChangeColumn changeColumn = (MySqlAlterTableChangeColumn) alterItem;
                boolean isFirst = changeColumn.isFirst();
                SQLIdentifierExpr afterColumn = (SQLIdentifierExpr) changeColumn.getAfterColumn();
                // SQLExpr afterColumn = changeColumn.getAfterColumn();
                if (afterColumn != null) {
                    String strAfterColumn = afterColumn.getName();
                }
                SQLColumnDefinition columnDef = changeColumn.getNewColumnDefinition();
            } else if (alterItem instanceof MySqlAlterTableModifyColumn) {
                MySqlAlterTableModifyColumn modifyColumn = (MySqlAlterTableModifyColumn) alterItem;
                boolean isFirst = modifyColumn.isFirst();
                SQLExpr afterColumn = modifyColumn.getAfterColumn();
                if (afterColumn != null) {
                    System.out.println(sql + ": afterColumns:\n" + afterColumn.getClass().toString() + "\n");
                }
                SQLColumnDefinition columnDef = modifyColumn.getNewColumnDefinition();
            } else if (alterItem instanceof SQLAlterTableDropColumnItem) {
                SQLAlterTableDropColumnItem dropColumn = (SQLAlterTableDropColumnItem) alterItem;
                for (SQLName dropName : dropColumn.getColumns()) {
                    System.out.println(sql + ":dropName:\n" + dropName.getClass().toString() + "\n");
                }
            } else if (alterItem instanceof SQLAlterTableDropPrimaryKey) {
                SQLAlterTableDropPrimaryKey dropPrimary = (SQLAlterTableDropPrimaryKey) alterItem;
            } else {
                System.out.println(sql + ":\n" + alterItem.getClass().toString() + "\n");
            }
            System.out.println("\n" + statement.toString());
        }
    } else if (statement instanceof SQLDropTableStatement) {
    } else if (statement instanceof SQLTruncateStatement) {
    // TODO:Sequence?
    } else if (statement instanceof SQLDropIndexStatement) {
    // TODO
    } else if (statement instanceof MySqlDeleteStatement) {
        MySqlDeleteStatement deleteStatement = (MySqlDeleteStatement) statement;
        SQLTableSource tableSource = deleteStatement.getTableSource();
        System.out.println(sql + ":getTableSource:" + tableSource.getClass().toString() + "\n");
        if (deleteStatement.getFrom() != null) {
            System.out.println(sql + ":getSchema:" + deleteStatement.getFrom().getClass().toString() + "\n");
        }
        System.out.println("\n");
    } else if (statement instanceof MySqlUpdateStatement) {
        MySqlUpdateStatement updateStatement = (MySqlUpdateStatement) statement;
        SQLTableSource tableSource = updateStatement.getTableSource();
        System.out.println(sql + ":getTableSource:" + tableSource.getClass().toString() + "\n");
        System.out.println("\n" + statement.toString());
    } else if (statement instanceof SQLCreateIndexStatement) {
        SQLCreateIndexStatement stament = (SQLCreateIndexStatement) statement;
        SQLTableSource tableSource = stament.getTable();
        System.out.println(sql + ":getTableSource:" + tableSource.getClass().toString() + "\n");
        System.out.println(sql + stament.getType());
    } else if (statement instanceof SQLSelectStatement) {
        SQLSelectStatement stament = (SQLSelectStatement) statement;
        SQLSelectQuery sqlSelectQuery = stament.getSelect().getQuery();
        if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
            MySqlSelectQueryBlock selectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
            SQLTableSource fromSource = selectQueryBlock.getFrom();
            if (fromSource instanceof SQLJoinTableSource) {
                SQLJoinTableSource fromJoinSource = (SQLJoinTableSource) fromSource;
                System.out.println("SQLJoinTableSource:");
                System.out.println("all:" + fromJoinSource.toString());
                System.out.println("left:" + fromJoinSource.getLeft().toString() + ",class" + fromJoinSource.getLeft().getClass());
                System.out.println("right:" + fromJoinSource.getRight().toString() + ",class" + fromJoinSource.getRight().getClass());
                System.out.println("---------------------------");
            }
            for (SQLSelectItem item : selectQueryBlock.getSelectList()) {
                if (item.getExpr() != null) {
                    SQLExpr func = item.getExpr();
                    if (func instanceof SQLAggregateExpr) {
                        System.out.println("SQLAggregateExpr:");
                        SQLAggregateExpr agg = (SQLAggregateExpr) func;
                        System.out.println("MethodName:" + agg.getMethodName() + ",getArguments size =" + agg.getArguments().size() + ",Option:" + agg.getOption());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLMethodInvokeExpr) {
                        System.out.println("SQLMethodInvokeExpr:");
                        SQLMethodInvokeExpr method = (SQLMethodInvokeExpr) func;
                        System.out.println("MethodName:" + method.getMethodName() + ",getArguments size =" + method.getParameters().size() + ",OWNER:" + method.getOwner());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLCastExpr) {
                        SQLCastExpr cast = (SQLCastExpr) func;
                        System.out.println("SQLCastExpr:");
                        System.out.println("Expr:" + cast.getExpr().getClass() + ",getDataType:" + cast.getDataType());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLBinaryOpExpr) {
                        SQLBinaryOpExpr Op = (SQLBinaryOpExpr) func;
                        System.out.println("SQLBinaryOpExpr:");
                        System.out.println("left:" + Op.getLeft().getClass());
                        System.out.println("right:" + Op.getRight().getClass());
                        System.out.println("operator:" + Op.getOperator().getClass());
                        System.out.println("dbtype:" + Op.getDbType());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLUnaryExpr) {
                        SQLUnaryExpr Op = (SQLUnaryExpr) func;
                        System.out.println("SQLUnaryExpr:");
                        System.out.println("EXPR:" + Op.getExpr().getClass());
                        System.out.println("operator:" + Op.getOperator().getClass());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLBetweenExpr) {
                        SQLBetweenExpr between = (SQLBetweenExpr) func;
                        System.out.println("SQLBetweenExpr:");
                        System.out.println("begin EXPR:" + between.getBeginExpr());
                        System.out.println("end EXPR:" + between.getEndExpr());
                        System.out.println("isnot :" + between.isNot());
                        System.out.println("test :" + between.getTestExpr());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLInListExpr) {
                        SQLInListExpr in = (SQLInListExpr) func;
                        System.out.println("SQLInListExpr:");
                        System.out.println("EXPR:" + in.getExpr());
                        System.out.println("isnot :" + in.isNot());
                        System.out.println("getTargetList size :" + in.getTargetList().size());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLNotExpr) {
                        SQLNotExpr not = (SQLNotExpr) func;
                        System.out.println("SQLNotExpr:");
                        System.out.println("EXPR:" + not.getExpr());
                        System.out.println("---------------------------");
                    } else if (func instanceof MySqlExtractExpr) {
                        MySqlExtractExpr extract = (MySqlExtractExpr) func;
                        System.out.println("MySqlExtractExpr:");
                        System.out.println("value:" + extract.getValue());
                        System.out.println("unit:" + extract.getUnit());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLCaseExpr) {
                        SQLCaseExpr Case = (SQLCaseExpr) func;
                        System.out.println("SQLCaseExpr:");
                        System.out.println("value:" + Case.getValueExpr());
                        System.out.println("else:" + Case.getElseExpr());
                        System.out.println("items size:" + Case.getItems().size());
                        System.out.println("---------------------------");
                    } else if (func instanceof SQLVariantRefExpr) {
                        SQLVariantRefExpr variant = (SQLVariantRefExpr) func;
                        System.out.println("SQLVariantRefExpr:");
                        System.out.println("name:" + variant.getName());
                        System.out.println("Global:" + variant.isGlobal());
                        System.out.println("index:" + variant.getIndex());
                        System.out.println("---------------------------");
                    } else // SQLAllColumnExpr
                    {
                        // MySqlOutputVisitor
                        System.out.println("item.getExpr():   :" + item.getExpr().getClass().toString() + "\n");
                    }
                }
            }
            // SQLInSubQueryExpr
            if (selectQueryBlock.getGroupBy() != null) {
                SQLSelectGroupByClause groupBy = selectQueryBlock.getGroupBy();
                for (SQLExpr groupByItem : groupBy.getItems()) {
                    System.out.println("groupByItem:");
                    System.out.println("class :" + groupByItem.getClass());
                    System.out.println("---------------------------");
                }
                if (groupBy.getHaving() != null) {
                    SQLExpr having = groupBy.getHaving();
                    System.out.println("having:");
                    System.out.println("class :" + having.getClass());
                    System.out.println("---------------------------");
                }
            // with rollup...
            }
            if (selectQueryBlock.getOrderBy() != null) {
                for (SQLSelectOrderByItem orderItem : selectQueryBlock.getOrderBy().getItems()) {
                    System.out.println("OrderBy:");
                    System.out.println("class :" + orderItem.getExpr().getClass());
                    System.out.println("---------------------------");
                }
            }
            if (selectQueryBlock.getWhere() != null) {
                if (selectQueryBlock.getWhere() instanceof SQLBinaryOpExpr) {
                    SQLBinaryOpExpr where = (SQLBinaryOpExpr) (selectQueryBlock.getWhere());
                    System.out.println("where right:");
                    System.out.println("class :" + where.getRight().getClass());
                    System.out.println("---------------------------");
                } else {
                    System.out.println("where:");
                    System.out.println("class :" + selectQueryBlock.getWhere().getClass());
                    System.out.println("---------------------------");
                }
            }
        } else if (sqlSelectQuery instanceof MySqlUnionQuery) {
        }
    } else if (statement instanceof MySqlShowColumnsStatement) {
        MySqlShowColumnsStatement showColumnsStatement = (MySqlShowColumnsStatement) statement;
        showColumnsStatement.setDatabase(null);
        showColumnsStatement.toString();
        System.out.println("change to->" + showColumnsStatement.toString());
    } else if (statement instanceof MySqlShowIndexesStatement) {
        MySqlShowIndexesStatement mySqlShowIndexesStatement = (MySqlShowIndexesStatement) statement;
        mySqlShowIndexesStatement.setDatabase(null);
        mySqlShowIndexesStatement.toString();
        System.out.println("change to 1->" + mySqlShowIndexesStatement.toString());
        System.out.println("change to 2->" + SQLUtils.toMySqlString(mySqlShowIndexesStatement));
    } else if (statement instanceof MySqlShowKeysStatement) {
        MySqlShowKeysStatement mySqlShowKeysStatement = (MySqlShowKeysStatement) statement;
        mySqlShowKeysStatement.setDatabase(null);
        mySqlShowKeysStatement.toString();
        System.out.println("change to 1->" + mySqlShowKeysStatement.toString());
        System.out.println("change to 2->" + SQLUtils.toMySqlString(mySqlShowKeysStatement));
    } else if (statement instanceof SQLSetStatement) {
        SQLSetStatement setStatement = (SQLSetStatement) statement;
        for (SQLAssignItem assignItem : setStatement.getItems()) {
            System.out.println("value is " + assignItem.getValue() + ", class is " + assignItem.getValue().getClass());
            if (assignItem.getTarget() instanceof SQLVariantRefExpr) {
                SQLVariantRefExpr target = (SQLVariantRefExpr) assignItem.getTarget();
                System.out.println("target is " + target + ", global is " + target.isGlobal());
            } else if (assignItem.getTarget() instanceof SQLPropertyExpr) {
                SQLPropertyExpr target = (SQLPropertyExpr) assignItem.getTarget();
                System.out.println("target is " + target.getName() + ", Owner is " + target.getOwner());
            } else {
                System.out.println("target is " + assignItem.getTarget() + ", class is " + assignItem.getTarget().getClass());
            }
        }
    } else if (statement instanceof MySqlSetNamesStatement) {
        MySqlSetNamesStatement setStatement = (MySqlSetNamesStatement) statement;
        System.out.println("charset =" + setStatement.getCharSet() + ",Collate =" + setStatement.getCollate() + ",default =" + setStatement.isDefault());
    } else if (statement instanceof MySqlSetCharSetStatement) {
        MySqlSetCharSetStatement setStatement = (MySqlSetCharSetStatement) statement;
        System.out.println("charset =" + setStatement.getCharSet() + ",Collate =" + setStatement.getCollate() + ",default =" + setStatement.isDefault());
    } else if (statement instanceof MySqlSetTransactionStatement) {
        MySqlSetTransactionStatement setStatement = (MySqlSetTransactionStatement) statement;
        System.out.println("global" + setStatement.getGlobal() + ",IsolationLevel=" + setStatement.getIsolationLevel() + ",access mode" + setStatement.getAccessModel());
    } else {
        System.out.println("statement:" + statement + "," + statement.getClass().toString());
    }
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlExtractExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) MySqlUnique(com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)

Aggregations

MySqlExtractExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLBinaryExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLHexExpr (com.alibaba.druid.sql.ast.expr.SQLHexExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)1 MySqlPrimaryKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)1 MySqlUnique (com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique)1 MysqlForeignKey (com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey)1 MySqlCharExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr)1 MySqlIntervalUnit (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalUnit)1 MySqlMatchAgainstExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr)1 MySqlUserName (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1