KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > ColumnInfo


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: ColumnInfo.java,v 1.6 2003/02/05 18:15:14 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.io.StringWriter JavaDoc;
15 import java.sql.DatabaseMetaData JavaDoc;
16 import java.sql.ResultSet JavaDoc;
17 import java.sql.SQLException JavaDoc;
18 import java.sql.Types JavaDoc;
19 import javax.jdo.JDOFatalDataStoreException;
20
21
22 /**
23  * Represents the metadata of a specific table column. This class is
24  * basically a data structure that makes accessing the JDBC column metadata
25  * easier. Each of the items returned by
26  * {@link DatabaseMetaData#getColumns(String,String,String,String)}
27  * is represented by a public field in this class.
28  *
29  * Subclasses of ColumnInfo can be created on a per-DBMS basis to supply missing
30  * metadata or correct faulty metadata obtained from that DBMS's JDBC driver(s).
31  *
32  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
33  * @version $Revision: 1.6 $
34  *
35  * @see StoreManager#getColumnInfo
36  * @see DatabaseAdapter#newColumnInfo
37  * @see DatabaseMetaData#getColumns
38  */

39
40 class ColumnInfo
41 {
42     /**
43      * The table catalog, which may be <tt>null</tt>.
44      */

45     public String JavaDoc tableCat;
46
47     /**
48      * The table schema, which may be <tt>null</tt>.
49      */

50     public String JavaDoc tableSchem;
51
52     /**
53      * The table name.
54      */

55     public String JavaDoc tableName;
56
57     /**
58      * The column name.
59      */

60     public String JavaDoc columnName;
61
62     /**
63      * Indicates the JDBC (SQL) data type from {@link java.sql.Types}.
64      */

65     public short dataType;
66
67     /**
68      * The local type name used by the data source.
69      */

70     public String JavaDoc typeName;
71
72     /**
73      * Indicates the column size. For char or date types, this is the maximum
74      * number of characters; for numeric or decimal types, this is the precision.
75      */

76     public int columnSize;
77
78     /**
79      * Indicates the number of fractional digits.
80      */

81     public int decimalDigits;
82
83     /**
84      * Indicates the radix, which is typically either 10 or 2.
85      */

86     public int numPrecRadix;
87
88     /**
89      * Indicates whether the column can be NULL.
90      *
91      * @see DatabaseMetaData#columnNoNulls
92      * @see DatabaseMetaData#columnNullable
93      * @see DatabaseMetaData#columnNullableUnknown
94      */

95     public int nullable;
96
97     /**
98      * An explanatory comment on the column; may be <tt>null</tt>.
99      */

100     public String JavaDoc remarks;
101
102     /**
103      * The default value for the column; may be <tt>null</tt>.
104      */

105     public String JavaDoc columnDef;
106
107     /**
108      * Indicates the maximum number of bytes in the column (for char types
109      * only).
110      */

111     public int charOctetLength;
112
113     /**
114      * Indicates the index of the column in its table; the first column is 1,
115      * the second column is 2, and so on.
116      */

117     public int ordinalPosition;
118
119     /**
120      * Either "NO" indicating that the column definitely does not allow
121      * <tt>null</tt> values, "YES" indicating that the column might allow
122      * <tt>null</tt> values, or an empty string ("") indicating that nullability
123      * is unknown.
124      */

125     public String JavaDoc isNullable;
126
127
128     private int hash = 0;
129
130
131     /**
132      * Constructs a column information object from the current row of the given
133      * result set. The {@link ResultSet} object passed must have been obtained
134      * from a call to DatabaseMetaData.getColumns().
135      *
136      * <p>This method only retrieves the values from the current row; the caller
137      * is required to advance to the next row with {@link ResultSet#next}.
138      *
139      * @param rs The result set returned from DatabaseMetaData.getColumns().
140      *
141      * @exception JDOFatalDataStoreException
142      * if a column of column information could not be retrieved from the
143      * result set.
144      */

145
146     public ColumnInfo(ResultSet JavaDoc rs) throws JDOFatalDataStoreException
147     {
148         try
149         {
150             tableCat = rs.getString(1);
151             tableSchem = rs.getString(2);
152             tableName = rs.getString(3);
153             columnName = rs.getString(4);
154             dataType = rs.getShort(5);
155             typeName = rs.getString(6);
156             columnSize = rs.getInt(7);
157             decimalDigits = rs.getInt(9);
158             numPrecRadix = rs.getInt(10);
159             nullable = rs.getInt(11);
160             remarks = rs.getString(12);
161             columnDef = rs.getString(13);
162             charOctetLength = rs.getInt(16);
163             ordinalPosition = rs.getInt(17);
164             isNullable = rs.getString(18);
165         }
166         catch (SQLException JavaDoc e)
167         {
168             throw new JDOFatalDataStoreException("Can't read JDBC metadata from result set", e);
169         }
170
171         if (dataType == Types.OTHER)
172         {
173             /*
174              * This nonsense is for drivers (like Oracle's) that inexplicably
175              * declare some columns to be of type Types.OTHER when they should
176              * obviously know better.
177              */

178             String JavaDoc upperTypeName = typeName.toUpperCase();
179
180             if (upperTypeName.equals("BLOB"))
181                 dataType = Types.BLOB;
182             else if (upperTypeName.equals("CLOB"))
183                 dataType = Types.CLOB;
184             else if (upperTypeName.equals("FLOAT"))
185                 dataType = Types.FLOAT;
186             else if (upperTypeName.endsWith("LONGVARBINARY"))
187                 dataType = Types.LONGVARBINARY;
188             else if (upperTypeName.endsWith("LONGVARCHAR"))
189                 dataType = Types.LONGVARCHAR;
190         }
191     }
192
193
194     /**
195      * Constructs a column information object from its individual attributes.
196      *
197      * <p>This can be useful to subclasses and/or custom DatabaseAdapters that
198      * need to modify and/or correct the metadata returned by the JDBC driver.
199      */

200
201     public ColumnInfo(String JavaDoc tableCat,
202                       String JavaDoc tableSchem,
203                       String JavaDoc tableName,
204                       String JavaDoc columnName,
205                       short dataType,
206                       String JavaDoc typeName,
207                       int columnSize,
208                       int decimalDigits,
209                       int numPrecRadix,
210                       int nullable,
211                       String JavaDoc remarks,
212                       String JavaDoc columnDef,
213                       int charOctetLength,
214                       int ordinalPosition,
215                       String JavaDoc isNullable)
216     {
217         this.tableCat = tableCat;
218         this.tableSchem = tableSchem;
219         this.tableName = tableName;
220         this.columnName = columnName;
221         this.dataType = dataType;
222         this.typeName = typeName;
223         this.columnSize = columnSize;
224         this.decimalDigits = decimalDigits;
225         this.numPrecRadix = numPrecRadix;
226         this.nullable = nullable;
227         this.remarks = remarks;
228         this.columnDef = columnDef;
229         this.charOctetLength = charOctetLength;
230         this.ordinalPosition = ordinalPosition;
231         this.isNullable = isNullable;
232     }
233
234
235     /**
236      * Indicates whether some object is "equal to" this one. Two <tt>ColumnInfo
237      * </tt> objects are considered equal if their catalog, schema, table, and
238      * column names are all equal.
239      *
240      * @param obj the reference object with which to compare
241      *
242      * @return <tt>true</tt> if this object is equal to the obj argument;
243      * <tt>false</tt> otherwise.
244      */

245
246     public final boolean equals(Object JavaDoc obj)
247     {
248         if (obj == this)
249             return true;
250
251         if (!(obj instanceof ColumnInfo))
252             return false;
253
254         ColumnInfo ci = (ColumnInfo)obj;
255
256         return (tableCat == null ? ci.tableCat == null : tableCat.equals(ci.tableCat))
257             && (tableSchem == null ? ci.tableSchem == null : tableSchem.equals(ci.tableSchem))
258             && tableName.equals(ci.tableName)
259             && columnName.equals(ci.columnName);
260     }
261
262
263     /**
264      * Returns a hash code value for this object.
265      *
266      * @return a hash code value for this object.
267      */

268
269     public final int hashCode()
270     {
271         if (hash == 0)
272         {
273             hash = (tableCat == null ? 0 : tableCat.hashCode())
274                  ^ (tableSchem == null ? 0 : tableSchem.hashCode())
275                  ^ tableName.hashCode()
276                  ^ columnName.hashCode();
277         }
278
279         return hash;
280     }
281
282
283     /**
284      * Returns the string representation of this object.
285      *
286      * @return string representation of this object.
287      */

288
289     public String JavaDoc toString()
290     {
291         StringWriter JavaDoc sw = new StringWriter JavaDoc();
292         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
293
294         pw.println(this.getClass().getName());
295         pw.print(" tableCat = "); pw.println(tableCat);
296         pw.print(" tableSchem = "); pw.println(tableSchem);
297         pw.print(" tableName = "); pw.println(tableName);
298         pw.print(" columnName = "); pw.println(columnName);
299         pw.print(" dataType = "); pw.println(dataType);
300         pw.print(" typeName = "); pw.println(typeName);
301         pw.print(" columnSize = "); pw.println(columnSize);
302         pw.print(" decimalDigits = "); pw.println(decimalDigits);
303         pw.print(" numPrecRadix = "); pw.println(numPrecRadix);
304         pw.print(" nullable = "); pw.println(nullable);
305         pw.print(" remarks = "); pw.println(remarks);
306         pw.print(" columnDef = "); pw.println(columnDef);
307         pw.print(" charOctetLength = "); pw.println(charOctetLength);
308         pw.print(" ordinalPosition = "); pw.println(ordinalPosition);
309         pw.print(" isNullable = "); pw.println(isNullable);
310         pw.close();
311
312         return sw.toString();
313     }
314 }
315
Popular Tags