com.github.javaclub.cdl.client.matrix.merger.ResultSetFactory Maven / Gradle / Ivy
package com.github.javaclub.cdl.client.matrix.merger;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.javaclub.cdl.client.matrix.merger.aggregation.AggregationInvokeHandler;
import com.github.javaclub.cdl.client.matrix.merger.aggregation.AggregationResultSet;
import com.github.javaclub.cdl.client.matrix.merger.groupby.GroupByInvokeHandler;
import com.github.javaclub.cdl.client.matrix.merger.groupby.GroupByResultSet;
import com.github.javaclub.cdl.client.matrix.merger.iterator.IteratorResultSet;
import com.github.javaclub.cdl.client.matrix.merger.orderby.OrderByResultSet;
import com.github.javaclub.cdl.client.matrix.parser.result.merger.MergeContext;
import com.github.javaclub.cdl.client.matrix.parser.result.merger.MergeContext.ResultSetType;
public class ResultSetFactory {
private static final Logger log = LoggerFactory.getLogger(ResultSetFactory.class);
/**
* 获取结果集
*
* @param resultSets 结果集列表
* @param mergeContext 结果归并上下文
* @return 结果集包装
*/
public static ResultSet getResultSet(final List resultSets, final MergeContext mergeContext) throws SQLException {
ResultSetType resultSetType = mergeContext.getResultSetType();
log.trace("Get '{}' result set", resultSetType);
switch (resultSetType) {
case GroupBy:
return createDelegateResultSet(new GroupByInvokeHandler(new GroupByResultSet(resultSets, mergeContext)));
case Aggregate:
return createDelegateResultSet(new AggregationInvokeHandler(new AggregationResultSet(resultSets, mergeContext)));
case OrderBy:
return new OrderByResultSet(resultSets, mergeContext);
case Iterator:
return new IteratorResultSet(resultSets, mergeContext);
default:
throw new UnsupportedOperationException(resultSetType.name());
}
}
private static ResultSet createDelegateResultSet(final InvocationHandler handler) {
return (ResultSet) Proxy.newProxyInstance(ResultSetFactory.class.getClassLoader(), new Class[]{ResultSet.class}, handler);
}
}