KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > source > sql > SQLTable


1 package jimm.datavision.source.sql;
2 import jimm.datavision.source.Table;
3 import jimm.datavision.source.Column;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.sql.*;
7
8 /**
9  * Represents a database table.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13 public class SQLTable extends Table {
14
15 protected DatabaseMetaData dbmd;
16 protected HashMap JavaDoc colCacheMap;
17
18 /**
19  * Constructor.
20  *
21  * @param database the database in which this table resides
22  * @param name the table's name
23  * @param dbmd database metadata information
24  */

25 public SQLTable(Database database, String JavaDoc name, DatabaseMetaData dbmd) {
26     super(database, name);
27     this.dbmd = dbmd;
28 }
29
30 public Column findColumn(Object JavaDoc colIdObj) {
31     if (dbmd != null) loadColumns();
32
33     String JavaDoc colId = colIdObj.toString();
34
35     // First try a simple exact match using colCacheMap. This will often
36
// fail the first time, but after we have found a column using the quite
37
// convoluted search below we store the column in colCacheMap.
38
Column col = (Column)colCacheMap.get(colId);
39     if (col != null)
40     return col;
41
42     boolean caseSensitive =
43     dataSource.getReport().caseSensitiveDatabaseNames();
44
45     String JavaDoc schemaName = null;
46     int pos = colId.indexOf('.');
47     if (pos >= 0) {
48     schemaName = colId.substring(0, pos);
49     colId = colId.substring(pos + 1);
50     }
51
52     if (!caseSensitive) {
53     if (schemaName != null) schemaName = schemaName.toLowerCase();
54     colId = colId.toLowerCase();
55     }
56
57     // First try with table's schema name, if any.
58
if (schemaName != null) {
59     String JavaDoc target = schemaName + '.' + colId;
60     for (Iterator JavaDoc iter = columns.keySet().iterator(); iter.hasNext(); ) {
61         String JavaDoc key = (String JavaDoc)iter.next();
62         if (caseSensitive) {
63         if (key.equals(target)) {
64             col = (Column)columns.get(key);
65             colCacheMap.put(colId, col); // Store in cache
66
return col;
67         }
68         }
69         else {
70         if (key.toLowerCase().equals(target.toLowerCase())) {
71             col = (Column)columns.get(key);
72             colCacheMap.put(colId, col); // Store in cache
73
return col;
74         }
75         }
76     }
77     }
78
79     // Now try with database's schema name if it's different from the
80
// table's schema name.
81
if (name != null && !name.equals(schemaName)) {
82     String JavaDoc target = name + '.' + colId;
83     for (Iterator JavaDoc iter = columns.keySet().iterator(); iter.hasNext(); ) {
84         String JavaDoc key = (String JavaDoc)iter.next();
85         if (caseSensitive) {
86         if (key.equals(target)) {
87             col = (Column)columns.get(key);
88             colCacheMap.put(colId, col); // Store in cache
89
return col;
90         }
91         }
92         else {
93         if (key.toLowerCase().equals(target.toLowerCase())) {
94             col = (Column)columns.get(key);
95             colCacheMap.put(colId, col); // Store in cache
96
return col;
97         }
98         }
99     }
100     }
101
102     // Finally, try with no schema name.
103
String JavaDoc target = colId;
104     for (Iterator JavaDoc iter = columns.keySet().iterator(); iter.hasNext(); ) {
105     String JavaDoc key = (String JavaDoc)iter.next();
106     if (caseSensitive) {
107         if (key.equals(target))
108         return (Column)columns.get(key);
109     }
110     else {
111         if (key.toLowerCase().equals(target.toLowerCase()))
112         return (Column)columns.get(key);
113     }
114     }
115
116     return null;
117 }
118
119 public Iterator JavaDoc columns() {
120     if (dbmd != null) loadColumns();
121     return super.columns();
122 }
123
124 protected void loadColumns() {
125     colCacheMap = new HashMap JavaDoc();
126
127     String JavaDoc schemaName = null;
128     String JavaDoc tableName = name;
129     int pos = name.indexOf('.');
130     if (pos >= 0) {
131     schemaName = name.substring(0, pos);
132     tableName = name.substring(pos + 1);
133     }
134
135     loadColumnsUsing(((Database)dataSource).getName(), tableName);
136     if (schemaName != null && columns.isEmpty())
137     loadColumnsUsing(schemaName, tableName);
138     if (columns.isEmpty())
139     loadColumnsUsing(null, tableName);
140
141     dbmd = null;
142 }
143
144 protected void loadColumnsUsing(String JavaDoc schemaName, String JavaDoc tableName) {
145     ResultSet rset = null;
146     try {
147     // The last two args should be able to be null, but some users have
148
// reported that their drivers barf unless I use the "%" wildcard.
149
// (That's ancient history; I'm not sure if it is still true.
150
// However, rough timing indicates that there's not much of a
151
// performance difference in either case.)
152
rset = dbmd.getColumns(null, schemaName, tableName, "%");
153
154     while (rset.next()) {
155         SQLColumn col = new SQLColumn(this,
156                       rset.getString("COLUMN_NAME").trim(),
157                       rset.getInt("DATA_TYPE"));
158         addColumn(col);
159     }
160     }
161     catch (SQLException e) {
162     }
163     finally {
164     try { if (rset != null) rset.close(); }
165     catch (SQLException e2) {}
166     }
167 }
168     
169 }
170
Popular Tags