This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 be2ef9a6c69 Add GeneratedValueUtils (#38080)
be2ef9a6c69 is described below

commit be2ef9a6c697b1601a1df11781242405d6a18276
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Feb 18 14:47:38 2026 +0800

    Add GeneratedValueUtils (#38080)
    
    * Add GeneratedValueUtils
    
    * Add GeneratedValueUtils
    
    * Add GeneratedValueUtils
    
    * Add GeneratedValueUtils
---
 .../jdbc/core/statement/GeneratedValueUtils.java   |  56 ++++++++++
 .../statement/ShardingSpherePreparedStatement.java |  18 +---
 .../core/statement/ShardingSphereStatement.java    |  18 +---
 .../core/statement/GeneratedValueUtilsTest.java    | 116 +++++++++++++++++++++
 .../ShardingSpherePreparedStatementTest.java       |  53 +++-------
 .../statement/ShardingSphereStatementTest.java     |  50 ++++-----
 6 files changed, 207 insertions(+), 104 deletions(-)

diff --git 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/GeneratedValueUtils.java
 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/GeneratedValueUtils.java
new file mode 100644
index 00000000000..a3ead3873b1
--- /dev/null
+++ 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/GeneratedValueUtils.java
@@ -0,0 +1,56 @@
+/*
+ * 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.driver.jdbc.core.statement;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Generated value utils.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class GeneratedValueUtils {
+    
+    /**
+     * Get generated value.
+     *
+     * @param resultSet result set
+     * @param generatedKeysColumnName generated keys column name
+     * @param columnName column name
+     * @return generated value
+     * @throws SQLException SQL exception
+     */
+    public static Comparable<?> getGeneratedValue(final ResultSet resultSet, 
final String generatedKeysColumnName, final String columnName) throws 
SQLException {
+        if (null != generatedKeysColumnName) {
+            try {
+                return (Comparable<?>) 
resultSet.getObject(generatedKeysColumnName);
+            } catch (final SQLException ignored) {
+            }
+        }
+        if (null != columnName && !columnName.equals(generatedKeysColumnName)) 
{
+            try {
+                return (Comparable<?>) resultSet.getObject(columnName);
+            } catch (final SQLException ignored) {
+            }
+        }
+        return (Comparable<?>) resultSet.getObject(1);
+    }
+}
diff --git 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index c73e21d3ba0..e3ba83d58cf 100644
--- 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++ 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -305,28 +305,12 @@ public final class ShardingSpherePreparedStatement 
extends AbstractPreparedState
         for (PreparedStatement each : statements) {
             ResultSet resultSet = each.getGeneratedKeys();
             while (resultSet.next()) {
-                generatedValues.add(getGeneratedValue(resultSet, 
generatedKeysColumnName, columnName));
+                
generatedValues.add(GeneratedValueUtils.getGeneratedValue(resultSet, 
generatedKeysColumnName, columnName));
             }
         }
         return new GeneratedKeysResultSet(generatedKeysColumnName, 
generatedValues.iterator(), this);
     }
     
