You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/08/23 08:20:43 UTC
[shardingsphere] branch master updated: Issue10968 (#11944)
This is an automated email from the ASF dual-hosted git repository.
menghaoran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new ca0e09b Issue10968 (#11944)
ca0e09b is described below
commit ca0e09b3016c8a484891b2d6b0d1ed8a7c374b38
Author: fwhdzh <43...@users.noreply.github.com>
AuthorDate: Mon Aug 23 16:20:11 2021 +0800
Issue10968 (#11944)
* Migrate YAML Configuration change history from
4.1.2.Configuration Manual->YAML Configuration->Change History to
7.2.API Change History->7.2.1.ShardingSphere-JDBC->YAML Configuration;
* Add content of change history
* add unit test of ShardingGeneratedKeyInsertValueParameterRewriter.
* define the constant as static final.
* Add test code of ShardingPaginationParameterRewriter and GeneratedKeyAssignmentTokenGenerator.
remove ShardingGeneratedKeyInsertValueParameterRewriterTest from *.rewrite to *.rewrite.parameter.
* add final.
---
...neratedKeyInsertValueParameterRewriterTest.java | 114 +++++++++++++++++++++
.../ShardingPaginationParameterRewriterTest.java | 105 +++++++++++++++++++
.../GeneratedKeyAssignmentTokenGeneratorTest.java | 69 +++++++++++++
3 files changed, 288 insertions(+)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingGeneratedKeyInsertValueParameterRewriterTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingGeneratedKeyInsertValueParameterRewriterTest.java
new file mode 100644
index 0000000..1f4e39f
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingGeneratedKeyInsertValueParameterRewriterTest.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shardingsphere.sharding.rewrite.parameter;
+
+import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.rewrite.parameter.builder.ParameterBuilder;
+import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
+import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
+import org.apache.shardingsphere.sharding.rewrite.parameter.impl.ShardingGeneratedKeyInsertValueParameterRewriter;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.hasItem;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.anyCollection;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class ShardingGeneratedKeyInsertValueParameterRewriterTest {
+
+ private static final int TEST_PARAMETER_COUNT = 3;
+
+ private static final String TEST_GENERATED_VALUE = "testGeneratedValue";
+
+ @Test
+ public void assertIsNeedRewrite() {
+ ShardingGeneratedKeyInsertValueParameterRewriter shardingGeneratedKeyInsertValueParameterRewriter = new ShardingGeneratedKeyInsertValueParameterRewriter();
+ SelectStatementContext selectStatementContext = mock(SelectStatementContext.class);
+ assertFalse(shardingGeneratedKeyInsertValueParameterRewriter.isNeedRewrite(selectStatementContext));
+ InsertStatementContext insertStatementContext = mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
+ assertFalse(shardingGeneratedKeyInsertValueParameterRewriter.isNeedRewrite(insertStatementContext));
+ when(insertStatementContext.getGeneratedKeyContext().isPresent()).thenReturn(Boolean.TRUE);
+ assertFalse(shardingGeneratedKeyInsertValueParameterRewriter.isNeedRewrite(insertStatementContext));
+ when(insertStatementContext.getGeneratedKeyContext().get().isGenerated()).thenReturn(Boolean.TRUE);
+ when(insertStatementContext.getGeneratedKeyContext().get().getGeneratedValues().isEmpty()).thenReturn(Boolean.TRUE);
+ assertFalse(shardingGeneratedKeyInsertValueParameterRewriter.isNeedRewrite(insertStatementContext));
+ when(insertStatementContext.getGeneratedKeyContext().get().getGeneratedValues().isEmpty()).thenReturn(Boolean.FALSE);
+ assertTrue(shardingGeneratedKeyInsertValueParameterRewriter.isNeedRewrite(insertStatementContext));
+ }
+
+ @Test
+ public void assertRewrite() {
+ InsertStatementContext insertStatementContext = getInsertStatementContext();
+ ParameterBuilder groupedParameterBuilder = getParameterBuilder();
+ ShardingGeneratedKeyInsertValueParameterRewriter shardingGeneratedKeyInsertValueParameterRewriter = new ShardingGeneratedKeyInsertValueParameterRewriter();
+ shardingGeneratedKeyInsertValueParameterRewriter.rewrite(groupedParameterBuilder, insertStatementContext, null);
+ assertThat(((GroupedParameterBuilder) groupedParameterBuilder).getParameterBuilders().get(0).getAddedIndexAndParameters().get(TEST_PARAMETER_COUNT), hasItem(TEST_GENERATED_VALUE));
+ }
+
+ private ParameterBuilder getParameterBuilder() {
+ StandardParameterBuilder standardParameterBuilder = mock(StandardParameterBuilder.class);
+ Map<Integer, Collection<Object>> addedIndexAndParameters = new HashMap<>();
+ when(standardParameterBuilder.getAddedIndexAndParameters()).thenReturn(addedIndexAndParameters);
+ doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(final InvocationOnMock invocation) throws Throwable {
+ int index = invocation.getArgument(0);
+ Collection<Object> parameters = invocation.getArgument(1);
+ addedIndexAndParameters.put(index, parameters);
+ return null;
+ }
+ }).when(standardParameterBuilder).addAddedParameters(anyInt(), anyCollection());
+ List<StandardParameterBuilder> parameterBuildersList = new ArrayList<>();
+ parameterBuildersList.add(standardParameterBuilder);
+ GroupedParameterBuilder result = mock(GroupedParameterBuilder.class);
+ when(result.getParameterBuilders()).thenReturn(parameterBuildersList);
+ return result;
+ }
+
+ private InsertStatementContext getInsertStatementContext() {
+ InsertStatementContext result = mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
+ when(result.getGeneratedKeyContext().isPresent()).thenReturn(Boolean.TRUE);
+ when(result.getGeneratedKeyContext().get().getColumnName()).thenReturn("testColumnName");
+ Collection<Comparable<?>> generatedValuesCollection = new LinkedList<>();
+ generatedValuesCollection.add(TEST_GENERATED_VALUE);
+ when(result.getGeneratedKeyContext().get().getGeneratedValues()).thenReturn(generatedValuesCollection);
+ List<Object> groupedParameter = new LinkedList<>();
+ groupedParameter.add("testGroupedParameter");
+ List<List<Object>> groupedParametersList = new LinkedList<>();
+ groupedParametersList.add(groupedParameter);
+ when(result.getGroupedParameters()).thenReturn(groupedParametersList);
+ when(result.getInsertValueContexts().get(0).getParameterCount()).thenReturn(TEST_PARAMETER_COUNT);
+ return result;
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingPaginationParameterRewriterTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingPaginationParameterRewriterTest.java
new file mode 100644
index 0000000..0e107ac
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingPaginationParameterRewriterTest.java
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shardingsphere.sharding.rewrite.parameter;
+
+import org.apache.shardingsphere.infra.binder.segment.select.pagination.PaginationContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.sharding.rewrite.parameter.impl.ShardingPaginationParameterRewriter;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.util.Optional;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class ShardingPaginationParameterRewriterTest {
+
+ private static final int TEST_OFFSET_PARAMETER_INDEX = 3;
+
+ private static final int TEST_ROW_COUNT_PARAMETER_INDEX = 5;
+
+ private static final long TEST_REVISED_OFFSET = 4;
+
+ private static final long TEST_REVISED_ROW_COUNT = 6;
+
+ private static boolean addOffsetParametersFlag = Boolean.FALSE;
+
+ private static boolean addRowCountParameterFlag = Boolean.FALSE;
+
+ @Test
+ public void assertIsNeedRewrite() {
+ ShardingPaginationParameterRewriter shardingPaginationParameterRewriter = new ShardingPaginationParameterRewriter();
+ RouteContext routeContext = mock(RouteContext.class);
+ shardingPaginationParameterRewriter.setRouteContext(routeContext);
+ InsertStatementContext insertStatementContext = mock(InsertStatementContext.class);
+ assertFalse(shardingPaginationParameterRewriter.isNeedRewrite(insertStatementContext));
+ SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+ when(selectStatementContext.getPaginationContext().isHasPagination()).thenReturn(Boolean.FALSE);
+ assertFalse(shardingPaginationParameterRewriter.isNeedRewrite(selectStatementContext));
+ when(selectStatementContext.getPaginationContext().isHasPagination()).thenReturn(Boolean.TRUE);
+ when(routeContext.isSingleRouting()).thenReturn(Boolean.TRUE);
+ assertFalse(shardingPaginationParameterRewriter.isNeedRewrite(selectStatementContext));
+ when(routeContext.isSingleRouting()).thenReturn(Boolean.FALSE);
+ assertTrue(shardingPaginationParameterRewriter.isNeedRewrite(selectStatementContext));
+ }
+
+ @Test
+ public void assertRewrite() {
+ addOffsetParametersFlag = false;
+ addRowCountParameterFlag = false;
+ StandardParameterBuilder standardParameterBuilder = mock(StandardParameterBuilder.class);
+ doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(final InvocationOnMock invocation) throws Throwable {
+ int index = invocation.getArgument(0);
+ long parameter = invocation.getArgument(1);
+ if (index == TEST_OFFSET_PARAMETER_INDEX
+ && parameter == TEST_REVISED_OFFSET) {
+ addOffsetParametersFlag = true;
+ }
+ if (index == TEST_ROW_COUNT_PARAMETER_INDEX
+ && parameter == TEST_REVISED_ROW_COUNT) {
+ addRowCountParameterFlag = true;
+ }
+ return null;
+ }
+ }).when(standardParameterBuilder).addReplacedParameters(anyInt(), anyLong());
+ SelectStatementContext selectStatementContext = mock(SelectStatementContext.class);
+ PaginationContext pagination = mock(PaginationContext.class);
+ when(pagination.getOffsetParameterIndex()).thenReturn(Optional.of(TEST_OFFSET_PARAMETER_INDEX));
+ when(pagination.getRowCountParameterIndex()).thenReturn(Optional.of(TEST_ROW_COUNT_PARAMETER_INDEX));
+ when(pagination.getRevisedOffset()).thenReturn(TEST_REVISED_OFFSET);
+ when(pagination.getRevisedRowCount(selectStatementContext)).thenReturn(TEST_REVISED_ROW_COUNT);
+ when(selectStatementContext.getPaginationContext()).thenReturn(pagination);
+ ShardingPaginationParameterRewriter shardingPaginationParameterRewriter = new ShardingPaginationParameterRewriter();
+ shardingPaginationParameterRewriter.rewrite(standardParameterBuilder, selectStatementContext, null);
+ assertTrue(addOffsetParametersFlag);
+ assertTrue(addRowCountParameterFlag);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyAssignmentTokenGeneratorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyAssignmentTokenGeneratorTest.java
new file mode 100644
index 0000000..8d12d6b
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyAssignmentTokenGeneratorTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.shardingsphere.sharding.rewrite.token;
+
+import org.apache.shardingsphere.infra.binder.segment.insert.keygen.GeneratedKeyContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.keygen.GeneratedKeyAssignmentTokenGenerator;
+import org.apache.shardingsphere.sharding.rewrite.token.pojo.LiteralGeneratedKeyAssignmentToken;
+import org.apache.shardingsphere.sharding.rewrite.token.pojo.ParameterMarkerGeneratedKeyAssignmentToken;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class GeneratedKeyAssignmentTokenGeneratorTest {
+
+ private static final int TEST_STOP_INDEX = 2;
+
+ private static final String TEST_COLUMN_NAME = "testColumnName";
+
+ private static final int TEST_GENERATED_VALUE = 4;
+
+ @Test
+ public void assertGenerateSQLToken() {
+ GeneratedKeyContext generatedKeyContext = mock(GeneratedKeyContext.class, RETURNS_DEEP_STUBS);
+ when(generatedKeyContext.getColumnName()).thenReturn(TEST_COLUMN_NAME);
+ Collection<Comparable<?>> testGeneratedValuesCollection = new LinkedList<>();
+ testGeneratedValuesCollection.add(TEST_GENERATED_VALUE);
+ when(generatedKeyContext.getGeneratedValues()).thenReturn(testGeneratedValuesCollection);
+ InsertStatementContext insertStatementContext = mock(InsertStatementContext.class);
+ when(insertStatementContext.getGeneratedKeyContext()).thenReturn(Optional.of(generatedKeyContext));
+ MySQLInsertStatement insertStatement = mock(MySQLInsertStatement.class);
+ when(insertStatementContext.getSqlStatement()).thenReturn(insertStatement);
+ SetAssignmentSegment setAssignmentSegment = mock(SetAssignmentSegment.class);
+ when(setAssignmentSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX);
+ when(insertStatement.getSetAssignment()).thenReturn(Optional.of(setAssignmentSegment));
+ List<Object> testParameters = new LinkedList<>();
+ GeneratedKeyAssignmentTokenGenerator generatedKeyAssignmentTokenGenerator = new GeneratedKeyAssignmentTokenGenerator();
+ generatedKeyAssignmentTokenGenerator.setParameters(testParameters);
+ assertThat(generatedKeyAssignmentTokenGenerator.generateSQLToken(insertStatementContext), instanceOf(LiteralGeneratedKeyAssignmentToken.class));
+ testParameters.add("testObject");
+ assertThat(generatedKeyAssignmentTokenGenerator.generateSQLToken(insertStatementContext), instanceOf(ParameterMarkerGeneratedKeyAssignmentToken.class));
+ }
+}