Skip to content

Commit

Permalink
for apache#2082, Add SQLParserEntry.getDatabaseTypeAliases()
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 31, 2019
1 parent 275956f commit c475f3f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,25 @@ public final class SQLParserFactory {
*/
public static SQLParser newInstance(final DatabaseType databaseType, final String sql) {
for (SQLParserEntry each : NewInstanceServiceLoader.newServiceInstances(SQLParserEntry.class)) {
if (DatabaseType.valueOf(each.getDatabaseType()) == databaseType) {
if (isCurrentDatabaseType(databaseType, each)) {
return createSQLParser(sql, each);
}
}
throw new UnsupportedOperationException(String.format("Cannot support database type '%s'", databaseType));
}

private static boolean isCurrentDatabaseType(final DatabaseType databaseType, final SQLParserEntry sqlParserEntry) {
if (DatabaseType.valueOf(sqlParserEntry.getDatabaseType()) == databaseType) {
return true;
}
for (String each : sqlParserEntry.getDatabaseTypeAliases()) {
if (DatabaseType.valueOf(each) == databaseType) {
return true;
}
}
return false;
}

@SneakyThrows
private static SQLParser createSQLParser(final String sql, final SQLParserEntry parserEntry) {
Lexer lexer = parserEntry.getLexerClass().getConstructor(CharStream.class).newInstance(CharStreams.fromString(sql));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.apache.shardingsphere.core.parse.autogen.MySQLStatementLexer;
import org.apache.shardingsphere.core.parse.spi.SQLParserEntry;

import java.util.Collection;
import java.util.Collections;

/**
* SQL parser entry for MySQL.
*
Expand All @@ -34,6 +37,11 @@ public String getDatabaseType() {
return "MySQL";
}

@Override
public Collection<String> getDatabaseTypeAliases() {
return Collections.singletonList("H2");
}

@Override
public Class<? extends Lexer> getLexerClass() {
return MySQLStatementLexer.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@
#

org.apache.shardingsphere.core.parse.MySQLParserEntry
org.apache.shardingsphere.core.parse.H2ParserEntry
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.apache.shardingsphere.core.parse.autogen.OracleStatementLexer;
import org.apache.shardingsphere.core.parse.spi.SQLParserEntry;

import java.util.Collection;
import java.util.Collections;

/**
* SQL parser entry for Oracle.
*
Expand All @@ -34,6 +37,11 @@ public String getDatabaseType() {
return "Oracle";
}

@Override
public Collection<String> getDatabaseTypeAliases() {
return Collections.emptyList();
}

@Override
public Class<? extends Lexer> getLexerClass() {
return OracleStatementLexer.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.apache.shardingsphere.core.parse.autogen.PostgreSQLStatementLexer;
import org.apache.shardingsphere.core.parse.spi.SQLParserEntry;

import java.util.Collection;
import java.util.Collections;

/**
* SQL parser entry for PostgreSQL.
*
Expand All @@ -34,6 +37,11 @@ public String getDatabaseType() {
return "PostgreSQL";
}

@Override
public Collection<String> getDatabaseTypeAliases() {
return Collections.emptyList();
}

@Override
public Class<? extends Lexer> getLexerClass() {
return PostgreSQLStatementLexer.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.antlr.v4.runtime.Lexer;
import org.apache.shardingsphere.core.parse.api.SQLParser;

import java.util.Collection;

/**
* SQL parser entry.
*
Expand All @@ -34,6 +36,13 @@ public interface SQLParserEntry {
*/
String getDatabaseType();

/**
* Get database type alias.
*
* @return database type alias
*/
Collection<String> getDatabaseTypeAliases();

/**
* Get SQL lexer class type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.apache.shardingsphere.core.parse.autogen.SQLServerStatementLexer;
import org.apache.shardingsphere.core.parse.spi.SQLParserEntry;

import java.util.Collection;
import java.util.Collections;

/**
* SQL parser entry for SQLServer.
*
Expand All @@ -34,6 +37,11 @@ public String getDatabaseType() {
return "SQLServer";
}

@Override
public Collection<String> getDatabaseTypeAliases() {
return Collections.emptyList();
}

@Override
public Class<? extends Lexer> getLexerClass() {
return SQLServerStatementLexer.class;
Expand Down

0 comments on commit c475f3f

Please sign in to comment.