Skip to content

Commit

Permalink
Merge pull request #996 from tristaZero/dev
Browse files Browse the repository at this point in the history
Support authorization parsing for Sharding core
  • Loading branch information
terrymanu committed Jul 11, 2018
2 parents 32fa95d + 0888154 commit c60f3bd
Show file tree
Hide file tree
Showing 105 changed files with 2,430 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ public enum SQLType {
/**
* Database administrator Language.
*/
DAL
DAL,

/**
* Database control Language.
*/
DCL
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ private Iterator<MemoryQueryResultRow> init(final List<QueryResult> queryResults
String actualTableName = memoryResultSetRow.getCell(1).toString();
Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(actualTableName);
if (!tableRule.isPresent()) {
if (shardingMetaData.getTableMetaDataMap().keySet().contains(actualTableName)) {
if (shardingMetaData.getTableMetaDataMap().keySet().contains(actualTableName) && tableNames.add(actualTableName)) {
result.add(memoryResultSetRow);
} else if (!shardingMetaData.isSupportedDatabaseType()) {
} else if (!shardingMetaData.isSupportedDatabaseType() && tableNames.add(actualTableName)) {
result.add(memoryResultSetRow);
}
} else if (tableNames.add(tableRule.get().getLogicTable())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.UseStatement;
import io.shardingsphere.core.parsing.parser.exception.SQLParsingException;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.dcl.DCLStatement;
import io.shardingsphere.core.parsing.parser.sql.ddl.DDLStatement;
import io.shardingsphere.core.parsing.parser.sql.dml.DMLStatement;
import io.shardingsphere.core.parsing.parser.sql.dml.insert.InsertStatement;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingsphere.core.parsing.parser.sql.tcl.TCLStatement;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.Collection;

/**
* SQL judge engine.
*
* @author zhangliang
* @author panjuan
*/
@RequiredArgsConstructor
public final class SQLJudgeEngine {
Expand All @@ -67,20 +72,25 @@ public SQLStatement judge() {
if (isDML(tokenType)) {
return getDMLStatement(tokenType);
}
if (isDDL(tokenType)) {
return getDDLStatement();
}
if (isTCL(tokenType)) {
return getTCLStatement();
}
if (isDAL(tokenType)) {
return getDALStatement(tokenType, lexerEngine);
}
lexerEngine.nextToken();
if (isDCL(tokenType, lexerEngine)) {
return getDCLStatement();
}
if (isDDL(tokenType, lexerEngine)) {
return getDDLStatement();
}
} else {
lexerEngine.nextToken();
}
if (tokenType instanceof Assist && Assist.END == tokenType) {
throw new SQLParsingException("Unsupported SQL statement: [%s]", sql);
}
lexerEngine.nextToken();
}
}

Expand All @@ -92,8 +102,20 @@ private boolean isDML(final TokenType tokenType) {
return DefaultKeyword.INSERT == tokenType || DefaultKeyword.UPDATE == tokenType || DefaultKeyword.DELETE == tokenType;
}

private boolean isDDL(final TokenType tokenType) {
return DefaultKeyword.CREATE == tokenType || DefaultKeyword.ALTER == tokenType || DefaultKeyword.DROP == tokenType || DefaultKeyword.TRUNCATE == tokenType;
private static boolean isDDL(final TokenType tokenType, final LexerEngine lexerEngine) {
Collection<DefaultKeyword> primaryTokens = Arrays.asList(DefaultKeyword.CREATE, DefaultKeyword.ALTER, DefaultKeyword.DROP, DefaultKeyword.TRUNCATE);
Collection<DefaultKeyword> secondaryTokens = Arrays.asList(DefaultKeyword.LOGIN, DefaultKeyword.USER, DefaultKeyword.ROLE);
return primaryTokens.contains(tokenType) && !secondaryTokens.contains(lexerEngine.getCurrentToken().getType());
}

private static boolean isDCL(final TokenType tokenType, final LexerEngine lexerEngine) {
Collection<DefaultKeyword> primaryTokens = Arrays.asList(DefaultKeyword.GRANT, DefaultKeyword.REVOKE, DefaultKeyword.DENY);
Collection<DefaultKeyword> secondaryTokens = Arrays.asList(DefaultKeyword.LOGIN, DefaultKeyword.USER, DefaultKeyword.ROLE);
if (primaryTokens.contains(tokenType)) {
return true;
}
primaryTokens = Arrays.asList(DefaultKeyword.CREATE, DefaultKeyword.ALTER, DefaultKeyword.DROP, DefaultKeyword.RENAME);
return primaryTokens.contains(tokenType) && secondaryTokens.contains(lexerEngine.getCurrentToken().getType());
}

private boolean isTCL(final TokenType tokenType) {
Expand All @@ -120,6 +142,10 @@ private SQLStatement getDDLStatement() {
return new DDLStatement();
}

private SQLStatement getDCLStatement() {
return new DCLStatement();
}

private SQLStatement getTCLStatement() {
return new TCLStatement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public void nextToken() {
lexer.nextToken();
}

/**
* Is end or not.
*
* @return current token is end token or not.
*/
public boolean isEnd() {
return lexer.getCurrentToken().getType() == Assist.END;
}

/**
* Get current token.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public enum DefaultKeyword implements Keyword {
DISABLE,
VALIDATE,
USER,
ROLE,
LOGIN,
DENY,
IDENTIFIED,

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
package io.shardingsphere.core.parsing.parser.dialect.mysql.sql;

import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.parser.sql.ddl.alter.AbstractAlterParser;
import io.shardingsphere.core.parsing.parser.sql.ddl.alter.table.AbstractAlterTableParser;
import io.shardingsphere.core.rule.ShardingRule;

/**
* Alter parser for MySQL.
*
* @author zhangliang
* @author panjuan
*/
public final class MySQLAlterParser extends AbstractAlterParser {
public final class MySQLAlterTableParser extends AbstractAlterTableParser {

public MySQLAlterParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
public MySQLAlterTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
super(shardingRule, lexerEngine);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.parser.dialect.mysql.sql;

import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.lexer.dialect.mysql.MySQLKeyword;
import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword;
import io.shardingsphere.core.parsing.lexer.token.Keyword;
import io.shardingsphere.core.parsing.parser.sql.ddl.create.index.AbstractCreateIndexParser;
import io.shardingsphere.core.rule.ShardingRule;

/**
* Create parser for MySQL.
*
* @author zhangliang
* @author panjuan
*/
public final class MySQLCreateIndexParser extends AbstractCreateIndexParser {

public MySQLCreateIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
super(shardingRule, lexerEngine);
}

@Override
protected Keyword[] getSkippedKeywordsBetweenCreateAndKeyword() {
return new Keyword[] {DefaultKeyword.TEMPORARY};
}

@Override
protected Keyword[] getSkippedKeywordsBetweenCreateIndexAndKeyword() {
return new Keyword[] {DefaultKeyword.UNIQUE, DefaultKeyword.FULLTEXT, MySQLKeyword.SPATIAL};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
import io.shardingsphere.core.parsing.lexer.dialect.mysql.MySQLKeyword;
import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword;
import io.shardingsphere.core.parsing.lexer.token.Keyword;
import io.shardingsphere.core.parsing.parser.sql.ddl.create.AbstractCreateParser;
import io.shardingsphere.core.parsing.parser.sql.ddl.create.table.AbstractCreateTableParser;
import io.shardingsphere.core.rule.ShardingRule;

/**
* Create parser for MySQL.
*
* @author zhangliang
* @author panjuan
*/
public final class MySQLCreateParser extends AbstractCreateParser {
public final class MySQLCreateTableParser extends AbstractCreateTableParser {

public MySQLCreateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
public MySQLCreateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
super(shardingRule, lexerEngine);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.parser.dialect.mysql.sql;

import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword;
import io.shardingsphere.core.parsing.lexer.token.Keyword;
import io.shardingsphere.core.parsing.parser.sql.ddl.drop.index.AbstractDropIndexParser;
import io.shardingsphere.core.rule.ShardingRule;

/**
* Drop parser for MySQL.
*
* @author zhangliang
* @author panjuan
*/
public final class MySQLDropIndexParser extends AbstractDropIndexParser {

public MySQLDropIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
super(shardingRule, lexerEngine);
}

@Override
protected Keyword[] getSkippedKeywordsBetweenDropAndTable() {
return new Keyword[] {DefaultKeyword.TEMPORARY};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword;
import io.shardingsphere.core.parsing.lexer.token.Keyword;
import io.shardingsphere.core.parsing.parser.sql.ddl.drop.AbstractDropParser;
import io.shardingsphere.core.parsing.parser.sql.ddl.drop.table.AbstractDropTableParser;
import io.shardingsphere.core.rule.ShardingRule;

/**
* Drop parser for MySQL.
*
* @author zhangliang
* @author panjuan
*/
public final class MySQLDropParser extends AbstractDropParser {
public final class MySQLDropTableParser extends AbstractDropTableParser {

public MySQLDropParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
public MySQLDropTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
super(shardingRule, lexerEngine);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
package io.shardingsphere.core.parsing.parser.dialect.mysql.sql;

import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.parser.sql.ddl.truncate.AbstractTruncateParser;
import io.shardingsphere.core.parsing.parser.sql.ddl.truncate.table.AbstractTruncateTableParser;
import io.shardingsphere.core.rule.ShardingRule;

/**
* Truncate parser for MySQL.
*
* @author zhangliang
* @author panjuan
*/
public final class MySQLTruncateParser extends AbstractTruncateParser {
public final class MySQLTruncateTableParser extends AbstractTruncateTableParser {

public MySQLTruncateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
public MySQLTruncateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
super(shardingRule, lexerEngine);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
package io.shardingsphere.core.parsing.parser.dialect.oracle.sql;

import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.parser.sql.ddl.alter.AbstractAlterParser;
import io.shardingsphere.core.parsing.parser.sql.ddl.alter.table.AbstractAlterTableParser;
import io.shardingsphere.core.rule.ShardingRule;

/**
* Alter parser for Oracle.
*
* @author zhangliang
* @author panjuan
*/
public final class OracleAlterParser extends AbstractAlterParser {
public final class OracleAlterTableParser extends AbstractAlterTableParser {

public OracleAlterParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
public OracleAlterTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
super(shardingRule, lexerEngine);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.parsing.parser.dialect.oracle.sql;

import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword;
import io.shardingsphere.core.parsing.lexer.token.Keyword;
import io.shardingsphere.core.parsing.parser.sql.ddl.create.index.AbstractCreateIndexParser;
import io.shardingsphere.core.rule.ShardingRule;

/**
* Create parser for Oracle.
*
* @author zhangliang
* @author panjuan
*/
public final class OracleCreateIndexParser extends AbstractCreateIndexParser {

public OracleCreateIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
super(shardingRule, lexerEngine);
}

@Override
protected Keyword[] getSkippedKeywordsBetweenCreateAndKeyword() {
return new Keyword[] {DefaultKeyword.GLOBAL, DefaultKeyword.TEMPORARY};
}

@Override
protected Keyword[] getSkippedKeywordsBetweenCreateIndexAndKeyword() {
return new Keyword[] {DefaultKeyword.UNIQUE, DefaultKeyword.BITMAP};
}
}
Loading

0 comments on commit c60f3bd

Please sign in to comment.