diff --git a/sharding-core/src/main/java/io/shardingsphere/core/constant/SQLType.java b/sharding-core/src/main/java/io/shardingsphere/core/constant/SQLType.java index 823d5cbf6fdf5..e06807bd9cc87 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/constant/SQLType.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/constant/SQLType.java @@ -55,5 +55,10 @@ public enum SQLType { /** * Database administrator Language. */ - DAL + DAL, + + /** + * Database control Language. + */ + DCL } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/merger/dal/show/ShowTablesMergedResult.java b/sharding-core/src/main/java/io/shardingsphere/core/merger/dal/show/ShowTablesMergedResult.java index 4fa86199ee403..8eaac74f54b78 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/merger/dal/show/ShowTablesMergedResult.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/merger/dal/show/ShowTablesMergedResult.java @@ -72,9 +72,9 @@ private Iterator init(final List queryResults String actualTableName = memoryResultSetRow.getCell(1).toString(); Optional 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())) { diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/SQLJudgeEngine.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/SQLJudgeEngine.java index a75ac183709c3..db4678106cfc9 100755 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/SQLJudgeEngine.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/SQLJudgeEngine.java @@ -33,6 +33,7 @@ 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; @@ -40,10 +41,14 @@ 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 { @@ -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(); } } @@ -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 primaryTokens = Arrays.asList(DefaultKeyword.CREATE, DefaultKeyword.ALTER, DefaultKeyword.DROP, DefaultKeyword.TRUNCATE); + Collection 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 primaryTokens = Arrays.asList(DefaultKeyword.GRANT, DefaultKeyword.REVOKE, DefaultKeyword.DENY); + Collection 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) { @@ -120,6 +142,10 @@ private SQLStatement getDDLStatement() { return new DDLStatement(); } + private SQLStatement getDCLStatement() { + return new DCLStatement(); + } + private SQLStatement getTCLStatement() { return new TCLStatement(); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/LexerEngine.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/LexerEngine.java index a883fc7dbd101..6c2b24212446c 100755 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/LexerEngine.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/LexerEngine.java @@ -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. * diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/token/DefaultKeyword.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/token/DefaultKeyword.java index c444e3426dd4e..47efd7a34b8a6 100755 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/token/DefaultKeyword.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/token/DefaultKeyword.java @@ -86,6 +86,9 @@ public enum DefaultKeyword implements Keyword { DISABLE, VALIDATE, USER, + ROLE, + LOGIN, + DENY, IDENTIFIED, /* diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLAlterParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLAlterTableParser.java similarity index 76% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLAlterParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLAlterTableParser.java index b9761d3f593fd..e9d94731ce5cf 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLAlterParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLAlterTableParser.java @@ -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); } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLCreateIndexParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLCreateIndexParser.java new file mode 100644 index 0000000000000..1e100d632197b --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLCreateIndexParser.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +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}; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLCreateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLCreateTableParser.java similarity index 85% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLCreateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLCreateTableParser.java index 1de830addfea7..1ed5e513ee4a1 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLCreateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLCreateTableParser.java @@ -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); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLDropIndexParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLDropIndexParser.java new file mode 100644 index 0000000000000..3e8971c16fe23 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLDropIndexParser.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +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}; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLDropParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLDropTableParser.java similarity index 82% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLDropParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLDropTableParser.java index 2b8687e2917c7..1df1f6f1cff6d 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLDropParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLDropTableParser.java @@ -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); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLTruncateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLTruncateTableParser.java similarity index 75% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLTruncateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLTruncateTableParser.java index f644309f05b95..0a8ce40e58fa7 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLTruncateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLTruncateTableParser.java @@ -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); } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleAlterParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleAlterTableParser.java similarity index 76% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleAlterParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleAlterTableParser.java index c7fc6e2312db5..e80663d0d835b 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleAlterParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleAlterTableParser.java @@ -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); } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleCreateIndexParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleCreateIndexParser.java new file mode 100644 index 0000000000000..7ccfc07e781f4 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleCreateIndexParser.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +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}; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleCreateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleCreateTableParser.java similarity index 83% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleCreateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleCreateTableParser.java index 5da430af804cd..0985718f234cb 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleCreateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleCreateTableParser.java @@ -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.create.AbstractCreateParser; +import io.shardingsphere.core.parsing.parser.sql.ddl.create.table.AbstractCreateTableParser; import io.shardingsphere.core.rule.ShardingRule; /** * Create parser for Oracle. * * @author zhangliang + * @author panjuan */ -public final class OracleCreateParser extends AbstractCreateParser { +public final class OracleCreateTableParser extends AbstractCreateTableParser { - public OracleCreateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public OracleCreateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleDropIndexParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleDropIndexParser.java new file mode 100644 index 0000000000000..42a2f36e15d6c --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleDropIndexParser.java @@ -0,0 +1,35 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.dialect.oracle.sql; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.parser.sql.ddl.drop.index.AbstractDropIndexParser; +import io.shardingsphere.core.rule.ShardingRule; + +/** + * Drop parser for Oracle. + * + * @author zhangliang + * @author panjuan + */ +public final class OracleDropIndexParser extends AbstractDropIndexParser { + + public OracleDropIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + super(shardingRule, lexerEngine); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleDropParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleDropTableParser.java similarity index 79% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleDropParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleDropTableParser.java index b6d3f0aeb902f..08fb1c4caafa6 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleDropParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleDropTableParser.java @@ -19,17 +19,18 @@ import io.shardingsphere.core.parsing.lexer.LexerEngine; 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 Oracle. * * @author zhangliang + * @author panjuan */ -public final class OracleDropParser extends AbstractDropParser { +public final class OracleDropTableParser extends AbstractDropTableParser { - public OracleDropParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public OracleDropTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleTruncateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleTruncateTableParser.java similarity index 75% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleTruncateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleTruncateTableParser.java index 7dc24dd60c490..00ffacb34d5f7 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleTruncateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/oracle/sql/OracleTruncateTableParser.java @@ -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.truncate.AbstractTruncateParser; +import io.shardingsphere.core.parsing.parser.sql.ddl.truncate.table.AbstractTruncateTableParser; import io.shardingsphere.core.rule.ShardingRule; /** * Truncate parser for Oracle. * * @author zhangliang + * @author panjuan */ -public final class OracleTruncateParser extends AbstractTruncateParser { +public final class OracleTruncateTableParser extends AbstractTruncateTableParser { - public OracleTruncateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public OracleTruncateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLAlterParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLAlterTableParser.java similarity index 81% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLAlterParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLAlterTableParser.java index 616a7d06599d4..d92ead70b8bc8 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLAlterParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLAlterTableParser.java @@ -21,17 +21,18 @@ import io.shardingsphere.core.parsing.lexer.dialect.postgresql.PostgreSQLKeyword; import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; import io.shardingsphere.core.parsing.lexer.token.Keyword; -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 PostgreSQL. * * @author zhangliang + * @author panjuan */ -public final class PostgreSQLAlterParser extends AbstractAlterParser { +public final class PostgreSQLAlterTableParser extends AbstractAlterTableParser { - public PostgreSQLAlterParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public PostgreSQLAlterTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLCreateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLCreateIndexParser.java similarity index 79% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLCreateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLCreateIndexParser.java index 83837096449fb..81ec1df47e8ab 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLCreateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLCreateIndexParser.java @@ -21,17 +21,18 @@ import io.shardingsphere.core.parsing.lexer.dialect.postgresql.PostgreSQLKeyword; 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.index.AbstractCreateIndexParser; import io.shardingsphere.core.rule.ShardingRule; /** * Create parser for PostgreSQL. * * @author zhangliang + * @author panjuan */ -public final class PostgreSQLCreateParser extends AbstractCreateParser { +public final class PostgreSQLCreateIndexParser extends AbstractCreateIndexParser { - public PostgreSQLCreateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public PostgreSQLCreateIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } @@ -45,11 +46,6 @@ protected Keyword[] getSkippedKeywordsBetweenCreateAndKeyword() { return new Keyword[] {DefaultKeyword.GLOBAL, DefaultKeyword.LOCAL, DefaultKeyword.TEMPORARY, DefaultKeyword.TEMP, PostgreSQLKeyword.UNLOGGED}; } - @Override - protected Keyword[] getSkippedKeywordsBetweenCreateTableAndTableName() { - return new Keyword[] {DefaultKeyword.IF, DefaultKeyword.NOT, DefaultKeyword.EXISTS}; - } - @Override protected Keyword[] getSkippedKeywordsBetweenCreateIndexAndKeyword() { return new Keyword[] {DefaultKeyword.UNIQUE}; diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLCreateTableParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLCreateTableParser.java new file mode 100644 index 0000000000000..700a6e484d4f7 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLCreateTableParser.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.dialect.postgresql.sql; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.lexer.dialect.postgresql.PostgreSQLKeyword; +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.table.AbstractCreateTableParser; +import io.shardingsphere.core.rule.ShardingRule; + +/** + * Create parser for PostgreSQL. + * + * @author zhangliang + * @author panjuan + */ +public final class PostgreSQLCreateTableParser extends AbstractCreateTableParser { + + public PostgreSQLCreateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + super(shardingRule, lexerEngine); + } + + @Override + protected Keyword[] getSkippedKeywordsBetweenCreateAndKeyword() { + return new Keyword[] {DefaultKeyword.GLOBAL, DefaultKeyword.LOCAL, DefaultKeyword.TEMPORARY, DefaultKeyword.TEMP, PostgreSQLKeyword.UNLOGGED}; + } + + @Override + protected Keyword[] getSkippedKeywordsBetweenCreateTableAndTableName() { + return new Keyword[] {DefaultKeyword.IF, DefaultKeyword.NOT, DefaultKeyword.EXISTS}; + } + + @Override + protected Keyword[] getSkippedKeywordsBetweenCreateIndexAndKeyword() { + return new Keyword[] {DefaultKeyword.UNIQUE}; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLDropParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLDropIndexParser.java similarity index 75% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLDropParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLDropIndexParser.java index db1404746948a..489dcca85c099 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLDropParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLDropIndexParser.java @@ -20,26 +20,23 @@ 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.index.AbstractDropIndexParser; import io.shardingsphere.core.rule.ShardingRule; /** * Drop parser for PostgreSQL. * * @author zhangliang + * @author panjuan */ -public final class PostgreSQLDropParser extends AbstractDropParser { +public final class PostgreSQLDropIndexParser extends AbstractDropIndexParser { - public PostgreSQLDropParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public PostgreSQLDropIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } - protected Keyword[] getSkippedKeywordsBetweenDropIndexAndIndexName() { - return new Keyword[] {DefaultKeyword.IF, DefaultKeyword.EXISTS}; - } - @Override - protected Keyword[] getSkippedKeywordsBetweenDropTableAndTableName() { + protected Keyword[] getSkippedKeywordsBetweenDropIndexAndIndexName() { return new Keyword[] {DefaultKeyword.IF, DefaultKeyword.EXISTS}; } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLDropTableParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLDropTableParser.java new file mode 100644 index 0000000000000..984cc84317f00 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLDropTableParser.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.dialect.postgresql.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.table.AbstractDropTableParser; +import io.shardingsphere.core.rule.ShardingRule; + +/** + * Drop parser for PostgreSQL. + * + * @author zhangliang + * @author panjuan + */ +public final class PostgreSQLDropTableParser extends AbstractDropTableParser { + + public PostgreSQLDropTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + super(shardingRule, lexerEngine); + } + + @Override + protected Keyword[] getSkippedKeywordsBetweenDropTableAndTableName() { + return new Keyword[] {DefaultKeyword.IF, DefaultKeyword.EXISTS}; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLTruncateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLTruncateTableParser.java similarity index 79% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLTruncateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLTruncateTableParser.java index e134ff4a5badc..60059308978e3 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLTruncateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/sql/PostgreSQLTruncateTableParser.java @@ -20,17 +20,18 @@ import io.shardingsphere.core.parsing.lexer.LexerEngine; import io.shardingsphere.core.parsing.lexer.dialect.postgresql.PostgreSQLKeyword; import io.shardingsphere.core.parsing.lexer.token.Keyword; -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 PostgreSQL. * * @author zhangliang + * @author panjuan */ -public final class PostgreSQLTruncateParser extends AbstractTruncateParser { +public final class PostgreSQLTruncateTableParser extends AbstractTruncateTableParser { - public PostgreSQLTruncateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public PostgreSQLTruncateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerAlterParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerAlterTableParser.java similarity index 75% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerAlterParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerAlterTableParser.java index 87e205332d454..961b0d4227395 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerAlterParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerAlterTableParser.java @@ -18,17 +18,18 @@ package io.shardingsphere.core.parsing.parser.dialect.sqlserver.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 SQLServer. * * @author zhangliang + * @author panjuan */ -public final class SQLServerAlterParser extends AbstractAlterParser { +public final class SQLServerAlterTableParser extends AbstractAlterTableParser { - public SQLServerAlterParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public SQLServerAlterTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerCreateIndexParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerCreateIndexParser.java new file mode 100644 index 0000000000000..a6074dad18392 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerCreateIndexParser.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.dialect.sqlserver.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 SQLServer. + * + * @author zhangliang + * @author panjuan + */ +public final class SQLServerCreateIndexParser extends AbstractCreateIndexParser { + + public SQLServerCreateIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + super(shardingRule, lexerEngine); + } + + @Override + protected Keyword[] getSkippedKeywordsBetweenCreateAndKeyword() { + return new Keyword[] {}; + } + + @Override + protected Keyword[] getSkippedKeywordsBetweenCreateIndexAndKeyword() { + return new Keyword[] {DefaultKeyword.UNIQUE, DefaultKeyword.FULLTEXT}; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerCreateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerCreateTableParser.java similarity index 83% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerCreateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerCreateTableParser.java index aad87117561ab..d65ccdb351176 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerCreateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerCreateTableParser.java @@ -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.create.AbstractCreateParser; +import io.shardingsphere.core.parsing.parser.sql.ddl.create.table.AbstractCreateTableParser; import io.shardingsphere.core.rule.ShardingRule; /** * Create parser for SQLServer. * * @author zhangliang + * @author panjuan */ -public final class SQLServerCreateParser extends AbstractCreateParser { +public final class SQLServerCreateTableParser extends AbstractCreateTableParser { - public SQLServerCreateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public SQLServerCreateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerDropIndexParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerDropIndexParser.java new file mode 100644 index 0000000000000..75c79fa735869 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerDropIndexParser.java @@ -0,0 +1,35 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.parser.sql.ddl.drop.index.AbstractDropIndexParser; +import io.shardingsphere.core.rule.ShardingRule; + +/** + * Drop parser for SQLServer. + * + * @author zhangliang + * @author panjuan + */ +public final class SQLServerDropIndexParser extends AbstractDropIndexParser { + + public SQLServerDropIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + super(shardingRule, lexerEngine); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerDropParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerDropTableParser.java similarity index 80% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerDropParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerDropTableParser.java index 760b8cc5d72e0..aa64aacfcf4ee 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerDropParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerDropTableParser.java @@ -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 SQLServer. * * @author zhangliang + * @author panjuan */ -public final class SQLServerDropParser extends AbstractDropParser { +public final class SQLServerDropTableParser extends AbstractDropTableParser { - public SQLServerDropParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public SQLServerDropTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerTruncateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerTruncateTableParser.java similarity index 74% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerTruncateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerTruncateTableParser.java index 0f42958b48bc7..8f4a1a93db1c8 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerTruncateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/sqlserver/sql/SQLServerTruncateTableParser.java @@ -18,17 +18,18 @@ package io.shardingsphere.core.parsing.parser.dialect.sqlserver.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 SQLServer. * * @author zhangliang + * @author panjuan */ -public final class SQLServerTruncateParser extends AbstractTruncateParser { +public final class SQLServerTruncateTableParser extends AbstractTruncateTableParser { - public SQLServerTruncateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public SQLServerTruncateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { super(shardingRule, lexerEngine); } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/SQLParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/SQLParserFactory.java index a7642c4b07f65..758524a4fe265 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/SQLParserFactory.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/SQLParserFactory.java @@ -28,10 +28,19 @@ import io.shardingsphere.core.parsing.parser.sql.dal.describe.DescribeParserFactory; import io.shardingsphere.core.parsing.parser.sql.dal.show.ShowParserFactory; import io.shardingsphere.core.parsing.parser.sql.dal.use.UseParserFactory; -import io.shardingsphere.core.parsing.parser.sql.ddl.alter.AlterParserFactory; -import io.shardingsphere.core.parsing.parser.sql.ddl.create.CreateParserFactory; -import io.shardingsphere.core.parsing.parser.sql.ddl.drop.DropParserFactory; -import io.shardingsphere.core.parsing.parser.sql.ddl.truncate.TruncateParserFactory; +import io.shardingsphere.core.parsing.parser.sql.dcl.alter.AlterUserParserFactory; +import io.shardingsphere.core.parsing.parser.sql.dcl.create.CreateUserParserFactory; +import io.shardingsphere.core.parsing.parser.sql.dcl.deny.DenyUserParserFactory; +import io.shardingsphere.core.parsing.parser.sql.dcl.drop.DropUserParserFactory; +import io.shardingsphere.core.parsing.parser.sql.dcl.grant.GrantUserParserFactory; +import io.shardingsphere.core.parsing.parser.sql.dcl.rename.RenameUserParserFactory; +import io.shardingsphere.core.parsing.parser.sql.dcl.revoke.RevokeUserParserFactory; +import io.shardingsphere.core.parsing.parser.sql.ddl.alter.table.AlterTableParserFactory; +import io.shardingsphere.core.parsing.parser.sql.ddl.create.index.CreateIndexParserFactory; +import io.shardingsphere.core.parsing.parser.sql.ddl.create.table.CreateTableParserFactory; +import io.shardingsphere.core.parsing.parser.sql.ddl.drop.index.DropIndexParserFactory; +import io.shardingsphere.core.parsing.parser.sql.ddl.drop.table.DropTableParserFactory; +import io.shardingsphere.core.parsing.parser.sql.ddl.truncate.table.TruncateTableParserFactory; import io.shardingsphere.core.parsing.parser.sql.dml.delete.DeleteParserFactory; import io.shardingsphere.core.parsing.parser.sql.dml.insert.InsertParserFactory; import io.shardingsphere.core.parsing.parser.sql.dml.update.UpdateParserFactory; @@ -41,6 +50,9 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import java.util.Arrays; +import java.util.Collection; + /** * SQL parser factory. * @@ -67,15 +79,19 @@ public static SQLParser newInstance(final DatabaseType dbType, final TokenType t if (isDML(tokenType)) { return getDMLParser(dbType, tokenType, shardingRule, lexerEngine, shardingMetaData); } - if (isDDL(tokenType)) { - return getDDLParser(dbType, tokenType, shardingRule, lexerEngine); - } if (isTCL(tokenType)) { return getTCLParser(dbType, shardingRule, lexerEngine); } if (isDAL(tokenType)) { return getDALParser(dbType, (Keyword) tokenType, shardingRule, lexerEngine); } + lexerEngine.nextToken(); + if (isDCL(tokenType, lexerEngine)) { + return getDCLParser(dbType, tokenType, shardingRule, lexerEngine); + } + if (isDDL(tokenType, lexerEngine)) { + return getDDLParser(dbType, tokenType, shardingRule, lexerEngine); + } throw new SQLParsingUnsupportedException(tokenType); } @@ -87,10 +103,6 @@ private static boolean isDML(final TokenType tokenType) { return DefaultKeyword.INSERT == tokenType || DefaultKeyword.UPDATE == tokenType || DefaultKeyword.DELETE == tokenType; } - private static boolean isDDL(final TokenType tokenType) { - return DefaultKeyword.CREATE == tokenType || DefaultKeyword.ALTER == tokenType || DefaultKeyword.DROP == tokenType || DefaultKeyword.TRUNCATE == tokenType; - } - private static boolean isTCL(final TokenType tokenType) { return DefaultKeyword.SET == tokenType || DefaultKeyword.COMMIT == tokenType || DefaultKeyword.ROLLBACK == tokenType || DefaultKeyword.SAVEPOINT == tokenType || DefaultKeyword.BEGIN == tokenType; @@ -100,6 +112,22 @@ private static boolean isDAL(final TokenType tokenType) { return DefaultKeyword.USE == tokenType || DefaultKeyword.DESC == tokenType || MySQLKeyword.DESCRIBE == tokenType || MySQLKeyword.SHOW == tokenType; } + private static boolean isDDL(final TokenType tokenType, final LexerEngine lexerEngine) { + Collection primaryTokens = Arrays.asList(DefaultKeyword.CREATE, DefaultKeyword.ALTER, DefaultKeyword.DROP, DefaultKeyword.TRUNCATE); + Collection 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 primaryTokens = Arrays.asList(DefaultKeyword.GRANT, DefaultKeyword.REVOKE, DefaultKeyword.DENY); + Collection 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 static SQLParser getDQLParser(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingMetaData shardingMetaData) { return SelectParserFactory.newInstance(dbType, shardingRule, lexerEngine, shardingMetaData); } @@ -119,15 +147,35 @@ private static SQLParser getDMLParser( } private static SQLParser getDDLParser(final DatabaseType dbType, final TokenType tokenType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + lexerEngine.skipUntil(DefaultKeyword.INDEX, DefaultKeyword.TABLE); + if (lexerEngine.isEnd()) { + throw new SQLParsingUnsupportedException(tokenType); + } + return DefaultKeyword.TABLE == lexerEngine.getCurrentToken().getType() ? getTableDDLParser(dbType, tokenType, shardingRule, lexerEngine) + : getIndexDDLParser(dbType, tokenType, shardingRule, lexerEngine); + } + + private static SQLParser getTableDDLParser(final DatabaseType dbType, final TokenType tokenType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { switch ((DefaultKeyword) tokenType) { case CREATE: - return CreateParserFactory.newInstance(dbType, shardingRule, lexerEngine); + return CreateTableParserFactory.newInstance(dbType, shardingRule, lexerEngine); case ALTER: - return AlterParserFactory.newInstance(dbType, shardingRule, lexerEngine); + return AlterTableParserFactory.newInstance(dbType, shardingRule, lexerEngine); case DROP: - return DropParserFactory.newInstance(dbType, shardingRule, lexerEngine); + return DropTableParserFactory.newInstance(dbType, shardingRule, lexerEngine); case TRUNCATE: - return TruncateParserFactory.newInstance(dbType, shardingRule, lexerEngine); + return TruncateTableParserFactory.newInstance(dbType, shardingRule, lexerEngine); + default: + throw new SQLParsingUnsupportedException(tokenType); + } + } + + private static SQLParser getIndexDDLParser(final DatabaseType dbType, final TokenType tokenType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + switch ((DefaultKeyword) tokenType) { + case CREATE: + return CreateIndexParserFactory.newInstance(dbType, shardingRule, lexerEngine); + case DROP: + return DropIndexParserFactory.newInstance(dbType, shardingRule, lexerEngine); default: throw new SQLParsingUnsupportedException(tokenType); } @@ -149,4 +197,25 @@ private static SQLParser getDALParser(final DatabaseType dbType, final Keyword t } throw new SQLParsingUnsupportedException(tokenType); } + + private static SQLParser getDCLParser(final DatabaseType dbType, final TokenType tokenType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + switch ((DefaultKeyword) tokenType) { + case CREATE: + return CreateUserParserFactory.newInstance(dbType, shardingRule, lexerEngine); + case ALTER: + return AlterUserParserFactory.newInstance(dbType, shardingRule, lexerEngine); + case DROP: + return DropUserParserFactory.newInstance(dbType, shardingRule, lexerEngine); + case RENAME: + return RenameUserParserFactory.newInstance(dbType, shardingRule, lexerEngine); + case GRANT: + return GrantUserParserFactory.newInstance(dbType, shardingRule, lexerEngine); + case REVOKE: + return RevokeUserParserFactory.newInstance(dbType, shardingRule, lexerEngine); + case DENY: + return DenyUserParserFactory.newInstance(dbType, shardingRule, lexerEngine); + default: + throw new SQLParsingUnsupportedException(tokenType); + } + } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/DCLStatement.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/DCLStatement.java new file mode 100644 index 0000000000000..113265a342f55 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/DCLStatement.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl; + +import io.shardingsphere.core.constant.SQLType; +import io.shardingsphere.core.parsing.lexer.token.Symbol; +import io.shardingsphere.core.parsing.parser.sql.AbstractSQLStatement; +import lombok.ToString; + +/** + * DCL statement. + * + * @author panjuan + */ +@ToString(callSuper = true) +public class DCLStatement extends AbstractSQLStatement { + + public DCLStatement() { + super(SQLType.DCL); + } + + /** + * Judge whether contain star for table. + * + * @return contain star or not. + */ + public boolean isGrantForSingleTable() { + return !getTables().isEmpty() && !getTables().getSingleTableName().equals(Symbol.STAR.getLiterals()); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/alter/AlterUserParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/alter/AlterUserParser.java new file mode 100644 index 0000000000000..daa60d75728d3 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/alter/AlterUserParser.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.alter; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; +import io.shardingsphere.core.parsing.parser.exception.SQLParsingException; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; +import io.shardingsphere.core.parsing.parser.sql.dcl.DCLStatement; +import lombok.RequiredArgsConstructor; + +/** + * Alter user parser. + * + * @author panjuan + */ +@RequiredArgsConstructor +public class AlterUserParser implements SQLParser { + + private final LexerEngine lexerEngine; + + @Override + public DCLStatement parse() { + if (lexerEngine.skipIfEqual(DefaultKeyword.USER) || lexerEngine.skipIfEqual(DefaultKeyword.ROLE) || lexerEngine.skipIfEqual(DefaultKeyword.LOGIN)) { + return new DCLStatement(); + } else { + throw new SQLParsingException("Can't support other ALTER grammar unless ALTER USER, ALTER ROLE, ALTER LOGIN."); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/alter/AlterUserParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/alter/AlterUserParserFactory.java new file mode 100644 index 0000000000000..b49e2eb318afc --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/alter/AlterUserParserFactory.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.alter; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Alter user parser factory. + * + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class AlterUserParserFactory { + + /** + * Alter user parser instance. + * + * @param dbType database type + * @param shardingRule databases and tables sharding rule + * @param lexerEngine lexical analysis engine. + * @return alter user parser instance + */ + public static AlterUserParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + return new AlterUserParser(lexerEngine); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/create/CreateUserParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/create/CreateUserParser.java new file mode 100644 index 0000000000000..68d2da0ff4654 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/create/CreateUserParser.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.create; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; +import io.shardingsphere.core.parsing.parser.exception.SQLParsingException; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; +import io.shardingsphere.core.parsing.parser.sql.dcl.DCLStatement; +import lombok.RequiredArgsConstructor; + +/** + * Create user parser. + * + * @author panjuan + */ +@RequiredArgsConstructor +public class CreateUserParser implements SQLParser { + + private final LexerEngine lexerEngine; + + @Override + public DCLStatement parse() { + if (lexerEngine.skipIfEqual(DefaultKeyword.USER) || lexerEngine.skipIfEqual(DefaultKeyword.ROLE) || lexerEngine.skipIfEqual(DefaultKeyword.LOGIN)) { + return new DCLStatement(); + } else { + throw new SQLParsingException("Can't support other CREATE grammar unless CREATE USER, CREATE ROLE, CREATE LOGIN."); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/create/CreateUserParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/create/CreateUserParserFactory.java new file mode 100644 index 0000000000000..ec788253974b5 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/create/CreateUserParserFactory.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.create; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Create user parser factory. + * + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class CreateUserParserFactory { + + /** + * Create user parser instance. + * + * @param dbType database type + * @param shardingRule databases and tables sharding rule + * @param lexerEngine lexical analysis engine. + * @return create user parser instance + */ + public static CreateUserParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + return new CreateUserParser(lexerEngine); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/deny/DenyUserParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/deny/DenyUserParser.java new file mode 100644 index 0000000000000..ff8aaf7f3a4d2 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/deny/DenyUserParser.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.deny; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; +import io.shardingsphere.core.parsing.parser.clause.TableReferencesClauseParser; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; +import io.shardingsphere.core.parsing.parser.sql.dcl.DCLStatement; +import io.shardingsphere.core.rule.ShardingRule; + +/** + * Deny user parser. + * + * @author panjuan + */ +public class DenyUserParser implements SQLParser { + + private final LexerEngine lexerEngine; + + private final TableReferencesClauseParser tableReferencesClauseParser; + + public DenyUserParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + this.lexerEngine = lexerEngine; + tableReferencesClauseParser = new TableReferencesClauseParser(shardingRule, lexerEngine); + } + + @Override + public DCLStatement parse() { + DCLStatement result = new DCLStatement(); + lexerEngine.skipUntil(DefaultKeyword.ON); + if (!lexerEngine.isEnd()) { + lexerEngine.nextToken(); + tableReferencesClauseParser.parseSingleTableWithoutAlias(result); + } + return result; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/deny/DenyUserParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/deny/DenyUserParserFactory.java new file mode 100644 index 0000000000000..e9a9a182f61aa --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/deny/DenyUserParserFactory.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.deny; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Deny user parser factory. + * + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class DenyUserParserFactory { + + /** + * Deny user parser instance. + * + * @param dbType database type + * @param shardingRule databases and tables sharding rule + * @param lexerEngine lexical analysis engine. + * @return deny user parser instance + */ + public static DenyUserParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + switch (dbType) { + case SQLServer: + return new DenyUserParser(shardingRule, lexerEngine); + default: + throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType)); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/drop/DropUserParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/drop/DropUserParser.java new file mode 100644 index 0000000000000..c88dac0b775a0 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/drop/DropUserParser.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.drop; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; +import io.shardingsphere.core.parsing.parser.exception.SQLParsingException; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; +import io.shardingsphere.core.parsing.parser.sql.dcl.DCLStatement; +import lombok.RequiredArgsConstructor; + +/** + * Drop user parser. + * + * @author panjuan + */ +@RequiredArgsConstructor +public class DropUserParser implements SQLParser { + + private final LexerEngine lexerEngine; + + @Override + public DCLStatement parse() { + if (lexerEngine.skipIfEqual(DefaultKeyword.USER) || lexerEngine.skipIfEqual(DefaultKeyword.ROLE) || lexerEngine.skipIfEqual(DefaultKeyword.LOGIN)) { + return new DCLStatement(); + } else { + throw new SQLParsingException("Can't support other DROP grammar unless DROP USER, DROP ROLE, DROP LOGIN."); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/drop/DropUserParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/drop/DropUserParserFactory.java new file mode 100644 index 0000000000000..fef63bec50d72 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/drop/DropUserParserFactory.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.drop; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Drop user parser factory. + * + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class DropUserParserFactory { + + /** + * Drop user parser instance. + * + * @param dbType database type + * @param shardingRule databases and tables sharding rule + * @param lexerEngine lexical analysis engine. + * @return drop user parser instance + */ + public static DropUserParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + return new DropUserParser(lexerEngine); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/grant/GrantUserParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/grant/GrantUserParser.java new file mode 100644 index 0000000000000..7aca37b00e1d4 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/grant/GrantUserParser.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.grant; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; +import io.shardingsphere.core.parsing.parser.clause.TableReferencesClauseParser; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; +import io.shardingsphere.core.parsing.parser.sql.dcl.DCLStatement; +import io.shardingsphere.core.rule.ShardingRule; + +/** + * Grant user parser. + * + * @author panjuan + */ +public class GrantUserParser implements SQLParser { + + private final LexerEngine lexerEngine; + + private final TableReferencesClauseParser tableReferencesClauseParser; + + public GrantUserParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + this.lexerEngine = lexerEngine; + tableReferencesClauseParser = new TableReferencesClauseParser(shardingRule, lexerEngine); + } + + @Override + public DCLStatement parse() { + DCLStatement result = new DCLStatement(); + lexerEngine.skipUntil(DefaultKeyword.ON); + if (!lexerEngine.isEnd()) { + lexerEngine.nextToken(); + tableReferencesClauseParser.parseSingleTableWithoutAlias(result); + } + return result; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/grant/GrantUserParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/grant/GrantUserParserFactory.java new file mode 100644 index 0000000000000..9d5ee0172be00 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/grant/GrantUserParserFactory.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.grant; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Grant user parser factory. + * + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class GrantUserParserFactory { + + /** + * Grant user parser instance. + * + * @param dbType database type + * @param shardingRule databases and tables sharding rule + * @param lexerEngine lexical analysis engine. + * @return grant user parser instance + */ + public static GrantUserParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + return new GrantUserParser(shardingRule, lexerEngine); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/rename/RenameUserParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/rename/RenameUserParser.java new file mode 100644 index 0000000000000..82f9fda3b6a7e --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/rename/RenameUserParser.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.rename; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; +import io.shardingsphere.core.parsing.parser.exception.SQLParsingException; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; +import io.shardingsphere.core.parsing.parser.sql.dcl.DCLStatement; +import lombok.RequiredArgsConstructor; + +/** + * Rename user parser. + * + * @author panjuan + */ +@RequiredArgsConstructor +public class RenameUserParser implements SQLParser { + + private final LexerEngine lexerEngine; + + @Override + public DCLStatement parse() { + if (lexerEngine.skipIfEqual(DefaultKeyword.USER)) { + return new DCLStatement(); + } else { + throw new SQLParsingException("Can't support other CREATE grammar unless RENAME USER."); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/rename/RenameUserParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/rename/RenameUserParserFactory.java new file mode 100644 index 0000000000000..bed71acb80af7 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/rename/RenameUserParserFactory.java @@ -0,0 +1,51 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.rename; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Rename user parser factory. + * + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class RenameUserParserFactory { + + /** + * Rename user parser instance. + * + * @param dbType database type + * @param shardingRule databases and tables sharding rule + * @param lexerEngine lexical analysis engine. + * @return rename user parser instance + */ + public static RenameUserParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + switch (dbType) { + case H2: + case MySQL: + return new RenameUserParser(lexerEngine); + default: + throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType)); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/revoke/RevokeUserParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/revoke/RevokeUserParser.java new file mode 100644 index 0000000000000..6afcf1bc107c8 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/revoke/RevokeUserParser.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.revoke; + +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; +import io.shardingsphere.core.parsing.parser.clause.TableReferencesClauseParser; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; +import io.shardingsphere.core.parsing.parser.sql.dcl.DCLStatement; +import io.shardingsphere.core.rule.ShardingRule; + +/** + * Revoke user parser. + * + * @author panjuan + */ +public class RevokeUserParser implements SQLParser { + + private final LexerEngine lexerEngine; + + private final TableReferencesClauseParser tableReferencesClauseParser; + + public RevokeUserParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + this.lexerEngine = lexerEngine; + tableReferencesClauseParser = new TableReferencesClauseParser(shardingRule, lexerEngine); + } + + @Override + public DCLStatement parse() { + DCLStatement result = new DCLStatement(); + lexerEngine.skipUntil(DefaultKeyword.ON); + if (!lexerEngine.isEnd()) { + lexerEngine.nextToken(); + tableReferencesClauseParser.parseSingleTableWithoutAlias(result); + } + return result; + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/revoke/RevokeUserParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/revoke/RevokeUserParserFactory.java new file mode 100644 index 0000000000000..cebdc687f98e6 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dcl/revoke/RevokeUserParserFactory.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.dcl.revoke; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Revoke user parser factory. + * + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class RevokeUserParserFactory { + + /** + * Revoke user parser instance. + * + * @param dbType database type + * @param shardingRule databases and tables sharding rule + * @param lexerEngine lexical analysis engine. + * @return revoke user parser instance + */ + public static RevokeUserParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + return new RevokeUserParser(shardingRule, lexerEngine); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/AbstractAlterParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/table/AbstractAlterTableParser.java similarity index 88% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/AbstractAlterParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/table/AbstractAlterTableParser.java index e013c7543b0d6..d0695e3068f89 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/AbstractAlterParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/table/AbstractAlterTableParser.java @@ -15,7 +15,7 @@ *

*/ -package io.shardingsphere.core.parsing.parser.sql.ddl.alter; +package io.shardingsphere.core.parsing.parser.sql.ddl.alter.table; import io.shardingsphere.core.parsing.lexer.LexerEngine; import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; @@ -31,9 +31,10 @@ * Alter parser. * * @author zhangliang + * @author panjuan */ @Getter(AccessLevel.PROTECTED) -public abstract class AbstractAlterParser implements SQLParser { +public abstract class AbstractAlterTableParser implements SQLParser { private final ShardingRule shardingRule; @@ -41,7 +42,7 @@ public abstract class AbstractAlterParser implements SQLParser { private final TableReferencesClauseParser tableReferencesClauseParser; - public AbstractAlterParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public AbstractAlterTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { this.shardingRule = shardingRule; this.lexerEngine = lexerEngine; tableReferencesClauseParser = new TableReferencesClauseParser(shardingRule, lexerEngine); @@ -49,7 +50,6 @@ public AbstractAlterParser(final ShardingRule shardingRule, final LexerEngine le @Override public DDLStatement parse() { - lexerEngine.nextToken(); lexerEngine.unsupportedIfNotSkip(DefaultKeyword.TABLE); lexerEngine.skipAll(getSkippedKeywordsBetweenAlterTableAndTableName()); DDLStatement result = new DDLStatement(); diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/AlterParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/table/AlterTableParserFactory.java similarity index 71% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/AlterParserFactory.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/table/AlterTableParserFactory.java index 7218bac5a34cd..e4d1522960ca0 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/AlterParserFactory.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/alter/table/AlterTableParserFactory.java @@ -15,14 +15,14 @@ *

*/ -package io.shardingsphere.core.parsing.parser.sql.ddl.alter; +package io.shardingsphere.core.parsing.parser.sql.ddl.alter.table; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.parsing.lexer.LexerEngine; -import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLAlterParser; -import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleAlterParser; -import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLAlterParser; -import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerAlterParser; +import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLAlterTableParser; +import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleAlterTableParser; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLAlterTableParser; +import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerAlterTableParser; import io.shardingsphere.core.rule.ShardingRule; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -31,9 +31,10 @@ * Alter parser factory. * * @author zhangliang + * @author panjuan */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class AlterParserFactory { +public final class AlterTableParserFactory { /** * Create alter parser instance. @@ -43,17 +44,17 @@ public final class AlterParserFactory { * @param lexerEngine lexical analysis engine. * @return alter parser instance */ - public static AbstractAlterParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public static AbstractAlterTableParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { switch (dbType) { case H2: case MySQL: - return new MySQLAlterParser(shardingRule, lexerEngine); + return new MySQLAlterTableParser(shardingRule, lexerEngine); case Oracle: - return new OracleAlterParser(shardingRule, lexerEngine); + return new OracleAlterTableParser(shardingRule, lexerEngine); case SQLServer: - return new SQLServerAlterParser(shardingRule, lexerEngine); + return new SQLServerAlterTableParser(shardingRule, lexerEngine); case PostgreSQL: - return new PostgreSQLAlterParser(shardingRule, lexerEngine); + return new PostgreSQLAlterTableParser(shardingRule, lexerEngine); default: throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType)); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/AbstractCreateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/index/AbstractCreateIndexParser.java similarity index 85% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/AbstractCreateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/index/AbstractCreateIndexParser.java index 2d1ac9389ff11..ffc8d80499718 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/AbstractCreateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/index/AbstractCreateIndexParser.java @@ -15,7 +15,7 @@ *

*/ -package io.shardingsphere.core.parsing.parser.sql.ddl.create; +package io.shardingsphere.core.parsing.parser.sql.ddl.create.index; import io.shardingsphere.core.parsing.lexer.LexerEngine; import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; @@ -34,9 +34,10 @@ * Create parser. * * @author zhangliang + * @author panjuan */ @Getter(AccessLevel.PROTECTED) -public abstract class AbstractCreateParser implements SQLParser { +public abstract class AbstractCreateIndexParser implements SQLParser { private final ShardingRule shardingRule; @@ -44,7 +45,7 @@ public abstract class AbstractCreateParser implements SQLParser { private final TableReferencesClauseParser tableReferencesClauseParser; - public AbstractCreateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public AbstractCreateIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { this.shardingRule = shardingRule; this.lexerEngine = lexerEngine; tableReferencesClauseParser = new TableReferencesClauseParser(shardingRule, lexerEngine); @@ -52,17 +53,14 @@ public AbstractCreateParser(final ShardingRule shardingRule, final LexerEngine l @Override public DDLStatement parse() { - lexerEngine.nextToken(); lexerEngine.skipAll(getSkippedKeywordsBetweenCreateIndexAndKeyword()); lexerEngine.skipAll(getSkippedKeywordsBetweenCreateAndKeyword()); DDLStatement result = new DDLStatement(); if (lexerEngine.skipIfEqual(DefaultKeyword.INDEX)) { lexerEngine.skipAll(getSkippedKeywordsBetweenCreateIndexAndIndexName()); parseIndex(result); - } else if (lexerEngine.skipIfEqual(DefaultKeyword.TABLE)) { - lexerEngine.skipAll(getSkippedKeywordsBetweenCreateTableAndTableName()); } else { - throw new SQLParsingException("Can't support other CREATE grammar unless CREATE TABLE, CREATE INDEX."); + throw new SQLParsingException("Can't support other CREATE grammar unless CREATE INDEX."); } tableReferencesClauseParser.parseSingleTableWithoutAlias(result); return result; @@ -85,6 +83,4 @@ private void parseIndex(final DDLStatement ddlStatement) { String tableName = lexerEngine.getCurrentToken().getLiterals(); ddlStatement.getSqlTokens().add(new IndexToken(beginPosition, literals, tableName)); } - - protected abstract Keyword[] getSkippedKeywordsBetweenCreateTableAndTableName(); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/CreateParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/index/CreateIndexParserFactory.java similarity index 71% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/CreateParserFactory.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/index/CreateIndexParserFactory.java index 343f1a79265d9..6940783ee070a 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/CreateParserFactory.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/index/CreateIndexParserFactory.java @@ -15,14 +15,14 @@ *

*/ -package io.shardingsphere.core.parsing.parser.sql.ddl.create; +package io.shardingsphere.core.parsing.parser.sql.ddl.create.index; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.parsing.lexer.LexerEngine; -import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLCreateParser; -import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleCreateParser; -import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLCreateParser; -import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerCreateParser; +import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLCreateIndexParser; +import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleCreateIndexParser; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLCreateIndexParser; +import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerCreateIndexParser; import io.shardingsphere.core.rule.ShardingRule; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -31,9 +31,10 @@ * Create parser factory. * * @author zhangliang + * @author panjuan */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class CreateParserFactory { +public final class CreateIndexParserFactory { /** * Create create parser instance. @@ -43,17 +44,17 @@ public final class CreateParserFactory { * @param lexerEngine lexical analysis engine. * @return create parser instance */ - public static AbstractCreateParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public static AbstractCreateIndexParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { switch (dbType) { case H2: case MySQL: - return new MySQLCreateParser(shardingRule, lexerEngine); + return new MySQLCreateIndexParser(shardingRule, lexerEngine); case Oracle: - return new OracleCreateParser(shardingRule, lexerEngine); + return new OracleCreateIndexParser(shardingRule, lexerEngine); case SQLServer: - return new SQLServerCreateParser(shardingRule, lexerEngine); + return new SQLServerCreateIndexParser(shardingRule, lexerEngine); case PostgreSQL: - return new PostgreSQLCreateParser(shardingRule, lexerEngine); + return new PostgreSQLCreateIndexParser(shardingRule, lexerEngine); default: throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType)); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/table/AbstractCreateTableParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/table/AbstractCreateTableParser.java new file mode 100644 index 0000000000000..f9e5d76ae9d6a --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/table/AbstractCreateTableParser.java @@ -0,0 +1,71 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.ddl.create.table; + +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.clause.TableReferencesClauseParser; +import io.shardingsphere.core.parsing.parser.exception.SQLParsingException; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; +import io.shardingsphere.core.parsing.parser.sql.ddl.DDLStatement; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.Getter; + +/** + * Create parser. + * + * @author zhangliang + * @author panjuan + */ +@Getter(AccessLevel.PROTECTED) +public abstract class AbstractCreateTableParser implements SQLParser { + + private final ShardingRule shardingRule; + + private final LexerEngine lexerEngine; + + private final TableReferencesClauseParser tableReferencesClauseParser; + + public AbstractCreateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + this.shardingRule = shardingRule; + this.lexerEngine = lexerEngine; + tableReferencesClauseParser = new TableReferencesClauseParser(shardingRule, lexerEngine); + } + + @Override + public DDLStatement parse() { + lexerEngine.skipAll(getSkippedKeywordsBetweenCreateIndexAndKeyword()); + lexerEngine.skipAll(getSkippedKeywordsBetweenCreateAndKeyword()); + DDLStatement result = new DDLStatement(); + if (lexerEngine.skipIfEqual(DefaultKeyword.TABLE)) { + lexerEngine.skipAll(getSkippedKeywordsBetweenCreateTableAndTableName()); + } else { + throw new SQLParsingException("Can't support other CREATE grammar unless CREATE TABLE."); + } + tableReferencesClauseParser.parseSingleTableWithoutAlias(result); + return result; + } + + protected abstract Keyword[] getSkippedKeywordsBetweenCreateIndexAndKeyword(); + + protected abstract Keyword[] getSkippedKeywordsBetweenCreateAndKeyword(); + + protected abstract Keyword[] getSkippedKeywordsBetweenCreateTableAndTableName(); +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/table/CreateTableParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/table/CreateTableParserFactory.java new file mode 100644 index 0000000000000..fa532ec4d46d1 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/create/table/CreateTableParserFactory.java @@ -0,0 +1,62 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.ddl.create.table; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLCreateTableParser; +import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleCreateTableParser; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLCreateTableParser; +import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerCreateTableParser; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Create parser factory. + * + * @author zhangliang + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class CreateTableParserFactory { + + /** + * Create create parser instance. + * + * @param dbType database type + * @param shardingRule databases and tables sharding rule + * @param lexerEngine lexical analysis engine. + * @return create parser instance + */ + public static AbstractCreateTableParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + switch (dbType) { + case H2: + case MySQL: + return new MySQLCreateTableParser(shardingRule, lexerEngine); + case Oracle: + return new OracleCreateTableParser(shardingRule, lexerEngine); + case SQLServer: + return new SQLServerCreateTableParser(shardingRule, lexerEngine); + case PostgreSQL: + return new PostgreSQLCreateTableParser(shardingRule, lexerEngine); + default: + throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType)); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/AbstractDropParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/index/AbstractDropIndexParser.java similarity index 83% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/AbstractDropParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/index/AbstractDropIndexParser.java index 4f6cdde03aa1d..12fcab6c58b6a 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/AbstractDropParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/index/AbstractDropIndexParser.java @@ -15,7 +15,7 @@ *

*/ -package io.shardingsphere.core.parsing.parser.sql.ddl.drop; +package io.shardingsphere.core.parsing.parser.sql.ddl.drop.index; import io.shardingsphere.core.parsing.lexer.LexerEngine; import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; @@ -34,9 +34,10 @@ * Drop parser. * * @author zhangliang + * @author panjuan */ @Getter(AccessLevel.PROTECTED) -public abstract class AbstractDropParser implements SQLParser { +public abstract class AbstractDropIndexParser implements SQLParser { private final ShardingRule shardingRule; @@ -44,7 +45,7 @@ public abstract class AbstractDropParser implements SQLParser { private final TableReferencesClauseParser tableReferencesClauseParser; - public AbstractDropParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public AbstractDropIndexParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { this.shardingRule = shardingRule; this.lexerEngine = lexerEngine; tableReferencesClauseParser = new TableReferencesClauseParser(shardingRule, lexerEngine); @@ -52,17 +53,13 @@ public AbstractDropParser(final ShardingRule shardingRule, final LexerEngine lex @Override public DDLStatement parse() { - lexerEngine.nextToken(); lexerEngine.skipAll(getSkippedKeywordsBetweenDropAndTable()); DDLStatement result = new DDLStatement(); if (lexerEngine.skipIfEqual(DefaultKeyword.INDEX)) { lexerEngine.skipAll(getSkippedKeywordsBetweenDropIndexAndIndexName()); 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."); + throw new SQLParsingException("Can't support other DROP grammar unless DROP INDEX."); } return result; } @@ -87,6 +84,4 @@ private void parseIndex(final DDLStatement ddlStatement) { ddlStatement.getSqlTokens().add(new IndexToken(beginPosition, literals, "")); } } - - protected abstract Keyword[] getSkippedKeywordsBetweenDropTableAndTableName(); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/DropParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/index/DropIndexParserFactory.java similarity index 72% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/DropParserFactory.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/index/DropIndexParserFactory.java index 7dd99087c52cd..a537cce1fe7ad 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/DropParserFactory.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/index/DropIndexParserFactory.java @@ -15,14 +15,14 @@ *

*/ -package io.shardingsphere.core.parsing.parser.sql.ddl.drop; +package io.shardingsphere.core.parsing.parser.sql.ddl.drop.index; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.parsing.lexer.LexerEngine; -import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLDropParser; -import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleDropParser; -import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLDropParser; -import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerDropParser; +import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLDropIndexParser; +import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleDropIndexParser; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLDropIndexParser; +import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerDropIndexParser; import io.shardingsphere.core.rule.ShardingRule; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -31,9 +31,10 @@ * Drop parser factory. * * @author zhangliang + * @author panjuan */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class DropParserFactory { +public final class DropIndexParserFactory { /** * Create drop parser instance. @@ -43,17 +44,17 @@ public final class DropParserFactory { * @param lexerEngine lexical analysis engine. * @return drop parser instance */ - public static AbstractDropParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public static AbstractDropIndexParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { switch (dbType) { case H2: case MySQL: - return new MySQLDropParser(shardingRule, lexerEngine); + return new MySQLDropIndexParser(shardingRule, lexerEngine); case Oracle: - return new OracleDropParser(shardingRule, lexerEngine); + return new OracleDropIndexParser(shardingRule, lexerEngine); case SQLServer: - return new SQLServerDropParser(shardingRule, lexerEngine); + return new SQLServerDropIndexParser(shardingRule, lexerEngine); case PostgreSQL: - return new PostgreSQLDropParser(shardingRule, lexerEngine); + return new PostgreSQLDropIndexParser(shardingRule, lexerEngine); default: throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType)); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/table/AbstractDropTableParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/table/AbstractDropTableParser.java new file mode 100644 index 0000000000000..f7518b8f013ba --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/table/AbstractDropTableParser.java @@ -0,0 +1,70 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.ddl.drop.table; + +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.clause.TableReferencesClauseParser; +import io.shardingsphere.core.parsing.parser.exception.SQLParsingException; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; +import io.shardingsphere.core.parsing.parser.sql.ddl.DDLStatement; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.Getter; + +/** + * Drop parser. + * + * @author zhangliang + * @author panjuan + */ +@Getter(AccessLevel.PROTECTED) +public abstract class AbstractDropTableParser implements SQLParser { + + private final ShardingRule shardingRule; + + private final LexerEngine lexerEngine; + + private final TableReferencesClauseParser tableReferencesClauseParser; + + public AbstractDropTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + this.shardingRule = shardingRule; + this.lexerEngine = lexerEngine; + tableReferencesClauseParser = new TableReferencesClauseParser(shardingRule, lexerEngine); + } + + @Override + public DDLStatement parse() { + lexerEngine.skipAll(getSkippedKeywordsBetweenDropAndTable()); + DDLStatement result = new DDLStatement(); + if (lexerEngine.skipIfEqual(DefaultKeyword.TABLE)) { + lexerEngine.skipAll(getSkippedKeywordsBetweenDropTableAndTableName()); + tableReferencesClauseParser.parseSingleTableWithoutAlias(result); + } else { + throw new SQLParsingException("Can't support other DROP grammar unless DROP TABLE."); + } + return result; + } + + protected Keyword[] getSkippedKeywordsBetweenDropAndTable() { + return new Keyword[0]; + } + + protected abstract Keyword[] getSkippedKeywordsBetweenDropTableAndTableName(); +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/table/DropTableParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/table/DropTableParserFactory.java new file mode 100644 index 0000000000000..de796f6197486 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/drop/table/DropTableParserFactory.java @@ -0,0 +1,62 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.parsing.parser.sql.ddl.drop.table; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.parsing.lexer.LexerEngine; +import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLDropTableParser; +import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleDropTableParser; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLDropTableParser; +import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerDropTableParser; +import io.shardingsphere.core.rule.ShardingRule; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Drop parser factory. + * + * @author zhangliang + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class DropTableParserFactory { + + /** + * Create drop parser instance. + * + * @param dbType database type + * @param shardingRule databases and tables sharding rule + * @param lexerEngine lexical analysis engine. + * @return drop parser instance + */ + public static AbstractDropTableParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + switch (dbType) { + case H2: + case MySQL: + return new MySQLDropTableParser(shardingRule, lexerEngine); + case Oracle: + return new OracleDropTableParser(shardingRule, lexerEngine); + case SQLServer: + return new SQLServerDropTableParser(shardingRule, lexerEngine); + case PostgreSQL: + return new PostgreSQLDropTableParser(shardingRule, lexerEngine); + default: + throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType)); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/AbstractTruncateParser.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/table/AbstractTruncateTableParser.java similarity index 88% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/AbstractTruncateParser.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/table/AbstractTruncateTableParser.java index 7fed06783e13c..4c1bf99d417b6 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/AbstractTruncateParser.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/table/AbstractTruncateTableParser.java @@ -15,7 +15,7 @@ *

*/ -package io.shardingsphere.core.parsing.parser.sql.ddl.truncate; +package io.shardingsphere.core.parsing.parser.sql.ddl.truncate.table; import io.shardingsphere.core.parsing.lexer.LexerEngine; import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword; @@ -31,9 +31,10 @@ * Truncate parser. * * @author zhangliang + * @author panjuan */ @Getter(AccessLevel.PROTECTED) -public abstract class AbstractTruncateParser implements SQLParser { +public abstract class AbstractTruncateTableParser implements SQLParser { private final ShardingRule shardingRule; @@ -41,7 +42,7 @@ public abstract class AbstractTruncateParser implements SQLParser { private final TableReferencesClauseParser tableReferencesClauseParser; - public AbstractTruncateParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public AbstractTruncateTableParser(final ShardingRule shardingRule, final LexerEngine lexerEngine) { this.shardingRule = shardingRule; this.lexerEngine = lexerEngine; tableReferencesClauseParser = new TableReferencesClauseParser(shardingRule, lexerEngine); @@ -49,7 +50,6 @@ public AbstractTruncateParser(final ShardingRule shardingRule, final LexerEngine @Override public DDLStatement parse() { - lexerEngine.nextToken(); lexerEngine.skipIfEqual(DefaultKeyword.TABLE); lexerEngine.skipAll(getSkippedKeywordsBetweenTruncateTableAndTableName()); DDLStatement result = new DDLStatement(); diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/TruncateParserFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/table/TruncateTableParserFactory.java similarity index 71% rename from sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/TruncateParserFactory.java rename to sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/table/TruncateTableParserFactory.java index 2fd2686e1f191..4c18889cddb71 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/TruncateParserFactory.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/ddl/truncate/table/TruncateTableParserFactory.java @@ -15,14 +15,14 @@ *

*/ -package io.shardingsphere.core.parsing.parser.sql.ddl.truncate; +package io.shardingsphere.core.parsing.parser.sql.ddl.truncate.table; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.parsing.lexer.LexerEngine; -import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLTruncateParser; -import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleTruncateParser; -import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLTruncateParser; -import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerTruncateParser; +import io.shardingsphere.core.parsing.parser.dialect.mysql.sql.MySQLTruncateTableParser; +import io.shardingsphere.core.parsing.parser.dialect.oracle.sql.OracleTruncateTableParser; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.sql.PostgreSQLTruncateTableParser; +import io.shardingsphere.core.parsing.parser.dialect.sqlserver.sql.SQLServerTruncateTableParser; import io.shardingsphere.core.rule.ShardingRule; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -31,9 +31,10 @@ * Truncate parser factory. * * @author zhangliang + * @author panjuan */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class TruncateParserFactory { +public final class TruncateTableParserFactory { /** * Create truncate parser instance. @@ -43,17 +44,17 @@ public final class TruncateParserFactory { * @param lexerEngine lexical analysis engine. * @return truncate parser instance */ - public static AbstractTruncateParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { + public static AbstractTruncateTableParser newInstance(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { switch (dbType) { case H2: case MySQL: - return new MySQLTruncateParser(shardingRule, lexerEngine); + return new MySQLTruncateTableParser(shardingRule, lexerEngine); case Oracle: - return new OracleTruncateParser(shardingRule, lexerEngine); + return new OracleTruncateTableParser(shardingRule, lexerEngine); case SQLServer: - return new SQLServerTruncateParser(shardingRule, lexerEngine); + return new SQLServerTruncateTableParser(shardingRule, lexerEngine); case PostgreSQL: - return new PostgreSQLTruncateParser(shardingRule, lexerEngine); + return new PostgreSQLTruncateTableParser(shardingRule, lexerEngine); default: throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType)); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourceProperty.java b/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourceProperty.java new file mode 100644 index 0000000000000..ce2f7463652a2 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourceProperty.java @@ -0,0 +1,51 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.property; + +import io.shardingsphere.core.constant.DatabaseType; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * Data source property. + * + * @author panjuan + */ +@RequiredArgsConstructor +@Getter +public class DataSourceProperty { + + private final String hostName; + + private final Integer port; + + private final String schemeName; + + private final DatabaseType databaseType; + + /** + * Judge whether two of data source properties point at the same instance. + * + * @param dataSourceProperty data source property. + * @return pointing at the same instance or not. + */ + public boolean isPointAtSameInstance(final DataSourceProperty dataSourceProperty) { + return getHostName().equals(dataSourceProperty.getHostName()) && getPort().equals(dataSourceProperty.getPort()) + && getDatabaseType().equals(dataSourceProperty.getDatabaseType()); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourcePropertyFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourcePropertyFactory.java new file mode 100644 index 0000000000000..18acfbe627272 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourcePropertyFactory.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.property; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.property.dialect.H2DataSourcePropertyParser; +import io.shardingsphere.core.property.dialect.MySQLDataSourcePropertyParser; +import io.shardingsphere.core.property.dialect.OracleDataSourcePropertyParser; +import io.shardingsphere.core.property.dialect.PostgreSQLDataSourcePropertyParser; +import io.shardingsphere.core.property.dialect.SQLServerDataSourcePropertyParser; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Data source property factory. + * + * @author panjuan + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class DataSourcePropertyFactory { + + /** + * Create data source property parser. + * + * @param databaseType data base type. + * @return data source property parser. + */ + public static DataSourcePropertyParser createDataSourcePropertyParser(final DatabaseType databaseType) { + switch (databaseType) { + case H2: + return new H2DataSourcePropertyParser(); + case MySQL: + return new MySQLDataSourcePropertyParser(); + case Oracle: + return new OracleDataSourcePropertyParser(); + case PostgreSQL: + return new PostgreSQLDataSourcePropertyParser(); + case SQLServer: + return new SQLServerDataSourcePropertyParser(); + default: + throw new UnsupportedOperationException(String.format("Cannot support database [%s].", databaseType)); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourcePropertyManager.java b/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourcePropertyManager.java new file mode 100644 index 0000000000000..9b374b60fae2b --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourcePropertyManager.java @@ -0,0 +1,83 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.property; + +import io.shardingsphere.core.constant.DatabaseType; + +import javax.sql.DataSource; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * Data source property manager. + * + * @author panjuan + */ +public class DataSourcePropertyManager { + + private final Map dataSourcePropertyMap; + + public DataSourcePropertyManager(final Map dataSourceMap, final DatabaseType databaseType) { + dataSourcePropertyMap = initDataSourcePropertyMap(dataSourceMap, databaseType); + } + + private Map initDataSourcePropertyMap(final Map dataSourceMap, + final DatabaseType databaseType) { + Map result = new LinkedHashMap<>(); + for (Map.Entry each : dataSourceMap.entrySet()) { + result.put(each.getKey(), DataSourcePropertyFactory.createDataSourcePropertyParser(databaseType).parseDataSource(each.getValue())); + } + return result; + } + + /** + * Get all instance data source names. + * + * @return instance data source name list + */ + public List getAllInstanceDataSourceNames() { + List result = new LinkedList<>(); + for (Map.Entry each : dataSourcePropertyMap.entrySet()) { + if (!isExisted(each.getKey(), result)) { + result.add(each.getKey()); + } + } + return result; + } + + private boolean isExisted(final String dataSourceName, final List existedDataSourceNames) { + for (String each : existedDataSourceNames) { + if (dataSourcePropertyMap.get(each).isPointAtSameInstance(dataSourcePropertyMap.get(dataSourceName))) { + return true; + } + } + return false; + } + + /** + * Get actual schema name. + * + * @param actualDataSourceName actual data source name + * @return actual schema name + */ + public String getActualSchemaName(final String actualDataSourceName) { + return dataSourcePropertyMap.get(actualDataSourceName).getSchemeName(); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourcePropertyParser.java b/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourcePropertyParser.java new file mode 100644 index 0000000000000..b9c6499891426 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/property/DataSourcePropertyParser.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.property; + +import io.shardingsphere.core.exception.ShardingException; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +/** + * Data source parameter parser. + * + * @author panjuan + */ +public abstract class DataSourcePropertyParser { + + /** + * Parse data source. + * + * @param dataSource data source + * @return data source property + */ + public DataSourceProperty parseDataSource(final DataSource dataSource) { + try (Connection connection = dataSource.getConnection()) { + return parseJDBCUrl(connection.getMetaData().getURL()); + } catch (SQLException ex) { + throw new ShardingException(ex); + } + } + + protected abstract DataSourceProperty parseJDBCUrl(String url); +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/H2DataSourcePropertyParser.java b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/H2DataSourcePropertyParser.java new file mode 100644 index 0000000000000..37fe69a7e731f --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/H2DataSourcePropertyParser.java @@ -0,0 +1,60 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.property.dialect; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.exception.ShardingException; +import io.shardingsphere.core.property.DataSourceProperty; +import io.shardingsphere.core.property.DataSourcePropertyParser; + +import java.net.URI; + +/** + * H2 data source property parser. + * + * @author panjuan + */ +public final class H2DataSourcePropertyParser extends DataSourcePropertyParser { + + private static final Integer DEFAULT_PORT = -1; + + private static final String DEFAULT_HOST = "localhost"; + + @Override + protected DataSourceProperty parseJDBCUrl(final String url) { + String cleanUrl = url.substring(5); + if (cleanUrl.contains("h2:~")) { + cleanUrl = cleanUrl.split(";")[0]; + cleanUrl = cleanUrl.replace(":", "://").replace("~", DEFAULT_HOST); + } else if (cleanUrl.contains("h2:mem")) { + cleanUrl = cleanUrl.split(";")[0]; + String[] parts = cleanUrl.split(":"); + if (3 == parts.length) { + cleanUrl = parts[0] + "://" + parts[1] + "/" + parts[2]; + } + } else { + throw new ShardingException("The URL of JDBC is not supported."); + } + URI uri = URI.create(cleanUrl); + if (null == uri.getHost()) { + throw new ShardingException("The URL of JDBC is not supported."); + } + return new DataSourceProperty(uri.getHost(), -1 == uri.getPort() ? DEFAULT_PORT : uri.getPort(), + uri.getPath().isEmpty() ? "" : uri.getPath().substring(1), DatabaseType.H2); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/MySQLDataSourcePropertyParser.java b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/MySQLDataSourcePropertyParser.java new file mode 100644 index 0000000000000..9589fb20c0a2e --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/MySQLDataSourcePropertyParser.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.property.dialect; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.exception.ShardingException; +import io.shardingsphere.core.property.DataSourceProperty; +import io.shardingsphere.core.property.DataSourcePropertyParser; + +import java.net.URI; + +/** + * MySQL data source property parser. + * + * @author panjuan + */ +public final class MySQLDataSourcePropertyParser extends DataSourcePropertyParser { + + private static final Integer DEFAULT_PORT = 3306; + + @Override + protected DataSourceProperty parseJDBCUrl(final String url) { + String cleanUrl = url.substring(5); + URI uri = URI.create(cleanUrl); + if (null == uri.getHost()) { + throw new ShardingException("The URL of JDBC is not supported."); + } + return new DataSourceProperty(uri.getHost(), -1 == uri.getPort() ? DEFAULT_PORT : uri.getPort(), + uri.getPath().isEmpty() ? "" : uri.getPath().substring(1), DatabaseType.MySQL); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/OracleDataSourcePropertyParser.java b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/OracleDataSourcePropertyParser.java new file mode 100644 index 0000000000000..06913db5610ac --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/OracleDataSourcePropertyParser.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.property.dialect; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.exception.ShardingException; +import io.shardingsphere.core.property.DataSourceProperty; +import io.shardingsphere.core.property.DataSourcePropertyParser; + +import java.net.URI; + +/** + * Oracle data source property parser. + * + * @author panjuan + */ +public final class OracleDataSourcePropertyParser extends DataSourcePropertyParser { + + private static final Integer DEFAULT_PORT = 1521; + + @Override + protected DataSourceProperty parseJDBCUrl(final String url) { + String cleanUrl = url.substring(5); + if (cleanUrl.contains("oracle:thin:@//")) { + cleanUrl = cleanUrl.replace("oracle:thin:@//", "oracle://"); + } else if (cleanUrl.contains("oracle:thin:@")) { + cleanUrl = cleanUrl.replace("oracle:thin:@", "oracle://"); + } + + String[] parts = cleanUrl.split(":"); + if (4 == parts.length) { + cleanUrl = parts[0] + ":" + parts[1] + ":" + parts[2] + "/" + parts[3]; + } + URI uri = URI.create(cleanUrl); + if (null == uri.getHost()) { + throw new ShardingException("The URL of JDBC is not supported."); + } + return new DataSourceProperty(uri.getHost(), -1 == uri.getPort() ? DEFAULT_PORT : uri.getPort(), + uri.getPath().isEmpty() ? "" : uri.getPath().substring(1), DatabaseType.Oracle); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/PostgreSQLDataSourcePropertyParser.java b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/PostgreSQLDataSourcePropertyParser.java new file mode 100644 index 0000000000000..1f51fc5114ebf --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/PostgreSQLDataSourcePropertyParser.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.property.dialect; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.exception.ShardingException; +import io.shardingsphere.core.property.DataSourceProperty; +import io.shardingsphere.core.property.DataSourcePropertyParser; + +import java.net.URI; + +/** + * PostgreSQL data source property parser. + * + * @author panjuan + */ +public final class PostgreSQLDataSourcePropertyParser extends DataSourcePropertyParser { + + private static final Integer DEFAULT_PORT = 5432; + + @Override + protected DataSourceProperty parseJDBCUrl(final String url) { + String cleanUrl = url.substring(5); + URI uri = URI.create(cleanUrl); + if (null == uri.getHost()) { + throw new ShardingException("The URL of JDBC is not supported."); + } + return new DataSourceProperty(uri.getHost(), -1 == uri.getPort() ? DEFAULT_PORT : uri.getPort(), + uri.getPath().isEmpty() ? "" : uri.getPath().substring(1), DatabaseType.PostgreSQL); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/SQLServerDataSourcePropertyParser.java b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/SQLServerDataSourcePropertyParser.java new file mode 100644 index 0000000000000..3b6d37ab1cd60 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/property/dialect/SQLServerDataSourcePropertyParser.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * 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. + *

+ */ + +package io.shardingsphere.core.property.dialect; + +import io.shardingsphere.core.constant.DatabaseType; +import io.shardingsphere.core.exception.ShardingException; +import io.shardingsphere.core.property.DataSourceProperty; +import io.shardingsphere.core.property.DataSourcePropertyParser; + +import java.net.URI; + +/** + * SQLServer data source property parser. + * + * @author panjuan + */ +public final class SQLServerDataSourcePropertyParser extends DataSourcePropertyParser { + + private static final Integer DEFAULT_PORT = 1433; + + @Override + protected DataSourceProperty parseJDBCUrl(final String url) { + String cleanUrl = url.substring(5); + cleanUrl = cleanUrl.replace("microsoft:", "").replace(";DatabaseName=", "/"); + URI uri = URI.create(cleanUrl); + if (null == uri.getHost()) { + throw new ShardingException("The URL of JDBC is not supported."); + } + return new DataSourceProperty(uri.getHost(), -1 == uri.getPort() ? DEFAULT_PORT : uri.getPort(), + uri.getPath().isEmpty() ? "" : uri.getPath().substring(1), DatabaseType.SQLServer); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/rewrite/SQLBuilder.java b/sharding-core/src/main/java/io/shardingsphere/core/rewrite/SQLBuilder.java index 6fe8c6bafb589..f2bd40d09e471 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/rewrite/SQLBuilder.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/rewrite/SQLBuilder.java @@ -23,6 +23,7 @@ import io.shardingsphere.core.exception.ShardingException; import io.shardingsphere.core.optimizer.condition.ShardingCondition; import io.shardingsphere.core.optimizer.insert.InsertShardingCondition; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.rewrite.placeholder.IndexPlaceholder; import io.shardingsphere.core.rewrite.placeholder.InsertValuesPlaceholder; import io.shardingsphere.core.rewrite.placeholder.SchemaPlaceholder; @@ -42,7 +43,7 @@ /** * SQL builder. - * + * * @author gaohongtao * @author zhangliang * @author maxiaoguang @@ -92,9 +93,11 @@ public void appendPlaceholder(final ShardingPlaceholder shardingPlaceholder) { * @param tableUnit table unit * @param logicAndActualTableMap logic and actual map * @param shardingRule sharding rule + * @param dataSourcePropertyManager data source property manager * @return SQL unit */ - public SQLUnit toSQL(final TableUnit tableUnit, final Map logicAndActualTableMap, final ShardingRule shardingRule) { + public SQLUnit toSQL(final TableUnit tableUnit, final Map logicAndActualTableMap, + final ShardingRule shardingRule, final DataSourcePropertyManager dataSourcePropertyManager) { List insertParameters = new LinkedList<>(); StringBuilder result = new StringBuilder(); for (Object each : segments) { @@ -112,10 +115,9 @@ public SQLUnit toSQL(final TableUnit tableUnit, final Map logicA if (!tableRule.isPresent() && Strings.isNullOrEmpty(shardingRule.getShardingDataSourceNames().getDefaultDataSourceName())) { throw new ShardingException("Cannot found schema name '%s' in sharding rule.", schemaPlaceholder.getLogicSchemaName()); } - // TODO 目前只能找到真实数据源名称. 未来需要在初始化sharding rule时创建connection,并验证连接是否正确,并获取出真实的schema的名字, 然后在这里替换actualDataSourceName为actualSchemaName - // TODO 目前actualDataSourceName必须actualSchemaName一样,才能保证替换schema的场景不出错, 如: show columns xxx Preconditions.checkState(tableRule.isPresent()); - result.append(tableRule.get().getActualDatasourceNames().iterator().next()); + String actualSchemaName = dataSourcePropertyManager.getActualSchemaName(tableRule.get().getActualDatasourceNames().iterator().next()); + result.append(actualSchemaName); } else if (each instanceof IndexPlaceholder) { IndexPlaceholder indexPlaceholder = (IndexPlaceholder) each; result.append(indexPlaceholder.getLogicIndexName()); diff --git a/sharding-core/src/main/java/io/shardingsphere/core/rewrite/SQLRewriteEngine.java b/sharding-core/src/main/java/io/shardingsphere/core/rewrite/SQLRewriteEngine.java index 83464ba1fb544..18e65fc75c606 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/rewrite/SQLRewriteEngine.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/rewrite/SQLRewriteEngine.java @@ -37,6 +37,7 @@ import io.shardingsphere.core.parsing.parser.token.SQLToken; import io.shardingsphere.core.parsing.parser.token.SchemaToken; import io.shardingsphere.core.parsing.parser.token.TableToken; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.rewrite.placeholder.IndexPlaceholder; import io.shardingsphere.core.rewrite.placeholder.InsertValuesPlaceholder; import io.shardingsphere.core.rewrite.placeholder.SchemaPlaceholder; @@ -62,6 +63,7 @@ * * @author zhangliang * @author maxiaoguang + * @author panjuan */ public final class SQLRewriteEngine { @@ -247,10 +249,11 @@ private void appendRest(final SQLBuilder sqlBuilder, final int count, final List * * @param tableUnit route table unit * @param sqlBuilder SQL builder + * @param dataSourcePropertyManager dataSource property manager * @return SQL unit */ - public SQLUnit generateSQL(final TableUnit tableUnit, final SQLBuilder sqlBuilder) { - return sqlBuilder.toSQL(tableUnit, getTableTokens(tableUnit), shardingRule); + public SQLUnit generateSQL(final TableUnit tableUnit, final SQLBuilder sqlBuilder, final DataSourcePropertyManager dataSourcePropertyManager) { + return sqlBuilder.toSQL(tableUnit, getTableTokens(tableUnit), shardingRule, dataSourcePropertyManager); } private Map getTableTokens(final TableUnit tableUnit) { diff --git a/sharding-core/src/main/java/io/shardingsphere/core/routing/PreparedStatementRoutingEngine.java b/sharding-core/src/main/java/io/shardingsphere/core/routing/PreparedStatementRoutingEngine.java index 0ff33797f1ff6..f20f725e95bda 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/routing/PreparedStatementRoutingEngine.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/routing/PreparedStatementRoutingEngine.java @@ -20,6 +20,7 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.metadata.ShardingMetaData; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.routing.router.masterslave.ShardingMasterSlaveRouter; import io.shardingsphere.core.routing.router.sharding.ShardingRouter; import io.shardingsphere.core.routing.router.sharding.ShardingRouterFactory; @@ -43,9 +44,11 @@ public final class PreparedStatementRoutingEngine { private SQLStatement sqlStatement; - public PreparedStatementRoutingEngine(final String logicSQL, final ShardingRule shardingRule, final ShardingMetaData shardingMetaData, final DatabaseType databaseType, final boolean showSQL) { + public PreparedStatementRoutingEngine(final String logicSQL, final ShardingRule shardingRule, + final ShardingMetaData shardingMetaData, final DatabaseType databaseType, + final boolean showSQL, final DataSourcePropertyManager dataSourcePropertyManager) { this.logicSQL = logicSQL; - shardingRouter = ShardingRouterFactory.createSQLRouter(shardingRule, shardingMetaData, databaseType, showSQL); + shardingRouter = ShardingRouterFactory.createSQLRouter(shardingRule, shardingMetaData, databaseType, showSQL, dataSourcePropertyManager); masterSlaveRouter = new ShardingMasterSlaveRouter(shardingRule.getMasterSlaveRules()); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/routing/StatementRoutingEngine.java b/sharding-core/src/main/java/io/shardingsphere/core/routing/StatementRoutingEngine.java index 0aac4b5aeea8c..47723f8a8f0e0 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/routing/StatementRoutingEngine.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/routing/StatementRoutingEngine.java @@ -20,6 +20,7 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.metadata.ShardingMetaData; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.routing.router.masterslave.ShardingMasterSlaveRouter; import io.shardingsphere.core.routing.router.sharding.ShardingRouter; import io.shardingsphere.core.routing.router.sharding.ShardingRouterFactory; @@ -39,8 +40,9 @@ public final class StatementRoutingEngine { private final ShardingMasterSlaveRouter masterSlaveRouter; - public StatementRoutingEngine(final ShardingRule shardingRule, final ShardingMetaData shardingMetaData, final DatabaseType databaseType, final boolean showSQL) { - shardingRouter = ShardingRouterFactory.createSQLRouter(shardingRule, shardingMetaData, databaseType, showSQL); + public StatementRoutingEngine(final ShardingRule shardingRule, final ShardingMetaData shardingMetaData, + final DatabaseType databaseType, final boolean showSQL, final DataSourcePropertyManager dataSourcePropertyManager) { + shardingRouter = ShardingRouterFactory.createSQLRouter(shardingRule, shardingMetaData, databaseType, showSQL, dataSourcePropertyManager); masterSlaveRouter = new ShardingMasterSlaveRouter(shardingRule.getMasterSlaveRules()); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ParsingSQLRouter.java b/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ParsingSQLRouter.java index e4c836091f837..72b730d76ae73 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ParsingSQLRouter.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ParsingSQLRouter.java @@ -30,9 +30,11 @@ import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.UseStatement; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement; +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.insert.InsertStatement; import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.rewrite.SQLBuilder; import io.shardingsphere.core.rewrite.SQLRewriteEngine; import io.shardingsphere.core.routing.SQLExecutionUnit; @@ -75,6 +77,8 @@ public final class ParsingSQLRouter implements ShardingRouter { private final List generatedKeys = new LinkedList<>(); + private final DataSourcePropertyManager dataSourcePropertyManager; + @Override public SQLStatement parse(final String logicSQL, final boolean useCache) { return new SQLParsingEngine(databaseType, logicSQL, shardingRule, shardingMetaData).parse(useCache); @@ -99,7 +103,7 @@ public SQLRouteResult route(final String logicSQL, final List parameters } SQLBuilder sqlBuilder = rewriteEngine.rewrite(!isSingleRouting); for (TableUnit each : routingResult.getTableUnits().getTableUnits()) { - result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), rewriteEngine.generateSQL(each, sqlBuilder))); + result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), rewriteEngine.generateSQL(each, sqlBuilder, dataSourcePropertyManager))); } if (showSQL) { SQLLogger.logSQL(logicSQL, sqlStatement, result.getExecutionUnits()); @@ -112,9 +116,9 @@ private RoutingResult route(final List parameters, final SQLStatement sq RoutingEngine routingEngine; if (sqlStatement instanceof UseStatement) { routingEngine = new IgnoreRoutingEngine(); - } else if (sqlStatement instanceof DDLStatement) { + } else if (sqlStatement instanceof DDLStatement || (sqlStatement instanceof DCLStatement && ((DCLStatement) sqlStatement).isGrantForSingleTable())) { routingEngine = new TableBroadcastRoutingEngine(shardingRule, sqlStatement); - } else if (sqlStatement instanceof ShowDatabasesStatement || sqlStatement instanceof ShowTablesStatement) { + } else if (sqlStatement instanceof ShowDatabasesStatement || sqlStatement instanceof ShowTablesStatement || sqlStatement instanceof DCLStatement) { routingEngine = new DatabaseBroadcastRoutingEngine(shardingRule); } else if (shardingConditions.isAlwaysFalse()) { routingEngine = new UnicastRoutingEngine(shardingRule, tableNames); @@ -130,7 +134,23 @@ private RoutingResult route(final List parameters, final SQLStatement sq // TODO config for cartesian set routingEngine = new ComplexRoutingEngine(shardingRule, parameters, tableNames, shardingConditions); } - return routingEngine.route(); + return getRoutingResult(routingEngine, sqlStatement); + } + + private RoutingResult getRoutingResult(final RoutingEngine routingEngine, final SQLStatement sqlStatement) { + RoutingResult result = routingEngine.route(); + if (routingEngine instanceof DatabaseBroadcastRoutingEngine && sqlStatement instanceof DCLStatement) { + removeRedundantTableUnits(result); + } + return result; + } + + private void removeRedundantTableUnits(final RoutingResult routingResult) { + for (TableUnit each : routingResult.getTableUnits().getTableUnits()) { + if (!dataSourcePropertyManager.getAllInstanceDataSourceNames().contains(each.getDataSourceName())) { + routingResult.getTableUnits().getTableUnits().remove(each); + } + } } private GeneratedKey getGenerateKey(final ShardingRule shardingRule, final InsertStatement insertStatement, final List parameters) { diff --git a/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ShardingRouterFactory.java b/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ShardingRouterFactory.java index 86070c98efa9f..ac33aff901904 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ShardingRouterFactory.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ShardingRouterFactory.java @@ -20,6 +20,7 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.hint.HintManagerHolder; import io.shardingsphere.core.metadata.ShardingMetaData; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.rule.ShardingRule; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -40,9 +41,13 @@ public final class ShardingRouterFactory { * @param shardingMetaData sharding meta data * @param databaseType database type * @param showSQL show SQL or not + * @param dataSourcePropertyManager data source property manager * @return sharding router instance */ - public static ShardingRouter createSQLRouter(final ShardingRule shardingRule, final ShardingMetaData shardingMetaData, final DatabaseType databaseType, final boolean showSQL) { - return HintManagerHolder.isDatabaseShardingOnly() ? new DatabaseHintSQLRouter(shardingRule, showSQL) : new ParsingSQLRouter(shardingRule, shardingMetaData, databaseType, showSQL); + public static ShardingRouter createSQLRouter(final ShardingRule shardingRule, final ShardingMetaData shardingMetaData, + final DatabaseType databaseType, final boolean showSQL, + final DataSourcePropertyManager dataSourcePropertyManager) { + return HintManagerHolder.isDatabaseShardingOnly() ? new DatabaseHintSQLRouter(shardingRule, showSQL) + : new ParsingSQLRouter(shardingRule, shardingMetaData, databaseType, showSQL, dataSourcePropertyManager); } } diff --git a/sharding-core/src/test/java/io/shardingsphere/core/rewrite/SQLBuilderTest.java b/sharding-core/src/test/java/io/shardingsphere/core/rewrite/SQLBuilderTest.java index 9ab2f537da0f4..c197fa27ec7d9 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/rewrite/SQLBuilderTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/rewrite/SQLBuilderTest.java @@ -20,11 +20,13 @@ import io.shardingsphere.core.api.config.ShardingRuleConfiguration; import io.shardingsphere.core.api.config.TableRuleConfiguration; import io.shardingsphere.core.exception.ShardingException; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.rewrite.placeholder.IndexPlaceholder; import io.shardingsphere.core.rewrite.placeholder.SchemaPlaceholder; import io.shardingsphere.core.rewrite.placeholder.TablePlaceholder; import io.shardingsphere.core.rule.ShardingRule; import org.junit.Test; +import org.mockito.Mockito; import java.util.Arrays; import java.util.Collection; @@ -45,7 +47,7 @@ public void assertAppendLiteralsOnly() { sqlBuilder.appendLiterals(".id"); sqlBuilder.appendLiterals(" FROM "); sqlBuilder.appendLiterals("table_x"); - assertThat(sqlBuilder.toSQL(null, Collections.emptyMap(), null).getSql(), is("SELECT table_x.id FROM table_x")); + assertThat(sqlBuilder.toSQL(null, Collections.emptyMap(), null, null).getSql(), is("SELECT table_x.id FROM table_x")); } @Test @@ -56,7 +58,7 @@ public void assertAppendTableWithoutTableToken() { sqlBuilder.appendLiterals(".id"); sqlBuilder.appendLiterals(" FROM "); sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x")); - assertThat(sqlBuilder.toSQL(null, Collections.emptyMap(), null).getSql(), is("SELECT table_x.id FROM table_x")); + assertThat(sqlBuilder.toSQL(null, Collections.emptyMap(), null, null).getSql(), is("SELECT table_x.id FROM table_x")); } @Test @@ -69,7 +71,7 @@ public void assertAppendTableWithTableToken() { sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x")); Map tableTokens = new HashMap<>(1, 1); tableTokens.put("table_x", "table_x_1"); - assertThat(sqlBuilder.toSQL(null, tableTokens, null).getSql(), is("SELECT table_x_1.id FROM table_x_1")); + assertThat(sqlBuilder.toSQL(null, tableTokens, null, null).getSql(), is("SELECT table_x_1.id FROM table_x_1")); } @Test @@ -80,7 +82,7 @@ public void assertIndexPlaceholderAppendTableWithoutTableToken() { sqlBuilder.appendLiterals(" ON "); sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x")); sqlBuilder.appendLiterals(" ('column')"); - assertThat(sqlBuilder.toSQL(null, Collections.emptyMap(), null).getSql(), is("CREATE INDEX index_name ON table_x ('column')")); + assertThat(sqlBuilder.toSQL(null, Collections.emptyMap(), null, null).getSql(), is("CREATE INDEX index_name ON table_x ('column')")); } @Test @@ -93,7 +95,7 @@ public void assertIndexPlaceholderAppendTableWithTableToken() { sqlBuilder.appendLiterals(" ('column')"); Map tableTokens = new HashMap<>(1, 1); tableTokens.put("table_x", "table_x_1"); - assertThat(sqlBuilder.toSQL(null, tableTokens, null).getSql(), is("CREATE INDEX index_name_table_x_1 ON table_x_1 ('column')")); + assertThat(sqlBuilder.toSQL(null, tableTokens, null, null).getSql(), is("CREATE INDEX index_name_table_x_1 ON table_x_1 ('column')")); } @Test(expected = ShardingException.class) @@ -104,7 +106,7 @@ public void assertSchemaPlaceholderAppendTableWithoutTableToken() { sqlBuilder.appendPlaceholder(new TablePlaceholder("table_x")); sqlBuilder.appendLiterals("ON "); sqlBuilder.appendPlaceholder(new SchemaPlaceholder("dx", "table_x")); - sqlBuilder.toSQL(null, Collections.emptyMap(), createShardingRule()); + sqlBuilder.toSQL(null, Collections.emptyMap(), createShardingRule(), null); } @Test @@ -117,7 +119,9 @@ public void assertSchemaPlaceholderAppendTableWithTableToken() { sqlBuilder.appendPlaceholder(new SchemaPlaceholder("ds", "table_0")); Map tableTokens = new HashMap<>(1, 1); tableTokens.put("table_0", "table_1"); - assertThat(sqlBuilder.toSQL(null, tableTokens, createShardingRule()).getSql(), is("SHOW CREATE TABLE table_1 ON ds0")); + DataSourcePropertyManager dataSourcePropertyManager = Mockito.mock(DataSourcePropertyManager.class); + Mockito.when(dataSourcePropertyManager.getActualSchemaName(Mockito.anyString())).thenReturn("actual_db"); + assertThat(sqlBuilder.toSQL(null, tableTokens, createShardingRule(), dataSourcePropertyManager).getSql(), is("SHOW CREATE TABLE table_1 ON actual_db")); } @Test diff --git a/sharding-core/src/test/java/io/shardingsphere/core/rewrite/SQLRewriteEngineTest.java b/sharding-core/src/test/java/io/shardingsphere/core/rewrite/SQLRewriteEngineTest.java index bf64b8675dfdd..2a909efce105f 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/rewrite/SQLRewriteEngineTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/rewrite/SQLRewriteEngineTest.java @@ -41,6 +41,7 @@ import io.shardingsphere.core.parsing.parser.token.RowCountToken; import io.shardingsphere.core.parsing.parser.token.SchemaToken; import io.shardingsphere.core.parsing.parser.token.TableToken; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.routing.type.RoutingTable; import io.shardingsphere.core.routing.type.TableUnit; import io.shardingsphere.core.rule.DataNode; @@ -48,6 +49,7 @@ import io.shardingsphere.core.yaml.sharding.YamlShardingConfiguration; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import java.io.File; import java.io.IOException; @@ -56,6 +58,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -76,6 +79,8 @@ public final class SQLRewriteEngineTest { private Map tableTokens; + private DataSourcePropertyManager dataSourcePropertyManager; + @Before public void setUp() throws IOException { URL url = SQLRewriteEngineTest.class.getClassLoader().getResource("yaml/rewrite-rule.yaml"); @@ -88,12 +93,16 @@ public void setUp() throws IOException { dmlStatement = new DMLStatement(); tableTokens = new HashMap<>(1, 1); tableTokens.put("table_x", "table_1"); + dataSourcePropertyManager = Mockito.mock(DataSourcePropertyManager.class); + Mockito.when(dataSourcePropertyManager.getActualSchemaName(Mockito.anyString())).thenReturn("actual_db"); + } @Test public void assertRewriteWithoutChange() { - SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT table_y.id FROM table_y WHERE table_y.id=?", DatabaseType.MySQL, selectStatement, null, Collections.singletonList(1)); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), is("SELECT table_y.id FROM table_y WHERE table_y.id=?")); + SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT table_y.id FROM table_y WHERE table_y.id=?", + DatabaseType.MySQL, selectStatement, null, Collections.singletonList(1)); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT table_y.id FROM table_y WHERE table_y.id=?")); } @Test @@ -105,7 +114,7 @@ public void assertRewriteForTableName() { selectStatement.getSqlTokens().add(new TableToken(31, 0, "table_x")); selectStatement.getSqlTokens().add(new TableToken(47, 0, "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT table_x.id, x.name FROM table_x x WHERE table_x.id=? AND x.name=?", DatabaseType.MySQL, selectStatement, null, parameters); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), is("SELECT table_1.id, x.name FROM table_1 x WHERE table_1.id=? AND x.name=?")); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT table_1.id, x.name FROM table_1 x WHERE table_1.id=? AND x.name=?")); } @Test @@ -115,7 +124,7 @@ public void assertRewriteForOrderByAndGroupByDerivedColumns() { itemsToken.getItems().addAll(Arrays.asList("x.id as ORDER_BY_DERIVED_0", "x.name as GROUP_BY_DERIVED_0")); selectStatement.getSqlTokens().add(itemsToken); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.age FROM table_x x GROUP BY x.id ORDER BY x.name", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), is("SELECT x.age, x.id as ORDER_BY_DERIVED_0, x.name as GROUP_BY_DERIVED_0 FROM table_1 x GROUP BY x.id ORDER BY x.name")); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT x.age, x.id as ORDER_BY_DERIVED_0, x.name as GROUP_BY_DERIVED_0 FROM table_1 x GROUP BY x.id ORDER BY x.name")); } @Test @@ -125,7 +134,7 @@ public void assertRewriteForAggregationDerivedColumns() { itemsToken.getItems().addAll(Arrays.asList("COUNT(x.age) as AVG_DERIVED_COUNT_0", "SUM(x.age) as AVG_DERIVED_SUM_0")); selectStatement.getSqlTokens().add(itemsToken); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT AVG(x.age) FROM table_x x", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), is("SELECT AVG(x.age), COUNT(x.age) as AVG_DERIVED_COUNT_0, SUM(x.age) as AVG_DERIVED_SUM_0 FROM table_1 x")); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT AVG(x.age), COUNT(x.age) as AVG_DERIVED_COUNT_0, SUM(x.age) as AVG_DERIVED_SUM_0 FROM table_1 x")); } @Test @@ -146,7 +155,7 @@ public void assertRewriteForAutoGeneratedKeyColumn() { tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine( shardingRule, "INSERT INTO table_x (name, age) VALUES (?, ?)", DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.singletonList(shardingCondition)), parameters); - assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null).getSql(), is("INSERT INTO table_1 (name, age, id) VALUES (?, ?, ?)")); + assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, dataSourcePropertyManager).getSql(), is("INSERT INTO table_1 (name, age, id) VALUES (?, ?, ?)")); } @Test @@ -171,7 +180,7 @@ public void assertRewriteForAutoGeneratedKeyColumnWithoutColumnsWithParameter() tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine( shardingRule, "INSERT INTO `table_x` VALUES (?)", DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.singletonList(shardingCondition)), parameters); - assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null).getSql(), is("INSERT INTO table_1(name, id) VALUES (?, ?)")); + assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, dataSourcePropertyManager).getSql(), is("INSERT INTO table_1(name, id) VALUES (?, ?)")); } @Test @@ -193,7 +202,7 @@ public void assertRewriteForAutoGeneratedKeyColumnWithoutColumnsWithoutParameter tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine( shardingRule, "INSERT INTO `table_x` VALUES (10)", DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.singletonList(shardingCondition)), Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null).getSql(), is("INSERT INTO table_1(name, id) VALUES (10, 1)")); + assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, dataSourcePropertyManager).getSql(), is("INSERT INTO table_1(name, id) VALUES (10, 1)")); } @Test @@ -218,7 +227,7 @@ public void assertRewriteColumnWithoutColumnsWithoutParameter() { tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine( shardingRule, "INSERT INTO `table_x` VALUES (10, 1)", DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.singletonList(shardingCondition)), parameters); - assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null).getSql(), is("INSERT INTO table_1(name, id) VALUES (10, 1)")); + assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, dataSourcePropertyManager).getSql(), is("INSERT INTO table_1(name, id) VALUES (10, 1)")); } @Test @@ -243,7 +252,7 @@ public void assertRewriteColumnWithoutColumnsWithParameter() { tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine( shardingRule, "INSERT INTO `table_x` VALUES (?, ?)", DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.singletonList(shardingCondition)), parameters); - assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null).getSql(), is("INSERT INTO table_1(name, id) VALUES (?, ?)")); + assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, dataSourcePropertyManager).getSql(), is("INSERT INTO table_1(name, id) VALUES (?, ?)")); } @Test @@ -255,7 +264,7 @@ public void assertRewriteForLimit() { selectStatement.getSqlTokens().add(new OffsetToken(33, 2)); selectStatement.getSqlTokens().add(new RowCountToken(36, 2)); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id FROM table_x x LIMIT 2, 2", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), is("SELECT x.id FROM table_1 x LIMIT 0, 4")); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT x.id FROM table_1 x LIMIT 0, 4")); } @Test @@ -268,7 +277,7 @@ public void assertRewriteForRowNum() { selectStatement.getSqlTokens().add(new RowCountToken(98, 4)); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=4) t WHERE t.rownum_>0")); } @@ -282,7 +291,7 @@ public void assertRewriteForTopAndRowNumber() { selectStatement.getSqlTokens().add(new RowCountToken(26, 4)); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>0")); } @@ -297,7 +306,7 @@ public void assertRewriteForLimitForMemoryGroupBy() { selectStatement.getSqlTokens().add(new OffsetToken(33, 2)); selectStatement.getSqlTokens().add(new RowCountToken(36, 2)); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id FROM table_x x LIMIT 2, 2", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), is("SELECT x.id FROM table_1 x LIMIT 0, 2147483647")); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT x.id FROM table_1 x LIMIT 0, 2147483647")); } @Test @@ -312,7 +321,7 @@ public void assertRewriteForRowNumForMemoryGroupBy() { selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC, Optional.absent())); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=2147483647) t WHERE t.rownum_>0")); } @@ -328,7 +337,7 @@ public void assertRewriteForTopAndRowNumberForMemoryGroupBy() { selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC, Optional.absent())); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT * FROM (SELECT TOP(2147483647) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>0")); } @@ -341,7 +350,7 @@ public void assertRewriteForLimitForNotRewriteLimit() { selectStatement.getSqlTokens().add(new OffsetToken(33, 2)); selectStatement.getSqlTokens().add(new RowCountToken(36, 2)); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id FROM table_x x LIMIT 2, 2", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, null).getSql(), is("SELECT x.id FROM table_1 x LIMIT 2, 2")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT x.id FROM table_1 x LIMIT 2, 2")); } @Test @@ -354,7 +363,7 @@ public void assertRewriteForRowNumForNotRewriteLimit() { selectStatement.getSqlTokens().add(new RowCountToken(98, 4)); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, null).getSql(), + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=4) t WHERE t.rownum_>2")); } @@ -368,7 +377,7 @@ public void assertRewriteForTopAndRowNumberForNotRewriteLimit() { selectStatement.getSqlTokens().add(new RowCountToken(26, 4)); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, null).getSql(), + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>2")); } @@ -380,7 +389,7 @@ public void assertRewriteForDerivedOrderBy() { selectStatement.getSqlTokens().add(new TableToken(25, 0, "table_x")); selectStatement.getSqlTokens().add(new OrderByToken(61)); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id, x.name FROM table_x x GROUP BY x.id, x.name DESC", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null).getSql(), is("SELECT x.id, x.name FROM table_1 x GROUP BY x.id, x.name DESC ORDER BY id ASC,name DESC ")); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, dataSourcePropertyManager).getSql(), is("SELECT x.id, x.name FROM table_1 x GROUP BY x.id, x.name DESC ORDER BY id ASC,name DESC ")); } @Test @@ -398,7 +407,7 @@ public void assertGenerateSQL() { SQLBuilder sqlBuilder = sqlRewriteEngine.rewrite(true); TableUnit tableUnit = new TableUnit("db0"); tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x")); - assertThat(sqlRewriteEngine.generateSQL(tableUnit, sqlBuilder).getSql(), is("SELECT table_x.id, x.name FROM table_x x, table_y y WHERE table_x.id=? AND x.name=?")); + assertThat(sqlRewriteEngine.generateSQL(tableUnit, sqlBuilder, dataSourcePropertyManager).getSql(), is("SELECT table_x.id, x.name FROM table_x x, table_y y WHERE table_x.id=? AND x.name=?")); } @Test @@ -408,7 +417,7 @@ public void assertSchemaTokenRewriteForTableName() { selectStatement.getSqlTokens().add(new TableToken(18, 0, "table_x")); selectStatement.getSqlTokens().add(new SchemaToken(29, "table_x", "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW CREATE TABLE table_x ON table_x", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW CREATE TABLE table_y ON db0")); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW CREATE TABLE table_y ON actual_db")); } @Test @@ -416,7 +425,7 @@ public void assertIndexTokenForIndexNameTableName() { selectStatement.getSqlTokens().add(new IndexToken(13, "index_name", "table_x")); selectStatement.getSqlTokens().add(new TableToken(27, 0, "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "CREATE INDEX index_name ON table_x ('column')", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule).getSql(), is("CREATE INDEX index_name_table_1 ON table_1 ('column')")); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("CREATE INDEX index_name_table_1 ON table_1 ('column')")); } @Test @@ -424,104 +433,105 @@ public void assertIndexTokenForIndexNameTableNameWithoutLogicTableName() { selectStatement.getSqlTokens().add(new IndexToken(13, "logic_index", "")); selectStatement.getSqlTokens().add(new TableToken(28, 0, "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "CREATE INDEX index_names ON table_x ('column')", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule).getSql(), is("CREATE INDEX logic_index_table_1 ON table_1 ('column')")); + assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("CREATE INDEX logic_index_table_1 ON table_1 ('column')")); } @Test public void assertTableTokenWithoutBackQuoteForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, 0, "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM table_x", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_1")); } @Test public void assertTableTokenWithoutBackQuoteFromSchemaForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, 0, "table_x")); + showTablesStatement.getSqlTokens().add(new SchemaToken(31, "'sharding_db'", "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM table_x FROM 'sharding_db'", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1 FROM 'sharding_db'")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, new LinkedHashMap(){{put("table_x", "table_x");}}, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_x FROM actual_db")); } @Test public void assertTableTokenWithBackQuoteForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, 0, "`table_x`")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM `table_x`", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_1")); } @Test public void assertTableTokenWithBackQuoteFromSchemaForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, 0, "`table_x`")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM `table_x` FROM 'sharding_db'", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1 FROM 'sharding_db'")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_1 FROM 'sharding_db'")); } @Test public void assertTableTokenWithSchemaForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, "sharding_db".length() + 1, "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM sharding_db.table_x", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_1")); } @Test public void assertTableTokenWithSchemaFromSchemaForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, "sharding_db".length() + 1, "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM sharding_db.table_x FROM sharding_db", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1 FROM sharding_db")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_1 FROM sharding_db")); } @Test public void assertTableTokenWithBackQuoteWithSchemaForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, "sharding_db".length() + 1, "`table_x`")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM sharding_db.`table_x`", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_1")); } @Test public void assertTableTokenWithBackQuoteWithSchemaFromSchemaForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, "sharding_db".length() + 1, "`table_x`")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM sharding_db.`table_x` FROM sharding_db", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1 FROM sharding_db")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_1 FROM sharding_db")); } @Test public void assertTableTokenWithSchemaWithBackQuoteForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, "`sharding_db`".length() + 1, "`table_x`")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM `sharding_db`.`table_x`", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_1")); } @Test public void assertTableTokenWithSchemaWithBackQuoteFromSchemaForShow() { showTablesStatement.getSqlTokens().add(new TableToken(18, "`sharding_db`".length() + 1, "`table_x`")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SHOW COLUMNS FROM `sharding_db`.`table_x` FROM sharding_db", DatabaseType.MySQL, showTablesStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SHOW COLUMNS FROM table_1 FROM sharding_db")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SHOW COLUMNS FROM table_1 FROM sharding_db")); } @Test public void assertTableTokenWithSchemaForSelect() { selectStatement.getSqlTokens().add(new TableToken(14, "sharding_db".length() + 1, "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT * FROM sharding_db.table_x", DatabaseType.MySQL, selectStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("SELECT * FROM table_1")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("SELECT * FROM table_1")); } @Test public void assertTableTokenWithSchemaForInsert() { insertStatement.getSqlTokens().add(new TableToken(12, "sharding_db".length() + 1, "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "INSERT INTO sharding_db.table_x (order_id, user_id, status) values (1, 1, 'OK')", DatabaseType.MySQL, insertStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("INSERT INTO table_1 (order_id, user_id, status) values (1, 1, 'OK')")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("INSERT INTO table_1 (order_id, user_id, status) values (1, 1, 'OK')")); } @Test public void assertTableTokenWithSchemaForUpdate() { dmlStatement.getSqlTokens().add(new TableToken(7, "`sharding_db`".length() + 1, "table_x")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "UPDATE `sharding_db`.table_x SET user_id=1 WHERE order_id=1", DatabaseType.MySQL, dmlStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("UPDATE table_1 SET user_id=1 WHERE order_id=1")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("UPDATE table_1 SET user_id=1 WHERE order_id=1")); } @Test public void assertTableTokenWithSchemaForDelete() { dmlStatement.getSqlTokens().add(new TableToken(12, "`sharding_db`".length() + 1, "`table_x`")); SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "DELETE FROM `sharding_db`.`table_x` WHERE user_id=1", DatabaseType.MySQL, dmlStatement, null, Collections.emptyList()); - assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule).getSql(), is("DELETE FROM table_1 WHERE user_id=1")); + assertThat(rewriteEngine.rewrite(false).toSQL(null, tableTokens, shardingRule, dataSourcePropertyManager).getSql(), is("DELETE FROM table_1 WHERE user_id=1")); } } \ No newline at end of file diff --git a/sharding-core/src/test/java/io/shardingsphere/core/routing/DatabaseTest.java b/sharding-core/src/test/java/io/shardingsphere/core/routing/DatabaseTest.java index 065c8bb98ed96..a677f26c4678b 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/routing/DatabaseTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/routing/DatabaseTest.java @@ -24,9 +24,11 @@ import io.shardingsphere.core.api.config.strategy.HintShardingStrategyConfiguration; import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.fixture.OrderDatabaseHintShardingAlgorithm; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.rule.ShardingRule; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import javax.sql.DataSource; import java.util.Collection; @@ -43,6 +45,8 @@ public class DatabaseTest { private Map dataSourceMap; + private DataSourcePropertyManager dataSourcePropertyManager; + private ShardingRule shardingRule; @Before @@ -53,6 +57,7 @@ public void setRouteRuleContext() { dataSourceMap.put("ds_0", null); dataSourceMap.put("ds_1", null); shardingRule = new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()); + dataSourcePropertyManager = Mockito.mock(DataSourcePropertyManager.class); } @Test @@ -72,7 +77,7 @@ public void assertHintSQL() { @Test public void assertDatabaseAllRoutingSQL() { String originSql = "select * from tesT"; - SQLRouteResult actual = new StatementRoutingEngine(shardingRule, null, DatabaseType.MySQL, false).route(originSql); + SQLRouteResult actual = new StatementRoutingEngine(shardingRule, null, DatabaseType.MySQL, false, dataSourcePropertyManager).route(originSql); assertThat(actual.getExecutionUnits().size(), is(1)); Set actualDataSources = new HashSet<>(Collections2.transform(actual.getExecutionUnits(), new Function() { @@ -94,7 +99,7 @@ public String apply(final SQLExecutionUnit input) { } private void assertTarget(final String originSql, final String targetDataSource) { - SQLRouteResult actual = new StatementRoutingEngine(shardingRule, null, DatabaseType.MySQL, false).route(originSql); + SQLRouteResult actual = new StatementRoutingEngine(shardingRule, null, DatabaseType.MySQL, false, dataSourcePropertyManager).route(originSql); assertThat(actual.getExecutionUnits().size(), is(1)); Set actualDataSources = new HashSet<>(Collections2.transform(actual.getExecutionUnits(), new Function() { diff --git a/sharding-core/src/test/resources/parser/alter_user.xml b/sharding-core/src/test/resources/parser/alter_user.xml new file mode 100644 index 0000000000000..b55d32e64a5fb --- /dev/null +++ b/sharding-core/src/test/resources/parser/alter_user.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/sharding-core/src/test/resources/parser/create_user.xml b/sharding-core/src/test/resources/parser/create_user.xml new file mode 100644 index 0000000000000..d9916afebebda --- /dev/null +++ b/sharding-core/src/test/resources/parser/create_user.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/sharding-core/src/test/resources/parser/deny_user.xml b/sharding-core/src/test/resources/parser/deny_user.xml new file mode 100644 index 0000000000000..6151c50919e9d --- /dev/null +++ b/sharding-core/src/test/resources/parser/deny_user.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/sharding-core/src/test/resources/parser/drop_user.xml b/sharding-core/src/test/resources/parser/drop_user.xml new file mode 100644 index 0000000000000..710db9a6106aa --- /dev/null +++ b/sharding-core/src/test/resources/parser/drop_user.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/sharding-core/src/test/resources/parser/grant_user.xml b/sharding-core/src/test/resources/parser/grant_user.xml new file mode 100644 index 0000000000000..9fd56ad5801cf --- /dev/null +++ b/sharding-core/src/test/resources/parser/grant_user.xml @@ -0,0 +1,37 @@ + + + + +
+ + + + + + + +
+ + + + + + + + + +
+ + + + + + + +
+ + + + + + diff --git a/sharding-core/src/test/resources/parser/rename_user.xml b/sharding-core/src/test/resources/parser/rename_user.xml new file mode 100644 index 0000000000000..a59050790b6b0 --- /dev/null +++ b/sharding-core/src/test/resources/parser/rename_user.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-core/src/test/resources/parser/revoke_user.xml b/sharding-core/src/test/resources/parser/revoke_user.xml new file mode 100644 index 0000000000000..b621eb7c9a2a2 --- /dev/null +++ b/sharding-core/src/test/resources/parser/revoke_user.xml @@ -0,0 +1,30 @@ + + + + +
+ + + + + + + + + +
+ + + + + + + +
+ + + + + + + diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/ShardingContext.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/ShardingContext.java index 6638875d75d8b..01e6350283087 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/ShardingContext.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/ShardingContext.java @@ -20,6 +20,7 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.executor.ExecutorEngine; import io.shardingsphere.core.metadata.ShardingMetaData; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.rule.ShardingRule; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -39,6 +40,8 @@ public final class ShardingContext { private final Map dataSourceMap; + private final DataSourcePropertyManager dataSourcePropertyManager; + private final ShardingRule shardingRule; private final DatabaseType databaseType; @@ -48,4 +51,16 @@ public final class ShardingContext { private final ShardingMetaData shardingMetaData; private final boolean showSQL; + + public ShardingContext(final Map dataSourceMap, final ShardingRule shardingRule, + final DatabaseType databaseType, final ExecutorEngine executorEngine, final ShardingMetaData shardingMetaData, + final boolean showSQL) { + this.dataSourceMap = dataSourceMap; + this.shardingRule = shardingRule; + this.databaseType = databaseType; + this.executorEngine = executorEngine; + this.shardingMetaData = shardingMetaData; + this.showSQL = showSQL; + this.dataSourcePropertyManager = new DataSourcePropertyManager(dataSourceMap, databaseType); + } } diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatement.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatement.java index a2368fdde1440..722df53cf2474 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatement.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/statement/ShardingPreparedStatement.java @@ -123,7 +123,8 @@ public ShardingPreparedStatement(final ShardingConnection connection, final Stri this.sql = sql; ShardingContext shardingContext = connection.getShardingContext(); routingEngine = new PreparedStatementRoutingEngine( - sql, shardingContext.getShardingRule(), shardingContext.getShardingMetaData(), shardingContext.getDatabaseType(), shardingContext.isShowSQL()); + sql, shardingContext.getShardingRule(), shardingContext.getShardingMetaData(), shardingContext.getDatabaseType(), + shardingContext.isShowSQL(), shardingContext.getDataSourcePropertyManager()); } @Override diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatement.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatement.java index f1ef5aa97e7c2..e30195c6e53e1 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatement.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/statement/ShardingStatement.java @@ -234,7 +234,8 @@ private void sqlRoute(final String sql) { EventBusInstance.getInstance().post(event); try { routeResult = new StatementRoutingEngine(shardingContext.getShardingRule(), - shardingContext.getShardingMetaData(), shardingContext.getDatabaseType(), shardingContext.isShowSQL()) + shardingContext.getShardingMetaData(), shardingContext.getDatabaseType(), + shardingContext.isShowSQL(), shardingContext.getDataSourcePropertyManager()) .route(sql); // CHECKSTYLE:OFF } catch (final Exception ex) { diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/api/ShardingDataSourceFactoryTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/api/ShardingDataSourceFactoryTest.java index 0681f7c337b92..fe1974253971c 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/api/ShardingDataSourceFactoryTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/api/ShardingDataSourceFactoryTest.java @@ -75,6 +75,7 @@ private Map getDataSourceMap() throws SQLException { ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(resultSet); when(resultSet.next()).thenReturn(false); Map result = new HashMap<>(1); + when(statement.getConnection().getMetaData().getURL()).thenReturn("jdbc:h2:mem:demo_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"); result.put("ds", dataSource); return result; } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/BatchPreparedStatementExecutorTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/BatchPreparedStatementExecutorTest.java index f5737de4c8ed0..d417c82ab48aa 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/BatchPreparedStatementExecutorTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/BatchPreparedStatementExecutorTest.java @@ -149,7 +149,7 @@ private Collection createPreparedStatementUnits(fina SQLBuilder sqlBuilder = new SQLBuilder(); sqlBuilder.appendLiterals(sql); BatchPreparedStatementUnit batchPreparedStatementUnit = - new BatchPreparedStatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(null, Collections.emptyMap(), null)), preparedStatement); + new BatchPreparedStatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(null, Collections.emptyMap(), null, null)), preparedStatement); batchPreparedStatementUnit.getSqlExecutionUnit().getSqlUnit().getParameterSets().clear(); for (int i = 0; i < addBatchTimes; i++) { batchPreparedStatementUnit.getSqlExecutionUnit().getSqlUnit().getParameterSets().add(Collections.singletonList(i + 1)); diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/PreparedStatementExecutorTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/PreparedStatementExecutorTest.java index 7557f2a4c3692..f96012daf518b 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/PreparedStatementExecutorTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/PreparedStatementExecutorTest.java @@ -377,7 +377,7 @@ private Collection createPreparedStatementUnits(final Str Collection result = new LinkedList<>(); SQLBuilder sqlBuilder = new SQLBuilder(); sqlBuilder.appendLiterals(sql); - result.add(new PreparedStatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(null, Collections.emptyMap(), null)), preparedStatement)); + result.add(new PreparedStatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(null, Collections.emptyMap(), null, null)), preparedStatement)); return result; } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/StatementExecutorTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/StatementExecutorTest.java index 02119ca24630b..f4937b390188e 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/StatementExecutorTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/executor/type/StatementExecutorTest.java @@ -482,7 +482,7 @@ private Collection createStatementUnits(final String sql, final S Collection result = new LinkedList<>(); SQLBuilder sqlBuilder = new SQLBuilder(); sqlBuilder.appendLiterals(sql); - result.add(new StatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(null, Collections.emptyMap(), null)), statement)); + result.add(new StatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(null, Collections.emptyMap(), null, null)), statement)); return result; } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/fixture/TestDataSource.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/fixture/TestDataSource.java index b4c277d10c962..e492ec17ac63e 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/fixture/TestDataSource.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/fixture/TestDataSource.java @@ -60,6 +60,7 @@ public Connection getConnection() throws SQLException { DatabaseMetaData metaData = Mockito.mock(DatabaseMetaData.class); when(metaData.getDatabaseProductName()).thenReturn("H2"); when(result.getMetaData()).thenReturn(metaData); + when(result.getMetaData().getURL()).thenReturn("jdbc:h2:mem:demo_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"); if (throwExceptionWhenClosing) { doThrow(SQLException.class).when(result).close(); } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnectionTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnectionTest.java index fcc444ec3aa7e..9fc490d621c2f 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnectionTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnectionTest.java @@ -21,6 +21,7 @@ import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration; import io.shardingsphere.core.api.config.ShardingRuleConfiguration; import io.shardingsphere.core.api.config.TableRuleConfiguration; +import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.fixture.TestDataSource; import io.shardingsphere.core.jdbc.core.ShardingContext; import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource; @@ -74,7 +75,7 @@ public void setUp() { ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()); ListeningExecutorService executorService = Mockito.mock(ListeningExecutorService.class); ShardingMetaData shardingMetaData = new JDBCShardingMetaData(executorService, dataSourceMap, shardingRule, null); - ShardingContext shardingContext = new ShardingContext(dataSourceMap, shardingRule, null, null, shardingMetaData, false); + ShardingContext shardingContext = new ShardingContext(dataSourceMap, shardingRule, DatabaseType.H2, null, shardingMetaData, false); connection = new ShardingConnection(shardingContext); } diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/datasource/ShardingDataSourceTest.java b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/datasource/ShardingDataSourceTest.java index 50cd137e583a0..8a7eb696c60de 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/datasource/ShardingDataSourceTest.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/core/jdbc/core/datasource/ShardingDataSourceTest.java @@ -131,6 +131,7 @@ private DataSource mockDataSource(final String dataBaseProductName) throws SQLEx when(statement.getConnection()).thenReturn(connection); when(statement.getConnection().getMetaData().getTables(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(resultSet); + when(statement.getConnection().getMetaData().getURL()).thenReturn("jdbc:h2:mem:demo_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"); return result; } diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/MergeEventListenerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/MergeEventListenerTest.java index 44fa16c821baa..c0cbc51fd2ca3 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/MergeEventListenerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/MergeEventListenerTest.java @@ -81,7 +81,7 @@ public void setUp() { dataSourceMap.put("ds_0", null); dataSourceMap.put("ds_1", null); ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()); - shardingContext = new ShardingContext(dataSourceMap, shardingRule, DatabaseType.MySQL, null, null, true); + shardingContext = new ShardingContext(dataSourceMap, null, shardingRule, DatabaseType.MySQL, null, null, true); mergeEngine = new DALMergeEngine(null, null, new ShowDatabasesStatement(), null); } diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/SqlRoutingEventListenerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/SqlRoutingEventListenerTest.java index 25b43a8aac53e..123cd75d3dcf6 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/SqlRoutingEventListenerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/SqlRoutingEventListenerTest.java @@ -32,12 +32,15 @@ import io.shardingsphere.core.jdbc.core.connection.ShardingConnection; import io.shardingsphere.core.jdbc.core.statement.ShardingPreparedStatement; import io.shardingsphere.core.jdbc.core.statement.ShardingStatement; +import io.shardingsphere.core.property.DataSourceProperty; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.rule.ShardingRule; import io.shardingsphere.core.util.EventBusInstance; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.Mockito; import javax.sql.DataSource; import java.lang.reflect.Field; @@ -76,7 +79,7 @@ public void setUp() { dataSourceMap.put("ds_0", null); dataSourceMap.put("ds_1", null); ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()); - shardingContext = new ShardingContext(dataSourceMap, shardingRule, DatabaseType.MySQL, null, null, true); + shardingContext = new ShardingContext(dataSourceMap, null, shardingRule, DatabaseType.MySQL, null, null, true); } @Test diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java index 7ef53ed585c2a..102646fde8bd1 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLExecuteBackendHandler.java @@ -54,7 +54,7 @@ public SQLExecuteBackendHandler(final String sql, final DatabaseType databaseTyp @Override protected SQLRouteResult doSqlShardingRoute() { StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), - RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), isShowSQL()); + RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), isShowSQL(), RuleRegistry.getInstance().getDataSourcePropertyManager()); return routingEngine.route(getSql()); } diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java index 9af8cce6b0ab8..762ff5e846caa 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/SQLPacketsBackendHandler.java @@ -104,7 +104,7 @@ public CommandResponsePackets execute() { } } - protected CommandResponsePackets executeForMasterSlave() { + private CommandResponsePackets executeForMasterSlave() { MasterSlaveRouter masterSlaveRouter = new MasterSlaveRouter(RuleRegistry.getInstance().getMasterSlaveRule()); SQLStatement sqlStatement = new SQLJudgeEngine(rebuilder.sql()).judge(); String dataSourceName = masterSlaveRouter.route(sqlStatement.getType()).iterator().next(); @@ -124,8 +124,9 @@ protected CommandResponsePackets executeForMasterSlave() { return merge(sqlStatement, packets, queryResults); } - protected CommandResponsePackets executeForSharding() { - StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), databaseType, showSQL); + private CommandResponsePackets executeForSharding() { + StatementRoutingEngine routingEngine = new StatementRoutingEngine(RuleRegistry.getInstance().getShardingRule(), + RuleRegistry.getInstance().getShardingMetaData(), databaseType, showSQL, RuleRegistry.getInstance().getDataSourcePropertyManager()); SQLRouteResult routeResult = routingEngine.route(rebuilder.sql()); if (routeResult.getExecutionUnits().isEmpty()) { return new CommandResponsePackets(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, "")); @@ -158,7 +159,7 @@ protected CommandResponsePackets executeForSharding() { return result; } - protected CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets, final List queryResults) { + private CommandResponsePackets merge(final SQLStatement sqlStatement, final List packets, final List queryResults) { CommandResponsePackets headPackets = new CommandResponsePackets(); for (CommandResponsePackets each : packets) { headPackets.addPacket(each.getHeadPacket()); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java index a8ceb7bec9b07..a004c7a8a436d 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/common/StatementExecuteBackendHandler.java @@ -69,16 +69,12 @@ public StatementExecuteBackendHandler(final List pre @Override protected SQLRouteResult doSqlShardingRoute() { PreparedStatementRoutingEngine routingEngine = new PreparedStatementRoutingEngine(getSql(), - RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), getDatabaseType(), isShowSQL()); + RuleRegistry.getInstance().getShardingRule(), RuleRegistry.getInstance().getShardingMetaData(), + getDatabaseType(), isShowSQL(), RuleRegistry.getInstance().getDataSourcePropertyManager()); return routingEngine.route(getComStmtExecuteParameters()); } - /** - * Get PreparedStatement Parameter values. - * - * @return parameter value list - */ - public List getComStmtExecuteParameters() { + private List getComStmtExecuteParameters() { List result = new ArrayList<>(32); for (PreparedStatementParameter each : preparedStatementParameters) { result.add(each.getValue()); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java index 9aac3ced21f34..6470f98138562 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/config/RuleRegistry.java @@ -21,10 +21,12 @@ import com.google.common.base.Preconditions; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; +import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.constant.ShardingProperties; import io.shardingsphere.core.constant.ShardingPropertiesConstant; import io.shardingsphere.core.constant.TransactionType; import io.shardingsphere.core.metadata.ShardingMetaData; +import io.shardingsphere.core.property.DataSourcePropertyManager; import io.shardingsphere.core.rule.DataSourceParameter; import io.shardingsphere.core.rule.MasterSlaveRule; import io.shardingsphere.core.rule.ProxyAuthority; @@ -86,6 +88,8 @@ public final class RuleRegistry implements AutoCloseable { private OrchestrationFacade orchestrationFacade; + private DataSourcePropertyManager dataSourcePropertyManager; + /** * Initialize rule registry. * @@ -110,6 +114,7 @@ public void init(final YamlProxyConfiguration yamlProxyConfiguration) { maxWorkingThreads = yamlProxyConfiguration.getMaxWorkingThreads(); executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(maxWorkingThreads)); showSQL = shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW); + dataSourcePropertyManager = new DataSourcePropertyManager(dataSourceMap, DatabaseType.MySQL); shardingMetaData = new ProxyShardingMetaData(executorService, dataSourceMap); if (!isOnlyMasterSlave) { shardingMetaData.init(shardingRule); diff --git a/sharding-sql-test/src/main/resources/sql/dcl/alter_user.xml b/sharding-sql-test/src/main/resources/sql/dcl/alter_user.xml new file mode 100644 index 0000000000000..e82316b018faa --- /dev/null +++ b/sharding-sql-test/src/main/resources/sql/dcl/alter_user.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sharding-sql-test/src/main/resources/sql/dcl/create_user.xml b/sharding-sql-test/src/main/resources/sql/dcl/create_user.xml new file mode 100644 index 0000000000000..a6a01db003ac9 --- /dev/null +++ b/sharding-sql-test/src/main/resources/sql/dcl/create_user.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dcl/deny_user.xml b/sharding-sql-test/src/main/resources/sql/dcl/deny_user.xml new file mode 100644 index 0000000000000..a02bb16ffc882 --- /dev/null +++ b/sharding-sql-test/src/main/resources/sql/dcl/deny_user.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dcl/drop_user.xml b/sharding-sql-test/src/main/resources/sql/dcl/drop_user.xml new file mode 100644 index 0000000000000..b8e76f2c78737 --- /dev/null +++ b/sharding-sql-test/src/main/resources/sql/dcl/drop_user.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dcl/grant_user.xml b/sharding-sql-test/src/main/resources/sql/dcl/grant_user.xml new file mode 100644 index 0000000000000..5f146d96a3d3f --- /dev/null +++ b/sharding-sql-test/src/main/resources/sql/dcl/grant_user.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dcl/rename_user.xml b/sharding-sql-test/src/main/resources/sql/dcl/rename_user.xml new file mode 100644 index 0000000000000..2dd4677da0b89 --- /dev/null +++ b/sharding-sql-test/src/main/resources/sql/dcl/rename_user.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sharding-sql-test/src/main/resources/sql/dcl/revoke_user.xml b/sharding-sql-test/src/main/resources/sql/dcl/revoke_user.xml new file mode 100644 index 0000000000000..3f02a954be348 --- /dev/null +++ b/sharding-sql-test/src/main/resources/sql/dcl/revoke_user.xml @@ -0,0 +1,9 @@ + + + + + + + + +