mysql parser

This commit is contained in:
温少
2012-05-15 21:35:12 +08:00
9 changed files with 258 additions and 17 deletions
@@ -0,0 +1,27 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowCreateTableStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private SQLExpr name;
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
}
visitor.endVisit(this);
}
public SQLExpr getName() {
return name;
}
public void setName(SQLExpr functionName) {
this.name = functionName;
}
}
@@ -0,0 +1,27 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowCreateTriggerStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private SQLExpr name;
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
}
visitor.endVisit(this);
}
public SQLExpr getName() {
return name;
}
public void setName(SQLExpr functionName) {
this.name = functionName;
}
}
@@ -0,0 +1,27 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowCreateViewStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private SQLExpr name;
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
}
visitor.endVisit(this);
}
public SQLExpr getName() {
return name;
}
public void setName(SQLExpr functionName) {
this.name = functionName;
}
}
@@ -66,6 +66,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabase
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
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;
@@ -636,6 +639,30 @@ public class MySqlStatementParser extends SQLStatementParser {
return stmt;
}
if (lexer.token() == Token.TABLE) {
lexer.nextToken();
MySqlShowCreateTableStatement stmt = new MySqlShowCreateTableStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
if (lexer.token() == Token.VIEW) {
lexer.nextToken();
MySqlShowCreateViewStatement stmt = new MySqlShowCreateViewStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
if (identifierEquals("TRIGGER")) {
lexer.nextToken();
MySqlShowCreateTriggerStatement stmt = new MySqlShowCreateTriggerStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
throw new ParserException("TODO " + lexer.stringVal());
}
@@ -57,6 +57,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabase
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
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;
@@ -276,4 +279,16 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlShowCreateProcedureStatement x);
void endVisit(MySqlShowCreateProcedureStatement x);
boolean visit(MySqlShowCreateTableStatement x);
void endVisit(MySqlShowCreateTableStatement x);
boolean visit(MySqlShowCreateTriggerStatement x);
void endVisit(MySqlShowCreateTriggerStatement x);
boolean visit(MySqlShowCreateViewStatement x);
void endVisit(MySqlShowCreateViewStatement x);
}
@@ -44,6 +44,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabase
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
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;
@@ -597,4 +600,34 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
public void endVisit(MySqlShowCreateProcedureStatement x) {
}
@Override
public boolean visit(MySqlShowCreateTableStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowCreateTableStatement x) {
}
@Override
public boolean visit(MySqlShowCreateTriggerStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowCreateTriggerStatement x) {
}
@Override
public boolean visit(MySqlShowCreateViewStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowCreateViewStatement x) {
}
}
@@ -76,6 +76,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabase
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
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;
@@ -1638,4 +1641,40 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlShowCreateProcedureStatement x) {
}
@Override
public boolean visit(MySqlShowCreateTableStatement x) {
print("SHOW CREATE TABLE ");
x.getName().accept(this);
return false;
}
@Override
public void endVisit(MySqlShowCreateTableStatement x) {
}
@Override
public boolean visit(MySqlShowCreateTriggerStatement x) {
print("SHOW CREATE TRIGGER ");
x.getName().accept(this);
return false;
}
@Override
public void endVisit(MySqlShowCreateTriggerStatement x) {
}
@Override
public boolean visit(MySqlShowCreateViewStatement x) {
print("SHOW CREATE VIEW ");
x.getName().accept(this);
return false;
}
@Override
public void endVisit(MySqlShowCreateViewStatement x) {
}
}
@@ -53,6 +53,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabase
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
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;
@@ -659,4 +662,34 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlShowCreateProcedureStatement x) {
}
@Override
public boolean visit(MySqlShowCreateTableStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowCreateTableStatement x) {
}
@Override
public boolean visit(MySqlShowCreateTriggerStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowCreateTriggerStatement x) {
}
@Override
public boolean visit(MySqlShowCreateViewStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowCreateViewStatement x) {
}
}
@@ -23,6 +23,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateDatabase
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateEventStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateFunctionStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateProcedureStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
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;
@@ -377,26 +380,36 @@ public class DALParserTest extends TestCase {
Assert.assertEquals("SHOW CREATE PROCEDURE x", output);
}
public void test_show_create_table() throws Exception {
String sql = "SHOW CREATE table x";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowCreateTableStatement show = (MySqlShowCreateTableStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW CREATE TABLE x", output);
}
public void test_show_create_trigger() throws Exception {
String sql = "SHOW CREATE trigger x";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowCreateTriggerStatement show = (MySqlShowCreateTriggerStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW CREATE TRIGGER x", output);
}
public void test_show_create_view() throws Exception {
String sql = "SHOW CREATE VIEW x";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowCreateViewStatement show = (MySqlShowCreateViewStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW CREATE VIEW x", output);
}
//
// public void testShow() throws Exception {
//
//
// sql = "SHOW create procedure pro ";
// 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 CREATE PROCEDURE pro", output);
//
// sql = "SHOW create table tb ";
// 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 CREATE TABLE tb", output);
//
// sql = "SHOW create trigger tri ";
// lexer = new SQLLexer(sql);
// parser = new DALParser(lexer, new SQLExprParser(lexer));