net.ttddyy.dsproxy.proxy.RepeatableReadResultSetProxyLogic Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datasource-proxy Show documentation
Show all versions of datasource-proxy Show documentation
Provide a datasource proxy that can inject your own logic into all queries.
package net.ttddyy.dsproxy.proxy;
import net.ttddyy.dsproxy.ConnectionInfo;
import net.ttddyy.dsproxy.listener.MethodExecutionListenerUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import static java.lang.Boolean.TRUE;
import static java.lang.String.format;
/**
* Allows {@link java.sql.ResultSet} to be consumed more than once.
*
* @author Liam Williams
* @see net.ttddyy.dsproxy.proxy.jdk.ResultSetInvocationHandler
* @since 1.4
*/
public class RepeatableReadResultSetProxyLogic implements ResultSetProxyLogic {
private static final Set METHODS_TO_INTERCEPT = Collections.unmodifiableSet(
new HashSet() {
{
// getDeclaredMethods does NOT include parent class methods(e.g: Wrapper#unwrap()"
for (Method method : ResultSet.class.getDeclaredMethods()) {
add(method.getName());
}
add("toString");
add("getTarget"); // from ProxyJdbcObject
}
}
);
private static final Object UNCONSUMED_RESULT_COLUMN = new Object();
public static class Builder {
private ResultSet resultSet;
private ConnectionInfo connectionInfo;
private ProxyConfig proxyConfig;
private Map columnNameToIndex;
private int columnCount;
public static Builder create() {
return new Builder();
}
public RepeatableReadResultSetProxyLogic build() {
RepeatableReadResultSetProxyLogic logic = new RepeatableReadResultSetProxyLogic();
logic.resultSet = this.resultSet;
logic.connectionInfo = this.connectionInfo;
logic.proxyConfig = this.proxyConfig;
logic.columnNameToIndex = this.columnNameToIndex;
logic.columnCount = this.columnCount;
return logic;
}
public Builder resultSet(ResultSet resultSet) {
this.resultSet = resultSet;
return this;
}
public Builder connectionInfo(ConnectionInfo connectionInfo) {
this.connectionInfo = connectionInfo;
return this;
}
public Builder proxyConfig(ProxyConfig proxyConfig) {
this.proxyConfig = proxyConfig;
return this;
}
public Builder columnNameToIndex(Map columnNameToIndex) {
this.columnNameToIndex = columnNameToIndex;
return this;
}
public Builder columnCount(int columnCount) {
this.columnCount = columnCount;
return this;
}
}
private Map columnNameToIndex;
private ResultSet resultSet;
private ConnectionInfo connectionInfo;
private int columnCount;
private ProxyConfig proxyConfig;
private int resultPointer;
private boolean resultSetConsumed;
private boolean closed;
private Object[] currentResult;
private final List