Row.java 2.2 KB
Newer Older
G
gaohongtao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**
 * 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.merger.row;

G
gaoht 已提交
20 21 22 23
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

G
gaohongtao 已提交
24 25 26
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
G
gaoht 已提交
27
import java.util.Arrays;
G
gaohongtao 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

/**
 * 数据行.
 *
 * @author gaohongtao
 */
public class Row {
    
    private final Object[] rowData;
    
    public Row(final ResultSet resultSet) throws SQLException {
        rowData = getRowData(resultSet);
    }
    
    private Object[] getRowData(final ResultSet resultSet) throws SQLException {
        ResultSetMetaData md = resultSet.getMetaData();
        Object[] result = new Object[md.getColumnCount()];
        for (int i = 0; i < md.getColumnCount(); i++) {
            result[i] = resultSet.getObject(i + 1);
        }
        return result;
    }
    
G
gaoht 已提交
51
    void setCell(final int index, final Object value) {
G
gaohongtao 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64
        Preconditions.checkArgument(containsCell(index));
        rowData[index - 1] = value;
    }
    
    public Object getCell(final int index) {
        Preconditions.checkArgument(containsCell(index));
        return rowData[index - 1];
    }
    
    public boolean containsCell(final int index) {
        return index - 1 > -1 && index - 1 < rowData.length;
    }
    
G
gaoht 已提交
65 66 67 68 69 70 71 72 73 74
    @Override
    public String toString() {
        return String.format("value is : %s", Lists.transform(Arrays.asList(rowData), new Function<Object, Object>() {
    
            @Override
            public Object apply(final Object input) {
                return null == input ? "nil" : input;
            }
        }));
    }
G
gaohongtao 已提交
75
}