KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > global > ColumnDescription


1 /**
2  * com.mckoi.database.global.ColumnDescription 19 May 1998
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.global;
26
27 import java.io.*;
28 import java.util.Date JavaDoc;
29 import java.math.BigDecimal JavaDoc;
30
31 /**
32  * This is a description of a column and the data it stores. Specifically it
33  * stores the 'type' as defined in the Types class, the 'size' if the column
34  * cells may be different lengths (eg, string), the name of the column, whether
35  * the column set must contain unique elements, and whether a cell may be added
36  * that is null.
37  *
38  * @author Tobias Downer
39  */

40
41 public class ColumnDescription {
42
43 // static final long serialVersionUID = 8210197301596138014L;
44

45   /**
46    * The name of the field.
47    */

48   private String JavaDoc name;
49
50   /**
51    * The type of the field, from the Types object.
52    */

53   private int type;
54
55   /**
56    * The size of the type. The meaning of this field changes depending on the
57    * type. For example, the size of an SQL NUMERIC represents the number of
58    * digits in the value (precision).
59    */

60   private int size;
61
62   /**
63    * The scale of a numerical value. This represents the number of digits to
64    * the right of the decimal point. The number is rounded to this scale
65    * in arithmatic operations. By default, the scale is '10'
66    */

67   private int scale = -1;
68
69   /**
70    * The SQL type as defined in java.sql.Types. This is required to emulate
71    * the various SQL types. The value is initialised to -9332 to indicate
72    * the sql type has not be defined.
73    */

74   private int sql_type = -9332;
75
76   /**
77    * If true, the field may not be null. If false, the column may contain
78    * no information. This is enforced at the parse stage when adding or
79    * altering a table.
80    */

81   private boolean not_null;
82
83   /**
84    * If true, the field may only contain unique values. This is enforced at
85    * the parse stage when adding or altering a table.
86    */

87   private boolean unique;
88
89   /**
90    * This represents the 'unique_group' that this column is in. If two
91    * columns in a table belong to the same unique_group, then the specific
92    * combination of the groups columns can not exist more than once in the
93    * table.
94    * A value of -1 means the column does not belong to any unique group.
95    */

96   private int unique_group;
97
98   /**
99    * The Constructors if the type does require a size.
100    */

101   public ColumnDescription(String JavaDoc name, int type, int size, boolean not_null) {
102     this.name = name;
103     this.type = type;
104     this.size = size;
105     this.not_null = not_null;
106     this.unique = false;
107     this.unique_group = -1;
108   }
109
110   public ColumnDescription(String JavaDoc name, int type, boolean not_null) {
111     this(name, type, -1, not_null);
112   }
113
114   public ColumnDescription(ColumnDescription cd) {
115     this(cd.getName(), cd.getType(), cd.getSize(), cd.isNotNull());
116     if (cd.isUnique()) {
117       setUnique();
118     }
119     setUniqueGroup(cd.getUniqueGroup());
120     setScale(cd.getScale());
121     setSQLType(cd.getSQLType());
122   }
123
124   public ColumnDescription(String JavaDoc name, ColumnDescription cd) {
125     this(name, cd.getType(), cd.getSize(), cd.isNotNull());
126     if (cd.isUnique()) {
127       setUnique();
128     }
129     setUniqueGroup(cd.getUniqueGroup());
130     setScale(cd.getScale());
131     setSQLType(cd.getSQLType());
132   }
133
134   /**
135    * Sets this column to unique.
136    * NOTE: This can only happen during the setup of the object. Unpredictable
137    * results will occur otherwise.
138    */

139   public void setUnique() {
140     this.unique = true;
141   }
142
143   /**
144    * Sets the column to belong to the specified unique group in the table.
145    * Setting to -1 sets the column to no unique group.
146    * NOTE: This can only happen during the setup of the object. Unpredictable
147    * results will occur otherwise.
148    */

149   public void setUniqueGroup(int group) {
150     this.unique_group = group;
151   }
152
153   /**
154    * Sets the SQL type for this ColumnDescription object. This is only used
155    * to emulate SQL types in the database. They are mapped to the simpler
156    * internal types as follows:<p>
157    * <pre>
158    * DB_STRING := CHAR, VARCHAR, LONGVARCHAR
159    * DB_NUMERIC := TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, REAL,
160    * DOUBLE, NUMERIC, DECIMAL
161    * DB_DATE := DATE, TIME, TIMESTAMP
162    * DB_BOOLEAN := BIT
163    * DB_BLOB := BINARY, VARBINARY, LONGVARBINARY
164    * DB_OBJECT := JAVA_OBJECT
165    * </pre>
166    */

167   public void setSQLType(int sql_type) {
168     this.sql_type = sql_type;
169   }
170
171   /**
172    * Sets the scale of the numerical values stored.
173    */

174   public void setScale(int scale) {
175     this.scale = scale;
176   }
177
178   /**
179    * Returns the name of the field. The field type returned should be
180    * 'ZIP' or 'Address1'. To resolve to the tables type, we must append
181    * an additional 'Company.' or 'Customer.' string to the front.
182    */

183   public String JavaDoc getName() {
184     return name;
185   }
186
187   /**
188    * Returns an integer representing the type of the field. The types are
189    * outlined in com.mckoi.database.global.Types.
190    */

191   public int getType() {
192     return type;
193   }
194
195   /**
196    * Returns true if this column is a numeric type.
197    */

198   public boolean isNumericType() {
199     return (type == Types.DB_NUMERIC);
200   }
201
202   /**
203    * Returns a value from java.sql.Type that is the SQL type defined for this
204    * column. It's possible that the column may not have had the SQL type
205    * set in which case we map from the internal db type ( DB_??? ) to the
206    * most logical sql type.
207    */

208   public int getSQLType() {
209     if (sql_type == -9332) {
210       // If sql type is unknown find from internal type
211
if (type == Types.DB_NUMERIC) {
212         return SQLTypes.NUMERIC;
213       }
214       else if (type == Types.DB_STRING) {
215         return SQLTypes.LONGVARCHAR;
216       }
217       else if (type == Types.DB_BOOLEAN) {
218         return SQLTypes.BIT;
219       }
220       else if (type == Types.DB_TIME) {
221         return SQLTypes.TIMESTAMP;
222       }
223       else if (type == Types.DB_BLOB) {
224         return SQLTypes.LONGVARBINARY;
225       }
226       else if (type == Types.DB_OBJECT) {
227         return SQLTypes.JAVA_OBJECT;
228       }
229       else {
230         throw new Error JavaDoc("Unrecognised internal type.");
231       }
232     }
233     return sql_type;
234   }
235
236   /**
237    * Returns the name (as a string) of the SQL type or null if the type is
238    * not understood.
239    */

240   public String JavaDoc getSQLTypeName() {
241     int type = getSQLType();
242     switch (type) {
243       case(SQLTypes.BIT):
244         return "BIT";
245       case(SQLTypes.TINYINT):
246         return "TINYINT";
247       case(SQLTypes.SMALLINT):
248         return "SMALLINT";
249       case(SQLTypes.INTEGER):
250         return "INTEGER";
251       case(SQLTypes.BIGINT):
252         return "BIGINT";
253       case(SQLTypes.FLOAT):
254         return "FLOAT";
255       case(SQLTypes.REAL):
256         return "REAL";
257       case(SQLTypes.DOUBLE):
258         return "DOUBLE";
259       case(SQLTypes.NUMERIC):
260         return "NUMERIC";
261       case(SQLTypes.DECIMAL):
262         return "DECIMAL";
263       case(SQLTypes.CHAR):
264         return "CHAR";
265       case(SQLTypes.VARCHAR):
266         return "VARCHAR";
267       case(SQLTypes.LONGVARCHAR):
268         return "LONGVARCHAR";
269       case(SQLTypes.DATE):
270         return "DATE";
271       case(SQLTypes.TIME):
272         return "TIME";
273       case(SQLTypes.TIMESTAMP):
274         return "TIMESTAMP";
275       case(SQLTypes.BINARY):
276         return "BINARY";
277       case(SQLTypes.VARBINARY):
278         return "VARBINARY";
279       case(SQLTypes.LONGVARBINARY):
280         return "LONGVARBINARY";
281       case(SQLTypes.NULL):
282         return "NULL";
283       case(SQLTypes.OTHER):
284         return "OTHER";
285       case(SQLTypes.JAVA_OBJECT):
286         return "JAVA_OBJECT";
287       case(SQLTypes.DISTINCT):
288         return "DISTINCT";
289       case(SQLTypes.STRUCT):
290         return "STRUCT";
291       case(SQLTypes.ARRAY):
292         return "ARRAY";
293       case(SQLTypes.BLOB):
294         return "BLOB";
295       case(SQLTypes.CLOB):
296         return "CLOB";
297       case(SQLTypes.REF):
298         return "REF";
299       case(SQLTypes.BOOLEAN):
300         return "BOOLEAN";
301       default:
302         return null;
303     }
304   }
305
306
307
308   /**
309    * Returns the class of Java object for this field.
310    */

311   public Class JavaDoc classType() {
312     return TypeUtil.toClass(type);
313 // if (type == Types.DB_STRING) {
314
// return String.class;
315
// }
316
// else if (type == Types.DB_NUMERIC) {
317
// return BigDecimal.class;
318
// }
319
// else if (type == Types.DB_TIME) {
320
// return Date.class;
321
// }
322
// else if (type == Types.DB_BOOLEAN) {
323
// return Boolean.class;
324
// }
325
// else {
326
// throw new Error("Unknown type.");
327
// }
328
}
329
330   /**
331    * Returns the size of the given field. This is only applicable to a few
332    * of the types, ie VARCHAR.
333    */

334   public int getSize() {
335     return size;
336   }
337
338   /**
339    * If this is a number, returns the scale of the field.
340    */

341   public int getScale() {
342     return scale;
343   }
344
345   /**
346    * Determines whether the field can contain a null value or not. Returns
347    * true if it is required for the column to contain data.
348    */

349   public boolean isNotNull() {
350     return not_null;
351   }
352
353   /**
354    * Determines whether the field can contain two items that are identical.
355    * Returns true if each element must be unique.
356    */

357   public boolean isUnique() {
358     return unique;
359   }
360
361   /**
362    * Returns the unique group that this column is in. If it does not belong
363    * to a unique group then the value -1 is returned.
364    */

365   public int getUniqueGroup() {
366     return unique_group;
367   }
368
369   /**
370    * Returns true if the type of the field is searchable. Searchable means
371    * that the database driver can quantify it, as in determine if a given
372    * object of the same type is greater, equal or less. We can not quantify
373    * BLOB types.
374    */

375   public boolean isQuantifiable() {
376     if (type == Types.DB_BLOB ||
377         type == Types.DB_OBJECT) {
378       return false;
379     }
380     return true;
381   }
382
383   /**
384    * The 'equals' method, used to determine equality between column
385    * descriptions.
386    */

387   public boolean equals(Object JavaDoc ob) {
388     ColumnDescription cd = (ColumnDescription) ob;
389     return (name.equals(cd.name) &&
390             type == cd.type &&
391             size == cd.size &&
392             not_null == cd.not_null &&
393             unique == cd.unique &&
394             unique_group == cd.unique_group);
395   }
396
397   /**
398    * Writes this ColumnDescription to the given DataOutputStream.
399    * ( Remember to flush output stream )
400    */

401   public void writeTo(DataOutputStream out) throws IOException {
402     out.writeUTF(name);
403     out.writeInt(type);
404     out.writeInt(size);
405     out.writeBoolean(not_null);
406     out.writeBoolean(unique);
407     out.writeInt(unique_group);
408     out.writeInt(sql_type);
409     out.writeInt(scale);
410   }
411
412   /**
413    * Reads a ColumnDescription from the given DataInputStream and returns
414    * a new instance of it.
415    */

416   public static ColumnDescription readFrom(DataInputStream in)
417                                                           throws IOException {
418     String JavaDoc name = in.readUTF();
419     int type = in.readInt();
420     int size = in.readInt();
421     boolean not_null = in.readBoolean();
422     boolean unique = in.readBoolean();
423     int unique_group = in.readInt();
424
425     ColumnDescription col_desc = new ColumnDescription(name,
426                                                        type, size, not_null);
427     if (unique) col_desc.setUnique();
428     col_desc.setUniqueGroup(unique_group);
429     col_desc.setSQLType(in.readInt());
430     col_desc.setScale(in.readInt());
431
432     return col_desc;
433   }
434
435 }
436
Popular Tags