KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > mapping > TableMetaDataImpl


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.mapping;
24
25 import java.sql.DatabaseMetaData JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.*;
29
30 import org.xquark.jdbc.typing.ColumnMetaData;
31 import org.xquark.jdbc.typing.ColumnMetaDataImpl;
32 import org.xquark.mapper.dbms.AbstractConnection;
33
34 /**
35  * Table properties gained from the JDBC API.
36  *
37  * Owns a set of metadata objects for table's columns.
38  *
39  */

40 public class TableMetaDataImpl implements TableMetaData
41 {
42     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
43     private static final String JavaDoc RCSName = "$Name: $";
44
45     /* Table name */
46     private String JavaDoc name;
47     /* List of colum metadata */
48     private Map columnsMetaData;
49     /* Map {index name, {column metadatas}} (In fact unique indexes, JDBC does not provide constraints) */
50     private Map uniqueConstraints = new HashMap();
51     
52     private boolean statementsAreShareable = true;
53     private boolean shared = false;
54
55     /**
56      * Constructor.
57      * This constructor uses native information for column information
58      *
59      * Performs metadata construction, included for table's columns.
60      */

61     public TableMetaDataImpl(String JavaDoc name, AbstractConnection connection)
62         throws SQLException JavaDoc
63     {
64         DatabaseMetaData JavaDoc metadata = connection.getConnection().getMetaData();
65         this.name = name;
66         String JavaDoc catalog = connection.getConnection().getCatalog();
67         String JavaDoc schemaName = connection.getSchemaName();
68         ResultSet JavaDoc rs = null;
69         try
70         {
71             // Use database specific access for retrieving column info (Oracle
72
// JDBC implementation is incorrect
73
columnsMetaData = connection.getTableMetadata(name);
74
75             // PK
76
rs = metadata.getPrimaryKeys(catalog, schemaName, name);
77             while (rs.next())
78             {
79                 String JavaDoc cname = rs.getString(4);
80                 short seq = rs.getShort(5);
81                 ColumnMetaDataImpl cmeta =
82                     (ColumnMetaDataImpl) columnsMetaData.get(cname);
83                 cmeta.setPrimaryKeySeq(seq);
84             }
85             rs.close();
86
87             // FK
88
rs = metadata.getImportedKeys(catalog, schemaName, name);
89             while (rs.next())
90             {
91                 String JavaDoc pkTableName = rs.getString(3);
92                 String JavaDoc pkColumnName = rs.getString(4);
93                 String JavaDoc cname = rs.getString(8);
94                 ColumnMetaDataImpl cmeta =
95                     (ColumnMetaDataImpl) columnsMetaData.get(cname);
96                 cmeta.setRefTable(pkTableName);
97                 cmeta.setRefColumn(pkColumnName);
98             }
99             rs.close();
100
101             // Unique constraints
102
// Warning : JDBC does not provide access to UNIQUE constraints but
103
// only to Index which on Oracle match since Oracle create an index
104
// for these constraints.
105
try {
106                 rs = metadata.getIndexInfo(catalog, schemaName, name, true, true);
107                 while (rs.next())
108                 {
109                     String JavaDoc indexName = rs.getString(6);
110                     String JavaDoc indexColumnName = rs.getString(9);
111     
112                     ArrayList columns =
113                         (ArrayList) uniqueConstraints.get(indexName);
114                     if (columns == null)
115                     {
116                         columns = new ArrayList();
117                         uniqueConstraints.put(indexName, columns);
118                     }
119                     // put the column name in the set
120
columns.add(columnsMetaData.get(indexColumnName));
121                 }
122     
123                 rs.close();
124             }
125             catch (SQLException JavaDoc e) { // TODO: check if it is a JDBC oracle bug to automatically convert
126
// for this method the table name to uppercase. Either change driver or perform a native call.
127
if (connection.getUniversalError(e) != AbstractConnection.OBJECT_DOES_NOT_EXISTS)
128                     throw e;
129             }
130         }
131         finally
132         {
133             if (rs != null)
134                 rs.close();
135         }
136     }
137
138     //////////////////////////////////////////////////////////////////////////
139
// TableMetaData IMPLEMENTATION
140
//////////////////////////////////////////////////////////////////////////
141
public boolean areStatementsShareable()
142     {
143         return shared && statementsAreShareable;
144     }
145
146     public void setStatementsNotShareable()
147     {
148         statementsAreShareable = false;
149     }
150
151     public String JavaDoc getTableName()
152     {
153         return name;
154     }
155
156     public ColumnMetaData getColumnMetaData(String JavaDoc cname)
157     {
158         ColumnMetaData ret = (ColumnMetaData) columnsMetaData.get(cname);
159 // if (ret == null)
160
// // SR 18/12/2000 Oracle is case-sensitive for metadata !
161
// ret = (ColumnMetaData) columnsMetaData.get(cname.toUpperCase());
162
// if (ret == null)
163
// // SR 18/12/2000 Oracle is case-sensitive for metadata !
164
// ret = (ColumnMetaData) columnsMetaData.get(cname.toLowerCase());
165
return ret;
166     }
167
168     public Collection getColumnsMetaData()
169     {
170         return columnsMetaData.values();
171     }
172
173     public int getColumnCount()
174     {
175         return columnsMetaData.size();
176     }
177
178     public Collection getUniqueConstraints()
179     {
180         return uniqueConstraints.values();
181     }
182
183     public boolean isShared()
184     {
185         return shared;
186     }
187
188     public void setShared()
189     {
190         shared = true;
191     }
192
193 }
194
Popular Tags