mysql parser

This commit is contained in:
温少
2012-05-15 20:33:39 +08:00
parent cb5fac206c
commit 2a0a78e8d3
8 changed files with 173 additions and 108 deletions
@@ -17,6 +17,8 @@ package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowColumnsStatement extends MySqlStatementImpl {
@@ -43,6 +45,12 @@ public class MySqlShowColumnsStatement extends MySqlStatementImpl {
}
public void setTable(SQLName table) {
if (table instanceof SQLPropertyExpr) {
SQLPropertyExpr propExpr = (SQLPropertyExpr) table;
this.setDatabase((SQLName) propExpr.getOwner());
this.table = new SQLIdentifierExpr(propExpr.getName());
return;
}
this.table = table;
}
@@ -0,0 +1,14 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowContributorsStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
}
@@ -61,6 +61,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStat
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement;
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;
@@ -411,7 +412,7 @@ public class MySqlStatementParser extends SQLStatementParser {
public SQLStatement parseShow() throws ParserException {
acceptIdentifier("SHOW");
if (identifierEquals("FULL")) {
if (lexer.token() == Token.FULL) {
lexer.nextToken();
acceptIdentifier("COLUMNS");
@@ -500,77 +501,77 @@ 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();
}
if (identifierEquals("CHARACTER")) {
lexer.nextToken();
accept(Token.SET);
MySqlShowCharacterSetStatement stmt = new MySqlShowCharacterSetStatement();
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
stmt.setPattern(this.exprParser.expr());
}
if (lexer.token() == Token.WHERE) {
lexer.nextToken();
stmt.setWhere(this.exprParser.expr());
}
return stmt;
}
if (identifierEquals("COLLATION")) {
lexer.nextToken();
MySqlShowCollationStatement stmt = new MySqlShowCollationStatement();
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
stmt.setPattern(this.exprParser.expr());
}
if (lexer.token() == Token.WHERE) {
lexer.nextToken();
stmt.setWhere(this.exprParser.expr());
}
return stmt;
}
if (identifierEquals("BINLOG")) {
lexer.nextToken();
acceptIdentifier("EVENTS");
MySqlShowBinLogEventsStatement stmt = new MySqlShowBinLogEventsStatement();
if (lexer.token() == Token.IN) {
lexer.nextToken();
stmt.setIn(this.exprParser.expr());
}
if (lexer.token() == Token.FROM) {
lexer.nextToken();
stmt.setFrom(this.exprParser.expr());
}
if (lexer.token() == Token.LIMIT) {
lexer.nextToken();
MySqlSelectQueryBlock.Limit limit = new MySqlSelectQueryBlock.Limit();
SQLExpr temp = this.exprParser.expr();
@@ -587,10 +588,15 @@ public class MySqlStatementParser extends SQLStatementParser {
}
stmt.setLimit(limit);
}
return stmt;
}
if (identifierEquals("CONTRIBUTORS")) {
lexer.nextToken();
return new MySqlShowContributorsStatement();
}
throw new ParserException("TODO " + lexer.stringVal());
}
@@ -1189,13 +1195,13 @@ public class MySqlStatementParser extends SQLStatementParser {
lexer.nextToken();
acceptIdentifier("ISOLATION");
acceptIdentifier("LEVEL");
MySqlSetTransactionIsolationLevelStatement stmt = new MySqlSetTransactionIsolationLevelStatement();
stmt.setGlobal(global);
if (identifierEquals("READ")) {
lexer.nextToken();
if (identifierEquals("UNCOMMITTED")) {
stmt.setLevel("READ UNCOMMITTED");
lexer.nextToken();
@@ -1225,11 +1231,11 @@ public class MySqlStatementParser extends SQLStatementParser {
} else {
throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
}
return stmt;
} else if (identifierEquals("NAMES")) {
lexer.nextToken();
MySqlSetNamesStatement stmt = new MySqlSetNamesStatement();
if (lexer.token() == Token.DEFAULT) {
lexer.nextToken();
@@ -1249,9 +1255,9 @@ public class MySqlStatementParser extends SQLStatementParser {
return stmt;
} else if (identifierEquals("CHARACTER")) {
lexer.nextToken();
accept(Token.SET);
MySqlSetCharSetStatement stmt = new MySqlSetCharSetStatement();
if (lexer.token() == Token.DEFAULT) {
lexer.nextToken();
@@ -1262,7 +1268,7 @@ public class MySqlStatementParser extends SQLStatementParser {
lexer.nextToken();
if (identifierEquals("COLLATE")) {
lexer.nextToken();
String collate = lexer.stringVal();
stmt.setCollate(collate);
lexer.nextToken();
@@ -1273,7 +1279,7 @@ public class MySqlStatementParser extends SQLStatementParser {
SQLSetStatement stmt = new SQLSetStatement();
parseAssignItems(stmt.getItems());
if (global != null && global.booleanValue()) {
SQLVariantRefExpr varRef = (SQLVariantRefExpr) stmt.getItems().get(0).getTarget();
varRef.setGlobal(true);
@@ -52,6 +52,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStat
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement;
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;
@@ -163,9 +164,9 @@ 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);
@@ -199,56 +200,60 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
void endVisit(MySqlDropTableStatement x);
boolean visit(MySqlDropTableStatement x);
void endVisit(MySqlPartitionByKey x);
boolean visit(MySqlPartitionByKey x);
boolean visit(MySqlSelectQueryBlock x);
void endVisit(MySqlSelectQueryBlock x);
boolean visit(MySqlOutFileExpr x);
void endVisit(MySqlOutFileExpr x);
boolean visit(MySqlDescribeStatement x);
void endVisit(MySqlDescribeStatement x);
boolean visit(MySqlUpdateStatement x);
void endVisit(MySqlUpdateStatement x);
boolean visit(MySqlSetTransactionIsolationLevelStatement x);
void endVisit(MySqlSetTransactionIsolationLevelStatement x);
boolean visit(MySqlSetNamesStatement x);
void endVisit(MySqlSetNamesStatement x);
boolean visit(MySqlSetCharSetStatement x);
void endVisit(MySqlSetCharSetStatement x);
boolean visit(MySqlShowBinaryLogsStatement x);
void endVisit(MySqlShowBinaryLogsStatement x);
boolean visit(MySqlShowMasterLogsStatement x);
void endVisit(MySqlShowMasterLogsStatement x);
boolean visit(MySqlShowCharacterSetStatement x);
void endVisit(MySqlShowCharacterSetStatement x);
boolean visit(MySqlShowCollationStatement x);
void endVisit(MySqlShowCollationStatement x);
boolean visit(MySqlShowBinLogEventsStatement x);
void endVisit(MySqlShowBinLogEventsStatement x);
boolean visit(MySqlShowContributorsStatement x);
void endVisit(MySqlShowContributorsStatement x);
}
@@ -39,6 +39,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStat
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement;
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;
@@ -542,4 +543,14 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
public void endVisit(MySqlShowCharacterSetStatement x) {
}
@Override
public boolean visit(MySqlShowContributorsStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowContributorsStatement x) {
}
}
@@ -71,6 +71,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStat
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement;
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;
@@ -1022,14 +1023,13 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getTable() != null) {
print(" FROM ");
if (x.getDatabase() != null) {
x.getDatabase().accept(this);
print('.');
}
x.getTable().accept(this);
}
if (x.getDatabase() != null) {
print(" FROM ");
x.getDatabase().accept(this);
}
if (x.getLike() != null) {
print(" LIKE ");
x.getLike().accept(this);
@@ -1575,4 +1575,15 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlShowCharacterSetStatement x) {
}
@Override
public boolean visit(MySqlShowContributorsStatement x) {
print("SHOW CONTRIBUTORS");
return false;
}
@Override
public void endVisit(MySqlShowContributorsStatement x) {
}
}
@@ -48,6 +48,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStat
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement;
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;
@@ -604,4 +605,14 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlShowCharacterSetStatement x) {
}
@Override
public boolean visit(MySqlShowContributorsStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowContributorsStatement x) {
}
}
@@ -17,6 +17,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsSt
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinaryLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCharacterSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCollationStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowContributorsStatement;
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;
@@ -290,57 +292,54 @@ public class DALParserTest extends TestCase {
Assert.assertEquals("SHOW CHARACTER SET", output);
}
public void test_show_columns() throws Exception {
String sql = "SHOW full columns from tb1 from db1 like 'var' ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowColumnsStatement show = (MySqlShowColumnsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW FULL COLUMNS FROM db1.tb1 LIKE 'var'", output);
}
public void test_show_columns_1() throws Exception {
String sql = "show columns from events where Field = 'name'";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowColumnsStatement show = (MySqlShowColumnsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW COLUMNS FROM events WHERE Field = 'name'", output);
}
public void test_show_columns_2() throws Exception {
String sql = "SHOW COLUMNS FROM City";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowColumnsStatement show = (MySqlShowColumnsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW COLUMNS FROM City", output);
}
public void test_show_columns_3() throws Exception {
String sql = "SHOW full columns from db1.tb1 like 'var' ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowColumnsStatement show = (MySqlShowColumnsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW FULL COLUMNS FROM db1.tb1 LIKE 'var'", output);
}
public void test_show_contributors() throws Exception {
String sql = "SHOW contributors";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowContributorsStatement show = (MySqlShowContributorsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW CONTRIBUTORS", output);
}
//
// public void testShow() throws Exception {
//
// sql = "SHOW full columns from tb1 from db1 like 'var' ";
// 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 FULL COLUMNS FROM db1.tb1 LIKE 'var'", output);
//
// sql = "SHOW full columns from tb1 from db1 where count(col)>10 ";
// 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 FULL COLUMNS FROM db1.tb1 WHERE COUNT(col) > 10", output);
//
// sql = "SHOW columns from tb1 from db1 like 'var' ";
// 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 COLUMNS FROM db1.tb1 LIKE 'var'", output);
//
// sql = "SHOW columns from tb1 like 'var' ";
// 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 COLUMNS FROM tb1 LIKE 'var'", output);
//
// sql = "SHOW full columns from tb1 from db1 ";
// 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 FULL COLUMNS FROM db1.tb1", output);
//
// sql = "SHOW contributors ";
// 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 CONTRIBUTORS", output);
//
// sql = "SHOW CREATE DATABASE db_name ";
// lexer = new SQLLexer(sql);
// parser = new DALParser(lexer, new SQLExprParser(lexer));