KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > gui > JDBCMetaData


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/gui/JDBCMetaData.java#1 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2006-2007 Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 //
10 // Created on April 28, 2006, 2:43 PM
11  */

12 package mondrian.gui;
13
14 import java.util.*;
15 import java.sql.*;
16
17 /**
18  *
19  * @version $Id: //open/mondrian/src/main/mondrian/gui/JDBCMetaData.java#1 $
20  */

21 public class JDBCMetaData {
22     String JavaDoc jdbcDriverClassName = null; //"org.postgresql.Driver"
23
String JavaDoc jdbcConnectionUrl = null; // "jdbc:postgresql://localhost:5432/hello?user=postgres&password=post"
24

25     Connection conn = null;
26     DatabaseMetaData md = null;
27
28     /* Map of Schema and its fact tables ::
29      * allFactTableDimensions = [Schema1, Schema2] -> [FactTableT8, FactTable9] -> [ForeignKeys -> PrimaryKeyTable]
30      *
31      * Map of Schema, its tables and their Primary Keys ::
32      * allTablesPKs = [Schema1, Schema2] -> [Tables -> PrimaryKey]
33      *
34      * Map of Schemas, its tables and their columns with their data types
35      * allTablesCols = [Schema1, Schema2] -> [Table1, Table2] -> [Columns -> DataType]
36      *
37      * Map of schemas and their tables
38      * allSchemasMap = [Schema1, Schema2] -> [Table1, Table2]
39      *
40      */

41     private Map allFactTableDimensions = new HashMap(); //unsynchronized, permits null values and null key
42
private Map allTablesPKs = new HashMap();
43     private Map allTablesCols = new HashMap();
44     private Map allSchemasMap = new HashMap();
45
46     private Vector allSchemas = new Vector(); // Vector of all schemas in the connected database
47

48     private String JavaDoc errMsg = null;
49     private Database db = new Database();
50
51     public JDBCMetaData(String JavaDoc jdbcDriverClassName, String JavaDoc jdbcConnectionUrl) {
52         this.jdbcConnectionUrl = jdbcConnectionUrl;
53         this.jdbcDriverClassName = jdbcDriverClassName;
54
55         if (initConnection() == null) {
56             setAllSchemas();
57             closeConnection();
58         }
59     }
60
61     /* Creates a database connection and initializes the meta data details */
62     public String JavaDoc initConnection(){
63         try{
64             if (jdbcDriverClassName==null || jdbcConnectionUrl==null) {
65                 throw new Exception JavaDoc("Driver="+jdbcDriverClassName+"\nConn Url="+jdbcConnectionUrl+"\n(Hint: Use Prefrences to set Database Connection parameters first and then open a Schema.)");
66             }
67
68             Class.forName(jdbcDriverClassName);
69             conn = DriverManager.getConnection(jdbcConnectionUrl);
70
71             System.out.println("JDBC connection OPEN");
72             md = conn.getMetaData();
73
74             db.productName = md.getDatabaseProductName();
75             db.productVersion = md.getDatabaseProductVersion();
76             db.catalogName = conn.getCatalog();
77
78             System.out.println("Catalog name = "+db.catalogName);
79             /*
80             ResultSet rsd = md.getSchemas();
81             while (rsd.next())
82             { System.out.println(" Schema ="+rsd.getString("TABLE_SCHEM"));
83                  System.out.println(" Schema ="+rsd.getString("TABLE_CATALOG"));
84             }
85             rsd = md.getCatalogs();
86             while (rsd.next())
87                 System.out.println(" Caltalog ="+rsd.getString("TABLE_CAT"));
88              */

89             System.out.println("Database Product Name: " + db.productName);
90             System.out.println("Database Product Version: " + db.productVersion);
91
92             /*
93             Class.forName("org.postgresql.Driver");
94             conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/demo","admin","admin");
95              */

96             return null;
97         } catch (Exception JavaDoc e) {
98             errMsg = e.getMessage();
99             System.out.println("Database connection exception : "+errMsg);
100             return errMsg;
101             //e.printStackTrace();
102
}
103     }
104
105     public void closeConnection() {
106         try {
107             conn.close();
108             System.out.println("JDBC connection CLOSE");
109         }catch (Exception JavaDoc e) {e.printStackTrace();}
110     }
111
112     /* set all schemas in the currently connected database */
113     private void setAllSchemas(){
114         ResultSet rs = null;
115         try{
116             rs = md.getSchemas();
117             /*
118             if (true)
119             throw new Exception("Schema concept not found in database");
120              */

121             while(rs.next()) {
122                 DbSchema dbs = new DbSchema();
123                 dbs.name = rs.getString("TABLE_SCHEM");
124                 setAllTables(dbs);
125                 db.addDbSchema(dbs);
126             }
127             rs.close();
128         } catch (Exception JavaDoc e) {
129             System.out.println("Exception : Database does not support schemas."+e.getMessage());
130             DbSchema dbs = new DbSchema();
131             dbs.name = null; //tables with no schema name
132
setAllTables(dbs);
133             db.addDbSchema(dbs);
134         }
135     }
136
137     /* set all tables in the currently connected database */
138     private void setAllTables(DbSchema dbs){
139         ResultSet rs = null;
140         try{
141             rs = md.getTables(null, dbs.name, null, new String JavaDoc[]{"TABLE"});
142             while(rs.next()) {
143                 String JavaDoc tbname = rs.getString("TABLE_NAME");
144                 DbTable dbt;
145
146                 /* Note : Imported keys are foreign keys which are primary keys of in some other tables
147                  * : Exported keys are primary keys which are referenced as foreign keys in other tables.
148                  */

149                 ResultSet rs_fks = md.getImportedKeys(null, dbs.name, tbname);
150                 if (rs_fks.next()) {
151                     dbt = new FactTable();
152                     do {
153                         ((FactTable) dbt).addFks(rs_fks.getString("FKCOLUMN_NAME"),rs_fks.getString("pktable_name"));
154                     } while(rs_fks.next());
155
156                 } else {
157                     dbt = new DbTable();
158                 }
159                 rs_fks.close();
160
161                 dbt.schemaName = dbs.name;
162                 dbt.name = tbname;
163                 setPKey(dbt);
164                 setColumns(dbt);
165                 dbs.addDbTable(dbt);
166                 db.addDbTable(dbt);
167             }
168             rs.close();
169         } catch (Exception JavaDoc e) {e.printStackTrace();}
170     }
171
172     /* get the Primary key name for a given table name
173      * This key may be a composite key made of multiple columns.
174      */

175     private void setPKey(DbTable dbt){
176         ResultSet rs = null;
177         try{
178             rs = md.getPrimaryKeys(null, dbt.schemaName, dbt.name);
179             /*
180             while(rs.next()) {
181                 primKeys.add(rs.getString("COLUMN_NAME"));
182             }
183              **/

184             if (rs.next()) {
185                 //===dbt.pk = rs.getString("PK_NAME"); // a column may have been given a primary key name
186
dbt.pk = rs.getString("column_name"); // we need the column name which is primary key for the given table.
187
}
188             rs.close();
189         } catch (Exception JavaDoc e) {
190             e.printStackTrace();
191         }
192     }
193
194     /* get all columns for a given table name */
195     private void setColumns(DbTable dbt){
196         ResultSet rs = null;
197         try{
198             rs = md.getColumns(null, dbt.schemaName, dbt.name, null);
199             while(rs.next()) {
200                 dbt.addColsDataType(rs.getString("COLUMN_NAME"), rs.getString("DATA_TYPE"));
201             }
202             rs.close();
203         } catch (Exception JavaDoc e) {
204             e.printStackTrace();
205         }
206     }
207
208 /* ===================================================================================================
209  * The following functions provide an interface to JDBCMetaData class to retrieve the meta data details
210  * =================================================================================================== */

211
212     public Vector getAllSchemas() {
213         return db.getAllSchemas();
214     }
215
216
217     /* get all tables in a given schema */
218     public Vector getAllTables(String JavaDoc schemaName) {
219         return db.getAllTables(schemaName);
220     }
221
222     /* get all tables in given schema minus the given table name */
223     public Vector getAllTables(String JavaDoc schemaName, String JavaDoc minusTable) {
224
225         if (minusTable == null) {
226             return getAllTables(schemaName);
227         } else {
228             Vector allTablesMinusOne = new Vector();
229             Iterator i = getAllTables(schemaName).iterator();
230             while (i.hasNext()) {
231                 String JavaDoc s = (String JavaDoc) i.next();
232                 if (s.endsWith(minusTable)) { // startsWith and endsWith cannot be compared with null argument, throws exception
233
if ((schemaName == null) || s.startsWith(schemaName) ) {
234                         continue;
235                     }
236                 }
237                 allTablesMinusOne.add(s);
238             }
239             return allTablesMinusOne;
240         }
241     }
242
243     /* get all possible cases of fact tables in a schema */
244     public Vector getFactTables(String JavaDoc schemaName) {
245         return db.getFactTables(schemaName);
246     }
247
248     /* get all possible cases of dimension tables which are linked to given fact table by foreign keys */
249     public Vector getDimensionTables(String JavaDoc schemaName, String JavaDoc factTable) {
250         Vector dimeTables = new Vector();
251
252         if (factTable == null) {
253             return dimeTables;
254         } else {
255             return db.getDimensionTables(schemaName, factTable);
256         }
257     }
258
259     public boolean isTableExists(String JavaDoc schemaName, String JavaDoc tableName) {
260         if (tableName == null) {
261             return true;
262         } else {
263             return db.tableExists(schemaName, tableName);
264         }
265     }
266
267     public boolean isColExists(String JavaDoc schemaName, String JavaDoc tableName, String JavaDoc colName) {
268         if (tableName == null || colName == null) {
269             return true;
270         } else {
271             return db.colExists(schemaName, tableName, colName);
272         }
273     }
274
275     /* get all foreign keys in given fact table */
276     public Vector getFactTableFKs(String JavaDoc schemaName, String JavaDoc factTable) {
277         Vector fks = new Vector();
278
279         if (factTable == null) {
280             return fks;
281         } else {
282             return db.getFactTableFKs(schemaName, factTable);
283         }
284     }
285
286     public String JavaDoc getTablePK(String JavaDoc schemaName, String JavaDoc tableName) {
287
288         if (tableName == null) {
289             return null;
290         } else {
291             return db.getTablePK(schemaName, tableName);
292         }
293     }
294
295     /* get all columns of given table in schema */
296     public Vector getAllColumns(String JavaDoc schemaName, String JavaDoc tableName) {
297         Vector allcols = new Vector();
298
299         if (tableName == null) {
300             return allcols;
301         } else {
302             return db.getAllColumns(schemaName, tableName);
303         }
304     }
305
306     // get column data type of given table and its col
307
public int getColumnDataType(String JavaDoc schemaName, String JavaDoc tableName, String JavaDoc colName) {
308         if (tableName == null || colName==null) {
309             return -1;
310         } else {
311             return db.getColumnDataType(schemaName, tableName, colName);
312         }
313
314     }
315     public String JavaDoc getDbCatalogName() {
316         return db.catalogName;
317     }
318
319     public String JavaDoc getDatabaseProductName() {
320         return db.productName;
321     }
322
323     public String JavaDoc getErrMsg() {
324         return errMsg;
325     }
326
327     public static void main(String JavaDoc[] args) {
328         /*
329         JDBCMetaData sb = new JDBCMetaData("org.postgresql.Driver","jdbc:postgresql://localhost:5432/testdb?user=admin&password=admin");
330         System.out.println("allSchemas="+sb.allSchemas);
331         System.out.println("allSchemasMap="+sb.allSchemasMap);
332         System.out.println("allTablesCols="+sb.allTablesCols);
333         System.out.println("allTablesPKs="+sb.allTablesPKs);
334         System.out.println("allFactTableDimensions="+sb.allFactTableDimensions);
335         System.out.println("getAllTables(null, part)="+sb.getAllTables(null, "part"));
336         System.out.println("sb.getColumnDataType(null, part,part_nbr)="+sb.getColumnDataType(null, "part","part_nbr"));
337          */

338         String JavaDoc s = "somita->namita";
339         String JavaDoc [] p = s.split("->");
340         if (p.length >=2)
341             System.out.println("p0="+p[0]+", p1="+p[1]);
342     }
343
344 /* ===================================================================================================
345  * class structure for storing database metadata
346  * =================================================================================================== */

347     class Database {
348         String JavaDoc catalogName = ""; // database name.
349
String JavaDoc productName = "Unknown";
350         String JavaDoc productVersion = "";
351
352         // list of all schemas in database
353
List schemas = new ArrayList(); //ordered collection, allows duplicates and null
354
List tables = new ArrayList(); // list of all tables in all schemas in database
355
Map tablesCount = new TreeMap(); // map of table names and the count of tables with this name in the database.
356

357         Vector allSchemas ;
358
359         private void addDbSchema(DbSchema dbs) {
360             schemas.add(dbs);
361         }
362
363         private void addDbTable(DbTable dbs) {
364             tables.add(dbs);
365             Integer JavaDoc count = (Integer JavaDoc) tablesCount.get(dbs.name);
366             if (count == null) {
367                 count = new Integer JavaDoc(1);
368             } else {
369                 count = new Integer JavaDoc(count.intValue()+1);
370             }
371             tablesCount.put(dbs.name, count);
372         }
373
374         private Vector getAllSchemas() {
375             if (allSchemas == null) {
376                 allSchemas = new Vector();
377                 if (schemas.size() > 0) {
378                     Iterator i = schemas.iterator();
379                     while (i.hasNext()) {
380                         allSchemas.add( ((DbSchema) i.next()).name );
381                     }
382                 }
383             }
384             return allSchemas;
385         }
386
387         private boolean tableExists(String JavaDoc sname, String JavaDoc tableName) {
388             if (sname == null || sname.equals("")) {
389                 return tablesCount.containsKey(tableName);
390             } else {
391                 Iterator i = schemas.iterator();
392                 while (i.hasNext()) {
393                     DbSchema s = (DbSchema) i.next();
394                     if ( s.name.equals(sname) ) {
395                         Iterator ti = s.tables.iterator();
396                         while (ti.hasNext()) {
397                             DbTable d = (DbTable) ti.next();
398                             if (d.name.equals(tableName)) {
399                                 return true;
400                             }
401                         }
402                         break;
403                     }
404                 }
405             }
406             return false;
407         }
408
409         private boolean colExists(String JavaDoc sname, String JavaDoc tableName, String JavaDoc colName) {
410             if (sname == null || sname.equals("")) {
411                 Iterator ti = tables.iterator();
412                 while (ti.hasNext()) {
413                     DbTable t = (DbTable) ti.next();
414                     if (t.name.equals(tableName)){
415                         return t.colsDataType.containsKey(colName);
416                     }
417                 }
418             } else {
419                 // return a vector of "fk col name" string objects if schema is given
420
Iterator i = schemas.iterator();
421                 while (i.hasNext()) {
422                     DbSchema s = (DbSchema) i.next();
423                     if ( s.name.equals(sname) ) {
424                         Iterator ti = s.tables.iterator();
425                         while (ti.hasNext()) {
426                             DbTable t = (DbTable) ti.next();
427                             if (t.name.equals(tableName)){
428                                 return t.colsDataType.containsKey(colName);
429                             }
430                         }
431                         break;
432                     }
433                 }
434             }
435
436             return false;
437         }
438
439         private Vector getAllTables(String JavaDoc sname) {
440             Vector v = new Vector();
441
442             if (sname == null || sname.equals("")) {
443                 // return a vector of "schemaname -> table name" string objects
444
Iterator i = tables.iterator();
445                 while (i.hasNext()) {
446                     DbTable d = (DbTable) i.next();
447                     if ( d.schemaName == null ) {
448                         v.add(d.name);
449                     } else {
450                         v.add(d.schemaName + "->"+ d.name);
451                     }
452                 }
453
454             } else {
455                 // return a vector of "tablename" string objects
456
Iterator i = schemas.iterator();
457                 while (i.hasNext()) {
458                     DbSchema s = (DbSchema) i.next();
459                     if ( s.name.equals(sname) ) {
460                         Iterator ti = s.tables.iterator();
461                         while (ti.hasNext()) {
462                             DbTable d = (DbTable) ti.next();
463                             v.add(d.name);
464                         }
465                         break;
466                     }
467                 }
468             }
469             return v;
470         }
471
472         private Vector getFactTables(String JavaDoc sname) {
473             Vector f = new Vector();
474
475             if (sname == null || sname.equals("")) {
476                 // return a vector of "schemaname -> table name" string objects if schema is not given
477
Iterator ti = tables.iterator();
478                 while (ti.hasNext()) {
479                     DbTable t = (DbTable) ti.next();
480                     if (t instanceof FactTable){
481                         if ( t.schemaName == null ) {
482                             f.add(t.name);
483                         } else {
484                             f.add(t.schemaName + "->"+ t.name);
485                         }
486                     }
487                 }
488             } else {
489                 // return a vector of "fact tablename" string objects if schema is given
490
Iterator i = schemas.iterator();
491                 while (i.hasNext()) {
492                     DbSchema s = (DbSchema) i.next();
493                     if ( s.name.equals(sname) ) {
494                         Iterator ti = s.tables.iterator();
495                         while (ti.hasNext()) {
496                             Object JavaDoc t = ti.next();
497                             if (t instanceof FactTable){
498                                 f.add(((FactTable)t).name);
499                             }
500                         }
501                         break;
502                     }
503                 }
504             }
505
506             return f;
507         }
508
509         /* get all foreign keys in given fact table */
510         private Vector getFactTableFKs(String JavaDoc sname, String JavaDoc factTable) {
511             Vector f = new Vector();
512
513             if (sname == null || sname.equals("")) {
514                 // return a vector of "schemaname -> table name -> fk col" string objects if schema is not given
515
boolean duplicate = (tablesCount.containsKey(factTable)) && (((Integer JavaDoc) tablesCount.get(factTable)).intValue() > 1);
516
517                 Iterator ti = tables.iterator();
518                 while (ti.hasNext()) {
519                     DbTable t = (DbTable) ti.next();
520                     if (t instanceof FactTable && t.name.equals(factTable)){
521                         if (duplicate) {
522                             Iterator fki = ((FactTable)t).fks.keySet().iterator();
523                             while (fki.hasNext()) {
524                                 String JavaDoc fk = (String JavaDoc) fki.next();
525                                 if ( t.schemaName == null ) {
526                                     f.add(t.name + "->" + fk);
527                                 } else {
528                                     f.add(t.schemaName + "->"+ t.name + "->" + fk);
529                                 }
530                             }
531                         } else {
532                             f.addAll( ((FactTable)t).fks.keySet());
533                         }
534                     }
535                 }
536             } else {
537                 // return a vector of "fk col name" string objects if schema is given
538
Iterator i = schemas.iterator();
539                 while (i.hasNext()) {
540                     DbSchema s = (DbSchema) i.next();
541                     if ( s.name.equals(sname) ) {
542                         Iterator ti = s.tables.iterator();
543                         while (ti.hasNext()) {
544                             DbTable t = (DbTable) ti.next();
545                             if (t instanceof FactTable && t.name.equals(factTable)){
546                                 f.addAll(((FactTable)t).fks.keySet());
547                                 break;
548                             }
549                         }
550                         break;
551                     }
552                 }
553             }
554             return f;
555         }
556
557         private Vector getDimensionTables(String JavaDoc sname, String JavaDoc factTable) {
558             Vector f = new Vector();
559
560             if (sname == null || sname.equals("")) {
561                 // return a vector of "schemaname -> table name -> dimension table name" string objects if schema is not given
562
boolean duplicate = (tablesCount.containsKey(factTable)) && (((Integer JavaDoc) tablesCount.get(factTable)).intValue() > 1);
563
564                 Iterator ti = tables.iterator();
565                 while (ti.hasNext()) {
566                     DbTable t = (DbTable) ti.next();
567                     if (t instanceof FactTable && t.name.equals(factTable)){
568                         if (duplicate) {
569                             Iterator fki = ((FactTable)t).fks.values().iterator();
570                             while (fki.hasNext()) {
571                                 String JavaDoc fkt = (String JavaDoc) fki.next();
572                                 if ( t.schemaName == null ) {
573                                     f.add(t.name + "->" + fkt);
574                                 } else {
575                                     f.add(t.schemaName + "->"+ t.name + "->" + fkt);
576                                 }
577                             }
578                         } else {
579                             f.addAll(((FactTable)t).fks.values());
580                             break;
581                         }
582                     }
583                 }
584             } else {
585                 // return a vector of "fk col name" string objects if schema is given
586
Iterator i = schemas.iterator();
587                 while (i.hasNext()) {
588                     DbSchema s = (DbSchema) i.next();
589                     if ( s.name.equals(sname) ) {
590                         Iterator ti = s.tables.iterator();
591                         while (ti.hasNext()) {
592                             DbTable t = (DbTable) ti.next();
593                             if (t instanceof FactTable && t.name.equals(factTable)){
594                                 f.addAll(((FactTable)t).fks.values());
595                                 break;
596                             }
597                         }
598                         break;
599                     }
600                 }
601             }
602             return f;
603         }
604
605         private String JavaDoc getTablePK(String JavaDoc sname, String JavaDoc tableName) {
606
607             if (sname == null || sname.equals("")) {
608                 // return a vector of "schemaname -> table name -> dimension table name" string objects if schema is not given
609
Iterator ti = tables.iterator();
610                 while (ti.hasNext()) {
611                     DbTable t = (DbTable) ti.next();
612                     if (t.name.equals(tableName)){
613                         return t.pk;
614                     }
615                 }
616             } else {
617                 // return a vector of "fk col name" string objects if schema is given
618
Iterator i = schemas.iterator();
619                 while (i.hasNext()) {
620                     DbSchema s = (DbSchema) i.next();
621                     if ( s.name.equals(sname) ) {
622                         Iterator ti = s.tables.iterator();
623                         while (ti.hasNext()) {
624                             DbTable t = (DbTable) ti.next();
625                             if (t.name.equals(tableName)){
626                                 return t.pk;
627                             }
628                         }
629                         break;
630                     }
631                 }
632             }
633             return null;
634         }
635
636         private Vector getAllColumns(String JavaDoc sname, String JavaDoc tableName) {
637             Vector f = new Vector();
638
639             if (sname == null || sname.equals("")) {
640                 // return a vector of "schemaname -> table name -> cols" string objects if schema is not given
641
boolean duplicate = (tablesCount.containsKey(tableName)) && (((Integer JavaDoc) tablesCount.get(tableName)).intValue() > 1);
642
643                 Iterator ti = tables.iterator();
644                 while (ti.hasNext()) {
645                     DbTable t = (DbTable) ti.next();
646                     if (t.name.equals(tableName)){
647                         if (duplicate) {
648                             Iterator ci = t.colsDataType.keySet().iterator();
649                             while (ci.hasNext()) {
650                                 String JavaDoc c = (String JavaDoc) ci.next();
651                                 if ( t.schemaName == null ) {
652                                     f.add(t.name + "->" + c);
653                                 } else {
654                                     f.add(t.schemaName + "->"+ t.name + "->" + c);
655                                 }
656                             }
657                         } else {
658                             f.addAll(t.colsDataType.keySet()); //display only col names
659
break;
660                         }
661                     }
662                 }
663             } else {
664                 // return a vector of "col name" string objects if schema is given
665
Iterator i = schemas.iterator();
666                 while (i.hasNext()) {
667                     DbSchema s = (DbSchema) i.next();
668                     if ( s.name.equals(sname) ) {
669                         Iterator ti = s.tables.iterator();
670                         while (ti.hasNext()) {
671                             DbTable t = (DbTable) ti.next();
672                             if (t.name.equals(tableName)){
673                                 f.addAll(t.colsDataType.keySet());
674                                 break;
675                             }
676                         }
677                         break;
678                     }
679                 }
680             }
681             return f;
682         }
683
684         private int getColumnDataType(String JavaDoc sname, String JavaDoc tableName, String JavaDoc colName) {
685
686             if (sname == null || sname.equals("")) {
687                 Iterator ti = tables.iterator();
688                 while (ti.hasNext()) {
689                     DbTable t = (DbTable) ti.next();
690                     if (t.name.equals(tableName)){
691                         int dataType = Integer.parseInt((String JavaDoc) t.colsDataType.get(colName));
692                         return dataType;
693                     }
694                 }
695             } else {
696                 // return a vector of "fk col name" string objects if schema is given
697
Iterator i = schemas.iterator();
698                 while (i.hasNext()) {
699                     DbSchema s = (DbSchema) i.next();
700                     if ( s.name.equals(sname) ) {
701                         Iterator ti = s.tables.iterator();
702                         while (ti.hasNext()) {
703                             DbTable t = (DbTable) ti.next();
704                             if (t.name.equals(tableName)){
705                                 int dataType = Integer.parseInt((String JavaDoc) t.colsDataType.get(colName));
706                                 return dataType;
707                             }
708                         }
709                         break;
710                     }
711                 }
712             }
713
714             return -1;
715         }
716
717     }
718
719     class DbSchema {
720         String JavaDoc name;
721         List tables = new ArrayList(); //ordered collection, allows duplicates and null
722

723         private void addDbTable(DbTable dbt){
724             tables.add(dbt);
725         }
726     }
727
728     class DbTable {
729         String JavaDoc schemaName;
730         String JavaDoc name;
731         String JavaDoc pk;
732         Map colsDataType = new TreeMap(); // sorted map key=column, value=data type of column
733

734         private void addColsDataType(String JavaDoc col, String JavaDoc dataType) {
735             colsDataType.put(col, dataType);
736         }
737
738     }
739
740     class FactTable extends DbTable {
741         Map fks = new TreeMap(); // sorted map key = foreign key col, value=primary key table associated with this fk
742

743         private void addFks(String JavaDoc fk, String JavaDoc pkt) {
744             fks.put(fk, pkt);
745         }
746     }
747 }
748
749
750 // End JDBCMetaData.java
751
Popular Tags