Skip to content

Commit

Permalink
for #540
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 16, 2018
1 parent 2b3e5b2 commit 0beb39f
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@

package io.shardingjdbc.core.parsing.lexer;

import com.google.common.collect.Sets;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.parsing.lexer.dialect.mysql.MySQLLexer;
import io.shardingjdbc.core.parsing.lexer.dialect.oracle.OracleLexer;
import io.shardingjdbc.core.parsing.lexer.dialect.postgresql.PostgreSQLLexer;
import io.shardingjdbc.core.parsing.lexer.dialect.sqlserver.SQLServerLexer;
import io.shardingjdbc.core.parsing.lexer.token.Assist;
import io.shardingjdbc.core.parsing.lexer.token.Symbol;
import io.shardingjdbc.core.parsing.lexer.token.Token;
import io.shardingjdbc.core.parsing.lexer.token.TokenType;
import io.shardingjdbc.core.parsing.parser.exception.SQLParsingException;
import io.shardingjdbc.core.parsing.parser.exception.SQLParsingUnsupportedException;
import io.shardingjdbc.core.parsing.parser.sql.SQLStatement;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;

import java.util.Set;
Expand Down Expand Up @@ -184,4 +189,25 @@ public void unsupportedIfNotSkip(final TokenType... tokenTypes) {
throw new SQLParsingUnsupportedException(lexer.getCurrentToken().getType());
}
}