-    private Comparable<?> getGeneratedValue(final ResultSet resultSet, final 
String generatedKeysColumnName, final String columnName) throws SQLException {
-        if (null != generatedKeysColumnName) {
-            try {
-                return (Comparable<?>) 
resultSet.getObject(generatedKeysColumnName);
-            } catch (final SQLException ignored) {
-            }
-        }
-        if (null != columnName && !columnName.equals(generatedKeysColumnName)) 
{
-            try {
-                return (Comparable<?>) resultSet.getObject(columnName);
-            } catch (final SQLException ignored) {
-            }
-        }
-        return (Comparable<?>) resultSet.getObject(1);
-    }
-    
     private String getGeneratedKeysColumnName(final String columnName) {
         Optional<DialectGeneratedKeyOption> generatedKeyOption = new 
DatabaseTypeRegistry(usedDatabase.getProtocolType()).getDialectDatabaseMetaData().getGeneratedKeyOption();
         return generatedKeyOption.isPresent() ? 
generatedKeyOption.get().getColumnName() : columnName;
diff --git 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index f1be7e71f6b..c43c4cc9479 100644
--- 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++ 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -352,28 +352,12 @@ public final class ShardingSphereStatement extends 
AbstractStatementAdapter {
         for (Statement each : statements) {
             ResultSet resultSet = each.getGeneratedKeys();
             while (resultSet.next()) {
-                generatedValues.add(getGeneratedValue(resultSet, 
generatedKeysColumnName, columnName));
+                
generatedValues.add(GeneratedValueUtils.getGeneratedValue(resultSet, 
generatedKeysColumnName, columnName));
             }
         }
         return new GeneratedKeysResultSet(generatedKeysColumnName, 
generatedValues.iterator(), this);
     }
     
-    private Comparable<?> getGeneratedValue(final ResultSet resultSet, final 
String generatedKeysColumnName, final String columnName) throws SQLException {
-        if (null != generatedKeysColumnName) {
-            try {
-                return (Comparable<?>) 
resultSet.getObject(generatedKeysColumnName);
-            } catch (final SQLException ignored) {
-            }
-        }
-        if (null != columnName && !columnName.equals(generatedKeysColumnName)) 
{
-            try {
-                return (Comparable<?>) resultSet.getObject(columnName);
-            } catch (final SQLException ignored) {
-            }
-        }
-        return (Comparable<?>) resultSet.getObject(1);
-    }
-    
     private Optional<GeneratedKeyContext> findGeneratedKey() {
         SQLStatementContext sqlStatementContext = 
queryContext.getSqlStatementContext();
         return sqlStatementContext instanceof InsertStatementContext ? 
((InsertStatementContext) sqlStatementContext).getGeneratedKeyContext() : 
Optional.empty();
diff --git 
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/GeneratedValueUtilsTest.java
 
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/GeneratedValueUtilsTest.java
new file mode 100644
index 00000000000..9460a0a04cc
--- /dev/null
+++ 
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/GeneratedValueUtilsTest.java
@@ -0,0 +1,116 @@
+/*
+ * 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.driver.jdbc.core.statement;
+
+import org.junit.jupiter.api.Test;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+class GeneratedValueUtilsTest {
+    
+    @Test
+    void assertGetGeneratedValueByGeneratedKeysColumn() throws SQLException {
+        ResultSet resultSet = mock(ResultSet.class);
+        when(resultSet.getObject("generated_key")).thenReturn(1L);
+        Comparable<?> actual = 
GeneratedValueUtils.getGeneratedValue(resultSet, "generated_key", "id");
+        assertThat(actual, is(1L));
+        verify(resultSet).getObject("generated_key");
+        verifyNoMoreInteractions(resultSet);
+    }
+    
+    @Test
+    void assertGetGeneratedValueByColumnNameWhenGeneratedKeysColumnThrows() 
throws SQLException {
+        ResultSet resultSet = mock(ResultSet.class);
+        
when(resultSet.getObject("generated_key")).thenThrow(SQLException.class);
+        when(resultSet.getObject("id")).thenReturn(2L);
+        Comparable<?> actual = 
GeneratedValueUtils.getGeneratedValue(resultSet, "generated_key", "id");
+        assertThat(actual, is(2L));
+        verify(resultSet).getObject("generated_key");
+        verify(resultSet).getObject("id");
+        verifyNoMoreInteractions(resultSet);
+    }
+    
+    @Test
+    void assertGetGeneratedValueByColumnNameWhenGeneratedKeysColumnIsNull() 
throws SQLException {
+        ResultSet resultSet = mock(ResultSet.class);
+        when(resultSet.getObject("id")).thenReturn(2L);
+        Comparable<?> actual = 
GeneratedValueUtils.getGeneratedValue(resultSet, null, "id");
+        assertThat(actual, is(2L));
+        verify(resultSet).getObject("id");
+        verifyNoMoreInteractions(resultSet);
+    }
+    
+    @Test
+    void 
assertGetGeneratedValueByColumnIndexWhenColumnNameEqualsGeneratedKeysColumn() 
throws SQLException {
+        ResultSet resultSet = mock(ResultSet.class);
+        when(resultSet.getObject("id")).thenThrow(SQLException.class);
+        when(resultSet.getObject(1)).thenReturn(3L);
+        Comparable<?> actual = 
GeneratedValueUtils.getGeneratedValue(resultSet, "id", "id");
+        assertThat(actual, is(3L));
+        verify(resultSet).getObject("id");
+        verify(resultSet).getObject(1);
+        verifyNoMoreInteractions(resultSet);
+    }
+    
+    @Test
+    void assertGetGeneratedValueByColumnIndexWhenColumnNameThrows() throws 
SQLException {
+        ResultSet resultSet = mock(ResultSet.class);
+        
when(resultSet.getObject("generated_key")).thenThrow(SQLException.class);
+        when(resultSet.getObject("id")).thenThrow(SQLException.class);
+        when(resultSet.getObject(1)).thenReturn(3L);
+        Comparable<?> actual = 
GeneratedValueUtils.getGeneratedValue(resultSet, "generated_key", "id");
+        assertThat(actual, is(3L));
+        verify(resultSet).getObject("generated_key");
+        verify(resultSet).getObject("id");
+        verify(resultSet).getObject(1);
+        verifyNoMoreInteractions(resultSet);
+    }
+    
+    @Test
+    void 
assertGetGeneratedValueByColumnIndexWhenGeneratedKeysAndColumnNameAreNull() 
throws SQLException {
+        ResultSet resultSet = mock(ResultSet.class);
+        when(resultSet.getObject(1)).thenReturn(3L);
+        Comparable<?> actual = 
GeneratedValueUtils.getGeneratedValue(resultSet, null, null);
+        assertThat(actual, is(3L));
+        verify(resultSet).getObject(1);
+        verifyNoMoreInteractions(resultSet);
+    }
+    
+    @Test
+    void assertGetGeneratedValueWithSQLException() throws SQLException {
+        ResultSet resultSet = mock(ResultSet.class);
+        
when(resultSet.getObject("generated_key")).thenThrow(SQLException.class);
+        when(resultSet.getObject("id")).thenThrow(SQLException.class);
+        when(resultSet.getObject(1)).thenThrow(new SQLException("index not 
found"));
+        SQLException actual = assertThrows(SQLException.class, () -> 
GeneratedValueUtils.getGeneratedValue(resultSet, "generated_key", "id"));
+        assertThat(actual.getMessage(), is("index not found"));
+        verify(resultSet).getObject("generated_key");
+        verify(resultSet).getObject("id");
+        verify(resultSet).getObject(1);
+        verifyNoMoreInteractions(resultSet);
+    }
+}
diff --git 
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatementTest.java
 
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatementTest.java
index 9771043b7e2..88897b80408 100644
--- 
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatementTest.java
+++ 
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatementTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.driver.jdbc.core.statement;
 
+import lombok.SneakyThrows;
 import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
 import 
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
 import 
org.apache.shardingsphere.driver.jdbc.core.resultset.GeneratedKeysResultSet;
@@ -84,47 +85,17 @@ class ShardingSpherePreparedStatementTest {
     }
     
     @Test
-    void assertGetGeneratedKeysByColumnName() throws SQLException, 
ReflectiveOperationException {
-        ShardingSpherePreparedStatement preparedStatement = 
createPreparedStatement(TypedSPILoader.getService(DatabaseType.class, "SQL92"));
-        ResultSet generatedKeys = mock(ResultSet.class);
-        when(generatedKeys.next()).thenReturn(true, false);
-        when(generatedKeys.getObject("id")).thenReturn(1L);
-        setInsertStatementContext(preparedStatement, "id");
-        addPreparedStatement(preparedStatement, generatedKeys);
-        ResultSet actual = preparedStatement.getGeneratedKeys();
-        assertTrue(actual.next());
-        assertThat(actual.getObject(1), is(1L));
-        verify(generatedKeys).getObject("id");
-        verify(generatedKeys, never()).getObject(1);
-    }
-    
-    @Test
-    void assertGetGeneratedKeysByColumnIndexWhenColumnNameMissing() throws 
SQLException, ReflectiveOperationException {
-        ResultSet generatedKeys = mock(ResultSet.class);
-        when(generatedKeys.next()).thenReturn(true, false);
-        when(generatedKeys.getObject("id")).thenThrow(new SQLException("Column 
not found"));
-        when(generatedKeys.getObject(1)).thenReturn(2L);
-        ShardingSpherePreparedStatement preparedStatement = 
createPreparedStatement(TypedSPILoader.getService(DatabaseType.class, "SQL92"));
-        setInsertStatementContext(preparedStatement, "id");
-        addPreparedStatement(preparedStatement, generatedKeys);
-        ResultSet actual = preparedStatement.getGeneratedKeys();
-        assertTrue(actual.next());
-        assertThat(actual.getObject(1), is(2L));
-        verify(generatedKeys).getObject("id");
-        verify(generatedKeys).getObject(1);
-    }
-    
-    @Test
-    void assertGetGeneratedKeysByDialectGeneratedKeyColumn() throws 
SQLException, ReflectiveOperationException {
+    void assertGetGeneratedKeysByDialectGeneratedKeyColumn() throws 
SQLException {
         ShardingSpherePreparedStatement preparedStatement = 
createPreparedStatement(TypedSPILoader.getService(DatabaseType.class, "MySQL"));
         ResultSet generatedKeys = mock(ResultSet.class);
         when(generatedKeys.next()).thenReturn(true, false);
         when(generatedKeys.getObject("GENERATED_KEY")).thenReturn(3L);
-        setInsertStatementContext(preparedStatement, "id");
+        setInsertStatementContext(preparedStatement);
         addPreparedStatement(preparedStatement, generatedKeys);
-        ResultSet actual = preparedStatement.getGeneratedKeys();
-        assertTrue(actual.next());
-        assertThat(actual.getObject(1), is(3L));
+        try (ResultSet actual = preparedStatement.getGeneratedKeys()) {
+            assertTrue(actual.next());
+            assertThat(actual.getObject(1), is(3L));
+        }
         verify(generatedKeys).getObject("GENERATED_KEY");
         verify(generatedKeys, never()).getObject("id");
         verify(generatedKeys, never()).getObject(1);
@@ -151,23 +122,25 @@ class ShardingSpherePreparedStatementTest {
         return result;
     }
     
-    private void setInsertStatementContext(final 
ShardingSpherePreparedStatement preparedStatement, final String columnName) 
throws ReflectiveOperationException {
+    @SneakyThrows(ReflectiveOperationException.class)
+    private void setInsertStatementContext(final 
ShardingSpherePreparedStatement preparedStatement) {
         GeneratedKeyContext generatedKeyContext = 
mock(GeneratedKeyContext.class);
-        when(generatedKeyContext.getColumnName()).thenReturn(columnName);
+        when(generatedKeyContext.getColumnName()).thenReturn("id");
         
when(generatedKeyContext.getGeneratedValues()).thenReturn(Collections.emptyList());
         InsertStatementContext insertStatementContext = 
mock(InsertStatementContext.class);
         
when(insertStatementContext.getGeneratedKeyContext()).thenReturn(Optional.of(generatedKeyContext));
         
Plugins.getMemberAccessor().set(ShardingSpherePreparedStatement.class.getDeclaredField("sqlStatementContext"),
 preparedStatement, insertStatementContext);
     }
     
-    private void addPreparedStatement(final ShardingSpherePreparedStatement 
preparedStatement, final ResultSet generatedKeys) throws 
ReflectiveOperationException, SQLException {
+    private void addPreparedStatement(final ShardingSpherePreparedStatement 
preparedStatement, final ResultSet generatedKeys) throws SQLException {
         PreparedStatement statement = mock(PreparedStatement.class);
         when(statement.getGeneratedKeys()).thenReturn(generatedKeys);
         getPreparedStatements(preparedStatement).add(statement);
     }
     
+    @SneakyThrows(ReflectiveOperationException.class)
     @SuppressWarnings("unchecked")
-    private Collection<PreparedStatement> getPreparedStatements(final 
ShardingSpherePreparedStatement preparedStatement) throws 
ReflectiveOperationException {
+    private Collection<PreparedStatement> getPreparedStatements(final 
ShardingSpherePreparedStatement preparedStatement) {
         Field statementsField = 
ShardingSpherePreparedStatement.class.getDeclaredField("statements");
         return (Collection<PreparedStatement>) 
Plugins.getMemberAccessor().get(statementsField, preparedStatement);
     }
diff --git 
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatementTest.java
 
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatementTest.java
index eeef347136c..0e8b1ece6cc 100644
--- 
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatementTest.java
+++ 
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatementTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.driver.jdbc.core.statement;
 
+import lombok.SneakyThrows;
 import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
 import 
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
 import 
org.apache.shardingsphere.infra.binder.context.segment.insert.keygen.GeneratedKeyContext;
@@ -56,53 +57,40 @@ import static org.mockito.Mockito.when;
 class ShardingSphereStatementTest {
     
     @Test
-    void assertGetGeneratedKeysByColumnName() throws SQLException, 
ReflectiveOperationException {
-        ShardingSphereStatement statement = 
createStatement(TypedSPILoader.getService(DatabaseType.class, "SQL92"));
-        ResultSet generatedKeys = mock(ResultSet.class);
-        when(generatedKeys.next()).thenReturn(true, false);
-        when(generatedKeys.getObject("id")).thenReturn(1L);
-        setInsertStatementContext(statement, "id");
-        addStatement(statement, generatedKeys);
-        ResultSet actual = statement.getGeneratedKeys();
-        assertTrue(actual.next());
-        assertThat(actual.getObject(1), is(1L));
-        verify(generatedKeys).getObject("id");
-        verify(generatedKeys, never()).getObject(1);
-    }
-    
-    @Test
-    void assertGetGeneratedKeysByColumnIndexWhenColumnNameMissing() throws 
SQLException, ReflectiveOperationException {
+    void assertGetGeneratedKeysByColumnIndexWhenColumnNameMissing() throws 
SQLException {
         ResultSet generatedKeys = mock(ResultSet.class);
         when(generatedKeys.next()).thenReturn(true, false);
         when(generatedKeys.getObject("id")).thenThrow(new SQLException("Column 
not found"));
         when(generatedKeys.getObject(1)).thenReturn(2L);
         ShardingSphereStatement statement = 
createStatement(TypedSPILoader.getService(DatabaseType.class, "SQL92"));
-        setInsertStatementContext(statement, "id");
+        setInsertStatementContext(statement);
         addStatement(statement, generatedKeys);
-        ResultSet actual = statement.getGeneratedKeys();
-        assertTrue(actual.next());
-        assertThat(actual.getObject(1), is(2L));
+        try (ResultSet actual = statement.getGeneratedKeys()) {
+            assertTrue(actual.next());
+            assertThat(actual.getObject(1), is(2L));
+        }
         verify(generatedKeys).getObject("id");
         verify(generatedKeys).getObject(1);
     }
     
     @Test
-    void assertGetGeneratedKeysByDialectGeneratedKeyColumn() throws 
SQLException, ReflectiveOperationException {
+    void assertGetGeneratedKeysByDialectGeneratedKeyColumn() throws 
SQLException {
         ShardingSphereStatement statement = 
createStatement(TypedSPILoader.getService(DatabaseType.class, "MySQL"));
         ResultSet generatedKeys = mock(ResultSet.class);
         when(generatedKeys.next()).thenReturn(true, false);
         when(generatedKeys.getObject("GENERATED_KEY")).thenReturn(3L);
-        setInsertStatementContext(statement, "id");
+        setInsertStatementContext(statement);
         addStatement(statement, generatedKeys);
-        ResultSet actual = statement.getGeneratedKeys();
-        assertTrue(actual.next());
-        assertThat(actual.getObject(1), is(3L));
+        try (ResultSet actual = statement.getGeneratedKeys()) {
+            assertTrue(actual.next());
+            assertThat(actual.getObject(1), is(3L));
+        }
         verify(generatedKeys).getObject("GENERATED_KEY");
         verify(generatedKeys, never()).getObject("id");
         verify(generatedKeys, never()).getObject(1);
     }
     
-    private ShardingSphereStatement createStatement(final DatabaseType 
databaseType) throws SQLException {
+    private ShardingSphereStatement createStatement(final DatabaseType 
databaseType) {
         ShardingSphereMetaData metaData = createMetaData(databaseType);
         ShardingSphereConnection connection = 
mock(ShardingSphereConnection.class, RETURNS_DEEP_STUBS);
         
when(connection.getContextManager().getMetaDataContexts().getMetaData()).thenReturn(metaData);
@@ -123,9 +111,10 @@ class ShardingSphereStatementTest {
         return result;
     }
     
-    private void setInsertStatementContext(final ShardingSphereStatement 
statement, final String columnName) throws ReflectiveOperationException {
+    @SneakyThrows(ReflectiveOperationException.class)
+    private void setInsertStatementContext(final ShardingSphereStatement 
statement) {
         GeneratedKeyContext generatedKeyContext = 
mock(GeneratedKeyContext.class);
-        when(generatedKeyContext.getColumnName()).thenReturn(columnName);
+        when(generatedKeyContext.getColumnName()).thenReturn("id");
         
when(generatedKeyContext.getGeneratedValues()).thenReturn(Collections.emptyList());
         InsertStatementContext insertStatementContext = 
mock(InsertStatementContext.class);
         
when(insertStatementContext.getGeneratedKeyContext()).thenReturn(Optional.of(generatedKeyContext));
@@ -135,14 +124,15 @@ class ShardingSphereStatementTest {
         
Plugins.getMemberAccessor().set(ShardingSphereStatement.class.getDeclaredField("returnGeneratedKeys"),
 statement, true);
     }
     
-    private void addStatement(final ShardingSphereStatement 
shardingSphereStatement, final ResultSet generatedKeys) throws 
ReflectiveOperationException, SQLException {
+    private void addStatement(final ShardingSphereStatement 
shardingSphereStatement, final ResultSet generatedKeys) throws SQLException {
         Statement statement = mock(Statement.class);
         when(statement.getGeneratedKeys()).thenReturn(generatedKeys);
         getStatements(shardingSphereStatement).add(statement);
     }
     
+    @SneakyThrows(ReflectiveOperationException.class)
     @SuppressWarnings("unchecked")
-    private Collection<Statement> getStatements(final ShardingSphereStatement 
statement) throws ReflectiveOperationException {
+    private Collection<Statement> getStatements(final ShardingSphereStatement 
statement) {
         Field statementsField = 
ShardingSphereStatement.class.getDeclaredField("statements");
         return (Collection<Statement>) 
Plugins.getMemberAccessor().get(statementsField, statement);
     }

Reply via email to