View Javadoc

1   package org.yajul.sql;
2   
3   import org.slf4j.Logger;
4   import org.slf4j.LoggerFactory;
5   
6   import java.io.Serializable;
7   import java.sql.ResultSet;
8   import java.sql.SQLException;
9   import java.sql.Types;
10  
11  /***
12   * Bean that encapsulates database column metadata.
13   * <br>
14   * User: jdavis
15   * Date: Aug 4, 2003
16   * Time: 6:05:02 PM
17   *
18   * @author jdavis
19   */
20  public class ColumnMetaData implements Serializable {
21      private static final Logger log = LoggerFactory.getLogger(ColumnMetaData.class);
22  
23      private String name;
24      private String tableName;
25      private short dataType;
26      private String dataTypeName;
27      private int columnSize;
28      private int nullable;
29  
30      /***
31       * Creates column meta data from the column name, table name, and result
32       * set positioned at the proper column.
33       *
34       * @param name      The name of the column.
35       * @param tableName The name of the table the column is in.
36       * @param rs        The result set containing the rest of the information.
37       * @throws SQLException If there is a problem reading the
38       *                      meta data result set.
39       */
40      public ColumnMetaData(String name, String tableName, ResultSet rs)
41              throws SQLException {
42          this.name = name;
43          this.tableName = tableName;
44  /*
45  1.  TABLE_CAT String => table catalog (may be null)
46  2.  TABLE_SCHEM String => table schema (may be null)
47  3.  TABLE_NAME String => table name
48  4.  COLUMN_NAME String => column name
49  5.  DATA_TYPE short => SQL type from java.sql.Types
50  6.  TYPE_NAME String => Data source dependent type name, for a UDT the type name
51      is fully qualified
52  7.  COLUMN_SIZE int => column size. For char or date types this is the maximum
53      number of characters, for numeric or decimal types this is precision.
54  8.  BUFFER_LENGTH is not used.
55  9.  DECIMAL_DIGITS int => the number of fractional digits
56  10. NUM_PREC_RADIX int => Radix (typically either 10 or 2)
57  11. NULLABLE int => is NULL allowed?
58          columnNoNulls - might not allow NULL values
59          columnNullable - definitely allows NULL values
60          columnNullableUnknown - nullability unknown
61  12. REMARKS String => comment describing column (may be null)
62  13. COLUMN_DEF String => default value (may be null)
63  14. SQL_DATA_TYPE int => unused
64  15. SQL_DATETIME_SUB int => unused
65  16. CHAR_OCTET_LENGTH int => for char types the maximum number of
66      bytes in the column
67  17. ORDINAL_POSITION int => index of column in table (starting at 1)
68  18. IS_NULLABLE String => "NO" means column definitely does not allow
69      NULL values; "YES" means the column might allow NULL values. An empty
70      string means nobody knows.
71  */
72  
73          this.dataType = rs.getShort(5);
74          this.dataTypeName = rs.getString(6);
75          this.columnSize = rs.getInt(7);
76          this.nullable = rs.getInt(11);
77      }
78  
79      /***
80       * Returns the name of the column.
81       *
82       * @return String - The name of the column.
83       */
84      public String getName() {
85          return name;
86      }
87  
88      /***
89       * Returns the table name that the column is in.
90       *
91       * @return String - The name of the table.
92       */
93      public String getTableName() {
94          return tableName;
95      }
96  
97      /***
98       * Returns the column data type (see java.sql.Types).
99       *
100      * @return int - The column data type.
101      * @see java.sql.Types
102      */
103     public short getDataType() {
104         return dataType;
105     }
106 
107     /***
108      * Returns the name of the data type.
109      *
110      * @return Sring - The name of the datatype.
111      */
112     public String getDataTypeName() {
113         return dataTypeName;
114     }
115 
116     /***
117      * Returns the column size.
118      *
119      * @return int - The number of bytes in the column.
120      */
121     public int getColumnSize() {
122         return columnSize;
123     }
124 
125     /***
126      * Returns the nullalbe attribute of the column.
127      *
128      * @return int - Nullable attribute.
129      */
130     public int getNullable() {
131         return nullable;
132     }
133 
134     /***
135      * Returns true if the column is a type compatible with 'int'.
136      *
137      * @return boolean - True if the column is an int-compatible column.
138      */
139     public boolean isIntType() {
140         return isIntType(dataType);
141     }
142 
143     /***
144      * Returns true if the column is a type compatible with 'String'.
145      *
146      * @return boolean - True if the column is a String-compatible column.
147      */
148     public boolean isStringType() {
149         return isStringType(dataType);
150     }
151 
152     /***
153      * Returns true if the column is a type compatible with 'int'.
154      *
155      * @param dataType The data type (see java.sql.Types)
156      * @return boolean - True if the column is an int-compatible column.
157      * @see java.sql.Types
158      */
159     public static boolean isIntType(int dataType) {
160         if (log.isDebugEnabled())
161             log.debug("isIntType() : dataType = " + dataType);
162         switch (dataType) {
163             // --- Lossless conversion ---
164 
165             // These types will always convert properly into an int.
166             case Types.INTEGER:
167                 return true;
168             case Types.SMALLINT:
169                 return true;
170             case Types.TINYINT:
171                 return true;
172 
173                 // --- Potential conversion loss ---
174 
175                 // BIGINT values may lose data on conversion.
176                 // TODO: Add a flag to allow/dissalow this.
177             case Types.BIGINT:
178                 return true;
179                 // NUMERIC values may lose data on conversion (depending on the
180                 // size of the numeric value).
181                 // TODO: Add a flag to allow/disallow this.
182             case Types.NUMERIC:
183                 return true;
184                 // DECIMAL values may lose data on conversion (depending on the
185                 // size of the numeric value).
186                 // TODO: Add a flag to allow/disallow this.
187             case Types.DECIMAL:
188                 return true;
189 
190                 // --- All other types ---
191                 // All other types are incompatible.
192             default:
193                 return false;
194         }
195     }
196 
197     /***
198      * Returns true if the column is a type compatible with 'String'.
199      *
200      * @param dataType The data type (see java.sql.Types)
201      * @return boolean - True if the column is a String-compatible column.
202      * @see java.sql.Types
203      */
204     public static boolean isStringType(int dataType) {
205         if (log.isDebugEnabled())
206             log.debug("isStringType() : dataType = " + dataType);
207         switch (dataType) {
208             // --- Lossless conversion ---
209 
210             case Types.CHAR:
211                 return true;
212             case Types.VARCHAR:
213                 return true;
214 
215                 // --- All other types ---
216                 // All other types are incompatible.
217             default:
218                 return false;
219         }
220     }
221 }