Search in sources :

Example 1 with DALSetStatement

use of com.alibaba.cobar.parser.ast.stmt.dal.DALSetStatement in project cobar by alibaba.

the class MySQLDALParserTest method testSet.

public void testSet() throws Exception {
    String sql = "seT sysVar1 = ? ";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDALParser parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    SQLStatement set = (DALSetStatement) parser.set();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(set, sql);
    Assert.assertEquals("SET @@sysVar1 = ?", output);
    sql = "SET `sysVar1` = ?, @@gloBal . `var2` :=1 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (DALSetStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET @@`sysVar1` = ?, @@global.`var2` = 1", output);
    sql = "SET @usrVar1 := ?, @@`var2` =1, @@var3:=?, @'var\\'3'=? ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (DALSetStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET @usrVar1 = ?, @@`var2` = 1, @@var3 = ?, @'var\\'3' = ?", output);
    sql = "SET GLOBAL var1=1, SESSION var2:=2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (DALSetStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET @@global.var1 = 1, @@var2 = 2", output);
    sql = "SET @@GLOBAL. var1=1, SESSION var2:=2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (DALSetStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET @@global.var1 = 1, @@var2 = 2", output);
    sql = "SET transaction ISOLATION LEVEL READ UNCOMMITTED ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (MTSSetTransactionStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED", output);
    sql = "SET global transaction ISOLATION LEVEL READ COMMITTED ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (MTSSetTransactionStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED", output);
    sql = "SET transaction ISOLATION LEVEL REPEATABLE READ ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (MTSSetTransactionStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ", output);
    sql = "SET session transaction ISOLATION LEVEL SERIALIZABLE ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (MTSSetTransactionStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE", output);
    sql = "SET names default ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (DALSetNamesStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET NAMES DEFAULT", output);
    sql = "SET NAMEs 'utf8' collatE \"latin1_danish_ci\" ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (DALSetNamesStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET NAMES utf8 COLLATE latin1_danish_ci", output);
    sql = "SET NAMEs utf8  ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (DALSetNamesStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET NAMES utf8", output);
    sql = "SET CHARACTEr SEt 'utf8'  ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (DALSetCharacterSetStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET CHARACTER SET utf8", output);
    sql = "SET CHARACTEr SEt DEFaULT  ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    set = (DALSetCharacterSetStatement) parser.set();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(set, sql);
    Assert.assertEquals("SET CHARACTER SET DEFAULT", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) DALSetStatement(com.alibaba.cobar.parser.ast.stmt.dal.DALSetStatement)

Example 2 with DALSetStatement

use of com.alibaba.cobar.parser.ast.stmt.dal.DALSetStatement in project cobar by alibaba.

the class MySQLDALParser method set.

/**
 * @return {@link DALSetStatement} or {@link MTSSetTransactionStatement}
 */
@SuppressWarnings("unchecked")
public SQLStatement set() throws SQLSyntaxErrorException {
    match(KW_SET);
    if (lexer.token() == KW_OPTION) {
        lexer.nextToken();
    }
    if (lexer.token() == IDENTIFIER && SpecialIdentifier.NAMES == specialIdentifiers.get(lexer.stringValueUppercase())) {
        if (lexer.nextToken() == KW_DEFAULT) {
            lexer.nextToken();
            return new DALSetNamesStatement();
        }
        String charsetName = getStringValue();
        String collationName = null;
        if (lexer.token() == KW_COLLATE) {
            lexer.nextToken();
            collationName = getStringValue();
        }
        return new DALSetNamesStatement(charsetName, collationName);
    } else if (lexer.token() == KW_CHARACTER) {
        lexer.nextToken();
        match(KW_SET);
        if (lexer.token() == KW_DEFAULT) {
            lexer.nextToken();
            return new DALSetCharacterSetStatement();
        }
        String charsetName = getStringValue();
        return new DALSetCharacterSetStatement(charsetName);
    }
    List<Pair<VariableExpression, Expression>> assignmentList;
    Object obj = varAssign();
    if (obj instanceof MTSSetTransactionStatement) {
        return (MTSSetTransactionStatement) obj;
    }
    Pair<VariableExpression, Expression> pair = (Pair<VariableExpression, Expression>) obj;
    if (lexer.token() != PUNC_COMMA) {
        assignmentList = new ArrayList<Pair<VariableExpression, Expression>>(1);
        assignmentList.add(pair);
        return new DALSetStatement(assignmentList);
    }
    assignmentList = new LinkedList<Pair<VariableExpression, Expression>>();
    assignmentList.add(pair);
    for (; lexer.token() == PUNC_COMMA; ) {
        lexer.nextToken();
        pair = (Pair<VariableExpression, Expression>) varAssign();
        assignmentList.add(pair);
    }
    return new DALSetStatement(assignmentList);
}
Also used : DALSetNamesStatement(com.alibaba.cobar.parser.ast.stmt.dal.DALSetNamesStatement) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) MTSSetTransactionStatement(com.alibaba.cobar.parser.ast.stmt.mts.MTSSetTransactionStatement) Expression(com.alibaba.cobar.parser.ast.expression.Expression) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) DALSetCharacterSetStatement(com.alibaba.cobar.parser.ast.stmt.dal.DALSetCharacterSetStatement) DALSetStatement(com.alibaba.cobar.parser.ast.stmt.dal.DALSetStatement) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

DALSetStatement (com.alibaba.cobar.parser.ast.stmt.dal.DALSetStatement)2 Expression (com.alibaba.cobar.parser.ast.expression.Expression)1 VariableExpression (com.alibaba.cobar.parser.ast.expression.primary.VariableExpression)1 LiteralString (com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)1 SQLStatement (com.alibaba.cobar.parser.ast.stmt.SQLStatement)1 DALSetCharacterSetStatement (com.alibaba.cobar.parser.ast.stmt.dal.DALSetCharacterSetStatement)1 DALSetNamesStatement (com.alibaba.cobar.parser.ast.stmt.dal.DALSetNamesStatement)1 MTSSetTransactionStatement (com.alibaba.cobar.parser.ast.stmt.mts.MTSSetTransactionStatement)1 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)1 Pair (com.alibaba.cobar.parser.util.Pair)1