001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.dataobjs;
034:
035: import java.sql.DatabaseMetaData;
036: import java.sql.Types;
037: import java.sql.Timestamp;
038:
039: import java.text.ParseException;
040: import java.text.SimpleDateFormat;
041: import java.math.BigDecimal;
042:
043: /**
044: * <p>Object representing metadata for a database table column.</p>
045: * @author Sergio Montoro Ten
046: * @version 3.0
047: */
048:
049: public final class DBColumn {
050:
051: public DBColumn() {
052: }
053:
054: public DBColumn(String sTable, String sColName, short iColType,
055: String sColType, int iPrecision, int iDecDigits,
056: int iNullable, int iColPos)
057:
058: {
059: sName = sColName;
060: iPosition = iColPos;
061: sTableName = sTable;
062: iSQLType = iColType;
063: sSQLTypeName = sColType;
064: iMaxSize = iPrecision;
065: iDecimalDigits = iDecDigits;
066: bNullable = (iNullable == DatabaseMetaData.columnNullable);
067: }
068:
069: //-----------------------------------------------------------
070:
071: /**
072: *
073: * @return Column Name
074: */
075: public String getName() {
076: return sName;
077: }
078:
079: /**
080: * Set column name
081: * @param sColName String
082: */
083: public void setName(String sColName) {
084: sName = sColName;
085: }
086:
087: /**
088: *
089: * @return Column Position (starting at column 1)
090: */
091: public int getPosition() {
092: return iPosition;
093: }
094:
095: /**
096: * Set column position
097: * @param iPos int
098: */
099: public void setPosition(int iPos) {
100: iPosition = iPos;
101: }
102:
103: /**
104: *
105: * @return Name of table containing this column
106: */
107: public String getTableName() {
108: return sTableName;
109: }
110:
111: /**
112: *
113: * @return Column SQL Type
114: * @see java.sql.Types
115: */
116:
117: public short getSqlType() {
118: return iSQLType;
119: }
120:
121: /**
122: * Set SQL type for this column
123: * @param iType short
124: */
125: public void setSqlType(short iType) {
126: iSQLType = iType;
127: sSQLTypeName = DBColumn.typeName(iSQLType);
128: }
129:
130: public void setSqlType(int iType) {
131: iSQLType = (short) iType;
132: sSQLTypeName = DBColumn.typeName(iSQLType);
133: }
134:
135: /**
136: *
137: * @return SQL Type Name
138: */
139: public String getSqlTypeName() {
140: return sSQLTypeName;
141: }
142:
143: /**
144: *
145: * @return Column Precision
146: */
147:
148: public int getPrecision() {
149: return iMaxSize;
150: }
151:
152: /**
153: *
154: * @return Decimal Digits
155: */
156:
157: public int getDecimalDigits() {
158: return iDecimalDigits;
159: }
160:
161: /**
162: *
163: * @return Allows NULLs?
164: */
165:
166: public boolean isNullable() {
167: return bNullable;
168: }
169:
170: //-----------------------------------------------------------
171:
172: public void setDateFormat(String sFmt)
173: throws IllegalArgumentException {
174: oDtFmt = new SimpleDateFormat(sFmt);
175: }
176:
177: //-----------------------------------------------------------
178:
179: public SimpleDateFormat getDateFormat() {
180: return oDtFmt;
181: }
182:
183: //-----------------------------------------------------------
184:
185: /**
186: * Get SQL type name from its integer identifier
187: * @param iSQLtype int
188: * @return String
189: */
190: public static String typeName(int iSQLtype) {
191: switch (iSQLtype) {
192: case Types.BIGINT:
193: return "BIGINT";
194: case Types.BINARY:
195: return "BINARY";
196: case Types.BIT:
197: return "BIT";
198: case Types.BLOB:
199: return "BLOB";
200: case Types.BOOLEAN:
201: return "BOOLEAN";
202: case Types.CHAR:
203: return "CHAR";
204: case Types.CLOB:
205: return "CLOB";
206: case Types.DATE:
207: return "DATE";
208: case Types.DECIMAL:
209: return "DECIMAL";
210: case Types.DOUBLE:
211: return "DOUBLE";
212: case Types.FLOAT:
213: return "FLOAT";
214: case Types.INTEGER:
215: return "INTEGER";
216: case Types.LONGVARBINARY:
217: return "LONGVARBINARY";
218: case Types.LONGVARCHAR:
219: return "LONGVARCHAR";
220: case Types.NULL:
221: return "NULL";
222: case Types.NUMERIC:
223: return "NUMERIC";
224: case Types.REAL:
225: return "REAL";
226: case Types.SMALLINT:
227: return "SMALLINT";
228: case Types.TIME:
229: return "TIME";
230: case Types.TIMESTAMP:
231: return "TIMESTAMP";
232: case Types.TINYINT:
233: return "TINYINT";
234: case Types.VARBINARY:
235: return "VARBINARY";
236: case Types.VARCHAR:
237: return "VARCHAR";
238: default:
239: return "OTHER";
240: }
241: }
242:
243: //-----------------------------------------------------------
244:
245: /**
246: * Get SQL type identifier from its name
247: * @param sToken String
248: * @return String
249: */
250:
251: public static int getSQLType(String sToken) {
252: int iSQLType;
253: if (sToken.equalsIgnoreCase("VARCHAR"))
254: iSQLType = Types.VARCHAR;
255: else if (sToken.equalsIgnoreCase("CHAR"))
256: iSQLType = Types.CHAR;
257: else if (sToken.equalsIgnoreCase("SMALLINT"))
258: iSQLType = Types.SMALLINT;
259: else if (sToken.equalsIgnoreCase("INTEGER"))
260: iSQLType = Types.INTEGER;
261: else if (sToken.equalsIgnoreCase("FLOAT"))
262: iSQLType = Types.FLOAT;
263: else if (sToken.equalsIgnoreCase("DOUBLE"))
264: iSQLType = Types.DOUBLE;
265: else if (sToken.equalsIgnoreCase("NUMERIC"))
266: iSQLType = Types.NUMERIC;
267: else if (sToken.equalsIgnoreCase("DECIMAL"))
268: iSQLType = Types.DECIMAL;
269: else if (sToken.equalsIgnoreCase("DATE"))
270: iSQLType = Types.DATE;
271: else if (sToken.equalsIgnoreCase("TIMESTAMP"))
272: iSQLType = Types.TIMESTAMP;
273: else if (sToken.equalsIgnoreCase("DATETIME"))
274: iSQLType = Types.TIMESTAMP;
275: else if (sToken.equalsIgnoreCase("NVARCHAR"))
276: iSQLType = Types.VARCHAR;
277: else if (sToken.equalsIgnoreCase("VARCHAR2"))
278: iSQLType = Types.VARCHAR;
279: else if (sToken.equalsIgnoreCase("LONGVARCHAR"))
280: iSQLType = Types.LONGVARCHAR;
281: else if (sToken.equalsIgnoreCase("LONG"))
282: iSQLType = Types.LONGVARCHAR;
283: else if (sToken.equalsIgnoreCase("TEXT"))
284: iSQLType = Types.LONGVARCHAR;
285: else if (sToken.equalsIgnoreCase("LONGVARBINARY"))
286: iSQLType = Types.LONGVARBINARY;
287: else if (sToken.equalsIgnoreCase("LONG RAW"))
288: iSQLType = Types.LONGVARBINARY;
289: else if (sToken.equalsIgnoreCase("BLOB"))
290: iSQLType = Types.BLOB;
291: else if (sToken.equalsIgnoreCase("CLOB"))
292: iSQLType = Types.CLOB;
293: else
294: iSQLType = Types.NULL;
295: return iSQLType;
296: }
297:
298: // -------------------------------------------------
299:
300: public Object convert(String sIn) throws NumberFormatException,
301: ParseException, NullPointerException {
302: if (sIn == null)
303: return null;
304: if (sIn.length() == 0)
305: return null;
306: switch (iSQLType) {
307: case Types.SMALLINT:
308: return new Short(sIn);
309: case Types.INTEGER:
310: return new Integer(sIn);
311: case Types.FLOAT:
312: return new Float(sIn);
313: case Types.DOUBLE:
314: return new Double(sIn);
315: case Types.DECIMAL:
316: case Types.NUMERIC:
317: return new BigDecimal(sIn);
318: case Types.DATE:
319: return oDtFmt.parse(sIn);
320: case Types.TIMESTAMP:
321: return new Timestamp(oDtFmt.parse(sIn).getTime());
322: default:
323: return sIn;
324: }
325: } // convert
326:
327: //-----------------------------------------------------------
328:
329: private String sName;
330: private int iPosition;
331: private String sTableName;
332: private short iSQLType;
333: private String sSQLTypeName;
334: private int iMaxSize;
335: private int iDecimalDigits;
336: private boolean bNullable;
337: private SimpleDateFormat oDtFmt;
338: } // DBColumn
|