17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
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
|
|
|
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.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.sql.model;
|
|
|
15 |
|
|
|
16 |
import java.sql.ResultSet;
|
|
|
17 |
import java.sql.ResultSetMetaData;
|
|
|
18 |
import java.sql.SQLException;
|
|
|
19 |
import java.util.HashMap;
|
|
|
20 |
import java.util.Map;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* A class to help find fields by their fullname in resultset. Some jdbc drivers only accept the
|
|
|
24 |
* short name of fields, eg you execute "select B.DESIGNATION from BATIMENT B" but you can only
|
|
|
25 |
* access the designation with "DESIGNATION". With this class you can ask the index of
|
|
|
26 |
* "BATIMENT.DESIGNATION".
|
|
|
27 |
*
|
|
|
28 |
* @author Sylvain
|
|
|
29 |
*/
|
|
|
30 |
public final class ResultSetFullnameHelper {
|
|
|
31 |
private final ResultSet delegate;
|
|
|
32 |
private ResultSetMetaData rsMD;
|
180 |
ilm |
33 |
private final Map<String, Map<String, Integer>> tablesMap;
|
17 |
ilm |
34 |
|
|
|
35 |
public ResultSetFullnameHelper(ResultSet rs) {
|
|
|
36 |
this.delegate = rs;
|
180 |
ilm |
37 |
this.tablesMap = new HashMap<>();
|
17 |
ilm |
38 |
this.rsMD = null;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public final ResultSet getRS() {
|
|
|
42 |
return this.delegate;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
private final ResultSetMetaData getMetaData() throws SQLException {
|
|
|
46 |
if (this.rsMD == null) {
|
|
|
47 |
this.rsMD = this.delegate.getMetaData();
|
|
|
48 |
}
|
|
|
49 |
return this.rsMD;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public final int getIndex(SQLField f) throws SQLException {
|
|
|
53 |
return this.getIndex(f.getTable().getName(), f.getName());
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public final int getIndex(String fieldFullName) throws SQLException {
|
65 |
ilm |
57 |
final SQLName names = SQLName.parse(fieldFullName);
|
|
|
58 |
return this.getIndex(names.getItem(-2), names.getName());
|
17 |
ilm |
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Get the index of the specified field.
|
|
|
63 |
*
|
|
|
64 |
* @param tableName the table of the field, eg "SITE".
|
|
|
65 |
* @param fieldName the name of the field, eg "DESIGNATION".
|
|
|
66 |
* @return the index of the field, or -1 if not found.
|
|
|
67 |
* @throws SQLException if an error occur while retrieving metadata.
|
|
|
68 |
*/
|
|
|
69 |
public final int getIndex(String tableName, String fieldName) throws SQLException {
|
180 |
ilm |
70 |
final Map<String, Integer> m = this.tablesMap.computeIfAbsent(tableName, (k) -> new HashMap<>());
|
17 |
ilm |
71 |
if (!m.containsKey(fieldName)) {
|
|
|
72 |
final int index = this.searchIndex(tableName, fieldName);
|
|
|
73 |
m.put(fieldName, index < 1 ? null : new Integer(index));
|
|
|
74 |
}
|
180 |
ilm |
75 |
final Integer val = m.get(fieldName);
|
17 |
ilm |
76 |
return val == null ? -1 : val.intValue();
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
private final int searchIndex(String tableName, String fieldName) throws SQLException {
|
|
|
80 |
for (int i = 1; i <= this.getMetaData().getColumnCount(); i++) {
|
|
|
81 |
final String colName = this.getMetaData().getColumnName(i);
|
|
|
82 |
if (colName.equals(fieldName) && this.getMetaData().getTableName(i).equals(tableName)) {
|
|
|
83 |
return i;
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
return -1;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
}
|