Search in sources :

Example 16 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer 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 17 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLDALParserTest method testShow.

public void testShow() throws Exception {
    String sql = "shoW authors  ";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDALParser parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    DALShowStatement show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW AUTHORS", output);
    sql = "SHOW BINARY LOGS  ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW BINARY LOGS", output);
    sql = "SHOW MASTER LOGS  ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW BINARY LOGS", output);
    sql = "SHOW binlog events in 'a' from 1 limit 1,2  ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW BINLOG EVENTS IN 'a' FROM 1 LIMIT 1, 2", output);
    sql = "SHOW binlog events from 1 limit 1,2  ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW BINLOG EVENTS FROM 1 LIMIT 1, 2", output);
    sql = "SHOW binlog events ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW BINLOG EVENTS", output);
    sql = "SHOW CHARACTER SET like 'var' ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CHARACTER SET LIKE 'var'", output);
    sql = "SHOW CHARACTER SET where (select a from tb)in(a) ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CHARACTER SET WHERE (SELECT a FROM tb) IN (a)", output);
    sql = "SHOW CHARACTER SET ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CHARACTER SET", output);
    sql = "SHOW collation ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW COLLATION", output);
    sql = "SHOW Collation like 'var1' ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW COLLATION LIKE 'var1'", output);
    sql = "SHOW collation where avg((select * from (tb1,tb2)))!=100 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW COLLATION WHERE AVG(SELECT * FROM tb1, tb2) != 100", output);
    sql = "SHOW full columns from tb1 from db1 like 'var' ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FULL COLUMNS FROM db1.tb1 LIKE 'var'", output);
    sql = "SHOW full columns from tb1 from db1 where count(col)>10 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FULL COLUMNS FROM db1.tb1 WHERE COUNT(col) > 10", output);
    sql = "SHOW columns from tb1 from db1 like 'var' ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW COLUMNS FROM db1.tb1 LIKE 'var'", output);
    sql = "SHOW columns from tb1 like 'var' ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW COLUMNS FROM tb1 LIKE 'var'", output);
    sql = "SHOW full columns from tb1 from db1 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FULL COLUMNS FROM db1.tb1", output);
    sql = "SHOW contributors ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CONTRIBUTORS", output);
    sql = "SHOW CREATE DATABASE db_name ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CREATE DATABASE db_name", output);
    sql = "SHOW create event expr ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CREATE EVENT expr", output);
    sql = "SHOW create function func ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CREATE FUNCTION func", output);
    sql = "SHOW create procedure pro ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CREATE PROCEDURE pro", output);
    sql = "SHOW create table tb ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CREATE TABLE tb", output);
    sql = "SHOW create trigger tri ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CREATE TRIGGER tri", output);
    sql = "SHOW create view view";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW CREATE VIEW view", output);
    sql = "SHOW databases like 'var'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW DATABASES LIKE 'var'", output);
    sql = "SHOW databases where (select * from `select`)is not null";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW DATABASES WHERE (SELECT * FROM `select`) IS NOT NULL", output);
    sql = "SHOW databases ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW DATABASES", output);
    sql = "SHOW engine innodb status";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW ENGINE INNODB STATUS", output);
    sql = "SHOW engine innodb mutex";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW ENGINE INNODB MUTEX", output);
    sql = "SHOW engine performance_schema status";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW ENGINE PERFORMANCE SCHEMA STATUS", output);
    sql = "SHOW engines";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW ENGINES", output);
    sql = "SHOW storage engines";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW ENGINES", output);
    sql = "SHOW errors";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW ERRORS", output);
    sql = "SHOW errors limit 1 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW ERRORS LIMIT 0, 1", output);
    sql = "SHOW count(*) errors";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW COUNT(*) ERRORS", output);
    sql = "SHOW events";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW EVENTS", output);
    sql = "SHOW events from expr like 'var' ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW EVENTS FROM expr LIKE 'var'", output);
    sql = "SHOW events from expr where expr1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW EVENTS FROM expr WHERE expr1", output);
    sql = "SHOW function code expr";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FUNCTION CODE expr", output);
    sql = "SHOW function status like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FUNCTION STATUS LIKE 'expr'", output);
    sql = "SHOW function status where a is true";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FUNCTION STATUS WHERE a IS TRUE", output);
    sql = "SHOW function status ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FUNCTION STATUS", output);
    sql = "SHOW grants for 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW GRANTS FOR 'expr'", output);
    sql = "SHOW grants";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW GRANTS", output);
    sql = "SHOW index from tb1 from db";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW INDEX IN db.tb1", output);
    sql = "SHOW index from tb1 in db";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW INDEX IN db.tb1", output);
    sql = "SHOW index in tb1 from db";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW INDEX IN db.tb1", output);
    sql = "SHOW index in tb1 in db";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW INDEX IN db.tb1", output);
    sql = "SHOW indexes from tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW INDEXES IN tb1", output);
    sql = "SHOW keys in tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW KEYS IN tb1", output);
    sql = "SHOW master status";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW MASTER STATUS", output);
    sql = "SHOW open tables from db like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW OPEN TABLES FROM db LIKE 'expr'", output);
    sql = "SHOW open tables from db where tb is not null";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW OPEN TABLES FROM db WHERE tb IS NOT NULL", output);
    sql = "SHOW open tables from db";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW OPEN TABLES FROM db", output);
    sql = "SHOW open tables in db";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW OPEN TABLES FROM db", output);
    sql = "SHOW open tables";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW OPEN TABLES", output);
    sql = "SHOW plugins";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PLUGINS", output);
    sql = "SHOW privileges";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PRIVILEGES", output);
    sql = "SHOW procedure code proc";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROCEDURE CODE proc", output);
    sql = "SHOW procedure status like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROCEDURE STATUS LIKE 'expr'", output);
    sql = "SHOW procedure status where (a||b)*(a&&b)";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROCEDURE STATUS WHERE (a OR b) * (a AND b)", output);
    sql = "SHOW procedure status";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROCEDURE STATUS", output);
    sql = "SHOW processlist";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROCESSLIST", output);
    sql = "SHOW full processlist";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FULL PROCESSLIST", output);
    sql = "SHOW profiles";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROFILES", output);
    sql = "SHOW profile all,block io,context switches,cpu,ipc,memory," + "page faults,source,swaps for query 2 limit 1 offset 2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROFILE ALL, BLOCK IO, CONTEXT SWITCHES, CPU, IPC, MEMORY, " + "PAGE FAULTS, SOURCE, SWAPS FOR QUERY 2 LIMIT 2, 1", output);
    sql = "SHOW profile";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROFILE", output);
    sql = "SHOW profile all,block io,context switches,cpu,ipc," + "memory,page faults,source,swaps for query 2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROFILE ALL, BLOCK IO, CONTEXT SWITCHES, CPU, IPC, " + "MEMORY, PAGE FAULTS, SOURCE, SWAPS FOR QUERY 2", output);
    sql = "SHOW profile all for query 2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW PROFILE ALL FOR QUERY 2", output);
    sql = "SHOW slave hosts";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SLAVE HOSTS", output);
    sql = "SHOW slave status";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SLAVE STATUS", output);
    sql = "SHOW global status like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW GLOBAL STATUS LIKE 'expr'", output);
    sql = "SHOW global status where ${abc}";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW GLOBAL STATUS WHERE ${abc}", output);
    sql = "SHOW session status like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION STATUS LIKE 'expr'", output);
    sql = "SHOW session status where ?";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION STATUS WHERE ?", output);
    sql = "SHOW status like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION STATUS LIKE 'expr'", output);
    sql = "SHOW status where 0b10^b'11'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION STATUS WHERE b'10' ^ b'11'", output);
    sql = "SHOW status";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION STATUS", output);
    sql = "SHOW global status";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW GLOBAL STATUS", output);
    sql = "SHOW session status";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION STATUS", output);
    sql = "SHOW table status from db like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TABLE STATUS FROM db LIKE 'expr'", output);
    sql = "SHOW table status in db where (select a)>(select b)";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TABLE STATUS FROM db WHERE (SELECT a) > (SELECT b)", output);
    sql = "SHOW table status from db where id1=a||b";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TABLE STATUS FROM db WHERE id1 = a OR b", output);
    sql = "SHOW table status ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TABLE STATUS", output);
    sql = "SHOW tables from db like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TABLES FROM db LIKE 'expr'", output);
    sql = "SHOW tables in db where !a";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TABLES FROM db WHERE ! a", output);
    sql = "SHOW tables like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TABLES LIKE 'expr'", output);
    sql = "SHOW tables where log((select a))=b";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TABLES WHERE LOG(SELECT a) = b", output);
    sql = "SHOW tables ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TABLES", output);
    sql = "SHOW full tables from db like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FULL TABLES FROM db LIKE 'expr'", output);
    sql = "SHOW full tables in db where id1=abs((select a))";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FULL TABLES FROM db WHERE id1 = ABS(SELECT a)", output);
    sql = "SHOW full tables ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW FULL TABLES", output);
    sql = "SHOW triggers from db like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TRIGGERS FROM db LIKE 'expr'", output);
    sql = "SHOW triggers in db where strcmp('test1','test2')";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TRIGGERS FROM db WHERE STRCMP('test1', 'test2')", output);
    sql = "SHOW triggers ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW TRIGGERS", output);
    sql = "SHOW global variables like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW GLOBAL VARIABLES LIKE 'expr'", output);
    sql = "SHOW global variables where ~a is null";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW GLOBAL VARIABLES WHERE ~ a IS NULL", output);
    sql = "SHOW session variables like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION VARIABLES LIKE 'expr'", output);
    sql = "SHOW session variables where a*b+1=c";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION VARIABLES WHERE a * b + 1 = c", output);
    sql = "SHOW variables like 'expr'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION VARIABLES LIKE 'expr'", output);
    sql = "SHOW variables where a&&b";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION VARIABLES WHERE a AND b", output);
    sql = "SHOW variables";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION VARIABLES", output);
    sql = "SHOW global variables";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW GLOBAL VARIABLES", output);
    sql = "SHOW session variables";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW SESSION VARIABLES", output);
    sql = "SHOW warnings limit 1,2 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW WARNINGS LIMIT 1, 2", output);
    sql = "SHOW warnings";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW WARNINGS", output);
    sql = "SHOW count(*) warnings";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    show = (DALShowStatement) parser.show();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(show, sql);
    Assert.assertEquals("SHOW COUNT(*) WARNINGS", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DALShowStatement(com.alibaba.cobar.parser.ast.stmt.dal.DALShowStatement)

Example 18 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLDDLParserTest method testDDLStmt.

public void testDDLStmt() throws Exception {
    String sql = "alTer ignore table tb_name";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDDLParser parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    DDLStatement dst = parser.ddlStmt();
    sql = "alTeR table tb_name";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    sql = "crEate temporary tabLe if not exists tb_name";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    sql = "crEate tabLe if not exists tb_name";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    sql = "crEate temporary tabLe tb_name";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    sql = "crEate unique index index_name on tb(col(id)) desc";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    sql = "crEate fulltext index index_name on tb(col(id))";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    sql = "crEate spatial index index_name on tb(col(id))";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    sql = "crEate index index_name using hash on tb(col(id))";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    sql = "drop index index_name on tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    String output = output2MySQL(dst, sql);
    Assert.assertEquals("DROP INDEX index_name ON tb1", output);
    sql = "drop temporary tabLe if exists tb1,tb2,tb3 restrict";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    output = output2MySQL(dst, sql);
    Assert.assertEquals("DROP TEMPORARY TABLE IF EXISTS tb1, tb2, tb3 RESTRICT", output);
    sql = "drop temporary tabLe if exists tb1,tb2,tb3 cascade";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    output = output2MySQL(dst, sql);
    Assert.assertEquals("DROP TEMPORARY TABLE IF EXISTS tb1, tb2, tb3 CASCADE", output);
    sql = "drop temporary tabLe if exists tb1 cascade";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    output = output2MySQL(dst, sql);
    Assert.assertEquals("DROP TEMPORARY TABLE IF EXISTS tb1 CASCADE", output);
    sql = "drop tabLe if exists tb1 cascade";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    output = output2MySQL(dst, sql);
    Assert.assertEquals("DROP TABLE IF EXISTS tb1 CASCADE", output);
    sql = "drop temporary tabLe tb1 cascade";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    output = output2MySQL(dst, sql);
    Assert.assertEquals("DROP TEMPORARY TABLE tb1 CASCADE", output);
    sql = "rename table tb1 to ntb1,tb2 to ntb2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    output = output2MySQL(dst, sql);
    Assert.assertEquals("RENAME TABLE tb1 TO ntb1, tb2 TO ntb2", output);
    sql = "rename table tb1 to ntb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    dst = parser.ddlStmt();
    output = output2MySQL(dst, sql);
    Assert.assertEquals("RENAME TABLE tb1 TO ntb1", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DDLStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLStatement)

Example 19 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLDMLCallParserTest method testCall.

public void testCall() throws SQLSyntaxErrorException {
    String sql = "call p(?,?) ";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLCallParser parser = new MySQLDMLCallParser(lexer, new MySQLExprParser(lexer));
    DMLCallStatement calls = parser.call();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(calls, sql);
    Assert.assertEquals("CALL p(?, ?)", output);
    sql = "call p(@var1,'@var2',var3)";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLCallParser(lexer, new MySQLExprParser(lexer));
    calls = parser.call();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(calls, sql);
    Assert.assertEquals("CALL p(@var1, '@var2', var3)", output);
    sql = "call p()";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLCallParser(lexer, new MySQLExprParser(lexer));
    calls = parser.call();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(calls, sql);
    Assert.assertEquals("CALL p()", output);
    sql = "call p(?)";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLCallParser(lexer, new MySQLExprParser(lexer));
    calls = parser.call();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(calls, sql);
    Assert.assertEquals("CALL p(?)", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLCallStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLCallStatement)

Example 20 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLMTSParserTest method testRollback.

public void testRollback() throws SQLSyntaxErrorException {
    // ROLLBACK [WORK] TO [SAVEPOINT] identifier
    // ROLLBACK [WORK] [AND [NO] CHAIN | [NO] RELEASE]
    String sql = "rollBack work  ";
    MySQLMTSParser parser = new MySQLMTSParser(new MySQLLexer(sql));
    MTSRollbackStatement rollback = parser.rollback();
    String output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.UN_DEF, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
    sql = "rollBack  ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.UN_DEF, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
    sql = "rollBack work TO savepoint 123e ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK TO SAVEPOINT 123e", output);
    Assert.assertEquals("123e", rollback.getSavepoint().getIdText());
    Assert.assertNull(rollback.getCompleteType());
    sql = "rollBack to savePOINT savepoint ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK TO SAVEPOINT savepoint", output);
    Assert.assertEquals("savepoint", rollback.getSavepoint().getIdText());
    Assert.assertNull(rollback.getCompleteType());
    sql = "rollBack to `select` ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK TO SAVEPOINT `select`", output);
    Assert.assertEquals("`select`", rollback.getSavepoint().getIdText());
    Assert.assertNull(rollback.getCompleteType());
    sql = "rollBack work to  `select` ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK TO SAVEPOINT `select`", output);
    Assert.assertEquals("`select`", rollback.getSavepoint().getIdText());
    Assert.assertNull(rollback.getCompleteType());
    sql = "rollBack work and no chaiN ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK AND NO CHAIN", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.NO_CHAIN, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
    sql = "rollBack work and  chaiN ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK AND CHAIN", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.CHAIN, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
    sql = "rollBack work NO release ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK NO RELEASE", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.NO_RELEASE, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
    sql = "rollBack work  release ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK RELEASE", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.RELEASE, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
    sql = "rollBack  and no chaiN ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK AND NO CHAIN", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.NO_CHAIN, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
    sql = "rollBack  and  chaiN ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK AND CHAIN", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.CHAIN, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
    sql = "rollBack  NO release ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK NO RELEASE", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.NO_RELEASE, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
    sql = "rollBack   release ";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    rollback = parser.rollback();
    output = output2MySQL(rollback, sql);
    Assert.assertEquals("ROLLBACK RELEASE", output);
    Assert.assertEquals(MTSRollbackStatement.CompleteType.RELEASE, rollback.getCompleteType());
    Assert.assertNull(rollback.getSavepoint());
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) MTSRollbackStatement(com.alibaba.cobar.parser.ast.stmt.mts.MTSRollbackStatement)

Aggregations

MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)40 Expression (com.alibaba.cobar.parser.ast.expression.Expression)18 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)17 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)17 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)17 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)16 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)16 TernaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression)16 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)16 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)16 ArithmeticDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression)16 ArithmeticIntegerDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression)16 ArithmeticModExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression)16 ArithmeticMultiplyExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression)16 ArithmeticSubtractExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression)16 MinusExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression)16 BitAndExpression (com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression)16 BitInvertExpression (com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression)16 BitOrExpression (com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression)16 BitShiftExpression (com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression)16