354798ggg
2023-08-29 fd0c7c174d7b7313aa49061df05350405a3c1bf1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.product.data.sync.util;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 自定义查询结果
 */
public class CustomResultSet {
    /**
     * 结果集
     */
    private ResultSet resultSet;
    /**
     * 是否自定义结果集
     */
    private boolean isCustom;
    /**
     * next 下表
     */
    private int currentIndex = -1;
    /**
     * 结果集中的列名
     */
    private String[] columnNames;
    /**
     * 自定义结果集时存储每条数据的集合
     */
    private List<Map<String, Object>> values;
 
    /**
     * 构造函数
     * java.sql.ResultSet
     *
     * @param resultSet
     */
    public CustomResultSet(ResultSet resultSet) {
        this.resultSet = resultSet;
        this.isCustom = false;
    }
 
    /**
     * 自定义结果集构造
     *
     * @param resultSet
     * @param columnNames
     * @throws SQLException
     */
    public CustomResultSet(ResultSet resultSet, String[] columnNames) throws SQLException {
        this.resultSet = resultSet;
        this.columnNames = columnNames;
        if (this.columnNames == null || this.columnNames.length <= 0) {
            this.columnNames = BatchData.getColumnNames(resultSet);
        }
        this.isCustom = true;
        values = new ArrayList<>();
        while (resultSet.next()) {
            Map<String, Object> value = new HashMap<>();
            for (String columnName : this.columnNames) {
                value.put(columnName, resultSet.getObject(columnName));
            }
            values.add(value);
        }
        this.resultSet.close();
        this.resultSet=null;
    }
 
 
    public boolean next() throws SQLException {
        if (isCustom && currentIndex >= 0 && this.values.get(currentIndex) != null) {
            this.values.get(currentIndex).clear();
        }
        currentIndex++;
        return !isCustom ? resultSet.next() : this.values.size() - 1 >= currentIndex;
    }
 
    public Object getObject(String columnLabel) throws SQLException {
        if (isCustom && this.values.get(this.currentIndex) != null) {
            return this.values.get(this.currentIndex).get(columnLabel);
        } else if (!isCustom) {
            return resultSet.getObject(columnLabel);
        }
        return null;
    }
 
    public ResultSet getResultSet() {
        return isCustom ? null : resultSet;
    }
 
    public String[] getColumnNames() {
        return columnNames;
    }
}