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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
164
165
166 case Types.INTEGER:
167 return true;
168 case Types.SMALLINT:
169 return true;
170 case Types.TINYINT:
171 return true;
172
173
174
175
176
177 case Types.BIGINT:
178 return true;
179
180
181
182 case Types.NUMERIC:
183 return true;
184
185
186
187 case Types.DECIMAL:
188 return true;
189
190
191
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
209
210 case Types.CHAR:
211 return true;
212 case Types.VARCHAR:
213 return true;
214
215
216
217 default:
218 return false;
219 }
220 }
221 }