/**
* Get database type.
*
* @return database type
*/
public DatabaseType getDatabaseType() {
if (lexer instanceof MySQLLexer) {
return DatabaseType.MySQL;
}
if (lexer instanceof OracleLexer) {
return DatabaseType.Oracle;
}
if (lexer instanceof SQLServerLexer) {
return DatabaseType.SQLServer;
}
if (lexer instanceof PostgreSQLLexer) {
return DatabaseType.PostgreSQL;
}
throw new UnsupportedOperationException(String.format("Cannot support lexer class: %s", lexer.getClass().getCanonicalName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.shardingjdbc.core.parsing.lexer.token.Literals;
import io.shardingjdbc.core.parsing.lexer.token.Symbol;
import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.lexer.token.TokenType;
import io.shardingjdbc.core.util.SQLUtil;
import com.google.common.base.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -14,7 +15,7 @@
* @author zhangliang
*/
@RequiredArgsConstructor
public final class AliasClauseParser implements SQLClauseParser {
public class AliasClauseParser implements SQLClauseParser {

private final LexerEngine lexerEngine;

Expand All @@ -32,15 +33,23 @@ public Optional<String> parse() {
lexerEngine.nextToken();
return Optional.of(result);
}
if (lexerEngine.equalAny(
Literals.IDENTIFIER, Literals.CHARS, DefaultKeyword.TABLESPACE, DefaultKeyword.FUNCTION, DefaultKeyword.SEQUENCE, DefaultKeyword.OF, DefaultKeyword.DO, DefaultKeyword.NO,
DefaultKeyword.TEMPORARY, DefaultKeyword.TEMP, DefaultKeyword.COMMENT, DefaultKeyword.AFTER, DefaultKeyword.INSTEAD, DefaultKeyword.ROW, DefaultKeyword.STATEMENT,
DefaultKeyword.EXECUTE, DefaultKeyword.BITMAP, DefaultKeyword.NOSORT, DefaultKeyword.REVERSE, DefaultKeyword.COMPILE, DefaultKeyword.PASSWORD,
DefaultKeyword.USER, DefaultKeyword.END, DefaultKeyword.CASE, DefaultKeyword.KEY, DefaultKeyword.INTERVAL, DefaultKeyword.CONSTRAINT)) {
if (lexerEngine.equalAny(getDefaultAvailableKeywordsForAlias()) || lexerEngine.equalAny(getCustomizedAvailableKeywordsForAlias())) {
String result = SQLUtil.getExactlyValue(lexerEngine.getCurrentToken().getLiterals());
lexerEngine.nextToken();
return Optional.of(result);
}
return Optional.absent();
}

private TokenType[] getDefaultAvailableKeywordsForAlias() {
return new TokenType[] {
Literals.IDENTIFIER, Literals.CHARS, DefaultKeyword.TABLESPACE, DefaultKeyword.FUNCTION, DefaultKeyword.SEQUENCE, DefaultKeyword.OF, DefaultKeyword.DO,
DefaultKeyword.NO, DefaultKeyword.TEMPORARY, DefaultKeyword.TEMP, DefaultKeyword.COMMENT, DefaultKeyword.AFTER, DefaultKeyword.INSTEAD, DefaultKeyword.ROW,
DefaultKeyword.STATEMENT, DefaultKeyword.EXECUTE, DefaultKeyword.BITMAP, DefaultKeyword.NOSORT, DefaultKeyword.REVERSE, DefaultKeyword.COMPILE,
DefaultKeyword.PASSWORD, DefaultKeyword.USER, DefaultKeyword.END, DefaultKeyword.CASE, DefaultKeyword.KEY, DefaultKeyword.INTERVAL, DefaultKeyword.CONSTRAINT, };
}

protected TokenType[] getCustomizedAvailableKeywordsForAlias() {
return new TokenType[0];
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.shardingjdbc.core.parsing.parser.clause;

import io.shardingjdbc.core.parsing.parser.dialect.AliasClauseParserFactory;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.constant.AggregationType;
import io.shardingjdbc.core.parsing.lexer.LexerEngine;
Expand Down Expand Up @@ -35,7 +36,7 @@ public class SelectListClauseParser implements SQLClauseParser {
public SelectListClauseParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
this.shardingRule = shardingRule;
this.lexerEngine = lexerEngine;
aliasClauseParser = new AliasClauseParser(lexerEngine);
aliasClauseParser = AliasClauseParserFactory.createInstance(lexerEngine);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.shardingjdbc.core.parsing.parser.clause;

import com.google.common.base.Strings;
import io.shardingjdbc.core.parsing.parser.dialect.AliasClauseParserFactory;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class TableReferencesClauseParser implements SQLClauseParser {
public TableReferencesClauseParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) {
this.shardingRule = shardingRule;
this.lexerEngine = lexerEngine;
aliasClauseParser = new AliasClauseParser(lexerEngine);
aliasClauseParser = AliasClauseParserFactory.createInstance(lexerEngine);
expressionClauseParser = new ExpressionClauseParser(lexerEngine);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.shardingjdbc.core.parsing.parser.clause;

import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.parsing.parser.dialect.AliasClauseParserFactory;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword;
Expand Down Expand Up @@ -48,7 +49,7 @@ public class WhereClauseParser implements SQLClauseParser {
public WhereClauseParser(final DatabaseType databaseType, final LexerEngine lexerEngine) {
this.databaseType = databaseType;
this.lexerEngine = lexerEngine;
aliasClauseParser = new AliasClauseParser(lexerEngine);
aliasClauseParser = AliasClauseParserFactory.createInstance(lexerEngine);
expressionClauseParser = new ExpressionClauseParser(lexerEngine);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.shardingjdbc.core.parsing.parser.dialect;

import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.parser.clause.AliasClauseParser;
import io.shardingjdbc.core.parsing.parser.dialect.mysql.clause.MySQLAliasClauseParser;
import io.shardingjdbc.core.parsing.parser.dialect.oracle.clause.OracleAliasClauseParser;
import io.shardingjdbc.core.parsing.parser.dialect.postgresql.clause.PostgreSQLAliasClauseParser;
import io.shardingjdbc.core.parsing.parser.dialect.sqlserver.clause.SQLServerAliasClauseParser;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
* Alias clause parser factory.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AliasClauseParserFactory {

/**
* Create alias clause parser instance.
*
* @param lexerEngine lexical analysis engine.
* @return alias clause parser instance
*/
public static AliasClauseParser createInstance(final LexerEngine lexerEngine) {
switch (lexerEngine.getDatabaseType()) {
case MySQL:
return new MySQLAliasClauseParser(lexerEngine);
case Oracle:
return new OracleAliasClauseParser(lexerEngine);
case SQLServer:
return new SQLServerAliasClauseParser(lexerEngine);
case PostgreSQL:
return new PostgreSQLAliasClauseParser(lexerEngine);
default:
throw new UnsupportedOperationException(String.format("Cannot support database type: %s", lexerEngine.getDatabaseType()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.shardingjdbc.core.parsing.parser.dialect.mysql.clause;

import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.lexer.token.TokenType;
import io.shardingjdbc.core.parsing.parser.clause.AliasClauseParser;

/**
* Alias clause parser for MySQL.
*
* @author zhangliang
*/
public final class MySQLAliasClauseParser extends AliasClauseParser {

public MySQLAliasClauseParser(final LexerEngine lexerEngine) {
super(lexerEngine);
}

@Override
protected TokenType[] getCustomizedAvailableKeywordsForAlias() {
return new TokenType[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.shardingjdbc.core.parsing.parser.dialect.oracle.clause;

import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.lexer.token.TokenType;
import io.shardingjdbc.core.parsing.parser.clause.AliasClauseParser;

/**
* Alias clause parser for Oracle.
*
* @author zhangliang
*/
public final class OracleAliasClauseParser extends AliasClauseParser {

public OracleAliasClauseParser(final LexerEngine lexerEngine) {
super(lexerEngine);
}

@Override
protected TokenType[] getCustomizedAvailableKeywordsForAlias() {
return new TokenType[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.shardingjdbc.core.parsing.parser.dialect.postgresql.clause;

import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.lexer.token.TokenType;
import io.shardingjdbc.core.parsing.parser.clause.AliasClauseParser;

/**
* Alias clause parser for PostgreSQL.
*
* @author zhangliang
*/
public final class PostgreSQLAliasClauseParser extends AliasClauseParser {

public PostgreSQLAliasClauseParser(final LexerEngine lexerEngine) {
super(lexerEngine);
}

@Override
protected TokenType[] getCustomizedAvailableKeywordsForAlias() {
return new TokenType[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.shardingjdbc.core.parsing.parser.dialect.sqlserver.clause;

import io.shardingjdbc.core.parsing.lexer.LexerEngine;
import io.shardingjdbc.core.parsing.lexer.token.TokenType;
import io.shardingjdbc.core.parsing.parser.clause.AliasClauseParser;

/**
* Alias clause parser for SQLServer.
*
* @author zhangliang
*/
public final class SQLServerAliasClauseParser extends AliasClauseParser {

public SQLServerAliasClauseParser(final LexerEngine lexerEngine) {
super(lexerEngine);
}

@Override
protected TokenType[] getCustomizedAvailableKeywordsForAlias() {
return new TokenType[0];
}
}

0 comments on commit 0beb39f

Please sign in to comment.