OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
Line 32... Line 32...
32
import java.sql.SQLWarning;
32
import java.sql.SQLWarning;
33
import java.sql.SQLXML;
33
import java.sql.SQLXML;
34
import java.sql.Statement;
34
import java.sql.Statement;
35
import java.sql.Time;
35
import java.sql.Time;
36
import java.sql.Timestamp;
36
import java.sql.Timestamp;
-
 
37
import java.util.ArrayList;
37
import java.util.Calendar;
38
import java.util.Calendar;
-
 
39
import java.util.Collection;
-
 
40
import java.util.Collections;
38
import java.util.HashMap;
41
import java.util.HashMap;
-
 
42
import java.util.List;
39
import java.util.Map;
43
import java.util.Map;
40
 
44
 
41
/**
45
/**
42
 * A resultSet that wraps onto another one, caching name to index translation, and using a
46
 * A resultSet that wraps onto another one, caching name to index translation, and using a
43
 * ResultSetFullnameHelper.
47
 * ResultSetFullnameHelper.
44
 * 
48
 * 
45
 * @author Sylvain
49
 * @author Sylvain
46
 */
50
 */
47
public class SQLResultSet implements ResultSet {
51
public class SQLResultSet implements ResultSet {
48
 
52
 
-
 
53
    // useful since some JDBC drivers (e.g. PgResultSet) implement getObject(col, clazz) by the
-
 
54
    // equivalent of clazz.cast(getObject(col)). I.e. getObject(col, Long.class) will fail if col
-
 
55
    // is an int.
49
    static public final <T> T getValue(final ResultSet rs, final Class<T> clz, final int columnIndex) throws SQLException {
56
    static public final <T> T getValue(final ResultSet rs, final Class<T> clz, final int columnIndex) throws SQLException {
50
        final Object res;
57
        final Object res;
51
        if (clz == Object.class)
58
        if (clz == Object.class)
52
            res = rs.getObject(columnIndex);
59
            res = rs.getObject(columnIndex);
53
        else if (Integer.class.isAssignableFrom(clz))
60
        else if (Integer.class.isAssignableFrom(clz))
Line 71... Line 78...
71
        else if (Float.class.isAssignableFrom(clz))
78
        else if (Float.class.isAssignableFrom(clz))
72
            res = rs.getFloat(columnIndex);
79
            res = rs.getFloat(columnIndex);
73
        else if (Time.class.isAssignableFrom(clz))
80
        else if (Time.class.isAssignableFrom(clz))
74
            res = rs.getTime(columnIndex);
81
            res = rs.getTime(columnIndex);
75
        else
82
        else
76
            res = rs.getObject(columnIndex);
83
            res = rs.getObject(columnIndex, clz);
77
        return clz.cast(res);
84
        return clz.cast(res);
78
    }
85
    }
79
 
86
 
-
 
87
    // see above comment
80
    static public final <T> T getValue(final ResultSet rs, final Class<T> clz, final String columnLabel) throws SQLException {
88
    static public final <T> T getValue(final ResultSet rs, final Class<T> clz, final String columnLabel) throws SQLException {
81
        final Object res;
89
        final Object res;
82
        if (clz == Object.class)
90
        if (clz == Object.class)
83
            res = rs.getObject(columnLabel);
91
            res = rs.getObject(columnLabel);
84
        else if (Integer.class.isAssignableFrom(clz))
92
        else if (Integer.class.isAssignableFrom(clz))
Line 102... Line 110...
102
        else if (Float.class.isAssignableFrom(clz))
110
        else if (Float.class.isAssignableFrom(clz))
103
            res = rs.getFloat(columnLabel);
111
            res = rs.getFloat(columnLabel);
104
        else if (Time.class.isAssignableFrom(clz))
112
        else if (Time.class.isAssignableFrom(clz))
105
            res = rs.getTime(columnLabel);
113
            res = rs.getTime(columnLabel);
106
        else
114
        else
107
            res = rs.getObject(columnLabel);
115
            res = rs.getObject(columnLabel, clz);
108
        return clz.cast(res);
116
        return clz.cast(res);
109
    }
117
    }
110
 
118
 
111
    static public final int getRowProcessedCount(final ResultSet rs) {
119
    static public final int getRowProcessedCount(final ResultSet rs) {
112
        if (rs instanceof SQLResultSet) {
120
        if (rs instanceof SQLResultSet) {
Line 115... Line 123...
115
            // ResultSet.getRow() always return 0 after the last row
123
            // ResultSet.getRow() always return 0 after the last row
116
            return 0;
124
            return 0;
117
        }
125
        }
118
    }
126
    }
119
 
127
 
-
 
128
    static public final <T> List<T> getList(final ResultSet rs, int column, final Class<? extends T> itemType) throws SQLException {
-
 
129
        final Array array = rs.getArray(column);
-
 
130
        try {
-
 
131
            return Collections.unmodifiableList(readArray(array, itemType, new ArrayList<>()));
-
 
132
        } finally {
-
 
133
            array.free();
-
 
134
        }
-
 
135
    }
-
 
136
 
-
 
137
    static public final <T, C extends Collection<T>> C readArray(final Array array, final Class<? extends T> itemType, final C res) throws SQLException {
-
 
138
        final ResultSet rs = array.getResultSet();
-
 
139
        while (rs.next()) {
-
 
140
            // 1 is the index
-
 
141
            res.add(getValue(rs, itemType, 2));
-
 
142
        }
-
 
143
        return res;
-
 
144
    }
-
 
145
 
120
    private final ResultSet delegate;
146
    private final ResultSet delegate;
121
    private final ResultSetFullnameHelper helper;
147
    private final ResultSetFullnameHelper helper;
122
    private final CachedTransformer<String, Integer, SQLException> indexes;
148
    private final CachedTransformer<String, Integer, SQLException> indexes;
123
    private int rowProcessedCount;
149
    private int rowProcessedCount;
124
 
150