KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > dataobjs > DBColumn


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.dataobjs;
34
35 import java.sql.DatabaseMetaData JavaDoc;
36 import java.sql.Types JavaDoc;
37
38 import java.text.ParseException JavaDoc;
39 import java.text.SimpleDateFormat JavaDoc;
40 import java.math.BigDecimal JavaDoc;
41
42 /**
43  * <p>Object representing metadata for a database table column.</p>
44  * @author Sergio Montoro Ten
45  * @version 3.0
46  */

47
48 public final class DBColumn {
49
50   public DBColumn() { }
51
52   public DBColumn(String JavaDoc sTable, String JavaDoc sColName,
53                   short iColType, String JavaDoc sColType,
54                   int iPrecision, int iDecDigits,
55                   int iNullable, int iColPos)
56
57   {
58     sName = sColName;
59     iPosition = iColPos;
60     sTableName = sTable;
61     iSQLType = iColType;
62     sSQLTypeName = sColType;
63     iMaxSize = iPrecision;
64     iDecimalDigits = iDecDigits;
65     bNullable = (iNullable==DatabaseMetaData.columnNullable);
66   }
67
68   //-----------------------------------------------------------
69

70   /**
71    *
72    * @return Column Name
73    */

74   public String JavaDoc getName() { return sName; }
75
76   /**
77    * Set column name
78    * @param sColName String
79    */

80   public void setName(String JavaDoc sColName) { sName=sColName; }
81
82   /**
83    *
84    * @return Column Position (starting at column 1)
85    */

86   public int getPosition() { return iPosition; }
87
88   /**
89    * Set column position
90    * @param iPos int
91    */

92   public void setPosition(int iPos) { iPosition=iPos; }
93
94   /**
95    *
96    * @return Name of table containing this column
97    */

98   public String JavaDoc getTableName() { return sTableName; }
99
100   /**
101    *
102    * @return Column SQL Type
103    * @see java.sql.Types
104    */

105
106   public short getSqlType() { return iSQLType; }
107
108   /**
109    * Set SQL type for this column
110    * @param iType short
111    */

112   public void setSqlType(short iType) {
113     iSQLType=iType;
114     sSQLTypeName=DBColumn.typeName(iSQLType);
115   }
116
117   public void setSqlType(int iType) {
118     iSQLType=(short) iType;
119     sSQLTypeName=DBColumn.typeName(iSQLType);
120   }
121
122   /**
123    *
124    * @return SQL Type Name
125    */

126   public String JavaDoc getSqlTypeName() { return sSQLTypeName; }
127
128   /**
129    *
130    * @return Column Precision
131    */

132
133   public int getPrecision() { return iMaxSize; }
134
135   /**
136    *
137    * @return Decimal Digits
138    */

139
140   public int getDecimalDigits() { return iDecimalDigits; }
141
142   /**
143    *
144    * @return Allows NULLs?
145    */

146
147   public boolean isNullable() { return bNullable; }
148
149   //-----------------------------------------------------------
150

151   public void setDateFormat(String JavaDoc sFmt) throws IllegalArgumentException JavaDoc {
152     oDtFmt = new SimpleDateFormat JavaDoc(sFmt);
153   }
154
155   //-----------------------------------------------------------
156

157   public SimpleDateFormat JavaDoc getDateFormat() {
158     return oDtFmt;
159   }
160
161   //-----------------------------------------------------------
162

163   /**
164    * Get SQL type name from its integer identifier
165    * @param iSQLtype int
166    * @return String
167    */

168   public static String JavaDoc typeName(int iSQLtype) {
169     switch (iSQLtype) {
170       case Types.BIGINT:
171         return "BIGINT";
172       case Types.BINARY:
173         return "BINARY";
174       case Types.BIT:
175         return "BIT";
176       case Types.BLOB:
177         return "BLOB";
178       case Types.BOOLEAN:
179         return "BOOLEAN";
180       case Types.CHAR:
181         return "CHAR";
182       case Types.CLOB:
183         return "CLOB";
184       case Types.DATE:
185         return "DATE";
186       case Types.DECIMAL:
187         return "DECIMAL";
188       case Types.DOUBLE:
189         return "DOUBLE";
190       case Types.FLOAT:
191         return "FLOAT";
192       case Types.INTEGER:
193         return "INTEGER";
194       case Types.LONGVARBINARY:
195         return "LONGVARBINARY";
196       case Types.LONGVARCHAR:
197         return "LONGVARCHAR";
198       case Types.NULL:
199         return "NULL";
200       case Types.NUMERIC:
201         return "NUMERIC";
202       case Types.REAL:
203         return "REAL";
204       case Types.SMALLINT:
205         return "SMALLINT";
206       case Types.TIME:
207         return "TIME";
208       case Types.TIMESTAMP:
209         return "TIMESTAMP";
210       case Types.TINYINT:
211         return "TINYINT";
212       case Types.VARBINARY:
213         return "VARBINARY";
214       case Types.VARCHAR:
215         return "VARCHAR";
216       default:
217         return "OTHER";
218     }
219   }
220
221   //-----------------------------------------------------------
222

223   /**
224    * Get SQL type identifier from its name
225    * @param sToken String
226    * @return String
227    */

228
229   public static int getSQLType (String JavaDoc sToken) {
230     int iSQLType;
231     if (sToken.equalsIgnoreCase("VARCHAR"))
232       iSQLType = Types.VARCHAR;
233     else if (sToken.equalsIgnoreCase("CHAR"))
234       iSQLType = Types.CHAR;
235     else if (sToken.equalsIgnoreCase("SMALLINT"))
236       iSQLType = Types.SMALLINT;
237     else if (sToken.equalsIgnoreCase("INTEGER"))
238       iSQLType = Types.INTEGER;
239     else if (sToken.equalsIgnoreCase("FLOAT"))
240       iSQLType = Types.FLOAT;
241     else if (sToken.equalsIgnoreCase("DOUBLE"))
242       iSQLType = Types.DOUBLE;
243     else if (sToken.equalsIgnoreCase("NUMERIC"))
244       iSQLType = Types.NUMERIC;
245     else if (sToken.equalsIgnoreCase("DECIMAL"))
246       iSQLType = Types.DECIMAL;
247     else if (sToken.equalsIgnoreCase("DATE"))
248       iSQLType = Types.DATE;
249     else if (sToken.equalsIgnoreCase("TIMESTAMP"))
250       iSQLType = Types.TIMESTAMP;
251     else if (sToken.equalsIgnoreCase("DATETIME"))
252       iSQLType = Types.TIMESTAMP;
253     else if (sToken.equalsIgnoreCase("NVARCHAR"))
254       iSQLType = Types.VARCHAR;
255     else if (sToken.equalsIgnoreCase("VARCHAR2"))
256       iSQLType = Types.VARCHAR;
257     else if (sToken.equalsIgnoreCase("LONGVARCHAR"))
258       iSQLType = Types.LONGVARCHAR;
259     else if (sToken.equalsIgnoreCase("LONG"))
260       iSQLType = Types.LONGVARCHAR;
261     else if (sToken.equalsIgnoreCase("TEXT"))
262       iSQLType = Types.LONGVARCHAR;
263     else if (sToken.equalsIgnoreCase("LONGVARBINARY"))
264       iSQLType = Types.LONGVARBINARY;
265     else if (sToken.equalsIgnoreCase("LONG RAW"))
266       iSQLType = Types.LONGVARBINARY;
267     else if (sToken.equalsIgnoreCase("BLOB"))
268       iSQLType = Types.BLOB;
269     else if (sToken.equalsIgnoreCase("CLOB"))
270       iSQLType = Types.CLOB;
271     else
272       iSQLType = Types.NULL;
273     return iSQLType;
274   }
275
276   // -------------------------------------------------
277

278   public Object JavaDoc convert(String JavaDoc sIn)
279     throws NumberFormatException JavaDoc,ParseException JavaDoc,NullPointerException JavaDoc {
280     if (sIn==null) return null;
281     if (sIn.length()==0) return null;
282     switch (iSQLType) {
283       case Types.SMALLINT:
284         return new Short JavaDoc(sIn);
285       case Types.INTEGER:
286         return new Integer JavaDoc(sIn);
287       case Types.FLOAT:
288         return new Float JavaDoc(sIn);
289       case Types.DOUBLE:
290         return new Double JavaDoc(sIn);
291       case Types.DECIMAL:
292       case Types.NUMERIC:
293         return new BigDecimal JavaDoc(sIn);
294       case Types.DATE:
295       case Types.TIMESTAMP:
296         return oDtFmt.parse(sIn);
297       default:
298         return sIn;
299     }
300   } // convert
301

302   //-----------------------------------------------------------
303

304   private String JavaDoc sName;
305   private int iPosition;
306   private String JavaDoc sTableName;
307   private short iSQLType;
308   private String JavaDoc sSQLTypeName;
309   private int iMaxSize;
310   private int iDecimalDigits;
311   private boolean bNullable;
312   private SimpleDateFormat JavaDoc oDtFmt;
313 } // DBColumn
314
Popular Tags