HHH-12699 Resolve once and for all the method mapping in ResultSet proxy
This commit is contained in:
parent
377b3d1fbe
commit
4c22b52f80
|
@ -6,19 +6,23 @@
|
|||
*/
|
||||
package org.hibernate.engine.jdbc;
|
||||
|
||||
import static org.hibernate.internal.CoreLogging.messageLogger;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
import static org.hibernate.internal.CoreLogging.messageLogger;
|
||||
|
||||
/**
|
||||
* A proxy for a ResultSet delegate, responsible for locally caching the columnName-to-columnIndex resolution that
|
||||
* has been found to be inefficient in a few vendor's drivers (i.e., Oracle and Postgres).
|
||||
|
@ -31,9 +35,29 @@ public class ResultSetWrapperProxy implements InvocationHandler {
|
|||
|
||||
private static final SqlExceptionHelper SQL_EXCEPTION_HELPER = new SqlExceptionHelper( false );
|
||||
|
||||
private static final Map<ResultSetMethodKey, Method> NAME_TO_INDEX_METHOD_MAPPING;
|
||||
|
||||
private final ResultSet rs;
|
||||
private final ColumnNameCache columnNameCache;
|
||||
|
||||
static {
|
||||
Map<ResultSetMethodKey, Method> nameToIndexMethodMapping = new HashMap<>();
|
||||
for ( Method method : ResultSet.class.getDeclaredMethods() ) {
|
||||
if ( isFirstArgColumnLabel( method ) ) {
|
||||
try {
|
||||
nameToIndexMethodMapping.put(
|
||||
new ResultSetMethodKey( method.getName(), method.getParameterTypes() ),
|
||||
locateCorrespondingColumnIndexMethod( method )
|
||||
);
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
LOG.unableToSwitchToMethodUsingColumnIndex( method );
|
||||
}
|
||||
}
|
||||
}
|
||||
NAME_TO_INDEX_METHOD_MAPPING = Collections.unmodifiableMap( nameToIndexMethodMapping );
|
||||
}
|
||||
|
||||
private ResultSetWrapperProxy(ResultSet rs, ColumnNameCache columnNameCache) {
|
||||
this.rs = rs;
|
||||
this.columnNameCache = columnNameCache;
|
||||
|
@ -64,13 +88,13 @@ public class ResultSetWrapperProxy implements InvocationHandler {
|
|||
return findColumn( (String) args[0] );
|
||||
}
|
||||
|
||||
if ( isFirstArgColumnLabel( method, args ) ) {
|
||||
if ( isFirstArgColumnLabel( method ) ) {
|
||||
Method columnIndexMethod = NAME_TO_INDEX_METHOD_MAPPING.get( new ResultSetMethodKey( method.getName(), method.getParameterTypes() ) );
|
||||
if ( columnIndexMethod != null ) {
|
||||
try {
|
||||
final Integer columnIndex = findColumn( (String) args[0] );
|
||||
return invokeMethod(
|
||||
locateCorrespondingColumnIndexMethod( method ),
|
||||
buildColumnIndexMethodArgs( args, columnIndex )
|
||||
);
|
||||
|
||||
return invokeMethod( columnIndexMethod, buildColumnIndexMethodArgs( args, columnIndex ) );
|
||||
}
|
||||
catch ( SQLException ex ) {
|
||||
final String msg = "Exception getting column index for column: [" + args[0] +
|
||||
|
@ -78,10 +102,9 @@ public class ResultSetWrapperProxy implements InvocationHandler {
|
|||
"] as first argument for method: [" + method + "]";
|
||||
SQL_EXCEPTION_HELPER.logExceptions( ex, msg );
|
||||
}
|
||||
catch ( NoSuchMethodException ex ) {
|
||||
LOG.unableToSwitchToMethodUsingColumnIndex( method );
|
||||
}
|
||||
}
|
||||
|
||||
return invokeMethod( method, args );
|
||||
}
|
||||
|
||||
|
@ -96,20 +119,19 @@ public class ResultSetWrapperProxy implements InvocationHandler {
|
|||
return columnNameCache.getIndexForColumnName( columnName, rs );
|
||||
}
|
||||
|
||||
private boolean isFirstArgColumnLabel(Method method, Object[] args) {
|
||||
private static boolean isFirstArgColumnLabel(Method method) {
|
||||
// method name should start with either get or update
|
||||
if ( ! ( method.getName().startsWith( "get" ) || method.getName().startsWith( "update" ) ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// method should have arguments, and have same number as incoming arguments
|
||||
if ( ! ( method.getParameterCount() > 0 && args.length == method.getParameterCount() ) ) {
|
||||
// method should have at least one parameter
|
||||
if ( ! ( method.getParameterCount() > 0 ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The first argument should be a String (the column name)
|
||||
//noinspection RedundantIfStatement
|
||||
if ( ! ( String.class.isInstance( args[0] ) && method.getParameterTypes()[0].equals( String.class ) ) ) {
|
||||
// The first parameter should be a String (the column name)
|
||||
if ( ! method.getParameterTypes()[0].equals( String.class ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -124,8 +146,8 @@ public class ResultSetWrapperProxy implements InvocationHandler {
|
|||
* @return The corresponding method passed the column index.
|
||||
* @throws NoSuchMethodException Should never happen, but...
|
||||
*/
|
||||
private Method locateCorrespondingColumnIndexMethod(Method columnNameMethod) throws NoSuchMethodException {
|
||||
final Class[] actualParameterTypes = new Class[columnNameMethod.getParameterCount()];
|
||||
private static Method locateCorrespondingColumnIndexMethod(Method columnNameMethod) throws NoSuchMethodException {
|
||||
final Class<?>[] actualParameterTypes = new Class[columnNameMethod.getParameterCount()];
|
||||
actualParameterTypes[0] = int.class;
|
||||
System.arraycopy(
|
||||
columnNameMethod.getParameterTypes(),
|
||||
|
@ -152,4 +174,47 @@ public class ResultSetWrapperProxy implements InvocationHandler {
|
|||
throw e.getTargetException();
|
||||
}
|
||||
}
|
||||
|
||||
private static class ResultSetMethodKey {
|
||||
|
||||
private String methodName;
|
||||
|
||||
private Class<?>[] parameterTypes;
|
||||
|
||||
public ResultSetMethodKey(String methodName, Class<?>[] parameterTypes) {
|
||||
this.methodName = methodName;
|
||||
this.parameterTypes = parameterTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + methodName.hashCode();
|
||||
result = prime * result + Arrays.hashCode( parameterTypes );
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if ( this == obj ) {
|
||||
return true;
|
||||
}
|
||||
if ( obj == null ) {
|
||||
return false;
|
||||
}
|
||||
if ( getClass() != obj.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ResultSetMethodKey other = (ResultSetMethodKey) obj;
|
||||
if ( !methodName.equals( other.methodName ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( !Arrays.equals( parameterTypes, other.parameterTypes ) ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.engine.jdbc;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
public class ResultSetWrapperProxyTest {
|
||||
|
||||
private ResultSet resultSet;
|
||||
|
||||
private ResultSet resultSetProxy;
|
||||
|
||||
@Before
|
||||
public void initialize() throws SQLException {
|
||||
ServiceRegistry serviceRegistry = Mockito.mock( ServiceRegistry.class );
|
||||
when( serviceRegistry.getService( eq( ClassLoaderService.class ) ) ).thenReturn( new ClassLoaderServiceImpl() );
|
||||
|
||||
ColumnNameCache columnNameCache = new ColumnNameCache( 2 );
|
||||
|
||||
resultSet = Mockito.mock( ResultSet.class );
|
||||
when( resultSet.findColumn( eq( "myColumn" ) ) ).thenReturn( 1 );
|
||||
|
||||
resultSetProxy = ResultSetWrapperProxy.generateProxy( resultSet, columnNameCache, serviceRegistry );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRedirectedGetMethod() throws SQLException {
|
||||
resultSetProxy.getBigDecimal( "myColumn" );
|
||||
|
||||
verify( resultSet, times( 1 ) ).getBigDecimal( 1 );
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void testRedirectedGetMethodWithAdditionalParameters() throws SQLException {
|
||||
resultSetProxy.getBigDecimal( "myColumn", 8 );
|
||||
|
||||
verify( resultSet, times( 1 ) ).getBigDecimal( 1, 8 );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRedirectedUpdateMethod() throws SQLException {
|
||||
resultSetProxy.updateInt( "myColumn", 19 );
|
||||
|
||||
verify( resultSet, times( 1 ) ).updateInt( 1, 19 );
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void testIntMethods() throws SQLException {
|
||||
resultSetProxy.getBigDecimal( 3 );
|
||||
verify( resultSet, times( 1 ) ).getBigDecimal( 3 );
|
||||
|
||||
resultSetProxy.getBigDecimal( 13, 8 );
|
||||
verify( resultSet, times( 1 ) ).getBigDecimal( 13, 8 );
|
||||
|
||||
resultSetProxy.updateInt( 23, 19 );
|
||||
verify( resultSet, times( 1 ) ).updateInt( 23, 19 );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStandardMethod() throws SQLException {
|
||||
resultSetProxy.getFetchSize();
|
||||
|
||||
verify( resultSet, times( 1 ) ).getFetchSize();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue