View Javadoc

1   /*
2    * The Working-Dogs.com License, Version 1.1
3    *
4    * Copyright (c) 1999 Working-Dogs.com.  All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in
15   *    the documentation and/or other materials provided with the
16   *    distribution.
17   *
18   * 3. The end-user documentation included with the redistribution, if
19   *    any, must include the following acknowlegement:
20   *       "This product includes software developed by the
21   *        Working-Dogs.com <http://www.Working-Dogs.com/>."
22   *    Alternately, this acknowlegement may appear in the software itself,
23   *    if and wherever such third-party acknowlegements normally appear.
24   *
25   * 4. The names "Working-Dogs.com" and "Village" must not be used to
26   *    endorse or promote products derived from this software without
27   *    prior written permission. For written permission, please contact
28   *    jon@working-dogs.com.
29   *
30   * 5. Products derived from this software may not be called
31   *    "Working-Dogs.com" nor may "Village" appear in their names
32   *    without prior written permission of Working-Dogs.com.
33   *
34   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
38   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45   * SUCH DAMAGE.
46   * ====================================================================
47   *
48   * This software consists of voluntary contributions made by many
49   * individuals on behalf of the Working-Dogs.com.  For more
50   * information on the Working-Dogs.com, please see
51   * <http://www.Working-Dogs.com/>.
52   */
53  package com.workingdogs.village;
54  
55  import java.sql.ResultSetMetaData;
56  import java.sql.SQLException;
57  import java.sql.Types;
58  
59  /***
60   * This class represents a Column in the database and its associated meta information. A <a href="Record.html">Record</A> is a
61   * collection of columns.
62   *
63   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
64   * @version $Revision: 568 $
65   */
66  public class Column
67  {
68      /*** column number in a schema object */
69      private int columnNumber = -1;
70  
71      /*** name of the column */
72      private String name = "";
73  
74      /*** example: this column is of type "String" */
75      private String columnTypeName = "";
76  
77      /*** what java.sql.Type is this column? */
78      private int columnType = Types.LONGVARCHAR;
79  
80      /*** name of table that this column belongs to */
81      private String tableName = "";
82  
83      /*** is null allowed for this column? */
84      private boolean nullAllowed = false;
85  
86      /*** is this an auto increment column? */
87      private boolean autoIncrement = false;
88  
89      /*** is this a read only column? */
90      private boolean readOnly = false;
91  
92      /*** is this a searchable column? */
93      private boolean searchable = false;
94  
95      /*** what is the scale of this column? */
96      private int scale = -1;
97  
98      /*** what is the precision of this column? */
99      private int precision = -1;
100 
101     /*** what is the length of this column? */
102     private int length = -1;
103 
104     /***
105      * constructor
106      */
107     public Column()
108     {
109         this.columnNumber = -1;
110         this.name = "";
111         this.columnTypeName = "";
112         this.tableName = "";
113         this.columnType = Types.LONGVARCHAR;
114         this.nullAllowed = false;
115         this.autoIncrement = false;
116         this.readOnly = false;
117         this.searchable = false;
118         this.scale = -1;
119         this.precision = -1;
120         this.length = -1;
121     }
122 
123     /***
124      * internal package method for populating a Column instance
125      *
126      * @param rsmd TODO: DOCUMENT ME!
127      * @param colNum TODO: DOCUMENT ME!
128      * @param tableName TODO: DOCUMENT ME!
129      *
130      * @throws SQLException TODO: DOCUMENT ME!
131      */
132     void populate(ResultSetMetaData rsmd, int colNum, String tableName)
133             throws SQLException
134     {
135         this.columnNumber = colNum;
136         this.name = rsmd.getColumnName(columnNumber);
137 
138         // Workaround for Sybase jConnect 5.2 and older.
139         try
140         {
141             this.tableName = rsmd.getTableName(columnNumber);
142 
143             // ResultSetMetaData may report table name as the empty
144             // string when a database-specific function has been
145             // called to generate a Column.
146             if ((this.tableName == null) || this.tableName.equals(""))
147             {
148                 if (tableName != null)
149                 {
150                     this.tableName = tableName;
151                 }
152                 else
153                 {
154                     this.tableName = "";
155                 }
156             }
157         }
158         catch (RuntimeException e)
159         {
160             if (tableName != null)
161             {
162                 this.tableName = tableName;
163             }
164             else
165             {
166                 this.tableName = "";
167             }
168         }
169 
170         this.columnTypeName = rsmd.getColumnTypeName(columnNumber);
171         this.columnType = rsmd.getColumnType(columnNumber);
172         this.nullAllowed = rsmd.isNullable(columnNumber) == 1;
173         this.autoIncrement = rsmd.isAutoIncrement(columnNumber);
174         this.readOnly = rsmd.isReadOnly(columnNumber);
175         this.searchable = rsmd.isSearchable(columnNumber);
176         this.scale = rsmd.getScale(columnNumber);
177 
178         try
179         {
180             this.precision = rsmd.getPrecision(columnNumber);
181         }
182         catch (NumberFormatException assumedTooLarge)
183         {
184             // This may happen if the precision is too large for an
185             // int, with column types such as MySQL BIGINT, Oracle
186             // BLOB, etc..  See bug #4625851 at the JDC for details.
187             this.precision = Integer.MAX_VALUE;
188         }
189 
190         this.length = rsmd.getColumnDisplaySize(columnNumber);
191     }
192 
193     /***
194      * the name of the column
195      *
196      * @return the name of the column
197      */
198     public String name()
199     {
200         return this.name;
201     }
202 
203     /***
204      * the data type of a column
205      *
206      * @return the java.sql.Types String
207      */
208     public String dbType()
209     {
210         return this.columnTypeName;
211     }
212 
213     /***
214      * the data type of a column
215      *
216      * @return the java.sql.Types enum
217      */
218     public int typeEnum()
219     {
220         return this.columnType;
221     }
222 
223     /***
224      * does this column allow null?
225      *
226      * @return whether or not the column has null Allowed
227      */
228     public boolean nullAllowed()
229     {
230         return this.nullAllowed;
231     }
232 
233     /***
234      * does this column auto increment?
235      *
236      * @return whether or not this column auto increments
237      */
238     public boolean autoIncrement()
239     {
240         return this.autoIncrement;
241     }
242 
243     /***
244      * is this column read only?
245      *
246      * @return whether or not this column is read only
247      */
248     public boolean readOnly()
249     {
250         return this.readOnly;
251     }
252 
253     /***
254      * is this column searchable?
255      *
256      * @return true if this column is searchable
257      */
258     public boolean searchable()
259     {
260         return this.searchable;
261     }
262 
263     /***
264      * the scale of the column
265      *
266      * @return the scale of the column
267      */
268     public int scale()
269     {
270         return this.scale;
271     }
272 
273     /***
274      * the precision of the column
275      *
276      * @return the precision of the column
277      */
278     public int precision()
279     {
280         return this.precision;
281     }
282 
283     /***
284      * the storage length of a column
285      *
286      * @return the storage length of a column
287      */
288     public int length()
289     {
290         return this.length;
291     }
292 
293     /***
294      * the type of the column as a string
295      *
296      * @return the type of the column as a string
297      */
298     public String type()
299     {
300         if (isBoolean())
301         {
302             return "BOOLEAN";
303         }
304         else if (isByte())
305         {
306             return "BYTE";
307         }
308         else if (isShort())
309         {
310             return "SHORT";
311         }
312         else if (isInt())
313         {
314             return "INTEGER";
315         }
316         else if (isLong())
317         {
318             return "LONG";
319         }
320         else if (isFloat())
321         {
322             return "FLOAT";
323         }
324         else if (isDouble())
325         {
326             return "DOUBLE";
327         }
328         else if (isBigDecimal())
329         {
330             return "BIGDECIMAL";
331         }
332         else if (isDate())
333         {
334             return "DATE";
335         }
336         else if (isTime())
337         {
338             return "TIME";
339         }
340         else if (isTimestamp())
341         {
342             return "TIMESTAMP";
343         }
344         else if (isString())
345         {
346             return "STRING";
347         }
348         else if (isBinary())
349         {
350             return "BINARY";
351         }
352         else if (isVarBinary())
353         {
354             return "VARBINARY";
355         }
356         else if (isLongVarBinary())
357         {
358             return "LONGVARBINARY";
359         }
360 
361         return "UNKNOWN TYPE: " + typeEnum();
362     }
363 
364     /***
365      * column isBoolean: -7
366      *
367      * @return TODO: DOCUMENT ME!
368      */
369     public boolean isBoolean()
370     {
371         return this.typeEnum() == Types.BIT;
372     }
373 
374     /***
375      * column isBigDecimal: 2 || 3
376      *
377      * @return TODO: DOCUMENT ME!
378      */
379     public boolean isBigDecimal()
380     {
381         return (this.typeEnum() == Types.NUMERIC) || (this.typeEnum() == Types.DECIMAL);
382     }
383 
384     /***
385      * column isBinary: -2
386      *
387      * @return TODO: DOCUMENT ME!
388      */
389     public boolean isBinary()
390     {
391         return this.typeEnum() == Types.BINARY;
392     }
393 
394     /***
395      * column isByte: -6
396      *
397      * @return TODO: DOCUMENT ME!
398      */
399     public boolean isByte()
400     {
401         return this.typeEnum() == Types.TINYINT;
402     }
403 
404     /***
405      * column isBytes: -4 || -3 || -2
406      *
407      * @return TODO: DOCUMENT ME!
408      */
409     public boolean isBytes()
410     {
411         return (this.typeEnum() == Types.LONGVARBINARY)
412                 || (this.typeEnum() == Types.VARBINARY)
413                 || (this.columnType == Types.BINARY);
414     }
415 
416     /***
417      * column isBytes: 91
418      *
419      * @return TODO: DOCUMENT ME!
420      */
421     public boolean isDate()
422     {
423         return this.typeEnum() == Types.DATE;
424     }
425 
426     /***
427      * column isDouble: 6 || 8
428      *
429      * @return TODO: DOCUMENT ME!
430      */
431     public boolean isDouble()
432     {
433         return (this.typeEnum() == Types.FLOAT) || (this.typeEnum() == Types.DOUBLE);
434     }
435 
436     /***
437      * column isFloat: 7
438      *
439      * @return TODO: DOCUMENT ME!
440      */
441     public boolean isFloat()
442     {
443         return this.typeEnum() == Types.REAL;
444     }
445 
446     /***
447      * column isInt: 4
448      *
449      * @return TODO: DOCUMENT ME!
450      */
451     public boolean isInt()
452     {
453         return this.typeEnum() == Types.INTEGER;
454     }
455 
456     /***
457      * column isLong: -5
458      *
459      * @return TODO: DOCUMENT ME!
460      */
461     public boolean isLong()
462     {
463         return this.typeEnum() == Types.BIGINT;
464     }
465 
466     /***
467      * column isShort: 5
468      *
469      * @return TODO: DOCUMENT ME!
470      */
471     public boolean isShort()
472     {
473         return this.typeEnum() == Types.SMALLINT;
474     }
475 
476     /***
477      * column isString: -1 || -11 || 12
478      *
479      * @return TODO: DOCUMENT ME!
480      */
481     public boolean isString()
482     {
483         return (this.typeEnum() == Types.LONGVARCHAR)
484                 || (this.typeEnum() == Types.VARCHAR)
485                 || (this.typeEnum() == 11);
486     }
487 
488     /***
489      * column isTime: 92
490      *
491      * @return TODO: DOCUMENT ME!
492      */
493     public boolean isTime()
494     {
495         return this.typeEnum() == Types.TIME;
496     }
497 
498     /***
499      * column isTimestamp: 93
500      *
501      * @return TODO: DOCUMENT ME!
502      */
503     public boolean isTimestamp()
504     {
505         return this.typeEnum() == Types.TIMESTAMP;
506     }
507 
508     /***
509      * column isVarBinary: -3
510      *
511      * @return TODO: DOCUMENT ME!
512      */
513     public boolean isVarBinary()
514     {
515         return this.typeEnum() == Types.VARBINARY;
516     }
517 
518     /***
519      * column isLongVarBinary: -4
520      *
521      * @return TODO: DOCUMENT ME!
522      */
523     public boolean isLongVarBinary()
524     {
525         return this.typeEnum() == Types.LONGVARBINARY;
526     }
527 
528     /***
529      * unknown use
530      *
531      * @return TODO: DOCUMENT ME!
532      *
533      * @throws DataSetException TODO: DOCUMENT ME!
534      */
535     public String dbKonaMethod()
536             throws DataSetException
537     {
538         throw new DataSetException("Method not implemented: Unknown use!");
539     }
540 
541     /***
542      * unknown use
543      *
544      * @return TODO: DOCUMENT ME!
545      *
546      * @throws DataSetException TODO: DOCUMENT ME!
547      */
548     public String javaType()
549             throws DataSetException
550     {
551         throw new DataSetException("Method not implemented: Unknown use!");
552     }
553 
554     /***
555      * unknown use
556      *
557      * @return TODO: DOCUMENT ME!
558      *
559      * @throws DataSetException TODO: DOCUMENT ME!
560      */
561     public final String preparedStatemntBindMethod()
562             throws DataSetException
563     {
564         throw new DataSetException("Method not implemented: Unknown use!");
565     }
566 
567     /***
568      * unknown use
569      *
570      * @return TODO: DOCUMENT ME!
571      *
572      * @throws DataSetException TODO: DOCUMENT ME!
573      */
574     public final String resultSetMethod()
575             throws DataSetException
576     {
577         throw new DataSetException("Method not implemented: Unknown use!");
578     }
579 
580     /***
581      * TODO: DOCUMENT ME!
582      *
583      * @return TODO: DOCUMENT ME!
584      */
585     public String getTableName()
586     {
587         return tableName;
588     }
589 }