View Javadoc

1   package com.workingdogs.village;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.sql.ResultSetMetaData;
23  import java.sql.SQLException;
24  import java.sql.Types;
25  
26  /***
27   * This class represents a Column in the database and its associated meta information. A <a href="Record.html">Record</A> is a
28   * collection of columns.
29   *
30   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
31   * @version $Revision: 568 $
32   */
33  public class Column
34  {
35      /*** column number in a schema object */
36      private int columnNumber = -1;
37  
38      /*** name of the column */
39      private String name = "";
40  
41      /*** example: this column is of type "String" */
42      private String columnTypeName = "";
43  
44      /*** what java.sql.Type is this column? */
45      private int columnType = Types.LONGVARCHAR;
46  
47      /*** name of table that this column belongs to */
48      private String tableName = "";
49  
50      /*** is null allowed for this column? */
51      private boolean nullAllowed = false;
52  
53      /*** is this an auto increment column? */
54      private boolean autoIncrement = false;
55  
56      /*** is this a read only column? */
57      private boolean readOnly = false;
58  
59      /*** is this a searchable column? */
60      private boolean searchable = false;
61  
62      /*** what is the scale of this column? */
63      private int scale = -1;
64  
65      /*** what is the precision of this column? */
66      private int precision = -1;
67  
68      /*** what is the length of this column? */
69      private int length = -1;
70  
71      /***
72       * constructor
73       */
74      public Column()
75      {
76          this.columnNumber = -1;
77          this.name = "";
78          this.columnTypeName = "";
79          this.tableName = "";
80          this.columnType = Types.LONGVARCHAR;
81          this.nullAllowed = false;
82          this.autoIncrement = false;
83          this.readOnly = false;
84          this.searchable = false;
85          this.scale = -1;
86          this.precision = -1;
87          this.length = -1;
88      }
89  
90      /***
91       * internal package method for populating a Column instance
92       *
93       * @param rsmd TODO: DOCUMENT ME!
94       * @param colNum TODO: DOCUMENT ME!
95       * @param tableName TODO: DOCUMENT ME!
96       *
97       * @throws SQLException TODO: DOCUMENT ME!
98       */
99      void populate(ResultSetMetaData rsmd, int colNum, String tableName)
100             throws SQLException
101     {
102         this.columnNumber = colNum;
103         this.name = rsmd.getColumnName(columnNumber);
104 
105         // Workaround for Sybase jConnect 5.2 and older.
106         try
107         {
108             this.tableName = rsmd.getTableName(columnNumber);
109 
110             // ResultSetMetaData may report table name as the empty
111             // string when a database-specific function has been
112             // called to generate a Column.
113             if ((this.tableName == null) || this.tableName.equals(""))
114             {
115                 if (tableName != null)
116                 {
117                     this.tableName = tableName;
118                 }
119                 else
120                 {
121                     this.tableName = "";
122                 }
123             }
124         }
125         catch (RuntimeException e)
126         {
127             if (tableName != null)
128             {
129                 this.tableName = tableName;
130             }
131             else
132             {
133                 this.tableName = "";
134             }
135         }
136 
137         this.columnTypeName = rsmd.getColumnTypeName(columnNumber);
138         this.columnType = rsmd.getColumnType(columnNumber);
139         this.nullAllowed = rsmd.isNullable(columnNumber) == 1;
140         this.autoIncrement = rsmd.isAutoIncrement(columnNumber);
141 
142         // The JDBC spec is VERY unclear about what this means and as 
143         // such, it should be ignored.  Derby returns true all the time.
144         // Sybase and Informix say it's unsupported (and false).
145         this.readOnly = false; // rsmd.isReadOnly (columnNumber);
146         
147         this.searchable = rsmd.isSearchable(columnNumber);
148         this.scale = rsmd.getScale(columnNumber);
149 
150         try
151         {
152             this.precision = rsmd.getPrecision(columnNumber);
153         }
154         catch (NumberFormatException assumedTooLarge)
155         {
156             // This may happen if the precision is too large for an
157             // int, with column types such as MySQL BIGINT, Oracle
158             // BLOB, etc..  See bug #4625851 at the JDC for details.
159             this.precision = Integer.MAX_VALUE;
160         }
161 
162         this.length = rsmd.getColumnDisplaySize(columnNumber);
163     }
164 
165     /***
166      * the name of the column
167      *
168      * @return the name of the column
169      */
170     public String name()
171     {
172         return this.name;
173     }
174 
175     /***
176      * the data type of a column
177      *
178      * @return the java.sql.Types String
179      */
180     public String dbType()
181     {
182         return this.columnTypeName;
183     }
184 
185     /***
186      * the data type of a column
187      *
188      * @return the java.sql.Types enum
189      */
190     public int typeEnum()
191     {
192         return this.columnType;
193     }
194 
195     /***
196      * does this column allow null?
197      *
198      * @return whether or not the column has null Allowed
199      */
200     public boolean nullAllowed()
201     {
202         return this.nullAllowed;
203     }
204 
205     /***
206      * does this column auto increment?
207      *
208      * @return whether or not this column auto increments
209      */
210     public boolean autoIncrement()
211     {
212         return this.autoIncrement;
213     }
214 
215     /***
216      * is this column read only?
217      *
218      * @return whether or not this column is read only
219      */
220     public boolean readOnly()
221     {
222         return this.readOnly;
223     }
224 
225     /***
226      * is this column searchable?
227      *
228      * @return true if this column is searchable
229      */
230     public boolean searchable()
231     {
232         return this.searchable;
233     }
234 
235     /***
236      * the scale of the column
237      *
238      * @return the scale of the column
239      */
240     public int scale()
241     {
242         return this.scale;
243     }
244 
245     /***
246      * the precision of the column
247      *
248      * @return the precision of the column
249      */
250     public int precision()
251     {
252         return this.precision;
253     }
254 
255     /***
256      * the storage length of a column
257      *
258      * @return the storage length of a column
259      */
260     public int length()
261     {
262         return this.length;
263     }
264 
265     /***
266      * the type of the column as a string
267      *
268      * @return the type of the column as a string
269      */
270     public String type()
271     {
272         if (isBoolean())
273         {
274             return "BOOLEAN";
275         }
276         else if (isByte())
277         {
278             return "BYTE";
279         }
280         else if (isShort())
281         {
282             return "SHORT";
283         }
284         else if (isInt())
285         {
286             return "INTEGER";
287         }
288         else if (isLong())
289         {
290             return "LONG";
291         }
292         else if (isFloat())
293         {
294             return "FLOAT";
295         }
296         else if (isDouble())
297         {
298             return "DOUBLE";
299         }
300         else if (isBigDecimal())
301         {
302             return "BIGDECIMAL";
303         }
304         else if (isDate())
305         {
306             return "DATE";
307         }
308         else if (isTime())
309         {
310             return "TIME";
311         }
312         else if (isTimestamp())
313         {
314             return "TIMESTAMP";
315         }
316         else if (isString())
317         {
318             return "STRING";
319         }
320         else if (isBinary())
321         {
322             return "BINARY";
323         }
324         else if (isVarBinary())
325         {
326             return "VARBINARY";
327         }
328         else if (isLongVarBinary())
329         {
330             return "LONGVARBINARY";
331         }
332 
333         return "UNKNOWN TYPE: " + typeEnum();
334     }
335 
336     /***
337      * column isBoolean: -7
338      *
339      * @return TODO: DOCUMENT ME!
340      */
341     public boolean isBoolean()
342     {
343         return this.typeEnum() == Types.BIT;
344     }
345 
346     /***
347      * column isBigDecimal: 2 || 3
348      *
349      * @return TODO: DOCUMENT ME!
350      */
351     public boolean isBigDecimal()
352     {
353         return (this.typeEnum() == Types.NUMERIC) || (this.typeEnum() == Types.DECIMAL);
354     }
355 
356     /***
357      * column isBinary: -2
358      *
359      * @return TODO: DOCUMENT ME!
360      */
361     public boolean isBinary()
362     {
363         return this.typeEnum() == Types.BINARY;
364     }
365 
366     /***
367      * column isByte: -6
368      *
369      * @return TODO: DOCUMENT ME!
370      */
371     public boolean isByte()
372     {
373         return this.typeEnum() == Types.TINYINT;
374     }
375 
376     /***
377      * column isBytes: -4 || -3 || -2
378      *
379      * @return TODO: DOCUMENT ME!
380      */
381     public boolean isBytes()
382     {
383         return (this.typeEnum() == Types.LONGVARBINARY)
384                 || (this.typeEnum() == Types.VARBINARY)
385                 || (this.columnType == Types.BINARY);
386     }
387 
388     /***
389      * column isBytes: 91
390      *
391      * @return TODO: DOCUMENT ME!
392      */
393     public boolean isDate()
394     {
395         return this.typeEnum() == Types.DATE;
396     }
397 
398     /***
399      * column isDouble: 6 || 8
400      *
401      * @return TODO: DOCUMENT ME!
402      */
403     public boolean isDouble()
404     {
405         return (this.typeEnum() == Types.FLOAT) || (this.typeEnum() == Types.DOUBLE);
406     }
407 
408     /***
409      * column isFloat: 7
410      *
411      * @return TODO: DOCUMENT ME!
412      */
413     public boolean isFloat()
414     {
415         return this.typeEnum() == Types.REAL;
416     }
417 
418     /***
419      * column isInt: 4
420      *
421      * @return TODO: DOCUMENT ME!
422      */
423     public boolean isInt()
424     {
425         return this.typeEnum() == Types.INTEGER;
426     }
427 
428     /***
429      * column isLong: -5
430      *
431      * @return TODO: DOCUMENT ME!
432      */
433     public boolean isLong()
434     {
435         return this.typeEnum() == Types.BIGINT;
436     }
437 
438     /***
439      * column isShort: 5
440      *
441      * @return TODO: DOCUMENT ME!
442      */
443     public boolean isShort()
444     {
445         return this.typeEnum() == Types.SMALLINT;
446     }
447 
448     /***
449      * column isString: -1 || -11 || 12
450      *
451      * @return TODO: DOCUMENT ME!
452      */
453     public boolean isString()
454     {
455         return (this.typeEnum() == Types.LONGVARCHAR)
456                 || (this.typeEnum() == Types.VARCHAR)
457                 || (this.typeEnum() == 11);
458     }
459 
460     /***
461      * column isTime: 92
462      *
463      * @return TODO: DOCUMENT ME!
464      */
465     public boolean isTime()
466     {
467         return this.typeEnum() == Types.TIME;
468     }
469 
470     /***
471      * column isTimestamp: 93
472      *
473      * @return TODO: DOCUMENT ME!
474      */
475     public boolean isTimestamp()
476     {
477         return this.typeEnum() == Types.TIMESTAMP;
478     }
479 
480     /***
481      * column isVarBinary: -3
482      *
483      * @return TODO: DOCUMENT ME!
484      */
485     public boolean isVarBinary()
486     {
487         return this.typeEnum() == Types.VARBINARY;
488     }
489 
490     /***
491      * column isLongVarBinary: -4
492      *
493      * @return TODO: DOCUMENT ME!
494      */
495     public boolean isLongVarBinary()
496     {
497         return this.typeEnum() == Types.LONGVARBINARY;
498     }
499 
500     /***
501      * unknown use
502      *
503      * @return TODO: DOCUMENT ME!
504      *
505      * @throws DataSetException TODO: DOCUMENT ME!
506      */
507     public String dbKonaMethod()
508             throws DataSetException
509     {
510         throw new DataSetException("Method not implemented: Unknown use!");
511     }
512 
513     /***
514      * unknown use
515      *
516      * @return TODO: DOCUMENT ME!
517      *
518      * @throws DataSetException TODO: DOCUMENT ME!
519      */
520     public String javaType()
521             throws DataSetException
522     {
523         throw new DataSetException("Method not implemented: Unknown use!");
524     }
525 
526     /***
527      * unknown use
528      *
529      * @return TODO: DOCUMENT ME!
530      *
531      * @throws DataSetException TODO: DOCUMENT ME!
532      */
533     public final String preparedStatemntBindMethod()
534             throws DataSetException
535     {
536         throw new DataSetException("Method not implemented: Unknown use!");
537     }
538 
539     /***
540      * unknown use
541      *
542      * @return TODO: DOCUMENT ME!
543      *
544      * @throws DataSetException TODO: DOCUMENT ME!
545      */
546     public final String resultSetMethod()
547             throws DataSetException
548     {
549         throw new DataSetException("Method not implemented: Unknown use!");
550     }
551 
552     /***
553      * TODO: DOCUMENT ME!
554      *
555      * @return TODO: DOCUMENT ME!
556      */
557     public String getTableName()
558     {
559         return tableName;
560     }
561 }