Java tutorial
/* * Copyright 1999-2015 dangdang.com. * <p> * Licensed 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. * </p> */ package com.dangdang.ddframe.rdb.sharding.jdbc.adapter; import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationResultSet; import com.dangdang.ddframe.rdb.sharding.util.SQLUtil; import com.dangdang.ddframe.rdb.sharding.util.ThrowableSQLExceptionMethod; import com.google.common.base.Preconditions; import lombok.AccessLevel; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.map.CaseInsensitiveMap; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.List; import java.util.Map; /** * ??. * * @author zhangliang */ @Slf4j public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperationResultSet { @Getter(AccessLevel.PROTECTED) private final List<ResultSet> resultSets; @Getter private final Map<String, Integer> columnLabelIndexMap; private boolean closed; public AbstractResultSetAdapter(final List<ResultSet> resultSets) throws SQLException { Preconditions.checkArgument(!resultSets.isEmpty()); this.resultSets = resultSets; columnLabelIndexMap = generateColumnLabelIndexMap(); } private Map<String, Integer> generateColumnLabelIndexMap() throws SQLException { ResultSetMetaData resultSetMetaData = resultSets.get(0).getMetaData(); Map<String, Integer> result = new CaseInsensitiveMap<>(resultSetMetaData.getColumnCount()); for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { result.put(resultSetMetaData.getColumnLabel(i), i); } return result; } @Override public final void close() throws SQLException { SQLUtil.safeInvoke(resultSets, new ThrowableSQLExceptionMethod<ResultSet>() { @Override public void apply(final ResultSet object) throws SQLException { object.close(); } }); closed = true; } @Override public final boolean isClosed() throws SQLException { return closed; } @Override public final void setFetchDirection(final int direction) throws SQLException { for (ResultSet each : resultSets) { each.setFetchDirection(direction); } } @Override public final void setFetchSize(final int rows) throws SQLException { for (ResultSet each : resultSets) { each.setFetchSize(rows); } } }