1 package com.workingdogs.village;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 /*** name of the column */
36 private String name = "";
37
38 /*** what java.sql.Type is this column? */
39 private int columnType = Types.LONGVARCHAR;
40
41 /*** name of table that this column belongs to */
42 private String tableName = "";
43
44 /*** is null allowed for this column? */
45 private boolean nullAllowed = false;
46
47 /*** is this a read only column? */
48 private boolean readOnly = false;
49
50 /***
51 * constructor
52 */
53 public Column()
54 {
55 this.name = "";
56 this.tableName = "";
57 this.columnType = Types.LONGVARCHAR;
58 this.nullAllowed = false;
59 this.readOnly = false;
60 }
61
62 /***
63 * internal package method for populating a Column instance
64 *
65 * @param rsmd TODO: DOCUMENT ME!
66 * @param colNum TODO: DOCUMENT ME!
67 * @param tableName TODO: DOCUMENT ME!
68 * @param columnName The name of the column
69 *
70 * @throws SQLException TODO: DOCUMENT ME!
71 */
72 public void populate(ResultSetMetaData rsmd, int colNum, String tableName, String columnName)
73 throws SQLException
74 {
75 this.name = columnName;
76 this.tableName = tableName;
77
78 this.columnType = rsmd.getColumnType(colNum);
79 this.nullAllowed = rsmd.isNullable(colNum) == 1;
80 }
81
82 /***
83 * internal package method for populating a Column instance
84 *
85 * @param tableName The name of the table
86 * @param columnName The name of the column
87 * @param columnTypeName The Data source dependent type name
88 * @param columnType The SQL type from java.sql.Types
89 * @param isNullable true if NULL allowed.
90 *
91 */
92 public void populate(String tableName, String columnName, String columnTypeName,
93 int columnType, boolean isNullable)
94 {
95 this.name = columnName;
96 this.tableName = tableName;
97 this.columnType = columnType;
98 this.nullAllowed = isNullable;
99 }
100
101 /***
102 * the name of the column
103 *
104 * @return the name of the column
105 */
106 public String name()
107 {
108 return this.name;
109 }
110
111 /***
112 * the data type of a column
113 *
114 * @return the java.sql.Types enum
115 */
116 public int typeEnum()
117 {
118 return this.columnType;
119 }
120
121 /***
122 * does this column allow null?
123 *
124 * @return whether or not the column has null Allowed
125 */
126 public boolean nullAllowed()
127 {
128 return this.nullAllowed;
129 }
130
131 /***
132 * is this column read only?
133 *
134 * @return whether or not this column is read only
135 */
136 public boolean readOnly()
137 {
138 return this.readOnly;
139 }
140
141 /***
142 * the type of the column as a string
143 *
144 * @return the type of the column as a string
145 */
146 public String type()
147 {
148 if (isBoolean())
149 {
150 return "BOOLEAN";
151 }
152 else if (isByte())
153 {
154 return "BYTE";
155 }
156 else if (isShort())
157 {
158 return "SHORT";
159 }
160 else if (isInt())
161 {
162 return "INTEGER";
163 }
164 else if (isLong())
165 {
166 return "LONG";
167 }
168 else if (isFloat())
169 {
170 return "FLOAT";
171 }
172 else if (isDouble())
173 {
174 return "DOUBLE";
175 }
176 else if (isBigDecimal())
177 {
178 return "BIGDECIMAL";
179 }
180 else if (isDate())
181 {
182 return "DATE";
183 }
184 else if (isTime())
185 {
186 return "TIME";
187 }
188 else if (isTimestamp())
189 {
190 return "TIMESTAMP";
191 }
192 else if (isString())
193 {
194 return "STRING";
195 }
196 else if (isBinary())
197 {
198 return "BINARY";
199 }
200 else if (isVarBinary())
201 {
202 return "VARBINARY";
203 }
204 else if (isLongVarBinary())
205 {
206 return "LONGVARBINARY";
207 }
208
209 return "UNKNOWN TYPE: " + typeEnum();
210 }
211
212 /***
213 * column isBoolean: -7
214 *
215 * @return TODO: DOCUMENT ME!
216 */
217 public boolean isBoolean()
218 {
219 return this.typeEnum() == Types.BIT;
220 }
221
222 /***
223 * column isBigDecimal: 2 || 3
224 *
225 * @return TODO: DOCUMENT ME!
226 */
227 public boolean isBigDecimal()
228 {
229 return (this.typeEnum() == Types.NUMERIC) || (this.typeEnum() == Types.DECIMAL);
230 }
231
232 /***
233 * column isBinary: -2
234 *
235 * @return TODO: DOCUMENT ME!
236 */
237 public boolean isBinary()
238 {
239 return this.typeEnum() == Types.BINARY;
240 }
241
242 /***
243 * column isByte: -6
244 *
245 * @return TODO: DOCUMENT ME!
246 */
247 public boolean isByte()
248 {
249 return this.typeEnum() == Types.TINYINT;
250 }
251
252 /***
253 * column isBytes: -4 || -3 || -2
254 *
255 * @return TODO: DOCUMENT ME!
256 */
257 public boolean isBytes()
258 {
259 return (this.typeEnum() == Types.LONGVARBINARY)
260 || (this.typeEnum() == Types.VARBINARY)
261 || (this.columnType == Types.BINARY);
262 }
263
264 /***
265 * column isBytes: 91
266 *
267 * @return TODO: DOCUMENT ME!
268 */
269 public boolean isDate()
270 {
271 return this.typeEnum() == Types.DATE;
272 }
273
274 /***
275 * column isDouble: 6 || 8
276 *
277 * @return TODO: DOCUMENT ME!
278 */
279 public boolean isDouble()
280 {
281 return (this.typeEnum() == Types.FLOAT) || (this.typeEnum() == Types.DOUBLE);
282 }
283
284 /***
285 * column isFloat: 7
286 *
287 * @return TODO: DOCUMENT ME!
288 */
289 public boolean isFloat()
290 {
291 return this.typeEnum() == Types.REAL;
292 }
293
294 /***
295 * column isInt: 4
296 *
297 * @return TODO: DOCUMENT ME!
298 */
299 public boolean isInt()
300 {
301 return this.typeEnum() == Types.INTEGER;
302 }
303
304 /***
305 * column isLong: -5
306 *
307 * @return TODO: DOCUMENT ME!
308 */
309 public boolean isLong()
310 {
311 return this.typeEnum() == Types.BIGINT;
312 }
313
314 /***
315 * column isShort: 5
316 *
317 * @return TODO: DOCUMENT ME!
318 */
319 public boolean isShort()
320 {
321 return this.typeEnum() == Types.SMALLINT;
322 }
323
324 /***
325 * column isString: -1 || -11 || 12
326 *
327 * @return TODO: DOCUMENT ME!
328 */
329 public boolean isString()
330 {
331 return (this.typeEnum() == Types.LONGVARCHAR)
332 || (this.typeEnum() == Types.VARCHAR)
333 || (this.typeEnum() == 11);
334 }
335
336 /***
337 * column isTime: 92
338 *
339 * @return TODO: DOCUMENT ME!
340 */
341 public boolean isTime()
342 {
343 return this.typeEnum() == Types.TIME;
344 }
345
346 /***
347 * column isTimestamp: 93
348 *
349 * @return TODO: DOCUMENT ME!
350 */
351 public boolean isTimestamp()
352 {
353 return this.typeEnum() == Types.TIMESTAMP;
354 }
355
356 /***
357 * column isVarBinary: -3
358 *
359 * @return TODO: DOCUMENT ME!
360 */
361 public boolean isVarBinary()
362 {
363 return this.typeEnum() == Types.VARBINARY;
364 }
365
366 /***
367 * column isLongVarBinary: -4
368 *
369 * @return TODO: DOCUMENT ME!
370 */
371 public boolean isLongVarBinary()
372 {
373 return this.typeEnum() == Types.LONGVARBINARY;
374 }
375
376 /***
377 * TODO: DOCUMENT ME!
378 *
379 * @return TODO: DOCUMENT ME!
380 */
381 public String getTableName()
382 {
383 return tableName;
384 }
385 }