diff --git a/sharding-core/src/main/antlr4/imports/PostgreSQLDALStatement.g4 b/sharding-core/src/main/antlr4/imports/PostgreSQLDALStatement.g4 index 04c2d42f04c51..2b5e2d5dacd87 100644 --- a/sharding-core/src/main/antlr4/imports/PostgreSQLDALStatement.g4 +++ b/sharding-core/src/main/antlr4/imports/PostgreSQLDALStatement.g4 @@ -3,10 +3,26 @@ grammar PostgreSQLDALStatement; import PostgreSQLKeyword, Keyword, BaseRule, DataType, Symbol; show - : SHOW showParam + : SHOW (ALL | ID) + ; + +setParam + : SET scope? setClause + ; + +scope + : SESSION | LOCAL ; -showParam - : ALL - | ID +setClause + : TIME ZONE timeZoneType + | ID (TO | EQ_) (STRING | DEFAULT) ; + +timeZoneType + : NUMBER | LOCAL | DEFAULT + ; + +resetParam + : RESET (ALL | ID) + ; \ No newline at end of file diff --git a/sharding-core/src/main/antlr4/io/shardingsphere/core/parsing/antlr/autogen/PostgreSQLStatement.g4 b/sharding-core/src/main/antlr4/io/shardingsphere/core/parsing/antlr/autogen/PostgreSQLStatement.g4 index 97587dfe2f6fd..320a4dc3379f3 100644 --- a/sharding-core/src/main/antlr4/io/shardingsphere/core/parsing/antlr/autogen/PostgreSQLStatement.g4 +++ b/sharding-core/src/main/antlr4/io/shardingsphere/core/parsing/antlr/autogen/PostgreSQLStatement.g4 @@ -35,4 +35,6 @@ execute | alterRoleResetConfig | dropRole | show + | setParam + | resetParam ; diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/SQLParsingEngine.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/SQLParsingEngine.java index 62b497ec50b9f..2082af258e0bf 100755 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/SQLParsingEngine.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/SQLParsingEngine.java @@ -62,28 +62,7 @@ public SQLStatement parse(final boolean useCache) { return cachedSQLStatement.get(); } LexerEngine lexerEngine = LexerEngineFactory.newInstance(dbType, sql); - lexerEngine.nextToken(); - Token firstToken = lexerEngine.getCurrentToken(); - SQLStatement result; - - if (PostgreSQLKeyword.SHOW == lexerEngine.getCurrentToken().getType()) { - result = AntlrParsingEngine.parse(dbType, sql, shardingRule, shardingTableMetaData); - return result; - } - - SQLParser sqlParser = SQLParserFactory.newInstance(dbType, lexerEngine.getCurrentToken().getType(), shardingRule, lexerEngine, shardingTableMetaData); - Token currentToken = lexerEngine.getCurrentToken(); - if (firstToken != currentToken) { - if (DDLStatement.isDDL(firstToken.getType(), currentToken.getType())) { - result = AntlrParsingEngine.parse(dbType, sql, shardingRule, shardingTableMetaData); - } else { - result = sqlParser.parse(); - } - } else if (TCLStatement.isTCL(firstToken.getType())) { - result = AntlrParsingEngine.parse(dbType, sql, shardingRule, shardingTableMetaData); - } else { - result = sqlParser.parse(); - } + SQLStatement result = SQLParserFactory.newInstance(dbType, shardingRule, lexerEngine, shardingTableMetaData, sql).parse(); if (useCache) { ParsingResultCache.getInstance().put(sql, result); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/AntlrParsingEngine.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/AntlrParsingEngine.java index 166d0ef23f366..14fb45441d2ab 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/AntlrParsingEngine.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/AntlrParsingEngine.java @@ -24,10 +24,10 @@ import io.shardingsphere.core.parsing.antlr.extractor.statement.SQLStatementExtractorFactory; import io.shardingsphere.core.parsing.antlr.extractor.statement.SQLStatementType; import io.shardingsphere.core.parsing.parser.exception.SQLParsingUnsupportedException; +import io.shardingsphere.core.parsing.parser.sql.SQLParser; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; import io.shardingsphere.core.rule.ShardingRule; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; +import lombok.AllArgsConstructor; import org.antlr.v4.runtime.ParserRuleContext; /** @@ -35,19 +35,19 @@ * * @author duhongjun */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class AntlrParsingEngine { +@AllArgsConstructor +public final class AntlrParsingEngine implements SQLParser { - /** - * Parse SQL. - * - * @param dbType database type - * @param sql SQL - * @param shardingRule sharding rule - * @param shardingTableMetaData sharding table meta data - * @return SQL statement - */ - public static SQLStatement parse(final DatabaseType dbType, final String sql, final ShardingRule shardingRule, final ShardingTableMetaData shardingTableMetaData) { + private DatabaseType dbType; + + private String sql; + + private ShardingRule shardingRule; + + private ShardingTableMetaData shardingTableMetaData; + + @Override + public SQLStatement parse() { ParserRuleContext rootContext = SQLASTParserFactory.newInstance(dbType, sql).execute(); if (null == rootContext) { throw new SQLParsingUnsupportedException(String.format("Unsupported SQL of `%s`", sql)); diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/segment/engine/dialect/postgresql/ShowParamExtractor.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/segment/engine/dialect/postgresql/ShowParamExtractor.java index 79b70e58c2f5c..5c48f60ab82a2 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/segment/engine/dialect/postgresql/ShowParamExtractor.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/segment/engine/dialect/postgresql/ShowParamExtractor.java @@ -1,3 +1,20 @@ +/* + * 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.antlr.extractor.segment.engine.dialect.postgresql; import com.google.common.base.Optional; @@ -8,6 +25,11 @@ import io.shardingsphere.core.parsing.antlr.sql.segment.ShowParamSegment; import org.antlr.v4.runtime.ParserRuleContext; +/** + * PostgreSQL show param extractor. + * + * @author loxp + */ public class ShowParamExtractor implements OptionalSQLSegmentExtractor { @Override diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/SQLStatementType.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/SQLStatementType.java index be9a36ab56efc..99c6ec17b9691 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/SQLStatementType.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/SQLStatementType.java @@ -53,6 +53,10 @@ public enum SQLStatementType { SET_VARIABLE("SetVariable"), + SET_PARAM("SetParam"), // PostgreSQL SET statement + + RESET_PARAM("ResetParam"), + SHOW("Show"); private final String name; diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/DALStatementExtractor.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/DALStatementExtractor.java index f1732caa750d6..12057199742d8 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/DALStatementExtractor.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/DALStatementExtractor.java @@ -1,9 +1,31 @@ +/* + * 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.antlr.extractor.statement.engine.dal; import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.AbstractSQLStatementExtractor; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement; +/** + * DAL statement extractor. + * + * @author loxp + */ public abstract class DALStatementExtractor extends AbstractSQLStatementExtractor { @Override diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLResetParamExtractor.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLResetParamExtractor.java new file mode 100644 index 0000000000000..d6783090ffee1 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLResetParamExtractor.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.antlr.extractor.statement.engine.dal.dialect.postgresql; + +import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.DALStatementExtractor; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.ResetParamStatement; +import io.shardingsphere.core.parsing.parser.sql.SQLStatement; + +/** + * PostgreSQL reset param statement extractor. + * + * @author loxp + */ +public class PostgreSQLResetParamExtractor extends DALStatementExtractor { + + @Override + protected SQLStatement createStatement() { + return new ResetParamStatement(); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLSetParamExtractor.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLSetParamExtractor.java new file mode 100644 index 0000000000000..dfeae1e6fad2d --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLSetParamExtractor.java @@ -0,0 +1,37 @@ +/* + * 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.antlr.extractor.statement.engine.dal.dialect.postgresql; + +import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.DALStatementExtractor; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.SetParamStatement; +import io.shardingsphere.core.parsing.parser.sql.SQLStatement; +import lombok.NoArgsConstructor; + +/** + * PostgreSQL set param statement extractor. + * + * @author loxp + */ +@NoArgsConstructor +public class PostgreSQLSetParamExtractor extends DALStatementExtractor { + + @Override + protected SQLStatement createStatement() { + return new SetParamStatement(); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLShowExtractor.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLShowExtractor.java index 06403de536822..00ae3cbf9bd73 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLShowExtractor.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/engine/dal/dialect/postgresql/PostgreSQLShowExtractor.java @@ -1,3 +1,20 @@ +/* + * 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.antlr.extractor.statement.engine.dal.dialect.postgresql; import io.shardingsphere.core.parsing.antlr.extractor.segment.engine.dialect.postgresql.ShowParamExtractor; @@ -5,6 +22,11 @@ import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.ShowStatement; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; +/** + * PostgreSQL show statement extractor. + * + * @author loxp + */ public final class PostgreSQLShowExtractor extends DALStatementExtractor { public PostgreSQLShowExtractor() { diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/registry/dialect/PostgreSQLStatementExtractorRegistry.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/registry/dialect/PostgreSQLStatementExtractorRegistry.java index 14b26872f5a69..7cff70833aac9 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/registry/dialect/PostgreSQLStatementExtractorRegistry.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/extractor/statement/registry/dialect/PostgreSQLStatementExtractorRegistry.java @@ -19,6 +19,8 @@ import io.shardingsphere.core.parsing.antlr.extractor.statement.SQLStatementExtractor; import io.shardingsphere.core.parsing.antlr.extractor.statement.SQLStatementType; +import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.dialect.postgresql.PostgreSQLResetParamExtractor; +import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.dialect.postgresql.PostgreSQLSetParamExtractor; import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.dal.dialect.postgresql.PostgreSQLShowExtractor; import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.ddl.CreateIndexExtractor; import io.shardingsphere.core.parsing.antlr.extractor.statement.engine.ddl.CreateTableExtractor; @@ -65,6 +67,8 @@ private static void registerTCL() { private static void registerDAL() { EXTRACTORS.put(SQLStatementType.SHOW, new PostgreSQLShowExtractor()); + EXTRACTORS.put(SQLStatementType.SET_PARAM, new PostgreSQLSetParamExtractor()); + EXTRACTORS.put(SQLStatementType.RESET_PARAM, new PostgreSQLResetParamExtractor()); } @Override diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/filler/engnie/ShowParamSegmentFiller.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/filler/engnie/ShowParamSegmentFiller.java index a13076d73256f..670a4235dd687 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/filler/engnie/ShowParamSegmentFiller.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/filler/engnie/ShowParamSegmentFiller.java @@ -1,3 +1,20 @@ +/* + * 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.antlr.filler.engnie; import io.shardingsphere.core.metadata.table.ShardingTableMetaData; @@ -7,6 +24,11 @@ import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.ShowStatement; import io.shardingsphere.core.parsing.parser.sql.SQLStatement; +/** + * Show param segment filler. + * + * @author loxp + */ public class ShowParamSegmentFiller implements SQLSegmentFiller { @Override public void fill(SQLSegment sqlSegment, SQLStatement sqlStatement, ShardingTableMetaData shardingTableMetaData) { diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/sql/segment/ShowParamSegment.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/sql/segment/ShowParamSegment.java index 6a560e6207b2b..03f3dc69728c6 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/sql/segment/ShowParamSegment.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/antlr/sql/segment/ShowParamSegment.java @@ -1,9 +1,31 @@ +/* + * 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.antlr.sql.segment; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; +/** + * Show param segment. + * + * @author loxp + */ @RequiredArgsConstructor @Getter @Setter diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/dialect/postgresql/PostgreSQLKeyword.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/dialect/postgresql/PostgreSQLKeyword.java index 149da2a3718a0..c3c8beea9e1de 100755 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/dialect/postgresql/PostgreSQLKeyword.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/lexer/dialect/postgresql/PostgreSQLKeyword.java @@ -65,5 +65,6 @@ public enum PostgreSQLKeyword implements Keyword { INITIALLY, DEFERRED, IMMEDIATE, - EXTRACT + EXTRACT, + CONSTRAINTS } 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 47efd7a34b8a6..5836922517527 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 @@ -156,7 +156,8 @@ public enum DefaultKeyword implements Keyword { COMMIT, ROLLBACK, SAVEPOINT, - BEGIN, + BEGIN, + TRANSACTION, /* Other Command diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/ResetParamStatement.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/ResetParamStatement.java new file mode 100644 index 0000000000000..cb802b94e6c82 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/ResetParamStatement.java @@ -0,0 +1,28 @@ +/* + * 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.statement; + +import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement; + +/** + * Reset param statement. + * + * @author loxp + */ +public class ResetParamStatement extends DALStatement { +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/SetParamStatement.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/SetParamStatement.java new file mode 100644 index 0000000000000..732d06737beb6 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/SetParamStatement.java @@ -0,0 +1,28 @@ +/* + * 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.statement; + +import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement; + +/** + * Set param statement. + * + * @author loxp + */ +public class SetParamStatement extends DALStatement { +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/ShowStatement.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/ShowStatement.java index dbdfe5096c30f..d0cba24fe9e9d 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/ShowStatement.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/postgresql/statement/ShowStatement.java @@ -1,9 +1,31 @@ +/* + * 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.statement; import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement; import lombok.Getter; import lombok.Setter; +/** + * Show statement. + * + * @author loxp + */ @Getter @Setter public final class ShowStatement extends DALStatement { 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 283c3140084f6..2bb028185695f 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 @@ -19,8 +19,10 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.metadata.table.ShardingTableMetaData; +import io.shardingsphere.core.parsing.antlr.AntlrParsingEngine; import io.shardingsphere.core.parsing.lexer.LexerEngine; import io.shardingsphere.core.parsing.lexer.dialect.mysql.MySQLKeyword; +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.lexer.token.TokenType; @@ -69,30 +71,40 @@ public final class SQLParserFactory { * Create SQL parser. * * @param dbType database type - * @param tokenType token type * @param shardingRule databases and tables sharding rule * @param lexerEngine lexical analysis engine * @param shardingTableMetaData sharding metadata + * @param sql sql to parse * @return SQL parser */ public static SQLParser newInstance( - final DatabaseType dbType, final TokenType tokenType, final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData) { + final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine, final ShardingTableMetaData shardingTableMetaData, final String sql) { + + lexerEngine.nextToken(); + TokenType tokenType = lexerEngine.getCurrentToken().getType(); + if (DQLStatement.isDQL(tokenType)) { return getDQLParser(dbType, shardingRule, lexerEngine, shardingTableMetaData); } if (DMLStatement.isDML(tokenType)) { return getDMLParser(dbType, tokenType, shardingRule, lexerEngine, shardingTableMetaData); } - if (TCLStatement.isTCL(tokenType)) { - return getTCLParser(dbType, shardingRule, lexerEngine); - } if (DALStatement.isDAL(tokenType)) { + if (PostgreSQLKeyword.SHOW == tokenType) { + new AntlrParsingEngine(dbType, sql, shardingRule, shardingTableMetaData); + } return getDALParser(dbType, (Keyword) tokenType, shardingRule, lexerEngine); } lexerEngine.nextToken(); TokenType secondaryTokenType = lexerEngine.getCurrentToken().getType(); + if (DALStatement.isDAL(tokenType, secondaryTokenType)) { + new AntlrParsingEngine(dbType, sql, shardingRule, shardingTableMetaData); + } + if (TCLStatement.isTCL(tokenType)) { + return new AntlrParsingEngine(dbType, sql, shardingRule, shardingTableMetaData); + } if (DDLStatement.isDDL(tokenType, secondaryTokenType)) { - return getDDLParser(dbType, tokenType, shardingRule, lexerEngine); + return new AntlrParsingEngine(dbType, sql, shardingRule, shardingTableMetaData); } if (DCLStatement.isDCL(tokenType, secondaryTokenType)) { return getDCLParser(dbType, tokenType, shardingRule, lexerEngine); @@ -118,45 +130,6 @@ 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 CreateTableParserFactory.newInstance(dbType, shardingRule, lexerEngine); - case ALTER: - return AlterTableParserFactory.newInstance(dbType, shardingRule, lexerEngine); - case DROP: - return DropTableParserFactory.newInstance(dbType, shardingRule, lexerEngine); - case TRUNCATE: - 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); - } - } - - private static SQLParser getTCLParser(final DatabaseType dbType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { - return TCLParserFactory.newInstance(dbType, shardingRule, lexerEngine); - } - private static SQLParser getDALParser(final DatabaseType dbType, final Keyword tokenType, final ShardingRule shardingRule, final LexerEngine lexerEngine) { if (DefaultKeyword.USE == tokenType) { return UseParserFactory.newInstance(dbType, shardingRule, lexerEngine); diff --git a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dal/DALStatement.java b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dal/DALStatement.java index 2b49082904a09..691332dd5b5c5 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dal/DALStatement.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dal/DALStatement.java @@ -37,7 +37,12 @@ @ToString(callSuper = true) public class DALStatement extends AbstractSQLStatement { - private static final Collection STATEMENT_PREFIX = Arrays.asList(DefaultKeyword.USE, DefaultKeyword.DESC, MySQLKeyword.DESCRIBE, MySQLKeyword.SHOW, PostgreSQLKeyword.SHOW); + private static final Collection SINGLE_TOKEN_STATEMENT_PREFIX = Arrays.asList(DefaultKeyword.USE, DefaultKeyword.DESC, MySQLKeyword.DESCRIBE, MySQLKeyword.SHOW, + PostgreSQLKeyword.SHOW, PostgreSQLKeyword.RESET); + + private static final Collection DUAL_TOKEN_PRIMARY_STATEMENT_PREFIX = Arrays.asList(DefaultKeyword.SET); + + private static final Collection DUAL_TOKEN_NOT_SECONDARY_STATEMENT_PREFIX = Arrays.asList(DefaultKeyword.ROLE, DefaultKeyword.TRANSACTION, PostgreSQLKeyword.CONSTRAINTS); public DALStatement() { super(SQLType.DAL); @@ -50,6 +55,17 @@ public DALStatement() { * @return is DAL or not */ public static boolean isDAL(final TokenType tokenType) { - return STATEMENT_PREFIX.contains(tokenType); + return SINGLE_TOKEN_STATEMENT_PREFIX.contains(tokenType); + } + + /** + * Is DAL statement. + * + * @param primaryTokenType primary token type + * @param secondaryTokenType secondary token type + * @return is DAL or not + */ + public static boolean isDAL(final TokenType primaryTokenType, final TokenType secondaryTokenType) { + return DUAL_TOKEN_PRIMARY_STATEMENT_PREFIX.contains(primaryTokenType) && !DUAL_TOKEN_NOT_SECONDARY_STATEMENT_PREFIX.contains(secondaryTokenType); } } 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 331e713057573..ebb37d085f471 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,6 +30,8 @@ import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowTableStatusStatement; import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowTablesStatement; import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.UseStatement; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.ResetParamStatement; +import io.shardingsphere.core.parsing.parser.dialect.postgresql.statement.SetParamStatement; 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; @@ -137,7 +139,8 @@ private RoutingResult route(final SQLStatement sqlStatement, final ShardingCondi routingEngine = new DatabaseBroadcastRoutingEngine(shardingRule); } else if (sqlStatement instanceof DDLStatement || (sqlStatement instanceof DCLStatement && ((DCLStatement) sqlStatement).isGrantForSingleTable())) { routingEngine = new TableBroadcastRoutingEngine(shardingRule, sqlStatement); - } else if (sqlStatement instanceof ShowDatabasesStatement || ((sqlStatement instanceof ShowTablesStatement || sqlStatement instanceof ShowTableStatusStatement) && tableNames.isEmpty())) { + } else if (sqlStatement instanceof ShowDatabasesStatement || ((sqlStatement instanceof ShowTablesStatement || sqlStatement instanceof ShowTableStatusStatement) && tableNames.isEmpty()) + || sqlStatement instanceof SetParamStatement || sqlStatement instanceof ResetParamStatement) { routingEngine = new DatabaseBroadcastRoutingEngine(shardingRule); } else if (sqlStatement instanceof DCLStatement) { routingEngine = new InstanceBroadcastRoutingEngine(shardingRule, shardingDataSourceMetaData); diff --git a/sharding-core/src/test/java/io/shardingsphere/core/parsing/antlr/AntlrIntegrateParsingTest.java b/sharding-core/src/test/java/io/shardingsphere/core/parsing/antlr/AntlrIntegrateParsingTest.java index 57f0451e0f0ec..b660f61833c2b 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/parsing/antlr/AntlrIntegrateParsingTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/parsing/antlr/AntlrIntegrateParsingTest.java @@ -109,8 +109,8 @@ public void assertSupportedSQL() { if (DatabaseType.H2 == databaseType) { execDatabaseType = DatabaseType.MySQL; } - new SQLStatementAssert(AntlrParsingEngine.parse( - execDatabaseType, sql, getShardingRule(), getShardingTableMetaData()), sqlCaseId, sqlCaseType, sqlCasesLoader, parserResultSetLoader).assertSQLStatement(); + new SQLStatementAssert(new AntlrParsingEngine( + execDatabaseType, sql, getShardingRule(), getShardingTableMetaData()).parse(), sqlCaseId, sqlCaseType, sqlCasesLoader, parserResultSetLoader).assertSQLStatement(); } } diff --git a/sharding-core/src/test/java/io/shardingsphere/core/parsing/antlr/ddl/IntegrateDDLParsingCompatTest.java b/sharding-core/src/test/java/io/shardingsphere/core/parsing/antlr/ddl/IntegrateDDLParsingCompatTest.java index 6c0af423746ed..f98569a51b641 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/parsing/antlr/ddl/IntegrateDDLParsingCompatTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/parsing/antlr/ddl/IntegrateDDLParsingCompatTest.java @@ -104,8 +104,8 @@ public void assertSupportedSQL() { if (DatabaseType.H2 == databaseType) { execDatabaseType = DatabaseType.MySQL; } - new SQLStatementAssert(AntlrParsingEngine.parse( - execDatabaseType, sql, getShardingRule(), getShardingTableMetaData()), sqlCaseId, sqlCaseType, sqlCasesLoader, parserResultSetLoader).assertSQLStatement(); + new SQLStatementAssert(new AntlrParsingEngine( + execDatabaseType, sql, getShardingRule(), getShardingTableMetaData()).parse(), sqlCaseId, sqlCaseType, sqlCasesLoader, parserResultSetLoader).assertSQLStatement(); } /** diff --git a/sharding-sql-test/src/main/resources/antlr_supported_sql/postgre/dal/reset.xml b/sharding-sql-test/src/main/resources/antlr_supported_sql/postgre/dal/reset.xml new file mode 100644 index 0000000000000..1be84015116c6 --- /dev/null +++ b/sharding-sql-test/src/main/resources/antlr_supported_sql/postgre/dal/reset.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sharding-sql-test/src/main/resources/antlr_supported_sql/postgre/dal/set.xml b/sharding-sql-test/src/main/resources/antlr_supported_sql/postgre/dal/set.xml new file mode 100644 index 0000000000000..82045c891e4d8 --- /dev/null +++ b/sharding-sql-test/src/main/resources/antlr_supported_sql/postgre/dal/set.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + +