OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 65 Rev 180
Line 17... Line 17...
17
import java.sql.ResultSetMetaData;
17
import java.sql.ResultSetMetaData;
18
import java.sql.SQLException;
18
import java.sql.SQLException;
19
import java.util.HashMap;
19
import java.util.HashMap;
20
import java.util.Map;
20
import java.util.Map;
21
 
21
 
22
import org.apache.commons.collections.Factory;
-
 
23
import org.apache.commons.collections.map.LazyMap;
-
 
24
 
-
 
25
/**
22
/**
26
 * A class to help find fields by their fullname in resultset. Some jdbc drivers only accept the
23
 * A class to help find fields by their fullname in resultset. Some jdbc drivers only accept the
27
 * short name of fields, eg you execute "select B.DESIGNATION from BATIMENT B" but you can only
24
 * short name of fields, eg you execute "select B.DESIGNATION from BATIMENT B" but you can only
28
 * access the designation with "DESIGNATION". With this class you can ask the index of
25
 * access the designation with "DESIGNATION". With this class you can ask the index of
29
 * "BATIMENT.DESIGNATION".
26
 * "BATIMENT.DESIGNATION".
Line 31... Line 28...
31
 * @author Sylvain
28
 * @author Sylvain
32
 */
29
 */
33
public final class ResultSetFullnameHelper {
30
public final class ResultSetFullnameHelper {
34
    private final ResultSet delegate;
31
    private final ResultSet delegate;
35
    private ResultSetMetaData rsMD;
32
    private ResultSetMetaData rsMD;
36
    private final Map tablesMap;
33
    private final Map<String, Map<String, Integer>> tablesMap;
37
 
34
 
38
    public ResultSetFullnameHelper(ResultSet rs) {
35
    public ResultSetFullnameHelper(ResultSet rs) {
39
        this.delegate = rs;
36
        this.delegate = rs;
40
        this.tablesMap = LazyMap.decorate(new HashMap(), new Factory() {
-
 
41
            public Object create() {
-
 
42
                return new HashMap();
37
        this.tablesMap = new HashMap<>();
43
            }
-
 
44
        });
-
 
45
        this.rsMD = null;
38
        this.rsMD = null;
46
    }
39
    }
47
 
40
 
48
    public final ResultSet getRS() {
41
    public final ResultSet getRS() {
49
        return this.delegate;
42
        return this.delegate;
Line 72... Line 65...
72
     * @param fieldName the name of the field, eg "DESIGNATION".
65
     * @param fieldName the name of the field, eg "DESIGNATION".
73
     * @return the index of the field, or -1 if not found.
66
     * @return the index of the field, or -1 if not found.
74
     * @throws SQLException if an error occur while retrieving metadata.
67
     * @throws SQLException if an error occur while retrieving metadata.
75
     */
68
     */
76
    public final int getIndex(String tableName, String fieldName) throws SQLException {
69
    public final int getIndex(String tableName, String fieldName) throws SQLException {
77
        final Map m = (Map) this.tablesMap.get(tableName);
70
        final Map<String, Integer> m = this.tablesMap.computeIfAbsent(tableName, (k) -> new HashMap<>());
78
        if (!m.containsKey(fieldName)) {
71
        if (!m.containsKey(fieldName)) {
79
            final int index = this.searchIndex(tableName, fieldName);
72
            final int index = this.searchIndex(tableName, fieldName);
80
            m.put(fieldName, index < 1 ? null : new Integer(index));
73
            m.put(fieldName, index < 1 ? null : new Integer(index));
81
        }
74
        }
82
        final Integer val = (Integer) m.get(fieldName);
75
        final Integer val = m.get(fieldName);
83
        return val == null ? -1 : val.intValue();
76
        return val == null ? -1 : val.intValue();
84
    }
77
    }
85
 
78
 
86
    private final int searchIndex(String tableName, String fieldName) throws SQLException {
79
    private final int searchIndex(String tableName, String fieldName) throws SQLException {
87
        for (int i = 1; i <= this.getMetaData().getColumnCount(); i++) {
80
        for (int i = 1; i <= this.getMetaData().getColumnCount(); i++) {