mysql parser enhance

This commit is contained in:
温少
2012-05-15 19:12:01 +08:00
9 changed files with 209 additions and 72 deletions
@@ -0,0 +1,14 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowAuthorsStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
}
@@ -0,0 +1,14 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowBinaryLogsStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
}
@@ -0,0 +1,14 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowMasterLogsStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
}
@@ -55,8 +55,11 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -494,6 +497,23 @@ public class MySqlStatementParser extends SQLStatementParser {
lexer.nextToken();
return new CobarShowStatus();
}
if (identifierEquals("AUTHORS")) {
lexer.nextToken();
return new MySqlShowAuthorsStatement();
}
if (identifierEquals("BINARY")) {
lexer.nextToken();
acceptIdentifier("LOGS");
return new MySqlShowBinaryLogsStatement();
}
if (identifierEquals("MASTER")) {
lexer.nextToken();
acceptIdentifier("LOGS");
return new MySqlShowMasterLogsStatement();
}
throw new ParserException("TODO");
}
@@ -46,8 +46,11 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -157,6 +160,10 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
void endVisit(MySqlShowStatusStatement x);
boolean visit(MySqlShowStatusStatement x);
void endVisit(MySqlShowAuthorsStatement x);
boolean visit(MySqlShowAuthorsStatement x);
void endVisit(CobarShowStatus x);
@@ -221,4 +228,12 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlSetCharSetStatement x);
void endVisit(MySqlSetCharSetStatement x);
boolean visit(MySqlShowBinaryLogsStatement x);
void endVisit(MySqlShowBinaryLogsStatement x);
boolean visit(MySqlShowMasterLogsStatement x);
void endVisit(MySqlShowMasterLogsStatement x);
}
@@ -33,8 +33,11 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.L
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -476,4 +479,34 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
public void endVisit(MySqlSetCharSetStatement x) {
}
@Override
public boolean visit(MySqlShowAuthorsStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowAuthorsStatement x) {
}
@Override
public boolean visit(MySqlShowBinaryLogsStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowBinaryLogsStatement x) {
}
@Override
public boolean visit(MySqlShowMasterLogsStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowMasterLogsStatement x) {
}
}
@@ -65,8 +65,11 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.L
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -1475,4 +1478,37 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlSetCharSetStatement x) {
}
@Override
public void endVisit(MySqlShowAuthorsStatement x) {
}
@Override
public boolean visit(MySqlShowAuthorsStatement x) {
print("SHOW AUTHORS");
return false;
}
@Override
public void endVisit(MySqlShowBinaryLogsStatement x) {
}
@Override
public boolean visit(MySqlShowBinaryLogsStatement x) {
print("SHOW BINARY LOGS");
return false;
}
@Override
public boolean visit(MySqlShowMasterLogsStatement x) {
print("SHOW MASTER LOGS");
return false;
}
@Override
public void endVisit(MySqlShowMasterLogsStatement x) {
}
}
@@ -42,8 +42,11 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.L
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -538,4 +541,34 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlSetCharSetStatement x) {
}
@Override
public boolean visit(MySqlShowAuthorsStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowAuthorsStatement x) {
}
@Override
public boolean visit(MySqlShowBinaryLogsStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowBinaryLogsStatement x) {
}
@Override
public boolean visit(MySqlShowMasterLogsStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowMasterLogsStatement x) {
}
}
@@ -9,12 +9,12 @@ import junit.framework.TestCase;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement;
<<<<<<< HEAD
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
=======
>>>>>>> fc222e6172fe4786fb2c68d21d9ef79937042e92
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.Token;
@@ -123,7 +123,6 @@ public class DALParserTest extends TestCase {
Assert.assertEquals("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE", output);
}
<<<<<<< HEAD
public void test_setNames() throws Exception {
String sql = "SET names default ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
@@ -169,76 +168,35 @@ public class DALParserTest extends TestCase {
Assert.assertEquals("SET CHARACTER SET DEFAULT", output);
}
=======
// public void testSet() throws Exception {
//
//
// sql = "SET names default ";
// lexer = new SQLLexer(sql);
// parser = new DALParser(lexer, new SQLExprParser(lexer));
// set = (DALSetNamesStatement) parser.set();
// parser.match(Token.EOF);
// output = output2MySQL(set, sql);
// Assert.assertEquals("SET NAMES DEFAULT", output);
//
// sql = "SET NAMEs 'utf8' collatE \"latin1_danish_ci\" ";
// lexer = new SQLLexer(sql);
// parser = new DALParser(lexer, new SQLExprParser(lexer));
// set = (DALSetNamesStatement) parser.set();
// parser.match(Token.EOF);
// output = output2MySQL(set, sql);
// Assert.assertEquals("SET NAMES utf8 COLLATE latin1_danish_ci", output);
//
// sql = "SET NAMEs utf8 ";
// lexer = new SQLLexer(sql);
// parser = new DALParser(lexer, new SQLExprParser(lexer));
// set = (DALSetNamesStatement) parser.set();
// parser.match(Token.EOF);
// output = output2MySQL(set, sql);
// Assert.assertEquals("SET NAMES utf8", output);
//
// sql = "SET CHARACTEr SEt 'utf8' ";
// lexer = new SQLLexer(sql);
// parser = new DALParser(lexer, new SQLExprParser(lexer));
// set = (DALSetCharacterSetStatement) parser.set();
// parser.match(Token.EOF);
// output = output2MySQL(set, sql);
// Assert.assertEquals("SET CHARACTER SET utf8", output);
//
// sql = "SET CHARACTEr SEt DEFaULT ";
// lexer = new SQLLexer(sql);
// parser = new DALParser(lexer, new SQLExprParser(lexer));
// set = (DALSetCharacterSetStatement) parser.set();
// parser.match(Token.EOF);
// output = output2MySQL(set, sql);
// Assert.assertEquals("SET CHARACTER SET DEFAULT", output);
// }
>>>>>>> fc222e6172fe4786fb2c68d21d9ef79937042e92
public void test_show_authors() throws Exception {
String sql = "shoW authors ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowAuthorsStatement show = (MySqlShowAuthorsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW AUTHORS", output);
}
public void test_show_binaryLogs() throws Exception {
String sql = "SHOW BINARY LOGS ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowBinaryLogsStatement show = (MySqlShowBinaryLogsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW BINARY LOGS", output);
}
public void test_show_masterLogs() throws Exception {
String sql = "SHOW MASTER LOGS ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowMasterLogsStatement show = (MySqlShowMasterLogsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW MASTER LOGS", output);
}
//
// public void testShow() throws Exception {
// String sql = "shoW authors ";
// SQLLexer lexer = new SQLLexer(sql);
// DALParser parser = new DALParser(lexer, new SQLExprParser(lexer));
// DALShowStatement show = (DALShowStatement) parser.show();
// parser.match(Token.EOF);
// String output = output2MySQL(show, sql);
// Assert.assertEquals("SHOW AUTHORS", output);
//
// sql = "SHOW BINARY LOGS ";
// lexer = new SQLLexer(sql);
// parser = new DALParser(lexer, new SQLExprParser(lexer));
// show = (DALShowStatement) parser.show();
// parser.match(Token.EOF);
// output = output2MySQL(show, sql);
// Assert.assertEquals("SHOW BINARY LOGS", output);
//
// sql = "SHOW MASTER LOGS ";
// lexer = new SQLLexer(sql);
// parser = new DALParser(lexer, new SQLExprParser(lexer));
// show = (DALShowStatement) parser.show();
// parser.match(Token.EOF);
// output = output2MySQL(show, sql);
// Assert.assertEquals("SHOW BINARY LOGS", output);
//
// sql = "SHOW binlog events in 'a' from 1 limit 1,2 ";
// lexer = new SQLLexer(sql);