diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/clause/TableReferencesClauseParser.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/clause/TableReferencesClauseParser.java index f2fc897baf097..2053f17a63dac 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/clause/TableReferencesClauseParser.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/clause/TableReferencesClauseParser.java @@ -120,4 +120,16 @@ private void parseJoinCondition(final SQLStatement sqlStatement) { lexerEngine.skipParentheses(sqlStatement); } } + + /** + * Parse single table without alias. + * + * @param sqlStatement SQL statement + */ + public final void parseSingleTableWithoutAlias(final SQLStatement sqlStatement) { + int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length(); + sqlStatement.getSqlTokens().add(new TableToken(beginPosition, lexerEngine.getCurrentToken().getLiterals())); + sqlStatement.getTables().add(new Table(lexerEngine.getCurrentToken().getLiterals(), Optional.absent())); + lexerEngine.nextToken(); + } } diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/alter/AbstractAlterParser.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/alter/AbstractAlterParser.java index 8c3d2b32565ee..a7bb1a24d4b33 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/alter/AbstractAlterParser.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/alter/AbstractAlterParser.java @@ -53,7 +53,7 @@ public DDLStatement parse() { lexerEngine.unsupportedIfNotSkip(DefaultKeyword.TABLE); lexerEngine.skipAll(getSkippedKeywordsBetweenAlterTableAndTableName()); DDLStatement result = new DDLStatement(); - tableReferencesClauseParser.parse(result, true); + tableReferencesClauseParser.parseSingleTableWithoutAlias(result); return result; } diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/create/AbstractCreateParser.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/create/AbstractCreateParser.java index 01dec14cbbf2f..7257924bb17c3 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/create/AbstractCreateParser.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/create/AbstractCreateParser.java @@ -64,7 +64,7 @@ public DDLStatement parse() { } else { throw new SQLParsingException("Can't support other CREATE grammar unless CREATE TABLE, CREATE INDEX."); } - tableReferencesClauseParser.parse(result, true); + tableReferencesClauseParser.parseSingleTableWithoutAlias(result); return result; } diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/drop/AbstractDropParser.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/drop/AbstractDropParser.java index d3aa9c4c7c7f6..eaa40035792c1 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/drop/AbstractDropParser.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/drop/AbstractDropParser.java @@ -60,10 +60,10 @@ public DDLStatement parse() { parseIndex(result); } else if (lexerEngine.skipIfEqual(DefaultKeyword.TABLE)) { lexerEngine.skipAll(getSkippedKeywordsBetweenDropTableAndTableName()); + tableReferencesClauseParser.parseSingleTableWithoutAlias(result); } else { throw new SQLParsingException("Can't support other DROP grammar unless DROP TABLE, DROP INDEX."); } - tableReferencesClauseParser.parse(result, true); return result; } @@ -80,9 +80,12 @@ private void parseIndex(final DDLStatement ddlStatement) { int beginPosition = currentToken.getEndPosition() - currentToken.getLiterals().length(); String literals = currentToken.getLiterals(); lexerEngine.skipUntil(DefaultKeyword.ON); - lexerEngine.nextToken(); - String tableName = lexerEngine.getCurrentToken().getLiterals(); - ddlStatement.getSqlTokens().add(new IndexToken(beginPosition, literals, tableName)); + if (lexerEngine.skipIfEqual(DefaultKeyword.ON)) { + tableReferencesClauseParser.parseSingleTableWithoutAlias(ddlStatement); + ddlStatement.getSqlTokens().add(new IndexToken(beginPosition, literals, ddlStatement.getTables().getSingleTableName())); + } else { + ddlStatement.getSqlTokens().add(new IndexToken(beginPosition, literals, "")); + } } protected abstract Keyword[] getSkippedKeywordsBetweenDropTableAndTableName(); diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/truncate/AbstractTruncateParser.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/truncate/AbstractTruncateParser.java index c5f824930d6b4..0ad8c2cc7edac 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/truncate/AbstractTruncateParser.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/parsing/parser/sql/ddl/truncate/AbstractTruncateParser.java @@ -53,7 +53,7 @@ public DDLStatement parse() { lexerEngine.skipIfEqual(DefaultKeyword.TABLE); lexerEngine.skipAll(getSkippedKeywordsBetweenTruncateTableAndTableName()); DDLStatement result = new DDLStatement(); - tableReferencesClauseParser.parse(result, true); + tableReferencesClauseParser.parseSingleTableWithoutAlias(result); return result; }