Skip to content

Commit

Permalink
split mysql visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
tristaZero committed Feb 5, 2020
1 parent 700adf0 commit 2dc9963
Show file tree
Hide file tree
Showing 9 changed files with 697 additions and 484 deletions.
Expand Up @@ -171,7 +171,77 @@ public enum RuleName {

UPDATE("Update"),

DELETE("Delete");
DELETE("Delete"),

REPLACE("Replace"),

CREATE_TABLE("CreateTable"),

ALTER_TABLE("AlterTable"),

DROP_TABLE("DropTable"),

TRUNCATE_TABLE("TruncateTable"),

CREATE_INDEX("CreateIndex"),

DROP_INDEX("DropIndex"),

SET_TRANSACTION("SetTransaction"),

BEGIN_TRANSACTION("BeginTransaction"),

SET_AUTOCOMMIT("SetAutoCommit"),

COMMIT("Commit"),

ROLLBACK("Rollback"),

SAVE_POINT("Savepoint"),

GRANT("Grant"),

REVOKE("Revoke"),

CREATE_USER("CreateUser"),

DROP_USER("DropUser"),

ALTER_USER("AlterUser"),

RENAME_USER("RenameUser"),

CREATE_ROLE("CreateRole"),

DROP_ROLE("DropRole"),

SET_DEFAULT_ROLE("SetDefaultRole"),

SET_ROLE("SetRole"),

SET_PASSWORD("SetPassword"),

USE("Use"),

DESC("Desc"),

SHOW_DATABASES("ShowDatabases"),

SHOW_TABLES("ShowTables"),

SHOW_TABLE_STATUS("ShowTableStatus"),

SHOW_COLUMNS("ShowColumns"),

SHOW_INDEX("ShowIndex"),

SHOW_CREATE_TABLE("ShowCreateTable"),

SHOW_OTHER("ShowOther"),

SET_VARIABLE("SetVariable"),

CALL("Call");

private final String name;

Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.spi.NewInstanceServiceLoader;
import org.apache.shardingsphere.sql.parser.api.SQLVisitor;
import org.apache.shardingsphere.sql.parser.core.extractor.util.RuleName;
import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
import org.apache.shardingsphere.sql.parser.spi.SQLParserEntry;

import java.util.Collection;
Expand All @@ -42,8 +43,18 @@ public final class SQLVisitorFactory {
private static final Map<String, Collection<String>> SQL_VISITOR_RULES = new LinkedHashMap<>();

static {
SQL_VISITOR_RULES.put("DMLVisitor",
Lists.newArrayList(RuleName.SELECT.getName(), RuleName.DELETE.getName(), RuleName.UPDATE.getName(), RuleName.INSERT.getName()));
SQL_VISITOR_RULES.put("DMLVisitor", Lists.newArrayList(RuleName.SELECT.getName(),
RuleName.DELETE.getName(), RuleName.UPDATE.getName(), RuleName.INSERT.getName(), RuleName.REPLACE.getName()));
SQL_VISITOR_RULES.put("DDLVisitor", Lists.newArrayList(RuleName.CREATE_TABLE.getName(), RuleName.ALTER_TABLE.getName(),
RuleName.DROP_TABLE.getName(), RuleName.TRUNCATE_TABLE.getName(), RuleName.CREATE_INDEX.getName(), RuleName.DROP_INDEX.getName()));
SQL_VISITOR_RULES.put("TCLVisitor", Lists.newArrayList(RuleName.SET_TRANSACTION.getName(), RuleName.BEGIN_TRANSACTION.getName(),
RuleName.SET_AUTOCOMMIT.getName(), RuleName.COMMIT.getName(), RuleName.ROLLBACK.getName(), RuleName.SAVE_POINT.getName()));
SQL_VISITOR_RULES.put("DCLVisitor", Lists.newArrayList(RuleName.GRANT.getName(), RuleName.REVOKE.getName(), RuleName.CREATE_USER.getName(),
RuleName.DROP_USER.getName(), RuleName.ALTER_USER.getName(), RuleName.RENAME_USER.getName(), RuleName.CREATE_ROLE.getName(),
RuleName.DROP_ROLE.getName(), RuleName.SET_DEFAULT_ROLE.getName(), RuleName.SET_ROLE.getName(), RuleName.SET_PASSWORD.getName()));
SQL_VISITOR_RULES.put("DALVisitor", Lists.newArrayList(RuleName.USE.getName(), RuleName.DESC.getName(), RuleName.SHOW_DATABASES.getName(),
RuleName.SHOW_TABLES.getName(), RuleName.SHOW_TABLE_STATUS.getName(), RuleName.SHOW_COLUMNS.getName(), RuleName.SHOW_INDEX.getName(),
RuleName.SHOW_CREATE_TABLE.getName(), RuleName.SHOW_OTHER.getName(), RuleName.SET_VARIABLE.getName(), RuleName.CALL.getName()));
}


Expand All @@ -69,8 +80,7 @@ private static String getVisitorName(final String visitorRuleName) {
return entry.getKey();
}
}
return "MySQLVisitor";
// throw new SQLParsingException("Could not find corresponding SQL visitor for %s.", visitorRuleName);
throw new SQLParsingException("Could not find corresponding SQL visitor for %s.", visitorRuleName);
}

@SneakyThrows
Expand Down
Expand Up @@ -22,6 +22,11 @@
import org.apache.shardingsphere.sql.parser.api.SQLVisitor;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementLexer;
import org.apache.shardingsphere.sql.parser.spi.SQLParserEntry;
import org.apache.shardingsphere.sql.parser.visitor.MySQLDALVisitor;
import org.apache.shardingsphere.sql.parser.visitor.MySQLDCLVisitor;
import org.apache.shardingsphere.sql.parser.visitor.MySQLDDLVisitor;
import org.apache.shardingsphere.sql.parser.visitor.MySQLDMLVisitor;
import org.apache.shardingsphere.sql.parser.visitor.MySQLTCLVisitor;

/**
* SQL parser entry for MySQL.
Expand Down Expand Up @@ -51,6 +56,15 @@ public Class<? extends SQLVisitor> getVisitorClass(final String visitorName) {
if (MySQLDMLVisitor.class.getSimpleName().contains(visitorName)) {
return MySQLDMLVisitor.class;
}
return MySQLVisitor.class;
if (MySQLDDLVisitor.class.getSimpleName().contains(visitorName)) {
return MySQLDDLVisitor.class;
}
if (MySQLTCLVisitor.class.getSimpleName().contains(visitorName)) {
return MySQLTCLVisitor.class;
}
if (MySQLDCLVisitor.class.getSimpleName().contains(visitorName)) {
return MySQLDCLVisitor.class;
}
return MySQLDALVisitor.class;
}
}

0 comments on commit 2dc9963

Please sign in to comment.