KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > db > JdbcTableMetaData


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib.db;
31
32 import com.caucho.util.Alarm;
33
34 import java.sql.DatabaseMetaData JavaDoc;
35 import java.sql.ResultSet JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.util.HashMap JavaDoc;
38
39 /**
40  * Represents a JDBC column metadata
41  */

42 public class JdbcTableMetaData {
43   private final String JavaDoc _catalog;
44   private final String JavaDoc _schema;
45   private final String JavaDoc _name;
46   
47   private final long _lastModified;
48
49   private final HashMap JavaDoc<String JavaDoc,JdbcColumnMetaData> _columnMap
50     = new HashMap JavaDoc<String JavaDoc,JdbcColumnMetaData>();
51
52   public JdbcTableMetaData(String JavaDoc catalog,
53                String JavaDoc schema,
54                String JavaDoc name,
55                DatabaseMetaData JavaDoc md)
56     throws SQLException JavaDoc
57   {
58     _catalog = catalog;
59     _schema = schema;
60     _name = name;
61     _lastModified = Alarm.getCurrentTime();
62
63     ResultSet JavaDoc rs = md.getColumns(_catalog, _schema, _name, null);
64     try {
65       while (rs.next()) {
66     // COLUMN_NAME
67
String JavaDoc columnName = rs.getString(4);
68
69     JdbcColumnMetaData column = new JdbcColumnMetaData(this, rs);
70
71     _columnMap.put(columnName, column);
72       }
73
74       rs.close();
75
76       rs = md.getPrimaryKeys(_catalog, _schema, _name);
77       while (rs.next()) {
78     // COLUMN_NAME
79
String JavaDoc columnName = rs.getString(4);
80
81     JdbcColumnMetaData column = _columnMap.get(columnName);
82
83     column.setPrimaryKey(true);
84       }
85       rs.close();
86
87       rs = md.getIndexInfo(_catalog, _schema, _name, false, true);
88       while (rs.next()) {
89     // COLUMN_NAME
90
String JavaDoc columnName = rs.getString(9);
91
92     JdbcColumnMetaData column = _columnMap.get(columnName);
93
94     column.setIndex(true);
95       }
96     } finally {
97       rs.close();
98     }
99   }
100
101   /**
102    * Returns the table's name.
103    */

104   public String JavaDoc getName()
105   {
106     return _name;
107   }
108
109   /**
110    * Returns the table's catalog
111    */

112   public String JavaDoc getCatalog()
113   {
114     return _catalog;
115   }
116
117   /**
118    * Returns the matching column.
119    */

120   public JdbcColumnMetaData getColumn(String JavaDoc name)
121   {
122     return _columnMap.get(name);
123   }
124
125   public boolean isValid()
126   {
127     return Alarm.getCurrentTime() - _lastModified < 1000L;
128   }
129
130   public String JavaDoc toString()
131   {
132     return "JdbcTableMetaData[" + getName() + "]";
133   }
134 }
135
136
Popular Tags