Search in sources :

Example 1 with SQLSetStatement

use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.

the class OdpsStatementParser method parseSet.

public SQLStatement parseSet() {
    List<String> comments = null;
    if (lexer.isKeepComments() && lexer.hasComment()) {
        comments = lexer.readAndResetComments();
    }
    accept(Token.SET);
    if (identifierEquals("LABEL")) {
        OdpsSetLabelStatement stmt = new OdpsSetLabelStatement();
        if (comments != null) {
            stmt.addBeforeComment(comments);
        }
        lexer.nextToken();
        stmt.setLabel(lexer.stringVal());
        lexer.nextToken();
        accept(Token.TO);
        if (lexer.token() == Token.USER) {
            lexer.nextToken();
            SQLName name = this.exprParser.name();
            stmt.setUser(name);
            return stmt;
        }
        accept(Token.TABLE);
        SQLExpr expr = this.exprParser.name();
        stmt.setTable(new SQLExprTableSource(expr));
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            this.exprParser.names(stmt.getColumns(), stmt);
            accept(Token.RPAREN);
        }
        return stmt;
    } else {
        SQLSetStatement stmt = new SQLSetStatement(getDbType());
        if (comments != null) {
            stmt.addBeforeComment(comments);
        }
        parseAssignItems(stmt.getItems(), stmt);
        return stmt;
    }
}
Also used : SQLSetStatement(com.alibaba.druid.sql.ast.statement.SQLSetStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) OdpsSetLabelStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsSetLabelStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with SQLSetStatement

use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.

the class DALParserTest method testSet_2.

public void testSet_2() throws Exception {
    String sql = "SET `sysVar1` = ?, @@gloBal . `var2` :=1";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLSetStatement set = (SQLSetStatement) parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    String output = SQLUtils.toMySqlString(set);
    Assert.assertEquals("SET @@`sysVar1` = ?, @@global.`var2` = 1", output);
}
Also used : SQLSetStatement(com.alibaba.druid.sql.ast.statement.SQLSetStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Example 3 with SQLSetStatement

use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.

the class DALParserTest method testSet_3.

public void testSet_3() throws Exception {
    String sql = "SET @usrVar1 := ?, @@`var2` =1, @@var3:=?, @'var\\'3'=?";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLSetStatement set = (SQLSetStatement) parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    String output = SQLUtils.toMySqlString(set);
    Assert.assertEquals("SET @usrVar1 = ?, @@`var2` = 1, @@var3 = ?, @'var\\'3' = ?", output);
}
Also used : SQLSetStatement(com.alibaba.druid.sql.ast.statement.SQLSetStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Example 4 with SQLSetStatement

use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.

the class SQLUtils method toSQLString.

public static String toSQLString(List<SQLStatement> statementList, String dbType, List<Object> parameters, FormatOption option, Map<String, String> tableMapping) {
    StringBuilder out = new StringBuilder();
    SQLASTOutputVisitor visitor = createFormatOutputVisitor(out, statementList, dbType);
    if (parameters != null) {
        visitor.setParameters(parameters);
    }
    if (option == null) {
        option = DEFAULT_FORMAT_OPTION;
    }
    visitor.setUppCase(option.isUppCase());
    visitor.setPrettyFormat(option.isPrettyFormat());
    visitor.setParameterized(option.isParameterized());
    if (tableMapping != null) {
        visitor.setTableMapping(tableMapping);
    }
    for (int i = 0; i < statementList.size(); i++) {
        SQLStatement stmt = statementList.get(i);
        if (i > 0) {
            visitor.print(";");
            SQLStatement preStmt = statementList.get(i - 1);
            List<String> comments = preStmt.getAfterCommentsDirect();
            if (comments != null) {
                for (int j = 0; j < comments.size(); ++j) {
                    String comment = comments.get(j);
                    if (j != 0) {
                        visitor.println();
                    }
                    visitor.print(comment);
                }
            }
            visitor.println();
            if (!(stmt instanceof SQLSetStatement)) {
                visitor.println();
            }
        }
        {
            List<String> comments = stmt.getBeforeCommentsDirect();
            if (comments != null) {
                for (String comment : comments) {
                    visitor.println(comment);
                }
            }
        }
        stmt.accept(visitor);
        if (i == statementList.size() - 1) {
            Boolean semi = (Boolean) stmt.getAttribute("format.semi");
            if (semi != null && semi.booleanValue()) {
                //                    if (stmt.hasAfterComment()) {
                //                        visitor.println();
                //                    }
                visitor.print(";");
            }
            List<String> comments = stmt.getAfterCommentsDirect();
            if (comments != null) {
                for (int j = 0; j < comments.size(); ++j) {
                    String comment = comments.get(j);
                    if (j != 0) {
                        visitor.println();
                    }
                    visitor.print(comment);
                }
            }
        }
    }
    return out.toString();
}
Also used : SQLSetStatement(com.alibaba.druid.sql.ast.statement.SQLSetStatement) SQLASTOutputVisitor(com.alibaba.druid.sql.visitor.SQLASTOutputVisitor) ArrayList(java.util.ArrayList) List(java.util.List) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 5 with SQLSetStatement

use of com.alibaba.druid.sql.ast.statement.SQLSetStatement in project druid by alibaba.

the class DALParserTest method testSet_1.

public void testSet_1() throws Exception {
    String sql = "seT sysVar1 = ? ";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLSetStatement set = (SQLSetStatement) parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    String output = SQLUtils.toMySqlString(set);
    Assert.assertEquals("SET @@sysVar1 = ?", output);
}
Also used : SQLSetStatement(com.alibaba.druid.sql.ast.statement.SQLSetStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Aggregations

SQLSetStatement (com.alibaba.druid.sql.ast.statement.SQLSetStatement)7 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 OdpsSetLabelStatement (com.alibaba.druid.sql.dialect.odps.ast.OdpsSetLabelStatement)1 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1